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 | |
| 31 | import stdapi |
| 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); |
| 52 | # static void __abort(void); |
| 53 | # |
| 54 | raise NotImplementedError |
| 55 | |
| 56 | def dispatch_api(self, api): |
| 57 | for function in api.functions: |
| 58 | self.dispatch_function(function) |
| 59 | |
| 60 | # define standard name aliases for convenience, but only when not |
| 61 | # tracing, as that would cause symbol clashing with the tracing |
| 62 | # functions |
| 63 | print '#ifdef RETRACE' |
| 64 | for function in api.functions: |
| 65 | if not self.is_public_function(function): |
| 66 | print '#define %s __%s' % (function.name, function.name) |
| 67 | print '#endif /* RETRACE */' |
| 68 | print |
| 69 | |
| 70 | def dispatch_function(self, function): |
| 71 | if self.is_public_function(function): |
| 72 | print '#ifndef RETRACE' |
| 73 | print |
| 74 | ptype = function_pointer_type(function) |
| 75 | pvalue = function_pointer_value(function) |
| 76 | print 'typedef ' + function.prototype('* %s' % ptype) + ';' |
| 77 | print 'static %s %s = NULL;' % (ptype, pvalue) |
| 78 | print |
| 79 | print 'static inline ' + function.prototype('__' + function.name) + ' {' |
| 80 | if function.type is stdapi.Void: |
| 81 | ret = '' |
| 82 | else: |
| 83 | ret = 'return ' |
| 84 | self.get_true_pointer(function) |
| 85 | print ' %s%s(%s);' % (ret, pvalue, ', '.join([str(arg.name) for arg in function.args])) |
| 86 | print '}' |
| 87 | print |
| 88 | if self.is_public_function(function): |
| 89 | print '#endif /* !RETRACE */' |
| 90 | print |
| 91 | |
| 92 | def is_public_function(self, function): |
| 93 | return True |
| 94 | |
| 95 | def get_true_pointer(self, function): |
| 96 | ptype = function_pointer_type(function) |
| 97 | pvalue = function_pointer_value(function) |
| 98 | if self.is_public_function(function): |
| 99 | get_proc_address = '__getPublicProcAddress' |
| 100 | else: |
| 101 | get_proc_address = '__getPrivateProcAddress' |
| 102 | print ' if (!%s) {' % (pvalue,) |
| 103 | print ' %s = (%s)%s("%s");' % (pvalue, ptype, get_proc_address, function.name) |
| 104 | print ' if (!%s) {' % (pvalue,) |
| 105 | self.fail_function(function) |
| 106 | print ' }' |
| 107 | print ' }' |
| 108 | |
| 109 | def fail_function(self, function): |
| 110 | print ' OS::DebugMessage("error: unavailable function \\"%s\\"\\n");' % function.name |
| 111 | if function.fail is not None: |
| 112 | if function.type is stdapi.Void: |
| 113 | assert function.fail == '' |
| 114 | print ' return;' |
| 115 | else: |
| 116 | assert function.fail != '' |
| 117 | print ' return %s;' % function.fail |
| 118 | else: |
| 119 | print ' __abort();' |
| 120 | |
| 121 | |