move to C++23 for that sweet std::println
This commit is contained in:
4
.clangd
4
.clangd
@@ -1,4 +1,4 @@
|
|||||||
If:
|
If:
|
||||||
Language: c++
|
PathMatch: '.*\.cc$'
|
||||||
CompileFlags:
|
CompileFlags:
|
||||||
Add: [-std=c++20]
|
Add: [-std=c++23]
|
||||||
|
|||||||
2
build.sh
2
build.sh
@@ -17,7 +17,7 @@ if command -v pkg-config >/dev/null; then
|
|||||||
LIBELF_FLAGS=$(pkg-config --cflags --libs libelf 2>/dev/null)
|
LIBELF_FLAGS=$(pkg-config --cflags --libs libelf 2>/dev/null)
|
||||||
if [ $? -eq 0 ]; then
|
if [ $? -eq 0 ]; then
|
||||||
echo "building riscv64..."
|
echo "building riscv64..."
|
||||||
c++ -std=c++20 $COMMON -o riscv64 riscv64.cc $LIBELF_FLAGS
|
c++ -std=c++23 $COMMON -o riscv64 riscv64.cc $LIBELF_FLAGS
|
||||||
else
|
else
|
||||||
echo "libelf not found - skipping riscv64..."
|
echo "libelf not found - skipping riscv64..."
|
||||||
fi
|
fi
|
||||||
|
|||||||
337
riscv64.cc
337
riscv64.cc
@@ -3,12 +3,12 @@
|
|||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <bit>
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <gelf.h>
|
#include <gelf.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <libelf.h>
|
#include <libelf.h>
|
||||||
|
#include <print>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
// TODO: dont use macros now that we're in an actual language
|
// TODO: dont use macros now that we're in an actual language
|
||||||
@@ -126,19 +126,19 @@ public:
|
|||||||
PARSE_B_INS(ins);
|
PARSE_B_INS(ins);
|
||||||
|
|
||||||
if (funct3 == 0b000) {
|
if (funct3 == 0b000) {
|
||||||
printf("beq %s, %s, %d\n", REGS[rs1], REGS[rs2], imm);
|
std::println("beq {}, {}, {}", REGS[rs1], REGS[rs2], imm);
|
||||||
} else if (funct3 == 0b001) {
|
} else if (funct3 == 0b001) {
|
||||||
printf("bne %s, %s, %d\n", REGS[rs1], REGS[rs2], imm);
|
std::println("bne {}, {}, {}", REGS[rs1], REGS[rs2], imm);
|
||||||
} else if (funct3 == 0b100) {
|
} else if (funct3 == 0b100) {
|
||||||
printf("blt %s, %s, %d\n", REGS[rs1], REGS[rs2], imm);
|
std::println("blt {}, {}, {}", REGS[rs1], REGS[rs2], imm);
|
||||||
} else if (funct3 == 0b101) {
|
} else if (funct3 == 0b101) {
|
||||||
printf("bge %s, %s, %d\n", REGS[rs1], REGS[rs2], imm);
|
std::println("bge {}, {}, {}", REGS[rs1], REGS[rs2], imm);
|
||||||
} else if (funct3 == 0b110) {
|
} else if (funct3 == 0b110) {
|
||||||
printf("bltu %s, %s, %d\n", REGS[rs1], REGS[rs2], imm);
|
std::println("bltu {}, {}, {}", REGS[rs1], REGS[rs2], imm);
|
||||||
} else if (funct3 == 0b111) {
|
} else if (funct3 == 0b111) {
|
||||||
printf("bgeu %s, %s, %d\n", REGS[rs1], REGS[rs2], imm);
|
std::println("bgeu {}, {}, {}", REGS[rs1], REGS[rs2], imm);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "B-type: unrecognized funct3: %03b\n", funct3);
|
std::println(stderr, "B-type: unrecognized funct3: {:03b}", funct3);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}; break;
|
}; break;
|
||||||
@@ -146,44 +146,46 @@ public:
|
|||||||
PARSE_I_INS(ins);
|
PARSE_I_INS(ins);
|
||||||
|
|
||||||
if (funct3 == 0b000) {
|
if (funct3 == 0b000) {
|
||||||
printf("addi %s, %s, %d\n", REGS[rd], REGS[rs1], imm);
|
std::println("addi {}, {}, {}", REGS[rd], REGS[rs1], imm);
|
||||||
} else if (funct3 == 0b001) {
|
} else if (funct3 == 0b001) {
|
||||||
u32 shamt = (ins >> 20) & 0b111111;
|
u32 shamt = (ins >> 20) & 0b111111;
|
||||||
u32 funct6 = (ins >> 26) & 0b111111;
|
u32 funct6 = (ins >> 26) & 0b111111;
|
||||||
|
|
||||||
if (funct6 == 0b000000) {
|
if (funct6 == 0b000000) {
|
||||||
printf("slli %s, %s, %d\n", REGS[rd], REGS[rs1], shamt);
|
std::println("slli {}, {}, {}", REGS[rd], REGS[rs1], shamt);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "I-type 1: funct3=001: unrecognized funct6: %b\n",
|
std::println(stderr,
|
||||||
funct6);
|
"I-type 1: funct3=001: unrecognized funct6: {:b}",
|
||||||
|
funct6);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
} else if (funct3 == 0b010) {
|
} else if (funct3 == 0b010) {
|
||||||
printf("slti %s, %s, %d\n", REGS[rd], REGS[rs1], imm);
|
std::println("slti {}, {}, {}", REGS[rd], REGS[rs1], imm);
|
||||||
} else if (funct3 == 0b011) {
|
} else if (funct3 == 0b011) {
|
||||||
printf("sltiu %s, %s, %d\n", REGS[rd], REGS[rs1], imm);
|
std::println("sltiu {}, {}, {}", REGS[rd], REGS[rs1], imm);
|
||||||
} else if (funct3 == 0b100) {
|
} else if (funct3 == 0b100) {
|
||||||
printf("xori %s, %s, %d\n", REGS[rd], REGS[rs1], imm);
|
std::println("xori {}, {}, {}", REGS[rd], REGS[rs1], imm);
|
||||||
} else if (funct3 == 0b101) {
|
} else if (funct3 == 0b101) {
|
||||||
// of course this one just has to be different
|
// of course this one just has to be different
|
||||||
u32 shamt = (ins >> 20) & 0b111111;
|
u32 shamt = (ins >> 20) & 0b111111;
|
||||||
u32 funct6 = (ins >> 26) & 0b111111;
|
u32 funct6 = (ins >> 26) & 0b111111;
|
||||||
|
|
||||||
if (funct6 == 0b000000) {
|
if (funct6 == 0b000000) {
|
||||||
printf("srli %s, %s, %d\n", REGS[rd], REGS[rs1], shamt);
|
std::println("srli {}, {}, {}", REGS[rd], REGS[rs1], shamt);
|
||||||
} else if (funct6 == 0b010000) {
|
} else if (funct6 == 0b010000) {
|
||||||
printf("srai %s, %s, %d\n", REGS[rd], REGS[rs1], shamt);
|
std::println("srai {}, {}, {}", REGS[rd], REGS[rs1], shamt);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "I-type 1: funct3=101: unrecognized funct6: %b\n",
|
std::println(stderr,
|
||||||
funct6);
|
"I-type 1: funct3=101: unrecognized funct6: {:b}",
|
||||||
|
funct6);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
} else if (funct3 == 0b110) {
|
} else if (funct3 == 0b110) {
|
||||||
printf("ori %s, %s, %d\n", REGS[rd], REGS[rs1], imm);
|
std::println("ori {}, {}, {}", REGS[rd], REGS[rs1], imm);
|
||||||
} else if (funct3 == 0b111) {
|
} else if (funct3 == 0b111) {
|
||||||
printf("andi %s, %s, %d\n", REGS[rd], REGS[rs1], imm);
|
std::println("andi {}, {}, {}", REGS[rd], REGS[rs1], imm);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "I-type 1: unrecognized funct3: %03b\n", funct3);
|
std::println(stderr, "I-type 1: unrecognized funct3: {:03b}", funct3);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}; break;
|
}; break;
|
||||||
@@ -191,175 +193,186 @@ public:
|
|||||||
PARSE_I_INS(ins);
|
PARSE_I_INS(ins);
|
||||||
|
|
||||||
if (funct3 == 0b000) {
|
if (funct3 == 0b000) {
|
||||||
printf("lb %s, %d(%s)\n", REGS[rd], imm, REGS[rs1]);
|
std::println("lb {}, {}({})", REGS[rd], imm, REGS[rs1]);
|
||||||
} else if (funct3 == 0b001) {
|
} else if (funct3 == 0b001) {
|
||||||
printf("lh %s, %d(%s)\n", REGS[rd], imm, REGS[rs1]);
|
std::println("lh {}, {}({})", REGS[rd], imm, REGS[rs1]);
|
||||||
} else if (funct3 == 0b010) {
|
} else if (funct3 == 0b010) {
|
||||||
printf("lw %s, %d(%s)\n", REGS[rd], imm, REGS[rs1]);
|
std::println("lw {}, {}({})", REGS[rd], imm, REGS[rs1]);
|
||||||
} else if (funct3 == 0b011) {
|
} else if (funct3 == 0b011) {
|
||||||
printf("ld %s, %d(%s)\n", REGS[rd], imm, REGS[rs1]);
|
std::println("ld {}, {}({})", REGS[rd], imm, REGS[rs1]);
|
||||||
} else if (funct3 == 0b100) {
|
} else if (funct3 == 0b100) {
|
||||||
printf("lbu %s, %d(%s)\n", REGS[rd], imm, REGS[rs1]);
|
std::println("lbu {}, {}({})", REGS[rd], imm, REGS[rs1]);
|
||||||
} else if (funct3 == 0b101) {
|
} else if (funct3 == 0b101) {
|
||||||
printf("lhu %s, %d(%s)\n", REGS[rd], imm, REGS[rs1]);
|
std::println("lhu {}, {}({})", REGS[rd], imm, REGS[rs1]);
|
||||||
} else if (funct3 == 0b110) {
|
} else if (funct3 == 0b110) {
|
||||||
printf("lwu %s, %d(%s)\n", REGS[rd], imm, REGS[rs1]);
|
std::println("lwu {}, {}({})", REGS[rd], imm, REGS[rs1]);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "I-type 2: unrecognized funct3: %03b\n", funct3);
|
std::println(stderr, "I-type 2: unrecognized funct3: {:03b}", funct3);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}; break;
|
}; break;
|
||||||
case 0b1100111: {
|
case 0b1100111: {
|
||||||
PARSE_I_INS(ins);
|
PARSE_I_INS(ins);
|
||||||
printf("jalr %s, %s, %d\n", REGS[rd], REGS[rs1], imm);
|
std::println("jalr {}, {}, {}", REGS[rd], REGS[rs1], imm);
|
||||||
}; break;
|
}; break;
|
||||||
case 0b1110011: {
|
case 0b1110011: {
|
||||||
PARSE_I_INS(ins);
|
PARSE_I_INS(ins);
|
||||||
|
|
||||||
if (funct3 == 0b000) {
|
if (funct3 == 0b000) {
|
||||||
if (imm == 0) {
|
if (imm == 0) {
|
||||||
printf("ecall\n");
|
std::println("ecall");
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "I-type 4: funct3=000 unrecognized imm: %b\n", imm);
|
std::println(stderr, "I-type 4: funct3=000 unrecognized imm: {:b}",
|
||||||
|
imm);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "I-type 4: unrecognized funct3: %03b\n", funct3);
|
std::println(stderr, "I-type 4: unrecognized funct3: {:03b}", funct3);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}; break;
|
}; break;
|
||||||
case 0b1101111: {
|
case 0b1101111: {
|
||||||
PARSE_J_INS(ins);
|
PARSE_J_INS(ins);
|
||||||
printf("jal %s, %d\n", REGS[rd], imm);
|
std::println("jal {}, {}", REGS[rd], imm);
|
||||||
}; break;
|
}; break;
|
||||||
case 0b0110011: {
|
case 0b0110011: {
|
||||||
PARSE_R_INS(ins);
|
PARSE_R_INS(ins);
|
||||||
if (funct3 == 0b000) {
|
if (funct3 == 0b000) {
|
||||||
if (funct7 == 0b0000000) {
|
if (funct7 == 0b0000000) {
|
||||||
printf("add %s, %s, %s\n", REGS[rd], REGS[rs1], REGS[rs2]);
|
std::println("add {}, {}, {}", REGS[rd], REGS[rs1], REGS[rs2]);
|
||||||
} else if (funct7 == 0b0100000) {
|
} else if (funct7 == 0b0100000) {
|
||||||
printf("sub %s, %s, %s\n", REGS[rd], REGS[rs1], REGS[rs2]);
|
std::println("sub {}, {}, {}", REGS[rd], REGS[rs1], REGS[rs2]);
|
||||||
} else if (funct7 == 0b0000001) {
|
} else if (funct7 == 0b0000001) {
|
||||||
printf("mul %s, %s, %s\n", REGS[rd], REGS[rs1], REGS[rs2]);
|
std::println("mul {}, {}, {}", REGS[rd], REGS[rs1], REGS[rs2]);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "R-type 1: funct3=0b000: unrecognized funct7: %b\n",
|
std::println(stderr,
|
||||||
funct7);
|
"R-type 1: funct3=0b000: unrecognized funct7: {:b}",
|
||||||
|
funct7);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
} else if (funct3 == 0b001) {
|
} else if (funct3 == 0b001) {
|
||||||
if (funct7 == 0b0000000) {
|
if (funct7 == 0b0000000) {
|
||||||
printf("sll %s, %s, %s\n", REGS[rd], REGS[rs1], REGS[rs2]);
|
std::println("sll {}, {}, {}", REGS[rd], REGS[rs1], REGS[rs2]);
|
||||||
} else if (funct7 == 0b0000001) {
|
} else if (funct7 == 0b0000001) {
|
||||||
printf("mulh %s, %s, %s\n", REGS[rd], REGS[rs1], REGS[rs2]);
|
std::println("mulh {}, {}, {}", REGS[rd], REGS[rs1], REGS[rs2]);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "R-type 1: funct3=0b001: unrecognized funct7: %b\n",
|
std::println(stderr,
|
||||||
funct7);
|
"R-type 1: funct3=0b001: unrecognized funct7: {:b}",
|
||||||
|
funct7);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
} else if (funct3 == 0b010) {
|
} else if (funct3 == 0b010) {
|
||||||
if (funct7 == 0b0000000) {
|
if (funct7 == 0b0000000) {
|
||||||
printf("slt %s, %s, %s\n", REGS[rd], REGS[rs1], REGS[rs2]);
|
std::println("slt {}, {}, {}", REGS[rd], REGS[rs1], REGS[rs2]);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "R-type 1: funct3=0b010: unrecognized funct7: %b\n",
|
std::println(stderr,
|
||||||
funct7);
|
"R-type 1: funct3=0b010: unrecognized funct7: {:b}",
|
||||||
|
funct7);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
} else if (funct3 == 0b011) {
|
} else if (funct3 == 0b011) {
|
||||||
if (funct7 == 0b0000000) {
|
if (funct7 == 0b0000000) {
|
||||||
printf("sltu %s, %s, %s\n", REGS[rd], REGS[rs1], REGS[rs2]);
|
std::println("sltu {}, {}, {}", REGS[rd], REGS[rs1], REGS[rs2]);
|
||||||
} else if (funct7 == 0b0000001) {
|
} else if (funct7 == 0b0000001) {
|
||||||
printf("mulhu %s, %s, %s\n", REGS[rd], REGS[rs1], REGS[rs2]);
|
std::println("mulhu {}, {}, {}", REGS[rd], REGS[rs1], REGS[rs2]);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "R-type 1: funct3=0b011: unrecognized funct7: %b\n",
|
std::println(stderr,
|
||||||
funct7);
|
"R-type 1: funct3=0b011: unrecognized funct7: {:b}",
|
||||||
|
funct7);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
} else if (funct3 == 0b100) {
|
} else if (funct3 == 0b100) {
|
||||||
if (funct7 == 0b0000000) {
|
if (funct7 == 0b0000000) {
|
||||||
printf("xor %s, %s, %s\n", REGS[rd], REGS[rs1], REGS[rs2]);
|
std::println("xor {}, {}, {}", REGS[rd], REGS[rs1], REGS[rs2]);
|
||||||
} else if (funct7 == 0b0000001) {
|
} else if (funct7 == 0b0000001) {
|
||||||
printf("div %s, %s, %s\n", REGS[rd], REGS[rs1], REGS[rs2]);
|
std::println("div {}, {}, {}", REGS[rd], REGS[rs1], REGS[rs2]);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "R-type 1: funct3=0b100: unrecognized funct7: %b\n",
|
std::println(stderr,
|
||||||
funct7);
|
"R-type 1: funct3=0b100: unrecognized funct7: {:b}",
|
||||||
|
funct7);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
} else if (funct3 == 0b101) {
|
} else if (funct3 == 0b101) {
|
||||||
if (funct7 == 0b0000000) {
|
if (funct7 == 0b0000000) {
|
||||||
printf("srl %s, %s, %s\n", REGS[rd], REGS[rs1], REGS[rs2]);
|
std::println("srl {}, {}, {}", REGS[rd], REGS[rs1], REGS[rs2]);
|
||||||
} else if (funct7 == 0b0000001) {
|
} else if (funct7 == 0b0000001) {
|
||||||
printf("divu %s, %s, %s\n", REGS[rd], REGS[rs1], REGS[rs2]);
|
std::println("divu {}, {}, {}", REGS[rd], REGS[rs1], REGS[rs2]);
|
||||||
} else if (funct7 == 0b0100000) {
|
} else if (funct7 == 0b0100000) {
|
||||||
printf("sra %s, %s, %s\n", REGS[rd], REGS[rs1], REGS[rs2]);
|
std::println("sra {}, {}, {}", REGS[rd], REGS[rs1], REGS[rs2]);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "R-type 1: funct3=0b101: unrecognized funct7: %b\n",
|
std::println(stderr,
|
||||||
funct7);
|
"R-type 1: funct3=0b101: unrecognized funct7: {:b}",
|
||||||
|
funct7);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
} else if (funct3 == 0b110) {
|
} else if (funct3 == 0b110) {
|
||||||
if (funct7 == 0b0000001) {
|
if (funct7 == 0b0000001) {
|
||||||
printf("rem %s, %s, %s\n", REGS[rd], REGS[rs1], REGS[rs2]);
|
std::println("rem {}, {}, {}", REGS[rd], REGS[rs1], REGS[rs2]);
|
||||||
} else if (funct7 == 0b0000000) {
|
} else if (funct7 == 0b0000000) {
|
||||||
printf("or %s, %s, %s\n", REGS[rd], REGS[rs1], REGS[rs2]);
|
std::println("or {}, {}, {}", REGS[rd], REGS[rs1], REGS[rs2]);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "R-type 1: funct3=0b110: unrecognized funct7: %b\n",
|
std::println(stderr,
|
||||||
funct7);
|
"R-type 1: funct3=0b110: unrecognized funct7: {:b}",
|
||||||
|
funct7);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
} else if (funct3 == 0b111) {
|
} else if (funct3 == 0b111) {
|
||||||
if (funct7 == 0b000) {
|
if (funct7 == 0b000) {
|
||||||
printf("and %s, %s, %s\n", REGS[rd], REGS[rs1], REGS[rs2]);
|
std::println("and {}, {}, {}", REGS[rd], REGS[rs1], REGS[rs2]);
|
||||||
} else if (funct7 == 0b001) {
|
} else if (funct7 == 0b001) {
|
||||||
printf("remu %s, %s, %s\n", REGS[rd], REGS[rs1], REGS[rs2]);
|
std::println("remu {}, {}, {}", REGS[rd], REGS[rs1], REGS[rs2]);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "R-type 1: funct3=0b111: unrecognized funct7: %b\n",
|
std::println(stderr,
|
||||||
funct7);
|
"R-type 1: funct3=0b111: unrecognized funct7: {:b}",
|
||||||
|
funct7);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "R-type 1: unrecognized funct3: %03b\n", funct3);
|
std::println(stderr, "R-type 1: unrecognized funct3: {:03b}", funct3);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}; break;
|
}; break;
|
||||||
case 0b0101111:
|
case 0b0101111:
|
||||||
fprintf(stderr, "A extension not implemented yet.\n");
|
std::println(stderr, "A extension not implemented yet.");
|
||||||
exit(1);
|
exit(1);
|
||||||
case 0b0111011: {
|
case 0b0111011: {
|
||||||
PARSE_R_INS(ins);
|
PARSE_R_INS(ins);
|
||||||
if (funct3 == 0b000) {
|
if (funct3 == 0b000) {
|
||||||
if (funct7 == 0b0000000) {
|
if (funct7 == 0b0000000) {
|
||||||
printf("addw %s, %s, %s\n", REGS[rd], REGS[rs1], REGS[rs2]);
|
std::println("addw {}, {}, {}", REGS[rd], REGS[rs1], REGS[rs2]);
|
||||||
} else if (funct7 == 0b0100000) {
|
} else if (funct7 == 0b0100000) {
|
||||||
printf("subw %s, %s, %s\n", REGS[rd], REGS[rs1], REGS[rs2]);
|
std::println("subw {}, {}, {}", REGS[rd], REGS[rs1], REGS[rs2]);
|
||||||
} else if (funct7 == 0b0000001) {
|
} else if (funct7 == 0b0000001) {
|
||||||
printf("mulw %s, %s, %s\n", REGS[rd], REGS[rs1], REGS[rs2]);
|
std::println("mulw {}, {}, {}", REGS[rd], REGS[rs1], REGS[rs2]);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "R-type 3: funct3=000: unrecognized funct7: %b\n",
|
std::println(stderr,
|
||||||
funct7);
|
"R-type 3: funct3=000: unrecognized funct7: {:b}",
|
||||||
|
funct7);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
} else if (funct3 == 0b001) {
|
} else if (funct3 == 0b001) {
|
||||||
printf("sllw %s, %s, %s\n", REGS[rd], REGS[rs1], REGS[rs2]);
|
std::println("sllw {}, {}, {}", REGS[rd], REGS[rs1], REGS[rs2]);
|
||||||
} else if (funct3 == 0b100) {
|
} else if (funct3 == 0b100) {
|
||||||
printf("divw %s, %s, %s\n", REGS[rd], REGS[rs1], REGS[rs2]);
|
std::println("divw {}, {}, {}", REGS[rd], REGS[rs1], REGS[rs2]);
|
||||||
} else if (funct3 == 0b101) {
|
} else if (funct3 == 0b101) {
|
||||||
if (funct7 == 0b0000000) {
|
if (funct7 == 0b0000000) {
|
||||||
printf("srlw %s, %s, %s\n", REGS[rd], REGS[rs1], REGS[rs2]);
|
std::println("srlw {}, {}, {}", REGS[rd], REGS[rs1], REGS[rs2]);
|
||||||
} else if (funct7 == 0b0100000) {
|
} else if (funct7 == 0b0100000) {
|
||||||
printf("sraw %s, %s, %s\n", REGS[rd], REGS[rs1], REGS[rs2]);
|
std::println("sraw {}, {}, {}", REGS[rd], REGS[rs1], REGS[rs2]);
|
||||||
} else if (funct7 == 0b0000001) {
|
} else if (funct7 == 0b0000001) {
|
||||||
printf("divuw %s, %s, %s\n", REGS[rd], REGS[rs1], REGS[rs2]);
|
std::println("divuw {}, {}, {}", REGS[rd], REGS[rs1], REGS[rs2]);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "R-type 3: funct3=101: unrecognized funct7: %b\n",
|
std::println(stderr,
|
||||||
funct7);
|
"R-type 3: funct3=101: unrecognized funct7: {:b}",
|
||||||
|
funct7);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
} else if (funct3 == 0b110) {
|
} else if (funct3 == 0b110) {
|
||||||
printf("remw %s, %s, %s\n", REGS[rd], REGS[rs1], REGS[rs2]);
|
std::println("remw {}, {}, {}", REGS[rd], REGS[rs1], REGS[rs2]);
|
||||||
} else if (funct3 == 0b111) {
|
} else if (funct3 == 0b111) {
|
||||||
printf("remuw %s, %s, %s\n", REGS[rd], REGS[rs1], REGS[rs2]);
|
std::println("remuw {}, {}, {}", REGS[rd], REGS[rs1], REGS[rs2]);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "R-type 3: unrecognized funct3: %03b\n", funct3);
|
std::println(stderr, "R-type 3: unrecognized funct3: {:03b}", funct3);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}; break;
|
}; break;
|
||||||
@@ -367,66 +380,72 @@ public:
|
|||||||
u8 funct3 = (ins >> 12) & 0b111;
|
u8 funct3 = (ins >> 12) & 0b111;
|
||||||
if (funct3 == 0b000) {
|
if (funct3 == 0b000) {
|
||||||
PARSE_I_INS(ins);
|
PARSE_I_INS(ins);
|
||||||
printf("addiw %s, %s, %d\n", REGS[rd], REGS[rs1], imm);
|
std::println("addiw {}, {}, {}", REGS[rd], REGS[rs1], imm);
|
||||||
} else if (funct3 == 0b001) {
|
} else if (funct3 == 0b001) {
|
||||||
PARSE_R_INS(ins);
|
PARSE_R_INS(ins);
|
||||||
printf("slliw %s, %s, %d\n", REGS[rd], REGS[rs1], rs2);
|
std::println("slliw {}, {}, {}", REGS[rd], REGS[rs1], rs2);
|
||||||
} else if (funct3 == 0b101) {
|
} else if (funct3 == 0b101) {
|
||||||
PARSE_R_INS(ins);
|
PARSE_R_INS(ins);
|
||||||
if (funct7 == 0b0000000) {
|
if (funct7 == 0b0000000) {
|
||||||
printf("srliw %s, %s, %d\n", REGS[rd], REGS[rs1], rs2);
|
std::println("srliw {}, {}, {}", REGS[rd], REGS[rs1], rs2);
|
||||||
} else if (funct7 == 0b0100000) {
|
} else if (funct7 == 0b0100000) {
|
||||||
printf("sraiw %s, %s, %d\n", REGS[rd], REGS[rs1], rs2);
|
std::println("sraiw {}, {}, {}", REGS[rd], REGS[rs1], rs2);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "R-type 4: funct3=101: unrecognized funct7: %b\n",
|
std::println(stderr,
|
||||||
funct7);
|
"R-type 4: funct3=101: unrecognized funct7: {:b}",
|
||||||
|
funct7);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "R-type 4: unrecognized funct3: %03b\n", funct3);
|
std::println(stderr, "R-type 4: unrecognized funct3: {:03b}", funct3);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}; break;
|
}; break;
|
||||||
|
case 0b0000111: {
|
||||||
|
std::println(stderr, "F extension not implemented yet.");
|
||||||
|
exit(1);
|
||||||
|
}; break;
|
||||||
case 0b0100111: {
|
case 0b0100111: {
|
||||||
fprintf(stderr, "F extension not implemented yet.\n");
|
std::println(stderr, "F extension not implemented yet.");
|
||||||
exit(1);
|
exit(1);
|
||||||
}; break;
|
}; break;
|
||||||
case 0b0100011: {
|
case 0b0100011: {
|
||||||
PARSE_S_INS(ins);
|
PARSE_S_INS(ins);
|
||||||
|
|
||||||
if (funct3 == 0b000) {
|
if (funct3 == 0b000) {
|
||||||
printf("sb %s, %d(%s)\n", REGS[rs2], imm, REGS[rs1]);
|
std::println("sb {}, {}({})", REGS[rs2], imm, REGS[rs1]);
|
||||||
} else if (funct3 == 0b001) {
|
} else if (funct3 == 0b001) {
|
||||||
printf("sh %s, %d(%s)\n", REGS[rs2], imm, REGS[rs1]);
|
std::println("sh {}, {}({})", REGS[rs2], imm, REGS[rs1]);
|
||||||
} else if (funct3 == 0b010) {
|
} else if (funct3 == 0b010) {
|
||||||
printf("sw %s, %d(%s)\n", REGS[rs2], imm, REGS[rs1]);
|
std::println("sw {}, {}({})", REGS[rs2], imm, REGS[rs1]);
|
||||||
} else if (funct3 == 0b011) {
|
} else if (funct3 == 0b011) {
|
||||||
printf("sd %s, %d(%s)\n", REGS[rs2], imm, REGS[rs1]);
|
std::println("sd {}, {}({})", REGS[rs2], imm, REGS[rs1]);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "S-type: unrecognized funct3: %03b\n", funct3);
|
std::println(stderr, "S-type: unrecognized funct3: {:03b}", funct3);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}; break;
|
}; break;
|
||||||
case 0b0110111: {
|
case 0b0110111: {
|
||||||
PARSE_U_INS(ins);
|
PARSE_U_INS(ins);
|
||||||
printf("lui %s, %d\n", REGS[rd], imm);
|
std::println("lui {}, {}", REGS[rd], imm);
|
||||||
}; break;
|
}; break;
|
||||||
case 0b0010111: {
|
case 0b0010111: {
|
||||||
PARSE_U_INS(ins);
|
PARSE_U_INS(ins);
|
||||||
printf("auipc %s, %d\n", REGS[rd], imm);
|
std::println("auipc {}, {}", REGS[rd], imm);
|
||||||
}; break;
|
}; break;
|
||||||
default:
|
default:
|
||||||
fprintf(stderr, "Unrecognized opcode: %07b\n", opcode);
|
std::println(stderr, "Unrecognized opcode: {:07b}", opcode);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void dump() {
|
void dump() {
|
||||||
std::cout << "REGS:";
|
std::print("REGS:");
|
||||||
for (u64 i = 0; i < 32; i++) {
|
for (u64 i = 0; i < 32; i++) {
|
||||||
std::cout << " " << m_regs[i];
|
std::print(" ");
|
||||||
|
std::print("{}", m_regs[i]);
|
||||||
}
|
}
|
||||||
std::cout << "\n";
|
std::println();
|
||||||
}
|
}
|
||||||
|
|
||||||
void execute() {
|
void execute() {
|
||||||
@@ -473,7 +492,7 @@ public:
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "B-type: unrecognized funct3: %03b\n", funct3);
|
std::println(stderr, "B-type: unrecognized funct3: {:03b}", funct3);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}; break;
|
}; break;
|
||||||
@@ -489,8 +508,9 @@ public:
|
|||||||
if (funct6 == 0b000000) {
|
if (funct6 == 0b000000) {
|
||||||
m_regs[rd] = m_regs[rs1] << shamt;
|
m_regs[rd] = m_regs[rs1] << shamt;
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "I-type 1: funct3=001: unrecognized funct6: %b\n",
|
std::println(stderr,
|
||||||
funct6);
|
"I-type 1: funct3=001: unrecognized funct6: {:b}",
|
||||||
|
funct6);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
} else if (funct3 == 0b011) { // sltiu
|
} else if (funct3 == 0b011) { // sltiu
|
||||||
@@ -507,14 +527,15 @@ public:
|
|||||||
} else if (funct6 == 0b010000) { // srai
|
} else if (funct6 == 0b010000) { // srai
|
||||||
m_regs[rd] = (i64)m_regs[rs1] >> shamt;
|
m_regs[rd] = (i64)m_regs[rs1] >> shamt;
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "I-type 1: funct3=101: unrecognized funct6: %b\n",
|
std::println(stderr,
|
||||||
funct6);
|
"I-type 1: funct3=101: unrecognized funct6: {:b}",
|
||||||
|
funct6);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
} else if (funct3 == 0b111) {
|
} else if (funct3 == 0b111) {
|
||||||
m_regs[rd] = m_regs[rs1] & imm;
|
m_regs[rd] = m_regs[rs1] & imm;
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "I-type 1: unrecognized funct3: %03b\n", funct3);
|
std::println(stderr, "I-type 1: unrecognized funct3: {:03b}", funct3);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}; break;
|
}; break;
|
||||||
@@ -532,7 +553,7 @@ public:
|
|||||||
} else if (funct3 == 0b100) { // lbu
|
} else if (funct3 == 0b100) { // lbu
|
||||||
m_regs[rd] = m_memory[m_regs[rs1] + imm];
|
m_regs[rd] = m_memory[m_regs[rs1] + imm];
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "I-type 2: unrecognized funct3: %03b\n", funct3);
|
std::println(stderr, "I-type 2: unrecognized funct3: {:03b}", funct3);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}; break;
|
}; break;
|
||||||
@@ -552,7 +573,8 @@ public:
|
|||||||
switch (m_regs[17]) {
|
switch (m_regs[17]) {
|
||||||
case 63: { // read
|
case 63: { // read
|
||||||
if (m_regs[10] != 0) {
|
if (m_regs[10] != 0) {
|
||||||
fprintf(stderr, "read syscall implemented only for stdin.\n");
|
std::println(stderr,
|
||||||
|
"read syscall implemented only for stdin.");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -574,7 +596,8 @@ public:
|
|||||||
}; break;
|
}; break;
|
||||||
case 64: { // write
|
case 64: { // write
|
||||||
if (m_regs[10] != 1) {
|
if (m_regs[10] != 1) {
|
||||||
fprintf(stderr, "write syscall implemented only for stdout.\n");
|
std::println(stderr,
|
||||||
|
"write syscall implemented only for stdout.");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -586,7 +609,7 @@ public:
|
|||||||
}
|
}
|
||||||
}; break;
|
}; break;
|
||||||
case 93: { // exit
|
case 93: { // exit
|
||||||
printf("Program exited with code %lu.\n", m_regs[10]);
|
std::println("Program exited with code {}.", m_regs[10]);
|
||||||
return;
|
return;
|
||||||
}; break;
|
}; break;
|
||||||
case 169: { // gettimeofday
|
case 169: { // gettimeofday
|
||||||
@@ -607,17 +630,18 @@ public:
|
|||||||
m_regs[10] = 0;
|
m_regs[10] = 0;
|
||||||
}; break;
|
}; break;
|
||||||
default:
|
default:
|
||||||
fprintf(stderr, "Unimplemented syscall: %lu\n", m_regs[17]);
|
std::println(stderr, "Unimplemented syscall: {}", m_regs[17]);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "I-type 4: funct3=000 unrecognized imm: %b\n", imm);
|
std::println(stderr, "I-type 4: funct3=000 unrecognized imm: {:b}",
|
||||||
|
imm);
|
||||||
exit(1);
|
exit(1);
|
||||||
};
|
};
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "I-type 4: unrecognized funct3: %03b\n", funct3);
|
std::println(stderr, "I-type 4: unrecognized funct3: {:03b}", funct3);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}; break;
|
}; break;
|
||||||
@@ -637,8 +661,9 @@ public:
|
|||||||
} else if (funct7 == 0b0000001) { // mul
|
} else if (funct7 == 0b0000001) { // mul
|
||||||
m_regs[rd] = m_regs[rs1] * m_regs[rs2];
|
m_regs[rd] = m_regs[rs1] * m_regs[rs2];
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "R-type 1: funct3=0b000: unrecognized funct7: %b\n",
|
std::println(stderr,
|
||||||
funct7);
|
"R-type 1: funct3=0b000: unrecognized funct7: {:b}",
|
||||||
|
funct7);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
} else if (funct3 == 0b001) {
|
} else if (funct3 == 0b001) {
|
||||||
@@ -655,16 +680,18 @@ public:
|
|||||||
i64 mid = p1 + p2 + carry;
|
i64 mid = p1 + p2 + carry;
|
||||||
m_regs[rd] = p3 + (mid >> 32);
|
m_regs[rd] = p3 + (mid >> 32);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "R-type 1: funct3=0b001: unrecognized funct7: %b\n",
|
std::println(stderr,
|
||||||
funct7);
|
"R-type 1: funct3=0b001: unrecognized funct7: {:b}",
|
||||||
|
funct7);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
} else if (funct3 == 0b010) {
|
} else if (funct3 == 0b010) {
|
||||||
if (funct7 == 0b0000000) { // slt
|
if (funct7 == 0b0000000) { // slt
|
||||||
m_regs[rd] = (m_regs[rs1] < m_regs[rs2]) ? 1 : 0;
|
m_regs[rd] = (m_regs[rs1] < m_regs[rs2]) ? 1 : 0;
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "R-type 1: funct3=0b010: unrecognized funct7: %b\n",
|
std::println(stderr,
|
||||||
funct7);
|
"R-type 1: funct3=0b010: unrecognized funct7: {:b}",
|
||||||
|
funct7);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
} else if (funct3 == 0b011) {
|
} else if (funct3 == 0b011) {
|
||||||
@@ -685,8 +712,9 @@ public:
|
|||||||
(lo_lo >> 32) + (hi_lo & 0xffffffff) + (lo_hi & 0xffffffff);
|
(lo_lo >> 32) + (hi_lo & 0xffffffff) + (lo_hi & 0xffffffff);
|
||||||
m_regs[rd] = hi_hi + (hi_lo >> 32) + (lo_hi >> 32) + (mid >> 32);
|
m_regs[rd] = hi_hi + (hi_lo >> 32) + (lo_hi >> 32) + (mid >> 32);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "R-type 1: funct3=0b011: unrecognized funct7: %b\n",
|
std::println(stderr,
|
||||||
funct7);
|
"R-type 1: funct3=0b011: unrecognized funct7: {:b}",
|
||||||
|
funct7);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
} else if (funct3 == 0b100) {
|
} else if (funct3 == 0b100) {
|
||||||
@@ -699,8 +727,9 @@ public:
|
|||||||
m_regs[rd] = m_regs[rs1] / m_regs[rs2];
|
m_regs[rd] = m_regs[rs1] / m_regs[rs2];
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "R-type 1: funct3=0b100: unrecognized funct7: %b\n",
|
std::println(stderr,
|
||||||
funct7);
|
"R-type 1: funct3=0b100: unrecognized funct7: {:b}",
|
||||||
|
funct7);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
} else if (funct3 == 0b110) {
|
} else if (funct3 == 0b110) {
|
||||||
@@ -713,8 +742,9 @@ public:
|
|||||||
} else if (funct7 == 0b0000000) { // or
|
} else if (funct7 == 0b0000000) { // or
|
||||||
m_regs[rd] = m_regs[rs1] | m_regs[rs2];
|
m_regs[rd] = m_regs[rs1] | m_regs[rs2];
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "R-type 1: funct3=0b110: unrecognized funct7: %b\n",
|
std::println(stderr,
|
||||||
funct7);
|
"R-type 1: funct3=0b110: unrecognized funct7: {:b}",
|
||||||
|
funct7);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
} else if (funct3 == 0b111) {
|
} else if (funct3 == 0b111) {
|
||||||
@@ -727,17 +757,18 @@ public:
|
|||||||
m_regs[rd] = (u64)m_regs[rs1] % (u64)m_regs[rs2];
|
m_regs[rd] = (u64)m_regs[rs1] % (u64)m_regs[rs2];
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "R-type 1: funct3=0b111: unrecognized funct7: %b\n",
|
std::println(stderr,
|
||||||
funct7);
|
"R-type 1: funct3=0b111: unrecognized funct7: {:b}",
|
||||||
|
funct7);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "R-type 1: unrecognized funct3: %03b\n", funct3);
|
std::println(stderr, "R-type 1: unrecognized funct3: {:03b}", funct3);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}; break;
|
}; break;
|
||||||
case 0b0101111: {
|
case 0b0101111: {
|
||||||
fprintf(stderr, "A extension not implemented yet.\n");
|
std::println(stderr, "A extension not implemented yet.");
|
||||||
exit(1);
|
exit(1);
|
||||||
}; break;
|
}; break;
|
||||||
case 0b0111011: {
|
case 0b0111011: {
|
||||||
@@ -750,8 +781,9 @@ public:
|
|||||||
} else if (funct7 == 0b0000001) { // mulw
|
} else if (funct7 == 0b0000001) { // mulw
|
||||||
m_regs[rd] = (i32)(m_regs[rs1] * m_regs[rs2]);
|
m_regs[rd] = (i32)(m_regs[rs1] * m_regs[rs2]);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "R-type 3: funct3=000: unrecognized funct7: %b\n",
|
std::println(stderr,
|
||||||
funct7);
|
"R-type 3: funct3=000: unrecognized funct7: {:b}",
|
||||||
|
funct7);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
} else if (funct3 == 0b001) {
|
} else if (funct3 == 0b001) {
|
||||||
@@ -759,8 +791,9 @@ public:
|
|||||||
m_regs[rd] =
|
m_regs[rd] =
|
||||||
(i32)(u32)((u32)m_regs[rs1] << ((u32)m_regs[rs2] & 0b11111));
|
(i32)(u32)((u32)m_regs[rs1] << ((u32)m_regs[rs2] & 0b11111));
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "R-type 3: funct3=001: unrecognized funct7: %b\n",
|
std::println(stderr,
|
||||||
funct7);
|
"R-type 3: funct3=001: unrecognized funct7: {:b}",
|
||||||
|
funct7);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
} else if (funct3 == 0b100) { // divw
|
} else if (funct3 == 0b100) { // divw
|
||||||
@@ -784,8 +817,9 @@ public:
|
|||||||
m_regs[rd] = (i32)((u32)m_regs[rs1] / (u32)m_regs[rs2]);
|
m_regs[rd] = (i32)((u32)m_regs[rs1] / (u32)m_regs[rs2]);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "R-type 3: funct3=101: unrecognized funct7: %b\n",
|
std::println(stderr,
|
||||||
funct7);
|
"R-type 3: funct3=101: unrecognized funct7: {:b}",
|
||||||
|
funct7);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
} else if (funct3 == 0b111) { // remuw
|
} else if (funct3 == 0b111) { // remuw
|
||||||
@@ -795,7 +829,7 @@ public:
|
|||||||
m_regs[rd] = (i32)((u32)m_regs[rs1] % (u32)m_regs[rs2]);
|
m_regs[rd] = (i32)((u32)m_regs[rs1] % (u32)m_regs[rs2]);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "R-type 3: unrecognized funct3: %03b\n", funct3);
|
std::println(stderr, "R-type 3: unrecognized funct3: {:03b}", funct3);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}; break;
|
}; break;
|
||||||
@@ -813,12 +847,13 @@ public:
|
|||||||
} else if (funct7 == 0b0100000) { // sraiw
|
} else if (funct7 == 0b0100000) { // sraiw
|
||||||
m_regs[rd] = (i32)m_regs[rs1] >> rs2;
|
m_regs[rd] = (i32)m_regs[rs1] >> rs2;
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "R-type 4: funct3=101: unrecognized funct7: %b\n",
|
std::println(stderr,
|
||||||
funct7);
|
"R-type 4: funct3=101: unrecognized funct7: {:b}",
|
||||||
|
funct7);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "R-type 4: unrecognized funct3: %03b\n", funct3);
|
std::println(stderr, "R-type 4: unrecognized funct3: {:03b}", funct3);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}; break;
|
}; break;
|
||||||
@@ -839,7 +874,7 @@ public:
|
|||||||
// take this rust
|
// take this rust
|
||||||
*(u64 *)(&m_memory[addr]) = m_regs[rs2];
|
*(u64 *)(&m_memory[addr]) = m_regs[rs2];
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "S-type: unrecognized funct3: %03b\n", funct3);
|
std::println(stderr, "S-type: unrecognized funct3: {:03b}", funct3);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}; break;
|
}; break;
|
||||||
@@ -852,7 +887,7 @@ public:
|
|||||||
m_regs[rd] = m_pc + ((i64)imm << 12);
|
m_regs[rd] = m_pc + ((i64)imm << 12);
|
||||||
}; break;
|
}; break;
|
||||||
default:
|
default:
|
||||||
fprintf(stderr, "Unrecognized opcode: %07b\n", opcode);
|
std::println(stderr, "Unrecognized opcode: {:07b}", opcode);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -869,7 +904,7 @@ private:
|
|||||||
static Section get_code_section(Elf *elf, GElf_Ehdr ehdr) {
|
static Section get_code_section(Elf *elf, GElf_Ehdr ehdr) {
|
||||||
u64 str_table_index;
|
u64 str_table_index;
|
||||||
if (elf_getshdrstrndx(elf, &str_table_index) != 0) {
|
if (elf_getshdrstrndx(elf, &str_table_index) != 0) {
|
||||||
std::cerr << "elf_getshdrstrndx failed: " << elf_errmsg(-1) << "\n";
|
std::println(stderr, "elf_getshdrstrndx failed: {}", elf_errmsg(-1));
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -887,14 +922,14 @@ private:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cerr << "Failed to locate .text\n";
|
std::println(stderr, "Failed to locate .text");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
if (elf_version(EV_CURRENT) == EV_NONE) {
|
if (elf_version(EV_CURRENT) == EV_NONE) {
|
||||||
std::cerr << "Failed to initialize libelf: " << elf_errmsg(-1) << "\n";
|
std::println(stderr, "Failed to initialize libelf: {}", elf_errmsg(-1));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -904,13 +939,13 @@ int main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (path == nullptr) {
|
if (path == nullptr) {
|
||||||
std::cerr << "Usage: " << argv[0] << " <path>\n";
|
std::println(stderr, "Usage: {} <path>", argv[0]);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ifstream file(path, std::ios::binary | std::ios::ate);
|
std::ifstream file(path, std::ios::binary | std::ios::ate);
|
||||||
if (!file) {
|
if (!file) {
|
||||||
std::cerr << "Failed to open: " << path << "\n";
|
std::println(stderr, "Failed to open: {}", path);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -927,7 +962,7 @@ int main(int argc, char *argv[]) {
|
|||||||
exe_bytes.shrink_to_fit();
|
exe_bytes.shrink_to_fit();
|
||||||
|
|
||||||
r.disassemble_all();
|
r.disassemble_all();
|
||||||
printf("END DISASSEMBLY\n");
|
std::println("END DISASSEMBLY");
|
||||||
|
|
||||||
r.execute();
|
r.execute();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user