]> git.cworth.org Git - apitrace/blob - dispatch.py
os_posix: Fix return value for os::execute()
[apitrace] / dispatch.py
1 ##########################################################################
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
27 """Generate DLL/SO dispatching functions.
28 """ 
29
30
31 import specs.stdapi as stdapi
32
33
34 def function_pointer_type(function):
35     return '__PFN' + function.name.upper()
36
37
38 def function_pointer_value(function):
39     return '__' + function.name + '_ptr'
40
41
42 class Dispatcher:
43
44     def header(self):
45         # Must be implemented by derived classes, which should define, declare,
46         # or implement something like:
47         #
48         #  typedef void (*__PROC)(void);
49         #
50         #  static __PROC __getPublicProcAddress(const char *name);
51         #  static __PROC __getPrivateProcAddress(const char *name);
52         #
53         raise NotImplementedError
54
55     def dispatch_api(self, api):
56         for function in api.functions:
57             self.invokeFunction(function)
58         
59         # define standard name aliases for convenience, but only when not
60         # tracing, as that would cause symbol clashing with the tracing
61         # functions
62         print '#ifdef RETRACE'
63         for function in api.functions:
64             print '#define %s __%s' % (function.name, function.name)
65         print '#endif /* RETRACE */'
66         print
67
68     def invokeFunction(self, function):
69         ptype = function_pointer_type(function)
70         pvalue = function_pointer_value(function)
71         print 'typedef ' + function.prototype('* %s' % ptype) + ';'
72         print 'static %s %s = NULL;' % (ptype, pvalue)
73         print
74         print 'static inline ' + function.prototype('__' + function.name) + ' {'
75         print '    const char *__name = "%s";' % function.name
76         if function.type is stdapi.Void:
77             ret = ''
78         else:
79             ret = 'return '
80         self.get_true_pointer(function)
81         print '    %s%s(%s);' % (ret, pvalue, ', '.join([str(arg.name) for arg in function.args]))
82         print '}'
83         print
84
85     def isFunctionPublic(self, function):
86         return True
87
88     def get_true_pointer(self, function):
89         ptype = function_pointer_type(function)
90         pvalue = function_pointer_value(function)
91         if self.isFunctionPublic(function):
92             get_proc_address = '__getPublicProcAddress'
93         else:
94             get_proc_address = '__getPrivateProcAddress'
95         print '    if (!%s) {' % (pvalue,)
96         print '        %s = (%s)%s(__name);' % (pvalue, ptype, get_proc_address)
97         print '        if (!%s) {' % (pvalue,)
98         self.failFunction(function)
99         print '        }'
100         print '    }'
101
102     def failFunction(self, function):
103         if function.type is stdapi.Void or function.fail is not None:
104             print r'            os::log("warning: ignoring call to unavailable function %s\n", __name);'
105             if function.type is stdapi.Void:
106                 assert function.fail is None
107                 print '            return;' 
108             else:
109                 assert function.fail is not None
110                 print '            return %s;' % function.fail
111         else:
112             print r'            os::log("error: unavailable function %s\n", __name);'
113             print r'            os::abort();'
114
115