13464ebd5Sriastradh
23464ebd5Sriastradh# (C) Copyright IBM Corporation 2004
33464ebd5Sriastradh# All Rights Reserved.
401e04c3fSmrg# Copyright (c) 2014 Intel Corporation
53464ebd5Sriastradh#
63464ebd5Sriastradh# Permission is hereby granted, free of charge, to any person obtaining a
73464ebd5Sriastradh# copy of this software and associated documentation files (the "Software"),
83464ebd5Sriastradh# to deal in the Software without restriction, including without limitation
93464ebd5Sriastradh# on the rights to use, copy, modify, merge, publish, distribute, sub
103464ebd5Sriastradh# license, and/or sell copies of the Software, and to permit persons to whom
113464ebd5Sriastradh# the Software is furnished to do so, subject to the following conditions:
123464ebd5Sriastradh#
133464ebd5Sriastradh# The above copyright notice and this permission notice (including the next
143464ebd5Sriastradh# paragraph) shall be included in all copies or substantial portions of the
153464ebd5Sriastradh# Software.
163464ebd5Sriastradh#
173464ebd5Sriastradh# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
183464ebd5Sriastradh# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
193464ebd5Sriastradh# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
203464ebd5Sriastradh# IBM AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
213464ebd5Sriastradh# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
223464ebd5Sriastradh# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
233464ebd5Sriastradh# IN THE SOFTWARE.
243464ebd5Sriastradh#
253464ebd5Sriastradh# Authors:
263464ebd5Sriastradh#    Ian Romanick <idr@us.ibm.com>
273464ebd5Sriastradh
2801e04c3fSmrgimport argparse
2901e04c3fSmrg
303464ebd5Sriastradhimport gl_XML
313464ebd5Sriastradhimport license
3201e04c3fSmrg
333464ebd5Sriastradh
343464ebd5Sriastradhclass PrintGlTable(gl_XML.gl_print_base):
3501e04c3fSmrg    def __init__(self):
36af69d88dSmrg        gl_XML.gl_print_base.__init__(self)
373464ebd5Sriastradh
38af69d88dSmrg        self.header_tag = '_GLAPI_TABLE_H_'
39af69d88dSmrg        self.name = "gl_table.py (from Mesa)"
40af69d88dSmrg        self.license = license.bsd_license_template % ( \
413464ebd5Sriastradh"""Copyright (C) 1999-2003  Brian Paul   All Rights Reserved.
423464ebd5Sriastradh(C) Copyright IBM Corporation 2004""", "BRIAN PAUL, IBM")
43af69d88dSmrg        return
443464ebd5Sriastradh
45af69d88dSmrg    def printBody(self, api):
46af69d88dSmrg        for f in api.functionIterateByOffset():
47af69d88dSmrg            arg_string = f.get_parameter_string()
4801e04c3fSmrg            print('   %s (GLAPIENTRYP %s)(%s); /* %d */' % (
4901e04c3fSmrg                f.return_type, f.name, arg_string, f.offset))
503464ebd5Sriastradh
51af69d88dSmrg    def printRealHeader(self):
5201e04c3fSmrg        print('#ifndef GLAPIENTRYP')
5301e04c3fSmrg        print('# ifndef GLAPIENTRY')
5401e04c3fSmrg        print('#  define GLAPIENTRY')
5501e04c3fSmrg        print('# endif')
5601e04c3fSmrg        print('')
5701e04c3fSmrg        print('# define GLAPIENTRYP GLAPIENTRY *')
5801e04c3fSmrg        print('#endif')
5901e04c3fSmrg        print('')
6001e04c3fSmrg        print('')
6101e04c3fSmrg        print('#ifdef __cplusplus')
6201e04c3fSmrg        print('extern "C" {')
6301e04c3fSmrg        print('#endif')
6401e04c3fSmrg        print('')
657ec681f3Smrg        print('#ifdef MemoryBarrier')
667ec681f3Smrg        print('#undef MemoryBarrier')
677ec681f3Smrg        print('#endif')
687ec681f3Smrg        print('')
6901e04c3fSmrg        print('struct _glapi_table')
7001e04c3fSmrg        print('{')
71af69d88dSmrg        return
723464ebd5Sriastradh
73af69d88dSmrg    def printRealFooter(self):
7401e04c3fSmrg        print('};')
7501e04c3fSmrg        print('')
7601e04c3fSmrg        print('#ifdef __cplusplus')
7701e04c3fSmrg        print('}')
7801e04c3fSmrg        print('#endif')
79af69d88dSmrg        return
803464ebd5Sriastradh
813464ebd5Sriastradh
823464ebd5Sriastradhclass PrintRemapTable(gl_XML.gl_print_base):
8301e04c3fSmrg    def __init__(self):
84af69d88dSmrg        gl_XML.gl_print_base.__init__(self)
853464ebd5Sriastradh
86af69d88dSmrg        self.header_tag = '_DISPATCH_H_'
87af69d88dSmrg        self.name = "gl_table.py (from Mesa)"
8801e04c3fSmrg        self.license = license.bsd_license_template % (
8901e04c3fSmrg            "(C) Copyright IBM Corporation 2005", "IBM")
90af69d88dSmrg        return
913464ebd5Sriastradh
923464ebd5Sriastradh
93af69d88dSmrg    def printRealHeader(self):
9401e04c3fSmrg        print("""
953464ebd5Sriastradh/**
963464ebd5Sriastradh * \\file main/dispatch.h
973464ebd5Sriastradh * Macros for handling GL dispatch tables.
983464ebd5Sriastradh *
993464ebd5Sriastradh * For each known GL function, there are 3 macros in this file.  The first
1003464ebd5Sriastradh * macro is named CALL_FuncName and is used to call that GL function using
1013464ebd5Sriastradh * the specified dispatch table.  The other 2 macros, called GET_FuncName
1023464ebd5Sriastradh * can SET_FuncName, are used to get and set the dispatch pointer for the
1033464ebd5Sriastradh * named function in the specified dispatch table.
1043464ebd5Sriastradh */
1057ec681f3Smrg
1067ec681f3Smrg#include "main/glheader.h"
10701e04c3fSmrg""")
1087ec681f3Smrg        print('#include "main/glheader.h"')
109af69d88dSmrg        return
110af69d88dSmrg
11101e04c3fSmrg
112af69d88dSmrg    def printBody(self, api):
11301e04c3fSmrg        print('#define CALL_by_offset(disp, cast, offset, parameters) \\')
11401e04c3fSmrg        print('    (*(cast (GET_by_offset(disp, offset)))) parameters')
11501e04c3fSmrg        print('#define GET_by_offset(disp, offset) \\')
11601e04c3fSmrg        print('    (offset >= 0) ? (((_glapi_proc *)(disp))[offset]) : NULL')
11701e04c3fSmrg        print('#define SET_by_offset(disp, offset, fn) \\')
11801e04c3fSmrg        print('    do { \\')
11901e04c3fSmrg        print('        if ( (offset) < 0 ) { \\')
12001e04c3fSmrg        print('            /* fprintf( stderr, "[%s:%u] SET_by_offset(%p, %d, %s)!\\n", */ \\')
12101e04c3fSmrg        print('            /*         __func__, __LINE__, disp, offset, # fn); */ \\')
12201e04c3fSmrg        print('            /* abort(); */ \\')
12301e04c3fSmrg        print('        } \\')
12401e04c3fSmrg        print('        else { \\')
12501e04c3fSmrg        print('            ( (_glapi_proc *) (disp) )[offset] = (_glapi_proc) fn; \\')
12601e04c3fSmrg        print('        } \\')
12701e04c3fSmrg        print('    } while(0)')
12801e04c3fSmrg        print('')
129af69d88dSmrg
130af69d88dSmrg        functions = []
131af69d88dSmrg        abi_functions = []
132af69d88dSmrg        count = 0
133af69d88dSmrg        for f in api.functionIterateByOffset():
134af69d88dSmrg            if not f.is_abi():
13501e04c3fSmrg                functions.append([f, count])
136af69d88dSmrg                count += 1
137af69d88dSmrg            else:
13801e04c3fSmrg                abi_functions.append([f, -1])
139af69d88dSmrg
14001e04c3fSmrg        print('/* total number of offsets below */')
14101e04c3fSmrg        print('#define _gloffset_COUNT %d' % (len(abi_functions + functions)))
14201e04c3fSmrg        print('')
143af69d88dSmrg
144af69d88dSmrg        for f, index in abi_functions:
14501e04c3fSmrg            print('#define _gloffset_%s %d' % (f.name, f.offset))
146af69d88dSmrg
14701e04c3fSmrg        remap_table = "driDispatchRemapTable"
148af69d88dSmrg
14901e04c3fSmrg        print('#define %s_size %u' % (remap_table, count))
15001e04c3fSmrg        print('extern int %s[ %s_size ];' % (remap_table, remap_table))
15101e04c3fSmrg        print('')
152af69d88dSmrg
153af69d88dSmrg        for f, index in functions:
15401e04c3fSmrg            print('#define %s_remap_index %u' % (f.name, index))
155af69d88dSmrg
15601e04c3fSmrg        print('')
157af69d88dSmrg
158af69d88dSmrg        for f, index in functions:
15901e04c3fSmrg            print('#define _gloffset_%s %s[%s_remap_index]' % (f.name, remap_table, f.name))
160af69d88dSmrg
16101e04c3fSmrg        print('')
162af69d88dSmrg
163af69d88dSmrg        for f, index in abi_functions + functions:
16401e04c3fSmrg            arg_string = gl_XML.create_parameter_string(f.parameters, 0)
16501e04c3fSmrg
16601e04c3fSmrg            print('typedef %s (GLAPIENTRYP _glptr_%s)(%s);' % (f.return_type, f.name, arg_string))
16701e04c3fSmrg            print('#define CALL_%s(disp, parameters) \\' % (f.name))
16801e04c3fSmrg            print('    (* GET_%s(disp)) parameters' % (f.name))
16901e04c3fSmrg            print('static inline _glptr_%s GET_%s(struct _glapi_table *disp) {' % (f.name, f.name))
17001e04c3fSmrg            print('   return (_glptr_%s) (GET_by_offset(disp, _gloffset_%s));' % (f.name, f.name))
17101e04c3fSmrg            print('}')
17201e04c3fSmrg            print()
17301e04c3fSmrg            print('static inline void SET_%s(struct _glapi_table *disp, %s (GLAPIENTRYP fn)(%s)) {' % (f.name, f.return_type, arg_string))
17401e04c3fSmrg            print('   SET_by_offset(disp, _gloffset_%s, fn);' % (f.name))
17501e04c3fSmrg            print('}')
17601e04c3fSmrg            print()
177af69d88dSmrg
178af69d88dSmrg        return
1793464ebd5Sriastradh
1803464ebd5Sriastradh
18101e04c3fSmrgdef _parser():
18201e04c3fSmrg    """Parse arguments and return a namespace."""
18301e04c3fSmrg    parser = argparse.ArgumentParser()
18401e04c3fSmrg    parser.add_argument('-f', '--filename',
18501e04c3fSmrg                        default='gl_API.xml',
18601e04c3fSmrg                        metavar="input_file_name",
18701e04c3fSmrg                        dest='file_name',
18801e04c3fSmrg                        help="Path to an XML description of OpenGL API.")
18901e04c3fSmrg    parser.add_argument('-m', '--mode',
19001e04c3fSmrg                        choices=['table', 'remap_table'],
19101e04c3fSmrg                        default='table',
19201e04c3fSmrg                        metavar="mode",
19301e04c3fSmrg                        help="Generate either a table or a remap_table")
19401e04c3fSmrg    return parser.parse_args()
19501e04c3fSmrg
19601e04c3fSmrg
19701e04c3fSmrgdef main():
19801e04c3fSmrg    """Main function."""
19901e04c3fSmrg    args = _parser()
20001e04c3fSmrg
20101e04c3fSmrg    api = gl_XML.parse_GL_API(args.file_name)
20201e04c3fSmrg
20301e04c3fSmrg    if args.mode == "table":
20401e04c3fSmrg        printer = PrintGlTable()
20501e04c3fSmrg    elif args.mode == "remap_table":
20601e04c3fSmrg        printer = PrintRemapTable()
20701e04c3fSmrg
20801e04c3fSmrg    printer.Print(api)
20901e04c3fSmrg
2103464ebd5Sriastradh
2113464ebd5Sriastradhif __name__ == '__main__':
21201e04c3fSmrg    main()
213