rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 25 Jan 2016 05:53:21 +0100
changeset 1856 9cdc403018b5
parent 1855 34efbdde4eca
child 1857 f1344425bcb1
permissions -rw-r--r--
Resolving field access during compilation time
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 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 org.apidesign.bck2brwsr.core.JavaScriptBody;
    23 import static org.apidesign.vm4brwsr.ByteCodeParser.*;
    24 import org.apidesign.vm4brwsr.ByteCodeParser.AnnotationParser;
    25 import org.apidesign.vm4brwsr.ByteCodeParser.BootMethodData;
    26 import org.apidesign.vm4brwsr.ByteCodeParser.CPX2;
    27 import org.apidesign.vm4brwsr.ByteCodeParser.ClassData;
    28 import org.apidesign.vm4brwsr.ByteCodeParser.FieldData;
    29 import org.apidesign.vm4brwsr.ByteCodeParser.MethodData;
    30 import org.apidesign.vm4brwsr.ByteCodeParser.StackMapIterator;
    31 import org.apidesign.vm4brwsr.ByteCodeParser.TrapData;
    32 import org.apidesign.vm4brwsr.ByteCodeParser.TrapDataIterator;
    33 
    34 /** Translator of the code inside class files to JavaScript.
    35  *
    36  * @author Jaroslav Tulach <jtulach@netbeans.org>
    37  */
    38 abstract class ByteCodeToJavaScript implements Appendable {
    39     private ClassData jc;
    40     private final Appendable out;
    41     private final StringArray classRefs = new StringArray();
    42     private boolean outChanged;
    43     private boolean callbacks;
    44 
    45     protected ByteCodeToJavaScript(Appendable out) {
    46         this.out = out;
    47     }
    48     
    49     @Override
    50     public final Appendable append(CharSequence csq) throws IOException {
    51         out.append(csq);
    52         outChanged = true;
    53         return this;
    54     }
    55 
    56     @Override
    57     public final Appendable append(CharSequence csq, int start, int end) throws IOException {
    58         out.append(csq, start, end);
    59         outChanged = true;
    60         return this;
    61     }
    62 
    63     @Override
    64     public final Appendable append(char c) throws IOException {
    65         out.append(c);
    66         outChanged = true;
    67         return this;
    68     }
    69     
    70     /* Collects additional required resources.
    71      * 
    72      * @param internalClassName classes that were referenced and should be loaded in order the
    73      *   generated JavaScript code works properly. The names are in internal 
    74      *   JVM form so String is <code>java/lang/String</code>. 
    75      */
    76     protected abstract boolean requireReference(String internalClassName);
    77     
    78     /*
    79      * @param resourcePath name of resources to read
    80      */
    81     protected abstract void requireScript(String resourcePath) throws IOException;
    82     
    83     protected abstract void requireResource(String resourcePath) throws IOException;
    84     
    85     /** Allows subclasses to redefine what field a function representing a
    86      * class gets assigned. By default it returns the suggested name followed
    87      * by <code>" = "</code>;
    88      * 
    89      * @param className suggested name of the class
    90      */
    91     /* protected */ String assignClass(String className) {
    92         return className + " = ";
    93     }
    94     /* protected */ String accessClass(String classOperation) {
    95         return classOperation;
    96     }
    97     
    98     final String accessClassFalse(String classOperation) {
    99         if (mangleClassName(jc.getClassName()).equals(classOperation)) {
   100             return "c";
   101         }
   102         classRefs.addIfMissing(classOperation);
   103         return "(refs_" + classOperation + " || (refs_" + classOperation + " = " + accessClass(classOperation) + "(false)))";
   104     }
   105 
   106     protected FieldData findField(String[] fieldInfoName) throws IOException {
   107         return null;
   108     }
   109 
   110     protected String accessField(String object, FieldData data, String[] fieldInfoName)
   111     throws IOException {
   112         String mangledName = "_" + fieldInfoName[1];
   113         return object + "." + mangledName;
   114     }
   115 
   116     protected String accessStaticMethod(
   117                              String object,
   118                              String mangledName,
   119                              String[] fieldInfoName) throws IOException {
   120         return object + "." + mangledName;
   121     }
   122 
   123     protected String accessVirtualMethod(
   124             String object, 
   125             String mangledName, 
   126             String[] fieldInfoName, 
   127             int params
   128     ) throws IOException {
   129         return object + "." + mangledName + '(';
   130     }
   131 
   132     protected void declaredClass(ClassData classData, String mangledName)
   133             throws IOException {
   134     }
   135 
   136     protected void declaredField(FieldData fieldData,
   137                                  String destObject,
   138                                  String mangledName) throws IOException {
   139     }
   140 
   141     protected void declaredMethod(MethodData methodData,
   142                                   String destObject,
   143                                   String mangledName) throws IOException {
   144     }
   145 
   146     /** Prints out a debug message. 
   147      * 
   148      * @param msg the message
   149      * @return true if the message has been printed
   150      * @throws IOException 
   151      */
   152     boolean debug(String msg) throws IOException {
   153         append(msg);
   154         return true;
   155     }
   156 
   157     /**
   158      * Converts a given class file to a JavaScript version.
   159      *
   160      * @param classFile input stream with code of the .class file
   161      * @return the initialization code for this class, if any. Otherwise <code>null</code>
   162      * 
   163      * @throws IOException if something goes wrong during read or write or translating
   164      */
   165     
   166     public String compile(InputStream classFile) throws IOException {
   167         return compile(new ClassData(classFile));
   168     }
   169 
   170     protected String compile(ClassData classData) throws IOException {
   171         this.jc = classData;
   172         final String cn = this.jc.getClassName();
   173         try {
   174             return compileImpl(cn);
   175         } catch (IOException ex) {
   176             throw new IOException("Cannot compile " + cn + ":", ex);
   177         }
   178     }
   179 
   180     private String compileImpl(final String cn) throws IOException {
   181         this.callbacks = cn.endsWith("/$JsCallbacks$");
   182         if (jc.getMajor_version() < 50 && !cn.endsWith("/package-info")) {
   183             throw new IOException("Can't compile " + cn + ". Class file version " + jc.getMajor_version() + "."
   184                 + jc.getMinor_version() + " - recompile with -target 1.6 (at least)."
   185             );
   186         }
   187         byte[] arrData = jc.findAnnotationData(true);
   188         {
   189             String[] arr = findAnnotation(arrData, jc,
   190                 "org.apidesign.bck2brwsr.core.ExtraJavaScript", 
   191                 "resource", "processByteCode"
   192             );
   193             if (arr != null) {
   194                 if (!arr[0].isEmpty()) {
   195                     requireScript(arr[0]);
   196                 }
   197                 if ("0".equals(arr[1])) {
   198                     return null;
   199                 }
   200             }
   201         }
   202         final String jsResource;
   203         {
   204             String[] arr = findAnnotation(arrData, jc,
   205                 "net.java.html.js.JavaScriptResource", 
   206                 "value"
   207             );
   208             if (arr != null) {
   209                 if (arr[0].startsWith("/")) {
   210                     jsResource = arr[0];
   211                 } else {
   212                     int last = cn.lastIndexOf('/');
   213                     jsResource = cn.substring(0, last + 1).replace('.', '/') + arr[0];
   214                 }
   215             } else {
   216                 jsResource = null;
   217             }
   218         }
   219         String[] proto = findAnnotation(arrData, jc,
   220             "org.apidesign.bck2brwsr.core.JavaScriptPrototype", 
   221             "container", "prototype"
   222         );
   223         StringArray toInitilize = new StringArray();
   224         final String className = className(jc);
   225         append("\n\n").append(assignClass(className));
   226         append("function ").append(className).append("() {");
   227         append("\n  var m;");
   228         append("\n  var CLS = ").append(className).append(';');
   229         append("\n  if (!CLS.$class) {");
   230         if (proto == null) {
   231             String sc = jc.getSuperClassName(); // with _
   232             append("\n    var pp = ").
   233                 append(accessClass(mangleClassName(sc))).append("(true);");
   234             append("\n    var p = CLS.prototype = pp;");
   235             append("\n    var c = p;");
   236             append("\n    var sprcls = pp.constructor.$class;");
   237         } else {
   238             append("\n    var p = CLS.prototype = ").append(proto[1]).append(";");
   239             if (proto[0] == null) {
   240                 proto[0] = "p";
   241             }
   242             append("\n    var c = ").append(proto[0]).append(";");
   243             append("\n    var sprcls = null;");
   244         }
   245         for (FieldData v : jc.getFields()) {
   246             if (v.isStatic()) {
   247                 if ((v.access & ACC_FINAL) != 0 && v.hasConstantValue()) {
   248                     if (v.getInternalSig().length() == 1 || v.getInternalSig().equals("Ljava/lang/String;")) {
   249                         continue;
   250                     }
   251                 }
   252                 append("\n  CLS.fld_").append(v.getName()).append(initField(v));
   253                 append("\n  m = c._").append(v.getName()).append(" = function (v) {")
   254                     .append("  if (arguments.length == 1) CLS.fld_").append(v.getName())
   255                     .append(" = v; return CLS.fld_").
   256                     append(v.getName()).append("; };");
   257             } else {
   258                 append("\n  m = c._").append(v.getName()).append(" = function (v) {")
   259                     .append("  if (arguments.length == 1) this.fld_").
   260                     append(className).append('_').append(v.getName())
   261                     .append(" = v; return this.fld_").
   262                     append(className).append('_').append(v.getName())
   263                     .append("; };");
   264             }
   265 
   266             declaredField(v, "c", "_" + v.getName());
   267         }
   268         for (MethodData m : jc.getMethods()) {
   269             byte[] onlyArr = m.findAnnotationData(true);
   270             if (javaScriptOnly(onlyArr)) continue;
   271             String destObject;
   272             String mn;
   273             append("\n    ");
   274             if (m.isStatic()) {
   275                 destObject = "c";
   276                 mn = generateStaticMethod(destObject, m, toInitilize);
   277             } else {
   278                 if (m.isConstructor()) {
   279                     destObject = "CLS";
   280                     mn = generateInstanceMethod(destObject, m);
   281                 } else {
   282                     destObject = "c";
   283                     mn = generateInstanceMethod(destObject, m);
   284                 }
   285             }
   286             declaredMethod(m, destObject, mn);
   287             byte[] runAnno = m.findAnnotationData(false);
   288             if (runAnno != null) {
   289                 append("\n    m.anno = {");
   290                 generateAnno(jc, runAnno);
   291                 append("\n    };");
   292             }
   293             append("\n    m.access = " + m.getAccess()).append(";");
   294             append("\n    m.cls = CLS;");
   295         }
   296         append("\n    c.constructor = CLS;");
   297         append("\n    function ").append(className).append("fillInstOf(x) {");
   298         String instOfName = "$instOf_" + className;
   299         append("\n        Object.defineProperty(x, '").append(instOfName).append("', { value : true });");
   300         if (jc.isInterface()) {
   301             for (MethodData m : jc.getMethods()) {
   302                 if ((m.getAccess() & ACC_ABSTRACT) == 0
   303                     && (m.getAccess() & ACC_STATIC) == 0
   304                     && (m.getAccess() & ACC_PRIVATE) == 0) {
   305                     final String mn = findMethodName(m, new StringBuilder());
   306                     append("\n        if (!x['").append(mn).append("']) Object.defineProperty(x, '").append(mn).append("', { value : c['").append(mn).append("']});");
   307                 }
   308             }
   309         }
   310         for (String superInterface : jc.getSuperInterfaces()) {
   311             String intrfc = mangleClassName(superInterface);
   312             append("\n      vm.").append(intrfc).append("(false)['fillInstOf'](x);");
   313             requireReference(superInterface);
   314         }
   315         append("\n    }");
   316         append("\n    if (!c.hasOwnProperty('fillInstOf')) Object.defineProperty(c, 'fillInstOf', { value: ").append(className).append("fillInstOf });");
   317         append("\n    ").append(className).append("fillInstOf(c);");
   318 //        obfuscationDelegate.exportJSProperty(this, "c", instOfName);
   319         append("\n    CLS.$class = 'temp';");
   320         append("\n    CLS.$class = ");
   321         append(accessClass("java_lang_Class(true);"));
   322         append("\n    CLS.$class.jvmName = '").append(cn).append("';");
   323         append("\n    CLS.$class.superclass = sprcls;");
   324         append("\n    CLS.$class.interfaces = function() { return [");
   325         {
   326             boolean first = true;
   327             for (String intrfc : jc.getSuperInterfaces()) {
   328                 if (!first) {
   329                     append(",");
   330                 }
   331                 requireReference(intrfc);
   332                 String mangledIface = mangleClassName(intrfc);
   333                 append("\n        ");
   334                 append(accessClass(mangledIface)).append("(false).constructor.$class");
   335                 first = false;
   336             }
   337         }
   338         append("\n    ]; };");
   339         append("\n    CLS.$class.access = ").append(jc.getAccessFlags()+";");
   340         append("\n    CLS.$class.cnstr = CLS;");
   341         byte[] classAnno = jc.findAnnotationData(false);
   342         if (classAnno != null) {
   343             append("\n    CLS.$class.anno = {");
   344             generateAnno(jc, classAnno);
   345             append("\n    };");
   346         }
   347         for (String init : toInitilize.toArray()) {
   348             append("\n    ").append(init).append("();");
   349         }
   350         for (String ref : classRefs.toArray()) {
   351             append("\n    var refs_").append(ref).append(";");
   352         }
   353         classRefs.clear();
   354         
   355         if (jsResource != null) {
   356             requireResource(jsResource);
   357         }
   358         
   359         append("\n  }");
   360         append("\n  if (arguments.length === 0) {");
   361         append("\n    if (!(this instanceof CLS)) {");
   362         append("\n      return new CLS();");
   363         append("\n    }");
   364         for (FieldData v : jc.getFields()) {
   365             byte[] onlyArr = v.findAnnotationData(true);
   366             if (javaScriptOnly(onlyArr)) continue;
   367             if (!v.isStatic()) {
   368                 append("\n    this.fld_").
   369                     append(className).append('_').
   370                     append(v.getName()).append(initField(v));
   371             }
   372         }
   373         append("\n    return this;");
   374         append("\n  }");
   375         append("\n  return arguments[0] ? new CLS() : CLS.prototype;");
   376         append("\n};");
   377 
   378         declaredClass(jc, className);
   379 
   380 //        StringBuilder sb = new StringBuilder();
   381 //        for (String init : toInitilize.toArray()) {
   382 //            sb.append("\n").append(init).append("();");
   383 //        }
   384         return "";
   385     }
   386 
   387     private boolean javaScriptOnly(byte[] anno) throws IOException {
   388         String[] only = findAnnotation(anno, jc,
   389             "org.apidesign.bck2brwsr.core.JavaScriptOnly",
   390             "name", "value"
   391         );
   392         if (only != null) {
   393             if (only[0] != null && only[1] != null) {
   394                 append("\n    p.").append(only[0]).append(" = ")
   395                     .append(only[1]).append(";");
   396             }
   397             if (ExportedSymbols.isMarkedAsExported(anno, jc)) {
   398                 append("\n    p['").append(only[0]).append("'] = p.")
   399                     .append(only[0]).append(";");
   400             }
   401             return true;
   402         }
   403         return false;
   404     }
   405     private String generateStaticMethod(String destObject, MethodData m, StringArray toInitilize) throws IOException {
   406         String jsb = javaScriptBody(destObject, m, true);
   407         if (jsb != null) {
   408             return jsb;
   409         }
   410         final String mn = findMethodName(m, new StringBuilder());
   411         boolean defineProp = generateMethod(destObject, mn, m);
   412         if (mn.equals("class__V")) {
   413             if (defineProp) {
   414                 toInitilize.add(accessClassFalse(className(jc)) + "['" + mn + "']");
   415             } else {
   416                 toInitilize.add(accessClassFalse(className(jc)) + "." + mn);
   417             }
   418         }
   419         return mn;
   420     }
   421 
   422     private String generateInstanceMethod(String destObject, MethodData m) throws IOException {
   423         String jsb = javaScriptBody(destObject, m, false);
   424         if (jsb != null) {
   425             return jsb;
   426         }
   427         final String mn = findMethodName(m, new StringBuilder());
   428         generateMethod(destObject, mn, m);
   429         return mn;
   430     }
   431 
   432     private boolean generateMethod(String destObject, String name, MethodData m)
   433             throws IOException {
   434         final StackMapIterator stackMapIterator = m.createStackMapIterator();
   435         TrapDataIterator trap = m.getTrapDataIterator();
   436         final LocalsMapper lmapper =
   437                 new LocalsMapper(stackMapIterator.getArguments());
   438 
   439         boolean defineProp = 
   440             "java/lang/Object".equals(jc.getClassName()) ||
   441             "java/lang/reflect/Array".equals(jc.getClassName());
   442         
   443         if (defineProp) {
   444             append("Object.defineProperty(").append(destObject).
   445             append(", '").append(name).append("', { configurable: true, writable: true, value: m = function(");
   446         } else {
   447             append("m = ").append(destObject).append(".").append(name).append(" = function(");
   448         }
   449         lmapper.outputArguments(this, m.isStatic());
   450         append(") {").append("\n");
   451 
   452         final byte[] byteCodes = m.getCode();
   453         if (byteCodes == null) {
   454             if (debug("  throw 'no code found for ")) {
   455                this
   456                .append(jc.getClassName()).append('.')
   457                .append(m.getName()).append("';\n");
   458             }
   459             if (defineProp) {
   460                 append("}});");
   461             } else {
   462                 append("};");
   463             }
   464             return defineProp;
   465         }
   466 
   467         final StackMapper smapper = new StackMapper();
   468 
   469         if (!m.isStatic()) {
   470             append("  var ").append(" lcA0 = this;\n");
   471         }
   472 
   473         int lastStackFrame;
   474         TrapData[] previousTrap = null;
   475         boolean wide = false;
   476         boolean didBranches;
   477         if (stackMapIterator.isEmpty()) {
   478             didBranches = false;
   479             lastStackFrame = 0;
   480         } else {
   481             didBranches = true;
   482             lastStackFrame = -1;
   483             append("\n  var gt = 0;\n");
   484         }
   485         
   486         int openBraces = 0;
   487         int topMostLabel = 0;
   488         for (int i = 0; i < byteCodes.length; i++) {
   489             int prev = i;
   490             outChanged = false;
   491             stackMapIterator.advanceTo(i);
   492             boolean changeInCatch = trap.advanceTo(i);
   493             if (changeInCatch || lastStackFrame != stackMapIterator.getFrameIndex()) {
   494                 if (previousTrap != null) {
   495                     generateCatch(previousTrap, i, topMostLabel);
   496                     previousTrap = null;
   497                 }
   498             }
   499             if (lastStackFrame != stackMapIterator.getFrameIndex()) {
   500                 smapper.flush(this);
   501                 if (i != 0) {
   502                     append("    }\n");
   503                 }
   504                 if (openBraces > 64) {
   505                     for (int c = 0; c < 64; c++) {
   506                         append("break;}\n");
   507                     }
   508                     openBraces = 1;
   509                     topMostLabel = i;
   510                 }
   511                 
   512                 lastStackFrame = stackMapIterator.getFrameIndex();
   513                 lmapper.syncWithFrameLocals(stackMapIterator.getFrameLocals());
   514                 smapper.syncWithFrameStack(stackMapIterator.getFrameStack());
   515                 append("    X_" + i).append(": for (;;) { IF: if (gt <= " + i + ") {\n");
   516                 openBraces++;
   517                 changeInCatch = true;
   518             } else {
   519                 debug("    /* " + i + " */ ");
   520             }
   521             if (changeInCatch && trap.useTry()) {
   522                 append("try {");
   523                 previousTrap = trap.current();
   524             }
   525             final int c = readUByte(byteCodes, i);
   526             switch (c) {
   527                 case opc_aload_0:
   528                     smapper.assign(this, VarType.REFERENCE, lmapper.getA(0));
   529                     break;
   530                 case opc_iload_0:
   531                     smapper.assign(this, VarType.INTEGER, lmapper.getI(0));
   532                     break;
   533                 case opc_lload_0:
   534                     smapper.assign(this, VarType.LONG, lmapper.getL(0));
   535                     break;
   536                 case opc_fload_0:
   537                     smapper.assign(this, VarType.FLOAT, lmapper.getF(0));
   538                     break;
   539                 case opc_dload_0:
   540                     smapper.assign(this, VarType.DOUBLE, lmapper.getD(0));
   541                     break;
   542                 case opc_aload_1:
   543                     smapper.assign(this, VarType.REFERENCE, lmapper.getA(1));
   544                     break;
   545                 case opc_iload_1:
   546                     smapper.assign(this, VarType.INTEGER, lmapper.getI(1));
   547                     break;
   548                 case opc_lload_1:
   549                     smapper.assign(this, VarType.LONG, lmapper.getL(1));
   550                     break;
   551                 case opc_fload_1:
   552                     smapper.assign(this, VarType.FLOAT, lmapper.getF(1));
   553                     break;
   554                 case opc_dload_1:
   555                     smapper.assign(this, VarType.DOUBLE, lmapper.getD(1));
   556                     break;
   557                 case opc_aload_2:
   558                     smapper.assign(this, VarType.REFERENCE, lmapper.getA(2));
   559                     break;
   560                 case opc_iload_2:
   561                     smapper.assign(this, VarType.INTEGER, lmapper.getI(2));
   562                     break;
   563                 case opc_lload_2:
   564                     smapper.assign(this, VarType.LONG, lmapper.getL(2));
   565                     break;
   566                 case opc_fload_2:
   567                     smapper.assign(this, VarType.FLOAT, lmapper.getF(2));
   568                     break;
   569                 case opc_dload_2:
   570                     smapper.assign(this, VarType.DOUBLE, lmapper.getD(2));
   571                     break;
   572                 case opc_aload_3:
   573                     smapper.assign(this, VarType.REFERENCE, lmapper.getA(3));
   574                     break;
   575                 case opc_iload_3:
   576                     smapper.assign(this, VarType.INTEGER, lmapper.getI(3));
   577                     break;
   578                 case opc_lload_3:
   579                     smapper.assign(this, VarType.LONG, lmapper.getL(3));
   580                     break;
   581                 case opc_fload_3:
   582                     smapper.assign(this, VarType.FLOAT, lmapper.getF(3));
   583                     break;
   584                 case opc_dload_3:
   585                     smapper.assign(this, VarType.DOUBLE, lmapper.getD(3));
   586                     break;
   587                 case opc_iload: {
   588                     ++i;
   589                     final int indx = wide ? readUShort(byteCodes, i++)
   590                                           : readUByte(byteCodes, i);
   591                     wide = false;
   592                     smapper.assign(this, VarType.INTEGER, lmapper.getI(indx));
   593                     break;
   594                 }
   595                 case opc_lload: {
   596                     ++i;
   597                     final int indx = wide ? readUShort(byteCodes, i++)
   598                                           : readUByte(byteCodes, i);
   599                     wide = false;
   600                     smapper.assign(this, VarType.LONG, lmapper.getL(indx));
   601                     break;
   602                 }
   603                 case opc_fload: {
   604                     ++i;
   605                     final int indx = wide ? readUShort(byteCodes, i++)
   606                                           : readUByte(byteCodes, i);
   607                     wide = false;
   608                     smapper.assign(this, VarType.FLOAT, lmapper.getF(indx));
   609                     break;
   610                 }
   611                 case opc_dload: {
   612                     ++i;
   613                     final int indx = wide ? readUShort(byteCodes, i++)
   614                                           : readUByte(byteCodes, i);
   615                     wide = false;
   616                     smapper.assign(this, VarType.DOUBLE, lmapper.getD(indx));
   617                     break;
   618                 }
   619                 case opc_aload: {
   620                     ++i;
   621                     final int indx = wide ? readUShort(byteCodes, i++)
   622                                           : readUByte(byteCodes, i);
   623                     wide = false;
   624                     smapper.assign(this, VarType.REFERENCE, lmapper.getA(indx));
   625                     break;
   626                 }
   627                 case opc_istore: {
   628                     ++i;
   629                     final int indx = wide ? readUShort(byteCodes, i++)
   630                                           : readUByte(byteCodes, i);
   631                     wide = false;
   632                     emit(smapper, this, "var @1 = @2;",
   633                          lmapper.setI(indx), smapper.popI());
   634                     break;
   635                 }
   636                 case opc_lstore: {
   637                     ++i;
   638                     final int indx = wide ? readUShort(byteCodes, i++)
   639                                           : readUByte(byteCodes, i);
   640                     wide = false;
   641                     emit(smapper, this, "var @1 = @2;",
   642                          lmapper.setL(indx), smapper.popL());
   643                     break;
   644                 }
   645                 case opc_fstore: {
   646                     ++i;
   647                     final int indx = wide ? readUShort(byteCodes, i++)
   648                                           : readUByte(byteCodes, i);
   649                     wide = false;
   650                     emit(smapper, this, "var @1 = @2;",
   651                          lmapper.setF(indx), smapper.popF());
   652                     break;
   653                 }
   654                 case opc_dstore: {
   655                     ++i;
   656                     final int indx = wide ? readUShort(byteCodes, i++)
   657                                           : readUByte(byteCodes, i);
   658                     wide = false;
   659                     emit(smapper, this, "var @1 = @2;",
   660                          lmapper.setD(indx), smapper.popD());
   661                     break;
   662                 }
   663                 case opc_astore: {
   664                     ++i;
   665                     final int indx = wide ? readUShort(byteCodes, i++)
   666                                           : readUByte(byteCodes, i);
   667                     wide = false;
   668                     emit(smapper, this, "var @1 = @2;",
   669                          lmapper.setA(indx), smapper.popA());
   670                     break;
   671                 }
   672                 case opc_astore_0:
   673                     emit(smapper, this, "var @1 = @2;", lmapper.setA(0), smapper.popA());
   674                     break;
   675                 case opc_istore_0:
   676                     emit(smapper, this, "var @1 = @2;", lmapper.setI(0), smapper.popI());
   677                     break;
   678                 case opc_lstore_0:
   679                     emit(smapper, this, "var @1 = @2;", lmapper.setL(0), smapper.popL());
   680                     break;
   681                 case opc_fstore_0:
   682                     emit(smapper, this, "var @1 = @2;", lmapper.setF(0), smapper.popF());
   683                     break;
   684                 case opc_dstore_0:
   685                     emit(smapper, this, "var @1 = @2;", lmapper.setD(0), smapper.popD());
   686                     break;
   687                 case opc_astore_1:
   688                     emit(smapper, this, "var @1 = @2;", lmapper.setA(1), smapper.popA());
   689                     break;
   690                 case opc_istore_1:
   691                     emit(smapper, this, "var @1 = @2;", lmapper.setI(1), smapper.popI());
   692                     break;
   693                 case opc_lstore_1:
   694                     emit(smapper, this, "var @1 = @2;", lmapper.setL(1), smapper.popL());
   695                     break;
   696                 case opc_fstore_1:
   697                     emit(smapper, this, "var @1 = @2;", lmapper.setF(1), smapper.popF());
   698                     break;
   699                 case opc_dstore_1:
   700                     emit(smapper, this, "var @1 = @2;", lmapper.setD(1), smapper.popD());
   701                     break;
   702                 case opc_astore_2:
   703                     emit(smapper, this, "var @1 = @2;", lmapper.setA(2), smapper.popA());
   704                     break;
   705                 case opc_istore_2:
   706                     emit(smapper, this, "var @1 = @2;", lmapper.setI(2), smapper.popI());
   707                     break;
   708                 case opc_lstore_2:
   709                     emit(smapper, this, "var @1 = @2;", lmapper.setL(2), smapper.popL());
   710                     break;
   711                 case opc_fstore_2:
   712                     emit(smapper, this, "var @1 = @2;", lmapper.setF(2), smapper.popF());
   713                     break;
   714                 case opc_dstore_2:
   715                     emit(smapper, this, "var @1 = @2;", lmapper.setD(2), smapper.popD());
   716                     break;
   717                 case opc_astore_3:
   718                     emit(smapper, this, "var @1 = @2;", lmapper.setA(3), smapper.popA());
   719                     break;
   720                 case opc_istore_3:
   721                     emit(smapper, this, "var @1 = @2;", lmapper.setI(3), smapper.popI());
   722                     break;
   723                 case opc_lstore_3:
   724                     emit(smapper, this, "var @1 = @2;", lmapper.setL(3), smapper.popL());
   725                     break;
   726                 case opc_fstore_3:
   727                     emit(smapper, this, "var @1 = @2;", lmapper.setF(3), smapper.popF());
   728                     break;
   729                 case opc_dstore_3:
   730                     emit(smapper, this, "var @1 = @2;", lmapper.setD(3), smapper.popD());
   731                     break;
   732                 case opc_iadd:
   733                     smapper.replace(this, VarType.INTEGER, "(@1).add32(@2)", smapper.getI(1), smapper.popI());
   734                     break;
   735                 case opc_ladd:
   736                     smapper.replace(this, VarType.LONG, "(@1).add64(@2)", smapper.getL(1), smapper.popL());
   737                     break;
   738                 case opc_fadd:
   739                     smapper.replace(this, VarType.FLOAT, "(@1 + @2)", smapper.getF(1), smapper.popF());
   740                     break;
   741                 case opc_dadd:
   742                     smapper.replace(this, VarType.DOUBLE, "(@1 + @2)", smapper.getD(1), smapper.popD());
   743                     break;
   744                 case opc_isub:
   745                     smapper.replace(this, VarType.INTEGER, "(@1).sub32(@2)", smapper.getI(1), smapper.popI());
   746                     break;
   747                 case opc_lsub:
   748                     smapper.replace(this, VarType.LONG, "(@1).sub64(@2)", smapper.getL(1), smapper.popL());
   749                     break;
   750                 case opc_fsub:
   751                     smapper.replace(this, VarType.FLOAT, "(@1 - @2)", smapper.getF(1), smapper.popF());
   752                     break;
   753                 case opc_dsub:
   754                     smapper.replace(this, VarType.DOUBLE, "(@1 - @2)", smapper.getD(1), smapper.popD());
   755                     break;
   756                 case opc_imul:
   757                     smapper.replace(this, VarType.INTEGER, "(@1).mul32(@2)", smapper.getI(1), smapper.popI());
   758                     break;
   759                 case opc_lmul:
   760                     smapper.replace(this, VarType.LONG, "(@1).mul64(@2)", smapper.getL(1), smapper.popL());
   761                     break;
   762                 case opc_fmul:
   763                     smapper.replace(this, VarType.FLOAT, "(@1 * @2)", smapper.getF(1), smapper.popF());
   764                     break;
   765                 case opc_dmul:
   766                     smapper.replace(this, VarType.DOUBLE, "(@1 * @2)", smapper.getD(1), smapper.popD());
   767                     break;
   768                 case opc_idiv:
   769                     smapper.replace(this, VarType.INTEGER, "(@1).div32(@2)",
   770                          smapper.getI(1), smapper.popI());
   771                     break;
   772                 case opc_ldiv:
   773                     smapper.replace(this, VarType.LONG, "(@1).div64(@2)",
   774                          smapper.getL(1), smapper.popL());
   775                     break;
   776                 case opc_fdiv:
   777                     smapper.replace(this, VarType.FLOAT, "(@1 / @2)", smapper.getF(1), smapper.popF());
   778                     break;
   779                 case opc_ddiv:
   780                     smapper.replace(this, VarType.DOUBLE, "(@1 / @2)", smapper.getD(1), smapper.popD());
   781                     break;
   782                 case opc_irem:
   783                     smapper.replace(this, VarType.INTEGER, "(@1).mod32(@2)",
   784                          smapper.getI(1), smapper.popI());
   785                     break;
   786                 case opc_lrem:
   787                     smapper.replace(this, VarType.LONG, "(@1).mod64(@2)",
   788                          smapper.getL(1), smapper.popL());
   789                     break;
   790                 case opc_frem:
   791                     smapper.replace(this, VarType.FLOAT, "(@1 % @2)", smapper.getF(1), smapper.popF());
   792                     break;
   793                 case opc_drem:
   794                     smapper.replace(this, VarType.DOUBLE, "(@1 % @2)", smapper.getD(1), smapper.popD());
   795                     break;
   796                 case opc_iand:
   797                     smapper.replace(this, VarType.INTEGER, "(@1 & @2)", smapper.getI(1), smapper.popI());
   798                     break;
   799                 case opc_land:
   800                     smapper.replace(this, VarType.LONG, "(@1).and64(@2)", smapper.getL(1), smapper.popL());
   801                     break;
   802                 case opc_ior:
   803                     smapper.replace(this, VarType.INTEGER, "(@1 | @2)", smapper.getI(1), smapper.popI());
   804                     break;
   805                 case opc_lor:
   806                     smapper.replace(this, VarType.LONG, "(@1).or64(@2)", smapper.getL(1), smapper.popL());
   807                     break;
   808                 case opc_ixor:
   809                     smapper.replace(this, VarType.INTEGER, "(@1 ^ @2)", smapper.getI(1), smapper.popI());
   810                     break;
   811                 case opc_lxor:
   812                     smapper.replace(this, VarType.LONG, "(@1).xor64(@2)", smapper.getL(1), smapper.popL());
   813                     break;
   814                 case opc_ineg:
   815                     smapper.replace(this, VarType.INTEGER, "(@1).neg32()", smapper.getI(0));
   816                     break;
   817                 case opc_lneg:
   818                     smapper.replace(this, VarType.LONG, "(@1).neg64()", smapper.getL(0));
   819                     break;
   820                 case opc_fneg:
   821                     smapper.replace(this, VarType.FLOAT, "(-@1)", smapper.getF(0));
   822                     break;
   823                 case opc_dneg:
   824                     smapper.replace(this, VarType.DOUBLE, "(-@1)", smapper.getD(0));
   825                     break;
   826                 case opc_ishl:
   827                     smapper.replace(this, VarType.INTEGER, "(@1 << @2)", smapper.getI(1), smapper.popI());
   828                     break;
   829                 case opc_lshl:
   830                     smapper.replace(this, VarType.LONG, "(@1).shl64(@2)", smapper.getL(1), smapper.popI());
   831                     break;
   832                 case opc_ishr:
   833                     smapper.replace(this, VarType.INTEGER, "(@1 >> @2)", smapper.getI(1), smapper.popI());
   834                     break;
   835                 case opc_lshr:
   836                     smapper.replace(this, VarType.LONG, "(@1).shr64(@2)", smapper.getL(1), smapper.popI());
   837                     break;
   838                 case opc_iushr:
   839                     smapper.replace(this, VarType.INTEGER, "(@1 >>> @2)", smapper.getI(1), smapper.popI());
   840                     break;
   841                 case opc_lushr:
   842                     smapper.replace(this, VarType.LONG, "(@1).ushr64(@2)", smapper.getL(1), smapper.popI());
   843                     break;
   844                 case opc_iinc: {
   845                     ++i;
   846                     final int varIndx = wide ? readUShort(byteCodes, i++)
   847                                              : readUByte(byteCodes, i);
   848                     ++i;
   849                     final int incrBy = wide ? readShort(byteCodes, i++)
   850                                             : byteCodes[i];
   851                     wide = false;
   852                     if (incrBy == 1) {
   853                         emit(smapper, this, "@1++;", lmapper.getI(varIndx));
   854                     } else {
   855                         emit(smapper, this, "@1 += @2;",
   856                              lmapper.getI(varIndx),
   857                              Integer.toString(incrBy));
   858                     }
   859                     break;
   860                 }
   861                 case opc_return:
   862                     emit(smapper, this, "return;");
   863                     break;
   864                 case opc_ireturn:
   865                     emit(smapper, this, "return @1;", smapper.popI());
   866                     break;
   867                 case opc_lreturn:
   868                     emit(smapper, this, "return @1;", smapper.popL());
   869                     break;
   870                 case opc_freturn:
   871                     emit(smapper, this, "return @1;", smapper.popF());
   872                     break;
   873                 case opc_dreturn:
   874                     emit(smapper, this, "return @1;", smapper.popD());
   875                     break;
   876                 case opc_areturn:
   877                     emit(smapper, this, "return @1;", smapper.popA());
   878                     break;
   879                 case opc_i2l:
   880                     smapper.replace(this, VarType.LONG, "@1", smapper.getI(0));
   881                     break;
   882                 case opc_i2f:
   883                     smapper.replace(this, VarType.FLOAT, "@1", smapper.getI(0));
   884                     break;
   885                 case opc_i2d:
   886                     smapper.replace(this, VarType.DOUBLE, "@1", smapper.getI(0));
   887                     break;
   888                 case opc_l2i:
   889                     smapper.replace(this, VarType.INTEGER, "(@1).toInt32()", smapper.getL(0));
   890                     break;
   891                     // max int check?
   892                 case opc_l2f:
   893                     smapper.replace(this, VarType.FLOAT, "(@1).toFP()", smapper.getL(0));
   894                     break;
   895                 case opc_l2d:
   896                     smapper.replace(this, VarType.DOUBLE, "(@1).toFP()", smapper.getL(0));
   897                     break;
   898                 case opc_f2d:
   899                     smapper.replace(this, VarType.DOUBLE, "@1",
   900                          smapper.getF(0));
   901                     break;
   902                 case opc_d2f:
   903                     smapper.replace(this, VarType.FLOAT, "@1",
   904                          smapper.getD(0));
   905                     break;
   906                 case opc_f2i:
   907                     smapper.replace(this, VarType.INTEGER, "(@1).toInt32()",
   908                          smapper.getF(0));
   909                     break;
   910                 case opc_f2l:
   911                     smapper.replace(this, VarType.LONG, "(@1).toLong()",
   912                          smapper.getF(0));
   913                     break;
   914                 case opc_d2i:
   915                     smapper.replace(this, VarType.INTEGER, "(@1).toInt32()",
   916                          smapper.getD(0));
   917                     break;
   918                 case opc_d2l:
   919                     smapper.replace(this, VarType.LONG, "(@1).toLong()", smapper.getD(0));
   920                     break;
   921                 case opc_i2b:
   922                     smapper.replace(this, VarType.INTEGER, "(@1).toInt8()", smapper.getI(0));
   923                     break;
   924                 case opc_i2c:
   925                 case opc_i2s:
   926                     smapper.replace(this, VarType.INTEGER, "(@1).toInt16()", smapper.getI(0));
   927                     break;
   928                 case opc_aconst_null:
   929                     smapper.assign(this, VarType.REFERENCE, "null");
   930                     break;
   931                 case opc_iconst_m1:
   932                     smapper.assign(this, VarType.INTEGER, "-1");
   933                     break;
   934                 case opc_iconst_0:
   935                     smapper.assign(this, VarType.INTEGER, "0");
   936                     break;
   937                 case opc_dconst_0:
   938                     smapper.assign(this, VarType.DOUBLE, "0");
   939                     break;
   940                 case opc_lconst_0:
   941                     smapper.assign(this, VarType.LONG, "0");
   942                     break;
   943                 case opc_fconst_0:
   944                     smapper.assign(this, VarType.FLOAT, "0");
   945                     break;
   946                 case opc_iconst_1:
   947                     smapper.assign(this, VarType.INTEGER, "1");
   948                     break;
   949                 case opc_lconst_1:
   950                     smapper.assign(this, VarType.LONG, "1");
   951                     break;
   952                 case opc_fconst_1:
   953                     smapper.assign(this, VarType.FLOAT, "1");
   954                     break;
   955                 case opc_dconst_1:
   956                     smapper.assign(this, VarType.DOUBLE, "1");
   957                     break;
   958                 case opc_iconst_2:
   959                     smapper.assign(this, VarType.INTEGER, "2");
   960                     break;
   961                 case opc_fconst_2:
   962                     smapper.assign(this, VarType.FLOAT, "2");
   963                     break;
   964                 case opc_iconst_3:
   965                     smapper.assign(this, VarType.INTEGER, "3");
   966                     break;
   967                 case opc_iconst_4:
   968                     smapper.assign(this, VarType.INTEGER, "4");
   969                     break;
   970                 case opc_iconst_5:
   971                     smapper.assign(this, VarType.INTEGER, "5");
   972                     break;
   973                 case opc_ldc: {
   974                     int indx = readUByte(byteCodes, ++i);
   975                     String v = encodeConstant(indx);
   976                     int type = VarType.fromConstantType(jc.getTag(indx));
   977                     smapper.assign(this, type, v);
   978                     break;
   979                 }
   980                 case opc_ldc_w:
   981                 case opc_ldc2_w: {
   982                     int indx = readUShortArg(byteCodes, i);
   983                     i += 2;
   984                     String v = encodeConstant(indx);
   985                     int type = VarType.fromConstantType(jc.getTag(indx));
   986                     if (type == VarType.LONG) {
   987                         final Long lv = new Long(v);
   988                         final int low = (int)(lv.longValue() & 0xFFFFFFFF);
   989                         final int hi = (int)(lv.longValue() >> 32);
   990                         if (hi == 0) {
   991                             smapper.assign(this, VarType.LONG, "0x" + Integer.toHexString(low));
   992                         } else {
   993                             smapper.assign(this, VarType.LONG,
   994                                 "0x" + Integer.toHexString(hi) + ".next32(0x" + 
   995                                     Integer.toHexString(low) + ")"
   996                             );
   997                         }
   998                     } else {
   999                         smapper.assign(this, type, v);
  1000                     }
  1001                     break;
  1002                 }
  1003                 case opc_lcmp:
  1004                     smapper.replace(this, VarType.INTEGER, "(@2).compare64(@1)", smapper.popL(), smapper.getL(0));
  1005                     break;
  1006                 case opc_fcmpl:
  1007                 case opc_fcmpg:
  1008                     emit(smapper, this, "var @3 = (@2 == @1) ? 0 : ((@2 < @1) ? -1 : 1);",
  1009                          smapper.popF(), smapper.popF(), smapper.pushI());
  1010                     break;
  1011                 case opc_dcmpl:
  1012                 case opc_dcmpg:
  1013                     emit(smapper, this, "var @3 = (@2 == @1) ? 0 : ((@2 < @1) ? -1 : 1);",
  1014                          smapper.popD(), smapper.popD(), smapper.pushI());
  1015                     break;
  1016                 case opc_if_acmpeq:
  1017                     i = generateIf(smapper, byteCodes, i, smapper.popA(), smapper.popA(),
  1018                                    "===", topMostLabel);
  1019                     break;
  1020                 case opc_if_acmpne:
  1021                     i = generateIf(smapper, byteCodes, i, smapper.popA(), smapper.popA(),
  1022                                    "!==", topMostLabel);
  1023                     break;
  1024                 case opc_if_icmpeq:
  1025                     i = generateIf(smapper, byteCodes, i, smapper.popI(), smapper.popI(),
  1026                                    "==", topMostLabel);
  1027                     break;
  1028                 case opc_ifeq: {
  1029                     int indx = i + readShortArg(byteCodes, i);
  1030                     emitIf(smapper, this, "if ((@1) == 0) ",
  1031                          smapper.popI(), i, indx, topMostLabel);
  1032                     i += 2;
  1033                     break;
  1034                 }
  1035                 case opc_ifne: {
  1036                     int indx = i + readShortArg(byteCodes, i);
  1037                     emitIf(smapper, this, "if ((@1) != 0) ",
  1038                          smapper.popI(), i, indx, topMostLabel);
  1039                     i += 2;
  1040                     break;
  1041                 }
  1042                 case opc_iflt: {
  1043                     int indx = i + readShortArg(byteCodes, i);
  1044                     emitIf(smapper, this, "if ((@1) < 0) ",
  1045                          smapper.popI(), i, indx, topMostLabel);
  1046                     i += 2;
  1047                     break;
  1048                 }
  1049                 case opc_ifle: {
  1050                     int indx = i + readShortArg(byteCodes, i);
  1051                     emitIf(smapper, this, "if ((@1) <= 0) ",
  1052                          smapper.popI(), i, indx, topMostLabel);
  1053                     i += 2;
  1054                     break;
  1055                 }
  1056                 case opc_ifgt: {
  1057                     int indx = i + readShortArg(byteCodes, i);
  1058                     emitIf(smapper, this, "if ((@1) > 0) ",
  1059                          smapper.popI(), i, indx, topMostLabel);
  1060                     i += 2;
  1061                     break;
  1062                 }
  1063                 case opc_ifge: {
  1064                     int indx = i + readShortArg(byteCodes, i);
  1065                     emitIf(smapper, this, "if ((@1) >= 0) ",
  1066                          smapper.popI(), i, indx, topMostLabel);
  1067                     i += 2;
  1068                     break;
  1069                 }
  1070                 case opc_ifnonnull: {
  1071                     int indx = i + readShortArg(byteCodes, i);
  1072                     emitIf(smapper, this, "if ((@1) !== null) ",
  1073                          smapper.popA(), i, indx, topMostLabel);
  1074                     i += 2;
  1075                     break;
  1076                 }
  1077                 case opc_ifnull: {
  1078                     int indx = i + readShortArg(byteCodes, i);
  1079                     emitIf(smapper, this, "if ((@1) === null) ",
  1080                          smapper.popA(), i, indx, topMostLabel);
  1081                     i += 2;
  1082                     break;
  1083                 }
  1084                 case opc_if_icmpne:
  1085                     i = generateIf(smapper, byteCodes, i, smapper.popI(), smapper.popI(),
  1086                                    "!=", topMostLabel);
  1087                     break;
  1088                 case opc_if_icmplt:
  1089                     i = generateIf(smapper, byteCodes, i, smapper.popI(), smapper.popI(),
  1090                                    "<", topMostLabel);
  1091                     break;
  1092                 case opc_if_icmple:
  1093                     i = generateIf(smapper, byteCodes, i, smapper.popI(), smapper.popI(),
  1094                                    "<=", topMostLabel);
  1095                     break;
  1096                 case opc_if_icmpgt:
  1097                     i = generateIf(smapper, byteCodes, i, smapper.popI(), smapper.popI(),
  1098                                    ">", topMostLabel);
  1099                     break;
  1100                 case opc_if_icmpge:
  1101                     i = generateIf(smapper, byteCodes, i, smapper.popI(), smapper.popI(),
  1102                                    ">=", topMostLabel);
  1103                     break;
  1104                 case opc_goto: {
  1105                     smapper.flush(this);
  1106                     int indx = i + readShortArg(byteCodes, i);
  1107                     goTo(this, i, indx, topMostLabel);
  1108                     i += 2;
  1109                     break;
  1110                 }
  1111                 case opc_lookupswitch: {
  1112                     i = generateLookupSwitch(i, byteCodes, smapper, topMostLabel);
  1113                     break;
  1114                 }
  1115                 case opc_tableswitch: {
  1116                     i = generateTableSwitch(i, byteCodes, smapper, topMostLabel);
  1117                     break;
  1118                 }
  1119                 case opc_invokeinterface: {
  1120                     i = invokeVirtualMethod(byteCodes, i, smapper) + 2;
  1121                     break;
  1122                 }
  1123                 case opc_invokevirtual:
  1124                     i = invokeVirtualMethod(byteCodes, i, smapper);
  1125                     break;
  1126                 case opc_invokespecial:
  1127                     i = invokeStaticMethod(byteCodes, i, smapper, false);
  1128                     break;
  1129                 case opc_invokestatic:
  1130                     i = invokeStaticMethod(byteCodes, i, smapper, true);
  1131                     break;
  1132                 case opc_invokedynamic: {
  1133                     int indx = readUShortArg(byteCodes, i);
  1134                     println("invoke dynamic: " + indx);
  1135                     ByteCodeParser.CPX2 c2 = jc.getCpoolEntry(indx);
  1136                     BootMethodData bm = jc.getBootMethod(c2.cpx1);
  1137                     CPX2 methodHandle = jc.getCpoolEntry(bm.method);
  1138                     println("  type: " + methodHandle.cpx1);
  1139                     String[] mi = jc.getFieldInfoName(methodHandle.cpx2);
  1140                     String mcn = mangleClassName(mi[0]);
  1141                     char[] returnType = {'V'};
  1142                     StringBuilder cnt = new StringBuilder();
  1143                     String mn = findMethodName(mi, cnt, returnType);
  1144                     StringBuilder sb = new StringBuilder();
  1145                     sb.append("We don't handle invokedynamic, need to preprocess ahead-of-time:\n");
  1146                     sb.append("  mi[0]: ").append(mi[0]).append("\n");
  1147                     sb.append("  mi[1]: ").append(mi[1]).append("\n");
  1148                     sb.append("  mi[2]: ").append(mi[2]).append("\n");
  1149                     sb.append("  mn   : ").append(mn).append("\n");
  1150                     sb.append("  name and type: ").append(jc.stringValue(c2.cpx2, true)).append("\n");
  1151                     throw new IOException(sb.toString());
  1152                     /*
  1153                     CPX2 nameAndType = jc.getCpoolEntry(c2.cpx2);
  1154                     String type = jc.StringValue(nameAndType.cpx2);
  1155                     String object = accessClass(mcn) + "(false)";
  1156                     if (mn.startsWith("cons_")) {
  1157                         object += ".constructor";
  1158                     }
  1159                     append("var metHan = ");
  1160                     append(accessStaticMethod(object, mn, mi));
  1161                     append('(');
  1162                     String lookup = accessClass("java_lang_invoke_MethodHandles") + "(false).findFor__Ljava_lang_invoke_MethodHandles$Lookup_2Ljava_lang_Class_2(CLS.$class)";
  1163                     append(lookup);
  1164                     append(", '").append(mi[1]).append("', ");
  1165                     String methodType = accessClass("java_lang_invoke_MethodType") + "(false).fromMethodDescriptorString__Ljava_lang_invoke_MethodType_2Ljava_lang_String_2Ljava_lang_ClassLoader_2(";
  1166                     append(methodType).append("'").append(type).append("', null)");
  1167 //                    if (numArguments > 0) {
  1168 //                        append(vars[0]);
  1169 //                        for (int j = 1; j < numArguments; ++j) {
  1170 //                            append(", ");
  1171 //                            append(vars[j]);
  1172 //                        }
  1173 //                    }
  1174                     append(");");
  1175                     emit(smapper, this, "throw 'Invoke dynamic: ' + @1 + ': ' + metHan;", "" + indx);
  1176                     i += 4;
  1177                     break;
  1178                     */
  1179                 }
  1180                 case opc_new: {
  1181                     int indx = readUShortArg(byteCodes, i);
  1182                     String ci = jc.getClassName(indx);
  1183                     emit(smapper, this, "var @1 = new @2;",
  1184                          smapper.pushA(), accessClass(mangleClassName(ci)));
  1185                     addReference(ci);
  1186                     i += 2;
  1187                     break;
  1188                 }
  1189                 case opc_newarray:
  1190                     int atype = readUByte(byteCodes, ++i);
  1191                     generateNewArray(atype, smapper);
  1192                     break;
  1193                 case opc_anewarray: {
  1194                     int type = readUShortArg(byteCodes, i);
  1195                     i += 2;
  1196                     generateANewArray(type, smapper);
  1197                     break;
  1198                 }
  1199                 case opc_multianewarray: {
  1200                     int type = readUShortArg(byteCodes, i);
  1201                     i += 2;
  1202                     i = generateMultiANewArray(type, byteCodes, i, smapper);
  1203                     break;
  1204                 }
  1205                 case opc_arraylength:
  1206                     smapper.replace(this, VarType.INTEGER, "(@1).length", smapper.getA(0));
  1207                     break;
  1208                 case opc_lastore:
  1209                     emit(smapper, this, "Array.at(@3, @2, @1);",
  1210                          smapper.popL(), smapper.popI(), smapper.popA());
  1211                     break;
  1212                 case opc_fastore:
  1213                     emit(smapper, this, "Array.at(@3, @2, @1);",
  1214                          smapper.popF(), smapper.popI(), smapper.popA());
  1215                     break;
  1216                 case opc_dastore:
  1217                     emit(smapper, this, "Array.at(@3, @2, @1);",
  1218                          smapper.popD(), smapper.popI(), smapper.popA());
  1219                     break;
  1220                 case opc_aastore:
  1221                     emit(smapper, this, "Array.at(@3, @2, @1);",
  1222                          smapper.popA(), smapper.popI(), smapper.popA());
  1223                     break;
  1224                 case opc_iastore:
  1225                 case opc_bastore:
  1226                 case opc_castore:
  1227                 case opc_sastore:
  1228                     emit(smapper, this, "Array.at(@3, @2, @1);",
  1229                          smapper.popI(), smapper.popI(), smapper.popA());
  1230                     break;
  1231                 case opc_laload:
  1232                     smapper.replace(this, VarType.LONG, "Array.at(@2, @1)",
  1233                          smapper.popI(), smapper.getA(0));
  1234                     break;
  1235                 case opc_faload:
  1236                     smapper.replace(this, VarType.FLOAT, "Array.at(@2, @1)",
  1237                          smapper.popI(), smapper.getA(0));
  1238                     break;
  1239                 case opc_daload:
  1240                     smapper.replace(this, VarType.DOUBLE, "Array.at(@2, @1)",
  1241                          smapper.popI(), smapper.getA(0));
  1242                     break;
  1243                 case opc_aaload:
  1244                     smapper.replace(this, VarType.REFERENCE, "Array.at(@2, @1)",
  1245                          smapper.popI(), smapper.getA(0));
  1246                     break;
  1247                 case opc_iaload:
  1248                 case opc_baload:
  1249                 case opc_caload:
  1250                 case opc_saload:
  1251                     smapper.replace(this, VarType.INTEGER, "Array.at(@2, @1)",
  1252                          smapper.popI(), smapper.getA(0));
  1253                     break;
  1254                 case opc_pop:
  1255                 case opc_pop2:
  1256                     smapper.pop(1);
  1257                     debug("/* pop */");
  1258                     break;
  1259                 case opc_dup: {
  1260                     final Variable v = smapper.get(0);
  1261                     if (smapper.isDirty()) {
  1262                         emit(smapper, this, "var @1 = @2;", smapper.pushT(v.getType()), v);
  1263                     } else {
  1264                         smapper.assign(this, v.getType(), v);
  1265                     }   
  1266                     break;
  1267                 }
  1268                 case opc_dup2: {
  1269                     final Variable vi1 = smapper.get(0);
  1270 
  1271                     if (vi1.isCategory2()) {
  1272                         emit(smapper, this, "var @1 = @2;",
  1273                              smapper.pushT(vi1.getType()), vi1);
  1274                     } else {
  1275                         final Variable vi2 = smapper.get(1);
  1276                         emit(smapper, this, "var @1 = @2, @3 = @4;",
  1277                              smapper.pushT(vi2.getType()), vi2,
  1278                              smapper.pushT(vi1.getType()), vi1);
  1279                     }
  1280                     break;
  1281                 }
  1282                 case opc_dup_x1: {
  1283                     final Variable vi1 = smapper.pop(this);
  1284                     final Variable vi2 = smapper.pop(this);
  1285                     final Variable vo3 = smapper.pushT(vi1.getType());
  1286                     final Variable vo2 = smapper.pushT(vi2.getType());
  1287                     final Variable vo1 = smapper.pushT(vi1.getType());
  1288 
  1289                     emit(smapper, this, "var @1 = @2, @3 = @4, @5 = @6;",
  1290                          vo1, vi1, vo2, vi2, vo3, vo1);
  1291                     break;
  1292                 }
  1293                 case opc_dup2_x1: {
  1294                     final Variable vi1 = smapper.pop(this);
  1295                     final Variable vi2 = smapper.pop(this);
  1296 
  1297                     if (vi1.isCategory2()) {
  1298                         final Variable vo3 = smapper.pushT(vi1.getType());
  1299                         final Variable vo2 = smapper.pushT(vi2.getType());
  1300                         final Variable vo1 = smapper.pushT(vi1.getType());
  1301 
  1302                         emit(smapper, this, "var @1 = @2, @3 = @4, @5 = @6;",
  1303                              vo1, vi1, vo2, vi2, vo3, vo1);
  1304                     } else {
  1305                         final Variable vi3 = smapper.pop(this);
  1306                         final Variable vo5 = smapper.pushT(vi2.getType());
  1307                         final Variable vo4 = smapper.pushT(vi1.getType());
  1308                         final Variable vo3 = smapper.pushT(vi3.getType());
  1309                         final Variable vo2 = smapper.pushT(vi2.getType());
  1310                         final Variable vo1 = smapper.pushT(vi1.getType());
  1311 
  1312                         emit(smapper, this, "var @1 = @2, @3 = @4, @5 = @6,",
  1313                              vo1, vi1, vo2, vi2, vo3, vi3);
  1314                         emit(smapper, this, " @1 = @2, @3 = @4;",
  1315                              vo4, vo1, vo5, vo2);
  1316                     }
  1317                     break;
  1318                 }
  1319                 case opc_dup_x2: {
  1320                     final Variable vi1 = smapper.pop(this);
  1321                     final Variable vi2 = smapper.pop(this);
  1322 
  1323                     if (vi2.isCategory2()) {
  1324                         final Variable vo3 = smapper.pushT(vi1.getType());
  1325                         final Variable vo2 = smapper.pushT(vi2.getType());
  1326                         final Variable vo1 = smapper.pushT(vi1.getType());
  1327 
  1328                         emit(smapper, this, "var @1 = @2, @3 = @4, @5 = @6;",
  1329                              vo1, vi1, vo2, vi2, vo3, vo1);
  1330                     } else {
  1331                         final Variable vi3 = smapper.pop(this);
  1332                         final Variable vo4 = smapper.pushT(vi1.getType());
  1333                         final Variable vo3 = smapper.pushT(vi3.getType());
  1334                         final Variable vo2 = smapper.pushT(vi2.getType());
  1335                         final Variable vo1 = smapper.pushT(vi1.getType());
  1336 
  1337                         emit(smapper, this, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8;",
  1338                              vo1, vi1, vo2, vi2, vo3, vi3, vo4, vo1);
  1339                     }
  1340                     break;
  1341                 }
  1342                 case opc_dup2_x2: {
  1343                     final Variable vi1 = smapper.pop(this);
  1344                     final Variable vi2 = smapper.pop(this);
  1345 
  1346                     if (vi1.isCategory2()) {
  1347                         if (vi2.isCategory2()) {
  1348                             final Variable vo3 = smapper.pushT(vi1.getType());
  1349                             final Variable vo2 = smapper.pushT(vi2.getType());
  1350                             final Variable vo1 = smapper.pushT(vi1.getType());
  1351 
  1352                             emit(smapper, this, "var @1 = @2, @3 = @4, @5 = @6;",
  1353                                  vo1, vi1, vo2, vi2, vo3, vo1);
  1354                         } else {
  1355                             final Variable vi3 = smapper.pop(this);
  1356                             final Variable vo4 = smapper.pushT(vi1.getType());
  1357                             final Variable vo3 = smapper.pushT(vi3.getType());
  1358                             final Variable vo2 = smapper.pushT(vi2.getType());
  1359                             final Variable vo1 = smapper.pushT(vi1.getType());
  1360 
  1361                             emit(smapper, this, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8;",
  1362                                  vo1, vi1, vo2, vi2, vo3, vi3, vo4, vo1);
  1363                         }
  1364                     } else {
  1365                         final Variable vi3 = smapper.pop(this);
  1366 
  1367                         if (vi3.isCategory2()) {
  1368                             final Variable vo5 = smapper.pushT(vi2.getType());
  1369                             final Variable vo4 = smapper.pushT(vi1.getType());
  1370                             final Variable vo3 = smapper.pushT(vi3.getType());
  1371                             final Variable vo2 = smapper.pushT(vi2.getType());
  1372                             final Variable vo1 = smapper.pushT(vi1.getType());
  1373 
  1374                             emit(smapper, this, "var @1 = @2, @3 = @4, @5 = @6,",
  1375                                  vo1, vi1, vo2, vi2, vo3, vi3);
  1376                             emit(smapper, this, " @1 = @2, @3 = @4;",
  1377                                  vo4, vo1, vo5, vo2);
  1378                         } else {
  1379                             final Variable vi4 = smapper.pop(this);
  1380                             final Variable vo6 = smapper.pushT(vi2.getType());
  1381                             final Variable vo5 = smapper.pushT(vi1.getType());
  1382                             final Variable vo4 = smapper.pushT(vi4.getType());
  1383                             final Variable vo3 = smapper.pushT(vi3.getType());
  1384                             final Variable vo2 = smapper.pushT(vi2.getType());
  1385                             final Variable vo1 = smapper.pushT(vi1.getType());
  1386                             
  1387                             emit(smapper, this, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8,",
  1388                                  vo1, vi1, vo2, vi2, vo3, vi3, vo4, vi4);
  1389                             emit(smapper, this, " @1 = @2, @3 = @4;",
  1390                                  vo5, vo1, vo6, vo2);
  1391                         }
  1392                     }
  1393                     break;
  1394                 }
  1395                 case opc_swap: {
  1396                     final Variable vi1 = smapper.get(0);
  1397                     final Variable vi2 = smapper.get(1);
  1398 
  1399                     if (vi1.getType() == vi2.getType()) {
  1400                         final Variable tmp = smapper.pushT(vi1.getType());
  1401 
  1402                         emit(smapper, this, "var @1 = @2, @2 = @3, @3 = @1;",
  1403                              tmp, vi1, vi2);
  1404                         smapper.pop(1);
  1405                     } else {
  1406                         smapper.pop(2);
  1407                         smapper.pushT(vi1.getType());
  1408                         smapper.pushT(vi2.getType());
  1409                     }
  1410                     break;
  1411                 }
  1412                 case opc_bipush:
  1413                     smapper.assign(this, VarType.INTEGER, 
  1414                         "(" + Integer.toString(byteCodes[++i]) + ")");
  1415                     break;
  1416                 case opc_sipush:
  1417                     smapper.assign(this, VarType.INTEGER, 
  1418                         "(" + Integer.toString(readShortArg(byteCodes, i)) + ")"
  1419                     );
  1420                     i += 2;
  1421                     break;
  1422                 case opc_getfield: {
  1423                     int indx = readUShortArg(byteCodes, i);
  1424                     String[] fi = jc.getFieldInfoName(indx);
  1425                     final int type = VarType.fromFieldType(fi[2].charAt(0));
  1426                     FieldData field = findField(fi);
  1427                     if (field == null) {
  1428                         final String mangleClass = mangleClassName(fi[0]);
  1429                         final String mangleClassAccess = accessClassFalse(mangleClass);
  1430                         smapper.replace(this, type, "@2.call(@1)",
  1431                              smapper.getA(0),
  1432                              accessField(mangleClassAccess, null, fi)
  1433                         );
  1434                     } else {
  1435                         final String fieldOwner = mangleClassName(field.cls.getClassName());
  1436                         smapper.replace(this, type, "@1.@2",
  1437                              smapper.getA(0),
  1438                              accessField(fieldOwner, field, fi)
  1439                         );
  1440                     }
  1441                     i += 2;
  1442                     addReference(fi[0]);
  1443                     break;
  1444                 }
  1445                 case opc_putfield: {
  1446                     int indx = readUShortArg(byteCodes, i);
  1447                     String[] fi = jc.getFieldInfoName(indx);
  1448                     final int type = VarType.fromFieldType(fi[2].charAt(0));
  1449                     FieldData field = findField(fi);
  1450                     if (field == null) {
  1451                         final String mangleClass = mangleClassName(fi[0]);
  1452                         final String mangleClassAccess = accessClassFalse(mangleClass);
  1453                         emit(smapper, this, "@3.call(@2, @1);",
  1454                              smapper.popT(type),
  1455                              smapper.popA(),
  1456                              accessField(mangleClassAccess, null, fi)
  1457                         );
  1458                     } else {
  1459                         final String fieldOwner = mangleClassName(field.cls.getClassName());
  1460                         emit(smapper, this, "@2.@3 = @1;",
  1461                              smapper.popT(type),
  1462                              smapper.popA(),
  1463                              accessField(fieldOwner, field, fi)
  1464                         );
  1465                     }
  1466                     i += 2;
  1467                     addReference(fi[0]);
  1468                     break;
  1469                 }
  1470                 case opc_getstatic: {
  1471                     int indx = readUShortArg(byteCodes, i);
  1472                     String[] fi = jc.getFieldInfoName(indx);
  1473                     final int type = VarType.fromFieldType(fi[2].charAt(0));
  1474                     String ac = accessClassFalse(mangleClassName(fi[0]));
  1475                     String af = accessField(ac, null, fi);
  1476                     smapper.assign(this, type, af + "()");
  1477                     i += 2;
  1478                     addReference(fi[0]);
  1479                     break;
  1480                 }
  1481                 case opc_putstatic: {
  1482                     int indx = readUShortArg(byteCodes, i);
  1483                     String[] fi = jc.getFieldInfoName(indx);
  1484                     final int type = VarType.fromFieldType(fi[2].charAt(0));
  1485                     emit(smapper, this, "@1._@2(@3);",
  1486                          accessClassFalse(mangleClassName(fi[0])), fi[1],
  1487                          smapper.popT(type));
  1488                     i += 2;
  1489                     addReference(fi[0]);
  1490                     break;
  1491                 }
  1492                 case opc_checkcast: {
  1493                     int indx = readUShortArg(byteCodes, i);
  1494                     generateCheckcast(indx, smapper);
  1495                     i += 2;
  1496                     break;
  1497                 }
  1498                 case opc_instanceof: {
  1499                     int indx = readUShortArg(byteCodes, i);
  1500                     generateInstanceOf(indx, smapper);
  1501                     i += 2;
  1502                     break;
  1503                 }
  1504                 case opc_athrow: {
  1505                     final CharSequence v = smapper.popA();
  1506                     smapper.clear();
  1507 
  1508                     emit(smapper, this, "{ var @1 = @2; throw @2; }",
  1509                          smapper.pushA(), v);
  1510                     break;
  1511                 }
  1512 
  1513                 case opc_monitorenter: {
  1514                     debug("/* monitor enter */");
  1515                     smapper.popA();
  1516                     break;
  1517                 }
  1518 
  1519                 case opc_monitorexit: {
  1520                     debug("/* monitor exit */");
  1521                     smapper.popA();
  1522                     break;
  1523                 }
  1524 
  1525                 case opc_wide:
  1526                     wide = true;
  1527                     break;
  1528 
  1529                 default: {
  1530                     wide = false;
  1531                     emit(smapper, this, "throw 'unknown bytecode @1';",
  1532                          Integer.toString(c));
  1533                 }
  1534             }
  1535             if (debug(" //")) {
  1536                 generateByteCodeComment(prev, i, byteCodes);
  1537             }
  1538             if (outChanged) {
  1539                 append("\n");
  1540             }
  1541         }
  1542         if (previousTrap != null) {
  1543             generateCatch(previousTrap, byteCodes.length, topMostLabel);
  1544         }
  1545         if (didBranches) {
  1546             append("\n    }\n");
  1547         }
  1548         while (openBraces-- > 0) {
  1549             append('}');
  1550         }
  1551         if (defineProp) {
  1552             append("\n}});");
  1553         } else {
  1554             append("\n};");
  1555         }
  1556         return defineProp;
  1557     }
  1558 
  1559     private int generateIf(StackMapper mapper, byte[] byteCodes, 
  1560         int i, final CharSequence v2, final CharSequence v1, 
  1561         final String test, int topMostLabel
  1562     ) throws IOException {
  1563         mapper.flush(this);
  1564         int indx = i + readShortArg(byteCodes, i);
  1565         append("if ((").append(v1)
  1566            .append(") ").append(test).append(" (")
  1567            .append(v2).append(")) ");
  1568         goTo(this, i, indx, topMostLabel);
  1569         return i + 2;
  1570     }
  1571     
  1572     private int readInt4(byte[] byteCodes, int offset) {
  1573         final int d = byteCodes[offset + 0] << 24;
  1574         final int c = byteCodes[offset + 1] << 16;
  1575         final int b = byteCodes[offset + 2] << 8;
  1576         final int a = byteCodes[offset + 3];
  1577         return (d & 0xff000000) | (c & 0xff0000) | (b & 0xff00) | (a & 0xff);
  1578     }
  1579     private static int readUByte(byte[] byteCodes, int offset) {
  1580         return byteCodes[offset] & 0xff;
  1581     }
  1582 
  1583     private static int readUShort(byte[] byteCodes, int offset) {
  1584         return ((byteCodes[offset] & 0xff) << 8)
  1585                     | (byteCodes[offset + 1] & 0xff);
  1586     }
  1587     private static int readUShortArg(byte[] byteCodes, int offsetInstruction) {
  1588         return readUShort(byteCodes, offsetInstruction + 1);
  1589     }
  1590 
  1591     private static int readShort(byte[] byteCodes, int offset) {
  1592         int signed = byteCodes[offset];
  1593         byte b0 = (byte)signed;
  1594         return (b0 << 8) | (byteCodes[offset + 1] & 0xff);
  1595     }
  1596     private static int readShortArg(byte[] byteCodes, int offsetInstruction) {
  1597         return readShort(byteCodes, offsetInstruction + 1);
  1598     }
  1599 
  1600     private static void countArgs(String descriptor, char[] returnType, StringBuilder sig, StringBuilder cnt) {
  1601         int i = 0;
  1602         Boolean count = null;
  1603         boolean array = false;
  1604         sig.append("__");
  1605         int firstPos = sig.length();
  1606         while (i < descriptor.length()) {
  1607             char ch = descriptor.charAt(i++);
  1608             switch (ch) {
  1609                 case '(':
  1610                     count = true;
  1611                     continue;
  1612                 case ')':
  1613                     count = false;
  1614                     continue;
  1615                 case 'B': 
  1616                 case 'C': 
  1617                 case 'D': 
  1618                 case 'F': 
  1619                 case 'I': 
  1620                 case 'J': 
  1621                 case 'S': 
  1622                 case 'Z': 
  1623                     if (count) {
  1624                         if (array) {
  1625                             sig.append("_3");
  1626                         }
  1627                         sig.append(ch);
  1628                         if (ch == 'J' || ch == 'D') {
  1629                             cnt.append('1');
  1630                         } else {
  1631                             cnt.append('0');
  1632                         }
  1633                     } else {
  1634                         sig.insert(firstPos, ch);
  1635                         if (array) {
  1636                             returnType[0] = '[';
  1637                             sig.insert(firstPos, "_3");
  1638                         } else {
  1639                             returnType[0] = ch;
  1640                         }
  1641                     }
  1642                     array = false;
  1643                     continue;
  1644                 case 'V': 
  1645                     assert !count;
  1646                     returnType[0] = 'V';
  1647                     sig.insert(firstPos, 'V');
  1648                     continue;
  1649                 case 'L':
  1650                     int next = descriptor.indexOf(';', i);
  1651                     String realSig = mangleSig(descriptor, i - 1, next + 1);
  1652                     if (count) {
  1653                         if (array) {
  1654                             sig.append("_3");
  1655                         }
  1656                         sig.append(realSig);
  1657                         cnt.append('0');
  1658                     } else {
  1659                         sig.insert(firstPos, realSig);
  1660                         if (array) {
  1661                             sig.insert(firstPos, "_3");
  1662                         }
  1663                         returnType[0] = 'L';
  1664                     }
  1665                     i = next + 1;
  1666                     array = false;
  1667                     continue;
  1668                 case '[':
  1669                     array = true;
  1670                     continue;
  1671                 default:
  1672                     throw new IllegalStateException("Invalid char: " + ch);
  1673             }
  1674         }
  1675     }
  1676     
  1677     static String mangleSig(String sig) {
  1678         return mangleSig(sig, 0, sig.length());
  1679     }
  1680     
  1681     private static String mangleMethodName(String name) {
  1682         StringBuilder sb = new StringBuilder(name.length() * 2);
  1683         int last = name.length();
  1684         for (int i = 0; i < last; i++) {
  1685             final char ch = name.charAt(i);
  1686             switch (ch) {
  1687                 case '_': sb.append("_1"); break;
  1688                 default: sb.append(ch); break;
  1689             }
  1690         }
  1691         return sb.toString();
  1692     }
  1693     private static String mangleSig(String txt, int first, int last) {
  1694         StringBuilder sb = new StringBuilder((last - first) * 2);
  1695         for (int i = first; i < last; i++) {
  1696             final char ch = txt.charAt(i);
  1697             switch (ch) {
  1698                 case '/': sb.append('_'); break;
  1699                 case '_': sb.append("_1"); break;
  1700                 case ';': sb.append("_2"); break;
  1701                 case '[': sb.append("_3"); break;
  1702                 default: 
  1703                     if (Character.isJavaIdentifierPart(ch)) {
  1704                         sb.append(ch);
  1705                     } else {
  1706                         sb.append("_0");
  1707                         String hex = Integer.toHexString(ch).toLowerCase();
  1708                         for (int m = hex.length(); m < 4; m++) {
  1709                             sb.append("0");
  1710                         }
  1711                         sb.append(hex);
  1712                     }
  1713                 break;
  1714             }
  1715         }
  1716         return sb.toString();
  1717     }
  1718     
  1719     private static String mangleClassName(String name) {
  1720         return mangleSig(name);
  1721     }
  1722 
  1723     private static String findMethodName(MethodData m, StringBuilder cnt) {
  1724         StringBuilder name = new StringBuilder();
  1725         if ("<init>".equals(m.getName())) { // NOI18N
  1726             name.append("cons"); // NOI18N
  1727         } else if ("<clinit>".equals(m.getName())) { // NOI18N
  1728             name.append("class"); // NOI18N
  1729         } else {
  1730             name.append(mangleMethodName(m.getName()));
  1731         } 
  1732         
  1733         countArgs(m.getInternalSig(), new char[1], name, cnt);
  1734         return name.toString();
  1735     }
  1736 
  1737     static String findMethodName(String[] mi, StringBuilder cnt, char[] returnType) {
  1738         StringBuilder name = new StringBuilder();
  1739         String descr = mi[2];//mi.getDescriptor();
  1740         String nm= mi[1];
  1741         if ("<init>".equals(nm)) { // NOI18N
  1742             name.append("cons"); // NOI18N
  1743         } else {
  1744             name.append(mangleMethodName(nm));
  1745         }
  1746         countArgs(descr, returnType, name, cnt);
  1747         return name.toString();
  1748     }
  1749 
  1750     private int invokeStaticMethod(byte[] byteCodes, int i, final StackMapper mapper, boolean isStatic)
  1751     throws IOException {
  1752         int methodIndex = readUShortArg(byteCodes, i);
  1753         String[] mi = jc.getFieldInfoName(methodIndex);
  1754         char[] returnType = { 'V' };
  1755         StringBuilder cnt = new StringBuilder();
  1756         String mn = findMethodName(mi, cnt, returnType);
  1757         
  1758         final int numArguments = isStatic ? cnt.length() : cnt.length() + 1;
  1759         final CharSequence[] vars = new CharSequence[numArguments];
  1760 
  1761         for (int j = numArguments - 1; j >= 0; --j) {
  1762             vars[j] = mapper.popValue();
  1763         }
  1764 
  1765         if ((
  1766             "newUpdater__Ljava_util_concurrent_atomic_AtomicIntegerFieldUpdater_2Ljava_lang_Class_2Ljava_lang_String_2".equals(mn)
  1767             && "java/util/concurrent/atomic/AtomicIntegerFieldUpdater".equals(mi[0])
  1768         ) || (
  1769             "newUpdater__Ljava_util_concurrent_atomic_AtomicLongFieldUpdater_2Ljava_lang_Class_2Ljava_lang_String_2".equals(mn)
  1770             && "java/util/concurrent/atomic/AtomicLongFieldUpdater".equals(mi[0])
  1771         )) {
  1772             if (vars[1] instanceof String) {
  1773                 String field = vars[1].toString();
  1774                 if (field.length() > 2 && field.charAt(0) == '"' && field.charAt(field.length() - 1) == '"') {
  1775                     vars[1] = "c._" + field.substring(1, field.length() - 1);
  1776                 }
  1777             }
  1778         }
  1779         if (
  1780             "newUpdater__Ljava_util_concurrent_atomic_AtomicReferenceFieldUpdater_2Ljava_lang_Class_2Ljava_lang_Class_2Ljava_lang_String_2".equals(mn)
  1781             && "java/util/concurrent/atomic/AtomicReferenceFieldUpdater".equals(mi[0])
  1782         ) {
  1783             if (vars[1] instanceof String) {
  1784                 String field = vars[2].toString();
  1785                 if (field.length() > 2 && field.charAt(0) == '"' && field.charAt(field.length() - 1) == '"') {
  1786                     vars[2] = "c._" + field.substring(1, field.length() - 1);
  1787                 }
  1788             }
  1789         }
  1790 
  1791         if (returnType[0] != 'V') {
  1792             mapper.flush(this);
  1793             append("var ")
  1794                .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
  1795                .append(" = ");
  1796         }
  1797 
  1798         final String in = mi[0];
  1799         String mcn;
  1800         if (callbacks && (
  1801             in.equals("org/apidesign/html/boot/spi/Fn") ||
  1802             in.equals("org/netbeans/html/boot/spi/Fn")
  1803         )) {
  1804             mcn = "java_lang_Class";
  1805         } else {
  1806             mcn = mangleClassName(in);
  1807         }
  1808         String object = accessClassFalse(mcn);
  1809         if (mn.startsWith("cons_")) {
  1810             object += ".constructor";
  1811         }
  1812         append(accessStaticMethod(object, mn, mi));
  1813         if (isStatic) {
  1814             append('(');
  1815         } else {
  1816             append(".call(");
  1817         }
  1818         if (numArguments > 0) {
  1819             append(vars[0]);
  1820             for (int j = 1; j < numArguments; ++j) {
  1821                 append(", ");
  1822                 append(vars[j]);
  1823             }
  1824         }
  1825         append(");");
  1826         i += 2;
  1827         addReference(in);
  1828         return i;
  1829     }
  1830     private int invokeVirtualMethod(byte[] byteCodes, int i, final StackMapper mapper)
  1831     throws IOException {
  1832         int methodIndex = readUShortArg(byteCodes, i);
  1833         String[] mi = jc.getFieldInfoName(methodIndex);
  1834         char[] returnType = { 'V' };
  1835         StringBuilder cnt = new StringBuilder();
  1836         String mn = findMethodName(mi, cnt, returnType);
  1837 
  1838         final int numArguments = cnt.length() + 1;
  1839         final CharSequence[] vars =  new CharSequence[numArguments];
  1840 
  1841         for (int j = numArguments - 1; j >= 0; --j) {
  1842             vars[j] = mapper.popValue();
  1843         }
  1844 
  1845         if (returnType[0] != 'V') {
  1846             mapper.flush(this);
  1847             append("var ")
  1848                .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
  1849                .append(" = ");
  1850         }
  1851 
  1852         append(accessVirtualMethod(vars[0].toString(), mn, mi, numArguments));
  1853         String sep = "";
  1854         for (int j = 1; j < numArguments; ++j) {
  1855             append(sep);
  1856             append(vars[j]);
  1857             sep = ", ";
  1858         }
  1859         append(");");
  1860         i += 2;
  1861         return i;
  1862     }
  1863 
  1864     private void addReference(String cn) throws IOException {
  1865         if (requireReference(cn)) {
  1866             debug(" /* needs " + cn + " */");
  1867         }
  1868     }
  1869 
  1870     private void outType(String d, StringBuilder out) {
  1871         int arr = 0;
  1872         while (d.charAt(0) == '[') {
  1873             out.append('A');
  1874             d = d.substring(1);
  1875         }
  1876         if (d.charAt(0) == 'L') {
  1877             assert d.charAt(d.length() - 1) == ';';
  1878             out.append(mangleClassName(d).substring(0, d.length() - 1));
  1879         } else {
  1880             out.append(d);
  1881         }
  1882     }
  1883 
  1884     private String encodeConstant(int entryIndex) throws IOException {
  1885         String[] classRef = { null };
  1886         String s = jc.stringValue(entryIndex, classRef);
  1887         if (classRef[0] != null) {
  1888             if (classRef[0].startsWith("[")) {
  1889                 s = accessClass("java_lang_Class") + "(false)['forName__Ljava_lang_Class_2Ljava_lang_String_2']('" + classRef[0] + "')";
  1890             } else {
  1891                 addReference(classRef[0]);
  1892                 s = accessClassFalse(mangleClassName(s)) + ".constructor.$class";
  1893             }
  1894         }
  1895         return s;
  1896     }
  1897 
  1898     private String javaScriptBody(String destObject, MethodData m, boolean isStatic) throws IOException {
  1899         byte[] arr = m.findAnnotationData(true);
  1900         if (arr == null) {
  1901             return null;
  1902         }
  1903         final String jvmType = "Lorg/apidesign/bck2brwsr/core/JavaScriptBody;";
  1904         final String htmlType = "Lnet/java/html/js/JavaScriptBody;";
  1905         class P extends AnnotationParser {
  1906             public P() {
  1907                 super(false, true);
  1908             }
  1909             
  1910             int cnt;
  1911             String[] args = new String[30];
  1912             String body;
  1913             boolean javacall;
  1914             boolean html4j;
  1915             
  1916             @Override
  1917             protected void visitAttr(String type, String attr, String at, String value) {
  1918                 if (type.equals(jvmType)) {
  1919                     if ("body".equals(attr)) {
  1920                         body = value;
  1921                     } else if ("args".equals(attr)) {
  1922                         args[cnt++] = value;
  1923                     } else {
  1924                         throw new IllegalArgumentException(attr);
  1925                     }
  1926                 }
  1927                 if (type.equals(htmlType)) {
  1928                     html4j = true;
  1929                     if ("body".equals(attr)) {
  1930                         body = value;
  1931                     } else if ("args".equals(attr)) {
  1932                         args[cnt++] = value;
  1933                     } else if ("javacall".equals(attr)) {
  1934                         javacall = "1".equals(value);
  1935                     } else if ("wait4js".equals(attr)) {
  1936                         // ignore, we always invoke synchronously
  1937                     } else {
  1938                         throw new IllegalArgumentException(attr);
  1939                     }
  1940                 }
  1941             }
  1942         }
  1943         P p = new P();
  1944         p.parse(arr, jc);
  1945         if (p.body == null) {
  1946             return null;
  1947         }
  1948         StringBuilder cnt = new StringBuilder();
  1949         final String mn = findMethodName(m, cnt);
  1950         append("m = ").append(destObject).append(".").append(mn);
  1951         append(" = function(");
  1952         String space = "";
  1953         int index = 0;
  1954         StringBuilder toValue = new StringBuilder();
  1955         for (int i = 0; i < cnt.length(); i++) {
  1956             append(space);
  1957             space = outputArg(this, p.args, index);
  1958             if (p.html4j && space.length() > 0) {
  1959                 toValue.append("\n  ").append(p.args[index]).append(" = ")
  1960                     .append(accessClass("java_lang_Class")).append("(false).toJS(").
  1961                     append(p.args[index]).append(");");
  1962             }
  1963             index++;
  1964         }
  1965         append(") {").append("\n");
  1966         append(toValue.toString());
  1967         if (p.javacall) {
  1968             int lastSlash = jc.getClassName().lastIndexOf('/');
  1969             final String pkg = jc.getClassName().substring(0, lastSlash);
  1970             append(mangleCallbacks(pkg, p.body));
  1971             requireReference(pkg + "/$JsCallbacks$");
  1972         } else {
  1973             append(p.body);
  1974         }
  1975         append("\n}\n");
  1976         return mn;
  1977     }
  1978     
  1979     private static CharSequence mangleCallbacks(String pkgName, String body) {
  1980         StringBuilder sb = new StringBuilder();
  1981         int pos = 0;
  1982         for (;;) {
  1983             int next = body.indexOf(".@", pos);
  1984             if (next == -1) {
  1985                 sb.append(body.substring(pos));
  1986                 body = sb.toString();
  1987                 break;
  1988             }
  1989             int ident = next;
  1990             while (ident > 0) {
  1991                 if (!Character.isJavaIdentifierPart(body.charAt(--ident))) {
  1992                     ident++;
  1993                     break;
  1994                 }
  1995             }
  1996             String refId = body.substring(ident, next);
  1997 
  1998             sb.append(body.substring(pos, ident));
  1999 
  2000             int sigBeg = body.indexOf('(', next);
  2001             int sigEnd = body.indexOf(')', sigBeg);
  2002             int colon4 = body.indexOf("::", next);
  2003             if (sigBeg == -1 || sigEnd == -1 || colon4 == -1) {
  2004                 throw new IllegalStateException("Malformed body " + body);
  2005             }
  2006             String fqn = body.substring(next + 2, colon4);
  2007             String method = body.substring(colon4 + 2, sigBeg);
  2008             String params = body.substring(sigBeg, sigEnd + 1);
  2009 
  2010             int paramBeg = body.indexOf('(', sigEnd + 1);
  2011             
  2012             sb.append("vm.").append(mangleClassName(pkgName)).append("_$JsCallbacks$(false)._VM().");
  2013             sb.append(mangleJsCallbacks(fqn, method, params, false));
  2014             sb.append("(").append(refId);
  2015             if (body.charAt(paramBeg + 1) != ')') {
  2016                 sb.append(",");
  2017             }
  2018             pos = paramBeg + 1;
  2019         }
  2020         sb = null;
  2021         pos = 0;
  2022         for (;;) {
  2023             int next = body.indexOf("@", pos);
  2024             if (next == -1) {
  2025                 if (sb == null) {
  2026                     return body;
  2027                 }
  2028                 sb.append(body.substring(pos));
  2029                 return sb;
  2030             }
  2031             if (sb == null) {
  2032                 sb = new StringBuilder();
  2033             }
  2034 
  2035             sb.append(body.substring(pos, next));
  2036 
  2037             int sigBeg = body.indexOf('(', next);
  2038             int sigEnd = body.indexOf(')', sigBeg);
  2039             int colon4 = body.indexOf("::", next);
  2040             if (sigBeg == -1 || sigEnd == -1 || colon4 == -1) {
  2041                 throw new IllegalStateException("Malformed body " + body);
  2042             }
  2043             String fqn = body.substring(next + 1, colon4);
  2044             String method = body.substring(colon4 + 2, sigBeg);
  2045             String params = body.substring(sigBeg, sigEnd + 1);
  2046 
  2047             int paramBeg = body.indexOf('(', sigEnd + 1);
  2048             
  2049             sb.append("vm.").append(mangleClassName(pkgName)).append("_$JsCallbacks$(false)._VM().");
  2050             sb.append(mangleJsCallbacks(fqn, method, params, true));
  2051             sb.append("(");
  2052             pos = paramBeg + 1;
  2053         }
  2054     }
  2055 
  2056     static String mangleJsCallbacks(String fqn, String method, String params, boolean isStatic) {
  2057         if (params.startsWith("(")) {
  2058             params = params.substring(1);
  2059         }
  2060         if (params.endsWith(")")) {
  2061             params = params.substring(0, params.length() - 1);
  2062         }
  2063         StringBuilder sb = new StringBuilder();
  2064         final String fqnu = fqn.replace('.', '_');
  2065         final String rfqn = mangleClassName(fqnu);
  2066         final String rm = mangleMethodName(method);
  2067         final String srp;
  2068         {
  2069             StringBuilder pb = new StringBuilder();
  2070             int len = params.length();
  2071             int indx = 0;
  2072             while (indx < len) {
  2073                 char ch = params.charAt(indx);
  2074                 if (ch == '[' || ch == 'L') {
  2075                     int column = params.indexOf(';', indx) + 1;
  2076                     if (column > indx) {
  2077                         String real = params.substring(indx, column);
  2078                         if ("Ljava/lang/String;".equals(real)) {
  2079                             pb.append("Ljava/lang/String;");
  2080                             indx = column;
  2081                             continue;
  2082                         }
  2083                     }
  2084                     pb.append("Ljava/lang/Object;");
  2085                     indx = column;
  2086                 } else {
  2087                     pb.append(ch);
  2088                     indx++;
  2089                 }
  2090             }
  2091             srp = mangleSig(pb.toString());
  2092         }
  2093         final String rp = mangleSig(params);
  2094         final String mrp = mangleMethodName(rp);
  2095         sb.append(rfqn).append("$").append(rm).
  2096             append('$').append(mrp).append("__Ljava_lang_Object_2");
  2097         if (!isStatic) {
  2098             sb.append('L').append(fqnu).append("_2");
  2099         }
  2100         sb.append(srp);
  2101         return sb.toString();
  2102     }
  2103 
  2104     private static String className(ClassData jc) {
  2105         //return jc.getName().getInternalName().replace('/', '_');
  2106         return mangleClassName(jc.getClassName());
  2107     }
  2108     
  2109     private static String[] findAnnotation(
  2110         byte[] arr, ClassData cd, final String className, 
  2111         final String... attrNames
  2112     ) throws IOException {
  2113         if (arr == null) {
  2114             return null;
  2115         }
  2116         final String[] values = new String[attrNames.length];
  2117         final boolean[] found = { false };
  2118         final String jvmType = "L" + className.replace('.', '/') + ";";
  2119         AnnotationParser ap = new AnnotationParser(false, true) {
  2120             @Override
  2121             protected void visitAttr(String type, String attr, String at, String value) {
  2122                 if (type.equals(jvmType)) {
  2123                     found[0] = true;
  2124                     for (int i = 0; i < attrNames.length; i++) {
  2125                         if (attrNames[i].equals(attr)) {
  2126                             values[i] = value;
  2127                         }
  2128                     }
  2129                 }
  2130             }
  2131             
  2132         };
  2133         ap.parse(arr, cd);
  2134         return found[0] ? values : null;
  2135     }
  2136 
  2137     private CharSequence initField(FieldData v) {
  2138         final String is = v.getInternalSig();
  2139         if (is.length() == 1) {
  2140             switch (is.charAt(0)) {
  2141                 case 'S':
  2142                 case 'J':
  2143                 case 'B':
  2144                 case 'Z':
  2145                 case 'C':
  2146                 case 'I': return " = 0;";
  2147                 case 'F': 
  2148                 case 'D': return " = 0.0;";
  2149                 default:
  2150                     throw new IllegalStateException(is);
  2151             }
  2152         }
  2153         return " = null;";
  2154     }
  2155 
  2156     private void generateAnno(ClassData cd, byte[] data) throws IOException {
  2157         AnnotationParser ap = new AnnotationParser(true, false) {
  2158             int[] cnt = new int[32];
  2159             int depth;
  2160             
  2161             @Override
  2162             protected void visitAnnotationStart(String attrType, boolean top) throws IOException {
  2163                 final String slashType = attrType.substring(1, attrType.length() - 1);
  2164                 requireReference(slashType);
  2165                 
  2166                 if (cnt[depth]++ > 0) {
  2167                     append(",");
  2168                 }
  2169                 if (top) {
  2170                     append('"').append(attrType).append("\" : ");
  2171                 }
  2172                 append("{\n");
  2173                 cnt[++depth] = 0;
  2174             }
  2175 
  2176             @Override
  2177             protected void visitAnnotationEnd(String type, boolean top) throws IOException {
  2178                 append("\n}\n");
  2179                 depth--;
  2180             }
  2181 
  2182             @Override
  2183             protected void visitValueStart(String attrName, char type) throws IOException {
  2184                 if (cnt[depth]++ > 0) {
  2185                     append(",\n");
  2186                 }
  2187                 cnt[++depth] = 0;
  2188                 if (attrName != null) {
  2189                     append('"').append(attrName).append("\" : ");
  2190                 }
  2191                 if (type == '[') {
  2192                     append("[");
  2193                 }
  2194             }
  2195 
  2196             @Override
  2197             protected void visitValueEnd(String attrName, char type) throws IOException {
  2198                 if (type == '[') {
  2199                     append("]");
  2200                 }
  2201                 depth--;
  2202             }
  2203             
  2204             @Override
  2205             protected void visitAttr(String type, String attr, String attrType, String value) 
  2206             throws IOException {
  2207                 if (attr == null && value == null) {
  2208                     return;
  2209                 }
  2210                 append(value);
  2211             }
  2212 
  2213             @Override
  2214             protected void visitEnumAttr(String type, String attr, String attrType, String value) 
  2215             throws IOException {
  2216                 final String slashType = attrType.substring(1, attrType.length() - 1);
  2217                 requireReference(slashType);
  2218                 
  2219                 final String cn = mangleClassName(slashType);
  2220                 append(accessClassFalse(cn))
  2221                    .append("['valueOf__L").
  2222                     append(cn).
  2223                     append("_2Ljava_lang_String_2']('").
  2224                     append(value).
  2225                     append("')");
  2226             }
  2227         };
  2228         ap.parse(data, cd);
  2229     }
  2230 
  2231     private static String outputArg(Appendable out, String[] args, int indx) throws IOException {
  2232         final String name = args[indx];
  2233         if (name == null) {
  2234             return "";
  2235         }
  2236         if (name.contains(",")) {
  2237             throw new IOException("Wrong parameter with ',': " + name);
  2238         }
  2239         out.append(name);
  2240         return ",";
  2241     }
  2242 
  2243     final void emitNoFlush(
  2244         StackMapper sm, 
  2245         final String format, final CharSequence... params
  2246     ) throws IOException {
  2247         emitImpl(this, format, params);
  2248     }
  2249     static final void emit(
  2250         StackMapper sm, 
  2251         final Appendable out, 
  2252         final String format, final CharSequence... params
  2253     ) throws IOException {
  2254         sm.flush(out);
  2255         emitImpl(out, format, params);
  2256     }
  2257     static void emitImpl(final Appendable out,
  2258                              final String format,
  2259                              final CharSequence... params) throws IOException {
  2260         final int length = format.length();
  2261 
  2262         int processed = 0;
  2263         int paramOffset = format.indexOf('@');
  2264         while ((paramOffset != -1) && (paramOffset < (length - 1))) {
  2265             final char paramChar = format.charAt(paramOffset + 1);
  2266             if ((paramChar >= '1') && (paramChar <= '9')) {
  2267                 final int paramIndex = paramChar - '0' - 1;
  2268 
  2269                 out.append(format, processed, paramOffset);
  2270                 out.append(params[paramIndex]);
  2271 
  2272                 ++paramOffset;
  2273                 processed = paramOffset + 1;
  2274             }
  2275 
  2276             paramOffset = format.indexOf('@', paramOffset + 1);
  2277         }
  2278 
  2279         out.append(format, processed, length);
  2280     }
  2281 
  2282     private void generateCatch(TrapData[] traps, int current, int topMostLabel) throws IOException {
  2283         append("} catch (e) {\n");
  2284         int finallyPC = -1;
  2285         for (TrapData e : traps) {
  2286             if (e == null) {
  2287                 break;
  2288             }
  2289             if (e.catch_cpx != 0) { //not finally
  2290                 final String classInternalName = jc.getClassName(e.catch_cpx);
  2291                 addReference(classInternalName);
  2292                 append("e = vm.java_lang_Class(false).bck2BrwsrThrwrbl(e);");
  2293                 append("if (e['$instOf_" + mangleClassName(classInternalName) + "']) {");
  2294                 append("var stA0 = e;");
  2295                 goTo(this, current, e.handler_pc, topMostLabel);
  2296                 append("}\n");
  2297             } else {
  2298                 finallyPC = e.handler_pc;
  2299             }
  2300         }
  2301         if (finallyPC == -1) {
  2302             append("throw e;");
  2303         } else {
  2304             append("var stA0 = e;");
  2305             goTo(this, current, finallyPC, topMostLabel);
  2306         }
  2307         append("\n}");
  2308     }
  2309 
  2310     private static void goTo(Appendable out, int current, int to, int canBack) throws IOException {
  2311         if (to < current) {
  2312             if (canBack < to) {
  2313                 out.append("{ gt = 0; continue X_" + to + "; }");
  2314             } else {
  2315                 out.append("{ gt = " + to + "; continue X_0; }");
  2316             }
  2317         } else {
  2318             out.append("{ gt = " + to + "; break IF; }");
  2319         }
  2320     }
  2321 
  2322     private static void emitIf(
  2323         StackMapper sm, 
  2324         Appendable out, String pattern, 
  2325         CharSequence param, 
  2326         int current, int to, int canBack
  2327     ) throws IOException {
  2328         sm.flush(out);
  2329         emitImpl(out, pattern, param);
  2330         goTo(out, current, to, canBack);
  2331     }
  2332 
  2333     private void generateNewArray(int atype, final StackMapper smapper) throws IOException, IllegalStateException {
  2334         String jvmType;
  2335         switch (atype) {
  2336             case 4: jvmType = "[Z"; break;
  2337             case 5: jvmType = "[C"; break;
  2338             case 6: jvmType = "[F"; break;
  2339             case 7: jvmType = "[D"; break;
  2340             case 8: jvmType = "[B"; break;
  2341             case 9: jvmType = "[S"; break;
  2342             case 10: jvmType = "[I"; break;
  2343             case 11: jvmType = "[J"; break;
  2344             default: throw new IllegalStateException("Array type: " + atype);
  2345         }
  2346         emit(smapper, this, 
  2347             "var @2 = Array.prototype['newArray__Ljava_lang_Object_2ZLjava_lang_String_2Ljava_lang_Object_2I'](true, '@3', null, @1);",
  2348              smapper.popI(), smapper.pushA(), jvmType);
  2349     }
  2350 
  2351     private void generateANewArray(int type, final StackMapper smapper) throws IOException {
  2352         String typeName = jc.getClassName(type);
  2353         String ref = "null";
  2354         if (typeName.startsWith("[")) {
  2355             typeName = "'[" + typeName + "'";
  2356         } else {
  2357             ref = "vm." + mangleClassName(typeName);
  2358             typeName = "'[L" + typeName + ";'";
  2359         }
  2360         emit(smapper, this,
  2361             "var @2 = Array.prototype['newArray__Ljava_lang_Object_2ZLjava_lang_String_2Ljava_lang_Object_2I'](false, @3, @4, @1);",
  2362              smapper.popI(), smapper.pushA(), typeName, ref);
  2363     }
  2364 
  2365     private int generateMultiANewArray(int type, final byte[] byteCodes, int i, final StackMapper smapper) throws IOException {
  2366         String typeName = jc.getClassName(type);
  2367         int dim = readUByte(byteCodes, ++i);
  2368         StringBuilder dims = new StringBuilder();
  2369         dims.append('[');
  2370         for (int d = 0; d < dim; d++) {
  2371             if (d != 0) {
  2372                 dims.insert(1, ",");
  2373             }
  2374             dims.insert(1, smapper.popI());
  2375         }
  2376         dims.append(']');
  2377         String fn = "null";
  2378         if (typeName.charAt(dim) == 'L') {
  2379             fn = "vm." + mangleClassName(typeName.substring(dim + 1, typeName.length() - 1));
  2380         }
  2381         emit(smapper, this, 
  2382             "var @2 = Array.prototype['multiNewArray__Ljava_lang_Object_2Ljava_lang_String_2_3ILjava_lang_Object_2']('@3', @1, @4);",
  2383              dims.toString(), smapper.pushA(), typeName, fn
  2384         );
  2385         return i;
  2386     }
  2387 
  2388     private int generateTableSwitch(int i, final byte[] byteCodes, final StackMapper smapper, int topMostLabel) throws IOException {
  2389         int table = i / 4 * 4 + 4;
  2390         int dflt = i + readInt4(byteCodes, table);
  2391         table += 4;
  2392         int low = readInt4(byteCodes, table);
  2393         table += 4;
  2394         int high = readInt4(byteCodes, table);
  2395         table += 4;
  2396         final CharSequence swVar = smapper.popValue();
  2397         smapper.flush(this);
  2398         append("switch (").append(swVar).append(") {\n");
  2399         while (low <= high) {
  2400             int offset = i + readInt4(byteCodes, table);
  2401             table += 4;
  2402             append("  case " + low).append(":"); goTo(this, i, offset, topMostLabel); append('\n');
  2403             low++;
  2404         }
  2405         append("  default: ");
  2406         goTo(this, i, dflt, topMostLabel);
  2407         append("\n}");
  2408         i = table - 1;
  2409         return i;
  2410     }
  2411 
  2412     private int generateLookupSwitch(int i, final byte[] byteCodes, final StackMapper smapper, int topMostLabel) throws IOException {
  2413         int table = i / 4 * 4 + 4;
  2414         int dflt = i + readInt4(byteCodes, table);
  2415         table += 4;
  2416         int n = readInt4(byteCodes, table);
  2417         table += 4;
  2418         final CharSequence swVar = smapper.popValue();
  2419         smapper.flush(this);
  2420         append("switch (").append(swVar).append(") {\n");
  2421         while (n-- > 0) {
  2422             int cnstnt = readInt4(byteCodes, table);
  2423             table += 4;
  2424             int offset = i + readInt4(byteCodes, table);
  2425             table += 4;
  2426             append("  case " + cnstnt).append(": "); goTo(this, i, offset, topMostLabel); append('\n');
  2427         }
  2428         append("  default: ");
  2429         goTo(this, i, dflt, topMostLabel);
  2430         append("\n}");
  2431         i = table - 1;
  2432         return i;
  2433     }
  2434 
  2435     private void generateInstanceOf(int indx, final StackMapper smapper) throws IOException {
  2436         String type = jc.getClassName(indx);
  2437         if (!type.startsWith("[")) {
  2438             emit(smapper, this, 
  2439                     "var @2 = @1 != null && @1['$instOf_@3'] ? 1 : 0;",
  2440                  smapper.popA(), smapper.pushI(),
  2441                  mangleClassName(type));
  2442         } else {
  2443             int cnt = 0;
  2444             while (type.charAt(cnt) == '[') {
  2445                 cnt++;
  2446             }
  2447             if (type.charAt(cnt) == 'L') {
  2448                 String component = type.substring(cnt + 1, type.length() - 1);
  2449                 requireReference(component);
  2450                 type = "vm." + mangleClassName(component);
  2451                 emit(smapper, this, 
  2452                     "var @2 = Array.prototype['isInstance__ZLjava_lang_Object_2ILjava_lang_Object_2'](@1, @4, @3);",
  2453                     smapper.popA(), smapper.pushI(),
  2454                     type, "" + cnt
  2455                 );
  2456             } else {
  2457                 emit(smapper, this, 
  2458                     "var @2 = Array.prototype['isInstance__ZLjava_lang_Object_2Ljava_lang_String_2'](@1, '@3');",
  2459                     smapper.popA(), smapper.pushI(), type
  2460                 );
  2461             }
  2462         }
  2463     }
  2464 
  2465     private void generateCheckcast(int indx, final StackMapper smapper) throws IOException {
  2466         String type = jc.getClassName(indx);
  2467         if (!type.startsWith("[")) {
  2468             emitNoFlush(smapper, 
  2469                  "if (@1 !== null && !@1['$instOf_@2']) vm.java_lang_Class(false).castEx();",
  2470                  smapper.getT(0, VarType.REFERENCE, false), mangleClassName(type));
  2471         } else {
  2472             int cnt = 0;
  2473             while (type.charAt(cnt) == '[') {
  2474                 cnt++;
  2475             }
  2476             if (type.charAt(cnt) == 'L') {
  2477                 String component = type.substring(cnt + 1, type.length() - 1);
  2478                 requireReference(component);
  2479                 type = "vm." + mangleClassName(component);
  2480                 emitNoFlush(smapper, 
  2481                     "if (@1 !== null && !Array.prototype['isInstance__ZLjava_lang_Object_2ILjava_lang_Object_2'](@1, @3, @2)) vm.java_lang_Class(false).castEx();",
  2482                      smapper.getT(0, VarType.REFERENCE, false), type, "" + cnt
  2483                 );
  2484             } else {
  2485                 emitNoFlush(smapper, 
  2486                     "if (@1 !== null && !Array.prototype['isInstance__ZLjava_lang_Object_2Ljava_lang_String_2'](@1, '@2')) vm.java_lang_Class(false).castEx();",
  2487                      smapper.getT(0, VarType.REFERENCE, false), type
  2488                 );
  2489             }
  2490         }
  2491     }
  2492 
  2493     private void generateByteCodeComment(int prev, int i, final byte[] byteCodes) throws IOException {
  2494         for (int j = prev; j <= i; j++) {
  2495             append(" ");
  2496             final int cc = readUByte(byteCodes, j);
  2497             append(Integer.toString(cc));
  2498         }
  2499     }
  2500     
  2501     @JavaScriptBody(args = "msg", body = "")
  2502     private static void println(String msg) {
  2503         System.err.println(msg);
  2504     }
  2505 }