riscv: fix more sign/zero-extension problems
[tinycc.git] / riscv64-gen.c
blobac40f00bdb646ad93dd092031da00182fbe33b86
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
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),
57 1 << TREG_RA,
58 1 << TREG_SP
61 static int ireg(int r)
63 if (r == TREG_RA)
64 return 1; // ra
65 if (r == TREG_SP)
66 return 2; // sp
67 assert(r >= 0 && r < 8);
68 return r + 10; // tccrX --> aX == x(10+X)
71 static int is_ireg(int r)
73 return r < 8 || r == TREG_RA || r == TREG_SP;
76 static int freg(int r)
78 assert(r >= 8 && r < 16);
79 return r - 8 + 10; // tccfX --> faX == f(10+X)
82 static int is_freg(int r)
84 return r >= 8 && r < 16;
87 ST_FUNC void o(unsigned int c)
89 int ind1 = ind + 4;
90 if (nocode_wanted)
91 return;
92 if (ind1 > cur_text_section->data_allocated)
93 section_realloc(cur_text_section, ind1);
94 write32le(cur_text_section->data + ind, c);
95 ind = ind1;
98 static void EIu(uint32_t opcode, uint32_t func3,
99 uint32_t rd, uint32_t rs1, uint32_t imm)
101 o(opcode | (func3 << 12) | (rd << 7) | (rs1 << 15) | (imm << 20));
104 static void EI(uint32_t opcode, uint32_t func3,
105 uint32_t rd, uint32_t rs1, uint32_t imm)
107 assert(! ((imm + (1 << 11)) >> 12));
108 EIu(opcode, func3, rd, rs1, imm);
111 static void ES(uint32_t opcode, uint32_t func3,
112 uint32_t rs1, uint32_t rs2, uint32_t imm)
114 assert(! ((imm + (1 << 11)) >> 12));
115 o(opcode | (func3 << 12) | ((imm & 0x1f) << 7) | (rs1 << 15)
116 | (rs2 << 20) | ((imm >> 5) << 25));
119 // Patch all branches in list pointed to by t to branch to a:
120 ST_FUNC void gsym_addr(int t_, int a_)
122 uint32_t t = t_;
123 uint32_t a = a_;
124 while (t) {
125 unsigned char *ptr = cur_text_section->data + t;
126 uint32_t next = read32le(ptr);
127 uint32_t r = a - t, imm;
128 if ((r + (1 << 21)) & ~((1U << 22) - 2))
129 tcc_error("out-of-range branch chain");
130 imm = (((r >> 12) & 0xff) << 12)
131 | (((r >> 11) & 1) << 20)
132 | (((r >> 1) & 0x3ff) << 21)
133 | (((r >> 20) & 1) << 31);
134 write32le(ptr, r == 4 ? 0x33 : 0x6f | imm); // nop || j imm
135 t = next;
139 ST_FUNC void load(int r, SValue *sv)
141 int fr = sv->r;
142 int v = fr & VT_VALMASK;
143 int rr = is_ireg(r) ? ireg(r) : freg(r);
144 int fc = sv->c.i;
145 int bt = sv->type.t & VT_BTYPE;
146 int align, size = type_size(&sv->type, &align);
147 if (fr & VT_LVAL) {
148 int func3, opcode = 0x03, doload = 0;
149 if (is_freg(r)) {
150 assert(bt == VT_DOUBLE || bt == VT_FLOAT);
151 opcode = 0x07;
152 func3 = bt == VT_DOUBLE ? 3 : 2;
153 } else {
154 assert(is_ireg(r));
155 if (bt == VT_FUNC)
156 size = PTR_SIZE;
157 func3 = size == 1 ? 0 : size == 2 ? 1 : size == 4 ? 2 : 3;
158 if (size < 4 && !is_float(sv->type.t) && (sv->type.t & VT_UNSIGNED))
159 func3 |= 4;
161 if (v == VT_LOCAL) {
162 int br = 8; // s0
163 if (fc != sv->c.i)
164 tcc_error("unimp: load1(giant local ofs) (0x%llx)", (long long)sv->c.i);
165 if (((unsigned)fc + (1 << 11)) >> 12) {
166 br = is_ireg(r) ? rr : 5;
167 o(0x37 | (br << 7) | ((0x800 + fc) & 0xfffff000)); //lui BR, upper(fc)
168 o(0x33 | (br << 7) | (br << 15) | (8 << 20)); // add BR, BR, s0
169 fc = fc << 20 >> 20;
171 EI(opcode, func3, rr, br, fc); // l[bhwd][u]/fl[wd] RR, fc(BR)
172 } else if (v < VT_CONST) {
173 /*if (((unsigned)fc + (1 << 11)) >> 12)
174 tcc_error("unimp: load(large addend) (0x%x)", fc);*/
175 fc = 0; // XXX store ofs in LVAL(reg)
176 EI(opcode, func3, rr, ireg(v), fc); // l[bhwd][u] RR, 0(V)
177 } else if (v == VT_CONST && (fr & VT_SYM)) {
178 static Sym label;
179 int tempr;
180 if (sv->sym->type.t & VT_STATIC) { // XXX do this per linker relax
181 greloca(cur_text_section, sv->sym, ind,
182 R_RISCV_PCREL_HI20, sv->c.i);
183 fc = 0;
184 sv->c.i = 0;
185 } else {
186 if (((unsigned)fc + (1 << 11)) >> 12)
187 tcc_error("unimp: large addend for global address");
188 greloca(cur_text_section, sv->sym, ind,
189 R_RISCV_GOT_HI20, 0);
190 doload = 1;
192 if (!label.v) {
193 label.v = tok_alloc(".L0 ", 4)->tok;
194 label.type.t = VT_VOID | VT_STATIC;
196 label.c = 0; /* force new local ELF symbol */
197 put_extern_sym(&label, cur_text_section, ind, 0);
198 tempr = is_ireg(r) ? rr : 5;
199 o(0x17 | (tempr << 7)); // auipc TR, 0 %pcrel_hi(sym)+addend
200 greloca(cur_text_section, &label, ind,
201 R_RISCV_PCREL_LO12_I, 0);
202 if (doload) {
203 EI(0x03, 3, tempr, tempr, 0); // ld TR, 0(TR)
204 if (fc)
205 EI(0x13, 0, tempr, tempr, fc << 20 >> 20); // addi TR, TR, FC
206 fc = 0;
208 EI(opcode, func3, rr, tempr, fc); // l[bhwd][u] RR, fc(TR)
209 } else if (v == VT_LLOCAL) {
210 int br = 8, tempr = is_ireg(r) ? rr : 5;
211 if (fc != sv->c.i)
212 tcc_error("unimp: load2(giant local ofs) (0x%llx)", (long long)sv->c.i);
213 if (((unsigned)fc + (1 << 11)) >> 12) {
214 br = tempr;
215 o(0x37 | (br << 7) | ((0x800 + fc) & 0xfffff000)); //lui BR, upper(fc)
216 o(0x33 | (br << 7) | (br << 15) | (8 << 20)); // add BR, BR, s0
217 fc = fc << 20 >> 20;
219 EI(0x03, 3, tempr, br, fc); // ld TEMPR, fc(BR)
220 EI(opcode, func3, rr, tempr, 0); // l[bhwd][u] RR, 0(TEMPR)
221 } else {
222 tcc_error("unimp: load(non-local lval)");
224 } else if (v == VT_CONST) {
225 int rb = 0, do32bit = 8, doload = 0, zext = 0;
226 assert((!is_float(sv->type.t) && is_ireg(r)) || bt == VT_LDOUBLE);
227 if (fr & VT_SYM) {
228 static Sym label;
229 if (sv->sym->type.t & VT_STATIC) { // XXX do this per linker relax
230 greloca(cur_text_section, sv->sym, ind,
231 R_RISCV_PCREL_HI20, sv->c.i);
232 fc = 0;
233 sv->c.i = 0;
234 } else {
235 if (((unsigned)fc + (1 << 11)) >> 12)
236 tcc_error("unimp: large addend for global address");
237 greloca(cur_text_section, sv->sym, ind,
238 R_RISCV_GOT_HI20, 0);
239 doload = 1;
241 if (!label.v) {
242 label.v = tok_alloc(".L0 ", 4)->tok;
243 label.type.t = VT_VOID | VT_STATIC;
245 label.c = 0; /* force new local ELF symbol */
246 put_extern_sym(&label, cur_text_section, ind, 0);
247 o(0x17 | (rr << 7)); // auipc RR, 0 %call(func)
248 greloca(cur_text_section, &label, ind,
249 R_RISCV_PCREL_LO12_I, 0);
250 rb = rr;
251 do32bit = 0;
253 if (is_float(sv->type.t) && bt != VT_LDOUBLE)
254 tcc_error("unimp: load(float)");
255 if (fc != sv->c.i) {
256 int64_t si = sv->c.i;
257 uint32_t pi;
258 si >>= 32;
259 if (si != 0) {
260 pi = si;
261 if (fc < 0)
262 pi++;
263 o(0x37 | (rr << 7) | (((pi + 0x800) & 0xfffff000))); // lui RR, up(up(fc))
264 EI(0x13, 0, rr, rr, (int)pi << 20 >> 20); // addi RR, RR, lo(up(fc))
265 EI(0x13, 1, rr, rr, 12); // slli RR, RR, 12
266 EI(0x13, 0, rr, rr, (fc + (1 << 19)) >> 20); // addi RR, RR, up(lo(fc))
267 EI(0x13, 1, rr, rr, 12); // slli RR, RR, 12
268 fc = fc << 12 >> 12;
269 EI(0x13, 0, rr, rr, fc >> 8); // addi RR, RR, lo1(lo(fc))
270 EI(0x13, 1, rr, rr, 8); // slli RR, RR, 8
271 fc &= 0xff;
272 rb = rr;
273 do32bit = 0;
274 } else if (bt == VT_LLONG) {
275 /* A 32bit unsigned constant for a 64bit type.
276 lui always sign extends, so we need to do an explicit zext.*/
277 zext = 1;
280 if (((unsigned)fc + (1 << 11)) >> 12)
281 o(0x37 | (rr << 7) | ((0x800 + fc) & 0xfffff000)), rb = rr; //lui RR, upper(fc)
282 if (doload) {
283 EI(0x03, 3, rr, rr, 0); // ld RR, 0(RR)
284 if (fc)
285 EI(0x13 | do32bit, 0, rr, rr, fc << 20 >> 20); // addi[w] R, x0|R, FC
286 } else
287 EI(0x13 | do32bit, 0, rr, rb, fc << 20 >> 20); // addi[w] R, x0|R, FC
288 if (zext) {
289 EI(0x13, 1, rr, rr, 32); // slli RR, RR, 32
290 EI(0x13, 5, rr, rr, 32); // srli RR, RR, 32
292 } else if (v == VT_LOCAL) {
293 int br = 8; // s0
294 assert(is_ireg(r));
295 if (fc != sv->c.i)
296 tcc_error("unimp: load(addr giant local ofs) (0xll%x)", (long long)sv->c.i);
297 if (((unsigned)fc + (1 << 11)) >> 12) {
298 o(0x37 | (rr << 7) | ((0x800 + fc) & 0xfffff000)); //lui RR, upper(fc)
299 o(0x33 | (rr << 7) | (rr << 15) | (8 << 20)); // add RR, RR, s0
300 fc = fc << 20 >> 20;
301 br = rr;
303 EI(0x13, 0, rr, br, fc); // addi R, s0, FC
304 } else if (v < VT_CONST) {
305 /* reg-reg */
306 //assert(!fc); XXX support offseted regs
307 if (is_freg(r) && is_freg(v))
308 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
309 else if (is_ireg(r) && is_ireg(v))
310 EI(0x13, 0, rr, ireg(v), 0); // addi RR, V, 0 == mv RR, V
311 else {
312 int func7 = is_ireg(r) ? 0x70 : 0x78;
313 if (size == 8)
314 func7 |= 1;
315 assert(size == 4 || size == 8);
316 o(0x53 | (rr << 7) | ((is_freg(v) ? freg(v) : ireg(v)) << 15)
317 | (func7 << 25)); // fmv.{w.x, x.w, d.x, x.d} RR, VR
319 } else if (v == VT_CMP) { // we rely on cmp_r to be the correct result
320 EI(0x13, 0, rr, vtop->cmp_r, 0); // mv RR, CMP_R
321 } else if ((v & ~1) == VT_JMP) {
322 int t = v & 1;
323 assert(is_ireg(r));
324 EI(0x13, 0, rr, 0, t); // addi RR, x0, t
325 gjmp_addr(ind + 8);
326 gsym(fc);
327 EI(0x13, 0, rr, 0, t ^ 1); // addi RR, x0, !t
328 } else
329 tcc_error("unimp: load(non-const)");
332 ST_FUNC void store(int r, SValue *sv)
334 int fr = sv->r & VT_VALMASK;
335 int rr = is_ireg(r) ? ireg(r) : freg(r);
336 int fc = sv->c.i;
337 int ft = sv->type.t;
338 int bt = ft & VT_BTYPE;
339 int align, size = type_size(&sv->type, &align);
340 assert(!is_float(bt) || is_freg(r) || bt == VT_LDOUBLE);
341 /* long doubles are in two integer registers, but the load/store
342 primitives only deal with one, so do as if it's one reg. */
343 if (bt == VT_LDOUBLE)
344 size = align = 8;
345 if (bt == VT_STRUCT)
346 tcc_error("unimp: store(struct)");
347 if (size > 8)
348 tcc_error("unimp: large sized store");
349 assert(sv->r & VT_LVAL);
350 if (fr == VT_LOCAL) {
351 int br = 8; // s0
352 if (fc != sv->c.i)
353 tcc_error("unimp: store(giant local off) (0x%llx)", (long long)sv->c.i);
354 if (((unsigned)fc + (1 << 11)) >> 12) {
355 br = 5; // t0
356 o(0x37 | (br << 7) | ((0x800 + fc) & 0xfffff000)); //lui BR, upper(fc)
357 o(0x33 | (br << 7) | (br << 15) | (8 << 20)); // add BR, BR, s0
358 fc = fc << 20 >> 20;
360 if (is_freg(r))
361 ES(0x27, size == 4 ? 2 : 3, br, rr, fc); // fs[wd] RR, fc(base)
362 else
363 ES(0x23, size == 1 ? 0 : size == 2 ? 1 : size == 4 ? 2 : 3,
364 br, rr, fc); // s[bhwd] RR, fc(base)
365 } else if (fr < VT_CONST) {
366 int ptrreg = ireg(fr);
367 /*if (((unsigned)fc + (1 << 11)) >> 12)
368 tcc_error("unimp: store(large addend) (0x%x)", fc);*/
369 fc = 0; // XXX support offsets regs
370 if (is_freg(r))
371 ES(0x27, size == 4 ? 2 : 3, ptrreg, rr, fc); // fs[wd] RR, fc(PTRREG)
372 else
373 ES(0x23, size == 1 ? 0 : size == 2 ? 1 : size == 4 ? 2 : 3,
374 ptrreg, rr, fc); // s[bhwd] RR, fc(PTRREG)
375 } else if ((sv->r & ~VT_LVAL_TYPE) == (VT_CONST | VT_SYM | VT_LVAL)) {
376 static Sym label;
377 int tempr, doload = 0;
378 tempr = 5; // t0
379 if (sv->sym->type.t & VT_STATIC) { // XXX do this per linker relax
380 greloca(cur_text_section, sv->sym, ind,
381 R_RISCV_PCREL_HI20, sv->c.i);
382 fc = 0;
383 sv->c.i = 0;
384 } else {
385 if (((unsigned)fc + (1 << 11)) >> 12)
386 tcc_error("unimp: large addend for global address");
387 greloca(cur_text_section, sv->sym, ind,
388 R_RISCV_GOT_HI20, 0);
389 doload = 1;
391 if (!label.v) {
392 label.v = tok_alloc(".L0 ", 4)->tok;
393 label.type.t = VT_VOID | VT_STATIC;
395 label.c = 0; /* force new local ELF symbol */
396 put_extern_sym(&label, cur_text_section, ind, 0);
397 o(0x17 | (tempr << 7)); // auipc TEMPR, 0 %pcrel_hi(sym)+addend
398 greloca(cur_text_section, &label, ind,
399 doload ? R_RISCV_PCREL_LO12_I : R_RISCV_PCREL_LO12_S, 0);
400 if (doload) {
401 EI(0x03, 3, tempr, tempr, 0); // ld TR, 0(TR)
402 if (fc)
403 EI(0x13, 0, tempr, tempr, fc << 20 >> 20); // addi TR, TR, FC
404 fc = 0;
406 if (is_freg(r))
407 ES(0x27, size == 4 ? 2 : 3, tempr, rr, fc); // fs[wd] RR, fc(TEMPR)
408 else
409 ES(0x23, size == 1 ? 0 : size == 2 ? 1 : size == 4 ? 2 : 3,
410 tempr, rr, fc); // s[bhwd] RR, fc(TEMPR)
411 } else
412 tcc_error("implement me: %s(!local)", __FUNCTION__);
415 static void gcall_or_jmp(int docall)
417 int tr = docall ? 1 : 5; // ra or t0
418 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST &&
419 ((vtop->r & VT_SYM) && vtop->c.i == (int)vtop->c.i)) {
420 /* constant symbolic case -> simple relocation */
421 greloca(cur_text_section, vtop->sym, ind,
422 R_RISCV_CALL_PLT, (int)vtop->c.i);
423 o(0x17 | (tr << 7)); // auipc TR, 0 %call(func)
424 EI(0x67, 0, tr, tr, 0);// jalr TR, r(TR)
425 } else if (vtop->r < VT_CONST) {
426 int r = ireg(vtop->r);
427 EI(0x67, 0, tr, r, 0); // jalr TR, 0(R)
428 } else {
429 int r = TREG_RA;
430 load(r, vtop);
431 r = ireg(r);
432 EI(0x67, 0, tr, r, 0); // jalr TR, 0(R)
436 ST_FUNC void gfunc_call(int nb_args)
438 int i, align, size, aireg, afreg;
439 int info[nb_args ? nb_args : 1];
440 int stack_adj = 0, tempspace = 0, ofs, splitofs = 0;
441 int force_stack = 0;
442 SValue *sv;
443 Sym *sa;
444 aireg = afreg = 0;
445 sa = vtop[-nb_args].type.ref->next;
446 for (i = 0; i < nb_args; i++) {
447 int *pareg, nregs, infreg = 0, byref = 0, tempofs;
448 sv = &vtop[1 + i - nb_args];
449 sv->type.t &= ~VT_ARRAY; // XXX this should be done in tccgen.c
450 size = type_size(&sv->type, &align);
451 if (size > 16) {
452 if (align < XLEN)
453 align = XLEN;
454 tempspace = (tempspace + align - 1) & -align;
455 tempofs = tempspace;
456 tempspace += size;
457 size = align = 8;
458 byref = 1;
460 if (size > 8)
461 nregs = 2;
462 else
463 nregs = 1;
464 if ((sv->type.t & VT_BTYPE) == VT_LDOUBLE) {
465 infreg = 0;
466 } else
467 infreg = sa && is_float(sv->type.t);
468 if (!infreg && !sa && align == 2*XLEN && size <= 2*XLEN)
469 aireg = (aireg + 1) & ~1;
470 pareg = infreg ? &afreg : &aireg;
471 if ((*pareg < 8) && !force_stack) {
472 info[i] = *pareg + (infreg ? 8 : 0);
473 (*pareg)++;
474 if (nregs == 1)
476 else if (*pareg < 8)
477 (*pareg)++;
478 else {
479 info[i] |= 16;
480 stack_adj += 8;
482 } else {
483 info[i] = 32;
484 if (align < XLEN)
485 align = XLEN;
486 stack_adj += (size + align - 1) & -align;
487 if (!sa)
488 force_stack = 1;
490 if (byref)
491 info[i] |= 64 | (tempofs << 7);
492 if (sa)
493 sa = sa->next;
495 stack_adj = (stack_adj + 15) & -16;
496 tempspace = (tempspace + 15) & -16;
497 if (stack_adj + tempspace) {
498 EI(0x13, 0, 2, 2, -(stack_adj + tempspace)); // addi sp, sp, -adj
499 for (i = ofs = 0; i < nb_args; i++) {
500 if (info[i] >= 32) {
501 vrotb(nb_args - i);
502 size = type_size(&vtop->type, &align);
503 if (info[i] & 64) {
504 vset(&char_pointer_type, TREG_SP, 0);
505 vpushi(stack_adj + (info[i] >> 7));
506 gen_op('+');
507 vpushv(vtop); // this replaces the old argument
508 vrott(3);
509 indir();
510 vtop->type = vtop[-1].type;
511 vswap();
512 vstore();
513 vpop();
514 size = align = 8;
516 if (info[i] & 32) {
517 if (align < XLEN)
518 align = XLEN;
519 /* Once we support offseted regs we can do this:
520 vset(&vtop->type, TREG_SP | VT_LVAL, ofs);
521 to construct the lvalue for the outgoing stack slot,
522 until then we have to jump through hoops. */
523 vset(&char_pointer_type, TREG_SP, 0);
524 ofs = (ofs + align - 1) & -align;
525 vpushi(ofs);
526 gen_op('+');
527 indir();
528 vtop->type = vtop[-1].type;
529 vswap();
530 vstore();
531 vtop->r = vtop->r2 = VT_CONST; // this arg is done
532 ofs += size;
534 vrott(nb_args - i);
535 } else if (info[i] & 16) {
536 assert(!splitofs);
537 splitofs = ofs;
538 ofs += 8;
542 for (i = 0; i < nb_args; i++) {
543 int r = info[nb_args - 1 - i];
544 if (!(r & 32)) {
545 CType origtype;
546 r &= 15;
547 vrotb(i+1);
548 origtype = vtop->type;
549 size = type_size(&vtop->type, &align);
550 if (size > 8 && (vtop->type.t & VT_BTYPE) == VT_STRUCT)
551 vtop->type.t = VT_LDOUBLE; // force loading a pair of regs
552 gv(r < 8 ? RC_R(r) : RC_F(r - 8));
553 vtop->type = origtype;
554 if (size > 8) {
555 assert((vtop->type.t & VT_BTYPE) == VT_LDOUBLE
556 || (vtop->type.t & VT_BTYPE) == VT_STRUCT);
557 assert(vtop->r2 < VT_CONST);
558 if (info[nb_args - 1 - i] & 16) {
559 ES(0x23, 3, 2, ireg(vtop->r2), splitofs); // sd t0, ofs(sp)
560 } else if (vtop->r2 != 1 + vtop->r) {
561 assert(vtop->r < 7);
562 /* XXX we'd like to have 'gv' move directly into
563 the right class instead of us fixing it up. */
564 EI(0x13, 0, ireg(vtop->r) + 1, ireg(vtop->r2), 0); // mv Ra+1, RR2
565 vtop->r2 = 1 + vtop->r;
568 vrott(i+1);
571 vrotb(nb_args + 1);
572 gcall_or_jmp(1);
573 vtop -= nb_args + 1;
574 if (stack_adj + tempspace)
575 EI(0x13, 0, 2, 2, stack_adj + tempspace); // addi sp, sp, adj
578 static int func_sub_sp_offset, num_va_regs;
580 ST_FUNC void gfunc_prolog(CType *func_type)
582 int i, addr, align, size;
583 int param_addr = 0;
584 int aireg, afreg;
585 Sym *sym;
586 CType *type;
588 sym = func_type->ref;
589 func_vt = sym->type;
590 loc = -16; // for ra and s0
591 func_sub_sp_offset = ind;
592 ind += 5 * 4;
594 aireg = afreg = 0;
595 addr = 0; // XXX not correct
596 /* if the function returns by reference, then add an
597 implicit pointer parameter */
598 size = type_size(&func_vt, &align);
599 if (size > 2 * XLEN) {
600 loc -= 8;
601 func_vc = loc;
602 ES(0x23, 3, 8, 10 + aireg, loc); // sd a0, loc(s0)
603 aireg++;
605 /* define parameters */
606 while ((sym = sym->next) != NULL) {
607 int byref = 0;
608 type = &sym->type;
609 size = type_size(type, &align);
610 if (size > 2 * XLEN) {
611 type = &char_pointer_type;
612 size = align = byref = 8;
614 if (size > 2 * XLEN) {
615 from_stack:
616 if (align < XLEN)
617 align = XLEN;
618 addr = (addr + align - 1) & -align;
619 param_addr = addr;
620 addr += size;
621 } else {
622 int regcount = 1, *pareg = &aireg;
623 if (is_float(type->t) && (type->t & VT_BTYPE) != VT_LDOUBLE)
624 pareg = &afreg;
625 if (regcount + *pareg > 8)
626 goto from_stack;
627 if (size > XLEN)
628 regcount++;
629 loc -= regcount * 8; // XXX could reserve only 'size' bytes
630 param_addr = loc;
631 for (i = 0; i < regcount; i++) {
632 if (*pareg >= 8) {
633 assert(i == 1 && regcount == 2 && !(addr & 7));
634 EI(0x03, 3, 5, 8, addr); // ld t0, addr(s0)
635 addr += 8;
636 ES(0x23, 3, 8, 5, loc + i*8); // sd t0, loc(s0)
637 continue;
639 if (pareg == &afreg) {
640 assert(type->t == VT_FLOAT || type->t == VT_DOUBLE);
641 ES(0x27, size == 4 ? 2 : 3, 8, 10 + *pareg, loc + i*8); // fs[wd] FAi, loc(s0)
642 } else {
643 ES(0x23, 3, 8, 10 + *pareg, loc + i*8); // sd aX, loc(s0) // XXX
645 (*pareg)++;
648 sym_push(sym->v & ~SYM_FIELD, &sym->type,
649 (byref ? VT_LLOCAL : VT_LOCAL) | lvalue_type(sym->type.t),
650 param_addr);
652 num_va_regs = 0;
653 if (func_type->ref->f.func_type == FUNC_ELLIPSIS) {
654 for (; aireg < 8; aireg++) {
655 num_va_regs++;
656 ES(0x23, 3, 8, 10 + aireg, -8 + num_va_regs * 8); // sd aX, loc(s0)
661 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret,
662 int *ret_align, int *regsize)
664 /* generic code can only deal with structs of pow(2) sizes
665 (it always deals with whole registers), so go through our own
666 code. */
667 int align, size = type_size(vt, &align);
668 *ret_align = 1;
669 *regsize = 8;
670 if (size > 16)
671 return 0;
672 if (size > 8)
673 ret->t = VT_LLONG;
674 else if (size > 4)
675 ret->t = VT_LLONG;
676 else if (size > 2)
677 ret->t = VT_INT;
678 else if (size > 1)
679 ret->t = VT_SHORT;
680 else
681 ret->t = VT_BYTE;
682 return (size + 7) / 8;
685 ST_FUNC void gfunc_return(CType *func_type)
687 int align, size = type_size(func_type, &align), nregs;
688 CType type = *func_type;
689 if (size > 2 * XLEN) {
690 mk_pointer(&type);
691 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
692 indir();
693 vswap();
694 vstore();
695 vpop();
696 return;
698 nregs = (size + 7) / 8;
699 if (nregs == 2)
700 vtop->type.t = VT_LDOUBLE;
702 if (is_float(func_type->t) && (vtop->type.t & VT_BTYPE) != VT_LDOUBLE)
703 gv(RC_FRET);
704 else
705 gv(RC_IRET);
706 vtop--;
709 ST_FUNC void gfunc_epilog(void)
711 int v, saved_ind, d, large_ofs_ind;
713 loc = (loc - num_va_regs * 8);
714 d = v = (-loc + 15) & -16;
716 if (v >= (1 << 11)) {
717 d = 16;
718 o(0x37 | (5 << 7) | ((0x800 + (v-16)) & 0xfffff000)); //lui t0, upper(v)
719 EI(0x13, 0, 5, 5, (v-16) << 20 >> 20); // addi t0, t0, lo(v)
720 o(0x33 | (2 << 7) | (2 << 15) | (5 << 20)); //add sp, sp, t0
722 EI(0x03, 3, 1, 2, d - 8 - num_va_regs * 8); // ld ra, v-8(sp)
723 EI(0x03, 3, 8, 2, d - 16 - num_va_regs * 8); // ld s0, v-16(sp)
724 EI(0x13, 0, 2, 2, d); // addi sp, sp, v
725 EI(0x67, 0, 0, 1, 0); // jalr x0, 0(x1), aka ret
726 if (v >= (1 << 11)) {
727 large_ofs_ind = ind;
728 EI(0x13, 0, 8, 2, d - num_va_regs * 8); // addi s0, sp, d
729 o(0x37 | (5 << 7) | ((0x800 + (v-16)) & 0xfffff000)); //lui t0, upper(v)
730 EI(0x13, 0, 5, 5, (v-16) << 20 >> 20); // addi t0, t0, lo(v)
731 o(0x33 | (2 << 7) | (2 << 15) | (5 << 20) | (0x20 << 25)); //sub sp, sp, t0
732 gjmp_addr(func_sub_sp_offset + 5*4);
734 saved_ind = ind;
736 ind = func_sub_sp_offset;
737 EI(0x13, 0, 2, 2, -d); // addi sp, sp, -d
738 ES(0x23, 3, 2, 1, d - 8 - num_va_regs * 8); // sd ra, d-8(sp)
739 ES(0x23, 3, 2, 8, d - 16 - num_va_regs * 8); // sd s0, d-16(sp)
740 if (v < (1 << 11))
741 EI(0x13, 0, 8, 2, d - num_va_regs * 8); // addi s0, sp, d
742 else
743 gjmp_addr(large_ofs_ind);
744 if ((ind - func_sub_sp_offset) != 5*4)
745 EI(0x13, 0, 0, 0, 0); // addi x0, x0, 0 == nop
746 ind = saved_ind;
749 ST_FUNC void gen_va_start(void)
751 tcc_error("implement me: %s", __FUNCTION__);
754 ST_FUNC void gen_va_arg(CType *t)
756 tcc_error("implement me: %s", __FUNCTION__);
759 ST_FUNC void gen_fill_nops(int bytes)
761 if ((bytes & 3))
762 tcc_error("alignment of code section not multiple of 4");
763 while (bytes > 0) {
764 EI(0x13, 0, 0, 0, 0); // addi x0, x0, 0 == nop
765 bytes -= 4;
769 // Generate forward branch to label:
770 ST_FUNC int gjmp(int t)
772 if (nocode_wanted)
773 return t;
774 o(t);
775 return ind - 4;
778 // Generate branch to known address:
779 ST_FUNC void gjmp_addr(int a)
781 uint32_t r = a - ind, imm;
782 if ((r + (1 << 21)) & ~((1U << 22) - 2)) {
783 o(0x17 | (5 << 7) | (((r + 0x800) & 0xfffff000))); // lui RR, up(r)
784 r = (int)r << 20 >> 20;
785 EI(0x67, 0, 0, 5, r); // jalr x0, r(t0)
786 } else {
787 imm = (((r >> 12) & 0xff) << 12)
788 | (((r >> 11) & 1) << 20)
789 | (((r >> 1) & 0x3ff) << 21)
790 | (((r >> 20) & 1) << 31);
791 o(0x6f | imm); // jal x0, imm == j imm
795 ST_FUNC int gjmp_cond(int op, int t)
797 int inv = op & 1;
798 assert(op == TOK_EQ || op == TOK_NE);
799 assert(vtop->cmp_r >= 10 && vtop->cmp_r < 18);
800 o(0x63 | (!inv << 12) | (vtop->cmp_r << 15) | (8 << 7)); // bne/beq x0,r,+4
801 return gjmp(t);
804 ST_FUNC int gjmp_append(int n, int t)
806 void *p;
807 /* insert jump list n into t */
808 if (n) {
809 uint32_t n1 = n, n2;
810 while ((n2 = read32le(p = cur_text_section->data + n1)))
811 n1 = n2;
812 write32le(p, t);
813 t = n;
815 return t;
818 static void gen_opil(int op, int ll)
820 int a, b, d;
821 int inv = 0;
822 int func3 = 0, func7 = 0;
823 /* XXX We could special-case some constant args. */
824 gv2(RC_INT, RC_INT);
825 a = ireg(vtop[-1].r);
826 b = ireg(vtop[0].r);
827 vtop -= 2;
828 d = get_reg(RC_INT);
829 vtop++;
830 vtop[0].r = d;
831 d = ireg(d);
832 ll = ll ? 0 : 8;
833 switch (op) {
834 default:
835 tcc_error("implement me: %s(%s)", __FUNCTION__, get_tok_str(op, NULL));
837 case '+':
838 o(0x33 | (d << 7) | (a << 15) | (b << 20)); // add d, a, b
839 break;
840 case '-':
841 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (0x20 << 25)); //sub d, a, b
842 break;
843 case TOK_SAR:
844 o(0x33 | ll | (d << 7) | (a << 15) | (b << 20) | (5 << 12) | (1 << 30)); //sra d, a, b
845 break;
846 case TOK_SHR:
847 o(0x33 | ll | (d << 7) | (a << 15) | (b << 20) | (5 << 12)); //srl d, a, b
848 break;
849 case TOK_SHL:
850 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (1 << 12)); //sll d, a, b
851 break;
852 case '*':
853 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (0x01 << 25)); //mul d, a, b
854 break;
855 case '/':
856 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (0x01 << 25) | (4 << 12)); //div d, a, b
857 break;
858 case '&':
859 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (7 << 12)); // and d, a, b
860 break;
861 case '^':
862 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (4 << 12)); // xor d, a, b
863 break;
864 case '|':
865 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (6 << 12)); // or d, a, b
866 break;
867 case '%':
868 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (0x01 << 25) | (6 << 12)); //rem d, a, b
869 break;
870 case TOK_UMOD:
871 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (0x01 << 25) | (7 << 12)); //remu d, a, b
872 break;
873 case TOK_PDIV:
874 case TOK_UDIV:
875 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (0x01 << 25) | (5 << 12)); //divu d, a, b
876 break;
878 case TOK_ULT:
879 case TOK_UGE:
880 case TOK_ULE:
881 case TOK_UGT:
882 case TOK_LT:
883 case TOK_GE:
884 case TOK_LE:
885 case TOK_GT:
886 if (op & 1) { // remove [U]GE,GT
887 inv = 1;
888 op--;
890 if ((op & 7) == 6) { // [U]LE
891 int t = a; a = b; b = t;
892 inv ^= 1;
894 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (((op > TOK_UGT) ? 2 : 3) << 12)); // slt[u] d, a, b
895 if (inv)
896 EI(0x13, 4, d, d, 1); // xori d, d, 1
897 vset_VT_CMP(TOK_NE);
898 vtop->cmp_r = d;
899 break;
900 case TOK_NE:
901 case TOK_EQ:
902 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (0x20 << 25)); // sub d, a, b
903 if (op == TOK_NE)
904 o(0x33 | (3 << 12) | (d << 7) | (0 << 15) | (d << 20)); // sltu d, x0, d == snez d,d
905 else
906 EI(0x13, 3, d, d, 1); // sltiu d, d, 1 == seqz d,d
907 vset_VT_CMP(TOK_NE);
908 vtop->cmp_r = d;
909 break;
913 ST_FUNC void gen_opi(int op)
915 gen_opil(op, 0);
918 ST_FUNC void gen_opl(int op)
920 gen_opil(op, 1);
923 ST_FUNC void gen_opf(int op)
925 int rs1, rs2, rd, dbl, invert;
926 if (vtop[0].type.t == VT_LDOUBLE) {
927 CType type = vtop[0].type;
928 int func = 0;
929 int cond = -1;
930 switch (op) {
931 case '*': func = TOK___multf3; break;
932 case '+': func = TOK___addtf3; break;
933 case '-': func = TOK___subtf3; break;
934 case '/': func = TOK___divtf3; break;
935 case TOK_EQ: func = TOK___eqtf2; cond = 1; break;
936 case TOK_NE: func = TOK___netf2; cond = 0; break;
937 case TOK_LT: func = TOK___lttf2; cond = 10; break;
938 case TOK_GE: func = TOK___getf2; cond = 11; break;
939 case TOK_LE: func = TOK___letf2; cond = 12; break;
940 case TOK_GT: func = TOK___gttf2; cond = 13; break;
941 default: assert(0); break;
943 vpush_global_sym(&func_old_type, func);
944 vrott(3);
945 gfunc_call(2);
946 vpushi(0);
947 vtop->r = REG_IRET;
948 vtop->r2 = cond < 0 ? TREG_R(1) : VT_CONST;
949 if (cond < 0)
950 vtop->type = type;
951 else {
952 vpushi(0);
953 gen_opil(op, 1);
955 return;
958 gv2(RC_FLOAT, RC_FLOAT);
959 assert(vtop->type.t == VT_DOUBLE || vtop->type.t == VT_FLOAT);
960 dbl = vtop->type.t == VT_DOUBLE;
961 rs1 = freg(vtop[-1].r);
962 rs2 = freg(vtop->r);
963 vtop--;
964 invert = 0;
965 switch(op) {
966 default:
967 assert(0);
968 case '+':
969 op = 0; // fadd
970 arithop:
971 rd = get_reg(RC_FLOAT);
972 vtop->r = rd;
973 rd = freg(rd);
974 o(0x53 | (rd << 7) | (rs1 << 15) | (rs2 << 20) | (7 << 12) | (dbl << 25) | (op << 27)); // fop.[sd] RD, RS1, RS2 (dyn rm)
975 break;
976 case '-':
977 op = 1; // fsub
978 goto arithop;
979 case '*':
980 op = 2; // fmul
981 goto arithop;
982 case '/':
983 op = 3; // fdiv
984 goto arithop;
985 case TOK_EQ:
986 op = 2; // EQ
987 cmpop:
988 rd = get_reg(RC_INT);
989 vtop->r = rd;
990 rd = ireg(rd);
991 o(0x53 | (rd << 7) | (rs1 << 15) | (rs2 << 20) | (op << 12) | (dbl << 25) | (0x14 << 27)); // fcmp.[sd] RD, RS1, RS2 (op == eq/lt/le)
992 if (invert)
993 EI(0x13, 4, rd, rd, 1); // xori RD, 1
994 break;
995 case TOK_NE:
996 invert = 1;
997 op = 2; // EQ
998 goto cmpop;
999 case TOK_LT:
1000 op = 1; // LT
1001 goto cmpop;
1002 case TOK_LE:
1003 op = 0; // LE
1004 goto cmpop;
1005 case TOK_GT:
1006 op = 1; // LT
1007 rd = rs1, rs1 = rs2, rs2 = rd;
1008 goto cmpop;
1009 case TOK_GE:
1010 op = 0; // LE
1011 rd = rs1, rs1 = rs2, rs2 = rd;
1012 goto cmpop;
1016 ST_FUNC void gen_cvt_sxtw(void)
1018 /* XXX on risc-v the registers are usually sign-extended already.
1019 Let's try to not do anything here. */
1022 ST_FUNC void gen_cvt_itof(int t)
1024 int rr = ireg(gv(RC_INT)), dr;
1025 int u = vtop->type.t & VT_UNSIGNED;
1026 int l = (vtop->type.t & VT_BTYPE) == VT_LLONG;
1027 if (t == VT_LDOUBLE) {
1028 int func = l ?
1029 (u ? TOK___floatunditf : TOK___floatditf) :
1030 (u ? TOK___floatunsitf : TOK___floatsitf);
1031 vpush_global_sym(&func_old_type, func);
1032 vrott(2);
1033 gfunc_call(1);
1034 vpushi(0);
1035 vtop->type.t = t;
1036 vtop->r = REG_IRET;
1037 vtop->r2 = TREG_R(1);
1038 } else {
1039 vtop--;
1040 dr = get_reg(RC_FLOAT);
1041 vtop++;
1042 vtop->r = dr;
1043 dr = freg(dr);
1044 EIu(0x53, 7, dr, rr, ((0x68 | (t == VT_DOUBLE ? 1 : 0)) << 5) | (u ? 1 : 0) | (l ? 2 : 0)); // fcvt.[sd].[wl][u]
1048 ST_FUNC void gen_cvt_ftoi(int t)
1050 int ft = vtop->type.t & VT_BTYPE;
1051 int l = (t & VT_BTYPE) == VT_LLONG;
1052 int u = t & VT_UNSIGNED;
1053 if (ft == VT_LDOUBLE) {
1054 int func = l ?
1055 (u ? TOK___fixunstfdi : TOK___fixtfdi) :
1056 (u ? TOK___fixunstfsi : TOK___fixtfsi);
1057 vpush_global_sym(&func_old_type, func);
1058 vrott(2);
1059 gfunc_call(1);
1060 vpushi(0);
1061 vtop->type.t = t;
1062 vtop->r = REG_IRET;
1063 } else {
1064 int rr = freg(gv(RC_FLOAT)), dr;
1065 vtop--;
1066 dr = get_reg(RC_INT);
1067 vtop++;
1068 vtop->r = dr;
1069 dr = ireg(dr);
1070 EIu(0x53, 1, dr, rr, ((0x60 | (ft == VT_DOUBLE ? 1 : 0)) << 5) | (u ? 1 : 0) | (l ? 2 : 0)); // fcvt.[wl][u].[sd] rtz
1074 ST_FUNC void gen_cvt_ftof(int dt)
1076 int st = vtop->type.t & VT_BTYPE, rs, rd;
1077 dt &= VT_BTYPE;
1078 if (st == dt)
1079 return;
1080 if (dt == VT_LDOUBLE || st == VT_LDOUBLE) {
1081 int func = (dt == VT_LDOUBLE) ?
1082 (st == VT_FLOAT ? TOK___extendsftf2 : TOK___extenddftf2) :
1083 (dt == VT_FLOAT ? TOK___trunctfsf2 : TOK___trunctfdf2);
1084 vpush_global_sym(&func_old_type, func);
1085 vrott(2);
1086 gfunc_call(1);
1087 vpushi(0);
1088 vtop->type.t = dt;
1089 if (dt == VT_LDOUBLE)
1090 vtop->r = REG_IRET, vtop->r2 = REG_IRET+1;
1091 else
1092 vtop->r = REG_FRET;
1093 } else {
1094 assert (dt == VT_FLOAT || dt == VT_DOUBLE);
1095 assert (st == VT_FLOAT || st == VT_DOUBLE);
1096 rs = gv(RC_FLOAT);
1097 rd = get_reg(RC_FLOAT);
1098 if (dt == VT_DOUBLE)
1099 EI(0x53, 7, freg(rd), freg(rs), 0x21 << 5); // fcvt.d.s RD, RS (dyn rm)
1100 else
1101 EI(0x53, 7, freg(rd), freg(rs), (0x20 << 5) | 1); // fcvt.s.d RD, RS
1102 vtop->r = rd;
1106 ST_FUNC void ggoto(void)
1108 gcall_or_jmp(0);
1109 vtop--;
1112 ST_FUNC void gen_vla_sp_save(int addr)
1114 ES(0x23, 3, 8, 2, addr); // sd sp, fc(s0)
1117 ST_FUNC void gen_vla_sp_restore(int addr)
1119 EI(0x03, 3, 2, 8, addr); // ld sp, fc(s0)
1122 ST_FUNC void gen_vla_alloc(CType *type, int align)
1124 int rr = ireg(gv(RC_INT));
1125 EI(0x13, 0, rr, rr, 15); // addi RR, RR, 15
1126 EI(0x13, 7, rr, rr, -16); // andi, RR, RR, -16
1127 o(0x33 | (2 << 7) | (2 << 15) | (rr << 20) | (0x20 << 25)); //sub sp, sp, rr
1128 vpop();
1130 #endif