Turn off debug
[mesa.git] / src / mesa / glapi / gl_enums.py
1 #!/usr/bin/python2
2 # -*- Mode: Python; py-indent-offset: 8 -*-
3
4 # (C) Copyright Zack Rusin 2005
5 # All Rights Reserved.
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining a
8 # copy of this software and associated documentation files (the "Software"),
9 # to deal in the Software without restriction, including without limitation
10 # on the rights to use, copy, modify, merge, publish, distribute, sub
11 # license, and/or sell copies of the Software, and to permit persons to whom
12 # the Software is furnished to do so, subject to the following conditions:
13 #
14 # The above copyright notice and this permission notice (including the next
15 # paragraph) shall be included in all copies or substantial portions of the
16 # Software.
17 #
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 # IBM AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 # IN THE SOFTWARE.
25 #
26 # Authors:
27 # Zack Rusin <zack@kde.org>
28
29 import license
30 import gl_XML
31 import sys, getopt
32
33 class PrintGlEnums(gl_XML.FilterGLAPISpecBase):
34
35 def __init__(self):
36 gl_XML.FilterGLAPISpecBase.__init__(self)
37
38 self.name = "gl_enums.py (from Mesa)"
39 self.license = license.bsd_license_template % ( \
40 """Copyright (C) 1999-2005 Brian Paul All Rights Reserved.""", "BRIAN PAUL")
41 self.enum_table = {}
42
43
44 def printRealHeader(self):
45 print '#include "glheader.h"'
46 print '#include "enums.h"'
47 print '#include "imports.h"'
48 print ''
49 print 'typedef struct {'
50 print ' size_t offset;'
51 print ' int n;'
52 print '} enum_elt;'
53 print ''
54 return
55
56 def printBody(self):
57 print """
58 #define Elements(x) sizeof(x)/sizeof(*x)
59
60 typedef int (*cfunc)(const void *, const void *);
61
62 /**
63 * Compare a key name to an element in the \c all_enums array.
64 *
65 * \c bsearch always passes the key as the first parameter and the pointer
66 * to the array element as the second parameter. We can elimiate some
67 * extra work by taking advantage of that fact.
68 *
69 * \param a Pointer to the desired enum name.
70 * \param b Pointer to an element of the \c all_enums array.
71 */
72 static int compar_name( const char *a, const enum_elt *b )
73 {
74 return _mesa_strcmp( a, & enum_string_table[ b->offset ] );
75 }
76
77 /**
78 * Compare a key enum value to an element in the \c all_enums array.
79 *
80 * \c bsearch always passes the key as the first parameter and the pointer
81 * to the array element as the second parameter. We can elimiate some
82 * extra work by taking advantage of that fact.
83 *
84 * \param a Pointer to the desired enum name.
85 * \param b Pointer to an index into the \c all_enums array.
86 */
87 static int compar_nr( const int *a, const unsigned *b )
88 {
89 return a[0] - all_enums[*b].n;
90 }
91
92
93 static char token_tmp[20];
94
95 const char *_mesa_lookup_enum_by_nr( int nr )
96 {
97 unsigned * i;
98
99 i = (unsigned *)bsearch( & nr, reduced_enums, Elements(reduced_enums),
100 sizeof(reduced_enums[0]), (cfunc) compar_nr );
101
102 if ( i != NULL ) {
103 return & enum_string_table[ all_enums[ *i ].offset ];
104 }
105 else {
106 /* this is not re-entrant safe, no big deal here */
107 _mesa_sprintf(token_tmp, "0x%x", nr);
108 return token_tmp;
109 }
110 }
111
112 int _mesa_lookup_enum_by_name( const char *symbol )
113 {
114 enum_elt * f = NULL;
115
116 if ( symbol != NULL ) {
117 f = (enum_elt *)bsearch( symbol, all_enums, Elements(all_enums),
118 sizeof( enum_elt ), (cfunc) compar_name );
119 }
120
121 return (f != NULL) ? f->n : -1;
122 }
123
124 """
125 return
126
127 def printFunctions(self):
128 keys = self.enum_table.keys()
129 keys.sort()
130
131 name_table = []
132 enum_table = {}
133
134 for enum in keys:
135 low_pri = 9
136 for [name, pri] in self.enum_table[ enum ]:
137 name_table.append( [name, enum] )
138
139 if pri < low_pri:
140 low_pri = pri
141 enum_table[enum] = name
142
143
144 name_table.sort()
145
146 string_offsets = {}
147 i = 0;
148 print 'static const char enum_string_table[] = '
149 for [name, enum] in name_table:
150 print ' "%s\\0"' % (name)
151 string_offsets[ name ] = i
152 i += len(name) + 1
153
154 print ' ;'
155 print ''
156
157
158 print 'static const enum_elt all_enums[%u] =' % (len(name_table))
159 print '{'
160 for [name, enum] in name_table:
161 print ' { % 5u, 0x%08X }, /* %s */' % (string_offsets[name], enum, name)
162 print '};'
163 print ''
164
165 print 'static const unsigned reduced_enums[%u] =' % (len(keys))
166 print '{'
167 for enum in keys:
168 name = enum_table[ enum ]
169 if [name, enum] not in name_table:
170 print ' /* Error! %s, 0x%04x */ 0,' % (name, enum)
171 else:
172 i = name_table.index( [name, enum] )
173
174 print ' % 4u, /* %s */' % (i, name)
175 print '};'
176
177
178 self.printBody();
179 return
180
181
182 def append(self, object_type, obj):
183 if object_type == "enum":
184 if obj.value not in self.enum_table:
185 self.enum_table[ obj.value ] = []
186
187
188 # Prevent duplicate names in the enum table.
189
190 found_it = 0
191 for [n, junk] in self.enum_table[ obj.value ]:
192 if n == obj.name:
193 found_it = 1
194 break
195
196 if not found_it:
197
198 # Calculate a "priority" for this enum name.
199 # When we lookup an enum by number, there may
200 # be many possible names, but only one can be
201 # returned. The priority is used by this
202 # script to select which name is the "best".
203 #
204 # Highest precedence is given to "core" name.
205 # ARB extension names have the next highest,
206 # followed by EXT extension names. Vendor
207 # extension names are the lowest.
208
209 if obj.category.startswith( "GL_VERSION_" ):
210 priority = 0
211 elif obj.category.startswith( "GL_ARB_" ):
212 priority = 1
213 elif obj.category.startswith( "GL_EXT_" ):
214 priority = 2
215 else:
216 priority = 3
217
218 self.enum_table[ obj.value ].append( [obj.name, priority] )
219
220 else:
221 gl_XML.FilterGLAPISpecBase.append(self, object_type, obj)
222
223
224 def show_usage():
225 print "Usage: %s [-f input_file_name]" % sys.argv[0]
226 sys.exit(1)
227
228 if __name__ == '__main__':
229 file_name = "gl_API.xml"
230
231 try:
232 (args, trail) = getopt.getopt(sys.argv[1:], "f:")
233 except Exception,e:
234 show_usage()
235
236 for (arg,val) in args:
237 if arg == "-f":
238 file_name = val
239
240 dh = PrintGlEnums()
241 gl_XML.parse_GL_API( dh, file_name )