1.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Thu Mar 12 10:32:13 2015 +0100
1.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Thu Mar 12 11:16:15 2015 +0100
1.3 @@ -480,7 +480,7 @@
1.4 boolean changeInCatch = trap.advanceTo(i);
1.5 if (changeInCatch || lastStackFrame != stackMapIterator.getFrameIndex()) {
1.6 if (previousTrap != null) {
1.7 - generateCatch(previousTrap, i, topMostLabel);
1.8 + generateCatch(previousTrap, flow, i, topMostLabel);
1.9 previousTrap = null;
1.10 }
1.11 }
1.12 @@ -1002,106 +1002,106 @@
1.13 smapper.popD(), smapper.popD(), smapper.pushI());
1.14 break;
1.15 case opc_if_acmpeq:
1.16 - i = generateIf(smapper, byteCodes, i, smapper.popA(), smapper.popA(),
1.17 + i = generateIf(smapper, flow, byteCodes, i, smapper.popA(), smapper.popA(),
1.18 "===", topMostLabel);
1.19 break;
1.20 case opc_if_acmpne:
1.21 - i = generateIf(smapper, byteCodes, i, smapper.popA(), smapper.popA(),
1.22 + i = generateIf(smapper, flow, byteCodes, i, smapper.popA(), smapper.popA(),
1.23 "!==", topMostLabel);
1.24 break;
1.25 case opc_if_icmpeq:
1.26 - i = generateIf(smapper, byteCodes, i, smapper.popI(), smapper.popI(),
1.27 + i = generateIf(smapper, flow, byteCodes, i, smapper.popI(), smapper.popI(),
1.28 "==", topMostLabel);
1.29 break;
1.30 case opc_ifeq: {
1.31 int indx = i + readShortArg(byteCodes, i);
1.32 - emitIf(smapper, this, "if ((@1) == 0) ",
1.33 + emitIf(smapper, flow, this, "if ((@1) == 0) ",
1.34 smapper.popI(), i, indx, topMostLabel);
1.35 i += 2;
1.36 break;
1.37 }
1.38 case opc_ifne: {
1.39 int indx = i + readShortArg(byteCodes, i);
1.40 - emitIf(smapper, this, "if ((@1) != 0) ",
1.41 + emitIf(smapper, flow, this, "if ((@1) != 0) ",
1.42 smapper.popI(), i, indx, topMostLabel);
1.43 i += 2;
1.44 break;
1.45 }
1.46 case opc_iflt: {
1.47 int indx = i + readShortArg(byteCodes, i);
1.48 - emitIf(smapper, this, "if ((@1) < 0) ",
1.49 + emitIf(smapper, flow, this, "if ((@1) < 0) ",
1.50 smapper.popI(), i, indx, topMostLabel);
1.51 i += 2;
1.52 break;
1.53 }
1.54 case opc_ifle: {
1.55 int indx = i + readShortArg(byteCodes, i);
1.56 - emitIf(smapper, this, "if ((@1) <= 0) ",
1.57 + emitIf(smapper, flow, this, "if ((@1) <= 0) ",
1.58 smapper.popI(), i, indx, topMostLabel);
1.59 i += 2;
1.60 break;
1.61 }
1.62 case opc_ifgt: {
1.63 int indx = i + readShortArg(byteCodes, i);
1.64 - emitIf(smapper, this, "if ((@1) > 0) ",
1.65 + emitIf(smapper, flow, this, "if ((@1) > 0) ",
1.66 smapper.popI(), i, indx, topMostLabel);
1.67 i += 2;
1.68 break;
1.69 }
1.70 case opc_ifge: {
1.71 int indx = i + readShortArg(byteCodes, i);
1.72 - emitIf(smapper, this, "if ((@1) >= 0) ",
1.73 + emitIf(smapper, flow, this, "if ((@1) >= 0) ",
1.74 smapper.popI(), i, indx, topMostLabel);
1.75 i += 2;
1.76 break;
1.77 }
1.78 case opc_ifnonnull: {
1.79 int indx = i + readShortArg(byteCodes, i);
1.80 - emitIf(smapper, this, "if ((@1) !== null) ",
1.81 + emitIf(smapper, flow, this, "if ((@1) !== null) ",
1.82 smapper.popA(), i, indx, topMostLabel);
1.83 i += 2;
1.84 break;
1.85 }
1.86 case opc_ifnull: {
1.87 int indx = i + readShortArg(byteCodes, i);
1.88 - emitIf(smapper, this, "if ((@1) === null) ",
1.89 + emitIf(smapper, flow, this, "if ((@1) === null) ",
1.90 smapper.popA(), i, indx, topMostLabel);
1.91 i += 2;
1.92 break;
1.93 }
1.94 case opc_if_icmpne:
1.95 - i = generateIf(smapper, byteCodes, i, smapper.popI(), smapper.popI(),
1.96 + i = generateIf(smapper, flow, byteCodes, i, smapper.popI(), smapper.popI(),
1.97 "!=", topMostLabel);
1.98 break;
1.99 case opc_if_icmplt:
1.100 - i = generateIf(smapper, byteCodes, i, smapper.popI(), smapper.popI(),
1.101 + i = generateIf(smapper, flow, byteCodes, i, smapper.popI(), smapper.popI(),
1.102 "<", topMostLabel);
1.103 break;
1.104 case opc_if_icmple:
1.105 - i = generateIf(smapper, byteCodes, i, smapper.popI(), smapper.popI(),
1.106 + i = generateIf(smapper, flow, byteCodes, i, smapper.popI(), smapper.popI(),
1.107 "<=", topMostLabel);
1.108 break;
1.109 case opc_if_icmpgt:
1.110 - i = generateIf(smapper, byteCodes, i, smapper.popI(), smapper.popI(),
1.111 + i = generateIf(smapper, flow, byteCodes, i, smapper.popI(), smapper.popI(),
1.112 ">", topMostLabel);
1.113 break;
1.114 case opc_if_icmpge:
1.115 - i = generateIf(smapper, byteCodes, i, smapper.popI(), smapper.popI(),
1.116 + i = generateIf(smapper, flow, byteCodes, i, smapper.popI(), smapper.popI(),
1.117 ">=", topMostLabel);
1.118 break;
1.119 case opc_goto: {
1.120 smapper.flush(this);
1.121 int indx = i + readShortArg(byteCodes, i);
1.122 - goTo(this, i, indx, topMostLabel);
1.123 + goTo(this, flow, i, indx, topMostLabel);
1.124 i += 2;
1.125 break;
1.126 }
1.127 case opc_lookupswitch: {
1.128 - i = generateLookupSwitch(i, byteCodes, smapper, topMostLabel);
1.129 + i = generateLookupSwitch(i, byteCodes, smapper, flow, topMostLabel);
1.130 break;
1.131 }
1.132 case opc_tableswitch: {
1.133 - i = generateTableSwitch(i, byteCodes, smapper, topMostLabel);
1.134 + i = generateTableSwitch(i, byteCodes, smapper, flow, topMostLabel);
1.135 break;
1.136 }
1.137 case opc_invokeinterface: {
1.138 @@ -1510,7 +1510,7 @@
1.139 }
1.140 }
1.141 if (previousTrap != null) {
1.142 - generateCatch(previousTrap, byteCodes.length, topMostLabel);
1.143 + generateCatch(previousTrap, flow, byteCodes.length, topMostLabel);
1.144 }
1.145 if (didBranches) {
1.146 append("\n }\n");
1.147 @@ -1526,16 +1526,13 @@
1.148 return defineProp;
1.149 }
1.150
1.151 - private int generateIf(StackMapper mapper, byte[] byteCodes,
1.152 - int i, final CharSequence v2, final CharSequence v1,
1.153 - final String test, int topMostLabel
1.154 - ) throws IOException {
1.155 + private int generateIf(StackMapper mapper, Flow flow, byte[] byteCodes, int i, final CharSequence v2, final CharSequence v1, final String test, int topMostLabel) throws IOException {
1.156 mapper.flush(this);
1.157 int indx = i + readShortArg(byteCodes, i);
1.158 append("if ((").append(v1)
1.159 .append(") ").append(test).append(" (")
1.160 .append(v2).append(")) ");
1.161 - goTo(this, i, indx, topMostLabel);
1.162 + goTo(this, flow, i, indx, topMostLabel);
1.163 return i + 2;
1.164 }
1.165
1.166 @@ -2249,7 +2246,7 @@
1.167 out.append(format, processed, length);
1.168 }
1.169
1.170 - private void generateCatch(TrapData[] traps, int current, int topMostLabel) throws IOException {
1.171 + private void generateCatch(TrapData[] traps, Flow flow, int current, int topMostLabel) throws IOException {
1.172 append("} catch (e) {\n");
1.173 int finallyPC = -1;
1.174 for (TrapData e : traps) {
1.175 @@ -2262,7 +2259,7 @@
1.176 append("e = vm.java_lang_Class(false).bck2BrwsrThrwrbl(e);");
1.177 append("if (e['$instOf_" + classInternalName.replace('/', '_') + "']) {");
1.178 append("var stA0 = e;");
1.179 - goTo(this, current, e.handler_pc, topMostLabel);
1.180 + goTo(this, flow, current, e.handler_pc, topMostLabel);
1.181 append("}\n");
1.182 } else {
1.183 finallyPC = e.handler_pc;
1.184 @@ -2272,12 +2269,12 @@
1.185 append("throw e;");
1.186 } else {
1.187 append("var stA0 = e;");
1.188 - goTo(this, current, finallyPC, topMostLabel);
1.189 + goTo(this, flow, current, finallyPC, topMostLabel);
1.190 }
1.191 append("\n}");
1.192 }
1.193
1.194 - private static void goTo(Appendable out, int current, int to, int canBack) throws IOException {
1.195 + private static void goTo(Appendable out, Flow flow, int current, int to, int canBack) throws IOException {
1.196 if (to < current) {
1.197 if (canBack < to) {
1.198 out.append("{ gt = 0; continue X_" + to + "; }");
1.199 @@ -2290,14 +2287,11 @@
1.200 }
1.201
1.202 private static void emitIf(
1.203 - StackMapper sm,
1.204 - Appendable out, String pattern,
1.205 - CharSequence param,
1.206 - int current, int to, int canBack
1.207 + StackMapper sm, Flow flow, Appendable out, String pattern, CharSequence param, int current, int to, int canBack
1.208 ) throws IOException {
1.209 sm.flush(out);
1.210 emitImpl(out, pattern, param);
1.211 - goTo(out, current, to, canBack);
1.212 + goTo(out, flow, current, to, canBack);
1.213 }
1.214
1.215 private void generateNewArray(int atype, final StackMapper smapper) throws IOException, IllegalStateException {
1.216 @@ -2355,7 +2349,7 @@
1.217 return i;
1.218 }
1.219
1.220 - private int generateTableSwitch(int i, final byte[] byteCodes, final StackMapper smapper, int topMostLabel) throws IOException {
1.221 + private int generateTableSwitch(int i, final byte[] byteCodes, final StackMapper smapper, Flow flow, int topMostLabel) throws IOException {
1.222 int table = i / 4 * 4 + 4;
1.223 int dflt = i + readInt4(byteCodes, table);
1.224 table += 4;
1.225 @@ -2369,17 +2363,17 @@
1.226 while (low <= high) {
1.227 int offset = i + readInt4(byteCodes, table);
1.228 table += 4;
1.229 - append(" case " + low).append(":"); goTo(this, i, offset, topMostLabel); append('\n');
1.230 + append(" case " + low).append(":"); goTo(this, flow, i, offset, topMostLabel); append('\n');
1.231 low++;
1.232 }
1.233 append(" default: ");
1.234 - goTo(this, i, dflt, topMostLabel);
1.235 + goTo(this, flow, i, dflt, topMostLabel);
1.236 append("\n}");
1.237 i = table - 1;
1.238 return i;
1.239 }
1.240
1.241 - private int generateLookupSwitch(int i, final byte[] byteCodes, final StackMapper smapper, int topMostLabel) throws IOException {
1.242 + private int generateLookupSwitch(int i, final byte[] byteCodes, final StackMapper smapper, Flow flow, int topMostLabel) throws IOException {
1.243 int table = i / 4 * 4 + 4;
1.244 int dflt = i + readInt4(byteCodes, table);
1.245 table += 4;
1.246 @@ -2393,10 +2387,10 @@
1.247 table += 4;
1.248 int offset = i + readInt4(byteCodes, table);
1.249 table += 4;
1.250 - append(" case " + cnstnt).append(": "); goTo(this, i, offset, topMostLabel); append('\n');
1.251 + append(" case " + cnstnt).append(": "); goTo(this, flow, i, offset, topMostLabel); append('\n');
1.252 }
1.253 append(" default: ");
1.254 - goTo(this, i, dflt, topMostLabel);
1.255 + goTo(this, flow, i, dflt, topMostLabel);
1.256 append("\n}");
1.257 i = table - 1;
1.258 return i;