From: Dmitry Selyutin Date: Wed, 10 May 2023 17:47:14 +0000 (+0000) Subject: power_insn: decouple assemble routine X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=0a60795540fcfeca0910f7ac1c40f995b7cfb097;p=openpower-isa.git power_insn: decouple assemble routine --- diff --git a/src/openpower/decoder/power_insn.py b/src/openpower/decoder/power_insn.py index b9d92508..f8188eda 100644 --- a/src/openpower/decoder/power_insn.py +++ b/src/openpower/decoder/power_insn.py @@ -3635,6 +3635,10 @@ class SVP64Database: return None +class AssemblerError(Exception): + pass + + class Database: def __init__(self, root): root = _pathlib.Path(root) @@ -3700,3 +3704,48 @@ class Database: return self.__names.get(key) raise ValueError("instruction or name expected") + + def assemble(self, insn, macros=None): + def subst(argument): + again = True + while again: + again = False + argument = _re.sub(r"\s", "", argument) + for (macro, value) in macros.items(): + if argument == macro: + again = True + argument = value + elif argument == f"{macro}.s": + again = True + argument = f"{value}.s" + elif argument == f"{macro}.v": + again = True + argument = f"{value}.v" + elif argument == f"*{macro}": + again = True + argument = f"*{value}" + elif argument == f"({macro})": + again = True + argument = f"({value})" + return argument + + if macros is None: + macros = {} + + (opcode, *arguments) = map(str.strip, _re.split(r"\s", insn)) + arguments = _re.split(r",", "".join(arguments)) + arguments = tuple(filter(bool, map(subst, arguments))) + record = self[opcode] + if record is not None: + return WordInstruction.assemble(record=record, arguments=arguments) + + if opcode.startswith("sv."): + (opcode, *specifiers) = map(str.strip, opcode.split("/")) + specifiers = tuple(filter(bool, specifiers)) + opcode = opcode[3:] + record = self[opcode] + if record is not None: + return SVP64Instruction.assemble(record=record, + arguments=arguments, specifiers=specifiers) + + raise AssemblerError(opcode)