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