power_insn: refactor databases composition
authorDmitry Selyutin <ghostmansd@gmail.com>
Thu, 1 Sep 2022 15:04:24 +0000 (18:04 +0300)
committerDmitry Selyutin <ghostmansd@gmail.com>
Fri, 2 Sep 2022 08:44:11 +0000 (11:44 +0300)
src/openpower/decoder/power_insn.py

index d352f46d98312f4dbc02690a7ac4e7f9a4084957..9147446d730240350fce771ccd83ca8305d2ca3a 100644 (file)
@@ -777,8 +777,48 @@ def parse(stream, factory):
     return tuple(map(factory, entries))
 
 
+class MarkdownDatabase:
+    def __init__(self):
+        db = {}
+        for (name, desc) in _ISA():
+            operands = []
+            if desc.regs:
+                (dynamic, *static) = desc.regs
+                operands.extend(dynamic)
+                operands.extend(static)
+            db[name] = Operands(insn=name, iterable=operands)
+        self.__db = db
+        return super().__init__()
+
+    def __iter__(self):
+        yield from self.__db.items()
+
+    def __getitem__(self, key):
+        return self.__db.__getitem__(key)
+
+
+class FieldsDatabase:
+    def __init__(self):
+        db = {}
+        df = _DecodeFields()
+        df.create_specs()
+        for (form, fields) in df.instrs.items():
+            if form in {"DQE", "TX"}:
+                continue
+            if form == "all":
+                form = "NONE"
+            db[_Form[form]] = Fields(fields)
+
+        self.__db = db
+
+        return super().__init__()
+
+    def __getitem__(self, key):
+        return self.__db.__getitem__(key)
+
+
 class PPCDatabase:
-    def __init__(self, root):
+    def __init__(self, root, mdwndb, fieldsdb):
         db = _collections.defaultdict(set)
         path = (root / "insndb.csv")
         with open(path, "r", encoding="UTF-8") as stream:
@@ -791,7 +831,11 @@ class PPCDatabase:
                 factory = _functools.partial(PPCRecord.CSV, opcode_cls=opcode_cls)
                 with open(path, "r", encoding="UTF-8") as stream:
                     db[section].update(parse(stream, factory))
+
         self.__db = db
+        self.__mdwndb = mdwndb
+        self.__fieldsdb = fieldsdb
+
         return super().__init__()
 
     def __getitem__(self, key):
@@ -803,11 +847,12 @@ class PPCDatabase:
                                 key.endswith(".") and
                                 name == key[:-1])):
                         return (section, record)
+
         return (None, None)
 
 
 class SVP64Database:
-    def __init__(self, root):
+    def __init__(self, root, ppcdb):
         db = set()
         pattern = _re.compile(r"^(?:LDST)?RM-(1P|2P)-.*?\.csv$")
         for (prefix, _, names) in _os.walk(root):
@@ -816,53 +861,23 @@ class SVP64Database:
                 path = (prefix / _pathlib.Path(name))
                 with open(path, "r", encoding="UTF-8") as stream:
                     db.update(parse(stream, SVP64Record.CSV))
+
         self.__db = {record.name:record for record in db}
+        self.__ppcdb = ppcdb
+
         return super().__init__()
 
     def __getitem__(self, key):
-        for name in key:
+        (_, record) = self.__ppcdb[key]
+        if record is None:
+            return None
+
+        for name in record.names:
             record = self.__db.get(name, None)
             if record is not None:
                 return record
-        return None
-
-
-class FieldsDatabase:
-    def __init__(self):
-        db = {}
-        df = _DecodeFields()
-        df.create_specs()
-        for (form, fields) in df.instrs.items():
-            if form in {"DQE", "TX"}:
-                continue
-            if form == "all":
-                form = "NONE"
-            db[_Form[form]] = Fields(fields)
-        self.__db = db
-        return super().__init__()
-
-    def __getitem__(self, key):
-        return self.__db.__getitem__(key)
-
-
-class MarkdownDatabase:
-    def __init__(self):
-        db = {}
-        for (name, desc) in _ISA():
-            operands = []
-            if desc.regs:
-                (dynamic, *static) = desc.regs
-                operands.extend(dynamic)
-                operands.extend(static)
-            db[name] = Operands(insn=name, iterable=operands)
-        self.__db = db
-        return super().__init__()
 
-    def __iter__(self):
-        yield from self.__db.items()
-
-    def __getitem__(self, key):
-        return self.__db.__getitem__(key)
+        return None
 
 
 class Database:
@@ -871,15 +886,15 @@ class Database:
 
         mdwndb = MarkdownDatabase()
         fieldsdb = FieldsDatabase()
-        svp64db = SVP64Database(root)
-        ppcdb = PPCDatabase(root)
+        ppcdb = PPCDatabase(root=root, mdwndb=mdwndb, fieldsdb=fieldsdb)
+        svp64db = SVP64Database(root=root, ppcdb=ppcdb)
 
         db = set()
         for (name, operands) in mdwndb:
             (section, ppc) = ppcdb[name]
             if ppc is None:
                 continue
-            svp64 = svp64db[ppc.names]
+            svp64 = svp64db[name]
             fields = fieldsdb[ppc.form]
             record = Record(name=name,
                 section=section, ppc=ppc, svp64=svp64,