blob: 51852df5f49d058458cfdaeed3830d0c97072395 [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;
Fabian Vogtb7235b82014-09-03 16:59:33 +020043 mp_uint_t code_offset;
44 mp_uint_t code_size;
Fabian Vogtfe3d16e2014-08-16 22:55:53 +020045 byte *code_base;
46 byte dummy_data[4];
47
Damien George0b610de2014-09-29 16:25:04 +010048 mp_uint_t max_num_labels;
49 mp_uint_t *label_offsets;
Fabian Vogtfe3d16e2014-08-16 22:55:53 +020050 uint push_reglist;
51 uint stack_adjust;
52};
53
54asm_arm_t *asm_arm_new(uint max_num_labels) {
55 asm_arm_t *as;
56
57 as = m_new0(asm_arm_t, 1);
58 as->max_num_labels = max_num_labels;
Damien George0b610de2014-09-29 16:25:04 +010059 as->label_offsets = m_new(mp_uint_t, max_num_labels);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +020060
61 return as;
62}
63
64void asm_arm_free(asm_arm_t *as, bool free_code) {
65 if (free_code) {
Fabian Vogtb7235b82014-09-03 16:59:33 +020066 MP_PLAT_FREE_EXEC(as->code_base, as->code_size);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +020067 }
Damien George0b610de2014-09-29 16:25:04 +010068 m_del(mp_uint_t, as->label_offsets, as->max_num_labels);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +020069 m_del_obj(asm_arm_t, as);
70}
71
72void asm_arm_start_pass(asm_arm_t *as, uint pass) {
73 as->pass = pass;
74 as->code_offset = 0;
75 if (pass == ASM_ARM_PASS_COMPUTE) {
Damien George0b610de2014-09-29 16:25:04 +010076 memset(as->label_offsets, -1, as->max_num_labels * sizeof(mp_uint_t));
Fabian Vogtfe3d16e2014-08-16 22:55:53 +020077 }
78}
79
80void asm_arm_end_pass(asm_arm_t *as) {
81 if (as->pass == ASM_ARM_PASS_COMPUTE) {
Fabian Vogtb7235b82014-09-03 16:59:33 +020082 MP_PLAT_ALLOC_EXEC(as->code_offset, (void**) &as->code_base, &as->code_size);
83 if(as->code_base == NULL) {
84 assert(0);
85 }
Damien Georgedda46462014-09-03 22:47:23 +010086 } else if(as->pass == ASM_ARM_PASS_EMIT) {
Fabian Vogtb7235b82014-09-03 16:59:33 +020087#ifdef __arm__
88 // flush I- and D-cache
Damien Georgedda46462014-09-03 22:47:23 +010089 asm volatile(
Fabian Vogtb7235b82014-09-03 16:59:33 +020090 "0:"
91 "mrc p15, 0, r15, c7, c10, 3\n"
92 "bne 0b\n"
93 "mov r0, #0\n"
Damien Georgedda46462014-09-03 22:47:23 +010094 "mcr p15, 0, r0, c7, c7, 0\n"
Fabian Vogtb7235b82014-09-03 16:59:33 +020095 : : : "r0", "cc");
96#endif
Fabian Vogtfe3d16e2014-08-16 22:55:53 +020097 }
98}
99
100// all functions must go through this one to emit bytes
101// if as->pass < ASM_ARM_PASS_EMIT, then this function only returns a buffer of 4 bytes length
102STATIC byte *asm_arm_get_cur_to_write_bytes(asm_arm_t *as, int num_bytes_to_write) {
103 if (as->pass < ASM_ARM_PASS_EMIT) {
104 as->code_offset += num_bytes_to_write;
105 return as->dummy_data;
106 } else {
107 assert(as->code_offset + num_bytes_to_write <= as->code_size);
108 byte *c = as->code_base + as->code_offset;
109 as->code_offset += num_bytes_to_write;
110 return c;
111 }
112}
113
114uint asm_arm_get_code_size(asm_arm_t *as) {
115 return as->code_size;
116}
117
118void *asm_arm_get_code(asm_arm_t *as) {
119 return as->code_base;
120}
121
122// Insert word into instruction flow
123STATIC void emit(asm_arm_t *as, uint op) {
124 *(uint*)asm_arm_get_cur_to_write_bytes(as, 4) = op;
125}
126
127// Insert word into instruction flow, add "ALWAYS" condition code
128STATIC void emit_al(asm_arm_t *as, uint op) {
Damien George0b610de2014-09-29 16:25:04 +0100129 emit(as, op | ASM_ARM_CC_AL);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200130}
131
132// Basic instructions without condition code
133STATIC uint asm_arm_op_push(uint reglist) {
134 // stmfd sp!, {reglist}
135 return 0x92d0000 | (reglist & 0xFFFF);
136}
137
138STATIC uint asm_arm_op_pop(uint reglist) {
139 // ldmfd sp!, {reglist}
140 return 0x8bd0000 | (reglist & 0xFFFF);
141}
142
143STATIC uint asm_arm_op_mov_reg(uint rd, uint rn) {
144 // mov rd, rn
145 return 0x1a00000 | (rd << 12) | rn;
146}
147
148STATIC uint asm_arm_op_mov_imm(uint rd, uint imm) {
149 // mov rd, #imm
150 return 0x3a00000 | (rd << 12) | imm;
151}
152
153STATIC uint asm_arm_op_mvn_imm(uint rd, uint imm) {
154 // mvn rd, #imm
155 return 0x3e00000 | (rd << 12) | imm;
156}
157
158STATIC uint asm_arm_op_add_imm(uint rd, uint rn, uint imm) {
159 // add rd, rn, #imm
160 return 0x2800000 | (rn << 16) | (rd << 12) | (imm & 0xFF);
161}
162
163STATIC uint asm_arm_op_add_reg(uint rd, uint rn, uint rm) {
164 // add rd, rn, rm
165 return 0x0800000 | (rn << 16) | (rd << 12) | rm;
166}
167
168STATIC uint asm_arm_op_sub_imm(uint rd, uint rn, uint imm) {
169 // sub rd, rn, #imm
170 return 0x2400000 | (rn << 16) | (rd << 12) | (imm & 0xFF);
171}
172
Damien George3112cde2014-09-29 18:45:42 +0100173STATIC uint asm_arm_op_sub_reg(uint rd, uint rn, uint rm) {
174 // sub rd, rn, rm
175 return 0x0400000 | (rn << 16) | (rd << 12) | rm;
176}
177
Damien George1ef23482014-10-12 14:21:06 +0100178STATIC uint asm_arm_op_and_reg(uint rd, uint rn, uint rm) {
179 // and rd, rn, rm
180 return 0x0000000 | (rn << 16) | (rd << 12) | rm;
181}
182
183STATIC uint asm_arm_op_eor_reg(uint rd, uint rn, uint rm) {
184 // eor rd, rn, rm
185 return 0x0200000 | (rn << 16) | (rd << 12) | rm;
186}
187
188STATIC uint asm_arm_op_orr_reg(uint rd, uint rn, uint rm) {
189 // orr rd, rn, rm
190 return 0x1800000 | (rn << 16) | (rd << 12) | rm;
191}
192
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200193void asm_arm_bkpt(asm_arm_t *as) {
194 // bkpt #0
195 emit_al(as, 0x1200070);
196}
197
198// locals:
199// - stored on the stack in ascending order
Damien George0b610de2014-09-29 16:25:04 +0100200// - numbered 0 through num_locals-1
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200201// - SP points to first local
202//
203// | SP
204// v
205// l0 l1 l2 ... l(n-1)
206// ^ ^
207// | low address | high address in RAM
208
209void asm_arm_entry(asm_arm_t *as, int num_locals) {
210
211 if (num_locals < 0) {
212 num_locals = 0;
213 }
214
215 as->stack_adjust = 0;
Damien George0b610de2014-09-29 16:25:04 +0100216 as->push_reglist = 1 << ASM_ARM_REG_R1
217 | 1 << ASM_ARM_REG_R2
218 | 1 << ASM_ARM_REG_R3
219 | 1 << ASM_ARM_REG_R4
220 | 1 << ASM_ARM_REG_R5
221 | 1 << ASM_ARM_REG_R6
222 | 1 << ASM_ARM_REG_R7
223 | 1 << ASM_ARM_REG_R8;
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200224
225 // Only adjust the stack if there are more locals than usable registers
226 if(num_locals > 3) {
227 as->stack_adjust = num_locals * 4;
228 // Align stack to 8 bytes
Damien George0b610de2014-09-29 16:25:04 +0100229 if (num_locals & 1) {
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200230 as->stack_adjust += 4;
Damien George0b610de2014-09-29 16:25:04 +0100231 }
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200232 }
233
Damien George0b610de2014-09-29 16:25:04 +0100234 emit_al(as, asm_arm_op_push(as->push_reglist | 1 << ASM_ARM_REG_LR));
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200235 if (as->stack_adjust > 0) {
Damien George0b610de2014-09-29 16:25:04 +0100236 emit_al(as, asm_arm_op_sub_imm(ASM_ARM_REG_SP, ASM_ARM_REG_SP, as->stack_adjust));
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200237 }
238}
239
240void asm_arm_exit(asm_arm_t *as) {
241 if (as->stack_adjust > 0) {
Damien George0b610de2014-09-29 16:25:04 +0100242 emit_al(as, asm_arm_op_add_imm(ASM_ARM_REG_SP, ASM_ARM_REG_SP, as->stack_adjust));
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200243 }
244
Damien George0b610de2014-09-29 16:25:04 +0100245 emit_al(as, asm_arm_op_pop(as->push_reglist | (1 << ASM_ARM_REG_PC)));
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200246}
247
248void asm_arm_label_assign(asm_arm_t *as, uint label) {
249 assert(label < as->max_num_labels);
250 if (as->pass < ASM_ARM_PASS_EMIT) {
251 // assign label offset
252 assert(as->label_offsets[label] == -1);
253 as->label_offsets[label] = as->code_offset;
254 } else {
255 // ensure label offset has not changed from PASS_COMPUTE to PASS_EMIT
256 assert(as->label_offsets[label] == as->code_offset);
257 }
258}
259
260void asm_arm_align(asm_arm_t* as, uint align) {
261 // TODO fill unused data with NOPs?
262 as->code_offset = (as->code_offset + align - 1) & (~(align - 1));
263}
264
265void asm_arm_data(asm_arm_t* as, uint bytesize, uint val) {
266 byte *c = asm_arm_get_cur_to_write_bytes(as, bytesize);
267 // only write to the buffer in the emit pass (otherwise we overflow dummy_data)
268 if (as->pass == ASM_ARM_PASS_EMIT) {
269 // little endian
270 for (uint i = 0; i < bytesize; i++) {
271 *c++ = val;
272 val >>= 8;
273 }
274 }
275}
276
277void asm_arm_mov_reg_reg(asm_arm_t *as, uint reg_dest, uint reg_src) {
278 emit_al(as, asm_arm_op_mov_reg(reg_dest, reg_src));
279}
280
281void asm_arm_mov_reg_i32(asm_arm_t *as, uint rd, int imm) {
282 // TODO: There are more variants of immediate values
283 if ((imm & 0xFF) == imm) {
284 emit_al(as, asm_arm_op_mov_imm(rd, imm));
Paul Sokolovskyc0bc3bd2014-12-14 03:24:17 +0200285 } else if (imm < 0 && imm >= -256) {
286 // mvn is "move not", not "move negative"
287 emit_al(as, asm_arm_op_mvn_imm(rd, ~imm));
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200288 } else {
289 //Insert immediate into code and jump over it
290 emit_al(as, 0x59f0000 | (rd << 12)); // ldr rd, [pc]
291 emit_al(as, 0xa000000); // b pc
292 emit(as, imm);
293 }
294}
295
296void asm_arm_mov_local_reg(asm_arm_t *as, int local_num, uint rd) {
297 // str rd, [sp, #local_num*4]
298 emit_al(as, 0x58d0000 | (rd << 12) | (local_num << 2));
299}
300
301void asm_arm_mov_reg_local(asm_arm_t *as, uint rd, int local_num) {
302 // ldr rd, [sp, #local_num*4]
303 emit_al(as, 0x59d0000 | (rd << 12) | (local_num << 2));
304}
305
306void asm_arm_cmp_reg_i8(asm_arm_t *as, uint rd, int imm) {
307 // cmp rd, #imm
308 emit_al(as, 0x3500000 | (rd << 16) | (imm & 0xFF));
309}
310
311void asm_arm_cmp_reg_reg(asm_arm_t *as, uint rd, uint rn) {
312 // cmp rd, rn
313 emit_al(as, 0x1500000 | (rd << 16) | rn);
314}
315
Fabian Vogte5268962014-10-04 00:53:46 +0200316void asm_arm_setcc_reg(asm_arm_t *as, uint rd, uint cond) {
317 emit(as, asm_arm_op_mov_imm(rd, 1) | cond); // movCOND rd, #1
318 emit(as, asm_arm_op_mov_imm(rd, 0) | (cond ^ (1 << 28))); // mov!COND rd, #0
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200319}
320
Damien George3112cde2014-09-29 18:45:42 +0100321void asm_arm_add_reg_reg_reg(asm_arm_t *as, uint rd, uint rn, uint rm) {
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200322 // add rd, rn, rm
323 emit_al(as, asm_arm_op_add_reg(rd, rn, rm));
324}
325
Damien George3112cde2014-09-29 18:45:42 +0100326void asm_arm_sub_reg_reg_reg(asm_arm_t *as, uint rd, uint rn, uint rm) {
327 // sub rd, rn, rm
328 emit_al(as, asm_arm_op_sub_reg(rd, rn, rm));
329}
330
Damien George1ef23482014-10-12 14:21:06 +0100331void asm_arm_and_reg_reg_reg(asm_arm_t *as, uint rd, uint rn, uint rm) {
332 // and rd, rn, rm
333 emit_al(as, asm_arm_op_and_reg(rd, rn, rm));
334}
335
336void asm_arm_eor_reg_reg_reg(asm_arm_t *as, uint rd, uint rn, uint rm) {
337 // eor rd, rn, rm
338 emit_al(as, asm_arm_op_eor_reg(rd, rn, rm));
339}
340
341void asm_arm_orr_reg_reg_reg(asm_arm_t *as, uint rd, uint rn, uint rm) {
342 // orr rd, rn, rm
343 emit_al(as, asm_arm_op_orr_reg(rd, rn, rm));
344}
345
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200346void asm_arm_mov_reg_local_addr(asm_arm_t *as, uint rd, int local_num) {
347 // add rd, sp, #local_num*4
Damien George0b610de2014-09-29 16:25:04 +0100348 emit_al(as, asm_arm_op_add_imm(rd, ASM_ARM_REG_SP, local_num << 2));
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200349}
350
Fabian Vogte5268962014-10-04 00:53:46 +0200351void asm_arm_lsl_reg_reg(asm_arm_t *as, uint rd, uint rs) {
352 // mov rd, rd, lsl rs
353 emit_al(as, 0x1a00010 | (rd << 12) | (rs << 8) | rd);
354}
355
356void asm_arm_asr_reg_reg(asm_arm_t *as, uint rd, uint rs) {
357 // mov rd, rd, asr rs
358 emit_al(as, 0x1a00050 | (rd << 12) | (rs << 8) | rd);
359}
360
Damien George91cfd412014-10-12 16:59:29 +0100361void asm_arm_ldr_reg_reg(asm_arm_t *as, uint rd, uint rn) {
362 // ldr rd, [rn]
363 emit_al(as, 0x5900000 | (rn << 16) | (rd << 12));
364}
365
366void asm_arm_ldrh_reg_reg(asm_arm_t *as, uint rd, uint rn) {
367 // ldrh rd, [rn]
368 emit_al(as, 0x1d000b0 | (rn << 16) | (rd << 12));
369}
370
371void asm_arm_ldrb_reg_reg(asm_arm_t *as, uint rd, uint rn) {
372 // ldrb rd, [rn]
373 emit_al(as, 0x5d00000 | (rn << 16) | (rd << 12));
374}
375
Fabian Vogte5268962014-10-04 00:53:46 +0200376void asm_arm_str_reg_reg(asm_arm_t *as, uint rd, uint rm) {
377 // str rd, [rm]
378 emit_al(as, 0x5800000 | (rm << 16) | (rd << 12));
379}
380
381void asm_arm_strh_reg_reg(asm_arm_t *as, uint rd, uint rm) {
382 // strh rd, [rm]
383 emit_al(as, 0x1c000b0 | (rm << 16) | (rd << 12));
384}
385
386void asm_arm_strb_reg_reg(asm_arm_t *as, uint rd, uint rm) {
387 // strb rd, [rm]
388 emit_al(as, 0x5c00000 | (rm << 16) | (rd << 12));
389}
390
391void asm_arm_str_reg_reg_reg(asm_arm_t *as, uint rd, uint rm, uint rn) {
392 // str rd, [rm, rn, lsl #2]
393 emit_al(as, 0x7800100 | (rm << 16) | (rd << 12) | rn);
394}
395
396void asm_arm_strh_reg_reg_reg(asm_arm_t *as, uint rd, uint rm, uint rn) {
397 // strh doesn't support scaled register index
398 emit_al(as, 0x1a00080 | (ASM_ARM_REG_R8 << 12) | rn); // mov r8, rn, lsl #1
399 emit_al(as, 0x18000b0 | (rm << 16) | (rd << 12) | ASM_ARM_REG_R8); // strh rd, [rm, r8]
400}
401
402void asm_arm_strb_reg_reg_reg(asm_arm_t *as, uint rd, uint rm, uint rn) {
403 // strb rd, [rm, rn]
404 emit_al(as, 0x7c00000 | (rm << 16) | (rd << 12) | rn);
405}
406
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200407void asm_arm_bcc_label(asm_arm_t *as, int cond, uint label) {
408 assert(label < as->max_num_labels);
Damien George0b610de2014-09-29 16:25:04 +0100409 mp_uint_t dest = as->label_offsets[label];
410 mp_int_t rel = dest - as->code_offset;
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200411 rel -= 8; // account for instruction prefetch, PC is 8 bytes ahead of this instruction
412 rel >>= 2; // in ARM mode the branch target is 32-bit aligned, so the 2 LSB are omitted
413
414 if (SIGNED_FIT24(rel)) {
415 emit(as, cond | 0xa000000 | (rel & 0xffffff));
416 } else {
417 printf("asm_arm_bcc: branch does not fit in 24 bits\n");
418 }
419}
420
421void asm_arm_b_label(asm_arm_t *as, uint label) {
Damien George0b610de2014-09-29 16:25:04 +0100422 asm_arm_bcc_label(as, ASM_ARM_CC_AL, label);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200423}
424
425void asm_arm_bl_ind(asm_arm_t *as, void *fun_ptr, uint fun_id, uint reg_temp) {
426 // If the table offset fits into the ldr instruction
427 if(fun_id < (0x1000 / 4)) {
Damien George0b610de2014-09-29 16:25:04 +0100428 emit_al(as, asm_arm_op_mov_reg(ASM_ARM_REG_LR, ASM_ARM_REG_PC)); // mov lr, pc
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200429 emit_al(as, 0x597f000 | (fun_id << 2)); // ldr pc, [r7, #fun_id*4]
430 return;
431 }
432
433 emit_al(as, 0x59f0004 | (reg_temp << 12)); // ldr rd, [pc, #4]
434 // Set lr after fun_ptr
Damien George0b610de2014-09-29 16:25:04 +0100435 emit_al(as, asm_arm_op_add_imm(ASM_ARM_REG_LR, ASM_ARM_REG_PC, 4)); // add lr, pc, #4
436 emit_al(as, asm_arm_op_mov_reg(ASM_ARM_REG_PC, reg_temp)); // mov pc, reg_temp
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200437 emit(as, (uint) fun_ptr);
438}
439
440#endif // MICROPY_EMIT_ARM