regenerate with correct script
[mesa.git] / src / mesa / glapi / glapitemp.py
1 #!/usr/bin/env python
2
3 # $Id: glapitemp.py,v 1.7 2004/10/02 22:47:48 brianp Exp $
4
5 # Mesa 3-D graphics library
6 # Version: 4.1
7 #
8 # Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
9 #
10 # Permission is hereby granted, free of charge, to any person obtaining a
11 # copy of this software and associated documentation files (the "Software"),
12 # to deal in the Software without restriction, including without limitation
13 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 # and/or sell copies of the Software, and to permit persons to whom the
15 # Software is furnished to do so, subject to the following conditions:
16 #
17 # The above copyright notice and this permission notice shall be included
18 # in all copies or substantial portions of the Software.
19 #
20 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 # BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
24 # AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
27
28 # Generate the glapitemp.h file.
29 #
30 # Usage:
31 # gloffsets.py >glapitemp.h
32 #
33 # Dependencies:
34 # The apispec file must be in the current directory.
35
36
37 import string
38 import apiparser;
39
40
41 def PrintHead():
42 print """
43 /* DO NOT EDIT! This file is generated by the glapitemp.py script. */
44
45 /*
46 * This file is a template which generates the OpenGL API entry point
47 * functions. It should be included by a .c file which first defines
48 * the following macros:
49 * KEYWORD1 - usually nothing, but might be __declspec(dllexport) on Win32
50 * KEYWORD2 - usually nothing, but might be __stdcall on Win32
51 * NAME(n) - builds the final function name (usually add "gl" prefix)
52 * DISPATCH(func, args, msg) - code to do dispatch of named function.
53 * msg is a printf-style debug message.
54 * RETURN_DISPATCH(func, args, msg) - code to do dispatch with a return value
55 *
56 * Here's an example which generates the usual OpenGL functions:
57 * #define KEYWORD1
58 * #define KEYWORD2
59 * #define NAME(func) gl##func
60 * #define DISPATCH(func, args, msg) \\
61 * struct _glapi_table *dispatch = CurrentDispatch; \\
62 * (*dispatch->func) args
63 * #define RETURN DISPATCH(func, args, msg) \\
64 * struct _glapi_table *dispatch = CurrentDispatch; \\
65 * return (*dispatch->func) args
66 *
67 */
68
69
70 #if defined( NAME )
71 #ifndef KEYWORD1
72 #define KEYWORD1
73 #endif
74
75 #ifndef KEYWORD2
76 #define KEYWORD2
77 #endif
78
79 #ifndef DISPATCH
80 #error DISPATCH must be defined
81 #endif
82
83 #ifndef RETURN_DISPATCH
84 #error RETURN_DISPATCH must be defined
85 #endif
86
87 """
88
89 #enddef
90
91
92 def PrintTail():
93 print"""
94 #undef KEYWORD1
95 #undef KEYWORD2
96 #undef NAME
97 #undef DISPATCH
98 #undef RETURN_DISPATCH
99 #undef DISPATCH_TABLE_NAME
100 #undef UNUSED_TABLE_NAME
101 #undef TABLE_ENTRY
102 """
103 #endif
104
105
106 def MakeParamList(nameList):
107 n = len(nameList)
108 i = 1
109 result = ''
110 for name in nameList:
111 result = result + name
112 if i < n:
113 result = result + ', '
114 i = i + 1
115 return result
116 #enddef
117
118
119 def Contains(haystack, needle):
120 if string.find(haystack, needle) >= 0:
121 return 1
122 else:
123 return 0
124 #enddef
125
126
127 def MakePrintfString(funcName, argTypeList, argNameList):
128 result = '(F, "gl%s(' % (funcName)
129
130 n = len(argTypeList)
131 i = 1
132 isPointer = {}
133 floatv = {}
134 for argType in argTypeList:
135 isPointer[i] = 0
136 floatv[i] = 0
137 if argType == 'GLenum':
138 result = result + '0x%x'
139 elif argType in ['GLfloat', 'GLdouble', 'GLclampf', 'GLclampd']:
140 result = result + '%f'
141 elif argType in ['GLbyte', 'GLubyte', 'GLshort', 'GLushort', 'GLint', 'GLuint', 'GLboolean', 'GLsizei']:
142 result = result + '%d'
143 else:
144 result = result + '%p'
145 isPointer[i] = 1
146 if argType[0:13] == 'const GLfloat' or argType[0:14] == 'const GLdouble':
147 if Contains(funcName, '2fv') or Contains(funcName, '2dv'):
148 result = result + ' /* %g, %g */'
149 floatv[i] = 2
150 elif Contains(funcName, '3fv') or Contains(funcName, '3dv'):
151 result = result + ' /* %g, %g, %g */'
152 floatv[i] = 3
153 elif Contains(funcName, '4fv') or Contains(funcName, '4dv'):
154 result = result + ' /* %g, %g, %g, %g */'
155 floatv[i] = 4
156 #endif
157 if i < n:
158 result = result + ', '
159 i = i + 1
160 #endfor
161
162 result = result + ');\\n"'
163
164 n = len(argNameList)
165 i = 1
166 if n > 0:
167 result = result + ', '
168 for pname in argNameList:
169 if isPointer[i]:
170 result = result + '(const void *) '
171 result = result + pname
172 if floatv[i] == 2:
173 result = result + ', ' + pname + '[0], ' + pname + '[1]'
174 elif floatv[i] == 3:
175 result = result + ', ' + pname + '[0], ' + pname + '[1], ' + pname + '[2]'
176 elif floatv[i] == 4:
177 result = result + ', ' + pname + '[0], ' + pname + '[1], ' + pname + '[2], ' + pname + '[3]'
178 if i < n:
179 result = result + ', '
180 i = i + 1
181 result = result + ')'
182 return result
183 #enddef
184
185
186 records = []
187 emittedFuncs = {}
188 aliasedFuncs = []
189
190 def FindOffset(funcName):
191 for (name, alias, offset) in records:
192 if name == funcName:
193 return offset
194 #endif
195 #endfor
196 return -1
197 #enddef
198
199 def EmitFunction(name, returnType, argTypeList, argNameList, alias, offset):
200 argList = apiparser.MakeArgList(argTypeList, argNameList)
201 parms = MakeParamList(argNameList)
202 printString = MakePrintfString(name, argTypeList, argNameList)
203 if alias == '':
204 dispatchName = name
205 else:
206 dispatchName = alias
207 if offset < 0:
208 offset = FindOffset(dispatchName)
209 if offset >= 0:
210 print 'KEYWORD1 %s KEYWORD2 NAME(%s)(%s)' % (returnType, name, argList)
211 print '{'
212 if returnType == 'void':
213 print ' DISPATCH(%s, (%s), %s);' % (dispatchName, parms, printString)
214 else:
215 print ' RETURN_DISPATCH(%s, (%s), %s);' % (dispatchName, parms, printString)
216 print '}'
217 print ''
218 records.append((name, dispatchName, offset))
219 if not emittedFuncs.has_key(offset):
220 emittedFuncs[offset] = name
221 else:
222 aliasedFuncs.append(name)
223 else:
224 print '/* No dispatch for %s() */' % (name)
225 #endif
226
227
228 def PrintInitDispatch():
229 print """
230 #endif /* defined( NAME ) */
231
232 /*
233 * This is how a dispatch table can be initialized with all the functions
234 * we generated above.
235 */
236 #ifdef DISPATCH_TABLE_NAME
237
238 #ifndef TABLE_ENTRY
239 #error TABLE_ENTRY must be defined
240 #endif
241
242 void *DISPATCH_TABLE_NAME[] = {"""
243 keys = emittedFuncs.keys()
244 keys.sort()
245 for k in keys:
246 print ' TABLE_ENTRY(%s),' % (emittedFuncs[k])
247
248 print ' /* A whole bunch of no-op functions. These might be called'
249 print ' * when someone tries to call a dynamically-registered'
250 print ' * extension function without a current rendering context.'
251 print ' */'
252 for i in range(1, 100):
253 print ' TABLE_ENTRY(Unused),'
254
255 print '};'
256 print '#endif /* DISPATCH_TABLE_NAME */'
257 print ''
258 #enddef
259
260
261
262 def PrintAliasedTable():
263 print """
264 /*
265 * This is just used to silence compiler warnings.
266 * We list the functions which aren't otherwise used.
267 */
268 #ifdef UNUSED_TABLE_NAME
269 void *UNUSED_TABLE_NAME[] = {"""
270 for alias in aliasedFuncs:
271 print ' TABLE_ENTRY(%s),' % (alias)
272 #endfor
273 print '};'
274 print '#endif /*UNUSED_TABLE_NAME*/'
275 print ''
276 #enddef
277
278
279
280 PrintHead()
281 apiparser.ProcessSpecFile("APIspec", EmitFunction)
282 PrintInitDispatch()
283 PrintAliasedTable()
284 PrintTail()