riscv: TLC of param passing
[tinycc.git] / arm64-gen.c
blob5606271cf55aa0c8416a741dfc484b5095d1ea86
1 /*
2 * A64 code generator for TCC
4 * Copyright (c) 2014-2015 Edmund Grimley Evans
6 * Copying and distribution of this file, with or without modification,
7 * are permitted in any medium without royalty provided the copyright
8 * notice and this notice are preserved. This file is offered as-is,
9 * without any warranty.
12 #ifdef TARGET_DEFS_ONLY
14 // Number of registers available to allocator:
15 #define NB_REGS 28 // x0-x18, x30, v0-v7
17 #define TREG_R(x) (x) // x = 0..18
18 #define TREG_R30 19
19 #define TREG_F(x) (x + 20) // x = 0..7
21 // Register classes sorted from more general to more precise:
22 #define RC_INT (1 << 0)
23 #define RC_FLOAT (1 << 1)
24 #define RC_R(x) (1 << (2 + (x))) // x = 0..18
25 #define RC_R30 (1 << 21)
26 #define RC_F(x) (1 << (22 + (x))) // x = 0..7
28 #define RC_IRET (RC_R(0)) // int return register class
29 #define RC_FRET (RC_F(0)) // float return register class
31 #define REG_IRET (TREG_R(0)) // int return register number
32 #define REG_FRET (TREG_F(0)) // float return register number
34 #define PTR_SIZE 8
36 #define LDOUBLE_SIZE 16
37 #define LDOUBLE_ALIGN 16
39 #define MAX_ALIGN 16
41 #define CHAR_IS_UNSIGNED
43 /******************************************************/
44 #else /* ! TARGET_DEFS_ONLY */
45 /******************************************************/
46 #include "tcc.h"
47 #include <assert.h>
49 ST_DATA const int reg_classes[NB_REGS] = {
50 RC_INT | RC_R(0),
51 RC_INT | RC_R(1),
52 RC_INT | RC_R(2),
53 RC_INT | RC_R(3),
54 RC_INT | RC_R(4),
55 RC_INT | RC_R(5),
56 RC_INT | RC_R(6),
57 RC_INT | RC_R(7),
58 RC_INT | RC_R(8),
59 RC_INT | RC_R(9),
60 RC_INT | RC_R(10),
61 RC_INT | RC_R(11),
62 RC_INT | RC_R(12),
63 RC_INT | RC_R(13),
64 RC_INT | RC_R(14),
65 RC_INT | RC_R(15),
66 RC_INT | RC_R(16),
67 RC_INT | RC_R(17),
68 RC_INT | RC_R(18),
69 RC_R30, // not in RC_INT as we make special use of x30
70 RC_FLOAT | RC_F(0),
71 RC_FLOAT | RC_F(1),
72 RC_FLOAT | RC_F(2),
73 RC_FLOAT | RC_F(3),
74 RC_FLOAT | RC_F(4),
75 RC_FLOAT | RC_F(5),
76 RC_FLOAT | RC_F(6),
77 RC_FLOAT | RC_F(7)
80 #define IS_FREG(x) ((x) >= TREG_F(0))
82 static uint32_t intr(int r)
84 assert(TREG_R(0) <= r && r <= TREG_R30);
85 return r < TREG_R30 ? r : 30;
88 static uint32_t fltr(int r)
90 assert(TREG_F(0) <= r && r <= TREG_F(7));
91 return r - TREG_F(0);
94 // Add an instruction to text section:
95 ST_FUNC void o(unsigned int c)
97 int ind1 = ind + 4;
98 if (nocode_wanted)
99 return;
100 if (ind1 > cur_text_section->data_allocated)
101 section_realloc(cur_text_section, ind1);
102 write32le(cur_text_section->data + ind, c);
103 ind = ind1;
106 static int arm64_encode_bimm64(uint64_t x)
108 int neg = x & 1;
109 int rep, pos, len;
111 if (neg)
112 x = ~x;
113 if (!x)
114 return -1;
116 if (x >> 2 == (x & (((uint64_t)1 << (64 - 2)) - 1)))
117 rep = 2, x &= ((uint64_t)1 << 2) - 1;
118 else if (x >> 4 == (x & (((uint64_t)1 << (64 - 4)) - 1)))
119 rep = 4, x &= ((uint64_t)1 << 4) - 1;
120 else if (x >> 8 == (x & (((uint64_t)1 << (64 - 8)) - 1)))
121 rep = 8, x &= ((uint64_t)1 << 8) - 1;
122 else if (x >> 16 == (x & (((uint64_t)1 << (64 - 16)) - 1)))
123 rep = 16, x &= ((uint64_t)1 << 16) - 1;
124 else if (x >> 32 == (x & (((uint64_t)1 << (64 - 32)) - 1)))
125 rep = 32, x &= ((uint64_t)1 << 32) - 1;
126 else
127 rep = 64;
129 pos = 0;
130 if (!(x & (((uint64_t)1 << 32) - 1))) x >>= 32, pos += 32;
131 if (!(x & (((uint64_t)1 << 16) - 1))) x >>= 16, pos += 16;
132 if (!(x & (((uint64_t)1 << 8) - 1))) x >>= 8, pos += 8;
133 if (!(x & (((uint64_t)1 << 4) - 1))) x >>= 4, pos += 4;
134 if (!(x & (((uint64_t)1 << 2) - 1))) x >>= 2, pos += 2;
135 if (!(x & (((uint64_t)1 << 1) - 1))) x >>= 1, pos += 1;
137 len = 0;
138 if (!(~x & (((uint64_t)1 << 32) - 1))) x >>= 32, len += 32;
139 if (!(~x & (((uint64_t)1 << 16) - 1))) x >>= 16, len += 16;
140 if (!(~x & (((uint64_t)1 << 8) - 1))) x >>= 8, len += 8;
141 if (!(~x & (((uint64_t)1 << 4) - 1))) x >>= 4, len += 4;
142 if (!(~x & (((uint64_t)1 << 2) - 1))) x >>= 2, len += 2;
143 if (!(~x & (((uint64_t)1 << 1) - 1))) x >>= 1, len += 1;
145 if (x)
146 return -1;
147 if (neg) {
148 pos = (pos + len) & (rep - 1);
149 len = rep - len;
151 return ((0x1000 & rep << 6) | (((rep - 1) ^ 31) << 1 & 63) |
152 ((rep - pos) & (rep - 1)) << 6 | (len - 1));
155 static uint32_t arm64_movi(int r, uint64_t x)
157 uint64_t m = 0xffff;
158 int e;
159 if (!(x & ~m))
160 return 0x52800000 | r | x << 5; // movz w(r),#(x)
161 if (!(x & ~(m << 16)))
162 return 0x52a00000 | r | x >> 11; // movz w(r),#(x >> 16),lsl #16
163 if (!(x & ~(m << 32)))
164 return 0xd2c00000 | r | x >> 27; // movz x(r),#(x >> 32),lsl #32
165 if (!(x & ~(m << 48)))
166 return 0xd2e00000 | r | x >> 43; // movz x(r),#(x >> 48),lsl #48
167 if ((x & ~m) == m << 16)
168 return (0x12800000 | r |
169 (~x << 5 & 0x1fffe0)); // movn w(r),#(~x)
170 if ((x & ~(m << 16)) == m)
171 return (0x12a00000 | r |
172 (~x >> 11 & 0x1fffe0)); // movn w(r),#(~x >> 16),lsl #16
173 if (!~(x | m))
174 return (0x92800000 | r |
175 (~x << 5 & 0x1fffe0)); // movn x(r),#(~x)
176 if (!~(x | m << 16))
177 return (0x92a00000 | r |
178 (~x >> 11 & 0x1fffe0)); // movn x(r),#(~x >> 16),lsl #16
179 if (!~(x | m << 32))
180 return (0x92c00000 | r |
181 (~x >> 27 & 0x1fffe0)); // movn x(r),#(~x >> 32),lsl #32
182 if (!~(x | m << 48))
183 return (0x92e00000 | r |
184 (~x >> 43 & 0x1fffe0)); // movn x(r),#(~x >> 32),lsl #32
185 if (!(x >> 32) && (e = arm64_encode_bimm64(x | x << 32)) >= 0)
186 return 0x320003e0 | r | (uint32_t)e << 10; // movi w(r),#(x)
187 if ((e = arm64_encode_bimm64(x)) >= 0)
188 return 0xb20003e0 | r | (uint32_t)e << 10; // movi x(r),#(x)
189 return 0;
192 static void arm64_movimm(int r, uint64_t x)
194 uint32_t i;
195 if ((i = arm64_movi(r, x)))
196 o(i); // a single MOV
197 else {
198 // MOVZ/MOVN and 1-3 MOVKs
199 int z = 0, m = 0;
200 uint32_t mov1 = 0xd2800000; // movz
201 uint64_t x1 = x;
202 for (i = 0; i < 64; i += 16) {
203 z += !(x >> i & 0xffff);
204 m += !(~x >> i & 0xffff);
206 if (m > z) {
207 x1 = ~x;
208 mov1 = 0x92800000; // movn
210 for (i = 0; i < 64; i += 16)
211 if (x1 >> i & 0xffff) {
212 o(mov1 | r | (x1 >> i & 0xffff) << 5 | i << 17);
213 // movz/movn x(r),#(*),lsl #(i)
214 break;
216 for (i += 16; i < 64; i += 16)
217 if (x1 >> i & 0xffff)
218 o(0xf2800000 | r | (x >> i & 0xffff) << 5 | i << 17);
219 // movk x(r),#(*),lsl #(i)
223 // Patch all branches in list pointed to by t to branch to a:
224 ST_FUNC void gsym_addr(int t_, int a_)
226 uint32_t t = t_;
227 uint32_t a = a_;
228 while (t) {
229 unsigned char *ptr = cur_text_section->data + t;
230 uint32_t next = read32le(ptr);
231 if (a - t + 0x8000000 >= 0x10000000)
232 tcc_error("branch out of range");
233 write32le(ptr, (a - t == 4 ? 0xd503201f : // nop
234 0x14000000 | ((a - t) >> 2 & 0x3ffffff))); // b
235 t = next;
239 static int arm64_type_size(int t)
241 switch (t & VT_BTYPE) {
242 case VT_INT: return 2;
243 case VT_BYTE: return 0;
244 case VT_SHORT: return 1;
245 case VT_PTR: return 3;
246 case VT_FUNC: return 3;
247 case VT_FLOAT: return 2;
248 case VT_DOUBLE: return 3;
249 case VT_LDOUBLE: return 4;
250 case VT_BOOL: return 0;
251 case VT_LLONG: return 3;
253 assert(0);
254 return 0;
257 static void arm64_spoff(int reg, uint64_t off)
259 uint32_t sub = off >> 63;
260 if (sub)
261 off = -off;
262 if (off < 4096)
263 o(0x910003e0 | sub << 30 | reg | off << 10);
264 // (add|sub) x(reg),sp,#(off)
265 else {
266 arm64_movimm(30, off); // use x30 for offset
267 o(0x8b3e63e0 | sub << 30 | reg); // (add|sub) x(reg),sp,x30
271 static void arm64_ldrx(int sg, int sz_, int dst, int bas, uint64_t off)
273 uint32_t sz = sz_;
274 if (sz >= 2)
275 sg = 0;
276 if (!(off & ~((uint32_t)0xfff << sz)))
277 o(0x39400000 | dst | bas << 5 | off << (10 - sz) |
278 (uint32_t)!!sg << 23 | sz << 30); // ldr(*) x(dst),[x(bas),#(off)]
279 else if (off < 256 || -off <= 256)
280 o(0x38400000 | dst | bas << 5 | (off & 511) << 12 |
281 (uint32_t)!!sg << 23 | sz << 30); // ldur(*) x(dst),[x(bas),#(off)]
282 else {
283 arm64_movimm(30, off); // use x30 for offset
284 o(0x38206800 | dst | bas << 5 | (uint32_t)30 << 16 |
285 (uint32_t)(!!sg + 1) << 22 | sz << 30); // ldr(*) x(dst),[x(bas),x30]
289 static void arm64_ldrv(int sz_, int dst, int bas, uint64_t off)
291 uint32_t sz = sz_;
292 if (!(off & ~((uint32_t)0xfff << sz)))
293 o(0x3d400000 | dst | bas << 5 | off << (10 - sz) |
294 (sz & 4) << 21 | (sz & 3) << 30); // ldr (s|d|q)(dst),[x(bas),#(off)]
295 else if (off < 256 || -off <= 256)
296 o(0x3c400000 | dst | bas << 5 | (off & 511) << 12 |
297 (sz & 4) << 21 | (sz & 3) << 30); // ldur (s|d|q)(dst),[x(bas),#(off)]
298 else {
299 arm64_movimm(30, off); // use x30 for offset
300 o(0x3c606800 | dst | bas << 5 | (uint32_t)30 << 16 |
301 sz << 30 | (sz & 4) << 21); // ldr (s|d|q)(dst),[x(bas),x30]
305 static void arm64_ldrs(int reg_, int size)
307 uint32_t reg = reg_;
308 // Use x30 for intermediate value in some cases.
309 switch (size) {
310 default: assert(0); break;
311 case 1:
312 arm64_ldrx(0, 0, reg, reg, 0);
313 break;
314 case 2:
315 arm64_ldrx(0, 1, reg, reg, 0);
316 break;
317 case 3:
318 arm64_ldrx(0, 1, 30, reg, 0);
319 arm64_ldrx(0, 0, reg, reg, 2);
320 o(0x2a0043c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #16
321 break;
322 case 4:
323 arm64_ldrx(0, 2, reg, reg, 0);
324 break;
325 case 5:
326 arm64_ldrx(0, 2, 30, reg, 0);
327 arm64_ldrx(0, 0, reg, reg, 4);
328 o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32
329 break;
330 case 6:
331 arm64_ldrx(0, 2, 30, reg, 0);
332 arm64_ldrx(0, 1, reg, reg, 4);
333 o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32
334 break;
335 case 7:
336 arm64_ldrx(0, 2, 30, reg, 0);
337 arm64_ldrx(0, 2, reg, reg, 3);
338 o(0x53087c00 | reg | reg << 5); // lsr w(reg), w(reg), #8
339 o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32
340 break;
341 case 8:
342 arm64_ldrx(0, 3, reg, reg, 0);
343 break;
344 case 9:
345 arm64_ldrx(0, 0, reg + 1, reg, 8);
346 arm64_ldrx(0, 3, reg, reg, 0);
347 break;
348 case 10:
349 arm64_ldrx(0, 1, reg + 1, reg, 8);
350 arm64_ldrx(0, 3, reg, reg, 0);
351 break;
352 case 11:
353 arm64_ldrx(0, 2, reg + 1, reg, 7);
354 o(0x53087c00 | (reg+1) | (reg+1) << 5); // lsr w(reg+1), w(reg+1), #8
355 arm64_ldrx(0, 3, reg, reg, 0);
356 break;
357 case 12:
358 arm64_ldrx(0, 2, reg + 1, reg, 8);
359 arm64_ldrx(0, 3, reg, reg, 0);
360 break;
361 case 13:
362 arm64_ldrx(0, 3, reg + 1, reg, 5);
363 o(0xd358fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #24
364 arm64_ldrx(0, 3, reg, reg, 0);
365 break;
366 case 14:
367 arm64_ldrx(0, 3, reg + 1, reg, 6);
368 o(0xd350fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #16
369 arm64_ldrx(0, 3, reg, reg, 0);
370 break;
371 case 15:
372 arm64_ldrx(0, 3, reg + 1, reg, 7);
373 o(0xd348fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #8
374 arm64_ldrx(0, 3, reg, reg, 0);
375 break;
376 case 16:
377 o(0xa9400000 | reg | (reg+1) << 10 | reg << 5);
378 // ldp x(reg),x(reg+1),[x(reg)]
379 break;
383 static void arm64_strx(int sz_, int dst, int bas, uint64_t off)
385 uint32_t sz = sz_;
386 if (!(off & ~((uint32_t)0xfff << sz)))
387 o(0x39000000 | dst | bas << 5 | off << (10 - sz) | sz << 30);
388 // str(*) x(dst),[x(bas],#(off)]
389 else if (off < 256 || -off <= 256)
390 o(0x38000000 | dst | bas << 5 | (off & 511) << 12 | sz << 30);
391 // stur(*) x(dst),[x(bas],#(off)]
392 else {
393 arm64_movimm(30, off); // use x30 for offset
394 o(0x38206800 | dst | bas << 5 | (uint32_t)30 << 16 | sz << 30);
395 // str(*) x(dst),[x(bas),x30]
399 static void arm64_strv(int sz_, int dst, int bas, uint64_t off)
401 uint32_t sz = sz_;
402 if (!(off & ~((uint32_t)0xfff << sz)))
403 o(0x3d000000 | dst | bas << 5 | off << (10 - sz) |
404 (sz & 4) << 21 | (sz & 3) << 30); // str (s|d|q)(dst),[x(bas),#(off)]
405 else if (off < 256 || -off <= 256)
406 o(0x3c000000 | dst | bas << 5 | (off & 511) << 12 |
407 (sz & 4) << 21 | (sz & 3) << 30); // stur (s|d|q)(dst),[x(bas),#(off)]
408 else {
409 arm64_movimm(30, off); // use x30 for offset
410 o(0x3c206800 | dst | bas << 5 | (uint32_t)30 << 16 |
411 sz << 30 | (sz & 4) << 21); // str (s|d|q)(dst),[x(bas),x30]
415 static void arm64_sym(int r, Sym *sym, unsigned long addend)
417 // Currently TCC's linker does not generate COPY relocations for
418 // STT_OBJECTs when tcc is invoked with "-run". This typically
419 // results in "R_AARCH64_ADR_PREL_PG_HI21 relocation failed" when
420 // a program refers to stdin. A workaround is to avoid that
421 // relocation and use only relocations with unlimited range.
422 int avoid_adrp = 1;
424 if (avoid_adrp || sym->a.weak) {
425 // (GCC uses a R_AARCH64_ABS64 in this case.)
426 greloca(cur_text_section, sym, ind, R_AARCH64_MOVW_UABS_G0_NC, addend);
427 o(0xd2800000 | r); // mov x(rt),#0,lsl #0
428 greloca(cur_text_section, sym, ind, R_AARCH64_MOVW_UABS_G1_NC, addend);
429 o(0xf2a00000 | r); // movk x(rt),#0,lsl #16
430 greloca(cur_text_section, sym, ind, R_AARCH64_MOVW_UABS_G2_NC, addend);
431 o(0xf2c00000 | r); // movk x(rt),#0,lsl #32
432 greloca(cur_text_section, sym, ind, R_AARCH64_MOVW_UABS_G3, addend);
433 o(0xf2e00000 | r); // movk x(rt),#0,lsl #48
435 else {
436 greloca(cur_text_section, sym, ind, R_AARCH64_ADR_PREL_PG_HI21, addend);
437 o(0x90000000 | r);
438 greloca(cur_text_section, sym, ind, R_AARCH64_ADD_ABS_LO12_NC, addend);
439 o(0x91000000 | r | r << 5);
443 static void arm64_load_cmp(int r, SValue *sv);
445 ST_FUNC void load(int r, SValue *sv)
447 int svtt = sv->type.t;
448 int svr = sv->r & ~VT_LVAL_TYPE;
449 int svrv = svr & VT_VALMASK;
450 uint64_t svcul = (uint32_t)sv->c.i;
451 svcul = svcul >> 31 & 1 ? svcul - ((uint64_t)1 << 32) : svcul;
453 if (svr == (VT_LOCAL | VT_LVAL)) {
454 if (IS_FREG(r))
455 arm64_ldrv(arm64_type_size(svtt), fltr(r), 29, svcul);
456 else
457 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
458 intr(r), 29, svcul);
459 return;
462 if ((svr & ~VT_VALMASK) == VT_LVAL && svrv < VT_CONST) {
463 if (IS_FREG(r))
464 arm64_ldrv(arm64_type_size(svtt), fltr(r), intr(svrv), 0);
465 else
466 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
467 intr(r), intr(svrv), 0);
468 return;
471 if (svr == (VT_CONST | VT_LVAL | VT_SYM)) {
472 arm64_sym(30, sv->sym, svcul); // use x30 for address
473 if (IS_FREG(r))
474 arm64_ldrv(arm64_type_size(svtt), fltr(r), 30, 0);
475 else
476 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
477 intr(r), 30, 0);
478 return;
481 if (svr == (VT_CONST | VT_SYM)) {
482 arm64_sym(intr(r), sv->sym, svcul);
483 return;
486 if (svr == VT_CONST) {
487 if ((svtt & VT_BTYPE) != VT_VOID)
488 arm64_movimm(intr(r), arm64_type_size(svtt) == 3 ?
489 sv->c.i : (uint32_t)svcul);
490 return;
493 if (svr < VT_CONST) {
494 if (IS_FREG(r) && IS_FREG(svr))
495 if (svtt == VT_LDOUBLE)
496 o(0x4ea01c00 | fltr(r) | fltr(svr) << 5);
497 // mov v(r).16b,v(svr).16b
498 else
499 o(0x1e604000 | fltr(r) | fltr(svr) << 5); // fmov d(r),d(svr)
500 else if (!IS_FREG(r) && !IS_FREG(svr))
501 o(0xaa0003e0 | intr(r) | intr(svr) << 16); // mov x(r),x(svr)
502 else
503 assert(0);
504 return;
507 if (svr == VT_LOCAL) {
508 if (-svcul < 0x1000)
509 o(0xd10003a0 | intr(r) | -svcul << 10); // sub x(r),x29,#...
510 else {
511 arm64_movimm(30, -svcul); // use x30 for offset
512 o(0xcb0003a0 | intr(r) | (uint32_t)30 << 16); // sub x(r),x29,x30
514 return;
517 if (svr == VT_JMP || svr == VT_JMPI) {
518 int t = (svr == VT_JMPI);
519 arm64_movimm(intr(r), t);
520 o(0x14000002); // b .+8
521 gsym(svcul);
522 arm64_movimm(intr(r), t ^ 1);
523 return;
526 if (svr == (VT_LLOCAL | VT_LVAL)) {
527 arm64_ldrx(0, 3, 30, 29, svcul); // use x30 for offset
528 if (IS_FREG(r))
529 arm64_ldrv(arm64_type_size(svtt), fltr(r), 30, 0);
530 else
531 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
532 intr(r), 30, 0);
533 return;
536 if (svr == VT_CMP) {
537 arm64_load_cmp(r, sv);
538 return;
541 printf("load(%x, (%x, %x, %llx))\n", r, svtt, sv->r, (long long)svcul);
542 assert(0);
545 ST_FUNC void store(int r, SValue *sv)
547 int svtt = sv->type.t;
548 int svr = sv->r & ~VT_LVAL_TYPE;
549 int svrv = svr & VT_VALMASK;
550 uint64_t svcul = (uint32_t)sv->c.i;
551 svcul = svcul >> 31 & 1 ? svcul - ((uint64_t)1 << 32) : svcul;
553 if (svr == (VT_LOCAL | VT_LVAL)) {
554 if (IS_FREG(r))
555 arm64_strv(arm64_type_size(svtt), fltr(r), 29, svcul);
556 else
557 arm64_strx(arm64_type_size(svtt), intr(r), 29, svcul);
558 return;
561 if ((svr & ~VT_VALMASK) == VT_LVAL && svrv < VT_CONST) {
562 if (IS_FREG(r))
563 arm64_strv(arm64_type_size(svtt), fltr(r), intr(svrv), 0);
564 else
565 arm64_strx(arm64_type_size(svtt), intr(r), intr(svrv), 0);
566 return;
569 if (svr == (VT_CONST | VT_LVAL | VT_SYM)) {
570 arm64_sym(30, sv->sym, svcul); // use x30 for address
571 if (IS_FREG(r))
572 arm64_strv(arm64_type_size(svtt), fltr(r), 30, 0);
573 else
574 arm64_strx(arm64_type_size(svtt), intr(r), 30, 0);
575 return;
578 printf("store(%x, (%x, %x, %llx))\n", r, svtt, sv->r, (long long)svcul);
579 assert(0);
582 static void arm64_gen_bl_or_b(int b)
584 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST && (vtop->r & VT_SYM)) {
585 assert(!b);
586 greloca(cur_text_section, vtop->sym, ind, R_AARCH64_CALL26, 0);
587 o(0x94000000); // bl .
589 else
590 o(0xd61f0000 | (uint32_t)!b << 21 | intr(gv(RC_R30)) << 5); // br/blr
593 static int arm64_hfa_aux(CType *type, int *fsize, int num)
595 if (is_float(type->t)) {
596 int a, n = type_size(type, &a);
597 if (num >= 4 || (*fsize && *fsize != n))
598 return -1;
599 *fsize = n;
600 return num + 1;
602 else if ((type->t & VT_BTYPE) == VT_STRUCT) {
603 int is_struct = 0; // rather than union
604 Sym *field;
605 for (field = type->ref->next; field; field = field->next)
606 if (field->c) {
607 is_struct = 1;
608 break;
610 if (is_struct) {
611 int num0 = num;
612 for (field = type->ref->next; field; field = field->next) {
613 if (field->c != (num - num0) * *fsize)
614 return -1;
615 num = arm64_hfa_aux(&field->type, fsize, num);
616 if (num == -1)
617 return -1;
619 if (type->ref->c != (num - num0) * *fsize)
620 return -1;
621 return num;
623 else { // union
624 int num0 = num;
625 for (field = type->ref->next; field; field = field->next) {
626 int num1 = arm64_hfa_aux(&field->type, fsize, num0);
627 if (num1 == -1)
628 return -1;
629 num = num1 < num ? num : num1;
631 if (type->ref->c != (num - num0) * *fsize)
632 return -1;
633 return num;
636 else if (type->t & VT_ARRAY) {
637 int num1;
638 if (!type->ref->c)
639 return num;
640 num1 = arm64_hfa_aux(&type->ref->type, fsize, num);
641 if (num1 == -1 || (num1 != num && type->ref->c > 4))
642 return -1;
643 num1 = num + type->ref->c * (num1 - num);
644 if (num1 > 4)
645 return -1;
646 return num1;
648 return -1;
651 static int arm64_hfa(CType *type, int *fsize)
653 if ((type->t & VT_BTYPE) == VT_STRUCT || (type->t & VT_ARRAY)) {
654 int sz = 0;
655 int n = arm64_hfa_aux(type, &sz, 0);
656 if (0 < n && n <= 4) {
657 if (fsize)
658 *fsize = sz;
659 return n;
662 return 0;
665 static unsigned long arm64_pcs_aux(int n, CType **type, unsigned long *a)
667 int nx = 0; // next integer register
668 int nv = 0; // next vector register
669 unsigned long ns = 32; // next stack offset
670 int i;
672 for (i = 0; i < n; i++) {
673 int hfa = arm64_hfa(type[i], 0);
674 int size, align;
676 if ((type[i]->t & VT_ARRAY) ||
677 (type[i]->t & VT_BTYPE) == VT_FUNC)
678 size = align = 8;
679 else
680 size = type_size(type[i], &align);
682 if (hfa)
683 // B.2
685 else if (size > 16) {
686 // B.3: replace with pointer
687 if (nx < 8)
688 a[i] = nx++ << 1 | 1;
689 else {
690 ns = (ns + 7) & ~7;
691 a[i] = ns | 1;
692 ns += 8;
694 continue;
696 else if ((type[i]->t & VT_BTYPE) == VT_STRUCT)
697 // B.4
698 size = (size + 7) & ~7;
700 // C.1
701 if (is_float(type[i]->t) && nv < 8) {
702 a[i] = 16 + (nv++ << 1);
703 continue;
706 // C.2
707 if (hfa && nv + hfa <= 8) {
708 a[i] = 16 + (nv << 1);
709 nv += hfa;
710 continue;
713 // C.3
714 if (hfa) {
715 nv = 8;
716 size = (size + 7) & ~7;
719 // C.4
720 if (hfa || (type[i]->t & VT_BTYPE) == VT_LDOUBLE) {
721 ns = (ns + 7) & ~7;
722 ns = (ns + align - 1) & -align;
725 // C.5
726 if ((type[i]->t & VT_BTYPE) == VT_FLOAT)
727 size = 8;
729 // C.6
730 if (hfa || is_float(type[i]->t)) {
731 a[i] = ns;
732 ns += size;
733 continue;
736 // C.7
737 if ((type[i]->t & VT_BTYPE) != VT_STRUCT && size <= 8 && nx < 8) {
738 a[i] = nx++ << 1;
739 continue;
742 // C.8
743 if (align == 16)
744 nx = (nx + 1) & ~1;
746 // C.9
747 if ((type[i]->t & VT_BTYPE) != VT_STRUCT && size == 16 && nx < 7) {
748 a[i] = nx << 1;
749 nx += 2;
750 continue;
753 // C.10
754 if ((type[i]->t & VT_BTYPE) == VT_STRUCT && size <= (8 - nx) * 8) {
755 a[i] = nx << 1;
756 nx += (size + 7) >> 3;
757 continue;
760 // C.11
761 nx = 8;
763 // C.12
764 ns = (ns + 7) & ~7;
765 ns = (ns + align - 1) & -align;
767 // C.13
768 if ((type[i]->t & VT_BTYPE) == VT_STRUCT) {
769 a[i] = ns;
770 ns += size;
771 continue;
774 // C.14
775 if (size < 8)
776 size = 8;
778 // C.15
779 a[i] = ns;
780 ns += size;
783 return ns - 32;
786 static unsigned long arm64_pcs(int n, CType **type, unsigned long *a)
788 unsigned long stack;
790 // Return type:
791 if ((type[0]->t & VT_BTYPE) == VT_VOID)
792 a[0] = -1;
793 else {
794 arm64_pcs_aux(1, type, a);
795 assert(a[0] == 0 || a[0] == 1 || a[0] == 16);
798 // Argument types:
799 stack = arm64_pcs_aux(n, type + 1, a + 1);
801 if (0) {
802 int i;
803 for (i = 0; i <= n; i++) {
804 if (!i)
805 printf("arm64_pcs return: ");
806 else
807 printf("arm64_pcs arg %d: ", i);
808 if (a[i] == (unsigned long)-1)
809 printf("void\n");
810 else if (a[i] == 1 && !i)
811 printf("X8 pointer\n");
812 else if (a[i] < 16)
813 printf("X%lu%s\n", a[i] / 2, a[i] & 1 ? " pointer" : "");
814 else if (a[i] < 32)
815 printf("V%lu\n", a[i] / 2 - 8);
816 else
817 printf("stack %lu%s\n",
818 (a[i] - 32) & ~1, a[i] & 1 ? " pointer" : "");
822 return stack;
825 ST_FUNC void gfunc_call(int nb_args)
827 CType *return_type;
828 CType **t;
829 unsigned long *a, *a1;
830 unsigned long stack;
831 int i;
833 return_type = &vtop[-nb_args].type.ref->type;
834 if ((return_type->t & VT_BTYPE) == VT_STRUCT)
835 --nb_args;
837 t = tcc_malloc((nb_args + 1) * sizeof(*t));
838 a = tcc_malloc((nb_args + 1) * sizeof(*a));
839 a1 = tcc_malloc((nb_args + 1) * sizeof(*a1));
841 t[0] = return_type;
842 for (i = 0; i < nb_args; i++)
843 t[nb_args - i] = &vtop[-i].type;
845 stack = arm64_pcs(nb_args, t, a);
847 // Allocate space for structs replaced by pointer:
848 for (i = nb_args; i; i--)
849 if (a[i] & 1) {
850 SValue *arg = &vtop[i - nb_args];
851 int align, size = type_size(&arg->type, &align);
852 assert((arg->type.t & VT_BTYPE) == VT_STRUCT);
853 stack = (stack + align - 1) & -align;
854 a1[i] = stack;
855 stack += size;
858 stack = (stack + 15) >> 4 << 4;
860 assert(stack < 0x1000);
861 if (stack)
862 o(0xd10003ff | stack << 10); // sub sp,sp,#(n)
864 // First pass: set all values on stack
865 for (i = nb_args; i; i--) {
866 vpushv(vtop - nb_args + i);
868 if (a[i] & 1) {
869 // struct replaced by pointer
870 int r = get_reg(RC_INT);
871 arm64_spoff(intr(r), a1[i]);
872 vset(&vtop->type, r | VT_LVAL, 0);
873 vswap();
874 vstore();
875 if (a[i] >= 32) {
876 // pointer on stack
877 r = get_reg(RC_INT);
878 arm64_spoff(intr(r), a1[i]);
879 arm64_strx(3, intr(r), 31, (a[i] - 32) >> 1 << 1);
882 else if (a[i] >= 32) {
883 // value on stack
884 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
885 int r = get_reg(RC_INT);
886 arm64_spoff(intr(r), a[i] - 32);
887 vset(&vtop->type, r | VT_LVAL, 0);
888 vswap();
889 vstore();
891 else if (is_float(vtop->type.t)) {
892 gv(RC_FLOAT);
893 arm64_strv(arm64_type_size(vtop[0].type.t),
894 fltr(vtop[0].r), 31, a[i] - 32);
896 else {
897 gv(RC_INT);
898 arm64_strx(arm64_type_size(vtop[0].type.t),
899 intr(vtop[0].r), 31, a[i] - 32);
903 --vtop;
906 // Second pass: assign values to registers
907 for (i = nb_args; i; i--, vtop--) {
908 if (a[i] < 16 && !(a[i] & 1)) {
909 // value in general-purpose registers
910 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
911 int align, size = type_size(&vtop->type, &align);
912 vtop->type.t = VT_PTR;
913 gaddrof();
914 gv(RC_R(a[i] / 2));
915 arm64_ldrs(a[i] / 2, size);
917 else
918 gv(RC_R(a[i] / 2));
920 else if (a[i] < 16)
921 // struct replaced by pointer in register
922 arm64_spoff(a[i] / 2, a1[i]);
923 else if (a[i] < 32) {
924 // value in floating-point registers
925 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
926 uint32_t j, sz, n = arm64_hfa(&vtop->type, &sz);
927 vtop->type.t = VT_PTR;
928 gaddrof();
929 gv(RC_R30);
930 for (j = 0; j < n; j++)
931 o(0x3d4003c0 |
932 (sz & 16) << 19 | -(sz & 8) << 27 | (sz & 4) << 29 |
933 (a[i] / 2 - 8 + j) |
934 j << 10); // ldr ([sdq])(*),[x30,#(j * sz)]
936 else
937 gv(RC_F(a[i] / 2 - 8));
941 if ((return_type->t & VT_BTYPE) == VT_STRUCT) {
942 if (a[0] == 1) {
943 // indirect return: set x8 and discard the stack value
944 gv(RC_R(8));
945 --vtop;
947 else
948 // return in registers: keep the address for after the call
949 vswap();
952 save_regs(0);
953 arm64_gen_bl_or_b(0);
954 --vtop;
955 if (stack)
956 o(0x910003ff | stack << 10); // add sp,sp,#(n)
959 int rt = return_type->t;
960 int bt = rt & VT_BTYPE;
961 if (bt == VT_BYTE || bt == VT_SHORT)
962 // Promote small integers:
963 o(0x13001c00 | (bt == VT_SHORT) << 13 |
964 (uint32_t)!!(rt & VT_UNSIGNED) << 30); // [su]xt[bh] w0,w0
965 else if (bt == VT_STRUCT && !(a[0] & 1)) {
966 // A struct was returned in registers, so write it out:
967 gv(RC_R(8));
968 --vtop;
969 if (a[0] == 0) {
970 int align, size = type_size(return_type, &align);
971 assert(size <= 16);
972 if (size > 8)
973 o(0xa9000500); // stp x0,x1,[x8]
974 else if (size)
975 arm64_strx(size > 4 ? 3 : size > 2 ? 2 : size > 1, 0, 8, 0);
978 else if (a[0] == 16) {
979 uint32_t j, sz, n = arm64_hfa(return_type, &sz);
980 for (j = 0; j < n; j++)
981 o(0x3d000100 |
982 (sz & 16) << 19 | -(sz & 8) << 27 | (sz & 4) << 29 |
983 (a[i] / 2 - 8 + j) |
984 j << 10); // str ([sdq])(*),[x8,#(j * sz)]
989 tcc_free(a1);
990 tcc_free(a);
991 tcc_free(t);
994 static unsigned long arm64_func_va_list_stack;
995 static int arm64_func_va_list_gr_offs;
996 static int arm64_func_va_list_vr_offs;
997 static int arm64_func_sub_sp_offset;
999 ST_FUNC void gfunc_prolog(CType *func_type)
1001 int n = 0;
1002 int i = 0;
1003 Sym *sym;
1004 CType **t;
1005 unsigned long *a;
1007 // Why doesn't the caller (gen_function) set func_vt?
1008 func_vt = func_type->ref->type;
1009 func_vc = 144; // offset of where x8 is stored
1011 for (sym = func_type->ref; sym; sym = sym->next)
1012 ++n;
1013 t = tcc_malloc(n * sizeof(*t));
1014 a = tcc_malloc(n * sizeof(*a));
1016 for (sym = func_type->ref; sym; sym = sym->next)
1017 t[i++] = &sym->type;
1019 arm64_func_va_list_stack = arm64_pcs(n - 1, t, a);
1021 o(0xa9b27bfd); // stp x29,x30,[sp,#-224]!
1022 o(0xad0087e0); // stp q0,q1,[sp,#16]
1023 o(0xad018fe2); // stp q2,q3,[sp,#48]
1024 o(0xad0297e4); // stp q4,q5,[sp,#80]
1025 o(0xad039fe6); // stp q6,q7,[sp,#112]
1026 o(0xa90923e8); // stp x8,x8,[sp,#144]
1027 o(0xa90a07e0); // stp x0,x1,[sp,#160]
1028 o(0xa90b0fe2); // stp x2,x3,[sp,#176]
1029 o(0xa90c17e4); // stp x4,x5,[sp,#192]
1030 o(0xa90d1fe6); // stp x6,x7,[sp,#208]
1032 arm64_func_va_list_gr_offs = -64;
1033 arm64_func_va_list_vr_offs = -128;
1035 for (i = 1, sym = func_type->ref->next; sym; i++, sym = sym->next) {
1036 int off = (a[i] < 16 ? 160 + a[i] / 2 * 8 :
1037 a[i] < 32 ? 16 + (a[i] - 16) / 2 * 16 :
1038 224 + ((a[i] - 32) >> 1 << 1));
1039 sym_push(sym->v & ~SYM_FIELD, &sym->type,
1040 (a[i] & 1 ? VT_LLOCAL : VT_LOCAL) | lvalue_type(sym->type.t),
1041 off);
1043 if (a[i] < 16) {
1044 int align, size = type_size(&sym->type, &align);
1045 arm64_func_va_list_gr_offs = (a[i] / 2 - 7 +
1046 (!(a[i] & 1) && size > 8)) * 8;
1048 else if (a[i] < 32) {
1049 uint32_t hfa = arm64_hfa(&sym->type, 0);
1050 arm64_func_va_list_vr_offs = (a[i] / 2 - 16 +
1051 (hfa ? hfa : 1)) * 16;
1054 // HFAs of float and double need to be written differently:
1055 if (16 <= a[i] && a[i] < 32 && (sym->type.t & VT_BTYPE) == VT_STRUCT) {
1056 uint32_t j, sz, k = arm64_hfa(&sym->type, &sz);
1057 if (sz < 16)
1058 for (j = 0; j < k; j++) {
1059 o(0x3d0003e0 | -(sz & 8) << 27 | (sz & 4) << 29 |
1060 ((a[i] - 16) / 2 + j) | (off / sz + j) << 10);
1061 // str ([sdq])(*),[sp,#(j * sz)]
1066 tcc_free(a);
1067 tcc_free(t);
1069 o(0x910003fd); // mov x29,sp
1070 arm64_func_sub_sp_offset = ind;
1071 // In gfunc_epilog these will be replaced with code to decrement SP:
1072 o(0xd503201f); // nop
1073 o(0xd503201f); // nop
1074 loc = 0;
1077 ST_FUNC void gen_va_start(void)
1079 int r;
1080 --vtop; // we don't need the "arg"
1081 gaddrof();
1082 r = intr(gv(RC_INT));
1084 if (arm64_func_va_list_stack) {
1085 //xx could use add (immediate) here
1086 arm64_movimm(30, arm64_func_va_list_stack + 224);
1087 o(0x8b1e03be); // add x30,x29,x30
1089 else
1090 o(0x910383be); // add x30,x29,#224
1091 o(0xf900001e | r << 5); // str x30,[x(r)]
1093 if (arm64_func_va_list_gr_offs) {
1094 if (arm64_func_va_list_stack)
1095 o(0x910383be); // add x30,x29,#224
1096 o(0xf900041e | r << 5); // str x30,[x(r),#8]
1099 if (arm64_func_va_list_vr_offs) {
1100 o(0x910243be); // add x30,x29,#144
1101 o(0xf900081e | r << 5); // str x30,[x(r),#16]
1104 arm64_movimm(30, arm64_func_va_list_gr_offs);
1105 o(0xb900181e | r << 5); // str w30,[x(r),#24]
1107 arm64_movimm(30, arm64_func_va_list_vr_offs);
1108 o(0xb9001c1e | r << 5); // str w30,[x(r),#28]
1110 --vtop;
1113 ST_FUNC void gen_va_arg(CType *t)
1115 int align, size = type_size(t, &align);
1116 int fsize, hfa = arm64_hfa(t, &fsize);
1117 uint32_t r0, r1;
1119 if (is_float(t->t)) {
1120 hfa = 1;
1121 fsize = size;
1124 gaddrof();
1125 r0 = intr(gv(RC_INT));
1126 r1 = get_reg(RC_INT);
1127 vtop[0].r = r1 | lvalue_type(t->t);
1128 r1 = intr(r1);
1130 if (!hfa) {
1131 uint32_t n = size > 16 ? 8 : (size + 7) & -8;
1132 o(0xb940181e | r0 << 5); // ldr w30,[x(r0),#24] // __gr_offs
1133 if (align == 16) {
1134 assert(0); // this path untested but needed for __uint128_t
1135 o(0x11003fde); // add w30,w30,#15
1136 o(0x121c6fde); // and w30,w30,#-16
1138 o(0x310003c0 | r1 | n << 10); // adds w(r1),w30,#(n)
1139 o(0x540000ad); // b.le .+20
1140 o(0xf9400000 | r1 | r0 << 5); // ldr x(r1),[x(r0)] // __stack
1141 o(0x9100001e | r1 << 5 | n << 10); // add x30,x(r1),#(n)
1142 o(0xf900001e | r0 << 5); // str x30,[x(r0)] // __stack
1143 o(0x14000004); // b .+16
1144 o(0xb9001800 | r1 | r0 << 5); // str w(r1),[x(r0),#24] // __gr_offs
1145 o(0xf9400400 | r1 | r0 << 5); // ldr x(r1),[x(r0),#8] // __gr_top
1146 o(0x8b3ec000 | r1 | r1 << 5); // add x(r1),x(r1),w30,sxtw
1147 if (size > 16)
1148 o(0xf9400000 | r1 | r1 << 5); // ldr x(r1),[x(r1)]
1150 else {
1151 uint32_t rsz = hfa << 4;
1152 uint32_t ssz = (size + 7) & -(uint32_t)8;
1153 uint32_t b1, b2;
1154 o(0xb9401c1e | r0 << 5); // ldr w30,[x(r0),#28] // __vr_offs
1155 o(0x310003c0 | r1 | rsz << 10); // adds w(r1),w30,#(rsz)
1156 b1 = ind; o(0x5400000d); // b.le lab1
1157 o(0xf9400000 | r1 | r0 << 5); // ldr x(r1),[x(r0)] // __stack
1158 if (fsize == 16) {
1159 o(0x91003c00 | r1 | r1 << 5); // add x(r1),x(r1),#15
1160 o(0x927cec00 | r1 | r1 << 5); // and x(r1),x(r1),#-16
1162 o(0x9100001e | r1 << 5 | ssz << 10); // add x30,x(r1),#(ssz)
1163 o(0xf900001e | r0 << 5); // str x30,[x(r0)] // __stack
1164 b2 = ind; o(0x14000000); // b lab2
1165 // lab1:
1166 write32le(cur_text_section->data + b1, 0x5400000d | (ind - b1) << 3);
1167 o(0xb9001c00 | r1 | r0 << 5); // str w(r1),[x(r0),#28] // __vr_offs
1168 o(0xf9400800 | r1 | r0 << 5); // ldr x(r1),[x(r0),#16] // __vr_top
1169 if (hfa == 1 || fsize == 16)
1170 o(0x8b3ec000 | r1 | r1 << 5); // add x(r1),x(r1),w30,sxtw
1171 else {
1172 // We need to change the layout of this HFA.
1173 // Get some space on the stack using global variable "loc":
1174 loc = (loc - size) & -(uint32_t)align;
1175 o(0x8b3ec000 | 30 | r1 << 5); // add x30,x(r1),w30,sxtw
1176 arm64_movimm(r1, loc);
1177 o(0x8b0003a0 | r1 | r1 << 16); // add x(r1),x29,x(r1)
1178 o(0x4c402bdc | (uint32_t)fsize << 7 |
1179 (uint32_t)(hfa == 2) << 15 |
1180 (uint32_t)(hfa == 3) << 14); // ld1 {v28.(4s|2d),...},[x30]
1181 o(0x0d00801c | r1 << 5 | (fsize == 8) << 10 |
1182 (uint32_t)(hfa != 2) << 13 |
1183 (uint32_t)(hfa != 3) << 21); // st(hfa) {v28.(s|d),...}[0],[x(r1)]
1185 // lab2:
1186 write32le(cur_text_section->data + b2, 0x14000000 | (ind - b2) >> 2);
1190 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret,
1191 int *align, int *regsize)
1193 return 0;
1196 ST_FUNC void gfunc_return(CType *func_type)
1198 CType *t = func_type;
1199 unsigned long a;
1201 arm64_pcs(0, &t, &a);
1202 switch (a) {
1203 case -1:
1204 break;
1205 case 0:
1206 if ((func_type->t & VT_BTYPE) == VT_STRUCT) {
1207 int align, size = type_size(func_type, &align);
1208 gaddrof();
1209 gv(RC_R(0));
1210 arm64_ldrs(0, size);
1212 else
1213 gv(RC_IRET);
1214 break;
1215 case 1: {
1216 CType type = *func_type;
1217 mk_pointer(&type);
1218 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
1219 indir();
1220 vswap();
1221 vstore();
1222 break;
1224 case 16:
1225 if ((func_type->t & VT_BTYPE) == VT_STRUCT) {
1226 uint32_t j, sz, n = arm64_hfa(&vtop->type, &sz);
1227 gaddrof();
1228 gv(RC_R(0));
1229 for (j = 0; j < n; j++)
1230 o(0x3d400000 |
1231 (sz & 16) << 19 | -(sz & 8) << 27 | (sz & 4) << 29 |
1232 j | j << 10); // ldr ([sdq])(*),[x0,#(j * sz)]
1234 else
1235 gv(RC_FRET);
1236 break;
1237 default:
1238 assert(0);
1240 vtop--;
1243 ST_FUNC void gfunc_epilog(void)
1245 if (loc) {
1246 // Insert instructions to subtract size of stack frame from SP.
1247 unsigned char *ptr = cur_text_section->data + arm64_func_sub_sp_offset;
1248 uint64_t diff = (-loc + 15) & ~15;
1249 if (!(diff >> 24)) {
1250 if (diff & 0xfff) // sub sp,sp,#(diff & 0xfff)
1251 write32le(ptr, 0xd10003ff | (diff & 0xfff) << 10);
1252 if (diff >> 12) // sub sp,sp,#(diff >> 12),lsl #12
1253 write32le(ptr + 4, 0xd14003ff | (diff >> 12) << 10);
1255 else {
1256 // In this case we may subtract more than necessary,
1257 // but always less than 17/16 of what we were aiming for.
1258 int i = 0;
1259 int j = 0;
1260 while (diff >> 20) {
1261 diff = (diff + 0xffff) >> 16;
1262 ++i;
1264 while (diff >> 16) {
1265 diff = (diff + 1) >> 1;
1266 ++j;
1268 write32le(ptr, 0xd2800010 | diff << 5 | i << 21);
1269 // mov x16,#(diff),lsl #(16 * i)
1270 write32le(ptr + 4, 0xcb3063ff | j << 10);
1271 // sub sp,sp,x16,lsl #(j)
1274 o(0x910003bf); // mov sp,x29
1275 o(0xa8ce7bfd); // ldp x29,x30,[sp],#224
1277 o(0xd65f03c0); // ret
1280 ST_FUNC void gen_fill_nops(int bytes)
1282 if ((bytes & 3))
1283 tcc_error("alignment of code section not multiple of 4");
1284 while (bytes > 0) {
1285 o(0xd503201f); // nop
1286 bytes -= 4;
1290 // Generate forward branch to label:
1291 ST_FUNC int gjmp(int t)
1293 int r = ind;
1294 if (nocode_wanted)
1295 return t;
1296 o(t);
1297 return r;
1300 // Generate branch to known address:
1301 ST_FUNC void gjmp_addr(int a)
1303 assert(a - ind + 0x8000000 < 0x10000000);
1304 o(0x14000000 | ((a - ind) >> 2 & 0x3ffffff));
1307 ST_FUNC int gjmp_append(int n, int t)
1309 void *p;
1310 /* insert vtop->c jump list in t */
1311 if (n) {
1312 uint32_t n1 = n, n2;
1313 while ((n2 = read32le(p = cur_text_section->data + n1)))
1314 n1 = n2;
1315 write32le(p, t);
1316 t = n;
1318 return t;
1321 void arm64_vset_VT_CMP(int op)
1323 if (op >= TOK_ULT && op <= TOK_GT) {
1324 vtop->cmp_r = vtop->r;
1325 vset_VT_CMP(0x80);
1329 static void arm64_gen_opil(int op, uint32_t l);
1331 static void arm64_load_cmp(int r, SValue *sv)
1333 sv->r = sv->cmp_r;
1334 if (sv->c.i & 1) {
1335 vpushi(1);
1336 arm64_gen_opil('^', 0);
1338 if (r != sv->r) {
1339 load(r, sv);
1340 sv->r = r;
1344 ST_FUNC int gjmp_cond(int op, int t)
1346 int bt = vtop->type.t & VT_BTYPE;
1348 int inv = op & 1;
1349 vtop->r = vtop->cmp_r;
1351 if (bt == VT_LDOUBLE) {
1352 uint32_t a, b, f = fltr(gv(RC_FLOAT));
1353 a = get_reg(RC_INT);
1354 vpushi(0);
1355 vtop[0].r = a;
1356 b = get_reg(RC_INT);
1357 a = intr(a);
1358 b = intr(b);
1359 o(0x4e083c00 | a | f << 5); // mov x(a),v(f).d[0]
1360 o(0x4e183c00 | b | f << 5); // mov x(b),v(f).d[1]
1361 o(0xaa000400 | a | a << 5 | b << 16); // orr x(a),x(a),x(b),lsl #1
1362 o(0xb4000040 | a | !!inv << 24); // cbz/cbnz x(a),.+8
1363 --vtop;
1365 else if (bt == VT_FLOAT || bt == VT_DOUBLE) {
1366 uint32_t a = fltr(gv(RC_FLOAT));
1367 o(0x1e202008 | a << 5 | (bt != VT_FLOAT) << 22); // fcmp
1368 o(0x54000040 | !!inv); // b.eq/b.ne .+8
1370 else {
1371 uint32_t ll = (bt == VT_PTR || bt == VT_LLONG);
1372 uint32_t a = intr(gv(RC_INT));
1373 o(0x34000040 | a | !!inv << 24 | ll << 31); // cbz/cbnz wA,.+8
1375 return gjmp(t);
1378 static int arm64_iconst(uint64_t *val, SValue *sv)
1380 if ((sv->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
1381 return 0;
1382 if (val) {
1383 int t = sv->type.t;
1384 int bt = t & VT_BTYPE;
1385 *val = ((bt == VT_LLONG || bt == VT_PTR) ? sv->c.i :
1386 (uint32_t)sv->c.i |
1387 (t & VT_UNSIGNED ? 0 : -(sv->c.i & 0x80000000)));
1389 return 1;
1392 static int arm64_gen_opic(int op, uint32_t l, int rev, uint64_t val,
1393 uint32_t x, uint32_t a)
1395 if (op == '-' && !rev) {
1396 val = -val;
1397 op = '+';
1399 val = l ? val : (uint32_t)val;
1401 switch (op) {
1403 case '+': {
1404 uint32_t s = l ? val >> 63 : val >> 31;
1405 val = s ? -val : val;
1406 val = l ? val : (uint32_t)val;
1407 if (!(val & ~(uint64_t)0xfff))
1408 o(0x11000000 | l << 31 | s << 30 | x | a << 5 | val << 10);
1409 else if (!(val & ~(uint64_t)0xfff000))
1410 o(0x11400000 | l << 31 | s << 30 | x | a << 5 | val >> 12 << 10);
1411 else {
1412 arm64_movimm(30, val); // use x30
1413 o(0x0b1e0000 | l << 31 | s << 30 | x | a << 5);
1415 return 1;
1418 case '-':
1419 if (!val)
1420 o(0x4b0003e0 | l << 31 | x | a << 16); // neg
1421 else if (val == (l ? (uint64_t)-1 : (uint32_t)-1))
1422 o(0x2a2003e0 | l << 31 | x | a << 16); // mvn
1423 else {
1424 arm64_movimm(30, val); // use x30
1425 o(0x4b0003c0 | l << 31 | x | a << 16); // sub
1427 return 1;
1429 case '^':
1430 if (val == -1 || (val == 0xffffffff && !l)) {
1431 o(0x2a2003e0 | l << 31 | x | a << 16); // mvn
1432 return 1;
1434 // fall through
1435 case '&':
1436 case '|': {
1437 int e = arm64_encode_bimm64(l ? val : val | val << 32);
1438 if (e < 0)
1439 return 0;
1440 o((op == '&' ? 0x12000000 :
1441 op == '|' ? 0x32000000 : 0x52000000) |
1442 l << 31 | x | a << 5 | (uint32_t)e << 10);
1443 return 1;
1446 case TOK_SAR:
1447 case TOK_SHL:
1448 case TOK_SHR: {
1449 uint32_t n = 32 << l;
1450 val = val & (n - 1);
1451 if (rev)
1452 return 0;
1453 if (!val)
1454 assert(0);
1455 else if (op == TOK_SHL)
1456 o(0x53000000 | l << 31 | l << 22 | x | a << 5 |
1457 (n - val) << 16 | (n - 1 - val) << 10); // lsl
1458 else
1459 o(0x13000000 | (op == TOK_SHR) << 30 | l << 31 | l << 22 |
1460 x | a << 5 | val << 16 | (n - 1) << 10); // lsr/asr
1461 return 1;
1465 return 0;
1468 static void arm64_gen_opil(int op, uint32_t l)
1470 uint32_t x, a, b;
1472 // Special treatment for operations with a constant operand:
1474 uint64_t val;
1475 int rev = 1;
1477 if (arm64_iconst(0, &vtop[0])) {
1478 vswap();
1479 rev = 0;
1481 if (arm64_iconst(&val, &vtop[-1])) {
1482 gv(RC_INT);
1483 a = intr(vtop[0].r);
1484 --vtop;
1485 x = get_reg(RC_INT);
1486 ++vtop;
1487 if (arm64_gen_opic(op, l, rev, val, intr(x), a)) {
1488 vtop[0].r = x;
1489 vswap();
1490 --vtop;
1491 return;
1494 if (!rev)
1495 vswap();
1498 gv2(RC_INT, RC_INT);
1499 assert(vtop[-1].r < VT_CONST && vtop[0].r < VT_CONST);
1500 a = intr(vtop[-1].r);
1501 b = intr(vtop[0].r);
1502 vtop -= 2;
1503 x = get_reg(RC_INT);
1504 ++vtop;
1505 vtop[0].r = x;
1506 x = intr(x);
1508 switch (op) {
1509 case '%':
1510 // Use x30 for quotient:
1511 o(0x1ac00c00 | l << 31 | 30 | a << 5 | b << 16); // sdiv
1512 o(0x1b008000 | l << 31 | x | (uint32_t)30 << 5 |
1513 b << 16 | a << 10); // msub
1514 break;
1515 case '&':
1516 o(0x0a000000 | l << 31 | x | a << 5 | b << 16); // and
1517 break;
1518 case '*':
1519 o(0x1b007c00 | l << 31 | x | a << 5 | b << 16); // mul
1520 break;
1521 case '+':
1522 o(0x0b000000 | l << 31 | x | a << 5 | b << 16); // add
1523 break;
1524 case '-':
1525 o(0x4b000000 | l << 31 | x | a << 5 | b << 16); // sub
1526 break;
1527 case '/':
1528 o(0x1ac00c00 | l << 31 | x | a << 5 | b << 16); // sdiv
1529 break;
1530 case '^':
1531 o(0x4a000000 | l << 31 | x | a << 5 | b << 16); // eor
1532 break;
1533 case '|':
1534 o(0x2a000000 | l << 31 | x | a << 5 | b << 16); // orr
1535 break;
1536 case TOK_EQ:
1537 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1538 o(0x1a9f17e0 | x); // cset wA,eq
1539 break;
1540 case TOK_GE:
1541 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1542 o(0x1a9fb7e0 | x); // cset wA,ge
1543 break;
1544 case TOK_GT:
1545 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1546 o(0x1a9fd7e0 | x); // cset wA,gt
1547 break;
1548 case TOK_LE:
1549 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1550 o(0x1a9fc7e0 | x); // cset wA,le
1551 break;
1552 case TOK_LT:
1553 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1554 o(0x1a9fa7e0 | x); // cset wA,lt
1555 break;
1556 case TOK_NE:
1557 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1558 o(0x1a9f07e0 | x); // cset wA,ne
1559 break;
1560 case TOK_SAR:
1561 o(0x1ac02800 | l << 31 | x | a << 5 | b << 16); // asr
1562 break;
1563 case TOK_SHL:
1564 o(0x1ac02000 | l << 31 | x | a << 5 | b << 16); // lsl
1565 break;
1566 case TOK_SHR:
1567 o(0x1ac02400 | l << 31 | x | a << 5 | b << 16); // lsr
1568 break;
1569 case TOK_UDIV:
1570 case TOK_PDIV:
1571 o(0x1ac00800 | l << 31 | x | a << 5 | b << 16); // udiv
1572 break;
1573 case TOK_UGE:
1574 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1575 o(0x1a9f37e0 | x); // cset wA,cs
1576 break;
1577 case TOK_UGT:
1578 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1579 o(0x1a9f97e0 | x); // cset wA,hi
1580 break;
1581 case TOK_ULT:
1582 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1583 o(0x1a9f27e0 | x); // cset wA,cc
1584 break;
1585 case TOK_ULE:
1586 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1587 o(0x1a9f87e0 | x); // cset wA,ls
1588 break;
1589 case TOK_UMOD:
1590 // Use x30 for quotient:
1591 o(0x1ac00800 | l << 31 | 30 | a << 5 | b << 16); // udiv
1592 o(0x1b008000 | l << 31 | x | (uint32_t)30 << 5 |
1593 b << 16 | a << 10); // msub
1594 break;
1595 default:
1596 assert(0);
1600 ST_FUNC void gen_opi(int op)
1602 arm64_gen_opil(op, 0);
1603 arm64_vset_VT_CMP(op);
1606 ST_FUNC void gen_opl(int op)
1608 arm64_gen_opil(op, 1);
1609 arm64_vset_VT_CMP(op);
1612 ST_FUNC void gen_opf(int op)
1614 uint32_t x, a, b, dbl;
1616 if (vtop[0].type.t == VT_LDOUBLE) {
1617 CType type = vtop[0].type;
1618 int func = 0;
1619 int cond = -1;
1620 switch (op) {
1621 case '*': func = TOK___multf3; break;
1622 case '+': func = TOK___addtf3; break;
1623 case '-': func = TOK___subtf3; break;
1624 case '/': func = TOK___divtf3; break;
1625 case TOK_EQ: func = TOK___eqtf2; cond = 1; break;
1626 case TOK_NE: func = TOK___netf2; cond = 0; break;
1627 case TOK_LT: func = TOK___lttf2; cond = 10; break;
1628 case TOK_GE: func = TOK___getf2; cond = 11; break;
1629 case TOK_LE: func = TOK___letf2; cond = 12; break;
1630 case TOK_GT: func = TOK___gttf2; cond = 13; break;
1631 default: assert(0); break;
1633 vpush_global_sym(&func_old_type, func);
1634 vrott(3);
1635 gfunc_call(2);
1636 vpushi(0);
1637 vtop->r = cond < 0 ? REG_FRET : REG_IRET;
1638 if (cond < 0)
1639 vtop->type = type;
1640 else {
1641 o(0x7100001f); // cmp w0,#0
1642 o(0x1a9f07e0 | (uint32_t)cond << 12); // cset w0,(cond)
1644 return;
1647 dbl = vtop[0].type.t != VT_FLOAT;
1648 gv2(RC_FLOAT, RC_FLOAT);
1649 assert(vtop[-1].r < VT_CONST && vtop[0].r < VT_CONST);
1650 a = fltr(vtop[-1].r);
1651 b = fltr(vtop[0].r);
1652 vtop -= 2;
1653 switch (op) {
1654 case TOK_EQ: case TOK_NE:
1655 case TOK_LT: case TOK_GE: case TOK_LE: case TOK_GT:
1656 x = get_reg(RC_INT);
1657 ++vtop;
1658 vtop[0].r = x;
1659 x = intr(x);
1660 break;
1661 default:
1662 x = get_reg(RC_FLOAT);
1663 ++vtop;
1664 vtop[0].r = x;
1665 x = fltr(x);
1666 break;
1669 switch (op) {
1670 case '*':
1671 o(0x1e200800 | dbl << 22 | x | a << 5 | b << 16); // fmul
1672 break;
1673 case '+':
1674 o(0x1e202800 | dbl << 22 | x | a << 5 | b << 16); // fadd
1675 break;
1676 case '-':
1677 o(0x1e203800 | dbl << 22 | x | a << 5 | b << 16); // fsub
1678 break;
1679 case '/':
1680 o(0x1e201800 | dbl << 22 | x | a << 5 | b << 16); // fdiv
1681 break;
1682 case TOK_EQ:
1683 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1684 o(0x1a9f17e0 | x); // cset w(x),eq
1685 break;
1686 case TOK_GE:
1687 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1688 o(0x1a9fb7e0 | x); // cset w(x),ge
1689 break;
1690 case TOK_GT:
1691 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1692 o(0x1a9fd7e0 | x); // cset w(x),gt
1693 break;
1694 case TOK_LE:
1695 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1696 o(0x1a9f87e0 | x); // cset w(x),ls
1697 break;
1698 case TOK_LT:
1699 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1700 o(0x1a9f57e0 | x); // cset w(x),mi
1701 break;
1702 case TOK_NE:
1703 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1704 o(0x1a9f07e0 | x); // cset w(x),ne
1705 break;
1706 default:
1707 assert(0);
1709 arm64_vset_VT_CMP(op);
1712 // Generate sign extension from 32 to 64 bits:
1713 ST_FUNC void gen_cvt_sxtw(void)
1715 uint32_t r = intr(gv(RC_INT));
1716 o(0x93407c00 | r | r << 5); // sxtw x(r),w(r)
1719 ST_FUNC void gen_cvt_itof(int t)
1721 if (t == VT_LDOUBLE) {
1722 int f = vtop->type.t;
1723 int func = (f & VT_BTYPE) == VT_LLONG ?
1724 (f & VT_UNSIGNED ? TOK___floatunditf : TOK___floatditf) :
1725 (f & VT_UNSIGNED ? TOK___floatunsitf : TOK___floatsitf);
1726 vpush_global_sym(&func_old_type, func);
1727 vrott(2);
1728 gfunc_call(1);
1729 vpushi(0);
1730 vtop->type.t = t;
1731 vtop->r = REG_FRET;
1732 return;
1734 else {
1735 int d, n = intr(gv(RC_INT));
1736 int s = !(vtop->type.t & VT_UNSIGNED);
1737 uint32_t l = ((vtop->type.t & VT_BTYPE) == VT_LLONG);
1738 --vtop;
1739 d = get_reg(RC_FLOAT);
1740 ++vtop;
1741 vtop[0].r = d;
1742 o(0x1e220000 | (uint32_t)!s << 16 |
1743 (uint32_t)(t != VT_FLOAT) << 22 | fltr(d) |
1744 l << 31 | n << 5); // [us]cvtf [sd](d),[wx](n)
1748 ST_FUNC void gen_cvt_ftoi(int t)
1750 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
1751 int func = (t & VT_BTYPE) == VT_LLONG ?
1752 (t & VT_UNSIGNED ? TOK___fixunstfdi : TOK___fixtfdi) :
1753 (t & VT_UNSIGNED ? TOK___fixunstfsi : TOK___fixtfsi);
1754 vpush_global_sym(&func_old_type, func);
1755 vrott(2);
1756 gfunc_call(1);
1757 vpushi(0);
1758 vtop->type.t = t;
1759 vtop->r = REG_IRET;
1760 return;
1762 else {
1763 int d, n = fltr(gv(RC_FLOAT));
1764 uint32_t l = ((vtop->type.t & VT_BTYPE) != VT_FLOAT);
1765 --vtop;
1766 d = get_reg(RC_INT);
1767 ++vtop;
1768 vtop[0].r = d;
1769 o(0x1e380000 |
1770 (uint32_t)!!(t & VT_UNSIGNED) << 16 |
1771 (uint32_t)((t & VT_BTYPE) == VT_LLONG) << 31 | intr(d) |
1772 l << 22 | n << 5); // fcvtz[su] [wx](d),[sd](n)
1776 ST_FUNC void gen_cvt_ftof(int t)
1778 int f = vtop[0].type.t;
1779 assert(t == VT_FLOAT || t == VT_DOUBLE || t == VT_LDOUBLE);
1780 assert(f == VT_FLOAT || f == VT_DOUBLE || f == VT_LDOUBLE);
1781 if (t == f)
1782 return;
1784 if (t == VT_LDOUBLE || f == VT_LDOUBLE) {
1785 int func = (t == VT_LDOUBLE) ?
1786 (f == VT_FLOAT ? TOK___extendsftf2 : TOK___extenddftf2) :
1787 (t == VT_FLOAT ? TOK___trunctfsf2 : TOK___trunctfdf2);
1788 vpush_global_sym(&func_old_type, func);
1789 vrott(2);
1790 gfunc_call(1);
1791 vpushi(0);
1792 vtop->type.t = t;
1793 vtop->r = REG_FRET;
1795 else {
1796 int x, a;
1797 gv(RC_FLOAT);
1798 assert(vtop[0].r < VT_CONST);
1799 a = fltr(vtop[0].r);
1800 --vtop;
1801 x = get_reg(RC_FLOAT);
1802 ++vtop;
1803 vtop[0].r = x;
1804 x = fltr(x);
1806 if (f == VT_FLOAT)
1807 o(0x1e22c000 | x | a << 5); // fcvt d(x),s(a)
1808 else
1809 o(0x1e624000 | x | a << 5); // fcvt s(x),d(a)
1813 ST_FUNC void ggoto(void)
1815 arm64_gen_bl_or_b(1);
1816 --vtop;
1819 ST_FUNC void gen_clear_cache(void)
1821 uint32_t beg, end, dsz, isz, p, lab1, b1;
1822 gv2(RC_INT, RC_INT);
1823 vpushi(0);
1824 vtop->r = get_reg(RC_INT);
1825 vpushi(0);
1826 vtop->r = get_reg(RC_INT);
1827 vpushi(0);
1828 vtop->r = get_reg(RC_INT);
1829 beg = intr(vtop[-4].r); // x0
1830 end = intr(vtop[-3].r); // x1
1831 dsz = intr(vtop[-2].r); // x2
1832 isz = intr(vtop[-1].r); // x3
1833 p = intr(vtop[0].r); // x4
1834 vtop -= 5;
1836 o(0xd53b0020 | isz); // mrs x(isz),ctr_el0
1837 o(0x52800080 | p); // mov w(p),#4
1838 o(0x53104c00 | dsz | isz << 5); // ubfx w(dsz),w(isz),#16,#4
1839 o(0x1ac02000 | dsz | p << 5 | dsz << 16); // lsl w(dsz),w(p),w(dsz)
1840 o(0x12000c00 | isz | isz << 5); // and w(isz),w(isz),#15
1841 o(0x1ac02000 | isz | p << 5 | isz << 16); // lsl w(isz),w(p),w(isz)
1842 o(0x51000400 | p | dsz << 5); // sub w(p),w(dsz),#1
1843 o(0x8a240004 | p | beg << 5 | p << 16); // bic x(p),x(beg),x(p)
1844 b1 = ind; o(0x14000000); // b
1845 lab1 = ind;
1846 o(0xd50b7b20 | p); // dc cvau,x(p)
1847 o(0x8b000000 | p | p << 5 | dsz << 16); // add x(p),x(p),x(dsz)
1848 write32le(cur_text_section->data + b1, 0x14000000 | (ind - b1) >> 2);
1849 o(0xeb00001f | p << 5 | end << 16); // cmp x(p),x(end)
1850 o(0x54ffffa3 | ((lab1 - ind) << 3 & 0xffffe0)); // b.cc lab1
1851 o(0xd5033b9f); // dsb ish
1852 o(0x51000400 | p | isz << 5); // sub w(p),w(isz),#1
1853 o(0x8a240004 | p | beg << 5 | p << 16); // bic x(p),x(beg),x(p)
1854 b1 = ind; o(0x14000000); // b
1855 lab1 = ind;
1856 o(0xd50b7520 | p); // ic ivau,x(p)
1857 o(0x8b000000 | p | p << 5 | isz << 16); // add x(p),x(p),x(isz)
1858 write32le(cur_text_section->data + b1, 0x14000000 | (ind - b1) >> 2);
1859 o(0xeb00001f | p << 5 | end << 16); // cmp x(p),x(end)
1860 o(0x54ffffa3 | ((lab1 - ind) << 3 & 0xffffe0)); // b.cc lab1
1861 o(0xd5033b9f); // dsb ish
1862 o(0xd5033fdf); // isb
1865 ST_FUNC void gen_vla_sp_save(int addr) {
1866 uint32_t r = intr(get_reg(RC_INT));
1867 o(0x910003e0 | r); // mov x(r),sp
1868 arm64_strx(3, r, 29, addr);
1871 ST_FUNC void gen_vla_sp_restore(int addr) {
1872 // Use x30 because this function can be called when there
1873 // is a live return value in x0 but there is nothing on
1874 // the value stack to prevent get_reg from returning x0.
1875 uint32_t r = 30;
1876 arm64_ldrx(0, 3, r, 29, addr);
1877 o(0x9100001f | r << 5); // mov sp,x(r)
1880 ST_FUNC void gen_vla_alloc(CType *type, int align) {
1881 uint32_t r = intr(gv(RC_INT));
1882 o(0x91003c00 | r | r << 5); // add x(r),x(r),#15
1883 o(0x927cec00 | r | r << 5); // bic x(r),x(r),#15
1884 o(0xcb2063ff | r << 16); // sub sp,sp,x(r)
1885 vpop();
1888 /* end of A64 code generator */
1889 /*************************************************************/
1890 #endif
1891 /*************************************************************/