pysvp64dis: switch to common disassembly routine insndb-asm-dis
authorDmitry Selyutin <ghostmansd@gmail.com>
Fri, 12 May 2023 12:23:39 +0000 (12:23 +0000)
committerDmitry Selyutin <ghostmansd@gmail.com>
Sat, 13 May 2023 18:37:22 +0000 (18:37 +0000)
src/openpower/sv/trans/pysvp64dis.py
src/openpower/sv/trans/test_pysvp64dis.py

index b2fceeaa85cb417e5f25f1200aa429a0be9b2354..2512a23166f59d3e6384a792c6b9bb064be380b0 100644 (file)
@@ -8,47 +8,13 @@ from openpower.decoder.power_enums import (
 )
 from openpower.decoder.power_insn import (
     ByteOrder as _ByteOrder,
-    Style as _Style,
+    collect as _collect,
     Database as _Database,
-    WordInstruction as _WordInstruction,
-    PrefixedInstruction as _PrefixedInstruction,
-    SVP64Instruction as _SVP64Instruction,
+    DisassemblerError as _DisassemblerError,
+    Style as _Style,
 )
 
 
-def load(ifile, byteorder=_ByteOrder.LITTLE, **_):
-    while True:
-        insn = ifile.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 = ifile.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
-
-
-def dump(insns, style, **_):
-    db = _Database(_find_wiki_dir())
-    for insn in insns:
-        record = db[insn]
-        yield from insn.disassemble(record=record, style=style)
-
-
 # this is the entry-point for the console-script pysvp64dis
 def main():
     parser = _argparse.ArgumentParser()
@@ -71,15 +37,22 @@ def main():
         action="store_true", default=False)
 
     args = dict(vars(parser.parse_args()))
+    db = _Database(_find_wiki_dir())
 
     # if logging requested do not disable it.
-    if not args['log']:
-        _os.environ['SILENCELOG'] = '1'
+    if not args["log"]:
+        _os.environ["SILENCELOG"] = "1"
 
     # load instructions and dump them
-    insns = load(**args)
-    for line in dump(insns, **args):
-        print(line, file=args["ofile"])
+    ifile = args["ifile"]
+    ofile = args["ofile"]
+    byteorder = args["byteorder"]
+    style = args["style"]
+
+    insns = _collect(stream=ifile, byteorder=byteorder)
+    lines = db.disassemble(insns=insns, byteorder=byteorder, style=style)
+    for line in lines:
+        print(line, file=ofile)
 
 
 # still here but use "python3 setup.py develop" then run the
index 7573ef8af626a3fa6c70d8c0050ec126669fed55..3fd30a33c42f7b1a695f9085aa028ae799d9dc7e 100644 (file)
@@ -1,7 +1,6 @@
 from openpower.simulator.program import Program
-from openpower.sv.trans.pysvp64dis import load, dump
 from openpower.sv.trans.svp64 import SVP64Asm
-from openpower.decoder.power_insn import Database, Style
+from openpower.decoder.power_insn import ByteOrder, collect, Database, Style
 from openpower.decoder.power_enums import find_wiki_dir
 from openpower.sv import sv_binutils_fptrans
 import unittest
@@ -9,11 +8,21 @@ from io import BytesIO
 import itertools
 import sys
 
-class SVSTATETestCase(unittest.TestCase):
 
+def disassemble(db, insns):
+    for insn in insns:
+        yield "; ".join(db.disassemble(insn,
+            byteorder=ByteOrder.LITTLE,
+            style=Style.SHORT))
+
+
+class SVSTATETestCase(unittest.TestCase):
     def _do_tst(self, expected):
+        db = Database(find_wiki_dir())
         isa = SVP64Asm(expected)
         lst = list(isa)
+        byteorder = ByteOrder.LITTLE
+        print(lst)
         with Program(lst, bigendian=False) as program:
             print ("ops", program._instructions)
             binfile = BytesIO()
@@ -21,12 +30,13 @@ class SVSTATETestCase(unittest.TestCase):
             binfile.write(program.binfile.read())
             program.binfile.seek(0)
             binfile.seek(0)
-            insns = load(binfile)
+            insns = collect(stream=binfile, byteorder=byteorder)
             #for insn in insns:
                 #print ("insn", insn)
             insns = list(insns)
-            print ("insns", insns)
-            for i, line in enumerate(dump(insns, style=Style.SHORT)):
+            print("insns", insns)
+            lines = tuple(disassemble(db, insns))
+            for i, line in enumerate(lines):
                 name = expected[i].split(" ")[0]
                 with self.subTest("%d:%s" % (i, name)):
                     print("instruction", repr(line), repr(expected[i]))