gl_table.py revision af69d88d
1#!/usr/bin/python2
2
3# (C) Copyright IBM Corporation 2004
4# All Rights Reserved.
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
28import gl_XML
29import license
30import sys, getopt
31
32class PrintGlTable(gl_XML.gl_print_base):
33    def __init__(self, es=False):
34        gl_XML.gl_print_base.__init__(self)
35
36        self.es = es
37        self.header_tag = '_GLAPI_TABLE_H_'
38        self.name = "gl_table.py (from Mesa)"
39        self.license = license.bsd_license_template % ( \
40"""Copyright (C) 1999-2003  Brian Paul   All Rights Reserved.
41(C) Copyright IBM Corporation 2004""", "BRIAN PAUL, IBM")
42        self.ifdef_emitted = False;
43        return
44
45
46    def printBody(self, api):
47        for f in api.functionIterateByOffset():
48            if not f.is_abi() and not self.ifdef_emitted:
49                print '#if !defined HAVE_SHARED_GLAPI'
50                self.ifdef_emitted = True
51            arg_string = f.get_parameter_string()
52            print '   %s (GLAPIENTRYP %s)(%s); /* %d */' % (f.return_type, f.name, arg_string, f.offset)
53
54        print '#endif /* !defined HAVE_SHARED_GLAPI */'
55
56
57    def printRealHeader(self):
58        print '#ifndef GLAPIENTRYP'
59        print '# ifndef GLAPIENTRY'
60        print '#  define GLAPIENTRY'
61        print '# endif'
62        print ''
63        print '# define GLAPIENTRYP GLAPIENTRY *'
64        print '#endif'
65        print ''
66        print ''
67        print 'struct _glapi_table'
68        print '{'
69        return
70
71
72    def printRealFooter(self):
73        print '};'
74        return
75
76
77class PrintRemapTable(gl_XML.gl_print_base):
78    def __init__(self, es=False):
79        gl_XML.gl_print_base.__init__(self)
80
81        self.es = es
82        self.header_tag = '_DISPATCH_H_'
83        self.name = "gl_table.py (from Mesa)"
84        self.license = license.bsd_license_template % ("(C) Copyright IBM Corporation 2005", "IBM")
85        return
86
87
88    def printRealHeader(self):
89        print """
90/**
91 * \\file main/dispatch.h
92 * Macros for handling GL dispatch tables.
93 *
94 * For each known GL function, there are 3 macros in this file.  The first
95 * macro is named CALL_FuncName and is used to call that GL function using
96 * the specified dispatch table.  The other 2 macros, called GET_FuncName
97 * can SET_FuncName, are used to get and set the dispatch pointer for the
98 * named function in the specified dispatch table.
99 */
100"""
101        return
102
103    def printBody(self, api):
104        print '#define CALL_by_offset(disp, cast, offset, parameters) \\'
105        print '    (*(cast (GET_by_offset(disp, offset)))) parameters'
106        print '#define GET_by_offset(disp, offset) \\'
107        print '    (offset >= 0) ? (((_glapi_proc *)(disp))[offset]) : NULL'
108        print '#define SET_by_offset(disp, offset, fn) \\'
109        print '    do { \\'
110        print '        if ( (offset) < 0 ) { \\'
111        print '            /* fprintf( stderr, "[%s:%u] SET_by_offset(%p, %d, %s)!\\n", */ \\'
112        print '            /*         __func__, __LINE__, disp, offset, # fn); */ \\'
113        print '            /* abort(); */ \\'
114        print '        } \\'
115        print '        else { \\'
116        print '            ( (_glapi_proc *) (disp) )[offset] = (_glapi_proc) fn; \\'
117        print '        } \\'
118        print '    } while(0)'
119        print ''
120
121        functions = []
122        abi_functions = []
123        alias_functions = []
124        count = 0
125        for f in api.functionIterateByOffset():
126            if not f.is_abi():
127                functions.append( [f, count] )
128                count += 1
129            else:
130                abi_functions.append( [f, -1] )
131
132            if self.es:
133                # remember functions with aliases
134                if len(f.entry_points) > 1:
135                    alias_functions.append(f)
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        if self.es:
145            remap_table = "esLocalRemapTable"
146
147            print '#define %s_size %u' % (remap_table, count)
148            print 'static int %s[ %s_size ];' % (remap_table, remap_table)
149            print ''
150        else:
151            remap_table = "driDispatchRemapTable"
152
153            print '#define %s_size %u' % (remap_table, count)
154            print 'extern int %s[ %s_size ];' % (remap_table, remap_table)
155            print ''
156
157        for f, index in functions:
158            print '#define %s_remap_index %u' % (f.name, index)
159
160        print ''
161
162        for f, index in functions:
163            print '#define _gloffset_%s %s[%s_remap_index]' % (f.name, remap_table, f.name)
164
165        print ''
166
167        for f, index in abi_functions + functions:
168            arg_string = gl_XML.create_parameter_string( f.parameters, 0 )
169
170            print 'typedef %s (GLAPIENTRYP _glptr_%s)(%s);' % (f.return_type, f.name, arg_string)
171            print '#define CALL_%s(disp, parameters) \\' % (f.name)
172            print '    (* GET_%s(disp)) parameters' % (f.name)
173            print 'static inline _glptr_%s GET_%s(struct _glapi_table *disp) {' % (f.name, f.name)
174            print '   return (_glptr_%s) (GET_by_offset(disp, _gloffset_%s));' % (f.name, f.name)
175            print '}'
176            print
177            print 'static inline void SET_%s(struct _glapi_table *disp, %s (GLAPIENTRYP fn)(%s)) {' % (f.name, f.return_type, arg_string)
178            print '   SET_by_offset(disp, _gloffset_%s, fn);' % (f.name)
179            print '}'
180            print
181
182        if alias_functions:
183            print ''
184            print '/* define aliases for compatibility */'
185            for f in alias_functions:
186                for name in f.entry_points:
187                    if name != f.name:
188                        print '#define CALL_%s(disp, parameters) CALL_%s(disp, parameters)' % (name, f.name)
189                        print '#define GET_%s(disp) GET_%s(disp)' % (name, f.name)
190                        print '#define SET_%s(disp, fn) SET_%s(disp, fn)' % (name, f.name)
191            print ''
192
193            for f in alias_functions:
194                for name in f.entry_points:
195                    if name != f.name:
196                        print '#define %s_remap_index %s_remap_index' % (name, f.name)
197            print ''
198
199        return
200
201
202def show_usage():
203    print "Usage: %s [-f input_file_name] [-m mode] [-c ver]" % sys.argv[0]
204    print "    -m mode   Mode can be 'table' or 'remap_table'."
205    print "    -c ver    Version can be 'es1' or 'es2'."
206    sys.exit(1)
207
208if __name__ == '__main__':
209    file_name = "gl_API.xml"
210
211    try:
212        (args, trail) = getopt.getopt(sys.argv[1:], "f:m:c:")
213    except Exception,e:
214        show_usage()
215
216    mode = "table"
217    es = None
218    for (arg,val) in args:
219        if arg == "-f":
220            file_name = val
221        elif arg == "-m":
222            mode = val
223        elif arg == "-c":
224            es = val
225
226    if mode == "table":
227        printer = PrintGlTable(es)
228    elif mode == "remap_table":
229        printer = PrintRemapTable(es)
230    else:
231        show_usage()
232
233    api = gl_XML.parse_GL_API( file_name )
234
235    if es is not None:
236        api.filter_functions_by_api(es)
237
238    printer.Print( api )
239