rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
author Lubomir Nerad <lubomir.nerad@oracle.com>
Tue, 19 Mar 2013 13:18:02 +0100
branchclosure
changeset 861 f41a1fef7738
parent 848 677c9bcf507f
parent 843 140c739c607f
child 869 151f4ccd7673
permissions -rw-r--r--
Merge with trunk
     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 static org.apidesign.vm4brwsr.ByteCodeParser.*;
    23 
    24 /** Translator of the code inside class files to JavaScript.
    25  *
    26  * @author Jaroslav Tulach <jtulach@netbeans.org>
    27  */
    28 abstract class ByteCodeToJavaScript {
    29     private ClassData jc;
    30     final Appendable out;
    31 
    32     protected ByteCodeToJavaScript(Appendable out) {
    33         this.out = out;
    34     }
    35     
    36     /* Collects additional required resources.
    37      * 
    38      * @param internalClassName classes that were referenced and should be loaded in order the
    39      *   generated JavaScript code works properly. The names are in internal 
    40      *   JVM form so String is <code>java/lang/String</code>. 
    41      */
    42     protected abstract boolean requireReference(String internalClassName);
    43     
    44     /*
    45      * @param resourcePath name of resources to read
    46      */
    47     protected abstract void requireScript(String resourcePath) throws IOException;
    48     
    49     /** Allows subclasses to redefine what field a function representing a
    50      * class gets assigned. By default it returns the suggested name followed
    51      * by <code>" = "</code>;
    52      * 
    53      * @param className suggested name of the class
    54      */
    55     /* protected */ String assignClass(String className) {
    56         return className + " = ";
    57     }
    58     /* protected */ String accessClass(String classOperation) {
    59         return classOperation;
    60     }
    61     
    62     /** Prints out a debug message. 
    63      * 
    64      * @param msg the message
    65      * @return true if the message has been printed
    66      * @throws IOException 
    67      */
    68     boolean debug(String msg) throws IOException {
    69         out.append(msg);
    70         return true;
    71     }
    72 
    73     /**
    74      * Converts a given class file to a JavaScript version.
    75      *
    76      * @param classFile input stream with code of the .class file
    77      * @return the initialization code for this class, if any. Otherwise <code>null</code>
    78      * 
    79      * @throws IOException if something goes wrong during read or write or translating
    80      */
    81     
    82     public String compile(InputStream classFile) throws IOException {
    83         this.jc = new ClassData(classFile);
    84         if (jc.getMajor_version() < 50) {
    85             throw new IOException("Can't compile " + jc.getClassName() + ". Class file version " + jc.getMajor_version() + "."
    86                 + jc.getMinor_version() + " - recompile with -target 1.6 (at least)."
    87             );
    88         }
    89         byte[] arrData = jc.findAnnotationData(true);
    90         String[] arr = findAnnotation(arrData, jc, 
    91             "org.apidesign.bck2brwsr.core.ExtraJavaScript", 
    92             "resource", "processByteCode"
    93         );
    94         if (arr != null) {
    95             if (!arr[0].isEmpty()) {
    96                 requireScript(arr[0]);
    97             }
    98             if ("0".equals(arr[1])) {
    99                 return null;
   100             }
   101         }
   102         String[] proto = findAnnotation(arrData, jc, 
   103             "org.apidesign.bck2brwsr.core.JavaScriptPrototype", 
   104             "container", "prototype"
   105         );
   106         StringArray toInitilize = new StringArray();
   107         final String className = className(jc);
   108         out.append("\n\n").append(assignClass(className));
   109         out.append("function CLS() {");
   110         out.append("\n  if (!CLS.$class) {");
   111         if (proto == null) {
   112             String sc = jc.getSuperClassName(); // with _
   113             out.append("\n    var pp = ").
   114                 append(accessClass(sc.replace('/', '_'))).append("(true);");
   115             out.append("\n    var p = CLS.prototype = pp;");
   116             out.append("\n    var c = p;");
   117             out.append("\n    var sprcls = pp.constructor.$class;");
   118         } else {
   119             out.append("\n    var p = CLS.prototype = ").append(proto[1]).append(";");
   120             if (proto[0] == null) {
   121                 proto[0] = "p";
   122             }
   123             out.append("\n    var c = ").append(proto[0]).append(";");
   124             out.append("\n    var sprcls = null;");
   125         }
   126         for (FieldData v : jc.getFields()) {
   127             if (v.isStatic()) {
   128                 out.append("\n  CLS.").append(v.getName()).append(initField(v));
   129                 out.append("\n  c._").append(v.getName()).append(" = function (v) {")
   130                    .append("  if (arguments.length == 1) CLS.").append(v.getName())
   131                    .append(" = v; return CLS.").
   132                     append(v.getName()).append("; };");
   133             } else {
   134                 out.append("\n  c._").append(v.getName()).append(" = function (v) {")
   135                    .append("  if (arguments.length == 1) this.fld_").
   136                     append(className).append('_').append(v.getName())
   137                    .append(" = v; return this.fld_").
   138                     append(className).append('_').append(v.getName())
   139                    .append("; };");
   140             }
   141         }
   142         for (MethodData m : jc.getMethods()) {
   143             byte[] onlyArr = m.findAnnotationData(true);
   144             String[] only = findAnnotation(onlyArr, jc, 
   145                 "org.apidesign.bck2brwsr.core.JavaScriptOnly", 
   146                 "name", "value"
   147             );
   148             if (only != null) {
   149                 if (only[0] != null && only[1] != null) {
   150                     out.append("\n    p.").append(only[0]).append(" = ")
   151                         .append(only[1]).append(";");
   152                 }
   153                 continue;
   154             }
   155             String prefix;
   156             String mn;
   157             if (m.isStatic()) {
   158                 prefix = "\n    c.";
   159                 mn = generateStaticMethod(prefix, m, toInitilize);
   160             } else {
   161                 if (m.isConstructor()) {
   162                     prefix = "\n    CLS.";
   163                     mn = generateInstanceMethod(prefix, m);
   164                 } else {
   165                     prefix = "\n    c.";
   166                     mn = generateInstanceMethod(prefix, m);
   167                 }
   168             }
   169             byte[] runAnno = m.findAnnotationData(false);
   170             if (runAnno != null) {
   171                 out.append(prefix).append(mn).append(".anno = {");
   172                 generateAnno(jc, out, runAnno);
   173                 out.append("\n    };");
   174             }
   175             out.append(prefix).append(mn).append(".access = " + m.getAccess()).append(";");
   176             out.append(prefix).append(mn).append(".cls = CLS;");
   177         }
   178         out.append("\n    c.constructor = CLS;");
   179         out.append("\n    c.$instOf_").append(className).append(" = true;");
   180         for (String superInterface : jc.getSuperInterfaces()) {
   181             out.append("\n    c.$instOf_").append(superInterface.replace('/', '_')).append(" = true;");
   182         }
   183         out.append("\n    CLS.$class = 'temp';");
   184         out.append("\n    CLS.$class = ");
   185         out.append(accessClass("java_lang_Class(true);"));
   186         out.append("\n    CLS.$class.jvmName = '").append(jc.getClassName()).append("';");
   187         out.append("\n    CLS.$class.superclass = sprcls;");
   188         out.append("\n    CLS.$class.access = ").append(jc.getAccessFlags()+";");
   189         out.append("\n    CLS.$class.cnstr = CLS;");
   190         byte[] classAnno = jc.findAnnotationData(false);
   191         if (classAnno != null) {
   192             out.append("\n    CLS.$class.anno = {");
   193             generateAnno(jc, out, classAnno);
   194             out.append("\n    };");
   195         }
   196         for (String init : toInitilize.toArray()) {
   197             out.append("\n    ").append(init).append("();");
   198         }
   199         out.append("\n  }");
   200         out.append("\n  if (arguments.length === 0) {");
   201         out.append("\n    if (!(this instanceof CLS)) {");
   202         out.append("\n      return new CLS();");
   203         out.append("\n    }");
   204         for (FieldData v : jc.getFields()) {
   205             byte[] onlyArr = v.findAnnotationData(true);
   206             String[] only = findAnnotation(onlyArr, jc, 
   207                 "org.apidesign.bck2brwsr.core.JavaScriptOnly", 
   208                 "name", "value"
   209             );
   210             if (only != null) {
   211                 if (only[0] != null && only[1] != null) {
   212                     out.append("\n    p.").append(only[0]).append(" = ")
   213                         .append(only[1]).append(";");
   214                 }
   215                 continue;
   216             }
   217             if (!v.isStatic()) {
   218                 out.append("\n    this.fld_").
   219                     append(className).append('_').
   220                     append(v.getName()).append(initField(v));
   221             }
   222         }
   223         out.append("\n    return this;");
   224         out.append("\n  }");
   225         out.append("\n  return arguments[0] ? new CLS() : CLS.prototype;");
   226         out.append("\n};");
   227 //        StringBuilder sb = new StringBuilder();
   228 //        for (String init : toInitilize.toArray()) {
   229 //            sb.append("\n").append(init).append("();");
   230 //        }
   231         return "";
   232     }
   233     private String generateStaticMethod(String prefix, MethodData m, StringArray toInitilize) throws IOException {
   234         String jsb = javaScriptBody(prefix, m, true);
   235         if (jsb != null) {
   236             return jsb;
   237         }
   238         final String mn = findMethodName(m, new StringBuilder());
   239         if (mn.equals("class__V")) {
   240             toInitilize.add(accessClass(className(jc)) + "(false)." + mn);
   241         }
   242         generateMethod(prefix, mn, m);
   243         return mn;
   244     }
   245 
   246     private String generateInstanceMethod(String prefix, MethodData m) throws IOException {
   247         String jsb = javaScriptBody(prefix, m, false);
   248         if (jsb != null) {
   249             return jsb;
   250         }
   251         final String mn = findMethodName(m, new StringBuilder());
   252         generateMethod(prefix, mn, m);
   253         return mn;
   254     }
   255 
   256     private void generateMethod(String prefix, String name, MethodData m)
   257             throws IOException {
   258         final StackMapIterator stackMapIterator = m.createStackMapIterator();
   259         TrapDataIterator trap = m.getTrapDataIterator();
   260         final LocalsMapper lmapper =
   261                 new LocalsMapper(stackMapIterator.getArguments());
   262 
   263         out.append(prefix).append(name).append(" = function(");
   264         lmapper.outputArguments(out, m.isStatic());
   265         out.append(") {").append("\n");
   266 
   267         final byte[] byteCodes = m.getCode();
   268         if (byteCodes == null) {
   269             out.append("  throw 'no code found for ")
   270                .append(jc.getClassName()).append('.')
   271                .append(m.getName()).append("';\n");
   272             out.append("};");
   273             return;
   274         }
   275 
   276         final StackMapper smapper = new StackMapper();
   277 
   278         if (!m.isStatic()) {
   279             out.append("  var ").append(" lcA0 = this;\n");
   280         }
   281 
   282         int lastStackFrame = -1;
   283         TrapData[] previousTrap = null;
   284         boolean wide = false;
   285         
   286         out.append("\n  var gt = 0;\n");
   287         int openBraces = 0;
   288         int topMostLabel = 0;
   289         for (int i = 0; i < byteCodes.length; i++) {
   290             int prev = i;
   291             stackMapIterator.advanceTo(i);
   292             boolean changeInCatch = trap.advanceTo(i);
   293             if (changeInCatch || lastStackFrame != stackMapIterator.getFrameIndex()) {
   294                 if (previousTrap != null) {
   295                     generateCatch(previousTrap, i, topMostLabel);
   296                     previousTrap = null;
   297                 }
   298             }
   299             if (lastStackFrame != stackMapIterator.getFrameIndex()) {
   300                 if (i != 0) {
   301                     out.append("    }\n");
   302                 }
   303                 if (openBraces > 64) {
   304                     for (int c = 0; c < 64; c++) {
   305                         out.append("break;}\n");
   306                     }
   307                     openBraces = 1;
   308                     topMostLabel = i;
   309                 }
   310                 
   311                 lastStackFrame = stackMapIterator.getFrameIndex();
   312                 lmapper.syncWithFrameLocals(stackMapIterator.getFrameLocals());
   313                 smapper.syncWithFrameStack(stackMapIterator.getFrameStack());
   314                 out.append("    X_" + i).append(": for (;;) { IF: if (gt <= " + i + ") {\n");
   315                 openBraces++;
   316                 changeInCatch = true;
   317             } else {
   318                 debug("    /* " + i + " */ ");
   319             }
   320             if (changeInCatch && trap.useTry()) {
   321                 out.append("try {");
   322                 previousTrap = trap.current();
   323             }
   324             final int c = readUByte(byteCodes, i);
   325             switch (c) {
   326                 case opc_aload_0:
   327                     emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(0));
   328                     break;
   329                 case opc_iload_0:
   330                     emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(0));
   331                     break;
   332                 case opc_lload_0:
   333                     emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(0));
   334                     break;
   335                 case opc_fload_0:
   336                     emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(0));
   337                     break;
   338                 case opc_dload_0:
   339                     emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(0));
   340                     break;
   341                 case opc_aload_1:
   342                     emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(1));
   343                     break;
   344                 case opc_iload_1:
   345                     emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(1));
   346                     break;
   347                 case opc_lload_1:
   348                     emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(1));
   349                     break;
   350                 case opc_fload_1:
   351                     emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(1));
   352                     break;
   353                 case opc_dload_1:
   354                     emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(1));
   355                     break;
   356                 case opc_aload_2:
   357                     emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(2));
   358                     break;
   359                 case opc_iload_2:
   360                     emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(2));
   361                     break;
   362                 case opc_lload_2:
   363                     emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(2));
   364                     break;
   365                 case opc_fload_2:
   366                     emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(2));
   367                     break;
   368                 case opc_dload_2:
   369                     emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(2));
   370                     break;
   371                 case opc_aload_3:
   372                     emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(3));
   373                     break;
   374                 case opc_iload_3:
   375                     emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(3));
   376                     break;
   377                 case opc_lload_3:
   378                     emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(3));
   379                     break;
   380                 case opc_fload_3:
   381                     emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(3));
   382                     break;
   383                 case opc_dload_3:
   384                     emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(3));
   385                     break;
   386                 case opc_iload: {
   387                     ++i;
   388                     final int indx = wide ? readUShort(byteCodes, i++)
   389                                           : readUByte(byteCodes, i);
   390                     wide = false;
   391                     emit(out, "var @1 = @2;",
   392                          smapper.pushI(), lmapper.getI(indx));
   393                     break;
   394                 }
   395                 case opc_lload: {
   396                     ++i;
   397                     final int indx = wide ? readUShort(byteCodes, i++)
   398                                           : readUByte(byteCodes, i);
   399                     wide = false;
   400                     emit(out, "var @1 = @2;",
   401                          smapper.pushL(), lmapper.getL(indx));
   402                     break;
   403                 }
   404                 case opc_fload: {
   405                     ++i;
   406                     final int indx = wide ? readUShort(byteCodes, i++)
   407                                           : readUByte(byteCodes, i);
   408                     wide = false;
   409                     emit(out, "var @1 = @2;",
   410                          smapper.pushF(), lmapper.getF(indx));
   411                     break;
   412                 }
   413                 case opc_dload: {
   414                     ++i;
   415                     final int indx = wide ? readUShort(byteCodes, i++)
   416                                           : readUByte(byteCodes, i);
   417                     wide = false;
   418                     emit(out, "var @1 = @2;",
   419                          smapper.pushD(), lmapper.getD(indx));
   420                     break;
   421                 }
   422                 case opc_aload: {
   423                     ++i;
   424                     final int indx = wide ? readUShort(byteCodes, i++)
   425                                           : readUByte(byteCodes, i);
   426                     wide = false;
   427                     emit(out, "var @1 = @2;",
   428                          smapper.pushA(), lmapper.getA(indx));
   429                     break;
   430                 }
   431                 case opc_istore: {
   432                     ++i;
   433                     final int indx = wide ? readUShort(byteCodes, i++)
   434                                           : readUByte(byteCodes, i);
   435                     wide = false;
   436                     emit(out, "var @1 = @2;",
   437                          lmapper.setI(indx), smapper.popI());
   438                     break;
   439                 }
   440                 case opc_lstore: {
   441                     ++i;
   442                     final int indx = wide ? readUShort(byteCodes, i++)
   443                                           : readUByte(byteCodes, i);
   444                     wide = false;
   445                     emit(out, "var @1 = @2;",
   446                          lmapper.setL(indx), smapper.popL());
   447                     break;
   448                 }
   449                 case opc_fstore: {
   450                     ++i;
   451                     final int indx = wide ? readUShort(byteCodes, i++)
   452                                           : readUByte(byteCodes, i);
   453                     wide = false;
   454                     emit(out, "var @1 = @2;",
   455                          lmapper.setF(indx), smapper.popF());
   456                     break;
   457                 }
   458                 case opc_dstore: {
   459                     ++i;
   460                     final int indx = wide ? readUShort(byteCodes, i++)
   461                                           : readUByte(byteCodes, i);
   462                     wide = false;
   463                     emit(out, "var @1 = @2;",
   464                          lmapper.setD(indx), smapper.popD());
   465                     break;
   466                 }
   467                 case opc_astore: {
   468                     ++i;
   469                     final int indx = wide ? readUShort(byteCodes, i++)
   470                                           : readUByte(byteCodes, i);
   471                     wide = false;
   472                     emit(out, "var @1 = @2;",
   473                          lmapper.setA(indx), smapper.popA());
   474                     break;
   475                 }
   476                 case opc_astore_0:
   477                     emit(out, "var @1 = @2;", lmapper.setA(0), smapper.popA());
   478                     break;
   479                 case opc_istore_0:
   480                     emit(out, "var @1 = @2;", lmapper.setI(0), smapper.popI());
   481                     break;
   482                 case opc_lstore_0:
   483                     emit(out, "var @1 = @2;", lmapper.setL(0), smapper.popL());
   484                     break;
   485                 case opc_fstore_0:
   486                     emit(out, "var @1 = @2;", lmapper.setF(0), smapper.popF());
   487                     break;
   488                 case opc_dstore_0:
   489                     emit(out, "var @1 = @2;", lmapper.setD(0), smapper.popD());
   490                     break;
   491                 case opc_astore_1:
   492                     emit(out, "var @1 = @2;", lmapper.setA(1), smapper.popA());
   493                     break;
   494                 case opc_istore_1:
   495                     emit(out, "var @1 = @2;", lmapper.setI(1), smapper.popI());
   496                     break;
   497                 case opc_lstore_1:
   498                     emit(out, "var @1 = @2;", lmapper.setL(1), smapper.popL());
   499                     break;
   500                 case opc_fstore_1:
   501                     emit(out, "var @1 = @2;", lmapper.setF(1), smapper.popF());
   502                     break;
   503                 case opc_dstore_1:
   504                     emit(out, "var @1 = @2;", lmapper.setD(1), smapper.popD());
   505                     break;
   506                 case opc_astore_2:
   507                     emit(out, "var @1 = @2;", lmapper.setA(2), smapper.popA());
   508                     break;
   509                 case opc_istore_2:
   510                     emit(out, "var @1 = @2;", lmapper.setI(2), smapper.popI());
   511                     break;
   512                 case opc_lstore_2:
   513                     emit(out, "var @1 = @2;", lmapper.setL(2), smapper.popL());
   514                     break;
   515                 case opc_fstore_2:
   516                     emit(out, "var @1 = @2;", lmapper.setF(2), smapper.popF());
   517                     break;
   518                 case opc_dstore_2:
   519                     emit(out, "var @1 = @2;", lmapper.setD(2), smapper.popD());
   520                     break;
   521                 case opc_astore_3:
   522                     emit(out, "var @1 = @2;", lmapper.setA(3), smapper.popA());
   523                     break;
   524                 case opc_istore_3:
   525                     emit(out, "var @1 = @2;", lmapper.setI(3), smapper.popI());
   526                     break;
   527                 case opc_lstore_3:
   528                     emit(out, "var @1 = @2;", lmapper.setL(3), smapper.popL());
   529                     break;
   530                 case opc_fstore_3:
   531                     emit(out, "var @1 = @2;", lmapper.setF(3), smapper.popF());
   532                     break;
   533                 case opc_dstore_3:
   534                     emit(out, "var @1 = @2;", lmapper.setD(3), smapper.popD());
   535                     break;
   536                 case opc_iadd:
   537                     emit(out, "@1 = @1.add32(@2);", smapper.getI(1), smapper.popI());
   538                     break;
   539                 case opc_ladd:
   540                     emit(out, "@1 = @1.add64(@2);", smapper.getL(1), smapper.popL());
   541                     break;
   542                 case opc_fadd:
   543                     emit(out, "@1 += @2;", smapper.getF(1), smapper.popF());
   544                     break;
   545                 case opc_dadd:
   546                     emit(out, "@1 += @2;", smapper.getD(1), smapper.popD());
   547                     break;
   548                 case opc_isub:
   549                     emit(out, "@1 = @1.sub32(@2);", smapper.getI(1), smapper.popI());
   550                     break;
   551                 case opc_lsub:
   552                     emit(out, "@1 = @1.sub64(@2);", smapper.getL(1), smapper.popL());
   553                     break;
   554                 case opc_fsub:
   555                     emit(out, "@1 -= @2;", smapper.getF(1), smapper.popF());
   556                     break;
   557                 case opc_dsub:
   558                     emit(out, "@1 -= @2;", smapper.getD(1), smapper.popD());
   559                     break;
   560                 case opc_imul:
   561                     emit(out, "@1 = @1.mul32(@2);", smapper.getI(1), smapper.popI());
   562                     break;
   563                 case opc_lmul:
   564                     emit(out, "@1 = @1.mul64(@2);", smapper.getL(1), smapper.popL());
   565                     break;
   566                 case opc_fmul:
   567                     emit(out, "@1 *= @2;", smapper.getF(1), smapper.popF());
   568                     break;
   569                 case opc_dmul:
   570                     emit(out, "@1 *= @2;", smapper.getD(1), smapper.popD());
   571                     break;
   572                 case opc_idiv:
   573                     emit(out, "@1 = @1.div32(@2);",
   574                          smapper.getI(1), smapper.popI());
   575                     break;
   576                 case opc_ldiv:
   577                     emit(out, "@1 = @1.div64(@2);",
   578                          smapper.getL(1), smapper.popL());
   579                     break;
   580                 case opc_fdiv:
   581                     emit(out, "@1 /= @2;", smapper.getF(1), smapper.popF());
   582                     break;
   583                 case opc_ddiv:
   584                     emit(out, "@1 /= @2;", smapper.getD(1), smapper.popD());
   585                     break;
   586                 case opc_irem:
   587                     emit(out, "@1 = @1.mod32(@2);",
   588                          smapper.getI(1), smapper.popI());
   589                     break;
   590                 case opc_lrem:
   591                     emit(out, "@1 = @1.mod64(@2);",
   592                          smapper.getL(1), smapper.popL());
   593                     break;
   594                 case opc_frem:
   595                     emit(out, "@1 %= @2;", smapper.getF(1), smapper.popF());
   596                     break;
   597                 case opc_drem:
   598                     emit(out, "@1 %= @2;", smapper.getD(1), smapper.popD());
   599                     break;
   600                 case opc_iand:
   601                     emit(out, "@1 &= @2;", smapper.getI(1), smapper.popI());
   602                     break;
   603                 case opc_land:
   604                     emit(out, "@1 = @1.and64(@2);", smapper.getL(1), smapper.popL());
   605                     break;
   606                 case opc_ior:
   607                     emit(out, "@1 |= @2;", smapper.getI(1), smapper.popI());
   608                     break;
   609                 case opc_lor:
   610                     emit(out, "@1 = @1.or64(@2);", smapper.getL(1), smapper.popL());
   611                     break;
   612                 case opc_ixor:
   613                     emit(out, "@1 ^= @2;", smapper.getI(1), smapper.popI());
   614                     break;
   615                 case opc_lxor:
   616                     emit(out, "@1 = @1.xor64(@2);", smapper.getL(1), smapper.popL());
   617                     break;
   618                 case opc_ineg:
   619                     emit(out, "@1 = @1.neg32();", smapper.getI(0));
   620                     break;
   621                 case opc_lneg:
   622                     emit(out, "@1 = @1.neg64();", smapper.getL(0));
   623                     break;
   624                 case opc_fneg:
   625                     emit(out, "@1 = -@1;", smapper.getF(0));
   626                     break;
   627                 case opc_dneg:
   628                     emit(out, "@1 = -@1;", smapper.getD(0));
   629                     break;
   630                 case opc_ishl:
   631                     emit(out, "@1 <<= @2;", smapper.getI(1), smapper.popI());
   632                     break;
   633                 case opc_lshl:
   634                     emit(out, "@1 = @1.shl64(@2);", smapper.getL(1), smapper.popI());
   635                     break;
   636                 case opc_ishr:
   637                     emit(out, "@1 >>= @2;", smapper.getI(1), smapper.popI());
   638                     break;
   639                 case opc_lshr:
   640                     emit(out, "@1 = @1.shr64(@2);", smapper.getL(1), smapper.popI());
   641                     break;
   642                 case opc_iushr:
   643                     emit(out, "@1 >>>= @2;", smapper.getI(1), smapper.popI());
   644                     break;
   645                 case opc_lushr:
   646                     emit(out, "@1 = @1.ushr64(@2);", smapper.getL(1), smapper.popI());
   647                     break;
   648                 case opc_iinc: {
   649                     ++i;
   650                     final int varIndx = wide ? readUShort(byteCodes, i++)
   651                                              : readUByte(byteCodes, i);
   652                     ++i;
   653                     final int incrBy = wide ? readShort(byteCodes, i++)
   654                                             : byteCodes[i];
   655                     wide = false;
   656                     if (incrBy == 1) {
   657                         emit(out, "@1++;", lmapper.getI(varIndx));
   658                     } else {
   659                         emit(out, "@1 += @2;",
   660                              lmapper.getI(varIndx),
   661                              Integer.toString(incrBy));
   662                     }
   663                     break;
   664                 }
   665                 case opc_return:
   666                     emit(out, "return;");
   667                     break;
   668                 case opc_ireturn:
   669                     emit(out, "return @1;", smapper.popI());
   670                     break;
   671                 case opc_lreturn:
   672                     emit(out, "return @1;", smapper.popL());
   673                     break;
   674                 case opc_freturn:
   675                     emit(out, "return @1;", smapper.popF());
   676                     break;
   677                 case opc_dreturn:
   678                     emit(out, "return @1;", smapper.popD());
   679                     break;
   680                 case opc_areturn:
   681                     emit(out, "return @1;", smapper.popA());
   682                     break;
   683                 case opc_i2l:
   684                     emit(out, "var @2 = @1;", smapper.popI(), smapper.pushL());
   685                     break;
   686                 case opc_i2f:
   687                     emit(out, "var @2 = @1;", smapper.popI(), smapper.pushF());
   688                     break;
   689                 case opc_i2d:
   690                     emit(out, "var @2 = @1;", smapper.popI(), smapper.pushD());
   691                     break;
   692                 case opc_l2i:
   693                     emit(out, "var @2 = @1.toInt32();", smapper.popL(), smapper.pushI());
   694                     break;
   695                     // max int check?
   696                 case opc_l2f:
   697                     emit(out, "var @2 = @1.toFP();", smapper.popL(), smapper.pushF());
   698                     break;
   699                 case opc_l2d:
   700                     emit(out, "var @2 = @1.toFP();", smapper.popL(), smapper.pushD());
   701                     break;
   702                 case opc_f2d:
   703                     emit(out, "var @2 = @1;", smapper.popF(), smapper.pushD());
   704                     break;
   705                 case opc_d2f:
   706                     emit(out, "var @2 = @1;", smapper.popD(), smapper.pushF());
   707                     break;
   708                 case opc_f2i:
   709                     emit(out, "var @2 = @1.toInt32();",
   710                          smapper.popF(), smapper.pushI());
   711                     break;
   712                 case opc_f2l:
   713                     emit(out, "var @2 = @1.toLong();",
   714                          smapper.popF(), smapper.pushL());
   715                     break;
   716                 case opc_d2i:
   717                     emit(out, "var @2 = @1.toInt32();",
   718                          smapper.popD(), smapper.pushI());
   719                     break;
   720                 case opc_d2l:
   721                     emit(out, "var @2 = @1.toLong();",
   722                          smapper.popD(), smapper.pushL());
   723                     break;
   724                 case opc_i2b:
   725                     emit(out, "var @1 = @1.toInt8();", smapper.getI(0));
   726                     break;
   727                 case opc_i2c:
   728                     out.append("{ /* number conversion */ }");
   729                     break;
   730                 case opc_i2s:
   731                     emit(out, "var @1 = @1.toInt16();", smapper.getI(0));
   732                     break;
   733                 case opc_aconst_null:
   734                     emit(out, "var @1 = null;", smapper.pushA());
   735                     break;
   736                 case opc_iconst_m1:
   737                     emit(out, "var @1 = -1;", smapper.pushI());
   738                     break;
   739                 case opc_iconst_0:
   740                     emit(out, "var @1 = 0;", smapper.pushI());
   741                     break;
   742                 case opc_dconst_0:
   743                     emit(out, "var @1 = 0;", smapper.pushD());
   744                     break;
   745                 case opc_lconst_0:
   746                     emit(out, "var @1 = 0;", smapper.pushL());
   747                     break;
   748                 case opc_fconst_0:
   749                     emit(out, "var @1 = 0;", smapper.pushF());
   750                     break;
   751                 case opc_iconst_1:
   752                     emit(out, "var @1 = 1;", smapper.pushI());
   753                     break;
   754                 case opc_lconst_1:
   755                     emit(out, "var @1 = 1;", smapper.pushL());
   756                     break;
   757                 case opc_fconst_1:
   758                     emit(out, "var @1 = 1;", smapper.pushF());
   759                     break;
   760                 case opc_dconst_1:
   761                     emit(out, "var @1 = 1;", smapper.pushD());
   762                     break;
   763                 case opc_iconst_2:
   764                     emit(out, "var @1 = 2;", smapper.pushI());
   765                     break;
   766                 case opc_fconst_2:
   767                     emit(out, "var @1 = 2;", smapper.pushF());
   768                     break;
   769                 case opc_iconst_3:
   770                     emit(out, "var @1 = 3;", smapper.pushI());
   771                     break;
   772                 case opc_iconst_4:
   773                     emit(out, "var @1 = 4;", smapper.pushI());
   774                     break;
   775                 case opc_iconst_5:
   776                     emit(out, "var @1 = 5;", smapper.pushI());
   777                     break;
   778                 case opc_ldc: {
   779                     int indx = readUByte(byteCodes, ++i);
   780                     String v = encodeConstant(indx);
   781                     int type = VarType.fromConstantType(jc.getTag(indx));
   782                     emit(out, "var @1 = @2;", smapper.pushT(type), v);
   783                     break;
   784                 }
   785                 case opc_ldc_w:
   786                 case opc_ldc2_w: {
   787                     int indx = readIntArg(byteCodes, i);
   788                     i += 2;
   789                     String v = encodeConstant(indx);
   790                     int type = VarType.fromConstantType(jc.getTag(indx));
   791                     if (type == VarType.LONG) {
   792                         final Long lv = new Long(v);
   793                         final int low = (int)(lv.longValue() & 0xFFFFFFFF);
   794                         final int hi = (int)(lv.longValue() >> 32);
   795                         emit(out, "var @1 = 0x@3.next32(0x@2);", smapper.pushL(), 
   796                                 Integer.toHexString(low), Integer.toHexString(hi));
   797                     } else {
   798                         emit(out, "var @1 = @2;", smapper.pushT(type), v);
   799                     }
   800                     break;
   801                 }
   802                 case opc_lcmp:
   803                     emit(out, "var @3 = @2.compare64(@1);",
   804                          smapper.popL(), smapper.popL(), smapper.pushI());
   805                     break;
   806                 case opc_fcmpl:
   807                 case opc_fcmpg:
   808                     emit(out, "var @3 = (@2 == @1) ? 0 : ((@2 < @1) ? -1 : 1);",
   809                          smapper.popF(), smapper.popF(), smapper.pushI());
   810                     break;
   811                 case opc_dcmpl:
   812                 case opc_dcmpg:
   813                     emit(out, "var @3 = (@2 == @1) ? 0 : ((@2 < @1) ? -1 : 1);",
   814                          smapper.popD(), smapper.popD(), smapper.pushI());
   815                     break;
   816                 case opc_if_acmpeq:
   817                     i = generateIf(byteCodes, i, smapper.popA(), smapper.popA(),
   818                                    "===", topMostLabel);
   819                     break;
   820                 case opc_if_acmpne:
   821                     i = generateIf(byteCodes, i, smapper.popA(), smapper.popA(),
   822                                    "!==", topMostLabel);
   823                     break;
   824                 case opc_if_icmpeq:
   825                     i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
   826                                    "==", topMostLabel);
   827                     break;
   828                 case opc_ifeq: {
   829                     int indx = i + readIntArg(byteCodes, i);
   830                     emitIf(out, "if (@1 == 0) ",
   831                          smapper.popI(), i, indx, topMostLabel);
   832                     i += 2;
   833                     break;
   834                 }
   835                 case opc_ifne: {
   836                     int indx = i + readIntArg(byteCodes, i);
   837                     emitIf(out, "if (@1 != 0) ",
   838                          smapper.popI(), i, indx, topMostLabel);
   839                     i += 2;
   840                     break;
   841                 }
   842                 case opc_iflt: {
   843                     int indx = i + readIntArg(byteCodes, i);
   844                     emitIf(out, "if (@1 < 0) ",
   845                          smapper.popI(), i, indx, topMostLabel);
   846                     i += 2;
   847                     break;
   848                 }
   849                 case opc_ifle: {
   850                     int indx = i + readIntArg(byteCodes, i);
   851                     emitIf(out, "if (@1 <= 0) ",
   852                          smapper.popI(), i, indx, topMostLabel);
   853                     i += 2;
   854                     break;
   855                 }
   856                 case opc_ifgt: {
   857                     int indx = i + readIntArg(byteCodes, i);
   858                     emitIf(out, "if (@1 > 0) ",
   859                          smapper.popI(), i, indx, topMostLabel);
   860                     i += 2;
   861                     break;
   862                 }
   863                 case opc_ifge: {
   864                     int indx = i + readIntArg(byteCodes, i);
   865                     emitIf(out, "if (@1 >= 0) ",
   866                          smapper.popI(), i, indx, topMostLabel);
   867                     i += 2;
   868                     break;
   869                 }
   870                 case opc_ifnonnull: {
   871                     int indx = i + readIntArg(byteCodes, i);
   872                     emitIf(out, "if (@1 !== null) ",
   873                          smapper.popA(), i, indx, topMostLabel);
   874                     i += 2;
   875                     break;
   876                 }
   877                 case opc_ifnull: {
   878                     int indx = i + readIntArg(byteCodes, i);
   879                     emitIf(out, "if (@1 === null) ",
   880                          smapper.popA(), i, indx, topMostLabel);
   881                     i += 2;
   882                     break;
   883                 }
   884                 case opc_if_icmpne:
   885                     i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
   886                                    "!=", topMostLabel);
   887                     break;
   888                 case opc_if_icmplt:
   889                     i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
   890                                    "<", topMostLabel);
   891                     break;
   892                 case opc_if_icmple:
   893                     i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
   894                                    "<=", topMostLabel);
   895                     break;
   896                 case opc_if_icmpgt:
   897                     i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
   898                                    ">", topMostLabel);
   899                     break;
   900                 case opc_if_icmpge:
   901                     i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
   902                                    ">=", topMostLabel);
   903                     break;
   904                 case opc_goto: {
   905                     int indx = i + readIntArg(byteCodes, i);
   906                     goTo(out, i, indx, topMostLabel);
   907                     i += 2;
   908                     break;
   909                 }
   910                 case opc_lookupswitch: {
   911                     i = generateLookupSwitch(i, byteCodes, smapper, topMostLabel);
   912                     break;
   913                 }
   914                 case opc_tableswitch: {
   915                     i = generateTableSwitch(i, byteCodes, smapper, topMostLabel);
   916                     break;
   917                 }
   918                 case opc_invokeinterface: {
   919                     i = invokeVirtualMethod(byteCodes, i, smapper) + 2;
   920                     break;
   921                 }
   922                 case opc_invokevirtual:
   923                     i = invokeVirtualMethod(byteCodes, i, smapper);
   924                     break;
   925                 case opc_invokespecial:
   926                     i = invokeStaticMethod(byteCodes, i, smapper, false);
   927                     break;
   928                 case opc_invokestatic:
   929                     i = invokeStaticMethod(byteCodes, i, smapper, true);
   930                     break;
   931                 case opc_new: {
   932                     int indx = readIntArg(byteCodes, i);
   933                     String ci = jc.getClassName(indx);
   934                     emit(out, "var @1 = new @2;",
   935                          smapper.pushA(), accessClass(ci.replace('/', '_')));
   936                     addReference(ci);
   937                     i += 2;
   938                     break;
   939                 }
   940                 case opc_newarray:
   941                     int atype = readUByte(byteCodes, ++i);
   942                     generateNewArray(atype, smapper);
   943                     break;
   944                 case opc_anewarray: {
   945                     int type = readIntArg(byteCodes, i);
   946                     i += 2;
   947                     generateANewArray(type, smapper);
   948                     break;
   949                 }
   950                 case opc_multianewarray: {
   951                     int type = readIntArg(byteCodes, i);
   952                     i += 2;
   953                     i = generateMultiANewArray(type, byteCodes, i, smapper);
   954                     break;
   955                 }
   956                 case opc_arraylength:
   957                     emit(out, "var @2 = @1.length;",
   958                          smapper.popA(), smapper.pushI());
   959                     break;
   960                 case opc_lastore:
   961                     emit(out, "@3.at(@2, @1);",
   962                          smapper.popL(), smapper.popI(), smapper.popA());
   963                     break;
   964                 case opc_fastore:
   965                     emit(out, "@3.at(@2, @1);",
   966                          smapper.popF(), smapper.popI(), smapper.popA());
   967                     break;
   968                 case opc_dastore:
   969                     emit(out, "@3.at(@2, @1);",
   970                          smapper.popD(), smapper.popI(), smapper.popA());
   971                     break;
   972                 case opc_aastore:
   973                     emit(out, "@3.at(@2, @1);",
   974                          smapper.popA(), smapper.popI(), smapper.popA());
   975                     break;
   976                 case opc_iastore:
   977                 case opc_bastore:
   978                 case opc_castore:
   979                 case opc_sastore:
   980                     emit(out, "@3.at(@2, @1);",
   981                          smapper.popI(), smapper.popI(), smapper.popA());
   982                     break;
   983                 case opc_laload:
   984                     emit(out, "var @3 = @2.at(@1);",
   985                          smapper.popI(), smapper.popA(), smapper.pushL());
   986                     break;
   987                 case opc_faload:
   988                     emit(out, "var @3 = @2.at(@1);",
   989                          smapper.popI(), smapper.popA(), smapper.pushF());
   990                     break;
   991                 case opc_daload:
   992                     emit(out, "var @3 = @2.at(@1);",
   993                          smapper.popI(), smapper.popA(), smapper.pushD());
   994                     break;
   995                 case opc_aaload:
   996                     emit(out, "var @3 = @2.at(@1);",
   997                          smapper.popI(), smapper.popA(), smapper.pushA());
   998                     break;
   999                 case opc_iaload:
  1000                 case opc_baload:
  1001                 case opc_caload:
  1002                 case opc_saload:
  1003                     emit(out, "var @3 = @2.at(@1);",
  1004                          smapper.popI(), smapper.popA(), smapper.pushI());
  1005                     break;
  1006                 case opc_pop:
  1007                 case opc_pop2:
  1008                     smapper.pop(1);
  1009                     debug("/* pop */");
  1010                     break;
  1011                 case opc_dup: {
  1012                     final Variable v = smapper.get(0);
  1013                     emit(out, "var @1 = @2;", smapper.pushT(v.getType()), v);
  1014                     break;
  1015                 }
  1016                 case opc_dup2: {
  1017                     final Variable vi1 = smapper.get(0);
  1018 
  1019                     if (vi1.isCategory2()) {
  1020                         emit(out, "var @1 = @2;",
  1021                              smapper.pushT(vi1.getType()), vi1);
  1022                     } else {
  1023                         final Variable vi2 = smapper.get(1);
  1024                         emit(out, "var @1 = @2, @3 = @4;",
  1025                              smapper.pushT(vi2.getType()), vi2,
  1026                              smapper.pushT(vi1.getType()), vi1);
  1027                     }
  1028                     break;
  1029                 }
  1030                 case opc_dup_x1: {
  1031                     final Variable vi1 = smapper.pop();
  1032                     final Variable vi2 = smapper.pop();
  1033                     final Variable vo3 = smapper.pushT(vi1.getType());
  1034                     final Variable vo2 = smapper.pushT(vi2.getType());
  1035                     final Variable vo1 = smapper.pushT(vi1.getType());
  1036 
  1037                     emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
  1038                          vo1, vi1, vo2, vi2, vo3, vo1);
  1039                     break;
  1040                 }
  1041                 case opc_dup2_x1: {
  1042                     final Variable vi1 = smapper.pop();
  1043                     final Variable vi2 = smapper.pop();
  1044 
  1045                     if (vi1.isCategory2()) {
  1046                         final Variable vo3 = smapper.pushT(vi1.getType());
  1047                         final Variable vo2 = smapper.pushT(vi2.getType());
  1048                         final Variable vo1 = smapper.pushT(vi1.getType());
  1049 
  1050                         emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
  1051                              vo1, vi1, vo2, vi2, vo3, vo1);
  1052                     } else {
  1053                         final Variable vi3 = smapper.pop();
  1054                         final Variable vo5 = smapper.pushT(vi2.getType());
  1055                         final Variable vo4 = smapper.pushT(vi1.getType());
  1056                         final Variable vo3 = smapper.pushT(vi3.getType());
  1057                         final Variable vo2 = smapper.pushT(vi2.getType());
  1058                         final Variable vo1 = smapper.pushT(vi1.getType());
  1059 
  1060                         emit(out, "var @1 = @2, @3 = @4, @5 = @6,",
  1061                              vo1, vi1, vo2, vi2, vo3, vi3);
  1062                         emit(out, " @1 = @2, @3 = @4;",
  1063                              vo4, vo1, vo5, vo2);
  1064                     }
  1065                     break;
  1066                 }
  1067                 case opc_dup_x2: {
  1068                     final Variable vi1 = smapper.pop();
  1069                     final Variable vi2 = smapper.pop();
  1070 
  1071                     if (vi2.isCategory2()) {
  1072                         final Variable vo3 = smapper.pushT(vi1.getType());
  1073                         final Variable vo2 = smapper.pushT(vi2.getType());
  1074                         final Variable vo1 = smapper.pushT(vi1.getType());
  1075 
  1076                         emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
  1077                              vo1, vi1, vo2, vi2, vo3, vo1);
  1078                     } else {
  1079                         final Variable vi3 = smapper.pop();
  1080                         final Variable vo4 = smapper.pushT(vi1.getType());
  1081                         final Variable vo3 = smapper.pushT(vi3.getType());
  1082                         final Variable vo2 = smapper.pushT(vi2.getType());
  1083                         final Variable vo1 = smapper.pushT(vi1.getType());
  1084 
  1085                         emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8;",
  1086                              vo1, vi1, vo2, vi2, vo3, vi3, vo4, vo1);
  1087                     }
  1088                     break;
  1089                 }
  1090                 case opc_dup2_x2: {
  1091                     final Variable vi1 = smapper.pop();
  1092                     final Variable vi2 = smapper.pop();
  1093 
  1094                     if (vi1.isCategory2()) {
  1095                         if (vi2.isCategory2()) {
  1096                             final Variable vo3 = smapper.pushT(vi1.getType());
  1097                             final Variable vo2 = smapper.pushT(vi2.getType());
  1098                             final Variable vo1 = smapper.pushT(vi1.getType());
  1099 
  1100                             emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
  1101                                  vo1, vi1, vo2, vi2, vo3, vo1);
  1102                         } else {
  1103                             final Variable vi3 = smapper.pop();
  1104                             final Variable vo4 = smapper.pushT(vi1.getType());
  1105                             final Variable vo3 = smapper.pushT(vi3.getType());
  1106                             final Variable vo2 = smapper.pushT(vi2.getType());
  1107                             final Variable vo1 = smapper.pushT(vi1.getType());
  1108 
  1109                             emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8;",
  1110                                  vo1, vi1, vo2, vi2, vo3, vi3, vo4, vo1);
  1111                         }
  1112                     } else {
  1113                         final Variable vi3 = smapper.pop();
  1114 
  1115                         if (vi3.isCategory2()) {
  1116                             final Variable vo5 = smapper.pushT(vi2.getType());
  1117                             final Variable vo4 = smapper.pushT(vi1.getType());
  1118                             final Variable vo3 = smapper.pushT(vi3.getType());
  1119                             final Variable vo2 = smapper.pushT(vi2.getType());
  1120                             final Variable vo1 = smapper.pushT(vi1.getType());
  1121 
  1122                             emit(out, "var @1 = @2, @3 = @4, @5 = @6,",
  1123                                  vo1, vi1, vo2, vi2, vo3, vi3);
  1124                             emit(out, " @1 = @2, @3 = @4;",
  1125                                  vo4, vo1, vo5, vo2);
  1126                         } else {
  1127                             final Variable vi4 = smapper.pop();
  1128                             final Variable vo6 = smapper.pushT(vi2.getType());
  1129                             final Variable vo5 = smapper.pushT(vi1.getType());
  1130                             final Variable vo4 = smapper.pushT(vi4.getType());
  1131                             final Variable vo3 = smapper.pushT(vi3.getType());
  1132                             final Variable vo2 = smapper.pushT(vi2.getType());
  1133                             final Variable vo1 = smapper.pushT(vi1.getType());
  1134                             
  1135                             emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8,",
  1136                                  vo1, vi1, vo2, vi2, vo3, vi3, vo4, vi4);
  1137                             emit(out, " @1 = @2, @3 = @4;",
  1138                                  vo5, vo1, vo6, vo2);
  1139                         }
  1140                     }
  1141                     break;
  1142                 }
  1143                 case opc_swap: {
  1144                     final Variable vi1 = smapper.get(0);
  1145                     final Variable vi2 = smapper.get(1);
  1146 
  1147                     if (vi1.getType() == vi2.getType()) {
  1148                         final Variable tmp = smapper.pushT(vi1.getType());
  1149 
  1150                         emit(out, "var @1 = @2, @2 = @3, @3 = @1;",
  1151                              tmp, vi1, vi2);
  1152                         smapper.pop(1);
  1153                     } else {
  1154                         smapper.pop(2);
  1155                         smapper.pushT(vi1.getType());
  1156                         smapper.pushT(vi2.getType());
  1157                     }
  1158                     break;
  1159                 }
  1160                 case opc_bipush:
  1161                     emit(out, "var @1 = @2;",
  1162                          smapper.pushI(), Integer.toString(byteCodes[++i]));
  1163                     break;
  1164                 case opc_sipush:
  1165                     emit(out, "var @1 = @2;",
  1166                          smapper.pushI(),
  1167                          Integer.toString(readIntArg(byteCodes, i)));
  1168                     i += 2;
  1169                     break;
  1170                 case opc_getfield: {
  1171                     int indx = readIntArg(byteCodes, i);
  1172                     String[] fi = jc.getFieldInfoName(indx);
  1173                     final int type = VarType.fromFieldType(fi[2].charAt(0));
  1174                     final String mangleClass = mangleSig(fi[0]);
  1175                     final String mangleClassAccess = accessClass(mangleClass);
  1176                     emit(out, "var @2 = @4(false)._@3.call(@1);",
  1177                          smapper.popA(),
  1178                          smapper.pushT(type), fi[1], mangleClassAccess
  1179                     );
  1180                     i += 2;
  1181                     break;
  1182                 }
  1183                 case opc_putfield: {
  1184                     int indx = readIntArg(byteCodes, i);
  1185                     String[] fi = jc.getFieldInfoName(indx);
  1186                     final int type = VarType.fromFieldType(fi[2].charAt(0));
  1187                     final String mangleClass = mangleSig(fi[0]);
  1188                     final String mangleClassAccess = accessClass(mangleClass);
  1189                     emit(out, "@4(false)._@3.call(@2, @1);",
  1190                          smapper.popT(type),
  1191                          smapper.popA(), fi[1], 
  1192                          mangleClassAccess
  1193                     );
  1194                     i += 2;
  1195                     break;
  1196                 }
  1197                 case opc_getstatic: {
  1198                     int indx = readIntArg(byteCodes, i);
  1199                     String[] fi = jc.getFieldInfoName(indx);
  1200                     final int type = VarType.fromFieldType(fi[2].charAt(0));
  1201                     emit(out, "var @1 = @2(false)._@3();",
  1202                          smapper.pushT(type),
  1203                          accessClass(fi[0].replace('/', '_')), fi[1]);
  1204                     i += 2;
  1205                     addReference(fi[0]);
  1206                     break;
  1207                 }
  1208                 case opc_putstatic: {
  1209                     int indx = readIntArg(byteCodes, i);
  1210                     String[] fi = jc.getFieldInfoName(indx);
  1211                     final int type = VarType.fromFieldType(fi[2].charAt(0));
  1212                     emit(out, "@1(false)._@2(@3);",
  1213                          accessClass(fi[0].replace('/', '_')), fi[1],
  1214                          smapper.popT(type));
  1215                     i += 2;
  1216                     addReference(fi[0]);
  1217                     break;
  1218                 }
  1219                 case opc_checkcast: {
  1220                     int indx = readIntArg(byteCodes, i);
  1221                     generateCheckcast(indx, smapper);
  1222                     i += 2;
  1223                     break;
  1224                 }
  1225                 case opc_instanceof: {
  1226                     int indx = readIntArg(byteCodes, i);
  1227                     generateInstanceOf(indx, smapper);
  1228                     i += 2;
  1229                     break;
  1230                 }
  1231                 case opc_athrow: {
  1232                     final Variable v = smapper.popA();
  1233                     smapper.clear();
  1234 
  1235                     emit(out, "{ var @1 = @2; throw @2; }",
  1236                          smapper.pushA(), v);
  1237                     break;
  1238                 }
  1239 
  1240                 case opc_monitorenter: {
  1241                     out.append("/* monitor enter */");
  1242                     smapper.popA();
  1243                     break;
  1244                 }
  1245 
  1246                 case opc_monitorexit: {
  1247                     out.append("/* monitor exit */");
  1248                     smapper.popA();
  1249                     break;
  1250                 }
  1251 
  1252                 case opc_wide:
  1253                     wide = true;
  1254                     break;
  1255 
  1256                 default: {
  1257                     wide = false;
  1258                     emit(out, "throw 'unknown bytecode @1';",
  1259                          Integer.toString(c));
  1260                 }
  1261             }
  1262             if (debug(" //")) {
  1263                 generateByteCodeComment(prev, i, byteCodes);
  1264             }
  1265             out.append("\n");            
  1266         }
  1267         if (previousTrap != null) {
  1268             generateCatch(previousTrap, byteCodes.length, topMostLabel);
  1269         }
  1270         out.append("\n    }\n");
  1271         while (openBraces-- > 0) {
  1272             out.append('}');
  1273         }
  1274         out.append("\n};");
  1275     }
  1276 
  1277     private int generateIf(byte[] byteCodes, int i, final Variable v2, final Variable v1, final String test, int topMostLabel) throws IOException {
  1278         int indx = i + readIntArg(byteCodes, i);
  1279         out.append("if (").append(v1)
  1280            .append(' ').append(test).append(' ')
  1281            .append(v2).append(") ");
  1282         goTo(out, i, indx, topMostLabel);
  1283         return i + 2;
  1284     }
  1285     
  1286     private int readIntArg(byte[] byteCodes, int offsetInstruction) {
  1287         final int indxHi = byteCodes[offsetInstruction + 1] << 8;
  1288         final int indxLo = byteCodes[offsetInstruction + 2];
  1289         return (indxHi & 0xffffff00) | (indxLo & 0xff);
  1290     }
  1291     private int readInt4(byte[] byteCodes, int offset) {
  1292         final int d = byteCodes[offset + 0] << 24;
  1293         final int c = byteCodes[offset + 1] << 16;
  1294         final int b = byteCodes[offset + 2] << 8;
  1295         final int a = byteCodes[offset + 3];
  1296         return (d & 0xff000000) | (c & 0xff0000) | (b & 0xff00) | (a & 0xff);
  1297     }
  1298     private int readUByte(byte[] byteCodes, int offset) {
  1299         return byteCodes[offset] & 0xff;
  1300     }
  1301 
  1302     private int readUShort(byte[] byteCodes, int offset) {
  1303         return ((byteCodes[offset] & 0xff) << 8)
  1304                     | (byteCodes[offset + 1] & 0xff);
  1305     }
  1306 
  1307     private int readShort(byte[] byteCodes, int offset) {
  1308         return (byteCodes[offset] << 8)
  1309                     | (byteCodes[offset + 1] & 0xff);
  1310     }
  1311 
  1312     private static void countArgs(String descriptor, char[] returnType, StringBuilder sig, StringBuilder cnt) {
  1313         int i = 0;
  1314         Boolean count = null;
  1315         boolean array = false;
  1316         sig.append("__");
  1317         int firstPos = sig.length();
  1318         while (i < descriptor.length()) {
  1319             char ch = descriptor.charAt(i++);
  1320             switch (ch) {
  1321                 case '(':
  1322                     count = true;
  1323                     continue;
  1324                 case ')':
  1325                     count = false;
  1326                     continue;
  1327                 case 'B': 
  1328                 case 'C': 
  1329                 case 'D': 
  1330                 case 'F': 
  1331                 case 'I': 
  1332                 case 'J': 
  1333                 case 'S': 
  1334                 case 'Z': 
  1335                     if (count) {
  1336                         if (array) {
  1337                             sig.append("_3");
  1338                         }
  1339                         sig.append(ch);
  1340                         if (ch == 'J' || ch == 'D') {
  1341                             cnt.append('1');
  1342                         } else {
  1343                             cnt.append('0');
  1344                         }
  1345                     } else {
  1346                         sig.insert(firstPos, ch);
  1347                         if (array) {
  1348                             returnType[0] = '[';
  1349                             sig.insert(firstPos, "_3");
  1350                         } else {
  1351                             returnType[0] = ch;
  1352                         }
  1353                     }
  1354                     array = false;
  1355                     continue;
  1356                 case 'V': 
  1357                     assert !count;
  1358                     returnType[0] = 'V';
  1359                     sig.insert(firstPos, 'V');
  1360                     continue;
  1361                 case 'L':
  1362                     int next = descriptor.indexOf(';', i);
  1363                     String realSig = mangleSig(descriptor, i - 1, next + 1);
  1364                     if (count) {
  1365                         if (array) {
  1366                             sig.append("_3");
  1367                         }
  1368                         sig.append(realSig);
  1369                         cnt.append('0');
  1370                     } else {
  1371                         sig.insert(firstPos, realSig);
  1372                         if (array) {
  1373                             sig.insert(firstPos, "_3");
  1374                         }
  1375                         returnType[0] = 'L';
  1376                     }
  1377                     i = next + 1;
  1378                     array = false;
  1379                     continue;
  1380                 case '[':
  1381                     array = true;
  1382                     continue;
  1383                 default:
  1384                     throw new IllegalStateException("Invalid char: " + ch);
  1385             }
  1386         }
  1387     }
  1388     
  1389     static String mangleSig(String sig) {
  1390         return mangleSig(sig, 0, sig.length());
  1391     }
  1392     
  1393     private static String mangleSig(String txt, int first, int last) {
  1394         StringBuilder sb = new StringBuilder();
  1395         for (int i = first; i < last; i++) {
  1396             final char ch = txt.charAt(i);
  1397             switch (ch) {
  1398                 case '/': sb.append('_'); break;
  1399                 case '_': sb.append("_1"); break;
  1400                 case ';': sb.append("_2"); break;
  1401                 case '[': sb.append("_3"); break;
  1402                 default: sb.append(ch); break;
  1403             }
  1404         }
  1405         return sb.toString();
  1406     }
  1407 
  1408     private static String findMethodName(MethodData m, StringBuilder cnt) {
  1409         StringBuilder name = new StringBuilder();
  1410         if ("<init>".equals(m.getName())) { // NOI18N
  1411             name.append("cons"); // NOI18N
  1412         } else if ("<clinit>".equals(m.getName())) { // NOI18N
  1413             name.append("class"); // NOI18N
  1414         } else {
  1415             name.append(m.getName());
  1416         } 
  1417         
  1418         countArgs(m.getInternalSig(), new char[1], name, cnt);
  1419         return name.toString();
  1420     }
  1421 
  1422     static String findMethodName(String[] mi, StringBuilder cnt, char[] returnType) {
  1423         StringBuilder name = new StringBuilder();
  1424         String descr = mi[2];//mi.getDescriptor();
  1425         String nm= mi[1];
  1426         if ("<init>".equals(nm)) { // NOI18N
  1427             name.append("cons"); // NOI18N
  1428         } else {
  1429             name.append(nm);
  1430         }
  1431         countArgs(descr, returnType, name, cnt);
  1432         return name.toString();
  1433     }
  1434 
  1435     private int invokeStaticMethod(byte[] byteCodes, int i, final StackMapper mapper, boolean isStatic)
  1436     throws IOException {
  1437         int methodIndex = readIntArg(byteCodes, i);
  1438         String[] mi = jc.getFieldInfoName(methodIndex);
  1439         char[] returnType = { 'V' };
  1440         StringBuilder cnt = new StringBuilder();
  1441         String mn = findMethodName(mi, cnt, returnType);
  1442 
  1443         final int numArguments = isStatic ? cnt.length() : cnt.length() + 1;
  1444         final Variable[] vars = new Variable[numArguments];
  1445 
  1446         for (int j = numArguments - 1; j >= 0; --j) {
  1447             vars[j] = mapper.pop();
  1448         }
  1449 
  1450         if (returnType[0] != 'V') {
  1451             out.append("var ")
  1452                .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
  1453                .append(" = ");
  1454         }
  1455 
  1456         final String in = mi[0];
  1457         out.append(accessClass(in.replace('/', '_')));
  1458         out.append("(false).");
  1459         if (mn.startsWith("cons_")) {
  1460             out.append("constructor.");
  1461         }
  1462         out.append(mn);
  1463         if (isStatic) {
  1464             out.append('(');
  1465         } else {
  1466             out.append(".call(");
  1467         }
  1468         if (numArguments > 0) {
  1469             out.append(vars[0]);
  1470             for (int j = 1; j < numArguments; ++j) {
  1471                 out.append(", ");
  1472                 out.append(vars[j]);
  1473             }
  1474         }
  1475         out.append(");");
  1476         i += 2;
  1477         addReference(in);
  1478         return i;
  1479     }
  1480     private int invokeVirtualMethod(byte[] byteCodes, int i, final StackMapper mapper)
  1481     throws IOException {
  1482         int methodIndex = readIntArg(byteCodes, i);
  1483         String[] mi = jc.getFieldInfoName(methodIndex);
  1484         char[] returnType = { 'V' };
  1485         StringBuilder cnt = new StringBuilder();
  1486         String mn = findMethodName(mi, cnt, returnType);
  1487 
  1488         final int numArguments = cnt.length() + 1;
  1489         final Variable[] vars = new Variable[numArguments];
  1490 
  1491         for (int j = numArguments - 1; j >= 0; --j) {
  1492             vars[j] = mapper.pop();
  1493         }
  1494 
  1495         if (returnType[0] != 'V') {
  1496             out.append("var ")
  1497                .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
  1498                .append(" = ");
  1499         }
  1500 
  1501         out.append(vars[0]).append('.');
  1502         out.append(mn);
  1503         out.append('(');
  1504         String sep = "";
  1505         for (int j = 1; j < numArguments; ++j) {
  1506             out.append(sep);
  1507             out.append(vars[j]);
  1508             sep = ", ";
  1509         }
  1510         out.append(");");
  1511         i += 2;
  1512         return i;
  1513     }
  1514 
  1515     private void addReference(String cn) throws IOException {
  1516         if (requireReference(cn)) {
  1517             debug(" /* needs " + cn + " */");
  1518         }
  1519     }
  1520 
  1521     private void outType(String d, StringBuilder out) {
  1522         int arr = 0;
  1523         while (d.charAt(0) == '[') {
  1524             out.append('A');
  1525             d = d.substring(1);
  1526         }
  1527         if (d.charAt(0) == 'L') {
  1528             assert d.charAt(d.length() - 1) == ';';
  1529             out.append(d.replace('/', '_').substring(0, d.length() - 1));
  1530         } else {
  1531             out.append(d);
  1532         }
  1533     }
  1534 
  1535     private String encodeConstant(int entryIndex) throws IOException {
  1536         String[] classRef = { null };
  1537         String s = jc.stringValue(entryIndex, classRef);
  1538         if (classRef[0] != null) {
  1539             if (classRef[0].startsWith("[")) {
  1540                 s = accessClass("java_lang_Class") + "(false).forName__Ljava_lang_Class_2Ljava_lang_String_2('" + classRef[0] + "');";
  1541             } else {
  1542                 addReference(classRef[0]);
  1543                 s = accessClass(s.replace('/', '_')) + "(false).constructor.$class";
  1544             }
  1545         }
  1546         return s;
  1547     }
  1548 
  1549     private String javaScriptBody(String prefix, MethodData m, boolean isStatic) throws IOException {
  1550         byte[] arr = m.findAnnotationData(true);
  1551         if (arr == null) {
  1552             return null;
  1553         }
  1554         final String jvmType = "Lorg/apidesign/bck2brwsr/core/JavaScriptBody;";
  1555         class P extends AnnotationParser {
  1556             public P() {
  1557                 super(false, true);
  1558             }
  1559             
  1560             int cnt;
  1561             String[] args = new String[30];
  1562             String body;
  1563             
  1564             @Override
  1565             protected void visitAttr(String type, String attr, String at, String value) {
  1566                 if (type.equals(jvmType)) {
  1567                     if ("body".equals(attr)) {
  1568                         body = value;
  1569                     } else if ("args".equals(attr)) {
  1570                         args[cnt++] = value;
  1571                     } else {
  1572                         throw new IllegalArgumentException(attr);
  1573                     }
  1574                 }
  1575             }
  1576         }
  1577         P p = new P();
  1578         p.parse(arr, jc);
  1579         if (p.body == null) {
  1580             return null;
  1581         }
  1582         StringBuilder cnt = new StringBuilder();
  1583         final String mn = findMethodName(m, cnt);
  1584         out.append(prefix).append(mn);
  1585         out.append(" = function(");
  1586         String space = "";
  1587         int index = 0;
  1588         for (int i = 0; i < cnt.length(); i++) {
  1589             out.append(space);
  1590             space = outputArg(out, p.args, index);
  1591             index++;
  1592         }
  1593         out.append(") {").append("\n");
  1594         out.append(p.body);
  1595         out.append("\n}\n");
  1596         return mn;
  1597     }
  1598     private static String className(ClassData jc) {
  1599         //return jc.getName().getInternalName().replace('/', '_');
  1600         return jc.getClassName().replace('/', '_');
  1601     }
  1602     
  1603     private static String[] findAnnotation(
  1604         byte[] arr, ClassData cd, final String className, 
  1605         final String... attrNames
  1606     ) throws IOException {
  1607         if (arr == null) {
  1608             return null;
  1609         }
  1610         final String[] values = new String[attrNames.length];
  1611         final boolean[] found = { false };
  1612         final String jvmType = "L" + className.replace('.', '/') + ";";
  1613         AnnotationParser ap = new AnnotationParser(false, true) {
  1614             @Override
  1615             protected void visitAttr(String type, String attr, String at, String value) {
  1616                 if (type.equals(jvmType)) {
  1617                     found[0] = true;
  1618                     for (int i = 0; i < attrNames.length; i++) {
  1619                         if (attrNames[i].equals(attr)) {
  1620                             values[i] = value;
  1621                         }
  1622                     }
  1623                 }
  1624             }
  1625             
  1626         };
  1627         ap.parse(arr, cd);
  1628         return found[0] ? values : null;
  1629     }
  1630 
  1631     private CharSequence initField(FieldData v) {
  1632         final String is = v.getInternalSig();
  1633         if (is.length() == 1) {
  1634             switch (is.charAt(0)) {
  1635                 case 'S':
  1636                 case 'J':
  1637                 case 'B':
  1638                 case 'Z':
  1639                 case 'C':
  1640                 case 'I': return " = 0;";
  1641                 case 'F': 
  1642                 case 'D': return " = 0.0;";
  1643                 default:
  1644                     throw new IllegalStateException(is);
  1645             }
  1646         }
  1647         return " = null;";
  1648     }
  1649 
  1650     private void generateAnno(ClassData cd, final Appendable out, byte[] data) throws IOException {
  1651         AnnotationParser ap = new AnnotationParser(true, false) {
  1652             int[] cnt = new int[32];
  1653             int depth;
  1654             
  1655             @Override
  1656             protected void visitAnnotationStart(String attrType, boolean top) throws IOException {
  1657                 final String slashType = attrType.substring(1, attrType.length() - 1);
  1658                 requireReference(slashType);
  1659                 
  1660                 if (cnt[depth]++ > 0) {
  1661                     out.append(",");
  1662                 }
  1663                 if (top) {
  1664                     out.append('"').append(attrType).append("\" : ");
  1665                 }
  1666                 out.append("{\n");
  1667                 cnt[++depth] = 0;
  1668             }
  1669 
  1670             @Override
  1671             protected void visitAnnotationEnd(String type, boolean top) throws IOException {
  1672                 out.append("\n}\n");
  1673                 depth--;
  1674             }
  1675 
  1676             @Override
  1677             protected void visitValueStart(String attrName, char type) throws IOException {
  1678                 if (cnt[depth]++ > 0) {
  1679                     out.append(",\n");
  1680                 }
  1681                 cnt[++depth] = 0;
  1682                 if (attrName != null) {
  1683                     out.append(attrName).append(" : ");
  1684                 }
  1685                 if (type == '[') {
  1686                     out.append("[");
  1687                 }
  1688             }
  1689 
  1690             @Override
  1691             protected void visitValueEnd(String attrName, char type) throws IOException {
  1692                 if (type == '[') {
  1693                     out.append("]");
  1694                 }
  1695                 depth--;
  1696             }
  1697             
  1698             @Override
  1699             protected void visitAttr(String type, String attr, String attrType, String value) 
  1700             throws IOException {
  1701                 if (attr == null && value == null) {
  1702                     return;
  1703                 }
  1704                 out.append(value);
  1705             }
  1706 
  1707             @Override
  1708             protected void visitEnumAttr(String type, String attr, String attrType, String value) 
  1709             throws IOException {
  1710                 final String slashType = attrType.substring(1, attrType.length() - 1);
  1711                 requireReference(slashType);
  1712                 
  1713                 out.append(accessClass(slashType.replace('/', '_')))
  1714                    .append("(false).constructor.").append(value);
  1715             }
  1716         };
  1717         ap.parse(data, cd);
  1718     }
  1719 
  1720     private static String outputArg(Appendable out, String[] args, int indx) throws IOException {
  1721         final String name = args[indx];
  1722         if (name == null) {
  1723             return "";
  1724         }
  1725         if (name.contains(",")) {
  1726             throw new IOException("Wrong parameter with ',': " + name);
  1727         }
  1728         out.append(name);
  1729         return ",";
  1730     }
  1731 
  1732     private static void emit(final Appendable out,
  1733                              final String format,
  1734                              final CharSequence... params) throws IOException {
  1735         final int length = format.length();
  1736 
  1737         int processed = 0;
  1738         int paramOffset = format.indexOf('@');
  1739         while ((paramOffset != -1) && (paramOffset < (length - 1))) {
  1740             final char paramChar = format.charAt(paramOffset + 1);
  1741             if ((paramChar >= '1') && (paramChar <= '9')) {
  1742                 final int paramIndex = paramChar - '0' - 1;
  1743 
  1744                 out.append(format, processed, paramOffset);
  1745                 out.append(params[paramIndex]);
  1746 
  1747                 ++paramOffset;
  1748                 processed = paramOffset + 1;
  1749             }
  1750 
  1751             paramOffset = format.indexOf('@', paramOffset + 1);
  1752         }
  1753 
  1754         out.append(format, processed, length);
  1755     }
  1756 
  1757     private void generateCatch(TrapData[] traps, int current, int topMostLabel) throws IOException {
  1758         out.append("} catch (e) {\n");
  1759         int finallyPC = -1;
  1760         for (TrapData e : traps) {
  1761             if (e == null) {
  1762                 break;
  1763             }
  1764             if (e.catch_cpx != 0) { //not finally
  1765                 final String classInternalName = jc.getClassName(e.catch_cpx);
  1766                 addReference(classInternalName);
  1767                 if ("java/lang/Throwable".equals(classInternalName)) {
  1768                     out.append("if (e.$instOf_java_lang_Throwable) {");
  1769                     out.append("  var stA0 = e;");
  1770                     out.append("} else {");
  1771                     out.append("  var stA0 = vm.java_lang_Throwable(true);");
  1772                     out.append("  vm.java_lang_Throwable.cons__VLjava_lang_String_2.call(stA0, e.toString());");
  1773                     out.append("}");
  1774                     goTo(out, current, e.handler_pc, topMostLabel);
  1775                 } else {
  1776                     out.append("if (e.$instOf_" + classInternalName.replace('/', '_') + ") {");
  1777                     out.append("var stA0 = e;");
  1778                     goTo(out, current, e.handler_pc, topMostLabel);
  1779                     out.append("}\n");
  1780                 }
  1781             } else {
  1782                 finallyPC = e.handler_pc;
  1783             }
  1784         }
  1785         if (finallyPC == -1) {
  1786             out.append("throw e;");
  1787         } else {
  1788             out.append("var stA0 = e;");
  1789             goTo(out, current, finallyPC, topMostLabel);
  1790         }
  1791         out.append("\n}");
  1792     }
  1793 
  1794     private static void goTo(Appendable out, int current, int to, int canBack) throws IOException {
  1795         if (to < current) {
  1796             if (canBack < to) {
  1797                 out.append("{ gt = 0; continue X_" + to + "; }");
  1798             } else {
  1799                 out.append("{ gt = " + to + "; continue X_0; }");
  1800             }
  1801         } else {
  1802             out.append("{ gt = " + to + "; break IF; }");
  1803         }
  1804     }
  1805 
  1806     private static void emitIf(
  1807         Appendable out, String pattern, Variable param, 
  1808         int current, int to, int canBack
  1809     ) throws IOException {
  1810         emit(out, pattern, param);
  1811         goTo(out, current, to, canBack);
  1812     }
  1813 
  1814     private void generateNewArray(int atype, final StackMapper smapper) throws IOException, IllegalStateException {
  1815         String jvmType;
  1816         switch (atype) {
  1817             case 4: jvmType = "[Z"; break;
  1818             case 5: jvmType = "[C"; break;
  1819             case 6: jvmType = "[F"; break;
  1820             case 7: jvmType = "[D"; break;
  1821             case 8: jvmType = "[B"; break;
  1822             case 9: jvmType = "[S"; break;
  1823             case 10: jvmType = "[I"; break;
  1824             case 11: jvmType = "[J"; break;
  1825             default: throw new IllegalStateException("Array type: " + atype);
  1826         }
  1827         emit(out, "var @2 = Array.prototype.newArray__Ljava_lang_Object_2ZLjava_lang_String_2I(true, '@3', @1);",
  1828              smapper.popI(), smapper.pushA(), jvmType);
  1829     }
  1830 
  1831     private void generateANewArray(int type, final StackMapper smapper) throws IOException {
  1832         String typeName = jc.getClassName(type);
  1833         if (typeName.startsWith("[")) {
  1834             typeName = "[" + typeName;
  1835         } else {
  1836             typeName = "[L" + typeName + ";";
  1837         }
  1838         emit(out, "var @2 = Array.prototype.newArray__Ljava_lang_Object_2ZLjava_lang_String_2I(false, '@3', @1);",
  1839              smapper.popI(), smapper.pushA(), typeName);
  1840     }
  1841 
  1842     private int generateMultiANewArray(int type, final byte[] byteCodes, int i, final StackMapper smapper) throws IOException {
  1843         String typeName = jc.getClassName(type);
  1844         int dim = readUByte(byteCodes, ++i);
  1845         StringBuilder dims = new StringBuilder();
  1846         dims.append('[');
  1847         for (int d = 0; d < dim; d++) {
  1848             if (d != 0) {
  1849                 dims.insert(1, ",");
  1850             }
  1851             dims.insert(1, smapper.popI());
  1852         }
  1853         dims.append(']');
  1854         emit(out, "var @2 = Array.prototype.multiNewArray__Ljava_lang_Object_2Ljava_lang_String_2_3II('@3', @1, 0);",
  1855              dims.toString(), smapper.pushA(), typeName);
  1856         return i;
  1857     }
  1858 
  1859     private int generateTableSwitch(int i, final byte[] byteCodes, final StackMapper smapper, int topMostLabel) throws IOException {
  1860         int table = i / 4 * 4 + 4;
  1861         int dflt = i + readInt4(byteCodes, table);
  1862         table += 4;
  1863         int low = readInt4(byteCodes, table);
  1864         table += 4;
  1865         int high = readInt4(byteCodes, table);
  1866         table += 4;
  1867         out.append("switch (").append(smapper.popI()).append(") {\n");
  1868         while (low <= high) {
  1869             int offset = i + readInt4(byteCodes, table);
  1870             table += 4;
  1871             out.append("  case " + low).append(":"); goTo(out, i, offset, topMostLabel); out.append('\n');
  1872             low++;
  1873         }
  1874         out.append("  default: ");
  1875         goTo(out, i, dflt, topMostLabel);
  1876         out.append("\n}");
  1877         i = table - 1;
  1878         return i;
  1879     }
  1880 
  1881     private int generateLookupSwitch(int i, final byte[] byteCodes, final StackMapper smapper, int topMostLabel) throws IOException {
  1882         int table = i / 4 * 4 + 4;
  1883         int dflt = i + readInt4(byteCodes, table);
  1884         table += 4;
  1885         int n = readInt4(byteCodes, table);
  1886         table += 4;
  1887         out.append("switch (").append(smapper.popI()).append(") {\n");
  1888         while (n-- > 0) {
  1889             int cnstnt = readInt4(byteCodes, table);
  1890             table += 4;
  1891             int offset = i + readInt4(byteCodes, table);
  1892             table += 4;
  1893             out.append("  case " + cnstnt).append(": "); goTo(out, i, offset, topMostLabel); out.append('\n');
  1894         }
  1895         out.append("  default: ");
  1896         goTo(out, i, dflt, topMostLabel);
  1897         out.append("\n}");
  1898         i = table - 1;
  1899         return i;
  1900     }
  1901 
  1902     private void generateInstanceOf(int indx, final StackMapper smapper) throws IOException {
  1903         final String type = jc.getClassName(indx);
  1904         if (!type.startsWith("[")) {
  1905             emit(out, "var @2 = @1 != null && @1.$instOf_@3 ? 1 : 0;",
  1906                  smapper.popA(), smapper.pushI(),
  1907                  type.replace('/', '_'));
  1908         } else {
  1909             emit(out, "var @2 = vm.java_lang_Class(false).forName__Ljava_lang_Class_2Ljava_lang_String_2('@3').isInstance__ZLjava_lang_Object_2(@1);",
  1910                 smapper.popA(), smapper.pushI(),
  1911                 type
  1912             );
  1913         }
  1914     }
  1915 
  1916     private void generateCheckcast(int indx, final StackMapper smapper) throws IOException {
  1917         final String type = jc.getClassName(indx);
  1918         if (!type.startsWith("[")) {
  1919             emit(out,
  1920                  "if (@1 !== null && !@1.$instOf_@2) throw {};",
  1921                  smapper.getA(0), type.replace('/', '_'));
  1922         } else {
  1923             emit(out, "vm.java_lang_Class(false).forName__Ljava_lang_Class_2Ljava_lang_String_2('@2').cast__Ljava_lang_Object_2Ljava_lang_Object_2(@1);",
  1924                  smapper.getA(0), type
  1925             );
  1926         }
  1927     }
  1928 
  1929     private void generateByteCodeComment(int prev, int i, final byte[] byteCodes) throws IOException {
  1930         for (int j = prev; j <= i; j++) {
  1931             out.append(" ");
  1932             final int cc = readUByte(byteCodes, j);
  1933             out.append(Integer.toString(cc));
  1934         }
  1935     }
  1936 }