summaryrefslogtreecommitdiff
path: root/src/mesa/glapi/typeexpr.py
diff options
context:
space:
mode:
authorIan Romanick <idr@us.ibm.com>2005-06-21 23:42:43 +0000
committerIan Romanick <idr@us.ibm.com>2005-06-21 23:42:43 +0000
commit66a5548fbbf4c04a74b0bbc451718a8fc95502d9 (patch)
treef0e5a5964bca25e1e48c62bdb09e7f4c2fb88ce1 /src/mesa/glapi/typeexpr.py
parentf292e13a20a262411360bf4af5337595976cebc7 (diff)
Mammoth update to the Python code generator scripts that live in
src/mesa/glapi. Basically, the scripts that did simple things (like gl_offsets.py) were simple, and the scripts that did more complicated things (like glX_proto_send.py) were getting progressively more and more out of control. So, I re-write the foundation classes on which everything is based. One problem with the existing code is that the division between the GL API database representation and the way the output code is generated was either blury or nonexistant. The new code somewhat follows the Model-View-Controller pattern, minus the Controller. There is a distinct set of classes that model the API data, and there is a distinct set of classes that generate code from that data. One big change is in the class that represents GL functions (was glFunction, is now gl_function). There used to be an instance of this calls for each function and for each alias to that function. For example, there was an instance for PointParameterivSGIS, PointParameterivEXT, PointParameterivARB, and PointParameteriv. In the new code, there is one instance. Each instance has a list of entrypoint names for the function. In the next revision, this will allow a couple useful things. The script will be able to verify that the parameters, return type, and GLX protocol for a function and all it's aliases match. It will also allow aliases to be represented in the XML more compactly. Instead of repeating all the information, an alias can be listed as: <function name="PointParameterivARB" alias="PointParameterivEXT"/> Because the data representation was changed, the order that the alias functions are processed by the scripts also changed. This accounts for at least 2,700 of the ~3,600 lines of diffs in the generated code. Most of the remaining ~900 lines of diffs are the result of bugs *fixed* by the new scripts. The old scripts also generated code with some bugs in it. These bugs were discovered while the new code was being written. These changes were discussed on the mesa3d-dev mailing list back at the end of May: http://marc.theaimsgroup.com/?t=111714569000004&r=1&w=2 Xorg bug: 3197, 3208
Diffstat (limited to 'src/mesa/glapi/typeexpr.py')
-rw-r--r--src/mesa/glapi/typeexpr.py288
1 files changed, 288 insertions, 0 deletions
diff --git a/src/mesa/glapi/typeexpr.py b/src/mesa/glapi/typeexpr.py
new file mode 100644
index 0000000000..eface2a521
--- /dev/null
+++ b/src/mesa/glapi/typeexpr.py
@@ -0,0 +1,288 @@
+#!/usr/bin/env python
+
+# (C) Copyright IBM Corporation 2005
+# All Rights Reserved.
+#
+# Permission is hereby granted, free of charge, to any person obtaining a
+# copy of this software and associated documentation files (the "Software"),
+# to deal in the Software without restriction, including without limitation
+# on the rights to use, copy, modify, merge, publish, distribute, sub
+# license, and/or sell copies of the Software, and to permit persons to whom
+# the Software is furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice (including the next
+# paragraph) shall be included in all copies or substantial portions of the
+# Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+# IBM AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+# IN THE SOFTWARE.
+#
+# Authors:
+# Ian Romanick <idr@us.ibm.com>
+
+import string, copy
+
+class type_node:
+ def __init__(self):
+ self.pointer = 0
+ self.const = 0
+ self.signed = 1
+ self.integer = 1
+
+ # If elements is set to non-zero, then field is an array.
+ self.elements = 0
+
+ self.name = None
+ self.size = 0
+ return
+
+
+ def string(self):
+ s = ""
+
+ if self.pointer:
+ s = "* "
+
+ if self.const:
+ s += "const "
+
+ if not self.pointer:
+ if self.integer:
+ if self.signed:
+ s += "signed "
+ else:
+ s += "unsigned "
+
+ if self.name:
+ s += "%s " % (self.name)
+
+ return s
+
+
+class type_table:
+ def __init__(self):
+ self.types_by_name = {}
+ return
+
+
+ def add_type(self, type_expr):
+ self.types_by_name[ type_expr.get_base_name() ] = type_expr
+ return
+
+
+ def find_type(self, name):
+ if name in self.types_by_name:
+ return self.types_by_name[ name ]
+ else:
+ return None
+
+
+def create_initial_types():
+ tt = type_table()
+
+ basic_types = [ ["char", 1, 1], \
+ ["short", 2, 1], \
+ ["int", 4, 1], \
+ ["long", 4, 1], \
+ ["float", 4, 0], \
+ ["double", 8, 0], \
+ ["enum", 4, 1] ]
+
+
+ for [type_name, type_size, integer] in basic_types:
+ te = type_expression(None)
+ tn = type_node()
+ tn.name = type_name
+ tn.size = type_size
+ tn.integer = integer
+ te.expr.append(tn)
+ tt.add_type( te )
+
+ type_expression.built_in_types = tt
+ return
+
+
+class type_expression:
+ built_in_types = None
+
+ def __init__(self, type_string, extra_types = None):
+ self.expr = []
+
+ if not type_string: return
+
+ self.original_string = type_string
+
+ if not type_expression.built_in_types:
+ raise RuntimeError("create_initial_types must be called before creating type_expression objects.")
+
+
+ elements = string.split( string.replace( type_string, "*", " * " ) )
+
+ const = 0
+ t = None
+ signed = 0
+ unsigned = 0
+
+ for i in elements:
+ if i == "const":
+ if t and t.pointer:
+ t.const = 1
+ else:
+ const = 1
+ elif i == "signed":
+ signed = 1
+ elif i == "unsigned":
+ unsigned = 1
+ elif i == "*":
+ # This is a quirky special-case because of the
+ # way the C works for types. If 'unsigned' is
+ # specified all by itself, it is treated the
+ # same as "unsigned int".
+
+ if unsigned:
+ self.set_base_type( "int", signed, unsigned, const, extra_types )
+ const = 0
+ signed = 0
+ unsigned = 0
+
+ if not self.expr:
+ raise RuntimeError("Invalid type expression (dangling pointer)")
+
+ if signed:
+ raise RuntimeError("Invalid type expression (signed / unsigned applied to pointer)")
+
+ t = type_node()
+ t.pointer = 1
+ self.expr.append( t )
+ else:
+ if self.expr:
+ raise RuntimeError('Invalid type expression (garbage after pointer qualifier -> "%s")' % (self.original_string))
+
+ self.set_base_type( i, signed, unsigned, const, extra_types )
+ const = 0
+ signed = 0
+ unsigned = 0
+
+ if signed and unsigned:
+ raise RuntimeError("Invalid type expression (both signed and unsigned specified)")
+
+
+ if const:
+ raise RuntimeError("Invalid type expression (dangling const)")
+
+ if unsigned:
+ raise RuntimeError("Invalid type expression (dangling signed)")
+
+ if signed:
+ raise RuntimeError("Invalid type expression (dangling unsigned)")
+
+ return
+
+
+ def set_base_type(self, type_name, signed, unsigned, const, extra_types):
+ te = type_expression.built_in_types.find_type( type_name )
+ if not te:
+ te = extra_types.find_type( type_name )
+
+ if not te:
+ raise RuntimeError('Unknown base type "%s".' % (type_name))
+
+ self.expr = copy.deepcopy(te.expr)
+
+ t = self.expr[ len(self.expr) - 1 ]
+ t.const = const
+ if signed:
+ t.signed = 1
+ elif unsigned:
+ t.signed = 0
+
+
+ def set_base_type_node(self, tn):
+ self.expr = [tn]
+ return
+
+
+ def set_elements(self, count):
+ tn = self.expr[0]
+
+ tn.elements = count
+ return
+
+
+ def string(self):
+ s = ""
+ for t in self.expr:
+ s += t.string()
+
+ return s
+
+
+ def get_base_type_node(self):
+ return self.expr[0]
+
+
+ def get_base_name(self):
+ if len(self.expr):
+ return self.expr[0].name
+ else:
+ return None
+
+
+ def get_element_size(self):
+ tn = self.expr[0]
+
+ if tn.elements:
+ return tn.elements * tn.size
+ else:
+ return tn.size
+
+
+ def get_element_count(self):
+ tn = self.expr[0]
+ return tn.elements
+
+
+ def get_stack_size(self):
+ tn = self.expr[ len(self.expr) - 1 ]
+
+ if tn.elements or tn.pointer:
+ return 4
+ elif not tn.integer:
+ return tn.size
+ else:
+ return 4
+
+
+ def is_pointer(self):
+ tn = self.expr[ len(self.expr) - 1 ]
+ return tn.pointer
+
+
+ def format_string(self):
+ tn = self.expr[ len(self.expr) - 1 ]
+ if tn.pointer:
+ return "%p"
+ elif not tn.integer:
+ return "%f"
+ else:
+ return "%d"
+
+
+
+if __name__ == '__main__':
+
+ types_to_try = [ "int", "int *", "const int *", "int * const", "const int * const", \
+ "unsigned * const *", \
+ "float", "const double", "double * const"]
+
+ create_initial_types()
+
+ for t in types_to_try:
+ print 'Trying "%s"...' % (t)
+ te = type_expression( t )
+ print 'Got "%s" (%u, %u).' % (te.string(), te.get_stack_size(), te.get_element_size())