summaryrefslogtreecommitdiff
path: root/py/asmthumb.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2018-08-16 13:45:24 +1000
committerDamien George <damien.p.george@gmail.com>2018-08-16 13:45:24 +1000
commit2964b41c282917bfb3f6e3a1d6e3fd7a078abed6 (patch)
tree82e8039004d9a19790878c9cff77413d87cf2f81 /py/asmthumb.c
parentf7d6108d1ad83f26598e73d3b163f3de411c902b (diff)
py/asm*: Support assembling code to jump to a register, and get PC+off.
Useful for position independent code, and implementing state machines.
Diffstat (limited to 'py/asmthumb.c')
-rw-r--r--py/asmthumb.c9
1 files changed, 9 insertions, 0 deletions
diff --git a/py/asmthumb.c b/py/asmthumb.c
index ce9e4fdce..6550d9398 100644
--- a/py/asmthumb.c
+++ b/py/asmthumb.c
@@ -310,6 +310,15 @@ void asm_thumb_mov_reg_local_addr(asm_thumb_t *as, uint rlo_dest, int local_num)
asm_thumb_op16(as, OP_ADD_REG_SP_OFFSET(rlo_dest, word_offset));
}
+void asm_thumb_mov_reg_pcrel(asm_thumb_t *as, uint rlo_dest, uint label) {
+ mp_uint_t dest = get_label_dest(as, label);
+ mp_int_t rel = dest - as->base.code_offset;
+ rel -= 4 + 4; // adjust for mov_reg_i16 and then PC+4 prefetch of add_reg_reg
+ rel |= 1; // to stay in Thumb state when jumping to this address
+ asm_thumb_mov_reg_i16(as, ASM_THUMB_OP_MOVW, rlo_dest, rel); // 4 bytes
+ asm_thumb_add_reg_reg(as, rlo_dest, ASM_THUMB_REG_R15); // 2 bytes
+}
+
// this could be wrong, because it should have a range of +/- 16MiB...
#define OP_BW_HI(byte_offset) (0xf000 | (((byte_offset) >> 12) & 0x07ff))
#define OP_BW_LO(byte_offset) (0xb800 | (((byte_offset) >> 1) & 0x07ff))