spirv: Update headers and grammar json
[mesa.git] / src / compiler / spirv / spirv_info_c.py
index 11235cfa3e416c85afceaf8bc12f6c2632819db0..29baec1fc307ac925855b8ba454b6b29e03b3295 100644 (file)
@@ -36,14 +36,30 @@ def collect_data(spirv, kind):
 
     # There are some duplicate values in some of the tables (thanks guys!), so
     # filter them out.
-    last_value = -1
+    seen = set()
     values = []
     for x in operands["enumerants"]:
-        if x["value"] != last_value:
-            last_value = x["value"]
+        if x["value"] not in seen:
+            seen.add(x["value"])
             values.append(x["enumerant"])
 
-    return (kind, values)
+    return (kind, values, operands["category"])
+
+def collect_opcodes(spirv):
+    seen = set()
+    values = []
+    for x in spirv["instructions"]:
+        # Handle aliases by choosing the first one in the grammar.
+        # E.g. OpDecorateString and OpDecorateStringGOOGLE share same opcode.
+        if x["opcode"] in seen:
+            continue
+        opcode = x["opcode"]
+        name = x["opname"]
+        assert name.startswith("Op")
+        values.append(name[2:])
+        seen.add(opcode)
+
+    return ("Op", values, None)
 
 def parse_args():
     p = argparse.ArgumentParser()
@@ -52,12 +68,29 @@ def parse_args():
     return p.parse_args()
 
 TEMPLATE  = Template("""\
-/* DO NOT EDIT - This file generated automatically by spirv_info_c.py script */
+/* DO NOT EDIT - This file is generated automatically by spirv_info_c.py script */
 
 """ + COPYRIGHT + """\
 #include "spirv_info.h"
-% for kind,values in info:
+% for kind,values,category in info:
+
+% if category == "BitEnum":
+const char *
+spirv_${kind.lower()}_to_string(Spv${kind}Mask v)
+{
+   switch (v) {
+    % for name in values:
+    %if name != "None":
+   case Spv${kind}${name}Mask: return "Spv${kind}${name}";
+    % else:
+   case Spv${kind}MaskNone: return "Spv${kind}${name}";
+    % endif
+    % endfor
+   }
 
+   return "unknown";
+}
+% else:
 const char *
 spirv_${kind.lower()}_to_string(Spv${kind} v)
 {
@@ -70,6 +103,7 @@ spirv_${kind.lower()}_to_string(Spv${kind} v)
 
    return "unknown";
 }
+% endif
 % endfor
 """)
 
@@ -78,8 +112,20 @@ if __name__ == "__main__":
 
     spirv_info = json.JSONDecoder().decode(open(pargs.json, "r").read())
 
-    capabilities = collect_data(spirv_info, "Capability")
-    decorations = collect_data(spirv_info, "Decoration")
+    info = [
+        collect_data(spirv_info, "AddressingModel"),
+        collect_data(spirv_info, "BuiltIn"),
+        collect_data(spirv_info, "Capability"),
+        collect_data(spirv_info, "Decoration"),
+        collect_data(spirv_info, "Dim"),
+        collect_data(spirv_info, "ExecutionMode"),
+        collect_data(spirv_info, "ExecutionModel"),
+        collect_data(spirv_info, "ImageFormat"),
+        collect_data(spirv_info, "MemoryModel"),
+        collect_data(spirv_info, "StorageClass"),
+        collect_data(spirv_info, "ImageOperands"),
+        collect_opcodes(spirv_info),
+    ]
 
     with open(pargs.out, 'w') as f:
-        f.write(TEMPLATE.render(info=[capabilities, decorations]))
+        f.write(TEMPLATE.render(info=info))