blob: 8ba22faa074433afc0dfce078756ccba0c6bc296 [file] [log] [blame]
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
Fabian Vogt16ee30c2014-08-28 01:18:56 +02006 * Copyright (c) 2014 Fabian Vogt
7 * Copyright (c) 2013, 2014 Damien P. George
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02008 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 */
27
28#include <stdio.h>
29#include <assert.h>
30#include <string.h>
31
32#include "mpconfig.h"
33#include "misc.h"
34#include "asmarm.h"
35
36// wrapper around everything in this file
37#if MICROPY_EMIT_ARM
38
39#define SIGNED_FIT24(x) (((x) & 0xff800000) == 0) || (((x) & 0xff000000) == 0xff000000)
40
41struct _asm_arm_t {
42 uint pass;
43 uint code_offset;
44 uint code_size;
45 byte *code_base;
46 byte dummy_data[4];
47
48 uint max_num_labels;
49 int *label_offsets;
50 int num_locals;
51 uint push_reglist;
52 uint stack_adjust;
53};
54
55asm_arm_t *asm_arm_new(uint max_num_labels) {
56 asm_arm_t *as;
57
58 as = m_new0(asm_arm_t, 1);
59 as->max_num_labels = max_num_labels;
60 as->label_offsets = m_new(int, max_num_labels);
61
62 return as;
63}
64
65void asm_arm_free(asm_arm_t *as, bool free_code) {
66 if (free_code) {
67 m_del(byte, as->code_base, as->code_size);
68 }
69
70 m_del_obj(asm_arm_t, as);
71}
72
73void asm_arm_start_pass(asm_arm_t *as, uint pass) {
74 as->pass = pass;
75 as->code_offset = 0;
76 if (pass == ASM_ARM_PASS_COMPUTE) {
77 memset(as->label_offsets, -1, as->max_num_labels * sizeof(int));
78 }
79}
80
81void asm_arm_end_pass(asm_arm_t *as) {
82 if (as->pass == ASM_ARM_PASS_COMPUTE) {
83 // calculate size of code in bytes
84 as->code_size = as->code_offset;
85 as->code_base = m_new(byte, as->code_size);
86 }
87}
88
89// all functions must go through this one to emit bytes
90// if as->pass < ASM_ARM_PASS_EMIT, then this function only returns a buffer of 4 bytes length
91STATIC byte *asm_arm_get_cur_to_write_bytes(asm_arm_t *as, int num_bytes_to_write) {
92 if (as->pass < ASM_ARM_PASS_EMIT) {
93 as->code_offset += num_bytes_to_write;
94 return as->dummy_data;
95 } else {
96 assert(as->code_offset + num_bytes_to_write <= as->code_size);
97 byte *c = as->code_base + as->code_offset;
98 as->code_offset += num_bytes_to_write;
99 return c;
100 }
101}
102
103uint asm_arm_get_code_size(asm_arm_t *as) {
104 return as->code_size;
105}
106
107void *asm_arm_get_code(asm_arm_t *as) {
108 return as->code_base;
109}
110
111// Insert word into instruction flow
112STATIC void emit(asm_arm_t *as, uint op) {
113 *(uint*)asm_arm_get_cur_to_write_bytes(as, 4) = op;
114}
115
116// Insert word into instruction flow, add "ALWAYS" condition code
117STATIC void emit_al(asm_arm_t *as, uint op) {
118 emit(as, op | ARM_CC_AL);
119}
120
121// Basic instructions without condition code
122STATIC uint asm_arm_op_push(uint reglist) {
123 // stmfd sp!, {reglist}
124 return 0x92d0000 | (reglist & 0xFFFF);
125}
126
127STATIC uint asm_arm_op_pop(uint reglist) {
128 // ldmfd sp!, {reglist}
129 return 0x8bd0000 | (reglist & 0xFFFF);
130}
131
132STATIC uint asm_arm_op_mov_reg(uint rd, uint rn) {
133 // mov rd, rn
134 return 0x1a00000 | (rd << 12) | rn;
135}
136
137STATIC uint asm_arm_op_mov_imm(uint rd, uint imm) {
138 // mov rd, #imm
139 return 0x3a00000 | (rd << 12) | imm;
140}
141
142STATIC uint asm_arm_op_mvn_imm(uint rd, uint imm) {
143 // mvn rd, #imm
144 return 0x3e00000 | (rd << 12) | imm;
145}
146
147STATIC uint asm_arm_op_add_imm(uint rd, uint rn, uint imm) {
148 // add rd, rn, #imm
149 return 0x2800000 | (rn << 16) | (rd << 12) | (imm & 0xFF);
150}
151
152STATIC uint asm_arm_op_add_reg(uint rd, uint rn, uint rm) {
153 // add rd, rn, rm
154 return 0x0800000 | (rn << 16) | (rd << 12) | rm;
155}
156
157STATIC uint asm_arm_op_sub_imm(uint rd, uint rn, uint imm) {
158 // sub rd, rn, #imm
159 return 0x2400000 | (rn << 16) | (rd << 12) | (imm & 0xFF);
160}
161
162void asm_arm_bkpt(asm_arm_t *as) {
163 // bkpt #0
164 emit_al(as, 0x1200070);
165}
166
167// locals:
168// - stored on the stack in ascending order
169// - numbered 0 through as->num_locals-1
170// - SP points to first local
171//
172// | SP
173// v
174// l0 l1 l2 ... l(n-1)
175// ^ ^
176// | low address | high address in RAM
177
178void asm_arm_entry(asm_arm_t *as, int num_locals) {
179
180 if (num_locals < 0) {
181 num_locals = 0;
182 }
183
184 as->stack_adjust = 0;
185 as->num_locals = num_locals;
186 as->push_reglist = 1 << REG_R1 | 1 << REG_R2 | 1 << REG_R3 | 1 << REG_R4
187 | 1 << REG_R5 | 1 << REG_R6 | 1 << REG_R7 | 1 << REG_R8;
188
189 // Only adjust the stack if there are more locals than usable registers
190 if(num_locals > 3) {
191 as->stack_adjust = num_locals * 4;
192 // Align stack to 8 bytes
193 if(as->num_locals & 1)
194 as->stack_adjust += 4;
195 }
196
197 emit_al(as, asm_arm_op_push(as->push_reglist | 1 << REG_LR));
198 if (as->stack_adjust > 0) {
199 emit_al(as, asm_arm_op_sub_imm(REG_SP, REG_SP, as->stack_adjust));
200 }
201}
202
203void asm_arm_exit(asm_arm_t *as) {
204 if (as->stack_adjust > 0) {
205 emit_al(as, asm_arm_op_add_imm(REG_SP, REG_SP, as->stack_adjust));
206 }
207
208 emit_al(as, asm_arm_op_pop(as->push_reglist | (1 << REG_PC)));
209}
210
211void asm_arm_label_assign(asm_arm_t *as, uint label) {
212 assert(label < as->max_num_labels);
213 if (as->pass < ASM_ARM_PASS_EMIT) {
214 // assign label offset
215 assert(as->label_offsets[label] == -1);
216 as->label_offsets[label] = as->code_offset;
217 } else {
218 // ensure label offset has not changed from PASS_COMPUTE to PASS_EMIT
219 assert(as->label_offsets[label] == as->code_offset);
220 }
221}
222
223void asm_arm_align(asm_arm_t* as, uint align) {
224 // TODO fill unused data with NOPs?
225 as->code_offset = (as->code_offset + align - 1) & (~(align - 1));
226}
227
228void asm_arm_data(asm_arm_t* as, uint bytesize, uint val) {
229 byte *c = asm_arm_get_cur_to_write_bytes(as, bytesize);
230 // only write to the buffer in the emit pass (otherwise we overflow dummy_data)
231 if (as->pass == ASM_ARM_PASS_EMIT) {
232 // little endian
233 for (uint i = 0; i < bytesize; i++) {
234 *c++ = val;
235 val >>= 8;
236 }
237 }
238}
239
240void asm_arm_mov_reg_reg(asm_arm_t *as, uint reg_dest, uint reg_src) {
241 emit_al(as, asm_arm_op_mov_reg(reg_dest, reg_src));
242}
243
244void asm_arm_mov_reg_i32(asm_arm_t *as, uint rd, int imm) {
245 // TODO: There are more variants of immediate values
246 if ((imm & 0xFF) == imm) {
247 emit_al(as, asm_arm_op_mov_imm(rd, imm));
248 } else if (imm < 0 && ((-imm) & 0xFF) == -imm) {
249 emit_al(as, asm_arm_op_mvn_imm(rd, -imm));
250 } else {
251 //Insert immediate into code and jump over it
252 emit_al(as, 0x59f0000 | (rd << 12)); // ldr rd, [pc]
253 emit_al(as, 0xa000000); // b pc
254 emit(as, imm);
255 }
256}
257
258void asm_arm_mov_local_reg(asm_arm_t *as, int local_num, uint rd) {
259 // str rd, [sp, #local_num*4]
260 emit_al(as, 0x58d0000 | (rd << 12) | (local_num << 2));
261}
262
263void asm_arm_mov_reg_local(asm_arm_t *as, uint rd, int local_num) {
264 // ldr rd, [sp, #local_num*4]
265 emit_al(as, 0x59d0000 | (rd << 12) | (local_num << 2));
266}
267
268void asm_arm_cmp_reg_i8(asm_arm_t *as, uint rd, int imm) {
269 // cmp rd, #imm
270 emit_al(as, 0x3500000 | (rd << 16) | (imm & 0xFF));
271}
272
273void asm_arm_cmp_reg_reg(asm_arm_t *as, uint rd, uint rn) {
274 // cmp rd, rn
275 emit_al(as, 0x1500000 | (rd << 16) | rn);
276}
277
278void asm_arm_less_op(asm_arm_t *as, uint rd, uint rn) {
279 asm_arm_cmp_reg_reg(as, rd, rn); // cmp rd, rn
280 emit(as, asm_arm_op_mov_imm(REG_RET, 1) | ARM_CC_LT); // movlt REG_RET, #1
281 emit(as, asm_arm_op_mov_imm(REG_RET, 0) | ARM_CC_GE); // movge REG_RET, #0
282}
283
284void asm_arm_add_reg(asm_arm_t *as, uint rd, uint rn, uint rm) {
285 // add rd, rn, rm
286 emit_al(as, asm_arm_op_add_reg(rd, rn, rm));
287}
288
289void asm_arm_mov_reg_local_addr(asm_arm_t *as, uint rd, int local_num) {
290 // add rd, sp, #local_num*4
291 emit_al(as, asm_arm_op_add_imm(rd, REG_SP, local_num << 2));
292}
293
294void asm_arm_bcc_label(asm_arm_t *as, int cond, uint label) {
295 assert(label < as->max_num_labels);
296 int dest = as->label_offsets[label];
297 int rel = dest - as->code_offset;
298 rel -= 8; // account for instruction prefetch, PC is 8 bytes ahead of this instruction
299 rel >>= 2; // in ARM mode the branch target is 32-bit aligned, so the 2 LSB are omitted
300
301 if (SIGNED_FIT24(rel)) {
302 emit(as, cond | 0xa000000 | (rel & 0xffffff));
303 } else {
304 printf("asm_arm_bcc: branch does not fit in 24 bits\n");
305 }
306}
307
308void asm_arm_b_label(asm_arm_t *as, uint label) {
309 asm_arm_bcc_label(as, ARM_CC_AL, label);
310}
311
312void asm_arm_bl_ind(asm_arm_t *as, void *fun_ptr, uint fun_id, uint reg_temp) {
313 // If the table offset fits into the ldr instruction
314 if(fun_id < (0x1000 / 4)) {
315 emit_al(as, asm_arm_op_mov_reg(REG_LR, REG_PC)); // mov lr, pc
316 emit_al(as, 0x597f000 | (fun_id << 2)); // ldr pc, [r7, #fun_id*4]
317 return;
318 }
319
320 emit_al(as, 0x59f0004 | (reg_temp << 12)); // ldr rd, [pc, #4]
321 // Set lr after fun_ptr
322 emit_al(as, asm_arm_op_add_imm(REG_LR, REG_PC, 4)); // add lr, pc, #4
323 emit_al(as, asm_arm_op_mov_reg(REG_PC, reg_temp)); // mov pc, reg_temp
324 emit(as, (uint) fun_ptr);
325}
326
327#endif // MICROPY_EMIT_ARM