1 ##########################################################################
3 # Copyright 2010 VMware, Inc.
6 # Permission is hereby granted, free of charge, to any person obtaining a copy
7 # of this software and associated documentation files (the "Software"), to deal
8 # in the Software without restriction, including without limitation the rights
9 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 # copies of the Software, and to permit persons to whom the Software is
11 # furnished to do so, subject to the following conditions:
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 ##########################################################################/
27 """Generic retracing code generator."""
34 class ConstRemover(stdapi.Rebuilder):
36 def visit_const(self, const):
39 def visit_opaque(self, opaque):
43 def handle_entry(handle, value):
44 if handle.key is None:
45 return "__%s_map[%s]" % (handle.name, value)
47 key_name, key_type = handle.key
48 return "__%s_map[%s][%s]" % (handle.name, key_name, value)
51 class ValueExtractor(stdapi.Visitor):
53 def visit_literal(self, literal, lvalue, rvalue):
54 #if literal.format in ('Bool', 'UInt'):
55 print ' %s = (%s).to%s();' % (lvalue, rvalue, literal.format)
57 def visit_const(self, const, lvalue, rvalue):
58 self.visit(const.type, lvalue, rvalue)
60 def visit_alias(self, alias, lvalue, rvalue):
61 self.visit(alias.type, lvalue, rvalue)
63 def visit_enum(self, enum, lvalue, rvalue):
64 print ' %s = (%s).toSInt();' % (lvalue, rvalue)
66 def visit_bitmask(self, bitmask, lvalue, rvalue):
67 self.visit(bitmask.type, lvalue, rvalue)
69 def visit_array(self, array, lvalue, rvalue):
70 print ' const Trace::Array *__a%s = dynamic_cast<const Trace::Array *>(&%s);' % (array.id, rvalue)
71 print ' if (__a%s) {' % (array.id)
72 length = '__a%s->values.size()' % array.id
73 print ' %s = new %s[%s];' % (lvalue, array.type, length)
74 index = '__j' + array.id
75 print ' for (size_t {i} = 0; {i} < {length}; ++{i}) {{'.format(i = index, length = length)
77 self.visit(array.type, '%s[%s]' % (lvalue, index), '*__a%s->values[%s]' % (array.id, index))
81 print ' %s = NULL;' % lvalue
84 def visit_pointer(self, pointer, lvalue, rvalue):
85 print ' const Trace::Array *__a%s = dynamic_cast<const Trace::Array *>(&%s);' % (pointer.id, rvalue)
86 print ' if (__a%s) {' % (pointer.id)
87 print ' %s = new %s;' % (lvalue, pointer.type)
89 self.visit(pointer.type, '%s[0]' % (lvalue,), '*__a%s->values[0]' % (pointer.id,))
92 print ' %s = NULL;' % lvalue
95 def visit_handle(self, handle, lvalue, rvalue):
96 OpaqueValueExtractor().visit(handle.type, lvalue, rvalue);
97 new_lvalue = handle_entry(handle, lvalue)
98 print ' if (retrace::verbosity >= 2)'
99 print ' std::cout << "%s " << size_t(%s) << " <- " << size_t(%s) << "\\n";' % (handle.name, lvalue, new_lvalue)
100 print ' %s = %s;' % (lvalue, new_lvalue)
102 def visit_blob(self, blob, lvalue, rvalue):
103 print ' %s = static_cast<%s>((%s).toPointer());' % (lvalue, blob, rvalue)
105 def visit_string(self, string, lvalue, rvalue):
106 print ' %s = (%s)((%s).toString());' % (lvalue, string.expr, rvalue)
109 class OpaqueValueExtractor(ValueExtractor):
110 '''Value extractor that also understands opaque values.
112 Normally opaque values can't be retraced, unless they are being extracted
113 in the context of handles.'''
115 def visit_opaque(self, opaque, lvalue, rvalue):
116 print ' %s = static_cast<%s>((%s).toPointer());' % (lvalue, opaque, rvalue)
119 class ValueWrapper(stdapi.Visitor):
121 def visit_literal(self, literal, lvalue, rvalue):
124 def visit_alias(self, alias, lvalue, rvalue):
125 self.visit(alias.type, lvalue, rvalue)
127 def visit_enum(self, enum, lvalue, rvalue):
130 def visit_bitmask(self, bitmask, lvalue, rvalue):
133 def visit_array(self, array, lvalue, rvalue):
134 print ' const Trace::Array *__a%s = dynamic_cast<const Trace::Array *>(&%s);' % (array.id, rvalue)
135 print ' if (__a%s) {' % (array.id)
136 length = '__a%s->values.size()' % array.id
137 index = '__j' + array.id
138 print ' for (size_t {i} = 0; {i} < {length}; ++{i}) {{'.format(i = index, length = length)
140 self.visit(array.type, '%s[%s]' % (lvalue, index), '*__a%s->values[%s]' % (array.id, index))
145 def visit_pointer(self, pointer, lvalue, rvalue):
146 print ' const Trace::Array *__a%s = dynamic_cast<const Trace::Array *>(&%s);' % (pointer.id, rvalue)
147 print ' if (__a%s) {' % (pointer.id)
149 self.visit(pointer.type, '%s[0]' % (lvalue,), '*__a%s->values[0]' % (pointer.id,))
153 def visit_handle(self, handle, lvalue, rvalue):
154 print ' %s __orig_result;' % handle.type
155 OpaqueValueExtractor().visit(handle.type, '__orig_result', rvalue);
156 if handle.range is None:
157 rvalue = "__orig_result"
158 entry = handle_entry(handle, rvalue)
159 print " %s = %s;" % (entry, lvalue)
160 print ' if (retrace::verbosity >= 2)'
161 print ' std::cout << "{handle.name} " << {rvalue} << " -> " << {lvalue} << "\\n";'.format(**locals())
163 i = '__h' + handle.id
164 lvalue = "%s + %s" % (lvalue, i)
165 rvalue = "__orig_result + %s" % (i,)
166 entry = handle_entry(handle, rvalue)
167 print ' for ({handle.type} {i} = 0; {i} < {handle.range}; ++{i}) {{'.format(**locals())
168 print ' {entry} = {lvalue};'.format(**locals())
169 print ' if (retrace::verbosity >= 2)'
170 print ' std::cout << "{handle.name} " << ({rvalue}) << " -> " << ({lvalue}) << "\\n";'.format(**locals())
173 def visit_blob(self, blob, lvalue, rvalue):
176 def visit_string(self, string, lvalue, rvalue):
182 def retrace_function(self, function):
183 print 'static void retrace_%s(Trace::Call &call) {' % function.name
184 self.retrace_function_body(function)
188 def retrace_function_body(self, function):
190 for arg in function.args:
191 arg_type = ConstRemover().visit(arg.type)
192 #print ' // %s -> %s' % (arg.type, arg_type)
193 print ' %s %s;' % (arg_type, arg.name)
194 rvalue = 'call.arg(%u)' % (arg.index,)
197 self.extract_arg(function, arg, arg_type, lvalue, rvalue)
198 except NotImplementedError:
200 print ' %s = 0; // FIXME' % arg.name
202 self.fail_function(function)
203 self.call_function(function)
204 for arg in function.args:
206 arg_type = ConstRemover().visit(arg.type)
207 rvalue = 'call.arg(%u)' % (arg.index,)
210 ValueWrapper().visit(arg_type, lvalue, rvalue)
211 except NotImplementedError:
212 print ' // FIXME: %s' % arg.name
213 if function.type is not stdapi.Void:
217 ValueWrapper().visit(function.type, lvalue, rvalue)
218 except NotImplementedError:
219 print ' // FIXME: result'
221 def fail_function(self, function):
222 print ' if (retrace::verbosity >= 0)'
223 print ' std::cerr << "warning: unsupported call %s\\n";' % function.name
226 def extract_arg(self, function, arg, arg_type, lvalue, rvalue):
227 ValueExtractor().visit(arg_type, lvalue, rvalue)
229 def call_function(self, function):
230 arg_names = ", ".join([arg.name for arg in function.args])
231 if function.type is not stdapi.Void:
232 print ' %s __result;' % (function.type)
233 print ' __result = %s(%s);' % (function.name, arg_names)
235 print ' %s(%s);' % (function.name, arg_names)
237 def filter_function(self, function):
240 def retrace_functions(self, functions):
241 functions = filter(self.filter_function, functions)
243 for function in functions:
244 if function.sideeffects:
245 self.retrace_function(function)
247 print 'void retrace::retrace_call(Trace::Call &call) {'
248 print ' const char *name = call.name().c_str();'
251 func_dict = dict([(function.name, function) for function in functions])
253 def handle_case(function_name):
254 function = func_dict[function_name]
255 if function.sideeffects:
256 print ' retrace_%s(call);' % function.name
259 string_switch('name', func_dict.keys(), handle_case)
261 print ' retrace_unknown(call);'
266 def retrace_api(self, api):
268 print '#include "trace_parser.hpp"'
269 print '#include "retrace.hpp"'
272 types = api.all_types()
273 handles = [type for type in types if isinstance(type, stdapi.Handle)]
275 for handle in handles:
276 if handle.name not in handle_names:
277 if handle.key is None:
278 print 'static retrace::map<%s> __%s_map;' % (handle.type, handle.name)
280 key_name, key_type = handle.key
281 print 'static std::map<%s, retrace::map<%s> > __%s_map;' % (key_type, handle.type, handle.name)
282 handle_names.add(handle.name)
285 self.retrace_functions(api.functions)