tccgen: inline_functions double free fix
[tinycc.git] / arm64-gen.c
blobcd6faac5ab01b32da20b53f674bba2a08d254bcc
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 /* ELF defines */
46 #define EM_TCC_TARGET EM_AARCH64
48 #define R_DATA_32 R_AARCH64_ABS32
49 #define R_DATA_PTR R_AARCH64_ABS64
50 #define R_JMP_SLOT R_AARCH64_JUMP_SLOT
51 #define R_COPY R_AARCH64_COPY
53 #define ELF_START_ADDR 0x00400000
54 #define ELF_PAGE_SIZE 0x1000
56 /******************************************************/
57 #else /* ! TARGET_DEFS_ONLY */
58 /******************************************************/
59 #include "tcc.h"
60 #include <assert.h>
62 ST_DATA const int reg_classes[NB_REGS] = {
63 RC_INT | RC_R(0),
64 RC_INT | RC_R(1),
65 RC_INT | RC_R(2),
66 RC_INT | RC_R(3),
67 RC_INT | RC_R(4),
68 RC_INT | RC_R(5),
69 RC_INT | RC_R(6),
70 RC_INT | RC_R(7),
71 RC_INT | RC_R(8),
72 RC_INT | RC_R(9),
73 RC_INT | RC_R(10),
74 RC_INT | RC_R(11),
75 RC_INT | RC_R(12),
76 RC_INT | RC_R(13),
77 RC_INT | RC_R(14),
78 RC_INT | RC_R(15),
79 RC_INT | RC_R(16),
80 RC_INT | RC_R(17),
81 RC_INT | RC_R(18),
82 RC_R30, // not in RC_INT as we make special use of x30
83 RC_FLOAT | RC_F(0),
84 RC_FLOAT | RC_F(1),
85 RC_FLOAT | RC_F(2),
86 RC_FLOAT | RC_F(3),
87 RC_FLOAT | RC_F(4),
88 RC_FLOAT | RC_F(5),
89 RC_FLOAT | RC_F(6),
90 RC_FLOAT | RC_F(7)
93 #define IS_FREG(x) ((x) >= TREG_F(0))
95 static uint32_t intr(int r)
97 assert(TREG_R(0) <= r && r <= TREG_R30);
98 return r < TREG_R30 ? r : 30;
101 static uint32_t fltr(int r)
103 assert(TREG_F(0) <= r && r <= TREG_F(7));
104 return r - TREG_F(0);
107 // Add an instruction to text section:
108 ST_FUNC void o(unsigned int c)
110 int ind1 = ind + 4;
111 if (ind1 > cur_text_section->data_allocated)
112 section_realloc(cur_text_section, ind1);
113 write32le(cur_text_section->data + ind, c);
114 ind = ind1;
117 static int arm64_encode_bimm64(uint64_t x)
119 int neg = x & 1;
120 int rep, pos, len;
122 if (neg)
123 x = ~x;
124 if (!x)
125 return -1;
127 if (x >> 2 == (x & (((uint64_t)1 << (64 - 2)) - 1)))
128 rep = 2, x &= ((uint64_t)1 << 2) - 1;
129 else if (x >> 4 == (x & (((uint64_t)1 << (64 - 4)) - 1)))
130 rep = 4, x &= ((uint64_t)1 << 4) - 1;
131 else if (x >> 8 == (x & (((uint64_t)1 << (64 - 8)) - 1)))
132 rep = 8, x &= ((uint64_t)1 << 8) - 1;
133 else if (x >> 16 == (x & (((uint64_t)1 << (64 - 16)) - 1)))
134 rep = 16, x &= ((uint64_t)1 << 16) - 1;
135 else if (x >> 32 == (x & (((uint64_t)1 << (64 - 32)) - 1)))
136 rep = 32, x &= ((uint64_t)1 << 32) - 1;
137 else
138 rep = 64;
140 pos = 0;
141 if (!(x & (((uint64_t)1 << 32) - 1))) x >>= 32, pos += 32;
142 if (!(x & (((uint64_t)1 << 16) - 1))) x >>= 16, pos += 16;
143 if (!(x & (((uint64_t)1 << 8) - 1))) x >>= 8, pos += 8;
144 if (!(x & (((uint64_t)1 << 4) - 1))) x >>= 4, pos += 4;
145 if (!(x & (((uint64_t)1 << 2) - 1))) x >>= 2, pos += 2;
146 if (!(x & (((uint64_t)1 << 1) - 1))) x >>= 1, pos += 1;
148 len = 0;
149 if (!(~x & (((uint64_t)1 << 32) - 1))) x >>= 32, len += 32;
150 if (!(~x & (((uint64_t)1 << 16) - 1))) x >>= 16, len += 16;
151 if (!(~x & (((uint64_t)1 << 8) - 1))) x >>= 8, len += 8;
152 if (!(~x & (((uint64_t)1 << 4) - 1))) x >>= 4, len += 4;
153 if (!(~x & (((uint64_t)1 << 2) - 1))) x >>= 2, len += 2;
154 if (!(~x & (((uint64_t)1 << 1) - 1))) x >>= 1, len += 1;
156 if (x)
157 return -1;
158 if (neg) {
159 pos = (pos + len) & (rep - 1);
160 len = rep - len;
162 return ((0x1000 & rep << 6) | (((rep - 1) ^ 31) << 1 & 63) |
163 ((rep - pos) & (rep - 1)) << 6 | (len - 1));
166 static uint32_t arm64_movi(int r, uint64_t x)
168 uint64_t m = 0xffff;
169 int e;
170 if (!(x & ~m))
171 return 0x52800000 | r | x << 5; // movz w(r),#(x)
172 if (!(x & ~(m << 16)))
173 return 0x52a00000 | r | x >> 11; // movz w(r),#(x >> 16),lsl #16
174 if (!(x & ~(m << 32)))
175 return 0xd2c00000 | r | x >> 27; // movz x(r),#(x >> 32),lsl #32
176 if (!(x & ~(m << 48)))
177 return 0xd2e00000 | r | x >> 43; // movz x(r),#(x >> 48),lsl #48
178 if ((x & ~m) == m << 16)
179 return (0x12800000 | r |
180 (~x << 5 & 0x1fffe0)); // movn w(r),#(~x)
181 if ((x & ~(m << 16)) == m)
182 return (0x12a00000 | r |
183 (~x >> 11 & 0x1fffe0)); // movn w(r),#(~x >> 16),lsl #16
184 if (!~(x | m))
185 return (0x92800000 | r |
186 (~x << 5 & 0x1fffe0)); // movn x(r),#(~x)
187 if (!~(x | m << 16))
188 return (0x92a00000 | r |
189 (~x >> 11 & 0x1fffe0)); // movn x(r),#(~x >> 16),lsl #16
190 if (!~(x | m << 32))
191 return (0x92c00000 | r |
192 (~x >> 27 & 0x1fffe0)); // movn x(r),#(~x >> 32),lsl #32
193 if (!~(x | m << 48))
194 return (0x92e00000 | r |
195 (~x >> 43 & 0x1fffe0)); // movn x(r),#(~x >> 32),lsl #32
196 if (!(x >> 32) && (e = arm64_encode_bimm64(x | x << 32)) >= 0)
197 return 0x320003e0 | r | (uint32_t)e << 10; // movi w(r),#(x)
198 if ((e = arm64_encode_bimm64(x)) >= 0)
199 return 0xb20003e0 | r | (uint32_t)e << 10; // movi x(r),#(x)
200 return 0;
203 static void arm64_movimm(int r, uint64_t x)
205 uint32_t i;
206 if ((i = arm64_movi(r, x)))
207 o(i); // a single MOV
208 else {
209 // MOVZ/MOVN and 1-3 MOVKs
210 int z = 0, m = 0;
211 uint32_t mov1 = 0xd2800000; // movz
212 uint64_t x1 = x;
213 for (i = 0; i < 64; i += 16) {
214 z += !(x >> i & 0xffff);
215 m += !(~x >> i & 0xffff);
217 if (m > z) {
218 x1 = ~x;
219 mov1 = 0x92800000; // movn
221 for (i = 0; i < 64; i += 16)
222 if (x1 >> i & 0xffff) {
223 o(mov1 | r | (x1 >> i & 0xffff) << 5 | i << 17);
224 // movz/movn x(r),#(*),lsl #(i)
225 break;
227 for (i += 16; i < 64; i += 16)
228 if (x1 >> i & 0xffff)
229 o(0xf2800000 | r | (x >> i & 0xffff) << 5 | i << 17);
230 // movk x(r),#(*),lsl #(i)
234 // Patch all branches in list pointed to by t to branch to a:
235 ST_FUNC void gsym_addr(int t_, int a_)
237 uint32_t t = t_;
238 uint32_t a = a_;
239 while (t) {
240 unsigned char *ptr = cur_text_section->data + t;
241 uint32_t next = read32le(ptr);
242 if (a - t + 0x8000000 >= 0x10000000)
243 tcc_error("branch out of range");
244 write32le(ptr, (a - t == 4 ? 0xd503201f : // nop
245 0x14000000 | ((a - t) >> 2 & 0x3ffffff))); // b
246 t = next;
250 // Patch all branches in list pointed to by t to branch to current location:
251 ST_FUNC void gsym(int t)
253 gsym_addr(t, ind);
256 static int arm64_type_size(int t)
258 switch (t & VT_BTYPE) {
259 case VT_INT: return 2;
260 case VT_BYTE: return 0;
261 case VT_SHORT: return 1;
262 case VT_PTR: return 3;
263 case VT_ENUM: return 2;
264 case VT_FUNC: return 3;
265 case VT_FLOAT: return 2;
266 case VT_DOUBLE: return 3;
267 case VT_LDOUBLE: return 4;
268 case VT_BOOL: return 0;
269 case VT_LLONG: return 3;
271 assert(0);
272 return 0;
275 static void arm64_spoff(int reg, uint64_t off)
277 uint32_t sub = off >> 63;
278 if (sub)
279 off = -off;
280 if (off < 4096)
281 o(0x910003e0 | sub << 30 | reg | off << 10);
282 // (add|sub) x(reg),sp,#(off)
283 else {
284 arm64_movimm(30, off); // use x30 for offset
285 o(0x8b3e63e0 | sub << 30 | reg); // (add|sub) x(reg),sp,x30
289 static void arm64_ldrx(int sg, int sz_, int dst, int bas, uint64_t off)
291 uint32_t sz = sz_;
292 if (sz >= 2)
293 sg = 0;
294 if (!(off & ~((uint32_t)0xfff << sz)))
295 o(0x39400000 | dst | bas << 5 | off << (10 - sz) |
296 (uint32_t)!!sg << 23 | sz << 30); // ldr(*) x(dst),[x(bas),#(off)]
297 else if (off < 256 || -off <= 256)
298 o(0x38400000 | dst | bas << 5 | (off & 511) << 12 |
299 (uint32_t)!!sg << 23 | sz << 30); // ldur(*) x(dst),[x(bas),#(off)]
300 else {
301 arm64_movimm(30, off); // use x30 for offset
302 o(0x38206800 | dst | bas << 5 | (uint32_t)30 << 16 |
303 (uint32_t)(!!sg + 1) << 22 | sz << 30); // ldr(*) x(dst),[x(bas),x30]
307 static void arm64_ldrv(int sz_, int dst, int bas, uint64_t off)
309 uint32_t sz = sz_;
310 if (!(off & ~((uint32_t)0xfff << sz)))
311 o(0x3d400000 | dst | bas << 5 | off << (10 - sz) |
312 (sz & 4) << 21 | (sz & 3) << 30); // ldr (s|d|q)(dst),[x(bas),#(off)]
313 else if (off < 256 || -off <= 256)
314 o(0x3c400000 | dst | bas << 5 | (off & 511) << 12 |
315 (sz & 4) << 21 | (sz & 3) << 30); // ldur (s|d|q)(dst),[x(bas),#(off)]
316 else {
317 arm64_movimm(30, off); // use x30 for offset
318 o(0x3c606800 | dst | bas << 5 | (uint32_t)30 << 16 |
319 sz << 30 | (sz & 4) << 21); // ldr (s|d|q)(dst),[x(bas),x30]
323 static void arm64_ldrs(int reg_, int size)
325 uint32_t reg = reg_;
326 // Use x30 for intermediate value in some cases.
327 switch (size) {
328 default: assert(0); break;
329 case 1:
330 arm64_ldrx(0, 0, reg, reg, 0);
331 break;
332 case 2:
333 arm64_ldrx(0, 1, reg, reg, 0);
334 break;
335 case 3:
336 arm64_ldrx(0, 1, 30, reg, 0);
337 arm64_ldrx(0, 0, reg, reg, 2);
338 o(0x2a0043c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #16
339 break;
340 case 4:
341 arm64_ldrx(0, 2, reg, reg, 0);
342 break;
343 case 5:
344 arm64_ldrx(0, 2, 30, reg, 0);
345 arm64_ldrx(0, 0, reg, reg, 4);
346 o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32
347 break;
348 case 6:
349 arm64_ldrx(0, 2, 30, reg, 0);
350 arm64_ldrx(0, 1, reg, reg, 4);
351 o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32
352 break;
353 case 7:
354 arm64_ldrx(0, 2, 30, reg, 0);
355 arm64_ldrx(0, 2, reg, reg, 3);
356 o(0x53087c00 | reg | reg << 5); // lsr w(reg), w(reg), #8
357 o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32
358 break;
359 case 8:
360 arm64_ldrx(0, 3, reg, reg, 0);
361 break;
362 case 9:
363 arm64_ldrx(0, 0, reg + 1, reg, 8);
364 arm64_ldrx(0, 3, reg, reg, 0);
365 break;
366 case 10:
367 arm64_ldrx(0, 1, reg + 1, reg, 8);
368 arm64_ldrx(0, 3, reg, reg, 0);
369 break;
370 case 11:
371 arm64_ldrx(0, 2, reg + 1, reg, 7);
372 o(0x53087c00 | (reg+1) | (reg+1) << 5); // lsr w(reg+1), w(reg+1), #8
373 arm64_ldrx(0, 3, reg, reg, 0);
374 break;
375 case 12:
376 arm64_ldrx(0, 2, reg + 1, reg, 8);
377 arm64_ldrx(0, 3, reg, reg, 0);
378 break;
379 case 13:
380 arm64_ldrx(0, 3, reg + 1, reg, 5);
381 o(0xd358fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #24
382 arm64_ldrx(0, 3, reg, reg, 0);
383 break;
384 case 14:
385 arm64_ldrx(0, 3, reg + 1, reg, 6);
386 o(0xd350fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #16
387 arm64_ldrx(0, 3, reg, reg, 0);
388 break;
389 case 15:
390 arm64_ldrx(0, 3, reg + 1, reg, 7);
391 o(0xd348fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #8
392 arm64_ldrx(0, 3, reg, reg, 0);
393 break;
394 case 16:
395 o(0xa9400000 | reg | (reg+1) << 10 | reg << 5);
396 // ldp x(reg),x(reg+1),[x(reg)]
397 break;
401 static void arm64_strx(int sz_, int dst, int bas, uint64_t off)
403 uint32_t sz = sz_;
404 if (!(off & ~((uint32_t)0xfff << sz)))
405 o(0x39000000 | dst | bas << 5 | off << (10 - sz) | sz << 30);
406 // str(*) x(dst),[x(bas],#(off)]
407 else if (off < 256 || -off <= 256)
408 o(0x38000000 | dst | bas << 5 | (off & 511) << 12 | sz << 30);
409 // stur(*) x(dst),[x(bas],#(off)]
410 else {
411 arm64_movimm(30, off); // use x30 for offset
412 o(0x38206800 | dst | bas << 5 | (uint32_t)30 << 16 | sz << 30);
413 // str(*) x(dst),[x(bas),x30]
417 static void arm64_strv(int sz_, int dst, int bas, uint64_t off)
419 uint32_t sz = sz_;
420 if (!(off & ~((uint32_t)0xfff << sz)))
421 o(0x3d000000 | dst | bas << 5 | off << (10 - sz) |
422 (sz & 4) << 21 | (sz & 3) << 30); // str (s|d|q)(dst),[x(bas),#(off)]
423 else if (off < 256 || -off <= 256)
424 o(0x3c000000 | dst | bas << 5 | (off & 511) << 12 |
425 (sz & 4) << 21 | (sz & 3) << 30); // stur (s|d|q)(dst),[x(bas),#(off)]
426 else {
427 arm64_movimm(30, off); // use x30 for offset
428 o(0x3c206800 | dst | bas << 5 | (uint32_t)30 << 16 |
429 sz << 30 | (sz & 4) << 21); // str (s|d|q)(dst),[x(bas),x30]
433 static void arm64_sym(int r, Sym *sym, unsigned long addend)
435 // Currently TCC's linker does not generate COPY relocations for
436 // STT_OBJECTs when tcc is invoked with "-run". This typically
437 // results in "R_AARCH64_ADR_PREL_PG_HI21 relocation failed" when
438 // a program refers to stdin. A workaround is to avoid that
439 // relocation and use only relocations with unlimited range.
440 int avoid_adrp = 1;
442 if (avoid_adrp || (sym->type.t & VT_WEAK)) {
443 // (GCC uses a R_AARCH64_ABS64 in this case.)
444 greloca(cur_text_section, sym, ind, R_AARCH64_MOVW_UABS_G0_NC, addend);
445 o(0xd2800000 | r); // mov x(rt),#0,lsl #0
446 greloca(cur_text_section, sym, ind, R_AARCH64_MOVW_UABS_G1_NC, addend);
447 o(0xf2a00000 | r); // movk x(rt),#0,lsl #16
448 greloca(cur_text_section, sym, ind, R_AARCH64_MOVW_UABS_G2_NC, addend);
449 o(0xf2c00000 | r); // movk x(rt),#0,lsl #32
450 greloca(cur_text_section, sym, ind, R_AARCH64_MOVW_UABS_G3, addend);
451 o(0xf2e00000 | r); // movk x(rt),#0,lsl #48
453 else {
454 greloca(cur_text_section, sym, ind, R_AARCH64_ADR_PREL_PG_HI21, addend);
455 o(0x90000000 | r);
456 greloca(cur_text_section, sym, ind, R_AARCH64_ADD_ABS_LO12_NC, addend);
457 o(0x91000000 | r | r << 5);
461 ST_FUNC void load(int r, SValue *sv)
463 int svtt = sv->type.t;
464 int svr = sv->r & ~VT_LVAL_TYPE;
465 int svrv = svr & VT_VALMASK;
466 uint64_t svcul = (uint32_t)sv->c.i;
467 svcul = svcul >> 31 & 1 ? svcul - ((uint64_t)1 << 32) : svcul;
469 if (svr == (VT_LOCAL | VT_LVAL)) {
470 if (IS_FREG(r))
471 arm64_ldrv(arm64_type_size(svtt), fltr(r), 29, svcul);
472 else
473 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
474 intr(r), 29, svcul);
475 return;
478 if ((svr & ~VT_VALMASK) == VT_LVAL && svrv < VT_CONST) {
479 if (IS_FREG(r))
480 arm64_ldrv(arm64_type_size(svtt), fltr(r), intr(svrv), 0);
481 else
482 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
483 intr(r), intr(svrv), 0);
484 return;
487 if (svr == (VT_CONST | VT_LVAL | VT_SYM)) {
488 arm64_sym(30, sv->sym, svcul); // use x30 for address
489 if (IS_FREG(r))
490 arm64_ldrv(arm64_type_size(svtt), fltr(r), 30, 0);
491 else
492 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
493 intr(r), 30, 0);
494 return;
497 if (svr == (VT_CONST | VT_SYM)) {
498 arm64_sym(intr(r), sv->sym, svcul);
499 return;
502 if (svr == VT_CONST) {
503 if ((svtt & VT_BTYPE) != VT_VOID)
504 arm64_movimm(intr(r), arm64_type_size(svtt) == 3 ?
505 sv->c.i : (uint32_t)svcul);
506 return;
509 if (svr < VT_CONST) {
510 if (IS_FREG(r) && IS_FREG(svr))
511 if (svtt == VT_LDOUBLE)
512 o(0x4ea01c00 | fltr(r) | fltr(svr) << 5);
513 // mov v(r).16b,v(svr).16b
514 else
515 o(0x1e604000 | fltr(r) | fltr(svr) << 5); // fmov d(r),d(svr)
516 else if (!IS_FREG(r) && !IS_FREG(svr))
517 o(0xaa0003e0 | intr(r) | intr(svr) << 16); // mov x(r),x(svr)
518 else
519 assert(0);
520 return;
523 if (svr == VT_LOCAL) {
524 if (-svcul < 0x1000)
525 o(0xd10003a0 | intr(r) | -svcul << 10); // sub x(r),x29,#...
526 else {
527 arm64_movimm(30, -svcul); // use x30 for offset
528 o(0xcb0003a0 | intr(r) | (uint32_t)30 << 16); // sub x(r),x29,x30
530 return;
533 if (svr == VT_JMP || svr == VT_JMPI) {
534 int t = (svr == VT_JMPI);
535 arm64_movimm(intr(r), t);
536 o(0x14000002); // b .+8
537 gsym(svcul);
538 arm64_movimm(intr(r), t ^ 1);
539 return;
542 if (svr == (VT_LLOCAL | VT_LVAL)) {
543 arm64_ldrx(0, 3, 30, 29, svcul); // use x30 for offset
544 if (IS_FREG(r))
545 arm64_ldrv(arm64_type_size(svtt), fltr(r), 30, 0);
546 else
547 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
548 intr(r), 30, 0);
549 return;
552 printf("load(%x, (%x, %x, %llx))\n", r, svtt, sv->r, (long long)svcul);
553 assert(0);
556 ST_FUNC void store(int r, SValue *sv)
558 int svtt = sv->type.t;
559 int svr = sv->r & ~VT_LVAL_TYPE;
560 int svrv = svr & VT_VALMASK;
561 uint64_t svcul = (uint32_t)sv->c.i;
562 svcul = svcul >> 31 & 1 ? svcul - ((uint64_t)1 << 32) : svcul;
564 if (svr == (VT_LOCAL | VT_LVAL)) {
565 if (IS_FREG(r))
566 arm64_strv(arm64_type_size(svtt), fltr(r), 29, svcul);
567 else
568 arm64_strx(arm64_type_size(svtt), intr(r), 29, svcul);
569 return;
572 if ((svr & ~VT_VALMASK) == VT_LVAL && svrv < VT_CONST) {
573 if (IS_FREG(r))
574 arm64_strv(arm64_type_size(svtt), fltr(r), intr(svrv), 0);
575 else
576 arm64_strx(arm64_type_size(svtt), intr(r), intr(svrv), 0);
577 return;
580 if (svr == (VT_CONST | VT_LVAL | VT_SYM)) {
581 arm64_sym(30, sv->sym, svcul); // use x30 for address
582 if (IS_FREG(r))
583 arm64_strv(arm64_type_size(svtt), fltr(r), 30, 0);
584 else
585 arm64_strx(arm64_type_size(svtt), intr(r), 30, 0);
586 return;
589 printf("store(%x, (%x, %x, %llx))\n", r, svtt, sv->r, (long long)svcul);
590 assert(0);
593 static void arm64_gen_bl_or_b(int b)
595 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
596 assert(!b && (vtop->r & VT_SYM));
597 greloc(cur_text_section, vtop->sym, ind, R_AARCH64_CALL26);
598 o(0x94000000); // bl .
600 else
601 o(0xd61f0000 | (uint32_t)!b << 21 | intr(gv(RC_R30)) << 5); // br/blr
604 static int arm64_hfa_aux(CType *type, int *fsize, int num)
606 if (is_float(type->t)) {
607 int a, n = type_size(type, &a);
608 if (num >= 4 || (*fsize && *fsize != n))
609 return -1;
610 *fsize = n;
611 return num + 1;
613 else if ((type->t & VT_BTYPE) == VT_STRUCT) {
614 int is_struct = 0; // rather than union
615 Sym *field;
616 for (field = type->ref->next; field; field = field->next)
617 if (field->c) {
618 is_struct = 1;
619 break;
621 if (is_struct) {
622 int num0 = num;
623 for (field = type->ref->next; field; field = field->next) {
624 if (field->c != (num - num0) * *fsize)
625 return -1;
626 num = arm64_hfa_aux(&field->type, fsize, num);
627 if (num == -1)
628 return -1;
630 if (type->ref->c != (num - num0) * *fsize)
631 return -1;
632 return num;
634 else { // union
635 int num0 = num;
636 for (field = type->ref->next; field; field = field->next) {
637 int num1 = arm64_hfa_aux(&field->type, fsize, num0);
638 if (num1 == -1)
639 return -1;
640 num = num1 < num ? num : num1;
642 if (type->ref->c != (num - num0) * *fsize)
643 return -1;
644 return num;
647 else if (type->t & VT_ARRAY) {
648 int num1;
649 if (!type->ref->c)
650 return num;
651 num1 = arm64_hfa_aux(&type->ref->type, fsize, num);
652 if (num1 == -1 || (num1 != num && type->ref->c > 4))
653 return -1;
654 num1 = num + type->ref->c * (num1 - num);
655 if (num1 > 4)
656 return -1;
657 return num1;
659 return -1;
662 static int arm64_hfa(CType *type, int *fsize)
664 if ((type->t & VT_BTYPE) == VT_STRUCT || (type->t & VT_ARRAY)) {
665 int sz = 0;
666 int n = arm64_hfa_aux(type, &sz, 0);
667 if (0 < n && n <= 4) {
668 if (fsize)
669 *fsize = sz;
670 return n;
673 return 0;
676 static unsigned long arm64_pcs_aux(int n, CType **type, unsigned long *a)
678 int nx = 0; // next integer register
679 int nv = 0; // next vector register
680 unsigned long ns = 32; // next stack offset
681 int i;
683 for (i = 0; i < n; i++) {
684 int hfa = arm64_hfa(type[i], 0);
685 int size, align;
687 if ((type[i]->t & VT_ARRAY) ||
688 (type[i]->t & VT_BTYPE) == VT_FUNC)
689 size = align = 8;
690 else
691 size = type_size(type[i], &align);
693 if (hfa)
694 // B.2
696 else if (size > 16) {
697 // B.3: replace with pointer
698 if (nx < 8)
699 a[i] = nx++ << 1 | 1;
700 else {
701 ns = (ns + 7) & ~7;
702 a[i] = ns | 1;
703 ns += 8;
705 continue;
707 else if ((type[i]->t & VT_BTYPE) == VT_STRUCT)
708 // B.4
709 size = (size + 7) & ~7;
711 // C.1
712 if (is_float(type[i]->t) && nv < 8) {
713 a[i] = 16 + (nv++ << 1);
714 continue;
717 // C.2
718 if (hfa && nv + hfa <= 8) {
719 a[i] = 16 + (nv << 1);
720 nv += hfa;
721 continue;
724 // C.3
725 if (hfa) {
726 nv = 8;
727 size = (size + 7) & ~7;
730 // C.4
731 if (hfa || (type[i]->t & VT_BTYPE) == VT_LDOUBLE) {
732 ns = (ns + 7) & ~7;
733 ns = (ns + align - 1) & -align;
736 // C.5
737 if ((type[i]->t & VT_BTYPE) == VT_FLOAT)
738 size = 8;
740 // C.6
741 if (hfa || is_float(type[i]->t)) {
742 a[i] = ns;
743 ns += size;
744 continue;
747 // C.7
748 if ((type[i]->t & VT_BTYPE) != VT_STRUCT && size <= 8 && nx < 8) {
749 a[i] = nx++ << 1;
750 continue;
753 // C.8
754 if (align == 16)
755 nx = (nx + 1) & ~1;
757 // C.9
758 if ((type[i]->t & VT_BTYPE) != VT_STRUCT && size == 16 && nx < 7) {
759 a[i] = nx << 1;
760 nx += 2;
761 continue;
764 // C.10
765 if ((type[i]->t & VT_BTYPE) == VT_STRUCT && size <= (8 - nx) * 8) {
766 a[i] = nx << 1;
767 nx += (size + 7) >> 3;
768 continue;
771 // C.11
772 nx = 8;
774 // C.12
775 ns = (ns + 7) & ~7;
776 ns = (ns + align - 1) & -align;
778 // C.13
779 if ((type[i]->t & VT_BTYPE) == VT_STRUCT) {
780 a[i] = ns;
781 ns += size;
782 continue;
785 // C.14
786 if (size < 8)
787 size = 8;
789 // C.15
790 a[i] = ns;
791 ns += size;
794 return ns - 32;
797 static unsigned long arm64_pcs(int n, CType **type, unsigned long *a)
799 unsigned long stack;
801 // Return type:
802 if ((type[0]->t & VT_BTYPE) == VT_VOID)
803 a[0] = -1;
804 else {
805 arm64_pcs_aux(1, type, a);
806 assert(a[0] == 0 || a[0] == 1 || a[0] == 16);
809 // Argument types:
810 stack = arm64_pcs_aux(n, type + 1, a + 1);
812 if (0) {
813 int i;
814 for (i = 0; i <= n; i++) {
815 if (!i)
816 printf("arm64_pcs return: ");
817 else
818 printf("arm64_pcs arg %d: ", i);
819 if (a[i] == (unsigned long)-1)
820 printf("void\n");
821 else if (a[i] == 1 && !i)
822 printf("X8 pointer\n");
823 else if (a[i] < 16)
824 printf("X%lu%s\n", a[i] / 2, a[i] & 1 ? " pointer" : "");
825 else if (a[i] < 32)
826 printf("V%lu\n", a[i] / 2 - 8);
827 else
828 printf("stack %lu%s\n",
829 (a[i] - 32) & ~1, a[i] & 1 ? " pointer" : "");
833 return stack;
836 ST_FUNC void gfunc_call(int nb_args)
838 CType *return_type;
839 CType **t;
840 unsigned long *a, *a1;
841 unsigned long stack;
842 int i;
844 return_type = &vtop[-nb_args].type.ref->type;
845 if ((return_type->t & VT_BTYPE) == VT_STRUCT)
846 --nb_args;
848 t = tcc_malloc((nb_args + 1) * sizeof(*t));
849 a = tcc_malloc((nb_args + 1) * sizeof(*a));
850 a1 = tcc_malloc((nb_args + 1) * sizeof(*a1));
852 t[0] = return_type;
853 for (i = 0; i < nb_args; i++)
854 t[nb_args - i] = &vtop[-i].type;
856 stack = arm64_pcs(nb_args, t, a);
858 // Allocate space for structs replaced by pointer:
859 for (i = nb_args; i; i--)
860 if (a[i] & 1) {
861 SValue *arg = &vtop[i - nb_args];
862 int align, size = type_size(&arg->type, &align);
863 assert((arg->type.t & VT_BTYPE) == VT_STRUCT);
864 stack = (stack + align - 1) & -align;
865 a1[i] = stack;
866 stack += size;
869 stack = (stack + 15) >> 4 << 4;
871 assert(stack < 0x1000);
872 if (stack)
873 o(0xd10003ff | stack << 10); // sub sp,sp,#(n)
875 // First pass: set all values on stack
876 for (i = nb_args; i; i--) {
877 vpushv(vtop - nb_args + i);
879 if (a[i] & 1) {
880 // struct replaced by pointer
881 int r = get_reg(RC_INT);
882 arm64_spoff(intr(r), a1[i]);
883 vset(&vtop->type, r | VT_LVAL, 0);
884 vswap();
885 vstore();
886 if (a[i] >= 32) {
887 // pointer on stack
888 r = get_reg(RC_INT);
889 arm64_spoff(intr(r), a1[i]);
890 arm64_strx(3, intr(r), 31, (a[i] - 32) >> 1 << 1);
893 else if (a[i] >= 32) {
894 // value on stack
895 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
896 int r = get_reg(RC_INT);
897 arm64_spoff(intr(r), a[i] - 32);
898 vset(&vtop->type, r | VT_LVAL, 0);
899 vswap();
900 vstore();
902 else if (is_float(vtop->type.t)) {
903 gv(RC_FLOAT);
904 arm64_strv(arm64_type_size(vtop[0].type.t),
905 fltr(vtop[0].r), 31, a[i] - 32);
907 else {
908 gv(RC_INT);
909 arm64_strx(arm64_type_size(vtop[0].type.t),
910 intr(vtop[0].r), 31, a[i] - 32);
914 --vtop;
917 // Second pass: assign values to registers
918 for (i = nb_args; i; i--, vtop--) {
919 if (a[i] < 16 && !(a[i] & 1)) {
920 // value in general-purpose registers
921 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
922 int align, size = type_size(&vtop->type, &align);
923 vtop->type.t = VT_PTR;
924 gaddrof();
925 gv(RC_R(a[i] / 2));
926 arm64_ldrs(a[i] / 2, size);
928 else
929 gv(RC_R(a[i] / 2));
931 else if (a[i] < 16)
932 // struct replaced by pointer in register
933 arm64_spoff(a[i] / 2, a1[i]);
934 else if (a[i] < 32) {
935 // value in floating-point registers
936 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
937 uint32_t j, sz, n = arm64_hfa(&vtop->type, &sz);
938 vtop->type.t = VT_PTR;
939 gaddrof();
940 gv(RC_R30);
941 for (j = 0; j < n; j++)
942 o(0x3d4003c0 |
943 (sz & 16) << 19 | -(sz & 8) << 27 | (sz & 4) << 29 |
944 (a[i] / 2 - 8 + j) |
945 j << 10); // ldr ([sdq])(*),[x30,#(j * sz)]
947 else
948 gv(RC_F(a[i] / 2 - 8));
952 if ((return_type->t & VT_BTYPE) == VT_STRUCT) {
953 if (a[0] == 1) {
954 // indirect return: set x8 and discard the stack value
955 gv(RC_R(8));
956 --vtop;
958 else
959 // return in registers: keep the address for after the call
960 vswap();
963 save_regs(0);
964 arm64_gen_bl_or_b(0);
965 --vtop;
966 if (stack)
967 o(0x910003ff | stack << 10); // add sp,sp,#(n)
970 int rt = return_type->t;
971 int bt = rt & VT_BTYPE;
972 if (bt == VT_BYTE || bt == VT_SHORT)
973 // Promote small integers:
974 o(0x13001c00 | (bt == VT_SHORT) << 13 |
975 (uint32_t)!!(rt & VT_UNSIGNED) << 30); // [su]xt[bh] w0,w0
976 else if (bt == VT_STRUCT && !(a[0] & 1)) {
977 // A struct was returned in registers, so write it out:
978 gv(RC_R(8));
979 --vtop;
980 if (a[0] == 0) {
981 int align, size = type_size(return_type, &align);
982 assert(size <= 16);
983 if (size > 8)
984 o(0xa9000500); // stp x0,x1,[x8]
985 else if (size)
986 arm64_strx(size > 4 ? 3 : size > 2 ? 2 : size > 1, 0, 8, 0);
989 else if (a[0] == 16) {
990 uint32_t j, sz, n = arm64_hfa(return_type, &sz);
991 for (j = 0; j < n; j++)
992 o(0x3d000100 |
993 (sz & 16) << 19 | -(sz & 8) << 27 | (sz & 4) << 29 |
994 (a[i] / 2 - 8 + j) |
995 j << 10); // str ([sdq])(*),[x8,#(j * sz)]
1000 tcc_free(a1);
1001 tcc_free(a);
1002 tcc_free(t);
1005 static unsigned long arm64_func_va_list_stack;
1006 static int arm64_func_va_list_gr_offs;
1007 static int arm64_func_va_list_vr_offs;
1008 static int arm64_func_sub_sp_offset;
1010 ST_FUNC void gfunc_prolog(CType *func_type)
1012 int n = 0;
1013 int i = 0;
1014 Sym *sym;
1015 CType **t;
1016 unsigned long *a;
1018 // Why doesn't the caller (gen_function) set func_vt?
1019 func_vt = func_type->ref->type;
1020 func_vc = 144; // offset of where x8 is stored
1022 for (sym = func_type->ref; sym; sym = sym->next)
1023 ++n;
1024 t = tcc_malloc(n * sizeof(*t));
1025 a = tcc_malloc(n * sizeof(*a));
1027 for (sym = func_type->ref; sym; sym = sym->next)
1028 t[i++] = &sym->type;
1030 arm64_func_va_list_stack = arm64_pcs(n - 1, t, a);
1032 o(0xa9b27bfd); // stp x29,x30,[sp,#-224]!
1033 o(0xad0087e0); // stp q0,q1,[sp,#16]
1034 o(0xad018fe2); // stp q2,q3,[sp,#48]
1035 o(0xad0297e4); // stp q4,q5,[sp,#80]
1036 o(0xad039fe6); // stp q6,q7,[sp,#112]
1037 o(0xa90923e8); // stp x8,x8,[sp,#144]
1038 o(0xa90a07e0); // stp x0,x1,[sp,#160]
1039 o(0xa90b0fe2); // stp x2,x3,[sp,#176]
1040 o(0xa90c17e4); // stp x4,x5,[sp,#192]
1041 o(0xa90d1fe6); // stp x6,x7,[sp,#208]
1043 arm64_func_va_list_gr_offs = -64;
1044 arm64_func_va_list_vr_offs = -128;
1046 for (i = 1, sym = func_type->ref->next; sym; i++, sym = sym->next) {
1047 int off = (a[i] < 16 ? 160 + a[i] / 2 * 8 :
1048 a[i] < 32 ? 16 + (a[i] - 16) / 2 * 16 :
1049 224 + ((a[i] - 32) >> 1 << 1));
1050 sym_push(sym->v & ~SYM_FIELD, &sym->type,
1051 (a[i] & 1 ? VT_LLOCAL : VT_LOCAL) | lvalue_type(sym->type.t),
1052 off);
1054 if (a[i] < 16) {
1055 int align, size = type_size(&sym->type, &align);
1056 arm64_func_va_list_gr_offs = (a[i] / 2 - 7 +
1057 (!(a[i] & 1) && size > 8)) * 8;
1059 else if (a[i] < 32) {
1060 uint32_t hfa = arm64_hfa(&sym->type, 0);
1061 arm64_func_va_list_vr_offs = (a[i] / 2 - 16 +
1062 (hfa ? hfa : 1)) * 16;
1065 // HFAs of float and double need to be written differently:
1066 if (16 <= a[i] && a[i] < 32 && (sym->type.t & VT_BTYPE) == VT_STRUCT) {
1067 uint32_t j, sz, k = arm64_hfa(&sym->type, &sz);
1068 if (sz < 16)
1069 for (j = 0; j < k; j++) {
1070 o(0x3d0003e0 | -(sz & 8) << 27 | (sz & 4) << 29 |
1071 ((a[i] - 16) / 2 + j) | (off / sz + j) << 10);
1072 // str ([sdq])(*),[sp,#(j * sz)]
1077 tcc_free(a);
1078 tcc_free(t);
1080 o(0x910003fd); // mov x29,sp
1081 arm64_func_sub_sp_offset = ind;
1082 // In gfunc_epilog these will be replaced with code to decrement SP:
1083 o(0xd503201f); // nop
1084 o(0xd503201f); // nop
1085 loc = 0;
1088 ST_FUNC void gen_va_start(void)
1090 int r;
1091 --vtop; // we don't need the "arg"
1092 gaddrof();
1093 r = intr(gv(RC_INT));
1095 if (arm64_func_va_list_stack) {
1096 //xx could use add (immediate) here
1097 arm64_movimm(30, arm64_func_va_list_stack + 224);
1098 o(0x8b1e03be); // add x30,x29,x30
1100 else
1101 o(0x910383be); // add x30,x29,#224
1102 o(0xf900001e | r << 5); // str x30,[x(r)]
1104 if (arm64_func_va_list_gr_offs) {
1105 if (arm64_func_va_list_stack)
1106 o(0x910383be); // add x30,x29,#224
1107 o(0xf900041e | r << 5); // str x30,[x(r),#8]
1110 if (arm64_func_va_list_vr_offs) {
1111 o(0x910243be); // add x30,x29,#144
1112 o(0xf900081e | r << 5); // str x30,[x(r),#16]
1115 arm64_movimm(30, arm64_func_va_list_gr_offs);
1116 o(0xb900181e | r << 5); // str w30,[x(r),#24]
1118 arm64_movimm(30, arm64_func_va_list_vr_offs);
1119 o(0xb9001c1e | r << 5); // str w30,[x(r),#28]
1121 --vtop;
1124 ST_FUNC void gen_va_arg(CType *t)
1126 int align, size = type_size(t, &align);
1127 int fsize, hfa = arm64_hfa(t, &fsize);
1128 uint32_t r0, r1;
1130 if (is_float(t->t)) {
1131 hfa = 1;
1132 fsize = size;
1135 gaddrof();
1136 r0 = intr(gv(RC_INT));
1137 r1 = get_reg(RC_INT);
1138 vtop[0].r = r1 | lvalue_type(t->t);
1139 r1 = intr(r1);
1141 if (!hfa) {
1142 uint32_t n = size > 16 ? 8 : (size + 7) & -8;
1143 o(0xb940181e | r0 << 5); // ldr w30,[x(r0),#24] // __gr_offs
1144 if (align == 16) {
1145 assert(0); // this path untested but needed for __uint128_t
1146 o(0x11003fde); // add w30,w30,#15
1147 o(0x121c6fde); // and w30,w30,#-16
1149 o(0x310003c0 | r1 | n << 10); // adds w(r1),w30,#(n)
1150 o(0x540000ad); // b.le .+20
1151 o(0xf9400000 | r1 | r0 << 5); // ldr x(r1),[x(r0)] // __stack
1152 o(0x9100001e | r1 << 5 | n << 10); // add x30,x(r1),#(n)
1153 o(0xf900001e | r0 << 5); // str x30,[x(r0)] // __stack
1154 o(0x14000004); // b .+16
1155 o(0xb9001800 | r1 | r0 << 5); // str w(r1),[x(r0),#24] // __gr_offs
1156 o(0xf9400400 | r1 | r0 << 5); // ldr x(r1),[x(r0),#8] // __gr_top
1157 o(0x8b3ec000 | r1 | r1 << 5); // add x(r1),x(r1),w30,sxtw
1158 if (size > 16)
1159 o(0xf9400000 | r1 | r1 << 5); // ldr x(r1),[x(r1)]
1161 else {
1162 uint32_t rsz = hfa << 4;
1163 uint32_t ssz = (size + 7) & -(uint32_t)8;
1164 uint32_t b1, b2;
1165 o(0xb9401c1e | r0 << 5); // ldr w30,[x(r0),#28] // __vr_offs
1166 o(0x310003c0 | r1 | rsz << 10); // adds w(r1),w30,#(rsz)
1167 b1 = ind; o(0x5400000d); // b.le lab1
1168 o(0xf9400000 | r1 | r0 << 5); // ldr x(r1),[x(r0)] // __stack
1169 if (fsize == 16) {
1170 o(0x91003c00 | r1 | r1 << 5); // add x(r1),x(r1),#15
1171 o(0x927cec00 | r1 | r1 << 5); // and x(r1),x(r1),#-16
1173 o(0x9100001e | r1 << 5 | ssz << 10); // add x30,x(r1),#(ssz)
1174 o(0xf900001e | r0 << 5); // str x30,[x(r0)] // __stack
1175 b2 = ind; o(0x14000000); // b lab2
1176 // lab1:
1177 write32le(cur_text_section->data + b1, 0x5400000d | (ind - b1) << 3);
1178 o(0xb9001c00 | r1 | r0 << 5); // str w(r1),[x(r0),#28] // __vr_offs
1179 o(0xf9400800 | r1 | r0 << 5); // ldr x(r1),[x(r0),#16] // __vr_top
1180 if (hfa == 1 || fsize == 16)
1181 o(0x8b3ec000 | r1 | r1 << 5); // add x(r1),x(r1),w30,sxtw
1182 else {
1183 // We need to change the layout of this HFA.
1184 // Get some space on the stack using global variable "loc":
1185 loc = (loc - size) & -(uint32_t)align;
1186 o(0x8b3ec000 | 30 | r1 << 5); // add x30,x(r1),w30,sxtw
1187 arm64_movimm(r1, loc);
1188 o(0x8b0003a0 | r1 | r1 << 16); // add x(r1),x29,x(r1)
1189 o(0x4c402bdc | (uint32_t)fsize << 7 |
1190 (uint32_t)(hfa == 2) << 15 |
1191 (uint32_t)(hfa == 3) << 14); // ld1 {v28.(4s|2d),...},[x30]
1192 o(0x0d00801c | r1 << 5 | (fsize == 8) << 10 |
1193 (uint32_t)(hfa != 2) << 13 |
1194 (uint32_t)(hfa != 3) << 21); // st(hfa) {v28.(s|d),...}[0],[x(r1)]
1196 // lab2:
1197 write32le(cur_text_section->data + b2, 0x14000000 | (ind - b2) >> 2);
1201 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret,
1202 int *align, int *regsize)
1204 return 0;
1207 ST_FUNC void greturn(void)
1209 CType *t = &func_vt;
1210 unsigned long a;
1212 arm64_pcs(0, &t, &a);
1213 switch (a) {
1214 case -1:
1215 break;
1216 case 0:
1217 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
1218 int align, size = type_size(&func_vt, &align);
1219 gaddrof();
1220 gv(RC_R(0));
1221 arm64_ldrs(0, size);
1223 else
1224 gv(RC_IRET);
1225 break;
1226 case 1: {
1227 CType type = func_vt;
1228 mk_pointer(&type);
1229 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
1230 indir();
1231 vswap();
1232 vstore();
1233 break;
1235 case 16:
1236 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
1237 uint32_t j, sz, n = arm64_hfa(&vtop->type, &sz);
1238 gaddrof();
1239 gv(RC_R(0));
1240 for (j = 0; j < n; j++)
1241 o(0x3d400000 |
1242 (sz & 16) << 19 | -(sz & 8) << 27 | (sz & 4) << 29 |
1243 j | j << 10); // ldr ([sdq])(*),[x0,#(j * sz)]
1245 else
1246 gv(RC_FRET);
1247 break;
1248 default:
1249 assert(0);
1253 ST_FUNC void gfunc_epilog(void)
1255 if (loc) {
1256 // Insert instructions to subtract size of stack frame from SP.
1257 unsigned char *ptr = cur_text_section->data + arm64_func_sub_sp_offset;
1258 uint64_t diff = (-loc + 15) & ~15;
1259 if (!(diff >> 24)) {
1260 if (diff & 0xfff) // sub sp,sp,#(diff & 0xfff)
1261 write32le(ptr, 0xd10003ff | (diff & 0xfff) << 10);
1262 if (diff >> 12) // sub sp,sp,#(diff >> 12),lsl #12
1263 write32le(ptr + 4, 0xd14003ff | (diff >> 12) << 10);
1265 else {
1266 // In this case we may subtract more than necessary,
1267 // but always less than 17/16 of what we were aiming for.
1268 int i = 0;
1269 int j = 0;
1270 while (diff >> 20) {
1271 diff = (diff + 0xffff) >> 16;
1272 ++i;
1274 while (diff >> 16) {
1275 diff = (diff + 1) >> 1;
1276 ++j;
1278 write32le(ptr, 0xd2800010 | diff << 5 | i << 21);
1279 // mov x16,#(diff),lsl #(16 * i)
1280 write32le(ptr + 4, 0xcb3063ff | j << 10);
1281 // sub sp,sp,x16,lsl #(j)
1284 o(0x910003bf); // mov sp,x29
1285 o(0xa8ce7bfd); // ldp x29,x30,[sp],#224
1287 o(0xd65f03c0); // ret
1290 // Generate forward branch to label:
1291 ST_FUNC int gjmp(int t)
1293 int r = ind;
1294 o(t);
1295 return r;
1298 // Generate branch to known address:
1299 ST_FUNC void gjmp_addr(int a)
1301 assert(a - ind + 0x8000000 < 0x10000000);
1302 o(0x14000000 | ((a - ind) >> 2 & 0x3ffffff));
1305 ST_FUNC int gtst(int inv, int t)
1307 int bt = vtop->type.t & VT_BTYPE;
1308 if (bt == VT_LDOUBLE) {
1309 uint32_t a, b, f = fltr(gv(RC_FLOAT));
1310 a = get_reg(RC_INT);
1311 vpushi(0);
1312 vtop[0].r = a;
1313 b = get_reg(RC_INT);
1314 a = intr(a);
1315 b = intr(b);
1316 o(0x4e083c00 | a | f << 5); // mov x(a),v(f).d[0]
1317 o(0x4e183c00 | b | f << 5); // mov x(b),v(f).d[1]
1318 o(0xaa000400 | a | a << 5 | b << 16); // orr x(a),x(a),x(b),lsl #1
1319 o(0xb4000040 | a | !!inv << 24); // cbz/cbnz x(a),.+8
1320 --vtop;
1322 else if (bt == VT_FLOAT || bt == VT_DOUBLE) {
1323 uint32_t a = fltr(gv(RC_FLOAT));
1324 o(0x1e202008 | a << 5 | (bt != VT_FLOAT) << 22); // fcmp
1325 o(0x54000040 | !!inv); // b.eq/b.ne .+8
1327 else {
1328 uint32_t ll = (bt == VT_PTR || bt == VT_LLONG);
1329 uint32_t a = intr(gv(RC_INT));
1330 o(0x34000040 | a | !!inv << 24 | ll << 31); // cbz/cbnz wA,.+8
1332 --vtop;
1333 return gjmp(t);
1336 static int arm64_iconst(uint64_t *val, SValue *sv)
1338 if ((sv->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
1339 return 0;
1340 if (val) {
1341 int t = sv->type.t;
1342 *val = ((t & VT_BTYPE) == VT_LLONG ? sv->c.i :
1343 (uint32_t)sv->c.i |
1344 (t & VT_UNSIGNED ? 0 : -(sv->c.i & 0x80000000)));
1346 return 1;
1349 static int arm64_gen_opic(int op, uint32_t l, int rev, uint64_t val,
1350 uint32_t x, uint32_t a)
1352 if (op == '-' && !rev) {
1353 val = -val;
1354 op = '+';
1356 val = l ? val : (uint32_t)val;
1358 switch (op) {
1360 case '+': {
1361 uint32_t s = l ? val >> 63 : val >> 31;
1362 val = s ? -val : val;
1363 val = l ? val : (uint32_t)val;
1364 if (!(val & ~(uint64_t)0xfff))
1365 o(0x11000000 | l << 31 | s << 30 | x | a << 5 | val << 10);
1366 else if (!(val & ~(uint64_t)0xfff000))
1367 o(0x11400000 | l << 31 | s << 30 | x | a << 5 | val >> 12 << 10);
1368 else {
1369 arm64_movimm(30, val); // use x30
1370 o(0x0b1e0000 | l << 31 | s << 30 | x | a << 5);
1372 return 1;
1375 case '-':
1376 if (!val)
1377 o(0x4b0003e0 | l << 31 | x | a << 16); // neg
1378 else if (val == (l ? (uint64_t)-1 : (uint32_t)-1))
1379 o(0x2a2003e0 | l << 31 | x | a << 16); // mvn
1380 else {
1381 arm64_movimm(30, val); // use x30
1382 o(0x4b0003c0 | l << 31 | x | a << 16); // sub
1384 return 1;
1386 case '^':
1387 if (val == -1 || (val == 0xffffffff && !l)) {
1388 o(0x2a2003e0 | l << 31 | x | a << 16); // mvn
1389 return 1;
1391 // fall through
1392 case '&':
1393 case '|': {
1394 int e = arm64_encode_bimm64(l ? val : val | val << 32);
1395 if (e < 0)
1396 return 0;
1397 o((op == '&' ? 0x12000000 :
1398 op == '|' ? 0x32000000 : 0x52000000) |
1399 l << 31 | x | a << 5 | (uint32_t)e << 10);
1400 return 1;
1403 case TOK_SAR:
1404 case TOK_SHL:
1405 case TOK_SHR: {
1406 uint32_t n = 32 << l;
1407 val = val & (n - 1);
1408 if (rev)
1409 return 0;
1410 if (!val)
1411 assert(0);
1412 else if (op == TOK_SHL)
1413 o(0x53000000 | l << 31 | l << 22 | x | a << 5 |
1414 (n - val) << 16 | (n - 1 - val) << 10); // lsl
1415 else
1416 o(0x13000000 | (op == TOK_SHR) << 30 | l << 31 | l << 22 |
1417 x | a << 5 | val << 16 | (n - 1) << 10); // lsr/asr
1418 return 1;
1422 return 0;
1425 static void arm64_gen_opil(int op, uint32_t l)
1427 uint32_t x, a, b;
1429 // Special treatment for operations with a constant operand:
1431 uint64_t val;
1432 int rev = 1;
1434 if (arm64_iconst(0, &vtop[0])) {
1435 vswap();
1436 rev = 0;
1438 if (arm64_iconst(&val, &vtop[-1])) {
1439 gv(RC_INT);
1440 a = intr(vtop[0].r);
1441 --vtop;
1442 x = get_reg(RC_INT);
1443 ++vtop;
1444 if (arm64_gen_opic(op, l, rev, val, intr(x), a)) {
1445 vtop[0].r = x;
1446 vswap();
1447 --vtop;
1448 return;
1451 if (!rev)
1452 vswap();
1455 gv2(RC_INT, RC_INT);
1456 assert(vtop[-1].r < VT_CONST && vtop[0].r < VT_CONST);
1457 a = intr(vtop[-1].r);
1458 b = intr(vtop[0].r);
1459 vtop -= 2;
1460 x = get_reg(RC_INT);
1461 ++vtop;
1462 vtop[0].r = x;
1463 x = intr(x);
1465 switch (op) {
1466 case '%':
1467 // Use x30 for quotient:
1468 o(0x1ac00c00 | l << 31 | 30 | a << 5 | b << 16); // sdiv
1469 o(0x1b008000 | l << 31 | x | (uint32_t)30 << 5 |
1470 b << 16 | a << 10); // msub
1471 break;
1472 case '&':
1473 o(0x0a000000 | l << 31 | x | a << 5 | b << 16); // and
1474 break;
1475 case '*':
1476 o(0x1b007c00 | l << 31 | x | a << 5 | b << 16); // mul
1477 break;
1478 case '+':
1479 o(0x0b000000 | l << 31 | x | a << 5 | b << 16); // add
1480 break;
1481 case '-':
1482 o(0x4b000000 | l << 31 | x | a << 5 | b << 16); // sub
1483 break;
1484 case '/':
1485 o(0x1ac00c00 | l << 31 | x | a << 5 | b << 16); // sdiv
1486 break;
1487 case '^':
1488 o(0x4a000000 | l << 31 | x | a << 5 | b << 16); // eor
1489 break;
1490 case '|':
1491 o(0x2a000000 | l << 31 | x | a << 5 | b << 16); // orr
1492 break;
1493 case TOK_EQ:
1494 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1495 o(0x1a9f17e0 | x); // cset wA,eq
1496 break;
1497 case TOK_GE:
1498 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1499 o(0x1a9fb7e0 | x); // cset wA,ge
1500 break;
1501 case TOK_GT:
1502 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1503 o(0x1a9fd7e0 | x); // cset wA,gt
1504 break;
1505 case TOK_LE:
1506 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1507 o(0x1a9fc7e0 | x); // cset wA,le
1508 break;
1509 case TOK_LT:
1510 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1511 o(0x1a9fa7e0 | x); // cset wA,lt
1512 break;
1513 case TOK_NE:
1514 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1515 o(0x1a9f07e0 | x); // cset wA,ne
1516 break;
1517 case TOK_SAR:
1518 o(0x1ac02800 | l << 31 | x | a << 5 | b << 16); // asr
1519 break;
1520 case TOK_SHL:
1521 o(0x1ac02000 | l << 31 | x | a << 5 | b << 16); // lsl
1522 break;
1523 case TOK_SHR:
1524 o(0x1ac02400 | l << 31 | x | a << 5 | b << 16); // lsr
1525 break;
1526 case TOK_UDIV:
1527 case TOK_PDIV:
1528 o(0x1ac00800 | l << 31 | x | a << 5 | b << 16); // udiv
1529 break;
1530 case TOK_UGE:
1531 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1532 o(0x1a9f37e0 | x); // cset wA,cs
1533 break;
1534 case TOK_UGT:
1535 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1536 o(0x1a9f97e0 | x); // cset wA,hi
1537 break;
1538 case TOK_ULT:
1539 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1540 o(0x1a9f27e0 | x); // cset wA,cc
1541 break;
1542 case TOK_ULE:
1543 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1544 o(0x1a9f87e0 | x); // cset wA,ls
1545 break;
1546 case TOK_UMOD:
1547 // Use x30 for quotient:
1548 o(0x1ac00800 | l << 31 | 30 | a << 5 | b << 16); // udiv
1549 o(0x1b008000 | l << 31 | x | (uint32_t)30 << 5 |
1550 b << 16 | a << 10); // msub
1551 break;
1552 default:
1553 assert(0);
1557 ST_FUNC void gen_opi(int op)
1559 arm64_gen_opil(op, 0);
1562 ST_FUNC void gen_opl(int op)
1564 arm64_gen_opil(op, 1);
1567 ST_FUNC void gen_opf(int op)
1569 uint32_t x, a, b, dbl;
1571 if (vtop[0].type.t == VT_LDOUBLE) {
1572 CType type = vtop[0].type;
1573 int func = 0;
1574 int cond = -1;
1575 switch (op) {
1576 case '*': func = TOK___multf3; break;
1577 case '+': func = TOK___addtf3; break;
1578 case '-': func = TOK___subtf3; break;
1579 case '/': func = TOK___divtf3; break;
1580 case TOK_EQ: func = TOK___eqtf2; cond = 1; break;
1581 case TOK_NE: func = TOK___netf2; cond = 0; break;
1582 case TOK_LT: func = TOK___lttf2; cond = 10; break;
1583 case TOK_GE: func = TOK___getf2; cond = 11; break;
1584 case TOK_LE: func = TOK___letf2; cond = 12; break;
1585 case TOK_GT: func = TOK___gttf2; cond = 13; break;
1586 default: assert(0); break;
1588 vpush_global_sym(&func_old_type, func);
1589 vrott(3);
1590 gfunc_call(2);
1591 vpushi(0);
1592 vtop->r = cond < 0 ? REG_FRET : REG_IRET;
1593 if (cond < 0)
1594 vtop->type = type;
1595 else {
1596 o(0x7100001f); // cmp w0,#0
1597 o(0x1a9f07e0 | (uint32_t)cond << 12); // cset w0,(cond)
1599 return;
1602 dbl = vtop[0].type.t != VT_FLOAT;
1603 gv2(RC_FLOAT, RC_FLOAT);
1604 assert(vtop[-1].r < VT_CONST && vtop[0].r < VT_CONST);
1605 a = fltr(vtop[-1].r);
1606 b = fltr(vtop[0].r);
1607 vtop -= 2;
1608 switch (op) {
1609 case TOK_EQ: case TOK_NE:
1610 case TOK_LT: case TOK_GE: case TOK_LE: case TOK_GT:
1611 x = get_reg(RC_INT);
1612 ++vtop;
1613 vtop[0].r = x;
1614 x = intr(x);
1615 break;
1616 default:
1617 x = get_reg(RC_FLOAT);
1618 ++vtop;
1619 vtop[0].r = x;
1620 x = fltr(x);
1621 break;
1624 switch (op) {
1625 case '*':
1626 o(0x1e200800 | dbl << 22 | x | a << 5 | b << 16); // fmul
1627 break;
1628 case '+':
1629 o(0x1e202800 | dbl << 22 | x | a << 5 | b << 16); // fadd
1630 break;
1631 case '-':
1632 o(0x1e203800 | dbl << 22 | x | a << 5 | b << 16); // fsub
1633 break;
1634 case '/':
1635 o(0x1e201800 | dbl << 22 | x | a << 5 | b << 16); // fdiv
1636 break;
1637 case TOK_EQ:
1638 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1639 o(0x1a9f17e0 | x); // cset w(x),eq
1640 break;
1641 case TOK_GE:
1642 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1643 o(0x1a9fb7e0 | x); // cset w(x),ge
1644 break;
1645 case TOK_GT:
1646 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1647 o(0x1a9fd7e0 | x); // cset w(x),gt
1648 break;
1649 case TOK_LE:
1650 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1651 o(0x1a9f87e0 | x); // cset w(x),ls
1652 break;
1653 case TOK_LT:
1654 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1655 o(0x1a9f57e0 | x); // cset w(x),mi
1656 break;
1657 case TOK_NE:
1658 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1659 o(0x1a9f07e0 | x); // cset w(x),ne
1660 break;
1661 default:
1662 assert(0);
1666 // Generate sign extension from 32 to 64 bits:
1667 ST_FUNC void gen_cvt_sxtw(void)
1669 uint32_t r = intr(gv(RC_INT));
1670 o(0x93407c00 | r | r << 5); // sxtw x(r),w(r)
1673 ST_FUNC void gen_cvt_itof(int t)
1675 if (t == VT_LDOUBLE) {
1676 int f = vtop->type.t;
1677 int func = (f & VT_BTYPE) == VT_LLONG ?
1678 (f & VT_UNSIGNED ? TOK___floatunditf : TOK___floatditf) :
1679 (f & VT_UNSIGNED ? TOK___floatunsitf : TOK___floatsitf);
1680 vpush_global_sym(&func_old_type, func);
1681 vrott(2);
1682 gfunc_call(1);
1683 vpushi(0);
1684 vtop->type.t = t;
1685 vtop->r = REG_FRET;
1686 return;
1688 else {
1689 int d, n = intr(gv(RC_INT));
1690 int s = !(vtop->type.t & VT_UNSIGNED);
1691 uint32_t l = ((vtop->type.t & VT_BTYPE) == VT_LLONG);
1692 --vtop;
1693 d = get_reg(RC_FLOAT);
1694 ++vtop;
1695 vtop[0].r = d;
1696 o(0x1e220000 | (uint32_t)!s << 16 |
1697 (uint32_t)(t != VT_FLOAT) << 22 | fltr(d) |
1698 l << 31 | n << 5); // [us]cvtf [sd](d),[wx](n)
1702 ST_FUNC void gen_cvt_ftoi(int t)
1704 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
1705 int func = (t & VT_BTYPE) == VT_LLONG ?
1706 (t & VT_UNSIGNED ? TOK___fixunstfdi : TOK___fixtfdi) :
1707 (t & VT_UNSIGNED ? TOK___fixunstfsi : TOK___fixtfsi);
1708 vpush_global_sym(&func_old_type, func);
1709 vrott(2);
1710 gfunc_call(1);
1711 vpushi(0);
1712 vtop->type.t = t;
1713 vtop->r = REG_IRET;
1714 return;
1716 else {
1717 int d, n = fltr(gv(RC_FLOAT));
1718 uint32_t l = ((vtop->type.t & VT_BTYPE) != VT_FLOAT);
1719 --vtop;
1720 d = get_reg(RC_INT);
1721 ++vtop;
1722 vtop[0].r = d;
1723 o(0x1e380000 |
1724 (uint32_t)!!(t & VT_UNSIGNED) << 16 |
1725 (uint32_t)((t & VT_BTYPE) == VT_LLONG) << 31 | intr(d) |
1726 l << 22 | n << 5); // fcvtz[su] [wx](d),[sd](n)
1730 ST_FUNC void gen_cvt_ftof(int t)
1732 int f = vtop[0].type.t;
1733 assert(t == VT_FLOAT || t == VT_DOUBLE || t == VT_LDOUBLE);
1734 assert(f == VT_FLOAT || f == VT_DOUBLE || f == VT_LDOUBLE);
1735 if (t == f)
1736 return;
1738 if (t == VT_LDOUBLE || f == VT_LDOUBLE) {
1739 int func = (t == VT_LDOUBLE) ?
1740 (f == VT_FLOAT ? TOK___extendsftf2 : TOK___extenddftf2) :
1741 (t == VT_FLOAT ? TOK___trunctfsf2 : TOK___trunctfdf2);
1742 vpush_global_sym(&func_old_type, func);
1743 vrott(2);
1744 gfunc_call(1);
1745 vpushi(0);
1746 vtop->type.t = t;
1747 vtop->r = REG_FRET;
1749 else {
1750 int x, a;
1751 gv(RC_FLOAT);
1752 assert(vtop[0].r < VT_CONST);
1753 a = fltr(vtop[0].r);
1754 --vtop;
1755 x = get_reg(RC_FLOAT);
1756 ++vtop;
1757 vtop[0].r = x;
1758 x = fltr(x);
1760 if (f == VT_FLOAT)
1761 o(0x1e22c000 | x | a << 5); // fcvt d(x),s(a)
1762 else
1763 o(0x1e624000 | x | a << 5); // fcvt s(x),d(a)
1767 ST_FUNC void ggoto(void)
1769 arm64_gen_bl_or_b(1);
1770 --vtop;
1773 ST_FUNC void gen_clear_cache(void)
1775 uint32_t beg, end, dsz, isz, p, lab1, b1;
1776 gv2(RC_INT, RC_INT);
1777 vpushi(0);
1778 vtop->r = get_reg(RC_INT);
1779 vpushi(0);
1780 vtop->r = get_reg(RC_INT);
1781 vpushi(0);
1782 vtop->r = get_reg(RC_INT);
1783 beg = intr(vtop[-4].r); // x0
1784 end = intr(vtop[-3].r); // x1
1785 dsz = intr(vtop[-2].r); // x2
1786 isz = intr(vtop[-1].r); // x3
1787 p = intr(vtop[0].r); // x4
1788 vtop -= 5;
1790 o(0xd53b0020 | isz); // mrs x(isz),ctr_el0
1791 o(0x52800080 | p); // mov w(p),#4
1792 o(0x53104c00 | dsz | isz << 5); // ubfx w(dsz),w(isz),#16,#4
1793 o(0x1ac02000 | dsz | p << 5 | dsz << 16); // lsl w(dsz),w(p),w(dsz)
1794 o(0x12000c00 | isz | isz << 5); // and w(isz),w(isz),#15
1795 o(0x1ac02000 | isz | p << 5 | isz << 16); // lsl w(isz),w(p),w(isz)
1796 o(0x51000400 | p | dsz << 5); // sub w(p),w(dsz),#1
1797 o(0x8a240004 | p | beg << 5 | p << 16); // bic x(p),x(beg),x(p)
1798 b1 = ind; o(0x14000000); // b
1799 lab1 = ind;
1800 o(0xd50b7b20 | p); // dc cvau,x(p)
1801 o(0x8b000000 | p | p << 5 | dsz << 16); // add x(p),x(p),x(dsz)
1802 write32le(cur_text_section->data + b1, 0x14000000 | (ind - b1) >> 2);
1803 o(0xeb00001f | p << 5 | end << 16); // cmp x(p),x(end)
1804 o(0x54ffffa3 | ((lab1 - ind) << 3 & 0xffffe0)); // b.cc lab1
1805 o(0xd5033b9f); // dsb ish
1806 o(0x51000400 | p | isz << 5); // sub w(p),w(isz),#1
1807 o(0x8a240004 | p | beg << 5 | p << 16); // bic x(p),x(beg),x(p)
1808 b1 = ind; o(0x14000000); // b
1809 lab1 = ind;
1810 o(0xd50b7520 | p); // ic ivau,x(p)
1811 o(0x8b000000 | p | p << 5 | isz << 16); // add x(p),x(p),x(isz)
1812 write32le(cur_text_section->data + b1, 0x14000000 | (ind - b1) >> 2);
1813 o(0xeb00001f | p << 5 | end << 16); // cmp x(p),x(end)
1814 o(0x54ffffa3 | ((lab1 - ind) << 3 & 0xffffe0)); // b.cc lab1
1815 o(0xd5033b9f); // dsb ish
1816 o(0xd5033fdf); // isb
1819 ST_FUNC void gen_vla_sp_save(int addr) {
1820 uint32_t r = intr(get_reg(RC_INT));
1821 o(0x910003e0 | r); // mov x(r),sp
1822 arm64_strx(3, r, 29, addr);
1825 ST_FUNC void gen_vla_sp_restore(int addr) {
1826 // Use x30 because this function can be called when there
1827 // is a live return value in x0 but there is nothing on
1828 // the value stack to prevent get_reg from returning x0.
1829 uint32_t r = 30;
1830 arm64_ldrx(0, 3, r, 29, addr);
1831 o(0x9100001f | r << 5); // mov sp,x(r)
1834 ST_FUNC void gen_vla_alloc(CType *type, int align) {
1835 uint32_t r = intr(gv(RC_INT));
1836 o(0x91003c00 | r | r << 5); // add x(r),x(r),#15
1837 o(0x927cec00 | r | r << 5); // bic x(r),x(r),#15
1838 o(0xcb2063ff | r << 16); // sub sp,sp,x(r)
1839 vpop();
1842 /* end of A64 code generator */
1843 /*************************************************************/
1844 #endif
1845 /*************************************************************/