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