pvr: csbgen: Generate enum-to-string functions for debugging
authorMatt Coster <matt.coster@imgtec.com>
Wed, 25 May 2022 12:30:55 +0000 (13:30 +0100)
committerMarge Bot <emma+marge@anholt.net>
Fri, 25 Nov 2022 16:42:55 +0000 (16:42 +0000)
All enums should be unambiguous, so an error is raised when multiple
enum variants with the same value are encountered. When no enum
variants match the provided value, NULL is returned. This allows the
to-string functions to double as validators.

Signed-off-by: Matt Coster <matt.coster@imgtec.com>
Reviewed-by: Karmjit Mahil <Karmjit.Mahil@imgtec.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/18948>

src/imagination/csbgen/gen_pack_header.py
src/imagination/csbgen/pvr_packet_helpers.h
src/imagination/vulkan/pvr_csb.h

index 1261837..d7bdf90 100644 (file)
@@ -231,8 +231,25 @@ class Enum(Node):
         if element.name in self._values:
             raise RuntimeError("Value is being redefined. Value: '%s'" % element.name)
 
+        if element.value in self._values.values():
+            raise RuntimeError("Ambiguous enum value detected. Value: '%s'" % element.value)
+
         self._values[element.name] = element
 
+    def _emit_to_str(self) -> None:
+        print(textwrap.dedent("""\
+            static const char *
+            %s_to_str(const enum %s value)
+            {""") % (self.full_name, self.full_name))
+
+        print("    switch (value) {")
+        for value in self._values.values():
+            print("    case %s: return \"%s\";" % (value.full_name, value.name))
+        print("    default: return NULL;")
+        print("    }")
+
+        print("}\n")
+
     def emit(self) -> None:
         # This check is invalid if tags other than Value can be nested within an enum.
         if not self._values.values():
@@ -243,6 +260,8 @@ class Enum(Node):
             value.emit()
         print("};\n")
 
+        self._emit_to_str()
+
 
 class Value(Node):
     __slots__ = ["value"]
index ae2b5fd..59d555d 100644 (file)
@@ -30,6 +30,7 @@
 #include <assert.h>
 #include <math.h>
 #include <stdbool.h>
+#include <stddef.h>
 #include <stdint.h>
 #include <stdio.h>
 
index 7a739eb..fa21988 100644 (file)
@@ -137,6 +137,7 @@ VkResult pvr_csb_emit_terminate(struct pvr_csb *csb);
 #define pvr_cmd_header(x) PVRX(x##_header)
 #define pvr_cmd_pack(x) PVRX(x##_pack)
 #define pvr_cmd_unpack(x) PVRX(x##_unpack)
+#define pvr_cmd_enum_to_str(x) PVRX(x##_to_str)
 
 /**
  * \brief Merges dwords0 and dwords1 arrays and stores the result into the