blob: 7037ac518751ca0a4ee12300c7ab2b0eca684149 [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 George6f355fd2014-04-10 14:11:31 +0100212STATIC int get_label_dest(asm_thumb_t *as, uint label) {
Damien5bfb7592013-10-05 18:41:24 +0100213 assert(label < as->max_num_labels);
214 return as->label_offsets[label];
215}
216
Damien George90edf9e2014-04-18 16:56:54 +0100217void asm_thumb_op16(asm_thumb_t *as, uint op) {
218 byte *c = asm_thumb_get_cur_to_write_bytes(as, 2);
219 // little endian
220 c[0] = op;
221 c[1] = op >> 8;
222}
223
224void asm_thumb_op32(asm_thumb_t *as, uint op1, uint op2) {
225 byte *c = asm_thumb_get_cur_to_write_bytes(as, 4);
226 // little endian, op1 then op2
227 c[0] = op1;
228 c[1] = op1 >> 8;
229 c[2] = op2;
230 c[3] = op2 >> 8;
231}
232
Damien George87210872014-04-13 00:30:32 +0100233#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 +0100234
Damien George87210872014-04-13 00:30:32 +0100235void 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 +0100236 assert(rlo_dest < REG_R8);
Damien George87210872014-04-13 00:30:32 +0100237 assert(rlo_src < REG_R8);
Damien George90edf9e2014-04-18 16:56:54 +0100238 asm_thumb_op16(as, OP_FORMAT_2(op, rlo_dest, rlo_src, src_b));
Damien George87210872014-04-13 00:30:32 +0100239}
240
241#define OP_FORMAT_3(op, rlo, i8) ((op) | ((rlo) << 8) | (i8))
242
243void asm_thumb_format_3(asm_thumb_t *as, uint op, uint rlo, int i8) {
244 assert(rlo < REG_R8);
Damien George90edf9e2014-04-18 16:56:54 +0100245 asm_thumb_op16(as, OP_FORMAT_3(op, rlo, i8));
Damien George87210872014-04-13 00:30:32 +0100246}
247
248#define OP_FORMAT_4(op, rlo_dest, rlo_src) ((op) | ((rlo_src) << 3) | (rlo_dest))
249
250void asm_thumb_format_4(asm_thumb_t *as, uint op, uint rlo_dest, uint rlo_src) {
251 assert(rlo_dest < REG_R8);
252 assert(rlo_src < REG_R8);
Damien George90edf9e2014-04-18 16:56:54 +0100253 asm_thumb_op16(as, OP_FORMAT_4(op, rlo_dest, rlo_src));
Damien George87210872014-04-13 00:30:32 +0100254}
255
256#define OP_FORMAT_9_10(op, rlo_dest, rlo_base, offset) ((op) | (((offset) << 6) & 0x07c0) | ((rlo_base) << 3) | (rlo_dest))
257
258void 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 +0100259 asm_thumb_op16(as, OP_FORMAT_9_10(op, rlo_dest, rlo_base, offset));
Damien George87210872014-04-13 00:30:32 +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 }
274 // mov reg_dest, reg_src
Damien George90edf9e2014-04-18 16:56:54 +0100275 asm_thumb_op16(as, 0x4600 | op_lo);
Damien429d7192013-10-04 19:53:11 +0100276}
277
Damien826005c2013-10-05 23:17:28 +0100278#define OP_MOVW (0xf240)
279#define OP_MOVT (0xf2c0)
280
281// if loading lo half with movw, the i16 value will be zero extended into the r32 register!
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200282STATIC 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 +0100283 assert(reg_dest < REG_R15);
Damien826005c2013-10-05 23:17:28 +0100284 // mov[wt] reg_dest, #i16_src
Damien George90edf9e2014-04-18 16:56:54 +0100285 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 +0100286}
287
Damien826005c2013-10-05 23:17:28 +0100288// the i16_src value will be zero extended into the r32 register!
289void asm_thumb_movw_reg_i16(asm_thumb_t *as, uint reg_dest, int i16_src) {
290 asm_thumb_mov_reg_i16(as, OP_MOVW, reg_dest, i16_src);
Damien429d7192013-10-04 19:53:11 +0100291}
292
Damien826005c2013-10-05 23:17:28 +0100293// the i16_src value will be zero extended into the r32 register!
294void asm_thumb_movt_reg_i16(asm_thumb_t *as, uint reg_dest, int i16_src) {
295 asm_thumb_mov_reg_i16(as, OP_MOVT, reg_dest, i16_src);
Damien429d7192013-10-04 19:53:11 +0100296}
297
Damien George47e1b852014-04-08 18:28:33 +0100298void asm_thumb_ite_ge(asm_thumb_t *as) {
Damien George90edf9e2014-04-18 16:56:54 +0100299 asm_thumb_op16(as, 0xbfac);
Damien George47e1b852014-04-08 18:28:33 +0100300}
301
Damien03d41242013-10-06 00:36:05 +0100302#define OP_B_N(byte_offset) (0xe000 | (((byte_offset) >> 1) & 0x07ff))
303
Damien George6f355fd2014-04-10 14:11:31 +0100304void asm_thumb_b_n(asm_thumb_t *as, uint label) {
Damien03d41242013-10-06 00:36:05 +0100305 int dest = get_label_dest(as, label);
306 int rel = dest - as->code_offset;
307 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
308 if (SIGNED_FIT12(rel)) {
Damien George90edf9e2014-04-18 16:56:54 +0100309 asm_thumb_op16(as, OP_B_N(rel));
Damien03d41242013-10-06 00:36:05 +0100310 } else {
311 printf("asm_thumb_b_n: branch does not fit in 12 bits\n");
312 }
313}
314
Damien1a6633a2013-11-03 13:58:19 +0000315#define OP_BCC_N(cond, byte_offset) (0xd000 | ((cond) << 8) | (((byte_offset) >> 1) & 0x00ff))
Damien826005c2013-10-05 23:17:28 +0100316
Damien George6f355fd2014-04-10 14:11:31 +0100317void asm_thumb_bcc_n(asm_thumb_t *as, int cond, uint label) {
Damien826005c2013-10-05 23:17:28 +0100318 int dest = get_label_dest(as, label);
319 int rel = dest - as->code_offset;
320 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
321 if (SIGNED_FIT9(rel)) {
Damien George90edf9e2014-04-18 16:56:54 +0100322 asm_thumb_op16(as, OP_BCC_N(cond, rel));
Damien826005c2013-10-05 23:17:28 +0100323 } else {
Damien1a6633a2013-11-03 13:58:19 +0000324 printf("asm_thumb_bcc_n: branch does not fit in 9 bits\n");
Damien826005c2013-10-05 23:17:28 +0100325 }
326}
327
328void asm_thumb_mov_reg_i32(asm_thumb_t *as, uint reg_dest, machine_uint_t i32) {
329 // movw, movt does it in 8 bytes
330 // ldr [pc, #], dw does it in 6 bytes, but we might not reach to end of code for dw
331
332 asm_thumb_mov_reg_i16(as, OP_MOVW, reg_dest, i32);
333 asm_thumb_mov_reg_i16(as, OP_MOVT, reg_dest, i32 >> 16);
334}
335
336void asm_thumb_mov_reg_i32_optimised(asm_thumb_t *as, uint reg_dest, int i32) {
337 if (reg_dest < 8 && UNSIGNED_FIT8(i32)) {
Damien George87210872014-04-13 00:30:32 +0100338 asm_thumb_mov_rlo_i8(as, reg_dest, i32);
Damien826005c2013-10-05 23:17:28 +0100339 } else if (UNSIGNED_FIT16(i32)) {
340 asm_thumb_mov_reg_i16(as, OP_MOVW, reg_dest, i32);
341 } else {
342 asm_thumb_mov_reg_i32(as, reg_dest, i32);
343 }
344}
345
Damien429d7192013-10-04 19:53:11 +0100346#define OP_STR_TO_SP_OFFSET(rlo_dest, word_offset) (0x9000 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
347#define OP_LDR_FROM_SP_OFFSET(rlo_dest, word_offset) (0x9800 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
348
349void asm_thumb_mov_local_reg(asm_thumb_t *as, int local_num, uint rlo_src) {
350 assert(rlo_src < REG_R8);
351 int word_offset = as->num_locals - local_num - 1;
352 assert(as->pass < ASM_THUMB_PASS_3 || word_offset >= 0);
Damien George90edf9e2014-04-18 16:56:54 +0100353 asm_thumb_op16(as, OP_STR_TO_SP_OFFSET(rlo_src, word_offset));
Damien429d7192013-10-04 19:53:11 +0100354}
355
356void asm_thumb_mov_reg_local(asm_thumb_t *as, uint rlo_dest, int local_num) {
357 assert(rlo_dest < REG_R8);
358 int word_offset = as->num_locals - local_num - 1;
359 assert(as->pass < ASM_THUMB_PASS_3 || word_offset >= 0);
Damien George90edf9e2014-04-18 16:56:54 +0100360 asm_thumb_op16(as, OP_LDR_FROM_SP_OFFSET(rlo_dest, word_offset));
Damien429d7192013-10-04 19:53:11 +0100361}
362
Damien9b9e9962013-11-03 14:25:43 +0000363#define OP_ADD_REG_SP_OFFSET(rlo_dest, word_offset) (0xa800 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
364
365void asm_thumb_mov_reg_local_addr(asm_thumb_t *as, uint rlo_dest, int local_num) {
366 assert(rlo_dest < REG_R8);
367 int word_offset = as->num_locals - local_num - 1;
368 assert(as->pass < ASM_THUMB_PASS_3 || word_offset >= 0);
Damien George90edf9e2014-04-18 16:56:54 +0100369 asm_thumb_op16(as, OP_ADD_REG_SP_OFFSET(rlo_dest, word_offset));
Damien429d7192013-10-04 19:53:11 +0100370}
371
Damien429d7192013-10-04 19:53:11 +0100372// this could be wrong, because it should have a range of +/- 16MiB...
373#define OP_BW_HI(byte_offset) (0xf000 | (((byte_offset) >> 12) & 0x07ff))
374#define OP_BW_LO(byte_offset) (0xb800 | (((byte_offset) >> 1) & 0x07ff))
375
Damien George6f355fd2014-04-10 14:11:31 +0100376void asm_thumb_b_label(asm_thumb_t *as, uint label) {
Damien5bfb7592013-10-05 18:41:24 +0100377 int dest = get_label_dest(as, label);
378 int rel = dest - as->code_offset;
379 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
380 if (dest >= 0 && rel <= -4) {
381 // is a backwards jump, so we know the size of the jump on the first pass
382 // calculate rel assuming 12 bit relative jump
383 if (SIGNED_FIT12(rel)) {
Damien George90edf9e2014-04-18 16:56:54 +0100384 asm_thumb_op16(as, OP_B_N(rel));
Damien429d7192013-10-04 19:53:11 +0100385 } else {
Damien5bfb7592013-10-05 18:41:24 +0100386 goto large_jump;
Damien429d7192013-10-04 19:53:11 +0100387 }
Damien5bfb7592013-10-05 18:41:24 +0100388 } else {
389 // is a forwards jump, so need to assume it's large
390 large_jump:
Damien George90edf9e2014-04-18 16:56:54 +0100391 asm_thumb_op32(as, OP_BW_HI(rel), OP_BW_LO(rel));
Damien429d7192013-10-04 19:53:11 +0100392 }
393}
394
Damien429d7192013-10-04 19:53:11 +0100395// all these bit arithmetics need coverage testing!
Damien1a6633a2013-11-03 13:58:19 +0000396#define OP_BCC_W_HI(cond, byte_offset) (0xf000 | ((cond) << 6) | (((byte_offset) >> 10) & 0x0400) | (((byte_offset) >> 14) & 0x003f))
397#define OP_BCC_W_LO(byte_offset) (0x8000 | ((byte_offset) & 0x2000) | (((byte_offset) >> 1) & 0x0fff))
Damien429d7192013-10-04 19:53:11 +0100398
Damien George6f355fd2014-04-10 14:11:31 +0100399void asm_thumb_bcc_label(asm_thumb_t *as, int cond, uint label) {
Damien5bfb7592013-10-05 18:41:24 +0100400 int dest = get_label_dest(as, label);
401 int rel = dest - as->code_offset;
402 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
403 if (dest >= 0 && rel <= -4) {
404 // is a backwards jump, so we know the size of the jump on the first pass
Damien826005c2013-10-05 23:17:28 +0100405 // calculate rel assuming 9 bit relative jump
Damien5bfb7592013-10-05 18:41:24 +0100406 if (SIGNED_FIT9(rel)) {
Damien George90edf9e2014-04-18 16:56:54 +0100407 asm_thumb_op16(as, OP_BCC_N(cond, rel));
Damien429d7192013-10-04 19:53:11 +0100408 } else {
Damien5bfb7592013-10-05 18:41:24 +0100409 goto large_jump;
Damien429d7192013-10-04 19:53:11 +0100410 }
Damien5bfb7592013-10-05 18:41:24 +0100411 } else {
412 // is a forwards jump, so need to assume it's large
413 large_jump:
Damien George90edf9e2014-04-18 16:56:54 +0100414 asm_thumb_op32(as, OP_BCC_W_HI(cond, rel), OP_BCC_W_LO(rel));
Damien429d7192013-10-04 19:53:11 +0100415 }
416}
417
418#define OP_BLX(reg) (0x4780 | ((reg) << 3))
419#define OP_SVC(arg) (0xdf00 | (arg))
Damien429d7192013-10-04 19:53:11 +0100420
421void asm_thumb_bl_ind(asm_thumb_t *as, void *fun_ptr, uint fun_id, uint reg_temp) {
422 /* TODO make this use less bytes
423 uint rlo_base = REG_R3;
424 uint rlo_dest = REG_R7;
425 uint word_offset = 4;
Damien George90edf9e2014-04-18 16:56:54 +0100426 asm_thumb_op16(as, 0x0000);
427 asm_thumb_op16(as, 0x6800 | (word_offset << 6) | (rlo_base << 3) | rlo_dest); // ldr rlo_dest, [rlo_base, #offset]
428 asm_thumb_op16(as, 0x4780 | (REG_R9 << 3)); // blx reg
Damien429d7192013-10-04 19:53:11 +0100429 */
430
431 if (0) {
432 // load ptr to function into register using immediate, then branch
433 // not relocatable
434 asm_thumb_mov_reg_i32(as, reg_temp, (machine_uint_t)fun_ptr);
Damien George90edf9e2014-04-18 16:56:54 +0100435 asm_thumb_op16(as, OP_BLX(reg_temp));
Damien429d7192013-10-04 19:53:11 +0100436 } else if (1) {
Damien George90edf9e2014-04-18 16:56:54 +0100437 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));
438 asm_thumb_op16(as, OP_BLX(reg_temp));
Damien429d7192013-10-04 19:53:11 +0100439 } else {
440 // use SVC
Damien George90edf9e2014-04-18 16:56:54 +0100441 asm_thumb_op16(as, OP_SVC(fun_id));
Damien429d7192013-10-04 19:53:11 +0100442 }
443}
Damien Georgee67ed5d2014-01-04 13:55:24 +0000444
445#endif // MICROPY_EMIT_THUMB || MICROPY_EMIT_INLINE_THUMB