Skip to content

Commit 567b349

Browse files
committed
py: Implement native multiply operation in viper emitter.
1 parent 4d9cad1 commit 567b349

9 files changed

Lines changed: 56 additions & 0 deletions

File tree

py/asmarm.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@ STATIC uint asm_arm_op_sub_reg(uint rd, uint rn, uint rm) {
179179
return 0x0400000 | (rn << 16) | (rd << 12) | rm;
180180
}
181181

182+
STATIC uint asm_arm_op_mul_reg(uint rd, uint rm, uint rs) {
183+
// mul rd, rm, rs
184+
assert(rd != rm);
185+
return 0x0000090 | (rd << 16) | (rs << 8) | rm;
186+
}
187+
182188
STATIC uint asm_arm_op_and_reg(uint rd, uint rn, uint rm) {
183189
// and rd, rn, rm
184190
return 0x0000000 | (rn << 16) | (rd << 12) | rm;
@@ -340,6 +346,12 @@ void asm_arm_sub_reg_reg_reg(asm_arm_t *as, uint rd, uint rn, uint rm) {
340346
emit_al(as, asm_arm_op_sub_reg(rd, rn, rm));
341347
}
342348

349+
void asm_arm_mul_reg_reg_reg(asm_arm_t *as, uint rd, uint rs, uint rm) {
350+
// rs and rm are swapped because of restriction rd!=rm
351+
// mul rd, rm, rs
352+
emit_al(as, asm_arm_op_mul_reg(rd, rm, rs));
353+
}
354+
343355
void asm_arm_and_reg_reg_reg(asm_arm_t *as, uint rd, uint rn, uint rm) {
344356
// and rd, rn, rm
345357
emit_al(as, asm_arm_op_and_reg(rd, rn, rm));

py/asmarm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ void asm_arm_cmp_reg_reg(asm_arm_t *as, uint rd, uint rn);
101101
// arithmetic
102102
void asm_arm_add_reg_reg_reg(asm_arm_t *as, uint rd, uint rn, uint rm);
103103
void asm_arm_sub_reg_reg_reg(asm_arm_t *as, uint rd, uint rn, uint rm);
104+
void asm_arm_mul_reg_reg_reg(asm_arm_t *as, uint rd, uint rn, uint rm);
104105
void asm_arm_and_reg_reg_reg(asm_arm_t *as, uint rd, uint rn, uint rm);
105106
void asm_arm_eor_reg_reg_reg(asm_arm_t *as, uint rd, uint rn, uint rm);
106107
void asm_arm_orr_reg_reg_reg(asm_arm_t *as, uint rd, uint rn, uint rm);

py/asmx64.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,12 @@ void asm_x64_sub_r64_r64(asm_x64_t *as, int dest_r64, int src_r64) {
453453
asm_x64_generic_r64_r64(as, dest_r64, src_r64, OPCODE_SUB_R64_FROM_RM64);
454454
}
455455

456+
void asm_x64_mul_r64_r64(asm_x64_t *as, int dest_r64, int src_r64) {
457+
// imul reg64, reg/mem64 -- 0x0f 0xaf /r
458+
asm_x64_write_byte_1(as, REX_PREFIX | REX_W | (dest_r64 < 8 ? 0 : REX_R) | (src_r64 < 8 ? 0 : REX_B));
459+
asm_x64_write_byte_3(as, 0x0f, 0xaf, MODRM_R64(dest_r64) | MODRM_RM_REG | MODRM_RM_R64(src_r64));
460+
}
461+
456462
/*
457463
void asm_x64_sub_i32_from_r32(asm_x64_t *as, int src_i32, int dest_r32) {
458464
if (SIGNED_FIT8(src_i32)) {

py/asmx64.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ void asm_x64_shl_r64_cl(asm_x64_t* as, int dest_r64);
105105
void asm_x64_sar_r64_cl(asm_x64_t* as, int dest_r64);
106106
void asm_x64_add_r64_r64(asm_x64_t* as, int dest_r64, int src_r64);
107107
void asm_x64_sub_r64_r64(asm_x64_t* as, int dest_r64, int src_r64);
108+
void asm_x64_mul_r64_r64(asm_x64_t* as, int dest_r64, int src_r64);
108109
void asm_x64_cmp_r64_with_r64(asm_x64_t* as, int src_r64_a, int src_r64_b);
109110
void asm_x64_test_r8_with_r8(asm_x64_t* as, int src_r64_a, int src_r64_b);
110111
void asm_x64_setcc_r8(asm_x64_t* as, int jcc_type, int dest_r8);

py/asmx86.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,11 @@ STATIC void asm_x86_sub_r32_i32(asm_x86_t *as, int dest_r32, int src_i32) {
369369
}
370370
}
371371

372+
void asm_x86_mul_r32_r32(asm_x86_t *as, int dest_r32, int src_r32) {
373+
// imul reg32, reg/mem32 -- 0x0f 0xaf /r
374+
asm_x86_write_byte_3(as, 0x0f, 0xaf, MODRM_R32(dest_r32) | MODRM_RM_REG | MODRM_RM_R32(src_r32));
375+
}
376+
372377
#if 0
373378
/* shifts not tested */
374379
void asm_x86_shl_r32_by_imm(asm_x86_t *as, int r32, int imm) {

py/asmx86.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ void asm_x86_shl_r32_cl(asm_x86_t* as, int dest_r32);
104104
void asm_x86_sar_r32_cl(asm_x86_t* as, int dest_r32);
105105
void asm_x86_add_r32_r32(asm_x86_t* as, int dest_r32, int src_r32);
106106
void asm_x86_sub_r32_r32(asm_x86_t* as, int dest_r32, int src_r32);
107+
void asm_x86_mul_r32_r32(asm_x86_t* as, int dest_r32, int src_r32);
107108
void asm_x86_cmp_r32_with_r32(asm_x86_t* as, int src_r32_a, int src_r32_b);
108109
void asm_x86_test_r8_with_r8(asm_x86_t* as, int src_r32_a, int src_r32_b);
109110
void asm_x86_setcc_r8(asm_x86_t* as, mp_uint_t jcc_type, int dest_r8);

py/emitnative.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@
146146
#define ASM_AND_REG_REG(as, reg_dest, reg_src) asm_x64_and_r64_r64((as), (reg_dest), (reg_src))
147147
#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_x64_add_r64_r64((as), (reg_dest), (reg_src))
148148
#define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_x64_sub_r64_r64((as), (reg_dest), (reg_src))
149+
#define ASM_MUL_REG_REG(as, reg_dest, reg_src) asm_x64_mul_r64_r64((as), (reg_dest), (reg_src))
149150

150151
#define ASM_LOAD_REG_REG(as, reg_dest, reg_base) asm_x64_mov_mem64_to_r64((as), (reg_base), 0, (reg_dest))
151152
#define ASM_LOAD_REG_REG_OFFSET(as, reg_dest, reg_base, word_offset) asm_x64_mov_mem64_to_r64((as), (reg_base), 8 * (word_offset), (reg_dest))
@@ -290,6 +291,7 @@ STATIC byte mp_f_n_args[MP_F_NUMBER_OF] = {
290291
#define ASM_AND_REG_REG(as, reg_dest, reg_src) asm_x86_and_r32_r32((as), (reg_dest), (reg_src))
291292
#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_x86_add_r32_r32((as), (reg_dest), (reg_src))
292293
#define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_x86_sub_r32_r32((as), (reg_dest), (reg_src))
294+
#define ASM_MUL_REG_REG(as, reg_dest, reg_src) asm_x86_mul_r32_r32((as), (reg_dest), (reg_src))
293295

294296
#define ASM_LOAD_REG_REG(as, reg_dest, reg_base) asm_x86_mov_mem32_to_r32((as), (reg_base), 0, (reg_dest))
295297
#define ASM_LOAD_REG_REG_OFFSET(as, reg_dest, reg_base, word_offset) asm_x86_mov_mem32_to_r32((as), (reg_base), 4 * (word_offset), (reg_dest))
@@ -382,6 +384,7 @@ STATIC byte mp_f_n_args[MP_F_NUMBER_OF] = {
382384
#define ASM_AND_REG_REG(as, reg_dest, reg_src) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_AND, (reg_dest), (reg_src))
383385
#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_thumb_add_rlo_rlo_rlo((as), (reg_dest), (reg_dest), (reg_src))
384386
#define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_thumb_sub_rlo_rlo_rlo((as), (reg_dest), (reg_dest), (reg_src))
387+
#define ASM_MUL_REG_REG(as, reg_dest, reg_src) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_MUL, (reg_dest), (reg_src))
385388

386389
#define ASM_LOAD_REG_REG(as, reg_dest, reg_base) asm_thumb_ldr_rlo_rlo_i5((as), (reg_dest), (reg_base), 0)
387390
#define ASM_LOAD_REG_REG_OFFSET(as, reg_dest, reg_base, word_offset) asm_thumb_ldr_rlo_rlo_i5((as), (reg_dest), (reg_base), (word_offset))
@@ -473,6 +476,7 @@ STATIC byte mp_f_n_args[MP_F_NUMBER_OF] = {
473476
#define ASM_AND_REG_REG(as, reg_dest, reg_src) asm_arm_and_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
474477
#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_arm_add_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
475478
#define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_arm_sub_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
479+
#define ASM_MUL_REG_REG(as, reg_dest, reg_src) asm_arm_mul_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
476480

477481
#define ASM_LOAD_REG_REG(as, reg_dest, reg_base) asm_arm_ldr_reg_reg((as), (reg_dest), (reg_base), 0)
478482
#define ASM_LOAD_REG_REG_OFFSET(as, reg_dest, reg_base, word_offset) asm_arm_ldr_reg_reg((as), (reg_dest), (reg_base), 4 * (word_offset))
@@ -2029,6 +2033,9 @@ STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
20292033
} else if (op == MP_BINARY_OP_SUBTRACT || op == MP_BINARY_OP_INPLACE_SUBTRACT) {
20302034
ASM_SUB_REG_REG(emit->as, REG_ARG_2, reg_rhs);
20312035
emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
2036+
} else if (op == MP_BINARY_OP_MULTIPLY || op == MP_BINARY_OP_INPLACE_MULTIPLY) {
2037+
ASM_MUL_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2038+
emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
20322039
} else if (MP_BINARY_OP_LESS <= op && op <= MP_BINARY_OP_NOT_EQUAL) {
20332040
// comparison ops are (in enum order):
20342041
// MP_BINARY_OP_LESS

tests/micropython/viper_binop_arith.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ def sub(x:int, y:int):
1818
sub(-1, 2)
1919
sub(-42, -3)
2020

21+
@micropython.viper
22+
def mul(x:int, y:int):
23+
print(x * y)
24+
print(y * x)
25+
mul(0, 1)
26+
mul(1, -1)
27+
mul(1, 2)
28+
mul(8, 3)
29+
mul(-3, 4)
30+
mul(-9, -6)
31+
2132
@micropython.viper
2233
def shl(x:int, y:int):
2334
print(x << y)

tests/micropython/viper_binop_arith.py.exp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@
1414
3
1515
-39
1616
39
17+
0
18+
0
19+
-1
20+
-1
21+
2
22+
2
23+
24
24+
24
25+
-12
26+
-12
27+
54
28+
54
1729
1
1830
8
1931
1073741824

0 commit comments

Comments
 (0)