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