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