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