blob: 75ce168f73b2aaa0817a8c1e7330aa13f67646de [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 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;
Damien George95977712014-05-10 18:07:08 +010049 byte dummy_data[4];
Damien429d7192013-10-04 19:53:11 +010050
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
Damien George95977712014-05-10 18:07:08 +0100116// 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 +0200117STATIC byte *asm_thumb_get_cur_to_write_bytes(asm_thumb_t *as, int num_bytes_to_write) {
Damien429d7192013-10-04 19:53:11 +0100118 //printf("emit %d\n", num_bytes_to_write);
Damien George36db6bc2014-05-07 17:24:22 +0100119 if (as->pass < ASM_THUMB_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100120 as->code_offset += num_bytes_to_write;
121 return as->dummy_data;
122 } else {
123 assert(as->code_offset + num_bytes_to_write <= as->code_size);
124 byte *c = as->code_base + as->code_offset;
125 as->code_offset += num_bytes_to_write;
126 return c;
127 }
128}
129
130uint asm_thumb_get_code_size(asm_thumb_t *as) {
131 return as->code_size;
132}
133
134void *asm_thumb_get_code(asm_thumb_t *as) {
135 // need to set low bit to indicate that it's thumb code
Damien George40f3c022014-07-03 13:25:24 +0100136 return (void *)(((mp_uint_t)as->code_base) | 1);
Damien429d7192013-10-04 19:53:11 +0100137}
138
139/*
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200140STATIC void asm_thumb_write_byte_1(asm_thumb_t *as, byte b1) {
Damien429d7192013-10-04 19:53:11 +0100141 byte *c = asm_thumb_get_cur_to_write_bytes(as, 1);
142 c[0] = b1;
143}
144*/
145
Damien429d7192013-10-04 19:53:11 +0100146/*
147#define IMM32_L0(x) ((x) & 0xff)
148#define IMM32_L1(x) (((x) >> 8) & 0xff)
149#define IMM32_L2(x) (((x) >> 16) & 0xff)
150#define IMM32_L3(x) (((x) >> 24) & 0xff)
151
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200152STATIC void asm_thumb_write_word32(asm_thumb_t *as, int w32) {
Damien429d7192013-10-04 19:53:11 +0100153 byte *c = asm_thumb_get_cur_to_write_bytes(as, 4);
154 c[0] = IMM32_L0(w32);
155 c[1] = IMM32_L1(w32);
156 c[2] = IMM32_L2(w32);
157 c[3] = IMM32_L3(w32);
158}
159*/
160
161// rlolist is a bit map indicating desired lo-registers
162#define OP_PUSH_RLIST(rlolist) (0xb400 | (rlolist))
163#define OP_PUSH_RLIST_LR(rlolist) (0xb400 | 0x0100 | (rlolist))
164#define OP_POP_RLIST(rlolist) (0xbc00 | (rlolist))
165#define OP_POP_RLIST_PC(rlolist) (0xbc00 | 0x0100 | (rlolist))
166
167#define OP_ADD_SP(num_words) (0xb000 | (num_words))
168#define OP_SUB_SP(num_words) (0xb080 | (num_words))
169
Damien Georged509ac22014-05-07 23:27:45 +0100170// locals:
171// - stored on the stack in ascending order
172// - numbered 0 through as->num_locals-1
173// - SP points to first local
174//
175// | SP
176// v
177// l0 l1 l2 ... l(n-1)
178// ^ ^
179// | low address | high address in RAM
180
Damien429d7192013-10-04 19:53:11 +0100181void asm_thumb_entry(asm_thumb_t *as, int num_locals) {
Damien Georged509ac22014-05-07 23:27:45 +0100182 // work out what to push and how many extra spaces to reserve on stack
Damien429d7192013-10-04 19:53:11 +0100183 // so that we have enough for all locals and it's aligned an 8-byte boundary
Damien Georged509ac22014-05-07 23:27:45 +0100184 // we push extra regs (r1, r2, r3) to help do the stack adjustment
185 // we probably should just always subtract from sp, since this would be more efficient
186 // for push rlist, lowest numbered register at the lowest address
Damien429d7192013-10-04 19:53:11 +0100187 uint reglist;
188 uint stack_adjust;
189 if (num_locals < 0) {
190 num_locals = 0;
191 }
Damien Georged509ac22014-05-07 23:27:45 +0100192 // don't pop r0 because it's used for return value
Damien429d7192013-10-04 19:53:11 +0100193 switch (num_locals) {
194 case 0:
195 reglist = 0xf2;
196 stack_adjust = 0;
197 break;
198
199 case 1:
200 reglist = 0xf2;
201 stack_adjust = 0;
202 break;
203
204 case 2:
205 reglist = 0xfe;
206 stack_adjust = 0;
207 break;
208
209 case 3:
210 reglist = 0xfe;
211 stack_adjust = 0;
212 break;
213
214 default:
215 reglist = 0xfe;
216 stack_adjust = ((num_locals - 3) + 1) & (~1);
217 break;
218 }
Damien George90edf9e2014-04-18 16:56:54 +0100219 asm_thumb_op16(as, OP_PUSH_RLIST_LR(reglist));
Damien429d7192013-10-04 19:53:11 +0100220 if (stack_adjust > 0) {
Damien George90edf9e2014-04-18 16:56:54 +0100221 asm_thumb_op16(as, OP_SUB_SP(stack_adjust));
Damien429d7192013-10-04 19:53:11 +0100222 }
223 as->push_reglist = reglist;
224 as->stack_adjust = stack_adjust;
225 as->num_locals = num_locals;
226}
227
228void asm_thumb_exit(asm_thumb_t *as) {
229 if (as->stack_adjust > 0) {
Damien George90edf9e2014-04-18 16:56:54 +0100230 asm_thumb_op16(as, OP_ADD_SP(as->stack_adjust));
Damien429d7192013-10-04 19:53:11 +0100231 }
Damien George90edf9e2014-04-18 16:56:54 +0100232 asm_thumb_op16(as, OP_POP_RLIST_PC(as->push_reglist));
Damien429d7192013-10-04 19:53:11 +0100233}
234
Damien George6f355fd2014-04-10 14:11:31 +0100235void asm_thumb_label_assign(asm_thumb_t *as, uint label) {
Damien5bfb7592013-10-05 18:41:24 +0100236 assert(label < as->max_num_labels);
Damien George36db6bc2014-05-07 17:24:22 +0100237 if (as->pass < ASM_THUMB_PASS_EMIT) {
Damien5bfb7592013-10-05 18:41:24 +0100238 // assign label offset
239 assert(as->label_offsets[label] == -1);
240 as->label_offsets[label] = as->code_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100241 } else {
242 // ensure label offset has not changed from PASS_COMPUTE to PASS_EMIT
Damien5bfb7592013-10-05 18:41:24 +0100243 //printf("l%d: (at %d=%ld)\n", label, as->label_offsets[label], as->code_offset);
244 assert(as->label_offsets[label] == as->code_offset);
Damien429d7192013-10-04 19:53:11 +0100245 }
246}
247
Damien Georgee5f8a772014-04-21 13:33:15 +0100248void asm_thumb_align(asm_thumb_t* as, uint align) {
249 // TODO fill unused data with NOPs?
250 as->code_offset = (as->code_offset + align - 1) & (~(align - 1));
251}
252
253void asm_thumb_data(asm_thumb_t* as, uint bytesize, uint val) {
254 byte *c = asm_thumb_get_cur_to_write_bytes(as, bytesize);
Damien George95977712014-05-10 18:07:08 +0100255 // only write to the buffer in the emit pass (otherwise we overflow dummy_data)
256 if (as->pass == ASM_THUMB_PASS_EMIT) {
257 // little endian
258 for (uint i = 0; i < bytesize; i++) {
259 *c++ = val;
260 val >>= 8;
261 }
Damien Georgee5f8a772014-04-21 13:33:15 +0100262 }
263}
264
Damien George6f355fd2014-04-10 14:11:31 +0100265STATIC int get_label_dest(asm_thumb_t *as, uint label) {
Damien5bfb7592013-10-05 18:41:24 +0100266 assert(label < as->max_num_labels);
267 return as->label_offsets[label];
268}
269
Damien George90edf9e2014-04-18 16:56:54 +0100270void asm_thumb_op16(asm_thumb_t *as, uint op) {
271 byte *c = asm_thumb_get_cur_to_write_bytes(as, 2);
272 // little endian
273 c[0] = op;
274 c[1] = op >> 8;
275}
276
277void asm_thumb_op32(asm_thumb_t *as, uint op1, uint op2) {
278 byte *c = asm_thumb_get_cur_to_write_bytes(as, 4);
279 // little endian, op1 then op2
280 c[0] = op1;
281 c[1] = op1 >> 8;
282 c[2] = op2;
283 c[3] = op2 >> 8;
284}
285
Damien George87210872014-04-13 00:30:32 +0100286#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 +0100287
Damien George87210872014-04-13 00:30:32 +0100288void 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 +0100289 assert(rlo_dest < REG_R8);
Damien George87210872014-04-13 00:30:32 +0100290 assert(rlo_src < REG_R8);
Damien George90edf9e2014-04-18 16:56:54 +0100291 asm_thumb_op16(as, OP_FORMAT_2(op, rlo_dest, rlo_src, src_b));
Damien George87210872014-04-13 00:30:32 +0100292}
293
294#define OP_FORMAT_3(op, rlo, i8) ((op) | ((rlo) << 8) | (i8))
295
296void asm_thumb_format_3(asm_thumb_t *as, uint op, uint rlo, int i8) {
297 assert(rlo < REG_R8);
Damien George90edf9e2014-04-18 16:56:54 +0100298 asm_thumb_op16(as, OP_FORMAT_3(op, rlo, i8));
Damien George87210872014-04-13 00:30:32 +0100299}
300
301#define OP_FORMAT_4(op, rlo_dest, rlo_src) ((op) | ((rlo_src) << 3) | (rlo_dest))
302
303void asm_thumb_format_4(asm_thumb_t *as, uint op, uint rlo_dest, uint rlo_src) {
304 assert(rlo_dest < REG_R8);
305 assert(rlo_src < REG_R8);
Damien George90edf9e2014-04-18 16:56:54 +0100306 asm_thumb_op16(as, OP_FORMAT_4(op, rlo_dest, rlo_src));
Damien George87210872014-04-13 00:30:32 +0100307}
308
309#define OP_FORMAT_9_10(op, rlo_dest, rlo_base, offset) ((op) | (((offset) << 6) & 0x07c0) | ((rlo_base) << 3) | (rlo_dest))
310
311void 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 +0100312 asm_thumb_op16(as, OP_FORMAT_9_10(op, rlo_dest, rlo_base, offset));
Damien George87210872014-04-13 00:30:32 +0100313}
314
315void asm_thumb_mov_reg_reg(asm_thumb_t *as, uint reg_dest, uint reg_src) {
316 uint op_lo;
317 if (reg_src < 8) {
318 op_lo = reg_src << 3;
319 } else {
320 op_lo = 0x40 | ((reg_src - 8) << 3);
321 }
322 if (reg_dest < 8) {
323 op_lo |= reg_dest;
324 } else {
325 op_lo |= 0x80 | (reg_dest - 8);
326 }
327 // mov reg_dest, reg_src
Damien George90edf9e2014-04-18 16:56:54 +0100328 asm_thumb_op16(as, 0x4600 | op_lo);
Damien429d7192013-10-04 19:53:11 +0100329}
330
Damien826005c2013-10-05 23:17:28 +0100331#define OP_MOVW (0xf240)
332#define OP_MOVT (0xf2c0)
333
334// if loading lo half with movw, the i16 value will be zero extended into the r32 register!
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200335STATIC 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 +0100336 assert(reg_dest < REG_R15);
Damien826005c2013-10-05 23:17:28 +0100337 // mov[wt] reg_dest, #i16_src
Damien George90edf9e2014-04-18 16:56:54 +0100338 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 +0100339}
340
Damien826005c2013-10-05 23:17:28 +0100341// the i16_src value will be zero extended into the r32 register!
342void asm_thumb_movw_reg_i16(asm_thumb_t *as, uint reg_dest, int i16_src) {
343 asm_thumb_mov_reg_i16(as, OP_MOVW, reg_dest, i16_src);
Damien429d7192013-10-04 19:53:11 +0100344}
345
Damien826005c2013-10-05 23:17:28 +0100346// the i16_src value will be zero extended into the r32 register!
347void asm_thumb_movt_reg_i16(asm_thumb_t *as, uint reg_dest, int i16_src) {
348 asm_thumb_mov_reg_i16(as, OP_MOVT, reg_dest, i16_src);
Damien429d7192013-10-04 19:53:11 +0100349}
350
Damien George47e1b852014-04-08 18:28:33 +0100351void asm_thumb_ite_ge(asm_thumb_t *as) {
Damien George90edf9e2014-04-18 16:56:54 +0100352 asm_thumb_op16(as, 0xbfac);
Damien George47e1b852014-04-08 18:28:33 +0100353}
354
Damien03d41242013-10-06 00:36:05 +0100355#define OP_B_N(byte_offset) (0xe000 | (((byte_offset) >> 1) & 0x07ff))
356
Damien George6f355fd2014-04-10 14:11:31 +0100357void asm_thumb_b_n(asm_thumb_t *as, uint label) {
Damien03d41242013-10-06 00:36:05 +0100358 int dest = get_label_dest(as, label);
359 int rel = dest - as->code_offset;
360 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
361 if (SIGNED_FIT12(rel)) {
Damien George90edf9e2014-04-18 16:56:54 +0100362 asm_thumb_op16(as, OP_B_N(rel));
Damien03d41242013-10-06 00:36:05 +0100363 } else {
364 printf("asm_thumb_b_n: branch does not fit in 12 bits\n");
365 }
366}
367
Damien1a6633a2013-11-03 13:58:19 +0000368#define OP_BCC_N(cond, byte_offset) (0xd000 | ((cond) << 8) | (((byte_offset) >> 1) & 0x00ff))
Damien826005c2013-10-05 23:17:28 +0100369
Damien George6f355fd2014-04-10 14:11:31 +0100370void asm_thumb_bcc_n(asm_thumb_t *as, int cond, uint label) {
Damien826005c2013-10-05 23:17:28 +0100371 int dest = get_label_dest(as, label);
372 int rel = dest - as->code_offset;
373 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
374 if (SIGNED_FIT9(rel)) {
Damien George90edf9e2014-04-18 16:56:54 +0100375 asm_thumb_op16(as, OP_BCC_N(cond, rel));
Damien826005c2013-10-05 23:17:28 +0100376 } else {
Damien1a6633a2013-11-03 13:58:19 +0000377 printf("asm_thumb_bcc_n: branch does not fit in 9 bits\n");
Damien826005c2013-10-05 23:17:28 +0100378 }
379}
380
Damien George40f3c022014-07-03 13:25:24 +0100381void asm_thumb_mov_reg_i32(asm_thumb_t *as, uint reg_dest, mp_uint_t i32) {
Damien826005c2013-10-05 23:17:28 +0100382 // movw, movt does it in 8 bytes
383 // ldr [pc, #], dw does it in 6 bytes, but we might not reach to end of code for dw
384
385 asm_thumb_mov_reg_i16(as, OP_MOVW, reg_dest, i32);
386 asm_thumb_mov_reg_i16(as, OP_MOVT, reg_dest, i32 >> 16);
387}
388
389void asm_thumb_mov_reg_i32_optimised(asm_thumb_t *as, uint reg_dest, int i32) {
390 if (reg_dest < 8 && UNSIGNED_FIT8(i32)) {
Damien George87210872014-04-13 00:30:32 +0100391 asm_thumb_mov_rlo_i8(as, reg_dest, i32);
Damien826005c2013-10-05 23:17:28 +0100392 } else if (UNSIGNED_FIT16(i32)) {
393 asm_thumb_mov_reg_i16(as, OP_MOVW, reg_dest, i32);
394 } else {
395 asm_thumb_mov_reg_i32(as, reg_dest, i32);
396 }
397}
398
Damien George36db6bc2014-05-07 17:24:22 +0100399// i32 is stored as a full word in the code, and aligned to machine-word boundary
400// TODO this is very inefficient, improve it!
401void asm_thumb_mov_reg_i32_aligned(asm_thumb_t *as, uint reg_dest, int i32) {
402 // align on machine-word + 2
403 if ((as->code_offset & 3) == 0) {
404 asm_thumb_op16(as, ASM_THUMB_OP_NOP);
405 }
406 // jump over the i32 value (instruction prefect adds 4 to PC)
407 asm_thumb_op16(as, OP_B_N(0));
408 // store i32 on machine-word aligned boundary
409 asm_thumb_data(as, 4, i32);
410 // do the actual load of the i32 value
411 asm_thumb_mov_reg_i32_optimised(as, reg_dest, i32);
412}
413
Damien429d7192013-10-04 19:53:11 +0100414#define OP_STR_TO_SP_OFFSET(rlo_dest, word_offset) (0x9000 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
415#define OP_LDR_FROM_SP_OFFSET(rlo_dest, word_offset) (0x9800 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
416
417void asm_thumb_mov_local_reg(asm_thumb_t *as, int local_num, uint rlo_src) {
418 assert(rlo_src < REG_R8);
Damien Georged509ac22014-05-07 23:27:45 +0100419 int word_offset = local_num;
Damien George36db6bc2014-05-07 17:24:22 +0100420 assert(as->pass < ASM_THUMB_PASS_EMIT || word_offset >= 0);
Damien George90edf9e2014-04-18 16:56:54 +0100421 asm_thumb_op16(as, OP_STR_TO_SP_OFFSET(rlo_src, word_offset));
Damien429d7192013-10-04 19:53:11 +0100422}
423
424void asm_thumb_mov_reg_local(asm_thumb_t *as, uint rlo_dest, int local_num) {
425 assert(rlo_dest < REG_R8);
Damien Georged509ac22014-05-07 23:27:45 +0100426 int word_offset = local_num;
Damien George36db6bc2014-05-07 17:24:22 +0100427 assert(as->pass < ASM_THUMB_PASS_EMIT || word_offset >= 0);
Damien George90edf9e2014-04-18 16:56:54 +0100428 asm_thumb_op16(as, OP_LDR_FROM_SP_OFFSET(rlo_dest, word_offset));
Damien429d7192013-10-04 19:53:11 +0100429}
430
Damien9b9e9962013-11-03 14:25:43 +0000431#define OP_ADD_REG_SP_OFFSET(rlo_dest, word_offset) (0xa800 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
432
433void asm_thumb_mov_reg_local_addr(asm_thumb_t *as, uint rlo_dest, int local_num) {
434 assert(rlo_dest < REG_R8);
Damien Georged509ac22014-05-07 23:27:45 +0100435 int word_offset = local_num;
Damien George36db6bc2014-05-07 17:24:22 +0100436 assert(as->pass < ASM_THUMB_PASS_EMIT || word_offset >= 0);
Damien George90edf9e2014-04-18 16:56:54 +0100437 asm_thumb_op16(as, OP_ADD_REG_SP_OFFSET(rlo_dest, word_offset));
Damien429d7192013-10-04 19:53:11 +0100438}
439
Damien429d7192013-10-04 19:53:11 +0100440// this could be wrong, because it should have a range of +/- 16MiB...
441#define OP_BW_HI(byte_offset) (0xf000 | (((byte_offset) >> 12) & 0x07ff))
442#define OP_BW_LO(byte_offset) (0xb800 | (((byte_offset) >> 1) & 0x07ff))
443
Damien George6f355fd2014-04-10 14:11:31 +0100444void asm_thumb_b_label(asm_thumb_t *as, uint label) {
Damien5bfb7592013-10-05 18:41:24 +0100445 int dest = get_label_dest(as, label);
446 int rel = dest - as->code_offset;
447 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
448 if (dest >= 0 && rel <= -4) {
449 // is a backwards jump, so we know the size of the jump on the first pass
450 // calculate rel assuming 12 bit relative jump
451 if (SIGNED_FIT12(rel)) {
Damien George90edf9e2014-04-18 16:56:54 +0100452 asm_thumb_op16(as, OP_B_N(rel));
Damien429d7192013-10-04 19:53:11 +0100453 } else {
Damien5bfb7592013-10-05 18:41:24 +0100454 goto large_jump;
Damien429d7192013-10-04 19:53:11 +0100455 }
Damien5bfb7592013-10-05 18:41:24 +0100456 } else {
457 // is a forwards jump, so need to assume it's large
458 large_jump:
Damien George90edf9e2014-04-18 16:56:54 +0100459 asm_thumb_op32(as, OP_BW_HI(rel), OP_BW_LO(rel));
Damien429d7192013-10-04 19:53:11 +0100460 }
461}
462
Damien429d7192013-10-04 19:53:11 +0100463// all these bit arithmetics need coverage testing!
Damien1a6633a2013-11-03 13:58:19 +0000464#define OP_BCC_W_HI(cond, byte_offset) (0xf000 | ((cond) << 6) | (((byte_offset) >> 10) & 0x0400) | (((byte_offset) >> 14) & 0x003f))
465#define OP_BCC_W_LO(byte_offset) (0x8000 | ((byte_offset) & 0x2000) | (((byte_offset) >> 1) & 0x0fff))
Damien429d7192013-10-04 19:53:11 +0100466
Damien George6f355fd2014-04-10 14:11:31 +0100467void asm_thumb_bcc_label(asm_thumb_t *as, int cond, uint label) {
Damien5bfb7592013-10-05 18:41:24 +0100468 int dest = get_label_dest(as, label);
469 int rel = dest - as->code_offset;
470 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
471 if (dest >= 0 && rel <= -4) {
472 // is a backwards jump, so we know the size of the jump on the first pass
Damien826005c2013-10-05 23:17:28 +0100473 // calculate rel assuming 9 bit relative jump
Damien5bfb7592013-10-05 18:41:24 +0100474 if (SIGNED_FIT9(rel)) {
Damien George90edf9e2014-04-18 16:56:54 +0100475 asm_thumb_op16(as, OP_BCC_N(cond, rel));
Damien429d7192013-10-04 19:53:11 +0100476 } else {
Damien5bfb7592013-10-05 18:41:24 +0100477 goto large_jump;
Damien429d7192013-10-04 19:53:11 +0100478 }
Damien5bfb7592013-10-05 18:41:24 +0100479 } else {
480 // is a forwards jump, so need to assume it's large
481 large_jump:
Damien George90edf9e2014-04-18 16:56:54 +0100482 asm_thumb_op32(as, OP_BCC_W_HI(cond, rel), OP_BCC_W_LO(rel));
Damien429d7192013-10-04 19:53:11 +0100483 }
484}
485
486#define OP_BLX(reg) (0x4780 | ((reg) << 3))
487#define OP_SVC(arg) (0xdf00 | (arg))
Damien429d7192013-10-04 19:53:11 +0100488
489void asm_thumb_bl_ind(asm_thumb_t *as, void *fun_ptr, uint fun_id, uint reg_temp) {
490 /* TODO make this use less bytes
491 uint rlo_base = REG_R3;
492 uint rlo_dest = REG_R7;
493 uint word_offset = 4;
Damien George90edf9e2014-04-18 16:56:54 +0100494 asm_thumb_op16(as, 0x0000);
495 asm_thumb_op16(as, 0x6800 | (word_offset << 6) | (rlo_base << 3) | rlo_dest); // ldr rlo_dest, [rlo_base, #offset]
496 asm_thumb_op16(as, 0x4780 | (REG_R9 << 3)); // blx reg
Damien429d7192013-10-04 19:53:11 +0100497 */
498
Damien George7fe21912014-08-16 22:31:57 +0100499 if (fun_id < 32) {
500 // load ptr to function from table, indexed by fun_id (must be in range 0-31); 4 bytes
Damien George90edf9e2014-04-18 16:56:54 +0100501 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));
502 asm_thumb_op16(as, OP_BLX(reg_temp));
Damien429d7192013-10-04 19:53:11 +0100503 } else {
Damien George7fe21912014-08-16 22:31:57 +0100504 // load ptr to function into register using immediate; 6 bytes
505 asm_thumb_mov_reg_i32(as, reg_temp, (mp_uint_t)fun_ptr);
506 asm_thumb_op16(as, OP_BLX(reg_temp));
Damien429d7192013-10-04 19:53:11 +0100507 }
508}
Damien Georgee67ed5d2014-01-04 13:55:24 +0000509
510#endif // MICROPY_EMIT_THUMB || MICROPY_EMIT_INLINE_THUMB