Debian Codegraph @

rpython/rlib/jit_libffi.py

raw symbols SCIP
1 import sys
2 from rpython.rtyper.lltypesystem import lltype, llmemory, rffi
3 from rpython.rtyper.lltypesystem.lloperation import llop
4 from rpython.rlib import clibffi, jit
5 from rpython.rlib.rarithmetic import r_longlong, r_singlefloat
6 from rpython.rlib.unroll import unrolling_iterable
7
8 FFI_CIF = clibffi.FFI_CIFP.TO
9 FFI_TYPE = clibffi.FFI_TYPE_P.TO
10 FFI_TYPE_P = clibffi.FFI_TYPE_P
11 FFI_TYPE_PP = clibffi.FFI_TYPE_PP
12 FFI_ABI = clibffi.FFI_ABI
13 FFI_TYPE_STRUCT = clibffi.FFI_TYPE_STRUCT
14 SIZE_OF_FFI_ARG = rffi.sizeof(clibffi.ffi_arg)
15 SIZE_OF_SIGNED = rffi.sizeof(lltype.Signed)
16 FFI_ARG_P = rffi.CArrayPtr(clibffi.ffi_arg)
17
18 # Usage: for each C function, make one CIF_DESCRIPTION block of raw
19 # memory. Initialize it by filling all its fields apart from 'cif'.
20 # The 'atypes' points to an array of ffi_type pointers; a reasonable
21 # place to locate this array's memory is in the same block of raw
22 # memory, by allocating more than sizeof(CIF_DESCRIPTION).
23 #
24 # The four fields 'abi', 'nargs', 'rtype', 'atypes' are the same as
25 # the arguments to ffi_prep_cif().
26 #
27 # Following this, we find jit_libffi-specific information:
28 #
29 # - 'exchange_size': an integer that tells how big a buffer we must
30 # allocate to do the call; this buffer should have enough room at the
31 # beginning for an array of NARGS pointers which is initialized
32 # internally by jit_ffi_call().
33 #
34 # - 'exchange_result': the offset in that buffer for the result of the call.
35 # (this and the other offsets must be at least NARGS * sizeof(void*).)
36 #
37 # - 'exchange_args[nargs]': the offset in that buffer for each argument.
38 #
39 # Each argument and the result should have enough room for at least
40 # SIZE_OF_FFI_ARG bytes, even if they may be smaller. (Unlike ffi_call,
41 # we don't have any special rule about results that are integers smaller
42 # than SIZE_OF_FFI_ARG).
43
44 CIF_DESCRIPTION = lltype.Struct(
45 'CIF_DESCRIPTION',
46 ('cif', FFI_CIF),
47 ('abi', lltype.Signed), # these 4 fields could also be read directly
48 ('nargs', lltype.Signed), # from 'cif', but doing so adds a dependency
49 ('rtype', FFI_TYPE_P), # on the exact fields available from ffi_cif.
50 ('atypes', FFI_TYPE_PP), #
51 ('exchange_size', lltype.Signed),
52 ('exchange_result', lltype.Signed),
53 ('exchange_args', lltype.Array(lltype.Signed,
54 hints={'nolength': True, 'immutable': True})),
55 hints={'immutable': True})
56
57 CIF_DESCRIPTION_P = lltype.Ptr(CIF_DESCRIPTION)
58
59
60 def jit_ffi_prep_cif(cif_description):
61 """Minimal wrapper around ffi_prep_cif(). Call this after
62 cif_description is initialized, in order to fill the last field: 'cif'.
63 """
64 res = clibffi.c_ffi_prep_cif(cif_description.cif,
65 cif_description.abi,
66 cif_description.nargs,
67 cif_description.rtype,
68 cif_description.atypes)
69 return rffi.cast(lltype.Signed, res)
70
71
72 # =============================
73 # jit_ffi_call and its helpers
74 # =============================
75
76 ## Problem: jit_ffi_call is turned into call_release_gil by pyjitpl. Before
77 ## the refactor-call_release_gil branch, the resulting code looked like this:
78 ##
79 ## buffer = ...
80 ## i0 = call_release_gil(...)
81 ## guard_not_forced()
82 ## setarray_item_raw(buffer, ..., i0)
83 ##
84 ## The problem is that the result box i0 was generated freshly inside pyjitpl,
85 ## and the codewriter did not know about its liveness: the result was that i0
86 ## was not in the fail_args of guard_not_forced. See
87 ## test_fficall::test_guard_not_forced_fails for a more detalied explanation
88 ## of the problem.
89 ##
90 ## The solution is to create a new separate operation libffi_call.
91 ## The result is that now the jitcode looks like this:
92 ##
93 ## %i0 = direct_call(libffi_call, ...)
94 ## -live-
95 ## raw_store(exchange_result, %i0)
96 ##
97 ## the "-live-" is the key, because it make sure that the value is not lost if
98 ## guard_not_forced fails.
99 ##
100 ## The value of %i0 is stored back in the exchange_buffer at the offset
101 ## exchange_result, which is usually where functions like jit_ffi_call_impl_int
102 ## have just read it from when called *in interpreter mode* only.
103
104
105 def jit_ffi_call(cif_description, func_addr, exchange_buffer):
106 """Wrapper around ffi_call(). Must receive a CIF_DESCRIPTION_P that
107 describes the layout of the 'exchange_buffer'.
108
109 Note that this cannot be optimized if 'cif_description' is not
110 a constant for the JIT, so if it is ever possible, consider promoting
111 it. The promotion of 'cif_description' must be done earlier, before
112 the raw malloc of 'exchange_buffer'.
113 """
114 reskind = types.getkind(cif_description.rtype)
115 if reskind == 'v':
116 jit_ffi_call_impl_void(cif_description, func_addr, exchange_buffer)
117 elif reskind == 'i':
118 _do_ffi_call_sint(cif_description, func_addr, exchange_buffer)
119 elif reskind == 'u':
120 _do_ffi_call_uint(cif_description, func_addr, exchange_buffer)
121 elif reskind == 'f':
122 _do_ffi_call_float(cif_description, func_addr, exchange_buffer)
123 elif reskind == 'L': # L is for longlongs, on 32bit
124 _do_ffi_call_longlong(cif_description, func_addr, exchange_buffer)
125 elif reskind == 'S': # SingleFloat
126 _do_ffi_call_singlefloat(cif_description, func_addr, exchange_buffer)
127 else:
128 # the result kind is not supported: we disable the jit_ffi_call
129 # optimization by calling directly jit_ffi_call_impl_any, so the JIT
130 # does not see any libffi_call oopspec.
131 #
132 # Since call_release_gil is not generated, there is no need to
133 # jit_ffi_save_result
134 jit_ffi_call_impl_any(cif_description, func_addr, exchange_buffer)
135
136
137 _short_sint_types = unrolling_iterable([rffi.SIGNEDCHAR, rffi.SHORT, rffi.INT])
138 _short_uint_types = unrolling_iterable([rffi.UCHAR, rffi.USHORT, rffi.UINT])
139
140 def _do_ffi_call_sint(cif_description, func_addr, exchange_buffer):
141 result = jit_ffi_call_impl_int(cif_description, func_addr,
142 exchange_buffer)
143 size = types.getsize(cif_description.rtype)
144 for TP in _short_sint_types: # short **signed** types
145 if size == rffi.sizeof(TP):
146 llop.raw_store(lltype.Void,
147 llmemory.cast_ptr_to_adr(exchange_buffer),
148 cif_description.exchange_result,
149 rffi.cast(TP, result))
150 break
151 else:
152 # default case: expect a full signed number
153 llop.raw_store(lltype.Void,
154 llmemory.cast_ptr_to_adr(exchange_buffer),
155 cif_description.exchange_result,
156 result)
157
158 def _do_ffi_call_uint(cif_description, func_addr, exchange_buffer):
159 result = jit_ffi_call_impl_int(cif_description, func_addr,
160 exchange_buffer)
161 size = types.getsize(cif_description.rtype)
162 for TP in _short_uint_types: # short **unsigned** types
163 if size == rffi.sizeof(TP):
164 llop.raw_store(lltype.Void,
165 llmemory.cast_ptr_to_adr(exchange_buffer),
166 cif_description.exchange_result,
167 rffi.cast(TP, result))
168 break
169 else:
170 # default case: expect a full unsigned number
171 llop.raw_store(lltype.Void,
172 llmemory.cast_ptr_to_adr(exchange_buffer),
173 cif_description.exchange_result,
174 rffi.cast(lltype.Unsigned, result))
175
176 def _do_ffi_call_float(cif_description, func_addr, exchange_buffer):
177 # a separate function in case the backend doesn't support floats
178 result = jit_ffi_call_impl_float(cif_description, func_addr,
179 exchange_buffer)
180 llop.raw_store(lltype.Void,
181 llmemory.cast_ptr_to_adr(exchange_buffer),
182 cif_description.exchange_result,
183 result)
184
185 def _do_ffi_call_longlong(cif_description, func_addr, exchange_buffer):
186 # a separate function in case the backend doesn't support longlongs
187 result = jit_ffi_call_impl_longlong(cif_description, func_addr,
188 exchange_buffer)
189 llop.raw_store(lltype.Void,
190 llmemory.cast_ptr_to_adr(exchange_buffer),
191 cif_description.exchange_result,
192 result)
193
194 def _do_ffi_call_singlefloat(cif_description, func_addr, exchange_buffer):
195 # a separate function in case the backend doesn't support singlefloats
196 result = jit_ffi_call_impl_singlefloat(cif_description, func_addr,
197 exchange_buffer)
198 llop.raw_store(lltype.Void,
199 llmemory.cast_ptr_to_adr(exchange_buffer),
200 cif_description.exchange_result,
201 result)
202
203
204 @jit.oopspec("libffi_call(cif_description,func_addr,exchange_buffer)")
205 def jit_ffi_call_impl_int(cif_description, func_addr, exchange_buffer):
206 jit_ffi_call_impl_any(cif_description, func_addr, exchange_buffer)
207 # read a complete 'ffi_arg' word
208 resultdata = rffi.ptradd(exchange_buffer, cif_description.exchange_result)
209 return rffi.cast(lltype.Signed, rffi.cast(FFI_ARG_P, resultdata)[0])
210
211 @jit.oopspec("libffi_call(cif_description,func_addr,exchange_buffer)")
212 def jit_ffi_call_impl_float(cif_description, func_addr, exchange_buffer):
213 jit_ffi_call_impl_any(cif_description, func_addr, exchange_buffer)
214 resultdata = rffi.ptradd(exchange_buffer, cif_description.exchange_result)
215 return rffi.cast(rffi.DOUBLEP, resultdata)[0]
216
217 @jit.oopspec("libffi_call(cif_description,func_addr,exchange_buffer)")
218 def jit_ffi_call_impl_longlong(cif_description, func_addr, exchange_buffer):
219 jit_ffi_call_impl_any(cif_description, func_addr, exchange_buffer)
220 resultdata = rffi.ptradd(exchange_buffer, cif_description.exchange_result)
221 return rffi.cast(rffi.LONGLONGP, resultdata)[0]
222
223 @jit.oopspec("libffi_call(cif_description,func_addr,exchange_buffer)")
224 def jit_ffi_call_impl_singlefloat(cif_description, func_addr, exchange_buffer):
225 jit_ffi_call_impl_any(cif_description, func_addr, exchange_buffer)
226 resultdata = rffi.ptradd(exchange_buffer, cif_description.exchange_result)
227 return rffi.cast(rffi.FLOATP, resultdata)[0]
228
229 @jit.oopspec("libffi_call(cif_description,func_addr,exchange_buffer)")
230 def jit_ffi_call_impl_void(cif_description, func_addr, exchange_buffer):
231 jit_ffi_call_impl_any(cif_description, func_addr, exchange_buffer)
232 return None
233
234 def jit_ffi_call_impl_any(cif_description, func_addr, exchange_buffer):
235 """
236 This is the function which actually calls libffi. All the rest is just
237 infrastructure to convince the JIT to pass a typed result box to
238 jit_ffi_save_result
239 """
240 buffer_array = rffi.cast(rffi.VOIDPP, exchange_buffer)
241 for i in range(cif_description.nargs):
242 data = rffi.ptradd(exchange_buffer, cif_description.exchange_args[i])
243 buffer_array[i] = data
244 resultdata = rffi.ptradd(exchange_buffer,
245 cif_description.exchange_result)
246 clibffi.c_ffi_call(cif_description.cif, func_addr,
247 rffi.cast(rffi.VOIDP, resultdata),
248 buffer_array)
249
250
251 # ____________________________________________________________
252
253 class types(object):
254 """
255 This namespace contains the mapping the JIT needs from ffi types to
256 a less strict "kind" character.
257 """
258
259 @classmethod
260 def _import(cls):
261 prefix = 'ffi_type_'
262 for key, value in clibffi.__dict__.iteritems():
263 if key.startswith(prefix):
264 name = key[len(prefix):]
265 setattr(cls, name, value)
266 cls.slong = clibffi.cast_type_to_ffitype(rffi.LONG)
267 cls.ulong = clibffi.cast_type_to_ffitype(rffi.ULONG)
268 cls.slonglong = clibffi.cast_type_to_ffitype(rffi.LONGLONG)
269 cls.ulonglong = clibffi.cast_type_to_ffitype(rffi.ULONGLONG)
270 cls.signed = clibffi.cast_type_to_ffitype(rffi.SIGNED)
271 cls.unsigned = clibffi.cast_type_to_ffitype(rffi.UNSIGNED)
272 cls.wchar_t = clibffi.cast_type_to_ffitype(lltype.UniChar)
273 del cls._import
274
275 @staticmethod
276 @jit.elidable
277 def getkind(ffi_type):
278 """Returns 'v' for void, 'f' for float, 'i' for signed integer,
279 'u' for unsigned integer, 'S' for singlefloat, 'L' for long long
280 integer (signed or unsigned), '*' for struct, or '?' for others
281 (e.g. long double).
282 """
283 if ffi_type == types.void: return 'v'
284 elif ffi_type == types.double: return 'f'
285 elif ffi_type == types.float: return 'S'
286 elif ffi_type == types.pointer: return 'u'
287 #
288 elif ffi_type == types.schar: return 'i'
289 elif ffi_type == types.uchar: return 'u'
290 elif ffi_type == types.sshort: return 'i'
291 elif ffi_type == types.ushort: return 'u'
292 elif ffi_type == types.sint: return 'i'
293 elif ffi_type == types.uint: return 'u'
294 elif ffi_type == types.slong: return 'i'
295 elif ffi_type == types.ulong: return 'u'
296 #
297 elif ffi_type == types.sint8: return 'i'
298 elif ffi_type == types.uint8: return 'u'
299 elif ffi_type == types.sint16: return 'i'
300 elif ffi_type == types.uint16: return 'u'
301 elif ffi_type == types.sint32: return 'i'
302 elif ffi_type == types.uint32: return 'u'
303 ## (for Win64, ffi_type == types.signed is not caught above)
304 elif ffi_type == types.signed: return 'i'
305 elif ffi_type == types.unsigned:return 'u'
306 ## (note that on 64-bit platforms, types.sint64 == types.slong and the
307 ## case == caught above)
308 elif ffi_type == types.sint64: return 'L'
309 elif ffi_type == types.uint64: return 'L'
310 #
311 elif types.is_struct(ffi_type): return '*'
312 return '?'
313
314 @staticmethod
315 @jit.elidable
316 def getsize(ffi_type):
317 return rffi.getintfield(ffi_type, 'c_size')
318
319 @staticmethod
320 @jit.elidable
321 def is_struct(ffi_type):
322 return rffi.getintfield(ffi_type, 'c_type') == FFI_TYPE_STRUCT
323
324 types._import()