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