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