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 EI(uint32_t opcode
, uint32_t func3
,
99 uint32_t rd
, uint32_t rs1
, uint32_t imm
)
101 assert(! ((imm
+ (1 << 11)) >> 12));
102 o(opcode
| (func3
<< 12) | (rd
<< 7) | (rs1
<< 15) | (imm
<< 20));
105 static void ES(uint32_t opcode
, uint32_t func3
,
106 uint32_t rs1
, uint32_t rs2
, uint32_t imm
)
108 assert(! ((imm
+ (1 << 11)) >> 12));
109 o(opcode
| (func3
<< 12) | ((imm
& 0x1f) << 7) | (rs1
<< 15)
110 | (rs2
<< 20) | ((imm
>> 5) << 25));
113 // Patch all branches in list pointed to by t to branch to a:
114 ST_FUNC
void gsym_addr(int t_
, int a_
)
119 unsigned char *ptr
= cur_text_section
->data
+ t
;
120 uint32_t next
= read32le(ptr
);
121 uint32_t r
= a
- t
, imm
;
122 if ((r
+ (1 << 21)) & ~((1U << 22) - 2))
123 tcc_error("out-of-range branch chain");
124 imm
= (((r
>> 12) & 0xff) << 12)
125 | (((r
>> 11) & 1) << 20)
126 | (((r
>> 1) & 0x3ff) << 21)
127 | (((r
>> 20) & 1) << 31);
128 write32le(ptr
, r
== 4 ? 0x33 : 0x6f | imm
); // nop || j imm
133 ST_FUNC
void load(int r
, SValue
*sv
)
136 int v
= fr
& VT_VALMASK
;
137 int rr
= is_ireg(r
) ? ireg(r
) : freg(r
);
139 int bt
= sv
->type
.t
& VT_BTYPE
;
140 int align
, size
= type_size(&sv
->type
, &align
);
142 int func3
, opcode
= 0x03;
144 assert(bt
== VT_DOUBLE
|| bt
== VT_FLOAT
);
146 func3
= bt
== VT_DOUBLE
? 3 : 2;
151 func3
= size
== 1 ? 0 : size
== 2 ? 1 : size
== 4 ? 2 : 3;
152 if (size
< 8 && !is_float(sv
->type
.t
) && (sv
->type
.t
& VT_UNSIGNED
))
158 tcc_error("unimp: load1(giant local ofs) (0x%llx)", (long long)sv
->c
.i
);
159 if (((unsigned)fc
+ (1 << 11)) >> 12) {
160 br
= is_ireg(r
) ? rr
: ireg(get_reg(RC_INT
));
161 o(0x37 | (br
<< 7) | ((0x800 + fc
) & 0xfffff000)); //lui BR, upper(fc)
162 o(0x33 | (br
<< 7) | (br
<< 15) | (8 << 20)); // add BR, BR, s0
165 EI(opcode
, func3
, rr
, br
, fc
); // l[bhwd][u]/fl[wd] RR, fc(BR)
166 } else if (v
< VT_CONST
) {
167 /*if (((unsigned)fc + (1 << 11)) >> 12)
168 tcc_error("unimp: load(large addend) (0x%x)", fc);*/
169 fc
= 0; // XXX store ofs in LVAL(reg)
170 EI(opcode
, func3
, rr
, ireg(v
), fc
); // l[bhwd][u] RR, 0(V)
171 } else if (v
== VT_CONST
&& (fr
& VT_SYM
)) {
173 int addend
= 0, tempr
;
174 if (1 || ((unsigned)fc
+ (1 << 11)) >> 12)
177 greloca(cur_text_section
, sv
->sym
, ind
,
178 R_RISCV_PCREL_HI20
, addend
);
180 label
.v
= tok_alloc(".L0 ", 4)->tok
;
181 label
.type
.t
= VT_VOID
| VT_STATIC
;
183 label
.c
= 0; /* force new local ELF symbol */
184 put_extern_sym(&label
, cur_text_section
, ind
, 0);
185 tempr
= is_ireg(r
) ? rr
: ireg(get_reg(RC_INT
));
186 o(0x17 | (tempr
<< 7)); // auipc TR, 0 %pcrel_hi(sym)+addend
187 greloca(cur_text_section
, &label
, ind
,
188 R_RISCV_PCREL_LO12_I
, 0);
189 EI(opcode
, func3
, rr
, tempr
, fc
); // l[bhwd][u] RR, fc(TR)
190 } else if (v
== VT_LLOCAL
) {
191 int br
= 8, tempr
= is_ireg(r
) ? rr
: ireg(get_reg(RC_INT
));
193 tcc_error("unimp: load2(giant local ofs) (0x%llx)", (long long)sv
->c
.i
);
194 if (((unsigned)fc
+ (1 << 11)) >> 12) {
196 o(0x37 | (br
<< 7) | ((0x800 + fc
) & 0xfffff000)); //lui BR, upper(fc)
197 o(0x33 | (br
<< 7) | (br
<< 15) | (8 << 20)); // add BR, BR, s0
200 EI(0x03, 3, tempr
, br
, fc
); // ld TEMPR, fc(BR)
201 EI(opcode
, func3
, rr
, tempr
, 0); // l[bhwd][u] RR, 0(TEMPR)
203 tcc_error("unimp: load(non-local lval)");
205 } else if (v
== VT_CONST
) {
206 int rb
= 0, do32bit
= 8, doload
= 0;
207 assert(!is_float(sv
->type
.t
) && is_ireg(r
) || bt
== VT_LDOUBLE
);
210 if (sv
->sym
->type
.t
& VT_STATIC
) { // XXX do this per linker relax
211 greloca(cur_text_section
, sv
->sym
, ind
,
212 R_RISCV_PCREL_HI20
, sv
->c
.i
);
216 if (((unsigned)fc
+ (1 << 11)) >> 12)
217 tcc_error("unimp: large addend for global address");
218 greloca(cur_text_section
, sv
->sym
, ind
,
219 R_RISCV_GOT_HI20
, 0);
223 label
.v
= tok_alloc(".L0 ", 4)->tok
;
224 label
.type
.t
= VT_VOID
| VT_STATIC
;
226 label
.c
= 0; /* force new local ELF symbol */
227 put_extern_sym(&label
, cur_text_section
, ind
, 0);
228 o(0x17 | (rr
<< 7)); // auipc RR, 0 %call(func)
229 greloca(cur_text_section
, &label
, ind
,
230 R_RISCV_PCREL_LO12_I
, 0);
234 if (is_float(sv
->type
.t
) && bt
!= VT_LDOUBLE
)
235 tcc_error("unimp: load(float)");
237 int64_t si
= sv
->c
.i
;
244 o(0x37 | (rr
<< 7) | (((pi
+ 0x800) & 0xfffff000))); // lui RR, up(up(fc))
245 EI(0x13, 0, rr
, rr
, (int)pi
<< 20 >> 20); // addi RR, RR, lo(up(fc))
246 EI(0x13, 1, rr
, rr
, 12); // slli RR, RR, 12
247 EI(0x13, 0, rr
, rr
, (fc
+ (1 << 19)) >> 20); // addi RR, RR, up(lo(fc))
248 EI(0x13, 1, rr
, rr
, 12); // slli RR, RR, 12
250 EI(0x13, 0, rr
, rr
, fc
>> 8); // addi RR, RR, lo1(lo(fc))
251 EI(0x13, 1, rr
, rr
, 8); // slli RR, RR, 8
256 /* A 32bit unsigned constant. lui always sign extends, so we need
258 pi
= (uint32_t)sv
->c
.i
;
259 o(0x37 | (rr
<< 7) | (((pi
+ 0x80000) & 0xfff00000) >> 8)); // lui RR, up(fc)>>8
260 EI(0x13, 0, rr
, rr
, (((pi
+ 0x200) & 0x000ffc00) >> 8) | (-((int)(pi
+ 0x200) & 0x80000) >> 8)); // addi RR, RR, mid(fc)
261 EI(0x13, 1, rr
, rr
, 8); // slli RR, RR, 8
262 fc
= (pi
& 0x3ff) | (-((int)(pi
& 0x200)));
267 if (((unsigned)fc
+ (1 << 11)) >> 12)
268 o(0x37 | (rr
<< 7) | ((0x800 + fc
) & 0xfffff000)), rb
= rr
; //lui RR, upper(fc)
270 EI(0x03, 3, rr
, rr
, 0); // ld RR, 0(RR)
272 EI(0x13 | do32bit
, 0, rr
, rr
, fc
<< 20 >> 20); // addi[w] R, x0|R, FC
274 EI(0x13 | do32bit
, 0, rr
, rb
, fc
<< 20 >> 20); // addi[w] R, x0|R, FC
275 } else if (v
== VT_LOCAL
) {
279 tcc_error("unimp: load(addr giant local ofs) (0xll%x)", (long long)sv
->c
.i
);
280 if (((unsigned)fc
+ (1 << 11)) >> 12) {
281 o(0x37 | (rr
<< 7) | ((0x800 + fc
) & 0xfffff000)); //lui RR, upper(fc)
282 o(0x33 | (rr
<< 7) | (rr
<< 15) | (8 << 20)); // add RR, RR, s0
286 EI(0x13, 0, rr
, br
, fc
); // addi R, s0, FC
287 } else if (v
< VT_CONST
) {
289 //assert(!fc); XXX support offseted regs
290 if (is_freg(r
) && is_freg(v
))
291 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
292 else if (is_ireg(r
) && is_ireg(v
))
293 EI(0x13, 0, rr
, ireg(v
), 0); // addi RR, V, 0 == mv RR, V
295 int func7
= is_ireg(r
) ? 0x70 : 0x78;
298 assert(size
== 4 || size
== 8);
299 o(0x53 | (rr
<< 7) | ((is_freg(v
) ? freg(v
) : ireg(v
)) << 15)
300 | (func7
<< 25)); // fmv.{w.x, x.w, d.x, x.d} RR, VR
302 } else if (v
== VT_CMP
) { // we rely on cmp_r to be the correct result
303 EI(0x13, 0, rr
, vtop
->cmp_r
, 0); // mv RR, CMP_R
304 } else if ((v
& ~1) == VT_JMP
) {
307 EI(0x13, 0, rr
, 0, t
); // addi RR, x0, t
310 EI(0x13, 0, rr
, 0, t
^ 1); // addi RR, x0, !t
312 tcc_error("unimp: load(non-const)");
315 ST_FUNC
void store(int r
, SValue
*sv
)
317 int fr
= sv
->r
& VT_VALMASK
;
318 int rr
= is_ireg(r
) ? ireg(r
) : freg(r
);
321 int bt
= ft
& VT_BTYPE
;
322 int align
, size
= type_size(&sv
->type
, &align
);
323 assert(!is_float(bt
) || is_freg(r
) || bt
== VT_LDOUBLE
);
324 /* long doubles are in two integer registers, but the load/store
325 primitives only deal with one, so do as if it's one reg. */
326 if (bt
== VT_LDOUBLE
)
329 tcc_error("unimp: store(struct)");
331 tcc_error("unimp: large sized store");
332 assert(sv
->r
& VT_LVAL
);
333 if (fr
== VT_LOCAL
) {
336 tcc_error("unimp: store(giant local off) (0x%llx)", (long long)sv
->c
.i
);
337 if (((unsigned)fc
+ (1 << 11)) >> 12) {
338 br
= ireg(get_reg(RC_INT
));
339 o(0x37 | (br
<< 7) | ((0x800 + fc
) & 0xfffff000)); //lui BR, upper(fc)
340 o(0x33 | (br
<< 7) | (br
<< 15) | (8 << 20)); // add BR, BR, s0
344 ES(0x27, size
== 4 ? 2 : 3, br
, rr
, fc
); // fs[wd] RR, fc(base)
346 ES(0x23, size
== 1 ? 0 : size
== 2 ? 1 : size
== 4 ? 2 : 3,
347 br
, rr
, fc
); // s[bhwd] RR, fc(base)
348 } else if (fr
< VT_CONST
) {
349 int ptrreg
= ireg(fr
);
350 /*if (((unsigned)fc + (1 << 11)) >> 12)
351 tcc_error("unimp: store(large addend) (0x%x)", fc);*/
352 fc
= 0; // XXX support offsets regs
354 ES(0x27, size
== 4 ? 2 : 3, ptrreg
, rr
, fc
); // fs[wd] RR, fc(PTRREG)
356 ES(0x23, size
== 1 ? 0 : size
== 2 ? 1 : size
== 4 ? 2 : 3,
357 ptrreg
, rr
, fc
); // s[bhwd] RR, fc(PTRREG)
358 } else if (sv
->r
== (VT_CONST
| VT_SYM
| VT_LVAL
)) {
360 int tempr
, addend
= 0;
361 if (1 || ((unsigned)fc
+ (1 << 11)) >> 12)
364 tempr
= ireg(get_reg(RC_INT
));
365 greloca(cur_text_section
, sv
->sym
, ind
,
366 R_RISCV_PCREL_HI20
, addend
);
368 label
.v
= tok_alloc(".L0 ", 4)->tok
;
369 label
.type
.t
= VT_VOID
| VT_STATIC
;
371 label
.c
= 0; /* force new local ELF symbol */
372 put_extern_sym(&label
, cur_text_section
, ind
, 0);
373 o(0x17 | (tempr
<< 7)); // auipc TEMPR, 0 %pcrel_hi(sym)+addend
374 greloca(cur_text_section
, &label
, ind
,
375 R_RISCV_PCREL_LO12_S
, 0);
377 ES(0x27, size
== 4 ? 2 : 3, tempr
, rr
, fc
); // fs[wd] RR, fc(TEMPR)
379 ES(0x23, size
== 1 ? 0 : size
== 2 ? 1 : size
== 4 ? 2 : 3,
380 tempr
, rr
, fc
); // s[bhwd] RR, fc(TEMPR)
382 tcc_error("implement me: %s(!local)", __FUNCTION__
);
385 static void gcall(void)
387 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
&&
388 ((vtop
->r
& VT_SYM
) && vtop
->c
.i
== (int)vtop
->c
.i
)) {
389 /* constant symbolic case -> simple relocation */
390 greloca(cur_text_section
, vtop
->sym
, ind
,
391 R_RISCV_CALL_PLT
, (int)vtop
->c
.i
);
392 o(0x17 | (1 << 7)); // auipc ra, 0 %call(func)
393 o(0x80e7); // jalr ra, 0 %call(func)
394 } else if (vtop
->r
< VT_CONST
) {
395 int r
= ireg(vtop
->r
);
396 EI(0x67, 0, 1, r
, 0); // jalr ra, 0(R)
401 EI(0x67, 0, 1, r
, 0); // jalr ra, 0(R)
405 ST_FUNC
void gfunc_call(int nb_args
)
407 int i
, align
, size
, aireg
, afreg
;
408 int info
[nb_args
? nb_args
: 1];
409 int stack_adj
= 0, tempspace
= 0, ofs
, splitofs
= 0;
414 sa
= vtop
[-nb_args
].type
.ref
->next
;
415 for (i
= 0; i
< nb_args
; i
++) {
416 int *pareg
, nregs
, infreg
= 0, byref
= 0, tempofs
;
417 sv
= &vtop
[1 + i
- nb_args
];
418 sv
->type
.t
&= ~VT_ARRAY
; // XXX this should be done in tccgen.c
419 size
= type_size(&sv
->type
, &align
);
423 tempspace
= (tempspace
+ align
- 1) & -align
;
433 if ((sv
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
436 infreg
= sa
&& is_float(sv
->type
.t
);
437 if (!infreg
&& !sa
&& align
== 2*XLEN
&& size
<= 2*XLEN
)
438 aireg
= (aireg
+ 1) & ~1;
439 pareg
= infreg
? &afreg
: &aireg
;
440 if ((*pareg
< 8) && !force_stack
) {
441 info
[i
] = *pareg
+ (infreg
? 8 : 0);
455 stack_adj
+= (size
+ align
- 1) & -align
;
460 info
[i
] |= 64 | (tempofs
<< 7);
464 stack_adj
= (stack_adj
+ 15) & -16;
465 tempspace
= (tempspace
+ 15) & -16;
466 if (stack_adj
+ tempspace
) {
467 EI(0x13, 0, 2, 2, -(stack_adj
+ tempspace
)); // addi sp, sp, -adj
468 for (i
= ofs
= 0; i
< nb_args
; i
++) {
471 size
= type_size(&vtop
->type
, &align
);
473 vset(&char_pointer_type
, TREG_SP
, 0);
474 vpushi(stack_adj
+ (info
[i
] >> 7));
476 vpushv(vtop
); // this replaces the old argument
479 vtop
->type
= vtop
[-1].type
;
488 /* Once we support offseted regs we can do this:
489 vset(&vtop->type, TREG_SP | VT_LVAL, ofs);
490 to construct the lvalue for the outgoing stack slot,
491 until then we have to jump through hoops. */
492 vset(&char_pointer_type
, TREG_SP
, 0);
493 ofs
= (ofs
+ align
- 1) & -align
;
497 vtop
->type
= vtop
[-1].type
;
500 vtop
->r
= vtop
->r2
= VT_CONST
; // this arg is done
504 } else if (info
[i
] & 16) {
511 for (i
= 0; i
< nb_args
; i
++) {
512 int r
= info
[nb_args
- 1 - i
];
517 origtype
= vtop
->type
;
518 size
= type_size(&vtop
->type
, &align
);
519 if (size
> 8 && (vtop
->type
.t
& VT_BTYPE
) == VT_STRUCT
)
520 vtop
->type
.t
= VT_LDOUBLE
; // force loading a pair of regs
521 gv(r
< 8 ? RC_R(r
) : RC_F(r
- 8));
522 vtop
->type
= origtype
;
524 assert((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
525 || (vtop
->type
.t
& VT_BTYPE
) == VT_STRUCT
);
526 assert(vtop
->r2
< VT_CONST
);
527 if (info
[nb_args
- 1 - i
] & 16) {
528 ES(0x23, 3, 2, ireg(vtop
->r2
), splitofs
); // sd t0, ofs(sp)
529 } else if (vtop
->r2
!= 1 + vtop
->r
) {
531 /* XXX we'd like to have 'gv' move directly into
532 the right class instead of us fixing it up. */
533 EI(0x13, 0, ireg(vtop
->r
) + 1, ireg(vtop
->r2
), 0); // mv Ra+1, RR2
534 vtop
->r2
= 1 + vtop
->r
;
543 if (stack_adj
+ tempspace
)
544 EI(0x13, 0, 2, 2, stack_adj
+ tempspace
); // addi sp, sp, adj
547 static int func_sub_sp_offset
, num_va_regs
;
549 ST_FUNC
void gfunc_prolog(CType
*func_type
)
551 int i
, addr
, align
, size
;
557 sym
= func_type
->ref
;
559 loc
= -16; // for ra and s0
560 func_sub_sp_offset
= ind
;
564 addr
= 0; // XXX not correct
565 /* if the function returns by reference, then add an
566 implicit pointer parameter */
567 size
= type_size(&func_vt
, &align
);
568 if (size
> 2 * XLEN
) {
571 ES(0x23, 3, 8, 10 + aireg
, loc
); // sd a0, loc(s0)
574 /* define parameters */
575 while ((sym
= sym
->next
) != NULL
) {
578 size
= type_size(type
, &align
);
579 if (size
> 2 * XLEN
) {
580 type
= &char_pointer_type
;
581 size
= align
= byref
= 8;
583 if (size
> 2 * XLEN
) {
587 addr
= (addr
+ align
- 1) & -align
;
591 int regcount
= 1, *pareg
= &aireg
;
592 if (is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LDOUBLE
)
594 if (regcount
+ *pareg
> 8)
598 loc
-= regcount
* 8; // XXX could reserve only 'size' bytes
600 for (i
= 0; i
< regcount
; i
++) {
602 assert(i
== 1 && regcount
== 2 && !(addr
& 7));
603 EI(0x03, 3, 5, 8, addr
); // ld t0, addr(s0)
605 ES(0x23, 3, 8, 5, loc
+ i
*8); // sd t0, loc(s0)
608 if (pareg
== &afreg
) {
609 assert(type
->t
== VT_FLOAT
|| type
->t
== VT_DOUBLE
);
610 ES(0x27, size
== 4 ? 2 : 3, 8, 10 + *pareg
, loc
+ i
*8); // fs[wd] FAi, loc(s0)
612 ES(0x23, 3, 8, 10 + *pareg
, loc
+ i
*8); // sd aX, loc(s0) // XXX
617 sym_push(sym
->v
& ~SYM_FIELD
, &sym
->type
,
618 (byref
? VT_LLOCAL
: VT_LOCAL
) | lvalue_type(sym
->type
.t
),
622 if (func_type
->ref
->f
.func_type
== FUNC_ELLIPSIS
) {
623 for (; aireg
< 8; aireg
++) {
625 ES(0x23, 3, 8, 10 + aireg
, -8 + num_va_regs
* 8); // sd aX, loc(s0)
630 ST_FUNC
int gfunc_sret(CType
*vt
, int variadic
, CType
*ret
,
631 int *ret_align
, int *regsize
)
633 /* generic code can only deal with structs of pow(2) sizes
634 (it always deals with whole registers), so go through our own
636 int align
, size
= type_size(vt
, &align
);
651 return (size
+ 7) / 8;
654 ST_FUNC
void gfunc_return(CType
*func_type
)
656 int align
, size
= type_size(func_type
, &align
), nregs
;
657 CType type
= *func_type
;
658 if (size
> 2 * XLEN
) {
660 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
667 nregs
= (size
+ 7) / 8;
669 vtop
->type
.t
= VT_LDOUBLE
;
671 if (is_float(func_type
->t
) && (vtop
->type
.t
& VT_BTYPE
) != VT_LDOUBLE
)
678 ST_FUNC
void gfunc_epilog(void)
680 int v
, saved_ind
, d
, large_ofs_ind
;
682 loc
= (loc
- num_va_regs
* 8);
683 d
= v
= (-loc
+ 15) & -16;
685 if (v
>= (1 << 11)) {
687 o(0x37 | (5 << 7) | ((0x800 + (v
-16)) & 0xfffff000)); //lui t0, upper(v)
688 EI(0x13, 0, 5, 5, (v
-16) << 20 >> 20); // addi t0, t0, lo(v)
689 o(0x33 | (2 << 7) | (2 << 15) | (5 << 20)); //add sp, sp, t0
691 EI(0x03, 3, 1, 2, d
- 8 - num_va_regs
* 8); // ld ra, v-8(sp)
692 EI(0x03, 3, 8, 2, d
- 16 - num_va_regs
* 8); // ld s0, v-16(sp)
693 EI(0x13, 0, 2, 2, d
); // addi sp, sp, v
694 EI(0x67, 0, 0, 1, 0); // jalr x0, 0(x1), aka ret
695 if (v
>= (1 << 11)) {
697 EI(0x13, 0, 8, 2, d
- num_va_regs
* 8); // addi s0, sp, d
698 o(0x37 | (5 << 7) | ((0x800 + (v
-16)) & 0xfffff000)); //lui t0, upper(v)
699 EI(0x13, 0, 5, 5, (v
-16) << 20 >> 20); // addi t0, t0, lo(v)
700 o(0x33 | (2 << 7) | (2 << 15) | (5 << 20) | (0x20 << 25)); //sub sp, sp, t0
701 gjmp_addr(func_sub_sp_offset
+ 5*4);
705 ind
= func_sub_sp_offset
;
706 EI(0x13, 0, 2, 2, -d
); // addi sp, sp, -d
707 ES(0x23, 3, 2, 1, d
- 8 - num_va_regs
* 8); // sd ra, d-8(sp)
708 ES(0x23, 3, 2, 8, d
- 16 - num_va_regs
* 8); // sd s0, d-16(sp)
710 EI(0x13, 0, 8, 2, d
- num_va_regs
* 8); // addi s0, sp, d
712 gjmp_addr(large_ofs_ind
);
713 if ((ind
- func_sub_sp_offset
) != 5*4)
714 EI(0x13, 0, 0, 0, 0); // addi x0, x0, 0 == nop
718 ST_FUNC
void gen_va_start(void)
720 tcc_error("implement me: %s", __FUNCTION__
);
723 ST_FUNC
void gen_va_arg(CType
*t
)
725 tcc_error("implement me: %s", __FUNCTION__
);
728 ST_FUNC
void gen_fill_nops(int bytes
)
730 tcc_error("implement me: %s", __FUNCTION__
);
732 tcc_error("alignment of code section not multiple of 4");
735 // Generate forward branch to label:
736 ST_FUNC
int gjmp(int t
)
744 // Generate branch to known address:
745 ST_FUNC
void gjmp_addr(int a
)
747 uint32_t r
= a
- ind
, imm
;
748 if ((r
+ (1 << 21)) & ~((1U << 22) - 2)) {
749 o(0x17 | (5 << 7) | (((r
+ 0x800) & 0xfffff000))); // lui RR, up(r)
750 r
= (int)r
<< 20 >> 20;
751 EI(0x67, 0, 0, 5, r
); // jalr x0, r(t0)
753 imm
= (((r
>> 12) & 0xff) << 12)
754 | (((r
>> 11) & 1) << 20)
755 | (((r
>> 1) & 0x3ff) << 21)
756 | (((r
>> 20) & 1) << 31);
757 o(0x6f | imm
); // jal x0, imm == j imm
761 ST_FUNC
int gjmp_cond(int op
, int t
)
764 assert(op
== TOK_EQ
|| op
== TOK_NE
);
765 assert(vtop
->cmp_r
>= 10 && vtop
->cmp_r
< 18);
766 o(0x63 | (!inv
<< 12) | (vtop
->cmp_r
<< 15) | (8 << 7)); // bne/beq x0,r,+4
770 ST_FUNC
int gjmp_append(int n
, int t
)
773 /* insert jump list n into t */
776 while ((n2
= read32le(p
= cur_text_section
->data
+ n1
)))
784 static void gen_opil(int op
, int ll
)
788 int func3
= 0, func7
= 0;
789 /* XXX We could special-case some constant args. */
791 a
= ireg(vtop
[-1].r
);
802 tcc_error("implement me: %s(%s)", __FUNCTION__
, get_tok_str(op
, NULL
));
805 o(0x33 | (d
<< 7) | (a
<< 15) | (b
<< 20)); // add d, a, b
808 o(0x33 | (d
<< 7) | (a
<< 15) | (b
<< 20) | (0x20 << 25)); //sub d, a, b
811 o(0x33 | ll
| (d
<< 7) | (a
<< 15) | (b
<< 20) | (5 << 12) | (1 << 30)); //sra d, a, b
814 o(0x33 | ll
| (d
<< 7) | (a
<< 15) | (b
<< 20) | (5 << 12)); //srl d, a, b
817 o(0x33 | (d
<< 7) | (a
<< 15) | (b
<< 20) | (1 << 12)); //sll d, a, b
820 o(0x33 | (d
<< 7) | (a
<< 15) | (b
<< 20) | (0x01 << 25)); //mul d, a, b
823 o(0x33 | (d
<< 7) | (a
<< 15) | (b
<< 20) | (0x01 << 25) | (4 << 12)); //div d, a, b
826 o(0x33 | (d
<< 7) | (a
<< 15) | (b
<< 20) | (7 << 12)); // and d, a, b
829 o(0x33 | (d
<< 7) | (a
<< 15) | (b
<< 20) | (4 << 12)); // xor d, a, b
832 o(0x33 | (d
<< 7) | (a
<< 15) | (b
<< 20) | (6 << 12)); // or d, a, b
835 o(0x33 | (d
<< 7) | (a
<< 15) | (b
<< 20) | (0x01 << 25) | (6 << 12)); //rem d, a, b
838 o(0x33 | (d
<< 7) | (a
<< 15) | (b
<< 20) | (0x01 << 25) | (7 << 12)); //remu d, a, b
841 o(0x33 | (d
<< 7) | (a
<< 15) | (b
<< 20) | (0x01 << 25) | (5 << 12)); //divu d, a, b
852 if (op
& 1) { // remove [U]GE,GT
856 if ((op
& 7) == 6) { // [U]LE
857 int t
= a
; a
= b
; b
= t
;
860 o(0x33 | (d
<< 7) | (a
<< 15) | (b
<< 20) | (((op
> TOK_UGT
) ? 2 : 3) << 12)); // slt[u] d, a, b
862 EI(0x13, 4, d
, d
, 1); // xori d, d, 1
868 o(0x33 | (d
<< 7) | (a
<< 15) | (b
<< 20) | (0x20 << 25)); // sub d, a, b
870 o(0x33 | (3 << 12) | (d
<< 7) | (0 << 15) | (d
<< 20)); // sltu d, x0, d == snez d,d
872 EI(0x13, 3, d
, d
, 1); // sltiu d, d, 1 == seqz d,d
879 ST_FUNC
void gen_opi(int op
)
884 ST_FUNC
void gen_opl(int op
)
889 ST_FUNC
void gen_opf(int op
)
891 int rs1
, rs2
, rd
, dbl
, invert
;
892 gv2(RC_FLOAT
, RC_FLOAT
);
893 assert(vtop
->type
.t
== VT_DOUBLE
|| vtop
->type
.t
== VT_FLOAT
);
894 dbl
= vtop
->type
.t
== VT_DOUBLE
;
895 rs1
= freg(vtop
[-1].r
);
905 rd
= get_reg(RC_FLOAT
);
908 o(0x53 | (rd
<< 7) | (rs1
<< 15) | (rs2
<< 20) | (7 << 12) | (dbl
<< 25) | (op
<< 27)); // fop.[sd] RD, RS1, RS2 (dyn rm)
922 rd
= get_reg(RC_INT
);
925 o(0x53 | (rd
<< 7) | (rs1
<< 15) | (rs2
<< 20) | (op
<< 12) | (dbl
<< 25) | (0x14 << 27)); // fcmp.[sd] RD, RS1, RS2 (op == eq/lt/le)
927 EI(0x13, 4, rd
, rd
, 1); // xori RD, 1
941 rd
= rs1
, rs1
= rs2
, rs2
= rd
;
945 rd
= rs1
, rs1
= rs2
, rs2
= rd
;
950 ST_FUNC
void gen_cvt_sxtw(void)
952 /* XXX on risc-v the registers are usually sign-extended already.
953 Let's try to not do anything here. */
956 ST_FUNC
void gen_cvt_itof(int t
)
958 tcc_error("implement me: %s", __FUNCTION__
);
961 ST_FUNC
void gen_cvt_ftoi(int t
)
963 tcc_error("implement me: %s", __FUNCTION__
);
966 ST_FUNC
void gen_cvt_ftof(int dt
)
968 int st
= vtop
->type
.t
& VT_BTYPE
, rs
, rd
;
972 if (dt
== VT_LDOUBLE
|| st
== VT_LDOUBLE
) {
973 int func
= (dt
== VT_LDOUBLE
) ?
974 (st
== VT_FLOAT
? TOK___extendsftf2
: TOK___extenddftf2
) :
975 (st
== VT_FLOAT
? TOK___trunctfsf2
: TOK___trunctfdf2
);
976 vpush_global_sym(&func_old_type
, func
);
981 if (dt
== VT_LDOUBLE
)
982 vtop
->r
= REG_IRET
, vtop
->r2
= REG_IRET
+1;
986 assert (dt
== VT_FLOAT
|| dt
== VT_DOUBLE
);
987 assert (st
== VT_FLOAT
|| st
== VT_DOUBLE
);
989 rd
= get_reg(RC_FLOAT
);
991 EI(0x53, 7, freg(rd
), freg(rs
), 0x21 << 5); // fcvt.d.s RD, RS (dyn rm)
993 EI(0x53, 7, freg(rd
), freg(rs
), (0x20 << 5) | 1); // fcvt.s.d RD, RS
998 ST_FUNC
void ggoto(void)
1000 tcc_error("implement me: %s", __FUNCTION__
);
1003 ST_FUNC
void gen_vla_sp_save(int addr
)
1005 ES(0x23, 3, 8, 2, addr
); // sd sp, fc(s0)
1008 ST_FUNC
void gen_vla_sp_restore(int addr
)
1010 EI(0x03, 3, 2, 8, addr
); // ld sp, fc(s0)
1013 ST_FUNC
void gen_vla_alloc(CType
*type
, int align
)
1015 int rr
= ireg(gv(RC_INT
));
1016 EI(0x13, 0, rr
, rr
, 15); // addi RR, RR, 15
1017 EI(0x13, 7, rr
, rr
, -16); // andi, RR, RR, -16
1018 o(0x33 | (2 << 7) | (2 << 15) | (rr
<< 20) | (0x20 << 25)); //sub sp, sp, rr