summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/i965/brw_structs_dump.py
blob: 6dba49ad9114d54cd48180544d6eb760b6d635fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/env python
'''
Generates dumpers for the i965 state strucutures using pygccxml.

Run as 

  PYTHONPATH=/path/to/pygccxml-1.0.0 python brw_structs_dump.py

Jose Fonseca <jfonseca@vmware.com>
'''

copyright = '''
/**************************************************************************
 *
 * Copyright 2009 VMware, Inc.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 **************************************************************************/
 '''

import os
import sys
import re

from pygccxml import parser
from pygccxml import declarations

from pygccxml.declarations import algorithm
from pygccxml.declarations import decl_visitor
from pygccxml.declarations import type_traits
from pygccxml.declarations import type_visitor


enums = True


def vars_filter(variable):
    name = variable.name
    return not re.match('^pad\d*', name) and name != 'dword' 


class decl_dumper_t(decl_visitor.decl_visitor_t):

    def __init__(self, stream, instance = '', decl = None):
        decl_visitor.decl_visitor_t.__init__(self)
        self.stream = stream
        self._instance = instance
        self.decl = decl

    def clone(self):
        return decl_dumper_t(self.stream, self._instance, self.decl)

    def visit_class(self):
        class_ = self.decl
        assert self.decl.class_type in ('struct', 'union')

        for variable in class_.variables(recursive = False):
            if vars_filter(variable):
                dump_type(self.stream, self._instance + '.' + variable.name, variable.type)

    def visit_enumeration(self):
        if enums:
            self.stream.write('   switch(%s) {\n' % ("(*ptr)" + self._instance,))
            for name, value in self.decl.values:
                self.stream.write('   case %s:\n' % (name,))
                self.stream.write('      debug_printf("\\t\\t%s = %s\\n");\n' % (self._instance, name))
                self.stream.write('      break;\n')
            self.stream.write('   default:\n')
            self.stream.write('      debug_printf("\\t\\t%s = %%i\\n", %s);\n' % (self._instance, "(*ptr)" + self._instance))
            self.stream.write('      break;\n')
            self.stream.write('   }\n')
        else:
            self.stream.write('   debug_printf("\\t\\t%s = %%i\\n", %s);\n' % (self._instance, "(*ptr)" + self._instance))


def dump_decl(stream, instance, decl):
    dumper = decl_dumper_t(stream, instance, decl)
    algorithm.apply_visitor(dumper, decl)


class type_dumper_t(type_visitor.type_visitor_t):

    def __init__(self, stream, instance, type_):
        type_visitor.type_visitor_t.__init__(self)
        self.stream = stream
        self.instance = instance
        self.type = type_

    def clone(self):
        return type_dumper_t(self.instance, self.type)

    def visit_bool(self):
        self.print_instance('%i')
        
    def visit_char(self):
        #self.print_instance('%i')
        self.print_instance('0x%x')
        
    def visit_unsigned_char(self):
        #self.print_instance('%u')
        self.print_instance('0x%x')

    def visit_signed_char(self):
        #self.print_instance('%i')
        self.print_instance('0x%x')
    
    def visit_wchar(self):
        self.print_instance('0x%x')
        
    def visit_short_int(self):
        #self.print_instance('%i')
        self.print_instance('0x%x')
        
    def visit_short_unsigned_int(self):
        #self.print_instance('%u')
        self.print_instance('0x%x')
        
    def visit_int(self):
        #self.print_instance('%i')
        self.print_instance('0x%x')
        
    def visit_unsigned_int(self):
        #self.print_instance('%u')
        self.print_instance('0x%x')
        
    def visit_long_int(self):
        #self.print_instance('%li')
        self.print_instance('0x%lx')
        
    def visit_long_unsigned_int(self):
        #self.print_instance('%lu')
        self.print_instance('%0xlx')
        
    def visit_long_long_int(self):
        #self.print_instance('%lli')
        self.print_instance('%0xllx')
        
    def visit_long_long_unsigned_int(self):
        #self.print_instance('%llu')
        self.print_instance('0x%llx')
        
    def visit_float(self):
        self.print_instance('%f')
        
    def visit_double(self):
        self.print_instance('%f')
        
    def visit_array(self):
        for i in range(type_traits.array_size(self.type)):
            dump_type(self.stream, self.instance + '[%i]' % i, type_traits.base_type(self.type))

    def visit_pointer(self):
        self.print_instance('%p')

    def visit_declarated(self):
        #stream.write('decl = %r\n' % self.type.decl_string)
        decl = type_traits.remove_declarated(self.type)
        dump_decl(self.stream, self.instance, decl)

    def print_instance(self, format):
        self.stream.write('   debug_printf("\\t\\t%s = %s\\n", %s);\n' % (self.instance, format, "(*ptr)" + self.instance))



def dump_type(stream, instance, type_):
    type_ = type_traits.remove_alias(type_)
    visitor = type_dumper_t(stream, instance, type_)
    algorithm.apply_visitor(visitor, type_)


def dump_struct_interface(stream, class_, suffix = ';'):
    name = class_.name
    assert name.startswith('brw_');
    name = name[:4] + 'dump_' + name[4:]
    stream.write('void\n')
    stream.write('%s(const struct %s *ptr)%s\n' % (name, class_.name, suffix))


def dump_struct_implementation(stream, decls, class_):
    dump_struct_interface(stream, class_, suffix = '')
    stream.write('{\n')
    dump_decl(stream, '', class_)
    stream.write('}\n')
    stream.write('\n')


def dump_header(stream):
    stream.write(copyright.strip() + '\n')
    stream.write('\n')
    stream.write('/**\n')
    stream.write(' * @file\n')
    stream.write(' * Dump i965 data structures.\n')
    stream.write(' *\n')
    stream.write(' * Generated automatically from brw_structs.h by brw_structs_dump.py.\n')
    stream.write(' */\n')
    stream.write('\n')


def dump_interfaces(decls, global_ns, names):
    stream = open('brw_structs_dump.h', 'wt')
    
    dump_header(stream)
    
    stream.write('#ifndef BRW_STRUCTS_DUMP_H\n')
    stream.write('#define BRW_STRUCTS_DUMP_H\n')
    stream.write('\n')
    
    for name in names:
        stream.write('struct %s;\n' % (name,))
    stream.write('\n')

    for name in names:
        (class_,) = global_ns.classes(name = name)
        dump_struct_interface(stream, class_)
        stream.write('\n')
    stream.write('\n')

    stream.write('#endif /* BRW_STRUCTS_DUMP_H */\n')


def dump_implementations(decls, global_ns, names):
    stream = open('brw_structs_dump.c', 'wt')
    
    dump_header(stream)

    stream.write('#include "util/u_debug.h"\n')
    stream.write('\n')
    stream.write('#include "brw_types.h"\n')
    stream.write('#include "brw_structs.h"\n')
    stream.write('#include "brw_structs_dump.h"\n')
    stream.write('\n')

    for name in names:
        (class_,) = global_ns.classes(name = name)
        dump_struct_implementation(stream, decls, class_)


def decl_filter(decl):
    '''Filter the declarations we're interested in'''
    name = decl.name
    return name.startswith('brw_') and name not in ('brw_instruction',) 


def main():

    config = parser.config_t(
        include_paths = [
            '../../include',
        ],
        compiler = 'gcc',
    )

    headers = [
        'brw_types.h', 
        'brw_structs.h', 
    ]

    decls = parser.parse(headers, config, parser.COMPILATION_MODE.ALL_AT_ONCE)
    global_ns = declarations.get_global_namespace(decls)

    names = []
    for class_ in global_ns.classes(decl_filter):
        names.append(class_.name)
    names.sort()

    dump_interfaces(decls, global_ns, names)
    dump_implementations(decls, global_ns, names)


if __name__ == '__main__':
    main()