Skip to content

Commit 1f1939c

Browse files
committed
Fix Truffle compilation of exception handlers
1 parent 43c76ee commit 1f1939c

File tree

1 file changed

+79
-81
lines changed

1 file changed

+79
-81
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/bytecode/PBytecodeRootNode.java

Lines changed: 79 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -2227,7 +2227,7 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
22272227
bci = 0;
22282228
stackTop = getInitialStackTop();
22292229
oparg = 0;
2230-
notifyStatementAfterException(virtualFrame, instrumentation, beginBci);
2230+
notifyStatementAfterException(virtualFrame, instrumentation, bci);
22312231
continue;
22322232
} else if (result != null) {
22332233
return result;
@@ -2236,94 +2236,51 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
22362236
if (e instanceof ThreadDeath) {
22372237
throw e;
22382238
}
2239-
int targetIndex = handleException(virtualFrame, localFrame, isGeneratorOrCoroutine, noTraceOrProfile, instrumentation, mutableData, bciSlot, initialStackTop, beginBci, stackTop, e);
2239+
PException pe = null;
2240+
boolean isInteropException = false;
2241+
if (e instanceof PException) {
2242+
pe = (PException) e;
2243+
} else if (e instanceof AbstractTruffleException) {
2244+
isInteropException = true;
2245+
} else {
2246+
pe = wrapJavaExceptionIfApplicable(e);
2247+
if (pe == null) {
2248+
throw e;
2249+
}
2250+
}
2251+
2252+
tracingEnabled = isTracingEnabled(noTraceOrProfile, mutableData);
2253+
profilingEnabled = isProfilingEnabled(noTraceOrProfile, mutableData);
2254+
if (tracingEnabled && pe != null && !mutableData.getThreadState(this).isTracing()) {
2255+
traceException(virtualFrame, mutableData, beginBci, pe);
2256+
}
2257+
2258+
int targetIndex = findHandler(beginBci);
2259+
CompilerAsserts.partialEvaluationConstant(targetIndex);
2260+
chainPythonExceptions(virtualFrame, mutableData, pe);
22402261
if (targetIndex == -1) {
2262+
reraiseUnhandledException(virtualFrame, localFrame, initialStackTop, isGeneratorOrCoroutine, mutableData, bciSlot, beginBci, e, pe, tracingEnabled, profilingEnabled);
22412263
throw e;
22422264
}
2243-
stackTop = stackoffset + exceptionHandlerRanges[targetIndex + 1];
2265+
if (pe != null) {
2266+
pe.setCatchingFrameReference(virtualFrame, this, beginBci);
2267+
}
2268+
int stackSizeOnEntry = exceptionHandlerRanges[targetIndex + 1];
2269+
int targetStackTop = stackSizeOnEntry + stackoffset;
2270+
stackTop = unwindBlock(virtualFrame, stackTop, targetStackTop);
2271+
/*
2272+
* Handler range encodes the stack size, not the top of stack. so the stackTop is to
2273+
* be replaced with the exception
2274+
*/
2275+
virtualFrame.setObject(stackTop, isInteropException ? e : pe);
22442276
bci = exceptionHandlerRanges[targetIndex];
22452277
oparg = 0;
2246-
}
2247-
}
2248-
}
22492278

2250-
@InliningCutoff
2251-
private int handleException(VirtualFrame virtualFrame, Frame localFrame, boolean isGeneratorOrCoroutine, Assumption noTraceOrProfile, InstrumentationSupport instrumentation,
2252-
MutableLoopData mutableData, int bciSlot, int initialStackTop,
2253-
int beginBci, int stackTop, Throwable e) {
2254-
PException pe = null;
2255-
boolean isInteropException = false;
2256-
if (e instanceof PException) {
2257-
pe = (PException) e;
2258-
} else if (e instanceof AbstractTruffleException) {
2259-
isInteropException = true;
2260-
} else {
2261-
pe = wrapJavaExceptionIfApplicable(e);
2262-
if (pe == null) {
2263-
return -1;
2264-
}
2265-
}
2266-
2267-
boolean tracingEnabled = isTracingEnabled(noTraceOrProfile, mutableData);
2268-
boolean profilingEnabled = isProfilingEnabled(noTraceOrProfile, mutableData);
2269-
if (tracingEnabled && pe != null && !mutableData.getThreadState(this).isTracing()) {
2270-
traceException(virtualFrame, mutableData, beginBci, pe);
2271-
}
2272-
2273-
int targetIndex = findHandler(beginBci);
2274-
CompilerAsserts.partialEvaluationConstant(targetIndex);
2275-
if (pe != null) {
2276-
if (mutableData.localException != null) {
2277-
ExceptionHandlingStatementNode.chainExceptions(pe.getUnreifiedException(), mutableData.localException, exceptionChainProfile1, exceptionChainProfile2);
2278-
} else {
2279-
if (getCaughtExceptionNode == null) {
2280-
CompilerDirectives.transferToInterpreterAndInvalidate();
2281-
getCaughtExceptionNode = insert(ExceptionStateNodes.GetCaughtExceptionNode.create());
2282-
}
2283-
PException exceptionState = getCaughtExceptionNode.execute(virtualFrame);
2284-
if (exceptionState != null) {
2285-
ExceptionHandlingStatementNode.chainExceptions(pe.getUnreifiedException(), exceptionState, exceptionChainProfile1, exceptionChainProfile2);
2286-
}
2287-
}
2288-
}
2289-
if (targetIndex == -1) {
2290-
// For tracebacks
2291-
setCurrentBci(virtualFrame, bciSlot, beginBci);
2292-
if (isGeneratorOrCoroutine) {
2293-
if (localFrame != virtualFrame) {
2294-
// Unwind the generator frame stack
2295-
clearFrameSlots(localFrame, stackoffset, initialStackTop);
2279+
if (instrumentation != null) {
2280+
notifyStatementAfterException(virtualFrame, instrumentation, bci);
22962281
}
22972282
}
2298-
if (CompilerDirectives.hasNextTier() && mutableData.loopCount > 0) {
2299-
LoopNode.reportLoopCount(this, mutableData.loopCount);
2300-
}
2301-
traceOrProfileReturn(virtualFrame, mutableData, PNone.NONE, tracingEnabled, profilingEnabled);
2302-
if (e == pe) {
2303-
throw pe;
2304-
} else if (pe != null) {
2305-
throw pe.getExceptionForReraise();
2306-
} else {
2307-
return -1;
2308-
}
2309-
}
2310-
if (pe != null) {
2311-
pe.setCatchingFrameReference(virtualFrame, this, beginBci);
2312-
}
2313-
int stackSizeOnEntry = exceptionHandlerRanges[targetIndex + 1];
2314-
int targetStackTop = stackSizeOnEntry + stackoffset;
2315-
unwindBlock(virtualFrame, stackTop, targetStackTop);
2316-
/*
2317-
* Handler range encodes the stack size, not the top of stack. so the stackTop is to be
2318-
* replaced with the exception
2319-
*/
2320-
virtualFrame.setObject(targetStackTop, isInteropException ? e : pe);
2321-
2322-
if (instrumentation != null) {
2323-
int bci = exceptionHandlerRanges[targetIndex];
2324-
notifyStatementAfterException(virtualFrame, instrumentation, bci);
23252283
}
2326-
return targetIndex;
23272284
}
23282285

23292286
@InliningCutoff
@@ -2639,6 +2596,46 @@ private void unboxVariables(Frame localFrame) {
26392596
}
26402597
}
26412598

2599+
@InliningCutoff
2600+
private void reraiseUnhandledException(VirtualFrame virtualFrame, Frame localFrame, int initialStackTop, boolean isGeneratorOrCoroutine, MutableLoopData mutableData, int bciSlot, int beginBci,
2601+
Throwable e, PException pe, boolean tracingEnabled, boolean profilingEnabled) {
2602+
// For tracebacks
2603+
setCurrentBci(virtualFrame, bciSlot, beginBci);
2604+
if (isGeneratorOrCoroutine) {
2605+
if (localFrame != virtualFrame) {
2606+
// Unwind the generator frame stack
2607+
clearFrameSlots(localFrame, stackoffset, initialStackTop);
2608+
}
2609+
}
2610+
if (CompilerDirectives.hasNextTier() && mutableData.loopCount > 0) {
2611+
LoopNode.reportLoopCount(this, mutableData.loopCount);
2612+
}
2613+
traceOrProfileReturn(virtualFrame, mutableData, PNone.NONE, tracingEnabled, profilingEnabled);
2614+
if (e == pe) {
2615+
throw pe;
2616+
} else if (pe != null) {
2617+
throw pe.getExceptionForReraise();
2618+
}
2619+
}
2620+
2621+
@InliningCutoff
2622+
private void chainPythonExceptions(VirtualFrame virtualFrame, MutableLoopData mutableData, PException pe) {
2623+
if (pe != null) {
2624+
if (mutableData.localException != null) {
2625+
ExceptionHandlingStatementNode.chainExceptions(pe.getUnreifiedException(), mutableData.localException, exceptionChainProfile1, exceptionChainProfile2);
2626+
} else {
2627+
if (getCaughtExceptionNode == null) {
2628+
CompilerDirectives.transferToInterpreterAndInvalidate();
2629+
getCaughtExceptionNode = insert(ExceptionStateNodes.GetCaughtExceptionNode.create());
2630+
}
2631+
PException exceptionState = getCaughtExceptionNode.execute(virtualFrame);
2632+
if (exceptionState != null) {
2633+
ExceptionHandlingStatementNode.chainExceptions(pe.getUnreifiedException(), exceptionState, exceptionChainProfile1, exceptionChainProfile2);
2634+
}
2635+
}
2636+
}
2637+
}
2638+
26422639
private PException raiseUnkownBytecodeError(final byte bc) {
26432640
CompilerDirectives.transferToInterpreterAndInvalidate();
26442641
throw PRaiseNode.raiseUncached(this, SystemError, toTruffleStringUncached("not implemented bytecode %s"), OpCodes.fromOpCode(bc));
@@ -4943,12 +4940,13 @@ private int findHandler(int bci) {
49434940

49444941
@InliningCutoff
49454942
@ExplodeLoop
4946-
private static void unwindBlock(VirtualFrame virtualFrame, int stackTop, int stackTopBeforeBlock) {
4943+
private static int unwindBlock(VirtualFrame virtualFrame, int stackTop, int stackTopBeforeBlock) {
49474944
CompilerAsserts.partialEvaluationConstant(stackTop);
49484945
CompilerAsserts.partialEvaluationConstant(stackTopBeforeBlock);
49494946
for (int i = stackTop; i > stackTopBeforeBlock; i--) {
49504947
virtualFrame.setObject(i, null);
49514948
}
4949+
return stackTopBeforeBlock;
49524950
}
49534951

49544952
public PCell readClassCell(VirtualFrame virtualFrame) {

0 commit comments

Comments
 (0)