Merge branch 'mob' of git://repo.or.cz/tinycc into mypatch
[tinycc.git] / arm64-gen.c
blob35538d1a78dae361e7bd81da45c04472079a8a27
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)
245 switch (t & VT_BTYPE) {
246 case VT_INT: return 2;
247 case VT_BYTE: return 0;
248 case VT_SHORT: return 1;
249 case VT_PTR: return 3;
250 case VT_FUNC: return 3;
251 case VT_FLOAT: return 2;
252 case VT_DOUBLE: return 3;
253 case VT_LDOUBLE: return 4;
254 case VT_BOOL: return 0;
255 case VT_LLONG: return 3;
257 assert(0);
258 return 0;
261 static void arm64_spoff(int reg, uint64_t off)
263 uint32_t sub = off >> 63;
264 if (sub)
265 off = -off;
266 if (off < 4096)
267 o(0x910003e0 | sub << 30 | reg | off << 10);
268 // (add|sub) x(reg),sp,#(off)
269 else {
270 arm64_movimm(30, off); // use x30 for offset
271 o(0x8b3e63e0 | sub << 30 | reg); // (add|sub) x(reg),sp,x30
275 static void arm64_ldrx(int sg, int sz_, int dst, int bas, uint64_t off)
277 uint32_t sz = sz_;
278 if (sz >= 2)
279 sg = 0;
280 if (!(off & ~((uint32_t)0xfff << sz)))
281 o(0x39400000 | dst | bas << 5 | off << (10 - sz) |
282 (uint32_t)!!sg << 23 | sz << 30); // ldr(*) x(dst),[x(bas),#(off)]
283 else if (off < 256 || -off <= 256)
284 o(0x38400000 | dst | bas << 5 | (off & 511) << 12 |
285 (uint32_t)!!sg << 23 | sz << 30); // ldur(*) x(dst),[x(bas),#(off)]
286 else {
287 arm64_movimm(30, off); // use x30 for offset
288 o(0x38206800 | dst | bas << 5 | (uint32_t)30 << 16 |
289 (uint32_t)(!!sg + 1) << 22 | sz << 30); // ldr(*) x(dst),[x(bas),x30]
293 static void arm64_ldrv(int sz_, int dst, int bas, uint64_t off)
295 uint32_t sz = sz_;
296 if (!(off & ~((uint32_t)0xfff << sz)))
297 o(0x3d400000 | dst | bas << 5 | off << (10 - sz) |
298 (sz & 4) << 21 | (sz & 3) << 30); // ldr (s|d|q)(dst),[x(bas),#(off)]
299 else if (off < 256 || -off <= 256)
300 o(0x3c400000 | dst | bas << 5 | (off & 511) << 12 |
301 (sz & 4) << 21 | (sz & 3) << 30); // ldur (s|d|q)(dst),[x(bas),#(off)]
302 else {
303 arm64_movimm(30, off); // use x30 for offset
304 o(0x3c606800 | dst | bas << 5 | (uint32_t)30 << 16 |
305 sz << 30 | (sz & 4) << 21); // ldr (s|d|q)(dst),[x(bas),x30]
309 static void arm64_ldrs(int reg_, int size)
311 uint32_t reg = reg_;
312 // Use x30 for intermediate value in some cases.
313 switch (size) {
314 default: assert(0); break;
315 case 1:
316 arm64_ldrx(0, 0, reg, reg, 0);
317 break;
318 case 2:
319 arm64_ldrx(0, 1, reg, reg, 0);
320 break;
321 case 3:
322 arm64_ldrx(0, 1, 30, reg, 0);
323 arm64_ldrx(0, 0, reg, reg, 2);
324 o(0x2a0043c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #16
325 break;
326 case 4:
327 arm64_ldrx(0, 2, reg, reg, 0);
328 break;
329 case 5:
330 arm64_ldrx(0, 2, 30, reg, 0);
331 arm64_ldrx(0, 0, reg, reg, 4);
332 o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32
333 break;
334 case 6:
335 arm64_ldrx(0, 2, 30, reg, 0);
336 arm64_ldrx(0, 1, reg, reg, 4);
337 o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32
338 break;
339 case 7:
340 arm64_ldrx(0, 2, 30, reg, 0);
341 arm64_ldrx(0, 2, reg, reg, 3);
342 o(0x53087c00 | reg | reg << 5); // lsr w(reg), w(reg), #8
343 o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32
344 break;
345 case 8:
346 arm64_ldrx(0, 3, reg, reg, 0);
347 break;
348 case 9:
349 arm64_ldrx(0, 0, reg + 1, reg, 8);
350 arm64_ldrx(0, 3, reg, reg, 0);
351 break;
352 case 10:
353 arm64_ldrx(0, 1, reg + 1, reg, 8);
354 arm64_ldrx(0, 3, reg, reg, 0);
355 break;
356 case 11:
357 arm64_ldrx(0, 2, reg + 1, reg, 7);
358 o(0x53087c00 | (reg+1) | (reg+1) << 5); // lsr w(reg+1), w(reg+1), #8
359 arm64_ldrx(0, 3, reg, reg, 0);
360 break;
361 case 12:
362 arm64_ldrx(0, 2, reg + 1, reg, 8);
363 arm64_ldrx(0, 3, reg, reg, 0);
364 break;
365 case 13:
366 arm64_ldrx(0, 3, reg + 1, reg, 5);
367 o(0xd358fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #24
368 arm64_ldrx(0, 3, reg, reg, 0);
369 break;
370 case 14:
371 arm64_ldrx(0, 3, reg + 1, reg, 6);
372 o(0xd350fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #16
373 arm64_ldrx(0, 3, reg, reg, 0);
374 break;
375 case 15:
376 arm64_ldrx(0, 3, reg + 1, reg, 7);
377 o(0xd348fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #8
378 arm64_ldrx(0, 3, reg, reg, 0);
379 break;
380 case 16:
381 o(0xa9400000 | reg | (reg+1) << 10 | reg << 5);
382 // ldp x(reg),x(reg+1),[x(reg)]
383 break;
387 static void arm64_strx(int sz_, int dst, int bas, uint64_t off)
389 uint32_t sz = sz_;
390 if (!(off & ~((uint32_t)0xfff << sz)))
391 o(0x39000000 | dst | bas << 5 | off << (10 - sz) | sz << 30);
392 // str(*) x(dst),[x(bas],#(off)]
393 else if (off < 256 || -off <= 256)
394 o(0x38000000 | dst | bas << 5 | (off & 511) << 12 | sz << 30);
395 // stur(*) x(dst),[x(bas],#(off)]
396 else {
397 arm64_movimm(30, off); // use x30 for offset
398 o(0x38206800 | dst | bas << 5 | (uint32_t)30 << 16 | sz << 30);
399 // str(*) x(dst),[x(bas),x30]
403 static void arm64_strv(int sz_, int dst, int bas, uint64_t off)
405 uint32_t sz = sz_;
406 if (!(off & ~((uint32_t)0xfff << sz)))
407 o(0x3d000000 | dst | bas << 5 | off << (10 - sz) |
408 (sz & 4) << 21 | (sz & 3) << 30); // str (s|d|q)(dst),[x(bas),#(off)]
409 else if (off < 256 || -off <= 256)
410 o(0x3c000000 | dst | bas << 5 | (off & 511) << 12 |
411 (sz & 4) << 21 | (sz & 3) << 30); // stur (s|d|q)(dst),[x(bas),#(off)]
412 else {
413 arm64_movimm(30, off); // use x30 for offset
414 o(0x3c206800 | dst | bas << 5 | (uint32_t)30 << 16 |
415 sz << 30 | (sz & 4) << 21); // str (s|d|q)(dst),[x(bas),x30]
419 static void arm64_sym(int r, Sym *sym, unsigned long addend)
421 // Currently TCC's linker does not generate COPY relocations for
422 // STT_OBJECTs when tcc is invoked with "-run". This typically
423 // results in "R_AARCH64_ADR_PREL_PG_HI21 relocation failed" when
424 // a program refers to stdin. A workaround is to avoid that
425 // relocation and use only relocations with unlimited range.
426 int avoid_adrp = 1;
428 if (avoid_adrp || sym->a.weak) {
429 // (GCC uses a R_AARCH64_ABS64 in this case.)
430 greloca(cur_text_section, sym, ind, R_AARCH64_MOVW_UABS_G0_NC, addend);
431 o(0xd2800000 | r); // mov x(rt),#0,lsl #0
432 greloca(cur_text_section, sym, ind, R_AARCH64_MOVW_UABS_G1_NC, addend);
433 o(0xf2a00000 | r); // movk x(rt),#0,lsl #16
434 greloca(cur_text_section, sym, ind, R_AARCH64_MOVW_UABS_G2_NC, addend);
435 o(0xf2c00000 | r); // movk x(rt),#0,lsl #32
436 greloca(cur_text_section, sym, ind, R_AARCH64_MOVW_UABS_G3, addend);
437 o(0xf2e00000 | r); // movk x(rt),#0,lsl #48
439 else {
440 greloca(cur_text_section, sym, ind, R_AARCH64_ADR_PREL_PG_HI21, addend);
441 o(0x90000000 | r);
442 greloca(cur_text_section, sym, ind, R_AARCH64_ADD_ABS_LO12_NC, addend);
443 o(0x91000000 | r | r << 5);
447 static void arm64_load_cmp(int r, SValue *sv);
449 ST_FUNC void load(int r, SValue *sv)
451 int svtt = sv->type.t;
452 int svr = sv->r;
453 int svrv = svr & VT_VALMASK;
454 uint64_t svcul = (uint32_t)sv->c.i;
455 svcul = svcul >> 31 & 1 ? svcul - ((uint64_t)1 << 32) : svcul;
457 if (svr == (VT_LOCAL | VT_LVAL)) {
458 if (IS_FREG(r))
459 arm64_ldrv(arm64_type_size(svtt), fltr(r), 29, svcul);
460 else
461 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
462 intr(r), 29, svcul);
463 return;
466 if ((svr & ~VT_VALMASK) == VT_LVAL && svrv < VT_CONST) {
467 if (IS_FREG(r))
468 arm64_ldrv(arm64_type_size(svtt), fltr(r), intr(svrv), 0);
469 else
470 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
471 intr(r), intr(svrv), 0);
472 return;
475 if (svr == (VT_CONST | VT_LVAL | VT_SYM)) {
476 arm64_sym(30, sv->sym, svcul); // use x30 for address
477 if (IS_FREG(r))
478 arm64_ldrv(arm64_type_size(svtt), fltr(r), 30, 0);
479 else
480 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
481 intr(r), 30, 0);
482 return;
485 if (svr == (VT_CONST | VT_SYM)) {
486 arm64_sym(intr(r), sv->sym, svcul);
487 return;
490 if (svr == VT_CONST) {
491 if ((svtt & VT_BTYPE) != VT_VOID)
492 arm64_movimm(intr(r), arm64_type_size(svtt) == 3 ?
493 sv->c.i : (uint32_t)svcul);
494 return;
497 if (svr < VT_CONST) {
498 if (IS_FREG(r) && IS_FREG(svr))
499 if (svtt == VT_LDOUBLE)
500 o(0x4ea01c00 | fltr(r) | fltr(svr) << 5);
501 // mov v(r).16b,v(svr).16b
502 else
503 o(0x1e604000 | fltr(r) | fltr(svr) << 5); // fmov d(r),d(svr)
504 else if (!IS_FREG(r) && !IS_FREG(svr))
505 o(0xaa0003e0 | intr(r) | intr(svr) << 16); // mov x(r),x(svr)
506 else
507 assert(0);
508 return;
511 if (svr == VT_LOCAL) {
512 if (-svcul < 0x1000)
513 o(0xd10003a0 | intr(r) | -svcul << 10); // sub x(r),x29,#...
514 else {
515 arm64_movimm(30, -svcul); // use x30 for offset
516 o(0xcb0003a0 | intr(r) | (uint32_t)30 << 16); // sub x(r),x29,x30
518 return;
521 if (svr == VT_JMP || svr == VT_JMPI) {
522 int t = (svr == VT_JMPI);
523 arm64_movimm(intr(r), t);
524 o(0x14000002); // b .+8
525 gsym(svcul);
526 arm64_movimm(intr(r), t ^ 1);
527 return;
530 if (svr == (VT_LLOCAL | VT_LVAL)) {
531 arm64_ldrx(0, 3, 30, 29, svcul); // use x30 for offset
532 if (IS_FREG(r))
533 arm64_ldrv(arm64_type_size(svtt), fltr(r), 30, 0);
534 else
535 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
536 intr(r), 30, 0);
537 return;
540 if (svr == VT_CMP) {
541 arm64_load_cmp(r, sv);
542 return;
545 printf("load(%x, (%x, %x, %llx))\n", r, svtt, sv->r, (long long)svcul);
546 assert(0);
549 ST_FUNC void store(int r, SValue *sv)
551 int svtt = sv->type.t;
552 int svr = sv->r;
553 int svrv = svr & VT_VALMASK;
554 uint64_t svcul = (uint32_t)sv->c.i;
555 svcul = svcul >> 31 & 1 ? svcul - ((uint64_t)1 << 32) : svcul;
557 if (svr == (VT_LOCAL | VT_LVAL)) {
558 if (IS_FREG(r))
559 arm64_strv(arm64_type_size(svtt), fltr(r), 29, svcul);
560 else
561 arm64_strx(arm64_type_size(svtt), intr(r), 29, svcul);
562 return;
565 if ((svr & ~VT_VALMASK) == VT_LVAL && svrv < VT_CONST) {
566 if (IS_FREG(r))
567 arm64_strv(arm64_type_size(svtt), fltr(r), intr(svrv), 0);
568 else
569 arm64_strx(arm64_type_size(svtt), intr(r), intr(svrv), 0);
570 return;
573 if (svr == (VT_CONST | VT_LVAL | VT_SYM)) {
574 arm64_sym(30, sv->sym, svcul); // use x30 for address
575 if (IS_FREG(r))
576 arm64_strv(arm64_type_size(svtt), fltr(r), 30, 0);
577 else
578 arm64_strx(arm64_type_size(svtt), intr(r), 30, 0);
579 return;
582 printf("store(%x, (%x, %x, %llx))\n", r, svtt, sv->r, (long long)svcul);
583 assert(0);
586 static void arm64_gen_bl_or_b(int b)
588 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST && (vtop->r & VT_SYM)) {
589 assert(!b);
590 greloca(cur_text_section, vtop->sym, ind, R_AARCH64_CALL26, 0);
591 o(0x94000000); // bl .
593 else
594 o(0xd61f0000 | (uint32_t)!b << 21 | intr(gv(RC_R30)) << 5); // br/blr
597 static int arm64_hfa_aux(CType *type, int *fsize, int num)
599 if (is_float(type->t)) {
600 int a, n = type_size(type, &a);
601 if (num >= 4 || (*fsize && *fsize != n))
602 return -1;
603 *fsize = n;
604 return num + 1;
606 else if ((type->t & VT_BTYPE) == VT_STRUCT) {
607 int is_struct = 0; // rather than union
608 Sym *field;
609 for (field = type->ref->next; field; field = field->next)
610 if (field->c) {
611 is_struct = 1;
612 break;
614 if (is_struct) {
615 int num0 = num;
616 for (field = type->ref->next; field; field = field->next) {
617 if (field->c != (num - num0) * *fsize)
618 return -1;
619 num = arm64_hfa_aux(&field->type, fsize, num);
620 if (num == -1)
621 return -1;
623 if (type->ref->c != (num - num0) * *fsize)
624 return -1;
625 return num;
627 else { // union
628 int num0 = num;
629 for (field = type->ref->next; field; field = field->next) {
630 int num1 = arm64_hfa_aux(&field->type, fsize, num0);
631 if (num1 == -1)
632 return -1;
633 num = num1 < num ? num : num1;
635 if (type->ref->c != (num - num0) * *fsize)
636 return -1;
637 return num;
640 else if (type->t & VT_ARRAY) {
641 int num1;
642 if (!type->ref->c)
643 return num;
644 num1 = arm64_hfa_aux(&type->ref->type, fsize, num);
645 if (num1 == -1 || (num1 != num && type->ref->c > 4))
646 return -1;
647 num1 = num + type->ref->c * (num1 - num);
648 if (num1 > 4)
649 return -1;
650 return num1;
652 return -1;
655 static int arm64_hfa(CType *type, int *fsize)
657 if ((type->t & VT_BTYPE) == VT_STRUCT || (type->t & VT_ARRAY)) {
658 int sz = 0;
659 int n = arm64_hfa_aux(type, &sz, 0);
660 if (0 < n && n <= 4) {
661 if (fsize)
662 *fsize = sz;
663 return n;
666 return 0;
669 static unsigned long arm64_pcs_aux(int n, CType **type, unsigned long *a)
671 int nx = 0; // next integer register
672 int nv = 0; // next vector register
673 unsigned long ns = 32; // next stack offset
674 int i;
676 for (i = 0; i < n; i++) {
677 int hfa = arm64_hfa(type[i], 0);
678 int size, align;
680 if ((type[i]->t & VT_ARRAY) ||
681 (type[i]->t & VT_BTYPE) == VT_FUNC)
682 size = align = 8;
683 else
684 size = type_size(type[i], &align);
686 if (hfa)
687 // B.2
689 else if (size > 16) {
690 // B.3: replace with pointer
691 if (nx < 8)
692 a[i] = nx++ << 1 | 1;
693 else {
694 ns = (ns + 7) & ~7;
695 a[i] = ns | 1;
696 ns += 8;
698 continue;
700 else if ((type[i]->t & VT_BTYPE) == VT_STRUCT)
701 // B.4
702 size = (size + 7) & ~7;
704 // C.1
705 if (is_float(type[i]->t) && nv < 8) {
706 a[i] = 16 + (nv++ << 1);
707 continue;
710 // C.2
711 if (hfa && nv + hfa <= 8) {
712 a[i] = 16 + (nv << 1);
713 nv += hfa;
714 continue;
717 // C.3
718 if (hfa) {
719 nv = 8;
720 size = (size + 7) & ~7;
723 // C.4
724 if (hfa || (type[i]->t & VT_BTYPE) == VT_LDOUBLE) {
725 ns = (ns + 7) & ~7;
726 ns = (ns + align - 1) & -align;
729 // C.5
730 if ((type[i]->t & VT_BTYPE) == VT_FLOAT)
731 size = 8;
733 // C.6
734 if (hfa || is_float(type[i]->t)) {
735 a[i] = ns;
736 ns += size;
737 continue;
740 // C.7
741 if ((type[i]->t & VT_BTYPE) != VT_STRUCT && size <= 8 && nx < 8) {
742 a[i] = nx++ << 1;
743 continue;
746 // C.8
747 if (align == 16)
748 nx = (nx + 1) & ~1;
750 // C.9
751 if ((type[i]->t & VT_BTYPE) != VT_STRUCT && size == 16 && nx < 7) {
752 a[i] = nx << 1;
753 nx += 2;
754 continue;
757 // C.10
758 if ((type[i]->t & VT_BTYPE) == VT_STRUCT && size <= (8 - nx) * 8) {
759 a[i] = nx << 1;
760 nx += (size + 7) >> 3;
761 continue;
764 // C.11
765 nx = 8;
767 // C.12
768 ns = (ns + 7) & ~7;
769 ns = (ns + align - 1) & -align;
771 // C.13
772 if ((type[i]->t & VT_BTYPE) == VT_STRUCT) {
773 a[i] = ns;
774 ns += size;
775 continue;
778 // C.14
779 if (size < 8)
780 size = 8;
782 // C.15
783 a[i] = ns;
784 ns += size;
787 return ns - 32;
790 static unsigned long arm64_pcs(int n, CType **type, unsigned long *a)
792 unsigned long stack;
794 // Return type:
795 if ((type[0]->t & VT_BTYPE) == VT_VOID)
796 a[0] = -1;
797 else {
798 arm64_pcs_aux(1, type, a);
799 assert(a[0] == 0 || a[0] == 1 || a[0] == 16);
802 // Argument types:
803 stack = arm64_pcs_aux(n, type + 1, a + 1);
805 if (0) {
806 int i;
807 for (i = 0; i <= n; i++) {
808 if (!i)
809 printf("arm64_pcs return: ");
810 else
811 printf("arm64_pcs arg %d: ", i);
812 if (a[i] == (unsigned long)-1)
813 printf("void\n");
814 else if (a[i] == 1 && !i)
815 printf("X8 pointer\n");
816 else if (a[i] < 16)
817 printf("X%lu%s\n", a[i] / 2, a[i] & 1 ? " pointer" : "");
818 else if (a[i] < 32)
819 printf("V%lu\n", a[i] / 2 - 8);
820 else
821 printf("stack %lu%s\n",
822 (a[i] - 32) & ~1, a[i] & 1 ? " pointer" : "");
826 return stack;
829 ST_FUNC void gfunc_call(int nb_args)
831 CType *return_type;
832 CType **t;
833 unsigned long *a, *a1;
834 unsigned long stack;
835 int i;
837 return_type = &vtop[-nb_args].type.ref->type;
838 if ((return_type->t & VT_BTYPE) == VT_STRUCT)
839 --nb_args;
841 t = tcc_malloc((nb_args + 1) * sizeof(*t));
842 a = tcc_malloc((nb_args + 1) * sizeof(*a));
843 a1 = tcc_malloc((nb_args + 1) * sizeof(*a1));
845 t[0] = return_type;
846 for (i = 0; i < nb_args; i++)
847 t[nb_args - i] = &vtop[-i].type;
849 stack = arm64_pcs(nb_args, t, a);
851 // Allocate space for structs replaced by pointer:
852 for (i = nb_args; i; i--)
853 if (a[i] & 1) {
854 SValue *arg = &vtop[i - nb_args];
855 int align, size = type_size(&arg->type, &align);
856 assert((arg->type.t & VT_BTYPE) == VT_STRUCT);
857 stack = (stack + align - 1) & -align;
858 a1[i] = stack;
859 stack += size;
862 stack = (stack + 15) >> 4 << 4;
864 assert(stack < 0x1000);
865 if (stack)
866 o(0xd10003ff | stack << 10); // sub sp,sp,#(n)
868 // First pass: set all values on stack
869 for (i = nb_args; i; i--) {
870 vpushv(vtop - nb_args + i);
872 if (a[i] & 1) {
873 // struct replaced by pointer
874 int r = get_reg(RC_INT);
875 arm64_spoff(intr(r), a1[i]);
876 vset(&vtop->type, r | VT_LVAL, 0);
877 vswap();
878 vstore();
879 if (a[i] >= 32) {
880 // pointer on stack
881 r = get_reg(RC_INT);
882 arm64_spoff(intr(r), a1[i]);
883 arm64_strx(3, intr(r), 31, (a[i] - 32) >> 1 << 1);
886 else if (a[i] >= 32) {
887 // value on stack
888 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
889 int r = get_reg(RC_INT);
890 arm64_spoff(intr(r), a[i] - 32);
891 vset(&vtop->type, r | VT_LVAL, 0);
892 vswap();
893 vstore();
895 else if (is_float(vtop->type.t)) {
896 gv(RC_FLOAT);
897 arm64_strv(arm64_type_size(vtop[0].type.t),
898 fltr(vtop[0].r), 31, a[i] - 32);
900 else {
901 gv(RC_INT);
902 arm64_strx(arm64_type_size(vtop[0].type.t),
903 intr(vtop[0].r), 31, a[i] - 32);
907 --vtop;
910 // Second pass: assign values to registers
911 for (i = nb_args; i; i--, vtop--) {
912 if (a[i] < 16 && !(a[i] & 1)) {
913 // value in general-purpose registers
914 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
915 int align, size = type_size(&vtop->type, &align);
916 vtop->type.t = VT_PTR;
917 gaddrof();
918 gv(RC_R(a[i] / 2));
919 arm64_ldrs(a[i] / 2, size);
921 else
922 gv(RC_R(a[i] / 2));
924 else if (a[i] < 16)
925 // struct replaced by pointer in register
926 arm64_spoff(a[i] / 2, a1[i]);
927 else if (a[i] < 32) {
928 // value in floating-point registers
929 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
930 uint32_t j, sz, n = arm64_hfa(&vtop->type, &sz);
931 vtop->type.t = VT_PTR;
932 gaddrof();
933 gv(RC_R30);
934 for (j = 0; j < n; j++)
935 o(0x3d4003c0 |
936 (sz & 16) << 19 | -(sz & 8) << 27 | (sz & 4) << 29 |
937 (a[i] / 2 - 8 + j) |
938 j << 10); // ldr ([sdq])(*),[x30,#(j * sz)]
940 else
941 gv(RC_F(a[i] / 2 - 8));
945 if ((return_type->t & VT_BTYPE) == VT_STRUCT) {
946 if (a[0] == 1) {
947 // indirect return: set x8 and discard the stack value
948 gv(RC_R(8));
949 --vtop;
951 else
952 // return in registers: keep the address for after the call
953 vswap();
956 save_regs(0);
957 arm64_gen_bl_or_b(0);
958 --vtop;
959 if (stack)
960 o(0x910003ff | stack << 10); // add sp,sp,#(n)
963 int rt = return_type->t;
964 int bt = rt & VT_BTYPE;
965 if (bt == VT_STRUCT && !(a[0] & 1)) {
966 // A struct was returned in registers, so write it out:
967 gv(RC_R(8));
968 --vtop;
969 if (a[0] == 0) {
970 int align, size = type_size(return_type, &align);
971 assert(size <= 16);
972 if (size > 8)
973 o(0xa9000500); // stp x0,x1,[x8]
974 else if (size)
975 arm64_strx(size > 4 ? 3 : size > 2 ? 2 : size > 1, 0, 8, 0);
978 else if (a[0] == 16) {
979 uint32_t j, sz, n = arm64_hfa(return_type, &sz);
980 for (j = 0; j < n; j++)
981 o(0x3d000100 |
982 (sz & 16) << 19 | -(sz & 8) << 27 | (sz & 4) << 29 |
983 (a[i] / 2 - 8 + j) |
984 j << 10); // str ([sdq])(*),[x8,#(j * sz)]
989 tcc_free(a1);
990 tcc_free(a);
991 tcc_free(t);
994 static unsigned long arm64_func_va_list_stack;
995 static int arm64_func_va_list_gr_offs;
996 static int arm64_func_va_list_vr_offs;
997 static int arm64_func_sub_sp_offset;
999 ST_FUNC void gfunc_prolog(Sym *func_sym)
1001 CType *func_type = &func_sym->type;
1002 int n = 0;
1003 int i = 0;
1004 Sym *sym;
1005 CType **t;
1006 unsigned long *a;
1008 // Why doesn't the caller (gen_function) set func_vt?
1009 func_vt = func_type->ref->type;
1010 func_vc = 144; // offset of where x8 is stored
1012 for (sym = func_type->ref; sym; sym = sym->next)
1013 ++n;
1014 t = tcc_malloc(n * sizeof(*t));
1015 a = tcc_malloc(n * sizeof(*a));
1017 for (sym = func_type->ref; sym; sym = sym->next)
1018 t[i++] = &sym->type;
1020 arm64_func_va_list_stack = arm64_pcs(n - 1, t, a);
1022 o(0xa9b27bfd); // stp x29,x30,[sp,#-224]!
1023 o(0xad0087e0); // stp q0,q1,[sp,#16]
1024 o(0xad018fe2); // stp q2,q3,[sp,#48]
1025 o(0xad0297e4); // stp q4,q5,[sp,#80]
1026 o(0xad039fe6); // stp q6,q7,[sp,#112]
1027 o(0xa90923e8); // stp x8,x8,[sp,#144]
1028 o(0xa90a07e0); // stp x0,x1,[sp,#160]
1029 o(0xa90b0fe2); // stp x2,x3,[sp,#176]
1030 o(0xa90c17e4); // stp x4,x5,[sp,#192]
1031 o(0xa90d1fe6); // stp x6,x7,[sp,#208]
1033 arm64_func_va_list_gr_offs = -64;
1034 arm64_func_va_list_vr_offs = -128;
1036 for (i = 1, sym = func_type->ref->next; sym; i++, sym = sym->next) {
1037 int off = (a[i] < 16 ? 160 + a[i] / 2 * 8 :
1038 a[i] < 32 ? 16 + (a[i] - 16) / 2 * 16 :
1039 224 + ((a[i] - 32) >> 1 << 1));
1040 sym_push(sym->v & ~SYM_FIELD, &sym->type,
1041 (a[i] & 1 ? VT_LLOCAL : VT_LOCAL) | VT_LVAL,
1042 off);
1044 if (a[i] < 16) {
1045 int align, size = type_size(&sym->type, &align);
1046 arm64_func_va_list_gr_offs = (a[i] / 2 - 7 +
1047 (!(a[i] & 1) && size > 8)) * 8;
1049 else if (a[i] < 32) {
1050 uint32_t hfa = arm64_hfa(&sym->type, 0);
1051 arm64_func_va_list_vr_offs = (a[i] / 2 - 16 +
1052 (hfa ? hfa : 1)) * 16;
1055 // HFAs of float and double need to be written differently:
1056 if (16 <= a[i] && a[i] < 32 && (sym->type.t & VT_BTYPE) == VT_STRUCT) {
1057 uint32_t j, sz, k = arm64_hfa(&sym->type, &sz);
1058 if (sz < 16)
1059 for (j = 0; j < k; j++) {
1060 o(0x3d0003e0 | -(sz & 8) << 27 | (sz & 4) << 29 |
1061 ((a[i] - 16) / 2 + j) | (off / sz + j) << 10);
1062 // str ([sdq])(*),[sp,#(j * sz)]
1067 tcc_free(a);
1068 tcc_free(t);
1070 o(0x910003fd); // mov x29,sp
1071 arm64_func_sub_sp_offset = ind;
1072 // In gfunc_epilog these will be replaced with code to decrement SP:
1073 o(0xd503201f); // nop
1074 o(0xd503201f); // nop
1075 loc = 0;
1078 ST_FUNC void gen_va_start(void)
1080 int r;
1081 --vtop; // we don't need the "arg"
1082 gaddrof();
1083 r = intr(gv(RC_INT));
1085 if (arm64_func_va_list_stack) {
1086 //xx could use add (immediate) here
1087 arm64_movimm(30, arm64_func_va_list_stack + 224);
1088 o(0x8b1e03be); // add x30,x29,x30
1090 else
1091 o(0x910383be); // add x30,x29,#224
1092 o(0xf900001e | r << 5); // str x30,[x(r)]
1094 if (arm64_func_va_list_gr_offs) {
1095 if (arm64_func_va_list_stack)
1096 o(0x910383be); // add x30,x29,#224
1097 o(0xf900041e | r << 5); // str x30,[x(r),#8]
1100 if (arm64_func_va_list_vr_offs) {
1101 o(0x910243be); // add x30,x29,#144
1102 o(0xf900081e | r << 5); // str x30,[x(r),#16]
1105 arm64_movimm(30, arm64_func_va_list_gr_offs);
1106 o(0xb900181e | r << 5); // str w30,[x(r),#24]
1108 arm64_movimm(30, arm64_func_va_list_vr_offs);
1109 o(0xb9001c1e | r << 5); // str w30,[x(r),#28]
1111 --vtop;
1114 ST_FUNC void gen_va_arg(CType *t)
1116 int align, size = type_size(t, &align);
1117 int fsize, hfa = arm64_hfa(t, &fsize);
1118 uint32_t r0, r1;
1120 if (is_float(t->t)) {
1121 hfa = 1;
1122 fsize = size;
1125 gaddrof();
1126 r0 = intr(gv(RC_INT));
1127 r1 = get_reg(RC_INT);
1128 vtop[0].r = r1 | VT_LVAL;
1129 r1 = intr(r1);
1131 if (!hfa) {
1132 uint32_t n = size > 16 ? 8 : (size + 7) & -8;
1133 o(0xb940181e | r0 << 5); // ldr w30,[x(r0),#24] // __gr_offs
1134 if (align == 16) {
1135 assert(0); // this path untested but needed for __uint128_t
1136 o(0x11003fde); // add w30,w30,#15
1137 o(0x121c6fde); // and w30,w30,#-16
1139 o(0x310003c0 | r1 | n << 10); // adds w(r1),w30,#(n)
1140 o(0x540000ad); // b.le .+20
1141 o(0xf9400000 | r1 | r0 << 5); // ldr x(r1),[x(r0)] // __stack
1142 o(0x9100001e | r1 << 5 | n << 10); // add x30,x(r1),#(n)
1143 o(0xf900001e | r0 << 5); // str x30,[x(r0)] // __stack
1144 o(0x14000004); // b .+16
1145 o(0xb9001800 | r1 | r0 << 5); // str w(r1),[x(r0),#24] // __gr_offs
1146 o(0xf9400400 | r1 | r0 << 5); // ldr x(r1),[x(r0),#8] // __gr_top
1147 o(0x8b3ec000 | r1 | r1 << 5); // add x(r1),x(r1),w30,sxtw
1148 if (size > 16)
1149 o(0xf9400000 | r1 | r1 << 5); // ldr x(r1),[x(r1)]
1151 else {
1152 uint32_t rsz = hfa << 4;
1153 uint32_t ssz = (size + 7) & -(uint32_t)8;
1154 uint32_t b1, b2;
1155 o(0xb9401c1e | r0 << 5); // ldr w30,[x(r0),#28] // __vr_offs
1156 o(0x310003c0 | r1 | rsz << 10); // adds w(r1),w30,#(rsz)
1157 b1 = ind; o(0x5400000d); // b.le lab1
1158 o(0xf9400000 | r1 | r0 << 5); // ldr x(r1),[x(r0)] // __stack
1159 if (fsize == 16) {
1160 o(0x91003c00 | r1 | r1 << 5); // add x(r1),x(r1),#15
1161 o(0x927cec00 | r1 | r1 << 5); // and x(r1),x(r1),#-16
1163 o(0x9100001e | r1 << 5 | ssz << 10); // add x30,x(r1),#(ssz)
1164 o(0xf900001e | r0 << 5); // str x30,[x(r0)] // __stack
1165 b2 = ind; o(0x14000000); // b lab2
1166 // lab1:
1167 write32le(cur_text_section->data + b1, 0x5400000d | (ind - b1) << 3);
1168 o(0xb9001c00 | r1 | r0 << 5); // str w(r1),[x(r0),#28] // __vr_offs
1169 o(0xf9400800 | r1 | r0 << 5); // ldr x(r1),[x(r0),#16] // __vr_top
1170 if (hfa == 1 || fsize == 16)
1171 o(0x8b3ec000 | r1 | r1 << 5); // add x(r1),x(r1),w30,sxtw
1172 else {
1173 // We need to change the layout of this HFA.
1174 // Get some space on the stack using global variable "loc":
1175 loc = (loc - size) & -(uint32_t)align;
1176 o(0x8b3ec000 | 30 | r1 << 5); // add x30,x(r1),w30,sxtw
1177 arm64_movimm(r1, loc);
1178 o(0x8b0003a0 | r1 | r1 << 16); // add x(r1),x29,x(r1)
1179 o(0x4c402bdc | (uint32_t)fsize << 7 |
1180 (uint32_t)(hfa == 2) << 15 |
1181 (uint32_t)(hfa == 3) << 14); // ld1 {v28.(4s|2d),...},[x30]
1182 o(0x0d00801c | r1 << 5 | (fsize == 8) << 10 |
1183 (uint32_t)(hfa != 2) << 13 |
1184 (uint32_t)(hfa != 3) << 21); // st(hfa) {v28.(s|d),...}[0],[x(r1)]
1186 // lab2:
1187 write32le(cur_text_section->data + b2, 0x14000000 | (ind - b2) >> 2);
1191 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret,
1192 int *align, int *regsize)
1194 return 0;
1197 ST_FUNC void gfunc_return(CType *func_type)
1199 CType *t = func_type;
1200 unsigned long a;
1202 arm64_pcs(0, &t, &a);
1203 switch (a) {
1204 case -1:
1205 break;
1206 case 0:
1207 if ((func_type->t & VT_BTYPE) == VT_STRUCT) {
1208 int align, size = type_size(func_type, &align);
1209 gaddrof();
1210 gv(RC_R(0));
1211 arm64_ldrs(0, size);
1213 else
1214 gv(RC_IRET);
1215 break;
1216 case 1: {
1217 CType type = *func_type;
1218 mk_pointer(&type);
1219 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
1220 indir();
1221 vswap();
1222 vstore();
1223 break;
1225 case 16:
1226 if ((func_type->t & VT_BTYPE) == VT_STRUCT) {
1227 uint32_t j, sz, n = arm64_hfa(&vtop->type, &sz);
1228 gaddrof();
1229 gv(RC_R(0));
1230 for (j = 0; j < n; j++)
1231 o(0x3d400000 |
1232 (sz & 16) << 19 | -(sz & 8) << 27 | (sz & 4) << 29 |
1233 j | j << 10); // ldr ([sdq])(*),[x0,#(j * sz)]
1235 else
1236 gv(RC_FRET);
1237 break;
1238 default:
1239 assert(0);
1241 vtop--;
1244 ST_FUNC void gfunc_epilog(void)
1246 if (loc) {
1247 // Insert instructions to subtract size of stack frame from SP.
1248 unsigned char *ptr = cur_text_section->data + arm64_func_sub_sp_offset;
1249 uint64_t diff = (-loc + 15) & ~15;
1250 if (!(diff >> 24)) {
1251 if (diff & 0xfff) // sub sp,sp,#(diff & 0xfff)
1252 write32le(ptr, 0xd10003ff | (diff & 0xfff) << 10);
1253 if (diff >> 12) // sub sp,sp,#(diff >> 12),lsl #12
1254 write32le(ptr + 4, 0xd14003ff | (diff >> 12) << 10);
1256 else {
1257 // In this case we may subtract more than necessary,
1258 // but always less than 17/16 of what we were aiming for.
1259 int i = 0;
1260 int j = 0;
1261 while (diff >> 20) {
1262 diff = (diff + 0xffff) >> 16;
1263 ++i;
1265 while (diff >> 16) {
1266 diff = (diff + 1) >> 1;
1267 ++j;
1269 write32le(ptr, 0xd2800010 | diff << 5 | i << 21);
1270 // mov x16,#(diff),lsl #(16 * i)
1271 write32le(ptr + 4, 0xcb3063ff | j << 10);
1272 // sub sp,sp,x16,lsl #(j)
1275 o(0x910003bf); // mov sp,x29
1276 o(0xa8ce7bfd); // ldp x29,x30,[sp],#224
1278 o(0xd65f03c0); // ret
1281 ST_FUNC void gen_fill_nops(int bytes)
1283 if ((bytes & 3))
1284 tcc_error("alignment of code section not multiple of 4");
1285 while (bytes > 0) {
1286 o(0xd503201f); // nop
1287 bytes -= 4;
1291 // Generate forward branch to label:
1292 ST_FUNC int gjmp(int t)
1294 int r = ind;
1295 if (nocode_wanted)
1296 return t;
1297 o(t);
1298 return r;
1301 // Generate branch to known address:
1302 ST_FUNC void gjmp_addr(int a)
1304 assert(a - ind + 0x8000000 < 0x10000000);
1305 o(0x14000000 | ((a - ind) >> 2 & 0x3ffffff));
1308 ST_FUNC int gjmp_append(int n, int t)
1310 void *p;
1311 /* insert vtop->c jump list in t */
1312 if (n) {
1313 uint32_t n1 = n, n2;
1314 while ((n2 = read32le(p = cur_text_section->data + n1)))
1315 n1 = n2;
1316 write32le(p, t);
1317 t = n;
1319 return t;
1322 void arm64_vset_VT_CMP(int op)
1324 if (op >= TOK_ULT && op <= TOK_GT) {
1325 vtop->cmp_r = vtop->r;
1326 vset_VT_CMP(0x80);
1330 static void arm64_gen_opil(int op, uint32_t l);
1332 static void arm64_load_cmp(int r, SValue *sv)
1334 sv->r = sv->cmp_r;
1335 if (sv->c.i & 1) {
1336 vpushi(1);
1337 arm64_gen_opil('^', 0);
1339 if (r != sv->r) {
1340 load(r, sv);
1341 sv->r = r;
1345 ST_FUNC int gjmp_cond(int op, int t)
1347 int bt = vtop->type.t & VT_BTYPE;
1349 int inv = op & 1;
1350 vtop->r = vtop->cmp_r;
1352 if (bt == VT_LDOUBLE) {
1353 uint32_t a, b, f = fltr(gv(RC_FLOAT));
1354 a = get_reg(RC_INT);
1355 vpushi(0);
1356 vtop[0].r = a;
1357 b = get_reg(RC_INT);
1358 a = intr(a);
1359 b = intr(b);
1360 o(0x4e083c00 | a | f << 5); // mov x(a),v(f).d[0]
1361 o(0x4e183c00 | b | f << 5); // mov x(b),v(f).d[1]
1362 o(0xaa000400 | a | a << 5 | b << 16); // orr x(a),x(a),x(b),lsl #1
1363 o(0xb4000040 | a | !!inv << 24); // cbz/cbnz x(a),.+8
1364 --vtop;
1366 else if (bt == VT_FLOAT || bt == VT_DOUBLE) {
1367 uint32_t a = fltr(gv(RC_FLOAT));
1368 o(0x1e202008 | a << 5 | (bt != VT_FLOAT) << 22); // fcmp
1369 o(0x54000040 | !!inv); // b.eq/b.ne .+8
1371 else {
1372 uint32_t ll = (bt == VT_PTR || bt == VT_LLONG);
1373 uint32_t a = intr(gv(RC_INT));
1374 o(0x34000040 | a | !!inv << 24 | ll << 31); // cbz/cbnz wA,.+8
1376 return gjmp(t);
1379 static int arm64_iconst(uint64_t *val, SValue *sv)
1381 if ((sv->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
1382 return 0;
1383 if (val) {
1384 int t = sv->type.t;
1385 int bt = t & VT_BTYPE;
1386 *val = ((bt == VT_LLONG || bt == VT_PTR) ? sv->c.i :
1387 (uint32_t)sv->c.i |
1388 (t & VT_UNSIGNED ? 0 : -(sv->c.i & 0x80000000)));
1390 return 1;
1393 static int arm64_gen_opic(int op, uint32_t l, int rev, uint64_t val,
1394 uint32_t x, uint32_t a)
1396 if (op == '-' && !rev) {
1397 val = -val;
1398 op = '+';
1400 val = l ? val : (uint32_t)val;
1402 switch (op) {
1404 case '+': {
1405 uint32_t s = l ? val >> 63 : val >> 31;
1406 val = s ? -val : val;
1407 val = l ? val : (uint32_t)val;
1408 if (!(val & ~(uint64_t)0xfff))
1409 o(0x11000000 | l << 31 | s << 30 | x | a << 5 | val << 10);
1410 else if (!(val & ~(uint64_t)0xfff000))
1411 o(0x11400000 | l << 31 | s << 30 | x | a << 5 | val >> 12 << 10);
1412 else {
1413 arm64_movimm(30, val); // use x30
1414 o(0x0b1e0000 | l << 31 | s << 30 | x | a << 5);
1416 return 1;
1419 case '-':
1420 if (!val)
1421 o(0x4b0003e0 | l << 31 | x | a << 16); // neg
1422 else if (val == (l ? (uint64_t)-1 : (uint32_t)-1))
1423 o(0x2a2003e0 | l << 31 | x | a << 16); // mvn
1424 else {
1425 arm64_movimm(30, val); // use x30
1426 o(0x4b0003c0 | l << 31 | x | a << 16); // sub
1428 return 1;
1430 case '^':
1431 if (val == -1 || (val == 0xffffffff && !l)) {
1432 o(0x2a2003e0 | l << 31 | x | a << 16); // mvn
1433 return 1;
1435 // fall through
1436 case '&':
1437 case '|': {
1438 int e = arm64_encode_bimm64(l ? val : val | val << 32);
1439 if (e < 0)
1440 return 0;
1441 o((op == '&' ? 0x12000000 :
1442 op == '|' ? 0x32000000 : 0x52000000) |
1443 l << 31 | x | a << 5 | (uint32_t)e << 10);
1444 return 1;
1447 case TOK_SAR:
1448 case TOK_SHL:
1449 case TOK_SHR: {
1450 uint32_t n = 32 << l;
1451 val = val & (n - 1);
1452 if (rev)
1453 return 0;
1454 if (!val)
1455 assert(0);
1456 else if (op == TOK_SHL)
1457 o(0x53000000 | l << 31 | l << 22 | x | a << 5 |
1458 (n - val) << 16 | (n - 1 - val) << 10); // lsl
1459 else
1460 o(0x13000000 | (op == TOK_SHR) << 30 | l << 31 | l << 22 |
1461 x | a << 5 | val << 16 | (n - 1) << 10); // lsr/asr
1462 return 1;
1466 return 0;
1469 static void arm64_gen_opil(int op, uint32_t l)
1471 uint32_t x, a, b;
1473 // Special treatment for operations with a constant operand:
1475 uint64_t val;
1476 int rev = 1;
1478 if (arm64_iconst(0, &vtop[0])) {
1479 vswap();
1480 rev = 0;
1482 if (arm64_iconst(&val, &vtop[-1])) {
1483 gv(RC_INT);
1484 a = intr(vtop[0].r);
1485 --vtop;
1486 x = get_reg(RC_INT);
1487 ++vtop;
1488 if (arm64_gen_opic(op, l, rev, val, intr(x), a)) {
1489 vtop[0].r = x;
1490 vswap();
1491 --vtop;
1492 return;
1495 if (!rev)
1496 vswap();
1499 gv2(RC_INT, RC_INT);
1500 assert(vtop[-1].r < VT_CONST && vtop[0].r < VT_CONST);
1501 a = intr(vtop[-1].r);
1502 b = intr(vtop[0].r);
1503 vtop -= 2;
1504 x = get_reg(RC_INT);
1505 ++vtop;
1506 vtop[0].r = x;
1507 x = intr(x);
1509 switch (op) {
1510 case '%':
1511 // Use x30 for quotient:
1512 o(0x1ac00c00 | l << 31 | 30 | a << 5 | b << 16); // sdiv
1513 o(0x1b008000 | l << 31 | x | (uint32_t)30 << 5 |
1514 b << 16 | a << 10); // msub
1515 break;
1516 case '&':
1517 o(0x0a000000 | l << 31 | x | a << 5 | b << 16); // and
1518 break;
1519 case '*':
1520 o(0x1b007c00 | l << 31 | x | a << 5 | b << 16); // mul
1521 break;
1522 case '+':
1523 o(0x0b000000 | l << 31 | x | a << 5 | b << 16); // add
1524 break;
1525 case '-':
1526 o(0x4b000000 | l << 31 | x | a << 5 | b << 16); // sub
1527 break;
1528 case '/':
1529 o(0x1ac00c00 | l << 31 | x | a << 5 | b << 16); // sdiv
1530 break;
1531 case '^':
1532 o(0x4a000000 | l << 31 | x | a << 5 | b << 16); // eor
1533 break;
1534 case '|':
1535 o(0x2a000000 | l << 31 | x | a << 5 | b << 16); // orr
1536 break;
1537 case TOK_EQ:
1538 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1539 o(0x1a9f17e0 | x); // cset wA,eq
1540 break;
1541 case TOK_GE:
1542 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1543 o(0x1a9fb7e0 | x); // cset wA,ge
1544 break;
1545 case TOK_GT:
1546 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1547 o(0x1a9fd7e0 | x); // cset wA,gt
1548 break;
1549 case TOK_LE:
1550 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1551 o(0x1a9fc7e0 | x); // cset wA,le
1552 break;
1553 case TOK_LT:
1554 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1555 o(0x1a9fa7e0 | x); // cset wA,lt
1556 break;
1557 case TOK_NE:
1558 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1559 o(0x1a9f07e0 | x); // cset wA,ne
1560 break;
1561 case TOK_SAR:
1562 o(0x1ac02800 | l << 31 | x | a << 5 | b << 16); // asr
1563 break;
1564 case TOK_SHL:
1565 o(0x1ac02000 | l << 31 | x | a << 5 | b << 16); // lsl
1566 break;
1567 case TOK_SHR:
1568 o(0x1ac02400 | l << 31 | x | a << 5 | b << 16); // lsr
1569 break;
1570 case TOK_UDIV:
1571 case TOK_PDIV:
1572 o(0x1ac00800 | l << 31 | x | a << 5 | b << 16); // udiv
1573 break;
1574 case TOK_UGE:
1575 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1576 o(0x1a9f37e0 | x); // cset wA,cs
1577 break;
1578 case TOK_UGT:
1579 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1580 o(0x1a9f97e0 | x); // cset wA,hi
1581 break;
1582 case TOK_ULT:
1583 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1584 o(0x1a9f27e0 | x); // cset wA,cc
1585 break;
1586 case TOK_ULE:
1587 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1588 o(0x1a9f87e0 | x); // cset wA,ls
1589 break;
1590 case TOK_UMOD:
1591 // Use x30 for quotient:
1592 o(0x1ac00800 | l << 31 | 30 | a << 5 | b << 16); // udiv
1593 o(0x1b008000 | l << 31 | x | (uint32_t)30 << 5 |
1594 b << 16 | a << 10); // msub
1595 break;
1596 default:
1597 assert(0);
1601 ST_FUNC void gen_opi(int op)
1603 arm64_gen_opil(op, 0);
1604 arm64_vset_VT_CMP(op);
1607 ST_FUNC void gen_opl(int op)
1609 arm64_gen_opil(op, 1);
1610 arm64_vset_VT_CMP(op);
1613 ST_FUNC void gen_opf(int op)
1615 uint32_t x, a, b, dbl;
1617 if (vtop[0].type.t == VT_LDOUBLE) {
1618 CType type = vtop[0].type;
1619 int func = 0;
1620 int cond = -1;
1621 switch (op) {
1622 case '*': func = TOK___multf3; break;
1623 case '+': func = TOK___addtf3; break;
1624 case '-': func = TOK___subtf3; break;
1625 case '/': func = TOK___divtf3; break;
1626 case TOK_EQ: func = TOK___eqtf2; cond = 1; break;
1627 case TOK_NE: func = TOK___netf2; cond = 0; break;
1628 case TOK_LT: func = TOK___lttf2; cond = 10; break;
1629 case TOK_GE: func = TOK___getf2; cond = 11; break;
1630 case TOK_LE: func = TOK___letf2; cond = 12; break;
1631 case TOK_GT: func = TOK___gttf2; cond = 13; break;
1632 default: assert(0); break;
1634 vpush_global_sym(&func_old_type, func);
1635 vrott(3);
1636 gfunc_call(2);
1637 vpushi(0);
1638 vtop->r = cond < 0 ? REG_FRET : REG_IRET;
1639 if (cond < 0)
1640 vtop->type = type;
1641 else {
1642 o(0x7100001f); // cmp w0,#0
1643 o(0x1a9f07e0 | (uint32_t)cond << 12); // cset w0,(cond)
1645 return;
1648 dbl = vtop[0].type.t != VT_FLOAT;
1649 gv2(RC_FLOAT, RC_FLOAT);
1650 assert(vtop[-1].r < VT_CONST && vtop[0].r < VT_CONST);
1651 a = fltr(vtop[-1].r);
1652 b = fltr(vtop[0].r);
1653 vtop -= 2;
1654 switch (op) {
1655 case TOK_EQ: case TOK_NE:
1656 case TOK_LT: case TOK_GE: case TOK_LE: case TOK_GT:
1657 x = get_reg(RC_INT);
1658 ++vtop;
1659 vtop[0].r = x;
1660 x = intr(x);
1661 break;
1662 default:
1663 x = get_reg(RC_FLOAT);
1664 ++vtop;
1665 vtop[0].r = x;
1666 x = fltr(x);
1667 break;
1670 switch (op) {
1671 case '*':
1672 o(0x1e200800 | dbl << 22 | x | a << 5 | b << 16); // fmul
1673 break;
1674 case '+':
1675 o(0x1e202800 | dbl << 22 | x | a << 5 | b << 16); // fadd
1676 break;
1677 case '-':
1678 o(0x1e203800 | dbl << 22 | x | a << 5 | b << 16); // fsub
1679 break;
1680 case '/':
1681 o(0x1e201800 | dbl << 22 | x | a << 5 | b << 16); // fdiv
1682 break;
1683 case TOK_EQ:
1684 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1685 o(0x1a9f17e0 | x); // cset w(x),eq
1686 break;
1687 case TOK_GE:
1688 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1689 o(0x1a9fb7e0 | x); // cset w(x),ge
1690 break;
1691 case TOK_GT:
1692 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1693 o(0x1a9fd7e0 | x); // cset w(x),gt
1694 break;
1695 case TOK_LE:
1696 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1697 o(0x1a9f87e0 | x); // cset w(x),ls
1698 break;
1699 case TOK_LT:
1700 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1701 o(0x1a9f57e0 | x); // cset w(x),mi
1702 break;
1703 case TOK_NE:
1704 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1705 o(0x1a9f07e0 | x); // cset w(x),ne
1706 break;
1707 default:
1708 assert(0);
1710 arm64_vset_VT_CMP(op);
1713 // Generate sign extension from 32 to 64 bits:
1714 ST_FUNC void gen_cvt_sxtw(void)
1716 uint32_t r = intr(gv(RC_INT));
1717 o(0x93407c00 | r | r << 5); // sxtw x(r),w(r)
1720 /* char/short to int conversion */
1721 ST_FUNC void gen_cvt_csti(int t)
1723 int r = intr(gv(RC_INT));
1724 o(0x13001c00
1725 | ((t & VT_BTYPE) == VT_SHORT) << 13
1726 | (uint32_t)!!(t & VT_UNSIGNED) << 30
1727 | r | r << 5); // [su]xt[bh] w(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 & VT_BTYPE;
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 /*************************************************************/