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
23 #define LDOUBLE_SIZE 16
24 #define LDOUBLE_ALIGN 16
28 #define CHAR_IS_UNSIGNED
39 ST_DATA
const int reg_classes
[NB_REGS
] = {
61 static int ireg(int r
)
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
)
92 if (ind1
> cur_text_section
->data_allocated
)
93 section_realloc(cur_text_section
, ind1
);
94 write32le(cur_text_section
->data
+ ind
, c
);
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_
)
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
139 ST_FUNC
void load(int r
, SValue
*sv
)
142 int v
= fr
& VT_VALMASK
;
143 int rr
= is_ireg(r
) ? ireg(r
) : freg(r
);
145 int bt
= sv
->type
.t
& VT_BTYPE
;
146 int align
, size
= type_size(&sv
->type
, &align
);
148 int func3
, opcode
= 0x03, doload
= 0;
150 assert(bt
== VT_DOUBLE
|| bt
== VT_FLOAT
);
152 func3
= bt
== VT_DOUBLE
? 3 : 2;
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
))
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
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
)) {
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
);
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);
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);
203 EI(0x03, 3, tempr
, tempr
, 0); // ld TR, 0(TR)
205 EI(0x13, 0, tempr
, tempr
, fc
<< 20 >> 20); // addi TR, TR, FC
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;
212 tcc_error("unimp: load2(giant local ofs) (0x%llx)", (long long)sv
->c
.i
);
213 if (((unsigned)fc
+ (1 << 11)) >> 12) {
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
219 EI(0x03, 3, tempr
, br
, fc
); // ld TEMPR, fc(BR)
220 EI(opcode
, func3
, rr
, tempr
, 0); // l[bhwd][u] RR, 0(TEMPR)
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
);
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
);
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);
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);
253 if (is_float(sv
->type
.t
) && bt
!= VT_LDOUBLE
)
254 tcc_error("unimp: load(float)");
256 int64_t si
= sv
->c
.i
;
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
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
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.*/
280 if (((unsigned)fc
+ (1 << 11)) >> 12)
281 o(0x37 | (rr
<< 7) | ((0x800 + fc
) & 0xfffff000)), rb
= rr
; //lui RR, upper(fc)
283 EI(0x03, 3, rr
, rr
, 0); // ld RR, 0(RR)
285 EI(0x13 | do32bit
, 0, rr
, rr
, fc
<< 20 >> 20); // addi[w] R, x0|R, FC
287 EI(0x13 | do32bit
, 0, rr
, rb
, fc
<< 20 >> 20); // addi[w] R, x0|R, FC
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
) {
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
303 EI(0x13, 0, rr
, br
, fc
); // addi R, s0, FC
304 } else if (v
< VT_CONST
) {
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
312 int func7
= is_ireg(r
) ? 0x70 : 0x78;
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
) {
324 EI(0x13, 0, rr
, 0, t
); // addi RR, x0, t
327 EI(0x13, 0, rr
, 0, t
^ 1); // addi RR, x0, !t
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
);
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
)
346 tcc_error("unimp: store(struct)");
348 tcc_error("unimp: large sized store");
349 assert(sv
->r
& VT_LVAL
);
350 if (fr
== VT_LOCAL
) {
353 tcc_error("unimp: store(giant local off) (0x%llx)", (long long)sv
->c
.i
);
354 if (((unsigned)fc
+ (1 << 11)) >> 12) {
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
361 ES(0x27, size
== 4 ? 2 : 3, br
, rr
, fc
); // fs[wd] RR, fc(base)
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
371 ES(0x27, size
== 4 ? 2 : 3, ptrreg
, rr
, fc
); // fs[wd] RR, fc(PTRREG)
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
)) {
377 int tempr
, doload
= 0;
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
);
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);
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);
401 EI(0x03, 3, tempr
, tempr
, 0); // ld TR, 0(TR)
403 EI(0x13, 0, tempr
, tempr
, fc
<< 20 >> 20); // addi TR, TR, FC
407 ES(0x27, size
== 4 ? 2 : 3, tempr
, rr
, fc
); // fs[wd] RR, fc(TEMPR)
409 ES(0x23, size
== 1 ? 0 : size
== 2 ? 1 : size
== 4 ? 2 : 3,
410 tempr
, rr
, fc
); // s[bhwd] RR, fc(TEMPR)
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)
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;
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
);
454 tempspace
= (tempspace
+ align
- 1) & -align
;
464 if ((sv
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
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);
486 stack_adj
+= (size
+ align
- 1) & -align
;
491 info
[i
] |= 64 | (tempofs
<< 7);
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
++) {
502 size
= type_size(&vtop
->type
, &align
);
504 vset(&char_pointer_type
, TREG_SP
, 0);
505 vpushi(stack_adj
+ (info
[i
] >> 7));
507 vpushv(vtop
); // this replaces the old argument
510 vtop
->type
= vtop
[-1].type
;
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
;
528 vtop
->type
= vtop
[-1].type
;
531 vtop
->r
= vtop
->r2
= VT_CONST
; // this arg is done
535 } else if (info
[i
] & 16) {
542 for (i
= 0; i
< nb_args
; i
++) {
543 int r
= info
[nb_args
- 1 - i
];
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
;
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
) {
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
;
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
;
588 sym
= func_type
->ref
;
590 loc
= -16; // for ra and s0
591 func_sub_sp_offset
= ind
;
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
) {
602 ES(0x23, 3, 8, 10 + aireg
, loc
); // sd a0, loc(s0)
605 /* define parameters */
606 while ((sym
= sym
->next
) != NULL
) {
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
) {
618 addr
= (addr
+ align
- 1) & -align
;
622 int regcount
= 1, *pareg
= &aireg
;
623 if (is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LDOUBLE
)
625 if (regcount
+ *pareg
> 8)
629 loc
-= regcount
* 8; // XXX could reserve only 'size' bytes
631 for (i
= 0; i
< regcount
; i
++) {
633 assert(i
== 1 && regcount
== 2 && !(addr
& 7));
634 EI(0x03, 3, 5, 8, addr
); // ld t0, addr(s0)
636 ES(0x23, 3, 8, 5, loc
+ i
*8); // sd t0, loc(s0)
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)
643 ES(0x23, 3, 8, 10 + *pareg
, loc
+ i
*8); // sd aX, loc(s0) // XXX
648 sym_push(sym
->v
& ~SYM_FIELD
, &sym
->type
,
649 (byref
? VT_LLOCAL
: VT_LOCAL
) | lvalue_type(sym
->type
.t
),
653 if (func_type
->ref
->f
.func_type
== FUNC_ELLIPSIS
) {
654 for (; aireg
< 8; aireg
++) {
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
667 int align
, size
= type_size(vt
, &align
);
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
) {
691 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
698 nregs
= (size
+ 7) / 8;
700 vtop
->type
.t
= VT_LDOUBLE
;
702 if (is_float(func_type
->t
) && (vtop
->type
.t
& VT_BTYPE
) != VT_LDOUBLE
)
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)) {
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)) {
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);
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)
741 EI(0x13, 0, 8, 2, d
- num_va_regs
* 8); // addi s0, sp, d
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
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
)
762 tcc_error("alignment of code section not multiple of 4");
764 EI(0x13, 0, 0, 0, 0); // addi x0, x0, 0 == nop
769 // Generate forward branch to label:
770 ST_FUNC
int gjmp(int t
)
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)
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
)
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
804 ST_FUNC
int gjmp_append(int n
, int t
)
807 /* insert jump list n into t */
810 while ((n2
= read32le(p
= cur_text_section
->data
+ n1
)))
818 static void gen_opil(int op
, int ll
)
822 int func3
= 0, func7
= 0;
823 /* XXX We could special-case some constant args. */
825 a
= ireg(vtop
[-1].r
);
835 tcc_error("implement me: %s(%s)", __FUNCTION__
, get_tok_str(op
, NULL
));
838 o(0x33 | (d
<< 7) | (a
<< 15) | (b
<< 20)); // add d, a, b
841 o(0x33 | (d
<< 7) | (a
<< 15) | (b
<< 20) | (0x20 << 25)); //sub d, a, b
844 o(0x33 | ll
| (d
<< 7) | (a
<< 15) | (b
<< 20) | (5 << 12) | (1 << 30)); //sra d, a, b
847 o(0x33 | ll
| (d
<< 7) | (a
<< 15) | (b
<< 20) | (5 << 12)); //srl d, a, b
850 o(0x33 | (d
<< 7) | (a
<< 15) | (b
<< 20) | (1 << 12)); //sll d, a, b
853 o(0x33 | (d
<< 7) | (a
<< 15) | (b
<< 20) | (0x01 << 25)); //mul d, a, b
856 o(0x33 | (d
<< 7) | (a
<< 15) | (b
<< 20) | (0x01 << 25) | (4 << 12)); //div d, a, b
859 o(0x33 | (d
<< 7) | (a
<< 15) | (b
<< 20) | (7 << 12)); // and d, a, b
862 o(0x33 | (d
<< 7) | (a
<< 15) | (b
<< 20) | (4 << 12)); // xor d, a, b
865 o(0x33 | (d
<< 7) | (a
<< 15) | (b
<< 20) | (6 << 12)); // or d, a, b
868 o(0x33 | (d
<< 7) | (a
<< 15) | (b
<< 20) | (0x01 << 25) | (6 << 12)); //rem d, a, b
871 o(0x33 | (d
<< 7) | (a
<< 15) | (b
<< 20) | (0x01 << 25) | (7 << 12)); //remu d, a, b
875 o(0x33 | (d
<< 7) | (a
<< 15) | (b
<< 20) | (0x01 << 25) | (5 << 12)); //divu d, a, b
886 if (op
& 1) { // remove [U]GE,GT
890 if ((op
& 7) == 6) { // [U]LE
891 int t
= a
; a
= b
; b
= t
;
894 o(0x33 | (d
<< 7) | (a
<< 15) | (b
<< 20) | (((op
> TOK_UGT
) ? 2 : 3) << 12)); // slt[u] d, a, b
896 EI(0x13, 4, d
, d
, 1); // xori d, d, 1
902 o(0x33 | (d
<< 7) | (a
<< 15) | (b
<< 20) | (0x20 << 25)); // sub d, a, b
904 o(0x33 | (3 << 12) | (d
<< 7) | (0 << 15) | (d
<< 20)); // sltu d, x0, d == snez d,d
906 EI(0x13, 3, d
, d
, 1); // sltiu d, d, 1 == seqz d,d
913 ST_FUNC
void gen_opi(int op
)
918 ST_FUNC
void gen_opl(int op
)
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
;
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
);
948 vtop
->r2
= cond
< 0 ? TREG_R(1) : VT_CONST
;
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
);
971 rd
= get_reg(RC_FLOAT
);
974 o(0x53 | (rd
<< 7) | (rs1
<< 15) | (rs2
<< 20) | (7 << 12) | (dbl
<< 25) | (op
<< 27)); // fop.[sd] RD, RS1, RS2 (dyn rm)
988 rd
= get_reg(RC_INT
);
991 o(0x53 | (rd
<< 7) | (rs1
<< 15) | (rs2
<< 20) | (op
<< 12) | (dbl
<< 25) | (0x14 << 27)); // fcmp.[sd] RD, RS1, RS2 (op == eq/lt/le)
993 EI(0x13, 4, rd
, rd
, 1); // xori RD, 1
1007 rd
= rs1
, rs1
= rs2
, rs2
= rd
;
1011 rd
= rs1
, rs1
= rs2
, rs2
= rd
;
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
) {
1029 (u
? TOK___floatunditf
: TOK___floatditf
) :
1030 (u
? TOK___floatunsitf
: TOK___floatsitf
);
1031 vpush_global_sym(&func_old_type
, func
);
1037 vtop
->r2
= TREG_R(1);
1040 dr
= get_reg(RC_FLOAT
);
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
) {
1055 (u
? TOK___fixunstfdi
: TOK___fixtfdi
) :
1056 (u
? TOK___fixunstfsi
: TOK___fixtfsi
);
1057 vpush_global_sym(&func_old_type
, func
);
1064 int rr
= freg(gv(RC_FLOAT
)), dr
;
1066 dr
= get_reg(RC_INT
);
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
;
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
);
1089 if (dt
== VT_LDOUBLE
)
1090 vtop
->r
= REG_IRET
, vtop
->r2
= REG_IRET
+1;
1094 assert (dt
== VT_FLOAT
|| dt
== VT_DOUBLE
);
1095 assert (st
== VT_FLOAT
|| st
== VT_DOUBLE
);
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)
1101 EI(0x53, 7, freg(rd
), freg(rs
), (0x20 << 5) | 1); // fcvt.s.d RD, RS
1106 ST_FUNC
void ggoto(void)
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