blob: 491db91b5f98ed9bd5641e72b3c5700033c0b59c [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):
41 return '__PFN' + function.name.upper()
42
43
44def function_pointer_value(function):
45 return '__' + function.name + '_ptr'
46
47
48class Dispatcher:
49
50 def header(self):
51 # Must be implemented by derived classes, which should define, declare,
52 # or implement something like:
53 #
54 # typedef void (*__PROC)(void);
55 #
56 # static __PROC __getPublicProcAddress(const char *name);
57 # static __PROC __getPrivateProcAddress(const char *name);
José Fonseca669b2002011-02-20 13:32:19 +000058 #
59 raise NotImplementedError
60
61 def dispatch_api(self, api):
62 for function in api.functions:
José Fonseca54f304a2012-01-14 19:33:08 +000063 self.invokeFunction(function)
José Fonseca669b2002011-02-20 13:32:19 +000064
65 # define standard name aliases for convenience, but only when not
66 # tracing, as that would cause symbol clashing with the tracing
67 # functions
68 print '#ifdef RETRACE'
69 for function in api.functions:
Alexandros Frantzis486708b2011-12-01 15:52:32 +020070 print '#define %s __%s' % (function.name, function.name)
José Fonseca669b2002011-02-20 13:32:19 +000071 print '#endif /* RETRACE */'
72 print
73
José Fonseca54f304a2012-01-14 19:33:08 +000074 def invokeFunction(self, function):
José Fonseca669b2002011-02-20 13:32:19 +000075 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é Fonseca3878cab2011-06-04 14:23:53 +010081 print ' const char *__name = "%s";' % function.name
José Fonseca669b2002011-02-20 13:32:19 +000082 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
José Fonseca669b2002011-02-20 13:32:19 +000090
José Fonseca54f304a2012-01-14 19:33:08 +000091 def isFunctionPublic(self, function):
José Fonseca669b2002011-02-20 13:32:19 +000092 return True
93
94 def get_true_pointer(self, function):
95 ptype = function_pointer_type(function)
96 pvalue = function_pointer_value(function)
José Fonseca54f304a2012-01-14 19:33:08 +000097 if self.isFunctionPublic(function):
José Fonseca669b2002011-02-20 13:32:19 +000098 get_proc_address = '__getPublicProcAddress'
99 else:
100 get_proc_address = '__getPrivateProcAddress'
101 print ' if (!%s) {' % (pvalue,)
José Fonseca3878cab2011-06-04 14:23:53 +0100102 print ' %s = (%s)%s(__name);' % (pvalue, ptype, get_proc_address)
José Fonseca669b2002011-02-20 13:32:19 +0000103 print ' if (!%s) {' % (pvalue,)
José Fonseca54f304a2012-01-14 19:33:08 +0000104 self.failFunction(function)
José Fonseca669b2002011-02-20 13:32:19 +0000105 print ' }'
106 print ' }'
107
José Fonseca54f304a2012-01-14 19:33:08 +0000108 def failFunction(self, function):
José Fonsecabd31b462011-06-06 19:38:22 +0100109 if function.type is stdapi.Void or function.fail is not None:
José Fonseca559d5342011-10-27 08:10:56 +0100110 print r' os::log("warning: ignoring call to unavailable function %s\n", __name);'
José Fonseca669b2002011-02-20 13:32:19 +0000111 if function.type is stdapi.Void:
José Fonsecabd31b462011-06-06 19:38:22 +0100112 assert function.fail is None
José Fonseca669b2002011-02-20 13:32:19 +0000113 print ' return;'
114 else:
José Fonsecabd31b462011-06-06 19:38:22 +0100115 assert function.fail is not None
José Fonseca669b2002011-02-20 13:32:19 +0000116 print ' return %s;' % function.fail
117 else:
José Fonseca559d5342011-10-27 08:10:56 +0100118 print r' os::log("error: unavailable function %s\n", __name);'
119 print r' os::abort();'
José Fonseca669b2002011-02-20 13:32:19 +0000120
121