riscv: factor load/store code
[tinycc.git] / riscv64-gen.c
blob488ab25eab1a2361776253d88e84b8ce69969023
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 EI(uint32_t opcode, uint32_t func3,
105 uint32_t rd, uint32_t rs1, uint32_t imm)
107 assert(! ((imm + (1 << 11)) >> 12));
108 EIu(opcode, func3, rd, rs1, imm);
111 static void ES(uint32_t opcode, uint32_t func3,
112 uint32_t rs1, uint32_t rs2, uint32_t imm)
114 assert(! ((imm + (1 << 11)) >> 12));
115 o(opcode | (func3 << 12) | ((imm & 0x1f) << 7) | (rs1 << 15)
116 | (rs2 << 20) | ((imm >> 5) << 25));
119 // Patch all branches in list pointed to by t to branch to a:
120 ST_FUNC void gsym_addr(int t_, int a_)
122 uint32_t t = t_;
123 uint32_t a = a_;
124 while (t) {
125 unsigned char *ptr = cur_text_section->data + t;
126 uint32_t next = read32le(ptr);
127 uint32_t r = a - t, imm;
128 if ((r + (1 << 21)) & ~((1U << 22) - 2))
129 tcc_error("out-of-range branch chain");
130 imm = (((r >> 12) & 0xff) << 12)
131 | (((r >> 11) & 1) << 20)
132 | (((r >> 1) & 0x3ff) << 21)
133 | (((r >> 20) & 1) << 31);
134 write32le(ptr, r == 4 ? 0x33 : 0x6f | imm); // nop || j imm
135 t = next;
139 static int load_symofs(int r, SValue *sv, int forstore)
141 static Sym label;
142 int rr, doload = 0;
143 int fc = sv->c.i, v = sv->r & VT_VALMASK;
144 if (sv->r & VT_SYM) {
145 assert(v == VT_CONST);
146 if (sv->sym->type.t & VT_STATIC) { // XXX do this per linker relax
147 greloca(cur_text_section, sv->sym, ind,
148 R_RISCV_PCREL_HI20, sv->c.i);
149 sv->c.i = 0;
150 } else {
151 if (((unsigned)fc + (1 << 11)) >> 12)
152 tcc_error("unimp: large addend for global address (0x%llx)", sv->c.i);
153 greloca(cur_text_section, sv->sym, ind,
154 R_RISCV_GOT_HI20, 0);
155 doload = 1;
157 if (!label.v) {
158 label.v = tok_alloc(".L0 ", 4)->tok;
159 label.type.t = VT_VOID | VT_STATIC;
161 label.c = 0; /* force new local ELF symbol */
162 put_extern_sym(&label, cur_text_section, ind, 0);
163 rr = is_ireg(r) ? ireg(r) : 5;
164 o(0x17 | (rr << 7)); // auipc RR, 0 %pcrel_hi(sym)+addend
165 greloca(cur_text_section, &label, ind,
166 doload || !forstore
167 ? R_RISCV_PCREL_LO12_I : R_RISCV_PCREL_LO12_S, 0);
168 if (doload) {
169 EI(0x03, 3, rr, rr, 0); // ld RR, 0(RR)
171 } else if (v == VT_LOCAL || v == VT_LLOCAL) {
172 rr = 8; // s0
173 if (fc != sv->c.i)
174 tcc_error("unimp: store(giant local off) (0x%llx)", (long long)sv->c.i);
175 if (((unsigned)fc + (1 << 11)) >> 12) {
176 rr = is_ireg(r) ? ireg(r) : 5; // t0
177 o(0x37 | (rr << 7) | ((0x800 + fc) & 0xfffff000)); //lui RR, upper(fc)
178 o(0x33 | (rr << 7) | (rr << 15) | (8 << 20)); // add RR, RR, s0
179 sv->c.i = fc << 20 >> 20;
181 } else
182 tcc_error("uhh");
183 return rr;
186 ST_FUNC void load(int r, SValue *sv)
188 int fr = sv->r;
189 int v = fr & VT_VALMASK;
190 int rr = is_ireg(r) ? ireg(r) : freg(r);
191 int fc = sv->c.i;
192 int bt = sv->type.t & VT_BTYPE;
193 int align, size = type_size(&sv->type, &align);
194 if (fr & VT_LVAL) {
195 int func3, opcode = is_freg(r) ? 0x07 : 0x03, br;
196 assert (!is_freg(r) || bt == VT_FLOAT || bt == VT_DOUBLE);
197 if (bt == VT_FUNC) /* XXX should be done in generic code */
198 size = PTR_SIZE;
199 func3 = size == 1 ? 0 : size == 2 ? 1 : size == 4 ? 2 : 3;
200 if (size < 4 && !is_float(sv->type.t) && (sv->type.t & VT_UNSIGNED))
201 func3 |= 4;
202 if (v == VT_LOCAL || (fr & VT_SYM)) {
203 br = load_symofs(r, sv, 0);
204 fc = sv->c.i;
205 } else if (v < VT_CONST) {
206 br = ireg(v);
207 /*if (((unsigned)fc + (1 << 11)) >> 12)
208 tcc_error("unimp: load(large addend) (0x%x)", fc);*/
209 fc = 0; // XXX store ofs in LVAL(reg)
210 } else if (v == VT_LLOCAL) {
211 br = load_symofs(r, sv, 0);
212 fc = sv->c.i;
213 EI(0x03, 3, rr, br, fc); // ld RR, fc(BR)
214 br = rr;
215 fc = 0;
216 } else {
217 tcc_error("unimp: load(non-local lval)");
219 EI(opcode, func3, rr, br, fc); // l[bhwd][u] / fl[wd] RR, fc(BR)
220 } else if (v == VT_CONST) {
221 int rb = 0, do32bit = 8, zext = 0;
222 assert((!is_float(sv->type.t) && is_ireg(r)) || bt == VT_LDOUBLE);
223 if (fr & VT_SYM) {
224 rb = load_symofs(r, sv, 0);
225 fc = sv->c.i;
226 do32bit = 0;
228 if (is_float(sv->type.t) && bt != VT_LDOUBLE)
229 tcc_error("unimp: load(float)");
230 if (fc != sv->c.i) {
231 int64_t si = sv->c.i;
232 uint32_t pi;
233 si >>= 32;
234 if (si != 0) {
235 pi = si;
236 if (fc < 0)
237 pi++;
238 o(0x37 | (rr << 7) | (((pi + 0x800) & 0xfffff000))); // lui RR, up(up(fc))
239 EI(0x13, 0, rr, rr, (int)pi << 20 >> 20); // addi RR, RR, lo(up(fc))
240 EI(0x13, 1, rr, rr, 12); // slli RR, RR, 12
241 EI(0x13, 0, rr, rr, (fc + (1 << 19)) >> 20); // addi RR, RR, up(lo(fc))
242 EI(0x13, 1, rr, rr, 12); // slli RR, RR, 12
243 fc = fc << 12 >> 12;
244 EI(0x13, 0, rr, rr, fc >> 8); // addi RR, RR, lo1(lo(fc))
245 EI(0x13, 1, rr, rr, 8); // slli RR, RR, 8
246 fc &= 0xff;
247 rb = rr;
248 do32bit = 0;
249 } else if (bt == VT_LLONG) {
250 /* A 32bit unsigned constant for a 64bit type.
251 lui always sign extends, so we need to do an explicit zext.*/
252 zext = 1;
255 if (((unsigned)fc + (1 << 11)) >> 12)
256 o(0x37 | (rr << 7) | ((0x800 + fc) & 0xfffff000)), rb = rr; //lui RR, upper(fc)
257 if (fc || (rr != rb) || do32bit || (fr & VT_SYM))
258 EI(0x13 | do32bit, 0, rr, rb, fc << 20 >> 20); // addi[w] R, x0|R, FC
259 if (zext) {
260 EI(0x13, 1, rr, rr, 32); // slli RR, RR, 32
261 EI(0x13, 5, rr, rr, 32); // srli RR, RR, 32
263 } else if (v == VT_LOCAL) {
264 int br = load_symofs(r, sv, 0);
265 assert(is_ireg(r));
266 fc = sv->c.i;
267 EI(0x13, 0, rr, br, fc); // addi R, s0, FC
268 } else if (v < VT_CONST) { /* reg-reg */
269 //assert(!fc); XXX support offseted regs
270 if (is_freg(r) && is_freg(v))
271 o(0x53 | (rr << 7) | (freg(v) << 15) | (freg(v) << 20) | ((bt == VT_DOUBLE ? 0x11 : 0x10) << 25)); //fsgnj.[sd] RR, V, V == fmv.[sd] RR, V
272 else if (is_ireg(r) && is_ireg(v))
273 EI(0x13, 0, rr, ireg(v), 0); // addi RR, V, 0 == mv RR, V
274 else {
275 int func7 = is_ireg(r) ? 0x70 : 0x78;
276 if (size == 8)
277 func7 |= 1;
278 assert(size == 4 || size == 8);
279 o(0x53 | (rr << 7) | ((is_freg(v) ? freg(v) : ireg(v)) << 15)
280 | (func7 << 25)); // fmv.{w.x, x.w, d.x, x.d} RR, VR
282 } else if (v == VT_CMP) { // we rely on cmp_r to be the correct result
283 EI(0x13, 0, rr, vtop->cmp_r, 0); // mv RR, CMP_R
284 } else if ((v & ~1) == VT_JMP) {
285 int t = v & 1;
286 assert(is_ireg(r));
287 EI(0x13, 0, rr, 0, t); // addi RR, x0, t
288 gjmp_addr(ind + 8);
289 gsym(fc);
290 EI(0x13, 0, rr, 0, t ^ 1); // addi RR, x0, !t
291 } else
292 tcc_error("unimp: load(non-const)");
295 ST_FUNC void store(int r, SValue *sv)
297 int fr = sv->r & VT_VALMASK;
298 int rr = is_ireg(r) ? ireg(r) : freg(r), ptrreg;
299 int fc = sv->c.i;
300 int bt = sv->type.t & VT_BTYPE;
301 int align, size = type_size(&sv->type, &align);
302 assert(!is_float(bt) || is_freg(r) || bt == VT_LDOUBLE);
303 /* long doubles are in two integer registers, but the load/store
304 primitives only deal with one, so do as if it's one reg. */
305 if (bt == VT_LDOUBLE)
306 size = align = 8;
307 if (bt == VT_STRUCT)
308 tcc_error("unimp: store(struct)");
309 if (size > 8)
310 tcc_error("unimp: large sized store");
311 assert(sv->r & VT_LVAL);
312 if (fr == VT_LOCAL || (sv->r & VT_SYM)) {
313 ptrreg = load_symofs(-1, sv, 1);
314 fc = sv->c.i;
315 } else if (fr < VT_CONST) {
316 ptrreg = ireg(fr);
317 /*if (((unsigned)fc + (1 << 11)) >> 12)
318 tcc_error("unimp: store(large addend) (0x%x)", fc);*/
319 fc = 0; // XXX support offsets regs
320 } else
321 tcc_error("implement me: %s(!local)", __FUNCTION__);
322 ES(is_freg(r) ? 0x27 : 0x23, // fs... | s...
323 size == 1 ? 0 : size == 2 ? 1 : size == 4 ? 2 : 3, // ... [wd] | [bhwd]
324 ptrreg, rr, fc); // RR, fc(base)
327 static void gcall_or_jmp(int docall)
329 int tr = docall ? 1 : 5; // ra or t0
330 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST &&
331 ((vtop->r & VT_SYM) && vtop->c.i == (int)vtop->c.i)) {
332 /* constant symbolic case -> simple relocation */
333 greloca(cur_text_section, vtop->sym, ind,
334 R_RISCV_CALL_PLT, (int)vtop->c.i);
335 o(0x17 | (tr << 7)); // auipc TR, 0 %call(func)
336 EI(0x67, 0, tr, tr, 0);// jalr TR, r(TR)
337 } else if (vtop->r < VT_CONST) {
338 int r = ireg(vtop->r);
339 EI(0x67, 0, tr, r, 0); // jalr TR, 0(R)
340 } else {
341 int r = TREG_RA;
342 load(r, vtop);
343 r = ireg(r);
344 EI(0x67, 0, tr, r, 0); // jalr TR, 0(R)
348 static void reg_pass_rec(CType *type, int *rc, int *fieldofs, int ofs)
350 if ((type->t & VT_BTYPE) == VT_STRUCT) {
351 Sym *f;
352 if (type->ref->type.t == VT_UNION)
353 rc[0] = -1;
354 else for (f = type->ref->next; f; f = f->next)
355 reg_pass_rec(&f->type, rc, fieldofs, ofs + f->c);
356 } else if (type->t & VT_ARRAY) {
357 if (type->ref->c < 0 || type->ref->c > 2)
358 rc[0] = -1;
359 else {
360 int a, sz = type_size(&type->ref->type, &a);
361 reg_pass_rec(&type->ref->type, rc, fieldofs, ofs);
362 if (rc[0] > 2 || (rc[0] == 2 && type->ref->c > 1))
363 rc[0] = -1;
364 else if (type->ref->c == 2 && rc[0] && rc[1] == RC_FLOAT) {
365 rc[++rc[0]] = RC_FLOAT;
366 fieldofs[rc[0]] = ((ofs + sz) << 4)
367 | (type->ref->type.t & VT_BTYPE);
368 } else if (type->ref->c == 2)
369 rc[0] = -1;
371 } else if (rc[0] == 2 || rc[0] < 0 || (type->t & VT_BTYPE) == VT_LDOUBLE)
372 rc[0] = -1;
373 else if (!rc[0] || rc[1] == RC_FLOAT || is_float(type->t)) {
374 rc[++rc[0]] = is_float(type->t) ? RC_FLOAT : RC_INT;
375 fieldofs[rc[0]] = (ofs << 4) | (type->t & VT_BTYPE);
376 } else
377 rc[0] = -1;
380 static void reg_pass(CType *type, int *prc, int *fieldofs, int named)
382 prc[0] = 0;
383 reg_pass_rec(type, prc, fieldofs, 0);
384 if (prc[0] <= 0 || !named) {
385 int align, size = type_size(type, &align);
386 prc[0] = (size + 7) >> 3;
387 prc[1] = prc[2] = RC_INT;
388 fieldofs[1] = (0 << 4) | (size <= 1 ? VT_BYTE : size <= 2 ? VT_SHORT : size <= 4 ? VT_INT : VT_LLONG);
389 fieldofs[2] = (8 << 4) | (size <= 9 ? VT_BYTE : size <= 10 ? VT_SHORT : size <= 12 ? VT_INT : VT_LLONG);
393 ST_FUNC void gfunc_call(int nb_args)
395 int i, align, size, areg[2];
396 int info[nb_args ? nb_args : 1];
397 int stack_adj = 0, tempspace = 0, ofs, splitofs = 0;
398 SValue *sv;
399 Sym *sa;
400 areg[0] = 0; /* int arg regs */
401 areg[1] = 8; /* float arg regs */
402 sa = vtop[-nb_args].type.ref->next;
403 for (i = 0; i < nb_args; i++) {
404 int nregs, byref = 0, tempofs;
405 int prc[3], fieldofs[3];
406 sv = &vtop[1 + i - nb_args];
407 sv->type.t &= ~VT_ARRAY; // XXX this should be done in tccgen.c
408 size = type_size(&sv->type, &align);
409 if (size > 16) {
410 if (align < XLEN)
411 align = XLEN;
412 tempspace = (tempspace + align - 1) & -align;
413 tempofs = tempspace;
414 tempspace += size;
415 size = align = 8;
416 byref = 64 | (tempofs << 7);
418 reg_pass(&sv->type, prc, fieldofs, sa != 0);
419 if (!sa && align == 2*XLEN && size <= 2*XLEN)
420 areg[0] = (areg[0] + 1) & ~1;
421 nregs = prc[0];
422 if ((prc[1] == RC_INT && areg[0] >= 8)
423 || (prc[1] == RC_FLOAT && areg[1] >= 16)
424 || (nregs == 2 && prc[1] == RC_FLOAT && prc[2] == RC_FLOAT
425 && areg[1] >= 15)
426 || (nregs == 2 && prc[1] != prc[2]
427 && (areg[1] >= 16 || areg[0] >= 8))) {
428 info[i] = 32;
429 if (align < XLEN)
430 align = XLEN;
431 stack_adj += (size + align - 1) & -align;
432 if (!sa) /* one vararg on stack forces the rest on stack */
433 areg[0] = 8, areg[1] = 16;
434 } else {
435 info[i] = areg[prc[1] - 1]++;
436 if (!byref)
437 info[i] |= (fieldofs[1] & VT_BTYPE) << 12;
438 assert(!(fieldofs[1] >> 4));
439 if (nregs == 2) {
440 if (prc[2] == RC_FLOAT || areg[0] < 8)
441 info[i] |= (1 + areg[prc[2] - 1]++) << 7;
442 else {
443 info[i] |= 16;
444 stack_adj += 8;
446 if (!byref) {
447 assert((fieldofs[2] >> 4) < 2048);
448 info[i] |= fieldofs[2] << (12 + 4); // includes offset
452 info[i] |= byref;
453 if (sa)
454 sa = sa->next;
456 stack_adj = (stack_adj + 15) & -16;
457 tempspace = (tempspace + 15) & -16;
458 if (stack_adj + tempspace) {
459 EI(0x13, 0, 2, 2, -(stack_adj + tempspace)); // addi sp, sp, -adj
460 for (i = ofs = 0; i < nb_args; i++) {
461 if (info[i] & (64 | 32)) {
462 vrotb(nb_args - i);
463 size = type_size(&vtop->type, &align);
464 if (info[i] & 64) {
465 vset(&char_pointer_type, TREG_SP, 0);
466 vpushi(stack_adj + (info[i] >> 7));
467 gen_op('+');
468 vpushv(vtop); // this replaces the old argument
469 vrott(3);
470 indir();
471 vtop->type = vtop[-1].type;
472 vswap();
473 vstore();
474 vpop();
475 size = align = 8;
477 if (info[i] & 32) {
478 if (align < XLEN)
479 align = XLEN;
480 /* Once we support offseted regs we can do this:
481 vset(&vtop->type, TREG_SP | VT_LVAL, ofs);
482 to construct the lvalue for the outgoing stack slot,
483 until then we have to jump through hoops. */
484 vset(&char_pointer_type, TREG_SP, 0);
485 ofs = (ofs + align - 1) & -align;
486 vpushi(ofs);
487 gen_op('+');
488 indir();
489 vtop->type = vtop[-1].type;
490 vswap();
491 vstore();
492 vtop->r = vtop->r2 = VT_CONST; // this arg is done
493 ofs += size;
495 vrott(nb_args - i);
496 } else if (info[i] & 16) {
497 assert(!splitofs);
498 splitofs = ofs;
499 ofs += 8;
503 for (i = 0; i < nb_args; i++) {
504 int ii = info[nb_args - 1 - i], r = ii, r2 = r;
505 if (!(r & 32)) {
506 CType origtype;
507 int loadt;
508 r &= 15;
509 r2 = r2 & 64 ? 0 : (r2 >> 7) & 31;
510 assert(r2 <= 16);
511 vrotb(i+1);
512 origtype = vtop->type;
513 size = type_size(&vtop->type, &align);
514 loadt = vtop->type.t & VT_BTYPE;
515 if (loadt == VT_STRUCT) {
516 loadt = (ii >> 12) & VT_BTYPE;
518 if (info[nb_args - 1 - i] & 16) {
519 assert(!r2);
520 r2 = 1 + TREG_RA;
522 if (loadt == VT_LDOUBLE) {
523 assert(r2);
524 r2--;
525 } else if (r2) {
526 test_lvalue();
527 vpushv(vtop);
529 vtop->type.t = loadt;
530 gv(r < 8 ? RC_R(r) : RC_F(r - 8));
531 vtop->type = origtype;
533 if (r2 && loadt != VT_LDOUBLE) {
534 r2--;
535 assert(r2 < 16 || r2 == TREG_RA);
536 vswap();
537 gaddrof();
538 vtop->type = char_pointer_type;
539 vpushi(ii >> 20);
540 gen_op('+');
541 indir();
542 vtop->type = origtype;
543 loadt = vtop->type.t & VT_BTYPE;
544 if (loadt == VT_STRUCT) {
545 loadt = (ii >> 16) & VT_BTYPE;
547 save_reg_upstack(r2, 1);
548 vtop->type.t = loadt;
549 load(r2, vtop);
550 assert(r2 < VT_CONST);
551 vtop--;
552 vtop->r2 = r2;
554 if (info[nb_args - 1 - i] & 16) {
555 ES(0x23, 3, 2, ireg(vtop->r2), splitofs); // sd t0, ofs(sp)
556 vtop->r2 = VT_CONST;
557 } else if (loadt == VT_LDOUBLE && vtop->r2 != r2) {
558 assert(vtop->r2 <= 7 && r2 <= 7);
559 /* XXX we'd like to have 'gv' move directly into
560 the right class instead of us fixing it up. */
561 EI(0x13, 0, ireg(r2), ireg(vtop->r2), 0); // mv Ra+1, RR2
562 vtop->r2 = r2;
564 vrott(i+1);
567 vrotb(nb_args + 1);
568 save_regs(nb_args + 1);
569 gcall_or_jmp(1);
570 vtop -= nb_args + 1;
571 if (stack_adj + tempspace)
572 EI(0x13, 0, 2, 2, stack_adj + tempspace); // addi sp, sp, adj
575 static int func_sub_sp_offset, num_va_regs, func_va_list_ofs;
577 ST_FUNC void gfunc_prolog(CType *func_type)
579 int i, addr, align, size;
580 int param_addr = 0;
581 int areg[2];
582 Sym *sym;
583 CType *type;
585 sym = func_type->ref;
586 func_vt = sym->type;
587 loc = -16; // for ra and s0
588 func_sub_sp_offset = ind;
589 ind += 5 * 4;
591 areg[0] = 0, areg[1] = 0;
592 addr = 0;
593 /* if the function returns by reference, then add an
594 implicit pointer parameter */
595 size = type_size(&func_vt, &align);
596 if (size > 2 * XLEN) {
597 loc -= 8;
598 func_vc = loc;
599 ES(0x23, 3, 8, 10 + areg[0]++, loc); // sd a0, loc(s0)
601 /* define parameters */
602 while ((sym = sym->next) != NULL) {
603 int byref = 0;
604 int regcount;
605 int prc[3], fieldofs[3];
606 type = &sym->type;
607 size = type_size(type, &align);
608 if (size > 2 * XLEN) {
609 type = &char_pointer_type;
610 size = align = byref = 8;
612 reg_pass(type, prc, fieldofs, 1);
613 regcount = prc[0];
614 if (areg[prc[1] - 1] >= 8
615 || (regcount == 2
616 && ((prc[1] == RC_FLOAT && prc[2] == RC_FLOAT && areg[1] >= 7)
617 || (prc[1] != prc[2] && (areg[1] >= 8 || areg[0] >= 8))))) {
618 if (align < XLEN)
619 align = XLEN;
620 addr = (addr + align - 1) & -align;
621 param_addr = addr;
622 addr += size;
623 } else {
624 loc -= regcount * 8; // XXX could reserve only 'size' bytes
625 param_addr = loc;
626 for (i = 0; i < regcount; i++) {
627 if (areg[prc[1+i] - 1] >= 8) {
628 assert(i == 1 && regcount == 2 && !(addr & 7));
629 EI(0x03, 3, 5, 8, addr); // ld t0, addr(s0)
630 addr += 8;
631 ES(0x23, 3, 8, 5, loc + i*8); // sd t0, loc(s0)
632 } else if (prc[1+i] == RC_FLOAT) {
633 ES(0x27, (size / regcount) == 4 ? 2 : 3, 8, 10 + areg[1]++, loc + (fieldofs[i+1] >> 4)); // fs[wd] FAi, loc(s0)
634 } else {
635 ES(0x23, 3, 8, 10 + areg[0]++, loc + i*8); // sd aX, loc(s0) // XXX
639 sym_push(sym->v & ~SYM_FIELD, &sym->type,
640 (byref ? VT_LLOCAL : VT_LOCAL) | lvalue_type(sym->type.t),
641 param_addr);
643 func_va_list_ofs = addr;
644 num_va_regs = 0;
645 if (func_type->ref->f.func_type == FUNC_ELLIPSIS) {
646 for (; areg[0] < 8; areg[0]++) {
647 num_va_regs++;
648 ES(0x23, 3, 8, 10 + areg[0], -8 + num_va_regs * 8); // sd aX, loc(s0)
653 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret,
654 int *ret_align, int *regsize)
656 int align, size = type_size(vt, &align), nregs;
657 int prc[3], fieldofs[3];
658 *ret_align = 1;
659 *regsize = 8;
660 if (size > 16)
661 return 0;
662 reg_pass(vt, prc, fieldofs, 1);
663 nregs = prc[0];
664 if (nregs == 2 && prc[1] != prc[2])
665 return -1; /* generic code can't deal with this case */
666 if (prc[1] == RC_FLOAT) {
667 *regsize = size / nregs;
669 ret->t = fieldofs[1] & VT_BTYPE;
670 return nregs;
673 ST_FUNC void arch_transfer_ret_regs(int aftercall)
675 int prc[3], fieldofs[3];
676 reg_pass(&vtop->type, prc, fieldofs, 1);
677 assert(prc[0] == 2 && prc[1] != prc[2] && !(fieldofs[1] >> 4));
678 assert(vtop->r == (VT_LOCAL | VT_LVAL));
679 vpushv(vtop);
680 vtop->type.t = fieldofs[1] & VT_BTYPE;
681 (aftercall ? store : load)(prc[1] == RC_INT ? REG_IRET : REG_FRET, vtop);
682 vtop->c.i += fieldofs[2] >> 4;
683 vtop->type.t = fieldofs[2] & VT_BTYPE;
684 (aftercall ? store : load)(prc[2] == RC_INT ? REG_IRET : REG_FRET, vtop);
685 vtop--;
688 ST_FUNC void gfunc_epilog(void)
690 int v, saved_ind, d, large_ofs_ind;
692 loc = (loc - num_va_regs * 8);
693 d = v = (-loc + 15) & -16;
695 if (v >= (1 << 11)) {
696 d = 16;
697 o(0x37 | (5 << 7) | ((0x800 + (v-16)) & 0xfffff000)); //lui t0, upper(v)
698 EI(0x13, 0, 5, 5, (v-16) << 20 >> 20); // addi t0, t0, lo(v)
699 o(0x33 | (2 << 7) | (2 << 15) | (5 << 20)); //add sp, sp, t0
701 EI(0x03, 3, 1, 2, d - 8 - num_va_regs * 8); // ld ra, v-8(sp)
702 EI(0x03, 3, 8, 2, d - 16 - num_va_regs * 8); // ld s0, v-16(sp)
703 EI(0x13, 0, 2, 2, d); // addi sp, sp, v
704 EI(0x67, 0, 0, 1, 0); // jalr x0, 0(x1), aka ret
705 if (v >= (1 << 11)) {
706 large_ofs_ind = ind;
707 EI(0x13, 0, 8, 2, d - num_va_regs * 8); // addi s0, sp, d
708 o(0x37 | (5 << 7) | ((0x800 + (v-16)) & 0xfffff000)); //lui t0, upper(v)
709 EI(0x13, 0, 5, 5, (v-16) << 20 >> 20); // addi t0, t0, lo(v)
710 o(0x33 | (2 << 7) | (2 << 15) | (5 << 20) | (0x20 << 25)); //sub sp, sp, t0
711 gjmp_addr(func_sub_sp_offset + 5*4);
713 saved_ind = ind;
715 ind = func_sub_sp_offset;
716 EI(0x13, 0, 2, 2, -d); // addi sp, sp, -d
717 ES(0x23, 3, 2, 1, d - 8 - num_va_regs * 8); // sd ra, d-8(sp)
718 ES(0x23, 3, 2, 8, d - 16 - num_va_regs * 8); // sd s0, d-16(sp)
719 if (v < (1 << 11))
720 EI(0x13, 0, 8, 2, d - num_va_regs * 8); // addi s0, sp, d
721 else
722 gjmp_addr(large_ofs_ind);
723 if ((ind - func_sub_sp_offset) != 5*4)
724 EI(0x13, 0, 0, 0, 0); // addi x0, x0, 0 == nop
725 ind = saved_ind;
728 ST_FUNC void gen_va_start(void)
730 vtop--;
731 vset(&char_pointer_type, VT_LOCAL, func_va_list_ofs);
734 ST_FUNC void gen_fill_nops(int bytes)
736 if ((bytes & 3))
737 tcc_error("alignment of code section not multiple of 4");
738 while (bytes > 0) {
739 EI(0x13, 0, 0, 0, 0); // addi x0, x0, 0 == nop
740 bytes -= 4;
744 // Generate forward branch to label:
745 ST_FUNC int gjmp(int t)
747 if (nocode_wanted)
748 return t;
749 o(t);
750 return ind - 4;
753 // Generate branch to known address:
754 ST_FUNC void gjmp_addr(int a)
756 uint32_t r = a - ind, imm;
757 if ((r + (1 << 21)) & ~((1U << 22) - 2)) {
758 o(0x17 | (5 << 7) | (((r + 0x800) & 0xfffff000))); // lui RR, up(r)
759 r = (int)r << 20 >> 20;
760 EI(0x67, 0, 0, 5, r); // jalr x0, r(t0)
761 } else {
762 imm = (((r >> 12) & 0xff) << 12)
763 | (((r >> 11) & 1) << 20)
764 | (((r >> 1) & 0x3ff) << 21)
765 | (((r >> 20) & 1) << 31);
766 o(0x6f | imm); // jal x0, imm == j imm
770 ST_FUNC int gjmp_cond(int op, int t)
772 int inv = op & 1;
773 assert(op == TOK_EQ || op == TOK_NE);
774 assert(vtop->cmp_r >= 10 && vtop->cmp_r < 18);
775 o(0x63 | (!inv << 12) | (vtop->cmp_r << 15) | (8 << 7)); // bne/beq x0,r,+4
776 return gjmp(t);
779 ST_FUNC int gjmp_append(int n, int t)
781 void *p;
782 /* insert jump list n into t */
783 if (n) {
784 uint32_t n1 = n, n2;
785 while ((n2 = read32le(p = cur_text_section->data + n1)))
786 n1 = n2;
787 write32le(p, t);
788 t = n;
790 return t;
793 static void gen_opil(int op, int ll)
795 int a, b, d;
796 int inv = 0;
797 int func3 = 0, func7 = 0;
798 /* XXX We could special-case some constant args. */
799 gv2(RC_INT, RC_INT);
800 a = ireg(vtop[-1].r);
801 b = ireg(vtop[0].r);
802 vtop -= 2;
803 d = get_reg(RC_INT);
804 vtop++;
805 vtop[0].r = d;
806 d = ireg(d);
807 ll = ll ? 0 : 8;
808 switch (op) {
809 default:
810 tcc_error("implement me: %s(%s)", __FUNCTION__, get_tok_str(op, NULL));
812 case '+':
813 o(0x33 | (d << 7) | (a << 15) | (b << 20)); // add d, a, b
814 break;
815 case '-':
816 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (0x20 << 25)); //sub d, a, b
817 break;
818 case TOK_SAR:
819 o(0x33 | ll | (d << 7) | (a << 15) | (b << 20) | (5 << 12) | (1 << 30)); //sra d, a, b
820 break;
821 case TOK_SHR:
822 o(0x33 | ll | (d << 7) | (a << 15) | (b << 20) | (5 << 12)); //srl d, a, b
823 break;
824 case TOK_SHL:
825 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (1 << 12)); //sll d, a, b
826 break;
827 case '*':
828 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (0x01 << 25)); //mul d, a, b
829 break;
830 case '/':
831 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (0x01 << 25) | (4 << 12)); //div d, a, b
832 break;
833 case '&':
834 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (7 << 12)); // and d, a, b
835 break;
836 case '^':
837 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (4 << 12)); // xor d, a, b
838 break;
839 case '|':
840 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (6 << 12)); // or d, a, b
841 break;
842 case '%':
843 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (0x01 << 25) | (6 << 12)); //rem d, a, b
844 break;
845 case TOK_UMOD:
846 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (0x01 << 25) | (7 << 12)); //remu d, a, b
847 break;
848 case TOK_PDIV:
849 case TOK_UDIV:
850 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (0x01 << 25) | (5 << 12)); //divu d, a, b
851 break;
853 case TOK_ULT:
854 case TOK_UGE:
855 case TOK_ULE:
856 case TOK_UGT:
857 case TOK_LT:
858 case TOK_GE:
859 case TOK_LE:
860 case TOK_GT:
861 if (op & 1) { // remove [U]GE,GT
862 inv = 1;
863 op--;
865 if ((op & 7) == 6) { // [U]LE
866 int t = a; a = b; b = t;
867 inv ^= 1;
869 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (((op > TOK_UGT) ? 2 : 3) << 12)); // slt[u] d, a, b
870 if (inv)
871 EI(0x13, 4, d, d, 1); // xori d, d, 1
872 vset_VT_CMP(TOK_NE);
873 vtop->cmp_r = d;
874 break;
875 case TOK_NE:
876 case TOK_EQ:
877 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (0x20 << 25)); // sub d, a, b
878 if (op == TOK_NE)
879 o(0x33 | (3 << 12) | (d << 7) | (0 << 15) | (d << 20)); // sltu d, x0, d == snez d,d
880 else
881 EI(0x13, 3, d, d, 1); // sltiu d, d, 1 == seqz d,d
882 vset_VT_CMP(TOK_NE);
883 vtop->cmp_r = d;
884 break;
888 ST_FUNC void gen_opi(int op)
890 gen_opil(op, 0);
893 ST_FUNC void gen_opl(int op)
895 gen_opil(op, 1);
898 ST_FUNC void gen_opf(int op)
900 int rs1, rs2, rd, dbl, invert;
901 if (vtop[0].type.t == VT_LDOUBLE) {
902 CType type = vtop[0].type;
903 int func = 0;
904 int cond = -1;
905 switch (op) {
906 case '*': func = TOK___multf3; break;
907 case '+': func = TOK___addtf3; break;
908 case '-': func = TOK___subtf3; break;
909 case '/': func = TOK___divtf3; break;
910 case TOK_EQ: func = TOK___eqtf2; cond = 1; break;
911 case TOK_NE: func = TOK___netf2; cond = 0; break;
912 case TOK_LT: func = TOK___lttf2; cond = 10; break;
913 case TOK_GE: func = TOK___getf2; cond = 11; break;
914 case TOK_LE: func = TOK___letf2; cond = 12; break;
915 case TOK_GT: func = TOK___gttf2; cond = 13; break;
916 default: assert(0); break;
918 vpush_global_sym(&func_old_type, func);
919 vrott(3);
920 gfunc_call(2);
921 vpushi(0);
922 vtop->r = REG_IRET;
923 vtop->r2 = cond < 0 ? TREG_R(1) : VT_CONST;
924 if (cond < 0)
925 vtop->type = type;
926 else {
927 vpushi(0);
928 gen_opil(op, 1);
930 return;
933 gv2(RC_FLOAT, RC_FLOAT);
934 assert(vtop->type.t == VT_DOUBLE || vtop->type.t == VT_FLOAT);
935 dbl = vtop->type.t == VT_DOUBLE;
936 rs1 = freg(vtop[-1].r);
937 rs2 = freg(vtop->r);
938 vtop--;
939 invert = 0;
940 switch(op) {
941 default:
942 assert(0);
943 case '+':
944 op = 0; // fadd
945 arithop:
946 rd = get_reg(RC_FLOAT);
947 vtop->r = rd;
948 rd = freg(rd);
949 o(0x53 | (rd << 7) | (rs1 << 15) | (rs2 << 20) | (7 << 12) | (dbl << 25) | (op << 27)); // fop.[sd] RD, RS1, RS2 (dyn rm)
950 break;
951 case '-':
952 op = 1; // fsub
953 goto arithop;
954 case '*':
955 op = 2; // fmul
956 goto arithop;
957 case '/':
958 op = 3; // fdiv
959 goto arithop;
960 case TOK_EQ:
961 op = 2; // EQ
962 cmpop:
963 rd = get_reg(RC_INT);
964 vtop->r = rd;
965 rd = ireg(rd);
966 o(0x53 | (rd << 7) | (rs1 << 15) | (rs2 << 20) | (op << 12) | (dbl << 25) | (0x14 << 27)); // fcmp.[sd] RD, RS1, RS2 (op == eq/lt/le)
967 if (invert)
968 EI(0x13, 4, rd, rd, 1); // xori RD, 1
969 break;
970 case TOK_NE:
971 invert = 1;
972 op = 2; // EQ
973 goto cmpop;
974 case TOK_LT:
975 op = 1; // LT
976 goto cmpop;
977 case TOK_LE:
978 op = 0; // LE
979 goto cmpop;
980 case TOK_GT:
981 op = 1; // LT
982 rd = rs1, rs1 = rs2, rs2 = rd;
983 goto cmpop;
984 case TOK_GE:
985 op = 0; // LE
986 rd = rs1, rs1 = rs2, rs2 = rd;
987 goto cmpop;
991 ST_FUNC void gen_cvt_sxtw(void)
993 /* XXX on risc-v the registers are usually sign-extended already.
994 Let's try to not do anything here. */
997 ST_FUNC void gen_cvt_itof(int t)
999 int rr = ireg(gv(RC_INT)), dr;
1000 int u = vtop->type.t & VT_UNSIGNED;
1001 int l = (vtop->type.t & VT_BTYPE) == VT_LLONG;
1002 if (t == VT_LDOUBLE) {
1003 int func = l ?
1004 (u ? TOK___floatunditf : TOK___floatditf) :
1005 (u ? TOK___floatunsitf : TOK___floatsitf);
1006 vpush_global_sym(&func_old_type, func);
1007 vrott(2);
1008 gfunc_call(1);
1009 vpushi(0);
1010 vtop->type.t = t;
1011 vtop->r = REG_IRET;
1012 vtop->r2 = TREG_R(1);
1013 } else {
1014 vtop--;
1015 dr = get_reg(RC_FLOAT);
1016 vtop++;
1017 vtop->r = dr;
1018 dr = freg(dr);
1019 EIu(0x53, 7, dr, rr, ((0x68 | (t == VT_DOUBLE ? 1 : 0)) << 5) | (u ? 1 : 0) | (l ? 2 : 0)); // fcvt.[sd].[wl][u]
1023 ST_FUNC void gen_cvt_ftoi(int t)
1025 int ft = vtop->type.t & VT_BTYPE;
1026 int l = (t & VT_BTYPE) == VT_LLONG;
1027 int u = t & VT_UNSIGNED;
1028 if (ft == VT_LDOUBLE) {
1029 int func = l ?
1030 (u ? TOK___fixunstfdi : TOK___fixtfdi) :
1031 (u ? TOK___fixunstfsi : TOK___fixtfsi);
1032 vpush_global_sym(&func_old_type, func);
1033 vrott(2);
1034 gfunc_call(1);
1035 vpushi(0);
1036 vtop->type.t = t;
1037 vtop->r = REG_IRET;
1038 } else {
1039 int rr = freg(gv(RC_FLOAT)), dr;
1040 vtop--;
1041 dr = get_reg(RC_INT);
1042 vtop++;
1043 vtop->r = dr;
1044 dr = ireg(dr);
1045 EIu(0x53, 1, dr, rr, ((0x60 | (ft == VT_DOUBLE ? 1 : 0)) << 5) | (u ? 1 : 0) | (l ? 2 : 0)); // fcvt.[wl][u].[sd] rtz
1049 ST_FUNC void gen_cvt_ftof(int dt)
1051 int st = vtop->type.t & VT_BTYPE, rs, rd;
1052 dt &= VT_BTYPE;
1053 if (st == dt)
1054 return;
1055 if (dt == VT_LDOUBLE || st == VT_LDOUBLE) {
1056 int func = (dt == VT_LDOUBLE) ?
1057 (st == VT_FLOAT ? TOK___extendsftf2 : TOK___extenddftf2) :
1058 (dt == VT_FLOAT ? TOK___trunctfsf2 : TOK___trunctfdf2);
1059 /* We can't use gfunc_call, as func_old_type works like vararg
1060 functions, and on riscv unnamed float args are passed like
1061 integers. But we really need them in the float argument registers
1062 for extendsftf2/extenddftf2. So, do it explicitely. */
1063 save_regs(1);
1064 if (dt == VT_LDOUBLE)
1065 gv(RC_F(0));
1066 else {
1067 gv(RC_R(0));
1068 assert(vtop->r2 < 7);
1069 if (vtop->r2 != 1 + vtop->r) {
1070 EI(0x13, 0, ireg(vtop->r) + 1, ireg(vtop->r2), 0); // mv Ra+1, RR2
1071 vtop->r2 = 1 + vtop->r;
1074 vpush_global_sym(&func_old_type, func);
1075 gcall_or_jmp(1);
1076 vtop -= 2;
1077 vpushi(0);
1078 vtop->type.t = dt;
1079 if (dt == VT_LDOUBLE)
1080 vtop->r = REG_IRET, vtop->r2 = REG_IRET+1;
1081 else
1082 vtop->r = REG_FRET;
1083 } else {
1084 assert (dt == VT_FLOAT || dt == VT_DOUBLE);
1085 assert (st == VT_FLOAT || st == VT_DOUBLE);
1086 rs = gv(RC_FLOAT);
1087 rd = get_reg(RC_FLOAT);
1088 if (dt == VT_DOUBLE)
1089 EI(0x53, 7, freg(rd), freg(rs), 0x21 << 5); // fcvt.d.s RD, RS (dyn rm)
1090 else
1091 EI(0x53, 7, freg(rd), freg(rs), (0x20 << 5) | 1); // fcvt.s.d RD, RS
1092 vtop->r = rd;
1096 ST_FUNC void ggoto(void)
1098 gcall_or_jmp(0);
1099 vtop--;
1102 ST_FUNC void gen_vla_sp_save(int addr)
1104 ES(0x23, 3, 8, 2, addr); // sd sp, fc(s0)
1107 ST_FUNC void gen_vla_sp_restore(int addr)
1109 EI(0x03, 3, 2, 8, addr); // ld sp, fc(s0)
1112 ST_FUNC void gen_vla_alloc(CType *type, int align)
1114 int rr = ireg(gv(RC_INT));
1115 EI(0x13, 0, rr, rr, 15); // addi RR, RR, 15
1116 EI(0x13, 7, rr, rr, -16); // andi, RR, RR, -16
1117 o(0x33 | (2 << 7) | (2 << 15) | (rr << 20) | (0x20 << 25)); //sub sp, sp, rr
1118 vpop();
1120 #endif