Skip to content

Commit 26adad9

Browse files
committed
Extract exception handling to a method
1 parent 0f4caef commit 26adad9

File tree

1 file changed

+92
-113
lines changed

1 file changed

+92
-113
lines changed

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

Lines changed: 92 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -2167,63 +2167,104 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
21672167
} catch (OSRException e) {
21682168
// Exception from OSR was already handled in the OSR code
21692169
throw e.exception;
2170-
} catch (Exception | StackOverflowError | AssertionError e) {
2171-
PException pe = null;
2172-
boolean isInteropException = false;
2173-
if (e instanceof PException) {
2174-
pe = (PException) e;
2175-
} else if (e instanceof AbstractTruffleException) {
2176-
isInteropException = true;
2177-
} else {
2178-
pe = wrapJavaExceptionIfApplicable(e);
2179-
if (pe == null) {
2180-
throw e;
2181-
}
2182-
}
2183-
2184-
tracingEnabled = isTracingEnabled(noTraceOrProfile, mutableData);
2185-
if (tracingEnabled && pe != null && !mutableData.getThreadState(this).isTracing()) {
2186-
traceException(virtualFrame, mutableData, bci, pe);
2187-
}
2170+
} catch (Throwable e) {
21882171
if (instrumentation != null) {
2189-
Object result = notifyException(virtualFrame, instrumentation, returnCalled, beginBci, e);
2190-
if (result != null) {
2172+
// Need to handle instrumentation frame unwind
2173+
Object result = notifyException(virtualFrame, instrumentation, returnCalled, bci, e);
2174+
if (result == ProbeNode.UNWIND_ACTION_REENTER) {
2175+
bci = 0;
2176+
stackTop = getInitialStackTop();
2177+
oparg = 0;
2178+
continue;
2179+
} else if (result != null) {
21912180
return result;
21922181
}
21932182
}
2194-
2195-
int targetIndex = findHandler(beginBci);
2196-
CompilerAsserts.partialEvaluationConstant(targetIndex);
2197-
chainPythonExceptions(virtualFrame, mutableData, pe);
2183+
if (e instanceof ThreadDeath) {
2184+
throw e;
2185+
}
2186+
int targetIndex = handleException(virtualFrame, localFrame, isGeneratorOrCoroutine, noTraceOrProfile, mutableData, bciSlot, initialStackTop, beginBci, stackTop, e);
21982187
if (targetIndex == -1) {
2199-
reraiseUnhandledException(virtualFrame, localFrame, initialStackTop, mutableData, isGeneratorOrCoroutine, bciSlot, beginBci, e, pe, tracingEnabled, profilingEnabled);
22002188
throw e;
2201-
} else {
2202-
if (pe != null) {
2203-
pe.setCatchingFrameReference(virtualFrame, this, beginBci);
2204-
}
2205-
int stackSizeOnEntry = exceptionHandlerRanges[targetIndex + 1];
2206-
stackTop = unwindBlock(virtualFrame, stackTop, stackSizeOnEntry + stackoffset);
2207-
// handler range encodes the stack size, not the top of stack. so the stackTop
2208-
// is to be replaced with the exception
2209-
virtualFrame.setObject(stackTop, isInteropException ? e : pe);
2210-
bci = exceptionHandlerRanges[targetIndex];
2211-
oparg = 0;
22122189
}
2213-
} catch (ThreadDeath t) {
2214-
// Need to handle instrumentation frame unwind
2215-
Object result = handlePossibleReenter(virtualFrame, t, returnCalled);
2216-
if (result == ProbeNode.UNWIND_ACTION_REENTER) {
2217-
bci = 0;
2218-
stackTop = getInitialStackTop();
2219-
oparg = 0;
2220-
continue;
2221-
} else if (result != null) {
2222-
return result;
2190+
stackTop = stackoffset + exceptionHandlerRanges[targetIndex + 1];
2191+
bci = exceptionHandlerRanges[targetIndex];
2192+
oparg = 0;
2193+
}
2194+
}
2195+
}
2196+
2197+
@InliningCutoff
2198+
private int handleException(VirtualFrame virtualFrame, Frame localFrame, boolean isGeneratorOrCoroutine, Assumption noTraceOrProfile, MutableLoopData mutableData, int bciSlot, int initialStackTop,
2199+
int beginBci, int stackTop, Throwable e) {
2200+
PException pe = null;
2201+
boolean isInteropException = false;
2202+
if (e instanceof PException) {
2203+
pe = (PException) e;
2204+
} else if (e instanceof AbstractTruffleException) {
2205+
isInteropException = true;
2206+
} else {
2207+
pe = wrapJavaExceptionIfApplicable(e);
2208+
if (pe == null) {
2209+
return -1;
2210+
}
2211+
}
2212+
2213+
boolean tracingEnabled = isTracingEnabled(noTraceOrProfile, mutableData);
2214+
boolean profilingEnabled = isProfilingEnabled(noTraceOrProfile, mutableData);
2215+
if (tracingEnabled && pe != null && !mutableData.getThreadState(this).isTracing()) {
2216+
traceException(virtualFrame, mutableData, beginBci, pe);
2217+
}
2218+
2219+
int targetIndex = findHandler(beginBci);
2220+
CompilerAsserts.partialEvaluationConstant(targetIndex);
2221+
if (pe != null) {
2222+
if (mutableData.localException != null) {
2223+
ExceptionHandlingStatementNode.chainExceptions(pe.getUnreifiedException(), mutableData.localException, exceptionChainProfile1, exceptionChainProfile2);
2224+
} else {
2225+
if (getCaughtExceptionNode == null) {
2226+
CompilerDirectives.transferToInterpreterAndInvalidate();
2227+
getCaughtExceptionNode = insert(ExceptionStateNodes.GetCaughtExceptionNode.create());
2228+
}
2229+
PException exceptionState = getCaughtExceptionNode.execute(virtualFrame);
2230+
if (exceptionState != null) {
2231+
ExceptionHandlingStatementNode.chainExceptions(pe.getUnreifiedException(), exceptionState, exceptionChainProfile1, exceptionChainProfile2);
22232232
}
2224-
throw t;
22252233
}
22262234
}
2235+
if (targetIndex == -1) {
2236+
// For tracebacks
2237+
setCurrentBci(virtualFrame, bciSlot, beginBci);
2238+
if (isGeneratorOrCoroutine) {
2239+
if (localFrame != virtualFrame) {
2240+
// Unwind the generator frame stack
2241+
clearFrameSlots(localFrame, stackoffset, initialStackTop);
2242+
}
2243+
}
2244+
if (CompilerDirectives.hasNextTier() && mutableData.loopCount > 0) {
2245+
LoopNode.reportLoopCount(this, mutableData.loopCount);
2246+
}
2247+
traceOrProfileReturn(virtualFrame, mutableData, PNone.NONE, tracingEnabled, profilingEnabled);
2248+
if (e == pe) {
2249+
throw pe;
2250+
} else if (pe != null) {
2251+
throw pe.getExceptionForReraise();
2252+
} else {
2253+
return -1;
2254+
}
2255+
}
2256+
if (pe != null) {
2257+
pe.setCatchingFrameReference(virtualFrame, this, beginBci);
2258+
}
2259+
int stackSizeOnEntry = exceptionHandlerRanges[targetIndex + 1];
2260+
int targetStackTop = stackSizeOnEntry + stackoffset;
2261+
unwindBlock(virtualFrame, stackTop, targetStackTop);
2262+
/*
2263+
* Handler range encodes the stack size, not the top of stack. so the stackTop is to be
2264+
* replaced with the exception
2265+
*/
2266+
virtualFrame.setObject(targetStackTop, isInteropException ? e : pe);
2267+
return targetIndex;
22272268
}
22282269

22292270
@InliningCutoff
@@ -2232,11 +2273,10 @@ private Object notifyException(VirtualFrame virtualFrame, InstrumentationSupport
22322273
if (instrumentationRoot instanceof WrapperNode) {
22332274
WrapperNode wrapper = (WrapperNode) instrumentationRoot;
22342275
Object result = wrapper.getProbeNode().onReturnExceptionalOrUnwind(virtualFrame, e, returnCalled);
2235-
if (result == ProbeNode.UNWIND_ACTION_REENTER) {
2236-
throw CompilerDirectives.shouldNotReachHere("Reenter shouldn't occur at this point");
2237-
} else if (result != null) {
2276+
if (result != null) {
22382277
if (co.isGeneratorOrCoroutine()) {
2239-
throw CompilerDirectives.shouldNotReachHere("Cannot replace return value of generators");
2278+
CompilerDirectives.transferToInterpreterAndInvalidate();
2279+
throw new IllegalStateException(result == ProbeNode.UNWIND_ACTION_REENTER ? "Frame restarting is not possible in generators" : "Cannot replace return value of generators");
22402280
}
22412281
}
22422282
return result;
@@ -2259,23 +2299,6 @@ private void notifyStatement(VirtualFrame virtualFrame, InstrumentationSupport i
22592299
mutableData.setPastBci(bci);
22602300
}
22612301

2262-
@InliningCutoff
2263-
private Object handlePossibleReenter(VirtualFrame virtualFrame, ThreadDeath t, boolean returnCalled) {
2264-
if (instrumentationRoot instanceof WrapperNode) {
2265-
WrapperNode wrapper = (WrapperNode) instrumentationRoot;
2266-
Object result = wrapper.getProbeNode().onReturnExceptionalOrUnwind(virtualFrame, t, returnCalled);
2267-
if (result == ProbeNode.UNWIND_ACTION_REENTER) {
2268-
if (co.isGeneratorOrCoroutine()) {
2269-
CompilerDirectives.transferToInterpreterAndInvalidate();
2270-
throw new UnsupportedOperationException("Frame restarting is not possible in generators");
2271-
}
2272-
copyArgs(virtualFrame.getArguments(), virtualFrame);
2273-
}
2274-
return result;
2275-
}
2276-
return null;
2277-
}
2278-
22792302
@InliningCutoff
22802303
private Object enterRoot(VirtualFrame virtualFrame) {
22812304
if (instrumentationRoot instanceof WrapperNode) {
@@ -2538,49 +2561,6 @@ private void unboxVariables(Frame localFrame) {
25382561
}
25392562
}
25402563

2541-
@InliningCutoff
2542-
private void reraiseUnhandledException(VirtualFrame virtualFrame, Frame localFrame, int initialStackTop, MutableLoopData mutableData,
2543-
boolean isGeneratorOrCoroutine, final int bciSlot, final int beginBci, Throwable e, PException pe,
2544-
boolean tracingEnabled, boolean profilingEnabled) {
2545-
// For tracebacks
2546-
setCurrentBci(virtualFrame, bciSlot, beginBci);
2547-
if (isGeneratorOrCoroutine) {
2548-
if (localFrame != virtualFrame) {
2549-
// Unwind the generator frame stack
2550-
clearFrameSlots(localFrame, stackoffset, initialStackTop);
2551-
}
2552-
}
2553-
if (CompilerDirectives.hasNextTier() && mutableData.loopCount > 0) {
2554-
LoopNode.reportLoopCount(this, mutableData.loopCount);
2555-
}
2556-
traceOrProfileReturn(virtualFrame, mutableData, PNone.NONE, tracingEnabled, profilingEnabled);
2557-
if (e == pe) {
2558-
throw pe;
2559-
} else if (pe != null) {
2560-
throw pe.getExceptionForReraise();
2561-
} else {
2562-
return;
2563-
}
2564-
}
2565-
2566-
@InliningCutoff
2567-
private void chainPythonExceptions(VirtualFrame virtualFrame, MutableLoopData mutableData, PException pe) {
2568-
if (pe != null) {
2569-
if (mutableData.localException != null) {
2570-
ExceptionHandlingStatementNode.chainExceptions(pe.getUnreifiedException(), mutableData.localException, exceptionChainProfile1, exceptionChainProfile2);
2571-
} else {
2572-
if (getCaughtExceptionNode == null) {
2573-
CompilerDirectives.transferToInterpreterAndInvalidate();
2574-
getCaughtExceptionNode = insert(ExceptionStateNodes.GetCaughtExceptionNode.create());
2575-
}
2576-
PException exceptionState = getCaughtExceptionNode.execute(virtualFrame);
2577-
if (exceptionState != null) {
2578-
ExceptionHandlingStatementNode.chainExceptions(pe.getUnreifiedException(), exceptionState, exceptionChainProfile1, exceptionChainProfile2);
2579-
}
2580-
}
2581-
}
2582-
}
2583-
25842564
private PException raiseUnkownBytecodeError(final byte bc) {
25852565
CompilerDirectives.transferToInterpreterAndInvalidate();
25862566
throw PRaiseNode.raiseUncached(this, SystemError, toTruffleStringUncached("not implemented bytecode %s"), OpCodes.fromOpCode(bc));
@@ -4885,13 +4865,12 @@ private int findHandler(int bci) {
48854865

48864866
@InliningCutoff
48874867
@ExplodeLoop
4888-
private static int unwindBlock(VirtualFrame virtualFrame, int stackTop, int stackTopBeforeBlock) {
4868+
private static void unwindBlock(VirtualFrame virtualFrame, int stackTop, int stackTopBeforeBlock) {
48894869
CompilerAsserts.partialEvaluationConstant(stackTop);
48904870
CompilerAsserts.partialEvaluationConstant(stackTopBeforeBlock);
48914871
for (int i = stackTop; i > stackTopBeforeBlock; i--) {
48924872
virtualFrame.setObject(i, null);
48934873
}
4894-
return stackTopBeforeBlock;
48954874
}
48964875

48974876
public PCell readClassCell(VirtualFrame virtualFrame) {

0 commit comments

Comments
 (0)