Skip to content

Commit 7883ae4

Browse files
committed
py/emitnative: Provide dedicated local for exception unwind handler ptr.
This eliminates the need to save and restore the exception unwind handler pointer when calling nlr_push. Signed-off-by: Damien George <[email protected]>
1 parent b608964 commit 7883ae4

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

py/emitnative.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,14 @@
6363

6464
// C stack layout for native functions:
6565
// 0: nlr_buf_t [optional]
66+
// exc_handler_unwind [optional word]
6667
// emit->code_state_start: mp_code_state_native_t
6768
// emit->stack_start: Python object stack | emit->n_state
6869
// locals (reversed, L0 at end) |
6970
//
7071
// C stack layout for native generator functions:
7172
// 0=emit->stack_start: nlr_buf_t
73+
// exc_handler_unwind [optional word]
7274
//
7375
// Then REG_GENERATOR_STATE points to:
7476
// 0=emit->code_state_start: mp_code_state_native_t
@@ -77,6 +79,7 @@
7779
//
7880
// C stack layout for viper functions:
7981
// 0: nlr_buf_t [optional]
82+
// exc_handler_unwind [optional word]
8083
// emit->code_state_start: fun_obj, old_globals [optional]
8184
// emit->stack_start: Python object stack | emit->n_state
8285
// locals (reversed, L0 at end) |
@@ -120,6 +123,9 @@
120123
#define NEED_GLOBAL_EXC_HANDLER(emit) ((emit)->scope->exc_stack_size > 0 \
121124
|| ((emit)->scope->scope_flags & (MP_SCOPE_FLAG_GENERATOR | MP_SCOPE_FLAG_REFGLOBALS)))
122125

126+
// Whether a slot is needed to store LOCAL_IDX_EXC_HANDLER_UNWIND
127+
#define NEED_EXC_HANDLER_UNWIND(emit) ((emit)->scope->exc_stack_size > 0)
128+
123129
// Whether registers can be used to store locals (only true if there are no
124130
// exception handlers, because otherwise an nlr_jump will restore registers to
125131
// their state at the start of the function and updates to locals will be lost)
@@ -128,7 +134,7 @@
128134
// Indices within the local C stack for various variables
129135
#define LOCAL_IDX_EXC_VAL(emit) (NLR_BUF_IDX_RET_VAL)
130136
#define LOCAL_IDX_EXC_HANDLER_PC(emit) (NLR_BUF_IDX_LOCAL_1)
131-
#define LOCAL_IDX_EXC_HANDLER_UNWIND(emit) (NLR_BUF_IDX_LOCAL_2)
137+
#define LOCAL_IDX_EXC_HANDLER_UNWIND(emit) (SIZEOF_NLR_BUF) // this needs a dedicated variable outside nlr_buf_t
132138
#define LOCAL_IDX_RET_VAL(emit) (NLR_BUF_IDX_LOCAL_3)
133139
#define LOCAL_IDX_FUN_OBJ(emit) ((emit)->code_state_start + OFFSETOF_CODE_STATE_FUN_BC)
134140
#define LOCAL_IDX_OLD_GLOBALS(emit) ((emit)->code_state_start + OFFSETOF_CODE_STATE_IP)
@@ -403,6 +409,9 @@ STATIC void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scop
403409
emit->code_state_start = 0;
404410
if (NEED_GLOBAL_EXC_HANDLER(emit)) {
405411
emit->code_state_start = SIZEOF_NLR_BUF;
412+
if (NEED_EXC_HANDLER_UNWIND(emit)) {
413+
emit->code_state_start += 1;
414+
}
406415
}
407416

408417
size_t fun_table_off = mp_emit_common_use_const_obj(emit->emit_common, MP_OBJ_FROM_PTR(&mp_fun_table));
@@ -498,11 +507,13 @@ STATIC void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scop
498507
emit->n_state = scope->num_locals + scope->stack_size;
499508

500509
if (emit->scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
501-
emit->code_state_start = 0;
502-
emit->stack_start = SIZEOF_CODE_STATE;
503510
mp_asm_base_data(&emit->as->base, ASM_WORD_SIZE, (uintptr_t)emit->prelude_ptr_index);
504511
mp_asm_base_data(&emit->as->base, ASM_WORD_SIZE, (uintptr_t)emit->start_offset);
505-
ASM_ENTRY(emit->as, SIZEOF_NLR_BUF);
512+
ASM_ENTRY(emit->as, emit->code_state_start);
513+
514+
// Reset the state size for the state pointed to by REG_GENERATOR_STATE
515+
emit->code_state_start = 0;
516+
emit->stack_start = SIZEOF_CODE_STATE;
506517

507518
// Put address of code_state into REG_GENERATOR_STATE
508519
#if N_X86
@@ -1188,14 +1199,12 @@ STATIC void emit_native_global_exc_entry(emit_t *emit) {
11881199

11891200
// Wrap everything in an nlr context
11901201
emit_native_label_assign(emit, nlr_label);
1191-
ASM_MOV_REG_LOCAL(emit->as, REG_LOCAL_2, LOCAL_IDX_EXC_HANDLER_UNWIND(emit));
11921202
ASM_MOV_REG_LOCAL_ADDR(emit->as, REG_ARG_1, 0);
11931203
emit_call(emit, MP_F_NLR_PUSH);
11941204
#if N_NLR_SETJMP
11951205
ASM_MOV_REG_LOCAL_ADDR(emit->as, REG_ARG_1, 2);
11961206
emit_call(emit, MP_F_SETJMP);
11971207
#endif
1198-
ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_EXC_HANDLER_UNWIND(emit), REG_LOCAL_2);
11991208
ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, global_except_label, true);
12001209

12011210
// Clear PC of current code block, and jump there to resume execution

0 commit comments

Comments
 (0)