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