isaspec: Add BitSetEnumValue object
authorChristian Gmeiner <cgmeiner@igalia.com>
Mon, 4 Sep 2023 13:18:15 +0000 (15:18 +0200)
committerMarge Bot <emma+marge@anholt.net>
Tue, 3 Oct 2023 12:07:04 +0000 (12:07 +0000)
There might be cases where you describe an enum in isaspec and want it to use
for decoding but also for codegen with e.g. mako.

Lets have a look at the following exmaple:

<enum name="#cond">
<value val="0" display=""/>    <!-- always: display nothing -->
<value val="1" display=".gt"/>
...
</enum>

In the decoding case we want that nothing gets displayed if #cond has the value of "0". For
codegen with mako this could result in the following C code:

enum PACKED cond {
   COND_ = 0,
   COND_GT = 1,
   ...
};

What you really want is this:

enum PACKED cond {
   COND_ALWAYS = 0,
   COND_GT = 1,
   ...
};

To make this possible introduce BitSetEnumValue class which represents
an isaspec xml enum. It holds the value, displayname and now a name.

With the  __str__ method the old behaviour is still intact.

Signed-off-by: Christian Gmeiner <cgmeiner@igalia.com>
Reviewed-by: Rob Clark <robdclark@chromium.org>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/25451>

src/compiler/isaspec/isa.py

index 32defb8..dda0b7c 100644 (file)
@@ -405,18 +405,36 @@ class BitSetTemplate(object):
         self.display = xml.text.strip()
         dbg("found template '{}: {}'".format(self.name, self.display))
 
+class BitSetEnumValue(object):
+    """Class that encapsulates an enum value
+    """
+    def __init__(self, isa, xml):
+        self.isa = isa
+        self.displayname = xml.attrib['display']
+        self.value = xml.attrib['val']
+        self.name = xml.attrib.get('name')
+
+    def __str__(self):
+        return self.displayname
+
+    def get_name(self):
+        return self.name or self.displayname
+
+    def get_value(self):
+        return self.value
+
 class BitSetEnum(object):
     """Class that encapsulates an enum declaration
     """
     def __init__(self, isa, xml):
         self.isa = isa
         self.name = xml.attrib['name']
+
         # Table mapping value to name
-        # TODO currently just mapping to 'display' name, but if we
-        # need more attributes then maybe need BitSetEnumValue?
         self.values = {}
         for value in xml.findall('value'):
-            self.values[value.attrib['val']] = value.attrib['display']
+            v = BitSetEnumValue(self, value)
+            self.values[v.get_value()] = v
 
     def get_c_name(self):
         return 'enum_' + get_c_name(self.name)