Consolidate all relocations in relocate_section
[tinycc.git] / arm64-gen.c
blobc4e83e156b7702b4d7832ea414babb3fcf407267
1 /*
2 * A64 code generator for TCC
4 * Copyright (c) 2014-2015 Edmund Grimley Evans
6 * Copying and distribution of this file, with or without modification,
7 * are permitted in any medium without royalty provided the copyright
8 * notice and this notice are preserved. This file is offered as-is,
9 * without any warranty.
12 #ifdef TARGET_DEFS_ONLY
14 // Number of registers available to allocator:
15 #define NB_REGS 28 // x0-x18, x30, v0-v7
17 #define TREG_R(x) (x) // x = 0..18
18 #define TREG_R30 19
19 #define TREG_F(x) (x + 20) // x = 0..7
21 // Register classes sorted from more general to more precise:
22 #define RC_INT (1 << 0)
23 #define RC_FLOAT (1 << 1)
24 #define RC_R(x) (1 << (2 + (x))) // x = 0..18
25 #define RC_R30 (1 << 21)
26 #define RC_F(x) (1 << (22 + (x))) // x = 0..7
28 #define RC_IRET (RC_R(0)) // int return register class
29 #define RC_FRET (RC_F(0)) // float return register class
31 #define REG_IRET (TREG_R(0)) // int return register number
32 #define REG_FRET (TREG_F(0)) // float return register number
34 #define PTR_SIZE 8
36 #define LDOUBLE_SIZE 16
37 #define LDOUBLE_ALIGN 16
39 #define MAX_ALIGN 16
41 #define CHAR_IS_UNSIGNED
43 /******************************************************/
44 /* ELF defines */
46 #define EM_TCC_TARGET EM_AARCH64
48 #define R_DATA_32 R_AARCH64_ABS32
49 #define R_DATA_PTR R_AARCH64_ABS64
50 #define R_JMP_SLOT R_AARCH64_JUMP_SLOT
51 #define R_GLOB_DAT R_AARCH64_GLOB_DAT
52 #define R_COPY R_AARCH64_COPY
54 #define ELF_START_ADDR 0x00400000
55 #define ELF_PAGE_SIZE 0x1000
57 /******************************************************/
58 #else /* ! TARGET_DEFS_ONLY */
59 /******************************************************/
60 #include "tcc.h"
61 #include <assert.h>
63 ST_DATA const int reg_classes[NB_REGS] = {
64 RC_INT | RC_R(0),
65 RC_INT | RC_R(1),
66 RC_INT | RC_R(2),
67 RC_INT | RC_R(3),
68 RC_INT | RC_R(4),
69 RC_INT | RC_R(5),
70 RC_INT | RC_R(6),
71 RC_INT | RC_R(7),
72 RC_INT | RC_R(8),
73 RC_INT | RC_R(9),
74 RC_INT | RC_R(10),
75 RC_INT | RC_R(11),
76 RC_INT | RC_R(12),
77 RC_INT | RC_R(13),
78 RC_INT | RC_R(14),
79 RC_INT | RC_R(15),
80 RC_INT | RC_R(16),
81 RC_INT | RC_R(17),
82 RC_INT | RC_R(18),
83 RC_R30, // not in RC_INT as we make special use of x30
84 RC_FLOAT | RC_F(0),
85 RC_FLOAT | RC_F(1),
86 RC_FLOAT | RC_F(2),
87 RC_FLOAT | RC_F(3),
88 RC_FLOAT | RC_F(4),
89 RC_FLOAT | RC_F(5),
90 RC_FLOAT | RC_F(6),
91 RC_FLOAT | RC_F(7)
94 #define IS_FREG(x) ((x) >= TREG_F(0))
96 static uint32_t intr(int r)
98 assert(TREG_R(0) <= r && r <= TREG_R30);
99 return r < TREG_R30 ? r : 30;
102 static uint32_t fltr(int r)
104 assert(TREG_F(0) <= r && r <= TREG_F(7));
105 return r - TREG_F(0);
108 // Add an instruction to text section:
109 ST_FUNC void o(unsigned int c)
111 int ind1 = ind + 4;
112 if (ind1 > cur_text_section->data_allocated)
113 section_realloc(cur_text_section, ind1);
114 write32le(cur_text_section->data + ind, c);
115 ind = ind1;
118 static int arm64_encode_bimm64(uint64_t x)
120 int neg = x & 1;
121 int rep, pos, len;
123 if (neg)
124 x = ~x;
125 if (!x)
126 return -1;
128 if (x >> 2 == (x & (((uint64_t)1 << (64 - 2)) - 1)))
129 rep = 2, x &= ((uint64_t)1 << 2) - 1;
130 else if (x >> 4 == (x & (((uint64_t)1 << (64 - 4)) - 1)))
131 rep = 4, x &= ((uint64_t)1 << 4) - 1;
132 else if (x >> 8 == (x & (((uint64_t)1 << (64 - 8)) - 1)))
133 rep = 8, x &= ((uint64_t)1 << 8) - 1;
134 else if (x >> 16 == (x & (((uint64_t)1 << (64 - 16)) - 1)))
135 rep = 16, x &= ((uint64_t)1 << 16) - 1;
136 else if (x >> 32 == (x & (((uint64_t)1 << (64 - 32)) - 1)))
137 rep = 32, x &= ((uint64_t)1 << 32) - 1;
138 else
139 rep = 64;
141 pos = 0;
142 if (!(x & (((uint64_t)1 << 32) - 1))) x >>= 32, pos += 32;
143 if (!(x & (((uint64_t)1 << 16) - 1))) x >>= 16, pos += 16;
144 if (!(x & (((uint64_t)1 << 8) - 1))) x >>= 8, pos += 8;
145 if (!(x & (((uint64_t)1 << 4) - 1))) x >>= 4, pos += 4;
146 if (!(x & (((uint64_t)1 << 2) - 1))) x >>= 2, pos += 2;
147 if (!(x & (((uint64_t)1 << 1) - 1))) x >>= 1, pos += 1;
149 len = 0;
150 if (!(~x & (((uint64_t)1 << 32) - 1))) x >>= 32, len += 32;
151 if (!(~x & (((uint64_t)1 << 16) - 1))) x >>= 16, len += 16;
152 if (!(~x & (((uint64_t)1 << 8) - 1))) x >>= 8, len += 8;
153 if (!(~x & (((uint64_t)1 << 4) - 1))) x >>= 4, len += 4;
154 if (!(~x & (((uint64_t)1 << 2) - 1))) x >>= 2, len += 2;
155 if (!(~x & (((uint64_t)1 << 1) - 1))) x >>= 1, len += 1;
157 if (x)
158 return -1;
159 if (neg) {
160 pos = (pos + len) & (rep - 1);
161 len = rep - len;
163 return ((0x1000 & rep << 6) | (((rep - 1) ^ 31) << 1 & 63) |
164 ((rep - pos) & (rep - 1)) << 6 | (len - 1));
167 static uint32_t arm64_movi(int r, uint64_t x)
169 uint64_t m = 0xffff;
170 int e;
171 if (!(x & ~m))
172 return 0x52800000 | r | x << 5; // movz w(r),#(x)
173 if (!(x & ~(m << 16)))
174 return 0x52a00000 | r | x >> 11; // movz w(r),#(x >> 16),lsl #16
175 if (!(x & ~(m << 32)))
176 return 0xd2c00000 | r | x >> 27; // movz x(r),#(x >> 32),lsl #32
177 if (!(x & ~(m << 48)))
178 return 0xd2e00000 | r | x >> 43; // movz x(r),#(x >> 48),lsl #48
179 if ((x & ~m) == m << 16)
180 return (0x12800000 | r |
181 (~x << 5 & 0x1fffe0)); // movn w(r),#(~x)
182 if ((x & ~(m << 16)) == m)
183 return (0x12a00000 | r |
184 (~x >> 11 & 0x1fffe0)); // movn w(r),#(~x >> 16),lsl #16
185 if (!~(x | m))
186 return (0x92800000 | r |
187 (~x << 5 & 0x1fffe0)); // movn x(r),#(~x)
188 if (!~(x | m << 16))
189 return (0x92a00000 | r |
190 (~x >> 11 & 0x1fffe0)); // movn x(r),#(~x >> 16),lsl #16
191 if (!~(x | m << 32))
192 return (0x92c00000 | r |
193 (~x >> 27 & 0x1fffe0)); // movn x(r),#(~x >> 32),lsl #32
194 if (!~(x | m << 48))
195 return (0x92e00000 | r |
196 (~x >> 43 & 0x1fffe0)); // movn x(r),#(~x >> 32),lsl #32
197 if (!(x >> 32) && (e = arm64_encode_bimm64(x | x << 32)) >= 0)
198 return 0x320003e0 | r | (uint32_t)e << 10; // movi w(r),#(x)
199 if ((e = arm64_encode_bimm64(x)) >= 0)
200 return 0xb20003e0 | r | (uint32_t)e << 10; // movi x(r),#(x)
201 return 0;
204 static void arm64_movimm(int r, uint64_t x)
206 uint32_t i;
207 if ((i = arm64_movi(r, x)))
208 o(i); // a single MOV
209 else {
210 // MOVZ/MOVN and 1-3 MOVKs
211 int z = 0, m = 0;
212 uint32_t mov1 = 0xd2800000; // movz
213 uint64_t x1 = x;
214 for (i = 0; i < 64; i += 16) {
215 z += !(x >> i & 0xffff);
216 m += !(~x >> i & 0xffff);
218 if (m > z) {
219 x1 = ~x;
220 mov1 = 0x92800000; // movn
222 for (i = 0; i < 64; i += 16)
223 if (x1 >> i & 0xffff) {
224 o(mov1 | r | (x1 >> i & 0xffff) << 5 | i << 17);
225 // movz/movn x(r),#(*),lsl #(i)
226 break;
228 for (i += 16; i < 64; i += 16)
229 if (x1 >> i & 0xffff)
230 o(0xf2800000 | r | (x >> i & 0xffff) << 5 | i << 17);
231 // movk x(r),#(*),lsl #(i)
235 // Patch all branches in list pointed to by t to branch to a:
236 ST_FUNC void gsym_addr(int t_, int a_)
238 uint32_t t = t_;
239 uint32_t a = a_;
240 while (t) {
241 unsigned char *ptr = cur_text_section->data + t;
242 uint32_t next = read32le(ptr);
243 if (a - t + 0x8000000 >= 0x10000000)
244 tcc_error("branch out of range");
245 write32le(ptr, (a - t == 4 ? 0xd503201f : // nop
246 0x14000000 | ((a - t) >> 2 & 0x3ffffff))); // b
247 t = next;
251 // Patch all branches in list pointed to by t to branch to current location:
252 ST_FUNC void gsym(int t)
254 gsym_addr(t, ind);
257 static int arm64_type_size(int t)
259 switch (t & VT_BTYPE) {
260 case VT_INT: return 2;
261 case VT_BYTE: return 0;
262 case VT_SHORT: return 1;
263 case VT_PTR: return 3;
264 case VT_ENUM: return 2;
265 case VT_FUNC: return 3;
266 case VT_FLOAT: return 2;
267 case VT_DOUBLE: return 3;
268 case VT_LDOUBLE: return 4;
269 case VT_BOOL: return 0;
270 case VT_LLONG: return 3;
272 assert(0);
273 return 0;
276 static void arm64_spoff(int reg, uint64_t off)
278 uint32_t sub = off >> 63;
279 if (sub)
280 off = -off;
281 if (off < 4096)
282 o(0x910003e0 | sub << 30 | reg | off << 10);
283 // (add|sub) x(reg),sp,#(off)
284 else {
285 arm64_movimm(30, off); // use x30 for offset
286 o(0x8b3e63e0 | sub << 30 | reg); // (add|sub) x(reg),sp,x30
290 static void arm64_ldrx(int sg, int sz_, int dst, int bas, uint64_t off)
292 uint32_t sz = sz_;
293 if (sz >= 2)
294 sg = 0;
295 if (!(off & ~((uint32_t)0xfff << sz)))
296 o(0x39400000 | dst | bas << 5 | off << (10 - sz) |
297 (uint32_t)!!sg << 23 | sz << 30); // ldr(*) x(dst),[x(bas),#(off)]
298 else if (off < 256 || -off <= 256)
299 o(0x38400000 | dst | bas << 5 | (off & 511) << 12 |
300 (uint32_t)!!sg << 23 | sz << 30); // ldur(*) x(dst),[x(bas),#(off)]
301 else {
302 arm64_movimm(30, off); // use x30 for offset
303 o(0x38206800 | dst | bas << 5 | (uint32_t)30 << 16 |
304 (uint32_t)(!!sg + 1) << 22 | sz << 30); // ldr(*) x(dst),[x(bas),x30]
308 static void arm64_ldrv(int sz_, int dst, int bas, uint64_t off)
310 uint32_t sz = sz_;
311 if (!(off & ~((uint32_t)0xfff << sz)))
312 o(0x3d400000 | dst | bas << 5 | off << (10 - sz) |
313 (sz & 4) << 21 | (sz & 3) << 30); // ldr (s|d|q)(dst),[x(bas),#(off)]
314 else if (off < 256 || -off <= 256)
315 o(0x3c400000 | dst | bas << 5 | (off & 511) << 12 |
316 (sz & 4) << 21 | (sz & 3) << 30); // ldur (s|d|q)(dst),[x(bas),#(off)]
317 else {
318 arm64_movimm(30, off); // use x30 for offset
319 o(0x3c606800 | dst | bas << 5 | (uint32_t)30 << 16 |
320 sz << 30 | (sz & 4) << 21); // ldr (s|d|q)(dst),[x(bas),x30]
324 static void arm64_ldrs(int reg_, int size)
326 uint32_t reg = reg_;
327 // Use x30 for intermediate value in some cases.
328 switch (size) {
329 default: assert(0); break;
330 case 1:
331 arm64_ldrx(0, 0, reg, reg, 0);
332 break;
333 case 2:
334 arm64_ldrx(0, 1, reg, reg, 0);
335 break;
336 case 3:
337 arm64_ldrx(0, 1, 30, reg, 0);
338 arm64_ldrx(0, 0, reg, reg, 2);
339 o(0x2a0043c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #16
340 break;
341 case 4:
342 arm64_ldrx(0, 2, reg, reg, 0);
343 break;
344 case 5:
345 arm64_ldrx(0, 2, 30, reg, 0);
346 arm64_ldrx(0, 0, reg, reg, 4);
347 o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32
348 break;
349 case 6:
350 arm64_ldrx(0, 2, 30, reg, 0);
351 arm64_ldrx(0, 1, reg, reg, 4);
352 o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32
353 break;
354 case 7:
355 arm64_ldrx(0, 2, 30, reg, 0);
356 arm64_ldrx(0, 2, reg, reg, 3);
357 o(0x53087c00 | reg | reg << 5); // lsr w(reg), w(reg), #8
358 o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32
359 break;
360 case 8:
361 arm64_ldrx(0, 3, reg, reg, 0);
362 break;
363 case 9:
364 arm64_ldrx(0, 0, reg + 1, reg, 8);
365 arm64_ldrx(0, 3, reg, reg, 0);
366 break;
367 case 10:
368 arm64_ldrx(0, 1, reg + 1, reg, 8);
369 arm64_ldrx(0, 3, reg, reg, 0);
370 break;
371 case 11:
372 arm64_ldrx(0, 2, reg + 1, reg, 7);
373 o(0x53087c00 | (reg+1) | (reg+1) << 5); // lsr w(reg+1), w(reg+1), #8
374 arm64_ldrx(0, 3, reg, reg, 0);
375 break;
376 case 12:
377 arm64_ldrx(0, 2, reg + 1, reg, 8);
378 arm64_ldrx(0, 3, reg, reg, 0);
379 break;
380 case 13:
381 arm64_ldrx(0, 3, reg + 1, reg, 5);
382 o(0xd358fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #24
383 arm64_ldrx(0, 3, reg, reg, 0);
384 break;
385 case 14:
386 arm64_ldrx(0, 3, reg + 1, reg, 6);
387 o(0xd350fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #16
388 arm64_ldrx(0, 3, reg, reg, 0);
389 break;
390 case 15:
391 arm64_ldrx(0, 3, reg + 1, reg, 7);
392 o(0xd348fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #8
393 arm64_ldrx(0, 3, reg, reg, 0);
394 break;
395 case 16:
396 o(0xa9400000 | reg | (reg+1) << 10 | reg << 5);
397 // ldp x(reg),x(reg+1),[x(reg)]
398 break;
402 static void arm64_strx(int sz_, int dst, int bas, uint64_t off)
404 uint32_t sz = sz_;
405 if (!(off & ~((uint32_t)0xfff << sz)))
406 o(0x39000000 | dst | bas << 5 | off << (10 - sz) | sz << 30);
407 // str(*) x(dst),[x(bas],#(off)]
408 else if (off < 256 || -off <= 256)
409 o(0x38000000 | dst | bas << 5 | (off & 511) << 12 | sz << 30);
410 // stur(*) x(dst),[x(bas],#(off)]
411 else {
412 arm64_movimm(30, off); // use x30 for offset
413 o(0x38206800 | dst | bas << 5 | (uint32_t)30 << 16 | sz << 30);
414 // str(*) x(dst),[x(bas),x30]
418 static void arm64_strv(int sz_, int dst, int bas, uint64_t off)
420 uint32_t sz = sz_;
421 if (!(off & ~((uint32_t)0xfff << sz)))
422 o(0x3d000000 | dst | bas << 5 | off << (10 - sz) |
423 (sz & 4) << 21 | (sz & 3) << 30); // str (s|d|q)(dst),[x(bas),#(off)]
424 else if (off < 256 || -off <= 256)
425 o(0x3c000000 | dst | bas << 5 | (off & 511) << 12 |
426 (sz & 4) << 21 | (sz & 3) << 30); // stur (s|d|q)(dst),[x(bas),#(off)]
427 else {
428 arm64_movimm(30, off); // use x30 for offset
429 o(0x3c206800 | dst | bas << 5 | (uint32_t)30 << 16 |
430 sz << 30 | (sz & 4) << 21); // str (s|d|q)(dst),[x(bas),x30]
434 static void arm64_sym(int r, Sym *sym, unsigned long addend)
436 // Currently TCC's linker does not generate COPY relocations for
437 // STT_OBJECTs when tcc is invoked with "-run". This typically
438 // results in "R_AARCH64_ADR_PREL_PG_HI21 relocation failed" when
439 // a program refers to stdin. A workaround is to avoid that
440 // relocation and use only relocations with unlimited range.
441 int avoid_adrp = 1;
443 if (avoid_adrp || (sym->type.t & VT_WEAK)) {
444 // (GCC uses a R_AARCH64_ABS64 in this case.)
445 greloca(cur_text_section, sym, ind, R_AARCH64_MOVW_UABS_G0_NC, addend);
446 o(0xd2800000 | r); // mov x(rt),#0,lsl #0
447 greloca(cur_text_section, sym, ind, R_AARCH64_MOVW_UABS_G1_NC, addend);
448 o(0xf2a00000 | r); // movk x(rt),#0,lsl #16
449 greloca(cur_text_section, sym, ind, R_AARCH64_MOVW_UABS_G2_NC, addend);
450 o(0xf2c00000 | r); // movk x(rt),#0,lsl #32
451 greloca(cur_text_section, sym, ind, R_AARCH64_MOVW_UABS_G3, addend);
452 o(0xf2e00000 | r); // movk x(rt),#0,lsl #48
454 else {
455 greloca(cur_text_section, sym, ind, R_AARCH64_ADR_PREL_PG_HI21, addend);
456 o(0x90000000 | r);
457 greloca(cur_text_section, sym, ind, R_AARCH64_ADD_ABS_LO12_NC, addend);
458 o(0x91000000 | r | r << 5);
462 ST_FUNC void load(int r, SValue *sv)
464 int svtt = sv->type.t;
465 int svr = sv->r & ~VT_LVAL_TYPE;
466 int svrv = svr & VT_VALMASK;
467 uint64_t svcul = (uint32_t)sv->c.i;
468 svcul = svcul >> 31 & 1 ? svcul - ((uint64_t)1 << 32) : svcul;
470 if (svr == (VT_LOCAL | VT_LVAL)) {
471 if (IS_FREG(r))
472 arm64_ldrv(arm64_type_size(svtt), fltr(r), 29, svcul);
473 else
474 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
475 intr(r), 29, svcul);
476 return;
479 if ((svr & ~VT_VALMASK) == VT_LVAL && svrv < VT_CONST) {
480 if (IS_FREG(r))
481 arm64_ldrv(arm64_type_size(svtt), fltr(r), intr(svrv), 0);
482 else
483 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
484 intr(r), intr(svrv), 0);
485 return;
488 if (svr == (VT_CONST | VT_LVAL | VT_SYM)) {
489 arm64_sym(30, sv->sym, svcul); // use x30 for address
490 if (IS_FREG(r))
491 arm64_ldrv(arm64_type_size(svtt), fltr(r), 30, 0);
492 else
493 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
494 intr(r), 30, 0);
495 return;
498 if (svr == (VT_CONST | VT_SYM)) {
499 arm64_sym(intr(r), sv->sym, svcul);
500 return;
503 if (svr == VT_CONST) {
504 if ((svtt & VT_BTYPE) != VT_VOID)
505 arm64_movimm(intr(r), arm64_type_size(svtt) == 3 ?
506 sv->c.i : (uint32_t)svcul);
507 return;
510 if (svr < VT_CONST) {
511 if (IS_FREG(r) && IS_FREG(svr))
512 if (svtt == VT_LDOUBLE)
513 o(0x4ea01c00 | fltr(r) | fltr(svr) << 5);
514 // mov v(r).16b,v(svr).16b
515 else
516 o(0x1e604000 | fltr(r) | fltr(svr) << 5); // fmov d(r),d(svr)
517 else if (!IS_FREG(r) && !IS_FREG(svr))
518 o(0xaa0003e0 | intr(r) | intr(svr) << 16); // mov x(r),x(svr)
519 else
520 assert(0);
521 return;
524 if (svr == VT_LOCAL) {
525 if (-svcul < 0x1000)
526 o(0xd10003a0 | intr(r) | -svcul << 10); // sub x(r),x29,#...
527 else {
528 arm64_movimm(30, -svcul); // use x30 for offset
529 o(0xcb0003a0 | intr(r) | (uint32_t)30 << 16); // sub x(r),x29,x30
531 return;
534 if (svr == VT_JMP || svr == VT_JMPI) {
535 int t = (svr == VT_JMPI);
536 arm64_movimm(intr(r), t);
537 o(0x14000002); // b .+8
538 gsym(svcul);
539 arm64_movimm(intr(r), t ^ 1);
540 return;
543 if (svr == (VT_LLOCAL | VT_LVAL)) {
544 arm64_ldrx(0, 3, 30, 29, svcul); // use x30 for offset
545 if (IS_FREG(r))
546 arm64_ldrv(arm64_type_size(svtt), fltr(r), 30, 0);
547 else
548 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
549 intr(r), 30, 0);
550 return;
553 printf("load(%x, (%x, %x, %llx))\n", r, svtt, sv->r, (long long)svcul);
554 assert(0);
557 ST_FUNC void store(int r, SValue *sv)
559 int svtt = sv->type.t;
560 int svr = sv->r & ~VT_LVAL_TYPE;
561 int svrv = svr & VT_VALMASK;
562 uint64_t svcul = (uint32_t)sv->c.i;
563 svcul = svcul >> 31 & 1 ? svcul - ((uint64_t)1 << 32) : svcul;
565 if (svr == (VT_LOCAL | VT_LVAL)) {
566 if (IS_FREG(r))
567 arm64_strv(arm64_type_size(svtt), fltr(r), 29, svcul);
568 else
569 arm64_strx(arm64_type_size(svtt), intr(r), 29, svcul);
570 return;
573 if ((svr & ~VT_VALMASK) == VT_LVAL && svrv < VT_CONST) {
574 if (IS_FREG(r))
575 arm64_strv(arm64_type_size(svtt), fltr(r), intr(svrv), 0);
576 else
577 arm64_strx(arm64_type_size(svtt), intr(r), intr(svrv), 0);
578 return;
581 if (svr == (VT_CONST | VT_LVAL | VT_SYM)) {
582 arm64_sym(30, sv->sym, svcul); // use x30 for address
583 if (IS_FREG(r))
584 arm64_strv(arm64_type_size(svtt), fltr(r), 30, 0);
585 else
586 arm64_strx(arm64_type_size(svtt), intr(r), 30, 0);
587 return;
590 printf("store(%x, (%x, %x, %llx))\n", r, svtt, sv->r, (long long)svcul);
591 assert(0);
594 static void arm64_gen_bl_or_b(int b)
596 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
597 assert(!b && (vtop->r & VT_SYM));
598 greloc(cur_text_section, vtop->sym, ind, R_AARCH64_CALL26);
599 o(0x94000000); // bl .
601 else
602 o(0xd61f0000 | (uint32_t)!b << 21 | intr(gv(RC_R30)) << 5); // br/blr
605 static int arm64_hfa_aux(CType *type, int *fsize, int num)
607 if (is_float(type->t)) {
608 int a, n = type_size(type, &a);
609 if (num >= 4 || (*fsize && *fsize != n))
610 return -1;
611 *fsize = n;
612 return num + 1;
614 else if ((type->t & VT_BTYPE) == VT_STRUCT) {
615 int is_struct = 0; // rather than union
616 Sym *field;
617 for (field = type->ref->next; field; field = field->next)
618 if (field->c) {
619 is_struct = 1;
620 break;
622 if (is_struct) {
623 int num0 = num;
624 for (field = type->ref->next; field; field = field->next) {
625 if (field->c != (num - num0) * *fsize)
626 return -1;
627 num = arm64_hfa_aux(&field->type, fsize, num);
628 if (num == -1)
629 return -1;
631 if (type->ref->c != (num - num0) * *fsize)
632 return -1;
633 return num;
635 else { // union
636 int num0 = num;
637 for (field = type->ref->next; field; field = field->next) {
638 int num1 = arm64_hfa_aux(&field->type, fsize, num0);
639 if (num1 == -1)
640 return -1;
641 num = num1 < num ? num : num1;
643 if (type->ref->c != (num - num0) * *fsize)
644 return -1;
645 return num;
648 else if (type->t & VT_ARRAY) {
649 int num1;
650 if (!type->ref->c)
651 return num;
652 num1 = arm64_hfa_aux(&type->ref->type, fsize, num);
653 if (num1 == -1 || (num1 != num && type->ref->c > 4))
654 return -1;
655 num1 = num + type->ref->c * (num1 - num);
656 if (num1 > 4)
657 return -1;
658 return num1;
660 return -1;
663 static int arm64_hfa(CType *type, int *fsize)
665 if ((type->t & VT_BTYPE) == VT_STRUCT || (type->t & VT_ARRAY)) {
666 int sz = 0;
667 int n = arm64_hfa_aux(type, &sz, 0);
668 if (0 < n && n <= 4) {
669 if (fsize)
670 *fsize = sz;
671 return n;
674 return 0;
677 static unsigned long arm64_pcs_aux(int n, CType **type, unsigned long *a)
679 int nx = 0; // next integer register
680 int nv = 0; // next vector register
681 unsigned long ns = 32; // next stack offset
682 int i;
684 for (i = 0; i < n; i++) {
685 int hfa = arm64_hfa(type[i], 0);
686 int size, align;
688 if ((type[i]->t & VT_ARRAY) ||
689 (type[i]->t & VT_BTYPE) == VT_FUNC)
690 size = align = 8;
691 else
692 size = type_size(type[i], &align);
694 if (hfa)
695 // B.2
697 else if (size > 16) {
698 // B.3: replace with pointer
699 if (nx < 8)
700 a[i] = nx++ << 1 | 1;
701 else {
702 ns = (ns + 7) & ~7;
703 a[i] = ns | 1;
704 ns += 8;
706 continue;
708 else if ((type[i]->t & VT_BTYPE) == VT_STRUCT)
709 // B.4
710 size = (size + 7) & ~7;
712 // C.1
713 if (is_float(type[i]->t) && nv < 8) {
714 a[i] = 16 + (nv++ << 1);
715 continue;
718 // C.2
719 if (hfa && nv + hfa <= 8) {
720 a[i] = 16 + (nv << 1);
721 nv += hfa;
722 continue;
725 // C.3
726 if (hfa) {
727 nv = 8;
728 size = (size + 7) & ~7;
731 // C.4
732 if (hfa || (type[i]->t & VT_BTYPE) == VT_LDOUBLE) {
733 ns = (ns + 7) & ~7;
734 ns = (ns + align - 1) & -align;
737 // C.5
738 if ((type[i]->t & VT_BTYPE) == VT_FLOAT)
739 size = 8;
741 // C.6
742 if (hfa || is_float(type[i]->t)) {
743 a[i] = ns;
744 ns += size;
745 continue;
748 // C.7
749 if ((type[i]->t & VT_BTYPE) != VT_STRUCT && size <= 8 && nx < 8) {
750 a[i] = nx++ << 1;
751 continue;
754 // C.8
755 if (align == 16)
756 nx = (nx + 1) & ~1;
758 // C.9
759 if ((type[i]->t & VT_BTYPE) != VT_STRUCT && size == 16 && nx < 7) {
760 a[i] = nx << 1;
761 nx += 2;
762 continue;
765 // C.10
766 if ((type[i]->t & VT_BTYPE) == VT_STRUCT && size <= (8 - nx) * 8) {
767 a[i] = nx << 1;
768 nx += (size + 7) >> 3;
769 continue;
772 // C.11
773 nx = 8;
775 // C.12
776 ns = (ns + 7) & ~7;
777 ns = (ns + align - 1) & -align;
779 // C.13
780 if ((type[i]->t & VT_BTYPE) == VT_STRUCT) {
781 a[i] = ns;
782 ns += size;
783 continue;
786 // C.14
787 if (size < 8)
788 size = 8;
790 // C.15
791 a[i] = ns;
792 ns += size;
795 return ns - 32;
798 static unsigned long arm64_pcs(int n, CType **type, unsigned long *a)
800 unsigned long stack;
802 // Return type:
803 if ((type[0]->t & VT_BTYPE) == VT_VOID)
804 a[0] = -1;
805 else {
806 arm64_pcs_aux(1, type, a);
807 assert(a[0] == 0 || a[0] == 1 || a[0] == 16);
810 // Argument types:
811 stack = arm64_pcs_aux(n, type + 1, a + 1);
813 if (0) {
814 int i;
815 for (i = 0; i <= n; i++) {
816 if (!i)
817 printf("arm64_pcs return: ");
818 else
819 printf("arm64_pcs arg %d: ", i);
820 if (a[i] == (unsigned long)-1)
821 printf("void\n");
822 else if (a[i] == 1 && !i)
823 printf("X8 pointer\n");
824 else if (a[i] < 16)
825 printf("X%lu%s\n", a[i] / 2, a[i] & 1 ? " pointer" : "");
826 else if (a[i] < 32)
827 printf("V%lu\n", a[i] / 2 - 8);
828 else
829 printf("stack %lu%s\n",
830 (a[i] - 32) & ~1, a[i] & 1 ? " pointer" : "");
834 return stack;
837 ST_FUNC void gfunc_call(int nb_args)
839 CType *return_type;
840 CType **t;
841 unsigned long *a, *a1;
842 unsigned long stack;
843 int i;
845 return_type = &vtop[-nb_args].type.ref->type;
846 if ((return_type->t & VT_BTYPE) == VT_STRUCT)
847 --nb_args;
849 t = tcc_malloc((nb_args + 1) * sizeof(*t));
850 a = tcc_malloc((nb_args + 1) * sizeof(*a));
851 a1 = tcc_malloc((nb_args + 1) * sizeof(*a1));
853 t[0] = return_type;
854 for (i = 0; i < nb_args; i++)
855 t[nb_args - i] = &vtop[-i].type;
857 stack = arm64_pcs(nb_args, t, a);
859 // Allocate space for structs replaced by pointer:
860 for (i = nb_args; i; i--)
861 if (a[i] & 1) {
862 SValue *arg = &vtop[i - nb_args];
863 int align, size = type_size(&arg->type, &align);
864 assert((arg->type.t & VT_BTYPE) == VT_STRUCT);
865 stack = (stack + align - 1) & -align;
866 a1[i] = stack;
867 stack += size;
870 stack = (stack + 15) >> 4 << 4;
872 assert(stack < 0x1000);
873 if (stack)
874 o(0xd10003ff | stack << 10); // sub sp,sp,#(n)
876 // First pass: set all values on stack
877 for (i = nb_args; i; i--) {
878 vpushv(vtop - nb_args + i);
880 if (a[i] & 1) {
881 // struct replaced by pointer
882 int r = get_reg(RC_INT);
883 arm64_spoff(intr(r), a1[i]);
884 vset(&vtop->type, r | VT_LVAL, 0);
885 vswap();
886 vstore();
887 if (a[i] >= 32) {
888 // pointer on stack
889 r = get_reg(RC_INT);
890 arm64_spoff(intr(r), a1[i]);
891 arm64_strx(3, intr(r), 31, (a[i] - 32) >> 1 << 1);
894 else if (a[i] >= 32) {
895 // value on stack
896 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
897 int r = get_reg(RC_INT);
898 arm64_spoff(intr(r), a[i] - 32);
899 vset(&vtop->type, r | VT_LVAL, 0);
900 vswap();
901 vstore();
903 else if (is_float(vtop->type.t)) {
904 gv(RC_FLOAT);
905 arm64_strv(arm64_type_size(vtop[0].type.t),
906 fltr(vtop[0].r), 31, a[i] - 32);
908 else {
909 gv(RC_INT);
910 arm64_strx(arm64_type_size(vtop[0].type.t),
911 intr(vtop[0].r), 31, a[i] - 32);
915 --vtop;
918 // Second pass: assign values to registers
919 for (i = nb_args; i; i--, vtop--) {
920 if (a[i] < 16 && !(a[i] & 1)) {
921 // value in general-purpose registers
922 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
923 int align, size = type_size(&vtop->type, &align);
924 vtop->type.t = VT_PTR;
925 gaddrof();
926 gv(RC_R(a[i] / 2));
927 arm64_ldrs(a[i] / 2, size);
929 else
930 gv(RC_R(a[i] / 2));
932 else if (a[i] < 16)
933 // struct replaced by pointer in register
934 arm64_spoff(a[i] / 2, a1[i]);
935 else if (a[i] < 32) {
936 // value in floating-point registers
937 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
938 uint32_t j, sz, n = arm64_hfa(&vtop->type, &sz);
939 vtop->type.t = VT_PTR;
940 gaddrof();
941 gv(RC_R30);
942 for (j = 0; j < n; j++)
943 o(0x3d4003c0 |
944 (sz & 16) << 19 | -(sz & 8) << 27 | (sz & 4) << 29 |
945 (a[i] / 2 - 8 + j) |
946 j << 10); // ldr ([sdq])(*),[x30,#(j * sz)]
948 else
949 gv(RC_F(a[i] / 2 - 8));
953 if ((return_type->t & VT_BTYPE) == VT_STRUCT) {
954 if (a[0] == 1) {
955 // indirect return: set x8 and discard the stack value
956 gv(RC_R(8));
957 --vtop;
959 else
960 // return in registers: keep the address for after the call
961 vswap();
964 save_regs(0);
965 arm64_gen_bl_or_b(0);
966 --vtop;
967 if (stack)
968 o(0x910003ff | stack << 10); // add sp,sp,#(n)
971 int rt = return_type->t;
972 int bt = rt & VT_BTYPE;
973 if (bt == VT_BYTE || bt == VT_SHORT)
974 // Promote small integers:
975 o(0x13001c00 | (bt == VT_SHORT) << 13 |
976 (uint32_t)!!(rt & VT_UNSIGNED) << 30); // [su]xt[bh] w0,w0
977 else if (bt == VT_STRUCT && !(a[0] & 1)) {
978 // A struct was returned in registers, so write it out:
979 gv(RC_R(8));
980 --vtop;
981 if (a[0] == 0) {
982 int align, size = type_size(return_type, &align);
983 assert(size <= 16);
984 if (size > 8)
985 o(0xa9000500); // stp x0,x1,[x8]
986 else if (size)
987 arm64_strx(size > 4 ? 3 : size > 2 ? 2 : size > 1, 0, 8, 0);
990 else if (a[0] == 16) {
991 uint32_t j, sz, n = arm64_hfa(return_type, &sz);
992 for (j = 0; j < n; j++)
993 o(0x3d000100 |
994 (sz & 16) << 19 | -(sz & 8) << 27 | (sz & 4) << 29 |
995 (a[i] / 2 - 8 + j) |
996 j << 10); // str ([sdq])(*),[x8,#(j * sz)]
1001 tcc_free(a1);
1002 tcc_free(a);
1003 tcc_free(t);
1006 static unsigned long arm64_func_va_list_stack;
1007 static int arm64_func_va_list_gr_offs;
1008 static int arm64_func_va_list_vr_offs;
1009 static int arm64_func_sub_sp_offset;
1011 ST_FUNC void gfunc_prolog(CType *func_type)
1013 int n = 0;
1014 int i = 0;
1015 Sym *sym;
1016 CType **t;
1017 unsigned long *a;
1019 // Why doesn't the caller (gen_function) set func_vt?
1020 func_vt = func_type->ref->type;
1021 func_vc = 144; // offset of where x8 is stored
1023 for (sym = func_type->ref; sym; sym = sym->next)
1024 ++n;
1025 t = tcc_malloc(n * sizeof(*t));
1026 a = tcc_malloc(n * sizeof(*a));
1028 for (sym = func_type->ref; sym; sym = sym->next)
1029 t[i++] = &sym->type;
1031 arm64_func_va_list_stack = arm64_pcs(n - 1, t, a);
1033 o(0xa9b27bfd); // stp x29,x30,[sp,#-224]!
1034 o(0xad0087e0); // stp q0,q1,[sp,#16]
1035 o(0xad018fe2); // stp q2,q3,[sp,#48]
1036 o(0xad0297e4); // stp q4,q5,[sp,#80]
1037 o(0xad039fe6); // stp q6,q7,[sp,#112]
1038 o(0xa90923e8); // stp x8,x8,[sp,#144]
1039 o(0xa90a07e0); // stp x0,x1,[sp,#160]
1040 o(0xa90b0fe2); // stp x2,x3,[sp,#176]
1041 o(0xa90c17e4); // stp x4,x5,[sp,#192]
1042 o(0xa90d1fe6); // stp x6,x7,[sp,#208]
1044 arm64_func_va_list_gr_offs = -64;
1045 arm64_func_va_list_vr_offs = -128;
1047 for (i = 1, sym = func_type->ref->next; sym; i++, sym = sym->next) {
1048 int off = (a[i] < 16 ? 160 + a[i] / 2 * 8 :
1049 a[i] < 32 ? 16 + (a[i] - 16) / 2 * 16 :
1050 224 + ((a[i] - 32) >> 1 << 1));
1051 sym_push(sym->v & ~SYM_FIELD, &sym->type,
1052 (a[i] & 1 ? VT_LLOCAL : VT_LOCAL) | lvalue_type(sym->type.t),
1053 off);
1055 if (a[i] < 16) {
1056 int align, size = type_size(&sym->type, &align);
1057 arm64_func_va_list_gr_offs = (a[i] / 2 - 7 +
1058 (!(a[i] & 1) && size > 8)) * 8;
1060 else if (a[i] < 32) {
1061 uint32_t hfa = arm64_hfa(&sym->type, 0);
1062 arm64_func_va_list_vr_offs = (a[i] / 2 - 16 +
1063 (hfa ? hfa : 1)) * 16;
1066 // HFAs of float and double need to be written differently:
1067 if (16 <= a[i] && a[i] < 32 && (sym->type.t & VT_BTYPE) == VT_STRUCT) {
1068 uint32_t j, sz, k = arm64_hfa(&sym->type, &sz);
1069 if (sz < 16)
1070 for (j = 0; j < k; j++) {
1071 o(0x3d0003e0 | -(sz & 8) << 27 | (sz & 4) << 29 |
1072 ((a[i] - 16) / 2 + j) | (off / sz + j) << 10);
1073 // str ([sdq])(*),[sp,#(j * sz)]
1078 tcc_free(a);
1079 tcc_free(t);
1081 o(0x910003fd); // mov x29,sp
1082 arm64_func_sub_sp_offset = ind;
1083 // In gfunc_epilog these will be replaced with code to decrement SP:
1084 o(0xd503201f); // nop
1085 o(0xd503201f); // nop
1086 loc = 0;
1089 ST_FUNC void gen_va_start(void)
1091 int r;
1092 --vtop; // we don't need the "arg"
1093 gaddrof();
1094 r = intr(gv(RC_INT));
1096 if (arm64_func_va_list_stack) {
1097 //xx could use add (immediate) here
1098 arm64_movimm(30, arm64_func_va_list_stack + 224);
1099 o(0x8b1e03be); // add x30,x29,x30
1101 else
1102 o(0x910383be); // add x30,x29,#224
1103 o(0xf900001e | r << 5); // str x30,[x(r)]
1105 if (arm64_func_va_list_gr_offs) {
1106 if (arm64_func_va_list_stack)
1107 o(0x910383be); // add x30,x29,#224
1108 o(0xf900041e | r << 5); // str x30,[x(r),#8]
1111 if (arm64_func_va_list_vr_offs) {
1112 o(0x910243be); // add x30,x29,#144
1113 o(0xf900081e | r << 5); // str x30,[x(r),#16]
1116 arm64_movimm(30, arm64_func_va_list_gr_offs);
1117 o(0xb900181e | r << 5); // str w30,[x(r),#24]
1119 arm64_movimm(30, arm64_func_va_list_vr_offs);
1120 o(0xb9001c1e | r << 5); // str w30,[x(r),#28]
1122 --vtop;
1125 ST_FUNC void gen_va_arg(CType *t)
1127 int align, size = type_size(t, &align);
1128 int fsize, hfa = arm64_hfa(t, &fsize);
1129 uint32_t r0, r1;
1131 if (is_float(t->t)) {
1132 hfa = 1;
1133 fsize = size;
1136 gaddrof();
1137 r0 = intr(gv(RC_INT));
1138 r1 = get_reg(RC_INT);
1139 vtop[0].r = r1 | lvalue_type(t->t);
1140 r1 = intr(r1);
1142 if (!hfa) {
1143 uint32_t n = size > 16 ? 8 : (size + 7) & -8;
1144 o(0xb940181e | r0 << 5); // ldr w30,[x(r0),#24] // __gr_offs
1145 if (align == 16) {
1146 assert(0); // this path untested but needed for __uint128_t
1147 o(0x11003fde); // add w30,w30,#15
1148 o(0x121c6fde); // and w30,w30,#-16
1150 o(0x310003c0 | r1 | n << 10); // adds w(r1),w30,#(n)
1151 o(0x540000ad); // b.le .+20
1152 o(0xf9400000 | r1 | r0 << 5); // ldr x(r1),[x(r0)] // __stack
1153 o(0x9100001e | r1 << 5 | n << 10); // add x30,x(r1),#(n)
1154 o(0xf900001e | r0 << 5); // str x30,[x(r0)] // __stack
1155 o(0x14000004); // b .+16
1156 o(0xb9001800 | r1 | r0 << 5); // str w(r1),[x(r0),#24] // __gr_offs
1157 o(0xf9400400 | r1 | r0 << 5); // ldr x(r1),[x(r0),#8] // __gr_top
1158 o(0x8b3ec000 | r1 | r1 << 5); // add x(r1),x(r1),w30,sxtw
1159 if (size > 16)
1160 o(0xf9400000 | r1 | r1 << 5); // ldr x(r1),[x(r1)]
1162 else {
1163 uint32_t rsz = hfa << 4;
1164 uint32_t ssz = (size + 7) & -(uint32_t)8;
1165 uint32_t b1, b2;
1166 o(0xb9401c1e | r0 << 5); // ldr w30,[x(r0),#28] // __vr_offs
1167 o(0x310003c0 | r1 | rsz << 10); // adds w(r1),w30,#(rsz)
1168 b1 = ind; o(0x5400000d); // b.le lab1
1169 o(0xf9400000 | r1 | r0 << 5); // ldr x(r1),[x(r0)] // __stack
1170 if (fsize == 16) {
1171 o(0x91003c00 | r1 | r1 << 5); // add x(r1),x(r1),#15
1172 o(0x927cec00 | r1 | r1 << 5); // and x(r1),x(r1),#-16
1174 o(0x9100001e | r1 << 5 | ssz << 10); // add x30,x(r1),#(ssz)
1175 o(0xf900001e | r0 << 5); // str x30,[x(r0)] // __stack
1176 b2 = ind; o(0x14000000); // b lab2
1177 // lab1:
1178 write32le(cur_text_section->data + b1, 0x5400000d | (ind - b1) << 3);
1179 o(0xb9001c00 | r1 | r0 << 5); // str w(r1),[x(r0),#28] // __vr_offs
1180 o(0xf9400800 | r1 | r0 << 5); // ldr x(r1),[x(r0),#16] // __vr_top
1181 if (hfa == 1 || fsize == 16)
1182 o(0x8b3ec000 | r1 | r1 << 5); // add x(r1),x(r1),w30,sxtw
1183 else {
1184 // We need to change the layout of this HFA.
1185 // Get some space on the stack using global variable "loc":
1186 loc = (loc - size) & -(uint32_t)align;
1187 o(0x8b3ec000 | 30 | r1 << 5); // add x30,x(r1),w30,sxtw
1188 arm64_movimm(r1, loc);
1189 o(0x8b0003a0 | r1 | r1 << 16); // add x(r1),x29,x(r1)
1190 o(0x4c402bdc | (uint32_t)fsize << 7 |
1191 (uint32_t)(hfa == 2) << 15 |
1192 (uint32_t)(hfa == 3) << 14); // ld1 {v28.(4s|2d),...},[x30]
1193 o(0x0d00801c | r1 << 5 | (fsize == 8) << 10 |
1194 (uint32_t)(hfa != 2) << 13 |
1195 (uint32_t)(hfa != 3) << 21); // st(hfa) {v28.(s|d),...}[0],[x(r1)]
1197 // lab2:
1198 write32le(cur_text_section->data + b2, 0x14000000 | (ind - b2) >> 2);
1202 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret,
1203 int *align, int *regsize)
1205 return 0;
1208 ST_FUNC void greturn(void)
1210 CType *t = &func_vt;
1211 unsigned long a;
1213 arm64_pcs(0, &t, &a);
1214 switch (a) {
1215 case -1:
1216 break;
1217 case 0:
1218 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
1219 int align, size = type_size(&func_vt, &align);
1220 gaddrof();
1221 gv(RC_R(0));
1222 arm64_ldrs(0, size);
1224 else
1225 gv(RC_IRET);
1226 break;
1227 case 1: {
1228 CType type = func_vt;
1229 mk_pointer(&type);
1230 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
1231 indir();
1232 vswap();
1233 vstore();
1234 break;
1236 case 16:
1237 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
1238 uint32_t j, sz, n = arm64_hfa(&vtop->type, &sz);
1239 gaddrof();
1240 gv(RC_R(0));
1241 for (j = 0; j < n; j++)
1242 o(0x3d400000 |
1243 (sz & 16) << 19 | -(sz & 8) << 27 | (sz & 4) << 29 |
1244 j | j << 10); // ldr ([sdq])(*),[x0,#(j * sz)]
1246 else
1247 gv(RC_FRET);
1248 break;
1249 default:
1250 assert(0);
1254 ST_FUNC void gfunc_epilog(void)
1256 if (loc) {
1257 // Insert instructions to subtract size of stack frame from SP.
1258 unsigned char *ptr = cur_text_section->data + arm64_func_sub_sp_offset;
1259 uint64_t diff = (-loc + 15) & ~15;
1260 if (!(diff >> 24)) {
1261 if (diff & 0xfff) // sub sp,sp,#(diff & 0xfff)
1262 write32le(ptr, 0xd10003ff | (diff & 0xfff) << 10);
1263 if (diff >> 12) // sub sp,sp,#(diff >> 12),lsl #12
1264 write32le(ptr + 4, 0xd14003ff | (diff >> 12) << 10);
1266 else {
1267 // In this case we may subtract more than necessary,
1268 // but always less than 17/16 of what we were aiming for.
1269 int i = 0;
1270 int j = 0;
1271 while (diff >> 20) {
1272 diff = (diff + 0xffff) >> 16;
1273 ++i;
1275 while (diff >> 16) {
1276 diff = (diff + 1) >> 1;
1277 ++j;
1279 write32le(ptr, 0xd2800010 | diff << 5 | i << 21);
1280 // mov x16,#(diff),lsl #(16 * i)
1281 write32le(ptr + 4, 0xcb3063ff | j << 10);
1282 // sub sp,sp,x16,lsl #(j)
1285 o(0x910003bf); // mov sp,x29
1286 o(0xa8ce7bfd); // ldp x29,x30,[sp],#224
1288 o(0xd65f03c0); // ret
1291 // Generate forward branch to label:
1292 ST_FUNC int gjmp(int t)
1294 int r = ind;
1295 o(t);
1296 return r;
1299 // Generate branch to known address:
1300 ST_FUNC void gjmp_addr(int a)
1302 assert(a - ind + 0x8000000 < 0x10000000);
1303 o(0x14000000 | ((a - ind) >> 2 & 0x3ffffff));
1306 ST_FUNC int gtst(int inv, int t)
1308 int bt = vtop->type.t & VT_BTYPE;
1309 if (bt == VT_LDOUBLE) {
1310 uint32_t a, b, f = fltr(gv(RC_FLOAT));
1311 a = get_reg(RC_INT);
1312 vpushi(0);
1313 vtop[0].r = a;
1314 b = get_reg(RC_INT);
1315 a = intr(a);
1316 b = intr(b);
1317 o(0x4e083c00 | a | f << 5); // mov x(a),v(f).d[0]
1318 o(0x4e183c00 | b | f << 5); // mov x(b),v(f).d[1]
1319 o(0xaa000400 | a | a << 5 | b << 16); // orr x(a),x(a),x(b),lsl #1
1320 o(0xb4000040 | a | !!inv << 24); // cbz/cbnz x(a),.+8
1321 --vtop;
1323 else if (bt == VT_FLOAT || bt == VT_DOUBLE) {
1324 uint32_t a = fltr(gv(RC_FLOAT));
1325 o(0x1e202008 | a << 5 | (bt != VT_FLOAT) << 22); // fcmp
1326 o(0x54000040 | !!inv); // b.eq/b.ne .+8
1328 else {
1329 uint32_t ll = (bt == VT_PTR || bt == VT_LLONG);
1330 uint32_t a = intr(gv(RC_INT));
1331 o(0x34000040 | a | !!inv << 24 | ll << 31); // cbz/cbnz wA,.+8
1333 --vtop;
1334 return gjmp(t);
1337 static int arm64_iconst(uint64_t *val, SValue *sv)
1339 if ((sv->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
1340 return 0;
1341 if (val) {
1342 int t = sv->type.t;
1343 *val = ((t & VT_BTYPE) == VT_LLONG ? sv->c.i :
1344 (uint32_t)sv->c.i |
1345 (t & VT_UNSIGNED ? 0 : -(sv->c.i & 0x80000000)));
1347 return 1;
1350 static int arm64_gen_opic(int op, uint32_t l, int rev, uint64_t val,
1351 uint32_t x, uint32_t a)
1353 if (op == '-' && !rev) {
1354 val = -val;
1355 op = '+';
1357 val = l ? val : (uint32_t)val;
1359 switch (op) {
1361 case '+': {
1362 uint32_t s = l ? val >> 63 : val >> 31;
1363 val = s ? -val : val;
1364 val = l ? val : (uint32_t)val;
1365 if (!(val & ~(uint64_t)0xfff))
1366 o(0x11000000 | l << 31 | s << 30 | x | a << 5 | val << 10);
1367 else if (!(val & ~(uint64_t)0xfff000))
1368 o(0x11400000 | l << 31 | s << 30 | x | a << 5 | val >> 12 << 10);
1369 else {
1370 arm64_movimm(30, val); // use x30
1371 o(0x0b1e0000 | l << 31 | s << 30 | x | a << 5);
1373 return 1;
1376 case '-':
1377 if (!val)
1378 o(0x4b0003e0 | l << 31 | x | a << 16); // neg
1379 else if (val == (l ? (uint64_t)-1 : (uint32_t)-1))
1380 o(0x2a2003e0 | l << 31 | x | a << 16); // mvn
1381 else {
1382 arm64_movimm(30, val); // use x30
1383 o(0x4b0003c0 | l << 31 | x | a << 16); // sub
1385 return 1;
1387 case '^':
1388 if (val == -1 || (val == 0xffffffff && !l)) {
1389 o(0x2a2003e0 | l << 31 | x | a << 16); // mvn
1390 return 1;
1392 // fall through
1393 case '&':
1394 case '|': {
1395 int e = arm64_encode_bimm64(l ? val : val | val << 32);
1396 if (e < 0)
1397 return 0;
1398 o((op == '&' ? 0x12000000 :
1399 op == '|' ? 0x32000000 : 0x52000000) |
1400 l << 31 | x | a << 5 | (uint32_t)e << 10);
1401 return 1;
1404 case TOK_SAR:
1405 case TOK_SHL:
1406 case TOK_SHR: {
1407 uint32_t n = 32 << l;
1408 val = val & (n - 1);
1409 if (rev)
1410 return 0;
1411 if (!val)
1412 assert(0);
1413 else if (op == TOK_SHL)
1414 o(0x53000000 | l << 31 | l << 22 | x | a << 5 |
1415 (n - val) << 16 | (n - 1 - val) << 10); // lsl
1416 else
1417 o(0x13000000 | (op == TOK_SHR) << 30 | l << 31 | l << 22 |
1418 x | a << 5 | val << 16 | (n - 1) << 10); // lsr/asr
1419 return 1;
1423 return 0;
1426 static void arm64_gen_opil(int op, uint32_t l)
1428 uint32_t x, a, b;
1430 // Special treatment for operations with a constant operand:
1432 uint64_t val;
1433 int rev = 1;
1435 if (arm64_iconst(0, &vtop[0])) {
1436 vswap();
1437 rev = 0;
1439 if (arm64_iconst(&val, &vtop[-1])) {
1440 gv(RC_INT);
1441 a = intr(vtop[0].r);
1442 --vtop;
1443 x = get_reg(RC_INT);
1444 ++vtop;
1445 if (arm64_gen_opic(op, l, rev, val, intr(x), a)) {
1446 vtop[0].r = x;
1447 vswap();
1448 --vtop;
1449 return;
1452 if (!rev)
1453 vswap();
1456 gv2(RC_INT, RC_INT);
1457 assert(vtop[-1].r < VT_CONST && vtop[0].r < VT_CONST);
1458 a = intr(vtop[-1].r);
1459 b = intr(vtop[0].r);
1460 vtop -= 2;
1461 x = get_reg(RC_INT);
1462 ++vtop;
1463 vtop[0].r = x;
1464 x = intr(x);
1466 switch (op) {
1467 case '%':
1468 // Use x30 for quotient:
1469 o(0x1ac00c00 | l << 31 | 30 | a << 5 | b << 16); // sdiv
1470 o(0x1b008000 | l << 31 | x | (uint32_t)30 << 5 |
1471 b << 16 | a << 10); // msub
1472 break;
1473 case '&':
1474 o(0x0a000000 | l << 31 | x | a << 5 | b << 16); // and
1475 break;
1476 case '*':
1477 o(0x1b007c00 | l << 31 | x | a << 5 | b << 16); // mul
1478 break;
1479 case '+':
1480 o(0x0b000000 | l << 31 | x | a << 5 | b << 16); // add
1481 break;
1482 case '-':
1483 o(0x4b000000 | l << 31 | x | a << 5 | b << 16); // sub
1484 break;
1485 case '/':
1486 o(0x1ac00c00 | l << 31 | x | a << 5 | b << 16); // sdiv
1487 break;
1488 case '^':
1489 o(0x4a000000 | l << 31 | x | a << 5 | b << 16); // eor
1490 break;
1491 case '|':
1492 o(0x2a000000 | l << 31 | x | a << 5 | b << 16); // orr
1493 break;
1494 case TOK_EQ:
1495 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1496 o(0x1a9f17e0 | x); // cset wA,eq
1497 break;
1498 case TOK_GE:
1499 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1500 o(0x1a9fb7e0 | x); // cset wA,ge
1501 break;
1502 case TOK_GT:
1503 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1504 o(0x1a9fd7e0 | x); // cset wA,gt
1505 break;
1506 case TOK_LE:
1507 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1508 o(0x1a9fc7e0 | x); // cset wA,le
1509 break;
1510 case TOK_LT:
1511 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1512 o(0x1a9fa7e0 | x); // cset wA,lt
1513 break;
1514 case TOK_NE:
1515 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1516 o(0x1a9f07e0 | x); // cset wA,ne
1517 break;
1518 case TOK_SAR:
1519 o(0x1ac02800 | l << 31 | x | a << 5 | b << 16); // asr
1520 break;
1521 case TOK_SHL:
1522 o(0x1ac02000 | l << 31 | x | a << 5 | b << 16); // lsl
1523 break;
1524 case TOK_SHR:
1525 o(0x1ac02400 | l << 31 | x | a << 5 | b << 16); // lsr
1526 break;
1527 case TOK_UDIV:
1528 case TOK_PDIV:
1529 o(0x1ac00800 | l << 31 | x | a << 5 | b << 16); // udiv
1530 break;
1531 case TOK_UGE:
1532 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1533 o(0x1a9f37e0 | x); // cset wA,cs
1534 break;
1535 case TOK_UGT:
1536 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1537 o(0x1a9f97e0 | x); // cset wA,hi
1538 break;
1539 case TOK_ULT:
1540 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1541 o(0x1a9f27e0 | x); // cset wA,cc
1542 break;
1543 case TOK_ULE:
1544 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1545 o(0x1a9f87e0 | x); // cset wA,ls
1546 break;
1547 case TOK_UMOD:
1548 // Use x30 for quotient:
1549 o(0x1ac00800 | l << 31 | 30 | a << 5 | b << 16); // udiv
1550 o(0x1b008000 | l << 31 | x | (uint32_t)30 << 5 |
1551 b << 16 | a << 10); // msub
1552 break;
1553 default:
1554 assert(0);
1558 ST_FUNC void gen_opi(int op)
1560 arm64_gen_opil(op, 0);
1563 ST_FUNC void gen_opl(int op)
1565 arm64_gen_opil(op, 1);
1568 ST_FUNC void gen_opf(int op)
1570 uint32_t x, a, b, dbl;
1572 if (vtop[0].type.t == VT_LDOUBLE) {
1573 CType type = vtop[0].type;
1574 int func = 0;
1575 int cond = -1;
1576 switch (op) {
1577 case '*': func = TOK___multf3; break;
1578 case '+': func = TOK___addtf3; break;
1579 case '-': func = TOK___subtf3; break;
1580 case '/': func = TOK___divtf3; break;
1581 case TOK_EQ: func = TOK___eqtf2; cond = 1; break;
1582 case TOK_NE: func = TOK___netf2; cond = 0; break;
1583 case TOK_LT: func = TOK___lttf2; cond = 10; break;
1584 case TOK_GE: func = TOK___getf2; cond = 11; break;
1585 case TOK_LE: func = TOK___letf2; cond = 12; break;
1586 case TOK_GT: func = TOK___gttf2; cond = 13; break;
1587 default: assert(0); break;
1589 vpush_global_sym(&func_old_type, func);
1590 vrott(3);
1591 gfunc_call(2);
1592 vpushi(0);
1593 vtop->r = cond < 0 ? REG_FRET : REG_IRET;
1594 if (cond < 0)
1595 vtop->type = type;
1596 else {
1597 o(0x7100001f); // cmp w0,#0
1598 o(0x1a9f07e0 | (uint32_t)cond << 12); // cset w0,(cond)
1600 return;
1603 dbl = vtop[0].type.t != VT_FLOAT;
1604 gv2(RC_FLOAT, RC_FLOAT);
1605 assert(vtop[-1].r < VT_CONST && vtop[0].r < VT_CONST);
1606 a = fltr(vtop[-1].r);
1607 b = fltr(vtop[0].r);
1608 vtop -= 2;
1609 switch (op) {
1610 case TOK_EQ: case TOK_NE:
1611 case TOK_LT: case TOK_GE: case TOK_LE: case TOK_GT:
1612 x = get_reg(RC_INT);
1613 ++vtop;
1614 vtop[0].r = x;
1615 x = intr(x);
1616 break;
1617 default:
1618 x = get_reg(RC_FLOAT);
1619 ++vtop;
1620 vtop[0].r = x;
1621 x = fltr(x);
1622 break;
1625 switch (op) {
1626 case '*':
1627 o(0x1e200800 | dbl << 22 | x | a << 5 | b << 16); // fmul
1628 break;
1629 case '+':
1630 o(0x1e202800 | dbl << 22 | x | a << 5 | b << 16); // fadd
1631 break;
1632 case '-':
1633 o(0x1e203800 | dbl << 22 | x | a << 5 | b << 16); // fsub
1634 break;
1635 case '/':
1636 o(0x1e201800 | dbl << 22 | x | a << 5 | b << 16); // fdiv
1637 break;
1638 case TOK_EQ:
1639 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1640 o(0x1a9f17e0 | x); // cset w(x),eq
1641 break;
1642 case TOK_GE:
1643 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1644 o(0x1a9fb7e0 | x); // cset w(x),ge
1645 break;
1646 case TOK_GT:
1647 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1648 o(0x1a9fd7e0 | x); // cset w(x),gt
1649 break;
1650 case TOK_LE:
1651 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1652 o(0x1a9f87e0 | x); // cset w(x),ls
1653 break;
1654 case TOK_LT:
1655 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1656 o(0x1a9f57e0 | x); // cset w(x),mi
1657 break;
1658 case TOK_NE:
1659 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1660 o(0x1a9f07e0 | x); // cset w(x),ne
1661 break;
1662 default:
1663 assert(0);
1667 // Generate sign extension from 32 to 64 bits:
1668 ST_FUNC void gen_cvt_sxtw(void)
1670 uint32_t r = intr(gv(RC_INT));
1671 o(0x93407c00 | r | r << 5); // sxtw x(r),w(r)
1674 ST_FUNC void gen_cvt_itof(int t)
1676 if (t == VT_LDOUBLE) {
1677 int f = vtop->type.t;
1678 int func = (f & VT_BTYPE) == VT_LLONG ?
1679 (f & VT_UNSIGNED ? TOK___floatunditf : TOK___floatditf) :
1680 (f & VT_UNSIGNED ? TOK___floatunsitf : TOK___floatsitf);
1681 vpush_global_sym(&func_old_type, func);
1682 vrott(2);
1683 gfunc_call(1);
1684 vpushi(0);
1685 vtop->type.t = t;
1686 vtop->r = REG_FRET;
1687 return;
1689 else {
1690 int d, n = intr(gv(RC_INT));
1691 int s = !(vtop->type.t & VT_UNSIGNED);
1692 uint32_t l = ((vtop->type.t & VT_BTYPE) == VT_LLONG);
1693 --vtop;
1694 d = get_reg(RC_FLOAT);
1695 ++vtop;
1696 vtop[0].r = d;
1697 o(0x1e220000 | (uint32_t)!s << 16 |
1698 (uint32_t)(t != VT_FLOAT) << 22 | fltr(d) |
1699 l << 31 | n << 5); // [us]cvtf [sd](d),[wx](n)
1703 ST_FUNC void gen_cvt_ftoi(int t)
1705 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
1706 int func = (t & VT_BTYPE) == VT_LLONG ?
1707 (t & VT_UNSIGNED ? TOK___fixunstfdi : TOK___fixtfdi) :
1708 (t & VT_UNSIGNED ? TOK___fixunstfsi : TOK___fixtfsi);
1709 vpush_global_sym(&func_old_type, func);
1710 vrott(2);
1711 gfunc_call(1);
1712 vpushi(0);
1713 vtop->type.t = t;
1714 vtop->r = REG_IRET;
1715 return;
1717 else {
1718 int d, n = fltr(gv(RC_FLOAT));
1719 uint32_t l = ((vtop->type.t & VT_BTYPE) != VT_FLOAT);
1720 --vtop;
1721 d = get_reg(RC_INT);
1722 ++vtop;
1723 vtop[0].r = d;
1724 o(0x1e380000 |
1725 (uint32_t)!!(t & VT_UNSIGNED) << 16 |
1726 (uint32_t)((t & VT_BTYPE) == VT_LLONG) << 31 | intr(d) |
1727 l << 22 | n << 5); // fcvtz[su] [wx](d),[sd](n)
1731 ST_FUNC void gen_cvt_ftof(int t)
1733 int f = vtop[0].type.t;
1734 assert(t == VT_FLOAT || t == VT_DOUBLE || t == VT_LDOUBLE);
1735 assert(f == VT_FLOAT || f == VT_DOUBLE || f == VT_LDOUBLE);
1736 if (t == f)
1737 return;
1739 if (t == VT_LDOUBLE || f == VT_LDOUBLE) {
1740 int func = (t == VT_LDOUBLE) ?
1741 (f == VT_FLOAT ? TOK___extendsftf2 : TOK___extenddftf2) :
1742 (t == VT_FLOAT ? TOK___trunctfsf2 : TOK___trunctfdf2);
1743 vpush_global_sym(&func_old_type, func);
1744 vrott(2);
1745 gfunc_call(1);
1746 vpushi(0);
1747 vtop->type.t = t;
1748 vtop->r = REG_FRET;
1750 else {
1751 int x, a;
1752 gv(RC_FLOAT);
1753 assert(vtop[0].r < VT_CONST);
1754 a = fltr(vtop[0].r);
1755 --vtop;
1756 x = get_reg(RC_FLOAT);
1757 ++vtop;
1758 vtop[0].r = x;
1759 x = fltr(x);
1761 if (f == VT_FLOAT)
1762 o(0x1e22c000 | x | a << 5); // fcvt d(x),s(a)
1763 else
1764 o(0x1e624000 | x | a << 5); // fcvt s(x),d(a)
1768 ST_FUNC void ggoto(void)
1770 arm64_gen_bl_or_b(1);
1771 --vtop;
1774 ST_FUNC void gen_clear_cache(void)
1776 uint32_t beg, end, dsz, isz, p, lab1, b1;
1777 gv2(RC_INT, RC_INT);
1778 vpushi(0);
1779 vtop->r = get_reg(RC_INT);
1780 vpushi(0);
1781 vtop->r = get_reg(RC_INT);
1782 vpushi(0);
1783 vtop->r = get_reg(RC_INT);
1784 beg = intr(vtop[-4].r); // x0
1785 end = intr(vtop[-3].r); // x1
1786 dsz = intr(vtop[-2].r); // x2
1787 isz = intr(vtop[-1].r); // x3
1788 p = intr(vtop[0].r); // x4
1789 vtop -= 5;
1791 o(0xd53b0020 | isz); // mrs x(isz),ctr_el0
1792 o(0x52800080 | p); // mov w(p),#4
1793 o(0x53104c00 | dsz | isz << 5); // ubfx w(dsz),w(isz),#16,#4
1794 o(0x1ac02000 | dsz | p << 5 | dsz << 16); // lsl w(dsz),w(p),w(dsz)
1795 o(0x12000c00 | isz | isz << 5); // and w(isz),w(isz),#15
1796 o(0x1ac02000 | isz | p << 5 | isz << 16); // lsl w(isz),w(p),w(isz)
1797 o(0x51000400 | p | dsz << 5); // sub w(p),w(dsz),#1
1798 o(0x8a240004 | p | beg << 5 | p << 16); // bic x(p),x(beg),x(p)
1799 b1 = ind; o(0x14000000); // b
1800 lab1 = ind;
1801 o(0xd50b7b20 | p); // dc cvau,x(p)
1802 o(0x8b000000 | p | p << 5 | dsz << 16); // add x(p),x(p),x(dsz)
1803 write32le(cur_text_section->data + b1, 0x14000000 | (ind - b1) >> 2);
1804 o(0xeb00001f | p << 5 | end << 16); // cmp x(p),x(end)
1805 o(0x54ffffa3 | ((lab1 - ind) << 3 & 0xffffe0)); // b.cc lab1
1806 o(0xd5033b9f); // dsb ish
1807 o(0x51000400 | p | isz << 5); // sub w(p),w(isz),#1
1808 o(0x8a240004 | p | beg << 5 | p << 16); // bic x(p),x(beg),x(p)
1809 b1 = ind; o(0x14000000); // b
1810 lab1 = ind;
1811 o(0xd50b7520 | p); // ic ivau,x(p)
1812 o(0x8b000000 | p | p << 5 | isz << 16); // add x(p),x(p),x(isz)
1813 write32le(cur_text_section->data + b1, 0x14000000 | (ind - b1) >> 2);
1814 o(0xeb00001f | p << 5 | end << 16); // cmp x(p),x(end)
1815 o(0x54ffffa3 | ((lab1 - ind) << 3 & 0xffffe0)); // b.cc lab1
1816 o(0xd5033b9f); // dsb ish
1817 o(0xd5033fdf); // isb
1820 ST_FUNC void gen_vla_sp_save(int addr) {
1821 uint32_t r = intr(get_reg(RC_INT));
1822 o(0x910003e0 | r); // mov x(r),sp
1823 arm64_strx(3, r, 29, addr);
1826 ST_FUNC void gen_vla_sp_restore(int addr) {
1827 // Use x30 because this function can be called when there
1828 // is a live return value in x0 but there is nothing on
1829 // the value stack to prevent get_reg from returning x0.
1830 uint32_t r = 30;
1831 arm64_ldrx(0, 3, r, 29, addr);
1832 o(0x9100001f | r << 5); // mov sp,x(r)
1835 ST_FUNC void gen_vla_alloc(CType *type, int align) {
1836 uint32_t r = intr(gv(RC_INT));
1837 o(0x91003c00 | r | r << 5); // add x(r),x(r),#15
1838 o(0x927cec00 | r | r << 5); // bic x(r),x(r),#15
1839 o(0xcb2063ff | r << 16); // sub sp,sp,x(r)
1840 vpop();
1843 /* end of A64 code generator */
1844 /*************************************************************/
1845 #endif
1846 /*************************************************************/