From 0e3752e82a61b2378547457c4041270db3909c39 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Mon, 19 Jul 2021 13:13:32 +1000 Subject: [PATCH] py/emitnative: Ensure stack settling is safe mid-branch. And add a test for the case where REG_RET could be in use. Fixes #7523. Signed-off-by: Jim Mussared --- py/emitnative.c | 7 ++++--- tests/micropython/native_misc.py | 10 ++++++++++ tests/micropython/native_misc.py.exp | 2 ++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/py/emitnative.c b/py/emitnative.c index a40d690f2d..7c7c342839 100644 --- a/py/emitnative.c +++ b/py/emitnative.c @@ -877,7 +877,7 @@ STATIC vtype_kind_t load_reg_stack_imm(emit_t *emit, int reg_dest, const stack_i // Copies all unsettled registers and immediates that are Python values into the // concrete Python stack. This ensures the concrete Python stack holds valid // values for the current stack_size. -// This function may clobber REG_TEMP0. +// This function may clobber REG_TEMP1. STATIC void need_stack_settled(emit_t *emit) { DEBUG_printf(" need_stack_settled; stack_size=%d\n", emit->stack_size); need_reg_all(emit); @@ -886,8 +886,9 @@ STATIC void need_stack_settled(emit_t *emit) { if (si->kind == STACK_IMM) { DEBUG_printf(" imm(" INT_FMT ") to local(%u)\n", si->data.u_imm, emit->stack_start + i); si->kind = STACK_VALUE; - si->vtype = load_reg_stack_imm(emit, REG_TEMP0, si, false); - emit_native_mov_state_reg(emit, emit->stack_start + i, REG_TEMP0); + // using REG_TEMP1 to avoid clobbering REG_TEMP0 (aka REG_RET) + si->vtype = load_reg_stack_imm(emit, REG_TEMP1, si, false); + emit_native_mov_state_reg(emit, emit->stack_start + i, REG_TEMP1); } } } diff --git a/tests/micropython/native_misc.py b/tests/micropython/native_misc.py index 7c5415375e..f5ef807fe1 100644 --- a/tests/micropython/native_misc.py +++ b/tests/micropython/native_misc.py @@ -38,3 +38,13 @@ def f(a): f(False) f(True) + + +# stack settling in branch +@micropython.native +def f(a): + print(1, 2, 3, 4 if a else 5) + + +f(False) +f(True) diff --git a/tests/micropython/native_misc.py.exp b/tests/micropython/native_misc.py.exp index b3e1389ef7..8eec04228f 100644 --- a/tests/micropython/native_misc.py.exp +++ b/tests/micropython/native_misc.py.exp @@ -4,3 +4,5 @@ 6 True False +1 2 3 5 +1 2 3 4