José Fonseca | 669b200 | 2011-02-20 13:32:19 +0000 | [diff] [blame] | 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 | |
José Fonseca | 4f242f4 | 2012-04-14 18:13:25 +0100 | [diff] [blame] | 31 | # Adjust path |
| 32 | import os.path |
| 33 | import sys |
| 34 | sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) |
| 35 | |
| 36 | |
José Fonseca | bd86a22 | 2011-09-27 09:21:38 +0100 | [diff] [blame] | 37 | import specs.stdapi as stdapi |
José Fonseca | 669b200 | 2011-02-20 13:32:19 +0000 | [diff] [blame] | 38 | |
| 39 | |
| 40 | def function_pointer_type(function): |
José Fonseca | 632a78d | 2012-04-19 07:18:59 +0100 | [diff] [blame] | 41 | return 'PFN_' + function.name.upper() |
José Fonseca | 669b200 | 2011-02-20 13:32:19 +0000 | [diff] [blame] | 42 | |
| 43 | |
| 44 | def function_pointer_value(function): |
José Fonseca | e91f5bc | 2014-07-17 20:42:35 +0100 | [diff] [blame] | 45 | return '_' + function.name |
José Fonseca | 669b200 | 2011-02-20 13:32:19 +0000 | [diff] [blame] | 46 | |
| 47 | |
| 48 | class Dispatcher: |
| 49 | |
José Fonseca | 8130193 | 2012-11-11 00:10:20 +0000 | [diff] [blame] | 50 | def dispatchModule(self, module): |
José Fonseca | e91f5bc | 2014-07-17 20:42:35 +0100 | [diff] [blame] | 51 | self.dispatchModuleDecl(module) |
| 52 | self.dispatchModuleImpl(module) |
| 53 | |
| 54 | def dispatchModuleDecl(self, module): |
José Fonseca | 8130193 | 2012-11-11 00:10:20 +0000 | [diff] [blame] | 55 | for function in module.functions: |
José Fonseca | e91f5bc | 2014-07-17 20:42:35 +0100 | [diff] [blame] | 56 | self.dispatchFunctionDecl(module, function) |
José Fonseca | 669b200 | 2011-02-20 13:32:19 +0000 | [diff] [blame] | 57 | |
| 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ły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 61 | print('#ifdef RETRACE') |
José Fonseca | 8130193 | 2012-11-11 00:10:20 +0000 | [diff] [blame] | 62 | for function in module.functions: |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 63 | print('#define %s _%s' % (function.name, function.name)) |
| 64 | print('#endif /* RETRACE */') |
| 65 | print() |
José Fonseca | e91f5bc | 2014-07-17 20:42:35 +0100 | [diff] [blame] | 66 | |
| 67 | def dispatchFunctionDecl(self, module, function): |
José Fonseca | 669b200 | 2011-02-20 13:32:19 +0000 | [diff] [blame] | 68 | ptype = function_pointer_type(function) |
| 69 | pvalue = function_pointer_value(function) |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 70 | print('typedef ' + function.prototype('* %s' % ptype) + ';') |
| 71 | print('extern %s %s;' % (ptype, pvalue)) |
| 72 | print() |
José Fonseca | e91f5bc | 2014-07-17 20:42:35 +0100 | [diff] [blame] | 73 | |
| 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é Fonseca | 669b200 | 2011-02-20 13:32:19 +0000 | [diff] [blame] | 82 | if function.type is stdapi.Void: |
| 83 | ret = '' |
| 84 | else: |
| 85 | ret = 'return ' |
José Fonseca | e91f5bc | 2014-07-17 20:42:35 +0100 | [diff] [blame] | 86 | |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 87 | print('static ' + function.prototype('_fail_' + function.name) + ' {') |
José Fonseca | e91f5bc | 2014-07-17 20:42:35 +0100 | [diff] [blame] | 88 | self.failFunction(function) |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 89 | print('}') |
| 90 | print() |
José Fonseca | e91f5bc | 2014-07-17 20:42:35 +0100 | [diff] [blame] | 91 | |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 92 | print('static ' + function.prototype('_get_' + function.name) + ' {') |
José Fonseca | 8130193 | 2012-11-11 00:10:20 +0000 | [diff] [blame] | 93 | self.invokeGetProcAddress(module, function) |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 94 | print(' %s%s(%s);' % (ret, pvalue, ', '.join([str(arg.name) for arg in function.args]))) |
| 95 | print('}') |
| 96 | print() |
José Fonseca | 669b200 | 2011-02-20 13:32:19 +0000 | [diff] [blame] | 97 | |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 98 | print('%s %s = &%s;' % (ptype, pvalue, '_get_' + function.name)) |
| 99 | print() |
José Fonseca | e91f5bc | 2014-07-17 20:42:35 +0100 | [diff] [blame] | 100 | |
José Fonseca | 8130193 | 2012-11-11 00:10:20 +0000 | [diff] [blame] | 101 | def getProcAddressName(self, module, function): |
José Fonseca | 7989f93 | 2014-07-17 19:45:15 +0100 | [diff] [blame] | 102 | raise NotImplementedError |
José Fonseca | 0e5d1ff | 2012-04-22 10:12:46 +0100 | [diff] [blame] | 103 | |
José Fonseca | 8130193 | 2012-11-11 00:10:20 +0000 | [diff] [blame] | 104 | def invokeGetProcAddress(self, module, function): |
José Fonseca | 669b200 | 2011-02-20 13:32:19 +0000 | [diff] [blame] | 105 | ptype = function_pointer_type(function) |
| 106 | pvalue = function_pointer_value(function) |
José Fonseca | 8130193 | 2012-11-11 00:10:20 +0000 | [diff] [blame] | 107 | getProcAddressName = self.getProcAddressName(module, function) |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 108 | 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é Fonseca | 669b200 | 2011-02-20 13:32:19 +0000 | [diff] [blame] | 114 | |
José Fonseca | 54f304a | 2012-01-14 19:33:08 +0000 | [diff] [blame] | 115 | def failFunction(self, function): |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 116 | print(r' const char *_name = "%s";' % function.name) |
José Fonseca | bd31b46 | 2011-06-06 19:38:22 +0100 | [diff] [blame] | 117 | if function.type is stdapi.Void or function.fail is not None: |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 118 | print(r' os::log("warning: ignoring call to unavailable function %s\n", _name);') |
José Fonseca | 669b200 | 2011-02-20 13:32:19 +0000 | [diff] [blame] | 119 | if function.type is stdapi.Void: |
José Fonseca | bd31b46 | 2011-06-06 19:38:22 +0100 | [diff] [blame] | 120 | assert function.fail is None |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 121 | print(' return;') |
José Fonseca | 669b200 | 2011-02-20 13:32:19 +0000 | [diff] [blame] | 122 | else: |
José Fonseca | bd31b46 | 2011-06-06 19:38:22 +0100 | [diff] [blame] | 123 | assert function.fail is not None |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 124 | print(' return %s;' % function.fail) |
José Fonseca | 669b200 | 2011-02-20 13:32:19 +0000 | [diff] [blame] | 125 | else: |
Piotr Podsiadły | 0b8b019 | 2019-01-03 20:39:55 +0100 | [diff] [blame] | 126 | print(r' os::log("error: unavailable function %s\n", _name);') |
| 127 | print(r' os::abort();') |
José Fonseca | 669b200 | 2011-02-20 13:32:19 +0000 | [diff] [blame] | 128 | |
| 129 | |