Fix bitfield long types for stabs/dwarf
[tinycc.git] / arm64-gen.c
blob8232f2a9440d1466dbc3ed35fd25c77ca32e1969
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 /* define if return values need to be extended explicitely
44 at caller side (for interfacing with non-TCC compilers) */
45 #define PROMOTE_RET
46 /******************************************************/
47 #else /* ! TARGET_DEFS_ONLY */
48 /******************************************************/
49 #define USING_GLOBALS
50 #include "tcc.h"
51 #include <assert.h>
53 ST_DATA const char * const target_machine_defs =
54 #if defined(__APPLE__)
55 "__aarch64__\0"
56 "__arm64__\0"
57 #else
58 "__aarch64__\0"
59 #endif
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 #if defined(CONFIG_TCC_BCHECK)
94 static addr_t func_bound_offset;
95 static unsigned long func_bound_ind;
96 ST_DATA int func_bound_add_epilog;
97 #endif
99 #define IS_FREG(x) ((x) >= TREG_F(0))
101 static uint32_t intr(int r)
103 assert(TREG_R(0) <= r && r <= TREG_R30);
104 return r < TREG_R30 ? r : 30;
107 static uint32_t fltr(int r)
109 assert(TREG_F(0) <= r && r <= TREG_F(7));
110 return r - TREG_F(0);
113 // Add an instruction to text section:
114 ST_FUNC void o(unsigned int c)
116 int ind1 = ind + 4;
117 if (nocode_wanted)
118 return;
119 if (ind1 > cur_text_section->data_allocated)
120 section_realloc(cur_text_section, ind1);
121 write32le(cur_text_section->data + ind, c);
122 ind = ind1;
125 static int arm64_encode_bimm64(uint64_t x)
127 int neg = x & 1;
128 int rep, pos, len;
130 if (neg)
131 x = ~x;
132 if (!x)
133 return -1;
135 if (x >> 2 == (x & (((uint64_t)1 << (64 - 2)) - 1)))
136 rep = 2, x &= ((uint64_t)1 << 2) - 1;
137 else if (x >> 4 == (x & (((uint64_t)1 << (64 - 4)) - 1)))
138 rep = 4, x &= ((uint64_t)1 << 4) - 1;
139 else if (x >> 8 == (x & (((uint64_t)1 << (64 - 8)) - 1)))
140 rep = 8, x &= ((uint64_t)1 << 8) - 1;
141 else if (x >> 16 == (x & (((uint64_t)1 << (64 - 16)) - 1)))
142 rep = 16, x &= ((uint64_t)1 << 16) - 1;
143 else if (x >> 32 == (x & (((uint64_t)1 << (64 - 32)) - 1)))
144 rep = 32, x &= ((uint64_t)1 << 32) - 1;
145 else
146 rep = 64;
148 pos = 0;
149 if (!(x & (((uint64_t)1 << 32) - 1))) x >>= 32, pos += 32;
150 if (!(x & (((uint64_t)1 << 16) - 1))) x >>= 16, pos += 16;
151 if (!(x & (((uint64_t)1 << 8) - 1))) x >>= 8, pos += 8;
152 if (!(x & (((uint64_t)1 << 4) - 1))) x >>= 4, pos += 4;
153 if (!(x & (((uint64_t)1 << 2) - 1))) x >>= 2, pos += 2;
154 if (!(x & (((uint64_t)1 << 1) - 1))) x >>= 1, pos += 1;
156 len = 0;
157 if (!(~x & (((uint64_t)1 << 32) - 1))) x >>= 32, len += 32;
158 if (!(~x & (((uint64_t)1 << 16) - 1))) x >>= 16, len += 16;
159 if (!(~x & (((uint64_t)1 << 8) - 1))) x >>= 8, len += 8;
160 if (!(~x & (((uint64_t)1 << 4) - 1))) x >>= 4, len += 4;
161 if (!(~x & (((uint64_t)1 << 2) - 1))) x >>= 2, len += 2;
162 if (!(~x & (((uint64_t)1 << 1) - 1))) x >>= 1, len += 1;
164 if (x)
165 return -1;
166 if (neg) {
167 pos = (pos + len) & (rep - 1);
168 len = rep - len;
170 return ((0x1000 & rep << 6) | (((rep - 1) ^ 31) << 1 & 63) |
171 ((rep - pos) & (rep - 1)) << 6 | (len - 1));
174 static uint32_t arm64_movi(int r, uint64_t x)
176 uint64_t m = 0xffff;
177 int e;
178 if (!(x & ~m))
179 return 0x52800000 | r | x << 5; // movz w(r),#(x)
180 if (!(x & ~(m << 16)))
181 return 0x52a00000 | r | x >> 11; // movz w(r),#(x >> 16),lsl #16
182 if (!(x & ~(m << 32)))
183 return 0xd2c00000 | r | x >> 27; // movz x(r),#(x >> 32),lsl #32
184 if (!(x & ~(m << 48)))
185 return 0xd2e00000 | r | x >> 43; // movz x(r),#(x >> 48),lsl #48
186 if ((x & ~m) == m << 16)
187 return (0x12800000 | r |
188 (~x << 5 & 0x1fffe0)); // movn w(r),#(~x)
189 if ((x & ~(m << 16)) == m)
190 return (0x12a00000 | r |
191 (~x >> 11 & 0x1fffe0)); // movn w(r),#(~x >> 16),lsl #16
192 if (!~(x | m))
193 return (0x92800000 | r |
194 (~x << 5 & 0x1fffe0)); // movn x(r),#(~x)
195 if (!~(x | m << 16))
196 return (0x92a00000 | r |
197 (~x >> 11 & 0x1fffe0)); // movn x(r),#(~x >> 16),lsl #16
198 if (!~(x | m << 32))
199 return (0x92c00000 | r |
200 (~x >> 27 & 0x1fffe0)); // movn x(r),#(~x >> 32),lsl #32
201 if (!~(x | m << 48))
202 return (0x92e00000 | r |
203 (~x >> 43 & 0x1fffe0)); // movn x(r),#(~x >> 32),lsl #32
204 if (!(x >> 32) && (e = arm64_encode_bimm64(x | x << 32)) >= 0)
205 return 0x320003e0 | r | (uint32_t)e << 10; // movi w(r),#(x)
206 if ((e = arm64_encode_bimm64(x)) >= 0)
207 return 0xb20003e0 | r | (uint32_t)e << 10; // movi x(r),#(x)
208 return 0;
211 static void arm64_movimm(int r, uint64_t x)
213 uint32_t i;
214 if ((i = arm64_movi(r, x)))
215 o(i); // a single MOV
216 else {
217 // MOVZ/MOVN and 1-3 MOVKs
218 int z = 0, m = 0;
219 uint32_t mov1 = 0xd2800000; // movz
220 uint64_t x1 = x;
221 for (i = 0; i < 64; i += 16) {
222 z += !(x >> i & 0xffff);
223 m += !(~x >> i & 0xffff);
225 if (m > z) {
226 x1 = ~x;
227 mov1 = 0x92800000; // movn
229 for (i = 0; i < 64; i += 16)
230 if (x1 >> i & 0xffff) {
231 o(mov1 | r | (x1 >> i & 0xffff) << 5 | i << 17);
232 // movz/movn x(r),#(*),lsl #(i)
233 break;
235 for (i += 16; i < 64; i += 16)
236 if (x1 >> i & 0xffff)
237 o(0xf2800000 | r | (x >> i & 0xffff) << 5 | i << 17);
238 // movk x(r),#(*),lsl #(i)
242 // Patch all branches in list pointed to by t to branch to a:
243 ST_FUNC void gsym_addr(int t_, int a_)
245 uint32_t t = t_;
246 uint32_t a = a_;
247 while (t) {
248 unsigned char *ptr = cur_text_section->data + t;
249 uint32_t next = read32le(ptr);
250 if (a - t + 0x8000000 >= 0x10000000)
251 tcc_error("branch out of range");
252 write32le(ptr, (a - t == 4 ? 0xd503201f : // nop
253 0x14000000 | ((a - t) >> 2 & 0x3ffffff))); // b
254 t = next;
258 static int arm64_type_size(int t)
261 * case values are in increasing order (from 1 to 11).
262 * which 'may' help compiler optimizers. See tcc.h
264 switch (t & VT_BTYPE) {
265 case VT_BYTE: return 0;
266 case VT_SHORT: return 1;
267 case VT_INT: return 2;
268 case VT_LLONG: return 3;
269 case VT_PTR: return 3;
270 case VT_FUNC: return 3;
271 case VT_STRUCT: return 3;
272 case VT_FLOAT: return 2;
273 case VT_DOUBLE: return 3;
274 case VT_LDOUBLE: return 4;
275 case VT_BOOL: return 0;
277 assert(0);
278 return 0;
281 static void arm64_spoff(int reg, uint64_t off)
283 uint32_t sub = off >> 63;
284 if (sub)
285 off = -off;
286 if (off < 4096)
287 o(0x910003e0 | sub << 30 | reg | off << 10);
288 // (add|sub) x(reg),sp,#(off)
289 else {
290 arm64_movimm(30, off); // use x30 for offset
291 o(0x8b3e63e0 | sub << 30 | reg); // (add|sub) x(reg),sp,x30
295 /* invert 0: return value to use for store/load */
296 /* invert 1: return value to use for arm64_sym */
297 static uint64_t arm64_check_offset(int invert, int sz_, uint64_t off)
299 uint32_t sz = sz_;
300 if (!(off & ~((uint32_t)0xfff << sz)) ||
301 (off < 256 || -off <= 256))
302 return invert ? off : 0ul;
303 else if ((off & ((uint32_t)0xfff << sz)))
304 return invert ? off & ((uint32_t)0xfff << sz)
305 : off & ~((uint32_t)0xfff << sz);
306 else if (off & 0x1ff)
307 return invert ? off & 0x1ff : off & ~0x1ff;
308 else
309 return invert ? 0ul : off;
312 static void arm64_ldrx(int sg, int sz_, int dst, int bas, uint64_t off)
314 uint32_t sz = sz_;
315 if (sz >= 2)
316 sg = 0;
317 if (!(off & ~((uint32_t)0xfff << sz)))
318 o(0x39400000 | dst | bas << 5 | off << (10 - sz) |
319 (uint32_t)!!sg << 23 | sz << 30); // ldr(*) x(dst),[x(bas),#(off)]
320 else if (off < 256 || -off <= 256)
321 o(0x38400000 | dst | bas << 5 | (off & 511) << 12 |
322 (uint32_t)!!sg << 23 | sz << 30); // ldur(*) x(dst),[x(bas),#(off)]
323 else {
324 arm64_movimm(30, off); // use x30 for offset
325 o(0x38206800 | dst | bas << 5 | (uint32_t)30 << 16 |
326 (uint32_t)(!!sg + 1) << 22 | sz << 30); // ldr(*) x(dst),[x(bas),x30]
330 static void arm64_ldrv(int sz_, int dst, int bas, uint64_t off)
332 uint32_t sz = sz_;
333 if (!(off & ~((uint32_t)0xfff << sz)))
334 o(0x3d400000 | dst | bas << 5 | off << (10 - sz) |
335 (sz & 4) << 21 | (sz & 3) << 30); // ldr (s|d|q)(dst),[x(bas),#(off)]
336 else if (off < 256 || -off <= 256)
337 o(0x3c400000 | dst | bas << 5 | (off & 511) << 12 |
338 (sz & 4) << 21 | (sz & 3) << 30); // ldur (s|d|q)(dst),[x(bas),#(off)]
339 else {
340 arm64_movimm(30, off); // use x30 for offset
341 o(0x3c606800 | dst | bas << 5 | (uint32_t)30 << 16 |
342 sz << 30 | (sz & 4) << 21); // ldr (s|d|q)(dst),[x(bas),x30]
346 static void arm64_ldrs(int reg_, int size)
348 uint32_t reg = reg_;
349 // Use x30 for intermediate value in some cases.
350 switch (size) {
351 default: assert(0); break;
352 case 0:
353 /* Can happen with zero size structs */
354 break;
355 case 1:
356 arm64_ldrx(0, 0, reg, reg, 0);
357 break;
358 case 2:
359 arm64_ldrx(0, 1, reg, reg, 0);
360 break;
361 case 3:
362 arm64_ldrx(0, 1, 30, reg, 0);
363 arm64_ldrx(0, 0, reg, reg, 2);
364 o(0x2a0043c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #16
365 break;
366 case 4:
367 arm64_ldrx(0, 2, reg, reg, 0);
368 break;
369 case 5:
370 arm64_ldrx(0, 2, 30, reg, 0);
371 arm64_ldrx(0, 0, reg, reg, 4);
372 o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32
373 break;
374 case 6:
375 arm64_ldrx(0, 2, 30, reg, 0);
376 arm64_ldrx(0, 1, reg, reg, 4);
377 o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32
378 break;
379 case 7:
380 arm64_ldrx(0, 2, 30, reg, 0);
381 arm64_ldrx(0, 2, reg, reg, 3);
382 o(0x53087c00 | reg | reg << 5); // lsr w(reg), w(reg), #8
383 o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32
384 break;
385 case 8:
386 arm64_ldrx(0, 3, reg, reg, 0);
387 break;
388 case 9:
389 arm64_ldrx(0, 0, reg + 1, reg, 8);
390 arm64_ldrx(0, 3, reg, reg, 0);
391 break;
392 case 10:
393 arm64_ldrx(0, 1, reg + 1, reg, 8);
394 arm64_ldrx(0, 3, reg, reg, 0);
395 break;
396 case 11:
397 arm64_ldrx(0, 2, reg + 1, reg, 7);
398 o(0x53087c00 | (reg+1) | (reg+1) << 5); // lsr w(reg+1), w(reg+1), #8
399 arm64_ldrx(0, 3, reg, reg, 0);
400 break;
401 case 12:
402 arm64_ldrx(0, 2, reg + 1, reg, 8);
403 arm64_ldrx(0, 3, reg, reg, 0);
404 break;
405 case 13:
406 arm64_ldrx(0, 3, reg + 1, reg, 5);
407 o(0xd358fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #24
408 arm64_ldrx(0, 3, reg, reg, 0);
409 break;
410 case 14:
411 arm64_ldrx(0, 3, reg + 1, reg, 6);
412 o(0xd350fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #16
413 arm64_ldrx(0, 3, reg, reg, 0);
414 break;
415 case 15:
416 arm64_ldrx(0, 3, reg + 1, reg, 7);
417 o(0xd348fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #8
418 arm64_ldrx(0, 3, reg, reg, 0);
419 break;
420 case 16:
421 o(0xa9400000 | reg | (reg+1) << 10 | reg << 5);
422 // ldp x(reg),x(reg+1),[x(reg)]
423 break;
427 static void arm64_strx(int sz_, int dst, int bas, uint64_t off)
429 uint32_t sz = sz_;
430 if (!(off & ~((uint32_t)0xfff << sz)))
431 o(0x39000000 | dst | bas << 5 | off << (10 - sz) | sz << 30);
432 // str(*) x(dst),[x(bas],#(off)]
433 else if (off < 256 || -off <= 256)
434 o(0x38000000 | dst | bas << 5 | (off & 511) << 12 | sz << 30);
435 // stur(*) x(dst),[x(bas],#(off)]
436 else {
437 arm64_movimm(30, off); // use x30 for offset
438 o(0x38206800 | dst | bas << 5 | (uint32_t)30 << 16 | sz << 30);
439 // str(*) x(dst),[x(bas),x30]
443 static void arm64_strv(int sz_, int dst, int bas, uint64_t off)
445 uint32_t sz = sz_;
446 if (!(off & ~((uint32_t)0xfff << sz)))
447 o(0x3d000000 | dst | bas << 5 | off << (10 - sz) |
448 (sz & 4) << 21 | (sz & 3) << 30); // str (s|d|q)(dst),[x(bas),#(off)]
449 else if (off < 256 || -off <= 256)
450 o(0x3c000000 | dst | bas << 5 | (off & 511) << 12 |
451 (sz & 4) << 21 | (sz & 3) << 30); // stur (s|d|q)(dst),[x(bas),#(off)]
452 else {
453 arm64_movimm(30, off); // use x30 for offset
454 o(0x3c206800 | dst | bas << 5 | (uint32_t)30 << 16 |
455 sz << 30 | (sz & 4) << 21); // str (s|d|q)(dst),[x(bas),x30]
459 static void arm64_sym(int r, Sym *sym, unsigned long addend)
461 greloca(cur_text_section, sym, ind, R_AARCH64_ADR_GOT_PAGE, 0);
462 o(0x90000000 | r); // adrp xr, #sym
463 greloca(cur_text_section, sym, ind, R_AARCH64_LD64_GOT_LO12_NC, 0);
464 o(0xf9400000 | r | (r << 5)); // ld xr,[xr, #sym]
465 if (addend) {
466 // add xr, xr, #addend
467 if (addend & 0xffful)
468 o(0x91000000 | r | r << 5 | (addend & 0xfff) << 10);
469 if (addend > 0xffful) {
470 // add xr, xr, #addend, lsl #12
471 if (addend & 0xfff000ul)
472 o(0x91400000 | r | r << 5 | ((addend >> 12) & 0xfff) << 10);
473 if (addend > 0xfffffful) {
474 /* very unlikely */
475 int t = r ? 0 : 1;
476 o(0xf81f0fe0 | t); /* str xt, [sp, #-16]! */
477 arm64_movimm(t, addend & ~0xfffffful); // use xt for addent
478 o(0x91000000 | r | (t << 5)); /* add xr, xt, #0 */
479 o(0xf84107e0 | t); /* ldr xt, [sp], #16 */
485 static void arm64_load_cmp(int r, SValue *sv);
487 ST_FUNC void load(int r, SValue *sv)
489 int svtt = sv->type.t;
490 int svr = sv->r & ~VT_BOUNDED;
491 int svrv = svr & VT_VALMASK;
492 uint64_t svcul = (uint32_t)sv->c.i;
493 svcul = svcul >> 31 & 1 ? svcul - ((uint64_t)1 << 32) : svcul;
495 if (svr == (VT_LOCAL | VT_LVAL)) {
496 if (IS_FREG(r))
497 arm64_ldrv(arm64_type_size(svtt), fltr(r), 29, svcul);
498 else
499 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
500 intr(r), 29, svcul);
501 return;
504 if (svr == (VT_CONST | VT_LVAL)) {
505 if (sv->sym)
506 arm64_sym(30, sv->sym, // use x30 for address
507 arm64_check_offset(0, arm64_type_size(svtt), sv->c.i));
508 else
509 arm64_movimm (30, sv->c.i);
510 if (IS_FREG(r))
511 arm64_ldrv(arm64_type_size(svtt), fltr(r), 30,
512 arm64_check_offset(1, arm64_type_size(svtt), sv->c.i));
513 else
514 arm64_ldrx(!(svtt&VT_UNSIGNED), arm64_type_size(svtt), intr(r), 30,
515 arm64_check_offset(1, arm64_type_size(svtt), sv->c.i));
516 return;
519 if ((svr & ~VT_VALMASK) == VT_LVAL && svrv < VT_CONST) {
520 if ((svtt & VT_BTYPE) != VT_VOID) {
521 if (IS_FREG(r))
522 arm64_ldrv(arm64_type_size(svtt), fltr(r), intr(svrv), 0);
523 else
524 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
525 intr(r), intr(svrv), 0);
527 return;
530 if (svr == (VT_CONST | VT_LVAL | VT_SYM)) {
531 arm64_sym(30, sv->sym, // use x30 for address
532 arm64_check_offset(0, arm64_type_size(svtt), svcul));
533 if (IS_FREG(r))
534 arm64_ldrv(arm64_type_size(svtt), fltr(r), 30,
535 arm64_check_offset(1, arm64_type_size(svtt), svcul));
536 else
537 arm64_ldrx(!(svtt&VT_UNSIGNED), arm64_type_size(svtt), intr(r), 30,
538 arm64_check_offset(1, arm64_type_size(svtt), svcul));
539 return;
542 if (svr == (VT_CONST | VT_SYM)) {
543 arm64_sym(intr(r), sv->sym, svcul);
544 return;
547 if (svr == VT_CONST) {
548 if ((svtt & VT_BTYPE) != VT_VOID)
549 arm64_movimm(intr(r), arm64_type_size(svtt) == 3 ?
550 sv->c.i : (uint32_t)svcul);
551 return;
554 if (svr < VT_CONST) {
555 if (IS_FREG(r) && IS_FREG(svr))
556 if (svtt == VT_LDOUBLE)
557 o(0x4ea01c00 | fltr(r) | fltr(svr) << 5);
558 // mov v(r).16b,v(svr).16b
559 else
560 o(0x1e604000 | fltr(r) | fltr(svr) << 5); // fmov d(r),d(svr)
561 else if (!IS_FREG(r) && !IS_FREG(svr))
562 o(0xaa0003e0 | intr(r) | intr(svr) << 16); // mov x(r),x(svr)
563 else
564 assert(0);
565 return;
568 if (svr == VT_LOCAL) {
569 if (-svcul < 0x1000)
570 o(0xd10003a0 | intr(r) | -svcul << 10); // sub x(r),x29,#...
571 else {
572 arm64_movimm(30, -svcul); // use x30 for offset
573 o(0xcb0003a0 | intr(r) | (uint32_t)30 << 16); // sub x(r),x29,x30
575 return;
578 if (svr == VT_JMP || svr == VT_JMPI) {
579 int t = (svr == VT_JMPI);
580 arm64_movimm(intr(r), t);
581 o(0x14000002); // b .+8
582 gsym(svcul);
583 arm64_movimm(intr(r), t ^ 1);
584 return;
587 if (svr == (VT_LLOCAL | VT_LVAL)) {
588 arm64_ldrx(0, 3, 30, 29, svcul); // use x30 for offset
589 if (IS_FREG(r))
590 arm64_ldrv(arm64_type_size(svtt), fltr(r), 30, 0);
591 else
592 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
593 intr(r), 30, 0);
594 return;
597 if (svr == VT_CMP) {
598 arm64_load_cmp(r, sv);
599 return;
602 printf("load(%x, (%x, %x, %lx))\n", r, svtt, sv->r, (long)svcul);
603 assert(0);
606 ST_FUNC void store(int r, SValue *sv)
608 int svtt = sv->type.t;
609 int svr = sv->r & ~VT_BOUNDED;
610 int svrv = svr & VT_VALMASK;
611 uint64_t svcul = (uint32_t)sv->c.i;
612 svcul = svcul >> 31 & 1 ? svcul - ((uint64_t)1 << 32) : svcul;
614 if (svr == (VT_LOCAL | VT_LVAL)) {
615 if (IS_FREG(r))
616 arm64_strv(arm64_type_size(svtt), fltr(r), 29, svcul);
617 else
618 arm64_strx(arm64_type_size(svtt), intr(r), 29, svcul);
619 return;
622 if (svr == (VT_CONST | VT_LVAL)) {
623 if (sv->sym)
624 arm64_sym(30, sv->sym, // use x30 for address
625 arm64_check_offset(0, arm64_type_size(svtt), sv->c.i));
626 else
627 arm64_movimm (30, sv->c.i);
628 if (IS_FREG(r))
629 arm64_strv(arm64_type_size(svtt), fltr(r), 30,
630 arm64_check_offset(1, arm64_type_size(svtt), sv->c.i));
631 else
632 arm64_strx(arm64_type_size(svtt), intr(r), 30,
633 arm64_check_offset(1, arm64_type_size(svtt), sv->c.i));
634 return;
637 if ((svr & ~VT_VALMASK) == VT_LVAL && svrv < VT_CONST) {
638 if (IS_FREG(r))
639 arm64_strv(arm64_type_size(svtt), fltr(r), intr(svrv), 0);
640 else
641 arm64_strx(arm64_type_size(svtt), intr(r), intr(svrv), 0);
642 return;
645 if (svr == (VT_CONST | VT_LVAL | VT_SYM)) {
646 arm64_sym(30, sv->sym, // use x30 for address
647 arm64_check_offset(0, arm64_type_size(svtt), svcul));
648 if (IS_FREG(r))
649 arm64_strv(arm64_type_size(svtt), fltr(r), 30,
650 arm64_check_offset(1, arm64_type_size(svtt), svcul));
651 else
652 arm64_strx(arm64_type_size(svtt), intr(r), 30,
653 arm64_check_offset(1, arm64_type_size(svtt), svcul));
654 return;
657 printf("store(%x, (%x, %x, %lx))\n", r, svtt, sv->r, (long)svcul);
658 assert(0);
661 static void arm64_gen_bl_or_b(int b)
663 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST && (vtop->r & VT_SYM)) {
664 greloca(cur_text_section, vtop->sym, ind,
665 b ? R_AARCH64_JUMP26 : R_AARCH64_CALL26, 0);
666 o(0x14000000 | (uint32_t)!b << 31); // b/bl .
668 else {
669 #ifdef CONFIG_TCC_BCHECK
670 vtop->r &= ~VT_MUSTBOUND;
671 #endif
672 o(0xd61f0000 | (uint32_t)!b << 21 | intr(gv(RC_R30)) << 5); // br/blr
676 #if defined(CONFIG_TCC_BCHECK)
678 static void gen_bounds_call(int v)
680 Sym *sym = external_helper_sym(v);
682 greloca(cur_text_section, sym, ind, R_AARCH64_CALL26, 0);
683 o(0x94000000); // bl
686 static void gen_bounds_prolog(void)
688 /* leave some room for bound checking code */
689 func_bound_offset = lbounds_section->data_offset;
690 func_bound_ind = ind;
691 func_bound_add_epilog = 0;
692 o(0xd503201f); /* nop -> mov x0, lbound section pointer */
693 o(0xd503201f);
694 o(0xd503201f);
695 o(0xd503201f); /* nop -> call __bound_local_new */
698 static void gen_bounds_epilog(void)
700 addr_t saved_ind;
701 addr_t *bounds_ptr;
702 Sym *sym_data;
703 int offset_modified = func_bound_offset != lbounds_section->data_offset;
705 if (!offset_modified && !func_bound_add_epilog)
706 return;
708 /* add end of table info */
709 bounds_ptr = section_ptr_add(lbounds_section, sizeof(addr_t));
710 *bounds_ptr = 0;
712 sym_data = get_sym_ref(&char_pointer_type, lbounds_section,
713 func_bound_offset, PTR_SIZE);
715 /* generate bound local allocation */
716 if (offset_modified) {
717 saved_ind = ind;
718 ind = func_bound_ind;
719 greloca(cur_text_section, sym_data, ind, R_AARCH64_ADR_GOT_PAGE, 0);
720 o(0x90000000 | 0); // adrp x0, #sym_data
721 greloca(cur_text_section, sym_data, ind, R_AARCH64_LD64_GOT_LO12_NC, 0);
722 o(0xf9400000 | 0 | (0 << 5)); // ld x0,[x0, #sym_data]
723 gen_bounds_call(TOK___bound_local_new);
724 ind = saved_ind;
727 /* generate bound check local freeing */
728 o(0xa9bf07e0); /* stp x0, x1, [sp, #-16]! */
729 o(0x3c9f0fe0); /* str q0, [sp, #-16]! */
730 greloca(cur_text_section, sym_data, ind, R_AARCH64_ADR_GOT_PAGE, 0);
731 o(0x90000000 | 0); // adrp x0, #sym_data
732 greloca(cur_text_section, sym_data, ind, R_AARCH64_LD64_GOT_LO12_NC, 0);
733 o(0xf9400000 | 0 | (0 << 5)); // ld x0,[x0, #sym_data]
734 gen_bounds_call(TOK___bound_local_delete);
735 o(0x3cc107e0); /* ldr q0, [sp], #16 */
736 o(0xa8c107e0); /* ldp x0, x1, [sp], #16 */
738 #endif
740 static int arm64_hfa_aux(CType *type, int *fsize, int num)
742 if (is_float(type->t)) {
743 int a, n = type_size(type, &a);
744 if (num >= 4 || (*fsize && *fsize != n))
745 return -1;
746 *fsize = n;
747 return num + 1;
749 else if ((type->t & VT_BTYPE) == VT_STRUCT) {
750 int is_struct = 0; // rather than union
751 Sym *field;
752 for (field = type->ref->next; field; field = field->next)
753 if (field->c) {
754 is_struct = 1;
755 break;
757 if (is_struct) {
758 int num0 = num;
759 for (field = type->ref->next; field; field = field->next) {
760 if (field->c != (num - num0) * *fsize)
761 return -1;
762 num = arm64_hfa_aux(&field->type, fsize, num);
763 if (num == -1)
764 return -1;
766 if (type->ref->c != (num - num0) * *fsize)
767 return -1;
768 return num;
770 else { // union
771 int num0 = num;
772 for (field = type->ref->next; field; field = field->next) {
773 int num1 = arm64_hfa_aux(&field->type, fsize, num0);
774 if (num1 == -1)
775 return -1;
776 num = num1 < num ? num : num1;
778 if (type->ref->c != (num - num0) * *fsize)
779 return -1;
780 return num;
783 else if ((type->t & VT_ARRAY) && ((type->t & VT_BTYPE) != VT_PTR)) {
784 int num1;
785 if (!type->ref->c)
786 return num;
787 num1 = arm64_hfa_aux(&type->ref->type, fsize, num);
788 if (num1 == -1 || (num1 != num && type->ref->c > 4))
789 return -1;
790 num1 = num + type->ref->c * (num1 - num);
791 if (num1 > 4)
792 return -1;
793 return num1;
795 return -1;
798 static int arm64_hfa(CType *type, unsigned *fsize)
800 if ((type->t & VT_BTYPE) == VT_STRUCT ||
801 ((type->t & VT_ARRAY) && ((type->t & VT_BTYPE) != VT_PTR))) {
802 int sz = 0;
803 int n = arm64_hfa_aux(type, &sz, 0);
804 if (0 < n && n <= 4) {
805 if (fsize)
806 *fsize = sz;
807 return n;
810 return 0;
813 static unsigned long arm64_pcs_aux(int n, CType **type, unsigned long *a)
815 int nx = 0; // next integer register
816 int nv = 0; // next vector register
817 unsigned long ns = 32; // next stack offset
818 int i;
820 for (i = 0; i < n; i++) {
821 int hfa = arm64_hfa(type[i], 0);
822 int size, align;
824 if ((type[i]->t & VT_ARRAY) ||
825 (type[i]->t & VT_BTYPE) == VT_FUNC)
826 size = align = 8;
827 else
828 size = type_size(type[i], &align);
830 if (hfa)
831 // B.2
833 else if (size > 16) {
834 // B.3: replace with pointer
835 if (nx < 8)
836 a[i] = nx++ << 1 | 1;
837 else {
838 ns = (ns + 7) & ~7;
839 a[i] = ns | 1;
840 ns += 8;
842 continue;
844 else if ((type[i]->t & VT_BTYPE) == VT_STRUCT)
845 // B.4
846 size = (size + 7) & ~7;
848 // C.1
849 if (is_float(type[i]->t) && nv < 8) {
850 a[i] = 16 + (nv++ << 1);
851 continue;
854 // C.2
855 if (hfa && nv + hfa <= 8) {
856 a[i] = 16 + (nv << 1);
857 nv += hfa;
858 continue;
861 // C.3
862 if (hfa) {
863 nv = 8;
864 size = (size + 7) & ~7;
867 // C.4
868 if (hfa || (type[i]->t & VT_BTYPE) == VT_LDOUBLE) {
869 ns = (ns + 7) & ~7;
870 ns = (ns + align - 1) & -align;
873 // C.5
874 if ((type[i]->t & VT_BTYPE) == VT_FLOAT)
875 size = 8;
877 // C.6
878 if (hfa || is_float(type[i]->t)) {
879 a[i] = ns;
880 ns += size;
881 continue;
884 // C.7
885 if ((type[i]->t & VT_BTYPE) != VT_STRUCT && size <= 8 && nx < 8) {
886 a[i] = nx++ << 1;
887 continue;
890 // C.8
891 if (align == 16)
892 nx = (nx + 1) & ~1;
894 // C.9
895 if ((type[i]->t & VT_BTYPE) != VT_STRUCT && size == 16 && nx < 7) {
896 a[i] = nx << 1;
897 nx += 2;
898 continue;
901 // C.10
902 if ((type[i]->t & VT_BTYPE) == VT_STRUCT && size <= (8 - nx) * 8) {
903 a[i] = nx << 1;
904 nx += (size + 7) >> 3;
905 continue;
908 // C.11
909 nx = 8;
911 // C.12
912 ns = (ns + 7) & ~7;
913 ns = (ns + align - 1) & -align;
915 // C.13
916 if ((type[i]->t & VT_BTYPE) == VT_STRUCT) {
917 a[i] = ns;
918 ns += size;
919 continue;
922 // C.14
923 if (size < 8)
924 size = 8;
926 // C.15
927 a[i] = ns;
928 ns += size;
931 return ns - 32;
934 static unsigned long arm64_pcs(int n, CType **type, unsigned long *a)
936 unsigned long stack;
938 // Return type:
939 if ((type[0]->t & VT_BTYPE) == VT_VOID)
940 a[0] = -1;
941 else {
942 arm64_pcs_aux(1, type, a);
943 assert(a[0] == 0 || a[0] == 1 || a[0] == 16);
946 // Argument types:
947 stack = arm64_pcs_aux(n, type + 1, a + 1);
949 if (0) {
950 int i;
951 for (i = 0; i <= n; i++) {
952 if (!i)
953 printf("arm64_pcs return: ");
954 else
955 printf("arm64_pcs arg %d: ", i);
956 if (a[i] == (unsigned long)-1)
957 printf("void\n");
958 else if (a[i] == 1 && !i)
959 printf("X8 pointer\n");
960 else if (a[i] < 16)
961 printf("X%lu%s\n", a[i] / 2, a[i] & 1 ? " pointer" : "");
962 else if (a[i] < 32)
963 printf("V%lu\n", a[i] / 2 - 8);
964 else
965 printf("stack %lu%s\n",
966 (a[i] - 32) & ~1, a[i] & 1 ? " pointer" : "");
970 return stack;
973 ST_FUNC void gfunc_call(int nb_args)
975 CType *return_type;
976 CType **t;
977 unsigned long *a, *a1;
978 unsigned long stack;
979 int i;
981 #ifdef CONFIG_TCC_BCHECK
982 if (tcc_state->do_bounds_check)
983 gbound_args(nb_args);
984 #endif
986 return_type = &vtop[-nb_args].type.ref->type;
987 if ((return_type->t & VT_BTYPE) == VT_STRUCT)
988 --nb_args;
990 t = tcc_malloc((nb_args + 1) * sizeof(*t));
991 a = tcc_malloc((nb_args + 1) * sizeof(*a));
992 a1 = tcc_malloc((nb_args + 1) * sizeof(*a1));
994 t[0] = return_type;
995 for (i = 0; i < nb_args; i++)
996 t[nb_args - i] = &vtop[-i].type;
998 stack = arm64_pcs(nb_args, t, a);
1000 // Allocate space for structs replaced by pointer:
1001 for (i = nb_args; i; i--)
1002 if (a[i] & 1) {
1003 SValue *arg = &vtop[i - nb_args];
1004 int align, size = type_size(&arg->type, &align);
1005 assert((arg->type.t & VT_BTYPE) == VT_STRUCT);
1006 stack = (stack + align - 1) & -align;
1007 a1[i] = stack;
1008 stack += size;
1011 stack = (stack + 15) >> 4 << 4;
1013 /* fetch cpu flag before generating any code */
1014 if ((vtop->r & VT_VALMASK) == VT_CMP)
1015 gv(RC_INT);
1017 if (stack >= 0x1000000) // 16Mb
1018 tcc_error("stack size too big %lu", stack);
1019 if (stack & 0xfff)
1020 o(0xd10003ff | (stack & 0xfff) << 10); // sub sp,sp,#(n)
1021 if (stack >> 12)
1022 o(0xd14003ff | (stack >> 12) << 10);
1024 // First pass: set all values on stack
1025 for (i = nb_args; i; i--) {
1026 vpushv(vtop - nb_args + i);
1028 if (a[i] & 1) {
1029 // struct replaced by pointer
1030 int r = get_reg(RC_INT);
1031 arm64_spoff(intr(r), a1[i]);
1032 vset(&vtop->type, r | VT_LVAL, 0);
1033 vswap();
1034 vstore();
1035 if (a[i] >= 32) {
1036 // pointer on stack
1037 r = get_reg(RC_INT);
1038 arm64_spoff(intr(r), a1[i]);
1039 arm64_strx(3, intr(r), 31, (a[i] - 32) >> 1 << 1);
1042 else if (a[i] >= 32) {
1043 // value on stack
1044 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
1045 int r = get_reg(RC_INT);
1046 arm64_spoff(intr(r), a[i] - 32);
1047 vset(&vtop->type, r | VT_LVAL, 0);
1048 vswap();
1049 vstore();
1051 else if (is_float(vtop->type.t)) {
1052 gv(RC_FLOAT);
1053 arm64_strv(arm64_type_size(vtop[0].type.t),
1054 fltr(vtop[0].r), 31, a[i] - 32);
1056 else {
1057 gv(RC_INT);
1058 arm64_strx(arm64_type_size(vtop[0].type.t),
1059 intr(vtop[0].r), 31, a[i] - 32);
1063 --vtop;
1066 // Second pass: assign values to registers
1067 for (i = nb_args; i; i--, vtop--) {
1068 if (a[i] < 16 && !(a[i] & 1)) {
1069 // value in general-purpose registers
1070 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
1071 int align, size = type_size(&vtop->type, &align);
1072 if (size) {
1073 vtop->type.t = VT_PTR;
1074 gaddrof();
1075 gv(RC_R(a[i] / 2));
1076 arm64_ldrs(a[i] / 2, size);
1079 else
1080 gv(RC_R(a[i] / 2));
1082 else if (a[i] < 16)
1083 // struct replaced by pointer in register
1084 arm64_spoff(a[i] / 2, a1[i]);
1085 else if (a[i] < 32) {
1086 // value in floating-point registers
1087 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
1088 uint32_t j, sz, n = arm64_hfa(&vtop->type, &sz);
1089 vtop->type.t = VT_PTR;
1090 gaddrof();
1091 gv(RC_R30);
1092 for (j = 0; j < n; j++)
1093 o(0x3d4003c0 |
1094 (sz & 16) << 19 | -(sz & 8) << 27 | (sz & 4) << 29 |
1095 (a[i] / 2 - 8 + j) |
1096 j << 10); // ldr ([sdq])(*),[x30,#(j * sz)]
1098 else
1099 gv(RC_F(a[i] / 2 - 8));
1103 if ((return_type->t & VT_BTYPE) == VT_STRUCT) {
1104 if (a[0] == 1) {
1105 // indirect return: set x8 and discard the stack value
1106 gv(RC_R(8));
1107 --vtop;
1109 else
1110 // return in registers: keep the address for after the call
1111 vswap();
1114 save_regs(0);
1115 arm64_gen_bl_or_b(0);
1116 --vtop;
1117 if (stack & 0xfff)
1118 o(0x910003ff | (stack & 0xfff) << 10); // add sp,sp,#(n)
1119 if (stack >> 12)
1120 o(0x914003ff | (stack >> 12) << 10);
1123 int rt = return_type->t;
1124 int bt = rt & VT_BTYPE;
1125 if (bt == VT_STRUCT && !(a[0] & 1)) {
1126 // A struct was returned in registers, so write it out:
1127 gv(RC_R(8));
1128 --vtop;
1129 if (a[0] == 0) {
1130 int align, size = type_size(return_type, &align);
1131 assert(size <= 16);
1132 if (size > 8)
1133 o(0xa9000500); // stp x0,x1,[x8]
1134 else if (size)
1135 arm64_strx(size > 4 ? 3 : size > 2 ? 2 : size > 1, 0, 8, 0);
1138 else if (a[0] == 16) {
1139 uint32_t j, sz, n = arm64_hfa(return_type, &sz);
1140 for (j = 0; j < n; j++)
1141 o(0x3d000100 |
1142 (sz & 16) << 19 | -(sz & 8) << 27 | (sz & 4) << 29 |
1143 (a[i] / 2 - 8 + j) |
1144 j << 10); // str ([sdq])(*),[x8,#(j * sz)]
1149 tcc_free(a1);
1150 tcc_free(a);
1151 tcc_free(t);
1154 static unsigned long arm64_func_va_list_stack;
1155 static int arm64_func_va_list_gr_offs;
1156 static int arm64_func_va_list_vr_offs;
1157 static int arm64_func_sub_sp_offset;
1159 ST_FUNC void gfunc_prolog(Sym *func_sym)
1161 CType *func_type = &func_sym->type;
1162 int n = 0;
1163 int i = 0;
1164 Sym *sym;
1165 CType **t;
1166 unsigned long *a;
1167 int use_x8 = 0;
1168 int last_int = 0;
1169 int last_float = 0;
1171 func_vc = 144; // offset of where x8 is stored
1173 for (sym = func_type->ref; sym; sym = sym->next)
1174 ++n;
1175 t = n ? tcc_malloc(n * sizeof(*t)) : NULL;
1176 a = n ? tcc_malloc(n * sizeof(*a)) : NULL;
1178 for (sym = func_type->ref; sym; sym = sym->next)
1179 t[i++] = &sym->type;
1181 arm64_func_va_list_stack = arm64_pcs(n - 1, t, a);
1183 if (func_sym->type.ref->f.func_type == FUNC_ELLIPSIS) {
1184 use_x8 = 1;
1185 last_int = 4;
1186 last_float = 4;
1188 if (a && a[0] == 1)
1189 use_x8 = 1;
1190 for (i = 1, sym = func_type->ref->next; sym; i++, sym = sym->next) {
1191 if (a[i] == 1)
1192 use_x8 = 1;
1193 if (a[i] < 16) {
1194 int last, align, size = type_size(&sym->type, &align);
1195 last = a[i] / 4 + 1 + (size - 1) / 8;
1196 last_int = last > last_int ? last : last_int;
1198 else if (a[i] < 32) {
1199 int last, hfa = arm64_hfa(&sym->type, 0);
1200 last = a[i] / 4 - 3 + (hfa ? hfa - 1 : 0);
1201 last_float = last > last_float ? last : last_float;
1205 last_int = last_int > 4 ? 4 : last_int;
1206 last_float = last_float > 4 ? 4 : last_float;
1208 o(0xa9b27bfd); // stp x29,x30,[sp,#-224]!
1209 for (i = 0; i < last_float; i++)
1210 // stp q0,q1,[sp,#16], stp q2,q3,[sp,#48]
1211 // stp q4,q5,[sp,#80], stp q6,q7,[sp,#112]
1212 o(0xad0087e0 + i * 0x10000 + (i << 11) + (i << 1));
1213 if (use_x8)
1214 o(0xa90923e8); // stp x8,x8,[sp,#144]
1215 for (i = 0; i < last_int; i++)
1216 // stp x0,x1,[sp,#160], stp x2,x3,[sp,#176]
1217 // stp x4,x5,[sp,#192], stp x6,x7,[sp,#208]
1218 o(0xa90a07e0 + i * 0x10000 + (i << 11) + (i << 1));
1220 arm64_func_va_list_gr_offs = -64;
1221 arm64_func_va_list_vr_offs = -128;
1223 for (i = 1, sym = func_type->ref->next; sym; i++, sym = sym->next) {
1224 int off = (a[i] < 16 ? 160 + a[i] / 2 * 8 :
1225 a[i] < 32 ? 16 + (a[i] - 16) / 2 * 16 :
1226 224 + ((a[i] - 32) >> 1 << 1));
1227 sym_push(sym->v & ~SYM_FIELD, &sym->type,
1228 (a[i] & 1 ? VT_LLOCAL : VT_LOCAL) | VT_LVAL,
1229 off);
1231 if (a[i] < 16) {
1232 int align, size = type_size(&sym->type, &align);
1233 arm64_func_va_list_gr_offs = (a[i] / 2 - 7 +
1234 (!(a[i] & 1) && size > 8)) * 8;
1236 else if (a[i] < 32) {
1237 uint32_t hfa = arm64_hfa(&sym->type, 0);
1238 arm64_func_va_list_vr_offs = (a[i] / 2 - 16 +
1239 (hfa ? hfa : 1)) * 16;
1242 // HFAs of float and double need to be written differently:
1243 if (16 <= a[i] && a[i] < 32 && (sym->type.t & VT_BTYPE) == VT_STRUCT) {
1244 uint32_t j, sz, k = arm64_hfa(&sym->type, &sz);
1245 if (sz < 16)
1246 for (j = 0; j < k; j++) {
1247 o(0x3d0003e0 | -(sz & 8) << 27 | (sz & 4) << 29 |
1248 ((a[i] - 16) / 2 + j) | (off / sz + j) << 10);
1249 // str ([sdq])(*),[sp,#(j * sz)]
1254 tcc_free(a);
1255 tcc_free(t);
1257 o(0x910003fd); // mov x29,sp
1258 arm64_func_sub_sp_offset = ind;
1259 // In gfunc_epilog these will be replaced with code to decrement SP:
1260 o(0xd503201f); // nop
1261 o(0xd503201f); // nop
1262 loc = 0;
1263 #ifdef CONFIG_TCC_BCHECK
1264 if (tcc_state->do_bounds_check)
1265 gen_bounds_prolog();
1266 #endif
1269 ST_FUNC void gen_va_start(void)
1271 int r;
1272 --vtop; // we don't need the "arg"
1273 gaddrof();
1274 r = intr(gv(RC_INT));
1276 if (arm64_func_va_list_stack) {
1277 //xx could use add (immediate) here
1278 arm64_movimm(30, arm64_func_va_list_stack + 224);
1279 o(0x8b1e03be); // add x30,x29,x30
1281 else
1282 o(0x910383be); // add x30,x29,#224
1283 o(0xf900001e | r << 5); // str x30,[x(r)]
1285 if (arm64_func_va_list_gr_offs) {
1286 if (arm64_func_va_list_stack)
1287 o(0x910383be); // add x30,x29,#224
1288 o(0xf900041e | r << 5); // str x30,[x(r),#8]
1291 if (arm64_func_va_list_vr_offs) {
1292 o(0x910243be); // add x30,x29,#144
1293 o(0xf900081e | r << 5); // str x30,[x(r),#16]
1296 arm64_movimm(30, arm64_func_va_list_gr_offs);
1297 o(0xb900181e | r << 5); // str w30,[x(r),#24]
1299 arm64_movimm(30, arm64_func_va_list_vr_offs);
1300 o(0xb9001c1e | r << 5); // str w30,[x(r),#28]
1302 --vtop;
1305 ST_FUNC void gen_va_arg(CType *t)
1307 int align, size = type_size(t, &align);
1308 unsigned fsize, hfa = arm64_hfa(t, &fsize);
1309 uint32_t r0, r1;
1311 if (is_float(t->t)) {
1312 hfa = 1;
1313 fsize = size;
1316 gaddrof();
1317 r0 = intr(gv(RC_INT));
1318 r1 = get_reg(RC_INT);
1319 vtop[0].r = r1 | VT_LVAL;
1320 r1 = intr(r1);
1322 if (!hfa) {
1323 uint32_t n = size > 16 ? 8 : (size + 7) & -8;
1324 o(0xb940181e | r0 << 5); // ldr w30,[x(r0),#24] // __gr_offs
1325 if (align == 16) {
1326 assert(0); // this path untested but needed for __uint128_t
1327 o(0x11003fde); // add w30,w30,#15
1328 o(0x121c6fde); // and w30,w30,#-16
1330 o(0x310003c0 | r1 | n << 10); // adds w(r1),w30,#(n)
1331 o(0x540000ad); // b.le .+20
1332 o(0xf9400000 | r1 | r0 << 5); // ldr x(r1),[x(r0)] // __stack
1333 o(0x9100001e | r1 << 5 | n << 10); // add x30,x(r1),#(n)
1334 o(0xf900001e | r0 << 5); // str x30,[x(r0)] // __stack
1335 o(0x14000004); // b .+16
1336 o(0xb9001800 | r1 | r0 << 5); // str w(r1),[x(r0),#24] // __gr_offs
1337 o(0xf9400400 | r1 | r0 << 5); // ldr x(r1),[x(r0),#8] // __gr_top
1338 o(0x8b3ec000 | r1 | r1 << 5); // add x(r1),x(r1),w30,sxtw
1339 if (size > 16)
1340 o(0xf9400000 | r1 | r1 << 5); // ldr x(r1),[x(r1)]
1342 else {
1343 uint32_t rsz = hfa << 4;
1344 uint32_t ssz = (size + 7) & -(uint32_t)8;
1345 uint32_t b1, b2;
1346 o(0xb9401c1e | r0 << 5); // ldr w30,[x(r0),#28] // __vr_offs
1347 o(0x310003c0 | r1 | rsz << 10); // adds w(r1),w30,#(rsz)
1348 b1 = ind; o(0x5400000d); // b.le lab1
1349 o(0xf9400000 | r1 | r0 << 5); // ldr x(r1),[x(r0)] // __stack
1350 if (fsize == 16) {
1351 o(0x91003c00 | r1 | r1 << 5); // add x(r1),x(r1),#15
1352 o(0x927cec00 | r1 | r1 << 5); // and x(r1),x(r1),#-16
1354 o(0x9100001e | r1 << 5 | ssz << 10); // add x30,x(r1),#(ssz)
1355 o(0xf900001e | r0 << 5); // str x30,[x(r0)] // __stack
1356 b2 = ind; o(0x14000000); // b lab2
1357 // lab1:
1358 write32le(cur_text_section->data + b1, 0x5400000d | (ind - b1) << 3);
1359 o(0xb9001c00 | r1 | r0 << 5); // str w(r1),[x(r0),#28] // __vr_offs
1360 o(0xf9400800 | r1 | r0 << 5); // ldr x(r1),[x(r0),#16] // __vr_top
1361 if (hfa == 1 || fsize == 16)
1362 o(0x8b3ec000 | r1 | r1 << 5); // add x(r1),x(r1),w30,sxtw
1363 else {
1364 // We need to change the layout of this HFA.
1365 // Get some space on the stack using global variable "loc":
1366 loc = (loc - size) & -(uint32_t)align;
1367 o(0x8b3ec000 | 30 | r1 << 5); // add x30,x(r1),w30,sxtw
1368 arm64_movimm(r1, loc);
1369 o(0x8b0003a0 | r1 | r1 << 16); // add x(r1),x29,x(r1)
1370 o(0x4c402bdc | (uint32_t)fsize << 7 |
1371 (uint32_t)(hfa == 2) << 15 |
1372 (uint32_t)(hfa == 3) << 14); // ld1 {v28.(4s|2d),...},[x30]
1373 o(0x0d00801c | r1 << 5 | (fsize == 8) << 10 |
1374 (uint32_t)(hfa != 2) << 13 |
1375 (uint32_t)(hfa != 3) << 21); // st(hfa) {v28.(s|d),...}[0],[x(r1)]
1377 // lab2:
1378 write32le(cur_text_section->data + b2, 0x14000000 | (ind - b2) >> 2);
1382 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret,
1383 int *align, int *regsize)
1385 return 0;
1388 ST_FUNC void gfunc_return(CType *func_type)
1390 CType *t = func_type;
1391 unsigned long a;
1393 arm64_pcs(0, &t, &a);
1394 switch (a) {
1395 case -1:
1396 break;
1397 case 0:
1398 if ((func_type->t & VT_BTYPE) == VT_STRUCT) {
1399 int align, size = type_size(func_type, &align);
1400 gaddrof();
1401 gv(RC_R(0));
1402 arm64_ldrs(0, size);
1404 else
1405 gv(RC_IRET);
1406 break;
1407 case 1: {
1408 CType type = *func_type;
1409 mk_pointer(&type);
1410 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
1411 indir();
1412 vswap();
1413 vstore();
1414 break;
1416 case 16:
1417 if ((func_type->t & VT_BTYPE) == VT_STRUCT) {
1418 uint32_t j, sz, n = arm64_hfa(&vtop->type, &sz);
1419 gaddrof();
1420 gv(RC_R(0));
1421 for (j = 0; j < n; j++)
1422 o(0x3d400000 |
1423 (sz & 16) << 19 | -(sz & 8) << 27 | (sz & 4) << 29 |
1424 j | j << 10); // ldr ([sdq])(*),[x0,#(j * sz)]
1426 else
1427 gv(RC_FRET);
1428 break;
1429 default:
1430 assert(0);
1432 vtop--;
1435 ST_FUNC void gfunc_epilog(void)
1437 #ifdef CONFIG_TCC_BCHECK
1438 if (tcc_state->do_bounds_check)
1439 gen_bounds_epilog();
1440 #endif
1442 if (loc) {
1443 // Insert instructions to subtract size of stack frame from SP.
1444 unsigned char *ptr = cur_text_section->data + arm64_func_sub_sp_offset;
1445 uint64_t diff = (-loc + 15) & ~15;
1446 if (!(diff >> 24)) {
1447 if (diff & 0xfff) // sub sp,sp,#(diff & 0xfff)
1448 write32le(ptr, 0xd10003ff | (diff & 0xfff) << 10);
1449 if (diff >> 12) // sub sp,sp,#(diff >> 12),lsl #12
1450 write32le(ptr + 4, 0xd14003ff | (diff >> 12) << 10);
1452 else {
1453 // In this case we may subtract more than necessary,
1454 // but always less than 17/16 of what we were aiming for.
1455 int i = 0;
1456 int j = 0;
1457 while (diff >> 20) {
1458 diff = (diff + 0xffff) >> 16;
1459 ++i;
1461 while (diff >> 16) {
1462 diff = (diff + 1) >> 1;
1463 ++j;
1465 write32le(ptr, 0xd2800010 | diff << 5 | i << 21);
1466 // mov x16,#(diff),lsl #(16 * i)
1467 write32le(ptr + 4, 0xcb3063ff | j << 10);
1468 // sub sp,sp,x16,lsl #(j)
1471 o(0x910003bf); // mov sp,x29
1472 o(0xa8ce7bfd); // ldp x29,x30,[sp],#224
1474 o(0xd65f03c0); // ret
1477 ST_FUNC void gen_fill_nops(int bytes)
1479 if ((bytes & 3))
1480 tcc_error("alignment of code section not multiple of 4");
1481 while (bytes > 0) {
1482 o(0xd503201f); // nop
1483 bytes -= 4;
1487 // Generate forward branch to label:
1488 ST_FUNC int gjmp(int t)
1490 int r = ind;
1491 if (nocode_wanted)
1492 return t;
1493 o(t);
1494 return r;
1497 // Generate branch to known address:
1498 ST_FUNC void gjmp_addr(int a)
1500 assert(a - ind + 0x8000000 < 0x10000000);
1501 o(0x14000000 | ((a - ind) >> 2 & 0x3ffffff));
1504 ST_FUNC int gjmp_append(int n, int t)
1506 void *p;
1507 /* insert vtop->c jump list in t */
1508 if (n) {
1509 uint32_t n1 = n, n2;
1510 while ((n2 = read32le(p = cur_text_section->data + n1)))
1511 n1 = n2;
1512 write32le(p, t);
1513 t = n;
1515 return t;
1518 void arm64_vset_VT_CMP(int op)
1520 if (op >= TOK_ULT && op <= TOK_GT) {
1521 vtop->cmp_r = vtop->r;
1522 vset_VT_CMP(0x80);
1526 static void arm64_gen_opil(int op, uint32_t l);
1528 static void arm64_load_cmp(int r, SValue *sv)
1530 sv->r = sv->cmp_r;
1531 if (sv->c.i & 1) {
1532 vpushi(1);
1533 arm64_gen_opil('^', 0);
1535 if (r != sv->r) {
1536 load(r, sv);
1537 sv->r = r;
1541 ST_FUNC int gjmp_cond(int op, int t)
1543 int bt = vtop->type.t & VT_BTYPE;
1545 int inv = op & 1;
1546 vtop->r = vtop->cmp_r;
1548 if (bt == VT_LDOUBLE) {
1549 uint32_t a, b, f = fltr(gv(RC_FLOAT));
1550 a = get_reg(RC_INT);
1551 vpushi(0);
1552 vtop[0].r = a;
1553 b = get_reg(RC_INT);
1554 a = intr(a);
1555 b = intr(b);
1556 o(0x4e083c00 | a | f << 5); // mov x(a),v(f).d[0]
1557 o(0x4e183c00 | b | f << 5); // mov x(b),v(f).d[1]
1558 o(0xaa000400 | a | a << 5 | b << 16); // orr x(a),x(a),x(b),lsl #1
1559 o(0xb4000040 | a | !!inv << 24); // cbz/cbnz x(a),.+8
1560 --vtop;
1562 else if (bt == VT_FLOAT || bt == VT_DOUBLE) {
1563 uint32_t a = fltr(gv(RC_FLOAT));
1564 o(0x1e202008 | a << 5 | (bt != VT_FLOAT) << 22); // fcmp
1565 o(0x54000040 | !!inv); // b.eq/b.ne .+8
1567 else {
1568 uint32_t ll = (bt == VT_PTR || bt == VT_LLONG);
1569 uint32_t a = intr(gv(RC_INT));
1570 o(0x34000040 | a | !!inv << 24 | ll << 31); // cbz/cbnz wA,.+8
1572 return gjmp(t);
1575 static int arm64_iconst(uint64_t *val, SValue *sv)
1577 if ((sv->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
1578 return 0;
1579 if (val) {
1580 int t = sv->type.t;
1581 int bt = t & VT_BTYPE;
1582 *val = ((bt == VT_LLONG || bt == VT_PTR) ? sv->c.i :
1583 (uint32_t)sv->c.i |
1584 (t & VT_UNSIGNED ? 0 : -(sv->c.i & 0x80000000)));
1586 return 1;
1589 static int arm64_gen_opic(int op, uint32_t l, int rev, uint64_t val,
1590 uint32_t x, uint32_t a)
1592 if (op == '-' && !rev) {
1593 val = -val;
1594 op = '+';
1596 val = l ? val : (uint32_t)val;
1598 switch (op) {
1600 case '+': {
1601 uint32_t s = l ? val >> 63 : val >> 31;
1602 val = s ? -val : val;
1603 val = l ? val : (uint32_t)val;
1604 if (!(val & ~(uint64_t)0xfff))
1605 o(0x11000000 | l << 31 | s << 30 | x | a << 5 | val << 10);
1606 else if (!(val & ~(uint64_t)0xfff000))
1607 o(0x11400000 | l << 31 | s << 30 | x | a << 5 | val >> 12 << 10);
1608 else {
1609 arm64_movimm(30, val); // use x30
1610 o(0x0b1e0000 | l << 31 | s << 30 | x | a << 5);
1612 return 1;
1615 case '-':
1616 if (!val)
1617 o(0x4b0003e0 | l << 31 | x | a << 16); // neg
1618 else if (val == (l ? (uint64_t)-1 : (uint32_t)-1))
1619 o(0x2a2003e0 | l << 31 | x | a << 16); // mvn
1620 else {
1621 arm64_movimm(30, val); // use x30
1622 o(0x4b0003c0 | l << 31 | x | a << 16); // sub
1624 return 1;
1626 case '^':
1627 if (val == -1 || (val == 0xffffffff && !l)) {
1628 o(0x2a2003e0 | l << 31 | x | a << 16); // mvn
1629 return 1;
1631 // fall through
1632 case '&':
1633 case '|': {
1634 int e = arm64_encode_bimm64(l ? val : val | val << 32);
1635 if (e < 0)
1636 return 0;
1637 o((op == '&' ? 0x12000000 :
1638 op == '|' ? 0x32000000 : 0x52000000) |
1639 l << 31 | x | a << 5 | (uint32_t)e << 10);
1640 return 1;
1643 case TOK_SAR:
1644 case TOK_SHL:
1645 case TOK_SHR: {
1646 uint32_t n = 32 << l;
1647 val = val & (n - 1);
1648 if (rev)
1649 return 0;
1650 if (!val) {
1651 // tcc_warning("shift count >= width of type");
1652 o(0x2a0003e0 | l << 31 | a << 16);
1653 return 1;
1655 else if (op == TOK_SHL)
1656 o(0x53000000 | l << 31 | l << 22 | x | a << 5 |
1657 (n - val) << 16 | (n - 1 - val) << 10); // lsl
1658 else
1659 o(0x13000000 | (op == TOK_SHR) << 30 | l << 31 | l << 22 |
1660 x | a << 5 | val << 16 | (n - 1) << 10); // lsr/asr
1661 return 1;
1665 return 0;
1668 static void arm64_gen_opil(int op, uint32_t l)
1670 uint32_t x, a, b;
1672 // Special treatment for operations with a constant operand:
1674 uint64_t val;
1675 int rev = 1;
1677 if (arm64_iconst(0, &vtop[0])) {
1678 vswap();
1679 rev = 0;
1681 if (arm64_iconst(&val, &vtop[-1])) {
1682 gv(RC_INT);
1683 a = intr(vtop[0].r);
1684 --vtop;
1685 x = get_reg(RC_INT);
1686 ++vtop;
1687 if (arm64_gen_opic(op, l, rev, val, intr(x), a)) {
1688 vtop[0].r = x;
1689 vswap();
1690 --vtop;
1691 return;
1694 if (!rev)
1695 vswap();
1698 gv2(RC_INT, RC_INT);
1699 assert(vtop[-1].r < VT_CONST && vtop[0].r < VT_CONST);
1700 a = intr(vtop[-1].r);
1701 b = intr(vtop[0].r);
1702 vtop -= 2;
1703 x = get_reg(RC_INT);
1704 ++vtop;
1705 vtop[0].r = x;
1706 x = intr(x);
1708 switch (op) {
1709 case '%':
1710 // Use x30 for quotient:
1711 o(0x1ac00c00 | l << 31 | 30 | a << 5 | b << 16); // sdiv
1712 o(0x1b008000 | l << 31 | x | (uint32_t)30 << 5 |
1713 b << 16 | a << 10); // msub
1714 break;
1715 case '&':
1716 o(0x0a000000 | l << 31 | x | a << 5 | b << 16); // and
1717 break;
1718 case '*':
1719 o(0x1b007c00 | l << 31 | x | a << 5 | b << 16); // mul
1720 break;
1721 case '+':
1722 o(0x0b000000 | l << 31 | x | a << 5 | b << 16); // add
1723 break;
1724 case '-':
1725 o(0x4b000000 | l << 31 | x | a << 5 | b << 16); // sub
1726 break;
1727 case '/':
1728 o(0x1ac00c00 | l << 31 | x | a << 5 | b << 16); // sdiv
1729 break;
1730 case '^':
1731 o(0x4a000000 | l << 31 | x | a << 5 | b << 16); // eor
1732 break;
1733 case '|':
1734 o(0x2a000000 | l << 31 | x | a << 5 | b << 16); // orr
1735 break;
1736 case TOK_EQ:
1737 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1738 o(0x1a9f17e0 | x); // cset wA,eq
1739 break;
1740 case TOK_GE:
1741 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1742 o(0x1a9fb7e0 | x); // cset wA,ge
1743 break;
1744 case TOK_GT:
1745 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1746 o(0x1a9fd7e0 | x); // cset wA,gt
1747 break;
1748 case TOK_LE:
1749 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1750 o(0x1a9fc7e0 | x); // cset wA,le
1751 break;
1752 case TOK_LT:
1753 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1754 o(0x1a9fa7e0 | x); // cset wA,lt
1755 break;
1756 case TOK_NE:
1757 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1758 o(0x1a9f07e0 | x); // cset wA,ne
1759 break;
1760 case TOK_SAR:
1761 o(0x1ac02800 | l << 31 | x | a << 5 | b << 16); // asr
1762 break;
1763 case TOK_SHL:
1764 o(0x1ac02000 | l << 31 | x | a << 5 | b << 16); // lsl
1765 break;
1766 case TOK_SHR:
1767 o(0x1ac02400 | l << 31 | x | a << 5 | b << 16); // lsr
1768 break;
1769 case TOK_UDIV:
1770 case TOK_PDIV:
1771 o(0x1ac00800 | l << 31 | x | a << 5 | b << 16); // udiv
1772 break;
1773 case TOK_UGE:
1774 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1775 o(0x1a9f37e0 | x); // cset wA,cs
1776 break;
1777 case TOK_UGT:
1778 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1779 o(0x1a9f97e0 | x); // cset wA,hi
1780 break;
1781 case TOK_ULT:
1782 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1783 o(0x1a9f27e0 | x); // cset wA,cc
1784 break;
1785 case TOK_ULE:
1786 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1787 o(0x1a9f87e0 | x); // cset wA,ls
1788 break;
1789 case TOK_UMOD:
1790 // Use x30 for quotient:
1791 o(0x1ac00800 | l << 31 | 30 | a << 5 | b << 16); // udiv
1792 o(0x1b008000 | l << 31 | x | (uint32_t)30 << 5 |
1793 b << 16 | a << 10); // msub
1794 break;
1795 default:
1796 assert(0);
1800 ST_FUNC void gen_opi(int op)
1802 arm64_gen_opil(op, 0);
1803 arm64_vset_VT_CMP(op);
1806 ST_FUNC void gen_opl(int op)
1808 arm64_gen_opil(op, 1);
1809 arm64_vset_VT_CMP(op);
1812 ST_FUNC void gen_opf(int op)
1814 uint32_t x, a, b, dbl;
1816 if (vtop[0].type.t == VT_LDOUBLE) {
1817 CType type = vtop[0].type;
1818 int func = 0;
1819 int cond = -1;
1820 switch (op) {
1821 case '*': func = TOK___multf3; break;
1822 case '+': func = TOK___addtf3; break;
1823 case '-': func = TOK___subtf3; break;
1824 case '/': func = TOK___divtf3; break;
1825 case TOK_EQ: func = TOK___eqtf2; cond = 1; break;
1826 case TOK_NE: func = TOK___netf2; cond = 0; break;
1827 case TOK_LT: func = TOK___lttf2; cond = 10; break;
1828 case TOK_GE: func = TOK___getf2; cond = 11; break;
1829 case TOK_LE: func = TOK___letf2; cond = 12; break;
1830 case TOK_GT: func = TOK___gttf2; cond = 13; break;
1831 default: assert(0); break;
1833 vpush_helper_func(func);
1834 vrott(3);
1835 gfunc_call(2);
1836 vpushi(0);
1837 vtop->r = cond < 0 ? REG_FRET : REG_IRET;
1838 if (cond < 0)
1839 vtop->type = type;
1840 else {
1841 o(0x7100001f); // cmp w0,#0
1842 o(0x1a9f07e0 | (uint32_t)cond << 12); // cset w0,(cond)
1844 return;
1847 dbl = vtop[0].type.t != VT_FLOAT;
1848 gv2(RC_FLOAT, RC_FLOAT);
1849 assert(vtop[-1].r < VT_CONST && vtop[0].r < VT_CONST);
1850 a = fltr(vtop[-1].r);
1851 b = fltr(vtop[0].r);
1852 vtop -= 2;
1853 switch (op) {
1854 case TOK_EQ: case TOK_NE:
1855 case TOK_LT: case TOK_GE: case TOK_LE: case TOK_GT:
1856 x = get_reg(RC_INT);
1857 ++vtop;
1858 vtop[0].r = x;
1859 x = intr(x);
1860 break;
1861 default:
1862 x = get_reg(RC_FLOAT);
1863 ++vtop;
1864 vtop[0].r = x;
1865 x = fltr(x);
1866 break;
1869 switch (op) {
1870 case '*':
1871 o(0x1e200800 | dbl << 22 | x | a << 5 | b << 16); // fmul
1872 break;
1873 case '+':
1874 o(0x1e202800 | dbl << 22 | x | a << 5 | b << 16); // fadd
1875 break;
1876 case '-':
1877 o(0x1e203800 | dbl << 22 | x | a << 5 | b << 16); // fsub
1878 break;
1879 case '/':
1880 o(0x1e201800 | dbl << 22 | x | a << 5 | b << 16); // fdiv
1881 break;
1882 case TOK_EQ:
1883 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1884 o(0x1a9f17e0 | x); // cset w(x),eq
1885 break;
1886 case TOK_GE:
1887 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1888 o(0x1a9fb7e0 | x); // cset w(x),ge
1889 break;
1890 case TOK_GT:
1891 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1892 o(0x1a9fd7e0 | x); // cset w(x),gt
1893 break;
1894 case TOK_LE:
1895 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1896 o(0x1a9f87e0 | x); // cset w(x),ls
1897 break;
1898 case TOK_LT:
1899 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1900 o(0x1a9f57e0 | x); // cset w(x),mi
1901 break;
1902 case TOK_NE:
1903 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1904 o(0x1a9f07e0 | x); // cset w(x),ne
1905 break;
1906 default:
1907 assert(0);
1909 arm64_vset_VT_CMP(op);
1912 // Generate sign extension from 32 to 64 bits:
1913 ST_FUNC void gen_cvt_sxtw(void)
1915 uint32_t r = intr(gv(RC_INT));
1916 o(0x93407c00 | r | r << 5); // sxtw x(r),w(r)
1919 /* char/short to int conversion */
1920 ST_FUNC void gen_cvt_csti(int t)
1922 int r = intr(gv(RC_INT));
1923 o(0x13001c00
1924 | ((t & VT_BTYPE) == VT_SHORT) << 13
1925 | (uint32_t)!!(t & VT_UNSIGNED) << 30
1926 | r | r << 5); // [su]xt[bh] w(r),w(r)
1929 ST_FUNC void gen_cvt_itof(int t)
1931 if (t == VT_LDOUBLE) {
1932 int f = vtop->type.t;
1933 int func = (f & VT_BTYPE) == VT_LLONG ?
1934 (f & VT_UNSIGNED ? TOK___floatunditf : TOK___floatditf) :
1935 (f & VT_UNSIGNED ? TOK___floatunsitf : TOK___floatsitf);
1936 vpush_helper_func(func);
1937 vrott(2);
1938 gfunc_call(1);
1939 vpushi(0);
1940 vtop->type.t = t;
1941 vtop->r = REG_FRET;
1942 return;
1944 else {
1945 int d, n = intr(gv(RC_INT));
1946 int s = !(vtop->type.t & VT_UNSIGNED);
1947 uint32_t l = ((vtop->type.t & VT_BTYPE) == VT_LLONG);
1948 --vtop;
1949 d = get_reg(RC_FLOAT);
1950 ++vtop;
1951 vtop[0].r = d;
1952 o(0x1e220000 | (uint32_t)!s << 16 |
1953 (uint32_t)(t != VT_FLOAT) << 22 | fltr(d) |
1954 l << 31 | n << 5); // [us]cvtf [sd](d),[wx](n)
1958 ST_FUNC void gen_cvt_ftoi(int t)
1960 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
1961 int func = (t & VT_BTYPE) == VT_LLONG ?
1962 (t & VT_UNSIGNED ? TOK___fixunstfdi : TOK___fixtfdi) :
1963 (t & VT_UNSIGNED ? TOK___fixunstfsi : TOK___fixtfsi);
1964 vpush_helper_func(func);
1965 vrott(2);
1966 gfunc_call(1);
1967 vpushi(0);
1968 vtop->type.t = t;
1969 vtop->r = REG_IRET;
1970 return;
1972 else {
1973 int d, n = fltr(gv(RC_FLOAT));
1974 uint32_t l = ((vtop->type.t & VT_BTYPE) != VT_FLOAT);
1975 --vtop;
1976 d = get_reg(RC_INT);
1977 ++vtop;
1978 vtop[0].r = d;
1979 o(0x1e380000 |
1980 (uint32_t)!!(t & VT_UNSIGNED) << 16 |
1981 (uint32_t)((t & VT_BTYPE) == VT_LLONG) << 31 | intr(d) |
1982 l << 22 | n << 5); // fcvtz[su] [wx](d),[sd](n)
1986 ST_FUNC void gen_cvt_ftof(int t)
1988 int f = vtop[0].type.t & VT_BTYPE;
1989 assert(t == VT_FLOAT || t == VT_DOUBLE || t == VT_LDOUBLE);
1990 assert(f == VT_FLOAT || f == VT_DOUBLE || f == VT_LDOUBLE);
1991 if (t == f)
1992 return;
1994 if (t == VT_LDOUBLE || f == VT_LDOUBLE) {
1995 int func = (t == VT_LDOUBLE) ?
1996 (f == VT_FLOAT ? TOK___extendsftf2 : TOK___extenddftf2) :
1997 (t == VT_FLOAT ? TOK___trunctfsf2 : TOK___trunctfdf2);
1998 vpush_helper_func(func);
1999 vrott(2);
2000 gfunc_call(1);
2001 vpushi(0);
2002 vtop->type.t = t;
2003 vtop->r = REG_FRET;
2005 else {
2006 int x, a;
2007 gv(RC_FLOAT);
2008 assert(vtop[0].r < VT_CONST);
2009 a = fltr(vtop[0].r);
2010 --vtop;
2011 x = get_reg(RC_FLOAT);
2012 ++vtop;
2013 vtop[0].r = x;
2014 x = fltr(x);
2016 if (f == VT_FLOAT)
2017 o(0x1e22c000 | x | a << 5); // fcvt d(x),s(a)
2018 else
2019 o(0x1e624000 | x | a << 5); // fcvt s(x),d(a)
2023 /* increment tcov counter */
2024 ST_FUNC void gen_increment_tcov (SValue *sv)
2026 int r1, r2;
2028 vpushv(sv);
2029 vtop->r = r1 = get_reg(RC_INT);
2030 r2 = get_reg(RC_INT);
2031 greloca(cur_text_section, sv->sym, ind, R_AARCH64_ADR_GOT_PAGE, 0);
2032 o(0x90000000 | r1); // adrp r1, #sym
2033 greloca(cur_text_section, sv->sym, ind, R_AARCH64_LD64_GOT_LO12_NC, 0);
2034 o(0xf9400000 | r1 | (r1 << 5)); // ld xr,[xr, #sym]
2035 o(0xf9400000 | (intr(r1)<<5) | intr(r2)); // ldr r2, [r1]
2036 o(0x91000400 | (intr(r2)<<5) | intr(r2)); // add r2, r2, #1
2037 o(0xf9000000 | (intr(r1)<<5) | intr(r2)); // str r2, [r1]
2038 vpop();
2041 ST_FUNC void ggoto(void)
2043 arm64_gen_bl_or_b(1);
2044 --vtop;
2047 ST_FUNC void gen_clear_cache(void)
2049 uint32_t beg, end, dsz, isz, p, lab1, b1;
2050 gv2(RC_INT, RC_INT);
2051 vpushi(0);
2052 vtop->r = get_reg(RC_INT);
2053 vpushi(0);
2054 vtop->r = get_reg(RC_INT);
2055 vpushi(0);
2056 vtop->r = get_reg(RC_INT);
2057 beg = intr(vtop[-4].r); // x0
2058 end = intr(vtop[-3].r); // x1
2059 dsz = intr(vtop[-2].r); // x2
2060 isz = intr(vtop[-1].r); // x3
2061 p = intr(vtop[0].r); // x4
2062 vtop -= 5;
2064 o(0xd53b0020 | isz); // mrs x(isz),ctr_el0
2065 o(0x52800080 | p); // mov w(p),#4
2066 o(0x53104c00 | dsz | isz << 5); // ubfx w(dsz),w(isz),#16,#4
2067 o(0x1ac02000 | dsz | p << 5 | dsz << 16); // lsl w(dsz),w(p),w(dsz)
2068 o(0x12000c00 | isz | isz << 5); // and w(isz),w(isz),#15
2069 o(0x1ac02000 | isz | p << 5 | isz << 16); // lsl w(isz),w(p),w(isz)
2070 o(0x51000400 | p | dsz << 5); // sub w(p),w(dsz),#1
2071 o(0x8a240004 | p | beg << 5 | p << 16); // bic x(p),x(beg),x(p)
2072 b1 = ind; o(0x14000000); // b
2073 lab1 = ind;
2074 o(0xd50b7b20 | p); // dc cvau,x(p)
2075 o(0x8b000000 | p | p << 5 | dsz << 16); // add x(p),x(p),x(dsz)
2076 write32le(cur_text_section->data + b1, 0x14000000 | (ind - b1) >> 2);
2077 o(0xeb00001f | p << 5 | end << 16); // cmp x(p),x(end)
2078 o(0x54ffffa3 | ((lab1 - ind) << 3 & 0xffffe0)); // b.cc lab1
2079 o(0xd5033b9f); // dsb ish
2080 o(0x51000400 | p | isz << 5); // sub w(p),w(isz),#1
2081 o(0x8a240004 | p | beg << 5 | p << 16); // bic x(p),x(beg),x(p)
2082 b1 = ind; o(0x14000000); // b
2083 lab1 = ind;
2084 o(0xd50b7520 | p); // ic ivau,x(p)
2085 o(0x8b000000 | p | p << 5 | isz << 16); // add x(p),x(p),x(isz)
2086 write32le(cur_text_section->data + b1, 0x14000000 | (ind - b1) >> 2);
2087 o(0xeb00001f | p << 5 | end << 16); // cmp x(p),x(end)
2088 o(0x54ffffa3 | ((lab1 - ind) << 3 & 0xffffe0)); // b.cc lab1
2089 o(0xd5033b9f); // dsb ish
2090 o(0xd5033fdf); // isb
2093 ST_FUNC void gen_vla_sp_save(int addr) {
2094 uint32_t r = intr(get_reg(RC_INT));
2095 o(0x910003e0 | r); // mov x(r),sp
2096 arm64_strx(3, r, 29, addr);
2099 ST_FUNC void gen_vla_sp_restore(int addr) {
2100 // Use x30 because this function can be called when there
2101 // is a live return value in x0 but there is nothing on
2102 // the value stack to prevent get_reg from returning x0.
2103 uint32_t r = 30;
2104 arm64_ldrx(0, 3, r, 29, addr);
2105 o(0x9100001f | r << 5); // mov sp,x(r)
2108 ST_FUNC void gen_vla_alloc(CType *type, int align) {
2109 uint32_t r;
2110 #if defined(CONFIG_TCC_BCHECK)
2111 if (tcc_state->do_bounds_check)
2112 vpushv(vtop);
2113 #endif
2114 r = intr(gv(RC_INT));
2115 #if defined(CONFIG_TCC_BCHECK)
2116 if (tcc_state->do_bounds_check)
2117 o(0x91004000 | r | r << 5); // add x(r),x(r),#15+1
2118 else
2119 #endif
2120 o(0x91003c00 | r | r << 5); // add x(r),x(r),#15
2121 o(0x927cec00 | r | r << 5); // bic x(r),x(r),#15
2122 o(0xcb2063ff | r << 16); // sub sp,sp,x(r)
2123 vpop();
2124 #if defined(CONFIG_TCC_BCHECK)
2125 if (tcc_state->do_bounds_check) {
2126 vpushi(0);
2127 vtop->r = TREG_R(0);
2128 o(0x910003e0 | vtop->r); // mov r0,sp
2129 vswap();
2130 vpush_helper_func(TOK___bound_new_region);
2131 vrott(3);
2132 gfunc_call(2);
2133 func_bound_add_epilog = 1;
2135 #endif
2138 /* end of A64 code generator */
2139 /*************************************************************/
2140 #endif
2141 /*************************************************************/