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