rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 13 Sep 2014 18:33:05 +0200
changeset 1688 e709c530c227
parent 1676 87f66a77adf9
parent 1686 63ca617c8101
child 1696 ce34fdc36fac
permissions -rw-r--r--
Lambda's work OK in AOT mode. Defender methods work OK. Merging.
     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                     println("  mi[0]: " + mi[0]);
  1080                     println("  mi[1]: " + mi[1]);
  1081                     println("  mi[2]: " + mi[2]);
  1082                     println("  mn   : " + mn);
  1083                     println("  name and type: " + jc.stringValue(c2.cpx2, true));
  1084                     CPX2 nameAndType = jc.getCpoolEntry(c2.cpx2);
  1085                     String type = jc.StringValue(nameAndType.cpx2);
  1086                     String object = accessClass(mcn) + "(false)";
  1087                     if (mn.startsWith("cons_")) {
  1088                         object += ".constructor";
  1089                     }
  1090                     append("var metHan = ");
  1091                     append(accessStaticMethod(object, mn, mi));
  1092                     append('(');
  1093                     String lookup = accessClass("java_lang_invoke_MethodHandles") + "(false).findFor__Ljava_lang_invoke_MethodHandles$Lookup_2Ljava_lang_Class_2(CLS.$class)";
  1094                     append(lookup);
  1095                     append(", '").append(mi[1]).append("', ");
  1096                     String methodType = accessClass("java_lang_invoke_MethodType") + "(false).fromMethodDescriptorString__Ljava_lang_invoke_MethodType_2Ljava_lang_String_2Ljava_lang_ClassLoader_2(";
  1097                     append(methodType).append("'").append(type).append("', null)");
  1098 //                    if (numArguments > 0) {
  1099 //                        append(vars[0]);
  1100 //                        for (int j = 1; j < numArguments; ++j) {
  1101 //                            append(", ");
  1102 //                            append(vars[j]);
  1103 //                        }
  1104 //                    }
  1105                     append(");");
  1106                     emit(smapper, this, "throw 'Invoke dynamic: ' + @1 + ': ' + metHan;", "" + indx);
  1107                     i += 4;
  1108                     break;
  1109                 }
  1110                 case opc_new: {
  1111                     int indx = readUShortArg(byteCodes, i);
  1112                     String ci = jc.getClassName(indx);
  1113                     emit(smapper, this, "var @1 = new @2;",
  1114                          smapper.pushA(), accessClass(mangleClassName(ci)));
  1115                     addReference(ci);
  1116                     i += 2;
  1117                     break;
  1118                 }
  1119                 case opc_newarray:
  1120                     int atype = readUByte(byteCodes, ++i);
  1121                     generateNewArray(atype, smapper);
  1122                     break;
  1123                 case opc_anewarray: {
  1124                     int type = readUShortArg(byteCodes, i);
  1125                     i += 2;
  1126                     generateANewArray(type, smapper);
  1127                     break;
  1128                 }
  1129                 case opc_multianewarray: {
  1130                     int type = readUShortArg(byteCodes, i);
  1131                     i += 2;
  1132                     i = generateMultiANewArray(type, byteCodes, i, smapper);
  1133                     break;
  1134                 }
  1135                 case opc_arraylength:
  1136                     smapper.replace(this, VarType.INTEGER, "(@1).length", smapper.getA(0));
  1137                     break;
  1138                 case opc_lastore:
  1139                     emit(smapper, this, "Array.at(@3, @2, @1);",
  1140                          smapper.popL(), smapper.popI(), smapper.popA());
  1141                     break;
  1142                 case opc_fastore:
  1143                     emit(smapper, this, "Array.at(@3, @2, @1);",
  1144                          smapper.popF(), smapper.popI(), smapper.popA());
  1145                     break;
  1146                 case opc_dastore:
  1147                     emit(smapper, this, "Array.at(@3, @2, @1);",
  1148                          smapper.popD(), smapper.popI(), smapper.popA());
  1149                     break;
  1150                 case opc_aastore:
  1151                     emit(smapper, this, "Array.at(@3, @2, @1);",
  1152                          smapper.popA(), smapper.popI(), smapper.popA());
  1153                     break;
  1154                 case opc_iastore:
  1155                 case opc_bastore:
  1156                 case opc_castore:
  1157                 case opc_sastore:
  1158                     emit(smapper, this, "Array.at(@3, @2, @1);",
  1159                          smapper.popI(), smapper.popI(), smapper.popA());
  1160                     break;
  1161                 case opc_laload:
  1162                     smapper.replace(this, VarType.LONG, "Array.at(@2, @1)",
  1163                          smapper.popI(), smapper.getA(0));
  1164                     break;
  1165                 case opc_faload:
  1166                     smapper.replace(this, VarType.FLOAT, "Array.at(@2, @1)",
  1167                          smapper.popI(), smapper.getA(0));
  1168                     break;
  1169                 case opc_daload:
  1170                     smapper.replace(this, VarType.DOUBLE, "Array.at(@2, @1)",
  1171                          smapper.popI(), smapper.getA(0));
  1172                     break;
  1173                 case opc_aaload:
  1174                     smapper.replace(this, VarType.REFERENCE, "Array.at(@2, @1)",
  1175                          smapper.popI(), smapper.getA(0));
  1176                     break;
  1177                 case opc_iaload:
  1178                 case opc_baload:
  1179                 case opc_caload:
  1180                 case opc_saload:
  1181                     smapper.replace(this, VarType.INTEGER, "Array.at(@2, @1)",
  1182                          smapper.popI(), smapper.getA(0));
  1183                     break;
  1184                 case opc_pop:
  1185                 case opc_pop2:
  1186                     smapper.pop(1);
  1187                     debug("/* pop */");
  1188                     break;
  1189                 case opc_dup: {
  1190                     final Variable v = smapper.get(0);
  1191                     if (smapper.isDirty()) {
  1192                         emit(smapper, this, "var @1 = @2;", smapper.pushT(v.getType()), v);
  1193                     } else {
  1194                         smapper.assign(this, v.getType(), v);
  1195                     }   
  1196                     break;
  1197                 }
  1198                 case opc_dup2: {
  1199                     final Variable vi1 = smapper.get(0);
  1200 
  1201                     if (vi1.isCategory2()) {
  1202                         emit(smapper, this, "var @1 = @2;",
  1203                              smapper.pushT(vi1.getType()), vi1);
  1204                     } else {
  1205                         final Variable vi2 = smapper.get(1);
  1206                         emit(smapper, this, "var @1 = @2, @3 = @4;",
  1207                              smapper.pushT(vi2.getType()), vi2,
  1208                              smapper.pushT(vi1.getType()), vi1);
  1209                     }
  1210                     break;
  1211                 }
  1212                 case opc_dup_x1: {
  1213                     final Variable vi1 = smapper.pop(this);
  1214                     final Variable vi2 = smapper.pop(this);
  1215                     final Variable vo3 = smapper.pushT(vi1.getType());
  1216                     final Variable vo2 = smapper.pushT(vi2.getType());
  1217                     final Variable vo1 = smapper.pushT(vi1.getType());
  1218 
  1219                     emit(smapper, this, "var @1 = @2, @3 = @4, @5 = @6;",
  1220                          vo1, vi1, vo2, vi2, vo3, vo1);
  1221                     break;
  1222                 }
  1223                 case opc_dup2_x1: {
  1224                     final Variable vi1 = smapper.pop(this);
  1225                     final Variable vi2 = smapper.pop(this);
  1226 
  1227                     if (vi1.isCategory2()) {
  1228                         final Variable vo3 = smapper.pushT(vi1.getType());
  1229                         final Variable vo2 = smapper.pushT(vi2.getType());
  1230                         final Variable vo1 = smapper.pushT(vi1.getType());
  1231 
  1232                         emit(smapper, this, "var @1 = @2, @3 = @4, @5 = @6;",
  1233                              vo1, vi1, vo2, vi2, vo3, vo1);
  1234                     } else {
  1235                         final Variable vi3 = smapper.pop(this);
  1236                         final Variable vo5 = smapper.pushT(vi2.getType());
  1237                         final Variable vo4 = smapper.pushT(vi1.getType());
  1238                         final Variable vo3 = smapper.pushT(vi3.getType());
  1239                         final Variable vo2 = smapper.pushT(vi2.getType());
  1240                         final Variable vo1 = smapper.pushT(vi1.getType());
  1241 
  1242                         emit(smapper, this, "var @1 = @2, @3 = @4, @5 = @6,",
  1243                              vo1, vi1, vo2, vi2, vo3, vi3);
  1244                         emit(smapper, this, " @1 = @2, @3 = @4;",
  1245                              vo4, vo1, vo5, vo2);
  1246                     }
  1247                     break;
  1248                 }
  1249                 case opc_dup_x2: {
  1250                     final Variable vi1 = smapper.pop(this);
  1251                     final Variable vi2 = smapper.pop(this);
  1252 
  1253                     if (vi2.isCategory2()) {
  1254                         final Variable vo3 = smapper.pushT(vi1.getType());
  1255                         final Variable vo2 = smapper.pushT(vi2.getType());
  1256                         final Variable vo1 = smapper.pushT(vi1.getType());
  1257 
  1258                         emit(smapper, this, "var @1 = @2, @3 = @4, @5 = @6;",
  1259                              vo1, vi1, vo2, vi2, vo3, vo1);
  1260                     } else {
  1261                         final Variable vi3 = smapper.pop(this);
  1262                         final Variable vo4 = smapper.pushT(vi1.getType());
  1263                         final Variable vo3 = smapper.pushT(vi3.getType());
  1264                         final Variable vo2 = smapper.pushT(vi2.getType());
  1265                         final Variable vo1 = smapper.pushT(vi1.getType());
  1266 
  1267                         emit(smapper, this, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8;",
  1268                              vo1, vi1, vo2, vi2, vo3, vi3, vo4, vo1);
  1269                     }
  1270                     break;
  1271                 }
  1272                 case opc_dup2_x2: {
  1273                     final Variable vi1 = smapper.pop(this);
  1274                     final Variable vi2 = smapper.pop(this);
  1275 
  1276                     if (vi1.isCategory2()) {
  1277                         if (vi2.isCategory2()) {
  1278                             final Variable vo3 = smapper.pushT(vi1.getType());
  1279                             final Variable vo2 = smapper.pushT(vi2.getType());
  1280                             final Variable vo1 = smapper.pushT(vi1.getType());
  1281 
  1282                             emit(smapper, this, "var @1 = @2, @3 = @4, @5 = @6;",
  1283                                  vo1, vi1, vo2, vi2, vo3, vo1);
  1284                         } else {
  1285                             final Variable vi3 = smapper.pop(this);
  1286                             final Variable vo4 = smapper.pushT(vi1.getType());
  1287                             final Variable vo3 = smapper.pushT(vi3.getType());
  1288                             final Variable vo2 = smapper.pushT(vi2.getType());
  1289                             final Variable vo1 = smapper.pushT(vi1.getType());
  1290 
  1291                             emit(smapper, this, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8;",
  1292                                  vo1, vi1, vo2, vi2, vo3, vi3, vo4, vo1);
  1293                         }
  1294                     } else {
  1295                         final Variable vi3 = smapper.pop(this);
  1296 
  1297                         if (vi3.isCategory2()) {
  1298                             final Variable vo5 = smapper.pushT(vi2.getType());
  1299                             final Variable vo4 = smapper.pushT(vi1.getType());
  1300                             final Variable vo3 = smapper.pushT(vi3.getType());
  1301                             final Variable vo2 = smapper.pushT(vi2.getType());
  1302                             final Variable vo1 = smapper.pushT(vi1.getType());
  1303 
  1304                             emit(smapper, this, "var @1 = @2, @3 = @4, @5 = @6,",
  1305                                  vo1, vi1, vo2, vi2, vo3, vi3);
  1306                             emit(smapper, this, " @1 = @2, @3 = @4;",
  1307                                  vo4, vo1, vo5, vo2);
  1308                         } else {
  1309                             final Variable vi4 = smapper.pop(this);
  1310                             final Variable vo6 = smapper.pushT(vi2.getType());
  1311                             final Variable vo5 = smapper.pushT(vi1.getType());
  1312                             final Variable vo4 = smapper.pushT(vi4.getType());
  1313                             final Variable vo3 = smapper.pushT(vi3.getType());
  1314                             final Variable vo2 = smapper.pushT(vi2.getType());
  1315                             final Variable vo1 = smapper.pushT(vi1.getType());
  1316                             
  1317                             emit(smapper, this, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8,",
  1318                                  vo1, vi1, vo2, vi2, vo3, vi3, vo4, vi4);
  1319                             emit(smapper, this, " @1 = @2, @3 = @4;",
  1320                                  vo5, vo1, vo6, vo2);
  1321                         }
  1322                     }
  1323                     break;
  1324                 }
  1325                 case opc_swap: {
  1326                     final Variable vi1 = smapper.get(0);
  1327                     final Variable vi2 = smapper.get(1);
  1328 
  1329                     if (vi1.getType() == vi2.getType()) {
  1330                         final Variable tmp = smapper.pushT(vi1.getType());
  1331 
  1332                         emit(smapper, this, "var @1 = @2, @2 = @3, @3 = @1;",
  1333                              tmp, vi1, vi2);
  1334                         smapper.pop(1);
  1335                     } else {
  1336                         smapper.pop(2);
  1337                         smapper.pushT(vi1.getType());
  1338                         smapper.pushT(vi2.getType());
  1339                     }
  1340                     break;
  1341                 }
  1342                 case opc_bipush:
  1343                     smapper.assign(this, VarType.INTEGER, 
  1344                         "(" + Integer.toString(byteCodes[++i]) + ")");
  1345                     break;
  1346                 case opc_sipush:
  1347                     smapper.assign(this, VarType.INTEGER, 
  1348                         "(" + Integer.toString(readShortArg(byteCodes, i)) + ")"
  1349                     );
  1350                     i += 2;
  1351                     break;
  1352                 case opc_getfield: {
  1353                     int indx = readUShortArg(byteCodes, i);
  1354                     String[] fi = jc.getFieldInfoName(indx);
  1355                     final int type = VarType.fromFieldType(fi[2].charAt(0));
  1356                     final String mangleClass = mangleClassName(fi[0]);
  1357                     final String mangleClassAccess = accessClass(mangleClass);
  1358                     smapper.replace(this, type, "@2.call(@1)",
  1359                          smapper.getA(0),
  1360                          accessField(mangleClassAccess + "(false)",
  1361                                      "_" + fi[1], fi)
  1362                     );
  1363                     i += 2;
  1364                     break;
  1365                 }
  1366                 case opc_putfield: {
  1367                     int indx = readUShortArg(byteCodes, i);
  1368                     String[] fi = jc.getFieldInfoName(indx);
  1369                     final int type = VarType.fromFieldType(fi[2].charAt(0));
  1370                     final String mangleClass = mangleClassName(fi[0]);
  1371                     final String mangleClassAccess = accessClass(mangleClass);
  1372                     emit(smapper, this, "@3.call(@2, @1);",
  1373                          smapper.popT(type),
  1374                          smapper.popA(),
  1375                          accessField(mangleClassAccess + "(false)",
  1376                                      "_" + fi[1], fi));
  1377                     i += 2;
  1378                     break;
  1379                 }
  1380                 case opc_getstatic: {
  1381                     int indx = readUShortArg(byteCodes, i);
  1382                     String[] fi = jc.getFieldInfoName(indx);
  1383                     final int type = VarType.fromFieldType(fi[2].charAt(0));
  1384                     String ac = accessClass(mangleClassName(fi[0]));
  1385                     String af = accessField(ac + "(false)", "_" + fi[1], fi);
  1386                     smapper.assign(this, type, af + "()");
  1387                     i += 2;
  1388                     addReference(fi[0]);
  1389                     break;
  1390                 }
  1391                 case opc_putstatic: {
  1392                     int indx = readUShortArg(byteCodes, i);
  1393                     String[] fi = jc.getFieldInfoName(indx);
  1394                     final int type = VarType.fromFieldType(fi[2].charAt(0));
  1395                     emit(smapper, this, "@1(false)._@2(@3);",
  1396                          accessClass(mangleClassName(fi[0])), fi[1],
  1397                          smapper.popT(type));
  1398                     i += 2;
  1399                     addReference(fi[0]);
  1400                     break;
  1401                 }
  1402                 case opc_checkcast: {
  1403                     int indx = readUShortArg(byteCodes, i);
  1404                     generateCheckcast(indx, smapper);
  1405                     i += 2;
  1406                     break;
  1407                 }
  1408                 case opc_instanceof: {
  1409                     int indx = readUShortArg(byteCodes, i);
  1410                     generateInstanceOf(indx, smapper);
  1411                     i += 2;
  1412                     break;
  1413                 }
  1414                 case opc_athrow: {
  1415                     final CharSequence v = smapper.popA();
  1416                     smapper.clear();
  1417 
  1418                     emit(smapper, this, "{ var @1 = @2; throw @2; }",
  1419                          smapper.pushA(), v);
  1420                     break;
  1421                 }
  1422 
  1423                 case opc_monitorenter: {
  1424                     debug("/* monitor enter */");
  1425                     smapper.popA();
  1426                     break;
  1427                 }
  1428 
  1429                 case opc_monitorexit: {
  1430                     debug("/* monitor exit */");
  1431                     smapper.popA();
  1432                     break;
  1433                 }
  1434 
  1435                 case opc_wide:
  1436                     wide = true;
  1437                     break;
  1438 
  1439                 default: {
  1440                     wide = false;
  1441                     emit(smapper, this, "throw 'unknown bytecode @1';",
  1442                          Integer.toString(c));
  1443                 }
  1444             }
  1445             if (debug(" //")) {
  1446                 generateByteCodeComment(prev, i, byteCodes);
  1447             }
  1448             if (outChanged) {
  1449                 append("\n");
  1450             }
  1451         }
  1452         if (previousTrap != null) {
  1453             generateCatch(previousTrap, byteCodes.length, topMostLabel);
  1454         }
  1455         if (didBranches) {
  1456             append("\n    }\n");
  1457         }
  1458         while (openBraces-- > 0) {
  1459             append('}');
  1460         }
  1461         if (obj) {
  1462             append("\n}});");
  1463         } else {
  1464             append("\n};");
  1465         }
  1466     }
  1467 
  1468     private int generateIf(StackMapper mapper, byte[] byteCodes, 
  1469         int i, final CharSequence v2, final CharSequence v1, 
  1470         final String test, int topMostLabel
  1471     ) throws IOException {
  1472         mapper.flush(this);
  1473         int indx = i + readShortArg(byteCodes, i);
  1474         append("if ((").append(v1)
  1475            .append(") ").append(test).append(" (")
  1476            .append(v2).append(")) ");
  1477         goTo(this, i, indx, topMostLabel);
  1478         return i + 2;
  1479     }
  1480     
  1481     private int readInt4(byte[] byteCodes, int offset) {
  1482         final int d = byteCodes[offset + 0] << 24;
  1483         final int c = byteCodes[offset + 1] << 16;
  1484         final int b = byteCodes[offset + 2] << 8;
  1485         final int a = byteCodes[offset + 3];
  1486         return (d & 0xff000000) | (c & 0xff0000) | (b & 0xff00) | (a & 0xff);
  1487     }
  1488     private static int readUByte(byte[] byteCodes, int offset) {
  1489         return byteCodes[offset] & 0xff;
  1490     }
  1491 
  1492     private static int readUShort(byte[] byteCodes, int offset) {
  1493         return ((byteCodes[offset] & 0xff) << 8)
  1494                     | (byteCodes[offset + 1] & 0xff);
  1495     }
  1496     private static int readUShortArg(byte[] byteCodes, int offsetInstruction) {
  1497         return readUShort(byteCodes, offsetInstruction + 1);
  1498     }
  1499 
  1500     private static int readShort(byte[] byteCodes, int offset) {
  1501         int signed = byteCodes[offset];
  1502         byte b0 = (byte)signed;
  1503         return (b0 << 8) | (byteCodes[offset + 1] & 0xff);
  1504     }
  1505     private static int readShortArg(byte[] byteCodes, int offsetInstruction) {
  1506         return readShort(byteCodes, offsetInstruction + 1);
  1507     }
  1508 
  1509     private static void countArgs(String descriptor, char[] returnType, StringBuilder sig, StringBuilder cnt) {
  1510         int i = 0;
  1511         Boolean count = null;
  1512         boolean array = false;
  1513         sig.append("__");
  1514         int firstPos = sig.length();
  1515         while (i < descriptor.length()) {
  1516             char ch = descriptor.charAt(i++);
  1517             switch (ch) {
  1518                 case '(':
  1519                     count = true;
  1520                     continue;
  1521                 case ')':
  1522                     count = false;
  1523                     continue;
  1524                 case 'B': 
  1525                 case 'C': 
  1526                 case 'D': 
  1527                 case 'F': 
  1528                 case 'I': 
  1529                 case 'J': 
  1530                 case 'S': 
  1531                 case 'Z': 
  1532                     if (count) {
  1533                         if (array) {
  1534                             sig.append("_3");
  1535                         }
  1536                         sig.append(ch);
  1537                         if (ch == 'J' || ch == 'D') {
  1538                             cnt.append('1');
  1539                         } else {
  1540                             cnt.append('0');
  1541                         }
  1542                     } else {
  1543                         sig.insert(firstPos, ch);
  1544                         if (array) {
  1545                             returnType[0] = '[';
  1546                             sig.insert(firstPos, "_3");
  1547                         } else {
  1548                             returnType[0] = ch;
  1549                         }
  1550                     }
  1551                     array = false;
  1552                     continue;
  1553                 case 'V': 
  1554                     assert !count;
  1555                     returnType[0] = 'V';
  1556                     sig.insert(firstPos, 'V');
  1557                     continue;
  1558                 case 'L':
  1559                     int next = descriptor.indexOf(';', i);
  1560                     String realSig = mangleSig(descriptor, i - 1, next + 1);
  1561                     if (count) {
  1562                         if (array) {
  1563                             sig.append("_3");
  1564                         }
  1565                         sig.append(realSig);
  1566                         cnt.append('0');
  1567                     } else {
  1568                         sig.insert(firstPos, realSig);
  1569                         if (array) {
  1570                             sig.insert(firstPos, "_3");
  1571                         }
  1572                         returnType[0] = 'L';
  1573                     }
  1574                     i = next + 1;
  1575                     array = false;
  1576                     continue;
  1577                 case '[':
  1578                     array = true;
  1579                     continue;
  1580                 default:
  1581                     throw new IllegalStateException("Invalid char: " + ch);
  1582             }
  1583         }
  1584     }
  1585     
  1586     static String mangleSig(String sig) {
  1587         return mangleSig(sig, 0, sig.length());
  1588     }
  1589     
  1590     private static String mangleMethodName(String name) {
  1591         StringBuilder sb = new StringBuilder(name.length() * 2);
  1592         int last = name.length();
  1593         for (int i = 0; i < last; i++) {
  1594             final char ch = name.charAt(i);
  1595             switch (ch) {
  1596                 case '_': sb.append("_1"); break;
  1597                 default: sb.append(ch); break;
  1598             }
  1599         }
  1600         return sb.toString();
  1601     }
  1602     private static String mangleSig(String txt, int first, int last) {
  1603         StringBuilder sb = new StringBuilder((last - first) * 2);
  1604         for (int i = first; i < last; i++) {
  1605             final char ch = txt.charAt(i);
  1606             switch (ch) {
  1607                 case '/': sb.append('_'); break;
  1608                 case '_': sb.append("_1"); break;
  1609                 case ';': sb.append("_2"); break;
  1610                 case '[': sb.append("_3"); break;
  1611                 default: 
  1612                     if (Character.isJavaIdentifierPart(ch)) {
  1613                         sb.append(ch);
  1614                     } else {
  1615                         sb.append("_0");
  1616                         String hex = Integer.toHexString(ch).toLowerCase();
  1617                         for (int m = hex.length(); m < 4; m++) {
  1618                             sb.append("0");
  1619                         }
  1620                         sb.append(hex);
  1621                     }
  1622                 break;
  1623             }
  1624         }
  1625         return sb.toString();
  1626     }
  1627     
  1628     private static String mangleClassName(String name) {
  1629         return mangleSig(name);
  1630     }
  1631 
  1632     private static String findMethodName(MethodData m, StringBuilder cnt) {
  1633         StringBuilder name = new StringBuilder();
  1634         if ("<init>".equals(m.getName())) { // NOI18N
  1635             name.append("cons"); // NOI18N
  1636         } else if ("<clinit>".equals(m.getName())) { // NOI18N
  1637             name.append("class"); // NOI18N
  1638         } else {
  1639             name.append(mangleMethodName(m.getName()));
  1640         } 
  1641         
  1642         countArgs(m.getInternalSig(), new char[1], name, cnt);
  1643         return name.toString();
  1644     }
  1645 
  1646     static String findMethodName(String[] mi, StringBuilder cnt, char[] returnType) {
  1647         StringBuilder name = new StringBuilder();
  1648         String descr = mi[2];//mi.getDescriptor();
  1649         String nm= mi[1];
  1650         if ("<init>".equals(nm)) { // NOI18N
  1651             name.append("cons"); // NOI18N
  1652         } else {
  1653             name.append(mangleMethodName(nm));
  1654         }
  1655         countArgs(descr, returnType, name, cnt);
  1656         return name.toString();
  1657     }
  1658 
  1659     private int invokeStaticMethod(byte[] byteCodes, int i, final StackMapper mapper, boolean isStatic)
  1660     throws IOException {
  1661         int methodIndex = readUShortArg(byteCodes, i);
  1662         String[] mi = jc.getFieldInfoName(methodIndex);
  1663         char[] returnType = { 'V' };
  1664         StringBuilder cnt = new StringBuilder();
  1665         String mn = findMethodName(mi, cnt, returnType);
  1666 
  1667         final int numArguments = isStatic ? cnt.length() : cnt.length() + 1;
  1668         final CharSequence[] vars = new CharSequence[numArguments];
  1669 
  1670         for (int j = numArguments - 1; j >= 0; --j) {
  1671             vars[j] = mapper.popValue();
  1672         }
  1673 
  1674         if (returnType[0] != 'V') {
  1675             mapper.flush(this);
  1676             append("var ")
  1677                .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
  1678                .append(" = ");
  1679         }
  1680 
  1681         final String in = mi[0];
  1682         String mcn;
  1683         if (callbacks && (
  1684             in.equals("org/apidesign/html/boot/spi/Fn") ||
  1685             in.equals("org/netbeans/html/boot/spi/Fn")
  1686         )) {
  1687             mcn = "java_lang_Class";
  1688         } else {
  1689             mcn = mangleClassName(in);
  1690         }
  1691         String object = accessClass(mcn) + "(false)";
  1692         if (mn.startsWith("cons_")) {
  1693             object += ".constructor";
  1694         }
  1695         append(accessStaticMethod(object, mn, mi));
  1696         if (isStatic) {
  1697             append('(');
  1698         } else {
  1699             append(".call(");
  1700         }
  1701         if (numArguments > 0) {
  1702             append(vars[0]);
  1703             for (int j = 1; j < numArguments; ++j) {
  1704                 append(", ");
  1705                 append(vars[j]);
  1706             }
  1707         }
  1708         append(");");
  1709         i += 2;
  1710         addReference(in);
  1711         return i;
  1712     }
  1713     private int invokeVirtualMethod(byte[] byteCodes, int i, final StackMapper mapper)
  1714     throws IOException {
  1715         int methodIndex = readUShortArg(byteCodes, i);
  1716         String[] mi = jc.getFieldInfoName(methodIndex);
  1717         char[] returnType = { 'V' };
  1718         StringBuilder cnt = new StringBuilder();
  1719         String mn = findMethodName(mi, cnt, returnType);
  1720 
  1721         final int numArguments = cnt.length() + 1;
  1722         final CharSequence[] vars =  new CharSequence[numArguments];
  1723 
  1724         for (int j = numArguments - 1; j >= 0; --j) {
  1725             vars[j] = mapper.popValue();
  1726         }
  1727 
  1728         if (returnType[0] != 'V') {
  1729             append("var ")
  1730                .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
  1731                .append(" = ");
  1732         }
  1733 
  1734         append(accessVirtualMethod(vars[0].toString(), mn, mi, numArguments));
  1735         String sep = "";
  1736         for (int j = 1; j < numArguments; ++j) {
  1737             append(sep);
  1738             append(vars[j]);
  1739             sep = ", ";
  1740         }
  1741         append(");");
  1742         i += 2;
  1743         return i;
  1744     }
  1745 
  1746     private void addReference(String cn) throws IOException {
  1747         if (requireReference(cn)) {
  1748             debug(" /* needs " + cn + " */");
  1749         }
  1750     }
  1751 
  1752     private void outType(String d, StringBuilder out) {
  1753         int arr = 0;
  1754         while (d.charAt(0) == '[') {
  1755             out.append('A');
  1756             d = d.substring(1);
  1757         }
  1758         if (d.charAt(0) == 'L') {
  1759             assert d.charAt(d.length() - 1) == ';';
  1760             out.append(d.replace('/', '_').substring(0, d.length() - 1));
  1761         } else {
  1762             out.append(d);
  1763         }
  1764     }
  1765 
  1766     private String encodeConstant(int entryIndex) throws IOException {
  1767         String[] classRef = { null };
  1768         String s = jc.stringValue(entryIndex, classRef);
  1769         if (classRef[0] != null) {
  1770             if (classRef[0].startsWith("[")) {
  1771                 s = accessClass("java_lang_Class") + "(false)['forName__Ljava_lang_Class_2Ljava_lang_String_2']('" + classRef[0] + "')";
  1772             } else {
  1773                 addReference(classRef[0]);
  1774                 s = accessClass(mangleClassName(s)) + "(false).constructor.$class";
  1775             }
  1776         }
  1777         return s;
  1778     }
  1779 
  1780     private String javaScriptBody(String destObject, MethodData m, boolean isStatic) throws IOException {
  1781         byte[] arr = m.findAnnotationData(true);
  1782         if (arr == null) {
  1783             return null;
  1784         }
  1785         final String jvmType = "Lorg/apidesign/bck2brwsr/core/JavaScriptBody;";
  1786         final String htmlType = "Lnet/java/html/js/JavaScriptBody;";
  1787         class P extends AnnotationParser {
  1788             public P() {
  1789                 super(false, true);
  1790             }
  1791             
  1792             int cnt;
  1793             String[] args = new String[30];
  1794             String body;
  1795             boolean javacall;
  1796             boolean html4j;
  1797             
  1798             @Override
  1799             protected void visitAttr(String type, String attr, String at, String value) {
  1800                 if (type.equals(jvmType)) {
  1801                     if ("body".equals(attr)) {
  1802                         body = value;
  1803                     } else if ("args".equals(attr)) {
  1804                         args[cnt++] = value;
  1805                     } else {
  1806                         throw new IllegalArgumentException(attr);
  1807                     }
  1808                 }
  1809                 if (type.equals(htmlType)) {
  1810                     html4j = true;
  1811                     if ("body".equals(attr)) {
  1812                         body = value;
  1813                     } else if ("args".equals(attr)) {
  1814                         args[cnt++] = value;
  1815                     } else if ("javacall".equals(attr)) {
  1816                         javacall = "1".equals(value);
  1817                     } else if ("wait4js".equals(attr)) {
  1818                         // ignore, we always invoke synchronously
  1819                     } else {
  1820                         throw new IllegalArgumentException(attr);
  1821                     }
  1822                 }
  1823             }
  1824         }
  1825         P p = new P();
  1826         p.parse(arr, jc);
  1827         if (p.body == null) {
  1828             return null;
  1829         }
  1830         StringBuilder cnt = new StringBuilder();
  1831         final String mn = findMethodName(m, cnt);
  1832         append(destObject).append(".").append(mn);
  1833         append(" = function(");
  1834         String space = "";
  1835         int index = 0;
  1836         StringBuilder toValue = new StringBuilder();
  1837         for (int i = 0; i < cnt.length(); i++) {
  1838             append(space);
  1839             space = outputArg(this, p.args, index);
  1840             if (p.html4j && space.length() > 0) {
  1841                 toValue.append("\n  ").append(p.args[index]).append(" = ")
  1842                     .append(accessClass("java_lang_Class")).append("(false).toJS(").
  1843                     append(p.args[index]).append(");");
  1844             }
  1845             index++;
  1846         }
  1847         append(") {").append("\n");
  1848         append(toValue.toString());
  1849         if (p.javacall) {
  1850             int lastSlash = jc.getClassName().lastIndexOf('/');
  1851             final String pkg = jc.getClassName().substring(0, lastSlash);
  1852             append(mangleCallbacks(pkg, p.body));
  1853             requireReference(pkg + "/$JsCallbacks$");
  1854         } else {
  1855             append(p.body);
  1856         }
  1857         append("\n}\n");
  1858         return mn;
  1859     }
  1860     
  1861     private static CharSequence mangleCallbacks(String pkgName, String body) {
  1862         StringBuilder sb = new StringBuilder();
  1863         int pos = 0;
  1864         for (;;) {
  1865             int next = body.indexOf(".@", pos);
  1866             if (next == -1) {
  1867                 sb.append(body.substring(pos));
  1868                 body = sb.toString();
  1869                 break;
  1870             }
  1871             int ident = next;
  1872             while (ident > 0) {
  1873                 if (!Character.isJavaIdentifierPart(body.charAt(--ident))) {
  1874                     ident++;
  1875                     break;
  1876                 }
  1877             }
  1878             String refId = body.substring(ident, next);
  1879 
  1880             sb.append(body.substring(pos, ident));
  1881 
  1882             int sigBeg = body.indexOf('(', next);
  1883             int sigEnd = body.indexOf(')', sigBeg);
  1884             int colon4 = body.indexOf("::", next);
  1885             if (sigBeg == -1 || sigEnd == -1 || colon4 == -1) {
  1886                 throw new IllegalStateException("Malformed body " + body);
  1887             }
  1888             String fqn = body.substring(next + 2, colon4);
  1889             String method = body.substring(colon4 + 2, sigBeg);
  1890             String params = body.substring(sigBeg, sigEnd + 1);
  1891 
  1892             int paramBeg = body.indexOf('(', sigEnd + 1);
  1893             
  1894             sb.append("vm.").append(pkgName.replace('/', '_')).append("_$JsCallbacks$(false)._VM().");
  1895             sb.append(mangleJsCallbacks(fqn, method, params, false));
  1896             sb.append("(").append(refId);
  1897             if (body.charAt(paramBeg + 1) != ')') {
  1898                 sb.append(",");
  1899             }
  1900             pos = paramBeg + 1;
  1901         }
  1902         sb = null;
  1903         pos = 0;
  1904         for (;;) {
  1905             int next = body.indexOf("@", pos);
  1906             if (next == -1) {
  1907                 if (sb == null) {
  1908                     return body;
  1909                 }
  1910                 sb.append(body.substring(pos));
  1911                 return sb;
  1912             }
  1913             if (sb == null) {
  1914                 sb = new StringBuilder();
  1915             }
  1916 
  1917             sb.append(body.substring(pos, next));
  1918 
  1919             int sigBeg = body.indexOf('(', next);
  1920             int sigEnd = body.indexOf(')', sigBeg);
  1921             int colon4 = body.indexOf("::", next);
  1922             if (sigBeg == -1 || sigEnd == -1 || colon4 == -1) {
  1923                 throw new IllegalStateException("Malformed body " + body);
  1924             }
  1925             String fqn = body.substring(next + 1, colon4);
  1926             String method = body.substring(colon4 + 2, sigBeg);
  1927             String params = body.substring(sigBeg, sigEnd + 1);
  1928 
  1929             int paramBeg = body.indexOf('(', sigEnd + 1);
  1930             
  1931             sb.append("vm.").append(pkgName.replace('/', '_')).append("_$JsCallbacks$(false)._VM().");
  1932             sb.append(mangleJsCallbacks(fqn, method, params, true));
  1933             sb.append("(");
  1934             pos = paramBeg + 1;
  1935         }
  1936     }
  1937 
  1938     static String mangleJsCallbacks(String fqn, String method, String params, boolean isStatic) {
  1939         if (params.startsWith("(")) {
  1940             params = params.substring(1);
  1941         }
  1942         if (params.endsWith(")")) {
  1943             params = params.substring(0, params.length() - 1);
  1944         }
  1945         StringBuilder sb = new StringBuilder();
  1946         final String fqnu = fqn.replace('.', '_');
  1947         final String rfqn = mangleClassName(fqnu);
  1948         final String rm = mangleMethodName(method);
  1949         final String srp;
  1950         {
  1951             StringBuilder pb = new StringBuilder();
  1952             int len = params.length();
  1953             int indx = 0;
  1954             while (indx < len) {
  1955                 char ch = params.charAt(indx);
  1956                 if (ch == '[' || ch == 'L') {
  1957                     pb.append("Ljava/lang/Object;");
  1958                     indx = params.indexOf(';', indx) + 1;
  1959                 } else {
  1960                     pb.append(ch);
  1961                     indx++;
  1962                 }
  1963             }
  1964             srp = mangleSig(pb.toString());
  1965         }
  1966         final String rp = mangleSig(params);
  1967         final String mrp = mangleMethodName(rp);
  1968         sb.append(rfqn).append("$").append(rm).
  1969             append('$').append(mrp).append("__Ljava_lang_Object_2");
  1970         if (!isStatic) {
  1971             sb.append('L').append(fqnu).append("_2");
  1972         }
  1973         sb.append(srp);
  1974         return sb.toString();
  1975     }
  1976 
  1977     private static String className(ClassData jc) {
  1978         //return jc.getName().getInternalName().replace('/', '_');
  1979         return mangleClassName(jc.getClassName());
  1980     }
  1981     
  1982     private static String[] findAnnotation(
  1983         byte[] arr, ClassData cd, final String className, 
  1984         final String... attrNames
  1985     ) throws IOException {
  1986         if (arr == null) {
  1987             return null;
  1988         }
  1989         final String[] values = new String[attrNames.length];
  1990         final boolean[] found = { false };
  1991         final String jvmType = "L" + className.replace('.', '/') + ";";
  1992         AnnotationParser ap = new AnnotationParser(false, true) {
  1993             @Override
  1994             protected void visitAttr(String type, String attr, String at, String value) {
  1995                 if (type.equals(jvmType)) {
  1996                     found[0] = true;
  1997                     for (int i = 0; i < attrNames.length; i++) {
  1998                         if (attrNames[i].equals(attr)) {
  1999                             values[i] = value;
  2000                         }
  2001                     }
  2002                 }
  2003             }
  2004             
  2005         };
  2006         ap.parse(arr, cd);
  2007         return found[0] ? values : null;
  2008     }
  2009 
  2010     private CharSequence initField(FieldData v) {
  2011         final String is = v.getInternalSig();
  2012         if (is.length() == 1) {
  2013             switch (is.charAt(0)) {
  2014                 case 'S':
  2015                 case 'J':
  2016                 case 'B':
  2017                 case 'Z':
  2018                 case 'C':
  2019                 case 'I': return " = 0;";
  2020                 case 'F': 
  2021                 case 'D': return " = 0.0;";
  2022                 default:
  2023                     throw new IllegalStateException(is);
  2024             }
  2025         }
  2026         return " = null;";
  2027     }
  2028 
  2029     private void generateAnno(ClassData cd, byte[] data) throws IOException {
  2030         AnnotationParser ap = new AnnotationParser(true, false) {
  2031             int[] cnt = new int[32];
  2032             int depth;
  2033             
  2034             @Override
  2035             protected void visitAnnotationStart(String attrType, boolean top) throws IOException {
  2036                 final String slashType = attrType.substring(1, attrType.length() - 1);
  2037                 requireReference(slashType);
  2038                 
  2039                 if (cnt[depth]++ > 0) {
  2040                     append(",");
  2041                 }
  2042                 if (top) {
  2043                     append('"').append(attrType).append("\" : ");
  2044                 }
  2045                 append("{\n");
  2046                 cnt[++depth] = 0;
  2047             }
  2048 
  2049             @Override
  2050             protected void visitAnnotationEnd(String type, boolean top) throws IOException {
  2051                 append("\n}\n");
  2052                 depth--;
  2053             }
  2054 
  2055             @Override
  2056             protected void visitValueStart(String attrName, char type) throws IOException {
  2057                 if (cnt[depth]++ > 0) {
  2058                     append(",\n");
  2059                 }
  2060                 cnt[++depth] = 0;
  2061                 if (attrName != null) {
  2062                     append('"').append(attrName).append("\" : ");
  2063                 }
  2064                 if (type == '[') {
  2065                     append("[");
  2066                 }
  2067             }
  2068 
  2069             @Override
  2070             protected void visitValueEnd(String attrName, char type) throws IOException {
  2071                 if (type == '[') {
  2072                     append("]");
  2073                 }
  2074                 depth--;
  2075             }
  2076             
  2077             @Override
  2078             protected void visitAttr(String type, String attr, String attrType, String value) 
  2079             throws IOException {
  2080                 if (attr == null && value == null) {
  2081                     return;
  2082                 }
  2083                 append(value);
  2084             }
  2085 
  2086             @Override
  2087             protected void visitEnumAttr(String type, String attr, String attrType, String value) 
  2088             throws IOException {
  2089                 final String slashType = attrType.substring(1, attrType.length() - 1);
  2090                 requireReference(slashType);
  2091                 
  2092                 final String cn = mangleClassName(slashType);
  2093                 append(accessClass(cn))
  2094                    .append("(false)['valueOf__L").
  2095                     append(cn).
  2096                     append("_2Ljava_lang_String_2']('").
  2097                     append(value).
  2098                     append("')");
  2099             }
  2100         };
  2101         ap.parse(data, cd);
  2102     }
  2103 
  2104     private static String outputArg(Appendable out, String[] args, int indx) throws IOException {
  2105         final String name = args[indx];
  2106         if (name == null) {
  2107             return "";
  2108         }
  2109         if (name.contains(",")) {
  2110             throw new IOException("Wrong parameter with ',': " + name);
  2111         }
  2112         out.append(name);
  2113         return ",";
  2114     }
  2115 
  2116     final void emitNoFlush(
  2117         StackMapper sm, 
  2118         final String format, final CharSequence... params
  2119     ) throws IOException {
  2120         emitImpl(this, format, params);
  2121     }
  2122     static final void emit(
  2123         StackMapper sm, 
  2124         final Appendable out, 
  2125         final String format, final CharSequence... params
  2126     ) throws IOException {
  2127         sm.flush(out);
  2128         emitImpl(out, format, params);
  2129     }
  2130     static void emitImpl(final Appendable out,
  2131                              final String format,
  2132                              final CharSequence... params) throws IOException {
  2133         final int length = format.length();
  2134 
  2135         int processed = 0;
  2136         int paramOffset = format.indexOf('@');
  2137         while ((paramOffset != -1) && (paramOffset < (length - 1))) {
  2138             final char paramChar = format.charAt(paramOffset + 1);
  2139             if ((paramChar >= '1') && (paramChar <= '9')) {
  2140                 final int paramIndex = paramChar - '0' - 1;
  2141 
  2142                 out.append(format, processed, paramOffset);
  2143                 out.append(params[paramIndex]);
  2144 
  2145                 ++paramOffset;
  2146                 processed = paramOffset + 1;
  2147             }
  2148 
  2149             paramOffset = format.indexOf('@', paramOffset + 1);
  2150         }
  2151 
  2152         out.append(format, processed, length);
  2153     }
  2154 
  2155     private void generateCatch(TrapData[] traps, int current, int topMostLabel) throws IOException {
  2156         append("} catch (e) {\n");
  2157         int finallyPC = -1;
  2158         for (TrapData e : traps) {
  2159             if (e == null) {
  2160                 break;
  2161             }
  2162             if (e.catch_cpx != 0) { //not finally
  2163                 final String classInternalName = jc.getClassName(e.catch_cpx);
  2164                 addReference(classInternalName);
  2165                 append("e = vm.java_lang_Class(false).bck2BrwsrThrwrbl(e);");
  2166                 append("if (e['$instOf_" + classInternalName.replace('/', '_') + "']) {");
  2167                 append("var stA0 = e;");
  2168                 goTo(this, current, e.handler_pc, topMostLabel);
  2169                 append("}\n");
  2170             } else {
  2171                 finallyPC = e.handler_pc;
  2172             }
  2173         }
  2174         if (finallyPC == -1) {
  2175             append("throw e;");
  2176         } else {
  2177             append("var stA0 = e;");
  2178             goTo(this, current, finallyPC, topMostLabel);
  2179         }
  2180         append("\n}");
  2181     }
  2182 
  2183     private static void goTo(Appendable out, int current, int to, int canBack) throws IOException {
  2184         if (to < current) {
  2185             if (canBack < to) {
  2186                 out.append("{ gt = 0; continue X_" + to + "; }");
  2187             } else {
  2188                 out.append("{ gt = " + to + "; continue X_0; }");
  2189             }
  2190         } else {
  2191             out.append("{ gt = " + to + "; break IF; }");
  2192         }
  2193     }
  2194 
  2195     private static void emitIf(
  2196         StackMapper sm, 
  2197         Appendable out, String pattern, 
  2198         CharSequence param, 
  2199         int current, int to, int canBack
  2200     ) throws IOException {
  2201         sm.flush(out);
  2202         emitImpl(out, pattern, param);
  2203         goTo(out, current, to, canBack);
  2204     }
  2205 
  2206     private void generateNewArray(int atype, final StackMapper smapper) throws IOException, IllegalStateException {
  2207         String jvmType;
  2208         switch (atype) {
  2209             case 4: jvmType = "[Z"; break;
  2210             case 5: jvmType = "[C"; break;
  2211             case 6: jvmType = "[F"; break;
  2212             case 7: jvmType = "[D"; break;
  2213             case 8: jvmType = "[B"; break;
  2214             case 9: jvmType = "[S"; break;
  2215             case 10: jvmType = "[I"; break;
  2216             case 11: jvmType = "[J"; break;
  2217             default: throw new IllegalStateException("Array type: " + atype);
  2218         }
  2219         emit(smapper, this, 
  2220             "var @2 = Array.prototype['newArray__Ljava_lang_Object_2ZLjava_lang_String_2Ljava_lang_Object_2I'](true, '@3', null, @1);",
  2221              smapper.popI(), smapper.pushA(), jvmType);
  2222     }
  2223 
  2224     private void generateANewArray(int type, final StackMapper smapper) throws IOException {
  2225         String typeName = jc.getClassName(type);
  2226         String ref = "null";
  2227         if (typeName.startsWith("[")) {
  2228             typeName = "'[" + typeName + "'";
  2229         } else {
  2230             ref = "vm." + mangleClassName(typeName);
  2231             typeName = "'[L" + typeName + ";'";
  2232         }
  2233         emit(smapper, this,
  2234             "var @2 = Array.prototype['newArray__Ljava_lang_Object_2ZLjava_lang_String_2Ljava_lang_Object_2I'](false, @3, @4, @1);",
  2235              smapper.popI(), smapper.pushA(), typeName, ref);
  2236     }
  2237 
  2238     private int generateMultiANewArray(int type, final byte[] byteCodes, int i, final StackMapper smapper) throws IOException {
  2239         String typeName = jc.getClassName(type);
  2240         int dim = readUByte(byteCodes, ++i);
  2241         StringBuilder dims = new StringBuilder();
  2242         dims.append('[');
  2243         for (int d = 0; d < dim; d++) {
  2244             if (d != 0) {
  2245                 dims.insert(1, ",");
  2246             }
  2247             dims.insert(1, smapper.popI());
  2248         }
  2249         dims.append(']');
  2250         String fn = "null";
  2251         if (typeName.charAt(dim) == 'L') {
  2252             fn = "vm." + mangleClassName(typeName.substring(dim + 1, typeName.length() - 1));
  2253         }
  2254         emit(smapper, this, 
  2255             "var @2 = Array.prototype['multiNewArray__Ljava_lang_Object_2Ljava_lang_String_2_3ILjava_lang_Object_2']('@3', @1, @4);",
  2256              dims.toString(), smapper.pushA(), typeName, fn
  2257         );
  2258         return i;
  2259     }
  2260 
  2261     private int generateTableSwitch(int i, final byte[] byteCodes, final StackMapper smapper, int topMostLabel) throws IOException {
  2262         int table = i / 4 * 4 + 4;
  2263         int dflt = i + readInt4(byteCodes, table);
  2264         table += 4;
  2265         int low = readInt4(byteCodes, table);
  2266         table += 4;
  2267         int high = readInt4(byteCodes, table);
  2268         table += 4;
  2269         final CharSequence swVar = smapper.popValue();
  2270         smapper.flush(this);
  2271         append("switch (").append(swVar).append(") {\n");
  2272         while (low <= high) {
  2273             int offset = i + readInt4(byteCodes, table);
  2274             table += 4;
  2275             append("  case " + low).append(":"); goTo(this, i, offset, topMostLabel); append('\n');
  2276             low++;
  2277         }
  2278         append("  default: ");
  2279         goTo(this, i, dflt, topMostLabel);
  2280         append("\n}");
  2281         i = table - 1;
  2282         return i;
  2283     }
  2284 
  2285     private int generateLookupSwitch(int i, final byte[] byteCodes, final StackMapper smapper, int topMostLabel) throws IOException {
  2286         int table = i / 4 * 4 + 4;
  2287         int dflt = i + readInt4(byteCodes, table);
  2288         table += 4;
  2289         int n = readInt4(byteCodes, table);
  2290         table += 4;
  2291         final CharSequence swVar = smapper.popValue();
  2292         smapper.flush(this);
  2293         append("switch (").append(swVar).append(") {\n");
  2294         while (n-- > 0) {
  2295             int cnstnt = readInt4(byteCodes, table);
  2296             table += 4;
  2297             int offset = i + readInt4(byteCodes, table);
  2298             table += 4;
  2299             append("  case " + cnstnt).append(": "); goTo(this, i, offset, topMostLabel); append('\n');
  2300         }
  2301         append("  default: ");
  2302         goTo(this, i, dflt, topMostLabel);
  2303         append("\n}");
  2304         i = table - 1;
  2305         return i;
  2306     }
  2307 
  2308     private void generateInstanceOf(int indx, final StackMapper smapper) throws IOException {
  2309         String type = jc.getClassName(indx);
  2310         if (!type.startsWith("[")) {
  2311             emit(smapper, this, 
  2312                     "var @2 = @1 != null && @1['$instOf_@3'] ? 1 : 0;",
  2313                  smapper.popA(), smapper.pushI(),
  2314                  type.replace('/', '_'));
  2315         } else {
  2316             int cnt = 0;
  2317             while (type.charAt(cnt) == '[') {
  2318                 cnt++;
  2319             }
  2320             if (type.charAt(cnt) == 'L') {
  2321                 type = "vm." + mangleClassName(type.substring(cnt + 1, type.length() - 1));
  2322                 emit(smapper, this, 
  2323                     "var @2 = Array.prototype['isInstance__ZLjava_lang_Object_2ILjava_lang_Object_2'](@1, @4, @3);",
  2324                     smapper.popA(), smapper.pushI(),
  2325                     type, "" + cnt
  2326                 );
  2327             } else {
  2328                 emit(smapper, this, 
  2329                     "var @2 = Array.prototype['isInstance__ZLjava_lang_Object_2Ljava_lang_String_2'](@1, '@3');",
  2330                     smapper.popA(), smapper.pushI(), type
  2331                 );
  2332             }
  2333         }
  2334     }
  2335 
  2336     private void generateCheckcast(int indx, final StackMapper smapper) throws IOException {
  2337         String type = jc.getClassName(indx);
  2338         if (!type.startsWith("[")) {
  2339             emitNoFlush(smapper, 
  2340                  "if (@1 !== null && !@1['$instOf_@2']) throw vm.java_lang_ClassCastException(true);",
  2341                  smapper.getT(0, VarType.REFERENCE, false), type.replace('/', '_'));
  2342         } else {
  2343             int cnt = 0;
  2344             while (type.charAt(cnt) == '[') {
  2345                 cnt++;
  2346             }
  2347             if (type.charAt(cnt) == 'L') {
  2348                 type = "vm." + mangleClassName(type.substring(cnt + 1, type.length() - 1));
  2349                 emitNoFlush(smapper, 
  2350                     "if (@1 !== null && !Array.prototype['isInstance__ZLjava_lang_Object_2ILjava_lang_Object_2'](@1, @3, @2)) throw vm.java_lang_ClassCastException(true);",
  2351                      smapper.getT(0, VarType.REFERENCE, false), type, "" + cnt
  2352                 );
  2353             } else {
  2354                 emitNoFlush(smapper, 
  2355                     "if (@1 !== null && !Array.prototype['isInstance__ZLjava_lang_Object_2Ljava_lang_String_2'](@1, '@2')) throw vm.java_lang_ClassCastException(true);",
  2356                      smapper.getT(0, VarType.REFERENCE, false), type
  2357                 );
  2358             }
  2359         }
  2360     }
  2361 
  2362     private void generateByteCodeComment(int prev, int i, final byte[] byteCodes) throws IOException {
  2363         for (int j = prev; j <= i; j++) {
  2364             append(" ");
  2365             final int cc = readUByte(byteCodes, j);
  2366             append(Integer.toString(cc));
  2367         }
  2368     }
  2369     
  2370     @JavaScriptBody(args = "msg", body = "")
  2371     private static void println(String msg) {
  2372         System.err.println(msg);
  2373     }
  2374 }