Support --version cmdline arg
[tinycc.git] / arm64-gen.c
bloba73018e93f2457e720cf7b725c46140eab263e8d
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 #define IS_FREG(x) ((x) >= TREG_F(0))
86 static uint32_t intr(int r)
88 assert(TREG_R(0) <= r && r <= TREG_R30);
89 return r < TREG_R30 ? r : 30;
92 static uint32_t fltr(int r)
94 assert(TREG_F(0) <= r && r <= TREG_F(7));
95 return r - TREG_F(0);
98 // Add an instruction to text section:
99 ST_FUNC void o(unsigned int c)
101 int ind1 = ind + 4;
102 if (nocode_wanted)
103 return;
104 if (ind1 > cur_text_section->data_allocated)
105 section_realloc(cur_text_section, ind1);
106 write32le(cur_text_section->data + ind, c);
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 t = next;
243 static int arm64_type_size(int t)
246 * case values are in increasing order (from 1 to 11).
247 * which 'may' help compiler optimizers. See tcc.h
249 switch (t & VT_BTYPE) {
250 case VT_BYTE: return 0;
251 case VT_SHORT: return 1;
252 case VT_INT: return 2;
253 case VT_LLONG: return 3;
254 case VT_PTR: return 3;
255 case VT_FUNC: return 3;
256 case VT_STRUCT: return 3;
257 case VT_FLOAT: return 2;
258 case VT_DOUBLE: return 3;
259 case VT_LDOUBLE: return 4;
260 case VT_BOOL: return 0;
262 assert(0);
263 return 0;
266 static void arm64_spoff(int reg, uint64_t off)
268 uint32_t sub = off >> 63;
269 if (sub)
270 off = -off;
271 if (off < 4096)
272 o(0x910003e0 | sub << 30 | reg | off << 10);
273 // (add|sub) x(reg),sp,#(off)
274 else {
275 arm64_movimm(30, off); // use x30 for offset
276 o(0x8b3e63e0 | sub << 30 | reg); // (add|sub) x(reg),sp,x30
280 static void arm64_ldrx(int sg, int sz_, int dst, int bas, uint64_t off)
282 uint32_t sz = sz_;
283 if (sz >= 2)
284 sg = 0;
285 if (!(off & ~((uint32_t)0xfff << sz)))
286 o(0x39400000 | dst | bas << 5 | off << (10 - sz) |
287 (uint32_t)!!sg << 23 | sz << 30); // ldr(*) x(dst),[x(bas),#(off)]
288 else if (off < 256 || -off <= 256)
289 o(0x38400000 | dst | bas << 5 | (off & 511) << 12 |
290 (uint32_t)!!sg << 23 | sz << 30); // ldur(*) x(dst),[x(bas),#(off)]
291 else {
292 arm64_movimm(30, off); // use x30 for offset
293 o(0x38206800 | dst | bas << 5 | (uint32_t)30 << 16 |
294 (uint32_t)(!!sg + 1) << 22 | sz << 30); // ldr(*) x(dst),[x(bas),x30]
298 static void arm64_ldrv(int sz_, int dst, int bas, uint64_t off)
300 uint32_t sz = sz_;
301 if (!(off & ~((uint32_t)0xfff << sz)))
302 o(0x3d400000 | dst | bas << 5 | off << (10 - sz) |
303 (sz & 4) << 21 | (sz & 3) << 30); // ldr (s|d|q)(dst),[x(bas),#(off)]
304 else if (off < 256 || -off <= 256)
305 o(0x3c400000 | dst | bas << 5 | (off & 511) << 12 |
306 (sz & 4) << 21 | (sz & 3) << 30); // ldur (s|d|q)(dst),[x(bas),#(off)]
307 else {
308 arm64_movimm(30, off); // use x30 for offset
309 o(0x3c606800 | dst | bas << 5 | (uint32_t)30 << 16 |
310 sz << 30 | (sz & 4) << 21); // ldr (s|d|q)(dst),[x(bas),x30]
314 static void arm64_ldrs(int reg_, int size)
316 uint32_t reg = reg_;
317 // Use x30 for intermediate value in some cases.
318 switch (size) {
319 default: assert(0); break;
320 case 1:
321 arm64_ldrx(0, 0, reg, reg, 0);
322 break;
323 case 2:
324 arm64_ldrx(0, 1, reg, reg, 0);
325 break;
326 case 3:
327 arm64_ldrx(0, 1, 30, reg, 0);
328 arm64_ldrx(0, 0, reg, reg, 2);
329 o(0x2a0043c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #16
330 break;
331 case 4:
332 arm64_ldrx(0, 2, reg, reg, 0);
333 break;
334 case 5:
335 arm64_ldrx(0, 2, 30, reg, 0);
336 arm64_ldrx(0, 0, reg, reg, 4);
337 o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32
338 break;
339 case 6:
340 arm64_ldrx(0, 2, 30, reg, 0);
341 arm64_ldrx(0, 1, reg, reg, 4);
342 o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32
343 break;
344 case 7:
345 arm64_ldrx(0, 2, 30, reg, 0);
346 arm64_ldrx(0, 2, reg, reg, 3);
347 o(0x53087c00 | reg | reg << 5); // lsr w(reg), w(reg), #8
348 o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32
349 break;
350 case 8:
351 arm64_ldrx(0, 3, reg, reg, 0);
352 break;
353 case 9:
354 arm64_ldrx(0, 0, reg + 1, reg, 8);
355 arm64_ldrx(0, 3, reg, reg, 0);
356 break;
357 case 10:
358 arm64_ldrx(0, 1, reg + 1, reg, 8);
359 arm64_ldrx(0, 3, reg, reg, 0);
360 break;
361 case 11:
362 arm64_ldrx(0, 2, reg + 1, reg, 7);
363 o(0x53087c00 | (reg+1) | (reg+1) << 5); // lsr w(reg+1), w(reg+1), #8
364 arm64_ldrx(0, 3, reg, reg, 0);
365 break;
366 case 12:
367 arm64_ldrx(0, 2, reg + 1, reg, 8);
368 arm64_ldrx(0, 3, reg, reg, 0);
369 break;
370 case 13:
371 arm64_ldrx(0, 3, reg + 1, reg, 5);
372 o(0xd358fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #24
373 arm64_ldrx(0, 3, reg, reg, 0);
374 break;
375 case 14:
376 arm64_ldrx(0, 3, reg + 1, reg, 6);
377 o(0xd350fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #16
378 arm64_ldrx(0, 3, reg, reg, 0);
379 break;
380 case 15:
381 arm64_ldrx(0, 3, reg + 1, reg, 7);
382 o(0xd348fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #8
383 arm64_ldrx(0, 3, reg, reg, 0);
384 break;
385 case 16:
386 o(0xa9400000 | reg | (reg+1) << 10 | reg << 5);
387 // ldp x(reg),x(reg+1),[x(reg)]
388 break;
392 static void arm64_strx(int sz_, int dst, int bas, uint64_t off)
394 uint32_t sz = sz_;
395 if (!(off & ~((uint32_t)0xfff << sz)))
396 o(0x39000000 | dst | bas << 5 | off << (10 - sz) | sz << 30);
397 // str(*) x(dst),[x(bas],#(off)]
398 else if (off < 256 || -off <= 256)
399 o(0x38000000 | dst | bas << 5 | (off & 511) << 12 | sz << 30);
400 // stur(*) x(dst),[x(bas],#(off)]
401 else {
402 arm64_movimm(30, off); // use x30 for offset
403 o(0x38206800 | dst | bas << 5 | (uint32_t)30 << 16 | sz << 30);
404 // str(*) x(dst),[x(bas),x30]
408 static void arm64_strv(int sz_, int dst, int bas, uint64_t off)
410 uint32_t sz = sz_;
411 if (!(off & ~((uint32_t)0xfff << sz)))
412 o(0x3d000000 | dst | bas << 5 | off << (10 - sz) |
413 (sz & 4) << 21 | (sz & 3) << 30); // str (s|d|q)(dst),[x(bas),#(off)]
414 else if (off < 256 || -off <= 256)
415 o(0x3c000000 | dst | bas << 5 | (off & 511) << 12 |
416 (sz & 4) << 21 | (sz & 3) << 30); // stur (s|d|q)(dst),[x(bas),#(off)]
417 else {
418 arm64_movimm(30, off); // use x30 for offset
419 o(0x3c206800 | dst | bas << 5 | (uint32_t)30 << 16 |
420 sz << 30 | (sz & 4) << 21); // str (s|d|q)(dst),[x(bas),x30]
424 static void arm64_sym(int r, Sym *sym, unsigned long addend)
426 // Currently TCC's linker does not generate COPY relocations for
427 // STT_OBJECTs when tcc is invoked with "-run". This typically
428 // results in "R_AARCH64_ADR_PREL_PG_HI21 relocation failed" when
429 // a program refers to stdin. A workaround is to avoid that
430 // relocation and use only relocations with unlimited range.
431 int avoid_adrp = 1;
433 if (avoid_adrp || sym->a.weak) {
434 // (GCC uses a R_AARCH64_ABS64 in this case.)
435 greloca(cur_text_section, sym, ind, R_AARCH64_MOVW_UABS_G0_NC, addend);
436 o(0xd2800000 | r); // mov x(rt),#0,lsl #0
437 greloca(cur_text_section, sym, ind, R_AARCH64_MOVW_UABS_G1_NC, addend);
438 o(0xf2a00000 | r); // movk x(rt),#0,lsl #16
439 greloca(cur_text_section, sym, ind, R_AARCH64_MOVW_UABS_G2_NC, addend);
440 o(0xf2c00000 | r); // movk x(rt),#0,lsl #32
441 greloca(cur_text_section, sym, ind, R_AARCH64_MOVW_UABS_G3, addend);
442 o(0xf2e00000 | r); // movk x(rt),#0,lsl #48
444 else {
445 greloca(cur_text_section, sym, ind, R_AARCH64_ADR_PREL_PG_HI21, addend);
446 o(0x90000000 | r);
447 greloca(cur_text_section, sym, ind, R_AARCH64_ADD_ABS_LO12_NC, addend);
448 o(0x91000000 | r | r << 5);
452 static void arm64_load_cmp(int r, SValue *sv);
454 ST_FUNC void load(int r, SValue *sv)
456 int svtt = sv->type.t;
457 int svr = sv->r;
458 int svrv = svr & VT_VALMASK;
459 uint64_t svcul = (uint32_t)sv->c.i;
460 svcul = svcul >> 31 & 1 ? svcul - ((uint64_t)1 << 32) : svcul;
462 if (svr == (VT_LOCAL | VT_LVAL)) {
463 if (IS_FREG(r))
464 arm64_ldrv(arm64_type_size(svtt), fltr(r), 29, svcul);
465 else
466 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
467 intr(r), 29, svcul);
468 return;
471 if ((svr & ~VT_VALMASK) == VT_LVAL && svrv < VT_CONST) {
472 if (IS_FREG(r))
473 arm64_ldrv(arm64_type_size(svtt), fltr(r), intr(svrv), 0);
474 else
475 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
476 intr(r), intr(svrv), 0);
477 return;
480 if (svr == (VT_CONST | VT_LVAL | VT_SYM)) {
481 arm64_sym(30, sv->sym, svcul); // use x30 for address
482 if (IS_FREG(r))
483 arm64_ldrv(arm64_type_size(svtt), fltr(r), 30, 0);
484 else
485 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
486 intr(r), 30, 0);
487 return;
490 if (svr == (VT_CONST | VT_SYM)) {
491 arm64_sym(intr(r), sv->sym, svcul);
492 return;
495 if (svr == VT_CONST) {
496 if ((svtt & VT_BTYPE) != VT_VOID)
497 arm64_movimm(intr(r), arm64_type_size(svtt) == 3 ?
498 sv->c.i : (uint32_t)svcul);
499 return;
502 if (svr < VT_CONST) {
503 if (IS_FREG(r) && IS_FREG(svr))
504 if (svtt == VT_LDOUBLE)
505 o(0x4ea01c00 | fltr(r) | fltr(svr) << 5);
506 // mov v(r).16b,v(svr).16b
507 else
508 o(0x1e604000 | fltr(r) | fltr(svr) << 5); // fmov d(r),d(svr)
509 else if (!IS_FREG(r) && !IS_FREG(svr))
510 o(0xaa0003e0 | intr(r) | intr(svr) << 16); // mov x(r),x(svr)
511 else
512 assert(0);
513 return;
516 if (svr == VT_LOCAL) {
517 if (-svcul < 0x1000)
518 o(0xd10003a0 | intr(r) | -svcul << 10); // sub x(r),x29,#...
519 else {
520 arm64_movimm(30, -svcul); // use x30 for offset
521 o(0xcb0003a0 | intr(r) | (uint32_t)30 << 16); // sub x(r),x29,x30
523 return;
526 if (svr == VT_JMP || svr == VT_JMPI) {
527 int t = (svr == VT_JMPI);
528 arm64_movimm(intr(r), t);
529 o(0x14000002); // b .+8
530 gsym(svcul);
531 arm64_movimm(intr(r), t ^ 1);
532 return;
535 if (svr == (VT_LLOCAL | VT_LVAL)) {
536 arm64_ldrx(0, 3, 30, 29, svcul); // use x30 for offset
537 if (IS_FREG(r))
538 arm64_ldrv(arm64_type_size(svtt), fltr(r), 30, 0);
539 else
540 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
541 intr(r), 30, 0);
542 return;
545 if (svr == VT_CMP) {
546 arm64_load_cmp(r, sv);
547 return;
550 printf("load(%x, (%x, %x, %llx))\n", r, svtt, sv->r, (long long)svcul);
551 assert(0);
554 ST_FUNC void store(int r, SValue *sv)
556 int svtt = sv->type.t;
557 int svr = sv->r;
558 int svrv = svr & VT_VALMASK;
559 uint64_t svcul = (uint32_t)sv->c.i;
560 svcul = svcul >> 31 & 1 ? svcul - ((uint64_t)1 << 32) : svcul;
562 if (svr == (VT_LOCAL | VT_LVAL)) {
563 if (IS_FREG(r))
564 arm64_strv(arm64_type_size(svtt), fltr(r), 29, svcul);
565 else
566 arm64_strx(arm64_type_size(svtt), intr(r), 29, svcul);
567 return;
570 if ((svr & ~VT_VALMASK) == VT_LVAL && svrv < VT_CONST) {
571 if (IS_FREG(r))
572 arm64_strv(arm64_type_size(svtt), fltr(r), intr(svrv), 0);
573 else
574 arm64_strx(arm64_type_size(svtt), intr(r), intr(svrv), 0);
575 return;
578 if (svr == (VT_CONST | VT_LVAL | VT_SYM)) {
579 arm64_sym(30, sv->sym, svcul); // use x30 for address
580 if (IS_FREG(r))
581 arm64_strv(arm64_type_size(svtt), fltr(r), 30, 0);
582 else
583 arm64_strx(arm64_type_size(svtt), intr(r), 30, 0);
584 return;
587 printf("store(%x, (%x, %x, %llx))\n", r, svtt, sv->r, (long long)svcul);
588 assert(0);
591 static void arm64_gen_bl_or_b(int b)
593 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST && (vtop->r & VT_SYM)) {
594 assert(!b);
595 greloca(cur_text_section, vtop->sym, ind, R_AARCH64_CALL26, 0);
596 o(0x94000000); // bl .
598 else
599 o(0xd61f0000 | (uint32_t)!b << 21 | intr(gv(RC_R30)) << 5); // br/blr
602 static int arm64_hfa_aux(CType *type, int *fsize, int num)
604 if (is_float(type->t)) {
605 int a, n = type_size(type, &a);
606 if (num >= 4 || (*fsize && *fsize != n))
607 return -1;
608 *fsize = n;
609 return num + 1;
611 else if ((type->t & VT_BTYPE) == VT_STRUCT) {
612 int is_struct = 0; // rather than union
613 Sym *field;
614 for (field = type->ref->next; field; field = field->next)
615 if (field->c) {
616 is_struct = 1;
617 break;
619 if (is_struct) {
620 int num0 = num;
621 for (field = type->ref->next; field; field = field->next) {
622 if (field->c != (num - num0) * *fsize)
623 return -1;
624 num = arm64_hfa_aux(&field->type, fsize, num);
625 if (num == -1)
626 return -1;
628 if (type->ref->c != (num - num0) * *fsize)
629 return -1;
630 return num;
632 else { // union
633 int num0 = num;
634 for (field = type->ref->next; field; field = field->next) {
635 int num1 = arm64_hfa_aux(&field->type, fsize, num0);
636 if (num1 == -1)
637 return -1;
638 num = num1 < num ? num : num1;
640 if (type->ref->c != (num - num0) * *fsize)
641 return -1;
642 return num;
645 else if (type->t & VT_ARRAY) {
646 int num1;
647 if (!type->ref->c)
648 return num;
649 num1 = arm64_hfa_aux(&type->ref->type, fsize, num);
650 if (num1 == -1 || (num1 != num && type->ref->c > 4))
651 return -1;
652 num1 = num + type->ref->c * (num1 - num);
653 if (num1 > 4)
654 return -1;
655 return num1;
657 return -1;
660 static int arm64_hfa(CType *type, int *fsize)
662 if ((type->t & VT_BTYPE) == VT_STRUCT || (type->t & VT_ARRAY)) {
663 int sz = 0;
664 int n = arm64_hfa_aux(type, &sz, 0);
665 if (0 < n && n <= 4) {
666 if (fsize)
667 *fsize = sz;
668 return n;
671 return 0;
674 static unsigned long arm64_pcs_aux(int n, CType **type, unsigned long *a)
676 int nx = 0; // next integer register
677 int nv = 0; // next vector register
678 unsigned long ns = 32; // next stack offset
679 int i;
681 for (i = 0; i < n; i++) {
682 int hfa = arm64_hfa(type[i], 0);
683 int size, align;
685 if ((type[i]->t & VT_ARRAY) ||
686 (type[i]->t & VT_BTYPE) == VT_FUNC)
687 size = align = 8;
688 else
689 size = type_size(type[i], &align);
691 if (hfa)
692 // B.2
694 else if (size > 16) {
695 // B.3: replace with pointer
696 if (nx < 8)
697 a[i] = nx++ << 1 | 1;
698 else {
699 ns = (ns + 7) & ~7;
700 a[i] = ns | 1;
701 ns += 8;
703 continue;
705 else if ((type[i]->t & VT_BTYPE) == VT_STRUCT)
706 // B.4
707 size = (size + 7) & ~7;
709 // C.1
710 if (is_float(type[i]->t) && nv < 8) {
711 a[i] = 16 + (nv++ << 1);
712 continue;
715 // C.2
716 if (hfa && nv + hfa <= 8) {
717 a[i] = 16 + (nv << 1);
718 nv += hfa;
719 continue;
722 // C.3
723 if (hfa) {
724 nv = 8;
725 size = (size + 7) & ~7;
728 // C.4
729 if (hfa || (type[i]->t & VT_BTYPE) == VT_LDOUBLE) {
730 ns = (ns + 7) & ~7;
731 ns = (ns + align - 1) & -align;
734 // C.5
735 if ((type[i]->t & VT_BTYPE) == VT_FLOAT)
736 size = 8;
738 // C.6
739 if (hfa || is_float(type[i]->t)) {
740 a[i] = ns;
741 ns += size;
742 continue;
745 // C.7
746 if ((type[i]->t & VT_BTYPE) != VT_STRUCT && size <= 8 && nx < 8) {
747 a[i] = nx++ << 1;
748 continue;
751 // C.8
752 if (align == 16)
753 nx = (nx + 1) & ~1;
755 // C.9
756 if ((type[i]->t & VT_BTYPE) != VT_STRUCT && size == 16 && nx < 7) {
757 a[i] = nx << 1;
758 nx += 2;
759 continue;
762 // C.10
763 if ((type[i]->t & VT_BTYPE) == VT_STRUCT && size <= (8 - nx) * 8) {
764 a[i] = nx << 1;
765 nx += (size + 7) >> 3;
766 continue;
769 // C.11
770 nx = 8;
772 // C.12
773 ns = (ns + 7) & ~7;
774 ns = (ns + align - 1) & -align;
776 // C.13
777 if ((type[i]->t & VT_BTYPE) == VT_STRUCT) {
778 a[i] = ns;
779 ns += size;
780 continue;
783 // C.14
784 if (size < 8)
785 size = 8;
787 // C.15
788 a[i] = ns;
789 ns += size;
792 return ns - 32;
795 static unsigned long arm64_pcs(int n, CType **type, unsigned long *a)
797 unsigned long stack;
799 // Return type:
800 if ((type[0]->t & VT_BTYPE) == VT_VOID)
801 a[0] = -1;
802 else {
803 arm64_pcs_aux(1, type, a);
804 assert(a[0] == 0 || a[0] == 1 || a[0] == 16);
807 // Argument types:
808 stack = arm64_pcs_aux(n, type + 1, a + 1);
810 if (0) {
811 int i;
812 for (i = 0; i <= n; i++) {
813 if (!i)
814 printf("arm64_pcs return: ");
815 else
816 printf("arm64_pcs arg %d: ", i);
817 if (a[i] == (unsigned long)-1)
818 printf("void\n");
819 else if (a[i] == 1 && !i)
820 printf("X8 pointer\n");
821 else if (a[i] < 16)
822 printf("X%lu%s\n", a[i] / 2, a[i] & 1 ? " pointer" : "");
823 else if (a[i] < 32)
824 printf("V%lu\n", a[i] / 2 - 8);
825 else
826 printf("stack %lu%s\n",
827 (a[i] - 32) & ~1, a[i] & 1 ? " pointer" : "");
831 return stack;
834 ST_FUNC void gfunc_call(int nb_args)
836 CType *return_type;
837 CType **t;
838 unsigned long *a, *a1;
839 unsigned long stack;
840 int i;
842 return_type = &vtop[-nb_args].type.ref->type;
843 if ((return_type->t & VT_BTYPE) == VT_STRUCT)
844 --nb_args;
846 t = tcc_malloc((nb_args + 1) * sizeof(*t));
847 a = tcc_malloc((nb_args + 1) * sizeof(*a));
848 a1 = tcc_malloc((nb_args + 1) * sizeof(*a1));
850 t[0] = return_type;
851 for (i = 0; i < nb_args; i++)
852 t[nb_args - i] = &vtop[-i].type;
854 stack = arm64_pcs(nb_args, t, a);
856 // Allocate space for structs replaced by pointer:
857 for (i = nb_args; i; i--)
858 if (a[i] & 1) {
859 SValue *arg = &vtop[i - nb_args];
860 int align, size = type_size(&arg->type, &align);
861 assert((arg->type.t & VT_BTYPE) == VT_STRUCT);
862 stack = (stack + align - 1) & -align;
863 a1[i] = stack;
864 stack += size;
867 stack = (stack + 15) >> 4 << 4;
869 assert(stack < 0x1000);
870 if (stack)
871 o(0xd10003ff | stack << 10); // sub sp,sp,#(n)
873 // First pass: set all values on stack
874 for (i = nb_args; i; i--) {
875 vpushv(vtop - nb_args + i);
877 if (a[i] & 1) {
878 // struct replaced by pointer
879 int r = get_reg(RC_INT);
880 arm64_spoff(intr(r), a1[i]);
881 vset(&vtop->type, r | VT_LVAL, 0);
882 vswap();
883 vstore();
884 if (a[i] >= 32) {
885 // pointer on stack
886 r = get_reg(RC_INT);
887 arm64_spoff(intr(r), a1[i]);
888 arm64_strx(3, intr(r), 31, (a[i] - 32) >> 1 << 1);
891 else if (a[i] >= 32) {
892 // value on stack
893 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
894 int r = get_reg(RC_INT);
895 arm64_spoff(intr(r), a[i] - 32);
896 vset(&vtop->type, r | VT_LVAL, 0);
897 vswap();
898 vstore();
900 else if (is_float(vtop->type.t)) {
901 gv(RC_FLOAT);
902 arm64_strv(arm64_type_size(vtop[0].type.t),
903 fltr(vtop[0].r), 31, a[i] - 32);
905 else {
906 gv(RC_INT);
907 arm64_strx(arm64_type_size(vtop[0].type.t),
908 intr(vtop[0].r), 31, a[i] - 32);
912 --vtop;
915 // Second pass: assign values to registers
916 for (i = nb_args; i; i--, vtop--) {
917 if (a[i] < 16 && !(a[i] & 1)) {
918 // value in general-purpose registers
919 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
920 int align, size = type_size(&vtop->type, &align);
921 vtop->type.t = VT_PTR;
922 gaddrof();
923 gv(RC_R(a[i] / 2));
924 arm64_ldrs(a[i] / 2, size);
926 else
927 gv(RC_R(a[i] / 2));
929 else if (a[i] < 16)
930 // struct replaced by pointer in register
931 arm64_spoff(a[i] / 2, a1[i]);
932 else if (a[i] < 32) {
933 // value in floating-point registers
934 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
935 uint32_t j, sz, n = arm64_hfa(&vtop->type, &sz);
936 vtop->type.t = VT_PTR;
937 gaddrof();
938 gv(RC_R30);
939 for (j = 0; j < n; j++)
940 o(0x3d4003c0 |
941 (sz & 16) << 19 | -(sz & 8) << 27 | (sz & 4) << 29 |
942 (a[i] / 2 - 8 + j) |
943 j << 10); // ldr ([sdq])(*),[x30,#(j * sz)]
945 else
946 gv(RC_F(a[i] / 2 - 8));
950 if ((return_type->t & VT_BTYPE) == VT_STRUCT) {
951 if (a[0] == 1) {
952 // indirect return: set x8 and discard the stack value
953 gv(RC_R(8));
954 --vtop;
956 else
957 // return in registers: keep the address for after the call
958 vswap();
961 save_regs(0);
962 arm64_gen_bl_or_b(0);
963 --vtop;
964 if (stack)
965 o(0x910003ff | stack << 10); // add sp,sp,#(n)
968 int rt = return_type->t;
969 int bt = rt & VT_BTYPE;
970 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(Sym *func_sym)
1006 CType *func_type = &func_sym->type;
1007 int n = 0;
1008 int i = 0;
1009 Sym *sym;
1010 CType **t;
1011 unsigned long *a;
1013 // Why doesn't the caller (gen_function) set func_vt?
1014 func_vt = func_type->ref->type;
1015 func_vc = 144; // offset of where x8 is stored
1017 for (sym = func_type->ref; sym; sym = sym->next)
1018 ++n;
1019 t = tcc_malloc(n * sizeof(*t));
1020 a = tcc_malloc(n * sizeof(*a));
1022 for (sym = func_type->ref; sym; sym = sym->next)
1023 t[i++] = &sym->type;
1025 arm64_func_va_list_stack = arm64_pcs(n - 1, t, a);
1027 o(0xa9b27bfd); // stp x29,x30,[sp,#-224]!
1028 o(0xad0087e0); // stp q0,q1,[sp,#16]
1029 o(0xad018fe2); // stp q2,q3,[sp,#48]
1030 o(0xad0297e4); // stp q4,q5,[sp,#80]
1031 o(0xad039fe6); // stp q6,q7,[sp,#112]
1032 o(0xa90923e8); // stp x8,x8,[sp,#144]
1033 o(0xa90a07e0); // stp x0,x1,[sp,#160]
1034 o(0xa90b0fe2); // stp x2,x3,[sp,#176]
1035 o(0xa90c17e4); // stp x4,x5,[sp,#192]
1036 o(0xa90d1fe6); // stp x6,x7,[sp,#208]
1038 arm64_func_va_list_gr_offs = -64;
1039 arm64_func_va_list_vr_offs = -128;
1041 for (i = 1, sym = func_type->ref->next; sym; i++, sym = sym->next) {
1042 int off = (a[i] < 16 ? 160 + a[i] / 2 * 8 :
1043 a[i] < 32 ? 16 + (a[i] - 16) / 2 * 16 :
1044 224 + ((a[i] - 32) >> 1 << 1));
1045 sym_push(sym->v & ~SYM_FIELD, &sym->type,
1046 (a[i] & 1 ? VT_LLOCAL : VT_LOCAL) | VT_LVAL,
1047 off);
1049 if (a[i] < 16) {
1050 int align, size = type_size(&sym->type, &align);
1051 arm64_func_va_list_gr_offs = (a[i] / 2 - 7 +
1052 (!(a[i] & 1) && size > 8)) * 8;
1054 else if (a[i] < 32) {
1055 uint32_t hfa = arm64_hfa(&sym->type, 0);
1056 arm64_func_va_list_vr_offs = (a[i] / 2 - 16 +
1057 (hfa ? hfa : 1)) * 16;
1060 // HFAs of float and double need to be written differently:
1061 if (16 <= a[i] && a[i] < 32 && (sym->type.t & VT_BTYPE) == VT_STRUCT) {
1062 uint32_t j, sz, k = arm64_hfa(&sym->type, &sz);
1063 if (sz < 16)
1064 for (j = 0; j < k; j++) {
1065 o(0x3d0003e0 | -(sz & 8) << 27 | (sz & 4) << 29 |
1066 ((a[i] - 16) / 2 + j) | (off / sz + j) << 10);
1067 // str ([sdq])(*),[sp,#(j * sz)]
1072 tcc_free(a);
1073 tcc_free(t);
1075 o(0x910003fd); // mov x29,sp
1076 arm64_func_sub_sp_offset = ind;
1077 // In gfunc_epilog these will be replaced with code to decrement SP:
1078 o(0xd503201f); // nop
1079 o(0xd503201f); // nop
1080 loc = 0;
1083 ST_FUNC void gen_va_start(void)
1085 int r;
1086 --vtop; // we don't need the "arg"
1087 gaddrof();
1088 r = intr(gv(RC_INT));
1090 if (arm64_func_va_list_stack) {
1091 //xx could use add (immediate) here
1092 arm64_movimm(30, arm64_func_va_list_stack + 224);
1093 o(0x8b1e03be); // add x30,x29,x30
1095 else
1096 o(0x910383be); // add x30,x29,#224
1097 o(0xf900001e | r << 5); // str x30,[x(r)]
1099 if (arm64_func_va_list_gr_offs) {
1100 if (arm64_func_va_list_stack)
1101 o(0x910383be); // add x30,x29,#224
1102 o(0xf900041e | r << 5); // str x30,[x(r),#8]
1105 if (arm64_func_va_list_vr_offs) {
1106 o(0x910243be); // add x30,x29,#144
1107 o(0xf900081e | r << 5); // str x30,[x(r),#16]
1110 arm64_movimm(30, arm64_func_va_list_gr_offs);
1111 o(0xb900181e | r << 5); // str w30,[x(r),#24]
1113 arm64_movimm(30, arm64_func_va_list_vr_offs);
1114 o(0xb9001c1e | r << 5); // str w30,[x(r),#28]
1116 --vtop;
1119 ST_FUNC void gen_va_arg(CType *t)
1121 int align, size = type_size(t, &align);
1122 int fsize, hfa = arm64_hfa(t, &fsize);
1123 uint32_t r0, r1;
1125 if (is_float(t->t)) {
1126 hfa = 1;
1127 fsize = size;
1130 gaddrof();
1131 r0 = intr(gv(RC_INT));
1132 r1 = get_reg(RC_INT);
1133 vtop[0].r = r1 | VT_LVAL;
1134 r1 = intr(r1);
1136 if (!hfa) {
1137 uint32_t n = size > 16 ? 8 : (size + 7) & -8;
1138 o(0xb940181e | r0 << 5); // ldr w30,[x(r0),#24] // __gr_offs
1139 if (align == 16) {
1140 assert(0); // this path untested but needed for __uint128_t
1141 o(0x11003fde); // add w30,w30,#15
1142 o(0x121c6fde); // and w30,w30,#-16
1144 o(0x310003c0 | r1 | n << 10); // adds w(r1),w30,#(n)
1145 o(0x540000ad); // b.le .+20
1146 o(0xf9400000 | r1 | r0 << 5); // ldr x(r1),[x(r0)] // __stack
1147 o(0x9100001e | r1 << 5 | n << 10); // add x30,x(r1),#(n)
1148 o(0xf900001e | r0 << 5); // str x30,[x(r0)] // __stack
1149 o(0x14000004); // b .+16
1150 o(0xb9001800 | r1 | r0 << 5); // str w(r1),[x(r0),#24] // __gr_offs
1151 o(0xf9400400 | r1 | r0 << 5); // ldr x(r1),[x(r0),#8] // __gr_top
1152 o(0x8b3ec000 | r1 | r1 << 5); // add x(r1),x(r1),w30,sxtw
1153 if (size > 16)
1154 o(0xf9400000 | r1 | r1 << 5); // ldr x(r1),[x(r1)]
1156 else {
1157 uint32_t rsz = hfa << 4;
1158 uint32_t ssz = (size + 7) & -(uint32_t)8;
1159 uint32_t b1, b2;
1160 o(0xb9401c1e | r0 << 5); // ldr w30,[x(r0),#28] // __vr_offs
1161 o(0x310003c0 | r1 | rsz << 10); // adds w(r1),w30,#(rsz)
1162 b1 = ind; o(0x5400000d); // b.le lab1
1163 o(0xf9400000 | r1 | r0 << 5); // ldr x(r1),[x(r0)] // __stack
1164 if (fsize == 16) {
1165 o(0x91003c00 | r1 | r1 << 5); // add x(r1),x(r1),#15
1166 o(0x927cec00 | r1 | r1 << 5); // and x(r1),x(r1),#-16
1168 o(0x9100001e | r1 << 5 | ssz << 10); // add x30,x(r1),#(ssz)
1169 o(0xf900001e | r0 << 5); // str x30,[x(r0)] // __stack
1170 b2 = ind; o(0x14000000); // b lab2
1171 // lab1:
1172 write32le(cur_text_section->data + b1, 0x5400000d | (ind - b1) << 3);
1173 o(0xb9001c00 | r1 | r0 << 5); // str w(r1),[x(r0),#28] // __vr_offs
1174 o(0xf9400800 | r1 | r0 << 5); // ldr x(r1),[x(r0),#16] // __vr_top
1175 if (hfa == 1 || fsize == 16)
1176 o(0x8b3ec000 | r1 | r1 << 5); // add x(r1),x(r1),w30,sxtw
1177 else {
1178 // We need to change the layout of this HFA.
1179 // Get some space on the stack using global variable "loc":
1180 loc = (loc - size) & -(uint32_t)align;
1181 o(0x8b3ec000 | 30 | r1 << 5); // add x30,x(r1),w30,sxtw
1182 arm64_movimm(r1, loc);
1183 o(0x8b0003a0 | r1 | r1 << 16); // add x(r1),x29,x(r1)
1184 o(0x4c402bdc | (uint32_t)fsize << 7 |
1185 (uint32_t)(hfa == 2) << 15 |
1186 (uint32_t)(hfa == 3) << 14); // ld1 {v28.(4s|2d),...},[x30]
1187 o(0x0d00801c | r1 << 5 | (fsize == 8) << 10 |
1188 (uint32_t)(hfa != 2) << 13 |
1189 (uint32_t)(hfa != 3) << 21); // st(hfa) {v28.(s|d),...}[0],[x(r1)]
1191 // lab2:
1192 write32le(cur_text_section->data + b2, 0x14000000 | (ind - b2) >> 2);
1196 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret,
1197 int *align, int *regsize)
1199 return 0;
1202 ST_FUNC void gfunc_return(CType *func_type)
1204 CType *t = func_type;
1205 unsigned long a;
1207 arm64_pcs(0, &t, &a);
1208 switch (a) {
1209 case -1:
1210 break;
1211 case 0:
1212 if ((func_type->t & VT_BTYPE) == VT_STRUCT) {
1213 int align, size = type_size(func_type, &align);
1214 gaddrof();
1215 gv(RC_R(0));
1216 arm64_ldrs(0, size);
1218 else
1219 gv(RC_IRET);
1220 break;
1221 case 1: {
1222 CType type = *func_type;
1223 mk_pointer(&type);
1224 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
1225 indir();
1226 vswap();
1227 vstore();
1228 break;
1230 case 16:
1231 if ((func_type->t & VT_BTYPE) == VT_STRUCT) {
1232 uint32_t j, sz, n = arm64_hfa(&vtop->type, &sz);
1233 gaddrof();
1234 gv(RC_R(0));
1235 for (j = 0; j < n; j++)
1236 o(0x3d400000 |
1237 (sz & 16) << 19 | -(sz & 8) << 27 | (sz & 4) << 29 |
1238 j | j << 10); // ldr ([sdq])(*),[x0,#(j * sz)]
1240 else
1241 gv(RC_FRET);
1242 break;
1243 default:
1244 assert(0);
1246 vtop--;
1249 ST_FUNC void gfunc_epilog(void)
1251 if (loc) {
1252 // Insert instructions to subtract size of stack frame from SP.
1253 unsigned char *ptr = cur_text_section->data + arm64_func_sub_sp_offset;
1254 uint64_t diff = (-loc + 15) & ~15;
1255 if (!(diff >> 24)) {
1256 if (diff & 0xfff) // sub sp,sp,#(diff & 0xfff)
1257 write32le(ptr, 0xd10003ff | (diff & 0xfff) << 10);
1258 if (diff >> 12) // sub sp,sp,#(diff >> 12),lsl #12
1259 write32le(ptr + 4, 0xd14003ff | (diff >> 12) << 10);
1261 else {
1262 // In this case we may subtract more than necessary,
1263 // but always less than 17/16 of what we were aiming for.
1264 int i = 0;
1265 int j = 0;
1266 while (diff >> 20) {
1267 diff = (diff + 0xffff) >> 16;
1268 ++i;
1270 while (diff >> 16) {
1271 diff = (diff + 1) >> 1;
1272 ++j;
1274 write32le(ptr, 0xd2800010 | diff << 5 | i << 21);
1275 // mov x16,#(diff),lsl #(16 * i)
1276 write32le(ptr + 4, 0xcb3063ff | j << 10);
1277 // sub sp,sp,x16,lsl #(j)
1280 o(0x910003bf); // mov sp,x29
1281 o(0xa8ce7bfd); // ldp x29,x30,[sp],#224
1283 o(0xd65f03c0); // ret
1286 ST_FUNC void gen_fill_nops(int bytes)
1288 if ((bytes & 3))
1289 tcc_error("alignment of code section not multiple of 4");
1290 while (bytes > 0) {
1291 o(0xd503201f); // nop
1292 bytes -= 4;
1296 // Generate forward branch to label:
1297 ST_FUNC int gjmp(int t)
1299 int r = ind;
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));
1313 ST_FUNC int gjmp_append(int n, int t)
1315 void *p;
1316 /* insert vtop->c jump list in t */
1317 if (n) {
1318 uint32_t n1 = n, n2;
1319 while ((n2 = read32le(p = cur_text_section->data + n1)))
1320 n1 = n2;
1321 write32le(p, t);
1322 t = n;
1324 return t;
1327 void arm64_vset_VT_CMP(int op)
1329 if (op >= TOK_ULT && op <= TOK_GT) {
1330 vtop->cmp_r = vtop->r;
1331 vset_VT_CMP(0x80);
1335 static void arm64_gen_opil(int op, uint32_t l);
1337 static void arm64_load_cmp(int r, SValue *sv)
1339 sv->r = sv->cmp_r;
1340 if (sv->c.i & 1) {
1341 vpushi(1);
1342 arm64_gen_opil('^', 0);
1344 if (r != sv->r) {
1345 load(r, sv);
1346 sv->r = r;
1350 ST_FUNC int gjmp_cond(int op, int t)
1352 int bt = vtop->type.t & VT_BTYPE;
1354 int inv = op & 1;
1355 vtop->r = vtop->cmp_r;
1357 if (bt == VT_LDOUBLE) {
1358 uint32_t a, b, f = fltr(gv(RC_FLOAT));
1359 a = get_reg(RC_INT);
1360 vpushi(0);
1361 vtop[0].r = a;
1362 b = get_reg(RC_INT);
1363 a = intr(a);
1364 b = intr(b);
1365 o(0x4e083c00 | a | f << 5); // mov x(a),v(f).d[0]
1366 o(0x4e183c00 | b | f << 5); // mov x(b),v(f).d[1]
1367 o(0xaa000400 | a | a << 5 | b << 16); // orr x(a),x(a),x(b),lsl #1
1368 o(0xb4000040 | a | !!inv << 24); // cbz/cbnz x(a),.+8
1369 --vtop;
1371 else if (bt == VT_FLOAT || bt == VT_DOUBLE) {
1372 uint32_t a = fltr(gv(RC_FLOAT));
1373 o(0x1e202008 | a << 5 | (bt != VT_FLOAT) << 22); // fcmp
1374 o(0x54000040 | !!inv); // b.eq/b.ne .+8
1376 else {
1377 uint32_t ll = (bt == VT_PTR || bt == VT_LLONG);
1378 uint32_t a = intr(gv(RC_INT));
1379 o(0x34000040 | a | !!inv << 24 | ll << 31); // cbz/cbnz wA,.+8
1381 return gjmp(t);
1384 static int arm64_iconst(uint64_t *val, SValue *sv)
1386 if ((sv->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
1387 return 0;
1388 if (val) {
1389 int t = sv->type.t;
1390 int bt = t & VT_BTYPE;
1391 *val = ((bt == VT_LLONG || bt == VT_PTR) ? sv->c.i :
1392 (uint32_t)sv->c.i |
1393 (t & VT_UNSIGNED ? 0 : -(sv->c.i & 0x80000000)));
1395 return 1;
1398 static int arm64_gen_opic(int op, uint32_t l, int rev, uint64_t val,
1399 uint32_t x, uint32_t a)
1401 if (op == '-' && !rev) {
1402 val = -val;
1403 op = '+';
1405 val = l ? val : (uint32_t)val;
1407 switch (op) {
1409 case '+': {
1410 uint32_t s = l ? val >> 63 : val >> 31;
1411 val = s ? -val : val;
1412 val = l ? val : (uint32_t)val;
1413 if (!(val & ~(uint64_t)0xfff))
1414 o(0x11000000 | l << 31 | s << 30 | x | a << 5 | val << 10);
1415 else if (!(val & ~(uint64_t)0xfff000))
1416 o(0x11400000 | l << 31 | s << 30 | x | a << 5 | val >> 12 << 10);
1417 else {
1418 arm64_movimm(30, val); // use x30
1419 o(0x0b1e0000 | l << 31 | s << 30 | x | a << 5);
1421 return 1;
1424 case '-':
1425 if (!val)
1426 o(0x4b0003e0 | l << 31 | x | a << 16); // neg
1427 else if (val == (l ? (uint64_t)-1 : (uint32_t)-1))
1428 o(0x2a2003e0 | l << 31 | x | a << 16); // mvn
1429 else {
1430 arm64_movimm(30, val); // use x30
1431 o(0x4b0003c0 | l << 31 | x | a << 16); // sub
1433 return 1;
1435 case '^':
1436 if (val == -1 || (val == 0xffffffff && !l)) {
1437 o(0x2a2003e0 | l << 31 | x | a << 16); // mvn
1438 return 1;
1440 // fall through
1441 case '&':
1442 case '|': {
1443 int e = arm64_encode_bimm64(l ? val : val | val << 32);
1444 if (e < 0)
1445 return 0;
1446 o((op == '&' ? 0x12000000 :
1447 op == '|' ? 0x32000000 : 0x52000000) |
1448 l << 31 | x | a << 5 | (uint32_t)e << 10);
1449 return 1;
1452 case TOK_SAR:
1453 case TOK_SHL:
1454 case TOK_SHR: {
1455 uint32_t n = 32 << l;
1456 val = val & (n - 1);
1457 if (rev)
1458 return 0;
1459 if (!val)
1460 assert(0);
1461 else if (op == TOK_SHL)
1462 o(0x53000000 | l << 31 | l << 22 | x | a << 5 |
1463 (n - val) << 16 | (n - 1 - val) << 10); // lsl
1464 else
1465 o(0x13000000 | (op == TOK_SHR) << 30 | l << 31 | l << 22 |
1466 x | a << 5 | val << 16 | (n - 1) << 10); // lsr/asr
1467 return 1;
1471 return 0;
1474 static void arm64_gen_opil(int op, uint32_t l)
1476 uint32_t x, a, b;
1478 // Special treatment for operations with a constant operand:
1480 uint64_t val;
1481 int rev = 1;
1483 if (arm64_iconst(0, &vtop[0])) {
1484 vswap();
1485 rev = 0;
1487 if (arm64_iconst(&val, &vtop[-1])) {
1488 gv(RC_INT);
1489 a = intr(vtop[0].r);
1490 --vtop;
1491 x = get_reg(RC_INT);
1492 ++vtop;
1493 if (arm64_gen_opic(op, l, rev, val, intr(x), a)) {
1494 vtop[0].r = x;
1495 vswap();
1496 --vtop;
1497 return;
1500 if (!rev)
1501 vswap();
1504 gv2(RC_INT, RC_INT);
1505 assert(vtop[-1].r < VT_CONST && vtop[0].r < VT_CONST);
1506 a = intr(vtop[-1].r);
1507 b = intr(vtop[0].r);
1508 vtop -= 2;
1509 x = get_reg(RC_INT);
1510 ++vtop;
1511 vtop[0].r = x;
1512 x = intr(x);
1514 switch (op) {
1515 case '%':
1516 // Use x30 for quotient:
1517 o(0x1ac00c00 | l << 31 | 30 | a << 5 | b << 16); // sdiv
1518 o(0x1b008000 | l << 31 | x | (uint32_t)30 << 5 |
1519 b << 16 | a << 10); // msub
1520 break;
1521 case '&':
1522 o(0x0a000000 | l << 31 | x | a << 5 | b << 16); // and
1523 break;
1524 case '*':
1525 o(0x1b007c00 | l << 31 | x | a << 5 | b << 16); // mul
1526 break;
1527 case '+':
1528 o(0x0b000000 | l << 31 | x | a << 5 | b << 16); // add
1529 break;
1530 case '-':
1531 o(0x4b000000 | l << 31 | x | a << 5 | b << 16); // sub
1532 break;
1533 case '/':
1534 o(0x1ac00c00 | l << 31 | x | a << 5 | b << 16); // sdiv
1535 break;
1536 case '^':
1537 o(0x4a000000 | l << 31 | x | a << 5 | b << 16); // eor
1538 break;
1539 case '|':
1540 o(0x2a000000 | l << 31 | x | a << 5 | b << 16); // orr
1541 break;
1542 case TOK_EQ:
1543 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1544 o(0x1a9f17e0 | x); // cset wA,eq
1545 break;
1546 case TOK_GE:
1547 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1548 o(0x1a9fb7e0 | x); // cset wA,ge
1549 break;
1550 case TOK_GT:
1551 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1552 o(0x1a9fd7e0 | x); // cset wA,gt
1553 break;
1554 case TOK_LE:
1555 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1556 o(0x1a9fc7e0 | x); // cset wA,le
1557 break;
1558 case TOK_LT:
1559 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1560 o(0x1a9fa7e0 | x); // cset wA,lt
1561 break;
1562 case TOK_NE:
1563 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1564 o(0x1a9f07e0 | x); // cset wA,ne
1565 break;
1566 case TOK_SAR:
1567 o(0x1ac02800 | l << 31 | x | a << 5 | b << 16); // asr
1568 break;
1569 case TOK_SHL:
1570 o(0x1ac02000 | l << 31 | x | a << 5 | b << 16); // lsl
1571 break;
1572 case TOK_SHR:
1573 o(0x1ac02400 | l << 31 | x | a << 5 | b << 16); // lsr
1574 break;
1575 case TOK_UDIV:
1576 case TOK_PDIV:
1577 o(0x1ac00800 | l << 31 | x | a << 5 | b << 16); // udiv
1578 break;
1579 case TOK_UGE:
1580 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1581 o(0x1a9f37e0 | x); // cset wA,cs
1582 break;
1583 case TOK_UGT:
1584 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1585 o(0x1a9f97e0 | x); // cset wA,hi
1586 break;
1587 case TOK_ULT:
1588 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1589 o(0x1a9f27e0 | x); // cset wA,cc
1590 break;
1591 case TOK_ULE:
1592 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1593 o(0x1a9f87e0 | x); // cset wA,ls
1594 break;
1595 case TOK_UMOD:
1596 // Use x30 for quotient:
1597 o(0x1ac00800 | l << 31 | 30 | a << 5 | b << 16); // udiv
1598 o(0x1b008000 | l << 31 | x | (uint32_t)30 << 5 |
1599 b << 16 | a << 10); // msub
1600 break;
1601 default:
1602 assert(0);
1606 ST_FUNC void gen_opi(int op)
1608 arm64_gen_opil(op, 0);
1609 arm64_vset_VT_CMP(op);
1612 ST_FUNC void gen_opl(int op)
1614 arm64_gen_opil(op, 1);
1615 arm64_vset_VT_CMP(op);
1618 ST_FUNC void gen_opf(int op)
1620 uint32_t x, a, b, dbl;
1622 if (vtop[0].type.t == VT_LDOUBLE) {
1623 CType type = vtop[0].type;
1624 int func = 0;
1625 int cond = -1;
1626 switch (op) {
1627 case '*': func = TOK___multf3; break;
1628 case '+': func = TOK___addtf3; break;
1629 case '-': func = TOK___subtf3; break;
1630 case '/': func = TOK___divtf3; break;
1631 case TOK_EQ: func = TOK___eqtf2; cond = 1; break;
1632 case TOK_NE: func = TOK___netf2; cond = 0; break;
1633 case TOK_LT: func = TOK___lttf2; cond = 10; break;
1634 case TOK_GE: func = TOK___getf2; cond = 11; break;
1635 case TOK_LE: func = TOK___letf2; cond = 12; break;
1636 case TOK_GT: func = TOK___gttf2; cond = 13; break;
1637 default: assert(0); break;
1639 vpush_global_sym(&func_old_type, func);
1640 vrott(3);
1641 gfunc_call(2);
1642 vpushi(0);
1643 vtop->r = cond < 0 ? REG_FRET : REG_IRET;
1644 if (cond < 0)
1645 vtop->type = type;
1646 else {
1647 o(0x7100001f); // cmp w0,#0
1648 o(0x1a9f07e0 | (uint32_t)cond << 12); // cset w0,(cond)
1650 return;
1653 dbl = vtop[0].type.t != VT_FLOAT;
1654 gv2(RC_FLOAT, RC_FLOAT);
1655 assert(vtop[-1].r < VT_CONST && vtop[0].r < VT_CONST);
1656 a = fltr(vtop[-1].r);
1657 b = fltr(vtop[0].r);
1658 vtop -= 2;
1659 switch (op) {
1660 case TOK_EQ: case TOK_NE:
1661 case TOK_LT: case TOK_GE: case TOK_LE: case TOK_GT:
1662 x = get_reg(RC_INT);
1663 ++vtop;
1664 vtop[0].r = x;
1665 x = intr(x);
1666 break;
1667 default:
1668 x = get_reg(RC_FLOAT);
1669 ++vtop;
1670 vtop[0].r = x;
1671 x = fltr(x);
1672 break;
1675 switch (op) {
1676 case '*':
1677 o(0x1e200800 | dbl << 22 | x | a << 5 | b << 16); // fmul
1678 break;
1679 case '+':
1680 o(0x1e202800 | dbl << 22 | x | a << 5 | b << 16); // fadd
1681 break;
1682 case '-':
1683 o(0x1e203800 | dbl << 22 | x | a << 5 | b << 16); // fsub
1684 break;
1685 case '/':
1686 o(0x1e201800 | dbl << 22 | x | a << 5 | b << 16); // fdiv
1687 break;
1688 case TOK_EQ:
1689 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1690 o(0x1a9f17e0 | x); // cset w(x),eq
1691 break;
1692 case TOK_GE:
1693 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1694 o(0x1a9fb7e0 | x); // cset w(x),ge
1695 break;
1696 case TOK_GT:
1697 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1698 o(0x1a9fd7e0 | x); // cset w(x),gt
1699 break;
1700 case TOK_LE:
1701 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1702 o(0x1a9f87e0 | x); // cset w(x),ls
1703 break;
1704 case TOK_LT:
1705 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1706 o(0x1a9f57e0 | x); // cset w(x),mi
1707 break;
1708 case TOK_NE:
1709 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1710 o(0x1a9f07e0 | x); // cset w(x),ne
1711 break;
1712 default:
1713 assert(0);
1715 arm64_vset_VT_CMP(op);
1718 // Generate sign extension from 32 to 64 bits:
1719 ST_FUNC void gen_cvt_sxtw(void)
1721 uint32_t r = intr(gv(RC_INT));
1722 o(0x93407c00 | r | r << 5); // sxtw x(r),w(r)
1725 /* char/short to int conversion */
1726 ST_FUNC void gen_cvt_csti(int t)
1728 int r = intr(gv(RC_INT));
1729 o(0x13001c00
1730 | ((t & VT_BTYPE) == VT_SHORT) << 13
1731 | (uint32_t)!!(t & VT_UNSIGNED) << 30
1732 | r | r << 5); // [su]xt[bh] w(r),w(r)
1735 ST_FUNC void gen_cvt_itof(int t)
1737 if (t == VT_LDOUBLE) {
1738 int f = vtop->type.t;
1739 int func = (f & VT_BTYPE) == VT_LLONG ?
1740 (f & VT_UNSIGNED ? TOK___floatunditf : TOK___floatditf) :
1741 (f & VT_UNSIGNED ? TOK___floatunsitf : TOK___floatsitf);
1742 vpush_global_sym(&func_old_type, func);
1743 vrott(2);
1744 gfunc_call(1);
1745 vpushi(0);
1746 vtop->type.t = t;
1747 vtop->r = REG_FRET;
1748 return;
1750 else {
1751 int d, n = intr(gv(RC_INT));
1752 int s = !(vtop->type.t & VT_UNSIGNED);
1753 uint32_t l = ((vtop->type.t & VT_BTYPE) == VT_LLONG);
1754 --vtop;
1755 d = get_reg(RC_FLOAT);
1756 ++vtop;
1757 vtop[0].r = d;
1758 o(0x1e220000 | (uint32_t)!s << 16 |
1759 (uint32_t)(t != VT_FLOAT) << 22 | fltr(d) |
1760 l << 31 | n << 5); // [us]cvtf [sd](d),[wx](n)
1764 ST_FUNC void gen_cvt_ftoi(int t)
1766 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
1767 int func = (t & VT_BTYPE) == VT_LLONG ?
1768 (t & VT_UNSIGNED ? TOK___fixunstfdi : TOK___fixtfdi) :
1769 (t & VT_UNSIGNED ? TOK___fixunstfsi : TOK___fixtfsi);
1770 vpush_global_sym(&func_old_type, func);
1771 vrott(2);
1772 gfunc_call(1);
1773 vpushi(0);
1774 vtop->type.t = t;
1775 vtop->r = REG_IRET;
1776 return;
1778 else {
1779 int d, n = fltr(gv(RC_FLOAT));
1780 uint32_t l = ((vtop->type.t & VT_BTYPE) != VT_FLOAT);
1781 --vtop;
1782 d = get_reg(RC_INT);
1783 ++vtop;
1784 vtop[0].r = d;
1785 o(0x1e380000 |
1786 (uint32_t)!!(t & VT_UNSIGNED) << 16 |
1787 (uint32_t)((t & VT_BTYPE) == VT_LLONG) << 31 | intr(d) |
1788 l << 22 | n << 5); // fcvtz[su] [wx](d),[sd](n)
1792 ST_FUNC void gen_cvt_ftof(int t)
1794 int f = vtop[0].type.t & VT_BTYPE;
1795 assert(t == VT_FLOAT || t == VT_DOUBLE || t == VT_LDOUBLE);
1796 assert(f == VT_FLOAT || f == VT_DOUBLE || f == VT_LDOUBLE);
1797 if (t == f)
1798 return;
1800 if (t == VT_LDOUBLE || f == VT_LDOUBLE) {
1801 int func = (t == VT_LDOUBLE) ?
1802 (f == VT_FLOAT ? TOK___extendsftf2 : TOK___extenddftf2) :
1803 (t == VT_FLOAT ? TOK___trunctfsf2 : TOK___trunctfdf2);
1804 vpush_global_sym(&func_old_type, func);
1805 vrott(2);
1806 gfunc_call(1);
1807 vpushi(0);
1808 vtop->type.t = t;
1809 vtop->r = REG_FRET;
1811 else {
1812 int x, a;
1813 gv(RC_FLOAT);
1814 assert(vtop[0].r < VT_CONST);
1815 a = fltr(vtop[0].r);
1816 --vtop;
1817 x = get_reg(RC_FLOAT);
1818 ++vtop;
1819 vtop[0].r = x;
1820 x = fltr(x);
1822 if (f == VT_FLOAT)
1823 o(0x1e22c000 | x | a << 5); // fcvt d(x),s(a)
1824 else
1825 o(0x1e624000 | x | a << 5); // fcvt s(x),d(a)
1829 ST_FUNC void ggoto(void)
1831 arm64_gen_bl_or_b(1);
1832 --vtop;
1835 ST_FUNC void gen_clear_cache(void)
1837 uint32_t beg, end, dsz, isz, p, lab1, b1;
1838 gv2(RC_INT, RC_INT);
1839 vpushi(0);
1840 vtop->r = get_reg(RC_INT);
1841 vpushi(0);
1842 vtop->r = get_reg(RC_INT);
1843 vpushi(0);
1844 vtop->r = get_reg(RC_INT);
1845 beg = intr(vtop[-4].r); // x0
1846 end = intr(vtop[-3].r); // x1
1847 dsz = intr(vtop[-2].r); // x2
1848 isz = intr(vtop[-1].r); // x3
1849 p = intr(vtop[0].r); // x4
1850 vtop -= 5;
1852 o(0xd53b0020 | isz); // mrs x(isz),ctr_el0
1853 o(0x52800080 | p); // mov w(p),#4
1854 o(0x53104c00 | dsz | isz << 5); // ubfx w(dsz),w(isz),#16,#4
1855 o(0x1ac02000 | dsz | p << 5 | dsz << 16); // lsl w(dsz),w(p),w(dsz)
1856 o(0x12000c00 | isz | isz << 5); // and w(isz),w(isz),#15
1857 o(0x1ac02000 | isz | p << 5 | isz << 16); // lsl w(isz),w(p),w(isz)
1858 o(0x51000400 | p | dsz << 5); // sub w(p),w(dsz),#1
1859 o(0x8a240004 | p | beg << 5 | p << 16); // bic x(p),x(beg),x(p)
1860 b1 = ind; o(0x14000000); // b
1861 lab1 = ind;
1862 o(0xd50b7b20 | p); // dc cvau,x(p)
1863 o(0x8b000000 | p | p << 5 | dsz << 16); // add x(p),x(p),x(dsz)
1864 write32le(cur_text_section->data + b1, 0x14000000 | (ind - b1) >> 2);
1865 o(0xeb00001f | p << 5 | end << 16); // cmp x(p),x(end)
1866 o(0x54ffffa3 | ((lab1 - ind) << 3 & 0xffffe0)); // b.cc lab1
1867 o(0xd5033b9f); // dsb ish
1868 o(0x51000400 | p | isz << 5); // sub w(p),w(isz),#1
1869 o(0x8a240004 | p | beg << 5 | p << 16); // bic x(p),x(beg),x(p)
1870 b1 = ind; o(0x14000000); // b
1871 lab1 = ind;
1872 o(0xd50b7520 | p); // ic ivau,x(p)
1873 o(0x8b000000 | p | p << 5 | isz << 16); // add x(p),x(p),x(isz)
1874 write32le(cur_text_section->data + b1, 0x14000000 | (ind - b1) >> 2);
1875 o(0xeb00001f | p << 5 | end << 16); // cmp x(p),x(end)
1876 o(0x54ffffa3 | ((lab1 - ind) << 3 & 0xffffe0)); // b.cc lab1
1877 o(0xd5033b9f); // dsb ish
1878 o(0xd5033fdf); // isb
1881 ST_FUNC void gen_vla_sp_save(int addr) {
1882 uint32_t r = intr(get_reg(RC_INT));
1883 o(0x910003e0 | r); // mov x(r),sp
1884 arm64_strx(3, r, 29, addr);
1887 ST_FUNC void gen_vla_sp_restore(int addr) {
1888 // Use x30 because this function can be called when there
1889 // is a live return value in x0 but there is nothing on
1890 // the value stack to prevent get_reg from returning x0.
1891 uint32_t r = 30;
1892 arm64_ldrx(0, 3, r, 29, addr);
1893 o(0x9100001f | r << 5); // mov sp,x(r)
1896 ST_FUNC void gen_vla_alloc(CType *type, int align) {
1897 uint32_t r = intr(gv(RC_INT));
1898 o(0x91003c00 | r | r << 5); // add x(r),x(r),#15
1899 o(0x927cec00 | r | r << 5); // bic x(r),x(r),#15
1900 o(0xcb2063ff | r << 16); // sub sp,sp,x(r)
1901 vpop();
1904 /* end of A64 code generator */
1905 /*************************************************************/
1906 #endif
1907 /*************************************************************/