s/functionOffset/offset/
[mesa.git] / src / mesa / glapi / glapitemp.py
1 #!/usr/bin/env python
2
3 # $Id: glapitemp.py,v 1.4 2002/01/15 19:04:53 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 #ifndef KEYWORD1
71 #define KEYWORD1
72 #endif
73
74 #ifndef KEYWORD2
75 #define KEYWORD2
76 #endif
77
78 #ifndef NAME
79 #error NAME must be defined
80 #endif
81
82 #ifndef DISPATCH
83 #error DISPATCH must be defined
84 #endif
85
86 #ifndef RETURN_DISPATCH
87 #error RETURN_DISPATCH must be defined
88 #endif
89
90 GLAPI void GLAPIENTRY gl__unused413(void); /* silence warning */
91 """
92
93 #enddef
94
95
96 def PrintTail():
97 print"""
98 #undef KEYWORD1
99 #undef KEYWORD2
100 #undef NAME
101 #undef DISPATCH
102 #undef RETURN_DISPATCH
103 #undef DISPATCH_TABLE_NAME
104 #undef UNUSED_TABLE_NAME
105 #undef TABLE_ENTRY
106 """
107 #endif
108
109
110 def MakeParamList(nameList):
111 n = len(nameList)
112 i = 1
113 result = ''
114 for name in nameList:
115 result = result + name
116 if i < n:
117 result = result + ', '
118 i = i + 1
119 return result
120 #enddef
121
122
123 def Contains(haystack, needle):
124 if string.find(haystack, needle) >= 0:
125 return 1
126 else:
127 return 0
128 #enddef
129
130
131 def MakePrintfString(funcName, argTypeList, argNameList):
132 result = '(F, "gl%s(' % (funcName)
133
134 n = len(argTypeList)
135 i = 1
136 isPointer = {}
137 floatv = {}
138 for argType in argTypeList:
139 isPointer[i] = 0
140 floatv[i] = 0
141 if argType == 'GLenum':
142 result = result + '0x%x'
143 elif argType in ['GLfloat', 'GLdouble', 'GLclampf', 'GLclampd']:
144 result = result + '%f'
145 elif argType in ['GLbyte', 'GLubyte', 'GLshort', 'GLushort', 'GLint', 'GLuint', 'GLboolean', 'GLsizei']:
146 result = result + '%d'
147 else:
148 result = result + '%p'
149 isPointer[i] = 1
150 if argType[0:13] == 'const GLfloat' or argType[0:14] == 'const GLdouble':
151 if Contains(funcName, '2fv') or Contains(funcName, '2dv'):
152 result = result + ' /* %g, %g */'
153 floatv[i] = 2
154 elif Contains(funcName, '3fv') or Contains(funcName, '3dv'):
155 result = result + ' /* %g, %g, %g */'
156 floatv[i] = 3
157 elif Contains(funcName, '4fv') or Contains(funcName, '4dv'):
158 result = result + ' /* %g, %g, %g, %g */'
159 floatv[i] = 4
160 #endif
161 if i < n:
162 result = result + ', '
163 i = i + 1
164 #endfor
165
166 result = result + ');\\n"'
167
168 n = len(argNameList)
169 i = 1
170 if n > 0:
171 result = result + ', '
172 for pname in argNameList:
173 if isPointer[i]:
174 result = result + '(void *) '
175 result = result + pname
176 if floatv[i] == 2:
177 result = result + ', ' + pname + '[0], ' + pname + '[1]'
178 elif floatv[i] == 3:
179 result = result + ', ' + pname + '[0], ' + pname + '[1], ' + pname + '[2]'
180 elif floatv[i] == 4:
181 result = result + ', ' + pname + '[0], ' + pname + '[1], ' + pname + '[2], ' + pname + '[3]'
182 if i < n:
183 result = result + ', '
184 i = i + 1
185 result = result + ')'
186 return result
187 #enddef
188
189
190 records = []
191 emittedFuncs = {}
192 aliasedFuncs = []
193
194 def FindOffset(funcName):
195 for (name, alias, offset) in records:
196 if name == funcName:
197 return offset
198 #endif
199 #endfor
200 return -1
201 #enddef
202
203 def EmitFunction(name, returnType, argTypeList, argNameList, alias, offset):
204 argList = apiparser.MakeArgList(argTypeList, argNameList)
205 parms = MakeParamList(argNameList)
206 printString = MakePrintfString(name, argTypeList, argNameList)
207 if alias == '':
208 dispatchName = name
209 else:
210 dispatchName = alias
211 if offset < 0:
212 offset = FindOffset(dispatchName)
213 if offset >= 0:
214 print 'KEYWORD1 %s KEYWORD2 NAME(%s)(%s)' % (returnType, name, argList)
215 print '{'
216 if returnType == 'void':
217 print ' DISPATCH(%s, (%s), %s);' % (dispatchName, parms, printString)
218 else:
219 print ' RETURN_DISPATCH(%s, (%s), %s);' % (dispatchName, parms, printString)
220 print '}'
221 print ''
222 records.append((name, dispatchName, offset))
223 if not emittedFuncs.has_key(offset):
224 emittedFuncs[offset] = name
225 else:
226 aliasedFuncs.append(name)
227 else:
228 print '/* No dispatch for %s() */' % (name)
229 #endif
230
231
232 def PrintInitDispatch():
233 print """
234
235 /*
236 * This is how a dispatch table can be initialized with all the functions
237 * we generated above.
238 */
239 #ifdef DISPATCH_TABLE_NAME
240
241 #ifndef TABLE_ENTRY
242 #error TABLE_ENTRY must be defined
243 #endif
244
245 void *DISPATCH_TABLE_NAME[] = {"""
246 keys = emittedFuncs.keys()
247 keys.sort()
248 for k in keys:
249 print ' TABLE_ENTRY(%s),' % (emittedFuncs[k])
250
251 print ' /* A whole bunch of no-op functions. These might be called'
252 print ' * when someone tries to call a dynamically-registered'
253 print ' * extension function without a current rendering context.'
254 print ' */'
255 for i in range(1, 100):
256 print ' TABLE_ENTRY(Unused),'
257
258 print '};'
259 print '#endif /* DISPATCH_TABLE_NAME */'
260 print ''
261 #enddef
262
263
264
265 def PrintAliasedTable():
266 print """
267 /*
268 * This is just used to silence compiler warnings.
269 * We list the functions which aren't otherwise used.
270 */
271 #ifdef UNUSED_TABLE_NAME
272 void *UNUSED_TABLE_NAME[] = {"""
273 for alias in aliasedFuncs:
274 print ' TABLE_ENTRY(%s),' % (alias)
275 #endfor
276 print '};'
277 print '#endif /*UNUSED_TABLE_NAME*/'
278 print ''
279 #enddef
280
281
282
283 PrintHead()
284 apiparser.ProcessSpecFile("APIspec", EmitFunction)
285 PrintInitDispatch()
286 PrintAliasedTable()
287 PrintTail()