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