13464ebd5Sriastradh
23464ebd5Sriastradh# Copyright (C) 2009 Chia-I Wu <olv@0xlab.org>
33464ebd5Sriastradh# All Rights Reserved.
43464ebd5Sriastradh#
53464ebd5Sriastradh# This is based on extension_helper.py by Ian Romanick.
63464ebd5Sriastradh#
73464ebd5Sriastradh# Permission is hereby granted, free of charge, to any person obtaining a
83464ebd5Sriastradh# copy of this software and associated documentation files (the "Software"),
93464ebd5Sriastradh# to deal in the Software without restriction, including without limitation
103464ebd5Sriastradh# on the rights to use, copy, modify, merge, publish, distribute, sub
113464ebd5Sriastradh# license, and/or sell copies of the Software, and to permit persons to whom
123464ebd5Sriastradh# the Software is furnished to do so, subject to the following conditions:
133464ebd5Sriastradh#
143464ebd5Sriastradh# The above copyright notice and this permission notice (including the next
153464ebd5Sriastradh# paragraph) shall be included in all copies or substantial portions of the
163464ebd5Sriastradh# Software.
173464ebd5Sriastradh#
183464ebd5Sriastradh# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
193464ebd5Sriastradh# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
203464ebd5Sriastradh# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
213464ebd5Sriastradh# IBM AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
223464ebd5Sriastradh# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
233464ebd5Sriastradh# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
243464ebd5Sriastradh# IN THE SOFTWARE.
253464ebd5Sriastradh
2601e04c3fSmrgimport argparse
2701e04c3fSmrg
283464ebd5Sriastradhimport license
2901e04c3fSmrgimport gl_XML
3001e04c3fSmrg
313464ebd5Sriastradh
323464ebd5Sriastradhdef get_function_spec(func):
33af69d88dSmrg    sig = ""
34af69d88dSmrg    # derive parameter signature
35af69d88dSmrg    for p in func.parameterIterator():
36af69d88dSmrg        if p.is_padding:
37af69d88dSmrg            continue
38af69d88dSmrg        # FIXME: This is a *really* ugly hack. :(
39af69d88dSmrg        tn = p.type_expr.get_base_type_node()
40af69d88dSmrg        if p.is_pointer():
41af69d88dSmrg            sig += 'p'
42af69d88dSmrg        elif tn.integer:
43af69d88dSmrg            sig += 'i'
44af69d88dSmrg        elif tn.size == 4:
45af69d88dSmrg            sig += 'f'
46af69d88dSmrg        else:
47af69d88dSmrg            sig += 'd'
48af69d88dSmrg
49af69d88dSmrg    spec = [sig]
50af69d88dSmrg    for ent in func.entry_points:
51af69d88dSmrg        spec.append("gl" + ent)
52af69d88dSmrg
53af69d88dSmrg    # spec is terminated by an empty string
54af69d88dSmrg    spec.append('')
55af69d88dSmrg
56af69d88dSmrg    return spec
573464ebd5Sriastradh
5801e04c3fSmrg
593464ebd5Sriastradhclass PrintGlRemap(gl_XML.gl_print_base):
60af69d88dSmrg    def __init__(self):
61af69d88dSmrg        gl_XML.gl_print_base.__init__(self)
62af69d88dSmrg
63af69d88dSmrg        self.name = "remap_helper.py (from Mesa)"
64af69d88dSmrg        self.license = license.bsd_license_template % ("Copyright (C) 2009 Chia-I Wu <olv@0xlab.org>", "Chia-I Wu")
65af69d88dSmrg        return
66af69d88dSmrg
67af69d88dSmrg
68af69d88dSmrg    def printRealHeader(self):
6901e04c3fSmrg        print('#include "main/dispatch.h"')
7001e04c3fSmrg        print('#include "main/remap.h"')
7101e04c3fSmrg        print('')
72af69d88dSmrg        return
73af69d88dSmrg
74af69d88dSmrg
75af69d88dSmrg    def printBody(self, api):
76af69d88dSmrg        pool_indices = {}
77af69d88dSmrg
7801e04c3fSmrg        print('/* this is internal to remap.c */')
7901e04c3fSmrg        print('#ifndef need_MESA_remap_table')
8001e04c3fSmrg        print('#error Only remap.c should include this file!')
8101e04c3fSmrg        print('#endif /* need_MESA_remap_table */')
8201e04c3fSmrg        print('')
83af69d88dSmrg
8401e04c3fSmrg        print('')
8501e04c3fSmrg        print('static const char _mesa_function_pool[] =')
86af69d88dSmrg
87af69d88dSmrg        # output string pool
88af69d88dSmrg        index = 0;
89af69d88dSmrg        for f in api.functionIterateAll():
90af69d88dSmrg            pool_indices[f] = index
91af69d88dSmrg
92af69d88dSmrg            spec = get_function_spec(f)
93af69d88dSmrg
94af69d88dSmrg            # a function has either assigned offset, fixed offset,
95af69d88dSmrg            # or no offset
96af69d88dSmrg            if f.assign_offset:
97af69d88dSmrg                comments = "will be remapped"
98af69d88dSmrg            elif f.offset > 0:
99af69d88dSmrg                comments = "offset %d" % f.offset
100af69d88dSmrg            else:
101af69d88dSmrg                comments = "dynamic"
102af69d88dSmrg
10301e04c3fSmrg            print('   /* _mesa_function_pool[%d]: %s (%s) */' \
10401e04c3fSmrg                            % (index, f.name, comments))
105af69d88dSmrg            for line in spec:
10601e04c3fSmrg                print('   "%s\\0"' % line)
107af69d88dSmrg                index += len(line) + 1
10801e04c3fSmrg        print('   ;')
10901e04c3fSmrg        print('')
110af69d88dSmrg
11101e04c3fSmrg        print('/* these functions need to be remapped */')
11201e04c3fSmrg        print('static const struct gl_function_pool_remap MESA_remap_table_functions[] = {')
113af69d88dSmrg        # output all functions that need to be remapped
114af69d88dSmrg        # iterate by offsets so that they are sorted by remap indices
115af69d88dSmrg        for f in api.functionIterateByOffset():
116af69d88dSmrg            if not f.assign_offset:
117af69d88dSmrg                continue
11801e04c3fSmrg            print('   { %5d, %s_remap_index },' \
11901e04c3fSmrg                            % (pool_indices[f], f.name))
12001e04c3fSmrg        print('   {    -1, -1 }')
12101e04c3fSmrg        print('};')
12201e04c3fSmrg        print('')
123af69d88dSmrg        return
1243464ebd5Sriastradh
1253464ebd5Sriastradh
12601e04c3fSmrgdef _parser():
12701e04c3fSmrg    """Parse input options and return a namsepace."""
12801e04c3fSmrg    parser = argparse.ArgumentParser()
12901e04c3fSmrg    parser.add_argument('-f', '--filename',
13001e04c3fSmrg                        default="gl_API.xml",
13101e04c3fSmrg                        metavar="input_file_name",
13201e04c3fSmrg                        dest='file_name',
13301e04c3fSmrg                        help="An xml description file.")
13401e04c3fSmrg    return parser.parse_args()
1353464ebd5Sriastradh
1363464ebd5Sriastradh
13701e04c3fSmrgdef main():
13801e04c3fSmrg    """Main function."""
13901e04c3fSmrg    args = _parser()
1403464ebd5Sriastradh
14101e04c3fSmrg    api = gl_XML.parse_GL_API(args.file_name)
1423464ebd5Sriastradh
14301e04c3fSmrg    printer = PrintGlRemap()
14401e04c3fSmrg    printer.Print(api)
1453464ebd5Sriastradh
146af69d88dSmrg
14701e04c3fSmrgif __name__ == '__main__':
14801e04c3fSmrg    main()
149