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