jump optimizations
[tinycc.git] / arm64-gen.c
blob86672de6564fadcda50ae984910755e530bd4c82
1 /*
2 * A64 code generator for TCC
4 * Copyright (c) 2014-2015 Edmund Grimley Evans
6 * Copying and distribution of this file, with or without modification,
7 * are permitted in any medium without royalty provided the copyright
8 * notice and this notice are preserved. This file is offered as-is,
9 * without any warranty.
12 #ifdef TARGET_DEFS_ONLY
14 // Number of registers available to allocator:
15 #define NB_REGS 28 // x0-x18, x30, v0-v7
17 #define TREG_R(x) (x) // x = 0..18
18 #define TREG_R30 19
19 #define TREG_F(x) (x + 20) // x = 0..7
21 // Register classes sorted from more general to more precise:
22 #define RC_INT (1 << 0)
23 #define RC_FLOAT (1 << 1)
24 #define RC_R(x) (1 << (2 + (x))) // x = 0..18
25 #define RC_R30 (1 << 21)
26 #define RC_F(x) (1 << (22 + (x))) // x = 0..7
28 #define RC_IRET (RC_R(0)) // int return register class
29 #define RC_FRET (RC_F(0)) // float return register class
31 #define REG_IRET (TREG_R(0)) // int return register number
32 #define REG_FRET (TREG_F(0)) // float return register number
34 #define PTR_SIZE 8
36 #define LDOUBLE_SIZE 16
37 #define LDOUBLE_ALIGN 16
39 #define MAX_ALIGN 16
41 #define CHAR_IS_UNSIGNED
43 /******************************************************/
44 #else /* ! TARGET_DEFS_ONLY */
45 /******************************************************/
46 #include "tcc.h"
47 #include <assert.h>
49 ST_DATA const int reg_classes[NB_REGS] = {
50 RC_INT | RC_R(0),
51 RC_INT | RC_R(1),
52 RC_INT | RC_R(2),
53 RC_INT | RC_R(3),
54 RC_INT | RC_R(4),
55 RC_INT | RC_R(5),
56 RC_INT | RC_R(6),
57 RC_INT | RC_R(7),
58 RC_INT | RC_R(8),
59 RC_INT | RC_R(9),
60 RC_INT | RC_R(10),
61 RC_INT | RC_R(11),
62 RC_INT | RC_R(12),
63 RC_INT | RC_R(13),
64 RC_INT | RC_R(14),
65 RC_INT | RC_R(15),
66 RC_INT | RC_R(16),
67 RC_INT | RC_R(17),
68 RC_INT | RC_R(18),
69 RC_R30, // not in RC_INT as we make special use of x30
70 RC_FLOAT | RC_F(0),
71 RC_FLOAT | RC_F(1),
72 RC_FLOAT | RC_F(2),
73 RC_FLOAT | RC_F(3),
74 RC_FLOAT | RC_F(4),
75 RC_FLOAT | RC_F(5),
76 RC_FLOAT | RC_F(6),
77 RC_FLOAT | RC_F(7)
80 #define IS_FREG(x) ((x) >= TREG_F(0))
82 static uint32_t intr(int r)
84 assert(TREG_R(0) <= r && r <= TREG_R30);
85 return r < TREG_R30 ? r : 30;
88 static uint32_t fltr(int r)
90 assert(TREG_F(0) <= r && r <= TREG_F(7));
91 return r - TREG_F(0);
94 #define dprintf(x) ((void)(tcc_state->verbose == 2 && printf x))
95 //#define dprintf(x)
97 // Add an instruction to text section:
98 ST_FUNC void o(unsigned int c)
100 int ind1 = ind + 4;
101 if (nocode_wanted)
102 return;
103 if (ind1 > cur_text_section->data_allocated)
104 section_realloc(cur_text_section, ind1);
105 write32le(cur_text_section->data + ind, c);
106 dprintf(("o %04x : %08x\n", ind, c)); //gr
107 ind = ind1;
110 static int arm64_encode_bimm64(uint64_t x)
112 int neg = x & 1;
113 int rep, pos, len;
115 if (neg)
116 x = ~x;
117 if (!x)
118 return -1;
120 if (x >> 2 == (x & (((uint64_t)1 << (64 - 2)) - 1)))
121 rep = 2, x &= ((uint64_t)1 << 2) - 1;
122 else if (x >> 4 == (x & (((uint64_t)1 << (64 - 4)) - 1)))
123 rep = 4, x &= ((uint64_t)1 << 4) - 1;
124 else if (x >> 8 == (x & (((uint64_t)1 << (64 - 8)) - 1)))
125 rep = 8, x &= ((uint64_t)1 << 8) - 1;
126 else if (x >> 16 == (x & (((uint64_t)1 << (64 - 16)) - 1)))
127 rep = 16, x &= ((uint64_t)1 << 16) - 1;
128 else if (x >> 32 == (x & (((uint64_t)1 << (64 - 32)) - 1)))
129 rep = 32, x &= ((uint64_t)1 << 32) - 1;
130 else
131 rep = 64;
133 pos = 0;
134 if (!(x & (((uint64_t)1 << 32) - 1))) x >>= 32, pos += 32;
135 if (!(x & (((uint64_t)1 << 16) - 1))) x >>= 16, pos += 16;
136 if (!(x & (((uint64_t)1 << 8) - 1))) x >>= 8, pos += 8;
137 if (!(x & (((uint64_t)1 << 4) - 1))) x >>= 4, pos += 4;
138 if (!(x & (((uint64_t)1 << 2) - 1))) x >>= 2, pos += 2;
139 if (!(x & (((uint64_t)1 << 1) - 1))) x >>= 1, pos += 1;
141 len = 0;
142 if (!(~x & (((uint64_t)1 << 32) - 1))) x >>= 32, len += 32;
143 if (!(~x & (((uint64_t)1 << 16) - 1))) x >>= 16, len += 16;
144 if (!(~x & (((uint64_t)1 << 8) - 1))) x >>= 8, len += 8;
145 if (!(~x & (((uint64_t)1 << 4) - 1))) x >>= 4, len += 4;
146 if (!(~x & (((uint64_t)1 << 2) - 1))) x >>= 2, len += 2;
147 if (!(~x & (((uint64_t)1 << 1) - 1))) x >>= 1, len += 1;
149 if (x)
150 return -1;
151 if (neg) {
152 pos = (pos + len) & (rep - 1);
153 len = rep - len;
155 return ((0x1000 & rep << 6) | (((rep - 1) ^ 31) << 1 & 63) |
156 ((rep - pos) & (rep - 1)) << 6 | (len - 1));
159 static uint32_t arm64_movi(int r, uint64_t x)
161 uint64_t m = 0xffff;
162 int e;
163 if (!(x & ~m))
164 return 0x52800000 | r | x << 5; // movz w(r),#(x)
165 if (!(x & ~(m << 16)))
166 return 0x52a00000 | r | x >> 11; // movz w(r),#(x >> 16),lsl #16
167 if (!(x & ~(m << 32)))
168 return 0xd2c00000 | r | x >> 27; // movz x(r),#(x >> 32),lsl #32
169 if (!(x & ~(m << 48)))
170 return 0xd2e00000 | r | x >> 43; // movz x(r),#(x >> 48),lsl #48
171 if ((x & ~m) == m << 16)
172 return (0x12800000 | r |
173 (~x << 5 & 0x1fffe0)); // movn w(r),#(~x)
174 if ((x & ~(m << 16)) == m)
175 return (0x12a00000 | r |
176 (~x >> 11 & 0x1fffe0)); // movn w(r),#(~x >> 16),lsl #16
177 if (!~(x | m))
178 return (0x92800000 | r |
179 (~x << 5 & 0x1fffe0)); // movn x(r),#(~x)
180 if (!~(x | m << 16))
181 return (0x92a00000 | r |
182 (~x >> 11 & 0x1fffe0)); // movn x(r),#(~x >> 16),lsl #16
183 if (!~(x | m << 32))
184 return (0x92c00000 | r |
185 (~x >> 27 & 0x1fffe0)); // movn x(r),#(~x >> 32),lsl #32
186 if (!~(x | m << 48))
187 return (0x92e00000 | r |
188 (~x >> 43 & 0x1fffe0)); // movn x(r),#(~x >> 32),lsl #32
189 if (!(x >> 32) && (e = arm64_encode_bimm64(x | x << 32)) >= 0)
190 return 0x320003e0 | r | (uint32_t)e << 10; // movi w(r),#(x)
191 if ((e = arm64_encode_bimm64(x)) >= 0)
192 return 0xb20003e0 | r | (uint32_t)e << 10; // movi x(r),#(x)
193 return 0;
196 static void arm64_movimm(int r, uint64_t x)
198 uint32_t i;
199 if ((i = arm64_movi(r, x)))
200 o(i); // a single MOV
201 else {
202 // MOVZ/MOVN and 1-3 MOVKs
203 int z = 0, m = 0;
204 uint32_t mov1 = 0xd2800000; // movz
205 uint64_t x1 = x;
206 for (i = 0; i < 64; i += 16) {
207 z += !(x >> i & 0xffff);
208 m += !(~x >> i & 0xffff);
210 if (m > z) {
211 x1 = ~x;
212 mov1 = 0x92800000; // movn
214 for (i = 0; i < 64; i += 16)
215 if (x1 >> i & 0xffff) {
216 o(mov1 | r | (x1 >> i & 0xffff) << 5 | i << 17);
217 // movz/movn x(r),#(*),lsl #(i)
218 break;
220 for (i += 16; i < 64; i += 16)
221 if (x1 >> i & 0xffff)
222 o(0xf2800000 | r | (x >> i & 0xffff) << 5 | i << 17);
223 // movk x(r),#(*),lsl #(i)
227 // Patch all branches in list pointed to by t to branch to a:
228 ST_FUNC void gsym_addr(int t_, int a_)
230 uint32_t t = t_;
231 uint32_t a = a_;
232 while (t) {
233 unsigned char *ptr = cur_text_section->data + t;
234 uint32_t next = read32le(ptr);
235 if (a - t + 0x8000000 >= 0x10000000)
236 tcc_error("branch out of range");
237 write32le(ptr, (a - t == 4 ? 0xd503201f : // nop
238 0x14000000 | ((a - t) >> 2 & 0x3ffffff))); // b
239 dprintf((". gsym TARG=%04x ADDR=%04x\n", t, a)); //gr
240 t = next;
244 static int arm64_type_size(int t)
246 switch (t & VT_BTYPE) {
247 case VT_INT: return 2;
248 case VT_BYTE: return 0;
249 case VT_SHORT: return 1;
250 case VT_PTR: return 3;
251 case VT_FUNC: return 3;
252 case VT_FLOAT: return 2;
253 case VT_DOUBLE: return 3;
254 case VT_LDOUBLE: return 4;
255 case VT_BOOL: return 0;
256 case VT_LLONG: return 3;
258 assert(0);
259 return 0;
262 static void arm64_spoff(int reg, uint64_t off)
264 uint32_t sub = off >> 63;
265 if (sub)
266 off = -off;
267 if (off < 4096)
268 o(0x910003e0 | sub << 30 | reg | off << 10);
269 // (add|sub) x(reg),sp,#(off)
270 else {
271 arm64_movimm(30, off); // use x30 for offset
272 o(0x8b3e63e0 | sub << 30 | reg); // (add|sub) x(reg),sp,x30
276 static void arm64_ldrx(int sg, int sz_, int dst, int bas, uint64_t off)
278 uint32_t sz = sz_;
279 if (sz >= 2)
280 sg = 0;
281 if (!(off & ~((uint32_t)0xfff << sz)))
282 o(0x39400000 | dst | bas << 5 | off << (10 - sz) |
283 (uint32_t)!!sg << 23 | sz << 30); // ldr(*) x(dst),[x(bas),#(off)]
284 else if (off < 256 || -off <= 256)
285 o(0x38400000 | dst | bas << 5 | (off & 511) << 12 |
286 (uint32_t)!!sg << 23 | sz << 30); // ldur(*) x(dst),[x(bas),#(off)]
287 else {
288 arm64_movimm(30, off); // use x30 for offset
289 o(0x38206800 | dst | bas << 5 | (uint32_t)30 << 16 |
290 (uint32_t)(!!sg + 1) << 22 | sz << 30); // ldr(*) x(dst),[x(bas),x30]
294 static void arm64_ldrv(int sz_, int dst, int bas, uint64_t off)
296 uint32_t sz = sz_;
297 if (!(off & ~((uint32_t)0xfff << sz)))
298 o(0x3d400000 | dst | bas << 5 | off << (10 - sz) |
299 (sz & 4) << 21 | (sz & 3) << 30); // ldr (s|d|q)(dst),[x(bas),#(off)]
300 else if (off < 256 || -off <= 256)
301 o(0x3c400000 | dst | bas << 5 | (off & 511) << 12 |
302 (sz & 4) << 21 | (sz & 3) << 30); // ldur (s|d|q)(dst),[x(bas),#(off)]
303 else {
304 arm64_movimm(30, off); // use x30 for offset
305 o(0x3c606800 | dst | bas << 5 | (uint32_t)30 << 16 |
306 sz << 30 | (sz & 4) << 21); // ldr (s|d|q)(dst),[x(bas),x30]
310 static void arm64_ldrs(int reg_, int size)
312 uint32_t reg = reg_;
313 // Use x30 for intermediate value in some cases.
314 switch (size) {
315 default: assert(0); break;
316 case 1:
317 arm64_ldrx(0, 0, reg, reg, 0);
318 break;
319 case 2:
320 arm64_ldrx(0, 1, reg, reg, 0);
321 break;
322 case 3:
323 arm64_ldrx(0, 1, 30, reg, 0);
324 arm64_ldrx(0, 0, reg, reg, 2);
325 o(0x2a0043c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #16
326 break;
327 case 4:
328 arm64_ldrx(0, 2, reg, reg, 0);
329 break;
330 case 5:
331 arm64_ldrx(0, 2, 30, reg, 0);
332 arm64_ldrx(0, 0, reg, reg, 4);
333 o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32
334 break;
335 case 6:
336 arm64_ldrx(0, 2, 30, reg, 0);
337 arm64_ldrx(0, 1, reg, reg, 4);
338 o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32
339 break;
340 case 7:
341 arm64_ldrx(0, 2, 30, reg, 0);
342 arm64_ldrx(0, 2, reg, reg, 3);
343 o(0x53087c00 | reg | reg << 5); // lsr w(reg), w(reg), #8
344 o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32
345 break;
346 case 8:
347 arm64_ldrx(0, 3, reg, reg, 0);
348 break;
349 case 9:
350 arm64_ldrx(0, 0, reg + 1, reg, 8);
351 arm64_ldrx(0, 3, reg, reg, 0);
352 break;
353 case 10:
354 arm64_ldrx(0, 1, reg + 1, reg, 8);
355 arm64_ldrx(0, 3, reg, reg, 0);
356 break;
357 case 11:
358 arm64_ldrx(0, 2, reg + 1, reg, 7);
359 o(0x53087c00 | (reg+1) | (reg+1) << 5); // lsr w(reg+1), w(reg+1), #8
360 arm64_ldrx(0, 3, reg, reg, 0);
361 break;
362 case 12:
363 arm64_ldrx(0, 2, reg + 1, reg, 8);
364 arm64_ldrx(0, 3, reg, reg, 0);
365 break;
366 case 13:
367 arm64_ldrx(0, 3, reg + 1, reg, 5);
368 o(0xd358fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #24
369 arm64_ldrx(0, 3, reg, reg, 0);
370 break;
371 case 14:
372 arm64_ldrx(0, 3, reg + 1, reg, 6);
373 o(0xd350fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #16
374 arm64_ldrx(0, 3, reg, reg, 0);
375 break;
376 case 15:
377 arm64_ldrx(0, 3, reg + 1, reg, 7);
378 o(0xd348fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #8
379 arm64_ldrx(0, 3, reg, reg, 0);
380 break;
381 case 16:
382 o(0xa9400000 | reg | (reg+1) << 10 | reg << 5);
383 // ldp x(reg),x(reg+1),[x(reg)]
384 break;
388 static void arm64_strx(int sz_, int dst, int bas, uint64_t off)
390 uint32_t sz = sz_;
391 if (!(off & ~((uint32_t)0xfff << sz)))
392 o(0x39000000 | dst | bas << 5 | off << (10 - sz) | sz << 30);
393 // str(*) x(dst),[x(bas],#(off)]
394 else if (off < 256 || -off <= 256)
395 o(0x38000000 | dst | bas << 5 | (off & 511) << 12 | sz << 30);
396 // stur(*) x(dst),[x(bas],#(off)]
397 else {
398 arm64_movimm(30, off); // use x30 for offset
399 o(0x38206800 | dst | bas << 5 | (uint32_t)30 << 16 | sz << 30);
400 // str(*) x(dst),[x(bas),x30]
404 static void arm64_strv(int sz_, int dst, int bas, uint64_t off)
406 uint32_t sz = sz_;
407 if (!(off & ~((uint32_t)0xfff << sz)))
408 o(0x3d000000 | dst | bas << 5 | off << (10 - sz) |
409 (sz & 4) << 21 | (sz & 3) << 30); // str (s|d|q)(dst),[x(bas),#(off)]
410 else if (off < 256 || -off <= 256)
411 o(0x3c000000 | dst | bas << 5 | (off & 511) << 12 |
412 (sz & 4) << 21 | (sz & 3) << 30); // stur (s|d|q)(dst),[x(bas),#(off)]
413 else {
414 arm64_movimm(30, off); // use x30 for offset
415 o(0x3c206800 | dst | bas << 5 | (uint32_t)30 << 16 |
416 sz << 30 | (sz & 4) << 21); // str (s|d|q)(dst),[x(bas),x30]
420 static void arm64_sym(int r, Sym *sym, unsigned long addend)
422 // Currently TCC's linker does not generate COPY relocations for
423 // STT_OBJECTs when tcc is invoked with "-run". This typically
424 // results in "R_AARCH64_ADR_PREL_PG_HI21 relocation failed" when
425 // a program refers to stdin. A workaround is to avoid that
426 // relocation and use only relocations with unlimited range.
427 int avoid_adrp = 1;
429 if (avoid_adrp || sym->a.weak) {
430 // (GCC uses a R_AARCH64_ABS64 in this case.)
431 greloca(cur_text_section, sym, ind, R_AARCH64_MOVW_UABS_G0_NC, addend);
432 o(0xd2800000 | r); // mov x(rt),#0,lsl #0
433 greloca(cur_text_section, sym, ind, R_AARCH64_MOVW_UABS_G1_NC, addend);
434 o(0xf2a00000 | r); // movk x(rt),#0,lsl #16
435 greloca(cur_text_section, sym, ind, R_AARCH64_MOVW_UABS_G2_NC, addend);
436 o(0xf2c00000 | r); // movk x(rt),#0,lsl #32
437 greloca(cur_text_section, sym, ind, R_AARCH64_MOVW_UABS_G3, addend);
438 o(0xf2e00000 | r); // movk x(rt),#0,lsl #48
440 else {
441 greloca(cur_text_section, sym, ind, R_AARCH64_ADR_PREL_PG_HI21, addend);
442 o(0x90000000 | r);
443 greloca(cur_text_section, sym, ind, R_AARCH64_ADD_ABS_LO12_NC, addend);
444 o(0x91000000 | r | r << 5);
448 static void arm64_load_cmp(int r, SValue *sv);
450 ST_FUNC void load(int r, SValue *sv)
452 int svtt = sv->type.t;
453 int svr = sv->r & ~VT_LVAL_TYPE;
454 int svrv = svr & VT_VALMASK;
455 uint64_t svcul = (uint32_t)sv->c.i;
456 svcul = svcul >> 31 & 1 ? svcul - ((uint64_t)1 << 32) : svcul;
458 if (svr == (VT_LOCAL | VT_LVAL)) {
459 if (IS_FREG(r))
460 arm64_ldrv(arm64_type_size(svtt), fltr(r), 29, svcul);
461 else
462 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
463 intr(r), 29, svcul);
464 return;
467 if ((svr & ~VT_VALMASK) == VT_LVAL && svrv < VT_CONST) {
468 if (IS_FREG(r))
469 arm64_ldrv(arm64_type_size(svtt), fltr(r), intr(svrv), 0);
470 else
471 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
472 intr(r), intr(svrv), 0);
473 return;
476 if (svr == (VT_CONST | VT_LVAL | VT_SYM)) {
477 arm64_sym(30, sv->sym, svcul); // use x30 for address
478 if (IS_FREG(r))
479 arm64_ldrv(arm64_type_size(svtt), fltr(r), 30, 0);
480 else
481 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
482 intr(r), 30, 0);
483 return;
486 if (svr == (VT_CONST | VT_SYM)) {
487 arm64_sym(intr(r), sv->sym, svcul);
488 return;
491 if (svr == VT_CONST) {
492 if ((svtt & VT_BTYPE) != VT_VOID)
493 arm64_movimm(intr(r), arm64_type_size(svtt) == 3 ?
494 sv->c.i : (uint32_t)svcul);
495 return;
498 if (svr < VT_CONST) {
499 if (IS_FREG(r) && IS_FREG(svr))
500 if (svtt == VT_LDOUBLE)
501 o(0x4ea01c00 | fltr(r) | fltr(svr) << 5);
502 // mov v(r).16b,v(svr).16b
503 else
504 o(0x1e604000 | fltr(r) | fltr(svr) << 5); // fmov d(r),d(svr)
505 else if (!IS_FREG(r) && !IS_FREG(svr))
506 o(0xaa0003e0 | intr(r) | intr(svr) << 16); // mov x(r),x(svr)
507 else
508 assert(0);
509 return;
512 if (svr == VT_LOCAL) {
513 if (-svcul < 0x1000)
514 o(0xd10003a0 | intr(r) | -svcul << 10); // sub x(r),x29,#...
515 else {
516 arm64_movimm(30, -svcul); // use x30 for offset
517 o(0xcb0003a0 | intr(r) | (uint32_t)30 << 16); // sub x(r),x29,x30
519 return;
522 if (svr == VT_JMP || svr == VT_JMPI) {
523 int t = (svr == VT_JMPI);
524 arm64_movimm(intr(r), t);
525 o(0x14000002); // b .+8
526 gsym(svcul);
527 arm64_movimm(intr(r), t ^ 1);
528 return;
531 if (svr == (VT_LLOCAL | VT_LVAL)) {
532 arm64_ldrx(0, 3, 30, 29, svcul); // use x30 for offset
533 if (IS_FREG(r))
534 arm64_ldrv(arm64_type_size(svtt), fltr(r), 30, 0);
535 else
536 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
537 intr(r), 30, 0);
538 return;
541 if (svr == VT_CMP) {
542 arm64_load_cmp(r, sv);
543 return;
546 printf("load(%x, (%x, %x, %llx))\n", r, svtt, sv->r, (long long)svcul);
547 assert(0);
550 ST_FUNC void store(int r, SValue *sv)
552 int svtt = sv->type.t;
553 int svr = sv->r & ~VT_LVAL_TYPE;
554 int svrv = svr & VT_VALMASK;
555 uint64_t svcul = (uint32_t)sv->c.i;
556 svcul = svcul >> 31 & 1 ? svcul - ((uint64_t)1 << 32) : svcul;
558 if (svr == (VT_LOCAL | VT_LVAL)) {
559 if (IS_FREG(r))
560 arm64_strv(arm64_type_size(svtt), fltr(r), 29, svcul);
561 else
562 arm64_strx(arm64_type_size(svtt), intr(r), 29, svcul);
563 return;
566 if ((svr & ~VT_VALMASK) == VT_LVAL && svrv < VT_CONST) {
567 if (IS_FREG(r))
568 arm64_strv(arm64_type_size(svtt), fltr(r), intr(svrv), 0);
569 else
570 arm64_strx(arm64_type_size(svtt), intr(r), intr(svrv), 0);
571 return;
574 if (svr == (VT_CONST | VT_LVAL | VT_SYM)) {
575 arm64_sym(30, sv->sym, svcul); // use x30 for address
576 if (IS_FREG(r))
577 arm64_strv(arm64_type_size(svtt), fltr(r), 30, 0);
578 else
579 arm64_strx(arm64_type_size(svtt), intr(r), 30, 0);
580 return;
583 printf("store(%x, (%x, %x, %llx))\n", r, svtt, sv->r, (long long)svcul);
584 assert(0);
587 static void arm64_gen_bl_or_b(int b)
589 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST && (vtop->r & VT_SYM)) {
590 assert(!b);
591 greloca(cur_text_section, vtop->sym, ind, R_AARCH64_CALL26, 0);
592 o(0x94000000); // bl .
594 else
595 o(0xd61f0000 | (uint32_t)!b << 21 | intr(gv(RC_R30)) << 5); // br/blr
598 static int arm64_hfa_aux(CType *type, int *fsize, int num)
600 if (is_float(type->t)) {
601 int a, n = type_size(type, &a);
602 if (num >= 4 || (*fsize && *fsize != n))
603 return -1;
604 *fsize = n;
605 return num + 1;
607 else if ((type->t & VT_BTYPE) == VT_STRUCT) {
608 int is_struct = 0; // rather than union
609 Sym *field;
610 for (field = type->ref->next; field; field = field->next)
611 if (field->c) {
612 is_struct = 1;
613 break;
615 if (is_struct) {
616 int num0 = num;
617 for (field = type->ref->next; field; field = field->next) {
618 if (field->c != (num - num0) * *fsize)
619 return -1;
620 num = arm64_hfa_aux(&field->type, fsize, num);
621 if (num == -1)
622 return -1;
624 if (type->ref->c != (num - num0) * *fsize)
625 return -1;
626 return num;
628 else { // union
629 int num0 = num;
630 for (field = type->ref->next; field; field = field->next) {
631 int num1 = arm64_hfa_aux(&field->type, fsize, num0);
632 if (num1 == -1)
633 return -1;
634 num = num1 < num ? num : num1;
636 if (type->ref->c != (num - num0) * *fsize)
637 return -1;
638 return num;
641 else if (type->t & VT_ARRAY) {
642 int num1;
643 if (!type->ref->c)
644 return num;
645 num1 = arm64_hfa_aux(&type->ref->type, fsize, num);
646 if (num1 == -1 || (num1 != num && type->ref->c > 4))
647 return -1;
648 num1 = num + type->ref->c * (num1 - num);
649 if (num1 > 4)
650 return -1;
651 return num1;
653 return -1;
656 static int arm64_hfa(CType *type, int *fsize)
658 if ((type->t & VT_BTYPE) == VT_STRUCT || (type->t & VT_ARRAY)) {
659 int sz = 0;
660 int n = arm64_hfa_aux(type, &sz, 0);
661 if (0 < n && n <= 4) {
662 if (fsize)
663 *fsize = sz;
664 return n;
667 return 0;
670 static unsigned long arm64_pcs_aux(int n, CType **type, unsigned long *a)
672 int nx = 0; // next integer register
673 int nv = 0; // next vector register
674 unsigned long ns = 32; // next stack offset
675 int i;
677 for (i = 0; i < n; i++) {
678 int hfa = arm64_hfa(type[i], 0);
679 int size, align;
681 if ((type[i]->t & VT_ARRAY) ||
682 (type[i]->t & VT_BTYPE) == VT_FUNC)
683 size = align = 8;
684 else
685 size = type_size(type[i], &align);
687 if (hfa)
688 // B.2
690 else if (size > 16) {
691 // B.3: replace with pointer
692 if (nx < 8)
693 a[i] = nx++ << 1 | 1;
694 else {
695 ns = (ns + 7) & ~7;
696 a[i] = ns | 1;
697 ns += 8;
699 continue;
701 else if ((type[i]->t & VT_BTYPE) == VT_STRUCT)
702 // B.4
703 size = (size + 7) & ~7;
705 // C.1
706 if (is_float(type[i]->t) && nv < 8) {
707 a[i] = 16 + (nv++ << 1);
708 continue;
711 // C.2
712 if (hfa && nv + hfa <= 8) {
713 a[i] = 16 + (nv << 1);
714 nv += hfa;
715 continue;
718 // C.3
719 if (hfa) {
720 nv = 8;
721 size = (size + 7) & ~7;
724 // C.4
725 if (hfa || (type[i]->t & VT_BTYPE) == VT_LDOUBLE) {
726 ns = (ns + 7) & ~7;
727 ns = (ns + align - 1) & -align;
730 // C.5
731 if ((type[i]->t & VT_BTYPE) == VT_FLOAT)
732 size = 8;
734 // C.6
735 if (hfa || is_float(type[i]->t)) {
736 a[i] = ns;
737 ns += size;
738 continue;
741 // C.7
742 if ((type[i]->t & VT_BTYPE) != VT_STRUCT && size <= 8 && nx < 8) {
743 a[i] = nx++ << 1;
744 continue;
747 // C.8
748 if (align == 16)
749 nx = (nx + 1) & ~1;
751 // C.9
752 if ((type[i]->t & VT_BTYPE) != VT_STRUCT && size == 16 && nx < 7) {
753 a[i] = nx << 1;
754 nx += 2;
755 continue;
758 // C.10
759 if ((type[i]->t & VT_BTYPE) == VT_STRUCT && size <= (8 - nx) * 8) {
760 a[i] = nx << 1;
761 nx += (size + 7) >> 3;
762 continue;
765 // C.11
766 nx = 8;
768 // C.12
769 ns = (ns + 7) & ~7;
770 ns = (ns + align - 1) & -align;
772 // C.13
773 if ((type[i]->t & VT_BTYPE) == VT_STRUCT) {
774 a[i] = ns;
775 ns += size;
776 continue;
779 // C.14
780 if (size < 8)
781 size = 8;
783 // C.15
784 a[i] = ns;
785 ns += size;
788 return ns - 32;
791 static unsigned long arm64_pcs(int n, CType **type, unsigned long *a)
793 unsigned long stack;
795 // Return type:
796 if ((type[0]->t & VT_BTYPE) == VT_VOID)
797 a[0] = -1;
798 else {
799 arm64_pcs_aux(1, type, a);
800 assert(a[0] == 0 || a[0] == 1 || a[0] == 16);
803 // Argument types:
804 stack = arm64_pcs_aux(n, type + 1, a + 1);
806 if (0) {
807 int i;
808 for (i = 0; i <= n; i++) {
809 if (!i)
810 printf("arm64_pcs return: ");
811 else
812 printf("arm64_pcs arg %d: ", i);
813 if (a[i] == (unsigned long)-1)
814 printf("void\n");
815 else if (a[i] == 1 && !i)
816 printf("X8 pointer\n");
817 else if (a[i] < 16)
818 printf("X%lu%s\n", a[i] / 2, a[i] & 1 ? " pointer" : "");
819 else if (a[i] < 32)
820 printf("V%lu\n", a[i] / 2 - 8);
821 else
822 printf("stack %lu%s\n",
823 (a[i] - 32) & ~1, a[i] & 1 ? " pointer" : "");
827 return stack;
830 ST_FUNC void gfunc_call(int nb_args)
832 CType *return_type;
833 CType **t;
834 unsigned long *a, *a1;
835 unsigned long stack;
836 int i;
838 return_type = &vtop[-nb_args].type.ref->type;
839 if ((return_type->t & VT_BTYPE) == VT_STRUCT)
840 --nb_args;
842 t = tcc_malloc((nb_args + 1) * sizeof(*t));
843 a = tcc_malloc((nb_args + 1) * sizeof(*a));
844 a1 = tcc_malloc((nb_args + 1) * sizeof(*a1));
846 t[0] = return_type;
847 for (i = 0; i < nb_args; i++)
848 t[nb_args - i] = &vtop[-i].type;
850 stack = arm64_pcs(nb_args, t, a);
852 // Allocate space for structs replaced by pointer:
853 for (i = nb_args; i; i--)
854 if (a[i] & 1) {
855 SValue *arg = &vtop[i - nb_args];
856 int align, size = type_size(&arg->type, &align);
857 assert((arg->type.t & VT_BTYPE) == VT_STRUCT);
858 stack = (stack + align - 1) & -align;
859 a1[i] = stack;
860 stack += size;
863 stack = (stack + 15) >> 4 << 4;
865 assert(stack < 0x1000);
866 if (stack)
867 o(0xd10003ff | stack << 10); // sub sp,sp,#(n)
869 // First pass: set all values on stack
870 for (i = nb_args; i; i--) {
871 vpushv(vtop - nb_args + i);
873 if (a[i] & 1) {
874 // struct replaced by pointer
875 int r = get_reg(RC_INT);
876 arm64_spoff(intr(r), a1[i]);
877 vset(&vtop->type, r | VT_LVAL, 0);
878 vswap();
879 vstore();
880 if (a[i] >= 32) {
881 // pointer on stack
882 r = get_reg(RC_INT);
883 arm64_spoff(intr(r), a1[i]);
884 arm64_strx(3, intr(r), 31, (a[i] - 32) >> 1 << 1);
887 else if (a[i] >= 32) {
888 // value on stack
889 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
890 int r = get_reg(RC_INT);
891 arm64_spoff(intr(r), a[i] - 32);
892 vset(&vtop->type, r | VT_LVAL, 0);
893 vswap();
894 vstore();
896 else if (is_float(vtop->type.t)) {
897 gv(RC_FLOAT);
898 arm64_strv(arm64_type_size(vtop[0].type.t),
899 fltr(vtop[0].r), 31, a[i] - 32);
901 else {
902 gv(RC_INT);
903 arm64_strx(arm64_type_size(vtop[0].type.t),
904 intr(vtop[0].r), 31, a[i] - 32);
908 --vtop;
911 // Second pass: assign values to registers
912 for (i = nb_args; i; i--, vtop--) {
913 if (a[i] < 16 && !(a[i] & 1)) {
914 // value in general-purpose registers
915 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
916 int align, size = type_size(&vtop->type, &align);
917 vtop->type.t = VT_PTR;
918 gaddrof();
919 gv(RC_R(a[i] / 2));
920 arm64_ldrs(a[i] / 2, size);
922 else
923 gv(RC_R(a[i] / 2));
925 else if (a[i] < 16)
926 // struct replaced by pointer in register
927 arm64_spoff(a[i] / 2, a1[i]);
928 else if (a[i] < 32) {
929 // value in floating-point registers
930 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
931 uint32_t j, sz, n = arm64_hfa(&vtop->type, &sz);
932 vtop->type.t = VT_PTR;
933 gaddrof();
934 gv(RC_R30);
935 for (j = 0; j < n; j++)
936 o(0x3d4003c0 |
937 (sz & 16) << 19 | -(sz & 8) << 27 | (sz & 4) << 29 |
938 (a[i] / 2 - 8 + j) |
939 j << 10); // ldr ([sdq])(*),[x30,#(j * sz)]
941 else
942 gv(RC_F(a[i] / 2 - 8));
946 if ((return_type->t & VT_BTYPE) == VT_STRUCT) {
947 if (a[0] == 1) {
948 // indirect return: set x8 and discard the stack value
949 gv(RC_R(8));
950 --vtop;
952 else
953 // return in registers: keep the address for after the call
954 vswap();
957 save_regs(0);
958 arm64_gen_bl_or_b(0);
959 --vtop;
960 if (stack)
961 o(0x910003ff | stack << 10); // add sp,sp,#(n)
964 int rt = return_type->t;
965 int bt = rt & VT_BTYPE;
966 if (bt == VT_BYTE || bt == VT_SHORT)
967 // Promote small integers:
968 o(0x13001c00 | (bt == VT_SHORT) << 13 |
969 (uint32_t)!!(rt & VT_UNSIGNED) << 30); // [su]xt[bh] w0,w0
970 else if (bt == VT_STRUCT && !(a[0] & 1)) {
971 // A struct was returned in registers, so write it out:
972 gv(RC_R(8));
973 --vtop;
974 if (a[0] == 0) {
975 int align, size = type_size(return_type, &align);
976 assert(size <= 16);
977 if (size > 8)
978 o(0xa9000500); // stp x0,x1,[x8]
979 else if (size)
980 arm64_strx(size > 4 ? 3 : size > 2 ? 2 : size > 1, 0, 8, 0);
983 else if (a[0] == 16) {
984 uint32_t j, sz, n = arm64_hfa(return_type, &sz);
985 for (j = 0; j < n; j++)
986 o(0x3d000100 |
987 (sz & 16) << 19 | -(sz & 8) << 27 | (sz & 4) << 29 |
988 (a[i] / 2 - 8 + j) |
989 j << 10); // str ([sdq])(*),[x8,#(j * sz)]
994 tcc_free(a1);
995 tcc_free(a);
996 tcc_free(t);
999 static unsigned long arm64_func_va_list_stack;
1000 static int arm64_func_va_list_gr_offs;
1001 static int arm64_func_va_list_vr_offs;
1002 static int arm64_func_sub_sp_offset;
1004 ST_FUNC void gfunc_prolog(CType *func_type)
1006 int n = 0;
1007 int i = 0;
1008 Sym *sym;
1009 CType **t;
1010 unsigned long *a;
1012 // Why doesn't the caller (gen_function) set func_vt?
1013 func_vt = func_type->ref->type;
1014 func_vc = 144; // offset of where x8 is stored
1016 for (sym = func_type->ref; sym; sym = sym->next)
1017 ++n;
1018 t = tcc_malloc(n * sizeof(*t));
1019 a = tcc_malloc(n * sizeof(*a));
1021 for (sym = func_type->ref; sym; sym = sym->next)
1022 t[i++] = &sym->type;
1024 arm64_func_va_list_stack = arm64_pcs(n - 1, t, a);
1026 o(0xa9b27bfd); // stp x29,x30,[sp,#-224]!
1027 o(0xad0087e0); // stp q0,q1,[sp,#16]
1028 o(0xad018fe2); // stp q2,q3,[sp,#48]
1029 o(0xad0297e4); // stp q4,q5,[sp,#80]
1030 o(0xad039fe6); // stp q6,q7,[sp,#112]
1031 o(0xa90923e8); // stp x8,x8,[sp,#144]
1032 o(0xa90a07e0); // stp x0,x1,[sp,#160]
1033 o(0xa90b0fe2); // stp x2,x3,[sp,#176]
1034 o(0xa90c17e4); // stp x4,x5,[sp,#192]
1035 o(0xa90d1fe6); // stp x6,x7,[sp,#208]
1037 arm64_func_va_list_gr_offs = -64;
1038 arm64_func_va_list_vr_offs = -128;
1040 for (i = 1, sym = func_type->ref->next; sym; i++, sym = sym->next) {
1041 int off = (a[i] < 16 ? 160 + a[i] / 2 * 8 :
1042 a[i] < 32 ? 16 + (a[i] - 16) / 2 * 16 :
1043 224 + ((a[i] - 32) >> 1 << 1));
1044 sym_push(sym->v & ~SYM_FIELD, &sym->type,
1045 (a[i] & 1 ? VT_LLOCAL : VT_LOCAL) | lvalue_type(sym->type.t),
1046 off);
1048 if (a[i] < 16) {
1049 int align, size = type_size(&sym->type, &align);
1050 arm64_func_va_list_gr_offs = (a[i] / 2 - 7 +
1051 (!(a[i] & 1) && size > 8)) * 8;
1053 else if (a[i] < 32) {
1054 uint32_t hfa = arm64_hfa(&sym->type, 0);
1055 arm64_func_va_list_vr_offs = (a[i] / 2 - 16 +
1056 (hfa ? hfa : 1)) * 16;
1059 // HFAs of float and double need to be written differently:
1060 if (16 <= a[i] && a[i] < 32 && (sym->type.t & VT_BTYPE) == VT_STRUCT) {
1061 uint32_t j, sz, k = arm64_hfa(&sym->type, &sz);
1062 if (sz < 16)
1063 for (j = 0; j < k; j++) {
1064 o(0x3d0003e0 | -(sz & 8) << 27 | (sz & 4) << 29 |
1065 ((a[i] - 16) / 2 + j) | (off / sz + j) << 10);
1066 // str ([sdq])(*),[sp,#(j * sz)]
1071 tcc_free(a);
1072 tcc_free(t);
1074 o(0x910003fd); // mov x29,sp
1075 arm64_func_sub_sp_offset = ind;
1076 // In gfunc_epilog these will be replaced with code to decrement SP:
1077 o(0xd503201f); // nop
1078 o(0xd503201f); // nop
1079 loc = 0;
1082 ST_FUNC void gen_va_start(void)
1084 int r;
1085 --vtop; // we don't need the "arg"
1086 gaddrof();
1087 r = intr(gv(RC_INT));
1089 if (arm64_func_va_list_stack) {
1090 //xx could use add (immediate) here
1091 arm64_movimm(30, arm64_func_va_list_stack + 224);
1092 o(0x8b1e03be); // add x30,x29,x30
1094 else
1095 o(0x910383be); // add x30,x29,#224
1096 o(0xf900001e | r << 5); // str x30,[x(r)]
1098 if (arm64_func_va_list_gr_offs) {
1099 if (arm64_func_va_list_stack)
1100 o(0x910383be); // add x30,x29,#224
1101 o(0xf900041e | r << 5); // str x30,[x(r),#8]
1104 if (arm64_func_va_list_vr_offs) {
1105 o(0x910243be); // add x30,x29,#144
1106 o(0xf900081e | r << 5); // str x30,[x(r),#16]
1109 arm64_movimm(30, arm64_func_va_list_gr_offs);
1110 o(0xb900181e | r << 5); // str w30,[x(r),#24]
1112 arm64_movimm(30, arm64_func_va_list_vr_offs);
1113 o(0xb9001c1e | r << 5); // str w30,[x(r),#28]
1115 --vtop;
1118 ST_FUNC void gen_va_arg(CType *t)
1120 int align, size = type_size(t, &align);
1121 int fsize, hfa = arm64_hfa(t, &fsize);
1122 uint32_t r0, r1;
1124 if (is_float(t->t)) {
1125 hfa = 1;
1126 fsize = size;
1129 gaddrof();
1130 r0 = intr(gv(RC_INT));
1131 r1 = get_reg(RC_INT);
1132 vtop[0].r = r1 | lvalue_type(t->t);
1133 r1 = intr(r1);
1135 if (!hfa) {
1136 uint32_t n = size > 16 ? 8 : (size + 7) & -8;
1137 o(0xb940181e | r0 << 5); // ldr w30,[x(r0),#24] // __gr_offs
1138 if (align == 16) {
1139 assert(0); // this path untested but needed for __uint128_t
1140 o(0x11003fde); // add w30,w30,#15
1141 o(0x121c6fde); // and w30,w30,#-16
1143 o(0x310003c0 | r1 | n << 10); // adds w(r1),w30,#(n)
1144 o(0x540000ad); // b.le .+20
1145 o(0xf9400000 | r1 | r0 << 5); // ldr x(r1),[x(r0)] // __stack
1146 o(0x9100001e | r1 << 5 | n << 10); // add x30,x(r1),#(n)
1147 o(0xf900001e | r0 << 5); // str x30,[x(r0)] // __stack
1148 o(0x14000004); // b .+16
1149 o(0xb9001800 | r1 | r0 << 5); // str w(r1),[x(r0),#24] // __gr_offs
1150 o(0xf9400400 | r1 | r0 << 5); // ldr x(r1),[x(r0),#8] // __gr_top
1151 o(0x8b3ec000 | r1 | r1 << 5); // add x(r1),x(r1),w30,sxtw
1152 if (size > 16)
1153 o(0xf9400000 | r1 | r1 << 5); // ldr x(r1),[x(r1)]
1155 else {
1156 uint32_t rsz = hfa << 4;
1157 uint32_t ssz = (size + 7) & -(uint32_t)8;
1158 uint32_t b1, b2;
1159 o(0xb9401c1e | r0 << 5); // ldr w30,[x(r0),#28] // __vr_offs
1160 o(0x310003c0 | r1 | rsz << 10); // adds w(r1),w30,#(rsz)
1161 b1 = ind; o(0x5400000d); // b.le lab1
1162 o(0xf9400000 | r1 | r0 << 5); // ldr x(r1),[x(r0)] // __stack
1163 if (fsize == 16) {
1164 o(0x91003c00 | r1 | r1 << 5); // add x(r1),x(r1),#15
1165 o(0x927cec00 | r1 | r1 << 5); // and x(r1),x(r1),#-16
1167 o(0x9100001e | r1 << 5 | ssz << 10); // add x30,x(r1),#(ssz)
1168 o(0xf900001e | r0 << 5); // str x30,[x(r0)] // __stack
1169 b2 = ind; o(0x14000000); // b lab2
1170 // lab1:
1171 write32le(cur_text_section->data + b1, 0x5400000d | (ind - b1) << 3);
1172 o(0xb9001c00 | r1 | r0 << 5); // str w(r1),[x(r0),#28] // __vr_offs
1173 o(0xf9400800 | r1 | r0 << 5); // ldr x(r1),[x(r0),#16] // __vr_top
1174 if (hfa == 1 || fsize == 16)
1175 o(0x8b3ec000 | r1 | r1 << 5); // add x(r1),x(r1),w30,sxtw
1176 else {
1177 // We need to change the layout of this HFA.
1178 // Get some space on the stack using global variable "loc":
1179 loc = (loc - size) & -(uint32_t)align;
1180 o(0x8b3ec000 | 30 | r1 << 5); // add x30,x(r1),w30,sxtw
1181 arm64_movimm(r1, loc);
1182 o(0x8b0003a0 | r1 | r1 << 16); // add x(r1),x29,x(r1)
1183 o(0x4c402bdc | (uint32_t)fsize << 7 |
1184 (uint32_t)(hfa == 2) << 15 |
1185 (uint32_t)(hfa == 3) << 14); // ld1 {v28.(4s|2d),...},[x30]
1186 o(0x0d00801c | r1 << 5 | (fsize == 8) << 10 |
1187 (uint32_t)(hfa != 2) << 13 |
1188 (uint32_t)(hfa != 3) << 21); // st(hfa) {v28.(s|d),...}[0],[x(r1)]
1190 // lab2:
1191 write32le(cur_text_section->data + b2, 0x14000000 | (ind - b2) >> 2);
1195 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret,
1196 int *align, int *regsize)
1198 return 0;
1201 ST_FUNC void gfunc_return(CType *func_type)
1203 CType *t = func_type;
1204 unsigned long a;
1206 arm64_pcs(0, &t, &a);
1207 switch (a) {
1208 case -1:
1209 break;
1210 case 0:
1211 if ((func_type->t & VT_BTYPE) == VT_STRUCT) {
1212 int align, size = type_size(func_type, &align);
1213 gaddrof();
1214 gv(RC_R(0));
1215 arm64_ldrs(0, size);
1217 else
1218 gv(RC_IRET);
1219 break;
1220 case 1: {
1221 CType type = *func_type;
1222 mk_pointer(&type);
1223 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
1224 indir();
1225 vswap();
1226 vstore();
1227 break;
1229 case 16:
1230 if ((func_type->t & VT_BTYPE) == VT_STRUCT) {
1231 uint32_t j, sz, n = arm64_hfa(&vtop->type, &sz);
1232 gaddrof();
1233 gv(RC_R(0));
1234 for (j = 0; j < n; j++)
1235 o(0x3d400000 |
1236 (sz & 16) << 19 | -(sz & 8) << 27 | (sz & 4) << 29 |
1237 j | j << 10); // ldr ([sdq])(*),[x0,#(j * sz)]
1239 else
1240 gv(RC_FRET);
1241 break;
1242 default:
1243 assert(0);
1245 vtop--;
1248 ST_FUNC void gfunc_epilog(void)
1250 if (loc) {
1251 // Insert instructions to subtract size of stack frame from SP.
1252 unsigned char *ptr = cur_text_section->data + arm64_func_sub_sp_offset;
1253 uint64_t diff = (-loc + 15) & ~15;
1254 if (!(diff >> 24)) {
1255 if (diff & 0xfff) // sub sp,sp,#(diff & 0xfff)
1256 write32le(ptr, 0xd10003ff | (diff & 0xfff) << 10);
1257 if (diff >> 12) // sub sp,sp,#(diff >> 12),lsl #12
1258 write32le(ptr + 4, 0xd14003ff | (diff >> 12) << 10);
1260 else {
1261 // In this case we may subtract more than necessary,
1262 // but always less than 17/16 of what we were aiming for.
1263 int i = 0;
1264 int j = 0;
1265 while (diff >> 20) {
1266 diff = (diff + 0xffff) >> 16;
1267 ++i;
1269 while (diff >> 16) {
1270 diff = (diff + 1) >> 1;
1271 ++j;
1273 write32le(ptr, 0xd2800010 | diff << 5 | i << 21);
1274 // mov x16,#(diff),lsl #(16 * i)
1275 write32le(ptr + 4, 0xcb3063ff | j << 10);
1276 // sub sp,sp,x16,lsl #(j)
1279 o(0x910003bf); // mov sp,x29
1280 o(0xa8ce7bfd); // ldp x29,x30,[sp],#224
1282 o(0xd65f03c0); // ret
1285 ST_FUNC void gen_fill_nops(int bytes)
1287 if ((bytes & 3))
1288 tcc_error("alignment of code section not multiple of 4");
1289 while (bytes > 0) {
1290 o(0xd503201f); // nop
1291 bytes -= 4;
1295 // Generate forward branch to label:
1296 ST_FUNC int gjmp(int t)
1298 int r = ind;
1299 dprintf((". gjmp T=%04x\n", t)); //gr
1300 if (nocode_wanted)
1301 return t;
1302 o(t);
1303 return r;
1306 // Generate branch to known address:
1307 ST_FUNC void gjmp_addr(int a)
1309 assert(a - ind + 0x8000000 < 0x10000000);
1310 o(0x14000000 | ((a - ind) >> 2 & 0x3ffffff));
1311 dprintf((". gjmp_addr T=%04x\n", a)); //gr
1314 ST_FUNC int gjmp_append(int n, int t)
1316 void *p;
1317 /* insert vtop->c jump list in t */
1318 if (n) {
1319 uint32_t n1 = n, n2;
1320 while ((n2 = read32le(p = cur_text_section->data + n1)))
1321 n1 = n2;
1322 write32le(p, t);
1323 t = n;
1325 return t;
1328 void arm64_vset_VT_CMP(int op)
1330 if (op >= TOK_ULT && op <= TOK_GT) {
1331 vtop->cmp_r = vtop->r;
1332 vset_VT_CMP(0x80);
1333 dprintf((". set VT_CMP OP(%s) R=%x\n", get_tok_str(op, 0), vtop->cmp_r));
1337 static void arm64_gen_opil(int op, uint32_t l);
1339 static void arm64_load_cmp(int r, SValue *sv)
1341 sv->r = sv->cmp_r;
1342 dprintf((". load VT_CMP OP(%x), R=%x/%x\n", (int)sv->c.i, sv->r, r));
1343 if (sv->c.i & 1) {
1344 vpushi(1);
1345 arm64_gen_opil('^', 0);
1347 if (r != sv->r) {
1348 load(r, sv);
1349 sv->r = r;
1351 dprintf((". load VT_CMP done\n")); //gr
1354 ST_FUNC int gjmp_cond(int op, int t)
1356 int bt = vtop->type.t & VT_BTYPE;
1358 int inv = op & 1;
1359 vtop->r = vtop->cmp_r;
1360 dprintf((". gjmp_cond OP(%x) R=%x T=%04x\n", op, vtop->r, t)); //gr
1362 if (bt == VT_LDOUBLE) {
1363 uint32_t a, b, f = fltr(gv(RC_FLOAT));
1364 a = get_reg(RC_INT);
1365 vpushi(0);
1366 vtop[0].r = a;
1367 b = get_reg(RC_INT);
1368 a = intr(a);
1369 b = intr(b);
1370 o(0x4e083c00 | a | f << 5); // mov x(a),v(f).d[0]
1371 o(0x4e183c00 | b | f << 5); // mov x(b),v(f).d[1]
1372 o(0xaa000400 | a | a << 5 | b << 16); // orr x(a),x(a),x(b),lsl #1
1373 o(0xb4000040 | a | !!inv << 24); // cbz/cbnz x(a),.+8
1374 --vtop;
1376 else if (bt == VT_FLOAT || bt == VT_DOUBLE) {
1377 uint32_t a = fltr(gv(RC_FLOAT));
1378 o(0x1e202008 | a << 5 | (bt != VT_FLOAT) << 22); // fcmp
1379 o(0x54000040 | !!inv); // b.eq/b.ne .+8
1381 else {
1382 uint32_t ll = (bt == VT_PTR || bt == VT_LLONG);
1383 uint32_t a = intr(gv(RC_INT));
1384 o(0x34000040 | a | !!inv << 24 | ll << 31); // cbz/cbnz wA,.+8
1386 return gjmp(t);
1389 static int arm64_iconst(uint64_t *val, SValue *sv)
1391 if ((sv->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
1392 return 0;
1393 if (val) {
1394 int t = sv->type.t;
1395 int bt = t & VT_BTYPE;
1396 *val = ((bt == VT_LLONG || bt == VT_PTR) ? sv->c.i :
1397 (uint32_t)sv->c.i |
1398 (t & VT_UNSIGNED ? 0 : -(sv->c.i & 0x80000000)));
1400 return 1;
1403 static int arm64_gen_opic(int op, uint32_t l, int rev, uint64_t val,
1404 uint32_t x, uint32_t a)
1406 if (op == '-' && !rev) {
1407 val = -val;
1408 op = '+';
1410 val = l ? val : (uint32_t)val;
1412 switch (op) {
1414 case '+': {
1415 uint32_t s = l ? val >> 63 : val >> 31;
1416 val = s ? -val : val;
1417 val = l ? val : (uint32_t)val;
1418 if (!(val & ~(uint64_t)0xfff))
1419 o(0x11000000 | l << 31 | s << 30 | x | a << 5 | val << 10);
1420 else if (!(val & ~(uint64_t)0xfff000))
1421 o(0x11400000 | l << 31 | s << 30 | x | a << 5 | val >> 12 << 10);
1422 else {
1423 arm64_movimm(30, val); // use x30
1424 o(0x0b1e0000 | l << 31 | s << 30 | x | a << 5);
1426 return 1;
1429 case '-':
1430 if (!val)
1431 o(0x4b0003e0 | l << 31 | x | a << 16); // neg
1432 else if (val == (l ? (uint64_t)-1 : (uint32_t)-1))
1433 o(0x2a2003e0 | l << 31 | x | a << 16); // mvn
1434 else {
1435 arm64_movimm(30, val); // use x30
1436 o(0x4b0003c0 | l << 31 | x | a << 16); // sub
1438 return 1;
1440 case '^':
1441 if (val == -1 || (val == 0xffffffff && !l)) {
1442 o(0x2a2003e0 | l << 31 | x | a << 16); // mvn
1443 return 1;
1445 // fall through
1446 case '&':
1447 case '|': {
1448 int e = arm64_encode_bimm64(l ? val : val | val << 32);
1449 if (e < 0)
1450 return 0;
1451 o((op == '&' ? 0x12000000 :
1452 op == '|' ? 0x32000000 : 0x52000000) |
1453 l << 31 | x | a << 5 | (uint32_t)e << 10);
1454 return 1;
1457 case TOK_SAR:
1458 case TOK_SHL:
1459 case TOK_SHR: {
1460 uint32_t n = 32 << l;
1461 val = val & (n - 1);
1462 if (rev)
1463 return 0;
1464 if (!val)
1465 assert(0);
1466 else if (op == TOK_SHL)
1467 o(0x53000000 | l << 31 | l << 22 | x | a << 5 |
1468 (n - val) << 16 | (n - 1 - val) << 10); // lsl
1469 else
1470 o(0x13000000 | (op == TOK_SHR) << 30 | l << 31 | l << 22 |
1471 x | a << 5 | val << 16 | (n - 1) << 10); // lsr/asr
1472 return 1;
1476 return 0;
1479 static void arm64_gen_opil(int op, uint32_t l)
1481 uint32_t x, a, b;
1483 // Special treatment for operations with a constant operand:
1485 uint64_t val;
1486 int rev = 1;
1488 if (arm64_iconst(0, &vtop[0])) {
1489 vswap();
1490 rev = 0;
1492 if (arm64_iconst(&val, &vtop[-1])) {
1493 gv(RC_INT);
1494 a = intr(vtop[0].r);
1495 --vtop;
1496 x = get_reg(RC_INT);
1497 ++vtop;
1498 if (arm64_gen_opic(op, l, rev, val, intr(x), a)) {
1499 vtop[0].r = x;
1500 vswap();
1501 --vtop;
1502 return;
1505 if (!rev)
1506 vswap();
1509 gv2(RC_INT, RC_INT);
1510 assert(vtop[-1].r < VT_CONST && vtop[0].r < VT_CONST);
1511 a = intr(vtop[-1].r);
1512 b = intr(vtop[0].r);
1513 vtop -= 2;
1514 x = get_reg(RC_INT);
1515 ++vtop;
1516 vtop[0].r = x;
1517 x = intr(x);
1519 switch (op) {
1520 case '%':
1521 // Use x30 for quotient:
1522 o(0x1ac00c00 | l << 31 | 30 | a << 5 | b << 16); // sdiv
1523 o(0x1b008000 | l << 31 | x | (uint32_t)30 << 5 |
1524 b << 16 | a << 10); // msub
1525 break;
1526 case '&':
1527 o(0x0a000000 | l << 31 | x | a << 5 | b << 16); // and
1528 break;
1529 case '*':
1530 o(0x1b007c00 | l << 31 | x | a << 5 | b << 16); // mul
1531 break;
1532 case '+':
1533 o(0x0b000000 | l << 31 | x | a << 5 | b << 16); // add
1534 break;
1535 case '-':
1536 o(0x4b000000 | l << 31 | x | a << 5 | b << 16); // sub
1537 break;
1538 case '/':
1539 o(0x1ac00c00 | l << 31 | x | a << 5 | b << 16); // sdiv
1540 break;
1541 case '^':
1542 o(0x4a000000 | l << 31 | x | a << 5 | b << 16); // eor
1543 break;
1544 case '|':
1545 o(0x2a000000 | l << 31 | x | a << 5 | b << 16); // orr
1546 break;
1547 case TOK_EQ:
1548 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1549 o(0x1a9f17e0 | x); // cset wA,eq
1550 break;
1551 case TOK_GE:
1552 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1553 o(0x1a9fb7e0 | x); // cset wA,ge
1554 break;
1555 case TOK_GT:
1556 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1557 o(0x1a9fd7e0 | x); // cset wA,gt
1558 break;
1559 case TOK_LE:
1560 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1561 o(0x1a9fc7e0 | x); // cset wA,le
1562 break;
1563 case TOK_LT:
1564 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1565 o(0x1a9fa7e0 | x); // cset wA,lt
1566 break;
1567 case TOK_NE:
1568 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1569 o(0x1a9f07e0 | x); // cset wA,ne
1570 break;
1571 case TOK_SAR:
1572 o(0x1ac02800 | l << 31 | x | a << 5 | b << 16); // asr
1573 break;
1574 case TOK_SHL:
1575 o(0x1ac02000 | l << 31 | x | a << 5 | b << 16); // lsl
1576 break;
1577 case TOK_SHR:
1578 o(0x1ac02400 | l << 31 | x | a << 5 | b << 16); // lsr
1579 break;
1580 case TOK_UDIV:
1581 case TOK_PDIV:
1582 o(0x1ac00800 | l << 31 | x | a << 5 | b << 16); // udiv
1583 break;
1584 case TOK_UGE:
1585 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1586 o(0x1a9f37e0 | x); // cset wA,cs
1587 break;
1588 case TOK_UGT:
1589 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1590 o(0x1a9f97e0 | x); // cset wA,hi
1591 break;
1592 case TOK_ULT:
1593 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1594 o(0x1a9f27e0 | x); // cset wA,cc
1595 break;
1596 case TOK_ULE:
1597 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1598 o(0x1a9f87e0 | x); // cset wA,ls
1599 break;
1600 case TOK_UMOD:
1601 // Use x30 for quotient:
1602 o(0x1ac00800 | l << 31 | 30 | a << 5 | b << 16); // udiv
1603 o(0x1b008000 | l << 31 | x | (uint32_t)30 << 5 |
1604 b << 16 | a << 10); // msub
1605 break;
1606 default:
1607 assert(0);
1611 ST_FUNC void gen_opi(int op)
1613 arm64_gen_opil(op, 0);
1614 arm64_vset_VT_CMP(op);
1617 ST_FUNC void gen_opl(int op)
1619 arm64_gen_opil(op, 1);
1620 arm64_vset_VT_CMP(op);
1623 ST_FUNC void gen_opf(int op)
1625 uint32_t x, a, b, dbl;
1627 if (vtop[0].type.t == VT_LDOUBLE) {
1628 CType type = vtop[0].type;
1629 int func = 0;
1630 int cond = -1;
1631 switch (op) {
1632 case '*': func = TOK___multf3; break;
1633 case '+': func = TOK___addtf3; break;
1634 case '-': func = TOK___subtf3; break;
1635 case '/': func = TOK___divtf3; break;
1636 case TOK_EQ: func = TOK___eqtf2; cond = 1; break;
1637 case TOK_NE: func = TOK___netf2; cond = 0; break;
1638 case TOK_LT: func = TOK___lttf2; cond = 10; break;
1639 case TOK_GE: func = TOK___getf2; cond = 11; break;
1640 case TOK_LE: func = TOK___letf2; cond = 12; break;
1641 case TOK_GT: func = TOK___gttf2; cond = 13; break;
1642 default: assert(0); break;
1644 vpush_global_sym(&func_old_type, func);
1645 vrott(3);
1646 gfunc_call(2);
1647 vpushi(0);
1648 vtop->r = cond < 0 ? REG_FRET : REG_IRET;
1649 if (cond < 0)
1650 vtop->type = type;
1651 else {
1652 o(0x7100001f); // cmp w0,#0
1653 o(0x1a9f07e0 | (uint32_t)cond << 12); // cset w0,(cond)
1655 return;
1658 dbl = vtop[0].type.t != VT_FLOAT;
1659 gv2(RC_FLOAT, RC_FLOAT);
1660 assert(vtop[-1].r < VT_CONST && vtop[0].r < VT_CONST);
1661 a = fltr(vtop[-1].r);
1662 b = fltr(vtop[0].r);
1663 vtop -= 2;
1664 switch (op) {
1665 case TOK_EQ: case TOK_NE:
1666 case TOK_LT: case TOK_GE: case TOK_LE: case TOK_GT:
1667 x = get_reg(RC_INT);
1668 ++vtop;
1669 vtop[0].r = x;
1670 x = intr(x);
1671 break;
1672 default:
1673 x = get_reg(RC_FLOAT);
1674 ++vtop;
1675 vtop[0].r = x;
1676 x = fltr(x);
1677 break;
1680 switch (op) {
1681 case '*':
1682 o(0x1e200800 | dbl << 22 | x | a << 5 | b << 16); // fmul
1683 break;
1684 case '+':
1685 o(0x1e202800 | dbl << 22 | x | a << 5 | b << 16); // fadd
1686 break;
1687 case '-':
1688 o(0x1e203800 | dbl << 22 | x | a << 5 | b << 16); // fsub
1689 break;
1690 case '/':
1691 o(0x1e201800 | dbl << 22 | x | a << 5 | b << 16); // fdiv
1692 break;
1693 case TOK_EQ:
1694 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1695 o(0x1a9f17e0 | x); // cset w(x),eq
1696 break;
1697 case TOK_GE:
1698 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1699 o(0x1a9fb7e0 | x); // cset w(x),ge
1700 break;
1701 case TOK_GT:
1702 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1703 o(0x1a9fd7e0 | x); // cset w(x),gt
1704 break;
1705 case TOK_LE:
1706 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1707 o(0x1a9f87e0 | x); // cset w(x),ls
1708 break;
1709 case TOK_LT:
1710 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1711 o(0x1a9f57e0 | x); // cset w(x),mi
1712 break;
1713 case TOK_NE:
1714 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1715 o(0x1a9f07e0 | x); // cset w(x),ne
1716 break;
1717 default:
1718 assert(0);
1720 arm64_vset_VT_CMP(op);
1723 // Generate sign extension from 32 to 64 bits:
1724 ST_FUNC void gen_cvt_sxtw(void)
1726 uint32_t r = intr(gv(RC_INT));
1727 o(0x93407c00 | r | r << 5); // sxtw x(r),w(r)
1730 ST_FUNC void gen_cvt_itof(int t)
1732 if (t == VT_LDOUBLE) {
1733 int f = vtop->type.t;
1734 int func = (f & VT_BTYPE) == VT_LLONG ?
1735 (f & VT_UNSIGNED ? TOK___floatunditf : TOK___floatditf) :
1736 (f & VT_UNSIGNED ? TOK___floatunsitf : TOK___floatsitf);
1737 vpush_global_sym(&func_old_type, func);
1738 vrott(2);
1739 gfunc_call(1);
1740 vpushi(0);
1741 vtop->type.t = t;
1742 vtop->r = REG_FRET;
1743 return;
1745 else {
1746 int d, n = intr(gv(RC_INT));
1747 int s = !(vtop->type.t & VT_UNSIGNED);
1748 uint32_t l = ((vtop->type.t & VT_BTYPE) == VT_LLONG);
1749 --vtop;
1750 d = get_reg(RC_FLOAT);
1751 ++vtop;
1752 vtop[0].r = d;
1753 o(0x1e220000 | (uint32_t)!s << 16 |
1754 (uint32_t)(t != VT_FLOAT) << 22 | fltr(d) |
1755 l << 31 | n << 5); // [us]cvtf [sd](d),[wx](n)
1759 ST_FUNC void gen_cvt_ftoi(int t)
1761 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
1762 int func = (t & VT_BTYPE) == VT_LLONG ?
1763 (t & VT_UNSIGNED ? TOK___fixunstfdi : TOK___fixtfdi) :
1764 (t & VT_UNSIGNED ? TOK___fixunstfsi : TOK___fixtfsi);
1765 vpush_global_sym(&func_old_type, func);
1766 vrott(2);
1767 gfunc_call(1);
1768 vpushi(0);
1769 vtop->type.t = t;
1770 vtop->r = REG_IRET;
1771 return;
1773 else {
1774 int d, n = fltr(gv(RC_FLOAT));
1775 uint32_t l = ((vtop->type.t & VT_BTYPE) != VT_FLOAT);
1776 --vtop;
1777 d = get_reg(RC_INT);
1778 ++vtop;
1779 vtop[0].r = d;
1780 o(0x1e380000 |
1781 (uint32_t)!!(t & VT_UNSIGNED) << 16 |
1782 (uint32_t)((t & VT_BTYPE) == VT_LLONG) << 31 | intr(d) |
1783 l << 22 | n << 5); // fcvtz[su] [wx](d),[sd](n)
1787 ST_FUNC void gen_cvt_ftof(int t)
1789 int f = vtop[0].type.t;
1790 assert(t == VT_FLOAT || t == VT_DOUBLE || t == VT_LDOUBLE);
1791 assert(f == VT_FLOAT || f == VT_DOUBLE || f == VT_LDOUBLE);
1792 if (t == f)
1793 return;
1795 if (t == VT_LDOUBLE || f == VT_LDOUBLE) {
1796 int func = (t == VT_LDOUBLE) ?
1797 (f == VT_FLOAT ? TOK___extendsftf2 : TOK___extenddftf2) :
1798 (t == VT_FLOAT ? TOK___trunctfsf2 : TOK___trunctfdf2);
1799 vpush_global_sym(&func_old_type, func);
1800 vrott(2);
1801 gfunc_call(1);
1802 vpushi(0);
1803 vtop->type.t = t;
1804 vtop->r = REG_FRET;
1806 else {
1807 int x, a;
1808 gv(RC_FLOAT);
1809 assert(vtop[0].r < VT_CONST);
1810 a = fltr(vtop[0].r);
1811 --vtop;
1812 x = get_reg(RC_FLOAT);
1813 ++vtop;
1814 vtop[0].r = x;
1815 x = fltr(x);
1817 if (f == VT_FLOAT)
1818 o(0x1e22c000 | x | a << 5); // fcvt d(x),s(a)
1819 else
1820 o(0x1e624000 | x | a << 5); // fcvt s(x),d(a)
1824 ST_FUNC void ggoto(void)
1826 arm64_gen_bl_or_b(1);
1827 --vtop;
1830 ST_FUNC void gen_clear_cache(void)
1832 uint32_t beg, end, dsz, isz, p, lab1, b1;
1833 gv2(RC_INT, RC_INT);
1834 vpushi(0);
1835 vtop->r = get_reg(RC_INT);
1836 vpushi(0);
1837 vtop->r = get_reg(RC_INT);
1838 vpushi(0);
1839 vtop->r = get_reg(RC_INT);
1840 beg = intr(vtop[-4].r); // x0
1841 end = intr(vtop[-3].r); // x1
1842 dsz = intr(vtop[-2].r); // x2
1843 isz = intr(vtop[-1].r); // x3
1844 p = intr(vtop[0].r); // x4
1845 vtop -= 5;
1847 o(0xd53b0020 | isz); // mrs x(isz),ctr_el0
1848 o(0x52800080 | p); // mov w(p),#4
1849 o(0x53104c00 | dsz | isz << 5); // ubfx w(dsz),w(isz),#16,#4
1850 o(0x1ac02000 | dsz | p << 5 | dsz << 16); // lsl w(dsz),w(p),w(dsz)
1851 o(0x12000c00 | isz | isz << 5); // and w(isz),w(isz),#15
1852 o(0x1ac02000 | isz | p << 5 | isz << 16); // lsl w(isz),w(p),w(isz)
1853 o(0x51000400 | p | dsz << 5); // sub w(p),w(dsz),#1
1854 o(0x8a240004 | p | beg << 5 | p << 16); // bic x(p),x(beg),x(p)
1855 b1 = ind; o(0x14000000); // b
1856 lab1 = ind;
1857 o(0xd50b7b20 | p); // dc cvau,x(p)
1858 o(0x8b000000 | p | p << 5 | dsz << 16); // add x(p),x(p),x(dsz)
1859 write32le(cur_text_section->data + b1, 0x14000000 | (ind - b1) >> 2);
1860 o(0xeb00001f | p << 5 | end << 16); // cmp x(p),x(end)
1861 o(0x54ffffa3 | ((lab1 - ind) << 3 & 0xffffe0)); // b.cc lab1
1862 o(0xd5033b9f); // dsb ish
1863 o(0x51000400 | p | isz << 5); // sub w(p),w(isz),#1
1864 o(0x8a240004 | p | beg << 5 | p << 16); // bic x(p),x(beg),x(p)
1865 b1 = ind; o(0x14000000); // b
1866 lab1 = ind;
1867 o(0xd50b7520 | p); // ic ivau,x(p)
1868 o(0x8b000000 | p | p << 5 | isz << 16); // add x(p),x(p),x(isz)
1869 write32le(cur_text_section->data + b1, 0x14000000 | (ind - b1) >> 2);
1870 o(0xeb00001f | p << 5 | end << 16); // cmp x(p),x(end)
1871 o(0x54ffffa3 | ((lab1 - ind) << 3 & 0xffffe0)); // b.cc lab1
1872 o(0xd5033b9f); // dsb ish
1873 o(0xd5033fdf); // isb
1876 ST_FUNC void gen_vla_sp_save(int addr) {
1877 uint32_t r = intr(get_reg(RC_INT));
1878 o(0x910003e0 | r); // mov x(r),sp
1879 arm64_strx(3, r, 29, addr);
1882 ST_FUNC void gen_vla_sp_restore(int addr) {
1883 // Use x30 because this function can be called when there
1884 // is a live return value in x0 but there is nothing on
1885 // the value stack to prevent get_reg from returning x0.
1886 uint32_t r = 30;
1887 arm64_ldrx(0, 3, r, 29, addr);
1888 o(0x9100001f | r << 5); // mov sp,x(r)
1891 ST_FUNC void gen_vla_alloc(CType *type, int align) {
1892 uint32_t r = intr(gv(RC_INT));
1893 o(0x91003c00 | r | r << 5); // add x(r),x(r),#15
1894 o(0x927cec00 | r | r << 5); // bic x(r),x(r),#15
1895 o(0xcb2063ff | r << 16); // sub sp,sp,x(r)
1896 vpop();
1899 /* end of A64 code generator */
1900 /*************************************************************/
1901 #endif
1902 /*************************************************************/