win32/tccpe: use full dll-path with -run
[tinycc.git] / riscv64-gen.c
blobbaf7f5ee30cccb07ee4e831282c71a6d0eb5cde7
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%llx)", (long 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%llx)", (long 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_global_sym(v, &func_old_type);
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 ((prc[1] == RC_INT && areg[0] >= 8)
556 || (prc[1] == RC_FLOAT && areg[1] >= 16)
557 || (nregs == 2 && prc[1] == RC_FLOAT && prc[2] == RC_FLOAT
558 && areg[1] >= 15)
559 || (nregs == 2 && prc[1] != prc[2]
560 && (areg[1] >= 16 || areg[0] >= 8))) {
561 info[i] = 32;
562 if (align < XLEN)
563 align = XLEN;
564 stack_adj += (size + align - 1) & -align;
565 if (!sa) /* one vararg on stack forces the rest on stack */
566 areg[0] = 8, areg[1] = 16;
567 } else {
568 info[i] = areg[prc[1] - 1]++;
569 if (!byref)
570 info[i] |= (fieldofs[1] & VT_BTYPE) << 12;
571 assert(!(fieldofs[1] >> 4));
572 if (nregs == 2) {
573 if (prc[2] == RC_FLOAT || areg[0] < 8)
574 info[i] |= (1 + areg[prc[2] - 1]++) << 7;
575 else {
576 info[i] |= 16;
577 stack_adj += 8;
579 if (!byref) {
580 assert((fieldofs[2] >> 4) < 2048);
581 info[i] |= fieldofs[2] << (12 + 4); // includes offset
585 info[i] |= byref;
586 if (sa)
587 sa = sa->next;
589 stack_adj = (stack_adj + 15) & -16;
590 tempspace = (tempspace + 15) & -16;
591 stack_add = stack_adj + tempspace;
592 if (stack_add) {
593 if (stack_add >= 0x1000) {
594 o(0x37 | (5 << 7) | (-stack_add & 0xfffff000)); //lui t0, upper(v)
595 EI(0x13, 0, 5, 5, -stack_add << 20 >> 20); // addi t0, t0, lo(v)
596 ER(0x33, 0, 2, 2, 5, 0); // add sp, sp, t0
598 else
599 EI(0x13, 0, 2, 2, -stack_add); // addi sp, sp, -adj
600 for (i = ofs = 0; i < nb_args; i++) {
601 if (info[i] & (64 | 32)) {
602 vrotb(nb_args - i);
603 size = type_size(&vtop->type, &align);
604 if (info[i] & 64) {
605 vset(&char_pointer_type, TREG_SP, 0);
606 vpushi(stack_adj + (info[i] >> 7));
607 gen_op('+');
608 vpushv(vtop); // this replaces the old argument
609 vrott(3);
610 indir();
611 vtop->type = vtop[-1].type;
612 vswap();
613 vstore();
614 vpop();
615 size = align = 8;
617 if (info[i] & 32) {
618 if (align < XLEN)
619 align = XLEN;
620 /* Once we support offseted regs we can do this:
621 vset(&vtop->type, TREG_SP | VT_LVAL, ofs);
622 to construct the lvalue for the outgoing stack slot,
623 until then we have to jump through hoops. */
624 vset(&char_pointer_type, TREG_SP, 0);
625 ofs = (ofs + align - 1) & -align;
626 vpushi(ofs);
627 gen_op('+');
628 indir();
629 vtop->type = vtop[-1].type;
630 vswap();
631 vstore();
632 vtop->r = vtop->r2 = VT_CONST; // this arg is done
633 ofs += size;
635 vrott(nb_args - i);
636 } else if (info[i] & 16) {
637 assert(!splitofs);
638 splitofs = ofs;
639 ofs += 8;
643 for (i = 0; i < nb_args; i++) {
644 int ii = info[nb_args - 1 - i], r = ii, r2 = r;
645 if (!(r & 32)) {
646 CType origtype;
647 int loadt;
648 r &= 15;
649 r2 = r2 & 64 ? 0 : (r2 >> 7) & 31;
650 assert(r2 <= 16);
651 vrotb(i+1);
652 origtype = vtop->type;
653 size = type_size(&vtop->type, &align);
654 loadt = vtop->type.t & VT_BTYPE;
655 if (loadt == VT_STRUCT) {
656 loadt = (ii >> 12) & VT_BTYPE;
658 if (info[nb_args - 1 - i] & 16) {
659 assert(!r2);
660 r2 = 1 + TREG_RA;
662 if (loadt == VT_LDOUBLE) {
663 assert(r2);
664 r2--;
665 } else if (r2) {
666 test_lvalue();
667 vpushv(vtop);
669 vtop->type.t = loadt | (vtop->type.t & VT_UNSIGNED);
670 gv(r < 8 ? RC_R(r) : RC_F(r - 8));
671 vtop->type = origtype;
673 if (r2 && loadt != VT_LDOUBLE) {
674 r2--;
675 assert(r2 < 16 || r2 == TREG_RA);
676 vswap();
677 gaddrof();
678 vtop->type = char_pointer_type;
679 vpushi(ii >> 20);
680 gen_op('+');
681 indir();
682 vtop->type = origtype;
683 loadt = vtop->type.t & VT_BTYPE;
684 if (loadt == VT_STRUCT) {
685 loadt = (ii >> 16) & VT_BTYPE;
687 save_reg_upstack(r2, 1);
688 vtop->type.t = loadt | (vtop->type.t & VT_UNSIGNED);
689 load(r2, vtop);
690 assert(r2 < VT_CONST);
691 vtop--;
692 vtop->r2 = r2;
694 if (info[nb_args - 1 - i] & 16) {
695 ES(0x23, 3, 2, ireg(vtop->r2), splitofs); // sd t0, ofs(sp)
696 vtop->r2 = VT_CONST;
697 } else if (loadt == VT_LDOUBLE && vtop->r2 != r2) {
698 assert(vtop->r2 <= 7 && r2 <= 7);
699 /* XXX we'd like to have 'gv' move directly into
700 the right class instead of us fixing it up. */
701 EI(0x13, 0, ireg(r2), ireg(vtop->r2), 0); // mv Ra+1, RR2
702 vtop->r2 = r2;
704 vrott(i+1);
707 vrotb(nb_args + 1);
708 save_regs(nb_args + 1);
709 gcall_or_jmp(1);
710 vtop -= nb_args + 1;
711 if (stack_add) {
712 if (stack_add >= 0x1000) {
713 o(0x37 | (5 << 7) | (stack_add & 0xfffff000)); //lui t0, upper(v)
714 EI(0x13, 0, 5, 5, stack_add << 20 >> 20); // addi t0, t0, lo(v)
715 ER(0x33, 0, 2, 2, 5, 0); // add sp, sp, t0
717 else
718 EI(0x13, 0, 2, 2, stack_add); // addi sp, sp, adj
720 tcc_free(info);
723 static int func_sub_sp_offset, num_va_regs, func_va_list_ofs;
725 ST_FUNC void gfunc_prolog(Sym *func_sym)
727 CType *func_type = &func_sym->type;
728 int i, addr, align, size;
729 int param_addr = 0;
730 int areg[2];
731 Sym *sym;
732 CType *type;
734 sym = func_type->ref;
735 loc = -16; // for ra and s0
736 func_sub_sp_offset = ind;
737 ind += 5 * 4;
739 areg[0] = 0, areg[1] = 0;
740 addr = 0;
741 /* if the function returns by reference, then add an
742 implicit pointer parameter */
743 size = type_size(&func_vt, &align);
744 if (size > 2 * XLEN) {
745 loc -= 8;
746 func_vc = loc;
747 ES(0x23, 3, 8, 10 + areg[0]++, loc); // sd a0, loc(s0)
749 /* define parameters */
750 while ((sym = sym->next) != NULL) {
751 int byref = 0;
752 int regcount;
753 int prc[3], fieldofs[3];
754 type = &sym->type;
755 size = type_size(type, &align);
756 if (size > 2 * XLEN) {
757 type = &char_pointer_type;
758 size = align = byref = 8;
760 reg_pass(type, prc, fieldofs, 1);
761 regcount = prc[0];
762 if (areg[prc[1] - 1] >= 8
763 || (regcount == 2
764 && ((prc[1] == RC_FLOAT && prc[2] == RC_FLOAT && areg[1] >= 7)
765 || (prc[1] != prc[2] && (areg[1] >= 8 || areg[0] >= 8))))) {
766 if (align < XLEN)
767 align = XLEN;
768 addr = (addr + align - 1) & -align;
769 param_addr = addr;
770 addr += size;
771 } else {
772 loc -= regcount * 8; // XXX could reserve only 'size' bytes
773 param_addr = loc;
774 for (i = 0; i < regcount; i++) {
775 if (areg[prc[1+i] - 1] >= 8) {
776 assert(i == 1 && regcount == 2 && !(addr & 7));
777 EI(0x03, 3, 5, 8, addr); // ld t0, addr(s0)
778 addr += 8;
779 ES(0x23, 3, 8, 5, loc + i*8); // sd t0, loc(s0)
780 } else if (prc[1+i] == RC_FLOAT) {
781 ES(0x27, (size / regcount) == 4 ? 2 : 3, 8, 10 + areg[1]++, loc + (fieldofs[i+1] >> 4)); // fs[wd] FAi, loc(s0)
782 } else {
783 ES(0x23, 3, 8, 10 + areg[0]++, loc + i*8); // sd aX, loc(s0) // XXX
787 sym_push(sym->v & ~SYM_FIELD, &sym->type,
788 (byref ? VT_LLOCAL : VT_LOCAL) | VT_LVAL,
789 param_addr);
791 func_va_list_ofs = addr;
792 num_va_regs = 0;
793 if (func_var) {
794 for (; areg[0] < 8; areg[0]++) {
795 num_va_regs++;
796 ES(0x23, 3, 8, 10 + areg[0], -8 + num_va_regs * 8); // sd aX, loc(s0)
799 #ifdef CONFIG_TCC_BCHECK
800 if (tcc_state->do_bounds_check)
801 gen_bounds_prolog();
802 #endif
805 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret,
806 int *ret_align, int *regsize)
808 int align, size = type_size(vt, &align), nregs;
809 int prc[3], fieldofs[3];
810 *ret_align = 1;
811 *regsize = 8;
812 if (size > 16)
813 return 0;
814 reg_pass(vt, prc, fieldofs, 1);
815 nregs = prc[0];
816 if (nregs == 2 && prc[1] != prc[2])
817 return -1; /* generic code can't deal with this case */
818 if (prc[1] == RC_FLOAT) {
819 *regsize = size / nregs;
821 ret->t = fieldofs[1] & VT_BTYPE;
822 return nregs;
825 ST_FUNC void arch_transfer_ret_regs(int aftercall)
827 int prc[3], fieldofs[3];
828 reg_pass(&vtop->type, prc, fieldofs, 1);
829 assert(prc[0] == 2 && prc[1] != prc[2] && !(fieldofs[1] >> 4));
830 assert(vtop->r == (VT_LOCAL | VT_LVAL));
831 vpushv(vtop);
832 vtop->type.t = fieldofs[1] & VT_BTYPE;
833 (aftercall ? store : load)(prc[1] == RC_INT ? REG_IRET : REG_FRET, vtop);
834 vtop->c.i += fieldofs[2] >> 4;
835 vtop->type.t = fieldofs[2] & VT_BTYPE;
836 (aftercall ? store : load)(prc[2] == RC_INT ? REG_IRET : REG_FRET, vtop);
837 vtop--;
840 ST_FUNC void gfunc_epilog(void)
842 int v, saved_ind, d, large_ofs_ind;
844 #ifdef CONFIG_TCC_BCHECK
845 if (tcc_state->do_bounds_check)
846 gen_bounds_epilog();
847 #endif
849 loc = (loc - num_va_regs * 8);
850 d = v = (-loc + 15) & -16;
852 if (v >= (1 << 11)) {
853 d = 16;
854 o(0x37 | (5 << 7) | ((0x800 + (v-16)) & 0xfffff000)); //lui t0, upper(v)
855 EI(0x13, 0, 5, 5, (v-16) << 20 >> 20); // addi t0, t0, lo(v)
856 ER(0x33, 0, 2, 2, 5, 0); // add sp, sp, t0
858 EI(0x03, 3, 1, 2, d - 8 - num_va_regs * 8); // ld ra, v-8(sp)
859 EI(0x03, 3, 8, 2, d - 16 - num_va_regs * 8); // ld s0, v-16(sp)
860 EI(0x13, 0, 2, 2, d); // addi sp, sp, v
861 EI(0x67, 0, 0, 1, 0); // jalr x0, 0(x1), aka ret
862 large_ofs_ind = ind;
863 if (v >= (1 << 11)) {
864 EI(0x13, 0, 8, 2, d - num_va_regs * 8); // addi s0, sp, d
865 o(0x37 | (5 << 7) | ((0x800 + (v-16)) & 0xfffff000)); //lui t0, upper(v)
866 EI(0x13, 0, 5, 5, (v-16) << 20 >> 20); // addi t0, t0, lo(v)
867 ER(0x33, 0, 2, 2, 5, 0x20); // sub sp, sp, t0
868 gjmp_addr(func_sub_sp_offset + 5*4);
870 saved_ind = ind;
872 ind = func_sub_sp_offset;
873 EI(0x13, 0, 2, 2, -d); // addi sp, sp, -d
874 ES(0x23, 3, 2, 1, d - 8 - num_va_regs * 8); // sd ra, d-8(sp)
875 ES(0x23, 3, 2, 8, d - 16 - num_va_regs * 8); // sd s0, d-16(sp)
876 if (v < (1 << 11))
877 EI(0x13, 0, 8, 2, d - num_va_regs * 8); // addi s0, sp, d
878 else
879 gjmp_addr(large_ofs_ind);
880 if ((ind - func_sub_sp_offset) != 5*4)
881 EI(0x13, 0, 0, 0, 0); // addi x0, x0, 0 == nop
882 ind = saved_ind;
885 ST_FUNC void gen_va_start(void)
887 vtop--;
888 vset(&char_pointer_type, VT_LOCAL, func_va_list_ofs);
891 ST_FUNC void gen_fill_nops(int bytes)
893 if ((bytes & 3))
894 tcc_error("alignment of code section not multiple of 4");
895 while (bytes > 0) {
896 EI(0x13, 0, 0, 0, 0); // addi x0, x0, 0 == nop
897 bytes -= 4;
901 // Generate forward branch to label:
902 ST_FUNC int gjmp(int t)
904 if (nocode_wanted)
905 return t;
906 o(t);
907 return ind - 4;
910 // Generate branch to known address:
911 ST_FUNC void gjmp_addr(int a)
913 uint32_t r = a - ind, imm;
914 if ((r + (1 << 21)) & ~((1U << 22) - 2)) {
915 o(0x17 | (5 << 7) | (((r + 0x800) & 0xfffff000))); // lui RR, up(r)
916 r = (int)r << 20 >> 20;
917 EI(0x67, 0, 0, 5, r); // jalr x0, r(t0)
918 } else {
919 imm = (((r >> 12) & 0xff) << 12)
920 | (((r >> 11) & 1) << 20)
921 | (((r >> 1) & 0x3ff) << 21)
922 | (((r >> 20) & 1) << 31);
923 o(0x6f | imm); // jal x0, imm == j imm
927 ST_FUNC int gjmp_cond(int op, int t)
929 int tmp;
930 int a = vtop->cmp_r & 0xff;
931 int b = (vtop->cmp_r >> 8) & 0xff;
932 switch (op) {
933 case TOK_ULT: op = 6; break;
934 case TOK_UGE: op = 7; break;
935 case TOK_ULE: op = 7; tmp = a; a = b; b = tmp; break;
936 case TOK_UGT: op = 6; tmp = a; a = b; b = tmp; break;
937 case TOK_LT: op = 4; break;
938 case TOK_GE: op = 5; break;
939 case TOK_LE: op = 5; tmp = a; a = b; b = tmp; break;
940 case TOK_GT: op = 4; tmp = a; a = b; b = tmp; break;
941 case TOK_NE: op = 1; break;
942 case TOK_EQ: op = 0; break;
944 o(0x63 | (op ^ 1) << 12 | a << 15 | b << 20 | 8 << 7); // bOP a,b,+4
945 return gjmp(t);
948 ST_FUNC int gjmp_append(int n, int t)
950 void *p;
951 /* insert jump list n into t */
952 if (n) {
953 uint32_t n1 = n, n2;
954 while ((n2 = read32le(p = cur_text_section->data + n1)))
955 n1 = n2;
956 write32le(p, t);
957 t = n;
959 return t;
962 static void gen_opil(int op, int ll)
964 int a, b, d;
965 int func3 = 0;
966 ll = ll ? 0 : 8;
967 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
968 int fc = vtop->c.i;
969 if (fc == vtop->c.i && !(((unsigned)fc + (1 << 11)) >> 12)) {
970 int cll = 0;
971 int m = ll ? 31 : 63;
972 vswap();
973 gv(RC_INT);
974 a = ireg(vtop[0].r);
975 --vtop;
976 d = get_reg(RC_INT);
977 ++vtop;
978 vswap();
979 switch (op) {
980 case '-':
981 if (fc <= -(1 << 11))
982 break;
983 fc = -fc;
984 case '+':
985 func3 = 0; // addi d, a, fc
986 cll = ll;
987 do_cop:
988 EI(0x13 | cll, func3, ireg(d), a, fc);
989 --vtop;
990 if (op >= TOK_ULT && op <= TOK_GT) {
991 vset_VT_CMP(TOK_NE);
992 vtop->cmp_r = ireg(d) | 0 << 8;
993 } else
994 vtop[0].r = d;
995 return;
996 case TOK_LE:
997 if (fc >= (1 << 11) - 1)
998 break;
999 ++fc;
1000 case TOK_LT: func3 = 2; goto do_cop; // slti d, a, fc
1001 case TOK_ULE:
1002 if (fc >= (1 << 11) - 1 || fc == -1)
1003 break;
1004 ++fc;
1005 case TOK_ULT: func3 = 3; goto do_cop; // sltiu d, a, fc
1006 case '^': func3 = 4; goto do_cop; // xori d, a, fc
1007 case '|': func3 = 6; goto do_cop; // ori d, a, fc
1008 case '&': func3 = 7; goto do_cop; // andi d, a, fc
1009 case TOK_SHL: func3 = 1; cll = ll; fc &= m; goto do_cop; // slli d, a, fc
1010 case TOK_SHR: func3 = 5; cll = ll; fc &= m; goto do_cop; // srli d, a, fc
1011 case TOK_SAR: func3 = 5; cll = ll; fc = 1024 | (fc & m); goto do_cop;
1013 case TOK_UGE: /* -> TOK_ULT */
1014 case TOK_UGT: /* -> TOK_ULE */
1015 case TOK_GE: /* -> TOK_LT */
1016 case TOK_GT: /* -> TOK_LE */
1017 gen_opil(op - 1, !ll);
1018 vtop->cmp_op ^= 1;
1019 return;
1021 case TOK_NE:
1022 case TOK_EQ:
1023 if (fc)
1024 gen_opil('-', !ll), a = ireg(vtop++->r);
1025 --vtop;
1026 vset_VT_CMP(op);
1027 vtop->cmp_r = a | 0 << 8;
1028 return;
1032 gv2(RC_INT, RC_INT);
1033 a = ireg(vtop[-1].r);
1034 b = ireg(vtop[0].r);
1035 vtop -= 2;
1036 d = get_reg(RC_INT);
1037 vtop++;
1038 vtop[0].r = d;
1039 d = ireg(d);
1040 switch (op) {
1041 default:
1042 if (op >= TOK_ULT && op <= TOK_GT) {
1043 vset_VT_CMP(op);
1044 vtop->cmp_r = a | b << 8;
1045 break;
1047 tcc_error("implement me: %s(%s)", __FUNCTION__, get_tok_str(op, NULL));
1048 break;
1050 case '+':
1051 ER(0x33 | ll, 0, d, a, b, 0); // add d, a, b
1052 break;
1053 case '-':
1054 ER(0x33 | ll, 0, d, a, b, 0x20); // sub d, a, b
1055 break;
1056 case TOK_SAR:
1057 ER(0x33 | ll | ll, 5, d, a, b, 0x20); // sra d, a, b
1058 break;
1059 case TOK_SHR:
1060 ER(0x33 | ll | ll, 5, d, a, b, 0); // srl d, a, b
1061 break;
1062 case TOK_SHL:
1063 ER(0x33 | ll, 1, d, a, b, 0); // sll d, a, b
1064 break;
1065 case '*':
1066 ER(0x33 | ll, 0, d, a, b, 1); // mul d, a, b
1067 break;
1068 case '/':
1069 ER(0x33 | ll, 4, d, a, b, 1); // div d, a, b
1070 break;
1071 case '&':
1072 ER(0x33, 7, d, a, b, 0); // and d, a, b
1073 break;
1074 case '^':
1075 ER(0x33, 4, d, a, b, 0); // xor d, a, b
1076 break;
1077 case '|':
1078 ER(0x33, 6, d, a, b, 0); // or d, a, b
1079 break;
1080 case '%':
1081 ER(ll ? 0x3b: 0x33, 6, d, a, b, 1); // rem d, a, b
1082 break;
1083 case TOK_UMOD:
1084 ER(0x33 | ll, 7, d, a, b, 1); // remu d, a, b
1085 break;
1086 case TOK_PDIV:
1087 case TOK_UDIV:
1088 ER(0x33 | ll, 5, d, a, b, 1); // divu d, a, b
1089 break;
1093 ST_FUNC void gen_opi(int op)
1095 gen_opil(op, 0);
1098 ST_FUNC void gen_opl(int op)
1100 gen_opil(op, 1);
1103 ST_FUNC void gen_opf(int op)
1105 int rs1, rs2, rd, dbl, invert;
1106 if (vtop[0].type.t == VT_LDOUBLE) {
1107 CType type = vtop[0].type;
1108 int func = 0;
1109 int cond = -1;
1110 switch (op) {
1111 case '*': func = TOK___multf3; break;
1112 case '+': func = TOK___addtf3; break;
1113 case '-': func = TOK___subtf3; break;
1114 case '/': func = TOK___divtf3; break;
1115 case TOK_EQ: func = TOK___eqtf2; cond = 1; break;
1116 case TOK_NE: func = TOK___netf2; cond = 0; break;
1117 case TOK_LT: func = TOK___lttf2; cond = 10; break;
1118 case TOK_GE: func = TOK___getf2; cond = 11; break;
1119 case TOK_LE: func = TOK___letf2; cond = 12; break;
1120 case TOK_GT: func = TOK___gttf2; cond = 13; break;
1121 default: assert(0); break;
1123 vpush_global_sym(&func_old_type, func);
1124 vrott(3);
1125 gfunc_call(2);
1126 vpushi(0);
1127 vtop->r = REG_IRET;
1128 vtop->r2 = cond < 0 ? TREG_R(1) : VT_CONST;
1129 if (cond < 0)
1130 vtop->type = type;
1131 else {
1132 vpushi(0);
1133 gen_opil(op, 1);
1135 return;
1138 gv2(RC_FLOAT, RC_FLOAT);
1139 assert(vtop->type.t == VT_DOUBLE || vtop->type.t == VT_FLOAT);
1140 dbl = vtop->type.t == VT_DOUBLE;
1141 rs1 = freg(vtop[-1].r);
1142 rs2 = freg(vtop->r);
1143 vtop--;
1144 invert = 0;
1145 switch(op) {
1146 default:
1147 assert(0);
1148 case '+':
1149 op = 0; // fadd
1150 arithop:
1151 rd = get_reg(RC_FLOAT);
1152 vtop->r = rd;
1153 rd = freg(rd);
1154 ER(0x53, 7, rd, rs1, rs2, dbl | (op << 2)); // fop.[sd] RD, RS1, RS2 (dyn rm)
1155 break;
1156 case '-':
1157 op = 1; // fsub
1158 goto arithop;
1159 case '*':
1160 op = 2; // fmul
1161 goto arithop;
1162 case '/':
1163 op = 3; // fdiv
1164 goto arithop;
1165 case TOK_EQ:
1166 op = 2; // EQ
1167 cmpop:
1168 rd = get_reg(RC_INT);
1169 vtop->r = rd;
1170 rd = ireg(rd);
1171 ER(0x53, op, rd, rs1, rs2, dbl | 0x50); // fcmp.[sd] RD, RS1, RS2 (op == eq/lt/le)
1172 if (invert)
1173 EI(0x13, 4, rd, rd, 1); // xori RD, 1
1174 break;
1175 case TOK_NE:
1176 invert = 1;
1177 op = 2; // EQ
1178 goto cmpop;
1179 case TOK_LT:
1180 op = 1; // LT
1181 goto cmpop;
1182 case TOK_LE:
1183 op = 0; // LE
1184 goto cmpop;
1185 case TOK_GT:
1186 op = 1; // LT
1187 rd = rs1, rs1 = rs2, rs2 = rd;
1188 goto cmpop;
1189 case TOK_GE:
1190 op = 0; // LE
1191 rd = rs1, rs1 = rs2, rs2 = rd;
1192 goto cmpop;
1196 ST_FUNC void gen_cvt_sxtw(void)
1198 /* XXX on risc-v the registers are usually sign-extended already.
1199 Let's try to not do anything here. */
1202 ST_FUNC void gen_cvt_itof(int t)
1204 int rr = ireg(gv(RC_INT)), dr;
1205 int u = vtop->type.t & VT_UNSIGNED;
1206 int l = (vtop->type.t & VT_BTYPE) == VT_LLONG;
1207 if (t == VT_LDOUBLE) {
1208 int func = l ?
1209 (u ? TOK___floatunditf : TOK___floatditf) :
1210 (u ? TOK___floatunsitf : TOK___floatsitf);
1211 vpush_global_sym(&func_old_type, func);
1212 vrott(2);
1213 gfunc_call(1);
1214 vpushi(0);
1215 vtop->type.t = t;
1216 vtop->r = REG_IRET;
1217 vtop->r2 = TREG_R(1);
1218 } else {
1219 vtop--;
1220 dr = get_reg(RC_FLOAT);
1221 vtop++;
1222 vtop->r = dr;
1223 dr = freg(dr);
1224 EIu(0x53, 7, dr, rr, ((0x68 | (t == VT_DOUBLE ? 1 : 0)) << 5) | (u ? 1 : 0) | (l ? 2 : 0)); // fcvt.[sd].[wl][u]
1228 ST_FUNC void gen_cvt_ftoi(int t)
1230 int ft = vtop->type.t & VT_BTYPE;
1231 int l = (t & VT_BTYPE) == VT_LLONG;
1232 int u = t & VT_UNSIGNED;
1233 if (ft == VT_LDOUBLE) {
1234 int func = l ?
1235 (u ? TOK___fixunstfdi : TOK___fixtfdi) :
1236 (u ? TOK___fixunstfsi : TOK___fixtfsi);
1237 vpush_global_sym(&func_old_type, func);
1238 vrott(2);
1239 gfunc_call(1);
1240 vpushi(0);
1241 vtop->type.t = t;
1242 vtop->r = REG_IRET;
1243 } else {
1244 int rr = freg(gv(RC_FLOAT)), dr;
1245 vtop--;
1246 dr = get_reg(RC_INT);
1247 vtop++;
1248 vtop->r = dr;
1249 dr = ireg(dr);
1250 EIu(0x53, 1, dr, rr, ((0x60 | (ft == VT_DOUBLE ? 1 : 0)) << 5) | (u ? 1 : 0) | (l ? 2 : 0)); // fcvt.[wl][u].[sd] rtz
1254 ST_FUNC void gen_cvt_ftof(int dt)
1256 int st = vtop->type.t & VT_BTYPE, rs, rd;
1257 dt &= VT_BTYPE;
1258 if (st == dt)
1259 return;
1260 if (dt == VT_LDOUBLE || st == VT_LDOUBLE) {
1261 int func = (dt == VT_LDOUBLE) ?
1262 (st == VT_FLOAT ? TOK___extendsftf2 : TOK___extenddftf2) :
1263 (dt == VT_FLOAT ? TOK___trunctfsf2 : TOK___trunctfdf2);
1264 /* We can't use gfunc_call, as func_old_type works like vararg
1265 functions, and on riscv unnamed float args are passed like
1266 integers. But we really need them in the float argument registers
1267 for extendsftf2/extenddftf2. So, do it explicitely. */
1268 save_regs(1);
1269 if (dt == VT_LDOUBLE)
1270 gv(RC_F(0));
1271 else {
1272 gv(RC_R(0));
1273 assert(vtop->r2 < 7);
1274 if (vtop->r2 != 1 + vtop->r) {
1275 EI(0x13, 0, ireg(vtop->r) + 1, ireg(vtop->r2), 0); // mv Ra+1, RR2
1276 vtop->r2 = 1 + vtop->r;
1279 vpush_global_sym(&func_old_type, func);
1280 gcall_or_jmp(1);
1281 vtop -= 2;
1282 vpushi(0);
1283 vtop->type.t = dt;
1284 if (dt == VT_LDOUBLE)
1285 vtop->r = REG_IRET, vtop->r2 = REG_IRET+1;
1286 else
1287 vtop->r = REG_FRET;
1288 } else {
1289 assert (dt == VT_FLOAT || dt == VT_DOUBLE);
1290 assert (st == VT_FLOAT || st == VT_DOUBLE);
1291 rs = gv(RC_FLOAT);
1292 rd = get_reg(RC_FLOAT);
1293 if (dt == VT_DOUBLE)
1294 EI(0x53, 0, freg(rd), freg(rs), 0x21 << 5); // fcvt.d.s RD, RS (no rm)
1295 else
1296 EI(0x53, 7, freg(rd), freg(rs), (0x20 << 5) | 1); // fcvt.s.d RD, RS (dyn rm)
1297 vtop->r = rd;
1301 ST_FUNC void ggoto(void)
1303 gcall_or_jmp(0);
1304 vtop--;
1307 ST_FUNC void gen_vla_sp_save(int addr)
1309 ES(0x23, 3, 8, 2, addr); // sd sp, fc(s0)
1312 ST_FUNC void gen_vla_sp_restore(int addr)
1314 EI(0x03, 3, 2, 8, addr); // ld sp, fc(s0)
1317 ST_FUNC void gen_vla_alloc(CType *type, int align)
1319 int rr;
1320 #if defined(CONFIG_TCC_BCHECK)
1321 if (tcc_state->do_bounds_check)
1322 vpushv(vtop);
1323 #endif
1324 rr = ireg(gv(RC_INT));
1325 EI(0x13, 0, rr, rr, 15); // addi RR, RR, 15
1326 EI(0x13, 7, rr, rr, -16); // andi, RR, RR, -16
1327 ER(0x33, 0, 2, 2, rr, 0x20); // sub sp, sp, rr
1328 vpop();
1329 #if defined(CONFIG_TCC_BCHECK)
1330 if (tcc_state->do_bounds_check) {
1331 vpushi(0);
1332 vtop->r = TREG_R(0);
1333 o(0x00010513); /* mv a0,sp */
1334 vswap();
1335 vpush_global_sym(&func_old_type, TOK___bound_new_region);
1336 vrott(3);
1337 gfunc_call(2);
1338 func_bound_add_epilog = 1;
1340 #endif
1342 #endif