Skip to content

Commit 6c9beb7

Browse files
committed
Unwind/rewind: properly open/close exception handlers for tags
1 parent bf565a0 commit 6c9beb7

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,7 +1500,6 @@ public void testFinallyTry() {
15001500
events,
15011501
new Event(EventKind.ENTER, 0x0000, 0x0038, null, StatementTag.class),
15021502
new Event(EventKind.ENTER, 0x0002, 0x001d, null, ExpressionTag.class),
1503-
// TODO: tag range needs to be closed when copying finally handler
15041503
new Event(EventKind.RETURN_VALUE, 0x0002, 0x001d, 42L, ExpressionTag.class),
15051504
new Event(EventKind.ENTER, 0x000d, 0x0017, null, StatementTag.class),
15061505
new Event(EventKind.RETURN_VALUE, 0x000d, 0x0017, null, StatementTag.class),
@@ -1512,10 +1511,7 @@ public void testFinallyTry() {
15121511
events,
15131512
new Event(EventKind.ENTER, 0x0000, 0x0038, null, StatementTag.class),
15141513
new Event(EventKind.ENTER, 0x0002, 0x001d, null, ExpressionTag.class),
1515-
// TODO: missing ExpressionTag exception event. need to emit multiple ranges
1516-
// when inlining finally handler.
1517-
// new Event(EventKind.EXCEPTIONAL, 0x0002, 0x001d, null,
1518-
// ExpressionTag.class),
1514+
new Event(EventKind.EXCEPTIONAL, 0x0002, 0x001d, TestException.class, ExpressionTag.class),
15191515
new Event(EventKind.ENTER, 0x002b, 0x0035, null, StatementTag.class),
15201516
new Event(EventKind.RETURN_VALUE, 0x002b, 0x0035, null, StatementTag.class),
15211517
new Event(EventKind.RETURN_VALUE, 0x0000, 0x0038, null, StatementTag.class));
@@ -1728,6 +1724,7 @@ public void testFinallyTryYieldInTry() {
17281724
events,
17291725
new Event(EventKind.RESUME, 0x0000, 0x003b, null, StatementTag.class),
17301726
new Event(EventKind.RESUME, 0x0002, 0x0024, null, ExpressionTag.class),
1727+
new Event(EventKind.EXCEPTIONAL, 0x0002, 0x0024, TestException.class, ExpressionTag.class),
17311728
new Event(EventKind.ENTER, 0x0031, 0x0035, null, StatementTag.class),
17321729
new Event(EventKind.RETURN_VALUE, 0x0031, 0x0035, 123L, StatementTag.class),
17331730
new Event(EventKind.EXCEPTIONAL, 0x0000, 0x003b, TestException.class, StatementTag.class));
@@ -1818,8 +1815,7 @@ public void testFinallyTryYieldInFinally() {
18181815
events,
18191816
new Event(EventKind.ENTER, 0x0000, 0x004f, null, StatementTag.class),
18201817
new Event(EventKind.ENTER, 0x0002, 0x0024, null, ExpressionTag.class),
1821-
// TODO: missing ExpressionTag exception event. need to emit multiple ranges
1822-
// when inlining finally handler.
1818+
new Event(EventKind.EXCEPTIONAL, 0x0002, 0x0024, TestException.class, ExpressionTag.class),
18231819
new Event(EventKind.ENTER, 0x003b, 0x0049, null, StatementTag.class),
18241820
new Event(EventKind.YIELD, 0x003b, 0x0049, 123L, StatementTag.class),
18251821
new Event(EventKind.YIELD, 0x0000, 0x004f, 123L, StatementTag.class));

truffle/src/com.oracle.truffle.api.bytecode.test/src/com/oracle/truffle/api/bytecode/test/basic_interpreter/ExceptionHandlerTableTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ public void testTryCatchInTag() {
272272
assertHandlers(root, handler(0, "ex1"));
273273

274274
root.getRootNodes().update(createBytecodeConfigBuilder().addTag(ExpressionTag.class).build());
275-
assertHandlers(root, tag(2, handler(0, "ex1"), handler(1, "ex1")));
275+
assertHandlers(root, tag(1, handler(0, "ex1")), tag(3, handler(2, "ex1")));
276276
}
277277

278278
@Test
@@ -312,7 +312,7 @@ public void testTryCatchBranchOutOfTag() {
312312
assertHandlers(root, handler(0));
313313

314314
root.getRootNodes().update(createBytecodeConfigBuilder().addTag(ExpressionTag.class).build());
315-
assertHandlers(root, tag(2, handler(0, "ex1"), handler(1, "ex1")));
315+
assertHandlers(root, tag(1, handler(0, "ex1")), tag(3, handler(2, "ex1")));
316316
}
317317

318318
@Test

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1758,13 +1758,16 @@ private String getDataClassName(OperationModel operation) {
17581758
class OperationDataClassesFactory {
17591759

17601760
private Collection<CodeTypeElement> create() {
1761-
scopeDataType = new CodeTypeElement(Set.of(PRIVATE, STATIC, ABSTRACT), ElementKind.CLASS, null, "ScopeData");
1762-
scopeDataType.add(new CodeVariableElement(Set.of(), type(int.class), "frameOffset"));
1763-
scopeDataType.add(new CodeVariableElement(Set.of(), type(int[].class), "locals"));
1764-
scopeDataType.add(new CodeVariableElement(Set.of(), type(int.class), "localsCount"));
1765-
scopeDataType.add(new CodeVariableElement(Set.of(), type(boolean.class), "valid")).createInitBuilder().string("true");
17661761
List<CodeTypeElement> result = new ArrayList<>();
1767-
result.add(scopeDataType);
1762+
1763+
if (model.enableLocalScoping) {
1764+
scopeDataType = new CodeTypeElement(Set.of(PRIVATE, STATIC, ABSTRACT), ElementKind.CLASS, null, "ScopeData");
1765+
scopeDataType.add(new CodeVariableElement(Set.of(), type(int.class), "frameOffset"));
1766+
scopeDataType.add(new CodeVariableElement(Set.of(), type(int[].class), "locals"));
1767+
scopeDataType.add(new CodeVariableElement(Set.of(), type(int.class), "localsCount"));
1768+
scopeDataType.add(new CodeVariableElement(Set.of(), type(boolean.class), "valid")).createInitBuilder().string("true");
1769+
result.add(scopeDataType);
1770+
}
17681771

17691772
Map<String, CodeTypeElement> dataClassNames = new LinkedHashMap<>();
17701773
for (OperationModel operation : model.getOperations()) {
@@ -1826,6 +1829,7 @@ private CodeTypeElement createDataClass(OperationModel operation) {
18261829
field(context.getType(boolean.class), "operationReachable").asFinal(),
18271830
field(context.getType(int.class), "startStackHeight").asFinal(),
18281831
field(tagNode.asType(), "node").asFinal(),
1832+
field(context.getType(int.class), "handlerStartBci").withInitializer("node.enterBci"),
18291833
field(context.getType(boolean.class), "producedValue").withInitializer("false"),
18301834
field(context.getType(int.class), "childBci").withInitializer(UNINIT),
18311835
field(generic(type(List.class), tagNode.asType()), "children").withInitializer("null"));
@@ -4656,7 +4660,7 @@ private CodeExecutableElement createEnd(OperationModel operation) {
46564660
*/
46574661
b.statement("markReachable(true)");
46584662
buildEmitInstruction(b, model.tagLeaveVoidInstruction, "operationData.nodeId");
4659-
b.statement("doCreateExceptionHandler(tagNode.enterBci, bci, HANDLER_TAG_EXCEPTIONAL, operationData.nodeId, operationData.startStackHeight)");
4663+
b.statement("doCreateExceptionHandler(operationData.handlerStartBci, bci, HANDLER_TAG_EXCEPTIONAL, operationData.nodeId, operationData.startStackHeight)");
46604664
b.end().startElseBlock();
46614665
buildEmitInstruction(b, model.tagLeaveVoidInstruction, "operationData.nodeId");
46624666
b.end();
@@ -6829,6 +6833,7 @@ private void emitExitInstructionsStackWalk(CodeTreeBuilder b, OperationKind oper
68296833
assert operationKind == OperationKind.BRANCH;
68306834
buildEmitInstruction(b, model.tagLeaveVoidInstruction, "operationData.nodeId");
68316835
}
6836+
b.statement("doCreateExceptionHandler(operationData.handlerStartBci, bci, HANDLER_TAG_EXCEPTIONAL, operationData.nodeId, operationData.startStackHeight)");
68326837
b.statement("break");
68336838
b.end(); // case tag
68346839
}
@@ -6895,6 +6900,15 @@ private void emitReopenHandlersStackWalk(CodeTreeBuilder b, String lowestOperati
68956900
buildOperationStackWalk(b, lowestOperationIndex, () -> {
68966901
b.startSwitch().string("operationStack[i].operation").end().startBlock();
68976902

6903+
if (model.enableTagInstrumentation) {
6904+
b.startCase().tree(createOperationConstant(model.tagOperation)).end();
6905+
b.startBlock();
6906+
emitCastOperationData(b, model.tagOperation, "i");
6907+
b.statement("operationData.handlerStartBci = bci");
6908+
b.statement("break");
6909+
b.end();
6910+
}
6911+
68986912
b.startCase().tree(createOperationConstant(model.findOperation(OperationKind.FINALLY_TRY))).end();
68996913
b.startCase().tree(createOperationConstant(model.findOperation(OperationKind.FINALLY_TRY_CATCH))).end();
69006914
b.startCaseBlock();

0 commit comments

Comments
 (0)