blob: 2237b7d148a5368aafcef39a211114e27d6a9452 [file] [log] [blame]
Brian Paul78123bb2005-02-22 15:39:46 +00001#!/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
29from xml.sax import saxutils
30from xml.sax import make_parser
31from xml.sax.handler import feature_namespaces
32
33import license
34import gl_XML
35import sys, getopt
36
37class PrintGlEnums(gl_XML.FilterGLAPISpecBase):
38 name = "gl_enums.py (from Mesa)"
39
40 def __init__(self):
41 gl_XML.FilterGLAPISpecBase.__init__(self)
42 self.license = license.bsd_license_template % ( \
43"""Copyright (C) 1999-2005 Brian Paul All Rights Reserved.""", "BRIAN PAUL")
Ian Romanickf3a6e4f2005-02-26 01:09:35 +000044 self.enum_table = {}
Brian Paul78123bb2005-02-22 15:39:46 +000045
46
47 def printRealHeader(self):
48 print '#include "glheader.h"'
49 print '#include "enums.h"'
50 print '#include "imports.h"'
51 print ''
52 print 'typedef struct {'
Ian Romanickf3a6e4f2005-02-26 01:09:35 +000053 print ' size_t offset;'
Brian Paul78123bb2005-02-22 15:39:46 +000054 print ' int n;'
55 print '} enum_elt;'
56 print ''
Brian Paul78123bb2005-02-22 15:39:46 +000057 return
58
Brian Paul8f5f6b32005-02-23 16:36:17 +000059 def printBody(self):
Brian Paul78123bb2005-02-22 15:39:46 +000060 print """
Brian Paul78123bb2005-02-22 15:39:46 +000061#define Elements(x) sizeof(x)/sizeof(*x)
62
63typedef int (*cfunc)(const void *, const void *);
64
Ian Romanickf3a6e4f2005-02-26 01:09:35 +000065/**
66 * Compare a key name to an element in the \c all_enums array.
67 *
68 * \c bsearch always passes the key as the first parameter and the pointer
69 * to the array element as the second parameter. We can elimiate some
70 * extra work by taking advantage of that fact.
71 *
72 * \param a Pointer to the desired enum name.
73 * \param b Pointer to an element of the \c all_enums array.
Brian Paul78123bb2005-02-22 15:39:46 +000074 */
Ian Romanickf3a6e4f2005-02-26 01:09:35 +000075static int compar_name( const char *a, const enum_elt *b )
Brian Paul78123bb2005-02-22 15:39:46 +000076{
Ian Romanickf3a6e4f2005-02-26 01:09:35 +000077 return _mesa_strcmp( a, & enum_string_table[ b->offset ] );
Brian Paul78123bb2005-02-22 15:39:46 +000078}
79
Ian Romanickf3a6e4f2005-02-26 01:09:35 +000080/**
81 * Compare a key enum value to an element in the \c all_enums array.
82 *
83 * \c bsearch always passes the key as the first parameter and the pointer
84 * to the array element as the second parameter. We can elimiate some
85 * extra work by taking advantage of that fact.
86 *
87 * \param a Pointer to the desired enum name.
88 * \param b Pointer to an index into the \c all_enums array.
89 */
90static int compar_nr( const int *a, const unsigned *b )
Brian Paul78123bb2005-02-22 15:39:46 +000091{
Ian Romanickf3a6e4f2005-02-26 01:09:35 +000092 return a[0] - all_enums[*b].n;
Brian Paul78123bb2005-02-22 15:39:46 +000093}
94
95
96static char token_tmp[20];
97
98const char *_mesa_lookup_enum_by_nr( int nr )
99{
Ian Romanickf3a6e4f2005-02-26 01:09:35 +0000100 unsigned * i;
Brian Paul78123bb2005-02-22 15:39:46 +0000101
Ian Romanickf3a6e4f2005-02-26 01:09:35 +0000102 i = (unsigned *)bsearch( & nr, reduced_enums, Elements(reduced_enums),
103 sizeof(reduced_enums[0]), (cfunc) compar_nr );
Brian Paul78123bb2005-02-22 15:39:46 +0000104
Ian Romanickf3a6e4f2005-02-26 01:09:35 +0000105 if ( i != NULL ) {
106 return & enum_string_table[ all_enums[ *i ].offset ];
Brian Paul78123bb2005-02-22 15:39:46 +0000107 }
108 else {
Ian Romanickf3a6e4f2005-02-26 01:09:35 +0000109 /* this is not re-entrant safe, no big deal here */
Brian Paul78123bb2005-02-22 15:39:46 +0000110 _mesa_sprintf(token_tmp, "0x%x", nr);
111 return token_tmp;
112 }
113}
Brian Paul8f5f6b32005-02-23 16:36:17 +0000114
115int _mesa_lookup_enum_by_name( const char *symbol )
116{
Ian Romanickf3a6e4f2005-02-26 01:09:35 +0000117 enum_elt * f = NULL;
Brian Paul8f5f6b32005-02-23 16:36:17 +0000118
Ian Romanickf3a6e4f2005-02-26 01:09:35 +0000119 if ( symbol != NULL ) {
120 f = (enum_elt *)bsearch( symbol, all_enums, Elements(all_enums),
121 sizeof( enum_elt ), (cfunc) compar_name );
122 }
Brian Paul8f5f6b32005-02-23 16:36:17 +0000123
Ian Romanickf3a6e4f2005-02-26 01:09:35 +0000124 return (f != NULL) ? f->n : -1;
Brian Paul8f5f6b32005-02-23 16:36:17 +0000125}
126
Brian Paul78123bb2005-02-22 15:39:46 +0000127"""
128 return
129
130 def printFunctions(self):
Ian Romanickf3a6e4f2005-02-26 01:09:35 +0000131 keys = self.enum_table.keys()
132 keys.sort()
133
134 name_table = []
135 enum_table = {}
136
137 for enum in keys:
138 low_pri = 9
139 for [name, pri] in self.enum_table[ enum ]:
140 name_table.append( [name, enum] )
141
142 if pri < low_pri:
143 low_pri = pri
144 enum_table[enum] = name
145
146
147 name_table.sort()
148
149 string_offsets = {}
150 i = 0;
151 print 'static const char enum_string_table[] = '
152 for [name, enum] in name_table:
153 print ' "%s\\0"' % (name)
154 string_offsets[ name ] = i
155 i += len(name) + 1
156
157 print ' ;'
158 print ''
159
160
161 print 'static const enum_elt all_enums[%u] =' % (len(name_table))
162 print '{'
163 for [name, enum] in name_table:
164 print ' { % 5u, 0x%08X }, /* %s */' % (string_offsets[name], enum, name)
165 print '};'
166 print ''
167
168 print 'static const unsigned reduced_enums[%u] =' % (len(keys))
169 print '{'
170 for enum in keys:
171 name = enum_table[ enum ]
172 if [name, enum] not in name_table:
173 print ' /* Error! %s, 0x%04x */ 0,' % (name, enum)
174 else:
175 i = name_table.index( [name, enum] )
176
177 print ' % 4u, /* %s */' % (i, name)
178 print '};'
179
180
Brian Paul8f5f6b32005-02-23 16:36:17 +0000181 self.printBody();
Brian Paul78123bb2005-02-22 15:39:46 +0000182 return
183
184
Ian Romanickf3a6e4f2005-02-26 01:09:35 +0000185 def append(self, object_type, obj):
186 if object_type == "enum":
187 if obj.value not in self.enum_table:
188 self.enum_table[ obj.value ] = []
189
190
191 # Prevent duplicate names in the enum table.
192
193 if obj.name not in self.enum_table[ obj.value ]:
194
195 # Calculate a "priority" for this enum name.
196 # When we lookup an enum by number, there may
197 # be many possible names, but only one can be
198 # returned. The priority is used by this
199 # script to select which name is the "best".
200 #
201 # Highest precedence is given to "core" name.
202 # ARB extension names have the next highest,
203 # followed by EXT extension names. Vendor
204 # extension names are the lowest.
205
206 if obj.category.startswith( "GL_VERSION_" ):
207 priority = 0
208 elif obj.category.startswith( "GL_ARB_" ):
209 priority = 1
210 elif obj.category.startswith( "GL_EXT_" ):
211 priority = 2
212 else:
213 priority = 3
214
215 self.enum_table[ obj.value ].append( [obj.name, priority] )
216
217 else:
218 gl_XML.FilterGLAPISpecBase.append(self, object_type, obj)
219
220
Brian Paul78123bb2005-02-22 15:39:46 +0000221def show_usage():
222 print "Usage: %s [-f input_file_name]" % sys.argv[0]
223 sys.exit(1)
224
225if __name__ == '__main__':
226 file_name = "gl_API.xml"
Ian Romanickf3a6e4f2005-02-26 01:09:35 +0000227
Brian Paul78123bb2005-02-22 15:39:46 +0000228 try:
229 (args, trail) = getopt.getopt(sys.argv[1:], "f:")
230 except Exception,e:
231 show_usage()
232
233 for (arg,val) in args:
234 if arg == "-f":
235 file_name = val
236
237 dh = PrintGlEnums()
238
239 parser = make_parser()
240 parser.setFeature(feature_namespaces, 0)
241 parser.setContentHandler(dh)
242
243 f = open(file_name)
244
245 dh.printHeader()
246 parser.parse(f)
247 dh.printFooter()