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