Fix cast_test for clang.
[tinycc.git] / arm64-gen.c
blob211e771c53c4ec0358b418e28becb7375690bbc5
1 /*
2 * A64 code generator for TCC
4 * Copyright (c) 2014-2015 Edmund Grimley Evans
6 * Copying and distribution of this file, with or without modification,
7 * are permitted in any medium without royalty provided the copyright
8 * notice and this notice are preserved. This file is offered as-is,
9 * without any warranty.
12 #ifdef TARGET_DEFS_ONLY
14 // Number of registers available to allocator:
15 #define NB_REGS 28 // x0-x18, x30, v0-v7
17 #define TREG_R(x) (x) // x = 0..18
18 #define TREG_R30 19
19 #define TREG_F(x) (x + 20) // x = 0..7
21 // Register classes sorted from more general to more precise:
22 #define RC_INT (1 << 0)
23 #define RC_FLOAT (1 << 1)
24 #define RC_R(x) (1 << (2 + (x))) // x = 0..18
25 #define RC_R30 (1 << 21)
26 #define RC_F(x) (1 << (22 + (x))) // x = 0..7
28 #define RC_IRET (RC_R(0)) // int return register class
29 #define RC_FRET (RC_F(0)) // float return register class
31 #define REG_IRET (TREG_R(0)) // int return register number
32 #define REG_FRET (TREG_F(0)) // float return register number
34 #define PTR_SIZE 8
36 #define LDOUBLE_SIZE 16
37 #define LDOUBLE_ALIGN 16
39 #define MAX_ALIGN 16
41 #define CHAR_IS_UNSIGNED
43 /* define if return values need to be extended explicitely
44 at caller side (for interfacing with non-TCC compilers) */
45 #define PROMOTE_RET
46 /******************************************************/
47 #else /* ! TARGET_DEFS_ONLY */
48 /******************************************************/
49 #define USING_GLOBALS
50 #include "tcc.h"
51 #include <assert.h>
53 ST_DATA const int reg_classes[NB_REGS] = {
54 RC_INT | RC_R(0),
55 RC_INT | RC_R(1),
56 RC_INT | RC_R(2),
57 RC_INT | RC_R(3),
58 RC_INT | RC_R(4),
59 RC_INT | RC_R(5),
60 RC_INT | RC_R(6),
61 RC_INT | RC_R(7),
62 RC_INT | RC_R(8),
63 RC_INT | RC_R(9),
64 RC_INT | RC_R(10),
65 RC_INT | RC_R(11),
66 RC_INT | RC_R(12),
67 RC_INT | RC_R(13),
68 RC_INT | RC_R(14),
69 RC_INT | RC_R(15),
70 RC_INT | RC_R(16),
71 RC_INT | RC_R(17),
72 RC_INT | RC_R(18),
73 RC_R30, // not in RC_INT as we make special use of x30
74 RC_FLOAT | RC_F(0),
75 RC_FLOAT | RC_F(1),
76 RC_FLOAT | RC_F(2),
77 RC_FLOAT | RC_F(3),
78 RC_FLOAT | RC_F(4),
79 RC_FLOAT | RC_F(5),
80 RC_FLOAT | RC_F(6),
81 RC_FLOAT | RC_F(7)
84 #if defined(CONFIG_TCC_BCHECK)
85 static addr_t func_bound_offset;
86 static unsigned long func_bound_ind;
87 static int func_bound_add_epilog;
88 #endif
90 #define IS_FREG(x) ((x) >= TREG_F(0))
92 static uint32_t intr(int r)
94 assert(TREG_R(0) <= r && r <= TREG_R30);
95 return r < TREG_R30 ? r : 30;
98 static uint32_t fltr(int r)
100 assert(TREG_F(0) <= r && r <= TREG_F(7));
101 return r - TREG_F(0);
104 // Add an instruction to text section:
105 ST_FUNC void o(unsigned int c)
107 int ind1 = ind + 4;
108 if (nocode_wanted)
109 return;
110 if (ind1 > cur_text_section->data_allocated)
111 section_realloc(cur_text_section, ind1);
112 write32le(cur_text_section->data + ind, c);
113 ind = ind1;
116 static int arm64_encode_bimm64(uint64_t x)
118 int neg = x & 1;
119 int rep, pos, len;
121 if (neg)
122 x = ~x;
123 if (!x)
124 return -1;
126 if (x >> 2 == (x & (((uint64_t)1 << (64 - 2)) - 1)))
127 rep = 2, x &= ((uint64_t)1 << 2) - 1;
128 else if (x >> 4 == (x & (((uint64_t)1 << (64 - 4)) - 1)))
129 rep = 4, x &= ((uint64_t)1 << 4) - 1;
130 else if (x >> 8 == (x & (((uint64_t)1 << (64 - 8)) - 1)))
131 rep = 8, x &= ((uint64_t)1 << 8) - 1;
132 else if (x >> 16 == (x & (((uint64_t)1 << (64 - 16)) - 1)))
133 rep = 16, x &= ((uint64_t)1 << 16) - 1;
134 else if (x >> 32 == (x & (((uint64_t)1 << (64 - 32)) - 1)))
135 rep = 32, x &= ((uint64_t)1 << 32) - 1;
136 else
137 rep = 64;
139 pos = 0;
140 if (!(x & (((uint64_t)1 << 32) - 1))) x >>= 32, pos += 32;
141 if (!(x & (((uint64_t)1 << 16) - 1))) x >>= 16, pos += 16;
142 if (!(x & (((uint64_t)1 << 8) - 1))) x >>= 8, pos += 8;
143 if (!(x & (((uint64_t)1 << 4) - 1))) x >>= 4, pos += 4;
144 if (!(x & (((uint64_t)1 << 2) - 1))) x >>= 2, pos += 2;
145 if (!(x & (((uint64_t)1 << 1) - 1))) x >>= 1, pos += 1;
147 len = 0;
148 if (!(~x & (((uint64_t)1 << 32) - 1))) x >>= 32, len += 32;
149 if (!(~x & (((uint64_t)1 << 16) - 1))) x >>= 16, len += 16;
150 if (!(~x & (((uint64_t)1 << 8) - 1))) x >>= 8, len += 8;
151 if (!(~x & (((uint64_t)1 << 4) - 1))) x >>= 4, len += 4;
152 if (!(~x & (((uint64_t)1 << 2) - 1))) x >>= 2, len += 2;
153 if (!(~x & (((uint64_t)1 << 1) - 1))) x >>= 1, len += 1;
155 if (x)
156 return -1;
157 if (neg) {
158 pos = (pos + len) & (rep - 1);
159 len = rep - len;
161 return ((0x1000 & rep << 6) | (((rep - 1) ^ 31) << 1 & 63) |
162 ((rep - pos) & (rep - 1)) << 6 | (len - 1));
165 static uint32_t arm64_movi(int r, uint64_t x)
167 uint64_t m = 0xffff;
168 int e;
169 if (!(x & ~m))
170 return 0x52800000 | r | x << 5; // movz w(r),#(x)
171 if (!(x & ~(m << 16)))
172 return 0x52a00000 | r | x >> 11; // movz w(r),#(x >> 16),lsl #16
173 if (!(x & ~(m << 32)))
174 return 0xd2c00000 | r | x >> 27; // movz x(r),#(x >> 32),lsl #32
175 if (!(x & ~(m << 48)))
176 return 0xd2e00000 | r | x >> 43; // movz x(r),#(x >> 48),lsl #48
177 if ((x & ~m) == m << 16)
178 return (0x12800000 | r |
179 (~x << 5 & 0x1fffe0)); // movn w(r),#(~x)
180 if ((x & ~(m << 16)) == m)
181 return (0x12a00000 | r |
182 (~x >> 11 & 0x1fffe0)); // movn w(r),#(~x >> 16),lsl #16
183 if (!~(x | m))
184 return (0x92800000 | r |
185 (~x << 5 & 0x1fffe0)); // movn x(r),#(~x)
186 if (!~(x | m << 16))
187 return (0x92a00000 | r |
188 (~x >> 11 & 0x1fffe0)); // movn x(r),#(~x >> 16),lsl #16
189 if (!~(x | m << 32))
190 return (0x92c00000 | r |
191 (~x >> 27 & 0x1fffe0)); // movn x(r),#(~x >> 32),lsl #32
192 if (!~(x | m << 48))
193 return (0x92e00000 | r |
194 (~x >> 43 & 0x1fffe0)); // movn x(r),#(~x >> 32),lsl #32
195 if (!(x >> 32) && (e = arm64_encode_bimm64(x | x << 32)) >= 0)
196 return 0x320003e0 | r | (uint32_t)e << 10; // movi w(r),#(x)
197 if ((e = arm64_encode_bimm64(x)) >= 0)
198 return 0xb20003e0 | r | (uint32_t)e << 10; // movi x(r),#(x)
199 return 0;
202 static void arm64_movimm(int r, uint64_t x)
204 uint32_t i;
205 if ((i = arm64_movi(r, x)))
206 o(i); // a single MOV
207 else {
208 // MOVZ/MOVN and 1-3 MOVKs
209 int z = 0, m = 0;
210 uint32_t mov1 = 0xd2800000; // movz
211 uint64_t x1 = x;
212 for (i = 0; i < 64; i += 16) {
213 z += !(x >> i & 0xffff);
214 m += !(~x >> i & 0xffff);
216 if (m > z) {
217 x1 = ~x;
218 mov1 = 0x92800000; // movn
220 for (i = 0; i < 64; i += 16)
221 if (x1 >> i & 0xffff) {
222 o(mov1 | r | (x1 >> i & 0xffff) << 5 | i << 17);
223 // movz/movn x(r),#(*),lsl #(i)
224 break;
226 for (i += 16; i < 64; i += 16)
227 if (x1 >> i & 0xffff)
228 o(0xf2800000 | r | (x >> i & 0xffff) << 5 | i << 17);
229 // movk x(r),#(*),lsl #(i)
233 // Patch all branches in list pointed to by t to branch to a:
234 ST_FUNC void gsym_addr(int t_, int a_)
236 uint32_t t = t_;
237 uint32_t a = a_;
238 while (t) {
239 unsigned char *ptr = cur_text_section->data + t;
240 uint32_t next = read32le(ptr);
241 if (a - t + 0x8000000 >= 0x10000000)
242 tcc_error("branch out of range");
243 write32le(ptr, (a - t == 4 ? 0xd503201f : // nop
244 0x14000000 | ((a - t) >> 2 & 0x3ffffff))); // b
245 t = next;
249 static int arm64_type_size(int t)
252 * case values are in increasing order (from 1 to 11).
253 * which 'may' help compiler optimizers. See tcc.h
255 switch (t & VT_BTYPE) {
256 case VT_BYTE: return 0;
257 case VT_SHORT: return 1;
258 case VT_INT: return 2;
259 case VT_LLONG: return 3;
260 case VT_PTR: return 3;
261 case VT_FUNC: return 3;
262 case VT_STRUCT: return 3;
263 case VT_FLOAT: return 2;
264 case VT_DOUBLE: return 3;
265 case VT_LDOUBLE: return 4;
266 case VT_BOOL: return 0;
268 assert(0);
269 return 0;
272 static void arm64_spoff(int reg, uint64_t off)
274 uint32_t sub = off >> 63;
275 if (sub)
276 off = -off;
277 if (off < 4096)
278 o(0x910003e0 | sub << 30 | reg | off << 10);
279 // (add|sub) x(reg),sp,#(off)
280 else {
281 arm64_movimm(30, off); // use x30 for offset
282 o(0x8b3e63e0 | sub << 30 | reg); // (add|sub) x(reg),sp,x30
286 static void arm64_ldrx(int sg, int sz_, int dst, int bas, uint64_t off)
288 uint32_t sz = sz_;
289 if (sz >= 2)
290 sg = 0;
291 if (!(off & ~((uint32_t)0xfff << sz)))
292 o(0x39400000 | dst | bas << 5 | off << (10 - sz) |
293 (uint32_t)!!sg << 23 | sz << 30); // ldr(*) x(dst),[x(bas),#(off)]
294 else if (off < 256 || -off <= 256)
295 o(0x38400000 | dst | bas << 5 | (off & 511) << 12 |
296 (uint32_t)!!sg << 23 | sz << 30); // ldur(*) x(dst),[x(bas),#(off)]
297 else {
298 arm64_movimm(30, off); // use x30 for offset
299 o(0x38206800 | dst | bas << 5 | (uint32_t)30 << 16 |
300 (uint32_t)(!!sg + 1) << 22 | sz << 30); // ldr(*) x(dst),[x(bas),x30]
304 static void arm64_ldrv(int sz_, int dst, int bas, uint64_t off)
306 uint32_t sz = sz_;
307 if (!(off & ~((uint32_t)0xfff << sz)))
308 o(0x3d400000 | dst | bas << 5 | off << (10 - sz) |
309 (sz & 4) << 21 | (sz & 3) << 30); // ldr (s|d|q)(dst),[x(bas),#(off)]
310 else if (off < 256 || -off <= 256)
311 o(0x3c400000 | dst | bas << 5 | (off & 511) << 12 |
312 (sz & 4) << 21 | (sz & 3) << 30); // ldur (s|d|q)(dst),[x(bas),#(off)]
313 else {
314 arm64_movimm(30, off); // use x30 for offset
315 o(0x3c606800 | dst | bas << 5 | (uint32_t)30 << 16 |
316 sz << 30 | (sz & 4) << 21); // ldr (s|d|q)(dst),[x(bas),x30]
320 static void arm64_ldrs(int reg_, int size)
322 uint32_t reg = reg_;
323 // Use x30 for intermediate value in some cases.
324 switch (size) {
325 default: assert(0); break;
326 case 1:
327 arm64_ldrx(0, 0, reg, reg, 0);
328 break;
329 case 2:
330 arm64_ldrx(0, 1, reg, reg, 0);
331 break;
332 case 3:
333 arm64_ldrx(0, 1, 30, reg, 0);
334 arm64_ldrx(0, 0, reg, reg, 2);
335 o(0x2a0043c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #16
336 break;
337 case 4:
338 arm64_ldrx(0, 2, reg, reg, 0);
339 break;
340 case 5:
341 arm64_ldrx(0, 2, 30, reg, 0);
342 arm64_ldrx(0, 0, reg, reg, 4);
343 o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32
344 break;
345 case 6:
346 arm64_ldrx(0, 2, 30, reg, 0);
347 arm64_ldrx(0, 1, reg, reg, 4);
348 o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32
349 break;
350 case 7:
351 arm64_ldrx(0, 2, 30, reg, 0);
352 arm64_ldrx(0, 2, reg, reg, 3);
353 o(0x53087c00 | reg | reg << 5); // lsr w(reg), w(reg), #8
354 o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32
355 break;
356 case 8:
357 arm64_ldrx(0, 3, reg, reg, 0);
358 break;
359 case 9:
360 arm64_ldrx(0, 0, reg + 1, reg, 8);
361 arm64_ldrx(0, 3, reg, reg, 0);
362 break;
363 case 10:
364 arm64_ldrx(0, 1, reg + 1, reg, 8);
365 arm64_ldrx(0, 3, reg, reg, 0);
366 break;
367 case 11:
368 arm64_ldrx(0, 2, reg + 1, reg, 7);
369 o(0x53087c00 | (reg+1) | (reg+1) << 5); // lsr w(reg+1), w(reg+1), #8
370 arm64_ldrx(0, 3, reg, reg, 0);
371 break;
372 case 12:
373 arm64_ldrx(0, 2, reg + 1, reg, 8);
374 arm64_ldrx(0, 3, reg, reg, 0);
375 break;
376 case 13:
377 arm64_ldrx(0, 3, reg + 1, reg, 5);
378 o(0xd358fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #24
379 arm64_ldrx(0, 3, reg, reg, 0);
380 break;
381 case 14:
382 arm64_ldrx(0, 3, reg + 1, reg, 6);
383 o(0xd350fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #16
384 arm64_ldrx(0, 3, reg, reg, 0);
385 break;
386 case 15:
387 arm64_ldrx(0, 3, reg + 1, reg, 7);
388 o(0xd348fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #8
389 arm64_ldrx(0, 3, reg, reg, 0);
390 break;
391 case 16:
392 o(0xa9400000 | reg | (reg+1) << 10 | reg << 5);
393 // ldp x(reg),x(reg+1),[x(reg)]
394 break;
398 static void arm64_strx(int sz_, int dst, int bas, uint64_t off)
400 uint32_t sz = sz_;
401 if (!(off & ~((uint32_t)0xfff << sz)))
402 o(0x39000000 | dst | bas << 5 | off << (10 - sz) | sz << 30);
403 // str(*) x(dst),[x(bas],#(off)]
404 else if (off < 256 || -off <= 256)
405 o(0x38000000 | dst | bas << 5 | (off & 511) << 12 | sz << 30);
406 // stur(*) x(dst),[x(bas],#(off)]
407 else {
408 arm64_movimm(30, off); // use x30 for offset
409 o(0x38206800 | dst | bas << 5 | (uint32_t)30 << 16 | sz << 30);
410 // str(*) x(dst),[x(bas),x30]
414 static void arm64_strv(int sz_, int dst, int bas, uint64_t off)
416 uint32_t sz = sz_;
417 if (!(off & ~((uint32_t)0xfff << sz)))
418 o(0x3d000000 | dst | bas << 5 | off << (10 - sz) |
419 (sz & 4) << 21 | (sz & 3) << 30); // str (s|d|q)(dst),[x(bas),#(off)]
420 else if (off < 256 || -off <= 256)
421 o(0x3c000000 | dst | bas << 5 | (off & 511) << 12 |
422 (sz & 4) << 21 | (sz & 3) << 30); // stur (s|d|q)(dst),[x(bas),#(off)]
423 else {
424 arm64_movimm(30, off); // use x30 for offset
425 o(0x3c206800 | dst | bas << 5 | (uint32_t)30 << 16 |
426 sz << 30 | (sz & 4) << 21); // str (s|d|q)(dst),[x(bas),x30]
430 static void arm64_sym(int r, Sym *sym, unsigned long addend)
432 // Currently TCC's linker does not generate COPY relocations for
433 // STT_OBJECTs when tcc is invoked with "-run". This typically
434 // results in "R_AARCH64_ADR_PREL_PG_HI21 relocation failed" when
435 // a program refers to stdin. A workaround is to avoid that
436 // relocation and use only relocations with unlimited range.
437 int avoid_adrp = 1;
439 if (avoid_adrp || sym->a.weak) {
440 // (GCC uses a R_AARCH64_ABS64 in this case.)
441 greloca(cur_text_section, sym, ind, R_AARCH64_MOVW_UABS_G0_NC, addend);
442 o(0xd2800000 | r); // mov x(rt),#0,lsl #0
443 greloca(cur_text_section, sym, ind, R_AARCH64_MOVW_UABS_G1_NC, addend);
444 o(0xf2a00000 | r); // movk x(rt),#0,lsl #16
445 greloca(cur_text_section, sym, ind, R_AARCH64_MOVW_UABS_G2_NC, addend);
446 o(0xf2c00000 | r); // movk x(rt),#0,lsl #32
447 greloca(cur_text_section, sym, ind, R_AARCH64_MOVW_UABS_G3, addend);
448 o(0xf2e00000 | r); // movk x(rt),#0,lsl #48
450 else {
451 greloca(cur_text_section, sym, ind, R_AARCH64_ADR_PREL_PG_HI21, addend);
452 o(0x90000000 | r);
453 greloca(cur_text_section, sym, ind, R_AARCH64_ADD_ABS_LO12_NC, addend);
454 o(0x91000000 | r | r << 5);
458 static void arm64_load_cmp(int r, SValue *sv);
460 ST_FUNC void load(int r, SValue *sv)
462 int svtt = sv->type.t;
463 int svr = sv->r & ~VT_BOUNDED;
464 int svrv = svr & VT_VALMASK;
465 uint64_t svcul = (uint32_t)sv->c.i;
466 svcul = svcul >> 31 & 1 ? svcul - ((uint64_t)1 << 32) : svcul;
468 if (svr == (VT_LOCAL | VT_LVAL)) {
469 if (IS_FREG(r))
470 arm64_ldrv(arm64_type_size(svtt), fltr(r), 29, svcul);
471 else
472 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
473 intr(r), 29, svcul);
474 return;
477 if ((svr & ~VT_VALMASK) == VT_LVAL && svrv < VT_CONST) {
478 if (IS_FREG(r))
479 arm64_ldrv(arm64_type_size(svtt), fltr(r), intr(svrv), 0);
480 else
481 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
482 intr(r), intr(svrv), 0);
483 return;
486 if (svr == (VT_CONST | VT_LVAL | VT_SYM)) {
487 arm64_sym(30, sv->sym, svcul); // use x30 for address
488 if (IS_FREG(r))
489 arm64_ldrv(arm64_type_size(svtt), fltr(r), 30, 0);
490 else
491 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
492 intr(r), 30, 0);
493 return;
496 if (svr == (VT_CONST | VT_SYM)) {
497 arm64_sym(intr(r), sv->sym, svcul);
498 return;
501 if (svr == VT_CONST) {
502 if ((svtt & VT_BTYPE) != VT_VOID)
503 arm64_movimm(intr(r), arm64_type_size(svtt) == 3 ?
504 sv->c.i : (uint32_t)svcul);
505 return;
508 if (svr < VT_CONST) {
509 if (IS_FREG(r) && IS_FREG(svr))
510 if (svtt == VT_LDOUBLE)
511 o(0x4ea01c00 | fltr(r) | fltr(svr) << 5);
512 // mov v(r).16b,v(svr).16b
513 else
514 o(0x1e604000 | fltr(r) | fltr(svr) << 5); // fmov d(r),d(svr)
515 else if (!IS_FREG(r) && !IS_FREG(svr))
516 o(0xaa0003e0 | intr(r) | intr(svr) << 16); // mov x(r),x(svr)
517 else
518 assert(0);
519 return;
522 if (svr == VT_LOCAL) {
523 if (-svcul < 0x1000)
524 o(0xd10003a0 | intr(r) | -svcul << 10); // sub x(r),x29,#...
525 else {
526 arm64_movimm(30, -svcul); // use x30 for offset
527 o(0xcb0003a0 | intr(r) | (uint32_t)30 << 16); // sub x(r),x29,x30
529 return;
532 if (svr == VT_JMP || svr == VT_JMPI) {
533 int t = (svr == VT_JMPI);
534 arm64_movimm(intr(r), t);
535 o(0x14000002); // b .+8
536 gsym(svcul);
537 arm64_movimm(intr(r), t ^ 1);
538 return;
541 if (svr == (VT_LLOCAL | VT_LVAL)) {
542 arm64_ldrx(0, 3, 30, 29, svcul); // use x30 for offset
543 if (IS_FREG(r))
544 arm64_ldrv(arm64_type_size(svtt), fltr(r), 30, 0);
545 else
546 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
547 intr(r), 30, 0);
548 return;
551 if (svr == VT_CMP) {
552 arm64_load_cmp(r, sv);
553 return;
556 printf("load(%x, (%x, %x, %llx))\n", r, svtt, sv->r, (long long)svcul);
557 assert(0);
560 ST_FUNC void store(int r, SValue *sv)
562 int svtt = sv->type.t;
563 int svr = sv->r & ~VT_BOUNDED;
564 int svrv = svr & VT_VALMASK;
565 uint64_t svcul = (uint32_t)sv->c.i;
566 svcul = svcul >> 31 & 1 ? svcul - ((uint64_t)1 << 32) : svcul;
568 if (svr == (VT_LOCAL | VT_LVAL)) {
569 if (IS_FREG(r))
570 arm64_strv(arm64_type_size(svtt), fltr(r), 29, svcul);
571 else
572 arm64_strx(arm64_type_size(svtt), intr(r), 29, svcul);
573 return;
576 if ((svr & ~VT_VALMASK) == VT_LVAL && svrv < VT_CONST) {
577 if (IS_FREG(r))
578 arm64_strv(arm64_type_size(svtt), fltr(r), intr(svrv), 0);
579 else
580 arm64_strx(arm64_type_size(svtt), intr(r), intr(svrv), 0);
581 return;
584 if (svr == (VT_CONST | VT_LVAL | VT_SYM)) {
585 arm64_sym(30, sv->sym, svcul); // use x30 for address
586 if (IS_FREG(r))
587 arm64_strv(arm64_type_size(svtt), fltr(r), 30, 0);
588 else
589 arm64_strx(arm64_type_size(svtt), intr(r), 30, 0);
590 return;
593 printf("store(%x, (%x, %x, %llx))\n", r, svtt, sv->r, (long long)svcul);
594 assert(0);
597 static void arm64_gen_bl_or_b(int b)
599 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST && (vtop->r & VT_SYM)) {
600 assert(!b);
601 greloca(cur_text_section, vtop->sym, ind, R_AARCH64_CALL26, 0);
602 o(0x94000000); // bl .
603 #ifdef CONFIG_TCC_BCHECK
604 if (tcc_state->do_bounds_check &&
605 (vtop->sym->v == TOK_setjmp ||
606 vtop->sym->v == TOK__setjmp ||
607 vtop->sym->v == TOK_sigsetjmp ||
608 vtop->sym->v == TOK___sigsetjmp))
609 func_bound_add_epilog = 1;
610 #endif
612 else {
613 #ifdef CONFIG_TCC_BCHECK
614 vtop->r &= ~VT_MUSTBOUND;
615 #endif
616 o(0xd61f0000 | (uint32_t)!b << 21 | intr(gv(RC_R30)) << 5); // br/blr
620 #if defined(CONFIG_TCC_BCHECK)
622 static void gen_bounds_call(int v)
624 Sym *sym = external_global_sym(v, &func_old_type);
626 greloca(cur_text_section, sym, ind, R_AARCH64_CALL26, 0);
627 o(0x94000000); // bl
630 /* generate a bounded pointer addition */
631 ST_FUNC void gen_bounded_ptr_add(void)
633 vpush_global_sym(&func_old_type, TOK___bound_ptr_add);
634 vrott(3);
635 gfunc_call(2);
636 vpushi(0);
637 /* returned pointer is in REG_IRET */
638 vtop->r = REG_IRET | VT_BOUNDED;
639 if (nocode_wanted)
640 return;
641 /* relocation offset of the bounding function call point */
642 vtop->c.i = (cur_text_section->reloc->data_offset - sizeof(ElfW(Rela)));
645 /* patch pointer addition in vtop so that pointer dereferencing is
646 also tested */
647 ST_FUNC void gen_bounded_ptr_deref(void)
649 addr_t func;
650 int size, align;
651 ElfW(Rela) *rel;
652 Sym *sym;
654 if (nocode_wanted)
655 return;
657 size = type_size(&vtop->type, &align);
658 switch(size) {
659 case 1: func = TOK___bound_ptr_indir1; break;
660 case 2: func = TOK___bound_ptr_indir2; break;
661 case 4: func = TOK___bound_ptr_indir4; break;
662 case 8: func = TOK___bound_ptr_indir8; break;
663 case 12: func = TOK___bound_ptr_indir12; break;
664 case 16: func = TOK___bound_ptr_indir16; break;
665 default:
666 /* may happen with struct member access */
667 return;
668 //tcc_error("unhandled size when dereferencing bounded pointer");
669 //func = 0;
670 //break;
672 sym = external_global_sym(func, &func_old_type);
673 if (!sym->c)
674 put_extern_sym(sym, NULL, 0, 0);
675 /* patch relocation */
676 /* XXX: find a better solution ? */
677 rel = (ElfW(Rela) *)(cur_text_section->reloc->data + vtop->c.i);
678 rel->r_info = ELF64_R_INFO(sym->c, ELF64_R_TYPE(rel->r_info));
681 static void gen_bounds_prolog(void)
683 /* leave some room for bound checking code */
684 func_bound_offset = lbounds_section->data_offset;
685 func_bound_ind = ind;
686 func_bound_add_epilog = 0;
687 o(0xd503201f); /* nop -> mov x0,#0,lsl #0, lbound section pointer */
688 o(0xd503201f);
689 o(0xd503201f);
690 o(0xd503201f);
691 o(0xd503201f); /* nop -> call __bound_local_new */
694 static void gen_bounds_epilog(void)
696 addr_t saved_ind;
697 addr_t *bounds_ptr;
698 Sym *sym_data;
699 int offset_modified = func_bound_offset != lbounds_section->data_offset;
701 if (!offset_modified && !func_bound_add_epilog)
702 return;
704 /* add end of table info */
705 bounds_ptr = section_ptr_add(lbounds_section, sizeof(addr_t));
706 *bounds_ptr = 0;
708 sym_data = get_sym_ref(&char_pointer_type, lbounds_section,
709 func_bound_offset, lbounds_section->data_offset);
711 /* generate bound local allocation */
712 if (offset_modified) {
713 saved_ind = ind;
714 ind = func_bound_ind;
715 greloca(cur_text_section, sym_data, ind, R_AARCH64_MOVW_UABS_G0_NC, 0);
716 o(0xd2800000); /* mov x0,#0,lsl #0, lbound section pointer */
717 greloca(cur_text_section, sym_data, ind, R_AARCH64_MOVW_UABS_G1_NC, 0);
718 o(0xf2a00000); /* movk x0,#0,lsl #16 */
719 greloca(cur_text_section, sym_data, ind, R_AARCH64_MOVW_UABS_G2_NC, 0);
720 o(0xf2c00000); /* movk x0,#0,lsl #32 */
721 greloca(cur_text_section, sym_data, ind, R_AARCH64_MOVW_UABS_G3, 0);
722 o(0xf2e00000); /* movk x0,#0,lsl #48 */
723 gen_bounds_call(TOK___bound_local_new);
724 ind = saved_ind;
727 /* generate bound check local freeing */
728 o(0xf81f0fe0); /* str x0, [sp, #-16]! */
729 o(0x3c9f0fe0); /* str q0, [sp, #-16]! */
730 greloca(cur_text_section, sym_data, ind, R_AARCH64_MOVW_UABS_G0_NC, 0);
731 o(0xd2800000); // mov x0,#0,lsl #0
732 greloca(cur_text_section, sym_data, ind, R_AARCH64_MOVW_UABS_G1_NC, 0);
733 o(0xf2a00000); // movk x0,#0,lsl #16
734 greloca(cur_text_section, sym_data, ind, R_AARCH64_MOVW_UABS_G2_NC, 0);
735 o(0xf2c00000); // movk x0,#0,lsl #32
736 greloca(cur_text_section, sym_data, ind, R_AARCH64_MOVW_UABS_G3, 0);
737 o(0xf2e00000); // movk x0,#0,lsl #48
738 gen_bounds_call(TOK___bound_local_delete);
739 o(0x3cc107e0); /* ldr q0, [sp], #16 */
740 o(0xf84107e0); /* ldr x0, [sp], #16 */
742 #endif
744 static int arm64_hfa_aux(CType *type, int *fsize, int num)
746 if (is_float(type->t)) {
747 int a, n = type_size(type, &a);
748 if (num >= 4 || (*fsize && *fsize != n))
749 return -1;
750 *fsize = n;
751 return num + 1;
753 else if ((type->t & VT_BTYPE) == VT_STRUCT) {
754 int is_struct = 0; // rather than union
755 Sym *field;
756 for (field = type->ref->next; field; field = field->next)
757 if (field->c) {
758 is_struct = 1;
759 break;
761 if (is_struct) {
762 int num0 = num;
763 for (field = type->ref->next; field; field = field->next) {
764 if (field->c != (num - num0) * *fsize)
765 return -1;
766 num = arm64_hfa_aux(&field->type, fsize, num);
767 if (num == -1)
768 return -1;
770 if (type->ref->c != (num - num0) * *fsize)
771 return -1;
772 return num;
774 else { // union
775 int num0 = num;
776 for (field = type->ref->next; field; field = field->next) {
777 int num1 = arm64_hfa_aux(&field->type, fsize, num0);
778 if (num1 == -1)
779 return -1;
780 num = num1 < num ? num : num1;
782 if (type->ref->c != (num - num0) * *fsize)
783 return -1;
784 return num;
787 else if ((type->t & VT_ARRAY) && ((type->t & VT_BTYPE) != VT_PTR)) {
788 int num1;
789 if (!type->ref->c)
790 return num;
791 num1 = arm64_hfa_aux(&type->ref->type, fsize, num);
792 if (num1 == -1 || (num1 != num && type->ref->c > 4))
793 return -1;
794 num1 = num + type->ref->c * (num1 - num);
795 if (num1 > 4)
796 return -1;
797 return num1;
799 return -1;
802 static int arm64_hfa(CType *type, int *fsize)
804 if ((type->t & VT_BTYPE) == VT_STRUCT ||
805 ((type->t & VT_ARRAY) && ((type->t & VT_BTYPE) != VT_PTR))) {
806 int sz = 0;
807 int n = arm64_hfa_aux(type, &sz, 0);
808 if (0 < n && n <= 4) {
809 if (fsize)
810 *fsize = sz;
811 return n;
814 return 0;
817 static unsigned long arm64_pcs_aux(int n, CType **type, unsigned long *a)
819 int nx = 0; // next integer register
820 int nv = 0; // next vector register
821 unsigned long ns = 32; // next stack offset
822 int i;
824 for (i = 0; i < n; i++) {
825 int hfa = arm64_hfa(type[i], 0);
826 int size, align;
828 if ((type[i]->t & VT_ARRAY) ||
829 (type[i]->t & VT_BTYPE) == VT_FUNC)
830 size = align = 8;
831 else
832 size = type_size(type[i], &align);
834 if (hfa)
835 // B.2
837 else if (size > 16) {
838 // B.3: replace with pointer
839 if (nx < 8)
840 a[i] = nx++ << 1 | 1;
841 else {
842 ns = (ns + 7) & ~7;
843 a[i] = ns | 1;
844 ns += 8;
846 continue;
848 else if ((type[i]->t & VT_BTYPE) == VT_STRUCT)
849 // B.4
850 size = (size + 7) & ~7;
852 // C.1
853 if (is_float(type[i]->t) && nv < 8) {
854 a[i] = 16 + (nv++ << 1);
855 continue;
858 // C.2
859 if (hfa && nv + hfa <= 8) {
860 a[i] = 16 + (nv << 1);
861 nv += hfa;
862 continue;
865 // C.3
866 if (hfa) {
867 nv = 8;
868 size = (size + 7) & ~7;
871 // C.4
872 if (hfa || (type[i]->t & VT_BTYPE) == VT_LDOUBLE) {
873 ns = (ns + 7) & ~7;
874 ns = (ns + align - 1) & -align;
877 // C.5
878 if ((type[i]->t & VT_BTYPE) == VT_FLOAT)
879 size = 8;
881 // C.6
882 if (hfa || is_float(type[i]->t)) {
883 a[i] = ns;
884 ns += size;
885 continue;
888 // C.7
889 if ((type[i]->t & VT_BTYPE) != VT_STRUCT && size <= 8 && nx < 8) {
890 a[i] = nx++ << 1;
891 continue;
894 // C.8
895 if (align == 16)
896 nx = (nx + 1) & ~1;
898 // C.9
899 if ((type[i]->t & VT_BTYPE) != VT_STRUCT && size == 16 && nx < 7) {
900 a[i] = nx << 1;
901 nx += 2;
902 continue;
905 // C.10
906 if ((type[i]->t & VT_BTYPE) == VT_STRUCT && size <= (8 - nx) * 8) {
907 a[i] = nx << 1;
908 nx += (size + 7) >> 3;
909 continue;
912 // C.11
913 nx = 8;
915 // C.12
916 ns = (ns + 7) & ~7;
917 ns = (ns + align - 1) & -align;
919 // C.13
920 if ((type[i]->t & VT_BTYPE) == VT_STRUCT) {
921 a[i] = ns;
922 ns += size;
923 continue;
926 // C.14
927 if (size < 8)
928 size = 8;
930 // C.15
931 a[i] = ns;
932 ns += size;
935 return ns - 32;
938 static unsigned long arm64_pcs(int n, CType **type, unsigned long *a)
940 unsigned long stack;
942 // Return type:
943 if ((type[0]->t & VT_BTYPE) == VT_VOID)
944 a[0] = -1;
945 else {
946 arm64_pcs_aux(1, type, a);
947 assert(a[0] == 0 || a[0] == 1 || a[0] == 16);
950 // Argument types:
951 stack = arm64_pcs_aux(n, type + 1, a + 1);
953 if (0) {
954 int i;
955 for (i = 0; i <= n; i++) {
956 if (!i)
957 printf("arm64_pcs return: ");
958 else
959 printf("arm64_pcs arg %d: ", i);
960 if (a[i] == (unsigned long)-1)
961 printf("void\n");
962 else if (a[i] == 1 && !i)
963 printf("X8 pointer\n");
964 else if (a[i] < 16)
965 printf("X%lu%s\n", a[i] / 2, a[i] & 1 ? " pointer" : "");
966 else if (a[i] < 32)
967 printf("V%lu\n", a[i] / 2 - 8);
968 else
969 printf("stack %lu%s\n",
970 (a[i] - 32) & ~1, a[i] & 1 ? " pointer" : "");
974 return stack;
977 ST_FUNC void gfunc_call(int nb_args)
979 CType *return_type;
980 CType **t;
981 unsigned long *a, *a1;
982 unsigned long stack;
983 int i;
985 #ifdef CONFIG_TCC_BCHECK
986 if (tcc_state->do_bounds_check)
987 gbound_args(nb_args);
988 #endif
990 return_type = &vtop[-nb_args].type.ref->type;
991 if ((return_type->t & VT_BTYPE) == VT_STRUCT)
992 --nb_args;
994 t = tcc_malloc((nb_args + 1) * sizeof(*t));
995 a = tcc_malloc((nb_args + 1) * sizeof(*a));
996 a1 = tcc_malloc((nb_args + 1) * sizeof(*a1));
998 t[0] = return_type;
999 for (i = 0; i < nb_args; i++)
1000 t[nb_args - i] = &vtop[-i].type;
1002 stack = arm64_pcs(nb_args, t, a);
1004 // Allocate space for structs replaced by pointer:
1005 for (i = nb_args; i; i--)
1006 if (a[i] & 1) {
1007 SValue *arg = &vtop[i - nb_args];
1008 int align, size = type_size(&arg->type, &align);
1009 assert((arg->type.t & VT_BTYPE) == VT_STRUCT);
1010 stack = (stack + align - 1) & -align;
1011 a1[i] = stack;
1012 stack += size;
1015 stack = (stack + 15) >> 4 << 4;
1017 assert(stack < 0x1000);
1018 if (stack)
1019 o(0xd10003ff | stack << 10); // sub sp,sp,#(n)
1021 // First pass: set all values on stack
1022 for (i = nb_args; i; i--) {
1023 vpushv(vtop - nb_args + i);
1025 if (a[i] & 1) {
1026 // struct replaced by pointer
1027 int r = get_reg(RC_INT);
1028 arm64_spoff(intr(r), a1[i]);
1029 vset(&vtop->type, r | VT_LVAL, 0);
1030 vswap();
1031 vstore();
1032 if (a[i] >= 32) {
1033 // pointer on stack
1034 r = get_reg(RC_INT);
1035 arm64_spoff(intr(r), a1[i]);
1036 arm64_strx(3, intr(r), 31, (a[i] - 32) >> 1 << 1);
1039 else if (a[i] >= 32) {
1040 // value on stack
1041 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
1042 int r = get_reg(RC_INT);
1043 arm64_spoff(intr(r), a[i] - 32);
1044 vset(&vtop->type, r | VT_LVAL, 0);
1045 vswap();
1046 vstore();
1048 else if (is_float(vtop->type.t)) {
1049 gv(RC_FLOAT);
1050 arm64_strv(arm64_type_size(vtop[0].type.t),
1051 fltr(vtop[0].r), 31, a[i] - 32);
1053 else {
1054 gv(RC_INT);
1055 arm64_strx(arm64_type_size(vtop[0].type.t),
1056 intr(vtop[0].r), 31, a[i] - 32);
1060 --vtop;
1063 // Second pass: assign values to registers
1064 for (i = nb_args; i; i--, vtop--) {
1065 if (a[i] < 16 && !(a[i] & 1)) {
1066 // value in general-purpose registers
1067 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
1068 int align, size = type_size(&vtop->type, &align);
1069 vtop->type.t = VT_PTR;
1070 gaddrof();
1071 gv(RC_R(a[i] / 2));
1072 arm64_ldrs(a[i] / 2, size);
1074 else
1075 gv(RC_R(a[i] / 2));
1077 else if (a[i] < 16)
1078 // struct replaced by pointer in register
1079 arm64_spoff(a[i] / 2, a1[i]);
1080 else if (a[i] < 32) {
1081 // value in floating-point registers
1082 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
1083 uint32_t j, sz, n = arm64_hfa(&vtop->type, (int *)&sz);
1084 vtop->type.t = VT_PTR;
1085 gaddrof();
1086 gv(RC_R30);
1087 for (j = 0; j < n; j++)
1088 o(0x3d4003c0 |
1089 (sz & 16) << 19 | -(sz & 8) << 27 | (sz & 4) << 29 |
1090 (a[i] / 2 - 8 + j) |
1091 j << 10); // ldr ([sdq])(*),[x30,#(j * sz)]
1093 else
1094 gv(RC_F(a[i] / 2 - 8));
1098 if ((return_type->t & VT_BTYPE) == VT_STRUCT) {
1099 if (a[0] == 1) {
1100 // indirect return: set x8 and discard the stack value
1101 gv(RC_R(8));
1102 --vtop;
1104 else
1105 // return in registers: keep the address for after the call
1106 vswap();
1109 save_regs(0);
1110 arm64_gen_bl_or_b(0);
1111 --vtop;
1112 if (stack)
1113 o(0x910003ff | stack << 10); // add sp,sp,#(n)
1116 int rt = return_type->t;
1117 int bt = rt & VT_BTYPE;
1118 if (bt == VT_STRUCT && !(a[0] & 1)) {
1119 // A struct was returned in registers, so write it out:
1120 gv(RC_R(8));
1121 --vtop;
1122 if (a[0] == 0) {
1123 int align, size = type_size(return_type, &align);
1124 assert(size <= 16);
1125 if (size > 8)
1126 o(0xa9000500); // stp x0,x1,[x8]
1127 else if (size)
1128 arm64_strx(size > 4 ? 3 : size > 2 ? 2 : size > 1, 0, 8, 0);
1131 else if (a[0] == 16) {
1132 uint32_t j, sz, n = arm64_hfa(return_type, (int *)&sz);
1133 for (j = 0; j < n; j++)
1134 o(0x3d000100 |
1135 (sz & 16) << 19 | -(sz & 8) << 27 | (sz & 4) << 29 |
1136 (a[i] / 2 - 8 + j) |
1137 j << 10); // str ([sdq])(*),[x8,#(j * sz)]
1142 tcc_free(a1);
1143 tcc_free(a);
1144 tcc_free(t);
1147 static unsigned long arm64_func_va_list_stack;
1148 static int arm64_func_va_list_gr_offs;
1149 static int arm64_func_va_list_vr_offs;
1150 static int arm64_func_sub_sp_offset;
1152 ST_FUNC void gfunc_prolog(Sym *func_sym)
1154 CType *func_type = &func_sym->type;
1155 int n = 0;
1156 int i = 0;
1157 Sym *sym;
1158 CType **t;
1159 unsigned long *a;
1161 func_vc = 144; // offset of where x8 is stored
1163 for (sym = func_type->ref; sym; sym = sym->next)
1164 ++n;
1165 t = n ? tcc_malloc(n * sizeof(*t)) : NULL;
1166 a = n ? tcc_malloc(n * sizeof(*a)) : NULL;
1168 for (sym = func_type->ref; sym; sym = sym->next)
1169 t[i++] = &sym->type;
1171 arm64_func_va_list_stack = arm64_pcs(n - 1, t, a);
1173 o(0xa9b27bfd); // stp x29,x30,[sp,#-224]!
1174 o(0xad0087e0); // stp q0,q1,[sp,#16]
1175 o(0xad018fe2); // stp q2,q3,[sp,#48]
1176 o(0xad0297e4); // stp q4,q5,[sp,#80]
1177 o(0xad039fe6); // stp q6,q7,[sp,#112]
1178 o(0xa90923e8); // stp x8,x8,[sp,#144]
1179 o(0xa90a07e0); // stp x0,x1,[sp,#160]
1180 o(0xa90b0fe2); // stp x2,x3,[sp,#176]
1181 o(0xa90c17e4); // stp x4,x5,[sp,#192]
1182 o(0xa90d1fe6); // stp x6,x7,[sp,#208]
1184 arm64_func_va_list_gr_offs = -64;
1185 arm64_func_va_list_vr_offs = -128;
1187 for (i = 1, sym = func_type->ref->next; sym; i++, sym = sym->next) {
1188 int off = (a[i] < 16 ? 160 + a[i] / 2 * 8 :
1189 a[i] < 32 ? 16 + (a[i] - 16) / 2 * 16 :
1190 224 + ((a[i] - 32) >> 1 << 1));
1191 sym_push(sym->v & ~SYM_FIELD, &sym->type,
1192 (a[i] & 1 ? VT_LLOCAL : VT_LOCAL) | VT_LVAL,
1193 off);
1195 if (a[i] < 16) {
1196 int align, size = type_size(&sym->type, &align);
1197 arm64_func_va_list_gr_offs = (a[i] / 2 - 7 +
1198 (!(a[i] & 1) && size > 8)) * 8;
1200 else if (a[i] < 32) {
1201 uint32_t hfa = arm64_hfa(&sym->type, 0);
1202 arm64_func_va_list_vr_offs = (a[i] / 2 - 16 +
1203 (hfa ? hfa : 1)) * 16;
1206 // HFAs of float and double need to be written differently:
1207 if (16 <= a[i] && a[i] < 32 && (sym->type.t & VT_BTYPE) == VT_STRUCT) {
1208 uint32_t j, sz, k = arm64_hfa(&sym->type, (int *)&sz);
1209 if (sz < 16)
1210 for (j = 0; j < k; j++) {
1211 o(0x3d0003e0 | -(sz & 8) << 27 | (sz & 4) << 29 |
1212 ((a[i] - 16) / 2 + j) | (off / sz + j) << 10);
1213 // str ([sdq])(*),[sp,#(j * sz)]
1218 tcc_free(a);
1219 tcc_free(t);
1221 o(0x910003fd); // mov x29,sp
1222 arm64_func_sub_sp_offset = ind;
1223 // In gfunc_epilog these will be replaced with code to decrement SP:
1224 o(0xd503201f); // nop
1225 o(0xd503201f); // nop
1226 loc = 0;
1227 #ifdef CONFIG_TCC_BCHECK
1228 if (tcc_state->do_bounds_check)
1229 gen_bounds_prolog();
1230 #endif
1233 ST_FUNC void gen_va_start(void)
1235 int r;
1236 --vtop; // we don't need the "arg"
1237 gaddrof();
1238 r = intr(gv(RC_INT));
1240 if (arm64_func_va_list_stack) {
1241 //xx could use add (immediate) here
1242 arm64_movimm(30, arm64_func_va_list_stack + 224);
1243 o(0x8b1e03be); // add x30,x29,x30
1245 else
1246 o(0x910383be); // add x30,x29,#224
1247 o(0xf900001e | r << 5); // str x30,[x(r)]
1249 if (arm64_func_va_list_gr_offs) {
1250 if (arm64_func_va_list_stack)
1251 o(0x910383be); // add x30,x29,#224
1252 o(0xf900041e | r << 5); // str x30,[x(r),#8]
1255 if (arm64_func_va_list_vr_offs) {
1256 o(0x910243be); // add x30,x29,#144
1257 o(0xf900081e | r << 5); // str x30,[x(r),#16]
1260 arm64_movimm(30, arm64_func_va_list_gr_offs);
1261 o(0xb900181e | r << 5); // str w30,[x(r),#24]
1263 arm64_movimm(30, arm64_func_va_list_vr_offs);
1264 o(0xb9001c1e | r << 5); // str w30,[x(r),#28]
1266 --vtop;
1269 ST_FUNC void gen_va_arg(CType *t)
1271 int align, size = type_size(t, &align);
1272 int fsize, hfa = arm64_hfa(t, (int *)&fsize);
1273 uint32_t r0, r1;
1275 if (is_float(t->t)) {
1276 hfa = 1;
1277 fsize = size;
1280 gaddrof();
1281 r0 = intr(gv(RC_INT));
1282 r1 = get_reg(RC_INT);
1283 vtop[0].r = r1 | VT_LVAL;
1284 r1 = intr(r1);
1286 if (!hfa) {
1287 uint32_t n = size > 16 ? 8 : (size + 7) & -8;
1288 o(0xb940181e | r0 << 5); // ldr w30,[x(r0),#24] // __gr_offs
1289 if (align == 16) {
1290 assert(0); // this path untested but needed for __uint128_t
1291 o(0x11003fde); // add w30,w30,#15
1292 o(0x121c6fde); // and w30,w30,#-16
1294 o(0x310003c0 | r1 | n << 10); // adds w(r1),w30,#(n)
1295 o(0x540000ad); // b.le .+20
1296 o(0xf9400000 | r1 | r0 << 5); // ldr x(r1),[x(r0)] // __stack
1297 o(0x9100001e | r1 << 5 | n << 10); // add x30,x(r1),#(n)
1298 o(0xf900001e | r0 << 5); // str x30,[x(r0)] // __stack
1299 o(0x14000004); // b .+16
1300 o(0xb9001800 | r1 | r0 << 5); // str w(r1),[x(r0),#24] // __gr_offs
1301 o(0xf9400400 | r1 | r0 << 5); // ldr x(r1),[x(r0),#8] // __gr_top
1302 o(0x8b3ec000 | r1 | r1 << 5); // add x(r1),x(r1),w30,sxtw
1303 if (size > 16)
1304 o(0xf9400000 | r1 | r1 << 5); // ldr x(r1),[x(r1)]
1306 else {
1307 uint32_t rsz = hfa << 4;
1308 uint32_t ssz = (size + 7) & -(uint32_t)8;
1309 uint32_t b1, b2;
1310 o(0xb9401c1e | r0 << 5); // ldr w30,[x(r0),#28] // __vr_offs
1311 o(0x310003c0 | r1 | rsz << 10); // adds w(r1),w30,#(rsz)
1312 b1 = ind; o(0x5400000d); // b.le lab1
1313 o(0xf9400000 | r1 | r0 << 5); // ldr x(r1),[x(r0)] // __stack
1314 if (fsize == 16) {
1315 o(0x91003c00 | r1 | r1 << 5); // add x(r1),x(r1),#15
1316 o(0x927cec00 | r1 | r1 << 5); // and x(r1),x(r1),#-16
1318 o(0x9100001e | r1 << 5 | ssz << 10); // add x30,x(r1),#(ssz)
1319 o(0xf900001e | r0 << 5); // str x30,[x(r0)] // __stack
1320 b2 = ind; o(0x14000000); // b lab2
1321 // lab1:
1322 write32le(cur_text_section->data + b1, 0x5400000d | (ind - b1) << 3);
1323 o(0xb9001c00 | r1 | r0 << 5); // str w(r1),[x(r0),#28] // __vr_offs
1324 o(0xf9400800 | r1 | r0 << 5); // ldr x(r1),[x(r0),#16] // __vr_top
1325 if (hfa == 1 || fsize == 16)
1326 o(0x8b3ec000 | r1 | r1 << 5); // add x(r1),x(r1),w30,sxtw
1327 else {
1328 // We need to change the layout of this HFA.
1329 // Get some space on the stack using global variable "loc":
1330 loc = (loc - size) & -(uint32_t)align;
1331 o(0x8b3ec000 | 30 | r1 << 5); // add x30,x(r1),w30,sxtw
1332 arm64_movimm(r1, loc);
1333 o(0x8b0003a0 | r1 | r1 << 16); // add x(r1),x29,x(r1)
1334 o(0x4c402bdc | (uint32_t)fsize << 7 |
1335 (uint32_t)(hfa == 2) << 15 |
1336 (uint32_t)(hfa == 3) << 14); // ld1 {v28.(4s|2d),...},[x30]
1337 o(0x0d00801c | r1 << 5 | (fsize == 8) << 10 |
1338 (uint32_t)(hfa != 2) << 13 |
1339 (uint32_t)(hfa != 3) << 21); // st(hfa) {v28.(s|d),...}[0],[x(r1)]
1341 // lab2:
1342 write32le(cur_text_section->data + b2, 0x14000000 | (ind - b2) >> 2);
1346 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret,
1347 int *align, int *regsize)
1349 return 0;
1352 ST_FUNC void gfunc_return(CType *func_type)
1354 CType *t = func_type;
1355 unsigned long a;
1357 arm64_pcs(0, &t, &a);
1358 switch (a) {
1359 case -1:
1360 break;
1361 case 0:
1362 if ((func_type->t & VT_BTYPE) == VT_STRUCT) {
1363 int align, size = type_size(func_type, &align);
1364 gaddrof();
1365 gv(RC_R(0));
1366 arm64_ldrs(0, size);
1368 else
1369 gv(RC_IRET);
1370 break;
1371 case 1: {
1372 CType type = *func_type;
1373 mk_pointer(&type);
1374 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
1375 indir();
1376 vswap();
1377 vstore();
1378 break;
1380 case 16:
1381 if ((func_type->t & VT_BTYPE) == VT_STRUCT) {
1382 uint32_t j, sz, n = arm64_hfa(&vtop->type, (int *)&sz);
1383 gaddrof();
1384 gv(RC_R(0));
1385 for (j = 0; j < n; j++)
1386 o(0x3d400000 |
1387 (sz & 16) << 19 | -(sz & 8) << 27 | (sz & 4) << 29 |
1388 j | j << 10); // ldr ([sdq])(*),[x0,#(j * sz)]
1390 else
1391 gv(RC_FRET);
1392 break;
1393 default:
1394 assert(0);
1396 vtop--;
1399 ST_FUNC void gfunc_epilog(void)
1401 #ifdef CONFIG_TCC_BCHECK
1402 if (tcc_state->do_bounds_check)
1403 gen_bounds_epilog();
1404 #endif
1406 if (loc) {
1407 // Insert instructions to subtract size of stack frame from SP.
1408 unsigned char *ptr = cur_text_section->data + arm64_func_sub_sp_offset;
1409 uint64_t diff = (-loc + 15) & ~15;
1410 if (!(diff >> 24)) {
1411 if (diff & 0xfff) // sub sp,sp,#(diff & 0xfff)
1412 write32le(ptr, 0xd10003ff | (diff & 0xfff) << 10);
1413 if (diff >> 12) // sub sp,sp,#(diff >> 12),lsl #12
1414 write32le(ptr + 4, 0xd14003ff | (diff >> 12) << 10);
1416 else {
1417 // In this case we may subtract more than necessary,
1418 // but always less than 17/16 of what we were aiming for.
1419 int i = 0;
1420 int j = 0;
1421 while (diff >> 20) {
1422 diff = (diff + 0xffff) >> 16;
1423 ++i;
1425 while (diff >> 16) {
1426 diff = (diff + 1) >> 1;
1427 ++j;
1429 write32le(ptr, 0xd2800010 | diff << 5 | i << 21);
1430 // mov x16,#(diff),lsl #(16 * i)
1431 write32le(ptr + 4, 0xcb3063ff | j << 10);
1432 // sub sp,sp,x16,lsl #(j)
1435 o(0x910003bf); // mov sp,x29
1436 o(0xa8ce7bfd); // ldp x29,x30,[sp],#224
1438 o(0xd65f03c0); // ret
1441 ST_FUNC void gen_fill_nops(int bytes)
1443 if ((bytes & 3))
1444 tcc_error("alignment of code section not multiple of 4");
1445 while (bytes > 0) {
1446 o(0xd503201f); // nop
1447 bytes -= 4;
1451 // Generate forward branch to label:
1452 ST_FUNC int gjmp(int t)
1454 int r = ind;
1455 if (nocode_wanted)
1456 return t;
1457 o(t);
1458 return r;
1461 // Generate branch to known address:
1462 ST_FUNC void gjmp_addr(int a)
1464 assert(a - ind + 0x8000000 < 0x10000000);
1465 o(0x14000000 | ((a - ind) >> 2 & 0x3ffffff));
1468 ST_FUNC int gjmp_append(int n, int t)
1470 void *p;
1471 /* insert vtop->c jump list in t */
1472 if (n) {
1473 uint32_t n1 = n, n2;
1474 while ((n2 = read32le(p = cur_text_section->data + n1)))
1475 n1 = n2;
1476 write32le(p, t);
1477 t = n;
1479 return t;
1482 void arm64_vset_VT_CMP(int op)
1484 if (op >= TOK_ULT && op <= TOK_GT) {
1485 vtop->cmp_r = vtop->r;
1486 vset_VT_CMP(0x80);
1490 static void arm64_gen_opil(int op, uint32_t l);
1492 static void arm64_load_cmp(int r, SValue *sv)
1494 sv->r = sv->cmp_r;
1495 if (sv->c.i & 1) {
1496 vpushi(1);
1497 arm64_gen_opil('^', 0);
1499 if (r != sv->r) {
1500 load(r, sv);
1501 sv->r = r;
1505 ST_FUNC int gjmp_cond(int op, int t)
1507 int bt = vtop->type.t & VT_BTYPE;
1509 int inv = op & 1;
1510 vtop->r = vtop->cmp_r;
1512 if (bt == VT_LDOUBLE) {
1513 uint32_t a, b, f = fltr(gv(RC_FLOAT));
1514 a = get_reg(RC_INT);
1515 vpushi(0);
1516 vtop[0].r = a;
1517 b = get_reg(RC_INT);
1518 a = intr(a);
1519 b = intr(b);
1520 o(0x4e083c00 | a | f << 5); // mov x(a),v(f).d[0]
1521 o(0x4e183c00 | b | f << 5); // mov x(b),v(f).d[1]
1522 o(0xaa000400 | a | a << 5 | b << 16); // orr x(a),x(a),x(b),lsl #1
1523 o(0xb4000040 | a | !!inv << 24); // cbz/cbnz x(a),.+8
1524 --vtop;
1526 else if (bt == VT_FLOAT || bt == VT_DOUBLE) {
1527 uint32_t a = fltr(gv(RC_FLOAT));
1528 o(0x1e202008 | a << 5 | (bt != VT_FLOAT) << 22); // fcmp
1529 o(0x54000040 | !!inv); // b.eq/b.ne .+8
1531 else {
1532 uint32_t ll = (bt == VT_PTR || bt == VT_LLONG);
1533 uint32_t a = intr(gv(RC_INT));
1534 o(0x34000040 | a | !!inv << 24 | ll << 31); // cbz/cbnz wA,.+8
1536 return gjmp(t);
1539 static int arm64_iconst(uint64_t *val, SValue *sv)
1541 if ((sv->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
1542 return 0;
1543 if (val) {
1544 int t = sv->type.t;
1545 int bt = t & VT_BTYPE;
1546 *val = ((bt == VT_LLONG || bt == VT_PTR) ? sv->c.i :
1547 (uint32_t)sv->c.i |
1548 (t & VT_UNSIGNED ? 0 : -(sv->c.i & 0x80000000)));
1550 return 1;
1553 static int arm64_gen_opic(int op, uint32_t l, int rev, uint64_t val,
1554 uint32_t x, uint32_t a)
1556 if (op == '-' && !rev) {
1557 val = -val;
1558 op = '+';
1560 val = l ? val : (uint32_t)val;
1562 switch (op) {
1564 case '+': {
1565 uint32_t s = l ? val >> 63 : val >> 31;
1566 val = s ? -val : val;
1567 val = l ? val : (uint32_t)val;
1568 if (!(val & ~(uint64_t)0xfff))
1569 o(0x11000000 | l << 31 | s << 30 | x | a << 5 | val << 10);
1570 else if (!(val & ~(uint64_t)0xfff000))
1571 o(0x11400000 | l << 31 | s << 30 | x | a << 5 | val >> 12 << 10);
1572 else {
1573 arm64_movimm(30, val); // use x30
1574 o(0x0b1e0000 | l << 31 | s << 30 | x | a << 5);
1576 return 1;
1579 case '-':
1580 if (!val)
1581 o(0x4b0003e0 | l << 31 | x | a << 16); // neg
1582 else if (val == (l ? (uint64_t)-1 : (uint32_t)-1))
1583 o(0x2a2003e0 | l << 31 | x | a << 16); // mvn
1584 else {
1585 arm64_movimm(30, val); // use x30
1586 o(0x4b0003c0 | l << 31 | x | a << 16); // sub
1588 return 1;
1590 case '^':
1591 if (val == -1 || (val == 0xffffffff && !l)) {
1592 o(0x2a2003e0 | l << 31 | x | a << 16); // mvn
1593 return 1;
1595 // fall through
1596 case '&':
1597 case '|': {
1598 int e = arm64_encode_bimm64(l ? val : val | val << 32);
1599 if (e < 0)
1600 return 0;
1601 o((op == '&' ? 0x12000000 :
1602 op == '|' ? 0x32000000 : 0x52000000) |
1603 l << 31 | x | a << 5 | (uint32_t)e << 10);
1604 return 1;
1607 case TOK_SAR:
1608 case TOK_SHL:
1609 case TOK_SHR: {
1610 uint32_t n = 32 << l;
1611 val = val & (n - 1);
1612 if (rev)
1613 return 0;
1614 if (!val)
1615 assert(0);
1616 else if (op == TOK_SHL)
1617 o(0x53000000 | l << 31 | l << 22 | x | a << 5 |
1618 (n - val) << 16 | (n - 1 - val) << 10); // lsl
1619 else
1620 o(0x13000000 | (op == TOK_SHR) << 30 | l << 31 | l << 22 |
1621 x | a << 5 | val << 16 | (n - 1) << 10); // lsr/asr
1622 return 1;
1626 return 0;
1629 static void arm64_gen_opil(int op, uint32_t l)
1631 uint32_t x, a, b;
1633 // Special treatment for operations with a constant operand:
1635 uint64_t val;
1636 int rev = 1;
1638 if (arm64_iconst(0, &vtop[0])) {
1639 vswap();
1640 rev = 0;
1642 if (arm64_iconst(&val, &vtop[-1])) {
1643 gv(RC_INT);
1644 a = intr(vtop[0].r);
1645 --vtop;
1646 x = get_reg(RC_INT);
1647 ++vtop;
1648 if (arm64_gen_opic(op, l, rev, val, intr(x), a)) {
1649 vtop[0].r = x;
1650 vswap();
1651 --vtop;
1652 return;
1655 if (!rev)
1656 vswap();
1659 gv2(RC_INT, RC_INT);
1660 assert(vtop[-1].r < VT_CONST && vtop[0].r < VT_CONST);
1661 a = intr(vtop[-1].r);
1662 b = intr(vtop[0].r);
1663 vtop -= 2;
1664 x = get_reg(RC_INT);
1665 ++vtop;
1666 vtop[0].r = x;
1667 x = intr(x);
1669 switch (op) {
1670 case '%':
1671 // Use x30 for quotient:
1672 o(0x1ac00c00 | l << 31 | 30 | a << 5 | b << 16); // sdiv
1673 o(0x1b008000 | l << 31 | x | (uint32_t)30 << 5 |
1674 b << 16 | a << 10); // msub
1675 break;
1676 case '&':
1677 o(0x0a000000 | l << 31 | x | a << 5 | b << 16); // and
1678 break;
1679 case '*':
1680 o(0x1b007c00 | l << 31 | x | a << 5 | b << 16); // mul
1681 break;
1682 case '+':
1683 o(0x0b000000 | l << 31 | x | a << 5 | b << 16); // add
1684 break;
1685 case '-':
1686 o(0x4b000000 | l << 31 | x | a << 5 | b << 16); // sub
1687 break;
1688 case '/':
1689 o(0x1ac00c00 | l << 31 | x | a << 5 | b << 16); // sdiv
1690 break;
1691 case '^':
1692 o(0x4a000000 | l << 31 | x | a << 5 | b << 16); // eor
1693 break;
1694 case '|':
1695 o(0x2a000000 | l << 31 | x | a << 5 | b << 16); // orr
1696 break;
1697 case TOK_EQ:
1698 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1699 o(0x1a9f17e0 | x); // cset wA,eq
1700 break;
1701 case TOK_GE:
1702 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1703 o(0x1a9fb7e0 | x); // cset wA,ge
1704 break;
1705 case TOK_GT:
1706 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1707 o(0x1a9fd7e0 | x); // cset wA,gt
1708 break;
1709 case TOK_LE:
1710 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1711 o(0x1a9fc7e0 | x); // cset wA,le
1712 break;
1713 case TOK_LT:
1714 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1715 o(0x1a9fa7e0 | x); // cset wA,lt
1716 break;
1717 case TOK_NE:
1718 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1719 o(0x1a9f07e0 | x); // cset wA,ne
1720 break;
1721 case TOK_SAR:
1722 o(0x1ac02800 | l << 31 | x | a << 5 | b << 16); // asr
1723 break;
1724 case TOK_SHL:
1725 o(0x1ac02000 | l << 31 | x | a << 5 | b << 16); // lsl
1726 break;
1727 case TOK_SHR:
1728 o(0x1ac02400 | l << 31 | x | a << 5 | b << 16); // lsr
1729 break;
1730 case TOK_UDIV:
1731 case TOK_PDIV:
1732 o(0x1ac00800 | l << 31 | x | a << 5 | b << 16); // udiv
1733 break;
1734 case TOK_UGE:
1735 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1736 o(0x1a9f37e0 | x); // cset wA,cs
1737 break;
1738 case TOK_UGT:
1739 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1740 o(0x1a9f97e0 | x); // cset wA,hi
1741 break;
1742 case TOK_ULT:
1743 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1744 o(0x1a9f27e0 | x); // cset wA,cc
1745 break;
1746 case TOK_ULE:
1747 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1748 o(0x1a9f87e0 | x); // cset wA,ls
1749 break;
1750 case TOK_UMOD:
1751 // Use x30 for quotient:
1752 o(0x1ac00800 | l << 31 | 30 | a << 5 | b << 16); // udiv
1753 o(0x1b008000 | l << 31 | x | (uint32_t)30 << 5 |
1754 b << 16 | a << 10); // msub
1755 break;
1756 default:
1757 assert(0);
1761 ST_FUNC void gen_opi(int op)
1763 arm64_gen_opil(op, 0);
1764 arm64_vset_VT_CMP(op);
1767 ST_FUNC void gen_opl(int op)
1769 arm64_gen_opil(op, 1);
1770 arm64_vset_VT_CMP(op);
1773 ST_FUNC void gen_opf(int op)
1775 uint32_t x, a, b, dbl;
1777 if (vtop[0].type.t == VT_LDOUBLE) {
1778 CType type = vtop[0].type;
1779 int func = 0;
1780 int cond = -1;
1781 switch (op) {
1782 case '*': func = TOK___multf3; break;
1783 case '+': func = TOK___addtf3; break;
1784 case '-': func = TOK___subtf3; break;
1785 case '/': func = TOK___divtf3; break;
1786 case TOK_EQ: func = TOK___eqtf2; cond = 1; break;
1787 case TOK_NE: func = TOK___netf2; cond = 0; break;
1788 case TOK_LT: func = TOK___lttf2; cond = 10; break;
1789 case TOK_GE: func = TOK___getf2; cond = 11; break;
1790 case TOK_LE: func = TOK___letf2; cond = 12; break;
1791 case TOK_GT: func = TOK___gttf2; cond = 13; break;
1792 default: assert(0); break;
1794 vpush_global_sym(&func_old_type, func);
1795 vrott(3);
1796 gfunc_call(2);
1797 vpushi(0);
1798 vtop->r = cond < 0 ? REG_FRET : REG_IRET;
1799 if (cond < 0)
1800 vtop->type = type;
1801 else {
1802 o(0x7100001f); // cmp w0,#0
1803 o(0x1a9f07e0 | (uint32_t)cond << 12); // cset w0,(cond)
1805 return;
1808 dbl = vtop[0].type.t != VT_FLOAT;
1809 gv2(RC_FLOAT, RC_FLOAT);
1810 assert(vtop[-1].r < VT_CONST && vtop[0].r < VT_CONST);
1811 a = fltr(vtop[-1].r);
1812 b = fltr(vtop[0].r);
1813 vtop -= 2;
1814 switch (op) {
1815 case TOK_EQ: case TOK_NE:
1816 case TOK_LT: case TOK_GE: case TOK_LE: case TOK_GT:
1817 x = get_reg(RC_INT);
1818 ++vtop;
1819 vtop[0].r = x;
1820 x = intr(x);
1821 break;
1822 default:
1823 x = get_reg(RC_FLOAT);
1824 ++vtop;
1825 vtop[0].r = x;
1826 x = fltr(x);
1827 break;
1830 switch (op) {
1831 case '*':
1832 o(0x1e200800 | dbl << 22 | x | a << 5 | b << 16); // fmul
1833 break;
1834 case '+':
1835 o(0x1e202800 | dbl << 22 | x | a << 5 | b << 16); // fadd
1836 break;
1837 case '-':
1838 o(0x1e203800 | dbl << 22 | x | a << 5 | b << 16); // fsub
1839 break;
1840 case '/':
1841 o(0x1e201800 | dbl << 22 | x | a << 5 | b << 16); // fdiv
1842 break;
1843 case TOK_EQ:
1844 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1845 o(0x1a9f17e0 | x); // cset w(x),eq
1846 break;
1847 case TOK_GE:
1848 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1849 o(0x1a9fb7e0 | x); // cset w(x),ge
1850 break;
1851 case TOK_GT:
1852 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1853 o(0x1a9fd7e0 | x); // cset w(x),gt
1854 break;
1855 case TOK_LE:
1856 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1857 o(0x1a9f87e0 | x); // cset w(x),ls
1858 break;
1859 case TOK_LT:
1860 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1861 o(0x1a9f57e0 | x); // cset w(x),mi
1862 break;
1863 case TOK_NE:
1864 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1865 o(0x1a9f07e0 | x); // cset w(x),ne
1866 break;
1867 default:
1868 assert(0);
1870 arm64_vset_VT_CMP(op);
1873 // Generate sign extension from 32 to 64 bits:
1874 ST_FUNC void gen_cvt_sxtw(void)
1876 uint32_t r = intr(gv(RC_INT));
1877 o(0x93407c00 | r | r << 5); // sxtw x(r),w(r)
1880 /* char/short to int conversion */
1881 ST_FUNC void gen_cvt_csti(int t)
1883 int r = intr(gv(RC_INT));
1884 o(0x13001c00
1885 | ((t & VT_BTYPE) == VT_SHORT) << 13
1886 | (uint32_t)!!(t & VT_UNSIGNED) << 30
1887 | r | r << 5); // [su]xt[bh] w(r),w(r)
1890 ST_FUNC void gen_cvt_itof(int t)
1892 if (t == VT_LDOUBLE) {
1893 int f = vtop->type.t;
1894 int func = (f & VT_BTYPE) == VT_LLONG ?
1895 (f & VT_UNSIGNED ? TOK___floatunditf : TOK___floatditf) :
1896 (f & VT_UNSIGNED ? TOK___floatunsitf : TOK___floatsitf);
1897 vpush_global_sym(&func_old_type, func);
1898 vrott(2);
1899 gfunc_call(1);
1900 vpushi(0);
1901 vtop->type.t = t;
1902 vtop->r = REG_FRET;
1903 return;
1905 else {
1906 int d, n = intr(gv(RC_INT));
1907 int s = !(vtop->type.t & VT_UNSIGNED);
1908 uint32_t l = ((vtop->type.t & VT_BTYPE) == VT_LLONG);
1909 --vtop;
1910 d = get_reg(RC_FLOAT);
1911 ++vtop;
1912 vtop[0].r = d;
1913 o(0x1e220000 | (uint32_t)!s << 16 |
1914 (uint32_t)(t != VT_FLOAT) << 22 | fltr(d) |
1915 l << 31 | n << 5); // [us]cvtf [sd](d),[wx](n)
1919 ST_FUNC void gen_cvt_ftoi(int t)
1921 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
1922 int func = (t & VT_BTYPE) == VT_LLONG ?
1923 (t & VT_UNSIGNED ? TOK___fixunstfdi : TOK___fixtfdi) :
1924 (t & VT_UNSIGNED ? TOK___fixunstfsi : TOK___fixtfsi);
1925 vpush_global_sym(&func_old_type, func);
1926 vrott(2);
1927 gfunc_call(1);
1928 vpushi(0);
1929 vtop->type.t = t;
1930 vtop->r = REG_IRET;
1931 return;
1933 else {
1934 int d, n = fltr(gv(RC_FLOAT));
1935 uint32_t l = ((vtop->type.t & VT_BTYPE) != VT_FLOAT);
1936 --vtop;
1937 d = get_reg(RC_INT);
1938 ++vtop;
1939 vtop[0].r = d;
1940 o(0x1e380000 |
1941 (uint32_t)!!(t & VT_UNSIGNED) << 16 |
1942 (uint32_t)((t & VT_BTYPE) == VT_LLONG) << 31 | intr(d) |
1943 l << 22 | n << 5); // fcvtz[su] [wx](d),[sd](n)
1947 ST_FUNC void gen_cvt_ftof(int t)
1949 int f = vtop[0].type.t & VT_BTYPE;
1950 assert(t == VT_FLOAT || t == VT_DOUBLE || t == VT_LDOUBLE);
1951 assert(f == VT_FLOAT || f == VT_DOUBLE || f == VT_LDOUBLE);
1952 if (t == f)
1953 return;
1955 if (t == VT_LDOUBLE || f == VT_LDOUBLE) {
1956 int func = (t == VT_LDOUBLE) ?
1957 (f == VT_FLOAT ? TOK___extendsftf2 : TOK___extenddftf2) :
1958 (t == VT_FLOAT ? TOK___trunctfsf2 : TOK___trunctfdf2);
1959 vpush_global_sym(&func_old_type, func);
1960 vrott(2);
1961 gfunc_call(1);
1962 vpushi(0);
1963 vtop->type.t = t;
1964 vtop->r = REG_FRET;
1966 else {
1967 int x, a;
1968 gv(RC_FLOAT);
1969 assert(vtop[0].r < VT_CONST);
1970 a = fltr(vtop[0].r);
1971 --vtop;
1972 x = get_reg(RC_FLOAT);
1973 ++vtop;
1974 vtop[0].r = x;
1975 x = fltr(x);
1977 if (f == VT_FLOAT)
1978 o(0x1e22c000 | x | a << 5); // fcvt d(x),s(a)
1979 else
1980 o(0x1e624000 | x | a << 5); // fcvt s(x),d(a)
1984 ST_FUNC void ggoto(void)
1986 arm64_gen_bl_or_b(1);
1987 --vtop;
1990 ST_FUNC void gen_clear_cache(void)
1992 uint32_t beg, end, dsz, isz, p, lab1, b1;
1993 gv2(RC_INT, RC_INT);
1994 vpushi(0);
1995 vtop->r = get_reg(RC_INT);
1996 vpushi(0);
1997 vtop->r = get_reg(RC_INT);
1998 vpushi(0);
1999 vtop->r = get_reg(RC_INT);
2000 beg = intr(vtop[-4].r); // x0
2001 end = intr(vtop[-3].r); // x1
2002 dsz = intr(vtop[-2].r); // x2
2003 isz = intr(vtop[-1].r); // x3
2004 p = intr(vtop[0].r); // x4
2005 vtop -= 5;
2007 o(0xd53b0020 | isz); // mrs x(isz),ctr_el0
2008 o(0x52800080 | p); // mov w(p),#4
2009 o(0x53104c00 | dsz | isz << 5); // ubfx w(dsz),w(isz),#16,#4
2010 o(0x1ac02000 | dsz | p << 5 | dsz << 16); // lsl w(dsz),w(p),w(dsz)
2011 o(0x12000c00 | isz | isz << 5); // and w(isz),w(isz),#15
2012 o(0x1ac02000 | isz | p << 5 | isz << 16); // lsl w(isz),w(p),w(isz)
2013 o(0x51000400 | p | dsz << 5); // sub w(p),w(dsz),#1
2014 o(0x8a240004 | p | beg << 5 | p << 16); // bic x(p),x(beg),x(p)
2015 b1 = ind; o(0x14000000); // b
2016 lab1 = ind;
2017 o(0xd50b7b20 | p); // dc cvau,x(p)
2018 o(0x8b000000 | p | p << 5 | dsz << 16); // add x(p),x(p),x(dsz)
2019 write32le(cur_text_section->data + b1, 0x14000000 | (ind - b1) >> 2);
2020 o(0xeb00001f | p << 5 | end << 16); // cmp x(p),x(end)
2021 o(0x54ffffa3 | ((lab1 - ind) << 3 & 0xffffe0)); // b.cc lab1
2022 o(0xd5033b9f); // dsb ish
2023 o(0x51000400 | p | isz << 5); // sub w(p),w(isz),#1
2024 o(0x8a240004 | p | beg << 5 | p << 16); // bic x(p),x(beg),x(p)
2025 b1 = ind; o(0x14000000); // b
2026 lab1 = ind;
2027 o(0xd50b7520 | p); // ic ivau,x(p)
2028 o(0x8b000000 | p | p << 5 | isz << 16); // add x(p),x(p),x(isz)
2029 write32le(cur_text_section->data + b1, 0x14000000 | (ind - b1) >> 2);
2030 o(0xeb00001f | p << 5 | end << 16); // cmp x(p),x(end)
2031 o(0x54ffffa3 | ((lab1 - ind) << 3 & 0xffffe0)); // b.cc lab1
2032 o(0xd5033b9f); // dsb ish
2033 o(0xd5033fdf); // isb
2036 ST_FUNC void gen_vla_sp_save(int addr) {
2037 uint32_t r = intr(get_reg(RC_INT));
2038 o(0x910003e0 | r); // mov x(r),sp
2039 arm64_strx(3, r, 29, addr);
2042 ST_FUNC void gen_vla_sp_restore(int addr) {
2043 // Use x30 because this function can be called when there
2044 // is a live return value in x0 but there is nothing on
2045 // the value stack to prevent get_reg from returning x0.
2046 uint32_t r = 30;
2047 arm64_ldrx(0, 3, r, 29, addr);
2048 o(0x9100001f | r << 5); // mov sp,x(r)
2051 ST_FUNC void gen_vla_alloc(CType *type, int align) {
2052 uint32_t r;
2053 #if defined(CONFIG_TCC_BCHECK)
2054 if (tcc_state->do_bounds_check)
2055 vpushv(vtop);
2056 #endif
2057 r = intr(gv(RC_INT));
2058 o(0x91003c00 | r | r << 5); // add x(r),x(r),#15
2059 o(0x927cec00 | r | r << 5); // bic x(r),x(r),#15
2060 o(0xcb2063ff | r << 16); // sub sp,sp,x(r)
2061 vpop();
2062 #if defined(CONFIG_TCC_BCHECK)
2063 if (tcc_state->do_bounds_check) {
2064 vpushi(0);
2065 vtop->r = TREG_R(0);
2066 o(0x910003e0 | vtop->r); // mov r0,sp
2067 vswap();
2068 vpush_global_sym(&func_old_type, TOK___bound_new_region);
2069 vrott(3);
2070 gfunc_call(2);
2071 func_bound_add_epilog = 1;
2073 #endif
2076 /* end of A64 code generator */
2077 /*************************************************************/
2078 #endif
2079 /*************************************************************/