temporary hack-revert, the original is now in branch "paths"
[openpower-isa.git] / src / openpower / insndb / db.py
index 33586152d3bddc345e29c850a6811078ed3215e0..fa5eb267ea7ae14f442183b260c2580b16d56a80 100644 (file)
@@ -8,11 +8,14 @@ from openpower.decoder.power_enums import (
 )
 from openpower.insndb.core import (
     Database,
-    Extra,
+    Dataclass,
+    Dict,
     Record,
     Records,
+    Tuple,
     Visitor,
     visit,
+    visitormethod,
 )
 
 
@@ -39,26 +42,51 @@ class SVP64Instruction(Instruction):
         return self
 
 
-class ListVisitor(Visitor):
+class TreeVisitor(Visitor):
+    def __init__(self):
+        self.__depth = 0
+        self.__path = [""]
+        return super().__init__()
+
     @contextlib.contextmanager
-    def __call__(self, node):
-        if isinstance(node, Record):
-            print(node.name)
+    def __call__(self, path, node):
+        with super().__call__(path=path, node=node):
+            self.__path.append(path)
+            print("/".join(self.__path))
+            if not isinstance(node, (Dataclass, Tuple, Dict)):
+                print("    ", repr(node), sep="")
+            self.__depth += 1
+            yield node
+            self.__path.pop(-1)
+            self.__depth -= 1
+
+
+class ListVisitor(Visitor):
+    @visitormethod(Record)
+    def Record(self, path, node):
+        print(node.name)
         yield node
 
 
-class OpcodesVisitor(Visitor):
-    @contextlib.contextmanager
-    def __call__(self, node):
-        if isinstance(node, Record):
-            for opcode in node.opcodes:
-                print(opcode)
+# No use other than checking issubclass and adding an argument.
+class InstructionVisitor(Visitor):
+    pass
+
+class SVP64InstructionVisitor(InstructionVisitor):
+    pass
+
+
+class OpcodesVisitor(InstructionVisitor):
+    @visitormethod(Record)
+    def Record(self, path, node):
+        for opcode in node.opcodes:
+            print(opcode)
         yield node
 
 
-class OperandsVisitor(Visitor):
-    @contextlib.contextmanager
-    def __call__(self, node):
+class OperandsVisitor(InstructionVisitor):
+    @visitormethod(Record)
+    def Record(self, path, node):
         if isinstance(node, Record):
             for operand in node.dynamic_operands:
                 print(operand.name, ",".join(map(str, operand.span)))
@@ -69,29 +97,33 @@ class OperandsVisitor(Visitor):
         yield node
 
 
-class PCodeVisitor(Visitor):
-    @contextlib.contextmanager
-    def __call__(self, node):
+class PCodeVisitor(InstructionVisitor):
+    @visitormethod(Record)
+    def Record(self, path, node):
         if isinstance(node, Record):
             for line in node.pcode:
                 print(line)
         yield node
 
 
-class ExtrasVisitor(Visitor):
-    @contextlib.contextmanager
-    def __call__(self, node):
-        if isinstance(node, Extra):
-            print(node.name)
-            print("    sel", node.sel)
-            print("    reg", node.reg)
-            print("    seltype", node.seltype)
-            print("    idx", node.idx)
+class ExtrasVisitor(SVP64InstructionVisitor):
+    @visitormethod(Record)
+    def Record(self, path, node):
+        for (name, extra) in node.extras.items():
+            print(name)
+            print("    sel", extra["sel"])
+            print("    reg", extra["reg"])
+            print("    seltype", extra["seltype"])
+            print("    idx", extra["idx"])
         yield node
 
 
 def main():
     commands = {
+        "tree": (
+            TreeVisitor,
+            "list all records",
+        ),
         "list": (
             ListVisitor,
             "list available instructions",
@@ -123,7 +155,7 @@ def main():
 
     for (command, (visitor, helper)) in commands.items():
         parser = main_subparser.add_parser(command, help=helper)
-        if command not in ("list",):
+        if issubclass(visitor, InstructionVisitor):
             if command in ("extras",):
                 arg_cls = SVP64Instruction
             else:
@@ -139,16 +171,16 @@ def main():
     visitor = commands[command][0]()
 
     db = Database(find_wiki_dir())
-    records = next(db.subnodes(match=lambda node: isinstance(node, Records)))
-    if command in ("list",):
-        match = None
+    (path, records) = next(db.walk(match=lambda pair: isinstance(pair, Records)))
+    if not isinstance(visitor, InstructionVisitor):
+        match = lambda _: True
     else:
         insn = args.pop("insn")
         def match(record):
             return (isinstance(record, Record) and (record.name == insn))
 
-    for node in records.subnodes(match=match):
-        visit(visitor=visitor, node=node)
+    for (subpath, node) in records.walk(match=match):
+        visit(visitor=visitor, node=node, path=subpath)
 
 
 if __name__ == "__main__":