@@ -2227,7 +2227,7 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
2227
2227
bci = 0 ;
2228
2228
stackTop = getInitialStackTop ();
2229
2229
oparg = 0 ;
2230
- notifyStatementAfterException (virtualFrame , instrumentation , beginBci );
2230
+ notifyStatementAfterException (virtualFrame , instrumentation , bci );
2231
2231
continue ;
2232
2232
} else if (result != null ) {
2233
2233
return result ;
@@ -2236,94 +2236,51 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
2236
2236
if (e instanceof ThreadDeath ) {
2237
2237
throw e ;
2238
2238
}
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 );
2240
2261
if (targetIndex == -1 ) {
2262
+ reraiseUnhandledException (virtualFrame , localFrame , initialStackTop , isGeneratorOrCoroutine , mutableData , bciSlot , beginBci , e , pe , tracingEnabled , profilingEnabled );
2241
2263
throw e ;
2242
2264
}
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 );
2244
2276
bci = exceptionHandlerRanges [targetIndex ];
2245
2277
oparg = 0 ;
2246
- }
2247
- }
2248
- }
2249
2278
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 );
2296
2281
}
2297
2282
}
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 );
2325
2283
}
2326
- return targetIndex ;
2327
2284
}
2328
2285
2329
2286
@ InliningCutoff
@@ -2639,6 +2596,46 @@ private void unboxVariables(Frame localFrame) {
2639
2596
}
2640
2597
}
2641
2598
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
+
2642
2639
private PException raiseUnkownBytecodeError (final byte bc ) {
2643
2640
CompilerDirectives .transferToInterpreterAndInvalidate ();
2644
2641
throw PRaiseNode .raiseUncached (this , SystemError , toTruffleStringUncached ("not implemented bytecode %s" ), OpCodes .fromOpCode (bc ));
@@ -4943,12 +4940,13 @@ private int findHandler(int bci) {
4943
4940
4944
4941
@ InliningCutoff
4945
4942
@ ExplodeLoop
4946
- private static void unwindBlock (VirtualFrame virtualFrame , int stackTop , int stackTopBeforeBlock ) {
4943
+ private static int unwindBlock (VirtualFrame virtualFrame , int stackTop , int stackTopBeforeBlock ) {
4947
4944
CompilerAsserts .partialEvaluationConstant (stackTop );
4948
4945
CompilerAsserts .partialEvaluationConstant (stackTopBeforeBlock );
4949
4946
for (int i = stackTop ; i > stackTopBeforeBlock ; i --) {
4950
4947
virtualFrame .setObject (i , null );
4951
4948
}
4949
+ return stackTopBeforeBlock ;
4952
4950
}
4953
4951
4954
4952
public PCell readClassCell (VirtualFrame virtualFrame ) {
0 commit comments