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 protected void declaredClass(ClassData classData, String mangledName)
66 /** Prints out a debug message.
68 * @param msg the message
69 * @return true if the message has been printed
72 boolean debug(String msg) throws IOException {
78 * Converts a given class file to a JavaScript version.
80 * @param classFile input stream with code of the .class file
81 * @return the initialization code for this class, if any. Otherwise <code>null</code>
83 * @throws IOException if something goes wrong during read or write or translating
86 public String compile(InputStream classFile) throws IOException {
87 this.jc = new ClassData(classFile);
88 if (jc.getMajor_version() < 50) {
89 throw new IOException("Can't compile " + jc.getClassName() + ". Class file version " + jc.getMajor_version() + "."
90 + jc.getMinor_version() + " - recompile with -target 1.6 (at least)."
93 byte[] arrData = jc.findAnnotationData(true);
94 String[] arr = findAnnotation(arrData, jc,
95 "org.apidesign.bck2brwsr.core.ExtraJavaScript",
96 "resource", "processByteCode"
99 if (!arr[0].isEmpty()) {
100 requireScript(arr[0]);
102 if ("0".equals(arr[1])) {
106 String[] proto = findAnnotation(arrData, jc,
107 "org.apidesign.bck2brwsr.core.JavaScriptPrototype",
108 "container", "prototype"
110 StringArray toInitilize = new StringArray();
111 final String className = className(jc);
112 out.append("\n\n").append(assignClass(className));
113 out.append("function ").append(className).append("() {");
114 out.append("\n var CLS = ").append(className).append(';');
115 out.append("\n if (!CLS.$class) {");
117 String sc = jc.getSuperClassName(); // with _
118 out.append("\n var pp = ").
119 append(accessClass(sc.replace('/', '_'))).append("(true);");
120 out.append("\n var p = CLS.prototype = pp;");
121 out.append("\n var c = p;");
122 out.append("\n var sprcls = pp.constructor.$class;");
124 out.append("\n var p = CLS.prototype = ").append(proto[1]).append(";");
125 if (proto[0] == null) {
128 out.append("\n var c = ").append(proto[0]).append(";");
129 out.append("\n var sprcls = null;");
131 for (FieldData v : jc.getFields()) {
133 out.append("\n CLS.").append(v.getName()).append(initField(v));
134 out.append("\n c._").append(v.getName()).append(" = function (v) {")
135 .append(" if (arguments.length == 1) CLS.").append(v.getName())
136 .append(" = v; return CLS.").
137 append(v.getName()).append("; };");
139 out.append("\n c._").append(v.getName()).append(" = function (v) {")
140 .append(" if (arguments.length == 1) this.fld_").
141 append(className).append('_').append(v.getName())
142 .append(" = v; return this.fld_").
143 append(className).append('_').append(v.getName())
147 // obfuscationDelegate.exportField(out, "c", "_" + v.getName(), v);
149 for (MethodData m : jc.getMethods()) {
150 byte[] onlyArr = m.findAnnotationData(true);
151 String[] only = findAnnotation(onlyArr, jc,
152 "org.apidesign.bck2brwsr.core.JavaScriptOnly",
156 if (only[0] != null && only[1] != null) {
157 out.append("\n p.").append(only[0]).append(" = ")
158 .append(only[1]).append(";");
167 mn = generateStaticMethod(destObject, m, toInitilize);
169 if (m.isConstructor()) {
171 mn = generateInstanceMethod(destObject, m);
174 mn = generateInstanceMethod(destObject, m);
177 // obfuscationDelegate.exportMethod(out, destObject, mn, m);
178 byte[] runAnno = m.findAnnotationData(false);
179 if (runAnno != null) {
180 out.append("\n ").append(destObject).append(".").append(mn).append(".anno = {");
181 generateAnno(jc, out, runAnno);
184 out.append("\n ").append(destObject).append(".").append(mn).append(".access = " + m.getAccess()).append(";");
185 out.append("\n ").append(destObject).append(".").append(mn).append(".cls = CLS;");
187 out.append("\n c.constructor = CLS;");
188 out.append("\n c['$instOf_").append(className).append("'] = true;");
189 for (String superInterface : jc.getSuperInterfaces()) {
190 out.append("\n c['$instOf_").append(superInterface.replace('/', '_')).append("'] = true;");
192 out.append("\n CLS.$class = 'temp';");
193 out.append("\n CLS.$class = ");
194 out.append(accessClass("java_lang_Class(true);"));
195 out.append("\n CLS.$class.jvmName = '").append(jc.getClassName()).append("';");
196 out.append("\n CLS.$class.superclass = sprcls;");
197 out.append("\n CLS.$class.access = ").append(jc.getAccessFlags()+";");
198 out.append("\n CLS.$class.cnstr = CLS;");
199 byte[] classAnno = jc.findAnnotationData(false);
200 if (classAnno != null) {
201 out.append("\n CLS.$class.anno = {");
202 generateAnno(jc, out, classAnno);
205 for (String init : toInitilize.toArray()) {
206 out.append("\n ").append(init).append("();");
209 out.append("\n if (arguments.length === 0) {");
210 out.append("\n if (!(this instanceof CLS)) {");
211 out.append("\n return new CLS();");
213 for (FieldData v : jc.getFields()) {
214 byte[] onlyArr = v.findAnnotationData(true);
215 String[] only = findAnnotation(onlyArr, jc,
216 "org.apidesign.bck2brwsr.core.JavaScriptOnly",
220 if (only[0] != null && only[1] != null) {
221 out.append("\n p.").append(only[0]).append(" = ")
222 .append(only[1]).append(";");
227 out.append("\n this.fld_").
228 append(className).append('_').
229 append(v.getName()).append(initField(v));
232 out.append("\n return this;");
234 out.append("\n return arguments[0] ? new CLS() : CLS.prototype;");
237 declaredClass(jc, className);
239 // StringBuilder sb = new StringBuilder();
240 // for (String init : toInitilize.toArray()) {
241 // sb.append("\n").append(init).append("();");
245 private String generateStaticMethod(String destObject, MethodData m, StringArray toInitilize) throws IOException {
246 String jsb = javaScriptBody(destObject, m, true);
250 final String mn = findMethodName(m, new StringBuilder());
251 if (mn.equals("class__V")) {
252 toInitilize.add(accessClass(className(jc)) + "(false)." + mn);
254 generateMethod(destObject, mn, m);
258 private String generateInstanceMethod(String destObject, MethodData m) throws IOException {
259 String jsb = javaScriptBody(destObject, m, false);
263 final String mn = findMethodName(m, new StringBuilder());
264 generateMethod(destObject, mn, m);
268 private void generateMethod(String destObject, String name, MethodData m)
270 final StackMapIterator stackMapIterator = m.createStackMapIterator();
271 TrapDataIterator trap = m.getTrapDataIterator();
272 final LocalsMapper lmapper =
273 new LocalsMapper(stackMapIterator.getArguments());
275 out.append(destObject).append(".").append(name).append(" = function(");
276 lmapper.outputArguments(out, m.isStatic());
277 out.append(") {").append("\n");
279 final byte[] byteCodes = m.getCode();
280 if (byteCodes == null) {
281 out.append(" throw 'no code found for ")
282 .append(jc.getClassName()).append('.')
283 .append(m.getName()).append("';\n");
288 final StackMapper smapper = new StackMapper();
291 out.append(" var ").append(" lcA0 = this;\n");
294 int lastStackFrame = -1;
295 TrapData[] previousTrap = null;
296 boolean wide = false;
298 out.append("\n var gt = 0;\n");
300 int topMostLabel = 0;
301 for (int i = 0; i < byteCodes.length; i++) {
303 stackMapIterator.advanceTo(i);
304 boolean changeInCatch = trap.advanceTo(i);
305 if (changeInCatch || lastStackFrame != stackMapIterator.getFrameIndex()) {
306 if (previousTrap != null) {
307 generateCatch(previousTrap, i, topMostLabel);
311 if (lastStackFrame != stackMapIterator.getFrameIndex()) {
315 if (openBraces > 64) {
316 for (int c = 0; c < 64; c++) {
317 out.append("break;}\n");
323 lastStackFrame = stackMapIterator.getFrameIndex();
324 lmapper.syncWithFrameLocals(stackMapIterator.getFrameLocals());
325 smapper.syncWithFrameStack(stackMapIterator.getFrameStack());
326 out.append(" X_" + i).append(": for (;;) { IF: if (gt <= " + i + ") {\n");
328 changeInCatch = true;
330 debug(" /* " + i + " */ ");
332 if (changeInCatch && trap.useTry()) {
334 previousTrap = trap.current();
336 final int c = readUByte(byteCodes, i);
339 emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(0));
342 emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(0));
345 emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(0));
348 emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(0));
351 emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(0));
354 emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(1));
357 emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(1));
360 emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(1));
363 emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(1));
366 emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(1));
369 emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(2));
372 emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(2));
375 emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(2));
378 emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(2));
381 emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(2));
384 emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(3));
387 emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(3));
390 emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(3));
393 emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(3));
396 emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(3));
400 final int indx = wide ? readUShort(byteCodes, i++)
401 : readUByte(byteCodes, i);
403 emit(out, "var @1 = @2;",
404 smapper.pushI(), lmapper.getI(indx));
409 final int indx = wide ? readUShort(byteCodes, i++)
410 : readUByte(byteCodes, i);
412 emit(out, "var @1 = @2;",
413 smapper.pushL(), lmapper.getL(indx));
418 final int indx = wide ? readUShort(byteCodes, i++)
419 : readUByte(byteCodes, i);
421 emit(out, "var @1 = @2;",
422 smapper.pushF(), lmapper.getF(indx));
427 final int indx = wide ? readUShort(byteCodes, i++)
428 : readUByte(byteCodes, i);
430 emit(out, "var @1 = @2;",
431 smapper.pushD(), lmapper.getD(indx));
436 final int indx = wide ? readUShort(byteCodes, i++)
437 : readUByte(byteCodes, i);
439 emit(out, "var @1 = @2;",
440 smapper.pushA(), lmapper.getA(indx));
445 final int indx = wide ? readUShort(byteCodes, i++)
446 : readUByte(byteCodes, i);
448 emit(out, "var @1 = @2;",
449 lmapper.setI(indx), smapper.popI());
454 final int indx = wide ? readUShort(byteCodes, i++)
455 : readUByte(byteCodes, i);
457 emit(out, "var @1 = @2;",
458 lmapper.setL(indx), smapper.popL());
463 final int indx = wide ? readUShort(byteCodes, i++)
464 : readUByte(byteCodes, i);
466 emit(out, "var @1 = @2;",
467 lmapper.setF(indx), smapper.popF());
472 final int indx = wide ? readUShort(byteCodes, i++)
473 : readUByte(byteCodes, i);
475 emit(out, "var @1 = @2;",
476 lmapper.setD(indx), smapper.popD());
481 final int indx = wide ? readUShort(byteCodes, i++)
482 : readUByte(byteCodes, i);
484 emit(out, "var @1 = @2;",
485 lmapper.setA(indx), smapper.popA());
489 emit(out, "var @1 = @2;", lmapper.setA(0), smapper.popA());
492 emit(out, "var @1 = @2;", lmapper.setI(0), smapper.popI());
495 emit(out, "var @1 = @2;", lmapper.setL(0), smapper.popL());
498 emit(out, "var @1 = @2;", lmapper.setF(0), smapper.popF());
501 emit(out, "var @1 = @2;", lmapper.setD(0), smapper.popD());
504 emit(out, "var @1 = @2;", lmapper.setA(1), smapper.popA());
507 emit(out, "var @1 = @2;", lmapper.setI(1), smapper.popI());
510 emit(out, "var @1 = @2;", lmapper.setL(1), smapper.popL());
513 emit(out, "var @1 = @2;", lmapper.setF(1), smapper.popF());
516 emit(out, "var @1 = @2;", lmapper.setD(1), smapper.popD());
519 emit(out, "var @1 = @2;", lmapper.setA(2), smapper.popA());
522 emit(out, "var @1 = @2;", lmapper.setI(2), smapper.popI());
525 emit(out, "var @1 = @2;", lmapper.setL(2), smapper.popL());
528 emit(out, "var @1 = @2;", lmapper.setF(2), smapper.popF());
531 emit(out, "var @1 = @2;", lmapper.setD(2), smapper.popD());
534 emit(out, "var @1 = @2;", lmapper.setA(3), smapper.popA());
537 emit(out, "var @1 = @2;", lmapper.setI(3), smapper.popI());
540 emit(out, "var @1 = @2;", lmapper.setL(3), smapper.popL());
543 emit(out, "var @1 = @2;", lmapper.setF(3), smapper.popF());
546 emit(out, "var @1 = @2;", lmapper.setD(3), smapper.popD());
549 emit(out, "@1 = @1.add32(@2);", smapper.getI(1), smapper.popI());
552 emit(out, "@1 = @1.add64(@2);", smapper.getL(1), smapper.popL());
555 emit(out, "@1 += @2;", smapper.getF(1), smapper.popF());
558 emit(out, "@1 += @2;", smapper.getD(1), smapper.popD());
561 emit(out, "@1 = @1.sub32(@2);", smapper.getI(1), smapper.popI());
564 emit(out, "@1 = @1.sub64(@2);", smapper.getL(1), smapper.popL());
567 emit(out, "@1 -= @2;", smapper.getF(1), smapper.popF());
570 emit(out, "@1 -= @2;", smapper.getD(1), smapper.popD());
573 emit(out, "@1 = @1.mul32(@2);", smapper.getI(1), smapper.popI());
576 emit(out, "@1 = @1.mul64(@2);", 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.div32(@2);",
586 smapper.getI(1), smapper.popI());
589 emit(out, "@1 = @1.div64(@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 = @1.mod32(@2);",
600 smapper.getI(1), smapper.popI());
603 emit(out, "@1 = @1.mod64(@2);",
604 smapper.getL(1), smapper.popL());
607 emit(out, "@1 %= @2;", smapper.getF(1), smapper.popF());
610 emit(out, "@1 %= @2;", smapper.getD(1), smapper.popD());
613 emit(out, "@1 &= @2;", smapper.getI(1), smapper.popI());
616 emit(out, "@1 = @1.and64(@2);", smapper.getL(1), smapper.popL());
619 emit(out, "@1 |= @2;", smapper.getI(1), smapper.popI());
622 emit(out, "@1 = @1.or64(@2);", smapper.getL(1), smapper.popL());
625 emit(out, "@1 ^= @2;", smapper.getI(1), smapper.popI());
628 emit(out, "@1 = @1.xor64(@2);", smapper.getL(1), smapper.popL());
631 emit(out, "@1 = @1.neg32();", smapper.getI(0));
634 emit(out, "@1 = @1.neg64();", smapper.getL(0));
637 emit(out, "@1 = -@1;", smapper.getF(0));
640 emit(out, "@1 = -@1;", smapper.getD(0));
643 emit(out, "@1 <<= @2;", smapper.getI(1), smapper.popI());
646 emit(out, "@1 = @1.shl64(@2);", smapper.getL(1), smapper.popI());
649 emit(out, "@1 >>= @2;", smapper.getI(1), smapper.popI());
652 emit(out, "@1 = @1.shr64(@2);", smapper.getL(1), smapper.popI());
655 emit(out, "@1 >>>= @2;", smapper.getI(1), smapper.popI());
658 emit(out, "@1 = @1.ushr64(@2);", smapper.getL(1), smapper.popI());
662 final int varIndx = wide ? readUShort(byteCodes, i++)
663 : readUByte(byteCodes, i);
665 final int incrBy = wide ? readShort(byteCodes, i++)
669 emit(out, "@1++;", lmapper.getI(varIndx));
671 emit(out, "@1 += @2;",
672 lmapper.getI(varIndx),
673 Integer.toString(incrBy));
678 emit(out, "return;");
681 emit(out, "return @1;", smapper.popI());
684 emit(out, "return @1;", smapper.popL());
687 emit(out, "return @1;", smapper.popF());
690 emit(out, "return @1;", smapper.popD());
693 emit(out, "return @1;", smapper.popA());
696 emit(out, "var @2 = @1;", smapper.popI(), smapper.pushL());
699 emit(out, "var @2 = @1;", smapper.popI(), smapper.pushF());
702 emit(out, "var @2 = @1;", smapper.popI(), smapper.pushD());
705 emit(out, "var @2 = @1.toInt32();", smapper.popL(), smapper.pushI());
709 emit(out, "var @2 = @1.toFP();", smapper.popL(), smapper.pushF());
712 emit(out, "var @2 = @1.toFP();", smapper.popL(), smapper.pushD());
715 emit(out, "var @2 = @1;", smapper.popF(), smapper.pushD());
718 emit(out, "var @2 = @1;", smapper.popD(), smapper.pushF());
721 emit(out, "var @2 = @1.toInt32();",
722 smapper.popF(), smapper.pushI());
725 emit(out, "var @2 = @1.toLong();",
726 smapper.popF(), smapper.pushL());
729 emit(out, "var @2 = @1.toInt32();",
730 smapper.popD(), smapper.pushI());
733 emit(out, "var @2 = @1.toLong();",
734 smapper.popD(), smapper.pushL());
737 emit(out, "var @1 = @1.toInt8();", smapper.getI(0));
740 out.append("{ /* number conversion */ }");
743 emit(out, "var @1 = @1.toInt16();", smapper.getI(0));
745 case opc_aconst_null:
746 emit(out, "var @1 = null;", smapper.pushA());
749 emit(out, "var @1 = -1;", smapper.pushI());
752 emit(out, "var @1 = 0;", smapper.pushI());
755 emit(out, "var @1 = 0;", smapper.pushD());
758 emit(out, "var @1 = 0;", smapper.pushL());
761 emit(out, "var @1 = 0;", smapper.pushF());
764 emit(out, "var @1 = 1;", smapper.pushI());
767 emit(out, "var @1 = 1;", smapper.pushL());
770 emit(out, "var @1 = 1;", smapper.pushF());
773 emit(out, "var @1 = 1;", smapper.pushD());
776 emit(out, "var @1 = 2;", smapper.pushI());
779 emit(out, "var @1 = 2;", smapper.pushF());
782 emit(out, "var @1 = 3;", smapper.pushI());
785 emit(out, "var @1 = 4;", smapper.pushI());
788 emit(out, "var @1 = 5;", smapper.pushI());
791 int indx = readUByte(byteCodes, ++i);
792 String v = encodeConstant(indx);
793 int type = VarType.fromConstantType(jc.getTag(indx));
794 emit(out, "var @1 = @2;", smapper.pushT(type), v);
799 int indx = readUShortArg(byteCodes, i);
801 String v = encodeConstant(indx);
802 int type = VarType.fromConstantType(jc.getTag(indx));
803 if (type == VarType.LONG) {
804 final Long lv = new Long(v);
805 final int low = (int)(lv.longValue() & 0xFFFFFFFF);
806 final int hi = (int)(lv.longValue() >> 32);
807 emit(out, "var @1 = 0x@3.next32(0x@2);", smapper.pushL(),
808 Integer.toHexString(low), Integer.toHexString(hi));
810 emit(out, "var @1 = @2;", smapper.pushT(type), v);
815 emit(out, "var @3 = @2.compare64(@1);",
816 smapper.popL(), smapper.popL(), smapper.pushI());
820 emit(out, "var @3 = (@2 == @1) ? 0 : ((@2 < @1) ? -1 : 1);",
821 smapper.popF(), smapper.popF(), smapper.pushI());
825 emit(out, "var @3 = (@2 == @1) ? 0 : ((@2 < @1) ? -1 : 1);",
826 smapper.popD(), smapper.popD(), smapper.pushI());
829 i = generateIf(byteCodes, i, smapper.popA(), smapper.popA(),
830 "===", topMostLabel);
833 i = generateIf(byteCodes, i, smapper.popA(), smapper.popA(),
834 "!==", topMostLabel);
837 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
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);
869 int indx = i + readShortArg(byteCodes, i);
870 emitIf(out, "if (@1 > 0) ",
871 smapper.popI(), i, indx, topMostLabel);
876 int indx = i + readShortArg(byteCodes, i);
877 emitIf(out, "if (@1 >= 0) ",
878 smapper.popI(), i, indx, topMostLabel);
882 case opc_ifnonnull: {
883 int indx = i + readShortArg(byteCodes, i);
884 emitIf(out, "if (@1 !== null) ",
885 smapper.popA(), i, indx, topMostLabel);
890 int indx = i + readShortArg(byteCodes, i);
891 emitIf(out, "if (@1 === null) ",
892 smapper.popA(), i, indx, topMostLabel);
897 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
901 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
905 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
909 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
913 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
917 int indx = i + readShortArg(byteCodes, i);
918 goTo(out, i, indx, topMostLabel);
922 case opc_lookupswitch: {
923 i = generateLookupSwitch(i, byteCodes, smapper, topMostLabel);
926 case opc_tableswitch: {
927 i = generateTableSwitch(i, byteCodes, smapper, topMostLabel);
930 case opc_invokeinterface: {
931 i = invokeVirtualMethod(byteCodes, i, smapper) + 2;
934 case opc_invokevirtual:
935 i = invokeVirtualMethod(byteCodes, i, smapper);
937 case opc_invokespecial:
938 i = invokeStaticMethod(byteCodes, i, smapper, false);
940 case opc_invokestatic:
941 i = invokeStaticMethod(byteCodes, i, smapper, true);
944 int indx = readUShortArg(byteCodes, i);
945 String ci = jc.getClassName(indx);
946 emit(out, "var @1 = new @2;",
947 smapper.pushA(), accessClass(ci.replace('/', '_')));
953 int atype = readUByte(byteCodes, ++i);
954 generateNewArray(atype, smapper);
956 case opc_anewarray: {
957 int type = readUShortArg(byteCodes, i);
959 generateANewArray(type, smapper);
962 case opc_multianewarray: {
963 int type = readUShortArg(byteCodes, i);
965 i = generateMultiANewArray(type, byteCodes, i, smapper);
968 case opc_arraylength:
969 emit(out, "var @2 = @1.length;",
970 smapper.popA(), smapper.pushI());
973 emit(out, "@3.at(@2, @1);",
974 smapper.popL(), smapper.popI(), smapper.popA());
977 emit(out, "@3.at(@2, @1);",
978 smapper.popF(), smapper.popI(), smapper.popA());
981 emit(out, "@3.at(@2, @1);",
982 smapper.popD(), smapper.popI(), smapper.popA());
985 emit(out, "@3.at(@2, @1);",
986 smapper.popA(), smapper.popI(), smapper.popA());
992 emit(out, "@3.at(@2, @1);",
993 smapper.popI(), smapper.popI(), smapper.popA());
996 emit(out, "var @3 = @2.at(@1);",
997 smapper.popI(), smapper.popA(), smapper.pushL());
1000 emit(out, "var @3 = @2.at(@1);",
1001 smapper.popI(), smapper.popA(), smapper.pushF());
1004 emit(out, "var @3 = @2.at(@1);",
1005 smapper.popI(), smapper.popA(), smapper.pushD());
1008 emit(out, "var @3 = @2.at(@1);",
1009 smapper.popI(), smapper.popA(), smapper.pushA());
1015 emit(out, "var @3 = @2.at(@1);",
1016 smapper.popI(), smapper.popA(), smapper.pushI());
1024 final Variable v = smapper.get(0);
1025 emit(out, "var @1 = @2;", smapper.pushT(v.getType()), v);
1029 final Variable vi1 = smapper.get(0);
1031 if (vi1.isCategory2()) {
1032 emit(out, "var @1 = @2;",
1033 smapper.pushT(vi1.getType()), vi1);
1035 final Variable vi2 = smapper.get(1);
1036 emit(out, "var @1 = @2, @3 = @4;",
1037 smapper.pushT(vi2.getType()), vi2,
1038 smapper.pushT(vi1.getType()), vi1);
1043 final Variable vi1 = smapper.pop();
1044 final Variable vi2 = smapper.pop();
1045 final Variable vo3 = smapper.pushT(vi1.getType());
1046 final Variable vo2 = smapper.pushT(vi2.getType());
1047 final Variable vo1 = smapper.pushT(vi1.getType());
1049 emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
1050 vo1, vi1, vo2, vi2, vo3, vo1);
1054 final Variable vi1 = smapper.pop();
1055 final Variable vi2 = smapper.pop();
1057 if (vi1.isCategory2()) {
1058 final Variable vo3 = smapper.pushT(vi1.getType());
1059 final Variable vo2 = smapper.pushT(vi2.getType());
1060 final Variable vo1 = smapper.pushT(vi1.getType());
1062 emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
1063 vo1, vi1, vo2, vi2, vo3, vo1);
1065 final Variable vi3 = smapper.pop();
1066 final Variable vo5 = smapper.pushT(vi2.getType());
1067 final Variable vo4 = smapper.pushT(vi1.getType());
1068 final Variable vo3 = smapper.pushT(vi3.getType());
1069 final Variable vo2 = smapper.pushT(vi2.getType());
1070 final Variable vo1 = smapper.pushT(vi1.getType());
1072 emit(out, "var @1 = @2, @3 = @4, @5 = @6,",
1073 vo1, vi1, vo2, vi2, vo3, vi3);
1074 emit(out, " @1 = @2, @3 = @4;",
1075 vo4, vo1, vo5, vo2);
1080 final Variable vi1 = smapper.pop();
1081 final Variable vi2 = smapper.pop();
1083 if (vi2.isCategory2()) {
1084 final Variable vo3 = smapper.pushT(vi1.getType());
1085 final Variable vo2 = smapper.pushT(vi2.getType());
1086 final Variable vo1 = smapper.pushT(vi1.getType());
1088 emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
1089 vo1, vi1, vo2, vi2, vo3, vo1);
1091 final Variable vi3 = smapper.pop();
1092 final Variable vo4 = smapper.pushT(vi1.getType());
1093 final Variable vo3 = smapper.pushT(vi3.getType());
1094 final Variable vo2 = smapper.pushT(vi2.getType());
1095 final Variable vo1 = smapper.pushT(vi1.getType());
1097 emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8;",
1098 vo1, vi1, vo2, vi2, vo3, vi3, vo4, vo1);
1103 final Variable vi1 = smapper.pop();
1104 final Variable vi2 = smapper.pop();
1106 if (vi1.isCategory2()) {
1107 if (vi2.isCategory2()) {
1108 final Variable vo3 = smapper.pushT(vi1.getType());
1109 final Variable vo2 = smapper.pushT(vi2.getType());
1110 final Variable vo1 = smapper.pushT(vi1.getType());
1112 emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
1113 vo1, vi1, vo2, vi2, vo3, vo1);
1115 final Variable vi3 = smapper.pop();
1116 final Variable vo4 = smapper.pushT(vi1.getType());
1117 final Variable vo3 = smapper.pushT(vi3.getType());
1118 final Variable vo2 = smapper.pushT(vi2.getType());
1119 final Variable vo1 = smapper.pushT(vi1.getType());
1121 emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8;",
1122 vo1, vi1, vo2, vi2, vo3, vi3, vo4, vo1);
1125 final Variable vi3 = smapper.pop();
1127 if (vi3.isCategory2()) {
1128 final Variable vo5 = smapper.pushT(vi2.getType());
1129 final Variable vo4 = smapper.pushT(vi1.getType());
1130 final Variable vo3 = smapper.pushT(vi3.getType());
1131 final Variable vo2 = smapper.pushT(vi2.getType());
1132 final Variable vo1 = smapper.pushT(vi1.getType());
1134 emit(out, "var @1 = @2, @3 = @4, @5 = @6,",
1135 vo1, vi1, vo2, vi2, vo3, vi3);
1136 emit(out, " @1 = @2, @3 = @4;",
1137 vo4, vo1, vo5, vo2);
1139 final Variable vi4 = smapper.pop();
1140 final Variable vo6 = smapper.pushT(vi2.getType());
1141 final Variable vo5 = smapper.pushT(vi1.getType());
1142 final Variable vo4 = smapper.pushT(vi4.getType());
1143 final Variable vo3 = smapper.pushT(vi3.getType());
1144 final Variable vo2 = smapper.pushT(vi2.getType());
1145 final Variable vo1 = smapper.pushT(vi1.getType());
1147 emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8,",
1148 vo1, vi1, vo2, vi2, vo3, vi3, vo4, vi4);
1149 emit(out, " @1 = @2, @3 = @4;",
1150 vo5, vo1, vo6, vo2);
1156 final Variable vi1 = smapper.get(0);
1157 final Variable vi2 = smapper.get(1);
1159 if (vi1.getType() == vi2.getType()) {
1160 final Variable tmp = smapper.pushT(vi1.getType());
1162 emit(out, "var @1 = @2, @2 = @3, @3 = @1;",
1167 smapper.pushT(vi1.getType());
1168 smapper.pushT(vi2.getType());
1173 emit(out, "var @1 = @2;",
1174 smapper.pushI(), Integer.toString(byteCodes[++i]));
1177 emit(out, "var @1 = @2;",
1179 Integer.toString(readShortArg(byteCodes, i)));
1182 case opc_getfield: {
1183 int indx = readUShortArg(byteCodes, i);
1184 String[] fi = jc.getFieldInfoName(indx);
1185 final int type = VarType.fromFieldType(fi[2].charAt(0));
1186 final String mangleClass = mangleSig(fi[0]);
1187 final String mangleClassAccess = accessClass(mangleClass);
1188 emit(out, "var @2 = @4(false)._@3.call(@1);",
1190 smapper.pushT(type), fi[1], mangleClassAccess
1195 case opc_putfield: {
1196 int indx = readUShortArg(byteCodes, i);
1197 String[] fi = jc.getFieldInfoName(indx);
1198 final int type = VarType.fromFieldType(fi[2].charAt(0));
1199 final String mangleClass = mangleSig(fi[0]);
1200 final String mangleClassAccess = accessClass(mangleClass);
1201 emit(out, "@4(false)._@3.call(@2, @1);",
1203 smapper.popA(), fi[1],
1209 case opc_getstatic: {
1210 int indx = readUShortArg(byteCodes, i);
1211 String[] fi = jc.getFieldInfoName(indx);
1212 final int type = VarType.fromFieldType(fi[2].charAt(0));
1213 emit(out, "var @1 = @2(false)._@3();",
1214 smapper.pushT(type),
1215 accessClass(fi[0].replace('/', '_')), fi[1]);
1217 addReference(fi[0]);
1220 case opc_putstatic: {
1221 int indx = readUShortArg(byteCodes, i);
1222 String[] fi = jc.getFieldInfoName(indx);
1223 final int type = VarType.fromFieldType(fi[2].charAt(0));
1224 emit(out, "@1(false)._@2(@3);",
1225 accessClass(fi[0].replace('/', '_')), fi[1],
1226 smapper.popT(type));
1228 addReference(fi[0]);
1231 case opc_checkcast: {
1232 int indx = readUShortArg(byteCodes, i);
1233 generateCheckcast(indx, smapper);
1237 case opc_instanceof: {
1238 int indx = readUShortArg(byteCodes, i);
1239 generateInstanceOf(indx, smapper);
1244 final Variable v = smapper.popA();
1247 emit(out, "{ var @1 = @2; throw @2; }",
1248 smapper.pushA(), v);
1252 case opc_monitorenter: {
1253 out.append("/* monitor enter */");
1258 case opc_monitorexit: {
1259 out.append("/* monitor exit */");
1270 emit(out, "throw 'unknown bytecode @1';",
1271 Integer.toString(c));
1275 generateByteCodeComment(prev, i, byteCodes);
1279 if (previousTrap != null) {
1280 generateCatch(previousTrap, byteCodes.length, topMostLabel);
1282 out.append("\n }\n");
1283 while (openBraces-- > 0) {
1289 private int generateIf(byte[] byteCodes, int i, final Variable v2, final Variable v1, final String test, int topMostLabel) throws IOException {
1290 int indx = i + readShortArg(byteCodes, i);
1291 out.append("if (").append(v1)
1292 .append(' ').append(test).append(' ')
1293 .append(v2).append(") ");
1294 goTo(out, i, indx, topMostLabel);
1298 private int readInt4(byte[] byteCodes, int offset) {
1299 final int d = byteCodes[offset + 0] << 24;
1300 final int c = byteCodes[offset + 1] << 16;
1301 final int b = byteCodes[offset + 2] << 8;
1302 final int a = byteCodes[offset + 3];
1303 return (d & 0xff000000) | (c & 0xff0000) | (b & 0xff00) | (a & 0xff);
1305 private static int readUByte(byte[] byteCodes, int offset) {
1306 return byteCodes[offset] & 0xff;
1309 private static int readUShort(byte[] byteCodes, int offset) {
1310 return ((byteCodes[offset] & 0xff) << 8)
1311 | (byteCodes[offset + 1] & 0xff);
1313 private static int readUShortArg(byte[] byteCodes, int offsetInstruction) {
1314 return readUShort(byteCodes, offsetInstruction + 1);
1317 private static int readShort(byte[] byteCodes, int offset) {
1318 int signed = byteCodes[offset];
1319 byte b0 = (byte)signed;
1320 return (b0 << 8) | (byteCodes[offset + 1] & 0xff);
1322 private static int readShortArg(byte[] byteCodes, int offsetInstruction) {
1323 return readShort(byteCodes, offsetInstruction + 1);
1326 private static void countArgs(String descriptor, char[] returnType, StringBuilder sig, StringBuilder cnt) {
1328 Boolean count = null;
1329 boolean array = false;
1331 int firstPos = sig.length();
1332 while (i < descriptor.length()) {
1333 char ch = descriptor.charAt(i++);
1354 if (ch == 'J' || ch == 'D') {
1360 sig.insert(firstPos, ch);
1362 returnType[0] = '[';
1363 sig.insert(firstPos, "_3");
1372 returnType[0] = 'V';
1373 sig.insert(firstPos, 'V');
1376 int next = descriptor.indexOf(';', i);
1377 String realSig = mangleSig(descriptor, i - 1, next + 1);
1382 sig.append(realSig);
1385 sig.insert(firstPos, realSig);
1387 sig.insert(firstPos, "_3");
1389 returnType[0] = 'L';
1398 throw new IllegalStateException("Invalid char: " + ch);
1403 static String mangleSig(String sig) {
1404 return mangleSig(sig, 0, sig.length());
1407 private static String mangleSig(String txt, int first, int last) {
1408 StringBuilder sb = new StringBuilder();
1409 for (int i = first; i < last; i++) {
1410 final char ch = txt.charAt(i);
1412 case '/': sb.append('_'); break;
1413 case '_': sb.append("_1"); break;
1414 case ';': sb.append("_2"); break;
1415 case '[': sb.append("_3"); break;
1416 default: sb.append(ch); break;
1419 return sb.toString();
1422 private static String findMethodName(MethodData m, StringBuilder cnt) {
1423 StringBuilder name = new StringBuilder();
1424 if ("<init>".equals(m.getName())) { // NOI18N
1425 name.append("cons"); // NOI18N
1426 } else if ("<clinit>".equals(m.getName())) { // NOI18N
1427 name.append("class"); // NOI18N
1429 name.append(m.getName());
1432 countArgs(m.getInternalSig(), new char[1], name, cnt);
1433 return name.toString();
1436 static String findMethodName(String[] mi, StringBuilder cnt, char[] returnType) {
1437 StringBuilder name = new StringBuilder();
1438 String descr = mi[2];//mi.getDescriptor();
1440 if ("<init>".equals(nm)) { // NOI18N
1441 name.append("cons"); // NOI18N
1445 countArgs(descr, returnType, name, cnt);
1446 return name.toString();
1449 private int invokeStaticMethod(byte[] byteCodes, int i, final StackMapper mapper, boolean isStatic)
1450 throws IOException {
1451 int methodIndex = readUShortArg(byteCodes, i);
1452 String[] mi = jc.getFieldInfoName(methodIndex);
1453 char[] returnType = { 'V' };
1454 StringBuilder cnt = new StringBuilder();
1455 String mn = findMethodName(mi, cnt, returnType);
1457 final int numArguments = isStatic ? cnt.length() : cnt.length() + 1;
1458 final Variable[] vars = new Variable[numArguments];
1460 for (int j = numArguments - 1; j >= 0; --j) {
1461 vars[j] = mapper.pop();
1464 if (returnType[0] != 'V') {
1466 .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
1470 final String in = mi[0];
1471 out.append(accessClass(in.replace('/', '_')));
1472 out.append("(false).");
1473 if (mn.startsWith("cons_")) {
1474 out.append("constructor.");
1480 out.append(".call(");
1482 if (numArguments > 0) {
1483 out.append(vars[0]);
1484 for (int j = 1; j < numArguments; ++j) {
1486 out.append(vars[j]);
1494 private int invokeVirtualMethod(byte[] byteCodes, int i, final StackMapper mapper)
1495 throws IOException {
1496 int methodIndex = readUShortArg(byteCodes, i);
1497 String[] mi = jc.getFieldInfoName(methodIndex);
1498 char[] returnType = { 'V' };
1499 StringBuilder cnt = new StringBuilder();
1500 String mn = findMethodName(mi, cnt, returnType);
1502 final int numArguments = cnt.length() + 1;
1503 final Variable[] vars = new Variable[numArguments];
1505 for (int j = numArguments - 1; j >= 0; --j) {
1506 vars[j] = mapper.pop();
1509 if (returnType[0] != 'V') {
1511 .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
1515 out.append(vars[0]).append('.');
1519 for (int j = 1; j < numArguments; ++j) {
1521 out.append(vars[j]);
1529 private void addReference(String cn) throws IOException {
1530 if (requireReference(cn)) {
1531 debug(" /* needs " + cn + " */");
1535 private void outType(String d, StringBuilder out) {
1537 while (d.charAt(0) == '[') {
1541 if (d.charAt(0) == 'L') {
1542 assert d.charAt(d.length() - 1) == ';';
1543 out.append(d.replace('/', '_').substring(0, d.length() - 1));
1549 private String encodeConstant(int entryIndex) throws IOException {
1550 String[] classRef = { null };
1551 String s = jc.stringValue(entryIndex, classRef);
1552 if (classRef[0] != null) {
1553 if (classRef[0].startsWith("[")) {
1554 s = accessClass("java_lang_Class") + "(false).forName__Ljava_lang_Class_2Ljava_lang_String_2('" + classRef[0] + "');";
1556 addReference(classRef[0]);
1557 s = accessClass(s.replace('/', '_')) + "(false).constructor.$class";
1563 private String javaScriptBody(String destObject, MethodData m, boolean isStatic) throws IOException {
1564 byte[] arr = m.findAnnotationData(true);
1568 final String jvmType = "Lorg/apidesign/bck2brwsr/core/JavaScriptBody;";
1569 class P extends AnnotationParser {
1575 String[] args = new String[30];
1579 protected void visitAttr(String type, String attr, String at, String value) {
1580 if (type.equals(jvmType)) {
1581 if ("body".equals(attr)) {
1583 } else if ("args".equals(attr)) {
1584 args[cnt++] = value;
1586 throw new IllegalArgumentException(attr);
1593 if (p.body == null) {
1596 StringBuilder cnt = new StringBuilder();
1597 final String mn = findMethodName(m, cnt);
1598 out.append(destObject).append(".").append(mn);
1599 out.append(" = function(");
1602 for (int i = 0; i < cnt.length(); i++) {
1604 space = outputArg(out, p.args, index);
1607 out.append(") {").append("\n");
1609 out.append("\n}\n");
1612 private static String className(ClassData jc) {
1613 //return jc.getName().getInternalName().replace('/', '_');
1614 return jc.getClassName().replace('/', '_');
1617 private static String[] findAnnotation(
1618 byte[] arr, ClassData cd, final String className,
1619 final String... attrNames
1620 ) throws IOException {
1624 final String[] values = new String[attrNames.length];
1625 final boolean[] found = { false };
1626 final String jvmType = "L" + className.replace('.', '/') + ";";
1627 AnnotationParser ap = new AnnotationParser(false, true) {
1629 protected void visitAttr(String type, String attr, String at, String value) {
1630 if (type.equals(jvmType)) {
1632 for (int i = 0; i < attrNames.length; i++) {
1633 if (attrNames[i].equals(attr)) {
1642 return found[0] ? values : null;
1645 private CharSequence initField(FieldData v) {
1646 final String is = v.getInternalSig();
1647 if (is.length() == 1) {
1648 switch (is.charAt(0)) {
1654 case 'I': return " = 0;";
1656 case 'D': return " = 0.0;";
1658 throw new IllegalStateException(is);
1664 private void generateAnno(ClassData cd, final Appendable out, byte[] data) throws IOException {
1665 AnnotationParser ap = new AnnotationParser(true, false) {
1666 int[] cnt = new int[32];
1670 protected void visitAnnotationStart(String attrType, boolean top) throws IOException {
1671 final String slashType = attrType.substring(1, attrType.length() - 1);
1672 requireReference(slashType);
1674 if (cnt[depth]++ > 0) {
1678 out.append('"').append(attrType).append("\" : ");
1685 protected void visitAnnotationEnd(String type, boolean top) throws IOException {
1686 out.append("\n}\n");
1691 protected void visitValueStart(String attrName, char type) throws IOException {
1692 if (cnt[depth]++ > 0) {
1696 if (attrName != null) {
1697 out.append(attrName).append(" : ");
1705 protected void visitValueEnd(String attrName, char type) throws IOException {
1713 protected void visitAttr(String type, String attr, String attrType, String value)
1714 throws IOException {
1715 if (attr == null && value == null) {
1722 protected void visitEnumAttr(String type, String attr, String attrType, String value)
1723 throws IOException {
1724 final String slashType = attrType.substring(1, attrType.length() - 1);
1725 requireReference(slashType);
1727 out.append(accessClass(slashType.replace('/', '_')))
1728 .append("(false).constructor.").append(value);
1734 private static String outputArg(Appendable out, String[] args, int indx) throws IOException {
1735 final String name = args[indx];
1739 if (name.contains(",")) {
1740 throw new IOException("Wrong parameter with ',': " + name);
1746 private static void emit(final Appendable out,
1747 final String format,
1748 final CharSequence... params) throws IOException {
1749 final int length = format.length();
1752 int paramOffset = format.indexOf('@');
1753 while ((paramOffset != -1) && (paramOffset < (length - 1))) {
1754 final char paramChar = format.charAt(paramOffset + 1);
1755 if ((paramChar >= '1') && (paramChar <= '9')) {
1756 final int paramIndex = paramChar - '0' - 1;
1758 out.append(format, processed, paramOffset);
1759 out.append(params[paramIndex]);
1762 processed = paramOffset + 1;
1765 paramOffset = format.indexOf('@', paramOffset + 1);
1768 out.append(format, processed, length);
1771 private void generateCatch(TrapData[] traps, int current, int topMostLabel) throws IOException {
1772 out.append("} catch (e) {\n");
1774 for (TrapData e : traps) {
1778 if (e.catch_cpx != 0) { //not finally
1779 final String classInternalName = jc.getClassName(e.catch_cpx);
1780 addReference(classInternalName);
1781 if ("java/lang/Throwable".equals(classInternalName)) {
1782 out.append("if (e['$instOf_java_lang_Throwable']) {");
1783 out.append(" var stA0 = e;");
1784 out.append("} else {");
1785 out.append(" var stA0 = vm.java_lang_Throwable(true);");
1786 out.append(" vm.java_lang_Throwable.cons__VLjava_lang_String_2.call(stA0, e.toString());");
1788 goTo(out, current, e.handler_pc, topMostLabel);
1790 out.append("if (e['$instOf_" + classInternalName.replace('/', '_') + "']) {");
1791 out.append("var stA0 = e;");
1792 goTo(out, current, e.handler_pc, topMostLabel);
1796 finallyPC = e.handler_pc;
1799 if (finallyPC == -1) {
1800 out.append("throw e;");
1802 out.append("var stA0 = e;");
1803 goTo(out, current, finallyPC, topMostLabel);
1808 private static void goTo(Appendable out, int current, int to, int canBack) throws IOException {
1811 out.append("{ gt = 0; continue X_" + to + "; }");
1813 out.append("{ gt = " + to + "; continue X_0; }");
1816 out.append("{ gt = " + to + "; break IF; }");
1820 private static void emitIf(
1821 Appendable out, String pattern, Variable param,
1822 int current, int to, int canBack
1823 ) throws IOException {
1824 emit(out, pattern, param);
1825 goTo(out, current, to, canBack);
1828 private void generateNewArray(int atype, final StackMapper smapper) throws IOException, IllegalStateException {
1831 case 4: jvmType = "[Z"; break;
1832 case 5: jvmType = "[C"; break;
1833 case 6: jvmType = "[F"; break;
1834 case 7: jvmType = "[D"; break;
1835 case 8: jvmType = "[B"; break;
1836 case 9: jvmType = "[S"; break;
1837 case 10: jvmType = "[I"; break;
1838 case 11: jvmType = "[J"; break;
1839 default: throw new IllegalStateException("Array type: " + atype);
1841 emit(out, "var @2 = Array.prototype.newArray__Ljava_lang_Object_2ZLjava_lang_String_2I(true, '@3', @1);",
1842 smapper.popI(), smapper.pushA(), jvmType);
1845 private void generateANewArray(int type, final StackMapper smapper) throws IOException {
1846 String typeName = jc.getClassName(type);
1847 if (typeName.startsWith("[")) {
1848 typeName = "[" + typeName;
1850 typeName = "[L" + typeName + ";";
1852 emit(out, "var @2 = Array.prototype.newArray__Ljava_lang_Object_2ZLjava_lang_String_2I(false, '@3', @1);",
1853 smapper.popI(), smapper.pushA(), typeName);
1856 private int generateMultiANewArray(int type, final byte[] byteCodes, int i, final StackMapper smapper) throws IOException {
1857 String typeName = jc.getClassName(type);
1858 int dim = readUByte(byteCodes, ++i);
1859 StringBuilder dims = new StringBuilder();
1861 for (int d = 0; d < dim; d++) {
1863 dims.insert(1, ",");
1865 dims.insert(1, smapper.popI());
1868 emit(out, "var @2 = Array.prototype.multiNewArray__Ljava_lang_Object_2Ljava_lang_String_2_3II('@3', @1, 0);",
1869 dims.toString(), smapper.pushA(), typeName);
1873 private int generateTableSwitch(int i, final byte[] byteCodes, final StackMapper smapper, int topMostLabel) throws IOException {
1874 int table = i / 4 * 4 + 4;
1875 int dflt = i + readInt4(byteCodes, table);
1877 int low = readInt4(byteCodes, table);
1879 int high = readInt4(byteCodes, table);
1881 out.append("switch (").append(smapper.popI()).append(") {\n");
1882 while (low <= high) {
1883 int offset = i + readInt4(byteCodes, table);
1885 out.append(" case " + low).append(":"); goTo(out, i, offset, topMostLabel); out.append('\n');
1888 out.append(" default: ");
1889 goTo(out, i, dflt, topMostLabel);
1895 private int generateLookupSwitch(int i, final byte[] byteCodes, final StackMapper smapper, int topMostLabel) throws IOException {
1896 int table = i / 4 * 4 + 4;
1897 int dflt = i + readInt4(byteCodes, table);
1899 int n = readInt4(byteCodes, table);
1901 out.append("switch (").append(smapper.popI()).append(") {\n");
1903 int cnstnt = readInt4(byteCodes, table);
1905 int offset = i + readInt4(byteCodes, table);
1907 out.append(" case " + cnstnt).append(": "); goTo(out, i, offset, topMostLabel); out.append('\n');
1909 out.append(" default: ");
1910 goTo(out, i, dflt, topMostLabel);
1916 private void generateInstanceOf(int indx, final StackMapper smapper) throws IOException {
1917 final String type = jc.getClassName(indx);
1918 if (!type.startsWith("[")) {
1919 emit(out, "var @2 = @1 != null && @1['$instOf_@3'] ? 1 : 0;",
1920 smapper.popA(), smapper.pushI(),
1921 type.replace('/', '_'));
1923 emit(out, "var @2 = vm.java_lang_Class(false).forName__Ljava_lang_Class_2Ljava_lang_String_2('@3').isInstance__ZLjava_lang_Object_2(@1);",
1924 smapper.popA(), smapper.pushI(),
1930 private void generateCheckcast(int indx, final StackMapper smapper) throws IOException {
1931 final String type = jc.getClassName(indx);
1932 if (!type.startsWith("[")) {
1934 "if (@1 !== null && !@1['$instOf_@2']) throw vm.java_lang_ClassCastException(true);",
1935 smapper.getA(0), type.replace('/', '_'));
1937 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);",
1938 smapper.getA(0), type
1943 private void generateByteCodeComment(int prev, int i, final byte[] byteCodes) throws IOException {
1944 for (int j = prev; j <= i; j++) {
1946 final int cc = readUByte(byteCodes, j);
1947 out.append(Integer.toString(cc));