[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Tinycc-devel] [PATCH 08/11] riscv64-asm: Add beq, bne, blt, bge, bltu,
From: |
Danny Milosavljevic |
Subject: |
[Tinycc-devel] [PATCH 08/11] riscv64-asm: Add beq, bne, blt, bge, bltu, bgeu |
Date: |
Wed, 7 Apr 2021 13:53:11 +0200 |
---
riscv64-asm.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++
riscv64-tok.h | 9 +++++++
2 files changed, 79 insertions(+)
diff --git a/riscv64-asm.c b/riscv64-asm.c
index 4abbc6e..27ff2cb 100644
--- a/riscv64-asm.c
+++ b/riscv64-asm.c
@@ -503,6 +503,67 @@ static void asm_data_transfer_opcode(TCCState* s1, int
token)
}
}
+static void asm_branch_opcode(TCCState* s1, int token)
+{
+ // Branch (RS1,RS2,IMM); SB-format
+ uint32_t opcode = (0x18 << 2) | 3;
+ uint32_t offset = 0;
+ Operand ops[3];
+ parse_operand(s1, &ops[0]);
+ if (ops[0].type != OP_REG) {
+ expect("register");
+ return;
+ }
+ if (tok == ',')
+ next();
+ else
+ expect("','");
+ parse_operand(s1, &ops[1]);
+ if (ops[1].type != OP_REG) {
+ expect("register");
+ return;
+ }
+ if (tok == ',')
+ next();
+ else
+ expect("','");
+ parse_operand(s1, &ops[2]);
+
+ if (ops[2].type != OP_IM12S) {
+ tcc_error("'%s': Expected third operand that is an immediate value
between 0 and 0xfff", get_tok_str(token, NULL));
+ return;
+ }
+ offset = ops[2].e.v;
+ if (offset & 1) {
+ tcc_error("'%s': Expected third operand that is an even immediate
value", get_tok_str(token, NULL));
+ return;
+ }
+
+ switch (token) {
+ case TOK_ASM_beq:
+ opcode |= 0 << 12;
+ break;
+ case TOK_ASM_bne:
+ opcode |= 1 << 12;
+ break;
+ case TOK_ASM_blt:
+ opcode |= 4 << 12;
+ break;
+ case TOK_ASM_bge:
+ opcode |= 5 << 12;
+ break;
+ case TOK_ASM_bltu:
+ opcode |= 6 << 12;
+ break;
+ case TOK_ASM_bgeu:
+ opcode |= 7 << 12;
+ break;
+ default:
+ expect("known branch instruction");
+ }
+ asm_emit_opcode(opcode | ENCODE_RS1(ops[0].reg) | ENCODE_RS2(ops[1].reg) |
(((offset >> 1) & 0xF) << 8) | (((offset >> 5) & 0x1f) << 25) | (((offset >>
11) & 1) << 7) | (((offset >> 12) & 1) << 31));
+}
+
ST_FUNC void asm_opcode(TCCState *s1, int token)
{
switch (token) {
@@ -589,6 +650,15 @@ ST_FUNC void asm_opcode(TCCState *s1, int token)
asm_data_transfer_opcode(s1, token);
return;
+ case TOK_ASM_beq:
+ case TOK_ASM_bne:
+ case TOK_ASM_blt:
+ case TOK_ASM_bge:
+ case TOK_ASM_bltu:
+ case TOK_ASM_bgeu:
+ asm_branch_opcode(s1, token);
+ return;
+
default:
expect("known instruction");
}
diff --git a/riscv64-tok.h b/riscv64-tok.h
index 1261d95..0b21841 100644
--- a/riscv64-tok.h
+++ b/riscv64-tok.h
@@ -155,6 +155,15 @@
DEF_ASM(sltu)
DEF_ASM(sltiu)
+/* Branch */
+
+ DEF_ASM(beq)
+ DEF_ASM(bne)
+ DEF_ASM(blt)
+ DEF_ASM(bge)
+ DEF_ASM(bltu)
+ DEF_ASM(bgeu)
+
/* Sync */
DEF_ASM(fence)
--
2.29.2
- [Tinycc-devel] [PATCH 00/11] Add RISCV64 inline assembler, Danny Milosavljevic, 2021/04/07
- [Tinycc-devel] [PATCH 02/11] riscv64-asm: Add fence, fence.i, scall, sbreak, ecall, ebreak, wfi, Danny Milosavljevic, 2021/04/07
- [Tinycc-devel] [PATCH 06/11] riscv64-asm: Add add, addi, sub, addw, addd, addiw, addid, subw, subd, xor, xori, or, ori, and, andi, slt, slti, sltu, sltiu, Danny Milosavljevic, 2021/04/07
- [Tinycc-devel] [PATCH 05/11] riscv64-asm: Add sll, slli, srl, srli, sra, srai, sllw, slld, slliw, sllid, srlw, srld, srliw, srlid, sraw, srad, sraiw, sraid, Danny Milosavljevic, 2021/04/07
- [Tinycc-devel] [PATCH 07/11] riscv64-asm: Add lb, lh, lw, lbu, lhu, ld, lwu, sb, sh, sw, sd, Danny Milosavljevic, 2021/04/07
- [Tinycc-devel] [PATCH 10/11] riscv64-asm: Optimize gen_le32, Danny Milosavljevic, 2021/04/07
- [Tinycc-devel] [PATCH 01/11] riscv64-asm: Remove asm_error, Danny Milosavljevic, 2021/04/07
- [Tinycc-devel] [PATCH 03/11] riscv64-asm: Add rdcycle, rdcycleh, rdtime, rdtimeh, rdinstret, rdinstreth, Danny Milosavljevic, 2021/04/07
- [Tinycc-devel] [PATCH 08/11] riscv64-asm: Add beq, bne, blt, bge, bltu, bgeu,
Danny Milosavljevic <=
- [Tinycc-devel] [PATCH 11/11] riscv64-asm: Implement asm_clobber., Danny Milosavljevic, 2021/04/07
- [Tinycc-devel] [PATCH 09/11] riscv64-asm: Implement asm_parse_regvar, Danny Milosavljevic, 2021/04/07
- Re: [Tinycc-devel] [PATCH 09/11] riscv64-asm: Implement asm_parse_regvar, Charles Lohr, 2021/04/12
- Re: [Tinycc-devel] [PATCH 09/11] riscv64-asm: Implement asm_parse_regvar, Danny Milosavljevic, 2021/04/16
- Re: [Tinycc-devel] [PATCH 09/11] riscv64-asm: Implement asm_parse_regvar, Charles Lohr, 2021/04/16
- Re: [Tinycc-devel] [PATCH 09/11] riscv64-asm: Implement asm_parse_regvar, Danny Milosavljevic, 2021/04/23
- Re: [Tinycc-devel] [PATCH 09/11] riscv64-asm: Implement asm_parse_regvar, Christian Jullien, 2021/04/24
- Re: [Tinycc-devel] [PATCH 09/11] riscv64-asm: Implement asm_parse_regvar, Danny Milosavljevic, 2021/04/26
[Tinycc-devel] [PATCH 04/11] riscv64-asm: Add lui, auipc, Danny Milosavljevic, 2021/04/07
- Prev by Date:
[Tinycc-devel] [PATCH 03/11] riscv64-asm: Add rdcycle, rdcycleh, rdtime, rdtimeh, rdinstret, rdinstreth
- Next by Date:
[Tinycc-devel] [PATCH 11/11] riscv64-asm: Implement asm_clobber.
- Previous by thread:
[Tinycc-devel] [PATCH 03/11] riscv64-asm: Add rdcycle, rdcycleh, rdtime, rdtimeh, rdinstret, rdinstreth
- Next by thread:
[Tinycc-devel] [PATCH 11/11] riscv64-asm: Implement asm_clobber.
- Index(es):