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 {
31 final ObfuscationDelegate obfuscationDelegate;
33 protected ByteCodeToJavaScript(Appendable out) {
34 this(out, ObfuscationDelegate.NULL);
37 protected ByteCodeToJavaScript(
38 Appendable out, ObfuscationDelegate obfuscationDelegate) {
40 this.obfuscationDelegate = obfuscationDelegate;
43 /* Collects additional required resources.
45 * @param internalClassName classes that were referenced and should be loaded in order the
46 * generated JavaScript code works properly. The names are in internal
47 * JVM form so String is <code>java/lang/String</code>.
49 protected abstract boolean requireReference(String internalClassName);
52 * @param resourcePath name of resources to read
54 protected abstract void requireScript(String resourcePath) throws IOException;
56 /** Allows subclasses to redefine what field a function representing a
57 * class gets assigned. By default it returns the suggested name followed
58 * by <code>" = "</code>;
60 * @param className suggested name of the class
62 /* protected */ String assignClass(String className) {
63 return className + " = ";
65 /* protected */ String accessClass(String classOperation) {
66 return classOperation;
69 abstract String getVMObject();
71 /** Prints out a debug message.
73 * @param msg the message
74 * @return true if the message has been printed
77 boolean debug(String msg) throws IOException {
83 * Converts a given class file to a JavaScript version.
85 * @param classFile input stream with code of the .class file
86 * @return the initialization code for this class, if any. Otherwise <code>null</code>
88 * @throws IOException if something goes wrong during read or write or translating
91 public String compile(InputStream classFile) throws IOException {
92 this.jc = new ClassData(classFile);
93 if (jc.getMajor_version() < 50) {
94 throw new IOException("Can't compile " + jc.getClassName() + ". Class file version " + jc.getMajor_version() + "."
95 + jc.getMinor_version() + " - recompile with -target 1.6 (at least)."
98 byte[] arrData = jc.findAnnotationData(true);
99 String[] arr = findAnnotation(arrData, jc,
100 "org.apidesign.bck2brwsr.core.ExtraJavaScript",
101 "resource", "processByteCode"
104 if (!arr[0].isEmpty()) {
105 requireScript(arr[0]);
107 if ("0".equals(arr[1])) {
111 String[] proto = findAnnotation(arrData, jc,
112 "org.apidesign.bck2brwsr.core.JavaScriptPrototype",
113 "container", "prototype"
115 StringArray toInitilize = new StringArray();
116 final String className = className(jc);
117 out.append("\n\n").append(assignClass(className));
118 out.append("function CLS() {");
119 out.append("\n if (!CLS.$class) {");
121 String sc = jc.getSuperClassName(); // with _
122 out.append("\n var pp = ").
123 append(accessClass(sc.replace('/', '_'))).append("(true);");
124 out.append("\n var p = CLS.prototype = pp;");
125 out.append("\n var c = p;");
126 out.append("\n var sprcls = pp.constructor.$class;");
128 out.append("\n var p = CLS.prototype = ").append(proto[1]).append(";");
129 if (proto[0] == null) {
132 out.append("\n var c = ").append(proto[0]).append(";");
133 out.append("\n var sprcls = null;");
135 for (FieldData v : jc.getFields()) {
137 out.append("\n CLS.").append(v.getName()).append(initField(v));
138 out.append("\n c._").append(v.getName()).append(" = function (v) {")
139 .append(" if (arguments.length == 1) CLS.").append(v.getName())
140 .append(" = v; return CLS.").
141 append(v.getName()).append("; };");
143 out.append("\n c._").append(v.getName()).append(" = function (v) {")
144 .append(" if (arguments.length == 1) this.fld_").
145 append(className).append('_').append(v.getName())
146 .append(" = v; return this.fld_").
147 append(className).append('_').append(v.getName())
151 obfuscationDelegate.exportField(out, "c", "_" + v.getName(), v);
153 for (MethodData m : jc.getMethods()) {
154 byte[] onlyArr = m.findAnnotationData(true);
155 String[] only = findAnnotation(onlyArr, jc,
156 "org.apidesign.bck2brwsr.core.JavaScriptOnly",
160 if (only[0] != null && only[1] != null) {
161 out.append("\n p.").append(only[0]).append(" = ")
162 .append(only[1]).append(";");
171 mn = generateStaticMethod(destObject, m, toInitilize);
173 if (m.isConstructor()) {
175 mn = generateInstanceMethod(destObject, m);
178 mn = generateInstanceMethod(destObject, m);
181 obfuscationDelegate.exportMethod(out, destObject, mn, m);
182 byte[] runAnno = m.findAnnotationData(false);
183 if (runAnno != null) {
184 out.append("\n ").append(destObject).append(".").append(mn).append(".anno = {");
185 generateAnno(jc, out, runAnno);
188 out.append("\n ").append(destObject).append(".").append(mn).append(".access = " + m.getAccess()).append(";");
189 out.append("\n ").append(destObject).append(".").append(mn).append(".cls = CLS;");
191 out.append("\n c.constructor = CLS;");
192 String instOfName = "$instOf_" + className;
193 out.append("\n c.").append(instOfName).append(" = true;");
194 obfuscationDelegate.exportJSProperty(out, "c", instOfName);
195 for (String superInterface : jc.getSuperInterfaces()) {
196 instOfName = "$instOf_" + superInterface.replace('/', '_');
197 out.append("\n c.").append(instOfName).append(" = true;");
198 obfuscationDelegate.exportJSProperty(out, "c", instOfName);
200 out.append("\n CLS.$class = 'temp';");
201 out.append("\n CLS.$class = ");
202 out.append(accessClass("java_lang_Class(true);"));
203 out.append("\n CLS.$class.jvmName = '").append(jc.getClassName()).append("';");
204 out.append("\n CLS.$class.superclass = sprcls;");
205 out.append("\n CLS.$class.access = ").append(jc.getAccessFlags()+";");
206 out.append("\n CLS.$class.cnstr = CLS;");
207 byte[] classAnno = jc.findAnnotationData(false);
208 if (classAnno != null) {
209 out.append("\n CLS.$class.anno = {");
210 generateAnno(jc, out, classAnno);
213 for (String init : toInitilize.toArray()) {
214 out.append("\n ").append(init).append("();");
217 out.append("\n if (arguments.length === 0) {");
218 out.append("\n if (!(this instanceof CLS)) {");
219 out.append("\n return new CLS();");
221 for (FieldData v : jc.getFields()) {
222 byte[] onlyArr = v.findAnnotationData(true);
223 String[] only = findAnnotation(onlyArr, jc,
224 "org.apidesign.bck2brwsr.core.JavaScriptOnly",
228 if (only[0] != null && only[1] != null) {
229 out.append("\n p.").append(only[0]).append(" = ")
230 .append(only[1]).append(";");
235 out.append("\n this.fld_").
236 append(className).append('_').
237 append(v.getName()).append(initField(v));
240 out.append("\n return this;");
242 out.append("\n return arguments[0] ? new CLS() : CLS.prototype;");
245 obfuscationDelegate.exportClass(out, getVMObject(), className, jc);
247 // StringBuilder sb = new StringBuilder();
248 // for (String init : toInitilize.toArray()) {
249 // sb.append("\n").append(init).append("();");
253 private String generateStaticMethod(String destObject, MethodData m, StringArray toInitilize) throws IOException {
254 String jsb = javaScriptBody(destObject, m, true);
258 final String mn = findMethodName(m, new StringBuilder());
259 if (mn.equals("class__V")) {
260 toInitilize.add(accessClass(className(jc)) + "(false)." + mn);
262 generateMethod(destObject, mn, m);
266 private String generateInstanceMethod(String destObject, MethodData m) throws IOException {
267 String jsb = javaScriptBody(destObject, m, false);
271 final String mn = findMethodName(m, new StringBuilder());
272 generateMethod(destObject, mn, m);
276 private void generateMethod(String destObject, String name, MethodData m)
278 final StackMapIterator stackMapIterator = m.createStackMapIterator();
279 TrapDataIterator trap = m.getTrapDataIterator();
280 final LocalsMapper lmapper =
281 new LocalsMapper(stackMapIterator.getArguments());
283 out.append(destObject).append(".").append(name).append(" = function(");
284 lmapper.outputArguments(out, m.isStatic());
285 out.append(") {").append("\n");
287 final byte[] byteCodes = m.getCode();
288 if (byteCodes == null) {
289 out.append(" throw 'no code found for ")
290 .append(jc.getClassName()).append('.')
291 .append(m.getName()).append("';\n");
296 final StackMapper smapper = new StackMapper();
299 out.append(" var ").append(" lcA0 = this;\n");
302 int lastStackFrame = -1;
303 TrapData[] previousTrap = null;
304 boolean wide = false;
306 out.append("\n var gt = 0;\n");
308 int topMostLabel = 0;
309 for (int i = 0; i < byteCodes.length; i++) {
311 stackMapIterator.advanceTo(i);
312 boolean changeInCatch = trap.advanceTo(i);
313 if (changeInCatch || lastStackFrame != stackMapIterator.getFrameIndex()) {
314 if (previousTrap != null) {
315 generateCatch(previousTrap, i, topMostLabel);
319 if (lastStackFrame != stackMapIterator.getFrameIndex()) {
323 if (openBraces > 64) {
324 for (int c = 0; c < 64; c++) {
325 out.append("break;}\n");
331 lastStackFrame = stackMapIterator.getFrameIndex();
332 lmapper.syncWithFrameLocals(stackMapIterator.getFrameLocals());
333 smapper.syncWithFrameStack(stackMapIterator.getFrameStack());
334 out.append(" X_" + i).append(": for (;;) { IF: if (gt <= " + i + ") {\n");
336 changeInCatch = true;
338 debug(" /* " + i + " */ ");
340 if (changeInCatch && trap.useTry()) {
342 previousTrap = trap.current();
344 final int c = readUByte(byteCodes, i);
347 emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(0));
350 emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(0));
353 emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(0));
356 emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(0));
359 emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(0));
362 emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(1));
365 emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(1));
368 emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(1));
371 emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(1));
374 emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(1));
377 emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(2));
380 emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(2));
383 emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(2));
386 emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(2));
389 emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(2));
392 emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(3));
395 emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(3));
398 emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(3));
401 emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(3));
404 emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(3));
408 final int indx = wide ? readUShort(byteCodes, i++)
409 : readUByte(byteCodes, i);
411 emit(out, "var @1 = @2;",
412 smapper.pushI(), lmapper.getI(indx));
417 final int indx = wide ? readUShort(byteCodes, i++)
418 : readUByte(byteCodes, i);
420 emit(out, "var @1 = @2;",
421 smapper.pushL(), lmapper.getL(indx));
426 final int indx = wide ? readUShort(byteCodes, i++)
427 : readUByte(byteCodes, i);
429 emit(out, "var @1 = @2;",
430 smapper.pushF(), lmapper.getF(indx));
435 final int indx = wide ? readUShort(byteCodes, i++)
436 : readUByte(byteCodes, i);
438 emit(out, "var @1 = @2;",
439 smapper.pushD(), lmapper.getD(indx));
444 final int indx = wide ? readUShort(byteCodes, i++)
445 : readUByte(byteCodes, i);
447 emit(out, "var @1 = @2;",
448 smapper.pushA(), lmapper.getA(indx));
453 final int indx = wide ? readUShort(byteCodes, i++)
454 : readUByte(byteCodes, i);
456 emit(out, "var @1 = @2;",
457 lmapper.setI(indx), smapper.popI());
462 final int indx = wide ? readUShort(byteCodes, i++)
463 : readUByte(byteCodes, i);
465 emit(out, "var @1 = @2;",
466 lmapper.setL(indx), smapper.popL());
471 final int indx = wide ? readUShort(byteCodes, i++)
472 : readUByte(byteCodes, i);
474 emit(out, "var @1 = @2;",
475 lmapper.setF(indx), smapper.popF());
480 final int indx = wide ? readUShort(byteCodes, i++)
481 : readUByte(byteCodes, i);
483 emit(out, "var @1 = @2;",
484 lmapper.setD(indx), smapper.popD());
489 final int indx = wide ? readUShort(byteCodes, i++)
490 : readUByte(byteCodes, i);
492 emit(out, "var @1 = @2;",
493 lmapper.setA(indx), smapper.popA());
497 emit(out, "var @1 = @2;", lmapper.setA(0), smapper.popA());
500 emit(out, "var @1 = @2;", lmapper.setI(0), smapper.popI());
503 emit(out, "var @1 = @2;", lmapper.setL(0), smapper.popL());
506 emit(out, "var @1 = @2;", lmapper.setF(0), smapper.popF());
509 emit(out, "var @1 = @2;", lmapper.setD(0), smapper.popD());
512 emit(out, "var @1 = @2;", lmapper.setA(1), smapper.popA());
515 emit(out, "var @1 = @2;", lmapper.setI(1), smapper.popI());
518 emit(out, "var @1 = @2;", lmapper.setL(1), smapper.popL());
521 emit(out, "var @1 = @2;", lmapper.setF(1), smapper.popF());
524 emit(out, "var @1 = @2;", lmapper.setD(1), smapper.popD());
527 emit(out, "var @1 = @2;", lmapper.setA(2), smapper.popA());
530 emit(out, "var @1 = @2;", lmapper.setI(2), smapper.popI());
533 emit(out, "var @1 = @2;", lmapper.setL(2), smapper.popL());
536 emit(out, "var @1 = @2;", lmapper.setF(2), smapper.popF());
539 emit(out, "var @1 = @2;", lmapper.setD(2), smapper.popD());
542 emit(out, "var @1 = @2;", lmapper.setA(3), smapper.popA());
545 emit(out, "var @1 = @2;", lmapper.setI(3), smapper.popI());
548 emit(out, "var @1 = @2;", lmapper.setL(3), smapper.popL());
551 emit(out, "var @1 = @2;", lmapper.setF(3), smapper.popF());
554 emit(out, "var @1 = @2;", lmapper.setD(3), smapper.popD());
557 emit(out, "@1 = @1.add32(@2);", smapper.getI(1), smapper.popI());
560 emit(out, "@1 = @1.add64(@2);", smapper.getL(1), smapper.popL());
563 emit(out, "@1 += @2;", smapper.getF(1), smapper.popF());
566 emit(out, "@1 += @2;", smapper.getD(1), smapper.popD());
569 emit(out, "@1 = @1.sub32(@2);", smapper.getI(1), smapper.popI());
572 emit(out, "@1 = @1.sub64(@2);", smapper.getL(1), smapper.popL());
575 emit(out, "@1 -= @2;", smapper.getF(1), smapper.popF());
578 emit(out, "@1 -= @2;", smapper.getD(1), smapper.popD());
581 emit(out, "@1 = @1.mul32(@2);", smapper.getI(1), smapper.popI());
584 emit(out, "@1 = @1.mul64(@2);", smapper.getL(1), smapper.popL());
587 emit(out, "@1 *= @2;", smapper.getF(1), smapper.popF());
590 emit(out, "@1 *= @2;", smapper.getD(1), smapper.popD());
593 emit(out, "@1 = @1.div32(@2);",
594 smapper.getI(1), smapper.popI());
597 emit(out, "@1 = @1.div64(@2);",
598 smapper.getL(1), smapper.popL());
601 emit(out, "@1 /= @2;", smapper.getF(1), smapper.popF());
604 emit(out, "@1 /= @2;", smapper.getD(1), smapper.popD());
607 emit(out, "@1 = @1.mod32(@2);",
608 smapper.getI(1), smapper.popI());
611 emit(out, "@1 = @1.mod64(@2);",
612 smapper.getL(1), smapper.popL());
615 emit(out, "@1 %= @2;", smapper.getF(1), smapper.popF());
618 emit(out, "@1 %= @2;", smapper.getD(1), smapper.popD());
621 emit(out, "@1 &= @2;", smapper.getI(1), smapper.popI());
624 emit(out, "@1 = @1.and64(@2);", smapper.getL(1), smapper.popL());
627 emit(out, "@1 |= @2;", smapper.getI(1), smapper.popI());
630 emit(out, "@1 = @1.or64(@2);", smapper.getL(1), smapper.popL());
633 emit(out, "@1 ^= @2;", smapper.getI(1), smapper.popI());
636 emit(out, "@1 = @1.xor64(@2);", smapper.getL(1), smapper.popL());
639 emit(out, "@1 = @1.neg32();", smapper.getI(0));
642 emit(out, "@1 = @1.neg64();", smapper.getL(0));
645 emit(out, "@1 = -@1;", smapper.getF(0));
648 emit(out, "@1 = -@1;", smapper.getD(0));
651 emit(out, "@1 <<= @2;", smapper.getI(1), smapper.popI());
654 emit(out, "@1 = @1.shl64(@2);", smapper.getL(1), smapper.popI());
657 emit(out, "@1 >>= @2;", smapper.getI(1), smapper.popI());
660 emit(out, "@1 = @1.shr64(@2);", smapper.getL(1), smapper.popI());
663 emit(out, "@1 >>>= @2;", smapper.getI(1), smapper.popI());
666 emit(out, "@1 = @1.ushr64(@2);", smapper.getL(1), smapper.popI());
670 final int varIndx = wide ? readUShort(byteCodes, i++)
671 : readUByte(byteCodes, i);
673 final int incrBy = wide ? readShort(byteCodes, i++)
677 emit(out, "@1++;", lmapper.getI(varIndx));
679 emit(out, "@1 += @2;",
680 lmapper.getI(varIndx),
681 Integer.toString(incrBy));
686 emit(out, "return;");
689 emit(out, "return @1;", smapper.popI());
692 emit(out, "return @1;", smapper.popL());
695 emit(out, "return @1;", smapper.popF());
698 emit(out, "return @1;", smapper.popD());
701 emit(out, "return @1;", smapper.popA());
704 emit(out, "var @2 = @1;", smapper.popI(), smapper.pushL());
707 emit(out, "var @2 = @1;", smapper.popI(), smapper.pushF());
710 emit(out, "var @2 = @1;", smapper.popI(), smapper.pushD());
713 emit(out, "var @2 = @1.toInt32();", smapper.popL(), smapper.pushI());
717 emit(out, "var @2 = @1.toFP();", smapper.popL(), smapper.pushF());
720 emit(out, "var @2 = @1.toFP();", smapper.popL(), smapper.pushD());
723 emit(out, "var @2 = @1;", smapper.popF(), smapper.pushD());
726 emit(out, "var @2 = @1;", smapper.popD(), smapper.pushF());
729 emit(out, "var @2 = @1.toInt32();",
730 smapper.popF(), smapper.pushI());
733 emit(out, "var @2 = @1.toLong();",
734 smapper.popF(), smapper.pushL());
737 emit(out, "var @2 = @1.toInt32();",
738 smapper.popD(), smapper.pushI());
741 emit(out, "var @2 = @1.toLong();",
742 smapper.popD(), smapper.pushL());
745 emit(out, "var @1 = @1.toInt8();", smapper.getI(0));
748 out.append("{ /* number conversion */ }");
751 emit(out, "var @1 = @1.toInt16();", smapper.getI(0));
753 case opc_aconst_null:
754 emit(out, "var @1 = null;", smapper.pushA());
757 emit(out, "var @1 = -1;", smapper.pushI());
760 emit(out, "var @1 = 0;", smapper.pushI());
763 emit(out, "var @1 = 0;", smapper.pushD());
766 emit(out, "var @1 = 0;", smapper.pushL());
769 emit(out, "var @1 = 0;", smapper.pushF());
772 emit(out, "var @1 = 1;", smapper.pushI());
775 emit(out, "var @1 = 1;", smapper.pushL());
778 emit(out, "var @1 = 1;", smapper.pushF());
781 emit(out, "var @1 = 1;", smapper.pushD());
784 emit(out, "var @1 = 2;", smapper.pushI());
787 emit(out, "var @1 = 2;", smapper.pushF());
790 emit(out, "var @1 = 3;", smapper.pushI());
793 emit(out, "var @1 = 4;", smapper.pushI());
796 emit(out, "var @1 = 5;", smapper.pushI());
799 int indx = readUByte(byteCodes, ++i);
800 String v = encodeConstant(indx);
801 int type = VarType.fromConstantType(jc.getTag(indx));
802 emit(out, "var @1 = @2;", smapper.pushT(type), v);
807 int indx = readIntArg(byteCodes, i);
809 String v = encodeConstant(indx);
810 int type = VarType.fromConstantType(jc.getTag(indx));
811 if (type == VarType.LONG) {
812 final Long lv = new Long(v);
813 final int low = (int)(lv.longValue() & 0xFFFFFFFF);
814 final int hi = (int)(lv.longValue() >> 32);
815 emit(out, "var @1 = 0x@3.next32(0x@2);", smapper.pushL(),
816 Integer.toHexString(low), Integer.toHexString(hi));
818 emit(out, "var @1 = @2;", smapper.pushT(type), v);
823 emit(out, "var @3 = @2.compare64(@1);",
824 smapper.popL(), smapper.popL(), smapper.pushI());
828 emit(out, "var @3 = (@2 == @1) ? 0 : ((@2 < @1) ? -1 : 1);",
829 smapper.popF(), smapper.popF(), smapper.pushI());
833 emit(out, "var @3 = (@2 == @1) ? 0 : ((@2 < @1) ? -1 : 1);",
834 smapper.popD(), smapper.popD(), smapper.pushI());
837 i = generateIf(byteCodes, i, smapper.popA(), smapper.popA(),
838 "===", topMostLabel);
841 i = generateIf(byteCodes, i, smapper.popA(), smapper.popA(),
842 "!==", topMostLabel);
845 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
849 int indx = i + readIntArg(byteCodes, i);
850 emitIf(out, "if (@1 == 0) ",
851 smapper.popI(), i, indx, topMostLabel);
856 int indx = i + readIntArg(byteCodes, i);
857 emitIf(out, "if (@1 != 0) ",
858 smapper.popI(), i, indx, topMostLabel);
863 int indx = i + readIntArg(byteCodes, i);
864 emitIf(out, "if (@1 < 0) ",
865 smapper.popI(), i, indx, topMostLabel);
870 int indx = i + readIntArg(byteCodes, i);
871 emitIf(out, "if (@1 <= 0) ",
872 smapper.popI(), i, indx, topMostLabel);
877 int indx = i + readIntArg(byteCodes, i);
878 emitIf(out, "if (@1 > 0) ",
879 smapper.popI(), i, indx, topMostLabel);
884 int indx = i + readIntArg(byteCodes, i);
885 emitIf(out, "if (@1 >= 0) ",
886 smapper.popI(), i, indx, topMostLabel);
890 case opc_ifnonnull: {
891 int indx = i + readIntArg(byteCodes, i);
892 emitIf(out, "if (@1 !== null) ",
893 smapper.popA(), i, indx, topMostLabel);
898 int indx = i + readIntArg(byteCodes, i);
899 emitIf(out, "if (@1 === null) ",
900 smapper.popA(), i, indx, topMostLabel);
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 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
921 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
925 int indx = i + readIntArg(byteCodes, i);
926 goTo(out, i, indx, topMostLabel);
930 case opc_lookupswitch: {
931 i = generateLookupSwitch(i, byteCodes, smapper, topMostLabel);
934 case opc_tableswitch: {
935 i = generateTableSwitch(i, byteCodes, smapper, topMostLabel);
938 case opc_invokeinterface: {
939 i = invokeVirtualMethod(byteCodes, i, smapper) + 2;
942 case opc_invokevirtual:
943 i = invokeVirtualMethod(byteCodes, i, smapper);
945 case opc_invokespecial:
946 i = invokeStaticMethod(byteCodes, i, smapper, false);
948 case opc_invokestatic:
949 i = invokeStaticMethod(byteCodes, i, smapper, true);
952 int indx = readIntArg(byteCodes, i);
953 String ci = jc.getClassName(indx);
954 emit(out, "var @1 = new @2;",
955 smapper.pushA(), accessClass(ci.replace('/', '_')));
961 int atype = readUByte(byteCodes, ++i);
962 generateNewArray(atype, smapper);
964 case opc_anewarray: {
965 int type = readIntArg(byteCodes, i);
967 generateANewArray(type, smapper);
970 case opc_multianewarray: {
971 int type = readIntArg(byteCodes, i);
973 i = generateMultiANewArray(type, byteCodes, i, smapper);
976 case opc_arraylength:
977 emit(out, "var @2 = @1.length;",
978 smapper.popA(), smapper.pushI());
981 emit(out, "@3.at(@2, @1);",
982 smapper.popL(), smapper.popI(), smapper.popA());
985 emit(out, "@3.at(@2, @1);",
986 smapper.popF(), smapper.popI(), smapper.popA());
989 emit(out, "@3.at(@2, @1);",
990 smapper.popD(), smapper.popI(), smapper.popA());
993 emit(out, "@3.at(@2, @1);",
994 smapper.popA(), smapper.popI(), smapper.popA());
1000 emit(out, "@3.at(@2, @1);",
1001 smapper.popI(), smapper.popI(), smapper.popA());
1004 emit(out, "var @3 = @2.at(@1);",
1005 smapper.popI(), smapper.popA(), smapper.pushL());
1008 emit(out, "var @3 = @2.at(@1);",
1009 smapper.popI(), smapper.popA(), smapper.pushF());
1012 emit(out, "var @3 = @2.at(@1);",
1013 smapper.popI(), smapper.popA(), smapper.pushD());
1016 emit(out, "var @3 = @2.at(@1);",
1017 smapper.popI(), smapper.popA(), smapper.pushA());
1023 emit(out, "var @3 = @2.at(@1);",
1024 smapper.popI(), smapper.popA(), smapper.pushI());
1032 final Variable v = smapper.get(0);
1033 emit(out, "var @1 = @2;", smapper.pushT(v.getType()), v);
1037 final Variable vi1 = smapper.get(0);
1039 if (vi1.isCategory2()) {
1040 emit(out, "var @1 = @2;",
1041 smapper.pushT(vi1.getType()), vi1);
1043 final Variable vi2 = smapper.get(1);
1044 emit(out, "var @1 = @2, @3 = @4;",
1045 smapper.pushT(vi2.getType()), vi2,
1046 smapper.pushT(vi1.getType()), vi1);
1051 final Variable vi1 = smapper.pop();
1052 final Variable vi2 = smapper.pop();
1053 final Variable vo3 = smapper.pushT(vi1.getType());
1054 final Variable vo2 = smapper.pushT(vi2.getType());
1055 final Variable vo1 = smapper.pushT(vi1.getType());
1057 emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
1058 vo1, vi1, vo2, vi2, vo3, vo1);
1062 final Variable vi1 = smapper.pop();
1063 final Variable vi2 = smapper.pop();
1065 if (vi1.isCategory2()) {
1066 final Variable vo3 = smapper.pushT(vi1.getType());
1067 final Variable vo2 = smapper.pushT(vi2.getType());
1068 final Variable vo1 = smapper.pushT(vi1.getType());
1070 emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
1071 vo1, vi1, vo2, vi2, vo3, vo1);
1073 final Variable vi3 = smapper.pop();
1074 final Variable vo5 = smapper.pushT(vi2.getType());
1075 final Variable vo4 = smapper.pushT(vi1.getType());
1076 final Variable vo3 = smapper.pushT(vi3.getType());
1077 final Variable vo2 = smapper.pushT(vi2.getType());
1078 final Variable vo1 = smapper.pushT(vi1.getType());
1080 emit(out, "var @1 = @2, @3 = @4, @5 = @6,",
1081 vo1, vi1, vo2, vi2, vo3, vi3);
1082 emit(out, " @1 = @2, @3 = @4;",
1083 vo4, vo1, vo5, vo2);
1088 final Variable vi1 = smapper.pop();
1089 final Variable vi2 = smapper.pop();
1091 if (vi2.isCategory2()) {
1092 final Variable vo3 = smapper.pushT(vi1.getType());
1093 final Variable vo2 = smapper.pushT(vi2.getType());
1094 final Variable vo1 = smapper.pushT(vi1.getType());
1096 emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
1097 vo1, vi1, vo2, vi2, vo3, vo1);
1099 final Variable vi3 = smapper.pop();
1100 final Variable vo4 = smapper.pushT(vi1.getType());
1101 final Variable vo3 = smapper.pushT(vi3.getType());
1102 final Variable vo2 = smapper.pushT(vi2.getType());
1103 final Variable vo1 = smapper.pushT(vi1.getType());
1105 emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8;",
1106 vo1, vi1, vo2, vi2, vo3, vi3, vo4, vo1);
1111 final Variable vi1 = smapper.pop();
1112 final Variable vi2 = smapper.pop();
1114 if (vi1.isCategory2()) {
1115 if (vi2.isCategory2()) {
1116 final Variable vo3 = smapper.pushT(vi1.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, vo1);
1123 final Variable vi3 = smapper.pop();
1124 final Variable vo4 = smapper.pushT(vi1.getType());
1125 final Variable vo3 = smapper.pushT(vi3.getType());
1126 final Variable vo2 = smapper.pushT(vi2.getType());
1127 final Variable vo1 = smapper.pushT(vi1.getType());
1129 emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8;",
1130 vo1, vi1, vo2, vi2, vo3, vi3, vo4, vo1);
1133 final Variable vi3 = smapper.pop();
1135 if (vi3.isCategory2()) {
1136 final Variable vo5 = smapper.pushT(vi2.getType());
1137 final Variable vo4 = smapper.pushT(vi1.getType());
1138 final Variable vo3 = smapper.pushT(vi3.getType());
1139 final Variable vo2 = smapper.pushT(vi2.getType());
1140 final Variable vo1 = smapper.pushT(vi1.getType());
1142 emit(out, "var @1 = @2, @3 = @4, @5 = @6,",
1143 vo1, vi1, vo2, vi2, vo3, vi3);
1144 emit(out, " @1 = @2, @3 = @4;",
1145 vo4, vo1, vo5, vo2);
1147 final Variable vi4 = smapper.pop();
1148 final Variable vo6 = smapper.pushT(vi2.getType());
1149 final Variable vo5 = smapper.pushT(vi1.getType());
1150 final Variable vo4 = smapper.pushT(vi4.getType());
1151 final Variable vo3 = smapper.pushT(vi3.getType());
1152 final Variable vo2 = smapper.pushT(vi2.getType());
1153 final Variable vo1 = smapper.pushT(vi1.getType());
1155 emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8,",
1156 vo1, vi1, vo2, vi2, vo3, vi3, vo4, vi4);
1157 emit(out, " @1 = @2, @3 = @4;",
1158 vo5, vo1, vo6, vo2);
1164 final Variable vi1 = smapper.get(0);
1165 final Variable vi2 = smapper.get(1);
1167 if (vi1.getType() == vi2.getType()) {
1168 final Variable tmp = smapper.pushT(vi1.getType());
1170 emit(out, "var @1 = @2, @2 = @3, @3 = @1;",
1175 smapper.pushT(vi1.getType());
1176 smapper.pushT(vi2.getType());
1181 emit(out, "var @1 = @2;",
1182 smapper.pushI(), Integer.toString(byteCodes[++i]));
1185 emit(out, "var @1 = @2;",
1187 Integer.toString(readIntArg(byteCodes, i)));
1190 case opc_getfield: {
1191 int indx = readIntArg(byteCodes, i);
1192 String[] fi = jc.getFieldInfoName(indx);
1193 final int type = VarType.fromFieldType(fi[2].charAt(0));
1194 final String mangleClass = mangleSig(fi[0]);
1195 final String mangleClassAccess = accessClass(mangleClass);
1196 emit(out, "var @2 = @4(false)._@3.call(@1);",
1198 smapper.pushT(type), fi[1], mangleClassAccess
1203 case opc_putfield: {
1204 int indx = readIntArg(byteCodes, i);
1205 String[] fi = jc.getFieldInfoName(indx);
1206 final int type = VarType.fromFieldType(fi[2].charAt(0));
1207 final String mangleClass = mangleSig(fi[0]);
1208 final String mangleClassAccess = accessClass(mangleClass);
1209 emit(out, "@4(false)._@3.call(@2, @1);",
1211 smapper.popA(), fi[1],
1217 case opc_getstatic: {
1218 int indx = readIntArg(byteCodes, i);
1219 String[] fi = jc.getFieldInfoName(indx);
1220 final int type = VarType.fromFieldType(fi[2].charAt(0));
1221 emit(out, "var @1 = @2(false)._@3();",
1222 smapper.pushT(type),
1223 accessClass(fi[0].replace('/', '_')), fi[1]);
1225 addReference(fi[0]);
1228 case opc_putstatic: {
1229 int indx = readIntArg(byteCodes, i);
1230 String[] fi = jc.getFieldInfoName(indx);
1231 final int type = VarType.fromFieldType(fi[2].charAt(0));
1232 emit(out, "@1(false)._@2(@3);",
1233 accessClass(fi[0].replace('/', '_')), fi[1],
1234 smapper.popT(type));
1236 addReference(fi[0]);
1239 case opc_checkcast: {
1240 int indx = readIntArg(byteCodes, i);
1241 generateCheckcast(indx, smapper);
1245 case opc_instanceof: {
1246 int indx = readIntArg(byteCodes, i);
1247 generateInstanceOf(indx, smapper);
1252 final Variable v = smapper.popA();
1255 emit(out, "{ var @1 = @2; throw @2; }",
1256 smapper.pushA(), v);
1260 case opc_monitorenter: {
1261 out.append("/* monitor enter */");
1266 case opc_monitorexit: {
1267 out.append("/* monitor exit */");
1278 emit(out, "throw 'unknown bytecode @1';",
1279 Integer.toString(c));
1283 generateByteCodeComment(prev, i, byteCodes);
1287 if (previousTrap != null) {
1288 generateCatch(previousTrap, byteCodes.length, topMostLabel);
1290 out.append("\n }\n");
1291 while (openBraces-- > 0) {
1297 private int generateIf(byte[] byteCodes, int i, final Variable v2, final Variable v1, final String test, int topMostLabel) throws IOException {
1298 int indx = i + readIntArg(byteCodes, i);
1299 out.append("if (").append(v1)
1300 .append(' ').append(test).append(' ')
1301 .append(v2).append(") ");
1302 goTo(out, i, indx, topMostLabel);
1306 private int readIntArg(byte[] byteCodes, int offsetInstruction) {
1307 final int indxHi = byteCodes[offsetInstruction + 1] << 8;
1308 final int indxLo = byteCodes[offsetInstruction + 2];
1309 return (indxHi & 0xffffff00) | (indxLo & 0xff);
1311 private int readInt4(byte[] byteCodes, int offset) {
1312 final int d = byteCodes[offset + 0] << 24;
1313 final int c = byteCodes[offset + 1] << 16;
1314 final int b = byteCodes[offset + 2] << 8;
1315 final int a = byteCodes[offset + 3];
1316 return (d & 0xff000000) | (c & 0xff0000) | (b & 0xff00) | (a & 0xff);
1318 private int readUByte(byte[] byteCodes, int offset) {
1319 return byteCodes[offset] & 0xff;
1322 private int readUShort(byte[] byteCodes, int offset) {
1323 return ((byteCodes[offset] & 0xff) << 8)
1324 | (byteCodes[offset + 1] & 0xff);
1327 private int readShort(byte[] byteCodes, int offset) {
1328 return (byteCodes[offset] << 8)
1329 | (byteCodes[offset + 1] & 0xff);
1332 private static void countArgs(String descriptor, char[] returnType, StringBuilder sig, StringBuilder cnt) {
1334 Boolean count = null;
1335 boolean array = false;
1337 int firstPos = sig.length();
1338 while (i < descriptor.length()) {
1339 char ch = descriptor.charAt(i++);
1360 if (ch == 'J' || ch == 'D') {
1366 sig.insert(firstPos, ch);
1368 returnType[0] = '[';
1369 sig.insert(firstPos, "_3");
1378 returnType[0] = 'V';
1379 sig.insert(firstPos, 'V');
1382 int next = descriptor.indexOf(';', i);
1383 String realSig = mangleSig(descriptor, i - 1, next + 1);
1388 sig.append(realSig);
1391 sig.insert(firstPos, realSig);
1393 sig.insert(firstPos, "_3");
1395 returnType[0] = 'L';
1404 throw new IllegalStateException("Invalid char: " + ch);
1409 static String mangleSig(String sig) {
1410 return mangleSig(sig, 0, sig.length());
1413 private static String mangleSig(String txt, int first, int last) {
1414 StringBuilder sb = new StringBuilder();
1415 for (int i = first; i < last; i++) {
1416 final char ch = txt.charAt(i);
1418 case '/': sb.append('_'); break;
1419 case '_': sb.append("_1"); break;
1420 case ';': sb.append("_2"); break;
1421 case '[': sb.append("_3"); break;
1422 default: sb.append(ch); break;
1425 return sb.toString();
1428 private static String findMethodName(MethodData m, StringBuilder cnt) {
1429 StringBuilder name = new StringBuilder();
1430 if ("<init>".equals(m.getName())) { // NOI18N
1431 name.append("cons"); // NOI18N
1432 } else if ("<clinit>".equals(m.getName())) { // NOI18N
1433 name.append("class"); // NOI18N
1435 name.append(m.getName());
1438 countArgs(m.getInternalSig(), new char[1], name, cnt);
1439 return name.toString();
1442 static String findMethodName(String[] mi, StringBuilder cnt, char[] returnType) {
1443 StringBuilder name = new StringBuilder();
1444 String descr = mi[2];//mi.getDescriptor();
1446 if ("<init>".equals(nm)) { // NOI18N
1447 name.append("cons"); // NOI18N
1451 countArgs(descr, returnType, name, cnt);
1452 return name.toString();
1455 private int invokeStaticMethod(byte[] byteCodes, int i, final StackMapper mapper, boolean isStatic)
1456 throws IOException {
1457 int methodIndex = readIntArg(byteCodes, i);
1458 String[] mi = jc.getFieldInfoName(methodIndex);
1459 char[] returnType = { 'V' };
1460 StringBuilder cnt = new StringBuilder();
1461 String mn = findMethodName(mi, cnt, returnType);
1463 final int numArguments = isStatic ? cnt.length() : cnt.length() + 1;
1464 final Variable[] vars = new Variable[numArguments];
1466 for (int j = numArguments - 1; j >= 0; --j) {
1467 vars[j] = mapper.pop();
1470 if (returnType[0] != 'V') {
1472 .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
1476 final String in = mi[0];
1477 out.append(accessClass(in.replace('/', '_')));
1478 out.append("(false).");
1479 if (mn.startsWith("cons_")) {
1480 out.append("constructor.");
1486 out.append(".call(");
1488 if (numArguments > 0) {
1489 out.append(vars[0]);
1490 for (int j = 1; j < numArguments; ++j) {
1492 out.append(vars[j]);
1500 private int invokeVirtualMethod(byte[] byteCodes, int i, final StackMapper mapper)
1501 throws IOException {
1502 int methodIndex = readIntArg(byteCodes, i);
1503 String[] mi = jc.getFieldInfoName(methodIndex);
1504 char[] returnType = { 'V' };
1505 StringBuilder cnt = new StringBuilder();
1506 String mn = findMethodName(mi, cnt, returnType);
1508 final int numArguments = cnt.length() + 1;
1509 final Variable[] vars = new Variable[numArguments];
1511 for (int j = numArguments - 1; j >= 0; --j) {
1512 vars[j] = mapper.pop();
1515 if (returnType[0] != 'V') {
1517 .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
1521 out.append(vars[0]).append('.');
1525 for (int j = 1; j < numArguments; ++j) {
1527 out.append(vars[j]);
1535 private void addReference(String cn) throws IOException {
1536 if (requireReference(cn)) {
1537 debug(" /* needs " + cn + " */");
1541 private void outType(String d, StringBuilder out) {
1543 while (d.charAt(0) == '[') {
1547 if (d.charAt(0) == 'L') {
1548 assert d.charAt(d.length() - 1) == ';';
1549 out.append(d.replace('/', '_').substring(0, d.length() - 1));
1555 private String encodeConstant(int entryIndex) throws IOException {
1556 String[] classRef = { null };
1557 String s = jc.stringValue(entryIndex, classRef);
1558 if (classRef[0] != null) {
1559 if (classRef[0].startsWith("[")) {
1560 s = accessClass("java_lang_Class") + "(false).forName__Ljava_lang_Class_2Ljava_lang_String_2('" + classRef[0] + "');";
1562 addReference(classRef[0]);
1563 s = accessClass(s.replace('/', '_')) + "(false).constructor.$class";
1569 private String javaScriptBody(String destObject, MethodData m, boolean isStatic) throws IOException {
1570 byte[] arr = m.findAnnotationData(true);
1574 final String jvmType = "Lorg/apidesign/bck2brwsr/core/JavaScriptBody;";
1575 class P extends AnnotationParser {
1581 String[] args = new String[30];
1585 protected void visitAttr(String type, String attr, String at, String value) {
1586 if (type.equals(jvmType)) {
1587 if ("body".equals(attr)) {
1589 } else if ("args".equals(attr)) {
1590 args[cnt++] = value;
1592 throw new IllegalArgumentException(attr);
1599 if (p.body == null) {
1602 StringBuilder cnt = new StringBuilder();
1603 final String mn = findMethodName(m, cnt);
1604 out.append(destObject).append(".").append(mn);
1605 out.append(" = function(");
1608 for (int i = 0; i < cnt.length(); i++) {
1610 space = outputArg(out, p.args, index);
1613 out.append(") {").append("\n");
1615 out.append("\n}\n");
1618 private static String className(ClassData jc) {
1619 //return jc.getName().getInternalName().replace('/', '_');
1620 return jc.getClassName().replace('/', '_');
1623 private static String[] findAnnotation(
1624 byte[] arr, ClassData cd, final String className,
1625 final String... attrNames
1626 ) throws IOException {
1630 final String[] values = new String[attrNames.length];
1631 final boolean[] found = { false };
1632 final String jvmType = "L" + className.replace('.', '/') + ";";
1633 AnnotationParser ap = new AnnotationParser(false, true) {
1635 protected void visitAttr(String type, String attr, String at, String value) {
1636 if (type.equals(jvmType)) {
1638 for (int i = 0; i < attrNames.length; i++) {
1639 if (attrNames[i].equals(attr)) {
1648 return found[0] ? values : null;
1651 private CharSequence initField(FieldData v) {
1652 final String is = v.getInternalSig();
1653 if (is.length() == 1) {
1654 switch (is.charAt(0)) {
1660 case 'I': return " = 0;";
1662 case 'D': return " = 0.0;";
1664 throw new IllegalStateException(is);
1670 private void generateAnno(ClassData cd, final Appendable out, byte[] data) throws IOException {
1671 AnnotationParser ap = new AnnotationParser(true, false) {
1672 int[] cnt = new int[32];
1676 protected void visitAnnotationStart(String attrType, boolean top) throws IOException {
1677 final String slashType = attrType.substring(1, attrType.length() - 1);
1678 requireReference(slashType);
1680 if (cnt[depth]++ > 0) {
1684 out.append('"').append(attrType).append("\" : ");
1691 protected void visitAnnotationEnd(String type, boolean top) throws IOException {
1692 out.append("\n}\n");
1697 protected void visitValueStart(String attrName, char type) throws IOException {
1698 if (cnt[depth]++ > 0) {
1702 if (attrName != null) {
1703 out.append(attrName).append(" : ");
1711 protected void visitValueEnd(String attrName, char type) throws IOException {
1719 protected void visitAttr(String type, String attr, String attrType, String value)
1720 throws IOException {
1721 if (attr == null && value == null) {
1728 protected void visitEnumAttr(String type, String attr, String attrType, String value)
1729 throws IOException {
1730 final String slashType = attrType.substring(1, attrType.length() - 1);
1731 requireReference(slashType);
1733 out.append(accessClass(slashType.replace('/', '_')))
1734 .append("(false).constructor.").append(value);
1740 private static String outputArg(Appendable out, String[] args, int indx) throws IOException {
1741 final String name = args[indx];
1745 if (name.contains(",")) {
1746 throw new IOException("Wrong parameter with ',': " + name);
1752 private static void emit(final Appendable out,
1753 final String format,
1754 final CharSequence... params) throws IOException {
1755 final int length = format.length();
1758 int paramOffset = format.indexOf('@');
1759 while ((paramOffset != -1) && (paramOffset < (length - 1))) {
1760 final char paramChar = format.charAt(paramOffset + 1);
1761 if ((paramChar >= '1') && (paramChar <= '9')) {
1762 final int paramIndex = paramChar - '0' - 1;
1764 out.append(format, processed, paramOffset);
1765 out.append(params[paramIndex]);
1768 processed = paramOffset + 1;
1771 paramOffset = format.indexOf('@', paramOffset + 1);
1774 out.append(format, processed, length);
1777 private void generateCatch(TrapData[] traps, int current, int topMostLabel) throws IOException {
1778 out.append("} catch (e) {\n");
1780 for (TrapData e : traps) {
1784 if (e.catch_cpx != 0) { //not finally
1785 final String classInternalName = jc.getClassName(e.catch_cpx);
1786 addReference(classInternalName);
1787 if ("java/lang/Throwable".equals(classInternalName)) {
1788 out.append("if (e.$instOf_java_lang_Throwable) {");
1789 out.append(" var stA0 = e;");
1790 out.append("} else {");
1791 out.append(" var stA0 = vm.java_lang_Throwable(true);");
1792 out.append(" vm.java_lang_Throwable.cons__VLjava_lang_String_2.call(stA0, e.toString());");
1794 goTo(out, current, e.handler_pc, topMostLabel);
1796 out.append("if (e.$instOf_" + classInternalName.replace('/', '_') + ") {");
1797 out.append("var stA0 = e;");
1798 goTo(out, current, e.handler_pc, topMostLabel);
1802 finallyPC = e.handler_pc;
1805 if (finallyPC == -1) {
1806 out.append("throw e;");
1808 out.append("var stA0 = e;");
1809 goTo(out, current, finallyPC, topMostLabel);
1814 private static void goTo(Appendable out, int current, int to, int canBack) throws IOException {
1817 out.append("{ gt = 0; continue X_" + to + "; }");
1819 out.append("{ gt = " + to + "; continue X_0; }");
1822 out.append("{ gt = " + to + "; break IF; }");
1826 private static void emitIf(
1827 Appendable out, String pattern, Variable param,
1828 int current, int to, int canBack
1829 ) throws IOException {
1830 emit(out, pattern, param);
1831 goTo(out, current, to, canBack);
1834 private void generateNewArray(int atype, final StackMapper smapper) throws IOException, IllegalStateException {
1837 case 4: jvmType = "[Z"; break;
1838 case 5: jvmType = "[C"; break;
1839 case 6: jvmType = "[F"; break;
1840 case 7: jvmType = "[D"; break;
1841 case 8: jvmType = "[B"; break;
1842 case 9: jvmType = "[S"; break;
1843 case 10: jvmType = "[I"; break;
1844 case 11: jvmType = "[J"; break;
1845 default: throw new IllegalStateException("Array type: " + atype);
1847 emit(out, "var @2 = Array.prototype.newArray__Ljava_lang_Object_2ZLjava_lang_String_2I(true, '@3', @1);",
1848 smapper.popI(), smapper.pushA(), jvmType);
1851 private void generateANewArray(int type, final StackMapper smapper) throws IOException {
1852 String typeName = jc.getClassName(type);
1853 if (typeName.startsWith("[")) {
1854 typeName = "[" + typeName;
1856 typeName = "[L" + typeName + ";";
1858 emit(out, "var @2 = Array.prototype.newArray__Ljava_lang_Object_2ZLjava_lang_String_2I(false, '@3', @1);",
1859 smapper.popI(), smapper.pushA(), typeName);
1862 private int generateMultiANewArray(int type, final byte[] byteCodes, int i, final StackMapper smapper) throws IOException {
1863 String typeName = jc.getClassName(type);
1864 int dim = readUByte(byteCodes, ++i);
1865 StringBuilder dims = new StringBuilder();
1867 for (int d = 0; d < dim; d++) {
1869 dims.insert(1, ",");
1871 dims.insert(1, smapper.popI());
1874 emit(out, "var @2 = Array.prototype.multiNewArray__Ljava_lang_Object_2Ljava_lang_String_2_3II('@3', @1, 0);",
1875 dims.toString(), smapper.pushA(), typeName);
1879 private int generateTableSwitch(int i, final byte[] byteCodes, final StackMapper smapper, int topMostLabel) throws IOException {
1880 int table = i / 4 * 4 + 4;
1881 int dflt = i + readInt4(byteCodes, table);
1883 int low = readInt4(byteCodes, table);
1885 int high = readInt4(byteCodes, table);
1887 out.append("switch (").append(smapper.popI()).append(") {\n");
1888 while (low <= high) {
1889 int offset = i + readInt4(byteCodes, table);
1891 out.append(" case " + low).append(":"); goTo(out, i, offset, topMostLabel); out.append('\n');
1894 out.append(" default: ");
1895 goTo(out, i, dflt, topMostLabel);
1901 private int generateLookupSwitch(int i, final byte[] byteCodes, final StackMapper smapper, int topMostLabel) throws IOException {
1902 int table = i / 4 * 4 + 4;
1903 int dflt = i + readInt4(byteCodes, table);
1905 int n = readInt4(byteCodes, table);
1907 out.append("switch (").append(smapper.popI()).append(") {\n");
1909 int cnstnt = readInt4(byteCodes, table);
1911 int offset = i + readInt4(byteCodes, table);
1913 out.append(" case " + cnstnt).append(": "); goTo(out, i, offset, topMostLabel); out.append('\n');
1915 out.append(" default: ");
1916 goTo(out, i, dflt, topMostLabel);
1922 private void generateInstanceOf(int indx, final StackMapper smapper) throws IOException {
1923 final String type = jc.getClassName(indx);
1924 if (!type.startsWith("[")) {
1925 emit(out, "var @2 = @1 != null && @1.$instOf_@3 ? 1 : 0;",
1926 smapper.popA(), smapper.pushI(),
1927 type.replace('/', '_'));
1929 emit(out, "var @2 = vm.java_lang_Class(false).forName__Ljava_lang_Class_2Ljava_lang_String_2('@3').isInstance__ZLjava_lang_Object_2(@1);",
1930 smapper.popA(), smapper.pushI(),
1936 private void generateCheckcast(int indx, final StackMapper smapper) throws IOException {
1937 final String type = jc.getClassName(indx);
1938 if (!type.startsWith("[")) {
1940 "if (@1 !== null && !@1.$instOf_@2) throw {};",
1941 smapper.getA(0), type.replace('/', '_'));
1943 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);",
1944 smapper.getA(0), type
1949 private void generateByteCodeComment(int prev, int i, final byte[] byteCodes) throws IOException {
1950 for (int j = prev; j <= i; j++) {
1952 final int cc = readUByte(byteCodes, j);
1953 out.append(Integer.toString(cc));