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 | |
| 33 | class PrintGlEnums(gl_XML.FilterGLAPISpecBase): |
| 34 | name = "gl_enums.py (from Mesa)" |
| 35 | |
| 36 | def __init__(self): |
| 37 | gl_XML.FilterGLAPISpecBase.__init__(self) |
| 38 | self.license = license.bsd_license_template % ( \ |
| 39 | """Copyright (C) 1999-2005 Brian Paul All Rights Reserved.""", "BRIAN PAUL") |
Ian Romanick | f3a6e4f | 2005-02-26 01:09:35 +0000 | [diff] [blame] | 40 | self.enum_table = {} |
Brian Paul | 78123bb | 2005-02-22 15:39:46 +0000 | [diff] [blame] | 41 | |
| 42 | |
| 43 | def printRealHeader(self): |
| 44 | print '#include "glheader.h"' |
| 45 | print '#include "enums.h"' |
| 46 | print '#include "imports.h"' |
| 47 | print '' |
| 48 | print 'typedef struct {' |
Ian Romanick | f3a6e4f | 2005-02-26 01:09:35 +0000 | [diff] [blame] | 49 | print ' size_t offset;' |
Brian Paul | 78123bb | 2005-02-22 15:39:46 +0000 | [diff] [blame] | 50 | print ' int n;' |
| 51 | print '} enum_elt;' |
| 52 | print '' |
Brian Paul | 78123bb | 2005-02-22 15:39:46 +0000 | [diff] [blame] | 53 | return |
| 54 | |
Brian Paul | 8f5f6b3 | 2005-02-23 16:36:17 +0000 | [diff] [blame] | 55 | def printBody(self): |
Brian Paul | 78123bb | 2005-02-22 15:39:46 +0000 | [diff] [blame] | 56 | print """ |
Brian Paul | 78123bb | 2005-02-22 15:39:46 +0000 | [diff] [blame] | 57 | #define Elements(x) sizeof(x)/sizeof(*x) |
| 58 | |
| 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 | |
Ian Romanick | f3a6e4f | 2005-02-26 01:09:35 +0000 | [diff] [blame] | 98 | i = (unsigned *)bsearch( & nr, reduced_enums, Elements(reduced_enums), |
| 99 | sizeof(reduced_enums[0]), (cfunc) compar_nr ); |
Brian Paul | 78123bb | 2005-02-22 15:39:46 +0000 | [diff] [blame] | 100 | |
Ian Romanick | f3a6e4f | 2005-02-26 01:09:35 +0000 | [diff] [blame] | 101 | if ( i != NULL ) { |
| 102 | return & enum_string_table[ all_enums[ *i ].offset ]; |
Brian Paul | 78123bb | 2005-02-22 15:39:46 +0000 | [diff] [blame] | 103 | } |
| 104 | else { |
Ian Romanick | f3a6e4f | 2005-02-26 01:09:35 +0000 | [diff] [blame] | 105 | /* this is not re-entrant safe, no big deal here */ |
Brian Paul | 78123bb | 2005-02-22 15:39:46 +0000 | [diff] [blame] | 106 | _mesa_sprintf(token_tmp, "0x%x", nr); |
| 107 | return token_tmp; |
| 108 | } |
| 109 | } |
Brian Paul | 8f5f6b3 | 2005-02-23 16:36:17 +0000 | [diff] [blame] | 110 | |
| 111 | int _mesa_lookup_enum_by_name( const char *symbol ) |
| 112 | { |
Ian Romanick | f3a6e4f | 2005-02-26 01:09:35 +0000 | [diff] [blame] | 113 | enum_elt * f = NULL; |
Brian Paul | 8f5f6b3 | 2005-02-23 16:36:17 +0000 | [diff] [blame] | 114 | |
Ian Romanick | f3a6e4f | 2005-02-26 01:09:35 +0000 | [diff] [blame] | 115 | if ( symbol != NULL ) { |
| 116 | f = (enum_elt *)bsearch( symbol, all_enums, Elements(all_enums), |
| 117 | sizeof( enum_elt ), (cfunc) compar_name ); |
| 118 | } |
Brian Paul | 8f5f6b3 | 2005-02-23 16:36:17 +0000 | [diff] [blame] | 119 | |
Ian Romanick | f3a6e4f | 2005-02-26 01:09:35 +0000 | [diff] [blame] | 120 | return (f != NULL) ? f->n : -1; |
Brian Paul | 8f5f6b3 | 2005-02-23 16:36:17 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Brian Paul | 78123bb | 2005-02-22 15:39:46 +0000 | [diff] [blame] | 123 | """ |
| 124 | return |
| 125 | |
| 126 | def printFunctions(self): |
Ian Romanick | f3a6e4f | 2005-02-26 01:09:35 +0000 | [diff] [blame] | 127 | keys = self.enum_table.keys() |
| 128 | keys.sort() |
| 129 | |
| 130 | name_table = [] |
| 131 | enum_table = {} |
| 132 | |
| 133 | for enum in keys: |
| 134 | low_pri = 9 |
| 135 | for [name, pri] in self.enum_table[ enum ]: |
| 136 | name_table.append( [name, enum] ) |
| 137 | |
| 138 | if pri < low_pri: |
| 139 | low_pri = pri |
| 140 | enum_table[enum] = name |
| 141 | |
| 142 | |
| 143 | name_table.sort() |
| 144 | |
| 145 | string_offsets = {} |
| 146 | i = 0; |
| 147 | print 'static const char enum_string_table[] = ' |
| 148 | for [name, enum] in name_table: |
| 149 | print ' "%s\\0"' % (name) |
| 150 | string_offsets[ name ] = i |
| 151 | i += len(name) + 1 |
| 152 | |
| 153 | print ' ;' |
| 154 | print '' |
| 155 | |
| 156 | |
| 157 | print 'static const enum_elt all_enums[%u] =' % (len(name_table)) |
| 158 | print '{' |
| 159 | for [name, enum] in name_table: |
| 160 | print ' { % 5u, 0x%08X }, /* %s */' % (string_offsets[name], enum, name) |
| 161 | print '};' |
| 162 | print '' |
| 163 | |
| 164 | print 'static const unsigned reduced_enums[%u] =' % (len(keys)) |
| 165 | print '{' |
| 166 | for enum in keys: |
| 167 | name = enum_table[ enum ] |
| 168 | if [name, enum] not in name_table: |
| 169 | print ' /* Error! %s, 0x%04x */ 0,' % (name, enum) |
| 170 | else: |
| 171 | i = name_table.index( [name, enum] ) |
| 172 | |
| 173 | print ' % 4u, /* %s */' % (i, name) |
| 174 | print '};' |
| 175 | |
| 176 | |
Brian Paul | 8f5f6b3 | 2005-02-23 16:36:17 +0000 | [diff] [blame] | 177 | self.printBody(); |
Brian Paul | 78123bb | 2005-02-22 15:39:46 +0000 | [diff] [blame] | 178 | return |
| 179 | |
| 180 | |
Ian Romanick | f3a6e4f | 2005-02-26 01:09:35 +0000 | [diff] [blame] | 181 | def append(self, object_type, obj): |
| 182 | if object_type == "enum": |
| 183 | if obj.value not in self.enum_table: |
| 184 | self.enum_table[ obj.value ] = [] |
| 185 | |
| 186 | |
| 187 | # Prevent duplicate names in the enum table. |
| 188 | |
| 189 | if obj.name not in self.enum_table[ obj.value ]: |
| 190 | |
| 191 | # Calculate a "priority" for this enum name. |
| 192 | # When we lookup an enum by number, there may |
| 193 | # be many possible names, but only one can be |
| 194 | # returned. The priority is used by this |
| 195 | # script to select which name is the "best". |
| 196 | # |
| 197 | # Highest precedence is given to "core" name. |
| 198 | # ARB extension names have the next highest, |
| 199 | # followed by EXT extension names. Vendor |
| 200 | # extension names are the lowest. |
| 201 | |
| 202 | if obj.category.startswith( "GL_VERSION_" ): |
| 203 | priority = 0 |
| 204 | elif obj.category.startswith( "GL_ARB_" ): |
| 205 | priority = 1 |
| 206 | elif obj.category.startswith( "GL_EXT_" ): |
| 207 | priority = 2 |
| 208 | else: |
| 209 | priority = 3 |
| 210 | |
| 211 | self.enum_table[ obj.value ].append( [obj.name, priority] ) |
| 212 | |
| 213 | else: |
| 214 | gl_XML.FilterGLAPISpecBase.append(self, object_type, obj) |
| 215 | |
| 216 | |
Brian Paul | 78123bb | 2005-02-22 15:39:46 +0000 | [diff] [blame] | 217 | def show_usage(): |
| 218 | print "Usage: %s [-f input_file_name]" % sys.argv[0] |
| 219 | sys.exit(1) |
| 220 | |
| 221 | if __name__ == '__main__': |
| 222 | file_name = "gl_API.xml" |
Ian Romanick | f3a6e4f | 2005-02-26 01:09:35 +0000 | [diff] [blame] | 223 | |
Brian Paul | 78123bb | 2005-02-22 15:39:46 +0000 | [diff] [blame] | 224 | try: |
| 225 | (args, trail) = getopt.getopt(sys.argv[1:], "f:") |
| 226 | except Exception,e: |
| 227 | show_usage() |
| 228 | |
| 229 | for (arg,val) in args: |
| 230 | if arg == "-f": |
| 231 | file_name = val |
| 232 | |
| 233 | dh = PrintGlEnums() |
Ian Romanick | 93d2d54 | 2005-04-18 19:42:23 +0000 | [diff] [blame^] | 234 | gl_XML.parse_GL_API( dh, file_name ) |