blob: a21a3da3096ba93b509f980876901fbba8d04b8c [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Damien429d7192013-10-04 19:53:11 +010027#include <stdio.h>
28#include <assert.h>
29#include <string.h>
30
31#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +000032#include "mpconfig.h"
Damien429d7192013-10-04 19:53:11 +010033#include "asmthumb.h"
34
Damien Georgee67ed5d2014-01-04 13:55:24 +000035// wrapper around everything in this file
36#if MICROPY_EMIT_THUMB || MICROPY_EMIT_INLINE_THUMB
37
Damien429d7192013-10-04 19:53:11 +010038#define UNSIGNED_FIT8(x) (((x) & 0xffffff00) == 0)
39#define UNSIGNED_FIT16(x) (((x) & 0xffff0000) == 0)
40#define SIGNED_FIT8(x) (((x) & 0xffffff80) == 0) || (((x) & 0xffffff80) == 0xffffff80)
41#define SIGNED_FIT9(x) (((x) & 0xffffff00) == 0) || (((x) & 0xffffff00) == 0xffffff00)
42#define SIGNED_FIT12(x) (((x) & 0xfffff800) == 0) || (((x) & 0xfffff800) == 0xfffff800)
43
44struct _asm_thumb_t {
Damien George36db6bc2014-05-07 17:24:22 +010045 uint pass;
Damien429d7192013-10-04 19:53:11 +010046 uint code_offset;
47 uint code_size;
48 byte *code_base;
49 byte dummy_data[8];
50
Damien George6f355fd2014-04-10 14:11:31 +010051 uint max_num_labels;
Damien429d7192013-10-04 19:53:11 +010052 int *label_offsets;
53 int num_locals;
54 uint push_reglist;
55 uint stack_adjust;
56};
57
Damien5bfb7592013-10-05 18:41:24 +010058asm_thumb_t *asm_thumb_new(uint max_num_labels) {
Damien429d7192013-10-04 19:53:11 +010059 asm_thumb_t *as;
60
Damien George36db6bc2014-05-07 17:24:22 +010061 as = m_new0(asm_thumb_t, 1);
Damien5bfb7592013-10-05 18:41:24 +010062 as->max_num_labels = max_num_labels;
63 as->label_offsets = m_new(int, max_num_labels);
Damien429d7192013-10-04 19:53:11 +010064
65 return as;
66}
67
68void asm_thumb_free(asm_thumb_t *as, bool free_code) {
69 if (free_code) {
Damien732407f2013-12-29 19:33:23 +000070 m_del(byte, as->code_base, as->code_size);
Damien429d7192013-10-04 19:53:11 +010071 }
72 /*
73 if (as->label != NULL) {
74 int i;
75 for (i = 0; i < as->label->len; ++i)
76 {
77 Label *lab = &g_array_index(as->label, Label, i);
78 if (lab->unresolved != NULL)
79 g_array_free(lab->unresolved, true);
80 }
81 g_array_free(as->label, true);
82 }
83 */
Damien732407f2013-12-29 19:33:23 +000084 m_del_obj(asm_thumb_t, as);
Damien429d7192013-10-04 19:53:11 +010085}
86
Damien George36db6bc2014-05-07 17:24:22 +010087void asm_thumb_start_pass(asm_thumb_t *as, uint pass) {
Damien429d7192013-10-04 19:53:11 +010088 as->pass = pass;
89 as->code_offset = 0;
Damien George36db6bc2014-05-07 17:24:22 +010090 if (pass == ASM_THUMB_PASS_COMPUTE) {
Damien5bfb7592013-10-05 18:41:24 +010091 memset(as->label_offsets, -1, as->max_num_labels * sizeof(int));
Damien429d7192013-10-04 19:53:11 +010092 }
93}
94
95void asm_thumb_end_pass(asm_thumb_t *as) {
Damien George36db6bc2014-05-07 17:24:22 +010096 if (as->pass == ASM_THUMB_PASS_COMPUTE) {
Damien429d7192013-10-04 19:53:11 +010097 // calculate size of code in bytes
98 as->code_size = as->code_offset;
99 as->code_base = m_new(byte, as->code_size);
Damien0446a0d2013-11-17 13:16:36 +0000100 //printf("code_size: %u\n", as->code_size);
Damien429d7192013-10-04 19:53:11 +0100101 }
102
103 /*
104 // check labels are resolved
105 if (as->label != NULL)
106 {
107 int i;
108 for (i = 0; i < as->label->len; ++i)
109 if (g_array_index(as->label, Label, i).unresolved != NULL)
110 return false;
111 }
112 */
113}
114
115// all functions must go through this one to emit bytes
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200116STATIC byte *asm_thumb_get_cur_to_write_bytes(asm_thumb_t *as, int num_bytes_to_write) {
Damien429d7192013-10-04 19:53:11 +0100117 //printf("emit %d\n", num_bytes_to_write);
Damien George36db6bc2014-05-07 17:24:22 +0100118 if (as->pass < ASM_THUMB_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100119 as->code_offset += num_bytes_to_write;
120 return as->dummy_data;
121 } else {
122 assert(as->code_offset + num_bytes_to_write <= as->code_size);
123 byte *c = as->code_base + as->code_offset;
124 as->code_offset += num_bytes_to_write;
125 return c;
126 }
127}
128
129uint asm_thumb_get_code_size(asm_thumb_t *as) {
130 return as->code_size;
131}
132
133void *asm_thumb_get_code(asm_thumb_t *as) {
134 // need to set low bit to indicate that it's thumb code
135 return (void *)(((machine_uint_t)as->code_base) | 1);
136}
137
138/*
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200139STATIC void asm_thumb_write_byte_1(asm_thumb_t *as, byte b1) {
Damien429d7192013-10-04 19:53:11 +0100140 byte *c = asm_thumb_get_cur_to_write_bytes(as, 1);
141 c[0] = b1;
142}
143*/
144
Damien429d7192013-10-04 19:53:11 +0100145/*
146#define IMM32_L0(x) ((x) & 0xff)
147#define IMM32_L1(x) (((x) >> 8) & 0xff)
148#define IMM32_L2(x) (((x) >> 16) & 0xff)
149#define IMM32_L3(x) (((x) >> 24) & 0xff)
150
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200151STATIC void asm_thumb_write_word32(asm_thumb_t *as, int w32) {
Damien429d7192013-10-04 19:53:11 +0100152 byte *c = asm_thumb_get_cur_to_write_bytes(as, 4);
153 c[0] = IMM32_L0(w32);
154 c[1] = IMM32_L1(w32);
155 c[2] = IMM32_L2(w32);
156 c[3] = IMM32_L3(w32);
157}
158*/
159
160// rlolist is a bit map indicating desired lo-registers
161#define OP_PUSH_RLIST(rlolist) (0xb400 | (rlolist))
162#define OP_PUSH_RLIST_LR(rlolist) (0xb400 | 0x0100 | (rlolist))
163#define OP_POP_RLIST(rlolist) (0xbc00 | (rlolist))
164#define OP_POP_RLIST_PC(rlolist) (0xbc00 | 0x0100 | (rlolist))
165
166#define OP_ADD_SP(num_words) (0xb000 | (num_words))
167#define OP_SUB_SP(num_words) (0xb080 | (num_words))
168
169void asm_thumb_entry(asm_thumb_t *as, int num_locals) {
170 // work out what to push and how many extra space to reserve on stack
171 // so that we have enough for all locals and it's aligned an 8-byte boundary
172 uint reglist;
173 uint stack_adjust;
174 if (num_locals < 0) {
175 num_locals = 0;
176 }
177 // don't ppop r0 because it's used for return value
178 switch (num_locals) {
179 case 0:
180 reglist = 0xf2;
181 stack_adjust = 0;
182 break;
183
184 case 1:
185 reglist = 0xf2;
186 stack_adjust = 0;
187 break;
188
189 case 2:
190 reglist = 0xfe;
191 stack_adjust = 0;
192 break;
193
194 case 3:
195 reglist = 0xfe;
196 stack_adjust = 0;
197 break;
198
199 default:
200 reglist = 0xfe;
201 stack_adjust = ((num_locals - 3) + 1) & (~1);
202 break;
203 }
Damien George90edf9e2014-04-18 16:56:54 +0100204 asm_thumb_op16(as, OP_PUSH_RLIST_LR(reglist));
Damien429d7192013-10-04 19:53:11 +0100205 if (stack_adjust > 0) {
Damien George90edf9e2014-04-18 16:56:54 +0100206 asm_thumb_op16(as, OP_SUB_SP(stack_adjust));
Damien429d7192013-10-04 19:53:11 +0100207 }
208 as->push_reglist = reglist;
209 as->stack_adjust = stack_adjust;
210 as->num_locals = num_locals;
211}
212
213void asm_thumb_exit(asm_thumb_t *as) {
214 if (as->stack_adjust > 0) {
Damien George90edf9e2014-04-18 16:56:54 +0100215 asm_thumb_op16(as, OP_ADD_SP(as->stack_adjust));
Damien429d7192013-10-04 19:53:11 +0100216 }
Damien George90edf9e2014-04-18 16:56:54 +0100217 asm_thumb_op16(as, OP_POP_RLIST_PC(as->push_reglist));
Damien429d7192013-10-04 19:53:11 +0100218}
219
Damien George6f355fd2014-04-10 14:11:31 +0100220void asm_thumb_label_assign(asm_thumb_t *as, uint label) {
Damien5bfb7592013-10-05 18:41:24 +0100221 assert(label < as->max_num_labels);
Damien George36db6bc2014-05-07 17:24:22 +0100222 if (as->pass < ASM_THUMB_PASS_EMIT) {
Damien5bfb7592013-10-05 18:41:24 +0100223 // assign label offset
224 assert(as->label_offsets[label] == -1);
225 as->label_offsets[label] = as->code_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100226 } else {
227 // ensure label offset has not changed from PASS_COMPUTE to PASS_EMIT
Damien5bfb7592013-10-05 18:41:24 +0100228 //printf("l%d: (at %d=%ld)\n", label, as->label_offsets[label], as->code_offset);
229 assert(as->label_offsets[label] == as->code_offset);
Damien429d7192013-10-04 19:53:11 +0100230 }
231}
232
Damien Georgee5f8a772014-04-21 13:33:15 +0100233void asm_thumb_align(asm_thumb_t* as, uint align) {
234 // TODO fill unused data with NOPs?
235 as->code_offset = (as->code_offset + align - 1) & (~(align - 1));
236}
237
238void asm_thumb_data(asm_thumb_t* as, uint bytesize, uint val) {
239 byte *c = asm_thumb_get_cur_to_write_bytes(as, bytesize);
240 // little endian
241 for (uint i = 0; i < bytesize; i++) {
242 *c++ = val;
243 val >>= 8;
244 }
245}
246
Damien George6f355fd2014-04-10 14:11:31 +0100247STATIC int get_label_dest(asm_thumb_t *as, uint label) {
Damien5bfb7592013-10-05 18:41:24 +0100248 assert(label < as->max_num_labels);
249 return as->label_offsets[label];
250}
251
Damien George90edf9e2014-04-18 16:56:54 +0100252void asm_thumb_op16(asm_thumb_t *as, uint op) {
253 byte *c = asm_thumb_get_cur_to_write_bytes(as, 2);
254 // little endian
255 c[0] = op;
256 c[1] = op >> 8;
257}
258
259void asm_thumb_op32(asm_thumb_t *as, uint op1, uint op2) {
260 byte *c = asm_thumb_get_cur_to_write_bytes(as, 4);
261 // little endian, op1 then op2
262 c[0] = op1;
263 c[1] = op1 >> 8;
264 c[2] = op2;
265 c[3] = op2 >> 8;
266}
267
Damien George87210872014-04-13 00:30:32 +0100268#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 +0100269
Damien George87210872014-04-13 00:30:32 +0100270void 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 +0100271 assert(rlo_dest < REG_R8);
Damien George87210872014-04-13 00:30:32 +0100272 assert(rlo_src < REG_R8);
Damien George90edf9e2014-04-18 16:56:54 +0100273 asm_thumb_op16(as, OP_FORMAT_2(op, rlo_dest, rlo_src, src_b));
Damien George87210872014-04-13 00:30:32 +0100274}
275
276#define OP_FORMAT_3(op, rlo, i8) ((op) | ((rlo) << 8) | (i8))
277
278void asm_thumb_format_3(asm_thumb_t *as, uint op, uint rlo, int i8) {
279 assert(rlo < REG_R8);
Damien George90edf9e2014-04-18 16:56:54 +0100280 asm_thumb_op16(as, OP_FORMAT_3(op, rlo, i8));
Damien George87210872014-04-13 00:30:32 +0100281}
282
283#define OP_FORMAT_4(op, rlo_dest, rlo_src) ((op) | ((rlo_src) << 3) | (rlo_dest))
284
285void asm_thumb_format_4(asm_thumb_t *as, uint op, uint rlo_dest, uint rlo_src) {
286 assert(rlo_dest < REG_R8);
287 assert(rlo_src < REG_R8);
Damien George90edf9e2014-04-18 16:56:54 +0100288 asm_thumb_op16(as, OP_FORMAT_4(op, rlo_dest, rlo_src));
Damien George87210872014-04-13 00:30:32 +0100289}
290
291#define OP_FORMAT_9_10(op, rlo_dest, rlo_base, offset) ((op) | (((offset) << 6) & 0x07c0) | ((rlo_base) << 3) | (rlo_dest))
292
293void 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 +0100294 asm_thumb_op16(as, OP_FORMAT_9_10(op, rlo_dest, rlo_base, offset));
Damien George87210872014-04-13 00:30:32 +0100295}
296
297void asm_thumb_mov_reg_reg(asm_thumb_t *as, uint reg_dest, uint reg_src) {
298 uint op_lo;
299 if (reg_src < 8) {
300 op_lo = reg_src << 3;
301 } else {
302 op_lo = 0x40 | ((reg_src - 8) << 3);
303 }
304 if (reg_dest < 8) {
305 op_lo |= reg_dest;
306 } else {
307 op_lo |= 0x80 | (reg_dest - 8);
308 }
309 // mov reg_dest, reg_src
Damien George90edf9e2014-04-18 16:56:54 +0100310 asm_thumb_op16(as, 0x4600 | op_lo);
Damien429d7192013-10-04 19:53:11 +0100311}
312
Damien826005c2013-10-05 23:17:28 +0100313#define OP_MOVW (0xf240)
314#define OP_MOVT (0xf2c0)
315
316// if loading lo half with movw, the i16 value will be zero extended into the r32 register!
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200317STATIC 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 +0100318 assert(reg_dest < REG_R15);
Damien826005c2013-10-05 23:17:28 +0100319 // mov[wt] reg_dest, #i16_src
Damien George90edf9e2014-04-18 16:56:54 +0100320 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 +0100321}
322
Damien826005c2013-10-05 23:17:28 +0100323// the i16_src value will be zero extended into the r32 register!
324void asm_thumb_movw_reg_i16(asm_thumb_t *as, uint reg_dest, int i16_src) {
325 asm_thumb_mov_reg_i16(as, OP_MOVW, reg_dest, i16_src);
Damien429d7192013-10-04 19:53:11 +0100326}
327
Damien826005c2013-10-05 23:17:28 +0100328// the i16_src value will be zero extended into the r32 register!
329void asm_thumb_movt_reg_i16(asm_thumb_t *as, uint reg_dest, int i16_src) {
330 asm_thumb_mov_reg_i16(as, OP_MOVT, reg_dest, i16_src);
Damien429d7192013-10-04 19:53:11 +0100331}
332
Damien George47e1b852014-04-08 18:28:33 +0100333void asm_thumb_ite_ge(asm_thumb_t *as) {
Damien George90edf9e2014-04-18 16:56:54 +0100334 asm_thumb_op16(as, 0xbfac);
Damien George47e1b852014-04-08 18:28:33 +0100335}
336
Damien03d41242013-10-06 00:36:05 +0100337#define OP_B_N(byte_offset) (0xe000 | (((byte_offset) >> 1) & 0x07ff))
338
Damien George6f355fd2014-04-10 14:11:31 +0100339void asm_thumb_b_n(asm_thumb_t *as, uint label) {
Damien03d41242013-10-06 00:36:05 +0100340 int dest = get_label_dest(as, label);
341 int rel = dest - as->code_offset;
342 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
343 if (SIGNED_FIT12(rel)) {
Damien George90edf9e2014-04-18 16:56:54 +0100344 asm_thumb_op16(as, OP_B_N(rel));
Damien03d41242013-10-06 00:36:05 +0100345 } else {
346 printf("asm_thumb_b_n: branch does not fit in 12 bits\n");
347 }
348}
349
Damien1a6633a2013-11-03 13:58:19 +0000350#define OP_BCC_N(cond, byte_offset) (0xd000 | ((cond) << 8) | (((byte_offset) >> 1) & 0x00ff))
Damien826005c2013-10-05 23:17:28 +0100351
Damien George6f355fd2014-04-10 14:11:31 +0100352void asm_thumb_bcc_n(asm_thumb_t *as, int cond, uint label) {
Damien826005c2013-10-05 23:17:28 +0100353 int dest = get_label_dest(as, label);
354 int rel = dest - as->code_offset;
355 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
356 if (SIGNED_FIT9(rel)) {
Damien George90edf9e2014-04-18 16:56:54 +0100357 asm_thumb_op16(as, OP_BCC_N(cond, rel));
Damien826005c2013-10-05 23:17:28 +0100358 } else {
Damien1a6633a2013-11-03 13:58:19 +0000359 printf("asm_thumb_bcc_n: branch does not fit in 9 bits\n");
Damien826005c2013-10-05 23:17:28 +0100360 }
361}
362
363void asm_thumb_mov_reg_i32(asm_thumb_t *as, uint reg_dest, machine_uint_t i32) {
364 // movw, movt does it in 8 bytes
365 // ldr [pc, #], dw does it in 6 bytes, but we might not reach to end of code for dw
366
367 asm_thumb_mov_reg_i16(as, OP_MOVW, reg_dest, i32);
368 asm_thumb_mov_reg_i16(as, OP_MOVT, reg_dest, i32 >> 16);
369}
370
371void asm_thumb_mov_reg_i32_optimised(asm_thumb_t *as, uint reg_dest, int i32) {
372 if (reg_dest < 8 && UNSIGNED_FIT8(i32)) {
Damien George87210872014-04-13 00:30:32 +0100373 asm_thumb_mov_rlo_i8(as, reg_dest, i32);
Damien826005c2013-10-05 23:17:28 +0100374 } else if (UNSIGNED_FIT16(i32)) {
375 asm_thumb_mov_reg_i16(as, OP_MOVW, reg_dest, i32);
376 } else {
377 asm_thumb_mov_reg_i32(as, reg_dest, i32);
378 }
379}
380
Damien George36db6bc2014-05-07 17:24:22 +0100381// i32 is stored as a full word in the code, and aligned to machine-word boundary
382// TODO this is very inefficient, improve it!
383void asm_thumb_mov_reg_i32_aligned(asm_thumb_t *as, uint reg_dest, int i32) {
384 // align on machine-word + 2
385 if ((as->code_offset & 3) == 0) {
386 asm_thumb_op16(as, ASM_THUMB_OP_NOP);
387 }
388 // jump over the i32 value (instruction prefect adds 4 to PC)
389 asm_thumb_op16(as, OP_B_N(0));
390 // store i32 on machine-word aligned boundary
391 asm_thumb_data(as, 4, i32);
392 // do the actual load of the i32 value
393 asm_thumb_mov_reg_i32_optimised(as, reg_dest, i32);
394}
395
Damien429d7192013-10-04 19:53:11 +0100396#define OP_STR_TO_SP_OFFSET(rlo_dest, word_offset) (0x9000 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
397#define OP_LDR_FROM_SP_OFFSET(rlo_dest, word_offset) (0x9800 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
398
399void asm_thumb_mov_local_reg(asm_thumb_t *as, int local_num, uint rlo_src) {
400 assert(rlo_src < REG_R8);
401 int word_offset = as->num_locals - local_num - 1;
Damien George36db6bc2014-05-07 17:24:22 +0100402 assert(as->pass < ASM_THUMB_PASS_EMIT || word_offset >= 0);
Damien George90edf9e2014-04-18 16:56:54 +0100403 asm_thumb_op16(as, OP_STR_TO_SP_OFFSET(rlo_src, word_offset));
Damien429d7192013-10-04 19:53:11 +0100404}
405
406void asm_thumb_mov_reg_local(asm_thumb_t *as, uint rlo_dest, int local_num) {
407 assert(rlo_dest < REG_R8);
408 int word_offset = as->num_locals - local_num - 1;
Damien George36db6bc2014-05-07 17:24:22 +0100409 assert(as->pass < ASM_THUMB_PASS_EMIT || word_offset >= 0);
Damien George90edf9e2014-04-18 16:56:54 +0100410 asm_thumb_op16(as, OP_LDR_FROM_SP_OFFSET(rlo_dest, word_offset));
Damien429d7192013-10-04 19:53:11 +0100411}
412
Damien9b9e9962013-11-03 14:25:43 +0000413#define OP_ADD_REG_SP_OFFSET(rlo_dest, word_offset) (0xa800 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
414
415void asm_thumb_mov_reg_local_addr(asm_thumb_t *as, uint rlo_dest, int local_num) {
416 assert(rlo_dest < REG_R8);
417 int word_offset = as->num_locals - local_num - 1;
Damien George36db6bc2014-05-07 17:24:22 +0100418 assert(as->pass < ASM_THUMB_PASS_EMIT || word_offset >= 0);
Damien George90edf9e2014-04-18 16:56:54 +0100419 asm_thumb_op16(as, OP_ADD_REG_SP_OFFSET(rlo_dest, word_offset));
Damien429d7192013-10-04 19:53:11 +0100420}
421
Damien429d7192013-10-04 19:53:11 +0100422// this could be wrong, because it should have a range of +/- 16MiB...
423#define OP_BW_HI(byte_offset) (0xf000 | (((byte_offset) >> 12) & 0x07ff))
424#define OP_BW_LO(byte_offset) (0xb800 | (((byte_offset) >> 1) & 0x07ff))
425
Damien George6f355fd2014-04-10 14:11:31 +0100426void asm_thumb_b_label(asm_thumb_t *as, uint label) {
Damien5bfb7592013-10-05 18:41:24 +0100427 int dest = get_label_dest(as, label);
428 int rel = dest - as->code_offset;
429 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
430 if (dest >= 0 && rel <= -4) {
431 // is a backwards jump, so we know the size of the jump on the first pass
432 // calculate rel assuming 12 bit relative jump
433 if (SIGNED_FIT12(rel)) {
Damien George90edf9e2014-04-18 16:56:54 +0100434 asm_thumb_op16(as, OP_B_N(rel));
Damien429d7192013-10-04 19:53:11 +0100435 } else {
Damien5bfb7592013-10-05 18:41:24 +0100436 goto large_jump;
Damien429d7192013-10-04 19:53:11 +0100437 }
Damien5bfb7592013-10-05 18:41:24 +0100438 } else {
439 // is a forwards jump, so need to assume it's large
440 large_jump:
Damien George90edf9e2014-04-18 16:56:54 +0100441 asm_thumb_op32(as, OP_BW_HI(rel), OP_BW_LO(rel));
Damien429d7192013-10-04 19:53:11 +0100442 }
443}
444
Damien429d7192013-10-04 19:53:11 +0100445// all these bit arithmetics need coverage testing!
Damien1a6633a2013-11-03 13:58:19 +0000446#define OP_BCC_W_HI(cond, byte_offset) (0xf000 | ((cond) << 6) | (((byte_offset) >> 10) & 0x0400) | (((byte_offset) >> 14) & 0x003f))
447#define OP_BCC_W_LO(byte_offset) (0x8000 | ((byte_offset) & 0x2000) | (((byte_offset) >> 1) & 0x0fff))
Damien429d7192013-10-04 19:53:11 +0100448
Damien George6f355fd2014-04-10 14:11:31 +0100449void asm_thumb_bcc_label(asm_thumb_t *as, int cond, uint label) {
Damien5bfb7592013-10-05 18:41:24 +0100450 int dest = get_label_dest(as, label);
451 int rel = dest - as->code_offset;
452 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
453 if (dest >= 0 && rel <= -4) {
454 // is a backwards jump, so we know the size of the jump on the first pass
Damien826005c2013-10-05 23:17:28 +0100455 // calculate rel assuming 9 bit relative jump
Damien5bfb7592013-10-05 18:41:24 +0100456 if (SIGNED_FIT9(rel)) {
Damien George90edf9e2014-04-18 16:56:54 +0100457 asm_thumb_op16(as, OP_BCC_N(cond, rel));
Damien429d7192013-10-04 19:53:11 +0100458 } else {
Damien5bfb7592013-10-05 18:41:24 +0100459 goto large_jump;
Damien429d7192013-10-04 19:53:11 +0100460 }
Damien5bfb7592013-10-05 18:41:24 +0100461 } else {
462 // is a forwards jump, so need to assume it's large
463 large_jump:
Damien George90edf9e2014-04-18 16:56:54 +0100464 asm_thumb_op32(as, OP_BCC_W_HI(cond, rel), OP_BCC_W_LO(rel));
Damien429d7192013-10-04 19:53:11 +0100465 }
466}
467
468#define OP_BLX(reg) (0x4780 | ((reg) << 3))
469#define OP_SVC(arg) (0xdf00 | (arg))
Damien429d7192013-10-04 19:53:11 +0100470
471void asm_thumb_bl_ind(asm_thumb_t *as, void *fun_ptr, uint fun_id, uint reg_temp) {
472 /* TODO make this use less bytes
473 uint rlo_base = REG_R3;
474 uint rlo_dest = REG_R7;
475 uint word_offset = 4;
Damien George90edf9e2014-04-18 16:56:54 +0100476 asm_thumb_op16(as, 0x0000);
477 asm_thumb_op16(as, 0x6800 | (word_offset << 6) | (rlo_base << 3) | rlo_dest); // ldr rlo_dest, [rlo_base, #offset]
478 asm_thumb_op16(as, 0x4780 | (REG_R9 << 3)); // blx reg
Damien429d7192013-10-04 19:53:11 +0100479 */
480
481 if (0) {
482 // load ptr to function into register using immediate, then branch
483 // not relocatable
484 asm_thumb_mov_reg_i32(as, reg_temp, (machine_uint_t)fun_ptr);
Damien George90edf9e2014-04-18 16:56:54 +0100485 asm_thumb_op16(as, OP_BLX(reg_temp));
Damien429d7192013-10-04 19:53:11 +0100486 } else if (1) {
Damien George90edf9e2014-04-18 16:56:54 +0100487 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));
488 asm_thumb_op16(as, OP_BLX(reg_temp));
Damien429d7192013-10-04 19:53:11 +0100489 } else {
490 // use SVC
Damien George90edf9e2014-04-18 16:56:54 +0100491 asm_thumb_op16(as, OP_SVC(fun_id));
Damien429d7192013-10-04 19:53:11 +0100492 }
493}
Damien Georgee67ed5d2014-01-04 13:55:24 +0000494
495#endif // MICROPY_EMIT_THUMB || MICROPY_EMIT_INLINE_THUMB