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