riscemu.types.instruction module

class riscemu.types.instruction.Immediate(abs_value: Union[int, Int32], pcrel_value: Union[int, Int32])

Bases: object

This class solves a problem of ambiguity when interpreting assembly code.

Let’s look at the following four cases (assuming 1b is 16 bytes back):
  1. beq a0, a1, 1b // conditional jump 16 bytes back

  2. beq a0, a1, -16 // conditional jump 16 bytes back

  3. addi a0, a1, 1b // subtract (pc - 16) from a1

  4. addi a0, a1, -16 // subtract 16 from a1

We want a and b to behave the same, but c and d not to.

The Immediate class solves this problem, by giving each instruction two ways of interpreting a given immediate. It can either get the “absolute value” of the immediate, or it’s relative value to the PC.

In this case, the beq instruction would interpret both as PC relative values, while addi would treat them as absolute values.

__init__(abs_value: Union[int, Int32], pcrel_value: Union[int, Int32])
abs_value: Int32
pcrel_value: Int32
class riscemu.types.instruction.Instruction

Bases: ABC

name: str
args: tuple
abstract get_imm(num: int) Immediate

parse and get immediate argument

abstract get_reg(num: int) str

parse and get an register argument