blob: e1dd8bc999b13b11de3e95fa06d9aa1cf2aa71f9 [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é Fonsecae91f5bc2014-07-17 20:42:35 +010045 return '_' + function.name
José Fonseca669b2002011-02-20 13:32:19 +000046
47
48class Dispatcher:
49
José Fonseca81301932012-11-11 00:10:20 +000050 def dispatchModule(self, module):
José Fonsecae91f5bc2014-07-17 20:42:35 +010051 self.dispatchModuleDecl(module)
52 self.dispatchModuleImpl(module)
53
54 def dispatchModuleDecl(self, module):
José Fonseca81301932012-11-11 00:10:20 +000055 for function in module.functions:
José Fonsecae91f5bc2014-07-17 20:42:35 +010056 self.dispatchFunctionDecl(module, function)
José Fonseca669b2002011-02-20 13:32:19 +000057
58 # define standard name aliases for convenience, but only when not
59 # tracing, as that would cause symbol clashing with the tracing
60 # functions
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +010061 print('#ifdef RETRACE')
José Fonseca81301932012-11-11 00:10:20 +000062 for function in module.functions:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +010063 print('#define %s _%s' % (function.name, function.name))
64 print('#endif /* RETRACE */')
65 print()
José Fonsecae91f5bc2014-07-17 20:42:35 +010066
67 def dispatchFunctionDecl(self, module, function):
José Fonseca669b2002011-02-20 13:32:19 +000068 ptype = function_pointer_type(function)
69 pvalue = function_pointer_value(function)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +010070 print('typedef ' + function.prototype('* %s' % ptype) + ';')
71 print('extern %s %s;' % (ptype, pvalue))
72 print()
José Fonsecae91f5bc2014-07-17 20:42:35 +010073
74 def dispatchModuleImpl(self, module):
75 for function in module.functions:
76 self.dispatchFunctionImpl(module, function)
77
78 def dispatchFunctionImpl(self, module, function):
79 ptype = function_pointer_type(function)
80 pvalue = function_pointer_value(function)
81
José Fonseca669b2002011-02-20 13:32:19 +000082 if function.type is stdapi.Void:
83 ret = ''
84 else:
85 ret = 'return '
José Fonsecae91f5bc2014-07-17 20:42:35 +010086
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +010087 print('static ' + function.prototype('_fail_' + function.name) + ' {')
José Fonsecae91f5bc2014-07-17 20:42:35 +010088 self.failFunction(function)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +010089 print('}')
90 print()
José Fonsecae91f5bc2014-07-17 20:42:35 +010091
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +010092 print('static ' + function.prototype('_get_' + function.name) + ' {')
José Fonseca81301932012-11-11 00:10:20 +000093 self.invokeGetProcAddress(module, function)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +010094 print(' %s%s(%s);' % (ret, pvalue, ', '.join([str(arg.name) for arg in function.args])))
95 print('}')
96 print()
José Fonseca669b2002011-02-20 13:32:19 +000097
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +010098 print('%s %s = &%s;' % (ptype, pvalue, '_get_' + function.name))
99 print()
José Fonsecae91f5bc2014-07-17 20:42:35 +0100100
José Fonseca81301932012-11-11 00:10:20 +0000101 def getProcAddressName(self, module, function):
José Fonseca7989f932014-07-17 19:45:15 +0100102 raise NotImplementedError
José Fonseca0e5d1ff2012-04-22 10:12:46 +0100103
José Fonseca81301932012-11-11 00:10:20 +0000104 def invokeGetProcAddress(self, module, function):
José Fonseca669b2002011-02-20 13:32:19 +0000105 ptype = function_pointer_type(function)
106 pvalue = function_pointer_value(function)
José Fonseca81301932012-11-11 00:10:20 +0000107 getProcAddressName = self.getProcAddressName(module, function)
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100108 print(' %s _ptr;' % (ptype,))
109 print(' _ptr = (%s)%s("%s");' % (ptype, getProcAddressName, function.name))
110 print(' if (!_ptr) {')
111 print(' _ptr = &%s;' % ('_fail_' + function.name))
112 print(' }')
113 print(' %s = _ptr;' % (pvalue,))
José Fonseca669b2002011-02-20 13:32:19 +0000114
José Fonseca54f304a2012-01-14 19:33:08 +0000115 def failFunction(self, function):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100116 print(r' const char *_name = "%s";' % function.name)
José Fonsecabd31b462011-06-06 19:38:22 +0100117 if function.type is stdapi.Void or function.fail is not None:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100118 print(r' os::log("warning: ignoring call to unavailable function %s\n", _name);')
José Fonseca669b2002011-02-20 13:32:19 +0000119 if function.type is stdapi.Void:
José Fonsecabd31b462011-06-06 19:38:22 +0100120 assert function.fail is None
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100121 print(' return;')
José Fonseca669b2002011-02-20 13:32:19 +0000122 else:
José Fonsecabd31b462011-06-06 19:38:22 +0100123 assert function.fail is not None
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100124 print(' return %s;' % function.fail)
José Fonseca669b2002011-02-20 13:32:19 +0000125 else:
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100126 print(r' os::log("error: unavailable function %s\n", _name);')
127 print(r' os::abort();')
José Fonseca669b2002011-02-20 13:32:19 +0000128
129