blob: 8ce3092213c83e1ae1ee9583e0a62d39d570f3a5 [file] [log] [blame]
Brian Paul6c0d72f2001-11-18 22:42:57 +00001#!/usr/bin/env python
2
Ian Romanick8e77da1c2004-06-29 19:08:20 +00003# $Id: glapitemp.py,v 1.6 2004/06/29 19:08:20 idr Exp $
Brian Paul6c0d72f2001-11-18 22:42:57 +00004
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
Brian Paulb15a3b42001-12-15 16:42:59 +000037import string
Brian Paul6c0d72f2001-11-18 22:42:57 +000038import apiparser;
39
40
41def 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
Ian Romanick8e77da1c2004-06-29 19:08:20 +000070#if defined( NAME )
Brian Paul6c0d72f2001-11-18 22:42:57 +000071#ifndef KEYWORD1
72#define KEYWORD1
73#endif
74
75#ifndef KEYWORD2
76#define KEYWORD2
77#endif
78
Brian Paul6c0d72f2001-11-18 22:42:57 +000079#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
Brian Paul25c5f1b2002-01-15 19:04:52 +000087GLAPI void GLAPIENTRY gl__unused413(void); /* silence warning */
Brian Paul6c0d72f2001-11-18 22:42:57 +000088"""
Brian Paul15d4a232001-12-14 03:17:00 +000089
Brian Paul6c0d72f2001-11-18 22:42:57 +000090#enddef
91
92
93def PrintTail():
94 print"""
95#undef KEYWORD1
96#undef KEYWORD2
97#undef NAME
98#undef DISPATCH
99#undef RETURN_DISPATCH
100#undef DISPATCH_TABLE_NAME
101#undef UNUSED_TABLE_NAME
102#undef TABLE_ENTRY
103"""
104#endif
105
106
107def MakeParamList(nameList):
108 n = len(nameList)
109 i = 1
110 result = ''
111 for name in nameList:
112 result = result + name
113 if i < n:
114 result = result + ', '
115 i = i + 1
116 return result
117#enddef
118
119
Brian Paulb15a3b42001-12-15 16:42:59 +0000120def Contains(haystack, needle):
121 if string.find(haystack, needle) >= 0:
122 return 1
123 else:
124 return 0
125#enddef
126
127
Brian Paul6c0d72f2001-11-18 22:42:57 +0000128def MakePrintfString(funcName, argTypeList, argNameList):
129 result = '(F, "gl%s(' % (funcName)
130
131 n = len(argTypeList)
132 i = 1
133 isPointer = {}
Brian Paulb15a3b42001-12-15 16:42:59 +0000134 floatv = {}
Brian Paul6c0d72f2001-11-18 22:42:57 +0000135 for argType in argTypeList:
136 isPointer[i] = 0
Brian Paulb15a3b42001-12-15 16:42:59 +0000137 floatv[i] = 0
Brian Paul6c0d72f2001-11-18 22:42:57 +0000138 if argType == 'GLenum':
139 result = result + '0x%x'
140 elif argType in ['GLfloat', 'GLdouble', 'GLclampf', 'GLclampd']:
141 result = result + '%f'
Brian Paulb15a3b42001-12-15 16:42:59 +0000142 elif argType in ['GLbyte', 'GLubyte', 'GLshort', 'GLushort', 'GLint', 'GLuint', 'GLboolean', 'GLsizei']:
Brian Paul6c0d72f2001-11-18 22:42:57 +0000143 result = result + '%d'
144 else:
145 result = result + '%p'
146 isPointer[i] = 1
Brian Paulb15a3b42001-12-15 16:42:59 +0000147 if argType[0:13] == 'const GLfloat' or argType[0:14] == 'const GLdouble':
148 if Contains(funcName, '2fv') or Contains(funcName, '2dv'):
149 result = result + ' /* %g, %g */'
150 floatv[i] = 2
151 elif Contains(funcName, '3fv') or Contains(funcName, '3dv'):
152 result = result + ' /* %g, %g, %g */'
153 floatv[i] = 3
154 elif Contains(funcName, '4fv') or Contains(funcName, '4dv'):
155 result = result + ' /* %g, %g, %g, %g */'
156 floatv[i] = 4
157 #endif
Brian Paul6c0d72f2001-11-18 22:42:57 +0000158 if i < n:
159 result = result + ', '
160 i = i + 1
161 #endfor
162
Brian Paul15d4a232001-12-14 03:17:00 +0000163 result = result + ');\\n"'
164
Brian Paul6c0d72f2001-11-18 22:42:57 +0000165 n = len(argNameList)
166 i = 1
167 if n > 0:
168 result = result + ', '
169 for pname in argNameList:
170 if isPointer[i]:
Brian Pauld37363d2002-11-30 17:18:46 +0000171 result = result + '(const void *) '
Brian Paul6c0d72f2001-11-18 22:42:57 +0000172 result = result + pname
Brian Paulb15a3b42001-12-15 16:42:59 +0000173 if floatv[i] == 2:
174 result = result + ', ' + pname + '[0], ' + pname + '[1]'
175 elif floatv[i] == 3:
176 result = result + ', ' + pname + '[0], ' + pname + '[1], ' + pname + '[2]'
177 elif floatv[i] == 4:
178 result = result + ', ' + pname + '[0], ' + pname + '[1], ' + pname + '[2], ' + pname + '[3]'
Brian Paul6c0d72f2001-11-18 22:42:57 +0000179 if i < n:
180 result = result + ', '
181 i = i + 1
182 result = result + ')'
183 return result
184#enddef
185
186
187records = []
188emittedFuncs = {}
189aliasedFuncs = []
190
191def FindOffset(funcName):
192 for (name, alias, offset) in records:
193 if name == funcName:
194 return offset
195 #endif
196 #endfor
197 return -1
198#enddef
199
200def EmitFunction(name, returnType, argTypeList, argNameList, alias, offset):
201 argList = apiparser.MakeArgList(argTypeList, argNameList)
202 parms = MakeParamList(argNameList)
203 printString = MakePrintfString(name, argTypeList, argNameList)
204 if alias == '':
205 dispatchName = name
206 else:
207 dispatchName = alias
208 if offset < 0:
209 offset = FindOffset(dispatchName)
210 if offset >= 0:
211 print 'KEYWORD1 %s KEYWORD2 NAME(%s)(%s)' % (returnType, name, argList)
212 print '{'
213 if returnType == 'void':
214 print ' DISPATCH(%s, (%s), %s);' % (dispatchName, parms, printString)
215 else:
216 print ' RETURN_DISPATCH(%s, (%s), %s);' % (dispatchName, parms, printString)
217 print '}'
218 print ''
219 records.append((name, dispatchName, offset))
220 if not emittedFuncs.has_key(offset):
221 emittedFuncs[offset] = name
222 else:
223 aliasedFuncs.append(name)
224 else:
225 print '/* No dispatch for %s() */' % (name)
226#endif
227
228
229def PrintInitDispatch():
230 print """
Ian Romanick8e77da1c2004-06-29 19:08:20 +0000231#endif /* defined( NAME ) */
Brian Paul6c0d72f2001-11-18 22:42:57 +0000232
233/*
234 * This is how a dispatch table can be initialized with all the functions
235 * we generated above.
236 */
237#ifdef DISPATCH_TABLE_NAME
238
239#ifndef TABLE_ENTRY
240#error TABLE_ENTRY must be defined
241#endif
242
243void *DISPATCH_TABLE_NAME[] = {"""
244 keys = emittedFuncs.keys()
245 keys.sort()
246 for k in keys:
247 print ' TABLE_ENTRY(%s),' % (emittedFuncs[k])
248
249 print ' /* A whole bunch of no-op functions. These might be called'
250 print ' * when someone tries to call a dynamically-registered'
251 print ' * extension function without a current rendering context.'
252 print ' */'
253 for i in range(1, 100):
254 print ' TABLE_ENTRY(Unused),'
255
256 print '};'
257 print '#endif /* DISPATCH_TABLE_NAME */'
258 print ''
259#enddef
260
261
262
263def PrintAliasedTable():
264 print """
265/*
266 * This is just used to silence compiler warnings.
267 * We list the functions which aren't otherwise used.
268 */
269#ifdef UNUSED_TABLE_NAME
270void *UNUSED_TABLE_NAME[] = {"""
271 for alias in aliasedFuncs:
272 print ' TABLE_ENTRY(%s),' % (alias)
273 #endfor
274 print '};'
275 print '#endif /*UNUSED_TABLE_NAME*/'
276 print ''
277#enddef
278
279
280
281PrintHead()
282apiparser.ProcessSpecFile("APIspec", EmitFunction)
283PrintInitDispatch()
284PrintAliasedTable()
285PrintTail()