-W[no-]error...: features by shrinkage
[tinycc.git] / riscv64-gen.c
blob04b591f93c0beba9c38ea3ac43c0d6d0f5cc1cdb
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 ST_DATA const char * const target_machine_defs =
40 "__riscv\0"
41 "__riscv_xlen 64\0"
42 "__riscv_flen 64\0"
43 "__riscv_div\0"
44 "__riscv_mul\0"
45 "__riscv_fdiv\0"
46 "__riscv_fsqrt\0"
47 "__riscv_float_abi_double\0"
50 #define XLEN 8
52 #define TREG_RA 17
53 #define TREG_SP 18
55 ST_DATA const int reg_classes[NB_REGS] = {
56 RC_INT | RC_R(0),
57 RC_INT | RC_R(1),
58 RC_INT | RC_R(2),
59 RC_INT | RC_R(3),
60 RC_INT | RC_R(4),
61 RC_INT | RC_R(5),
62 RC_INT | RC_R(6),
63 RC_INT | RC_R(7),
64 RC_FLOAT | RC_F(0),
65 RC_FLOAT | RC_F(1),
66 RC_FLOAT | RC_F(2),
67 RC_FLOAT | RC_F(3),
68 RC_FLOAT | RC_F(4),
69 RC_FLOAT | RC_F(5),
70 RC_FLOAT | RC_F(6),
71 RC_FLOAT | RC_F(7),
73 1 << TREG_RA,
74 1 << TREG_SP
77 #if defined(CONFIG_TCC_BCHECK)
78 static addr_t func_bound_offset;
79 static unsigned long func_bound_ind;
80 ST_DATA int func_bound_add_epilog;
81 #endif
83 static int ireg(int r)
85 if (r == TREG_RA)
86 return 1; // ra
87 if (r == TREG_SP)
88 return 2; // sp
89 assert(r >= 0 && r < 8);
90 return r + 10; // tccrX --> aX == x(10+X)
93 static int is_ireg(int r)
95 return (unsigned)r < 8 || r == TREG_RA || r == TREG_SP;
98 static int freg(int r)
100 assert(r >= 8 && r < 16);
101 return r - 8 + 10; // tccfX --> faX == f(10+X)
104 static int is_freg(int r)
106 return r >= 8 && r < 16;
109 ST_FUNC void o(unsigned int c)
111 int ind1 = ind + 4;
112 if (nocode_wanted)
113 return;
114 if (ind1 > cur_text_section->data_allocated)
115 section_realloc(cur_text_section, ind1);
116 write32le(cur_text_section->data + ind, c);
117 ind = ind1;
120 static void EIu(uint32_t opcode, uint32_t func3,
121 uint32_t rd, uint32_t rs1, uint32_t imm)
123 o(opcode | (func3 << 12) | (rd << 7) | (rs1 << 15) | (imm << 20));
126 static void ER(uint32_t opcode, uint32_t func3,
127 uint32_t rd, uint32_t rs1, uint32_t rs2, uint32_t func7)
129 o(opcode | func3 << 12 | rd << 7 | rs1 << 15 | rs2 << 20 | func7 << 25);
132 static void EI(uint32_t opcode, uint32_t func3,
133 uint32_t rd, uint32_t rs1, uint32_t imm)
135 assert(! ((imm + (1 << 11)) >> 12));
136 EIu(opcode, func3, rd, rs1, imm);
139 static void ES(uint32_t opcode, uint32_t func3,
140 uint32_t rs1, uint32_t rs2, uint32_t imm)
142 assert(! ((imm + (1 << 11)) >> 12));
143 o(opcode | (func3 << 12) | ((imm & 0x1f) << 7) | (rs1 << 15)
144 | (rs2 << 20) | ((imm >> 5) << 25));
147 // Patch all branches in list pointed to by t to branch to a:
148 ST_FUNC void gsym_addr(int t_, int a_)
150 uint32_t t = t_;
151 uint32_t a = a_;
152 while (t) {
153 unsigned char *ptr = cur_text_section->data + t;
154 uint32_t next = read32le(ptr);
155 uint32_t r = a - t, imm;
156 if ((r + (1 << 21)) & ~((1U << 22) - 2))
157 tcc_error("out-of-range branch chain");
158 imm = (((r >> 12) & 0xff) << 12)
159 | (((r >> 11) & 1) << 20)
160 | (((r >> 1) & 0x3ff) << 21)
161 | (((r >> 20) & 1) << 31);
162 write32le(ptr, r == 4 ? 0x33 : 0x6f | imm); // nop || j imm
163 t = next;
167 static int load_symofs(int r, SValue *sv, int forstore)
169 int rr, doload = 0;
170 int fc = sv->c.i, v = sv->r & VT_VALMASK;
171 if (sv->r & VT_SYM) {
172 Sym label = {0};
173 assert(v == VT_CONST);
174 if (sv->sym->type.t & VT_STATIC) { // XXX do this per linker relax
175 greloca(cur_text_section, sv->sym, ind,
176 R_RISCV_PCREL_HI20, sv->c.i);
177 sv->c.i = 0;
178 } else {
179 if (((unsigned)fc + (1 << 11)) >> 12)
180 tcc_error("unimp: large addend for global address (0x%lx)", (long)sv->c.i);
181 greloca(cur_text_section, sv->sym, ind,
182 R_RISCV_GOT_HI20, 0);
183 doload = 1;
185 label.type.t = VT_VOID | VT_STATIC;
186 put_extern_sym(&label, cur_text_section, ind, 0);
187 rr = is_ireg(r) ? ireg(r) : 5;
188 o(0x17 | (rr << 7)); // auipc RR, 0 %pcrel_hi(sym)+addend
189 greloca(cur_text_section, &label, ind,
190 doload || !forstore
191 ? R_RISCV_PCREL_LO12_I : R_RISCV_PCREL_LO12_S, 0);
192 if (doload) {
193 EI(0x03, 3, rr, rr, 0); // ld RR, 0(RR)
195 } else if (v == VT_LOCAL || v == VT_LLOCAL) {
196 rr = 8; // s0
197 if (fc != sv->c.i)
198 tcc_error("unimp: store(giant local off) (0x%lx)", (long)sv->c.i);
199 if (((unsigned)fc + (1 << 11)) >> 12) {
200 rr = is_ireg(r) ? ireg(r) : 5; // t0
201 o(0x37 | (rr << 7) | ((0x800 + fc) & 0xfffff000)); //lui RR, upper(fc)
202 ER(0x33, 0, rr, rr, 8, 0); // add RR, RR, s0
203 sv->c.i = fc << 20 >> 20;
205 } else
206 tcc_error("uhh");
207 return rr;
210 static void load_large_constant(int rr, int fc, uint32_t pi)
212 if (fc < 0)
213 pi++;
214 o(0x37 | (rr << 7) | (((pi + 0x800) & 0xfffff000))); // lui RR, up(up(fc))
215 EI(0x13, 0, rr, rr, (int)pi << 20 >> 20); // addi RR, RR, lo(up(fc))
216 EI(0x13, 1, rr, rr, 12); // slli RR, RR, 12
217 EI(0x13, 0, rr, rr, (fc + (1 << 19)) >> 20); // addi RR, RR, up(lo(fc))
218 EI(0x13, 1, rr, rr, 12); // slli RR, RR, 12
219 fc = fc << 12 >> 12;
220 EI(0x13, 0, rr, rr, fc >> 8); // addi RR, RR, lo1(lo(fc))
221 EI(0x13, 1, rr, rr, 8); // slli RR, RR, 8
224 ST_FUNC void load(int r, SValue *sv)
226 int fr = sv->r;
227 int v = fr & VT_VALMASK;
228 int rr = is_ireg(r) ? ireg(r) : freg(r);
229 int fc = sv->c.i;
230 int bt = sv->type.t & VT_BTYPE;
231 int align, size;
232 if (fr & VT_LVAL) {
233 int func3, opcode = is_freg(r) ? 0x07 : 0x03, br;
234 size = type_size(&sv->type, &align);
235 assert (!is_freg(r) || bt == VT_FLOAT || bt == VT_DOUBLE);
236 if (bt == VT_FUNC) /* XXX should be done in generic code */
237 size = PTR_SIZE;
238 func3 = size == 1 ? 0 : size == 2 ? 1 : size == 4 ? 2 : 3;
239 if (size < 4 && !is_float(sv->type.t) && (sv->type.t & VT_UNSIGNED))
240 func3 |= 4;
241 if (v == VT_LOCAL || (fr & VT_SYM)) {
242 br = load_symofs(r, sv, 0);
243 fc = sv->c.i;
244 } else if (v < VT_CONST) {
245 br = ireg(v);
246 /*if (((unsigned)fc + (1 << 11)) >> 12)
247 tcc_error("unimp: load(large addend) (0x%x)", fc);*/
248 fc = 0; // XXX store ofs in LVAL(reg)
249 } else if (v == VT_LLOCAL) {
250 br = load_symofs(r, sv, 0);
251 fc = sv->c.i;
252 EI(0x03, 3, rr, br, fc); // ld RR, fc(BR)
253 br = rr;
254 fc = 0;
255 } else if (v == VT_CONST) {
256 int64_t si = sv->c.i;
257 si >>= 32;
258 if (si != 0) {
259 load_large_constant(rr, fc, si);
260 fc &= 0xff;
261 } else {
262 o(0x37 | (rr << 7) | ((0x800 + fc) & 0xfffff000)); //lui RR, upper(fc)
263 fc = fc << 20 >> 20;
265 br = rr;
266 } else {
267 tcc_error("unimp: load(non-local lval)");
269 EI(opcode, func3, rr, br, fc); // l[bhwd][u] / fl[wd] RR, fc(BR)
270 } else if (v == VT_CONST) {
271 int rb = 0, do32bit = 8, zext = 0;
272 assert((!is_float(sv->type.t) && is_ireg(r)) || bt == VT_LDOUBLE);
273 if (fr & VT_SYM) {
274 rb = load_symofs(r, sv, 0);
275 fc = sv->c.i;
276 do32bit = 0;
278 if (is_float(sv->type.t) && bt != VT_LDOUBLE)
279 tcc_error("unimp: load(float)");
280 if (fc != sv->c.i) {
281 int64_t si = sv->c.i;
282 si >>= 32;
283 if (si != 0) {
284 load_large_constant(rr, fc, si);
285 fc &= 0xff;
286 rb = rr;
287 do32bit = 0;
288 } else if (bt == VT_LLONG) {
289 /* A 32bit unsigned constant for a 64bit type.
290 lui always sign extends, so we need to do an explicit zext.*/
291 zext = 1;
294 if (((unsigned)fc + (1 << 11)) >> 12)
295 o(0x37 | (rr << 7) | ((0x800 + fc) & 0xfffff000)), rb = rr; //lui RR, upper(fc)
296 if (fc || (rr != rb) || do32bit || (fr & VT_SYM))
297 EI(0x13 | do32bit, 0, rr, rb, fc << 20 >> 20); // addi[w] R, x0|R, FC
298 if (zext) {
299 EI(0x13, 1, rr, rr, 32); // slli RR, RR, 32
300 EI(0x13, 5, rr, rr, 32); // srli RR, RR, 32
302 } else if (v == VT_LOCAL) {
303 int br = load_symofs(r, sv, 0);
304 assert(is_ireg(r));
305 fc = sv->c.i;
306 EI(0x13, 0, rr, br, fc); // addi R, s0, FC
307 } else if (v < VT_CONST) { /* reg-reg */
308 //assert(!fc); XXX support offseted regs
309 if (is_freg(r) && is_freg(v))
310 ER(0x53, 0, rr, freg(v), freg(v), bt == VT_DOUBLE ? 0x11 : 0x10); //fsgnj.[sd] RR, V, V == fmv.[sd] RR, V
311 else if (is_ireg(r) && is_ireg(v))
312 EI(0x13, 0, rr, ireg(v), 0); // addi RR, V, 0 == mv RR, V
313 else {
314 int func7 = is_ireg(r) ? 0x70 : 0x78;
315 size = type_size(&sv->type, &align);
316 if (size == 8)
317 func7 |= 1;
318 assert(size == 4 || size == 8);
319 o(0x53 | (rr << 7) | ((is_freg(v) ? freg(v) : ireg(v)) << 15)
320 | (func7 << 25)); // fmv.{w.x, x.w, d.x, x.d} RR, VR
322 } else if (v == VT_CMP) {
323 int op = vtop->cmp_op;
324 int a = vtop->cmp_r & 0xff;
325 int b = (vtop->cmp_r >> 8) & 0xff;
326 int inv = 0;
327 switch (op) {
328 case TOK_ULT:
329 case TOK_UGE:
330 case TOK_ULE:
331 case TOK_UGT:
332 case TOK_LT:
333 case TOK_GE:
334 case TOK_LE:
335 case TOK_GT:
336 if (op & 1) { // remove [U]GE,GT
337 inv = 1;
338 op--;
340 if ((op & 7) == 6) { // [U]LE
341 int t = a; a = b; b = t;
342 inv ^= 1;
344 ER(0x33, (op > TOK_UGT) ? 2 : 3, rr, a, b, 0); // slt[u] d, a, b
345 if (inv)
346 EI(0x13, 4, rr, rr, 1); // xori d, d, 1
347 break;
348 case TOK_NE:
349 case TOK_EQ:
350 if (rr != a || b)
351 ER(0x33, 0, rr, a, b, 0x20); // sub d, a, b
352 if (op == TOK_NE)
353 ER(0x33, 3, rr, 0, rr, 0); // sltu d, x0, d == snez d,d
354 else
355 EI(0x13, 3, rr, rr, 1); // sltiu d, d, 1 == seqz d,d
356 break;
358 } else if ((v & ~1) == VT_JMP) {
359 int t = v & 1;
360 assert(is_ireg(r));
361 EI(0x13, 0, rr, 0, t); // addi RR, x0, t
362 gjmp_addr(ind + 8);
363 gsym(fc);
364 EI(0x13, 0, rr, 0, t ^ 1); // addi RR, x0, !t
365 } else
366 tcc_error("unimp: load(non-const)");
369 ST_FUNC void store(int r, SValue *sv)
371 int fr = sv->r & VT_VALMASK;
372 int rr = is_ireg(r) ? ireg(r) : freg(r), ptrreg;
373 int fc = sv->c.i;
374 int bt = sv->type.t & VT_BTYPE;
375 int align, size = type_size(&sv->type, &align);
376 assert(!is_float(bt) || is_freg(r) || bt == VT_LDOUBLE);
377 /* long doubles are in two integer registers, but the load/store
378 primitives only deal with one, so do as if it's one reg. */
379 if (bt == VT_LDOUBLE)
380 size = align = 8;
381 if (bt == VT_STRUCT)
382 tcc_error("unimp: store(struct)");
383 if (size > 8)
384 tcc_error("unimp: large sized store");
385 assert(sv->r & VT_LVAL);
386 if (fr == VT_LOCAL || (sv->r & VT_SYM)) {
387 ptrreg = load_symofs(-1, sv, 1);
388 fc = sv->c.i;
389 } else if (fr < VT_CONST) {
390 ptrreg = ireg(fr);
391 /*if (((unsigned)fc + (1 << 11)) >> 12)
392 tcc_error("unimp: store(large addend) (0x%x)", fc);*/
393 fc = 0; // XXX support offsets regs
394 } else if (fr == VT_CONST) {
395 int64_t si = sv->c.i;
396 ptrreg = 8; // s0
397 si >>= 32;
398 if (si != 0) {
399 load_large_constant(ptrreg, fc, si);
400 fc &= 0xff;
401 } else {
402 o(0x37 | (ptrreg << 7) | ((0x800 + fc) & 0xfffff000)); //lui RR, upper(fc)
403 fc = fc << 20 >> 20;
405 } else
406 tcc_error("implement me: %s(!local)", __FUNCTION__);
407 ES(is_freg(r) ? 0x27 : 0x23, // fs... | s...
408 size == 1 ? 0 : size == 2 ? 1 : size == 4 ? 2 : 3, // ... [wd] | [bhwd]
409 ptrreg, rr, fc); // RR, fc(base)
412 static void gcall_or_jmp(int docall)
414 int tr = docall ? 1 : 5; // ra or t0
415 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST &&
416 ((vtop->r & VT_SYM) && vtop->c.i == (int)vtop->c.i)) {
417 /* constant symbolic case -> simple relocation */
418 greloca(cur_text_section, vtop->sym, ind,
419 R_RISCV_CALL_PLT, (int)vtop->c.i);
420 o(0x17 | (tr << 7)); // auipc TR, 0 %call(func)
421 EI(0x67, 0, tr, tr, 0);// jalr TR, r(TR)
422 } else if (vtop->r < VT_CONST) {
423 int r = ireg(vtop->r);
424 EI(0x67, 0, tr, r, 0); // jalr TR, 0(R)
425 } else {
426 int r = TREG_RA;
427 load(r, vtop);
428 r = ireg(r);
429 EI(0x67, 0, tr, r, 0); // jalr TR, 0(R)
433 #if defined(CONFIG_TCC_BCHECK)
435 static void gen_bounds_call(int v)
437 Sym *sym = external_helper_sym(v);
439 greloca(cur_text_section, sym, ind, R_RISCV_CALL_PLT, 0);
440 o(0x17 | (1 << 7)); // auipc TR, 0 %call(func)
441 EI(0x67, 0, 1, 1, 0); // jalr TR, r(TR)
444 static void gen_bounds_prolog(void)
446 /* leave some room for bound checking code */
447 func_bound_offset = lbounds_section->data_offset;
448 func_bound_ind = ind;
449 func_bound_add_epilog = 0;
450 o(0x00000013); /* ld a0,#lbound section pointer */
451 o(0x00000013);
452 o(0x00000013); /* nop -> call __bound_local_new */
453 o(0x00000013);
456 static void gen_bounds_epilog(void)
458 addr_t saved_ind;
459 addr_t *bounds_ptr;
460 Sym *sym_data;
461 Sym label = {0};
463 int offset_modified = func_bound_offset != lbounds_section->data_offset;
465 if (!offset_modified && !func_bound_add_epilog)
466 return;
468 /* add end of table info */
469 bounds_ptr = section_ptr_add(lbounds_section, sizeof(addr_t));
470 *bounds_ptr = 0;
472 sym_data = get_sym_ref(&char_pointer_type, lbounds_section,
473 func_bound_offset, lbounds_section->data_offset);
475 label.type.t = VT_VOID | VT_STATIC;
476 /* generate bound local allocation */
477 if (offset_modified) {
478 saved_ind = ind;
479 ind = func_bound_ind;
480 put_extern_sym(&label, cur_text_section, ind, 0);
481 greloca(cur_text_section, sym_data, ind, R_RISCV_GOT_HI20, 0);
482 o(0x17 | (10 << 7)); // auipc a0, 0 %pcrel_hi(sym)+addend
483 greloca(cur_text_section, &label, ind, R_RISCV_PCREL_LO12_I, 0);
484 EI(0x03, 3, 10, 10, 0); // ld a0, 0(a0)
485 gen_bounds_call(TOK___bound_local_new);
486 ind = saved_ind;
487 label.c = 0; /* force new local ELF symbol */
490 /* generate bound check local freeing */
491 o(0xe02a1101); /* addi sp,sp,-32 sd a0,0(sp) */
492 o(0xa82ae42e); /* sd a1,8(sp) fsd fa0,16(sp) */
493 put_extern_sym(&label, cur_text_section, ind, 0);
494 greloca(cur_text_section, sym_data, ind, R_RISCV_GOT_HI20, 0);
495 o(0x17 | (10 << 7)); // auipc a0, 0 %pcrel_hi(sym)+addend
496 greloca(cur_text_section, &label, ind, R_RISCV_PCREL_LO12_I, 0);
497 EI(0x03, 3, 10, 10, 0); // ld a0, 0(a0)
498 gen_bounds_call(TOK___bound_local_delete);
499 o(0x65a26502); /* ld a0,0(sp) ld a1,8(sp) */
500 o(0x61052542); /* fld fa0,16(sp) addi sp,sp,32 */
502 #endif
504 static void reg_pass_rec(CType *type, int *rc, int *fieldofs, int ofs)
506 if ((type->t & VT_BTYPE) == VT_STRUCT) {
507 Sym *f;
508 if (type->ref->type.t == VT_UNION)
509 rc[0] = -1;
510 else for (f = type->ref->next; f; f = f->next)
511 reg_pass_rec(&f->type, rc, fieldofs, ofs + f->c);
512 } else if (type->t & VT_ARRAY) {
513 if (type->ref->c < 0 || type->ref->c > 2)
514 rc[0] = -1;
515 else {
516 int a, sz = type_size(&type->ref->type, &a);
517 reg_pass_rec(&type->ref->type, rc, fieldofs, ofs);
518 if (rc[0] > 2 || (rc[0] == 2 && type->ref->c > 1))
519 rc[0] = -1;
520 else if (type->ref->c == 2 && rc[0] && rc[1] == RC_FLOAT) {
521 rc[++rc[0]] = RC_FLOAT;
522 fieldofs[rc[0]] = ((ofs + sz) << 4)
523 | (type->ref->type.t & VT_BTYPE);
524 } else if (type->ref->c == 2)
525 rc[0] = -1;
527 } else if (rc[0] == 2 || rc[0] < 0 || (type->t & VT_BTYPE) == VT_LDOUBLE)
528 rc[0] = -1;
529 else if (!rc[0] || rc[1] == RC_FLOAT || is_float(type->t)) {
530 rc[++rc[0]] = is_float(type->t) ? RC_FLOAT : RC_INT;
531 fieldofs[rc[0]] = (ofs << 4) | ((type->t & VT_BTYPE) == VT_PTR ? VT_LLONG : type->t & VT_BTYPE);
532 } else
533 rc[0] = -1;
536 static void reg_pass(CType *type, int *prc, int *fieldofs, int named)
538 prc[0] = 0;
539 reg_pass_rec(type, prc, fieldofs, 0);
540 if (prc[0] <= 0 || !named) {
541 int align, size = type_size(type, &align);
542 prc[0] = (size + 7) >> 3;
543 prc[1] = prc[2] = RC_INT;
544 fieldofs[1] = (0 << 4) | (size <= 1 ? VT_BYTE : size <= 2 ? VT_SHORT : size <= 4 ? VT_INT : VT_LLONG);
545 fieldofs[2] = (8 << 4) | (size <= 9 ? VT_BYTE : size <= 10 ? VT_SHORT : size <= 12 ? VT_INT : VT_LLONG);
549 ST_FUNC void gfunc_call(int nb_args)
551 int i, align, size, areg[2];
552 int *info = tcc_malloc((nb_args + 1) * sizeof (int));
553 int stack_adj = 0, tempspace = 0, stack_add, ofs, splitofs = 0;
554 SValue *sv;
555 Sym *sa;
557 #ifdef CONFIG_TCC_BCHECK
558 int bc_save = tcc_state->do_bounds_check;
559 if (tcc_state->do_bounds_check)
560 gbound_args(nb_args);
561 #endif
563 areg[0] = 0; /* int arg regs */
564 areg[1] = 8; /* float arg regs */
565 sa = vtop[-nb_args].type.ref->next;
566 for (i = 0; i < nb_args; i++) {
567 int nregs, byref = 0, tempofs;
568 int prc[3], fieldofs[3];
569 sv = &vtop[1 + i - nb_args];
570 sv->type.t &= ~VT_ARRAY; // XXX this should be done in tccgen.c
571 size = type_size(&sv->type, &align);
572 if (size > 16) {
573 if (align < XLEN)
574 align = XLEN;
575 tempspace = (tempspace + align - 1) & -align;
576 tempofs = tempspace;
577 tempspace += size;
578 size = align = 8;
579 byref = 64 | (tempofs << 7);
581 reg_pass(&sv->type, prc, fieldofs, sa != 0);
582 if (!sa && align == 2*XLEN && size <= 2*XLEN)
583 areg[0] = (areg[0] + 1) & ~1;
584 nregs = prc[0];
585 if (size == 0)
586 info[i] = 0;
587 else if ((prc[1] == RC_INT && areg[0] >= 8)
588 || (prc[1] == RC_FLOAT && areg[1] >= 16)
589 || (nregs == 2 && prc[1] == RC_FLOAT && prc[2] == RC_FLOAT
590 && areg[1] >= 15)
591 || (nregs == 2 && prc[1] != prc[2]
592 && (areg[1] >= 16 || areg[0] >= 8))) {
593 info[i] = 32;
594 if (align < XLEN)
595 align = XLEN;
596 stack_adj += (size + align - 1) & -align;
597 if (!sa) /* one vararg on stack forces the rest on stack */
598 areg[0] = 8, areg[1] = 16;
599 } else {
600 info[i] = areg[prc[1] - 1]++;
601 if (!byref)
602 info[i] |= (fieldofs[1] & VT_BTYPE) << 12;
603 assert(!(fieldofs[1] >> 4));
604 if (nregs == 2) {
605 if (prc[2] == RC_FLOAT || areg[0] < 8)
606 info[i] |= (1 + areg[prc[2] - 1]++) << 7;
607 else {
608 info[i] |= 16;
609 stack_adj += 8;
611 if (!byref) {
612 assert((fieldofs[2] >> 4) < 2048);
613 info[i] |= fieldofs[2] << (12 + 4); // includes offset
617 info[i] |= byref;
618 if (sa)
619 sa = sa->next;
621 stack_adj = (stack_adj + 15) & -16;
622 tempspace = (tempspace + 15) & -16;
623 stack_add = stack_adj + tempspace;
625 /* fetch cpu flag before generating any code */
626 if ((vtop->r & VT_VALMASK) == VT_CMP)
627 gv(RC_INT);
629 if (stack_add) {
630 if (stack_add >= 0x1000) {
631 o(0x37 | (5 << 7) | (-stack_add & 0xfffff000)); //lui t0, upper(v)
632 EI(0x13, 0, 5, 5, -stack_add << 20 >> 20); // addi t0, t0, lo(v)
633 ER(0x33, 0, 2, 2, 5, 0); // add sp, sp, t0
635 else
636 EI(0x13, 0, 2, 2, -stack_add); // addi sp, sp, -adj
637 for (i = ofs = 0; i < nb_args; i++) {
638 if (info[i] & (64 | 32)) {
639 vrotb(nb_args - i);
640 size = type_size(&vtop->type, &align);
641 if (info[i] & 64) {
642 vset(&char_pointer_type, TREG_SP, 0);
643 vpushi(stack_adj + (info[i] >> 7));
644 gen_op('+');
645 vpushv(vtop); // this replaces the old argument
646 vrott(3);
647 indir();
648 vtop->type = vtop[-1].type;
649 vswap();
650 vstore();
651 vpop();
652 size = align = 8;
654 if (info[i] & 32) {
655 if (align < XLEN)
656 align = XLEN;
657 /* Once we support offseted regs we can do this:
658 vset(&vtop->type, TREG_SP | VT_LVAL, ofs);
659 to construct the lvalue for the outgoing stack slot,
660 until then we have to jump through hoops. */
661 vset(&char_pointer_type, TREG_SP, 0);
662 ofs = (ofs + align - 1) & -align;
663 vpushi(ofs);
664 gen_op('+');
665 indir();
666 vtop->type = vtop[-1].type;
667 vswap();
668 vstore();
669 vtop->r = vtop->r2 = VT_CONST; // this arg is done
670 ofs += size;
672 vrott(nb_args - i);
673 } else if (info[i] & 16) {
674 assert(!splitofs);
675 splitofs = ofs;
676 ofs += 8;
680 for (i = 0; i < nb_args; i++) {
681 int ii = info[nb_args - 1 - i], r = ii, r2 = r;
682 if (!(r & 32)) {
683 CType origtype;
684 int loadt;
685 r &= 15;
686 r2 = r2 & 64 ? 0 : (r2 >> 7) & 31;
687 assert(r2 <= 16);
688 vrotb(i+1);
689 origtype = vtop->type;
690 size = type_size(&vtop->type, &align);
691 if (size == 0)
692 goto done;
693 loadt = vtop->type.t & VT_BTYPE;
694 if (loadt == VT_STRUCT) {
695 loadt = (ii >> 12) & VT_BTYPE;
697 if (info[nb_args - 1 - i] & 16) {
698 assert(!r2);
699 r2 = 1 + TREG_RA;
701 if (loadt == VT_LDOUBLE) {
702 assert(r2);
703 r2--;
704 } else if (r2) {
705 test_lvalue();
706 vpushv(vtop);
708 vtop->type.t = loadt | (vtop->type.t & VT_UNSIGNED);
709 gv(r < 8 ? RC_R(r) : RC_F(r - 8));
710 vtop->type = origtype;
712 if (r2 && loadt != VT_LDOUBLE) {
713 r2--;
714 assert(r2 < 16 || r2 == TREG_RA);
715 vswap();
716 gaddrof();
717 vtop->type = char_pointer_type;
718 vpushi(ii >> 20);
719 #ifdef CONFIG_TCC_BCHECK
720 if ((origtype.t & VT_BTYPE) == VT_STRUCT)
721 tcc_state->do_bounds_check = 0;
722 #endif
723 gen_op('+');
724 #ifdef CONFIG_TCC_BCHECK
725 tcc_state->do_bounds_check = bc_save;
726 #endif
727 indir();
728 vtop->type = origtype;
729 loadt = vtop->type.t & VT_BTYPE;
730 if (loadt == VT_STRUCT) {
731 loadt = (ii >> 16) & VT_BTYPE;
733 save_reg_upstack(r2, 1);
734 vtop->type.t = loadt | (vtop->type.t & VT_UNSIGNED);
735 load(r2, vtop);
736 assert(r2 < VT_CONST);
737 vtop--;
738 vtop->r2 = r2;
740 if (info[nb_args - 1 - i] & 16) {
741 ES(0x23, 3, 2, ireg(vtop->r2), splitofs); // sd t0, ofs(sp)
742 vtop->r2 = VT_CONST;
743 } else if (loadt == VT_LDOUBLE && vtop->r2 != r2) {
744 assert(vtop->r2 <= 7 && r2 <= 7);
745 /* XXX we'd like to have 'gv' move directly into
746 the right class instead of us fixing it up. */
747 EI(0x13, 0, ireg(r2), ireg(vtop->r2), 0); // mv Ra+1, RR2
748 vtop->r2 = r2;
750 done:
751 vrott(i+1);
754 vrotb(nb_args + 1);
755 save_regs(nb_args + 1);
756 gcall_or_jmp(1);
757 vtop -= nb_args + 1;
758 if (stack_add) {
759 if (stack_add >= 0x1000) {
760 o(0x37 | (5 << 7) | (stack_add & 0xfffff000)); //lui t0, upper(v)
761 EI(0x13, 0, 5, 5, stack_add << 20 >> 20); // addi t0, t0, lo(v)
762 ER(0x33, 0, 2, 2, 5, 0); // add sp, sp, t0
764 else
765 EI(0x13, 0, 2, 2, stack_add); // addi sp, sp, adj
767 tcc_free(info);
770 static int func_sub_sp_offset, num_va_regs, func_va_list_ofs;
772 ST_FUNC void gfunc_prolog(Sym *func_sym)
774 CType *func_type = &func_sym->type;
775 int i, addr, align, size;
776 int param_addr = 0;
777 int areg[2];
778 Sym *sym;
779 CType *type;
781 sym = func_type->ref;
782 loc = -16; // for ra and s0
783 func_sub_sp_offset = ind;
784 ind += 5 * 4;
786 areg[0] = 0, areg[1] = 0;
787 addr = 0;
788 /* if the function returns by reference, then add an
789 implicit pointer parameter */
790 size = type_size(&func_vt, &align);
791 if (size > 2 * XLEN) {
792 loc -= 8;
793 func_vc = loc;
794 ES(0x23, 3, 8, 10 + areg[0]++, loc); // sd a0, loc(s0)
796 /* define parameters */
797 while ((sym = sym->next) != NULL) {
798 int byref = 0;
799 int regcount;
800 int prc[3], fieldofs[3];
801 type = &sym->type;
802 size = type_size(type, &align);
803 if (size > 2 * XLEN) {
804 type = &char_pointer_type;
805 size = align = byref = 8;
807 reg_pass(type, prc, fieldofs, 1);
808 regcount = prc[0];
809 if (areg[prc[1] - 1] >= 8
810 || (regcount == 2
811 && ((prc[1] == RC_FLOAT && prc[2] == RC_FLOAT && areg[1] >= 7)
812 || (prc[1] != prc[2] && (areg[1] >= 8 || areg[0] >= 8))))) {
813 if (align < XLEN)
814 align = XLEN;
815 addr = (addr + align - 1) & -align;
816 param_addr = addr;
817 addr += size;
818 } else {
819 loc -= regcount * 8; // XXX could reserve only 'size' bytes
820 param_addr = loc;
821 for (i = 0; i < regcount; i++) {
822 if (areg[prc[1+i] - 1] >= 8) {
823 assert(i == 1 && regcount == 2 && !(addr & 7));
824 EI(0x03, 3, 5, 8, addr); // ld t0, addr(s0)
825 addr += 8;
826 ES(0x23, 3, 8, 5, loc + i*8); // sd t0, loc(s0)
827 } else if (prc[1+i] == RC_FLOAT) {
828 ES(0x27, (size / regcount) == 4 ? 2 : 3, 8, 10 + areg[1]++, loc + (fieldofs[i+1] >> 4)); // fs[wd] FAi, loc(s0)
829 } else {
830 ES(0x23, 3, 8, 10 + areg[0]++, loc + i*8); // sd aX, loc(s0) // XXX
834 sym_push(sym->v & ~SYM_FIELD, &sym->type,
835 (byref ? VT_LLOCAL : VT_LOCAL) | VT_LVAL,
836 param_addr);
838 func_va_list_ofs = addr;
839 num_va_regs = 0;
840 if (func_var) {
841 for (; areg[0] < 8; areg[0]++) {
842 num_va_regs++;
843 ES(0x23, 3, 8, 10 + areg[0], -8 + num_va_regs * 8); // sd aX, loc(s0)
846 #ifdef CONFIG_TCC_BCHECK
847 if (tcc_state->do_bounds_check)
848 gen_bounds_prolog();
849 #endif
852 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret,
853 int *ret_align, int *regsize)
855 int align, size = type_size(vt, &align), nregs;
856 int prc[3], fieldofs[3];
857 *ret_align = 1;
858 *regsize = 8;
859 if (size > 16)
860 return 0;
861 reg_pass(vt, prc, fieldofs, 1);
862 nregs = prc[0];
863 if (nregs == 2 && prc[1] != prc[2])
864 return -1; /* generic code can't deal with this case */
865 if (prc[1] == RC_FLOAT) {
866 *regsize = size / nregs;
868 ret->t = fieldofs[1] & VT_BTYPE;
869 ret->ref = NULL;
870 return nregs;
873 ST_FUNC void arch_transfer_ret_regs(int aftercall)
875 int prc[3], fieldofs[3];
876 reg_pass(&vtop->type, prc, fieldofs, 1);
877 assert(prc[0] == 2 && prc[1] != prc[2] && !(fieldofs[1] >> 4));
878 assert(vtop->r == (VT_LOCAL | VT_LVAL));
879 vpushv(vtop);
880 vtop->type.t = fieldofs[1] & VT_BTYPE;
881 (aftercall ? store : load)(prc[1] == RC_INT ? REG_IRET : REG_FRET, vtop);
882 vtop->c.i += fieldofs[2] >> 4;
883 vtop->type.t = fieldofs[2] & VT_BTYPE;
884 (aftercall ? store : load)(prc[2] == RC_INT ? REG_IRET : REG_FRET, vtop);
885 vtop--;
888 ST_FUNC void gfunc_epilog(void)
890 int v, saved_ind, d, large_ofs_ind;
892 #ifdef CONFIG_TCC_BCHECK
893 if (tcc_state->do_bounds_check)
894 gen_bounds_epilog();
895 #endif
897 loc = (loc - num_va_regs * 8);
898 d = v = (-loc + 15) & -16;
900 if (v >= (1 << 11)) {
901 d = 16;
902 o(0x37 | (5 << 7) | ((0x800 + (v-16)) & 0xfffff000)); //lui t0, upper(v)
903 EI(0x13, 0, 5, 5, (v-16) << 20 >> 20); // addi t0, t0, lo(v)
904 ER(0x33, 0, 2, 2, 5, 0); // add sp, sp, t0
906 EI(0x03, 3, 1, 2, d - 8 - num_va_regs * 8); // ld ra, v-8(sp)
907 EI(0x03, 3, 8, 2, d - 16 - num_va_regs * 8); // ld s0, v-16(sp)
908 EI(0x13, 0, 2, 2, d); // addi sp, sp, v
909 EI(0x67, 0, 0, 1, 0); // jalr x0, 0(x1), aka ret
910 large_ofs_ind = ind;
911 if (v >= (1 << 11)) {
912 EI(0x13, 0, 8, 2, d - num_va_regs * 8); // addi s0, sp, d
913 o(0x37 | (5 << 7) | ((0x800 + (v-16)) & 0xfffff000)); //lui t0, upper(v)
914 EI(0x13, 0, 5, 5, (v-16) << 20 >> 20); // addi t0, t0, lo(v)
915 ER(0x33, 0, 2, 2, 5, 0x20); // sub sp, sp, t0
916 gjmp_addr(func_sub_sp_offset + 5*4);
918 saved_ind = ind;
920 ind = func_sub_sp_offset;
921 EI(0x13, 0, 2, 2, -d); // addi sp, sp, -d
922 ES(0x23, 3, 2, 1, d - 8 - num_va_regs * 8); // sd ra, d-8(sp)
923 ES(0x23, 3, 2, 8, d - 16 - num_va_regs * 8); // sd s0, d-16(sp)
924 if (v < (1 << 11))
925 EI(0x13, 0, 8, 2, d - num_va_regs * 8); // addi s0, sp, d
926 else
927 gjmp_addr(large_ofs_ind);
928 if ((ind - func_sub_sp_offset) != 5*4)
929 EI(0x13, 0, 0, 0, 0); // addi x0, x0, 0 == nop
930 ind = saved_ind;
933 ST_FUNC void gen_va_start(void)
935 vtop--;
936 vset(&char_pointer_type, VT_LOCAL, func_va_list_ofs);
939 ST_FUNC void gen_fill_nops(int bytes)
941 if ((bytes & 3))
942 tcc_error("alignment of code section not multiple of 4");
943 while (bytes > 0) {
944 EI(0x13, 0, 0, 0, 0); // addi x0, x0, 0 == nop
945 bytes -= 4;
949 // Generate forward branch to label:
950 ST_FUNC int gjmp(int t)
952 if (nocode_wanted)
953 return t;
954 o(t);
955 return ind - 4;
958 // Generate branch to known address:
959 ST_FUNC void gjmp_addr(int a)
961 uint32_t r = a - ind, imm;
962 if ((r + (1 << 21)) & ~((1U << 22) - 2)) {
963 o(0x17 | (5 << 7) | (((r + 0x800) & 0xfffff000))); // lui RR, up(r)
964 r = (int)r << 20 >> 20;
965 EI(0x67, 0, 0, 5, r); // jalr x0, r(t0)
966 } else {
967 imm = (((r >> 12) & 0xff) << 12)
968 | (((r >> 11) & 1) << 20)
969 | (((r >> 1) & 0x3ff) << 21)
970 | (((r >> 20) & 1) << 31);
971 o(0x6f | imm); // jal x0, imm == j imm
975 ST_FUNC int gjmp_cond(int op, int t)
977 int tmp;
978 int a = vtop->cmp_r & 0xff;
979 int b = (vtop->cmp_r >> 8) & 0xff;
980 switch (op) {
981 case TOK_ULT: op = 6; break;
982 case TOK_UGE: op = 7; break;
983 case TOK_ULE: op = 7; tmp = a; a = b; b = tmp; break;
984 case TOK_UGT: op = 6; tmp = a; a = b; b = tmp; break;
985 case TOK_LT: op = 4; break;
986 case TOK_GE: op = 5; break;
987 case TOK_LE: op = 5; tmp = a; a = b; b = tmp; break;
988 case TOK_GT: op = 4; tmp = a; a = b; b = tmp; break;
989 case TOK_NE: op = 1; break;
990 case TOK_EQ: op = 0; break;
992 o(0x63 | (op ^ 1) << 12 | a << 15 | b << 20 | 8 << 7); // bOP a,b,+4
993 return gjmp(t);
996 ST_FUNC int gjmp_append(int n, int t)
998 void *p;
999 /* insert jump list n into t */
1000 if (n) {
1001 uint32_t n1 = n, n2;
1002 while ((n2 = read32le(p = cur_text_section->data + n1)))
1003 n1 = n2;
1004 write32le(p, t);
1005 t = n;
1007 return t;
1010 static void gen_opil(int op, int ll)
1012 int a, b, d;
1013 int func3 = 0;
1014 ll = ll ? 0 : 8;
1015 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
1016 int fc = vtop->c.i;
1017 if (fc == vtop->c.i && !(((unsigned)fc + (1 << 11)) >> 12)) {
1018 int cll = 0;
1019 int m = ll ? 31 : 63;
1020 vswap();
1021 gv(RC_INT);
1022 a = ireg(vtop[0].r);
1023 --vtop;
1024 d = get_reg(RC_INT);
1025 ++vtop;
1026 vswap();
1027 switch (op) {
1028 case '-':
1029 if (fc <= -(1 << 11))
1030 break;
1031 fc = -fc;
1032 case '+':
1033 func3 = 0; // addi d, a, fc
1034 cll = ll;
1035 do_cop:
1036 EI(0x13 | cll, func3, ireg(d), a, fc);
1037 --vtop;
1038 if (op >= TOK_ULT && op <= TOK_GT) {
1039 vset_VT_CMP(TOK_NE);
1040 vtop->cmp_r = ireg(d) | 0 << 8;
1041 } else
1042 vtop[0].r = d;
1043 return;
1044 case TOK_LE:
1045 if (fc >= (1 << 11) - 1)
1046 break;
1047 ++fc;
1048 case TOK_LT: func3 = 2; goto do_cop; // slti d, a, fc
1049 case TOK_ULE:
1050 if (fc >= (1 << 11) - 1 || fc == -1)
1051 break;
1052 ++fc;
1053 case TOK_ULT: func3 = 3; goto do_cop; // sltiu d, a, fc
1054 case '^': func3 = 4; goto do_cop; // xori d, a, fc
1055 case '|': func3 = 6; goto do_cop; // ori d, a, fc
1056 case '&': func3 = 7; goto do_cop; // andi d, a, fc
1057 case TOK_SHL: func3 = 1; cll = ll; fc &= m; goto do_cop; // slli d, a, fc
1058 case TOK_SHR: func3 = 5; cll = ll; fc &= m; goto do_cop; // srli d, a, fc
1059 case TOK_SAR: func3 = 5; cll = ll; fc = 1024 | (fc & m); goto do_cop;
1061 case TOK_UGE: /* -> TOK_ULT */
1062 case TOK_UGT: /* -> TOK_ULE */
1063 case TOK_GE: /* -> TOK_LT */
1064 case TOK_GT: /* -> TOK_LE */
1065 gen_opil(op - 1, !ll);
1066 vtop->cmp_op ^= 1;
1067 return;
1069 case TOK_NE:
1070 case TOK_EQ:
1071 if (fc)
1072 gen_opil('-', !ll), a = ireg(vtop++->r);
1073 --vtop;
1074 vset_VT_CMP(op);
1075 vtop->cmp_r = a | 0 << 8;
1076 return;
1080 gv2(RC_INT, RC_INT);
1081 a = ireg(vtop[-1].r);
1082 b = ireg(vtop[0].r);
1083 vtop -= 2;
1084 d = get_reg(RC_INT);
1085 vtop++;
1086 vtop[0].r = d;
1087 d = ireg(d);
1088 switch (op) {
1089 default:
1090 if (op >= TOK_ULT && op <= TOK_GT) {
1091 vset_VT_CMP(op);
1092 vtop->cmp_r = a | b << 8;
1093 break;
1095 tcc_error("implement me: %s(%s)", __FUNCTION__, get_tok_str(op, NULL));
1096 break;
1098 case '+':
1099 ER(0x33 | ll, 0, d, a, b, 0); // add d, a, b
1100 break;
1101 case '-':
1102 ER(0x33 | ll, 0, d, a, b, 0x20); // sub d, a, b
1103 break;
1104 case TOK_SAR:
1105 ER(0x33 | ll | ll, 5, d, a, b, 0x20); // sra d, a, b
1106 break;
1107 case TOK_SHR:
1108 ER(0x33 | ll | ll, 5, d, a, b, 0); // srl d, a, b
1109 break;
1110 case TOK_SHL:
1111 ER(0x33 | ll, 1, d, a, b, 0); // sll d, a, b
1112 break;
1113 case '*':
1114 ER(0x33 | ll, 0, d, a, b, 1); // mul d, a, b
1115 break;
1116 case '/':
1117 ER(0x33 | ll, 4, d, a, b, 1); // div d, a, b
1118 break;
1119 case '&':
1120 ER(0x33, 7, d, a, b, 0); // and d, a, b
1121 break;
1122 case '^':
1123 ER(0x33, 4, d, a, b, 0); // xor d, a, b
1124 break;
1125 case '|':
1126 ER(0x33, 6, d, a, b, 0); // or d, a, b
1127 break;
1128 case '%':
1129 ER(ll ? 0x3b: 0x33, 6, d, a, b, 1); // rem d, a, b
1130 break;
1131 case TOK_UMOD:
1132 ER(0x33 | ll, 7, d, a, b, 1); // remu d, a, b
1133 break;
1134 case TOK_PDIV:
1135 case TOK_UDIV:
1136 ER(0x33 | ll, 5, d, a, b, 1); // divu d, a, b
1137 break;
1141 ST_FUNC void gen_opi(int op)
1143 gen_opil(op, 0);
1146 ST_FUNC void gen_opl(int op)
1148 gen_opil(op, 1);
1151 ST_FUNC void gen_opf(int op)
1153 int rs1, rs2, rd, dbl, invert;
1154 if (vtop[0].type.t == VT_LDOUBLE) {
1155 CType type = vtop[0].type;
1156 int func = 0;
1157 int cond = -1;
1158 switch (op) {
1159 case '*': func = TOK___multf3; break;
1160 case '+': func = TOK___addtf3; break;
1161 case '-': func = TOK___subtf3; break;
1162 case '/': func = TOK___divtf3; break;
1163 case TOK_EQ: func = TOK___eqtf2; cond = 1; break;
1164 case TOK_NE: func = TOK___netf2; cond = 0; break;
1165 case TOK_LT: func = TOK___lttf2; cond = 10; break;
1166 case TOK_GE: func = TOK___getf2; cond = 11; break;
1167 case TOK_LE: func = TOK___letf2; cond = 12; break;
1168 case TOK_GT: func = TOK___gttf2; cond = 13; break;
1169 default: assert(0); break;
1171 vpush_helper_func(func);
1172 vrott(3);
1173 gfunc_call(2);
1174 vpushi(0);
1175 vtop->r = REG_IRET;
1176 vtop->r2 = cond < 0 ? TREG_R(1) : VT_CONST;
1177 if (cond < 0)
1178 vtop->type = type;
1179 else {
1180 vpushi(0);
1181 gen_opil(op, 1);
1183 return;
1186 gv2(RC_FLOAT, RC_FLOAT);
1187 assert(vtop->type.t == VT_DOUBLE || vtop->type.t == VT_FLOAT);
1188 dbl = vtop->type.t == VT_DOUBLE;
1189 rs1 = freg(vtop[-1].r);
1190 rs2 = freg(vtop->r);
1191 vtop--;
1192 invert = 0;
1193 switch(op) {
1194 default:
1195 assert(0);
1196 case '+':
1197 op = 0; // fadd
1198 arithop:
1199 rd = get_reg(RC_FLOAT);
1200 vtop->r = rd;
1201 rd = freg(rd);
1202 ER(0x53, 7, rd, rs1, rs2, dbl | (op << 2)); // fop.[sd] RD, RS1, RS2 (dyn rm)
1203 break;
1204 case '-':
1205 op = 1; // fsub
1206 goto arithop;
1207 case '*':
1208 op = 2; // fmul
1209 goto arithop;
1210 case '/':
1211 op = 3; // fdiv
1212 goto arithop;
1213 case TOK_EQ:
1214 op = 2; // EQ
1215 cmpop:
1216 rd = get_reg(RC_INT);
1217 vtop->r = rd;
1218 rd = ireg(rd);
1219 ER(0x53, op, rd, rs1, rs2, dbl | 0x50); // fcmp.[sd] RD, RS1, RS2 (op == eq/lt/le)
1220 if (invert)
1221 EI(0x13, 4, rd, rd, 1); // xori RD, 1
1222 break;
1223 case TOK_NE:
1224 invert = 1;
1225 op = 2; // EQ
1226 goto cmpop;
1227 case TOK_LT:
1228 op = 1; // LT
1229 goto cmpop;
1230 case TOK_LE:
1231 op = 0; // LE
1232 goto cmpop;
1233 case TOK_GT:
1234 op = 1; // LT
1235 rd = rs1, rs1 = rs2, rs2 = rd;
1236 goto cmpop;
1237 case TOK_GE:
1238 op = 0; // LE
1239 rd = rs1, rs1 = rs2, rs2 = rd;
1240 goto cmpop;
1244 ST_FUNC void gen_cvt_sxtw(void)
1246 /* XXX on risc-v the registers are usually sign-extended already.
1247 Let's try to not do anything here. */
1250 ST_FUNC void gen_cvt_itof(int t)
1252 int rr = ireg(gv(RC_INT)), dr;
1253 int u = vtop->type.t & VT_UNSIGNED;
1254 int l = (vtop->type.t & VT_BTYPE) == VT_LLONG;
1255 if (t == VT_LDOUBLE) {
1256 int func = l ?
1257 (u ? TOK___floatunditf : TOK___floatditf) :
1258 (u ? TOK___floatunsitf : TOK___floatsitf);
1259 vpush_helper_func(func);
1260 vrott(2);
1261 gfunc_call(1);
1262 vpushi(0);
1263 vtop->type.t = t;
1264 vtop->r = REG_IRET;
1265 vtop->r2 = TREG_R(1);
1266 } else {
1267 vtop--;
1268 dr = get_reg(RC_FLOAT);
1269 vtop++;
1270 vtop->r = dr;
1271 dr = freg(dr);
1272 EIu(0x53, 7, dr, rr, ((0x68 | (t == VT_DOUBLE ? 1 : 0)) << 5) | (u ? 1 : 0) | (l ? 2 : 0)); // fcvt.[sd].[wl][u]
1276 ST_FUNC void gen_cvt_ftoi(int t)
1278 int ft = vtop->type.t & VT_BTYPE;
1279 int l = (t & VT_BTYPE) == VT_LLONG;
1280 int u = t & VT_UNSIGNED;
1281 if (ft == VT_LDOUBLE) {
1282 int func = l ?
1283 (u ? TOK___fixunstfdi : TOK___fixtfdi) :
1284 (u ? TOK___fixunstfsi : TOK___fixtfsi);
1285 vpush_helper_func(func);
1286 vrott(2);
1287 gfunc_call(1);
1288 vpushi(0);
1289 vtop->type.t = t;
1290 vtop->r = REG_IRET;
1291 } else {
1292 int rr = freg(gv(RC_FLOAT)), dr;
1293 vtop--;
1294 dr = get_reg(RC_INT);
1295 vtop++;
1296 vtop->r = dr;
1297 dr = ireg(dr);
1298 EIu(0x53, 1, dr, rr, ((0x60 | (ft == VT_DOUBLE ? 1 : 0)) << 5) | (u ? 1 : 0) | (l ? 2 : 0)); // fcvt.[wl][u].[sd] rtz
1302 ST_FUNC void gen_cvt_ftof(int dt)
1304 int st = vtop->type.t & VT_BTYPE, rs, rd;
1305 dt &= VT_BTYPE;
1306 if (st == dt)
1307 return;
1308 if (dt == VT_LDOUBLE || st == VT_LDOUBLE) {
1309 int func = (dt == VT_LDOUBLE) ?
1310 (st == VT_FLOAT ? TOK___extendsftf2 : TOK___extenddftf2) :
1311 (dt == VT_FLOAT ? TOK___trunctfsf2 : TOK___trunctfdf2);
1312 /* We can't use gfunc_call, as func_old_type works like vararg
1313 functions, and on riscv unnamed float args are passed like
1314 integers. But we really need them in the float argument registers
1315 for extendsftf2/extenddftf2. So, do it explicitely. */
1316 save_regs(1);
1317 if (dt == VT_LDOUBLE)
1318 gv(RC_F(0));
1319 else {
1320 gv(RC_R(0));
1321 assert(vtop->r2 < 7);
1322 if (vtop->r2 != 1 + vtop->r) {
1323 EI(0x13, 0, ireg(vtop->r) + 1, ireg(vtop->r2), 0); // mv Ra+1, RR2
1324 vtop->r2 = 1 + vtop->r;
1327 vpush_helper_func(func);
1328 gcall_or_jmp(1);
1329 vtop -= 2;
1330 vpushi(0);
1331 vtop->type.t = dt;
1332 if (dt == VT_LDOUBLE)
1333 vtop->r = REG_IRET, vtop->r2 = REG_IRET+1;
1334 else
1335 vtop->r = REG_FRET;
1336 } else {
1337 assert (dt == VT_FLOAT || dt == VT_DOUBLE);
1338 assert (st == VT_FLOAT || st == VT_DOUBLE);
1339 rs = gv(RC_FLOAT);
1340 rd = get_reg(RC_FLOAT);
1341 if (dt == VT_DOUBLE)
1342 EI(0x53, 0, freg(rd), freg(rs), 0x21 << 5); // fcvt.d.s RD, RS (no rm)
1343 else
1344 EI(0x53, 7, freg(rd), freg(rs), (0x20 << 5) | 1); // fcvt.s.d RD, RS (dyn rm)
1345 vtop->r = rd;
1349 /* increment tcov counter */
1350 ST_FUNC void gen_increment_tcov (SValue *sv)
1352 int r1, r2;
1353 Sym label = {0};
1354 label.type.t = VT_VOID | VT_STATIC;
1356 vpushv(sv);
1357 vtop->r = r1 = get_reg(RC_INT);
1358 r2 = get_reg(RC_INT);
1359 r1 = ireg(r1);
1360 r2 = ireg(r2);
1361 greloca(cur_text_section, sv->sym, ind, R_RISCV_PCREL_HI20, 0);
1362 put_extern_sym(&label, cur_text_section, ind, 0);
1363 o(0x17 | (r1 << 7)); // auipc RR, 0 %pcrel_hi(sym)
1364 greloca(cur_text_section, &label, ind, R_RISCV_PCREL_LO12_I, 0);
1365 EI(0x03, 3, r2, r1, 0); // ld r2, x[r1]
1366 EI(0x13, 0, r2, r2, 1); // addi r2, r2, #1
1367 greloca(cur_text_section, sv->sym, ind, R_RISCV_PCREL_HI20, 0);
1368 label.c = 0; /* force new local ELF symbol */
1369 put_extern_sym(&label, cur_text_section, ind, 0);
1370 o(0x17 | (r1 << 7)); // auipc RR, 0 %pcrel_hi(sym)
1371 greloca(cur_text_section, &label, ind, R_RISCV_PCREL_LO12_S, 0);
1372 ES(0x23, 3, r1, r2, 0); // sd r2, [r1]
1373 vpop();
1376 ST_FUNC void ggoto(void)
1378 gcall_or_jmp(0);
1379 vtop--;
1382 ST_FUNC void gen_vla_sp_save(int addr)
1384 ES(0x23, 3, 8, 2, addr); // sd sp, fc(s0)
1387 ST_FUNC void gen_vla_sp_restore(int addr)
1389 EI(0x03, 3, 2, 8, addr); // ld sp, fc(s0)
1392 ST_FUNC void gen_vla_alloc(CType *type, int align)
1394 int rr;
1395 #if defined(CONFIG_TCC_BCHECK)
1396 if (tcc_state->do_bounds_check)
1397 vpushv(vtop);
1398 #endif
1399 rr = ireg(gv(RC_INT));
1400 #if defined(CONFIG_TCC_BCHECK)
1401 if (tcc_state->do_bounds_check)
1402 EI(0x13, 0, rr, rr, 15+1); // addi RR, RR, 15+1
1403 else
1404 #endif
1405 EI(0x13, 0, rr, rr, 15); // addi RR, RR, 15
1406 EI(0x13, 7, rr, rr, -16); // andi, RR, RR, -16
1407 ER(0x33, 0, 2, 2, rr, 0x20); // sub sp, sp, rr
1408 vpop();
1409 #if defined(CONFIG_TCC_BCHECK)
1410 if (tcc_state->do_bounds_check) {
1411 vpushi(0);
1412 vtop->r = TREG_R(0);
1413 o(0x00010513); /* mv a0,sp */
1414 vswap();
1415 vpush_helper_func(TOK___bound_new_region);
1416 vrott(3);
1417 gfunc_call(2);
1418 func_bound_add_epilog = 1;
1420 #endif
1422 #endif