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