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