Skip to content
This repository has been archived by the owner on Nov 22, 2023. It is now read-only.

Commit

Permalink
riscv64-asm.c: add jal/jalr
Browse files Browse the repository at this point in the history
this implements the base instructions, not the pseudoinstructions

examples
 jal ra, 0
 jalr x0, ra, 0
  • Loading branch information
CorruptedVor committed Aug 11, 2023
1 parent e70fec8 commit d1c1077
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions riscv64-asm.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ typedef struct Operand {
};
} Operand;

static void asm_emit_i(int token, uint32_t opcode, const Operand* rd, const Operand* rs1, const Operand* rs2);

/* Parse a text containing operand and store the result in OP */
static void parse_operand(TCCState *s1, Operand *op)
{
Expand Down Expand Up @@ -215,6 +217,9 @@ static void asm_binary_opcode(TCCState* s1, int token)
case TOK_ASM_auipc:
asm_emit_u(token, (0x05 << 2) | 3, &ops[0], &ops[1]);
return;
case TOK_ASM_jal:
asm_emit_u(token, 0x6f, ops, ops + 1);
return;
default:
expect("binary instruction");
}
Expand Down Expand Up @@ -399,6 +404,12 @@ static void asm_data_processing_opcode(TCCState* s1, int token)
case TOK_ASM_sltiu:
asm_emit_i(token, (0x4 << 2) | 3 | (3 << 12), &ops[0], &ops[1], &ops[2]);
return;

/* indirect jump (RD, RS1, IMM); I-format */
case TOK_ASM_jalr:
asm_emit_i(token, 0x67 | (0 << 12), ops, ops + 1, ops + 2);
return;

default:
expect("known data processing instruction");
}
Expand Down Expand Up @@ -588,6 +599,7 @@ ST_FUNC void asm_opcode(TCCState *s1, int token)

case TOK_ASM_lui:
case TOK_ASM_auipc:
case TOK_ASM_jal:
asm_binary_opcode(s1, token);
return;

Expand Down Expand Up @@ -628,6 +640,7 @@ ST_FUNC void asm_opcode(TCCState *s1, int token)
case TOK_ASM_slti:
case TOK_ASM_sltu:
case TOK_ASM_sltiu:
case TOK_ASM_jalr:
asm_data_processing_opcode(s1, token);
return;

Expand Down

0 comments on commit d1c1077

Please sign in to comment.