1848b8605Smrg
2848b8605Smrg# Copyright (C) 2009 Chia-I Wu <olv@0xlab.org>
3848b8605Smrg# All Rights Reserved.
4848b8605Smrg#
5848b8605Smrg# This is based on extension_helper.py by Ian Romanick.
6848b8605Smrg#
7848b8605Smrg# Permission is hereby granted, free of charge, to any person obtaining a
8848b8605Smrg# copy of this software and associated documentation files (the "Software"),
9848b8605Smrg# to deal in the Software without restriction, including without limitation
10848b8605Smrg# on the rights to use, copy, modify, merge, publish, distribute, sub
11848b8605Smrg# license, and/or sell copies of the Software, and to permit persons to whom
12848b8605Smrg# the Software is furnished to do so, subject to the following conditions:
13848b8605Smrg#
14848b8605Smrg# The above copyright notice and this permission notice (including the next
15848b8605Smrg# paragraph) shall be included in all copies or substantial portions of the
16848b8605Smrg# Software.
17848b8605Smrg#
18848b8605Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19848b8605Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20848b8605Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
21848b8605Smrg# IBM AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22848b8605Smrg# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23848b8605Smrg# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24848b8605Smrg# IN THE SOFTWARE.
25848b8605Smrg
26b8e80941Smrgfrom __future__ import print_function
27b8e80941Smrg
28b8e80941Smrgimport argparse
29b8e80941Smrg
30848b8605Smrgimport license
31b8e80941Smrgimport gl_XML
32b8e80941Smrg
33848b8605Smrg
34848b8605Smrgdef get_function_spec(func):
35848b8605Smrg    sig = ""
36848b8605Smrg    # derive parameter signature
37848b8605Smrg    for p in func.parameterIterator():
38848b8605Smrg        if p.is_padding:
39848b8605Smrg            continue
40848b8605Smrg        # FIXME: This is a *really* ugly hack. :(
41848b8605Smrg        tn = p.type_expr.get_base_type_node()
42848b8605Smrg        if p.is_pointer():
43848b8605Smrg            sig += 'p'
44848b8605Smrg        elif tn.integer:
45848b8605Smrg            sig += 'i'
46848b8605Smrg        elif tn.size == 4:
47848b8605Smrg            sig += 'f'
48848b8605Smrg        else:
49848b8605Smrg            sig += 'd'
50848b8605Smrg
51848b8605Smrg    spec = [sig]
52848b8605Smrg    for ent in func.entry_points:
53848b8605Smrg        spec.append("gl" + ent)
54848b8605Smrg
55848b8605Smrg    # spec is terminated by an empty string
56848b8605Smrg    spec.append('')
57848b8605Smrg
58848b8605Smrg    return spec
59848b8605Smrg
60b8e80941Smrg
61848b8605Smrgclass PrintGlRemap(gl_XML.gl_print_base):
62848b8605Smrg    def __init__(self):
63848b8605Smrg        gl_XML.gl_print_base.__init__(self)
64848b8605Smrg
65848b8605Smrg        self.name = "remap_helper.py (from Mesa)"
66848b8605Smrg        self.license = license.bsd_license_template % ("Copyright (C) 2009 Chia-I Wu <olv@0xlab.org>", "Chia-I Wu")
67848b8605Smrg        return
68848b8605Smrg
69848b8605Smrg
70848b8605Smrg    def printRealHeader(self):
71b8e80941Smrg        print('#include "main/dispatch.h"')
72b8e80941Smrg        print('#include "main/remap.h"')
73b8e80941Smrg        print('')
74848b8605Smrg        return
75848b8605Smrg
76848b8605Smrg
77848b8605Smrg    def printBody(self, api):
78848b8605Smrg        pool_indices = {}
79848b8605Smrg
80b8e80941Smrg        print('/* this is internal to remap.c */')
81b8e80941Smrg        print('#ifndef need_MESA_remap_table')
82b8e80941Smrg        print('#error Only remap.c should include this file!')
83b8e80941Smrg        print('#endif /* need_MESA_remap_table */')
84b8e80941Smrg        print('')
85848b8605Smrg
86b8e80941Smrg        print('')
87b8e80941Smrg        print('static const char _mesa_function_pool[] =')
88848b8605Smrg
89848b8605Smrg        # output string pool
90848b8605Smrg        index = 0;
91848b8605Smrg        for f in api.functionIterateAll():
92848b8605Smrg            pool_indices[f] = index
93848b8605Smrg
94848b8605Smrg            spec = get_function_spec(f)
95848b8605Smrg
96848b8605Smrg            # a function has either assigned offset, fixed offset,
97848b8605Smrg            # or no offset
98848b8605Smrg            if f.assign_offset:
99848b8605Smrg                comments = "will be remapped"
100848b8605Smrg            elif f.offset > 0:
101848b8605Smrg                comments = "offset %d" % f.offset
102848b8605Smrg            else:
103848b8605Smrg                comments = "dynamic"
104848b8605Smrg
105b8e80941Smrg            print('   /* _mesa_function_pool[%d]: %s (%s) */' \
106b8e80941Smrg                            % (index, f.name, comments))
107848b8605Smrg            for line in spec:
108b8e80941Smrg                print('   "%s\\0"' % line)
109848b8605Smrg                index += len(line) + 1
110b8e80941Smrg        print('   ;')
111b8e80941Smrg        print('')
112848b8605Smrg
113b8e80941Smrg        print('/* these functions need to be remapped */')
114b8e80941Smrg        print('static const struct gl_function_pool_remap MESA_remap_table_functions[] = {')
115848b8605Smrg        # output all functions that need to be remapped
116848b8605Smrg        # iterate by offsets so that they are sorted by remap indices
117848b8605Smrg        for f in api.functionIterateByOffset():
118848b8605Smrg            if not f.assign_offset:
119848b8605Smrg                continue
120b8e80941Smrg            print('   { %5d, %s_remap_index },' \
121b8e80941Smrg                            % (pool_indices[f], f.name))
122b8e80941Smrg        print('   {    -1, -1 }')
123b8e80941Smrg        print('};')
124b8e80941Smrg        print('')
125848b8605Smrg        return
126848b8605Smrg
127848b8605Smrg
128b8e80941Smrgdef _parser():
129b8e80941Smrg    """Parse input options and return a namsepace."""
130b8e80941Smrg    parser = argparse.ArgumentParser()
131b8e80941Smrg    parser.add_argument('-f', '--filename',
132b8e80941Smrg                        default="gl_API.xml",
133b8e80941Smrg                        metavar="input_file_name",
134b8e80941Smrg                        dest='file_name',
135b8e80941Smrg                        help="An xml description file.")
136b8e80941Smrg    return parser.parse_args()
137848b8605Smrg
138848b8605Smrg
139b8e80941Smrgdef main():
140b8e80941Smrg    """Main function."""
141b8e80941Smrg    args = _parser()
142848b8605Smrg
143b8e80941Smrg    api = gl_XML.parse_GL_API(args.file_name)
144848b8605Smrg
145b8e80941Smrg    printer = PrintGlRemap()
146b8e80941Smrg    printer.Print(api)
147848b8605Smrg
148848b8605Smrg
149b8e80941Smrgif __name__ == '__main__':
150b8e80941Smrg    main()
151