insndb/db: support opcodes command
authorDmitry Selyutin <ghostmansd@gmail.com>
Sat, 3 Jun 2023 09:59:09 +0000 (12:59 +0300)
committerDmitry Selyutin <ghostmansd@gmail.com>
Sat, 3 Jun 2023 09:59:09 +0000 (12:59 +0300)
src/openpower/insndb/db.py

index 96d881c57d6d78486b929f0dc5115021ccadad30..753ab626ab00e48ae5cd828f15bce7e1355d8d92 100644 (file)
@@ -12,21 +12,45 @@ from openpower.insndb.types import (
 
 
 def main():
-    class ListVisitor(Visitor):
+    class GenericVisitor(Visitor):
+        def __init__(self, **_):
+            pass
+
+    class ListVisitor(GenericVisitor):
         @contextlib.contextmanager
         def record(self, record):
             print(record.name)
             yield record
 
+    class OpcodesVisitor(GenericVisitor):
+        def __init__(self, insn, **_):
+            self.__insn = insn
+            return super().__init__()
+
+        @contextlib.contextmanager
+        def record(self, record):
+            if record.name == self.__insn:
+                for opcode in record.opcodes:
+                    print(opcode)
+            yield record
+
     visitors = {
         "list": ListVisitor,
+        "opcodes": OpcodesVisitor,
     }
     parser = argparse.ArgumentParser()
     subparser = parser.add_subparsers(dest="command", required=True)
-    parser_list = subparser.add_parser("list")
+    parser_list = subparser.add_parser("list",
+        help="list all instructions")
+    parser_opcodes = subparser.add_parser("opcodes",
+        help="print instruction opcodes")
+    parser_opcodes.add_argument("insn",
+        metavar="INSN",
+        help="instruction")
+
     args = vars(parser.parse_args())
     command = args.pop("command")
-    visitor = visitors[command]()
+    visitor = visitors[command](**args)
 
     db = Database(find_wiki_dir())
     db.visit(visitor=visitor)