riscv64: adjust for cast changes
[tinycc.git] / arm64-gen.c
blob5ea47db488138457c8d15adbc48af9e792575a4b
1 /*
2 * A64 code generator for TCC
4 * Copyright (c) 2014-2015 Edmund Grimley Evans
6 * Copying and distribution of this file, with or without modification,
7 * are permitted in any medium without royalty provided the copyright
8 * notice and this notice are preserved. This file is offered as-is,
9 * without any warranty.
12 #ifdef TARGET_DEFS_ONLY
14 // Number of registers available to allocator:
15 #define NB_REGS 28 // x0-x18, x30, v0-v7
17 #define TREG_R(x) (x) // x = 0..18
18 #define TREG_R30 19
19 #define TREG_F(x) (x + 20) // x = 0..7
21 // Register classes sorted from more general to more precise:
22 #define RC_INT (1 << 0)
23 #define RC_FLOAT (1 << 1)
24 #define RC_R(x) (1 << (2 + (x))) // x = 0..18
25 #define RC_R30 (1 << 21)
26 #define RC_F(x) (1 << (22 + (x))) // x = 0..7
28 #define RC_IRET (RC_R(0)) // int return register class
29 #define RC_FRET (RC_F(0)) // float return register class
31 #define REG_IRET (TREG_R(0)) // int return register number
32 #define REG_FRET (TREG_F(0)) // float return register number
34 #define PTR_SIZE 8
36 #define LDOUBLE_SIZE 16
37 #define LDOUBLE_ALIGN 16
39 #define MAX_ALIGN 16
41 #define CHAR_IS_UNSIGNED
43 /******************************************************/
44 #else /* ! TARGET_DEFS_ONLY */
45 /******************************************************/
46 #define USING_GLOBALS
47 #include "tcc.h"
48 #include <assert.h>
50 ST_DATA const int reg_classes[NB_REGS] = {
51 RC_INT | RC_R(0),
52 RC_INT | RC_R(1),
53 RC_INT | RC_R(2),
54 RC_INT | RC_R(3),
55 RC_INT | RC_R(4),
56 RC_INT | RC_R(5),
57 RC_INT | RC_R(6),
58 RC_INT | RC_R(7),
59 RC_INT | RC_R(8),
60 RC_INT | RC_R(9),
61 RC_INT | RC_R(10),
62 RC_INT | RC_R(11),
63 RC_INT | RC_R(12),
64 RC_INT | RC_R(13),
65 RC_INT | RC_R(14),
66 RC_INT | RC_R(15),
67 RC_INT | RC_R(16),
68 RC_INT | RC_R(17),
69 RC_INT | RC_R(18),
70 RC_R30, // not in RC_INT as we make special use of x30
71 RC_FLOAT | RC_F(0),
72 RC_FLOAT | RC_F(1),
73 RC_FLOAT | RC_F(2),
74 RC_FLOAT | RC_F(3),
75 RC_FLOAT | RC_F(4),
76 RC_FLOAT | RC_F(5),
77 RC_FLOAT | RC_F(6),
78 RC_FLOAT | RC_F(7)
81 #define IS_FREG(x) ((x) >= TREG_F(0))
83 static uint32_t intr(int r)
85 assert(TREG_R(0) <= r && r <= TREG_R30);
86 return r < TREG_R30 ? r : 30;
89 static uint32_t fltr(int r)
91 assert(TREG_F(0) <= r && r <= TREG_F(7));
92 return r - TREG_F(0);
95 // Add an instruction to text section:
96 ST_FUNC void o(unsigned int c)
98 int ind1 = ind + 4;
99 if (nocode_wanted)
100 return;
101 if (ind1 > cur_text_section->data_allocated)
102 section_realloc(cur_text_section, ind1);
103 write32le(cur_text_section->data + ind, c);
104 ind = ind1;
107 static int arm64_encode_bimm64(uint64_t x)
109 int neg = x & 1;
110 int rep, pos, len;
112 if (neg)
113 x = ~x;
114 if (!x)
115 return -1;
117 if (x >> 2 == (x & (((uint64_t)1 << (64 - 2)) - 1)))
118 rep = 2, x &= ((uint64_t)1 << 2) - 1;
119 else if (x >> 4 == (x & (((uint64_t)1 << (64 - 4)) - 1)))
120 rep = 4, x &= ((uint64_t)1 << 4) - 1;
121 else if (x >> 8 == (x & (((uint64_t)1 << (64 - 8)) - 1)))
122 rep = 8, x &= ((uint64_t)1 << 8) - 1;
123 else if (x >> 16 == (x & (((uint64_t)1 << (64 - 16)) - 1)))
124 rep = 16, x &= ((uint64_t)1 << 16) - 1;
125 else if (x >> 32 == (x & (((uint64_t)1 << (64 - 32)) - 1)))
126 rep = 32, x &= ((uint64_t)1 << 32) - 1;
127 else
128 rep = 64;
130 pos = 0;
131 if (!(x & (((uint64_t)1 << 32) - 1))) x >>= 32, pos += 32;
132 if (!(x & (((uint64_t)1 << 16) - 1))) x >>= 16, pos += 16;
133 if (!(x & (((uint64_t)1 << 8) - 1))) x >>= 8, pos += 8;
134 if (!(x & (((uint64_t)1 << 4) - 1))) x >>= 4, pos += 4;
135 if (!(x & (((uint64_t)1 << 2) - 1))) x >>= 2, pos += 2;
136 if (!(x & (((uint64_t)1 << 1) - 1))) x >>= 1, pos += 1;
138 len = 0;
139 if (!(~x & (((uint64_t)1 << 32) - 1))) x >>= 32, len += 32;
140 if (!(~x & (((uint64_t)1 << 16) - 1))) x >>= 16, len += 16;
141 if (!(~x & (((uint64_t)1 << 8) - 1))) x >>= 8, len += 8;
142 if (!(~x & (((uint64_t)1 << 4) - 1))) x >>= 4, len += 4;
143 if (!(~x & (((uint64_t)1 << 2) - 1))) x >>= 2, len += 2;
144 if (!(~x & (((uint64_t)1 << 1) - 1))) x >>= 1, len += 1;
146 if (x)
147 return -1;
148 if (neg) {
149 pos = (pos + len) & (rep - 1);
150 len = rep - len;
152 return ((0x1000 & rep << 6) | (((rep - 1) ^ 31) << 1 & 63) |
153 ((rep - pos) & (rep - 1)) << 6 | (len - 1));
156 static uint32_t arm64_movi(int r, uint64_t x)
158 uint64_t m = 0xffff;
159 int e;
160 if (!(x & ~m))
161 return 0x52800000 | r | x << 5; // movz w(r),#(x)
162 if (!(x & ~(m << 16)))
163 return 0x52a00000 | r | x >> 11; // movz w(r),#(x >> 16),lsl #16
164 if (!(x & ~(m << 32)))
165 return 0xd2c00000 | r | x >> 27; // movz x(r),#(x >> 32),lsl #32
166 if (!(x & ~(m << 48)))
167 return 0xd2e00000 | r | x >> 43; // movz x(r),#(x >> 48),lsl #48
168 if ((x & ~m) == m << 16)
169 return (0x12800000 | r |
170 (~x << 5 & 0x1fffe0)); // movn w(r),#(~x)
171 if ((x & ~(m << 16)) == m)
172 return (0x12a00000 | r |
173 (~x >> 11 & 0x1fffe0)); // movn w(r),#(~x >> 16),lsl #16
174 if (!~(x | m))
175 return (0x92800000 | r |
176 (~x << 5 & 0x1fffe0)); // movn x(r),#(~x)
177 if (!~(x | m << 16))
178 return (0x92a00000 | r |
179 (~x >> 11 & 0x1fffe0)); // movn x(r),#(~x >> 16),lsl #16
180 if (!~(x | m << 32))
181 return (0x92c00000 | r |
182 (~x >> 27 & 0x1fffe0)); // movn x(r),#(~x >> 32),lsl #32
183 if (!~(x | m << 48))
184 return (0x92e00000 | r |
185 (~x >> 43 & 0x1fffe0)); // movn x(r),#(~x >> 32),lsl #32
186 if (!(x >> 32) && (e = arm64_encode_bimm64(x | x << 32)) >= 0)
187 return 0x320003e0 | r | (uint32_t)e << 10; // movi w(r),#(x)
188 if ((e = arm64_encode_bimm64(x)) >= 0)
189 return 0xb20003e0 | r | (uint32_t)e << 10; // movi x(r),#(x)
190 return 0;
193 static void arm64_movimm(int r, uint64_t x)
195 uint32_t i;
196 if ((i = arm64_movi(r, x)))
197 o(i); // a single MOV
198 else {
199 // MOVZ/MOVN and 1-3 MOVKs
200 int z = 0, m = 0;
201 uint32_t mov1 = 0xd2800000; // movz
202 uint64_t x1 = x;
203 for (i = 0; i < 64; i += 16) {
204 z += !(x >> i & 0xffff);
205 m += !(~x >> i & 0xffff);
207 if (m > z) {
208 x1 = ~x;
209 mov1 = 0x92800000; // movn
211 for (i = 0; i < 64; i += 16)
212 if (x1 >> i & 0xffff) {
213 o(mov1 | r | (x1 >> i & 0xffff) << 5 | i << 17);
214 // movz/movn x(r),#(*),lsl #(i)
215 break;
217 for (i += 16; i < 64; i += 16)
218 if (x1 >> i & 0xffff)
219 o(0xf2800000 | r | (x >> i & 0xffff) << 5 | i << 17);
220 // movk x(r),#(*),lsl #(i)
224 // Patch all branches in list pointed to by t to branch to a:
225 ST_FUNC void gsym_addr(int t_, int a_)
227 uint32_t t = t_;
228 uint32_t a = a_;
229 while (t) {
230 unsigned char *ptr = cur_text_section->data + t;
231 uint32_t next = read32le(ptr);
232 if (a - t + 0x8000000 >= 0x10000000)
233 tcc_error("branch out of range");
234 write32le(ptr, (a - t == 4 ? 0xd503201f : // nop
235 0x14000000 | ((a - t) >> 2 & 0x3ffffff))); // b
236 t = next;
240 static int arm64_type_size(int t)
242 switch (t & VT_BTYPE) {
243 case VT_INT: return 2;
244 case VT_BYTE: return 0;
245 case VT_SHORT: return 1;
246 case VT_PTR: return 3;
247 case VT_FUNC: return 3;
248 case VT_FLOAT: return 2;
249 case VT_DOUBLE: return 3;
250 case VT_LDOUBLE: return 4;
251 case VT_BOOL: return 0;
252 case VT_LLONG: return 3;
254 assert(0);
255 return 0;
258 static void arm64_spoff(int reg, uint64_t off)
260 uint32_t sub = off >> 63;
261 if (sub)
262 off = -off;
263 if (off < 4096)
264 o(0x910003e0 | sub << 30 | reg | off << 10);
265 // (add|sub) x(reg),sp,#(off)
266 else {
267 arm64_movimm(30, off); // use x30 for offset
268 o(0x8b3e63e0 | sub << 30 | reg); // (add|sub) x(reg),sp,x30
272 static void arm64_ldrx(int sg, int sz_, int dst, int bas, uint64_t off)
274 uint32_t sz = sz_;
275 if (sz >= 2)
276 sg = 0;
277 if (!(off & ~((uint32_t)0xfff << sz)))
278 o(0x39400000 | dst | bas << 5 | off << (10 - sz) |
279 (uint32_t)!!sg << 23 | sz << 30); // ldr(*) x(dst),[x(bas),#(off)]
280 else if (off < 256 || -off <= 256)
281 o(0x38400000 | dst | bas << 5 | (off & 511) << 12 |
282 (uint32_t)!!sg << 23 | sz << 30); // ldur(*) x(dst),[x(bas),#(off)]
283 else {
284 arm64_movimm(30, off); // use x30 for offset
285 o(0x38206800 | dst | bas << 5 | (uint32_t)30 << 16 |
286 (uint32_t)(!!sg + 1) << 22 | sz << 30); // ldr(*) x(dst),[x(bas),x30]
290 static void arm64_ldrv(int sz_, int dst, int bas, uint64_t off)
292 uint32_t sz = sz_;
293 if (!(off & ~((uint32_t)0xfff << sz)))
294 o(0x3d400000 | dst | bas << 5 | off << (10 - sz) |
295 (sz & 4) << 21 | (sz & 3) << 30); // ldr (s|d|q)(dst),[x(bas),#(off)]
296 else if (off < 256 || -off <= 256)
297 o(0x3c400000 | dst | bas << 5 | (off & 511) << 12 |
298 (sz & 4) << 21 | (sz & 3) << 30); // ldur (s|d|q)(dst),[x(bas),#(off)]
299 else {
300 arm64_movimm(30, off); // use x30 for offset
301 o(0x3c606800 | dst | bas << 5 | (uint32_t)30 << 16 |
302 sz << 30 | (sz & 4) << 21); // ldr (s|d|q)(dst),[x(bas),x30]
306 static void arm64_ldrs(int reg_, int size)
308 uint32_t reg = reg_;
309 // Use x30 for intermediate value in some cases.
310 switch (size) {
311 default: assert(0); break;
312 case 1:
313 arm64_ldrx(0, 0, reg, reg, 0);
314 break;
315 case 2:
316 arm64_ldrx(0, 1, reg, reg, 0);
317 break;
318 case 3:
319 arm64_ldrx(0, 1, 30, reg, 0);
320 arm64_ldrx(0, 0, reg, reg, 2);
321 o(0x2a0043c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #16
322 break;
323 case 4:
324 arm64_ldrx(0, 2, reg, reg, 0);
325 break;
326 case 5:
327 arm64_ldrx(0, 2, 30, reg, 0);
328 arm64_ldrx(0, 0, reg, reg, 4);
329 o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32
330 break;
331 case 6:
332 arm64_ldrx(0, 2, 30, reg, 0);
333 arm64_ldrx(0, 1, reg, reg, 4);
334 o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32
335 break;
336 case 7:
337 arm64_ldrx(0, 2, 30, reg, 0);
338 arm64_ldrx(0, 2, reg, reg, 3);
339 o(0x53087c00 | reg | reg << 5); // lsr w(reg), w(reg), #8
340 o(0xaa0083c0 | reg | reg << 16); // orr x(reg),x30,x(reg),lsl #32
341 break;
342 case 8:
343 arm64_ldrx(0, 3, reg, reg, 0);
344 break;
345 case 9:
346 arm64_ldrx(0, 0, reg + 1, reg, 8);
347 arm64_ldrx(0, 3, reg, reg, 0);
348 break;
349 case 10:
350 arm64_ldrx(0, 1, reg + 1, reg, 8);
351 arm64_ldrx(0, 3, reg, reg, 0);
352 break;
353 case 11:
354 arm64_ldrx(0, 2, reg + 1, reg, 7);
355 o(0x53087c00 | (reg+1) | (reg+1) << 5); // lsr w(reg+1), w(reg+1), #8
356 arm64_ldrx(0, 3, reg, reg, 0);
357 break;
358 case 12:
359 arm64_ldrx(0, 2, reg + 1, reg, 8);
360 arm64_ldrx(0, 3, reg, reg, 0);
361 break;
362 case 13:
363 arm64_ldrx(0, 3, reg + 1, reg, 5);
364 o(0xd358fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #24
365 arm64_ldrx(0, 3, reg, reg, 0);
366 break;
367 case 14:
368 arm64_ldrx(0, 3, reg + 1, reg, 6);
369 o(0xd350fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #16
370 arm64_ldrx(0, 3, reg, reg, 0);
371 break;
372 case 15:
373 arm64_ldrx(0, 3, reg + 1, reg, 7);
374 o(0xd348fc00 | (reg+1) | (reg+1) << 5); // lsr x(reg+1), x(reg+1), #8
375 arm64_ldrx(0, 3, reg, reg, 0);
376 break;
377 case 16:
378 o(0xa9400000 | reg | (reg+1) << 10 | reg << 5);
379 // ldp x(reg),x(reg+1),[x(reg)]
380 break;
384 static void arm64_strx(int sz_, int dst, int bas, uint64_t off)
386 uint32_t sz = sz_;
387 if (!(off & ~((uint32_t)0xfff << sz)))
388 o(0x39000000 | dst | bas << 5 | off << (10 - sz) | sz << 30);
389 // str(*) x(dst),[x(bas],#(off)]
390 else if (off < 256 || -off <= 256)
391 o(0x38000000 | dst | bas << 5 | (off & 511) << 12 | sz << 30);
392 // stur(*) x(dst),[x(bas],#(off)]
393 else {
394 arm64_movimm(30, off); // use x30 for offset
395 o(0x38206800 | dst | bas << 5 | (uint32_t)30 << 16 | sz << 30);
396 // str(*) x(dst),[x(bas),x30]
400 static void arm64_strv(int sz_, int dst, int bas, uint64_t off)
402 uint32_t sz = sz_;
403 if (!(off & ~((uint32_t)0xfff << sz)))
404 o(0x3d000000 | dst | bas << 5 | off << (10 - sz) |
405 (sz & 4) << 21 | (sz & 3) << 30); // str (s|d|q)(dst),[x(bas),#(off)]
406 else if (off < 256 || -off <= 256)
407 o(0x3c000000 | dst | bas << 5 | (off & 511) << 12 |
408 (sz & 4) << 21 | (sz & 3) << 30); // stur (s|d|q)(dst),[x(bas),#(off)]
409 else {
410 arm64_movimm(30, off); // use x30 for offset
411 o(0x3c206800 | dst | bas << 5 | (uint32_t)30 << 16 |
412 sz << 30 | (sz & 4) << 21); // str (s|d|q)(dst),[x(bas),x30]
416 static void arm64_sym(int r, Sym *sym, unsigned long addend)
418 // Currently TCC's linker does not generate COPY relocations for
419 // STT_OBJECTs when tcc is invoked with "-run". This typically
420 // results in "R_AARCH64_ADR_PREL_PG_HI21 relocation failed" when
421 // a program refers to stdin. A workaround is to avoid that
422 // relocation and use only relocations with unlimited range.
423 int avoid_adrp = 1;
425 if (avoid_adrp || sym->a.weak) {
426 // (GCC uses a R_AARCH64_ABS64 in this case.)
427 greloca(cur_text_section, sym, ind, R_AARCH64_MOVW_UABS_G0_NC, addend);
428 o(0xd2800000 | r); // mov x(rt),#0,lsl #0
429 greloca(cur_text_section, sym, ind, R_AARCH64_MOVW_UABS_G1_NC, addend);
430 o(0xf2a00000 | r); // movk x(rt),#0,lsl #16
431 greloca(cur_text_section, sym, ind, R_AARCH64_MOVW_UABS_G2_NC, addend);
432 o(0xf2c00000 | r); // movk x(rt),#0,lsl #32
433 greloca(cur_text_section, sym, ind, R_AARCH64_MOVW_UABS_G3, addend);
434 o(0xf2e00000 | r); // movk x(rt),#0,lsl #48
436 else {
437 greloca(cur_text_section, sym, ind, R_AARCH64_ADR_PREL_PG_HI21, addend);
438 o(0x90000000 | r);
439 greloca(cur_text_section, sym, ind, R_AARCH64_ADD_ABS_LO12_NC, addend);
440 o(0x91000000 | r | r << 5);
444 static void arm64_load_cmp(int r, SValue *sv);
446 ST_FUNC void load(int r, SValue *sv)
448 int svtt = sv->type.t;
449 int svr = sv->r;
450 int svrv = svr & VT_VALMASK;
451 uint64_t svcul = (uint32_t)sv->c.i;
452 svcul = svcul >> 31 & 1 ? svcul - ((uint64_t)1 << 32) : svcul;
454 if (svr == (VT_LOCAL | VT_LVAL)) {
455 if (IS_FREG(r))
456 arm64_ldrv(arm64_type_size(svtt), fltr(r), 29, svcul);
457 else
458 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
459 intr(r), 29, svcul);
460 return;
463 if ((svr & ~VT_VALMASK) == VT_LVAL && svrv < VT_CONST) {
464 if (IS_FREG(r))
465 arm64_ldrv(arm64_type_size(svtt), fltr(r), intr(svrv), 0);
466 else
467 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
468 intr(r), intr(svrv), 0);
469 return;
472 if (svr == (VT_CONST | VT_LVAL | VT_SYM)) {
473 arm64_sym(30, sv->sym, svcul); // use x30 for address
474 if (IS_FREG(r))
475 arm64_ldrv(arm64_type_size(svtt), fltr(r), 30, 0);
476 else
477 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
478 intr(r), 30, 0);
479 return;
482 if (svr == (VT_CONST | VT_SYM)) {
483 arm64_sym(intr(r), sv->sym, svcul);
484 return;
487 if (svr == VT_CONST) {
488 if ((svtt & VT_BTYPE) != VT_VOID)
489 arm64_movimm(intr(r), arm64_type_size(svtt) == 3 ?
490 sv->c.i : (uint32_t)svcul);
491 return;
494 if (svr < VT_CONST) {
495 if (IS_FREG(r) && IS_FREG(svr))
496 if (svtt == VT_LDOUBLE)
497 o(0x4ea01c00 | fltr(r) | fltr(svr) << 5);
498 // mov v(r).16b,v(svr).16b
499 else
500 o(0x1e604000 | fltr(r) | fltr(svr) << 5); // fmov d(r),d(svr)
501 else if (!IS_FREG(r) && !IS_FREG(svr))
502 o(0xaa0003e0 | intr(r) | intr(svr) << 16); // mov x(r),x(svr)
503 else
504 assert(0);
505 return;
508 if (svr == VT_LOCAL) {
509 if (-svcul < 0x1000)
510 o(0xd10003a0 | intr(r) | -svcul << 10); // sub x(r),x29,#...
511 else {
512 arm64_movimm(30, -svcul); // use x30 for offset
513 o(0xcb0003a0 | intr(r) | (uint32_t)30 << 16); // sub x(r),x29,x30
515 return;
518 if (svr == VT_JMP || svr == VT_JMPI) {
519 int t = (svr == VT_JMPI);
520 arm64_movimm(intr(r), t);
521 o(0x14000002); // b .+8
522 gsym(svcul);
523 arm64_movimm(intr(r), t ^ 1);
524 return;
527 if (svr == (VT_LLOCAL | VT_LVAL)) {
528 arm64_ldrx(0, 3, 30, 29, svcul); // use x30 for offset
529 if (IS_FREG(r))
530 arm64_ldrv(arm64_type_size(svtt), fltr(r), 30, 0);
531 else
532 arm64_ldrx(!(svtt & VT_UNSIGNED), arm64_type_size(svtt),
533 intr(r), 30, 0);
534 return;
537 if (svr == VT_CMP) {
538 arm64_load_cmp(r, sv);
539 return;
542 printf("load(%x, (%x, %x, %llx))\n", r, svtt, sv->r, (long long)svcul);
543 assert(0);
546 ST_FUNC void store(int r, SValue *sv)
548 int svtt = sv->type.t;
549 int svr = sv->r;
550 int svrv = svr & VT_VALMASK;
551 uint64_t svcul = (uint32_t)sv->c.i;
552 svcul = svcul >> 31 & 1 ? svcul - ((uint64_t)1 << 32) : svcul;
554 if (svr == (VT_LOCAL | VT_LVAL)) {
555 if (IS_FREG(r))
556 arm64_strv(arm64_type_size(svtt), fltr(r), 29, svcul);
557 else
558 arm64_strx(arm64_type_size(svtt), intr(r), 29, svcul);
559 return;
562 if ((svr & ~VT_VALMASK) == VT_LVAL && svrv < VT_CONST) {
563 if (IS_FREG(r))
564 arm64_strv(arm64_type_size(svtt), fltr(r), intr(svrv), 0);
565 else
566 arm64_strx(arm64_type_size(svtt), intr(r), intr(svrv), 0);
567 return;
570 if (svr == (VT_CONST | VT_LVAL | VT_SYM)) {
571 arm64_sym(30, sv->sym, svcul); // use x30 for address
572 if (IS_FREG(r))
573 arm64_strv(arm64_type_size(svtt), fltr(r), 30, 0);
574 else
575 arm64_strx(arm64_type_size(svtt), intr(r), 30, 0);
576 return;
579 printf("store(%x, (%x, %x, %llx))\n", r, svtt, sv->r, (long long)svcul);
580 assert(0);
583 static void arm64_gen_bl_or_b(int b)
585 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST && (vtop->r & VT_SYM)) {
586 assert(!b);
587 greloca(cur_text_section, vtop->sym, ind, R_AARCH64_CALL26, 0);
588 o(0x94000000); // bl .
590 else
591 o(0xd61f0000 | (uint32_t)!b << 21 | intr(gv(RC_R30)) << 5); // br/blr
594 static int arm64_hfa_aux(CType *type, int *fsize, int num)
596 if (is_float(type->t)) {
597 int a, n = type_size(type, &a);
598 if (num >= 4 || (*fsize && *fsize != n))
599 return -1;
600 *fsize = n;
601 return num + 1;
603 else if ((type->t & VT_BTYPE) == VT_STRUCT) {
604 int is_struct = 0; // rather than union
605 Sym *field;
606 for (field = type->ref->next; field; field = field->next)
607 if (field->c) {
608 is_struct = 1;
609 break;
611 if (is_struct) {
612 int num0 = num;
613 for (field = type->ref->next; field; field = field->next) {
614 if (field->c != (num - num0) * *fsize)
615 return -1;
616 num = arm64_hfa_aux(&field->type, fsize, num);
617 if (num == -1)
618 return -1;
620 if (type->ref->c != (num - num0) * *fsize)
621 return -1;
622 return num;
624 else { // union
625 int num0 = num;
626 for (field = type->ref->next; field; field = field->next) {
627 int num1 = arm64_hfa_aux(&field->type, fsize, num0);
628 if (num1 == -1)
629 return -1;
630 num = num1 < num ? num : num1;
632 if (type->ref->c != (num - num0) * *fsize)
633 return -1;
634 return num;
637 else if (type->t & VT_ARRAY) {
638 int num1;
639 if (!type->ref->c)
640 return num;
641 num1 = arm64_hfa_aux(&type->ref->type, fsize, num);
642 if (num1 == -1 || (num1 != num && type->ref->c > 4))
643 return -1;
644 num1 = num + type->ref->c * (num1 - num);
645 if (num1 > 4)
646 return -1;
647 return num1;
649 return -1;
652 static int arm64_hfa(CType *type, int *fsize)
654 if ((type->t & VT_BTYPE) == VT_STRUCT || (type->t & VT_ARRAY)) {
655 int sz = 0;
656 int n = arm64_hfa_aux(type, &sz, 0);
657 if (0 < n && n <= 4) {
658 if (fsize)
659 *fsize = sz;
660 return n;
663 return 0;
666 static unsigned long arm64_pcs_aux(int n, CType **type, unsigned long *a)
668 int nx = 0; // next integer register
669 int nv = 0; // next vector register
670 unsigned long ns = 32; // next stack offset
671 int i;
673 for (i = 0; i < n; i++) {
674 int hfa = arm64_hfa(type[i], 0);
675 int size, align;
677 if ((type[i]->t & VT_ARRAY) ||
678 (type[i]->t & VT_BTYPE) == VT_FUNC)
679 size = align = 8;
680 else
681 size = type_size(type[i], &align);
683 if (hfa)
684 // B.2
686 else if (size > 16) {
687 // B.3: replace with pointer
688 if (nx < 8)
689 a[i] = nx++ << 1 | 1;
690 else {
691 ns = (ns + 7) & ~7;
692 a[i] = ns | 1;
693 ns += 8;
695 continue;
697 else if ((type[i]->t & VT_BTYPE) == VT_STRUCT)
698 // B.4
699 size = (size + 7) & ~7;
701 // C.1
702 if (is_float(type[i]->t) && nv < 8) {
703 a[i] = 16 + (nv++ << 1);
704 continue;
707 // C.2
708 if (hfa && nv + hfa <= 8) {
709 a[i] = 16 + (nv << 1);
710 nv += hfa;
711 continue;
714 // C.3
715 if (hfa) {
716 nv = 8;
717 size = (size + 7) & ~7;
720 // C.4
721 if (hfa || (type[i]->t & VT_BTYPE) == VT_LDOUBLE) {
722 ns = (ns + 7) & ~7;
723 ns = (ns + align - 1) & -align;
726 // C.5
727 if ((type[i]->t & VT_BTYPE) == VT_FLOAT)
728 size = 8;
730 // C.6
731 if (hfa || is_float(type[i]->t)) {
732 a[i] = ns;
733 ns += size;
734 continue;
737 // C.7
738 if ((type[i]->t & VT_BTYPE) != VT_STRUCT && size <= 8 && nx < 8) {
739 a[i] = nx++ << 1;
740 continue;
743 // C.8
744 if (align == 16)
745 nx = (nx + 1) & ~1;
747 // C.9
748 if ((type[i]->t & VT_BTYPE) != VT_STRUCT && size == 16 && nx < 7) {
749 a[i] = nx << 1;
750 nx += 2;
751 continue;
754 // C.10
755 if ((type[i]->t & VT_BTYPE) == VT_STRUCT && size <= (8 - nx) * 8) {
756 a[i] = nx << 1;
757 nx += (size + 7) >> 3;
758 continue;
761 // C.11
762 nx = 8;
764 // C.12
765 ns = (ns + 7) & ~7;
766 ns = (ns + align - 1) & -align;
768 // C.13
769 if ((type[i]->t & VT_BTYPE) == VT_STRUCT) {
770 a[i] = ns;
771 ns += size;
772 continue;
775 // C.14
776 if (size < 8)
777 size = 8;
779 // C.15
780 a[i] = ns;
781 ns += size;
784 return ns - 32;
787 static unsigned long arm64_pcs(int n, CType **type, unsigned long *a)
789 unsigned long stack;
791 // Return type:
792 if ((type[0]->t & VT_BTYPE) == VT_VOID)
793 a[0] = -1;
794 else {
795 arm64_pcs_aux(1, type, a);
796 assert(a[0] == 0 || a[0] == 1 || a[0] == 16);
799 // Argument types:
800 stack = arm64_pcs_aux(n, type + 1, a + 1);
802 if (0) {
803 int i;
804 for (i = 0; i <= n; i++) {
805 if (!i)
806 printf("arm64_pcs return: ");
807 else
808 printf("arm64_pcs arg %d: ", i);
809 if (a[i] == (unsigned long)-1)
810 printf("void\n");
811 else if (a[i] == 1 && !i)
812 printf("X8 pointer\n");
813 else if (a[i] < 16)
814 printf("X%lu%s\n", a[i] / 2, a[i] & 1 ? " pointer" : "");
815 else if (a[i] < 32)
816 printf("V%lu\n", a[i] / 2 - 8);
817 else
818 printf("stack %lu%s\n",
819 (a[i] - 32) & ~1, a[i] & 1 ? " pointer" : "");
823 return stack;
826 ST_FUNC void gfunc_call(int nb_args)
828 CType *return_type;
829 CType **t;
830 unsigned long *a, *a1;
831 unsigned long stack;
832 int i;
834 return_type = &vtop[-nb_args].type.ref->type;
835 if ((return_type->t & VT_BTYPE) == VT_STRUCT)
836 --nb_args;
838 t = tcc_malloc((nb_args + 1) * sizeof(*t));
839 a = tcc_malloc((nb_args + 1) * sizeof(*a));
840 a1 = tcc_malloc((nb_args + 1) * sizeof(*a1));
842 t[0] = return_type;
843 for (i = 0; i < nb_args; i++)
844 t[nb_args - i] = &vtop[-i].type;
846 stack = arm64_pcs(nb_args, t, a);
848 // Allocate space for structs replaced by pointer:
849 for (i = nb_args; i; i--)
850 if (a[i] & 1) {
851 SValue *arg = &vtop[i - nb_args];
852 int align, size = type_size(&arg->type, &align);
853 assert((arg->type.t & VT_BTYPE) == VT_STRUCT);
854 stack = (stack + align - 1) & -align;
855 a1[i] = stack;
856 stack += size;
859 stack = (stack + 15) >> 4 << 4;
861 assert(stack < 0x1000);
862 if (stack)
863 o(0xd10003ff | stack << 10); // sub sp,sp,#(n)
865 // First pass: set all values on stack
866 for (i = nb_args; i; i--) {
867 vpushv(vtop - nb_args + i);
869 if (a[i] & 1) {
870 // struct replaced by pointer
871 int r = get_reg(RC_INT);
872 arm64_spoff(intr(r), a1[i]);
873 vset(&vtop->type, r | VT_LVAL, 0);
874 vswap();
875 vstore();
876 if (a[i] >= 32) {
877 // pointer on stack
878 r = get_reg(RC_INT);
879 arm64_spoff(intr(r), a1[i]);
880 arm64_strx(3, intr(r), 31, (a[i] - 32) >> 1 << 1);
883 else if (a[i] >= 32) {
884 // value on stack
885 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
886 int r = get_reg(RC_INT);
887 arm64_spoff(intr(r), a[i] - 32);
888 vset(&vtop->type, r | VT_LVAL, 0);
889 vswap();
890 vstore();
892 else if (is_float(vtop->type.t)) {
893 gv(RC_FLOAT);
894 arm64_strv(arm64_type_size(vtop[0].type.t),
895 fltr(vtop[0].r), 31, a[i] - 32);
897 else {
898 gv(RC_INT);
899 arm64_strx(arm64_type_size(vtop[0].type.t),
900 intr(vtop[0].r), 31, a[i] - 32);
904 --vtop;
907 // Second pass: assign values to registers
908 for (i = nb_args; i; i--, vtop--) {
909 if (a[i] < 16 && !(a[i] & 1)) {
910 // value in general-purpose registers
911 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
912 int align, size = type_size(&vtop->type, &align);
913 vtop->type.t = VT_PTR;
914 gaddrof();
915 gv(RC_R(a[i] / 2));
916 arm64_ldrs(a[i] / 2, size);
918 else
919 gv(RC_R(a[i] / 2));
921 else if (a[i] < 16)
922 // struct replaced by pointer in register
923 arm64_spoff(a[i] / 2, a1[i]);
924 else if (a[i] < 32) {
925 // value in floating-point registers
926 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
927 uint32_t j, sz, n = arm64_hfa(&vtop->type, &sz);
928 vtop->type.t = VT_PTR;
929 gaddrof();
930 gv(RC_R30);
931 for (j = 0; j < n; j++)
932 o(0x3d4003c0 |
933 (sz & 16) << 19 | -(sz & 8) << 27 | (sz & 4) << 29 |
934 (a[i] / 2 - 8 + j) |
935 j << 10); // ldr ([sdq])(*),[x30,#(j * sz)]
937 else
938 gv(RC_F(a[i] / 2 - 8));
942 if ((return_type->t & VT_BTYPE) == VT_STRUCT) {
943 if (a[0] == 1) {
944 // indirect return: set x8 and discard the stack value
945 gv(RC_R(8));
946 --vtop;
948 else
949 // return in registers: keep the address for after the call
950 vswap();
953 save_regs(0);
954 arm64_gen_bl_or_b(0);
955 --vtop;
956 if (stack)
957 o(0x910003ff | stack << 10); // add sp,sp,#(n)
960 int rt = return_type->t;
961 int bt = rt & VT_BTYPE;
962 if (bt == VT_STRUCT && !(a[0] & 1)) {
963 // A struct was returned in registers, so write it out:
964 gv(RC_R(8));
965 --vtop;
966 if (a[0] == 0) {
967 int align, size = type_size(return_type, &align);
968 assert(size <= 16);
969 if (size > 8)
970 o(0xa9000500); // stp x0,x1,[x8]
971 else if (size)
972 arm64_strx(size > 4 ? 3 : size > 2 ? 2 : size > 1, 0, 8, 0);
975 else if (a[0] == 16) {
976 uint32_t j, sz, n = arm64_hfa(return_type, &sz);
977 for (j = 0; j < n; j++)
978 o(0x3d000100 |
979 (sz & 16) << 19 | -(sz & 8) << 27 | (sz & 4) << 29 |
980 (a[i] / 2 - 8 + j) |
981 j << 10); // str ([sdq])(*),[x8,#(j * sz)]
986 tcc_free(a1);
987 tcc_free(a);
988 tcc_free(t);
991 static unsigned long arm64_func_va_list_stack;
992 static int arm64_func_va_list_gr_offs;
993 static int arm64_func_va_list_vr_offs;
994 static int arm64_func_sub_sp_offset;
996 ST_FUNC void gfunc_prolog(Sym *func_sym)
998 CType *func_type = &func_sym->type;
999 int n = 0;
1000 int i = 0;
1001 Sym *sym;
1002 CType **t;
1003 unsigned long *a;
1005 // Why doesn't the caller (gen_function) set func_vt?
1006 func_vt = func_type->ref->type;
1007 func_vc = 144; // offset of where x8 is stored
1009 for (sym = func_type->ref; sym; sym = sym->next)
1010 ++n;
1011 t = tcc_malloc(n * sizeof(*t));
1012 a = tcc_malloc(n * sizeof(*a));
1014 for (sym = func_type->ref; sym; sym = sym->next)
1015 t[i++] = &sym->type;
1017 arm64_func_va_list_stack = arm64_pcs(n - 1, t, a);
1019 o(0xa9b27bfd); // stp x29,x30,[sp,#-224]!
1020 o(0xad0087e0); // stp q0,q1,[sp,#16]
1021 o(0xad018fe2); // stp q2,q3,[sp,#48]
1022 o(0xad0297e4); // stp q4,q5,[sp,#80]
1023 o(0xad039fe6); // stp q6,q7,[sp,#112]
1024 o(0xa90923e8); // stp x8,x8,[sp,#144]
1025 o(0xa90a07e0); // stp x0,x1,[sp,#160]
1026 o(0xa90b0fe2); // stp x2,x3,[sp,#176]
1027 o(0xa90c17e4); // stp x4,x5,[sp,#192]
1028 o(0xa90d1fe6); // stp x6,x7,[sp,#208]
1030 arm64_func_va_list_gr_offs = -64;
1031 arm64_func_va_list_vr_offs = -128;
1033 for (i = 1, sym = func_type->ref->next; sym; i++, sym = sym->next) {
1034 int off = (a[i] < 16 ? 160 + a[i] / 2 * 8 :
1035 a[i] < 32 ? 16 + (a[i] - 16) / 2 * 16 :
1036 224 + ((a[i] - 32) >> 1 << 1));
1037 sym_push(sym->v & ~SYM_FIELD, &sym->type,
1038 (a[i] & 1 ? VT_LLOCAL : VT_LOCAL) | VT_LVAL,
1039 off);
1041 if (a[i] < 16) {
1042 int align, size = type_size(&sym->type, &align);
1043 arm64_func_va_list_gr_offs = (a[i] / 2 - 7 +
1044 (!(a[i] & 1) && size > 8)) * 8;
1046 else if (a[i] < 32) {
1047 uint32_t hfa = arm64_hfa(&sym->type, 0);
1048 arm64_func_va_list_vr_offs = (a[i] / 2 - 16 +
1049 (hfa ? hfa : 1)) * 16;
1052 // HFAs of float and double need to be written differently:
1053 if (16 <= a[i] && a[i] < 32 && (sym->type.t & VT_BTYPE) == VT_STRUCT) {
1054 uint32_t j, sz, k = arm64_hfa(&sym->type, &sz);
1055 if (sz < 16)
1056 for (j = 0; j < k; j++) {
1057 o(0x3d0003e0 | -(sz & 8) << 27 | (sz & 4) << 29 |
1058 ((a[i] - 16) / 2 + j) | (off / sz + j) << 10);
1059 // str ([sdq])(*),[sp,#(j * sz)]
1064 tcc_free(a);
1065 tcc_free(t);
1067 o(0x910003fd); // mov x29,sp
1068 arm64_func_sub_sp_offset = ind;
1069 // In gfunc_epilog these will be replaced with code to decrement SP:
1070 o(0xd503201f); // nop
1071 o(0xd503201f); // nop
1072 loc = 0;
1075 ST_FUNC void gen_va_start(void)
1077 int r;
1078 --vtop; // we don't need the "arg"
1079 gaddrof();
1080 r = intr(gv(RC_INT));
1082 if (arm64_func_va_list_stack) {
1083 //xx could use add (immediate) here
1084 arm64_movimm(30, arm64_func_va_list_stack + 224);
1085 o(0x8b1e03be); // add x30,x29,x30
1087 else
1088 o(0x910383be); // add x30,x29,#224
1089 o(0xf900001e | r << 5); // str x30,[x(r)]
1091 if (arm64_func_va_list_gr_offs) {
1092 if (arm64_func_va_list_stack)
1093 o(0x910383be); // add x30,x29,#224
1094 o(0xf900041e | r << 5); // str x30,[x(r),#8]
1097 if (arm64_func_va_list_vr_offs) {
1098 o(0x910243be); // add x30,x29,#144
1099 o(0xf900081e | r << 5); // str x30,[x(r),#16]
1102 arm64_movimm(30, arm64_func_va_list_gr_offs);
1103 o(0xb900181e | r << 5); // str w30,[x(r),#24]
1105 arm64_movimm(30, arm64_func_va_list_vr_offs);
1106 o(0xb9001c1e | r << 5); // str w30,[x(r),#28]
1108 --vtop;
1111 ST_FUNC void gen_va_arg(CType *t)
1113 int align, size = type_size(t, &align);
1114 int fsize, hfa = arm64_hfa(t, &fsize);
1115 uint32_t r0, r1;
1117 if (is_float(t->t)) {
1118 hfa = 1;
1119 fsize = size;
1122 gaddrof();
1123 r0 = intr(gv(RC_INT));
1124 r1 = get_reg(RC_INT);
1125 vtop[0].r = r1 | VT_LVAL;
1126 r1 = intr(r1);
1128 if (!hfa) {
1129 uint32_t n = size > 16 ? 8 : (size + 7) & -8;
1130 o(0xb940181e | r0 << 5); // ldr w30,[x(r0),#24] // __gr_offs
1131 if (align == 16) {
1132 assert(0); // this path untested but needed for __uint128_t
1133 o(0x11003fde); // add w30,w30,#15
1134 o(0x121c6fde); // and w30,w30,#-16
1136 o(0x310003c0 | r1 | n << 10); // adds w(r1),w30,#(n)
1137 o(0x540000ad); // b.le .+20
1138 o(0xf9400000 | r1 | r0 << 5); // ldr x(r1),[x(r0)] // __stack
1139 o(0x9100001e | r1 << 5 | n << 10); // add x30,x(r1),#(n)
1140 o(0xf900001e | r0 << 5); // str x30,[x(r0)] // __stack
1141 o(0x14000004); // b .+16
1142 o(0xb9001800 | r1 | r0 << 5); // str w(r1),[x(r0),#24] // __gr_offs
1143 o(0xf9400400 | r1 | r0 << 5); // ldr x(r1),[x(r0),#8] // __gr_top
1144 o(0x8b3ec000 | r1 | r1 << 5); // add x(r1),x(r1),w30,sxtw
1145 if (size > 16)
1146 o(0xf9400000 | r1 | r1 << 5); // ldr x(r1),[x(r1)]
1148 else {
1149 uint32_t rsz = hfa << 4;
1150 uint32_t ssz = (size + 7) & -(uint32_t)8;
1151 uint32_t b1, b2;
1152 o(0xb9401c1e | r0 << 5); // ldr w30,[x(r0),#28] // __vr_offs
1153 o(0x310003c0 | r1 | rsz << 10); // adds w(r1),w30,#(rsz)
1154 b1 = ind; o(0x5400000d); // b.le lab1
1155 o(0xf9400000 | r1 | r0 << 5); // ldr x(r1),[x(r0)] // __stack
1156 if (fsize == 16) {
1157 o(0x91003c00 | r1 | r1 << 5); // add x(r1),x(r1),#15
1158 o(0x927cec00 | r1 | r1 << 5); // and x(r1),x(r1),#-16
1160 o(0x9100001e | r1 << 5 | ssz << 10); // add x30,x(r1),#(ssz)
1161 o(0xf900001e | r0 << 5); // str x30,[x(r0)] // __stack
1162 b2 = ind; o(0x14000000); // b lab2
1163 // lab1:
1164 write32le(cur_text_section->data + b1, 0x5400000d | (ind - b1) << 3);
1165 o(0xb9001c00 | r1 | r0 << 5); // str w(r1),[x(r0),#28] // __vr_offs
1166 o(0xf9400800 | r1 | r0 << 5); // ldr x(r1),[x(r0),#16] // __vr_top
1167 if (hfa == 1 || fsize == 16)
1168 o(0x8b3ec000 | r1 | r1 << 5); // add x(r1),x(r1),w30,sxtw
1169 else {
1170 // We need to change the layout of this HFA.
1171 // Get some space on the stack using global variable "loc":
1172 loc = (loc - size) & -(uint32_t)align;
1173 o(0x8b3ec000 | 30 | r1 << 5); // add x30,x(r1),w30,sxtw
1174 arm64_movimm(r1, loc);
1175 o(0x8b0003a0 | r1 | r1 << 16); // add x(r1),x29,x(r1)
1176 o(0x4c402bdc | (uint32_t)fsize << 7 |
1177 (uint32_t)(hfa == 2) << 15 |
1178 (uint32_t)(hfa == 3) << 14); // ld1 {v28.(4s|2d),...},[x30]
1179 o(0x0d00801c | r1 << 5 | (fsize == 8) << 10 |
1180 (uint32_t)(hfa != 2) << 13 |
1181 (uint32_t)(hfa != 3) << 21); // st(hfa) {v28.(s|d),...}[0],[x(r1)]
1183 // lab2:
1184 write32le(cur_text_section->data + b2, 0x14000000 | (ind - b2) >> 2);
1188 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret,
1189 int *align, int *regsize)
1191 return 0;
1194 ST_FUNC void gfunc_return(CType *func_type)
1196 CType *t = func_type;
1197 unsigned long a;
1199 arm64_pcs(0, &t, &a);
1200 switch (a) {
1201 case -1:
1202 break;
1203 case 0:
1204 if ((func_type->t & VT_BTYPE) == VT_STRUCT) {
1205 int align, size = type_size(func_type, &align);
1206 gaddrof();
1207 gv(RC_R(0));
1208 arm64_ldrs(0, size);
1210 else
1211 gv(RC_IRET);
1212 break;
1213 case 1: {
1214 CType type = *func_type;
1215 mk_pointer(&type);
1216 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
1217 indir();
1218 vswap();
1219 vstore();
1220 break;
1222 case 16:
1223 if ((func_type->t & VT_BTYPE) == VT_STRUCT) {
1224 uint32_t j, sz, n = arm64_hfa(&vtop->type, &sz);
1225 gaddrof();
1226 gv(RC_R(0));
1227 for (j = 0; j < n; j++)
1228 o(0x3d400000 |
1229 (sz & 16) << 19 | -(sz & 8) << 27 | (sz & 4) << 29 |
1230 j | j << 10); // ldr ([sdq])(*),[x0,#(j * sz)]
1232 else
1233 gv(RC_FRET);
1234 break;
1235 default:
1236 assert(0);
1238 vtop--;
1241 ST_FUNC void gfunc_epilog(void)
1243 if (loc) {
1244 // Insert instructions to subtract size of stack frame from SP.
1245 unsigned char *ptr = cur_text_section->data + arm64_func_sub_sp_offset;
1246 uint64_t diff = (-loc + 15) & ~15;
1247 if (!(diff >> 24)) {
1248 if (diff & 0xfff) // sub sp,sp,#(diff & 0xfff)
1249 write32le(ptr, 0xd10003ff | (diff & 0xfff) << 10);
1250 if (diff >> 12) // sub sp,sp,#(diff >> 12),lsl #12
1251 write32le(ptr + 4, 0xd14003ff | (diff >> 12) << 10);
1253 else {
1254 // In this case we may subtract more than necessary,
1255 // but always less than 17/16 of what we were aiming for.
1256 int i = 0;
1257 int j = 0;
1258 while (diff >> 20) {
1259 diff = (diff + 0xffff) >> 16;
1260 ++i;
1262 while (diff >> 16) {
1263 diff = (diff + 1) >> 1;
1264 ++j;
1266 write32le(ptr, 0xd2800010 | diff << 5 | i << 21);
1267 // mov x16,#(diff),lsl #(16 * i)
1268 write32le(ptr + 4, 0xcb3063ff | j << 10);
1269 // sub sp,sp,x16,lsl #(j)
1272 o(0x910003bf); // mov sp,x29
1273 o(0xa8ce7bfd); // ldp x29,x30,[sp],#224
1275 o(0xd65f03c0); // ret
1278 ST_FUNC void gen_fill_nops(int bytes)
1280 if ((bytes & 3))
1281 tcc_error("alignment of code section not multiple of 4");
1282 while (bytes > 0) {
1283 o(0xd503201f); // nop
1284 bytes -= 4;
1288 // Generate forward branch to label:
1289 ST_FUNC int gjmp(int t)
1291 int r = ind;
1292 if (nocode_wanted)
1293 return t;
1294 o(t);
1295 return r;
1298 // Generate branch to known address:
1299 ST_FUNC void gjmp_addr(int a)
1301 assert(a - ind + 0x8000000 < 0x10000000);
1302 o(0x14000000 | ((a - ind) >> 2 & 0x3ffffff));
1305 ST_FUNC int gjmp_append(int n, int t)
1307 void *p;
1308 /* insert vtop->c jump list in t */
1309 if (n) {
1310 uint32_t n1 = n, n2;
1311 while ((n2 = read32le(p = cur_text_section->data + n1)))
1312 n1 = n2;
1313 write32le(p, t);
1314 t = n;
1316 return t;
1319 void arm64_vset_VT_CMP(int op)
1321 if (op >= TOK_ULT && op <= TOK_GT) {
1322 vtop->cmp_r = vtop->r;
1323 vset_VT_CMP(0x80);
1327 static void arm64_gen_opil(int op, uint32_t l);
1329 static void arm64_load_cmp(int r, SValue *sv)
1331 sv->r = sv->cmp_r;
1332 if (sv->c.i & 1) {
1333 vpushi(1);
1334 arm64_gen_opil('^', 0);
1336 if (r != sv->r) {
1337 load(r, sv);
1338 sv->r = r;
1342 ST_FUNC int gjmp_cond(int op, int t)
1344 int bt = vtop->type.t & VT_BTYPE;
1346 int inv = op & 1;
1347 vtop->r = vtop->cmp_r;
1349 if (bt == VT_LDOUBLE) {
1350 uint32_t a, b, f = fltr(gv(RC_FLOAT));
1351 a = get_reg(RC_INT);
1352 vpushi(0);
1353 vtop[0].r = a;
1354 b = get_reg(RC_INT);
1355 a = intr(a);
1356 b = intr(b);
1357 o(0x4e083c00 | a | f << 5); // mov x(a),v(f).d[0]
1358 o(0x4e183c00 | b | f << 5); // mov x(b),v(f).d[1]
1359 o(0xaa000400 | a | a << 5 | b << 16); // orr x(a),x(a),x(b),lsl #1
1360 o(0xb4000040 | a | !!inv << 24); // cbz/cbnz x(a),.+8
1361 --vtop;
1363 else if (bt == VT_FLOAT || bt == VT_DOUBLE) {
1364 uint32_t a = fltr(gv(RC_FLOAT));
1365 o(0x1e202008 | a << 5 | (bt != VT_FLOAT) << 22); // fcmp
1366 o(0x54000040 | !!inv); // b.eq/b.ne .+8
1368 else {
1369 uint32_t ll = (bt == VT_PTR || bt == VT_LLONG);
1370 uint32_t a = intr(gv(RC_INT));
1371 o(0x34000040 | a | !!inv << 24 | ll << 31); // cbz/cbnz wA,.+8
1373 return gjmp(t);
1376 static int arm64_iconst(uint64_t *val, SValue *sv)
1378 if ((sv->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
1379 return 0;
1380 if (val) {
1381 int t = sv->type.t;
1382 int bt = t & VT_BTYPE;
1383 *val = ((bt == VT_LLONG || bt == VT_PTR) ? sv->c.i :
1384 (uint32_t)sv->c.i |
1385 (t & VT_UNSIGNED ? 0 : -(sv->c.i & 0x80000000)));
1387 return 1;
1390 static int arm64_gen_opic(int op, uint32_t l, int rev, uint64_t val,
1391 uint32_t x, uint32_t a)
1393 if (op == '-' && !rev) {
1394 val = -val;
1395 op = '+';
1397 val = l ? val : (uint32_t)val;
1399 switch (op) {
1401 case '+': {
1402 uint32_t s = l ? val >> 63 : val >> 31;
1403 val = s ? -val : val;
1404 val = l ? val : (uint32_t)val;
1405 if (!(val & ~(uint64_t)0xfff))
1406 o(0x11000000 | l << 31 | s << 30 | x | a << 5 | val << 10);
1407 else if (!(val & ~(uint64_t)0xfff000))
1408 o(0x11400000 | l << 31 | s << 30 | x | a << 5 | val >> 12 << 10);
1409 else {
1410 arm64_movimm(30, val); // use x30
1411 o(0x0b1e0000 | l << 31 | s << 30 | x | a << 5);
1413 return 1;
1416 case '-':
1417 if (!val)
1418 o(0x4b0003e0 | l << 31 | x | a << 16); // neg
1419 else if (val == (l ? (uint64_t)-1 : (uint32_t)-1))
1420 o(0x2a2003e0 | l << 31 | x | a << 16); // mvn
1421 else {
1422 arm64_movimm(30, val); // use x30
1423 o(0x4b0003c0 | l << 31 | x | a << 16); // sub
1425 return 1;
1427 case '^':
1428 if (val == -1 || (val == 0xffffffff && !l)) {
1429 o(0x2a2003e0 | l << 31 | x | a << 16); // mvn
1430 return 1;
1432 // fall through
1433 case '&':
1434 case '|': {
1435 int e = arm64_encode_bimm64(l ? val : val | val << 32);
1436 if (e < 0)
1437 return 0;
1438 o((op == '&' ? 0x12000000 :
1439 op == '|' ? 0x32000000 : 0x52000000) |
1440 l << 31 | x | a << 5 | (uint32_t)e << 10);
1441 return 1;
1444 case TOK_SAR:
1445 case TOK_SHL:
1446 case TOK_SHR: {
1447 uint32_t n = 32 << l;
1448 val = val & (n - 1);
1449 if (rev)
1450 return 0;
1451 if (!val)
1452 assert(0);
1453 else if (op == TOK_SHL)
1454 o(0x53000000 | l << 31 | l << 22 | x | a << 5 |
1455 (n - val) << 16 | (n - 1 - val) << 10); // lsl
1456 else
1457 o(0x13000000 | (op == TOK_SHR) << 30 | l << 31 | l << 22 |
1458 x | a << 5 | val << 16 | (n - 1) << 10); // lsr/asr
1459 return 1;
1463 return 0;
1466 static void arm64_gen_opil(int op, uint32_t l)
1468 uint32_t x, a, b;
1470 // Special treatment for operations with a constant operand:
1472 uint64_t val;
1473 int rev = 1;
1475 if (arm64_iconst(0, &vtop[0])) {
1476 vswap();
1477 rev = 0;
1479 if (arm64_iconst(&val, &vtop[-1])) {
1480 gv(RC_INT);
1481 a = intr(vtop[0].r);
1482 --vtop;
1483 x = get_reg(RC_INT);
1484 ++vtop;
1485 if (arm64_gen_opic(op, l, rev, val, intr(x), a)) {
1486 vtop[0].r = x;
1487 vswap();
1488 --vtop;
1489 return;
1492 if (!rev)
1493 vswap();
1496 gv2(RC_INT, RC_INT);
1497 assert(vtop[-1].r < VT_CONST && vtop[0].r < VT_CONST);
1498 a = intr(vtop[-1].r);
1499 b = intr(vtop[0].r);
1500 vtop -= 2;
1501 x = get_reg(RC_INT);
1502 ++vtop;
1503 vtop[0].r = x;
1504 x = intr(x);
1506 switch (op) {
1507 case '%':
1508 // Use x30 for quotient:
1509 o(0x1ac00c00 | l << 31 | 30 | a << 5 | b << 16); // sdiv
1510 o(0x1b008000 | l << 31 | x | (uint32_t)30 << 5 |
1511 b << 16 | a << 10); // msub
1512 break;
1513 case '&':
1514 o(0x0a000000 | l << 31 | x | a << 5 | b << 16); // and
1515 break;
1516 case '*':
1517 o(0x1b007c00 | l << 31 | x | a << 5 | b << 16); // mul
1518 break;
1519 case '+':
1520 o(0x0b000000 | l << 31 | x | a << 5 | b << 16); // add
1521 break;
1522 case '-':
1523 o(0x4b000000 | l << 31 | x | a << 5 | b << 16); // sub
1524 break;
1525 case '/':
1526 o(0x1ac00c00 | l << 31 | x | a << 5 | b << 16); // sdiv
1527 break;
1528 case '^':
1529 o(0x4a000000 | l << 31 | x | a << 5 | b << 16); // eor
1530 break;
1531 case '|':
1532 o(0x2a000000 | l << 31 | x | a << 5 | b << 16); // orr
1533 break;
1534 case TOK_EQ:
1535 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1536 o(0x1a9f17e0 | x); // cset wA,eq
1537 break;
1538 case TOK_GE:
1539 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1540 o(0x1a9fb7e0 | x); // cset wA,ge
1541 break;
1542 case TOK_GT:
1543 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1544 o(0x1a9fd7e0 | x); // cset wA,gt
1545 break;
1546 case TOK_LE:
1547 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1548 o(0x1a9fc7e0 | x); // cset wA,le
1549 break;
1550 case TOK_LT:
1551 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1552 o(0x1a9fa7e0 | x); // cset wA,lt
1553 break;
1554 case TOK_NE:
1555 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1556 o(0x1a9f07e0 | x); // cset wA,ne
1557 break;
1558 case TOK_SAR:
1559 o(0x1ac02800 | l << 31 | x | a << 5 | b << 16); // asr
1560 break;
1561 case TOK_SHL:
1562 o(0x1ac02000 | l << 31 | x | a << 5 | b << 16); // lsl
1563 break;
1564 case TOK_SHR:
1565 o(0x1ac02400 | l << 31 | x | a << 5 | b << 16); // lsr
1566 break;
1567 case TOK_UDIV:
1568 case TOK_PDIV:
1569 o(0x1ac00800 | l << 31 | x | a << 5 | b << 16); // udiv
1570 break;
1571 case TOK_UGE:
1572 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1573 o(0x1a9f37e0 | x); // cset wA,cs
1574 break;
1575 case TOK_UGT:
1576 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1577 o(0x1a9f97e0 | x); // cset wA,hi
1578 break;
1579 case TOK_ULT:
1580 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1581 o(0x1a9f27e0 | x); // cset wA,cc
1582 break;
1583 case TOK_ULE:
1584 o(0x6b00001f | l << 31 | a << 5 | b << 16); // cmp
1585 o(0x1a9f87e0 | x); // cset wA,ls
1586 break;
1587 case TOK_UMOD:
1588 // Use x30 for quotient:
1589 o(0x1ac00800 | l << 31 | 30 | a << 5 | b << 16); // udiv
1590 o(0x1b008000 | l << 31 | x | (uint32_t)30 << 5 |
1591 b << 16 | a << 10); // msub
1592 break;
1593 default:
1594 assert(0);
1598 ST_FUNC void gen_opi(int op)
1600 arm64_gen_opil(op, 0);
1601 arm64_vset_VT_CMP(op);
1604 ST_FUNC void gen_opl(int op)
1606 arm64_gen_opil(op, 1);
1607 arm64_vset_VT_CMP(op);
1610 ST_FUNC void gen_opf(int op)
1612 uint32_t x, a, b, dbl;
1614 if (vtop[0].type.t == VT_LDOUBLE) {
1615 CType type = vtop[0].type;
1616 int func = 0;
1617 int cond = -1;
1618 switch (op) {
1619 case '*': func = TOK___multf3; break;
1620 case '+': func = TOK___addtf3; break;
1621 case '-': func = TOK___subtf3; break;
1622 case '/': func = TOK___divtf3; break;
1623 case TOK_EQ: func = TOK___eqtf2; cond = 1; break;
1624 case TOK_NE: func = TOK___netf2; cond = 0; break;
1625 case TOK_LT: func = TOK___lttf2; cond = 10; break;
1626 case TOK_GE: func = TOK___getf2; cond = 11; break;
1627 case TOK_LE: func = TOK___letf2; cond = 12; break;
1628 case TOK_GT: func = TOK___gttf2; cond = 13; break;
1629 default: assert(0); break;
1631 vpush_global_sym(&func_old_type, func);
1632 vrott(3);
1633 gfunc_call(2);
1634 vpushi(0);
1635 vtop->r = cond < 0 ? REG_FRET : REG_IRET;
1636 if (cond < 0)
1637 vtop->type = type;
1638 else {
1639 o(0x7100001f); // cmp w0,#0
1640 o(0x1a9f07e0 | (uint32_t)cond << 12); // cset w0,(cond)
1642 return;
1645 dbl = vtop[0].type.t != VT_FLOAT;
1646 gv2(RC_FLOAT, RC_FLOAT);
1647 assert(vtop[-1].r < VT_CONST && vtop[0].r < VT_CONST);
1648 a = fltr(vtop[-1].r);
1649 b = fltr(vtop[0].r);
1650 vtop -= 2;
1651 switch (op) {
1652 case TOK_EQ: case TOK_NE:
1653 case TOK_LT: case TOK_GE: case TOK_LE: case TOK_GT:
1654 x = get_reg(RC_INT);
1655 ++vtop;
1656 vtop[0].r = x;
1657 x = intr(x);
1658 break;
1659 default:
1660 x = get_reg(RC_FLOAT);
1661 ++vtop;
1662 vtop[0].r = x;
1663 x = fltr(x);
1664 break;
1667 switch (op) {
1668 case '*':
1669 o(0x1e200800 | dbl << 22 | x | a << 5 | b << 16); // fmul
1670 break;
1671 case '+':
1672 o(0x1e202800 | dbl << 22 | x | a << 5 | b << 16); // fadd
1673 break;
1674 case '-':
1675 o(0x1e203800 | dbl << 22 | x | a << 5 | b << 16); // fsub
1676 break;
1677 case '/':
1678 o(0x1e201800 | dbl << 22 | x | a << 5 | b << 16); // fdiv
1679 break;
1680 case TOK_EQ:
1681 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1682 o(0x1a9f17e0 | x); // cset w(x),eq
1683 break;
1684 case TOK_GE:
1685 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1686 o(0x1a9fb7e0 | x); // cset w(x),ge
1687 break;
1688 case TOK_GT:
1689 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1690 o(0x1a9fd7e0 | x); // cset w(x),gt
1691 break;
1692 case TOK_LE:
1693 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1694 o(0x1a9f87e0 | x); // cset w(x),ls
1695 break;
1696 case TOK_LT:
1697 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1698 o(0x1a9f57e0 | x); // cset w(x),mi
1699 break;
1700 case TOK_NE:
1701 o(0x1e202000 | dbl << 22 | a << 5 | b << 16); // fcmp
1702 o(0x1a9f07e0 | x); // cset w(x),ne
1703 break;
1704 default:
1705 assert(0);
1707 arm64_vset_VT_CMP(op);
1710 // Generate sign extension from 32 to 64 bits:
1711 ST_FUNC void gen_cvt_sxtw(void)
1713 uint32_t r = intr(gv(RC_INT));
1714 o(0x93407c00 | r | r << 5); // sxtw x(r),w(r)
1717 /* char/short to int conversion */
1718 ST_FUNC void gen_cvt_csti(int t)
1720 int r = intr(gv(RC_INT));
1721 o(0x13001c00
1722 | ((t & VT_BTYPE) == VT_SHORT) << 13
1723 | (uint32_t)!!(t & VT_UNSIGNED) << 30
1724 | r | r << 5); // [su]xt[bh] w(r),w(r)
1727 ST_FUNC void gen_cvt_itof(int t)
1729 if (t == VT_LDOUBLE) {
1730 int f = vtop->type.t;
1731 int func = (f & VT_BTYPE) == VT_LLONG ?
1732 (f & VT_UNSIGNED ? TOK___floatunditf : TOK___floatditf) :
1733 (f & VT_UNSIGNED ? TOK___floatunsitf : TOK___floatsitf);
1734 vpush_global_sym(&func_old_type, func);
1735 vrott(2);
1736 gfunc_call(1);
1737 vpushi(0);
1738 vtop->type.t = t;
1739 vtop->r = REG_FRET;
1740 return;
1742 else {
1743 int d, n = intr(gv(RC_INT));
1744 int s = !(vtop->type.t & VT_UNSIGNED);
1745 uint32_t l = ((vtop->type.t & VT_BTYPE) == VT_LLONG);
1746 --vtop;
1747 d = get_reg(RC_FLOAT);
1748 ++vtop;
1749 vtop[0].r = d;
1750 o(0x1e220000 | (uint32_t)!s << 16 |
1751 (uint32_t)(t != VT_FLOAT) << 22 | fltr(d) |
1752 l << 31 | n << 5); // [us]cvtf [sd](d),[wx](n)
1756 ST_FUNC void gen_cvt_ftoi(int t)
1758 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
1759 int func = (t & VT_BTYPE) == VT_LLONG ?
1760 (t & VT_UNSIGNED ? TOK___fixunstfdi : TOK___fixtfdi) :
1761 (t & VT_UNSIGNED ? TOK___fixunstfsi : TOK___fixtfsi);
1762 vpush_global_sym(&func_old_type, func);
1763 vrott(2);
1764 gfunc_call(1);
1765 vpushi(0);
1766 vtop->type.t = t;
1767 vtop->r = REG_IRET;
1768 return;
1770 else {
1771 int d, n = fltr(gv(RC_FLOAT));
1772 uint32_t l = ((vtop->type.t & VT_BTYPE) != VT_FLOAT);
1773 --vtop;
1774 d = get_reg(RC_INT);
1775 ++vtop;
1776 vtop[0].r = d;
1777 o(0x1e380000 |
1778 (uint32_t)!!(t & VT_UNSIGNED) << 16 |
1779 (uint32_t)((t & VT_BTYPE) == VT_LLONG) << 31 | intr(d) |
1780 l << 22 | n << 5); // fcvtz[su] [wx](d),[sd](n)
1784 ST_FUNC void gen_cvt_ftof(int t)
1786 int f = vtop[0].type.t & VT_BTYPE;
1787 assert(t == VT_FLOAT || t == VT_DOUBLE || t == VT_LDOUBLE);
1788 assert(f == VT_FLOAT || f == VT_DOUBLE || f == VT_LDOUBLE);
1789 if (t == f)
1790 return;
1792 if (t == VT_LDOUBLE || f == VT_LDOUBLE) {
1793 int func = (t == VT_LDOUBLE) ?
1794 (f == VT_FLOAT ? TOK___extendsftf2 : TOK___extenddftf2) :
1795 (t == VT_FLOAT ? TOK___trunctfsf2 : TOK___trunctfdf2);
1796 vpush_global_sym(&func_old_type, func);
1797 vrott(2);
1798 gfunc_call(1);
1799 vpushi(0);
1800 vtop->type.t = t;
1801 vtop->r = REG_FRET;
1803 else {
1804 int x, a;
1805 gv(RC_FLOAT);
1806 assert(vtop[0].r < VT_CONST);
1807 a = fltr(vtop[0].r);
1808 --vtop;
1809 x = get_reg(RC_FLOAT);
1810 ++vtop;
1811 vtop[0].r = x;
1812 x = fltr(x);
1814 if (f == VT_FLOAT)
1815 o(0x1e22c000 | x | a << 5); // fcvt d(x),s(a)
1816 else
1817 o(0x1e624000 | x | a << 5); // fcvt s(x),d(a)
1821 ST_FUNC void ggoto(void)
1823 arm64_gen_bl_or_b(1);
1824 --vtop;
1827 ST_FUNC void gen_clear_cache(void)
1829 uint32_t beg, end, dsz, isz, p, lab1, b1;
1830 gv2(RC_INT, RC_INT);
1831 vpushi(0);
1832 vtop->r = get_reg(RC_INT);
1833 vpushi(0);
1834 vtop->r = get_reg(RC_INT);
1835 vpushi(0);
1836 vtop->r = get_reg(RC_INT);
1837 beg = intr(vtop[-4].r); // x0
1838 end = intr(vtop[-3].r); // x1
1839 dsz = intr(vtop[-2].r); // x2
1840 isz = intr(vtop[-1].r); // x3
1841 p = intr(vtop[0].r); // x4
1842 vtop -= 5;
1844 o(0xd53b0020 | isz); // mrs x(isz),ctr_el0
1845 o(0x52800080 | p); // mov w(p),#4
1846 o(0x53104c00 | dsz | isz << 5); // ubfx w(dsz),w(isz),#16,#4
1847 o(0x1ac02000 | dsz | p << 5 | dsz << 16); // lsl w(dsz),w(p),w(dsz)
1848 o(0x12000c00 | isz | isz << 5); // and w(isz),w(isz),#15
1849 o(0x1ac02000 | isz | p << 5 | isz << 16); // lsl w(isz),w(p),w(isz)
1850 o(0x51000400 | p | dsz << 5); // sub w(p),w(dsz),#1
1851 o(0x8a240004 | p | beg << 5 | p << 16); // bic x(p),x(beg),x(p)
1852 b1 = ind; o(0x14000000); // b
1853 lab1 = ind;
1854 o(0xd50b7b20 | p); // dc cvau,x(p)
1855 o(0x8b000000 | p | p << 5 | dsz << 16); // add x(p),x(p),x(dsz)
1856 write32le(cur_text_section->data + b1, 0x14000000 | (ind - b1) >> 2);
1857 o(0xeb00001f | p << 5 | end << 16); // cmp x(p),x(end)
1858 o(0x54ffffa3 | ((lab1 - ind) << 3 & 0xffffe0)); // b.cc lab1
1859 o(0xd5033b9f); // dsb ish
1860 o(0x51000400 | p | isz << 5); // sub w(p),w(isz),#1
1861 o(0x8a240004 | p | beg << 5 | p << 16); // bic x(p),x(beg),x(p)
1862 b1 = ind; o(0x14000000); // b
1863 lab1 = ind;
1864 o(0xd50b7520 | p); // ic ivau,x(p)
1865 o(0x8b000000 | p | p << 5 | isz << 16); // add x(p),x(p),x(isz)
1866 write32le(cur_text_section->data + b1, 0x14000000 | (ind - b1) >> 2);
1867 o(0xeb00001f | p << 5 | end << 16); // cmp x(p),x(end)
1868 o(0x54ffffa3 | ((lab1 - ind) << 3 & 0xffffe0)); // b.cc lab1
1869 o(0xd5033b9f); // dsb ish
1870 o(0xd5033fdf); // isb
1873 ST_FUNC void gen_vla_sp_save(int addr) {
1874 uint32_t r = intr(get_reg(RC_INT));
1875 o(0x910003e0 | r); // mov x(r),sp
1876 arm64_strx(3, r, 29, addr);
1879 ST_FUNC void gen_vla_sp_restore(int addr) {
1880 // Use x30 because this function can be called when there
1881 // is a live return value in x0 but there is nothing on
1882 // the value stack to prevent get_reg from returning x0.
1883 uint32_t r = 30;
1884 arm64_ldrx(0, 3, r, 29, addr);
1885 o(0x9100001f | r << 5); // mov sp,x(r)
1888 ST_FUNC void gen_vla_alloc(CType *type, int align) {
1889 uint32_t r = intr(gv(RC_INT));
1890 o(0x91003c00 | r | r << 5); // add x(r),x(r),#15
1891 o(0x927cec00 | r | r << 5); // bic x(r),x(r),#15
1892 o(0xcb2063ff | r << 16); // sub sp,sp,x(r)
1893 vpop();
1896 /* end of A64 code generator */
1897 /*************************************************************/
1898 #endif
1899 /*************************************************************/