Fix bug in gen_cvt_ftoi1. Add test 107_stack_safe for this fix.
[tinycc.git] / riscv64-gen.c
blob163657c85a5db4cd1b16ccded0cb2408bddf4ba3
1 #ifdef TARGET_DEFS_ONLY
3 // Number of registers available to allocator:
4 #define NB_REGS 19 // x10-x17 aka a0-a7, f10-f17 aka fa0-fa7, xxx, ra, sp
6 #define TREG_R(x) (x) // x = 0..7
7 #define TREG_F(x) (x + 8) // x = 0..7
9 // Register classes sorted from more general to more precise:
10 #define RC_INT (1 << 0)
11 #define RC_FLOAT (1 << 1)
12 #define RC_R(x) (1 << (2 + (x))) // x = 0..7
13 #define RC_F(x) (1 << (10 + (x))) // x = 0..7
15 #define RC_IRET (RC_R(0)) // int return register class
16 #define RC_FRET (RC_F(0)) // float return register class
18 #define REG_IRET (TREG_R(0)) // int return register number
19 #define REG_FRET (TREG_F(0)) // float return register number
21 #define PTR_SIZE 8
23 #define LDOUBLE_SIZE 16
24 #define LDOUBLE_ALIGN 16
26 #define MAX_ALIGN 16
28 #define CHAR_IS_UNSIGNED
30 #else
31 #include "tcc.h"
32 #include <assert.h>
34 #define XLEN 8
36 #define TREG_RA 17
37 #define TREG_SP 18
39 ST_DATA const int reg_classes[NB_REGS] = {
40 RC_INT | RC_R(0),
41 RC_INT | RC_R(1),
42 RC_INT | RC_R(2),
43 RC_INT | RC_R(3),
44 RC_INT | RC_R(4),
45 RC_INT | RC_R(5),
46 RC_INT | RC_R(6),
47 RC_INT | RC_R(7),
48 RC_FLOAT | RC_F(0),
49 RC_FLOAT | RC_F(1),
50 RC_FLOAT | RC_F(2),
51 RC_FLOAT | RC_F(3),
52 RC_FLOAT | RC_F(4),
53 RC_FLOAT | RC_F(5),
54 RC_FLOAT | RC_F(6),
55 RC_FLOAT | RC_F(7),
57 1 << TREG_RA,
58 1 << TREG_SP
61 static int ireg(int r)
63 if (r == TREG_RA)
64 return 1; // ra
65 if (r == TREG_SP)
66 return 2; // sp
67 assert(r >= 0 && r < 8);
68 return r + 10; // tccrX --> aX == x(10+X)
71 static int is_ireg(int r)
73 return (unsigned)r < 8 || r == TREG_RA || r == TREG_SP;
76 static int freg(int r)
78 assert(r >= 8 && r < 16);
79 return r - 8 + 10; // tccfX --> faX == f(10+X)
82 static int is_freg(int r)
84 return r >= 8 && r < 16;
87 ST_FUNC void o(unsigned int c)
89 int ind1 = ind + 4;
90 if (nocode_wanted)
91 return;
92 if (ind1 > cur_text_section->data_allocated)
93 section_realloc(cur_text_section, ind1);
94 write32le(cur_text_section->data + ind, c);
95 ind = ind1;
98 static void EIu(uint32_t opcode, uint32_t func3,
99 uint32_t rd, uint32_t rs1, uint32_t imm)
101 o(opcode | (func3 << 12) | (rd << 7) | (rs1 << 15) | (imm << 20));
104 static void ER(uint32_t opcode, uint32_t func3,
105 uint32_t rd, uint32_t rs1, uint32_t rs2, uint32_t func7)
107 o(opcode | func3 << 12 | rd << 7 | rs1 << 15 | rs2 << 20 | func7 << 25);
110 static void EI(uint32_t opcode, uint32_t func3,
111 uint32_t rd, uint32_t rs1, uint32_t imm)
113 assert(! ((imm + (1 << 11)) >> 12));
114 EIu(opcode, func3, rd, rs1, imm);
117 static void ES(uint32_t opcode, uint32_t func3,
118 uint32_t rs1, uint32_t rs2, uint32_t imm)
120 assert(! ((imm + (1 << 11)) >> 12));
121 o(opcode | (func3 << 12) | ((imm & 0x1f) << 7) | (rs1 << 15)
122 | (rs2 << 20) | ((imm >> 5) << 25));
125 // Patch all branches in list pointed to by t to branch to a:
126 ST_FUNC void gsym_addr(int t_, int a_)
128 uint32_t t = t_;
129 uint32_t a = a_;
130 while (t) {
131 unsigned char *ptr = cur_text_section->data + t;
132 uint32_t next = read32le(ptr);
133 uint32_t r = a - t, imm;
134 if ((r + (1 << 21)) & ~((1U << 22) - 2))
135 tcc_error("out-of-range branch chain");
136 imm = (((r >> 12) & 0xff) << 12)
137 | (((r >> 11) & 1) << 20)
138 | (((r >> 1) & 0x3ff) << 21)
139 | (((r >> 20) & 1) << 31);
140 write32le(ptr, r == 4 ? 0x33 : 0x6f | imm); // nop || j imm
141 t = next;
145 static int load_symofs(int r, SValue *sv, int forstore)
147 static Sym label;
148 int rr, doload = 0;
149 int fc = sv->c.i, v = sv->r & VT_VALMASK;
150 if (sv->r & VT_SYM) {
151 assert(v == VT_CONST);
152 if (sv->sym->type.t & VT_STATIC) { // XXX do this per linker relax
153 greloca(cur_text_section, sv->sym, ind,
154 R_RISCV_PCREL_HI20, sv->c.i);
155 sv->c.i = 0;
156 } else {
157 if (((unsigned)fc + (1 << 11)) >> 12)
158 tcc_error("unimp: large addend for global address (0x%llx)", sv->c.i);
159 greloca(cur_text_section, sv->sym, ind,
160 R_RISCV_GOT_HI20, 0);
161 doload = 1;
163 if (!label.v) {
164 label.v = tok_alloc(".L0 ", 4)->tok;
165 label.type.t = VT_VOID | VT_STATIC;
167 label.c = 0; /* force new local ELF symbol */
168 put_extern_sym(&label, cur_text_section, ind, 0);
169 rr = is_ireg(r) ? ireg(r) : 5;
170 o(0x17 | (rr << 7)); // auipc RR, 0 %pcrel_hi(sym)+addend
171 greloca(cur_text_section, &label, ind,
172 doload || !forstore
173 ? R_RISCV_PCREL_LO12_I : R_RISCV_PCREL_LO12_S, 0);
174 if (doload) {
175 EI(0x03, 3, rr, rr, 0); // ld RR, 0(RR)
177 } else if (v == VT_LOCAL || v == VT_LLOCAL) {
178 rr = 8; // s0
179 if (fc != sv->c.i)
180 tcc_error("unimp: store(giant local off) (0x%llx)", (long long)sv->c.i);
181 if (((unsigned)fc + (1 << 11)) >> 12) {
182 rr = is_ireg(r) ? ireg(r) : 5; // t0
183 o(0x37 | (rr << 7) | ((0x800 + fc) & 0xfffff000)); //lui RR, upper(fc)
184 ER(0x33, 0, rr, rr, 8, 0); // add RR, RR, s0
185 sv->c.i = fc << 20 >> 20;
187 } else
188 tcc_error("uhh");
189 return rr;
192 ST_FUNC void load(int r, SValue *sv)
194 int fr = sv->r;
195 int v = fr & VT_VALMASK;
196 int rr = is_ireg(r) ? ireg(r) : freg(r);
197 int fc = sv->c.i;
198 int bt = sv->type.t & VT_BTYPE;
199 int align, size = type_size(&sv->type, &align);
200 if (fr & VT_LVAL) {
201 int func3, opcode = is_freg(r) ? 0x07 : 0x03, br;
202 assert (!is_freg(r) || bt == VT_FLOAT || bt == VT_DOUBLE);
203 if (bt == VT_FUNC) /* XXX should be done in generic code */
204 size = PTR_SIZE;
205 func3 = size == 1 ? 0 : size == 2 ? 1 : size == 4 ? 2 : 3;
206 if (size < 4 && !is_float(sv->type.t) && (sv->type.t & VT_UNSIGNED))
207 func3 |= 4;
208 if (v == VT_LOCAL || (fr & VT_SYM)) {
209 br = load_symofs(r, sv, 0);
210 fc = sv->c.i;
211 } else if (v < VT_CONST) {
212 br = ireg(v);
213 /*if (((unsigned)fc + (1 << 11)) >> 12)
214 tcc_error("unimp: load(large addend) (0x%x)", fc);*/
215 fc = 0; // XXX store ofs in LVAL(reg)
216 } else if (v == VT_LLOCAL) {
217 br = load_symofs(r, sv, 0);
218 fc = sv->c.i;
219 EI(0x03, 3, rr, br, fc); // ld RR, fc(BR)
220 br = rr;
221 fc = 0;
222 } else {
223 tcc_error("unimp: load(non-local lval)");
225 EI(opcode, func3, rr, br, fc); // l[bhwd][u] / fl[wd] RR, fc(BR)
226 } else if (v == VT_CONST) {
227 int rb = 0, do32bit = 8, zext = 0;
228 assert((!is_float(sv->type.t) && is_ireg(r)) || bt == VT_LDOUBLE);
229 if (fr & VT_SYM) {
230 rb = load_symofs(r, sv, 0);
231 fc = sv->c.i;
232 do32bit = 0;
234 if (is_float(sv->type.t) && bt != VT_LDOUBLE)
235 tcc_error("unimp: load(float)");
236 if (fc != sv->c.i) {
237 int64_t si = sv->c.i;
238 uint32_t pi;
239 si >>= 32;
240 if (si != 0) {
241 pi = si;
242 if (fc < 0)
243 pi++;
244 o(0x37 | (rr << 7) | (((pi + 0x800) & 0xfffff000))); // lui RR, up(up(fc))
245 EI(0x13, 0, rr, rr, (int)pi << 20 >> 20); // addi RR, RR, lo(up(fc))
246 EI(0x13, 1, rr, rr, 12); // slli RR, RR, 12
247 EI(0x13, 0, rr, rr, (fc + (1 << 19)) >> 20); // addi RR, RR, up(lo(fc))
248 EI(0x13, 1, rr, rr, 12); // slli RR, RR, 12
249 fc = fc << 12 >> 12;
250 EI(0x13, 0, rr, rr, fc >> 8); // addi RR, RR, lo1(lo(fc))
251 EI(0x13, 1, rr, rr, 8); // slli RR, RR, 8
252 fc &= 0xff;
253 rb = rr;
254 do32bit = 0;
255 } else if (bt == VT_LLONG) {
256 /* A 32bit unsigned constant for a 64bit type.
257 lui always sign extends, so we need to do an explicit zext.*/
258 zext = 1;
261 if (((unsigned)fc + (1 << 11)) >> 12)
262 o(0x37 | (rr << 7) | ((0x800 + fc) & 0xfffff000)), rb = rr; //lui RR, upper(fc)
263 if (fc || (rr != rb) || do32bit || (fr & VT_SYM))
264 EI(0x13 | do32bit, 0, rr, rb, fc << 20 >> 20); // addi[w] R, x0|R, FC
265 if (zext) {
266 EI(0x13, 1, rr, rr, 32); // slli RR, RR, 32
267 EI(0x13, 5, rr, rr, 32); // srli RR, RR, 32
269 } else if (v == VT_LOCAL) {
270 int br = load_symofs(r, sv, 0);
271 assert(is_ireg(r));
272 fc = sv->c.i;
273 EI(0x13, 0, rr, br, fc); // addi R, s0, FC
274 } else if (v < VT_CONST) { /* reg-reg */
275 //assert(!fc); XXX support offseted regs
276 if (is_freg(r) && is_freg(v))
277 ER(0x53, 0, rr, freg(v), freg(v), bt == VT_DOUBLE ? 0x11 : 0x10); //fsgnj.[sd] RR, V, V == fmv.[sd] RR, V
278 else if (is_ireg(r) && is_ireg(v))
279 EI(0x13, 0, rr, ireg(v), 0); // addi RR, V, 0 == mv RR, V
280 else {
281 int func7 = is_ireg(r) ? 0x70 : 0x78;
282 if (size == 8)
283 func7 |= 1;
284 assert(size == 4 || size == 8);
285 o(0x53 | (rr << 7) | ((is_freg(v) ? freg(v) : ireg(v)) << 15)
286 | (func7 << 25)); // fmv.{w.x, x.w, d.x, x.d} RR, VR
288 } else if (v == VT_CMP) {
289 int op = vtop->cmp_op;
290 int a = vtop->cmp_r & 0xff;
291 int b = (vtop->cmp_r >> 8) & 0xff;
292 int inv = 0;
293 switch (op) {
294 case TOK_ULT:
295 case TOK_UGE:
296 case TOK_ULE:
297 case TOK_UGT:
298 case TOK_LT:
299 case TOK_GE:
300 case TOK_LE:
301 case TOK_GT:
302 if (op & 1) { // remove [U]GE,GT
303 inv = 1;
304 op--;
306 if ((op & 7) == 6) { // [U]LE
307 int t = a; a = b; b = t;
308 inv ^= 1;
310 ER(0x33, (op > TOK_UGT) ? 2 : 3, rr, a, b, 0); // slt[u] d, a, b
311 if (inv)
312 EI(0x13, 4, rr, rr, 1); // xori d, d, 1
313 break;
314 case TOK_NE:
315 case TOK_EQ:
316 if (rr != a || b)
317 ER(0x33, 0, rr, a, b, 0x20); // sub d, a, b
318 if (op == TOK_NE)
319 ER(0x33, 3, rr, 0, rr, 0); // sltu d, x0, d == snez d,d
320 else
321 EI(0x13, 3, rr, rr, 1); // sltiu d, d, 1 == seqz d,d
322 break;
324 } else if ((v & ~1) == VT_JMP) {
325 int t = v & 1;
326 assert(is_ireg(r));
327 EI(0x13, 0, rr, 0, t); // addi RR, x0, t
328 gjmp_addr(ind + 8);
329 gsym(fc);
330 EI(0x13, 0, rr, 0, t ^ 1); // addi RR, x0, !t
331 } else
332 tcc_error("unimp: load(non-const)");
335 ST_FUNC void store(int r, SValue *sv)
337 int fr = sv->r & VT_VALMASK;
338 int rr = is_ireg(r) ? ireg(r) : freg(r), ptrreg;
339 int fc = sv->c.i;
340 int bt = sv->type.t & VT_BTYPE;
341 int align, size = type_size(&sv->type, &align);
342 assert(!is_float(bt) || is_freg(r) || bt == VT_LDOUBLE);
343 /* long doubles are in two integer registers, but the load/store
344 primitives only deal with one, so do as if it's one reg. */
345 if (bt == VT_LDOUBLE)
346 size = align = 8;
347 if (bt == VT_STRUCT)
348 tcc_error("unimp: store(struct)");
349 if (size > 8)
350 tcc_error("unimp: large sized store");
351 assert(sv->r & VT_LVAL);
352 if (fr == VT_LOCAL || (sv->r & VT_SYM)) {
353 ptrreg = load_symofs(-1, sv, 1);
354 fc = sv->c.i;
355 } else if (fr < VT_CONST) {
356 ptrreg = ireg(fr);
357 /*if (((unsigned)fc + (1 << 11)) >> 12)
358 tcc_error("unimp: store(large addend) (0x%x)", fc);*/
359 fc = 0; // XXX support offsets regs
360 } else
361 tcc_error("implement me: %s(!local)", __FUNCTION__);
362 ES(is_freg(r) ? 0x27 : 0x23, // fs... | s...
363 size == 1 ? 0 : size == 2 ? 1 : size == 4 ? 2 : 3, // ... [wd] | [bhwd]
364 ptrreg, rr, fc); // RR, fc(base)
367 static void gcall_or_jmp(int docall)
369 int tr = docall ? 1 : 5; // ra or t0
370 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST &&
371 ((vtop->r & VT_SYM) && vtop->c.i == (int)vtop->c.i)) {
372 /* constant symbolic case -> simple relocation */
373 greloca(cur_text_section, vtop->sym, ind,
374 R_RISCV_CALL_PLT, (int)vtop->c.i);
375 o(0x17 | (tr << 7)); // auipc TR, 0 %call(func)
376 EI(0x67, 0, tr, tr, 0);// jalr TR, r(TR)
377 } else if (vtop->r < VT_CONST) {
378 int r = ireg(vtop->r);
379 EI(0x67, 0, tr, r, 0); // jalr TR, 0(R)
380 } else {
381 int r = TREG_RA;
382 load(r, vtop);
383 r = ireg(r);
384 EI(0x67, 0, tr, r, 0); // jalr TR, 0(R)
388 static void reg_pass_rec(CType *type, int *rc, int *fieldofs, int ofs)
390 if ((type->t & VT_BTYPE) == VT_STRUCT) {
391 Sym *f;
392 if (type->ref->type.t == VT_UNION)
393 rc[0] = -1;
394 else for (f = type->ref->next; f; f = f->next)
395 reg_pass_rec(&f->type, rc, fieldofs, ofs + f->c);
396 } else if (type->t & VT_ARRAY) {
397 if (type->ref->c < 0 || type->ref->c > 2)
398 rc[0] = -1;
399 else {
400 int a, sz = type_size(&type->ref->type, &a);
401 reg_pass_rec(&type->ref->type, rc, fieldofs, ofs);
402 if (rc[0] > 2 || (rc[0] == 2 && type->ref->c > 1))
403 rc[0] = -1;
404 else if (type->ref->c == 2 && rc[0] && rc[1] == RC_FLOAT) {
405 rc[++rc[0]] = RC_FLOAT;
406 fieldofs[rc[0]] = ((ofs + sz) << 4)
407 | (type->ref->type.t & VT_BTYPE);
408 } else if (type->ref->c == 2)
409 rc[0] = -1;
411 } else if (rc[0] == 2 || rc[0] < 0 || (type->t & VT_BTYPE) == VT_LDOUBLE)
412 rc[0] = -1;
413 else if (!rc[0] || rc[1] == RC_FLOAT || is_float(type->t)) {
414 rc[++rc[0]] = is_float(type->t) ? RC_FLOAT : RC_INT;
415 fieldofs[rc[0]] = (ofs << 4) | (type->t & VT_BTYPE);
416 } else
417 rc[0] = -1;
420 static void reg_pass(CType *type, int *prc, int *fieldofs, int named)
422 prc[0] = 0;
423 reg_pass_rec(type, prc, fieldofs, 0);
424 if (prc[0] <= 0 || !named) {
425 int align, size = type_size(type, &align);
426 prc[0] = (size + 7) >> 3;
427 prc[1] = prc[2] = RC_INT;
428 fieldofs[1] = (0 << 4) | (size <= 1 ? VT_BYTE : size <= 2 ? VT_SHORT : size <= 4 ? VT_INT : VT_LLONG);
429 fieldofs[2] = (8 << 4) | (size <= 9 ? VT_BYTE : size <= 10 ? VT_SHORT : size <= 12 ? VT_INT : VT_LLONG);
433 ST_FUNC void gfunc_call(int nb_args)
435 int i, align, size, areg[2];
436 int info[nb_args ? nb_args : 1];
437 int stack_adj = 0, tempspace = 0, ofs, splitofs = 0;
438 SValue *sv;
439 Sym *sa;
440 areg[0] = 0; /* int arg regs */
441 areg[1] = 8; /* float arg regs */
442 sa = vtop[-nb_args].type.ref->next;
443 for (i = 0; i < nb_args; i++) {
444 int nregs, byref = 0, tempofs;
445 int prc[3], fieldofs[3];
446 sv = &vtop[1 + i - nb_args];
447 sv->type.t &= ~VT_ARRAY; // XXX this should be done in tccgen.c
448 size = type_size(&sv->type, &align);
449 if (size > 16) {
450 if (align < XLEN)
451 align = XLEN;
452 tempspace = (tempspace + align - 1) & -align;
453 tempofs = tempspace;
454 tempspace += size;
455 size = align = 8;
456 byref = 64 | (tempofs << 7);
458 reg_pass(&sv->type, prc, fieldofs, sa != 0);
459 if (!sa && align == 2*XLEN && size <= 2*XLEN)
460 areg[0] = (areg[0] + 1) & ~1;
461 nregs = prc[0];
462 if ((prc[1] == RC_INT && areg[0] >= 8)
463 || (prc[1] == RC_FLOAT && areg[1] >= 16)
464 || (nregs == 2 && prc[1] == RC_FLOAT && prc[2] == RC_FLOAT
465 && areg[1] >= 15)
466 || (nregs == 2 && prc[1] != prc[2]
467 && (areg[1] >= 16 || areg[0] >= 8))) {
468 info[i] = 32;
469 if (align < XLEN)
470 align = XLEN;
471 stack_adj += (size + align - 1) & -align;
472 if (!sa) /* one vararg on stack forces the rest on stack */
473 areg[0] = 8, areg[1] = 16;
474 } else {
475 info[i] = areg[prc[1] - 1]++;
476 if (!byref)
477 info[i] |= (fieldofs[1] & VT_BTYPE) << 12;
478 assert(!(fieldofs[1] >> 4));
479 if (nregs == 2) {
480 if (prc[2] == RC_FLOAT || areg[0] < 8)
481 info[i] |= (1 + areg[prc[2] - 1]++) << 7;
482 else {
483 info[i] |= 16;
484 stack_adj += 8;
486 if (!byref) {
487 assert((fieldofs[2] >> 4) < 2048);
488 info[i] |= fieldofs[2] << (12 + 4); // includes offset
492 info[i] |= byref;
493 if (sa)
494 sa = sa->next;
496 stack_adj = (stack_adj + 15) & -16;
497 tempspace = (tempspace + 15) & -16;
498 if (stack_adj + tempspace) {
499 EI(0x13, 0, 2, 2, -(stack_adj + tempspace)); // addi sp, sp, -adj
500 for (i = ofs = 0; i < nb_args; i++) {
501 if (info[i] & (64 | 32)) {
502 vrotb(nb_args - i);
503 size = type_size(&vtop->type, &align);
504 if (info[i] & 64) {
505 vset(&char_pointer_type, TREG_SP, 0);
506 vpushi(stack_adj + (info[i] >> 7));
507 gen_op('+');
508 vpushv(vtop); // this replaces the old argument
509 vrott(3);
510 indir();
511 vtop->type = vtop[-1].type;
512 vswap();
513 vstore();
514 vpop();
515 size = align = 8;
517 if (info[i] & 32) {
518 if (align < XLEN)
519 align = XLEN;
520 /* Once we support offseted regs we can do this:
521 vset(&vtop->type, TREG_SP | VT_LVAL, ofs);
522 to construct the lvalue for the outgoing stack slot,
523 until then we have to jump through hoops. */
524 vset(&char_pointer_type, TREG_SP, 0);
525 ofs = (ofs + align - 1) & -align;
526 vpushi(ofs);
527 gen_op('+');
528 indir();
529 vtop->type = vtop[-1].type;
530 vswap();
531 vstore();
532 vtop->r = vtop->r2 = VT_CONST; // this arg is done
533 ofs += size;
535 vrott(nb_args - i);
536 } else if (info[i] & 16) {
537 assert(!splitofs);
538 splitofs = ofs;
539 ofs += 8;
543 for (i = 0; i < nb_args; i++) {
544 int ii = info[nb_args - 1 - i], r = ii, r2 = r;
545 if (!(r & 32)) {
546 CType origtype;
547 int loadt;
548 r &= 15;
549 r2 = r2 & 64 ? 0 : (r2 >> 7) & 31;
550 assert(r2 <= 16);
551 vrotb(i+1);
552 origtype = vtop->type;
553 size = type_size(&vtop->type, &align);
554 loadt = vtop->type.t & VT_BTYPE;
555 if (loadt == VT_STRUCT) {
556 loadt = (ii >> 12) & VT_BTYPE;
558 if (info[nb_args - 1 - i] & 16) {
559 assert(!r2);
560 r2 = 1 + TREG_RA;
562 if (loadt == VT_LDOUBLE) {
563 assert(r2);
564 r2--;
565 } else if (r2) {
566 test_lvalue();
567 vpushv(vtop);
569 vtop->type.t = loadt;
570 gv(r < 8 ? RC_R(r) : RC_F(r - 8));
571 vtop->type = origtype;
573 if (r2 && loadt != VT_LDOUBLE) {
574 r2--;
575 assert(r2 < 16 || r2 == TREG_RA);
576 vswap();
577 gaddrof();
578 vtop->type = char_pointer_type;
579 vpushi(ii >> 20);
580 gen_op('+');
581 indir();
582 vtop->type = origtype;
583 loadt = vtop->type.t & VT_BTYPE;
584 if (loadt == VT_STRUCT) {
585 loadt = (ii >> 16) & VT_BTYPE;
587 save_reg_upstack(r2, 1);
588 vtop->type.t = loadt;
589 load(r2, vtop);
590 assert(r2 < VT_CONST);
591 vtop--;
592 vtop->r2 = r2;
594 if (info[nb_args - 1 - i] & 16) {
595 ES(0x23, 3, 2, ireg(vtop->r2), splitofs); // sd t0, ofs(sp)
596 vtop->r2 = VT_CONST;
597 } else if (loadt == VT_LDOUBLE && vtop->r2 != r2) {
598 assert(vtop->r2 <= 7 && r2 <= 7);
599 /* XXX we'd like to have 'gv' move directly into
600 the right class instead of us fixing it up. */
601 EI(0x13, 0, ireg(r2), ireg(vtop->r2), 0); // mv Ra+1, RR2
602 vtop->r2 = r2;
604 vrott(i+1);
607 vrotb(nb_args + 1);
608 save_regs(nb_args + 1);
609 gcall_or_jmp(1);
610 vtop -= nb_args + 1;
611 if (stack_adj + tempspace)
612 EI(0x13, 0, 2, 2, stack_adj + tempspace); // addi sp, sp, adj
615 static int func_sub_sp_offset, num_va_regs, func_va_list_ofs;
617 ST_FUNC void gfunc_prolog(CType *func_type)
619 int i, addr, align, size;
620 int param_addr = 0;
621 int areg[2];
622 Sym *sym;
623 CType *type;
625 sym = func_type->ref;
626 func_vt = sym->type;
627 loc = -16; // for ra and s0
628 func_sub_sp_offset = ind;
629 ind += 5 * 4;
631 areg[0] = 0, areg[1] = 0;
632 addr = 0;
633 /* if the function returns by reference, then add an
634 implicit pointer parameter */
635 size = type_size(&func_vt, &align);
636 if (size > 2 * XLEN) {
637 loc -= 8;
638 func_vc = loc;
639 ES(0x23, 3, 8, 10 + areg[0]++, loc); // sd a0, loc(s0)
641 /* define parameters */
642 while ((sym = sym->next) != NULL) {
643 int byref = 0;
644 int regcount;
645 int prc[3], fieldofs[3];
646 type = &sym->type;
647 size = type_size(type, &align);
648 if (size > 2 * XLEN) {
649 type = &char_pointer_type;
650 size = align = byref = 8;
652 reg_pass(type, prc, fieldofs, 1);
653 regcount = prc[0];
654 if (areg[prc[1] - 1] >= 8
655 || (regcount == 2
656 && ((prc[1] == RC_FLOAT && prc[2] == RC_FLOAT && areg[1] >= 7)
657 || (prc[1] != prc[2] && (areg[1] >= 8 || areg[0] >= 8))))) {
658 if (align < XLEN)
659 align = XLEN;
660 addr = (addr + align - 1) & -align;
661 param_addr = addr;
662 addr += size;
663 } else {
664 loc -= regcount * 8; // XXX could reserve only 'size' bytes
665 param_addr = loc;
666 for (i = 0; i < regcount; i++) {
667 if (areg[prc[1+i] - 1] >= 8) {
668 assert(i == 1 && regcount == 2 && !(addr & 7));
669 EI(0x03, 3, 5, 8, addr); // ld t0, addr(s0)
670 addr += 8;
671 ES(0x23, 3, 8, 5, loc + i*8); // sd t0, loc(s0)
672 } else if (prc[1+i] == RC_FLOAT) {
673 ES(0x27, (size / regcount) == 4 ? 2 : 3, 8, 10 + areg[1]++, loc + (fieldofs[i+1] >> 4)); // fs[wd] FAi, loc(s0)
674 } else {
675 ES(0x23, 3, 8, 10 + areg[0]++, loc + i*8); // sd aX, loc(s0) // XXX
679 sym_push(sym->v & ~SYM_FIELD, &sym->type,
680 (byref ? VT_LLOCAL : VT_LOCAL) | lvalue_type(sym->type.t),
681 param_addr);
683 func_va_list_ofs = addr;
684 num_va_regs = 0;
685 if (func_type->ref->f.func_type == FUNC_ELLIPSIS) {
686 for (; areg[0] < 8; areg[0]++) {
687 num_va_regs++;
688 ES(0x23, 3, 8, 10 + areg[0], -8 + num_va_regs * 8); // sd aX, loc(s0)
693 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret,
694 int *ret_align, int *regsize)
696 int align, size = type_size(vt, &align), nregs;
697 int prc[3], fieldofs[3];
698 *ret_align = 1;
699 *regsize = 8;
700 if (size > 16)
701 return 0;
702 reg_pass(vt, prc, fieldofs, 1);
703 nregs = prc[0];
704 if (nregs == 2 && prc[1] != prc[2])
705 return -1; /* generic code can't deal with this case */
706 if (prc[1] == RC_FLOAT) {
707 *regsize = size / nregs;
709 ret->t = fieldofs[1] & VT_BTYPE;
710 return nregs;
713 ST_FUNC void arch_transfer_ret_regs(int aftercall)
715 int prc[3], fieldofs[3];
716 reg_pass(&vtop->type, prc, fieldofs, 1);
717 assert(prc[0] == 2 && prc[1] != prc[2] && !(fieldofs[1] >> 4));
718 assert(vtop->r == (VT_LOCAL | VT_LVAL));
719 vpushv(vtop);
720 vtop->type.t = fieldofs[1] & VT_BTYPE;
721 (aftercall ? store : load)(prc[1] == RC_INT ? REG_IRET : REG_FRET, vtop);
722 vtop->c.i += fieldofs[2] >> 4;
723 vtop->type.t = fieldofs[2] & VT_BTYPE;
724 (aftercall ? store : load)(prc[2] == RC_INT ? REG_IRET : REG_FRET, vtop);
725 vtop--;
728 ST_FUNC void gfunc_epilog(void)
730 int v, saved_ind, d, large_ofs_ind;
732 loc = (loc - num_va_regs * 8);
733 d = v = (-loc + 15) & -16;
735 if (v >= (1 << 11)) {
736 d = 16;
737 o(0x37 | (5 << 7) | ((0x800 + (v-16)) & 0xfffff000)); //lui t0, upper(v)
738 EI(0x13, 0, 5, 5, (v-16) << 20 >> 20); // addi t0, t0, lo(v)
739 ER(0x33, 0, 2, 2, 5, 0); // add sp, sp, t0
741 EI(0x03, 3, 1, 2, d - 8 - num_va_regs * 8); // ld ra, v-8(sp)
742 EI(0x03, 3, 8, 2, d - 16 - num_va_regs * 8); // ld s0, v-16(sp)
743 EI(0x13, 0, 2, 2, d); // addi sp, sp, v
744 EI(0x67, 0, 0, 1, 0); // jalr x0, 0(x1), aka ret
745 if (v >= (1 << 11)) {
746 large_ofs_ind = ind;
747 EI(0x13, 0, 8, 2, d - num_va_regs * 8); // addi s0, sp, d
748 o(0x37 | (5 << 7) | ((0x800 + (v-16)) & 0xfffff000)); //lui t0, upper(v)
749 EI(0x13, 0, 5, 5, (v-16) << 20 >> 20); // addi t0, t0, lo(v)
750 ER(0x33, 0, 2, 2, 5, 0x20); // sub sp, sp, t0
751 gjmp_addr(func_sub_sp_offset + 5*4);
753 saved_ind = ind;
755 ind = func_sub_sp_offset;
756 EI(0x13, 0, 2, 2, -d); // addi sp, sp, -d
757 ES(0x23, 3, 2, 1, d - 8 - num_va_regs * 8); // sd ra, d-8(sp)
758 ES(0x23, 3, 2, 8, d - 16 - num_va_regs * 8); // sd s0, d-16(sp)
759 if (v < (1 << 11))
760 EI(0x13, 0, 8, 2, d - num_va_regs * 8); // addi s0, sp, d
761 else
762 gjmp_addr(large_ofs_ind);
763 if ((ind - func_sub_sp_offset) != 5*4)
764 EI(0x13, 0, 0, 0, 0); // addi x0, x0, 0 == nop
765 ind = saved_ind;
768 ST_FUNC void gen_va_start(void)
770 vtop--;
771 vset(&char_pointer_type, VT_LOCAL, func_va_list_ofs);
774 ST_FUNC void gen_fill_nops(int bytes)
776 if ((bytes & 3))
777 tcc_error("alignment of code section not multiple of 4");
778 while (bytes > 0) {
779 EI(0x13, 0, 0, 0, 0); // addi x0, x0, 0 == nop
780 bytes -= 4;
784 // Generate forward branch to label:
785 ST_FUNC int gjmp(int t)
787 if (nocode_wanted)
788 return t;
789 o(t);
790 return ind - 4;
793 // Generate branch to known address:
794 ST_FUNC void gjmp_addr(int a)
796 uint32_t r = a - ind, imm;
797 if ((r + (1 << 21)) & ~((1U << 22) - 2)) {
798 o(0x17 | (5 << 7) | (((r + 0x800) & 0xfffff000))); // lui RR, up(r)
799 r = (int)r << 20 >> 20;
800 EI(0x67, 0, 0, 5, r); // jalr x0, r(t0)
801 } else {
802 imm = (((r >> 12) & 0xff) << 12)
803 | (((r >> 11) & 1) << 20)
804 | (((r >> 1) & 0x3ff) << 21)
805 | (((r >> 20) & 1) << 31);
806 o(0x6f | imm); // jal x0, imm == j imm
810 ST_FUNC int gjmp_cond(int op, int t)
812 int tmp;
813 int a = vtop->cmp_r & 0xff;
814 int b = (vtop->cmp_r >> 8) & 0xff;
815 switch (op) {
816 case TOK_ULT: op = 6; break;
817 case TOK_UGE: op = 7; break;
818 case TOK_ULE: op = 7; tmp = a; a = b; b = tmp; break;
819 case TOK_UGT: op = 6; tmp = a; a = b; b = tmp; break;
820 case TOK_LT: op = 4; break;
821 case TOK_GE: op = 5; break;
822 case TOK_LE: op = 5; tmp = a; a = b; b = tmp; break;
823 case TOK_GT: op = 4; tmp = a; a = b; b = tmp; break;
824 case TOK_NE: op = 1; break;
825 case TOK_EQ: op = 0; break;
827 o(0x63 | (op ^ 1) << 12 | a << 15 | b << 20 | 8 << 7); // bOP a,b,+4
828 return gjmp(t);
831 ST_FUNC int gjmp_append(int n, int t)
833 void *p;
834 /* insert jump list n into t */
835 if (n) {
836 uint32_t n1 = n, n2;
837 while ((n2 = read32le(p = cur_text_section->data + n1)))
838 n1 = n2;
839 write32le(p, t);
840 t = n;
842 return t;
845 static void gen_opil(int op, int ll)
847 int a, b, d;
848 int func3 = 0;
849 ll = ll ? 0 : 8;
850 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
851 int fc = vtop->c.i;
852 if (fc == vtop->c.i && !(((unsigned)fc + (1 << 11)) >> 12)) {
853 int cll = 0;
854 vswap();
855 gv(RC_INT);
856 a = ireg(vtop[0].r);
857 --vtop;
858 d = get_reg(RC_INT);
859 ++vtop;
860 vswap();
861 switch (op) {
862 case '-':
863 if (fc <= -(1 << 11))
864 break;
865 fc = -fc;
866 case '+':
867 func3 = 0; // addi d, a, fc
868 do_cop:
869 EI(0x13 | cll, func3, ireg(d), a, fc);
870 --vtop;
871 if (op >= TOK_ULT && op <= TOK_GT) {
872 vset_VT_CMP(TOK_NE);
873 vtop->cmp_r = ireg(d) | 0 << 8;
874 } else
875 vtop[0].r = d;
876 return;
877 case TOK_LE:
878 if (fc >= (1 << 11) - 1)
879 break;
880 ++fc;
881 case TOK_LT: func3 = 2; goto do_cop; // slti d, a, fc
882 case TOK_ULE:
883 if (fc >= (1 << 11) - 1)
884 break;
885 ++fc;
886 case TOK_ULT: func3 = 3; goto do_cop; // sltiu d, a, fc
887 case '^': func3 = 4; goto do_cop; // xori d, a, fc
888 case '|': func3 = 6; goto do_cop; // ori d, a, fc
889 case '&': func3 = 7; goto do_cop; // andi d, a, fc
890 case TOK_SHL: func3 = 1; fc &= 63; goto do_cop; // slli d, a, fc
891 case TOK_SHR: func3 = 5; cll = ll; fc &= 63; goto do_cop; // srli d, a, fc
892 case TOK_SAR: func3 = 5; cll = ll; fc = 1024 | (fc & 63); goto do_cop;
894 case TOK_UGE:
895 case TOK_UGT:
896 case TOK_GE:
897 case TOK_GT:
898 gen_opil(op - 1, ll);
899 vtop->cmp_op ^= 1;
900 return;
902 case TOK_NE:
903 case TOK_EQ:
904 if (fc)
905 gen_opil('-', ll), a = ireg(vtop++->r);
906 --vtop;
907 vset_VT_CMP(op);
908 vtop->cmp_r = a | 0 << 8;
909 return;
913 gv2(RC_INT, RC_INT);
914 a = ireg(vtop[-1].r);
915 b = ireg(vtop[0].r);
916 vtop -= 2;
917 d = get_reg(RC_INT);
918 vtop++;
919 vtop[0].r = d;
920 d = ireg(d);
921 switch (op) {
922 default:
923 if (op >= TOK_ULT && op <= TOK_GT) {
924 vset_VT_CMP(op);
925 vtop->cmp_r = a | b << 8;
926 break;
928 tcc_error("implement me: %s(%s)", __FUNCTION__, get_tok_str(op, NULL));
929 break;
931 case '+':
932 ER(0x33, 0, d, a, b, 0); // add d, a, b
933 break;
934 case '-':
935 ER(0x33, 0, d, a, b, 0x20); // sub d, a, b
936 break;
937 case TOK_SAR:
938 ER(0x33 | ll, 5, d, a, b, 0x20); // sra d, a, b
939 break;
940 case TOK_SHR:
941 ER(0x33 | ll, 5, d, a, b, 0); // srl d, a, b
942 break;
943 case TOK_SHL:
944 ER(0x33, 1, d, a, b, 0); // sll d, a, b
945 break;
946 case '*':
947 ER(0x33, 0, d, a, b, 1); // mul d, a, b
948 break;
949 case '/':
950 ER(0x33, 4, d, a, b, 1); // div d, a, b
951 break;
952 case '&':
953 ER(0x33, 7, d, a, b, 0); // and d, a, b
954 break;
955 case '^':
956 ER(0x33, 4, d, a, b, 0); // xor d, a, b
957 break;
958 case '|':
959 ER(0x33, 6, d, a, b, 0); // or d, a, b
960 break;
961 case '%':
962 ER(0x33, 6, d, a, b, 1); // rem d, a, b
963 break;
964 case TOK_UMOD:
965 ER(0x33, 7, d, a, b, 1); // remu d, a, b
966 break;
967 case TOK_PDIV:
968 case TOK_UDIV:
969 ER(0x33, 5, d, a, b, 1); // divu d, a, b
970 break;
974 ST_FUNC void gen_opi(int op)
976 gen_opil(op, 0);
979 ST_FUNC void gen_opl(int op)
981 gen_opil(op, 1);
984 ST_FUNC void gen_opf(int op)
986 int rs1, rs2, rd, dbl, invert;
987 if (vtop[0].type.t == VT_LDOUBLE) {
988 CType type = vtop[0].type;
989 int func = 0;
990 int cond = -1;
991 switch (op) {
992 case '*': func = TOK___multf3; break;
993 case '+': func = TOK___addtf3; break;
994 case '-': func = TOK___subtf3; break;
995 case '/': func = TOK___divtf3; break;
996 case TOK_EQ: func = TOK___eqtf2; cond = 1; break;
997 case TOK_NE: func = TOK___netf2; cond = 0; break;
998 case TOK_LT: func = TOK___lttf2; cond = 10; break;
999 case TOK_GE: func = TOK___getf2; cond = 11; break;
1000 case TOK_LE: func = TOK___letf2; cond = 12; break;
1001 case TOK_GT: func = TOK___gttf2; cond = 13; break;
1002 default: assert(0); break;
1004 vpush_global_sym(&func_old_type, func);
1005 vrott(3);
1006 gfunc_call(2);
1007 vpushi(0);
1008 vtop->r = REG_IRET;
1009 vtop->r2 = cond < 0 ? TREG_R(1) : VT_CONST;
1010 if (cond < 0)
1011 vtop->type = type;
1012 else {
1013 vpushi(0);
1014 gen_opil(op, 1);
1016 return;
1019 gv2(RC_FLOAT, RC_FLOAT);
1020 assert(vtop->type.t == VT_DOUBLE || vtop->type.t == VT_FLOAT);
1021 dbl = vtop->type.t == VT_DOUBLE;
1022 rs1 = freg(vtop[-1].r);
1023 rs2 = freg(vtop->r);
1024 vtop--;
1025 invert = 0;
1026 switch(op) {
1027 default:
1028 assert(0);
1029 case '+':
1030 op = 0; // fadd
1031 arithop:
1032 rd = get_reg(RC_FLOAT);
1033 vtop->r = rd;
1034 rd = freg(rd);
1035 ER(0x53, 7, rd, rs1, rs2, dbl | (op << 2)); // fop.[sd] RD, RS1, RS2 (dyn rm)
1036 break;
1037 case '-':
1038 op = 1; // fsub
1039 goto arithop;
1040 case '*':
1041 op = 2; // fmul
1042 goto arithop;
1043 case '/':
1044 op = 3; // fdiv
1045 goto arithop;
1046 case TOK_EQ:
1047 op = 2; // EQ
1048 cmpop:
1049 rd = get_reg(RC_INT);
1050 vtop->r = rd;
1051 rd = ireg(rd);
1052 ER(0x53, op, rd, rs1, rs2, dbl | 0x50); // fcmp.[sd] RD, RS1, RS2 (op == eq/lt/le)
1053 if (invert)
1054 EI(0x13, 4, rd, rd, 1); // xori RD, 1
1055 break;
1056 case TOK_NE:
1057 invert = 1;
1058 op = 2; // EQ
1059 goto cmpop;
1060 case TOK_LT:
1061 op = 1; // LT
1062 goto cmpop;
1063 case TOK_LE:
1064 op = 0; // LE
1065 goto cmpop;
1066 case TOK_GT:
1067 op = 1; // LT
1068 rd = rs1, rs1 = rs2, rs2 = rd;
1069 goto cmpop;
1070 case TOK_GE:
1071 op = 0; // LE
1072 rd = rs1, rs1 = rs2, rs2 = rd;
1073 goto cmpop;
1077 ST_FUNC void gen_cvt_sxtw(void)
1079 /* XXX on risc-v the registers are usually sign-extended already.
1080 Let's try to not do anything here. */
1083 ST_FUNC void gen_cvt_itof(int t)
1085 int rr = ireg(gv(RC_INT)), dr;
1086 int u = vtop->type.t & VT_UNSIGNED;
1087 int l = (vtop->type.t & VT_BTYPE) == VT_LLONG;
1088 if (t == VT_LDOUBLE) {
1089 int func = l ?
1090 (u ? TOK___floatunditf : TOK___floatditf) :
1091 (u ? TOK___floatunsitf : TOK___floatsitf);
1092 vpush_global_sym(&func_old_type, func);
1093 vrott(2);
1094 gfunc_call(1);
1095 vpushi(0);
1096 vtop->type.t = t;
1097 vtop->r = REG_IRET;
1098 vtop->r2 = TREG_R(1);
1099 } else {
1100 vtop--;
1101 dr = get_reg(RC_FLOAT);
1102 vtop++;
1103 vtop->r = dr;
1104 dr = freg(dr);
1105 EIu(0x53, 7, dr, rr, ((0x68 | (t == VT_DOUBLE ? 1 : 0)) << 5) | (u ? 1 : 0) | (l ? 2 : 0)); // fcvt.[sd].[wl][u]
1109 ST_FUNC void gen_cvt_ftoi(int t)
1111 int ft = vtop->type.t & VT_BTYPE;
1112 int l = (t & VT_BTYPE) == VT_LLONG;
1113 int u = t & VT_UNSIGNED;
1114 if (ft == VT_LDOUBLE) {
1115 int func = l ?
1116 (u ? TOK___fixunstfdi : TOK___fixtfdi) :
1117 (u ? TOK___fixunstfsi : TOK___fixtfsi);
1118 vpush_global_sym(&func_old_type, func);
1119 vrott(2);
1120 gfunc_call(1);
1121 vpushi(0);
1122 vtop->type.t = t;
1123 vtop->r = REG_IRET;
1124 } else {
1125 int rr = freg(gv(RC_FLOAT)), dr;
1126 vtop--;
1127 dr = get_reg(RC_INT);
1128 vtop++;
1129 vtop->r = dr;
1130 dr = ireg(dr);
1131 EIu(0x53, 1, dr, rr, ((0x60 | (ft == VT_DOUBLE ? 1 : 0)) << 5) | (u ? 1 : 0) | (l ? 2 : 0)); // fcvt.[wl][u].[sd] rtz
1135 ST_FUNC void gen_cvt_ftof(int dt)
1137 int st = vtop->type.t & VT_BTYPE, rs, rd;
1138 dt &= VT_BTYPE;
1139 if (st == dt)
1140 return;
1141 if (dt == VT_LDOUBLE || st == VT_LDOUBLE) {
1142 int func = (dt == VT_LDOUBLE) ?
1143 (st == VT_FLOAT ? TOK___extendsftf2 : TOK___extenddftf2) :
1144 (dt == VT_FLOAT ? TOK___trunctfsf2 : TOK___trunctfdf2);
1145 /* We can't use gfunc_call, as func_old_type works like vararg
1146 functions, and on riscv unnamed float args are passed like
1147 integers. But we really need them in the float argument registers
1148 for extendsftf2/extenddftf2. So, do it explicitely. */
1149 save_regs(1);
1150 if (dt == VT_LDOUBLE)
1151 gv(RC_F(0));
1152 else {
1153 gv(RC_R(0));
1154 assert(vtop->r2 < 7);
1155 if (vtop->r2 != 1 + vtop->r) {
1156 EI(0x13, 0, ireg(vtop->r) + 1, ireg(vtop->r2), 0); // mv Ra+1, RR2
1157 vtop->r2 = 1 + vtop->r;
1160 vpush_global_sym(&func_old_type, func);
1161 gcall_or_jmp(1);
1162 vtop -= 2;
1163 vpushi(0);
1164 vtop->type.t = dt;
1165 if (dt == VT_LDOUBLE)
1166 vtop->r = REG_IRET, vtop->r2 = REG_IRET+1;
1167 else
1168 vtop->r = REG_FRET;
1169 } else {
1170 assert (dt == VT_FLOAT || dt == VT_DOUBLE);
1171 assert (st == VT_FLOAT || st == VT_DOUBLE);
1172 rs = gv(RC_FLOAT);
1173 rd = get_reg(RC_FLOAT);
1174 if (dt == VT_DOUBLE)
1175 EI(0x53, 7, freg(rd), freg(rs), 0x21 << 5); // fcvt.d.s RD, RS (dyn rm)
1176 else
1177 EI(0x53, 7, freg(rd), freg(rs), (0x20 << 5) | 1); // fcvt.s.d RD, RS
1178 vtop->r = rd;
1182 ST_FUNC void ggoto(void)
1184 gcall_or_jmp(0);
1185 vtop--;
1188 ST_FUNC void gen_vla_sp_save(int addr)
1190 ES(0x23, 3, 8, 2, addr); // sd sp, fc(s0)
1193 ST_FUNC void gen_vla_sp_restore(int addr)
1195 EI(0x03, 3, 2, 8, addr); // ld sp, fc(s0)
1198 ST_FUNC void gen_vla_alloc(CType *type, int align)
1200 int rr = ireg(gv(RC_INT));
1201 EI(0x13, 0, rr, rr, 15); // addi RR, RR, 15
1202 EI(0x13, 7, rr, rr, -16); // andi, RR, RR, -16
1203 ER(0x33, 0, 2, 2, rr, 0x20); // sub sp, sp, rr
1204 vpop();
1206 #endif