minor tweaks
[mesa.git] / src / mesa / glapi / glapitemp.py
1 #!/usr/bin/env python
2
3 # $Id: glapitemp.py,v 1.2 2001/12/14 03:17:00 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
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 extern void 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 MakePrintfString(funcName, argTypeList, argNameList):
124 result = '(F, "gl%s(' % (funcName)
125
126 n = len(argTypeList)
127 i = 1
128 isPointer = {}
129 for argType in argTypeList:
130 isPointer[i] = 0
131 if argType == 'GLenum':
132 result = result + '0x%x'
133 elif argType in ['GLfloat', 'GLdouble', 'GLclampf', 'GLclampd']:
134 result = result + '%f'
135 elif argType in ['GLbyte', 'GLubyte', 'GLshort', 'GLushort', 'GLint', 'GLuint', 'GLboolean']:
136 result = result + '%d'
137 else:
138 result = result + '%p'
139 isPointer[i] = 1
140 if i < n:
141 result = result + ', '
142 i = i + 1
143 #endfor
144
145 result = result + ');\\n"'
146
147 n = len(argNameList)
148 i = 1
149 if n > 0:
150 result = result + ', '
151 for pname in argNameList:
152 if isPointer[i]:
153 result = result + '(void *) '
154 result = result + pname
155 if i < n:
156 result = result + ', '
157 i = i + 1
158 result = result + ')'
159 return result
160 #enddef
161
162
163 records = []
164 emittedFuncs = {}
165 aliasedFuncs = []
166
167 def FindOffset(funcName):
168 for (name, alias, offset) in records:
169 if name == funcName:
170 return offset
171 #endif
172 #endfor
173 return -1
174 #enddef
175
176 def EmitFunction(name, returnType, argTypeList, argNameList, alias, offset):
177 argList = apiparser.MakeArgList(argTypeList, argNameList)
178 parms = MakeParamList(argNameList)
179 printString = MakePrintfString(name, argTypeList, argNameList)
180 if alias == '':
181 dispatchName = name
182 else:
183 dispatchName = alias
184 if offset < 0:
185 offset = FindOffset(dispatchName)
186 if offset >= 0:
187 print 'KEYWORD1 %s KEYWORD2 NAME(%s)(%s)' % (returnType, name, argList)
188 print '{'
189 if returnType == 'void':
190 print ' DISPATCH(%s, (%s), %s);' % (dispatchName, parms, printString)
191 else:
192 print ' RETURN_DISPATCH(%s, (%s), %s);' % (dispatchName, parms, printString)
193 print '}'
194 print ''
195 records.append((name, dispatchName, offset))
196 if not emittedFuncs.has_key(offset):
197 emittedFuncs[offset] = name
198 else:
199 aliasedFuncs.append(name)
200 else:
201 print '/* No dispatch for %s() */' % (name)
202 #endif
203
204
205 def PrintInitDispatch():
206 print """
207
208 /*
209 * This is how a dispatch table can be initialized with all the functions
210 * we generated above.
211 */
212 #ifdef DISPATCH_TABLE_NAME
213
214 #ifndef TABLE_ENTRY
215 #error TABLE_ENTRY must be defined
216 #endif
217
218 void *DISPATCH_TABLE_NAME[] = {"""
219 keys = emittedFuncs.keys()
220 keys.sort()
221 for k in keys:
222 print ' TABLE_ENTRY(%s),' % (emittedFuncs[k])
223
224 print ' /* A whole bunch of no-op functions. These might be called'
225 print ' * when someone tries to call a dynamically-registered'
226 print ' * extension function without a current rendering context.'
227 print ' */'
228 for i in range(1, 100):
229 print ' TABLE_ENTRY(Unused),'
230
231 print '};'
232 print '#endif /* DISPATCH_TABLE_NAME */'
233 print ''
234 #enddef
235
236
237
238 def PrintAliasedTable():
239 print """
240 /*
241 * This is just used to silence compiler warnings.
242 * We list the functions which aren't otherwise used.
243 */
244 #ifdef UNUSED_TABLE_NAME
245 void *UNUSED_TABLE_NAME[] = {"""
246 for alias in aliasedFuncs:
247 print ' TABLE_ENTRY(%s),' % (alias)
248 #endfor
249 print '};'
250 print '#endif /*UNUSED_TABLE_NAME*/'
251 print ''
252 #enddef
253
254
255
256 PrintHead()
257 apiparser.ProcessSpecFile("APIspec", EmitFunction)
258 PrintInitDispatch()
259 PrintAliasedTable()
260 PrintTail()