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):
41 if expr.startswith('const '):
43 return stdapi.Opaque(expr)
46 def handle_entry(handle, value):
47 if handle.key is None:
48 return "__%s_map[%s]" % (handle.name, value)
50 key_name, key_type = handle.key
51 return "__%s_map[%s][%s]" % (handle.name, key_name, value)
54 class ValueExtractor(stdapi.Visitor):
56 def visit_literal(self, literal, lvalue, rvalue):
57 if literal.format == 'Bool':
58 print ' %s = static_cast<bool>(%s);' % (lvalue, rvalue)
60 print ' %s = %s;' % (lvalue, rvalue)
62 def visit_const(self, const, lvalue, rvalue):
63 self.visit(const.type, lvalue, rvalue)
65 def visit_alias(self, alias, lvalue, rvalue):
66 self.visit(alias.type, lvalue, rvalue)
68 def visit_enum(self, enum, lvalue, rvalue):
69 print ' %s = %s;' % (lvalue, rvalue)
71 def visit_bitmask(self, bitmask, lvalue, rvalue):
72 self.visit(bitmask.type, lvalue, rvalue)
74 def visit_array(self, array, lvalue, rvalue):
75 print ' const Trace::Array *__a%s = dynamic_cast<const Trace::Array *>(&%s);' % (array.id, rvalue)
76 print ' if (__a%s) {' % (array.id)
77 length = '__a%s->values.size()' % array.id
78 print ' %s = new %s[%s];' % (lvalue, array.type, length)
79 index = '__j' + array.id
80 print ' for(size_t {i} = 0; {i} < {length}; ++{i}) {{'.format(i = index, length = length)
82 self.visit(array.type, '%s[%s]' % (lvalue, index), '*__a%s->values[%s]' % (array.id, index))
86 print ' %s = NULL;' % lvalue
89 def visit_pointer(self, pointer, lvalue, rvalue):
90 print ' const Trace::Array *__a%s = dynamic_cast<const Trace::Array *>(&%s);' % (pointer.id, rvalue)
91 print ' if (__a%s) {' % (pointer.id)
92 print ' %s = new %s;' % (lvalue, pointer.type)
94 self.visit(pointer.type, '%s[0]' % (lvalue,), '*__a%s->values[0]' % (pointer.id,))
97 print ' %s = NULL;' % lvalue
100 def visit_handle(self, handle, lvalue, rvalue):
101 self.visit(handle.type, lvalue, handle_entry(handle, rvalue));
102 print ' if (retrace::verbosity >= 2)'
103 print ' std::cout << "%s " << static_cast<%s>(%s) << " <- " << %s << "\\n";' % (handle.name, handle.type, rvalue, lvalue)
105 def visit_blob(self, blob, lvalue, rvalue):
106 print ' %s = static_cast<%s>((%s).blob());' % (lvalue, blob, rvalue)
108 def visit_string(self, string, lvalue, rvalue):
109 print ' %s = (%s)((%s).string());' % (lvalue, string.expr, rvalue)
113 class ValueWrapper(stdapi.Visitor):
115 def visit_literal(self, literal, lvalue, rvalue):
118 def visit_alias(self, alias, lvalue, rvalue):
119 self.visit(alias.type, lvalue, rvalue)
121 def visit_enum(self, enum, lvalue, rvalue):
124 def visit_bitmask(self, bitmask, lvalue, rvalue):
127 def visit_array(self, array, lvalue, rvalue):
128 print ' const Trace::Array *__a%s = dynamic_cast<const Trace::Array *>(&%s);' % (array.id, rvalue)
129 print ' if (__a%s) {' % (array.id)
130 length = '__a%s->values.size()' % array.id
131 index = '__j' + array.id
132 print ' for(size_t {i} = 0; {i} < {length}; ++{i}) {{'.format(i = index, length = length)
134 self.visit(array.type, '%s[%s]' % (lvalue, index), '*__a%s->values[%s]' % (array.id, index))
139 def visit_pointer(self, pointer, lvalue, rvalue):
140 print ' const Trace::Array *__a%s = dynamic_cast<const Trace::Array *>(&%s);' % (pointer.id, rvalue)
141 print ' if (__a%s) {' % (pointer.id)
143 self.visit(pointer.type, '%s[0]' % (lvalue,), '*__a%s->values[0]' % (pointer.id,))
148 def visit_handle(self, handle, lvalue, rvalue):
149 if handle.range is None:
150 rvalue = "static_cast<%s>(%s)" % (handle.type, rvalue)
151 entry = handle_entry(handle, rvalue)
152 print " %s = %s;" % (entry, lvalue)
153 print ' if (retrace::verbosity >= 2)'
154 print ' std::cout << "{handle.name} " << {rvalue} << " -> " << {lvalue} << "\\n";'.format(**locals())
156 i = '__h' + handle.id
157 lvalue = "%s + %s" % (lvalue, i)
158 rvalue = "static_cast<%s>(%s) + %s" % (handle.type, rvalue, i)
159 entry = handle_entry(handle, rvalue)
160 print ' for({handle.type} {i} = 0; {i} < {handle.range}; ++{i}) {{'.format(**locals())
161 print ' {entry} = {lvalue};'.format(**locals())
162 print ' if (retrace::verbosity >= 2)'
163 print ' std::cout << "{handle.name} " << ({rvalue}) << " -> " << ({lvalue}) << "\\n";'.format(**locals())
166 def visit_blob(self, blob, lvalue, rvalue):
169 def visit_string(self, string, lvalue, rvalue):
175 def retrace_function(self, function):
176 print 'static void retrace_%s(Trace::Call &call) {' % function.name
177 self.retrace_function_body(function)
181 def retrace_function_body(self, function):
183 for arg in function.args:
184 arg_type = ConstRemover().visit(arg.type)
185 #print ' // %s -> %s' % (arg.type, arg_type)
186 print ' %s %s;' % (arg_type, arg.name)
187 rvalue = 'call.arg(%u)' % (arg.index,)
190 self.extract_arg(function, arg, arg_type, lvalue, rvalue)
191 except NotImplementedError:
193 print ' %s = 0; // FIXME' % arg.name
195 self.fail_function(function)
196 self.call_function(function)
197 for arg in function.args:
199 arg_type = ConstRemover().visit(arg.type)
200 rvalue = 'call.arg(%u)' % (arg.index,)
203 ValueWrapper().visit(arg_type, lvalue, rvalue)
204 except NotImplementedError:
205 print ' // FIXME: %s' % arg.name
206 if function.type is not stdapi.Void:
210 ValueWrapper().visit(function.type, lvalue, rvalue)
211 except NotImplementedError:
212 print ' // FIXME: result'
214 def fail_function(self, function):
215 print ' if (retrace::verbosity >= 0)'
216 print ' std::cerr << "warning: unsupported call %s\\n";' % function.name
219 def extract_arg(self, function, arg, arg_type, lvalue, rvalue):
220 ValueExtractor().visit(arg_type, lvalue, rvalue)
222 def call_function(self, function):
223 arg_names = ", ".join([arg.name for arg in function.args])
224 if function.type is not stdapi.Void:
225 print ' %s __result;' % (function.type)
226 print ' __result = %s(%s);' % (function.name, arg_names)
228 print ' %s(%s);' % (function.name, arg_names)
230 def filter_function(self, function):
233 def retrace_functions(self, functions):
234 functions = filter(self.filter_function, functions)
236 for function in functions:
237 if function.sideeffects:
238 self.retrace_function(function)
240 print 'bool retrace::retrace_call(Trace::Call &call) {'
241 print ' const char *name = call.name().c_str();'
243 print ' if (retrace::verbosity >= 1) {'
244 print ' std::cout << call;'
245 print ' std::cout.flush();'
249 func_dict = dict([(function.name, function) for function in functions])
251 def handle_case(function_name):
252 function = func_dict[function_name]
253 if function.sideeffects:
254 print ' retrace_%s(call);' % function.name
255 print ' return true;'
257 string_switch('name', func_dict.keys(), handle_case)
259 print ' if (retrace::verbosity >= 0)'
260 print ' std::cerr << "warning: unknown call " << call.name() << "\\n";'
261 print ' return false;'
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)