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