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