Skip to content

Commit 426785a

Browse files
committed
py/emitnative: Ensure load_subscr does not clobber existing REG_RET.
Fixes issue adafruit#7782, and part of issue adafruit#6314. Signed-off-by: Damien George <[email protected]>
1 parent c0761d2 commit 426785a

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

py/emitnative.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,6 +1560,7 @@ STATIC void emit_native_load_subscr(emit_t *emit) {
15601560
int reg_base = REG_ARG_1;
15611561
int reg_index = REG_ARG_2;
15621562
emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_index);
1563+
need_reg_single(emit, REG_RET, 0);
15631564
switch (vtype_base) {
15641565
case VTYPE_PTR8: {
15651566
// pointer to 8-bit memory
@@ -1623,6 +1624,7 @@ STATIC void emit_native_load_subscr(emit_t *emit) {
16231624
int reg_index = REG_ARG_2;
16241625
emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, REG_ARG_1);
16251626
emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
1627+
need_reg_single(emit, REG_RET, 0);
16261628
if (vtype_index != VTYPE_INT && vtype_index != VTYPE_UINT) {
16271629
EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
16281630
MP_ERROR_TEXT("can't load with '%q' index"), vtype_to_qstr(vtype_index));
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# test viper with multiple subscripts in a single expression
2+
3+
4+
@micropython.viper
5+
def f1(b: ptr8):
6+
b[0] += b[1]
7+
8+
9+
@micropython.viper
10+
def f2(b: ptr8, i: int):
11+
b[0] += b[i]
12+
13+
14+
b = bytearray(b"\x01\x02")
15+
f1(b)
16+
print(b)
17+
18+
b = bytearray(b"\x01\x02")
19+
f2(b, 1)
20+
print(b)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bytearray(b'\x03\x02')
2+
bytearray(b'\x03\x02')

0 commit comments

Comments
 (0)