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