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