blob: b32e0126631c174ade347f679d18fad4ad0e87a5 [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
Brian Paul78123bb2005-02-22 15:39:46 +000029import license
30import gl_XML
31import sys, getopt
32
Ian Romanick66a55482005-06-21 23:42:43 +000033class PrintGlEnums(gl_XML.gl_print_base):
Brian Paul78123bb2005-02-22 15:39:46 +000034
35 def __init__(self):
Ian Romanick66a55482005-06-21 23:42:43 +000036 gl_XML.gl_print_base.__init__(self)
Ian Romanick70dbbbf2005-05-26 16:59:47 +000037
38 self.name = "gl_enums.py (from Mesa)"
Brian Paul78123bb2005-02-22 15:39:46 +000039 self.license = license.bsd_license_template % ( \
40"""Copyright (C) 1999-2005 Brian Paul All Rights Reserved.""", "BRIAN PAUL")
Ian Romanickf3a6e4f2005-02-26 01:09:35 +000041 self.enum_table = {}
Brian Paul78123bb2005-02-22 15:39:46 +000042
43
44 def printRealHeader(self):
45 print '#include "glheader.h"'
Brian Paulf2c02322009-02-22 15:43:29 -070046 print '#include "mfeatures.h"'
Brian Paul78123bb2005-02-22 15:39:46 +000047 print '#include "enums.h"'
48 print '#include "imports.h"'
49 print ''
50 print 'typedef struct {'
Ian Romanickf3a6e4f2005-02-26 01:09:35 +000051 print ' size_t offset;'
Brian Paul78123bb2005-02-22 15:39:46 +000052 print ' int n;'
53 print '} enum_elt;'
54 print ''
Brian Paul78123bb2005-02-22 15:39:46 +000055 return
56
Ian Romanick66a55482005-06-21 23:42:43 +000057 def print_code(self):
Brian Paul78123bb2005-02-22 15:39:46 +000058 print """
Brian Paul78123bb2005-02-22 15:39:46 +000059typedef int (*cfunc)(const void *, const void *);
60
Ian Romanickf3a6e4f2005-02-26 01:09:35 +000061/**
62 * Compare a key name to an element in the \c all_enums array.
63 *
64 * \c bsearch always passes the key as the first parameter and the pointer
65 * to the array element as the second parameter. We can elimiate some
66 * extra work by taking advantage of that fact.
67 *
68 * \param a Pointer to the desired enum name.
69 * \param b Pointer to an element of the \c all_enums array.
Brian Paul78123bb2005-02-22 15:39:46 +000070 */
Ian Romanickf3a6e4f2005-02-26 01:09:35 +000071static int compar_name( const char *a, const enum_elt *b )
Brian Paul78123bb2005-02-22 15:39:46 +000072{
Ian Romanickf3a6e4f2005-02-26 01:09:35 +000073 return _mesa_strcmp( a, & enum_string_table[ b->offset ] );
Brian Paul78123bb2005-02-22 15:39:46 +000074}
75
Ian Romanickf3a6e4f2005-02-26 01:09:35 +000076/**
77 * Compare a key enum value to an element in the \c all_enums array.
78 *
79 * \c bsearch always passes the key as the first parameter and the pointer
80 * to the array element as the second parameter. We can elimiate some
81 * extra work by taking advantage of that fact.
82 *
83 * \param a Pointer to the desired enum name.
84 * \param b Pointer to an index into the \c all_enums array.
85 */
86static int compar_nr( const int *a, const unsigned *b )
Brian Paul78123bb2005-02-22 15:39:46 +000087{
Ian Romanickf3a6e4f2005-02-26 01:09:35 +000088 return a[0] - all_enums[*b].n;
Brian Paul78123bb2005-02-22 15:39:46 +000089}
90
91
92static char token_tmp[20];
93
94const char *_mesa_lookup_enum_by_nr( int nr )
95{
Ian Romanickf3a6e4f2005-02-26 01:09:35 +000096 unsigned * i;
Brian Paul78123bb2005-02-22 15:39:46 +000097
Brian Paul59cc9732008-11-10 14:42:02 -070098 i = (unsigned *) _mesa_bsearch(& nr, reduced_enums,
99 Elements(reduced_enums),
100 sizeof(reduced_enums[0]),
101 (cfunc) compar_nr);
Brian Paul78123bb2005-02-22 15:39:46 +0000102
Ian Romanickf3a6e4f2005-02-26 01:09:35 +0000103 if ( i != NULL ) {
104 return & enum_string_table[ all_enums[ *i ].offset ];
Brian Paul78123bb2005-02-22 15:39:46 +0000105 }
106 else {
Ian Romanickf3a6e4f2005-02-26 01:09:35 +0000107 /* this is not re-entrant safe, no big deal here */
Brian Paul78123bb2005-02-22 15:39:46 +0000108 _mesa_sprintf(token_tmp, "0x%x", nr);
109 return token_tmp;
110 }
111}
Brian Paul8f5f6b32005-02-23 16:36:17 +0000112
113int _mesa_lookup_enum_by_name( const char *symbol )
114{
Ian Romanickf3a6e4f2005-02-26 01:09:35 +0000115 enum_elt * f = NULL;
Brian Paul8f5f6b32005-02-23 16:36:17 +0000116
Ian Romanickf3a6e4f2005-02-26 01:09:35 +0000117 if ( symbol != NULL ) {
Brian Paul59cc9732008-11-10 14:42:02 -0700118 f = (enum_elt *) _mesa_bsearch(symbol, all_enums,
119 Elements(all_enums),
120 sizeof( enum_elt ),
121 (cfunc) compar_name);
Ian Romanickf3a6e4f2005-02-26 01:09:35 +0000122 }
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
Ian Romanick66a55482005-06-21 23:42:43 +0000130
131 def printBody(self, api):
132 self.process_enums( api )
133
Ian Romanickf3a6e4f2005-02-26 01:09:35 +0000134 keys = self.enum_table.keys()
135 keys.sort()
136
137 name_table = []
138 enum_table = {}
139
140 for enum in keys:
141 low_pri = 9
142 for [name, pri] in self.enum_table[ enum ]:
143 name_table.append( [name, enum] )
144
145 if pri < low_pri:
146 low_pri = pri
147 enum_table[enum] = name
148
149
150 name_table.sort()
151
152 string_offsets = {}
153 i = 0;
Brian Paul7438a782006-11-16 16:12:10 +0000154 print 'LONGSTRING static const char enum_string_table[] = '
Ian Romanickf3a6e4f2005-02-26 01:09:35 +0000155 for [name, enum] in name_table:
156 print ' "%s\\0"' % (name)
157 string_offsets[ name ] = i
158 i += len(name) + 1
159
160 print ' ;'
161 print ''
162
163
164 print 'static const enum_elt all_enums[%u] =' % (len(name_table))
165 print '{'
166 for [name, enum] in name_table:
Ian Romanick3bca4f62006-03-06 18:31:50 +0000167 print ' { %5u, 0x%08X }, /* %s */' % (string_offsets[name], enum, name)
Ian Romanickf3a6e4f2005-02-26 01:09:35 +0000168 print '};'
169 print ''
170
171 print 'static const unsigned reduced_enums[%u] =' % (len(keys))
172 print '{'
173 for enum in keys:
174 name = enum_table[ enum ]
175 if [name, enum] not in name_table:
176 print ' /* Error! %s, 0x%04x */ 0,' % (name, enum)
177 else:
178 i = name_table.index( [name, enum] )
179
Ian Romanick3bca4f62006-03-06 18:31:50 +0000180 print ' %4u, /* %s */' % (i, name)
Ian Romanickf3a6e4f2005-02-26 01:09:35 +0000181 print '};'
182
183
Ian Romanick66a55482005-06-21 23:42:43 +0000184 self.print_code()
Brian Paul78123bb2005-02-22 15:39:46 +0000185 return
186
187
Ian Romanick66a55482005-06-21 23:42:43 +0000188 def process_enums(self, api):
189 self.enum_table = {}
190
191 for obj in api.enumIterateByName():
Ian Romanickf3a6e4f2005-02-26 01:09:35 +0000192 if obj.value not in self.enum_table:
193 self.enum_table[ obj.value ] = []
194
195
Ian Romanick66a55482005-06-21 23:42:43 +0000196 name = "GL_" + obj.name
197 priority = obj.priority()
198 self.enum_table[ obj.value ].append( [name, priority] )
Ian Romanickf3a6e4f2005-02-26 01:09:35 +0000199
200
Brian Paul78123bb2005-02-22 15:39:46 +0000201def show_usage():
202 print "Usage: %s [-f input_file_name]" % sys.argv[0]
203 sys.exit(1)
204
205if __name__ == '__main__':
206 file_name = "gl_API.xml"
Ian Romanickf3a6e4f2005-02-26 01:09:35 +0000207
Brian Paul78123bb2005-02-22 15:39:46 +0000208 try:
209 (args, trail) = getopt.getopt(sys.argv[1:], "f:")
210 except Exception,e:
211 show_usage()
212
213 for (arg,val) in args:
214 if arg == "-f":
215 file_name = val
216
Ian Romanick66a55482005-06-21 23:42:43 +0000217 api = gl_XML.parse_GL_API( file_name )
218
219 printer = PrintGlEnums()
220 printer.Print( api )