vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 19 Jan 2013 06:07:49 +0100
changeset 488 9288ecf9657c
parent 474 859cc3a0b8f0
parent 481 472209ab1112
child 503 80a388c8c27b
permissions -rw-r--r--
Merging java.util.reflect.Array work to default branch
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
                    }
jaroslav@488
   912
                    emit(out, "var @2 = Array.prototype.newArray__Ljava_lang_Object_2ZLjava_lang_String_2I(true, '@3', @1);",
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
                    }
jaroslav@488
   924
                    emit(out, "var @2 = Array.prototype.newArray__Ljava_lang_Object_2ZLjava_lang_String_2I(false, '@3', @1);",
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);
jaroslav@481
   933
                    StringBuilder dims = new StringBuilder();
jaroslav@481
   934
                    dims.append('[');
jaroslav@481
   935
                    for (int d = 0; d < dim; d++) {
jaroslav@481
   936
                        if (d != 0) {
jaroslav@481
   937
                            dims.append(",");
jaroslav@481
   938
                        }
jaroslav@481
   939
                        dims.append(smapper.popI());
jtulach@128
   940
                    }
jaroslav@481
   941
                    dims.append(']');
jaroslav@488
   942
                    emit(out, "var @2 = Array.prototype.multiNewArray__Ljava_lang_Object_2Ljava_lang_String_2_3II('@3', @1, 0);",
jaroslav@481
   943
                         dims.toString(), smapper.pushA(), typeName);
jtulach@128
   944
                    break;
jtulach@128
   945
                }
jaroslav@151
   946
                case opc_arraylength:
lubomir@474
   947
                    emit(out, "var @2 = @1.length;",
lubomir@474
   948
                         smapper.popA(), smapper.pushI());
jaroslav@272
   949
                    break;
lubomir@283
   950
                case opc_lastore:
jaroslav@459
   951
                    emit(out, "@3.at(@2, @1);",
lubomir@307
   952
                         smapper.popL(), smapper.popI(), smapper.popA());
lubomir@281
   953
                    break;
lubomir@283
   954
                case opc_fastore:
jaroslav@459
   955
                    emit(out, "@3.at(@2, @1);",
lubomir@307
   956
                         smapper.popF(), smapper.popI(), smapper.popA());
lubomir@281
   957
                    break;
lubomir@283
   958
                case opc_dastore:
jaroslav@459
   959
                    emit(out, "@3.at(@2, @1);",
lubomir@307
   960
                         smapper.popD(), smapper.popI(), smapper.popA());
lubomir@281
   961
                    break;
lubomir@283
   962
                case opc_aastore:
jaroslav@459
   963
                    emit(out, "@3.at(@2, @1);",
lubomir@307
   964
                         smapper.popA(), smapper.popI(), smapper.popA());
jaroslav@21
   965
                    break;
jaroslav@151
   966
                case opc_iastore:
jaroslav@151
   967
                case opc_bastore:
jaroslav@151
   968
                case opc_castore:
lubomir@283
   969
                case opc_sastore:
jaroslav@459
   970
                    emit(out, "@3.at(@2, @1);",
lubomir@307
   971
                         smapper.popI(), smapper.popI(), smapper.popA());
jaroslav@240
   972
                    break;
lubomir@283
   973
                case opc_laload:
lubomir@474
   974
                    emit(out, "var @3 = @2.at(@1);",
lubomir@307
   975
                         smapper.popI(), smapper.popA(), smapper.pushL());
lubomir@281
   976
                    break;
lubomir@283
   977
                case opc_faload:
lubomir@474
   978
                    emit(out, "var @3 = @2.at(@1);",
lubomir@307
   979
                         smapper.popI(), smapper.popA(), smapper.pushF());
lubomir@281
   980
                    break;
lubomir@283
   981
                case opc_daload:
lubomir@474
   982
                    emit(out, "var @3 = @2.at(@1);",
lubomir@307
   983
                         smapper.popI(), smapper.popA(), smapper.pushD());
lubomir@281
   984
                    break;
lubomir@283
   985
                case opc_aaload:
lubomir@474
   986
                    emit(out, "var @3 = @2.at(@1);",
lubomir@307
   987
                         smapper.popI(), smapper.popA(), smapper.pushA());
jaroslav@272
   988
                    break;
jaroslav@272
   989
                case opc_iaload:
jaroslav@272
   990
                case opc_baload:
jaroslav@272
   991
                case opc_caload:
lubomir@283
   992
                case opc_saload:
lubomir@474
   993
                    emit(out, "var @3 = @2.at(@1);",
lubomir@307
   994
                         smapper.popI(), smapper.popA(), smapper.pushI());
jaroslav@272
   995
                    break;
lubomir@221
   996
                case opc_pop:
jaroslav@272
   997
                case opc_pop2:
lubomir@307
   998
                    smapper.pop(1);
jaroslav@399
   999
                    debug("/* pop */");
jaroslav@272
  1000
                    break;
lubomir@281
  1001
                case opc_dup: {
lubomir@307
  1002
                    final Variable v = smapper.get(0);
lubomir@474
  1003
                    emit(out, "var @1 = @2;", smapper.pushT(v.getType()), v);
jaroslav@21
  1004
                    break;
jaroslav@21
  1005
                }
lubomir@281
  1006
                case opc_dup2: {
lubomir@307
  1007
                    if (smapper.get(0).isCategory2()) {
lubomir@307
  1008
                        final Variable v = smapper.get(0);
lubomir@474
  1009
                        emit(out, "var @1 = @2;",
lubomir@474
  1010
                             smapper.pushT(v.getType()), v);
lubomir@281
  1011
                    } else {
lubomir@307
  1012
                        final Variable v1 = smapper.get(0);
lubomir@307
  1013
                        final Variable v2 = smapper.get(1);
lubomir@474
  1014
                        emit(out, "var @1 = @2, @3 = @4;",
lubomir@307
  1015
                             smapper.pushT(v2.getType()), v2,
lubomir@307
  1016
                             smapper.pushT(v1.getType()), v1);
lubomir@281
  1017
                    }
jaroslav@21
  1018
                    break;
jaroslav@21
  1019
                }
lubomir@281
  1020
                case opc_dup_x1: {
lubomir@307
  1021
                    final Variable vi1 = smapper.pop();
lubomir@307
  1022
                    final Variable vi2 = smapper.pop();
lubomir@307
  1023
                    final Variable vo3 = smapper.pushT(vi1.getType());
lubomir@307
  1024
                    final Variable vo2 = smapper.pushT(vi2.getType());
lubomir@307
  1025
                    final Variable vo1 = smapper.pushT(vi1.getType());
lubomir@281
  1026
lubomir@474
  1027
                    emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
lubomir@283
  1028
                         vo1, vi1, vo2, vi2, vo3, vo1);
jaroslav@93
  1029
                    break;
lubomir@281
  1030
                }
lubomir@281
  1031
                case opc_dup_x2: {
lubomir@307
  1032
                    if (smapper.get(1).isCategory2()) {
lubomir@307
  1033
                        final Variable vi1 = smapper.pop();
lubomir@307
  1034
                        final Variable vi2 = smapper.pop();
lubomir@307
  1035
                        final Variable vo3 = smapper.pushT(vi1.getType());
lubomir@307
  1036
                        final Variable vo2 = smapper.pushT(vi2.getType());
lubomir@307
  1037
                        final Variable vo1 = smapper.pushT(vi1.getType());
lubomir@281
  1038
lubomir@474
  1039
                        emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
lubomir@283
  1040
                             vo1, vi1, vo2, vi2, vo3, vo1);
lubomir@281
  1041
                    } else {
lubomir@307
  1042
                        final Variable vi1 = smapper.pop();
lubomir@307
  1043
                        final Variable vi2 = smapper.pop();
lubomir@307
  1044
                        final Variable vi3 = smapper.pop();
lubomir@307
  1045
                        final Variable vo4 = smapper.pushT(vi1.getType());
lubomir@307
  1046
                        final Variable vo3 = smapper.pushT(vi3.getType());
lubomir@307
  1047
                        final Variable vo2 = smapper.pushT(vi2.getType());
lubomir@307
  1048
                        final Variable vo1 = smapper.pushT(vi1.getType());
lubomir@281
  1049
lubomir@474
  1050
                        emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8;",
lubomir@283
  1051
                             vo1, vi1, vo2, vi2, vo3, vi3, vo4, vo1);
lubomir@281
  1052
                    }
jaroslav@8
  1053
                    break;
lubomir@281
  1054
                }
jaroslav@151
  1055
                case opc_bipush:
lubomir@474
  1056
                    emit(out, "var @1 = @2;",
lubomir@307
  1057
                         smapper.pushI(), Integer.toString(byteCodes[++i]));
jaroslav@8
  1058
                    break;
jaroslav@151
  1059
                case opc_sipush:
lubomir@474
  1060
                    emit(out, "var @1 = @2;",
lubomir@307
  1061
                         smapper.pushI(),
lubomir@283
  1062
                         Integer.toString(readIntArg(byteCodes, i)));
jaroslav@31
  1063
                    i += 2;
jaroslav@31
  1064
                    break;
jaroslav@151
  1065
                case opc_getfield: {
jaroslav@8
  1066
                    int indx = readIntArg(byteCodes, i);
jaroslav@151
  1067
                    String[] fi = jc.getFieldInfoName(indx);
lubomir@307
  1068
                    final int type = VarType.fromFieldType(fi[2].charAt(0));
lubomir@474
  1069
                    emit(out, "var @2 = @1.fld_@3;",
lubomir@307
  1070
                         smapper.popA(), smapper.pushT(type), fi[1]);
jaroslav@8
  1071
                    i += 2;
jaroslav@8
  1072
                    break;
jaroslav@8
  1073
                }
jaroslav@151
  1074
                case opc_getstatic: {
jaroslav@9
  1075
                    int indx = readIntArg(byteCodes, i);
jaroslav@151
  1076
                    String[] fi = jc.getFieldInfoName(indx);
lubomir@307
  1077
                    final int type = VarType.fromFieldType(fi[2].charAt(0));
lubomir@474
  1078
                    emit(out, "var @1 = @2(false).constructor.@3;",
lubomir@317
  1079
                         smapper.pushT(type),
lubomir@317
  1080
                         accessClass(fi[0].replace('/', '_')), fi[1]);
jaroslav@9
  1081
                    i += 2;
jaroslav@151
  1082
                    addReference(fi[0]);
jaroslav@9
  1083
                    break;
jaroslav@9
  1084
                }
jaroslav@151
  1085
                case opc_putfield: {
jaroslav@10
  1086
                    int indx = readIntArg(byteCodes, i);
jaroslav@151
  1087
                    String[] fi = jc.getFieldInfoName(indx);
lubomir@307
  1088
                    final int type = VarType.fromFieldType(fi[2].charAt(0));
lubomir@283
  1089
                    emit(out, "@2.fld_@3 = @1;",
lubomir@307
  1090
                         smapper.popT(type), smapper.popA(), fi[1]);
jaroslav@10
  1091
                    i += 2;
jaroslav@10
  1092
                    break;
jaroslav@10
  1093
                }
lubomir@281
  1094
                case opc_putstatic: {
lubomir@281
  1095
                    int indx = readIntArg(byteCodes, i);
lubomir@281
  1096
                    String[] fi = jc.getFieldInfoName(indx);
lubomir@307
  1097
                    final int type = VarType.fromFieldType(fi[2].charAt(0));
jaroslav@358
  1098
                    emit(out, "@1(false).constructor.@2 = @3;",
lubomir@317
  1099
                         accessClass(fi[0].replace('/', '_')), fi[1],
lubomir@317
  1100
                         smapper.popT(type));
lubomir@281
  1101
                    i += 2;
lubomir@281
  1102
                    addReference(fi[0]);
lubomir@281
  1103
                    break;
lubomir@281
  1104
                }
jaroslav@151
  1105
                case opc_checkcast: {
jaroslav@30
  1106
                    int indx = readIntArg(byteCodes, i);
jaroslav@151
  1107
                    final String type = jc.getClassName(indx);
jaroslav@42
  1108
                    if (!type.startsWith("[")) {
jaroslav@42
  1109
                        // no way to check arrays right now
lubomir@283
  1110
                        // XXX proper exception
lubomir@317
  1111
                        emit(out,
lubomir@317
  1112
                             "if (@1 !== null && !@1.$instOf_@2) throw {};",
lubomir@307
  1113
                             smapper.getA(0), type.replace('/', '_'));
jaroslav@42
  1114
                    }
jaroslav@30
  1115
                    i += 2;
jaroslav@30
  1116
                    break;
jaroslav@30
  1117
                }
jaroslav@151
  1118
                case opc_instanceof: {
jaroslav@17
  1119
                    int indx = readIntArg(byteCodes, i);
jaroslav@151
  1120
                    final String type = jc.getClassName(indx);
lubomir@474
  1121
                    emit(out, "var @2 = @1.$instOf_@3 ? 1 : 0;",
lubomir@317
  1122
                         smapper.popA(), smapper.pushI(),
lubomir@317
  1123
                         type.replace('/', '_'));
jaroslav@17
  1124
                    i += 2;
jaroslav@30
  1125
                    break;
jaroslav@17
  1126
                }
jaroslav@151
  1127
                case opc_athrow: {
lubomir@307
  1128
                    final Variable v = smapper.popA();
lubomir@307
  1129
                    smapper.clear();
lubomir@281
  1130
lubomir@474
  1131
                    emit(out, "{ var @1 = @2; throw @2; }",
lubomir@307
  1132
                         smapper.pushA(), v);
jaroslav@104
  1133
                    break;
jaroslav@104
  1134
                }
lubomir@221
  1135
lubomir@221
  1136
                case opc_monitorenter: {
lubomir@221
  1137
                    out.append("/* monitor enter */");
lubomir@307
  1138
                    smapper.popA();
lubomir@221
  1139
                    break;
lubomir@221
  1140
                }
lubomir@221
  1141
lubomir@221
  1142
                case opc_monitorexit: {
lubomir@221
  1143
                    out.append("/* monitor exit */");
lubomir@307
  1144
                    smapper.popA();
lubomir@221
  1145
                    break;
lubomir@221
  1146
                }
lubomir@221
  1147
jaroslav@104
  1148
                default: {
lubomir@283
  1149
                    emit(out, "throw 'unknown bytecode @1';",
lubomir@283
  1150
                         Integer.toString(c));
jaroslav@104
  1151
                }
jaroslav@0
  1152
            }
jaroslav@399
  1153
            if (debug(" //")) {
jaroslav@399
  1154
                for (int j = prev; j <= i; j++) {
jaroslav@399
  1155
                    out.append(" ");
jaroslav@399
  1156
                    final int cc = readByte(byteCodes, j);
jaroslav@399
  1157
                    out.append(Integer.toString(cc));
tzezula@285
  1158
                }
jaroslav@0
  1159
            }
tzezula@285
  1160
            out.append("\n");            
jaroslav@0
  1161
        }
jaroslav@398
  1162
        if (previousTrap != null) {
jaroslav@398
  1163
            generateCatch(previousTrap);
jaroslav@398
  1164
        }
jaroslav@10
  1165
        out.append("  }\n");
lubomir@307
  1166
        out.append("};");
jaroslav@4
  1167
    }
jaroslav@4
  1168
lubomir@281
  1169
    private int generateIf(byte[] byteCodes, int i,
lubomir@281
  1170
                           final Variable v2, final Variable v1,
lubomir@281
  1171
                           final String test) throws IOException {
jaroslav@4
  1172
        int indx = i + readIntArg(byteCodes, i);
lubomir@281
  1173
        out.append("if (").append(v1)
lubomir@221
  1174
           .append(' ').append(test).append(' ')
lubomir@281
  1175
           .append(v2).append(") { gt = " + indx)
lubomir@221
  1176
           .append("; continue; }");
jaroslav@4
  1177
        return i + 2;
jaroslav@4
  1178
    }
jaroslav@4
  1179
jaroslav@4
  1180
    private int readIntArg(byte[] byteCodes, int offsetInstruction) {
jaroslav@5
  1181
        final int indxHi = byteCodes[offsetInstruction + 1] << 8;
jaroslav@5
  1182
        final int indxLo = byteCodes[offsetInstruction + 2];
jaroslav@5
  1183
        return (indxHi & 0xffffff00) | (indxLo & 0xff);
jaroslav@4
  1184
    }
jaroslav@115
  1185
    private int readInt4(byte[] byteCodes, int offsetInstruction) {
jaroslav@115
  1186
        final int d = byteCodes[offsetInstruction + 0] << 24;
jaroslav@115
  1187
        final int c = byteCodes[offsetInstruction + 1] << 16;
jaroslav@115
  1188
        final int b = byteCodes[offsetInstruction + 2] << 8;
jaroslav@115
  1189
        final int a = byteCodes[offsetInstruction + 3];
jaroslav@115
  1190
        return (d & 0xff000000) | (c & 0xff0000) | (b & 0xff00) | (a & 0xff);
jaroslav@115
  1191
    }
jtulach@128
  1192
    private int readByte(byte[] byteCodes, int offsetInstruction) {
lubomir@221
  1193
        return byteCodes[offsetInstruction] & 0xff;
jtulach@128
  1194
    }
jaroslav@4
  1195
    
lubomir@281
  1196
    private static void countArgs(String descriptor, char[] returnType, StringBuilder sig, StringBuilder cnt) {
jaroslav@4
  1197
        int i = 0;
jaroslav@4
  1198
        Boolean count = null;
jaroslav@32
  1199
        boolean array = false;
jaroslav@248
  1200
        sig.append("__");
jaroslav@10
  1201
        int firstPos = sig.length();
jaroslav@4
  1202
        while (i < descriptor.length()) {
jaroslav@4
  1203
            char ch = descriptor.charAt(i++);
jaroslav@4
  1204
            switch (ch) {
jaroslav@4
  1205
                case '(':
jaroslav@4
  1206
                    count = true;
jaroslav@4
  1207
                    continue;
jaroslav@4
  1208
                case ')':
jaroslav@4
  1209
                    count = false;
jaroslav@4
  1210
                    continue;
jaroslav@4
  1211
                case 'B': 
jaroslav@4
  1212
                case 'C': 
jaroslav@4
  1213
                case 'D': 
jaroslav@4
  1214
                case 'F': 
jaroslav@4
  1215
                case 'I': 
jaroslav@4
  1216
                case 'J': 
jaroslav@4
  1217
                case 'S': 
jaroslav@4
  1218
                case 'Z': 
jaroslav@4
  1219
                    if (count) {
jaroslav@32
  1220
                        if (array) {
jaroslav@248
  1221
                            sig.append("_3");
jaroslav@32
  1222
                        }
jaroslav@4
  1223
                        sig.append(ch);
jtulach@156
  1224
                        if (ch == 'J' || ch == 'D') {
jtulach@156
  1225
                            cnt.append('1');
jtulach@156
  1226
                        } else {
jtulach@156
  1227
                            cnt.append('0');
jtulach@156
  1228
                        }
jaroslav@4
  1229
                    } else {
jaroslav@10
  1230
                        sig.insert(firstPos, ch);
jaroslav@32
  1231
                        if (array) {
lubomir@281
  1232
                            returnType[0] = '[';
jaroslav@248
  1233
                            sig.insert(firstPos, "_3");
lubomir@281
  1234
                        } else {
lubomir@281
  1235
                            returnType[0] = ch;
jaroslav@32
  1236
                        }
jaroslav@4
  1237
                    }
jaroslav@93
  1238
                    array = false;
jaroslav@4
  1239
                    continue;
jaroslav@4
  1240
                case 'V': 
jaroslav@4
  1241
                    assert !count;
lubomir@281
  1242
                    returnType[0] = 'V';
jaroslav@10
  1243
                    sig.insert(firstPos, 'V');
jaroslav@4
  1244
                    continue;
jaroslav@4
  1245
                case 'L':
jaroslav@16
  1246
                    int next = descriptor.indexOf(';', i);
jaroslav@248
  1247
                    String realSig = mangleSig(descriptor, i - 1, next + 1);
jaroslav@4
  1248
                    if (count) {
jaroslav@32
  1249
                        if (array) {
jaroslav@248
  1250
                            sig.append("_3");
jaroslav@32
  1251
                        }
jaroslav@248
  1252
                        sig.append(realSig);
jtulach@156
  1253
                        cnt.append('0');
jaroslav@4
  1254
                    } else {
jaroslav@248
  1255
                        sig.insert(firstPos, realSig);
jaroslav@32
  1256
                        if (array) {
jaroslav@248
  1257
                            sig.insert(firstPos, "_3");
jaroslav@32
  1258
                        }
lubomir@281
  1259
                        returnType[0] = 'L';
jaroslav@4
  1260
                    }
jaroslav@16
  1261
                    i = next + 1;
lubomir@407
  1262
                    array = false;
jaroslav@4
  1263
                    continue;
jaroslav@4
  1264
                case '[':
jaroslav@248
  1265
                    array = true;
jaroslav@4
  1266
                    continue;
jaroslav@4
  1267
                default:
jaroslav@248
  1268
                    throw new IllegalStateException("Invalid char: " + ch);
jaroslav@4
  1269
            }
jaroslav@4
  1270
        }
jaroslav@0
  1271
    }
jaroslav@248
  1272
    
jaroslav@248
  1273
    private static String mangleSig(String txt, int first, int last) {
jaroslav@248
  1274
        StringBuilder sb = new StringBuilder();
jaroslav@248
  1275
        for (int i = first; i < last; i++) {
jaroslav@248
  1276
            final char ch = txt.charAt(i);
jaroslav@248
  1277
            switch (ch) {
jaroslav@248
  1278
                case '/': sb.append('_'); break;
jaroslav@248
  1279
                case '_': sb.append("_1"); break;
jaroslav@248
  1280
                case ';': sb.append("_2"); break;
jaroslav@248
  1281
                case '[': sb.append("_3"); break;
jaroslav@248
  1282
                default: sb.append(ch); break;
jaroslav@248
  1283
            }
jaroslav@248
  1284
        }
jaroslav@248
  1285
        return sb.toString();
jaroslav@248
  1286
    }
jaroslav@9
  1287
jaroslav@248
  1288
    private static String findMethodName(MethodData m, StringBuilder cnt) {
jaroslav@42
  1289
        StringBuilder name = new StringBuilder();
jaroslav@10
  1290
        if ("<init>".equals(m.getName())) { // NOI18N
jaroslav@42
  1291
            name.append("cons"); // NOI18N
jaroslav@19
  1292
        } else if ("<clinit>".equals(m.getName())) { // NOI18N
jaroslav@42
  1293
            name.append("class"); // NOI18N
jaroslav@10
  1294
        } else {
jaroslav@42
  1295
            name.append(m.getName());
jaroslav@10
  1296
        } 
jaroslav@42
  1297
        
lubomir@282
  1298
        countArgs(m.getInternalSig(), new char[1], name, cnt);
jaroslav@42
  1299
        return name.toString();
jaroslav@10
  1300
    }
jaroslav@10
  1301
lubomir@282
  1302
    static String findMethodName(String[] mi, StringBuilder cnt, char[] returnType) {
jaroslav@10
  1303
        StringBuilder name = new StringBuilder();
jaroslav@151
  1304
        String descr = mi[2];//mi.getDescriptor();
jaroslav@151
  1305
        String nm= mi[1];
jaroslav@151
  1306
        if ("<init>".equals(nm)) { // NOI18N
jaroslav@10
  1307
            name.append("cons"); // NOI18N
jaroslav@10
  1308
        } else {
jaroslav@151
  1309
            name.append(nm);
jaroslav@10
  1310
        }
lubomir@282
  1311
        countArgs(descr, returnType, name, cnt);
jaroslav@10
  1312
        return name.toString();
jaroslav@10
  1313
    }
jaroslav@10
  1314
lubomir@307
  1315
    private int invokeStaticMethod(byte[] byteCodes, int i, final StackMapper mapper, boolean isStatic)
jaroslav@10
  1316
    throws IOException {
jaroslav@10
  1317
        int methodIndex = readIntArg(byteCodes, i);
jaroslav@151
  1318
        String[] mi = jc.getFieldInfoName(methodIndex);
lubomir@281
  1319
        char[] returnType = { 'V' };
jtulach@156
  1320
        StringBuilder cnt = new StringBuilder();
lubomir@281
  1321
        String mn = findMethodName(mi, cnt, returnType);
lubomir@221
  1322
lubomir@221
  1323
        final int numArguments = isStatic ? cnt.length() : cnt.length() + 1;
lubomir@281
  1324
        final Variable[] vars = new Variable[numArguments];
lubomir@221
  1325
lubomir@281
  1326
        for (int j = numArguments - 1; j >= 0; --j) {
lubomir@281
  1327
            vars[j] = mapper.pop();
jaroslav@11
  1328
        }
lubomir@281
  1329
lubomir@281
  1330
        if (returnType[0] != 'V') {
lubomir@474
  1331
            out.append("var ")
lubomir@474
  1332
               .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
lubomir@281
  1333
               .append(" = ");
jaroslav@10
  1334
        }
lubomir@221
  1335
jaroslav@151
  1336
        final String in = mi[0];
jaroslav@274
  1337
        out.append(accessClass(in.replace('/', '_')));
jaroslav@224
  1338
        out.append("(false).");
jaroslav@397
  1339
        if (mn.startsWith("cons_")) {
jaroslav@397
  1340
            out.append("constructor.");
jaroslav@397
  1341
        }
jaroslav@10
  1342
        out.append(mn);
jaroslav@442
  1343
        if (isStatic) {
jaroslav@442
  1344
            out.append('(');
jaroslav@442
  1345
        } else {
jaroslav@442
  1346
            out.append(".call(");
jaroslav@442
  1347
        }
lubomir@221
  1348
        if (numArguments > 0) {
lubomir@281
  1349
            out.append(vars[0]);
lubomir@281
  1350
            for (int j = 1; j < numArguments; ++j) {
lubomir@221
  1351
                out.append(", ");
lubomir@281
  1352
                out.append(vars[j]);
lubomir@221
  1353
            }
jaroslav@10
  1354
        }
lubomir@221
  1355
        out.append(");");
jaroslav@10
  1356
        i += 2;
jaroslav@18
  1357
        addReference(in);
jaroslav@10
  1358
        return i;
jaroslav@10
  1359
    }
lubomir@307
  1360
    private int invokeVirtualMethod(byte[] byteCodes, int i, final StackMapper mapper)
jaroslav@12
  1361
    throws IOException {
jaroslav@12
  1362
        int methodIndex = readIntArg(byteCodes, i);
jaroslav@151
  1363
        String[] mi = jc.getFieldInfoName(methodIndex);
lubomir@281
  1364
        char[] returnType = { 'V' };
jtulach@156
  1365
        StringBuilder cnt = new StringBuilder();
lubomir@281
  1366
        String mn = findMethodName(mi, cnt, returnType);
lubomir@221
  1367
lubomir@281
  1368
        final int numArguments = cnt.length() + 1;
lubomir@281
  1369
        final Variable[] vars = new Variable[numArguments];
lubomir@221
  1370
lubomir@281
  1371
        for (int j = numArguments - 1; j >= 0; --j) {
lubomir@281
  1372
            vars[j] = mapper.pop();
jaroslav@12
  1373
        }
lubomir@221
  1374
lubomir@281
  1375
        if (returnType[0] != 'V') {
lubomir@474
  1376
            out.append("var ")
lubomir@474
  1377
               .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
lubomir@281
  1378
               .append(" = ");
jaroslav@12
  1379
        }
lubomir@281
  1380
lubomir@281
  1381
        out.append(vars[0]).append('.');
jaroslav@12
  1382
        out.append(mn);
jaroslav@12
  1383
        out.append('(');
jaroslav@442
  1384
        String sep = "";
lubomir@281
  1385
        for (int j = 1; j < numArguments; ++j) {
jaroslav@442
  1386
            out.append(sep);
lubomir@281
  1387
            out.append(vars[j]);
jaroslav@442
  1388
            sep = ", ";
jaroslav@12
  1389
        }
lubomir@221
  1390
        out.append(");");
jaroslav@12
  1391
        i += 2;
jaroslav@12
  1392
        return i;
jaroslav@12
  1393
    }
lubomir@221
  1394
jaroslav@103
  1395
    private void addReference(String cn) throws IOException {
jtulach@162
  1396
        if (requireReference(cn)) {
jaroslav@399
  1397
            debug(" /* needs " + cn + " */");
jaroslav@18
  1398
        }
jaroslav@18
  1399
    }
jaroslav@16
  1400
jaroslav@33
  1401
    private void outType(String d, StringBuilder out) {
jaroslav@33
  1402
        int arr = 0;
jaroslav@33
  1403
        while (d.charAt(0) == '[') {
jaroslav@33
  1404
            out.append('A');
jaroslav@33
  1405
            d = d.substring(1);
jaroslav@33
  1406
        }
jaroslav@16
  1407
        if (d.charAt(0) == 'L') {
jaroslav@16
  1408
            assert d.charAt(d.length() - 1) == ';';
jaroslav@16
  1409
            out.append(d.replace('/', '_').substring(0, d.length() - 1));
jaroslav@16
  1410
        } else {
jaroslav@16
  1411
            out.append(d);
jaroslav@16
  1412
        }
jaroslav@16
  1413
    }
jaroslav@21
  1414
jaroslav@230
  1415
    private String encodeConstant(int entryIndex) throws IOException {
jaroslav@230
  1416
        String[] classRef = { null };
jaroslav@230
  1417
        String s = jc.stringValue(entryIndex, classRef);
jaroslav@230
  1418
        if (classRef[0] != null) {
jaroslav@230
  1419
            addReference(classRef[0]);
jaroslav@274
  1420
            s = accessClass(s.replace('/', '_')) + "(false).constructor.$class";
jaroslav@230
  1421
        }
jaroslav@151
  1422
        return s;
jaroslav@21
  1423
    }
jaroslav@32
  1424
jaroslav@266
  1425
    private String javaScriptBody(String prefix, MethodData m, boolean isStatic) throws IOException {
jaroslav@152
  1426
        byte[] arr = m.findAnnotationData(true);
jaroslav@152
  1427
        if (arr == null) {
jaroslav@266
  1428
            return null;
jaroslav@152
  1429
        }
jaroslav@200
  1430
        final String jvmType = "Lorg/apidesign/bck2brwsr/core/JavaScriptBody;";
jaroslav@152
  1431
        class P extends AnnotationParser {
jaroslav@237
  1432
            public P() {
jaroslav@237
  1433
                super(false);
jaroslav@237
  1434
            }
jaroslav@237
  1435
            
jaroslav@152
  1436
            int cnt;
jaroslav@152
  1437
            String[] args = new String[30];
jaroslav@152
  1438
            String body;
jaroslav@94
  1439
            
jaroslav@152
  1440
            @Override
jaroslav@236
  1441
            protected void visitAttr(String type, String attr, String at, String value) {
jaroslav@152
  1442
                if (type.equals(jvmType)) {
jaroslav@152
  1443
                    if ("body".equals(attr)) {
jaroslav@152
  1444
                        body = value;
jaroslav@152
  1445
                    } else if ("args".equals(attr)) {
jaroslav@152
  1446
                        args[cnt++] = value;
jaroslav@152
  1447
                    } else {
jaroslav@152
  1448
                        throw new IllegalArgumentException(attr);
jaroslav@152
  1449
                    }
jaroslav@152
  1450
                }
jaroslav@94
  1451
            }
jaroslav@94
  1452
        }
jaroslav@152
  1453
        P p = new P();
jaroslav@152
  1454
        p.parse(arr, jc);
jaroslav@152
  1455
        if (p.body == null) {
jaroslav@266
  1456
            return null;
jaroslav@152
  1457
        }
jtulach@156
  1458
        StringBuilder cnt = new StringBuilder();
jaroslav@266
  1459
        final String mn = findMethodName(m, cnt);
jaroslav@266
  1460
        out.append(prefix).append(mn);
jaroslav@203
  1461
        out.append(" = function(");
jaroslav@442
  1462
        String space = "";
jaroslav@443
  1463
        int index = 0;
jtulach@156
  1464
        for (int i = 0; i < cnt.length(); i++) {
jaroslav@152
  1465
            out.append(space);
jaroslav@316
  1466
            space = outputArg(out, p.args, index);
jaroslav@152
  1467
            index++;
jaroslav@152
  1468
        }
jaroslav@152
  1469
        out.append(") {").append("\n");
jaroslav@152
  1470
        out.append(p.body);
jaroslav@152
  1471
        out.append("\n}\n");
jaroslav@266
  1472
        return mn;
jaroslav@151
  1473
    }
jaroslav@151
  1474
    private static String className(ClassData jc) {
jaroslav@151
  1475
        //return jc.getName().getInternalName().replace('/', '_');
jaroslav@151
  1476
        return jc.getClassName().replace('/', '_');
jaroslav@94
  1477
    }
jaroslav@152
  1478
    
jaroslav@152
  1479
    private static String[] findAnnotation(
jaroslav@152
  1480
        byte[] arr, ClassData cd, final String className, 
jaroslav@152
  1481
        final String... attrNames
jaroslav@152
  1482
    ) throws IOException {
jaroslav@152
  1483
        if (arr == null) {
jaroslav@152
  1484
            return null;
jaroslav@152
  1485
        }
jaroslav@152
  1486
        final String[] values = new String[attrNames.length];
jaroslav@152
  1487
        final boolean[] found = { false };
jaroslav@152
  1488
        final String jvmType = "L" + className.replace('.', '/') + ";";
jaroslav@237
  1489
        AnnotationParser ap = new AnnotationParser(false) {
jaroslav@152
  1490
            @Override
jaroslav@236
  1491
            protected void visitAttr(String type, String attr, String at, String value) {
jaroslav@152
  1492
                if (type.equals(jvmType)) {
jaroslav@152
  1493
                    found[0] = true;
jaroslav@152
  1494
                    for (int i = 0; i < attrNames.length; i++) {
jaroslav@152
  1495
                        if (attrNames[i].equals(attr)) {
jaroslav@152
  1496
                            values[i] = value;
jaroslav@152
  1497
                        }
jaroslav@152
  1498
                    }
jaroslav@152
  1499
                }
jaroslav@152
  1500
            }
jaroslav@152
  1501
            
jaroslav@152
  1502
        };
jaroslav@152
  1503
        ap.parse(arr, cd);
jaroslav@152
  1504
        return found[0] ? values : null;
jaroslav@152
  1505
    }
jaroslav@173
  1506
jaroslav@173
  1507
    private CharSequence initField(FieldData v) {
jaroslav@173
  1508
        final String is = v.getInternalSig();
jaroslav@173
  1509
        if (is.length() == 1) {
jaroslav@173
  1510
            switch (is.charAt(0)) {
jaroslav@173
  1511
                case 'S':
jaroslav@173
  1512
                case 'J':
jaroslav@173
  1513
                case 'B':
jaroslav@173
  1514
                case 'Z':
jaroslav@173
  1515
                case 'C':
jaroslav@173
  1516
                case 'I': return " = 0;";
jaroslav@173
  1517
                case 'F': 
jaroslav@180
  1518
                case 'D': return " = 0.0;";
jaroslav@173
  1519
                default:
jaroslav@173
  1520
                    throw new IllegalStateException(is);
jaroslav@173
  1521
            }
jaroslav@173
  1522
        }
jaroslav@173
  1523
        return " = null;";
jaroslav@173
  1524
    }
jaroslav@235
  1525
jaroslav@235
  1526
    private static void generateAnno(ClassData cd, final Appendable out, byte[] data) throws IOException {
jaroslav@237
  1527
        AnnotationParser ap = new AnnotationParser(true) {
jaroslav@237
  1528
            int anno;
jaroslav@235
  1529
            int cnt;
jaroslav@235
  1530
            
jaroslav@235
  1531
            @Override
jaroslav@235
  1532
            protected void visitAnnotationStart(String type) throws IOException {
jaroslav@237
  1533
                if (anno++ > 0) {
jaroslav@237
  1534
                    out.append(",");
jaroslav@237
  1535
                }
jaroslav@235
  1536
                out.append('"').append(type).append("\" : {\n");
jaroslav@235
  1537
                cnt = 0;
jaroslav@235
  1538
            }
jaroslav@235
  1539
jaroslav@235
  1540
            @Override
jaroslav@235
  1541
            protected void visitAnnotationEnd(String type) throws IOException {
jaroslav@235
  1542
                out.append("\n}\n");
jaroslav@235
  1543
            }
jaroslav@235
  1544
            
jaroslav@235
  1545
            @Override
jaroslav@236
  1546
            protected void visitAttr(String type, String attr, String attrType, String value) 
jaroslav@235
  1547
            throws IOException {
jaroslav@266
  1548
                if (attr == null) {
jaroslav@266
  1549
                    return;
jaroslav@266
  1550
                }
jaroslav@235
  1551
                if (cnt++ > 0) {
jaroslav@235
  1552
                    out.append(",\n");
jaroslav@235
  1553
                }
jaroslav@250
  1554
                out.append(attr).append("__").append(attrType).append(" : ").append(value);
jaroslav@235
  1555
            }
jaroslav@235
  1556
        };
jaroslav@235
  1557
        ap.parse(data, cd);
jaroslav@235
  1558
    }
jaroslav@316
  1559
jaroslav@316
  1560
    private static String outputArg(Appendable out, String[] args, int indx) throws IOException {
jaroslav@316
  1561
        final String name = args[indx];
jaroslav@316
  1562
        if (name == null) {
jaroslav@316
  1563
            return "";
jaroslav@316
  1564
        }
jaroslav@316
  1565
        if (name.contains(",")) {
jaroslav@316
  1566
            throw new IOException("Wrong parameter with ',': " + name);
jaroslav@316
  1567
        }
jaroslav@316
  1568
        out.append(name);
jaroslav@316
  1569
        return ",";
jaroslav@316
  1570
    }
lubomir@317
  1571
lubomir@283
  1572
    private static void emit(final Appendable out,
lubomir@283
  1573
                             final String format,
lubomir@283
  1574
                             final CharSequence... params) throws IOException {
lubomir@283
  1575
        final int length = format.length();
lubomir@283
  1576
lubomir@283
  1577
        int processed = 0;
lubomir@283
  1578
        int paramOffset = format.indexOf('@');
lubomir@283
  1579
        while ((paramOffset != -1) && (paramOffset < (length - 1))) {
lubomir@283
  1580
            final char paramChar = format.charAt(paramOffset + 1);
lubomir@283
  1581
            if ((paramChar >= '1') && (paramChar <= '9')) {
lubomir@283
  1582
                final int paramIndex = paramChar - '0' - 1;
lubomir@283
  1583
lubomir@283
  1584
                out.append(format, processed, paramOffset);
lubomir@283
  1585
                out.append(params[paramIndex]);
lubomir@283
  1586
lubomir@283
  1587
                ++paramOffset;
lubomir@283
  1588
                processed = paramOffset + 1;
lubomir@283
  1589
            }
lubomir@283
  1590
lubomir@283
  1591
            paramOffset = format.indexOf('@', paramOffset + 1);
lubomir@283
  1592
        }
lubomir@283
  1593
lubomir@283
  1594
        out.append(format, processed, length);
lubomir@283
  1595
    }
jaroslav@398
  1596
jaroslav@398
  1597
    private void generateCatch(TrapData[] traps) throws IOException {
jaroslav@400
  1598
        out.append("} catch (e) {\n");
jaroslav@401
  1599
        int finallyPC = -1;
jaroslav@398
  1600
        for (TrapData e : traps) {
jaroslav@398
  1601
            if (e == null) {
jaroslav@398
  1602
                break;
jaroslav@398
  1603
            }
jaroslav@398
  1604
            if (e.catch_cpx != 0) { //not finally
jaroslav@398
  1605
                final String classInternalName = jc.getClassName(e.catch_cpx);
jaroslav@398
  1606
                addReference(classInternalName);
jaroslav@423
  1607
                if ("java/lang/Throwable".equals(classInternalName)) {
jaroslav@423
  1608
                    out.append("if (e.$instOf_java_lang_Throwable) {");
lubomir@474
  1609
                    out.append("  var stA0 = e;");
jaroslav@423
  1610
                    out.append("} else {");
lubomir@474
  1611
                    out.append("  var stA0 = vm.java_lang_Throwable(true);");
jaroslav@442
  1612
                    out.append("  vm.java_lang_Throwable.cons__VLjava_lang_String_2.call(stA0, e.toString());");
jaroslav@423
  1613
                    out.append("}");
jaroslav@423
  1614
                    out.append("gt=" + e.handler_pc + "; continue;");
jaroslav@423
  1615
                } else {
jaroslav@423
  1616
                    out.append("if (e.$instOf_" + classInternalName.replace('/', '_') + ") {");
lubomir@474
  1617
                    out.append("gt=" + e.handler_pc + "; var stA0 = e; continue;");
jaroslav@423
  1618
                    out.append("}\n");
jaroslav@423
  1619
                }
jaroslav@398
  1620
            } else {
jaroslav@401
  1621
                finallyPC = e.handler_pc;
jaroslav@398
  1622
            }
jaroslav@398
  1623
        }
jaroslav@401
  1624
        if (finallyPC == -1) {
jaroslav@401
  1625
            out.append("throw e;");
jaroslav@401
  1626
        } else {
lubomir@474
  1627
            out.append("gt=" + finallyPC + "; var stA0 = e; continue;");
jaroslav@401
  1628
        }
jaroslav@400
  1629
        out.append("\n}");
jaroslav@398
  1630
    }
jaroslav@0
  1631
}