Mammoth update to the Python code generator scripts that live in
[mesa.git] / src / mesa / glapi / gl_apitemp.py
1 #!/usr/bin/env python
2
3 # (C) Copyright IBM Corporation 2004, 2005
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 import gl_XML
29 import license
30 import sys, getopt
31
32 class PrintGlOffsets(gl_XML.gl_print_base):
33 def __init__(self):
34 gl_XML.gl_print_base.__init__(self)
35
36 self.name = "gl_apitemp.py (from Mesa)"
37 self.license = license.bsd_license_template % ( \
38 """Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
39 (C) Copyright IBM Corporation 2004""", "BRIAN PAUL, IBM")
40
41 self.undef_list.append( "KEYWORD1" )
42 self.undef_list.append( "KEYWORD2" )
43 self.undef_list.append( "NAME" )
44 self.undef_list.append( "DISPATCH" )
45 self.undef_list.append( "RETURN_DISPATCH" )
46 self.undef_list.append( "DISPATCH_TABLE_NAME" )
47 self.undef_list.append( "UNUSED_TABLE_NAME" )
48 self.undef_list.append( "TABLE_ENTRY" )
49
50
51 def printFunction(self, f, name):
52 p_string = ""
53 o_string = ""
54 t_string = ""
55 comma = ""
56
57 for p in f.parameterIterator():
58 if p.is_pointer():
59 cast = "(const void *) "
60 else:
61 cast = ""
62
63 t_string = t_string + comma + p.format_string()
64 p_string = p_string + comma + p.name
65 o_string = o_string + comma + cast + p.name
66 comma = ", "
67
68
69 if f.return_type != 'void':
70 dispatch = "RETURN_DISPATCH"
71 else:
72 dispatch = "DISPATCH"
73
74 print 'KEYWORD1 %s KEYWORD2 NAME(%s)(%s)' \
75 % (f.return_type, name, f.get_parameter_string())
76 print '{'
77 if p_string == "":
78 print ' %s(%s, (), (F, "gl%s();\\n"));' \
79 % (dispatch, f.name, name)
80 else:
81 print ' %s(%s, (%s), (F, "gl%s(%s);\\n", %s));' \
82 % (dispatch, f.name, p_string, name, t_string, o_string)
83 print '}'
84 print ''
85 return
86
87 def printRealHeader(self):
88 print """
89 /*
90 * This file is a template which generates the OpenGL API entry point
91 * functions. It should be included by a .c file which first defines
92 * the following macros:
93 * KEYWORD1 - usually nothing, but might be __declspec(dllexport) on Win32
94 * KEYWORD2 - usually nothing, but might be __stdcall on Win32
95 * NAME(n) - builds the final function name (usually add "gl" prefix)
96 * DISPATCH(func, args, msg) - code to do dispatch of named function.
97 * msg is a printf-style debug message.
98 * RETURN_DISPATCH(func, args, msg) - code to do dispatch with a return value
99 *
100 * Here is an example which generates the usual OpenGL functions:
101 * #define KEYWORD1
102 * #define KEYWORD2
103 * #define NAME(func) gl##func
104 * #define DISPATCH(func, args, msg) \\
105 * struct _glapi_table *dispatch = CurrentDispatch; \\
106 * (*dispatch->func) args
107 * #define RETURN DISPATCH(func, args, msg) \\
108 * struct _glapi_table *dispatch = CurrentDispatch; \\
109 * return (*dispatch->func) args
110 *
111 */
112
113
114 #if defined( NAME )
115 #ifndef KEYWORD1
116 #define KEYWORD1
117 #endif
118
119 #ifndef KEYWORD2
120 #define KEYWORD2
121 #endif
122
123 #ifndef DISPATCH
124 #error DISPATCH must be defined
125 #endif
126
127 #ifndef RETURN_DISPATCH
128 #error RETURN_DISPATCH must be defined
129 #endif
130
131 """
132 return
133
134
135
136 def printInitDispatch(self, api):
137 print """
138 #endif /* defined( NAME ) */
139
140 /*
141 * This is how a dispatch table can be initialized with all the functions
142 * we generated above.
143 */
144 #ifdef DISPATCH_TABLE_NAME
145
146 #ifndef TABLE_ENTRY
147 #error TABLE_ENTRY must be defined
148 #endif
149
150 static _glapi_proc DISPATCH_TABLE_NAME[] = {"""
151 for f in api.functionIterateByOffset():
152 print ' TABLE_ENTRY(%s),' % (f.name)
153
154 print ' /* A whole bunch of no-op functions. These might be called'
155 print ' * when someone tries to call a dynamically-registered'
156 print ' * extension function without a current rendering context.'
157 print ' */'
158 for i in range(1, 100):
159 print ' TABLE_ENTRY(Unused),'
160
161 print '};'
162 print '#endif /* DISPATCH_TABLE_NAME */'
163 print ''
164 return
165
166
167 def printAliasedTable(self, api):
168 print """
169 /*
170 * This is just used to silence compiler warnings.
171 * We list the functions which are not otherwise used.
172 */
173 #ifdef UNUSED_TABLE_NAME
174 static _glapi_proc UNUSED_TABLE_NAME[] = {"""
175
176 for f in api.functionIterateByOffset():
177 for n in f.entry_points:
178 if n != f.name:
179 print ' TABLE_ENTRY(%s),' % (n)
180
181 print '};'
182 print '#endif /*UNUSED_TABLE_NAME*/'
183 print ''
184 return
185
186
187 def printBody(self, api):
188 for func in api.functionIterateByOffset():
189 for n in func.entry_points:
190 self.printFunction( func, n )
191
192 self.printInitDispatch(api)
193 self.printAliasedTable(api)
194 return
195
196
197 def show_usage():
198 print "Usage: %s [-f input_file_name]" % sys.argv[0]
199 sys.exit(1)
200
201 if __name__ == '__main__':
202 file_name = "gl_API.xml"
203
204 try:
205 (args, trail) = getopt.getopt(sys.argv[1:], "f:")
206 except Exception,e:
207 show_usage()
208
209 for (arg,val) in args:
210 if arg == "-f":
211 file_name = val
212
213 api = gl_XML.parse_GL_API( file_name )
214
215 printer = PrintGlOffsets()
216 printer.Print(api)