Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 1 | #!/usr/bin/python2 |
| 2 | |
| 3 | # (C) Copyright IBM Corporation 2004 |
| 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | # copy of this software and associated documentation files (the "Software"), |
| 8 | # to deal in the Software without restriction, including without limitation |
| 9 | # on the rights to use, copy, modify, merge, publish, distribute, sub |
| 10 | # license, and/or sell copies of the Software, and to permit persons to whom |
| 11 | # the Software is furnished to do so, subject to the following conditions: |
| 12 | # |
| 13 | # The above copyright notice and this permission notice (including the next |
| 14 | # paragraph) shall be included in all copies or substantial portions of the |
| 15 | # Software. |
| 16 | # |
| 17 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL |
| 20 | # IBM AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 | # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 23 | # IN THE SOFTWARE. |
| 24 | # |
| 25 | # Authors: |
| 26 | # Ian Romanick <idr@us.ibm.com> |
| 27 | |
| 28 | from xml.sax import saxutils |
| 29 | from xml.sax import make_parser |
| 30 | from xml.sax.handler import feature_namespaces |
| 31 | |
| 32 | import gl_XML |
| 33 | import license |
| 34 | import sys, getopt |
| 35 | |
| 36 | class PrintGlOffsets(gl_XML.FilterGLAPISpecBase): |
| 37 | name = "gl_apitemp.py (from Mesa)" |
| 38 | |
| 39 | def __init__(self): |
| 40 | gl_XML.FilterGLAPISpecBase.__init__(self) |
| 41 | self.license = license.bsd_license_template % ( \ |
| 42 | """Copyright (C) 1999-2001 Brian Paul All Rights Reserved. |
| 43 | (C) Copyright IBM Corporation 2004""", "BRIAN PAUL, IBM") |
| 44 | |
| 45 | def printFunction(self, f): |
| 46 | p_string = "" |
| 47 | o_string = "" |
| 48 | t_string = "" |
| 49 | comma = "" |
| 50 | |
| 51 | for p in f: |
| 52 | cast = "" |
| 53 | |
| 54 | if p.is_pointer: |
| 55 | t = "%p" |
| 56 | cast = "(const void *) " |
| 57 | elif p.p_type_string == 'GLenum': |
| 58 | t = "0x%x" |
| 59 | elif p.p_type_string in ['GLfloat', 'GLdouble', 'GLclampf', 'GLclampd']: |
| 60 | t = "%f" |
| 61 | else: |
| 62 | t = "%d" |
| 63 | |
| 64 | t_string = t_string + comma + t |
Ian Romanick | a9d033c | 2004-05-19 23:33:08 +0000 | [diff] [blame] | 65 | p_string = p_string + comma + p.name |
| 66 | o_string = o_string + comma + cast + p.name |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 67 | comma = ", " |
| 68 | |
| 69 | |
| 70 | if f.fn_return_type != 'void': |
| 71 | dispatch = "RETURN_DISPATCH" |
| 72 | else: |
| 73 | dispatch = "DISPATCH" |
| 74 | |
| 75 | print 'KEYWORD1 %s KEYWORD2 NAME(%s)(%s)' \ |
| 76 | % (f.fn_return_type, f.name, f.get_parameter_string()) |
| 77 | print '{' |
| 78 | if p_string == "": |
| 79 | print ' %s(%s, (), (F, "gl%s();\\n"));' \ |
| 80 | % (dispatch, f.real_name, f.name) |
| 81 | else: |
| 82 | print ' %s(%s, (%s), (F, "gl%s(%s);\\n", %s));' \ |
| 83 | % (dispatch, f.real_name, p_string, f.name, t_string, o_string) |
| 84 | print '}' |
| 85 | print '' |
| 86 | return |
| 87 | |
| 88 | def printRealHeader(self): |
| 89 | print """ |
| 90 | /* |
| 91 | * This file is a template which generates the OpenGL API entry point |
| 92 | * functions. It should be included by a .c file which first defines |
| 93 | * the following macros: |
| 94 | * KEYWORD1 - usually nothing, but might be __declspec(dllexport) on Win32 |
| 95 | * KEYWORD2 - usually nothing, but might be __stdcall on Win32 |
| 96 | * NAME(n) - builds the final function name (usually add "gl" prefix) |
| 97 | * DISPATCH(func, args, msg) - code to do dispatch of named function. |
| 98 | * msg is a printf-style debug message. |
| 99 | * RETURN_DISPATCH(func, args, msg) - code to do dispatch with a return value |
| 100 | * |
| 101 | * Here is an example which generates the usual OpenGL functions: |
| 102 | * #define KEYWORD1 |
| 103 | * #define KEYWORD2 |
| 104 | * #define NAME(func) gl##func |
| 105 | * #define DISPATCH(func, args, msg) \\ |
| 106 | * struct _glapi_table *dispatch = CurrentDispatch; \\ |
| 107 | * (*dispatch->func) args |
| 108 | * #define RETURN DISPATCH(func, args, msg) \\ |
| 109 | * struct _glapi_table *dispatch = CurrentDispatch; \\ |
| 110 | * return (*dispatch->func) args |
| 111 | * |
| 112 | */ |
| 113 | |
| 114 | |
Ian Romanick | 8e77da1c | 2004-06-29 19:08:20 +0000 | [diff] [blame] | 115 | #if defined( NAME ) |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 116 | #ifndef KEYWORD1 |
| 117 | #define KEYWORD1 |
| 118 | #endif |
| 119 | |
| 120 | #ifndef KEYWORD2 |
| 121 | #define KEYWORD2 |
| 122 | #endif |
| 123 | |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 124 | #ifndef DISPATCH |
| 125 | #error DISPATCH must be defined |
| 126 | #endif |
| 127 | |
| 128 | #ifndef RETURN_DISPATCH |
| 129 | #error RETURN_DISPATCH must be defined |
| 130 | #endif |
| 131 | |
| 132 | GLAPI void GLAPIENTRY gl__unused413(void); /* silence warning */ |
| 133 | """ |
| 134 | return |
| 135 | |
| 136 | |
| 137 | |
| 138 | def printInitDispatch(self): |
| 139 | print """ |
Ian Romanick | 8e77da1c | 2004-06-29 19:08:20 +0000 | [diff] [blame] | 140 | #endif /* defined( NAME ) */ |
Ian Romanick | 73f59b0 | 2004-05-18 18:33:40 +0000 | [diff] [blame] | 141 | |
| 142 | /* |
| 143 | * This is how a dispatch table can be initialized with all the functions |
| 144 | * we generated above. |
| 145 | */ |
| 146 | #ifdef DISPATCH_TABLE_NAME |
| 147 | |
| 148 | #ifndef TABLE_ENTRY |
| 149 | #error TABLE_ENTRY must be defined |
| 150 | #endif |
| 151 | |
| 152 | static void * DISPATCH_TABLE_NAME[] = {""" |
| 153 | keys = self.functions.keys() |
| 154 | keys.sort() |
| 155 | for k in keys: |
| 156 | if k < 0: continue |
| 157 | |
| 158 | print ' TABLE_ENTRY(%s),' % (self.functions[k].name) |
| 159 | |
| 160 | print ' /* A whole bunch of no-op functions. These might be called' |
| 161 | print ' * when someone tries to call a dynamically-registered' |
| 162 | print ' * extension function without a current rendering context.' |
| 163 | print ' */' |
| 164 | for i in range(1, 100): |
| 165 | print ' TABLE_ENTRY(Unused),' |
| 166 | |
| 167 | print '};' |
| 168 | print '#endif /* DISPATCH_TABLE_NAME */' |
| 169 | print '' |
| 170 | return |
| 171 | |
| 172 | def printAliasedTable(self): |
| 173 | print """ |
| 174 | /* |
| 175 | * This is just used to silence compiler warnings. |
| 176 | * We list the functions which are not otherwise used. |
| 177 | */ |
| 178 | #ifdef UNUSED_TABLE_NAME |
| 179 | static const void * const UNUSED_TABLE_NAME[] = {""" |
| 180 | |
| 181 | keys = self.functions.keys() |
| 182 | keys.sort() |
| 183 | keys.reverse(); |
| 184 | for k in keys: |
| 185 | f = self.functions[k] |
| 186 | if f.fn_offset < 0: |
| 187 | print ' TABLE_ENTRY(%s),' % (f.name) |
| 188 | |
| 189 | print '};' |
| 190 | print '#endif /*UNUSED_TABLE_NAME*/' |
| 191 | print '' |
| 192 | return |
| 193 | |
| 194 | def printRealFooter(self): |
| 195 | self.printInitDispatch() |
| 196 | self.printAliasedTable() |
| 197 | print""" |
| 198 | #undef KEYWORD1 |
| 199 | #undef KEYWORD2 |
| 200 | #undef NAME |
| 201 | #undef DISPATCH |
| 202 | #undef RETURN_DISPATCH |
| 203 | #undef DISPATCH_TABLE_NAME |
| 204 | #undef UNUSED_TABLE_NAME |
| 205 | #undef TABLE_ENTRY |
| 206 | """ |
| 207 | return |
| 208 | |
| 209 | def show_usage(): |
| 210 | print "Usage: %s [-f input_file_name]" % sys.argv[0] |
| 211 | sys.exit(1) |
| 212 | |
| 213 | if __name__ == '__main__': |
| 214 | file_name = "gl_API.xml" |
| 215 | |
| 216 | try: |
| 217 | (args, trail) = getopt.getopt(sys.argv[1:], "f:") |
| 218 | except Exception,e: |
| 219 | show_usage() |
| 220 | |
| 221 | for (arg,val) in args: |
| 222 | if arg == "-f": |
| 223 | file_name = val |
| 224 | |
| 225 | dh = PrintGlOffsets() |
| 226 | |
| 227 | parser = make_parser() |
| 228 | parser.setFeature(feature_namespaces, 0) |
| 229 | parser.setContentHandler(dh) |
| 230 | |
| 231 | f = open(file_name) |
| 232 | |
| 233 | dh.printHeader() |
| 234 | parser.parse(f) |
| 235 | dh.printFooter() |
| 236 | |