@@ -2167,63 +2167,104 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
2167
2167
} catch (OSRException e ) {
2168
2168
// Exception from OSR was already handled in the OSR code
2169
2169
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 ) {
2188
2171
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 ) {
2191
2180
return result ;
2192
2181
}
2193
2182
}
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 );
2198
2187
if (targetIndex == -1 ) {
2199
- reraiseUnhandledException (virtualFrame , localFrame , initialStackTop , mutableData , isGeneratorOrCoroutine , bciSlot , beginBci , e , pe , tracingEnabled , profilingEnabled );
2200
2188
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 ;
2212
2189
}
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 );
2223
2232
}
2224
- throw t ;
2225
2233
}
2226
2234
}
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 ;
2227
2268
}
2228
2269
2229
2270
@ InliningCutoff
@@ -2232,11 +2273,10 @@ private Object notifyException(VirtualFrame virtualFrame, InstrumentationSupport
2232
2273
if (instrumentationRoot instanceof WrapperNode ) {
2233
2274
WrapperNode wrapper = (WrapperNode ) instrumentationRoot ;
2234
2275
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 ) {
2238
2277
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" );
2240
2280
}
2241
2281
}
2242
2282
return result ;
@@ -2259,23 +2299,6 @@ private void notifyStatement(VirtualFrame virtualFrame, InstrumentationSupport i
2259
2299
mutableData .setPastBci (bci );
2260
2300
}
2261
2301
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
-
2279
2302
@ InliningCutoff
2280
2303
private Object enterRoot (VirtualFrame virtualFrame ) {
2281
2304
if (instrumentationRoot instanceof WrapperNode ) {
@@ -2538,49 +2561,6 @@ private void unboxVariables(Frame localFrame) {
2538
2561
}
2539
2562
}
2540
2563
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
-
2584
2564
private PException raiseUnkownBytecodeError (final byte bc ) {
2585
2565
CompilerDirectives .transferToInterpreterAndInvalidate ();
2586
2566
throw PRaiseNode .raiseUncached (this , SystemError , toTruffleStringUncached ("not implemented bytecode %s" ), OpCodes .fromOpCode (bc ));
@@ -4885,13 +4865,12 @@ private int findHandler(int bci) {
4885
4865
4886
4866
@ InliningCutoff
4887
4867
@ ExplodeLoop
4888
- private static int unwindBlock (VirtualFrame virtualFrame , int stackTop , int stackTopBeforeBlock ) {
4868
+ private static void unwindBlock (VirtualFrame virtualFrame , int stackTop , int stackTopBeforeBlock ) {
4889
4869
CompilerAsserts .partialEvaluationConstant (stackTop );
4890
4870
CompilerAsserts .partialEvaluationConstant (stackTopBeforeBlock );
4891
4871
for (int i = stackTop ; i > stackTopBeforeBlock ; i --) {
4892
4872
virtualFrame .setObject (i , null );
4893
4873
}
4894
- return stackTopBeforeBlock ;
4895
4874
}
4896
4875
4897
4876
public PCell readClassCell (VirtualFrame virtualFrame ) {
0 commit comments