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