Skip to content

Commit ecd6500

Browse files
committed
Bytecode DSL: fix reachability analysis in try-catch-otherwise
1 parent 82ae059 commit ecd6500

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

truffle/src/com.oracle.truffle.api.bytecode.test/src/com/oracle/truffle/api/bytecode/test/DeadCodeTest.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,69 @@ public void testUnreachableTryCatchOtherwise1() {
272272
assertEquals(41, node.getCallTarget().call(42));
273273
}
274274

275+
@Test
276+
public void testReachableTryCatchOtherwise() {
277+
// @formatter:off
278+
// try {
279+
// if (argument[0])
280+
// throw
281+
// } catch ex {
282+
// if (argument[1])
283+
// branch end;
284+
// return 1
285+
// end:
286+
// return 2
287+
// } otherwise {
288+
// return 3
289+
// }
290+
// <dead>
291+
// @formatter:on
292+
DeadCodeTestRootNode node = (DeadCodeTestRootNode) parse(b -> {
293+
b.beginRoot();
294+
295+
b.beginTryCatchOtherwise(() -> {
296+
b.beginReturn();
297+
b.emitLoadConstant(3);
298+
b.endReturn();
299+
});
300+
301+
// @formatter:off
302+
// try:
303+
b.beginIfThen();
304+
b.emitLoadArgument(0);
305+
b.emitThrow();
306+
b.endIfThen();
307+
308+
// catch:
309+
b.beginBlock();
310+
b.beginBlock();
311+
BytecodeLabel endLabel = b.createLabel();
312+
b.beginIfThen();
313+
b.emitLoadArgument(1);
314+
b.emitBranch(endLabel);
315+
b.endIfThen();
316+
317+
b.beginReturn();
318+
b.emitLoadConstant(1);
319+
b.endReturn();
320+
321+
b.emitLabel(endLabel);
322+
b.endBlock();
323+
b.beginReturn();
324+
b.emitLoadConstant(2);
325+
b.endReturn();
326+
b.endBlock();
327+
// @formatter:on
328+
329+
b.endTryCatchOtherwise();
330+
b.endRoot();
331+
}).getRootNode();
332+
333+
assertEquals(3, node.getCallTarget().call(false, false));
334+
assertEquals(2, node.getCallTarget().call(true, true));
335+
assertEquals(1, node.getCallTarget().call(true, false));
336+
}
337+
275338
@Test
276339
public void testUnreachableTryCatchOtherwise2() {
277340
// @formatter:off

truffle/src/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/bytecode/generator/BytecodeRootNodeElement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3237,7 +3237,7 @@ private CodeExecutableElement createUpdateReachable() {
32373237
b.startIf().string("operation.childCount == 0").end().startBlock();
32383238
b.statement("state.reachable = " + operationStack.read(op, operationFields.tryReachable));
32393239
if (op.kind == OperationKind.TRY_CATCH_OTHERWISE) {
3240-
b.end().startElseIf().string("operation.childCount == 2").end().startBlock();
3240+
b.end().startElseIf().string("operation.childCount == 1").end().startBlock();
32413241
b.statement("state.reachable = " + operationStack.read(op, operationFields.catchReachable));
32423242
}
32433243
b.end().startElseBlock();

0 commit comments

Comments
 (0)