power_insn: support verbosity levels
[openpower-isa.git] / src / openpower / sv / trans / pysvp64dis.py
index 67833b358e3dcc2f74f54538e8e72dba86807270..837041da1ac9edbfeab150a4f3a272911b10aa9e 100644 (file)
@@ -1,14 +1,15 @@
 import argparse as _argparse
 import enum as _enum
-import functools as _functools
 import sys as _sys
+import os
 
 from openpower.decoder.power_enums import (
     find_wiki_dir as _find_wiki_dir,
 )
 from openpower.decoder.power_insn import (
+    Verbosity as _Verbosity,
     Database as _Database,
-    Instruction as _Instruction,
+    WordInstruction as _WordInstruction,
     PrefixedInstruction as _PrefixedInstruction,
     SVP64Instruction as _SVP64Instruction,
 )
@@ -22,45 +23,44 @@ class ByteOrder(_enum.Enum):
         return self.name.lower()
 
 
-def load(ifile, byteorder, **_):
-    db = _Database(_find_wiki_dir())
+def load(ifile, byteorder=ByteOrder.LITTLE, **_):
+    byteorder = str(byteorder)
+    curpos = ifile.tell() # get file position
 
-    def load(ifile):
-        prefix = ifile.read(4)
-        length = len(prefix)
-        if length == 0:
-            return None
-        elif length < 4:
-            raise IOError(prefix)
-        prefix = _Instruction(value=prefix, byteorder=byteorder, db=db)
-        if prefix.major != 0x1:
-            return prefix
-
-        suffix = ifile.read(4)
-        length = len(suffix)
+    while True:
+        insn = ifile.read(4)
+        length = len(insn)
         if length == 0:
-            return prefix
+            return
         elif length < 4:
-            raise IOError(suffix)
-        try:
-            return _SVP64Instruction(prefix=prefix, suffix=suffix,
-                byteorder=byteorder, db=db)
-        except _SVP64Instruction.PrefixError:
-            return _PrefixedInstruction(prefix=prefix, suffix=suffix,
-                byteorder=byteorder, db=db)
-
-    while True:
-        insn = load(ifile)
-        if insn is None:
-            break
+            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
 
+    ifile.seek(curpos) # restore position so that generator can be reused
+
 
-def dump(insns, **_):
+def dump(insns, verbosity, **_):
+    db = _Database(_find_wiki_dir())
     for insn in insns:
-        yield from insn.disassemble()
+        yield from insn.disassemble(db=db, verbosity=verbosity)
 
 
+# this is the entry-point for the console-script pysvp64dis
 def main():
     parser = _argparse.ArgumentParser()
     parser.add_argument("ifile", nargs="?",
@@ -69,16 +69,28 @@ def main():
         type=_argparse.FileType("w"), default=_sys.stdout)
     parser.add_argument("-b", "--byteorder",
         type=ByteOrder, default=ByteOrder.LITTLE)
+    parser.add_argument("-s", "--short",
+        dest="verbosity", default=_Verbosity.NORMAL,
+        action="store_const", const=_Verbosity.SHORT)
+    parser.add_argument("-v", "--verbose",
+        dest="verbosity", default=_Verbosity.NORMAL,
+        action="store_const", const=_Verbosity.VERBOSE)
+    parser.add_argument("-l", "--log",
+        action="store_true", default=False)
 
     args = dict(vars(parser.parse_args()))
-    ifile = args["ifile"]
-    ofile = args["ofile"]
-    byteorder = args["byteorder"]
 
-    insns = load(ifile, byteorder)
-    for line in dump(insns):
-        print(line, file=ofile)
+    # if logging requested do not disable it.
+    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"])
 
 
+# still here but use "python3 setup.py develop" then run the
+# command "pysvp64dis" instead of "python3 src/openpower/sv/trans/pysvp64dis.py"
 if __name__ == "__main__":
     main()