@@ -178,6 +178,12 @@ static const uint8_t reg_local_table[MAX_REGS_FOR_LOCAL_VARS] = {REG_LOCAL_1, RE
178
178
*emit->error_slot = mp_obj_new_exception_msg_varg(&mp_type_ViperTypeError, __VA_ARGS__); \
179
179
} while (0)
180
180
181
+ #if N_RV32
182
+ #define FIT_SIGNED (value , bits ) \
183
+ ((((value) & ~((1U << ((bits) - 1)) - 1)) == 0) || \
184
+ (((value) & ~((1U << ((bits) - 1)) - 1)) == ~((1U << ((bits) - 1)) - 1)))
185
+ #endif
186
+
181
187
typedef enum {
182
188
STACK_VALUE ,
183
189
STACK_REG ,
@@ -1519,6 +1525,11 @@ static void emit_native_load_subscr(emit_t *emit) {
1519
1525
asm_thumb_ldrb_rlo_rlo_i5 (emit -> as , REG_RET , reg_base , index_value );
1520
1526
break ;
1521
1527
}
1528
+ #elif N_RV32
1529
+ if (FIT_SIGNED (index_value , 12 )) {
1530
+ asm_rv32_opcode_lbu (emit -> as , REG_RET , reg_base , index_value );
1531
+ break ;
1532
+ }
1522
1533
#endif
1523
1534
need_reg_single (emit , reg_index , 0 );
1524
1535
ASM_MOV_REG_IMM (emit -> as , reg_index , index_value );
@@ -1537,6 +1548,11 @@ static void emit_native_load_subscr(emit_t *emit) {
1537
1548
asm_thumb_ldrh_rlo_rlo_i5 (emit -> as , REG_RET , reg_base , index_value );
1538
1549
break ;
1539
1550
}
1551
+ #elif N_RV32
1552
+ if (FIT_SIGNED (index_value , 11 )) {
1553
+ asm_rv32_opcode_lhu (emit -> as , REG_RET , reg_base , index_value << 1 );
1554
+ break ;
1555
+ }
1540
1556
#endif
1541
1557
need_reg_single (emit , reg_index , 0 );
1542
1558
ASM_MOV_REG_IMM (emit -> as , reg_index , index_value << 1 );
@@ -1555,6 +1571,11 @@ static void emit_native_load_subscr(emit_t *emit) {
1555
1571
asm_thumb_ldr_rlo_rlo_i5 (emit -> as , REG_RET , reg_base , index_value );
1556
1572
break ;
1557
1573
}
1574
+ #elif N_RV32
1575
+ if (FIT_SIGNED (index_value , 10 )) {
1576
+ asm_rv32_opcode_lw (emit -> as , REG_RET , reg_base , index_value << 2 );
1577
+ break ;
1578
+ }
1558
1579
#endif
1559
1580
need_reg_single (emit , reg_index , 0 );
1560
1581
ASM_MOV_REG_IMM (emit -> as , reg_index , index_value << 2 );
@@ -1596,6 +1617,12 @@ static void emit_native_load_subscr(emit_t *emit) {
1596
1617
}
1597
1618
case VTYPE_PTR32 : {
1598
1619
// pointer to word-size memory
1620
+ #if N_RV32
1621
+ asm_rv32_opcode_slli (emit -> as , REG_TEMP2 , reg_index , 2 );
1622
+ asm_rv32_opcode_cadd (emit -> as , REG_ARG_1 , REG_TEMP2 );
1623
+ asm_rv32_opcode_lw (emit -> as , REG_RET , REG_ARG_1 , 0 );
1624
+ break ;
1625
+ #endif
1599
1626
ASM_ADD_REG_REG (emit -> as , REG_ARG_1 , reg_index ); // add index to base
1600
1627
ASM_ADD_REG_REG (emit -> as , REG_ARG_1 , reg_index ); // add index to base
1601
1628
ASM_ADD_REG_REG (emit -> as , REG_ARG_1 , reg_index ); // add index to base
@@ -1751,6 +1778,11 @@ static void emit_native_store_subscr(emit_t *emit) {
1751
1778
asm_thumb_strb_rlo_rlo_i5 (emit -> as , reg_value , reg_base , index_value );
1752
1779
break ;
1753
1780
}
1781
+ #elif N_RV32
1782
+ if (FIT_SIGNED (index_value , 12 )) {
1783
+ asm_rv32_opcode_sb (emit -> as , reg_value , reg_base , index_value );
1784
+ break ;
1785
+ }
1754
1786
#endif
1755
1787
ASM_MOV_REG_IMM (emit -> as , reg_index , index_value );
1756
1788
#if N_ARM
@@ -1772,6 +1804,11 @@ static void emit_native_store_subscr(emit_t *emit) {
1772
1804
asm_thumb_strh_rlo_rlo_i5 (emit -> as , reg_value , reg_base , index_value );
1773
1805
break ;
1774
1806
}
1807
+ #elif N_RV32
1808
+ if (FIT_SIGNED (index_value , 11 )) {
1809
+ asm_rv32_opcode_sh (emit -> as , reg_value , reg_base , index_value << 1 );
1810
+ break ;
1811
+ }
1775
1812
#endif
1776
1813
ASM_MOV_REG_IMM (emit -> as , reg_index , index_value << 1 );
1777
1814
ASM_ADD_REG_REG (emit -> as , reg_index , reg_base ); // add 2*index to base
@@ -1789,8 +1826,12 @@ static void emit_native_store_subscr(emit_t *emit) {
1789
1826
asm_thumb_str_rlo_rlo_i5 (emit -> as , reg_value , reg_base , index_value );
1790
1827
break ;
1791
1828
}
1792
- #endif
1793
- #if N_ARM
1829
+ #elif N_RV32
1830
+ if (FIT_SIGNED (index_value , 10 )) {
1831
+ asm_rv32_opcode_sw (emit -> as , reg_value , reg_base , index_value << 2 );
1832
+ break ;
1833
+ }
1834
+ #elif N_ARM
1794
1835
ASM_MOV_REG_IMM (emit -> as , reg_index , index_value );
1795
1836
asm_arm_str_reg_reg_reg (emit -> as , reg_value , reg_base , reg_index );
1796
1837
return ;
@@ -1855,6 +1896,11 @@ static void emit_native_store_subscr(emit_t *emit) {
1855
1896
#if N_ARM
1856
1897
asm_arm_str_reg_reg_reg (emit -> as , reg_value , REG_ARG_1 , reg_index );
1857
1898
break ;
1899
+ #elif N_RV32
1900
+ asm_rv32_opcode_slli (emit -> as , REG_TEMP2 , reg_index , 2 );
1901
+ asm_rv32_opcode_cadd (emit -> as , REG_ARG_1 , REG_TEMP2 );
1902
+ asm_rv32_opcode_sw (emit -> as , reg_value , REG_ARG_1 , 0 );
1903
+ break ;
1858
1904
#endif
1859
1905
ASM_ADD_REG_REG (emit -> as , REG_ARG_1 , reg_index ); // add index to base
1860
1906
ASM_ADD_REG_REG (emit -> as , REG_ARG_1 , reg_index ); // add index to base
0 commit comments