Skip to content

Commit a2e5e4c

Browse files
committed
py/viper: Allow uint as index to load/store, and give better error msg.
1 parent 3e02b1d commit a2e5e4c

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

py/emitnative.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,26 +1545,27 @@ STATIC void emit_native_load_subscr(emit_t *emit) {
15451545
int reg_index = REG_ARG_2;
15461546
emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, REG_ARG_1);
15471547
emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
1548+
if (vtype_index != VTYPE_INT && vtype_index != VTYPE_UINT) {
1549+
EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1550+
"can't load with '%q' index", vtype_to_qstr(vtype_index));
1551+
}
15481552
switch (vtype_base) {
15491553
case VTYPE_PTR8: {
15501554
// pointer to 8-bit memory
15511555
// TODO optimise to use thumb ldrb r1, [r2, r3]
1552-
assert(vtype_index == VTYPE_INT);
15531556
ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
15541557
ASM_LOAD8_REG_REG(emit->as, REG_RET, REG_ARG_1); // store value to (base+index)
15551558
break;
15561559
}
15571560
case VTYPE_PTR16: {
15581561
// pointer to 16-bit memory
1559-
assert(vtype_index == VTYPE_INT);
15601562
ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
15611563
ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
15621564
ASM_LOAD16_REG_REG(emit->as, REG_RET, REG_ARG_1); // load from (base+2*index)
15631565
break;
15641566
}
15651567
case VTYPE_PTR32: {
15661568
// pointer to word-size memory
1567-
assert(vtype_index == VTYPE_INT);
15681569
ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
15691570
ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
15701571
ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
@@ -1773,6 +1774,10 @@ STATIC void emit_native_store_subscr(emit_t *emit) {
17731774
int reg_value = REG_ARG_3;
17741775
emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, reg_value);
17751776
emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
1777+
if (vtype_index != VTYPE_INT && vtype_index != VTYPE_UINT) {
1778+
EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1779+
"can't store with '%q' index", vtype_to_qstr(vtype_index));
1780+
}
17761781
#if N_X86
17771782
// special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
17781783
emit_pre_pop_reg(emit, &vtype_value, reg_value);
@@ -1783,7 +1788,6 @@ STATIC void emit_native_store_subscr(emit_t *emit) {
17831788
case VTYPE_PTR8: {
17841789
// pointer to 8-bit memory
17851790
// TODO optimise to use thumb strb r1, [r2, r3]
1786-
assert(vtype_index == VTYPE_INT);
17871791
#if N_ARM
17881792
asm_arm_strb_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
17891793
break;
@@ -1794,7 +1798,6 @@ STATIC void emit_native_store_subscr(emit_t *emit) {
17941798
}
17951799
case VTYPE_PTR16: {
17961800
// pointer to 16-bit memory
1797-
assert(vtype_index == VTYPE_INT);
17981801
#if N_ARM
17991802
asm_arm_strh_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
18001803
break;
@@ -1806,7 +1809,6 @@ STATIC void emit_native_store_subscr(emit_t *emit) {
18061809
}
18071810
case VTYPE_PTR32: {
18081811
// pointer to 32-bit memory
1809-
assert(vtype_index == VTYPE_INT);
18101812
#if N_ARM
18111813
asm_arm_str_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
18121814
break;

0 commit comments

Comments
 (0)