blob: a6b95764ba1ed1def54a5559700d5703261af56e [file] [log] [blame]
Damien429d7192013-10-04 19:53:11 +01001#include <stdint.h>
2#include <stdio.h>
3#include <assert.h>
4#include <string.h>
5
6#include "misc.h"
Damienc025ebb2013-10-12 14:30:21 +01007#include "mpyconfig.h"
Damien429d7192013-10-04 19:53:11 +01008#include "asmthumb.h"
9
10#define UNSIGNED_FIT8(x) (((x) & 0xffffff00) == 0)
11#define UNSIGNED_FIT16(x) (((x) & 0xffff0000) == 0)
12#define SIGNED_FIT8(x) (((x) & 0xffffff80) == 0) || (((x) & 0xffffff80) == 0xffffff80)
13#define SIGNED_FIT9(x) (((x) & 0xffffff00) == 0) || (((x) & 0xffffff00) == 0xffffff00)
14#define SIGNED_FIT12(x) (((x) & 0xfffff800) == 0) || (((x) & 0xfffff800) == 0xfffff800)
15
16struct _asm_thumb_t {
17 int pass;
18 uint code_offset;
19 uint code_size;
20 byte *code_base;
21 byte dummy_data[8];
22
Damien429d7192013-10-04 19:53:11 +010023 int max_num_labels;
24 int *label_offsets;
25 int num_locals;
26 uint push_reglist;
27 uint stack_adjust;
28};
29
Damien5bfb7592013-10-05 18:41:24 +010030asm_thumb_t *asm_thumb_new(uint max_num_labels) {
Damien429d7192013-10-04 19:53:11 +010031 asm_thumb_t *as;
32
33 as = m_new(asm_thumb_t, 1);
34 as->pass = 0;
35 as->code_offset = 0;
36 as->code_size = 0;
37 as->code_base = NULL;
Damien5bfb7592013-10-05 18:41:24 +010038 as->max_num_labels = max_num_labels;
39 as->label_offsets = m_new(int, max_num_labels);
Damien429d7192013-10-04 19:53:11 +010040 as->num_locals = 0;
41
42 return as;
43}
44
45void asm_thumb_free(asm_thumb_t *as, bool free_code) {
46 if (free_code) {
47 m_free(as->code_base);
48 }
49 /*
50 if (as->label != NULL) {
51 int i;
52 for (i = 0; i < as->label->len; ++i)
53 {
54 Label *lab = &g_array_index(as->label, Label, i);
55 if (lab->unresolved != NULL)
56 g_array_free(lab->unresolved, true);
57 }
58 g_array_free(as->label, true);
59 }
60 */
61 m_free(as);
62}
63
64void asm_thumb_start_pass(asm_thumb_t *as, int pass) {
65 as->pass = pass;
66 as->code_offset = 0;
Damien5bfb7592013-10-05 18:41:24 +010067 if (pass == ASM_THUMB_PASS_2) {
68 memset(as->label_offsets, -1, as->max_num_labels * sizeof(int));
Damien429d7192013-10-04 19:53:11 +010069 }
70}
71
72void asm_thumb_end_pass(asm_thumb_t *as) {
Damien5bfb7592013-10-05 18:41:24 +010073 if (as->pass == ASM_THUMB_PASS_2) {
Damien429d7192013-10-04 19:53:11 +010074 // calculate size of code in bytes
75 as->code_size = as->code_offset;
76 as->code_base = m_new(byte, as->code_size);
77 printf("code_size: %u\n", as->code_size);
78 }
79
80 /*
81 // check labels are resolved
82 if (as->label != NULL)
83 {
84 int i;
85 for (i = 0; i < as->label->len; ++i)
86 if (g_array_index(as->label, Label, i).unresolved != NULL)
87 return false;
88 }
89 */
90}
91
92// all functions must go through this one to emit bytes
93static byte *asm_thumb_get_cur_to_write_bytes(asm_thumb_t *as, int num_bytes_to_write) {
94 //printf("emit %d\n", num_bytes_to_write);
95 if (as->pass < ASM_THUMB_PASS_3) {
96 as->code_offset += num_bytes_to_write;
97 return as->dummy_data;
98 } else {
99 assert(as->code_offset + num_bytes_to_write <= as->code_size);
100 byte *c = as->code_base + as->code_offset;
101 as->code_offset += num_bytes_to_write;
102 return c;
103 }
104}
105
106uint asm_thumb_get_code_size(asm_thumb_t *as) {
107 return as->code_size;
108}
109
110void *asm_thumb_get_code(asm_thumb_t *as) {
111 // need to set low bit to indicate that it's thumb code
112 return (void *)(((machine_uint_t)as->code_base) | 1);
113}
114
115/*
116static void asm_thumb_write_byte_1(asm_thumb_t *as, byte b1) {
117 byte *c = asm_thumb_get_cur_to_write_bytes(as, 1);
118 c[0] = b1;
119}
120*/
121
122static void asm_thumb_write_op16(asm_thumb_t *as, uint op) {
123 byte *c = asm_thumb_get_cur_to_write_bytes(as, 2);
124 // little endian
125 c[0] = op;
126 c[1] = op >> 8;
127}
128
129static void asm_thumb_write_op32(asm_thumb_t *as, uint op1, uint op2) {
130 byte *c = asm_thumb_get_cur_to_write_bytes(as, 4);
131 // little endian, op1 then op2
132 c[0] = op1;
133 c[1] = op1 >> 8;
134 c[2] = op2;
135 c[3] = op2 >> 8;
136}
137
138/*
139#define IMM32_L0(x) ((x) & 0xff)
140#define IMM32_L1(x) (((x) >> 8) & 0xff)
141#define IMM32_L2(x) (((x) >> 16) & 0xff)
142#define IMM32_L3(x) (((x) >> 24) & 0xff)
143
144static void asm_thumb_write_word32(asm_thumb_t *as, int w32) {
145 byte *c = asm_thumb_get_cur_to_write_bytes(as, 4);
146 c[0] = IMM32_L0(w32);
147 c[1] = IMM32_L1(w32);
148 c[2] = IMM32_L2(w32);
149 c[3] = IMM32_L3(w32);
150}
151*/
152
153// rlolist is a bit map indicating desired lo-registers
154#define OP_PUSH_RLIST(rlolist) (0xb400 | (rlolist))
155#define OP_PUSH_RLIST_LR(rlolist) (0xb400 | 0x0100 | (rlolist))
156#define OP_POP_RLIST(rlolist) (0xbc00 | (rlolist))
157#define OP_POP_RLIST_PC(rlolist) (0xbc00 | 0x0100 | (rlolist))
158
159#define OP_ADD_SP(num_words) (0xb000 | (num_words))
160#define OP_SUB_SP(num_words) (0xb080 | (num_words))
161
162void asm_thumb_entry(asm_thumb_t *as, int num_locals) {
163 // work out what to push and how many extra space to reserve on stack
164 // so that we have enough for all locals and it's aligned an 8-byte boundary
165 uint reglist;
166 uint stack_adjust;
167 if (num_locals < 0) {
168 num_locals = 0;
169 }
170 // don't ppop r0 because it's used for return value
171 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 }
197 asm_thumb_write_op16(as, OP_PUSH_RLIST_LR(reglist));
198 if (stack_adjust > 0) {
199 asm_thumb_write_op16(as, OP_SUB_SP(stack_adjust));
200 }
201 as->push_reglist = reglist;
202 as->stack_adjust = stack_adjust;
203 as->num_locals = num_locals;
204}
205
206void asm_thumb_exit(asm_thumb_t *as) {
207 if (as->stack_adjust > 0) {
208 asm_thumb_write_op16(as, OP_ADD_SP(as->stack_adjust));
209 }
210 asm_thumb_write_op16(as, OP_POP_RLIST_PC(as->push_reglist));
211}
212
Damien429d7192013-10-04 19:53:11 +0100213void asm_thumb_label_assign(asm_thumb_t *as, int label) {
Damien5bfb7592013-10-05 18:41:24 +0100214 assert(label < as->max_num_labels);
215 if (as->pass == ASM_THUMB_PASS_2) {
216 // assign label offset
217 assert(as->label_offsets[label] == -1);
218 as->label_offsets[label] = as->code_offset;
219 } else if (as->pass == ASM_THUMB_PASS_3) {
220 // ensure label offset has not changed from PASS_2 to PASS_3
221 //printf("l%d: (at %d=%ld)\n", label, as->label_offsets[label], as->code_offset);
222 assert(as->label_offsets[label] == as->code_offset);
Damien429d7192013-10-04 19:53:11 +0100223 }
224}
225
Damien5bfb7592013-10-05 18:41:24 +0100226static int get_label_dest(asm_thumb_t *as, int label) {
227 assert(label < as->max_num_labels);
228 return as->label_offsets[label];
229}
230
Damien826005c2013-10-05 23:17:28 +0100231#define OP_MOVS_RLO_I8(rlo_dest, i8_src) (0x2000 | ((rlo_dest) << 8) | (i8_src))
232
233// the i8_src value will be zero extended into the r32 register!
234void asm_thumb_movs_rlo_i8(asm_thumb_t *as, uint rlo_dest, int i8_src) {
Damien429d7192013-10-04 19:53:11 +0100235 assert(rlo_dest < REG_R8);
Damien826005c2013-10-05 23:17:28 +0100236 // movs rlo_dest, #i8_src
237 asm_thumb_write_op16(as, OP_MOVS_RLO_I8(rlo_dest, i8_src));
Damien429d7192013-10-04 19:53:11 +0100238}
239
Damien826005c2013-10-05 23:17:28 +0100240#define OP_MOVW (0xf240)
241#define OP_MOVT (0xf2c0)
242
243// if loading lo half with movw, the i16 value will be zero extended into the r32 register!
244static 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 +0100245 assert(reg_dest < REG_R15);
Damien826005c2013-10-05 23:17:28 +0100246 // mov[wt] reg_dest, #i16_src
247 asm_thumb_write_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 +0100248}
249
Damien826005c2013-10-05 23:17:28 +0100250// the i16_src value will be zero extended into the r32 register!
251void asm_thumb_movw_reg_i16(asm_thumb_t *as, uint reg_dest, int i16_src) {
252 asm_thumb_mov_reg_i16(as, OP_MOVW, reg_dest, i16_src);
Damien429d7192013-10-04 19:53:11 +0100253}
254
Damien826005c2013-10-05 23:17:28 +0100255// the i16_src value will be zero extended into the r32 register!
256void asm_thumb_movt_reg_i16(asm_thumb_t *as, uint reg_dest, int i16_src) {
257 asm_thumb_mov_reg_i16(as, OP_MOVT, reg_dest, i16_src);
Damien429d7192013-10-04 19:53:11 +0100258}
259
260void asm_thumb_mov_reg_reg(asm_thumb_t *as, uint reg_dest, uint reg_src) {
261 uint op_lo;
262 if (reg_src < 8) {
263 op_lo = reg_src << 3;
264 } else {
265 op_lo = 0x40 | ((reg_src - 8) << 3);
266 }
267 if (reg_dest < 8) {
268 op_lo |= reg_dest;
269 } else {
270 op_lo |= 0x80 | (reg_dest - 8);
271 }
Damien826005c2013-10-05 23:17:28 +0100272 // mov reg_dest, reg_src
Damien429d7192013-10-04 19:53:11 +0100273 asm_thumb_write_op16(as, 0x4600 | op_lo);
274}
275
Damien826005c2013-10-05 23:17:28 +0100276#define OP_SUBS_RLO_RLO_I3(rlo_dest, rlo_src, i3_src) (0x1e00 | ((i3_src) << 6) | ((rlo_src) << 3) | (rlo_dest))
277
278void asm_thumb_subs_rlo_rlo_i3(asm_thumb_t *as, uint rlo_dest, uint rlo_src, int i3_src) {
279 assert(rlo_dest < REG_R8);
280 assert(rlo_src < REG_R8);
281 asm_thumb_write_op16(as, OP_SUBS_RLO_RLO_I3(rlo_dest, rlo_src, i3_src));
282}
283
284#define OP_CMP_RLO_I8(rlo, i8) (0x2800 | ((rlo) << 8) | (i8))
285
286void asm_thumb_cmp_rlo_i8(asm_thumb_t *as, uint rlo, int i8) {
287 assert(rlo < REG_R8);
288 asm_thumb_write_op16(as, OP_CMP_RLO_I8(rlo, i8));
289}
290
Damien03d41242013-10-06 00:36:05 +0100291#define OP_B_N(byte_offset) (0xe000 | (((byte_offset) >> 1) & 0x07ff))
292
293void asm_thumb_b_n(asm_thumb_t *as, int label) {
294 int dest = get_label_dest(as, label);
295 int rel = dest - as->code_offset;
296 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
297 if (SIGNED_FIT12(rel)) {
298 asm_thumb_write_op16(as, OP_B_N(rel));
299 } else {
300 printf("asm_thumb_b_n: branch does not fit in 12 bits\n");
301 }
302}
303
Damien826005c2013-10-05 23:17:28 +0100304#define OP_BEQ_N(byte_offset) (0xd000 | (((byte_offset) >> 1) & 0x00ff))
305#define OP_BNE_N(byte_offset) (0xd100 | (((byte_offset) >> 1) & 0x00ff))
306#define OP_BCS_N(byte_offset) (0xd200 | (((byte_offset) >> 1) & 0x00ff))
307#define OP_BCC_N(byte_offset) (0xd300 | (((byte_offset) >> 1) & 0x00ff))
308#define OP_BMI_N(byte_offset) (0xd400 | (((byte_offset) >> 1) & 0x00ff))
309#define OP_BPL_N(byte_offset) (0xd500 | (((byte_offset) >> 1) & 0x00ff))
310#define OP_BVS_N(byte_offset) (0xd600 | (((byte_offset) >> 1) & 0x00ff))
311#define OP_BVC_N(byte_offset) (0xd700 | (((byte_offset) >> 1) & 0x00ff))
312#define OP_BHI_N(byte_offset) (0xd800 | (((byte_offset) >> 1) & 0x00ff))
313#define OP_BLS_N(byte_offset) (0xd900 | (((byte_offset) >> 1) & 0x00ff))
314#define OP_BGE_N(byte_offset) (0xda00 | (((byte_offset) >> 1) & 0x00ff))
315#define OP_BLT_N(byte_offset) (0xdb00 | (((byte_offset) >> 1) & 0x00ff))
316#define OP_BGT_N(byte_offset) (0xdc00 | (((byte_offset) >> 1) & 0x00ff))
317#define OP_BLE_N(byte_offset) (0xdd00 | (((byte_offset) >> 1) & 0x00ff))
318
319void asm_thumb_bgt_n(asm_thumb_t *as, int label) {
320 int dest = get_label_dest(as, label);
321 int rel = dest - as->code_offset;
322 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
323 if (SIGNED_FIT9(rel)) {
324 asm_thumb_write_op16(as, OP_BGT_N(rel));
325 } else {
326 printf("asm_thumb_bgt: branch does not fit in 9 bits\n");
327 }
328}
329
330void asm_thumb_mov_reg_i32(asm_thumb_t *as, uint reg_dest, machine_uint_t i32) {
331 // movw, movt does it in 8 bytes
332 // ldr [pc, #], dw does it in 6 bytes, but we might not reach to end of code for dw
333
334 asm_thumb_mov_reg_i16(as, OP_MOVW, reg_dest, i32);
335 asm_thumb_mov_reg_i16(as, OP_MOVT, reg_dest, i32 >> 16);
336}
337
338void asm_thumb_mov_reg_i32_optimised(asm_thumb_t *as, uint reg_dest, int i32) {
339 if (reg_dest < 8 && UNSIGNED_FIT8(i32)) {
340 asm_thumb_movs_rlo_i8(as, reg_dest, i32);
341 } else if (UNSIGNED_FIT16(i32)) {
342 asm_thumb_mov_reg_i16(as, OP_MOVW, reg_dest, i32);
343 } else {
344 asm_thumb_mov_reg_i32(as, reg_dest, i32);
345 }
346}
347
Damien429d7192013-10-04 19:53:11 +0100348#define OP_STR_TO_SP_OFFSET(rlo_dest, word_offset) (0x9000 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
349#define OP_LDR_FROM_SP_OFFSET(rlo_dest, word_offset) (0x9800 | ((rlo_dest) << 8) | ((word_offset) & 0x00ff))
350
351void asm_thumb_mov_local_reg(asm_thumb_t *as, int local_num, uint rlo_src) {
352 assert(rlo_src < REG_R8);
353 int word_offset = as->num_locals - local_num - 1;
354 assert(as->pass < ASM_THUMB_PASS_3 || word_offset >= 0);
355 asm_thumb_write_op16(as, OP_STR_TO_SP_OFFSET(rlo_src, word_offset));
356}
357
358void asm_thumb_mov_reg_local(asm_thumb_t *as, uint rlo_dest, int local_num) {
359 assert(rlo_dest < REG_R8);
360 int word_offset = as->num_locals - local_num - 1;
361 assert(as->pass < ASM_THUMB_PASS_3 || word_offset >= 0);
362 asm_thumb_write_op16(as, OP_LDR_FROM_SP_OFFSET(rlo_dest, word_offset));
363}
364
365void asm_thumb_mov_reg_local_addr(asm_thumb_t *as, uint reg_dest, int local_num) {
366 assert(0);
367 // see format 12, load address
368 asm_thumb_write_op16(as, 0x0000);
369}
370
371#define OP_ADD_REG_REG_REG(rlo_dest, rlo_src_a, rlo_src_b) (0x1800 | ((rlo_src_b) << 6) | ((rlo_src_a) << 3) | (rlo_dest))
372
373void asm_thumb_add_reg_reg_reg(asm_thumb_t *as, uint rlo_dest, uint rlo_src_a, uint rlo_src_b) {
374 asm_thumb_write_op16(as, OP_ADD_REG_REG_REG(rlo_dest, rlo_src_a, rlo_src_b));
375}
376
377#define OP_CMP_REG_REG(rlo_a, rlo_b) (0x4280 | ((rlo_b) << 3) | (rlo_a))
378
379void asm_thumb_cmp_reg_reg(asm_thumb_t *as, uint rlo_a, uint rlo_b) {
380 asm_thumb_write_op16(as, OP_CMP_REG_REG(rlo_a, rlo_b));
381}
382
383void asm_thumb_ite_ge(asm_thumb_t *as) {
384 asm_thumb_write_op16(as, 0xbfac);
385}
386
Damien429d7192013-10-04 19:53:11 +0100387// this could be wrong, because it should have a range of +/- 16MiB...
388#define OP_BW_HI(byte_offset) (0xf000 | (((byte_offset) >> 12) & 0x07ff))
389#define OP_BW_LO(byte_offset) (0xb800 | (((byte_offset) >> 1) & 0x07ff))
390
391void asm_thumb_b_label(asm_thumb_t *as, int label) {
Damien5bfb7592013-10-05 18:41:24 +0100392 int dest = get_label_dest(as, label);
393 int rel = dest - as->code_offset;
394 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
395 if (dest >= 0 && rel <= -4) {
396 // is a backwards jump, so we know the size of the jump on the first pass
397 // calculate rel assuming 12 bit relative jump
398 if (SIGNED_FIT12(rel)) {
Damien03d41242013-10-06 00:36:05 +0100399 asm_thumb_write_op16(as, OP_B_N(rel));
Damien429d7192013-10-04 19:53:11 +0100400 } else {
Damien5bfb7592013-10-05 18:41:24 +0100401 goto large_jump;
Damien429d7192013-10-04 19:53:11 +0100402 }
Damien5bfb7592013-10-05 18:41:24 +0100403 } else {
404 // is a forwards jump, so need to assume it's large
405 large_jump:
406 asm_thumb_write_op32(as, OP_BW_HI(rel), OP_BW_LO(rel));
Damien429d7192013-10-04 19:53:11 +0100407 }
408}
409
Damien429d7192013-10-04 19:53:11 +0100410// all these bit arithmetics need coverage testing!
411#define OP_BEQ(byte_offset) (0xd000 | (((byte_offset) >> 1) & 0x00ff))
412#define OP_BEQW_HI(byte_offset) (0xf000 | (((byte_offset) >> 10) & 0x0400) | (((byte_offset) >> 14) & 0x003f))
413#define OP_BEQW_LO(byte_offset) (0x8000 | ((byte_offset) & 0x2000) | (((byte_offset) >> 1) & 0x0fff))
414
415void asm_thumb_cmp_reg_bz_label(asm_thumb_t *as, uint rlo, int label) {
416 assert(rlo < REG_R8);
417
418 // compare reg with 0
Damien826005c2013-10-05 23:17:28 +0100419 asm_thumb_write_op16(as, OP_CMP_RLO_I8(rlo, 0));
Damien429d7192013-10-04 19:53:11 +0100420
421 // branch if equal
Damien5bfb7592013-10-05 18:41:24 +0100422 int dest = get_label_dest(as, label);
423 int rel = dest - as->code_offset;
424 rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction
425 if (dest >= 0 && rel <= -4) {
426 // is a backwards jump, so we know the size of the jump on the first pass
Damien826005c2013-10-05 23:17:28 +0100427 // calculate rel assuming 9 bit relative jump
Damien5bfb7592013-10-05 18:41:24 +0100428 if (SIGNED_FIT9(rel)) {
429 asm_thumb_write_op16(as, OP_BEQ(rel));
Damien429d7192013-10-04 19:53:11 +0100430 } else {
Damien5bfb7592013-10-05 18:41:24 +0100431 goto large_jump;
Damien429d7192013-10-04 19:53:11 +0100432 }
Damien5bfb7592013-10-05 18:41:24 +0100433 } else {
434 // is a forwards jump, so need to assume it's large
435 large_jump:
436 asm_thumb_write_op32(as, OP_BEQW_HI(rel), OP_BEQW_LO(rel));
Damien429d7192013-10-04 19:53:11 +0100437 }
438}
439
440#define OP_BLX(reg) (0x4780 | ((reg) << 3))
441#define OP_SVC(arg) (0xdf00 | (arg))
442#define OP_LDR_FROM_BASE_OFFSET(rlo_dest, rlo_base, word_offset) (0x6800 | (((word_offset) << 6) & 0x07c0) | ((rlo_base) << 3) | (rlo_dest))
443
444void asm_thumb_bl_ind(asm_thumb_t *as, void *fun_ptr, uint fun_id, uint reg_temp) {
445 /* TODO make this use less bytes
446 uint rlo_base = REG_R3;
447 uint rlo_dest = REG_R7;
448 uint word_offset = 4;
449 asm_thumb_write_op16(as, 0x0000);
450 asm_thumb_write_op16(as, 0x6800 | (word_offset << 6) | (rlo_base << 3) | rlo_dest); // ldr rlo_dest, [rlo_base, #offset]
451 asm_thumb_write_op16(as, 0x4780 | (REG_R9 << 3)); // blx reg
452 */
453
454 if (0) {
455 // load ptr to function into register using immediate, then branch
456 // not relocatable
457 asm_thumb_mov_reg_i32(as, reg_temp, (machine_uint_t)fun_ptr);
458 asm_thumb_write_op16(as, OP_BLX(reg_temp));
459 } else if (1) {
460 asm_thumb_write_op16(as, OP_LDR_FROM_BASE_OFFSET(reg_temp, REG_R7, fun_id));
461 asm_thumb_write_op16(as, OP_BLX(reg_temp));
462 } else {
463 // use SVC
464 asm_thumb_write_op16(as, OP_SVC(fun_id));
465 }
466}