From 20d89532202e88e32628aa12a9c1c7e2105c7d3e Mon Sep 17 00:00:00 2001 From: Toni Date: Mon, 9 Mar 2026 15:35:07 +0100 Subject: [PATCH] we can now disassemble riscv64-linux-musl-gcc hello world! --- .gitignore | 2 + riscv64.cc | 158 ++++++++++++++++++++--------------------------------- 2 files changed, 60 insertions(+), 100 deletions(-) diff --git a/.gitignore b/.gitignore index a2dc88c..99d1916 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ /chip8 /mos6502 /riscv64 +/riscv64-linux-musl-cross +/dump \ No newline at end of file diff --git a/riscv64.cc b/riscv64.cc index 0824d10..5307852 100644 --- a/riscv64.cc +++ b/riscv64.cc @@ -86,6 +86,7 @@ enum Op { C_SUB, C_SUBW, C_SW, + C_SWSP, C_XOR, DIV, DIVU, @@ -187,106 +188,57 @@ struct OpDef { Format format; }; -static constexpr std::array OP_TABLE = {{ - {"???", Format::NONE}, - {"add", Format::R}, - {"addi", Format::I}, - {"addiw", Format::I}, - {"addw", Format::R}, - {"amoswap.d", Format::R_ATOMIC}, - {"and", Format::R}, - {"andi", Format::I}, - {"auipc", Format::U}, - {"beq", Format::B}, - {"bge", Format::B}, - {"bgeu", Format::B}, - {"blt", Format::B}, - {"bltu", Format::B}, - {"bne", Format::B}, - {"c.add", Format::CR}, - {"c.addi", Format::CI}, - {"c.addiw", Format::CI}, - {"c.addi16sp", Format::CI}, - {"c.addi4spn", Format::CI}, - {"c.addw", Format::CI}, - {"c.and", Format::CR}, - {"c.andi", Format::CI}, - {"c.beqz", Format::B}, - {"c.bnez", Format::B}, - {"c.ebreak", Format::CR}, - {"c.fldsp", Format::CSS}, - {"c.j", Format::CJ}, - {"c.jalr", Format::CR1}, - {"c.jr", Format::CR1}, - {"c.ld", Format::CL}, - {"c.ldsp", Format::CL}, - {"c.li", Format::CI}, - {"c.lui", Format::CI}, - {"c.lw", Format::CL}, - {"c.mv", Format::CR}, - {"c.or", Format::CR}, - {"c.sd", Format::S}, - {"c.sdsp", Format::CSS}, - {"c.slli", Format::CI}, - {"c.srai", Format::CI}, - {"c.srli", Format::CI}, - {"c.sub", Format::CR}, - {"c.subw", Format::CR}, - {"c.sw", Format::S}, - {"c.xor", Format::CR}, - {"div", Format::R}, - {"divu", Format::R}, - {"divuw", Format::R}, - {"divw", Format::R}, - {"ecall", Format::NONE}, - {"fence", Format::NONE}, - {"fence.tso", Format::NONE}, - {"jal", Format::J}, - {"jalr", Format::I}, - {"lb", Format::I_LOAD}, - {"lbu", Format::I_LOAD}, - {"ld", Format::I_LOAD}, - {"lh", Format::I_LOAD}, - {"lhu", Format::I_LOAD}, - {"lr.d", Format::R_ATOMIC_LR}, - {"lui", Format::U}, - {"lw", Format::I_LOAD}, - {"lwu", Format::I_LOAD}, - {"mul", Format::R}, - {"mulh", Format::R}, - {"mulhu", Format::R}, - {"mulw", Format::R}, - {"or", Format::R}, - {"ori", Format::I}, - {"pause", Format::NONE}, - {"rem", Format::R}, - {"remu", Format::R}, - {"remuw", Format::R}, - {"remw", Format::R}, - {"sb", Format::S}, - {"sc.d", Format::R_ATOMIC}, - {"sd", Format::S}, - {"sh", Format::S}, - {"sll", Format::R}, - {"slli", Format::I_SHIFT}, - {"slliw", Format::I_SHIFT}, - {"sllw", Format::R}, - {"slt", Format::R}, - {"slti", Format::I}, - {"sltiu", Format::I}, - {"sltu", Format::R}, - {"sra", Format::R}, - {"srai", Format::I_SHIFT}, - {"sraiw", Format::I_SHIFT}, - {"sraw", Format::R}, - {"srl", Format::R}, - {"srli", Format::I_SHIFT}, - {"srliw", Format::I_SHIFT}, - {"srlw", Format::R}, - {"sub", Format::R}, - {"subw", Format::R}, - {"sw", Format::S}, - {"xor", Format::R}, +static constexpr std::array OP_TABLE = {{ + {"???", Format::NONE}, {"add", Format::R}, + {"addi", Format::I}, {"addiw", Format::I}, + {"addw", Format::R}, {"amoswap.d", Format::R_ATOMIC}, + {"and", Format::R}, {"andi", Format::I}, + {"auipc", Format::U}, {"beq", Format::B}, + {"bge", Format::B}, {"bgeu", Format::B}, + {"blt", Format::B}, {"bltu", Format::B}, + {"bne", Format::B}, {"c.add", Format::CR}, + {"c.addi", Format::CI}, {"c.addiw", Format::CI}, + {"c.addi16sp", Format::CI}, {"c.addi4spn", Format::CI}, + {"c.addw", Format::CI}, {"c.and", Format::CR}, + {"c.andi", Format::CI}, {"c.beqz", Format::B}, + {"c.bnez", Format::B}, {"c.ebreak", Format::CR}, + {"c.fldsp", Format::CSS}, {"c.j", Format::CJ}, + {"c.jalr", Format::CR1}, {"c.jr", Format::CR1}, + {"c.ld", Format::CL}, {"c.ldsp", Format::CL}, + {"c.li", Format::CI}, {"c.lui", Format::CI}, + {"c.lw", Format::CL}, {"c.mv", Format::CR}, + {"c.or", Format::CR}, {"c.sd", Format::S}, + {"c.sdsp", Format::CSS}, {"c.slli", Format::CI}, + {"c.srai", Format::CI}, {"c.srli", Format::CI}, + {"c.sub", Format::CR}, {"c.subw", Format::CR}, + {"c.sw", Format::S}, {"c.swsp", Format::CSS}, + {"c.xor", Format::CR}, {"div", Format::R}, + {"divu", Format::R}, {"divuw", Format::R}, + {"divw", Format::R}, {"ecall", Format::NONE}, + {"fence", Format::NONE}, {"fence.tso", Format::NONE}, + {"jal", Format::J}, {"jalr", Format::I}, + {"lb", Format::I_LOAD}, {"lbu", Format::I_LOAD}, + {"ld", Format::I_LOAD}, {"lh", Format::I_LOAD}, + {"lhu", Format::I_LOAD}, {"lr.d", Format::R_ATOMIC_LR}, + {"lui", Format::U}, {"lw", Format::I_LOAD}, + {"lwu", Format::I_LOAD}, {"mul", Format::R}, + {"mulh", Format::R}, {"mulhu", Format::R}, + {"mulw", Format::R}, {"or", Format::R}, + {"ori", Format::I}, {"pause", Format::NONE}, + {"rem", Format::R}, {"remu", Format::R}, + {"remuw", Format::R}, {"remw", Format::R}, + {"sb", Format::S}, {"sc.d", Format::R_ATOMIC}, + {"sd", Format::S}, {"sh", Format::S}, + {"sll", Format::R}, {"slli", Format::I_SHIFT}, + {"slliw", Format::I_SHIFT}, {"sllw", Format::R}, + {"slt", Format::R}, {"slti", Format::I}, + {"sltiu", Format::I}, {"sltu", Format::R}, + {"sra", Format::R}, {"srai", Format::I_SHIFT}, + {"sraiw", Format::I_SHIFT}, {"sraw", Format::R}, + {"srl", Format::R}, {"srli", Format::I_SHIFT}, + {"srliw", Format::I_SHIFT}, {"srlw", Format::R}, + {"sub", Format::R}, {"subw", Format::R}, + {"sw", Format::S}, {"xor", Format::R}, {"xori", Format::I}, }}; @@ -1091,6 +1043,12 @@ private: } } }; break; + case 0b110: { + i.rs2 = (raw >> 2) & 0b11111; + i.rs1 = 2; + i.imm = (((raw >> 9) & 0b1111) << 2) | (((raw >> 7) & 0b11) << 6); + i.op = Op::C_SWSP; + }; break; case 0b111: { i.rs2 = (raw >> 2) & 0b11111; i.imm = (((raw >> 10) & 0b111) << 3) | (((raw >> 7) & 0b111) << 6);