power_insn: decouple disassemble routine
authorDmitry Selyutin <ghostmansd@gmail.com>
Fri, 12 May 2023 11:47:58 +0000 (14:47 +0300)
committerDmitry Selyutin <ghostmansd@gmail.com>
Sat, 13 May 2023 18:37:22 +0000 (18:37 +0000)
src/openpower/decoder/power_insn.py

index 6a34ccc44c090e0eba4c29f768f30faf27efc398..a1c9a977a72a37f351e3cf93c12466e2da6fc916 100644 (file)
@@ -3647,6 +3647,37 @@ class AssemblerError(Exception):
     pass
 
 
+class DisassemblerError(Exception):
+    pass
+
+
+
+def collect(stream, byteorder=ByteOrder.LITTLE):
+    while True:
+        insn = stream.read(4)
+        length = len(insn)
+        if length == 0:
+            return
+        elif length < 4:
+            raise IOError(insn)
+        insn = WordInstruction.integer(value=insn, byteorder=byteorder)
+        if insn.PO == 0x1:
+            suffix = stream.read(4)
+            length = len(suffix)
+            if length == 0:
+                yield insn
+                return
+            elif length < 4:
+                raise IOError(suffix)
+
+            prefix = insn
+            suffix = WordInstruction.integer(value=suffix, byteorder=byteorder)
+            insn = SVP64Instruction.pair(prefix=prefix, suffix=suffix)
+            if insn.prefix.id != 0b11:
+                insn = PrefixedInstruction.pair(prefix=prefix, suffix=suffix)
+        yield insn
+
+
 class Database:
     def __init__(self, root):
         root = _pathlib.Path(root)
@@ -3757,3 +3788,7 @@ class Database:
                     arguments=arguments, specifiers=specifiers)
 
         raise AssemblerError(opcode)
+
+    def disassemble(self, insn, byteorder=ByteOrder.LITTLE, style=Style.NORMAL):
+        return insn.disassemble(record=self[insn],
+            byteorder=byteorder, style=style)