1
2# Copyright (C) 2009 Chia-I Wu <olv@0xlab.org>
3# All Rights Reserved.
4#
5# This is based on extension_helper.py by Ian Romanick.
6#
7# Permission is hereby granted, free of charge, to any person obtaining a
8# copy of this software and associated documentation files (the "Software"),
9# to deal in the Software without restriction, including without limitation
10# on the rights to use, copy, modify, merge, publish, distribute, sub
11# license, and/or sell copies of the Software, and to permit persons to whom
12# the Software is furnished to do so, subject to the following conditions:
13#
14# The above copyright notice and this permission notice (including the next
15# paragraph) shall be included in all copies or substantial portions of the
16# Software.
17#
18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
21# IBM AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24# IN THE SOFTWARE.
25
26from __future__ import print_function
27
28import argparse
29
30import license
31import gl_XML
32
33
34def get_function_spec(func):
35    sig = ""
36    # derive parameter signature
37    for p in func.parameterIterator():
38        if p.is_padding:
39            continue
40        # FIXME: This is a *really* ugly hack. :(
41        tn = p.type_expr.get_base_type_node()
42        if p.is_pointer():
43            sig += 'p'
44        elif tn.integer:
45            sig += 'i'
46        elif tn.size == 4:
47            sig += 'f'
48        else:
49            sig += 'd'
50
51    spec = [sig]
52    for ent in func.entry_points:
53        spec.append("gl" + ent)
54
55    # spec is terminated by an empty string
56    spec.append('')
57
58    return spec
59
60
61class PrintGlRemap(gl_XML.gl_print_base):
62    def __init__(self):
63        gl_XML.gl_print_base.__init__(self)
64
65        self.name = "remap_helper.py (from Mesa)"
66        self.license = license.bsd_license_template % ("Copyright (C) 2009 Chia-I Wu <olv@0xlab.org>", "Chia-I Wu")
67        return
68
69
70    def printRealHeader(self):
71        print('#include "main/dispatch.h"')
72        print('#include "main/remap.h"')
73        print('')
74        return
75
76
77    def printBody(self, api):
78        pool_indices = {}
79
80        print('/* this is internal to remap.c */')
81        print('#ifndef need_MESA_remap_table')
82        print('#error Only remap.c should include this file!')
83        print('#endif /* need_MESA_remap_table */')
84        print('')
85
86        print('')
87        print('static const char _mesa_function_pool[] =')
88
89        # output string pool
90        index = 0;
91        for f in api.functionIterateAll():
92            pool_indices[f] = index
93
94            spec = get_function_spec(f)
95
96            # a function has either assigned offset, fixed offset,
97            # or no offset
98            if f.assign_offset:
99                comments = "will be remapped"
100            elif f.offset > 0:
101                comments = "offset %d" % f.offset
102            else:
103                comments = "dynamic"
104
105            print('   /* _mesa_function_pool[%d]: %s (%s) */' \
106                            % (index, f.name, comments))
107            for line in spec:
108                print('   "%s\\0"' % line)
109                index += len(line) + 1
110        print('   ;')
111        print('')
112
113        print('/* these functions need to be remapped */')
114        print('static const struct gl_function_pool_remap MESA_remap_table_functions[] = {')
115        # output all functions that need to be remapped
116        # iterate by offsets so that they are sorted by remap indices
117        for f in api.functionIterateByOffset():
118            if not f.assign_offset:
119                continue
120            print('   { %5d, %s_remap_index },' \
121                            % (pool_indices[f], f.name))
122        print('   {    -1, -1 }')
123        print('};')
124        print('')
125        return
126
127
128def _parser():
129    """Parse input options and return a namsepace."""
130    parser = argparse.ArgumentParser()
131    parser.add_argument('-f', '--filename',
132                        default="gl_API.xml",
133                        metavar="input_file_name",
134                        dest='file_name',
135                        help="An xml description file.")
136    return parser.parse_args()
137
138
139def main():
140    """Main function."""
141    args = _parser()
142
143    api = gl_XML.parse_GL_API(args.file_name)
144
145    printer = PrintGlRemap()
146    printer.Print(api)
147
148
149if __name__ == '__main__':
150    main()
151