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