Skip to content

Commit b596638

Browse files
committed
mpy-cross: Set number of registers in nlr_buf_t based on native arch.
Fixes micropython#5059. Done in collaboration with Jim Mussared.
1 parent 7450310 commit b596638

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

mpy-cross/main.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,16 @@ MP_NOINLINE int main_(int argc, char **argv) {
209209
mp_dynamic_compiler.py_builtins_str_unicode = 1;
210210
#if defined(__i386__)
211211
mp_dynamic_compiler.native_arch = MP_NATIVE_ARCH_X86;
212+
mp_dynamic_compiler.nlr_buf_num_regs = MICROPY_NLR_NUM_REGS_X86;
212213
#elif defined(__x86_64__)
213214
mp_dynamic_compiler.native_arch = MP_NATIVE_ARCH_X64;
215+
mp_dynamic_compiler.nlr_buf_num_regs = MAX(MICROPY_NLR_NUM_REGS_X64, MICROPY_NLR_NUM_REGS_X64_WIN);
214216
#elif defined(__arm__) && !defined(__thumb2__)
215217
mp_dynamic_compiler.native_arch = MP_NATIVE_ARCH_ARMV6;
218+
mp_dynamic_compiler.nlr_buf_num_regs = MICROPY_NLR_NUM_REGS_ARM_THUMB_FP;
216219
#else
217220
mp_dynamic_compiler.native_arch = MP_NATIVE_ARCH_NONE;
221+
mp_dynamic_compiler.nlr_buf_num_regs = 0;
218222
#endif
219223

220224
const char *input_file = NULL;
@@ -271,14 +275,19 @@ MP_NOINLINE int main_(int argc, char **argv) {
271275
const char *arch = argv[a] + sizeof("-march=") - 1;
272276
if (strcmp(arch, "x86") == 0) {
273277
mp_dynamic_compiler.native_arch = MP_NATIVE_ARCH_X86;
278+
mp_dynamic_compiler.nlr_buf_num_regs = MICROPY_NLR_NUM_REGS_X86;
274279
} else if (strcmp(arch, "x64") == 0) {
275280
mp_dynamic_compiler.native_arch = MP_NATIVE_ARCH_X64;
281+
mp_dynamic_compiler.nlr_buf_num_regs = MAX(MICROPY_NLR_NUM_REGS_X64, MICROPY_NLR_NUM_REGS_X64_WIN);
276282
} else if (strcmp(arch, "armv6") == 0) {
277283
mp_dynamic_compiler.native_arch = MP_NATIVE_ARCH_ARMV6;
284+
mp_dynamic_compiler.nlr_buf_num_regs = MICROPY_NLR_NUM_REGS_ARM_THUMB_FP;
278285
} else if (strcmp(arch, "armv7m") == 0) {
279286
mp_dynamic_compiler.native_arch = MP_NATIVE_ARCH_ARMV7M;
287+
mp_dynamic_compiler.nlr_buf_num_regs = MICROPY_NLR_NUM_REGS_ARM_THUMB_FP;
280288
} else if (strcmp(arch, "xtensa") == 0) {
281289
mp_dynamic_compiler.native_arch = MP_NATIVE_ARCH_XTENSA;
290+
mp_dynamic_compiler.nlr_buf_num_regs = MICROPY_NLR_NUM_REGS_XTENSA;
282291
} else {
283292
return usage(argv);
284293
}

py/emitnative.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@
8181
// (L0-L2 may be in regs instead)
8282

8383
// Native emitter needs to know the following sizes and offsets of C structs (on the target):
84+
#if MICROPY_DYNAMIC_COMPILER
85+
#define SIZEOF_NLR_BUF (2 + mp_dynamic_compiler.nlr_buf_num_regs + 1) // the +1 is conservative in case MICROPY_ENABLE_PYSTACK enabled
86+
#else
8487
#define SIZEOF_NLR_BUF (sizeof(nlr_buf_t) / sizeof(uintptr_t))
88+
#endif
8589
#define SIZEOF_CODE_STATE (sizeof(mp_code_state_t) / sizeof(uintptr_t))
8690
#define OFFSETOF_CODE_STATE_STATE (offsetof(mp_code_state_t, state) / sizeof(uintptr_t))
8791
#define OFFSETOF_CODE_STATE_FUN_BC (offsetof(mp_code_state_t, fun_bc) / sizeof(uintptr_t))

py/mpstate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ typedef struct mp_dynamic_compiler_t {
4747
bool opt_cache_map_lookup_in_bytecode;
4848
bool py_builtins_str_unicode;
4949
uint8_t native_arch;
50+
uint8_t nlr_buf_num_regs;
5051
} mp_dynamic_compiler_t;
5152
extern mp_dynamic_compiler_t mp_dynamic_compiler;
5253
#endif

0 commit comments

Comments
 (0)