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
26import argparse
27
28import license
29import gl_XML
30
31
32def get_function_spec(func):
33    sig = ""
34    # derive parameter signature
35    for p in func.parameterIterator():
36        if p.is_padding:
37            continue
38        # FIXME: This is a *really* ugly hack. :(
39        tn = p.type_expr.get_base_type_node()
40        if p.is_pointer():
41            sig += 'p'
42        elif tn.integer:
43            sig += 'i'
44        elif tn.size == 4:
45            sig += 'f'
46        else:
47            sig += 'd'
48
49    spec = [sig]
50    for ent in func.entry_points:
51        spec.append("gl" + ent)
52
53    # spec is terminated by an empty string
54    spec.append('')
55
56    return spec
57
58
59class PrintGlRemap(gl_XML.gl_print_base):
60    def __init__(self):
61        gl_XML.gl_print_base.__init__(self)
62
63        self.name = "remap_helper.py (from Mesa)"
64        self.license = license.bsd_license_template % ("Copyright (C) 2009 Chia-I Wu <olv@0xlab.org>", "Chia-I Wu")
65        return
66
67
68    def printRealHeader(self):
69        print '#include "main/dispatch.h"'
70        print '#include "main/remap.h"'
71        print ''
72        return
73
74
75    def printBody(self, api):
76        pool_indices = {}
77
78        print '/* this is internal to remap.c */'
79        print '#ifndef need_MESA_remap_table'
80        print '#error Only remap.c should include this file!'
81        print '#endif /* need_MESA_remap_table */'
82        print ''
83
84        print ''
85        print 'static const char _mesa_function_pool[] ='
86
87        # output string pool
88        index = 0;
89        for f in api.functionIterateAll():
90            pool_indices[f] = index
91
92            spec = get_function_spec(f)
93
94            # a function has either assigned offset, fixed offset,
95            # or no offset
96            if f.assign_offset:
97                comments = "will be remapped"
98            elif f.offset > 0:
99                comments = "offset %d" % f.offset
100            else:
101                comments = "dynamic"
102
103            print '   /* _mesa_function_pool[%d]: %s (%s) */' \
104                            % (index, f.name, comments)
105            for line in spec:
106                print '   "%s\\0"' % line
107                index += len(line) + 1
108        print '   ;'
109        print ''
110
111        print '/* these functions need to be remapped */'
112        print 'static const struct gl_function_pool_remap MESA_remap_table_functions[] = {'
113        # output all functions that need to be remapped
114        # iterate by offsets so that they are sorted by remap indices
115        for f in api.functionIterateByOffset():
116            if not f.assign_offset:
117                continue
118            print '   { %5d, %s_remap_index },' \
119                            % (pool_indices[f], f.name)
120        print '   {    -1, -1 }'
121        print '};'
122        print ''
123        return
124
125
126def _parser():
127    """Parse input options and return a namsepace."""
128    parser = argparse.ArgumentParser()
129    parser.add_argument('-f', '--filename',
130                        default="gl_API.xml",
131                        metavar="input_file_name",
132                        dest='file_name',
133                        help="An xml description file.")
134    return parser.parse_args()
135
136
137def main():
138    """Main function."""
139    args = _parser()
140
141    api = gl_XML.parse_GL_API(args.file_name)
142
143    printer = PrintGlRemap()
144    printer.Print(api)
145
146
147if __name__ == '__main__':
148    main()
149