Merge pull request #5 from AntonLydike/assembly-parser-rework
- Completely revamped parsing of readable assembly - Completely revamped internal types - Added hard and correct 32 bit integer handling in registersfloat_support
commit
4b77ce05a7
@ -1,189 +1,114 @@
|
||||
"""
|
||||
RiscEmu (c) 2021 Anton Lydike
|
||||
RiscEmu (c) 2021-2022 Anton Lydike
|
||||
|
||||
SPDX-License-Identifier: MIT
|
||||
|
||||
This file contains the CPU logic (not the individual instruction sets). See instructions/InstructionSet.py for more info
|
||||
This file contains the CPU logic (not the individual instruction sets). See instructions/instruction_set.py for more info
|
||||
on them.
|
||||
"""
|
||||
import sys
|
||||
from typing import Tuple, List, Dict, Callable, Type
|
||||
import typing
|
||||
from typing import List, Type
|
||||
|
||||
from .Tokenizer import RiscVTokenizer
|
||||
from .Executable import MemoryFlags
|
||||
from .Syscall import SyscallInterface, get_syscall_symbols
|
||||
from .Exceptions import RiscemuBaseException, LaunchDebuggerException
|
||||
import riscemu
|
||||
from .config import RunConfig
|
||||
from .MMU import MMU
|
||||
from .Config import RunConfig
|
||||
from .Registers import Registers
|
||||
from .colors import FMT_CPU, FMT_NONE
|
||||
from .debug import launch_debug_session
|
||||
from .colors import FMT_CPU, FMT_NONE, FMT_ERROR
|
||||
|
||||
import riscemu
|
||||
|
||||
import typing
|
||||
from .types.exceptions import RiscemuBaseException, LaunchDebuggerException
|
||||
from .syscall import SyscallInterface, get_syscall_symbols
|
||||
from .types import CPU, ProgramLoader, Int32, BinaryDataMemorySection
|
||||
from .parser import AssemblyFileLoader
|
||||
|
||||
if typing.TYPE_CHECKING:
|
||||
from . import Executable, LoadedExecutable, LoadedInstruction
|
||||
from .instructions.InstructionSet import InstructionSet
|
||||
from .instructions.instruction_set import InstructionSet
|
||||
|
||||
|
||||
class CPU:
|
||||
class UserModeCPU(CPU):
|
||||
"""
|
||||
This class represents a single CPU. It holds references to it's mmu, registers and syscall interrupt handler.
|
||||
|
||||
It is initialized with a configuration and a list of instruction sets.
|
||||
"""
|
||||
|
||||
INS_XLEN = 1
|
||||
|
||||
def __init__(self, conf: RunConfig, instruction_sets: List[Type['riscemu.InstructionSet']]):
|
||||
def __init__(self, instruction_sets: List[Type['riscemu.InstructionSet']], conf: RunConfig):
|
||||
"""
|
||||
Creates a CPU instance.
|
||||
|
||||
:param conf: An instance of the current RunConfiguration
|
||||
:param instruction_sets: A list of instruction set classes. These must inherit from the InstructionSet class
|
||||
"""
|
||||
# setup CPU states
|
||||
self.pc = 0
|
||||
self.cycle = 0
|
||||
self.exit: bool = False
|
||||
self.exit_code: int = 0
|
||||
self.conf = conf
|
||||
self.active_debug = False # if a debugging session is currently runnign
|
||||
|
||||
self.stack: typing.Optional['riscemu.LoadedMemorySection'] = None
|
||||
|
||||
# setup MMU, registers and syscall handlers
|
||||
self.mmu = MMU(conf)
|
||||
self.regs = Registers(conf)
|
||||
self.syscall_int = SyscallInterface()
|
||||
|
||||
# load all instruction sets
|
||||
self.instruction_sets: List[riscemu.InstructionSet] = list()
|
||||
self.instructions: Dict[str, Callable[[LoadedInstruction], None]] = dict()
|
||||
for set_class in instruction_sets:
|
||||
ins_set = set_class(self)
|
||||
self.instructions.update(ins_set.load())
|
||||
self.instruction_sets.append(ins_set)
|
||||
|
||||
# provide global syscall symbols if option is set
|
||||
if conf.include_scall_symbols:
|
||||
self.mmu.global_symbols.update(get_syscall_symbols())
|
||||
|
||||
def get_tokenizer(self, tokenizer_input):
|
||||
"""
|
||||
Returns a tokenizer that respects the language of the CPU
|
||||
|
||||
:param tokenizer_input: an instance of the RiscVTokenizerInput class
|
||||
"""
|
||||
return RiscVTokenizer(tokenizer_input, self.all_instructions())
|
||||
|
||||
def load(self, e: riscemu.Executable):
|
||||
"""
|
||||
Load an executable into Memory
|
||||
"""
|
||||
return self.mmu.load_bin(e)
|
||||
|
||||
def run_loaded(self, le: 'riscemu.LoadedExecutable'):
|
||||
"""
|
||||
Run a loaded executable
|
||||
"""
|
||||
self.pc = le.run_ptr
|
||||
|
||||
if self.conf.stack_size > 0:
|
||||
self.stack = self.mmu.allocate_section("stack", self.conf.stack_size, MemoryFlags(False, False))
|
||||
self.regs.set('sp', self.stack.base + self.stack.size)
|
||||
print(FMT_CPU + '[CPU] Allocated {} bytes of stack'.format(self.stack.size) + FMT_NONE)
|
||||
super().__init__(MMU(), instruction_sets, conf)
|
||||
|
||||
print(FMT_CPU + '[CPU] Started running from 0x{:08X} ({})'.format(le.run_ptr, le.name) + FMT_NONE)
|
||||
self._run()
|
||||
self.exit_code = 0
|
||||
|
||||
def continue_from_debugger(self, verbose=True):
|
||||
"""
|
||||
called from the debugger to continue running
|
||||
# setup syscall interface
|
||||
self.syscall_int = SyscallInterface()
|
||||
|
||||
:param verbose: If True, will print each executed instruction to STDOUT
|
||||
"""
|
||||
self._run(verbose)
|
||||
# add global syscall symbols, but don't overwrite any user-defined symbols
|
||||
syscall_symbols = get_syscall_symbols()
|
||||
syscall_symbols.update(self.mmu.global_symbols)
|
||||
self.mmu.global_symbols.update(syscall_symbols)
|
||||
|
||||
def step(self):
|
||||
def step(self, verbose=False):
|
||||
"""
|
||||
Execute a single instruction, then return.
|
||||
"""
|
||||
if self.exit:
|
||||
if self.halted:
|
||||
print(FMT_CPU + "[CPU] Program exited with code {}".format(self.exit_code) + FMT_NONE)
|
||||
else:
|
||||
try:
|
||||
self.cycle += 1
|
||||
ins = self.mmu.read_ins(self.pc)
|
||||
print(FMT_CPU + " Running 0x{:08X}:{} {}".format(self.pc, FMT_NONE, ins))
|
||||
self.pc += self.INS_XLEN
|
||||
self.run_instruction(ins)
|
||||
except LaunchDebuggerException:
|
||||
print(FMT_CPU + "[CPU] Returning to debugger!" + FMT_NONE)
|
||||
except RiscemuBaseException as ex:
|
||||
self.pc -= self.INS_XLEN
|
||||
print(ex.message())
|
||||
return
|
||||
|
||||
launch_debugger = False
|
||||
|
||||
def _run(self, verbose=False):
|
||||
if self.pc <= 0:
|
||||
return False
|
||||
ins = None
|
||||
try:
|
||||
while not self.exit:
|
||||
self.cycle += 1
|
||||
ins = self.mmu.read_ins(self.pc)
|
||||
if verbose:
|
||||
print(FMT_CPU + " Running 0x{:08X}:{} {}".format(self.pc, FMT_NONE, ins))
|
||||
self.pc += self.INS_XLEN
|
||||
self.run_instruction(ins)
|
||||
self.cycle += 1
|
||||
ins = self.mmu.read_ins(self.pc)
|
||||
if verbose:
|
||||
print(FMT_CPU + " Running 0x{:08X}:{} {}".format(self.pc, FMT_NONE, ins))
|
||||
self.pc += self.INS_XLEN
|
||||
self.run_instruction(ins)
|
||||
except RiscemuBaseException as ex:
|
||||
if not isinstance(ex, LaunchDebuggerException):
|
||||
print(FMT_ERROR + "[CPU] excpetion caught at 0x{:08X}: {}:".format(self.pc - 1, ins) + FMT_NONE)
|
||||
if isinstance(ex, LaunchDebuggerException):
|
||||
# if the debugger is active, raise the exception to
|
||||
if self.debugger_active:
|
||||
raise ex
|
||||
|
||||
print(FMT_CPU + '[CPU] Debugger launch requested!' + FMT_NONE)
|
||||
launch_debugger = True
|
||||
else:
|
||||
print(ex.message())
|
||||
self.pc -= self.INS_XLEN
|
||||
|
||||
if self.active_debug:
|
||||
print(FMT_CPU + "[CPU] Returning to debugger!" + FMT_NONE)
|
||||
return
|
||||
if self.conf.debug_on_exception:
|
||||
launch_debug_session(self, self.mmu, self.regs, "Exception encountered, launching debug:")
|
||||
|
||||
if self.exit:
|
||||
print()
|
||||
print(FMT_CPU + "Program exited with code {}".format(self.exit_code) + FMT_NONE)
|
||||
sys.exit(self.exit_code)
|
||||
else:
|
||||
print()
|
||||
print(FMT_CPU + "Program stopped without exiting - perhaps you stopped the debugger?" + FMT_NONE)
|
||||
|
||||
def run_instruction(self, ins: 'LoadedInstruction'):
|
||||
"""
|
||||
Execute a single instruction
|
||||
ex.print_stacktrace()
|
||||
print(FMT_CPU + '[CPU] Halting due to exception!' + FMT_NONE)
|
||||
self.halted = True
|
||||
|
||||
:param ins: The instruction to execute
|
||||
"""
|
||||
if ins.name in self.instructions:
|
||||
self.instructions[ins.name](ins)
|
||||
else:
|
||||
# this should never be reached, as unknown instructions are imparseable
|
||||
raise RuntimeError("Unknown instruction: {}".format(ins))
|
||||
if launch_debugger:
|
||||
launch_debug_session(self)
|
||||
|
||||
def all_instructions(self) -> List[str]:
|
||||
"""
|
||||
Return a list of all instructions this CPU can execute.
|
||||
"""
|
||||
return list(self.instructions.keys())
|
||||
def run(self, verbose=False):
|
||||
while not self.halted:
|
||||
self.step(verbose)
|
||||
|
||||
print(FMT_CPU + "[CPU] Program exited with code {}".format(self.exit_code) + FMT_NONE)
|
||||
|
||||
def __repr__(self):
|
||||
def setup_stack(self, stack_size=1024 * 4) -> bool:
|
||||
"""
|
||||
Returns a representation of the CPU and some of its state.
|
||||
Create program stack and populate stack pointer
|
||||
:param stack_size: the size of the required stack, defaults to 4Kib
|
||||
:return:
|
||||
"""
|
||||
return "{}(pc=0x{:08X}, cycle={}, exit={}, instructions={})".format(
|
||||
self.__class__.__name__,
|
||||
self.pc,
|
||||
self.cycle,
|
||||
self.exit,
|
||||
" ".join(s.name for s in self.instruction_sets)
|
||||
stack_sec = BinaryDataMemorySection(
|
||||
bytearray(stack_size),
|
||||
'.stack',
|
||||
None, # FIXME: why does a binary data memory section require a context?
|
||||
'',
|
||||
0
|
||||
)
|
||||
|
||||
if not self.mmu.load_section(stack_sec, fixed_position=False):
|
||||
return False
|
||||
|
||||
self.regs.set('sp', Int32(stack_sec.base + stack_sec.size))
|
||||
return True
|
||||
|
||||
@classmethod
|
||||
def get_loaders(cls) -> typing.Iterable[Type[ProgramLoader]]:
|
||||
return [AssemblyFileLoader]
|
||||
|
@ -1,319 +0,0 @@
|
||||
"""
|
||||
RiscEmu (c) 2021 Anton Lydike
|
||||
|
||||
SPDX-License-Identifier: MIT
|
||||
|
||||
This file holds Executable and LoadedExecutable classes as well as loading and some linking code.
|
||||
|
||||
FIXME: refactor this code into muliple files
|
||||
"""
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Dict, List, Tuple, Union, Optional
|
||||
from .Exceptions import *
|
||||
from .helpers import *
|
||||
from math import log
|
||||
|
||||
import typing
|
||||
|
||||
if typing.TYPE_CHECKING:
|
||||
from .Tokenizer import RiscVInstructionToken
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class MemoryFlags:
|
||||
read_only: bool
|
||||
executable: bool
|
||||
|
||||
def __repr__(self):
|
||||
return "{}({},{})".format(
|
||||
self.__class__.__name__,
|
||||
'ro' if self.read_only else 'rw',
|
||||
'x' if self.executable else '-'
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class MemorySection:
|
||||
name: str
|
||||
flags: MemoryFlags
|
||||
size: int = 0
|
||||
content: List[bytearray] = field(default_factory=list)
|
||||
|
||||
def add(self, data: bytearray):
|
||||
self.content.append(data)
|
||||
self.size += len(data)
|
||||
|
||||
def continuous_content(self, parent: 'LoadedExecutable'):
|
||||
"""
|
||||
converts the content into one continuous bytearray
|
||||
"""
|
||||
if self.size == 0:
|
||||
return bytearray(0)
|
||||
content = self.content[0]
|
||||
for b in self.content[1:]:
|
||||
content += b
|
||||
return content
|
||||
|
||||
|
||||
@dataclass
|
||||
class InstructionMemorySection(MemorySection):
|
||||
content: List['RiscVInstructionToken'] = field(default_factory=list)
|
||||
|
||||
def add_insn(self, insn: 'RiscVInstructionToken'):
|
||||
self.content.append(insn)
|
||||
self.size += 1
|
||||
|
||||
def continuous_content(self, parent: 'LoadedExecutable'):
|
||||
return [
|
||||
LoadedInstruction(ins.instruction, ins.args, parent)
|
||||
for ins in self.content
|
||||
]
|
||||
|
||||
|
||||
@dataclass()
|
||||
class Executable:
|
||||
run_ptr: Tuple[str, int]
|
||||
sections: Dict[str, MemorySection]
|
||||
symbols: Dict[str, Tuple[str, int]]
|
||||
exported_symbols: List[str]
|
||||
name: str
|
||||
|
||||
def __repr__(self):
|
||||
return "{}(sections = {}, symbols = {}, run_ptr = {}, globals={})".format(
|
||||
self.__class__.__name__,
|
||||
" ".join(self.sections.keys()),
|
||||
" ".join(self.symbols.keys()),
|
||||
self.run_ptr,
|
||||
",".join(self.exported_symbols)
|
||||
)
|
||||
|
||||
|
||||
### LOADING CODE
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class LoadedInstruction:
|
||||
"""
|
||||
An instruction which is loaded into memory. It knows the binary it belongs to to resolve symbols
|
||||
"""
|
||||
name: str
|
||||
args: List[str]
|
||||
bin: 'LoadedExecutable'
|
||||
|
||||
def get_imm(self, num: int):
|
||||
"""
|
||||
parse and get immediate argument
|
||||
"""
|
||||
if len(self.args) <= num:
|
||||
raise ParseException("Instruction {} expected argument at {} (args: {})".format(self.name, num, self.args))
|
||||
arg = self.args[num]
|
||||
# look up symbols
|
||||
if self.bin.has_symb(arg):
|
||||
return self.bin.lookup_symbol(arg)
|
||||
return parse_numeric_argument(arg)
|
||||
|
||||
def get_imm_reg(self, num: int):
|
||||
"""
|
||||
parse and get an argument imm(reg)
|
||||
"""
|
||||
if len(self.args) <= num:
|
||||
raise ParseException("Instruction {} expected argument at {} (args: {})".format(self.name, num, self.args))
|
||||
arg = self.args[num]
|
||||
ASSERT_IN("(", arg)
|
||||
imm, reg = arg[:-1].split("(")
|
||||
if self.bin.has_symb(imm):
|
||||
return self.bin.lookup_symbol(imm), reg
|
||||
return parse_numeric_argument(imm), reg
|
||||
|
||||
def get_reg(self, num: int):
|
||||
"""
|
||||
parse and get an register argument
|
||||
"""
|
||||
if len(self.args) <= num:
|
||||
raise ParseException("Instruction {} expected argument at {} (args: {})".format(self.name, num, self.args))
|
||||
return self.args[num]
|
||||
|
||||
def __repr__(self):
|
||||
return "{} {}".format(self.name, ", ".join(self.args))
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class LoadedMemorySection:
|
||||
"""
|
||||
A section which is loaded into memory
|
||||
"""
|
||||
name: str
|
||||
base: int
|
||||
size: int
|
||||
content: Union[List[LoadedInstruction], bytearray] = field(repr=False)
|
||||
flags: MemoryFlags
|
||||
owner: str
|
||||
|
||||
def read(self, offset: int, size: int):
|
||||
if offset < 0:
|
||||
raise MemoryAccessException('Invalid offset {}'.format(offset), self.base + offset, size, 'read')
|
||||
if offset + size > self.size:
|
||||
raise MemoryAccessException('Outside section boundary of section {}'.format(self.name), self.base + offset,
|
||||
size, 'read')
|
||||
return self.content[offset: offset + size]
|
||||
|
||||
def read_instruction(self, offset):
|
||||
if not self.flags.executable:
|
||||
raise MemoryAccessException('Section not executable!', self.base + offset, 1, 'read exec')
|
||||
|
||||
if offset < 0:
|
||||
raise MemoryAccessException('Invalid offset {}'.format(offset), self.base + offset, 1, 'read exec')
|
||||
if offset >= self.size:
|
||||
raise MemoryAccessException('Outside section boundary of section {}'.format(self.name), self.base + offset,
|
||||
1, 'read exec')
|
||||
return self.content[offset]
|
||||
|
||||
def write(self, offset, size, data):
|
||||
if self.flags.read_only:
|
||||
raise MemoryAccessException('Section not writeable {}'.format(self.name), self.base + offset, size, 'write')
|
||||
|
||||
if offset < 0:
|
||||
raise MemoryAccessException('Invalid offset {}'.format(offset), self.base + offset, 1, 'write')
|
||||
if offset >= self.size:
|
||||
raise MemoryAccessException('Outside section boundary of section {}'.format(self.name), self.base + offset,
|
||||
size, 'write')
|
||||
|
||||
for i in range(size):
|
||||
self.content[offset + i] = data[i]
|
||||
|
||||
def dump(self, at_addr=None, fmt='hex', max_rows=10, group=4, bytes_per_row=16, all=False):
|
||||
highlight = -1
|
||||
if at_addr is None:
|
||||
at_addr = self.base
|
||||
else:
|
||||
highlight = at_addr - self.base
|
||||
|
||||
at_off = at_addr - self.base
|
||||
start = max(align_addr(at_off - ((max_rows * bytes_per_row) // 2), 8) - 8, 0)
|
||||
if all:
|
||||
end = self.size
|
||||
start = 0
|
||||
else:
|
||||
end = min(start + (max_rows * bytes_per_row), self.size)
|
||||
|
||||
fmt_str = " 0x{:0" + str(ceil(log(self.base + end, 16))) + "X}: {}"
|
||||
|
||||
if self.flags.executable:
|
||||
# this section holds instructions!
|
||||
start = 0 if all else max(at_off - (max_rows // 2), 0)
|
||||
end = self.size if all else min(self.size, start + max_rows)
|
||||
print(FMT_MEM + "{}, viewing {} instructions:".format(
|
||||
self, end - start
|
||||
) + FMT_NONE)
|
||||
for i in range(start, end):
|
||||
if i == highlight:
|
||||
ins = FMT_UNDERLINE + FMT_ORANGE + repr(self.content[i]) + FMT_NONE
|
||||
else:
|
||||
ins = repr(self.content[i])
|
||||
print(fmt_str.format(self.base + i, ins))
|
||||
else:
|
||||
print(FMT_MEM + "{}, viewing {} bytes:".format(
|
||||
self, end - start
|
||||
) + FMT_NONE)
|
||||
for i in range(0, end - start, bytes_per_row):
|
||||
data = self.content[start + i: min(start + i + bytes_per_row, end)]
|
||||
if start + i <= highlight <= start + i + bytes_per_row:
|
||||
# do hightlight here!
|
||||
hi_ind = (highlight - start - i) // group
|
||||
print(fmt_str.format(self.base + start + i, format_bytes(data, fmt, group, highlight=hi_ind)))
|
||||
else:
|
||||
print(fmt_str.format(self.base + start + i, format_bytes(data, fmt, group)))
|
||||
if end == self.size:
|
||||
print(FMT_MEM + "End of section!" + FMT_NONE)
|
||||
else:
|
||||
print(FMT_MEM + "More bytes ..." + FMT_NONE)
|
||||
|
||||
def __repr__(self):
|
||||
return "{}[{}] at 0x{:08X} (size={}bytes, flags={}, owner={})".format(
|
||||
self.__class__.__name__,
|
||||
self.name,
|
||||
self.base,
|
||||
self.size,
|
||||
self.flags,
|
||||
self.owner
|
||||
)
|
||||
|
||||
|
||||
class LoadedExecutable:
|
||||
"""
|
||||
This represents an executable which is loaded into memory at address base_addr
|
||||
|
||||
This is basicalle the "loader" in normal system environments
|
||||
It initializes the stack and heap
|
||||
|
||||
It still holds a symbol table, that is not accessible memory since I don't want to deal with
|
||||
binary strings in memory etc.
|
||||
"""
|
||||
name: str
|
||||
base_addr: int
|
||||
sections_by_name: Dict[str, LoadedMemorySection]
|
||||
sections: List[LoadedMemorySection]
|
||||
symbols: Dict[str, int]
|
||||
run_ptr: int
|
||||
exported_symbols: Dict[str, int]
|
||||
global_symbol_table: Dict[str, int]
|
||||
|
||||
def __init__(self, exe: Executable, base_addr: int, global_symbol_table: Dict[str, int]):
|
||||
self.name = exe.name
|
||||
self.base_addr = base_addr
|
||||
self.sections = list()
|
||||
self.sections_by_name = dict()
|
||||
self.symbols = dict()
|
||||
self.exported_symbols = dict()
|
||||
self.global_symbol_table = global_symbol_table
|
||||
|
||||
curr = base_addr
|
||||
for sec in exe.sections.values():
|
||||
loaded_sec = LoadedMemorySection(
|
||||
sec.name,
|
||||
curr,
|
||||
sec.size,
|
||||
sec.continuous_content(self),
|
||||
sec.flags,
|
||||
self.name
|
||||
)
|
||||
self.sections.append(loaded_sec)
|
||||
self.sections_by_name[loaded_sec.name] = loaded_sec
|
||||
curr = align_addr(loaded_sec.size + curr)
|
||||
|
||||
for name, (sec_name, offset) in exe.symbols.items():
|
||||
if sec_name == '_static_':
|
||||
self.symbols[name] = offset
|
||||
else:
|
||||
ASSERT_IN(sec_name, self.sections_by_name)
|
||||
self.symbols[name] = self.sections_by_name[sec_name].base + offset
|
||||
|
||||
for name in exe.exported_symbols:
|
||||
self.exported_symbols[name] = self.symbols[name]
|
||||
|
||||
self.size = curr - base_addr
|
||||
|
||||
# translate run_ptr from executable
|
||||
run_ptr_sec, run_ptr_off = exe.run_ptr
|
||||
self.run_ptr = self.sections_by_name[run_ptr_sec].base + run_ptr_off
|
||||
|
||||
def lookup_symbol(self, name):
|
||||
if name in self.symbols:
|
||||
return self.symbols[name]
|
||||
if name in self.global_symbol_table:
|
||||
return self.global_symbol_table[name]
|
||||
raise LinkerException('Symbol {} not found!'.format(name), (self,))
|
||||
|
||||
def __repr__(self):
|
||||
return '{}[{}](base=0x{:08X}, size={}bytes, sections={}, run_ptr=0x{:08X})'.format(
|
||||
self.__class__.__name__,
|
||||
self.name,
|
||||
self.base_addr,
|
||||
self.size,
|
||||
" ".join(self.sections_by_name.keys()),
|
||||
self.run_ptr
|
||||
)
|
||||
|
||||
def has_symb(self, arg):
|
||||
return arg in self.symbols or arg in self.global_symbol_table
|
@ -1,193 +0,0 @@
|
||||
"""
|
||||
RiscEmu (c) 2021 Anton Lydike
|
||||
|
||||
SPDX-License-Identifier: MIT
|
||||
|
||||
This file holds the parser that parses the tokenizer output.
|
||||
"""
|
||||
|
||||
from .helpers import parse_numeric_argument, int_to_bytes
|
||||
from .Executable import Executable, InstructionMemorySection, MemorySection, MemoryFlags
|
||||
from .Exceptions import *
|
||||
|
||||
from .Tokenizer import RiscVTokenizer, RiscVInstructionToken, RiscVSymbolToken, RiscVPseudoOpToken
|
||||
|
||||
from typing import Dict, Tuple, List, Optional
|
||||
|
||||
|
||||
class ExecutableParser:
|
||||
"""
|
||||
Parses output form the RiscVTokenizer
|
||||
"""
|
||||
tokenizer: 'RiscVTokenizer'
|
||||
|
||||
def __init__(self, tokenizer: 'RiscVTokenizer'):
|
||||
self.instructions: List[RiscVInstructionToken] = list()
|
||||
self.symbols: Dict[str, Tuple[str, int]] = dict()
|
||||
self.sections: Dict[str, MemorySection] = dict()
|
||||
self.tokenizer = tokenizer
|
||||
self.active_section: Optional[str] = None
|
||||
self.implicit_sections = False
|
||||
self.globals: List[str] = list()
|
||||
|
||||
def parse(self) -> Executable:
|
||||
"""
|
||||
parse tokenizer output into an executable
|
||||
:return: the parsed executable
|
||||
:raise ParseException: Raises a ParseException when invalid input is read
|
||||
"""
|
||||
for token in self.tokenizer.tokens:
|
||||
if isinstance(token, RiscVInstructionToken):
|
||||
self.parse_instruction(token)
|
||||
elif isinstance(token, RiscVSymbolToken):
|
||||
self.handle_symbol(token)
|
||||
elif isinstance(token, RiscVPseudoOpToken):
|
||||
self.handle_pseudo_op(token)
|
||||
return self._get_execuable()
|
||||
|
||||
def _get_execuable(self) -> Executable:
|
||||
start_ptr = ('text', 0)
|
||||
if '_start' in self.symbols:
|
||||
start_ptr = self.symbols['_start']
|
||||
elif 'main' in self.symbols:
|
||||
start_ptr = self.symbols['main']
|
||||
return Executable(start_ptr, self.sections, self.symbols, self.globals, self.tokenizer.name)
|
||||
|
||||
def parse_instruction(self, ins: 'RiscVInstructionToken') -> None:
|
||||
"""
|
||||
parses an Instruction token
|
||||
:param ins: the instruction token
|
||||
"""
|
||||
if self.active_section is None:
|
||||
self.op_text()
|
||||
self.implicit_sections = True
|
||||
|
||||
ASSERT_EQ(self.active_section, 'text')
|
||||
sec = self._curr_sec()
|
||||
if isinstance(sec, InstructionMemorySection):
|
||||
sec.add_insn(ins)
|
||||
else:
|
||||
raise ParseException("SHOULD NOT BE REACHED")
|
||||
|
||||
def handle_symbol(self, token: 'RiscVSymbolToken'):
|
||||
"""
|
||||
Handle a symbol token (such as 'main:')
|
||||
:param token: the symbol token
|
||||
"""
|
||||
ASSERT_NOT_IN(token.name, self.symbols)
|
||||
ASSERT_NOT_NULL(self.active_section)
|
||||
sec_pos = self._curr_sec().size
|
||||
self.symbols[token.name] = (self.active_section, sec_pos)
|
||||
|
||||
def handle_pseudo_op(self, op: 'RiscVPseudoOpToken'):
|
||||
"""
|
||||
Handle a pseudo op token (such as '.word 0xffaabbcc')
|
||||
:param op: the peseudo-op token
|
||||
"""
|
||||
name = 'op_' + op.name
|
||||
if hasattr(self, name):
|
||||
getattr(self, name)(op)
|
||||
else:
|
||||
raise ParseException("Unknown pseudo op: {}".format(op), (op,))
|
||||
|
||||
## Pseudo op implementations:
|
||||
def op_section(self, op: 'RiscVPseudoOpToken'):
|
||||
"""
|
||||
handles a .section token
|
||||
:param op: The token
|
||||
"""
|
||||
ASSERT_LEN(op.args, 1)
|
||||
name = op.args[0][1:]
|
||||
ASSERT_IN(name, ('data', 'rodata', 'text'))
|
||||
getattr(self, 'op_' + name)(op)
|
||||
|
||||
def op_text(self, op: 'RiscVPseudoOpToken' = None):
|
||||
"""
|
||||
handles a .text token
|
||||
:param op: The token
|
||||
"""
|
||||
self._set_sec('text', MemoryFlags(read_only=True, executable=True), cls=InstructionMemorySection)
|
||||
|
||||
def op_data(self, op: 'RiscVPseudoOpToken' = None):
|
||||
"""
|
||||
handles a .data token
|
||||
:param op: The token
|
||||
"""
|
||||
self._set_sec('data', MemoryFlags(read_only=False, executable=False))
|
||||
|
||||
def op_rodata(self, op: 'RiscVPseudoOpToken' = None):
|
||||
"""
|
||||
handles a .rodata token
|
||||
:param op: The token
|
||||
"""
|
||||
self._set_sec('rodata', MemoryFlags(read_only=True, executable=False))
|
||||
|
||||
def op_space(self, op: 'RiscVPseudoOpToken'):
|
||||
"""
|
||||
handles a .space token. Inserts empty space into the current (data or rodata) section
|
||||
:param op: The token
|
||||
"""
|
||||
ASSERT_IN(self.active_section, ('data', 'rodata'))
|
||||
ASSERT_LEN(op.args, 1)
|
||||
size = parse_numeric_argument(op.args[0])
|
||||
self._curr_sec().add(bytearray(size))
|
||||
|
||||
def op_ascii(self, op: 'RiscVPseudoOpToken'):
|
||||
"""
|
||||
handles a .ascii token. Inserts ascii encoded text into the currrent data section
|
||||
:param op: The token
|
||||
"""
|
||||
ASSERT_IN(self.active_section, ('data', 'rodata'))
|
||||
ASSERT_LEN(op.args, 1)
|
||||
str = op.args[0][1:-1].encode('ascii').decode('unicode_escape')
|
||||
self._curr_sec().add(bytearray(str, 'ascii'))
|
||||
|
||||
def op_asciiz(self, op: 'RiscVPseudoOpToken'):
|
||||
"""
|
||||
handles a .ascii token. Inserts nullterminated ascii encoded text into the currrent data section
|
||||
:param op: The token
|
||||
"""
|
||||
ASSERT_IN(self.active_section, ('data', 'rodata'))
|
||||
ASSERT_LEN(op.args, 1)
|
||||
str = op.args[0][1:-1].encode('ascii').decode('unicode_escape')
|
||||
self._curr_sec().add(bytearray(str + '\0', 'ascii'))
|
||||
|
||||
def op_global(self, op: 'RiscVPseudoOpToken'):
|
||||
"""
|
||||
handles a .global token. Marks the token as global
|
||||
:param op: The token
|
||||
"""
|
||||
ASSERT_LEN(op.args, 1)
|
||||
name = op.args[0]
|
||||
self.globals.append(name)
|
||||
|
||||
def op_set(self, op: 'RiscVPseudoOpToken'):
|
||||
"""
|
||||
handles a .set name, val token. Sets the symbol name to val
|
||||
:param op: The token
|
||||
"""
|
||||
ASSERT_LEN(op.args, 2)
|
||||
name = op.args[0]
|
||||
val = parse_numeric_argument(op.args[1])
|
||||
self.symbols[name] = ('_static_', val)
|
||||
|
||||
def op_align(self, op: 'RiscVPseudoOpToken'):
|
||||
"""
|
||||
handles an align token. Currently a nop (just not implemented fully yet, as linker handles most alignement tasks)
|
||||
:param op: The token
|
||||
"""
|
||||
pass
|
||||
|
||||
def op_word(self, op: 'RiscVPseudoOpToken'):
|
||||
ASSERT_LEN(op.args, 1)
|
||||
val = parse_numeric_argument(op.args[0])
|
||||
self._curr_sec().add(int_to_bytes(val, 4))
|
||||
|
||||
## Section handler code
|
||||
def _set_sec(self, name: str, flags: MemoryFlags, cls=MemorySection):
|
||||
if name not in self.sections:
|
||||
self.sections[name] = cls(name, flags)
|
||||
self.active_section = name
|
||||
|
||||
def _curr_sec(self):
|
||||
return self.sections[self.active_section]
|
@ -1,22 +1,22 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Optional
|
||||
|
||||
from riscemu.types import MemorySection, MemoryFlags, T_RelativeAddress
|
||||
|
||||
class IOModule(ABC):
|
||||
addr: int
|
||||
size: int
|
||||
|
||||
def __init__(self, addr: int, size: int):
|
||||
self.addr = addr
|
||||
self.size = size
|
||||
class IOModule(MemorySection, ABC):
|
||||
def __init__(self, name: str, flags: MemoryFlags, size: int, owner: str = 'system', base: int = 0):
|
||||
super(IOModule, self).__init__(name, flags, size, base, owner, None)
|
||||
|
||||
@abstractmethod
|
||||
def read(self, addr: int, size: int):
|
||||
pass
|
||||
def contains(self, addr, size: int = 0):
|
||||
return self.base <= addr < self.base + self.size and \
|
||||
self.base <= addr + size <= self.base + self.size
|
||||
|
||||
@abstractmethod
|
||||
def write(self, addr: int, data: bytearray, size: int):
|
||||
pass
|
||||
def dump(self, start: T_RelativeAddress, end: Optional[T_RelativeAddress] = None, fmt: str = 'hex',
|
||||
bytes_per_row: int = 16, rows: int = 10, group: int = 4):
|
||||
print(self)
|
||||
|
||||
def contains(self, addr, size: int = 0):
|
||||
return self.addr <= addr < self.addr + self.size and \
|
||||
self.addr <= addr + size <= self.addr + self.size
|
||||
def __repr__(self):
|
||||
return "{}[{}] at 0x{:0X} (size={}bytes, flags={})".format(
|
||||
self.__class__.__name__, self.name, self.base, self.size, self.flags
|
||||
)
|
@ -1,320 +0,0 @@
|
||||
"""
|
||||
RiscEmu (c) 2021 Anton Lydike
|
||||
|
||||
SPDX-License-Identifier: MIT
|
||||
"""
|
||||
|
||||
import re
|
||||
from enum import IntEnum
|
||||
from typing import List
|
||||
|
||||
from .Exceptions import ParseException
|
||||
|
||||
PSEUDO_OPS = [
|
||||
'.asciiz',
|
||||
'.double',
|
||||
'.extern',
|
||||
'.global',
|
||||
'.align',
|
||||
'.float',
|
||||
'.kdata',
|
||||
'.ktext',
|
||||
'.space',
|
||||
'.ascii',
|
||||
'.byte',
|
||||
'.data',
|
||||
'.half',
|
||||
'.text',
|
||||
'.word',
|
||||
'.set',
|
||||
]
|
||||
|
||||
COMMENT_START = ["#", ";"]
|
||||
|
||||
REG_VALID_SYMBOL_LABEL = re.compile(r'^([A-z_.][A-z_0-9.]*[A-z_0-9]|[A-z_]):')
|
||||
|
||||
REG_WHITESPACE_UNTIL_NEWLINE = re.compile(r'^(\s*)\n')
|
||||
|
||||
REG_WHITESPACE = re.compile(r'^\s*')
|
||||
|
||||
REG_NONWHITESPACE = re.compile(r'^[^\s]*')
|
||||
|
||||
REG_UNTIL_NEWLINE = re.compile(r'^[^\n]*')
|
||||
|
||||
REG_WHITESPACE_NO_LINEBREAK = re.compile(r'^[ \t]*')
|
||||
|
||||
REG_VALID_ARGUMENT = re.compile(
|
||||
r'^([+-]?(0x[0-9A-f]+|[0-9]+)|[A-z_.][A-z0-9_.]*[A-z_0-9]|[A-z_])(\(([A-z_.][A-z_0-9.]*[A-z_0-9]|[A-z_])\))?'
|
||||
)
|
||||
|
||||
REG_ARG_SPLIT = re.compile(r'^,[ \t]*')
|
||||
|
||||
|
||||
def split_accepting_quotes(string, at=REG_ARG_SPLIT, quotes=('"', "'")):
|
||||
pos = 0
|
||||
last_piece = 0
|
||||
pieces = []
|
||||
in_quotes = False
|
||||
if string is None:
|
||||
return pieces
|
||||
while pos < len(string):
|
||||
match = at.match(string[pos:])
|
||||
if match is not None:
|
||||
if not in_quotes:
|
||||
pieces.append(string[last_piece:pos])
|
||||
pos += len(match.group(0))
|
||||
last_piece = pos
|
||||
else:
|
||||
pos += len(match.group(0))
|
||||
elif string[pos] in quotes:
|
||||
in_quotes = not in_quotes
|
||||
pos += 1
|
||||
elif string[pos] in COMMENT_START and not in_quotes: # entering comment
|
||||
break
|
||||
else:
|
||||
pos += 1
|
||||
if in_quotes:
|
||||
print("[Tokenizer.split] unbalanced quotes in \"{}\"!".format(string))
|
||||
pieces.append(string[last_piece:pos])
|
||||
return pieces
|
||||
|
||||
|
||||
class RiscVInput:
|
||||
"""
|
||||
Represents an Assembly file
|
||||
"""
|
||||
def __init__(self, content: str, name: str):
|
||||
self.content = content
|
||||
self.pos = 0
|
||||
self.len = len(content)
|
||||
self.name = name
|
||||
|
||||
@staticmethod
|
||||
def from_file(src: str):
|
||||
with open(src, 'r') as f:
|
||||
return RiscVInput(f.read(), src)
|
||||
|
||||
def peek(self, offset: int = 0, size: int = 1, regex: re.Pattern = None, text: str = None, regex_group: int = 0):
|
||||
at = self.pos + offset
|
||||
|
||||
if regex:
|
||||
if not isinstance(regex, re.Pattern):
|
||||
print("uncompiled regex passed to peek!")
|
||||
regex = re.compile(regex)
|
||||
match = regex.match(self.content[at:])
|
||||
if match is None:
|
||||
return None
|
||||
|
||||
if regex_group != 0 and not match.group(0).startswith(match.group(regex_group)):
|
||||
print("Cannot peek regex group that does not start at match start!")
|
||||
return None
|
||||
return match.group(regex_group)
|
||||
if text:
|
||||
if self.content[at:].startswith(text):
|
||||
return self.content[at:at + len(text)]
|
||||
return False
|
||||
return self.content[at:at + size]
|
||||
|
||||
def peek_one_of(self, options: List[str]):
|
||||
longest_peek = 0
|
||||
ret = False
|
||||
for text in options:
|
||||
if self.peek(text=text):
|
||||
if len(text) > longest_peek:
|
||||
longest_peek = len(text)
|
||||
ret = text
|
||||
return ret
|
||||
|
||||
def consume(self, size: int = 1, regex: re.Pattern = None, text: str = None, regex_group: int = 0):
|
||||
at = self.pos
|
||||
|
||||
if regex:
|
||||
if not isinstance(regex, re.Pattern):
|
||||
print("uncompiled regex passed to peek!")
|
||||
regex = re.compile(regex)
|
||||
match = regex.match(self.content[at:])
|
||||
if match is None:
|
||||
return None
|
||||
|
||||
if regex_group != 0 and not match.group(0).startswith(match.group(regex_group)):
|
||||
print("Cannot consume regex group that does not start at match start!")
|
||||
return None
|
||||
self.pos += len(match.group(regex_group))
|
||||
return match.group(regex_group)
|
||||
|
||||
if text:
|
||||
if self.content[at:].startswith(text):
|
||||
self.pos += len(text)
|
||||
return text
|
||||
return None
|
||||
|
||||
self.pos += size
|
||||
return self.content[at:at + size]
|
||||
|
||||
def consume_one_of(self, options: List[str]):
|
||||
longest_peek = 0
|
||||
ret = False
|
||||
for text in options:
|
||||
if self.peek(text=text):
|
||||
if len(text) > longest_peek:
|
||||
longest_peek = len(text)
|
||||
ret = text
|
||||
self.consume(text=ret)
|
||||
return ret
|
||||
|
||||
def seek_newline(self):
|
||||
return self.consume(regex=REG_WHITESPACE_UNTIL_NEWLINE, regex_group=1)
|
||||
|
||||
def consume_whitespace(self, linebreak=True):
|
||||
if linebreak:
|
||||
return self.consume(regex=REG_WHITESPACE)
|
||||
return self.consume(regex=REG_WHITESPACE_NO_LINEBREAK)
|
||||
|
||||
def has_next(self):
|
||||
return self.pos < self.len
|
||||
|
||||
def context(self, size: int = 5):
|
||||
"""
|
||||
returns a context string:
|
||||
<local input before pos>|<local input after pos>
|
||||
"""
|
||||
start = max(self.pos - size, 0)
|
||||
end = min(self.pos + size, self.len - 1)
|
||||
|
||||
return self.content[start:self.pos] + '|' + self.content[self.pos:end]
|
||||
|
||||
|
||||
class TokenType(IntEnum):
|
||||
SYMBOL = 0
|
||||
INSTRUCTION = 1
|
||||
PSEUDO_OP = 2
|
||||
|
||||
def __repr__(self):
|
||||
return self.name
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
class RiscVToken:
|
||||
type: TokenType
|
||||
|
||||
def __init__(self, t_type: TokenType):
|
||||
self.type = t_type
|
||||
|
||||
def __repr__(self):
|
||||
return "{}[{}]({})".format(self.__class__.__name__, self.type, self.text())
|
||||
|
||||
def text(self):
|
||||
"""
|
||||
create text representation of instruction
|
||||
"""
|
||||
return "unknown"
|
||||
|
||||
|
||||
class RiscVInstructionToken(RiscVToken):
|
||||
def __init__(self, name, args):
|
||||
super().__init__(TokenType.INSTRUCTION)
|
||||
self.instruction = name
|
||||
self.args = args
|
||||
|
||||
def text(self):
|
||||
if len(self.args) == 0:
|
||||
return self.instruction
|
||||
if len(self.args) == 1:
|
||||
return "{} {}".format(self.instruction, self.args[0])
|
||||
if len(self.args) == 2:
|
||||
return "{} {}, {}".format(self.instruction, *self.args)
|
||||
return "{} {}, {}, {}".format(self.instruction, *self.args)
|
||||
|
||||
|
||||
class RiscVSymbolToken(RiscVToken):
|
||||
def __init__(self, name):
|
||||
super().__init__(TokenType.SYMBOL)
|
||||
self.name = name
|
||||
|
||||
def text(self):
|
||||
return self.name
|
||||
|
||||
|
||||
class RiscVPseudoOpToken(RiscVToken):
|
||||
def __init__(self, name, args):
|
||||
super().__init__(TokenType.PSEUDO_OP)
|
||||
self.name = name
|
||||
self.args = args
|
||||
|
||||
def text(self):
|
||||
return "{} {}".format(self.name, self.args)
|
||||
|
||||
|
||||
class RiscVTokenizer:
|
||||
"""
|
||||
A tokenizer for the RISC-V syntax of a given CPU
|
||||
"""
|
||||
def __init__(self, input_assembly: RiscVInput, instructions: List[str]):
|
||||
self.input = input_assembly
|
||||
self.tokens: List[RiscVToken] = []
|
||||
self.name = input_assembly.name
|
||||
self.instructions = instructions
|
||||
|
||||
def tokenize(self):
|
||||
while self.input.has_next():
|
||||
# remove leading whitespaces, place cursor at text start
|
||||
self.input.consume_whitespace()
|
||||
|
||||
# check if we have a pseudo op
|
||||
if self.input.peek_one_of(PSEUDO_OPS):
|
||||
self.parse_pseudo_op()
|
||||
|
||||
# check if we have a symbol (like main:)
|
||||
elif self.input.peek(regex=REG_VALID_SYMBOL_LABEL):
|
||||
self.parse_symbol()
|
||||
|
||||
# comment
|
||||
elif self.input.peek() in COMMENT_START:
|
||||
self.parse_comment()
|
||||
|
||||
# must be instruction
|
||||
elif self.input.peek_one_of(self.instructions):
|
||||
self.parse_instruction()
|
||||
else:
|
||||
token = self.input.peek(size=5)
|
||||
raise ParseException("Unknown token around {} at: {}".format(repr(token), repr(self.input.context())))
|
||||
self.input.consume_whitespace()
|
||||
|
||||
def parse_pseudo_op(self):
|
||||
name = self.input.consume_one_of(PSEUDO_OPS)
|
||||
self.input.consume_whitespace(linebreak=False)
|
||||
|
||||
arg_str = self.input.consume(regex=REG_UNTIL_NEWLINE)
|
||||
if not arg_str:
|
||||
args = []
|
||||
else:
|
||||
args = split_accepting_quotes(arg_str)
|
||||
|
||||
self.tokens.append(RiscVPseudoOpToken(name[1:], args))
|
||||
|
||||
def parse_symbol(self):
|
||||
name = self.input.consume(regex=REG_VALID_SYMBOL_LABEL)
|
||||
self.tokens.append(RiscVSymbolToken(name[:-1]))
|
||||
if not self.input.consume_whitespace():
|
||||
print("[Tokenizer] symbol declaration should always be followed by whitespace (at {})!".format(
|
||||
self.input.context()))
|
||||
|
||||
def parse_instruction(self):
|
||||
ins = self.input.consume_one_of(self.instructions)
|
||||
args = []
|
||||
self.input.consume_whitespace(linebreak=False)
|
||||
while self.input.peek(regex=REG_VALID_ARGUMENT) and len(args) < 3:
|
||||
arg = self.input.consume(regex=REG_VALID_ARGUMENT)
|
||||
args.append(arg)
|
||||
if self.input.peek(text=','):
|
||||
self.input.consume(text=',')
|
||||
self.input.consume_whitespace(linebreak=False)
|
||||
else:
|
||||
break
|
||||
self.tokens.append(RiscVInstructionToken(ins, args))
|
||||
|
||||
def parse_comment(self):
|
||||
# just consume the rest
|
||||
self.input.consume(regex=REG_UNTIL_NEWLINE)
|
@ -0,0 +1,214 @@
|
||||
from enum import Enum, auto
|
||||
from typing import List
|
||||
from typing import Optional, Tuple, Union
|
||||
|
||||
from .colors import FMT_PARSE, FMT_NONE
|
||||
from riscemu.types.exceptions import ParseException, ASSERT_LEN
|
||||
from .helpers import parse_numeric_argument, align_addr, get_section_base_name
|
||||
from .tokenizer import Token
|
||||
from .types import Program, T_RelativeAddress, InstructionContext, Instruction, BinaryDataMemorySection, \
|
||||
InstructionMemorySection, Int32
|
||||
|
||||
INSTRUCTION_SECTION_NAMES = ('.text', '.init', '.fini')
|
||||
"""
|
||||
A tuple containing all section names which contain executable code (instead of data)
|
||||
|
||||
The first segment of each segment (first segment of ".text.main" is ".text") is checked
|
||||
against this list to determine the type of it.
|
||||
"""
|
||||
|
||||
|
||||
class MemorySectionType(Enum):
|
||||
Data = auto()
|
||||
Instructions = auto()
|
||||
|
||||
|
||||
class CurrentSection:
|
||||
name: str
|
||||
data: Union[List[Instruction], bytearray]
|
||||
type: MemorySectionType
|
||||
base: int
|
||||
|
||||
def __init__(self, name: str, type: MemorySectionType, base: int = 0):
|
||||
self.name = name
|
||||
self.type = type
|
||||
self.base = base
|
||||
if self.type == MemorySectionType.Data:
|
||||
self.data = bytearray()
|
||||
elif self.type == MemorySectionType.Instructions:
|
||||
self.data = list()
|
||||
else:
|
||||
raise ParseException("Unknown section type: {}".format(type))
|
||||
|
||||
def current_address(self) -> T_RelativeAddress:
|
||||
if self.type == MemorySectionType.Data:
|
||||
return len(self.data) + self.base
|
||||
return len(self.data) * 4 + self.base
|
||||
|
||||
def __repr__(self):
|
||||
return "{}(name={},data={},type={})".format(
|
||||
self.__class__.__name__, self.name,
|
||||
self.data, self.type.name
|
||||
)
|
||||
|
||||
|
||||
class ParseContext:
|
||||
section: Optional[CurrentSection]
|
||||
context: InstructionContext
|
||||
program: Program
|
||||
|
||||
def __init__(self, name: str):
|
||||
self.program = Program(name)
|
||||
self.context = self.program.context
|
||||
self.section = None
|
||||
|
||||
def finalize(self) -> Program:
|
||||
self._finalize_section()
|
||||
return self.program
|
||||
|
||||
def _finalize_section(self):
|
||||
if self.section is None:
|
||||
return
|
||||
if self.section.type == MemorySectionType.Data:
|
||||
section = BinaryDataMemorySection(
|
||||
self.section.data, self.section.name, self.context, self.program.name, self.section.base
|
||||
)
|
||||
self.program.add_section(section)
|
||||
elif self.section.type == MemorySectionType.Instructions:
|
||||
section = InstructionMemorySection(
|
||||
self.section.data, self.section.name, self.context, self.program.name, self.section.base
|
||||
)
|
||||
self.program.add_section(section)
|
||||
self.section = None
|
||||
|
||||
def new_section(self, name: str, type: MemorySectionType, alignment: int = 4):
|
||||
base = 0
|
||||
if self.section is not None:
|
||||
base = align_addr(self.section.current_address(), alignment)
|
||||
self._finalize_section()
|
||||
self.section = CurrentSection(name, type, base)
|
||||
|
||||
def add_label(self, name: str, value: int, is_global: bool = False, is_relative: bool = False):
|
||||
self.context.labels[name] = value
|
||||
if is_global:
|
||||
self.program.global_labels.add(name)
|
||||
if is_relative:
|
||||
self.program.relative_labels.add(name)
|
||||
|
||||
def __repr__(self):
|
||||
return "{}(\n\tsetion={},\n\tprogram={}\n)".format(
|
||||
self.__class__.__name__, self.section, self.program
|
||||
)
|
||||
|
||||
|
||||
def ASSERT_IN_SECTION_TYPE(context: ParseContext, type: MemorySectionType):
|
||||
if context.section is None:
|
||||
raise ParseException('Error, expected to be in {} section, but no section is present...'.format(type.name))
|
||||
if context.section.type != type:
|
||||
raise ParseException(
|
||||
'Error, expected to be in {} section, but currently in {}...'.format(type.name, context.section)
|
||||
)
|
||||
|
||||
|
||||
class AssemblerDirectives:
|
||||
"""
|
||||
This class represents a collection of all assembler directives as documented by
|
||||
https://github.com/riscv-non-isa/riscv-asm-manual/blob/master/riscv-asm.md#pseudo-ops
|
||||
|
||||
All class methods prefixed with op_ are directly used as assembler directives.
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def op_align(cls, token: Token, args: Tuple[str], context: ParseContext):
|
||||
ASSERT_LEN(args, 1)
|
||||
ASSERT_IN_SECTION_TYPE(context, MemorySectionType.Data)
|
||||
align_to = parse_numeric_argument(args[0])
|
||||
current_mod = context.section.current_address() % align_to
|
||||
if current_mod == 0:
|
||||
return
|
||||
context.section.data += bytearray(align_to - current_mod)
|
||||
|
||||
@classmethod
|
||||
def op_section(cls, token: Token, args: Tuple[str], context: ParseContext):
|
||||
ASSERT_LEN(args, 1)
|
||||
if get_section_base_name(args[0]) in INSTRUCTION_SECTION_NAMES:
|
||||
context.new_section(args[0], MemorySectionType.Instructions)
|
||||
else:
|
||||
context.new_section(args[0], MemorySectionType.Data)
|
||||
|
||||
@classmethod
|
||||
def op_globl(cls, token: Token, args: Tuple[str], context: ParseContext):
|
||||
ASSERT_LEN(args, 1)
|
||||
context.program.global_labels.add(args[0])
|
||||
|
||||
@classmethod
|
||||
def op_global(cls, token: Token, args: Tuple[str], context: ParseContext):
|
||||
cls.op_globl(token, args, context)
|
||||
|
||||
@classmethod
|
||||
def op_equ(cls, token: Token, args: Tuple[str], context: ParseContext):
|
||||
ASSERT_LEN(args, 2)
|
||||
name = args[0]
|
||||
value = parse_numeric_argument(args[1])
|
||||
context.context.labels[name] = value
|
||||
|
||||
@classmethod
|
||||
def op_space(cls, token: Token, args: Tuple[str], context: ParseContext):
|
||||
ASSERT_LEN(args, 1)
|
||||
ASSERT_IN_SECTION_TYPE(context, MemorySectionType.Data)
|
||||
|
||||
size = parse_numeric_argument(args[0])
|
||||
cls.add_bytes(size, None, context)
|
||||
|
||||
@classmethod
|
||||
def op_zero(cls, token: Token, args: Tuple[str], context: ParseContext):
|
||||
ASSERT_LEN(args, 1)
|
||||
ASSERT_IN_SECTION_TYPE(context, MemorySectionType.Data)
|
||||
size = parse_numeric_argument(args[0])
|
||||
cls.add_bytes(size, bytearray(size), context)
|
||||
|
||||
@classmethod
|
||||
def add_bytes(cls, size: int, content: Union[None, int, bytearray], context: ParseContext):
|
||||
ASSERT_IN_SECTION_TYPE(context, MemorySectionType.Data)
|
||||
|
||||
if content is None:
|
||||
content = bytearray(size)
|
||||
if isinstance(content, int):
|
||||
content = Int32(content).to_bytes(size)
|
||||
|
||||
context.section.data += content
|
||||
|
||||
@classmethod
|
||||
def add_text(cls, text: str, context: ParseContext, zero_terminate: bool = True):
|
||||
# replace '\t' and '\n' escape sequences
|
||||
text = text.replace('\\n', '\n').replace('\\t', '\t')
|
||||
|
||||
encoded_bytes = bytearray(text.encode('ascii'))
|
||||
if zero_terminate:
|
||||
encoded_bytes += bytearray(1)
|
||||
cls.add_bytes(len(encoded_bytes), encoded_bytes, context)
|
||||
|
||||
@classmethod
|
||||
def handle_instruction(cls, token: Token, args: Tuple[str], context: ParseContext):
|
||||
op = token.value[1:]
|
||||
if hasattr(cls, 'op_' + op):
|
||||
getattr(cls, 'op_' + op)(token, args, context)
|
||||
elif op in ('text', 'data', 'rodata', 'bss', 'sbss'):
|
||||
cls.op_section(token, (token.value,), context)
|
||||
elif op in ('string', 'asciiz', 'asciz', 'ascii'):
|
||||
ASSERT_LEN(args, 1)
|
||||
cls.add_text(args[0], context, op == 'ascii')
|
||||
elif op in DATA_OP_SIZES:
|
||||
size = DATA_OP_SIZES[op]
|
||||
for arg in args:
|
||||
cls.add_bytes(size, parse_numeric_argument(arg), context)
|
||||
else:
|
||||
print(FMT_PARSE + "Unknown assembler directive: {} {} in {}".format(token, args, context) + FMT_NONE)
|
||||
|
||||
|
||||
DATA_OP_SIZES = {
|
||||
'byte': 1,
|
||||
'2byte': 2, 'half': 2, 'short': 2,
|
||||
'4byte': 4, 'word': 4, 'long': 4,
|
||||
'8byte': 8, 'dword': 8, 'quad': 8,
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
from riscemu import RunConfig
|
||||
from riscemu.types import InstructionMemorySection, SimpleInstruction, Program
|
||||
|
||||
if __name__ == '__main__':
|
||||
from .CPU import UserModeCPU
|
||||
from .instructions import InstructionSetDict
|
||||
from .debug import launch_debug_session
|
||||
|
||||
cpu = UserModeCPU(list(InstructionSetDict.values()), RunConfig(verbosity=4))
|
||||
|
||||
program = Program('interactive session', base=0x100)
|
||||
context = program.context
|
||||
program.add_section(InstructionMemorySection([
|
||||
SimpleInstruction('ebreak', (), context, 0x100),
|
||||
SimpleInstruction('addi', ('a0', 'zero', '0'), context, 0x104),
|
||||
SimpleInstruction('addi', ('a7', 'zero', '93'), context, 0x108),
|
||||
SimpleInstruction('scall', (), context, 0x10C),
|
||||
], '.text', context, program.name, 0x100))
|
||||
|
||||
cpu.load_program(program)
|
||||
|
||||
cpu.setup_stack()
|
||||
|
||||
cpu.launch(program)
|
@ -0,0 +1,127 @@
|
||||
"""
|
||||
RiscEmu (c) 2021 Anton Lydike
|
||||
|
||||
SPDX-License-Identifier: MIT
|
||||
"""
|
||||
import re
|
||||
from typing import Dict, Tuple, Iterable, Callable, List
|
||||
|
||||
from .assembler import MemorySectionType, ParseContext, AssemblerDirectives
|
||||
from .colors import FMT_PARSE
|
||||
from .helpers import Peekable
|
||||
from .tokenizer import Token, TokenType, tokenize
|
||||
from .types import Program, T_ParserOpts, ProgramLoader, SimpleInstruction
|
||||
from .types.exceptions import ParseException
|
||||
|
||||
|
||||
def parse_instruction(token: Token, args: Tuple[str], context: ParseContext):
|
||||
if context.section is None or context.section.type != MemorySectionType.Instructions:
|
||||
raise ParseException("{} {} encountered in invalid context: {}".format(token, args, context))
|
||||
ins = SimpleInstruction(token.value, args, context.context, context.section.current_address())
|
||||
context.section.data.append(ins)
|
||||
|
||||
|
||||
def parse_label(token: Token, args: Tuple[str], context: ParseContext):
|
||||
name = token.value[:-1]
|
||||
if re.match(r'^\d+$', name):
|
||||
# relative label:
|
||||
context.context.numbered_labels[name].append(context.section.current_address())
|
||||
else:
|
||||
if name in context.context.labels:
|
||||
print(FMT_PARSE + 'Warn: Symbol {} defined twice!'.format(name))
|
||||
context.add_label(name, context.section.current_address(), is_relative=True)
|
||||
|
||||
|
||||
PARSERS: Dict[TokenType, Callable[[Token, Tuple[str], ParseContext], None]] = {
|
||||
TokenType.PSEUDO_OP: AssemblerDirectives.handle_instruction,
|
||||
TokenType.LABEL: parse_label,
|
||||
TokenType.INSTRUCTION_NAME: parse_instruction
|
||||
}
|
||||
|
||||
|
||||
def parse_tokens(name: str, tokens_iter: Iterable[Token]) -> Program:
|
||||
"""
|
||||
Convert a token stream into a parsed program
|
||||
:param name: the programs name
|
||||
:param tokens_iter: the programs content, tokenized
|
||||
:return: a parsed program
|
||||
"""
|
||||
context = ParseContext(name)
|
||||
|
||||
for token, args in composite_tokenizer(Peekable[Token](tokens_iter)):
|
||||
if token.type not in PARSERS:
|
||||
raise ParseException("Unexpected token type: {}, {}".format(token, args))
|
||||
PARSERS[token.type](token, args, context)
|
||||
|
||||
return context.finalize()
|
||||
|
||||
|
||||
def composite_tokenizer(tokens_iter: Iterable[Token]) -> Iterable[Tuple[Token, Tuple[str]]]:
|
||||
"""
|
||||
Convert an iterator over tokens into an iterator over tuples: (token, list(token))
|
||||
|
||||
The first token ist either a pseudo_op, label, or instruction name. The token list are all remaining tokens before
|
||||
a newline is encountered
|
||||
:param tokens_iter: An iterator over tokens
|
||||
:return: An iterator over a slightly more structured representation of the tokens
|
||||
"""
|
||||
tokens: Peekable[Token] = Peekable[Token](tokens_iter)
|
||||
|
||||
while not tokens.is_empty():
|
||||
token = next(tokens)
|
||||
if token.type in (TokenType.PSEUDO_OP, TokenType.LABEL, TokenType.INSTRUCTION_NAME):
|
||||
yield token, tuple(take_arguments(tokens))
|
||||
|
||||
|
||||
def take_arguments(tokens: Peekable[Token]) -> Iterable[str]:
|
||||
"""
|
||||
Consumes (argument comma)* and yields argument.value until newline is reached
|
||||
If an argument is not followed by either a newline or a comma, a parse exception is raised
|
||||
The newline at the end is consumed
|
||||
:param tokens: A Peekable iterator over some Tokens
|
||||
"""
|
||||
while True:
|
||||
if tokens.peek().type == TokenType.ARGUMENT:
|
||||
yield next(tokens).value
|
||||
elif tokens.peek().type == TokenType.NEWLINE:
|
||||
next(tokens)
|
||||
break
|
||||
elif tokens.peek().type == TokenType.COMMA:
|
||||
next(tokens)
|
||||
else:
|
||||
break
|
||||
|
||||
# raise ParseException("Expected newline, instead got {}".format(tokens.peek()))
|
||||
|
||||
|
||||
class AssemblyFileLoader(ProgramLoader):
|
||||
"""
|
||||
This class loads assembly files written by hand. It understands some assembler directives and supports most
|
||||
pseudo instructions. It does very little verification of source correctness.
|
||||
|
||||
It also supports numbered jump targets and properly supports local and global scope (.globl assembly directive)
|
||||
|
||||
|
||||
The AssemblyFileLoader loads .asm, .S and .s files by default, and acts as a weak fallback to all other filetypes.
|
||||
"""
|
||||
def parse(self) -> Program:
|
||||
with open(self.source_path, 'r') as f:
|
||||
return parse_tokens(self.filename, tokenize(f))
|
||||
|
||||
@classmethod
|
||||
def can_parse(cls, source_path: str) -> float:
|
||||
"""
|
||||
|
||||
It also acts as a weak fallback if no other loaders want to take the file.
|
||||
|
||||
:param source_path: the path to the source file
|
||||
:return:
|
||||
"""
|
||||
# gcc recognizes these line endings as assembly. So we will do too.
|
||||
if source_path.split('.')[-1] in ('asm', 'S', 's'):
|
||||
return 1
|
||||
return 0.01
|
||||
|
||||
@classmethod
|
||||
def get_options(cls, argv: list[str]) -> [List[str], T_ParserOpts]:
|
||||
return argv, {}
|
@ -1,42 +1,43 @@
|
||||
from .types import ElfMemorySection
|
||||
from ..MMU import *
|
||||
from abc import abstractmethod
|
||||
|
||||
import typing
|
||||
|
||||
from .ElfLoader import ElfExecutable
|
||||
|
||||
if typing.TYPE_CHECKING:
|
||||
from .PrivCPU import PrivCPU
|
||||
|
||||
|
||||
class PrivMMU(MMU):
|
||||
cpu: 'PrivCPU'
|
||||
|
||||
@abstractmethod
|
||||
def get_entrypoint(self) -> int:
|
||||
raise
|
||||
|
||||
def set_cpu(self, cpu: 'PrivCPU'):
|
||||
self.cpu = cpu
|
||||
def get_sec_containing(self, addr: T_AbsoluteAddress) -> MemorySection:
|
||||
# try to get an existing section
|
||||
existing_sec = super().get_sec_containing(addr)
|
||||
|
||||
def translate_address(self, addr: int):
|
||||
return ""
|
||||
if existing_sec is not None:
|
||||
return existing_sec
|
||||
|
||||
# get section preceding empty space at addr
|
||||
sec_before = next((sec for sec in reversed(self.sections) if sec.end < addr), None)
|
||||
# get sec succeeding empty space at addr
|
||||
sec_after = next((sec for sec in self.sections if sec.base > addr), None)
|
||||
|
||||
class LoadedElfMMU(PrivMMU):
|
||||
def __init__(self, elf: ElfExecutable):
|
||||
super().__init__(conf=RunConfig())
|
||||
self.entrypoint = elf.symbols['_start']
|
||||
# calc start end end of "free" space
|
||||
prev_sec_end = 0 if sec_before is None else sec_before.end
|
||||
next_sec_start = 0x7FFFFFFF if sec_after is None else sec_after.base
|
||||
|
||||
self.binaries.append(elf)
|
||||
for sec in elf.sections:
|
||||
self.sections.append(sec)
|
||||
# start at the end of the prev section, or current address - 0xFFFF (aligned to 16 byte boundary)
|
||||
start = max(prev_sec_end, align_addr(addr - 0xFFFF, 16))
|
||||
# end at the start of the next section, or address + 0xFFFF (aligned to 16 byte boundary)
|
||||
end = min(next_sec_start, align_addr(addr + 0xFFFF, 16))
|
||||
|
||||
def load_bin(self, exe: Executable) -> LoadedExecutable:
|
||||
raise NotImplementedError("This is a privMMU, it's initialized with a single ElfExecutable!")
|
||||
sec = ElfMemorySection(bytearray(end - start), '.empty', self.global_instruction_context(), '', start, MemoryFlags(False, True))
|
||||
self.sections.append(sec)
|
||||
self._update_state()
|
||||
|
||||
def allocate_section(self, name: str, req_size: int, flag: MemoryFlags):
|
||||
raise NotImplementedError("Not supported!")
|
||||
return sec
|
||||
|
||||
def get_entrypoint(self):
|
||||
return self.entrypoint
|
||||
def global_instruction_context(self) -> InstructionContext:
|
||||
context = InstructionContext()
|
||||
context.global_symbol_dict = self.global_symbols
|
||||
return context
|
@ -0,0 +1,147 @@
|
||||
import json
|
||||
from collections import defaultdict
|
||||
from dataclasses import dataclass
|
||||
from functools import lru_cache
|
||||
from typing import Tuple, Dict, Set
|
||||
|
||||
from riscemu.colors import FMT_NONE, FMT_PARSE
|
||||
from riscemu.decoder import format_ins, RISCV_REGS, decode
|
||||
from riscemu.priv.Exceptions import InstructionAccessFault, InstructionAddressMisalignedTrap, LoadAccessFault
|
||||
from riscemu.types import Instruction, InstructionContext, T_RelativeAddress, MemoryFlags, T_AbsoluteAddress, \
|
||||
BinaryDataMemorySection
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ElfInstruction(Instruction):
|
||||
name: str
|
||||
args: Tuple[int]
|
||||
encoded: int
|
||||
|
||||
def get_imm(self, num: int) -> int:
|
||||
return self.args[num]
|
||||
|
||||
def get_imm_reg(self, num: int) -> Tuple[int, int]:
|
||||
return self.args[-1], self.args[-2]
|
||||
|
||||
def get_reg(self, num: int) -> str:
|
||||
return RISCV_REGS[self.args[num]]
|
||||
|
||||
def __repr__(self) -> str:
|
||||
if self.name == 'jal' and self.args[0] == 0:
|
||||
return "j {}".format(self.args[1])
|
||||
if self.name == 'addi' and self.args[2] == 0:
|
||||
return "mv {}, {}".format(self.get_reg(0), self.get_reg(1))
|
||||
if self.name == 'addi' and self.args[1] == 0:
|
||||
return "li {}, {}".format(self.get_reg(0), self.args[2])
|
||||
if self.name == 'ret' and len(self.args) == 0:
|
||||
return "ret"
|
||||
return format_ins(self.encoded, self.name)
|
||||
|
||||
|
||||
class ElfMemorySection(BinaryDataMemorySection):
|
||||
def __init__(self, data: bytearray, name: str, context: InstructionContext, owner: str, base: int,
|
||||
flags: MemoryFlags):
|
||||
super().__init__(data, name, context, owner, base=base, flags=flags)
|
||||
self.read_ins = lru_cache(maxsize=self.size // 4)(self.read_ins)
|
||||
|
||||
def read_ins(self, offset):
|
||||
if not self.flags.executable:
|
||||
print(FMT_PARSE + "Reading instruction from non-executable memory!" + FMT_NONE)
|
||||
raise InstructionAccessFault(offset + self.base)
|
||||
if offset % 4 != 0:
|
||||
raise InstructionAddressMisalignedTrap(offset + self.base)
|
||||
return ElfInstruction(*decode(self.data[offset:offset + 4]))
|
||||
|
||||
def write(self, offset: T_RelativeAddress, size: int, data: bytearray):
|
||||
if self.flags.read_only:
|
||||
raise LoadAccessFault('read-only section', offset + self.base, size, 'write')
|
||||
self.read_ins.cache_clear()
|
||||
return super(ElfMemorySection, self).write(offset, size, data)
|
||||
|
||||
@property
|
||||
def end(self):
|
||||
return self.size + self.base
|
||||
|
||||
|
||||
class MemoryImageDebugInfos:
|
||||
VERSION = '1.0.0'
|
||||
"""
|
||||
Schema version
|
||||
"""
|
||||
|
||||
base: T_AbsoluteAddress = 0
|
||||
"""
|
||||
The base address where the image starts. Defaults to zero.
|
||||
"""
|
||||
|
||||
sections: Dict[str, Dict[str, Tuple[int, int]]]
|
||||
"""
|
||||
This dictionary maps a program and section to (start address, section length)
|
||||
"""
|
||||
|
||||
symbols: Dict[str, Dict[str, int]]
|
||||
"""
|
||||
This dictionary maps a program and a symbol to a value
|
||||
"""
|
||||
|
||||
globals: Dict[str, Set[str]]
|
||||
"""
|
||||
This dictionary contains the list of all global symbols of a given program
|
||||
"""
|
||||
|
||||
def __init__(self,
|
||||
sections: Dict[str, Dict[str, Tuple[int, int]]],
|
||||
symbols: Dict[str, Dict[str, int]],
|
||||
globals: Dict[str, Set[str]],
|
||||
base: int = 0
|
||||
):
|
||||
self.sections = sections
|
||||
self.symbols = symbols
|
||||
self.globals = globals
|
||||
for name in globals:
|
||||
globals[name] = set(globals[name])
|
||||
self.base = base
|
||||
|
||||
def serialize(self) -> str:
|
||||
def serialize(obj: any) -> str:
|
||||
if isinstance(obj, defaultdict):
|
||||
return json.dumps(dict(obj), default=serialize)
|
||||
if isinstance(obj, (set, tuple)):
|
||||
return json.dumps(list(obj), default=serialize)
|
||||
return "<<unserializable {}>>".format(getattr(obj, '__qualname__', '{unknown}'))
|
||||
|
||||
return json.dumps(
|
||||
dict(
|
||||
sections=self.sections,
|
||||
symbols=self.symbols,
|
||||
globals=self.globals,
|
||||
base=self.base,
|
||||
VERSION=self.VERSION
|
||||
),
|
||||
default=serialize
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def load(cls, serialized_str: str) -> 'MemoryImageDebugInfos':
|
||||
json_obj: dict = json.loads(serialized_str)
|
||||
|
||||
if 'VERSION' not in json_obj:
|
||||
raise RuntimeError("Unknown MemoryImageDebugInfo version!")
|
||||
|
||||
version: str = json_obj.pop('VERSION')
|
||||
|
||||
# compare major version
|
||||
if version != cls.VERSION and version.split('.')[0] != cls.VERSION.split('.')[0]:
|
||||
raise RuntimeError(
|
||||
"Unknown MemoryImageDebugInfo version! This emulator expects version {}, debug info version {}".format(
|
||||
cls.VERSION, version
|
||||
)
|
||||
)
|
||||
|
||||
return MemoryImageDebugInfos(**json_obj)
|
||||
|
||||
@classmethod
|
||||
def builder(cls) -> 'MemoryImageDebugInfos':
|
||||
return MemoryImageDebugInfos(
|
||||
defaultdict(dict), defaultdict(dict), defaultdict(set)
|
||||
)
|
@ -0,0 +1,135 @@
|
||||
"""
|
||||
RiscEmu (c) 2021 Anton Lydike
|
||||
|
||||
SPDX-License-Identifier: MIT
|
||||
"""
|
||||
|
||||
import re
|
||||
from dataclasses import dataclass
|
||||
from enum import Enum, auto
|
||||
from typing import List, Iterable
|
||||
|
||||
from riscemu.decoder import RISCV_REGS
|
||||
from riscemu.types.exceptions import ParseException
|
||||
|
||||
LINE_COMMENT_STARTERS = ('#', ';', '//')
|
||||
WHITESPACE_PATTERN = re.compile(r'\s+')
|
||||
MEMORY_ADDRESS_PATTERN = re.compile(r'^(0[xX][A-f0-9]+|\d+|0b[0-1]+|[A-z0-9_-]+)\(([A-z]+[0-9]{0,2})\)$')
|
||||
REGISTER_NAMES = RISCV_REGS
|
||||
|
||||
|
||||
class TokenType(Enum):
|
||||
COMMA = auto()
|
||||
ARGUMENT = auto()
|
||||
PSEUDO_OP = auto()
|
||||
INSTRUCTION_NAME = auto()
|
||||
NEWLINE = auto()
|
||||
LABEL = auto()
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Token:
|
||||
type: TokenType
|
||||
value: str
|
||||
|
||||
def __str__(self):
|
||||
if self.type == TokenType.NEWLINE:
|
||||
return '\\n'
|
||||
if self.type == TokenType.COMMA:
|
||||
return ', '
|
||||
return '{}({})'.format(self.type.name[0:3], self.value)
|
||||
|
||||
|
||||
NEWLINE = Token(TokenType.NEWLINE, '\n')
|
||||
COMMA = Token(TokenType.COMMA, ',')
|
||||
|
||||
|
||||
def tokenize(input: Iterable[str]) -> Iterable[Token]:
|
||||
for line in input:
|
||||
for line_comment_start in LINE_COMMENT_STARTERS:
|
||||
if line_comment_start in line:
|
||||
line = line[:line.index(line_comment_start)]
|
||||
line.strip(' \t\n')
|
||||
if not line:
|
||||
continue
|
||||
|
||||
parts = list(part for part in split_whitespace_respecting_quotes(line) if part)
|
||||
|
||||
yield from parse_line(parts)
|
||||
yield NEWLINE
|
||||
|
||||
|
||||
def parse_line(parts: List[str]) -> Iterable[Token]:
|
||||
if len(parts) == 0:
|
||||
return ()
|
||||
first_token = parts[0]
|
||||
|
||||
if first_token[0] == '.':
|
||||
yield Token(TokenType.PSEUDO_OP, first_token)
|
||||
elif first_token[-1] == ':':
|
||||
yield Token(TokenType.LABEL, first_token)
|
||||
yield from parse_line(parts[1:])
|
||||
return
|
||||
else:
|
||||
yield Token(TokenType.INSTRUCTION_NAME, first_token)
|
||||
|
||||
for part in parts[1:]:
|
||||
if part == ',':
|
||||
yield COMMA
|
||||
continue
|
||||
yield from parse_arg(part)
|
||||
|
||||
|
||||
def parse_arg(arg: str) -> Iterable[Token]:
|
||||
comma = arg[-1] == ','
|
||||
arg = arg[:-1] if comma else arg
|
||||
mem_match_resul = re.match(MEMORY_ADDRESS_PATTERN, arg)
|
||||
if mem_match_resul:
|
||||
register = mem_match_resul.group(2).lower()
|
||||
if register not in RISCV_REGS:
|
||||
raise ParseException(f'"{register}" is not a valid register!')
|
||||
yield Token(TokenType.ARGUMENT, register)
|
||||
yield Token(TokenType.ARGUMENT, mem_match_resul.group(1))
|
||||
else:
|
||||
yield Token(TokenType.ARGUMENT, arg)
|
||||
if comma:
|
||||
yield COMMA
|
||||
|
||||
|
||||
def print_tokens(tokens: Iterable[Token]):
|
||||
for token in tokens:
|
||||
print(token, end='\n' if token == NEWLINE else '')
|
||||
print("", flush=True, end="")
|
||||
|
||||
|
||||
def split_whitespace_respecting_quotes(line: str) -> Iterable[str]:
|
||||
quote = ""
|
||||
part = ""
|
||||
for c in line:
|
||||
if c == quote:
|
||||
yield part
|
||||
part = ""
|
||||
quote = ""
|
||||
continue
|
||||
|
||||
if quote != "":
|
||||
part += c
|
||||
continue
|
||||
|
||||
if c in "\"'":
|
||||
if part:
|
||||
yield part
|
||||
quote = c
|
||||
part = ""
|
||||
continue
|
||||
|
||||
if c in ' \t\n':
|
||||
if part:
|
||||
yield part
|
||||
part = ""
|
||||
continue
|
||||
|
||||
part += c
|
||||
|
||||
if part:
|
||||
yield part
|
@ -0,0 +1,29 @@
|
||||
from typing import Dict
|
||||
import re
|
||||
|
||||
# define some base type aliases so we can keep track of absolute and relative addresses
|
||||
T_RelativeAddress = int
|
||||
T_AbsoluteAddress = int
|
||||
|
||||
# parser options are just dictionaries with arbitrary values
|
||||
T_ParserOpts = Dict[str, any]
|
||||
|
||||
NUMBER_SYMBOL_PATTERN = re.compile(r'^\d+[fb]$')
|
||||
|
||||
# base classes
|
||||
from .flags import MemoryFlags
|
||||
from .int32 import UInt32, Int32
|
||||
from .instruction import Instruction
|
||||
from .instruction_context import InstructionContext
|
||||
from .memory_section import MemorySection
|
||||
from .program import Program
|
||||
from .program_loader import ProgramLoader
|
||||
from .cpu import CPU
|
||||
from .simple_instruction import SimpleInstruction
|
||||
from .instruction_memory_section import InstructionMemorySection
|
||||
from .binary_data_memory_section import BinaryDataMemorySection
|
||||
|
||||
# exceptions
|
||||
from .exceptions import ParseException, NumberFormatException, MemoryAccessException, OutOfMemoryException, \
|
||||
LinkerException, LaunchDebuggerException, RiscemuBaseException, InvalidRegisterException, \
|
||||
InvalidAllocationException, InvalidSyscallException, UnimplementedInstruction
|
@ -0,0 +1,29 @@
|
||||
from . import MemorySection, InstructionContext, MemoryFlags, T_RelativeAddress, Instruction
|
||||
from ..types.exceptions import MemoryAccessException
|
||||
|
||||
|
||||
class BinaryDataMemorySection(MemorySection):
|
||||
def __init__(self, data: bytearray, name: str, context: InstructionContext, owner: str, base: int = 0, flags: MemoryFlags = None):
|
||||
self.name = name
|
||||
self.base = base
|
||||
self.context = context
|
||||
self.size = len(data)
|
||||
self.flags = flags if flags is not None else MemoryFlags(False, False)
|
||||
self.data = data
|
||||
self.owner = owner
|
||||
|
||||
def read(self, offset: T_RelativeAddress, size: int) -> bytearray:
|
||||
if offset + size > self.size:
|
||||
raise MemoryAccessException("Out of bounds access in {}".format(self), offset, size, 'read')
|
||||
return self.data[offset:offset + size]
|
||||
|
||||
def write(self, offset: T_RelativeAddress, size: int, data: bytearray):
|
||||
if offset + size > self.size:
|
||||
raise MemoryAccessException("Out of bounds access in {}".format(self), offset, size, 'write')
|
||||
if len(data[0:size]) != size:
|
||||
raise MemoryAccessException("Invalid write parameter sizing", offset, size, 'write')
|
||||
self.data[offset:offset + size] = data[0:size]
|
||||
|
||||
def read_ins(self, offset: T_RelativeAddress) -> Instruction:
|
||||
raise MemoryAccessException("Tried reading instruction on non-executable section {}".format(self),
|
||||
offset, 4, 'instruction fetch')
|
@ -0,0 +1,107 @@
|
||||
import typing
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import List, Type, Callable, Set, Dict
|
||||
|
||||
from ..registers import Registers
|
||||
from ..config import RunConfig
|
||||
from ..colors import FMT_RED, FMT_NONE
|
||||
from . import T_AbsoluteAddress, Instruction, Program, ProgramLoader
|
||||
|
||||
|
||||
class CPU(ABC):
|
||||
# static cpu configuration
|
||||
INS_XLEN: int = 4
|
||||
|
||||
# housekeeping variables
|
||||
regs: Registers
|
||||
mmu: 'MMU'
|
||||
pc: T_AbsoluteAddress
|
||||
cycle: int
|
||||
halted: bool
|
||||
|
||||
# debugging context
|
||||
debugger_active: bool
|
||||
|
||||
# instruction information
|
||||
instructions: Dict[str, Callable[[Instruction], None]]
|
||||
instruction_sets: Set['InstructionSet']
|
||||
|
||||
# configuration
|
||||
conf: RunConfig
|
||||
|
||||
def __init__(self, mmu: 'MMU', instruction_sets: List[Type['InstructionSet']], conf: RunConfig):
|
||||
self.mmu = mmu
|
||||
self.regs = Registers()
|
||||
self.conf = conf
|
||||
|
||||
self.instruction_sets = set()
|
||||
self.instructions = dict()
|
||||
|
||||
for set_class in instruction_sets:
|
||||
ins_set = set_class(self)
|
||||
self.instructions.update(ins_set.load())
|
||||
self.instruction_sets.add(ins_set)
|
||||
|
||||
self.halted = False
|
||||
self.cycle = 0
|
||||
self.pc = 0
|
||||
self.debugger_active = False
|
||||
|
||||
def run_instruction(self, ins: Instruction):
|
||||
"""
|
||||
Execute a single instruction
|
||||
|
||||
:param ins: The instruction to execute
|
||||
"""
|
||||
if ins.name in self.instructions:
|
||||
self.instructions[ins.name](ins)
|
||||
else:
|
||||
# this should never be reached, as unknown instructions are imparseable
|
||||
raise RuntimeError("Unknown instruction: {}".format(ins))
|
||||
|
||||
def load_program(self, program: Program):
|
||||
self.mmu.load_program(program)
|
||||
|
||||
def __repr__(self):
|
||||
"""
|
||||
Returns a representation of the CPU and some of its state.
|
||||
"""
|
||||
return "{}(pc=0x{:08X}, cycle={}, halted={} instructions={})".format(
|
||||
self.__class__.__name__,
|
||||
self.pc,
|
||||
self.cycle,
|
||||
self.halted,
|
||||
" ".join(s.name for s in self.instruction_sets)
|
||||
)
|
||||
|
||||
@abstractmethod
|
||||
def step(self, verbose=False):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def run(self, verbose=False):
|
||||
pass
|
||||
|
||||
def launch(self, program: Program, verbose: bool = False):
|
||||
if program not in self.mmu.programs:
|
||||
print(FMT_RED + '[CPU] Cannot launch program that\'s not loaded!' + FMT_NONE)
|
||||
return
|
||||
|
||||
self.pc = program.entrypoint
|
||||
self.run(verbose)
|
||||
|
||||
@classmethod
|
||||
@abstractmethod
|
||||
def get_loaders(cls) -> typing.Iterable[Type[ProgramLoader]]:
|
||||
pass
|
||||
|
||||
def get_best_loader_for(self, file_name: str) -> Type[ProgramLoader]:
|
||||
return max(self.get_loaders(), key=lambda ld: ld.can_parse(file_name))
|
||||
|
||||
@property
|
||||
def sections(self):
|
||||
return self.mmu.sections
|
||||
|
||||
@property
|
||||
def programs(self):
|
||||
return self.mmu.programs
|
@ -0,0 +1,13 @@
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class MemoryFlags:
|
||||
read_only: bool
|
||||
executable: bool
|
||||
|
||||
def __repr__(self):
|
||||
return "r{}{}".format(
|
||||
'-' if self.read_only else 'w',
|
||||
'x' if self.executable else '-'
|
||||
)
|
@ -0,0 +1,31 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Tuple
|
||||
|
||||
|
||||
class Instruction(ABC):
|
||||
name: str
|
||||
args: tuple
|
||||
|
||||
@abstractmethod
|
||||
def get_imm(self, num: int) -> int:
|
||||
"""
|
||||
parse and get immediate argument
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def get_imm_reg(self, num: int) -> Tuple[int, str]:
|
||||
"""
|
||||
parse and get an argument imm(reg)
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def get_reg(self, num: int) -> str:
|
||||
"""
|
||||
parse and get an register argument
|
||||
"""
|
||||
pass
|
||||
|
||||
def __repr__(self):
|
||||
return "{} {}".format(self.name, ", ".join(self.args))
|
@ -0,0 +1,53 @@
|
||||
from collections import defaultdict
|
||||
from typing import Dict, List, Optional
|
||||
|
||||
from .exceptions import ParseException
|
||||
from ..types import T_AbsoluteAddress, T_RelativeAddress, NUMBER_SYMBOL_PATTERN
|
||||
|
||||
|
||||
class InstructionContext:
|
||||
base_address: T_AbsoluteAddress
|
||||
"""
|
||||
The address where the instruction block is placed
|
||||
"""
|
||||
|
||||
labels: Dict[str, T_RelativeAddress]
|
||||
"""
|
||||
This dictionary maps all labels to their relative position of the instruction block
|
||||
"""
|
||||
|
||||
numbered_labels: Dict[str, List[T_RelativeAddress]]
|
||||
"""
|
||||
This dictionary maps numbered labels (which can occur multiple times) to a list of (block-relative) addresses where
|
||||
the label was placed
|
||||
"""
|
||||
|
||||
global_symbol_dict: Dict[str, T_AbsoluteAddress]
|
||||
"""
|
||||
A reference to the MMU's global symbol dictionary for access to global symbols
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self.labels = dict()
|
||||
self.numbered_labels = defaultdict(list)
|
||||
self.base_address = 0
|
||||
self.global_symbol_dict = dict()
|
||||
|
||||
def resolve_label(self, symbol: str, address_at: Optional[T_RelativeAddress] = None) -> Optional[T_AbsoluteAddress]:
|
||||
if NUMBER_SYMBOL_PATTERN.match(symbol):
|
||||
if address_at is None:
|
||||
raise ParseException("Cannot resolve relative symbol {} without an address!".format(symbol))
|
||||
|
||||
direction = symbol[-1]
|
||||
values = self.numbered_labels.get(symbol[:-1], [])
|
||||
if direction == 'b':
|
||||
return max((addr + self.base_address for addr in values if addr < address_at), default=None)
|
||||
else:
|
||||
return min((addr + self.base_address for addr in values if addr > address_at), default=None)
|
||||
else:
|
||||
# if it's not a local symbol, try the globals
|
||||
if symbol not in self.labels:
|
||||
return self.global_symbol_dict.get(symbol, None)
|
||||
# otherwise return the local symbol
|
||||
return self.labels.get(symbol, None)
|
||||
|
@ -0,0 +1,27 @@
|
||||
from typing import List
|
||||
|
||||
from . import MemorySection, Instruction, InstructionContext, MemoryFlags, T_RelativeAddress
|
||||
from .exceptions import MemoryAccessException
|
||||
|
||||
|
||||
class InstructionMemorySection(MemorySection):
|
||||
def __init__(self, instructions: List[Instruction], name: str, context: InstructionContext, owner: str, base: int = 0):
|
||||
self.name = name
|
||||
self.base = base
|
||||
self.context = context
|
||||
self.size = len(instructions) * 4
|
||||
self.flags = MemoryFlags(True, True)
|
||||
self.instructions = instructions
|
||||
self.owner = owner
|
||||
|
||||
def read(self, offset: T_RelativeAddress, size: int) -> bytearray:
|
||||
raise MemoryAccessException("Cannot read raw bytes from instruction section", self.base + offset, size, 'read')
|
||||
|
||||
def write(self, offset: T_RelativeAddress, size: int, data: bytearray):
|
||||
raise MemoryAccessException("Cannot write raw bytes to instruction section", self.base + offset, size, 'write')
|
||||
|
||||
def read_ins(self, offset: T_RelativeAddress) -> Instruction:
|
||||
if offset % 4 != 0:
|
||||
raise MemoryAccessException("Unaligned instruction fetch!", self.base + offset, 4, 'instruction fetch')
|
||||
return self.instructions[offset // 4]
|
||||
|
@ -0,0 +1,273 @@
|
||||
from typing import Union
|
||||
from ctypes import c_int32, c_uint32
|
||||
|
||||
|
||||
class Int32:
|
||||
"""
|
||||
This class implements 32bit signed integers (see :class:`UInt32` for unsigned integers)
|
||||
|
||||
It implements basically all mathematical dunder magic methods (__add__, __sub__, etc.)
|
||||
|
||||
You can use it just like you would any other integer, just be careful when passing it
|
||||
to functions which actually expect an integer and not a Int32.
|
||||
"""
|
||||
_type = c_int32
|
||||
__slots__ = ('_val',)
|
||||
|
||||
def __init__(self, val: Union[int, c_int32, c_uint32, 'Int32', bytes, bytearray] = 0):
|
||||
if isinstance(val, (bytes, bytearray)):
|
||||
signed = len(val) == 4 and self._type == c_int32
|
||||
self._val = self.__class__._type(int.from_bytes(val, 'little', signed=signed))
|
||||
elif isinstance(val, self.__class__._type):
|
||||
self._val = val
|
||||
elif isinstance(val, (c_uint32, c_int32, Int32)):
|
||||
self._val = self.__class__._type(val.value)
|
||||
elif isinstance(val, int):
|
||||
self._val = self.__class__._type(val)
|
||||
else:
|
||||
raise RuntimeError(
|
||||
"Unknonw {} input type: {} ({})".format(self.__class__.__name__, type(val), val)
|
||||
)
|
||||
|
||||
def __add__(self, other: Union['Int32', int]):
|
||||
if isinstance(other, Int32):
|
||||
other = other.value
|
||||
|
||||
return self.__class__(self._val.value + other)
|
||||
|
||||
def __sub__(self, other: Union['Int32', int]):
|
||||
if isinstance(other, Int32):
|
||||
other = other.value
|
||||
return self.__class__(self._val.value - other)
|
||||
|
||||
def __mul__(self, other: Union['Int32', int]):
|
||||
if isinstance(other, Int32):
|
||||
other = other.value
|
||||
return self.__class__(self._val.value * other)
|
||||
|
||||
def __truediv__(self, other):
|
||||
return self // other
|
||||
|
||||
def __floordiv__(self, other):
|
||||
if isinstance(other, Int32):
|
||||
other = other.value
|
||||
return self.__class__(self.value // other)
|
||||
|
||||
def __mod__(self, other: Union['Int32', int]):
|
||||
if isinstance(other, Int32):
|
||||
other = other.value
|
||||
return self.__class__(self._val.value % other)
|
||||
|
||||
def __and__(self, other: Union['Int32', int]):
|
||||
if isinstance(other, Int32):
|
||||
other = other.value
|
||||
return self.__class__(self._val.value & other)
|
||||
|
||||
def __or__(self, other: Union['Int32', int]):
|
||||
if isinstance(other, Int32):
|
||||
other = other.value
|
||||
return self.__class__(self._val.value | other)
|
||||
|
||||
def __xor__(self, other: Union['Int32', int]):
|
||||
if isinstance(other, Int32):
|
||||
other = other.value
|
||||
return self.__class__(self._val.value ^ other)
|
||||
|
||||
def __lshift__(self, other: Union['Int32', int]):
|
||||
if isinstance(other, Int32):
|
||||
other = other.value
|
||||
return self.__class__(self.value << other)
|
||||
|
||||
def __rshift__(self, other: Union['Int32', int]):
|
||||
if isinstance(other, Int32):
|
||||
other = other.value
|
||||
return self.__class__(self.value >> other)
|
||||
|
||||
def __eq__(self, other: Union['Int32', int]):
|
||||
if isinstance(other, Int32):
|
||||
other = other.value
|
||||
return self.value == other
|
||||
|
||||
def __neg__(self):
|
||||
return self.__class__(-self._val.value)
|
||||
|
||||
def __abs__(self):
|
||||
return self.__class__(abs(self.value))
|
||||
|
||||
def __bytes__(self):
|
||||
return self.to_bytes(4)
|
||||
|
||||
def __repr__(self):
|
||||
return '{}({})'.format(self.__class__.__name__, self.value)
|
||||
|
||||
def __str__(self):
|
||||
return str(self.value)
|
||||
|
||||
def __format__(self, format_spec):
|
||||
return self.value.__format__(format_spec)
|
||||
|
||||
def __hash__(self):
|
||||
return hash(self.value)
|
||||
|
||||
def __gt__(self, other):
|
||||
if isinstance(other, Int32):
|
||||
other = other.value
|
||||
return self.value > other
|
||||
|
||||
def __lt__(self, other):
|
||||
if isinstance(other, Int32):
|
||||
other = other.value
|
||||
return self.value < other
|
||||
|
||||
def __le__(self, other):
|
||||
if isinstance(other, Int32):
|
||||
other = other.value
|
||||
return self.value <= other
|
||||
|
||||
def __ge__(self, other):
|
||||
if isinstance(other, Int32):
|
||||
other = other.value
|
||||
return self.value >= other
|
||||
|
||||
def __bool__(self):
|
||||
return bool(self.value)
|
||||
|
||||
def __cmp__(self, other):
|
||||
if isinstance(other, Int32):
|
||||
other = other.value
|
||||
return self.value.__cmp__(other)
|
||||
|
||||
# right handed binary operators
|
||||
|
||||
def __radd__(self, other):
|
||||
return self + other
|
||||
|
||||
def __rsub__(self, other):
|
||||
return self.__class__(other) - self
|
||||
|
||||
def __rmul__(self, other):
|
||||
return self * other
|
||||
|
||||
def __rtruediv__(self, other):
|
||||
return self.__class__(other) // self
|
||||
|
||||
def __rfloordiv__(self, other):
|
||||
return self.__class__(other) // self
|
||||
|
||||
def __rmod__(self, other):
|
||||
return self.__class__(other) % self
|
||||
|
||||
def __rand__(self, other):
|
||||
return self.__class__(other) & self
|
||||
|
||||
def __ror__(self, other):
|
||||
return self.__class__(other) | self
|
||||
|
||||
def __rxor__(self, other):
|
||||
return self.__class__(other) ^ self
|
||||
|
||||
@property
|
||||
def value(self) -> int:
|
||||
"""
|
||||
The value represented by this Integer
|
||||
:return:
|
||||
"""
|
||||
return self._val.value
|
||||
|
||||
def unsigned(self) -> 'UInt32':
|
||||
"""
|
||||
Convert to an unsigned representation. See :class:Uint32
|
||||
:return:
|
||||
"""
|
||||
return UInt32(self)
|
||||
|
||||
def to_bytes(self, bytes: int = 4) -> bytearray:
|
||||
"""
|
||||
Convert to a bytearray of length :param:bytes
|
||||
|
||||
:param bytes: The length of the bytearray
|
||||
:return: A little-endian representation of the contained integer
|
||||
"""
|
||||
return bytearray(self.unsigned_value.to_bytes(4, 'little'))[0:bytes]
|
||||
|
||||
def signed(self) -> 'Int32':
|
||||
"""
|
||||
Convert to a signed representation. See :class:Int32
|
||||
:return:
|
||||
"""
|
||||
if self.__class__ == Int32:
|
||||
return self
|
||||
return Int32(self)
|
||||
|
||||
@property
|
||||
def unsigned_value(self):
|
||||
"""
|
||||
Return the value interpreted as an unsigned integer
|
||||
:return:
|
||||
"""
|
||||
return c_uint32(self.value).value
|
||||
|
||||
def shift_right_logical(self, ammount: Union['Int32', int]) -> 'Int32':
|
||||
"""
|
||||
This function implements logical right shifts, meaning that the sign bit is shifted as well.
|
||||
|
||||
This is equivalent to (self.value % 0x100000000) >> ammount
|
||||
|
||||
:param ammount: Number of positions to shift
|
||||
:return: A new Int32 object representing the shifted value (keeps the signed-ness of the source)
|
||||
"""
|
||||
if isinstance(ammount, Int32):
|
||||
ammount = ammount.value
|
||||
return self.__class__((self.value % 0x100000000) >> ammount)
|
||||
|
||||
def __int__(self):
|
||||
return self.value
|
||||
|
||||
def __hex__(self):
|
||||
return hex(self.value)
|
||||
|
||||
@classmethod
|
||||
def sign_extend(cls, data: Union[bytes, bytearray, int], bits: int):
|
||||
"""
|
||||
Create an instance of Int32 by sign extending :param:bits bits from :param:data
|
||||
to 32 bits
|
||||
|
||||
:param data: The source data
|
||||
:param bits: The number of bits in the source data
|
||||
:return: An instance of Int32, holding the sign-extended value
|
||||
"""
|
||||
if isinstance(data, (bytes, bytearray)):
|
||||
data = int.from_bytes(data, 'little')
|
||||
sign = data >> (bits - 1)
|
||||
if sign > 1:
|
||||
print("overflow in Int32.sext!")
|
||||
if sign:
|
||||
data = (data & (2 ** (bits - 1) - 1)) - 2**(bits-1)
|
||||
return cls(data)
|
||||
|
||||
|
||||
class UInt32(Int32):
|
||||
"""
|
||||
An unsigned version of :class:Int32.
|
||||
"""
|
||||
_type = c_uint32
|
||||
|
||||
def unsigned(self) -> 'UInt32':
|
||||
"""
|
||||
Return a new instance representing the same bytes, but signed
|
||||
:return:
|
||||
"""
|
||||
return self
|
||||
|
||||
@property
|
||||
def unsigned_value(self) -> int:
|
||||
return self._val.value
|
||||
|
||||
def shift_right_logical(self, ammount: Union['Int32', int]) -> 'UInt32':
|
||||
"""
|
||||
see :meth:`Int32.shift_right_logical <Int32.shift_right_logical>`
|
||||
|
||||
:param ammount: Number of positions to shift
|
||||
:return: A new Int32 object representing the shifted value (keeps the signed-ness of the source)
|
||||
"""
|
||||
return self >> ammount
|
@ -0,0 +1,88 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from dataclasses import dataclass
|
||||
from typing import Optional
|
||||
|
||||
from ..colors import FMT_MEM, FMT_NONE, FMT_UNDERLINE, FMT_ORANGE
|
||||
from ..helpers import format_bytes
|
||||
from . import MemoryFlags, T_AbsoluteAddress, InstructionContext, T_RelativeAddress, Instruction
|
||||
|
||||
|
||||
@dataclass
|
||||
class MemorySection(ABC):
|
||||
name: str
|
||||
flags: MemoryFlags
|
||||
size: int
|
||||
base: T_AbsoluteAddress
|
||||
owner: str
|
||||
context: InstructionContext
|
||||
|
||||
@property
|
||||
def end(self):
|
||||
return self.base + self.size
|
||||
|
||||
@abstractmethod
|
||||
def read(self, offset: T_RelativeAddress, size: int) -> bytearray:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def write(self, offset: T_RelativeAddress, size: int, data: bytearray):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def read_ins(self, offset: T_RelativeAddress) -> Instruction:
|
||||
pass
|
||||
|
||||
def dump(self, start: T_RelativeAddress, end: Optional[T_RelativeAddress] = None, fmt: str = 'hex',
|
||||
bytes_per_row: int = 16, rows: int = 10, group: int = 4):
|
||||
if self.flags.executable:
|
||||
bytes_per_row = 4
|
||||
highlight = None
|
||||
if end is None:
|
||||
end = min(start + (bytes_per_row * (rows // 2)), self.size - 1)
|
||||
highlight = start
|
||||
start = max(0, start - (bytes_per_row * (rows // 2)))
|
||||
|
||||
if self.flags.executable:
|
||||
print(FMT_MEM + "{}, viewing {} instructions:".format(
|
||||
self, (end - start) // 4
|
||||
) + FMT_NONE)
|
||||
|
||||
for addr in range(start, end, 4):
|
||||
if addr == highlight:
|
||||
print(FMT_UNDERLINE + FMT_ORANGE, end='')
|
||||
print("0x{:04x}: {}{}".format(
|
||||
self.base + addr, self.read_ins(addr), FMT_NONE
|
||||
))
|
||||
else:
|
||||
print(FMT_MEM + "{}, viewing {} bytes:".format(
|
||||
self, (end - start)
|
||||
) + FMT_NONE)
|
||||
|
||||
aligned_end = end - (end % bytes_per_row) if end % bytes_per_row != 0 else end
|
||||
|
||||
for addr in range(start, aligned_end, bytes_per_row):
|
||||
hi_ind = (highlight - addr) // group if highlight is not None else -1
|
||||
print("0x{:04x}: {}{}".format(
|
||||
self.base + addr, format_bytes(self.read(addr, bytes_per_row), fmt, group, hi_ind), FMT_NONE
|
||||
))
|
||||
|
||||
if aligned_end != end:
|
||||
hi_ind = (highlight - aligned_end) // group if highlight is not None else -1
|
||||
print("0x{:04x}: {}{}".format(
|
||||
self.base + aligned_end, format_bytes(
|
||||
self.read(aligned_end, end % bytes_per_row), fmt, group, hi_ind
|
||||
), FMT_NONE
|
||||
))
|
||||
|
||||
def dump_all(self, *args, **kwargs):
|
||||
self.dump(0, self.size, *args, **kwargs)
|
||||
|
||||
def __repr__(self):
|
||||
return "{}[{}] at 0x{:08X} (size={}bytes, flags={}, owner={})".format(
|
||||
self.__class__.__name__,
|
||||
self.name,
|
||||
self.base,
|
||||
self.size,
|
||||
self.flags,
|
||||
self.owner
|
||||
)
|
@ -0,0 +1,104 @@
|
||||
from typing import List, Optional, Set
|
||||
|
||||
from ..colors import FMT_RED, FMT_BOLD, FMT_NONE, FMT_MEM
|
||||
from ..helpers import get_section_base_name
|
||||
from . import InstructionContext, T_AbsoluteAddress, MemorySection
|
||||
|
||||
|
||||
class Program:
|
||||
"""
|
||||
This represents a collection of sections which together form an executable program
|
||||
|
||||
When you want to create a program which can be located anywhere in memory, set base to None,
|
||||
this signals the other components, that this is relocatable. Set the base of each section to
|
||||
the offset in the program, and everything will be taken care of for you.
|
||||
|
||||
"""
|
||||
name: str
|
||||
context: InstructionContext
|
||||
global_labels: Set[str]
|
||||
relative_labels: Set[str]
|
||||
sections: List[MemorySection]
|
||||
base: Optional[T_AbsoluteAddress]
|
||||
is_loaded: bool
|
||||
|
||||
@property
|
||||
def size(self):
|
||||
if len(self.sections) == 0:
|
||||
return 0
|
||||
if self.base is None:
|
||||
return self.sections[-1].base + self.sections[-1].size
|
||||
return (self.sections[-1].base - self.base) + self.sections[-1].size
|
||||
|
||||
def __init__(self, name: str, base: Optional[int] = None):
|
||||
self.name = name
|
||||
self.context = InstructionContext()
|
||||
self.sections = []
|
||||
self.global_labels = set()
|
||||
self.relative_labels = set()
|
||||
self.base = base
|
||||
self.is_loaded = False
|
||||
|
||||
def add_section(self, sec: MemorySection):
|
||||
# print a warning when a section is located before the programs base
|
||||
if self.base is not None:
|
||||
if sec.base < self.base:
|
||||
print(
|
||||
FMT_RED + FMT_BOLD + "WARNING: memory section {} in {} is placed before program base (0x{:x})".format(
|
||||
sec, self.name, self.base
|
||||
) + FMT_NONE)
|
||||
|
||||
self.sections.append(sec)
|
||||
# keep section list ordered
|
||||
self.sections.sort(key=lambda section: section.base)
|
||||
|
||||
def __repr__(self):
|
||||
return "{}(name={},sections={},base={})".format(
|
||||
self.__class__.__name__, self.name, self.global_labels,
|
||||
[s.name for s in self.sections], self.base
|
||||
)
|
||||
|
||||
@property
|
||||
def entrypoint(self):
|
||||
if '_start' in self.context.labels:
|
||||
return self.context.labels.get('_start')
|
||||
if 'main' in self.context.labels:
|
||||
return self.context.labels.get('main')
|
||||
for sec in self.sections:
|
||||
if get_section_base_name(sec.name) == '.text' and sec.flags.executable:
|
||||
return sec.base
|
||||
|
||||
def loaded_trigger(self, at_addr: T_AbsoluteAddress):
|
||||
"""
|
||||
This trigger is called when the binary is loaded and its final address in memory is determined
|
||||
|
||||
This will do a small sanity check to prevent programs loading twice, or at addresses they don't
|
||||
expect to be loaded.
|
||||
|
||||
Then it will finalize all relative symbols defined in it to point to the correct addresses.
|
||||
|
||||
:param at_addr: the address where the program will be located
|
||||
"""
|
||||
if self.is_loaded:
|
||||
if at_addr != self.base:
|
||||
raise RuntimeError("Program loaded twice at different addresses! This will probably break things!")
|
||||
return
|
||||
|
||||
if self.base is not None and self.base != at_addr:
|
||||
print(FMT_MEM + 'WARNING: Program loaded at different address then expected! (loaded at {}, '
|
||||
'but expects to be loaded at {})'.format(at_addr, self.base) + FMT_NONE)
|
||||
|
||||
# check if we are relocating
|
||||
if self.base != at_addr:
|
||||
offset = at_addr if self.base is None else at_addr - self.base
|
||||
|
||||
# move all sections by the offset
|
||||
for sec in self.sections:
|
||||
sec.base += offset
|
||||
|
||||
# move all relative symbols by the offset
|
||||
for name in self.relative_labels:
|
||||
self.context.labels[name] += offset
|
||||
|
||||
self.base = at_addr
|
||||
self.context.base_address = at_addr
|
@ -0,0 +1,58 @@
|
||||
import os
|
||||
from abc import abstractmethod, ABC
|
||||
from typing import Union, Iterator, List
|
||||
|
||||
from . import T_ParserOpts, Program
|
||||
|
||||
|
||||
class ProgramLoader(ABC):
|
||||
"""
|
||||
A program loader is always specific to a given source file. It is a place to store all state
|
||||
concerning the parsing and loading of that specific source file, including options.
|
||||
"""
|
||||
|
||||
def __init__(self, source_path: str, options: T_ParserOpts):
|
||||
self.source_path = source_path
|
||||
self.options = options
|
||||
self.filename = os.path.split(self.source_path)[-1]
|
||||
|
||||
@classmethod
|
||||
@abstractmethod
|
||||
def can_parse(cls, source_path: str) -> float:
|
||||
"""
|
||||
Return confidence that the file located at source_path
|
||||
should be parsed and loaded by this loader
|
||||
:param source_path: the path of the source file
|
||||
:return: the confidence that this file belongs to this parser
|
||||
"""
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
@abstractmethod
|
||||
def get_options(cls, argv: list[str]) -> [List[str], T_ParserOpts]:
|
||||
"""
|
||||
parse command line args into an options dictionary
|
||||
|
||||
:param argv: the command line args list
|
||||
:return: all remaining command line args and the parser options object
|
||||
"""
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def instantiate(cls, source_path: str, options: T_ParserOpts) -> 'ProgramLoader':
|
||||
"""
|
||||
Instantiate a loader for the given source file with the required arguments
|
||||
|
||||
:param source_path: the path to the source file
|
||||
:param options: the parsed options (guaranteed to come from this classes get_options method.
|
||||
:return: An instance of a ProgramLoader for the spcified source
|
||||
"""
|
||||
return cls(source_path, options)
|
||||
|
||||
@abstractmethod
|
||||
def parse(self) -> Union[Program, Iterator[Program]]:
|
||||
"""
|
||||
|
||||
:return:
|
||||
"""
|
||||
pass
|
@ -0,0 +1,26 @@
|
||||
from typing import Union, Tuple
|
||||
|
||||
from . import Instruction, T_RelativeAddress, InstructionContext
|
||||
from ..helpers import parse_numeric_argument
|
||||
|
||||
|
||||
class SimpleInstruction(Instruction):
|
||||
def __init__(self, name: str, args: Union[Tuple[()], Tuple[str], Tuple[str, str], Tuple[str, str, str]],
|
||||
context: InstructionContext, addr: T_RelativeAddress):
|
||||
self.context = context
|
||||
self.name = name
|
||||
self.args = args
|
||||
self.addr = addr
|
||||
|
||||
def get_imm(self, num: int) -> int:
|
||||
resolved_label = self.context.resolve_label(self.args[num], self.addr)
|
||||
if resolved_label is None:
|
||||
return parse_numeric_argument(self.args[num])
|
||||
return resolved_label
|
||||
|
||||
def get_imm_reg(self, num: int) -> Tuple[int, str]:
|
||||
return self.get_imm(num + 1), self.get_reg(num)
|
||||
|
||||
def get_reg(self, num: int) -> str:
|
||||
return self.args[num]
|
||||
|
@ -0,0 +1,3 @@
|
||||
from .test_tokenizer import *
|
||||
from .test_helpers import *
|
||||
from .test_integers import *
|
@ -0,0 +1,73 @@
|
||||
import contextlib
|
||||
import os
|
||||
from abc import abstractmethod
|
||||
from tempfile import NamedTemporaryFile
|
||||
from typing import Optional, Union, Tuple
|
||||
from unittest import TestCase
|
||||
|
||||
from riscemu import CPU, UserModeCPU, InstructionSetDict, RunConfig
|
||||
from riscemu.types import Program
|
||||
|
||||
|
||||
class EndToEndTest(TestCase):
|
||||
|
||||
def __init__(self, cpu: Optional[CPU] = None):
|
||||
super().__init__()
|
||||
if cpu is None:
|
||||
cpu = UserModeCPU(InstructionSetDict.values(), RunConfig())
|
||||
self.cpu = cpu
|
||||
|
||||
@abstractmethod
|
||||
def get_source(self) -> Tuple[str, Union[bytes, str, bytearray]]:
|
||||
"""
|
||||
This method returns the source code of the program
|
||||
:return:
|
||||
"""
|
||||
pass
|
||||
|
||||
def test_run_program(self):
|
||||
"""
|
||||
Runs the program and verifies output
|
||||
:return:
|
||||
"""
|
||||
with self.with_source_file() as names:
|
||||
fname, orig_name = names
|
||||
loader = self.cpu.get_best_loader_for(fname)
|
||||
self.program = loader.instantiate(fname, loader.get_options([])).parse()
|
||||
self._change_program_file_name(self.program, orig_name)
|
||||
self.cpu.load_program(self.program)
|
||||
self.after_program_load(self.program)
|
||||
if isinstance(self.cpu, UserModeCPU):
|
||||
self.cpu.setup_stack()
|
||||
try:
|
||||
self.cpu.launch(self.program)
|
||||
except Exception as ex:
|
||||
if self.is_exception_expected(ex):
|
||||
pass
|
||||
raise ex
|
||||
|
||||
@contextlib.contextmanager
|
||||
def with_source_file(self):
|
||||
name, content = self.get_source()
|
||||
if isinstance(content, str):
|
||||
f = NamedTemporaryFile('w', suffix=name, delete=False)
|
||||
else:
|
||||
f = NamedTemporaryFile('wb', suffix=name, delete=False)
|
||||
f.write(content)
|
||||
f.flush()
|
||||
f.close()
|
||||
try:
|
||||
yield f.name, name
|
||||
finally:
|
||||
os.unlink(f.name)
|
||||
|
||||
def after_program_load(self, program):
|
||||
pass
|
||||
|
||||
def is_exception_expected(self, ex: Exception) -> bool:
|
||||
return False
|
||||
|
||||
def _change_program_file_name(self, program: Program, new_name: str):
|
||||
program.name = new_name
|
||||
for sec in program.sections:
|
||||
sec.owner = new_name
|
@ -0,0 +1,19 @@
|
||||
from unittest import TestCase
|
||||
|
||||
from riscemu.types import Int32, UInt32
|
||||
|
||||
|
||||
class TestTokenizer(TestCase):
|
||||
|
||||
def test_logical_right_shift(self):
|
||||
a = Int32(100)
|
||||
self.assertEqual(a.shift_right_logical(0), a)
|
||||
self.assertEqual(a.shift_right_logical(10), 0)
|
||||
self.assertEqual(a.shift_right_logical(1), 100>>1)
|
||||
|
||||
a = Int32(-100)
|
||||
self.assertEqual(a.shift_right_logical(0), a)
|
||||
self.assertEqual(a.shift_right_logical(1), 2147483598)
|
||||
self.assertEqual(a.shift_right_logical(10), 4194303)
|
||||
self.assertEqual(a.shift_right_logical(31), 1)
|
||||
self.assertEqual(a.shift_right_logical(32), 0)
|
@ -0,0 +1,75 @@
|
||||
from riscemu.colors import FMT_ERROR, FMT_NONE, FMT_BOLD, FMT_GREEN
|
||||
from riscemu.instructions import InstructionSet
|
||||
from riscemu.types import Instruction, CPU
|
||||
from riscemu.decoder import RISCV_REGS
|
||||
|
||||
FMT_SUCCESS = FMT_GREEN + FMT_BOLD
|
||||
|
||||
|
||||
def assert_equals(ins: Instruction, cpu: CPU):
|
||||
a, b = (get_arg_from_ins(ins, i, cpu) for i in (0, 2))
|
||||
return a == b
|
||||
|
||||
|
||||
def assert_equals_mem(ins: Instruction, cpu: CPU):
|
||||
a, b = (get_arg_from_ins(ins, i, cpu) for i in (0, 2))
|
||||
a = cpu.mmu.read_int(a)
|
||||
return a == b
|
||||
|
||||
|
||||
def assert_in(ins: Instruction, cpu: CPU):
|
||||
a = get_arg_from_ins(ins, 0, cpu)
|
||||
others = [get_arg_from_ins(ins, i, cpu) for i in range(2, len(ins.args))]
|
||||
return a in others
|
||||
|
||||
|
||||
def _not(func):
|
||||
def test(ins: Instruction, cpu: CPU):
|
||||
return not func(ins, cpu)
|
||||
|
||||
return test
|
||||
|
||||
|
||||
def get_arg_from_ins(ins: Instruction, num: int, cpu: CPU):
|
||||
a = ins.args[num]
|
||||
if a in RISCV_REGS:
|
||||
return cpu.regs.get(a)
|
||||
return ins.get_imm(num)
|
||||
|
||||
|
||||
assert_ops = {
|
||||
'==': assert_equals,
|
||||
'!=': _not(assert_equals),
|
||||
'in': assert_in,
|
||||
'not_in': _not(assert_in),
|
||||
}
|
||||
|
||||
|
||||
class TestIS(InstructionSet):
|
||||
def __init__(self, cpu: 'CPU'):
|
||||
print('[Test] loading testing ISA, this is only meant for running testcases and is not part of the RISC-V ISA!')
|
||||
self.failed = False
|
||||
super().__init__(cpu)
|
||||
|
||||
def instruction_assert(self, ins: Instruction):
|
||||
if len(ins.args) < 3:
|
||||
print(FMT_ERROR + '[Test] Unknown assert statement: {}'.format(ins) + FMT_NONE)
|
||||
return
|
||||
op = ins.args[1]
|
||||
if op not in assert_ops:
|
||||
print(FMT_ERROR + '[Test] Unknown operation statement: {} in {}'.format(op, ins) + FMT_NONE)
|
||||
return
|
||||
|
||||
if assert_ops[op](ins, self.cpu):
|
||||
print(FMT_SUCCESS + '[TestCase] 🟢 passed assertion {}'.format(ins))
|
||||
else:
|
||||
print(FMT_ERROR + '[TestCase] 🔴 failed assertion {}'.format(ins))
|
||||
self.cpu.halted = True
|
||||
self.failed = True
|
||||
|
||||
def instruction_fail(self, ins: Instruction):
|
||||
print(FMT_ERROR + '[TestCase] 🔴 reached fail instruction! {}'.format(ins))
|
||||
self.cpu.halted = True
|
||||
self.failed = True
|
||||
|
||||
def assert_mem(self, ins: Instruction):
|
@ -0,0 +1,126 @@
|
||||
from unittest import TestCase
|
||||
|
||||
from riscemu.tokenizer import tokenize, print_tokens, Token, TokenType, NEWLINE, COMMA, \
|
||||
split_whitespace_respecting_quotes
|
||||
|
||||
|
||||
def ins(name: str) -> Token:
|
||||
return Token(TokenType.INSTRUCTION_NAME, name)
|
||||
|
||||
|
||||
def arg(name: str) -> Token:
|
||||
return Token(TokenType.ARGUMENT, name)
|
||||
|
||||
|
||||
def op(name: str) -> Token:
|
||||
return Token(TokenType.PSEUDO_OP, name)
|
||||
|
||||
|
||||
def lbl(name: str) -> Token:
|
||||
return Token(TokenType.LABEL, name)
|
||||
|
||||
|
||||
class TestTokenizer(TestCase):
|
||||
|
||||
def test_instructions(self):
|
||||
program = [
|
||||
'li a0, 144',
|
||||
'divi a0, a0, 12',
|
||||
'xori a1, a0, 12'
|
||||
]
|
||||
tokens = [
|
||||
ins('li'), arg('a0'), COMMA, arg('144'), NEWLINE,
|
||||
ins('divi'), arg('a0'), COMMA, arg('a0'), COMMA, arg('12'), NEWLINE,
|
||||
ins('xori'), arg('a1'), COMMA, arg('a0'), COMMA, arg('12'), NEWLINE,
|
||||
]
|
||||
self.assertEqual(list(tokenize(program)), tokens)
|
||||
|
||||
def test_comments(self):
|
||||
parsed_res = [
|
||||
ins('li'), arg('a0'), COMMA, arg('144'), NEWLINE
|
||||
]
|
||||
for c in ('#', '//', ';'):
|
||||
lines = [
|
||||
c + ' this is a comment',
|
||||
'li a0, 144'
|
||||
]
|
||||
self.assertEqual(list(tokenize(lines)), parsed_res)
|
||||
|
||||
def test_pseudo_ins(self):
|
||||
parsed_res = [
|
||||
Token(TokenType.PSEUDO_OP, '.section'), Token(TokenType.ARGUMENT, '.text'), NEWLINE,
|
||||
Token(TokenType.PSEUDO_OP, '.type'), Token(TokenType.ARGUMENT, 'init'), COMMA,
|
||||
Token(TokenType.ARGUMENT, '@function'), NEWLINE
|
||||
]
|
||||
input_program = [
|
||||
'.section .text',
|
||||
'.type init, @function'
|
||||
]
|
||||
self.assertEqual(list(tokenize(input_program)), parsed_res)
|
||||
|
||||
def test_full_program(self):
|
||||
program = """
|
||||
# a hashtag comment
|
||||
|
||||
; semicolon comment followed by an empty line
|
||||
.section .text
|
||||
// double slash comment
|
||||
addi sp, sp, -32
|
||||
sw s0, 0(ra)
|
||||
section:
|
||||
sub s0, s0, s0
|
||||
"""
|
||||
tokens = [
|
||||
op('.section'), arg('.text'), NEWLINE,
|
||||
ins('addi'), arg('sp'), COMMA, arg('sp'), COMMA, arg('-32'), NEWLINE,
|
||||
ins('sw'), arg('s0'), COMMA, arg('ra'), arg('0'), NEWLINE,
|
||||
lbl('section:'), NEWLINE,
|
||||
ins('sub'), arg('s0'), COMMA, arg('s0'), COMMA, arg('s0'), NEWLINE
|
||||
]
|
||||
|
||||
self.assertEqual(list(tokenize(program.splitlines())), tokens)
|
||||
|
||||
def test_split_whitespace_respecting_quotes_single(self):
|
||||
self.assertEqual(
|
||||
list(split_whitespace_respecting_quotes("test")), ["test"]
|
||||
)
|
||||
|
||||
def test_split_whitespace_respecting_quotes_empty(self):
|
||||
self.assertEqual(
|
||||
list(split_whitespace_respecting_quotes("")), []
|
||||
)
|
||||
|
||||
def test_split_whitespace_respecting_quotes_two_parts(self):
|
||||
self.assertEqual(
|
||||
list(split_whitespace_respecting_quotes("test 123")), ["test", "123"]
|
||||
)
|
||||
|
||||
def test_split_whitespace_respecting_quotes_whole_quoted(self):
|
||||
self.assertEqual(
|
||||
list(split_whitespace_respecting_quotes("'test 123'")), ["test 123"]
|
||||
)
|
||||
|
||||
def test_split_whitespace_respecting_quotes_double_quotes(self):
|
||||
self.assertEqual(
|
||||
list(split_whitespace_respecting_quotes('"test 123"')), ["test 123"]
|
||||
)
|
||||
|
||||
def test_split_whitespace_respecting_quotes_quoted_then_normal(self):
|
||||
self.assertEqual(
|
||||
list(split_whitespace_respecting_quotes('"test 123" abc')), ["test 123", "abc"]
|
||||
)
|
||||
|
||||
def test_split_whitespace_respecting_quotes_quoted_sorrounded(self):
|
||||
self.assertEqual(
|
||||
list(split_whitespace_respecting_quotes('hello "test 123" abc')), ["hello", "test 123", "abc"]
|
||||
)
|
||||
|
||||
def test_split_whitespace_respecting_quotes_weird_spaces(self):
|
||||
self.assertEqual(
|
||||
list(split_whitespace_respecting_quotes('hello "test 123"\tabc')), ["hello", "test 123", "abc"]
|
||||
)
|
||||
|
||||
def test_split_whitespace_respecting_quotes_quotes_no_spaces(self):
|
||||
self.assertEqual(
|
||||
list(split_whitespace_respecting_quotes('hello"test 123"abc')), ["hello", "test 123", "abc"]
|
||||
)
|
@ -0,0 +1,53 @@
|
||||
from riscemu import AssemblyFileLoader
|
||||
from riscemu.colors import *
|
||||
|
||||
FMT_SUCCESS = FMT_GREEN + FMT_BOLD
|
||||
|
||||
def run_test(path: str):
|
||||
from riscemu import CPU, UserModeCPU, RunConfig
|
||||
from riscemu.instructions import InstructionSetDict
|
||||
from test.test_isa import TestIS
|
||||
import os
|
||||
|
||||
fname = os.path.basename(path)
|
||||
|
||||
ISAs = list(InstructionSetDict.values())
|
||||
ISAs.append(TestIS)
|
||||
|
||||
cpu = UserModeCPU(ISAs, RunConfig())
|
||||
try:
|
||||
program = AssemblyFileLoader(path, {}).parse()
|
||||
cpu.load_program(program)
|
||||
cpu.launch(program)
|
||||
except Exception as ex:
|
||||
print(FMT_ERROR + '[Test] 🔴 failed with exception "{}" ({})'.format(ex, fname) + FMT_NONE)
|
||||
raise ex
|
||||
|
||||
if cpu.halted:
|
||||
for isa in cpu.instruction_sets:
|
||||
if isinstance(isa, TestIS):
|
||||
if not isa.failed:
|
||||
print(FMT_SUCCESS + '[Test] 🟢 successful {}'.format(fname) + FMT_NONE)
|
||||
return not isa.failed
|
||||
return False
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
import os
|
||||
import glob
|
||||
|
||||
successes = 0
|
||||
failures = 0
|
||||
ttl = 0
|
||||
|
||||
for path in glob.glob(f'{os.path.dirname(__file__)}/*.asm'):
|
||||
print(FMT_BLUE + '[Test] running testcase ' + os.path.basename(path) + FMT_NONE)
|
||||
ttl += 1
|
||||
if run_test(path):
|
||||
successes += 1
|
||||
else:
|
||||
failures += 1
|
||||
|
||||
|
||||
|
@ -0,0 +1,7 @@
|
||||
.data
|
||||
|
||||
data:
|
||||
.word 0xFFFFFFFF, 0x0000FFFF, 0xFF00FF00, 0x7FFFFFFF
|
||||
|
||||
.text
|
||||
ebreak
|
@ -0,0 +1,20 @@
|
||||
.text
|
||||
|
||||
main:
|
||||
addi a0, zero, main
|
||||
addi a1, zero, main
|
||||
addi t0, zero, 1000
|
||||
assert a0, ==, 0x100
|
||||
1:
|
||||
addi a1, a1, 1
|
||||
blt a1, t0, 1b
|
||||
sub a1, a1, a0
|
||||
j 1f
|
||||
addi a1, zero, 0
|
||||
fail
|
||||
1:
|
||||
assert a1, ==, 744
|
||||
add a0, zero, a1 ; set exit code to a1
|
||||
addi a7, zero, SCALL_EXIT ; exit syscall code
|
||||
scall
|
||||
fail
|
Loading…
Reference in New Issue