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