rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
author Lubomir Nerad <lubomir.nerad@oracle.com>
Fri, 26 Apr 2013 18:48:34 +0200
branchclosure
changeset 1029 b1fe994d4267
parent 1020 a6bacea2518f
child 1082 977cc6141083
permissions -rw-r--r--
Partially working extension modules
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.vm4brwsr;
    19 
    20 import java.io.IOException;
    21 import java.io.InputStream;
    22 import static org.apidesign.vm4brwsr.ByteCodeParser.*;
    23 
    24 /** Translator of the code inside class files to JavaScript.
    25  *
    26  * @author Jaroslav Tulach <jtulach@netbeans.org>
    27  */
    28 abstract class ByteCodeToJavaScript {
    29     private ClassData jc;
    30     final Appendable out;
    31 
    32     protected ByteCodeToJavaScript(Appendable out) {
    33         this.out = out;
    34     }
    35     
    36     /* Collects additional required resources.
    37      * 
    38      * @param internalClassName classes that were referenced and should be loaded in order the
    39      *   generated JavaScript code works properly. The names are in internal 
    40      *   JVM form so String is <code>java/lang/String</code>. 
    41      */
    42     protected abstract boolean requireReference(String internalClassName);
    43     
    44     /*
    45      * @param resourcePath name of resources to read
    46      */
    47     protected abstract void requireScript(String resourcePath) throws IOException;
    48     
    49     /** Allows subclasses to redefine what field a function representing a
    50      * class gets assigned. By default it returns the suggested name followed
    51      * by <code>" = "</code>;
    52      * 
    53      * @param className suggested name of the class
    54      */
    55     /* protected */ String assignClass(String className) {
    56         return className + " = ";
    57     }
    58     /* protected */ String accessClass(String classOperation) {
    59         return classOperation;
    60     }
    61 
    62     protected void declaredClass(ClassData classData, String mangledName)
    63             throws IOException {
    64     }
    65 
    66     /** Prints out a debug message. 
    67      * 
    68      * @param msg the message
    69      * @return true if the message has been printed
    70      * @throws IOException 
    71      */
    72     boolean debug(String msg) throws IOException {
    73         out.append(msg);
    74         return true;
    75     }
    76 
    77     /**
    78      * Converts a given class file to a JavaScript version.
    79      *
    80      * @param classFile input stream with code of the .class file
    81      * @return the initialization code for this class, if any. Otherwise <code>null</code>
    82      * 
    83      * @throws IOException if something goes wrong during read or write or translating
    84      */
    85     
    86     public String compile(InputStream classFile) throws IOException {
    87         this.jc = new ClassData(classFile);
    88         if (jc.getMajor_version() < 50) {
    89             throw new IOException("Can't compile " + jc.getClassName() + ". Class file version " + jc.getMajor_version() + "."
    90                 + jc.getMinor_version() + " - recompile with -target 1.6 (at least)."
    91             );
    92         }
    93         byte[] arrData = jc.findAnnotationData(true);
    94         String[] arr = findAnnotation(arrData, jc, 
    95             "org.apidesign.bck2brwsr.core.ExtraJavaScript", 
    96             "resource", "processByteCode"
    97         );
    98         if (arr != null) {
    99             if (!arr[0].isEmpty()) {
   100                 requireScript(arr[0]);
   101             }
   102             if ("0".equals(arr[1])) {
   103                 return null;
   104             }
   105         }
   106         String[] proto = findAnnotation(arrData, jc, 
   107             "org.apidesign.bck2brwsr.core.JavaScriptPrototype", 
   108             "container", "prototype"
   109         );
   110         StringArray toInitilize = new StringArray();
   111         final String className = className(jc);
   112         out.append("\n\n").append(assignClass(className));
   113         out.append("function ").append(className).append("() {");
   114         out.append("\n  var CLS = ").append(className).append(';');
   115         out.append("\n  if (!CLS.$class) {");
   116         if (proto == null) {
   117             String sc = jc.getSuperClassName(); // with _
   118             out.append("\n    var pp = ").
   119                 append(accessClass(sc.replace('/', '_'))).append("(true);");
   120             out.append("\n    var p = CLS.prototype = pp;");
   121             out.append("\n    var c = p;");
   122             out.append("\n    var sprcls = pp.constructor.$class;");
   123         } else {
   124             out.append("\n    var p = CLS.prototype = ").append(proto[1]).append(";");
   125             if (proto[0] == null) {
   126                 proto[0] = "p";
   127             }
   128             out.append("\n    var c = ").append(proto[0]).append(";");
   129             out.append("\n    var sprcls = null;");
   130         }
   131         for (FieldData v : jc.getFields()) {
   132             if (v.isStatic()) {
   133                 out.append("\n  CLS.").append(v.getName()).append(initField(v));
   134                 out.append("\n  c._").append(v.getName()).append(" = function (v) {")
   135                    .append("  if (arguments.length == 1) CLS.").append(v.getName())
   136                    .append(" = v; return CLS.").
   137                     append(v.getName()).append("; };");
   138             } else {
   139                 out.append("\n  c._").append(v.getName()).append(" = function (v) {")
   140                    .append("  if (arguments.length == 1) this.fld_").
   141                     append(className).append('_').append(v.getName())
   142                    .append(" = v; return this.fld_").
   143                     append(className).append('_').append(v.getName())
   144                    .append("; };");
   145             }
   146 
   147 //            obfuscationDelegate.exportField(out, "c", "_" + v.getName(), v);
   148         }
   149         for (MethodData m : jc.getMethods()) {
   150             byte[] onlyArr = m.findAnnotationData(true);
   151             String[] only = findAnnotation(onlyArr, jc, 
   152                 "org.apidesign.bck2brwsr.core.JavaScriptOnly", 
   153                 "name", "value"
   154             );
   155             if (only != null) {
   156                 if (only[0] != null && only[1] != null) {
   157                     out.append("\n    p.").append(only[0]).append(" = ")
   158                         .append(only[1]).append(";");
   159                 }
   160                 continue;
   161             }
   162             String destObject;
   163             String mn;
   164             out.append("\n    ");
   165             if (m.isStatic()) {
   166                 destObject = "c";
   167                 mn = generateStaticMethod(destObject, m, toInitilize);
   168             } else {
   169                 if (m.isConstructor()) {
   170                     destObject = "CLS";
   171                     mn = generateInstanceMethod(destObject, m);
   172                 } else {
   173                     destObject = "c";
   174                     mn = generateInstanceMethod(destObject, m);
   175                 }
   176             }
   177 //            obfuscationDelegate.exportMethod(out, destObject, mn, m);
   178             byte[] runAnno = m.findAnnotationData(false);
   179             if (runAnno != null) {
   180                 out.append("\n    ").append(destObject).append(".").append(mn).append(".anno = {");
   181                 generateAnno(jc, out, runAnno);
   182                 out.append("\n    };");
   183             }
   184             out.append("\n    ").append(destObject).append(".").append(mn).append(".access = " + m.getAccess()).append(";");
   185             out.append("\n    ").append(destObject).append(".").append(mn).append(".cls = CLS;");
   186         }
   187         out.append("\n    c.constructor = CLS;");
   188         String instOfName = "$instOf_" + className;
   189         out.append("\n    c.").append(instOfName).append(" = true;");
   190 //        obfuscationDelegate.exportJSProperty(out, "c", instOfName);
   191         for (String superInterface : jc.getSuperInterfaces()) {
   192             instOfName = "$instOf_" + superInterface.replace('/', '_');
   193             out.append("\n    c.").append(instOfName).append(" = true;");
   194 //            obfuscationDelegate.exportJSProperty(out, "c", instOfName);
   195         }
   196         out.append("\n    CLS.$class = 'temp';");
   197         out.append("\n    CLS.$class = ");
   198         out.append(accessClass("java_lang_Class(true);"));
   199         out.append("\n    CLS.$class.jvmName = '").append(jc.getClassName()).append("';");
   200         out.append("\n    CLS.$class.superclass = sprcls;");
   201         out.append("\n    CLS.$class.access = ").append(jc.getAccessFlags()+";");
   202         out.append("\n    CLS.$class.cnstr = CLS;");
   203         byte[] classAnno = jc.findAnnotationData(false);
   204         if (classAnno != null) {
   205             out.append("\n    CLS.$class.anno = {");
   206             generateAnno(jc, out, classAnno);
   207             out.append("\n    };");
   208         }
   209         for (String init : toInitilize.toArray()) {
   210             out.append("\n    ").append(init).append("();");
   211         }
   212         out.append("\n  }");
   213         out.append("\n  if (arguments.length === 0) {");
   214         out.append("\n    if (!(this instanceof CLS)) {");
   215         out.append("\n      return new CLS();");
   216         out.append("\n    }");
   217         for (FieldData v : jc.getFields()) {
   218             byte[] onlyArr = v.findAnnotationData(true);
   219             String[] only = findAnnotation(onlyArr, jc, 
   220                 "org.apidesign.bck2brwsr.core.JavaScriptOnly", 
   221                 "name", "value"
   222             );
   223             if (only != null) {
   224                 if (only[0] != null && only[1] != null) {
   225                     out.append("\n    p.").append(only[0]).append(" = ")
   226                         .append(only[1]).append(";");
   227                 }
   228                 continue;
   229             }
   230             if (!v.isStatic()) {
   231                 out.append("\n    this.fld_").
   232                     append(className).append('_').
   233                     append(v.getName()).append(initField(v));
   234             }
   235         }
   236         out.append("\n    return this;");
   237         out.append("\n  }");
   238         out.append("\n  return arguments[0] ? new CLS() : CLS.prototype;");
   239         out.append("\n};");
   240 
   241         declaredClass(jc, className);
   242 
   243 //        StringBuilder sb = new StringBuilder();
   244 //        for (String init : toInitilize.toArray()) {
   245 //            sb.append("\n").append(init).append("();");
   246 //        }
   247         return "";
   248     }
   249     private String generateStaticMethod(String destObject, MethodData m, StringArray toInitilize) throws IOException {
   250         String jsb = javaScriptBody(destObject, m, true);
   251         if (jsb != null) {
   252             return jsb;
   253         }
   254         final String mn = findMethodName(m, new StringBuilder());
   255         if (mn.equals("class__V")) {
   256             toInitilize.add(accessClass(className(jc)) + "(false)." + mn);
   257         }
   258         generateMethod(destObject, mn, m);
   259         return mn;
   260     }
   261 
   262     private String generateInstanceMethod(String destObject, MethodData m) throws IOException {
   263         String jsb = javaScriptBody(destObject, m, false);
   264         if (jsb != null) {
   265             return jsb;
   266         }
   267         final String mn = findMethodName(m, new StringBuilder());
   268         generateMethod(destObject, mn, m);
   269         return mn;
   270     }
   271 
   272     private void generateMethod(String destObject, String name, MethodData m)
   273             throws IOException {
   274         final StackMapIterator stackMapIterator = m.createStackMapIterator();
   275         TrapDataIterator trap = m.getTrapDataIterator();
   276         final LocalsMapper lmapper =
   277                 new LocalsMapper(stackMapIterator.getArguments());
   278 
   279         out.append(destObject).append(".").append(name).append(" = function(");
   280         lmapper.outputArguments(out, m.isStatic());
   281         out.append(") {").append("\n");
   282 
   283         final byte[] byteCodes = m.getCode();
   284         if (byteCodes == null) {
   285             out.append("  throw 'no code found for ")
   286                .append(jc.getClassName()).append('.')
   287                .append(m.getName()).append("';\n");
   288             out.append("};");
   289             return;
   290         }
   291 
   292         final StackMapper smapper = new StackMapper();
   293 
   294         if (!m.isStatic()) {
   295             out.append("  var ").append(" lcA0 = this;\n");
   296         }
   297 
   298         int lastStackFrame = -1;
   299         TrapData[] previousTrap = null;
   300         boolean wide = false;
   301         
   302         out.append("\n  var gt = 0;\n");
   303         int openBraces = 0;
   304         int topMostLabel = 0;
   305         for (int i = 0; i < byteCodes.length; i++) {
   306             int prev = i;
   307             stackMapIterator.advanceTo(i);
   308             boolean changeInCatch = trap.advanceTo(i);
   309             if (changeInCatch || lastStackFrame != stackMapIterator.getFrameIndex()) {
   310                 if (previousTrap != null) {
   311                     generateCatch(previousTrap, i, topMostLabel);
   312                     previousTrap = null;
   313                 }
   314             }
   315             if (lastStackFrame != stackMapIterator.getFrameIndex()) {
   316                 if (i != 0) {
   317                     out.append("    }\n");
   318                 }
   319                 if (openBraces > 64) {
   320                     for (int c = 0; c < 64; c++) {
   321                         out.append("break;}\n");
   322                     }
   323                     openBraces = 1;
   324                     topMostLabel = i;
   325                 }
   326                 
   327                 lastStackFrame = stackMapIterator.getFrameIndex();
   328                 lmapper.syncWithFrameLocals(stackMapIterator.getFrameLocals());
   329                 smapper.syncWithFrameStack(stackMapIterator.getFrameStack());
   330                 out.append("    X_" + i).append(": for (;;) { IF: if (gt <= " + i + ") {\n");
   331                 openBraces++;
   332                 changeInCatch = true;
   333             } else {
   334                 debug("    /* " + i + " */ ");
   335             }
   336             if (changeInCatch && trap.useTry()) {
   337                 out.append("try {");
   338                 previousTrap = trap.current();
   339             }
   340             final int c = readUByte(byteCodes, i);
   341             switch (c) {
   342                 case opc_aload_0:
   343                     emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(0));
   344                     break;
   345                 case opc_iload_0:
   346                     emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(0));
   347                     break;
   348                 case opc_lload_0:
   349                     emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(0));
   350                     break;
   351                 case opc_fload_0:
   352                     emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(0));
   353                     break;
   354                 case opc_dload_0:
   355                     emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(0));
   356                     break;
   357                 case opc_aload_1:
   358                     emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(1));
   359                     break;
   360                 case opc_iload_1:
   361                     emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(1));
   362                     break;
   363                 case opc_lload_1:
   364                     emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(1));
   365                     break;
   366                 case opc_fload_1:
   367                     emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(1));
   368                     break;
   369                 case opc_dload_1:
   370                     emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(1));
   371                     break;
   372                 case opc_aload_2:
   373                     emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(2));
   374                     break;
   375                 case opc_iload_2:
   376                     emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(2));
   377                     break;
   378                 case opc_lload_2:
   379                     emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(2));
   380                     break;
   381                 case opc_fload_2:
   382                     emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(2));
   383                     break;
   384                 case opc_dload_2:
   385                     emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(2));
   386                     break;
   387                 case opc_aload_3:
   388                     emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(3));
   389                     break;
   390                 case opc_iload_3:
   391                     emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(3));
   392                     break;
   393                 case opc_lload_3:
   394                     emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(3));
   395                     break;
   396                 case opc_fload_3:
   397                     emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(3));
   398                     break;
   399                 case opc_dload_3:
   400                     emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(3));
   401                     break;
   402                 case opc_iload: {
   403                     ++i;
   404                     final int indx = wide ? readUShort(byteCodes, i++)
   405                                           : readUByte(byteCodes, i);
   406                     wide = false;
   407                     emit(out, "var @1 = @2;",
   408                          smapper.pushI(), lmapper.getI(indx));
   409                     break;
   410                 }
   411                 case opc_lload: {
   412                     ++i;
   413                     final int indx = wide ? readUShort(byteCodes, i++)
   414                                           : readUByte(byteCodes, i);
   415                     wide = false;
   416                     emit(out, "var @1 = @2;",
   417                          smapper.pushL(), lmapper.getL(indx));
   418                     break;
   419                 }
   420                 case opc_fload: {
   421                     ++i;
   422                     final int indx = wide ? readUShort(byteCodes, i++)
   423                                           : readUByte(byteCodes, i);
   424                     wide = false;
   425                     emit(out, "var @1 = @2;",
   426                          smapper.pushF(), lmapper.getF(indx));
   427                     break;
   428                 }
   429                 case opc_dload: {
   430                     ++i;
   431                     final int indx = wide ? readUShort(byteCodes, i++)
   432                                           : readUByte(byteCodes, i);
   433                     wide = false;
   434                     emit(out, "var @1 = @2;",
   435                          smapper.pushD(), lmapper.getD(indx));
   436                     break;
   437                 }
   438                 case opc_aload: {
   439                     ++i;
   440                     final int indx = wide ? readUShort(byteCodes, i++)
   441                                           : readUByte(byteCodes, i);
   442                     wide = false;
   443                     emit(out, "var @1 = @2;",
   444                          smapper.pushA(), lmapper.getA(indx));
   445                     break;
   446                 }
   447                 case opc_istore: {
   448                     ++i;
   449                     final int indx = wide ? readUShort(byteCodes, i++)
   450                                           : readUByte(byteCodes, i);
   451                     wide = false;
   452                     emit(out, "var @1 = @2;",
   453                          lmapper.setI(indx), smapper.popI());
   454                     break;
   455                 }
   456                 case opc_lstore: {
   457                     ++i;
   458                     final int indx = wide ? readUShort(byteCodes, i++)
   459                                           : readUByte(byteCodes, i);
   460                     wide = false;
   461                     emit(out, "var @1 = @2;",
   462                          lmapper.setL(indx), smapper.popL());
   463                     break;
   464                 }
   465                 case opc_fstore: {
   466                     ++i;
   467                     final int indx = wide ? readUShort(byteCodes, i++)
   468                                           : readUByte(byteCodes, i);
   469                     wide = false;
   470                     emit(out, "var @1 = @2;",
   471                          lmapper.setF(indx), smapper.popF());
   472                     break;
   473                 }
   474                 case opc_dstore: {
   475                     ++i;
   476                     final int indx = wide ? readUShort(byteCodes, i++)
   477                                           : readUByte(byteCodes, i);
   478                     wide = false;
   479                     emit(out, "var @1 = @2;",
   480                          lmapper.setD(indx), smapper.popD());
   481                     break;
   482                 }
   483                 case opc_astore: {
   484                     ++i;
   485                     final int indx = wide ? readUShort(byteCodes, i++)
   486                                           : readUByte(byteCodes, i);
   487                     wide = false;
   488                     emit(out, "var @1 = @2;",
   489                          lmapper.setA(indx), smapper.popA());
   490                     break;
   491                 }
   492                 case opc_astore_0:
   493                     emit(out, "var @1 = @2;", lmapper.setA(0), smapper.popA());
   494                     break;
   495                 case opc_istore_0:
   496                     emit(out, "var @1 = @2;", lmapper.setI(0), smapper.popI());
   497                     break;
   498                 case opc_lstore_0:
   499                     emit(out, "var @1 = @2;", lmapper.setL(0), smapper.popL());
   500                     break;
   501                 case opc_fstore_0:
   502                     emit(out, "var @1 = @2;", lmapper.setF(0), smapper.popF());
   503                     break;
   504                 case opc_dstore_0:
   505                     emit(out, "var @1 = @2;", lmapper.setD(0), smapper.popD());
   506                     break;
   507                 case opc_astore_1:
   508                     emit(out, "var @1 = @2;", lmapper.setA(1), smapper.popA());
   509                     break;
   510                 case opc_istore_1:
   511                     emit(out, "var @1 = @2;", lmapper.setI(1), smapper.popI());
   512                     break;
   513                 case opc_lstore_1:
   514                     emit(out, "var @1 = @2;", lmapper.setL(1), smapper.popL());
   515                     break;
   516                 case opc_fstore_1:
   517                     emit(out, "var @1 = @2;", lmapper.setF(1), smapper.popF());
   518                     break;
   519                 case opc_dstore_1:
   520                     emit(out, "var @1 = @2;", lmapper.setD(1), smapper.popD());
   521                     break;
   522                 case opc_astore_2:
   523                     emit(out, "var @1 = @2;", lmapper.setA(2), smapper.popA());
   524                     break;
   525                 case opc_istore_2:
   526                     emit(out, "var @1 = @2;", lmapper.setI(2), smapper.popI());
   527                     break;
   528                 case opc_lstore_2:
   529                     emit(out, "var @1 = @2;", lmapper.setL(2), smapper.popL());
   530                     break;
   531                 case opc_fstore_2:
   532                     emit(out, "var @1 = @2;", lmapper.setF(2), smapper.popF());
   533                     break;
   534                 case opc_dstore_2:
   535                     emit(out, "var @1 = @2;", lmapper.setD(2), smapper.popD());
   536                     break;
   537                 case opc_astore_3:
   538                     emit(out, "var @1 = @2;", lmapper.setA(3), smapper.popA());
   539                     break;
   540                 case opc_istore_3:
   541                     emit(out, "var @1 = @2;", lmapper.setI(3), smapper.popI());
   542                     break;
   543                 case opc_lstore_3:
   544                     emit(out, "var @1 = @2;", lmapper.setL(3), smapper.popL());
   545                     break;
   546                 case opc_fstore_3:
   547                     emit(out, "var @1 = @2;", lmapper.setF(3), smapper.popF());
   548                     break;
   549                 case opc_dstore_3:
   550                     emit(out, "var @1 = @2;", lmapper.setD(3), smapper.popD());
   551                     break;
   552                 case opc_iadd:
   553                     emit(out, "@1 = @1.add32(@2);", smapper.getI(1), smapper.popI());
   554                     break;
   555                 case opc_ladd:
   556                     emit(out, "@1 = @1.add64(@2);", smapper.getL(1), smapper.popL());
   557                     break;
   558                 case opc_fadd:
   559                     emit(out, "@1 += @2;", smapper.getF(1), smapper.popF());
   560                     break;
   561                 case opc_dadd:
   562                     emit(out, "@1 += @2;", smapper.getD(1), smapper.popD());
   563                     break;
   564                 case opc_isub:
   565                     emit(out, "@1 = @1.sub32(@2);", smapper.getI(1), smapper.popI());
   566                     break;
   567                 case opc_lsub:
   568                     emit(out, "@1 = @1.sub64(@2);", smapper.getL(1), smapper.popL());
   569                     break;
   570                 case opc_fsub:
   571                     emit(out, "@1 -= @2;", smapper.getF(1), smapper.popF());
   572                     break;
   573                 case opc_dsub:
   574                     emit(out, "@1 -= @2;", smapper.getD(1), smapper.popD());
   575                     break;
   576                 case opc_imul:
   577                     emit(out, "@1 = @1.mul32(@2);", smapper.getI(1), smapper.popI());
   578                     break;
   579                 case opc_lmul:
   580                     emit(out, "@1 = @1.mul64(@2);", smapper.getL(1), smapper.popL());
   581                     break;
   582                 case opc_fmul:
   583                     emit(out, "@1 *= @2;", smapper.getF(1), smapper.popF());
   584                     break;
   585                 case opc_dmul:
   586                     emit(out, "@1 *= @2;", smapper.getD(1), smapper.popD());
   587                     break;
   588                 case opc_idiv:
   589                     emit(out, "@1 = @1.div32(@2);",
   590                          smapper.getI(1), smapper.popI());
   591                     break;
   592                 case opc_ldiv:
   593                     emit(out, "@1 = @1.div64(@2);",
   594                          smapper.getL(1), smapper.popL());
   595                     break;
   596                 case opc_fdiv:
   597                     emit(out, "@1 /= @2;", smapper.getF(1), smapper.popF());
   598                     break;
   599                 case opc_ddiv:
   600                     emit(out, "@1 /= @2;", smapper.getD(1), smapper.popD());
   601                     break;
   602                 case opc_irem:
   603                     emit(out, "@1 = @1.mod32(@2);",
   604                          smapper.getI(1), smapper.popI());
   605                     break;
   606                 case opc_lrem:
   607                     emit(out, "@1 = @1.mod64(@2);",
   608                          smapper.getL(1), smapper.popL());
   609                     break;
   610                 case opc_frem:
   611                     emit(out, "@1 %= @2;", smapper.getF(1), smapper.popF());
   612                     break;
   613                 case opc_drem:
   614                     emit(out, "@1 %= @2;", smapper.getD(1), smapper.popD());
   615                     break;
   616                 case opc_iand:
   617                     emit(out, "@1 &= @2;", smapper.getI(1), smapper.popI());
   618                     break;
   619                 case opc_land:
   620                     emit(out, "@1 = @1.and64(@2);", smapper.getL(1), smapper.popL());
   621                     break;
   622                 case opc_ior:
   623                     emit(out, "@1 |= @2;", smapper.getI(1), smapper.popI());
   624                     break;
   625                 case opc_lor:
   626                     emit(out, "@1 = @1.or64(@2);", smapper.getL(1), smapper.popL());
   627                     break;
   628                 case opc_ixor:
   629                     emit(out, "@1 ^= @2;", smapper.getI(1), smapper.popI());
   630                     break;
   631                 case opc_lxor:
   632                     emit(out, "@1 = @1.xor64(@2);", smapper.getL(1), smapper.popL());
   633                     break;
   634                 case opc_ineg:
   635                     emit(out, "@1 = @1.neg32();", smapper.getI(0));
   636                     break;
   637                 case opc_lneg:
   638                     emit(out, "@1 = @1.neg64();", smapper.getL(0));
   639                     break;
   640                 case opc_fneg:
   641                     emit(out, "@1 = -@1;", smapper.getF(0));
   642                     break;
   643                 case opc_dneg:
   644                     emit(out, "@1 = -@1;", smapper.getD(0));
   645                     break;
   646                 case opc_ishl:
   647                     emit(out, "@1 <<= @2;", smapper.getI(1), smapper.popI());
   648                     break;
   649                 case opc_lshl:
   650                     emit(out, "@1 = @1.shl64(@2);", smapper.getL(1), smapper.popI());
   651                     break;
   652                 case opc_ishr:
   653                     emit(out, "@1 >>= @2;", smapper.getI(1), smapper.popI());
   654                     break;
   655                 case opc_lshr:
   656                     emit(out, "@1 = @1.shr64(@2);", smapper.getL(1), smapper.popI());
   657                     break;
   658                 case opc_iushr:
   659                     emit(out, "@1 >>>= @2;", smapper.getI(1), smapper.popI());
   660                     break;
   661                 case opc_lushr:
   662                     emit(out, "@1 = @1.ushr64(@2);", smapper.getL(1), smapper.popI());
   663                     break;
   664                 case opc_iinc: {
   665                     ++i;
   666                     final int varIndx = wide ? readUShort(byteCodes, i++)
   667                                              : readUByte(byteCodes, i);
   668                     ++i;
   669                     final int incrBy = wide ? readShort(byteCodes, i++)
   670                                             : byteCodes[i];
   671                     wide = false;
   672                     if (incrBy == 1) {
   673                         emit(out, "@1++;", lmapper.getI(varIndx));
   674                     } else {
   675                         emit(out, "@1 += @2;",
   676                              lmapper.getI(varIndx),
   677                              Integer.toString(incrBy));
   678                     }
   679                     break;
   680                 }
   681                 case opc_return:
   682                     emit(out, "return;");
   683                     break;
   684                 case opc_ireturn:
   685                     emit(out, "return @1;", smapper.popI());
   686                     break;
   687                 case opc_lreturn:
   688                     emit(out, "return @1;", smapper.popL());
   689                     break;
   690                 case opc_freturn:
   691                     emit(out, "return @1;", smapper.popF());
   692                     break;
   693                 case opc_dreturn:
   694                     emit(out, "return @1;", smapper.popD());
   695                     break;
   696                 case opc_areturn:
   697                     emit(out, "return @1;", smapper.popA());
   698                     break;
   699                 case opc_i2l:
   700                     emit(out, "var @2 = @1;", smapper.popI(), smapper.pushL());
   701                     break;
   702                 case opc_i2f:
   703                     emit(out, "var @2 = @1;", smapper.popI(), smapper.pushF());
   704                     break;
   705                 case opc_i2d:
   706                     emit(out, "var @2 = @1;", smapper.popI(), smapper.pushD());
   707                     break;
   708                 case opc_l2i:
   709                     emit(out, "var @2 = @1.toInt32();", smapper.popL(), smapper.pushI());
   710                     break;
   711                     // max int check?
   712                 case opc_l2f:
   713                     emit(out, "var @2 = @1.toFP();", smapper.popL(), smapper.pushF());
   714                     break;
   715                 case opc_l2d:
   716                     emit(out, "var @2 = @1.toFP();", smapper.popL(), smapper.pushD());
   717                     break;
   718                 case opc_f2d:
   719                     emit(out, "var @2 = @1;", smapper.popF(), smapper.pushD());
   720                     break;
   721                 case opc_d2f:
   722                     emit(out, "var @2 = @1;", smapper.popD(), smapper.pushF());
   723                     break;
   724                 case opc_f2i:
   725                     emit(out, "var @2 = @1.toInt32();",
   726                          smapper.popF(), smapper.pushI());
   727                     break;
   728                 case opc_f2l:
   729                     emit(out, "var @2 = @1.toLong();",
   730                          smapper.popF(), smapper.pushL());
   731                     break;
   732                 case opc_d2i:
   733                     emit(out, "var @2 = @1.toInt32();",
   734                          smapper.popD(), smapper.pushI());
   735                     break;
   736                 case opc_d2l:
   737                     emit(out, "var @2 = @1.toLong();",
   738                          smapper.popD(), smapper.pushL());
   739                     break;
   740                 case opc_i2b:
   741                     emit(out, "var @1 = @1.toInt8();", smapper.getI(0));
   742                     break;
   743                 case opc_i2c:
   744                     out.append("{ /* number conversion */ }");
   745                     break;
   746                 case opc_i2s:
   747                     emit(out, "var @1 = @1.toInt16();", smapper.getI(0));
   748                     break;
   749                 case opc_aconst_null:
   750                     emit(out, "var @1 = null;", smapper.pushA());
   751                     break;
   752                 case opc_iconst_m1:
   753                     emit(out, "var @1 = -1;", smapper.pushI());
   754                     break;
   755                 case opc_iconst_0:
   756                     emit(out, "var @1 = 0;", smapper.pushI());
   757                     break;
   758                 case opc_dconst_0:
   759                     emit(out, "var @1 = 0;", smapper.pushD());
   760                     break;
   761                 case opc_lconst_0:
   762                     emit(out, "var @1 = 0;", smapper.pushL());
   763                     break;
   764                 case opc_fconst_0:
   765                     emit(out, "var @1 = 0;", smapper.pushF());
   766                     break;
   767                 case opc_iconst_1:
   768                     emit(out, "var @1 = 1;", smapper.pushI());
   769                     break;
   770                 case opc_lconst_1:
   771                     emit(out, "var @1 = 1;", smapper.pushL());
   772                     break;
   773                 case opc_fconst_1:
   774                     emit(out, "var @1 = 1;", smapper.pushF());
   775                     break;
   776                 case opc_dconst_1:
   777                     emit(out, "var @1 = 1;", smapper.pushD());
   778                     break;
   779                 case opc_iconst_2:
   780                     emit(out, "var @1 = 2;", smapper.pushI());
   781                     break;
   782                 case opc_fconst_2:
   783                     emit(out, "var @1 = 2;", smapper.pushF());
   784                     break;
   785                 case opc_iconst_3:
   786                     emit(out, "var @1 = 3;", smapper.pushI());
   787                     break;
   788                 case opc_iconst_4:
   789                     emit(out, "var @1 = 4;", smapper.pushI());
   790                     break;
   791                 case opc_iconst_5:
   792                     emit(out, "var @1 = 5;", smapper.pushI());
   793                     break;
   794                 case opc_ldc: {
   795                     int indx = readUByte(byteCodes, ++i);
   796                     String v = encodeConstant(indx);
   797                     int type = VarType.fromConstantType(jc.getTag(indx));
   798                     emit(out, "var @1 = @2;", smapper.pushT(type), v);
   799                     break;
   800                 }
   801                 case opc_ldc_w:
   802                 case opc_ldc2_w: {
   803                     int indx = readUShortArg(byteCodes, i);
   804                     i += 2;
   805                     String v = encodeConstant(indx);
   806                     int type = VarType.fromConstantType(jc.getTag(indx));
   807                     if (type == VarType.LONG) {
   808                         final Long lv = new Long(v);
   809                         final int low = (int)(lv.longValue() & 0xFFFFFFFF);
   810                         final int hi = (int)(lv.longValue() >> 32);
   811                         emit(out, "var @1 = 0x@3.next32(0x@2);", smapper.pushL(), 
   812                                 Integer.toHexString(low), Integer.toHexString(hi));
   813                     } else {
   814                         emit(out, "var @1 = @2;", smapper.pushT(type), v);
   815                     }
   816                     break;
   817                 }
   818                 case opc_lcmp:
   819                     emit(out, "var @3 = @2.compare64(@1);",
   820                          smapper.popL(), smapper.popL(), smapper.pushI());
   821                     break;
   822                 case opc_fcmpl:
   823                 case opc_fcmpg:
   824                     emit(out, "var @3 = (@2 == @1) ? 0 : ((@2 < @1) ? -1 : 1);",
   825                          smapper.popF(), smapper.popF(), smapper.pushI());
   826                     break;
   827                 case opc_dcmpl:
   828                 case opc_dcmpg:
   829                     emit(out, "var @3 = (@2 == @1) ? 0 : ((@2 < @1) ? -1 : 1);",
   830                          smapper.popD(), smapper.popD(), smapper.pushI());
   831                     break;
   832                 case opc_if_acmpeq:
   833                     i = generateIf(byteCodes, i, smapper.popA(), smapper.popA(),
   834                                    "===", topMostLabel);
   835                     break;
   836                 case opc_if_acmpne:
   837                     i = generateIf(byteCodes, i, smapper.popA(), smapper.popA(),
   838                                    "!==", topMostLabel);
   839                     break;
   840                 case opc_if_icmpeq:
   841                     i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
   842                                    "==", topMostLabel);
   843                     break;
   844                 case opc_ifeq: {
   845                     int indx = i + readShortArg(byteCodes, i);
   846                     emitIf(out, "if (@1 == 0) ",
   847                          smapper.popI(), i, indx, topMostLabel);
   848                     i += 2;
   849                     break;
   850                 }
   851                 case opc_ifne: {
   852                     int indx = i + readShortArg(byteCodes, i);
   853                     emitIf(out, "if (@1 != 0) ",
   854                          smapper.popI(), i, indx, topMostLabel);
   855                     i += 2;
   856                     break;
   857                 }
   858                 case opc_iflt: {
   859                     int indx = i + readShortArg(byteCodes, i);
   860                     emitIf(out, "if (@1 < 0) ",
   861                          smapper.popI(), i, indx, topMostLabel);
   862                     i += 2;
   863                     break;
   864                 }
   865                 case opc_ifle: {
   866                     int indx = i + readShortArg(byteCodes, i);
   867                     emitIf(out, "if (@1 <= 0) ",
   868                          smapper.popI(), i, indx, topMostLabel);
   869                     i += 2;
   870                     break;
   871                 }
   872                 case opc_ifgt: {
   873                     int indx = i + readShortArg(byteCodes, i);
   874                     emitIf(out, "if (@1 > 0) ",
   875                          smapper.popI(), i, indx, topMostLabel);
   876                     i += 2;
   877                     break;
   878                 }
   879                 case opc_ifge: {
   880                     int indx = i + readShortArg(byteCodes, i);
   881                     emitIf(out, "if (@1 >= 0) ",
   882                          smapper.popI(), i, indx, topMostLabel);
   883                     i += 2;
   884                     break;
   885                 }
   886                 case opc_ifnonnull: {
   887                     int indx = i + readShortArg(byteCodes, i);
   888                     emitIf(out, "if (@1 !== null) ",
   889                          smapper.popA(), i, indx, topMostLabel);
   890                     i += 2;
   891                     break;
   892                 }
   893                 case opc_ifnull: {
   894                     int indx = i + readShortArg(byteCodes, i);
   895                     emitIf(out, "if (@1 === null) ",
   896                          smapper.popA(), i, indx, topMostLabel);
   897                     i += 2;
   898                     break;
   899                 }
   900                 case opc_if_icmpne:
   901                     i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
   902                                    "!=", topMostLabel);
   903                     break;
   904                 case opc_if_icmplt:
   905                     i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
   906                                    "<", topMostLabel);
   907                     break;
   908                 case opc_if_icmple:
   909                     i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
   910                                    "<=", topMostLabel);
   911                     break;
   912                 case opc_if_icmpgt:
   913                     i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
   914                                    ">", topMostLabel);
   915                     break;
   916                 case opc_if_icmpge:
   917                     i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
   918                                    ">=", topMostLabel);
   919                     break;
   920                 case opc_goto: {
   921                     int indx = i + readShortArg(byteCodes, i);
   922                     goTo(out, i, indx, topMostLabel);
   923                     i += 2;
   924                     break;
   925                 }
   926                 case opc_lookupswitch: {
   927                     i = generateLookupSwitch(i, byteCodes, smapper, topMostLabel);
   928                     break;
   929                 }
   930                 case opc_tableswitch: {
   931                     i = generateTableSwitch(i, byteCodes, smapper, topMostLabel);
   932                     break;
   933                 }
   934                 case opc_invokeinterface: {
   935                     i = invokeVirtualMethod(byteCodes, i, smapper) + 2;
   936                     break;
   937                 }
   938                 case opc_invokevirtual:
   939                     i = invokeVirtualMethod(byteCodes, i, smapper);
   940                     break;
   941                 case opc_invokespecial:
   942                     i = invokeStaticMethod(byteCodes, i, smapper, false);
   943                     break;
   944                 case opc_invokestatic:
   945                     i = invokeStaticMethod(byteCodes, i, smapper, true);
   946                     break;
   947                 case opc_new: {
   948                     int indx = readUShortArg(byteCodes, i);
   949                     String ci = jc.getClassName(indx);
   950                     emit(out, "var @1 = new @2;",
   951                          smapper.pushA(), accessClass(ci.replace('/', '_')));
   952                     addReference(ci);
   953                     i += 2;
   954                     break;
   955                 }
   956                 case opc_newarray:
   957                     int atype = readUByte(byteCodes, ++i);
   958                     generateNewArray(atype, smapper);
   959                     break;
   960                 case opc_anewarray: {
   961                     int type = readUShortArg(byteCodes, i);
   962                     i += 2;
   963                     generateANewArray(type, smapper);
   964                     break;
   965                 }
   966                 case opc_multianewarray: {
   967                     int type = readUShortArg(byteCodes, i);
   968                     i += 2;
   969                     i = generateMultiANewArray(type, byteCodes, i, smapper);
   970                     break;
   971                 }
   972                 case opc_arraylength:
   973                     emit(out, "var @2 = @1.length;",
   974                          smapper.popA(), smapper.pushI());
   975                     break;
   976                 case opc_lastore:
   977                     emit(out, "@3.at(@2, @1);",
   978                          smapper.popL(), smapper.popI(), smapper.popA());
   979                     break;
   980                 case opc_fastore:
   981                     emit(out, "@3.at(@2, @1);",
   982                          smapper.popF(), smapper.popI(), smapper.popA());
   983                     break;
   984                 case opc_dastore:
   985                     emit(out, "@3.at(@2, @1);",
   986                          smapper.popD(), smapper.popI(), smapper.popA());
   987                     break;
   988                 case opc_aastore:
   989                     emit(out, "@3.at(@2, @1);",
   990                          smapper.popA(), smapper.popI(), smapper.popA());
   991                     break;
   992                 case opc_iastore:
   993                 case opc_bastore:
   994                 case opc_castore:
   995                 case opc_sastore:
   996                     emit(out, "@3.at(@2, @1);",
   997                          smapper.popI(), smapper.popI(), smapper.popA());
   998                     break;
   999                 case opc_laload:
  1000                     emit(out, "var @3 = @2.at(@1);",
  1001                          smapper.popI(), smapper.popA(), smapper.pushL());
  1002                     break;
  1003                 case opc_faload:
  1004                     emit(out, "var @3 = @2.at(@1);",
  1005                          smapper.popI(), smapper.popA(), smapper.pushF());
  1006                     break;
  1007                 case opc_daload:
  1008                     emit(out, "var @3 = @2.at(@1);",
  1009                          smapper.popI(), smapper.popA(), smapper.pushD());
  1010                     break;
  1011                 case opc_aaload:
  1012                     emit(out, "var @3 = @2.at(@1);",
  1013                          smapper.popI(), smapper.popA(), smapper.pushA());
  1014                     break;
  1015                 case opc_iaload:
  1016                 case opc_baload:
  1017                 case opc_caload:
  1018                 case opc_saload:
  1019                     emit(out, "var @3 = @2.at(@1);",
  1020                          smapper.popI(), smapper.popA(), smapper.pushI());
  1021                     break;
  1022                 case opc_pop:
  1023                 case opc_pop2:
  1024                     smapper.pop(1);
  1025                     debug("/* pop */");
  1026                     break;
  1027                 case opc_dup: {
  1028                     final Variable v = smapper.get(0);
  1029                     emit(out, "var @1 = @2;", smapper.pushT(v.getType()), v);
  1030                     break;
  1031                 }
  1032                 case opc_dup2: {
  1033                     final Variable vi1 = smapper.get(0);
  1034 
  1035                     if (vi1.isCategory2()) {
  1036                         emit(out, "var @1 = @2;",
  1037                              smapper.pushT(vi1.getType()), vi1);
  1038                     } else {
  1039                         final Variable vi2 = smapper.get(1);
  1040                         emit(out, "var @1 = @2, @3 = @4;",
  1041                              smapper.pushT(vi2.getType()), vi2,
  1042                              smapper.pushT(vi1.getType()), vi1);
  1043                     }
  1044                     break;
  1045                 }
  1046                 case opc_dup_x1: {
  1047                     final Variable vi1 = smapper.pop();
  1048                     final Variable vi2 = smapper.pop();
  1049                     final Variable vo3 = smapper.pushT(vi1.getType());
  1050                     final Variable vo2 = smapper.pushT(vi2.getType());
  1051                     final Variable vo1 = smapper.pushT(vi1.getType());
  1052 
  1053                     emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
  1054                          vo1, vi1, vo2, vi2, vo3, vo1);
  1055                     break;
  1056                 }
  1057                 case opc_dup2_x1: {
  1058                     final Variable vi1 = smapper.pop();
  1059                     final Variable vi2 = smapper.pop();
  1060 
  1061                     if (vi1.isCategory2()) {
  1062                         final Variable vo3 = smapper.pushT(vi1.getType());
  1063                         final Variable vo2 = smapper.pushT(vi2.getType());
  1064                         final Variable vo1 = smapper.pushT(vi1.getType());
  1065 
  1066                         emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
  1067                              vo1, vi1, vo2, vi2, vo3, vo1);
  1068                     } else {
  1069                         final Variable vi3 = smapper.pop();
  1070                         final Variable vo5 = smapper.pushT(vi2.getType());
  1071                         final Variable vo4 = smapper.pushT(vi1.getType());
  1072                         final Variable vo3 = smapper.pushT(vi3.getType());
  1073                         final Variable vo2 = smapper.pushT(vi2.getType());
  1074                         final Variable vo1 = smapper.pushT(vi1.getType());
  1075 
  1076                         emit(out, "var @1 = @2, @3 = @4, @5 = @6,",
  1077                              vo1, vi1, vo2, vi2, vo3, vi3);
  1078                         emit(out, " @1 = @2, @3 = @4;",
  1079                              vo4, vo1, vo5, vo2);
  1080                     }
  1081                     break;
  1082                 }
  1083                 case opc_dup_x2: {
  1084                     final Variable vi1 = smapper.pop();
  1085                     final Variable vi2 = smapper.pop();
  1086 
  1087                     if (vi2.isCategory2()) {
  1088                         final Variable vo3 = smapper.pushT(vi1.getType());
  1089                         final Variable vo2 = smapper.pushT(vi2.getType());
  1090                         final Variable vo1 = smapper.pushT(vi1.getType());
  1091 
  1092                         emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
  1093                              vo1, vi1, vo2, vi2, vo3, vo1);
  1094                     } else {
  1095                         final Variable vi3 = smapper.pop();
  1096                         final Variable vo4 = smapper.pushT(vi1.getType());
  1097                         final Variable vo3 = smapper.pushT(vi3.getType());
  1098                         final Variable vo2 = smapper.pushT(vi2.getType());
  1099                         final Variable vo1 = smapper.pushT(vi1.getType());
  1100 
  1101                         emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8;",
  1102                              vo1, vi1, vo2, vi2, vo3, vi3, vo4, vo1);
  1103                     }
  1104                     break;
  1105                 }
  1106                 case opc_dup2_x2: {
  1107                     final Variable vi1 = smapper.pop();
  1108                     final Variable vi2 = smapper.pop();
  1109 
  1110                     if (vi1.isCategory2()) {
  1111                         if (vi2.isCategory2()) {
  1112                             final Variable vo3 = smapper.pushT(vi1.getType());
  1113                             final Variable vo2 = smapper.pushT(vi2.getType());
  1114                             final Variable vo1 = smapper.pushT(vi1.getType());
  1115 
  1116                             emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
  1117                                  vo1, vi1, vo2, vi2, vo3, vo1);
  1118                         } else {
  1119                             final Variable vi3 = smapper.pop();
  1120                             final Variable vo4 = smapper.pushT(vi1.getType());
  1121                             final Variable vo3 = smapper.pushT(vi3.getType());
  1122                             final Variable vo2 = smapper.pushT(vi2.getType());
  1123                             final Variable vo1 = smapper.pushT(vi1.getType());
  1124 
  1125                             emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8;",
  1126                                  vo1, vi1, vo2, vi2, vo3, vi3, vo4, vo1);
  1127                         }
  1128                     } else {
  1129                         final Variable vi3 = smapper.pop();
  1130 
  1131                         if (vi3.isCategory2()) {
  1132                             final Variable vo5 = smapper.pushT(vi2.getType());
  1133                             final Variable vo4 = smapper.pushT(vi1.getType());
  1134                             final Variable vo3 = smapper.pushT(vi3.getType());
  1135                             final Variable vo2 = smapper.pushT(vi2.getType());
  1136                             final Variable vo1 = smapper.pushT(vi1.getType());
  1137 
  1138                             emit(out, "var @1 = @2, @3 = @4, @5 = @6,",
  1139                                  vo1, vi1, vo2, vi2, vo3, vi3);
  1140                             emit(out, " @1 = @2, @3 = @4;",
  1141                                  vo4, vo1, vo5, vo2);
  1142                         } else {
  1143                             final Variable vi4 = smapper.pop();
  1144                             final Variable vo6 = smapper.pushT(vi2.getType());
  1145                             final Variable vo5 = smapper.pushT(vi1.getType());
  1146                             final Variable vo4 = smapper.pushT(vi4.getType());
  1147                             final Variable vo3 = smapper.pushT(vi3.getType());
  1148                             final Variable vo2 = smapper.pushT(vi2.getType());
  1149                             final Variable vo1 = smapper.pushT(vi1.getType());
  1150                             
  1151                             emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8,",
  1152                                  vo1, vi1, vo2, vi2, vo3, vi3, vo4, vi4);
  1153                             emit(out, " @1 = @2, @3 = @4;",
  1154                                  vo5, vo1, vo6, vo2);
  1155                         }
  1156                     }
  1157                     break;
  1158                 }
  1159                 case opc_swap: {
  1160                     final Variable vi1 = smapper.get(0);
  1161                     final Variable vi2 = smapper.get(1);
  1162 
  1163                     if (vi1.getType() == vi2.getType()) {
  1164                         final Variable tmp = smapper.pushT(vi1.getType());
  1165 
  1166                         emit(out, "var @1 = @2, @2 = @3, @3 = @1;",
  1167                              tmp, vi1, vi2);
  1168                         smapper.pop(1);
  1169                     } else {
  1170                         smapper.pop(2);
  1171                         smapper.pushT(vi1.getType());
  1172                         smapper.pushT(vi2.getType());
  1173                     }
  1174                     break;
  1175                 }
  1176                 case opc_bipush:
  1177                     emit(out, "var @1 = @2;",
  1178                          smapper.pushI(), Integer.toString(byteCodes[++i]));
  1179                     break;
  1180                 case opc_sipush:
  1181                     emit(out, "var @1 = @2;",
  1182                          smapper.pushI(),
  1183                          Integer.toString(readShortArg(byteCodes, i)));
  1184                     i += 2;
  1185                     break;
  1186                 case opc_getfield: {
  1187                     int indx = readUShortArg(byteCodes, i);
  1188                     String[] fi = jc.getFieldInfoName(indx);
  1189                     final int type = VarType.fromFieldType(fi[2].charAt(0));
  1190                     final String mangleClass = mangleSig(fi[0]);
  1191                     final String mangleClassAccess = accessClass(mangleClass);
  1192                     emit(out, "var @2 = @4(false)._@3.call(@1);",
  1193                          smapper.popA(),
  1194                          smapper.pushT(type), fi[1], mangleClassAccess
  1195                     );
  1196                     i += 2;
  1197                     break;
  1198                 }
  1199                 case opc_putfield: {
  1200                     int indx = readUShortArg(byteCodes, i);
  1201                     String[] fi = jc.getFieldInfoName(indx);
  1202                     final int type = VarType.fromFieldType(fi[2].charAt(0));
  1203                     final String mangleClass = mangleSig(fi[0]);
  1204                     final String mangleClassAccess = accessClass(mangleClass);
  1205                     emit(out, "@4(false)._@3.call(@2, @1);",
  1206                          smapper.popT(type),
  1207                          smapper.popA(), fi[1], 
  1208                          mangleClassAccess
  1209                     );
  1210                     i += 2;
  1211                     break;
  1212                 }
  1213                 case opc_getstatic: {
  1214                     int indx = readUShortArg(byteCodes, i);
  1215                     String[] fi = jc.getFieldInfoName(indx);
  1216                     final int type = VarType.fromFieldType(fi[2].charAt(0));
  1217                     emit(out, "var @1 = @2(false)._@3();",
  1218                          smapper.pushT(type),
  1219                          accessClass(fi[0].replace('/', '_')), fi[1]);
  1220                     i += 2;
  1221                     addReference(fi[0]);
  1222                     break;
  1223                 }
  1224                 case opc_putstatic: {
  1225                     int indx = readUShortArg(byteCodes, i);
  1226                     String[] fi = jc.getFieldInfoName(indx);
  1227                     final int type = VarType.fromFieldType(fi[2].charAt(0));
  1228                     emit(out, "@1(false)._@2(@3);",
  1229                          accessClass(fi[0].replace('/', '_')), fi[1],
  1230                          smapper.popT(type));
  1231                     i += 2;
  1232                     addReference(fi[0]);
  1233                     break;
  1234                 }
  1235                 case opc_checkcast: {
  1236                     int indx = readUShortArg(byteCodes, i);
  1237                     generateCheckcast(indx, smapper);
  1238                     i += 2;
  1239                     break;
  1240                 }
  1241                 case opc_instanceof: {
  1242                     int indx = readUShortArg(byteCodes, i);
  1243                     generateInstanceOf(indx, smapper);
  1244                     i += 2;
  1245                     break;
  1246                 }
  1247                 case opc_athrow: {
  1248                     final Variable v = smapper.popA();
  1249                     smapper.clear();
  1250 
  1251                     emit(out, "{ var @1 = @2; throw @2; }",
  1252                          smapper.pushA(), v);
  1253                     break;
  1254                 }
  1255 
  1256                 case opc_monitorenter: {
  1257                     out.append("/* monitor enter */");
  1258                     smapper.popA();
  1259                     break;
  1260                 }
  1261 
  1262                 case opc_monitorexit: {
  1263                     out.append("/* monitor exit */");
  1264                     smapper.popA();
  1265                     break;
  1266                 }
  1267 
  1268                 case opc_wide:
  1269                     wide = true;
  1270                     break;
  1271 
  1272                 default: {
  1273                     wide = false;
  1274                     emit(out, "throw 'unknown bytecode @1';",
  1275                          Integer.toString(c));
  1276                 }
  1277             }
  1278             if (debug(" //")) {
  1279                 generateByteCodeComment(prev, i, byteCodes);
  1280             }
  1281             out.append("\n");            
  1282         }
  1283         if (previousTrap != null) {
  1284             generateCatch(previousTrap, byteCodes.length, topMostLabel);
  1285         }
  1286         out.append("\n    }\n");
  1287         while (openBraces-- > 0) {
  1288             out.append('}');
  1289         }
  1290         out.append("\n};");
  1291     }
  1292 
  1293     private int generateIf(byte[] byteCodes, int i, final Variable v2, final Variable v1, final String test, int topMostLabel) throws IOException {
  1294         int indx = i + readShortArg(byteCodes, i);
  1295         out.append("if (").append(v1)
  1296            .append(' ').append(test).append(' ')
  1297            .append(v2).append(") ");
  1298         goTo(out, i, indx, topMostLabel);
  1299         return i + 2;
  1300     }
  1301     
  1302     private int readInt4(byte[] byteCodes, int offset) {
  1303         final int d = byteCodes[offset + 0] << 24;
  1304         final int c = byteCodes[offset + 1] << 16;
  1305         final int b = byteCodes[offset + 2] << 8;
  1306         final int a = byteCodes[offset + 3];
  1307         return (d & 0xff000000) | (c & 0xff0000) | (b & 0xff00) | (a & 0xff);
  1308     }
  1309     private static int readUByte(byte[] byteCodes, int offset) {
  1310         return byteCodes[offset] & 0xff;
  1311     }
  1312 
  1313     private static int readUShort(byte[] byteCodes, int offset) {
  1314         return ((byteCodes[offset] & 0xff) << 8)
  1315                     | (byteCodes[offset + 1] & 0xff);
  1316     }
  1317     private static int readUShortArg(byte[] byteCodes, int offsetInstruction) {
  1318         return readUShort(byteCodes, offsetInstruction + 1);
  1319     }
  1320 
  1321     private static int readShort(byte[] byteCodes, int offset) {
  1322         int signed = byteCodes[offset];
  1323         byte b0 = (byte)signed;
  1324         return (b0 << 8) | (byteCodes[offset + 1] & 0xff);
  1325     }
  1326     private static int readShortArg(byte[] byteCodes, int offsetInstruction) {
  1327         return readShort(byteCodes, offsetInstruction + 1);
  1328     }
  1329 
  1330     private static void countArgs(String descriptor, char[] returnType, StringBuilder sig, StringBuilder cnt) {
  1331         int i = 0;
  1332         Boolean count = null;
  1333         boolean array = false;
  1334         sig.append("__");
  1335         int firstPos = sig.length();
  1336         while (i < descriptor.length()) {
  1337             char ch = descriptor.charAt(i++);
  1338             switch (ch) {
  1339                 case '(':
  1340                     count = true;
  1341                     continue;
  1342                 case ')':
  1343                     count = false;
  1344                     continue;
  1345                 case 'B': 
  1346                 case 'C': 
  1347                 case 'D': 
  1348                 case 'F': 
  1349                 case 'I': 
  1350                 case 'J': 
  1351                 case 'S': 
  1352                 case 'Z': 
  1353                     if (count) {
  1354                         if (array) {
  1355                             sig.append("_3");
  1356                         }
  1357                         sig.append(ch);
  1358                         if (ch == 'J' || ch == 'D') {
  1359                             cnt.append('1');
  1360                         } else {
  1361                             cnt.append('0');
  1362                         }
  1363                     } else {
  1364                         sig.insert(firstPos, ch);
  1365                         if (array) {
  1366                             returnType[0] = '[';
  1367                             sig.insert(firstPos, "_3");
  1368                         } else {
  1369                             returnType[0] = ch;
  1370                         }
  1371                     }
  1372                     array = false;
  1373                     continue;
  1374                 case 'V': 
  1375                     assert !count;
  1376                     returnType[0] = 'V';
  1377                     sig.insert(firstPos, 'V');
  1378                     continue;
  1379                 case 'L':
  1380                     int next = descriptor.indexOf(';', i);
  1381                     String realSig = mangleSig(descriptor, i - 1, next + 1);
  1382                     if (count) {
  1383                         if (array) {
  1384                             sig.append("_3");
  1385                         }
  1386                         sig.append(realSig);
  1387                         cnt.append('0');
  1388                     } else {
  1389                         sig.insert(firstPos, realSig);
  1390                         if (array) {
  1391                             sig.insert(firstPos, "_3");
  1392                         }
  1393                         returnType[0] = 'L';
  1394                     }
  1395                     i = next + 1;
  1396                     array = false;
  1397                     continue;
  1398                 case '[':
  1399                     array = true;
  1400                     continue;
  1401                 default:
  1402                     throw new IllegalStateException("Invalid char: " + ch);
  1403             }
  1404         }
  1405     }
  1406     
  1407     static String mangleSig(String sig) {
  1408         return mangleSig(sig, 0, sig.length());
  1409     }
  1410     
  1411     private static String mangleSig(String txt, int first, int last) {
  1412         StringBuilder sb = new StringBuilder();
  1413         for (int i = first; i < last; i++) {
  1414             final char ch = txt.charAt(i);
  1415             switch (ch) {
  1416                 case '/': sb.append('_'); break;
  1417                 case '_': sb.append("_1"); break;
  1418                 case ';': sb.append("_2"); break;
  1419                 case '[': sb.append("_3"); break;
  1420                 default: sb.append(ch); break;
  1421             }
  1422         }
  1423         return sb.toString();
  1424     }
  1425 
  1426     private static String findMethodName(MethodData m, StringBuilder cnt) {
  1427         StringBuilder name = new StringBuilder();
  1428         if ("<init>".equals(m.getName())) { // NOI18N
  1429             name.append("cons"); // NOI18N
  1430         } else if ("<clinit>".equals(m.getName())) { // NOI18N
  1431             name.append("class"); // NOI18N
  1432         } else {
  1433             name.append(m.getName());
  1434         } 
  1435         
  1436         countArgs(m.getInternalSig(), new char[1], name, cnt);
  1437         return name.toString();
  1438     }
  1439 
  1440     static String findMethodName(String[] mi, StringBuilder cnt, char[] returnType) {
  1441         StringBuilder name = new StringBuilder();
  1442         String descr = mi[2];//mi.getDescriptor();
  1443         String nm= mi[1];
  1444         if ("<init>".equals(nm)) { // NOI18N
  1445             name.append("cons"); // NOI18N
  1446         } else {
  1447             name.append(nm);
  1448         }
  1449         countArgs(descr, returnType, name, cnt);
  1450         return name.toString();
  1451     }
  1452 
  1453     private int invokeStaticMethod(byte[] byteCodes, int i, final StackMapper mapper, boolean isStatic)
  1454     throws IOException {
  1455         int methodIndex = readUShortArg(byteCodes, i);
  1456         String[] mi = jc.getFieldInfoName(methodIndex);
  1457         char[] returnType = { 'V' };
  1458         StringBuilder cnt = new StringBuilder();
  1459         String mn = findMethodName(mi, cnt, returnType);
  1460 
  1461         final int numArguments = isStatic ? cnt.length() : cnt.length() + 1;
  1462         final Variable[] vars = new Variable[numArguments];
  1463 
  1464         for (int j = numArguments - 1; j >= 0; --j) {
  1465             vars[j] = mapper.pop();
  1466         }
  1467 
  1468         if (returnType[0] != 'V') {
  1469             out.append("var ")
  1470                .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
  1471                .append(" = ");
  1472         }
  1473 
  1474         final String in = mi[0];
  1475         out.append(accessClass(in.replace('/', '_')));
  1476         out.append("(false).");
  1477         if (mn.startsWith("cons_")) {
  1478             out.append("constructor.");
  1479         }
  1480         out.append(mn);
  1481         if (isStatic) {
  1482             out.append('(');
  1483         } else {
  1484             out.append(".call(");
  1485         }
  1486         if (numArguments > 0) {
  1487             out.append(vars[0]);
  1488             for (int j = 1; j < numArguments; ++j) {
  1489                 out.append(", ");
  1490                 out.append(vars[j]);
  1491             }
  1492         }
  1493         out.append(");");
  1494         i += 2;
  1495         addReference(in);
  1496         return i;
  1497     }
  1498     private int invokeVirtualMethod(byte[] byteCodes, int i, final StackMapper mapper)
  1499     throws IOException {
  1500         int methodIndex = readUShortArg(byteCodes, i);
  1501         String[] mi = jc.getFieldInfoName(methodIndex);
  1502         char[] returnType = { 'V' };
  1503         StringBuilder cnt = new StringBuilder();
  1504         String mn = findMethodName(mi, cnt, returnType);
  1505 
  1506         final int numArguments = cnt.length() + 1;
  1507         final Variable[] vars = new Variable[numArguments];
  1508 
  1509         for (int j = numArguments - 1; j >= 0; --j) {
  1510             vars[j] = mapper.pop();
  1511         }
  1512 
  1513         if (returnType[0] != 'V') {
  1514             out.append("var ")
  1515                .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
  1516                .append(" = ");
  1517         }
  1518 
  1519         out.append(vars[0]).append('.');
  1520         out.append(mn);
  1521         out.append('(');
  1522         String sep = "";
  1523         for (int j = 1; j < numArguments; ++j) {
  1524             out.append(sep);
  1525             out.append(vars[j]);
  1526             sep = ", ";
  1527         }
  1528         out.append(");");
  1529         i += 2;
  1530         return i;
  1531     }
  1532 
  1533     private void addReference(String cn) throws IOException {
  1534         if (requireReference(cn)) {
  1535             debug(" /* needs " + cn + " */");
  1536         }
  1537     }
  1538 
  1539     private void outType(String d, StringBuilder out) {
  1540         int arr = 0;
  1541         while (d.charAt(0) == '[') {
  1542             out.append('A');
  1543             d = d.substring(1);
  1544         }
  1545         if (d.charAt(0) == 'L') {
  1546             assert d.charAt(d.length() - 1) == ';';
  1547             out.append(d.replace('/', '_').substring(0, d.length() - 1));
  1548         } else {
  1549             out.append(d);
  1550         }
  1551     }
  1552 
  1553     private String encodeConstant(int entryIndex) throws IOException {
  1554         String[] classRef = { null };
  1555         String s = jc.stringValue(entryIndex, classRef);
  1556         if (classRef[0] != null) {
  1557             if (classRef[0].startsWith("[")) {
  1558                 s = accessClass("java_lang_Class") + "(false).forName__Ljava_lang_Class_2Ljava_lang_String_2('" + classRef[0] + "');";
  1559             } else {
  1560                 addReference(classRef[0]);
  1561                 s = accessClass(s.replace('/', '_')) + "(false).constructor.$class";
  1562             }
  1563         }
  1564         return s;
  1565     }
  1566 
  1567     private String javaScriptBody(String destObject, MethodData m, boolean isStatic) throws IOException {
  1568         byte[] arr = m.findAnnotationData(true);
  1569         if (arr == null) {
  1570             return null;
  1571         }
  1572         final String jvmType = "Lorg/apidesign/bck2brwsr/core/JavaScriptBody;";
  1573         class P extends AnnotationParser {
  1574             public P() {
  1575                 super(false, true);
  1576             }
  1577             
  1578             int cnt;
  1579             String[] args = new String[30];
  1580             String body;
  1581             
  1582             @Override
  1583             protected void visitAttr(String type, String attr, String at, String value) {
  1584                 if (type.equals(jvmType)) {
  1585                     if ("body".equals(attr)) {
  1586                         body = value;
  1587                     } else if ("args".equals(attr)) {
  1588                         args[cnt++] = value;
  1589                     } else {
  1590                         throw new IllegalArgumentException(attr);
  1591                     }
  1592                 }
  1593             }
  1594         }
  1595         P p = new P();
  1596         p.parse(arr, jc);
  1597         if (p.body == null) {
  1598             return null;
  1599         }
  1600         StringBuilder cnt = new StringBuilder();
  1601         final String mn = findMethodName(m, cnt);
  1602         out.append(destObject).append(".").append(mn);
  1603         out.append(" = function(");
  1604         String space = "";
  1605         int index = 0;
  1606         for (int i = 0; i < cnt.length(); i++) {
  1607             out.append(space);
  1608             space = outputArg(out, p.args, index);
  1609             index++;
  1610         }
  1611         out.append(") {").append("\n");
  1612         out.append(p.body);
  1613         out.append("\n}\n");
  1614         return mn;
  1615     }
  1616     private static String className(ClassData jc) {
  1617         //return jc.getName().getInternalName().replace('/', '_');
  1618         return jc.getClassName().replace('/', '_');
  1619     }
  1620     
  1621     private static String[] findAnnotation(
  1622         byte[] arr, ClassData cd, final String className, 
  1623         final String... attrNames
  1624     ) throws IOException {
  1625         if (arr == null) {
  1626             return null;
  1627         }
  1628         final String[] values = new String[attrNames.length];
  1629         final boolean[] found = { false };
  1630         final String jvmType = "L" + className.replace('.', '/') + ";";
  1631         AnnotationParser ap = new AnnotationParser(false, true) {
  1632             @Override
  1633             protected void visitAttr(String type, String attr, String at, String value) {
  1634                 if (type.equals(jvmType)) {
  1635                     found[0] = true;
  1636                     for (int i = 0; i < attrNames.length; i++) {
  1637                         if (attrNames[i].equals(attr)) {
  1638                             values[i] = value;
  1639                         }
  1640                     }
  1641                 }
  1642             }
  1643             
  1644         };
  1645         ap.parse(arr, cd);
  1646         return found[0] ? values : null;
  1647     }
  1648 
  1649     private CharSequence initField(FieldData v) {
  1650         final String is = v.getInternalSig();
  1651         if (is.length() == 1) {
  1652             switch (is.charAt(0)) {
  1653                 case 'S':
  1654                 case 'J':
  1655                 case 'B':
  1656                 case 'Z':
  1657                 case 'C':
  1658                 case 'I': return " = 0;";
  1659                 case 'F': 
  1660                 case 'D': return " = 0.0;";
  1661                 default:
  1662                     throw new IllegalStateException(is);
  1663             }
  1664         }
  1665         return " = null;";
  1666     }
  1667 
  1668     private void generateAnno(ClassData cd, final Appendable out, byte[] data) throws IOException {
  1669         AnnotationParser ap = new AnnotationParser(true, false) {
  1670             int[] cnt = new int[32];
  1671             int depth;
  1672             
  1673             @Override
  1674             protected void visitAnnotationStart(String attrType, boolean top) throws IOException {
  1675                 final String slashType = attrType.substring(1, attrType.length() - 1);
  1676                 requireReference(slashType);
  1677                 
  1678                 if (cnt[depth]++ > 0) {
  1679                     out.append(",");
  1680                 }
  1681                 if (top) {
  1682                     out.append('"').append(attrType).append("\" : ");
  1683                 }
  1684                 out.append("{\n");
  1685                 cnt[++depth] = 0;
  1686             }
  1687 
  1688             @Override
  1689             protected void visitAnnotationEnd(String type, boolean top) throws IOException {
  1690                 out.append("\n}\n");
  1691                 depth--;
  1692             }
  1693 
  1694             @Override
  1695             protected void visitValueStart(String attrName, char type) throws IOException {
  1696                 if (cnt[depth]++ > 0) {
  1697                     out.append(",\n");
  1698                 }
  1699                 cnt[++depth] = 0;
  1700                 if (attrName != null) {
  1701                     out.append(attrName).append(" : ");
  1702                 }
  1703                 if (type == '[') {
  1704                     out.append("[");
  1705                 }
  1706             }
  1707 
  1708             @Override
  1709             protected void visitValueEnd(String attrName, char type) throws IOException {
  1710                 if (type == '[') {
  1711                     out.append("]");
  1712                 }
  1713                 depth--;
  1714             }
  1715             
  1716             @Override
  1717             protected void visitAttr(String type, String attr, String attrType, String value) 
  1718             throws IOException {
  1719                 if (attr == null && value == null) {
  1720                     return;
  1721                 }
  1722                 out.append(value);
  1723             }
  1724 
  1725             @Override
  1726             protected void visitEnumAttr(String type, String attr, String attrType, String value) 
  1727             throws IOException {
  1728                 final String slashType = attrType.substring(1, attrType.length() - 1);
  1729                 requireReference(slashType);
  1730                 
  1731                 out.append(accessClass(slashType.replace('/', '_')))
  1732                    .append("(false).constructor.").append(value);
  1733             }
  1734         };
  1735         ap.parse(data, cd);
  1736     }
  1737 
  1738     private static String outputArg(Appendable out, String[] args, int indx) throws IOException {
  1739         final String name = args[indx];
  1740         if (name == null) {
  1741             return "";
  1742         }
  1743         if (name.contains(",")) {
  1744             throw new IOException("Wrong parameter with ',': " + name);
  1745         }
  1746         out.append(name);
  1747         return ",";
  1748     }
  1749 
  1750     private static void emit(final Appendable out,
  1751                              final String format,
  1752                              final CharSequence... params) throws IOException {
  1753         final int length = format.length();
  1754 
  1755         int processed = 0;
  1756         int paramOffset = format.indexOf('@');
  1757         while ((paramOffset != -1) && (paramOffset < (length - 1))) {
  1758             final char paramChar = format.charAt(paramOffset + 1);
  1759             if ((paramChar >= '1') && (paramChar <= '9')) {
  1760                 final int paramIndex = paramChar - '0' - 1;
  1761 
  1762                 out.append(format, processed, paramOffset);
  1763                 out.append(params[paramIndex]);
  1764 
  1765                 ++paramOffset;
  1766                 processed = paramOffset + 1;
  1767             }
  1768 
  1769             paramOffset = format.indexOf('@', paramOffset + 1);
  1770         }
  1771 
  1772         out.append(format, processed, length);
  1773     }
  1774 
  1775     private void generateCatch(TrapData[] traps, int current, int topMostLabel) throws IOException {
  1776         out.append("} catch (e) {\n");
  1777         int finallyPC = -1;
  1778         for (TrapData e : traps) {
  1779             if (e == null) {
  1780                 break;
  1781             }
  1782             if (e.catch_cpx != 0) { //not finally
  1783                 final String classInternalName = jc.getClassName(e.catch_cpx);
  1784                 addReference(classInternalName);
  1785                 if ("java/lang/Throwable".equals(classInternalName)) {
  1786                     out.append("if (e.$instOf_java_lang_Throwable) {");
  1787                     out.append("  var stA0 = e;");
  1788                     out.append("} else {");
  1789                     out.append("  var stA0 = vm.java_lang_Throwable(true);");
  1790                     out.append("  vm.java_lang_Throwable.cons__VLjava_lang_String_2.call(stA0, e.toString());");
  1791                     out.append("}");
  1792                     goTo(out, current, e.handler_pc, topMostLabel);
  1793                 } else {
  1794                     out.append("if (e.$instOf_" + classInternalName.replace('/', '_') + ") {");
  1795                     out.append("var stA0 = e;");
  1796                     goTo(out, current, e.handler_pc, topMostLabel);
  1797                     out.append("}\n");
  1798                 }
  1799             } else {
  1800                 finallyPC = e.handler_pc;
  1801             }
  1802         }
  1803         if (finallyPC == -1) {
  1804             out.append("throw e;");
  1805         } else {
  1806             out.append("var stA0 = e;");
  1807             goTo(out, current, finallyPC, topMostLabel);
  1808         }
  1809         out.append("\n}");
  1810     }
  1811 
  1812     private static void goTo(Appendable out, int current, int to, int canBack) throws IOException {
  1813         if (to < current) {
  1814             if (canBack < to) {
  1815                 out.append("{ gt = 0; continue X_" + to + "; }");
  1816             } else {
  1817                 out.append("{ gt = " + to + "; continue X_0; }");
  1818             }
  1819         } else {
  1820             out.append("{ gt = " + to + "; break IF; }");
  1821         }
  1822     }
  1823 
  1824     private static void emitIf(
  1825         Appendable out, String pattern, Variable param, 
  1826         int current, int to, int canBack
  1827     ) throws IOException {
  1828         emit(out, pattern, param);
  1829         goTo(out, current, to, canBack);
  1830     }
  1831 
  1832     private void generateNewArray(int atype, final StackMapper smapper) throws IOException, IllegalStateException {
  1833         String jvmType;
  1834         switch (atype) {
  1835             case 4: jvmType = "[Z"; break;
  1836             case 5: jvmType = "[C"; break;
  1837             case 6: jvmType = "[F"; break;
  1838             case 7: jvmType = "[D"; break;
  1839             case 8: jvmType = "[B"; break;
  1840             case 9: jvmType = "[S"; break;
  1841             case 10: jvmType = "[I"; break;
  1842             case 11: jvmType = "[J"; break;
  1843             default: throw new IllegalStateException("Array type: " + atype);
  1844         }
  1845         emit(out, "var @2 = Array.prototype.newArray__Ljava_lang_Object_2ZLjava_lang_String_2I(true, '@3', @1);",
  1846              smapper.popI(), smapper.pushA(), jvmType);
  1847     }
  1848 
  1849     private void generateANewArray(int type, final StackMapper smapper) throws IOException {
  1850         String typeName = jc.getClassName(type);
  1851         if (typeName.startsWith("[")) {
  1852             typeName = "[" + typeName;
  1853         } else {
  1854             typeName = "[L" + typeName + ";";
  1855         }
  1856         emit(out, "var @2 = Array.prototype.newArray__Ljava_lang_Object_2ZLjava_lang_String_2I(false, '@3', @1);",
  1857              smapper.popI(), smapper.pushA(), typeName);
  1858     }
  1859 
  1860     private int generateMultiANewArray(int type, final byte[] byteCodes, int i, final StackMapper smapper) throws IOException {
  1861         String typeName = jc.getClassName(type);
  1862         int dim = readUByte(byteCodes, ++i);
  1863         StringBuilder dims = new StringBuilder();
  1864         dims.append('[');
  1865         for (int d = 0; d < dim; d++) {
  1866             if (d != 0) {
  1867                 dims.insert(1, ",");
  1868             }
  1869             dims.insert(1, smapper.popI());
  1870         }
  1871         dims.append(']');
  1872         emit(out, "var @2 = Array.prototype.multiNewArray__Ljava_lang_Object_2Ljava_lang_String_2_3II('@3', @1, 0);",
  1873              dims.toString(), smapper.pushA(), typeName);
  1874         return i;
  1875     }
  1876 
  1877     private int generateTableSwitch(int i, final byte[] byteCodes, final StackMapper smapper, int topMostLabel) throws IOException {
  1878         int table = i / 4 * 4 + 4;
  1879         int dflt = i + readInt4(byteCodes, table);
  1880         table += 4;
  1881         int low = readInt4(byteCodes, table);
  1882         table += 4;
  1883         int high = readInt4(byteCodes, table);
  1884         table += 4;
  1885         out.append("switch (").append(smapper.popI()).append(") {\n");
  1886         while (low <= high) {
  1887             int offset = i + readInt4(byteCodes, table);
  1888             table += 4;
  1889             out.append("  case " + low).append(":"); goTo(out, i, offset, topMostLabel); out.append('\n');
  1890             low++;
  1891         }
  1892         out.append("  default: ");
  1893         goTo(out, i, dflt, topMostLabel);
  1894         out.append("\n}");
  1895         i = table - 1;
  1896         return i;
  1897     }
  1898 
  1899     private int generateLookupSwitch(int i, final byte[] byteCodes, final StackMapper smapper, int topMostLabel) throws IOException {
  1900         int table = i / 4 * 4 + 4;
  1901         int dflt = i + readInt4(byteCodes, table);
  1902         table += 4;
  1903         int n = readInt4(byteCodes, table);
  1904         table += 4;
  1905         out.append("switch (").append(smapper.popI()).append(") {\n");
  1906         while (n-- > 0) {
  1907             int cnstnt = readInt4(byteCodes, table);
  1908             table += 4;
  1909             int offset = i + readInt4(byteCodes, table);
  1910             table += 4;
  1911             out.append("  case " + cnstnt).append(": "); goTo(out, i, offset, topMostLabel); out.append('\n');
  1912         }
  1913         out.append("  default: ");
  1914         goTo(out, i, dflt, topMostLabel);
  1915         out.append("\n}");
  1916         i = table - 1;
  1917         return i;
  1918     }
  1919 
  1920     private void generateInstanceOf(int indx, final StackMapper smapper) throws IOException {
  1921         final String type = jc.getClassName(indx);
  1922         if (!type.startsWith("[")) {
  1923             emit(out, "var @2 = @1 != null && @1.$instOf_@3 ? 1 : 0;",
  1924                  smapper.popA(), smapper.pushI(),
  1925                  type.replace('/', '_'));
  1926         } else {
  1927             emit(out, "var @2 = vm.java_lang_Class(false).forName__Ljava_lang_Class_2Ljava_lang_String_2('@3').isInstance__ZLjava_lang_Object_2(@1);",
  1928                 smapper.popA(), smapper.pushI(),
  1929                 type
  1930             );
  1931         }
  1932     }
  1933 
  1934     private void generateCheckcast(int indx, final StackMapper smapper) throws IOException {
  1935         final String type = jc.getClassName(indx);
  1936         if (!type.startsWith("[")) {
  1937             emit(out,
  1938                  "if (@1 !== null && !@1.$instOf_@2) throw vm.java_lang_ClassCastException(true);",
  1939                  smapper.getA(0), type.replace('/', '_'));
  1940         } else {
  1941             emit(out, "vm.java_lang_Class(false).forName__Ljava_lang_Class_2Ljava_lang_String_2('@2').cast__Ljava_lang_Object_2Ljava_lang_Object_2(@1);",
  1942                  smapper.getA(0), type
  1943             );
  1944         }
  1945     }
  1946 
  1947     private void generateByteCodeComment(int prev, int i, final byte[] byteCodes) throws IOException {
  1948         for (int j = prev; j <= i; j++) {
  1949             out.append(" ");
  1950             final int cc = readUByte(byteCodes, j);
  1951             out.append(Integer.toString(cc));
  1952         }
  1953     }
  1954 }