Fix Extended Asm ignored constraints
[tinycc.git] / arm64-gen.c
blobe314de583ea6b061c20eac5115665bab3b4ce060
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 #ifndef TCC_TARGET_MACHO
42 #define CHAR_IS_UNSIGNED
43 #endif
45 /* define if return values need to be extended explicitely
46 at caller side (for interfacing with non-TCC compilers) */
47 #define PROMOTE_RET
48 /******************************************************/
49 #else /* ! TARGET_DEFS_ONLY */
50 /******************************************************/
51 #define USING_GLOBALS
52 #include "tcc.h"
53 #include <assert.h>
55 ST_DATA const char * const target_machine_defs =
56 "__aarch64__\0"
57 #if defined(TCC_TARGET_MACHO)
58 "__arm64__\0"
59 #endif
60 "__AARCH64EL__\0"
63 ST_DATA const int reg_classes[NB_REGS] = {
64 RC_INT | RC_R(0),
65 RC_INT | RC_R(1),
66 RC_INT | RC_R(2),
67 RC_INT | RC_R(3),
68 RC_INT | RC_R(4),
69 RC_INT | RC_R(5),
70 RC_INT | RC_R(6),
71 RC_INT | RC_R(7),
72 RC_INT | RC_R(8),
73 RC_INT | RC_R(9),
74 RC_INT | RC_R(10),
75 RC_INT | RC_R(11),
76 RC_INT | RC_R(12),
77 RC_INT | RC_R(13),
78 RC_INT | RC_R(14),
79 RC_INT | RC_R(15),
80 RC_INT | RC_R(16),
81 RC_INT | RC_R(17),
82 RC_INT | RC_R(18),
83 RC_R30, // not in RC_INT as we make special use of x30
84 RC_FLOAT | RC_F(0),
85 RC_FLOAT | RC_F(1),
86 RC_FLOAT | RC_F(2),
87 RC_FLOAT | RC_F(3),
88 RC_FLOAT | RC_F(4),
89 RC_FLOAT | RC_F(5),
90 RC_FLOAT | RC_F(6),
91 RC_FLOAT | RC_F(7)
94 #if defined(CONFIG_TCC_BCHECK)
95 static addr_t func_bound_offset;
96 static unsigned long func_bound_ind;
97 ST_DATA int func_bound_add_epilog;
98 #endif
100 #define IS_FREG(x) ((x) >= TREG_F(0))
102 static uint32_t intr(int r)
104 assert(TREG_R(0) <= r && r <= TREG_R30);
105 return r < TREG_R30 ? r : 30;
108 static uint32_t fltr(int r)
110 assert(TREG_F(0) <= r && r <= TREG_F(7));
111 return r - TREG_F(0);
114 // Add an instruction to text section:
115 ST_FUNC void o(unsigned int c)
117 int ind1 = ind + 4;
118 if (nocode_wanted)
119 return;
120 if (ind1 > cur_text_section->data_allocated)
121 section_realloc(cur_text_section, ind1);
122 write32le(cur_text_section->data + ind, c);
123 ind = ind1;
126 static int arm64_encode_bimm64(uint64_t x)
128 int neg = x & 1;
129 int rep, pos, len;
131 if (neg)
132 x = ~x;
133 if (!x)
134 return -1;
136 if (x >> 2 == (x & (((uint64_t)1 << (64 - 2)) - 1)))
137 rep = 2, x &= ((uint64_t)1 << 2) - 1;
138 else if (x >> 4 == (x & (((uint64_t)1 << (64 - 4)) - 1)))
139 rep = 4, x &= ((uint64_t)1 << 4) - 1;
140 else if (x >> 8 == (x & (((uint64_t)1 << (64 - 8)) - 1)))
141 rep = 8, x &= ((uint64_t)1 << 8) - 1;
142 else if (x >> 16 == (x & (((uint64_t)1 << (64 - 16)) - 1)))
143 rep = 16, x &= ((uint64_t)1 << 16) - 1;
144 else if (x >> 32 == (x & (((uint64_t)1 << (64 - 32)) - 1)))
145 rep = 32, x &= ((uint64_t)1 << 32) - 1;
146 else
147 rep = 64;
149 pos = 0;
150 if (!(x & (((uint64_t)1 << 32) - 1))) x >>= 32, pos += 32;
151 if (!(x & (((uint64_t)1 << 16) - 1))) x >>= 16, pos += 16;
152 if (!(x & (((uint64_t)1 << 8) - 1))) x >>= 8, pos += 8;
153 if (!(x & (((uint64_t)1 << 4) - 1))) x >>= 4, pos += 4;
154 if (!(x & (((uint64_t)1 << 2) - 1))) x >>= 2, pos += 2;
155 if (!(x & (((uint64_t)1 << 1) - 1))) x >>= 1, pos += 1;
157 len = 0;
158 if (!(~x & (((uint64_t)1 << 32) - 1))) x >>= 32, len += 32;
159 if (!(~x & (((uint64_t)1 << 16) - 1))) x >>= 16, len += 16;
160 if (!(~x & (((uint64_t)1 << 8) - 1))) x >>= 8, len += 8;
161 if (!(~x & (((uint64_t)1 << 4) - 1))) x >>= 4, len += 4;
162 if (!(~x & (((uint64_t)1 << 2) - 1))) x >>= 2, len += 2;
163 if (!(~x & (((uint64_t)1 << 1) - 1))) x >>= 1, len += 1;
165 if (x)
166 return -1;
167 if (neg) {
168 pos = (pos + len) & (rep - 1);
169 len = rep - len;
171 return ((0x1000 & rep << 6) | (((rep - 1) ^ 31) << 1 & 63) |
172 ((rep - pos) & (rep - 1)) << 6 | (len - 1));
175 static uint32_t arm64_movi(int r, uint64_t x)
177 uint64_t m = 0xffff;
178 int e;
179 if (!(x & ~m))
180 return 0x52800000 | r | x << 5; // movz w(r),#(x)
181 if (!(x & ~(m << 16)))
182 return 0x52a00000 | r | x >> 11; // movz w(r),#(x >> 16),lsl #16
183 if (!(x & ~(m << 32)))
184 return 0xd2c00000 | r | x >> 27; // movz x(r),#(x >> 32),lsl #32
185 if (!(x & ~(m << 48)))
186 return 0xd2e00000 | r | x >> 43; // movz x(r),#(x >> 48),lsl #48
187 if ((x & ~m) == m << 16)
188 return (0x12800000 | r |
189 (~x << 5 & 0x1fffe0)); // movn w(r),#(~x)
190 if ((x & ~(m << 16)) == m)
191 return (0x12a00000 | r |
192 (~x >> 11 & 0x1fffe0)); // movn w(r),#(~x >> 16),lsl #16
193 if (!~(x | m))
194 return (0x92800000 | r |
195 (~x << 5 & 0x1fffe0)); // movn x(r),#(~x)
196 if (!~(x | m << 16))
197 return (0x92a00000 | r |
198 (~x >> 11 & 0x1fffe0)); // movn x(r),#(~x >> 16),lsl #16
199 if (!~(x | m << 32))
200 return (0x92c00000 | r |
201 (~x >> 27 & 0x1fffe0)); // movn x(r),#(~x >> 32),lsl #32
202 if (!~(x | m << 48))
203 return (0x92e00000 | r |
204 (~x >> 43 & 0x1fffe0)); // movn x(r),#(~x >> 32),lsl #32
205 if (!(x >> 32) && (e = arm64_encode_bimm64(x | x << 32)) >= 0)
206 return 0x320003e0 | r | (uint32_t)e << 10; // movi w(r),#(x)
207 if ((e = arm64_encode_bimm64(x)) >= 0)
208 return 0xb20003e0 | r | (uint32_t)e << 10; // movi x(r),#(x)
209 return 0;
212 static void arm64_movimm(int r, uint64_t x)
214 uint32_t i;
215 if ((i = arm64_movi(r, x)))
216 o(i); // a single MOV
217 else {
218 // MOVZ/MOVN and 1-3 MOVKs
219 int z = 0, m = 0;
220 uint32_t mov1 = 0xd2800000; // movz
221 uint64_t x1 = x;
222 for (i = 0; i < 64; i += 16) {
223 z += !(x >> i & 0xffff);
224 m += !(~x >> i & 0xffff);
226 if (m > z) {
227 x1 = ~x;
228 mov1 = 0x92800000; // movn
230 for (i = 0; i < 64; i += 16)
231 if (x1 >> i & 0xffff) {
232 o(mov1 | r | (x1 >> i & 0xffff) << 5 | i << 17);
233 // movz/movn x(r),#(*),lsl #(i)
234 break;
236 for (i += 16; i < 64; i += 16)
237 if (x1 >> i & 0xffff)
238 o(0xf2800000 | r | (x >> i & 0xffff) << 5 | i << 17);
239 // movk x(r),#(*),lsl #(i)
243 // Patch all branches in list pointed to by t to branch to a:
244 ST_FUNC void gsym_addr(int t_, int a_)
246 uint32_t t = t_;
247 uint32_t a = a_;
248 while (t) {
249 unsigned char *ptr = cur_text_section->data + t;
250 uint32_t next = read32le(ptr);
251 if (a - t + 0x8000000 >= 0x10000000)
252 tcc_error("branch out of range");
253 write32le(ptr, (a - t == 4 ? 0xd503201f : // nop
254 0x14000000 | ((a - t) >> 2 & 0x3ffffff))); // b
255 t = next;
259 static int arm64_type_size(int t)
262 * case values are in increasing order (from 1 to 11).
263 * which 'may' help compiler optimizers. See tcc.h
265 switch (t & VT_BTYPE) {
266 case VT_BYTE: return 0;
267 case VT_SHORT: return 1;
268 case VT_INT: return 2;
269 case VT_LLONG: return 3;
270 case VT_PTR: return 3;
271 case VT_FUNC: return 3;
272 case VT_STRUCT: return 3;
273 case VT_FLOAT: return 2;
274 case VT_DOUBLE: return 3;
275 case VT_LDOUBLE: return 4;
276 case VT_BOOL: return 0;
278 assert(0);
279 return 0;
282 static void arm64_spoff(int reg, uint64_t off)
284 uint32_t sub = off >> 63;
285 if (sub)
286 off = -off;
287 if (off < 4096)
288 o(0x910003e0 | sub << 30 | reg | off << 10);
289 // (add|sub) x(reg),sp,#(off)
290 else {
291 arm64_movimm(30, off); // use x30 for offset
292 o(0x8b3e63e0 | sub << 30 | reg); // (add|sub) x(reg),sp,x30
296 /* invert 0: return value to use for store/load */
297 /* invert 1: return value to use for arm64_sym */
298 static uint64_t arm64_check_offset(int invert, int sz_, uint64_t off)
300 uint32_t sz = sz_;
301 if (!(off & ~((uint32_t)0xfff << sz)) ||
302 (off < 256 || -off <= 256))
303 return invert ? off : 0ul;
304 else if ((off & ((uint32_t)0xfff << sz)))
305 return invert ? off & ((uint32_t)0xfff << sz)
306 : off & ~((uint32_t)0xfff << sz);
307 else if (off & 0x1ff)
308 return invert ? off & 0x1ff : off & ~0x1ff;
309 else
310 return invert ? 0ul : off;
313 static void arm64_ldrx(int sg, int sz_, int dst, int bas, uint64_t off)
315 uint32_t sz = sz_;
316 if (sz >= 2)
317 sg = 0;
318 if (!(off & ~((uint32_t)0xfff << sz)))
319 o(0x39400000 | dst | bas << 5 | off << (10 - sz) |
320 (uint32_t)!!sg << 23 | sz << 30); // ldr(*) x(dst),[x(bas),#(off)]
321 else if (off < 256 || -off <= 256)
322 o(0x38400000 | dst | bas << 5 | (off & 511) << 12 |
323 (uint32_t)!!sg << 23 | sz << 30); // ldur(*) x(dst),[x(bas),#(off)]
324 else {
325 arm64_movimm(30, off); // use x30 for offset
326 o(0x38206800 | dst | bas << 5 | (uint32_t)30 << 16 |
327 (uint32_t)(!!sg + 1) << 22 | sz << 30); // ldr(*) x(dst),[x(bas),x30]
331 static void arm64_ldrv(int sz_, int dst, int bas, uint64_t off)
333 uint32_t sz = sz_;
334 if (!(off & ~((uint32_t)0xfff << sz)))
335 o(0x3d400000 | dst | bas << 5 | off << (10 - sz) |
336 (sz & 4) << 21 | (sz & 3) << 30); // ldr (s|d|q)(dst),[x(bas),#(off)]
337 else if (off < 256 || -off <= 256)
338 o(0x3c400000 | dst | bas << 5 | (off & 511) << 12 |
339 (sz & 4) << 21 | (sz & 3) << 30); // ldur (s|d|q)(dst),[x(bas),#(off)]
340 else {
341 arm64_movimm(30, off); // use x30 for offset
342 o(0x3c606800 | dst | bas << 5 | (uint32_t)30 << 16 |
343 sz << 30 | (sz & 4) << 21); // ldr (s|d|q)(dst),[x(bas),x30]
347 static void arm64_ldrs(int reg_, int size)
349 uint32_t reg = reg_;
350 // Use x30 for intermediate value in some cases.
351 switch (size) {
352 default: assert(0); break;
353 case 0:
354 /* Can happen with zero size structs */
355 break;
356 case 1:
357 arm64_ldrx(0, 0, reg, reg, 0);
358 break;
359 case 2:
360 arm64_ldrx(0, 1, reg, reg, 0);
361 break;
362 case 3:
363 arm64_ldrx(0, 1, 30, reg, 0);
364 arm64_ldrx(0, 0, reg, reg, 2);
365 o(0x2a0043c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #16
366 break;
367 case 4:
368 arm64_ldrx(0, 2, reg, reg, 0);
369 break;
370 case 5:
371 arm64_ldrx(0, 2, 30, reg, 0);
372 arm64_ldrx(0, 0, reg, reg, 4);
373 o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32
374 break;
375 case 6:
376 arm64_ldrx(0, 2, 30, reg, 0);
377 arm64_ldrx(0, 1, reg, reg, 4);
378 o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32
379 break;
380 case 7:
381 arm64_ldrx(0, 2, 30, reg, 0);
382 arm64_ldrx(0, 2, reg, reg, 3);
383 o(0x53087c00 | reg | reg << 5); // lsr w(reg), w(reg), #8
384 o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32
385 break;
386 case 8:
387 arm64_ldrx(0, 3, reg, reg, 0);
388 break;
389 case 9:
390 arm64_ldrx(0, 0, reg + 1, reg, 8);
391 arm64_ldrx(0, 3, reg, reg, 0);
392 break;
393 case 10:
394 arm64_ldrx(0, 1, reg + 1, reg, 8);
395 arm64_ldrx(0, 3, reg, reg, 0);
396 break;
397 case 11:
398 arm64_ldrx(0, 2, reg + 1, reg, 7);
399 o(0x53087c00 | (reg+1) | (reg+1) << 5); // lsr w(reg+1), w(reg+1), #8
400 arm64_ldrx(0, 3, reg, reg, 0);
401 break;
402 case 12:
403 arm64_ldrx(0, 2, reg + 1, reg, 8);
404 arm64_ldrx(0, 3, reg, reg, 0);
405 break;
406 case 13:
407 arm64_ldrx(0, 3, reg + 1, reg, 5);
408 o(0xd358fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #24
409 arm64_ldrx(0, 3, reg, reg, 0);
410 break;
411 case 14:
412 arm64_ldrx(0, 3, reg + 1, reg, 6);
413 o(0xd350fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #16
414 arm64_ldrx(0, 3, reg, reg, 0);
415 break;
416 case 15:
417 arm64_ldrx(0, 3, reg + 1, reg, 7);
418 o(0xd348fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #8
419 arm64_ldrx(0, 3, reg, reg, 0);
420 break;
421 case 16:
422 o(0xa9400000 | reg | (reg+1) << 10 | reg << 5);
423 // ldp x(reg),x(reg+1),[x(reg)]
424 break;
428 static void arm64_strx(int sz_, int dst, int bas, uint64_t off)
430 uint32_t sz = sz_;
431 if (!(off & ~((uint32_t)0xfff << sz)))
432 o(0x39000000 | dst | bas << 5 | off << (10 - sz) | sz << 30);
433 // str(*) x(dst),[x(bas],#(off)]
434 else if (off < 256 || -off <= 256)
435 o(0x38000000 | dst | bas << 5 | (off & 511) << 12 | sz << 30);
436 // stur(*) x(dst),[x(bas],#(off)]
437 else {
438 arm64_movimm(30, off); // use x30 for offset
439 o(0x38206800 | dst | bas << 5 | (uint32_t)30 << 16 | sz << 30);
440 // str(*) x(dst),[x(bas),x30]
444 static void arm64_strv(int sz_, int dst, int bas, uint64_t off)
446 uint32_t sz = sz_;
447 if (!(off & ~((uint32_t)0xfff << sz)))
448 o(0x3d000000 | dst | bas << 5 | off << (10 - sz) |
449 (sz & 4) << 21 | (sz & 3) << 30); // str (s|d|q)(dst),[x(bas),#(off)]
450 else if (off < 256 || -off <= 256)
451 o(0x3c000000 | dst | bas << 5 | (off & 511) << 12 |
452 (sz & 4) << 21 | (sz & 3) << 30); // stur (s|d|q)(dst),[x(bas),#(off)]
453 else {
454 arm64_movimm(30, off); // use x30 for offset
455 o(0x3c206800 | dst | bas << 5 | (uint32_t)30 << 16 |
456 sz << 30 | (sz & 4) << 21); // str (s|d|q)(dst),[x(bas),x30]
460 static void arm64_sym(int r, Sym *sym, unsigned long addend)
462 greloca(cur_text_section, sym, ind, R_AARCH64_ADR_GOT_PAGE, 0);
463 o(0x90000000 | r); // adrp xr, #sym
464 greloca(cur_text_section, sym, ind, R_AARCH64_LD64_GOT_LO12_NC, 0);
465 o(0xf9400000 | r | (r << 5)); // ld xr,[xr, #sym]
466 if (addend) {
467 // add xr, xr, #addend
468 if (addend & 0xffful)
469 o(0x91000000 | r | r << 5 | (addend & 0xfff) << 10);
470 if (addend > 0xffful) {
471 // add xr, xr, #addend, lsl #12
472 if (addend & 0xfff000ul)
473 o(0x91400000 | r | r << 5 | ((addend >> 12) & 0xfff) << 10);
474 if (addend > 0xfffffful) {
475 /* very unlikely */
476 int t = r ? 0 : 1;
477 o(0xf81f0fe0 | t); /* str xt, [sp, #-16]! */
478 arm64_movimm(t, addend & ~0xfffffful); // use xt for addent
479 o(0x91000000 | r | (t << 5)); /* add xr, xt, #0 */
480 o(0xf84107e0 | t); /* ldr xt, [sp], #16 */
486 static void arm64_load_cmp(int r, SValue *sv);
488 ST_FUNC void load(int r, SValue *sv)
490 int svtt = sv->type.t;
491 int svr = sv->r & ~(VT_BOUNDED | VT_NONCONST);
492 int svrv = svr & VT_VALMASK;
493 uint64_t svcul = (uint32_t)sv->c.i;
494 svcul = svcul >> 31 & 1 ? svcul - ((uint64_t)1 << 32) : svcul;
496 if (svr == (VT_LOCAL | VT_LVAL)) {
497 if (IS_FREG(r))
498 arm64_ldrv(arm64_type_size(svtt), fltr(r), 29, svcul);
499 else
500 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
501 intr(r), 29, svcul);
502 return;
505 if (svr == (VT_CONST | VT_LVAL)) {
506 if (sv->sym)
507 arm64_sym(30, sv->sym, // use x30 for address
508 arm64_check_offset(0, arm64_type_size(svtt), sv->c.i));
509 else
510 arm64_movimm (30, sv->c.i);
511 if (IS_FREG(r))
512 arm64_ldrv(arm64_type_size(svtt), fltr(r), 30,
513 arm64_check_offset(1, arm64_type_size(svtt), sv->c.i));
514 else
515 arm64_ldrx(!(svtt&VT_UNSIGNED), arm64_type_size(svtt), intr(r), 30,
516 arm64_check_offset(1, arm64_type_size(svtt), sv->c.i));
517 return;
520 if ((svr & ~VT_VALMASK) == VT_LVAL && svrv < VT_CONST) {
521 if ((svtt & VT_BTYPE) != VT_VOID) {
522 if (IS_FREG(r))
523 arm64_ldrv(arm64_type_size(svtt), fltr(r), intr(svrv), 0);
524 else
525 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
526 intr(r), intr(svrv), 0);
528 return;
531 if (svr == (VT_CONST | VT_LVAL | VT_SYM)) {
532 arm64_sym(30, sv->sym, // use x30 for address
533 arm64_check_offset(0, arm64_type_size(svtt), svcul));
534 if (IS_FREG(r))
535 arm64_ldrv(arm64_type_size(svtt), fltr(r), 30,
536 arm64_check_offset(1, arm64_type_size(svtt), svcul));
537 else
538 arm64_ldrx(!(svtt&VT_UNSIGNED), arm64_type_size(svtt), intr(r), 30,
539 arm64_check_offset(1, arm64_type_size(svtt), svcul));
540 return;
543 if (svr == (VT_CONST | VT_SYM)) {
544 arm64_sym(intr(r), sv->sym, svcul);
545 return;
548 if (svr == VT_CONST) {
549 if ((svtt & VT_BTYPE) != VT_VOID)
550 arm64_movimm(intr(r), arm64_type_size(svtt) == 3 ?
551 sv->c.i : (uint32_t)svcul);
552 return;
555 if (svr < VT_CONST) {
556 if (IS_FREG(r) && IS_FREG(svr))
557 if (svtt == VT_LDOUBLE)
558 o(0x4ea01c00 | fltr(r) | fltr(svr) << 5);
559 // mov v(r).16b,v(svr).16b
560 else
561 o(0x1e604000 | fltr(r) | fltr(svr) << 5); // fmov d(r),d(svr)
562 else if (!IS_FREG(r) && !IS_FREG(svr))
563 o(0xaa0003e0 | intr(r) | intr(svr) << 16); // mov x(r),x(svr)
564 else
565 assert(0);
566 return;
569 if (svr == VT_LOCAL) {
570 if (-svcul < 0x1000)
571 o(0xd10003a0 | intr(r) | -svcul << 10); // sub x(r),x29,#...
572 else {
573 arm64_movimm(30, -svcul); // use x30 for offset
574 o(0xcb0003a0 | intr(r) | (uint32_t)30 << 16); // sub x(r),x29,x30
576 return;
579 if (svr == VT_JMP || svr == VT_JMPI) {
580 int t = (svr == VT_JMPI);
581 arm64_movimm(intr(r), t);
582 o(0x14000002); // b .+8
583 gsym(svcul);
584 arm64_movimm(intr(r), t ^ 1);
585 return;
588 if (svr == (VT_LLOCAL | VT_LVAL)) {
589 arm64_ldrx(0, 3, 30, 29, svcul); // use x30 for offset
590 if (IS_FREG(r))
591 arm64_ldrv(arm64_type_size(svtt), fltr(r), 30, 0);
592 else
593 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
594 intr(r), 30, 0);
595 return;
598 if (svr == VT_CMP) {
599 arm64_load_cmp(r, sv);
600 return;
603 printf("load(%x, (%x, %x, %lx))\n", r, svtt, sv->r, (long)svcul);
604 assert(0);
607 ST_FUNC void store(int r, SValue *sv)
609 int svtt = sv->type.t;
610 int svr = sv->r & ~VT_BOUNDED;
611 int svrv = svr & VT_VALMASK;
612 uint64_t svcul = (uint32_t)sv->c.i;
613 svcul = svcul >> 31 & 1 ? svcul - ((uint64_t)1 << 32) : svcul;
615 if (svr == (VT_LOCAL | VT_LVAL)) {
616 if (IS_FREG(r))
617 arm64_strv(arm64_type_size(svtt), fltr(r), 29, svcul);
618 else
619 arm64_strx(arm64_type_size(svtt), intr(r), 29, svcul);
620 return;
623 if (svr == (VT_CONST | VT_LVAL)) {
624 if (sv->sym)
625 arm64_sym(30, sv->sym, // use x30 for address
626 arm64_check_offset(0, arm64_type_size(svtt), sv->c.i));
627 else
628 arm64_movimm (30, sv->c.i);
629 if (IS_FREG(r))
630 arm64_strv(arm64_type_size(svtt), fltr(r), 30,
631 arm64_check_offset(1, arm64_type_size(svtt), sv->c.i));
632 else
633 arm64_strx(arm64_type_size(svtt), intr(r), 30,
634 arm64_check_offset(1, arm64_type_size(svtt), sv->c.i));
635 return;
638 if ((svr & ~VT_VALMASK) == VT_LVAL && svrv < VT_CONST) {
639 if (IS_FREG(r))
640 arm64_strv(arm64_type_size(svtt), fltr(r), intr(svrv), 0);
641 else
642 arm64_strx(arm64_type_size(svtt), intr(r), intr(svrv), 0);
643 return;
646 if (svr == (VT_CONST | VT_LVAL | VT_SYM)) {
647 arm64_sym(30, sv->sym, // use x30 for address
648 arm64_check_offset(0, arm64_type_size(svtt), svcul));
649 if (IS_FREG(r))
650 arm64_strv(arm64_type_size(svtt), fltr(r), 30,
651 arm64_check_offset(1, arm64_type_size(svtt), svcul));
652 else
653 arm64_strx(arm64_type_size(svtt), intr(r), 30,
654 arm64_check_offset(1, arm64_type_size(svtt), svcul));
655 return;
658 printf("store(%x, (%x, %x, %lx))\n", r, svtt, sv->r, (long)svcul);
659 assert(0);
662 static void arm64_gen_bl_or_b(int b)
664 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST && (vtop->r & VT_SYM)) {
665 greloca(cur_text_section, vtop->sym, ind,
666 b ? R_AARCH64_JUMP26 : R_AARCH64_CALL26, 0);
667 o(0x14000000 | (uint32_t)!b << 31); // b/bl .
669 else {
670 #ifdef CONFIG_TCC_BCHECK
671 vtop->r &= ~VT_MUSTBOUND;
672 #endif
673 o(0xd61f0000 | (uint32_t)!b << 21 | intr(gv(RC_R30)) << 5); // br/blr
677 #if defined(CONFIG_TCC_BCHECK)
679 static void gen_bounds_call(int v)
681 Sym *sym = external_helper_sym(v);
683 greloca(cur_text_section, sym, ind, R_AARCH64_CALL26, 0);
684 o(0x94000000); // bl
687 static void gen_bounds_prolog(void)
689 /* leave some room for bound checking code */
690 func_bound_offset = lbounds_section->data_offset;
691 func_bound_ind = ind;
692 func_bound_add_epilog = 0;
693 o(0xd503201f); /* nop -> mov x0, lbound section pointer */
694 o(0xd503201f);
695 o(0xd503201f);
696 o(0xd503201f); /* nop -> call __bound_local_new */
699 static void gen_bounds_epilog(void)
701 addr_t saved_ind;
702 addr_t *bounds_ptr;
703 Sym *sym_data;
704 int offset_modified = func_bound_offset != lbounds_section->data_offset;
706 if (!offset_modified && !func_bound_add_epilog)
707 return;
709 /* add end of table info */
710 bounds_ptr = section_ptr_add(lbounds_section, sizeof(addr_t));
711 *bounds_ptr = 0;
713 sym_data = get_sym_ref(&char_pointer_type, lbounds_section,
714 func_bound_offset, PTR_SIZE);
716 /* generate bound local allocation */
717 if (offset_modified) {
718 saved_ind = ind;
719 ind = func_bound_ind;
720 greloca(cur_text_section, sym_data, ind, R_AARCH64_ADR_GOT_PAGE, 0);
721 o(0x90000000 | 0); // adrp x0, #sym_data
722 greloca(cur_text_section, sym_data, ind, R_AARCH64_LD64_GOT_LO12_NC, 0);
723 o(0xf9400000 | 0 | (0 << 5)); // ld x0,[x0, #sym_data]
724 gen_bounds_call(TOK___bound_local_new);
725 ind = saved_ind;
728 /* generate bound check local freeing */
729 o(0xa9bf07e0); /* stp x0, x1, [sp, #-16]! */
730 o(0x3c9f0fe0); /* str q0, [sp, #-16]! */
731 greloca(cur_text_section, sym_data, ind, R_AARCH64_ADR_GOT_PAGE, 0);
732 o(0x90000000 | 0); // adrp x0, #sym_data
733 greloca(cur_text_section, sym_data, ind, R_AARCH64_LD64_GOT_LO12_NC, 0);
734 o(0xf9400000 | 0 | (0 << 5)); // ld x0,[x0, #sym_data]
735 gen_bounds_call(TOK___bound_local_delete);
736 o(0x3cc107e0); /* ldr q0, [sp], #16 */
737 o(0xa8c107e0); /* ldp x0, x1, [sp], #16 */
739 #endif
741 static int arm64_hfa_aux(CType *type, int *fsize, int num)
743 if (is_float(type->t)) {
744 int a, n = type_size(type, &a);
745 if (num >= 4 || (*fsize && *fsize != n))
746 return -1;
747 *fsize = n;
748 return num + 1;
750 else if ((type->t & VT_BTYPE) == VT_STRUCT) {
751 int is_struct = 0; // rather than union
752 Sym *field;
753 for (field = type->ref->next; field; field = field->next)
754 if (field->c) {
755 is_struct = 1;
756 break;
758 if (is_struct) {
759 int num0 = num;
760 for (field = type->ref->next; field; field = field->next) {
761 if (field->c != (num - num0) * *fsize)
762 return -1;
763 num = arm64_hfa_aux(&field->type, fsize, num);
764 if (num == -1)
765 return -1;
767 if (type->ref->c != (num - num0) * *fsize)
768 return -1;
769 return num;
771 else { // union
772 int num0 = num;
773 for (field = type->ref->next; field; field = field->next) {
774 int num1 = arm64_hfa_aux(&field->type, fsize, num0);
775 if (num1 == -1)
776 return -1;
777 num = num1 < num ? num : num1;
779 if (type->ref->c != (num - num0) * *fsize)
780 return -1;
781 return num;
784 else if ((type->t & VT_ARRAY) && ((type->t & VT_BTYPE) != VT_PTR)) {
785 int num1;
786 if (!type->ref->c)
787 return num;
788 num1 = arm64_hfa_aux(&type->ref->type, fsize, num);
789 if (num1 == -1 || (num1 != num && type->ref->c > 4))
790 return -1;
791 num1 = num + type->ref->c * (num1 - num);
792 if (num1 > 4)
793 return -1;
794 return num1;
796 return -1;
799 static int arm64_hfa(CType *type, unsigned *fsize)
801 if ((type->t & VT_BTYPE) == VT_STRUCT ||
802 ((type->t & VT_ARRAY) && ((type->t & VT_BTYPE) != VT_PTR))) {
803 int sz = 0;
804 int n = arm64_hfa_aux(type, &sz, 0);
805 if (0 < n && n <= 4) {
806 if (fsize)
807 *fsize = sz;
808 return n;
811 return 0;
814 static unsigned long arm64_pcs_aux(int variadic, int n, CType **type, unsigned long *a)
816 int nx = 0; // next integer register
817 int nv = 0; // next vector register
818 unsigned long ns = 32; // next stack offset
819 int i;
821 for (i = 0; i < n; i++) {
822 int hfa = arm64_hfa(type[i], 0);
823 int size, align;
825 if ((type[i]->t & VT_ARRAY) ||
826 (type[i]->t & VT_BTYPE) == VT_FUNC)
827 size = align = 8;
828 else
829 size = type_size(type[i], &align);
831 #if defined(TCC_TARGET_MACHO)
832 if (variadic && i == variadic) {
833 nx = 8;
834 nv = 8;
836 #endif
837 if (hfa)
838 // B.2
840 else if (size > 16) {
841 // B.3: replace with pointer
842 if (nx < 8)
843 a[i] = nx++ << 1 | 1;
844 else {
845 ns = (ns + 7) & ~7;
846 a[i] = ns | 1;
847 ns += 8;
849 continue;
851 else if ((type[i]->t & VT_BTYPE) == VT_STRUCT)
852 // B.4
853 size = (size + 7) & ~7;
855 // C.1
856 if (is_float(type[i]->t) && nv < 8) {
857 a[i] = 16 + (nv++ << 1);
858 continue;
861 // C.2
862 if (hfa && nv + hfa <= 8) {
863 a[i] = 16 + (nv << 1);
864 nv += hfa;
865 continue;
868 // C.3
869 if (hfa) {
870 nv = 8;
871 size = (size + 7) & ~7;
874 // C.4
875 if (hfa || (type[i]->t & VT_BTYPE) == VT_LDOUBLE) {
876 ns = (ns + 7) & ~7;
877 ns = (ns + align - 1) & -align;
880 // C.5
881 if ((type[i]->t & VT_BTYPE) == VT_FLOAT)
882 size = 8;
884 // C.6
885 if (hfa || is_float(type[i]->t)) {
886 a[i] = ns;
887 ns += size;
888 continue;
891 // C.7
892 if ((type[i]->t & VT_BTYPE) != VT_STRUCT && size <= 8 && nx < 8) {
893 a[i] = nx++ << 1;
894 continue;
897 // C.8
898 if (align == 16)
899 nx = (nx + 1) & ~1;
901 // C.9
902 if ((type[i]->t & VT_BTYPE) != VT_STRUCT && size == 16 && nx < 7) {
903 a[i] = nx << 1;
904 nx += 2;
905 continue;
908 // C.10
909 if ((type[i]->t & VT_BTYPE) == VT_STRUCT && size <= (8 - nx) * 8) {
910 a[i] = nx << 1;
911 nx += (size + 7) >> 3;
912 continue;
915 // C.11
916 nx = 8;
918 // C.12
919 ns = (ns + 7) & ~7;
920 ns = (ns + align - 1) & -align;
922 // C.13
923 if ((type[i]->t & VT_BTYPE) == VT_STRUCT) {
924 a[i] = ns;
925 ns += size;
926 continue;
929 // C.14
930 if (size < 8)
931 size = 8;
933 // C.15
934 a[i] = ns;
935 ns += size;
938 return ns - 32;
941 static unsigned long arm64_pcs(int variadic, int n, CType **type, unsigned long *a)
943 unsigned long stack;
945 // Return type:
946 if ((type[0]->t & VT_BTYPE) == VT_VOID)
947 a[0] = -1;
948 else {
949 arm64_pcs_aux(0, 1, type, a);
950 assert(a[0] == 0 || a[0] == 1 || a[0] == 16);
953 // Argument types:
954 stack = arm64_pcs_aux(variadic, n, type + 1, a + 1);
956 if (0) {
957 int i;
958 for (i = 0; i <= n; i++) {
959 if (!i)
960 printf("arm64_pcs return: ");
961 else
962 printf("arm64_pcs arg %d: ", i);
963 if (a[i] == (unsigned long)-1)
964 printf("void\n");
965 else if (a[i] == 1 && !i)
966 printf("X8 pointer\n");
967 else if (a[i] < 16)
968 printf("X%lu%s\n", a[i] / 2, a[i] & 1 ? " pointer" : "");
969 else if (a[i] < 32)
970 printf("V%lu\n", a[i] / 2 - 8);
971 else
972 printf("stack %lu%s\n",
973 (a[i] - 32) & ~1, a[i] & 1 ? " pointer" : "");
977 return stack;
980 static int n_func_args(CType *type)
982 int n_args = 0;
983 Sym *arg;
985 for (arg = type->ref->next; arg; arg = arg->next)
986 n_args++;
987 return n_args;
990 ST_FUNC void gfunc_call(int nb_args)
992 CType *return_type;
993 CType **t;
994 unsigned long *a, *a1;
995 unsigned long stack;
996 int i;
997 int variadic = (vtop[-nb_args].type.ref->f.func_type == FUNC_ELLIPSIS);
998 int var_nb_arg = n_func_args(&vtop[-nb_args].type);
1000 #ifdef CONFIG_TCC_BCHECK
1001 if (tcc_state->do_bounds_check)
1002 gbound_args(nb_args);
1003 #endif
1005 return_type = &vtop[-nb_args].type.ref->type;
1006 if ((return_type->t & VT_BTYPE) == VT_STRUCT)
1007 --nb_args;
1009 t = tcc_malloc((nb_args + 1) * sizeof(*t));
1010 a = tcc_malloc((nb_args + 1) * sizeof(*a));
1011 a1 = tcc_malloc((nb_args + 1) * sizeof(*a1));
1013 t[0] = return_type;
1014 for (i = 0; i < nb_args; i++)
1015 t[nb_args - i] = &vtop[-i].type;
1017 stack = arm64_pcs(variadic ? var_nb_arg : 0, nb_args, t, a);
1019 // Allocate space for structs replaced by pointer:
1020 for (i = nb_args; i; i--)
1021 if (a[i] & 1) {
1022 SValue *arg = &vtop[i - nb_args];
1023 int align, size = type_size(&arg->type, &align);
1024 assert((arg->type.t & VT_BTYPE) == VT_STRUCT);
1025 stack = (stack + align - 1) & -align;
1026 a1[i] = stack;
1027 stack += size;
1030 stack = (stack + 15) >> 4 << 4;
1032 /* fetch cpu flag before generating any code */
1033 if ((vtop->r & VT_VALMASK) == VT_CMP)
1034 gv(RC_INT);
1036 if (stack >= 0x1000000) // 16Mb
1037 tcc_error("stack size too big %lu", stack);
1038 if (stack & 0xfff)
1039 o(0xd10003ff | (stack & 0xfff) << 10); // sub sp,sp,#(n)
1040 if (stack >> 12)
1041 o(0xd14003ff | (stack >> 12) << 10);
1043 // First pass: set all values on stack
1044 for (i = nb_args; i; i--) {
1045 vpushv(vtop - nb_args + i);
1047 if (a[i] & 1) {
1048 // struct replaced by pointer
1049 int r = get_reg(RC_INT);
1050 arm64_spoff(intr(r), a1[i]);
1051 vset(&vtop->type, r | VT_LVAL, 0);
1052 vswap();
1053 vstore();
1054 if (a[i] >= 32) {
1055 // pointer on stack
1056 r = get_reg(RC_INT);
1057 arm64_spoff(intr(r), a1[i]);
1058 arm64_strx(3, intr(r), 31, (a[i] - 32) >> 1 << 1);
1061 else if (a[i] >= 32) {
1062 // value on stack
1063 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
1064 int r = get_reg(RC_INT);
1065 arm64_spoff(intr(r), a[i] - 32);
1066 vset(&vtop->type, r | VT_LVAL, 0);
1067 vswap();
1068 vstore();
1070 else if (is_float(vtop->type.t)) {
1071 gv(RC_FLOAT);
1072 arm64_strv(arm64_type_size(vtop[0].type.t),
1073 fltr(vtop[0].r), 31, a[i] - 32);
1075 else {
1076 gv(RC_INT);
1077 arm64_strx(3, // arm64_type_size(vtop[0].type.t),
1078 intr(vtop[0].r), 31, a[i] - 32);
1082 --vtop;
1085 // Second pass: assign values to registers
1086 for (i = nb_args; i; i--, vtop--) {
1087 if (a[i] < 16 && !(a[i] & 1)) {
1088 // value in general-purpose registers
1089 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
1090 int align, size = type_size(&vtop->type, &align);
1091 if (size) {
1092 vtop->type.t = VT_PTR;
1093 gaddrof();
1094 gv(RC_R(a[i] / 2));
1095 arm64_ldrs(a[i] / 2, size);
1098 else
1099 gv(RC_R(a[i] / 2));
1101 else if (a[i] < 16)
1102 // struct replaced by pointer in register
1103 arm64_spoff(a[i] / 2, a1[i]);
1104 else if (a[i] < 32) {
1105 // value in floating-point registers
1106 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
1107 uint32_t j, sz, n = arm64_hfa(&vtop->type, &sz);
1108 vtop->type.t = VT_PTR;
1109 gaddrof();
1110 gv(RC_R30);
1111 for (j = 0; j < n; j++)
1112 o(0x3d4003c0 |
1113 (sz & 16) << 19 | -(sz & 8) << 27 | (sz & 4) << 29 |
1114 (a[i] / 2 - 8 + j) |
1115 j << 10); // ldr ([sdq])(*),[x30,#(j * sz)]
1117 else
1118 gv(RC_F(a[i] / 2 - 8));
1122 if ((return_type->t & VT_BTYPE) == VT_STRUCT) {
1123 if (a[0] == 1) {
1124 // indirect return: set x8 and discard the stack value
1125 gv(RC_R(8));
1126 --vtop;
1128 else
1129 // return in registers: keep the address for after the call
1130 vswap();
1133 save_regs(0);
1134 arm64_gen_bl_or_b(0);
1135 --vtop;
1136 if (stack & 0xfff)
1137 o(0x910003ff | (stack & 0xfff) << 10); // add sp,sp,#(n)
1138 if (stack >> 12)
1139 o(0x914003ff | (stack >> 12) << 10);
1142 int rt = return_type->t;
1143 int bt = rt & VT_BTYPE;
1144 if (bt == VT_STRUCT && !(a[0] & 1)) {
1145 // A struct was returned in registers, so write it out:
1146 gv(RC_R(8));
1147 --vtop;
1148 if (a[0] == 0) {
1149 int align, size = type_size(return_type, &align);
1150 assert(size <= 16);
1151 if (size > 8)
1152 o(0xa9000500); // stp x0,x1,[x8]
1153 else if (size)
1154 arm64_strx(size > 4 ? 3 : size > 2 ? 2 : size > 1, 0, 8, 0);
1157 else if (a[0] == 16) {
1158 uint32_t j, sz, n = arm64_hfa(return_type, &sz);
1159 for (j = 0; j < n; j++)
1160 o(0x3d000100 |
1161 (sz & 16) << 19 | -(sz & 8) << 27 | (sz & 4) << 29 |
1162 (a[i] / 2 - 8 + j) |
1163 j << 10); // str ([sdq])(*),[x8,#(j * sz)]
1168 tcc_free(a1);
1169 tcc_free(a);
1170 tcc_free(t);
1173 static unsigned long arm64_func_va_list_stack;
1174 static int arm64_func_va_list_gr_offs;
1175 static int arm64_func_va_list_vr_offs;
1176 static int arm64_func_sub_sp_offset;
1178 ST_FUNC void gfunc_prolog(Sym *func_sym)
1180 CType *func_type = &func_sym->type;
1181 int n = 0;
1182 int i = 0;
1183 Sym *sym;
1184 CType **t;
1185 unsigned long *a;
1186 int use_x8 = 0;
1187 int last_int = 0;
1188 int last_float = 0;
1189 int variadic = func_sym->type.ref->f.func_type == FUNC_ELLIPSIS;
1190 int var_nb_arg = n_func_args(&func_sym->type);
1192 func_vc = 144; // offset of where x8 is stored
1194 for (sym = func_type->ref; sym; sym = sym->next)
1195 ++n;
1196 t = n ? tcc_malloc(n * sizeof(*t)) : NULL;
1197 a = n ? tcc_malloc(n * sizeof(*a)) : NULL;
1199 for (sym = func_type->ref; sym; sym = sym->next)
1200 t[i++] = &sym->type;
1202 arm64_func_va_list_stack = arm64_pcs(variadic ? var_nb_arg : 0, n - 1, t, a);
1204 #if !defined(TCC_TARGET_MACHO)
1205 if (variadic) {
1206 use_x8 = 1;
1207 last_int = 4;
1208 last_float = 4;
1210 #endif
1211 if (a && a[0] == 1)
1212 use_x8 = 1;
1213 for (i = 1, sym = func_type->ref->next; sym; i++, sym = sym->next) {
1214 if (a[i] < 16) {
1215 int last, align, size = type_size(&sym->type, &align);
1216 last = a[i] / 4 + 1 + (size - 1) / 8;
1217 last_int = last > last_int ? last : last_int;
1219 else if (a[i] < 32) {
1220 int last, hfa = arm64_hfa(&sym->type, 0);
1221 last = a[i] / 4 - 3 + (hfa ? hfa - 1 : 0);
1222 last_float = last > last_float ? last : last_float;
1226 last_int = last_int > 4 ? 4 : last_int;
1227 last_float = last_float > 4 ? 4 : last_float;
1229 o(0xa9b27bfd); // stp x29,x30,[sp,#-224]!
1230 for (i = 0; i < last_float; i++)
1231 // stp q0,q1,[sp,#16], stp q2,q3,[sp,#48]
1232 // stp q4,q5,[sp,#80], stp q6,q7,[sp,#112]
1233 o(0xad0087e0 + i * 0x10000 + (i << 11) + (i << 1));
1234 if (use_x8)
1235 o(0xa90923e8); // stp x8,x8,[sp,#144]
1236 for (i = 0; i < last_int; i++)
1237 // stp x0,x1,[sp,#160], stp x2,x3,[sp,#176]
1238 // stp x4,x5,[sp,#192], stp x6,x7,[sp,#208]
1239 o(0xa90a07e0 + i * 0x10000 + (i << 11) + (i << 1));
1241 arm64_func_va_list_gr_offs = -64;
1242 arm64_func_va_list_vr_offs = -128;
1244 for (i = 1, sym = func_type->ref->next; sym; i++, sym = sym->next) {
1245 int off = (a[i] < 16 ? 160 + a[i] / 2 * 8 :
1246 a[i] < 32 ? 16 + (a[i] - 16) / 2 * 16 :
1247 224 + ((a[i] - 32) >> 1 << 1));
1248 sym_push(sym->v & ~SYM_FIELD, &sym->type,
1249 (a[i] & 1 ? VT_LLOCAL : VT_LOCAL) | VT_LVAL,
1250 off);
1252 if (a[i] < 16) {
1253 int align, size = type_size(&sym->type, &align);
1254 arm64_func_va_list_gr_offs = (a[i] / 2 - 7 +
1255 (!(a[i] & 1) && size > 8)) * 8;
1257 else if (a[i] < 32) {
1258 uint32_t hfa = arm64_hfa(&sym->type, 0);
1259 arm64_func_va_list_vr_offs = (a[i] / 2 - 16 +
1260 (hfa ? hfa : 1)) * 16;
1263 // HFAs of float and double need to be written differently:
1264 if (16 <= a[i] && a[i] < 32 && (sym->type.t & VT_BTYPE) == VT_STRUCT) {
1265 uint32_t j, sz, k = arm64_hfa(&sym->type, &sz);
1266 if (sz < 16)
1267 for (j = 0; j < k; j++) {
1268 o(0x3d0003e0 | -(sz & 8) << 27 | (sz & 4) << 29 |
1269 ((a[i] - 16) / 2 + j) | (off / sz + j) << 10);
1270 // str ([sdq])(*),[sp,#(j * sz)]
1275 tcc_free(a);
1276 tcc_free(t);
1278 o(0x910003fd); // mov x29,sp
1279 arm64_func_sub_sp_offset = ind;
1280 // In gfunc_epilog these will be replaced with code to decrement SP:
1281 o(0xd503201f); // nop
1282 o(0xd503201f); // nop
1283 loc = 0;
1284 #ifdef CONFIG_TCC_BCHECK
1285 if (tcc_state->do_bounds_check)
1286 gen_bounds_prolog();
1287 #endif
1290 ST_FUNC void gen_va_start(void)
1292 int r;
1293 --vtop; // we don't need the "arg"
1294 gaddrof();
1295 r = intr(gv(RC_INT));
1297 if (arm64_func_va_list_stack) {
1298 //xx could use add (immediate) here
1299 arm64_movimm(30, arm64_func_va_list_stack + 224);
1300 o(0x8b1e03be); // add x30,x29,x30
1302 else
1303 o(0x910383be); // add x30,x29,#224
1304 o(0xf900001e | r << 5); // str x30,[x(r)]
1306 #if !defined(TCC_TARGET_MACHO)
1307 if (arm64_func_va_list_gr_offs) {
1308 if (arm64_func_va_list_stack)
1309 o(0x910383be); // add x30,x29,#224
1310 o(0xf900041e | r << 5); // str x30,[x(r),#8]
1313 if (arm64_func_va_list_vr_offs) {
1314 o(0x910243be); // add x30,x29,#144
1315 o(0xf900081e | r << 5); // str x30,[x(r),#16]
1318 arm64_movimm(30, arm64_func_va_list_gr_offs);
1319 o(0xb900181e | r << 5); // str w30,[x(r),#24]
1321 arm64_movimm(30, arm64_func_va_list_vr_offs);
1322 o(0xb9001c1e | r << 5); // str w30,[x(r),#28]
1323 #endif
1325 --vtop;
1328 ST_FUNC void gen_va_arg(CType *t)
1330 int align, size = type_size(t, &align);
1331 unsigned fsize, hfa = arm64_hfa(t, &fsize);
1332 uint32_t r0, r1;
1334 if (is_float(t->t)) {
1335 hfa = 1;
1336 fsize = size;
1339 gaddrof();
1340 r0 = intr(gv(RC_INT));
1341 r1 = get_reg(RC_INT);
1342 vtop[0].r = r1 | VT_LVAL;
1343 r1 = intr(r1);
1345 if (!hfa) {
1346 uint32_t n = size > 16 ? 8 : (size + 7) & -8;
1347 #if !defined(TCC_TARGET_MACHO)
1348 o(0xb940181e | r0 << 5); // ldr w30,[x(r0),#24] // __gr_offs
1349 if (align == 16) {
1350 assert(0); // this path untested but needed for __uint128_t
1351 o(0x11003fde); // add w30,w30,#15
1352 o(0x121c6fde); // and w30,w30,#-16
1354 o(0x310003c0 | r1 | n << 10); // adds w(r1),w30,#(n)
1355 o(0x540000ad); // b.le .+20
1356 #endif
1357 o(0xf9400000 | r1 | r0 << 5); // ldr x(r1),[x(r0)] // __stack
1358 o(0x9100001e | r1 << 5 | n << 10); // add x30,x(r1),#(n)
1359 o(0xf900001e | r0 << 5); // str x30,[x(r0)] // __stack
1360 #if !defined(TCC_TARGET_MACHO)
1361 o(0x14000004); // b .+16
1362 o(0xb9001800 | r1 | r0 << 5); // str w(r1),[x(r0),#24] // __gr_offs
1363 o(0xf9400400 | r1 | r0 << 5); // ldr x(r1),[x(r0),#8] // __gr_top
1364 o(0x8b3ec000 | r1 | r1 << 5); // add x(r1),x(r1),w30,sxtw
1365 #endif
1366 if (size > 16)
1367 o(0xf9400000 | r1 | r1 << 5); // ldr x(r1),[x(r1)]
1369 else {
1370 uint32_t ssz = (size + 7) & -(uint32_t)8;
1371 #if !defined(TCC_TARGET_MACHO)
1372 uint32_t rsz = hfa << 4;
1373 uint32_t b1, b2;
1374 o(0xb9401c1e | r0 << 5); // ldr w30,[x(r0),#28] // __vr_offs
1375 o(0x310003c0 | r1 | rsz << 10); // adds w(r1),w30,#(rsz)
1376 b1 = ind; o(0x5400000d); // b.le lab1
1377 #endif
1378 o(0xf9400000 | r1 | r0 << 5); // ldr x(r1),[x(r0)] // __stack
1379 if (fsize == 16) {
1380 o(0x91003c00 | r1 | r1 << 5); // add x(r1),x(r1),#15
1381 o(0x927cec00 | r1 | r1 << 5); // and x(r1),x(r1),#-16
1383 o(0x9100001e | r1 << 5 | ssz << 10); // add x30,x(r1),#(ssz)
1384 o(0xf900001e | r0 << 5); // str x30,[x(r0)] // __stack
1385 #if !defined(TCC_TARGET_MACHO)
1386 b2 = ind; o(0x14000000); // b lab2
1387 // lab1:
1388 write32le(cur_text_section->data + b1, 0x5400000d | (ind - b1) << 3);
1389 o(0xb9001c00 | r1 | r0 << 5); // str w(r1),[x(r0),#28] // __vr_offs
1390 o(0xf9400800 | r1 | r0 << 5); // ldr x(r1),[x(r0),#16] // __vr_top
1391 if (hfa == 1 || fsize == 16)
1392 o(0x8b3ec000 | r1 | r1 << 5); // add x(r1),x(r1),w30,sxtw
1393 else {
1394 // We need to change the layout of this HFA.
1395 // Get some space on the stack using global variable "loc":
1396 loc = (loc - size) & -(uint32_t)align;
1397 o(0x8b3ec000 | 30 | r1 << 5); // add x30,x(r1),w30,sxtw
1398 arm64_movimm(r1, loc);
1399 o(0x8b0003a0 | r1 | r1 << 16); // add x(r1),x29,x(r1)
1400 o(0x4c402bdc | (uint32_t)fsize << 7 |
1401 (uint32_t)(hfa == 2) << 15 |
1402 (uint32_t)(hfa == 3) << 14); // ld1 {v28.(4s|2d),...},[x30]
1403 o(0x0d00801c | r1 << 5 | (fsize == 8) << 10 |
1404 (uint32_t)(hfa != 2) << 13 |
1405 (uint32_t)(hfa != 3) << 21); // st(hfa) {v28.(s|d),...}[0],[x(r1)]
1407 // lab2:
1408 write32le(cur_text_section->data + b2, 0x14000000 | (ind - b2) >> 2);
1409 #endif
1413 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret,
1414 int *align, int *regsize)
1416 return 0;
1419 ST_FUNC void gfunc_return(CType *func_type)
1421 CType *t = func_type;
1422 unsigned long a;
1424 arm64_pcs(0, 0, &t, &a);
1425 switch (a) {
1426 case -1:
1427 break;
1428 case 0:
1429 if ((func_type->t & VT_BTYPE) == VT_STRUCT) {
1430 int align, size = type_size(func_type, &align);
1431 gaddrof();
1432 gv(RC_R(0));
1433 arm64_ldrs(0, size);
1435 else
1436 gv(RC_IRET);
1437 break;
1438 case 1: {
1439 CType type = *func_type;
1440 mk_pointer(&type);
1441 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
1442 indir();
1443 vswap();
1444 vstore();
1445 break;
1447 case 16:
1448 if ((func_type->t & VT_BTYPE) == VT_STRUCT) {
1449 uint32_t j, sz, n = arm64_hfa(&vtop->type, &sz);
1450 gaddrof();
1451 gv(RC_R(0));
1452 for (j = 0; j < n; j++)
1453 o(0x3d400000 |
1454 (sz & 16) << 19 | -(sz & 8) << 27 | (sz & 4) << 29 |
1455 j | j << 10); // ldr ([sdq])(*),[x0,#(j * sz)]
1457 else
1458 gv(RC_FRET);
1459 break;
1460 default:
1461 assert(0);
1463 vtop--;
1466 ST_FUNC void gfunc_epilog(void)
1468 #ifdef CONFIG_TCC_BCHECK
1469 if (tcc_state->do_bounds_check)
1470 gen_bounds_epilog();
1471 #endif
1473 if (loc) {
1474 // Insert instructions to subtract size of stack frame from SP.
1475 unsigned char *ptr = cur_text_section->data + arm64_func_sub_sp_offset;
1476 uint64_t diff = (-loc + 15) & ~15;
1477 if (!(diff >> 24)) {
1478 if (diff & 0xfff) // sub sp,sp,#(diff & 0xfff)
1479 write32le(ptr, 0xd10003ff | (diff & 0xfff) << 10);
1480 if (diff >> 12) // sub sp,sp,#(diff >> 12),lsl #12
1481 write32le(ptr + 4, 0xd14003ff | (diff >> 12) << 10);
1483 else {
1484 // In this case we may subtract more than necessary,
1485 // but always less than 17/16 of what we were aiming for.
1486 int i = 0;
1487 int j = 0;
1488 while (diff >> 20) {
1489 diff = (diff + 0xffff) >> 16;
1490 ++i;
1492 while (diff >> 16) {
1493 diff = (diff + 1) >> 1;
1494 ++j;
1496 write32le(ptr, 0xd2800010 | diff << 5 | i << 21);
1497 // mov x16,#(diff),lsl #(16 * i)
1498 write32le(ptr + 4, 0xcb3063ff | j << 10);
1499 // sub sp,sp,x16,lsl #(j)
1502 o(0x910003bf); // mov sp,x29
1503 o(0xa8ce7bfd); // ldp x29,x30,[sp],#224
1505 o(0xd65f03c0); // ret
1508 ST_FUNC void gen_fill_nops(int bytes)
1510 if ((bytes & 3))
1511 tcc_error("alignment of code section not multiple of 4");
1512 while (bytes > 0) {
1513 o(0xd503201f); // nop
1514 bytes -= 4;
1518 // Generate forward branch to label:
1519 ST_FUNC int gjmp(int t)
1521 int r = ind;
1522 if (nocode_wanted)
1523 return t;
1524 o(t);
1525 return r;
1528 // Generate branch to known address:
1529 ST_FUNC void gjmp_addr(int a)
1531 assert(a - ind + 0x8000000 < 0x10000000);
1532 o(0x14000000 | ((a - ind) >> 2 & 0x3ffffff));
1535 ST_FUNC int gjmp_append(int n, int t)
1537 void *p;
1538 /* insert vtop->c jump list in t */
1539 if (n) {
1540 uint32_t n1 = n, n2;
1541 while ((n2 = read32le(p = cur_text_section->data + n1)))
1542 n1 = n2;
1543 write32le(p, t);
1544 t = n;
1546 return t;
1549 void arm64_vset_VT_CMP(int op)
1551 if (op >= TOK_ULT && op <= TOK_GT) {
1552 vtop->cmp_r = vtop->r;
1553 vset_VT_CMP(0x80);
1557 static void arm64_gen_opil(int op, uint32_t l);
1559 static void arm64_load_cmp(int r, SValue *sv)
1561 sv->r = sv->cmp_r;
1562 if (sv->c.i & 1) {
1563 vpushi(1);
1564 arm64_gen_opil('^', 0);
1566 if (r != sv->r) {
1567 load(r, sv);
1568 sv->r = r;
1572 ST_FUNC int gjmp_cond(int op, int t)
1574 int bt = vtop->type.t & VT_BTYPE;
1576 int inv = op & 1;
1577 vtop->r = vtop->cmp_r;
1579 if (bt == VT_LDOUBLE) {
1580 uint32_t a, b, f = fltr(gv(RC_FLOAT));
1581 a = get_reg(RC_INT);
1582 vpushi(0);
1583 vtop[0].r = a;
1584 b = get_reg(RC_INT);
1585 a = intr(a);
1586 b = intr(b);
1587 o(0x4e083c00 | a | f << 5); // mov x(a),v(f).d[0]
1588 o(0x4e183c00 | b | f << 5); // mov x(b),v(f).d[1]
1589 o(0xaa000400 | a | a << 5 | b << 16); // orr x(a),x(a),x(b),lsl #1
1590 o(0xb4000040 | a | !!inv << 24); // cbz/cbnz x(a),.+8
1591 --vtop;
1593 else if (bt == VT_FLOAT || bt == VT_DOUBLE) {
1594 uint32_t a = fltr(gv(RC_FLOAT));
1595 o(0x1e202008 | a << 5 | (bt != VT_FLOAT) << 22); // fcmp
1596 o(0x54000040 | !!inv); // b.eq/b.ne .+8
1598 else {
1599 uint32_t ll = (bt == VT_PTR || bt == VT_LLONG);
1600 uint32_t a = intr(gv(RC_INT));
1601 o(0x34000040 | a | !!inv << 24 | ll << 31); // cbz/cbnz wA,.+8
1603 return gjmp(t);
1606 static int arm64_iconst(uint64_t *val, SValue *sv)
1608 if ((sv->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
1609 return 0;
1610 if (val) {
1611 int t = sv->type.t;
1612 int bt = t & VT_BTYPE;
1613 *val = ((bt == VT_LLONG || bt == VT_PTR) ? sv->c.i :
1614 (uint32_t)sv->c.i |
1615 (t & VT_UNSIGNED ? 0 : -(sv->c.i & 0x80000000)));
1617 return 1;
1620 static int arm64_gen_opic(int op, uint32_t l, int rev, uint64_t val,
1621 uint32_t x, uint32_t a)
1623 if (op == '-' && !rev) {
1624 val = -val;
1625 op = '+';
1627 val = l ? val : (uint32_t)val;
1629 switch (op) {
1631 case '+': {
1632 uint32_t s = l ? val >> 63 : val >> 31;
1633 val = s ? -val : val;
1634 val = l ? val : (uint32_t)val;
1635 if (!(val & ~(uint64_t)0xfff))
1636 o(0x11000000 | l << 31 | s << 30 | x | a << 5 | val << 10);
1637 else if (!(val & ~(uint64_t)0xfff000))
1638 o(0x11400000 | l << 31 | s << 30 | x | a << 5 | val >> 12 << 10);
1639 else {
1640 arm64_movimm(30, val); // use x30
1641 o(0x0b1e0000 | l << 31 | s << 30 | x | a << 5);
1643 return 1;
1646 case '-':
1647 if (!val)
1648 o(0x4b0003e0 | l << 31 | x | a << 16); // neg
1649 else if (val == (l ? (uint64_t)-1 : (uint32_t)-1))
1650 o(0x2a2003e0 | l << 31 | x | a << 16); // mvn
1651 else {
1652 arm64_movimm(30, val); // use x30
1653 o(0x4b0003c0 | l << 31 | x | a << 16); // sub
1655 return 1;
1657 case '^':
1658 if (val == -1 || (val == 0xffffffff && !l)) {
1659 o(0x2a2003e0 | l << 31 | x | a << 16); // mvn
1660 return 1;
1662 // fall through
1663 case '&':
1664 case '|': {
1665 int e = arm64_encode_bimm64(l ? val : val | val << 32);
1666 if (e < 0)
1667 return 0;
1668 o((op == '&' ? 0x12000000 :
1669 op == '|' ? 0x32000000 : 0x52000000) |
1670 l << 31 | x | a << 5 | (uint32_t)e << 10);
1671 return 1;
1674 case TOK_SAR:
1675 case TOK_SHL:
1676 case TOK_SHR: {
1677 uint32_t n = 32 << l;
1678 val = val & (n - 1);
1679 if (rev)
1680 return 0;
1681 if (!val) {
1682 // tcc_warning("shift count >= width of type");
1683 o(0x2a0003e0 | l << 31 | a << 16);
1684 return 1;
1686 else if (op == TOK_SHL)
1687 o(0x53000000 | l << 31 | l << 22 | x | a << 5 |
1688 (n - val) << 16 | (n - 1 - val) << 10); // lsl
1689 else
1690 o(0x13000000 | (op == TOK_SHR) << 30 | l << 31 | l << 22 |
1691 x | a << 5 | val << 16 | (n - 1) << 10); // lsr/asr
1692 return 1;
1696 return 0;
1699 static void arm64_gen_opil(int op, uint32_t l)
1701 uint32_t x, a, b;
1703 // Special treatment for operations with a constant operand:
1705 uint64_t val;
1706 int rev = 1;
1708 if (arm64_iconst(0, &vtop[0])) {
1709 vswap();
1710 rev = 0;
1712 if (arm64_iconst(&val, &vtop[-1])) {
1713 gv(RC_INT);
1714 a = intr(vtop[0].r);
1715 --vtop;
1716 x = get_reg(RC_INT);
1717 ++vtop;
1718 if (arm64_gen_opic(op, l, rev, val, intr(x), a)) {
1719 vtop[0].r = x;
1720 vswap();
1721 --vtop;
1722 return;
1725 if (!rev)
1726 vswap();
1729 gv2(RC_INT, RC_INT);
1730 assert(vtop[-1].r < VT_CONST && vtop[0].r < VT_CONST);
1731 a = intr(vtop[-1].r);
1732 b = intr(vtop[0].r);
1733 vtop -= 2;
1734 x = get_reg(RC_INT);
1735 ++vtop;
1736 vtop[0].r = x;
1737 x = intr(x);
1739 switch (op) {
1740 case '%':
1741 // Use x30 for quotient:
1742 o(0x1ac00c00 | l << 31 | 30 | a << 5 | b << 16); // sdiv
1743 o(0x1b008000 | l << 31 | x | (uint32_t)30 << 5 |
1744 b << 16 | a << 10); // msub
1745 break;
1746 case '&':
1747 o(0x0a000000 | l << 31 | x | a << 5 | b << 16); // and
1748 break;
1749 case '*':
1750 o(0x1b007c00 | l << 31 | x | a << 5 | b << 16); // mul
1751 break;
1752 case '+':
1753 o(0x0b000000 | l << 31 | x | a << 5 | b << 16); // add
1754 break;
1755 case '-':
1756 o(0x4b000000 | l << 31 | x | a << 5 | b << 16); // sub
1757 break;
1758 case '/':
1759 o(0x1ac00c00 | l << 31 | x | a << 5 | b << 16); // sdiv
1760 break;
1761 case '^':
1762 o(0x4a000000 | l << 31 | x | a << 5 | b << 16); // eor
1763 break;
1764 case '|':
1765 o(0x2a000000 | l << 31 | x | a << 5 | b << 16); // orr
1766 break;
1767 case TOK_EQ:
1768 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1769 o(0x1a9f17e0 | x); // cset wA,eq
1770 break;
1771 case TOK_GE:
1772 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1773 o(0x1a9fb7e0 | x); // cset wA,ge
1774 break;
1775 case TOK_GT:
1776 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1777 o(0x1a9fd7e0 | x); // cset wA,gt
1778 break;
1779 case TOK_LE:
1780 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1781 o(0x1a9fc7e0 | x); // cset wA,le
1782 break;
1783 case TOK_LT:
1784 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1785 o(0x1a9fa7e0 | x); // cset wA,lt
1786 break;
1787 case TOK_NE:
1788 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1789 o(0x1a9f07e0 | x); // cset wA,ne
1790 break;
1791 case TOK_SAR:
1792 o(0x1ac02800 | l << 31 | x | a << 5 | b << 16); // asr
1793 break;
1794 case TOK_SHL:
1795 o(0x1ac02000 | l << 31 | x | a << 5 | b << 16); // lsl
1796 break;
1797 case TOK_SHR:
1798 o(0x1ac02400 | l << 31 | x | a << 5 | b << 16); // lsr
1799 break;
1800 case TOK_UDIV:
1801 case TOK_PDIV:
1802 o(0x1ac00800 | l << 31 | x | a << 5 | b << 16); // udiv
1803 break;
1804 case TOK_UGE:
1805 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1806 o(0x1a9f37e0 | x); // cset wA,cs
1807 break;
1808 case TOK_UGT:
1809 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1810 o(0x1a9f97e0 | x); // cset wA,hi
1811 break;
1812 case TOK_ULT:
1813 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1814 o(0x1a9f27e0 | x); // cset wA,cc
1815 break;
1816 case TOK_ULE:
1817 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1818 o(0x1a9f87e0 | x); // cset wA,ls
1819 break;
1820 case TOK_UMOD:
1821 // Use x30 for quotient:
1822 o(0x1ac00800 | l << 31 | 30 | a << 5 | b << 16); // udiv
1823 o(0x1b008000 | l << 31 | x | (uint32_t)30 << 5 |
1824 b << 16 | a << 10); // msub
1825 break;
1826 default:
1827 assert(0);
1831 ST_FUNC void gen_opi(int op)
1833 arm64_gen_opil(op, 0);
1834 arm64_vset_VT_CMP(op);
1837 ST_FUNC void gen_opl(int op)
1839 arm64_gen_opil(op, 1);
1840 arm64_vset_VT_CMP(op);
1843 ST_FUNC void gen_opf(int op)
1845 uint32_t x, a, b, dbl;
1847 if (vtop[0].type.t == VT_LDOUBLE) {
1848 CType type = vtop[0].type;
1849 int func = 0;
1850 int cond = -1;
1851 switch (op) {
1852 case '*': func = TOK___multf3; break;
1853 case '+': func = TOK___addtf3; break;
1854 case '-': func = TOK___subtf3; break;
1855 case '/': func = TOK___divtf3; break;
1856 case TOK_EQ: func = TOK___eqtf2; cond = 1; break;
1857 case TOK_NE: func = TOK___netf2; cond = 0; break;
1858 case TOK_LT: func = TOK___lttf2; cond = 10; break;
1859 case TOK_GE: func = TOK___getf2; cond = 11; break;
1860 case TOK_LE: func = TOK___letf2; cond = 12; break;
1861 case TOK_GT: func = TOK___gttf2; cond = 13; break;
1862 default: assert(0); break;
1864 vpush_helper_func(func);
1865 vrott(3);
1866 gfunc_call(2);
1867 vpushi(0);
1868 vtop->r = cond < 0 ? REG_FRET : REG_IRET;
1869 if (cond < 0)
1870 vtop->type = type;
1871 else {
1872 o(0x7100001f); // cmp w0,#0
1873 o(0x1a9f07e0 | (uint32_t)cond << 12); // cset w0,(cond)
1875 return;
1878 dbl = vtop[0].type.t != VT_FLOAT;
1879 gv2(RC_FLOAT, RC_FLOAT);
1880 assert(vtop[-1].r < VT_CONST && vtop[0].r < VT_CONST);
1881 a = fltr(vtop[-1].r);
1882 b = fltr(vtop[0].r);
1883 vtop -= 2;
1884 switch (op) {
1885 case TOK_EQ: case TOK_NE:
1886 case TOK_LT: case TOK_GE: case TOK_LE: case TOK_GT:
1887 x = get_reg(RC_INT);
1888 ++vtop;
1889 vtop[0].r = x;
1890 x = intr(x);
1891 break;
1892 default:
1893 x = get_reg(RC_FLOAT);
1894 ++vtop;
1895 vtop[0].r = x;
1896 x = fltr(x);
1897 break;
1900 switch (op) {
1901 case '*':
1902 o(0x1e200800 | dbl << 22 | x | a << 5 | b << 16); // fmul
1903 break;
1904 case '+':
1905 o(0x1e202800 | dbl << 22 | x | a << 5 | b << 16); // fadd
1906 break;
1907 case '-':
1908 o(0x1e203800 | dbl << 22 | x | a << 5 | b << 16); // fsub
1909 break;
1910 case '/':
1911 o(0x1e201800 | dbl << 22 | x | a << 5 | b << 16); // fdiv
1912 break;
1913 case TOK_EQ:
1914 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1915 o(0x1a9f17e0 | x); // cset w(x),eq
1916 break;
1917 case TOK_GE:
1918 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1919 o(0x1a9fb7e0 | x); // cset w(x),ge
1920 break;
1921 case TOK_GT:
1922 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1923 o(0x1a9fd7e0 | x); // cset w(x),gt
1924 break;
1925 case TOK_LE:
1926 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1927 o(0x1a9f87e0 | x); // cset w(x),ls
1928 break;
1929 case TOK_LT:
1930 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1931 o(0x1a9f57e0 | x); // cset w(x),mi
1932 break;
1933 case TOK_NE:
1934 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1935 o(0x1a9f07e0 | x); // cset w(x),ne
1936 break;
1937 default:
1938 assert(0);
1940 arm64_vset_VT_CMP(op);
1943 // Generate sign extension from 32 to 64 bits:
1944 ST_FUNC void gen_cvt_sxtw(void)
1946 uint32_t r = intr(gv(RC_INT));
1947 o(0x93407c00 | r | r << 5); // sxtw x(r),w(r)
1950 /* char/short to int conversion */
1951 ST_FUNC void gen_cvt_csti(int t)
1953 int r = intr(gv(RC_INT));
1954 o(0x13001c00
1955 | ((t & VT_BTYPE) == VT_SHORT) << 13
1956 | (uint32_t)!!(t & VT_UNSIGNED) << 30
1957 | r | r << 5); // [su]xt[bh] w(r),w(r)
1960 ST_FUNC void gen_cvt_itof(int t)
1962 if (t == VT_LDOUBLE) {
1963 int f = vtop->type.t;
1964 int func = (f & VT_BTYPE) == VT_LLONG ?
1965 (f & VT_UNSIGNED ? TOK___floatunditf : TOK___floatditf) :
1966 (f & VT_UNSIGNED ? TOK___floatunsitf : TOK___floatsitf);
1967 vpush_helper_func(func);
1968 vrott(2);
1969 gfunc_call(1);
1970 vpushi(0);
1971 vtop->type.t = t;
1972 vtop->r = REG_FRET;
1973 return;
1975 else {
1976 int d, n = intr(gv(RC_INT));
1977 int s = !(vtop->type.t & VT_UNSIGNED);
1978 uint32_t l = ((vtop->type.t & VT_BTYPE) == VT_LLONG);
1979 --vtop;
1980 d = get_reg(RC_FLOAT);
1981 ++vtop;
1982 vtop[0].r = d;
1983 o(0x1e220000 | (uint32_t)!s << 16 |
1984 (uint32_t)(t != VT_FLOAT) << 22 | fltr(d) |
1985 l << 31 | n << 5); // [us]cvtf [sd](d),[wx](n)
1989 ST_FUNC void gen_cvt_ftoi(int t)
1991 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
1992 int func = (t & VT_BTYPE) == VT_LLONG ?
1993 (t & VT_UNSIGNED ? TOK___fixunstfdi : TOK___fixtfdi) :
1994 (t & VT_UNSIGNED ? TOK___fixunstfsi : TOK___fixtfsi);
1995 vpush_helper_func(func);
1996 vrott(2);
1997 gfunc_call(1);
1998 vpushi(0);
1999 vtop->type.t = t;
2000 vtop->r = REG_IRET;
2001 return;
2003 else {
2004 int d, n = fltr(gv(RC_FLOAT));
2005 uint32_t l = ((vtop->type.t & VT_BTYPE) != VT_FLOAT);
2006 --vtop;
2007 d = get_reg(RC_INT);
2008 ++vtop;
2009 vtop[0].r = d;
2010 o(0x1e380000 |
2011 (uint32_t)!!(t & VT_UNSIGNED) << 16 |
2012 (uint32_t)((t & VT_BTYPE) == VT_LLONG) << 31 | intr(d) |
2013 l << 22 | n << 5); // fcvtz[su] [wx](d),[sd](n)
2017 ST_FUNC void gen_cvt_ftof(int t)
2019 int f = vtop[0].type.t & VT_BTYPE;
2020 assert(t == VT_FLOAT || t == VT_DOUBLE || t == VT_LDOUBLE);
2021 assert(f == VT_FLOAT || f == VT_DOUBLE || f == VT_LDOUBLE);
2022 if (t == f)
2023 return;
2025 if (t == VT_LDOUBLE || f == VT_LDOUBLE) {
2026 int func = (t == VT_LDOUBLE) ?
2027 (f == VT_FLOAT ? TOK___extendsftf2 : TOK___extenddftf2) :
2028 (t == VT_FLOAT ? TOK___trunctfsf2 : TOK___trunctfdf2);
2029 vpush_helper_func(func);
2030 vrott(2);
2031 gfunc_call(1);
2032 vpushi(0);
2033 vtop->type.t = t;
2034 vtop->r = REG_FRET;
2036 else {
2037 int x, a;
2038 gv(RC_FLOAT);
2039 assert(vtop[0].r < VT_CONST);
2040 a = fltr(vtop[0].r);
2041 --vtop;
2042 x = get_reg(RC_FLOAT);
2043 ++vtop;
2044 vtop[0].r = x;
2045 x = fltr(x);
2047 if (f == VT_FLOAT)
2048 o(0x1e22c000 | x | a << 5); // fcvt d(x),s(a)
2049 else
2050 o(0x1e624000 | x | a << 5); // fcvt s(x),d(a)
2054 /* increment tcov counter */
2055 ST_FUNC void gen_increment_tcov (SValue *sv)
2057 int r1, r2;
2059 vpushv(sv);
2060 vtop->r = r1 = get_reg(RC_INT);
2061 r2 = get_reg(RC_INT);
2062 greloca(cur_text_section, sv->sym, ind, R_AARCH64_ADR_GOT_PAGE, 0);
2063 o(0x90000000 | r1); // adrp r1, #sym
2064 greloca(cur_text_section, sv->sym, ind, R_AARCH64_LD64_GOT_LO12_NC, 0);
2065 o(0xf9400000 | r1 | (r1 << 5)); // ld xr,[xr, #sym]
2066 o(0xf9400000 | (intr(r1)<<5) | intr(r2)); // ldr r2, [r1]
2067 o(0x91000400 | (intr(r2)<<5) | intr(r2)); // add r2, r2, #1
2068 o(0xf9000000 | (intr(r1)<<5) | intr(r2)); // str r2, [r1]
2069 vpop();
2072 ST_FUNC void ggoto(void)
2074 arm64_gen_bl_or_b(1);
2075 --vtop;
2078 ST_FUNC void gen_clear_cache(void)
2080 uint32_t beg, end, dsz, isz, p, lab1, b1;
2081 gv2(RC_INT, RC_INT);
2082 vpushi(0);
2083 vtop->r = get_reg(RC_INT);
2084 vpushi(0);
2085 vtop->r = get_reg(RC_INT);
2086 vpushi(0);
2087 vtop->r = get_reg(RC_INT);
2088 beg = intr(vtop[-4].r); // x0
2089 end = intr(vtop[-3].r); // x1
2090 dsz = intr(vtop[-2].r); // x2
2091 isz = intr(vtop[-1].r); // x3
2092 p = intr(vtop[0].r); // x4
2093 vtop -= 5;
2095 o(0xd53b0020 | isz); // mrs x(isz),ctr_el0
2096 o(0x52800080 | p); // mov w(p),#4
2097 o(0x53104c00 | dsz | isz << 5); // ubfx w(dsz),w(isz),#16,#4
2098 o(0x1ac02000 | dsz | p << 5 | dsz << 16); // lsl w(dsz),w(p),w(dsz)
2099 o(0x12000c00 | isz | isz << 5); // and w(isz),w(isz),#15
2100 o(0x1ac02000 | isz | p << 5 | isz << 16); // lsl w(isz),w(p),w(isz)
2101 o(0x51000400 | p | dsz << 5); // sub w(p),w(dsz),#1
2102 o(0x8a240004 | p | beg << 5 | p << 16); // bic x(p),x(beg),x(p)
2103 b1 = ind; o(0x14000000); // b
2104 lab1 = ind;
2105 o(0xd50b7b20 | p); // dc cvau,x(p)
2106 o(0x8b000000 | p | p << 5 | dsz << 16); // add x(p),x(p),x(dsz)
2107 write32le(cur_text_section->data + b1, 0x14000000 | (ind - b1) >> 2);
2108 o(0xeb00001f | p << 5 | end << 16); // cmp x(p),x(end)
2109 o(0x54ffffa3 | ((lab1 - ind) << 3 & 0xffffe0)); // b.cc lab1
2110 o(0xd5033b9f); // dsb ish
2111 o(0x51000400 | p | isz << 5); // sub w(p),w(isz),#1
2112 o(0x8a240004 | p | beg << 5 | p << 16); // bic x(p),x(beg),x(p)
2113 b1 = ind; o(0x14000000); // b
2114 lab1 = ind;
2115 o(0xd50b7520 | p); // ic ivau,x(p)
2116 o(0x8b000000 | p | p << 5 | isz << 16); // add x(p),x(p),x(isz)
2117 write32le(cur_text_section->data + b1, 0x14000000 | (ind - b1) >> 2);
2118 o(0xeb00001f | p << 5 | end << 16); // cmp x(p),x(end)
2119 o(0x54ffffa3 | ((lab1 - ind) << 3 & 0xffffe0)); // b.cc lab1
2120 o(0xd5033b9f); // dsb ish
2121 o(0xd5033fdf); // isb
2124 ST_FUNC void gen_vla_sp_save(int addr) {
2125 uint32_t r = intr(get_reg(RC_INT));
2126 o(0x910003e0 | r); // mov x(r),sp
2127 arm64_strx(3, r, 29, addr);
2130 ST_FUNC void gen_vla_sp_restore(int addr) {
2131 // Use x30 because this function can be called when there
2132 // is a live return value in x0 but there is nothing on
2133 // the value stack to prevent get_reg from returning x0.
2134 uint32_t r = 30;
2135 arm64_ldrx(0, 3, r, 29, addr);
2136 o(0x9100001f | r << 5); // mov sp,x(r)
2139 ST_FUNC void gen_vla_alloc(CType *type, int align) {
2140 uint32_t r;
2141 #if defined(CONFIG_TCC_BCHECK)
2142 if (tcc_state->do_bounds_check)
2143 vpushv(vtop);
2144 #endif
2145 r = intr(gv(RC_INT));
2146 #if defined(CONFIG_TCC_BCHECK)
2147 if (tcc_state->do_bounds_check)
2148 o(0x91004000 | r | r << 5); // add x(r),x(r),#15+1
2149 else
2150 #endif
2151 o(0x91003c00 | r | r << 5); // add x(r),x(r),#15
2152 o(0x927cec00 | r | r << 5); // bic x(r),x(r),#15
2153 o(0xcb2063ff | r << 16); // sub sp,sp,x(r)
2154 vpop();
2155 #if defined(CONFIG_TCC_BCHECK)
2156 if (tcc_state->do_bounds_check) {
2157 vpushi(0);
2158 vtop->r = TREG_R(0);
2159 o(0x910003e0 | vtop->r); // mov r0,sp
2160 vswap();
2161 vpush_helper_func(TOK___bound_new_region);
2162 vrott(3);
2163 gfunc_call(2);
2164 func_bound_add_epilog = 1;
2166 #endif
2169 /* end of A64 code generator */
2170 /*************************************************************/
2171 #endif
2172 /*************************************************************/