]> git.cworth.org Git - apitrace/blob - trace.py
More efficient enum representation.
[apitrace] / trace.py
1 ##########################################################################
2 #
3 # Copyright 2008-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 """C basic types"""
27
28
29 import stdapi
30
31
32 all_types = {}
33
34
35 class DumpDeclarator(stdapi.OnceVisitor):
36     '''Declare helper functions to dump complex types.'''
37
38     def visit_void(self, literal):
39         pass
40
41     def visit_literal(self, literal):
42         pass
43
44     def visit_string(self, string):
45         pass
46
47     def visit_const(self, const):
48         self.visit(const.type)
49
50     def visit_struct(self, struct):
51         for type, name in struct.members:
52             self.visit(type)
53         print 'static void __traceStruct%s(const %s &value) {' % (struct.id, struct.expr)
54         print '    Trace::BeginStruct(%u);' % len(struct.members)
55         for type, name in struct.members:
56             print '    Trace::BeginMember("%s");' % (name,)
57             dump_instance(type, 'value.%s' % (name,))
58             print '    Trace::EndMember();'
59         print '    Trace::EndStruct();'
60         print '}'
61         print
62
63     def visit_array(self, array):
64         self.visit(array.type)
65
66     def visit_blob(self, array):
67         pass
68
69     def visit_enum(self, enum):
70         print 'static void __traceEnum%s(const %s value) {' % (enum.id, enum.expr)
71         n = len(enum.values)
72         for i in range(n):
73             value = enum.values[i]
74             print '    static const Trace::EnumSig sig%u = {%u, "%s", %s};' % (i, enum.vid + i, value, value)
75         print '    const Trace::EnumSig *sig;'
76         print '    switch(value) {'
77         for i in range(n):
78             value = enum.values[i]
79             print '    case %s:' % value
80             print '        sig = &sig%u;' % i
81             print '        break;'
82         print '    default:'
83         print '        Trace::LiteralSInt(value);'
84         print '        return;'
85         print '    }'
86         print '        Trace::LiteralEnum(sig);'
87         print '}'
88         print
89
90     def visit_bitmask(self, bitmask):
91         print 'static const Trace::BitmaskVal __bitmask%s_vals[] = {' % (bitmask.id)
92         for value in bitmask.values:
93             print '   {"%s", %s},' % (value, value)
94         print '};'
95         print
96         print 'static const Trace::BitmaskSig __bitmask%s_sig = {' % (bitmask.id)
97         print '   %u, %u, __bitmask%s_vals' % (int(bitmask.id), len(bitmask.values), bitmask.id)
98         print '};'
99         print
100
101     def visit_pointer(self, pointer):
102         self.visit(pointer.type)
103
104     def visit_handle(self, handle):
105         self.visit(handle.type)
106
107     def visit_alias(self, alias):
108         self.visit(alias.type)
109
110     def visit_opaque(self, opaque):
111         pass
112
113     def visit_interface(self, interface):
114         pass
115
116
117 class DumpImplementer(stdapi.Visitor):
118     '''Dump an instance.'''
119
120     def visit_literal(self, literal, instance):
121         print '    Trace::Literal%s(%s);' % (literal.format, instance)
122
123     def visit_string(self, string, instance):
124         if string.length is not None:
125             print '    Trace::LiteralString((const char *)%s, %s);' % (instance, string.length)
126         else:
127             print '    Trace::LiteralString((const char *)%s);' % instance
128
129     def visit_const(self, const, instance):
130         self.visit(const.type, instance)
131
132     def visit_struct(self, struct, instance):
133         print '    __traceStruct%s(%s);' % (struct.id, instance)
134
135     def visit_array(self, array, instance):
136         print '    if(%s) {' % instance
137         index = '__i' + array.type.id
138         print '        Trace::BeginArray(%s);' % (array.length,)
139         print '        for (int %s = 0; %s < %s; ++%s) {' % (index, index, array.length, index)
140         print '            Trace::BeginElement();'
141         self.visit(array.type, '(%s)[%s]' % (instance, index))
142         print '            Trace::EndElement();'
143         print '        }'
144         print '        Trace::EndArray();'
145         print '    }'
146         print '    else'
147         print '        Trace::LiteralNull();'
148
149     def visit_blob(self, blob, instance):
150         print '    Trace::LiteralBlob(%s, %s);' % (instance, blob.size)
151
152     def visit_enum(self, enum, instance):
153         print '    __traceEnum%s(%s);' % (enum.id, instance)
154
155     def visit_bitmask(self, bitmask, instance):
156         print '    Trace::LiteralBitmask(__bitmask%s_sig, %s);' % (bitmask.id, instance)
157
158     def visit_pointer(self, pointer, instance):
159         print '    if(%s) {' % instance
160         print '        Trace::BeginArray(1);'
161         print '        Trace::BeginElement();'
162         dump_instance(pointer.type, "*" + instance)
163         print '        Trace::EndElement();'
164         print '        Trace::EndArray();'
165         print '    }'
166         print '    else'
167         print '        Trace::LiteralNull();'
168
169     def visit_handle(self, handle, instance):
170         self.visit(handle.type, instance)
171
172     def visit_alias(self, alias, instance):
173         self.visit(alias.type, instance)
174
175     def visit_opaque(self, opaque, instance):
176         print '    Trace::LiteralOpaque((const void *)%s);' % instance
177
178     def visit_interface(self, interface, instance):
179         print '    Trace::LiteralOpaque((const void *)%s);' % instance
180
181
182 dump_instance = DumpImplementer().visit
183
184
185
186 class Wrapper(stdapi.Visitor):
187     '''Wrap an instance.'''
188
189     def visit_void(self, type, instance):
190         raise NotImplementedError
191
192     def visit_literal(self, type, instance):
193         pass
194
195     def visit_string(self, type, instance):
196         pass
197
198     def visit_const(self, type, instance):
199         pass
200
201     def visit_struct(self, struct, instance):
202         for type, name in struct.members:
203             self.visit(type, "(%s).%s" % (instance, name))
204
205     def visit_array(self, array, instance):
206         # XXX: actually it is possible to return an array of pointers
207         pass
208
209     def visit_blob(self, blob, instance):
210         pass
211
212     def visit_enum(self, enum, instance):
213         pass
214
215     def visit_bitmask(self, bitmask, instance):
216         pass
217
218     def visit_pointer(self, pointer, instance):
219         self.visit(pointer.type, "*" + instance)
220
221     def visit_handle(self, handle, instance):
222         self.visit(handle.type, instance)
223
224     def visit_alias(self, alias, instance):
225         self.visit(alias.type, instance)
226
227     def visit_opaque(self, opaque, instance):
228         pass
229     
230     def visit_interface(self, interface, instance):
231         print "    if(%s)" % instance
232         print "        %s = new %s(%s);" % (instance, interface.type.wrap_name(), instance)
233
234
235 class Unwrapper(Wrapper):
236
237     def visit_interface(self, interface, instance):
238         print "    if(%s)" % instance
239         print "        %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, interface.type.wrap_name(), instance)
240
241 wrap_instance = Wrapper().visit
242 unwrap_instance = Unwrapper().visit
243
244
245 class Tracer:
246
247     def trace_api(self, api):
248         self.header(api)
249
250         # Includes
251         for header in api.headers:
252             print header
253         print
254
255         # Type dumpers
256         types = api.all_types()
257         visitor = DumpDeclarator()
258         map(visitor.visit, types)
259         print
260
261         # Interfaces wrapers
262         map(self.interface_wrap_name, api.interfaces)
263         map(self.interface_pre_decl, api.interfaces)
264         map(self.interface_decl, api.interfaces)
265         map(self.interface_wrap_impl, api.interfaces)
266         print
267
268         # Function wrappers
269         map(self.trace_function_decl, api.functions)
270         map(self.trace_function_impl, api.functions)
271         print
272
273         self.footer(api)
274
275     def header(self, api):
276         pass
277
278     def footer(self, api):
279         pass
280
281     def function_pointer_type(self, function):
282         return 'P' + function.name
283
284     def function_pointer_value(self, function):
285         return 'p' + function.name
286
287     def trace_function_decl(self, function):
288         ptype = self.function_pointer_type(function)
289         pvalue = self.function_pointer_value(function)
290         print 'typedef ' + function.prototype('* %s' % ptype) + ';'
291         print 'static %s %s = NULL;' % (ptype, pvalue)
292         print
293         print 'static const char * __%s_args[] = {' % (function.name)
294         for arg in function.args:
295             print '   "%s",' % (arg.name,)
296         print '};'
297         print
298         print 'static const Trace::FunctionSig __%s_sig = {' % (function.name)
299         print '   %u, "%s", %u, __%s_args' % (int(function.id), function.name, len(function.args), function.name)
300         print '};'
301         print
302
303
304     def trace_function_fail(self, function):
305         if function.fail is not None:
306             if function.type is stdapi.Void:
307                 assert function.fail == ''
308                 print '            return;' 
309             else:
310                 assert function.fail != ''
311                 print '            return %s;' % function.fail
312         else:
313             print '            Trace::Abort();'
314
315     def get_function_address(self, function):
316         raise NotImplementedError
317
318     def _get_true_pointer(self, function):
319         ptype = self.function_pointer_type(function)
320         pvalue = self.function_pointer_value(function)
321         print '    if(!%s) {' % (pvalue,)
322         print '        %s = (%s)%s;' % (pvalue, ptype, self.get_function_address(function))
323         print '        if(!%s)' % (pvalue,)
324         self.trace_function_fail(function)
325         print '    }'
326
327     def trace_function_impl(self, function):
328         pvalue = self.function_pointer_value(function)
329         print function.prototype() + ' {'
330         if function.type is stdapi.Void:
331             result = ''
332         else:
333             print '    %s __result;' % function.type
334             result = '__result = '
335         self._get_true_pointer(function)
336         print '    unsigned __call = Trace::BeginEnter(__%s_sig);' % (function.name)
337         for arg in function.args:
338             if not arg.output:
339                 self.unwrap_arg(function, arg)
340                 self.dump_arg(function, arg)
341         print '    Trace::EndEnter();'
342         print '    %s%s(%s);' % (result, pvalue, ', '.join([str(arg.name) for arg in function.args]))
343         print '    Trace::BeginLeave(__call);'
344         for arg in function.args:
345             if arg.output:
346                 self.dump_arg(function, arg)
347                 self.wrap_arg(function, arg)
348         if function.type is not stdapi.Void:
349             self.dump_ret(function, "__result")
350         print '    Trace::EndLeave();'
351         if function.type is not stdapi.Void:
352             self.wrap_ret(function, "__result")
353             print '    return __result;'
354         print '}'
355         print
356
357     def dump_arg(self, function, arg):
358         print '    Trace::BeginArg(%u);' % (arg.index,)
359         dump_instance(arg.type, arg.name)
360         print '    Trace::EndArg();'
361
362     def wrap_arg(self, function, arg):
363         wrap_instance(arg.type, arg.name)
364
365     def unwrap_arg(self, function, arg):
366         unwrap_instance(arg.type, arg.name)
367
368     def dump_ret(self, function, instance):
369         print '    Trace::BeginReturn();'
370         dump_instance(function.type, instance)
371         print '    Trace::EndReturn();'
372
373     def wrap_ret(self, function, instance):
374         wrap_instance(function.type, instance)
375
376     def unwrap_ret(self, function, instance):
377         unwrap_instance(function.type, instance)
378
379     def interface_wrap_name(self, interface):
380         return "Wrap" + interface.expr
381
382     def interface_pre_decl(self, interface):
383         print "class %s;" % interface.wrap_name()
384
385     def interface_decl(self, interface):
386         print "class %s : public %s " % (interface.wrap_name(), interface.name)
387         print "{"
388         print "public:"
389         print "    %s(%s * pInstance);" % (interface.wrap_name(), interface.name)
390         print "    virtual ~%s();" % interface.wrap_name()
391         print
392         for method in interface.itermethods():
393             print "    " + method.prototype() + ";"
394         print
395         #print "private:"
396         print "    %s * m_pInstance;" % (interface.name,)
397         print "};"
398         print
399
400     def interface_wrap_impl(self, interface):
401         print '%s::%s(%s * pInstance) {' % (interface.wrap_name(), interface.wrap_name(), interface.name)
402         print '    m_pInstance = pInstance;'
403         print '}'
404         print
405         print '%s::~%s() {' % (interface.wrap_name(), interface.wrap_name())
406         print '}'
407         print
408         for method in interface.itermethods():
409             self.trace_method(interface, method)
410         print
411
412     def trace_method(self, interface, method):
413         print method.prototype(interface.wrap_name() + '::' + method.name) + ' {'
414         if method.type is Void:
415             result = ''
416         else:
417             print '    %s __result;' % method.type
418             result = '__result = '
419         print '    Trace::BeginCall("%s");' % (interface.name + '::' + method.name)
420         print '    Trace::BeginArg(0);'
421         print '    Trace::LiteralOpaque((const void *)m_pInstance);'
422         print '    Trace::EndArg();'
423         for arg in method.args:
424             if not arg.output:
425                 self.unwrap_arg(method, arg)
426                 self.dump_arg(method, arg)
427         print '    %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(arg.name) for arg in method.args]))
428         for arg in method.args:
429             if arg.output:
430                 self.dump_arg(method, arg)
431                 self.wrap_arg(method, arg)
432         if method.type is not Void:
433             print '    Trace::BeginReturn("%s");' % method.type
434             dump_instance(method.type, "__result")
435             print '    Trace::EndReturn();'
436             wrap_instance(method.type, '__result')
437         print '    Trace::EndCall();'
438         if method.name == 'QueryInterface':
439             print '    if (*ppvObj == m_pInstance)'
440             print '        *ppvObj = this;'
441         if method.name == 'Release':
442             assert method.type is not Void
443             print '    if (!__result)'
444             print '        delete this;'
445         if method.type is not Void:
446             print '    return __result;'
447         print '}'
448         print
449
450