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 """Generate DLL/SO dispatching functions.
34 sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
37 import specs.stdapi as stdapi
40 def function_pointer_type(function):
41 return 'PFN_' + function.name.upper()
44 def function_pointer_value(function):
45 return '_' + function.name + '_ptr'
51 # Must be implemented by derived classes, which should define, declare,
52 # or implement something like:
54 # typedef void (*_PROC)(void);
56 # static _PROC _getPublicProcAddress(const char *name);
57 # static _PROC _getPrivateProcAddress(const char *name);
59 raise NotImplementedError
61 def dispatchApi(self, api):
62 for function in api.functions:
63 self.dispatchFunction(api, function)
65 # define standard name aliases for convenience, but only when not
66 # tracing, as that would cause symbol clashing with the tracing
68 print '#ifdef RETRACE'
69 for function in api.functions:
70 print '#define %s _%s' % (function.name, function.name)
71 print '#endif /* RETRACE */'
74 def dispatchFunction(self, api, function):
75 ptype = function_pointer_type(function)
76 pvalue = function_pointer_value(function)
77 print 'typedef ' + function.prototype('* %s' % ptype) + ';'
78 print 'static %s %s = NULL;' % (ptype, pvalue)
80 print 'static inline ' + function.prototype('_' + function.name) + ' {'
81 print ' const char *_name = "%s";' % function.name
82 if function.type is stdapi.Void:
86 self.invokeGetProcAddress(api, function)
87 print ' %s%s(%s);' % (ret, pvalue, ', '.join([str(arg.name) for arg in function.args]))
91 def isFunctionPublic(self, api, function):
94 def getProcAddressName(self, api, function):
95 if self.isFunctionPublic(api, function):
96 return '_getPublicProcAddress'
98 return '_getPrivateProcAddress'
100 def invokeGetProcAddress(self, api, function):
101 ptype = function_pointer_type(function)
102 pvalue = function_pointer_value(function)
103 getProcAddressName = self.getProcAddressName(api, function)
104 print ' if (!%s) {' % (pvalue,)
105 print ' %s = (%s)%s(_name);' % (pvalue, ptype, getProcAddressName)
106 print ' if (!%s) {' % (pvalue,)
107 self.failFunction(function)
111 def failFunction(self, function):
112 if function.type is stdapi.Void or function.fail is not None:
113 print r' os::log("warning: ignoring call to unavailable function %s\n", _name);'
114 if function.type is stdapi.Void:
115 assert function.fail is None
118 assert function.fail is not None
119 print ' return %s;' % function.fail
121 print r' os::log("error: unavailable function %s\n", _name);'
122 print r' os::abort();'