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