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