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