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