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 accessField(String object, String mangledName,
63 String[] fieldInfoName) throws IOException {
64 return object + "." + mangledName;
67 protected String accessStaticMethod(
70 String[] fieldInfoName) throws IOException {
71 return object + "." + mangledName;
74 protected String accessVirtualMethod(
77 String[] fieldInfoName) throws IOException {
78 return object + "." + mangledName;
81 protected void declaredClass(ClassData classData, String mangledName)
85 protected void declaredField(FieldData fieldData,
87 String mangledName) throws IOException {
90 protected void declaredMethod(MethodData methodData,
92 String mangledName) throws IOException {
95 /** Prints out a debug message.
97 * @param msg the message
98 * @return true if the message has been printed
101 boolean debug(String msg) throws IOException {
107 * Converts a given class file to a JavaScript version.
109 * @param classFile input stream with code of the .class file
110 * @return the initialization code for this class, if any. Otherwise <code>null</code>
112 * @throws IOException if something goes wrong during read or write or translating
115 public String compile(InputStream classFile) throws IOException {
116 return compile(new ClassData(classFile));
119 protected String compile(ClassData classData) throws IOException {
121 if (jc.getMajor_version() < 50) {
122 throw new IOException("Can't compile " + jc.getClassName() + ". Class file version " + jc.getMajor_version() + "."
123 + jc.getMinor_version() + " - recompile with -target 1.6 (at least)."
126 byte[] arrData = jc.findAnnotationData(true);
127 String[] arr = findAnnotation(arrData, jc,
128 "org.apidesign.bck2brwsr.core.ExtraJavaScript",
129 "resource", "processByteCode"
132 if (!arr[0].isEmpty()) {
133 requireScript(arr[0]);
135 if ("0".equals(arr[1])) {
139 String[] proto = findAnnotation(arrData, jc,
140 "org.apidesign.bck2brwsr.core.JavaScriptPrototype",
141 "container", "prototype"
143 StringArray toInitilize = new StringArray();
144 final String className = className(jc);
145 out.append("\n\n").append(assignClass(className));
146 out.append("function ").append(className).append("() {");
147 out.append("\n var CLS = ").append(className).append(';');
148 out.append("\n if (!CLS.$class) {");
150 String sc = jc.getSuperClassName(); // with _
151 out.append("\n var pp = ").
152 append(accessClass(sc.replace('/', '_'))).append("(true);");
153 out.append("\n var p = CLS.prototype = pp;");
154 out.append("\n var c = p;");
155 out.append("\n var sprcls = pp.constructor.$class;");
157 out.append("\n var p = CLS.prototype = ").append(proto[1]).append(";");
158 if (proto[0] == null) {
161 out.append("\n var c = ").append(proto[0]).append(";");
162 out.append("\n var sprcls = null;");
164 for (FieldData v : jc.getFields()) {
166 out.append("\n CLS.").append(v.getName()).append(initField(v));
167 out.append("\n c._").append(v.getName()).append(" = function (v) {")
168 .append(" if (arguments.length == 1) CLS.").append(v.getName())
169 .append(" = v; return CLS.").
170 append(v.getName()).append("; };");
172 out.append("\n c._").append(v.getName()).append(" = function (v) {")
173 .append(" if (arguments.length == 1) this.fld_").
174 append(className).append('_').append(v.getName())
175 .append(" = v; return this.fld_").
176 append(className).append('_').append(v.getName())
180 declaredField(v, "c", "_" + v.getName());
182 for (MethodData m : jc.getMethods()) {
183 byte[] onlyArr = m.findAnnotationData(true);
184 String[] only = findAnnotation(onlyArr, jc,
185 "org.apidesign.bck2brwsr.core.JavaScriptOnly",
189 if (only[0] != null && only[1] != null) {
190 out.append("\n p.").append(only[0]).append(" = ")
191 .append(only[1]).append(";");
200 mn = generateStaticMethod(destObject, m, toInitilize);
202 if (m.isConstructor()) {
204 mn = generateInstanceMethod(destObject, m);
207 mn = generateInstanceMethod(destObject, m);
210 declaredMethod(m, destObject, mn);
211 byte[] runAnno = m.findAnnotationData(false);
212 if (runAnno != null) {
213 out.append("\n ").append(destObject).append(".").append(mn).append(".anno = {");
214 generateAnno(jc, out, runAnno);
217 out.append("\n ").append(destObject).append(".").append(mn).append(".access = " + m.getAccess()).append(";");
218 out.append("\n ").append(destObject).append(".").append(mn).append(".cls = CLS;");
220 out.append("\n c.constructor = CLS;");
221 out.append("\n c['$instOf_").append(className).append("'] = true;");
222 for (String superInterface : jc.getSuperInterfaces()) {
223 out.append("\n c['$instOf_").append(superInterface.replace('/', '_')).append("'] = true;");
225 out.append("\n CLS.$class = 'temp';");
226 out.append("\n CLS.$class = ");
227 out.append(accessClass("java_lang_Class(true);"));
228 out.append("\n CLS.$class.jvmName = '").append(jc.getClassName()).append("';");
229 out.append("\n CLS.$class.superclass = sprcls;");
230 out.append("\n CLS.$class.access = ").append(jc.getAccessFlags()+";");
231 out.append("\n CLS.$class.cnstr = CLS;");
232 byte[] classAnno = jc.findAnnotationData(false);
233 if (classAnno != null) {
234 out.append("\n CLS.$class.anno = {");
235 generateAnno(jc, out, classAnno);
238 for (String init : toInitilize.toArray()) {
239 out.append("\n ").append(init).append("();");
242 out.append("\n if (arguments.length === 0) {");
243 out.append("\n if (!(this instanceof CLS)) {");
244 out.append("\n return new CLS();");
246 for (FieldData v : jc.getFields()) {
247 byte[] onlyArr = v.findAnnotationData(true);
248 String[] only = findAnnotation(onlyArr, jc,
249 "org.apidesign.bck2brwsr.core.JavaScriptOnly",
253 if (only[0] != null && only[1] != null) {
254 out.append("\n p.").append(only[0]).append(" = ")
255 .append(only[1]).append(";");
260 out.append("\n this.fld_").
261 append(className).append('_').
262 append(v.getName()).append(initField(v));
265 out.append("\n return this;");
267 out.append("\n return arguments[0] ? new CLS() : CLS.prototype;");
270 declaredClass(jc, className);
272 // StringBuilder sb = new StringBuilder();
273 // for (String init : toInitilize.toArray()) {
274 // sb.append("\n").append(init).append("();");
278 private String generateStaticMethod(String destObject, MethodData m, StringArray toInitilize) throws IOException {
279 String jsb = javaScriptBody(destObject, m, true);
283 final String mn = findMethodName(m, new StringBuilder());
284 if (mn.equals("class__V")) {
285 toInitilize.add(accessClass(className(jc)) + "(false)." + mn);
287 generateMethod(destObject, mn, m);
291 private String generateInstanceMethod(String destObject, MethodData m) throws IOException {
292 String jsb = javaScriptBody(destObject, m, false);
296 final String mn = findMethodName(m, new StringBuilder());
297 generateMethod(destObject, mn, m);
301 private void generateMethod(String destObject, String name, MethodData m)
303 final StackMapIterator stackMapIterator = m.createStackMapIterator();
304 TrapDataIterator trap = m.getTrapDataIterator();
305 final LocalsMapper lmapper =
306 new LocalsMapper(stackMapIterator.getArguments());
308 out.append(destObject).append(".").append(name).append(" = function(");
309 lmapper.outputArguments(out, m.isStatic());
310 out.append(") {").append("\n");
312 final byte[] byteCodes = m.getCode();
313 if (byteCodes == null) {
314 out.append(" throw 'no code found for ")
315 .append(jc.getClassName()).append('.')
316 .append(m.getName()).append("';\n");
321 final StackMapper smapper = new StackMapper();
324 out.append(" var ").append(" lcA0 = this;\n");
327 int lastStackFrame = -1;
328 TrapData[] previousTrap = null;
329 boolean wide = false;
331 out.append("\n var gt = 0;\n");
333 int topMostLabel = 0;
334 for (int i = 0; i < byteCodes.length; i++) {
336 stackMapIterator.advanceTo(i);
337 boolean changeInCatch = trap.advanceTo(i);
338 if (changeInCatch || lastStackFrame != stackMapIterator.getFrameIndex()) {
339 if (previousTrap != null) {
340 generateCatch(previousTrap, i, topMostLabel);
344 if (lastStackFrame != stackMapIterator.getFrameIndex()) {
348 if (openBraces > 64) {
349 for (int c = 0; c < 64; c++) {
350 out.append("break;}\n");
356 lastStackFrame = stackMapIterator.getFrameIndex();
357 lmapper.syncWithFrameLocals(stackMapIterator.getFrameLocals());
358 smapper.syncWithFrameStack(stackMapIterator.getFrameStack());
359 out.append(" X_" + i).append(": for (;;) { IF: if (gt <= " + i + ") {\n");
361 changeInCatch = true;
363 debug(" /* " + i + " */ ");
365 if (changeInCatch && trap.useTry()) {
367 previousTrap = trap.current();
369 final int c = readUByte(byteCodes, i);
372 emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(0));
375 emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(0));
378 emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(0));
381 emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(0));
384 emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(0));
387 emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(1));
390 emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(1));
393 emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(1));
396 emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(1));
399 emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(1));
402 emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(2));
405 emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(2));
408 emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(2));
411 emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(2));
414 emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(2));
417 emit(out, "var @1 = @2;", smapper.pushA(), lmapper.getA(3));
420 emit(out, "var @1 = @2;", smapper.pushI(), lmapper.getI(3));
423 emit(out, "var @1 = @2;", smapper.pushL(), lmapper.getL(3));
426 emit(out, "var @1 = @2;", smapper.pushF(), lmapper.getF(3));
429 emit(out, "var @1 = @2;", smapper.pushD(), lmapper.getD(3));
433 final int indx = wide ? readUShort(byteCodes, i++)
434 : readUByte(byteCodes, i);
436 emit(out, "var @1 = @2;",
437 smapper.pushI(), lmapper.getI(indx));
442 final int indx = wide ? readUShort(byteCodes, i++)
443 : readUByte(byteCodes, i);
445 emit(out, "var @1 = @2;",
446 smapper.pushL(), lmapper.getL(indx));
451 final int indx = wide ? readUShort(byteCodes, i++)
452 : readUByte(byteCodes, i);
454 emit(out, "var @1 = @2;",
455 smapper.pushF(), lmapper.getF(indx));
460 final int indx = wide ? readUShort(byteCodes, i++)
461 : readUByte(byteCodes, i);
463 emit(out, "var @1 = @2;",
464 smapper.pushD(), lmapper.getD(indx));
469 final int indx = wide ? readUShort(byteCodes, i++)
470 : readUByte(byteCodes, i);
472 emit(out, "var @1 = @2;",
473 smapper.pushA(), lmapper.getA(indx));
478 final int indx = wide ? readUShort(byteCodes, i++)
479 : readUByte(byteCodes, i);
481 emit(out, "var @1 = @2;",
482 lmapper.setI(indx), smapper.popI());
487 final int indx = wide ? readUShort(byteCodes, i++)
488 : readUByte(byteCodes, i);
490 emit(out, "var @1 = @2;",
491 lmapper.setL(indx), smapper.popL());
496 final int indx = wide ? readUShort(byteCodes, i++)
497 : readUByte(byteCodes, i);
499 emit(out, "var @1 = @2;",
500 lmapper.setF(indx), smapper.popF());
505 final int indx = wide ? readUShort(byteCodes, i++)
506 : readUByte(byteCodes, i);
508 emit(out, "var @1 = @2;",
509 lmapper.setD(indx), smapper.popD());
514 final int indx = wide ? readUShort(byteCodes, i++)
515 : readUByte(byteCodes, i);
517 emit(out, "var @1 = @2;",
518 lmapper.setA(indx), smapper.popA());
522 emit(out, "var @1 = @2;", lmapper.setA(0), smapper.popA());
525 emit(out, "var @1 = @2;", lmapper.setI(0), smapper.popI());
528 emit(out, "var @1 = @2;", lmapper.setL(0), smapper.popL());
531 emit(out, "var @1 = @2;", lmapper.setF(0), smapper.popF());
534 emit(out, "var @1 = @2;", lmapper.setD(0), smapper.popD());
537 emit(out, "var @1 = @2;", lmapper.setA(1), smapper.popA());
540 emit(out, "var @1 = @2;", lmapper.setI(1), smapper.popI());
543 emit(out, "var @1 = @2;", lmapper.setL(1), smapper.popL());
546 emit(out, "var @1 = @2;", lmapper.setF(1), smapper.popF());
549 emit(out, "var @1 = @2;", lmapper.setD(1), smapper.popD());
552 emit(out, "var @1 = @2;", lmapper.setA(2), smapper.popA());
555 emit(out, "var @1 = @2;", lmapper.setI(2), smapper.popI());
558 emit(out, "var @1 = @2;", lmapper.setL(2), smapper.popL());
561 emit(out, "var @1 = @2;", lmapper.setF(2), smapper.popF());
564 emit(out, "var @1 = @2;", lmapper.setD(2), smapper.popD());
567 emit(out, "var @1 = @2;", lmapper.setA(3), smapper.popA());
570 emit(out, "var @1 = @2;", lmapper.setI(3), smapper.popI());
573 emit(out, "var @1 = @2;", lmapper.setL(3), smapper.popL());
576 emit(out, "var @1 = @2;", lmapper.setF(3), smapper.popF());
579 emit(out, "var @1 = @2;", lmapper.setD(3), smapper.popD());
582 emit(out, "@1 = @1.add32(@2);", smapper.getI(1), smapper.popI());
585 emit(out, "@1 = @1.add64(@2);", smapper.getL(1), smapper.popL());
588 emit(out, "@1 += @2;", smapper.getF(1), smapper.popF());
591 emit(out, "@1 += @2;", smapper.getD(1), smapper.popD());
594 emit(out, "@1 = @1.sub32(@2);", smapper.getI(1), smapper.popI());
597 emit(out, "@1 = @1.sub64(@2);", smapper.getL(1), smapper.popL());
600 emit(out, "@1 -= @2;", smapper.getF(1), smapper.popF());
603 emit(out, "@1 -= @2;", smapper.getD(1), smapper.popD());
606 emit(out, "@1 = @1.mul32(@2);", smapper.getI(1), smapper.popI());
609 emit(out, "@1 = @1.mul64(@2);", 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.div32(@2);",
619 smapper.getI(1), smapper.popI());
622 emit(out, "@1 = @1.div64(@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 = @1.mod32(@2);",
633 smapper.getI(1), smapper.popI());
636 emit(out, "@1 = @1.mod64(@2);",
637 smapper.getL(1), smapper.popL());
640 emit(out, "@1 %= @2;", smapper.getF(1), smapper.popF());
643 emit(out, "@1 %= @2;", smapper.getD(1), smapper.popD());
646 emit(out, "@1 &= @2;", smapper.getI(1), smapper.popI());
649 emit(out, "@1 = @1.and64(@2);", smapper.getL(1), smapper.popL());
652 emit(out, "@1 |= @2;", smapper.getI(1), smapper.popI());
655 emit(out, "@1 = @1.or64(@2);", smapper.getL(1), smapper.popL());
658 emit(out, "@1 ^= @2;", smapper.getI(1), smapper.popI());
661 emit(out, "@1 = @1.xor64(@2);", smapper.getL(1), smapper.popL());
664 emit(out, "@1 = @1.neg32();", smapper.getI(0));
667 emit(out, "@1 = @1.neg64();", smapper.getL(0));
670 emit(out, "@1 = -@1;", smapper.getF(0));
673 emit(out, "@1 = -@1;", smapper.getD(0));
676 emit(out, "@1 <<= @2;", smapper.getI(1), smapper.popI());
679 emit(out, "@1 = @1.shl64(@2);", smapper.getL(1), smapper.popI());
682 emit(out, "@1 >>= @2;", smapper.getI(1), smapper.popI());
685 emit(out, "@1 = @1.shr64(@2);", smapper.getL(1), smapper.popI());
688 emit(out, "@1 >>>= @2;", smapper.getI(1), smapper.popI());
691 emit(out, "@1 = @1.ushr64(@2);", smapper.getL(1), smapper.popI());
695 final int varIndx = wide ? readUShort(byteCodes, i++)
696 : readUByte(byteCodes, i);
698 final int incrBy = wide ? readShort(byteCodes, i++)
702 emit(out, "@1++;", lmapper.getI(varIndx));
704 emit(out, "@1 += @2;",
705 lmapper.getI(varIndx),
706 Integer.toString(incrBy));
711 emit(out, "return;");
714 emit(out, "return @1;", smapper.popI());
717 emit(out, "return @1;", smapper.popL());
720 emit(out, "return @1;", smapper.popF());
723 emit(out, "return @1;", smapper.popD());
726 emit(out, "return @1;", smapper.popA());
729 emit(out, "var @2 = @1;", smapper.popI(), smapper.pushL());
732 emit(out, "var @2 = @1;", smapper.popI(), smapper.pushF());
735 emit(out, "var @2 = @1;", smapper.popI(), smapper.pushD());
738 emit(out, "var @2 = @1.toInt32();", smapper.popL(), smapper.pushI());
742 emit(out, "var @2 = @1.toFP();", smapper.popL(), smapper.pushF());
745 emit(out, "var @2 = @1.toFP();", smapper.popL(), smapper.pushD());
748 emit(out, "var @2 = @1;", smapper.popF(), smapper.pushD());
751 emit(out, "var @2 = @1;", smapper.popD(), smapper.pushF());
754 emit(out, "var @2 = @1.toInt32();",
755 smapper.popF(), smapper.pushI());
758 emit(out, "var @2 = @1.toLong();",
759 smapper.popF(), smapper.pushL());
762 emit(out, "var @2 = @1.toInt32();",
763 smapper.popD(), smapper.pushI());
766 emit(out, "var @2 = @1.toLong();",
767 smapper.popD(), smapper.pushL());
770 emit(out, "var @1 = @1.toInt8();", smapper.getI(0));
773 out.append("{ /* number conversion */ }");
776 emit(out, "var @1 = @1.toInt16();", smapper.getI(0));
778 case opc_aconst_null:
779 emit(out, "var @1 = null;", smapper.pushA());
782 emit(out, "var @1 = -1;", smapper.pushI());
785 emit(out, "var @1 = 0;", smapper.pushI());
788 emit(out, "var @1 = 0;", smapper.pushD());
791 emit(out, "var @1 = 0;", smapper.pushL());
794 emit(out, "var @1 = 0;", smapper.pushF());
797 emit(out, "var @1 = 1;", smapper.pushI());
800 emit(out, "var @1 = 1;", smapper.pushL());
803 emit(out, "var @1 = 1;", smapper.pushF());
806 emit(out, "var @1 = 1;", smapper.pushD());
809 emit(out, "var @1 = 2;", smapper.pushI());
812 emit(out, "var @1 = 2;", smapper.pushF());
815 emit(out, "var @1 = 3;", smapper.pushI());
818 emit(out, "var @1 = 4;", smapper.pushI());
821 emit(out, "var @1 = 5;", smapper.pushI());
824 int indx = readUByte(byteCodes, ++i);
825 String v = encodeConstant(indx);
826 int type = VarType.fromConstantType(jc.getTag(indx));
827 emit(out, "var @1 = @2;", smapper.pushT(type), v);
832 int indx = readUShortArg(byteCodes, i);
834 String v = encodeConstant(indx);
835 int type = VarType.fromConstantType(jc.getTag(indx));
836 if (type == VarType.LONG) {
837 final Long lv = new Long(v);
838 final int low = (int)(lv.longValue() & 0xFFFFFFFF);
839 final int hi = (int)(lv.longValue() >> 32);
840 emit(out, "var @1 = 0x@3.next32(0x@2);", smapper.pushL(),
841 Integer.toHexString(low), Integer.toHexString(hi));
843 emit(out, "var @1 = @2;", smapper.pushT(type), v);
848 emit(out, "var @3 = @2.compare64(@1);",
849 smapper.popL(), smapper.popL(), smapper.pushI());
853 emit(out, "var @3 = (@2 == @1) ? 0 : ((@2 < @1) ? -1 : 1);",
854 smapper.popF(), smapper.popF(), smapper.pushI());
858 emit(out, "var @3 = (@2 == @1) ? 0 : ((@2 < @1) ? -1 : 1);",
859 smapper.popD(), smapper.popD(), smapper.pushI());
862 i = generateIf(byteCodes, i, smapper.popA(), smapper.popA(),
863 "===", topMostLabel);
866 i = generateIf(byteCodes, i, smapper.popA(), smapper.popA(),
867 "!==", topMostLabel);
870 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
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);
902 int indx = i + readShortArg(byteCodes, i);
903 emitIf(out, "if (@1 > 0) ",
904 smapper.popI(), i, indx, topMostLabel);
909 int indx = i + readShortArg(byteCodes, i);
910 emitIf(out, "if (@1 >= 0) ",
911 smapper.popI(), i, indx, topMostLabel);
915 case opc_ifnonnull: {
916 int indx = i + readShortArg(byteCodes, i);
917 emitIf(out, "if (@1 !== null) ",
918 smapper.popA(), i, indx, topMostLabel);
923 int indx = i + readShortArg(byteCodes, i);
924 emitIf(out, "if (@1 === null) ",
925 smapper.popA(), i, indx, topMostLabel);
930 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
934 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
938 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
942 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
946 i = generateIf(byteCodes, i, smapper.popI(), smapper.popI(),
950 int indx = i + readShortArg(byteCodes, i);
951 goTo(out, i, indx, topMostLabel);
955 case opc_lookupswitch: {
956 i = generateLookupSwitch(i, byteCodes, smapper, topMostLabel);
959 case opc_tableswitch: {
960 i = generateTableSwitch(i, byteCodes, smapper, topMostLabel);
963 case opc_invokeinterface: {
964 i = invokeVirtualMethod(byteCodes, i, smapper) + 2;
967 case opc_invokevirtual:
968 i = invokeVirtualMethod(byteCodes, i, smapper);
970 case opc_invokespecial:
971 i = invokeStaticMethod(byteCodes, i, smapper, false);
973 case opc_invokestatic:
974 i = invokeStaticMethod(byteCodes, i, smapper, true);
977 int indx = readUShortArg(byteCodes, i);
978 String ci = jc.getClassName(indx);
979 emit(out, "var @1 = new @2;",
980 smapper.pushA(), accessClass(ci.replace('/', '_')));
986 int atype = readUByte(byteCodes, ++i);
987 generateNewArray(atype, smapper);
989 case opc_anewarray: {
990 int type = readUShortArg(byteCodes, i);
992 generateANewArray(type, smapper);
995 case opc_multianewarray: {
996 int type = readUShortArg(byteCodes, i);
998 i = generateMultiANewArray(type, byteCodes, i, smapper);
1001 case opc_arraylength:
1002 emit(out, "var @2 = @1.length;",
1003 smapper.popA(), smapper.pushI());
1006 emit(out, "@3.at(@2, @1);",
1007 smapper.popL(), smapper.popI(), smapper.popA());
1010 emit(out, "@3.at(@2, @1);",
1011 smapper.popF(), smapper.popI(), smapper.popA());
1014 emit(out, "@3.at(@2, @1);",
1015 smapper.popD(), smapper.popI(), smapper.popA());
1018 emit(out, "@3.at(@2, @1);",
1019 smapper.popA(), smapper.popI(), smapper.popA());
1025 emit(out, "@3.at(@2, @1);",
1026 smapper.popI(), smapper.popI(), smapper.popA());
1029 emit(out, "var @3 = @2.at(@1);",
1030 smapper.popI(), smapper.popA(), smapper.pushL());
1033 emit(out, "var @3 = @2.at(@1);",
1034 smapper.popI(), smapper.popA(), smapper.pushF());
1037 emit(out, "var @3 = @2.at(@1);",
1038 smapper.popI(), smapper.popA(), smapper.pushD());
1041 emit(out, "var @3 = @2.at(@1);",
1042 smapper.popI(), smapper.popA(), smapper.pushA());
1048 emit(out, "var @3 = @2.at(@1);",
1049 smapper.popI(), smapper.popA(), smapper.pushI());
1057 final Variable v = smapper.get(0);
1058 emit(out, "var @1 = @2;", smapper.pushT(v.getType()), v);
1062 final Variable vi1 = smapper.get(0);
1064 if (vi1.isCategory2()) {
1065 emit(out, "var @1 = @2;",
1066 smapper.pushT(vi1.getType()), vi1);
1068 final Variable vi2 = smapper.get(1);
1069 emit(out, "var @1 = @2, @3 = @4;",
1070 smapper.pushT(vi2.getType()), vi2,
1071 smapper.pushT(vi1.getType()), vi1);
1076 final Variable vi1 = smapper.pop();
1077 final Variable vi2 = smapper.pop();
1078 final Variable vo3 = smapper.pushT(vi1.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, vo1);
1087 final Variable vi1 = smapper.pop();
1088 final Variable vi2 = smapper.pop();
1090 if (vi1.isCategory2()) {
1091 final Variable vo3 = smapper.pushT(vi1.getType());
1092 final Variable vo2 = smapper.pushT(vi2.getType());
1093 final Variable vo1 = smapper.pushT(vi1.getType());
1095 emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
1096 vo1, vi1, vo2, vi2, vo3, vo1);
1098 final Variable vi3 = smapper.pop();
1099 final Variable vo5 = smapper.pushT(vi2.getType());
1100 final Variable vo4 = smapper.pushT(vi1.getType());
1101 final Variable vo3 = smapper.pushT(vi3.getType());
1102 final Variable vo2 = smapper.pushT(vi2.getType());
1103 final Variable vo1 = smapper.pushT(vi1.getType());
1105 emit(out, "var @1 = @2, @3 = @4, @5 = @6,",
1106 vo1, vi1, vo2, vi2, vo3, vi3);
1107 emit(out, " @1 = @2, @3 = @4;",
1108 vo4, vo1, vo5, vo2);
1113 final Variable vi1 = smapper.pop();
1114 final Variable vi2 = smapper.pop();
1116 if (vi2.isCategory2()) {
1117 final Variable vo3 = smapper.pushT(vi1.getType());
1118 final Variable vo2 = smapper.pushT(vi2.getType());
1119 final Variable vo1 = smapper.pushT(vi1.getType());
1121 emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
1122 vo1, vi1, vo2, vi2, vo3, vo1);
1124 final Variable vi3 = smapper.pop();
1125 final Variable vo4 = smapper.pushT(vi1.getType());
1126 final Variable vo3 = smapper.pushT(vi3.getType());
1127 final Variable vo2 = smapper.pushT(vi2.getType());
1128 final Variable vo1 = smapper.pushT(vi1.getType());
1130 emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8;",
1131 vo1, vi1, vo2, vi2, vo3, vi3, vo4, vo1);
1136 final Variable vi1 = smapper.pop();
1137 final Variable vi2 = smapper.pop();
1139 if (vi1.isCategory2()) {
1140 if (vi2.isCategory2()) {
1141 final Variable vo3 = smapper.pushT(vi1.getType());
1142 final Variable vo2 = smapper.pushT(vi2.getType());
1143 final Variable vo1 = smapper.pushT(vi1.getType());
1145 emit(out, "var @1 = @2, @3 = @4, @5 = @6;",
1146 vo1, vi1, vo2, vi2, vo3, vo1);
1148 final Variable vi3 = smapper.pop();
1149 final Variable vo4 = smapper.pushT(vi1.getType());
1150 final Variable vo3 = smapper.pushT(vi3.getType());
1151 final Variable vo2 = smapper.pushT(vi2.getType());
1152 final Variable vo1 = smapper.pushT(vi1.getType());
1154 emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8;",
1155 vo1, vi1, vo2, vi2, vo3, vi3, vo4, vo1);
1158 final Variable vi3 = smapper.pop();
1160 if (vi3.isCategory2()) {
1161 final Variable vo5 = smapper.pushT(vi2.getType());
1162 final Variable vo4 = smapper.pushT(vi1.getType());
1163 final Variable vo3 = smapper.pushT(vi3.getType());
1164 final Variable vo2 = smapper.pushT(vi2.getType());
1165 final Variable vo1 = smapper.pushT(vi1.getType());
1167 emit(out, "var @1 = @2, @3 = @4, @5 = @6,",
1168 vo1, vi1, vo2, vi2, vo3, vi3);
1169 emit(out, " @1 = @2, @3 = @4;",
1170 vo4, vo1, vo5, vo2);
1172 final Variable vi4 = smapper.pop();
1173 final Variable vo6 = smapper.pushT(vi2.getType());
1174 final Variable vo5 = smapper.pushT(vi1.getType());
1175 final Variable vo4 = smapper.pushT(vi4.getType());
1176 final Variable vo3 = smapper.pushT(vi3.getType());
1177 final Variable vo2 = smapper.pushT(vi2.getType());
1178 final Variable vo1 = smapper.pushT(vi1.getType());
1180 emit(out, "var @1 = @2, @3 = @4, @5 = @6, @7 = @8,",
1181 vo1, vi1, vo2, vi2, vo3, vi3, vo4, vi4);
1182 emit(out, " @1 = @2, @3 = @4;",
1183 vo5, vo1, vo6, vo2);
1189 final Variable vi1 = smapper.get(0);
1190 final Variable vi2 = smapper.get(1);
1192 if (vi1.getType() == vi2.getType()) {
1193 final Variable tmp = smapper.pushT(vi1.getType());
1195 emit(out, "var @1 = @2, @2 = @3, @3 = @1;",
1200 smapper.pushT(vi1.getType());
1201 smapper.pushT(vi2.getType());
1206 emit(out, "var @1 = @2;",
1207 smapper.pushI(), Integer.toString(byteCodes[++i]));
1210 emit(out, "var @1 = @2;",
1212 Integer.toString(readShortArg(byteCodes, i)));
1215 case opc_getfield: {
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, "var @2 = @3.call(@1);",
1223 smapper.pushT(type),
1224 accessField(mangleClassAccess + "(false)",
1229 case opc_putfield: {
1230 int indx = readUShortArg(byteCodes, i);
1231 String[] fi = jc.getFieldInfoName(indx);
1232 final int type = VarType.fromFieldType(fi[2].charAt(0));
1233 final String mangleClass = mangleSig(fi[0]);
1234 final String mangleClassAccess = accessClass(mangleClass);
1235 emit(out, "@3.call(@2, @1);",
1238 accessField(mangleClassAccess + "(false)",
1243 case opc_getstatic: {
1244 int indx = readUShortArg(byteCodes, i);
1245 String[] fi = jc.getFieldInfoName(indx);
1246 final int type = VarType.fromFieldType(fi[2].charAt(0));
1247 emit(out, "var @1 = @2();",
1248 smapper.pushT(type),
1249 accessField(accessClass(fi[0].replace('/', '_'))
1253 addReference(fi[0]);
1256 case opc_putstatic: {
1257 int indx = readUShortArg(byteCodes, i);
1258 String[] fi = jc.getFieldInfoName(indx);
1259 final int type = VarType.fromFieldType(fi[2].charAt(0));
1260 emit(out, "@1(@2);",
1261 accessField(accessClass(fi[0].replace('/', '_'))
1264 smapper.popT(type));
1266 addReference(fi[0]);
1269 case opc_checkcast: {
1270 int indx = readUShortArg(byteCodes, i);
1271 generateCheckcast(indx, smapper);
1275 case opc_instanceof: {
1276 int indx = readUShortArg(byteCodes, i);
1277 generateInstanceOf(indx, smapper);
1282 final Variable v = smapper.popA();
1285 emit(out, "{ var @1 = @2; throw @2; }",
1286 smapper.pushA(), v);
1290 case opc_monitorenter: {
1291 out.append("/* monitor enter */");
1296 case opc_monitorexit: {
1297 out.append("/* monitor exit */");
1308 emit(out, "throw 'unknown bytecode @1';",
1309 Integer.toString(c));
1313 generateByteCodeComment(prev, i, byteCodes);
1317 if (previousTrap != null) {
1318 generateCatch(previousTrap, byteCodes.length, topMostLabel);
1320 out.append("\n }\n");
1321 while (openBraces-- > 0) {
1327 private int generateIf(byte[] byteCodes, int i, final Variable v2, final Variable v1, final String test, int topMostLabel) throws IOException {
1328 int indx = i + readShortArg(byteCodes, i);
1329 out.append("if (").append(v1)
1330 .append(' ').append(test).append(' ')
1331 .append(v2).append(") ");
1332 goTo(out, i, indx, topMostLabel);
1336 private int readInt4(byte[] byteCodes, int offset) {
1337 final int d = byteCodes[offset + 0] << 24;
1338 final int c = byteCodes[offset + 1] << 16;
1339 final int b = byteCodes[offset + 2] << 8;
1340 final int a = byteCodes[offset + 3];
1341 return (d & 0xff000000) | (c & 0xff0000) | (b & 0xff00) | (a & 0xff);
1343 private static int readUByte(byte[] byteCodes, int offset) {
1344 return byteCodes[offset] & 0xff;
1347 private static int readUShort(byte[] byteCodes, int offset) {
1348 return ((byteCodes[offset] & 0xff) << 8)
1349 | (byteCodes[offset + 1] & 0xff);
1351 private static int readUShortArg(byte[] byteCodes, int offsetInstruction) {
1352 return readUShort(byteCodes, offsetInstruction + 1);
1355 private static int readShort(byte[] byteCodes, int offset) {
1356 int signed = byteCodes[offset];
1357 byte b0 = (byte)signed;
1358 return (b0 << 8) | (byteCodes[offset + 1] & 0xff);
1360 private static int readShortArg(byte[] byteCodes, int offsetInstruction) {
1361 return readShort(byteCodes, offsetInstruction + 1);
1364 private static void countArgs(String descriptor, char[] returnType, StringBuilder sig, StringBuilder cnt) {
1366 Boolean count = null;
1367 boolean array = false;
1369 int firstPos = sig.length();
1370 while (i < descriptor.length()) {
1371 char ch = descriptor.charAt(i++);
1392 if (ch == 'J' || ch == 'D') {
1398 sig.insert(firstPos, ch);
1400 returnType[0] = '[';
1401 sig.insert(firstPos, "_3");
1410 returnType[0] = 'V';
1411 sig.insert(firstPos, 'V');
1414 int next = descriptor.indexOf(';', i);
1415 String realSig = mangleSig(descriptor, i - 1, next + 1);
1420 sig.append(realSig);
1423 sig.insert(firstPos, realSig);
1425 sig.insert(firstPos, "_3");
1427 returnType[0] = 'L';
1436 throw new IllegalStateException("Invalid char: " + ch);
1441 static String mangleSig(String sig) {
1442 return mangleSig(sig, 0, sig.length());
1445 private static String mangleSig(String txt, int first, int last) {
1446 StringBuilder sb = new StringBuilder();
1447 for (int i = first; i < last; i++) {
1448 final char ch = txt.charAt(i);
1450 case '/': sb.append('_'); break;
1451 case '_': sb.append("_1"); break;
1452 case ';': sb.append("_2"); break;
1453 case '[': sb.append("_3"); break;
1454 default: sb.append(ch); break;
1457 return sb.toString();
1460 private static String findMethodName(MethodData m, StringBuilder cnt) {
1461 StringBuilder name = new StringBuilder();
1462 if ("<init>".equals(m.getName())) { // NOI18N
1463 name.append("cons"); // NOI18N
1464 } else if ("<clinit>".equals(m.getName())) { // NOI18N
1465 name.append("class"); // NOI18N
1467 name.append(m.getName());
1470 countArgs(m.getInternalSig(), new char[1], name, cnt);
1471 return name.toString();
1474 static String findMethodName(String[] mi, StringBuilder cnt, char[] returnType) {
1475 StringBuilder name = new StringBuilder();
1476 String descr = mi[2];//mi.getDescriptor();
1478 if ("<init>".equals(nm)) { // NOI18N
1479 name.append("cons"); // NOI18N
1483 countArgs(descr, returnType, name, cnt);
1484 return name.toString();
1487 private int invokeStaticMethod(byte[] byteCodes, int i, final StackMapper mapper, boolean isStatic)
1488 throws IOException {
1489 int methodIndex = readUShortArg(byteCodes, i);
1490 String[] mi = jc.getFieldInfoName(methodIndex);
1491 char[] returnType = { 'V' };
1492 StringBuilder cnt = new StringBuilder();
1493 String mn = findMethodName(mi, cnt, returnType);
1495 final int numArguments = isStatic ? cnt.length() : cnt.length() + 1;
1496 final Variable[] vars = new Variable[numArguments];
1498 for (int j = numArguments - 1; j >= 0; --j) {
1499 vars[j] = mapper.pop();
1502 if (returnType[0] != 'V') {
1504 .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
1508 final String in = mi[0];
1509 String object = accessClass(in.replace('/', '_')) + "(false)";
1510 if (mn.startsWith("cons_")) {
1511 object += ".constructor";
1513 out.append(accessStaticMethod(object, mn, mi));
1517 out.append(".call(");
1519 if (numArguments > 0) {
1520 out.append(vars[0]);
1521 for (int j = 1; j < numArguments; ++j) {
1523 out.append(vars[j]);
1531 private int invokeVirtualMethod(byte[] byteCodes, int i, final StackMapper mapper)
1532 throws IOException {
1533 int methodIndex = readUShortArg(byteCodes, i);
1534 String[] mi = jc.getFieldInfoName(methodIndex);
1535 char[] returnType = { 'V' };
1536 StringBuilder cnt = new StringBuilder();
1537 String mn = findMethodName(mi, cnt, returnType);
1539 final int numArguments = cnt.length() + 1;
1540 final Variable[] vars = new Variable[numArguments];
1542 for (int j = numArguments - 1; j >= 0; --j) {
1543 vars[j] = mapper.pop();
1546 if (returnType[0] != 'V') {
1548 .append(mapper.pushT(VarType.fromFieldType(returnType[0])))
1552 out.append(accessVirtualMethod(vars[0].toString(), mn, mi));
1555 for (int j = 1; j < numArguments; ++j) {
1557 out.append(vars[j]);
1565 private void addReference(String cn) throws IOException {
1566 if (requireReference(cn)) {
1567 debug(" /* needs " + cn + " */");
1571 private void outType(String d, StringBuilder out) {
1573 while (d.charAt(0) == '[') {
1577 if (d.charAt(0) == 'L') {
1578 assert d.charAt(d.length() - 1) == ';';
1579 out.append(d.replace('/', '_').substring(0, d.length() - 1));
1585 private String encodeConstant(int entryIndex) throws IOException {
1586 String[] classRef = { null };
1587 String s = jc.stringValue(entryIndex, classRef);
1588 if (classRef[0] != null) {
1589 if (classRef[0].startsWith("[")) {
1590 s = accessClass("java_lang_Class") + "(false)['forName__Ljava_lang_Class_2Ljava_lang_String_2']('" + classRef[0] + "');";
1592 addReference(classRef[0]);
1593 s = accessClass(s.replace('/', '_')) + "(false).constructor.$class";
1599 private String javaScriptBody(String destObject, MethodData m, boolean isStatic) throws IOException {
1600 byte[] arr = m.findAnnotationData(true);
1604 final String jvmType = "Lorg/apidesign/bck2brwsr/core/JavaScriptBody;";
1605 class P extends AnnotationParser {
1611 String[] args = new String[30];
1615 protected void visitAttr(String type, String attr, String at, String value) {
1616 if (type.equals(jvmType)) {
1617 if ("body".equals(attr)) {
1619 } else if ("args".equals(attr)) {
1620 args[cnt++] = value;
1622 throw new IllegalArgumentException(attr);
1629 if (p.body == null) {
1632 StringBuilder cnt = new StringBuilder();
1633 final String mn = findMethodName(m, cnt);
1634 out.append(destObject).append(".").append(mn);
1635 out.append(" = function(");
1638 for (int i = 0; i < cnt.length(); i++) {
1640 space = outputArg(out, p.args, index);
1643 out.append(") {").append("\n");
1645 out.append("\n}\n");
1648 private static String className(ClassData jc) {
1649 //return jc.getName().getInternalName().replace('/', '_');
1650 return jc.getClassName().replace('/', '_');
1653 private static String[] findAnnotation(
1654 byte[] arr, ClassData cd, final String className,
1655 final String... attrNames
1656 ) throws IOException {
1660 final String[] values = new String[attrNames.length];
1661 final boolean[] found = { false };
1662 final String jvmType = "L" + className.replace('.', '/') + ";";
1663 AnnotationParser ap = new AnnotationParser(false, true) {
1665 protected void visitAttr(String type, String attr, String at, String value) {
1666 if (type.equals(jvmType)) {
1668 for (int i = 0; i < attrNames.length; i++) {
1669 if (attrNames[i].equals(attr)) {
1678 return found[0] ? values : null;
1681 private CharSequence initField(FieldData v) {
1682 final String is = v.getInternalSig();
1683 if (is.length() == 1) {
1684 switch (is.charAt(0)) {
1690 case 'I': return " = 0;";
1692 case 'D': return " = 0.0;";
1694 throw new IllegalStateException(is);
1700 private void generateAnno(ClassData cd, final Appendable out, byte[] data) throws IOException {
1701 AnnotationParser ap = new AnnotationParser(true, false) {
1702 int[] cnt = new int[32];
1706 protected void visitAnnotationStart(String attrType, boolean top) throws IOException {
1707 final String slashType = attrType.substring(1, attrType.length() - 1);
1708 requireReference(slashType);
1710 if (cnt[depth]++ > 0) {
1714 out.append('"').append(attrType).append("\" : ");
1721 protected void visitAnnotationEnd(String type, boolean top) throws IOException {
1722 out.append("\n}\n");
1727 protected void visitValueStart(String attrName, char type) throws IOException {
1728 if (cnt[depth]++ > 0) {
1732 if (attrName != null) {
1733 out.append(attrName).append(" : ");
1741 protected void visitValueEnd(String attrName, char type) throws IOException {
1749 protected void visitAttr(String type, String attr, String attrType, String value)
1750 throws IOException {
1751 if (attr == null && value == null) {
1758 protected void visitEnumAttr(String type, String attr, String attrType, String value)
1759 throws IOException {
1760 final String slashType = attrType.substring(1, attrType.length() - 1);
1761 requireReference(slashType);
1763 out.append(accessClass(slashType.replace('/', '_')))
1764 .append("(false).constructor.").append(value);
1770 private static String outputArg(Appendable out, String[] args, int indx) throws IOException {
1771 final String name = args[indx];
1775 if (name.contains(",")) {
1776 throw new IOException("Wrong parameter with ',': " + name);
1782 private static void emit(final Appendable out,
1783 final String format,
1784 final CharSequence... params) throws IOException {
1785 final int length = format.length();
1788 int paramOffset = format.indexOf('@');
1789 while ((paramOffset != -1) && (paramOffset < (length - 1))) {
1790 final char paramChar = format.charAt(paramOffset + 1);
1791 if ((paramChar >= '1') && (paramChar <= '9')) {
1792 final int paramIndex = paramChar - '0' - 1;
1794 out.append(format, processed, paramOffset);
1795 out.append(params[paramIndex]);
1798 processed = paramOffset + 1;
1801 paramOffset = format.indexOf('@', paramOffset + 1);
1804 out.append(format, processed, length);
1807 private void generateCatch(TrapData[] traps, int current, int topMostLabel) throws IOException {
1808 out.append("} catch (e) {\n");
1810 for (TrapData e : traps) {
1814 if (e.catch_cpx != 0) { //not finally
1815 final String classInternalName = jc.getClassName(e.catch_cpx);
1816 addReference(classInternalName);
1817 out.append("e = vm.java_lang_Throwable(false).bck2BrwsrCnvrt(e);");
1818 out.append("if (e['$instOf_" + classInternalName.replace('/', '_') + "']) {");
1819 out.append("var stA0 = e;");
1820 goTo(out, current, e.handler_pc, topMostLabel);
1823 finallyPC = e.handler_pc;
1826 if (finallyPC == -1) {
1827 out.append("throw e;");
1829 out.append("var stA0 = e;");
1830 goTo(out, current, finallyPC, topMostLabel);
1835 private static void goTo(Appendable out, int current, int to, int canBack) throws IOException {
1838 out.append("{ gt = 0; continue X_" + to + "; }");
1840 out.append("{ gt = " + to + "; continue X_0; }");
1843 out.append("{ gt = " + to + "; break IF; }");
1847 private static void emitIf(
1848 Appendable out, String pattern, Variable param,
1849 int current, int to, int canBack
1850 ) throws IOException {
1851 emit(out, pattern, param);
1852 goTo(out, current, to, canBack);
1855 private void generateNewArray(int atype, final StackMapper smapper) throws IOException, IllegalStateException {
1858 case 4: jvmType = "[Z"; break;
1859 case 5: jvmType = "[C"; break;
1860 case 6: jvmType = "[F"; break;
1861 case 7: jvmType = "[D"; break;
1862 case 8: jvmType = "[B"; break;
1863 case 9: jvmType = "[S"; break;
1864 case 10: jvmType = "[I"; break;
1865 case 11: jvmType = "[J"; break;
1866 default: throw new IllegalStateException("Array type: " + atype);
1868 emit(out, "var @2 = Array.prototype['newArray__Ljava_lang_Object_2ZLjava_lang_String_2I'](true, '@3', @1);",
1869 smapper.popI(), smapper.pushA(), jvmType);
1872 private void generateANewArray(int type, final StackMapper smapper) throws IOException {
1873 String typeName = jc.getClassName(type);
1874 if (typeName.startsWith("[")) {
1875 typeName = "[" + typeName;
1877 typeName = "[L" + typeName + ";";
1879 emit(out, "var @2 = Array.prototype['newArray__Ljava_lang_Object_2ZLjava_lang_String_2I'](false, '@3', @1);",
1880 smapper.popI(), smapper.pushA(), typeName);
1883 private int generateMultiANewArray(int type, final byte[] byteCodes, int i, final StackMapper smapper) throws IOException {
1884 String typeName = jc.getClassName(type);
1885 int dim = readUByte(byteCodes, ++i);
1886 StringBuilder dims = new StringBuilder();
1888 for (int d = 0; d < dim; d++) {
1890 dims.insert(1, ",");
1892 dims.insert(1, smapper.popI());
1895 emit(out, "var @2 = Array.prototype['multiNewArray__Ljava_lang_Object_2Ljava_lang_String_2_3II']('@3', @1, 0);",
1896 dims.toString(), smapper.pushA(), typeName);
1900 private int generateTableSwitch(int i, final byte[] byteCodes, final StackMapper smapper, int topMostLabel) throws IOException {
1901 int table = i / 4 * 4 + 4;
1902 int dflt = i + readInt4(byteCodes, table);
1904 int low = readInt4(byteCodes, table);
1906 int high = readInt4(byteCodes, table);
1908 out.append("switch (").append(smapper.popI()).append(") {\n");
1909 while (low <= high) {
1910 int offset = i + readInt4(byteCodes, table);
1912 out.append(" case " + low).append(":"); goTo(out, i, offset, topMostLabel); out.append('\n');
1915 out.append(" default: ");
1916 goTo(out, i, dflt, topMostLabel);
1922 private int generateLookupSwitch(int i, final byte[] byteCodes, final StackMapper smapper, int topMostLabel) throws IOException {
1923 int table = i / 4 * 4 + 4;
1924 int dflt = i + readInt4(byteCodes, table);
1926 int n = readInt4(byteCodes, table);
1928 out.append("switch (").append(smapper.popI()).append(") {\n");
1930 int cnstnt = readInt4(byteCodes, table);
1932 int offset = i + readInt4(byteCodes, table);
1934 out.append(" case " + cnstnt).append(": "); goTo(out, i, offset, topMostLabel); out.append('\n');
1936 out.append(" default: ");
1937 goTo(out, i, dflt, topMostLabel);
1943 private void generateInstanceOf(int indx, final StackMapper smapper) throws IOException {
1944 final String type = jc.getClassName(indx);
1945 if (!type.startsWith("[")) {
1946 emit(out, "var @2 = @1 != null && @1['$instOf_@3'] ? 1 : 0;",
1947 smapper.popA(), smapper.pushI(),
1948 type.replace('/', '_'));
1950 emit(out, "var @2 = vm.java_lang_Class(false)['forName__Ljava_lang_Class_2Ljava_lang_String_2']('@3')['isInstance__ZLjava_lang_Object_2'](@1);",
1951 smapper.popA(), smapper.pushI(),
1957 private void generateCheckcast(int indx, final StackMapper smapper) throws IOException {
1958 final String type = jc.getClassName(indx);
1959 if (!type.startsWith("[")) {
1961 "if (@1 !== null && !@1['$instOf_@2']) throw vm.java_lang_ClassCastException(true);",
1962 smapper.getA(0), type.replace('/', '_'));
1964 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);",
1965 smapper.getA(0), type
1970 private void generateByteCodeComment(int prev, int i, final byte[] byteCodes) throws IOException {
1971 for (int j = prev; j <= i; j++) {
1973 final int cc = readUByte(byteCodes, j);
1974 out.append(Integer.toString(cc));