From 849d5f4fc3aa13e755ee59c2ed0898401a6457be Mon Sep 17 00:00:00 2001 From: Anton Lydike Date: Sat, 5 Jun 2021 15:24:16 +0200 Subject: [PATCH] [decoder, ElfLoader] decoing an instruction now returns all args as int --- riscemu/decoder/__init__.py | 2 +- riscemu/decoder/__main__.py | 3 ++- riscemu/decoder/decoder.py | 8 ++++---- riscemu/decoder/formats.py | 30 +++++++++++++++--------------- riscemu/priv/ElfLoader.py | 14 +++++++------- 5 files changed, 29 insertions(+), 28 deletions(-) diff --git a/riscemu/decoder/__init__.py b/riscemu/decoder/__init__.py index 3ba852e..e68396a 100644 --- a/riscemu/decoder/__init__.py +++ b/riscemu/decoder/__init__.py @@ -1 +1 @@ -from .decoder import decode \ No newline at end of file +from .decoder import decode, RISCV_REGS diff --git a/riscemu/decoder/__main__.py b/riscemu/decoder/__main__.py index e109c97..98bc146 100644 --- a/riscemu/decoder/__main__.py +++ b/riscemu/decoder/__main__.py @@ -6,6 +6,7 @@ if __name__ == '__main__': from .decoder import * from .formats import * from .instruction_table import * + from .regs import RISCV_REGS sess_vars = globals() sess_vars.update(locals()) @@ -13,4 +14,4 @@ if __name__ == '__main__': readline.set_completer(rlcompleter.Completer(sess_vars).complete) readline.set_completer(rlcompleter.Completer(sess_vars).complete) readline.parse_and_bind("tab: complete") - code.InteractiveConsole(sess_vars).interact(banner="Interaktive decoding session started...", exitmsg="Closing...") \ No newline at end of file + code.InteractiveConsole(sess_vars).interact(banner="Interaktive decoding session started...", exitmsg="Closing...") diff --git a/riscemu/decoder/decoder.py b/riscemu/decoder/decoder.py index f0601e8..5b198ad 100644 --- a/riscemu/decoder/decoder.py +++ b/riscemu/decoder/decoder.py @@ -8,11 +8,11 @@ def print_ins(ins: int): f"0b{ins >> 25 :07b}_{(ins >> 20) & 0b11111:05b}_{(ins >> 15) & 0b11111:03b}_{(ins >> 12) & 0b111:03b}_{(ins >> 7) & 0b11111:05b}_{ins & 0b1111111:07b}"); -STATIC_INSN: Dict[int, Tuple[str, List[Union[str, int]], int]] = { +STATIC_INSN: Dict[int, Tuple[str, List[int], int]] = { 0x00000013: ("nop", [], 0x00000013), 0x00008067: ("ret", [], 0x00008067), - 0xfe010113: ("addi", ["sp", "sp", -32], 0xfe010113), - 0x02010113: ("addi", ["sp", "sp", 32], 0x02010113), + 0xfe010113: ("addi", [2, 2, -32], 0xfe010113), + 0x02010113: ("addi", [2, 2, 32], 0x02010113), 0x00100073: ("ebreak", [], 0x00100073), 0x00000073: ("ecall", [], 0x00000073), 0x30200073: ("mret", [], 0x30200073), @@ -72,7 +72,7 @@ def name_from_insn(ins: int): raise RuntimeError(f"Invalid instruction: {ins:x}") -def decode(ins: bytearray) -> Tuple[str, List[Union[str, int]], int]: +def decode(ins: bytearray) -> Tuple[str, List[int], int]: insn = int_from_ins(ins) if insn & 3 != 3: diff --git a/riscemu/decoder/formats.py b/riscemu/decoder/formats.py index ccca92f..0b67d6a 100644 --- a/riscemu/decoder/formats.py +++ b/riscemu/decoder/formats.py @@ -69,37 +69,37 @@ def sign_extend(num, bits): return (num & (sign_mask - 1)) - (num & sign_mask) -def decode_i(ins: int) -> List[Union[str, int]]: - return [RISCV_REGS[rd(ins)], RISCV_REGS[rs1(ins)], imm_i(ins)] +def decode_i(ins: int) -> List[int]: + return [rd(ins), rs1(ins), imm_i(ins)] -def decode_b(ins: int) -> List[Union[str, int]]: - return [RISCV_REGS[rs1(ins)], RISCV_REGS[rs2(ins)], imm_b(ins)] +def decode_b(ins: int) -> List[int]: + return [rs1(ins), rs2(ins), imm_b(ins)] -def decode_u(ins: int) -> List[Union[str, int]]: - return [RISCV_REGS[rd(ins)], imm_u(ins)] +def decode_u(ins: int) -> List[int]: + return [rd(ins), imm_u(ins)] -def decode_r(ins: int) -> List[Union[str, int]]: - return [RISCV_REGS[rd(ins)], RISCV_REGS[rs1(ins)], RISCV_REGS[rs2(ins)]] +def decode_r(ins: int) -> List[int]: + return [rd(ins), rs1(ins), rs2(ins)] -def decode_s(ins: int) -> List[Union[str, int]]: - return [RISCV_REGS[rs1(ins)], RISCV_REGS[rs2(ins)], imm_s(ins)] +def decode_s(ins: int) -> List[int]: + return [rs2(ins), rs1(ins), imm_s(ins)] -def decode_j(ins: int) -> List[Union[str, int]]: - return [RISCV_REGS[rd(ins)], imm_j(ins)] +def decode_j(ins: int) -> List[int]: + return [rd(ins), imm_j(ins)] -def decode_i_shamt(ins: int) -> List[Union[str, int]]: +def decode_i_shamt(ins: int) -> List[int]: if funct3(ins) in (1, 5): - return [RISCV_REGS[rd(ins)], RISCV_REGS[rs1(ins)], rs2(ins)] + return [rd(ins), rs1(ins), rs2(ins)] return decode_i(ins) -INSTRUCTION_ARGS_DECODER: Dict[int, Callable[[int], List[Union[str, int]]]] = { +INSTRUCTION_ARGS_DECODER: Dict[int, Callable[[int], List[int]]] = { 0x00: decode_i, 0x04: decode_i_shamt, 0x05: decode_u, diff --git a/riscemu/priv/ElfLoader.py b/riscemu/priv/ElfLoader.py index 09ac73d..cf58bea 100644 --- a/riscemu/priv/ElfLoader.py +++ b/riscemu/priv/ElfLoader.py @@ -7,7 +7,7 @@ from elftools.elf.sections import Section, SymbolTableSection from .Exceptions import * from ..Exceptions import RiscemuBaseException from ..Executable import MemoryFlags, LoadedMemorySection -from ..decoder import decode +from ..decoder import decode, RISCV_REGS from ..helpers import FMT_PARSE, FMT_NONE, FMT_GREEN, FMT_BOLD FMT_ELF = FMT_GREEN + FMT_BOLD @@ -96,17 +96,17 @@ class InvalidElfException(RiscemuBaseException): @dataclass(frozen=True) class ElfInstruction: name: str - args: List[Union[int, str]] + args: List[int] encoded: int - def get_imm(self, num: int): - return self.args[-1] + def get_imm(self, num: int) -> int: + return self.args[num] - def get_imm_reg(self, num: int): + def get_imm_reg(self, num: int) -> Tuple[int, int]: return self.args[-1], self.args[-2] - def get_reg(self, num: int): - return self.args[num] + def get_reg(self, num: int) -> str: + return RISCV_REGS[self.args[num]] def __repr__(self): if self.name in ('sw', 'sh', 'sb', 'lb', 'lh', 'lb', 'lbu', 'lhu'):