Skip to content

Commit 1673420

Browse files
committed
vm: Abstract working with tagged pointers in VM using macro accessors.
Based on issues raised during recent review and inconsistency of different implementations.
1 parent f357a19 commit 1673420

2 files changed

Lines changed: 14 additions & 9 deletions

File tree

py/bc.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ typedef enum {
88
typedef struct _mp_exc_stack {
99
const byte *handler;
1010
// bit 0 is saved currently_in_except_block value
11-
machine_uint_t val_sp;
11+
mp_obj_t *val_sp;
1212
// We might only have 2 interesting cases here: SETUP_EXCEPT & SETUP_FINALLY,
1313
// consider storing it in bit 1 of val_sp. TODO: SETUP_WITH?
1414
byte opcode;
@@ -17,3 +17,8 @@ typedef struct _mp_exc_stack {
1717
mp_vm_return_kind_t mp_execute_byte_code(const byte *code, const mp_obj_t *args, uint n_args, const mp_obj_t *args2, uint n_args2, uint n_state, mp_obj_t *ret);
1818
mp_vm_return_kind_t mp_execute_byte_code_2(const byte *code_info, const byte **ip_in_out, mp_obj_t *fastn, mp_obj_t **sp_in_out, mp_exc_stack *exc_stack, mp_exc_stack **exc_sp_in_out, volatile mp_obj_t inject_exc);
1919
void mp_byte_code_print(const byte *code, int len);
20+
21+
// Helper macros to access pointer with least significant bit holding a flag
22+
#define MP_TAGPTR_PTR(x) ((void*)((machine_uint_t)(x) & ~((machine_uint_t)1)))
23+
#define MP_TAGPTR_TAG(x) ((machine_uint_t)(x) & 1)
24+
#define MP_TAGPTR_MAKE(ptr, tag) ((void*)((machine_uint_t)(ptr) | tag))

py/vm.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ mp_vm_return_kind_t mp_execute_byte_code_2(const byte *code_info, const byte **i
118118
mp_obj_t obj1, obj2;
119119
nlr_buf_t nlr;
120120

121-
volatile machine_uint_t currently_in_except_block = (machine_uint_t)*exc_sp_in_out & 1; // 0 or 1, to detect nested exceptions
122-
mp_exc_stack *volatile exc_sp = (void*)((machine_uint_t)*exc_sp_in_out & ~1); // stack grows up, exc_sp points to top of stack
121+
volatile bool currently_in_except_block = MP_TAGPTR_TAG(*exc_sp_in_out); // 0 or 1, to detect nested exceptions
122+
mp_exc_stack *volatile exc_sp = MP_TAGPTR_PTR(*exc_sp_in_out); // stack grows up, exc_sp points to top of stack
123123
const byte *volatile save_ip = ip; // this is so we can access ip in the exception handler without making ip volatile (which means the compiler can't keep it in a register in the main loop)
124124

125125
// outer exception handling loop
@@ -387,7 +387,7 @@ mp_vm_return_kind_t mp_execute_byte_code_2(const byte *code_info, const byte **i
387387
++exc_sp;
388388
exc_sp->opcode = op;
389389
exc_sp->handler = ip + unum;
390-
exc_sp->val_sp = (((machine_uint_t)sp) | currently_in_except_block);
390+
exc_sp->val_sp = MP_TAGPTR_MAKE(sp, currently_in_except_block);
391391
currently_in_except_block = 0; // in a try block now
392392
break;
393393

@@ -437,7 +437,7 @@ mp_vm_return_kind_t mp_execute_byte_code_2(const byte *code_info, const byte **i
437437
case MP_BC_POP_BLOCK:
438438
// we are exiting an exception handler, so pop the last one of the exception-stack
439439
assert(exc_sp >= exc_stack);
440-
currently_in_except_block = (exc_sp->val_sp & 1); // restore previous state
440+
currently_in_except_block = MP_TAGPTR_TAG(exc_sp->val_sp); // restore previous state
441441
exc_sp--; // pop back to previous exception handler
442442
break;
443443

@@ -449,7 +449,7 @@ mp_vm_return_kind_t mp_execute_byte_code_2(const byte *code_info, const byte **i
449449
assert(currently_in_except_block);
450450
//sp = (mp_obj_t*)(*exc_sp--);
451451
//exc_sp--; // discard ip
452-
currently_in_except_block = (exc_sp->val_sp & 1); // restore previous state
452+
currently_in_except_block = MP_TAGPTR_TAG(exc_sp->val_sp); // restore previous state
453453
exc_sp--; // pop back to previous exception handler
454454
//sp -= 3; // pop 3 exception values
455455
break;
@@ -607,7 +607,7 @@ mp_vm_return_kind_t mp_execute_byte_code_2(const byte *code_info, const byte **i
607607
nlr_pop();
608608
*ip_in_out = ip;
609609
*sp_in_out = sp;
610-
*exc_sp_in_out = (void*)((machine_uint_t)exc_sp | currently_in_except_block);
610+
*exc_sp_in_out = MP_TAGPTR_MAKE(exc_sp, currently_in_except_block);
611611
return MP_VM_RETURN_YIELD;
612612

613613
case MP_BC_IMPORT_NAME:
@@ -663,7 +663,7 @@ mp_vm_return_kind_t mp_execute_byte_code_2(const byte *code_info, const byte **i
663663
// at the moment we are just raising the very last exception (the one that caused the nested exception)
664664

665665
// move up to previous exception handler
666-
currently_in_except_block = (exc_sp->val_sp & 1); // restore previous state
666+
currently_in_except_block = MP_TAGPTR_TAG(exc_sp->val_sp); // restore previous state
667667
exc_sp--; // pop back to previous exception handler
668668
}
669669

@@ -672,7 +672,7 @@ mp_vm_return_kind_t mp_execute_byte_code_2(const byte *code_info, const byte **i
672672
currently_in_except_block = 1;
673673

674674
// catch exception and pass to byte code
675-
sp = (mp_obj_t*)(exc_sp->val_sp & (~((machine_uint_t)1)));
675+
sp = MP_TAGPTR_PTR(exc_sp->val_sp);
676676
ip = exc_sp->handler;
677677
// push(traceback, exc-val, exc-type)
678678
PUSH(mp_const_none);

0 commit comments

Comments
 (0)