rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 15 Feb 2014 14:36:43 +0100
branchReducedStack
changeset 1453 e046cfcb8f00
parent 1427 38f80da886d7
child 1455 398911a8f401
permissions -rw-r--r--
Centralizing the stack pushes into single assign method
jaroslav@106
     1
/**
jaroslav@106
     2
 * Back 2 Browser Bytecode Translator
jaroslav@106
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@106
     4
 *
jaroslav@106
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@106
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@106
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@106
     8
 *
jaroslav@106
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@106
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@106
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@106
    12
 * GNU General Public License for more details.
jaroslav@106
    13
 *
jaroslav@106
    14
 * You should have received a copy of the GNU General Public License
jaroslav@106
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@106
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@106
    17
 */
jaroslav@22
    18
package org.apidesign.vm4brwsr;
jaroslav@0
    19
jaroslav@0
    20
import java.io.IOException;
jaroslav@0
    21
import java.io.InputStream;
jaroslav@810
    22
import static org.apidesign.vm4brwsr.ByteCodeParser.*;
jaroslav@0
    23
jaroslav@0
    24
/** Translator of the code inside class files to JavaScript.
jaroslav@0
    25
 *
jaroslav@0
    26
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@0
    27
 */
jaroslav@298
    28
abstract class ByteCodeToJavaScript {
jtulach@162
    29
    private ClassData jc;
jaroslav@272
    30
    final Appendable out;
lubomir@869
    31
    final ObfuscationDelegate obfuscationDelegate;
jaroslav@0
    32
jtulach@162
    33
    protected ByteCodeToJavaScript(Appendable out) {
lubomir@869
    34
        this(out, ObfuscationDelegate.NULL);
lubomir@869
    35
    }
lubomir@869
    36
lubomir@869
    37
    protected ByteCodeToJavaScript(
lubomir@869
    38
            Appendable out, ObfuscationDelegate obfuscationDelegate) {
jaroslav@0
    39
        this.out = out;
lubomir@869
    40
        this.obfuscationDelegate = obfuscationDelegate;
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
     */
jaroslav@503
    54
    protected abstract void requireScript(String resourcePath) throws IOException;
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
    }
lubomir@869
    68
lubomir@869
    69
    abstract String getVMObject();
lubomir@869
    70
jaroslav@399
    71
    /** Prints out a debug message. 
jaroslav@399
    72
     * 
jaroslav@399
    73
     * @param msg the message
jaroslav@399
    74
     * @return true if the message has been printed
jaroslav@399
    75
     * @throws IOException 
jaroslav@399
    76
     */
jaroslav@399
    77
    boolean debug(String msg) throws IOException {
jaroslav@399
    78
        out.append(msg);
jaroslav@399
    79
        return true;
jaroslav@399
    80
    }
jaroslav@18
    81
jaroslav@18
    82
    /**
jaroslav@18
    83
     * Converts a given class file to a JavaScript version.
jaroslav@18
    84
     *
jaroslav@0
    85
     * @param classFile input stream with code of the .class file
jaroslav@97
    86
     * @return the initialization code for this class, if any. Otherwise <code>null</code>
jaroslav@91
    87
     * 
jaroslav@0
    88
     * @throws IOException if something goes wrong during read or write or translating
jaroslav@0
    89
     */
jaroslav@18
    90
    
jtulach@162
    91
    public String compile(InputStream classFile) throws IOException {
jtulach@162
    92
        this.jc = new ClassData(classFile);
jaroslav@324
    93
        if (jc.getMajor_version() < 50) {
jaroslav@324
    94
            throw new IOException("Can't compile " + jc.getClassName() + ". Class file version " + jc.getMajor_version() + "."
jaroslav@324
    95
                + jc.getMinor_version() + " - recompile with -target 1.6 (at least)."
jaroslav@324
    96
            );
jaroslav@324
    97
        }
jaroslav@152
    98
        byte[] arrData = jc.findAnnotationData(true);
jaroslav@1239
    99
        {
jaroslav@1239
   100
            String[] arr = findAnnotation(arrData, jc, 
jaroslav@1239
   101
                "org.apidesign.bck2brwsr.core.ExtraJavaScript", 
jaroslav@1239
   102
                "resource", "processByteCode"
jaroslav@1239
   103
            );
jaroslav@1239
   104
            if (arr != null) {
jaroslav@1239
   105
                if (!arr[0].isEmpty()) {
jaroslav@1239
   106
                    requireScript(arr[0]);
jaroslav@1239
   107
                }
jaroslav@1239
   108
                if ("0".equals(arr[1])) {
jaroslav@1239
   109
                    return null;
jaroslav@1239
   110
                }
lubomir@848
   111
            }
jaroslav@1239
   112
        }
jaroslav@1239
   113
        {
jaroslav@1239
   114
            String[] arr = findAnnotation(arrData, jc, 
jaroslav@1239
   115
                "net.java.html.js.JavaScriptResource", 
jaroslav@1239
   116
                "value"
jaroslav@1239
   117
            );
jaroslav@1239
   118
            if (arr != null) {
jaroslav@1239
   119
                if (arr[0].startsWith("/")) {
jaroslav@1239
   120
                    requireScript(arr[0]);
jaroslav@1239
   121
                } else {
jaroslav@1239
   122
                    int last = jc.getClassName().lastIndexOf('/');
jaroslav@1239
   123
                    requireScript(
jaroslav@1239
   124
                        jc.getClassName().substring(0, last + 1).replace('.', '/') + arr[0]
jaroslav@1239
   125
                    );
jaroslav@1239
   126
                }
jaroslav@91
   127
            }
jaroslav@91
   128
        }
jaroslav@239
   129
        String[] proto = findAnnotation(arrData, jc, 
jaroslav@239
   130
            "org.apidesign.bck2brwsr.core.JavaScriptPrototype", 
jaroslav@239
   131
            "container", "prototype"
jaroslav@239
   132
        );
jtulach@162
   133
        StringArray toInitilize = new StringArray();
jaroslav@151
   134
        final String className = className(jc);
jaroslav@213
   135
        out.append("\n\n").append(assignClass(className));
jaroslav@880
   136
        out.append("function ").append(className).append("() {");
jaroslav@880
   137
        out.append("\n  var CLS = ").append(className).append(';');
jaroslav@711
   138
        out.append("\n  if (!CLS.$class) {");
jaroslav@239
   139
        if (proto == null) {
jaroslav@239
   140
            String sc = jc.getSuperClassName(); // with _
jaroslav@230
   141
            out.append("\n    var pp = ").
jaroslav@1409
   142
                append(accessClass(mangleClassName(sc))).append("(true);");
jaroslav@230
   143
            out.append("\n    var p = CLS.prototype = pp;");
jaroslav@239
   144
            out.append("\n    var c = p;");
jaroslav@230
   145
            out.append("\n    var sprcls = pp.constructor.$class;");
jtulach@130
   146
        } else {
jaroslav@240
   147
            out.append("\n    var p = CLS.prototype = ").append(proto[1]).append(";");
jaroslav@316
   148
            if (proto[0] == null) {
jaroslav@316
   149
                proto[0] = "p";
jaroslav@316
   150
            }
jaroslav@239
   151
            out.append("\n    var c = ").append(proto[0]).append(";");
jaroslav@230
   152
            out.append("\n    var sprcls = null;");
jaroslav@13
   153
        }
jaroslav@592
   154
        for (FieldData v : jc.getFields()) {
jaroslav@592
   155
            if (v.isStatic()) {
jaroslav@1353
   156
                out.append("\n  CLS.fld_").append(v.getName()).append(initField(v));
jaroslav@775
   157
                out.append("\n  c._").append(v.getName()).append(" = function (v) {")
jaroslav@1353
   158
                   .append("  if (arguments.length == 1) CLS.fld_").append(v.getName())
jaroslav@1353
   159
                   .append(" = v; return CLS.fld_").
jaroslav@775
   160
                    append(v.getName()).append("; };");
jaroslav@592
   161
            } else {
jaroslav@592
   162
                out.append("\n  c._").append(v.getName()).append(" = function (v) {")
jaroslav@592
   163
                   .append("  if (arguments.length == 1) this.fld_").
jaroslav@592
   164
                    append(className).append('_').append(v.getName())
jaroslav@592
   165
                   .append(" = v; return this.fld_").
jaroslav@592
   166
                    append(className).append('_').append(v.getName())
jaroslav@592
   167
                   .append("; };");
jaroslav@592
   168
            }
lubomir@869
   169
lubomir@869
   170
            obfuscationDelegate.exportField(out, "c", "_" + v.getName(), v);
jaroslav@592
   171
        }
jaroslav@151
   172
        for (MethodData m : jc.getMethods()) {
jaroslav@240
   173
            byte[] onlyArr = m.findAnnotationData(true);
jaroslav@240
   174
            String[] only = findAnnotation(onlyArr, jc, 
jaroslav@240
   175
                "org.apidesign.bck2brwsr.core.JavaScriptOnly", 
jaroslav@240
   176
                "name", "value"
jaroslav@240
   177
            );
jaroslav@240
   178
            if (only != null) {
jaroslav@240
   179
                if (only[0] != null && only[1] != null) {
jaroslav@240
   180
                    out.append("\n    p.").append(only[0]).append(" = ")
jaroslav@240
   181
                        .append(only[1]).append(";");
jaroslav@240
   182
                }
jaroslav@240
   183
                continue;
jaroslav@240
   184
            }
lubomir@869
   185
            String destObject;
jaroslav@266
   186
            String mn;
lubomir@869
   187
            out.append("\n    ");
jaroslav@203
   188
            if (m.isStatic()) {
lubomir@869
   189
                destObject = "c";
lubomir@869
   190
                mn = generateStaticMethod(destObject, m, toInitilize);
jaroslav@203
   191
            } else {
jaroslav@397
   192
                if (m.isConstructor()) {
lubomir@869
   193
                    destObject = "CLS";
lubomir@869
   194
                    mn = generateInstanceMethod(destObject, m);
jaroslav@397
   195
                } else {
lubomir@869
   196
                    destObject = "c";
lubomir@869
   197
                    mn = generateInstanceMethod(destObject, m);
jaroslav@397
   198
                }
jaroslav@266
   199
            }
lubomir@869
   200
            obfuscationDelegate.exportMethod(out, destObject, mn, m);
jaroslav@266
   201
            byte[] runAnno = m.findAnnotationData(false);
jaroslav@266
   202
            if (runAnno != null) {
lubomir@869
   203
                out.append("\n    ").append(destObject).append(".").append(mn).append(".anno = {");
jaroslav@266
   204
                generateAnno(jc, out, runAnno);
jaroslav@266
   205
                out.append("\n    };");
jaroslav@38
   206
            }
lubomir@869
   207
            out.append("\n    ").append(destObject).append(".").append(mn).append(".access = " + m.getAccess()).append(";");
lubomir@869
   208
            out.append("\n    ").append(destObject).append(".").append(mn).append(".cls = CLS;");
jaroslav@38
   209
        }
jaroslav@239
   210
        out.append("\n    c.constructor = CLS;");
jaroslav@1256
   211
        out.append("\n    function fillInstOf(x) {");
lubomir@869
   212
        String instOfName = "$instOf_" + className;
jaroslav@1256
   213
        out.append("\n        x.").append(instOfName).append(" = true;");
jaroslav@1256
   214
        for (String superInterface : jc.getSuperInterfaces()) {
jaroslav@1256
   215
            String intrfc = superInterface.replace('/', '_');
jaroslav@1256
   216
            out.append("\n      vm.").append(intrfc).append("(false).fillInstOf(x);");
jaroslav@1256
   217
            requireReference(superInterface);
jaroslav@1256
   218
        }
jaroslav@1256
   219
        out.append("\n    }");
jaroslav@1256
   220
        out.append("\n    c.fillInstOf = fillInstOf;");
jaroslav@1256
   221
        out.append("\n    fillInstOf(c);");
lubomir@869
   222
        obfuscationDelegate.exportJSProperty(out, "c", instOfName);
jaroslav@711
   223
        out.append("\n    CLS.$class = 'temp';");
jaroslav@274
   224
        out.append("\n    CLS.$class = ");
jaroslav@274
   225
        out.append(accessClass("java_lang_Class(true);"));
jaroslav@225
   226
        out.append("\n    CLS.$class.jvmName = '").append(jc.getClassName()).append("';");
jaroslav@230
   227
        out.append("\n    CLS.$class.superclass = sprcls;");
jaroslav@355
   228
        out.append("\n    CLS.$class.access = ").append(jc.getAccessFlags()+";");
jaroslav@231
   229
        out.append("\n    CLS.$class.cnstr = CLS;");
jaroslav@235
   230
        byte[] classAnno = jc.findAnnotationData(false);
jaroslav@235
   231
        if (classAnno != null) {
jaroslav@235
   232
            out.append("\n    CLS.$class.anno = {");
jaroslav@235
   233
            generateAnno(jc, out, classAnno);
jaroslav@235
   234
            out.append("\n    };");
jaroslav@235
   235
        }
jaroslav@834
   236
        for (String init : toInitilize.toArray()) {
jaroslav@834
   237
            out.append("\n    ").append(init).append("();");
jaroslav@834
   238
        }
jaroslav@204
   239
        out.append("\n  }");
jaroslav@205
   240
        out.append("\n  if (arguments.length === 0) {");
jaroslav@226
   241
        out.append("\n    if (!(this instanceof CLS)) {");
jaroslav@226
   242
        out.append("\n      return new CLS();");
jaroslav@226
   243
        out.append("\n    }");
jaroslav@205
   244
        for (FieldData v : jc.getFields()) {
jaroslav@240
   245
            byte[] onlyArr = v.findAnnotationData(true);
jaroslav@240
   246
            String[] only = findAnnotation(onlyArr, jc, 
jaroslav@240
   247
                "org.apidesign.bck2brwsr.core.JavaScriptOnly", 
jaroslav@240
   248
                "name", "value"
jaroslav@240
   249
            );
jaroslav@240
   250
            if (only != null) {
jaroslav@240
   251
                if (only[0] != null && only[1] != null) {
jaroslav@240
   252
                    out.append("\n    p.").append(only[0]).append(" = ")
jaroslav@240
   253
                        .append(only[1]).append(";");
jaroslav@240
   254
                }
jaroslav@240
   255
                continue;
jaroslav@240
   256
            }
jaroslav@205
   257
            if (!v.isStatic()) {
jaroslav@205
   258
                out.append("\n    this.fld_").
jaroslav@592
   259
                    append(className).append('_').
jaroslav@205
   260
                    append(v.getName()).append(initField(v));
jaroslav@205
   261
            }
jaroslav@205
   262
        }
jaroslav@205
   263
        out.append("\n    return this;");
jaroslav@205
   264
        out.append("\n  }");
jaroslav@224
   265
        out.append("\n  return arguments[0] ? new CLS() : CLS.prototype;");
jaroslav@509
   266
        out.append("\n};");
lubomir@869
   267
lubomir@869
   268
        obfuscationDelegate.exportClass(out, getVMObject(), className, jc);
lubomir@869
   269
jaroslav@834
   270
//        StringBuilder sb = new StringBuilder();
jaroslav@834
   271
//        for (String init : toInitilize.toArray()) {
jaroslav@834
   272
//            sb.append("\n").append(init).append("();");
jaroslav@834
   273
//        }
jaroslav@834
   274
        return "";
jaroslav@0
   275
    }
lubomir@869
   276
    private String generateStaticMethod(String destObject, MethodData m, StringArray toInitilize) throws IOException {
lubomir@869
   277
        String jsb = javaScriptBody(destObject, m, true);
jaroslav@266
   278
        if (jsb != null) {
jaroslav@266
   279
            return jsb;
jaroslav@94
   280
        }
lubomir@307
   281
        final String mn = findMethodName(m, new StringBuilder());
jaroslav@248
   282
        if (mn.equals("class__V")) {
jaroslav@274
   283
            toInitilize.add(accessClass(className(jc)) + "(false)." + mn);
jaroslav@21
   284
        }
lubomir@869
   285
        generateMethod(destObject, mn, m);
jaroslav@266
   286
        return mn;
jaroslav@10
   287
    }
lubomir@307
   288
lubomir@869
   289
    private String generateInstanceMethod(String destObject, MethodData m) throws IOException {
lubomir@869
   290
        String jsb = javaScriptBody(destObject, m, false);
jaroslav@266
   291
        if (jsb != null) {
jaroslav@266
   292
            return jsb;
jaroslav@94
   293
        }
lubomir@307
   294
        final String mn = findMethodName(m, new StringBuilder());
lubomir@869
   295
        generateMethod(destObject, mn, m);
jaroslav@266
   296
        return mn;
jaroslav@0
   297
    }
jaroslav@0
   298
lubomir@869
   299
    private void generateMethod(String destObject, String name, MethodData m)
lubomir@307
   300
            throws IOException {
lubomir@307
   301
        final StackMapIterator stackMapIterator = m.createStackMapIterator();
jaroslav@376
   302
        TrapDataIterator trap = m.getTrapDataIterator();
lubomir@307
   303
        final LocalsMapper lmapper =
lubomir@307
   304
                new LocalsMapper(stackMapIterator.getArguments());
lubomir@307
   305
lubomir@869
   306
        out.append(destObject).append(".").append(name).append(" = function(");
jaroslav@442
   307
        lmapper.outputArguments(out, m.isStatic());
lubomir@307
   308
        out.append(") {").append("\n");
lubomir@307
   309
lubomir@221
   310
        final byte[] byteCodes = m.getCode();
lubomir@307
   311
        if (byteCodes == null) {
lubomir@307
   312
            out.append("  throw 'no code found for ")
jaroslav@424
   313
               .append(jc.getClassName()).append('.')
jaroslav@424
   314
               .append(m.getName()).append("';\n");
lubomir@307
   315
            out.append("};");
lubomir@307
   316
            return;
lubomir@307
   317
        }
lubomir@307
   318
lubomir@307
   319
        final StackMapper smapper = new StackMapper();
lubomir@307
   320
jaroslav@442
   321
        if (!m.isStatic()) {
jaroslav@442
   322
            out.append("  var ").append(" lcA0 = this;\n");
jaroslav@442
   323
        }
lubomir@221
   324
lubomir@221
   325
        int lastStackFrame = -1;
jaroslav@398
   326
        TrapData[] previousTrap = null;
lubomir@585
   327
        boolean wide = false;
jaroslav@398
   328
        
jaroslav@839
   329
        out.append("\n  var gt = 0;\n");
jaroslav@839
   330
        int openBraces = 0;
jaroslav@843
   331
        int topMostLabel = 0;
jaroslav@0
   332
        for (int i = 0; i < byteCodes.length; i++) {
jaroslav@272
   333
            int prev = i;
lubomir@221
   334
            stackMapIterator.advanceTo(i);
jaroslav@398
   335
            boolean changeInCatch = trap.advanceTo(i);
jaroslav@398
   336
            if (changeInCatch || lastStackFrame != stackMapIterator.getFrameIndex()) {
jaroslav@398
   337
                if (previousTrap != null) {
jaroslav@843
   338
                    generateCatch(previousTrap, i, topMostLabel);
jaroslav@398
   339
                    previousTrap = null;
jaroslav@398
   340
                }
jaroslav@398
   341
            }
lubomir@221
   342
            if (lastStackFrame != stackMapIterator.getFrameIndex()) {
jaroslav@839
   343
                if (i != 0) {
jaroslav@839
   344
                    out.append("    }\n");
jaroslav@839
   345
                }
jaroslav@843
   346
                if (openBraces > 64) {
jaroslav@843
   347
                    for (int c = 0; c < 64; c++) {
jaroslav@843
   348
                        out.append("break;}\n");
jaroslav@843
   349
                    }
jaroslav@843
   350
                    openBraces = 1;
jaroslav@843
   351
                    topMostLabel = i;
jaroslav@843
   352
                }
jaroslav@839
   353
                
lubomir@221
   354
                lastStackFrame = stackMapIterator.getFrameIndex();
lubomir@307
   355
                lmapper.syncWithFrameLocals(stackMapIterator.getFrameLocals());
lubomir@307
   356
                smapper.syncWithFrameStack(stackMapIterator.getFrameStack());
jaroslav@839
   357
                out.append("    X_" + i).append(": for (;;) { IF: if (gt <= " + i + ") {\n");
jaroslav@839
   358
                openBraces++;
jaroslav@398
   359
                changeInCatch = true;
lubomir@221
   360
            } else {
jaroslav@399
   361
                debug("    /* " + i + " */ ");
lubomir@221
   362
            }
jaroslav@398
   363
            if (changeInCatch && trap.useTry()) {
tzezula@285
   364
                out.append("try {");
jaroslav@398
   365
                previousTrap = trap.current();
tzezula@285
   366
            }
lubomir@585
   367
            final int c = readUByte(byteCodes, i);
jaroslav@0
   368
            switch (c) {
jaroslav@151
   369
                case opc_aload_0:
jaroslav@1453
   370
                    smapper.assign(out, VarType.REFERENCE, lmapper.getA(0));
lubomir@281
   371
                    break;
jaroslav@151
   372
                case opc_iload_0:
jaroslav@1453
   373
                    smapper.assign(out, VarType.INTEGER, lmapper.getI(0));
lubomir@281
   374
                    break;
jaroslav@151
   375
                case opc_lload_0:
jaroslav@1453
   376
                    smapper.assign(out, VarType.LONG, lmapper.getL(0));
lubomir@281
   377
                    break;
jaroslav@151
   378
                case opc_fload_0:
jaroslav@1453
   379
                    smapper.assign(out, VarType.FLOAT, lmapper.getF(0));
lubomir@281
   380
                    break;
jaroslav@151
   381
                case opc_dload_0:
jaroslav@1453
   382
                    smapper.assign(out, VarType.DOUBLE, lmapper.getD(0));
jaroslav@0
   383
                    break;
jaroslav@151
   384
                case opc_aload_1:
jaroslav@1453
   385
                    smapper.assign(out, VarType.REFERENCE, lmapper.getA(1));
lubomir@281
   386
                    break;
jaroslav@151
   387
                case opc_iload_1:
jaroslav@1453
   388
                    smapper.assign(out, VarType.INTEGER, lmapper.getI(1));
lubomir@281
   389
                    break;
jaroslav@151
   390
                case opc_lload_1:
jaroslav@1453
   391
                    smapper.assign(out, VarType.LONG, lmapper.getL(1));
lubomir@281
   392
                    break;
jaroslav@151
   393
                case opc_fload_1:
jaroslav@1453
   394
                    smapper.assign(out, VarType.FLOAT, lmapper.getF(1));
lubomir@281
   395
                    break;
jaroslav@151
   396
                case opc_dload_1:
jaroslav@1453
   397
                    smapper.assign(out, VarType.DOUBLE, lmapper.getD(1));
jaroslav@0
   398
                    break;
jaroslav@151
   399
                case opc_aload_2:
jaroslav@1453
   400
                    smapper.assign(out, VarType.REFERENCE, lmapper.getA(2));
lubomir@281
   401
                    break;
jaroslav@151
   402
                case opc_iload_2:
jaroslav@1453
   403
                    smapper.assign(out, VarType.INTEGER, lmapper.getI(2));
lubomir@281
   404
                    break;
jaroslav@151
   405
                case opc_lload_2:
jaroslav@1453
   406
                    smapper.assign(out, VarType.LONG, lmapper.getL(2));
lubomir@281
   407
                    break;
jaroslav@151
   408
                case opc_fload_2:
jaroslav@1453
   409
                    smapper.assign(out, VarType.FLOAT, lmapper.getF(2));
lubomir@281
   410
                    break;
jaroslav@151
   411
                case opc_dload_2:
jaroslav@1453
   412
                    smapper.assign(out, VarType.DOUBLE, lmapper.getD(2));
jaroslav@2
   413
                    break;
jaroslav@151
   414
                case opc_aload_3:
jaroslav@1453
   415
                    smapper.assign(out, VarType.REFERENCE, lmapper.getA(3));
lubomir@281
   416
                    break;
jaroslav@151
   417
                case opc_iload_3:
jaroslav@1453
   418
                    smapper.assign(out, VarType.INTEGER, lmapper.getI(3));
lubomir@281
   419
                    break;
jaroslav@151
   420
                case opc_lload_3:
jaroslav@1453
   421
                    smapper.assign(out, VarType.LONG, lmapper.getL(3));
lubomir@281
   422
                    break;
jaroslav@151
   423
                case opc_fload_3:
jaroslav@1453
   424
                    smapper.assign(out, VarType.FLOAT, lmapper.getF(3));
lubomir@281
   425
                    break;
jaroslav@151
   426
                case opc_dload_3:
jaroslav@1453
   427
                    smapper.assign(out, VarType.DOUBLE, lmapper.getD(3));
jaroslav@3
   428
                    break;
lubomir@281
   429
                case opc_iload: {
lubomir@585
   430
                    ++i;
lubomir@585
   431
                    final int indx = wide ? readUShort(byteCodes, i++)
lubomir@585
   432
                                          : readUByte(byteCodes, i);
lubomir@585
   433
                    wide = false;
jaroslav@1453
   434
                    smapper.assign(out, VarType.INTEGER, lmapper.getI(indx));
lubomir@281
   435
                    break;
lubomir@281
   436
                }
lubomir@281
   437
                case opc_lload: {
lubomir@585
   438
                    ++i;
lubomir@585
   439
                    final int indx = wide ? readUShort(byteCodes, i++)
lubomir@585
   440
                                          : readUByte(byteCodes, i);
lubomir@585
   441
                    wide = false;
jaroslav@1453
   442
                    smapper.assign(out, VarType.LONG, lmapper.getL(indx));
lubomir@281
   443
                    break;
lubomir@281
   444
                }
lubomir@281
   445
                case opc_fload: {
lubomir@585
   446
                    ++i;
lubomir@585
   447
                    final int indx = wide ? readUShort(byteCodes, i++)
lubomir@585
   448
                                          : readUByte(byteCodes, i);
lubomir@585
   449
                    wide = false;
jaroslav@1453
   450
                    smapper.assign(out, VarType.FLOAT, lmapper.getF(indx));
lubomir@281
   451
                    break;
lubomir@281
   452
                }
lubomir@281
   453
                case opc_dload: {
lubomir@585
   454
                    ++i;
lubomir@585
   455
                    final int indx = wide ? readUShort(byteCodes, i++)
lubomir@585
   456
                                          : readUByte(byteCodes, i);
lubomir@585
   457
                    wide = false;
jaroslav@1453
   458
                    smapper.assign(out, VarType.DOUBLE, lmapper.getD(indx));
lubomir@281
   459
                    break;
lubomir@281
   460
                }
jaroslav@151
   461
                case opc_aload: {
lubomir@585
   462
                    ++i;
lubomir@585
   463
                    final int indx = wide ? readUShort(byteCodes, i++)
lubomir@585
   464
                                          : readUByte(byteCodes, i);
lubomir@585
   465
                    wide = false;
jaroslav@1453
   466
                    smapper.assign(out, VarType.REFERENCE, lmapper.getA(indx));
jaroslav@3
   467
                    break;
jaroslav@3
   468
                }
lubomir@281
   469
                case opc_istore: {
lubomir@585
   470
                    ++i;
lubomir@585
   471
                    final int indx = wide ? readUShort(byteCodes, i++)
lubomir@585
   472
                                          : readUByte(byteCodes, i);
lubomir@585
   473
                    wide = false;
lubomir@474
   474
                    emit(out, "var @1 = @2;",
lubomir@474
   475
                         lmapper.setI(indx), smapper.popI());
lubomir@281
   476
                    break;
lubomir@281
   477
                }
lubomir@281
   478
                case opc_lstore: {
lubomir@585
   479
                    ++i;
lubomir@585
   480
                    final int indx = wide ? readUShort(byteCodes, i++)
lubomir@585
   481
                                          : readUByte(byteCodes, i);
lubomir@585
   482
                    wide = false;
lubomir@474
   483
                    emit(out, "var @1 = @2;",
lubomir@474
   484
                         lmapper.setL(indx), smapper.popL());
lubomir@281
   485
                    break;
lubomir@281
   486
                }
lubomir@281
   487
                case opc_fstore: {
lubomir@585
   488
                    ++i;
lubomir@585
   489
                    final int indx = wide ? readUShort(byteCodes, i++)
lubomir@585
   490
                                          : readUByte(byteCodes, i);
lubomir@585
   491
                    wide = false;
lubomir@474
   492
                    emit(out, "var @1 = @2;",
lubomir@474
   493
                         lmapper.setF(indx), smapper.popF());
lubomir@281
   494
                    break;
lubomir@281
   495
                }
lubomir@281
   496
                case opc_dstore: {
lubomir@585
   497
                    ++i;
lubomir@585
   498
                    final int indx = wide ? readUShort(byteCodes, i++)
lubomir@585
   499
                                          : readUByte(byteCodes, i);
lubomir@585
   500
                    wide = false;
lubomir@474
   501
                    emit(out, "var @1 = @2;",
lubomir@474
   502
                         lmapper.setD(indx), smapper.popD());
lubomir@281
   503
                    break;
lubomir@281
   504
                }
jaroslav@151
   505
                case opc_astore: {
lubomir@585
   506
                    ++i;
lubomir@585
   507
                    final int indx = wide ? readUShort(byteCodes, i++)
lubomir@585
   508
                                          : readUByte(byteCodes, i);
lubomir@585
   509
                    wide = false;
lubomir@474
   510
                    emit(out, "var @1 = @2;",
lubomir@474
   511
                         lmapper.setA(indx), smapper.popA());
jaroslav@31
   512
                    break;
jaroslav@31
   513
                }
jaroslav@151
   514
                case opc_astore_0:
lubomir@474
   515
                    emit(out, "var @1 = @2;", lmapper.setA(0), smapper.popA());
lubomir@281
   516
                    break;
jaroslav@151
   517
                case opc_istore_0:
lubomir@474
   518
                    emit(out, "var @1 = @2;", lmapper.setI(0), smapper.popI());
lubomir@281
   519
                    break;
jaroslav@151
   520
                case opc_lstore_0:
lubomir@474
   521
                    emit(out, "var @1 = @2;", lmapper.setL(0), smapper.popL());
lubomir@281
   522
                    break;
jaroslav@151
   523
                case opc_fstore_0:
lubomir@474
   524
                    emit(out, "var @1 = @2;", lmapper.setF(0), smapper.popF());
lubomir@281
   525
                    break;
jaroslav@151
   526
                case opc_dstore_0:
lubomir@474
   527
                    emit(out, "var @1 = @2;", lmapper.setD(0), smapper.popD());
jaroslav@5
   528
                    break;
jaroslav@151
   529
                case opc_astore_1:
lubomir@474
   530
                    emit(out, "var @1 = @2;", lmapper.setA(1), smapper.popA());
lubomir@281
   531
                    break;
jaroslav@151
   532
                case opc_istore_1:
lubomir@474
   533
                    emit(out, "var @1 = @2;", lmapper.setI(1), smapper.popI());
lubomir@281
   534
                    break;
jaroslav@151
   535
                case opc_lstore_1:
lubomir@474
   536
                    emit(out, "var @1 = @2;", lmapper.setL(1), smapper.popL());
lubomir@281
   537
                    break;
jaroslav@151
   538
                case opc_fstore_1:
lubomir@474
   539
                    emit(out, "var @1 = @2;", lmapper.setF(1), smapper.popF());
lubomir@281
   540
                    break;
jaroslav@151
   541
                case opc_dstore_1:
lubomir@474
   542
                    emit(out, "var @1 = @2;", lmapper.setD(1), smapper.popD());
jaroslav@5
   543
                    break;
jaroslav@151
   544
                case opc_astore_2:
lubomir@474
   545
                    emit(out, "var @1 = @2;", lmapper.setA(2), smapper.popA());
lubomir@281
   546
                    break;
jaroslav@151
   547
                case opc_istore_2:
lubomir@474
   548
                    emit(out, "var @1 = @2;", lmapper.setI(2), smapper.popI());
lubomir@281
   549
                    break;
jaroslav@151
   550
                case opc_lstore_2:
lubomir@474
   551
                    emit(out, "var @1 = @2;", lmapper.setL(2), smapper.popL());
lubomir@281
   552
                    break;
jaroslav@151
   553
                case opc_fstore_2:
lubomir@474
   554
                    emit(out, "var @1 = @2;", lmapper.setF(2), smapper.popF());
lubomir@281
   555
                    break;
jaroslav@151
   556
                case opc_dstore_2:
lubomir@474
   557
                    emit(out, "var @1 = @2;", lmapper.setD(2), smapper.popD());
jaroslav@5
   558
                    break;
jaroslav@151
   559
                case opc_astore_3:
lubomir@474
   560
                    emit(out, "var @1 = @2;", lmapper.setA(3), smapper.popA());
lubomir@281
   561
                    break;
jaroslav@151
   562
                case opc_istore_3:
lubomir@474
   563
                    emit(out, "var @1 = @2;", lmapper.setI(3), smapper.popI());
lubomir@281
   564
                    break;
jaroslav@151
   565
                case opc_lstore_3:
lubomir@474
   566
                    emit(out, "var @1 = @2;", lmapper.setL(3), smapper.popL());
lubomir@281
   567
                    break;
jaroslav@151
   568
                case opc_fstore_3:
lubomir@474
   569
                    emit(out, "var @1 = @2;", lmapper.setF(3), smapper.popF());
lubomir@281
   570
                    break;
jaroslav@151
   571
                case opc_dstore_3:
lubomir@474
   572
                    emit(out, "var @1 = @2;", lmapper.setD(3), smapper.popD());
jaroslav@5
   573
                    break;
jaroslav@151
   574
                case opc_iadd:
Martin@445
   575
                    emit(out, "@1 = @1.add32(@2);", smapper.getI(1), smapper.popI());
lubomir@281
   576
                    break;
jaroslav@151
   577
                case opc_ladd:
Martin@582
   578
                    emit(out, "@1 = @1.add64(@2);", smapper.getL(1), smapper.popL());
lubomir@281
   579
                    break;
jaroslav@151
   580
                case opc_fadd:
lubomir@307
   581
                    emit(out, "@1 += @2;", smapper.getF(1), smapper.popF());
lubomir@281
   582
                    break;
jaroslav@151
   583
                case opc_dadd:
lubomir@307
   584
                    emit(out, "@1 += @2;", smapper.getD(1), smapper.popD());
jaroslav@0
   585
                    break;
jaroslav@151
   586
                case opc_isub:
Martin@445
   587
                    emit(out, "@1 = @1.sub32(@2);", smapper.getI(1), smapper.popI());
lubomir@281
   588
                    break;
jaroslav@151
   589
                case opc_lsub:
Martin@620
   590
                    emit(out, "@1 = @1.sub64(@2);", smapper.getL(1), smapper.popL());
lubomir@281
   591
                    break;
jaroslav@151
   592
                case opc_fsub:
lubomir@307
   593
                    emit(out, "@1 -= @2;", smapper.getF(1), smapper.popF());
lubomir@281
   594
                    break;
jaroslav@151
   595
                case opc_dsub:
lubomir@307
   596
                    emit(out, "@1 -= @2;", smapper.getD(1), smapper.popD());
jaroslav@2
   597
                    break;
jaroslav@151
   598
                case opc_imul:
Martin@445
   599
                    emit(out, "@1 = @1.mul32(@2);", smapper.getI(1), smapper.popI());
lubomir@281
   600
                    break;
jaroslav@151
   601
                case opc_lmul:
Martin@657
   602
                    emit(out, "@1 = @1.mul64(@2);", smapper.getL(1), smapper.popL());
lubomir@281
   603
                    break;
jaroslav@151
   604
                case opc_fmul:
lubomir@307
   605
                    emit(out, "@1 *= @2;", smapper.getF(1), smapper.popF());
lubomir@281
   606
                    break;
jaroslav@151
   607
                case opc_dmul:
lubomir@307
   608
                    emit(out, "@1 *= @2;", smapper.getD(1), smapper.popD());
jaroslav@1
   609
                    break;
jaroslav@151
   610
                case opc_idiv:
lubomir@737
   611
                    emit(out, "@1 = @1.div32(@2);",
lubomir@307
   612
                         smapper.getI(1), smapper.popI());
lubomir@281
   613
                    break;
jaroslav@151
   614
                case opc_ldiv:
Martin@582
   615
                    emit(out, "@1 = @1.div64(@2);",
lubomir@307
   616
                         smapper.getL(1), smapper.popL());
jaroslav@3
   617
                    break;
jaroslav@151
   618
                case opc_fdiv:
lubomir@307
   619
                    emit(out, "@1 /= @2;", smapper.getF(1), smapper.popF());
lubomir@281
   620
                    break;
jaroslav@151
   621
                case opc_ddiv:
lubomir@307
   622
                    emit(out, "@1 /= @2;", smapper.getD(1), smapper.popD());
jaroslav@3
   623
                    break;
jaroslav@178
   624
                case opc_irem:
lubomir@737
   625
                    emit(out, "@1 = @1.mod32(@2);",
lubomir@737
   626
                         smapper.getI(1), smapper.popI());
lubomir@281
   627
                    break;
jaroslav@178
   628
                case opc_lrem:
lubomir@676
   629
                    emit(out, "@1 = @1.mod64(@2);",
lubomir@676
   630
                         smapper.getL(1), smapper.popL());
lubomir@281
   631
                    break;
jaroslav@178
   632
                case opc_frem:
lubomir@307
   633
                    emit(out, "@1 %= @2;", smapper.getF(1), smapper.popF());
lubomir@281
   634
                    break;
jaroslav@178
   635
                case opc_drem:
lubomir@307
   636
                    emit(out, "@1 %= @2;", smapper.getD(1), smapper.popD());
jaroslav@178
   637
                    break;
jaroslav@151
   638
                case opc_iand:
lubomir@307
   639
                    emit(out, "@1 &= @2;", smapper.getI(1), smapper.popI());
lubomir@281
   640
                    break;
jaroslav@151
   641
                case opc_land:
Martin@615
   642
                    emit(out, "@1 = @1.and64(@2);", smapper.getL(1), smapper.popL());
jaroslav@7
   643
                    break;
jaroslav@151
   644
                case opc_ior:
lubomir@307
   645
                    emit(out, "@1 |= @2;", smapper.getI(1), smapper.popI());
lubomir@281
   646
                    break;
jaroslav@151
   647
                case opc_lor:
Martin@627
   648
                    emit(out, "@1 = @1.or64(@2);", smapper.getL(1), smapper.popL());
jaroslav@7
   649
                    break;
jaroslav@151
   650
                case opc_ixor:
lubomir@307
   651
                    emit(out, "@1 ^= @2;", smapper.getI(1), smapper.popI());
lubomir@281
   652
                    break;
jaroslav@151
   653
                case opc_lxor:
lubomir@679
   654
                    emit(out, "@1 = @1.xor64(@2);", smapper.getL(1), smapper.popL());
jaroslav@6
   655
                    break;
jaroslav@151
   656
                case opc_ineg:
Martin@700
   657
                    emit(out, "@1 = @1.neg32();", smapper.getI(0));
lubomir@281
   658
                    break;
jaroslav@151
   659
                case opc_lneg:
Martin@630
   660
                    emit(out, "@1 = @1.neg64();", smapper.getL(0));
lubomir@281
   661
                    break;
jaroslav@151
   662
                case opc_fneg:
lubomir@307
   663
                    emit(out, "@1 = -@1;", smapper.getF(0));
lubomir@281
   664
                    break;
jaroslav@151
   665
                case opc_dneg:
lubomir@307
   666
                    emit(out, "@1 = -@1;", smapper.getD(0));
jaroslav@93
   667
                    break;
jaroslav@151
   668
                case opc_ishl:
lubomir@307
   669
                    emit(out, "@1 <<= @2;", smapper.getI(1), smapper.popI());
lubomir@281
   670
                    break;
jaroslav@151
   671
                case opc_lshl:
Martin@582
   672
                    emit(out, "@1 = @1.shl64(@2);", smapper.getL(1), smapper.popI());
jaroslav@93
   673
                    break;
jaroslav@151
   674
                case opc_ishr:
lubomir@307
   675
                    emit(out, "@1 >>= @2;", smapper.getI(1), smapper.popI());
lubomir@281
   676
                    break;
jaroslav@151
   677
                case opc_lshr:
Martin@615
   678
                    emit(out, "@1 = @1.shr64(@2);", smapper.getL(1), smapper.popI());
jaroslav@93
   679
                    break;
jaroslav@151
   680
                case opc_iushr:
lubomir@307
   681
                    emit(out, "@1 >>>= @2;", smapper.getI(1), smapper.popI());
lubomir@281
   682
                    break;
jaroslav@151
   683
                case opc_lushr:
Martin@629
   684
                    emit(out, "@1 = @1.ushr64(@2);", smapper.getL(1), smapper.popI());
jaroslav@93
   685
                    break;
jaroslav@151
   686
                case opc_iinc: {
lubomir@585
   687
                    ++i;
lubomir@585
   688
                    final int varIndx = wide ? readUShort(byteCodes, i++)
lubomir@585
   689
                                             : readUByte(byteCodes, i);
lubomir@585
   690
                    ++i;
lubomir@697
   691
                    final int incrBy = wide ? readShort(byteCodes, i++)
lubomir@585
   692
                                            : byteCodes[i];
lubomir@585
   693
                    wide = false;
jaroslav@5
   694
                    if (incrBy == 1) {
lubomir@307
   695
                        emit(out, "@1++;", lmapper.getI(varIndx));
jaroslav@5
   696
                    } else {
lubomir@307
   697
                        emit(out, "@1 += @2;",
lubomir@307
   698
                             lmapper.getI(varIndx),
lubomir@283
   699
                             Integer.toString(incrBy));
jaroslav@5
   700
                    }
jaroslav@5
   701
                    break;
jaroslav@5
   702
                }
jaroslav@151
   703
                case opc_return:
lubomir@283
   704
                    emit(out, "return;");
jaroslav@10
   705
                    break;
jaroslav@151
   706
                case opc_ireturn:
lubomir@307
   707
                    emit(out, "return @1;", smapper.popI());
lubomir@281
   708
                    break;
jaroslav@151
   709
                case opc_lreturn:
lubomir@307
   710
                    emit(out, "return @1;", smapper.popL());
lubomir@281
   711
                    break;
jaroslav@151
   712
                case opc_freturn:
lubomir@307
   713
                    emit(out, "return @1;", smapper.popF());
lubomir@281
   714
                    break;
jaroslav@151
   715
                case opc_dreturn:
lubomir@307
   716
                    emit(out, "return @1;", smapper.popD());
lubomir@281
   717
                    break;
jaroslav@151
   718
                case opc_areturn:
lubomir@307
   719
                    emit(out, "return @1;", smapper.popA());
jaroslav@1
   720
                    break;
jaroslav@151
   721
                case opc_i2l:
lubomir@474
   722
                    emit(out, "var @2 = @1;", smapper.popI(), smapper.pushL());
lubomir@281
   723
                    break;
jaroslav@151
   724
                case opc_i2f:
lubomir@474
   725
                    emit(out, "var @2 = @1;", smapper.popI(), smapper.pushF());
lubomir@281
   726
                    break;
jaroslav@151
   727
                case opc_i2d:
lubomir@474
   728
                    emit(out, "var @2 = @1;", smapper.popI(), smapper.pushD());
lubomir@281
   729
                    break;
jaroslav@151
   730
                case opc_l2i:
Martin@582
   731
                    emit(out, "var @2 = @1.toInt32();", smapper.popL(), smapper.pushI());
lubomir@281
   732
                    break;
jaroslav@3
   733
                    // max int check?
jaroslav@151
   734
                case opc_l2f:
Martin@582
   735
                    emit(out, "var @2 = @1.toFP();", smapper.popL(), smapper.pushF());
jaroslav@272
   736
                    break;
jaroslav@151
   737
                case opc_l2d:
Martin@582
   738
                    emit(out, "var @2 = @1.toFP();", smapper.popL(), smapper.pushD());
jaroslav@272
   739
                    break;
jaroslav@151
   740
                case opc_f2d:
lubomir@474
   741
                    emit(out, "var @2 = @1;", smapper.popF(), smapper.pushD());
lubomir@281
   742
                    break;
jaroslav@151
   743
                case opc_d2f:
lubomir@474
   744
                    emit(out, "var @2 = @1;", smapper.popD(), smapper.pushF());
jaroslav@3
   745
                    break;
jaroslav@151
   746
                case opc_f2i:
lubomir@778
   747
                    emit(out, "var @2 = @1.toInt32();",
lubomir@307
   748
                         smapper.popF(), smapper.pushI());
lubomir@281
   749
                    break;
jaroslav@151
   750
                case opc_f2l:
lubomir@778
   751
                    emit(out, "var @2 = @1.toLong();",
lubomir@307
   752
                         smapper.popF(), smapper.pushL());
lubomir@281
   753
                    break;
jaroslav@151
   754
                case opc_d2i:
lubomir@778
   755
                    emit(out, "var @2 = @1.toInt32();",
lubomir@307
   756
                         smapper.popD(), smapper.pushI());
lubomir@281
   757
                    break;
jaroslav@151
   758
                case opc_d2l:
lubomir@778
   759
                    emit(out, "var @2 = @1.toLong();",
lubomir@307
   760
                         smapper.popD(), smapper.pushL());
jaroslav@3
   761
                    break;
jaroslav@151
   762
                case opc_i2b:
lubomir@474
   763
                    emit(out, "var @1 = @1.toInt8();", smapper.getI(0));
Martin@440
   764
                    break;
jaroslav@151
   765
                case opc_i2c:
Martin@439
   766
                    out.append("{ /* number conversion */ }");
Martin@439
   767
                    break;
jaroslav@151
   768
                case opc_i2s:
lubomir@474
   769
                    emit(out, "var @1 = @1.toInt16();", smapper.getI(0));
jaroslav@2
   770
                    break;
jaroslav@151
   771
                case opc_aconst_null:
lubomir@474
   772
                    emit(out, "var @1 = null;", smapper.pushA());
jaroslav@46
   773
                    break;
jaroslav@151
   774
                case opc_iconst_m1:
lubomir@474
   775
                    emit(out, "var @1 = -1;", smapper.pushI());
jaroslav@48
   776
                    break;
jaroslav@151
   777
                case opc_iconst_0:
lubomir@474
   778
                    emit(out, "var @1 = 0;", smapper.pushI());
lubomir@281
   779
                    break;
jaroslav@151
   780
                case opc_dconst_0:
lubomir@474
   781
                    emit(out, "var @1 = 0;", smapper.pushD());
lubomir@281
   782
                    break;
jaroslav@151
   783
                case opc_lconst_0:
lubomir@474
   784
                    emit(out, "var @1 = 0;", smapper.pushL());
lubomir@281
   785
                    break;
jaroslav@151
   786
                case opc_fconst_0:
lubomir@474
   787
                    emit(out, "var @1 = 0;", smapper.pushF());
jaroslav@4
   788
                    break;
jaroslav@151
   789
                case opc_iconst_1:
lubomir@474
   790
                    emit(out, "var @1 = 1;", smapper.pushI());
lubomir@281
   791
                    break;
jaroslav@151
   792
                case opc_lconst_1:
lubomir@474
   793
                    emit(out, "var @1 = 1;", smapper.pushL());
lubomir@281
   794
                    break;
jaroslav@151
   795
                case opc_fconst_1:
lubomir@474
   796
                    emit(out, "var @1 = 1;", smapper.pushF());
lubomir@281
   797
                    break;
jaroslav@151
   798
                case opc_dconst_1:
lubomir@474
   799
                    emit(out, "var @1 = 1;", smapper.pushD());
jaroslav@4
   800
                    break;
jaroslav@151
   801
                case opc_iconst_2:
lubomir@474
   802
                    emit(out, "var @1 = 2;", smapper.pushI());
lubomir@281
   803
                    break;
jaroslav@151
   804
                case opc_fconst_2:
lubomir@474
   805
                    emit(out, "var @1 = 2;", smapper.pushF());
jaroslav@4
   806
                    break;
jaroslav@151
   807
                case opc_iconst_3:
lubomir@474
   808
                    emit(out, "var @1 = 3;", smapper.pushI());
jaroslav@4
   809
                    break;
jaroslav@151
   810
                case opc_iconst_4:
lubomir@474
   811
                    emit(out, "var @1 = 4;", smapper.pushI());
jaroslav@4
   812
                    break;
jaroslav@151
   813
                case opc_iconst_5:
lubomir@474
   814
                    emit(out, "var @1 = 5;", smapper.pushI());
jaroslav@4
   815
                    break;
jaroslav@151
   816
                case opc_ldc: {
lubomir@585
   817
                    int indx = readUByte(byteCodes, ++i);
jaroslav@151
   818
                    String v = encodeConstant(indx);
lubomir@307
   819
                    int type = VarType.fromConstantType(jc.getTag(indx));
lubomir@474
   820
                    emit(out, "var @1 = @2;", smapper.pushT(type), v);
jaroslav@20
   821
                    break;
jaroslav@20
   822
                }
jaroslav@151
   823
                case opc_ldc_w:
jaroslav@151
   824
                case opc_ldc2_w: {
jaroslav@864
   825
                    int indx = readUShortArg(byteCodes, i);
jaroslav@8
   826
                    i += 2;
jaroslav@151
   827
                    String v = encodeConstant(indx);
lubomir@307
   828
                    int type = VarType.fromConstantType(jc.getTag(indx));
Martin@582
   829
                    if (type == VarType.LONG) {
Martin@582
   830
                        final Long lv = new Long(v);
Martin@582
   831
                        final int low = (int)(lv.longValue() & 0xFFFFFFFF);
Martin@582
   832
                        final int hi = (int)(lv.longValue() >> 32);
Martin@594
   833
                        emit(out, "var @1 = 0x@3.next32(0x@2);", smapper.pushL(), 
Martin@582
   834
                                Integer.toHexString(low), Integer.toHexString(hi));
Martin@582
   835
                    } else {
Martin@582
   836
                        emit(out, "var @1 = @2;", smapper.pushT(type), v);
Martin@582
   837
                    }
jaroslav@8
   838
                    break;
jaroslav@8
   839
                }
jaroslav@151
   840
                case opc_lcmp:
Martin@582
   841
                    emit(out, "var @3 = @2.compare64(@1);",
lubomir@307
   842
                         smapper.popL(), smapper.popL(), smapper.pushI());
lubomir@281
   843
                    break;
jaroslav@151
   844
                case opc_fcmpl:
jaroslav@151
   845
                case opc_fcmpg:
lubomir@474
   846
                    emit(out, "var @3 = (@2 == @1) ? 0 : ((@2 < @1) ? -1 : 1);",
lubomir@307
   847
                         smapper.popF(), smapper.popF(), smapper.pushI());
lubomir@281
   848
                    break;
jaroslav@151
   849
                case opc_dcmpl:
lubomir@281
   850
                case opc_dcmpg:
lubomir@474
   851
                    emit(out, "var @3 = (@2 == @1) ? 0 : ((@2 < @1) ? -1 : 1);",
lubomir@307
   852
                         smapper.popD(), smapper.popD(), smapper.pushI());
jaroslav@20
   853
                    break;
jaroslav@151
   854
                case opc_if_acmpeq:
lubomir@307
   855
                    i = generateIf(byteCodes, i, smapper.popA(), smapper.popA(),
jaroslav@843
   856
                                   "===", topMostLabel);
jaroslav@104
   857
                    break;
jaroslav@151
   858
                case opc_if_acmpne:
lubomir@307
   859
                    i = generateIf(byteCodes, i, smapper.popA(), smapper.popA(),
jaroslav@843
   860
                                   "!==", topMostLabel);
jaroslav@104
   861
                    break;
lubomir@283
   862
                case opc_if_icmpeq:
lubomir@307
   863
                    i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
jaroslav@843
   864
                                   "==", topMostLabel);
jaroslav@4
   865
                    break;
jaroslav@151
   866
                case opc_ifeq: {
jaroslav@864
   867
                    int indx = i + readShortArg(byteCodes, i);
jaroslav@839
   868
                    emitIf(out, "if (@1 == 0) ",
jaroslav@843
   869
                         smapper.popI(), i, indx, topMostLabel);
jaroslav@7
   870
                    i += 2;
jaroslav@7
   871
                    break;
jaroslav@7
   872
                }
jaroslav@151
   873
                case opc_ifne: {
jaroslav@864
   874
                    int indx = i + readShortArg(byteCodes, i);
jaroslav@839
   875
                    emitIf(out, "if (@1 != 0) ",
jaroslav@843
   876
                         smapper.popI(), i, indx, topMostLabel);
jaroslav@20
   877
                    i += 2;
jaroslav@20
   878
                    break;
jaroslav@20
   879
                }
jaroslav@151
   880
                case opc_iflt: {
jaroslav@864
   881
                    int indx = i + readShortArg(byteCodes, i);
jaroslav@839
   882
                    emitIf(out, "if (@1 < 0) ",
jaroslav@843
   883
                         smapper.popI(), i, indx, topMostLabel);
jaroslav@20
   884
                    i += 2;
jaroslav@20
   885
                    break;
jaroslav@20
   886
                }
jaroslav@151
   887
                case opc_ifle: {
jaroslav@864
   888
                    int indx = i + readShortArg(byteCodes, i);
jaroslav@839
   889
                    emitIf(out, "if (@1 <= 0) ",
jaroslav@843
   890
                         smapper.popI(), i, indx, topMostLabel);
jaroslav@20
   891
                    i += 2;
jaroslav@20
   892
                    break;
jaroslav@20
   893
                }
jaroslav@151
   894
                case opc_ifgt: {
jaroslav@864
   895
                    int indx = i + readShortArg(byteCodes, i);
jaroslav@839
   896
                    emitIf(out, "if (@1 > 0) ",
jaroslav@843
   897
                         smapper.popI(), i, indx, topMostLabel);
jaroslav@20
   898
                    i += 2;
jaroslav@20
   899
                    break;
jaroslav@20
   900
                }
jaroslav@151
   901
                case opc_ifge: {
jaroslav@864
   902
                    int indx = i + readShortArg(byteCodes, i);
jaroslav@839
   903
                    emitIf(out, "if (@1 >= 0) ",
jaroslav@843
   904
                         smapper.popI(), i, indx, topMostLabel);
jaroslav@20
   905
                    i += 2;
jaroslav@20
   906
                    break;
jaroslav@20
   907
                }
jaroslav@151
   908
                case opc_ifnonnull: {
jaroslav@864
   909
                    int indx = i + readShortArg(byteCodes, i);
jaroslav@839
   910
                    emitIf(out, "if (@1 !== null) ",
jaroslav@843
   911
                         smapper.popA(), i, indx, topMostLabel);
jaroslav@16
   912
                    i += 2;
jaroslav@16
   913
                    break;
jaroslav@16
   914
                }
jaroslav@151
   915
                case opc_ifnull: {
jaroslav@864
   916
                    int indx = i + readShortArg(byteCodes, i);
jaroslav@839
   917
                    emitIf(out, "if (@1 === null) ",
jaroslav@843
   918
                         smapper.popA(), i, indx, topMostLabel);
jaroslav@16
   919
                    i += 2;
jaroslav@16
   920
                    break;
jaroslav@16
   921
                }
jaroslav@151
   922
                case opc_if_icmpne:
lubomir@307
   923
                    i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
jaroslav@843
   924
                                   "!=", topMostLabel);
jaroslav@4
   925
                    break;
jaroslav@151
   926
                case opc_if_icmplt:
lubomir@307
   927
                    i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
jaroslav@843
   928
                                   "<", topMostLabel);
jaroslav@4
   929
                    break;
jaroslav@151
   930
                case opc_if_icmple:
lubomir@307
   931
                    i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
jaroslav@843
   932
                                   "<=", topMostLabel);
jaroslav@4
   933
                    break;
jaroslav@151
   934
                case opc_if_icmpgt:
lubomir@307
   935
                    i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
jaroslav@843
   936
                                   ">", topMostLabel);
jaroslav@4
   937
                    break;
jaroslav@151
   938
                case opc_if_icmpge:
lubomir@307
   939
                    i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
jaroslav@843
   940
                                   ">=", topMostLabel);
jaroslav@4
   941
                    break;
jaroslav@151
   942
                case opc_goto: {
jaroslav@864
   943
                    int indx = i + readShortArg(byteCodes, i);
jaroslav@843
   944
                    goTo(out, i, indx, topMostLabel);
jaroslav@5
   945
                    i += 2;
jaroslav@5
   946
                    break;
jaroslav@5
   947
                }
jaroslav@151
   948
                case opc_lookupswitch: {
jaroslav@843
   949
                    i = generateLookupSwitch(i, byteCodes, smapper, topMostLabel);
jaroslav@115
   950
                    break;
jaroslav@115
   951
                }
jaroslav@151
   952
                case opc_tableswitch: {
jaroslav@843
   953
                    i = generateTableSwitch(i, byteCodes, smapper, topMostLabel);
jaroslav@115
   954
                    break;
jaroslav@115
   955
                }
jaroslav@151
   956
                case opc_invokeinterface: {
lubomir@307
   957
                    i = invokeVirtualMethod(byteCodes, i, smapper) + 2;
jaroslav@46
   958
                    break;
jaroslav@46
   959
                }
jaroslav@151
   960
                case opc_invokevirtual:
lubomir@307
   961
                    i = invokeVirtualMethod(byteCodes, i, smapper);
jaroslav@12
   962
                    break;
jaroslav@151
   963
                case opc_invokespecial:
lubomir@307
   964
                    i = invokeStaticMethod(byteCodes, i, smapper, false);
jaroslav@4
   965
                    break;
jaroslav@151
   966
                case opc_invokestatic:
lubomir@307
   967
                    i = invokeStaticMethod(byteCodes, i, smapper, true);
jaroslav@10
   968
                    break;
jaroslav@151
   969
                case opc_new: {
jaroslav@864
   970
                    int indx = readUShortArg(byteCodes, i);
jaroslav@151
   971
                    String ci = jc.getClassName(indx);
lubomir@474
   972
                    emit(out, "var @1 = new @2;",
jaroslav@1409
   973
                         smapper.pushA(), accessClass(mangleClassName(ci)));
jaroslav@151
   974
                    addReference(ci);
jaroslav@8
   975
                    i += 2;
jaroslav@8
   976
                    break;
jaroslav@8
   977
                }
lubomir@283
   978
                case opc_newarray:
lubomir@585
   979
                    int atype = readUByte(byteCodes, ++i);
jaroslav@842
   980
                    generateNewArray(atype, smapper);
jaroslav@21
   981
                    break;
jaroslav@448
   982
                case opc_anewarray: {
jaroslav@864
   983
                    int type = readUShortArg(byteCodes, i);
jaroslav@448
   984
                    i += 2;
jaroslav@842
   985
                    generateANewArray(type, smapper);
jaroslav@21
   986
                    break;
jaroslav@448
   987
                }
jaroslav@151
   988
                case opc_multianewarray: {
jaroslav@864
   989
                    int type = readUShortArg(byteCodes, i);
jtulach@128
   990
                    i += 2;
jaroslav@842
   991
                    i = generateMultiANewArray(type, byteCodes, i, smapper);
jtulach@128
   992
                    break;
jtulach@128
   993
                }
jaroslav@151
   994
                case opc_arraylength:
lubomir@474
   995
                    emit(out, "var @2 = @1.length;",
lubomir@474
   996
                         smapper.popA(), smapper.pushI());
jaroslav@272
   997
                    break;
lubomir@283
   998
                case opc_lastore:
jaroslav@1392
   999
                    emit(out, "Array.at(@3, @2, @1);",
lubomir@307
  1000
                         smapper.popL(), smapper.popI(), smapper.popA());
lubomir@281
  1001
                    break;
lubomir@283
  1002
                case opc_fastore:
jaroslav@1392
  1003
                    emit(out, "Array.at(@3, @2, @1);",
lubomir@307
  1004
                         smapper.popF(), smapper.popI(), smapper.popA());
lubomir@281
  1005
                    break;
lubomir@283
  1006
                case opc_dastore:
jaroslav@1392
  1007
                    emit(out, "Array.at(@3, @2, @1);",
lubomir@307
  1008
                         smapper.popD(), smapper.popI(), smapper.popA());
lubomir@281
  1009
                    break;
lubomir@283
  1010
                case opc_aastore:
jaroslav@1392
  1011
                    emit(out, "Array.at(@3, @2, @1);",
lubomir@307
  1012
                         smapper.popA(), smapper.popI(), smapper.popA());
jaroslav@21
  1013
                    break;
jaroslav@151
  1014
                case opc_iastore:
jaroslav@151
  1015
                case opc_bastore:
jaroslav@151
  1016
                case opc_castore:
lubomir@283
  1017
                case opc_sastore:
jaroslav@1392
  1018
                    emit(out, "Array.at(@3, @2, @1);",
lubomir@307
  1019
                         smapper.popI(), smapper.popI(), smapper.popA());
jaroslav@240
  1020
                    break;
lubomir@283
  1021
                case opc_laload:
jaroslav@1392
  1022
                    emit(out, "var @3 = Array.at(@2, @1);",
lubomir@307
  1023
                         smapper.popI(), smapper.popA(), smapper.pushL());
lubomir@281
  1024
                    break;
lubomir@283
  1025
                case opc_faload:
jaroslav@1392
  1026
                    emit(out, "var @3 = Array.at(@2, @1);",
lubomir@307
  1027
                         smapper.popI(), smapper.popA(), smapper.pushF());
lubomir@281
  1028
                    break;
lubomir@283
  1029
                case opc_daload:
jaroslav@1392
  1030
                    emit(out, "var @3 = Array.at(@2, @1);",
lubomir@307
  1031
                         smapper.popI(), smapper.popA(), smapper.pushD());
lubomir@281
  1032
                    break;
lubomir@283
  1033
                case opc_aaload:
jaroslav@1392
  1034
                    emit(out, "var @3 = Array.at(@2, @1);",
lubomir@307
  1035
                         smapper.popI(), smapper.popA(), smapper.pushA());
jaroslav@272
  1036
                    break;
jaroslav@272
  1037
                case opc_iaload:
jaroslav@272
  1038
                case opc_baload:
jaroslav@272
  1039
                case opc_caload:
lubomir@283
  1040
                case opc_saload:
jaroslav@1392
  1041
                    emit(out, "var @3 = Array.at(@2, @1);",
lubomir@307
  1042
                         smapper.popI(), smapper.popA(), smapper.pushI());
jaroslav@272
  1043
                    break;
lubomir@221
  1044
                case opc_pop:
jaroslav@272
  1045
                case opc_pop2:
lubomir@307
  1046
                    smapper.pop(1);
jaroslav@399
  1047
                    debug("/* pop */");
jaroslav@272
  1048
                    break;
lubomir@281
  1049
                case opc_dup: {
lubomir@307
  1050
                    final Variable v = smapper.get(0);
lubomir@474
  1051
                    emit(out, "var @1 = @2;", smapper.pushT(v.getType()), v);
jaroslav@21
  1052
                    break;
jaroslav@21
  1053
                }
lubomir@281
  1054
                case opc_dup2: {
lubomir@585
  1055
                    final Variable vi1 = smapper.get(0);
lubomir@585
  1056
lubomir@585
  1057
                    if (vi1.isCategory2()) {
lubomir@474
  1058
                        emit(out, "var @1 = @2;",
lubomir@585
  1059
                             smapper.pushT(vi1.getType()), vi1);
lubomir@281
  1060
                    } else {
lubomir@585
  1061
                        final Variable vi2 = smapper.get(1);
lubomir@474
  1062
                        emit(out, "var @1 = @2, @3 = @4;",
lubomir@585
  1063
                             smapper.pushT(vi2.getType()), vi2,
lubomir@585
  1064
                             smapper.pushT(vi1.getType()), vi1);
lubomir@281
  1065
                    }
jaroslav@21
  1066
                    break;
jaroslav@21
  1067
                }
lubomir@281
  1068
                case opc_dup_x1: {
lubomir@307
  1069
                    final Variable vi1 = smapper.pop();
lubomir@307
  1070
                    final Variable vi2 = smapper.pop();
lubomir@307
  1071
                    final Variable vo3 = smapper.pushT(vi1.getType());
lubomir@307
  1072
                    final Variable vo2 = smapper.pushT(vi2.getType());
lubomir@307
  1073
                    final Variable vo1 = smapper.pushT(vi1.getType());
lubomir@281
  1074
lubomir@474
  1075
                    emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
lubomir@283
  1076
                         vo1, vi1, vo2, vi2, vo3, vo1);
jaroslav@93
  1077
                    break;
lubomir@281
  1078
                }
lubomir@585
  1079
                case opc_dup2_x1: {
lubomir@585
  1080
                    final Variable vi1 = smapper.pop();
lubomir@585
  1081
                    final Variable vi2 = smapper.pop();
lubomir@585
  1082
lubomir@585
  1083
                    if (vi1.isCategory2()) {
lubomir@307
  1084
                        final Variable vo3 = smapper.pushT(vi1.getType());
lubomir@307
  1085
                        final Variable vo2 = smapper.pushT(vi2.getType());
lubomir@307
  1086
                        final Variable vo1 = smapper.pushT(vi1.getType());
lubomir@281
  1087
lubomir@474
  1088
                        emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
lubomir@283
  1089
                             vo1, vi1, vo2, vi2, vo3, vo1);
lubomir@281
  1090
                    } else {
lubomir@585
  1091
                        final Variable vi3 = smapper.pop();
lubomir@585
  1092
                        final Variable vo5 = smapper.pushT(vi2.getType());
lubomir@585
  1093
                        final Variable vo4 = smapper.pushT(vi1.getType());
lubomir@585
  1094
                        final Variable vo3 = smapper.pushT(vi3.getType());
lubomir@585
  1095
                        final Variable vo2 = smapper.pushT(vi2.getType());
lubomir@585
  1096
                        final Variable vo1 = smapper.pushT(vi1.getType());
lubomir@585
  1097
lubomir@585
  1098
                        emit(out, "var @1 = @2, @3 = @4, @5 = @6,",
lubomir@585
  1099
                             vo1, vi1, vo2, vi2, vo3, vi3);
lubomir@585
  1100
                        emit(out, " @1 = @2, @3 = @4;",
lubomir@585
  1101
                             vo4, vo1, vo5, vo2);
lubomir@585
  1102
                    }
lubomir@585
  1103
                    break;
lubomir@585
  1104
                }
lubomir@585
  1105
                case opc_dup_x2: {
lubomir@585
  1106
                    final Variable vi1 = smapper.pop();
lubomir@585
  1107
                    final Variable vi2 = smapper.pop();
lubomir@585
  1108
lubomir@585
  1109
                    if (vi2.isCategory2()) {
lubomir@585
  1110
                        final Variable vo3 = smapper.pushT(vi1.getType());
lubomir@585
  1111
                        final Variable vo2 = smapper.pushT(vi2.getType());
lubomir@585
  1112
                        final Variable vo1 = smapper.pushT(vi1.getType());
lubomir@585
  1113
lubomir@585
  1114
                        emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
lubomir@585
  1115
                             vo1, vi1, vo2, vi2, vo3, vo1);
lubomir@585
  1116
                    } else {
lubomir@307
  1117
                        final Variable vi3 = smapper.pop();
lubomir@307
  1118
                        final Variable vo4 = smapper.pushT(vi1.getType());
lubomir@307
  1119
                        final Variable vo3 = smapper.pushT(vi3.getType());
lubomir@307
  1120
                        final Variable vo2 = smapper.pushT(vi2.getType());
lubomir@307
  1121
                        final Variable vo1 = smapper.pushT(vi1.getType());
lubomir@281
  1122
lubomir@474
  1123
                        emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8;",
lubomir@283
  1124
                             vo1, vi1, vo2, vi2, vo3, vi3, vo4, vo1);
lubomir@281
  1125
                    }
jaroslav@8
  1126
                    break;
lubomir@281
  1127
                }
lubomir@585
  1128
                case opc_dup2_x2: {
lubomir@585
  1129
                    final Variable vi1 = smapper.pop();
lubomir@585
  1130
                    final Variable vi2 = smapper.pop();
lubomir@585
  1131
lubomir@585
  1132
                    if (vi1.isCategory2()) {
lubomir@585
  1133
                        if (vi2.isCategory2()) {
lubomir@585
  1134
                            final Variable vo3 = smapper.pushT(vi1.getType());
lubomir@585
  1135
                            final Variable vo2 = smapper.pushT(vi2.getType());
lubomir@585
  1136
                            final Variable vo1 = smapper.pushT(vi1.getType());
lubomir@585
  1137
lubomir@585
  1138
                            emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
lubomir@585
  1139
                                 vo1, vi1, vo2, vi2, vo3, vo1);
lubomir@585
  1140
                        } else {
lubomir@585
  1141
                            final Variable vi3 = smapper.pop();
lubomir@585
  1142
                            final Variable vo4 = smapper.pushT(vi1.getType());
lubomir@585
  1143
                            final Variable vo3 = smapper.pushT(vi3.getType());
lubomir@585
  1144
                            final Variable vo2 = smapper.pushT(vi2.getType());
lubomir@585
  1145
                            final Variable vo1 = smapper.pushT(vi1.getType());
lubomir@585
  1146
lubomir@585
  1147
                            emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8;",
lubomir@585
  1148
                                 vo1, vi1, vo2, vi2, vo3, vi3, vo4, vo1);
lubomir@585
  1149
                        }
lubomir@585
  1150
                    } else {
lubomir@585
  1151
                        final Variable vi3 = smapper.pop();
lubomir@585
  1152
lubomir@585
  1153
                        if (vi3.isCategory2()) {
lubomir@585
  1154
                            final Variable vo5 = smapper.pushT(vi2.getType());
lubomir@585
  1155
                            final Variable vo4 = smapper.pushT(vi1.getType());
lubomir@585
  1156
                            final Variable vo3 = smapper.pushT(vi3.getType());
lubomir@585
  1157
                            final Variable vo2 = smapper.pushT(vi2.getType());
lubomir@585
  1158
                            final Variable vo1 = smapper.pushT(vi1.getType());
lubomir@585
  1159
lubomir@585
  1160
                            emit(out, "var @1 = @2, @3 = @4, @5 = @6,",
lubomir@585
  1161
                                 vo1, vi1, vo2, vi2, vo3, vi3);
lubomir@585
  1162
                            emit(out, " @1 = @2, @3 = @4;",
lubomir@585
  1163
                                 vo4, vo1, vo5, vo2);
lubomir@585
  1164
                        } else {
lubomir@585
  1165
                            final Variable vi4 = smapper.pop();
lubomir@585
  1166
                            final Variable vo6 = smapper.pushT(vi2.getType());
lubomir@585
  1167
                            final Variable vo5 = smapper.pushT(vi1.getType());
lubomir@585
  1168
                            final Variable vo4 = smapper.pushT(vi4.getType());
lubomir@585
  1169
                            final Variable vo3 = smapper.pushT(vi3.getType());
lubomir@585
  1170
                            final Variable vo2 = smapper.pushT(vi2.getType());
lubomir@585
  1171
                            final Variable vo1 = smapper.pushT(vi1.getType());
lubomir@585
  1172
                            
lubomir@585
  1173
                            emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8,",
lubomir@585
  1174
                                 vo1, vi1, vo2, vi2, vo3, vi3, vo4, vi4);
lubomir@585
  1175
                            emit(out, " @1 = @2, @3 = @4;",
lubomir@585
  1176
                                 vo5, vo1, vo6, vo2);
lubomir@585
  1177
                        }
lubomir@585
  1178
                    }
lubomir@585
  1179
                    break;
lubomir@585
  1180
                }
lubomir@585
  1181
                case opc_swap: {
lubomir@585
  1182
                    final Variable vi1 = smapper.get(0);
lubomir@585
  1183
                    final Variable vi2 = smapper.get(1);
lubomir@585
  1184
lubomir@585
  1185
                    if (vi1.getType() == vi2.getType()) {
lubomir@585
  1186
                        final Variable tmp = smapper.pushT(vi1.getType());
lubomir@585
  1187
lubomir@585
  1188
                        emit(out, "var @1 = @2, @2 = @3, @3 = @1;",
lubomir@585
  1189
                             tmp, vi1, vi2);
lubomir@585
  1190
                        smapper.pop(1);
lubomir@585
  1191
                    } else {
lubomir@585
  1192
                        smapper.pop(2);
lubomir@585
  1193
                        smapper.pushT(vi1.getType());
lubomir@585
  1194
                        smapper.pushT(vi2.getType());
lubomir@585
  1195
                    }
lubomir@585
  1196
                    break;
lubomir@585
  1197
                }
jaroslav@151
  1198
                case opc_bipush:
lubomir@474
  1199
                    emit(out, "var @1 = @2;",
lubomir@307
  1200
                         smapper.pushI(), Integer.toString(byteCodes[++i]));
jaroslav@8
  1201
                    break;
jaroslav@151
  1202
                case opc_sipush:
lubomir@474
  1203
                    emit(out, "var @1 = @2;",
lubomir@307
  1204
                         smapper.pushI(),
jaroslav@864
  1205
                         Integer.toString(readShortArg(byteCodes, i)));
jaroslav@31
  1206
                    i += 2;
jaroslav@31
  1207
                    break;
jaroslav@151
  1208
                case opc_getfield: {
jaroslav@864
  1209
                    int indx = readUShortArg(byteCodes, i);
jaroslav@151
  1210
                    String[] fi = jc.getFieldInfoName(indx);
lubomir@307
  1211
                    final int type = VarType.fromFieldType(fi[2].charAt(0));
jaroslav@1409
  1212
                    final String mangleClass = mangleClassName(fi[0]);
jaroslav@592
  1213
                    final String mangleClassAccess = accessClass(mangleClass);
jaroslav@592
  1214
                    emit(out, "var @2 = @4(false)._@3.call(@1);",
jaroslav@592
  1215
                         smapper.popA(),
jaroslav@592
  1216
                         smapper.pushT(type), fi[1], mangleClassAccess
jaroslav@592
  1217
                    );
jaroslav@592
  1218
                    i += 2;
jaroslav@592
  1219
                    break;
jaroslav@592
  1220
                }
jaroslav@592
  1221
                case opc_putfield: {
jaroslav@864
  1222
                    int indx = readUShortArg(byteCodes, i);
jaroslav@592
  1223
                    String[] fi = jc.getFieldInfoName(indx);
jaroslav@592
  1224
                    final int type = VarType.fromFieldType(fi[2].charAt(0));
jaroslav@1409
  1225
                    final String mangleClass = mangleClassName(fi[0]);
jaroslav@592
  1226
                    final String mangleClassAccess = accessClass(mangleClass);
jaroslav@592
  1227
                    emit(out, "@4(false)._@3.call(@2, @1);",
jaroslav@592
  1228
                         smapper.popT(type),
jaroslav@592
  1229
                         smapper.popA(), fi[1], 
jaroslav@592
  1230
                         mangleClassAccess
jaroslav@592
  1231
                    );
jaroslav@8
  1232
                    i += 2;
jaroslav@8
  1233
                    break;
jaroslav@8
  1234
                }
jaroslav@151
  1235
                case opc_getstatic: {
jaroslav@864
  1236
                    int indx = readUShortArg(byteCodes, i);
jaroslav@151
  1237
                    String[] fi = jc.getFieldInfoName(indx);
lubomir@307
  1238
                    final int type = VarType.fromFieldType(fi[2].charAt(0));
jaroslav@775
  1239
                    emit(out, "var @1 = @2(false)._@3();",
lubomir@317
  1240
                         smapper.pushT(type),
jaroslav@1409
  1241
                         accessClass(mangleClassName(fi[0])), fi[1]);
jaroslav@9
  1242
                    i += 2;
jaroslav@151
  1243
                    addReference(fi[0]);
jaroslav@9
  1244
                    break;
jaroslav@9
  1245
                }
lubomir@281
  1246
                case opc_putstatic: {
jaroslav@864
  1247
                    int indx = readUShortArg(byteCodes, i);
lubomir@281
  1248
                    String[] fi = jc.getFieldInfoName(indx);
lubomir@307
  1249
                    final int type = VarType.fromFieldType(fi[2].charAt(0));
jaroslav@775
  1250
                    emit(out, "@1(false)._@2(@3);",
jaroslav@1409
  1251
                         accessClass(mangleClassName(fi[0])), fi[1],
lubomir@317
  1252
                         smapper.popT(type));
lubomir@281
  1253
                    i += 2;
lubomir@281
  1254
                    addReference(fi[0]);
lubomir@281
  1255
                    break;
lubomir@281
  1256
                }
jaroslav@151
  1257
                case opc_checkcast: {
jaroslav@864
  1258
                    int indx = readUShortArg(byteCodes, i);
jaroslav@842
  1259
                    generateCheckcast(indx, smapper);
jaroslav@30
  1260
                    i += 2;
jaroslav@30
  1261
                    break;
jaroslav@30
  1262
                }
jaroslav@151
  1263
                case opc_instanceof: {
jaroslav@864
  1264
                    int indx = readUShortArg(byteCodes, i);
jaroslav@842
  1265
                    generateInstanceOf(indx, smapper);
jaroslav@17
  1266
                    i += 2;
jaroslav@30
  1267
                    break;
jaroslav@17
  1268
                }
jaroslav@151
  1269
                case opc_athrow: {
lubomir@307
  1270
                    final Variable v = smapper.popA();
lubomir@307
  1271
                    smapper.clear();
lubomir@281
  1272
lubomir@474
  1273
                    emit(out, "{ var @1 = @2; throw @2; }",
lubomir@307
  1274
                         smapper.pushA(), v);
jaroslav@104
  1275
                    break;
jaroslav@104
  1276
                }
lubomir@221
  1277
lubomir@221
  1278
                case opc_monitorenter: {
lubomir@221
  1279
                    out.append("/* monitor enter */");
lubomir@307
  1280
                    smapper.popA();
lubomir@221
  1281
                    break;
lubomir@221
  1282
                }
lubomir@221
  1283
lubomir@221
  1284
                case opc_monitorexit: {
lubomir@221
  1285
                    out.append("/* monitor exit */");
lubomir@307
  1286
                    smapper.popA();
lubomir@221
  1287
                    break;
lubomir@221
  1288
                }
lubomir@221
  1289
lubomir@585
  1290
                case opc_wide:
lubomir@585
  1291
                    wide = true;
lubomir@585
  1292
                    break;
lubomir@585
  1293
jaroslav@104
  1294
                default: {
lubomir@585
  1295
                    wide = false;
lubomir@283
  1296
                    emit(out, "throw 'unknown bytecode @1';",
lubomir@283
  1297
                         Integer.toString(c));
jaroslav@104
  1298
                }
jaroslav@0
  1299
            }
jaroslav@399
  1300
            if (debug(" //")) {
jaroslav@842
  1301
                generateByteCodeComment(prev, i, byteCodes);
jaroslav@0
  1302
            }
tzezula@285
  1303
            out.append("\n");            
jaroslav@0
  1304
        }
jaroslav@398
  1305
        if (previousTrap != null) {
jaroslav@843
  1306
            generateCatch(previousTrap, byteCodes.length, topMostLabel);
jaroslav@398
  1307
        }
jaroslav@839
  1308
        out.append("\n    }\n");
jaroslav@839
  1309
        while (openBraces-- > 0) {
jaroslav@839
  1310
            out.append('}');
jaroslav@839
  1311
        }
jaroslav@839
  1312
        out.append("\n};");
jaroslav@4
  1313
    }
jaroslav@4
  1314
jaroslav@843
  1315
    private int generateIf(byte[] byteCodes, int i, final Variable v2, final Variable v1, final String test, int topMostLabel) throws IOException {
jaroslav@864
  1316
        int indx = i + readShortArg(byteCodes, i);
lubomir@281
  1317
        out.append("if (").append(v1)
lubomir@221
  1318
           .append(' ').append(test).append(' ')
jaroslav@839
  1319
           .append(v2).append(") ");
jaroslav@843
  1320
        goTo(out, i, indx, topMostLabel);
jaroslav@4
  1321
        return i + 2;
jaroslav@4
  1322
    }
jaroslav@839
  1323
    
lubomir@697
  1324
    private int readInt4(byte[] byteCodes, int offset) {
lubomir@697
  1325
        final int d = byteCodes[offset + 0] << 24;
lubomir@697
  1326
        final int c = byteCodes[offset + 1] << 16;
lubomir@697
  1327
        final int b = byteCodes[offset + 2] << 8;
lubomir@697
  1328
        final int a = byteCodes[offset + 3];
jaroslav@115
  1329
        return (d & 0xff000000) | (c & 0xff0000) | (b & 0xff00) | (a & 0xff);
jaroslav@115
  1330
    }
jaroslav@864
  1331
    private static int readUByte(byte[] byteCodes, int offset) {
lubomir@697
  1332
        return byteCodes[offset] & 0xff;
jtulach@128
  1333
    }
lubomir@585
  1334
jaroslav@864
  1335
    private static int readUShort(byte[] byteCodes, int offset) {
lubomir@697
  1336
        return ((byteCodes[offset] & 0xff) << 8)
lubomir@697
  1337
                    | (byteCodes[offset + 1] & 0xff);
lubomir@697
  1338
    }
jaroslav@864
  1339
    private static int readUShortArg(byte[] byteCodes, int offsetInstruction) {
jaroslav@864
  1340
        return readUShort(byteCodes, offsetInstruction + 1);
jaroslav@864
  1341
    }
lubomir@697
  1342
jaroslav@864
  1343
    private static int readShort(byte[] byteCodes, int offset) {
jaroslav@864
  1344
        int signed = byteCodes[offset];
jaroslav@864
  1345
        byte b0 = (byte)signed;
jaroslav@864
  1346
        return (b0 << 8) | (byteCodes[offset + 1] & 0xff);
jaroslav@864
  1347
    }
jaroslav@864
  1348
    private static int readShortArg(byte[] byteCodes, int offsetInstruction) {
jaroslav@864
  1349
        return readShort(byteCodes, offsetInstruction + 1);
lubomir@585
  1350
    }
lubomir@585
  1351
lubomir@281
  1352
    private static void countArgs(String descriptor, char[] returnType, StringBuilder sig, StringBuilder cnt) {
jaroslav@4
  1353
        int i = 0;
jaroslav@4
  1354
        Boolean count = null;
jaroslav@32
  1355
        boolean array = false;
jaroslav@248
  1356
        sig.append("__");
jaroslav@10
  1357
        int firstPos = sig.length();
jaroslav@4
  1358
        while (i < descriptor.length()) {
jaroslav@4
  1359
            char ch = descriptor.charAt(i++);
jaroslav@4
  1360
            switch (ch) {
jaroslav@4
  1361
                case '(':
jaroslav@4
  1362
                    count = true;
jaroslav@4
  1363
                    continue;
jaroslav@4
  1364
                case ')':
jaroslav@4
  1365
                    count = false;
jaroslav@4
  1366
                    continue;
jaroslav@4
  1367
                case 'B': 
jaroslav@4
  1368
                case 'C': 
jaroslav@4
  1369
                case 'D': 
jaroslav@4
  1370
                case 'F': 
jaroslav@4
  1371
                case 'I': 
jaroslav@4
  1372
                case 'J': 
jaroslav@4
  1373
                case 'S': 
jaroslav@4
  1374
                case 'Z': 
jaroslav@4
  1375
                    if (count) {
jaroslav@32
  1376
                        if (array) {
jaroslav@248
  1377
                            sig.append("_3");
jaroslav@32
  1378
                        }
jaroslav@4
  1379
                        sig.append(ch);
jtulach@156
  1380
                        if (ch == 'J' || ch == 'D') {
jtulach@156
  1381
                            cnt.append('1');
jtulach@156
  1382
                        } else {
jtulach@156
  1383
                            cnt.append('0');
jtulach@156
  1384
                        }
jaroslav@4
  1385
                    } else {
jaroslav@10
  1386
                        sig.insert(firstPos, ch);
jaroslav@32
  1387
                        if (array) {
lubomir@281
  1388
                            returnType[0] = '[';
jaroslav@248
  1389
                            sig.insert(firstPos, "_3");
lubomir@281
  1390
                        } else {
lubomir@281
  1391
                            returnType[0] = ch;
jaroslav@32
  1392
                        }
jaroslav@4
  1393
                    }
jaroslav@93
  1394
                    array = false;
jaroslav@4
  1395
                    continue;
jaroslav@4
  1396
                case 'V': 
jaroslav@4
  1397
                    assert !count;
lubomir@281
  1398
                    returnType[0] = 'V';
jaroslav@10
  1399
                    sig.insert(firstPos, 'V');
jaroslav@4
  1400
                    continue;
jaroslav@4
  1401
                case 'L':
jaroslav@16
  1402
                    int next = descriptor.indexOf(';', i);
jaroslav@248
  1403
                    String realSig = mangleSig(descriptor, i - 1, next + 1);
jaroslav@4
  1404
                    if (count) {
jaroslav@32
  1405
                        if (array) {
jaroslav@248
  1406
                            sig.append("_3");
jaroslav@32
  1407
                        }
jaroslav@248
  1408
                        sig.append(realSig);
jtulach@156
  1409
                        cnt.append('0');
jaroslav@4
  1410
                    } else {
jaroslav@248
  1411
                        sig.insert(firstPos, realSig);
jaroslav@32
  1412
                        if (array) {
jaroslav@248
  1413
                            sig.insert(firstPos, "_3");
jaroslav@32
  1414
                        }
lubomir@281
  1415
                        returnType[0] = 'L';
jaroslav@4
  1416
                    }
jaroslav@16
  1417
                    i = next + 1;
lubomir@407
  1418
                    array = false;
jaroslav@4
  1419
                    continue;
jaroslav@4
  1420
                case '[':
jaroslav@248
  1421
                    array = true;
jaroslav@4
  1422
                    continue;
jaroslav@4
  1423
                default:
jaroslav@248
  1424
                    throw new IllegalStateException("Invalid char: " + ch);
jaroslav@4
  1425
            }
jaroslav@4
  1426
        }
jaroslav@0
  1427
    }
jaroslav@248
  1428
    
jaroslav@592
  1429
    static String mangleSig(String sig) {
jaroslav@592
  1430
        return mangleSig(sig, 0, sig.length());
jaroslav@592
  1431
    }
jaroslav@592
  1432
    
jaroslav@1409
  1433
    private static String mangleMethodName(String name) {
jaroslav@1409
  1434
        StringBuilder sb = new StringBuilder(name.length() * 2);
jaroslav@1409
  1435
        int last = name.length();
jaroslav@1409
  1436
        for (int i = 0; i < last; i++) {
jaroslav@1409
  1437
            final char ch = name.charAt(i);
jaroslav@1409
  1438
            switch (ch) {
jaroslav@1409
  1439
                case '_': sb.append("_1"); break;
jaroslav@1409
  1440
                default: sb.append(ch); break;
jaroslav@1409
  1441
            }
jaroslav@1409
  1442
        }
jaroslav@1409
  1443
        return sb.toString();
jaroslav@1409
  1444
    }
jaroslav@248
  1445
    private static String mangleSig(String txt, int first, int last) {
jaroslav@1409
  1446
        StringBuilder sb = new StringBuilder((last - first) * 2);
jaroslav@248
  1447
        for (int i = first; i < last; i++) {
jaroslav@248
  1448
            final char ch = txt.charAt(i);
jaroslav@248
  1449
            switch (ch) {
jaroslav@248
  1450
                case '/': sb.append('_'); break;
jaroslav@248
  1451
                case '_': sb.append("_1"); break;
jaroslav@248
  1452
                case ';': sb.append("_2"); break;
jaroslav@248
  1453
                case '[': sb.append("_3"); break;
jaroslav@248
  1454
                default: sb.append(ch); break;
jaroslav@248
  1455
            }
jaroslav@248
  1456
        }
jaroslav@248
  1457
        return sb.toString();
jaroslav@248
  1458
    }
jaroslav@1409
  1459
    
jaroslav@1409
  1460
    private static String mangleClassName(String name) {
jaroslav@1409
  1461
        return mangleSig(name);
jaroslav@1409
  1462
    }
jaroslav@9
  1463
jaroslav@248
  1464
    private static String findMethodName(MethodData m, StringBuilder cnt) {
jaroslav@42
  1465
        StringBuilder name = new StringBuilder();
jaroslav@10
  1466
        if ("<init>".equals(m.getName())) { // NOI18N
jaroslav@42
  1467
            name.append("cons"); // NOI18N
jaroslav@19
  1468
        } else if ("<clinit>".equals(m.getName())) { // NOI18N
jaroslav@42
  1469
            name.append("class"); // NOI18N
jaroslav@10
  1470
        } else {
jaroslav@1409
  1471
            name.append(mangleMethodName(m.getName()));
jaroslav@10
  1472
        } 
jaroslav@42
  1473
        
lubomir@282
  1474
        countArgs(m.getInternalSig(), new char[1], name, cnt);
jaroslav@42
  1475
        return name.toString();
jaroslav@10
  1476
    }
jaroslav@10
  1477
lubomir@282
  1478
    static String findMethodName(String[] mi, StringBuilder cnt, char[] returnType) {
jaroslav@10
  1479
        StringBuilder name = new StringBuilder();
jaroslav@151
  1480
        String descr = mi[2];//mi.getDescriptor();
jaroslav@151
  1481
        String nm= mi[1];
jaroslav@151
  1482
        if ("<init>".equals(nm)) { // NOI18N
jaroslav@10
  1483
            name.append("cons"); // NOI18N
jaroslav@10
  1484
        } else {
jaroslav@1409
  1485
            name.append(mangleMethodName(nm));
jaroslav@10
  1486
        }
lubomir@282
  1487
        countArgs(descr, returnType, name, cnt);
jaroslav@10
  1488
        return name.toString();
jaroslav@10
  1489
    }
jaroslav@10
  1490
lubomir@307
  1491
    private int invokeStaticMethod(byte[] byteCodes, int i, final StackMapper mapper, boolean isStatic)
jaroslav@10
  1492
    throws IOException {
jaroslav@864
  1493
        int methodIndex = readUShortArg(byteCodes, i);
jaroslav@151
  1494
        String[] mi = jc.getFieldInfoName(methodIndex);
lubomir@281
  1495
        char[] returnType = { 'V' };
jtulach@156
  1496
        StringBuilder cnt = new StringBuilder();
lubomir@281
  1497
        String mn = findMethodName(mi, cnt, returnType);
lubomir@221
  1498
lubomir@221
  1499
        final int numArguments = isStatic ? cnt.length() : cnt.length() + 1;
lubomir@281
  1500
        final Variable[] vars = new Variable[numArguments];
lubomir@221
  1501
lubomir@281
  1502
        for (int j = numArguments - 1; j >= 0; --j) {
lubomir@281
  1503
            vars[j] = mapper.pop();
jaroslav@11
  1504
        }
lubomir@281
  1505
lubomir@281
  1506
        if (returnType[0] != 'V') {
lubomir@474
  1507
            out.append("var ")
lubomir@474
  1508
               .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
lubomir@281
  1509
               .append(" = ");
jaroslav@10
  1510
        }
lubomir@221
  1511
jaroslav@151
  1512
        final String in = mi[0];
jaroslav@1409
  1513
        out.append(accessClass(mangleClassName(in)));
jaroslav@224
  1514
        out.append("(false).");
jaroslav@397
  1515
        if (mn.startsWith("cons_")) {
jaroslav@397
  1516
            out.append("constructor.");
jaroslav@397
  1517
        }
jaroslav@10
  1518
        out.append(mn);
jaroslav@442
  1519
        if (isStatic) {
jaroslav@442
  1520
            out.append('(');
jaroslav@442
  1521
        } else {
jaroslav@442
  1522
            out.append(".call(");
jaroslav@442
  1523
        }
lubomir@221
  1524
        if (numArguments > 0) {
lubomir@281
  1525
            out.append(vars[0]);
lubomir@281
  1526
            for (int j = 1; j < numArguments; ++j) {
lubomir@221
  1527
                out.append(", ");
lubomir@281
  1528
                out.append(vars[j]);
lubomir@221
  1529
            }
jaroslav@10
  1530
        }
lubomir@221
  1531
        out.append(");");
jaroslav@10
  1532
        i += 2;
jaroslav@18
  1533
        addReference(in);
jaroslav@10
  1534
        return i;
jaroslav@10
  1535
    }
lubomir@307
  1536
    private int invokeVirtualMethod(byte[] byteCodes, int i, final StackMapper mapper)
jaroslav@12
  1537
    throws IOException {
jaroslav@864
  1538
        int methodIndex = readUShortArg(byteCodes, i);
jaroslav@151
  1539
        String[] mi = jc.getFieldInfoName(methodIndex);
lubomir@281
  1540
        char[] returnType = { 'V' };
jtulach@156
  1541
        StringBuilder cnt = new StringBuilder();
lubomir@281
  1542
        String mn = findMethodName(mi, cnt, returnType);
lubomir@221
  1543
lubomir@281
  1544
        final int numArguments = cnt.length() + 1;
lubomir@281
  1545
        final Variable[] vars = new Variable[numArguments];
lubomir@221
  1546
lubomir@281
  1547
        for (int j = numArguments - 1; j >= 0; --j) {
lubomir@281
  1548
            vars[j] = mapper.pop();
jaroslav@12
  1549
        }
lubomir@221
  1550
lubomir@281
  1551
        if (returnType[0] != 'V') {
lubomir@474
  1552
            out.append("var ")
lubomir@474
  1553
               .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
lubomir@281
  1554
               .append(" = ");
jaroslav@12
  1555
        }
lubomir@281
  1556
lubomir@281
  1557
        out.append(vars[0]).append('.');
jaroslav@12
  1558
        out.append(mn);
jaroslav@12
  1559
        out.append('(');
jaroslav@442
  1560
        String sep = "";
lubomir@281
  1561
        for (int j = 1; j < numArguments; ++j) {
jaroslav@442
  1562
            out.append(sep);
lubomir@281
  1563
            out.append(vars[j]);
jaroslav@442
  1564
            sep = ", ";
jaroslav@12
  1565
        }
lubomir@221
  1566
        out.append(");");
jaroslav@12
  1567
        i += 2;
jaroslav@12
  1568
        return i;
jaroslav@12
  1569
    }
lubomir@221
  1570
jaroslav@103
  1571
    private void addReference(String cn) throws IOException {
jtulach@162
  1572
        if (requireReference(cn)) {
jaroslav@399
  1573
            debug(" /* needs " + cn + " */");
jaroslav@18
  1574
        }
jaroslav@18
  1575
    }
jaroslav@16
  1576
jaroslav@33
  1577
    private void outType(String d, StringBuilder out) {
jaroslav@33
  1578
        int arr = 0;
jaroslav@33
  1579
        while (d.charAt(0) == '[') {
jaroslav@33
  1580
            out.append('A');
jaroslav@33
  1581
            d = d.substring(1);
jaroslav@33
  1582
        }
jaroslav@16
  1583
        if (d.charAt(0) == 'L') {
jaroslav@16
  1584
            assert d.charAt(d.length() - 1) == ';';
jaroslav@16
  1585
            out.append(d.replace('/', '_').substring(0, d.length() - 1));
jaroslav@16
  1586
        } else {
jaroslav@16
  1587
            out.append(d);
jaroslav@16
  1588
        }
jaroslav@16
  1589
    }
jaroslav@21
  1590
jaroslav@230
  1591
    private String encodeConstant(int entryIndex) throws IOException {
jaroslav@230
  1592
        String[] classRef = { null };
jaroslav@230
  1593
        String s = jc.stringValue(entryIndex, classRef);
jaroslav@230
  1594
        if (classRef[0] != null) {
jaroslav@567
  1595
            if (classRef[0].startsWith("[")) {
jaroslav@567
  1596
                s = accessClass("java_lang_Class") + "(false).forName__Ljava_lang_Class_2Ljava_lang_String_2('" + classRef[0] + "');";
jaroslav@567
  1597
            } else {
jaroslav@567
  1598
                addReference(classRef[0]);
jaroslav@1409
  1599
                s = accessClass(mangleClassName(s)) + "(false).constructor.$class";
jaroslav@567
  1600
            }
jaroslav@230
  1601
        }
jaroslav@151
  1602
        return s;
jaroslav@21
  1603
    }
jaroslav@32
  1604
lubomir@869
  1605
    private String javaScriptBody(String destObject, MethodData m, boolean isStatic) throws IOException {
jaroslav@152
  1606
        byte[] arr = m.findAnnotationData(true);
jaroslav@152
  1607
        if (arr == null) {
jaroslav@266
  1608
            return null;
jaroslav@152
  1609
        }
jaroslav@200
  1610
        final String jvmType = "Lorg/apidesign/bck2brwsr/core/JavaScriptBody;";
jaroslav@1239
  1611
        final String htmlType = "Lnet/java/html/js/JavaScriptBody;";
jaroslav@152
  1612
        class P extends AnnotationParser {
jaroslav@237
  1613
            public P() {
jaroslav@652
  1614
                super(false, true);
jaroslav@237
  1615
            }
jaroslav@237
  1616
            
jaroslav@152
  1617
            int cnt;
jaroslav@152
  1618
            String[] args = new String[30];
jaroslav@152
  1619
            String body;
jaroslav@1241
  1620
            boolean javacall;
jaroslav@1421
  1621
            boolean html4j;
jaroslav@94
  1622
            
jaroslav@152
  1623
            @Override
jaroslav@236
  1624
            protected void visitAttr(String type, String attr, String at, String value) {
jaroslav@152
  1625
                if (type.equals(jvmType)) {
jaroslav@152
  1626
                    if ("body".equals(attr)) {
jaroslav@152
  1627
                        body = value;
jaroslav@152
  1628
                    } else if ("args".equals(attr)) {
jaroslav@152
  1629
                        args[cnt++] = value;
jaroslav@152
  1630
                    } else {
jaroslav@152
  1631
                        throw new IllegalArgumentException(attr);
jaroslav@152
  1632
                    }
jaroslav@152
  1633
                }
jaroslav@1239
  1634
                if (type.equals(htmlType)) {
jaroslav@1421
  1635
                    html4j = true;
jaroslav@1239
  1636
                    if ("body".equals(attr)) {
jaroslav@1239
  1637
                        body = value;
jaroslav@1239
  1638
                    } else if ("args".equals(attr)) {
jaroslav@1239
  1639
                        args[cnt++] = value;
jaroslav@1241
  1640
                    } else if ("javacall".equals(attr)) {
jaroslav@1241
  1641
                        javacall = "1".equals(value);
jaroslav@1239
  1642
                    } else {
jaroslav@1239
  1643
                        throw new IllegalArgumentException(attr);
jaroslav@1239
  1644
                    }
jaroslav@1239
  1645
                }
jaroslav@94
  1646
            }
jaroslav@94
  1647
        }
jaroslav@152
  1648
        P p = new P();
jaroslav@152
  1649
        p.parse(arr, jc);
jaroslav@152
  1650
        if (p.body == null) {
jaroslav@266
  1651
            return null;
jaroslav@152
  1652
        }
jtulach@156
  1653
        StringBuilder cnt = new StringBuilder();
jaroslav@266
  1654
        final String mn = findMethodName(m, cnt);
lubomir@869
  1655
        out.append(destObject).append(".").append(mn);
jaroslav@203
  1656
        out.append(" = function(");
jaroslav@442
  1657
        String space = "";
jaroslav@443
  1658
        int index = 0;
jaroslav@1421
  1659
        StringBuilder toValue = new StringBuilder();
jtulach@156
  1660
        for (int i = 0; i < cnt.length(); i++) {
jaroslav@152
  1661
            out.append(space);
jaroslav@316
  1662
            space = outputArg(out, p.args, index);
jaroslav@1421
  1663
            if (p.html4j && space.length() > 0) {
jaroslav@1421
  1664
                toValue.append("\n  ").append(p.args[index]).append(" = vm.org_apidesign_bck2brwsr_emul_lang_System(false).toJS(").
jaroslav@1421
  1665
                    append(p.args[index]).append(");");
jaroslav@1421
  1666
            }
jaroslav@152
  1667
            index++;
jaroslav@152
  1668
        }
jaroslav@152
  1669
        out.append(") {").append("\n");
jaroslav@1421
  1670
        out.append(toValue.toString());
jaroslav@1242
  1671
        if (p.javacall) {
jaroslav@1242
  1672
            int lastSlash = jc.getClassName().lastIndexOf('/');
jaroslav@1242
  1673
            final String pkg = jc.getClassName().substring(0, lastSlash);
jaroslav@1242
  1674
            out.append(mangleCallbacks(pkg, p.body));
jaroslav@1242
  1675
            requireReference(pkg + "/$JsCallbacks$");
jaroslav@1242
  1676
        } else {
jaroslav@1242
  1677
            out.append(p.body);
jaroslav@1242
  1678
        }
jaroslav@152
  1679
        out.append("\n}\n");
jaroslav@266
  1680
        return mn;
jaroslav@151
  1681
    }
jaroslav@1242
  1682
    
jaroslav@1242
  1683
    private static CharSequence mangleCallbacks(String pkgName, String body) {
jaroslav@1242
  1684
        StringBuilder sb = new StringBuilder();
jaroslav@1242
  1685
        int pos = 0;
jaroslav@1242
  1686
        for (;;) {
jaroslav@1242
  1687
            int next = body.indexOf(".@", pos);
jaroslav@1242
  1688
            if (next == -1) {
jaroslav@1242
  1689
                sb.append(body.substring(pos));
jaroslav@1243
  1690
                body = sb.toString();
jaroslav@1243
  1691
                break;
jaroslav@1242
  1692
            }
jaroslav@1242
  1693
            int ident = next;
jaroslav@1242
  1694
            while (ident > 0) {
jaroslav@1242
  1695
                if (!Character.isJavaIdentifierPart(body.charAt(--ident))) {
jaroslav@1242
  1696
                    ident++;
jaroslav@1242
  1697
                    break;
jaroslav@1242
  1698
                }
jaroslav@1242
  1699
            }
jaroslav@1242
  1700
            String refId = body.substring(ident, next);
jaroslav@1242
  1701
jaroslav@1242
  1702
            sb.append(body.substring(pos, ident));
jaroslav@1242
  1703
jaroslav@1242
  1704
            int sigBeg = body.indexOf('(', next);
jaroslav@1242
  1705
            int sigEnd = body.indexOf(')', sigBeg);
jaroslav@1242
  1706
            int colon4 = body.indexOf("::", next);
jaroslav@1242
  1707
            if (sigBeg == -1 || sigEnd == -1 || colon4 == -1) {
jaroslav@1242
  1708
                throw new IllegalStateException("Malformed body " + body);
jaroslav@1242
  1709
            }
jaroslav@1242
  1710
            String fqn = body.substring(next + 2, colon4);
jaroslav@1242
  1711
            String method = body.substring(colon4 + 2, sigBeg);
jaroslav@1242
  1712
            String params = body.substring(sigBeg, sigEnd + 1);
jaroslav@1242
  1713
jaroslav@1242
  1714
            int paramBeg = body.indexOf('(', sigEnd + 1);
jaroslav@1242
  1715
            
jaroslav@1242
  1716
            sb.append("vm.").append(pkgName.replace('/', '_')).append("_$JsCallbacks$(false)._VM().");
jaroslav@1409
  1717
            sb.append(mangleJsCallbacks(fqn, method, params, false));
jaroslav@1242
  1718
            sb.append("(").append(refId);
jaroslav@1242
  1719
            if (body.charAt(paramBeg + 1) != ')') {
jaroslav@1242
  1720
                sb.append(",");
jaroslav@1242
  1721
            }
jaroslav@1242
  1722
            pos = paramBeg + 1;
jaroslav@1242
  1723
        }
jaroslav@1243
  1724
        sb = null;
jaroslav@1243
  1725
        pos = 0;
jaroslav@1243
  1726
        for (;;) {
jaroslav@1243
  1727
            int next = body.indexOf("@", pos);
jaroslav@1243
  1728
            if (next == -1) {
jaroslav@1243
  1729
                if (sb == null) {
jaroslav@1243
  1730
                    return body;
jaroslav@1243
  1731
                }
jaroslav@1243
  1732
                sb.append(body.substring(pos));
jaroslav@1243
  1733
                return sb;
jaroslav@1243
  1734
            }
jaroslav@1243
  1735
            if (sb == null) {
jaroslav@1243
  1736
                sb = new StringBuilder();
jaroslav@1243
  1737
            }
jaroslav@1243
  1738
jaroslav@1243
  1739
            sb.append(body.substring(pos, next));
jaroslav@1243
  1740
jaroslav@1243
  1741
            int sigBeg = body.indexOf('(', next);
jaroslav@1243
  1742
            int sigEnd = body.indexOf(')', sigBeg);
jaroslav@1243
  1743
            int colon4 = body.indexOf("::", next);
jaroslav@1243
  1744
            if (sigBeg == -1 || sigEnd == -1 || colon4 == -1) {
jaroslav@1243
  1745
                throw new IllegalStateException("Malformed body " + body);
jaroslav@1243
  1746
            }
jaroslav@1243
  1747
            String fqn = body.substring(next + 1, colon4);
jaroslav@1243
  1748
            String method = body.substring(colon4 + 2, sigBeg);
jaroslav@1243
  1749
            String params = body.substring(sigBeg, sigEnd + 1);
jaroslav@1243
  1750
jaroslav@1243
  1751
            int paramBeg = body.indexOf('(', sigEnd + 1);
jaroslav@1243
  1752
            
jaroslav@1243
  1753
            sb.append("vm.").append(pkgName.replace('/', '_')).append("_$JsCallbacks$(false)._VM().");
jaroslav@1409
  1754
            sb.append(mangleJsCallbacks(fqn, method, params, true));
jaroslav@1243
  1755
            sb.append("(");
jaroslav@1243
  1756
            pos = paramBeg + 1;
jaroslav@1243
  1757
        }
jaroslav@1242
  1758
    }
jaroslav@1409
  1759
jaroslav@1409
  1760
    static String mangleJsCallbacks(String fqn, String method, String params, boolean isStatic) {
jaroslav@1242
  1761
        if (params.startsWith("(")) {
jaroslav@1242
  1762
            params = params.substring(1);
jaroslav@1242
  1763
        }
jaroslav@1242
  1764
        if (params.endsWith(")")) {
jaroslav@1242
  1765
            params = params.substring(0, params.length() - 1);
jaroslav@1242
  1766
        }
jaroslav@1243
  1767
        StringBuilder sb = new StringBuilder();
jaroslav@1409
  1768
        final String fqnu = fqn.replace('.', '_');
jaroslav@1409
  1769
        final String rfqn = mangleClassName(fqnu);
jaroslav@1409
  1770
        final String rm = mangleMethodName(method);
jaroslav@1427
  1771
        final String srp;
jaroslav@1427
  1772
        {
jaroslav@1427
  1773
            StringBuilder pb = new StringBuilder();
jaroslav@1427
  1774
            int len = params.length();
jaroslav@1427
  1775
            int indx = 0;
jaroslav@1427
  1776
            while (indx < len) {
jaroslav@1427
  1777
                char ch = params.charAt(indx);
jaroslav@1427
  1778
                if (ch == '[' || ch == 'L') {
jaroslav@1427
  1779
                    pb.append("Ljava/lang/Object;");
jaroslav@1427
  1780
                    indx = params.indexOf(';', indx) + 1;
jaroslav@1427
  1781
                } else {
jaroslav@1427
  1782
                    pb.append(ch);
jaroslav@1427
  1783
                    indx++;
jaroslav@1427
  1784
                }
jaroslav@1427
  1785
            }
jaroslav@1427
  1786
            srp = mangleSig(pb.toString());
jaroslav@1427
  1787
        }
jaroslav@1409
  1788
        final String rp = mangleSig(params);
jaroslav@1409
  1789
        final String mrp = mangleMethodName(rp);
jaroslav@1248
  1790
        sb.append(rfqn).append("$").append(rm).
jaroslav@1409
  1791
            append('$').append(mrp).append("__Ljava_lang_Object_2");
jaroslav@1243
  1792
        if (!isStatic) {
jaroslav@1409
  1793
            sb.append('L').append(fqnu).append("_2");
jaroslav@1243
  1794
        }
jaroslav@1427
  1795
        sb.append(srp);
jaroslav@1243
  1796
        return sb.toString();
jaroslav@1242
  1797
    }
jaroslav@1242
  1798
jaroslav@151
  1799
    private static String className(ClassData jc) {
jaroslav@151
  1800
        //return jc.getName().getInternalName().replace('/', '_');
jaroslav@1409
  1801
        return mangleClassName(jc.getClassName());
jaroslav@94
  1802
    }
jaroslav@152
  1803
    
jaroslav@152
  1804
    private static String[] findAnnotation(
jaroslav@152
  1805
        byte[] arr, ClassData cd, final String className, 
jaroslav@152
  1806
        final String... attrNames
jaroslav@152
  1807
    ) throws IOException {
jaroslav@152
  1808
        if (arr == null) {
jaroslav@152
  1809
            return null;
jaroslav@152
  1810
        }
jaroslav@152
  1811
        final String[] values = new String[attrNames.length];
jaroslav@152
  1812
        final boolean[] found = { false };
jaroslav@152
  1813
        final String jvmType = "L" + className.replace('.', '/') + ";";
jaroslav@652
  1814
        AnnotationParser ap = new AnnotationParser(false, true) {
jaroslav@152
  1815
            @Override
jaroslav@236
  1816
            protected void visitAttr(String type, String attr, String at, String value) {
jaroslav@152
  1817
                if (type.equals(jvmType)) {
jaroslav@152
  1818
                    found[0] = true;
jaroslav@152
  1819
                    for (int i = 0; i < attrNames.length; i++) {
jaroslav@152
  1820
                        if (attrNames[i].equals(attr)) {
jaroslav@152
  1821
                            values[i] = value;
jaroslav@152
  1822
                        }
jaroslav@152
  1823
                    }
jaroslav@152
  1824
                }
jaroslav@152
  1825
            }
jaroslav@152
  1826
            
jaroslav@152
  1827
        };
jaroslav@152
  1828
        ap.parse(arr, cd);
jaroslav@152
  1829
        return found[0] ? values : null;
jaroslav@152
  1830
    }
jaroslav@173
  1831
jaroslav@173
  1832
    private CharSequence initField(FieldData v) {
jaroslav@173
  1833
        final String is = v.getInternalSig();
jaroslav@173
  1834
        if (is.length() == 1) {
jaroslav@173
  1835
            switch (is.charAt(0)) {
jaroslav@173
  1836
                case 'S':
jaroslav@173
  1837
                case 'J':
jaroslav@173
  1838
                case 'B':
jaroslav@173
  1839
                case 'Z':
jaroslav@173
  1840
                case 'C':
jaroslav@173
  1841
                case 'I': return " = 0;";
jaroslav@173
  1842
                case 'F': 
jaroslav@180
  1843
                case 'D': return " = 0.0;";
jaroslav@173
  1844
                default:
jaroslav@173
  1845
                    throw new IllegalStateException(is);
jaroslav@173
  1846
            }
jaroslav@173
  1847
        }
jaroslav@173
  1848
        return " = null;";
jaroslav@173
  1849
    }
jaroslav@235
  1850
jaroslav@655
  1851
    private void generateAnno(ClassData cd, final Appendable out, byte[] data) throws IOException {
jaroslav@652
  1852
        AnnotationParser ap = new AnnotationParser(true, false) {
jaroslav@652
  1853
            int[] cnt = new int[32];
jaroslav@652
  1854
            int depth;
jaroslav@235
  1855
            
jaroslav@235
  1856
            @Override
jaroslav@659
  1857
            protected void visitAnnotationStart(String attrType, boolean top) throws IOException {
jaroslav@659
  1858
                final String slashType = attrType.substring(1, attrType.length() - 1);
jaroslav@659
  1859
                requireReference(slashType);
jaroslav@659
  1860
                
jaroslav@652
  1861
                if (cnt[depth]++ > 0) {
jaroslav@237
  1862
                    out.append(",");
jaroslav@237
  1863
                }
jaroslav@659
  1864
                if (top) {
jaroslav@659
  1865
                    out.append('"').append(attrType).append("\" : ");
jaroslav@659
  1866
                }
jaroslav@659
  1867
                out.append("{\n");
jaroslav@652
  1868
                cnt[++depth] = 0;
jaroslav@235
  1869
            }
jaroslav@235
  1870
jaroslav@235
  1871
            @Override
jaroslav@659
  1872
            protected void visitAnnotationEnd(String type, boolean top) throws IOException {
jaroslav@235
  1873
                out.append("\n}\n");
jaroslav@652
  1874
                depth--;
jaroslav@652
  1875
            }
jaroslav@652
  1876
jaroslav@652
  1877
            @Override
jaroslav@652
  1878
            protected void visitValueStart(String attrName, char type) throws IOException {
jaroslav@652
  1879
                if (cnt[depth]++ > 0) {
jaroslav@652
  1880
                    out.append(",\n");
jaroslav@652
  1881
                }
jaroslav@652
  1882
                cnt[++depth] = 0;
jaroslav@652
  1883
                if (attrName != null) {
jaroslav@652
  1884
                    out.append(attrName).append(" : ");
jaroslav@652
  1885
                }
jaroslav@652
  1886
                if (type == '[') {
jaroslav@652
  1887
                    out.append("[");
jaroslav@652
  1888
                }
jaroslav@652
  1889
            }
jaroslav@652
  1890
jaroslav@652
  1891
            @Override
jaroslav@652
  1892
            protected void visitValueEnd(String attrName, char type) throws IOException {
jaroslav@652
  1893
                if (type == '[') {
jaroslav@652
  1894
                    out.append("]");
jaroslav@652
  1895
                }
jaroslav@652
  1896
                depth--;
jaroslav@235
  1897
            }
jaroslav@235
  1898
            
jaroslav@235
  1899
            @Override
jaroslav@236
  1900
            protected void visitAttr(String type, String attr, String attrType, String value) 
jaroslav@235
  1901
            throws IOException {
jaroslav@652
  1902
                if (attr == null && value == null) {
jaroslav@266
  1903
                    return;
jaroslav@266
  1904
                }
jaroslav@652
  1905
                out.append(value);
jaroslav@592
  1906
            }
jaroslav@655
  1907
jaroslav@655
  1908
            @Override
jaroslav@655
  1909
            protected void visitEnumAttr(String type, String attr, String attrType, String value) 
jaroslav@655
  1910
            throws IOException {
jaroslav@655
  1911
                final String slashType = attrType.substring(1, attrType.length() - 1);
jaroslav@655
  1912
                requireReference(slashType);
jaroslav@655
  1913
                
jaroslav@1409
  1914
                out.append(accessClass(mangleClassName(slashType)))
jaroslav@1360
  1915
                   .append("(false).constructor.fld_").append(value);
jaroslav@235
  1916
            }
jaroslav@235
  1917
        };
jaroslav@235
  1918
        ap.parse(data, cd);
jaroslav@235
  1919
    }
jaroslav@316
  1920
jaroslav@316
  1921
    private static String outputArg(Appendable out, String[] args, int indx) throws IOException {
jaroslav@316
  1922
        final String name = args[indx];
jaroslav@316
  1923
        if (name == null) {
jaroslav@316
  1924
            return "";
jaroslav@316
  1925
        }
jaroslav@316
  1926
        if (name.contains(",")) {
jaroslav@316
  1927
            throw new IOException("Wrong parameter with ',': " + name);
jaroslav@316
  1928
        }
jaroslav@316
  1929
        out.append(name);
jaroslav@316
  1930
        return ",";
jaroslav@316
  1931
    }
lubomir@317
  1932
jaroslav@1453
  1933
    static void emit(final Appendable out,
lubomir@283
  1934
                             final String format,
lubomir@283
  1935
                             final CharSequence... params) throws IOException {
lubomir@283
  1936
        final int length = format.length();
lubomir@283
  1937
lubomir@283
  1938
        int processed = 0;
lubomir@283
  1939
        int paramOffset = format.indexOf('@');
lubomir@283
  1940
        while ((paramOffset != -1) && (paramOffset < (length - 1))) {
lubomir@283
  1941
            final char paramChar = format.charAt(paramOffset + 1);
lubomir@283
  1942
            if ((paramChar >= '1') && (paramChar <= '9')) {
lubomir@283
  1943
                final int paramIndex = paramChar - '0' - 1;
lubomir@283
  1944
lubomir@283
  1945
                out.append(format, processed, paramOffset);
lubomir@283
  1946
                out.append(params[paramIndex]);
lubomir@283
  1947
lubomir@283
  1948
                ++paramOffset;
lubomir@283
  1949
                processed = paramOffset + 1;
lubomir@283
  1950
            }
lubomir@283
  1951
lubomir@283
  1952
            paramOffset = format.indexOf('@', paramOffset + 1);
lubomir@283
  1953
        }
lubomir@283
  1954
lubomir@283
  1955
        out.append(format, processed, length);
lubomir@283
  1956
    }
jaroslav@398
  1957
jaroslav@843
  1958
    private void generateCatch(TrapData[] traps, int current, int topMostLabel) throws IOException {
jaroslav@400
  1959
        out.append("} catch (e) {\n");
jaroslav@401
  1960
        int finallyPC = -1;
jaroslav@398
  1961
        for (TrapData e : traps) {
jaroslav@398
  1962
            if (e == null) {
jaroslav@398
  1963
                break;
jaroslav@398
  1964
            }
jaroslav@398
  1965
            if (e.catch_cpx != 0) { //not finally
jaroslav@398
  1966
                final String classInternalName = jc.getClassName(e.catch_cpx);
jaroslav@398
  1967
                addReference(classInternalName);
jaroslav@1022
  1968
                out.append("e = vm.java_lang_Throwable(false).bck2BrwsrCnvrt(e);");
jaroslav@1022
  1969
                out.append("if (e.$instOf_" + classInternalName.replace('/', '_') + ") {");
jaroslav@1022
  1970
                out.append("var stA0 = e;");
jaroslav@1022
  1971
                goTo(out, current, e.handler_pc, topMostLabel);
jaroslav@1022
  1972
                out.append("}\n");
jaroslav@398
  1973
            } else {
jaroslav@401
  1974
                finallyPC = e.handler_pc;
jaroslav@398
  1975
            }
jaroslav@398
  1976
        }
jaroslav@401
  1977
        if (finallyPC == -1) {
jaroslav@401
  1978
            out.append("throw e;");
jaroslav@401
  1979
        } else {
jaroslav@839
  1980
            out.append("var stA0 = e;");
jaroslav@843
  1981
            goTo(out, current, finallyPC, topMostLabel);
jaroslav@401
  1982
        }
jaroslav@400
  1983
        out.append("\n}");
jaroslav@398
  1984
    }
jaroslav@839
  1985
jaroslav@843
  1986
    private static void goTo(Appendable out, int current, int to, int canBack) throws IOException {
jaroslav@839
  1987
        if (to < current) {
jaroslav@843
  1988
            if (canBack < to) {
jaroslav@843
  1989
                out.append("{ gt = 0; continue X_" + to + "; }");
jaroslav@843
  1990
            } else {
jaroslav@843
  1991
                out.append("{ gt = " + to + "; continue X_0; }");
jaroslav@843
  1992
            }
jaroslav@839
  1993
        } else {
jaroslav@839
  1994
            out.append("{ gt = " + to + "; break IF; }");
jaroslav@839
  1995
        }
jaroslav@839
  1996
    }
jaroslav@839
  1997
jaroslav@843
  1998
    private static void emitIf(
jaroslav@843
  1999
        Appendable out, String pattern, Variable param, 
jaroslav@843
  2000
        int current, int to, int canBack
jaroslav@843
  2001
    ) throws IOException {
jaroslav@839
  2002
        emit(out, pattern, param);
jaroslav@843
  2003
        goTo(out, current, to, canBack);
jaroslav@839
  2004
    }
jaroslav@842
  2005
jaroslav@842
  2006
    private void generateNewArray(int atype, final StackMapper smapper) throws IOException, IllegalStateException {
jaroslav@842
  2007
        String jvmType;
jaroslav@842
  2008
        switch (atype) {
jaroslav@842
  2009
            case 4: jvmType = "[Z"; break;
jaroslav@842
  2010
            case 5: jvmType = "[C"; break;
jaroslav@842
  2011
            case 6: jvmType = "[F"; break;
jaroslav@842
  2012
            case 7: jvmType = "[D"; break;
jaroslav@842
  2013
            case 8: jvmType = "[B"; break;
jaroslav@842
  2014
            case 9: jvmType = "[S"; break;
jaroslav@842
  2015
            case 10: jvmType = "[I"; break;
jaroslav@842
  2016
            case 11: jvmType = "[J"; break;
jaroslav@842
  2017
            default: throw new IllegalStateException("Array type: " + atype);
jaroslav@842
  2018
        }
jaroslav@842
  2019
        emit(out, "var @2 = Array.prototype.newArray__Ljava_lang_Object_2ZLjava_lang_String_2I(true, '@3', @1);",
jaroslav@842
  2020
             smapper.popI(), smapper.pushA(), jvmType);
jaroslav@842
  2021
    }
jaroslav@842
  2022
jaroslav@842
  2023
    private void generateANewArray(int type, final StackMapper smapper) throws IOException {
jaroslav@842
  2024
        String typeName = jc.getClassName(type);
jaroslav@842
  2025
        if (typeName.startsWith("[")) {
jaroslav@842
  2026
            typeName = "[" + typeName;
jaroslav@842
  2027
        } else {
jaroslav@842
  2028
            typeName = "[L" + typeName + ";";
jaroslav@842
  2029
        }
jaroslav@842
  2030
        emit(out, "var @2 = Array.prototype.newArray__Ljava_lang_Object_2ZLjava_lang_String_2I(false, '@3', @1);",
jaroslav@842
  2031
             smapper.popI(), smapper.pushA(), typeName);
jaroslav@842
  2032
    }
jaroslav@842
  2033
jaroslav@842
  2034
    private int generateMultiANewArray(int type, final byte[] byteCodes, int i, final StackMapper smapper) throws IOException {
jaroslav@842
  2035
        String typeName = jc.getClassName(type);
jaroslav@842
  2036
        int dim = readUByte(byteCodes, ++i);
jaroslav@842
  2037
        StringBuilder dims = new StringBuilder();
jaroslav@842
  2038
        dims.append('[');
jaroslav@842
  2039
        for (int d = 0; d < dim; d++) {
jaroslav@842
  2040
            if (d != 0) {
jaroslav@842
  2041
                dims.insert(1, ",");
jaroslav@842
  2042
            }
jaroslav@842
  2043
            dims.insert(1, smapper.popI());
jaroslav@842
  2044
        }
jaroslav@842
  2045
        dims.append(']');
jaroslav@842
  2046
        emit(out, "var @2 = Array.prototype.multiNewArray__Ljava_lang_Object_2Ljava_lang_String_2_3II('@3', @1, 0);",
jaroslav@842
  2047
             dims.toString(), smapper.pushA(), typeName);
jaroslav@842
  2048
        return i;
jaroslav@842
  2049
    }
jaroslav@842
  2050
jaroslav@843
  2051
    private int generateTableSwitch(int i, final byte[] byteCodes, final StackMapper smapper, int topMostLabel) throws IOException {
jaroslav@842
  2052
        int table = i / 4 * 4 + 4;
jaroslav@842
  2053
        int dflt = i + readInt4(byteCodes, table);
jaroslav@842
  2054
        table += 4;
jaroslav@842
  2055
        int low = readInt4(byteCodes, table);
jaroslav@842
  2056
        table += 4;
jaroslav@842
  2057
        int high = readInt4(byteCodes, table);
jaroslav@842
  2058
        table += 4;
jaroslav@842
  2059
        out.append("switch (").append(smapper.popI()).append(") {\n");
jaroslav@842
  2060
        while (low <= high) {
jaroslav@842
  2061
            int offset = i + readInt4(byteCodes, table);
jaroslav@842
  2062
            table += 4;
jaroslav@843
  2063
            out.append("  case " + low).append(":"); goTo(out, i, offset, topMostLabel); out.append('\n');
jaroslav@842
  2064
            low++;
jaroslav@842
  2065
        }
jaroslav@842
  2066
        out.append("  default: ");
jaroslav@843
  2067
        goTo(out, i, dflt, topMostLabel);
jaroslav@842
  2068
        out.append("\n}");
jaroslav@842
  2069
        i = table - 1;
jaroslav@842
  2070
        return i;
jaroslav@842
  2071
    }
jaroslav@842
  2072
jaroslav@843
  2073
    private int generateLookupSwitch(int i, final byte[] byteCodes, final StackMapper smapper, int topMostLabel) throws IOException {
jaroslav@842
  2074
        int table = i / 4 * 4 + 4;
jaroslav@842
  2075
        int dflt = i + readInt4(byteCodes, table);
jaroslav@842
  2076
        table += 4;
jaroslav@842
  2077
        int n = readInt4(byteCodes, table);
jaroslav@842
  2078
        table += 4;
jaroslav@842
  2079
        out.append("switch (").append(smapper.popI()).append(") {\n");
jaroslav@842
  2080
        while (n-- > 0) {
jaroslav@842
  2081
            int cnstnt = readInt4(byteCodes, table);
jaroslav@842
  2082
            table += 4;
jaroslav@842
  2083
            int offset = i + readInt4(byteCodes, table);
jaroslav@842
  2084
            table += 4;
jaroslav@843
  2085
            out.append("  case " + cnstnt).append(": "); goTo(out, i, offset, topMostLabel); out.append('\n');
jaroslav@842
  2086
        }
jaroslav@842
  2087
        out.append("  default: ");
jaroslav@843
  2088
        goTo(out, i, dflt, topMostLabel);
jaroslav@842
  2089
        out.append("\n}");
jaroslav@842
  2090
        i = table - 1;
jaroslav@842
  2091
        return i;
jaroslav@842
  2092
    }
jaroslav@842
  2093
jaroslav@842
  2094
    private void generateInstanceOf(int indx, final StackMapper smapper) throws IOException {
jaroslav@842
  2095
        final String type = jc.getClassName(indx);
jaroslav@842
  2096
        if (!type.startsWith("[")) {
jaroslav@842
  2097
            emit(out, "var @2 = @1 != null && @1.$instOf_@3 ? 1 : 0;",
jaroslav@842
  2098
                 smapper.popA(), smapper.pushI(),
jaroslav@842
  2099
                 type.replace('/', '_'));
jaroslav@842
  2100
        } else {
jaroslav@842
  2101
            emit(out, "var @2 = vm.java_lang_Class(false).forName__Ljava_lang_Class_2Ljava_lang_String_2('@3').isInstance__ZLjava_lang_Object_2(@1);",
jaroslav@842
  2102
                smapper.popA(), smapper.pushI(),
jaroslav@842
  2103
                type
jaroslav@842
  2104
            );
jaroslav@842
  2105
        }
jaroslav@842
  2106
    }
jaroslav@842
  2107
jaroslav@842
  2108
    private void generateCheckcast(int indx, final StackMapper smapper) throws IOException {
jaroslav@842
  2109
        final String type = jc.getClassName(indx);
jaroslav@842
  2110
        if (!type.startsWith("[")) {
jaroslav@842
  2111
            emit(out,
jaroslav@962
  2112
                 "if (@1 !== null && !@1.$instOf_@2) throw vm.java_lang_ClassCastException(true);",
jaroslav@842
  2113
                 smapper.getA(0), type.replace('/', '_'));
jaroslav@842
  2114
        } else {
jaroslav@842
  2115
            emit(out, "vm.java_lang_Class(false).forName__Ljava_lang_Class_2Ljava_lang_String_2('@2').cast__Ljava_lang_Object_2Ljava_lang_Object_2(@1);",
jaroslav@842
  2116
                 smapper.getA(0), type
jaroslav@842
  2117
            );
jaroslav@842
  2118
        }
jaroslav@842
  2119
    }
jaroslav@842
  2120
jaroslav@842
  2121
    private void generateByteCodeComment(int prev, int i, final byte[] byteCodes) throws IOException {
jaroslav@842
  2122
        for (int j = prev; j <= i; j++) {
jaroslav@842
  2123
            out.append(" ");
jaroslav@842
  2124
            final int cc = readUByte(byteCodes, j);
jaroslav@842
  2125
            out.append(Integer.toString(cc));
jaroslav@842
  2126
        }
jaroslav@842
  2127
    }
jaroslav@0
  2128
}