power_fields: support traversing over instances
authorDmitry Selyutin <ghostmansd@gmail.com>
Sat, 17 Sep 2022 12:41:59 +0000 (15:41 +0300)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Sat, 17 Sep 2022 16:44:16 +0000 (17:44 +0100)
src/openpower/decoder/power_fields.py

index 9e4758b4a675e00ddffcd667e15940efc8c42f0c..1212687a940ab4a3733ed5ef769c8f5d76d7d9b6 100644 (file)
@@ -162,9 +162,6 @@ class FieldMeta(type):
     def span(cls):
         return cls.__members__
 
-    def traverse(cls, path):
-        yield (path, cls.__members__)
-
 
 class Field(Reference, metaclass=FieldMeta):
     def __len__(self):
@@ -184,6 +181,10 @@ class Field(Reference, metaclass=FieldMeta):
 
         return _selectconcat(*(self[bit] for bit in tuple(key)))
 
+    def traverse(self, path):
+        span = self.__class__.__members__
+        yield (path, self.storage[span], span)
+
 
 class MappingMeta(type):
     def __new__(metacls, clsname, bases, ns):
@@ -255,15 +256,6 @@ class MappingMeta(type):
         for field in cls.__members__.values():
             yield from field.span
 
-    def traverse(cls, path=""):
-        for (name, field) in cls:
-            if name == "_":
-                yield from field.traverse(path=path)
-            elif path == "":
-                yield from field.traverse(path=name)
-            else:
-                yield from field.traverse(path=f"{path}.{name}")
-
 
 class Mapping(Reference, metaclass=MappingMeta):
     def __init__(self, storage, **kwargs):
@@ -288,6 +280,15 @@ class Mapping(Reference, metaclass=MappingMeta):
 
         return self.__members[key]
 
+    def traverse(self, path=""):
+        for (name, member) in self.__members.items():
+            if name == "_":
+                yield from member.traverse(path=path)
+            elif path == "":
+                yield from member.traverse(path=name)
+            else:
+                yield from member.traverse(path=f"{path}.{name}")
+
 
 def decode_instructions(form):
     res = {}