blob: 3ca3c568f81828d971878aaea4179211a3131ba8 [file] [log] [blame]
José Fonseca669b2002011-02-20 13:32:19 +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
27"""Generate DLL/SO dispatching functions.
28"""
29
30
José Fonseca4f242f42012-04-14 18:13:25 +010031# Adjust path
32import os.path
33import sys
34sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
35
36
José Fonsecabd86a222011-09-27 09:21:38 +010037import specs.stdapi as stdapi
José Fonseca669b2002011-02-20 13:32:19 +000038
39
40def function_pointer_type(function):
José Fonseca632a78d2012-04-19 07:18:59 +010041 return 'PFN_' + function.name.upper()
José Fonseca669b2002011-02-20 13:32:19 +000042
43
44def function_pointer_value(function):
José Fonseca632a78d2012-04-19 07:18:59 +010045 return '_' + function.name + '_ptr'
José Fonseca669b2002011-02-20 13:32:19 +000046
47
48class Dispatcher:
49
50 def header(self):
José Fonseca7989f932014-07-17 19:45:15 +010051 pass
José Fonseca669b2002011-02-20 13:32:19 +000052
José Fonseca81301932012-11-11 00:10:20 +000053 def dispatchModule(self, module):
54 for function in module.functions:
55 self.dispatchFunction(module, function)
José Fonseca669b2002011-02-20 13:32:19 +000056
57 # define standard name aliases for convenience, but only when not
58 # tracing, as that would cause symbol clashing with the tracing
59 # functions
60 print '#ifdef RETRACE'
José Fonseca81301932012-11-11 00:10:20 +000061 for function in module.functions:
José Fonseca632a78d2012-04-19 07:18:59 +010062 print '#define %s _%s' % (function.name, function.name)
José Fonseca669b2002011-02-20 13:32:19 +000063 print '#endif /* RETRACE */'
64 print
65
José Fonseca81301932012-11-11 00:10:20 +000066 def dispatchFunction(self, module, function):
José Fonseca669b2002011-02-20 13:32:19 +000067 ptype = function_pointer_type(function)
68 pvalue = function_pointer_value(function)
69 print 'typedef ' + function.prototype('* %s' % ptype) + ';'
70 print 'static %s %s = NULL;' % (ptype, pvalue)
71 print
José Fonseca632a78d2012-04-19 07:18:59 +010072 print 'static inline ' + function.prototype('_' + function.name) + ' {'
73 print ' const char *_name = "%s";' % function.name
José Fonseca669b2002011-02-20 13:32:19 +000074 if function.type is stdapi.Void:
75 ret = ''
76 else:
77 ret = 'return '
José Fonseca81301932012-11-11 00:10:20 +000078 self.invokeGetProcAddress(module, function)
José Fonseca669b2002011-02-20 13:32:19 +000079 print ' %s%s(%s);' % (ret, pvalue, ', '.join([str(arg.name) for arg in function.args]))
80 print '}'
81 print
José Fonseca669b2002011-02-20 13:32:19 +000082
José Fonseca81301932012-11-11 00:10:20 +000083 def getProcAddressName(self, module, function):
José Fonseca7989f932014-07-17 19:45:15 +010084 raise NotImplementedError
José Fonseca0e5d1ff2012-04-22 10:12:46 +010085
José Fonseca81301932012-11-11 00:10:20 +000086 def invokeGetProcAddress(self, module, function):
José Fonseca669b2002011-02-20 13:32:19 +000087 ptype = function_pointer_type(function)
88 pvalue = function_pointer_value(function)
José Fonseca81301932012-11-11 00:10:20 +000089 getProcAddressName = self.getProcAddressName(module, function)
José Fonseca669b2002011-02-20 13:32:19 +000090 print ' if (!%s) {' % (pvalue,)
José Fonseca0e5d1ff2012-04-22 10:12:46 +010091 print ' %s = (%s)%s(_name);' % (pvalue, ptype, getProcAddressName)
José Fonseca669b2002011-02-20 13:32:19 +000092 print ' if (!%s) {' % (pvalue,)
José Fonseca54f304a2012-01-14 19:33:08 +000093 self.failFunction(function)
José Fonseca669b2002011-02-20 13:32:19 +000094 print ' }'
95 print ' }'
96
José Fonseca54f304a2012-01-14 19:33:08 +000097 def failFunction(self, function):
José Fonsecabd31b462011-06-06 19:38:22 +010098 if function.type is stdapi.Void or function.fail is not None:
José Fonseca632a78d2012-04-19 07:18:59 +010099 print r' os::log("warning: ignoring call to unavailable function %s\n", _name);'
José Fonseca669b2002011-02-20 13:32:19 +0000100 if function.type is stdapi.Void:
José Fonsecabd31b462011-06-06 19:38:22 +0100101 assert function.fail is None
José Fonseca669b2002011-02-20 13:32:19 +0000102 print ' return;'
103 else:
José Fonsecabd31b462011-06-06 19:38:22 +0100104 assert function.fail is not None
José Fonseca669b2002011-02-20 13:32:19 +0000105 print ' return %s;' % function.fail
106 else:
José Fonseca632a78d2012-04-19 07:18:59 +0100107 print r' os::log("error: unavailable function %s\n", _name);'
José Fonseca559d5342011-10-27 08:10:56 +0100108 print r' os::abort();'
José Fonseca669b2002011-02-20 13:32:19 +0000109
110