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 protected void declaredField(FieldData fieldData,
68 String mangledName) throws IOException {
71 protected void declaredMethod(MethodData methodData,
73 String mangledName) throws IOException {
76 /** Prints out a debug message.
78 * @param msg the message
79 * @return true if the message has been printed
82 boolean debug(String msg) throws IOException {
88 * Converts a given class file to a JavaScript version.
90 * @param classFile input stream with code of the .class file
91 * @return the initialization code for this class, if any. Otherwise <code>null</code>
93 * @throws IOException if something goes wrong during read or write or translating
96 public String compile(InputStream classFile) throws IOException {
97 this.jc = new ClassData(classFile);
98 if (jc.getMajor_version() < 50) {
99 throw new IOException("Can't compile " + jc.getClassName() + ". Class file version " + jc.getMajor_version() + "."
100 + jc.getMinor_version() + " - recompile with -target 1.6 (at least)."
103 byte[] arrData = jc.findAnnotationData(true);
104 String[] arr = findAnnotation(arrData, jc,
105 "org.apidesign.bck2brwsr.core.ExtraJavaScript",
106 "resource", "processByteCode"
109 if (!arr[0].isEmpty()) {
110 requireScript(arr[0]);
112 if ("0".equals(arr[1])) {
116 String[] proto = findAnnotation(arrData, jc,
117 "org.apidesign.bck2brwsr.core.JavaScriptPrototype",
118 "container", "prototype"
120 StringArray toInitilize = new StringArray();
121 final String className = className(jc);
122 out.append("\n\n").append(assignClass(className));
123 out.append("function ").append(className).append("() {");
124 out.append("\n var CLS = ").append(className).append(';');
125 out.append("\n if (!CLS.$class) {");
127 String sc = jc.getSuperClassName(); // with _
128 out.append("\n var pp = ").
129 append(accessClass(sc.replace('/', '_'))).append("(true);");
130 out.append("\n var p = CLS.prototype = pp;");
131 out.append("\n var c = p;");
132 out.append("\n var sprcls = pp.constructor.$class;");
134 out.append("\n var p = CLS.prototype = ").append(proto[1]).append(";");
135 if (proto[0] == null) {
138 out.append("\n var c = ").append(proto[0]).append(";");
139 out.append("\n var sprcls = null;");
141 for (FieldData v : jc.getFields()) {
143 out.append("\n CLS.").append(v.getName()).append(initField(v));
144 out.append("\n c._").append(v.getName()).append(" = function (v) {")
145 .append(" if (arguments.length == 1) CLS.").append(v.getName())
146 .append(" = v; return CLS.").
147 append(v.getName()).append("; };");
149 out.append("\n c._").append(v.getName()).append(" = function (v) {")
150 .append(" if (arguments.length == 1) this.fld_").
151 append(className).append('_').append(v.getName())
152 .append(" = v; return this.fld_").
153 append(className).append('_').append(v.getName())
157 declaredField(v, "c", "_" + v.getName());
159 for (MethodData m : jc.getMethods()) {
160 byte[] onlyArr = m.findAnnotationData(true);
161 String[] only = findAnnotation(onlyArr, jc,
162 "org.apidesign.bck2brwsr.core.JavaScriptOnly",
166 if (only[0] != null && only[1] != null) {
167 out.append("\n p.").append(only[0]).append(" = ")
168 .append(only[1]).append(";");
177 mn = generateStaticMethod(destObject, m, toInitilize);
179 if (m.isConstructor()) {
181 mn = generateInstanceMethod(destObject, m);
184 mn = generateInstanceMethod(destObject, m);
187 declaredMethod(m, destObject, mn);
188 byte[] runAnno = m.findAnnotationData(false);
189 if (runAnno != null) {
190 out.append("\n ").append(destObject).append(".").append(mn).append(".anno = {");
191 generateAnno(jc, out, runAnno);
194 out.append("\n ").append(destObject).append(".").append(mn).append(".access = " + m.getAccess()).append(";");
195 out.append("\n ").append(destObject).append(".").append(mn).append(".cls = CLS;");
197 out.append("\n c.constructor = CLS;");
198 out.append("\n c['$instOf_").append(className).append("'] = true;");
199 for (String superInterface : jc.getSuperInterfaces()) {
200 out.append("\n c['$instOf_").append(superInterface.replace('/', '_')).append("'] = true;");
202 out.append("\n CLS.$class = 'temp';");
203 out.append("\n CLS.$class = ");
204 out.append(accessClass("java_lang_Class(true);"));
205 out.append("\n CLS.$class.jvmName = '").append(jc.getClassName()).append("';");
206 out.append("\n CLS.$class.superclass = sprcls;");
207 out.append("\n CLS.$class.access = ").append(jc.getAccessFlags()+";");
208 out.append("\n CLS.$class.cnstr = CLS;");
209 byte[] classAnno = jc.findAnnotationData(false);
210 if (classAnno != null) {
211 out.append("\n CLS.$class.anno = {");
212 generateAnno(jc, out, classAnno);
215 for (String init : toInitilize.toArray()) {
216 out.append("\n ").append(init).append("();");
219 out.append("\n if (arguments.length === 0) {");
220 out.append("\n if (!(this instanceof CLS)) {");
221 out.append("\n return new CLS();");
223 for (FieldData v : jc.getFields()) {
224 byte[] onlyArr = v.findAnnotationData(true);
225 String[] only = findAnnotation(onlyArr, jc,
226 "org.apidesign.bck2brwsr.core.JavaScriptOnly",
230 if (only[0] != null && only[1] != null) {
231 out.append("\n p.").append(only[0]).append(" = ")
232 .append(only[1]).append(";");
237 out.append("\n this.fld_").
238 append(className).append('_').
239 append(v.getName()).append(initField(v));
242 out.append("\n return this;");
244 out.append("\n return arguments[0] ? new CLS() : CLS.prototype;");
247 declaredClass(jc, className);
249 // StringBuilder sb = new StringBuilder();
250 // for (String init : toInitilize.toArray()) {
251 // sb.append("\n").append(init).append("();");
255 private String generateStaticMethod(String destObject, MethodData m, StringArray toInitilize) throws IOException {
256 String jsb = javaScriptBody(destObject, m, true);
260 final String mn = findMethodName(m, new StringBuilder());
261 if (mn.equals("class__V")) {
262 toInitilize.add(accessClass(className(jc)) + "(false)." + mn);
264 generateMethod(destObject, mn, m);
268 private String generateInstanceMethod(String destObject, MethodData m) throws IOException {
269 String jsb = javaScriptBody(destObject, m, false);
273 final String mn = findMethodName(m, new StringBuilder());
274 generateMethod(destObject, mn, m);
278 private void generateMethod(String destObject, String name, MethodData m)
280 final StackMapIterator stackMapIterator = m.createStackMapIterator();
281 TrapDataIterator trap = m.getTrapDataIterator();
282 final LocalsMapper lmapper =
283 new LocalsMapper(stackMapIterator.getArguments());
285 out.append(destObject).append(".").append(name).append(" = function(");
286 lmapper.outputArguments(out, m.isStatic());
287 out.append(") {").append("\n");
289 final byte[] byteCodes = m.getCode();
290 if (byteCodes == null) {
291 out.append(" throw 'no code found for ")
292 .append(jc.getClassName()).append('.')
293 .append(m.getName()).append("';\n");
298 final StackMapper smapper = new StackMapper();
301 out.append(" var ").append(" lcA0 = this;\n");
304 int lastStackFrame = -1;
305 TrapData[] previousTrap = null;
306 boolean wide = false;
308 out.append("\n var gt = 0;\n");
310 int topMostLabel = 0;
311 for (int i = 0; i < byteCodes.length; i++) {
313 stackMapIterator.advanceTo(i);
314 boolean changeInCatch = trap.advanceTo(i);
315 if (changeInCatch || lastStackFrame != stackMapIterator.getFrameIndex()) {
316 if (previousTrap != null) {
317 generateCatch(previousTrap, i, topMostLabel);
321 if (lastStackFrame != stackMapIterator.getFrameIndex()) {
325 if (openBraces > 64) {
326 for (int c = 0; c < 64; c++) {
327 out.append("break;}\n");
333 lastStackFrame = stackMapIterator.getFrameIndex();
334 lmapper.syncWithFrameLocals(stackMapIterator.getFrameLocals());
335 smapper.syncWithFrameStack(stackMapIterator.getFrameStack());
336 out.append(" X_" + i).append(": for (;;) { IF: if (gt <= " + i + ") {\n");
338 changeInCatch = true;
340 debug(" /* " + i + " */ ");
342 if (changeInCatch && trap.useTry()) {
344 previousTrap = trap.current();
346 final int c = readUByte(byteCodes, i);
349 emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(0));
352 emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(0));
355 emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(0));
358 emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(0));
361 emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(0));
364 emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(1));
367 emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(1));
370 emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(1));
373 emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(1));
376 emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(1));
379 emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(2));
382 emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(2));
385 emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(2));
388 emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(2));
391 emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(2));
394 emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(3));
397 emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(3));
400 emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(3));
403 emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(3));
406 emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(3));
410 final int indx = wide ? readUShort(byteCodes, i++)
411 : readUByte(byteCodes, i);
413 emit(out, "var @1 = @2;",
414 smapper.pushI(), lmapper.getI(indx));
419 final int indx = wide ? readUShort(byteCodes, i++)
420 : readUByte(byteCodes, i);
422 emit(out, "var @1 = @2;",
423 smapper.pushL(), lmapper.getL(indx));
428 final int indx = wide ? readUShort(byteCodes, i++)
429 : readUByte(byteCodes, i);
431 emit(out, "var @1 = @2;",
432 smapper.pushF(), lmapper.getF(indx));
437 final int indx = wide ? readUShort(byteCodes, i++)
438 : readUByte(byteCodes, i);
440 emit(out, "var @1 = @2;",
441 smapper.pushD(), lmapper.getD(indx));
446 final int indx = wide ? readUShort(byteCodes, i++)
447 : readUByte(byteCodes, i);
449 emit(out, "var @1 = @2;",
450 smapper.pushA(), lmapper.getA(indx));
455 final int indx = wide ? readUShort(byteCodes, i++)
456 : readUByte(byteCodes, i);
458 emit(out, "var @1 = @2;",
459 lmapper.setI(indx), smapper.popI());
464 final int indx = wide ? readUShort(byteCodes, i++)
465 : readUByte(byteCodes, i);
467 emit(out, "var @1 = @2;",
468 lmapper.setL(indx), smapper.popL());
473 final int indx = wide ? readUShort(byteCodes, i++)
474 : readUByte(byteCodes, i);
476 emit(out, "var @1 = @2;",
477 lmapper.setF(indx), smapper.popF());
482 final int indx = wide ? readUShort(byteCodes, i++)
483 : readUByte(byteCodes, i);
485 emit(out, "var @1 = @2;",
486 lmapper.setD(indx), smapper.popD());
491 final int indx = wide ? readUShort(byteCodes, i++)
492 : readUByte(byteCodes, i);
494 emit(out, "var @1 = @2;",
495 lmapper.setA(indx), smapper.popA());
499 emit(out, "var @1 = @2;", lmapper.setA(0), smapper.popA());
502 emit(out, "var @1 = @2;", lmapper.setI(0), smapper.popI());
505 emit(out, "var @1 = @2;", lmapper.setL(0), smapper.popL());
508 emit(out, "var @1 = @2;", lmapper.setF(0), smapper.popF());
511 emit(out, "var @1 = @2;", lmapper.setD(0), smapper.popD());
514 emit(out, "var @1 = @2;", lmapper.setA(1), smapper.popA());
517 emit(out, "var @1 = @2;", lmapper.setI(1), smapper.popI());
520 emit(out, "var @1 = @2;", lmapper.setL(1), smapper.popL());
523 emit(out, "var @1 = @2;", lmapper.setF(1), smapper.popF());
526 emit(out, "var @1 = @2;", lmapper.setD(1), smapper.popD());
529 emit(out, "var @1 = @2;", lmapper.setA(2), smapper.popA());
532 emit(out, "var @1 = @2;", lmapper.setI(2), smapper.popI());
535 emit(out, "var @1 = @2;", lmapper.setL(2), smapper.popL());
538 emit(out, "var @1 = @2;", lmapper.setF(2), smapper.popF());
541 emit(out, "var @1 = @2;", lmapper.setD(2), smapper.popD());
544 emit(out, "var @1 = @2;", lmapper.setA(3), smapper.popA());
547 emit(out, "var @1 = @2;", lmapper.setI(3), smapper.popI());
550 emit(out, "var @1 = @2;", lmapper.setL(3), smapper.popL());
553 emit(out, "var @1 = @2;", lmapper.setF(3), smapper.popF());
556 emit(out, "var @1 = @2;", lmapper.setD(3), smapper.popD());
559 emit(out, "@1 = @1.add32(@2);", smapper.getI(1), smapper.popI());
562 emit(out, "@1 = @1.add64(@2);", smapper.getL(1), smapper.popL());
565 emit(out, "@1 += @2;", smapper.getF(1), smapper.popF());
568 emit(out, "@1 += @2;", smapper.getD(1), smapper.popD());
571 emit(out, "@1 = @1.sub32(@2);", smapper.getI(1), smapper.popI());
574 emit(out, "@1 = @1.sub64(@2);", smapper.getL(1), smapper.popL());
577 emit(out, "@1 -= @2;", smapper.getF(1), smapper.popF());
580 emit(out, "@1 -= @2;", smapper.getD(1), smapper.popD());
583 emit(out, "@1 = @1.mul32(@2);", smapper.getI(1), smapper.popI());
586 emit(out, "@1 = @1.mul64(@2);", smapper.getL(1), smapper.popL());
589 emit(out, "@1 *= @2;", smapper.getF(1), smapper.popF());
592 emit(out, "@1 *= @2;", smapper.getD(1), smapper.popD());
595 emit(out, "@1 = @1.div32(@2);",
596 smapper.getI(1), smapper.popI());
599 emit(out, "@1 = @1.div64(@2);",
600 smapper.getL(1), smapper.popL());
603 emit(out, "@1 /= @2;", smapper.getF(1), smapper.popF());
606 emit(out, "@1 /= @2;", smapper.getD(1), smapper.popD());
609 emit(out, "@1 = @1.mod32(@2);",
610 smapper.getI(1), smapper.popI());
613 emit(out, "@1 = @1.mod64(@2);",
614 smapper.getL(1), smapper.popL());
617 emit(out, "@1 %= @2;", smapper.getF(1), smapper.popF());
620 emit(out, "@1 %= @2;", smapper.getD(1), smapper.popD());
623 emit(out, "@1 &= @2;", smapper.getI(1), smapper.popI());
626 emit(out, "@1 = @1.and64(@2);", smapper.getL(1), smapper.popL());
629 emit(out, "@1 |= @2;", smapper.getI(1), smapper.popI());
632 emit(out, "@1 = @1.or64(@2);", smapper.getL(1), smapper.popL());
635 emit(out, "@1 ^= @2;", smapper.getI(1), smapper.popI());
638 emit(out, "@1 = @1.xor64(@2);", smapper.getL(1), smapper.popL());
641 emit(out, "@1 = @1.neg32();", smapper.getI(0));
644 emit(out, "@1 = @1.neg64();", smapper.getL(0));
647 emit(out, "@1 = -@1;", smapper.getF(0));
650 emit(out, "@1 = -@1;", smapper.getD(0));
653 emit(out, "@1 <<= @2;", smapper.getI(1), smapper.popI());
656 emit(out, "@1 = @1.shl64(@2);", smapper.getL(1), smapper.popI());
659 emit(out, "@1 >>= @2;", smapper.getI(1), smapper.popI());
662 emit(out, "@1 = @1.shr64(@2);", smapper.getL(1), smapper.popI());
665 emit(out, "@1 >>>= @2;", smapper.getI(1), smapper.popI());
668 emit(out, "@1 = @1.ushr64(@2);", smapper.getL(1), smapper.popI());
672 final int varIndx = wide ? readUShort(byteCodes, i++)
673 : readUByte(byteCodes, i);
675 final int incrBy = wide ? readShort(byteCodes, i++)
679 emit(out, "@1++;", lmapper.getI(varIndx));
681 emit(out, "@1 += @2;",
682 lmapper.getI(varIndx),
683 Integer.toString(incrBy));
688 emit(out, "return;");
691 emit(out, "return @1;", smapper.popI());
694 emit(out, "return @1;", smapper.popL());
697 emit(out, "return @1;", smapper.popF());
700 emit(out, "return @1;", smapper.popD());
703 emit(out, "return @1;", smapper.popA());
706 emit(out, "var @2 = @1;", smapper.popI(), smapper.pushL());
709 emit(out, "var @2 = @1;", smapper.popI(), smapper.pushF());
712 emit(out, "var @2 = @1;", smapper.popI(), smapper.pushD());
715 emit(out, "var @2 = @1.toInt32();", smapper.popL(), smapper.pushI());
719 emit(out, "var @2 = @1.toFP();", smapper.popL(), smapper.pushF());
722 emit(out, "var @2 = @1.toFP();", smapper.popL(), smapper.pushD());
725 emit(out, "var @2 = @1;", smapper.popF(), smapper.pushD());
728 emit(out, "var @2 = @1;", smapper.popD(), smapper.pushF());
731 emit(out, "var @2 = @1.toInt32();",
732 smapper.popF(), smapper.pushI());
735 emit(out, "var @2 = @1.toLong();",
736 smapper.popF(), smapper.pushL());
739 emit(out, "var @2 = @1.toInt32();",
740 smapper.popD(), smapper.pushI());
743 emit(out, "var @2 = @1.toLong();",
744 smapper.popD(), smapper.pushL());
747 emit(out, "var @1 = @1.toInt8();", smapper.getI(0));
750 out.append("{ /* number conversion */ }");
753 emit(out, "var @1 = @1.toInt16();", smapper.getI(0));
755 case opc_aconst_null:
756 emit(out, "var @1 = null;", smapper.pushA());
759 emit(out, "var @1 = -1;", smapper.pushI());
762 emit(out, "var @1 = 0;", smapper.pushI());
765 emit(out, "var @1 = 0;", smapper.pushD());
768 emit(out, "var @1 = 0;", smapper.pushL());
771 emit(out, "var @1 = 0;", smapper.pushF());
774 emit(out, "var @1 = 1;", smapper.pushI());
777 emit(out, "var @1 = 1;", smapper.pushL());
780 emit(out, "var @1 = 1;", smapper.pushF());
783 emit(out, "var @1 = 1;", smapper.pushD());
786 emit(out, "var @1 = 2;", smapper.pushI());
789 emit(out, "var @1 = 2;", smapper.pushF());
792 emit(out, "var @1 = 3;", smapper.pushI());
795 emit(out, "var @1 = 4;", smapper.pushI());
798 emit(out, "var @1 = 5;", smapper.pushI());
801 int indx = readUByte(byteCodes, ++i);
802 String v = encodeConstant(indx);
803 int type = VarType.fromConstantType(jc.getTag(indx));
804 emit(out, "var @1 = @2;", smapper.pushT(type), v);
809 int indx = readUShortArg(byteCodes, i);
811 String v = encodeConstant(indx);
812 int type = VarType.fromConstantType(jc.getTag(indx));
813 if (type == VarType.LONG) {
814 final Long lv = new Long(v);
815 final int low = (int)(lv.longValue() & 0xFFFFFFFF);
816 final int hi = (int)(lv.longValue() >> 32);
817 emit(out, "var @1 = 0x@3.next32(0x@2);", smapper.pushL(),
818 Integer.toHexString(low), Integer.toHexString(hi));
820 emit(out, "var @1 = @2;", smapper.pushT(type), v);
825 emit(out, "var @3 = @2.compare64(@1);",
826 smapper.popL(), smapper.popL(), smapper.pushI());
830 emit(out, "var @3 = (@2 == @1) ? 0 : ((@2 < @1) ? -1 : 1);",
831 smapper.popF(), smapper.popF(), smapper.pushI());
835 emit(out, "var @3 = (@2 == @1) ? 0 : ((@2 < @1) ? -1 : 1);",
836 smapper.popD(), smapper.popD(), smapper.pushI());
839 i = generateIf(byteCodes, i, smapper.popA(), smapper.popA(),
840 "===", topMostLabel);
843 i = generateIf(byteCodes, i, smapper.popA(), smapper.popA(),
844 "!==", topMostLabel);
847 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
851 int indx = i + readShortArg(byteCodes, i);
852 emitIf(out, "if (@1 == 0) ",
853 smapper.popI(), i, indx, topMostLabel);
858 int indx = i + readShortArg(byteCodes, i);
859 emitIf(out, "if (@1 != 0) ",
860 smapper.popI(), i, indx, topMostLabel);
865 int indx = i + readShortArg(byteCodes, i);
866 emitIf(out, "if (@1 < 0) ",
867 smapper.popI(), i, indx, topMostLabel);
872 int indx = i + readShortArg(byteCodes, i);
873 emitIf(out, "if (@1 <= 0) ",
874 smapper.popI(), i, indx, topMostLabel);
879 int indx = i + readShortArg(byteCodes, i);
880 emitIf(out, "if (@1 > 0) ",
881 smapper.popI(), i, indx, topMostLabel);
886 int indx = i + readShortArg(byteCodes, i);
887 emitIf(out, "if (@1 >= 0) ",
888 smapper.popI(), i, indx, topMostLabel);
892 case opc_ifnonnull: {
893 int indx = i + readShortArg(byteCodes, i);
894 emitIf(out, "if (@1 !== null) ",
895 smapper.popA(), i, indx, topMostLabel);
900 int indx = i + readShortArg(byteCodes, i);
901 emitIf(out, "if (@1 === null) ",
902 smapper.popA(), i, indx, topMostLabel);
907 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
911 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
915 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
919 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
923 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
927 int indx = i + readShortArg(byteCodes, i);
928 goTo(out, i, indx, topMostLabel);
932 case opc_lookupswitch: {
933 i = generateLookupSwitch(i, byteCodes, smapper, topMostLabel);
936 case opc_tableswitch: {
937 i = generateTableSwitch(i, byteCodes, smapper, topMostLabel);
940 case opc_invokeinterface: {
941 i = invokeVirtualMethod(byteCodes, i, smapper) + 2;
944 case opc_invokevirtual:
945 i = invokeVirtualMethod(byteCodes, i, smapper);
947 case opc_invokespecial:
948 i = invokeStaticMethod(byteCodes, i, smapper, false);
950 case opc_invokestatic:
951 i = invokeStaticMethod(byteCodes, i, smapper, true);
954 int indx = readUShortArg(byteCodes, i);
955 String ci = jc.getClassName(indx);
956 emit(out, "var @1 = new @2;",
957 smapper.pushA(), accessClass(ci.replace('/', '_')));
963 int atype = readUByte(byteCodes, ++i);
964 generateNewArray(atype, smapper);
966 case opc_anewarray: {
967 int type = readUShortArg(byteCodes, i);
969 generateANewArray(type, smapper);
972 case opc_multianewarray: {
973 int type = readUShortArg(byteCodes, i);
975 i = generateMultiANewArray(type, byteCodes, i, smapper);
978 case opc_arraylength:
979 emit(out, "var @2 = @1.length;",
980 smapper.popA(), smapper.pushI());
983 emit(out, "@3.at(@2, @1);",
984 smapper.popL(), smapper.popI(), smapper.popA());
987 emit(out, "@3.at(@2, @1);",
988 smapper.popF(), smapper.popI(), smapper.popA());
991 emit(out, "@3.at(@2, @1);",
992 smapper.popD(), smapper.popI(), smapper.popA());
995 emit(out, "@3.at(@2, @1);",
996 smapper.popA(), smapper.popI(), smapper.popA());
1002 emit(out, "@3.at(@2, @1);",
1003 smapper.popI(), smapper.popI(), smapper.popA());
1006 emit(out, "var @3 = @2.at(@1);",
1007 smapper.popI(), smapper.popA(), smapper.pushL());
1010 emit(out, "var @3 = @2.at(@1);",
1011 smapper.popI(), smapper.popA(), smapper.pushF());
1014 emit(out, "var @3 = @2.at(@1);",
1015 smapper.popI(), smapper.popA(), smapper.pushD());
1018 emit(out, "var @3 = @2.at(@1);",
1019 smapper.popI(), smapper.popA(), smapper.pushA());
1025 emit(out, "var @3 = @2.at(@1);",
1026 smapper.popI(), smapper.popA(), smapper.pushI());
1034 final Variable v = smapper.get(0);
1035 emit(out, "var @1 = @2;", smapper.pushT(v.getType()), v);
1039 final Variable vi1 = smapper.get(0);
1041 if (vi1.isCategory2()) {
1042 emit(out, "var @1 = @2;",
1043 smapper.pushT(vi1.getType()), vi1);
1045 final Variable vi2 = smapper.get(1);
1046 emit(out, "var @1 = @2, @3 = @4;",
1047 smapper.pushT(vi2.getType()), vi2,
1048 smapper.pushT(vi1.getType()), vi1);
1053 final Variable vi1 = smapper.pop();
1054 final Variable vi2 = smapper.pop();
1055 final Variable vo3 = smapper.pushT(vi1.getType());
1056 final Variable vo2 = smapper.pushT(vi2.getType());
1057 final Variable vo1 = smapper.pushT(vi1.getType());
1059 emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
1060 vo1, vi1, vo2, vi2, vo3, vo1);
1064 final Variable vi1 = smapper.pop();
1065 final Variable vi2 = smapper.pop();
1067 if (vi1.isCategory2()) {
1068 final Variable vo3 = smapper.pushT(vi1.getType());
1069 final Variable vo2 = smapper.pushT(vi2.getType());
1070 final Variable vo1 = smapper.pushT(vi1.getType());
1072 emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
1073 vo1, vi1, vo2, vi2, vo3, vo1);
1075 final Variable vi3 = smapper.pop();
1076 final Variable vo5 = smapper.pushT(vi2.getType());
1077 final Variable vo4 = smapper.pushT(vi1.getType());
1078 final Variable vo3 = smapper.pushT(vi3.getType());
1079 final Variable vo2 = smapper.pushT(vi2.getType());
1080 final Variable vo1 = smapper.pushT(vi1.getType());
1082 emit(out, "var @1 = @2, @3 = @4, @5 = @6,",
1083 vo1, vi1, vo2, vi2, vo3, vi3);
1084 emit(out, " @1 = @2, @3 = @4;",
1085 vo4, vo1, vo5, vo2);
1090 final Variable vi1 = smapper.pop();
1091 final Variable vi2 = smapper.pop();
1093 if (vi2.isCategory2()) {
1094 final Variable vo3 = smapper.pushT(vi1.getType());
1095 final Variable vo2 = smapper.pushT(vi2.getType());
1096 final Variable vo1 = smapper.pushT(vi1.getType());
1098 emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
1099 vo1, vi1, vo2, vi2, vo3, vo1);
1101 final Variable vi3 = smapper.pop();
1102 final Variable vo4 = smapper.pushT(vi1.getType());
1103 final Variable vo3 = smapper.pushT(vi3.getType());
1104 final Variable vo2 = smapper.pushT(vi2.getType());
1105 final Variable vo1 = smapper.pushT(vi1.getType());
1107 emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8;",
1108 vo1, vi1, vo2, vi2, vo3, vi3, vo4, vo1);
1113 final Variable vi1 = smapper.pop();
1114 final Variable vi2 = smapper.pop();
1116 if (vi1.isCategory2()) {
1117 if (vi2.isCategory2()) {
1118 final Variable vo3 = smapper.pushT(vi1.getType());
1119 final Variable vo2 = smapper.pushT(vi2.getType());
1120 final Variable vo1 = smapper.pushT(vi1.getType());
1122 emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
1123 vo1, vi1, vo2, vi2, vo3, vo1);
1125 final Variable vi3 = smapper.pop();
1126 final Variable vo4 = smapper.pushT(vi1.getType());
1127 final Variable vo3 = smapper.pushT(vi3.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, @7 = @8;",
1132 vo1, vi1, vo2, vi2, vo3, vi3, vo4, vo1);
1135 final Variable vi3 = smapper.pop();
1137 if (vi3.isCategory2()) {
1138 final Variable vo5 = smapper.pushT(vi2.getType());
1139 final Variable vo4 = smapper.pushT(vi1.getType());
1140 final Variable vo3 = smapper.pushT(vi3.getType());
1141 final Variable vo2 = smapper.pushT(vi2.getType());
1142 final Variable vo1 = smapper.pushT(vi1.getType());
1144 emit(out, "var @1 = @2, @3 = @4, @5 = @6,",
1145 vo1, vi1, vo2, vi2, vo3, vi3);
1146 emit(out, " @1 = @2, @3 = @4;",
1147 vo4, vo1, vo5, vo2);
1149 final Variable vi4 = smapper.pop();
1150 final Variable vo6 = smapper.pushT(vi2.getType());
1151 final Variable vo5 = smapper.pushT(vi1.getType());
1152 final Variable vo4 = smapper.pushT(vi4.getType());
1153 final Variable vo3 = smapper.pushT(vi3.getType());
1154 final Variable vo2 = smapper.pushT(vi2.getType());
1155 final Variable vo1 = smapper.pushT(vi1.getType());
1157 emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8,",
1158 vo1, vi1, vo2, vi2, vo3, vi3, vo4, vi4);
1159 emit(out, " @1 = @2, @3 = @4;",
1160 vo5, vo1, vo6, vo2);
1166 final Variable vi1 = smapper.get(0);
1167 final Variable vi2 = smapper.get(1);
1169 if (vi1.getType() == vi2.getType()) {
1170 final Variable tmp = smapper.pushT(vi1.getType());
1172 emit(out, "var @1 = @2, @2 = @3, @3 = @1;",
1177 smapper.pushT(vi1.getType());
1178 smapper.pushT(vi2.getType());
1183 emit(out, "var @1 = @2;",
1184 smapper.pushI(), Integer.toString(byteCodes[++i]));
1187 emit(out, "var @1 = @2;",
1189 Integer.toString(readShortArg(byteCodes, i)));
1192 case opc_getfield: {
1193 int indx = readUShortArg(byteCodes, i);
1194 String[] fi = jc.getFieldInfoName(indx);
1195 final int type = VarType.fromFieldType(fi[2].charAt(0));
1196 final String mangleClass = mangleSig(fi[0]);
1197 final String mangleClassAccess = accessClass(mangleClass);
1198 emit(out, "var @2 = @4(false)._@3.call(@1);",
1200 smapper.pushT(type), fi[1], mangleClassAccess
1205 case opc_putfield: {
1206 int indx = readUShortArg(byteCodes, i);
1207 String[] fi = jc.getFieldInfoName(indx);
1208 final int type = VarType.fromFieldType(fi[2].charAt(0));
1209 final String mangleClass = mangleSig(fi[0]);
1210 final String mangleClassAccess = accessClass(mangleClass);
1211 emit(out, "@4(false)._@3.call(@2, @1);",
1213 smapper.popA(), fi[1],
1219 case opc_getstatic: {
1220 int indx = readUShortArg(byteCodes, i);
1221 String[] fi = jc.getFieldInfoName(indx);
1222 final int type = VarType.fromFieldType(fi[2].charAt(0));
1223 emit(out, "var @1 = @2(false)._@3();",
1224 smapper.pushT(type),
1225 accessClass(fi[0].replace('/', '_')), fi[1]);
1227 addReference(fi[0]);
1230 case opc_putstatic: {
1231 int indx = readUShortArg(byteCodes, i);
1232 String[] fi = jc.getFieldInfoName(indx);
1233 final int type = VarType.fromFieldType(fi[2].charAt(0));
1234 emit(out, "@1(false)._@2(@3);",
1235 accessClass(fi[0].replace('/', '_')), fi[1],
1236 smapper.popT(type));
1238 addReference(fi[0]);
1241 case opc_checkcast: {
1242 int indx = readUShortArg(byteCodes, i);
1243 generateCheckcast(indx, smapper);
1247 case opc_instanceof: {
1248 int indx = readUShortArg(byteCodes, i);
1249 generateInstanceOf(indx, smapper);
1254 final Variable v = smapper.popA();
1257 emit(out, "{ var @1 = @2; throw @2; }",
1258 smapper.pushA(), v);
1262 case opc_monitorenter: {
1263 out.append("/* monitor enter */");
1268 case opc_monitorexit: {
1269 out.append("/* monitor exit */");
1280 emit(out, "throw 'unknown bytecode @1';",
1281 Integer.toString(c));
1285 generateByteCodeComment(prev, i, byteCodes);
1289 if (previousTrap != null) {
1290 generateCatch(previousTrap, byteCodes.length, topMostLabel);
1292 out.append("\n }\n");
1293 while (openBraces-- > 0) {
1299 private int generateIf(byte[] byteCodes, int i, final Variable v2, final Variable v1, final String test, int topMostLabel) throws IOException {
1300 int indx = i + readShortArg(byteCodes, i);
1301 out.append("if (").append(v1)
1302 .append(' ').append(test).append(' ')
1303 .append(v2).append(") ");
1304 goTo(out, i, indx, topMostLabel);
1308 private int readInt4(byte[] byteCodes, int offset) {
1309 final int d = byteCodes[offset + 0] << 24;
1310 final int c = byteCodes[offset + 1] << 16;
1311 final int b = byteCodes[offset + 2] << 8;
1312 final int a = byteCodes[offset + 3];
1313 return (d & 0xff000000) | (c & 0xff0000) | (b & 0xff00) | (a & 0xff);
1315 private static int readUByte(byte[] byteCodes, int offset) {
1316 return byteCodes[offset] & 0xff;
1319 private static int readUShort(byte[] byteCodes, int offset) {
1320 return ((byteCodes[offset] & 0xff) << 8)
1321 | (byteCodes[offset + 1] & 0xff);
1323 private static int readUShortArg(byte[] byteCodes, int offsetInstruction) {
1324 return readUShort(byteCodes, offsetInstruction + 1);
1327 private static int readShort(byte[] byteCodes, int offset) {
1328 int signed = byteCodes[offset];
1329 byte b0 = (byte)signed;
1330 return (b0 << 8) | (byteCodes[offset + 1] & 0xff);
1332 private static int readShortArg(byte[] byteCodes, int offsetInstruction) {
1333 return readShort(byteCodes, offsetInstruction + 1);
1336 private static void countArgs(String descriptor, char[] returnType, StringBuilder sig, StringBuilder cnt) {
1338 Boolean count = null;
1339 boolean array = false;
1341 int firstPos = sig.length();
1342 while (i < descriptor.length()) {
1343 char ch = descriptor.charAt(i++);
1364 if (ch == 'J' || ch == 'D') {
1370 sig.insert(firstPos, ch);
1372 returnType[0] = '[';
1373 sig.insert(firstPos, "_3");
1382 returnType[0] = 'V';
1383 sig.insert(firstPos, 'V');
1386 int next = descriptor.indexOf(';', i);
1387 String realSig = mangleSig(descriptor, i - 1, next + 1);
1392 sig.append(realSig);
1395 sig.insert(firstPos, realSig);
1397 sig.insert(firstPos, "_3");
1399 returnType[0] = 'L';
1408 throw new IllegalStateException("Invalid char: " + ch);
1413 static String mangleSig(String sig) {
1414 return mangleSig(sig, 0, sig.length());
1417 private static String mangleSig(String txt, int first, int last) {
1418 StringBuilder sb = new StringBuilder();
1419 for (int i = first; i < last; i++) {
1420 final char ch = txt.charAt(i);
1422 case '/': sb.append('_'); break;
1423 case '_': sb.append("_1"); break;
1424 case ';': sb.append("_2"); break;
1425 case '[': sb.append("_3"); break;
1426 default: sb.append(ch); break;
1429 return sb.toString();
1432 private static String findMethodName(MethodData m, StringBuilder cnt) {
1433 StringBuilder name = new StringBuilder();
1434 if ("<init>".equals(m.getName())) { // NOI18N
1435 name.append("cons"); // NOI18N
1436 } else if ("<clinit>".equals(m.getName())) { // NOI18N
1437 name.append("class"); // NOI18N
1439 name.append(m.getName());
1442 countArgs(m.getInternalSig(), new char[1], name, cnt);
1443 return name.toString();
1446 static String findMethodName(String[] mi, StringBuilder cnt, char[] returnType) {
1447 StringBuilder name = new StringBuilder();
1448 String descr = mi[2];//mi.getDescriptor();
1450 if ("<init>".equals(nm)) { // NOI18N
1451 name.append("cons"); // NOI18N
1455 countArgs(descr, returnType, name, cnt);
1456 return name.toString();
1459 private int invokeStaticMethod(byte[] byteCodes, int i, final StackMapper mapper, boolean isStatic)
1460 throws IOException {
1461 int methodIndex = readUShortArg(byteCodes, i);
1462 String[] mi = jc.getFieldInfoName(methodIndex);
1463 char[] returnType = { 'V' };
1464 StringBuilder cnt = new StringBuilder();
1465 String mn = findMethodName(mi, cnt, returnType);
1467 final int numArguments = isStatic ? cnt.length() : cnt.length() + 1;
1468 final Variable[] vars = new Variable[numArguments];
1470 for (int j = numArguments - 1; j >= 0; --j) {
1471 vars[j] = mapper.pop();
1474 if (returnType[0] != 'V') {
1476 .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
1480 final String in = mi[0];
1481 out.append(accessClass(in.replace('/', '_')));
1482 out.append("(false).");
1483 if (mn.startsWith("cons_")) {
1484 out.append("constructor.");
1490 out.append(".call(");
1492 if (numArguments > 0) {
1493 out.append(vars[0]);
1494 for (int j = 1; j < numArguments; ++j) {
1496 out.append(vars[j]);
1504 private int invokeVirtualMethod(byte[] byteCodes, int i, final StackMapper mapper)
1505 throws IOException {
1506 int methodIndex = readUShortArg(byteCodes, i);
1507 String[] mi = jc.getFieldInfoName(methodIndex);
1508 char[] returnType = { 'V' };
1509 StringBuilder cnt = new StringBuilder();
1510 String mn = findMethodName(mi, cnt, returnType);
1512 final int numArguments = cnt.length() + 1;
1513 final Variable[] vars = new Variable[numArguments];
1515 for (int j = numArguments - 1; j >= 0; --j) {
1516 vars[j] = mapper.pop();
1519 if (returnType[0] != 'V') {
1521 .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
1525 out.append(vars[0]).append('.');
1529 for (int j = 1; j < numArguments; ++j) {
1531 out.append(vars[j]);
1539 private void addReference(String cn) throws IOException {
1540 if (requireReference(cn)) {
1541 debug(" /* needs " + cn + " */");
1545 private void outType(String d, StringBuilder out) {
1547 while (d.charAt(0) == '[') {
1551 if (d.charAt(0) == 'L') {
1552 assert d.charAt(d.length() - 1) == ';';
1553 out.append(d.replace('/', '_').substring(0, d.length() - 1));
1559 private String encodeConstant(int entryIndex) throws IOException {
1560 String[] classRef = { null };
1561 String s = jc.stringValue(entryIndex, classRef);
1562 if (classRef[0] != null) {
1563 if (classRef[0].startsWith("[")) {
1564 s = accessClass("java_lang_Class") + "(false).forName__Ljava_lang_Class_2Ljava_lang_String_2('" + classRef[0] + "');";
1566 addReference(classRef[0]);
1567 s = accessClass(s.replace('/', '_')) + "(false).constructor.$class";
1573 private String javaScriptBody(String destObject, MethodData m, boolean isStatic) throws IOException {
1574 byte[] arr = m.findAnnotationData(true);
1578 final String jvmType = "Lorg/apidesign/bck2brwsr/core/JavaScriptBody;";
1579 class P extends AnnotationParser {
1585 String[] args = new String[30];
1589 protected void visitAttr(String type, String attr, String at, String value) {
1590 if (type.equals(jvmType)) {
1591 if ("body".equals(attr)) {
1593 } else if ("args".equals(attr)) {
1594 args[cnt++] = value;
1596 throw new IllegalArgumentException(attr);
1603 if (p.body == null) {
1606 StringBuilder cnt = new StringBuilder();
1607 final String mn = findMethodName(m, cnt);
1608 out.append(destObject).append(".").append(mn);
1609 out.append(" = function(");
1612 for (int i = 0; i < cnt.length(); i++) {
1614 space = outputArg(out, p.args, index);
1617 out.append(") {").append("\n");
1619 out.append("\n}\n");
1622 private static String className(ClassData jc) {
1623 //return jc.getName().getInternalName().replace('/', '_');
1624 return jc.getClassName().replace('/', '_');
1627 private static String[] findAnnotation(
1628 byte[] arr, ClassData cd, final String className,
1629 final String... attrNames
1630 ) throws IOException {
1634 final String[] values = new String[attrNames.length];
1635 final boolean[] found = { false };
1636 final String jvmType = "L" + className.replace('.', '/') + ";";
1637 AnnotationParser ap = new AnnotationParser(false, true) {
1639 protected void visitAttr(String type, String attr, String at, String value) {
1640 if (type.equals(jvmType)) {
1642 for (int i = 0; i < attrNames.length; i++) {
1643 if (attrNames[i].equals(attr)) {
1652 return found[0] ? values : null;
1655 private CharSequence initField(FieldData v) {
1656 final String is = v.getInternalSig();
1657 if (is.length() == 1) {
1658 switch (is.charAt(0)) {
1664 case 'I': return " = 0;";
1666 case 'D': return " = 0.0;";
1668 throw new IllegalStateException(is);
1674 private void generateAnno(ClassData cd, final Appendable out, byte[] data) throws IOException {
1675 AnnotationParser ap = new AnnotationParser(true, false) {
1676 int[] cnt = new int[32];
1680 protected void visitAnnotationStart(String attrType, boolean top) throws IOException {
1681 final String slashType = attrType.substring(1, attrType.length() - 1);
1682 requireReference(slashType);
1684 if (cnt[depth]++ > 0) {
1688 out.append('"').append(attrType).append("\" : ");
1695 protected void visitAnnotationEnd(String type, boolean top) throws IOException {
1696 out.append("\n}\n");
1701 protected void visitValueStart(String attrName, char type) throws IOException {
1702 if (cnt[depth]++ > 0) {
1706 if (attrName != null) {
1707 out.append(attrName).append(" : ");
1715 protected void visitValueEnd(String attrName, char type) throws IOException {
1723 protected void visitAttr(String type, String attr, String attrType, String value)
1724 throws IOException {
1725 if (attr == null && value == null) {
1732 protected void visitEnumAttr(String type, String attr, String attrType, String value)
1733 throws IOException {
1734 final String slashType = attrType.substring(1, attrType.length() - 1);
1735 requireReference(slashType);
1737 out.append(accessClass(slashType.replace('/', '_')))
1738 .append("(false).constructor.").append(value);
1744 private static String outputArg(Appendable out, String[] args, int indx) throws IOException {
1745 final String name = args[indx];
1749 if (name.contains(",")) {
1750 throw new IOException("Wrong parameter with ',': " + name);
1756 private static void emit(final Appendable out,
1757 final String format,
1758 final CharSequence... params) throws IOException {
1759 final int length = format.length();
1762 int paramOffset = format.indexOf('@');
1763 while ((paramOffset != -1) && (paramOffset < (length - 1))) {
1764 final char paramChar = format.charAt(paramOffset + 1);
1765 if ((paramChar >= '1') && (paramChar <= '9')) {
1766 final int paramIndex = paramChar - '0' - 1;
1768 out.append(format, processed, paramOffset);
1769 out.append(params[paramIndex]);
1772 processed = paramOffset + 1;
1775 paramOffset = format.indexOf('@', paramOffset + 1);
1778 out.append(format, processed, length);
1781 private void generateCatch(TrapData[] traps, int current, int topMostLabel) throws IOException {
1782 out.append("} catch (e) {\n");
1784 for (TrapData e : traps) {
1788 if (e.catch_cpx != 0) { //not finally
1789 final String classInternalName = jc.getClassName(e.catch_cpx);
1790 addReference(classInternalName);
1791 if ("java/lang/Throwable".equals(classInternalName)) {
1792 out.append("if (e['$instOf_java_lang_Throwable']) {");
1793 out.append(" var stA0 = e;");
1794 out.append("} else {");
1795 out.append(" var stA0 = vm.java_lang_Throwable(true);");
1796 out.append(" vm.java_lang_Throwable.cons__VLjava_lang_String_2.call(stA0, e.toString());");
1798 goTo(out, current, e.handler_pc, topMostLabel);
1800 out.append("if (e['$instOf_" + classInternalName.replace('/', '_') + "']) {");
1801 out.append("var stA0 = e;");
1802 goTo(out, current, e.handler_pc, topMostLabel);
1806 finallyPC = e.handler_pc;
1809 if (finallyPC == -1) {
1810 out.append("throw e;");
1812 out.append("var stA0 = e;");
1813 goTo(out, current, finallyPC, topMostLabel);
1818 private static void goTo(Appendable out, int current, int to, int canBack) throws IOException {
1821 out.append("{ gt = 0; continue X_" + to + "; }");
1823 out.append("{ gt = " + to + "; continue X_0; }");
1826 out.append("{ gt = " + to + "; break IF; }");
1830 private static void emitIf(
1831 Appendable out, String pattern, Variable param,
1832 int current, int to, int canBack
1833 ) throws IOException {
1834 emit(out, pattern, param);
1835 goTo(out, current, to, canBack);
1838 private void generateNewArray(int atype, final StackMapper smapper) throws IOException, IllegalStateException {
1841 case 4: jvmType = "[Z"; break;
1842 case 5: jvmType = "[C"; break;
1843 case 6: jvmType = "[F"; break;
1844 case 7: jvmType = "[D"; break;
1845 case 8: jvmType = "[B"; break;
1846 case 9: jvmType = "[S"; break;
1847 case 10: jvmType = "[I"; break;
1848 case 11: jvmType = "[J"; break;
1849 default: throw new IllegalStateException("Array type: " + atype);
1851 emit(out, "var @2 = Array.prototype.newArray__Ljava_lang_Object_2ZLjava_lang_String_2I(true, '@3', @1);",
1852 smapper.popI(), smapper.pushA(), jvmType);
1855 private void generateANewArray(int type, final StackMapper smapper) throws IOException {
1856 String typeName = jc.getClassName(type);
1857 if (typeName.startsWith("[")) {
1858 typeName = "[" + typeName;
1860 typeName = "[L" + typeName + ";";
1862 emit(out, "var @2 = Array.prototype.newArray__Ljava_lang_Object_2ZLjava_lang_String_2I(false, '@3', @1);",
1863 smapper.popI(), smapper.pushA(), typeName);
1866 private int generateMultiANewArray(int type, final byte[] byteCodes, int i, final StackMapper smapper) throws IOException {
1867 String typeName = jc.getClassName(type);
1868 int dim = readUByte(byteCodes, ++i);
1869 StringBuilder dims = new StringBuilder();
1871 for (int d = 0; d < dim; d++) {
1873 dims.insert(1, ",");
1875 dims.insert(1, smapper.popI());
1878 emit(out, "var @2 = Array.prototype.multiNewArray__Ljava_lang_Object_2Ljava_lang_String_2_3II('@3', @1, 0);",
1879 dims.toString(), smapper.pushA(), typeName);
1883 private int generateTableSwitch(int i, final byte[] byteCodes, final StackMapper smapper, int topMostLabel) throws IOException {
1884 int table = i / 4 * 4 + 4;
1885 int dflt = i + readInt4(byteCodes, table);
1887 int low = readInt4(byteCodes, table);
1889 int high = readInt4(byteCodes, table);
1891 out.append("switch (").append(smapper.popI()).append(") {\n");
1892 while (low <= high) {
1893 int offset = i + readInt4(byteCodes, table);
1895 out.append(" case " + low).append(":"); goTo(out, i, offset, topMostLabel); out.append('\n');
1898 out.append(" default: ");
1899 goTo(out, i, dflt, topMostLabel);
1905 private int generateLookupSwitch(int i, final byte[] byteCodes, final StackMapper smapper, int topMostLabel) throws IOException {
1906 int table = i / 4 * 4 + 4;
1907 int dflt = i + readInt4(byteCodes, table);
1909 int n = readInt4(byteCodes, table);
1911 out.append("switch (").append(smapper.popI()).append(") {\n");
1913 int cnstnt = readInt4(byteCodes, table);
1915 int offset = i + readInt4(byteCodes, table);
1917 out.append(" case " + cnstnt).append(": "); goTo(out, i, offset, topMostLabel); out.append('\n');
1919 out.append(" default: ");
1920 goTo(out, i, dflt, topMostLabel);
1926 private void generateInstanceOf(int indx, final StackMapper smapper) throws IOException {
1927 final String type = jc.getClassName(indx);
1928 if (!type.startsWith("[")) {
1929 emit(out, "var @2 = @1 != null && @1['$instOf_@3'] ? 1 : 0;",
1930 smapper.popA(), smapper.pushI(),
1931 type.replace('/', '_'));
1933 emit(out, "var @2 = vm.java_lang_Class(false).forName__Ljava_lang_Class_2Ljava_lang_String_2('@3').isInstance__ZLjava_lang_Object_2(@1);",
1934 smapper.popA(), smapper.pushI(),
1940 private void generateCheckcast(int indx, final StackMapper smapper) throws IOException {
1941 final String type = jc.getClassName(indx);
1942 if (!type.startsWith("[")) {
1944 "if (@1 !== null && !@1['$instOf_@2']) throw vm.java_lang_ClassCastException(true);",
1945 smapper.getA(0), type.replace('/', '_'));
1947 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);",
1948 smapper.getA(0), type
1953 private void generateByteCodeComment(int prev, int i, final byte[] byteCodes) throws IOException {
1954 for (int j = prev; j <= i; j++) {
1956 final int cc = readUByte(byteCodes, j);
1957 out.append(Integer.toString(cc));