blob: 7ae6862b51b367583ee8458cb3f9e8508602bd03 [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
Damiend99b0522013-12-21 18:17:45 +000031#include "mpconfig.h"
Paul Sokolovsky59c675a2014-06-21 22:43:22 +030032#include "misc.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 George851f15f2014-09-29 10:05:32 +010045 mp_uint_t pass;
46 mp_uint_t code_offset;
47 mp_uint_t code_size;
Damien429d7192013-10-04 19:53:11 +010048 byte *code_base;
Damien George95977712014-05-10 18:07:08 +010049 byte dummy_data[4];
Damien429d7192013-10-04 19:53:11 +010050
Damien George851f15f2014-09-29 10:05:32 +010051 mp_uint_t max_num_labels;
52 mp_uint_t *label_offsets;
53 mp_uint_t push_reglist;
54 mp_uint_t stack_adjust;
Damien429d7192013-10-04 19:53:11 +010055};
56
Damien5bfb7592013-10-05 18:41:24 +010057asm_thumb_t *asm_thumb_new(uint max_num_labels) {
Damien429d7192013-10-04 19:53:11 +010058 asm_thumb_t *as;
59
Damien George36db6bc2014-05-07 17:24:22 +010060 as = m_new0(asm_thumb_t, 1);
Damien5bfb7592013-10-05 18:41:24 +010061 as->max_num_labels = max_num_labels;
Damien George851f15f2014-09-29 10:05:32 +010062 as->label_offsets = m_new(mp_uint_t, max_num_labels);
Damien429d7192013-10-04 19:53:11 +010063
64 return as;
65}
66
67void asm_thumb_free(asm_thumb_t *as, bool free_code) {
68 if (free_code) {
Fabian Vogtb7235b82014-09-03 16:59:33 +020069 MP_PLAT_FREE_EXEC(as->code_base, as->code_size);
Damien429d7192013-10-04 19:53:11 +010070 }
Damien George0b610de2014-09-29 16:25:04 +010071 m_del(mp_uint_t, as->label_offsets, as->max_num_labels);
Damien732407f2013-12-29 19:33:23 +000072 m_del_obj(asm_thumb_t, as);
Damien429d7192013-10-04 19:53:11 +010073}
74
Damien George36db6bc2014-05-07 17:24:22 +010075void asm_thumb_start_pass(asm_thumb_t *as, uint pass) {
Damien429d7192013-10-04 19:53:11 +010076 as->pass = pass;
77 as->code_offset = 0;
Damien George36db6bc2014-05-07 17:24:22 +010078 if (pass == ASM_THUMB_PASS_COMPUTE) {
Damien George851f15f2014-09-29 10:05:32 +010079 memset(as->label_offsets, -1, as->max_num_labels * sizeof(mp_uint_t));
Damien429d7192013-10-04 19:53:11 +010080 }
81}
82
83void asm_thumb_end_pass(asm_thumb_t *as) {
Damien George36db6bc2014-05-07 17:24:22 +010084 if (as->pass == ASM_THUMB_PASS_COMPUTE) {
Fabian Vogtb7235b82014-09-03 16:59:33 +020085 MP_PLAT_ALLOC_EXEC(as->code_offset, (void**) &as->code_base, &as->code_size);
86 if(as->code_base == NULL) {
87 assert(0);
88 }
Damien0446a0d2013-11-17 13:16:36 +000089 //printf("code_size: %u\n", as->code_size);
Damien429d7192013-10-04 19:53:11 +010090 }
91
92 /*
93 // check labels are resolved
94 if (as->label != NULL)
95 {
96 int i;
97 for (i = 0; i < as->label->len; ++i)
98 if (g_array_index(as->label, Label, i).unresolved != NULL)
99 return false;
100 }
101 */
102}
103
104// all functions must go through this one to emit bytes
Damien George95977712014-05-10 18:07:08 +0100105// if as->pass < ASM_THUMB_PASS_EMIT, then this function only returns a buffer of 4 bytes length
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200106STATIC byte *asm_thumb_get_cur_to_write_bytes(asm_thumb_t *as, int num_bytes_to_write) {
Damien429d7192013-10-04 19:53:11 +0100107 //printf("emit %d\n", num_bytes_to_write);
Damien George36db6bc2014-05-07 17:24:22 +0100108 if (as->pass < ASM_THUMB_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100109 as->code_offset += num_bytes_to_write;
110 return as->dummy_data;
111 } else {
112 assert(as->code_offset + num_bytes_to_write <= as->code_size);
113 byte *c = as->code_base + as->code_offset;
114 as->code_offset += num_bytes_to_write;
115 return c;
116 }
117}
118
119uint asm_thumb_get_code_size(asm_thumb_t *as) {
120 return as->code_size;
121}
122
123void *asm_thumb_get_code(asm_thumb_t *as) {
Damien George3c658a42014-08-24 16:28:17 +0100124 return as->code_base;
Damien429d7192013-10-04 19:53:11 +0100125}
126
127/*
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200128STATIC void asm_thumb_write_byte_1(asm_thumb_t *as, byte b1) {
Damien429d7192013-10-04 19:53:11 +0100129 byte *c = asm_thumb_get_cur_to_write_bytes(as, 1);
130 c[0] = b1;
131}
132*/
133
Damien429d7192013-10-04 19:53:11 +0100134/*
135#define IMM32_L0(x) ((x) & 0xff)
136#define IMM32_L1(x) (((x) >> 8) & 0xff)
137#define IMM32_L2(x) (((x) >> 16) & 0xff)
138#define IMM32_L3(x) (((x) >> 24) & 0xff)
139
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200140STATIC void asm_thumb_write_word32(asm_thumb_t *as, int w32) {
Damien429d7192013-10-04 19:53:11 +0100141 byte *c = asm_thumb_get_cur_to_write_bytes(as, 4);
142 c[0] = IMM32_L0(w32);
143 c[1] = IMM32_L1(w32);
144 c[2] = IMM32_L2(w32);
145 c[3] = IMM32_L3(w32);
146}
147*/
148
149// rlolist is a bit map indicating desired lo-registers
150#define OP_PUSH_RLIST(rlolist) (0xb400 | (rlolist))
151#define OP_PUSH_RLIST_LR(rlolist) (0xb400 | 0x0100 | (rlolist))
152#define OP_POP_RLIST(rlolist) (0xbc00 | (rlolist))
153#define OP_POP_RLIST_PC(rlolist) (0xbc00 | 0x0100 | (rlolist))
154
155#define OP_ADD_SP(num_words) (0xb000 | (num_words))
156#define OP_SUB_SP(num_words) (0xb080 | (num_words))
157
Damien Georged509ac22014-05-07 23:27:45 +0100158// locals:
159// - stored on the stack in ascending order
Damien George851f15f2014-09-29 10:05:32 +0100160// - numbered 0 through num_locals-1
Damien Georged509ac22014-05-07 23:27:45 +0100161// - SP points to first local
162//
163// | SP
164// v
165// l0 l1 l2 ... l(n-1)
166// ^ ^
167// | low address | high address in RAM
168
Damien429d7192013-10-04 19:53:11 +0100169void asm_thumb_entry(asm_thumb_t *as, int num_locals) {
Damien Georged509ac22014-05-07 23:27:45 +0100170 // work out what to push and how many extra spaces to reserve on stack
Damien429d7192013-10-04 19:53:11 +0100171 // so that we have enough for all locals and it's aligned an 8-byte boundary
Damien Georged509ac22014-05-07 23:27:45 +0100172 // we push extra regs (r1, r2, r3) to help do the stack adjustment
173 // we probably should just always subtract from sp, since this would be more efficient
174 // for push rlist, lowest numbered register at the lowest address
Damien429d7192013-10-04 19:53:11 +0100175 uint reglist;
176 uint stack_adjust;
177 if (num_locals < 0) {
178 num_locals = 0;
179 }
Damien Georged509ac22014-05-07 23:27:45 +0100180 // don't pop r0 because it's used for return value
Damien429d7192013-10-04 19:53:11 +0100181 switch (num_locals) {
182 case 0:
183 reglist = 0xf2;
184 stack_adjust = 0;
185 break;
186
187 case 1:
188 reglist = 0xf2;
189 stack_adjust = 0;
190 break;
191
192 case 2:
193 reglist = 0xfe;
194 stack_adjust = 0;
195 break;
196
197 case 3:
198 reglist = 0xfe;
199 stack_adjust = 0;
200 break;
201
202 default:
203 reglist = 0xfe;
204 stack_adjust = ((num_locals - 3) + 1) & (~1);
205 break;
206 }
Damien George90edf9e2014-04-18 16:56:54 +0100207 asm_thumb_op16(as, OP_PUSH_RLIST_LR(reglist));
Damien429d7192013-10-04 19:53:11 +0100208 if (stack_adjust > 0) {
Damien George90edf9e2014-04-18 16:56:54 +0100209 asm_thumb_op16(as, OP_SUB_SP(stack_adjust));
Damien429d7192013-10-04 19:53:11 +0100210 }
211 as->push_reglist = reglist;
212 as->stack_adjust = stack_adjust;
Damien429d7192013-10-04 19:53:11 +0100213}
214
215void asm_thumb_exit(asm_thumb_t *as) {
216 if (as->stack_adjust > 0) {
Damien George90edf9e2014-04-18 16:56:54 +0100217 asm_thumb_op16(as, OP_ADD_SP(as->stack_adjust));
Damien429d7192013-10-04 19:53:11 +0100218 }
Damien George90edf9e2014-04-18 16:56:54 +0100219 asm_thumb_op16(as, OP_POP_RLIST_PC(as->push_reglist));
Damien429d7192013-10-04 19:53:11 +0100220}
221
Damien George6f355fd2014-04-10 14:11:31 +0100222void asm_thumb_label_assign(asm_thumb_t *as, uint label) {
Damien5bfb7592013-10-05 18:41:24 +0100223 assert(label < as->max_num_labels);
Damien George36db6bc2014-05-07 17:24:22 +0100224 if (as->pass < ASM_THUMB_PASS_EMIT) {
Damien5bfb7592013-10-05 18:41:24 +0100225 // assign label offset
226 assert(as->label_offsets[label] == -1);
227 as->label_offsets[label] = as->code_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100228 } else {
229 // ensure label offset has not changed from PASS_COMPUTE to PASS_EMIT
Damien5bfb7592013-10-05 18:41:24 +0100230 //printf("l%d: (at %d=%ld)\n", label, as->label_offsets[label], as->code_offset);
231 assert(as->label_offsets[label] == as->code_offset);
Damien429d7192013-10-04 19:53:11 +0100232 }
233}
234
Damien Georgee5f8a772014-04-21 13:33:15 +0100235void asm_thumb_align(asm_thumb_t* as, uint align) {
236 // TODO fill unused data with NOPs?
237 as->code_offset = (as->code_offset + align - 1) & (~(align - 1));
238}
239
240void asm_thumb_data(asm_thumb_t* as, uint bytesize, uint val) {
241 byte *c = asm_thumb_get_cur_to_write_bytes(as, bytesize);
Damien George95977712014-05-10 18:07:08 +0100242 // only write to the buffer in the emit pass (otherwise we overflow dummy_data)
243 if (as->pass == ASM_THUMB_PASS_EMIT) {
244 // little endian
245 for (uint i = 0; i < bytesize; i++) {
246 *c++ = val;
247 val >>= 8;
248 }
Damien Georgee5f8a772014-04-21 13:33:15 +0100249 }
250}
251
Damien George851f15f2014-09-29 10:05:32 +0100252STATIC mp_uint_t get_label_dest(asm_thumb_t *as, uint label) {
Damien5bfb7592013-10-05 18:41:24 +0100253 assert(label < as->max_num_labels);
254 return as->label_offsets[label];
255}
256
Damien George90edf9e2014-04-18 16:56:54 +0100257void asm_thumb_op16(asm_thumb_t *as, uint op) {
258 byte *c = asm_thumb_get_cur_to_write_bytes(as, 2);
259 // little endian
260 c[0] = op;
261 c[1] = op >> 8;
262}
263
264void asm_thumb_op32(asm_thumb_t *as, uint op1, uint op2) {
265 byte *c = asm_thumb_get_cur_to_write_bytes(as, 4);
266 // little endian, op1 then op2
267 c[0] = op1;
268 c[1] = op1 >> 8;
269 c[2] = op2;
270 c[3] = op2 >> 8;
271}
272
Damien George87210872014-04-13 00:30:32 +0100273#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 +0100274
Damien George87210872014-04-13 00:30:32 +0100275void asm_thumb_format_2(asm_thumb_t *as, uint op, uint rlo_dest, uint rlo_src, int src_b) {
Damien George0b610de2014-09-29 16:25:04 +0100276 assert(rlo_dest < ASM_THUMB_REG_R8);
277 assert(rlo_src < ASM_THUMB_REG_R8);
Damien George90edf9e2014-04-18 16:56:54 +0100278 asm_thumb_op16(as, OP_FORMAT_2(op, rlo_dest, rlo_src, src_b));
Damien George87210872014-04-13 00:30:32 +0100279}
280
281#define OP_FORMAT_3(op, rlo, i8) ((op) | ((rlo) << 8) | (i8))
282
283void asm_thumb_format_3(asm_thumb_t *as, uint op, uint rlo, int i8) {
Damien George0b610de2014-09-29 16:25:04 +0100284 assert(rlo < ASM_THUMB_REG_R8);
Damien George90edf9e2014-04-18 16:56:54 +0100285 asm_thumb_op16(as, OP_FORMAT_3(op, rlo, i8));
Damien George87210872014-04-13 00:30:32 +0100286}
287
288#define OP_FORMAT_4(op, rlo_dest, rlo_src) ((op) | ((rlo_src) << 3) | (rlo_dest))
289
290void asm_thumb_format_4(asm_thumb_t *as, uint op, uint rlo_dest, uint rlo_src) {
Damien George0b610de2014-09-29 16:25:04 +0100291 assert(rlo_dest < ASM_THUMB_REG_R8);
292 assert(rlo_src < ASM_THUMB_REG_R8);
Damien George90edf9e2014-04-18 16:56:54 +0100293 asm_thumb_op16(as, OP_FORMAT_4(op, rlo_dest, rlo_src));
Damien George87210872014-04-13 00:30:32 +0100294}
295
296#define OP_FORMAT_9_10(op, rlo_dest, rlo_base, offset) ((op) | (((offset) << 6) & 0x07c0) | ((rlo_base) << 3) | (rlo_dest))
297
298void 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 +0100299 asm_thumb_op16(as, OP_FORMAT_9_10(op, rlo_dest, rlo_base, offset));
Damien George87210872014-04-13 00:30:32 +0100300}
301
302void asm_thumb_mov_reg_reg(asm_thumb_t *as, uint reg_dest, uint reg_src) {
303 uint op_lo;
304 if (reg_src < 8) {
305 op_lo = reg_src << 3;
306 } else {
307 op_lo = 0x40 | ((reg_src - 8) << 3);
308 }
309 if (reg_dest < 8) {
310 op_lo |= reg_dest;
311 } else {
312 op_lo |= 0x80 | (reg_dest - 8);
313 }
314 // mov reg_dest, reg_src
Damien George90edf9e2014-04-18 16:56:54 +0100315 asm_thumb_op16(as, 0x4600 | op_lo);
Damien429d7192013-10-04 19:53:11 +0100316}
317
Damien826005c2013-10-05 23:17:28 +0100318#define OP_MOVW (0xf240)
319#define OP_MOVT (0xf2c0)
320
321// if loading lo half with movw, the i16 value will be zero extended into the r32 register!
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200322STATIC void asm_thumb_mov_reg_i16(asm_thumb_t *as, uint mov_op, uint reg_dest, int i16_src) {
Damien George0b610de2014-09-29 16:25:04 +0100323 assert(reg_dest < ASM_THUMB_REG_R15);
Damien826005c2013-10-05 23:17:28 +0100324 // mov[wt] reg_dest, #i16_src
Damien George90edf9e2014-04-18 16:56:54 +0100325 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 +0100326}
327
Damien826005c2013-10-05 23:17:28 +0100328// the i16_src value will be zero extended into the r32 register!
329void asm_thumb_movw_reg_i16(asm_thumb_t *as, uint reg_dest, int i16_src) {
330 asm_thumb_mov_reg_i16(as, OP_MOVW, reg_dest, i16_src);
Damien429d7192013-10-04 19:53:11 +0100331}
332
Damien826005c2013-10-05 23:17:28 +0100333// the i16_src value will be zero extended into the r32 register!
334void asm_thumb_movt_reg_i16(asm_thumb_t *as, uint reg_dest, int i16_src) {
335 asm_thumb_mov_reg_i16(as, OP_MOVT, reg_dest, i16_src);
Damien429d7192013-10-04 19:53:11 +0100336}
337
Damien03d41242013-10-06 00:36:05 +0100338#define OP_B_N(byte_offset) (0xe000 | (((byte_offset) >> 1) & 0x07ff))
339
Damien George6f355fd2014-04-10 14:11:31 +0100340void asm_thumb_b_n(asm_thumb_t *as, uint label) {
Damien George851f15f2014-09-29 10:05:32 +0100341 mp_uint_t dest = get_label_dest(as, label);
342 mp_int_t rel = dest - as->code_offset;
Damien03d41242013-10-06 00:36:05 +0100343 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
344 if (SIGNED_FIT12(rel)) {
Damien George90edf9e2014-04-18 16:56:54 +0100345 asm_thumb_op16(as, OP_B_N(rel));
Damien03d41242013-10-06 00:36:05 +0100346 } else {
347 printf("asm_thumb_b_n: branch does not fit in 12 bits\n");
348 }
349}
350
Damien1a6633a2013-11-03 13:58:19 +0000351#define OP_BCC_N(cond, byte_offset) (0xd000 | ((cond) << 8) | (((byte_offset) >> 1) & 0x00ff))
Damien826005c2013-10-05 23:17:28 +0100352
Damien George6f355fd2014-04-10 14:11:31 +0100353void asm_thumb_bcc_n(asm_thumb_t *as, int cond, uint label) {
Damien George851f15f2014-09-29 10:05:32 +0100354 mp_uint_t dest = get_label_dest(as, label);
355 mp_int_t rel = dest - as->code_offset;
Damien826005c2013-10-05 23:17:28 +0100356 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
357 if (SIGNED_FIT9(rel)) {
Damien George90edf9e2014-04-18 16:56:54 +0100358 asm_thumb_op16(as, OP_BCC_N(cond, rel));
Damien826005c2013-10-05 23:17:28 +0100359 } else {
Damien1a6633a2013-11-03 13:58:19 +0000360 printf("asm_thumb_bcc_n: branch does not fit in 9 bits\n");
Damien826005c2013-10-05 23:17:28 +0100361 }
362}
363
Damien George40f3c022014-07-03 13:25:24 +0100364void asm_thumb_mov_reg_i32(asm_thumb_t *as, uint reg_dest, mp_uint_t i32) {
Damien826005c2013-10-05 23:17:28 +0100365 // movw, movt does it in 8 bytes
366 // ldr [pc, #], dw does it in 6 bytes, but we might not reach to end of code for dw
367
368 asm_thumb_mov_reg_i16(as, OP_MOVW, reg_dest, i32);
369 asm_thumb_mov_reg_i16(as, OP_MOVT, reg_dest, i32 >> 16);
370}
371
372void asm_thumb_mov_reg_i32_optimised(asm_thumb_t *as, uint reg_dest, int i32) {
373 if (reg_dest < 8 && UNSIGNED_FIT8(i32)) {
Damien George87210872014-04-13 00:30:32 +0100374 asm_thumb_mov_rlo_i8(as, reg_dest, i32);
Damien826005c2013-10-05 23:17:28 +0100375 } else if (UNSIGNED_FIT16(i32)) {
376 asm_thumb_mov_reg_i16(as, OP_MOVW, reg_dest, i32);
377 } else {
378 asm_thumb_mov_reg_i32(as, reg_dest, i32);
379 }
380}
381
Damien George36db6bc2014-05-07 17:24:22 +0100382// i32 is stored as a full word in the code, and aligned to machine-word boundary
383// TODO this is very inefficient, improve it!
384void asm_thumb_mov_reg_i32_aligned(asm_thumb_t *as, uint reg_dest, int i32) {
385 // align on machine-word + 2
386 if ((as->code_offset & 3) == 0) {
387 asm_thumb_op16(as, ASM_THUMB_OP_NOP);
388 }
389 // jump over the i32 value (instruction prefect adds 4 to PC)
390 asm_thumb_op16(as, OP_B_N(0));
391 // store i32 on machine-word aligned boundary
392 asm_thumb_data(as, 4, i32);
393 // do the actual load of the i32 value
394 asm_thumb_mov_reg_i32_optimised(as, reg_dest, i32);
395}
396
Damien429d7192013-10-04 19:53:11 +0100397#define OP_STR_TO_SP_OFFSET(rlo_dest, word_offset) (0x9000 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
398#define OP_LDR_FROM_SP_OFFSET(rlo_dest, word_offset) (0x9800 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
399
400void asm_thumb_mov_local_reg(asm_thumb_t *as, int local_num, uint rlo_src) {
Damien George0b610de2014-09-29 16:25:04 +0100401 assert(rlo_src < ASM_THUMB_REG_R8);
Damien Georged509ac22014-05-07 23:27:45 +0100402 int word_offset = local_num;
Damien George36db6bc2014-05-07 17:24:22 +0100403 assert(as->pass < ASM_THUMB_PASS_EMIT || word_offset >= 0);
Damien George90edf9e2014-04-18 16:56:54 +0100404 asm_thumb_op16(as, OP_STR_TO_SP_OFFSET(rlo_src, word_offset));
Damien429d7192013-10-04 19:53:11 +0100405}
406
407void asm_thumb_mov_reg_local(asm_thumb_t *as, uint rlo_dest, int local_num) {
Damien George0b610de2014-09-29 16:25:04 +0100408 assert(rlo_dest < ASM_THUMB_REG_R8);
Damien Georged509ac22014-05-07 23:27:45 +0100409 int word_offset = local_num;
Damien George36db6bc2014-05-07 17:24:22 +0100410 assert(as->pass < ASM_THUMB_PASS_EMIT || word_offset >= 0);
Damien George90edf9e2014-04-18 16:56:54 +0100411 asm_thumb_op16(as, OP_LDR_FROM_SP_OFFSET(rlo_dest, word_offset));
Damien429d7192013-10-04 19:53:11 +0100412}
413
Damien9b9e9962013-11-03 14:25:43 +0000414#define OP_ADD_REG_SP_OFFSET(rlo_dest, word_offset) (0xa800 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
415
416void asm_thumb_mov_reg_local_addr(asm_thumb_t *as, uint rlo_dest, int local_num) {
Damien George0b610de2014-09-29 16:25:04 +0100417 assert(rlo_dest < ASM_THUMB_REG_R8);
Damien Georged509ac22014-05-07 23:27:45 +0100418 int word_offset = local_num;
Damien George36db6bc2014-05-07 17:24:22 +0100419 assert(as->pass < ASM_THUMB_PASS_EMIT || word_offset >= 0);
Damien George90edf9e2014-04-18 16:56:54 +0100420 asm_thumb_op16(as, OP_ADD_REG_SP_OFFSET(rlo_dest, word_offset));
Damien429d7192013-10-04 19:53:11 +0100421}
422
Damien429d7192013-10-04 19:53:11 +0100423// this could be wrong, because it should have a range of +/- 16MiB...
424#define OP_BW_HI(byte_offset) (0xf000 | (((byte_offset) >> 12) & 0x07ff))
425#define OP_BW_LO(byte_offset) (0xb800 | (((byte_offset) >> 1) & 0x07ff))
426
Damien George6f355fd2014-04-10 14:11:31 +0100427void asm_thumb_b_label(asm_thumb_t *as, uint label) {
Damien George851f15f2014-09-29 10:05:32 +0100428 mp_uint_t dest = get_label_dest(as, label);
429 mp_int_t rel = dest - as->code_offset;
Damien5bfb7592013-10-05 18:41:24 +0100430 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
Damien George0b610de2014-09-29 16:25:04 +0100431 if (dest != -1 && rel <= -4) {
Damien5bfb7592013-10-05 18:41:24 +0100432 // is a backwards jump, so we know the size of the jump on the first pass
433 // calculate rel assuming 12 bit relative jump
434 if (SIGNED_FIT12(rel)) {
Damien George90edf9e2014-04-18 16:56:54 +0100435 asm_thumb_op16(as, OP_B_N(rel));
Damien429d7192013-10-04 19:53:11 +0100436 } else {
Damien5bfb7592013-10-05 18:41:24 +0100437 goto large_jump;
Damien429d7192013-10-04 19:53:11 +0100438 }
Damien5bfb7592013-10-05 18:41:24 +0100439 } else {
440 // is a forwards jump, so need to assume it's large
441 large_jump:
Damien George90edf9e2014-04-18 16:56:54 +0100442 asm_thumb_op32(as, OP_BW_HI(rel), OP_BW_LO(rel));
Damien429d7192013-10-04 19:53:11 +0100443 }
444}
445
Damien429d7192013-10-04 19:53:11 +0100446// all these bit arithmetics need coverage testing!
Damien1a6633a2013-11-03 13:58:19 +0000447#define OP_BCC_W_HI(cond, byte_offset) (0xf000 | ((cond) << 6) | (((byte_offset) >> 10) & 0x0400) | (((byte_offset) >> 14) & 0x003f))
448#define OP_BCC_W_LO(byte_offset) (0x8000 | ((byte_offset) & 0x2000) | (((byte_offset) >> 1) & 0x0fff))
Damien429d7192013-10-04 19:53:11 +0100449
Damien George6f355fd2014-04-10 14:11:31 +0100450void asm_thumb_bcc_label(asm_thumb_t *as, int cond, uint label) {
Damien George851f15f2014-09-29 10:05:32 +0100451 mp_uint_t dest = get_label_dest(as, label);
452 mp_int_t rel = dest - as->code_offset;
Damien5bfb7592013-10-05 18:41:24 +0100453 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
Damien George0b610de2014-09-29 16:25:04 +0100454 if (dest != -1 && rel <= -4) {
Damien5bfb7592013-10-05 18:41:24 +0100455 // is a backwards jump, so we know the size of the jump on the first pass
Damien826005c2013-10-05 23:17:28 +0100456 // calculate rel assuming 9 bit relative jump
Damien5bfb7592013-10-05 18:41:24 +0100457 if (SIGNED_FIT9(rel)) {
Damien George90edf9e2014-04-18 16:56:54 +0100458 asm_thumb_op16(as, OP_BCC_N(cond, rel));
Damien429d7192013-10-04 19:53:11 +0100459 } else {
Damien5bfb7592013-10-05 18:41:24 +0100460 goto large_jump;
Damien429d7192013-10-04 19:53:11 +0100461 }
Damien5bfb7592013-10-05 18:41:24 +0100462 } else {
463 // is a forwards jump, so need to assume it's large
464 large_jump:
Damien George90edf9e2014-04-18 16:56:54 +0100465 asm_thumb_op32(as, OP_BCC_W_HI(cond, rel), OP_BCC_W_LO(rel));
Damien429d7192013-10-04 19:53:11 +0100466 }
467}
468
469#define OP_BLX(reg) (0x4780 | ((reg) << 3))
470#define OP_SVC(arg) (0xdf00 | (arg))
Damien429d7192013-10-04 19:53:11 +0100471
472void asm_thumb_bl_ind(asm_thumb_t *as, void *fun_ptr, uint fun_id, uint reg_temp) {
473 /* TODO make this use less bytes
Damien George0b610de2014-09-29 16:25:04 +0100474 uint rlo_base = ASM_THUMB_REG_R3;
475 uint rlo_dest = ASM_THUMB_REG_R7;
Damien429d7192013-10-04 19:53:11 +0100476 uint word_offset = 4;
Damien George90edf9e2014-04-18 16:56:54 +0100477 asm_thumb_op16(as, 0x0000);
478 asm_thumb_op16(as, 0x6800 | (word_offset << 6) | (rlo_base << 3) | rlo_dest); // ldr rlo_dest, [rlo_base, #offset]
Damien George0b610de2014-09-29 16:25:04 +0100479 asm_thumb_op16(as, 0x4780 | (ASM_THUMB_REG_R9 << 3)); // blx reg
Damien429d7192013-10-04 19:53:11 +0100480 */
481
Damien George7fe21912014-08-16 22:31:57 +0100482 if (fun_id < 32) {
483 // load ptr to function from table, indexed by fun_id (must be in range 0-31); 4 bytes
Damien George0b610de2014-09-29 16:25:04 +0100484 asm_thumb_op16(as, OP_FORMAT_9_10(ASM_THUMB_FORMAT_9_LDR | ASM_THUMB_FORMAT_9_WORD_TRANSFER, reg_temp, ASM_THUMB_REG_R7, fun_id));
Damien George90edf9e2014-04-18 16:56:54 +0100485 asm_thumb_op16(as, OP_BLX(reg_temp));
Damien429d7192013-10-04 19:53:11 +0100486 } else {
Damien George7fe21912014-08-16 22:31:57 +0100487 // load ptr to function into register using immediate; 6 bytes
488 asm_thumb_mov_reg_i32(as, reg_temp, (mp_uint_t)fun_ptr);
489 asm_thumb_op16(as, OP_BLX(reg_temp));
Damien429d7192013-10-04 19:53:11 +0100490 }
491}
Damien Georgee67ed5d2014-01-04 13:55:24 +0000492
493#endif // MICROPY_EMIT_THUMB || MICROPY_EMIT_INLINE_THUMB