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