Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / gallium / drivers / i965 / brw_structs_dump.py
1 #!/usr/bin/env python
2 '''
3 Generates dumpers for the i965 state strucutures using pygccxml.
4
5 Run as 
6
7   PYTHONPATH=/path/to/pygccxml-1.0.0 python brw_structs_dump.py
8
9 Jose Fonseca <jfonseca@vmware.com>
10 '''
11
12 copyright = '''
13 /**************************************************************************
14  *
15  * Copyright 2009 VMware, Inc.
16  * All Rights Reserved.
17  *
18  * Permission is hereby granted, free of charge, to any person obtaining a
19  * copy of this software and associated documentation files (the
20  * "Software"), to deal in the Software without restriction, including
21  * without limitation the rights to use, copy, modify, merge, publish,
22  * distribute, sub license, and/or sell copies of the Software, and to
23  * permit persons to whom the Software is furnished to do so, subject to
24  * the following conditions:
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
29  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
30  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
31  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
32  * USE OR OTHER DEALINGS IN THE SOFTWARE.
33  *
34  * The above copyright notice and this permission notice (including the
35  * next paragraph) shall be included in all copies or substantial portions
36  * of the Software.
37  *
38  **************************************************************************/
39  '''
40
41 import os
42 import sys
43 import re
44
45 from pygccxml import parser
46 from pygccxml import declarations
47
48 from pygccxml.declarations import algorithm
49 from pygccxml.declarations import decl_visitor
50 from pygccxml.declarations import type_traits
51 from pygccxml.declarations import type_visitor
52
53
54 enums = True
55
56
57 def vars_filter(variable):
58     name = variable.name
59     return not re.match('^pad\d*', name) and name != 'dword' 
60
61
62 class decl_dumper_t(decl_visitor.decl_visitor_t):
63
64     def __init__(self, stream, instance = '', decl = None):
65         decl_visitor.decl_visitor_t.__init__(self)
66         self.stream = stream
67         self._instance = instance
68         self.decl = decl
69
70     def clone(self):
71         return decl_dumper_t(self.stream, self._instance, self.decl)
72
73     def visit_class(self):
74         class_ = self.decl
75         assert self.decl.class_type in ('struct', 'union')
76
77         for variable in class_.variables(recursive = False):
78             if vars_filter(variable):
79                 dump_type(self.stream, self._instance + '.' + variable.name, variable.type)
80
81     def visit_enumeration(self):
82         if enums:
83             self.stream.write('   switch(%s) {\n' % ("(*ptr)" + self._instance,))
84             for name, value in self.decl.values:
85                 self.stream.write('   case %s:\n' % (name,))
86                 self.stream.write('      debug_printf("\\t\\t%s = %s\\n");\n' % (self._instance, name))
87                 self.stream.write('      break;\n')
88             self.stream.write('   default:\n')
89             self.stream.write('      debug_printf("\\t\\t%s = %%i\\n", %s);\n' % (self._instance, "(*ptr)" + self._instance))
90             self.stream.write('      break;\n')
91             self.stream.write('   }\n')
92         else:
93             self.stream.write('   debug_printf("\\t\\t%s = %%i\\n", %s);\n' % (self._instance, "(*ptr)" + self._instance))
94
95
96 def dump_decl(stream, instance, decl):
97     dumper = decl_dumper_t(stream, instance, decl)
98     algorithm.apply_visitor(dumper, decl)
99
100
101 class type_dumper_t(type_visitor.type_visitor_t):
102
103     def __init__(self, stream, instance, type_):
104         type_visitor.type_visitor_t.__init__(self)
105         self.stream = stream
106         self.instance = instance
107         self.type = type_
108
109     def clone(self):
110         return type_dumper_t(self.instance, self.type)
111
112     def visit_bool(self):
113         self.print_instance('%i')
114         
115     def visit_char(self):
116         #self.print_instance('%i')
117         self.print_instance('0x%x')
118         
119     def visit_unsigned_char(self):
120         #self.print_instance('%u')
121         self.print_instance('0x%x')
122
123     def visit_signed_char(self):
124         #self.print_instance('%i')
125         self.print_instance('0x%x')
126     
127     def visit_wchar(self):
128         self.print_instance('0x%x')
129         
130     def visit_short_int(self):
131         #self.print_instance('%i')
132         self.print_instance('0x%x')
133         
134     def visit_short_unsigned_int(self):
135         #self.print_instance('%u')
136         self.print_instance('0x%x')
137         
138     def visit_int(self):
139         #self.print_instance('%i')
140         self.print_instance('0x%x')
141         
142     def visit_unsigned_int(self):
143         #self.print_instance('%u')
144         self.print_instance('0x%x')
145         
146     def visit_long_int(self):
147         #self.print_instance('%li')
148         self.print_instance('0x%lx')
149         
150     def visit_long_unsigned_int(self):
151         #self.print_instance('%lu')
152         self.print_instance('%0xlx')
153         
154     def visit_long_long_int(self):
155         #self.print_instance('%lli')
156         self.print_instance('%0xllx')
157         
158     def visit_long_long_unsigned_int(self):
159         #self.print_instance('%llu')
160         self.print_instance('0x%llx')
161         
162     def visit_float(self):
163         self.print_instance('%f')
164         
165     def visit_double(self):
166         self.print_instance('%f')
167         
168     def visit_array(self):
169         for i in range(type_traits.array_size(self.type)):
170             dump_type(self.stream, self.instance + '[%i]' % i, type_traits.base_type(self.type))
171
172     def visit_pointer(self):
173         self.print_instance('%p')
174
175     def visit_declarated(self):
176         #stream.write('decl = %r\n' % self.type.decl_string)
177         decl = type_traits.remove_declarated(self.type)
178         dump_decl(self.stream, self.instance, decl)
179
180     def print_instance(self, format):
181         self.stream.write('   debug_printf("\\t\\t%s = %s\\n", %s);\n' % (self.instance, format, "(*ptr)" + self.instance))
182
183
184
185 def dump_type(stream, instance, type_):
186     type_ = type_traits.remove_alias(type_)
187     visitor = type_dumper_t(stream, instance, type_)
188     algorithm.apply_visitor(visitor, type_)
189
190
191 def dump_struct_interface(stream, class_, suffix = ';'):
192     name = class_.name
193     assert name.startswith('brw_');
194     name = name[:4] + 'dump_' + name[4:]
195     stream.write('void\n')
196     stream.write('%s(const struct %s *ptr)%s\n' % (name, class_.name, suffix))
197
198
199 def dump_struct_implementation(stream, decls, class_):
200     dump_struct_interface(stream, class_, suffix = '')
201     stream.write('{\n')
202     dump_decl(stream, '', class_)
203     stream.write('}\n')
204     stream.write('\n')
205
206
207 def dump_header(stream):
208     stream.write(copyright.strip() + '\n')
209     stream.write('\n')
210     stream.write('/**\n')
211     stream.write(' * @file\n')
212     stream.write(' * Dump i965 data structures.\n')
213     stream.write(' *\n')
214     stream.write(' * Generated automatically from brw_structs.h by brw_structs_dump.py.\n')
215     stream.write(' */\n')
216     stream.write('\n')
217
218
219 def dump_interfaces(decls, global_ns, names):
220     stream = open('brw_structs_dump.h', 'wt')
221     
222     dump_header(stream)
223     
224     stream.write('#ifndef BRW_STRUCTS_DUMP_H\n')
225     stream.write('#define BRW_STRUCTS_DUMP_H\n')
226     stream.write('\n')
227     
228     for name in names:
229         stream.write('struct %s;\n' % (name,))
230     stream.write('\n')
231
232     for name in names:
233         (class_,) = global_ns.classes(name = name)
234         dump_struct_interface(stream, class_)
235         stream.write('\n')
236     stream.write('\n')
237
238     stream.write('#endif /* BRW_STRUCTS_DUMP_H */\n')
239
240
241 def dump_implementations(decls, global_ns, names):
242     stream = open('brw_structs_dump.c', 'wt')
243     
244     dump_header(stream)
245
246     stream.write('#include "util/u_debug.h"\n')
247     stream.write('\n')
248     stream.write('#include "brw_types.h"\n')
249     stream.write('#include "brw_structs.h"\n')
250     stream.write('#include "brw_structs_dump.h"\n')
251     stream.write('\n')
252
253     for name in names:
254         (class_,) = global_ns.classes(name = name)
255         dump_struct_implementation(stream, decls, class_)
256
257
258 def decl_filter(decl):
259     '''Filter the declarations we're interested in'''
260     name = decl.name
261     return name.startswith('brw_') and name not in ('brw_instruction',) 
262
263
264 def main():
265
266     config = parser.config_t(
267         include_paths = [
268             '../../include',
269         ],
270         compiler = 'gcc',
271     )
272
273     headers = [
274         'brw_types.h', 
275         'brw_structs.h', 
276     ]
277
278     decls = parser.parse(headers, config, parser.COMPILATION_MODE.ALL_AT_ONCE)
279     global_ns = declarations.get_global_namespace(decls)
280
281     names = []
282     for class_ in global_ns.classes(decl_filter):
283         names.append(class_.name)
284     names.sort()
285
286     dump_interfaces(decls, global_ns, names)
287     dump_implementations(decls, global_ns, names)
288
289
290 if __name__ == '__main__':
291     main()