Skip to content

Commit

Permalink
fix SimpleInstruction.get_imm and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonLydike committed Oct 12, 2023
1 parent fed43be commit 702dedb
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`)
Expand Down
2 changes: 1 addition & 1 deletion riscemu/core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
12 changes: 9 additions & 3 deletions riscemu/core/simple_instruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]")
Expand Down Expand Up @@ -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):
Expand All @@ -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)

Expand Down
53 changes: 53 additions & 0 deletions test/test_instruction.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 702dedb

Please sign in to comment.