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