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