Skip to content

Commit dfef424

Browse files
committed
py: Fix viper store on x86; add tests for viper ptr16.
1 parent e9dac3b commit dfef424

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

py/emitnative.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1391,7 +1391,10 @@ STATIC void emit_native_store_subscr(emit_t *emit) {
13911391
assert(vtype_value == VTYPE_PYOBJ);
13921392
emit_call(emit, MP_F_OBJ_SUBSCR);
13931393
} else {
1394-
// viper call
1394+
// viper store
1395+
// TODO The different machine architectures have very different
1396+
// capabilities and requirements for stores, so probably best to
1397+
// write a completely separate store-optimiser for each one.
13951398
stack_info_t *top = peek_stack(emit, 0);
13961399
if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
13971400
// index is an immediate
@@ -1402,7 +1405,12 @@ STATIC void emit_native_store_subscr(emit_t *emit) {
14021405
int reg_index = REG_ARG_2;
14031406
int reg_value = REG_ARG_3;
14041407
emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_value);
1408+
#if N_X86
1409+
// special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
1410+
emit_pre_pop_reg(emit, &vtype_value, reg_value);
1411+
#else
14051412
emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, reg_base, reg_index);
1413+
#endif
14061414
switch (vtype_base) {
14071415
case VTYPE_PTR8: {
14081416
// pointer to 8-bit memory
@@ -1449,7 +1457,12 @@ STATIC void emit_native_store_subscr(emit_t *emit) {
14491457
int reg_value = REG_ARG_3;
14501458
emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, reg_value);
14511459
emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
1460+
#if N_X86
1461+
// special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
1462+
emit_pre_pop_reg(emit, &vtype_value, reg_value);
1463+
#else
14521464
emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, REG_ARG_1, reg_index);
1465+
#endif
14531466
switch (vtype_base) {
14541467
case VTYPE_PTR8: {
14551468
// pointer to 8-bit memory
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# test ptr16 type
2+
3+
@micropython.viper
4+
def set(dest:ptr16, val:int):
5+
dest[0] = val
6+
7+
@micropython.viper
8+
def memset(dest:ptr16, val:int, n:int):
9+
for i in range(n):
10+
dest[i] = val
11+
12+
b = bytearray(4)
13+
print(b)
14+
15+
set(b, 0x4242)
16+
print(b)
17+
18+
memset(b, 0x4343, len(b) // 2)
19+
print(b)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
bytearray(b'\x00\x00\x00\x00')
2+
bytearray(b'BB\x00\x00')
3+
bytearray(b'CCCC')

0 commit comments

Comments
 (0)