Skip to content

Commit 0e3752e

Browse files
jimmodpgeorge
authored andcommitted
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 <[email protected]>
1 parent d0227d5 commit 0e3752e

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

py/emitnative.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@ STATIC vtype_kind_t load_reg_stack_imm(emit_t *emit, int reg_dest, const stack_i
877877
// Copies all unsettled registers and immediates that are Python values into the
878878
// concrete Python stack. This ensures the concrete Python stack holds valid
879879
// values for the current stack_size.
880-
// This function may clobber REG_TEMP0.
880+
// This function may clobber REG_TEMP1.
881881
STATIC void need_stack_settled(emit_t *emit) {
882882
DEBUG_printf(" need_stack_settled; stack_size=%d\n", emit->stack_size);
883883
need_reg_all(emit);
@@ -886,8 +886,9 @@ STATIC void need_stack_settled(emit_t *emit) {
886886
if (si->kind == STACK_IMM) {
887887
DEBUG_printf(" imm(" INT_FMT ") to local(%u)\n", si->data.u_imm, emit->stack_start + i);
888888
si->kind = STACK_VALUE;
889-
si->vtype = load_reg_stack_imm(emit, REG_TEMP0, si, false);
890-
emit_native_mov_state_reg(emit, emit->stack_start + i, REG_TEMP0);
889+
// using REG_TEMP1 to avoid clobbering REG_TEMP0 (aka REG_RET)
890+
si->vtype = load_reg_stack_imm(emit, REG_TEMP1, si, false);
891+
emit_native_mov_state_reg(emit, emit->stack_start + i, REG_TEMP1);
891892
}
892893
}
893894
}

tests/micropython/native_misc.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,13 @@ def f(a):
3838

3939
f(False)
4040
f(True)
41+
42+
43+
# stack settling in branch
44+
@micropython.native
45+
def f(a):
46+
print(1, 2, 3, 4 if a else 5)
47+
48+
49+
f(False)
50+
f(True)

tests/micropython/native_misc.py.exp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44
6
55
True
66
False
7+
1 2 3 5
8+
1 2 3 4

0 commit comments

Comments
 (0)