@@ -81,18 +81,17 @@ typedef enum {
8181#define TOP () (*sp)
8282#define SET_TOP (val ) *sp = (val)
8383
84- #define PUSH_EXC_BLOCK () do { \
84+ #define PUSH_EXC_BLOCK (with_or_finally ) do { \
8585 DECODE_ULABEL; /* except labels are always forward */ \
8686 ++ exc_sp ; \
87- exc_sp -> opcode = * code_state -> ip ; \
8887 exc_sp -> handler = ip + ulab ; \
89- exc_sp -> val_sp = MP_TAGPTR_MAKE (sp , currently_in_except_block ); \
88+ exc_sp -> val_sp = MP_TAGPTR_MAKE (sp , (( with_or_finally ) << 1 ) | currently_in_except_block ); \
9089 exc_sp -> prev_exc = MP_OBJ_NULL ; \
9190 currently_in_except_block = 0 ; /* in a try block now */ \
9291} while (0 )
9392
9493#define POP_EXC_BLOCK () \
95- currently_in_except_block = MP_TAGPTR_TAG (exc_sp->val_sp); /* restore previous state */ \
94+ currently_in_except_block = MP_TAGPTR_TAG0 (exc_sp->val_sp); /* restore previous state */ \
9695 exc_sp -- ; /* pop back to previous exception handler */
9796
9897// fastn has items in reverse order (fastn[0] is local[0], fastn[-1] is local[1], etc)
@@ -130,7 +129,7 @@ mp_vm_return_kind_t mp_execute_bytecode(mp_code_state *code_state, volatile mp_o
130129 mp_exc_stack_t * const exc_stack = (mp_exc_stack_t * )(code_state -> state + code_state -> n_state );
131130
132131 // variables that are visible to the exception handler (declared volatile)
133- volatile bool currently_in_except_block = MP_TAGPTR_TAG (code_state -> exc_sp ); // 0 or 1, to detect nested exceptions
132+ volatile bool currently_in_except_block = MP_TAGPTR_TAG0 (code_state -> exc_sp ); // 0 or 1, to detect nested exceptions
134133 mp_exc_stack_t * volatile exc_sp = MP_TAGPTR_PTR (code_state -> exc_sp ); // stack grows up, exc_sp points to top of stack
135134
136135 // outer exception handling loop
@@ -422,7 +421,7 @@ mp_vm_return_kind_t mp_execute_bytecode(mp_code_state *code_state, volatile mp_o
422421 SET_TOP (mp_load_attr (obj , MP_QSTR___exit__ ));
423422 mp_load_method (obj , MP_QSTR___enter__ , sp + 1 );
424423 mp_obj_t ret = mp_call_method_n_kw (0 , 0 , sp + 1 );
425- PUSH_EXC_BLOCK ();
424+ PUSH_EXC_BLOCK (1 );
426425 PUSH (ret );
427426 DISPATCH ();
428427 }
@@ -477,7 +476,6 @@ mp_vm_return_kind_t mp_execute_bytecode(mp_code_state *code_state, volatile mp_o
477476 // to just execute finally handler normally (by pushing None
478477 // on value stack)
479478 assert (exc_sp >= exc_stack );
480- assert (exc_sp -> opcode == MP_BC_SETUP_WITH );
481479 POP_EXC_BLOCK ();
482480 PUSH (mp_const_none );
483481 }
@@ -496,7 +494,7 @@ unwind_jump:;
496494 while ((unum & 0x7f ) > 0 ) {
497495 unum -= 1 ;
498496 assert (exc_sp >= exc_stack );
499- if (exc_sp -> opcode == MP_BC_SETUP_FINALLY || exc_sp -> opcode == MP_BC_SETUP_WITH ) {
497+ if (MP_TAGPTR_TAG1 ( exc_sp -> val_sp ) ) {
500498 // We're going to run "finally" code as a coroutine
501499 // (not calling it recursively). Set up a sentinel
502500 // on a stack so it can return back to us when it is
@@ -519,7 +517,7 @@ unwind_jump:;
519517 // matched against: POP_BLOCK or POP_EXCEPT (anything else?)
520518 ENTRY (MP_BC_SETUP_EXCEPT ):
521519 ENTRY (MP_BC_SETUP_FINALLY ): {
522- PUSH_EXC_BLOCK ();
520+ PUSH_EXC_BLOCK (( * code_state -> ip == MP_BC_SETUP_FINALLY ) ? 1 : 0 );
523521 DISPATCH ();
524522 }
525523
@@ -759,7 +757,7 @@ unwind_jump:;
759757 ENTRY (MP_BC_RETURN_VALUE ):
760758unwind_return :
761759 while (exc_sp >= exc_stack ) {
762- if (exc_sp -> opcode == MP_BC_SETUP_FINALLY || exc_sp -> opcode == MP_BC_SETUP_WITH ) {
760+ if (MP_TAGPTR_TAG1 ( exc_sp -> val_sp ) ) {
763761 // We're going to run "finally" code as a coroutine
764762 // (not calling it recursively). Set up a sentinel
765763 // on a stack so it can return back to us when it is
0 commit comments