vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
author Lubomir Nerad <lubomir.nerad@oracle.com>
Fri, 18 Jan 2013 15:33:03 +0100
branchregisters
changeset 474 859cc3a0b8f0
parent 459 a2871a3fd4c5
child 488 9288ecf9657c
child 585 fe1a07c17681
permissions -rw-r--r--
Var is the Answer
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
     */
jtulach@162
    55
    protected abstract void requireScript(String resourcePath);
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:
lubomir@474
   307
                    emit(out, "var @1 = @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:
lubomir@474
   322
                    emit(out, "var @1 = @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:
lubomir@474
   337
                    emit(out, "var @1 = @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:
lubomir@474
   352
                    emit(out, "var @1 = @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:
lubomir@307
   484
                    emit(out, "@1 += @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:
lubomir@283
   521
                    emit(out, "@1 = Math.floor(@1 / @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:
lubomir@307
   576
                    emit(out, "@1 <<= @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:
lubomir@474
   621
                    emit(out, "var @2 = @1;", 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:
lubomir@474
   630
                    emit(out, "var @2 = @1;", smapper.popL(), smapper.pushI());
lubomir@281
   631
                    break;
jaroslav@3
   632
                    // max int check?
jaroslav@151
   633
                case opc_l2f:
lubomir@474
   634
                    emit(out, "var @2 = @1;", smapper.popL(), smapper.pushF());
jaroslav@272
   635
                    break;
jaroslav@151
   636
                case opc_l2d:
lubomir@474
   637
                    emit(out, "var @2 = @1;", 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:
lubomir@474
   650
                    emit(out, "var @2 = 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:
lubomir@474
   658
                    emit(out, "var @2 = 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:
lubomir@474
   683
                    emit(out, "var @1 = 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:
lubomir@474
   692
                    emit(out, "var @1 = 1;", 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));
lubomir@474
   728
                    emit(out, "var @1 = @2;", smapper.pushT(type), v);
jaroslav@8
   729
                    break;
jaroslav@8
   730
                }
jaroslav@151
   731
                case opc_lcmp:
lubomir@474
   732
                    emit(out, "var @3 = (@2 == @1) ? 0 : ((@2 < @1) ? -1 : 1);",
lubomir@307
   733
                         smapper.popL(), smapper.popL(), smapper.pushI());
lubomir@281
   734
                    break;
jaroslav@151
   735
                case opc_fcmpl:
jaroslav@151
   736
                case opc_fcmpg:
lubomir@474
   737
                    emit(out, "var @3 = (@2 == @1) ? 0 : ((@2 < @1) ? -1 : 1);",
lubomir@307
   738
                         smapper.popF(), smapper.popF(), smapper.pushI());
lubomir@281
   739
                    break;
jaroslav@151
   740
                case opc_dcmpl:
lubomir@281
   741
                case opc_dcmpg:
lubomir@474
   742
                    emit(out, "var @3 = (@2 == @1) ? 0 : ((@2 < @1) ? -1 : 1);",
lubomir@307
   743
                         smapper.popD(), smapper.popD(), smapper.pushI());
jaroslav@20
   744
                    break;
jaroslav@151
   745
                case opc_if_acmpeq:
lubomir@307
   746
                    i = generateIf(byteCodes, i, smapper.popA(), smapper.popA(),
lubomir@281
   747
                                   "===");
jaroslav@104
   748
                    break;
jaroslav@151
   749
                case opc_if_acmpne:
lubomir@307
   750
                    i = generateIf(byteCodes, i, smapper.popA(), smapper.popA(),
lubomir@281
   751
                                   "!=");
jaroslav@104
   752
                    break;
lubomir@283
   753
                case opc_if_icmpeq:
lubomir@307
   754
                    i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
lubomir@281
   755
                                   "==");
jaroslav@4
   756
                    break;
jaroslav@151
   757
                case opc_ifeq: {
jaroslav@7
   758
                    int indx = i + readIntArg(byteCodes, i);
lubomir@283
   759
                    emit(out, "if (@1 == 0) { gt = @2; continue; }",
lubomir@307
   760
                         smapper.popI(), Integer.toString(indx));
jaroslav@7
   761
                    i += 2;
jaroslav@7
   762
                    break;
jaroslav@7
   763
                }
jaroslav@151
   764
                case opc_ifne: {
jaroslav@20
   765
                    int indx = i + readIntArg(byteCodes, i);
lubomir@283
   766
                    emit(out, "if (@1 != 0) { gt = @2; continue; }",
lubomir@307
   767
                         smapper.popI(), Integer.toString(indx));
jaroslav@20
   768
                    i += 2;
jaroslav@20
   769
                    break;
jaroslav@20
   770
                }
jaroslav@151
   771
                case opc_iflt: {
jaroslav@20
   772
                    int indx = i + readIntArg(byteCodes, i);
lubomir@283
   773
                    emit(out, "if (@1 < 0) { gt = @2; continue; }",
lubomir@307
   774
                         smapper.popI(), Integer.toString(indx));
jaroslav@20
   775
                    i += 2;
jaroslav@20
   776
                    break;
jaroslav@20
   777
                }
jaroslav@151
   778
                case opc_ifle: {
jaroslav@20
   779
                    int indx = i + readIntArg(byteCodes, i);
lubomir@283
   780
                    emit(out, "if (@1 <= 0) { gt = @2; continue; }",
lubomir@307
   781
                         smapper.popI(), Integer.toString(indx));
jaroslav@20
   782
                    i += 2;
jaroslav@20
   783
                    break;
jaroslav@20
   784
                }
jaroslav@151
   785
                case opc_ifgt: {
jaroslav@20
   786
                    int indx = i + readIntArg(byteCodes, i);
lubomir@283
   787
                    emit(out, "if (@1 > 0) { gt = @2; continue; }",
lubomir@307
   788
                         smapper.popI(), Integer.toString(indx));
jaroslav@20
   789
                    i += 2;
jaroslav@20
   790
                    break;
jaroslav@20
   791
                }
jaroslav@151
   792
                case opc_ifge: {
jaroslav@20
   793
                    int indx = i + readIntArg(byteCodes, i);
lubomir@283
   794
                    emit(out, "if (@1 >= 0) { gt = @2; continue; }",
lubomir@307
   795
                         smapper.popI(), Integer.toString(indx));
jaroslav@20
   796
                    i += 2;
jaroslav@20
   797
                    break;
jaroslav@20
   798
                }
jaroslav@151
   799
                case opc_ifnonnull: {
jaroslav@16
   800
                    int indx = i + readIntArg(byteCodes, i);
lubomir@283
   801
                    emit(out, "if (@1 !== null) { gt = @2; continue; }",
lubomir@307
   802
                         smapper.popA(), Integer.toString(indx));
jaroslav@16
   803
                    i += 2;
jaroslav@16
   804
                    break;
jaroslav@16
   805
                }
jaroslav@151
   806
                case opc_ifnull: {
jaroslav@16
   807
                    int indx = i + readIntArg(byteCodes, i);
lubomir@283
   808
                    emit(out, "if (@1 === null) { gt = @2; continue; }",
lubomir@307
   809
                         smapper.popA(), Integer.toString(indx));
jaroslav@16
   810
                    i += 2;
jaroslav@16
   811
                    break;
jaroslav@16
   812
                }
jaroslav@151
   813
                case opc_if_icmpne:
lubomir@307
   814
                    i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
lubomir@281
   815
                                   "!=");
jaroslav@4
   816
                    break;
jaroslav@151
   817
                case opc_if_icmplt:
lubomir@307
   818
                    i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
lubomir@281
   819
                                   "<");
jaroslav@4
   820
                    break;
jaroslav@151
   821
                case opc_if_icmple:
lubomir@307
   822
                    i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
lubomir@281
   823
                                   "<=");
jaroslav@4
   824
                    break;
jaroslav@151
   825
                case opc_if_icmpgt:
lubomir@307
   826
                    i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
lubomir@281
   827
                                   ">");
jaroslav@4
   828
                    break;
jaroslav@151
   829
                case opc_if_icmpge:
lubomir@307
   830
                    i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
lubomir@281
   831
                                   ">=");
jaroslav@4
   832
                    break;
jaroslav@151
   833
                case opc_goto: {
jaroslav@5
   834
                    int indx = i + readIntArg(byteCodes, i);
lubomir@283
   835
                    emit(out, "gt = @1; continue;", Integer.toString(indx));
jaroslav@5
   836
                    i += 2;
jaroslav@5
   837
                    break;
jaroslav@5
   838
                }
jaroslav@151
   839
                case opc_lookupswitch: {
jtulach@128
   840
                    int table = i / 4 * 4 + 4;
jaroslav@115
   841
                    int dflt = i + readInt4(byteCodes, table);
jaroslav@115
   842
                    table += 4;
jaroslav@115
   843
                    int n = readInt4(byteCodes, table);
jaroslav@115
   844
                    table += 4;
lubomir@307
   845
                    out.append("switch (").append(smapper.popI()).append(") {\n");
jaroslav@115
   846
                    while (n-- > 0) {
jaroslav@115
   847
                        int cnstnt = readInt4(byteCodes, table);
jaroslav@115
   848
                        table += 4;
jaroslav@115
   849
                        int offset = i + readInt4(byteCodes, table);
jaroslav@115
   850
                        table += 4;
jaroslav@115
   851
                        out.append("  case " + cnstnt).append(": gt = " + offset).append("; continue;\n");
jaroslav@115
   852
                    }
jaroslav@115
   853
                    out.append("  default: gt = " + dflt).append("; continue;\n}");
jaroslav@115
   854
                    i = table - 1;
jaroslav@115
   855
                    break;
jaroslav@115
   856
                }
jaroslav@151
   857
                case opc_tableswitch: {
jtulach@128
   858
                    int table = i / 4 * 4 + 4;
jaroslav@115
   859
                    int dflt = i + readInt4(byteCodes, table);
jaroslav@115
   860
                    table += 4;
jaroslav@115
   861
                    int low = readInt4(byteCodes, table);
jaroslav@115
   862
                    table += 4;
jaroslav@115
   863
                    int high = readInt4(byteCodes, table);
jaroslav@115
   864
                    table += 4;
lubomir@307
   865
                    out.append("switch (").append(smapper.popI()).append(") {\n");
jaroslav@115
   866
                    while (low <= high) {
jaroslav@115
   867
                        int offset = i + readInt4(byteCodes, table);
jaroslav@115
   868
                        table += 4;
jaroslav@115
   869
                        out.append("  case " + low).append(": gt = " + offset).append("; continue;\n");
jaroslav@115
   870
                        low++;
jaroslav@115
   871
                    }
jaroslav@115
   872
                    out.append("  default: gt = " + dflt).append("; continue;\n}");
jaroslav@115
   873
                    i = table - 1;
jaroslav@115
   874
                    break;
jaroslav@115
   875
                }
jaroslav@151
   876
                case opc_invokeinterface: {
lubomir@307
   877
                    i = invokeVirtualMethod(byteCodes, i, smapper) + 2;
jaroslav@46
   878
                    break;
jaroslav@46
   879
                }
jaroslav@151
   880
                case opc_invokevirtual:
lubomir@307
   881
                    i = invokeVirtualMethod(byteCodes, i, smapper);
jaroslav@12
   882
                    break;
jaroslav@151
   883
                case opc_invokespecial:
lubomir@307
   884
                    i = invokeStaticMethod(byteCodes, i, smapper, false);
jaroslav@4
   885
                    break;
jaroslav@151
   886
                case opc_invokestatic:
lubomir@307
   887
                    i = invokeStaticMethod(byteCodes, i, smapper, true);
jaroslav@10
   888
                    break;
jaroslav@151
   889
                case opc_new: {
jaroslav@8
   890
                    int indx = readIntArg(byteCodes, i);
jaroslav@151
   891
                    String ci = jc.getClassName(indx);
lubomir@474
   892
                    emit(out, "var @1 = new @2;",
lubomir@317
   893
                         smapper.pushA(), accessClass(ci.replace('/', '_')));
jaroslav@151
   894
                    addReference(ci);
jaroslav@8
   895
                    i += 2;
jaroslav@8
   896
                    break;
jaroslav@8
   897
                }
lubomir@283
   898
                case opc_newarray:
jaroslav@448
   899
                    int atype = readByte(byteCodes, ++i);
jaroslav@448
   900
                    String jvmType;
jaroslav@448
   901
                    switch (atype) {
jaroslav@448
   902
                        case 4: jvmType = "[Z"; break;
jaroslav@448
   903
                        case 5: jvmType = "[C"; break;
jaroslav@448
   904
                        case 6: jvmType = "[F"; break;
jaroslav@448
   905
                        case 7: jvmType = "[D"; break;
jaroslav@448
   906
                        case 8: jvmType = "[B"; break;
jaroslav@448
   907
                        case 9: jvmType = "[S"; break;
jaroslav@448
   908
                        case 10: jvmType = "[I"; break;
jaroslav@448
   909
                        case 11: jvmType = "[J"; break;
jaroslav@448
   910
                        default: throw new IllegalStateException("Array type: " + atype);
jaroslav@448
   911
                    }
lubomir@474
   912
                    emit(out, "var @2 = new Array(@1).initWith('@3', 0);",
jaroslav@448
   913
                         smapper.popI(), smapper.pushA(), jvmType);
jaroslav@21
   914
                    break;
jaroslav@448
   915
                case opc_anewarray: {
jaroslav@448
   916
                    int type = readIntArg(byteCodes, i);
jaroslav@448
   917
                    i += 2;
jaroslav@448
   918
                    String typeName = jc.getClassName(type);
jaroslav@448
   919
                    if (typeName.startsWith("[")) {
jaroslav@448
   920
                        typeName = "[" + typeName;
jaroslav@448
   921
                    } else {
jaroslav@448
   922
                        typeName = "[L" + typeName + ";";
jaroslav@448
   923
                    }
lubomir@474
   924
                    emit(out, "var @2 = new Array(@1).initWith('@3', null);",
jaroslav@448
   925
                         smapper.popI(), smapper.pushA(), typeName);
jaroslav@21
   926
                    break;
jaroslav@448
   927
                }
jaroslav@151
   928
                case opc_multianewarray: {
jaroslav@448
   929
                    int type = readIntArg(byteCodes, i);
jtulach@128
   930
                    i += 2;
jaroslav@448
   931
                    String typeName = jc.getClassName(type);
jtulach@128
   932
                    int dim = readByte(byteCodes, ++i);
lubomir@307
   933
                    out.append("{ var a0 = new Array(").append(smapper.popI())
jaroslav@456
   934
                       .append(").initWith('").append(typeName).append("', null);");
jtulach@128
   935
                    for (int d = 1; d < dim; d++) {
jaroslav@453
   936
                        typeName = typeName.substring(1);
lubomir@221
   937
                        out.append("\n  var l" + d).append(" = ")
lubomir@307
   938
                           .append(smapper.popI()).append(';');
jtulach@128
   939
                        out.append("\n  for (var i" + d).append (" = 0; i" + d).
jtulach@128
   940
                            append(" < a" + (d - 1)).
jtulach@128
   941
                            append(".length; i" + d).append("++) {");
jtulach@128
   942
                        out.append("\n    var a" + d).
jaroslav@456
   943
                            append (" = new Array(l" + d).append(").initWith('")
jaroslav@457
   944
                            .append(typeName).append("', ")
jaroslav@457
   945
                            .append(typeName.length() == 2 ? "0" : "null").append(");");
jtulach@128
   946
                        out.append("\n    a" + (d - 1)).append("[i" + d).append("] = a" + d).
jtulach@128
   947
                            append(";");
jtulach@128
   948
                    }
jtulach@128
   949
                    for (int d = 1; d < dim; d++) {
jtulach@128
   950
                        out.append("\n  }");
jtulach@128
   951
                    }
lubomir@474
   952
                    out.append("\nvar ").append(smapper.pushA())
lubomir@474
   953
                                        .append(" = a0; }");
jtulach@128
   954
                    break;
jtulach@128
   955
                }
jaroslav@151
   956
                case opc_arraylength:
lubomir@474
   957
                    emit(out, "var @2 = @1.length;",
lubomir@474
   958
                         smapper.popA(), smapper.pushI());
jaroslav@272
   959
                    break;
lubomir@283
   960
                case opc_lastore:
jaroslav@459
   961
                    emit(out, "@3.at(@2, @1);",
lubomir@307
   962
                         smapper.popL(), smapper.popI(), smapper.popA());
lubomir@281
   963
                    break;
lubomir@283
   964
                case opc_fastore:
jaroslav@459
   965
                    emit(out, "@3.at(@2, @1);",
lubomir@307
   966
                         smapper.popF(), smapper.popI(), smapper.popA());
lubomir@281
   967
                    break;
lubomir@283
   968
                case opc_dastore:
jaroslav@459
   969
                    emit(out, "@3.at(@2, @1);",
lubomir@307
   970
                         smapper.popD(), smapper.popI(), smapper.popA());
lubomir@281
   971
                    break;
lubomir@283
   972
                case opc_aastore:
jaroslav@459
   973
                    emit(out, "@3.at(@2, @1);",
lubomir@307
   974
                         smapper.popA(), smapper.popI(), smapper.popA());
jaroslav@21
   975
                    break;
jaroslav@151
   976
                case opc_iastore:
jaroslav@151
   977
                case opc_bastore:
jaroslav@151
   978
                case opc_castore:
lubomir@283
   979
                case opc_sastore:
jaroslav@459
   980
                    emit(out, "@3.at(@2, @1);",
lubomir@307
   981
                         smapper.popI(), smapper.popI(), smapper.popA());
jaroslav@240
   982
                    break;
lubomir@283
   983
                case opc_laload:
lubomir@474
   984
                    emit(out, "var @3 = @2.at(@1);",
lubomir@307
   985
                         smapper.popI(), smapper.popA(), smapper.pushL());
lubomir@281
   986
                    break;
lubomir@283
   987
                case opc_faload:
lubomir@474
   988
                    emit(out, "var @3 = @2.at(@1);",
lubomir@307
   989
                         smapper.popI(), smapper.popA(), smapper.pushF());
lubomir@281
   990
                    break;
lubomir@283
   991
                case opc_daload:
lubomir@474
   992
                    emit(out, "var @3 = @2.at(@1);",
lubomir@307
   993
                         smapper.popI(), smapper.popA(), smapper.pushD());
lubomir@281
   994
                    break;
lubomir@283
   995
                case opc_aaload:
lubomir@474
   996
                    emit(out, "var @3 = @2.at(@1);",
lubomir@307
   997
                         smapper.popI(), smapper.popA(), smapper.pushA());
jaroslav@272
   998
                    break;
jaroslav@272
   999
                case opc_iaload:
jaroslav@272
  1000
                case opc_baload:
jaroslav@272
  1001
                case opc_caload:
lubomir@283
  1002
                case opc_saload:
lubomir@474
  1003
                    emit(out, "var @3 = @2.at(@1);",
lubomir@307
  1004
                         smapper.popI(), smapper.popA(), smapper.pushI());
jaroslav@272
  1005
                    break;
lubomir@221
  1006
                case opc_pop:
jaroslav@272
  1007
                case opc_pop2:
lubomir@307
  1008
                    smapper.pop(1);
jaroslav@399
  1009
                    debug("/* pop */");
jaroslav@272
  1010
                    break;
lubomir@281
  1011
                case opc_dup: {
lubomir@307
  1012
                    final Variable v = smapper.get(0);
lubomir@474
  1013
                    emit(out, "var @1 = @2;", smapper.pushT(v.getType()), v);
jaroslav@21
  1014
                    break;
jaroslav@21
  1015
                }
lubomir@281
  1016
                case opc_dup2: {
lubomir@307
  1017
                    if (smapper.get(0).isCategory2()) {
lubomir@307
  1018
                        final Variable v = smapper.get(0);
lubomir@474
  1019
                        emit(out, "var @1 = @2;",
lubomir@474
  1020
                             smapper.pushT(v.getType()), v);
lubomir@281
  1021
                    } else {
lubomir@307
  1022
                        final Variable v1 = smapper.get(0);
lubomir@307
  1023
                        final Variable v2 = smapper.get(1);
lubomir@474
  1024
                        emit(out, "var @1 = @2, @3 = @4;",
lubomir@307
  1025
                             smapper.pushT(v2.getType()), v2,
lubomir@307
  1026
                             smapper.pushT(v1.getType()), v1);
lubomir@281
  1027
                    }
jaroslav@21
  1028
                    break;
jaroslav@21
  1029
                }
lubomir@281
  1030
                case opc_dup_x1: {
lubomir@307
  1031
                    final Variable vi1 = smapper.pop();
lubomir@307
  1032
                    final Variable vi2 = smapper.pop();
lubomir@307
  1033
                    final Variable vo3 = smapper.pushT(vi1.getType());
lubomir@307
  1034
                    final Variable vo2 = smapper.pushT(vi2.getType());
lubomir@307
  1035
                    final Variable vo1 = smapper.pushT(vi1.getType());
lubomir@281
  1036
lubomir@474
  1037
                    emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
lubomir@283
  1038
                         vo1, vi1, vo2, vi2, vo3, vo1);
jaroslav@93
  1039
                    break;
lubomir@281
  1040
                }
lubomir@281
  1041
                case opc_dup_x2: {
lubomir@307
  1042
                    if (smapper.get(1).isCategory2()) {
lubomir@307
  1043
                        final Variable vi1 = smapper.pop();
lubomir@307
  1044
                        final Variable vi2 = smapper.pop();
lubomir@307
  1045
                        final Variable vo3 = smapper.pushT(vi1.getType());
lubomir@307
  1046
                        final Variable vo2 = smapper.pushT(vi2.getType());
lubomir@307
  1047
                        final Variable vo1 = smapper.pushT(vi1.getType());
lubomir@281
  1048
lubomir@474
  1049
                        emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
lubomir@283
  1050
                             vo1, vi1, vo2, vi2, vo3, vo1);
lubomir@281
  1051
                    } else {
lubomir@307
  1052
                        final Variable vi1 = smapper.pop();
lubomir@307
  1053
                        final Variable vi2 = smapper.pop();
lubomir@307
  1054
                        final Variable vi3 = smapper.pop();
lubomir@307
  1055
                        final Variable vo4 = smapper.pushT(vi1.getType());
lubomir@307
  1056
                        final Variable vo3 = smapper.pushT(vi3.getType());
lubomir@307
  1057
                        final Variable vo2 = smapper.pushT(vi2.getType());
lubomir@307
  1058
                        final Variable vo1 = smapper.pushT(vi1.getType());
lubomir@281
  1059
lubomir@474
  1060
                        emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8;",
lubomir@283
  1061
                             vo1, vi1, vo2, vi2, vo3, vi3, vo4, vo1);
lubomir@281
  1062
                    }
jaroslav@8
  1063
                    break;
lubomir@281
  1064
                }
jaroslav@151
  1065
                case opc_bipush:
lubomir@474
  1066
                    emit(out, "var @1 = @2;",
lubomir@307
  1067
                         smapper.pushI(), Integer.toString(byteCodes[++i]));
jaroslav@8
  1068
                    break;
jaroslav@151
  1069
                case opc_sipush:
lubomir@474
  1070
                    emit(out, "var @1 = @2;",
lubomir@307
  1071
                         smapper.pushI(),
lubomir@283
  1072
                         Integer.toString(readIntArg(byteCodes, i)));
jaroslav@31
  1073
                    i += 2;
jaroslav@31
  1074
                    break;
jaroslav@151
  1075
                case opc_getfield: {
jaroslav@8
  1076
                    int indx = readIntArg(byteCodes, i);
jaroslav@151
  1077
                    String[] fi = jc.getFieldInfoName(indx);
lubomir@307
  1078
                    final int type = VarType.fromFieldType(fi[2].charAt(0));
lubomir@474
  1079
                    emit(out, "var @2 = @1.fld_@3;",
lubomir@307
  1080
                         smapper.popA(), smapper.pushT(type), fi[1]);
jaroslav@8
  1081
                    i += 2;
jaroslav@8
  1082
                    break;
jaroslav@8
  1083
                }
jaroslav@151
  1084
                case opc_getstatic: {
jaroslav@9
  1085
                    int indx = readIntArg(byteCodes, i);
jaroslav@151
  1086
                    String[] fi = jc.getFieldInfoName(indx);
lubomir@307
  1087
                    final int type = VarType.fromFieldType(fi[2].charAt(0));
lubomir@474
  1088
                    emit(out, "var @1 = @2(false).constructor.@3;",
lubomir@317
  1089
                         smapper.pushT(type),
lubomir@317
  1090
                         accessClass(fi[0].replace('/', '_')), fi[1]);
jaroslav@9
  1091
                    i += 2;
jaroslav@151
  1092
                    addReference(fi[0]);
jaroslav@9
  1093
                    break;
jaroslav@9
  1094
                }
jaroslav@151
  1095
                case opc_putfield: {
jaroslav@10
  1096
                    int indx = readIntArg(byteCodes, i);
jaroslav@151
  1097
                    String[] fi = jc.getFieldInfoName(indx);
lubomir@307
  1098
                    final int type = VarType.fromFieldType(fi[2].charAt(0));
lubomir@283
  1099
                    emit(out, "@2.fld_@3 = @1;",
lubomir@307
  1100
                         smapper.popT(type), smapper.popA(), fi[1]);
jaroslav@10
  1101
                    i += 2;
jaroslav@10
  1102
                    break;
jaroslav@10
  1103
                }
lubomir@281
  1104
                case opc_putstatic: {
lubomir@281
  1105
                    int indx = readIntArg(byteCodes, i);
lubomir@281
  1106
                    String[] fi = jc.getFieldInfoName(indx);
lubomir@307
  1107
                    final int type = VarType.fromFieldType(fi[2].charAt(0));
jaroslav@358
  1108
                    emit(out, "@1(false).constructor.@2 = @3;",
lubomir@317
  1109
                         accessClass(fi[0].replace('/', '_')), fi[1],
lubomir@317
  1110
                         smapper.popT(type));
lubomir@281
  1111
                    i += 2;
lubomir@281
  1112
                    addReference(fi[0]);
lubomir@281
  1113
                    break;
lubomir@281
  1114
                }
jaroslav@151
  1115
                case opc_checkcast: {
jaroslav@30
  1116
                    int indx = readIntArg(byteCodes, i);
jaroslav@151
  1117
                    final String type = jc.getClassName(indx);
jaroslav@42
  1118
                    if (!type.startsWith("[")) {
jaroslav@42
  1119
                        // no way to check arrays right now
lubomir@283
  1120
                        // XXX proper exception
lubomir@317
  1121
                        emit(out,
lubomir@317
  1122
                             "if (@1 !== null && !@1.$instOf_@2) throw {};",
lubomir@307
  1123
                             smapper.getA(0), type.replace('/', '_'));
jaroslav@42
  1124
                    }
jaroslav@30
  1125
                    i += 2;
jaroslav@30
  1126
                    break;
jaroslav@30
  1127
                }
jaroslav@151
  1128
                case opc_instanceof: {
jaroslav@17
  1129
                    int indx = readIntArg(byteCodes, i);
jaroslav@151
  1130
                    final String type = jc.getClassName(indx);
lubomir@474
  1131
                    emit(out, "var @2 = @1.$instOf_@3 ? 1 : 0;",
lubomir@317
  1132
                         smapper.popA(), smapper.pushI(),
lubomir@317
  1133
                         type.replace('/', '_'));
jaroslav@17
  1134
                    i += 2;
jaroslav@30
  1135
                    break;
jaroslav@17
  1136
                }
jaroslav@151
  1137
                case opc_athrow: {
lubomir@307
  1138
                    final Variable v = smapper.popA();
lubomir@307
  1139
                    smapper.clear();
lubomir@281
  1140
lubomir@474
  1141
                    emit(out, "{ var @1 = @2; throw @2; }",
lubomir@307
  1142
                         smapper.pushA(), v);
jaroslav@104
  1143
                    break;
jaroslav@104
  1144
                }
lubomir@221
  1145
lubomir@221
  1146
                case opc_monitorenter: {
lubomir@221
  1147
                    out.append("/* monitor enter */");
lubomir@307
  1148
                    smapper.popA();
lubomir@221
  1149
                    break;
lubomir@221
  1150
                }
lubomir@221
  1151
lubomir@221
  1152
                case opc_monitorexit: {
lubomir@221
  1153
                    out.append("/* monitor exit */");
lubomir@307
  1154
                    smapper.popA();
lubomir@221
  1155
                    break;
lubomir@221
  1156
                }
lubomir@221
  1157
jaroslav@104
  1158
                default: {
lubomir@283
  1159
                    emit(out, "throw 'unknown bytecode @1';",
lubomir@283
  1160
                         Integer.toString(c));
jaroslav@104
  1161
                }
jaroslav@0
  1162
            }
jaroslav@399
  1163
            if (debug(" //")) {
jaroslav@399
  1164
                for (int j = prev; j <= i; j++) {
jaroslav@399
  1165
                    out.append(" ");
jaroslav@399
  1166
                    final int cc = readByte(byteCodes, j);
jaroslav@399
  1167
                    out.append(Integer.toString(cc));
tzezula@285
  1168
                }
jaroslav@0
  1169
            }
tzezula@285
  1170
            out.append("\n");            
jaroslav@0
  1171
        }
jaroslav@398
  1172
        if (previousTrap != null) {
jaroslav@398
  1173
            generateCatch(previousTrap);
jaroslav@398
  1174
        }
jaroslav@10
  1175
        out.append("  }\n");
lubomir@307
  1176
        out.append("};");
jaroslav@4
  1177
    }
jaroslav@4
  1178
lubomir@281
  1179
    private int generateIf(byte[] byteCodes, int i,
lubomir@281
  1180
                           final Variable v2, final Variable v1,
lubomir@281
  1181
                           final String test) throws IOException {
jaroslav@4
  1182
        int indx = i + readIntArg(byteCodes, i);
lubomir@281
  1183
        out.append("if (").append(v1)
lubomir@221
  1184
           .append(' ').append(test).append(' ')
lubomir@281
  1185
           .append(v2).append(") { gt = " + indx)
lubomir@221
  1186
           .append("; continue; }");
jaroslav@4
  1187
        return i + 2;
jaroslav@4
  1188
    }
jaroslav@4
  1189
jaroslav@4
  1190
    private int readIntArg(byte[] byteCodes, int offsetInstruction) {
jaroslav@5
  1191
        final int indxHi = byteCodes[offsetInstruction + 1] << 8;
jaroslav@5
  1192
        final int indxLo = byteCodes[offsetInstruction + 2];
jaroslav@5
  1193
        return (indxHi & 0xffffff00) | (indxLo & 0xff);
jaroslav@4
  1194
    }
jaroslav@115
  1195
    private int readInt4(byte[] byteCodes, int offsetInstruction) {
jaroslav@115
  1196
        final int d = byteCodes[offsetInstruction + 0] << 24;
jaroslav@115
  1197
        final int c = byteCodes[offsetInstruction + 1] << 16;
jaroslav@115
  1198
        final int b = byteCodes[offsetInstruction + 2] << 8;
jaroslav@115
  1199
        final int a = byteCodes[offsetInstruction + 3];
jaroslav@115
  1200
        return (d & 0xff000000) | (c & 0xff0000) | (b & 0xff00) | (a & 0xff);
jaroslav@115
  1201
    }
jtulach@128
  1202
    private int readByte(byte[] byteCodes, int offsetInstruction) {
lubomir@221
  1203
        return byteCodes[offsetInstruction] & 0xff;
jtulach@128
  1204
    }
jaroslav@4
  1205
    
lubomir@281
  1206
    private static void countArgs(String descriptor, char[] returnType, StringBuilder sig, StringBuilder cnt) {
jaroslav@4
  1207
        int i = 0;
jaroslav@4
  1208
        Boolean count = null;
jaroslav@32
  1209
        boolean array = false;
jaroslav@248
  1210
        sig.append("__");
jaroslav@10
  1211
        int firstPos = sig.length();
jaroslav@4
  1212
        while (i < descriptor.length()) {
jaroslav@4
  1213
            char ch = descriptor.charAt(i++);
jaroslav@4
  1214
            switch (ch) {
jaroslav@4
  1215
                case '(':
jaroslav@4
  1216
                    count = true;
jaroslav@4
  1217
                    continue;
jaroslav@4
  1218
                case ')':
jaroslav@4
  1219
                    count = false;
jaroslav@4
  1220
                    continue;
jaroslav@4
  1221
                case 'B': 
jaroslav@4
  1222
                case 'C': 
jaroslav@4
  1223
                case 'D': 
jaroslav@4
  1224
                case 'F': 
jaroslav@4
  1225
                case 'I': 
jaroslav@4
  1226
                case 'J': 
jaroslav@4
  1227
                case 'S': 
jaroslav@4
  1228
                case 'Z': 
jaroslav@4
  1229
                    if (count) {
jaroslav@32
  1230
                        if (array) {
jaroslav@248
  1231
                            sig.append("_3");
jaroslav@32
  1232
                        }
jaroslav@4
  1233
                        sig.append(ch);
jtulach@156
  1234
                        if (ch == 'J' || ch == 'D') {
jtulach@156
  1235
                            cnt.append('1');
jtulach@156
  1236
                        } else {
jtulach@156
  1237
                            cnt.append('0');
jtulach@156
  1238
                        }
jaroslav@4
  1239
                    } else {
jaroslav@10
  1240
                        sig.insert(firstPos, ch);
jaroslav@32
  1241
                        if (array) {
lubomir@281
  1242
                            returnType[0] = '[';
jaroslav@248
  1243
                            sig.insert(firstPos, "_3");
lubomir@281
  1244
                        } else {
lubomir@281
  1245
                            returnType[0] = ch;
jaroslav@32
  1246
                        }
jaroslav@4
  1247
                    }
jaroslav@93
  1248
                    array = false;
jaroslav@4
  1249
                    continue;
jaroslav@4
  1250
                case 'V': 
jaroslav@4
  1251
                    assert !count;
lubomir@281
  1252
                    returnType[0] = 'V';
jaroslav@10
  1253
                    sig.insert(firstPos, 'V');
jaroslav@4
  1254
                    continue;
jaroslav@4
  1255
                case 'L':
jaroslav@16
  1256
                    int next = descriptor.indexOf(';', i);
jaroslav@248
  1257
                    String realSig = mangleSig(descriptor, i - 1, next + 1);
jaroslav@4
  1258
                    if (count) {
jaroslav@32
  1259
                        if (array) {
jaroslav@248
  1260
                            sig.append("_3");
jaroslav@32
  1261
                        }
jaroslav@248
  1262
                        sig.append(realSig);
jtulach@156
  1263
                        cnt.append('0');
jaroslav@4
  1264
                    } else {
jaroslav@248
  1265
                        sig.insert(firstPos, realSig);
jaroslav@32
  1266
                        if (array) {
jaroslav@248
  1267
                            sig.insert(firstPos, "_3");
jaroslav@32
  1268
                        }
lubomir@281
  1269
                        returnType[0] = 'L';
jaroslav@4
  1270
                    }
jaroslav@16
  1271
                    i = next + 1;
lubomir@407
  1272
                    array = false;
jaroslav@4
  1273
                    continue;
jaroslav@4
  1274
                case '[':
jaroslav@248
  1275
                    array = true;
jaroslav@4
  1276
                    continue;
jaroslav@4
  1277
                default:
jaroslav@248
  1278
                    throw new IllegalStateException("Invalid char: " + ch);
jaroslav@4
  1279
            }
jaroslav@4
  1280
        }
jaroslav@0
  1281
    }
jaroslav@248
  1282
    
jaroslav@248
  1283
    private static String mangleSig(String txt, int first, int last) {
jaroslav@248
  1284
        StringBuilder sb = new StringBuilder();
jaroslav@248
  1285
        for (int i = first; i < last; i++) {
jaroslav@248
  1286
            final char ch = txt.charAt(i);
jaroslav@248
  1287
            switch (ch) {
jaroslav@248
  1288
                case '/': sb.append('_'); break;
jaroslav@248
  1289
                case '_': sb.append("_1"); break;
jaroslav@248
  1290
                case ';': sb.append("_2"); break;
jaroslav@248
  1291
                case '[': sb.append("_3"); break;
jaroslav@248
  1292
                default: sb.append(ch); break;
jaroslav@248
  1293
            }
jaroslav@248
  1294
        }
jaroslav@248
  1295
        return sb.toString();
jaroslav@248
  1296
    }
jaroslav@9
  1297
jaroslav@248
  1298
    private static String findMethodName(MethodData m, StringBuilder cnt) {
jaroslav@42
  1299
        StringBuilder name = new StringBuilder();
jaroslav@10
  1300
        if ("<init>".equals(m.getName())) { // NOI18N
jaroslav@42
  1301
            name.append("cons"); // NOI18N
jaroslav@19
  1302
        } else if ("<clinit>".equals(m.getName())) { // NOI18N
jaroslav@42
  1303
            name.append("class"); // NOI18N
jaroslav@10
  1304
        } else {
jaroslav@42
  1305
            name.append(m.getName());
jaroslav@10
  1306
        } 
jaroslav@42
  1307
        
lubomir@282
  1308
        countArgs(m.getInternalSig(), new char[1], name, cnt);
jaroslav@42
  1309
        return name.toString();
jaroslav@10
  1310
    }
jaroslav@10
  1311
lubomir@282
  1312
    static String findMethodName(String[] mi, StringBuilder cnt, char[] returnType) {
jaroslav@10
  1313
        StringBuilder name = new StringBuilder();
jaroslav@151
  1314
        String descr = mi[2];//mi.getDescriptor();
jaroslav@151
  1315
        String nm= mi[1];
jaroslav@151
  1316
        if ("<init>".equals(nm)) { // NOI18N
jaroslav@10
  1317
            name.append("cons"); // NOI18N
jaroslav@10
  1318
        } else {
jaroslav@151
  1319
            name.append(nm);
jaroslav@10
  1320
        }
lubomir@282
  1321
        countArgs(descr, returnType, name, cnt);
jaroslav@10
  1322
        return name.toString();
jaroslav@10
  1323
    }
jaroslav@10
  1324
lubomir@307
  1325
    private int invokeStaticMethod(byte[] byteCodes, int i, final StackMapper mapper, boolean isStatic)
jaroslav@10
  1326
    throws IOException {
jaroslav@10
  1327
        int methodIndex = readIntArg(byteCodes, i);
jaroslav@151
  1328
        String[] mi = jc.getFieldInfoName(methodIndex);
lubomir@281
  1329
        char[] returnType = { 'V' };
jtulach@156
  1330
        StringBuilder cnt = new StringBuilder();
lubomir@281
  1331
        String mn = findMethodName(mi, cnt, returnType);
lubomir@221
  1332
lubomir@221
  1333
        final int numArguments = isStatic ? cnt.length() : cnt.length() + 1;
lubomir@281
  1334
        final Variable[] vars = new Variable[numArguments];
lubomir@221
  1335
lubomir@281
  1336
        for (int j = numArguments - 1; j >= 0; --j) {
lubomir@281
  1337
            vars[j] = mapper.pop();
jaroslav@11
  1338
        }
lubomir@281
  1339
lubomir@281
  1340
        if (returnType[0] != 'V') {
lubomir@474
  1341
            out.append("var ")
lubomir@474
  1342
               .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
lubomir@281
  1343
               .append(" = ");
jaroslav@10
  1344
        }
lubomir@221
  1345
jaroslav@151
  1346
        final String in = mi[0];
jaroslav@274
  1347
        out.append(accessClass(in.replace('/', '_')));
jaroslav@224
  1348
        out.append("(false).");
jaroslav@397
  1349
        if (mn.startsWith("cons_")) {
jaroslav@397
  1350
            out.append("constructor.");
jaroslav@397
  1351
        }
jaroslav@10
  1352
        out.append(mn);
jaroslav@442
  1353
        if (isStatic) {
jaroslav@442
  1354
            out.append('(');
jaroslav@442
  1355
        } else {
jaroslav@442
  1356
            out.append(".call(");
jaroslav@442
  1357
        }
lubomir@221
  1358
        if (numArguments > 0) {
lubomir@281
  1359
            out.append(vars[0]);
lubomir@281
  1360
            for (int j = 1; j < numArguments; ++j) {
lubomir@221
  1361
                out.append(", ");
lubomir@281
  1362
                out.append(vars[j]);
lubomir@221
  1363
            }
jaroslav@10
  1364
        }
lubomir@221
  1365
        out.append(");");
jaroslav@10
  1366
        i += 2;
jaroslav@18
  1367
        addReference(in);
jaroslav@10
  1368
        return i;
jaroslav@10
  1369
    }
lubomir@307
  1370
    private int invokeVirtualMethod(byte[] byteCodes, int i, final StackMapper mapper)
jaroslav@12
  1371
    throws IOException {
jaroslav@12
  1372
        int methodIndex = readIntArg(byteCodes, i);
jaroslav@151
  1373
        String[] mi = jc.getFieldInfoName(methodIndex);
lubomir@281
  1374
        char[] returnType = { 'V' };
jtulach@156
  1375
        StringBuilder cnt = new StringBuilder();
lubomir@281
  1376
        String mn = findMethodName(mi, cnt, returnType);
lubomir@221
  1377
lubomir@281
  1378
        final int numArguments = cnt.length() + 1;
lubomir@281
  1379
        final Variable[] vars = new Variable[numArguments];
lubomir@221
  1380
lubomir@281
  1381
        for (int j = numArguments - 1; j >= 0; --j) {
lubomir@281
  1382
            vars[j] = mapper.pop();
jaroslav@12
  1383
        }
lubomir@221
  1384
lubomir@281
  1385
        if (returnType[0] != 'V') {
lubomir@474
  1386
            out.append("var ")
lubomir@474
  1387
               .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
lubomir@281
  1388
               .append(" = ");
jaroslav@12
  1389
        }
lubomir@281
  1390
lubomir@281
  1391
        out.append(vars[0]).append('.');
jaroslav@12
  1392
        out.append(mn);
jaroslav@12
  1393
        out.append('(');
jaroslav@442
  1394
        String sep = "";
lubomir@281
  1395
        for (int j = 1; j < numArguments; ++j) {
jaroslav@442
  1396
            out.append(sep);
lubomir@281
  1397
            out.append(vars[j]);
jaroslav@442
  1398
            sep = ", ";
jaroslav@12
  1399
        }
lubomir@221
  1400
        out.append(");");
jaroslav@12
  1401
        i += 2;
jaroslav@12
  1402
        return i;
jaroslav@12
  1403
    }
lubomir@221
  1404
jaroslav@103
  1405
    private void addReference(String cn) throws IOException {
jtulach@162
  1406
        if (requireReference(cn)) {
jaroslav@399
  1407
            debug(" /* needs " + cn + " */");
jaroslav@18
  1408
        }
jaroslav@18
  1409
    }
jaroslav@16
  1410
jaroslav@33
  1411
    private void outType(String d, StringBuilder out) {
jaroslav@33
  1412
        int arr = 0;
jaroslav@33
  1413
        while (d.charAt(0) == '[') {
jaroslav@33
  1414
            out.append('A');
jaroslav@33
  1415
            d = d.substring(1);
jaroslav@33
  1416
        }
jaroslav@16
  1417
        if (d.charAt(0) == 'L') {
jaroslav@16
  1418
            assert d.charAt(d.length() - 1) == ';';
jaroslav@16
  1419
            out.append(d.replace('/', '_').substring(0, d.length() - 1));
jaroslav@16
  1420
        } else {
jaroslav@16
  1421
            out.append(d);
jaroslav@16
  1422
        }
jaroslav@16
  1423
    }
jaroslav@21
  1424
jaroslav@230
  1425
    private String encodeConstant(int entryIndex) throws IOException {
jaroslav@230
  1426
        String[] classRef = { null };
jaroslav@230
  1427
        String s = jc.stringValue(entryIndex, classRef);
jaroslav@230
  1428
        if (classRef[0] != null) {
jaroslav@230
  1429
            addReference(classRef[0]);
jaroslav@274
  1430
            s = accessClass(s.replace('/', '_')) + "(false).constructor.$class";
jaroslav@230
  1431
        }
jaroslav@151
  1432
        return s;
jaroslav@21
  1433
    }
jaroslav@32
  1434
jaroslav@266
  1435
    private String javaScriptBody(String prefix, MethodData m, boolean isStatic) throws IOException {
jaroslav@152
  1436
        byte[] arr = m.findAnnotationData(true);
jaroslav@152
  1437
        if (arr == null) {
jaroslav@266
  1438
            return null;
jaroslav@152
  1439
        }
jaroslav@200
  1440
        final String jvmType = "Lorg/apidesign/bck2brwsr/core/JavaScriptBody;";
jaroslav@152
  1441
        class P extends AnnotationParser {
jaroslav@237
  1442
            public P() {
jaroslav@237
  1443
                super(false);
jaroslav@237
  1444
            }
jaroslav@237
  1445
            
jaroslav@152
  1446
            int cnt;
jaroslav@152
  1447
            String[] args = new String[30];
jaroslav@152
  1448
            String body;
jaroslav@94
  1449
            
jaroslav@152
  1450
            @Override
jaroslav@236
  1451
            protected void visitAttr(String type, String attr, String at, String value) {
jaroslav@152
  1452
                if (type.equals(jvmType)) {
jaroslav@152
  1453
                    if ("body".equals(attr)) {
jaroslav@152
  1454
                        body = value;
jaroslav@152
  1455
                    } else if ("args".equals(attr)) {
jaroslav@152
  1456
                        args[cnt++] = value;
jaroslav@152
  1457
                    } else {
jaroslav@152
  1458
                        throw new IllegalArgumentException(attr);
jaroslav@152
  1459
                    }
jaroslav@152
  1460
                }
jaroslav@94
  1461
            }
jaroslav@94
  1462
        }
jaroslav@152
  1463
        P p = new P();
jaroslav@152
  1464
        p.parse(arr, jc);
jaroslav@152
  1465
        if (p.body == null) {
jaroslav@266
  1466
            return null;
jaroslav@152
  1467
        }
jtulach@156
  1468
        StringBuilder cnt = new StringBuilder();
jaroslav@266
  1469
        final String mn = findMethodName(m, cnt);
jaroslav@266
  1470
        out.append(prefix).append(mn);
jaroslav@203
  1471
        out.append(" = function(");
jaroslav@442
  1472
        String space = "";
jaroslav@443
  1473
        int index = 0;
jtulach@156
  1474
        for (int i = 0; i < cnt.length(); i++) {
jaroslav@152
  1475
            out.append(space);
jaroslav@316
  1476
            space = outputArg(out, p.args, index);
jaroslav@152
  1477
            index++;
jaroslav@152
  1478
        }
jaroslav@152
  1479
        out.append(") {").append("\n");
jaroslav@152
  1480
        out.append(p.body);
jaroslav@152
  1481
        out.append("\n}\n");
jaroslav@266
  1482
        return mn;
jaroslav@151
  1483
    }
jaroslav@151
  1484
    private static String className(ClassData jc) {
jaroslav@151
  1485
        //return jc.getName().getInternalName().replace('/', '_');
jaroslav@151
  1486
        return jc.getClassName().replace('/', '_');
jaroslav@94
  1487
    }
jaroslav@152
  1488
    
jaroslav@152
  1489
    private static String[] findAnnotation(
jaroslav@152
  1490
        byte[] arr, ClassData cd, final String className, 
jaroslav@152
  1491
        final String... attrNames
jaroslav@152
  1492
    ) throws IOException {
jaroslav@152
  1493
        if (arr == null) {
jaroslav@152
  1494
            return null;
jaroslav@152
  1495
        }
jaroslav@152
  1496
        final String[] values = new String[attrNames.length];
jaroslav@152
  1497
        final boolean[] found = { false };
jaroslav@152
  1498
        final String jvmType = "L" + className.replace('.', '/') + ";";
jaroslav@237
  1499
        AnnotationParser ap = new AnnotationParser(false) {
jaroslav@152
  1500
            @Override
jaroslav@236
  1501
            protected void visitAttr(String type, String attr, String at, String value) {
jaroslav@152
  1502
                if (type.equals(jvmType)) {
jaroslav@152
  1503
                    found[0] = true;
jaroslav@152
  1504
                    for (int i = 0; i < attrNames.length; i++) {
jaroslav@152
  1505
                        if (attrNames[i].equals(attr)) {
jaroslav@152
  1506
                            values[i] = value;
jaroslav@152
  1507
                        }
jaroslav@152
  1508
                    }
jaroslav@152
  1509
                }
jaroslav@152
  1510
            }
jaroslav@152
  1511
            
jaroslav@152
  1512
        };
jaroslav@152
  1513
        ap.parse(arr, cd);
jaroslav@152
  1514
        return found[0] ? values : null;
jaroslav@152
  1515
    }
jaroslav@173
  1516
jaroslav@173
  1517
    private CharSequence initField(FieldData v) {
jaroslav@173
  1518
        final String is = v.getInternalSig();
jaroslav@173
  1519
        if (is.length() == 1) {
jaroslav@173
  1520
            switch (is.charAt(0)) {
jaroslav@173
  1521
                case 'S':
jaroslav@173
  1522
                case 'J':
jaroslav@173
  1523
                case 'B':
jaroslav@173
  1524
                case 'Z':
jaroslav@173
  1525
                case 'C':
jaroslav@173
  1526
                case 'I': return " = 0;";
jaroslav@173
  1527
                case 'F': 
jaroslav@180
  1528
                case 'D': return " = 0.0;";
jaroslav@173
  1529
                default:
jaroslav@173
  1530
                    throw new IllegalStateException(is);
jaroslav@173
  1531
            }
jaroslav@173
  1532
        }
jaroslav@173
  1533
        return " = null;";
jaroslav@173
  1534
    }
jaroslav@235
  1535
jaroslav@235
  1536
    private static void generateAnno(ClassData cd, final Appendable out, byte[] data) throws IOException {
jaroslav@237
  1537
        AnnotationParser ap = new AnnotationParser(true) {
jaroslav@237
  1538
            int anno;
jaroslav@235
  1539
            int cnt;
jaroslav@235
  1540
            
jaroslav@235
  1541
            @Override
jaroslav@235
  1542
            protected void visitAnnotationStart(String type) throws IOException {
jaroslav@237
  1543
                if (anno++ > 0) {
jaroslav@237
  1544
                    out.append(",");
jaroslav@237
  1545
                }
jaroslav@235
  1546
                out.append('"').append(type).append("\" : {\n");
jaroslav@235
  1547
                cnt = 0;
jaroslav@235
  1548
            }
jaroslav@235
  1549
jaroslav@235
  1550
            @Override
jaroslav@235
  1551
            protected void visitAnnotationEnd(String type) throws IOException {
jaroslav@235
  1552
                out.append("\n}\n");
jaroslav@235
  1553
            }
jaroslav@235
  1554
            
jaroslav@235
  1555
            @Override
jaroslav@236
  1556
            protected void visitAttr(String type, String attr, String attrType, String value) 
jaroslav@235
  1557
            throws IOException {
jaroslav@266
  1558
                if (attr == null) {
jaroslav@266
  1559
                    return;
jaroslav@266
  1560
                }
jaroslav@235
  1561
                if (cnt++ > 0) {
jaroslav@235
  1562
                    out.append(",\n");
jaroslav@235
  1563
                }
jaroslav@250
  1564
                out.append(attr).append("__").append(attrType).append(" : ").append(value);
jaroslav@235
  1565
            }
jaroslav@235
  1566
        };
jaroslav@235
  1567
        ap.parse(data, cd);
jaroslav@235
  1568
    }
jaroslav@316
  1569
jaroslav@316
  1570
    private static String outputArg(Appendable out, String[] args, int indx) throws IOException {
jaroslav@316
  1571
        final String name = args[indx];
jaroslav@316
  1572
        if (name == null) {
jaroslav@316
  1573
            return "";
jaroslav@316
  1574
        }
jaroslav@316
  1575
        if (name.contains(",")) {
jaroslav@316
  1576
            throw new IOException("Wrong parameter with ',': " + name);
jaroslav@316
  1577
        }
jaroslav@316
  1578
        out.append(name);
jaroslav@316
  1579
        return ",";
jaroslav@316
  1580
    }
lubomir@317
  1581
lubomir@283
  1582
    private static void emit(final Appendable out,
lubomir@283
  1583
                             final String format,
lubomir@283
  1584
                             final CharSequence... params) throws IOException {
lubomir@283
  1585
        final int length = format.length();
lubomir@283
  1586
lubomir@283
  1587
        int processed = 0;
lubomir@283
  1588
        int paramOffset = format.indexOf('@');
lubomir@283
  1589
        while ((paramOffset != -1) && (paramOffset < (length - 1))) {
lubomir@283
  1590
            final char paramChar = format.charAt(paramOffset + 1);
lubomir@283
  1591
            if ((paramChar >= '1') && (paramChar <= '9')) {
lubomir@283
  1592
                final int paramIndex = paramChar - '0' - 1;
lubomir@283
  1593
lubomir@283
  1594
                out.append(format, processed, paramOffset);
lubomir@283
  1595
                out.append(params[paramIndex]);
lubomir@283
  1596
lubomir@283
  1597
                ++paramOffset;
lubomir@283
  1598
                processed = paramOffset + 1;
lubomir@283
  1599
            }
lubomir@283
  1600
lubomir@283
  1601
            paramOffset = format.indexOf('@', paramOffset + 1);
lubomir@283
  1602
        }
lubomir@283
  1603
lubomir@283
  1604
        out.append(format, processed, length);
lubomir@283
  1605
    }
jaroslav@398
  1606
jaroslav@398
  1607
    private void generateCatch(TrapData[] traps) throws IOException {
jaroslav@400
  1608
        out.append("} catch (e) {\n");
jaroslav@401
  1609
        int finallyPC = -1;
jaroslav@398
  1610
        for (TrapData e : traps) {
jaroslav@398
  1611
            if (e == null) {
jaroslav@398
  1612
                break;
jaroslav@398
  1613
            }
jaroslav@398
  1614
            if (e.catch_cpx != 0) { //not finally
jaroslav@398
  1615
                final String classInternalName = jc.getClassName(e.catch_cpx);
jaroslav@398
  1616
                addReference(classInternalName);
jaroslav@423
  1617
                if ("java/lang/Throwable".equals(classInternalName)) {
jaroslav@423
  1618
                    out.append("if (e.$instOf_java_lang_Throwable) {");
lubomir@474
  1619
                    out.append("  var stA0 = e;");
jaroslav@423
  1620
                    out.append("} else {");
lubomir@474
  1621
                    out.append("  var stA0 = vm.java_lang_Throwable(true);");
jaroslav@442
  1622
                    out.append("  vm.java_lang_Throwable.cons__VLjava_lang_String_2.call(stA0, e.toString());");
jaroslav@423
  1623
                    out.append("}");
jaroslav@423
  1624
                    out.append("gt=" + e.handler_pc + "; continue;");
jaroslav@423
  1625
                } else {
jaroslav@423
  1626
                    out.append("if (e.$instOf_" + classInternalName.replace('/', '_') + ") {");
lubomir@474
  1627
                    out.append("gt=" + e.handler_pc + "; var stA0 = e; continue;");
jaroslav@423
  1628
                    out.append("}\n");
jaroslav@423
  1629
                }
jaroslav@398
  1630
            } else {
jaroslav@401
  1631
                finallyPC = e.handler_pc;
jaroslav@398
  1632
            }
jaroslav@398
  1633
        }
jaroslav@401
  1634
        if (finallyPC == -1) {
jaroslav@401
  1635
            out.append("throw e;");
jaroslav@401
  1636
        } else {
lubomir@474
  1637
            out.append("gt=" + finallyPC + "; var stA0 = e; continue;");
jaroslav@401
  1638
        }
jaroslav@400
  1639
        out.append("\n}");
jaroslav@398
  1640
    }
jaroslav@0
  1641
}