Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow register indices in addition to ABI names #52

Merged
merged 4 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion riscemu/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
SimpleInstruction,
ParseException,
)
from .regs import REG_NAME_CANONICALIZER


def parse_instruction(token: Token, args: Tuple[str], context: ParseContext):
Expand All @@ -28,7 +29,7 @@ def parse_instruction(token: Token, args: Tuple[str], context: ParseContext):
"{} {} encountered in invalid context: {}".format(token, args, context)
)
ins = SimpleInstruction(
token.value, args, context.context, context.current_address()
token.value, parse_instruction_arguments(args), context.context, context.current_address()
)
context.section.data.append(ins)

Expand Down Expand Up @@ -112,6 +113,30 @@ def take_arguments(tokens: Peekable[Token]) -> Iterable[str]:
# raise ParseException("Expected newline, instead got {}".format(tokens.peek()))


def parse_instruction_arguments(args: Tuple[str]) -> Tuple[str]:
"""
Parses argument Tuples of instructions. In this process canonicalize register names
:param args: A tuple of an instructions arguments
:return: A tuple of an instructions parsed arguments
"""
return tuple(canonicalize_register_names(args))


def canonicalize_register_names(args: Tuple[str]) -> Iterable[str]:
"""
Translate register indices names to ABI names. Leaves other arguments unchanged.
Examples:
"x0" -> "zero"
"x5" -> "t0"
"x8" -> "s0"
"fp" -> "s0"
:param args: A tuple of an instructions arguments
:return: An iterator over the arguments of an instruction, but with canonicalized register names
"""
for arg in args:
yield REG_NAME_CANONICALIZER.get(arg, arg)


class AssemblyFileLoader(ProgramLoader):
"""
This class loads assembly files written by hand. It understands some assembler
Expand Down
69 changes: 69 additions & 0 deletions riscemu/regs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
REG_NAME_CANONICALIZER = {
"fp": "s0",

"x0": "zero",
"x1": "ra",
"x2": "sp",
"x3": "gp",
"x4": "tp",
"x5": "t0",
"x6": "t1",
"x7": "t2",
"x8": "s0",
AntonLydike marked this conversation as resolved.
Show resolved Hide resolved
"x9": "s1",
"x10": "a0",
"x11": "a1",
"x12": "a2",
"x13": "a3",
"x14": "a4",
"x15": "a5",
"x16": "a6",
"x17": "a7",
"x18": "s2",
"x19": "s3",
"x20": "s4",
"x21": "s5",
"x22": "s6",
"x23": "s7",
"x24": "s8",
"x25": "s9",
"x26": "s10",
"x27": "s11",
"x28": "t3",
"x29": "t4",
"x30": "t5",
"x31": "t6",

"f0": "ft0",
"f1": "ft1",
"f2": "ft2",
"f3": "ft3",
"f4": "ft4",
"f5": "ft5",
"f6": "ft6",
"f7": "ft7",
"f8": "fs0",
"f9": "fs1",
"f10": "fa0",
"f11": "fa1",
"f12": "fa2",
"f13": "fa3",
"f14": "fa4",
"f15": "fa5",
"f16": "fa6",
"f17": "fa7",
"f18": "fs2",
"f19": "fs3",
"f20": "fs4",
"f21": "fs5",
"f22": "fs6",
"f23": "fs7",
"f24": "fs8",
"f25": "fs9",
"f26": "fs10",
"f27": "fs11",
"f28": "ft8",
"f29": "ft9",
"f30": "ft10",
"f31": "ft11"
}
15 changes: 15 additions & 0 deletions test/filecheck/hello-world-register-indices.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// RUN: python3 -m riscemu -v %s | filecheck %s
.data
msg: .ascii "Hello world\n"
.text
addi x10, x0, 1 ; print to stdout
addi x11, x0, msg ; load msg address
addi x12, x0, 12 ; write 12 bytes
addi x17, x0, SCALL_WRITE ; write syscall code
scall
addi x10, x0, 0 ; set exit code to 0
addi x17, x0, SCALL_EXIT ; exit syscall code
scall

// CHECK: Hello world
// CHECK: [CPU] Program exited with code