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 | bd86a22 | 2011-09-27 09:21:38 +0100 | [diff] [blame^] | 31 | import specs.stdapi as stdapi |
José Fonseca | 669b200 | 2011-02-20 13:32:19 +0000 | [diff] [blame] | 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); |
José Fonseca | 669b200 | 2011-02-20 13:32:19 +0000 | [diff] [blame] | 52 | # |
| 53 | raise NotImplementedError |
| 54 | |
| 55 | def dispatch_api(self, api): |
| 56 | for function in api.functions: |
| 57 | self.dispatch_function(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: |
José Fonseca | b794df1 | 2011-04-12 08:28:45 +0100 | [diff] [blame] | 64 | if self.is_public_function(function): |
| 65 | print '#define __%s %s' % (function.name, function.name) |
| 66 | else: |
José Fonseca | 669b200 | 2011-02-20 13:32:19 +0000 | [diff] [blame] | 67 | print '#define %s __%s' % (function.name, function.name) |
| 68 | print '#endif /* RETRACE */' |
| 69 | print |
| 70 | |
| 71 | def dispatch_function(self, function): |
| 72 | if self.is_public_function(function): |
| 73 | print '#ifndef RETRACE' |
| 74 | print |
| 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) |
| 79 | print |
| 80 | print 'static inline ' + function.prototype('__' + function.name) + ' {' |
José Fonseca | 3878cab | 2011-06-04 14:23:53 +0100 | [diff] [blame] | 81 | print ' const char *__name = "%s";' % function.name |
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 ' |
| 86 | self.get_true_pointer(function) |
| 87 | print ' %s%s(%s);' % (ret, pvalue, ', '.join([str(arg.name) for arg in function.args])) |
| 88 | print '}' |
| 89 | print |
| 90 | if self.is_public_function(function): |
| 91 | print '#endif /* !RETRACE */' |
| 92 | print |
| 93 | |
| 94 | def is_public_function(self, function): |
| 95 | return True |
| 96 | |
| 97 | def get_true_pointer(self, function): |
| 98 | ptype = function_pointer_type(function) |
| 99 | pvalue = function_pointer_value(function) |
| 100 | if self.is_public_function(function): |
| 101 | get_proc_address = '__getPublicProcAddress' |
| 102 | else: |
| 103 | get_proc_address = '__getPrivateProcAddress' |
| 104 | print ' if (!%s) {' % (pvalue,) |
José Fonseca | 3878cab | 2011-06-04 14:23:53 +0100 | [diff] [blame] | 105 | print ' %s = (%s)%s(__name);' % (pvalue, ptype, get_proc_address) |
José Fonseca | 669b200 | 2011-02-20 13:32:19 +0000 | [diff] [blame] | 106 | print ' if (!%s) {' % (pvalue,) |
| 107 | self.fail_function(function) |
| 108 | print ' }' |
| 109 | print ' }' |
| 110 | |
| 111 | def fail_function(self, function): |
José Fonseca | bd31b46 | 2011-06-06 19:38:22 +0100 | [diff] [blame] | 112 | if function.type is stdapi.Void or function.fail is not None: |
| 113 | print r' OS::DebugMessage("warning: ignoring call to unavailable function %s\n", __name);' |
José Fonseca | 669b200 | 2011-02-20 13:32:19 +0000 | [diff] [blame] | 114 | if function.type is stdapi.Void: |
José Fonseca | bd31b46 | 2011-06-06 19:38:22 +0100 | [diff] [blame] | 115 | assert function.fail is None |
José Fonseca | 669b200 | 2011-02-20 13:32:19 +0000 | [diff] [blame] | 116 | print ' return;' |
| 117 | else: |
José Fonseca | bd31b46 | 2011-06-06 19:38:22 +0100 | [diff] [blame] | 118 | assert function.fail is not None |
José Fonseca | 669b200 | 2011-02-20 13:32:19 +0000 | [diff] [blame] | 119 | print ' return %s;' % function.fail |
| 120 | else: |
José Fonseca | bd31b46 | 2011-06-06 19:38:22 +0100 | [diff] [blame] | 121 | print r' OS::DebugMessage("error: unavailable function %s\n", __name);' |
| 122 | print r' OS::Abort();' |
José Fonseca | 669b200 | 2011-02-20 13:32:19 +0000 | [diff] [blame] | 123 | |
| 124 | |