Brian Paul | 78123bb | 2005-02-22 15:39:46 +0000 | [diff] [blame] | 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 | |
Brian Paul | 78123bb | 2005-02-22 15:39:46 +0000 | [diff] [blame] | 29 | import license |
| 30 | import gl_XML |
| 31 | import sys, getopt |
| 32 | |
Ian Romanick | 66a5548 | 2005-06-21 23:42:43 +0000 | [diff] [blame] | 33 | class PrintGlEnums(gl_XML.gl_print_base): |
Brian Paul | 78123bb | 2005-02-22 15:39:46 +0000 | [diff] [blame] | 34 | |
| 35 | def __init__(self): |
Ian Romanick | 66a5548 | 2005-06-21 23:42:43 +0000 | [diff] [blame] | 36 | gl_XML.gl_print_base.__init__(self) |
Ian Romanick | 70dbbbf | 2005-05-26 16:59:47 +0000 | [diff] [blame] | 37 | |
| 38 | self.name = "gl_enums.py (from Mesa)" |
Brian Paul | 78123bb | 2005-02-22 15:39:46 +0000 | [diff] [blame] | 39 | self.license = license.bsd_license_template % ( \ |
| 40 | """Copyright (C) 1999-2005 Brian Paul All Rights Reserved.""", "BRIAN PAUL") |
Ian Romanick | f3a6e4f | 2005-02-26 01:09:35 +0000 | [diff] [blame] | 41 | self.enum_table = {} |
Brian Paul | 78123bb | 2005-02-22 15:39:46 +0000 | [diff] [blame] | 42 | |
| 43 | |
| 44 | def printRealHeader(self): |
Chia-I Wu | 5b42628 | 2009-09-03 11:03:20 +0800 | [diff] [blame] | 45 | print '#include "main/glheader.h"' |
| 46 | print '#include "main/mfeatures.h"' |
| 47 | print '#include "main/enums.h"' |
| 48 | print '#include "main/imports.h"' |
Brian Paul | 78123bb | 2005-02-22 15:39:46 +0000 | [diff] [blame] | 49 | print '' |
| 50 | print 'typedef struct {' |
Ian Romanick | f3a6e4f | 2005-02-26 01:09:35 +0000 | [diff] [blame] | 51 | print ' size_t offset;' |
Brian Paul | 78123bb | 2005-02-22 15:39:46 +0000 | [diff] [blame] | 52 | print ' int n;' |
| 53 | print '} enum_elt;' |
| 54 | print '' |
Brian Paul | 78123bb | 2005-02-22 15:39:46 +0000 | [diff] [blame] | 55 | return |
| 56 | |
Ian Romanick | 66a5548 | 2005-06-21 23:42:43 +0000 | [diff] [blame] | 57 | def print_code(self): |
Brian Paul | 78123bb | 2005-02-22 15:39:46 +0000 | [diff] [blame] | 58 | print """ |
Brian Paul | 78123bb | 2005-02-22 15:39:46 +0000 | [diff] [blame] | 59 | typedef int (*cfunc)(const void *, const void *); |
| 60 | |
Ian Romanick | f3a6e4f | 2005-02-26 01:09:35 +0000 | [diff] [blame] | 61 | /** |
| 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 Paul | 78123bb | 2005-02-22 15:39:46 +0000 | [diff] [blame] | 70 | */ |
Ian Romanick | f3a6e4f | 2005-02-26 01:09:35 +0000 | [diff] [blame] | 71 | static int compar_name( const char *a, const enum_elt *b ) |
Brian Paul | 78123bb | 2005-02-22 15:39:46 +0000 | [diff] [blame] | 72 | { |
Ian Romanick | f3a6e4f | 2005-02-26 01:09:35 +0000 | [diff] [blame] | 73 | return _mesa_strcmp( a, & enum_string_table[ b->offset ] ); |
Brian Paul | 78123bb | 2005-02-22 15:39:46 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Ian Romanick | f3a6e4f | 2005-02-26 01:09:35 +0000 | [diff] [blame] | 76 | /** |
| 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 | */ |
| 86 | static int compar_nr( const int *a, const unsigned *b ) |
Brian Paul | 78123bb | 2005-02-22 15:39:46 +0000 | [diff] [blame] | 87 | { |
Ian Romanick | f3a6e4f | 2005-02-26 01:09:35 +0000 | [diff] [blame] | 88 | return a[0] - all_enums[*b].n; |
Brian Paul | 78123bb | 2005-02-22 15:39:46 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | |
| 92 | static char token_tmp[20]; |
| 93 | |
| 94 | const char *_mesa_lookup_enum_by_nr( int nr ) |
| 95 | { |
Ian Romanick | f3a6e4f | 2005-02-26 01:09:35 +0000 | [diff] [blame] | 96 | unsigned * i; |
Brian Paul | 78123bb | 2005-02-22 15:39:46 +0000 | [diff] [blame] | 97 | |
Brian Paul | 59cc973 | 2008-11-10 14:42:02 -0700 | [diff] [blame] | 98 | i = (unsigned *) _mesa_bsearch(& nr, reduced_enums, |
| 99 | Elements(reduced_enums), |
| 100 | sizeof(reduced_enums[0]), |
| 101 | (cfunc) compar_nr); |
Brian Paul | 78123bb | 2005-02-22 15:39:46 +0000 | [diff] [blame] | 102 | |
Ian Romanick | f3a6e4f | 2005-02-26 01:09:35 +0000 | [diff] [blame] | 103 | if ( i != NULL ) { |
| 104 | return & enum_string_table[ all_enums[ *i ].offset ]; |
Brian Paul | 78123bb | 2005-02-22 15:39:46 +0000 | [diff] [blame] | 105 | } |
| 106 | else { |
Ian Romanick | f3a6e4f | 2005-02-26 01:09:35 +0000 | [diff] [blame] | 107 | /* this is not re-entrant safe, no big deal here */ |
Brian Paul | 78123bb | 2005-02-22 15:39:46 +0000 | [diff] [blame] | 108 | _mesa_sprintf(token_tmp, "0x%x", nr); |
| 109 | return token_tmp; |
| 110 | } |
| 111 | } |
Brian Paul | 8f5f6b3 | 2005-02-23 16:36:17 +0000 | [diff] [blame] | 112 | |
Keith Whitwell | 798cd2a | 2009-07-02 13:28:20 +0100 | [diff] [blame] | 113 | /* Get the name of an enum given that it is a primitive type. Avoids |
| 114 | * GL_FALSE/GL_POINTS ambiguity and others. |
| 115 | */ |
| 116 | const char *_mesa_lookup_prim_by_nr( int nr ) |
| 117 | { |
| 118 | switch (nr) { |
| 119 | case GL_POINTS: return "GL_POINTS"; |
| 120 | case GL_LINES: return "GL_LINES"; |
| 121 | case GL_LINE_STRIP: return "GL_LINE_STRIP"; |
| 122 | case GL_LINE_LOOP: return "GL_LINE_LOOP"; |
| 123 | case GL_TRIANGLES: return "GL_TRIANGLES"; |
| 124 | case GL_TRIANGLE_STRIP: return "GL_TRIANGLE_STRIP"; |
| 125 | case GL_TRIANGLE_FAN: return "GL_TRIANGLE_FAN"; |
| 126 | case GL_QUADS: return "GL_QUADS"; |
| 127 | case GL_QUAD_STRIP: return "GL_QUAD_STRIP"; |
| 128 | case GL_POLYGON: return "GL_POLYGON"; |
| 129 | case GL_POLYGON+1: return "OUTSIDE_BEGIN_END"; |
| 130 | default: return "<invalid>"; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | |
| 135 | |
Brian Paul | 8f5f6b3 | 2005-02-23 16:36:17 +0000 | [diff] [blame] | 136 | int _mesa_lookup_enum_by_name( const char *symbol ) |
| 137 | { |
Ian Romanick | f3a6e4f | 2005-02-26 01:09:35 +0000 | [diff] [blame] | 138 | enum_elt * f = NULL; |
Brian Paul | 8f5f6b3 | 2005-02-23 16:36:17 +0000 | [diff] [blame] | 139 | |
Ian Romanick | f3a6e4f | 2005-02-26 01:09:35 +0000 | [diff] [blame] | 140 | if ( symbol != NULL ) { |
Brian Paul | 59cc973 | 2008-11-10 14:42:02 -0700 | [diff] [blame] | 141 | f = (enum_elt *) _mesa_bsearch(symbol, all_enums, |
| 142 | Elements(all_enums), |
| 143 | sizeof( enum_elt ), |
| 144 | (cfunc) compar_name); |
Ian Romanick | f3a6e4f | 2005-02-26 01:09:35 +0000 | [diff] [blame] | 145 | } |
Brian Paul | 8f5f6b3 | 2005-02-23 16:36:17 +0000 | [diff] [blame] | 146 | |
Ian Romanick | f3a6e4f | 2005-02-26 01:09:35 +0000 | [diff] [blame] | 147 | return (f != NULL) ? f->n : -1; |
Brian Paul | 8f5f6b3 | 2005-02-23 16:36:17 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Brian Paul | 78123bb | 2005-02-22 15:39:46 +0000 | [diff] [blame] | 150 | """ |
| 151 | return |
| 152 | |
Ian Romanick | 66a5548 | 2005-06-21 23:42:43 +0000 | [diff] [blame] | 153 | |
| 154 | def printBody(self, api): |
| 155 | self.process_enums( api ) |
| 156 | |
Ian Romanick | f3a6e4f | 2005-02-26 01:09:35 +0000 | [diff] [blame] | 157 | keys = self.enum_table.keys() |
| 158 | keys.sort() |
| 159 | |
| 160 | name_table = [] |
| 161 | enum_table = {} |
| 162 | |
| 163 | for enum in keys: |
| 164 | low_pri = 9 |
| 165 | for [name, pri] in self.enum_table[ enum ]: |
| 166 | name_table.append( [name, enum] ) |
| 167 | |
| 168 | if pri < low_pri: |
| 169 | low_pri = pri |
| 170 | enum_table[enum] = name |
| 171 | |
| 172 | |
| 173 | name_table.sort() |
| 174 | |
| 175 | string_offsets = {} |
| 176 | i = 0; |
Brian Paul | 7438a78 | 2006-11-16 16:12:10 +0000 | [diff] [blame] | 177 | print 'LONGSTRING static const char enum_string_table[] = ' |
Ian Romanick | f3a6e4f | 2005-02-26 01:09:35 +0000 | [diff] [blame] | 178 | for [name, enum] in name_table: |
| 179 | print ' "%s\\0"' % (name) |
| 180 | string_offsets[ name ] = i |
| 181 | i += len(name) + 1 |
| 182 | |
| 183 | print ' ;' |
| 184 | print '' |
| 185 | |
| 186 | |
| 187 | print 'static const enum_elt all_enums[%u] =' % (len(name_table)) |
| 188 | print '{' |
| 189 | for [name, enum] in name_table: |
Ian Romanick | 3bca4f6 | 2006-03-06 18:31:50 +0000 | [diff] [blame] | 190 | print ' { %5u, 0x%08X }, /* %s */' % (string_offsets[name], enum, name) |
Ian Romanick | f3a6e4f | 2005-02-26 01:09:35 +0000 | [diff] [blame] | 191 | print '};' |
| 192 | print '' |
| 193 | |
| 194 | print 'static const unsigned reduced_enums[%u] =' % (len(keys)) |
| 195 | print '{' |
| 196 | for enum in keys: |
| 197 | name = enum_table[ enum ] |
| 198 | if [name, enum] not in name_table: |
| 199 | print ' /* Error! %s, 0x%04x */ 0,' % (name, enum) |
| 200 | else: |
| 201 | i = name_table.index( [name, enum] ) |
| 202 | |
Ian Romanick | 3bca4f6 | 2006-03-06 18:31:50 +0000 | [diff] [blame] | 203 | print ' %4u, /* %s */' % (i, name) |
Ian Romanick | f3a6e4f | 2005-02-26 01:09:35 +0000 | [diff] [blame] | 204 | print '};' |
| 205 | |
| 206 | |
Ian Romanick | 66a5548 | 2005-06-21 23:42:43 +0000 | [diff] [blame] | 207 | self.print_code() |
Brian Paul | 78123bb | 2005-02-22 15:39:46 +0000 | [diff] [blame] | 208 | return |
| 209 | |
| 210 | |
Ian Romanick | 66a5548 | 2005-06-21 23:42:43 +0000 | [diff] [blame] | 211 | def process_enums(self, api): |
| 212 | self.enum_table = {} |
| 213 | |
| 214 | for obj in api.enumIterateByName(): |
Ian Romanick | f3a6e4f | 2005-02-26 01:09:35 +0000 | [diff] [blame] | 215 | if obj.value not in self.enum_table: |
| 216 | self.enum_table[ obj.value ] = [] |
| 217 | |
| 218 | |
Ian Romanick | 66a5548 | 2005-06-21 23:42:43 +0000 | [diff] [blame] | 219 | name = "GL_" + obj.name |
| 220 | priority = obj.priority() |
| 221 | self.enum_table[ obj.value ].append( [name, priority] ) |
Ian Romanick | f3a6e4f | 2005-02-26 01:09:35 +0000 | [diff] [blame] | 222 | |
| 223 | |
Brian Paul | 78123bb | 2005-02-22 15:39:46 +0000 | [diff] [blame] | 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" |
Ian Romanick | f3a6e4f | 2005-02-26 01:09:35 +0000 | [diff] [blame] | 230 | |
Brian Paul | 78123bb | 2005-02-22 15:39:46 +0000 | [diff] [blame] | 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 | |
Ian Romanick | 66a5548 | 2005-06-21 23:42:43 +0000 | [diff] [blame] | 240 | api = gl_XML.parse_GL_API( file_name ) |
| 241 | |
| 242 | printer = PrintGlEnums() |
| 243 | printer.Print( api ) |