Move compiler.h and imports.h/c from src/mesa/main into src/util
[mesa.git] / src / mapi / glapi / gen / gl_table.py
1
2 # (C) Copyright IBM Corporation 2004
3 # All Rights Reserved.
4 # Copyright (c) 2014 Intel Corporation
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining a
7 # copy of this software and associated documentation files (the "Software"),
8 # to deal in the Software without restriction, including without limitation
9 # on the rights to use, copy, modify, merge, publish, distribute, sub
10 # license, and/or sell copies of the Software, and to permit persons to whom
11 # the Software is furnished to do so, subject to the following conditions:
12 #
13 # The above copyright notice and this permission notice (including the next
14 # paragraph) shall be included in all copies or substantial portions of the
15 # Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20 # IBM AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 # IN THE SOFTWARE.
24 #
25 # Authors:
26 # Ian Romanick <idr@us.ibm.com>
27
28 from __future__ import print_function
29
30 import argparse
31
32 import gl_XML
33 import license
34
35
36 class PrintGlTable(gl_XML.gl_print_base):
37 def __init__(self):
38 gl_XML.gl_print_base.__init__(self)
39
40 self.header_tag = '_GLAPI_TABLE_H_'
41 self.name = "gl_table.py (from Mesa)"
42 self.license = license.bsd_license_template % ( \
43 """Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
44 (C) Copyright IBM Corporation 2004""", "BRIAN PAUL, IBM")
45 return
46
47 def printBody(self, api):
48 for f in api.functionIterateByOffset():
49 arg_string = f.get_parameter_string()
50 print(' %s (GLAPIENTRYP %s)(%s); /* %d */' % (
51 f.return_type, f.name, arg_string, f.offset))
52
53 def printRealHeader(self):
54 print('#ifndef GLAPIENTRYP')
55 print('# ifndef GLAPIENTRY')
56 print('# define GLAPIENTRY')
57 print('# endif')
58 print('')
59 print('# define GLAPIENTRYP GLAPIENTRY *')
60 print('#endif')
61 print('')
62 print('')
63 print('#ifdef __cplusplus')
64 print('extern "C" {')
65 print('#endif')
66 print('')
67 print('struct _glapi_table')
68 print('{')
69 return
70
71 def printRealFooter(self):
72 print('};')
73 print('')
74 print('#ifdef __cplusplus')
75 print('}')
76 print('#endif')
77 return
78
79
80 class PrintRemapTable(gl_XML.gl_print_base):
81 def __init__(self):
82 gl_XML.gl_print_base.__init__(self)
83
84 self.header_tag = '_DISPATCH_H_'
85 self.name = "gl_table.py (from Mesa)"
86 self.license = license.bsd_license_template % (
87 "(C) Copyright IBM Corporation 2005", "IBM")
88 return
89
90
91 def printRealHeader(self):
92 print("""
93 /**
94 * \\file main/dispatch.h
95 * Macros for handling GL dispatch tables.
96 *
97 * For each known GL function, there are 3 macros in this file. The first
98 * macro is named CALL_FuncName and is used to call that GL function using
99 * the specified dispatch table. The other 2 macros, called GET_FuncName
100 * can SET_FuncName, are used to get and set the dispatch pointer for the
101 * named function in the specified dispatch table.
102 */
103
104 #include "main/glheader.h"
105 """)
106 return
107
108
109 def printBody(self, api):
110 print('#define CALL_by_offset(disp, cast, offset, parameters) \\')
111 print(' (*(cast (GET_by_offset(disp, offset)))) parameters')
112 print('#define GET_by_offset(disp, offset) \\')
113 print(' (offset >= 0) ? (((_glapi_proc *)(disp))[offset]) : NULL')
114 print('#define SET_by_offset(disp, offset, fn) \\')
115 print(' do { \\')
116 print(' if ( (offset) < 0 ) { \\')
117 print(' /* fprintf( stderr, "[%s:%u] SET_by_offset(%p, %d, %s)!\\n", */ \\')
118 print(' /* __func__, __LINE__, disp, offset, # fn); */ \\')
119 print(' /* abort(); */ \\')
120 print(' } \\')
121 print(' else { \\')
122 print(' ( (_glapi_proc *) (disp) )[offset] = (_glapi_proc) fn; \\')
123 print(' } \\')
124 print(' } while(0)')
125 print('')
126
127 functions = []
128 abi_functions = []
129 count = 0
130 for f in api.functionIterateByOffset():
131 if not f.is_abi():
132 functions.append([f, count])
133 count += 1
134 else:
135 abi_functions.append([f, -1])
136
137 print('/* total number of offsets below */')
138 print('#define _gloffset_COUNT %d' % (len(abi_functions + functions)))
139 print('')
140
141 for f, index in abi_functions:
142 print('#define _gloffset_%s %d' % (f.name, f.offset))
143
144 remap_table = "driDispatchRemapTable"
145
146 print('#define %s_size %u' % (remap_table, count))
147 print('extern int %s[ %s_size ];' % (remap_table, remap_table))
148 print('')
149
150 for f, index in functions:
151 print('#define %s_remap_index %u' % (f.name, index))
152
153 print('')
154
155 for f, index in functions:
156 print('#define _gloffset_%s %s[%s_remap_index]' % (f.name, remap_table, f.name))
157
158 print('')
159
160 for f, index in abi_functions + functions:
161 arg_string = gl_XML.create_parameter_string(f.parameters, 0)
162
163 print('typedef %s (GLAPIENTRYP _glptr_%s)(%s);' % (f.return_type, f.name, arg_string))
164 print('#define CALL_%s(disp, parameters) \\' % (f.name))
165 print(' (* GET_%s(disp)) parameters' % (f.name))
166 print('static inline _glptr_%s GET_%s(struct _glapi_table *disp) {' % (f.name, f.name))
167 print(' return (_glptr_%s) (GET_by_offset(disp, _gloffset_%s));' % (f.name, f.name))
168 print('}')
169 print()
170 print('static inline void SET_%s(struct _glapi_table *disp, %s (GLAPIENTRYP fn)(%s)) {' % (f.name, f.return_type, arg_string))
171 print(' SET_by_offset(disp, _gloffset_%s, fn);' % (f.name))
172 print('}')
173 print()
174
175 return
176
177
178 def _parser():
179 """Parse arguments and return a namespace."""
180 parser = argparse.ArgumentParser()
181 parser.add_argument('-f', '--filename',
182 default='gl_API.xml',
183 metavar="input_file_name",
184 dest='file_name',
185 help="Path to an XML description of OpenGL API.")
186 parser.add_argument('-m', '--mode',
187 choices=['table', 'remap_table'],
188 default='table',
189 metavar="mode",
190 help="Generate either a table or a remap_table")
191 return parser.parse_args()
192
193
194 def main():
195 """Main function."""
196 args = _parser()
197
198 api = gl_XML.parse_GL_API(args.file_name)
199
200 if args.mode == "table":
201 printer = PrintGlTable()
202 elif args.mode == "remap_table":
203 printer = PrintRemapTable()
204
205 printer.Print(api)
206
207
208 if __name__ == '__main__':
209 main()