blob: 5bf2d80bb4073d1cac36d30d30c42be6bae770bc [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) {
91 // could check labels are resolved...
Damien429d7192013-10-04 19:53:11 +010092}
93
94// all functions must go through this one to emit bytes
Damien George95977712014-05-10 18:07:08 +010095// 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 +020096STATIC byte *asm_thumb_get_cur_to_write_bytes(asm_thumb_t *as, int num_bytes_to_write) {
Damien429d7192013-10-04 19:53:11 +010097 //printf("emit %d\n", num_bytes_to_write);
Damien George36db6bc2014-05-07 17:24:22 +010098 if (as->pass < ASM_THUMB_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +010099 as->code_offset += num_bytes_to_write;
100 return as->dummy_data;
101 } else {
102 assert(as->code_offset + num_bytes_to_write <= as->code_size);
103 byte *c = as->code_base + as->code_offset;
104 as->code_offset += num_bytes_to_write;
105 return c;
106 }
107}
108
109uint asm_thumb_get_code_size(asm_thumb_t *as) {
110 return as->code_size;
111}
112
113void *asm_thumb_get_code(asm_thumb_t *as) {
Damien George3c658a42014-08-24 16:28:17 +0100114 return as->code_base;
Damien429d7192013-10-04 19:53:11 +0100115}
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
Damien Georged509ac22014-05-07 23:27:45 +0100148// locals:
149// - stored on the stack in ascending order
Damien George851f15f2014-09-29 10:05:32 +0100150// - numbered 0 through num_locals-1
Damien Georged509ac22014-05-07 23:27:45 +0100151// - SP points to first local
152//
153// | SP
154// v
155// l0 l1 l2 ... l(n-1)
156// ^ ^
157// | low address | high address in RAM
158
Damien429d7192013-10-04 19:53:11 +0100159void asm_thumb_entry(asm_thumb_t *as, int num_locals) {
Damien Georged509ac22014-05-07 23:27:45 +0100160 // work out what to push and how many extra spaces to reserve on stack
Damien429d7192013-10-04 19:53:11 +0100161 // so that we have enough for all locals and it's aligned an 8-byte boundary
Damien Georged509ac22014-05-07 23:27:45 +0100162 // we push extra regs (r1, r2, r3) to help do the stack adjustment
163 // we probably should just always subtract from sp, since this would be more efficient
164 // for push rlist, lowest numbered register at the lowest address
Damien429d7192013-10-04 19:53:11 +0100165 uint reglist;
166 uint stack_adjust;
167 if (num_locals < 0) {
168 num_locals = 0;
169 }
Damien Georged509ac22014-05-07 23:27:45 +0100170 // don't pop r0 because it's used for return value
Damien429d7192013-10-04 19:53:11 +0100171 switch (num_locals) {
172 case 0:
173 reglist = 0xf2;
174 stack_adjust = 0;
175 break;
176
177 case 1:
178 reglist = 0xf2;
179 stack_adjust = 0;
180 break;
181
182 case 2:
183 reglist = 0xfe;
184 stack_adjust = 0;
185 break;
186
187 case 3:
188 reglist = 0xfe;
189 stack_adjust = 0;
190 break;
191
192 default:
193 reglist = 0xfe;
194 stack_adjust = ((num_locals - 3) + 1) & (~1);
195 break;
196 }
Damien George90edf9e2014-04-18 16:56:54 +0100197 asm_thumb_op16(as, OP_PUSH_RLIST_LR(reglist));
Damien429d7192013-10-04 19:53:11 +0100198 if (stack_adjust > 0) {
Damien George90edf9e2014-04-18 16:56:54 +0100199 asm_thumb_op16(as, OP_SUB_SP(stack_adjust));
Damien429d7192013-10-04 19:53:11 +0100200 }
201 as->push_reglist = reglist;
202 as->stack_adjust = stack_adjust;
Damien429d7192013-10-04 19:53:11 +0100203}
204
205void asm_thumb_exit(asm_thumb_t *as) {
206 if (as->stack_adjust > 0) {
Damien George90edf9e2014-04-18 16:56:54 +0100207 asm_thumb_op16(as, OP_ADD_SP(as->stack_adjust));
Damien429d7192013-10-04 19:53:11 +0100208 }
Damien George90edf9e2014-04-18 16:56:54 +0100209 asm_thumb_op16(as, OP_POP_RLIST_PC(as->push_reglist));
Damien429d7192013-10-04 19:53:11 +0100210}
211
Damien George6f355fd2014-04-10 14:11:31 +0100212void asm_thumb_label_assign(asm_thumb_t *as, uint label) {
Damien5bfb7592013-10-05 18:41:24 +0100213 assert(label < as->max_num_labels);
Damien George36db6bc2014-05-07 17:24:22 +0100214 if (as->pass < ASM_THUMB_PASS_EMIT) {
Damien5bfb7592013-10-05 18:41:24 +0100215 // assign label offset
216 assert(as->label_offsets[label] == -1);
217 as->label_offsets[label] = as->code_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100218 } else {
219 // ensure label offset has not changed from PASS_COMPUTE to PASS_EMIT
Damien5bfb7592013-10-05 18:41:24 +0100220 //printf("l%d: (at %d=%ld)\n", label, as->label_offsets[label], as->code_offset);
221 assert(as->label_offsets[label] == as->code_offset);
Damien429d7192013-10-04 19:53:11 +0100222 }
223}
224
Damien Georgee5f8a772014-04-21 13:33:15 +0100225void asm_thumb_align(asm_thumb_t* as, uint align) {
226 // TODO fill unused data with NOPs?
227 as->code_offset = (as->code_offset + align - 1) & (~(align - 1));
228}
229
230void asm_thumb_data(asm_thumb_t* as, uint bytesize, uint val) {
231 byte *c = asm_thumb_get_cur_to_write_bytes(as, bytesize);
Damien George95977712014-05-10 18:07:08 +0100232 // only write to the buffer in the emit pass (otherwise we overflow dummy_data)
233 if (as->pass == ASM_THUMB_PASS_EMIT) {
234 // little endian
235 for (uint i = 0; i < bytesize; i++) {
236 *c++ = val;
237 val >>= 8;
238 }
Damien Georgee5f8a772014-04-21 13:33:15 +0100239 }
240}
241
Damien George851f15f2014-09-29 10:05:32 +0100242STATIC mp_uint_t get_label_dest(asm_thumb_t *as, uint label) {
Damien5bfb7592013-10-05 18:41:24 +0100243 assert(label < as->max_num_labels);
244 return as->label_offsets[label];
245}
246
Damien George90edf9e2014-04-18 16:56:54 +0100247void asm_thumb_op16(asm_thumb_t *as, uint op) {
248 byte *c = asm_thumb_get_cur_to_write_bytes(as, 2);
249 // little endian
250 c[0] = op;
251 c[1] = op >> 8;
252}
253
254void asm_thumb_op32(asm_thumb_t *as, uint op1, uint op2) {
255 byte *c = asm_thumb_get_cur_to_write_bytes(as, 4);
256 // little endian, op1 then op2
257 c[0] = op1;
258 c[1] = op1 >> 8;
259 c[2] = op2;
260 c[3] = op2 >> 8;
261}
262
Damien George87210872014-04-13 00:30:32 +0100263#define OP_FORMAT_4(op, rlo_dest, rlo_src) ((op) | ((rlo_src) << 3) | (rlo_dest))
264
265void asm_thumb_format_4(asm_thumb_t *as, uint op, uint rlo_dest, uint rlo_src) {
Damien George0b610de2014-09-29 16:25:04 +0100266 assert(rlo_dest < ASM_THUMB_REG_R8);
267 assert(rlo_src < ASM_THUMB_REG_R8);
Damien George90edf9e2014-04-18 16:56:54 +0100268 asm_thumb_op16(as, OP_FORMAT_4(op, rlo_dest, rlo_src));
Damien George87210872014-04-13 00:30:32 +0100269}
270
Damien George87210872014-04-13 00:30:32 +0100271void asm_thumb_mov_reg_reg(asm_thumb_t *as, uint reg_dest, uint reg_src) {
272 uint op_lo;
273 if (reg_src < 8) {
274 op_lo = reg_src << 3;
275 } else {
276 op_lo = 0x40 | ((reg_src - 8) << 3);
277 }
278 if (reg_dest < 8) {
279 op_lo |= reg_dest;
280 } else {
281 op_lo |= 0x80 | (reg_dest - 8);
282 }
283 // mov reg_dest, reg_src
Damien George90edf9e2014-04-18 16:56:54 +0100284 asm_thumb_op16(as, 0x4600 | op_lo);
Damien429d7192013-10-04 19:53:11 +0100285}
286
Damien826005c2013-10-05 23:17:28 +0100287// if loading lo half with movw, the i16 value will be zero extended into the r32 register!
Damien Georgee41b21c2015-02-24 16:32:52 +0000288void 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 +0100289 assert(reg_dest < ASM_THUMB_REG_R15);
Damien826005c2013-10-05 23:17:28 +0100290 // mov[wt] reg_dest, #i16_src
Damien George90edf9e2014-04-18 16:56:54 +0100291 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 +0100292}
293
Damien03d41242013-10-06 00:36:05 +0100294#define OP_B_N(byte_offset) (0xe000 | (((byte_offset) >> 1) & 0x07ff))
295
Damien George53457432015-02-25 15:45:55 +0000296bool asm_thumb_b_n_label(asm_thumb_t *as, uint label) {
Damien George851f15f2014-09-29 10:05:32 +0100297 mp_uint_t dest = get_label_dest(as, label);
298 mp_int_t rel = dest - as->code_offset;
Damien03d41242013-10-06 00:36:05 +0100299 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
Damien George67c5f892015-03-02 17:51:32 +0000300 asm_thumb_op16(as, OP_B_N(rel));
301 return as->pass != ASM_THUMB_PASS_EMIT || SIGNED_FIT12(rel);
Damien03d41242013-10-06 00:36:05 +0100302}
303
Damien1a6633a2013-11-03 13:58:19 +0000304#define OP_BCC_N(cond, byte_offset) (0xd000 | ((cond) << 8) | (((byte_offset) >> 1) & 0x00ff))
Damien826005c2013-10-05 23:17:28 +0100305
Damien George9f142f02015-03-02 14:29:52 +0000306// all these bit arithmetics need coverage testing!
307#define OP_BCC_W_HI(cond, byte_offset) (0xf000 | ((cond) << 6) | (((byte_offset) >> 10) & 0x0400) | (((byte_offset) >> 14) & 0x003f))
308#define OP_BCC_W_LO(byte_offset) (0x8000 | ((byte_offset) & 0x2000) | (((byte_offset) >> 1) & 0x0fff))
309
310bool asm_thumb_bcc_nw_label(asm_thumb_t *as, int cond, uint label, bool wide) {
Damien George851f15f2014-09-29 10:05:32 +0100311 mp_uint_t dest = get_label_dest(as, label);
312 mp_int_t rel = dest - as->code_offset;
Damien826005c2013-10-05 23:17:28 +0100313 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
Damien George9f142f02015-03-02 14:29:52 +0000314 if (!wide) {
Damien George67c5f892015-03-02 17:51:32 +0000315 asm_thumb_op16(as, OP_BCC_N(cond, rel));
316 return as->pass != ASM_THUMB_PASS_EMIT || SIGNED_FIT9(rel);
Damien826005c2013-10-05 23:17:28 +0100317 } else {
Damien George9f142f02015-03-02 14:29:52 +0000318 asm_thumb_op32(as, OP_BCC_W_HI(cond, rel), OP_BCC_W_LO(rel));
319 return true;
Damien George53457432015-02-25 15:45:55 +0000320 }
321}
322
323#define OP_BL_HI(byte_offset) (0xf000 | (((byte_offset) >> 12) & 0x07ff))
324#define OP_BL_LO(byte_offset) (0xf800 | (((byte_offset) >> 1) & 0x07ff))
325
326bool asm_thumb_bl_label(asm_thumb_t *as, uint label) {
327 mp_uint_t dest = get_label_dest(as, label);
328 mp_int_t rel = dest - as->code_offset;
329 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
Damien George67c5f892015-03-02 17:51:32 +0000330 asm_thumb_op32(as, OP_BL_HI(rel), OP_BL_LO(rel));
331 return as->pass != ASM_THUMB_PASS_EMIT || SIGNED_FIT23(rel);
Damien826005c2013-10-05 23:17:28 +0100332}
333
Damien George40f3c022014-07-03 13:25:24 +0100334void asm_thumb_mov_reg_i32(asm_thumb_t *as, uint reg_dest, mp_uint_t i32) {
Damien826005c2013-10-05 23:17:28 +0100335 // movw, movt does it in 8 bytes
336 // ldr [pc, #], dw does it in 6 bytes, but we might not reach to end of code for dw
337
Damien Georgee41b21c2015-02-24 16:32:52 +0000338 asm_thumb_mov_reg_i16(as, ASM_THUMB_OP_MOVW, reg_dest, i32);
339 asm_thumb_mov_reg_i16(as, ASM_THUMB_OP_MOVT, reg_dest, i32 >> 16);
Damien826005c2013-10-05 23:17:28 +0100340}
341
342void asm_thumb_mov_reg_i32_optimised(asm_thumb_t *as, uint reg_dest, int i32) {
343 if (reg_dest < 8 && UNSIGNED_FIT8(i32)) {
Damien George87210872014-04-13 00:30:32 +0100344 asm_thumb_mov_rlo_i8(as, reg_dest, i32);
Damien826005c2013-10-05 23:17:28 +0100345 } else if (UNSIGNED_FIT16(i32)) {
Damien Georgee41b21c2015-02-24 16:32:52 +0000346 asm_thumb_mov_reg_i16(as, ASM_THUMB_OP_MOVW, reg_dest, i32);
Damien826005c2013-10-05 23:17:28 +0100347 } else {
348 asm_thumb_mov_reg_i32(as, reg_dest, i32);
349 }
350}
351
Damien George36db6bc2014-05-07 17:24:22 +0100352// i32 is stored as a full word in the code, and aligned to machine-word boundary
353// TODO this is very inefficient, improve it!
354void asm_thumb_mov_reg_i32_aligned(asm_thumb_t *as, uint reg_dest, int i32) {
355 // align on machine-word + 2
356 if ((as->code_offset & 3) == 0) {
357 asm_thumb_op16(as, ASM_THUMB_OP_NOP);
358 }
Damien Georged1c37882015-02-15 00:45:28 +0000359 // jump over the i32 value (instruction prefetch adds 2 to PC)
360 asm_thumb_op16(as, OP_B_N(2));
Damien George36db6bc2014-05-07 17:24:22 +0100361 // store i32 on machine-word aligned boundary
362 asm_thumb_data(as, 4, i32);
363 // do the actual load of the i32 value
364 asm_thumb_mov_reg_i32_optimised(as, reg_dest, i32);
365}
366
Damien429d7192013-10-04 19:53:11 +0100367#define OP_STR_TO_SP_OFFSET(rlo_dest, word_offset) (0x9000 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
368#define OP_LDR_FROM_SP_OFFSET(rlo_dest, word_offset) (0x9800 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
369
370void asm_thumb_mov_local_reg(asm_thumb_t *as, int local_num, uint rlo_src) {
Damien George0b610de2014-09-29 16:25:04 +0100371 assert(rlo_src < ASM_THUMB_REG_R8);
Damien Georged509ac22014-05-07 23:27:45 +0100372 int word_offset = local_num;
Damien George36db6bc2014-05-07 17:24:22 +0100373 assert(as->pass < ASM_THUMB_PASS_EMIT || word_offset >= 0);
Damien George90edf9e2014-04-18 16:56:54 +0100374 asm_thumb_op16(as, OP_STR_TO_SP_OFFSET(rlo_src, word_offset));
Damien429d7192013-10-04 19:53:11 +0100375}
376
377void asm_thumb_mov_reg_local(asm_thumb_t *as, uint rlo_dest, int local_num) {
Damien George0b610de2014-09-29 16:25:04 +0100378 assert(rlo_dest < ASM_THUMB_REG_R8);
Damien Georged509ac22014-05-07 23:27:45 +0100379 int word_offset = local_num;
Damien George36db6bc2014-05-07 17:24:22 +0100380 assert(as->pass < ASM_THUMB_PASS_EMIT || word_offset >= 0);
Damien George90edf9e2014-04-18 16:56:54 +0100381 asm_thumb_op16(as, OP_LDR_FROM_SP_OFFSET(rlo_dest, word_offset));
Damien429d7192013-10-04 19:53:11 +0100382}
383
Damien9b9e9962013-11-03 14:25:43 +0000384#define OP_ADD_REG_SP_OFFSET(rlo_dest, word_offset) (0xa800 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
385
386void asm_thumb_mov_reg_local_addr(asm_thumb_t *as, uint rlo_dest, int local_num) {
Damien George0b610de2014-09-29 16:25:04 +0100387 assert(rlo_dest < ASM_THUMB_REG_R8);
Damien Georged509ac22014-05-07 23:27:45 +0100388 int word_offset = local_num;
Damien George36db6bc2014-05-07 17:24:22 +0100389 assert(as->pass < ASM_THUMB_PASS_EMIT || word_offset >= 0);
Damien George90edf9e2014-04-18 16:56:54 +0100390 asm_thumb_op16(as, OP_ADD_REG_SP_OFFSET(rlo_dest, word_offset));
Damien429d7192013-10-04 19:53:11 +0100391}
392
Damien429d7192013-10-04 19:53:11 +0100393// this could be wrong, because it should have a range of +/- 16MiB...
394#define OP_BW_HI(byte_offset) (0xf000 | (((byte_offset) >> 12) & 0x07ff))
395#define OP_BW_LO(byte_offset) (0xb800 | (((byte_offset) >> 1) & 0x07ff))
396
Damien George6f355fd2014-04-10 14:11:31 +0100397void asm_thumb_b_label(asm_thumb_t *as, uint label) {
Damien George851f15f2014-09-29 10:05:32 +0100398 mp_uint_t dest = get_label_dest(as, label);
399 mp_int_t rel = dest - as->code_offset;
Damien5bfb7592013-10-05 18:41:24 +0100400 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
Damien George0b610de2014-09-29 16:25:04 +0100401 if (dest != -1 && rel <= -4) {
Damien5bfb7592013-10-05 18:41:24 +0100402 // is a backwards jump, so we know the size of the jump on the first pass
403 // calculate rel assuming 12 bit relative jump
404 if (SIGNED_FIT12(rel)) {
Damien George90edf9e2014-04-18 16:56:54 +0100405 asm_thumb_op16(as, OP_B_N(rel));
Damien429d7192013-10-04 19:53:11 +0100406 } else {
Damien5bfb7592013-10-05 18:41:24 +0100407 goto large_jump;
Damien429d7192013-10-04 19:53:11 +0100408 }
Damien5bfb7592013-10-05 18:41:24 +0100409 } else {
410 // is a forwards jump, so need to assume it's large
411 large_jump:
Damien George90edf9e2014-04-18 16:56:54 +0100412 asm_thumb_op32(as, OP_BW_HI(rel), OP_BW_LO(rel));
Damien429d7192013-10-04 19:53:11 +0100413 }
414}
415
Damien George6f355fd2014-04-10 14:11:31 +0100416void asm_thumb_bcc_label(asm_thumb_t *as, int cond, uint label) {
Damien George851f15f2014-09-29 10:05:32 +0100417 mp_uint_t dest = get_label_dest(as, label);
418 mp_int_t rel = dest - as->code_offset;
Damien5bfb7592013-10-05 18:41:24 +0100419 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
Damien George0b610de2014-09-29 16:25:04 +0100420 if (dest != -1 && rel <= -4) {
Damien5bfb7592013-10-05 18:41:24 +0100421 // is a backwards jump, so we know the size of the jump on the first pass
Damien826005c2013-10-05 23:17:28 +0100422 // calculate rel assuming 9 bit relative jump
Damien5bfb7592013-10-05 18:41:24 +0100423 if (SIGNED_FIT9(rel)) {
Damien George90edf9e2014-04-18 16:56:54 +0100424 asm_thumb_op16(as, OP_BCC_N(cond, rel));
Damien429d7192013-10-04 19:53:11 +0100425 } else {
Damien5bfb7592013-10-05 18:41:24 +0100426 goto large_jump;
Damien429d7192013-10-04 19:53:11 +0100427 }
Damien5bfb7592013-10-05 18:41:24 +0100428 } else {
429 // is a forwards jump, so need to assume it's large
430 large_jump:
Damien George90edf9e2014-04-18 16:56:54 +0100431 asm_thumb_op32(as, OP_BCC_W_HI(cond, rel), OP_BCC_W_LO(rel));
Damien429d7192013-10-04 19:53:11 +0100432 }
433}
434
435#define OP_BLX(reg) (0x4780 | ((reg) << 3))
436#define OP_SVC(arg) (0xdf00 | (arg))
Damien429d7192013-10-04 19:53:11 +0100437
438void asm_thumb_bl_ind(asm_thumb_t *as, void *fun_ptr, uint fun_id, uint reg_temp) {
439 /* TODO make this use less bytes
Damien George0b610de2014-09-29 16:25:04 +0100440 uint rlo_base = ASM_THUMB_REG_R3;
441 uint rlo_dest = ASM_THUMB_REG_R7;
Damien429d7192013-10-04 19:53:11 +0100442 uint word_offset = 4;
Damien George90edf9e2014-04-18 16:56:54 +0100443 asm_thumb_op16(as, 0x0000);
444 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 +0100445 asm_thumb_op16(as, 0x4780 | (ASM_THUMB_REG_R9 << 3)); // blx reg
Damien429d7192013-10-04 19:53:11 +0100446 */
447
Damien George7fe21912014-08-16 22:31:57 +0100448 if (fun_id < 32) {
449 // 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 +0000450 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 +0100451 asm_thumb_op16(as, OP_BLX(reg_temp));
Damien429d7192013-10-04 19:53:11 +0100452 } else {
Damien George7fe21912014-08-16 22:31:57 +0100453 // load ptr to function into register using immediate; 6 bytes
454 asm_thumb_mov_reg_i32(as, reg_temp, (mp_uint_t)fun_ptr);
455 asm_thumb_op16(as, OP_BLX(reg_temp));
Damien429d7192013-10-04 19:53:11 +0100456 }
457}
Damien Georgee67ed5d2014-01-04 13:55:24 +0000458
459#endif // MICROPY_EMIT_THUMB || MICROPY_EMIT_INLINE_THUMB