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
23 #define LDOUBLE_SIZE 16
24 #define LDOUBLE_ALIGN 16
28 #define CHAR_IS_UNSIGNED
36 ST_DATA
const int reg_classes
[NB_REGS
] = {
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
)
66 static int is_freg(int r
)
68 return r
>= 8 && r
< 16;
71 ST_FUNC
void o(unsigned int c
)
76 if (ind1
> cur_text_section
->data_allocated
)
77 section_realloc(cur_text_section
, ind1
);
78 write32le(cur_text_section
->data
+ ind
, c
);
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_
)
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
117 ST_FUNC
void load(int r
, SValue
*sv
)
120 int v
= fr
& VT_VALMASK
;
125 int bt
= sv
->type
.t
& VT_BTYPE
;
126 int align
, size
= type_size(&sv
->type
, &align
);
128 if (((unsigned)fc
+ (1 << 11)) >> 12)
129 tcc_error("unimp: load(large local ofs) (0x%x)", fc
);
131 tcc_error("unimp: load-local(float)");
132 else if (bt
== VT_FUNC
)
134 func3
= size
== 1 ? 0 : size
== 2 ? 1 : size
== 4 ? 2 : 3;
135 if (size
< 8 && (sv
->type
.t
& VT_UNSIGNED
))
137 EI(0x03, func3
, rr
, 8, fc
); // l[bhwd][u] RR, fc(s0)
139 tcc_error("unimp: load(non-local lval)");
141 } else if (v
== VT_CONST
) {
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
);
149 greloca(cur_text_section
, vtop
->sym
, ind
,
150 R_RISCV_PCREL_HI20
, fc
);
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);
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
) {
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
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
176 tcc_error("unimp: load(non-const)");
179 ST_FUNC
void store(int r
, SValue
*sv
)
181 int fr
= sv
->r
& VT_VALMASK
;
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
);
191 tcc_error("unimp: store(float)");
193 tcc_error("unimp: store(struct)");
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)
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)
212 tcc_error("unimp: indirect call");
216 ST_FUNC
void gfunc_call(int nb_args
)
218 int i
, align
, size
, aireg
;
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
);
226 tcc_error("unimp: too many register args");
228 gv(RC_R(nb_args
- 1 - aireg
));
237 static int func_sub_sp_offset
;
239 ST_FUNC
void gfunc_prolog(CType
*func_type
)
241 int i
, addr
, align
, size
;
247 sym
= func_type
->ref
;
249 loc
= -16; // for ra and s0
250 func_sub_sp_offset
= ind
;
252 if (sym
->f
.func_type
== FUNC_ELLIPSIS
) {
253 tcc_error("unimp: vararg prologue");
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");
265 /* define parameters */
266 while ((sym
= sym
->next
) != NULL
) {
268 size
= type_size(type
, &align
);
269 if (size
> 2 * XLEN
) {
271 addr
= (addr
+ align
- 1) & -align
;
278 if (regcount
+ (is_float(type
->t
) ? afreg
: aireg
) >= 8)
282 for (i
= 0; i
< regcount
; i
++) {
283 if (is_float(type
->t
)) {
284 tcc_error("unimp: float args");
286 ES(0x23, 3, 8, 10 + aireg
, loc
+ i
*8); // sd aX, loc(s0) // XXX
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
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
))
319 ST_FUNC
void gfunc_epilog(void)
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
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
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__
);
352 tcc_error("alignment of code section not multiple of 4");
355 // Generate forward branch to label:
356 ST_FUNC
int gjmp(int t
)
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
)
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
386 ST_FUNC
int gjmp_append(int n
, int t
)
389 /* insert jump list n into t */
392 while ((n2
= read32le(p
= cur_text_section
->data
+ n1
)))
400 static void gen_opil(int op
, int ll
)
404 /* XXX We could special-case some constant args. */
406 a
= ireg(vtop
[-1].r
);
428 tcc_error("implement me: %s(%s)", __FUNCTION__
, get_tok_str(op
, NULL
));
431 o(0x33 | (d
<< 7) | (a
<< 15) | (b
<< 20)); // add d, a, b
442 if (op
& 1) { // remove [U]GE,GT
446 if ((op
& 7) == 6) { // [U]LE
447 int t
= a
; a
= b
; b
= t
;
450 o(0x33 | (d
<< 7) | (a
<< 15) | (b
<< 20) | (((op
> TOK_UGT
) ? 2 : 3) << 12)); // slt[u] d, a, b
452 EI(0x13, 4, d
, d
, 1); // xori d, d, 1
458 o(0x33 | (d
<< 7) | (a
<< 15) | (b
<< 20) | (0x20 << 25)); // sub d, a, b
460 o(0x33 | (3 << 12) | (d
<< 7) | (0 << 15) | (d
<< 20)); // sltu d, x0, d == snez d,d
462 EI(0x13, 3, d
, d
, 1); // sltiu d, d, 1 == seqz d,d
469 ST_FUNC
void gen_opi(int op
)
474 ST_FUNC
void gen_opl(int op
)
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__
);