riscv: float ops
[tinycc.git] / riscv64-gen.c
blobc75b45160c14f5ef2a671d718c0ce339802b6af1
1 #ifdef TARGET_DEFS_ONLY
3 // Number of registers available to allocator:
4 #define NB_REGS 16 // x10-x17 aka a0-a7, f10-f17 aka fa0-fa7
6 #define TREG_R(x) (x) // x = 0..7
7 #define TREG_F(x) (x + 8) // x = 0..7
9 // Register classes sorted from more general to more precise:
10 #define RC_INT (1 << 0)
11 #define RC_FLOAT (1 << 1)
12 #define RC_R(x) (1 << (2 + (x))) // x = 0..7
13 #define RC_F(x) (1 << (10 + (x))) // x = 0..7
15 #define RC_IRET (RC_R(0)) // int return register class
16 #define RC_FRET (RC_F(0)) // float return register class
18 #define REG_IRET (TREG_R(0)) // int return register number
19 #define REG_FRET (TREG_F(0)) // float return register number
21 #define PTR_SIZE 8
23 #define LDOUBLE_SIZE 16
24 #define LDOUBLE_ALIGN 16
26 #define MAX_ALIGN 16
28 #define CHAR_IS_UNSIGNED
30 #else
31 #include "tcc.h"
32 #include <assert.h>
34 #define XLEN 8
36 #define TREG_RA 17
37 #define TREG_SP 18
39 ST_DATA const int reg_classes[NB_REGS] = {
40 RC_INT | RC_R(0),
41 RC_INT | RC_R(1),
42 RC_INT | RC_R(2),
43 RC_INT | RC_R(3),
44 RC_INT | RC_R(4),
45 RC_INT | RC_R(5),
46 RC_INT | RC_R(6),
47 RC_INT | RC_R(7),
48 RC_FLOAT | RC_F(0),
49 RC_FLOAT | RC_F(1),
50 RC_FLOAT | RC_F(2),
51 RC_FLOAT | RC_F(3),
52 RC_FLOAT | RC_F(4),
53 RC_FLOAT | RC_F(5),
54 RC_FLOAT | RC_F(6),
55 RC_FLOAT | RC_F(7)
58 static int ireg(int r)
60 if (r == TREG_RA)
61 return 1; // ra
62 if (r == TREG_SP)
63 return 2; // sp
64 assert(r >= 0 && r < 8);
65 return r + 10; // tccrX --> aX == x(10+X)
68 static int is_ireg(int r)
70 return r < 8 || r == TREG_RA || r == TREG_SP;
73 static int freg(int r)
75 assert(r >= 8 && r < 16);
76 return r - 8 + 10; // tccfX --> faX == f(10+X)
79 static int is_freg(int r)
81 return r >= 8 && r < 16;
84 ST_FUNC void o(unsigned int c)
86 int ind1 = ind + 4;
87 if (nocode_wanted)
88 return;
89 if (ind1 > cur_text_section->data_allocated)
90 section_realloc(cur_text_section, ind1);
91 write32le(cur_text_section->data + ind, c);
92 ind = ind1;
95 static void EI(uint32_t opcode, uint32_t func3,
96 uint32_t rd, uint32_t rs1, uint32_t imm)
98 assert(! ((imm + (1 << 11)) >> 12));
99 o(opcode | (func3 << 12) | (rd << 7) | (rs1 << 15) | (imm << 20));
102 static void ES(uint32_t opcode, uint32_t func3,
103 uint32_t rs1, uint32_t rs2, uint32_t imm)
105 assert(! ((imm + (1 << 11)) >> 12));
106 o(opcode | (func3 << 12) | ((imm & 0x1f) << 7) | (rs1 << 15)
107 | (rs2 << 20) | ((imm >> 5) << 25));
110 // Patch all branches in list pointed to by t to branch to a:
111 ST_FUNC void gsym_addr(int t_, int a_)
113 uint32_t t = t_;
114 uint32_t a = a_;
115 while (t) {
116 unsigned char *ptr = cur_text_section->data + t;
117 uint32_t next = read32le(ptr);
118 uint32_t r = a - t, imm;
119 if ((r + (1 << 21)) & ~((1U << 22) - 2))
120 tcc_error("out-of-range branch chain");
121 imm = (((r >> 12) & 0xff) << 12)
122 | (((r >> 11) & 1) << 20)
123 | (((r >> 1) & 0x3ff) << 21)
124 | (((r >> 20) & 1) << 31);
125 write32le(ptr, r == 4 ? 0x33 : 0x6f | imm); // nop || j imm
126 t = next;
130 ST_FUNC void load(int r, SValue *sv)
132 int fr = sv->r;
133 int v = fr & VT_VALMASK;
134 int rr = is_ireg(r) ? ireg(r) : freg(r);
135 int fc = sv->c.i;
136 int bt = sv->type.t & VT_BTYPE;
137 int align, size = type_size(&sv->type, &align);
138 if (fr & VT_LVAL) {
139 int func3, opcode = 0x03;
140 if (is_freg(r)) {
141 assert(bt == VT_DOUBLE || bt == VT_FLOAT);
142 opcode = 0x07;
143 func3 = bt == VT_DOUBLE ? 3 : 2;
144 } else {
145 assert(is_ireg(r));
146 if (bt == VT_FUNC)
147 size = PTR_SIZE;
148 func3 = size == 1 ? 0 : size == 2 ? 1 : size == 4 ? 2 : 3;
149 if (size < 8 && !is_float(sv->type.t) && (sv->type.t & VT_UNSIGNED))
150 func3 |= 4;
152 if (v == VT_LOCAL) {
153 if (((unsigned)fc + (1 << 11)) >> 12)
154 tcc_error("unimp: load(large local ofs) (0x%x)", fc);
155 EI(opcode, func3, rr, 8, fc); // l[bhwd][u]/fl[wd] RR, fc(s0)
156 } else if (v < VT_CONST) {
157 /*if (((unsigned)fc + (1 << 11)) >> 12)
158 tcc_error("unimp: load(large addend) (0x%x)", fc);*/
159 fc = 0; // XXX store ofs in LVAL(reg)
160 EI(opcode, func3, rr, ireg(v), fc); // l[bhwd][u] RR, 0(V)
161 } else if (v == VT_CONST && (fr & VT_SYM)) {
162 static Sym label;
163 int addend = 0, tempr;
164 if (1 || ((unsigned)fc + (1 << 11)) >> 12)
165 addend = fc, fc = 0;
167 greloca(cur_text_section, sv->sym, ind,
168 R_RISCV_PCREL_HI20, addend);
169 if (!label.v) {
170 label.v = tok_alloc(".L0 ", 4)->tok;
171 label.type.t = VT_VOID | VT_STATIC;
173 label.c = 0; /* force new local ELF symbol */
174 put_extern_sym(&label, cur_text_section, ind, 0);
175 tempr = is_ireg(r) ? rr : ireg(get_reg(RC_INT));
176 o(0x17 | (tempr << 7)); // auipc TR, 0 %pcrel_hi(sym)+addend
177 greloca(cur_text_section, &label, ind,
178 R_RISCV_PCREL_LO12_I, 0);
179 EI(opcode, func3, rr, tempr, fc); // l[bhwd][u] RR, fc(TR)
180 } else if (v == VT_LLOCAL) {
181 int tempr = rr;
182 if (((unsigned)fc + (1 << 11)) >> 12)
183 tcc_error("unimp: load(large local ofs) (0x%x)", fc);
184 if (!is_ireg(r))
185 tempr = ireg(get_reg(RC_INT));
186 EI(0x03, 3, tempr, 8, fc); // ld TEMPR, fc(s0)
187 EI(opcode, func3, rr, tempr, 0); // l[bhwd][u] RR, 0(TEMPR)
188 } else {
189 tcc_error("unimp: load(non-local lval)");
191 } else if (v == VT_CONST) {
192 int rb = 0;
193 assert(!is_float(sv->type.t) && is_ireg(r));
194 if (fc != sv->c.i)
195 tcc_error("unimp: load(very large const)");
196 if (fr & VT_SYM) {
197 static Sym label;
198 greloca(cur_text_section, sv->sym, ind,
199 R_RISCV_PCREL_HI20, fc);
200 if (!label.v) {
201 label.v = tok_alloc(".L0 ", 4)->tok;
202 label.type.t = VT_VOID | VT_STATIC;
204 label.c = 0; /* force new local ELF symbol */
205 put_extern_sym(&label, cur_text_section, ind, 0);
206 o(0x17 | (rr << 7)); // auipc RR, 0 %call(func)
207 greloca(cur_text_section, &label, ind,
208 R_RISCV_PCREL_LO12_I, 0);
209 rb = rr;
210 fc = 0;
212 if (is_float(sv->type.t))
213 tcc_error("unimp: load(float)");
214 if (((unsigned)fc + (1 << 11)) >> 12)
215 o(0x37 | (rr << 7) | ((0x800 + fc) & 0xfffff000)), rb = rr; //lui RR, upper(fc)
216 EI(0x13, 0, rr, rb, fc << 20 >> 20); // addi R, x0|R, FC
217 } else if (v == VT_LOCAL) {
218 assert(is_ireg(r));
219 if (((unsigned)fc + (1 << 11)) >> 12)
220 tcc_error("unimp: load(addr large local ofs) (0x%x)", fc);
221 EI(0x13, 0, rr, 8, fc); // addi R, s0, FC
222 } else if (v < VT_CONST) {
223 /* reg-reg */
224 //assert(!fc); XXX support offseted regs
225 if (is_freg(r) && is_freg(v))
226 o(0x53 | (rr << 7) | (freg(v) << 15) | (freg(v) << 20) | ((bt == VT_DOUBLE ? 0x11 : 0x10) << 25)); //fsgnj.[sd] RR, V, V == fmv.[sd] RR, V
227 else if (is_ireg(r) && is_ireg(v))
228 EI(0x13, 0, rr, ireg(v), 0); // addi RR, V, 0 == mv RR, V
229 else {
230 int func7 = is_ireg(r) ? 0x70 : 0x78;
231 if (size == 8)
232 func7 |= 1;
233 assert(size == 4 || size == 8);
234 o(0x53 | (rr << 7) | ((is_freg(v) ? freg(v) : ireg(v)) << 15)
235 | (func7 << 25)); // fmv.{w.x, x.w, d.x, x.d} RR, VR
237 } else if (v == VT_CMP) { // we rely on cmp_r to be the correct result
238 EI(0x13, 0, rr, vtop->cmp_r, 0); // mv RR, CMP_R
239 } else if ((v & ~1) == VT_JMP) {
240 int t = v & 1;
241 assert(is_ireg(r));
242 EI(0x13, 0, rr, 0, t); // addi RR, x0, t
243 gjmp_addr(ind + 8);
244 gsym(fc);
245 EI(0x13, 0, rr, 0, t ^ 1); // addi RR, x0, !t
246 } else
247 tcc_error("unimp: load(non-const)");
250 ST_FUNC void store(int r, SValue *sv)
252 int fr = sv->r & VT_VALMASK;
253 int rr = is_ireg(r) ? ireg(r) : freg(r);
254 int fc = sv->c.i;
255 int ft = sv->type.t;
256 int bt = ft & VT_BTYPE;
257 int align, size = type_size(&sv->type, &align);
258 assert(!is_float(bt) || is_freg(r));
259 if (bt == VT_STRUCT)
260 tcc_error("unimp: store(struct)");
261 if (size > 8)
262 tcc_error("unimp: large sized store");
263 assert(sv->r & VT_LVAL);
264 if (fr == VT_LOCAL) {
265 if (((unsigned)fc + (1 << 11)) >> 12)
266 tcc_error("unimp: store(large local off) (0x%x)", fc);
267 if (is_freg(r))
268 ES(0x27, size == 4 ? 2 : 3, 8, rr, fc); // fs[wd] RR, fc(s0)
269 else
270 ES(0x23, size == 1 ? 0 : size == 2 ? 1 : size == 4 ? 2 : 3,
271 8, rr, fc); // s[bhwd] RR, fc(s0)
272 } else if (fr < VT_CONST) {
273 int ptrreg = ireg(fr);
274 /*if (((unsigned)fc + (1 << 11)) >> 12)
275 tcc_error("unimp: store(large addend) (0x%x)", fc);*/
276 fc = 0; // XXX support offsets regs
277 if (is_freg(r))
278 ES(0x27, size == 4 ? 2 : 3, ptrreg, rr, fc); // fs[wd] RR, fc(PTRREG)
279 else
280 ES(0x23, size == 1 ? 0 : size == 2 ? 1 : size == 4 ? 2 : 3,
281 ptrreg, rr, fc); // s[bhwd] RR, fc(PTRREG)
282 } else if (sv->r == (VT_CONST | VT_SYM | VT_LVAL)) {
283 static Sym label;
284 int tempr, addend = 0;
285 if (1 || ((unsigned)fc + (1 << 11)) >> 12)
286 addend = fc, fc = 0;
288 tempr = ireg(get_reg(RC_INT));
289 greloca(cur_text_section, sv->sym, ind,
290 R_RISCV_PCREL_HI20, addend);
291 if (!label.v) {
292 label.v = tok_alloc(".L0 ", 4)->tok;
293 label.type.t = VT_VOID | VT_STATIC;
295 label.c = 0; /* force new local ELF symbol */
296 put_extern_sym(&label, cur_text_section, ind, 0);
297 o(0x17 | (tempr << 7)); // auipc TEMPR, 0 %pcrel_hi(sym)+addend
298 greloca(cur_text_section, &label, ind,
299 R_RISCV_PCREL_LO12_S, 0);
300 if (is_freg(r))
301 ES(0x27, size == 4 ? 2 : 3, tempr, rr, fc); // fs[wd] RR, fc(TEMPR)
302 else
303 ES(0x23, size == 1 ? 0 : size == 2 ? 1 : size == 4 ? 2 : 3,
304 tempr, rr, fc); // s[bhwd] RR, fc(TEMPR)
305 } else
306 tcc_error("implement me: %s(!local)", __FUNCTION__);
309 static void gcall(void)
311 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST &&
312 ((vtop->r & VT_SYM) && vtop->c.i == (int)vtop->c.i)) {
313 /* constant symbolic case -> simple relocation */
314 greloca(cur_text_section, vtop->sym, ind,
315 R_RISCV_CALL_PLT, (int)vtop->c.i);
316 o(0x17 | (1 << 7)); // auipc ra, 0 %call(func)
317 o(0x80e7); // jalr ra, 0 %call(func)
318 } else if ((vtop->r & VT_VALMASK) < VT_CONST) {
319 int r = ireg(vtop->r & VT_VALMASK);
320 EI(0x67, 0, 1, r, 0); // jalr ra, 0(R)
321 } else {
322 int r = TREG_RA;
323 load(r, vtop);
324 r = ireg(r);
325 EI(0x67, 0, 1, r, 0); // jalr ra, 0(R)
329 ST_FUNC void gfunc_call(int nb_args)
331 int i, align, size, aireg, afreg;
332 int info[nb_args ? nb_args : 1];
333 int stack_adj = 0, ofs;
334 SValue *sv;
335 Sym *sa;
336 aireg = afreg = 0;
337 sa = vtop[-nb_args].type.ref->next;
338 for (i = 0; i < nb_args; i++) {
339 int *pareg;
340 sv = &vtop[1 + i - nb_args];
341 sv->type.t &= ~VT_ARRAY; // XXX this should be done in tccgen.c
342 size = type_size(&sv->type, &align);
343 if (size > 8 || ((sv->type.t & VT_BTYPE) == VT_STRUCT))
344 tcc_error("unimp: call arg %d wrong type", nb_args - i);
345 pareg = sa && is_float(sv->type.t) ? &afreg : &aireg;
346 if (*pareg < 8) {
347 info[i] = *pareg + (sa && is_float(sv->type.t) ? 8 : 0);
348 (*pareg)++;
349 } else {
350 info[i] = 16;
351 stack_adj += (size + align - 1) & -align;
353 if (sa)
354 sa = sa->next;
356 stack_adj = (stack_adj + 15) & -16;
357 if (stack_adj) {
358 EI(0x13, 0, 2, 2, -stack_adj); // addi sp, sp, -adj
359 for (i = ofs = 0; i < nb_args; i++) {
360 if (1 && info[i] >= 16) {
361 vrotb(nb_args - i);
362 size = type_size(&vtop->type, &align);
363 /* Once we support offseted regs we can do this:
364 vset(&vtop->type, TREG_SP | VT_LVAL, ofs);
365 to construct the lvalue for the outgoing stack slot,
366 until then we have to jump through hoops. */
367 vset(&char_pointer_type, TREG_SP, 0);
368 vpushi(ofs);
369 gen_op('+');
370 indir();
371 vtop->type = vtop[-1].type;
372 vswap();
373 vstore();
374 vrott(nb_args - i);
375 ofs += (size + align - 1) & -align;
376 ofs = (ofs + 7) & -8;
380 for (i = 0; i < nb_args; i++) {
381 int r = info[nb_args - 1 - i];
382 if (r < 16) {
383 vrotb(i+1);
384 gv(r < 8 ? RC_R(r) : RC_F(r - 8));
385 vrott(i+1);
388 vrotb(nb_args + 1);
389 gcall();
390 vtop -= nb_args + 1;
391 if (stack_adj)
392 EI(0x13, 0, 2, 2, stack_adj); // addi sp, sp, adj
395 static int func_sub_sp_offset;
397 ST_FUNC void gfunc_prolog(CType *func_type)
399 int i, addr, align, size;
400 int param_addr = 0;
401 int aireg, afreg;
402 Sym *sym;
403 CType *type;
405 sym = func_type->ref;
406 func_vt = sym->type;
407 loc = -16; // for ra and s0
408 func_sub_sp_offset = ind;
409 ind += 4 * 4;
410 if (sym->f.func_type == FUNC_ELLIPSIS) {
411 tcc_error("unimp: vararg prologue");
414 aireg = afreg = 0;
415 addr = 0; // XXX not correct
416 /* if the function returns a structure, then add an
417 implicit pointer parameter */
418 size = type_size(&func_vt, &align);
419 if (size > 2 * XLEN) {
420 tcc_error("unimp: struct return");
421 func_vc = loc;
423 /* define parameters */
424 while ((sym = sym->next) != NULL) {
425 type = &sym->type;
426 size = type_size(type, &align);
427 if (size > 2 * XLEN) {
428 from_stack:
429 addr = (addr + align - 1) & -align;
430 param_addr = addr;
431 addr += size;
432 } else {
433 int regcount = 1;
434 if (size > XLEN)
435 regcount++;
436 if (regcount + (is_float(type->t) ? afreg : aireg) >= 8)
437 goto from_stack;
438 loc -= regcount * 8;
439 param_addr = loc;
440 for (i = 0; i < regcount; i++) {
441 if (is_float(type->t)) {
442 tcc_error("unimp: float args");
443 } else {
444 ES(0x23, 3, 8, 10 + aireg, loc + i*8); // sd aX, loc(s0) // XXX
445 aireg++;
449 sym_push(sym->v & ~SYM_FIELD, type,
450 VT_LOCAL | lvalue_type(type->t), param_addr);
454 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret,
455 int *ret_align, int *regsize)
457 /* generic code can only deal with structs of pow(2) sizes
458 (it always deals with whole registers), so go through our own
459 code. */
460 return 0;
463 ST_FUNC void gfunc_return(CType *func_type)
465 int align, size = type_size(func_type, &align);
466 if ((func_type->t & VT_BTYPE) == VT_STRUCT
467 || size > 2 * XLEN) {
468 tcc_error("unimp: struct or large return");
470 if (is_float(func_type->t))
471 gv(RC_FRET);
472 else
473 gv(RC_IRET);
474 vtop--;
477 ST_FUNC void gfunc_epilog(void)
479 int v, saved_ind;
481 v = (-loc + 15) & -16;
483 EI(0x03, 3, 1, 2, v - 8); // ld ra, v-8(sp)
484 EI(0x03, 3, 8, 2, v - 16); // ld s0, v-16(sp)
485 EI(0x13, 0, 2, 2, v); // addi sp, sp, v
486 EI(0x67, 0, 0, 1, 0); // jalr x0, 0(x1), aka ret
487 saved_ind = ind;
488 ind = func_sub_sp_offset;
489 EI(0x13, 0, 2, 2, -v); // addi sp, sp, -v
490 ES(0x23, 3, 2, 1, v - 8); // sd ra, v-8(sp)
491 ES(0x23, 3, 2, 8, v - 16); // sd s0, v-16(sp)
492 EI(0x13, 0, 8, 2, v); // addi s0, sp, v
493 ind = saved_ind;
496 ST_FUNC void gen_va_start(void)
498 tcc_error("implement me: %s", __FUNCTION__);
501 ST_FUNC void gen_va_arg(CType *t)
503 tcc_error("implement me: %s", __FUNCTION__);
506 ST_FUNC void gen_fill_nops(int bytes)
508 tcc_error("implement me: %s", __FUNCTION__);
509 if ((bytes & 3))
510 tcc_error("alignment of code section not multiple of 4");
513 // Generate forward branch to label:
514 ST_FUNC int gjmp(int t)
516 if (nocode_wanted)
517 return t;
518 o(t);
519 return ind - 4;
522 // Generate branch to known address:
523 ST_FUNC void gjmp_addr(int a)
525 uint32_t r = a - ind, imm;
526 if ((r + (1 << 21)) & ~((1U << 22) - 2))
527 tcc_error("out-of-range jump");
528 imm = (((r >> 12) & 0xff) << 12)
529 | (((r >> 11) & 1) << 20)
530 | (((r >> 1) & 0x3ff) << 21)
531 | (((r >> 20) & 1) << 31);
532 o(0x6f | imm); // jal x0, imm == j imm
535 ST_FUNC int gjmp_cond(int op, int t)
537 int inv = op & 1;
538 assert(op == TOK_EQ || op == TOK_NE);
539 assert(vtop->cmp_r >= 10 && vtop->cmp_r < 18);
540 o(0x63 | (!inv << 12) | (vtop->cmp_r << 15) | (8 << 7)); // bne/beq x0,r,+4
541 return gjmp(t);
544 ST_FUNC int gjmp_append(int n, int t)
546 void *p;
547 /* insert jump list n into t */
548 if (n) {
549 uint32_t n1 = n, n2;
550 while ((n2 = read32le(p = cur_text_section->data + n1)))
551 n1 = n2;
552 write32le(p, t);
553 t = n;
555 return t;
558 static void gen_opil(int op, int ll)
560 int a, b, d;
561 int inv = 0;
562 int func3 = 0, func7 = 0;
563 /* XXX We could special-case some constant args. */
564 gv2(RC_INT, RC_INT);
565 a = ireg(vtop[-1].r);
566 b = ireg(vtop[0].r);
567 vtop -= 2;
568 d = get_reg(RC_INT);
569 vtop++;
570 vtop[0].r = d;
571 d = ireg(d);
572 switch (op) {
573 case '%':
574 case TOK_SAR:
575 case TOK_SHR:
576 case TOK_UDIV:
577 case TOK_PDIV:
578 case TOK_UMOD:
579 default:
580 tcc_error("implement me: %s(%s)", __FUNCTION__, get_tok_str(op, NULL));
582 case '+':
583 o(0x33 | (d << 7) | (a << 15) | (b << 20)); // add d, a, b
584 break;
585 case '-':
586 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (0x20 << 25)); //sub d, a, b
587 break;
588 case TOK_SHL:
589 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (1 << 12)); //sll d, a, b
590 break;
591 case '*':
592 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (0x01 << 25)); //mul d, a, b
593 break;
594 case '/':
595 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (0x01 << 25) | (4 << 12)); //div d, a, b
596 break;
597 case '&':
598 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (7 << 12)); // and d, a, b
599 break;
600 case '^':
601 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (4 << 12)); // xor d, a, b
602 break;
603 case '|':
604 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (6 << 12)); // or d, a, b
605 break;
607 case TOK_ULT:
608 case TOK_UGE:
609 case TOK_ULE:
610 case TOK_UGT:
611 case TOK_LT:
612 case TOK_GE:
613 case TOK_LE:
614 case TOK_GT:
615 if (op & 1) { // remove [U]GE,GT
616 inv = 1;
617 op--;
619 if ((op & 7) == 6) { // [U]LE
620 int t = a; a = b; b = t;
621 inv ^= 1;
623 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (((op > TOK_UGT) ? 2 : 3) << 12)); // slt[u] d, a, b
624 if (inv)
625 EI(0x13, 4, d, d, 1); // xori d, d, 1
626 vset_VT_CMP(TOK_NE);
627 vtop->cmp_r = d;
628 break;
629 case TOK_NE:
630 case TOK_EQ:
631 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (0x20 << 25)); // sub d, a, b
632 if (op == TOK_NE)
633 o(0x33 | (3 << 12) | (d << 7) | (0 << 15) | (d << 20)); // sltu d, x0, d == snez d,d
634 else
635 EI(0x13, 3, d, d, 1); // sltiu d, d, 1 == seqz d,d
636 vset_VT_CMP(TOK_NE);
637 vtop->cmp_r = d;
638 break;
642 ST_FUNC void gen_opi(int op)
644 gen_opil(op, 0);
647 ST_FUNC void gen_opl(int op)
649 gen_opil(op, 1);
652 ST_FUNC void gen_opf(int op)
654 int rs1, rs2, rd, dbl, invert;
655 gv2(RC_FLOAT, RC_FLOAT);
656 assert(vtop->type.t == VT_DOUBLE || vtop->type.t == VT_FLOAT);
657 dbl = vtop->type.t == VT_DOUBLE;
658 rs1 = freg(vtop[-1].r);
659 rs2 = freg(vtop->r);
660 vtop--;
661 invert = 0;
662 switch(op) {
663 default:
664 assert(0);
665 case '+':
666 op = 0; // fadd
667 arithop:
668 rd = get_reg(RC_FLOAT);
669 vtop->r = rd;
670 rd = freg(rd);
671 o(0x53 | (rd << 7) | (rs1 << 15) | (rs2 << 20) | (7 << 12) | (dbl << 25) | (op << 27)); // fop.[sd] RD, RS1, RS2 (dyn rm)
672 break;
673 case '-':
674 op = 1; // fsub
675 goto arithop;
676 case '*':
677 op = 2; // fmul
678 goto arithop;
679 case '/':
680 op = 3; // fdiv
681 goto arithop;
682 case TOK_EQ:
683 op = 2; // EQ
684 cmpop:
685 rd = get_reg(RC_INT);
686 vtop->r = rd;
687 rd = ireg(rd);
688 o(0x53 | (rd << 7) | (rs1 << 15) | (rs2 << 20) | (op << 12) | (dbl << 25) | (0x14 << 27)); // fcmp.[sd] RD, RS1, RS2 (op == eq/lt/le)
689 if (invert)
690 EI(0x13, 4, rd, rd, 1); // xori RD, 1
691 break;
692 case TOK_NE:
693 invert = 1;
694 op = 2; // EQ
695 goto cmpop;
696 case TOK_LT:
697 op = 1; // LT
698 goto cmpop;
699 case TOK_LE:
700 op = 0; // LE
701 goto cmpop;
702 case TOK_GT:
703 op = 1; // LT
704 rd = rs1, rs1 = rs2, rs2 = rd;
705 goto cmpop;
706 case TOK_GE:
707 op = 0; // LE
708 rd = rs1, rs1 = rs2, rs2 = rd;
709 goto cmpop;
713 ST_FUNC void gen_cvt_sxtw(void)
715 /* XXX on risc-v the registers are usually sign-extended already.
716 Let's try to not do anything here. */
719 ST_FUNC void gen_cvt_itof(int t)
721 tcc_error("implement me: %s", __FUNCTION__);
724 ST_FUNC void gen_cvt_ftoi(int t)
726 tcc_error("implement me: %s", __FUNCTION__);
729 ST_FUNC void gen_cvt_ftof(int dt)
731 int st = vtop->type.t & VT_BTYPE, rs, rd;
732 dt &= VT_BTYPE;
733 assert (dt == VT_FLOAT || dt == VT_DOUBLE);
734 assert (st == VT_FLOAT || st == VT_DOUBLE);
735 if (st == dt)
736 return;
737 rs = gv(RC_FLOAT);
738 rd = get_reg(RC_FLOAT);
739 if (dt == VT_DOUBLE)
740 EI(0x53, 7, freg(rd), freg(rs), 0x21 << 5); // fcvt.d.s RD, RS (dyn rm)
741 else
742 EI(0x53, 7, freg(rd), freg(rs), (0x20 << 5) | 1); // fcvt.s.d RD, RS
743 vtop->r = rd;
746 ST_FUNC void ggoto(void)
748 tcc_error("implement me: %s", __FUNCTION__);
751 ST_FUNC void gen_vla_sp_save(int addr)
753 tcc_error("implement me: %s", __FUNCTION__);
756 ST_FUNC void gen_vla_sp_restore(int addr)
758 tcc_error("implement me: %s", __FUNCTION__);
761 ST_FUNC void gen_vla_alloc(CType *type, int align)
763 tcc_error("implement me: %s", __FUNCTION__);
765 #endif