blob: e8c8927a9ce4e323dd0e02e0945c7a6a3eb79f69 [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
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200124STATIC void asm_thumb_write_op16(asm_thumb_t *as, uint op) {
Damien429d7192013-10-04 19:53:11 +0100125 byte *c = asm_thumb_get_cur_to_write_bytes(as, 2);
126 // little endian
127 c[0] = op;
128 c[1] = op >> 8;
129}
130
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200131STATIC void asm_thumb_write_op32(asm_thumb_t *as, uint op1, uint op2) {
Damien429d7192013-10-04 19:53:11 +0100132 byte *c = asm_thumb_get_cur_to_write_bytes(as, 4);
133 // little endian, op1 then op2
134 c[0] = op1;
135 c[1] = op1 >> 8;
136 c[2] = op2;
137 c[3] = op2 >> 8;
138}
139
140/*
141#define IMM32_L0(x) ((x) & 0xff)
142#define IMM32_L1(x) (((x) >> 8) & 0xff)
143#define IMM32_L2(x) (((x) >> 16) & 0xff)
144#define IMM32_L3(x) (((x) >> 24) & 0xff)
145
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200146STATIC void asm_thumb_write_word32(asm_thumb_t *as, int w32) {
Damien429d7192013-10-04 19:53:11 +0100147 byte *c = asm_thumb_get_cur_to_write_bytes(as, 4);
148 c[0] = IMM32_L0(w32);
149 c[1] = IMM32_L1(w32);
150 c[2] = IMM32_L2(w32);
151 c[3] = IMM32_L3(w32);
152}
153*/
154
155// rlolist is a bit map indicating desired lo-registers
156#define OP_PUSH_RLIST(rlolist) (0xb400 | (rlolist))
157#define OP_PUSH_RLIST_LR(rlolist) (0xb400 | 0x0100 | (rlolist))
158#define OP_POP_RLIST(rlolist) (0xbc00 | (rlolist))
159#define OP_POP_RLIST_PC(rlolist) (0xbc00 | 0x0100 | (rlolist))
160
161#define OP_ADD_SP(num_words) (0xb000 | (num_words))
162#define OP_SUB_SP(num_words) (0xb080 | (num_words))
163
164void asm_thumb_entry(asm_thumb_t *as, int num_locals) {
165 // work out what to push and how many extra space to reserve on stack
166 // so that we have enough for all locals and it's aligned an 8-byte boundary
167 uint reglist;
168 uint stack_adjust;
169 if (num_locals < 0) {
170 num_locals = 0;
171 }
172 // don't ppop r0 because it's used for return value
173 switch (num_locals) {
174 case 0:
175 reglist = 0xf2;
176 stack_adjust = 0;
177 break;
178
179 case 1:
180 reglist = 0xf2;
181 stack_adjust = 0;
182 break;
183
184 case 2:
185 reglist = 0xfe;
186 stack_adjust = 0;
187 break;
188
189 case 3:
190 reglist = 0xfe;
191 stack_adjust = 0;
192 break;
193
194 default:
195 reglist = 0xfe;
196 stack_adjust = ((num_locals - 3) + 1) & (~1);
197 break;
198 }
199 asm_thumb_write_op16(as, OP_PUSH_RLIST_LR(reglist));
200 if (stack_adjust > 0) {
201 asm_thumb_write_op16(as, OP_SUB_SP(stack_adjust));
202 }
203 as->push_reglist = reglist;
204 as->stack_adjust = stack_adjust;
205 as->num_locals = num_locals;
206}
207
208void asm_thumb_exit(asm_thumb_t *as) {
209 if (as->stack_adjust > 0) {
210 asm_thumb_write_op16(as, OP_ADD_SP(as->stack_adjust));
211 }
212 asm_thumb_write_op16(as, OP_POP_RLIST_PC(as->push_reglist));
213}
214
Damien George6f355fd2014-04-10 14:11:31 +0100215void asm_thumb_label_assign(asm_thumb_t *as, uint label) {
Damien5bfb7592013-10-05 18:41:24 +0100216 assert(label < as->max_num_labels);
217 if (as->pass == ASM_THUMB_PASS_2) {
218 // assign label offset
219 assert(as->label_offsets[label] == -1);
220 as->label_offsets[label] = as->code_offset;
221 } else if (as->pass == ASM_THUMB_PASS_3) {
222 // ensure label offset has not changed from PASS_2 to PASS_3
223 //printf("l%d: (at %d=%ld)\n", label, as->label_offsets[label], as->code_offset);
224 assert(as->label_offsets[label] == as->code_offset);
Damien429d7192013-10-04 19:53:11 +0100225 }
226}
227
Damien George6f355fd2014-04-10 14:11:31 +0100228STATIC int get_label_dest(asm_thumb_t *as, uint label) {
Damien5bfb7592013-10-05 18:41:24 +0100229 assert(label < as->max_num_labels);
230 return as->label_offsets[label];
231}
232
Damien826005c2013-10-05 23:17:28 +0100233#define OP_MOVS_RLO_I8(rlo_dest, i8_src) (0x2000 | ((rlo_dest) << 8) | (i8_src))
234
235// the i8_src value will be zero extended into the r32 register!
236void asm_thumb_movs_rlo_i8(asm_thumb_t *as, uint rlo_dest, int i8_src) {
Damien429d7192013-10-04 19:53:11 +0100237 assert(rlo_dest < REG_R8);
Damien826005c2013-10-05 23:17:28 +0100238 // movs rlo_dest, #i8_src
239 asm_thumb_write_op16(as, OP_MOVS_RLO_I8(rlo_dest, i8_src));
Damien429d7192013-10-04 19:53:11 +0100240}
241
Damien826005c2013-10-05 23:17:28 +0100242#define OP_MOVW (0xf240)
243#define OP_MOVT (0xf2c0)
244
245// if loading lo half with movw, the i16 value will be zero extended into the r32 register!
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200246STATIC 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 +0100247 assert(reg_dest < REG_R15);
Damien826005c2013-10-05 23:17:28 +0100248 // mov[wt] reg_dest, #i16_src
249 asm_thumb_write_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 +0100250}
251
Damien826005c2013-10-05 23:17:28 +0100252// the i16_src value will be zero extended into the r32 register!
253void asm_thumb_movw_reg_i16(asm_thumb_t *as, uint reg_dest, int i16_src) {
254 asm_thumb_mov_reg_i16(as, OP_MOVW, reg_dest, i16_src);
Damien429d7192013-10-04 19:53:11 +0100255}
256
Damien826005c2013-10-05 23:17:28 +0100257// the i16_src value will be zero extended into the r32 register!
258void asm_thumb_movt_reg_i16(asm_thumb_t *as, uint reg_dest, int i16_src) {
259 asm_thumb_mov_reg_i16(as, OP_MOVT, reg_dest, i16_src);
Damien429d7192013-10-04 19:53:11 +0100260}
261
262void asm_thumb_mov_reg_reg(asm_thumb_t *as, uint reg_dest, uint reg_src) {
263 uint op_lo;
264 if (reg_src < 8) {
265 op_lo = reg_src << 3;
266 } else {
267 op_lo = 0x40 | ((reg_src - 8) << 3);
268 }
269 if (reg_dest < 8) {
270 op_lo |= reg_dest;
271 } else {
272 op_lo |= 0x80 | (reg_dest - 8);
273 }
Damien826005c2013-10-05 23:17:28 +0100274 // mov reg_dest, reg_src
Damien429d7192013-10-04 19:53:11 +0100275 asm_thumb_write_op16(as, 0x4600 | op_lo);
276}
277
Damien George47e1b852014-04-08 18:28:33 +0100278#define OP_ADD_REG_REG_REG(rlo_dest, rlo_src_a, rlo_src_b) (0x1800 | ((rlo_src_b) << 6) | ((rlo_src_a) << 3) | (rlo_dest))
279
280void asm_thumb_add_reg_reg_reg(asm_thumb_t *as, uint rlo_dest, uint rlo_src_a, uint rlo_src_b) {
281 asm_thumb_write_op16(as, OP_ADD_REG_REG_REG(rlo_dest, rlo_src_a, rlo_src_b));
282}
283
Damien826005c2013-10-05 23:17:28 +0100284#define OP_SUBS_RLO_RLO_I3(rlo_dest, rlo_src, i3_src) (0x1e00 | ((i3_src) << 6) | ((rlo_src) << 3) | (rlo_dest))
285
286void asm_thumb_subs_rlo_rlo_i3(asm_thumb_t *as, uint rlo_dest, uint rlo_src, int i3_src) {
287 assert(rlo_dest < REG_R8);
288 assert(rlo_src < REG_R8);
289 asm_thumb_write_op16(as, OP_SUBS_RLO_RLO_I3(rlo_dest, rlo_src, i3_src));
290}
291
Damien George47e1b852014-04-08 18:28:33 +0100292#define OP_CMP_REG_REG(rlo_a, rlo_b) (0x4280 | ((rlo_b) << 3) | (rlo_a))
293
294void asm_thumb_cmp_reg_reg(asm_thumb_t *as, uint rlo_a, uint rlo_b) {
295 asm_thumb_write_op16(as, OP_CMP_REG_REG(rlo_a, rlo_b));
296}
297
Damien826005c2013-10-05 23:17:28 +0100298#define OP_CMP_RLO_I8(rlo, i8) (0x2800 | ((rlo) << 8) | (i8))
299
300void asm_thumb_cmp_rlo_i8(asm_thumb_t *as, uint rlo, int i8) {
301 assert(rlo < REG_R8);
302 asm_thumb_write_op16(as, OP_CMP_RLO_I8(rlo, i8));
303}
304
Damien George47e1b852014-04-08 18:28:33 +0100305void asm_thumb_ite_ge(asm_thumb_t *as) {
306 asm_thumb_write_op16(as, 0xbfac);
307}
308
Damien03d41242013-10-06 00:36:05 +0100309#define OP_B_N(byte_offset) (0xe000 | (((byte_offset) >> 1) & 0x07ff))
310
Damien George6f355fd2014-04-10 14:11:31 +0100311void asm_thumb_b_n(asm_thumb_t *as, uint label) {
Damien03d41242013-10-06 00:36:05 +0100312 int dest = get_label_dest(as, label);
313 int rel = dest - as->code_offset;
314 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
315 if (SIGNED_FIT12(rel)) {
316 asm_thumb_write_op16(as, OP_B_N(rel));
317 } else {
318 printf("asm_thumb_b_n: branch does not fit in 12 bits\n");
319 }
320}
321
Damien1a6633a2013-11-03 13:58:19 +0000322#define OP_BCC_N(cond, byte_offset) (0xd000 | ((cond) << 8) | (((byte_offset) >> 1) & 0x00ff))
Damien826005c2013-10-05 23:17:28 +0100323
Damien George6f355fd2014-04-10 14:11:31 +0100324void asm_thumb_bcc_n(asm_thumb_t *as, int cond, uint label) {
Damien826005c2013-10-05 23:17:28 +0100325 int dest = get_label_dest(as, label);
326 int rel = dest - as->code_offset;
327 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
328 if (SIGNED_FIT9(rel)) {
Damien1a6633a2013-11-03 13:58:19 +0000329 asm_thumb_write_op16(as, OP_BCC_N(cond, rel));
Damien826005c2013-10-05 23:17:28 +0100330 } else {
Damien1a6633a2013-11-03 13:58:19 +0000331 printf("asm_thumb_bcc_n: branch does not fit in 9 bits\n");
Damien826005c2013-10-05 23:17:28 +0100332 }
333}
334
335void asm_thumb_mov_reg_i32(asm_thumb_t *as, uint reg_dest, machine_uint_t i32) {
336 // movw, movt does it in 8 bytes
337 // ldr [pc, #], dw does it in 6 bytes, but we might not reach to end of code for dw
338
339 asm_thumb_mov_reg_i16(as, OP_MOVW, reg_dest, i32);
340 asm_thumb_mov_reg_i16(as, OP_MOVT, reg_dest, i32 >> 16);
341}
342
343void asm_thumb_mov_reg_i32_optimised(asm_thumb_t *as, uint reg_dest, int i32) {
344 if (reg_dest < 8 && UNSIGNED_FIT8(i32)) {
345 asm_thumb_movs_rlo_i8(as, reg_dest, i32);
346 } else if (UNSIGNED_FIT16(i32)) {
347 asm_thumb_mov_reg_i16(as, OP_MOVW, reg_dest, i32);
348 } else {
349 asm_thumb_mov_reg_i32(as, reg_dest, i32);
350 }
351}
352
Damien429d7192013-10-04 19:53:11 +0100353#define OP_STR_TO_SP_OFFSET(rlo_dest, word_offset) (0x9000 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
354#define OP_LDR_FROM_SP_OFFSET(rlo_dest, word_offset) (0x9800 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
355
356void asm_thumb_mov_local_reg(asm_thumb_t *as, int local_num, uint rlo_src) {
357 assert(rlo_src < REG_R8);
358 int word_offset = as->num_locals - local_num - 1;
359 assert(as->pass < ASM_THUMB_PASS_3 || word_offset >= 0);
360 asm_thumb_write_op16(as, OP_STR_TO_SP_OFFSET(rlo_src, word_offset));
361}
362
363void asm_thumb_mov_reg_local(asm_thumb_t *as, uint rlo_dest, int local_num) {
364 assert(rlo_dest < REG_R8);
365 int word_offset = as->num_locals - local_num - 1;
366 assert(as->pass < ASM_THUMB_PASS_3 || word_offset >= 0);
367 asm_thumb_write_op16(as, OP_LDR_FROM_SP_OFFSET(rlo_dest, word_offset));
368}
369
Damien9b9e9962013-11-03 14:25:43 +0000370#define OP_ADD_REG_SP_OFFSET(rlo_dest, word_offset) (0xa800 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
371
372void asm_thumb_mov_reg_local_addr(asm_thumb_t *as, uint rlo_dest, int local_num) {
373 assert(rlo_dest < REG_R8);
374 int word_offset = as->num_locals - local_num - 1;
375 assert(as->pass < ASM_THUMB_PASS_3 || word_offset >= 0);
376 asm_thumb_write_op16(as, OP_ADD_REG_SP_OFFSET(rlo_dest, word_offset));
Damien429d7192013-10-04 19:53:11 +0100377}
378
Damien429d7192013-10-04 19:53:11 +0100379// this could be wrong, because it should have a range of +/- 16MiB...
380#define OP_BW_HI(byte_offset) (0xf000 | (((byte_offset) >> 12) & 0x07ff))
381#define OP_BW_LO(byte_offset) (0xb800 | (((byte_offset) >> 1) & 0x07ff))
382
Damien George6f355fd2014-04-10 14:11:31 +0100383void asm_thumb_b_label(asm_thumb_t *as, uint label) {
Damien5bfb7592013-10-05 18:41:24 +0100384 int dest = get_label_dest(as, label);
385 int rel = dest - as->code_offset;
386 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
387 if (dest >= 0 && rel <= -4) {
388 // is a backwards jump, so we know the size of the jump on the first pass
389 // calculate rel assuming 12 bit relative jump
390 if (SIGNED_FIT12(rel)) {
Damien03d41242013-10-06 00:36:05 +0100391 asm_thumb_write_op16(as, OP_B_N(rel));
Damien429d7192013-10-04 19:53:11 +0100392 } else {
Damien5bfb7592013-10-05 18:41:24 +0100393 goto large_jump;
Damien429d7192013-10-04 19:53:11 +0100394 }
Damien5bfb7592013-10-05 18:41:24 +0100395 } else {
396 // is a forwards jump, so need to assume it's large
397 large_jump:
398 asm_thumb_write_op32(as, OP_BW_HI(rel), OP_BW_LO(rel));
Damien429d7192013-10-04 19:53:11 +0100399 }
400}
401
Damien429d7192013-10-04 19:53:11 +0100402// all these bit arithmetics need coverage testing!
Damien1a6633a2013-11-03 13:58:19 +0000403#define OP_BCC_W_HI(cond, byte_offset) (0xf000 | ((cond) << 6) | (((byte_offset) >> 10) & 0x0400) | (((byte_offset) >> 14) & 0x003f))
404#define OP_BCC_W_LO(byte_offset) (0x8000 | ((byte_offset) & 0x2000) | (((byte_offset) >> 1) & 0x0fff))
Damien429d7192013-10-04 19:53:11 +0100405
Damien George6f355fd2014-04-10 14:11:31 +0100406void asm_thumb_bcc_label(asm_thumb_t *as, int cond, uint label) {
Damien5bfb7592013-10-05 18:41:24 +0100407 int dest = get_label_dest(as, label);
408 int rel = dest - as->code_offset;
409 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
410 if (dest >= 0 && rel <= -4) {
411 // is a backwards jump, so we know the size of the jump on the first pass
Damien826005c2013-10-05 23:17:28 +0100412 // calculate rel assuming 9 bit relative jump
Damien5bfb7592013-10-05 18:41:24 +0100413 if (SIGNED_FIT9(rel)) {
Damien1a6633a2013-11-03 13:58:19 +0000414 asm_thumb_write_op16(as, OP_BCC_N(cond, rel));
Damien429d7192013-10-04 19:53:11 +0100415 } else {
Damien5bfb7592013-10-05 18:41:24 +0100416 goto large_jump;
Damien429d7192013-10-04 19:53:11 +0100417 }
Damien5bfb7592013-10-05 18:41:24 +0100418 } else {
419 // is a forwards jump, so need to assume it's large
420 large_jump:
Damien1a6633a2013-11-03 13:58:19 +0000421 asm_thumb_write_op32(as, OP_BCC_W_HI(cond, rel), OP_BCC_W_LO(rel));
Damien429d7192013-10-04 19:53:11 +0100422 }
423}
424
425#define OP_BLX(reg) (0x4780 | ((reg) << 3))
426#define OP_SVC(arg) (0xdf00 | (arg))
427#define OP_LDR_FROM_BASE_OFFSET(rlo_dest, rlo_base, word_offset) (0x6800 | (((word_offset) << 6) & 0x07c0) | ((rlo_base) << 3) | (rlo_dest))
428
429void asm_thumb_bl_ind(asm_thumb_t *as, void *fun_ptr, uint fun_id, uint reg_temp) {
430 /* TODO make this use less bytes
431 uint rlo_base = REG_R3;
432 uint rlo_dest = REG_R7;
433 uint word_offset = 4;
434 asm_thumb_write_op16(as, 0x0000);
435 asm_thumb_write_op16(as, 0x6800 | (word_offset << 6) | (rlo_base << 3) | rlo_dest); // ldr rlo_dest, [rlo_base, #offset]
436 asm_thumb_write_op16(as, 0x4780 | (REG_R9 << 3)); // blx reg
437 */
438
439 if (0) {
440 // load ptr to function into register using immediate, then branch
441 // not relocatable
442 asm_thumb_mov_reg_i32(as, reg_temp, (machine_uint_t)fun_ptr);
443 asm_thumb_write_op16(as, OP_BLX(reg_temp));
444 } else if (1) {
445 asm_thumb_write_op16(as, OP_LDR_FROM_BASE_OFFSET(reg_temp, REG_R7, fun_id));
446 asm_thumb_write_op16(as, OP_BLX(reg_temp));
447 } else {
448 // use SVC
449 asm_thumb_write_op16(as, OP_SVC(fun_id));
450 }
451}
Damien Georgee67ed5d2014-01-04 13:55:24 +0000452
453#endif // MICROPY_EMIT_THUMB || MICROPY_EMIT_INLINE_THUMB