py/emitnative: Ensure load_subscr does not clobber existing REG_RET.

Fixes issue #7782, and part of issue #6314.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George 2021-09-13 22:30:24 +10:00
parent c0761d28fc
commit 426785a19e
3 changed files with 24 additions and 0 deletions

View File

@ -1560,6 +1560,7 @@ STATIC void emit_native_load_subscr(emit_t *emit) {
int reg_base = REG_ARG_1; int reg_base = REG_ARG_1;
int reg_index = REG_ARG_2; int reg_index = REG_ARG_2;
emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_index); emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_index);
need_reg_single(emit, REG_RET, 0);
switch (vtype_base) { switch (vtype_base) {
case VTYPE_PTR8: { case VTYPE_PTR8: {
// pointer to 8-bit memory // pointer to 8-bit memory
@ -1623,6 +1624,7 @@ STATIC void emit_native_load_subscr(emit_t *emit) {
int reg_index = REG_ARG_2; int reg_index = REG_ARG_2;
emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, REG_ARG_1); emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, REG_ARG_1);
emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
need_reg_single(emit, REG_RET, 0);
if (vtype_index != VTYPE_INT && vtype_index != VTYPE_UINT) { if (vtype_index != VTYPE_INT && vtype_index != VTYPE_UINT) {
EMIT_NATIVE_VIPER_TYPE_ERROR(emit, EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
MP_ERROR_TEXT("can't load with '%q' index"), vtype_to_qstr(vtype_index)); MP_ERROR_TEXT("can't load with '%q' index"), vtype_to_qstr(vtype_index));

View File

@ -0,0 +1,20 @@
# test viper with multiple subscripts in a single expression
@micropython.viper
def f1(b: ptr8):
b[0] += b[1]
@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)

View File

@ -0,0 +1,2 @@
bytearray(b'\x03\x02')
bytearray(b'\x03\x02')