Jump instructions need to use signed and constant pool referencing instructions unsigned short. Also getting ready for reading signed short composed from (improperly) two unsigned bytes.
2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.vm4brwsr;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import static org.apidesign.vm4brwsr.ByteCodeParser.*;
24 /** Translator of the code inside class files to JavaScript.
26 * @author Jaroslav Tulach <jtulach@netbeans.org>
28 abstract class ByteCodeToJavaScript {
32 protected ByteCodeToJavaScript(Appendable out) {
36 /* Collects additional required resources.
38 * @param internalClassName classes that were referenced and should be loaded in order the
39 * generated JavaScript code works properly. The names are in internal
40 * JVM form so String is <code>java/lang/String</code>.
42 protected abstract boolean requireReference(String internalClassName);
45 * @param resourcePath name of resources to read
47 protected abstract void requireScript(String resourcePath) throws IOException;
49 /** Allows subclasses to redefine what field a function representing a
50 * class gets assigned. By default it returns the suggested name followed
51 * by <code>" = "</code>;
53 * @param className suggested name of the class
55 /* protected */ String assignClass(String className) {
56 return className + " = ";
58 /* protected */ String accessClass(String classOperation) {
59 return classOperation;
62 /** Prints out a debug message.
64 * @param msg the message
65 * @return true if the message has been printed
68 boolean debug(String msg) throws IOException {
74 * Converts a given class file to a JavaScript version.
76 * @param classFile input stream with code of the .class file
77 * @return the initialization code for this class, if any. Otherwise <code>null</code>
79 * @throws IOException if something goes wrong during read or write or translating
82 public String compile(InputStream classFile) throws IOException {
83 this.jc = new ClassData(classFile);
84 if (jc.getMajor_version() < 50) {
85 throw new IOException("Can't compile " + jc.getClassName() + ". Class file version " + jc.getMajor_version() + "."
86 + jc.getMinor_version() + " - recompile with -target 1.6 (at least)."
89 byte[] arrData = jc.findAnnotationData(true);
90 String[] arr = findAnnotation(arrData, jc,
91 "org.apidesign.bck2brwsr.core.ExtraJavaScript",
92 "resource", "processByteCode"
95 requireScript(arr[0]);
96 if ("0".equals(arr[1])) {
100 String[] proto = findAnnotation(arrData, jc,
101 "org.apidesign.bck2brwsr.core.JavaScriptPrototype",
102 "container", "prototype"
104 StringArray toInitilize = new StringArray();
105 final String className = className(jc);
106 out.append("\n\n").append(assignClass(className));
107 out.append("function CLS() {");
108 out.append("\n if (!CLS.$class) {");
110 String sc = jc.getSuperClassName(); // with _
111 out.append("\n var pp = ").
112 append(accessClass(sc.replace('/', '_'))).append("(true);");
113 out.append("\n var p = CLS.prototype = pp;");
114 out.append("\n var c = p;");
115 out.append("\n var sprcls = pp.constructor.$class;");
117 out.append("\n var p = CLS.prototype = ").append(proto[1]).append(";");
118 if (proto[0] == null) {
121 out.append("\n var c = ").append(proto[0]).append(";");
122 out.append("\n var sprcls = null;");
124 for (FieldData v : jc.getFields()) {
126 out.append("\n CLS.").append(v.getName()).append(initField(v));
127 out.append("\n c._").append(v.getName()).append(" = function (v) {")
128 .append(" if (arguments.length == 1) CLS.").append(v.getName())
129 .append(" = v; return CLS.").
130 append(v.getName()).append("; };");
132 out.append("\n c._").append(v.getName()).append(" = function (v) {")
133 .append(" if (arguments.length == 1) this.fld_").
134 append(className).append('_').append(v.getName())
135 .append(" = v; return this.fld_").
136 append(className).append('_').append(v.getName())
140 for (MethodData m : jc.getMethods()) {
141 byte[] onlyArr = m.findAnnotationData(true);
142 String[] only = findAnnotation(onlyArr, jc,
143 "org.apidesign.bck2brwsr.core.JavaScriptOnly",
147 if (only[0] != null && only[1] != null) {
148 out.append("\n p.").append(only[0]).append(" = ")
149 .append(only[1]).append(";");
157 mn = generateStaticMethod(prefix, m, toInitilize);
159 if (m.isConstructor()) {
161 mn = generateInstanceMethod(prefix, m);
164 mn = generateInstanceMethod(prefix, m);
167 byte[] runAnno = m.findAnnotationData(false);
168 if (runAnno != null) {
169 out.append(prefix).append(mn).append(".anno = {");
170 generateAnno(jc, out, runAnno);
173 out.append(prefix).append(mn).append(".access = " + m.getAccess()).append(";");
174 out.append(prefix).append(mn).append(".cls = CLS;");
176 out.append("\n c.constructor = CLS;");
177 out.append("\n c.$instOf_").append(className).append(" = true;");
178 for (String superInterface : jc.getSuperInterfaces()) {
179 out.append("\n c.$instOf_").append(superInterface.replace('/', '_')).append(" = true;");
181 out.append("\n CLS.$class = 'temp';");
182 out.append("\n CLS.$class = ");
183 out.append(accessClass("java_lang_Class(true);"));
184 out.append("\n CLS.$class.jvmName = '").append(jc.getClassName()).append("';");
185 out.append("\n CLS.$class.superclass = sprcls;");
186 out.append("\n CLS.$class.access = ").append(jc.getAccessFlags()+";");
187 out.append("\n CLS.$class.cnstr = CLS;");
188 byte[] classAnno = jc.findAnnotationData(false);
189 if (classAnno != null) {
190 out.append("\n CLS.$class.anno = {");
191 generateAnno(jc, out, classAnno);
194 for (String init : toInitilize.toArray()) {
195 out.append("\n ").append(init).append("();");
198 out.append("\n if (arguments.length === 0) {");
199 out.append("\n if (!(this instanceof CLS)) {");
200 out.append("\n return new CLS();");
202 for (FieldData v : jc.getFields()) {
203 byte[] onlyArr = v.findAnnotationData(true);
204 String[] only = findAnnotation(onlyArr, jc,
205 "org.apidesign.bck2brwsr.core.JavaScriptOnly",
209 if (only[0] != null && only[1] != null) {
210 out.append("\n p.").append(only[0]).append(" = ")
211 .append(only[1]).append(";");
216 out.append("\n this.fld_").
217 append(className).append('_').
218 append(v.getName()).append(initField(v));
221 out.append("\n return this;");
223 out.append("\n return arguments[0] ? new CLS() : CLS.prototype;");
225 // StringBuilder sb = new StringBuilder();
226 // for (String init : toInitilize.toArray()) {
227 // sb.append("\n").append(init).append("();");
231 private String generateStaticMethod(String prefix, MethodData m, StringArray toInitilize) throws IOException {
232 String jsb = javaScriptBody(prefix, m, true);
236 final String mn = findMethodName(m, new StringBuilder());
237 if (mn.equals("class__V")) {
238 toInitilize.add(accessClass(className(jc)) + "(false)." + mn);
240 generateMethod(prefix, mn, m);
244 private String generateInstanceMethod(String prefix, MethodData m) throws IOException {
245 String jsb = javaScriptBody(prefix, m, false);
249 final String mn = findMethodName(m, new StringBuilder());
250 generateMethod(prefix, mn, m);
254 private void generateMethod(String prefix, String name, MethodData m)
256 final StackMapIterator stackMapIterator = m.createStackMapIterator();
257 TrapDataIterator trap = m.getTrapDataIterator();
258 final LocalsMapper lmapper =
259 new LocalsMapper(stackMapIterator.getArguments());
261 out.append(prefix).append(name).append(" = function(");
262 lmapper.outputArguments(out, m.isStatic());
263 out.append(") {").append("\n");
265 final byte[] byteCodes = m.getCode();
266 if (byteCodes == null) {
267 out.append(" throw 'no code found for ")
268 .append(jc.getClassName()).append('.')
269 .append(m.getName()).append("';\n");
274 final StackMapper smapper = new StackMapper();
277 out.append(" var ").append(" lcA0 = this;\n");
280 int lastStackFrame = -1;
281 TrapData[] previousTrap = null;
282 boolean wide = false;
284 out.append("\n var gt = 0;\n");
286 int topMostLabel = 0;
287 for (int i = 0; i < byteCodes.length; i++) {
289 stackMapIterator.advanceTo(i);
290 boolean changeInCatch = trap.advanceTo(i);
291 if (changeInCatch || lastStackFrame != stackMapIterator.getFrameIndex()) {
292 if (previousTrap != null) {
293 generateCatch(previousTrap, i, topMostLabel);
297 if (lastStackFrame != stackMapIterator.getFrameIndex()) {
301 if (openBraces > 64) {
302 for (int c = 0; c < 64; c++) {
303 out.append("break;}\n");
309 lastStackFrame = stackMapIterator.getFrameIndex();
310 lmapper.syncWithFrameLocals(stackMapIterator.getFrameLocals());
311 smapper.syncWithFrameStack(stackMapIterator.getFrameStack());
312 out.append(" X_" + i).append(": for (;;) { IF: if (gt <= " + i + ") {\n");
314 changeInCatch = true;
316 debug(" /* " + i + " */ ");
318 if (changeInCatch && trap.useTry()) {
320 previousTrap = trap.current();
322 final int c = readUByte(byteCodes, i);
325 emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(0));
328 emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(0));
331 emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(0));
334 emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(0));
337 emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(0));
340 emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(1));
343 emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(1));
346 emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(1));
349 emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(1));
352 emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(1));
355 emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(2));
358 emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(2));
361 emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(2));
364 emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(2));
367 emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(2));
370 emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(3));
373 emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(3));
376 emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(3));
379 emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(3));
382 emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(3));
386 final int indx = wide ? readUShort(byteCodes, i++)
387 : readUByte(byteCodes, i);
389 emit(out, "var @1 = @2;",
390 smapper.pushI(), lmapper.getI(indx));
395 final int indx = wide ? readUShort(byteCodes, i++)
396 : readUByte(byteCodes, i);
398 emit(out, "var @1 = @2;",
399 smapper.pushL(), lmapper.getL(indx));
404 final int indx = wide ? readUShort(byteCodes, i++)
405 : readUByte(byteCodes, i);
407 emit(out, "var @1 = @2;",
408 smapper.pushF(), lmapper.getF(indx));
413 final int indx = wide ? readUShort(byteCodes, i++)
414 : readUByte(byteCodes, i);
416 emit(out, "var @1 = @2;",
417 smapper.pushD(), lmapper.getD(indx));
422 final int indx = wide ? readUShort(byteCodes, i++)
423 : readUByte(byteCodes, i);
425 emit(out, "var @1 = @2;",
426 smapper.pushA(), lmapper.getA(indx));
431 final int indx = wide ? readUShort(byteCodes, i++)
432 : readUByte(byteCodes, i);
434 emit(out, "var @1 = @2;",
435 lmapper.setI(indx), smapper.popI());
440 final int indx = wide ? readUShort(byteCodes, i++)
441 : readUByte(byteCodes, i);
443 emit(out, "var @1 = @2;",
444 lmapper.setL(indx), smapper.popL());
449 final int indx = wide ? readUShort(byteCodes, i++)
450 : readUByte(byteCodes, i);
452 emit(out, "var @1 = @2;",
453 lmapper.setF(indx), smapper.popF());
458 final int indx = wide ? readUShort(byteCodes, i++)
459 : readUByte(byteCodes, i);
461 emit(out, "var @1 = @2;",
462 lmapper.setD(indx), smapper.popD());
467 final int indx = wide ? readUShort(byteCodes, i++)
468 : readUByte(byteCodes, i);
470 emit(out, "var @1 = @2;",
471 lmapper.setA(indx), smapper.popA());
475 emit(out, "var @1 = @2;", lmapper.setA(0), smapper.popA());
478 emit(out, "var @1 = @2;", lmapper.setI(0), smapper.popI());
481 emit(out, "var @1 = @2;", lmapper.setL(0), smapper.popL());
484 emit(out, "var @1 = @2;", lmapper.setF(0), smapper.popF());
487 emit(out, "var @1 = @2;", lmapper.setD(0), smapper.popD());
490 emit(out, "var @1 = @2;", lmapper.setA(1), smapper.popA());
493 emit(out, "var @1 = @2;", lmapper.setI(1), smapper.popI());
496 emit(out, "var @1 = @2;", lmapper.setL(1), smapper.popL());
499 emit(out, "var @1 = @2;", lmapper.setF(1), smapper.popF());
502 emit(out, "var @1 = @2;", lmapper.setD(1), smapper.popD());
505 emit(out, "var @1 = @2;", lmapper.setA(2), smapper.popA());
508 emit(out, "var @1 = @2;", lmapper.setI(2), smapper.popI());
511 emit(out, "var @1 = @2;", lmapper.setL(2), smapper.popL());
514 emit(out, "var @1 = @2;", lmapper.setF(2), smapper.popF());
517 emit(out, "var @1 = @2;", lmapper.setD(2), smapper.popD());
520 emit(out, "var @1 = @2;", lmapper.setA(3), smapper.popA());
523 emit(out, "var @1 = @2;", lmapper.setI(3), smapper.popI());
526 emit(out, "var @1 = @2;", lmapper.setL(3), smapper.popL());
529 emit(out, "var @1 = @2;", lmapper.setF(3), smapper.popF());
532 emit(out, "var @1 = @2;", lmapper.setD(3), smapper.popD());
535 emit(out, "@1 = @1.add32(@2);", smapper.getI(1), smapper.popI());
538 emit(out, "@1 = @1.add64(@2);", smapper.getL(1), smapper.popL());
541 emit(out, "@1 += @2;", smapper.getF(1), smapper.popF());
544 emit(out, "@1 += @2;", smapper.getD(1), smapper.popD());
547 emit(out, "@1 = @1.sub32(@2);", smapper.getI(1), smapper.popI());
550 emit(out, "@1 = @1.sub64(@2);", smapper.getL(1), smapper.popL());
553 emit(out, "@1 -= @2;", smapper.getF(1), smapper.popF());
556 emit(out, "@1 -= @2;", smapper.getD(1), smapper.popD());
559 emit(out, "@1 = @1.mul32(@2);", smapper.getI(1), smapper.popI());
562 emit(out, "@1 = @1.mul64(@2);", smapper.getL(1), smapper.popL());
565 emit(out, "@1 *= @2;", smapper.getF(1), smapper.popF());
568 emit(out, "@1 *= @2;", smapper.getD(1), smapper.popD());
571 emit(out, "@1 = @1.div32(@2);",
572 smapper.getI(1), smapper.popI());
575 emit(out, "@1 = @1.div64(@2);",
576 smapper.getL(1), smapper.popL());
579 emit(out, "@1 /= @2;", smapper.getF(1), smapper.popF());
582 emit(out, "@1 /= @2;", smapper.getD(1), smapper.popD());
585 emit(out, "@1 = @1.mod32(@2);",
586 smapper.getI(1), smapper.popI());
589 emit(out, "@1 = @1.mod64(@2);",
590 smapper.getL(1), smapper.popL());
593 emit(out, "@1 %= @2;", smapper.getF(1), smapper.popF());
596 emit(out, "@1 %= @2;", smapper.getD(1), smapper.popD());
599 emit(out, "@1 &= @2;", smapper.getI(1), smapper.popI());
602 emit(out, "@1 = @1.and64(@2);", smapper.getL(1), smapper.popL());
605 emit(out, "@1 |= @2;", smapper.getI(1), smapper.popI());
608 emit(out, "@1 = @1.or64(@2);", smapper.getL(1), smapper.popL());
611 emit(out, "@1 ^= @2;", smapper.getI(1), smapper.popI());
614 emit(out, "@1 = @1.xor64(@2);", smapper.getL(1), smapper.popL());
617 emit(out, "@1 = @1.neg32();", smapper.getI(0));
620 emit(out, "@1 = @1.neg64();", smapper.getL(0));
623 emit(out, "@1 = -@1;", smapper.getF(0));
626 emit(out, "@1 = -@1;", smapper.getD(0));
629 emit(out, "@1 <<= @2;", smapper.getI(1), smapper.popI());
632 emit(out, "@1 = @1.shl64(@2);", smapper.getL(1), smapper.popI());
635 emit(out, "@1 >>= @2;", smapper.getI(1), smapper.popI());
638 emit(out, "@1 = @1.shr64(@2);", smapper.getL(1), smapper.popI());
641 emit(out, "@1 >>>= @2;", smapper.getI(1), smapper.popI());
644 emit(out, "@1 = @1.ushr64(@2);", smapper.getL(1), smapper.popI());
648 final int varIndx = wide ? readUShort(byteCodes, i++)
649 : readUByte(byteCodes, i);
651 final int incrBy = wide ? readShort(byteCodes, i++)
655 emit(out, "@1++;", lmapper.getI(varIndx));
657 emit(out, "@1 += @2;",
658 lmapper.getI(varIndx),
659 Integer.toString(incrBy));
664 emit(out, "return;");
667 emit(out, "return @1;", smapper.popI());
670 emit(out, "return @1;", smapper.popL());
673 emit(out, "return @1;", smapper.popF());
676 emit(out, "return @1;", smapper.popD());
679 emit(out, "return @1;", smapper.popA());
682 emit(out, "var @2 = @1;", smapper.popI(), smapper.pushL());
685 emit(out, "var @2 = @1;", smapper.popI(), smapper.pushF());
688 emit(out, "var @2 = @1;", smapper.popI(), smapper.pushD());
691 emit(out, "var @2 = @1.toInt32();", smapper.popL(), smapper.pushI());
695 emit(out, "var @2 = @1.toFP();", smapper.popL(), smapper.pushF());
698 emit(out, "var @2 = @1.toFP();", smapper.popL(), smapper.pushD());
701 emit(out, "var @2 = @1;", smapper.popF(), smapper.pushD());
704 emit(out, "var @2 = @1;", smapper.popD(), smapper.pushF());
707 emit(out, "var @2 = @1.toInt32();",
708 smapper.popF(), smapper.pushI());
711 emit(out, "var @2 = @1.toLong();",
712 smapper.popF(), smapper.pushL());
715 emit(out, "var @2 = @1.toInt32();",
716 smapper.popD(), smapper.pushI());
719 emit(out, "var @2 = @1.toLong();",
720 smapper.popD(), smapper.pushL());
723 emit(out, "var @1 = @1.toInt8();", smapper.getI(0));
726 out.append("{ /* number conversion */ }");
729 emit(out, "var @1 = @1.toInt16();", smapper.getI(0));
731 case opc_aconst_null:
732 emit(out, "var @1 = null;", smapper.pushA());
735 emit(out, "var @1 = -1;", smapper.pushI());
738 emit(out, "var @1 = 0;", smapper.pushI());
741 emit(out, "var @1 = 0;", smapper.pushD());
744 emit(out, "var @1 = 0;", smapper.pushL());
747 emit(out, "var @1 = 0;", smapper.pushF());
750 emit(out, "var @1 = 1;", smapper.pushI());
753 emit(out, "var @1 = 1;", smapper.pushL());
756 emit(out, "var @1 = 1;", smapper.pushF());
759 emit(out, "var @1 = 1;", smapper.pushD());
762 emit(out, "var @1 = 2;", smapper.pushI());
765 emit(out, "var @1 = 2;", smapper.pushF());
768 emit(out, "var @1 = 3;", smapper.pushI());
771 emit(out, "var @1 = 4;", smapper.pushI());
774 emit(out, "var @1 = 5;", smapper.pushI());
777 int indx = readUByte(byteCodes, ++i);
778 String v = encodeConstant(indx);
779 int type = VarType.fromConstantType(jc.getTag(indx));
780 emit(out, "var @1 = @2;", smapper.pushT(type), v);
785 int indx = readUShortArg(byteCodes, i);
787 String v = encodeConstant(indx);
788 int type = VarType.fromConstantType(jc.getTag(indx));
789 if (type == VarType.LONG) {
790 final Long lv = new Long(v);
791 final int low = (int)(lv.longValue() & 0xFFFFFFFF);
792 final int hi = (int)(lv.longValue() >> 32);
793 emit(out, "var @1 = 0x@3.next32(0x@2);", smapper.pushL(),
794 Integer.toHexString(low), Integer.toHexString(hi));
796 emit(out, "var @1 = @2;", smapper.pushT(type), v);
801 emit(out, "var @3 = @2.compare64(@1);",
802 smapper.popL(), smapper.popL(), smapper.pushI());
806 emit(out, "var @3 = (@2 == @1) ? 0 : ((@2 < @1) ? -1 : 1);",
807 smapper.popF(), smapper.popF(), smapper.pushI());
811 emit(out, "var @3 = (@2 == @1) ? 0 : ((@2 < @1) ? -1 : 1);",
812 smapper.popD(), smapper.popD(), smapper.pushI());
815 i = generateIf(byteCodes, i, smapper.popA(), smapper.popA(),
816 "===", topMostLabel);
819 i = generateIf(byteCodes, i, smapper.popA(), smapper.popA(),
820 "!==", topMostLabel);
823 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
827 int indx = i + readShortArg(byteCodes, i);
828 emitIf(out, "if (@1 == 0) ",
829 smapper.popI(), i, indx, topMostLabel);
834 int indx = i + readShortArg(byteCodes, i);
835 emitIf(out, "if (@1 != 0) ",
836 smapper.popI(), i, indx, topMostLabel);
841 int indx = i + readShortArg(byteCodes, i);
842 emitIf(out, "if (@1 < 0) ",
843 smapper.popI(), i, indx, topMostLabel);
848 int indx = i + readShortArg(byteCodes, i);
849 emitIf(out, "if (@1 <= 0) ",
850 smapper.popI(), i, indx, topMostLabel);
855 int indx = i + readShortArg(byteCodes, i);
856 emitIf(out, "if (@1 > 0) ",
857 smapper.popI(), i, indx, topMostLabel);
862 int indx = i + readShortArg(byteCodes, i);
863 emitIf(out, "if (@1 >= 0) ",
864 smapper.popI(), i, indx, topMostLabel);
868 case opc_ifnonnull: {
869 int indx = i + readShortArg(byteCodes, i);
870 emitIf(out, "if (@1 !== null) ",
871 smapper.popA(), i, indx, topMostLabel);
876 int indx = i + readShortArg(byteCodes, i);
877 emitIf(out, "if (@1 === null) ",
878 smapper.popA(), i, indx, topMostLabel);
883 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
887 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
891 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
895 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
899 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
903 int indx = i + readShortArg(byteCodes, i);
904 goTo(out, i, indx, topMostLabel);
908 case opc_lookupswitch: {
909 i = generateLookupSwitch(i, byteCodes, smapper, topMostLabel);
912 case opc_tableswitch: {
913 i = generateTableSwitch(i, byteCodes, smapper, topMostLabel);
916 case opc_invokeinterface: {
917 i = invokeVirtualMethod(byteCodes, i, smapper) + 2;
920 case opc_invokevirtual:
921 i = invokeVirtualMethod(byteCodes, i, smapper);
923 case opc_invokespecial:
924 i = invokeStaticMethod(byteCodes, i, smapper, false);
926 case opc_invokestatic:
927 i = invokeStaticMethod(byteCodes, i, smapper, true);
930 int indx = readUShortArg(byteCodes, i);
931 String ci = jc.getClassName(indx);
932 emit(out, "var @1 = new @2;",
933 smapper.pushA(), accessClass(ci.replace('/', '_')));
939 int atype = readUByte(byteCodes, ++i);
940 generateNewArray(atype, smapper);
942 case opc_anewarray: {
943 int type = readUShortArg(byteCodes, i);
945 generateANewArray(type, smapper);
948 case opc_multianewarray: {
949 int type = readUShortArg(byteCodes, i);
951 i = generateMultiANewArray(type, byteCodes, i, smapper);
954 case opc_arraylength:
955 emit(out, "var @2 = @1.length;",
956 smapper.popA(), smapper.pushI());
959 emit(out, "@3.at(@2, @1);",
960 smapper.popL(), smapper.popI(), smapper.popA());
963 emit(out, "@3.at(@2, @1);",
964 smapper.popF(), smapper.popI(), smapper.popA());
967 emit(out, "@3.at(@2, @1);",
968 smapper.popD(), smapper.popI(), smapper.popA());
971 emit(out, "@3.at(@2, @1);",
972 smapper.popA(), smapper.popI(), smapper.popA());
978 emit(out, "@3.at(@2, @1);",
979 smapper.popI(), smapper.popI(), smapper.popA());
982 emit(out, "var @3 = @2.at(@1);",
983 smapper.popI(), smapper.popA(), smapper.pushL());
986 emit(out, "var @3 = @2.at(@1);",
987 smapper.popI(), smapper.popA(), smapper.pushF());
990 emit(out, "var @3 = @2.at(@1);",
991 smapper.popI(), smapper.popA(), smapper.pushD());
994 emit(out, "var @3 = @2.at(@1);",
995 smapper.popI(), smapper.popA(), smapper.pushA());
1001 emit(out, "var @3 = @2.at(@1);",
1002 smapper.popI(), smapper.popA(), smapper.pushI());
1010 final Variable v = smapper.get(0);
1011 emit(out, "var @1 = @2;", smapper.pushT(v.getType()), v);
1015 final Variable vi1 = smapper.get(0);
1017 if (vi1.isCategory2()) {
1018 emit(out, "var @1 = @2;",
1019 smapper.pushT(vi1.getType()), vi1);
1021 final Variable vi2 = smapper.get(1);
1022 emit(out, "var @1 = @2, @3 = @4;",
1023 smapper.pushT(vi2.getType()), vi2,
1024 smapper.pushT(vi1.getType()), vi1);
1029 final Variable vi1 = smapper.pop();
1030 final Variable vi2 = smapper.pop();
1031 final Variable vo3 = smapper.pushT(vi1.getType());
1032 final Variable vo2 = smapper.pushT(vi2.getType());
1033 final Variable vo1 = smapper.pushT(vi1.getType());
1035 emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
1036 vo1, vi1, vo2, vi2, vo3, vo1);
1040 final Variable vi1 = smapper.pop();
1041 final Variable vi2 = smapper.pop();
1043 if (vi1.isCategory2()) {
1044 final Variable vo3 = smapper.pushT(vi1.getType());
1045 final Variable vo2 = smapper.pushT(vi2.getType());
1046 final Variable vo1 = smapper.pushT(vi1.getType());
1048 emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
1049 vo1, vi1, vo2, vi2, vo3, vo1);
1051 final Variable vi3 = smapper.pop();
1052 final Variable vo5 = smapper.pushT(vi2.getType());
1053 final Variable vo4 = smapper.pushT(vi1.getType());
1054 final Variable vo3 = smapper.pushT(vi3.getType());
1055 final Variable vo2 = smapper.pushT(vi2.getType());
1056 final Variable vo1 = smapper.pushT(vi1.getType());
1058 emit(out, "var @1 = @2, @3 = @4, @5 = @6,",
1059 vo1, vi1, vo2, vi2, vo3, vi3);
1060 emit(out, " @1 = @2, @3 = @4;",
1061 vo4, vo1, vo5, vo2);
1066 final Variable vi1 = smapper.pop();
1067 final Variable vi2 = smapper.pop();
1069 if (vi2.isCategory2()) {
1070 final Variable vo3 = smapper.pushT(vi1.getType());
1071 final Variable vo2 = smapper.pushT(vi2.getType());
1072 final Variable vo1 = smapper.pushT(vi1.getType());
1074 emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
1075 vo1, vi1, vo2, vi2, vo3, vo1);
1077 final Variable vi3 = smapper.pop();
1078 final Variable vo4 = smapper.pushT(vi1.getType());
1079 final Variable vo3 = smapper.pushT(vi3.getType());
1080 final Variable vo2 = smapper.pushT(vi2.getType());
1081 final Variable vo1 = smapper.pushT(vi1.getType());
1083 emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8;",
1084 vo1, vi1, vo2, vi2, vo3, vi3, vo4, vo1);
1089 final Variable vi1 = smapper.pop();
1090 final Variable vi2 = smapper.pop();
1092 if (vi1.isCategory2()) {
1093 if (vi2.isCategory2()) {
1094 final Variable vo3 = smapper.pushT(vi1.getType());
1095 final Variable vo2 = smapper.pushT(vi2.getType());
1096 final Variable vo1 = smapper.pushT(vi1.getType());
1098 emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
1099 vo1, vi1, vo2, vi2, vo3, vo1);
1101 final Variable vi3 = smapper.pop();
1102 final Variable vo4 = smapper.pushT(vi1.getType());
1103 final Variable vo3 = smapper.pushT(vi3.getType());
1104 final Variable vo2 = smapper.pushT(vi2.getType());
1105 final Variable vo1 = smapper.pushT(vi1.getType());
1107 emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8;",
1108 vo1, vi1, vo2, vi2, vo3, vi3, vo4, vo1);
1111 final Variable vi3 = smapper.pop();
1113 if (vi3.isCategory2()) {
1114 final Variable vo5 = smapper.pushT(vi2.getType());
1115 final Variable vo4 = smapper.pushT(vi1.getType());
1116 final Variable vo3 = smapper.pushT(vi3.getType());
1117 final Variable vo2 = smapper.pushT(vi2.getType());
1118 final Variable vo1 = smapper.pushT(vi1.getType());
1120 emit(out, "var @1 = @2, @3 = @4, @5 = @6,",
1121 vo1, vi1, vo2, vi2, vo3, vi3);
1122 emit(out, " @1 = @2, @3 = @4;",
1123 vo4, vo1, vo5, vo2);
1125 final Variable vi4 = smapper.pop();
1126 final Variable vo6 = smapper.pushT(vi2.getType());
1127 final Variable vo5 = smapper.pushT(vi1.getType());
1128 final Variable vo4 = smapper.pushT(vi4.getType());
1129 final Variable vo3 = smapper.pushT(vi3.getType());
1130 final Variable vo2 = smapper.pushT(vi2.getType());
1131 final Variable vo1 = smapper.pushT(vi1.getType());
1133 emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8,",
1134 vo1, vi1, vo2, vi2, vo3, vi3, vo4, vi4);
1135 emit(out, " @1 = @2, @3 = @4;",
1136 vo5, vo1, vo6, vo2);
1142 final Variable vi1 = smapper.get(0);
1143 final Variable vi2 = smapper.get(1);
1145 if (vi1.getType() == vi2.getType()) {
1146 final Variable tmp = smapper.pushT(vi1.getType());
1148 emit(out, "var @1 = @2, @2 = @3, @3 = @1;",
1153 smapper.pushT(vi1.getType());
1154 smapper.pushT(vi2.getType());
1159 emit(out, "var @1 = @2;",
1160 smapper.pushI(), Integer.toString(byteCodes[++i]));
1163 emit(out, "var @1 = @2;",
1165 Integer.toString(readShortArg(byteCodes, i)));
1168 case opc_getfield: {
1169 int indx = readUShortArg(byteCodes, i);
1170 String[] fi = jc.getFieldInfoName(indx);
1171 final int type = VarType.fromFieldType(fi[2].charAt(0));
1172 final String mangleClass = mangleSig(fi[0]);
1173 final String mangleClassAccess = accessClass(mangleClass);
1174 emit(out, "var @2 = @4(false)._@3.call(@1);",
1176 smapper.pushT(type), fi[1], mangleClassAccess
1181 case opc_putfield: {
1182 int indx = readUShortArg(byteCodes, i);
1183 String[] fi = jc.getFieldInfoName(indx);
1184 final int type = VarType.fromFieldType(fi[2].charAt(0));
1185 final String mangleClass = mangleSig(fi[0]);
1186 final String mangleClassAccess = accessClass(mangleClass);
1187 emit(out, "@4(false)._@3.call(@2, @1);",
1189 smapper.popA(), fi[1],
1195 case opc_getstatic: {
1196 int indx = readUShortArg(byteCodes, i);
1197 String[] fi = jc.getFieldInfoName(indx);
1198 final int type = VarType.fromFieldType(fi[2].charAt(0));
1199 emit(out, "var @1 = @2(false)._@3();",
1200 smapper.pushT(type),
1201 accessClass(fi[0].replace('/', '_')), fi[1]);
1203 addReference(fi[0]);
1206 case opc_putstatic: {
1207 int indx = readUShortArg(byteCodes, i);
1208 String[] fi = jc.getFieldInfoName(indx);
1209 final int type = VarType.fromFieldType(fi[2].charAt(0));
1210 emit(out, "@1(false)._@2(@3);",
1211 accessClass(fi[0].replace('/', '_')), fi[1],
1212 smapper.popT(type));
1214 addReference(fi[0]);
1217 case opc_checkcast: {
1218 int indx = readUShortArg(byteCodes, i);
1219 generateCheckcast(indx, smapper);
1223 case opc_instanceof: {
1224 int indx = readUShortArg(byteCodes, i);
1225 generateInstanceOf(indx, smapper);
1230 final Variable v = smapper.popA();
1233 emit(out, "{ var @1 = @2; throw @2; }",
1234 smapper.pushA(), v);
1238 case opc_monitorenter: {
1239 out.append("/* monitor enter */");
1244 case opc_monitorexit: {
1245 out.append("/* monitor exit */");
1256 emit(out, "throw 'unknown bytecode @1';",
1257 Integer.toString(c));
1261 generateByteCodeComment(prev, i, byteCodes);
1265 if (previousTrap != null) {
1266 generateCatch(previousTrap, byteCodes.length, topMostLabel);
1268 out.append("\n }\n");
1269 while (openBraces-- > 0) {
1275 private int generateIf(byte[] byteCodes, int i, final Variable v2, final Variable v1, final String test, int topMostLabel) throws IOException {
1276 int indx = i + readShortArg(byteCodes, i);
1277 out.append("if (").append(v1)
1278 .append(' ').append(test).append(' ')
1279 .append(v2).append(") ");
1280 goTo(out, i, indx, topMostLabel);
1284 private int readInt4(byte[] byteCodes, int offset) {
1285 final int d = byteCodes[offset + 0] << 24;
1286 final int c = byteCodes[offset + 1] << 16;
1287 final int b = byteCodes[offset + 2] << 8;
1288 final int a = byteCodes[offset + 3];
1289 return (d & 0xff000000) | (c & 0xff0000) | (b & 0xff00) | (a & 0xff);
1291 private static int readUByte(byte[] byteCodes, int offset) {
1292 return byteCodes[offset] & 0xff;
1295 private static int readUShort(byte[] byteCodes, int offset) {
1296 return ((byteCodes[offset] & 0xff) << 8)
1297 | (byteCodes[offset + 1] & 0xff);
1299 private static int readUShortArg(byte[] byteCodes, int offsetInstruction) {
1300 return readUShort(byteCodes, offsetInstruction + 1);
1303 private static int readShort(byte[] byteCodes, int offset) {
1304 int signed = byteCodes[offset];
1305 byte b0 = (byte)signed;
1306 return (b0 << 8) | (byteCodes[offset + 1] & 0xff);
1308 private static int readShortArg(byte[] byteCodes, int offsetInstruction) {
1309 return readShort(byteCodes, offsetInstruction + 1);
1312 private static void countArgs(String descriptor, char[] returnType, StringBuilder sig, StringBuilder cnt) {
1314 Boolean count = null;
1315 boolean array = false;
1317 int firstPos = sig.length();
1318 while (i < descriptor.length()) {
1319 char ch = descriptor.charAt(i++);
1340 if (ch == 'J' || ch == 'D') {
1346 sig.insert(firstPos, ch);
1348 returnType[0] = '[';
1349 sig.insert(firstPos, "_3");
1358 returnType[0] = 'V';
1359 sig.insert(firstPos, 'V');
1362 int next = descriptor.indexOf(';', i);
1363 String realSig = mangleSig(descriptor, i - 1, next + 1);
1368 sig.append(realSig);
1371 sig.insert(firstPos, realSig);
1373 sig.insert(firstPos, "_3");
1375 returnType[0] = 'L';
1384 throw new IllegalStateException("Invalid char: " + ch);
1389 static String mangleSig(String sig) {
1390 return mangleSig(sig, 0, sig.length());
1393 private static String mangleSig(String txt, int first, int last) {
1394 StringBuilder sb = new StringBuilder();
1395 for (int i = first; i < last; i++) {
1396 final char ch = txt.charAt(i);
1398 case '/': sb.append('_'); break;
1399 case '_': sb.append("_1"); break;
1400 case ';': sb.append("_2"); break;
1401 case '[': sb.append("_3"); break;
1402 default: sb.append(ch); break;
1405 return sb.toString();
1408 private static String findMethodName(MethodData m, StringBuilder cnt) {
1409 StringBuilder name = new StringBuilder();
1410 if ("<init>".equals(m.getName())) { // NOI18N
1411 name.append("cons"); // NOI18N
1412 } else if ("<clinit>".equals(m.getName())) { // NOI18N
1413 name.append("class"); // NOI18N
1415 name.append(m.getName());
1418 countArgs(m.getInternalSig(), new char[1], name, cnt);
1419 return name.toString();
1422 static String findMethodName(String[] mi, StringBuilder cnt, char[] returnType) {
1423 StringBuilder name = new StringBuilder();
1424 String descr = mi[2];//mi.getDescriptor();
1426 if ("<init>".equals(nm)) { // NOI18N
1427 name.append("cons"); // NOI18N
1431 countArgs(descr, returnType, name, cnt);
1432 return name.toString();
1435 private int invokeStaticMethod(byte[] byteCodes, int i, final StackMapper mapper, boolean isStatic)
1436 throws IOException {
1437 int methodIndex = readUShortArg(byteCodes, i);
1438 String[] mi = jc.getFieldInfoName(methodIndex);
1439 char[] returnType = { 'V' };
1440 StringBuilder cnt = new StringBuilder();
1441 String mn = findMethodName(mi, cnt, returnType);
1443 final int numArguments = isStatic ? cnt.length() : cnt.length() + 1;
1444 final Variable[] vars = new Variable[numArguments];
1446 for (int j = numArguments - 1; j >= 0; --j) {
1447 vars[j] = mapper.pop();
1450 if (returnType[0] != 'V') {
1452 .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
1456 final String in = mi[0];
1457 out.append(accessClass(in.replace('/', '_')));
1458 out.append("(false).");
1459 if (mn.startsWith("cons_")) {
1460 out.append("constructor.");
1466 out.append(".call(");
1468 if (numArguments > 0) {
1469 out.append(vars[0]);
1470 for (int j = 1; j < numArguments; ++j) {
1472 out.append(vars[j]);
1480 private int invokeVirtualMethod(byte[] byteCodes, int i, final StackMapper mapper)
1481 throws IOException {
1482 int methodIndex = readUShortArg(byteCodes, i);
1483 String[] mi = jc.getFieldInfoName(methodIndex);
1484 char[] returnType = { 'V' };
1485 StringBuilder cnt = new StringBuilder();
1486 String mn = findMethodName(mi, cnt, returnType);
1488 final int numArguments = cnt.length() + 1;
1489 final Variable[] vars = new Variable[numArguments];
1491 for (int j = numArguments - 1; j >= 0; --j) {
1492 vars[j] = mapper.pop();
1495 if (returnType[0] != 'V') {
1497 .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
1501 out.append(vars[0]).append('.');
1505 for (int j = 1; j < numArguments; ++j) {
1507 out.append(vars[j]);
1515 private void addReference(String cn) throws IOException {
1516 if (requireReference(cn)) {
1517 debug(" /* needs " + cn + " */");
1521 private void outType(String d, StringBuilder out) {
1523 while (d.charAt(0) == '[') {
1527 if (d.charAt(0) == 'L') {
1528 assert d.charAt(d.length() - 1) == ';';
1529 out.append(d.replace('/', '_').substring(0, d.length() - 1));
1535 private String encodeConstant(int entryIndex) throws IOException {
1536 String[] classRef = { null };
1537 String s = jc.stringValue(entryIndex, classRef);
1538 if (classRef[0] != null) {
1539 if (classRef[0].startsWith("[")) {
1540 s = accessClass("java_lang_Class") + "(false).forName__Ljava_lang_Class_2Ljava_lang_String_2('" + classRef[0] + "');";
1542 addReference(classRef[0]);
1543 s = accessClass(s.replace('/', '_')) + "(false).constructor.$class";
1549 private String javaScriptBody(String prefix, MethodData m, boolean isStatic) throws IOException {
1550 byte[] arr = m.findAnnotationData(true);
1554 final String jvmType = "Lorg/apidesign/bck2brwsr/core/JavaScriptBody;";
1555 class P extends AnnotationParser {
1561 String[] args = new String[30];
1565 protected void visitAttr(String type, String attr, String at, String value) {
1566 if (type.equals(jvmType)) {
1567 if ("body".equals(attr)) {
1569 } else if ("args".equals(attr)) {
1570 args[cnt++] = value;
1572 throw new IllegalArgumentException(attr);
1579 if (p.body == null) {
1582 StringBuilder cnt = new StringBuilder();
1583 final String mn = findMethodName(m, cnt);
1584 out.append(prefix).append(mn);
1585 out.append(" = function(");
1588 for (int i = 0; i < cnt.length(); i++) {
1590 space = outputArg(out, p.args, index);
1593 out.append(") {").append("\n");
1595 out.append("\n}\n");
1598 private static String className(ClassData jc) {
1599 //return jc.getName().getInternalName().replace('/', '_');
1600 return jc.getClassName().replace('/', '_');
1603 private static String[] findAnnotation(
1604 byte[] arr, ClassData cd, final String className,
1605 final String... attrNames
1606 ) throws IOException {
1610 final String[] values = new String[attrNames.length];
1611 final boolean[] found = { false };
1612 final String jvmType = "L" + className.replace('.', '/') + ";";
1613 AnnotationParser ap = new AnnotationParser(false, true) {
1615 protected void visitAttr(String type, String attr, String at, String value) {
1616 if (type.equals(jvmType)) {
1618 for (int i = 0; i < attrNames.length; i++) {
1619 if (attrNames[i].equals(attr)) {
1628 return found[0] ? values : null;
1631 private CharSequence initField(FieldData v) {
1632 final String is = v.getInternalSig();
1633 if (is.length() == 1) {
1634 switch (is.charAt(0)) {
1640 case 'I': return " = 0;";
1642 case 'D': return " = 0.0;";
1644 throw new IllegalStateException(is);
1650 private void generateAnno(ClassData cd, final Appendable out, byte[] data) throws IOException {
1651 AnnotationParser ap = new AnnotationParser(true, false) {
1652 int[] cnt = new int[32];
1656 protected void visitAnnotationStart(String attrType, boolean top) throws IOException {
1657 final String slashType = attrType.substring(1, attrType.length() - 1);
1658 requireReference(slashType);
1660 if (cnt[depth]++ > 0) {
1664 out.append('"').append(attrType).append("\" : ");
1671 protected void visitAnnotationEnd(String type, boolean top) throws IOException {
1672 out.append("\n}\n");
1677 protected void visitValueStart(String attrName, char type) throws IOException {
1678 if (cnt[depth]++ > 0) {
1682 if (attrName != null) {
1683 out.append(attrName).append(" : ");
1691 protected void visitValueEnd(String attrName, char type) throws IOException {
1699 protected void visitAttr(String type, String attr, String attrType, String value)
1700 throws IOException {
1701 if (attr == null && value == null) {
1708 protected void visitEnumAttr(String type, String attr, String attrType, String value)
1709 throws IOException {
1710 final String slashType = attrType.substring(1, attrType.length() - 1);
1711 requireReference(slashType);
1713 out.append(accessClass(slashType.replace('/', '_')))
1714 .append("(false).constructor.").append(value);
1720 private static String outputArg(Appendable out, String[] args, int indx) throws IOException {
1721 final String name = args[indx];
1725 if (name.contains(",")) {
1726 throw new IOException("Wrong parameter with ',': " + name);
1732 private static void emit(final Appendable out,
1733 final String format,
1734 final CharSequence... params) throws IOException {
1735 final int length = format.length();
1738 int paramOffset = format.indexOf('@');
1739 while ((paramOffset != -1) && (paramOffset < (length - 1))) {
1740 final char paramChar = format.charAt(paramOffset + 1);
1741 if ((paramChar >= '1') && (paramChar <= '9')) {
1742 final int paramIndex = paramChar - '0' - 1;
1744 out.append(format, processed, paramOffset);
1745 out.append(params[paramIndex]);
1748 processed = paramOffset + 1;
1751 paramOffset = format.indexOf('@', paramOffset + 1);
1754 out.append(format, processed, length);
1757 private void generateCatch(TrapData[] traps, int current, int topMostLabel) throws IOException {
1758 out.append("} catch (e) {\n");
1760 for (TrapData e : traps) {
1764 if (e.catch_cpx != 0) { //not finally
1765 final String classInternalName = jc.getClassName(e.catch_cpx);
1766 addReference(classInternalName);
1767 if ("java/lang/Throwable".equals(classInternalName)) {
1768 out.append("if (e.$instOf_java_lang_Throwable) {");
1769 out.append(" var stA0 = e;");
1770 out.append("} else {");
1771 out.append(" var stA0 = vm.java_lang_Throwable(true);");
1772 out.append(" vm.java_lang_Throwable.cons__VLjava_lang_String_2.call(stA0, e.toString());");
1774 goTo(out, current, e.handler_pc, topMostLabel);
1776 out.append("if (e.$instOf_" + classInternalName.replace('/', '_') + ") {");
1777 out.append("var stA0 = e;");
1778 goTo(out, current, e.handler_pc, topMostLabel);
1782 finallyPC = e.handler_pc;
1785 if (finallyPC == -1) {
1786 out.append("throw e;");
1788 out.append("var stA0 = e;");
1789 goTo(out, current, finallyPC, topMostLabel);
1794 private static void goTo(Appendable out, int current, int to, int canBack) throws IOException {
1797 out.append("{ gt = 0; continue X_" + to + "; }");
1799 out.append("{ gt = " + to + "; continue X_0; }");
1802 out.append("{ gt = " + to + "; break IF; }");
1806 private static void emitIf(
1807 Appendable out, String pattern, Variable param,
1808 int current, int to, int canBack
1809 ) throws IOException {
1810 emit(out, pattern, param);
1811 goTo(out, current, to, canBack);
1814 private void generateNewArray(int atype, final StackMapper smapper) throws IOException, IllegalStateException {
1817 case 4: jvmType = "[Z"; break;
1818 case 5: jvmType = "[C"; break;
1819 case 6: jvmType = "[F"; break;
1820 case 7: jvmType = "[D"; break;
1821 case 8: jvmType = "[B"; break;
1822 case 9: jvmType = "[S"; break;
1823 case 10: jvmType = "[I"; break;
1824 case 11: jvmType = "[J"; break;
1825 default: throw new IllegalStateException("Array type: " + atype);
1827 emit(out, "var @2 = Array.prototype.newArray__Ljava_lang_Object_2ZLjava_lang_String_2I(true, '@3', @1);",
1828 smapper.popI(), smapper.pushA(), jvmType);
1831 private void generateANewArray(int type, final StackMapper smapper) throws IOException {
1832 String typeName = jc.getClassName(type);
1833 if (typeName.startsWith("[")) {
1834 typeName = "[" + typeName;
1836 typeName = "[L" + typeName + ";";
1838 emit(out, "var @2 = Array.prototype.newArray__Ljava_lang_Object_2ZLjava_lang_String_2I(false, '@3', @1);",
1839 smapper.popI(), smapper.pushA(), typeName);
1842 private int generateMultiANewArray(int type, final byte[] byteCodes, int i, final StackMapper smapper) throws IOException {
1843 String typeName = jc.getClassName(type);
1844 int dim = readUByte(byteCodes, ++i);
1845 StringBuilder dims = new StringBuilder();
1847 for (int d = 0; d < dim; d++) {
1849 dims.insert(1, ",");
1851 dims.insert(1, smapper.popI());
1854 emit(out, "var @2 = Array.prototype.multiNewArray__Ljava_lang_Object_2Ljava_lang_String_2_3II('@3', @1, 0);",
1855 dims.toString(), smapper.pushA(), typeName);
1859 private int generateTableSwitch(int i, final byte[] byteCodes, final StackMapper smapper, int topMostLabel) throws IOException {
1860 int table = i / 4 * 4 + 4;
1861 int dflt = i + readInt4(byteCodes, table);
1863 int low = readInt4(byteCodes, table);
1865 int high = readInt4(byteCodes, table);
1867 out.append("switch (").append(smapper.popI()).append(") {\n");
1868 while (low <= high) {
1869 int offset = i + readInt4(byteCodes, table);
1871 out.append(" case " + low).append(":"); goTo(out, i, offset, topMostLabel); out.append('\n');
1874 out.append(" default: ");
1875 goTo(out, i, dflt, topMostLabel);
1881 private int generateLookupSwitch(int i, final byte[] byteCodes, final StackMapper smapper, int topMostLabel) throws IOException {
1882 int table = i / 4 * 4 + 4;
1883 int dflt = i + readInt4(byteCodes, table);
1885 int n = readInt4(byteCodes, table);
1887 out.append("switch (").append(smapper.popI()).append(") {\n");
1889 int cnstnt = readInt4(byteCodes, table);
1891 int offset = i + readInt4(byteCodes, table);
1893 out.append(" case " + cnstnt).append(": "); goTo(out, i, offset, topMostLabel); out.append('\n');
1895 out.append(" default: ");
1896 goTo(out, i, dflt, topMostLabel);
1902 private void generateInstanceOf(int indx, final StackMapper smapper) throws IOException {
1903 final String type = jc.getClassName(indx);
1904 if (!type.startsWith("[")) {
1905 emit(out, "var @2 = @1 != null && @1.$instOf_@3 ? 1 : 0;",
1906 smapper.popA(), smapper.pushI(),
1907 type.replace('/', '_'));
1909 emit(out, "var @2 = vm.java_lang_Class(false).forName__Ljava_lang_Class_2Ljava_lang_String_2('@3').isInstance__ZLjava_lang_Object_2(@1);",
1910 smapper.popA(), smapper.pushI(),
1916 private void generateCheckcast(int indx, final StackMapper smapper) throws IOException {
1917 final String type = jc.getClassName(indx);
1918 if (!type.startsWith("[")) {
1920 "if (@1 !== null && !@1.$instOf_@2) throw {};",
1921 smapper.getA(0), type.replace('/', '_'));
1923 emit(out, "vm.java_lang_Class(false).forName__Ljava_lang_Class_2Ljava_lang_String_2('@2').cast__Ljava_lang_Object_2Ljava_lang_Object_2(@1);",
1924 smapper.getA(0), type
1929 private void generateByteCodeComment(int prev, int i, final byte[] byteCodes) throws IOException {
1930 for (int j = prev; j <= i; j++) {
1932 final int cc = readUByte(byteCodes, j);
1933 out.append(Integer.toString(cc));