vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
author Lubomir Nerad <lubomir.nerad@oracle.com>
Fri, 15 Feb 2013 16:06:12 +0100
brancharithmetic
changeset 737 b2731af0357d
parent 712 a84cb25dde74
child 753 cc0e6767259b
permissions -rw-r--r--
Fixed int & long division by zero (Bug 4668) and int negative division
     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 = @1.div32(@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 = @1.mod32(@2);",
   571                          smapper.getI(1), smapper.popI());
   572                     break;
   573                 case opc_lrem:
   574                     emit(out, "@1 = @1.mod64(@2);",
   575                          smapper.getL(1), smapper.popL());
   576                     break;
   577                 case opc_frem:
   578                     emit(out, "@1 %= @2;", smapper.getF(1), smapper.popF());
   579                     break;
   580                 case opc_drem:
   581                     emit(out, "@1 %= @2;", smapper.getD(1), smapper.popD());
   582                     break;
   583                 case opc_iand:
   584                     emit(out, "@1 &= @2;", smapper.getI(1), smapper.popI());
   585                     break;
   586                 case opc_land:
   587                     emit(out, "@1 = @1.and64(@2);", smapper.getL(1), smapper.popL());
   588                     break;
   589                 case opc_ior:
   590                     emit(out, "@1 |= @2;", smapper.getI(1), smapper.popI());
   591                     break;
   592                 case opc_lor:
   593                     emit(out, "@1 = @1.or64(@2);", smapper.getL(1), smapper.popL());
   594                     break;
   595                 case opc_ixor:
   596                     emit(out, "@1 ^= @2;", smapper.getI(1), smapper.popI());
   597                     break;
   598                 case opc_lxor:
   599                     emit(out, "@1 = @1.xor64(@2);", smapper.getL(1), smapper.popL());
   600                     break;
   601                 case opc_ineg:
   602                     emit(out, "@1 = @1.neg32();", smapper.getI(0));
   603                     break;
   604                 case opc_lneg:
   605                     emit(out, "@1 = @1.neg64();", smapper.getL(0));
   606                     break;
   607                 case opc_fneg:
   608                     emit(out, "@1 = -@1;", smapper.getF(0));
   609                     break;
   610                 case opc_dneg:
   611                     emit(out, "@1 = -@1;", smapper.getD(0));
   612                     break;
   613                 case opc_ishl:
   614                     emit(out, "@1 <<= @2;", smapper.getI(1), smapper.popI());
   615                     break;
   616                 case opc_lshl:
   617                     emit(out, "@1 = @1.shl64(@2);", smapper.getL(1), smapper.popI());
   618                     break;
   619                 case opc_ishr:
   620                     emit(out, "@1 >>= @2;", smapper.getI(1), smapper.popI());
   621                     break;
   622                 case opc_lshr:
   623                     emit(out, "@1 = @1.shr64(@2);", smapper.getL(1), smapper.popI());
   624                     break;
   625                 case opc_iushr:
   626                     emit(out, "@1 >>>= @2;", smapper.getI(1), smapper.popI());
   627                     break;
   628                 case opc_lushr:
   629                     emit(out, "@1 = @1.ushr64(@2);", smapper.getL(1), smapper.popI());
   630                     break;
   631                 case opc_iinc: {
   632                     ++i;
   633                     final int varIndx = wide ? readUShort(byteCodes, i++)
   634                                              : readUByte(byteCodes, i);
   635                     ++i;
   636                     final int incrBy = wide ? readShort(byteCodes, i++)
   637                                             : byteCodes[i];
   638                     wide = false;
   639                     if (incrBy == 1) {
   640                         emit(out, "@1++;", lmapper.getI(varIndx));
   641                     } else {
   642                         emit(out, "@1 += @2;",
   643                              lmapper.getI(varIndx),
   644                              Integer.toString(incrBy));
   645                     }
   646                     break;
   647                 }
   648                 case opc_return:
   649                     emit(out, "return;");
   650                     break;
   651                 case opc_ireturn:
   652                     emit(out, "return @1;", smapper.popI());
   653                     break;
   654                 case opc_lreturn:
   655                     emit(out, "return @1;", smapper.popL());
   656                     break;
   657                 case opc_freturn:
   658                     emit(out, "return @1;", smapper.popF());
   659                     break;
   660                 case opc_dreturn:
   661                     emit(out, "return @1;", smapper.popD());
   662                     break;
   663                 case opc_areturn:
   664                     emit(out, "return @1;", smapper.popA());
   665                     break;
   666                 case opc_i2l:
   667                     emit(out, "var @2 = @1;", smapper.popI(), smapper.pushL());
   668                     break;
   669                 case opc_i2f:
   670                     emit(out, "var @2 = @1;", smapper.popI(), smapper.pushF());
   671                     break;
   672                 case opc_i2d:
   673                     emit(out, "var @2 = @1;", smapper.popI(), smapper.pushD());
   674                     break;
   675                 case opc_l2i:
   676                     emit(out, "var @2 = @1.toInt32();", smapper.popL(), smapper.pushI());
   677                     break;
   678                     // max int check?
   679                 case opc_l2f:
   680                     emit(out, "var @2 = @1.toFP();", smapper.popL(), smapper.pushF());
   681                     break;
   682                 case opc_l2d:
   683                     emit(out, "var @2 = @1.toFP();", smapper.popL(), smapper.pushD());
   684                     break;
   685                 case opc_f2d:
   686                     emit(out, "var @2 = @1;", smapper.popF(), smapper.pushD());
   687                     break;
   688                 case opc_d2f:
   689                     emit(out, "var @2 = @1;", smapper.popD(), smapper.pushF());
   690                     break;
   691                 case opc_f2i:
   692                     emit(out, "var @2 = Math.floor(@1);",
   693                          smapper.popF(), smapper.pushI());
   694                     break;
   695                 case opc_f2l:
   696                     emit(out, "var @2 = Math.floor(@1).toLong();",
   697                          smapper.popF(), smapper.pushL());
   698                     break;
   699                 case opc_d2i:
   700                     emit(out, "var @2 = Math.floor(@1);",
   701                          smapper.popD(), smapper.pushI());
   702                     break;
   703                 case opc_d2l:
   704                     emit(out, "var @2 = Math.floor(@1).toLong();",
   705                          smapper.popD(), smapper.pushL());
   706                     break;
   707                 case opc_i2b:
   708                     emit(out, "var @1 = @1.toInt8();", smapper.getI(0));
   709                     break;
   710                 case opc_i2c:
   711                     out.append("{ /* number conversion */ }");
   712                     break;
   713                 case opc_i2s:
   714                     emit(out, "var @1 = @1.toInt16();", smapper.getI(0));
   715                     break;
   716                 case opc_aconst_null:
   717                     emit(out, "var @1 = null;", smapper.pushA());
   718                     break;
   719                 case opc_iconst_m1:
   720                     emit(out, "var @1 = -1;", smapper.pushI());
   721                     break;
   722                 case opc_iconst_0:
   723                     emit(out, "var @1 = 0;", smapper.pushI());
   724                     break;
   725                 case opc_dconst_0:
   726                     emit(out, "var @1 = 0;", smapper.pushD());
   727                     break;
   728                 case opc_lconst_0:
   729                     emit(out, "var @1 = 0;", smapper.pushL());
   730                     break;
   731                 case opc_fconst_0:
   732                     emit(out, "var @1 = 0;", smapper.pushF());
   733                     break;
   734                 case opc_iconst_1:
   735                     emit(out, "var @1 = 1;", smapper.pushI());
   736                     break;
   737                 case opc_lconst_1:
   738                     emit(out, "var @1 = 1;", smapper.pushL());
   739                     break;
   740                 case opc_fconst_1:
   741                     emit(out, "var @1 = 1;", smapper.pushF());
   742                     break;
   743                 case opc_dconst_1:
   744                     emit(out, "var @1 = 1;", smapper.pushD());
   745                     break;
   746                 case opc_iconst_2:
   747                     emit(out, "var @1 = 2;", smapper.pushI());
   748                     break;
   749                 case opc_fconst_2:
   750                     emit(out, "var @1 = 2;", smapper.pushF());
   751                     break;
   752                 case opc_iconst_3:
   753                     emit(out, "var @1 = 3;", smapper.pushI());
   754                     break;
   755                 case opc_iconst_4:
   756                     emit(out, "var @1 = 4;", smapper.pushI());
   757                     break;
   758                 case opc_iconst_5:
   759                     emit(out, "var @1 = 5;", smapper.pushI());
   760                     break;
   761                 case opc_ldc: {
   762                     int indx = readUByte(byteCodes, ++i);
   763                     String v = encodeConstant(indx);
   764                     int type = VarType.fromConstantType(jc.getTag(indx));
   765                     emit(out, "var @1 = @2;", smapper.pushT(type), v);
   766                     break;
   767                 }
   768                 case opc_ldc_w:
   769                 case opc_ldc2_w: {
   770                     int indx = readIntArg(byteCodes, i);
   771                     i += 2;
   772                     String v = encodeConstant(indx);
   773                     int type = VarType.fromConstantType(jc.getTag(indx));
   774                     if (type == VarType.LONG) {
   775                         final Long lv = new Long(v);
   776                         final int low = (int)(lv.longValue() & 0xFFFFFFFF);
   777                         final int hi = (int)(lv.longValue() >> 32);
   778                         emit(out, "var @1 = 0x@3.next32(0x@2);", smapper.pushL(), 
   779                                 Integer.toHexString(low), Integer.toHexString(hi));
   780                     } else {
   781                         emit(out, "var @1 = @2;", smapper.pushT(type), v);
   782                     }
   783                     break;
   784                 }
   785                 case opc_lcmp:
   786                     emit(out, "var @3 = @2.compare64(@1);",
   787                          smapper.popL(), smapper.popL(), smapper.pushI());
   788                     break;
   789                 case opc_fcmpl:
   790                 case opc_fcmpg:
   791                     emit(out, "var @3 = (@2 == @1) ? 0 : ((@2 < @1) ? -1 : 1);",
   792                          smapper.popF(), smapper.popF(), smapper.pushI());
   793                     break;
   794                 case opc_dcmpl:
   795                 case opc_dcmpg:
   796                     emit(out, "var @3 = (@2 == @1) ? 0 : ((@2 < @1) ? -1 : 1);",
   797                          smapper.popD(), smapper.popD(), smapper.pushI());
   798                     break;
   799                 case opc_if_acmpeq:
   800                     i = generateIf(byteCodes, i, smapper.popA(), smapper.popA(),
   801                                    "===");
   802                     break;
   803                 case opc_if_acmpne:
   804                     i = generateIf(byteCodes, i, smapper.popA(), smapper.popA(),
   805                                    "!=");
   806                     break;
   807                 case opc_if_icmpeq:
   808                     i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
   809                                    "==");
   810                     break;
   811                 case opc_ifeq: {
   812                     int indx = i + readIntArg(byteCodes, i);
   813                     emit(out, "if (@1 == 0) { gt = @2; continue; }",
   814                          smapper.popI(), Integer.toString(indx));
   815                     i += 2;
   816                     break;
   817                 }
   818                 case opc_ifne: {
   819                     int indx = i + readIntArg(byteCodes, i);
   820                     emit(out, "if (@1 != 0) { gt = @2; continue; }",
   821                          smapper.popI(), Integer.toString(indx));
   822                     i += 2;
   823                     break;
   824                 }
   825                 case opc_iflt: {
   826                     int indx = i + readIntArg(byteCodes, i);
   827                     emit(out, "if (@1 < 0) { gt = @2; continue; }",
   828                          smapper.popI(), Integer.toString(indx));
   829                     i += 2;
   830                     break;
   831                 }
   832                 case opc_ifle: {
   833                     int indx = i + readIntArg(byteCodes, i);
   834                     emit(out, "if (@1 <= 0) { gt = @2; continue; }",
   835                          smapper.popI(), Integer.toString(indx));
   836                     i += 2;
   837                     break;
   838                 }
   839                 case opc_ifgt: {
   840                     int indx = i + readIntArg(byteCodes, i);
   841                     emit(out, "if (@1 > 0) { gt = @2; continue; }",
   842                          smapper.popI(), Integer.toString(indx));
   843                     i += 2;
   844                     break;
   845                 }
   846                 case opc_ifge: {
   847                     int indx = i + readIntArg(byteCodes, i);
   848                     emit(out, "if (@1 >= 0) { gt = @2; continue; }",
   849                          smapper.popI(), Integer.toString(indx));
   850                     i += 2;
   851                     break;
   852                 }
   853                 case opc_ifnonnull: {
   854                     int indx = i + readIntArg(byteCodes, i);
   855                     emit(out, "if (@1 !== null) { gt = @2; continue; }",
   856                          smapper.popA(), Integer.toString(indx));
   857                     i += 2;
   858                     break;
   859                 }
   860                 case opc_ifnull: {
   861                     int indx = i + readIntArg(byteCodes, i);
   862                     emit(out, "if (@1 === null) { gt = @2; continue; }",
   863                          smapper.popA(), Integer.toString(indx));
   864                     i += 2;
   865                     break;
   866                 }
   867                 case opc_if_icmpne:
   868                     i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
   869                                    "!=");
   870                     break;
   871                 case opc_if_icmplt:
   872                     i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
   873                                    "<");
   874                     break;
   875                 case opc_if_icmple:
   876                     i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
   877                                    "<=");
   878                     break;
   879                 case opc_if_icmpgt:
   880                     i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
   881                                    ">");
   882                     break;
   883                 case opc_if_icmpge:
   884                     i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
   885                                    ">=");
   886                     break;
   887                 case opc_goto: {
   888                     int indx = i + readIntArg(byteCodes, i);
   889                     emit(out, "gt = @1; continue;", Integer.toString(indx));
   890                     i += 2;
   891                     break;
   892                 }
   893                 case opc_lookupswitch: {
   894                     int table = i / 4 * 4 + 4;
   895                     int dflt = i + readInt4(byteCodes, table);
   896                     table += 4;
   897                     int n = readInt4(byteCodes, table);
   898                     table += 4;
   899                     out.append("switch (").append(smapper.popI()).append(") {\n");
   900                     while (n-- > 0) {
   901                         int cnstnt = readInt4(byteCodes, table);
   902                         table += 4;
   903                         int offset = i + readInt4(byteCodes, table);
   904                         table += 4;
   905                         out.append("  case " + cnstnt).append(": gt = " + offset).append("; continue;\n");
   906                     }
   907                     out.append("  default: gt = " + dflt).append("; continue;\n}");
   908                     i = table - 1;
   909                     break;
   910                 }
   911                 case opc_tableswitch: {
   912                     int table = i / 4 * 4 + 4;
   913                     int dflt = i + readInt4(byteCodes, table);
   914                     table += 4;
   915                     int low = readInt4(byteCodes, table);
   916                     table += 4;
   917                     int high = readInt4(byteCodes, table);
   918                     table += 4;
   919                     out.append("switch (").append(smapper.popI()).append(") {\n");
   920                     while (low <= high) {
   921                         int offset = i + readInt4(byteCodes, table);
   922                         table += 4;
   923                         out.append("  case " + low).append(": gt = " + offset).append("; continue;\n");
   924                         low++;
   925                     }
   926                     out.append("  default: gt = " + dflt).append("; continue;\n}");
   927                     i = table - 1;
   928                     break;
   929                 }
   930                 case opc_invokeinterface: {
   931                     i = invokeVirtualMethod(byteCodes, i, smapper) + 2;
   932                     break;
   933                 }
   934                 case opc_invokevirtual:
   935                     i = invokeVirtualMethod(byteCodes, i, smapper);
   936                     break;
   937                 case opc_invokespecial:
   938                     i = invokeStaticMethod(byteCodes, i, smapper, false);
   939                     break;
   940                 case opc_invokestatic:
   941                     i = invokeStaticMethod(byteCodes, i, smapper, true);
   942                     break;
   943                 case opc_new: {
   944                     int indx = readIntArg(byteCodes, i);
   945                     String ci = jc.getClassName(indx);
   946                     emit(out, "var @1 = new @2;",
   947                          smapper.pushA(), accessClass(ci.replace('/', '_')));
   948                     addReference(ci);
   949                     i += 2;
   950                     break;
   951                 }
   952                 case opc_newarray:
   953                     int atype = readUByte(byteCodes, ++i);
   954                     String jvmType;
   955                     switch (atype) {
   956                         case 4: jvmType = "[Z"; break;
   957                         case 5: jvmType = "[C"; break;
   958                         case 6: jvmType = "[F"; break;
   959                         case 7: jvmType = "[D"; break;
   960                         case 8: jvmType = "[B"; break;
   961                         case 9: jvmType = "[S"; break;
   962                         case 10: jvmType = "[I"; break;
   963                         case 11: jvmType = "[J"; break;
   964                         default: throw new IllegalStateException("Array type: " + atype);
   965                     }
   966                     emit(out, "var @2 = Array.prototype.newArray__Ljava_lang_Object_2ZLjava_lang_String_2I(true, '@3', @1);",
   967                          smapper.popI(), smapper.pushA(), jvmType);
   968                     break;
   969                 case opc_anewarray: {
   970                     int type = readIntArg(byteCodes, i);
   971                     i += 2;
   972                     String typeName = jc.getClassName(type);
   973                     if (typeName.startsWith("[")) {
   974                         typeName = "[" + typeName;
   975                     } else {
   976                         typeName = "[L" + typeName + ";";
   977                     }
   978                     emit(out, "var @2 = Array.prototype.newArray__Ljava_lang_Object_2ZLjava_lang_String_2I(false, '@3', @1);",
   979                          smapper.popI(), smapper.pushA(), typeName);
   980                     break;
   981                 }
   982                 case opc_multianewarray: {
   983                     int type = readIntArg(byteCodes, i);
   984                     i += 2;
   985                     String typeName = jc.getClassName(type);
   986                     int dim = readUByte(byteCodes, ++i);
   987                     StringBuilder dims = new StringBuilder();
   988                     dims.append('[');
   989                     for (int d = 0; d < dim; d++) {
   990                         if (d != 0) {
   991                             dims.append(",");
   992                         }
   993                         dims.append(smapper.popI());
   994                     }
   995                     dims.append(']');
   996                     emit(out, "var @2 = Array.prototype.multiNewArray__Ljava_lang_Object_2Ljava_lang_String_2_3II('@3', @1, 0);",
   997                          dims.toString(), smapper.pushA(), typeName);
   998                     break;
   999                 }
  1000                 case opc_arraylength:
  1001                     emit(out, "var @2 = @1.length;",
  1002                          smapper.popA(), smapper.pushI());
  1003                     break;
  1004                 case opc_lastore:
  1005                     emit(out, "@3.at(@2, @1);",
  1006                          smapper.popL(), smapper.popI(), smapper.popA());
  1007                     break;
  1008                 case opc_fastore:
  1009                     emit(out, "@3.at(@2, @1);",
  1010                          smapper.popF(), smapper.popI(), smapper.popA());
  1011                     break;
  1012                 case opc_dastore:
  1013                     emit(out, "@3.at(@2, @1);",
  1014                          smapper.popD(), smapper.popI(), smapper.popA());
  1015                     break;
  1016                 case opc_aastore:
  1017                     emit(out, "@3.at(@2, @1);",
  1018                          smapper.popA(), smapper.popI(), smapper.popA());
  1019                     break;
  1020                 case opc_iastore:
  1021                 case opc_bastore:
  1022                 case opc_castore:
  1023                 case opc_sastore:
  1024                     emit(out, "@3.at(@2, @1);",
  1025                          smapper.popI(), smapper.popI(), smapper.popA());
  1026                     break;
  1027                 case opc_laload:
  1028                     emit(out, "var @3 = @2.at(@1);",
  1029                          smapper.popI(), smapper.popA(), smapper.pushL());
  1030                     break;
  1031                 case opc_faload:
  1032                     emit(out, "var @3 = @2.at(@1);",
  1033                          smapper.popI(), smapper.popA(), smapper.pushF());
  1034                     break;
  1035                 case opc_daload:
  1036                     emit(out, "var @3 = @2.at(@1);",
  1037                          smapper.popI(), smapper.popA(), smapper.pushD());
  1038                     break;
  1039                 case opc_aaload:
  1040                     emit(out, "var @3 = @2.at(@1);",
  1041                          smapper.popI(), smapper.popA(), smapper.pushA());
  1042                     break;
  1043                 case opc_iaload:
  1044                 case opc_baload:
  1045                 case opc_caload:
  1046                 case opc_saload:
  1047                     emit(out, "var @3 = @2.at(@1);",
  1048                          smapper.popI(), smapper.popA(), smapper.pushI());
  1049                     break;
  1050                 case opc_pop:
  1051                 case opc_pop2:
  1052                     smapper.pop(1);
  1053                     debug("/* pop */");
  1054                     break;
  1055                 case opc_dup: {
  1056                     final Variable v = smapper.get(0);
  1057                     emit(out, "var @1 = @2;", smapper.pushT(v.getType()), v);
  1058                     break;
  1059                 }
  1060                 case opc_dup2: {
  1061                     final Variable vi1 = smapper.get(0);
  1062 
  1063                     if (vi1.isCategory2()) {
  1064                         emit(out, "var @1 = @2;",
  1065                              smapper.pushT(vi1.getType()), vi1);
  1066                     } else {
  1067                         final Variable vi2 = smapper.get(1);
  1068                         emit(out, "var @1 = @2, @3 = @4;",
  1069                              smapper.pushT(vi2.getType()), vi2,
  1070                              smapper.pushT(vi1.getType()), vi1);
  1071                     }
  1072                     break;
  1073                 }
  1074                 case opc_dup_x1: {
  1075                     final Variable vi1 = smapper.pop();
  1076                     final Variable vi2 = smapper.pop();
  1077                     final Variable vo3 = smapper.pushT(vi1.getType());
  1078                     final Variable vo2 = smapper.pushT(vi2.getType());
  1079                     final Variable vo1 = smapper.pushT(vi1.getType());
  1080 
  1081                     emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
  1082                          vo1, vi1, vo2, vi2, vo3, vo1);
  1083                     break;
  1084                 }
  1085                 case opc_dup2_x1: {
  1086                     final Variable vi1 = smapper.pop();
  1087                     final Variable vi2 = smapper.pop();
  1088 
  1089                     if (vi1.isCategory2()) {
  1090                         final Variable vo3 = smapper.pushT(vi1.getType());
  1091                         final Variable vo2 = smapper.pushT(vi2.getType());
  1092                         final Variable vo1 = smapper.pushT(vi1.getType());
  1093 
  1094                         emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
  1095                              vo1, vi1, vo2, vi2, vo3, vo1);
  1096                     } else {
  1097                         final Variable vi3 = smapper.pop();
  1098                         final Variable vo5 = smapper.pushT(vi2.getType());
  1099                         final Variable vo4 = smapper.pushT(vi1.getType());
  1100                         final Variable vo3 = smapper.pushT(vi3.getType());
  1101                         final Variable vo2 = smapper.pushT(vi2.getType());
  1102                         final Variable vo1 = smapper.pushT(vi1.getType());
  1103 
  1104                         emit(out, "var @1 = @2, @3 = @4, @5 = @6,",
  1105                              vo1, vi1, vo2, vi2, vo3, vi3);
  1106                         emit(out, " @1 = @2, @3 = @4;",
  1107                              vo4, vo1, vo5, vo2);
  1108                     }
  1109                     break;
  1110                 }
  1111                 case opc_dup_x2: {
  1112                     final Variable vi1 = smapper.pop();
  1113                     final Variable vi2 = smapper.pop();
  1114 
  1115                     if (vi2.isCategory2()) {
  1116                         final Variable vo3 = smapper.pushT(vi1.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, vo1);
  1122                     } else {
  1123                         final Variable vi3 = smapper.pop();
  1124                         final Variable vo4 = smapper.pushT(vi1.getType());
  1125                         final Variable vo3 = smapper.pushT(vi3.getType());
  1126                         final Variable vo2 = smapper.pushT(vi2.getType());
  1127                         final Variable vo1 = smapper.pushT(vi1.getType());
  1128 
  1129                         emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8;",
  1130                              vo1, vi1, vo2, vi2, vo3, vi3, vo4, vo1);
  1131                     }
  1132                     break;
  1133                 }
  1134                 case opc_dup2_x2: {
  1135                     final Variable vi1 = smapper.pop();
  1136                     final Variable vi2 = smapper.pop();
  1137 
  1138                     if (vi1.isCategory2()) {
  1139                         if (vi2.isCategory2()) {
  1140                             final Variable vo3 = smapper.pushT(vi1.getType());
  1141                             final Variable vo2 = smapper.pushT(vi2.getType());
  1142                             final Variable vo1 = smapper.pushT(vi1.getType());
  1143 
  1144                             emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
  1145                                  vo1, vi1, vo2, vi2, vo3, vo1);
  1146                         } else {
  1147                             final Variable vi3 = smapper.pop();
  1148                             final Variable vo4 = smapper.pushT(vi1.getType());
  1149                             final Variable vo3 = smapper.pushT(vi3.getType());
  1150                             final Variable vo2 = smapper.pushT(vi2.getType());
  1151                             final Variable vo1 = smapper.pushT(vi1.getType());
  1152 
  1153                             emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8;",
  1154                                  vo1, vi1, vo2, vi2, vo3, vi3, vo4, vo1);
  1155                         }
  1156                     } else {
  1157                         final Variable vi3 = smapper.pop();
  1158 
  1159                         if (vi3.isCategory2()) {
  1160                             final Variable vo5 = smapper.pushT(vi2.getType());
  1161                             final Variable vo4 = smapper.pushT(vi1.getType());
  1162                             final Variable vo3 = smapper.pushT(vi3.getType());
  1163                             final Variable vo2 = smapper.pushT(vi2.getType());
  1164                             final Variable vo1 = smapper.pushT(vi1.getType());
  1165 
  1166                             emit(out, "var @1 = @2, @3 = @4, @5 = @6,",
  1167                                  vo1, vi1, vo2, vi2, vo3, vi3);
  1168                             emit(out, " @1 = @2, @3 = @4;",
  1169                                  vo4, vo1, vo5, vo2);
  1170                         } else {
  1171                             final Variable vi4 = smapper.pop();
  1172                             final Variable vo6 = smapper.pushT(vi2.getType());
  1173                             final Variable vo5 = smapper.pushT(vi1.getType());
  1174                             final Variable vo4 = smapper.pushT(vi4.getType());
  1175                             final Variable vo3 = smapper.pushT(vi3.getType());
  1176                             final Variable vo2 = smapper.pushT(vi2.getType());
  1177                             final Variable vo1 = smapper.pushT(vi1.getType());
  1178                             
  1179                             emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8,",
  1180                                  vo1, vi1, vo2, vi2, vo3, vi3, vo4, vi4);
  1181                             emit(out, " @1 = @2, @3 = @4;",
  1182                                  vo5, vo1, vo6, vo2);
  1183                         }
  1184                     }
  1185                     break;
  1186                 }
  1187                 case opc_swap: {
  1188                     final Variable vi1 = smapper.get(0);
  1189                     final Variable vi2 = smapper.get(1);
  1190 
  1191                     if (vi1.getType() == vi2.getType()) {
  1192                         final Variable tmp = smapper.pushT(vi1.getType());
  1193 
  1194                         emit(out, "var @1 = @2, @2 = @3, @3 = @1;",
  1195                              tmp, vi1, vi2);
  1196                         smapper.pop(1);
  1197                     } else {
  1198                         smapper.pop(2);
  1199                         smapper.pushT(vi1.getType());
  1200                         smapper.pushT(vi2.getType());
  1201                     }
  1202                     break;
  1203                 }
  1204                 case opc_bipush:
  1205                     emit(out, "var @1 = @2;",
  1206                          smapper.pushI(), Integer.toString(byteCodes[++i]));
  1207                     break;
  1208                 case opc_sipush:
  1209                     emit(out, "var @1 = @2;",
  1210                          smapper.pushI(),
  1211                          Integer.toString(readIntArg(byteCodes, i)));
  1212                     i += 2;
  1213                     break;
  1214                 case opc_getfield: {
  1215                     int indx = readIntArg(byteCodes, i);
  1216                     String[] fi = jc.getFieldInfoName(indx);
  1217                     final int type = VarType.fromFieldType(fi[2].charAt(0));
  1218                     final String mangleClass = mangleSig(fi[0]);
  1219                     final String mangleClassAccess = accessClass(mangleClass);
  1220                     emit(out, "var @2 = @4(false)._@3.call(@1);",
  1221                          smapper.popA(),
  1222                          smapper.pushT(type), fi[1], mangleClassAccess
  1223                     );
  1224                     i += 2;
  1225                     break;
  1226                 }
  1227                 case opc_putfield: {
  1228                     int indx = readIntArg(byteCodes, i);
  1229                     String[] fi = jc.getFieldInfoName(indx);
  1230                     final int type = VarType.fromFieldType(fi[2].charAt(0));
  1231                     final String mangleClass = mangleSig(fi[0]);
  1232                     final String mangleClassAccess = accessClass(mangleClass);
  1233                     emit(out, "@4(false)._@3.call(@2, @1);",
  1234                          smapper.popT(type),
  1235                          smapper.popA(), fi[1], 
  1236                          mangleClassAccess
  1237                     );
  1238                     i += 2;
  1239                     break;
  1240                 }
  1241                 case opc_getstatic: {
  1242                     int indx = readIntArg(byteCodes, i);
  1243                     String[] fi = jc.getFieldInfoName(indx);
  1244                     final int type = VarType.fromFieldType(fi[2].charAt(0));
  1245                     emit(out, "var @1 = @2(false).constructor.@3;",
  1246                          smapper.pushT(type),
  1247                          accessClass(fi[0].replace('/', '_')), fi[1]);
  1248                     i += 2;
  1249                     addReference(fi[0]);
  1250                     break;
  1251                 }
  1252                 case opc_putstatic: {
  1253                     int indx = readIntArg(byteCodes, i);
  1254                     String[] fi = jc.getFieldInfoName(indx);
  1255                     final int type = VarType.fromFieldType(fi[2].charAt(0));
  1256                     emit(out, "@1(false).constructor.@2 = @3;",
  1257                          accessClass(fi[0].replace('/', '_')), fi[1],
  1258                          smapper.popT(type));
  1259                     i += 2;
  1260                     addReference(fi[0]);
  1261                     break;
  1262                 }
  1263                 case opc_checkcast: {
  1264                     int indx = readIntArg(byteCodes, i);
  1265                     final String type = jc.getClassName(indx);
  1266                     if (!type.startsWith("[")) {
  1267                         emit(out,
  1268                              "if (@1 !== null && !@1.$instOf_@2) throw {};",
  1269                              smapper.getA(0), type.replace('/', '_'));
  1270                     } else {
  1271                         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);",
  1272                              smapper.getA(0), type
  1273                         );
  1274                     }
  1275                     i += 2;
  1276                     break;
  1277                 }
  1278                 case opc_instanceof: {
  1279                     int indx = readIntArg(byteCodes, i);
  1280                     final String type = jc.getClassName(indx);
  1281                     if (!type.startsWith("[")) {
  1282                         emit(out, "var @2 = @1 != null && @1.$instOf_@3 ? 1 : 0;",
  1283                              smapper.popA(), smapper.pushI(),
  1284                              type.replace('/', '_'));
  1285                     } else {
  1286                         emit(out, "var @2 = vm.java_lang_Class(false).forName__Ljava_lang_Class_2Ljava_lang_String_2('@3').isInstance__ZLjava_lang_Object_2(@1);",
  1287                             smapper.popA(), smapper.pushI(),
  1288                             type
  1289                         );
  1290                     }
  1291                     i += 2;
  1292                     break;
  1293                 }
  1294                 case opc_athrow: {
  1295                     final Variable v = smapper.popA();
  1296                     smapper.clear();
  1297 
  1298                     emit(out, "{ var @1 = @2; throw @2; }",
  1299                          smapper.pushA(), v);
  1300                     break;
  1301                 }
  1302 
  1303                 case opc_monitorenter: {
  1304                     out.append("/* monitor enter */");
  1305                     smapper.popA();
  1306                     break;
  1307                 }
  1308 
  1309                 case opc_monitorexit: {
  1310                     out.append("/* monitor exit */");
  1311                     smapper.popA();
  1312                     break;
  1313                 }
  1314 
  1315                 case opc_wide:
  1316                     wide = true;
  1317                     break;
  1318 
  1319                 default: {
  1320                     wide = false;
  1321                     emit(out, "throw 'unknown bytecode @1';",
  1322                          Integer.toString(c));
  1323                 }
  1324             }
  1325             if (debug(" //")) {
  1326                 for (int j = prev; j <= i; j++) {
  1327                     out.append(" ");
  1328                     final int cc = readUByte(byteCodes, j);
  1329                     out.append(Integer.toString(cc));
  1330                 }
  1331             }
  1332             out.append("\n");            
  1333         }
  1334         if (previousTrap != null) {
  1335             generateCatch(previousTrap);
  1336         }
  1337         out.append("  }\n");
  1338         out.append("};");
  1339     }
  1340 
  1341     private int generateIf(byte[] byteCodes, int i,
  1342                            final Variable v2, final Variable v1,
  1343                            final String test) throws IOException {
  1344         int indx = i + readIntArg(byteCodes, i);
  1345         out.append("if (").append(v1)
  1346            .append(' ').append(test).append(' ')
  1347            .append(v2).append(") { gt = " + indx)
  1348            .append("; continue; }");
  1349         return i + 2;
  1350     }
  1351 
  1352     private int readIntArg(byte[] byteCodes, int offsetInstruction) {
  1353         final int indxHi = byteCodes[offsetInstruction + 1] << 8;
  1354         final int indxLo = byteCodes[offsetInstruction + 2];
  1355         return (indxHi & 0xffffff00) | (indxLo & 0xff);
  1356     }
  1357     private int readInt4(byte[] byteCodes, int offset) {
  1358         final int d = byteCodes[offset + 0] << 24;
  1359         final int c = byteCodes[offset + 1] << 16;
  1360         final int b = byteCodes[offset + 2] << 8;
  1361         final int a = byteCodes[offset + 3];
  1362         return (d & 0xff000000) | (c & 0xff0000) | (b & 0xff00) | (a & 0xff);
  1363     }
  1364     private int readUByte(byte[] byteCodes, int offset) {
  1365         return byteCodes[offset] & 0xff;
  1366     }
  1367 
  1368     private int readUShort(byte[] byteCodes, int offset) {
  1369         return ((byteCodes[offset] & 0xff) << 8)
  1370                     | (byteCodes[offset + 1] & 0xff);
  1371     }
  1372 
  1373     private int readShort(byte[] byteCodes, int offset) {
  1374         return (byteCodes[offset] << 8)
  1375                     | (byteCodes[offset + 1] & 0xff);
  1376     }
  1377 
  1378     private static void countArgs(String descriptor, char[] returnType, StringBuilder sig, StringBuilder cnt) {
  1379         int i = 0;
  1380         Boolean count = null;
  1381         boolean array = false;
  1382         sig.append("__");
  1383         int firstPos = sig.length();
  1384         while (i < descriptor.length()) {
  1385             char ch = descriptor.charAt(i++);
  1386             switch (ch) {
  1387                 case '(':
  1388                     count = true;
  1389                     continue;
  1390                 case ')':
  1391                     count = false;
  1392                     continue;
  1393                 case 'B': 
  1394                 case 'C': 
  1395                 case 'D': 
  1396                 case 'F': 
  1397                 case 'I': 
  1398                 case 'J': 
  1399                 case 'S': 
  1400                 case 'Z': 
  1401                     if (count) {
  1402                         if (array) {
  1403                             sig.append("_3");
  1404                         }
  1405                         sig.append(ch);
  1406                         if (ch == 'J' || ch == 'D') {
  1407                             cnt.append('1');
  1408                         } else {
  1409                             cnt.append('0');
  1410                         }
  1411                     } else {
  1412                         sig.insert(firstPos, ch);
  1413                         if (array) {
  1414                             returnType[0] = '[';
  1415                             sig.insert(firstPos, "_3");
  1416                         } else {
  1417                             returnType[0] = ch;
  1418                         }
  1419                     }
  1420                     array = false;
  1421                     continue;
  1422                 case 'V': 
  1423                     assert !count;
  1424                     returnType[0] = 'V';
  1425                     sig.insert(firstPos, 'V');
  1426                     continue;
  1427                 case 'L':
  1428                     int next = descriptor.indexOf(';', i);
  1429                     String realSig = mangleSig(descriptor, i - 1, next + 1);
  1430                     if (count) {
  1431                         if (array) {
  1432                             sig.append("_3");
  1433                         }
  1434                         sig.append(realSig);
  1435                         cnt.append('0');
  1436                     } else {
  1437                         sig.insert(firstPos, realSig);
  1438                         if (array) {
  1439                             sig.insert(firstPos, "_3");
  1440                         }
  1441                         returnType[0] = 'L';
  1442                     }
  1443                     i = next + 1;
  1444                     array = false;
  1445                     continue;
  1446                 case '[':
  1447                     array = true;
  1448                     continue;
  1449                 default:
  1450                     throw new IllegalStateException("Invalid char: " + ch);
  1451             }
  1452         }
  1453     }
  1454     
  1455     static String mangleSig(String sig) {
  1456         return mangleSig(sig, 0, sig.length());
  1457     }
  1458     
  1459     private static String mangleSig(String txt, int first, int last) {
  1460         StringBuilder sb = new StringBuilder();
  1461         for (int i = first; i < last; i++) {
  1462             final char ch = txt.charAt(i);
  1463             switch (ch) {
  1464                 case '/': sb.append('_'); break;
  1465                 case '_': sb.append("_1"); break;
  1466                 case ';': sb.append("_2"); break;
  1467                 case '[': sb.append("_3"); break;
  1468                 default: sb.append(ch); break;
  1469             }
  1470         }
  1471         return sb.toString();
  1472     }
  1473 
  1474     private static String findMethodName(MethodData m, StringBuilder cnt) {
  1475         StringBuilder name = new StringBuilder();
  1476         if ("<init>".equals(m.getName())) { // NOI18N
  1477             name.append("cons"); // NOI18N
  1478         } else if ("<clinit>".equals(m.getName())) { // NOI18N
  1479             name.append("class"); // NOI18N
  1480         } else {
  1481             name.append(m.getName());
  1482         } 
  1483         
  1484         countArgs(m.getInternalSig(), new char[1], name, cnt);
  1485         return name.toString();
  1486     }
  1487 
  1488     static String findMethodName(String[] mi, StringBuilder cnt, char[] returnType) {
  1489         StringBuilder name = new StringBuilder();
  1490         String descr = mi[2];//mi.getDescriptor();
  1491         String nm= mi[1];
  1492         if ("<init>".equals(nm)) { // NOI18N
  1493             name.append("cons"); // NOI18N
  1494         } else {
  1495             name.append(nm);
  1496         }
  1497         countArgs(descr, returnType, name, cnt);
  1498         return name.toString();
  1499     }
  1500 
  1501     private int invokeStaticMethod(byte[] byteCodes, int i, final StackMapper mapper, boolean isStatic)
  1502     throws IOException {
  1503         int methodIndex = readIntArg(byteCodes, i);
  1504         String[] mi = jc.getFieldInfoName(methodIndex);
  1505         char[] returnType = { 'V' };
  1506         StringBuilder cnt = new StringBuilder();
  1507         String mn = findMethodName(mi, cnt, returnType);
  1508 
  1509         final int numArguments = isStatic ? cnt.length() : cnt.length() + 1;
  1510         final Variable[] vars = new Variable[numArguments];
  1511 
  1512         for (int j = numArguments - 1; j >= 0; --j) {
  1513             vars[j] = mapper.pop();
  1514         }
  1515 
  1516         if (returnType[0] != 'V') {
  1517             out.append("var ")
  1518                .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
  1519                .append(" = ");
  1520         }
  1521 
  1522         final String in = mi[0];
  1523         out.append(accessClass(in.replace('/', '_')));
  1524         out.append("(false).");
  1525         if (mn.startsWith("cons_")) {
  1526             out.append("constructor.");
  1527         }
  1528         out.append(mn);
  1529         if (isStatic) {
  1530             out.append('(');
  1531         } else {
  1532             out.append(".call(");
  1533         }
  1534         if (numArguments > 0) {
  1535             out.append(vars[0]);
  1536             for (int j = 1; j < numArguments; ++j) {
  1537                 out.append(", ");
  1538                 out.append(vars[j]);
  1539             }
  1540         }
  1541         out.append(");");
  1542         i += 2;
  1543         addReference(in);
  1544         return i;
  1545     }
  1546     private int invokeVirtualMethod(byte[] byteCodes, int i, final StackMapper mapper)
  1547     throws IOException {
  1548         int methodIndex = readIntArg(byteCodes, i);
  1549         String[] mi = jc.getFieldInfoName(methodIndex);
  1550         char[] returnType = { 'V' };
  1551         StringBuilder cnt = new StringBuilder();
  1552         String mn = findMethodName(mi, cnt, returnType);
  1553 
  1554         final int numArguments = cnt.length() + 1;
  1555         final Variable[] vars = new Variable[numArguments];
  1556 
  1557         for (int j = numArguments - 1; j >= 0; --j) {
  1558             vars[j] = mapper.pop();
  1559         }
  1560 
  1561         if (returnType[0] != 'V') {
  1562             out.append("var ")
  1563                .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
  1564                .append(" = ");
  1565         }
  1566 
  1567         out.append(vars[0]).append('.');
  1568         out.append(mn);
  1569         out.append('(');
  1570         String sep = "";
  1571         for (int j = 1; j < numArguments; ++j) {
  1572             out.append(sep);
  1573             out.append(vars[j]);
  1574             sep = ", ";
  1575         }
  1576         out.append(");");
  1577         i += 2;
  1578         return i;
  1579     }
  1580 
  1581     private void addReference(String cn) throws IOException {
  1582         if (requireReference(cn)) {
  1583             debug(" /* needs " + cn + " */");
  1584         }
  1585     }
  1586 
  1587     private void outType(String d, StringBuilder out) {
  1588         int arr = 0;
  1589         while (d.charAt(0) == '[') {
  1590             out.append('A');
  1591             d = d.substring(1);
  1592         }
  1593         if (d.charAt(0) == 'L') {
  1594             assert d.charAt(d.length() - 1) == ';';
  1595             out.append(d.replace('/', '_').substring(0, d.length() - 1));
  1596         } else {
  1597             out.append(d);
  1598         }
  1599     }
  1600 
  1601     private String encodeConstant(int entryIndex) throws IOException {
  1602         String[] classRef = { null };
  1603         String s = jc.stringValue(entryIndex, classRef);
  1604         if (classRef[0] != null) {
  1605             if (classRef[0].startsWith("[")) {
  1606                 s = accessClass("java_lang_Class") + "(false).forName__Ljava_lang_Class_2Ljava_lang_String_2('" + classRef[0] + "');";
  1607             } else {
  1608                 addReference(classRef[0]);
  1609                 s = accessClass(s.replace('/', '_')) + "(false).constructor.$class";
  1610             }
  1611         }
  1612         return s;
  1613     }
  1614 
  1615     private String javaScriptBody(String prefix, MethodData m, boolean isStatic) throws IOException {
  1616         byte[] arr = m.findAnnotationData(true);
  1617         if (arr == null) {
  1618             return null;
  1619         }
  1620         final String jvmType = "Lorg/apidesign/bck2brwsr/core/JavaScriptBody;";
  1621         class P extends AnnotationParser {
  1622             public P() {
  1623                 super(false);
  1624             }
  1625             
  1626             int cnt;
  1627             String[] args = new String[30];
  1628             String body;
  1629             
  1630             @Override
  1631             protected void visitAttr(String type, String attr, String at, String value) {
  1632                 if (type.equals(jvmType)) {
  1633                     if ("body".equals(attr)) {
  1634                         body = value;
  1635                     } else if ("args".equals(attr)) {
  1636                         args[cnt++] = value;
  1637                     } else {
  1638                         throw new IllegalArgumentException(attr);
  1639                     }
  1640                 }
  1641             }
  1642         }
  1643         P p = new P();
  1644         p.parse(arr, jc);
  1645         if (p.body == null) {
  1646             return null;
  1647         }
  1648         StringBuilder cnt = new StringBuilder();
  1649         final String mn = findMethodName(m, cnt);
  1650         out.append(prefix).append(mn);
  1651         out.append(" = function(");
  1652         String space = "";
  1653         int index = 0;
  1654         for (int i = 0; i < cnt.length(); i++) {
  1655             out.append(space);
  1656             space = outputArg(out, p.args, index);
  1657             index++;
  1658         }
  1659         out.append(") {").append("\n");
  1660         out.append(p.body);
  1661         out.append("\n}\n");
  1662         return mn;
  1663     }
  1664     private static String className(ClassData jc) {
  1665         //return jc.getName().getInternalName().replace('/', '_');
  1666         return jc.getClassName().replace('/', '_');
  1667     }
  1668     
  1669     private static String[] findAnnotation(
  1670         byte[] arr, ClassData cd, final String className, 
  1671         final String... attrNames
  1672     ) throws IOException {
  1673         if (arr == null) {
  1674             return null;
  1675         }
  1676         final String[] values = new String[attrNames.length];
  1677         final boolean[] found = { false };
  1678         final String jvmType = "L" + className.replace('.', '/') + ";";
  1679         AnnotationParser ap = new AnnotationParser(false) {
  1680             @Override
  1681             protected void visitAttr(String type, String attr, String at, String value) {
  1682                 if (type.equals(jvmType)) {
  1683                     found[0] = true;
  1684                     for (int i = 0; i < attrNames.length; i++) {
  1685                         if (attrNames[i].equals(attr)) {
  1686                             values[i] = value;
  1687                         }
  1688                     }
  1689                 }
  1690             }
  1691             
  1692         };
  1693         ap.parse(arr, cd);
  1694         return found[0] ? values : null;
  1695     }
  1696 
  1697     private CharSequence initField(FieldData v) {
  1698         final String is = v.getInternalSig();
  1699         if (is.length() == 1) {
  1700             switch (is.charAt(0)) {
  1701                 case 'S':
  1702                 case 'J':
  1703                 case 'B':
  1704                 case 'Z':
  1705                 case 'C':
  1706                 case 'I': return " = 0;";
  1707                 case 'F': 
  1708                 case 'D': return " = 0.0;";
  1709                 default:
  1710                     throw new IllegalStateException(is);
  1711             }
  1712         }
  1713         return " = null;";
  1714     }
  1715 
  1716     private static void generateAnno(ClassData cd, final Appendable out, byte[] data) throws IOException {
  1717         AnnotationParser ap = new AnnotationParser(true) {
  1718             int anno;
  1719             int cnt;
  1720             
  1721             @Override
  1722             protected void visitAnnotationStart(String type) throws IOException {
  1723                 if (anno++ > 0) {
  1724                     out.append(",");
  1725                 }
  1726                 out.append('"').append(type).append("\" : {\n");
  1727                 cnt = 0;
  1728             }
  1729 
  1730             @Override
  1731             protected void visitAnnotationEnd(String type) throws IOException {
  1732                 out.append("\n}\n");
  1733             }
  1734             
  1735             @Override
  1736             protected void visitAttr(String type, String attr, String attrType, String value) 
  1737             throws IOException {
  1738                 if (attr == null) {
  1739                     return;
  1740                 }
  1741                 if (cnt++ > 0) {
  1742                     out.append(",\n");
  1743                 }
  1744                 out.append(attr).append("__").append(attrType).append(" : ").append(value);
  1745             }
  1746         };
  1747         ap.parse(data, cd);
  1748     }
  1749 
  1750     private static String outputArg(Appendable out, String[] args, int indx) throws IOException {
  1751         final String name = args[indx];
  1752         if (name == null) {
  1753             return "";
  1754         }
  1755         if (name.contains(",")) {
  1756             throw new IOException("Wrong parameter with ',': " + name);
  1757         }
  1758         out.append(name);
  1759         return ",";
  1760     }
  1761 
  1762     private static void emit(final Appendable out,
  1763                              final String format,
  1764                              final CharSequence... params) throws IOException {
  1765         final int length = format.length();
  1766 
  1767         int processed = 0;
  1768         int paramOffset = format.indexOf('@');
  1769         while ((paramOffset != -1) && (paramOffset < (length - 1))) {
  1770             final char paramChar = format.charAt(paramOffset + 1);
  1771             if ((paramChar >= '1') && (paramChar <= '9')) {
  1772                 final int paramIndex = paramChar - '0' - 1;
  1773 
  1774                 out.append(format, processed, paramOffset);
  1775                 out.append(params[paramIndex]);
  1776 
  1777                 ++paramOffset;
  1778                 processed = paramOffset + 1;
  1779             }
  1780 
  1781             paramOffset = format.indexOf('@', paramOffset + 1);
  1782         }
  1783 
  1784         out.append(format, processed, length);
  1785     }
  1786 
  1787     private void generateCatch(TrapData[] traps) throws IOException {
  1788         out.append("} catch (e) {\n");
  1789         int finallyPC = -1;
  1790         for (TrapData e : traps) {
  1791             if (e == null) {
  1792                 break;
  1793             }
  1794             if (e.catch_cpx != 0) { //not finally
  1795                 final String classInternalName = jc.getClassName(e.catch_cpx);
  1796                 addReference(classInternalName);
  1797                 if ("java/lang/Throwable".equals(classInternalName)) {
  1798                     out.append("if (e.$instOf_java_lang_Throwable) {");
  1799                     out.append("  var stA0 = e;");
  1800                     out.append("} else {");
  1801                     out.append("  var stA0 = vm.java_lang_Throwable(true);");
  1802                     out.append("  vm.java_lang_Throwable.cons__VLjava_lang_String_2.call(stA0, e.toString());");
  1803                     out.append("}");
  1804                     out.append("gt=" + e.handler_pc + "; continue;");
  1805                 } else {
  1806                     out.append("if (e.$instOf_" + classInternalName.replace('/', '_') + ") {");
  1807                     out.append("gt=" + e.handler_pc + "; var stA0 = e; continue;");
  1808                     out.append("}\n");
  1809                 }
  1810             } else {
  1811                 finallyPC = e.handler_pc;
  1812             }
  1813         }
  1814         if (finallyPC == -1) {
  1815             out.append("throw e;");
  1816         } else {
  1817             out.append("gt=" + finallyPC + "; var stA0 = e; continue;");
  1818         }
  1819         out.append("\n}");
  1820     }
  1821 }