diff --git a/CHANGELOG.md b/CHANGELOG.md index 349a857..850baac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 2.2.5 + + - BugFix: Fix missed import in core.simple_instruction + ## 2.2.4 - BugFix: Found and added some missing floating point registers (`ft8` to `ft11`) diff --git a/riscemu/core/exceptions.py b/riscemu/core/exceptions.py index ad15072..9b7117e 100644 --- a/riscemu/core/exceptions.py +++ b/riscemu/core/exceptions.py @@ -187,7 +187,7 @@ def INS_NOT_IMPLEMENTED(ins): class NumberFormatException(RiscemuBaseException): def __init__(self, msg): - super().__init__() + super().__init__(msg) self.msg = msg def message(self): diff --git a/riscemu/core/simple_instruction.py b/riscemu/core/simple_instruction.py index efbf9e1..8172b8b 100644 --- a/riscemu/core/simple_instruction.py +++ b/riscemu/core/simple_instruction.py @@ -2,7 +2,13 @@ from typing import Union, Tuple from functools import lru_cache -from . import Instruction, T_RelativeAddress, InstructionContext, Immediate +from . import ( + Instruction, + T_RelativeAddress, + InstructionContext, + Immediate, + NumberFormatException, +) from ..helpers import parse_numeric_argument _NUM_LABEL_RE = re.compile(r"[0-9][fb]") @@ -32,7 +38,7 @@ def get_imm(self, num: int) -> Immediate: if _INT_IMM_RE.fullmatch(token): value = parse_numeric_argument(token) - return Immediate(abs_value=value, pcrel_value=value + self.addr) + return Immediate(abs_value=value, pcrel_value=value - self.addr) # resolve label correctly if _NUM_LABEL_RE.fullmatch(token): @@ -43,7 +49,7 @@ def get_imm(self, num: int) -> Immediate: # TODO: make it raise a nice error instead if value is None: raise NumberFormatException( - "{} is neither a number now a known symbol!".format(token) + "{} is neither a number now a known symbol".format(token) ) return Immediate(abs_value=value, pcrel_value=value - self.addr) diff --git a/test/test_instruction.py b/test/test_instruction.py new file mode 100644 index 0000000..6b0179a --- /dev/null +++ b/test/test_instruction.py @@ -0,0 +1,53 @@ +from riscemu.core import InstructionContext, SimpleInstruction, NumberFormatException +import pytest + + +def test_int_and_hex_immediates(): + ctx = InstructionContext() + + ins = SimpleInstruction("addi", ("a0", "a1", "100"), ctx, 0x100) + ins_hex = SimpleInstruction("addi", ("a0", "a1", "0x10"), ctx, 0x100) + + assert ins.get_reg(0) == "a0" + assert ins.get_reg(1) == "a1" + assert ins.get_imm(2).abs_value == 100 + assert ins.get_imm(2).pcrel_value == 100 - 0x100 + + assert ins_hex.get_imm(2).abs_value == 0x10 + assert ins_hex.get_imm(2).pcrel_value == 0x10 - 0x100 + + +def test_label_immediates(): + ctx = InstructionContext() + ctx.labels["test"] = 100 + + ins = SimpleInstruction("addi", ("a0", "a1", "test"), ctx, 0x100) + + assert ins.get_reg(0) == "a0" + assert ins.get_reg(1) == "a1" + assert ins.get_imm(2).abs_value == 100 + assert ins.get_imm(2).pcrel_value == 100 - 0x100 + + +def test_numerical_labels(): + ctx = InstructionContext() + ctx.numbered_labels["1"] = [0x100 - 4, 0x100 + 16] + + ins = SimpleInstruction("addi", ("a0", "a1", "1b"), ctx, 0x100) + + assert ins.get_reg(0) == "a0" + assert ins.get_reg(1) == "a1" + assert ins.get_imm(2).abs_value == 0x100 - 4 + assert ins.get_imm(2).pcrel_value == -4 + + +def test_invalid_immediate_val(): + ctx = InstructionContext() + ctx.labels["test"] = 100 + + ins = SimpleInstruction("addi", ("a0", "a1", "test2"), ctx, 0x100) + + with pytest.raises( + NumberFormatException, match="test2 is neither a number now a known symbol" + ): + ins.get_imm(2)