riscv: Add more ops and fixes
[tinycc.git] / riscv64-gen.c
blobbb80bd3ed7a6f90f7dbb808843fb82eba4256d6d
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 ST_DATA const int reg_classes[NB_REGS] = {
37 RC_INT | RC_R(0),
38 RC_INT | RC_R(1),
39 RC_INT | RC_R(2),
40 RC_INT | RC_R(3),
41 RC_INT | RC_R(4),
42 RC_INT | RC_R(5),
43 RC_INT | RC_R(6),
44 RC_INT | RC_R(7),
45 RC_FLOAT | RC_F(0),
46 RC_FLOAT | RC_F(1),
47 RC_FLOAT | RC_F(2),
48 RC_FLOAT | RC_F(3),
49 RC_FLOAT | RC_F(4),
50 RC_FLOAT | RC_F(5),
51 RC_FLOAT | RC_F(6),
52 RC_FLOAT | RC_F(7)
55 static int ireg(int r)
57 assert(r >= 0 && r < 8);
58 return r + 10; // tccrX --> aX == x(10+X)
61 static int is_ireg(int r)
63 return r < 8;
66 static int is_freg(int r)
68 return r >= 8 && r < 16;
71 ST_FUNC void o(unsigned int c)
73 int ind1 = ind + 4;
74 if (nocode_wanted)
75 return;
76 if (ind1 > cur_text_section->data_allocated)
77 section_realloc(cur_text_section, ind1);
78 write32le(cur_text_section->data + ind, c);
79 ind = ind1;
82 static void EI(uint32_t opcode, uint32_t func3,
83 uint32_t rd, uint32_t rs1, uint32_t imm)
85 assert(! ((imm + (1 << 11)) >> 12));
86 o(opcode | (func3 << 12) | (rd << 7) | (rs1 << 15) | (imm << 20));
89 static void ES(uint32_t opcode, uint32_t func3,
90 uint32_t rs1, uint32_t rs2, uint32_t imm)
92 assert(! ((imm + (1 << 11)) >> 12));
93 o(opcode | (func3 << 12) | ((imm & 0x1f) << 7) | (rs1 << 15)
94 | (rs2 << 20) | ((imm >> 5) << 25));
97 // Patch all branches in list pointed to by t to branch to a:
98 ST_FUNC void gsym_addr(int t_, int a_)
100 uint32_t t = t_;
101 uint32_t a = a_;
102 while (t) {
103 unsigned char *ptr = cur_text_section->data + t;
104 uint32_t next = read32le(ptr);
105 uint32_t r = a - t, imm;
106 if ((r + (1 << 21)) & ~((1U << 22) - 2))
107 tcc_error("out-of-range branch chain");
108 imm = (((r >> 12) & 0xff) << 12)
109 | (((r >> 11) & 1) << 20)
110 | (((r >> 1) & 0x3ff) << 21)
111 | (((r >> 20) & 1) << 31);
112 write32le(ptr, r == 4 ? 0x33 : 0x6f | imm); // nop || j imm
113 t = next;
117 ST_FUNC void load(int r, SValue *sv)
119 int fr = sv->r;
120 int v = fr & VT_VALMASK;
121 int rr = ireg(r);
122 int fc = sv->c.i;
123 if (fr & VT_LVAL) {
124 if (v == VT_LOCAL) {
125 int bt = sv->type.t & VT_BTYPE;
126 int align, size = type_size(&sv->type, &align);
127 int func3;
128 if (((unsigned)fc + (1 << 11)) >> 12)
129 tcc_error("unimp: load(large local ofs) (0x%x)", fc);
130 if (is_float(bt))
131 tcc_error("unimp: load-local(float)");
132 else if (bt == VT_FUNC)
133 size = PTR_SIZE;
134 func3 = size == 1 ? 0 : size == 2 ? 1 : size == 4 ? 2 : 3;
135 if (size < 8 && (sv->type.t & VT_UNSIGNED))
136 func3 |= 4;
137 EI(0x03, func3, rr, 8, fc); // l[bhwd][u] RR, fc(s0)
138 } else {
139 tcc_error("unimp: load(non-local lval)");
141 } else if (v == VT_CONST) {
142 int rb = 0;
143 if (fc != sv->c.i)
144 tcc_error("unimp: load(very large const)");
145 if (((unsigned)fc + (1 << 11)) >> 12)
146 tcc_error("unimp: load(large const) (0x%x)", fc);
147 if (fr & VT_SYM) {
148 static Sym label;
149 greloca(cur_text_section, vtop->sym, ind,
150 R_RISCV_PCREL_HI20, fc);
151 if (!label.v) {
152 label.v = tok_alloc(".L0 ", 4)->tok;
153 label.type.t = VT_VOID | VT_STATIC;
155 label.c = 0; /* force new local ELF symbol */
156 put_extern_sym(&label, cur_text_section, ind, 0);
157 o(0x17 | (rr << 7)); // auipc RR, 0 %call(func)
158 greloca(cur_text_section, &label, ind,
159 R_RISCV_PCREL_LO12_I, 0);
160 rb = rr;
162 if (is_float(sv->type.t))
163 tcc_error("unimp: load(float)");
164 EI(0x13, 0, rr, rb, fc); // addi R, x0|R, FC
165 } else if (v < VT_CONST) {
166 /* reg-reg */
167 if (is_freg(r) && is_freg(v))
168 tcc_error("unimp load: float reg-reg move");
169 else if (is_ireg(r) && is_ireg(v))
170 EI(0x13, 0, rr, ireg(v), 0); // addi RR, V, 0 == mv RR, V
171 else
172 tcc_error("ICE: inter-unit reg-reg move");
173 } else if (v == VT_CMP) { // we rely on cmp_r to be the correct result
174 EI(0x13, 0, rr, vtop->cmp_r, 0); // mv RR, CMP_R
175 } else
176 tcc_error("unimp: load(non-const)");
179 ST_FUNC void store(int r, SValue *sv)
181 int fr = sv->r & VT_VALMASK;
182 int rr = ireg(r);
183 int fc = sv->c.i;
184 int ft = sv->type.t;
185 int bt = ft & VT_BTYPE;
186 int align, size = type_size(&sv->type, &align);
187 if (fr == VT_LOCAL) {
188 if (((unsigned)fc + (1 << 11)) >> 12)
189 tcc_error("unimp: store(large local off) (0x%x)", fc);
190 if (is_float(bt))
191 tcc_error("unimp: store(float)");
192 if (bt == VT_STRUCT)
193 tcc_error("unimp: store(struct)");
194 if (size > 8)
195 tcc_error("unimp: large sized store");
196 ES(0x23, size == 1 ? 0 : size == 2 ? 1 : size == 4 ? 2 : 3,
197 8, rr, fc); // s[bhwd] RR, fc(s0)
198 } else
199 tcc_error("implement me: %s(!local)", __FUNCTION__);
202 static void gcall(void)
204 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST &&
205 ((vtop->r & VT_SYM) && vtop->c.i == (int)vtop->c.i)) {
206 /* constant symbolic case -> simple relocation */
207 greloca(cur_text_section, vtop->sym, ind,
208 R_RISCV_CALL_PLT, (int)vtop->c.i);
209 o(0x17 | (1 << 7)); // auipc ra, 0 %call(func)
210 o(0x80e7); // jalr ra, 0 %call(func)
211 } else {
212 tcc_error("unimp: indirect call");
216 ST_FUNC void gfunc_call(int nb_args)
218 int i, align, size, aireg;
219 aireg = 0;
220 for (i = 0; i < nb_args; i++) {
221 size = type_size(&vtop[-i].type, &align);
222 if (size > 8 || ((vtop[-i].type.t & VT_BTYPE) == VT_STRUCT)
223 || is_float(vtop[-i].type.t))
224 tcc_error("unimp: call arg %d wrong type", nb_args - i);
225 if (aireg >= 8)
226 tcc_error("unimp: too many register args");
227 vrotb(i+1);
228 gv(RC_R(nb_args - 1 - aireg));
229 vrott(i+1);
230 aireg++;
232 vrotb(nb_args + 1);
233 gcall();
234 vtop -= nb_args + 1;
237 static int func_sub_sp_offset;
239 ST_FUNC void gfunc_prolog(CType *func_type)
241 int i, addr, align, size;
242 int param_addr = 0;
243 int aireg, afreg;
244 Sym *sym;
245 CType *type;
247 sym = func_type->ref;
248 func_vt = sym->type;
249 loc = -16; // for ra and s0
250 func_sub_sp_offset = ind;
251 ind += 4 * 4;
252 if (sym->f.func_type == FUNC_ELLIPSIS) {
253 tcc_error("unimp: vararg prologue");
256 aireg = afreg = 0;
257 addr = 0; // XXX not correct
258 /* if the function returns a structure, then add an
259 implicit pointer parameter */
260 size = type_size(&func_vt, &align);
261 if (size > 2 * XLEN) {
262 tcc_error("unimp: struct return");
263 func_vc = loc;
265 /* define parameters */
266 while ((sym = sym->next) != NULL) {
267 type = &sym->type;
268 size = type_size(type, &align);
269 if (size > 2 * XLEN) {
270 from_stack:
271 addr = (addr + align - 1) & -align;
272 param_addr = addr;
273 addr += size;
274 } else {
275 int regcount = 1;
276 if (size > XLEN)
277 regcount++;
278 if (regcount + (is_float(type->t) ? afreg : aireg) >= 8)
279 goto from_stack;
280 loc -= regcount * 8;
281 param_addr = loc;
282 for (i = 0; i < regcount; i++) {
283 if (is_float(type->t)) {
284 tcc_error("unimp: float args");
285 } else {
286 ES(0x23, 3, 8, 10 + aireg, loc + i*8); // sd aX, loc(s0) // XXX
287 aireg++;
291 sym_push(sym->v & ~SYM_FIELD, type,
292 VT_LOCAL | lvalue_type(type->t), param_addr);
296 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret,
297 int *ret_align, int *regsize)
299 /* generic code can only deal with structs of pow(2) sizes
300 (it always deals with whole registers), so go through our own
301 code. */
302 return 0;
305 ST_FUNC void gfunc_return(CType *func_type)
307 int align, size = type_size(func_type, &align);
308 if ((func_type->t & VT_BTYPE) == VT_STRUCT
309 || size > 2 * XLEN) {
310 tcc_error("unimp: struct or large return");
312 if (is_float(func_type->t))
313 gv(RC_FRET);
314 else
315 gv(RC_IRET);
316 vtop--;
319 ST_FUNC void gfunc_epilog(void)
321 int v, saved_ind;
323 v = (-loc + 15) & -16;
325 EI(0x03, 3, 1, 2, v - 8); // ld ra, v-8(sp)
326 EI(0x03, 3, 8, 2, v - 16); // ld s0, v-16(sp)
327 EI(0x13, 0, 2, 2, v); // addi sp, sp, v
328 EI(0x67, 0, 0, 1, 0); // jalr x0, 0(x1), aka ret
329 saved_ind = ind;
330 ind = func_sub_sp_offset;
331 EI(0x13, 0, 2, 2, -v); // addi sp, sp, -v
332 ES(0x23, 3, 2, 1, v - 8); // sd ra, v-8(sp)
333 ES(0x23, 3, 2, 8, v - 16); // sd s0, v-16(sp)
334 EI(0x13, 0, 8, 2, v); // addi s0, sp, v
335 ind = saved_ind;
338 ST_FUNC void gen_va_start(void)
340 tcc_error("implement me: %s", __FUNCTION__);
343 ST_FUNC void gen_va_arg(CType *t)
345 tcc_error("implement me: %s", __FUNCTION__);
348 ST_FUNC void gen_fill_nops(int bytes)
350 tcc_error("implement me: %s", __FUNCTION__);
351 if ((bytes & 3))
352 tcc_error("alignment of code section not multiple of 4");
355 // Generate forward branch to label:
356 ST_FUNC int gjmp(int t)
358 if (nocode_wanted)
359 return t;
360 o(t);
361 return ind - 4;
364 // Generate branch to known address:
365 ST_FUNC void gjmp_addr(int a)
367 uint32_t r = a - ind, imm;
368 if ((r + (1 << 21)) & ~((1U << 22) - 2))
369 tcc_error("out-of-range jump");
370 imm = (((r >> 12) & 0xff) << 12)
371 | (((r >> 11) & 1) << 20)
372 | (((r >> 1) & 0x3ff) << 21)
373 | (((r >> 20) & 1) << 31);
374 o(0x6f | imm); // jal x0, imm == j imm
377 ST_FUNC int gjmp_cond(int op, int t)
379 int inv = op & 1;
380 assert(op == TOK_EQ || op == TOK_NE);
381 assert(vtop->cmp_r >= 10 && vtop->cmp_r < 18);
382 o(0x63 | (!inv << 12) | (vtop->cmp_r << 15) | (8 << 7)); // bne/beq x0,r,+4
383 return gjmp(t);
386 ST_FUNC int gjmp_append(int n, int t)
388 void *p;
389 /* insert jump list n into t */
390 if (n) {
391 uint32_t n1 = n, n2;
392 while ((n2 = read32le(p = cur_text_section->data + n1)))
393 n1 = n2;
394 write32le(p, t);
395 t = n;
397 return t;
400 static void gen_opil(int op, int ll)
402 int a, b, d;
403 int inv = 0;
404 /* XXX We could special-case some constant args. */
405 gv2(RC_INT, RC_INT);
406 a = ireg(vtop[-1].r);
407 b = ireg(vtop[0].r);
408 vtop -= 2;
409 d = get_reg(RC_INT);
410 vtop++;
411 vtop[0].r = d;
412 d = ireg(d);
413 switch (op) {
414 case '%':
415 case '&':
416 case '*':
417 case '-':
418 case '/':
419 case '^':
420 case '|':
421 case TOK_SAR:
422 case TOK_SHL:
423 case TOK_SHR:
424 case TOK_UDIV:
425 case TOK_PDIV:
426 case TOK_UMOD:
427 default:
428 tcc_error("implement me: %s(%s)", __FUNCTION__, get_tok_str(op, NULL));
430 case '+':
431 o(0x33 | (d << 7) | (a << 15) | (b << 20)); // add d, a, b
432 break;
434 case TOK_ULT:
435 case TOK_UGE:
436 case TOK_ULE:
437 case TOK_UGT:
438 case TOK_LT:
439 case TOK_GE:
440 case TOK_LE:
441 case TOK_GT:
442 if (op & 1) { // remove [U]GE,GT
443 inv = 1;
444 op--;
446 if ((op & 7) == 6) { // [U]LE
447 int t = a; a = b; b = t;
448 inv ^= 1;
450 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (((op > TOK_UGT) ? 2 : 3) << 12)); // slt[u] d, a, b
451 if (inv)
452 EI(0x13, 4, d, d, 1); // xori d, d, 1
453 vset_VT_CMP(TOK_NE);
454 vtop->cmp_r = d;
455 break;
456 case TOK_NE:
457 case TOK_EQ:
458 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (0x20 << 25)); // sub d, a, b
459 if (op == TOK_NE)
460 o(0x33 | (3 << 12) | (d << 7) | (0 << 15) | (d << 20)); // sltu d, x0, d == snez d,d
461 else
462 EI(0x13, 3, d, d, 1); // sltiu d, d, 1 == seqz d,d
463 vset_VT_CMP(TOK_NE);
464 vtop->cmp_r = d;
465 break;
469 ST_FUNC void gen_opi(int op)
471 gen_opil(op, 0);
474 ST_FUNC void gen_opl(int op)
476 gen_opil(op, 1);
479 ST_FUNC void gen_opf(int op)
481 tcc_error("implement me: %s", __FUNCTION__);
484 ST_FUNC void gen_cvt_sxtw(void)
486 tcc_error("implement me: %s", __FUNCTION__);
489 ST_FUNC void gen_cvt_itof(int t)
491 tcc_error("implement me: %s", __FUNCTION__);
494 ST_FUNC void gen_cvt_ftoi(int t)
496 tcc_error("implement me: %s", __FUNCTION__);
499 ST_FUNC void gen_cvt_ftof(int t)
501 tcc_error("implement me: %s", __FUNCTION__);
504 ST_FUNC void ggoto(void)
506 tcc_error("implement me: %s", __FUNCTION__);
509 ST_FUNC void gen_vla_sp_save(int addr)
511 tcc_error("implement me: %s", __FUNCTION__);
514 ST_FUNC void gen_vla_sp_restore(int addr)
516 tcc_error("implement me: %s", __FUNCTION__);
519 ST_FUNC void gen_vla_alloc(CType *type, int align)
521 tcc_error("implement me: %s", __FUNCTION__);
523 #endif