From 0698dd72ea3b9d7897e63c6e219061bff8d162cf Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 11 Nov 2022 12:25:32 +1100 Subject: [PATCH] py/emitnative: Ensure load_subscr does not clobber existing REG_ARG_2. Follow up from a similar fix in 426785a19eeb12aef7383fbda4693575d8c4dddf Fixes issue #6314. Signed-off-by: Damien George --- py/emitnative.c | 3 +++ tests/micropython/viper_subscr_multi.py | 17 +++++++++++++---- tests/micropython/viper_subscr_multi.py.exp | 1 + 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/py/emitnative.c b/py/emitnative.c index 7e98ba1556..2d694983bd 100644 --- a/py/emitnative.c +++ b/py/emitnative.c @@ -1527,6 +1527,7 @@ STATIC void emit_native_load_subscr(emit_t *emit) { break; } #endif + need_reg_single(emit, reg_index, 0); ASM_MOV_REG_IMM(emit->as, reg_index, index_value); ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base reg_base = reg_index; @@ -1544,6 +1545,7 @@ STATIC void emit_native_load_subscr(emit_t *emit) { break; } #endif + need_reg_single(emit, reg_index, 0); ASM_MOV_REG_IMM(emit->as, reg_index, index_value << 1); ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base reg_base = reg_index; @@ -1561,6 +1563,7 @@ STATIC void emit_native_load_subscr(emit_t *emit) { break; } #endif + need_reg_single(emit, reg_index, 0); ASM_MOV_REG_IMM(emit->as, reg_index, index_value << 2); ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 4*index to base reg_base = reg_index; diff --git a/tests/micropython/viper_subscr_multi.py b/tests/micropython/viper_subscr_multi.py index 1561e5534d..a2baba2411 100644 --- a/tests/micropython/viper_subscr_multi.py +++ b/tests/micropython/viper_subscr_multi.py @@ -6,15 +6,24 @@ def f1(b: ptr8): b[0] += b[1] +b = bytearray(b"\x01\x02") +f1(b) +print(b) + + @micropython.viper def f2(b: ptr8, i: int): b[0] += b[i] -b = bytearray(b"\x01\x02") -f1(b) -print(b) - b = bytearray(b"\x01\x02") f2(b, 1) print(b) + + +@micropython.viper +def f3(b: ptr8) -> int: + return b[0] << 24 | b[1] << 16 | b[2] << 8 | b[3] + + +print(hex(f3(b"\x01\x02\x03\x04"))) diff --git a/tests/micropython/viper_subscr_multi.py.exp b/tests/micropython/viper_subscr_multi.py.exp index a2c298bb16..ea644c046d 100644 --- a/tests/micropython/viper_subscr_multi.py.exp +++ b/tests/micropython/viper_subscr_multi.py.exp @@ -1,2 +1,3 @@ bytearray(b'\x03\x02') bytearray(b'\x03\x02') +0x1020304