Debian Codegraph @

rpython/rtyper/extfunc.py

raw symbols SCIP
1 from rpython.annotator.model import unionof, SomeObject
2 from rpython.annotator.signature import annotation, SignatureError
3 from rpython.rtyper.extregistry import ExtRegistryEntry, lookup
4 from rpython.rtyper.lltypesystem.lltype import (
5 typeOf, FuncType, functionptr, _ptr, Void)
6 from rpython.rtyper.error import TyperError
7 from rpython.rtyper.rmodel import Repr
8
9 class SomeExternalFunction(SomeObject):
10 def __init__(self, name, args_s, s_result):
11 self.name = name
12 self.args_s = args_s
13 self.s_result = s_result
14
15 def check_args(self, callspec):
16 params_s = self.args_s
17 args_s, kwargs = callspec.unpack()
18 if kwargs:
19 raise SignatureError(
20 "External functions cannot be called with keyword arguments")
21 if len(args_s) != len(params_s):
22 raise SignatureError("Argument number mismatch")
23 for i, s_param in enumerate(params_s):
24 arg = unionof(args_s[i], s_param)
25 if not s_param.contains(arg):
26 raise SignatureError(
27 "In call to external function %r:\n"
28 "arg %d must be %s,\n"
29 " got %s" % (
30 self.name, i + 1, s_param, args_s[i]))
31
32 def call(self, callspec):
33 self.check_args(callspec)
34 return self.s_result
35
36 def rtyper_makerepr(self, rtyper):
37 if not self.is_constant():
38 raise TyperError("Non-constant external function!")
39 entry = lookup(self.const)
40 impl = getattr(entry, 'lltypeimpl', None)
41 fakeimpl = getattr(entry, 'lltypefakeimpl', None)
42 return ExternalFunctionRepr(self, impl, fakeimpl)
43
44 def rtyper_makekey(self):
45 return self.__class__, self
46
47 class ExternalFunctionRepr(Repr):
48 lowleveltype = Void
49
50 def __init__(self, s_func, impl, fakeimpl):
51 self.s_func = s_func
52 self.impl = impl
53 self.fakeimpl = fakeimpl
54
55 def rtype_simple_call(self, hop):
56 rtyper = hop.rtyper
57 args_r = [rtyper.getrepr(s_arg) for s_arg in self.s_func.args_s]
58 r_result = rtyper.getrepr(self.s_func.s_result)
59 obj = self.get_funcptr(rtyper, args_r, r_result)
60 hop2 = hop.copy()
61 hop2.r_s_popfirstarg()
62 vlist = [hop2.inputconst(typeOf(obj), obj)] + hop2.inputargs(*args_r)
63 hop2.exception_is_here()
64 return hop2.genop('direct_call', vlist, r_result)
65
66 def get_funcptr(self, rtyper, args_r, r_result):
67 from rpython.rtyper.rtyper import llinterp_backend
68 args_ll = [r_arg.lowleveltype for r_arg in args_r]
69 ll_result = r_result.lowleveltype
70 name = self.s_func.name
71 if self.fakeimpl and rtyper.backend is llinterp_backend:
72 FT = FuncType(args_ll, ll_result)
73 return functionptr(
74 FT, name, _external_name=name, _callable=self.fakeimpl)
75 elif self.impl:
76 if isinstance(self.impl, _ptr):
77 return self.impl
78 else:
79 # store some attributes to the 'impl' function, where
80 # the eventual call to rtyper.getcallable() will find them
81 # and transfer them to the final lltype.functionptr().
82 self.impl._llfnobjattrs_ = {'_name': name}
83 return rtyper.getannmixlevel().delayedfunction(
84 self.impl, self.s_func.args_s, self.s_func.s_result)
85 else:
86 fakeimpl = self.fakeimpl or self.s_func.const
87 FT = FuncType(args_ll, ll_result)
88 return functionptr(
89 FT, name, _external_name=name, _callable=fakeimpl)
90
91
92 class ExtFuncEntry(ExtRegistryEntry):
93 safe_not_sandboxed = False
94
95 def compute_annotation(self):
96 s_result = SomeExternalFunction(
97 self.name, self.signature_args, self.signature_result)
98 if (self.bookkeeper.annotator.translator.config.translation.sandbox
99 and not self.safe_not_sandboxed):
100 s_result.needs_sandboxing = True
101 return s_result
102
103
104 def register_external(function, args, result=None, export_name=None,
105 llimpl=None, llfakeimpl=None, sandboxsafe=False):
106 """
107 function: the RPython function that will be rendered as an external function (e.g.: math.floor)
108 args: a list containing the annotation of the arguments
109 result: surprisingly enough, the annotation of the result
110 export_name: the name of the function as it will be seen by the backends
111 llimpl: optional; if provided, this RPython function is called instead of the target function
112 llfakeimpl: optional; if provided, called by the llinterpreter
113 sandboxsafe: use True if the function performs no I/O (safe for --sandbox)
114 """
115
116 if export_name is None:
117 export_name = function.__name__
118 params_s = [annotation(arg) for arg in args]
119 s_result = annotation(result)
120
121 class FunEntry(ExtFuncEntry):
122 _about_ = function
123 safe_not_sandboxed = sandboxsafe
124 signature_args = params_s
125 signature_result = s_result
126 name = export_name
127 if llimpl:
128 lltypeimpl = staticmethod(llimpl)
129 if llfakeimpl:
130 lltypefakeimpl = staticmethod(llfakeimpl)
131
132 def is_external(func):
133 if hasattr(func, 'value'):
134 func = func.value
135 if hasattr(func, '_external_name'):
136 return True
137 return False