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 String instOfName = "$instOf_" + className;
189 out.append("\n c.").append(instOfName).append(" = true;");
190 // obfuscationDelegate.exportJSProperty(out, "c", instOfName);
191 for (String superInterface : jc.getSuperInterfaces()) {
192 instOfName = "$instOf_" + superInterface.replace('/', '_');
193 out.append("\n c.").append(instOfName).append(" = true;");
194 // obfuscationDelegate.exportJSProperty(out, "c", instOfName);
196 out.append("\n CLS.$class = 'temp';");
197 out.append("\n CLS.$class = ");
198 out.append(accessClass("java_lang_Class(true);"));
199 out.append("\n CLS.$class.jvmName = '").append(jc.getClassName()).append("';");
200 out.append("\n CLS.$class.superclass = sprcls;");
201 out.append("\n CLS.$class.access = ").append(jc.getAccessFlags()+";");
202 out.append("\n CLS.$class.cnstr = CLS;");
203 byte[] classAnno = jc.findAnnotationData(false);
204 if (classAnno != null) {
205 out.append("\n CLS.$class.anno = {");
206 generateAnno(jc, out, classAnno);
209 for (String init : toInitilize.toArray()) {
210 out.append("\n ").append(init).append("();");
213 out.append("\n if (arguments.length === 0) {");
214 out.append("\n if (!(this instanceof CLS)) {");
215 out.append("\n return new CLS();");
217 for (FieldData v : jc.getFields()) {
218 byte[] onlyArr = v.findAnnotationData(true);
219 String[] only = findAnnotation(onlyArr, jc,
220 "org.apidesign.bck2brwsr.core.JavaScriptOnly",
224 if (only[0] != null && only[1] != null) {
225 out.append("\n p.").append(only[0]).append(" = ")
226 .append(only[1]).append(";");
231 out.append("\n this.fld_").
232 append(className).append('_').
233 append(v.getName()).append(initField(v));
236 out.append("\n return this;");
238 out.append("\n return arguments[0] ? new CLS() : CLS.prototype;");
241 declaredClass(jc, className);
243 // StringBuilder sb = new StringBuilder();
244 // for (String init : toInitilize.toArray()) {
245 // sb.append("\n").append(init).append("();");
249 private String generateStaticMethod(String destObject, MethodData m, StringArray toInitilize) throws IOException {
250 String jsb = javaScriptBody(destObject, m, true);
254 final String mn = findMethodName(m, new StringBuilder());
255 if (mn.equals("class__V")) {
256 toInitilize.add(accessClass(className(jc)) + "(false)." + mn);
258 generateMethod(destObject, mn, m);
262 private String generateInstanceMethod(String destObject, MethodData m) throws IOException {
263 String jsb = javaScriptBody(destObject, m, false);
267 final String mn = findMethodName(m, new StringBuilder());
268 generateMethod(destObject, mn, m);
272 private void generateMethod(String destObject, String name, MethodData m)
274 final StackMapIterator stackMapIterator = m.createStackMapIterator();
275 TrapDataIterator trap = m.getTrapDataIterator();
276 final LocalsMapper lmapper =
277 new LocalsMapper(stackMapIterator.getArguments());
279 out.append(destObject).append(".").append(name).append(" = function(");
280 lmapper.outputArguments(out, m.isStatic());
281 out.append(") {").append("\n");
283 final byte[] byteCodes = m.getCode();
284 if (byteCodes == null) {
285 out.append(" throw 'no code found for ")
286 .append(jc.getClassName()).append('.')
287 .append(m.getName()).append("';\n");
292 final StackMapper smapper = new StackMapper();
295 out.append(" var ").append(" lcA0 = this;\n");
298 int lastStackFrame = -1;
299 TrapData[] previousTrap = null;
300 boolean wide = false;
302 out.append("\n var gt = 0;\n");
304 int topMostLabel = 0;
305 for (int i = 0; i < byteCodes.length; i++) {
307 stackMapIterator.advanceTo(i);
308 boolean changeInCatch = trap.advanceTo(i);
309 if (changeInCatch || lastStackFrame != stackMapIterator.getFrameIndex()) {
310 if (previousTrap != null) {
311 generateCatch(previousTrap, i, topMostLabel);
315 if (lastStackFrame != stackMapIterator.getFrameIndex()) {
319 if (openBraces > 64) {
320 for (int c = 0; c < 64; c++) {
321 out.append("break;}\n");
327 lastStackFrame = stackMapIterator.getFrameIndex();
328 lmapper.syncWithFrameLocals(stackMapIterator.getFrameLocals());
329 smapper.syncWithFrameStack(stackMapIterator.getFrameStack());
330 out.append(" X_" + i).append(": for (;;) { IF: if (gt <= " + i + ") {\n");
332 changeInCatch = true;
334 debug(" /* " + i + " */ ");
336 if (changeInCatch && trap.useTry()) {
338 previousTrap = trap.current();
340 final int c = readUByte(byteCodes, i);
343 emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(0));
346 emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(0));
349 emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(0));
352 emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(0));
355 emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(0));
358 emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(1));
361 emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(1));
364 emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(1));
367 emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(1));
370 emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(1));
373 emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(2));
376 emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(2));
379 emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(2));
382 emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(2));
385 emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(2));
388 emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(3));
391 emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(3));
394 emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(3));
397 emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(3));
400 emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(3));
404 final int indx = wide ? readUShort(byteCodes, i++)
405 : readUByte(byteCodes, i);
407 emit(out, "var @1 = @2;",
408 smapper.pushI(), lmapper.getI(indx));
413 final int indx = wide ? readUShort(byteCodes, i++)
414 : readUByte(byteCodes, i);
416 emit(out, "var @1 = @2;",
417 smapper.pushL(), lmapper.getL(indx));
422 final int indx = wide ? readUShort(byteCodes, i++)
423 : readUByte(byteCodes, i);
425 emit(out, "var @1 = @2;",
426 smapper.pushF(), lmapper.getF(indx));
431 final int indx = wide ? readUShort(byteCodes, i++)
432 : readUByte(byteCodes, i);
434 emit(out, "var @1 = @2;",
435 smapper.pushD(), lmapper.getD(indx));
440 final int indx = wide ? readUShort(byteCodes, i++)
441 : readUByte(byteCodes, i);
443 emit(out, "var @1 = @2;",
444 smapper.pushA(), lmapper.getA(indx));
449 final int indx = wide ? readUShort(byteCodes, i++)
450 : readUByte(byteCodes, i);
452 emit(out, "var @1 = @2;",
453 lmapper.setI(indx), smapper.popI());
458 final int indx = wide ? readUShort(byteCodes, i++)
459 : readUByte(byteCodes, i);
461 emit(out, "var @1 = @2;",
462 lmapper.setL(indx), smapper.popL());
467 final int indx = wide ? readUShort(byteCodes, i++)
468 : readUByte(byteCodes, i);
470 emit(out, "var @1 = @2;",
471 lmapper.setF(indx), smapper.popF());
476 final int indx = wide ? readUShort(byteCodes, i++)
477 : readUByte(byteCodes, i);
479 emit(out, "var @1 = @2;",
480 lmapper.setD(indx), smapper.popD());
485 final int indx = wide ? readUShort(byteCodes, i++)
486 : readUByte(byteCodes, i);
488 emit(out, "var @1 = @2;",
489 lmapper.setA(indx), smapper.popA());
493 emit(out, "var @1 = @2;", lmapper.setA(0), smapper.popA());
496 emit(out, "var @1 = @2;", lmapper.setI(0), smapper.popI());
499 emit(out, "var @1 = @2;", lmapper.setL(0), smapper.popL());
502 emit(out, "var @1 = @2;", lmapper.setF(0), smapper.popF());
505 emit(out, "var @1 = @2;", lmapper.setD(0), smapper.popD());
508 emit(out, "var @1 = @2;", lmapper.setA(1), smapper.popA());
511 emit(out, "var @1 = @2;", lmapper.setI(1), smapper.popI());
514 emit(out, "var @1 = @2;", lmapper.setL(1), smapper.popL());
517 emit(out, "var @1 = @2;", lmapper.setF(1), smapper.popF());
520 emit(out, "var @1 = @2;", lmapper.setD(1), smapper.popD());
523 emit(out, "var @1 = @2;", lmapper.setA(2), smapper.popA());
526 emit(out, "var @1 = @2;", lmapper.setI(2), smapper.popI());
529 emit(out, "var @1 = @2;", lmapper.setL(2), smapper.popL());
532 emit(out, "var @1 = @2;", lmapper.setF(2), smapper.popF());
535 emit(out, "var @1 = @2;", lmapper.setD(2), smapper.popD());
538 emit(out, "var @1 = @2;", lmapper.setA(3), smapper.popA());
541 emit(out, "var @1 = @2;", lmapper.setI(3), smapper.popI());
544 emit(out, "var @1 = @2;", lmapper.setL(3), smapper.popL());
547 emit(out, "var @1 = @2;", lmapper.setF(3), smapper.popF());
550 emit(out, "var @1 = @2;", lmapper.setD(3), smapper.popD());
553 emit(out, "@1 = @1.add32(@2);", smapper.getI(1), smapper.popI());
556 emit(out, "@1 = @1.add64(@2);", smapper.getL(1), smapper.popL());
559 emit(out, "@1 += @2;", smapper.getF(1), smapper.popF());
562 emit(out, "@1 += @2;", smapper.getD(1), smapper.popD());
565 emit(out, "@1 = @1.sub32(@2);", smapper.getI(1), smapper.popI());
568 emit(out, "@1 = @1.sub64(@2);", smapper.getL(1), smapper.popL());
571 emit(out, "@1 -= @2;", smapper.getF(1), smapper.popF());
574 emit(out, "@1 -= @2;", smapper.getD(1), smapper.popD());
577 emit(out, "@1 = @1.mul32(@2);", smapper.getI(1), smapper.popI());
580 emit(out, "@1 = @1.mul64(@2);", smapper.getL(1), smapper.popL());
583 emit(out, "@1 *= @2;", smapper.getF(1), smapper.popF());
586 emit(out, "@1 *= @2;", smapper.getD(1), smapper.popD());
589 emit(out, "@1 = @1.div32(@2);",
590 smapper.getI(1), smapper.popI());
593 emit(out, "@1 = @1.div64(@2);",
594 smapper.getL(1), smapper.popL());
597 emit(out, "@1 /= @2;", smapper.getF(1), smapper.popF());
600 emit(out, "@1 /= @2;", smapper.getD(1), smapper.popD());
603 emit(out, "@1 = @1.mod32(@2);",
604 smapper.getI(1), smapper.popI());
607 emit(out, "@1 = @1.mod64(@2);",
608 smapper.getL(1), smapper.popL());
611 emit(out, "@1 %= @2;", smapper.getF(1), smapper.popF());
614 emit(out, "@1 %= @2;", smapper.getD(1), smapper.popD());
617 emit(out, "@1 &= @2;", smapper.getI(1), smapper.popI());
620 emit(out, "@1 = @1.and64(@2);", smapper.getL(1), smapper.popL());
623 emit(out, "@1 |= @2;", smapper.getI(1), smapper.popI());
626 emit(out, "@1 = @1.or64(@2);", smapper.getL(1), smapper.popL());
629 emit(out, "@1 ^= @2;", smapper.getI(1), smapper.popI());
632 emit(out, "@1 = @1.xor64(@2);", smapper.getL(1), smapper.popL());
635 emit(out, "@1 = @1.neg32();", smapper.getI(0));
638 emit(out, "@1 = @1.neg64();", smapper.getL(0));
641 emit(out, "@1 = -@1;", smapper.getF(0));
644 emit(out, "@1 = -@1;", smapper.getD(0));
647 emit(out, "@1 <<= @2;", smapper.getI(1), smapper.popI());
650 emit(out, "@1 = @1.shl64(@2);", smapper.getL(1), smapper.popI());
653 emit(out, "@1 >>= @2;", smapper.getI(1), smapper.popI());
656 emit(out, "@1 = @1.shr64(@2);", smapper.getL(1), smapper.popI());
659 emit(out, "@1 >>>= @2;", smapper.getI(1), smapper.popI());
662 emit(out, "@1 = @1.ushr64(@2);", smapper.getL(1), smapper.popI());
666 final int varIndx = wide ? readUShort(byteCodes, i++)
667 : readUByte(byteCodes, i);
669 final int incrBy = wide ? readShort(byteCodes, i++)
673 emit(out, "@1++;", lmapper.getI(varIndx));
675 emit(out, "@1 += @2;",
676 lmapper.getI(varIndx),
677 Integer.toString(incrBy));
682 emit(out, "return;");
685 emit(out, "return @1;", smapper.popI());
688 emit(out, "return @1;", smapper.popL());
691 emit(out, "return @1;", smapper.popF());
694 emit(out, "return @1;", smapper.popD());
697 emit(out, "return @1;", smapper.popA());
700 emit(out, "var @2 = @1;", smapper.popI(), smapper.pushL());
703 emit(out, "var @2 = @1;", smapper.popI(), smapper.pushF());
706 emit(out, "var @2 = @1;", smapper.popI(), smapper.pushD());
709 emit(out, "var @2 = @1.toInt32();", smapper.popL(), smapper.pushI());
713 emit(out, "var @2 = @1.toFP();", smapper.popL(), smapper.pushF());
716 emit(out, "var @2 = @1.toFP();", smapper.popL(), smapper.pushD());
719 emit(out, "var @2 = @1;", smapper.popF(), smapper.pushD());
722 emit(out, "var @2 = @1;", smapper.popD(), smapper.pushF());
725 emit(out, "var @2 = @1.toInt32();",
726 smapper.popF(), smapper.pushI());
729 emit(out, "var @2 = @1.toLong();",
730 smapper.popF(), smapper.pushL());
733 emit(out, "var @2 = @1.toInt32();",
734 smapper.popD(), smapper.pushI());
737 emit(out, "var @2 = @1.toLong();",
738 smapper.popD(), smapper.pushL());
741 emit(out, "var @1 = @1.toInt8();", smapper.getI(0));
744 out.append("{ /* number conversion */ }");
747 emit(out, "var @1 = @1.toInt16();", smapper.getI(0));
749 case opc_aconst_null:
750 emit(out, "var @1 = null;", smapper.pushA());
753 emit(out, "var @1 = -1;", smapper.pushI());
756 emit(out, "var @1 = 0;", smapper.pushI());
759 emit(out, "var @1 = 0;", smapper.pushD());
762 emit(out, "var @1 = 0;", smapper.pushL());
765 emit(out, "var @1 = 0;", smapper.pushF());
768 emit(out, "var @1 = 1;", smapper.pushI());
771 emit(out, "var @1 = 1;", smapper.pushL());
774 emit(out, "var @1 = 1;", smapper.pushF());
777 emit(out, "var @1 = 1;", smapper.pushD());
780 emit(out, "var @1 = 2;", smapper.pushI());
783 emit(out, "var @1 = 2;", smapper.pushF());
786 emit(out, "var @1 = 3;", smapper.pushI());
789 emit(out, "var @1 = 4;", smapper.pushI());
792 emit(out, "var @1 = 5;", smapper.pushI());
795 int indx = readUByte(byteCodes, ++i);
796 String v = encodeConstant(indx);
797 int type = VarType.fromConstantType(jc.getTag(indx));
798 emit(out, "var @1 = @2;", smapper.pushT(type), v);
803 int indx = readUShortArg(byteCodes, i);
805 String v = encodeConstant(indx);
806 int type = VarType.fromConstantType(jc.getTag(indx));
807 if (type == VarType.LONG) {
808 final Long lv = new Long(v);
809 final int low = (int)(lv.longValue() & 0xFFFFFFFF);
810 final int hi = (int)(lv.longValue() >> 32);
811 emit(out, "var @1 = 0x@3.next32(0x@2);", smapper.pushL(),
812 Integer.toHexString(low), Integer.toHexString(hi));
814 emit(out, "var @1 = @2;", smapper.pushT(type), v);
819 emit(out, "var @3 = @2.compare64(@1);",
820 smapper.popL(), smapper.popL(), smapper.pushI());
824 emit(out, "var @3 = (@2 == @1) ? 0 : ((@2 < @1) ? -1 : 1);",
825 smapper.popF(), smapper.popF(), smapper.pushI());
829 emit(out, "var @3 = (@2 == @1) ? 0 : ((@2 < @1) ? -1 : 1);",
830 smapper.popD(), smapper.popD(), smapper.pushI());
833 i = generateIf(byteCodes, i, smapper.popA(), smapper.popA(),
834 "===", topMostLabel);
837 i = generateIf(byteCodes, i, smapper.popA(), smapper.popA(),
838 "!==", topMostLabel);
841 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
845 int indx = i + readShortArg(byteCodes, i);
846 emitIf(out, "if (@1 == 0) ",
847 smapper.popI(), i, indx, topMostLabel);
852 int indx = i + readShortArg(byteCodes, i);
853 emitIf(out, "if (@1 != 0) ",
854 smapper.popI(), i, indx, topMostLabel);
859 int indx = i + readShortArg(byteCodes, i);
860 emitIf(out, "if (@1 < 0) ",
861 smapper.popI(), i, indx, topMostLabel);
866 int indx = i + readShortArg(byteCodes, i);
867 emitIf(out, "if (@1 <= 0) ",
868 smapper.popI(), i, indx, topMostLabel);
873 int indx = i + readShortArg(byteCodes, i);
874 emitIf(out, "if (@1 > 0) ",
875 smapper.popI(), i, indx, topMostLabel);
880 int indx = i + readShortArg(byteCodes, i);
881 emitIf(out, "if (@1 >= 0) ",
882 smapper.popI(), i, indx, topMostLabel);
886 case opc_ifnonnull: {
887 int indx = i + readShortArg(byteCodes, i);
888 emitIf(out, "if (@1 !== null) ",
889 smapper.popA(), i, indx, topMostLabel);
894 int indx = i + readShortArg(byteCodes, i);
895 emitIf(out, "if (@1 === null) ",
896 smapper.popA(), i, indx, topMostLabel);
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 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
921 int indx = i + readShortArg(byteCodes, i);
922 goTo(out, i, indx, topMostLabel);
926 case opc_lookupswitch: {
927 i = generateLookupSwitch(i, byteCodes, smapper, topMostLabel);
930 case opc_tableswitch: {
931 i = generateTableSwitch(i, byteCodes, smapper, topMostLabel);
934 case opc_invokeinterface: {
935 i = invokeVirtualMethod(byteCodes, i, smapper) + 2;
938 case opc_invokevirtual:
939 i = invokeVirtualMethod(byteCodes, i, smapper);
941 case opc_invokespecial:
942 i = invokeStaticMethod(byteCodes, i, smapper, false);
944 case opc_invokestatic:
945 i = invokeStaticMethod(byteCodes, i, smapper, true);
948 int indx = readUShortArg(byteCodes, i);
949 String ci = jc.getClassName(indx);
950 emit(out, "var @1 = new @2;",
951 smapper.pushA(), accessClass(ci.replace('/', '_')));
957 int atype = readUByte(byteCodes, ++i);
958 generateNewArray(atype, smapper);
960 case opc_anewarray: {
961 int type = readUShortArg(byteCodes, i);
963 generateANewArray(type, smapper);
966 case opc_multianewarray: {
967 int type = readUShortArg(byteCodes, i);
969 i = generateMultiANewArray(type, byteCodes, i, smapper);
972 case opc_arraylength:
973 emit(out, "var @2 = @1.length;",
974 smapper.popA(), smapper.pushI());
977 emit(out, "@3.at(@2, @1);",
978 smapper.popL(), smapper.popI(), smapper.popA());
981 emit(out, "@3.at(@2, @1);",
982 smapper.popF(), smapper.popI(), smapper.popA());
985 emit(out, "@3.at(@2, @1);",
986 smapper.popD(), smapper.popI(), smapper.popA());
989 emit(out, "@3.at(@2, @1);",
990 smapper.popA(), smapper.popI(), smapper.popA());
996 emit(out, "@3.at(@2, @1);",
997 smapper.popI(), smapper.popI(), smapper.popA());
1000 emit(out, "var @3 = @2.at(@1);",
1001 smapper.popI(), smapper.popA(), smapper.pushL());
1004 emit(out, "var @3 = @2.at(@1);",
1005 smapper.popI(), smapper.popA(), smapper.pushF());
1008 emit(out, "var @3 = @2.at(@1);",
1009 smapper.popI(), smapper.popA(), smapper.pushD());
1012 emit(out, "var @3 = @2.at(@1);",
1013 smapper.popI(), smapper.popA(), smapper.pushA());
1019 emit(out, "var @3 = @2.at(@1);",
1020 smapper.popI(), smapper.popA(), smapper.pushI());
1028 final Variable v = smapper.get(0);
1029 emit(out, "var @1 = @2;", smapper.pushT(v.getType()), v);
1033 final Variable vi1 = smapper.get(0);
1035 if (vi1.isCategory2()) {
1036 emit(out, "var @1 = @2;",
1037 smapper.pushT(vi1.getType()), vi1);
1039 final Variable vi2 = smapper.get(1);
1040 emit(out, "var @1 = @2, @3 = @4;",
1041 smapper.pushT(vi2.getType()), vi2,
1042 smapper.pushT(vi1.getType()), vi1);
1047 final Variable vi1 = smapper.pop();
1048 final Variable vi2 = smapper.pop();
1049 final Variable vo3 = smapper.pushT(vi1.getType());
1050 final Variable vo2 = smapper.pushT(vi2.getType());
1051 final Variable vo1 = smapper.pushT(vi1.getType());
1053 emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
1054 vo1, vi1, vo2, vi2, vo3, vo1);
1058 final Variable vi1 = smapper.pop();
1059 final Variable vi2 = smapper.pop();
1061 if (vi1.isCategory2()) {
1062 final Variable vo3 = smapper.pushT(vi1.getType());
1063 final Variable vo2 = smapper.pushT(vi2.getType());
1064 final Variable vo1 = smapper.pushT(vi1.getType());
1066 emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
1067 vo1, vi1, vo2, vi2, vo3, vo1);
1069 final Variable vi3 = smapper.pop();
1070 final Variable vo5 = smapper.pushT(vi2.getType());
1071 final Variable vo4 = smapper.pushT(vi1.getType());
1072 final Variable vo3 = smapper.pushT(vi3.getType());
1073 final Variable vo2 = smapper.pushT(vi2.getType());
1074 final Variable vo1 = smapper.pushT(vi1.getType());
1076 emit(out, "var @1 = @2, @3 = @4, @5 = @6,",
1077 vo1, vi1, vo2, vi2, vo3, vi3);
1078 emit(out, " @1 = @2, @3 = @4;",
1079 vo4, vo1, vo5, vo2);
1084 final Variable vi1 = smapper.pop();
1085 final Variable vi2 = smapper.pop();
1087 if (vi2.isCategory2()) {
1088 final Variable vo3 = smapper.pushT(vi1.getType());
1089 final Variable vo2 = smapper.pushT(vi2.getType());
1090 final Variable vo1 = smapper.pushT(vi1.getType());
1092 emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
1093 vo1, vi1, vo2, vi2, vo3, vo1);
1095 final Variable vi3 = smapper.pop();
1096 final Variable vo4 = smapper.pushT(vi1.getType());
1097 final Variable vo3 = smapper.pushT(vi3.getType());
1098 final Variable vo2 = smapper.pushT(vi2.getType());
1099 final Variable vo1 = smapper.pushT(vi1.getType());
1101 emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8;",
1102 vo1, vi1, vo2, vi2, vo3, vi3, vo4, vo1);
1107 final Variable vi1 = smapper.pop();
1108 final Variable vi2 = smapper.pop();
1110 if (vi1.isCategory2()) {
1111 if (vi2.isCategory2()) {
1112 final Variable vo3 = smapper.pushT(vi1.getType());
1113 final Variable vo2 = smapper.pushT(vi2.getType());
1114 final Variable vo1 = smapper.pushT(vi1.getType());
1116 emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
1117 vo1, vi1, vo2, vi2, vo3, vo1);
1119 final Variable vi3 = smapper.pop();
1120 final Variable vo4 = smapper.pushT(vi1.getType());
1121 final Variable vo3 = smapper.pushT(vi3.getType());
1122 final Variable vo2 = smapper.pushT(vi2.getType());
1123 final Variable vo1 = smapper.pushT(vi1.getType());
1125 emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8;",
1126 vo1, vi1, vo2, vi2, vo3, vi3, vo4, vo1);
1129 final Variable vi3 = smapper.pop();
1131 if (vi3.isCategory2()) {
1132 final Variable vo5 = smapper.pushT(vi2.getType());
1133 final Variable vo4 = smapper.pushT(vi1.getType());
1134 final Variable vo3 = smapper.pushT(vi3.getType());
1135 final Variable vo2 = smapper.pushT(vi2.getType());
1136 final Variable vo1 = smapper.pushT(vi1.getType());
1138 emit(out, "var @1 = @2, @3 = @4, @5 = @6,",
1139 vo1, vi1, vo2, vi2, vo3, vi3);
1140 emit(out, " @1 = @2, @3 = @4;",
1141 vo4, vo1, vo5, vo2);
1143 final Variable vi4 = smapper.pop();
1144 final Variable vo6 = smapper.pushT(vi2.getType());
1145 final Variable vo5 = smapper.pushT(vi1.getType());
1146 final Variable vo4 = smapper.pushT(vi4.getType());
1147 final Variable vo3 = smapper.pushT(vi3.getType());
1148 final Variable vo2 = smapper.pushT(vi2.getType());
1149 final Variable vo1 = smapper.pushT(vi1.getType());
1151 emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8,",
1152 vo1, vi1, vo2, vi2, vo3, vi3, vo4, vi4);
1153 emit(out, " @1 = @2, @3 = @4;",
1154 vo5, vo1, vo6, vo2);
1160 final Variable vi1 = smapper.get(0);
1161 final Variable vi2 = smapper.get(1);
1163 if (vi1.getType() == vi2.getType()) {
1164 final Variable tmp = smapper.pushT(vi1.getType());
1166 emit(out, "var @1 = @2, @2 = @3, @3 = @1;",
1171 smapper.pushT(vi1.getType());
1172 smapper.pushT(vi2.getType());
1177 emit(out, "var @1 = @2;",
1178 smapper.pushI(), Integer.toString(byteCodes[++i]));
1181 emit(out, "var @1 = @2;",
1183 Integer.toString(readShortArg(byteCodes, i)));
1186 case opc_getfield: {
1187 int indx = readUShortArg(byteCodes, i);
1188 String[] fi = jc.getFieldInfoName(indx);
1189 final int type = VarType.fromFieldType(fi[2].charAt(0));
1190 final String mangleClass = mangleSig(fi[0]);
1191 final String mangleClassAccess = accessClass(mangleClass);
1192 emit(out, "var @2 = @4(false)._@3.call(@1);",
1194 smapper.pushT(type), fi[1], mangleClassAccess
1199 case opc_putfield: {
1200 int indx = readUShortArg(byteCodes, i);
1201 String[] fi = jc.getFieldInfoName(indx);
1202 final int type = VarType.fromFieldType(fi[2].charAt(0));
1203 final String mangleClass = mangleSig(fi[0]);
1204 final String mangleClassAccess = accessClass(mangleClass);
1205 emit(out, "@4(false)._@3.call(@2, @1);",
1207 smapper.popA(), fi[1],
1213 case opc_getstatic: {
1214 int indx = readUShortArg(byteCodes, i);
1215 String[] fi = jc.getFieldInfoName(indx);
1216 final int type = VarType.fromFieldType(fi[2].charAt(0));
1217 emit(out, "var @1 = @2(false)._@3();",
1218 smapper.pushT(type),
1219 accessClass(fi[0].replace('/', '_')), fi[1]);
1221 addReference(fi[0]);
1224 case opc_putstatic: {
1225 int indx = readUShortArg(byteCodes, i);
1226 String[] fi = jc.getFieldInfoName(indx);
1227 final int type = VarType.fromFieldType(fi[2].charAt(0));
1228 emit(out, "@1(false)._@2(@3);",
1229 accessClass(fi[0].replace('/', '_')), fi[1],
1230 smapper.popT(type));
1232 addReference(fi[0]);
1235 case opc_checkcast: {
1236 int indx = readUShortArg(byteCodes, i);
1237 generateCheckcast(indx, smapper);
1241 case opc_instanceof: {
1242 int indx = readUShortArg(byteCodes, i);
1243 generateInstanceOf(indx, smapper);
1248 final Variable v = smapper.popA();
1251 emit(out, "{ var @1 = @2; throw @2; }",
1252 smapper.pushA(), v);
1256 case opc_monitorenter: {
1257 out.append("/* monitor enter */");
1262 case opc_monitorexit: {
1263 out.append("/* monitor exit */");
1274 emit(out, "throw 'unknown bytecode @1';",
1275 Integer.toString(c));
1279 generateByteCodeComment(prev, i, byteCodes);
1283 if (previousTrap != null) {
1284 generateCatch(previousTrap, byteCodes.length, topMostLabel);
1286 out.append("\n }\n");
1287 while (openBraces-- > 0) {
1293 private int generateIf(byte[] byteCodes, int i, final Variable v2, final Variable v1, final String test, int topMostLabel) throws IOException {
1294 int indx = i + readShortArg(byteCodes, i);
1295 out.append("if (").append(v1)
1296 .append(' ').append(test).append(' ')
1297 .append(v2).append(") ");
1298 goTo(out, i, indx, topMostLabel);
1302 private int readInt4(byte[] byteCodes, int offset) {
1303 final int d = byteCodes[offset + 0] << 24;
1304 final int c = byteCodes[offset + 1] << 16;
1305 final int b = byteCodes[offset + 2] << 8;
1306 final int a = byteCodes[offset + 3];
1307 return (d & 0xff000000) | (c & 0xff0000) | (b & 0xff00) | (a & 0xff);
1309 private static int readUByte(byte[] byteCodes, int offset) {
1310 return byteCodes[offset] & 0xff;
1313 private static int readUShort(byte[] byteCodes, int offset) {
1314 return ((byteCodes[offset] & 0xff) << 8)
1315 | (byteCodes[offset + 1] & 0xff);
1317 private static int readUShortArg(byte[] byteCodes, int offsetInstruction) {
1318 return readUShort(byteCodes, offsetInstruction + 1);
1321 private static int readShort(byte[] byteCodes, int offset) {
1322 int signed = byteCodes[offset];
1323 byte b0 = (byte)signed;
1324 return (b0 << 8) | (byteCodes[offset + 1] & 0xff);
1326 private static int readShortArg(byte[] byteCodes, int offsetInstruction) {
1327 return readShort(byteCodes, offsetInstruction + 1);
1330 private static void countArgs(String descriptor, char[] returnType, StringBuilder sig, StringBuilder cnt) {
1332 Boolean count = null;
1333 boolean array = false;
1335 int firstPos = sig.length();
1336 while (i < descriptor.length()) {
1337 char ch = descriptor.charAt(i++);
1358 if (ch == 'J' || ch == 'D') {
1364 sig.insert(firstPos, ch);
1366 returnType[0] = '[';
1367 sig.insert(firstPos, "_3");
1376 returnType[0] = 'V';
1377 sig.insert(firstPos, 'V');
1380 int next = descriptor.indexOf(';', i);
1381 String realSig = mangleSig(descriptor, i - 1, next + 1);
1386 sig.append(realSig);
1389 sig.insert(firstPos, realSig);
1391 sig.insert(firstPos, "_3");
1393 returnType[0] = 'L';
1402 throw new IllegalStateException("Invalid char: " + ch);
1407 static String mangleSig(String sig) {
1408 return mangleSig(sig, 0, sig.length());
1411 private static String mangleSig(String txt, int first, int last) {
1412 StringBuilder sb = new StringBuilder();
1413 for (int i = first; i < last; i++) {
1414 final char ch = txt.charAt(i);
1416 case '/': sb.append('_'); break;
1417 case '_': sb.append("_1"); break;
1418 case ';': sb.append("_2"); break;
1419 case '[': sb.append("_3"); break;
1420 default: sb.append(ch); break;
1423 return sb.toString();
1426 private static String findMethodName(MethodData m, StringBuilder cnt) {
1427 StringBuilder name = new StringBuilder();
1428 if ("<init>".equals(m.getName())) { // NOI18N
1429 name.append("cons"); // NOI18N
1430 } else if ("<clinit>".equals(m.getName())) { // NOI18N
1431 name.append("class"); // NOI18N
1433 name.append(m.getName());
1436 countArgs(m.getInternalSig(), new char[1], name, cnt);
1437 return name.toString();
1440 static String findMethodName(String[] mi, StringBuilder cnt, char[] returnType) {
1441 StringBuilder name = new StringBuilder();
1442 String descr = mi[2];//mi.getDescriptor();
1444 if ("<init>".equals(nm)) { // NOI18N
1445 name.append("cons"); // NOI18N
1449 countArgs(descr, returnType, name, cnt);
1450 return name.toString();
1453 private int invokeStaticMethod(byte[] byteCodes, int i, final StackMapper mapper, boolean isStatic)
1454 throws IOException {
1455 int methodIndex = readUShortArg(byteCodes, i);
1456 String[] mi = jc.getFieldInfoName(methodIndex);
1457 char[] returnType = { 'V' };
1458 StringBuilder cnt = new StringBuilder();
1459 String mn = findMethodName(mi, cnt, returnType);
1461 final int numArguments = isStatic ? cnt.length() : cnt.length() + 1;
1462 final Variable[] vars = new Variable[numArguments];
1464 for (int j = numArguments - 1; j >= 0; --j) {
1465 vars[j] = mapper.pop();
1468 if (returnType[0] != 'V') {
1470 .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
1474 final String in = mi[0];
1475 out.append(accessClass(in.replace('/', '_')));
1476 out.append("(false).");
1477 if (mn.startsWith("cons_")) {
1478 out.append("constructor.");
1484 out.append(".call(");
1486 if (numArguments > 0) {
1487 out.append(vars[0]);
1488 for (int j = 1; j < numArguments; ++j) {
1490 out.append(vars[j]);
1498 private int invokeVirtualMethod(byte[] byteCodes, int i, final StackMapper mapper)
1499 throws IOException {
1500 int methodIndex = readUShortArg(byteCodes, i);
1501 String[] mi = jc.getFieldInfoName(methodIndex);
1502 char[] returnType = { 'V' };
1503 StringBuilder cnt = new StringBuilder();
1504 String mn = findMethodName(mi, cnt, returnType);
1506 final int numArguments = cnt.length() + 1;
1507 final Variable[] vars = new Variable[numArguments];
1509 for (int j = numArguments - 1; j >= 0; --j) {
1510 vars[j] = mapper.pop();
1513 if (returnType[0] != 'V') {
1515 .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
1519 out.append(vars[0]).append('.');
1523 for (int j = 1; j < numArguments; ++j) {
1525 out.append(vars[j]);
1533 private void addReference(String cn) throws IOException {
1534 if (requireReference(cn)) {
1535 debug(" /* needs " + cn + " */");
1539 private void outType(String d, StringBuilder out) {
1541 while (d.charAt(0) == '[') {
1545 if (d.charAt(0) == 'L') {
1546 assert d.charAt(d.length() - 1) == ';';
1547 out.append(d.replace('/', '_').substring(0, d.length() - 1));
1553 private String encodeConstant(int entryIndex) throws IOException {
1554 String[] classRef = { null };
1555 String s = jc.stringValue(entryIndex, classRef);
1556 if (classRef[0] != null) {
1557 if (classRef[0].startsWith("[")) {
1558 s = accessClass("java_lang_Class") + "(false).forName__Ljava_lang_Class_2Ljava_lang_String_2('" + classRef[0] + "');";
1560 addReference(classRef[0]);
1561 s = accessClass(s.replace('/', '_')) + "(false).constructor.$class";
1567 private String javaScriptBody(String destObject, MethodData m, boolean isStatic) throws IOException {
1568 byte[] arr = m.findAnnotationData(true);
1572 final String jvmType = "Lorg/apidesign/bck2brwsr/core/JavaScriptBody;";
1573 class P extends AnnotationParser {
1579 String[] args = new String[30];
1583 protected void visitAttr(String type, String attr, String at, String value) {
1584 if (type.equals(jvmType)) {
1585 if ("body".equals(attr)) {
1587 } else if ("args".equals(attr)) {
1588 args[cnt++] = value;
1590 throw new IllegalArgumentException(attr);
1597 if (p.body == null) {
1600 StringBuilder cnt = new StringBuilder();
1601 final String mn = findMethodName(m, cnt);
1602 out.append(destObject).append(".").append(mn);
1603 out.append(" = function(");
1606 for (int i = 0; i < cnt.length(); i++) {
1608 space = outputArg(out, p.args, index);
1611 out.append(") {").append("\n");
1613 out.append("\n}\n");
1616 private static String className(ClassData jc) {
1617 //return jc.getName().getInternalName().replace('/', '_');
1618 return jc.getClassName().replace('/', '_');
1621 private static String[] findAnnotation(
1622 byte[] arr, ClassData cd, final String className,
1623 final String... attrNames
1624 ) throws IOException {
1628 final String[] values = new String[attrNames.length];
1629 final boolean[] found = { false };
1630 final String jvmType = "L" + className.replace('.', '/') + ";";
1631 AnnotationParser ap = new AnnotationParser(false, true) {
1633 protected void visitAttr(String type, String attr, String at, String value) {
1634 if (type.equals(jvmType)) {
1636 for (int i = 0; i < attrNames.length; i++) {
1637 if (attrNames[i].equals(attr)) {
1646 return found[0] ? values : null;
1649 private CharSequence initField(FieldData v) {
1650 final String is = v.getInternalSig();
1651 if (is.length() == 1) {
1652 switch (is.charAt(0)) {
1658 case 'I': return " = 0;";
1660 case 'D': return " = 0.0;";
1662 throw new IllegalStateException(is);
1668 private void generateAnno(ClassData cd, final Appendable out, byte[] data) throws IOException {
1669 AnnotationParser ap = new AnnotationParser(true, false) {
1670 int[] cnt = new int[32];
1674 protected void visitAnnotationStart(String attrType, boolean top) throws IOException {
1675 final String slashType = attrType.substring(1, attrType.length() - 1);
1676 requireReference(slashType);
1678 if (cnt[depth]++ > 0) {
1682 out.append('"').append(attrType).append("\" : ");
1689 protected void visitAnnotationEnd(String type, boolean top) throws IOException {
1690 out.append("\n}\n");
1695 protected void visitValueStart(String attrName, char type) throws IOException {
1696 if (cnt[depth]++ > 0) {
1700 if (attrName != null) {
1701 out.append(attrName).append(" : ");
1709 protected void visitValueEnd(String attrName, char type) throws IOException {
1717 protected void visitAttr(String type, String attr, String attrType, String value)
1718 throws IOException {
1719 if (attr == null && value == null) {
1726 protected void visitEnumAttr(String type, String attr, String attrType, String value)
1727 throws IOException {
1728 final String slashType = attrType.substring(1, attrType.length() - 1);
1729 requireReference(slashType);
1731 out.append(accessClass(slashType.replace('/', '_')))
1732 .append("(false).constructor.").append(value);
1738 private static String outputArg(Appendable out, String[] args, int indx) throws IOException {
1739 final String name = args[indx];
1743 if (name.contains(",")) {
1744 throw new IOException("Wrong parameter with ',': " + name);
1750 private static void emit(final Appendable out,
1751 final String format,
1752 final CharSequence... params) throws IOException {
1753 final int length = format.length();
1756 int paramOffset = format.indexOf('@');
1757 while ((paramOffset != -1) && (paramOffset < (length - 1))) {
1758 final char paramChar = format.charAt(paramOffset + 1);
1759 if ((paramChar >= '1') && (paramChar <= '9')) {
1760 final int paramIndex = paramChar - '0' - 1;
1762 out.append(format, processed, paramOffset);
1763 out.append(params[paramIndex]);
1766 processed = paramOffset + 1;
1769 paramOffset = format.indexOf('@', paramOffset + 1);
1772 out.append(format, processed, length);
1775 private void generateCatch(TrapData[] traps, int current, int topMostLabel) throws IOException {
1776 out.append("} catch (e) {\n");
1778 for (TrapData e : traps) {
1782 if (e.catch_cpx != 0) { //not finally
1783 final String classInternalName = jc.getClassName(e.catch_cpx);
1784 addReference(classInternalName);
1785 if ("java/lang/Throwable".equals(classInternalName)) {
1786 out.append("if (e.$instOf_java_lang_Throwable) {");
1787 out.append(" var stA0 = e;");
1788 out.append("} else {");
1789 out.append(" var stA0 = vm.java_lang_Throwable(true);");
1790 out.append(" vm.java_lang_Throwable.cons__VLjava_lang_String_2.call(stA0, e.toString());");
1792 goTo(out, current, e.handler_pc, topMostLabel);
1794 out.append("if (e.$instOf_" + classInternalName.replace('/', '_') + ") {");
1795 out.append("var stA0 = e;");
1796 goTo(out, current, e.handler_pc, topMostLabel);
1800 finallyPC = e.handler_pc;
1803 if (finallyPC == -1) {
1804 out.append("throw e;");
1806 out.append("var stA0 = e;");
1807 goTo(out, current, finallyPC, topMostLabel);
1812 private static void goTo(Appendable out, int current, int to, int canBack) throws IOException {
1815 out.append("{ gt = 0; continue X_" + to + "; }");
1817 out.append("{ gt = " + to + "; continue X_0; }");
1820 out.append("{ gt = " + to + "; break IF; }");
1824 private static void emitIf(
1825 Appendable out, String pattern, Variable param,
1826 int current, int to, int canBack
1827 ) throws IOException {
1828 emit(out, pattern, param);
1829 goTo(out, current, to, canBack);
1832 private void generateNewArray(int atype, final StackMapper smapper) throws IOException, IllegalStateException {
1835 case 4: jvmType = "[Z"; break;
1836 case 5: jvmType = "[C"; break;
1837 case 6: jvmType = "[F"; break;
1838 case 7: jvmType = "[D"; break;
1839 case 8: jvmType = "[B"; break;
1840 case 9: jvmType = "[S"; break;
1841 case 10: jvmType = "[I"; break;
1842 case 11: jvmType = "[J"; break;
1843 default: throw new IllegalStateException("Array type: " + atype);
1845 emit(out, "var @2 = Array.prototype.newArray__Ljava_lang_Object_2ZLjava_lang_String_2I(true, '@3', @1);",
1846 smapper.popI(), smapper.pushA(), jvmType);
1849 private void generateANewArray(int type, final StackMapper smapper) throws IOException {
1850 String typeName = jc.getClassName(type);
1851 if (typeName.startsWith("[")) {
1852 typeName = "[" + typeName;
1854 typeName = "[L" + typeName + ";";
1856 emit(out, "var @2 = Array.prototype.newArray__Ljava_lang_Object_2ZLjava_lang_String_2I(false, '@3', @1);",
1857 smapper.popI(), smapper.pushA(), typeName);
1860 private int generateMultiANewArray(int type, final byte[] byteCodes, int i, final StackMapper smapper) throws IOException {
1861 String typeName = jc.getClassName(type);
1862 int dim = readUByte(byteCodes, ++i);
1863 StringBuilder dims = new StringBuilder();
1865 for (int d = 0; d < dim; d++) {
1867 dims.insert(1, ",");
1869 dims.insert(1, smapper.popI());
1872 emit(out, "var @2 = Array.prototype.multiNewArray__Ljava_lang_Object_2Ljava_lang_String_2_3II('@3', @1, 0);",
1873 dims.toString(), smapper.pushA(), typeName);
1877 private int generateTableSwitch(int i, final byte[] byteCodes, final StackMapper smapper, int topMostLabel) throws IOException {
1878 int table = i / 4 * 4 + 4;
1879 int dflt = i + readInt4(byteCodes, table);
1881 int low = readInt4(byteCodes, table);
1883 int high = readInt4(byteCodes, table);
1885 out.append("switch (").append(smapper.popI()).append(") {\n");
1886 while (low <= high) {
1887 int offset = i + readInt4(byteCodes, table);
1889 out.append(" case " + low).append(":"); goTo(out, i, offset, topMostLabel); out.append('\n');
1892 out.append(" default: ");
1893 goTo(out, i, dflt, topMostLabel);
1899 private int generateLookupSwitch(int i, final byte[] byteCodes, final StackMapper smapper, int topMostLabel) throws IOException {
1900 int table = i / 4 * 4 + 4;
1901 int dflt = i + readInt4(byteCodes, table);
1903 int n = readInt4(byteCodes, table);
1905 out.append("switch (").append(smapper.popI()).append(") {\n");
1907 int cnstnt = readInt4(byteCodes, table);
1909 int offset = i + readInt4(byteCodes, table);
1911 out.append(" case " + cnstnt).append(": "); goTo(out, i, offset, topMostLabel); out.append('\n');
1913 out.append(" default: ");
1914 goTo(out, i, dflt, topMostLabel);
1920 private void generateInstanceOf(int indx, final StackMapper smapper) throws IOException {
1921 final String type = jc.getClassName(indx);
1922 if (!type.startsWith("[")) {
1923 emit(out, "var @2 = @1 != null && @1.$instOf_@3 ? 1 : 0;",
1924 smapper.popA(), smapper.pushI(),
1925 type.replace('/', '_'));
1927 emit(out, "var @2 = vm.java_lang_Class(false).forName__Ljava_lang_Class_2Ljava_lang_String_2('@3').isInstance__ZLjava_lang_Object_2(@1);",
1928 smapper.popA(), smapper.pushI(),
1934 private void generateCheckcast(int indx, final StackMapper smapper) throws IOException {
1935 final String type = jc.getClassName(indx);
1936 if (!type.startsWith("[")) {
1938 "if (@1 !== null && !@1.$instOf_@2) throw vm.java_lang_ClassCastException(true);",
1939 smapper.getA(0), type.replace('/', '_'));
1941 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);",
1942 smapper.getA(0), type
1947 private void generateByteCodeComment(int prev, int i, final byte[] byteCodes) throws IOException {
1948 for (int j = prev; j <= i; j++) {
1950 final int cc = readUByte(byteCodes, j);
1951 out.append(Integer.toString(cc));