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