mesa: Rename _mesa_lookup_enum_by_nr() to _mesa_enum_to_string().
[mesa.git] / src / mapi / glapi / gen / gl_enums.py
1 #!/usr/bin/python2
2 # -*- Mode: Python; py-indent-offset: 8 -*-
3
4 # (C) Copyright Zack Rusin 2005. All Rights Reserved.
5 # Copyright (C) 2015 Intel Corporation
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 argparse
30
31 import license
32 import gl_XML
33 import sys, getopt
34
35 class PrintGlEnums(gl_XML.gl_print_base):
36
37 def __init__(self):
38 gl_XML.gl_print_base.__init__(self)
39
40 self.name = "gl_enums.py (from Mesa)"
41 self.license = license.bsd_license_template % ( \
42 """Copyright (C) 1999-2005 Brian Paul All Rights Reserved.""", "BRIAN PAUL")
43 self.enum_table = {}
44
45
46 def printRealHeader(self):
47 print '#include "main/glheader.h"'
48 print '#include "main/enums.h"'
49 print '#include "main/imports.h"'
50 print '#include "main/mtypes.h"'
51 print ''
52 print 'typedef struct PACKED {'
53 print ' uint16_t offset;'
54 print ' int n;'
55 print '} enum_elt;'
56 print ''
57 return
58
59 def print_code(self):
60 print """
61 typedef int (*cfunc)(const void *, const void *);
62
63 /**
64 * Compare a key enum value to an element in the \c enum_string_table_offsets array.
65 *
66 * \c bsearch always passes the key as the first parameter and the pointer
67 * to the array element as the second parameter. We can elimiate some
68 * extra work by taking advantage of that fact.
69 *
70 * \param a Pointer to the desired enum name.
71 * \param b Pointer into the \c enum_string_table_offsets array.
72 */
73 static int compar_nr( const int *a, enum_elt *b )
74 {
75 return a[0] - b->n;
76 }
77
78
79 static char token_tmp[20];
80
81 const char *_mesa_enum_to_string( int nr )
82 {
83 enum_elt *elt;
84
85 STATIC_ASSERT(sizeof(enum_string_table) < (1 << 16));
86
87 elt = bsearch(& nr, enum_string_table_offsets,
88 ARRAY_SIZE(enum_string_table_offsets),
89 sizeof(enum_string_table_offsets[0]),
90 (cfunc) compar_nr);
91
92 if (elt != NULL) {
93 return &enum_string_table[elt->offset];
94 }
95 else {
96 /* this is not re-entrant safe, no big deal here */
97 _mesa_snprintf(token_tmp, sizeof(token_tmp) - 1, "0x%x", nr);
98 token_tmp[sizeof(token_tmp) - 1] = '\\0';
99 return token_tmp;
100 }
101 }
102
103 /**
104 * Primitive names
105 */
106 static const char *prim_names[PRIM_MAX+3] = {
107 "GL_POINTS",
108 "GL_LINES",
109 "GL_LINE_LOOP",
110 "GL_LINE_STRIP",
111 "GL_TRIANGLES",
112 "GL_TRIANGLE_STRIP",
113 "GL_TRIANGLE_FAN",
114 "GL_QUADS",
115 "GL_QUAD_STRIP",
116 "GL_POLYGON",
117 "GL_LINES_ADJACENCY",
118 "GL_LINE_STRIP_ADJACENCY",
119 "GL_TRIANGLES_ADJACENCY",
120 "GL_TRIANGLE_STRIP_ADJACENCY",
121 "outside begin/end",
122 "unknown state"
123 };
124
125
126 /* Get the name of an enum given that it is a primitive type. Avoids
127 * GL_FALSE/GL_POINTS ambiguity and others.
128 */
129 const char *
130 _mesa_lookup_prim_by_nr(GLuint nr)
131 {
132 if (nr < ARRAY_SIZE(prim_names))
133 return prim_names[nr];
134 else
135 return "invalid mode";
136 }
137
138
139 """
140 return
141
142
143 def printBody(self, api_list):
144 self.enum_table = {}
145 for api in api_list:
146 self.process_enums( api )
147
148 enum_table = []
149
150 for enum in sorted(self.enum_table.keys()):
151 low_pri = 9
152 best_name = ''
153 for [name, pri] in self.enum_table[ enum ]:
154 if pri < low_pri:
155 low_pri = pri
156 best_name = name
157
158 enum_table.append((enum, best_name))
159
160 string_offsets = {}
161 i = 0;
162 print '#if defined(__GNUC__)'
163 print '# define LONGSTRING __extension__'
164 print '#else'
165 print '# define LONGSTRING'
166 print '#endif'
167 print ''
168 print 'LONGSTRING static const char enum_string_table[] = '
169 for enum, name in enum_table:
170 print ' "%s\\0"' % (name)
171 string_offsets[ enum ] = i
172 i += len(name) + 1
173
174 print ' ;'
175 print ''
176
177
178 print 'static const enum_elt enum_string_table_offsets[%u] =' % (len(enum_table))
179 print '{'
180 for enum, name in enum_table:
181 print ' { %5u, 0x%08X }, /* %s */' % (string_offsets[enum], enum, name)
182 print '};'
183 print ''
184
185 self.print_code()
186 return
187
188
189 def process_enums(self, api):
190 for obj in api.enumIterateByName():
191 if obj.value not in self.enum_table:
192 self.enum_table[ obj.value ] = []
193
194
195 enum = self.enum_table[ obj.value ]
196 name = "GL_" + obj.name
197 priority = obj.priority()
198 already_in = False;
199 for n, p in enum:
200 if n == name:
201 already_in = True
202 if not already_in:
203 enum.append( [name, priority] )
204
205
206 def _parser():
207 parser = argparse.ArgumentParser()
208 parser.add_argument('-f', '--input_file',
209 required=True,
210 help="Choose an xml file to parse.")
211 return parser.parse_args()
212
213
214 def main():
215 args = _parser()
216 api_list = [gl_XML.parse_GL_API(args.input_file)]
217
218 printer = PrintGlEnums()
219 printer.Print(api_list)
220
221
222 if __name__ == '__main__':
223 main()