blob: c178f634d6a6390ab14b87fe68e620f9ec78b30b [file] [log] [blame]
Damien429d7192013-10-04 19:53:11 +01001#include <stdio.h>
2#include <assert.h>
3#include <string.h>
4
5#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +00006#include "mpconfig.h"
Damien429d7192013-10-04 19:53:11 +01007#include "asmthumb.h"
8
Damien Georgee67ed5d2014-01-04 13:55:24 +00009// wrapper around everything in this file
10#if MICROPY_EMIT_THUMB || MICROPY_EMIT_INLINE_THUMB
11
Damien429d7192013-10-04 19:53:11 +010012#define UNSIGNED_FIT8(x) (((x) & 0xffffff00) == 0)
13#define UNSIGNED_FIT16(x) (((x) & 0xffff0000) == 0)
14#define SIGNED_FIT8(x) (((x) & 0xffffff80) == 0) || (((x) & 0xffffff80) == 0xffffff80)
15#define SIGNED_FIT9(x) (((x) & 0xffffff00) == 0) || (((x) & 0xffffff00) == 0xffffff00)
16#define SIGNED_FIT12(x) (((x) & 0xfffff800) == 0) || (((x) & 0xfffff800) == 0xfffff800)
17
18struct _asm_thumb_t {
19 int pass;
20 uint code_offset;
21 uint code_size;
22 byte *code_base;
23 byte dummy_data[8];
24
Damien George6f355fd2014-04-10 14:11:31 +010025 uint max_num_labels;
Damien429d7192013-10-04 19:53:11 +010026 int *label_offsets;
27 int num_locals;
28 uint push_reglist;
29 uint stack_adjust;
30};
31
Damien5bfb7592013-10-05 18:41:24 +010032asm_thumb_t *asm_thumb_new(uint max_num_labels) {
Damien429d7192013-10-04 19:53:11 +010033 asm_thumb_t *as;
34
35 as = m_new(asm_thumb_t, 1);
36 as->pass = 0;
37 as->code_offset = 0;
38 as->code_size = 0;
39 as->code_base = NULL;
Damien5bfb7592013-10-05 18:41:24 +010040 as->max_num_labels = max_num_labels;
41 as->label_offsets = m_new(int, max_num_labels);
Damien429d7192013-10-04 19:53:11 +010042 as->num_locals = 0;
43
44 return as;
45}
46
47void asm_thumb_free(asm_thumb_t *as, bool free_code) {
48 if (free_code) {
Damien732407f2013-12-29 19:33:23 +000049 m_del(byte, as->code_base, as->code_size);
Damien429d7192013-10-04 19:53:11 +010050 }
51 /*
52 if (as->label != NULL) {
53 int i;
54 for (i = 0; i < as->label->len; ++i)
55 {
56 Label *lab = &g_array_index(as->label, Label, i);
57 if (lab->unresolved != NULL)
58 g_array_free(lab->unresolved, true);
59 }
60 g_array_free(as->label, true);
61 }
62 */
Damien732407f2013-12-29 19:33:23 +000063 m_del_obj(asm_thumb_t, as);
Damien429d7192013-10-04 19:53:11 +010064}
65
66void asm_thumb_start_pass(asm_thumb_t *as, int pass) {
67 as->pass = pass;
68 as->code_offset = 0;
Damien5bfb7592013-10-05 18:41:24 +010069 if (pass == ASM_THUMB_PASS_2) {
70 memset(as->label_offsets, -1, as->max_num_labels * sizeof(int));
Damien429d7192013-10-04 19:53:11 +010071 }
72}
73
74void asm_thumb_end_pass(asm_thumb_t *as) {
Damien5bfb7592013-10-05 18:41:24 +010075 if (as->pass == ASM_THUMB_PASS_2) {
Damien429d7192013-10-04 19:53:11 +010076 // calculate size of code in bytes
77 as->code_size = as->code_offset;
78 as->code_base = m_new(byte, as->code_size);
Damien0446a0d2013-11-17 13:16:36 +000079 //printf("code_size: %u\n", as->code_size);
Damien429d7192013-10-04 19:53:11 +010080 }
81
82 /*
83 // check labels are resolved
84 if (as->label != NULL)
85 {
86 int i;
87 for (i = 0; i < as->label->len; ++i)
88 if (g_array_index(as->label, Label, i).unresolved != NULL)
89 return false;
90 }
91 */
92}
93
94// all functions must go through this one to emit bytes
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020095STATIC byte *asm_thumb_get_cur_to_write_bytes(asm_thumb_t *as, int num_bytes_to_write) {
Damien429d7192013-10-04 19:53:11 +010096 //printf("emit %d\n", num_bytes_to_write);
97 if (as->pass < ASM_THUMB_PASS_3) {
98 as->code_offset += num_bytes_to_write;
99 return as->dummy_data;
100 } else {
101 assert(as->code_offset + num_bytes_to_write <= as->code_size);
102 byte *c = as->code_base + as->code_offset;
103 as->code_offset += num_bytes_to_write;
104 return c;
105 }
106}
107
108uint asm_thumb_get_code_size(asm_thumb_t *as) {
109 return as->code_size;
110}
111
112void *asm_thumb_get_code(asm_thumb_t *as) {
113 // need to set low bit to indicate that it's thumb code
114 return (void *)(((machine_uint_t)as->code_base) | 1);
115}
116
117/*
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200118STATIC void asm_thumb_write_byte_1(asm_thumb_t *as, byte b1) {
Damien429d7192013-10-04 19:53:11 +0100119 byte *c = asm_thumb_get_cur_to_write_bytes(as, 1);
120 c[0] = b1;
121}
122*/
123
Damien429d7192013-10-04 19:53:11 +0100124/*
125#define IMM32_L0(x) ((x) & 0xff)
126#define IMM32_L1(x) (((x) >> 8) & 0xff)
127#define IMM32_L2(x) (((x) >> 16) & 0xff)
128#define IMM32_L3(x) (((x) >> 24) & 0xff)
129
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200130STATIC void asm_thumb_write_word32(asm_thumb_t *as, int w32) {
Damien429d7192013-10-04 19:53:11 +0100131 byte *c = asm_thumb_get_cur_to_write_bytes(as, 4);
132 c[0] = IMM32_L0(w32);
133 c[1] = IMM32_L1(w32);
134 c[2] = IMM32_L2(w32);
135 c[3] = IMM32_L3(w32);
136}
137*/
138
139// rlolist is a bit map indicating desired lo-registers
140#define OP_PUSH_RLIST(rlolist) (0xb400 | (rlolist))
141#define OP_PUSH_RLIST_LR(rlolist) (0xb400 | 0x0100 | (rlolist))
142#define OP_POP_RLIST(rlolist) (0xbc00 | (rlolist))
143#define OP_POP_RLIST_PC(rlolist) (0xbc00 | 0x0100 | (rlolist))
144
145#define OP_ADD_SP(num_words) (0xb000 | (num_words))
146#define OP_SUB_SP(num_words) (0xb080 | (num_words))
147
148void asm_thumb_entry(asm_thumb_t *as, int num_locals) {
149 // work out what to push and how many extra space to reserve on stack
150 // so that we have enough for all locals and it's aligned an 8-byte boundary
151 uint reglist;
152 uint stack_adjust;
153 if (num_locals < 0) {
154 num_locals = 0;
155 }
156 // don't ppop r0 because it's used for return value
157 switch (num_locals) {
158 case 0:
159 reglist = 0xf2;
160 stack_adjust = 0;
161 break;
162
163 case 1:
164 reglist = 0xf2;
165 stack_adjust = 0;
166 break;
167
168 case 2:
169 reglist = 0xfe;
170 stack_adjust = 0;
171 break;
172
173 case 3:
174 reglist = 0xfe;
175 stack_adjust = 0;
176 break;
177
178 default:
179 reglist = 0xfe;
180 stack_adjust = ((num_locals - 3) + 1) & (~1);
181 break;
182 }
Damien George90edf9e2014-04-18 16:56:54 +0100183 asm_thumb_op16(as, OP_PUSH_RLIST_LR(reglist));
Damien429d7192013-10-04 19:53:11 +0100184 if (stack_adjust > 0) {
Damien George90edf9e2014-04-18 16:56:54 +0100185 asm_thumb_op16(as, OP_SUB_SP(stack_adjust));
Damien429d7192013-10-04 19:53:11 +0100186 }
187 as->push_reglist = reglist;
188 as->stack_adjust = stack_adjust;
189 as->num_locals = num_locals;
190}
191
192void asm_thumb_exit(asm_thumb_t *as) {
193 if (as->stack_adjust > 0) {
Damien George90edf9e2014-04-18 16:56:54 +0100194 asm_thumb_op16(as, OP_ADD_SP(as->stack_adjust));
Damien429d7192013-10-04 19:53:11 +0100195 }
Damien George90edf9e2014-04-18 16:56:54 +0100196 asm_thumb_op16(as, OP_POP_RLIST_PC(as->push_reglist));
Damien429d7192013-10-04 19:53:11 +0100197}
198
Damien George6f355fd2014-04-10 14:11:31 +0100199void asm_thumb_label_assign(asm_thumb_t *as, uint label) {
Damien5bfb7592013-10-05 18:41:24 +0100200 assert(label < as->max_num_labels);
201 if (as->pass == ASM_THUMB_PASS_2) {
202 // assign label offset
203 assert(as->label_offsets[label] == -1);
204 as->label_offsets[label] = as->code_offset;
205 } else if (as->pass == ASM_THUMB_PASS_3) {
206 // ensure label offset has not changed from PASS_2 to PASS_3
207 //printf("l%d: (at %d=%ld)\n", label, as->label_offsets[label], as->code_offset);
208 assert(as->label_offsets[label] == as->code_offset);
Damien429d7192013-10-04 19:53:11 +0100209 }
210}
211
Damien Georgee5f8a772014-04-21 13:33:15 +0100212void asm_thumb_align(asm_thumb_t* as, uint align) {
213 // TODO fill unused data with NOPs?
214 as->code_offset = (as->code_offset + align - 1) & (~(align - 1));
215}
216
217void asm_thumb_data(asm_thumb_t* as, uint bytesize, uint val) {
218 byte *c = asm_thumb_get_cur_to_write_bytes(as, bytesize);
219 // little endian
220 for (uint i = 0; i < bytesize; i++) {
221 *c++ = val;
222 val >>= 8;
223 }
224}
225
Damien George6f355fd2014-04-10 14:11:31 +0100226STATIC int get_label_dest(asm_thumb_t *as, uint label) {
Damien5bfb7592013-10-05 18:41:24 +0100227 assert(label < as->max_num_labels);
228 return as->label_offsets[label];
229}
230
Damien George90edf9e2014-04-18 16:56:54 +0100231void asm_thumb_op16(asm_thumb_t *as, uint op) {
232 byte *c = asm_thumb_get_cur_to_write_bytes(as, 2);
233 // little endian
234 c[0] = op;
235 c[1] = op >> 8;
236}
237
238void asm_thumb_op32(asm_thumb_t *as, uint op1, uint op2) {
239 byte *c = asm_thumb_get_cur_to_write_bytes(as, 4);
240 // little endian, op1 then op2
241 c[0] = op1;
242 c[1] = op1 >> 8;
243 c[2] = op2;
244 c[3] = op2 >> 8;
245}
246
Damien George87210872014-04-13 00:30:32 +0100247#define OP_FORMAT_2(op, rlo_dest, rlo_src, src_b) ((op) | ((src_b) << 6) | ((rlo_src) << 3) | (rlo_dest))
Damien826005c2013-10-05 23:17:28 +0100248
Damien George87210872014-04-13 00:30:32 +0100249void asm_thumb_format_2(asm_thumb_t *as, uint op, uint rlo_dest, uint rlo_src, int src_b) {
Damien429d7192013-10-04 19:53:11 +0100250 assert(rlo_dest < REG_R8);
Damien George87210872014-04-13 00:30:32 +0100251 assert(rlo_src < REG_R8);
Damien George90edf9e2014-04-18 16:56:54 +0100252 asm_thumb_op16(as, OP_FORMAT_2(op, rlo_dest, rlo_src, src_b));
Damien George87210872014-04-13 00:30:32 +0100253}
254
255#define OP_FORMAT_3(op, rlo, i8) ((op) | ((rlo) << 8) | (i8))
256
257void asm_thumb_format_3(asm_thumb_t *as, uint op, uint rlo, int i8) {
258 assert(rlo < REG_R8);
Damien George90edf9e2014-04-18 16:56:54 +0100259 asm_thumb_op16(as, OP_FORMAT_3(op, rlo, i8));
Damien George87210872014-04-13 00:30:32 +0100260}
261
262#define OP_FORMAT_4(op, rlo_dest, rlo_src) ((op) | ((rlo_src) << 3) | (rlo_dest))
263
264void asm_thumb_format_4(asm_thumb_t *as, uint op, uint rlo_dest, uint rlo_src) {
265 assert(rlo_dest < REG_R8);
266 assert(rlo_src < REG_R8);
Damien George90edf9e2014-04-18 16:56:54 +0100267 asm_thumb_op16(as, OP_FORMAT_4(op, rlo_dest, rlo_src));
Damien George87210872014-04-13 00:30:32 +0100268}
269
270#define OP_FORMAT_9_10(op, rlo_dest, rlo_base, offset) ((op) | (((offset) << 6) & 0x07c0) | ((rlo_base) << 3) | (rlo_dest))
271
272void asm_thumb_format_9_10(asm_thumb_t *as, uint op, uint rlo_dest, uint rlo_base, uint offset) {
Damien George90edf9e2014-04-18 16:56:54 +0100273 asm_thumb_op16(as, OP_FORMAT_9_10(op, rlo_dest, rlo_base, offset));
Damien George87210872014-04-13 00:30:32 +0100274}
275
276void asm_thumb_mov_reg_reg(asm_thumb_t *as, uint reg_dest, uint reg_src) {
277 uint op_lo;
278 if (reg_src < 8) {
279 op_lo = reg_src << 3;
280 } else {
281 op_lo = 0x40 | ((reg_src - 8) << 3);
282 }
283 if (reg_dest < 8) {
284 op_lo |= reg_dest;
285 } else {
286 op_lo |= 0x80 | (reg_dest - 8);
287 }
288 // mov reg_dest, reg_src
Damien George90edf9e2014-04-18 16:56:54 +0100289 asm_thumb_op16(as, 0x4600 | op_lo);
Damien429d7192013-10-04 19:53:11 +0100290}
291
Damien826005c2013-10-05 23:17:28 +0100292#define OP_MOVW (0xf240)
293#define OP_MOVT (0xf2c0)
294
295// if loading lo half with movw, the i16 value will be zero extended into the r32 register!
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200296STATIC void asm_thumb_mov_reg_i16(asm_thumb_t *as, uint mov_op, uint reg_dest, int i16_src) {
Damien429d7192013-10-04 19:53:11 +0100297 assert(reg_dest < REG_R15);
Damien826005c2013-10-05 23:17:28 +0100298 // mov[wt] reg_dest, #i16_src
Damien George90edf9e2014-04-18 16:56:54 +0100299 asm_thumb_op32(as, mov_op | ((i16_src >> 1) & 0x0400) | ((i16_src >> 12) & 0xf), ((i16_src << 4) & 0x7000) | (reg_dest << 8) | (i16_src & 0xff));
Damien429d7192013-10-04 19:53:11 +0100300}
301
Damien826005c2013-10-05 23:17:28 +0100302// the i16_src value will be zero extended into the r32 register!
303void asm_thumb_movw_reg_i16(asm_thumb_t *as, uint reg_dest, int i16_src) {
304 asm_thumb_mov_reg_i16(as, OP_MOVW, reg_dest, i16_src);
Damien429d7192013-10-04 19:53:11 +0100305}
306
Damien826005c2013-10-05 23:17:28 +0100307// the i16_src value will be zero extended into the r32 register!
308void asm_thumb_movt_reg_i16(asm_thumb_t *as, uint reg_dest, int i16_src) {
309 asm_thumb_mov_reg_i16(as, OP_MOVT, reg_dest, i16_src);
Damien429d7192013-10-04 19:53:11 +0100310}
311
Damien George47e1b852014-04-08 18:28:33 +0100312void asm_thumb_ite_ge(asm_thumb_t *as) {
Damien George90edf9e2014-04-18 16:56:54 +0100313 asm_thumb_op16(as, 0xbfac);
Damien George47e1b852014-04-08 18:28:33 +0100314}
315
Damien03d41242013-10-06 00:36:05 +0100316#define OP_B_N(byte_offset) (0xe000 | (((byte_offset) >> 1) & 0x07ff))
317
Damien George6f355fd2014-04-10 14:11:31 +0100318void asm_thumb_b_n(asm_thumb_t *as, uint label) {
Damien03d41242013-10-06 00:36:05 +0100319 int dest = get_label_dest(as, label);
320 int rel = dest - as->code_offset;
321 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
322 if (SIGNED_FIT12(rel)) {
Damien George90edf9e2014-04-18 16:56:54 +0100323 asm_thumb_op16(as, OP_B_N(rel));
Damien03d41242013-10-06 00:36:05 +0100324 } else {
325 printf("asm_thumb_b_n: branch does not fit in 12 bits\n");
326 }
327}
328
Damien1a6633a2013-11-03 13:58:19 +0000329#define OP_BCC_N(cond, byte_offset) (0xd000 | ((cond) << 8) | (((byte_offset) >> 1) & 0x00ff))
Damien826005c2013-10-05 23:17:28 +0100330
Damien George6f355fd2014-04-10 14:11:31 +0100331void asm_thumb_bcc_n(asm_thumb_t *as, int cond, uint label) {
Damien826005c2013-10-05 23:17:28 +0100332 int dest = get_label_dest(as, label);
333 int rel = dest - as->code_offset;
334 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
335 if (SIGNED_FIT9(rel)) {
Damien George90edf9e2014-04-18 16:56:54 +0100336 asm_thumb_op16(as, OP_BCC_N(cond, rel));
Damien826005c2013-10-05 23:17:28 +0100337 } else {
Damien1a6633a2013-11-03 13:58:19 +0000338 printf("asm_thumb_bcc_n: branch does not fit in 9 bits\n");
Damien826005c2013-10-05 23:17:28 +0100339 }
340}
341
342void asm_thumb_mov_reg_i32(asm_thumb_t *as, uint reg_dest, machine_uint_t i32) {
343 // movw, movt does it in 8 bytes
344 // ldr [pc, #], dw does it in 6 bytes, but we might not reach to end of code for dw
345
346 asm_thumb_mov_reg_i16(as, OP_MOVW, reg_dest, i32);
347 asm_thumb_mov_reg_i16(as, OP_MOVT, reg_dest, i32 >> 16);
348}
349
350void asm_thumb_mov_reg_i32_optimised(asm_thumb_t *as, uint reg_dest, int i32) {
351 if (reg_dest < 8 && UNSIGNED_FIT8(i32)) {
Damien George87210872014-04-13 00:30:32 +0100352 asm_thumb_mov_rlo_i8(as, reg_dest, i32);
Damien826005c2013-10-05 23:17:28 +0100353 } else if (UNSIGNED_FIT16(i32)) {
354 asm_thumb_mov_reg_i16(as, OP_MOVW, reg_dest, i32);
355 } else {
356 asm_thumb_mov_reg_i32(as, reg_dest, i32);
357 }
358}
359
Damien429d7192013-10-04 19:53:11 +0100360#define OP_STR_TO_SP_OFFSET(rlo_dest, word_offset) (0x9000 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
361#define OP_LDR_FROM_SP_OFFSET(rlo_dest, word_offset) (0x9800 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
362
363void asm_thumb_mov_local_reg(asm_thumb_t *as, int local_num, uint rlo_src) {
364 assert(rlo_src < REG_R8);
365 int word_offset = as->num_locals - local_num - 1;
366 assert(as->pass < ASM_THUMB_PASS_3 || word_offset >= 0);
Damien George90edf9e2014-04-18 16:56:54 +0100367 asm_thumb_op16(as, OP_STR_TO_SP_OFFSET(rlo_src, word_offset));
Damien429d7192013-10-04 19:53:11 +0100368}
369
370void asm_thumb_mov_reg_local(asm_thumb_t *as, uint rlo_dest, int local_num) {
371 assert(rlo_dest < REG_R8);
372 int word_offset = as->num_locals - local_num - 1;
373 assert(as->pass < ASM_THUMB_PASS_3 || word_offset >= 0);
Damien George90edf9e2014-04-18 16:56:54 +0100374 asm_thumb_op16(as, OP_LDR_FROM_SP_OFFSET(rlo_dest, word_offset));
Damien429d7192013-10-04 19:53:11 +0100375}
376
Damien9b9e9962013-11-03 14:25:43 +0000377#define OP_ADD_REG_SP_OFFSET(rlo_dest, word_offset) (0xa800 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
378
379void asm_thumb_mov_reg_local_addr(asm_thumb_t *as, uint rlo_dest, int local_num) {
380 assert(rlo_dest < REG_R8);
381 int word_offset = as->num_locals - local_num - 1;
382 assert(as->pass < ASM_THUMB_PASS_3 || word_offset >= 0);
Damien George90edf9e2014-04-18 16:56:54 +0100383 asm_thumb_op16(as, OP_ADD_REG_SP_OFFSET(rlo_dest, word_offset));
Damien429d7192013-10-04 19:53:11 +0100384}
385
Damien429d7192013-10-04 19:53:11 +0100386// this could be wrong, because it should have a range of +/- 16MiB...
387#define OP_BW_HI(byte_offset) (0xf000 | (((byte_offset) >> 12) & 0x07ff))
388#define OP_BW_LO(byte_offset) (0xb800 | (((byte_offset) >> 1) & 0x07ff))
389
Damien George6f355fd2014-04-10 14:11:31 +0100390void asm_thumb_b_label(asm_thumb_t *as, uint label) {
Damien5bfb7592013-10-05 18:41:24 +0100391 int dest = get_label_dest(as, label);
392 int rel = dest - as->code_offset;
393 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
394 if (dest >= 0 && rel <= -4) {
395 // is a backwards jump, so we know the size of the jump on the first pass
396 // calculate rel assuming 12 bit relative jump
397 if (SIGNED_FIT12(rel)) {
Damien George90edf9e2014-04-18 16:56:54 +0100398 asm_thumb_op16(as, OP_B_N(rel));
Damien429d7192013-10-04 19:53:11 +0100399 } else {
Damien5bfb7592013-10-05 18:41:24 +0100400 goto large_jump;
Damien429d7192013-10-04 19:53:11 +0100401 }
Damien5bfb7592013-10-05 18:41:24 +0100402 } else {
403 // is a forwards jump, so need to assume it's large
404 large_jump:
Damien George90edf9e2014-04-18 16:56:54 +0100405 asm_thumb_op32(as, OP_BW_HI(rel), OP_BW_LO(rel));
Damien429d7192013-10-04 19:53:11 +0100406 }
407}
408
Damien429d7192013-10-04 19:53:11 +0100409// all these bit arithmetics need coverage testing!
Damien1a6633a2013-11-03 13:58:19 +0000410#define OP_BCC_W_HI(cond, byte_offset) (0xf000 | ((cond) << 6) | (((byte_offset) >> 10) & 0x0400) | (((byte_offset) >> 14) & 0x003f))
411#define OP_BCC_W_LO(byte_offset) (0x8000 | ((byte_offset) & 0x2000) | (((byte_offset) >> 1) & 0x0fff))
Damien429d7192013-10-04 19:53:11 +0100412
Damien George6f355fd2014-04-10 14:11:31 +0100413void asm_thumb_bcc_label(asm_thumb_t *as, int cond, uint label) {
Damien5bfb7592013-10-05 18:41:24 +0100414 int dest = get_label_dest(as, label);
415 int rel = dest - as->code_offset;
416 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
417 if (dest >= 0 && rel <= -4) {
418 // is a backwards jump, so we know the size of the jump on the first pass
Damien826005c2013-10-05 23:17:28 +0100419 // calculate rel assuming 9 bit relative jump
Damien5bfb7592013-10-05 18:41:24 +0100420 if (SIGNED_FIT9(rel)) {
Damien George90edf9e2014-04-18 16:56:54 +0100421 asm_thumb_op16(as, OP_BCC_N(cond, rel));
Damien429d7192013-10-04 19:53:11 +0100422 } else {
Damien5bfb7592013-10-05 18:41:24 +0100423 goto large_jump;
Damien429d7192013-10-04 19:53:11 +0100424 }
Damien5bfb7592013-10-05 18:41:24 +0100425 } else {
426 // is a forwards jump, so need to assume it's large
427 large_jump:
Damien George90edf9e2014-04-18 16:56:54 +0100428 asm_thumb_op32(as, OP_BCC_W_HI(cond, rel), OP_BCC_W_LO(rel));
Damien429d7192013-10-04 19:53:11 +0100429 }
430}
431
432#define OP_BLX(reg) (0x4780 | ((reg) << 3))
433#define OP_SVC(arg) (0xdf00 | (arg))
Damien429d7192013-10-04 19:53:11 +0100434
435void asm_thumb_bl_ind(asm_thumb_t *as, void *fun_ptr, uint fun_id, uint reg_temp) {
436 /* TODO make this use less bytes
437 uint rlo_base = REG_R3;
438 uint rlo_dest = REG_R7;
439 uint word_offset = 4;
Damien George90edf9e2014-04-18 16:56:54 +0100440 asm_thumb_op16(as, 0x0000);
441 asm_thumb_op16(as, 0x6800 | (word_offset << 6) | (rlo_base << 3) | rlo_dest); // ldr rlo_dest, [rlo_base, #offset]
442 asm_thumb_op16(as, 0x4780 | (REG_R9 << 3)); // blx reg
Damien429d7192013-10-04 19:53:11 +0100443 */
444
445 if (0) {
446 // load ptr to function into register using immediate, then branch
447 // not relocatable
448 asm_thumb_mov_reg_i32(as, reg_temp, (machine_uint_t)fun_ptr);
Damien George90edf9e2014-04-18 16:56:54 +0100449 asm_thumb_op16(as, OP_BLX(reg_temp));
Damien429d7192013-10-04 19:53:11 +0100450 } else if (1) {
Damien George90edf9e2014-04-18 16:56:54 +0100451 asm_thumb_op16(as, OP_FORMAT_9_10(ASM_THUMB_FORMAT_9_LDR | ASM_THUMB_FORMAT_9_WORD_TRANSFER, reg_temp, REG_R7, fun_id));
452 asm_thumb_op16(as, OP_BLX(reg_temp));
Damien429d7192013-10-04 19:53:11 +0100453 } else {
454 // use SVC
Damien George90edf9e2014-04-18 16:56:54 +0100455 asm_thumb_op16(as, OP_SVC(fun_id));
Damien429d7192013-10-04 19:53:11 +0100456 }
457}
Damien Georgee67ed5d2014-01-04 13:55:24 +0000458
459#endif // MICROPY_EMIT_THUMB || MICROPY_EMIT_INLINE_THUMB