blob: c68179b7a6a0a9156ac4c2cb784bbb57ad1cce92 [file] [log] [blame]
José Fonsecae0e61402010-11-25 15:03:23 +00001##########################################################################
2#
3# Copyright 2010 VMware, Inc.
4# All Rights Reserved.
5#
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:
12#
13# The above copyright notice and this permission notice shall be included in
14# all copies or substantial portions of the Software.
15#
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
22# THE SOFTWARE.
23#
24##########################################################################/
25
26
José Fonseca4a826ed2010-11-30 16:58:22 +000027"""Generic retracing code generator."""
28
José Fonsecae0e61402010-11-25 15:03:23 +000029import stdapi
30import glapi
José Fonseca522b7442010-11-27 15:30:40 +000031from codegen import *
José Fonsecae0e61402010-11-25 15:03:23 +000032
33
34class ConstRemover(stdapi.Rebuilder):
35
36 def visit_const(self, const):
37 return const.type
38
39 def visit_opaque(self, opaque):
40 expr = opaque.expr
41 if expr.startswith('const '):
42 expr = expr[6:]
43 return stdapi.Opaque(expr)
44
45
José Fonseca8a844ae2010-12-06 18:50:52 +000046def handle_entry(handle, value):
47 if handle.key is None:
48 return "__%s_map[%s]" % (handle.name, value)
49 else:
50 key_name, key_type = handle.key
51 return "__%s_map[%s][%s]" % (handle.name, key_name, value)
52
53
José Fonsecae0e61402010-11-25 15:03:23 +000054class ValueExtractor(stdapi.Visitor):
55
56 def visit_literal(self, literal, lvalue, rvalue):
57 if literal.format == 'Bool':
58 print ' %s = static_cast<bool>(%s);' % (lvalue, rvalue)
59 else:
60 print ' %s = %s;' % (lvalue, rvalue)
61
62 def visit_const(self, const, lvalue, rvalue):
63 self.visit(const.type, lvalue, rvalue)
64
65 def visit_alias(self, alias, lvalue, rvalue):
66 self.visit(alias.type, lvalue, rvalue)
67
68 def visit_enum(self, enum, lvalue, rvalue):
69 print ' %s = %s;' % (lvalue, rvalue)
70
71 def visit_bitmask(self, bitmask, lvalue, rvalue):
72 self.visit(bitmask.type, lvalue, rvalue)
73
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)
81 try:
82 self.visit(array.type, '%s[%s]' % (lvalue, index), '*__a%s->values[%s]' % (array.id, index))
83 finally:
84 print ' }'
85 print ' } else {'
86 print ' %s = NULL;' % lvalue
87 print ' }'
88
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)
93 try:
94 self.visit(pointer.type, '%s[0]' % (lvalue,), '*__a%s->values[0]' % (pointer.id,))
95 finally:
96 print ' } else {'
97 print ' %s = NULL;' % lvalue
98 print ' }'
99
100 def visit_handle(self, handle, lvalue, rvalue):
José Fonseca8a844ae2010-12-06 18:50:52 +0000101 self.visit(handle.type, lvalue, handle_entry(handle, rvalue));
José Fonsecae0e61402010-11-25 15:03:23 +0000102 print ' if (verbosity >= 2)'
103 print ' std::cout << "%s " << static_cast<%s>(%s) << " <- " << %s << "\\n";' % (handle.name, handle.type, rvalue, lvalue)
104
105 def visit_blob(self, blob, lvalue, rvalue):
106 print ' %s = static_cast<%s>((%s).blob());' % (lvalue, blob, rvalue)
107
108 def visit_string(self, string, lvalue, rvalue):
109 print ' %s = (%s)((%s).string());' % (lvalue, string.expr, rvalue)
110
111
112
113class ValueWrapper(stdapi.Visitor):
114
115 def visit_literal(self, literal, lvalue, rvalue):
116 pass
117
118 def visit_alias(self, alias, lvalue, rvalue):
119 self.visit(alias.type, lvalue, rvalue)
120
121 def visit_enum(self, enum, lvalue, rvalue):
122 pass
123
124 def visit_bitmask(self, bitmask, lvalue, rvalue):
125 pass
126
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)
133 try:
134 self.visit(array.type, '%s[%s]' % (lvalue, index), '*__a%s->values[%s]' % (array.id, index))
135 finally:
136 print ' }'
137 print ' }'
138
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)
142 try:
143 self.visit(pointer.type, '%s[0]' % (lvalue,), '*__a%s->values[0]' % (pointer.id,))
144 finally:
145 print ' }'
146
147
148 def visit_handle(self, handle, lvalue, rvalue):
José Fonsecad922e1d2010-11-25 17:14:02 +0000149 if handle.range is None:
José Fonseca8a844ae2010-12-06 18:50:52 +0000150 rvalue = "static_cast<%s>(%s)" % (handle.type, rvalue)
151 entry = handle_entry(handle, rvalue)
152 print " %s = %s;" % (entry, lvalue)
José Fonsecad922e1d2010-11-25 17:14:02 +0000153 print ' if (verbosity >= 2)'
José Fonseca8a844ae2010-12-06 18:50:52 +0000154 print ' std::cout << "{handle.name} " << {rvalue} << " -> " << {lvalue} << "\\n";'.format(**locals())
José Fonsecad922e1d2010-11-25 17:14:02 +0000155 else:
156 i = '__h' + handle.id
José Fonseca8a844ae2010-12-06 18:50:52 +0000157 lvalue = "%s + %s" % (lvalue, i)
158 rvalue = "static_cast<%s>(%s) + %s" % (handle.type, rvalue, i)
159 entry = handle_entry(handle, rvalue)
José Fonsecad922e1d2010-11-25 17:14:02 +0000160 print ' for({handle.type} {i} = 0; {i} < {handle.range}; ++{i}) {{'.format(**locals())
José Fonseca8a844ae2010-12-06 18:50:52 +0000161 print ' {entry} = {lvalue};'.format(**locals())
José Fonsecad922e1d2010-11-25 17:14:02 +0000162 print ' if (verbosity >= 2)'
José Fonseca8a844ae2010-12-06 18:50:52 +0000163 print ' std::cout << "{handle.name} " << ({rvalue}) << " -> " << ({lvalue}) << "\\n";'.format(**locals())
José Fonsecad922e1d2010-11-25 17:14:02 +0000164 print ' }'
José Fonsecae0e61402010-11-25 15:03:23 +0000165
166 def visit_blob(self, blob, lvalue, rvalue):
167 pass
168
169 def visit_string(self, string, lvalue, rvalue):
170 pass
171
172
José Fonsecae0e61402010-11-25 15:03:23 +0000173class Retracer:
174
175 def retrace_function(self, function):
176 print 'static void retrace_%s(Trace::Call &call) {' % function.name
177 success = True
178 for arg in function.args:
179 arg_type = ConstRemover().visit(arg.type)
180 #print ' // %s -> %s' % (arg.type, arg_type)
181 print ' %s %s;' % (arg_type, arg.name)
182 rvalue = 'call.arg(%u)' % (arg.index,)
183 lvalue = arg.name
184 try:
José Fonsecadacd8dd2010-11-25 17:50:26 +0000185 self.extract_arg(function, arg, arg_type, lvalue, rvalue)
José Fonsecae0e61402010-11-25 15:03:23 +0000186 except NotImplementedError:
187 success = False
188 print ' %s = 0; // FIXME' % arg.name
189 if not success:
José Fonsecafa15d332010-11-25 20:22:39 +0000190 self.fail_function(function)
191 self.call_function(function)
José Fonsecae0e61402010-11-25 15:03:23 +0000192 for arg in function.args:
193 if arg.output:
194 arg_type = ConstRemover().visit(arg.type)
195 rvalue = 'call.arg(%u)' % (arg.index,)
196 lvalue = arg.name
197 try:
198 ValueWrapper().visit(arg_type, lvalue, rvalue)
199 except NotImplementedError:
200 print ' // FIXME: %s' % arg.name
201 if function.type is not stdapi.Void:
202 rvalue = '*call.ret'
203 lvalue = '__result'
204 try:
205 ValueWrapper().visit(function.type, lvalue, rvalue)
206 except NotImplementedError:
207 print ' // FIXME: result'
208 print '}'
209 print
210
José Fonsecafa15d332010-11-25 20:22:39 +0000211 def fail_function(self, function):
José Fonseca5e687162011-02-09 15:11:12 +0000212 print ' if (verbosity >= 0)'
213 print ' std::cerr << "warning: unsupported call %s\\n";' % function.name
José Fonsecafa15d332010-11-25 20:22:39 +0000214 print ' return;'
215
José Fonsecadacd8dd2010-11-25 17:50:26 +0000216 def extract_arg(self, function, arg, arg_type, lvalue, rvalue):
217 ValueExtractor().visit(arg_type, lvalue, rvalue)
José Fonsecae0e61402010-11-25 15:03:23 +0000218
José Fonsecafa15d332010-11-25 20:22:39 +0000219 def call_function(self, function):
220 arg_names = ", ".join([arg.name for arg in function.args])
221 if function.type is not stdapi.Void:
222 print ' %s __result;' % (function.type)
223 print ' __result = %s(%s);' % (function.name, arg_names)
224 else:
225 print ' %s(%s);' % (function.name, arg_names)
226
José Fonseca4441baf2010-11-25 19:55:27 +0000227 def filter_function(self, function):
228 return True
229
José Fonsecae0e61402010-11-25 15:03:23 +0000230 def retrace_functions(self, functions):
José Fonseca4441baf2010-11-25 19:55:27 +0000231 functions = filter(self.filter_function, functions)
232
José Fonsecae0e61402010-11-25 15:03:23 +0000233 for function in functions:
234 if function.sideeffects:
235 self.retrace_function(function)
236
237 print 'static bool retrace_call(Trace::Call &call) {'
José Fonseca522b7442010-11-27 15:30:40 +0000238 print ' const char *name = call.name().c_str();'
José Fonsecae0e61402010-11-25 15:03:23 +0000239 print
José Fonseca5e687162011-02-09 15:11:12 +0000240 print ' if (verbosity >= 1) {'
José Fonsecae0e61402010-11-25 15:03:23 +0000241 print ' std::cout << call;'
242 print ' std::cout.flush();'
243 print ' };'
244 print
José Fonseca522b7442010-11-27 15:30:40 +0000245
246 func_dict = dict([(function.name, function) for function in functions])
247
248 def handle_case(function_name):
249 function = func_dict[function_name]
José Fonsecae0e61402010-11-25 15:03:23 +0000250 if function.sideeffects:
José Fonsecae0e61402010-11-25 15:03:23 +0000251 print ' retrace_%s(call);' % function.name
José Fonseca522b7442010-11-27 15:30:40 +0000252 print ' return true;'
253
254 string_switch('name', func_dict.keys(), handle_case)
255
José Fonseca5e687162011-02-09 15:11:12 +0000256 print ' if (verbosity >= 0)'
257 print ' std::cerr << "warning: unknown call " << call.name() << "\\n";'
José Fonsecae0e61402010-11-25 15:03:23 +0000258 print ' return false;'
259 print '}'
260 print
261
262
263 def retrace_api(self, api):
264
265 print '#include "trace_parser.hpp"'
266 print
267
268 types = api.all_types()
269 handles = [type for type in types if isinstance(type, stdapi.Handle)]
José Fonsecad922e1d2010-11-25 17:14:02 +0000270 handle_names = set()
José Fonsecae0e61402010-11-25 15:03:23 +0000271 for handle in handles:
José Fonsecad922e1d2010-11-25 17:14:02 +0000272 if handle.name not in handle_names:
José Fonseca8a844ae2010-12-06 18:50:52 +0000273 if handle.key is None:
274 print 'static std::map<%s, %s> __%s_map;' % (handle.type, handle.type, handle.name)
275 else:
276 key_name, key_type = handle.key
277 print 'static std::map<%s, std::map<%s, %s> > __%s_map;' % (key_type, handle.type, handle.type, handle.name)
José Fonsecad922e1d2010-11-25 17:14:02 +0000278 handle_names.add(handle.name)
José Fonsecae0e61402010-11-25 15:03:23 +0000279 print
280
José Fonsecaecf2f742011-02-09 14:51:07 +0000281 print 'int verbosity = 0;'
José Fonsecae0e61402010-11-25 15:03:23 +0000282 print
283
284 self.retrace_functions(api.functions)
285