Skip to content

Commit 096d1e4

Browse files
committed
py: Add lsl/lsr/asr opcode support to inline Thumb2 assembler.
1 parent 949c5c9 commit 096d1e4

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

py/asmthumb.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,21 @@ void asm_thumb_op32(asm_thumb_t *as, uint op1, uint op2);
105105
static inline void asm_thumb_it_cc(asm_thumb_t *as, uint cc, uint mask)
106106
{ asm_thumb_op16(as, ASM_THUMB_OP_IT | (cc << 4) | mask); }
107107

108+
// FORMAT 1: move shifted register
109+
110+
#define ASM_THUMB_FORMAT_1_LSL (0x0000)
111+
#define ASM_THUMB_FORMAT_1_LSR (0x0800)
112+
#define ASM_THUMB_FORMAT_1_ASR (0x1000)
113+
114+
#define ASM_THUMB_FORMAT_1_ENCODE(op, rlo_dest, rlo_src, offset) \
115+
((op) | ((offset) << 6) | ((rlo_src) << 3) | (rlo_dest))
116+
117+
static inline void asm_thumb_format_1(asm_thumb_t *as, uint op, uint rlo_dest, uint rlo_src, uint offset) {
118+
assert(rlo_dest < ASM_THUMB_REG_R8);
119+
assert(rlo_src < ASM_THUMB_REG_R8);
120+
asm_thumb_op16(as, ASM_THUMB_FORMAT_1_ENCODE(op, rlo_dest, rlo_src, offset));
121+
}
122+
108123
// FORMAT 2: add/subtract
109124

110125
#define ASM_THUMB_FORMAT_2_ADD (0x1800)

py/emitinlinethumb.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,21 @@ STATIC void emit_inline_thumb_op(emit_inline_asm_t *emit, qstr op, mp_uint_t n_a
719719

720720
} else if (n_args == 3) {
721721
mp_uint_t op_code;
722-
if (strcmp(op_str, "add") == 0) {
722+
if (strcmp(op_str, "lsl") == 0) {
723+
op_code = ASM_THUMB_FORMAT_1_LSL;
724+
mp_uint_t rlo_dest, rlo_src, i5;
725+
op_format_1:
726+
rlo_dest = get_arg_reg(emit, op_str, pn_args[0], 7);
727+
rlo_src = get_arg_reg(emit, op_str, pn_args[1], 7);
728+
i5 = get_arg_i(emit, op_str, pn_args[2], 0x1f);
729+
asm_thumb_format_1(emit->as, op_code, rlo_dest, rlo_src, i5);
730+
} else if (strcmp(op_str, "lsr") == 0) {
731+
op_code = ASM_THUMB_FORMAT_1_LSR;
732+
goto op_format_1;
733+
} else if (strcmp(op_str, "asr") == 0) {
734+
op_code = ASM_THUMB_FORMAT_1_ASR;
735+
goto op_format_1;
736+
} else if (strcmp(op_str, "add") == 0) {
723737
op_code = ASM_THUMB_FORMAT_2_ADD;
724738
mp_uint_t rlo_dest, rlo_src;
725739
op_format_2:

tests/inlineasm/asmshift.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
@micropython.asm_thumb
2+
def lsl1(r0):
3+
lsl(r0, r0, 1)
4+
print(hex(lsl1(0x123)))
5+
6+
@micropython.asm_thumb
7+
def lsl23(r0):
8+
lsl(r0, r0, 23)
9+
print(hex(lsl23(1)))
10+
11+
@micropython.asm_thumb
12+
def lsr1(r0):
13+
lsr(r0, r0, 1)
14+
print(hex(lsr1(0x123)))
15+
16+
@micropython.asm_thumb
17+
def lsr31(r0):
18+
lsr(r0, r0, 31)
19+
print(hex(lsr31(0x80000000)))
20+
21+
@micropython.asm_thumb
22+
def asr1(r0):
23+
asr(r0, r0, 1)
24+
print(hex(asr1(0x123)))
25+
26+
@micropython.asm_thumb
27+
def asr31(r0):
28+
asr(r0, r0, 31)
29+
print(hex(asr31(0x80000000)))

tests/inlineasm/asmshift.py.exp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
0x246
2+
0x800000
3+
0x91
4+
0x1
5+
0x91
6+
-0x1

0 commit comments

Comments
 (0)