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