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