intel/compiler: use the same name for nir shaders in brw_compile_* functions
[mesa.git] / src / intel / genxml / gen_pack_header.py
index c3d712c7a86e1580ec0fadcf8b4203fb07b21d69..feeaed01e09caa12fa187b48d48252f3eed17402 100644 (file)
@@ -3,12 +3,14 @@
 from __future__ import (
     absolute_import, division, print_function, unicode_literals
 )
+import argparse
 import ast
 import xml.parsers.expat
 import re
 import sys
 import copy
 import textwrap
+from util import *
 
 license =  """/*
  * Copyright (C) 2016 Intel Corporation
@@ -68,13 +70,13 @@ union __gen_value {
    uint32_t dw;
 };
 
-static inline uint64_t
+static inline __attribute__((always_inline)) uint64_t
 __gen_mbo(uint32_t start, uint32_t end)
 {
    return (~0ull >> (64 - (end - start + 1))) << start;
 }
 
-static inline uint64_t
+static inline __attribute__((always_inline)) uint64_t
 __gen_uint(uint64_t v, uint32_t start, NDEBUG_UNUSED uint32_t end)
 {
    __gen_validate_value(v);
@@ -90,7 +92,7 @@ __gen_uint(uint64_t v, uint32_t start, NDEBUG_UNUSED uint32_t end)
    return v << start;
 }
 
-static inline uint64_t
+static inline __attribute__((always_inline)) uint64_t
 __gen_sint(int64_t v, uint32_t start, uint32_t end)
 {
    const int width = end - start + 1;
@@ -110,7 +112,7 @@ __gen_sint(int64_t v, uint32_t start, uint32_t end)
    return (v & mask) << start;
 }
 
-static inline uint64_t
+static inline __attribute__((always_inline)) uint64_t
 __gen_offset(uint64_t v, NDEBUG_UNUSED uint32_t start, NDEBUG_UNUSED uint32_t end)
 {
    __gen_validate_value(v);
@@ -123,14 +125,14 @@ __gen_offset(uint64_t v, NDEBUG_UNUSED uint32_t start, NDEBUG_UNUSED uint32_t en
    return v;
 }
 
-static inline uint32_t
+static inline __attribute__((always_inline)) uint32_t
 __gen_float(float v)
 {
    __gen_validate_value(v);
    return ((union __gen_value) { .f = (v) }).dw;
 }
 
-static inline uint64_t
+static inline __attribute__((always_inline)) uint64_t
 __gen_sfixed(float v, uint32_t start, uint32_t end, uint32_t fract_bits)
 {
    __gen_validate_value(v);
@@ -149,7 +151,7 @@ __gen_sfixed(float v, uint32_t start, uint32_t end, uint32_t fract_bits)
    return (int_val & mask) << start;
 }
 
-static inline uint64_t
+static inline __attribute__((always_inline)) uint64_t
 __gen_ufixed(float v, uint32_t start, NDEBUG_UNUSED uint32_t end, uint32_t fract_bits)
 {
    __gen_validate_value(v);
@@ -181,47 +183,12 @@ __gen_ufixed(float v, uint32_t start, NDEBUG_UNUSED uint32_t end, uint32_t fract
 
 """
 
-def to_alphanum(name):
-    substitutions = {
-        ' ': '',
-        '/': '',
-        '[': '',
-        ']': '',
-        '(': '',
-        ')': '',
-        '-': '',
-        ':': '',
-        '.': '',
-        ',': '',
-        '=': '',
-        '>': '',
-        '#': '',
-        'α': 'alpha',
-        '&': '',
-        '*': '',
-        '"': '',
-        '+': '',
-        '\'': '',
-    }
-
-    for i, j in substitutions.items():
-        name = name.replace(i, j)
-
-    return name
-
-def safe_name(name):
-    name = to_alphanum(name)
-    if not name[0].isalpha():
-        name = '_' + name
-
-    return name
-
 def num_from_str(num_str):
     if num_str.lower().startswith('0x'):
         return int(num_str, base=16)
-    else:
-        assert not num_str.startswith('0'), 'octals numbers not allowed'
-        return int(num_str)
+
+    assert not num_str.startswith('0'), 'octals numbers not allowed'
+    return int(num_str)
 
 class Field(object):
     ufixed_pattern = re.compile(r"u(\d+)\.(\d+)")
@@ -306,7 +273,7 @@ class Field(object):
         print("   %-36s %s%s;" % (type, self.name, dim))
 
         prefix = ""
-        if len(self.values) > 0 and self.default == None:
+        if self.values and self.default is None:
             if self.prefix:
                 prefix = self.prefix + "_"
 
@@ -340,7 +307,7 @@ class Group(object):
 
     def collect_dwords(self, dwords, start, dim):
         for field in self.fields:
-            if type(field) is Group:
+            if isinstance(field, Group):
                 if field.count == 1:
                     field.collect_dwords(dwords, start + field.start, dim)
                 else:
@@ -424,7 +391,7 @@ class Group(object):
             # to the dword for those fields.
             field_index = 0
             for field in dw.fields:
-                if type(field) is Field and field.is_struct_type():
+                if isinstance(field, Field) and field.is_struct_type():
                     name = field.name + field.dim
                     print("")
                     print("   uint32_t v%d_%d;" % (index, field_index))
@@ -490,7 +457,7 @@ class Group(object):
                     non_address_fields.append("/* unhandled field %s, type %s */\n" % \
                                               (name, field.type))
 
-            if len(non_address_fields) > 0:
+            if non_address_fields:
                 print(" |\n".join("      " + f for f in non_address_fields) + ";")
 
             if dw.size == 32:
@@ -531,8 +498,7 @@ class Parser(object):
     def gen_prefix(self, name):
         if name[0] == "_":
             return 'GEN%s%s' % (self.gen, name)
-        else:
-            return 'GEN%s_%s' % (self.gen, name)
+        return 'GEN%s_%s' % (self.gen, name)
 
     def gen_guard(self):
         return self.gen_prefix("PACK_H")
@@ -546,6 +512,13 @@ class Parser(object):
             if name == "instruction":
                 self.instruction = safe_name(attrs["name"])
                 self.length_bias = int(attrs["bias"])
+                if "engine" in attrs:
+                    self.instruction_engines = set(attrs["engine"].split('|'))
+                else:
+                    # When an instruction doesn't have the engine specified,
+                    # it is considered to be for all engines, so 'None' is used
+                    # to signify that the instruction belongs to all engines.
+                    self.instruction_engines = None
             elif name == "struct":
                 self.struct = safe_name(attrs["name"])
                 self.structs[attrs["name"]] = 1
@@ -612,7 +585,7 @@ class Parser(object):
     def emit_pack_function(self, name, group):
         name = self.gen_prefix(name)
         print(textwrap.dedent("""\
-            static inline void
+            static inline __attribute__((always_inline)) void
             %s_pack(__attribute__((unused)) __gen_user_data *data,
                   %s__attribute__((unused)) void * restrict dst,
                   %s__attribute__((unused)) const struct %s * restrict values)
@@ -629,7 +602,10 @@ class Parser(object):
 
     def emit_instruction(self):
         name = self.instruction
-        if not self.length == None:
+        if self.instruction_engines and not self.instruction_engines & self.engines:
+            return
+
+        if not self.length is None:
             print('#define %-33s %6d' %
                   (self.gen_prefix(name + "_length"), self.length))
         print('#define %-33s %6d' %
@@ -637,11 +613,17 @@ class Parser(object):
 
         default_fields = []
         for field in self.group.fields:
-            if not type(field) is Field:
+            if not isinstance(field, Field):
                 continue
-            if field.default == None:
+            if field.default is None:
                 continue
-            default_fields.append("   .%-35s = %6d" % (field.name, field.default))
+
+            if field.is_builtin_type():
+                default_fields.append("   .%-35s = %6d" % (field.name, field.default))
+            else:
+                # Default values should not apply to structures
+                assert field.is_enum_type()
+                default_fields.append("   .%-35s = (enum %s) %6d" % (field.name, self.gen_prefix(safe_name(field.type)), field.default))
 
         if default_fields:
             print('#define %-40s\\' % (self.gen_prefix(name + '_header')))
@@ -654,11 +636,11 @@ class Parser(object):
 
     def emit_register(self):
         name = self.register
-        if not self.reg_num == None:
+        if not self.reg_num is None:
             print('#define %-33s 0x%04x' %
                   (self.gen_prefix(name + "_num"), self.reg_num))
 
-        if not self.length == None:
+        if not self.length is None:
             print('#define %-33s %6d' %
                   (self.gen_prefix(name + "_length"), self.length))
 
@@ -667,7 +649,7 @@ class Parser(object):
 
     def emit_struct(self):
         name = self.struct
-        if not self.length == None:
+        if not self.length is None:
             print('#define %-33s %6d' %
                   (self.gen_prefix(name + "_length"), self.length))
 
@@ -689,11 +671,36 @@ class Parser(object):
         self.parser.ParseFile(file)
         file.close()
 
-if len(sys.argv) < 2:
-    print("No input xml file specified")
-    sys.exit(1)
+def parse_args():
+    p = argparse.ArgumentParser()
+    p.add_argument('xml_source', metavar='XML_SOURCE',
+                   help="Input xml file")
+    p.add_argument('--engines', nargs='?', type=str, default='render',
+                   help="Comma-separated list of engines whose instructions should be parsed (default: %(default)s)")
+
+    pargs = p.parse_args()
+
+    if pargs.engines is None:
+        print("No engines specified")
+        sys.exit(1)
+
+    return pargs
+
+def main():
+    pargs = parse_args()
+
+    input_file = pargs.xml_source
+    engines = pargs.engines.split(',')
+    valid_engines = [ 'render', 'blitter', 'video' ]
+    if set(engines) - set(valid_engines):
+        print("Invalid engine specified, valid engines are:\n")
+        for e in valid_engines:
+            print("\t%s" % e)
+        sys.exit(1)
 
-input_file = sys.argv[1]
+    p = Parser()
+    p.engines = set(engines)
+    p.parse(input_file)
 
-p = Parser()
-p.parse(input_file)
+if __name__ == '__main__':
+    main()