Fix include SHT_NOTE sections everywhere
[tinycc/self_contained.git] / riscv64-gen.c
blob384d0ec7bd6a9445b41fcca3ef418328c8b8cfa4
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
5 #define NB_ASM_REGS 32
6 #define CONFIG_TCC_ASM
8 #define TREG_R(x) (x) // x = 0..7
9 #define TREG_F(x) (x + 8) // x = 0..7
11 // Register classes sorted from more general to more precise:
12 #define RC_INT (1 << 0)
13 #define RC_FLOAT (1 << 1)
14 #define RC_R(x) (1 << (2 + (x))) // x = 0..7
15 #define RC_F(x) (1 << (10 + (x))) // x = 0..7
17 #define RC_IRET (RC_R(0)) // int return register class
18 #define RC_IRE2 (RC_R(1)) // int 2nd return register class
19 #define RC_FRET (RC_F(0)) // float return register class
21 #define REG_IRET (TREG_R(0)) // int return register number
22 #define REG_IRE2 (TREG_R(1)) // int 2nd return register number
23 #define REG_FRET (TREG_F(0)) // float return register number
25 #define PTR_SIZE 8
27 #define LDOUBLE_SIZE 16
28 #define LDOUBLE_ALIGN 16
30 #define MAX_ALIGN 16
32 #define CHAR_IS_UNSIGNED
34 #else
35 #define USING_GLOBALS
36 #include "tcc.h"
37 #include <assert.h>
39 #define XLEN 8
41 #define TREG_RA 17
42 #define TREG_SP 18
44 ST_DATA const int reg_classes[NB_REGS] = {
45 RC_INT | RC_R(0),
46 RC_INT | RC_R(1),
47 RC_INT | RC_R(2),
48 RC_INT | RC_R(3),
49 RC_INT | RC_R(4),
50 RC_INT | RC_R(5),
51 RC_INT | RC_R(6),
52 RC_INT | RC_R(7),
53 RC_FLOAT | RC_F(0),
54 RC_FLOAT | RC_F(1),
55 RC_FLOAT | RC_F(2),
56 RC_FLOAT | RC_F(3),
57 RC_FLOAT | RC_F(4),
58 RC_FLOAT | RC_F(5),
59 RC_FLOAT | RC_F(6),
60 RC_FLOAT | RC_F(7),
62 1 << TREG_RA,
63 1 << TREG_SP
66 #if defined(CONFIG_TCC_BCHECK)
67 static addr_t func_bound_offset;
68 static unsigned long func_bound_ind;
69 ST_DATA int func_bound_add_epilog;
70 #endif
72 static int ireg(int r)
74 if (r == TREG_RA)
75 return 1; // ra
76 if (r == TREG_SP)
77 return 2; // sp
78 assert(r >= 0 && r < 8);
79 return r + 10; // tccrX --> aX == x(10+X)
82 static int is_ireg(int r)
84 return (unsigned)r < 8 || r == TREG_RA || r == TREG_SP;
87 static int freg(int r)
89 assert(r >= 8 && r < 16);
90 return r - 8 + 10; // tccfX --> faX == f(10+X)
93 static int is_freg(int r)
95 return r >= 8 && r < 16;
98 ST_FUNC void o(unsigned int c)
100 int ind1 = ind + 4;
101 if (nocode_wanted)
102 return;
103 if (ind1 > cur_text_section->data_allocated)
104 section_realloc(cur_text_section, ind1);
105 write32le(cur_text_section->data + ind, c);
106 ind = ind1;
109 static void EIu(uint32_t opcode, uint32_t func3,
110 uint32_t rd, uint32_t rs1, uint32_t imm)
112 o(opcode | (func3 << 12) | (rd << 7) | (rs1 << 15) | (imm << 20));
115 static void ER(uint32_t opcode, uint32_t func3,
116 uint32_t rd, uint32_t rs1, uint32_t rs2, uint32_t func7)
118 o(opcode | func3 << 12 | rd << 7 | rs1 << 15 | rs2 << 20 | func7 << 25);
121 static void EI(uint32_t opcode, uint32_t func3,
122 uint32_t rd, uint32_t rs1, uint32_t imm)
124 assert(! ((imm + (1 << 11)) >> 12));
125 EIu(opcode, func3, rd, rs1, imm);
128 static void ES(uint32_t opcode, uint32_t func3,
129 uint32_t rs1, uint32_t rs2, uint32_t imm)
131 assert(! ((imm + (1 << 11)) >> 12));
132 o(opcode | (func3 << 12) | ((imm & 0x1f) << 7) | (rs1 << 15)
133 | (rs2 << 20) | ((imm >> 5) << 25));
136 // Patch all branches in list pointed to by t to branch to a:
137 ST_FUNC void gsym_addr(int t_, int a_)
139 uint32_t t = t_;
140 uint32_t a = a_;
141 while (t) {
142 unsigned char *ptr = cur_text_section->data + t;
143 uint32_t next = read32le(ptr);
144 uint32_t r = a - t, imm;
145 if ((r + (1 << 21)) & ~((1U << 22) - 2))
146 tcc_error("out-of-range branch chain");
147 imm = (((r >> 12) & 0xff) << 12)
148 | (((r >> 11) & 1) << 20)
149 | (((r >> 1) & 0x3ff) << 21)
150 | (((r >> 20) & 1) << 31);
151 write32le(ptr, r == 4 ? 0x33 : 0x6f | imm); // nop || j imm
152 t = next;
156 static int load_symofs(int r, SValue *sv, int forstore)
158 static Sym label;
159 int rr, doload = 0;
160 int fc = sv->c.i, v = sv->r & VT_VALMASK;
161 if (sv->r & VT_SYM) {
162 assert(v == VT_CONST);
163 if (sv->sym->type.t & VT_STATIC) { // XXX do this per linker relax
164 greloca(cur_text_section, sv->sym, ind,
165 R_RISCV_PCREL_HI20, sv->c.i);
166 sv->c.i = 0;
167 } else {
168 if (((unsigned)fc + (1 << 11)) >> 12)
169 tcc_error("unimp: large addend for global address (0x%lx)", (long)sv->c.i);
170 greloca(cur_text_section, sv->sym, ind,
171 R_RISCV_GOT_HI20, 0);
172 doload = 1;
174 if (!label.v) {
175 label.v = tok_alloc(".L0 ", 4)->tok;
176 label.type.t = VT_VOID | VT_STATIC;
178 label.c = 0; /* force new local ELF symbol */
179 put_extern_sym(&label, cur_text_section, ind, 0);
180 rr = is_ireg(r) ? ireg(r) : 5;
181 o(0x17 | (rr << 7)); // auipc RR, 0 %pcrel_hi(sym)+addend
182 greloca(cur_text_section, &label, ind,
183 doload || !forstore
184 ? R_RISCV_PCREL_LO12_I : R_RISCV_PCREL_LO12_S, 0);
185 if (doload) {
186 EI(0x03, 3, rr, rr, 0); // ld RR, 0(RR)
188 } else if (v == VT_LOCAL || v == VT_LLOCAL) {
189 rr = 8; // s0
190 if (fc != sv->c.i)
191 tcc_error("unimp: store(giant local off) (0x%lx)", (long)sv->c.i);
192 if (((unsigned)fc + (1 << 11)) >> 12) {
193 rr = is_ireg(r) ? ireg(r) : 5; // t0
194 o(0x37 | (rr << 7) | ((0x800 + fc) & 0xfffff000)); //lui RR, upper(fc)
195 ER(0x33, 0, rr, rr, 8, 0); // add RR, RR, s0
196 sv->c.i = fc << 20 >> 20;
198 } else
199 tcc_error("uhh");
200 return rr;
203 ST_FUNC void load(int r, SValue *sv)
205 int fr = sv->r;
206 int v = fr & VT_VALMASK;
207 int rr = is_ireg(r) ? ireg(r) : freg(r);
208 int fc = sv->c.i;
209 int bt = sv->type.t & VT_BTYPE;
210 int align, size;
211 if (fr & VT_LVAL) {
212 int func3, opcode = is_freg(r) ? 0x07 : 0x03, br;
213 size = type_size(&sv->type, &align);
214 assert (!is_freg(r) || bt == VT_FLOAT || bt == VT_DOUBLE);
215 if (bt == VT_FUNC) /* XXX should be done in generic code */
216 size = PTR_SIZE;
217 func3 = size == 1 ? 0 : size == 2 ? 1 : size == 4 ? 2 : 3;
218 if (size < 4 && !is_float(sv->type.t) && (sv->type.t & VT_UNSIGNED))
219 func3 |= 4;
220 if (v == VT_LOCAL || (fr & VT_SYM)) {
221 br = load_symofs(r, sv, 0);
222 fc = sv->c.i;
223 } else if (v < VT_CONST) {
224 br = ireg(v);
225 /*if (((unsigned)fc + (1 << 11)) >> 12)
226 tcc_error("unimp: load(large addend) (0x%x)", fc);*/
227 fc = 0; // XXX store ofs in LVAL(reg)
228 } else if (v == VT_LLOCAL) {
229 br = load_symofs(r, sv, 0);
230 fc = sv->c.i;
231 EI(0x03, 3, rr, br, fc); // ld RR, fc(BR)
232 br = rr;
233 fc = 0;
234 } else {
235 tcc_error("unimp: load(non-local lval)");
237 EI(opcode, func3, rr, br, fc); // l[bhwd][u] / fl[wd] RR, fc(BR)
238 } else if (v == VT_CONST) {
239 int rb = 0, do32bit = 8, zext = 0;
240 assert((!is_float(sv->type.t) && is_ireg(r)) || bt == VT_LDOUBLE);
241 if (fr & VT_SYM) {
242 rb = load_symofs(r, sv, 0);
243 fc = sv->c.i;
244 do32bit = 0;
246 if (is_float(sv->type.t) && bt != VT_LDOUBLE)
247 tcc_error("unimp: load(float)");
248 if (fc != sv->c.i) {
249 int64_t si = sv->c.i;
250 uint32_t pi;
251 si >>= 32;
252 if (si != 0) {
253 pi = si;
254 if (fc < 0)
255 pi++;
256 o(0x37 | (rr << 7) | (((pi + 0x800) & 0xfffff000))); // lui RR, up(up(fc))
257 EI(0x13, 0, rr, rr, (int)pi << 20 >> 20); // addi RR, RR, lo(up(fc))
258 EI(0x13, 1, rr, rr, 12); // slli RR, RR, 12
259 EI(0x13, 0, rr, rr, (fc + (1 << 19)) >> 20); // addi RR, RR, up(lo(fc))
260 EI(0x13, 1, rr, rr, 12); // slli RR, RR, 12
261 fc = fc << 12 >> 12;
262 EI(0x13, 0, rr, rr, fc >> 8); // addi RR, RR, lo1(lo(fc))
263 EI(0x13, 1, rr, rr, 8); // slli RR, RR, 8
264 fc &= 0xff;
265 rb = rr;
266 do32bit = 0;
267 } else if (bt == VT_LLONG) {
268 /* A 32bit unsigned constant for a 64bit type.
269 lui always sign extends, so we need to do an explicit zext.*/
270 zext = 1;
273 if (((unsigned)fc + (1 << 11)) >> 12)
274 o(0x37 | (rr << 7) | ((0x800 + fc) & 0xfffff000)), rb = rr; //lui RR, upper(fc)
275 if (fc || (rr != rb) || do32bit || (fr & VT_SYM))
276 EI(0x13 | do32bit, 0, rr, rb, fc << 20 >> 20); // addi[w] R, x0|R, FC
277 if (zext) {
278 EI(0x13, 1, rr, rr, 32); // slli RR, RR, 32
279 EI(0x13, 5, rr, rr, 32); // srli RR, RR, 32
281 } else if (v == VT_LOCAL) {
282 int br = load_symofs(r, sv, 0);
283 assert(is_ireg(r));
284 fc = sv->c.i;
285 EI(0x13, 0, rr, br, fc); // addi R, s0, FC
286 } else if (v < VT_CONST) { /* reg-reg */
287 //assert(!fc); XXX support offseted regs
288 if (is_freg(r) && is_freg(v))
289 ER(0x53, 0, rr, freg(v), freg(v), bt == VT_DOUBLE ? 0x11 : 0x10); //fsgnj.[sd] RR, V, V == fmv.[sd] RR, V
290 else if (is_ireg(r) && is_ireg(v))
291 EI(0x13, 0, rr, ireg(v), 0); // addi RR, V, 0 == mv RR, V
292 else {
293 int func7 = is_ireg(r) ? 0x70 : 0x78;
294 size = type_size(&sv->type, &align);
295 if (size == 8)
296 func7 |= 1;
297 assert(size == 4 || size == 8);
298 o(0x53 | (rr << 7) | ((is_freg(v) ? freg(v) : ireg(v)) << 15)
299 | (func7 << 25)); // fmv.{w.x, x.w, d.x, x.d} RR, VR
301 } else if (v == VT_CMP) {
302 int op = vtop->cmp_op;
303 int a = vtop->cmp_r & 0xff;
304 int b = (vtop->cmp_r >> 8) & 0xff;
305 int inv = 0;
306 switch (op) {
307 case TOK_ULT:
308 case TOK_UGE:
309 case TOK_ULE:
310 case TOK_UGT:
311 case TOK_LT:
312 case TOK_GE:
313 case TOK_LE:
314 case TOK_GT:
315 if (op & 1) { // remove [U]GE,GT
316 inv = 1;
317 op--;
319 if ((op & 7) == 6) { // [U]LE
320 int t = a; a = b; b = t;
321 inv ^= 1;
323 ER(0x33, (op > TOK_UGT) ? 2 : 3, rr, a, b, 0); // slt[u] d, a, b
324 if (inv)
325 EI(0x13, 4, rr, rr, 1); // xori d, d, 1
326 break;
327 case TOK_NE:
328 case TOK_EQ:
329 if (rr != a || b)
330 ER(0x33, 0, rr, a, b, 0x20); // sub d, a, b
331 if (op == TOK_NE)
332 ER(0x33, 3, rr, 0, rr, 0); // sltu d, x0, d == snez d,d
333 else
334 EI(0x13, 3, rr, rr, 1); // sltiu d, d, 1 == seqz d,d
335 break;
337 } else if ((v & ~1) == VT_JMP) {
338 int t = v & 1;
339 assert(is_ireg(r));
340 EI(0x13, 0, rr, 0, t); // addi RR, x0, t
341 gjmp_addr(ind + 8);
342 gsym(fc);
343 EI(0x13, 0, rr, 0, t ^ 1); // addi RR, x0, !t
344 } else
345 tcc_error("unimp: load(non-const)");
348 ST_FUNC void store(int r, SValue *sv)
350 int fr = sv->r & VT_VALMASK;
351 int rr = is_ireg(r) ? ireg(r) : freg(r), ptrreg;
352 int fc = sv->c.i;
353 int bt = sv->type.t & VT_BTYPE;
354 int align, size = type_size(&sv->type, &align);
355 assert(!is_float(bt) || is_freg(r) || bt == VT_LDOUBLE);
356 /* long doubles are in two integer registers, but the load/store
357 primitives only deal with one, so do as if it's one reg. */
358 if (bt == VT_LDOUBLE)
359 size = align = 8;
360 if (bt == VT_STRUCT)
361 tcc_error("unimp: store(struct)");
362 if (size > 8)
363 tcc_error("unimp: large sized store");
364 assert(sv->r & VT_LVAL);
365 if (fr == VT_LOCAL || (sv->r & VT_SYM)) {
366 ptrreg = load_symofs(-1, sv, 1);
367 fc = sv->c.i;
368 } else if (fr < VT_CONST) {
369 ptrreg = ireg(fr);
370 /*if (((unsigned)fc + (1 << 11)) >> 12)
371 tcc_error("unimp: store(large addend) (0x%x)", fc);*/
372 fc = 0; // XXX support offsets regs
373 } else
374 tcc_error("implement me: %s(!local)", __FUNCTION__);
375 ES(is_freg(r) ? 0x27 : 0x23, // fs... | s...
376 size == 1 ? 0 : size == 2 ? 1 : size == 4 ? 2 : 3, // ... [wd] | [bhwd]
377 ptrreg, rr, fc); // RR, fc(base)
380 static void gcall_or_jmp(int docall)
382 int tr = docall ? 1 : 5; // ra or t0
383 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST &&
384 ((vtop->r & VT_SYM) && vtop->c.i == (int)vtop->c.i)) {
385 /* constant symbolic case -> simple relocation */
386 greloca(cur_text_section, vtop->sym, ind,
387 R_RISCV_CALL_PLT, (int)vtop->c.i);
388 o(0x17 | (tr << 7)); // auipc TR, 0 %call(func)
389 EI(0x67, 0, tr, tr, 0);// jalr TR, r(TR)
390 } else if (vtop->r < VT_CONST) {
391 int r = ireg(vtop->r);
392 EI(0x67, 0, tr, r, 0); // jalr TR, 0(R)
393 } else {
394 int r = TREG_RA;
395 load(r, vtop);
396 r = ireg(r);
397 EI(0x67, 0, tr, r, 0); // jalr TR, 0(R)
401 #if defined(CONFIG_TCC_BCHECK)
403 static void gen_bounds_call(int v)
405 Sym *sym = external_helper_sym(v);
407 greloca(cur_text_section, sym, ind, R_RISCV_CALL_PLT, 0);
408 o(0x17 | (1 << 7)); // auipc TR, 0 %call(func)
409 EI(0x67, 0, 1, 1, 0); // jalr TR, r(TR)
412 static void gen_bounds_prolog(void)
414 /* leave some room for bound checking code */
415 func_bound_offset = lbounds_section->data_offset;
416 func_bound_ind = ind;
417 func_bound_add_epilog = 0;
418 o(0x00000013); /* ld a0,#lbound section pointer */
419 o(0x00000013);
420 o(0x00000013); /* nop -> call __bound_local_new */
421 o(0x00000013);
424 static void gen_bounds_epilog(void)
426 static Sym label;
427 addr_t saved_ind;
428 addr_t *bounds_ptr;
429 Sym *sym_data;
430 int offset_modified = func_bound_offset != lbounds_section->data_offset;
432 if (!offset_modified && !func_bound_add_epilog)
433 return;
435 /* add end of table info */
436 bounds_ptr = section_ptr_add(lbounds_section, sizeof(addr_t));
437 *bounds_ptr = 0;
439 sym_data = get_sym_ref(&char_pointer_type, lbounds_section,
440 func_bound_offset, lbounds_section->data_offset);
442 if (!label.v) {
443 label.v = tok_alloc(".LB0 ", 4)->tok;
444 label.type.t = VT_VOID | VT_STATIC;
446 /* generate bound local allocation */
447 if (offset_modified) {
448 saved_ind = ind;
449 ind = func_bound_ind;
450 label.c = 0; /* force new local ELF symbol */
451 put_extern_sym(&label, cur_text_section, ind, 0);
452 greloca(cur_text_section, sym_data, ind, R_RISCV_GOT_HI20, 0);
453 o(0x17 | (10 << 7)); // auipc a0, 0 %pcrel_hi(sym)+addend
454 greloca(cur_text_section, &label, ind, R_RISCV_PCREL_LO12_I, 0);
455 EI(0x03, 3, 10, 10, 0); // ld a0, 0(a0)
456 gen_bounds_call(TOK___bound_local_new);
457 ind = saved_ind;
460 /* generate bound check local freeing */
461 o(0xe02a1101); /* addi sp,sp,-32 sd a0,0(sp) */
462 o(0xa82ae42e); /* sd a1,8(sp) fsd fa0,16(sp) */
463 label.c = 0; /* force new local ELF symbol */
464 put_extern_sym(&label, cur_text_section, ind, 0);
465 greloca(cur_text_section, sym_data, ind, R_RISCV_GOT_HI20, 0);
466 o(0x17 | (10 << 7)); // auipc a0, 0 %pcrel_hi(sym)+addend
467 greloca(cur_text_section, &label, ind, R_RISCV_PCREL_LO12_I, 0);
468 EI(0x03, 3, 10, 10, 0); // ld a0, 0(a0)
469 gen_bounds_call(TOK___bound_local_delete);
470 o(0x65a26502); /* ld a0,0(sp) ld a1,8(sp) */
471 o(0x61052542); /* fld fa0,16(sp) addi sp,sp,32 */
473 #endif
475 static void reg_pass_rec(CType *type, int *rc, int *fieldofs, int ofs)
477 if ((type->t & VT_BTYPE) == VT_STRUCT) {
478 Sym *f;
479 if (type->ref->type.t == VT_UNION)
480 rc[0] = -1;
481 else for (f = type->ref->next; f; f = f->next)
482 reg_pass_rec(&f->type, rc, fieldofs, ofs + f->c);
483 } else if (type->t & VT_ARRAY) {
484 if (type->ref->c < 0 || type->ref->c > 2)
485 rc[0] = -1;
486 else {
487 int a, sz = type_size(&type->ref->type, &a);
488 reg_pass_rec(&type->ref->type, rc, fieldofs, ofs);
489 if (rc[0] > 2 || (rc[0] == 2 && type->ref->c > 1))
490 rc[0] = -1;
491 else if (type->ref->c == 2 && rc[0] && rc[1] == RC_FLOAT) {
492 rc[++rc[0]] = RC_FLOAT;
493 fieldofs[rc[0]] = ((ofs + sz) << 4)
494 | (type->ref->type.t & VT_BTYPE);
495 } else if (type->ref->c == 2)
496 rc[0] = -1;
498 } else if (rc[0] == 2 || rc[0] < 0 || (type->t & VT_BTYPE) == VT_LDOUBLE)
499 rc[0] = -1;
500 else if (!rc[0] || rc[1] == RC_FLOAT || is_float(type->t)) {
501 rc[++rc[0]] = is_float(type->t) ? RC_FLOAT : RC_INT;
502 fieldofs[rc[0]] = (ofs << 4) | (type->t & VT_BTYPE);
503 } else
504 rc[0] = -1;
507 static void reg_pass(CType *type, int *prc, int *fieldofs, int named)
509 prc[0] = 0;
510 reg_pass_rec(type, prc, fieldofs, 0);
511 if (prc[0] <= 0 || !named) {
512 int align, size = type_size(type, &align);
513 prc[0] = (size + 7) >> 3;
514 prc[1] = prc[2] = RC_INT;
515 fieldofs[1] = (0 << 4) | (size <= 1 ? VT_BYTE : size <= 2 ? VT_SHORT : size <= 4 ? VT_INT : VT_LLONG);
516 fieldofs[2] = (8 << 4) | (size <= 9 ? VT_BYTE : size <= 10 ? VT_SHORT : size <= 12 ? VT_INT : VT_LLONG);
520 ST_FUNC void gfunc_call(int nb_args)
522 int i, align, size, areg[2];
523 int *info = tcc_malloc((nb_args + 1) * sizeof (int));
524 int stack_adj = 0, tempspace = 0, stack_add, ofs, splitofs = 0;
525 SValue *sv;
526 Sym *sa;
528 #ifdef CONFIG_TCC_BCHECK
529 if (tcc_state->do_bounds_check)
530 gbound_args(nb_args);
531 #endif
533 areg[0] = 0; /* int arg regs */
534 areg[1] = 8; /* float arg regs */
535 sa = vtop[-nb_args].type.ref->next;
536 for (i = 0; i < nb_args; i++) {
537 int nregs, byref = 0, tempofs;
538 int prc[3], fieldofs[3];
539 sv = &vtop[1 + i - nb_args];
540 sv->type.t &= ~VT_ARRAY; // XXX this should be done in tccgen.c
541 size = type_size(&sv->type, &align);
542 if (size > 16) {
543 if (align < XLEN)
544 align = XLEN;
545 tempspace = (tempspace + align - 1) & -align;
546 tempofs = tempspace;
547 tempspace += size;
548 size = align = 8;
549 byref = 64 | (tempofs << 7);
551 reg_pass(&sv->type, prc, fieldofs, sa != 0);
552 if (!sa && align == 2*XLEN && size <= 2*XLEN)
553 areg[0] = (areg[0] + 1) & ~1;
554 nregs = prc[0];
555 if (size == 0)
556 info[i] = 0;
557 else if ((prc[1] == RC_INT && areg[0] >= 8)
558 || (prc[1] == RC_FLOAT && areg[1] >= 16)
559 || (nregs == 2 && prc[1] == RC_FLOAT && prc[2] == RC_FLOAT
560 && areg[1] >= 15)
561 || (nregs == 2 && prc[1] != prc[2]
562 && (areg[1] >= 16 || areg[0] >= 8))) {
563 info[i] = 32;
564 if (align < XLEN)
565 align = XLEN;
566 stack_adj += (size + align - 1) & -align;
567 if (!sa) /* one vararg on stack forces the rest on stack */
568 areg[0] = 8, areg[1] = 16;
569 } else {
570 info[i] = areg[prc[1] - 1]++;
571 if (!byref)
572 info[i] |= (fieldofs[1] & VT_BTYPE) << 12;
573 assert(!(fieldofs[1] >> 4));
574 if (nregs == 2) {
575 if (prc[2] == RC_FLOAT || areg[0] < 8)
576 info[i] |= (1 + areg[prc[2] - 1]++) << 7;
577 else {
578 info[i] |= 16;
579 stack_adj += 8;
581 if (!byref) {
582 assert((fieldofs[2] >> 4) < 2048);
583 info[i] |= fieldofs[2] << (12 + 4); // includes offset
587 info[i] |= byref;
588 if (sa)
589 sa = sa->next;
591 stack_adj = (stack_adj + 15) & -16;
592 tempspace = (tempspace + 15) & -16;
593 stack_add = stack_adj + tempspace;
594 if (stack_add) {
595 if (stack_add >= 0x1000) {
596 o(0x37 | (5 << 7) | (-stack_add & 0xfffff000)); //lui t0, upper(v)
597 EI(0x13, 0, 5, 5, -stack_add << 20 >> 20); // addi t0, t0, lo(v)
598 ER(0x33, 0, 2, 2, 5, 0); // add sp, sp, t0
600 else
601 EI(0x13, 0, 2, 2, -stack_add); // addi sp, sp, -adj
602 for (i = ofs = 0; i < nb_args; i++) {
603 if (info[i] & (64 | 32)) {
604 vrotb(nb_args - i);
605 size = type_size(&vtop->type, &align);
606 if (info[i] & 64) {
607 vset(&char_pointer_type, TREG_SP, 0);
608 vpushi(stack_adj + (info[i] >> 7));
609 gen_op('+');
610 vpushv(vtop); // this replaces the old argument
611 vrott(3);
612 indir();
613 vtop->type = vtop[-1].type;
614 vswap();
615 vstore();
616 vpop();
617 size = align = 8;
619 if (info[i] & 32) {
620 if (align < XLEN)
621 align = XLEN;
622 /* Once we support offseted regs we can do this:
623 vset(&vtop->type, TREG_SP | VT_LVAL, ofs);
624 to construct the lvalue for the outgoing stack slot,
625 until then we have to jump through hoops. */
626 vset(&char_pointer_type, TREG_SP, 0);
627 ofs = (ofs + align - 1) & -align;
628 vpushi(ofs);
629 gen_op('+');
630 indir();
631 vtop->type = vtop[-1].type;
632 vswap();
633 vstore();
634 vtop->r = vtop->r2 = VT_CONST; // this arg is done
635 ofs += size;
637 vrott(nb_args - i);
638 } else if (info[i] & 16) {
639 assert(!splitofs);
640 splitofs = ofs;
641 ofs += 8;
645 for (i = 0; i < nb_args; i++) {
646 int ii = info[nb_args - 1 - i], r = ii, r2 = r;
647 if (!(r & 32)) {
648 CType origtype;
649 int loadt;
650 r &= 15;
651 r2 = r2 & 64 ? 0 : (r2 >> 7) & 31;
652 assert(r2 <= 16);
653 vrotb(i+1);
654 origtype = vtop->type;
655 size = type_size(&vtop->type, &align);
656 if (size == 0)
657 goto done;
658 loadt = vtop->type.t & VT_BTYPE;
659 if (loadt == VT_STRUCT) {
660 loadt = (ii >> 12) & VT_BTYPE;
662 if (info[nb_args - 1 - i] & 16) {
663 assert(!r2);
664 r2 = 1 + TREG_RA;
666 if (loadt == VT_LDOUBLE) {
667 assert(r2);
668 r2--;
669 } else if (r2) {
670 test_lvalue();
671 vpushv(vtop);
673 vtop->type.t = loadt | (vtop->type.t & VT_UNSIGNED);
674 gv(r < 8 ? RC_R(r) : RC_F(r - 8));
675 vtop->type = origtype;
677 if (r2 && loadt != VT_LDOUBLE) {
678 r2--;
679 assert(r2 < 16 || r2 == TREG_RA);
680 vswap();
681 gaddrof();
682 vtop->type = char_pointer_type;
683 vpushi(ii >> 20);
684 gen_op('+');
685 indir();
686 vtop->type = origtype;
687 loadt = vtop->type.t & VT_BTYPE;
688 if (loadt == VT_STRUCT) {
689 loadt = (ii >> 16) & VT_BTYPE;
691 save_reg_upstack(r2, 1);
692 vtop->type.t = loadt | (vtop->type.t & VT_UNSIGNED);
693 load(r2, vtop);
694 assert(r2 < VT_CONST);
695 vtop--;
696 vtop->r2 = r2;
698 if (info[nb_args - 1 - i] & 16) {
699 ES(0x23, 3, 2, ireg(vtop->r2), splitofs); // sd t0, ofs(sp)
700 vtop->r2 = VT_CONST;
701 } else if (loadt == VT_LDOUBLE && vtop->r2 != r2) {
702 assert(vtop->r2 <= 7 && r2 <= 7);
703 /* XXX we'd like to have 'gv' move directly into
704 the right class instead of us fixing it up. */
705 EI(0x13, 0, ireg(r2), ireg(vtop->r2), 0); // mv Ra+1, RR2
706 vtop->r2 = r2;
708 done:
709 vrott(i+1);
712 vrotb(nb_args + 1);
713 save_regs(nb_args + 1);
714 gcall_or_jmp(1);
715 vtop -= nb_args + 1;
716 if (stack_add) {
717 if (stack_add >= 0x1000) {
718 o(0x37 | (5 << 7) | (stack_add & 0xfffff000)); //lui t0, upper(v)
719 EI(0x13, 0, 5, 5, stack_add << 20 >> 20); // addi t0, t0, lo(v)
720 ER(0x33, 0, 2, 2, 5, 0); // add sp, sp, t0
722 else
723 EI(0x13, 0, 2, 2, stack_add); // addi sp, sp, adj
725 tcc_free(info);
728 static int func_sub_sp_offset, num_va_regs, func_va_list_ofs;
730 ST_FUNC void gfunc_prolog(Sym *func_sym)
732 CType *func_type = &func_sym->type;
733 int i, addr, align, size;
734 int param_addr = 0;
735 int areg[2];
736 Sym *sym;
737 CType *type;
739 sym = func_type->ref;
740 loc = -16; // for ra and s0
741 func_sub_sp_offset = ind;
742 ind += 5 * 4;
744 areg[0] = 0, areg[1] = 0;
745 addr = 0;
746 /* if the function returns by reference, then add an
747 implicit pointer parameter */
748 size = type_size(&func_vt, &align);
749 if (size > 2 * XLEN) {
750 loc -= 8;
751 func_vc = loc;
752 ES(0x23, 3, 8, 10 + areg[0]++, loc); // sd a0, loc(s0)
754 /* define parameters */
755 while ((sym = sym->next) != NULL) {
756 int byref = 0;
757 int regcount;
758 int prc[3], fieldofs[3];
759 type = &sym->type;
760 size = type_size(type, &align);
761 if (size > 2 * XLEN) {
762 type = &char_pointer_type;
763 size = align = byref = 8;
765 reg_pass(type, prc, fieldofs, 1);
766 regcount = prc[0];
767 if (areg[prc[1] - 1] >= 8
768 || (regcount == 2
769 && ((prc[1] == RC_FLOAT && prc[2] == RC_FLOAT && areg[1] >= 7)
770 || (prc[1] != prc[2] && (areg[1] >= 8 || areg[0] >= 8))))) {
771 if (align < XLEN)
772 align = XLEN;
773 addr = (addr + align - 1) & -align;
774 param_addr = addr;
775 addr += size;
776 } else {
777 loc -= regcount * 8; // XXX could reserve only 'size' bytes
778 param_addr = loc;
779 for (i = 0; i < regcount; i++) {
780 if (areg[prc[1+i] - 1] >= 8) {
781 assert(i == 1 && regcount == 2 && !(addr & 7));
782 EI(0x03, 3, 5, 8, addr); // ld t0, addr(s0)
783 addr += 8;
784 ES(0x23, 3, 8, 5, loc + i*8); // sd t0, loc(s0)
785 } else if (prc[1+i] == RC_FLOAT) {
786 ES(0x27, (size / regcount) == 4 ? 2 : 3, 8, 10 + areg[1]++, loc + (fieldofs[i+1] >> 4)); // fs[wd] FAi, loc(s0)
787 } else {
788 ES(0x23, 3, 8, 10 + areg[0]++, loc + i*8); // sd aX, loc(s0) // XXX
792 sym_push(sym->v & ~SYM_FIELD, &sym->type,
793 (byref ? VT_LLOCAL : VT_LOCAL) | VT_LVAL,
794 param_addr);
796 func_va_list_ofs = addr;
797 num_va_regs = 0;
798 if (func_var) {
799 for (; areg[0] < 8; areg[0]++) {
800 num_va_regs++;
801 ES(0x23, 3, 8, 10 + areg[0], -8 + num_va_regs * 8); // sd aX, loc(s0)
804 #ifdef CONFIG_TCC_BCHECK
805 if (tcc_state->do_bounds_check)
806 gen_bounds_prolog();
807 #endif
810 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret,
811 int *ret_align, int *regsize)
813 int align, size = type_size(vt, &align), nregs;
814 int prc[3], fieldofs[3];
815 *ret_align = 1;
816 *regsize = 8;
817 if (size > 16)
818 return 0;
819 reg_pass(vt, prc, fieldofs, 1);
820 nregs = prc[0];
821 if (nregs == 2 && prc[1] != prc[2])
822 return -1; /* generic code can't deal with this case */
823 if (prc[1] == RC_FLOAT) {
824 *regsize = size / nregs;
826 ret->t = fieldofs[1] & VT_BTYPE;
827 return nregs;
830 ST_FUNC void arch_transfer_ret_regs(int aftercall)
832 int prc[3], fieldofs[3];
833 reg_pass(&vtop->type, prc, fieldofs, 1);
834 assert(prc[0] == 2 && prc[1] != prc[2] && !(fieldofs[1] >> 4));
835 assert(vtop->r == (VT_LOCAL | VT_LVAL));
836 vpushv(vtop);
837 vtop->type.t = fieldofs[1] & VT_BTYPE;
838 (aftercall ? store : load)(prc[1] == RC_INT ? REG_IRET : REG_FRET, vtop);
839 vtop->c.i += fieldofs[2] >> 4;
840 vtop->type.t = fieldofs[2] & VT_BTYPE;
841 (aftercall ? store : load)(prc[2] == RC_INT ? REG_IRET : REG_FRET, vtop);
842 vtop--;
845 ST_FUNC void gfunc_epilog(void)
847 int v, saved_ind, d, large_ofs_ind;
849 #ifdef CONFIG_TCC_BCHECK
850 if (tcc_state->do_bounds_check)
851 gen_bounds_epilog();
852 #endif
854 loc = (loc - num_va_regs * 8);
855 d = v = (-loc + 15) & -16;
857 if (v >= (1 << 11)) {
858 d = 16;
859 o(0x37 | (5 << 7) | ((0x800 + (v-16)) & 0xfffff000)); //lui t0, upper(v)
860 EI(0x13, 0, 5, 5, (v-16) << 20 >> 20); // addi t0, t0, lo(v)
861 ER(0x33, 0, 2, 2, 5, 0); // add sp, sp, t0
863 EI(0x03, 3, 1, 2, d - 8 - num_va_regs * 8); // ld ra, v-8(sp)
864 EI(0x03, 3, 8, 2, d - 16 - num_va_regs * 8); // ld s0, v-16(sp)
865 EI(0x13, 0, 2, 2, d); // addi sp, sp, v
866 EI(0x67, 0, 0, 1, 0); // jalr x0, 0(x1), aka ret
867 large_ofs_ind = ind;
868 if (v >= (1 << 11)) {
869 EI(0x13, 0, 8, 2, d - num_va_regs * 8); // addi s0, sp, d
870 o(0x37 | (5 << 7) | ((0x800 + (v-16)) & 0xfffff000)); //lui t0, upper(v)
871 EI(0x13, 0, 5, 5, (v-16) << 20 >> 20); // addi t0, t0, lo(v)
872 ER(0x33, 0, 2, 2, 5, 0x20); // sub sp, sp, t0
873 gjmp_addr(func_sub_sp_offset + 5*4);
875 saved_ind = ind;
877 ind = func_sub_sp_offset;
878 EI(0x13, 0, 2, 2, -d); // addi sp, sp, -d
879 ES(0x23, 3, 2, 1, d - 8 - num_va_regs * 8); // sd ra, d-8(sp)
880 ES(0x23, 3, 2, 8, d - 16 - num_va_regs * 8); // sd s0, d-16(sp)
881 if (v < (1 << 11))
882 EI(0x13, 0, 8, 2, d - num_va_regs * 8); // addi s0, sp, d
883 else
884 gjmp_addr(large_ofs_ind);
885 if ((ind - func_sub_sp_offset) != 5*4)
886 EI(0x13, 0, 0, 0, 0); // addi x0, x0, 0 == nop
887 ind = saved_ind;
890 ST_FUNC void gen_va_start(void)
892 vtop--;
893 vset(&char_pointer_type, VT_LOCAL, func_va_list_ofs);
896 ST_FUNC void gen_fill_nops(int bytes)
898 if ((bytes & 3))
899 tcc_error("alignment of code section not multiple of 4");
900 while (bytes > 0) {
901 EI(0x13, 0, 0, 0, 0); // addi x0, x0, 0 == nop
902 bytes -= 4;
906 // Generate forward branch to label:
907 ST_FUNC int gjmp(int t)
909 if (nocode_wanted)
910 return t;
911 o(t);
912 return ind - 4;
915 // Generate branch to known address:
916 ST_FUNC void gjmp_addr(int a)
918 uint32_t r = a - ind, imm;
919 if ((r + (1 << 21)) & ~((1U << 22) - 2)) {
920 o(0x17 | (5 << 7) | (((r + 0x800) & 0xfffff000))); // lui RR, up(r)
921 r = (int)r << 20 >> 20;
922 EI(0x67, 0, 0, 5, r); // jalr x0, r(t0)
923 } else {
924 imm = (((r >> 12) & 0xff) << 12)
925 | (((r >> 11) & 1) << 20)
926 | (((r >> 1) & 0x3ff) << 21)
927 | (((r >> 20) & 1) << 31);
928 o(0x6f | imm); // jal x0, imm == j imm
932 ST_FUNC int gjmp_cond(int op, int t)
934 int tmp;
935 int a = vtop->cmp_r & 0xff;
936 int b = (vtop->cmp_r >> 8) & 0xff;
937 switch (op) {
938 case TOK_ULT: op = 6; break;
939 case TOK_UGE: op = 7; break;
940 case TOK_ULE: op = 7; tmp = a; a = b; b = tmp; break;
941 case TOK_UGT: op = 6; tmp = a; a = b; b = tmp; break;
942 case TOK_LT: op = 4; break;
943 case TOK_GE: op = 5; break;
944 case TOK_LE: op = 5; tmp = a; a = b; b = tmp; break;
945 case TOK_GT: op = 4; tmp = a; a = b; b = tmp; break;
946 case TOK_NE: op = 1; break;
947 case TOK_EQ: op = 0; break;
949 o(0x63 | (op ^ 1) << 12 | a << 15 | b << 20 | 8 << 7); // bOP a,b,+4
950 return gjmp(t);
953 ST_FUNC int gjmp_append(int n, int t)
955 void *p;
956 /* insert jump list n into t */
957 if (n) {
958 uint32_t n1 = n, n2;
959 while ((n2 = read32le(p = cur_text_section->data + n1)))
960 n1 = n2;
961 write32le(p, t);
962 t = n;
964 return t;
967 static void gen_opil(int op, int ll)
969 int a, b, d;
970 int func3 = 0;
971 ll = ll ? 0 : 8;
972 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
973 int fc = vtop->c.i;
974 if (fc == vtop->c.i && !(((unsigned)fc + (1 << 11)) >> 12)) {
975 int cll = 0;
976 int m = ll ? 31 : 63;
977 vswap();
978 gv(RC_INT);
979 a = ireg(vtop[0].r);
980 --vtop;
981 d = get_reg(RC_INT);
982 ++vtop;
983 vswap();
984 switch (op) {
985 case '-':
986 if (fc <= -(1 << 11))
987 break;
988 fc = -fc;
989 case '+':
990 func3 = 0; // addi d, a, fc
991 cll = ll;
992 do_cop:
993 EI(0x13 | cll, func3, ireg(d), a, fc);
994 --vtop;
995 if (op >= TOK_ULT && op <= TOK_GT) {
996 vset_VT_CMP(TOK_NE);
997 vtop->cmp_r = ireg(d) | 0 << 8;
998 } else
999 vtop[0].r = d;
1000 return;
1001 case TOK_LE:
1002 if (fc >= (1 << 11) - 1)
1003 break;
1004 ++fc;
1005 case TOK_LT: func3 = 2; goto do_cop; // slti d, a, fc
1006 case TOK_ULE:
1007 if (fc >= (1 << 11) - 1 || fc == -1)
1008 break;
1009 ++fc;
1010 case TOK_ULT: func3 = 3; goto do_cop; // sltiu d, a, fc
1011 case '^': func3 = 4; goto do_cop; // xori d, a, fc
1012 case '|': func3 = 6; goto do_cop; // ori d, a, fc
1013 case '&': func3 = 7; goto do_cop; // andi d, a, fc
1014 case TOK_SHL: func3 = 1; cll = ll; fc &= m; goto do_cop; // slli d, a, fc
1015 case TOK_SHR: func3 = 5; cll = ll; fc &= m; goto do_cop; // srli d, a, fc
1016 case TOK_SAR: func3 = 5; cll = ll; fc = 1024 | (fc & m); goto do_cop;
1018 case TOK_UGE: /* -> TOK_ULT */
1019 case TOK_UGT: /* -> TOK_ULE */
1020 case TOK_GE: /* -> TOK_LT */
1021 case TOK_GT: /* -> TOK_LE */
1022 gen_opil(op - 1, !ll);
1023 vtop->cmp_op ^= 1;
1024 return;
1026 case TOK_NE:
1027 case TOK_EQ:
1028 if (fc)
1029 gen_opil('-', !ll), a = ireg(vtop++->r);
1030 --vtop;
1031 vset_VT_CMP(op);
1032 vtop->cmp_r = a | 0 << 8;
1033 return;
1037 gv2(RC_INT, RC_INT);
1038 a = ireg(vtop[-1].r);
1039 b = ireg(vtop[0].r);
1040 vtop -= 2;
1041 d = get_reg(RC_INT);
1042 vtop++;
1043 vtop[0].r = d;
1044 d = ireg(d);
1045 switch (op) {
1046 default:
1047 if (op >= TOK_ULT && op <= TOK_GT) {
1048 vset_VT_CMP(op);
1049 vtop->cmp_r = a | b << 8;
1050 break;
1052 tcc_error("implement me: %s(%s)", __FUNCTION__, get_tok_str(op, NULL));
1053 break;
1055 case '+':
1056 ER(0x33 | ll, 0, d, a, b, 0); // add d, a, b
1057 break;
1058 case '-':
1059 ER(0x33 | ll, 0, d, a, b, 0x20); // sub d, a, b
1060 break;
1061 case TOK_SAR:
1062 ER(0x33 | ll | ll, 5, d, a, b, 0x20); // sra d, a, b
1063 break;
1064 case TOK_SHR:
1065 ER(0x33 | ll | ll, 5, d, a, b, 0); // srl d, a, b
1066 break;
1067 case TOK_SHL:
1068 ER(0x33 | ll, 1, d, a, b, 0); // sll d, a, b
1069 break;
1070 case '*':
1071 ER(0x33 | ll, 0, d, a, b, 1); // mul d, a, b
1072 break;
1073 case '/':
1074 ER(0x33 | ll, 4, d, a, b, 1); // div d, a, b
1075 break;
1076 case '&':
1077 ER(0x33, 7, d, a, b, 0); // and d, a, b
1078 break;
1079 case '^':
1080 ER(0x33, 4, d, a, b, 0); // xor d, a, b
1081 break;
1082 case '|':
1083 ER(0x33, 6, d, a, b, 0); // or d, a, b
1084 break;
1085 case '%':
1086 ER(ll ? 0x3b: 0x33, 6, d, a, b, 1); // rem d, a, b
1087 break;
1088 case TOK_UMOD:
1089 ER(0x33 | ll, 7, d, a, b, 1); // remu d, a, b
1090 break;
1091 case TOK_PDIV:
1092 case TOK_UDIV:
1093 ER(0x33 | ll, 5, d, a, b, 1); // divu d, a, b
1094 break;
1098 ST_FUNC void gen_opi(int op)
1100 gen_opil(op, 0);
1103 ST_FUNC void gen_opl(int op)
1105 gen_opil(op, 1);
1108 ST_FUNC void gen_opf(int op)
1110 int rs1, rs2, rd, dbl, invert;
1111 if (vtop[0].type.t == VT_LDOUBLE) {
1112 CType type = vtop[0].type;
1113 int func = 0;
1114 int cond = -1;
1115 switch (op) {
1116 case '*': func = TOK___multf3; break;
1117 case '+': func = TOK___addtf3; break;
1118 case '-': func = TOK___subtf3; break;
1119 case '/': func = TOK___divtf3; break;
1120 case TOK_EQ: func = TOK___eqtf2; cond = 1; break;
1121 case TOK_NE: func = TOK___netf2; cond = 0; break;
1122 case TOK_LT: func = TOK___lttf2; cond = 10; break;
1123 case TOK_GE: func = TOK___getf2; cond = 11; break;
1124 case TOK_LE: func = TOK___letf2; cond = 12; break;
1125 case TOK_GT: func = TOK___gttf2; cond = 13; break;
1126 default: assert(0); break;
1128 vpush_helper_func(func);
1129 vrott(3);
1130 gfunc_call(2);
1131 vpushi(0);
1132 vtop->r = REG_IRET;
1133 vtop->r2 = cond < 0 ? TREG_R(1) : VT_CONST;
1134 if (cond < 0)
1135 vtop->type = type;
1136 else {
1137 vpushi(0);
1138 gen_opil(op, 1);
1140 return;
1143 gv2(RC_FLOAT, RC_FLOAT);
1144 assert(vtop->type.t == VT_DOUBLE || vtop->type.t == VT_FLOAT);
1145 dbl = vtop->type.t == VT_DOUBLE;
1146 rs1 = freg(vtop[-1].r);
1147 rs2 = freg(vtop->r);
1148 vtop--;
1149 invert = 0;
1150 switch(op) {
1151 default:
1152 assert(0);
1153 case '+':
1154 op = 0; // fadd
1155 arithop:
1156 rd = get_reg(RC_FLOAT);
1157 vtop->r = rd;
1158 rd = freg(rd);
1159 ER(0x53, 7, rd, rs1, rs2, dbl | (op << 2)); // fop.[sd] RD, RS1, RS2 (dyn rm)
1160 break;
1161 case '-':
1162 op = 1; // fsub
1163 goto arithop;
1164 case '*':
1165 op = 2; // fmul
1166 goto arithop;
1167 case '/':
1168 op = 3; // fdiv
1169 goto arithop;
1170 case TOK_EQ:
1171 op = 2; // EQ
1172 cmpop:
1173 rd = get_reg(RC_INT);
1174 vtop->r = rd;
1175 rd = ireg(rd);
1176 ER(0x53, op, rd, rs1, rs2, dbl | 0x50); // fcmp.[sd] RD, RS1, RS2 (op == eq/lt/le)
1177 if (invert)
1178 EI(0x13, 4, rd, rd, 1); // xori RD, 1
1179 break;
1180 case TOK_NE:
1181 invert = 1;
1182 op = 2; // EQ
1183 goto cmpop;
1184 case TOK_LT:
1185 op = 1; // LT
1186 goto cmpop;
1187 case TOK_LE:
1188 op = 0; // LE
1189 goto cmpop;
1190 case TOK_GT:
1191 op = 1; // LT
1192 rd = rs1, rs1 = rs2, rs2 = rd;
1193 goto cmpop;
1194 case TOK_GE:
1195 op = 0; // LE
1196 rd = rs1, rs1 = rs2, rs2 = rd;
1197 goto cmpop;
1201 ST_FUNC void gen_cvt_sxtw(void)
1203 /* XXX on risc-v the registers are usually sign-extended already.
1204 Let's try to not do anything here. */
1207 ST_FUNC void gen_cvt_itof(int t)
1209 int rr = ireg(gv(RC_INT)), dr;
1210 int u = vtop->type.t & VT_UNSIGNED;
1211 int l = (vtop->type.t & VT_BTYPE) == VT_LLONG;
1212 if (t == VT_LDOUBLE) {
1213 int func = l ?
1214 (u ? TOK___floatunditf : TOK___floatditf) :
1215 (u ? TOK___floatunsitf : TOK___floatsitf);
1216 vpush_helper_func(func);
1217 vrott(2);
1218 gfunc_call(1);
1219 vpushi(0);
1220 vtop->type.t = t;
1221 vtop->r = REG_IRET;
1222 vtop->r2 = TREG_R(1);
1223 } else {
1224 vtop--;
1225 dr = get_reg(RC_FLOAT);
1226 vtop++;
1227 vtop->r = dr;
1228 dr = freg(dr);
1229 EIu(0x53, 7, dr, rr, ((0x68 | (t == VT_DOUBLE ? 1 : 0)) << 5) | (u ? 1 : 0) | (l ? 2 : 0)); // fcvt.[sd].[wl][u]
1233 ST_FUNC void gen_cvt_ftoi(int t)
1235 int ft = vtop->type.t & VT_BTYPE;
1236 int l = (t & VT_BTYPE) == VT_LLONG;
1237 int u = t & VT_UNSIGNED;
1238 if (ft == VT_LDOUBLE) {
1239 int func = l ?
1240 (u ? TOK___fixunstfdi : TOK___fixtfdi) :
1241 (u ? TOK___fixunstfsi : TOK___fixtfsi);
1242 vpush_helper_func(func);
1243 vrott(2);
1244 gfunc_call(1);
1245 vpushi(0);
1246 vtop->type.t = t;
1247 vtop->r = REG_IRET;
1248 } else {
1249 int rr = freg(gv(RC_FLOAT)), dr;
1250 vtop--;
1251 dr = get_reg(RC_INT);
1252 vtop++;
1253 vtop->r = dr;
1254 dr = ireg(dr);
1255 EIu(0x53, 1, dr, rr, ((0x60 | (ft == VT_DOUBLE ? 1 : 0)) << 5) | (u ? 1 : 0) | (l ? 2 : 0)); // fcvt.[wl][u].[sd] rtz
1259 ST_FUNC void gen_cvt_ftof(int dt)
1261 int st = vtop->type.t & VT_BTYPE, rs, rd;
1262 dt &= VT_BTYPE;
1263 if (st == dt)
1264 return;
1265 if (dt == VT_LDOUBLE || st == VT_LDOUBLE) {
1266 int func = (dt == VT_LDOUBLE) ?
1267 (st == VT_FLOAT ? TOK___extendsftf2 : TOK___extenddftf2) :
1268 (dt == VT_FLOAT ? TOK___trunctfsf2 : TOK___trunctfdf2);
1269 /* We can't use gfunc_call, as func_old_type works like vararg
1270 functions, and on riscv unnamed float args are passed like
1271 integers. But we really need them in the float argument registers
1272 for extendsftf2/extenddftf2. So, do it explicitely. */
1273 save_regs(1);
1274 if (dt == VT_LDOUBLE)
1275 gv(RC_F(0));
1276 else {
1277 gv(RC_R(0));
1278 assert(vtop->r2 < 7);
1279 if (vtop->r2 != 1 + vtop->r) {
1280 EI(0x13, 0, ireg(vtop->r) + 1, ireg(vtop->r2), 0); // mv Ra+1, RR2
1281 vtop->r2 = 1 + vtop->r;
1284 vpush_helper_func(func);
1285 gcall_or_jmp(1);
1286 vtop -= 2;
1287 vpushi(0);
1288 vtop->type.t = dt;
1289 if (dt == VT_LDOUBLE)
1290 vtop->r = REG_IRET, vtop->r2 = REG_IRET+1;
1291 else
1292 vtop->r = REG_FRET;
1293 } else {
1294 assert (dt == VT_FLOAT || dt == VT_DOUBLE);
1295 assert (st == VT_FLOAT || st == VT_DOUBLE);
1296 rs = gv(RC_FLOAT);
1297 rd = get_reg(RC_FLOAT);
1298 if (dt == VT_DOUBLE)
1299 EI(0x53, 0, freg(rd), freg(rs), 0x21 << 5); // fcvt.d.s RD, RS (no rm)
1300 else
1301 EI(0x53, 7, freg(rd), freg(rs), (0x20 << 5) | 1); // fcvt.s.d RD, RS (dyn rm)
1302 vtop->r = rd;
1306 ST_FUNC void ggoto(void)
1308 gcall_or_jmp(0);
1309 vtop--;
1312 ST_FUNC void gen_vla_sp_save(int addr)
1314 ES(0x23, 3, 8, 2, addr); // sd sp, fc(s0)
1317 ST_FUNC void gen_vla_sp_restore(int addr)
1319 EI(0x03, 3, 2, 8, addr); // ld sp, fc(s0)
1322 ST_FUNC void gen_vla_alloc(CType *type, int align)
1324 int rr;
1325 #if defined(CONFIG_TCC_BCHECK)
1326 if (tcc_state->do_bounds_check)
1327 vpushv(vtop);
1328 #endif
1329 rr = ireg(gv(RC_INT));
1330 #if defined(CONFIG_TCC_BCHECK)
1331 if (tcc_state->do_bounds_check)
1332 EI(0x13, 0, rr, rr, 15+1); // addi RR, RR, 15+1
1333 else
1334 #endif
1335 EI(0x13, 0, rr, rr, 15); // addi RR, RR, 15
1336 EI(0x13, 7, rr, rr, -16); // andi, RR, RR, -16
1337 ER(0x33, 0, 2, 2, rr, 0x20); // sub sp, sp, rr
1338 vpop();
1339 #if defined(CONFIG_TCC_BCHECK)
1340 if (tcc_state->do_bounds_check) {
1341 vpushi(0);
1342 vtop->r = TREG_R(0);
1343 o(0x00010513); /* mv a0,sp */
1344 vswap();
1345 vpush_helper_func(TOK___bound_new_region);
1346 vrott(3);
1347 gfunc_call(2);
1348 func_bound_add_epilog = 1;
1350 #endif
1352 #endif