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 String accessMember(String object, String mangledName,
63 String[] fieldInfoName) throws IOException {
64 return object + "." + mangledName;
67 protected void declaredClass(ClassData classData, String mangledName)
71 protected void declaredField(FieldData fieldData,
73 String mangledName) throws IOException {
76 protected void declaredMethod(MethodData methodData,
78 String mangledName) throws IOException {
81 /** Prints out a debug message.
83 * @param msg the message
84 * @return true if the message has been printed
87 boolean debug(String msg) throws IOException {
93 * Converts a given class file to a JavaScript version.
95 * @param classFile input stream with code of the .class file
96 * @return the initialization code for this class, if any. Otherwise <code>null</code>
98 * @throws IOException if something goes wrong during read or write or translating
101 public String compile(InputStream classFile) throws IOException {
102 return compile(new ClassData(classFile));
105 protected String compile(ClassData classData) throws IOException {
107 if (jc.getMajor_version() < 50) {
108 throw new IOException("Can't compile " + jc.getClassName() + ". Class file version " + jc.getMajor_version() + "."
109 + jc.getMinor_version() + " - recompile with -target 1.6 (at least)."
112 byte[] arrData = jc.findAnnotationData(true);
113 String[] arr = findAnnotation(arrData, jc,
114 "org.apidesign.bck2brwsr.core.ExtraJavaScript",
115 "resource", "processByteCode"
118 if (!arr[0].isEmpty()) {
119 requireScript(arr[0]);
121 if ("0".equals(arr[1])) {
125 String[] proto = findAnnotation(arrData, jc,
126 "org.apidesign.bck2brwsr.core.JavaScriptPrototype",
127 "container", "prototype"
129 StringArray toInitilize = new StringArray();
130 final String className = className(jc);
131 out.append("\n\n").append(assignClass(className));
132 out.append("function ").append(className).append("() {");
133 out.append("\n var CLS = ").append(className).append(';');
134 out.append("\n if (!CLS.$class) {");
136 String sc = jc.getSuperClassName(); // with _
137 out.append("\n var pp = ").
138 append(accessClass(sc.replace('/', '_'))).append("(true);");
139 out.append("\n var p = CLS.prototype = pp;");
140 out.append("\n var c = p;");
141 out.append("\n var sprcls = pp.constructor.$class;");
143 out.append("\n var p = CLS.prototype = ").append(proto[1]).append(";");
144 if (proto[0] == null) {
147 out.append("\n var c = ").append(proto[0]).append(";");
148 out.append("\n var sprcls = null;");
150 for (FieldData v : jc.getFields()) {
152 out.append("\n CLS.").append(v.getName()).append(initField(v));
153 out.append("\n c._").append(v.getName()).append(" = function (v) {")
154 .append(" if (arguments.length == 1) CLS.").append(v.getName())
155 .append(" = v; return CLS.").
156 append(v.getName()).append("; };");
158 out.append("\n c._").append(v.getName()).append(" = function (v) {")
159 .append(" if (arguments.length == 1) this.fld_").
160 append(className).append('_').append(v.getName())
161 .append(" = v; return this.fld_").
162 append(className).append('_').append(v.getName())
166 declaredField(v, "c", "_" + v.getName());
168 for (MethodData m : jc.getMethods()) {
169 byte[] onlyArr = m.findAnnotationData(true);
170 String[] only = findAnnotation(onlyArr, jc,
171 "org.apidesign.bck2brwsr.core.JavaScriptOnly",
175 if (only[0] != null && only[1] != null) {
176 out.append("\n p.").append(only[0]).append(" = ")
177 .append(only[1]).append(";");
186 mn = generateStaticMethod(destObject, m, toInitilize);
188 if (m.isConstructor()) {
190 mn = generateInstanceMethod(destObject, m);
193 mn = generateInstanceMethod(destObject, m);
196 declaredMethod(m, destObject, mn);
197 byte[] runAnno = m.findAnnotationData(false);
198 if (runAnno != null) {
199 out.append("\n ").append(destObject).append(".").append(mn).append(".anno = {");
200 generateAnno(jc, out, runAnno);
203 out.append("\n ").append(destObject).append(".").append(mn).append(".access = " + m.getAccess()).append(";");
204 out.append("\n ").append(destObject).append(".").append(mn).append(".cls = CLS;");
206 out.append("\n c.constructor = CLS;");
207 out.append("\n c['$instOf_").append(className).append("'] = true;");
208 for (String superInterface : jc.getSuperInterfaces()) {
209 out.append("\n c['$instOf_").append(superInterface.replace('/', '_')).append("'] = true;");
211 out.append("\n CLS.$class = 'temp';");
212 out.append("\n CLS.$class = ");
213 out.append(accessClass("java_lang_Class(true);"));
214 out.append("\n CLS.$class.jvmName = '").append(jc.getClassName()).append("';");
215 out.append("\n CLS.$class.superclass = sprcls;");
216 out.append("\n CLS.$class.access = ").append(jc.getAccessFlags()+";");
217 out.append("\n CLS.$class.cnstr = CLS;");
218 byte[] classAnno = jc.findAnnotationData(false);
219 if (classAnno != null) {
220 out.append("\n CLS.$class.anno = {");
221 generateAnno(jc, out, classAnno);
224 for (String init : toInitilize.toArray()) {
225 out.append("\n ").append(init).append("();");
228 out.append("\n if (arguments.length === 0) {");
229 out.append("\n if (!(this instanceof CLS)) {");
230 out.append("\n return new CLS();");
232 for (FieldData v : jc.getFields()) {
233 byte[] onlyArr = v.findAnnotationData(true);
234 String[] only = findAnnotation(onlyArr, jc,
235 "org.apidesign.bck2brwsr.core.JavaScriptOnly",
239 if (only[0] != null && only[1] != null) {
240 out.append("\n p.").append(only[0]).append(" = ")
241 .append(only[1]).append(";");
246 out.append("\n this.fld_").
247 append(className).append('_').
248 append(v.getName()).append(initField(v));
251 out.append("\n return this;");
253 out.append("\n return arguments[0] ? new CLS() : CLS.prototype;");
256 declaredClass(jc, className);
258 // StringBuilder sb = new StringBuilder();
259 // for (String init : toInitilize.toArray()) {
260 // sb.append("\n").append(init).append("();");
264 private String generateStaticMethod(String destObject, MethodData m, StringArray toInitilize) throws IOException {
265 String jsb = javaScriptBody(destObject, m, true);
269 final String mn = findMethodName(m, new StringBuilder());
270 if (mn.equals("class__V")) {
271 toInitilize.add(accessClass(className(jc)) + "(false)." + mn);
273 generateMethod(destObject, mn, m);
277 private String generateInstanceMethod(String destObject, MethodData m) throws IOException {
278 String jsb = javaScriptBody(destObject, m, false);
282 final String mn = findMethodName(m, new StringBuilder());
283 generateMethod(destObject, mn, m);
287 private void generateMethod(String destObject, String name, MethodData m)
289 final StackMapIterator stackMapIterator = m.createStackMapIterator();
290 TrapDataIterator trap = m.getTrapDataIterator();
291 final LocalsMapper lmapper =
292 new LocalsMapper(stackMapIterator.getArguments());
294 out.append(destObject).append(".").append(name).append(" = function(");
295 lmapper.outputArguments(out, m.isStatic());
296 out.append(") {").append("\n");
298 final byte[] byteCodes = m.getCode();
299 if (byteCodes == null) {
300 out.append(" throw 'no code found for ")
301 .append(jc.getClassName()).append('.')
302 .append(m.getName()).append("';\n");
307 final StackMapper smapper = new StackMapper();
310 out.append(" var ").append(" lcA0 = this;\n");
313 int lastStackFrame = -1;
314 TrapData[] previousTrap = null;
315 boolean wide = false;
317 out.append("\n var gt = 0;\n");
319 int topMostLabel = 0;
320 for (int i = 0; i < byteCodes.length; i++) {
322 stackMapIterator.advanceTo(i);
323 boolean changeInCatch = trap.advanceTo(i);
324 if (changeInCatch || lastStackFrame != stackMapIterator.getFrameIndex()) {
325 if (previousTrap != null) {
326 generateCatch(previousTrap, i, topMostLabel);
330 if (lastStackFrame != stackMapIterator.getFrameIndex()) {
334 if (openBraces > 64) {
335 for (int c = 0; c < 64; c++) {
336 out.append("break;}\n");
342 lastStackFrame = stackMapIterator.getFrameIndex();
343 lmapper.syncWithFrameLocals(stackMapIterator.getFrameLocals());
344 smapper.syncWithFrameStack(stackMapIterator.getFrameStack());
345 out.append(" X_" + i).append(": for (;;) { IF: if (gt <= " + i + ") {\n");
347 changeInCatch = true;
349 debug(" /* " + i + " */ ");
351 if (changeInCatch && trap.useTry()) {
353 previousTrap = trap.current();
355 final int c = readUByte(byteCodes, i);
358 emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(0));
361 emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(0));
364 emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(0));
367 emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(0));
370 emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(0));
373 emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(1));
376 emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(1));
379 emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(1));
382 emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(1));
385 emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(1));
388 emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(2));
391 emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(2));
394 emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(2));
397 emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(2));
400 emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(2));
403 emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(3));
406 emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(3));
409 emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(3));
412 emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(3));
415 emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(3));
419 final int indx = wide ? readUShort(byteCodes, i++)
420 : readUByte(byteCodes, i);
422 emit(out, "var @1 = @2;",
423 smapper.pushI(), lmapper.getI(indx));
428 final int indx = wide ? readUShort(byteCodes, i++)
429 : readUByte(byteCodes, i);
431 emit(out, "var @1 = @2;",
432 smapper.pushL(), lmapper.getL(indx));
437 final int indx = wide ? readUShort(byteCodes, i++)
438 : readUByte(byteCodes, i);
440 emit(out, "var @1 = @2;",
441 smapper.pushF(), lmapper.getF(indx));
446 final int indx = wide ? readUShort(byteCodes, i++)
447 : readUByte(byteCodes, i);
449 emit(out, "var @1 = @2;",
450 smapper.pushD(), lmapper.getD(indx));
455 final int indx = wide ? readUShort(byteCodes, i++)
456 : readUByte(byteCodes, i);
458 emit(out, "var @1 = @2;",
459 smapper.pushA(), lmapper.getA(indx));
464 final int indx = wide ? readUShort(byteCodes, i++)
465 : readUByte(byteCodes, i);
467 emit(out, "var @1 = @2;",
468 lmapper.setI(indx), smapper.popI());
473 final int indx = wide ? readUShort(byteCodes, i++)
474 : readUByte(byteCodes, i);
476 emit(out, "var @1 = @2;",
477 lmapper.setL(indx), smapper.popL());
482 final int indx = wide ? readUShort(byteCodes, i++)
483 : readUByte(byteCodes, i);
485 emit(out, "var @1 = @2;",
486 lmapper.setF(indx), smapper.popF());
491 final int indx = wide ? readUShort(byteCodes, i++)
492 : readUByte(byteCodes, i);
494 emit(out, "var @1 = @2;",
495 lmapper.setD(indx), smapper.popD());
500 final int indx = wide ? readUShort(byteCodes, i++)
501 : readUByte(byteCodes, i);
503 emit(out, "var @1 = @2;",
504 lmapper.setA(indx), smapper.popA());
508 emit(out, "var @1 = @2;", lmapper.setA(0), smapper.popA());
511 emit(out, "var @1 = @2;", lmapper.setI(0), smapper.popI());
514 emit(out, "var @1 = @2;", lmapper.setL(0), smapper.popL());
517 emit(out, "var @1 = @2;", lmapper.setF(0), smapper.popF());
520 emit(out, "var @1 = @2;", lmapper.setD(0), smapper.popD());
523 emit(out, "var @1 = @2;", lmapper.setA(1), smapper.popA());
526 emit(out, "var @1 = @2;", lmapper.setI(1), smapper.popI());
529 emit(out, "var @1 = @2;", lmapper.setL(1), smapper.popL());
532 emit(out, "var @1 = @2;", lmapper.setF(1), smapper.popF());
535 emit(out, "var @1 = @2;", lmapper.setD(1), smapper.popD());
538 emit(out, "var @1 = @2;", lmapper.setA(2), smapper.popA());
541 emit(out, "var @1 = @2;", lmapper.setI(2), smapper.popI());
544 emit(out, "var @1 = @2;", lmapper.setL(2), smapper.popL());
547 emit(out, "var @1 = @2;", lmapper.setF(2), smapper.popF());
550 emit(out, "var @1 = @2;", lmapper.setD(2), smapper.popD());
553 emit(out, "var @1 = @2;", lmapper.setA(3), smapper.popA());
556 emit(out, "var @1 = @2;", lmapper.setI(3), smapper.popI());
559 emit(out, "var @1 = @2;", lmapper.setL(3), smapper.popL());
562 emit(out, "var @1 = @2;", lmapper.setF(3), smapper.popF());
565 emit(out, "var @1 = @2;", lmapper.setD(3), smapper.popD());
568 emit(out, "@1 = @1.add32(@2);", smapper.getI(1), smapper.popI());
571 emit(out, "@1 = @1.add64(@2);", smapper.getL(1), smapper.popL());
574 emit(out, "@1 += @2;", smapper.getF(1), smapper.popF());
577 emit(out, "@1 += @2;", smapper.getD(1), smapper.popD());
580 emit(out, "@1 = @1.sub32(@2);", smapper.getI(1), smapper.popI());
583 emit(out, "@1 = @1.sub64(@2);", smapper.getL(1), smapper.popL());
586 emit(out, "@1 -= @2;", smapper.getF(1), smapper.popF());
589 emit(out, "@1 -= @2;", smapper.getD(1), smapper.popD());
592 emit(out, "@1 = @1.mul32(@2);", smapper.getI(1), smapper.popI());
595 emit(out, "@1 = @1.mul64(@2);", smapper.getL(1), smapper.popL());
598 emit(out, "@1 *= @2;", smapper.getF(1), smapper.popF());
601 emit(out, "@1 *= @2;", smapper.getD(1), smapper.popD());
604 emit(out, "@1 = @1.div32(@2);",
605 smapper.getI(1), smapper.popI());
608 emit(out, "@1 = @1.div64(@2);",
609 smapper.getL(1), smapper.popL());
612 emit(out, "@1 /= @2;", smapper.getF(1), smapper.popF());
615 emit(out, "@1 /= @2;", smapper.getD(1), smapper.popD());
618 emit(out, "@1 = @1.mod32(@2);",
619 smapper.getI(1), smapper.popI());
622 emit(out, "@1 = @1.mod64(@2);",
623 smapper.getL(1), smapper.popL());
626 emit(out, "@1 %= @2;", smapper.getF(1), smapper.popF());
629 emit(out, "@1 %= @2;", smapper.getD(1), smapper.popD());
632 emit(out, "@1 &= @2;", smapper.getI(1), smapper.popI());
635 emit(out, "@1 = @1.and64(@2);", smapper.getL(1), smapper.popL());
638 emit(out, "@1 |= @2;", smapper.getI(1), smapper.popI());
641 emit(out, "@1 = @1.or64(@2);", smapper.getL(1), smapper.popL());
644 emit(out, "@1 ^= @2;", smapper.getI(1), smapper.popI());
647 emit(out, "@1 = @1.xor64(@2);", smapper.getL(1), smapper.popL());
650 emit(out, "@1 = @1.neg32();", smapper.getI(0));
653 emit(out, "@1 = @1.neg64();", smapper.getL(0));
656 emit(out, "@1 = -@1;", smapper.getF(0));
659 emit(out, "@1 = -@1;", smapper.getD(0));
662 emit(out, "@1 <<= @2;", smapper.getI(1), smapper.popI());
665 emit(out, "@1 = @1.shl64(@2);", smapper.getL(1), smapper.popI());
668 emit(out, "@1 >>= @2;", smapper.getI(1), smapper.popI());
671 emit(out, "@1 = @1.shr64(@2);", smapper.getL(1), smapper.popI());
674 emit(out, "@1 >>>= @2;", smapper.getI(1), smapper.popI());
677 emit(out, "@1 = @1.ushr64(@2);", smapper.getL(1), smapper.popI());
681 final int varIndx = wide ? readUShort(byteCodes, i++)
682 : readUByte(byteCodes, i);
684 final int incrBy = wide ? readShort(byteCodes, i++)
688 emit(out, "@1++;", lmapper.getI(varIndx));
690 emit(out, "@1 += @2;",
691 lmapper.getI(varIndx),
692 Integer.toString(incrBy));
697 emit(out, "return;");
700 emit(out, "return @1;", smapper.popI());
703 emit(out, "return @1;", smapper.popL());
706 emit(out, "return @1;", smapper.popF());
709 emit(out, "return @1;", smapper.popD());
712 emit(out, "return @1;", smapper.popA());
715 emit(out, "var @2 = @1;", smapper.popI(), smapper.pushL());
718 emit(out, "var @2 = @1;", smapper.popI(), smapper.pushF());
721 emit(out, "var @2 = @1;", smapper.popI(), smapper.pushD());
724 emit(out, "var @2 = @1.toInt32();", smapper.popL(), smapper.pushI());
728 emit(out, "var @2 = @1.toFP();", smapper.popL(), smapper.pushF());
731 emit(out, "var @2 = @1.toFP();", smapper.popL(), smapper.pushD());
734 emit(out, "var @2 = @1;", smapper.popF(), smapper.pushD());
737 emit(out, "var @2 = @1;", smapper.popD(), smapper.pushF());
740 emit(out, "var @2 = @1.toInt32();",
741 smapper.popF(), smapper.pushI());
744 emit(out, "var @2 = @1.toLong();",
745 smapper.popF(), smapper.pushL());
748 emit(out, "var @2 = @1.toInt32();",
749 smapper.popD(), smapper.pushI());
752 emit(out, "var @2 = @1.toLong();",
753 smapper.popD(), smapper.pushL());
756 emit(out, "var @1 = @1.toInt8();", smapper.getI(0));
759 out.append("{ /* number conversion */ }");
762 emit(out, "var @1 = @1.toInt16();", smapper.getI(0));
764 case opc_aconst_null:
765 emit(out, "var @1 = null;", smapper.pushA());
768 emit(out, "var @1 = -1;", smapper.pushI());
771 emit(out, "var @1 = 0;", smapper.pushI());
774 emit(out, "var @1 = 0;", smapper.pushD());
777 emit(out, "var @1 = 0;", smapper.pushL());
780 emit(out, "var @1 = 0;", smapper.pushF());
783 emit(out, "var @1 = 1;", smapper.pushI());
786 emit(out, "var @1 = 1;", smapper.pushL());
789 emit(out, "var @1 = 1;", smapper.pushF());
792 emit(out, "var @1 = 1;", smapper.pushD());
795 emit(out, "var @1 = 2;", smapper.pushI());
798 emit(out, "var @1 = 2;", smapper.pushF());
801 emit(out, "var @1 = 3;", smapper.pushI());
804 emit(out, "var @1 = 4;", smapper.pushI());
807 emit(out, "var @1 = 5;", smapper.pushI());
810 int indx = readUByte(byteCodes, ++i);
811 String v = encodeConstant(indx);
812 int type = VarType.fromConstantType(jc.getTag(indx));
813 emit(out, "var @1 = @2;", smapper.pushT(type), v);
818 int indx = readUShortArg(byteCodes, i);
820 String v = encodeConstant(indx);
821 int type = VarType.fromConstantType(jc.getTag(indx));
822 if (type == VarType.LONG) {
823 final Long lv = new Long(v);
824 final int low = (int)(lv.longValue() & 0xFFFFFFFF);
825 final int hi = (int)(lv.longValue() >> 32);
826 emit(out, "var @1 = 0x@3.next32(0x@2);", smapper.pushL(),
827 Integer.toHexString(low), Integer.toHexString(hi));
829 emit(out, "var @1 = @2;", smapper.pushT(type), v);
834 emit(out, "var @3 = @2.compare64(@1);",
835 smapper.popL(), smapper.popL(), smapper.pushI());
839 emit(out, "var @3 = (@2 == @1) ? 0 : ((@2 < @1) ? -1 : 1);",
840 smapper.popF(), smapper.popF(), smapper.pushI());
844 emit(out, "var @3 = (@2 == @1) ? 0 : ((@2 < @1) ? -1 : 1);",
845 smapper.popD(), smapper.popD(), smapper.pushI());
848 i = generateIf(byteCodes, i, smapper.popA(), smapper.popA(),
849 "===", topMostLabel);
852 i = generateIf(byteCodes, i, smapper.popA(), smapper.popA(),
853 "!==", topMostLabel);
856 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
860 int indx = i + readShortArg(byteCodes, i);
861 emitIf(out, "if (@1 == 0) ",
862 smapper.popI(), i, indx, topMostLabel);
867 int indx = i + readShortArg(byteCodes, i);
868 emitIf(out, "if (@1 != 0) ",
869 smapper.popI(), i, indx, topMostLabel);
874 int indx = i + readShortArg(byteCodes, i);
875 emitIf(out, "if (@1 < 0) ",
876 smapper.popI(), i, indx, topMostLabel);
881 int indx = i + readShortArg(byteCodes, i);
882 emitIf(out, "if (@1 <= 0) ",
883 smapper.popI(), i, indx, topMostLabel);
888 int indx = i + readShortArg(byteCodes, i);
889 emitIf(out, "if (@1 > 0) ",
890 smapper.popI(), i, indx, topMostLabel);
895 int indx = i + readShortArg(byteCodes, i);
896 emitIf(out, "if (@1 >= 0) ",
897 smapper.popI(), i, indx, topMostLabel);
901 case opc_ifnonnull: {
902 int indx = i + readShortArg(byteCodes, i);
903 emitIf(out, "if (@1 !== null) ",
904 smapper.popA(), i, indx, topMostLabel);
909 int indx = i + readShortArg(byteCodes, i);
910 emitIf(out, "if (@1 === null) ",
911 smapper.popA(), i, indx, topMostLabel);
916 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
920 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
924 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
928 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
932 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
936 int indx = i + readShortArg(byteCodes, i);
937 goTo(out, i, indx, topMostLabel);
941 case opc_lookupswitch: {
942 i = generateLookupSwitch(i, byteCodes, smapper, topMostLabel);
945 case opc_tableswitch: {
946 i = generateTableSwitch(i, byteCodes, smapper, topMostLabel);
949 case opc_invokeinterface: {
950 i = invokeVirtualMethod(byteCodes, i, smapper) + 2;
953 case opc_invokevirtual:
954 i = invokeVirtualMethod(byteCodes, i, smapper);
956 case opc_invokespecial:
957 i = invokeStaticMethod(byteCodes, i, smapper, false);
959 case opc_invokestatic:
960 i = invokeStaticMethod(byteCodes, i, smapper, true);
963 int indx = readUShortArg(byteCodes, i);
964 String ci = jc.getClassName(indx);
965 emit(out, "var @1 = new @2;",
966 smapper.pushA(), accessClass(ci.replace('/', '_')));
972 int atype = readUByte(byteCodes, ++i);
973 generateNewArray(atype, smapper);
975 case opc_anewarray: {
976 int type = readUShortArg(byteCodes, i);
978 generateANewArray(type, smapper);
981 case opc_multianewarray: {
982 int type = readUShortArg(byteCodes, i);
984 i = generateMultiANewArray(type, byteCodes, i, smapper);
987 case opc_arraylength:
988 emit(out, "var @2 = @1.length;",
989 smapper.popA(), smapper.pushI());
992 emit(out, "@3.at(@2, @1);",
993 smapper.popL(), smapper.popI(), smapper.popA());
996 emit(out, "@3.at(@2, @1);",
997 smapper.popF(), smapper.popI(), smapper.popA());
1000 emit(out, "@3.at(@2, @1);",
1001 smapper.popD(), smapper.popI(), smapper.popA());
1004 emit(out, "@3.at(@2, @1);",
1005 smapper.popA(), smapper.popI(), smapper.popA());
1011 emit(out, "@3.at(@2, @1);",
1012 smapper.popI(), smapper.popI(), smapper.popA());
1015 emit(out, "var @3 = @2.at(@1);",
1016 smapper.popI(), smapper.popA(), smapper.pushL());
1019 emit(out, "var @3 = @2.at(@1);",
1020 smapper.popI(), smapper.popA(), smapper.pushF());
1023 emit(out, "var @3 = @2.at(@1);",
1024 smapper.popI(), smapper.popA(), smapper.pushD());
1027 emit(out, "var @3 = @2.at(@1);",
1028 smapper.popI(), smapper.popA(), smapper.pushA());
1034 emit(out, "var @3 = @2.at(@1);",
1035 smapper.popI(), smapper.popA(), smapper.pushI());
1043 final Variable v = smapper.get(0);
1044 emit(out, "var @1 = @2;", smapper.pushT(v.getType()), v);
1048 final Variable vi1 = smapper.get(0);
1050 if (vi1.isCategory2()) {
1051 emit(out, "var @1 = @2;",
1052 smapper.pushT(vi1.getType()), vi1);
1054 final Variable vi2 = smapper.get(1);
1055 emit(out, "var @1 = @2, @3 = @4;",
1056 smapper.pushT(vi2.getType()), vi2,
1057 smapper.pushT(vi1.getType()), vi1);
1062 final Variable vi1 = smapper.pop();
1063 final Variable vi2 = smapper.pop();
1064 final Variable vo3 = smapper.pushT(vi1.getType());
1065 final Variable vo2 = smapper.pushT(vi2.getType());
1066 final Variable vo1 = smapper.pushT(vi1.getType());
1068 emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
1069 vo1, vi1, vo2, vi2, vo3, vo1);
1073 final Variable vi1 = smapper.pop();
1074 final Variable vi2 = smapper.pop();
1076 if (vi1.isCategory2()) {
1077 final Variable vo3 = smapper.pushT(vi1.getType());
1078 final Variable vo2 = smapper.pushT(vi2.getType());
1079 final Variable vo1 = smapper.pushT(vi1.getType());
1081 emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
1082 vo1, vi1, vo2, vi2, vo3, vo1);
1084 final Variable vi3 = smapper.pop();
1085 final Variable vo5 = smapper.pushT(vi2.getType());
1086 final Variable vo4 = smapper.pushT(vi1.getType());
1087 final Variable vo3 = smapper.pushT(vi3.getType());
1088 final Variable vo2 = smapper.pushT(vi2.getType());
1089 final Variable vo1 = smapper.pushT(vi1.getType());
1091 emit(out, "var @1 = @2, @3 = @4, @5 = @6,",
1092 vo1, vi1, vo2, vi2, vo3, vi3);
1093 emit(out, " @1 = @2, @3 = @4;",
1094 vo4, vo1, vo5, vo2);
1099 final Variable vi1 = smapper.pop();
1100 final Variable vi2 = smapper.pop();
1102 if (vi2.isCategory2()) {
1103 final Variable vo3 = smapper.pushT(vi1.getType());
1104 final Variable vo2 = smapper.pushT(vi2.getType());
1105 final Variable vo1 = smapper.pushT(vi1.getType());
1107 emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
1108 vo1, vi1, vo2, vi2, vo3, vo1);
1110 final Variable vi3 = smapper.pop();
1111 final Variable vo4 = smapper.pushT(vi1.getType());
1112 final Variable vo3 = smapper.pushT(vi3.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, @7 = @8;",
1117 vo1, vi1, vo2, vi2, vo3, vi3, vo4, vo1);
1122 final Variable vi1 = smapper.pop();
1123 final Variable vi2 = smapper.pop();
1125 if (vi1.isCategory2()) {
1126 if (vi2.isCategory2()) {
1127 final Variable vo3 = smapper.pushT(vi1.getType());
1128 final Variable vo2 = smapper.pushT(vi2.getType());
1129 final Variable vo1 = smapper.pushT(vi1.getType());
1131 emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
1132 vo1, vi1, vo2, vi2, vo3, vo1);
1134 final Variable vi3 = smapper.pop();
1135 final Variable vo4 = smapper.pushT(vi1.getType());
1136 final Variable vo3 = smapper.pushT(vi3.getType());
1137 final Variable vo2 = smapper.pushT(vi2.getType());
1138 final Variable vo1 = smapper.pushT(vi1.getType());
1140 emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8;",
1141 vo1, vi1, vo2, vi2, vo3, vi3, vo4, vo1);
1144 final Variable vi3 = smapper.pop();
1146 if (vi3.isCategory2()) {
1147 final Variable vo5 = smapper.pushT(vi2.getType());
1148 final Variable vo4 = smapper.pushT(vi1.getType());
1149 final Variable vo3 = smapper.pushT(vi3.getType());
1150 final Variable vo2 = smapper.pushT(vi2.getType());
1151 final Variable vo1 = smapper.pushT(vi1.getType());
1153 emit(out, "var @1 = @2, @3 = @4, @5 = @6,",
1154 vo1, vi1, vo2, vi2, vo3, vi3);
1155 emit(out, " @1 = @2, @3 = @4;",
1156 vo4, vo1, vo5, vo2);
1158 final Variable vi4 = smapper.pop();
1159 final Variable vo6 = smapper.pushT(vi2.getType());
1160 final Variable vo5 = smapper.pushT(vi1.getType());
1161 final Variable vo4 = smapper.pushT(vi4.getType());
1162 final Variable vo3 = smapper.pushT(vi3.getType());
1163 final Variable vo2 = smapper.pushT(vi2.getType());
1164 final Variable vo1 = smapper.pushT(vi1.getType());
1166 emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8,",
1167 vo1, vi1, vo2, vi2, vo3, vi3, vo4, vi4);
1168 emit(out, " @1 = @2, @3 = @4;",
1169 vo5, vo1, vo6, vo2);
1175 final Variable vi1 = smapper.get(0);
1176 final Variable vi2 = smapper.get(1);
1178 if (vi1.getType() == vi2.getType()) {
1179 final Variable tmp = smapper.pushT(vi1.getType());
1181 emit(out, "var @1 = @2, @2 = @3, @3 = @1;",
1186 smapper.pushT(vi1.getType());
1187 smapper.pushT(vi2.getType());
1192 emit(out, "var @1 = @2;",
1193 smapper.pushI(), Integer.toString(byteCodes[++i]));
1196 emit(out, "var @1 = @2;",
1198 Integer.toString(readShortArg(byteCodes, i)));
1201 case opc_getfield: {
1202 int indx = readUShortArg(byteCodes, i);
1203 String[] fi = jc.getFieldInfoName(indx);
1204 final int type = VarType.fromFieldType(fi[2].charAt(0));
1205 final String mangleClass = mangleSig(fi[0]);
1206 final String mangleClassAccess = accessClass(mangleClass);
1207 emit(out, "var @2 = @3.call(@1);",
1209 smapper.pushT(type),
1210 accessMember(mangleClassAccess + "(false)",
1215 case opc_putfield: {
1216 int indx = readUShortArg(byteCodes, i);
1217 String[] fi = jc.getFieldInfoName(indx);
1218 final int type = VarType.fromFieldType(fi[2].charAt(0));
1219 final String mangleClass = mangleSig(fi[0]);
1220 final String mangleClassAccess = accessClass(mangleClass);
1221 emit(out, "@3.call(@2, @1);",
1224 accessMember(mangleClassAccess + "(false)",
1229 case opc_getstatic: {
1230 int indx = readUShortArg(byteCodes, i);
1231 String[] fi = jc.getFieldInfoName(indx);
1232 final int type = VarType.fromFieldType(fi[2].charAt(0));
1233 emit(out, "var @1 = @2();",
1234 smapper.pushT(type),
1235 accessMember(accessClass(fi[0].replace('/', '_'))
1239 addReference(fi[0]);
1242 case opc_putstatic: {
1243 int indx = readUShortArg(byteCodes, i);
1244 String[] fi = jc.getFieldInfoName(indx);
1245 final int type = VarType.fromFieldType(fi[2].charAt(0));
1246 emit(out, "@1(@2);",
1247 accessMember(accessClass(fi[0].replace('/', '_'))
1250 smapper.popT(type));
1252 addReference(fi[0]);
1255 case opc_checkcast: {
1256 int indx = readUShortArg(byteCodes, i);
1257 generateCheckcast(indx, smapper);
1261 case opc_instanceof: {
1262 int indx = readUShortArg(byteCodes, i);
1263 generateInstanceOf(indx, smapper);
1268 final Variable v = smapper.popA();
1271 emit(out, "{ var @1 = @2; throw @2; }",
1272 smapper.pushA(), v);
1276 case opc_monitorenter: {
1277 out.append("/* monitor enter */");
1282 case opc_monitorexit: {
1283 out.append("/* monitor exit */");
1294 emit(out, "throw 'unknown bytecode @1';",
1295 Integer.toString(c));
1299 generateByteCodeComment(prev, i, byteCodes);
1303 if (previousTrap != null) {
1304 generateCatch(previousTrap, byteCodes.length, topMostLabel);
1306 out.append("\n }\n");
1307 while (openBraces-- > 0) {
1313 private int generateIf(byte[] byteCodes, int i, final Variable v2, final Variable v1, final String test, int topMostLabel) throws IOException {
1314 int indx = i + readShortArg(byteCodes, i);
1315 out.append("if (").append(v1)
1316 .append(' ').append(test).append(' ')
1317 .append(v2).append(") ");
1318 goTo(out, i, indx, topMostLabel);
1322 private int readInt4(byte[] byteCodes, int offset) {
1323 final int d = byteCodes[offset + 0] << 24;
1324 final int c = byteCodes[offset + 1] << 16;
1325 final int b = byteCodes[offset + 2] << 8;
1326 final int a = byteCodes[offset + 3];
1327 return (d & 0xff000000) | (c & 0xff0000) | (b & 0xff00) | (a & 0xff);
1329 private static int readUByte(byte[] byteCodes, int offset) {
1330 return byteCodes[offset] & 0xff;
1333 private static int readUShort(byte[] byteCodes, int offset) {
1334 return ((byteCodes[offset] & 0xff) << 8)
1335 | (byteCodes[offset + 1] & 0xff);
1337 private static int readUShortArg(byte[] byteCodes, int offsetInstruction) {
1338 return readUShort(byteCodes, offsetInstruction + 1);
1341 private static int readShort(byte[] byteCodes, int offset) {
1342 int signed = byteCodes[offset];
1343 byte b0 = (byte)signed;
1344 return (b0 << 8) | (byteCodes[offset + 1] & 0xff);
1346 private static int readShortArg(byte[] byteCodes, int offsetInstruction) {
1347 return readShort(byteCodes, offsetInstruction + 1);
1350 private static void countArgs(String descriptor, char[] returnType, StringBuilder sig, StringBuilder cnt) {
1352 Boolean count = null;
1353 boolean array = false;
1355 int firstPos = sig.length();
1356 while (i < descriptor.length()) {
1357 char ch = descriptor.charAt(i++);
1378 if (ch == 'J' || ch == 'D') {
1384 sig.insert(firstPos, ch);
1386 returnType[0] = '[';
1387 sig.insert(firstPos, "_3");
1396 returnType[0] = 'V';
1397 sig.insert(firstPos, 'V');
1400 int next = descriptor.indexOf(';', i);
1401 String realSig = mangleSig(descriptor, i - 1, next + 1);
1406 sig.append(realSig);
1409 sig.insert(firstPos, realSig);
1411 sig.insert(firstPos, "_3");
1413 returnType[0] = 'L';
1422 throw new IllegalStateException("Invalid char: " + ch);
1427 static String mangleSig(String sig) {
1428 return mangleSig(sig, 0, sig.length());
1431 private static String mangleSig(String txt, int first, int last) {
1432 StringBuilder sb = new StringBuilder();
1433 for (int i = first; i < last; i++) {
1434 final char ch = txt.charAt(i);
1436 case '/': sb.append('_'); break;
1437 case '_': sb.append("_1"); break;
1438 case ';': sb.append("_2"); break;
1439 case '[': sb.append("_3"); break;
1440 default: sb.append(ch); break;
1443 return sb.toString();
1446 private static String findMethodName(MethodData m, StringBuilder cnt) {
1447 StringBuilder name = new StringBuilder();
1448 if ("<init>".equals(m.getName())) { // NOI18N
1449 name.append("cons"); // NOI18N
1450 } else if ("<clinit>".equals(m.getName())) { // NOI18N
1451 name.append("class"); // NOI18N
1453 name.append(m.getName());
1456 countArgs(m.getInternalSig(), new char[1], name, cnt);
1457 return name.toString();
1460 static String findMethodName(String[] mi, StringBuilder cnt, char[] returnType) {
1461 StringBuilder name = new StringBuilder();
1462 String descr = mi[2];//mi.getDescriptor();
1464 if ("<init>".equals(nm)) { // NOI18N
1465 name.append("cons"); // NOI18N
1469 countArgs(descr, returnType, name, cnt);
1470 return name.toString();
1473 private int invokeStaticMethod(byte[] byteCodes, int i, final StackMapper mapper, boolean isStatic)
1474 throws IOException {
1475 int methodIndex = readUShortArg(byteCodes, i);
1476 String[] mi = jc.getFieldInfoName(methodIndex);
1477 char[] returnType = { 'V' };
1478 StringBuilder cnt = new StringBuilder();
1479 String mn = findMethodName(mi, cnt, returnType);
1481 final int numArguments = isStatic ? cnt.length() : cnt.length() + 1;
1482 final Variable[] vars = new Variable[numArguments];
1484 for (int j = numArguments - 1; j >= 0; --j) {
1485 vars[j] = mapper.pop();
1488 if (returnType[0] != 'V') {
1490 .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
1494 final String in = mi[0];
1495 out.append(accessMember(
1496 accessClass(in.replace('/', '_')) + "(false)",
1497 mn.startsWith("cons_")
1498 ? "constructor." + mn
1504 out.append(".call(");
1506 if (numArguments > 0) {
1507 out.append(vars[0]);
1508 for (int j = 1; j < numArguments; ++j) {
1510 out.append(vars[j]);
1518 private int invokeVirtualMethod(byte[] byteCodes, int i, final StackMapper mapper)
1519 throws IOException {
1520 int methodIndex = readUShortArg(byteCodes, i);
1521 String[] mi = jc.getFieldInfoName(methodIndex);
1522 char[] returnType = { 'V' };
1523 StringBuilder cnt = new StringBuilder();
1524 String mn = findMethodName(mi, cnt, returnType);
1526 final int numArguments = cnt.length() + 1;
1527 final Variable[] vars = new Variable[numArguments];
1529 for (int j = numArguments - 1; j >= 0; --j) {
1530 vars[j] = mapper.pop();
1533 if (returnType[0] != 'V') {
1535 .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
1539 out.append(accessMember(vars[0].toString(), mn, mi));
1542 for (int j = 1; j < numArguments; ++j) {
1544 out.append(vars[j]);
1552 private void addReference(String cn) throws IOException {
1553 if (requireReference(cn)) {
1554 debug(" /* needs " + cn + " */");
1558 private void outType(String d, StringBuilder out) {
1560 while (d.charAt(0) == '[') {
1564 if (d.charAt(0) == 'L') {
1565 assert d.charAt(d.length() - 1) == ';';
1566 out.append(d.replace('/', '_').substring(0, d.length() - 1));
1572 private String encodeConstant(int entryIndex) throws IOException {
1573 String[] classRef = { null };
1574 String s = jc.stringValue(entryIndex, classRef);
1575 if (classRef[0] != null) {
1576 if (classRef[0].startsWith("[")) {
1577 s = accessClass("java_lang_Class") + "(false).forName__Ljava_lang_Class_2Ljava_lang_String_2('" + classRef[0] + "');";
1579 addReference(classRef[0]);
1580 s = accessClass(s.replace('/', '_')) + "(false).constructor.$class";
1586 private String javaScriptBody(String destObject, MethodData m, boolean isStatic) throws IOException {
1587 byte[] arr = m.findAnnotationData(true);
1591 final String jvmType = "Lorg/apidesign/bck2brwsr/core/JavaScriptBody;";
1592 class P extends AnnotationParser {
1598 String[] args = new String[30];
1602 protected void visitAttr(String type, String attr, String at, String value) {
1603 if (type.equals(jvmType)) {
1604 if ("body".equals(attr)) {
1606 } else if ("args".equals(attr)) {
1607 args[cnt++] = value;
1609 throw new IllegalArgumentException(attr);
1616 if (p.body == null) {
1619 StringBuilder cnt = new StringBuilder();
1620 final String mn = findMethodName(m, cnt);
1621 out.append(destObject).append(".").append(mn);
1622 out.append(" = function(");
1625 for (int i = 0; i < cnt.length(); i++) {
1627 space = outputArg(out, p.args, index);
1630 out.append(") {").append("\n");
1632 out.append("\n}\n");
1635 private static String className(ClassData jc) {
1636 //return jc.getName().getInternalName().replace('/', '_');
1637 return jc.getClassName().replace('/', '_');
1640 private static String[] findAnnotation(
1641 byte[] arr, ClassData cd, final String className,
1642 final String... attrNames
1643 ) throws IOException {
1647 final String[] values = new String[attrNames.length];
1648 final boolean[] found = { false };
1649 final String jvmType = "L" + className.replace('.', '/') + ";";
1650 AnnotationParser ap = new AnnotationParser(false, true) {
1652 protected void visitAttr(String type, String attr, String at, String value) {
1653 if (type.equals(jvmType)) {
1655 for (int i = 0; i < attrNames.length; i++) {
1656 if (attrNames[i].equals(attr)) {
1665 return found[0] ? values : null;
1668 private CharSequence initField(FieldData v) {
1669 final String is = v.getInternalSig();
1670 if (is.length() == 1) {
1671 switch (is.charAt(0)) {
1677 case 'I': return " = 0;";
1679 case 'D': return " = 0.0;";
1681 throw new IllegalStateException(is);
1687 private void generateAnno(ClassData cd, final Appendable out, byte[] data) throws IOException {
1688 AnnotationParser ap = new AnnotationParser(true, false) {
1689 int[] cnt = new int[32];
1693 protected void visitAnnotationStart(String attrType, boolean top) throws IOException {
1694 final String slashType = attrType.substring(1, attrType.length() - 1);
1695 requireReference(slashType);
1697 if (cnt[depth]++ > 0) {
1701 out.append('"').append(attrType).append("\" : ");
1708 protected void visitAnnotationEnd(String type, boolean top) throws IOException {
1709 out.append("\n}\n");
1714 protected void visitValueStart(String attrName, char type) throws IOException {
1715 if (cnt[depth]++ > 0) {
1719 if (attrName != null) {
1720 out.append(attrName).append(" : ");
1728 protected void visitValueEnd(String attrName, char type) throws IOException {
1736 protected void visitAttr(String type, String attr, String attrType, String value)
1737 throws IOException {
1738 if (attr == null && value == null) {
1745 protected void visitEnumAttr(String type, String attr, String attrType, String value)
1746 throws IOException {
1747 final String slashType = attrType.substring(1, attrType.length() - 1);
1748 requireReference(slashType);
1750 out.append(accessClass(slashType.replace('/', '_')))
1751 .append("(false).constructor.").append(value);
1757 private static String outputArg(Appendable out, String[] args, int indx) throws IOException {
1758 final String name = args[indx];
1762 if (name.contains(",")) {
1763 throw new IOException("Wrong parameter with ',': " + name);
1769 private static void emit(final Appendable out,
1770 final String format,
1771 final CharSequence... params) throws IOException {
1772 final int length = format.length();
1775 int paramOffset = format.indexOf('@');
1776 while ((paramOffset != -1) && (paramOffset < (length - 1))) {
1777 final char paramChar = format.charAt(paramOffset + 1);
1778 if ((paramChar >= '1') && (paramChar <= '9')) {
1779 final int paramIndex = paramChar - '0' - 1;
1781 out.append(format, processed, paramOffset);
1782 out.append(params[paramIndex]);
1785 processed = paramOffset + 1;
1788 paramOffset = format.indexOf('@', paramOffset + 1);
1791 out.append(format, processed, length);
1794 private void generateCatch(TrapData[] traps, int current, int topMostLabel) throws IOException {
1795 out.append("} catch (e) {\n");
1797 for (TrapData e : traps) {
1801 if (e.catch_cpx != 0) { //not finally
1802 final String classInternalName = jc.getClassName(e.catch_cpx);
1803 addReference(classInternalName);
1804 if ("java/lang/Throwable".equals(classInternalName)) {
1805 out.append("if (e['$instOf_java_lang_Throwable']) {");
1806 out.append(" var stA0 = e;");
1807 out.append("} else {");
1808 out.append(" var stA0 = vm.java_lang_Throwable(true);");
1809 out.append(" vm.java_lang_Throwable.cons__VLjava_lang_String_2.call(stA0, e.toString());");
1811 goTo(out, current, e.handler_pc, topMostLabel);
1813 out.append("if (e['$instOf_" + classInternalName.replace('/', '_') + "']) {");
1814 out.append("var stA0 = e;");
1815 goTo(out, current, e.handler_pc, topMostLabel);
1819 finallyPC = e.handler_pc;
1822 if (finallyPC == -1) {
1823 out.append("throw e;");
1825 out.append("var stA0 = e;");
1826 goTo(out, current, finallyPC, topMostLabel);
1831 private static void goTo(Appendable out, int current, int to, int canBack) throws IOException {
1834 out.append("{ gt = 0; continue X_" + to + "; }");
1836 out.append("{ gt = " + to + "; continue X_0; }");
1839 out.append("{ gt = " + to + "; break IF; }");
1843 private static void emitIf(
1844 Appendable out, String pattern, Variable param,
1845 int current, int to, int canBack
1846 ) throws IOException {
1847 emit(out, pattern, param);
1848 goTo(out, current, to, canBack);
1851 private void generateNewArray(int atype, final StackMapper smapper) throws IOException, IllegalStateException {
1854 case 4: jvmType = "[Z"; break;
1855 case 5: jvmType = "[C"; break;
1856 case 6: jvmType = "[F"; break;
1857 case 7: jvmType = "[D"; break;
1858 case 8: jvmType = "[B"; break;
1859 case 9: jvmType = "[S"; break;
1860 case 10: jvmType = "[I"; break;
1861 case 11: jvmType = "[J"; break;
1862 default: throw new IllegalStateException("Array type: " + atype);
1864 emit(out, "var @2 = Array.prototype.newArray__Ljava_lang_Object_2ZLjava_lang_String_2I(true, '@3', @1);",
1865 smapper.popI(), smapper.pushA(), jvmType);
1868 private void generateANewArray(int type, final StackMapper smapper) throws IOException {
1869 String typeName = jc.getClassName(type);
1870 if (typeName.startsWith("[")) {
1871 typeName = "[" + typeName;
1873 typeName = "[L" + typeName + ";";
1875 emit(out, "var @2 = Array.prototype.newArray__Ljava_lang_Object_2ZLjava_lang_String_2I(false, '@3', @1);",
1876 smapper.popI(), smapper.pushA(), typeName);
1879 private int generateMultiANewArray(int type, final byte[] byteCodes, int i, final StackMapper smapper) throws IOException {
1880 String typeName = jc.getClassName(type);
1881 int dim = readUByte(byteCodes, ++i);
1882 StringBuilder dims = new StringBuilder();
1884 for (int d = 0; d < dim; d++) {
1886 dims.insert(1, ",");
1888 dims.insert(1, smapper.popI());
1891 emit(out, "var @2 = Array.prototype.multiNewArray__Ljava_lang_Object_2Ljava_lang_String_2_3II('@3', @1, 0);",
1892 dims.toString(), smapper.pushA(), typeName);
1896 private int generateTableSwitch(int i, final byte[] byteCodes, final StackMapper smapper, int topMostLabel) throws IOException {
1897 int table = i / 4 * 4 + 4;
1898 int dflt = i + readInt4(byteCodes, table);
1900 int low = readInt4(byteCodes, table);
1902 int high = readInt4(byteCodes, table);
1904 out.append("switch (").append(smapper.popI()).append(") {\n");
1905 while (low <= high) {
1906 int offset = i + readInt4(byteCodes, table);
1908 out.append(" case " + low).append(":"); goTo(out, i, offset, topMostLabel); out.append('\n');
1911 out.append(" default: ");
1912 goTo(out, i, dflt, topMostLabel);
1918 private int generateLookupSwitch(int i, final byte[] byteCodes, final StackMapper smapper, int topMostLabel) throws IOException {
1919 int table = i / 4 * 4 + 4;
1920 int dflt = i + readInt4(byteCodes, table);
1922 int n = readInt4(byteCodes, table);
1924 out.append("switch (").append(smapper.popI()).append(") {\n");
1926 int cnstnt = readInt4(byteCodes, table);
1928 int offset = i + readInt4(byteCodes, table);
1930 out.append(" case " + cnstnt).append(": "); goTo(out, i, offset, topMostLabel); out.append('\n');
1932 out.append(" default: ");
1933 goTo(out, i, dflt, topMostLabel);
1939 private void generateInstanceOf(int indx, final StackMapper smapper) throws IOException {
1940 final String type = jc.getClassName(indx);
1941 if (!type.startsWith("[")) {
1942 emit(out, "var @2 = @1 != null && @1['$instOf_@3'] ? 1 : 0;",
1943 smapper.popA(), smapper.pushI(),
1944 type.replace('/', '_'));
1946 emit(out, "var @2 = vm.java_lang_Class(false).forName__Ljava_lang_Class_2Ljava_lang_String_2('@3').isInstance__ZLjava_lang_Object_2(@1);",
1947 smapper.popA(), smapper.pushI(),
1953 private void generateCheckcast(int indx, final StackMapper smapper) throws IOException {
1954 final String type = jc.getClassName(indx);
1955 if (!type.startsWith("[")) {
1957 "if (@1 !== null && !@1['$instOf_@2']) throw vm.java_lang_ClassCastException(true);",
1958 smapper.getA(0), type.replace('/', '_'));
1960 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);",
1961 smapper.getA(0), type
1966 private void generateByteCodeComment(int prev, int i, final byte[] byteCodes) throws IOException {
1967 for (int j = prev; j <= i; j++) {
1969 final int cc = readUByte(byteCodes, j);
1970 out.append(Integer.toString(cc));