History

riscv64-asm.c: add support for calculating addresses of symbols

add some pseudoinstructions riscv64-tok.h: add pseudoinstructions from tables 25.{2,3}

noneofyourbusiness committed 2 years ago

Commit ada17a01 parent b390fee2 files · +83 −5Browse files

Unified · Split

2 files
  1. riscv64-asm.c +41 −1
  2. riscv64-tok.h +42 −4
riscv64-asm.c+41−1
@@ -167,6 +167,7 @@ static void asm_nullary_opcode(TCCState *s1, int token) · 166 unmodified lines
167167 static void parse_operand(TCCState *s1, Operand *op)
168168 {
169169 ExprValue e = {0};
170+ Sym label = {0};
170171 int8_t reg;
171172
172173 op->type = 0;
@@ -190,6 +191,19 @@ static void parse_operand(TCCState *s1, Operand *op) · 17 unmodified lines
190191 if (!op->e.sym) {
191192 if (op->e.v < 0x1000)
192193 op->type = OP_IM12S;
194+ } else if (op->e.sym->type.t & (VT_EXTERN | VT_STATIC)) {
195+ label.type.t = VT_VOID | VT_STATIC;
196+
197+ /* use the medium PIC model: GOT, auipc, lw */
198+ if (op->e.sym->type.t & VT_STATIC)
199+ greloca(cur_text_section, op->e.sym, ind, R_RISCV_PCREL_HI20, 0);
200+ else
201+ greloca(cur_text_section, op->e.sym, ind, R_RISCV_GOT_HI20, 0);
202+ put_extern_sym(&label, cur_text_section, ind, 0);
203+ greloca(cur_text_section, &label, ind+4, R_RISCV_PCREL_LO12_I, 0);
204+
205+ op->type = OP_IM12S;
206+ op->e.v = 0;
193207 } else {
194208 expect("operand");
195209 }
@@ -206,6 +220,7 @@ static void asm_unary_opcode(TCCState *s1, int token) · 10 unmodified lines
206220 opcode |= ENCODE_RD(op.reg);
207221
208222 switch (token) {
223+ /* pseudoinstructions */
209224 case TOK_ASM_rdcycle:
210225 asm_emit_opcode(opcode | (0xC00 << 20));
211226 return;
@@ -224,6 +239,7 @@ static void asm_unary_opcode(TCCState *s1, int token) · 12 unmodified lines
224239 case TOK_ASM_rdinstreth:
225240 asm_emit_opcode(opcode | (0xC82 << 20) | ENCODE_RD(op.reg));
226241 return;
242+ /* C extension */
227243 case TOK_ASM_c_j:
228244 asm_emit_cj(token, 1 | (5 << 13), &op);
229245 return;
@@ -375,6 +391,21 @@ static void asm_binary_opcode(TCCState* s1, int token) · 145 unmodified lines
375391 asm_emit_css(token, 2 | (5 << 13), ops, ops + 1);
376392 return;
377393
394+ /* pseudoinstructions */
395+ /* rd, sym */
396+ case TOK_ASM_la:
397+ /* auipc rd, 0 */
398+ asm_emit_u(token, 3 | (5 << 2), ops, ops + 1);
399+ /* lw rd, rd, 0 */
400+ asm_emit_i(token, 3 | (2 << 12), ops, ops, ops + 1);
401+ return;
402+ case TOK_ASM_lla:
403+ /* auipc rd, 0 */
404+ asm_emit_u(token, 3 | (5 << 2), ops, ops + 1);
405+ /* addi rd, rd, 0 */
406+ asm_emit_i(token, 3 | (4 << 2), ops, ops, ops + 1);
407+ return;
408+
378409 default:
379410 expect("binary instruction");
380411 }
@@ -807,7 +838,6 @@ ST_FUNC void asm_opcode(TCCState *s1, int token) · 426 unmodified lines
807838 case TOK_ASM_hrts:
808839 case TOK_ASM_mrth:
809840 case TOK_ASM_mrts:
810− case TOK_ASM_nop:
811841 case TOK_ASM_wfi:
812842 asm_nullary_opcode(s1, token);
813843 return;
@@ -952,6 +982,16 @@ ST_FUNC void asm_opcode(TCCState *s1, int token) · 138 unmodified lines
952982 asm_ternary_opcode(s1, token);
953983 return;
954984
985+ /* pseudoinstructions */
986+ case TOK_ASM_nop:
987+ asm_nullary_opcode(s1, token);
988+ return;
989+
990+ case TOK_ASM_la:
991+ case TOK_ASM_lla:
992+ asm_binary_opcode(s1, token);
993+ return;
994+
955995 default:
956996 expect("known instruction");
957997 }
riscv64-tok.h+42−4
@@ -244,10 +244,6 @@ · 243 unmodified lines
244244 DEF_ASM(rdinstret)
245245 DEF_ASM(rdinstreth)
246246
247−/* no operation */
248− DEF_ASM(nop)
249− DEF_ASM_WITH_SUFFIX(c, nop)
250−
251247 /* “M” Standard Extension for Integer Multiplication and Division, V2.0 */
252248 DEF_ASM(mul)
253249 DEF_ASM(mulh)
@@ -265,6 +261,7 @@ · 11 unmodified lines
265261 DEF_ASM(remuw)
266262
267263 /* "C" Extension for Compressed Instructions, V2.0 */
264+ DEF_ASM_WITH_SUFFIX(c, nop)
268265 /* Loads */
269266 DEF_ASM_WITH_SUFFIX(c, li)
270267 DEF_ASM_WITH_SUFFIX(c, lw)
@@ -360,8 +357,10 @@ · 89 unmodified lines
360357 DEF_ASM(csrw)
361358 DEF_ASM(csrwi)
362359 DEF_ASM(frcsr)
360+ DEF_ASM(frflags)
363361 DEF_ASM(frrm)
364362 DEF_ASM(fscsr)
363+ DEF_ASM(fsflags)
365364 DEF_ASM(fsrm)
366365
367366 /* Privileged Instructions */
@@ -371,4 +370,43 @@ · 3 unmodified lines
371370 DEF_ASM(hrts)
372371 DEF_ASM(wfi)
373372
373+/* pseudoinstructions */
374+ DEF_ASM(beqz)
375+ DEF_ASM(bgez)
376+ DEF_ASM(bgt)
377+ DEF_ASM(bgtu)
378+ DEF_ASM(bgtz)
379+ DEF_ASM(ble)
380+ DEF_ASM(bleu)
381+ DEF_ASM(blez)
382+ DEF_ASM(bltz)
383+ DEF_ASM(bnez)
384+ DEF_ASM(call)
385+ DEF_ASM_WITH_SUFFIX(fabs, d)
386+ DEF_ASM_WITH_SUFFIX(fabs, s)
387+ DEF_ASM(fld)
388+ DEF_ASM(flw)
389+ DEF_ASM_WITH_SUFFIX(fmv, d)
390+ DEF_ASM_WITH_SUFFIX(fmv, s)
391+ DEF_ASM_WITH_SUFFIX(fneg, d)
392+ DEF_ASM_WITH_SUFFIX(fneg, s)
393+ DEF_ASM(fsd)
394+ DEF_ASM(fsw)
395+ DEF_ASM(j)
396+ DEF_ASM(la)
397+ DEF_ASM(li)
398+ DEF_ASM(lla)
399+ DEF_ASM(mv)
400+ DEF_ASM(neg)
401+ DEF_ASM(negw)
402+ DEF_ASM(nop)
403+ DEF_ASM(not)
404+ DEF_ASM(ret)
405+ DEF_ASM(seqz)
406+ DEF_ASM_WITH_SUFFIX(sext, w)
407+ DEF_ASM(sgtz)
408+ DEF_ASM(sltz)
409+ DEF_ASM(snez)
410+ DEF_ASM(tail)
411+
374412 #undef DEF_ASM_WITH_SUFFIX

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t