blob: 1102bb74ab4acce7ba69b57fdc5cde51474b19b7 [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) {
Damien George3c658a42014-08-24 16:28:17 +0100135 return as->code_base;
Damien429d7192013-10-04 19:53:11 +0100136}
137
138/*
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200139STATIC void asm_thumb_write_byte_1(asm_thumb_t *as, byte b1) {
Damien429d7192013-10-04 19:53:11 +0100140 byte *c = asm_thumb_get_cur_to_write_bytes(as, 1);
141 c[0] = b1;
142}
143*/
144
Damien429d7192013-10-04 19:53:11 +0100145/*
146#define IMM32_L0(x) ((x) & 0xff)
147#define IMM32_L1(x) (((x) >> 8) & 0xff)
148#define IMM32_L2(x) (((x) >> 16) & 0xff)
149#define IMM32_L3(x) (((x) >> 24) & 0xff)
150
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200151STATIC void asm_thumb_write_word32(asm_thumb_t *as, int w32) {
Damien429d7192013-10-04 19:53:11 +0100152 byte *c = asm_thumb_get_cur_to_write_bytes(as, 4);
153 c[0] = IMM32_L0(w32);
154 c[1] = IMM32_L1(w32);
155 c[2] = IMM32_L2(w32);
156 c[3] = IMM32_L3(w32);
157}
158*/
159
160// rlolist is a bit map indicating desired lo-registers
161#define OP_PUSH_RLIST(rlolist) (0xb400 | (rlolist))
162#define OP_PUSH_RLIST_LR(rlolist) (0xb400 | 0x0100 | (rlolist))
163#define OP_POP_RLIST(rlolist) (0xbc00 | (rlolist))
164#define OP_POP_RLIST_PC(rlolist) (0xbc00 | 0x0100 | (rlolist))
165
166#define OP_ADD_SP(num_words) (0xb000 | (num_words))
167#define OP_SUB_SP(num_words) (0xb080 | (num_words))
168
Damien Georged509ac22014-05-07 23:27:45 +0100169// locals:
170// - stored on the stack in ascending order
171// - numbered 0 through as->num_locals-1
172// - SP points to first local
173//
174// | SP
175// v
176// l0 l1 l2 ... l(n-1)
177// ^ ^
178// | low address | high address in RAM
179
Damien429d7192013-10-04 19:53:11 +0100180void asm_thumb_entry(asm_thumb_t *as, int num_locals) {
Damien Georged509ac22014-05-07 23:27:45 +0100181 // work out what to push and how many extra spaces to reserve on stack
Damien429d7192013-10-04 19:53:11 +0100182 // so that we have enough for all locals and it's aligned an 8-byte boundary
Damien Georged509ac22014-05-07 23:27:45 +0100183 // we push extra regs (r1, r2, r3) to help do the stack adjustment
184 // we probably should just always subtract from sp, since this would be more efficient
185 // for push rlist, lowest numbered register at the lowest address
Damien429d7192013-10-04 19:53:11 +0100186 uint reglist;
187 uint stack_adjust;
188 if (num_locals < 0) {
189 num_locals = 0;
190 }
Damien Georged509ac22014-05-07 23:27:45 +0100191 // don't pop r0 because it's used for return value
Damien429d7192013-10-04 19:53:11 +0100192 switch (num_locals) {
193 case 0:
194 reglist = 0xf2;
195 stack_adjust = 0;
196 break;
197
198 case 1:
199 reglist = 0xf2;
200 stack_adjust = 0;
201 break;
202
203 case 2:
204 reglist = 0xfe;
205 stack_adjust = 0;
206 break;
207
208 case 3:
209 reglist = 0xfe;
210 stack_adjust = 0;
211 break;
212
213 default:
214 reglist = 0xfe;
215 stack_adjust = ((num_locals - 3) + 1) & (~1);
216 break;
217 }
Damien George90edf9e2014-04-18 16:56:54 +0100218 asm_thumb_op16(as, OP_PUSH_RLIST_LR(reglist));
Damien429d7192013-10-04 19:53:11 +0100219 if (stack_adjust > 0) {
Damien George90edf9e2014-04-18 16:56:54 +0100220 asm_thumb_op16(as, OP_SUB_SP(stack_adjust));
Damien429d7192013-10-04 19:53:11 +0100221 }
222 as->push_reglist = reglist;
223 as->stack_adjust = stack_adjust;
224 as->num_locals = num_locals;
225}
226
227void asm_thumb_exit(asm_thumb_t *as) {
228 if (as->stack_adjust > 0) {
Damien George90edf9e2014-04-18 16:56:54 +0100229 asm_thumb_op16(as, OP_ADD_SP(as->stack_adjust));
Damien429d7192013-10-04 19:53:11 +0100230 }
Damien George90edf9e2014-04-18 16:56:54 +0100231 asm_thumb_op16(as, OP_POP_RLIST_PC(as->push_reglist));
Damien429d7192013-10-04 19:53:11 +0100232}
233
Damien George6f355fd2014-04-10 14:11:31 +0100234void asm_thumb_label_assign(asm_thumb_t *as, uint label) {
Damien5bfb7592013-10-05 18:41:24 +0100235 assert(label < as->max_num_labels);
Damien George36db6bc2014-05-07 17:24:22 +0100236 if (as->pass < ASM_THUMB_PASS_EMIT) {
Damien5bfb7592013-10-05 18:41:24 +0100237 // assign label offset
238 assert(as->label_offsets[label] == -1);
239 as->label_offsets[label] = as->code_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100240 } else {
241 // ensure label offset has not changed from PASS_COMPUTE to PASS_EMIT
Damien5bfb7592013-10-05 18:41:24 +0100242 //printf("l%d: (at %d=%ld)\n", label, as->label_offsets[label], as->code_offset);
243 assert(as->label_offsets[label] == as->code_offset);
Damien429d7192013-10-04 19:53:11 +0100244 }
245}
246
Damien Georgee5f8a772014-04-21 13:33:15 +0100247void asm_thumb_align(asm_thumb_t* as, uint align) {
248 // TODO fill unused data with NOPs?
249 as->code_offset = (as->code_offset + align - 1) & (~(align - 1));
250}
251
252void asm_thumb_data(asm_thumb_t* as, uint bytesize, uint val) {
253 byte *c = asm_thumb_get_cur_to_write_bytes(as, bytesize);
Damien George95977712014-05-10 18:07:08 +0100254 // only write to the buffer in the emit pass (otherwise we overflow dummy_data)
255 if (as->pass == ASM_THUMB_PASS_EMIT) {
256 // little endian
257 for (uint i = 0; i < bytesize; i++) {
258 *c++ = val;
259 val >>= 8;
260 }
Damien Georgee5f8a772014-04-21 13:33:15 +0100261 }
262}
263
Damien George6f355fd2014-04-10 14:11:31 +0100264STATIC int get_label_dest(asm_thumb_t *as, uint label) {
Damien5bfb7592013-10-05 18:41:24 +0100265 assert(label < as->max_num_labels);
266 return as->label_offsets[label];
267}
268
Damien George90edf9e2014-04-18 16:56:54 +0100269void asm_thumb_op16(asm_thumb_t *as, uint op) {
270 byte *c = asm_thumb_get_cur_to_write_bytes(as, 2);
271 // little endian
272 c[0] = op;
273 c[1] = op >> 8;
274}
275
276void asm_thumb_op32(asm_thumb_t *as, uint op1, uint op2) {
277 byte *c = asm_thumb_get_cur_to_write_bytes(as, 4);
278 // little endian, op1 then op2
279 c[0] = op1;
280 c[1] = op1 >> 8;
281 c[2] = op2;
282 c[3] = op2 >> 8;
283}
284
Damien George87210872014-04-13 00:30:32 +0100285#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 +0100286
Damien George87210872014-04-13 00:30:32 +0100287void 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 +0100288 assert(rlo_dest < REG_R8);
Damien George87210872014-04-13 00:30:32 +0100289 assert(rlo_src < REG_R8);
Damien George90edf9e2014-04-18 16:56:54 +0100290 asm_thumb_op16(as, OP_FORMAT_2(op, rlo_dest, rlo_src, src_b));
Damien George87210872014-04-13 00:30:32 +0100291}
292
293#define OP_FORMAT_3(op, rlo, i8) ((op) | ((rlo) << 8) | (i8))
294
295void asm_thumb_format_3(asm_thumb_t *as, uint op, uint rlo, int i8) {
296 assert(rlo < REG_R8);
Damien George90edf9e2014-04-18 16:56:54 +0100297 asm_thumb_op16(as, OP_FORMAT_3(op, rlo, i8));
Damien George87210872014-04-13 00:30:32 +0100298}
299
300#define OP_FORMAT_4(op, rlo_dest, rlo_src) ((op) | ((rlo_src) << 3) | (rlo_dest))
301
302void asm_thumb_format_4(asm_thumb_t *as, uint op, uint rlo_dest, uint rlo_src) {
303 assert(rlo_dest < REG_R8);
304 assert(rlo_src < REG_R8);
Damien George90edf9e2014-04-18 16:56:54 +0100305 asm_thumb_op16(as, OP_FORMAT_4(op, rlo_dest, rlo_src));
Damien George87210872014-04-13 00:30:32 +0100306}
307
308#define OP_FORMAT_9_10(op, rlo_dest, rlo_base, offset) ((op) | (((offset) << 6) & 0x07c0) | ((rlo_base) << 3) | (rlo_dest))
309
310void 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 +0100311 asm_thumb_op16(as, OP_FORMAT_9_10(op, rlo_dest, rlo_base, offset));
Damien George87210872014-04-13 00:30:32 +0100312}
313
314void asm_thumb_mov_reg_reg(asm_thumb_t *as, uint reg_dest, uint reg_src) {
315 uint op_lo;
316 if (reg_src < 8) {
317 op_lo = reg_src << 3;
318 } else {
319 op_lo = 0x40 | ((reg_src - 8) << 3);
320 }
321 if (reg_dest < 8) {
322 op_lo |= reg_dest;
323 } else {
324 op_lo |= 0x80 | (reg_dest - 8);
325 }
326 // mov reg_dest, reg_src
Damien George90edf9e2014-04-18 16:56:54 +0100327 asm_thumb_op16(as, 0x4600 | op_lo);
Damien429d7192013-10-04 19:53:11 +0100328}
329
Damien826005c2013-10-05 23:17:28 +0100330#define OP_MOVW (0xf240)
331#define OP_MOVT (0xf2c0)
332
333// if loading lo half with movw, the i16 value will be zero extended into the r32 register!
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200334STATIC 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 +0100335 assert(reg_dest < REG_R15);
Damien826005c2013-10-05 23:17:28 +0100336 // mov[wt] reg_dest, #i16_src
Damien George90edf9e2014-04-18 16:56:54 +0100337 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 +0100338}
339
Damien826005c2013-10-05 23:17:28 +0100340// the i16_src value will be zero extended into the r32 register!
341void asm_thumb_movw_reg_i16(asm_thumb_t *as, uint reg_dest, int i16_src) {
342 asm_thumb_mov_reg_i16(as, OP_MOVW, reg_dest, i16_src);
Damien429d7192013-10-04 19:53:11 +0100343}
344
Damien826005c2013-10-05 23:17:28 +0100345// the i16_src value will be zero extended into the r32 register!
346void asm_thumb_movt_reg_i16(asm_thumb_t *as, uint reg_dest, int i16_src) {
347 asm_thumb_mov_reg_i16(as, OP_MOVT, reg_dest, i16_src);
Damien429d7192013-10-04 19:53:11 +0100348}
349
Damien George47e1b852014-04-08 18:28:33 +0100350void asm_thumb_ite_ge(asm_thumb_t *as) {
Damien George90edf9e2014-04-18 16:56:54 +0100351 asm_thumb_op16(as, 0xbfac);
Damien George47e1b852014-04-08 18:28:33 +0100352}
353
Damien03d41242013-10-06 00:36:05 +0100354#define OP_B_N(byte_offset) (0xe000 | (((byte_offset) >> 1) & 0x07ff))
355
Damien George6f355fd2014-04-10 14:11:31 +0100356void asm_thumb_b_n(asm_thumb_t *as, uint label) {
Damien03d41242013-10-06 00:36:05 +0100357 int dest = get_label_dest(as, label);
358 int rel = dest - as->code_offset;
359 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
360 if (SIGNED_FIT12(rel)) {
Damien George90edf9e2014-04-18 16:56:54 +0100361 asm_thumb_op16(as, OP_B_N(rel));
Damien03d41242013-10-06 00:36:05 +0100362 } else {
363 printf("asm_thumb_b_n: branch does not fit in 12 bits\n");
364 }
365}
366
Damien1a6633a2013-11-03 13:58:19 +0000367#define OP_BCC_N(cond, byte_offset) (0xd000 | ((cond) << 8) | (((byte_offset) >> 1) & 0x00ff))
Damien826005c2013-10-05 23:17:28 +0100368
Damien George6f355fd2014-04-10 14:11:31 +0100369void asm_thumb_bcc_n(asm_thumb_t *as, int cond, uint label) {
Damien826005c2013-10-05 23:17:28 +0100370 int dest = get_label_dest(as, label);
371 int rel = dest - as->code_offset;
372 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
373 if (SIGNED_FIT9(rel)) {
Damien George90edf9e2014-04-18 16:56:54 +0100374 asm_thumb_op16(as, OP_BCC_N(cond, rel));
Damien826005c2013-10-05 23:17:28 +0100375 } else {
Damien1a6633a2013-11-03 13:58:19 +0000376 printf("asm_thumb_bcc_n: branch does not fit in 9 bits\n");
Damien826005c2013-10-05 23:17:28 +0100377 }
378}
379
Damien George40f3c022014-07-03 13:25:24 +0100380void asm_thumb_mov_reg_i32(asm_thumb_t *as, uint reg_dest, mp_uint_t i32) {
Damien826005c2013-10-05 23:17:28 +0100381 // movw, movt does it in 8 bytes
382 // ldr [pc, #], dw does it in 6 bytes, but we might not reach to end of code for dw
383
384 asm_thumb_mov_reg_i16(as, OP_MOVW, reg_dest, i32);
385 asm_thumb_mov_reg_i16(as, OP_MOVT, reg_dest, i32 >> 16);
386}
387
388void asm_thumb_mov_reg_i32_optimised(asm_thumb_t *as, uint reg_dest, int i32) {
389 if (reg_dest < 8 && UNSIGNED_FIT8(i32)) {
Damien George87210872014-04-13 00:30:32 +0100390 asm_thumb_mov_rlo_i8(as, reg_dest, i32);
Damien826005c2013-10-05 23:17:28 +0100391 } else if (UNSIGNED_FIT16(i32)) {
392 asm_thumb_mov_reg_i16(as, OP_MOVW, reg_dest, i32);
393 } else {
394 asm_thumb_mov_reg_i32(as, reg_dest, i32);
395 }
396}
397
Damien George36db6bc2014-05-07 17:24:22 +0100398// i32 is stored as a full word in the code, and aligned to machine-word boundary
399// TODO this is very inefficient, improve it!
400void asm_thumb_mov_reg_i32_aligned(asm_thumb_t *as, uint reg_dest, int i32) {
401 // align on machine-word + 2
402 if ((as->code_offset & 3) == 0) {
403 asm_thumb_op16(as, ASM_THUMB_OP_NOP);
404 }
405 // jump over the i32 value (instruction prefect adds 4 to PC)
406 asm_thumb_op16(as, OP_B_N(0));
407 // store i32 on machine-word aligned boundary
408 asm_thumb_data(as, 4, i32);
409 // do the actual load of the i32 value
410 asm_thumb_mov_reg_i32_optimised(as, reg_dest, i32);
411}
412
Damien429d7192013-10-04 19:53:11 +0100413#define OP_STR_TO_SP_OFFSET(rlo_dest, word_offset) (0x9000 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
414#define OP_LDR_FROM_SP_OFFSET(rlo_dest, word_offset) (0x9800 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
415
416void asm_thumb_mov_local_reg(asm_thumb_t *as, int local_num, uint rlo_src) {
417 assert(rlo_src < 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_STR_TO_SP_OFFSET(rlo_src, word_offset));
Damien429d7192013-10-04 19:53:11 +0100421}
422
423void asm_thumb_mov_reg_local(asm_thumb_t *as, uint rlo_dest, int local_num) {
424 assert(rlo_dest < REG_R8);
Damien Georged509ac22014-05-07 23:27:45 +0100425 int word_offset = local_num;
Damien George36db6bc2014-05-07 17:24:22 +0100426 assert(as->pass < ASM_THUMB_PASS_EMIT || word_offset >= 0);
Damien George90edf9e2014-04-18 16:56:54 +0100427 asm_thumb_op16(as, OP_LDR_FROM_SP_OFFSET(rlo_dest, word_offset));
Damien429d7192013-10-04 19:53:11 +0100428}
429
Damien9b9e9962013-11-03 14:25:43 +0000430#define OP_ADD_REG_SP_OFFSET(rlo_dest, word_offset) (0xa800 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
431
432void asm_thumb_mov_reg_local_addr(asm_thumb_t *as, uint rlo_dest, int local_num) {
433 assert(rlo_dest < REG_R8);
Damien Georged509ac22014-05-07 23:27:45 +0100434 int word_offset = local_num;
Damien George36db6bc2014-05-07 17:24:22 +0100435 assert(as->pass < ASM_THUMB_PASS_EMIT || word_offset >= 0);
Damien George90edf9e2014-04-18 16:56:54 +0100436 asm_thumb_op16(as, OP_ADD_REG_SP_OFFSET(rlo_dest, word_offset));
Damien429d7192013-10-04 19:53:11 +0100437}
438
Damien429d7192013-10-04 19:53:11 +0100439// this could be wrong, because it should have a range of +/- 16MiB...
440#define OP_BW_HI(byte_offset) (0xf000 | (((byte_offset) >> 12) & 0x07ff))
441#define OP_BW_LO(byte_offset) (0xb800 | (((byte_offset) >> 1) & 0x07ff))
442
Damien George6f355fd2014-04-10 14:11:31 +0100443void asm_thumb_b_label(asm_thumb_t *as, uint label) {
Damien5bfb7592013-10-05 18:41:24 +0100444 int dest = get_label_dest(as, label);
445 int rel = dest - as->code_offset;
446 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
447 if (dest >= 0 && rel <= -4) {
448 // is a backwards jump, so we know the size of the jump on the first pass
449 // calculate rel assuming 12 bit relative jump
450 if (SIGNED_FIT12(rel)) {
Damien George90edf9e2014-04-18 16:56:54 +0100451 asm_thumb_op16(as, OP_B_N(rel));
Damien429d7192013-10-04 19:53:11 +0100452 } else {
Damien5bfb7592013-10-05 18:41:24 +0100453 goto large_jump;
Damien429d7192013-10-04 19:53:11 +0100454 }
Damien5bfb7592013-10-05 18:41:24 +0100455 } else {
456 // is a forwards jump, so need to assume it's large
457 large_jump:
Damien George90edf9e2014-04-18 16:56:54 +0100458 asm_thumb_op32(as, OP_BW_HI(rel), OP_BW_LO(rel));
Damien429d7192013-10-04 19:53:11 +0100459 }
460}
461
Damien429d7192013-10-04 19:53:11 +0100462// all these bit arithmetics need coverage testing!
Damien1a6633a2013-11-03 13:58:19 +0000463#define OP_BCC_W_HI(cond, byte_offset) (0xf000 | ((cond) << 6) | (((byte_offset) >> 10) & 0x0400) | (((byte_offset) >> 14) & 0x003f))
464#define OP_BCC_W_LO(byte_offset) (0x8000 | ((byte_offset) & 0x2000) | (((byte_offset) >> 1) & 0x0fff))
Damien429d7192013-10-04 19:53:11 +0100465
Damien George6f355fd2014-04-10 14:11:31 +0100466void asm_thumb_bcc_label(asm_thumb_t *as, int cond, uint label) {
Damien5bfb7592013-10-05 18:41:24 +0100467 int dest = get_label_dest(as, label);
468 int rel = dest - as->code_offset;
469 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
470 if (dest >= 0 && rel <= -4) {
471 // is a backwards jump, so we know the size of the jump on the first pass
Damien826005c2013-10-05 23:17:28 +0100472 // calculate rel assuming 9 bit relative jump
Damien5bfb7592013-10-05 18:41:24 +0100473 if (SIGNED_FIT9(rel)) {
Damien George90edf9e2014-04-18 16:56:54 +0100474 asm_thumb_op16(as, OP_BCC_N(cond, rel));
Damien429d7192013-10-04 19:53:11 +0100475 } else {
Damien5bfb7592013-10-05 18:41:24 +0100476 goto large_jump;
Damien429d7192013-10-04 19:53:11 +0100477 }
Damien5bfb7592013-10-05 18:41:24 +0100478 } else {
479 // is a forwards jump, so need to assume it's large
480 large_jump:
Damien George90edf9e2014-04-18 16:56:54 +0100481 asm_thumb_op32(as, OP_BCC_W_HI(cond, rel), OP_BCC_W_LO(rel));
Damien429d7192013-10-04 19:53:11 +0100482 }
483}
484
485#define OP_BLX(reg) (0x4780 | ((reg) << 3))
486#define OP_SVC(arg) (0xdf00 | (arg))
Damien429d7192013-10-04 19:53:11 +0100487
488void asm_thumb_bl_ind(asm_thumb_t *as, void *fun_ptr, uint fun_id, uint reg_temp) {
489 /* TODO make this use less bytes
490 uint rlo_base = REG_R3;
491 uint rlo_dest = REG_R7;
492 uint word_offset = 4;
Damien George90edf9e2014-04-18 16:56:54 +0100493 asm_thumb_op16(as, 0x0000);
494 asm_thumb_op16(as, 0x6800 | (word_offset << 6) | (rlo_base << 3) | rlo_dest); // ldr rlo_dest, [rlo_base, #offset]
495 asm_thumb_op16(as, 0x4780 | (REG_R9 << 3)); // blx reg
Damien429d7192013-10-04 19:53:11 +0100496 */
497
Damien George7fe21912014-08-16 22:31:57 +0100498 if (fun_id < 32) {
499 // 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 +0100500 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));
501 asm_thumb_op16(as, OP_BLX(reg_temp));
Damien429d7192013-10-04 19:53:11 +0100502 } else {
Damien George7fe21912014-08-16 22:31:57 +0100503 // load ptr to function into register using immediate; 6 bytes
504 asm_thumb_mov_reg_i32(as, reg_temp, (mp_uint_t)fun_ptr);
505 asm_thumb_op16(as, OP_BLX(reg_temp));
Damien429d7192013-10-04 19:53:11 +0100506 }
507}
Damien Georgee67ed5d2014-01-04 13:55:24 +0000508
509#endif // MICROPY_EMIT_THUMB || MICROPY_EMIT_INLINE_THUMB