Fix init bitfield padding with size 32/64
[tinycc.git] / arm64-gen.c
blob764493375081e51baf939e1941290392607beae3
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 #if defined(TCC_TARGET_MACHO)
57 "__aarch64__\0"
58 "__arm64__\0"
59 #else
60 "__aarch64__\0"
61 #endif
64 ST_DATA const int reg_classes[NB_REGS] = {
65 RC_INT | RC_R(0),
66 RC_INT | RC_R(1),
67 RC_INT | RC_R(2),
68 RC_INT | RC_R(3),
69 RC_INT | RC_R(4),
70 RC_INT | RC_R(5),
71 RC_INT | RC_R(6),
72 RC_INT | RC_R(7),
73 RC_INT | RC_R(8),
74 RC_INT | RC_R(9),
75 RC_INT | RC_R(10),
76 RC_INT | RC_R(11),
77 RC_INT | RC_R(12),
78 RC_INT | RC_R(13),
79 RC_INT | RC_R(14),
80 RC_INT | RC_R(15),
81 RC_INT | RC_R(16),
82 RC_INT | RC_R(17),
83 RC_INT | RC_R(18),
84 RC_R30, // not in RC_INT as we make special use of x30
85 RC_FLOAT | RC_F(0),
86 RC_FLOAT | RC_F(1),
87 RC_FLOAT | RC_F(2),
88 RC_FLOAT | RC_F(3),
89 RC_FLOAT | RC_F(4),
90 RC_FLOAT | RC_F(5),
91 RC_FLOAT | RC_F(6),
92 RC_FLOAT | RC_F(7)
95 #if defined(CONFIG_TCC_BCHECK)
96 static addr_t func_bound_offset;
97 static unsigned long func_bound_ind;
98 ST_DATA int func_bound_add_epilog;
99 #endif
101 #define IS_FREG(x) ((x) >= TREG_F(0))
103 static uint32_t intr(int r)
105 assert(TREG_R(0) <= r && r <= TREG_R30);
106 return r < TREG_R30 ? r : 30;
109 static uint32_t fltr(int r)
111 assert(TREG_F(0) <= r && r <= TREG_F(7));
112 return r - TREG_F(0);
115 // Add an instruction to text section:
116 ST_FUNC void o(unsigned int c)
118 int ind1 = ind + 4;
119 if (nocode_wanted)
120 return;
121 if (ind1 > cur_text_section->data_allocated)
122 section_realloc(cur_text_section, ind1);
123 write32le(cur_text_section->data + ind, c);
124 ind = ind1;
127 static int arm64_encode_bimm64(uint64_t x)
129 int neg = x & 1;
130 int rep, pos, len;
132 if (neg)
133 x = ~x;
134 if (!x)
135 return -1;
137 if (x >> 2 == (x & (((uint64_t)1 << (64 - 2)) - 1)))
138 rep = 2, x &= ((uint64_t)1 << 2) - 1;
139 else if (x >> 4 == (x & (((uint64_t)1 << (64 - 4)) - 1)))
140 rep = 4, x &= ((uint64_t)1 << 4) - 1;
141 else if (x >> 8 == (x & (((uint64_t)1 << (64 - 8)) - 1)))
142 rep = 8, x &= ((uint64_t)1 << 8) - 1;
143 else if (x >> 16 == (x & (((uint64_t)1 << (64 - 16)) - 1)))
144 rep = 16, x &= ((uint64_t)1 << 16) - 1;
145 else if (x >> 32 == (x & (((uint64_t)1 << (64 - 32)) - 1)))
146 rep = 32, x &= ((uint64_t)1 << 32) - 1;
147 else
148 rep = 64;
150 pos = 0;
151 if (!(x & (((uint64_t)1 << 32) - 1))) x >>= 32, pos += 32;
152 if (!(x & (((uint64_t)1 << 16) - 1))) x >>= 16, pos += 16;
153 if (!(x & (((uint64_t)1 << 8) - 1))) x >>= 8, pos += 8;
154 if (!(x & (((uint64_t)1 << 4) - 1))) x >>= 4, pos += 4;
155 if (!(x & (((uint64_t)1 << 2) - 1))) x >>= 2, pos += 2;
156 if (!(x & (((uint64_t)1 << 1) - 1))) x >>= 1, pos += 1;
158 len = 0;
159 if (!(~x & (((uint64_t)1 << 32) - 1))) x >>= 32, len += 32;
160 if (!(~x & (((uint64_t)1 << 16) - 1))) x >>= 16, len += 16;
161 if (!(~x & (((uint64_t)1 << 8) - 1))) x >>= 8, len += 8;
162 if (!(~x & (((uint64_t)1 << 4) - 1))) x >>= 4, len += 4;
163 if (!(~x & (((uint64_t)1 << 2) - 1))) x >>= 2, len += 2;
164 if (!(~x & (((uint64_t)1 << 1) - 1))) x >>= 1, len += 1;
166 if (x)
167 return -1;
168 if (neg) {
169 pos = (pos + len) & (rep - 1);
170 len = rep - len;
172 return ((0x1000 & rep << 6) | (((rep - 1) ^ 31) << 1 & 63) |
173 ((rep - pos) & (rep - 1)) << 6 | (len - 1));
176 static uint32_t arm64_movi(int r, uint64_t x)
178 uint64_t m = 0xffff;
179 int e;
180 if (!(x & ~m))
181 return 0x52800000 | r | x << 5; // movz w(r),#(x)
182 if (!(x & ~(m << 16)))
183 return 0x52a00000 | r | x >> 11; // movz w(r),#(x >> 16),lsl #16
184 if (!(x & ~(m << 32)))
185 return 0xd2c00000 | r | x >> 27; // movz x(r),#(x >> 32),lsl #32
186 if (!(x & ~(m << 48)))
187 return 0xd2e00000 | r | x >> 43; // movz x(r),#(x >> 48),lsl #48
188 if ((x & ~m) == m << 16)
189 return (0x12800000 | r |
190 (~x << 5 & 0x1fffe0)); // movn w(r),#(~x)
191 if ((x & ~(m << 16)) == m)
192 return (0x12a00000 | r |
193 (~x >> 11 & 0x1fffe0)); // movn w(r),#(~x >> 16),lsl #16
194 if (!~(x | m))
195 return (0x92800000 | r |
196 (~x << 5 & 0x1fffe0)); // movn x(r),#(~x)
197 if (!~(x | m << 16))
198 return (0x92a00000 | r |
199 (~x >> 11 & 0x1fffe0)); // movn x(r),#(~x >> 16),lsl #16
200 if (!~(x | m << 32))
201 return (0x92c00000 | r |
202 (~x >> 27 & 0x1fffe0)); // movn x(r),#(~x >> 32),lsl #32
203 if (!~(x | m << 48))
204 return (0x92e00000 | r |
205 (~x >> 43 & 0x1fffe0)); // movn x(r),#(~x >> 32),lsl #32
206 if (!(x >> 32) && (e = arm64_encode_bimm64(x | x << 32)) >= 0)
207 return 0x320003e0 | r | (uint32_t)e << 10; // movi w(r),#(x)
208 if ((e = arm64_encode_bimm64(x)) >= 0)
209 return 0xb20003e0 | r | (uint32_t)e << 10; // movi x(r),#(x)
210 return 0;
213 static void arm64_movimm(int r, uint64_t x)
215 uint32_t i;
216 if ((i = arm64_movi(r, x)))
217 o(i); // a single MOV
218 else {
219 // MOVZ/MOVN and 1-3 MOVKs
220 int z = 0, m = 0;
221 uint32_t mov1 = 0xd2800000; // movz
222 uint64_t x1 = x;
223 for (i = 0; i < 64; i += 16) {
224 z += !(x >> i & 0xffff);
225 m += !(~x >> i & 0xffff);
227 if (m > z) {
228 x1 = ~x;
229 mov1 = 0x92800000; // movn
231 for (i = 0; i < 64; i += 16)
232 if (x1 >> i & 0xffff) {
233 o(mov1 | r | (x1 >> i & 0xffff) << 5 | i << 17);
234 // movz/movn x(r),#(*),lsl #(i)
235 break;
237 for (i += 16; i < 64; i += 16)
238 if (x1 >> i & 0xffff)
239 o(0xf2800000 | r | (x >> i & 0xffff) << 5 | i << 17);
240 // movk x(r),#(*),lsl #(i)
244 // Patch all branches in list pointed to by t to branch to a:
245 ST_FUNC void gsym_addr(int t_, int a_)
247 uint32_t t = t_;
248 uint32_t a = a_;
249 while (t) {
250 unsigned char *ptr = cur_text_section->data + t;
251 uint32_t next = read32le(ptr);
252 if (a - t + 0x8000000 >= 0x10000000)
253 tcc_error("branch out of range");
254 write32le(ptr, (a - t == 4 ? 0xd503201f : // nop
255 0x14000000 | ((a - t) >> 2 & 0x3ffffff))); // b
256 t = next;
260 static int arm64_type_size(int t)
263 * case values are in increasing order (from 1 to 11).
264 * which 'may' help compiler optimizers. See tcc.h
266 switch (t & VT_BTYPE) {
267 case VT_BYTE: return 0;
268 case VT_SHORT: return 1;
269 case VT_INT: return 2;
270 case VT_LLONG: return 3;
271 case VT_PTR: return 3;
272 case VT_FUNC: return 3;
273 case VT_STRUCT: return 3;
274 case VT_FLOAT: return 2;
275 case VT_DOUBLE: return 3;
276 case VT_LDOUBLE: return 4;
277 case VT_BOOL: return 0;
279 assert(0);
280 return 0;
283 static void arm64_spoff(int reg, uint64_t off)
285 uint32_t sub = off >> 63;
286 if (sub)
287 off = -off;
288 if (off < 4096)
289 o(0x910003e0 | sub << 30 | reg | off << 10);
290 // (add|sub) x(reg),sp,#(off)
291 else {
292 arm64_movimm(30, off); // use x30 for offset
293 o(0x8b3e63e0 | sub << 30 | reg); // (add|sub) x(reg),sp,x30
297 /* invert 0: return value to use for store/load */
298 /* invert 1: return value to use for arm64_sym */
299 static uint64_t arm64_check_offset(int invert, int sz_, uint64_t off)
301 uint32_t sz = sz_;
302 if (!(off & ~((uint32_t)0xfff << sz)) ||
303 (off < 256 || -off <= 256))
304 return invert ? off : 0ul;
305 else if ((off & ((uint32_t)0xfff << sz)))
306 return invert ? off & ((uint32_t)0xfff << sz)
307 : off & ~((uint32_t)0xfff << sz);
308 else if (off & 0x1ff)
309 return invert ? off & 0x1ff : off & ~0x1ff;
310 else
311 return invert ? 0ul : off;
314 static void arm64_ldrx(int sg, int sz_, int dst, int bas, uint64_t off)
316 uint32_t sz = sz_;
317 if (sz >= 2)
318 sg = 0;
319 if (!(off & ~((uint32_t)0xfff << sz)))
320 o(0x39400000 | dst | bas << 5 | off << (10 - sz) |
321 (uint32_t)!!sg << 23 | sz << 30); // ldr(*) x(dst),[x(bas),#(off)]
322 else if (off < 256 || -off <= 256)
323 o(0x38400000 | dst | bas << 5 | (off & 511) << 12 |
324 (uint32_t)!!sg << 23 | sz << 30); // ldur(*) x(dst),[x(bas),#(off)]
325 else {
326 arm64_movimm(30, off); // use x30 for offset
327 o(0x38206800 | dst | bas << 5 | (uint32_t)30 << 16 |
328 (uint32_t)(!!sg + 1) << 22 | sz << 30); // ldr(*) x(dst),[x(bas),x30]
332 static void arm64_ldrv(int sz_, int dst, int bas, uint64_t off)
334 uint32_t sz = sz_;
335 if (!(off & ~((uint32_t)0xfff << sz)))
336 o(0x3d400000 | dst | bas << 5 | off << (10 - sz) |
337 (sz & 4) << 21 | (sz & 3) << 30); // ldr (s|d|q)(dst),[x(bas),#(off)]
338 else if (off < 256 || -off <= 256)
339 o(0x3c400000 | dst | bas << 5 | (off & 511) << 12 |
340 (sz & 4) << 21 | (sz & 3) << 30); // ldur (s|d|q)(dst),[x(bas),#(off)]
341 else {
342 arm64_movimm(30, off); // use x30 for offset
343 o(0x3c606800 | dst | bas << 5 | (uint32_t)30 << 16 |
344 sz << 30 | (sz & 4) << 21); // ldr (s|d|q)(dst),[x(bas),x30]
348 static void arm64_ldrs(int reg_, int size)
350 uint32_t reg = reg_;
351 // Use x30 for intermediate value in some cases.
352 switch (size) {
353 default: assert(0); break;
354 case 0:
355 /* Can happen with zero size structs */
356 break;
357 case 1:
358 arm64_ldrx(0, 0, reg, reg, 0);
359 break;
360 case 2:
361 arm64_ldrx(0, 1, reg, reg, 0);
362 break;
363 case 3:
364 arm64_ldrx(0, 1, 30, reg, 0);
365 arm64_ldrx(0, 0, reg, reg, 2);
366 o(0x2a0043c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #16
367 break;
368 case 4:
369 arm64_ldrx(0, 2, reg, reg, 0);
370 break;
371 case 5:
372 arm64_ldrx(0, 2, 30, reg, 0);
373 arm64_ldrx(0, 0, reg, reg, 4);
374 o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32
375 break;
376 case 6:
377 arm64_ldrx(0, 2, 30, reg, 0);
378 arm64_ldrx(0, 1, reg, reg, 4);
379 o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32
380 break;
381 case 7:
382 arm64_ldrx(0, 2, 30, reg, 0);
383 arm64_ldrx(0, 2, reg, reg, 3);
384 o(0x53087c00 | reg | reg << 5); // lsr w(reg), w(reg), #8
385 o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32
386 break;
387 case 8:
388 arm64_ldrx(0, 3, reg, reg, 0);
389 break;
390 case 9:
391 arm64_ldrx(0, 0, reg + 1, reg, 8);
392 arm64_ldrx(0, 3, reg, reg, 0);
393 break;
394 case 10:
395 arm64_ldrx(0, 1, reg + 1, reg, 8);
396 arm64_ldrx(0, 3, reg, reg, 0);
397 break;
398 case 11:
399 arm64_ldrx(0, 2, reg + 1, reg, 7);
400 o(0x53087c00 | (reg+1) | (reg+1) << 5); // lsr w(reg+1), w(reg+1), #8
401 arm64_ldrx(0, 3, reg, reg, 0);
402 break;
403 case 12:
404 arm64_ldrx(0, 2, reg + 1, reg, 8);
405 arm64_ldrx(0, 3, reg, reg, 0);
406 break;
407 case 13:
408 arm64_ldrx(0, 3, reg + 1, reg, 5);
409 o(0xd358fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #24
410 arm64_ldrx(0, 3, reg, reg, 0);
411 break;
412 case 14:
413 arm64_ldrx(0, 3, reg + 1, reg, 6);
414 o(0xd350fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #16
415 arm64_ldrx(0, 3, reg, reg, 0);
416 break;
417 case 15:
418 arm64_ldrx(0, 3, reg + 1, reg, 7);
419 o(0xd348fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #8
420 arm64_ldrx(0, 3, reg, reg, 0);
421 break;
422 case 16:
423 o(0xa9400000 | reg | (reg+1) << 10 | reg << 5);
424 // ldp x(reg),x(reg+1),[x(reg)]
425 break;
429 static void arm64_strx(int sz_, int dst, int bas, uint64_t off)
431 uint32_t sz = sz_;
432 if (!(off & ~((uint32_t)0xfff << sz)))
433 o(0x39000000 | dst | bas << 5 | off << (10 - sz) | sz << 30);
434 // str(*) x(dst),[x(bas],#(off)]
435 else if (off < 256 || -off <= 256)
436 o(0x38000000 | dst | bas << 5 | (off & 511) << 12 | sz << 30);
437 // stur(*) x(dst),[x(bas],#(off)]
438 else {
439 arm64_movimm(30, off); // use x30 for offset
440 o(0x38206800 | dst | bas << 5 | (uint32_t)30 << 16 | sz << 30);
441 // str(*) x(dst),[x(bas),x30]
445 static void arm64_strv(int sz_, int dst, int bas, uint64_t off)
447 uint32_t sz = sz_;
448 if (!(off & ~((uint32_t)0xfff << sz)))
449 o(0x3d000000 | dst | bas << 5 | off << (10 - sz) |
450 (sz & 4) << 21 | (sz & 3) << 30); // str (s|d|q)(dst),[x(bas),#(off)]
451 else if (off < 256 || -off <= 256)
452 o(0x3c000000 | dst | bas << 5 | (off & 511) << 12 |
453 (sz & 4) << 21 | (sz & 3) << 30); // stur (s|d|q)(dst),[x(bas),#(off)]
454 else {
455 arm64_movimm(30, off); // use x30 for offset
456 o(0x3c206800 | dst | bas << 5 | (uint32_t)30 << 16 |
457 sz << 30 | (sz & 4) << 21); // str (s|d|q)(dst),[x(bas),x30]
461 static void arm64_sym(int r, Sym *sym, unsigned long addend)
463 greloca(cur_text_section, sym, ind, R_AARCH64_ADR_GOT_PAGE, 0);
464 o(0x90000000 | r); // adrp xr, #sym
465 greloca(cur_text_section, sym, ind, R_AARCH64_LD64_GOT_LO12_NC, 0);
466 o(0xf9400000 | r | (r << 5)); // ld xr,[xr, #sym]
467 if (addend) {
468 // add xr, xr, #addend
469 if (addend & 0xffful)
470 o(0x91000000 | r | r << 5 | (addend & 0xfff) << 10);
471 if (addend > 0xffful) {
472 // add xr, xr, #addend, lsl #12
473 if (addend & 0xfff000ul)
474 o(0x91400000 | r | r << 5 | ((addend >> 12) & 0xfff) << 10);
475 if (addend > 0xfffffful) {
476 /* very unlikely */
477 int t = r ? 0 : 1;
478 o(0xf81f0fe0 | t); /* str xt, [sp, #-16]! */
479 arm64_movimm(t, addend & ~0xfffffful); // use xt for addent
480 o(0x91000000 | r | (t << 5)); /* add xr, xt, #0 */
481 o(0xf84107e0 | t); /* ldr xt, [sp], #16 */
487 static void arm64_load_cmp(int r, SValue *sv);
489 ST_FUNC void load(int r, SValue *sv)
491 int svtt = sv->type.t;
492 int svr = sv->r & ~(VT_BOUNDED | VT_NONCONST);
493 int svrv = svr & VT_VALMASK;
494 uint64_t svcul = (uint32_t)sv->c.i;
495 svcul = svcul >> 31 & 1 ? svcul - ((uint64_t)1 << 32) : svcul;
497 if (svr == (VT_LOCAL | VT_LVAL)) {
498 if (IS_FREG(r))
499 arm64_ldrv(arm64_type_size(svtt), fltr(r), 29, svcul);
500 else
501 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
502 intr(r), 29, svcul);
503 return;
506 if (svr == (VT_CONST | VT_LVAL)) {
507 if (sv->sym)
508 arm64_sym(30, sv->sym, // use x30 for address
509 arm64_check_offset(0, arm64_type_size(svtt), sv->c.i));
510 else
511 arm64_movimm (30, sv->c.i);
512 if (IS_FREG(r))
513 arm64_ldrv(arm64_type_size(svtt), fltr(r), 30,
514 arm64_check_offset(1, arm64_type_size(svtt), sv->c.i));
515 else
516 arm64_ldrx(!(svtt&VT_UNSIGNED), arm64_type_size(svtt), intr(r), 30,
517 arm64_check_offset(1, arm64_type_size(svtt), sv->c.i));
518 return;
521 if ((svr & ~VT_VALMASK) == VT_LVAL && svrv < VT_CONST) {
522 if ((svtt & VT_BTYPE) != VT_VOID) {
523 if (IS_FREG(r))
524 arm64_ldrv(arm64_type_size(svtt), fltr(r), intr(svrv), 0);
525 else
526 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
527 intr(r), intr(svrv), 0);
529 return;
532 if (svr == (VT_CONST | VT_LVAL | VT_SYM)) {
533 arm64_sym(30, sv->sym, // use x30 for address
534 arm64_check_offset(0, arm64_type_size(svtt), svcul));
535 if (IS_FREG(r))
536 arm64_ldrv(arm64_type_size(svtt), fltr(r), 30,
537 arm64_check_offset(1, arm64_type_size(svtt), svcul));
538 else
539 arm64_ldrx(!(svtt&VT_UNSIGNED), arm64_type_size(svtt), intr(r), 30,
540 arm64_check_offset(1, arm64_type_size(svtt), svcul));
541 return;
544 if (svr == (VT_CONST | VT_SYM)) {
545 arm64_sym(intr(r), sv->sym, svcul);
546 return;
549 if (svr == VT_CONST) {
550 if ((svtt & VT_BTYPE) != VT_VOID)
551 arm64_movimm(intr(r), arm64_type_size(svtt) == 3 ?
552 sv->c.i : (uint32_t)svcul);
553 return;
556 if (svr < VT_CONST) {
557 if (IS_FREG(r) && IS_FREG(svr))
558 if (svtt == VT_LDOUBLE)
559 o(0x4ea01c00 | fltr(r) | fltr(svr) << 5);
560 // mov v(r).16b,v(svr).16b
561 else
562 o(0x1e604000 | fltr(r) | fltr(svr) << 5); // fmov d(r),d(svr)
563 else if (!IS_FREG(r) && !IS_FREG(svr))
564 o(0xaa0003e0 | intr(r) | intr(svr) << 16); // mov x(r),x(svr)
565 else
566 assert(0);
567 return;
570 if (svr == VT_LOCAL) {
571 if (-svcul < 0x1000)
572 o(0xd10003a0 | intr(r) | -svcul << 10); // sub x(r),x29,#...
573 else {
574 arm64_movimm(30, -svcul); // use x30 for offset
575 o(0xcb0003a0 | intr(r) | (uint32_t)30 << 16); // sub x(r),x29,x30
577 return;
580 if (svr == VT_JMP || svr == VT_JMPI) {
581 int t = (svr == VT_JMPI);
582 arm64_movimm(intr(r), t);
583 o(0x14000002); // b .+8
584 gsym(svcul);
585 arm64_movimm(intr(r), t ^ 1);
586 return;
589 if (svr == (VT_LLOCAL | VT_LVAL)) {
590 arm64_ldrx(0, 3, 30, 29, svcul); // use x30 for offset
591 if (IS_FREG(r))
592 arm64_ldrv(arm64_type_size(svtt), fltr(r), 30, 0);
593 else
594 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
595 intr(r), 30, 0);
596 return;
599 if (svr == VT_CMP) {
600 arm64_load_cmp(r, sv);
601 return;
604 printf("load(%x, (%x, %x, %lx))\n", r, svtt, sv->r, (long)svcul);
605 assert(0);
608 ST_FUNC void store(int r, SValue *sv)
610 int svtt = sv->type.t;
611 int svr = sv->r & ~VT_BOUNDED;
612 int svrv = svr & VT_VALMASK;
613 uint64_t svcul = (uint32_t)sv->c.i;
614 svcul = svcul >> 31 & 1 ? svcul - ((uint64_t)1 << 32) : svcul;
616 if (svr == (VT_LOCAL | VT_LVAL)) {
617 if (IS_FREG(r))
618 arm64_strv(arm64_type_size(svtt), fltr(r), 29, svcul);
619 else
620 arm64_strx(arm64_type_size(svtt), intr(r), 29, svcul);
621 return;
624 if (svr == (VT_CONST | VT_LVAL)) {
625 if (sv->sym)
626 arm64_sym(30, sv->sym, // use x30 for address
627 arm64_check_offset(0, arm64_type_size(svtt), sv->c.i));
628 else
629 arm64_movimm (30, sv->c.i);
630 if (IS_FREG(r))
631 arm64_strv(arm64_type_size(svtt), fltr(r), 30,
632 arm64_check_offset(1, arm64_type_size(svtt), sv->c.i));
633 else
634 arm64_strx(arm64_type_size(svtt), intr(r), 30,
635 arm64_check_offset(1, arm64_type_size(svtt), sv->c.i));
636 return;
639 if ((svr & ~VT_VALMASK) == VT_LVAL && svrv < VT_CONST) {
640 if (IS_FREG(r))
641 arm64_strv(arm64_type_size(svtt), fltr(r), intr(svrv), 0);
642 else
643 arm64_strx(arm64_type_size(svtt), intr(r), intr(svrv), 0);
644 return;
647 if (svr == (VT_CONST | VT_LVAL | VT_SYM)) {
648 arm64_sym(30, sv->sym, // use x30 for address
649 arm64_check_offset(0, arm64_type_size(svtt), svcul));
650 if (IS_FREG(r))
651 arm64_strv(arm64_type_size(svtt), fltr(r), 30,
652 arm64_check_offset(1, arm64_type_size(svtt), svcul));
653 else
654 arm64_strx(arm64_type_size(svtt), intr(r), 30,
655 arm64_check_offset(1, arm64_type_size(svtt), svcul));
656 return;
659 printf("store(%x, (%x, %x, %lx))\n", r, svtt, sv->r, (long)svcul);
660 assert(0);
663 static void arm64_gen_bl_or_b(int b)
665 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST && (vtop->r & VT_SYM)) {
666 greloca(cur_text_section, vtop->sym, ind,
667 b ? R_AARCH64_JUMP26 : R_AARCH64_CALL26, 0);
668 o(0x14000000 | (uint32_t)!b << 31); // b/bl .
670 else {
671 #ifdef CONFIG_TCC_BCHECK
672 vtop->r &= ~VT_MUSTBOUND;
673 #endif
674 o(0xd61f0000 | (uint32_t)!b << 21 | intr(gv(RC_R30)) << 5); // br/blr
678 #if defined(CONFIG_TCC_BCHECK)
680 static void gen_bounds_call(int v)
682 Sym *sym = external_helper_sym(v);
684 greloca(cur_text_section, sym, ind, R_AARCH64_CALL26, 0);
685 o(0x94000000); // bl
688 static void gen_bounds_prolog(void)
690 /* leave some room for bound checking code */
691 func_bound_offset = lbounds_section->data_offset;
692 func_bound_ind = ind;
693 func_bound_add_epilog = 0;
694 o(0xd503201f); /* nop -> mov x0, lbound section pointer */
695 o(0xd503201f);
696 o(0xd503201f);
697 o(0xd503201f); /* nop -> call __bound_local_new */
700 static void gen_bounds_epilog(void)
702 addr_t saved_ind;
703 addr_t *bounds_ptr;
704 Sym *sym_data;
705 int offset_modified = func_bound_offset != lbounds_section->data_offset;
707 if (!offset_modified && !func_bound_add_epilog)
708 return;
710 /* add end of table info */
711 bounds_ptr = section_ptr_add(lbounds_section, sizeof(addr_t));
712 *bounds_ptr = 0;
714 sym_data = get_sym_ref(&char_pointer_type, lbounds_section,
715 func_bound_offset, PTR_SIZE);
717 /* generate bound local allocation */
718 if (offset_modified) {
719 saved_ind = ind;
720 ind = func_bound_ind;
721 greloca(cur_text_section, sym_data, ind, R_AARCH64_ADR_GOT_PAGE, 0);
722 o(0x90000000 | 0); // adrp x0, #sym_data
723 greloca(cur_text_section, sym_data, ind, R_AARCH64_LD64_GOT_LO12_NC, 0);
724 o(0xf9400000 | 0 | (0 << 5)); // ld x0,[x0, #sym_data]
725 gen_bounds_call(TOK___bound_local_new);
726 ind = saved_ind;
729 /* generate bound check local freeing */
730 o(0xa9bf07e0); /* stp x0, x1, [sp, #-16]! */
731 o(0x3c9f0fe0); /* str q0, [sp, #-16]! */
732 greloca(cur_text_section, sym_data, ind, R_AARCH64_ADR_GOT_PAGE, 0);
733 o(0x90000000 | 0); // adrp x0, #sym_data
734 greloca(cur_text_section, sym_data, ind, R_AARCH64_LD64_GOT_LO12_NC, 0);
735 o(0xf9400000 | 0 | (0 << 5)); // ld x0,[x0, #sym_data]
736 gen_bounds_call(TOK___bound_local_delete);
737 o(0x3cc107e0); /* ldr q0, [sp], #16 */
738 o(0xa8c107e0); /* ldp x0, x1, [sp], #16 */
740 #endif
742 static int arm64_hfa_aux(CType *type, int *fsize, int num)
744 if (is_float(type->t)) {
745 int a, n = type_size(type, &a);
746 if (num >= 4 || (*fsize && *fsize != n))
747 return -1;
748 *fsize = n;
749 return num + 1;
751 else if ((type->t & VT_BTYPE) == VT_STRUCT) {
752 int is_struct = 0; // rather than union
753 Sym *field;
754 for (field = type->ref->next; field; field = field->next)
755 if (field->c) {
756 is_struct = 1;
757 break;
759 if (is_struct) {
760 int num0 = num;
761 for (field = type->ref->next; field; field = field->next) {
762 if (field->c != (num - num0) * *fsize)
763 return -1;
764 num = arm64_hfa_aux(&field->type, fsize, num);
765 if (num == -1)
766 return -1;
768 if (type->ref->c != (num - num0) * *fsize)
769 return -1;
770 return num;
772 else { // union
773 int num0 = num;
774 for (field = type->ref->next; field; field = field->next) {
775 int num1 = arm64_hfa_aux(&field->type, fsize, num0);
776 if (num1 == -1)
777 return -1;
778 num = num1 < num ? num : num1;
780 if (type->ref->c != (num - num0) * *fsize)
781 return -1;
782 return num;
785 else if ((type->t & VT_ARRAY) && ((type->t & VT_BTYPE) != VT_PTR)) {
786 int num1;
787 if (!type->ref->c)
788 return num;
789 num1 = arm64_hfa_aux(&type->ref->type, fsize, num);
790 if (num1 == -1 || (num1 != num && type->ref->c > 4))
791 return -1;
792 num1 = num + type->ref->c * (num1 - num);
793 if (num1 > 4)
794 return -1;
795 return num1;
797 return -1;
800 static int arm64_hfa(CType *type, unsigned *fsize)
802 if ((type->t & VT_BTYPE) == VT_STRUCT ||
803 ((type->t & VT_ARRAY) && ((type->t & VT_BTYPE) != VT_PTR))) {
804 int sz = 0;
805 int n = arm64_hfa_aux(type, &sz, 0);
806 if (0 < n && n <= 4) {
807 if (fsize)
808 *fsize = sz;
809 return n;
812 return 0;
815 static unsigned long arm64_pcs_aux(int variadic, int n, CType **type, unsigned long *a)
817 int nx = 0; // next integer register
818 int nv = 0; // next vector register
819 unsigned long ns = 32; // next stack offset
820 int i;
822 for (i = 0; i < n; i++) {
823 int hfa = arm64_hfa(type[i], 0);
824 int size, align;
826 if ((type[i]->t & VT_ARRAY) ||
827 (type[i]->t & VT_BTYPE) == VT_FUNC)
828 size = align = 8;
829 else
830 size = type_size(type[i], &align);
832 #if defined(TCC_TARGET_MACHO)
833 if (variadic && i == variadic) {
834 nx = 8;
835 nv = 8;
837 #endif
838 if (hfa)
839 // B.2
841 else if (size > 16) {
842 // B.3: replace with pointer
843 if (nx < 8)
844 a[i] = nx++ << 1 | 1;
845 else {
846 ns = (ns + 7) & ~7;
847 a[i] = ns | 1;
848 ns += 8;
850 continue;
852 else if ((type[i]->t & VT_BTYPE) == VT_STRUCT)
853 // B.4
854 size = (size + 7) & ~7;
856 // C.1
857 if (is_float(type[i]->t) && nv < 8) {
858 a[i] = 16 + (nv++ << 1);
859 continue;
862 // C.2
863 if (hfa && nv + hfa <= 8) {
864 a[i] = 16 + (nv << 1);
865 nv += hfa;
866 continue;
869 // C.3
870 if (hfa) {
871 nv = 8;
872 size = (size + 7) & ~7;
875 // C.4
876 if (hfa || (type[i]->t & VT_BTYPE) == VT_LDOUBLE) {
877 ns = (ns + 7) & ~7;
878 ns = (ns + align - 1) & -align;
881 // C.5
882 if ((type[i]->t & VT_BTYPE) == VT_FLOAT)
883 size = 8;
885 // C.6
886 if (hfa || is_float(type[i]->t)) {
887 a[i] = ns;
888 ns += size;
889 continue;
892 // C.7
893 if ((type[i]->t & VT_BTYPE) != VT_STRUCT && size <= 8 && nx < 8) {
894 a[i] = nx++ << 1;
895 continue;
898 // C.8
899 if (align == 16)
900 nx = (nx + 1) & ~1;
902 // C.9
903 if ((type[i]->t & VT_BTYPE) != VT_STRUCT && size == 16 && nx < 7) {
904 a[i] = nx << 1;
905 nx += 2;
906 continue;
909 // C.10
910 if ((type[i]->t & VT_BTYPE) == VT_STRUCT && size <= (8 - nx) * 8) {
911 a[i] = nx << 1;
912 nx += (size + 7) >> 3;
913 continue;
916 // C.11
917 nx = 8;
919 // C.12
920 ns = (ns + 7) & ~7;
921 ns = (ns + align - 1) & -align;
923 // C.13
924 if ((type[i]->t & VT_BTYPE) == VT_STRUCT) {
925 a[i] = ns;
926 ns += size;
927 continue;
930 // C.14
931 if (size < 8)
932 size = 8;
934 // C.15
935 a[i] = ns;
936 ns += size;
939 return ns - 32;
942 static unsigned long arm64_pcs(int variadic, int n, CType **type, unsigned long *a)
944 unsigned long stack;
946 // Return type:
947 if ((type[0]->t & VT_BTYPE) == VT_VOID)
948 a[0] = -1;
949 else {
950 arm64_pcs_aux(0, 1, type, a);
951 assert(a[0] == 0 || a[0] == 1 || a[0] == 16);
954 // Argument types:
955 stack = arm64_pcs_aux(variadic, n, type + 1, a + 1);
957 if (0) {
958 int i;
959 for (i = 0; i <= n; i++) {
960 if (!i)
961 printf("arm64_pcs return: ");
962 else
963 printf("arm64_pcs arg %d: ", i);
964 if (a[i] == (unsigned long)-1)
965 printf("void\n");
966 else if (a[i] == 1 && !i)
967 printf("X8 pointer\n");
968 else if (a[i] < 16)
969 printf("X%lu%s\n", a[i] / 2, a[i] & 1 ? " pointer" : "");
970 else if (a[i] < 32)
971 printf("V%lu\n", a[i] / 2 - 8);
972 else
973 printf("stack %lu%s\n",
974 (a[i] - 32) & ~1, a[i] & 1 ? " pointer" : "");
978 return stack;
981 static int n_func_args(CType *type)
983 int n_args = 0;
984 Sym *arg;
986 for (arg = type->ref->next; arg; arg = arg->next)
987 n_args++;
988 return n_args;
991 ST_FUNC void gfunc_call(int nb_args)
993 CType *return_type;
994 CType **t;
995 unsigned long *a, *a1;
996 unsigned long stack;
997 int i;
998 int variadic = (vtop[-nb_args].type.ref->f.func_type == FUNC_ELLIPSIS);
999 int var_nb_arg = n_func_args(&vtop[-nb_args].type);
1001 #ifdef CONFIG_TCC_BCHECK
1002 if (tcc_state->do_bounds_check)
1003 gbound_args(nb_args);
1004 #endif
1006 return_type = &vtop[-nb_args].type.ref->type;
1007 if ((return_type->t & VT_BTYPE) == VT_STRUCT)
1008 --nb_args;
1010 t = tcc_malloc((nb_args + 1) * sizeof(*t));
1011 a = tcc_malloc((nb_args + 1) * sizeof(*a));
1012 a1 = tcc_malloc((nb_args + 1) * sizeof(*a1));
1014 t[0] = return_type;
1015 for (i = 0; i < nb_args; i++)
1016 t[nb_args - i] = &vtop[-i].type;
1018 stack = arm64_pcs(variadic ? var_nb_arg : 0, nb_args, t, a);
1020 // Allocate space for structs replaced by pointer:
1021 for (i = nb_args; i; i--)
1022 if (a[i] & 1) {
1023 SValue *arg = &vtop[i - nb_args];
1024 int align, size = type_size(&arg->type, &align);
1025 assert((arg->type.t & VT_BTYPE) == VT_STRUCT);
1026 stack = (stack + align - 1) & -align;
1027 a1[i] = stack;
1028 stack += size;
1031 stack = (stack + 15) >> 4 << 4;
1033 /* fetch cpu flag before generating any code */
1034 if ((vtop->r & VT_VALMASK) == VT_CMP)
1035 gv(RC_INT);
1037 if (stack >= 0x1000000) // 16Mb
1038 tcc_error("stack size too big %lu", stack);
1039 if (stack & 0xfff)
1040 o(0xd10003ff | (stack & 0xfff) << 10); // sub sp,sp,#(n)
1041 if (stack >> 12)
1042 o(0xd14003ff | (stack >> 12) << 10);
1044 // First pass: set all values on stack
1045 for (i = nb_args; i; i--) {
1046 vpushv(vtop - nb_args + i);
1048 if (a[i] & 1) {
1049 // struct replaced by pointer
1050 int r = get_reg(RC_INT);
1051 arm64_spoff(intr(r), a1[i]);
1052 vset(&vtop->type, r | VT_LVAL, 0);
1053 vswap();
1054 vstore();
1055 if (a[i] >= 32) {
1056 // pointer on stack
1057 r = get_reg(RC_INT);
1058 arm64_spoff(intr(r), a1[i]);
1059 arm64_strx(3, intr(r), 31, (a[i] - 32) >> 1 << 1);
1062 else if (a[i] >= 32) {
1063 // value on stack
1064 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
1065 int r = get_reg(RC_INT);
1066 arm64_spoff(intr(r), a[i] - 32);
1067 vset(&vtop->type, r | VT_LVAL, 0);
1068 vswap();
1069 vstore();
1071 else if (is_float(vtop->type.t)) {
1072 gv(RC_FLOAT);
1073 arm64_strv(arm64_type_size(vtop[0].type.t),
1074 fltr(vtop[0].r), 31, a[i] - 32);
1076 else {
1077 gv(RC_INT);
1078 arm64_strx(3, // arm64_type_size(vtop[0].type.t),
1079 intr(vtop[0].r), 31, a[i] - 32);
1083 --vtop;
1086 // Second pass: assign values to registers
1087 for (i = nb_args; i; i--, vtop--) {
1088 if (a[i] < 16 && !(a[i] & 1)) {
1089 // value in general-purpose registers
1090 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
1091 int align, size = type_size(&vtop->type, &align);
1092 if (size) {
1093 vtop->type.t = VT_PTR;
1094 gaddrof();
1095 gv(RC_R(a[i] / 2));
1096 arm64_ldrs(a[i] / 2, size);
1099 else
1100 gv(RC_R(a[i] / 2));
1102 else if (a[i] < 16)
1103 // struct replaced by pointer in register
1104 arm64_spoff(a[i] / 2, a1[i]);
1105 else if (a[i] < 32) {
1106 // value in floating-point registers
1107 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
1108 uint32_t j, sz, n = arm64_hfa(&vtop->type, &sz);
1109 vtop->type.t = VT_PTR;
1110 gaddrof();
1111 gv(RC_R30);
1112 for (j = 0; j < n; j++)
1113 o(0x3d4003c0 |
1114 (sz & 16) << 19 | -(sz & 8) << 27 | (sz & 4) << 29 |
1115 (a[i] / 2 - 8 + j) |
1116 j << 10); // ldr ([sdq])(*),[x30,#(j * sz)]
1118 else
1119 gv(RC_F(a[i] / 2 - 8));
1123 if ((return_type->t & VT_BTYPE) == VT_STRUCT) {
1124 if (a[0] == 1) {
1125 // indirect return: set x8 and discard the stack value
1126 gv(RC_R(8));
1127 --vtop;
1129 else
1130 // return in registers: keep the address for after the call
1131 vswap();
1134 save_regs(0);
1135 arm64_gen_bl_or_b(0);
1136 --vtop;
1137 if (stack & 0xfff)
1138 o(0x910003ff | (stack & 0xfff) << 10); // add sp,sp,#(n)
1139 if (stack >> 12)
1140 o(0x914003ff | (stack >> 12) << 10);
1143 int rt = return_type->t;
1144 int bt = rt & VT_BTYPE;
1145 if (bt == VT_STRUCT && !(a[0] & 1)) {
1146 // A struct was returned in registers, so write it out:
1147 gv(RC_R(8));
1148 --vtop;
1149 if (a[0] == 0) {
1150 int align, size = type_size(return_type, &align);
1151 assert(size <= 16);
1152 if (size > 8)
1153 o(0xa9000500); // stp x0,x1,[x8]
1154 else if (size)
1155 arm64_strx(size > 4 ? 3 : size > 2 ? 2 : size > 1, 0, 8, 0);
1158 else if (a[0] == 16) {
1159 uint32_t j, sz, n = arm64_hfa(return_type, &sz);
1160 for (j = 0; j < n; j++)
1161 o(0x3d000100 |
1162 (sz & 16) << 19 | -(sz & 8) << 27 | (sz & 4) << 29 |
1163 (a[i] / 2 - 8 + j) |
1164 j << 10); // str ([sdq])(*),[x8,#(j * sz)]
1169 tcc_free(a1);
1170 tcc_free(a);
1171 tcc_free(t);
1174 static unsigned long arm64_func_va_list_stack;
1175 static int arm64_func_va_list_gr_offs;
1176 static int arm64_func_va_list_vr_offs;
1177 static int arm64_func_sub_sp_offset;
1179 ST_FUNC void gfunc_prolog(Sym *func_sym)
1181 CType *func_type = &func_sym->type;
1182 int n = 0;
1183 int i = 0;
1184 Sym *sym;
1185 CType **t;
1186 unsigned long *a;
1187 int use_x8 = 0;
1188 int last_int = 0;
1189 int last_float = 0;
1190 int variadic = func_sym->type.ref->f.func_type == FUNC_ELLIPSIS;
1191 int var_nb_arg = n_func_args(&func_sym->type);
1193 func_vc = 144; // offset of where x8 is stored
1195 for (sym = func_type->ref; sym; sym = sym->next)
1196 ++n;
1197 t = n ? tcc_malloc(n * sizeof(*t)) : NULL;
1198 a = n ? tcc_malloc(n * sizeof(*a)) : NULL;
1200 for (sym = func_type->ref; sym; sym = sym->next)
1201 t[i++] = &sym->type;
1203 arm64_func_va_list_stack = arm64_pcs(variadic ? var_nb_arg : 0, n - 1, t, a);
1205 #if !defined(TCC_TARGET_MACHO)
1206 if (variadic) {
1207 use_x8 = 1;
1208 last_int = 4;
1209 last_float = 4;
1211 #endif
1212 if (a && a[0] == 1)
1213 use_x8 = 1;
1214 for (i = 1, sym = func_type->ref->next; sym; i++, sym = sym->next) {
1215 if (a[i] < 16) {
1216 int last, align, size = type_size(&sym->type, &align);
1217 last = a[i] / 4 + 1 + (size - 1) / 8;
1218 last_int = last > last_int ? last : last_int;
1220 else if (a[i] < 32) {
1221 int last, hfa = arm64_hfa(&sym->type, 0);
1222 last = a[i] / 4 - 3 + (hfa ? hfa - 1 : 0);
1223 last_float = last > last_float ? last : last_float;
1227 last_int = last_int > 4 ? 4 : last_int;
1228 last_float = last_float > 4 ? 4 : last_float;
1230 o(0xa9b27bfd); // stp x29,x30,[sp,#-224]!
1231 for (i = 0; i < last_float; i++)
1232 // stp q0,q1,[sp,#16], stp q2,q3,[sp,#48]
1233 // stp q4,q5,[sp,#80], stp q6,q7,[sp,#112]
1234 o(0xad0087e0 + i * 0x10000 + (i << 11) + (i << 1));
1235 if (use_x8)
1236 o(0xa90923e8); // stp x8,x8,[sp,#144]
1237 for (i = 0; i < last_int; i++)
1238 // stp x0,x1,[sp,#160], stp x2,x3,[sp,#176]
1239 // stp x4,x5,[sp,#192], stp x6,x7,[sp,#208]
1240 o(0xa90a07e0 + i * 0x10000 + (i << 11) + (i << 1));
1242 arm64_func_va_list_gr_offs = -64;
1243 arm64_func_va_list_vr_offs = -128;
1245 for (i = 1, sym = func_type->ref->next; sym; i++, sym = sym->next) {
1246 int off = (a[i] < 16 ? 160 + a[i] / 2 * 8 :
1247 a[i] < 32 ? 16 + (a[i] - 16) / 2 * 16 :
1248 224 + ((a[i] - 32) >> 1 << 1));
1249 sym_push(sym->v & ~SYM_FIELD, &sym->type,
1250 (a[i] & 1 ? VT_LLOCAL : VT_LOCAL) | VT_LVAL,
1251 off);
1253 if (a[i] < 16) {
1254 int align, size = type_size(&sym->type, &align);
1255 arm64_func_va_list_gr_offs = (a[i] / 2 - 7 +
1256 (!(a[i] & 1) && size > 8)) * 8;
1258 else if (a[i] < 32) {
1259 uint32_t hfa = arm64_hfa(&sym->type, 0);
1260 arm64_func_va_list_vr_offs = (a[i] / 2 - 16 +
1261 (hfa ? hfa : 1)) * 16;
1264 // HFAs of float and double need to be written differently:
1265 if (16 <= a[i] && a[i] < 32 && (sym->type.t & VT_BTYPE) == VT_STRUCT) {
1266 uint32_t j, sz, k = arm64_hfa(&sym->type, &sz);
1267 if (sz < 16)
1268 for (j = 0; j < k; j++) {
1269 o(0x3d0003e0 | -(sz & 8) << 27 | (sz & 4) << 29 |
1270 ((a[i] - 16) / 2 + j) | (off / sz + j) << 10);
1271 // str ([sdq])(*),[sp,#(j * sz)]
1276 tcc_free(a);
1277 tcc_free(t);
1279 o(0x910003fd); // mov x29,sp
1280 arm64_func_sub_sp_offset = ind;
1281 // In gfunc_epilog these will be replaced with code to decrement SP:
1282 o(0xd503201f); // nop
1283 o(0xd503201f); // nop
1284 loc = 0;
1285 #ifdef CONFIG_TCC_BCHECK
1286 if (tcc_state->do_bounds_check)
1287 gen_bounds_prolog();
1288 #endif
1291 ST_FUNC void gen_va_start(void)
1293 int r;
1294 --vtop; // we don't need the "arg"
1295 gaddrof();
1296 r = intr(gv(RC_INT));
1298 if (arm64_func_va_list_stack) {
1299 //xx could use add (immediate) here
1300 arm64_movimm(30, arm64_func_va_list_stack + 224);
1301 o(0x8b1e03be); // add x30,x29,x30
1303 else
1304 o(0x910383be); // add x30,x29,#224
1305 o(0xf900001e | r << 5); // str x30,[x(r)]
1307 #if !defined(TCC_TARGET_MACHO)
1308 if (arm64_func_va_list_gr_offs) {
1309 if (arm64_func_va_list_stack)
1310 o(0x910383be); // add x30,x29,#224
1311 o(0xf900041e | r << 5); // str x30,[x(r),#8]
1314 if (arm64_func_va_list_vr_offs) {
1315 o(0x910243be); // add x30,x29,#144
1316 o(0xf900081e | r << 5); // str x30,[x(r),#16]
1319 arm64_movimm(30, arm64_func_va_list_gr_offs);
1320 o(0xb900181e | r << 5); // str w30,[x(r),#24]
1322 arm64_movimm(30, arm64_func_va_list_vr_offs);
1323 o(0xb9001c1e | r << 5); // str w30,[x(r),#28]
1324 #endif
1326 --vtop;
1329 ST_FUNC void gen_va_arg(CType *t)
1331 int align, size = type_size(t, &align);
1332 unsigned fsize, hfa = arm64_hfa(t, &fsize);
1333 uint32_t r0, r1;
1335 if (is_float(t->t)) {
1336 hfa = 1;
1337 fsize = size;
1340 gaddrof();
1341 r0 = intr(gv(RC_INT));
1342 r1 = get_reg(RC_INT);
1343 vtop[0].r = r1 | VT_LVAL;
1344 r1 = intr(r1);
1346 if (!hfa) {
1347 uint32_t n = size > 16 ? 8 : (size + 7) & -8;
1348 #if !defined(TCC_TARGET_MACHO)
1349 o(0xb940181e | r0 << 5); // ldr w30,[x(r0),#24] // __gr_offs
1350 if (align == 16) {
1351 assert(0); // this path untested but needed for __uint128_t
1352 o(0x11003fde); // add w30,w30,#15
1353 o(0x121c6fde); // and w30,w30,#-16
1355 o(0x310003c0 | r1 | n << 10); // adds w(r1),w30,#(n)
1356 o(0x540000ad); // b.le .+20
1357 #endif
1358 o(0xf9400000 | r1 | r0 << 5); // ldr x(r1),[x(r0)] // __stack
1359 o(0x9100001e | r1 << 5 | n << 10); // add x30,x(r1),#(n)
1360 o(0xf900001e | r0 << 5); // str x30,[x(r0)] // __stack
1361 #if !defined(TCC_TARGET_MACHO)
1362 o(0x14000004); // b .+16
1363 o(0xb9001800 | r1 | r0 << 5); // str w(r1),[x(r0),#24] // __gr_offs
1364 o(0xf9400400 | r1 | r0 << 5); // ldr x(r1),[x(r0),#8] // __gr_top
1365 o(0x8b3ec000 | r1 | r1 << 5); // add x(r1),x(r1),w30,sxtw
1366 #endif
1367 if (size > 16)
1368 o(0xf9400000 | r1 | r1 << 5); // ldr x(r1),[x(r1)]
1370 else {
1371 uint32_t ssz = (size + 7) & -(uint32_t)8;
1372 #if !defined(TCC_TARGET_MACHO)
1373 uint32_t rsz = hfa << 4;
1374 uint32_t b1, b2;
1375 o(0xb9401c1e | r0 << 5); // ldr w30,[x(r0),#28] // __vr_offs
1376 o(0x310003c0 | r1 | rsz << 10); // adds w(r1),w30,#(rsz)
1377 b1 = ind; o(0x5400000d); // b.le lab1
1378 #endif
1379 o(0xf9400000 | r1 | r0 << 5); // ldr x(r1),[x(r0)] // __stack
1380 if (fsize == 16) {
1381 o(0x91003c00 | r1 | r1 << 5); // add x(r1),x(r1),#15
1382 o(0x927cec00 | r1 | r1 << 5); // and x(r1),x(r1),#-16
1384 o(0x9100001e | r1 << 5 | ssz << 10); // add x30,x(r1),#(ssz)
1385 o(0xf900001e | r0 << 5); // str x30,[x(r0)] // __stack
1386 #if !defined(TCC_TARGET_MACHO)
1387 b2 = ind; o(0x14000000); // b lab2
1388 // lab1:
1389 write32le(cur_text_section->data + b1, 0x5400000d | (ind - b1) << 3);
1390 o(0xb9001c00 | r1 | r0 << 5); // str w(r1),[x(r0),#28] // __vr_offs
1391 o(0xf9400800 | r1 | r0 << 5); // ldr x(r1),[x(r0),#16] // __vr_top
1392 if (hfa == 1 || fsize == 16)
1393 o(0x8b3ec000 | r1 | r1 << 5); // add x(r1),x(r1),w30,sxtw
1394 else {
1395 // We need to change the layout of this HFA.
1396 // Get some space on the stack using global variable "loc":
1397 loc = (loc - size) & -(uint32_t)align;
1398 o(0x8b3ec000 | 30 | r1 << 5); // add x30,x(r1),w30,sxtw
1399 arm64_movimm(r1, loc);
1400 o(0x8b0003a0 | r1 | r1 << 16); // add x(r1),x29,x(r1)
1401 o(0x4c402bdc | (uint32_t)fsize << 7 |
1402 (uint32_t)(hfa == 2) << 15 |
1403 (uint32_t)(hfa == 3) << 14); // ld1 {v28.(4s|2d),...},[x30]
1404 o(0x0d00801c | r1 << 5 | (fsize == 8) << 10 |
1405 (uint32_t)(hfa != 2) << 13 |
1406 (uint32_t)(hfa != 3) << 21); // st(hfa) {v28.(s|d),...}[0],[x(r1)]
1408 // lab2:
1409 write32le(cur_text_section->data + b2, 0x14000000 | (ind - b2) >> 2);
1410 #endif
1414 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret,
1415 int *align, int *regsize)
1417 return 0;
1420 ST_FUNC void gfunc_return(CType *func_type)
1422 CType *t = func_type;
1423 unsigned long a;
1425 arm64_pcs(0, 0, &t, &a);
1426 switch (a) {
1427 case -1:
1428 break;
1429 case 0:
1430 if ((func_type->t & VT_BTYPE) == VT_STRUCT) {
1431 int align, size = type_size(func_type, &align);
1432 gaddrof();
1433 gv(RC_R(0));
1434 arm64_ldrs(0, size);
1436 else
1437 gv(RC_IRET);
1438 break;
1439 case 1: {
1440 CType type = *func_type;
1441 mk_pointer(&type);
1442 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
1443 indir();
1444 vswap();
1445 vstore();
1446 break;
1448 case 16:
1449 if ((func_type->t & VT_BTYPE) == VT_STRUCT) {
1450 uint32_t j, sz, n = arm64_hfa(&vtop->type, &sz);
1451 gaddrof();
1452 gv(RC_R(0));
1453 for (j = 0; j < n; j++)
1454 o(0x3d400000 |
1455 (sz & 16) << 19 | -(sz & 8) << 27 | (sz & 4) << 29 |
1456 j | j << 10); // ldr ([sdq])(*),[x0,#(j * sz)]
1458 else
1459 gv(RC_FRET);
1460 break;
1461 default:
1462 assert(0);
1464 vtop--;
1467 ST_FUNC void gfunc_epilog(void)
1469 #ifdef CONFIG_TCC_BCHECK
1470 if (tcc_state->do_bounds_check)
1471 gen_bounds_epilog();
1472 #endif
1474 if (loc) {
1475 // Insert instructions to subtract size of stack frame from SP.
1476 unsigned char *ptr = cur_text_section->data + arm64_func_sub_sp_offset;
1477 uint64_t diff = (-loc + 15) & ~15;
1478 if (!(diff >> 24)) {
1479 if (diff & 0xfff) // sub sp,sp,#(diff & 0xfff)
1480 write32le(ptr, 0xd10003ff | (diff & 0xfff) << 10);
1481 if (diff >> 12) // sub sp,sp,#(diff >> 12),lsl #12
1482 write32le(ptr + 4, 0xd14003ff | (diff >> 12) << 10);
1484 else {
1485 // In this case we may subtract more than necessary,
1486 // but always less than 17/16 of what we were aiming for.
1487 int i = 0;
1488 int j = 0;
1489 while (diff >> 20) {
1490 diff = (diff + 0xffff) >> 16;
1491 ++i;
1493 while (diff >> 16) {
1494 diff = (diff + 1) >> 1;
1495 ++j;
1497 write32le(ptr, 0xd2800010 | diff << 5 | i << 21);
1498 // mov x16,#(diff),lsl #(16 * i)
1499 write32le(ptr + 4, 0xcb3063ff | j << 10);
1500 // sub sp,sp,x16,lsl #(j)
1503 o(0x910003bf); // mov sp,x29
1504 o(0xa8ce7bfd); // ldp x29,x30,[sp],#224
1506 o(0xd65f03c0); // ret
1509 ST_FUNC void gen_fill_nops(int bytes)
1511 if ((bytes & 3))
1512 tcc_error("alignment of code section not multiple of 4");
1513 while (bytes > 0) {
1514 o(0xd503201f); // nop
1515 bytes -= 4;
1519 // Generate forward branch to label:
1520 ST_FUNC int gjmp(int t)
1522 int r = ind;
1523 if (nocode_wanted)
1524 return t;
1525 o(t);
1526 return r;
1529 // Generate branch to known address:
1530 ST_FUNC void gjmp_addr(int a)
1532 assert(a - ind + 0x8000000 < 0x10000000);
1533 o(0x14000000 | ((a - ind) >> 2 & 0x3ffffff));
1536 ST_FUNC int gjmp_append(int n, int t)
1538 void *p;
1539 /* insert vtop->c jump list in t */
1540 if (n) {
1541 uint32_t n1 = n, n2;
1542 while ((n2 = read32le(p = cur_text_section->data + n1)))
1543 n1 = n2;
1544 write32le(p, t);
1545 t = n;
1547 return t;
1550 void arm64_vset_VT_CMP(int op)
1552 if (op >= TOK_ULT && op <= TOK_GT) {
1553 vtop->cmp_r = vtop->r;
1554 vset_VT_CMP(0x80);
1558 static void arm64_gen_opil(int op, uint32_t l);
1560 static void arm64_load_cmp(int r, SValue *sv)
1562 sv->r = sv->cmp_r;
1563 if (sv->c.i & 1) {
1564 vpushi(1);
1565 arm64_gen_opil('^', 0);
1567 if (r != sv->r) {
1568 load(r, sv);
1569 sv->r = r;
1573 ST_FUNC int gjmp_cond(int op, int t)
1575 int bt = vtop->type.t & VT_BTYPE;
1577 int inv = op & 1;
1578 vtop->r = vtop->cmp_r;
1580 if (bt == VT_LDOUBLE) {
1581 uint32_t a, b, f = fltr(gv(RC_FLOAT));
1582 a = get_reg(RC_INT);
1583 vpushi(0);
1584 vtop[0].r = a;
1585 b = get_reg(RC_INT);
1586 a = intr(a);
1587 b = intr(b);
1588 o(0x4e083c00 | a | f << 5); // mov x(a),v(f).d[0]
1589 o(0x4e183c00 | b | f << 5); // mov x(b),v(f).d[1]
1590 o(0xaa000400 | a | a << 5 | b << 16); // orr x(a),x(a),x(b),lsl #1
1591 o(0xb4000040 | a | !!inv << 24); // cbz/cbnz x(a),.+8
1592 --vtop;
1594 else if (bt == VT_FLOAT || bt == VT_DOUBLE) {
1595 uint32_t a = fltr(gv(RC_FLOAT));
1596 o(0x1e202008 | a << 5 | (bt != VT_FLOAT) << 22); // fcmp
1597 o(0x54000040 | !!inv); // b.eq/b.ne .+8
1599 else {
1600 uint32_t ll = (bt == VT_PTR || bt == VT_LLONG);
1601 uint32_t a = intr(gv(RC_INT));
1602 o(0x34000040 | a | !!inv << 24 | ll << 31); // cbz/cbnz wA,.+8
1604 return gjmp(t);
1607 static int arm64_iconst(uint64_t *val, SValue *sv)
1609 if ((sv->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
1610 return 0;
1611 if (val) {
1612 int t = sv->type.t;
1613 int bt = t & VT_BTYPE;
1614 *val = ((bt == VT_LLONG || bt == VT_PTR) ? sv->c.i :
1615 (uint32_t)sv->c.i |
1616 (t & VT_UNSIGNED ? 0 : -(sv->c.i & 0x80000000)));
1618 return 1;
1621 static int arm64_gen_opic(int op, uint32_t l, int rev, uint64_t val,
1622 uint32_t x, uint32_t a)
1624 if (op == '-' && !rev) {
1625 val = -val;
1626 op = '+';
1628 val = l ? val : (uint32_t)val;
1630 switch (op) {
1632 case '+': {
1633 uint32_t s = l ? val >> 63 : val >> 31;
1634 val = s ? -val : val;
1635 val = l ? val : (uint32_t)val;
1636 if (!(val & ~(uint64_t)0xfff))
1637 o(0x11000000 | l << 31 | s << 30 | x | a << 5 | val << 10);
1638 else if (!(val & ~(uint64_t)0xfff000))
1639 o(0x11400000 | l << 31 | s << 30 | x | a << 5 | val >> 12 << 10);
1640 else {
1641 arm64_movimm(30, val); // use x30
1642 o(0x0b1e0000 | l << 31 | s << 30 | x | a << 5);
1644 return 1;
1647 case '-':
1648 if (!val)
1649 o(0x4b0003e0 | l << 31 | x | a << 16); // neg
1650 else if (val == (l ? (uint64_t)-1 : (uint32_t)-1))
1651 o(0x2a2003e0 | l << 31 | x | a << 16); // mvn
1652 else {
1653 arm64_movimm(30, val); // use x30
1654 o(0x4b0003c0 | l << 31 | x | a << 16); // sub
1656 return 1;
1658 case '^':
1659 if (val == -1 || (val == 0xffffffff && !l)) {
1660 o(0x2a2003e0 | l << 31 | x | a << 16); // mvn
1661 return 1;
1663 // fall through
1664 case '&':
1665 case '|': {
1666 int e = arm64_encode_bimm64(l ? val : val | val << 32);
1667 if (e < 0)
1668 return 0;
1669 o((op == '&' ? 0x12000000 :
1670 op == '|' ? 0x32000000 : 0x52000000) |
1671 l << 31 | x | a << 5 | (uint32_t)e << 10);
1672 return 1;
1675 case TOK_SAR:
1676 case TOK_SHL:
1677 case TOK_SHR: {
1678 uint32_t n = 32 << l;
1679 val = val & (n - 1);
1680 if (rev)
1681 return 0;
1682 if (!val) {
1683 // tcc_warning("shift count >= width of type");
1684 o(0x2a0003e0 | l << 31 | a << 16);
1685 return 1;
1687 else if (op == TOK_SHL)
1688 o(0x53000000 | l << 31 | l << 22 | x | a << 5 |
1689 (n - val) << 16 | (n - 1 - val) << 10); // lsl
1690 else
1691 o(0x13000000 | (op == TOK_SHR) << 30 | l << 31 | l << 22 |
1692 x | a << 5 | val << 16 | (n - 1) << 10); // lsr/asr
1693 return 1;
1697 return 0;
1700 static void arm64_gen_opil(int op, uint32_t l)
1702 uint32_t x, a, b;
1704 // Special treatment for operations with a constant operand:
1706 uint64_t val;
1707 int rev = 1;
1709 if (arm64_iconst(0, &vtop[0])) {
1710 vswap();
1711 rev = 0;
1713 if (arm64_iconst(&val, &vtop[-1])) {
1714 gv(RC_INT);
1715 a = intr(vtop[0].r);
1716 --vtop;
1717 x = get_reg(RC_INT);
1718 ++vtop;
1719 if (arm64_gen_opic(op, l, rev, val, intr(x), a)) {
1720 vtop[0].r = x;
1721 vswap();
1722 --vtop;
1723 return;
1726 if (!rev)
1727 vswap();
1730 gv2(RC_INT, RC_INT);
1731 assert(vtop[-1].r < VT_CONST && vtop[0].r < VT_CONST);
1732 a = intr(vtop[-1].r);
1733 b = intr(vtop[0].r);
1734 vtop -= 2;
1735 x = get_reg(RC_INT);
1736 ++vtop;
1737 vtop[0].r = x;
1738 x = intr(x);
1740 switch (op) {
1741 case '%':
1742 // Use x30 for quotient:
1743 o(0x1ac00c00 | l << 31 | 30 | a << 5 | b << 16); // sdiv
1744 o(0x1b008000 | l << 31 | x | (uint32_t)30 << 5 |
1745 b << 16 | a << 10); // msub
1746 break;
1747 case '&':
1748 o(0x0a000000 | l << 31 | x | a << 5 | b << 16); // and
1749 break;
1750 case '*':
1751 o(0x1b007c00 | l << 31 | x | a << 5 | b << 16); // mul
1752 break;
1753 case '+':
1754 o(0x0b000000 | l << 31 | x | a << 5 | b << 16); // add
1755 break;
1756 case '-':
1757 o(0x4b000000 | l << 31 | x | a << 5 | b << 16); // sub
1758 break;
1759 case '/':
1760 o(0x1ac00c00 | l << 31 | x | a << 5 | b << 16); // sdiv
1761 break;
1762 case '^':
1763 o(0x4a000000 | l << 31 | x | a << 5 | b << 16); // eor
1764 break;
1765 case '|':
1766 o(0x2a000000 | l << 31 | x | a << 5 | b << 16); // orr
1767 break;
1768 case TOK_EQ:
1769 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1770 o(0x1a9f17e0 | x); // cset wA,eq
1771 break;
1772 case TOK_GE:
1773 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1774 o(0x1a9fb7e0 | x); // cset wA,ge
1775 break;
1776 case TOK_GT:
1777 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1778 o(0x1a9fd7e0 | x); // cset wA,gt
1779 break;
1780 case TOK_LE:
1781 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1782 o(0x1a9fc7e0 | x); // cset wA,le
1783 break;
1784 case TOK_LT:
1785 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1786 o(0x1a9fa7e0 | x); // cset wA,lt
1787 break;
1788 case TOK_NE:
1789 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1790 o(0x1a9f07e0 | x); // cset wA,ne
1791 break;
1792 case TOK_SAR:
1793 o(0x1ac02800 | l << 31 | x | a << 5 | b << 16); // asr
1794 break;
1795 case TOK_SHL:
1796 o(0x1ac02000 | l << 31 | x | a << 5 | b << 16); // lsl
1797 break;
1798 case TOK_SHR:
1799 o(0x1ac02400 | l << 31 | x | a << 5 | b << 16); // lsr
1800 break;
1801 case TOK_UDIV:
1802 case TOK_PDIV:
1803 o(0x1ac00800 | l << 31 | x | a << 5 | b << 16); // udiv
1804 break;
1805 case TOK_UGE:
1806 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1807 o(0x1a9f37e0 | x); // cset wA,cs
1808 break;
1809 case TOK_UGT:
1810 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1811 o(0x1a9f97e0 | x); // cset wA,hi
1812 break;
1813 case TOK_ULT:
1814 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1815 o(0x1a9f27e0 | x); // cset wA,cc
1816 break;
1817 case TOK_ULE:
1818 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1819 o(0x1a9f87e0 | x); // cset wA,ls
1820 break;
1821 case TOK_UMOD:
1822 // Use x30 for quotient:
1823 o(0x1ac00800 | l << 31 | 30 | a << 5 | b << 16); // udiv
1824 o(0x1b008000 | l << 31 | x | (uint32_t)30 << 5 |
1825 b << 16 | a << 10); // msub
1826 break;
1827 default:
1828 assert(0);
1832 ST_FUNC void gen_opi(int op)
1834 arm64_gen_opil(op, 0);
1835 arm64_vset_VT_CMP(op);
1838 ST_FUNC void gen_opl(int op)
1840 arm64_gen_opil(op, 1);
1841 arm64_vset_VT_CMP(op);
1844 ST_FUNC void gen_opf(int op)
1846 uint32_t x, a, b, dbl;
1848 if (vtop[0].type.t == VT_LDOUBLE) {
1849 CType type = vtop[0].type;
1850 int func = 0;
1851 int cond = -1;
1852 switch (op) {
1853 case '*': func = TOK___multf3; break;
1854 case '+': func = TOK___addtf3; break;
1855 case '-': func = TOK___subtf3; break;
1856 case '/': func = TOK___divtf3; break;
1857 case TOK_EQ: func = TOK___eqtf2; cond = 1; break;
1858 case TOK_NE: func = TOK___netf2; cond = 0; break;
1859 case TOK_LT: func = TOK___lttf2; cond = 10; break;
1860 case TOK_GE: func = TOK___getf2; cond = 11; break;
1861 case TOK_LE: func = TOK___letf2; cond = 12; break;
1862 case TOK_GT: func = TOK___gttf2; cond = 13; break;
1863 default: assert(0); break;
1865 vpush_helper_func(func);
1866 vrott(3);
1867 gfunc_call(2);
1868 vpushi(0);
1869 vtop->r = cond < 0 ? REG_FRET : REG_IRET;
1870 if (cond < 0)
1871 vtop->type = type;
1872 else {
1873 o(0x7100001f); // cmp w0,#0
1874 o(0x1a9f07e0 | (uint32_t)cond << 12); // cset w0,(cond)
1876 return;
1879 dbl = vtop[0].type.t != VT_FLOAT;
1880 gv2(RC_FLOAT, RC_FLOAT);
1881 assert(vtop[-1].r < VT_CONST && vtop[0].r < VT_CONST);
1882 a = fltr(vtop[-1].r);
1883 b = fltr(vtop[0].r);
1884 vtop -= 2;
1885 switch (op) {
1886 case TOK_EQ: case TOK_NE:
1887 case TOK_LT: case TOK_GE: case TOK_LE: case TOK_GT:
1888 x = get_reg(RC_INT);
1889 ++vtop;
1890 vtop[0].r = x;
1891 x = intr(x);
1892 break;
1893 default:
1894 x = get_reg(RC_FLOAT);
1895 ++vtop;
1896 vtop[0].r = x;
1897 x = fltr(x);
1898 break;
1901 switch (op) {
1902 case '*':
1903 o(0x1e200800 | dbl << 22 | x | a << 5 | b << 16); // fmul
1904 break;
1905 case '+':
1906 o(0x1e202800 | dbl << 22 | x | a << 5 | b << 16); // fadd
1907 break;
1908 case '-':
1909 o(0x1e203800 | dbl << 22 | x | a << 5 | b << 16); // fsub
1910 break;
1911 case '/':
1912 o(0x1e201800 | dbl << 22 | x | a << 5 | b << 16); // fdiv
1913 break;
1914 case TOK_EQ:
1915 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1916 o(0x1a9f17e0 | x); // cset w(x),eq
1917 break;
1918 case TOK_GE:
1919 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1920 o(0x1a9fb7e0 | x); // cset w(x),ge
1921 break;
1922 case TOK_GT:
1923 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1924 o(0x1a9fd7e0 | x); // cset w(x),gt
1925 break;
1926 case TOK_LE:
1927 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1928 o(0x1a9f87e0 | x); // cset w(x),ls
1929 break;
1930 case TOK_LT:
1931 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1932 o(0x1a9f57e0 | x); // cset w(x),mi
1933 break;
1934 case TOK_NE:
1935 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1936 o(0x1a9f07e0 | x); // cset w(x),ne
1937 break;
1938 default:
1939 assert(0);
1941 arm64_vset_VT_CMP(op);
1944 // Generate sign extension from 32 to 64 bits:
1945 ST_FUNC void gen_cvt_sxtw(void)
1947 uint32_t r = intr(gv(RC_INT));
1948 o(0x93407c00 | r | r << 5); // sxtw x(r),w(r)
1951 /* char/short to int conversion */
1952 ST_FUNC void gen_cvt_csti(int t)
1954 int r = intr(gv(RC_INT));
1955 o(0x13001c00
1956 | ((t & VT_BTYPE) == VT_SHORT) << 13
1957 | (uint32_t)!!(t & VT_UNSIGNED) << 30
1958 | r | r << 5); // [su]xt[bh] w(r),w(r)
1961 ST_FUNC void gen_cvt_itof(int t)
1963 if (t == VT_LDOUBLE) {
1964 int f = vtop->type.t;
1965 int func = (f & VT_BTYPE) == VT_LLONG ?
1966 (f & VT_UNSIGNED ? TOK___floatunditf : TOK___floatditf) :
1967 (f & VT_UNSIGNED ? TOK___floatunsitf : TOK___floatsitf);
1968 vpush_helper_func(func);
1969 vrott(2);
1970 gfunc_call(1);
1971 vpushi(0);
1972 vtop->type.t = t;
1973 vtop->r = REG_FRET;
1974 return;
1976 else {
1977 int d, n = intr(gv(RC_INT));
1978 int s = !(vtop->type.t & VT_UNSIGNED);
1979 uint32_t l = ((vtop->type.t & VT_BTYPE) == VT_LLONG);
1980 --vtop;
1981 d = get_reg(RC_FLOAT);
1982 ++vtop;
1983 vtop[0].r = d;
1984 o(0x1e220000 | (uint32_t)!s << 16 |
1985 (uint32_t)(t != VT_FLOAT) << 22 | fltr(d) |
1986 l << 31 | n << 5); // [us]cvtf [sd](d),[wx](n)
1990 ST_FUNC void gen_cvt_ftoi(int t)
1992 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
1993 int func = (t & VT_BTYPE) == VT_LLONG ?
1994 (t & VT_UNSIGNED ? TOK___fixunstfdi : TOK___fixtfdi) :
1995 (t & VT_UNSIGNED ? TOK___fixunstfsi : TOK___fixtfsi);
1996 vpush_helper_func(func);
1997 vrott(2);
1998 gfunc_call(1);
1999 vpushi(0);
2000 vtop->type.t = t;
2001 vtop->r = REG_IRET;
2002 return;
2004 else {
2005 int d, n = fltr(gv(RC_FLOAT));
2006 uint32_t l = ((vtop->type.t & VT_BTYPE) != VT_FLOAT);
2007 --vtop;
2008 d = get_reg(RC_INT);
2009 ++vtop;
2010 vtop[0].r = d;
2011 o(0x1e380000 |
2012 (uint32_t)!!(t & VT_UNSIGNED) << 16 |
2013 (uint32_t)((t & VT_BTYPE) == VT_LLONG) << 31 | intr(d) |
2014 l << 22 | n << 5); // fcvtz[su] [wx](d),[sd](n)
2018 ST_FUNC void gen_cvt_ftof(int t)
2020 int f = vtop[0].type.t & VT_BTYPE;
2021 assert(t == VT_FLOAT || t == VT_DOUBLE || t == VT_LDOUBLE);
2022 assert(f == VT_FLOAT || f == VT_DOUBLE || f == VT_LDOUBLE);
2023 if (t == f)
2024 return;
2026 if (t == VT_LDOUBLE || f == VT_LDOUBLE) {
2027 int func = (t == VT_LDOUBLE) ?
2028 (f == VT_FLOAT ? TOK___extendsftf2 : TOK___extenddftf2) :
2029 (t == VT_FLOAT ? TOK___trunctfsf2 : TOK___trunctfdf2);
2030 vpush_helper_func(func);
2031 vrott(2);
2032 gfunc_call(1);
2033 vpushi(0);
2034 vtop->type.t = t;
2035 vtop->r = REG_FRET;
2037 else {
2038 int x, a;
2039 gv(RC_FLOAT);
2040 assert(vtop[0].r < VT_CONST);
2041 a = fltr(vtop[0].r);
2042 --vtop;
2043 x = get_reg(RC_FLOAT);
2044 ++vtop;
2045 vtop[0].r = x;
2046 x = fltr(x);
2048 if (f == VT_FLOAT)
2049 o(0x1e22c000 | x | a << 5); // fcvt d(x),s(a)
2050 else
2051 o(0x1e624000 | x | a << 5); // fcvt s(x),d(a)
2055 /* increment tcov counter */
2056 ST_FUNC void gen_increment_tcov (SValue *sv)
2058 int r1, r2;
2060 vpushv(sv);
2061 vtop->r = r1 = get_reg(RC_INT);
2062 r2 = get_reg(RC_INT);
2063 greloca(cur_text_section, sv->sym, ind, R_AARCH64_ADR_GOT_PAGE, 0);
2064 o(0x90000000 | r1); // adrp r1, #sym
2065 greloca(cur_text_section, sv->sym, ind, R_AARCH64_LD64_GOT_LO12_NC, 0);
2066 o(0xf9400000 | r1 | (r1 << 5)); // ld xr,[xr, #sym]
2067 o(0xf9400000 | (intr(r1)<<5) | intr(r2)); // ldr r2, [r1]
2068 o(0x91000400 | (intr(r2)<<5) | intr(r2)); // add r2, r2, #1
2069 o(0xf9000000 | (intr(r1)<<5) | intr(r2)); // str r2, [r1]
2070 vpop();
2073 ST_FUNC void ggoto(void)
2075 arm64_gen_bl_or_b(1);
2076 --vtop;
2079 ST_FUNC void gen_clear_cache(void)
2081 uint32_t beg, end, dsz, isz, p, lab1, b1;
2082 gv2(RC_INT, RC_INT);
2083 vpushi(0);
2084 vtop->r = get_reg(RC_INT);
2085 vpushi(0);
2086 vtop->r = get_reg(RC_INT);
2087 vpushi(0);
2088 vtop->r = get_reg(RC_INT);
2089 beg = intr(vtop[-4].r); // x0
2090 end = intr(vtop[-3].r); // x1
2091 dsz = intr(vtop[-2].r); // x2
2092 isz = intr(vtop[-1].r); // x3
2093 p = intr(vtop[0].r); // x4
2094 vtop -= 5;
2096 o(0xd53b0020 | isz); // mrs x(isz),ctr_el0
2097 o(0x52800080 | p); // mov w(p),#4
2098 o(0x53104c00 | dsz | isz << 5); // ubfx w(dsz),w(isz),#16,#4
2099 o(0x1ac02000 | dsz | p << 5 | dsz << 16); // lsl w(dsz),w(p),w(dsz)
2100 o(0x12000c00 | isz | isz << 5); // and w(isz),w(isz),#15
2101 o(0x1ac02000 | isz | p << 5 | isz << 16); // lsl w(isz),w(p),w(isz)
2102 o(0x51000400 | p | dsz << 5); // sub w(p),w(dsz),#1
2103 o(0x8a240004 | p | beg << 5 | p << 16); // bic x(p),x(beg),x(p)
2104 b1 = ind; o(0x14000000); // b
2105 lab1 = ind;
2106 o(0xd50b7b20 | p); // dc cvau,x(p)
2107 o(0x8b000000 | p | p << 5 | dsz << 16); // add x(p),x(p),x(dsz)
2108 write32le(cur_text_section->data + b1, 0x14000000 | (ind - b1) >> 2);
2109 o(0xeb00001f | p << 5 | end << 16); // cmp x(p),x(end)
2110 o(0x54ffffa3 | ((lab1 - ind) << 3 & 0xffffe0)); // b.cc lab1
2111 o(0xd5033b9f); // dsb ish
2112 o(0x51000400 | p | isz << 5); // sub w(p),w(isz),#1
2113 o(0x8a240004 | p | beg << 5 | p << 16); // bic x(p),x(beg),x(p)
2114 b1 = ind; o(0x14000000); // b
2115 lab1 = ind;
2116 o(0xd50b7520 | p); // ic ivau,x(p)
2117 o(0x8b000000 | p | p << 5 | isz << 16); // add x(p),x(p),x(isz)
2118 write32le(cur_text_section->data + b1, 0x14000000 | (ind - b1) >> 2);
2119 o(0xeb00001f | p << 5 | end << 16); // cmp x(p),x(end)
2120 o(0x54ffffa3 | ((lab1 - ind) << 3 & 0xffffe0)); // b.cc lab1
2121 o(0xd5033b9f); // dsb ish
2122 o(0xd5033fdf); // isb
2125 ST_FUNC void gen_vla_sp_save(int addr) {
2126 uint32_t r = intr(get_reg(RC_INT));
2127 o(0x910003e0 | r); // mov x(r),sp
2128 arm64_strx(3, r, 29, addr);
2131 ST_FUNC void gen_vla_sp_restore(int addr) {
2132 // Use x30 because this function can be called when there
2133 // is a live return value in x0 but there is nothing on
2134 // the value stack to prevent get_reg from returning x0.
2135 uint32_t r = 30;
2136 arm64_ldrx(0, 3, r, 29, addr);
2137 o(0x9100001f | r << 5); // mov sp,x(r)
2140 ST_FUNC void gen_vla_alloc(CType *type, int align) {
2141 uint32_t r;
2142 #if defined(CONFIG_TCC_BCHECK)
2143 if (tcc_state->do_bounds_check)
2144 vpushv(vtop);
2145 #endif
2146 r = intr(gv(RC_INT));
2147 #if defined(CONFIG_TCC_BCHECK)
2148 if (tcc_state->do_bounds_check)
2149 o(0x91004000 | r | r << 5); // add x(r),x(r),#15+1
2150 else
2151 #endif
2152 o(0x91003c00 | r | r << 5); // add x(r),x(r),#15
2153 o(0x927cec00 | r | r << 5); // bic x(r),x(r),#15
2154 o(0xcb2063ff | r << 16); // sub sp,sp,x(r)
2155 vpop();
2156 #if defined(CONFIG_TCC_BCHECK)
2157 if (tcc_state->do_bounds_check) {
2158 vpushi(0);
2159 vtop->r = TREG_R(0);
2160 o(0x910003e0 | vtop->r); // mov r0,sp
2161 vswap();
2162 vpush_helper_func(TOK___bound_new_region);
2163 vrott(3);
2164 gfunc_call(2);
2165 func_bound_add_epilog = 1;
2167 #endif
2170 /* end of A64 code generator */
2171 /*************************************************************/
2172 #endif
2173 /*************************************************************/