8000 py/emitnative: Add support for using setjmp with native emitter. · jimmo/micropython@3504edc · GitHub
[go: up one dir, main page]

Skip to content

Commit 3504edc

Browse files
committed
py/emitnative: Add support for using setjmp with native emitter.
To enable this feature the N_NLR_SETJMP macro should be set to 1 before including py/emitnative.c.
1 parent 4107597 commit 3504edc

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

py/emitnative.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,10 @@ STATIC void emit_native_global_exc_entry(emit_t *emit) {
11541154
// Wrap everything in an nlr context
11551155
ASM_MOV_REG_LOCAL_ADDR(emit->as, REG_ARG_1, 0);
11561156
emit_call(emit, MP_F_NLR_PUSH);
1157+
#if N_NLR_SETJMP
1158+
ASM_MOV_REG_LOCAL_ADDR(emit->as, REG_ARG_1, 2);
1159+
emit_call(emit, MP_F_SETJMP);
1160+
#endif
11571161
ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, start_label, true);
11581162
} else {
11591163
// Clear the unwind state
@@ -1168,6 +1172,10 @@ STATIC void emit_native_global_exc_entry(emit_t *emit) {
11681172
ASM_MOV_REG_LOCAL(emit->as, REG_LOCAL_2, LOCAL_IDX_EXC_HANDLER_UNWIND(emit));
11691173
ASM_MOV_REG_LOCAL_ADDR(emit->as, REG_ARG_1, 0);
11701174
emit_call(emit, MP_F_NLR_PUSH);
1175+
#if N_NLR_SETJMP
1176+
ASM_MOV_REG_LOCAL_ADDR(emit->as, REG_ARG_1, 2);
1177+
emit_call(emit, MP_F_SETJMP);
1178+
#endif
11711179
ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_EXC_HANDLER_UNWIND(emit), REG_LOCAL_2);
11721180
ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, global_except_label, true);
11731181

@@ -1178,6 +1186,12 @@ STATIC void emit_native_global_exc_entry(emit_t *emit) {
11781186

11791187
// Global exception handler: check for valid exception handler
11801188
emit_native_label_assign(emit, global_except_label);
1189+
#if N_NLR_SETJMP
1190+
// Reload REG_FUN_TABLE, since it may be clobbered by longjmp
1191+
emit_native_mov_reg_state(emit, REG_LOCAL_1, LOCAL_IDX_FUN_OBJ(emit));
1192+
ASM_LOAD_REG_REG_OFFSET(emit->as, REG_LOCAL_1, REG_LOCAL_1, offsetof(mp_obj_fun_bc_t, const_table) / sizeof(uintptr_t));
1193+
ASM_LOAD_REG_REG_OFFSET(emit->as, REG_FUN_TABLE, REG_LOCAL_1, emit->scope->num_pos_args + emit->scope->num_kwonly_args);
1194+
#endif
11811195
ASM_MOV_REG_LOCAL(emit->as, REG_LOCAL_1, LOCAL_IDX_EXC_HANDLER_PC(emit));
11821196
ASM_JUMP_IF_REG_NONZERO(emit->as, REG_LOCAL_1, nlr_label, false);
11831197
}

py/nativeglue.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,11 @@ const void *const mp_fun_table[MP_F_NUMBER_OF] = {
244244
mp_call_method_n_kw_var,
245245
mp_native_getiter,
246246
mp_native_iternext,
247+
#if MICROPY_NLR_SETJMP
248+
nlr_push_tail,
249+
#else
247250
nlr_push,
251+
#endif
248252
nlr_pop,
249253
mp_native_raise,
250254
mp_import_name,
@@ -262,6 +266,11 @@ const void *const mp_fun_table[MP_F_NUMBER_OF] = {
262266
mp_small_int_floor_divide,
263267
mp_small_int_modulo,
264268
mp_native_yield_from,
269+
#if MICROPY_NLR_SETJMP
270+
setjmp,
271+
#else
272+
NULL,
273+
#endif
265274
};
266275

267276
#endif // MICROPY_EMIT_NATIVE

py/runtime0.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ typedef enum {
201201
MP_F_SMALL_INT_FLOOR_DIVIDE,
202202
MP_F_SMALL_INT_MODULO,
203203
MP_F_NATIVE_YIELD_FROM,
204+
MP_F_SETJMP,
204205
MP_F_NUMBER_OF,
205206
} mp_fun_kind_t;
206207

0 commit comments

Comments
 (0)
0