Skip to content

Commit eff10f6

Browse files
committed
py: Implement bl/bx instructions for inline Thumb assembler.
1 parent 4249539 commit eff10f6

File tree

7 files changed

+64
-0
lines changed

7 files changed

+64
-0
lines changed

py/asmthumb.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#define SIGNED_FIT8(x) (((x) & 0xffffff80) == 0) || (((x) & 0xffffff80) == 0xffffff80)
4141
#define SIGNED_FIT9(x) (((x) & 0xffffff00) == 0) || (((x) & 0xffffff00) == 0xffffff00)
4242
#define SIGNED_FIT12(x) (((x) & 0xfffff800) == 0) || (((x) & 0xfffff800) == 0xfffff800)
43+
#define SIGNED_FIT23(x) (((x) & 0xffc00000) == 0) || (((x) & 0xffc00000) == 0xffc00000)
4344

4445
struct _asm_thumb_t {
4546
mp_uint_t pass;
@@ -455,6 +456,20 @@ void asm_thumb_bcc_label(asm_thumb_t *as, int cond, uint label) {
455456
}
456457
}
457458

459+
#define OP_BL_HI(byte_offset) (0xf000 | (((byte_offset) >> 12) & 0x07ff))
460+
#define OP_BL_LO(byte_offset) (0xf800 | (((byte_offset) >> 1) & 0x07ff))
461+
462+
void asm_thumb_bl(asm_thumb_t *as, uint label) {
463+
mp_uint_t dest = get_label_dest(as, label);
464+
mp_int_t rel = dest - as->code_offset;
465+
rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
466+
if (SIGNED_FIT23(rel)) {
467+
asm_thumb_op32(as, OP_BL_HI(rel), OP_BL_LO(rel));
468+
} else {
469+
printf("asm_thumb_bl: branch does not fit in 23 bits\n");
470+
}
471+
}
472+
458473
#define OP_BLX(reg) (0x4780 | ((reg) << 3))
459474
#define OP_SVC(arg) (0xdf00 | (arg))
460475

py/asmthumb.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ void asm_thumb_mov_reg_local_addr(asm_thumb_t *as, uint rlo_dest, int local_num)
207207

208208
void asm_thumb_b_label(asm_thumb_t *as, uint label); // convenience ?
209209
void asm_thumb_bcc_label(asm_thumb_t *as, int cc, uint label); // convenience: picks narrow or wide branch
210+
void asm_thumb_bl(asm_thumb_t *as, uint label);
210211
void asm_thumb_bl_ind(asm_thumb_t *as, void *fun_ptr, uint fun_id, uint reg_temp); // convenience ?
211212

212213
#endif // __MICROPY_INCLUDED_PY_ASMTHUMB_H__

py/emitinlinethumb.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,13 @@ STATIC void emit_inline_thumb_op(emit_inline_asm_t *emit, qstr op, mp_uint_t n_a
336336
int label_num = get_arg_label(emit, op_str, pn_args[0]);
337337
// TODO check that this succeeded, ie branch was within range
338338
asm_thumb_b_n(emit->as, label_num);
339+
} else if (strcmp(op_str, "bl") == 0) {
340+
int label_num = get_arg_label(emit, op_str, pn_args[0]);
341+
// TODO check that this succeeded, ie branch was within range
342+
asm_thumb_bl(emit->as, label_num);
343+
} else if (strcmp(op_str, "bx") == 0) {
344+
mp_uint_t r = get_arg_reg(emit, op_str, pn_args[0], 15);
345+
asm_thumb_op16(emit->as, 0x4700 | (r << 3));
339346
} else if (op_str[0] == 'b' && op_len == 3) {
340347
mp_uint_t cc = -1;
341348
for (mp_uint_t i = 0; i < MP_ARRAY_SIZE(cc_name_table); i++) {

tests/inlineasm/asmblbx.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# test bl and bx instructions
2+
3+
@micropython.asm_thumb
4+
def f(r0):
5+
# jump over the internal functions
6+
b(entry)
7+
8+
label(func1)
9+
add(r0, 2)
10+
bx(lr)
11+
12+
label(func2)
13+
sub(r0, 1)
14+
bx(lr)
15+
16+
label(entry)
17+
bl(func1)
18+
bl(func2)
19+
20+
print(f(0))
21+
print(f(1))

tests/inlineasm/asmblbx.py.exp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
1
2+
2

tests/inlineasm/asmit.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# test it instruction
2+
3+
@micropython.asm_thumb
4+
def f(r0, r1):
5+
cmp(r0, r1)
6+
it(eq)
7+
mov(r0, 100)
8+
print(f(0, 0), f(1, 2))
9+
10+
@micropython.asm_thumb
11+
def g(r0, r1):
12+
cmp(r0, r1)
13+
ite(eq)
14+
mov(r0, 100)
15+
mov(r0, 200)
16+
print(g(0, 0), g(0, 1))

tests/inlineasm/asmit.py.exp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
100 1
2+
100 200

0 commit comments

Comments
 (0)