3 Generates dumpers for the i965 state strucutures using pygccxml.
7 PYTHONPATH=/path/to/pygccxml-1.0.0 python brw_structs_dump.py
9 Jose Fonseca <jfonseca@vmware.com>
13 /**************************************************************************
15 * Copyright 2009 VMware, Inc.
16 * All Rights Reserved.
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:
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.
34 * The above copyright notice and this permission notice (including the
35 * next paragraph) shall be included in all copies or substantial portions
38 **************************************************************************/
45 from pygccxml
import parser
46 from pygccxml
import declarations
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
57 def vars_filter(variable
):
59 return not re
.match('^pad\d*', name
) and name
!= 'dword'
62 class decl_dumper_t(decl_visitor
.decl_visitor_t
):
64 def __init__(self
, stream
, instance
= '', decl
= None):
65 decl_visitor
.decl_visitor_t
.__init
__(self
)
67 self
._instance
= instance
71 return decl_dumper_t(self
.stream
, self
._instance
, self
.decl
)
73 def visit_class(self
):
75 assert self
.decl
.class_type
in ('struct', 'union')
77 for variable
in class_
.variables(recursive
= False):
78 if vars_filter(variable
):
79 dump_type(self
.stream
, self
._instance
+ '.' + variable
.name
, variable
.type)
81 def visit_enumeration(self
):
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')
93 self
.stream
.write(' debug_printf("\\t\\t%s = %%i\\n", %s);\n' % (self
._instance
, "(*ptr)" + self
._instance
))
96 def dump_decl(stream
, instance
, decl
):
97 dumper
= decl_dumper_t(stream
, instance
, decl
)
98 algorithm
.apply_visitor(dumper
, decl
)
101 class type_dumper_t(type_visitor
.type_visitor_t
):
103 def __init__(self
, stream
, instance
, type_
):
104 type_visitor
.type_visitor_t
.__init
__(self
)
106 self
.instance
= instance
110 return type_dumper_t(self
.instance
, self
.type)
112 def visit_bool(self
):
113 self
.print_instance('%i')
115 def visit_char(self
):
116 #self.print_instance('%i')
117 self
.print_instance('0x%x')
119 def visit_unsigned_char(self
):
120 #self.print_instance('%u')
121 self
.print_instance('0x%x')
123 def visit_signed_char(self
):
124 #self.print_instance('%i')
125 self
.print_instance('0x%x')
127 def visit_wchar(self
):
128 self
.print_instance('0x%x')
130 def visit_short_int(self
):
131 #self.print_instance('%i')
132 self
.print_instance('0x%x')
134 def visit_short_unsigned_int(self
):
135 #self.print_instance('%u')
136 self
.print_instance('0x%x')
139 #self.print_instance('%i')
140 self
.print_instance('0x%x')
142 def visit_unsigned_int(self
):
143 #self.print_instance('%u')
144 self
.print_instance('0x%x')
146 def visit_long_int(self
):
147 #self.print_instance('%li')
148 self
.print_instance('0x%lx')
150 def visit_long_unsigned_int(self
):
151 #self.print_instance('%lu')
152 self
.print_instance('%0xlx')
154 def visit_long_long_int(self
):
155 #self.print_instance('%lli')
156 self
.print_instance('%0xllx')
158 def visit_long_long_unsigned_int(self
):
159 #self.print_instance('%llu')
160 self
.print_instance('0x%llx')
162 def visit_float(self
):
163 self
.print_instance('%f')
165 def visit_double(self
):
166 self
.print_instance('%f')
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))
172 def visit_pointer(self
):
173 self
.print_instance('%p')
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
)
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
))
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_
)
191 def dump_struct_interface(stream
, class_
, suffix
= ';'):
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
))
199 def dump_struct_implementation(stream
, decls
, class_
):
200 dump_struct_interface(stream
, class_
, suffix
= '')
202 dump_decl(stream
, '', class_
)
207 def dump_header(stream
):
208 stream
.write(copyright
.strip() + '\n')
210 stream
.write('/**\n')
211 stream
.write(' * @file\n')
212 stream
.write(' * Dump i965 data structures.\n')
214 stream
.write(' * Generated automatically from brw_structs.h by brw_structs_dump.py.\n')
215 stream
.write(' */\n')
219 def dump_interfaces(decls
, global_ns
, names
):
220 stream
= open('brw_structs_dump.h', 'wt')
224 stream
.write('#ifndef BRW_STRUCTS_DUMP_H\n')
225 stream
.write('#define BRW_STRUCTS_DUMP_H\n')
229 stream
.write('struct %s;\n' % (name
,))
233 (class_
,) = global_ns
.classes(name
= name
)
234 dump_struct_interface(stream
, class_
)
238 stream
.write('#endif /* BRW_STRUCTS_DUMP_H */\n')
241 def dump_implementations(decls
, global_ns
, names
):
242 stream
= open('brw_structs_dump.c', 'wt')
246 stream
.write('#include "util/u_debug.h"\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')
254 (class_
,) = global_ns
.classes(name
= name
)
255 dump_struct_implementation(stream
, decls
, class_
)
258 def decl_filter(decl
):
259 '''Filter the declarations we're interested in'''
261 return name
.startswith('brw_') and name
not in ('brw_instruction',)
266 config
= parser
.config_t(
278 decls
= parser
.parse(headers
, config
, parser
.COMPILATION_MODE
.ALL_AT_ONCE
)
279 global_ns
= declarations
.get_global_namespace(decls
)
282 for class_
in global_ns
.classes(decl_filter
):
283 names
.append(class_
.name
)
286 dump_interfaces(decls
, global_ns
, names
)
287 dump_implementations(decls
, global_ns
, names
)
290 if __name__
== '__main__':