blob: 8341c958e9368971d3022fa738a5afd898d50ebf [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
Damien George51dfcb42015-01-01 20:27:54 +000031#include "py/mpconfig.h"
Damien429d7192013-10-04 19:53:11 +010032
Damien Georgee67ed5d2014-01-04 13:55:24 +000033// wrapper around everything in this file
34#if MICROPY_EMIT_THUMB || MICROPY_EMIT_INLINE_THUMB
35
Damien George51dfcb42015-01-01 20:27:54 +000036#include "py/asmthumb.h"
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)
Damien Georgeeff10f62015-02-16 18:17:07 +000043#define SIGNED_FIT23(x) (((x) & 0xffc00000) == 0) || (((x) & 0xffc00000) == 0xffc00000)
Damien429d7192013-10-04 19:53:11 +010044
45struct _asm_thumb_t {
Damien George851f15f2014-09-29 10:05:32 +010046 mp_uint_t pass;
47 mp_uint_t code_offset;
48 mp_uint_t code_size;
Damien429d7192013-10-04 19:53:11 +010049 byte *code_base;
Damien George95977712014-05-10 18:07:08 +010050 byte dummy_data[4];
Damien429d7192013-10-04 19:53:11 +010051
Damien George851f15f2014-09-29 10:05:32 +010052 mp_uint_t max_num_labels;
53 mp_uint_t *label_offsets;
54 mp_uint_t push_reglist;
55 mp_uint_t stack_adjust;
Damien429d7192013-10-04 19:53:11 +010056};
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;
Damien George851f15f2014-09-29 10:05:32 +010063 as->label_offsets = m_new(mp_uint_t, 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) {
Fabian Vogtb7235b82014-09-03 16:59:33 +020070 MP_PLAT_FREE_EXEC(as->code_base, as->code_size);
Damien429d7192013-10-04 19:53:11 +010071 }
Damien George0b610de2014-09-29 16:25:04 +010072 m_del(mp_uint_t, as->label_offsets, as->max_num_labels);
Damien732407f2013-12-29 19:33:23 +000073 m_del_obj(asm_thumb_t, as);
Damien429d7192013-10-04 19:53:11 +010074}
75
Damien George36db6bc2014-05-07 17:24:22 +010076void asm_thumb_start_pass(asm_thumb_t *as, uint pass) {
Damien George36db6bc2014-05-07 17:24:22 +010077 if (pass == ASM_THUMB_PASS_COMPUTE) {
Damien George851f15f2014-09-29 10:05:32 +010078 memset(as->label_offsets, -1, as->max_num_labels * sizeof(mp_uint_t));
Damien Georged9dc6ff2015-01-14 00:38:33 +000079 } else if (pass == ASM_THUMB_PASS_EMIT) {
80 MP_PLAT_ALLOC_EXEC(as->code_offset, (void**)&as->code_base, &as->code_size);
81 if (as->code_base == NULL) {
Fabian Vogtb7235b82014-09-03 16:59:33 +020082 assert(0);
83 }
Damien0446a0d2013-11-17 13:16:36 +000084 //printf("code_size: %u\n", as->code_size);
Damien429d7192013-10-04 19:53:11 +010085 }
Damien Georged9dc6ff2015-01-14 00:38:33 +000086 as->pass = pass;
87 as->code_offset = 0;
88}
Damien429d7192013-10-04 19:53:11 +010089
Damien Georged9dc6ff2015-01-14 00:38:33 +000090void asm_thumb_end_pass(asm_thumb_t *as) {
Damien George723d5982015-11-09 14:11:21 +000091 (void)as;
Damien Georged9dc6ff2015-01-14 00:38:33 +000092 // could check labels are resolved...
Damien429d7192013-10-04 19:53:11 +010093}
94
95// all functions must go through this one to emit bytes
Damien George95977712014-05-10 18:07:08 +010096// 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 +020097STATIC byte *asm_thumb_get_cur_to_write_bytes(asm_thumb_t *as, int num_bytes_to_write) {
Damien429d7192013-10-04 19:53:11 +010098 //printf("emit %d\n", num_bytes_to_write);
Damien George36db6bc2014-05-07 17:24:22 +010099 if (as->pass < ASM_THUMB_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100100 as->code_offset += num_bytes_to_write;
101 return as->dummy_data;
102 } else {
103 assert(as->code_offset + num_bytes_to_write <= as->code_size);
104 byte *c = as->code_base + as->code_offset;
105 as->code_offset += num_bytes_to_write;
106 return c;
107 }
108}
109
Damien George99886182015-04-06 22:38:53 +0100110uint asm_thumb_get_code_pos(asm_thumb_t *as) {
111 return as->code_offset;
112}
113
Damien429d7192013-10-04 19:53:11 +0100114uint asm_thumb_get_code_size(asm_thumb_t *as) {
115 return as->code_size;
116}
117
118void *asm_thumb_get_code(asm_thumb_t *as) {
Damien George3c658a42014-08-24 16:28:17 +0100119 return as->code_base;
Damien429d7192013-10-04 19:53:11 +0100120}
121
122/*
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200123STATIC void asm_thumb_write_byte_1(asm_thumb_t *as, byte b1) {
Damien429d7192013-10-04 19:53:11 +0100124 byte *c = asm_thumb_get_cur_to_write_bytes(as, 1);
125 c[0] = b1;
126}
127*/
128
Damien429d7192013-10-04 19:53:11 +0100129/*
130#define IMM32_L0(x) ((x) & 0xff)
131#define IMM32_L1(x) (((x) >> 8) & 0xff)
132#define IMM32_L2(x) (((x) >> 16) & 0xff)
133#define IMM32_L3(x) (((x) >> 24) & 0xff)
134
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200135STATIC void asm_thumb_write_word32(asm_thumb_t *as, int w32) {
Damien429d7192013-10-04 19:53:11 +0100136 byte *c = asm_thumb_get_cur_to_write_bytes(as, 4);
137 c[0] = IMM32_L0(w32);
138 c[1] = IMM32_L1(w32);
139 c[2] = IMM32_L2(w32);
140 c[3] = IMM32_L3(w32);
141}
142*/
143
144// rlolist is a bit map indicating desired lo-registers
145#define OP_PUSH_RLIST(rlolist) (0xb400 | (rlolist))
146#define OP_PUSH_RLIST_LR(rlolist) (0xb400 | 0x0100 | (rlolist))
147#define OP_POP_RLIST(rlolist) (0xbc00 | (rlolist))
148#define OP_POP_RLIST_PC(rlolist) (0xbc00 | 0x0100 | (rlolist))
149
150#define OP_ADD_SP(num_words) (0xb000 | (num_words))
151#define OP_SUB_SP(num_words) (0xb080 | (num_words))
152
Damien Georged509ac22014-05-07 23:27:45 +0100153// locals:
154// - stored on the stack in ascending order
Damien George851f15f2014-09-29 10:05:32 +0100155// - numbered 0 through num_locals-1
Damien Georged509ac22014-05-07 23:27:45 +0100156// - SP points to first local
157//
158// | SP
159// v
160// l0 l1 l2 ... l(n-1)
161// ^ ^
162// | low address | high address in RAM
163
Damien429d7192013-10-04 19:53:11 +0100164void asm_thumb_entry(asm_thumb_t *as, int num_locals) {
Damien Georged509ac22014-05-07 23:27:45 +0100165 // work out what to push and how many extra spaces to reserve on stack
Damien429d7192013-10-04 19:53:11 +0100166 // so that we have enough for all locals and it's aligned an 8-byte boundary
Damien Georged509ac22014-05-07 23:27:45 +0100167 // we push extra regs (r1, r2, r3) to help do the stack adjustment
168 // we probably should just always subtract from sp, since this would be more efficient
169 // for push rlist, lowest numbered register at the lowest address
Damien429d7192013-10-04 19:53:11 +0100170 uint reglist;
171 uint stack_adjust;
172 if (num_locals < 0) {
173 num_locals = 0;
174 }
Damien Georged509ac22014-05-07 23:27:45 +0100175 // don't pop r0 because it's used for return value
Damien429d7192013-10-04 19:53:11 +0100176 switch (num_locals) {
177 case 0:
178 reglist = 0xf2;
179 stack_adjust = 0;
180 break;
181
182 case 1:
183 reglist = 0xf2;
184 stack_adjust = 0;
185 break;
186
187 case 2:
188 reglist = 0xfe;
189 stack_adjust = 0;
190 break;
191
192 case 3:
193 reglist = 0xfe;
194 stack_adjust = 0;
195 break;
196
197 default:
198 reglist = 0xfe;
199 stack_adjust = ((num_locals - 3) + 1) & (~1);
200 break;
201 }
Damien George90edf9e2014-04-18 16:56:54 +0100202 asm_thumb_op16(as, OP_PUSH_RLIST_LR(reglist));
Damien429d7192013-10-04 19:53:11 +0100203 if (stack_adjust > 0) {
Damien George90edf9e2014-04-18 16:56:54 +0100204 asm_thumb_op16(as, OP_SUB_SP(stack_adjust));
Damien429d7192013-10-04 19:53:11 +0100205 }
206 as->push_reglist = reglist;
207 as->stack_adjust = stack_adjust;
Damien429d7192013-10-04 19:53:11 +0100208}
209
210void asm_thumb_exit(asm_thumb_t *as) {
211 if (as->stack_adjust > 0) {
Damien George90edf9e2014-04-18 16:56:54 +0100212 asm_thumb_op16(as, OP_ADD_SP(as->stack_adjust));
Damien429d7192013-10-04 19:53:11 +0100213 }
Damien George90edf9e2014-04-18 16:56:54 +0100214 asm_thumb_op16(as, OP_POP_RLIST_PC(as->push_reglist));
Damien429d7192013-10-04 19:53:11 +0100215}
216
Damien George6f355fd2014-04-10 14:11:31 +0100217void asm_thumb_label_assign(asm_thumb_t *as, uint label) {
Damien5bfb7592013-10-05 18:41:24 +0100218 assert(label < as->max_num_labels);
Damien George36db6bc2014-05-07 17:24:22 +0100219 if (as->pass < ASM_THUMB_PASS_EMIT) {
Damien5bfb7592013-10-05 18:41:24 +0100220 // assign label offset
221 assert(as->label_offsets[label] == -1);
222 as->label_offsets[label] = as->code_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100223 } else {
224 // ensure label offset has not changed from PASS_COMPUTE to PASS_EMIT
Damien5bfb7592013-10-05 18:41:24 +0100225 //printf("l%d: (at %d=%ld)\n", label, as->label_offsets[label], as->code_offset);
226 assert(as->label_offsets[label] == as->code_offset);
Damien429d7192013-10-04 19:53:11 +0100227 }
228}
229
Damien Georgee5f8a772014-04-21 13:33:15 +0100230void asm_thumb_align(asm_thumb_t* as, uint align) {
231 // TODO fill unused data with NOPs?
232 as->code_offset = (as->code_offset + align - 1) & (~(align - 1));
233}
234
235void asm_thumb_data(asm_thumb_t* as, uint bytesize, uint val) {
236 byte *c = asm_thumb_get_cur_to_write_bytes(as, bytesize);
Damien George95977712014-05-10 18:07:08 +0100237 // only write to the buffer in the emit pass (otherwise we overflow dummy_data)
238 if (as->pass == ASM_THUMB_PASS_EMIT) {
239 // little endian
240 for (uint i = 0; i < bytesize; i++) {
241 *c++ = val;
242 val >>= 8;
243 }
Damien Georgee5f8a772014-04-21 13:33:15 +0100244 }
245}
246
Damien George851f15f2014-09-29 10:05:32 +0100247STATIC mp_uint_t 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_4(op, rlo_dest, rlo_src) ((op) | ((rlo_src) << 3) | (rlo_dest))
269
270void asm_thumb_format_4(asm_thumb_t *as, uint op, uint rlo_dest, uint rlo_src) {
Damien George0b610de2014-09-29 16:25:04 +0100271 assert(rlo_dest < ASM_THUMB_REG_R8);
272 assert(rlo_src < ASM_THUMB_REG_R8);
Damien George90edf9e2014-04-18 16:56:54 +0100273 asm_thumb_op16(as, OP_FORMAT_4(op, rlo_dest, rlo_src));
Damien George87210872014-04-13 00:30:32 +0100274}
275
Damien George87210872014-04-13 00:30:32 +0100276void 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// if loading lo half with movw, the i16 value will be zero extended into the r32 register!
Damien Georgee41b21c2015-02-24 16:32:52 +0000293void 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 +0100294 assert(reg_dest < ASM_THUMB_REG_R15);
Damien826005c2013-10-05 23:17:28 +0100295 // mov[wt] reg_dest, #i16_src
Damien George90edf9e2014-04-18 16:56:54 +0100296 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 +0100297}
298
Damien03d41242013-10-06 00:36:05 +0100299#define OP_B_N(byte_offset) (0xe000 | (((byte_offset) >> 1) & 0x07ff))
300
Damien George53457432015-02-25 15:45:55 +0000301bool asm_thumb_b_n_label(asm_thumb_t *as, uint label) {
Damien George851f15f2014-09-29 10:05:32 +0100302 mp_uint_t dest = get_label_dest(as, label);
303 mp_int_t rel = dest - as->code_offset;
Damien03d41242013-10-06 00:36:05 +0100304 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
Damien George67c5f892015-03-02 17:51:32 +0000305 asm_thumb_op16(as, OP_B_N(rel));
306 return as->pass != ASM_THUMB_PASS_EMIT || SIGNED_FIT12(rel);
Damien03d41242013-10-06 00:36:05 +0100307}
308
Damien1a6633a2013-11-03 13:58:19 +0000309#define OP_BCC_N(cond, byte_offset) (0xd000 | ((cond) << 8) | (((byte_offset) >> 1) & 0x00ff))
Damien826005c2013-10-05 23:17:28 +0100310
Damien George9f142f02015-03-02 14:29:52 +0000311// all these bit arithmetics need coverage testing!
312#define OP_BCC_W_HI(cond, byte_offset) (0xf000 | ((cond) << 6) | (((byte_offset) >> 10) & 0x0400) | (((byte_offset) >> 14) & 0x003f))
313#define OP_BCC_W_LO(byte_offset) (0x8000 | ((byte_offset) & 0x2000) | (((byte_offset) >> 1) & 0x0fff))
314
315bool asm_thumb_bcc_nw_label(asm_thumb_t *as, int cond, uint label, bool wide) {
Damien George851f15f2014-09-29 10:05:32 +0100316 mp_uint_t dest = get_label_dest(as, label);
317 mp_int_t rel = dest - as->code_offset;
Damien826005c2013-10-05 23:17:28 +0100318 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
Damien George9f142f02015-03-02 14:29:52 +0000319 if (!wide) {
Damien George67c5f892015-03-02 17:51:32 +0000320 asm_thumb_op16(as, OP_BCC_N(cond, rel));
321 return as->pass != ASM_THUMB_PASS_EMIT || SIGNED_FIT9(rel);
Damien826005c2013-10-05 23:17:28 +0100322 } else {
Damien George9f142f02015-03-02 14:29:52 +0000323 asm_thumb_op32(as, OP_BCC_W_HI(cond, rel), OP_BCC_W_LO(rel));
324 return true;
Damien George53457432015-02-25 15:45:55 +0000325 }
326}
327
328#define OP_BL_HI(byte_offset) (0xf000 | (((byte_offset) >> 12) & 0x07ff))
329#define OP_BL_LO(byte_offset) (0xf800 | (((byte_offset) >> 1) & 0x07ff))
330
331bool asm_thumb_bl_label(asm_thumb_t *as, uint label) {
332 mp_uint_t dest = get_label_dest(as, label);
333 mp_int_t rel = dest - as->code_offset;
334 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
Damien George67c5f892015-03-02 17:51:32 +0000335 asm_thumb_op32(as, OP_BL_HI(rel), OP_BL_LO(rel));
336 return as->pass != ASM_THUMB_PASS_EMIT || SIGNED_FIT23(rel);
Damien826005c2013-10-05 23:17:28 +0100337}
338
Damien George40f3c022014-07-03 13:25:24 +0100339void asm_thumb_mov_reg_i32(asm_thumb_t *as, uint reg_dest, mp_uint_t i32) {
Damien826005c2013-10-05 23:17:28 +0100340 // movw, movt does it in 8 bytes
341 // ldr [pc, #], dw does it in 6 bytes, but we might not reach to end of code for dw
342
Damien Georgee41b21c2015-02-24 16:32:52 +0000343 asm_thumb_mov_reg_i16(as, ASM_THUMB_OP_MOVW, reg_dest, i32);
344 asm_thumb_mov_reg_i16(as, ASM_THUMB_OP_MOVT, reg_dest, i32 >> 16);
Damien826005c2013-10-05 23:17:28 +0100345}
346
347void asm_thumb_mov_reg_i32_optimised(asm_thumb_t *as, uint reg_dest, int i32) {
348 if (reg_dest < 8 && UNSIGNED_FIT8(i32)) {
Damien George87210872014-04-13 00:30:32 +0100349 asm_thumb_mov_rlo_i8(as, reg_dest, i32);
Damien826005c2013-10-05 23:17:28 +0100350 } else if (UNSIGNED_FIT16(i32)) {
Damien Georgee41b21c2015-02-24 16:32:52 +0000351 asm_thumb_mov_reg_i16(as, ASM_THUMB_OP_MOVW, reg_dest, i32);
Damien826005c2013-10-05 23:17:28 +0100352 } else {
353 asm_thumb_mov_reg_i32(as, reg_dest, i32);
354 }
355}
356
Damien George36db6bc2014-05-07 17:24:22 +0100357// i32 is stored as a full word in the code, and aligned to machine-word boundary
358// TODO this is very inefficient, improve it!
359void asm_thumb_mov_reg_i32_aligned(asm_thumb_t *as, uint reg_dest, int i32) {
360 // align on machine-word + 2
361 if ((as->code_offset & 3) == 0) {
362 asm_thumb_op16(as, ASM_THUMB_OP_NOP);
363 }
Damien Georged1c37882015-02-15 00:45:28 +0000364 // jump over the i32 value (instruction prefetch adds 2 to PC)
365 asm_thumb_op16(as, OP_B_N(2));
Damien George36db6bc2014-05-07 17:24:22 +0100366 // store i32 on machine-word aligned boundary
367 asm_thumb_data(as, 4, i32);
368 // do the actual load of the i32 value
369 asm_thumb_mov_reg_i32_optimised(as, reg_dest, i32);
370}
371
Damien429d7192013-10-04 19:53:11 +0100372#define OP_STR_TO_SP_OFFSET(rlo_dest, word_offset) (0x9000 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
373#define OP_LDR_FROM_SP_OFFSET(rlo_dest, word_offset) (0x9800 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
374
375void asm_thumb_mov_local_reg(asm_thumb_t *as, int local_num, uint rlo_src) {
Damien George0b610de2014-09-29 16:25:04 +0100376 assert(rlo_src < ASM_THUMB_REG_R8);
Damien Georged509ac22014-05-07 23:27:45 +0100377 int word_offset = local_num;
Damien George36db6bc2014-05-07 17:24:22 +0100378 assert(as->pass < ASM_THUMB_PASS_EMIT || word_offset >= 0);
Damien George90edf9e2014-04-18 16:56:54 +0100379 asm_thumb_op16(as, OP_STR_TO_SP_OFFSET(rlo_src, word_offset));
Damien429d7192013-10-04 19:53:11 +0100380}
381
382void asm_thumb_mov_reg_local(asm_thumb_t *as, uint rlo_dest, int local_num) {
Damien George0b610de2014-09-29 16:25:04 +0100383 assert(rlo_dest < ASM_THUMB_REG_R8);
Damien Georged509ac22014-05-07 23:27:45 +0100384 int word_offset = local_num;
Damien George36db6bc2014-05-07 17:24:22 +0100385 assert(as->pass < ASM_THUMB_PASS_EMIT || word_offset >= 0);
Damien George90edf9e2014-04-18 16:56:54 +0100386 asm_thumb_op16(as, OP_LDR_FROM_SP_OFFSET(rlo_dest, word_offset));
Damien429d7192013-10-04 19:53:11 +0100387}
388
Damien9b9e9962013-11-03 14:25:43 +0000389#define OP_ADD_REG_SP_OFFSET(rlo_dest, word_offset) (0xa800 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
390
391void asm_thumb_mov_reg_local_addr(asm_thumb_t *as, uint rlo_dest, int local_num) {
Damien George0b610de2014-09-29 16:25:04 +0100392 assert(rlo_dest < ASM_THUMB_REG_R8);
Damien Georged509ac22014-05-07 23:27:45 +0100393 int word_offset = local_num;
Damien George36db6bc2014-05-07 17:24:22 +0100394 assert(as->pass < ASM_THUMB_PASS_EMIT || word_offset >= 0);
Damien George90edf9e2014-04-18 16:56:54 +0100395 asm_thumb_op16(as, OP_ADD_REG_SP_OFFSET(rlo_dest, word_offset));
Damien429d7192013-10-04 19:53:11 +0100396}
397
Damien429d7192013-10-04 19:53:11 +0100398// this could be wrong, because it should have a range of +/- 16MiB...
399#define OP_BW_HI(byte_offset) (0xf000 | (((byte_offset) >> 12) & 0x07ff))
400#define OP_BW_LO(byte_offset) (0xb800 | (((byte_offset) >> 1) & 0x07ff))
401
Damien George6f355fd2014-04-10 14:11:31 +0100402void asm_thumb_b_label(asm_thumb_t *as, uint label) {
Damien George851f15f2014-09-29 10:05:32 +0100403 mp_uint_t dest = get_label_dest(as, label);
404 mp_int_t rel = dest - as->code_offset;
Damien5bfb7592013-10-05 18:41:24 +0100405 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
Damien George723d5982015-11-09 14:11:21 +0000406 if (dest != (mp_uint_t)-1 && rel <= -4) {
Damien5bfb7592013-10-05 18:41:24 +0100407 // is a backwards jump, so we know the size of the jump on the first pass
408 // calculate rel assuming 12 bit relative jump
409 if (SIGNED_FIT12(rel)) {
Damien George90edf9e2014-04-18 16:56:54 +0100410 asm_thumb_op16(as, OP_B_N(rel));
Damien429d7192013-10-04 19:53:11 +0100411 } else {
Damien5bfb7592013-10-05 18:41:24 +0100412 goto large_jump;
Damien429d7192013-10-04 19:53:11 +0100413 }
Damien5bfb7592013-10-05 18:41:24 +0100414 } else {
415 // is a forwards jump, so need to assume it's large
416 large_jump:
Damien George90edf9e2014-04-18 16:56:54 +0100417 asm_thumb_op32(as, OP_BW_HI(rel), OP_BW_LO(rel));
Damien429d7192013-10-04 19:53:11 +0100418 }
419}
420
Damien George6f355fd2014-04-10 14:11:31 +0100421void asm_thumb_bcc_label(asm_thumb_t *as, int cond, uint label) {
Damien George851f15f2014-09-29 10:05:32 +0100422 mp_uint_t dest = get_label_dest(as, label);
423 mp_int_t rel = dest - as->code_offset;
Damien5bfb7592013-10-05 18:41:24 +0100424 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
Damien George723d5982015-11-09 14:11:21 +0000425 if (dest != (mp_uint_t)-1 && rel <= -4) {
Damien5bfb7592013-10-05 18:41:24 +0100426 // is a backwards jump, so we know the size of the jump on the first pass
Damien826005c2013-10-05 23:17:28 +0100427 // calculate rel assuming 9 bit relative jump
Damien5bfb7592013-10-05 18:41:24 +0100428 if (SIGNED_FIT9(rel)) {
Damien George90edf9e2014-04-18 16:56:54 +0100429 asm_thumb_op16(as, OP_BCC_N(cond, rel));
Damien429d7192013-10-04 19:53:11 +0100430 } else {
Damien5bfb7592013-10-05 18:41:24 +0100431 goto large_jump;
Damien429d7192013-10-04 19:53:11 +0100432 }
Damien5bfb7592013-10-05 18:41:24 +0100433 } else {
434 // is a forwards jump, so need to assume it's large
435 large_jump:
Damien George90edf9e2014-04-18 16:56:54 +0100436 asm_thumb_op32(as, OP_BCC_W_HI(cond, rel), OP_BCC_W_LO(rel));
Damien429d7192013-10-04 19:53:11 +0100437 }
438}
439
440#define OP_BLX(reg) (0x4780 | ((reg) << 3))
441#define OP_SVC(arg) (0xdf00 | (arg))
Damien429d7192013-10-04 19:53:11 +0100442
443void asm_thumb_bl_ind(asm_thumb_t *as, void *fun_ptr, uint fun_id, uint reg_temp) {
444 /* TODO make this use less bytes
Damien George0b610de2014-09-29 16:25:04 +0100445 uint rlo_base = ASM_THUMB_REG_R3;
446 uint rlo_dest = ASM_THUMB_REG_R7;
Damien429d7192013-10-04 19:53:11 +0100447 uint word_offset = 4;
Damien George90edf9e2014-04-18 16:56:54 +0100448 asm_thumb_op16(as, 0x0000);
449 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 +0100450 asm_thumb_op16(as, 0x4780 | (ASM_THUMB_REG_R9 << 3)); // blx reg
Damien429d7192013-10-04 19:53:11 +0100451 */
452
Damien George7fe21912014-08-16 22:31:57 +0100453 if (fun_id < 32) {
454 // load ptr to function from table, indexed by fun_id (must be in range 0-31); 4 bytes
Damien Georgee41b21c2015-02-24 16:32:52 +0000455 asm_thumb_op16(as, ASM_THUMB_FORMAT_9_10_ENCODE(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 +0100456 asm_thumb_op16(as, OP_BLX(reg_temp));
Damien429d7192013-10-04 19:53:11 +0100457 } else {
Damien George7fe21912014-08-16 22:31:57 +0100458 // load ptr to function into register using immediate; 6 bytes
459 asm_thumb_mov_reg_i32(as, reg_temp, (mp_uint_t)fun_ptr);
460 asm_thumb_op16(as, OP_BLX(reg_temp));
Damien429d7192013-10-04 19:53:11 +0100461 }
462}
Damien Georgee67ed5d2014-01-04 13:55:24 +0000463
464#endif // MICROPY_EMIT_THUMB || MICROPY_EMIT_INLINE_THUMB