1 | n/a | /* ----------------------------------------------------------------------- |
---|
2 | n/a | ffi.c - Copyright (c) 1996, 1998, 1999, 2001 Red Hat, Inc. |
---|
3 | n/a | Copyright (c) 2002 Ranjit Mathew |
---|
4 | n/a | Copyright (c) 2002 Bo Thorsen |
---|
5 | n/a | Copyright (c) 2002 Roger Sayle |
---|
6 | n/a | |
---|
7 | n/a | x86 Foreign Function Interface |
---|
8 | n/a | |
---|
9 | n/a | Permission is hereby granted, free of charge, to any person obtaining |
---|
10 | n/a | a copy of this software and associated documentation files (the |
---|
11 | n/a | ``Software''), to deal in the Software without restriction, including |
---|
12 | n/a | without limitation the rights to use, copy, modify, merge, publish, |
---|
13 | n/a | distribute, sublicense, and/or sell copies of the Software, and to |
---|
14 | n/a | permit persons to whom the Software is furnished to do so, subject to |
---|
15 | n/a | the following conditions: |
---|
16 | n/a | |
---|
17 | n/a | The above copyright notice and this permission notice shall be included |
---|
18 | n/a | in all copies or substantial portions of the Software. |
---|
19 | n/a | |
---|
20 | n/a | THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS |
---|
21 | n/a | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
---|
22 | n/a | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
---|
23 | n/a | IN NO EVENT SHALL CYGNUS SOLUTIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
---|
24 | n/a | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
---|
25 | n/a | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
---|
26 | n/a | OTHER DEALINGS IN THE SOFTWARE. |
---|
27 | n/a | ----------------------------------------------------------------------- */ |
---|
28 | n/a | |
---|
29 | n/a | #include <ffi.h> |
---|
30 | n/a | #include <ffi_common.h> |
---|
31 | n/a | |
---|
32 | n/a | #include <stdlib.h> |
---|
33 | n/a | |
---|
34 | n/a | /* ffi_prep_args is called by the assembly routine once stack space |
---|
35 | n/a | has been allocated for the function's arguments */ |
---|
36 | n/a | |
---|
37 | n/a | extern void Py_FatalError(const char *msg); |
---|
38 | n/a | |
---|
39 | n/a | /*@-exportheader@*/ |
---|
40 | n/a | void ffi_prep_args(char *stack, extended_cif *ecif) |
---|
41 | n/a | /*@=exportheader@*/ |
---|
42 | n/a | { |
---|
43 | n/a | register unsigned int i; |
---|
44 | n/a | register void **p_argv; |
---|
45 | n/a | register char *argp; |
---|
46 | n/a | register ffi_type **p_arg; |
---|
47 | n/a | |
---|
48 | n/a | argp = stack; |
---|
49 | n/a | if (ecif->cif->rtype->type == FFI_TYPE_STRUCT) |
---|
50 | n/a | { |
---|
51 | n/a | *(void **) argp = ecif->rvalue; |
---|
52 | n/a | argp += sizeof(void *); |
---|
53 | n/a | } |
---|
54 | n/a | |
---|
55 | n/a | p_argv = ecif->avalue; |
---|
56 | n/a | |
---|
57 | n/a | for (i = ecif->cif->nargs, p_arg = ecif->cif->arg_types; |
---|
58 | n/a | i != 0; |
---|
59 | n/a | i--, p_arg++) |
---|
60 | n/a | { |
---|
61 | n/a | size_t z; |
---|
62 | n/a | |
---|
63 | n/a | /* Align if necessary */ |
---|
64 | n/a | if ((sizeof(void *) - 1) & (size_t) argp) |
---|
65 | n/a | argp = (char *) ALIGN(argp, sizeof(void *)); |
---|
66 | n/a | |
---|
67 | n/a | z = (*p_arg)->size; |
---|
68 | n/a | if (z < sizeof(intptr_t)) |
---|
69 | n/a | { |
---|
70 | n/a | z = sizeof(intptr_t); |
---|
71 | n/a | switch ((*p_arg)->type) |
---|
72 | n/a | { |
---|
73 | n/a | case FFI_TYPE_SINT8: |
---|
74 | n/a | *(intptr_t *) argp = (intptr_t)*(SINT8 *)(* p_argv); |
---|
75 | n/a | break; |
---|
76 | n/a | |
---|
77 | n/a | case FFI_TYPE_UINT8: |
---|
78 | n/a | *(uintptr_t *) argp = (uintptr_t)*(UINT8 *)(* p_argv); |
---|
79 | n/a | break; |
---|
80 | n/a | |
---|
81 | n/a | case FFI_TYPE_SINT16: |
---|
82 | n/a | *(intptr_t *) argp = (intptr_t)*(SINT16 *)(* p_argv); |
---|
83 | n/a | break; |
---|
84 | n/a | |
---|
85 | n/a | case FFI_TYPE_UINT16: |
---|
86 | n/a | *(uintptr_t *) argp = (uintptr_t)*(UINT16 *)(* p_argv); |
---|
87 | n/a | break; |
---|
88 | n/a | |
---|
89 | n/a | case FFI_TYPE_SINT32: |
---|
90 | n/a | *(intptr_t *) argp = (intptr_t)*(SINT32 *)(* p_argv); |
---|
91 | n/a | break; |
---|
92 | n/a | |
---|
93 | n/a | case FFI_TYPE_UINT32: |
---|
94 | n/a | *(uintptr_t *) argp = (uintptr_t)*(UINT32 *)(* p_argv); |
---|
95 | n/a | break; |
---|
96 | n/a | |
---|
97 | n/a | case FFI_TYPE_FLOAT: |
---|
98 | n/a | *(uintptr_t *) argp = 0; |
---|
99 | n/a | *(float *) argp = *(float *)(* p_argv); |
---|
100 | n/a | break; |
---|
101 | n/a | |
---|
102 | n/a | // 64-bit value cases should never be used for x86 and AMD64 builds |
---|
103 | n/a | case FFI_TYPE_SINT64: |
---|
104 | n/a | *(intptr_t *) argp = (intptr_t)*(SINT64 *)(* p_argv); |
---|
105 | n/a | break; |
---|
106 | n/a | |
---|
107 | n/a | case FFI_TYPE_UINT64: |
---|
108 | n/a | *(uintptr_t *) argp = (uintptr_t)*(UINT64 *)(* p_argv); |
---|
109 | n/a | break; |
---|
110 | n/a | |
---|
111 | n/a | case FFI_TYPE_STRUCT: |
---|
112 | n/a | *(uintptr_t *) argp = (uintptr_t)*(UINT32 *)(* p_argv); |
---|
113 | n/a | break; |
---|
114 | n/a | |
---|
115 | n/a | case FFI_TYPE_DOUBLE: |
---|
116 | n/a | *(uintptr_t *) argp = 0; |
---|
117 | n/a | *(double *) argp = *(double *)(* p_argv); |
---|
118 | n/a | break; |
---|
119 | n/a | |
---|
120 | n/a | default: |
---|
121 | n/a | FFI_ASSERT(0); |
---|
122 | n/a | } |
---|
123 | n/a | } |
---|
124 | n/a | #ifdef _WIN64 |
---|
125 | n/a | else if (z > 8) |
---|
126 | n/a | { |
---|
127 | n/a | /* On Win64, if a single argument takes more than 8 bytes, |
---|
128 | n/a | then it is always passed by reference. */ |
---|
129 | n/a | *(void **)argp = *p_argv; |
---|
130 | n/a | z = 8; |
---|
131 | n/a | } |
---|
132 | n/a | #endif |
---|
133 | n/a | else |
---|
134 | n/a | { |
---|
135 | n/a | memcpy(argp, *p_argv, z); |
---|
136 | n/a | } |
---|
137 | n/a | p_argv++; |
---|
138 | n/a | argp += z; |
---|
139 | n/a | } |
---|
140 | n/a | |
---|
141 | n/a | if (argp >= stack && (unsigned)(argp - stack) > ecif->cif->bytes) |
---|
142 | n/a | { |
---|
143 | n/a | Py_FatalError("FFI BUG: not enough stack space for arguments"); |
---|
144 | n/a | } |
---|
145 | n/a | return; |
---|
146 | n/a | } |
---|
147 | n/a | |
---|
148 | n/a | /* Perform machine dependent cif processing */ |
---|
149 | n/a | ffi_status ffi_prep_cif_machdep(ffi_cif *cif) |
---|
150 | n/a | { |
---|
151 | n/a | /* Set the return type flag */ |
---|
152 | n/a | switch (cif->rtype->type) |
---|
153 | n/a | { |
---|
154 | n/a | case FFI_TYPE_VOID: |
---|
155 | n/a | case FFI_TYPE_SINT64: |
---|
156 | n/a | case FFI_TYPE_FLOAT: |
---|
157 | n/a | case FFI_TYPE_DOUBLE: |
---|
158 | n/a | case FFI_TYPE_LONGDOUBLE: |
---|
159 | n/a | cif->flags = (unsigned) cif->rtype->type; |
---|
160 | n/a | break; |
---|
161 | n/a | |
---|
162 | n/a | case FFI_TYPE_STRUCT: |
---|
163 | n/a | /* MSVC returns small structures in registers. Put in cif->flags |
---|
164 | n/a | the value FFI_TYPE_STRUCT only if the structure is big enough; |
---|
165 | n/a | otherwise, put the 4- or 8-bytes integer type. */ |
---|
166 | n/a | if (cif->rtype->size <= 4) |
---|
167 | n/a | cif->flags = FFI_TYPE_INT; |
---|
168 | n/a | else if (cif->rtype->size <= 8) |
---|
169 | n/a | cif->flags = FFI_TYPE_SINT64; |
---|
170 | n/a | else |
---|
171 | n/a | cif->flags = FFI_TYPE_STRUCT; |
---|
172 | n/a | break; |
---|
173 | n/a | |
---|
174 | n/a | case FFI_TYPE_UINT64: |
---|
175 | n/a | #ifdef _WIN64 |
---|
176 | n/a | case FFI_TYPE_POINTER: |
---|
177 | n/a | #endif |
---|
178 | n/a | cif->flags = FFI_TYPE_SINT64; |
---|
179 | n/a | break; |
---|
180 | n/a | |
---|
181 | n/a | default: |
---|
182 | n/a | cif->flags = FFI_TYPE_INT; |
---|
183 | n/a | break; |
---|
184 | n/a | } |
---|
185 | n/a | |
---|
186 | n/a | return FFI_OK; |
---|
187 | n/a | } |
---|
188 | n/a | |
---|
189 | n/a | #ifdef _WIN32 |
---|
190 | n/a | extern int |
---|
191 | n/a | ffi_call_x86(void (*)(char *, extended_cif *), |
---|
192 | n/a | /*@out@*/ extended_cif *, |
---|
193 | n/a | unsigned, unsigned, |
---|
194 | n/a | /*@out@*/ unsigned *, |
---|
195 | n/a | void (*fn)()); |
---|
196 | n/a | #endif |
---|
197 | n/a | |
---|
198 | n/a | #ifdef _WIN64 |
---|
199 | n/a | extern int |
---|
200 | n/a | ffi_call_AMD64(void (*)(char *, extended_cif *), |
---|
201 | n/a | /*@out@*/ extended_cif *, |
---|
202 | n/a | unsigned, unsigned, |
---|
203 | n/a | /*@out@*/ unsigned *, |
---|
204 | n/a | void (*fn)()); |
---|
205 | n/a | #endif |
---|
206 | n/a | |
---|
207 | n/a | int |
---|
208 | n/a | ffi_call(/*@dependent@*/ ffi_cif *cif, |
---|
209 | n/a | void (*fn)(), |
---|
210 | n/a | /*@out@*/ void *rvalue, |
---|
211 | n/a | /*@dependent@*/ void **avalue) |
---|
212 | n/a | { |
---|
213 | n/a | extended_cif ecif; |
---|
214 | n/a | |
---|
215 | n/a | ecif.cif = cif; |
---|
216 | n/a | ecif.avalue = avalue; |
---|
217 | n/a | |
---|
218 | n/a | /* If the return value is a struct and we don't have a return */ |
---|
219 | n/a | /* value address then we need to make one */ |
---|
220 | n/a | |
---|
221 | n/a | if ((rvalue == NULL) && |
---|
222 | n/a | (cif->rtype->type == FFI_TYPE_STRUCT)) |
---|
223 | n/a | { |
---|
224 | n/a | /*@-sysunrecog@*/ |
---|
225 | n/a | ecif.rvalue = alloca(cif->rtype->size); |
---|
226 | n/a | /*@=sysunrecog@*/ |
---|
227 | n/a | } |
---|
228 | n/a | else |
---|
229 | n/a | ecif.rvalue = rvalue; |
---|
230 | n/a | |
---|
231 | n/a | |
---|
232 | n/a | switch (cif->abi) |
---|
233 | n/a | { |
---|
234 | n/a | #if !defined(_WIN64) |
---|
235 | n/a | case FFI_SYSV: |
---|
236 | n/a | case FFI_STDCALL: |
---|
237 | n/a | return ffi_call_x86(ffi_prep_args, &ecif, cif->bytes, |
---|
238 | n/a | cif->flags, ecif.rvalue, fn); |
---|
239 | n/a | break; |
---|
240 | n/a | #else |
---|
241 | n/a | case FFI_SYSV: |
---|
242 | n/a | /*@-usedef@*/ |
---|
243 | n/a | return ffi_call_AMD64(ffi_prep_args, &ecif, cif->bytes, |
---|
244 | n/a | cif->flags, ecif.rvalue, fn); |
---|
245 | n/a | /*@=usedef@*/ |
---|
246 | n/a | break; |
---|
247 | n/a | #endif |
---|
248 | n/a | |
---|
249 | n/a | default: |
---|
250 | n/a | FFI_ASSERT(0); |
---|
251 | n/a | break; |
---|
252 | n/a | } |
---|
253 | n/a | return -1; /* theller: Hrm. */ |
---|
254 | n/a | } |
---|
255 | n/a | |
---|
256 | n/a | |
---|
257 | n/a | /** private members **/ |
---|
258 | n/a | |
---|
259 | n/a | static void ffi_prep_incoming_args_SYSV (char *stack, void **ret, |
---|
260 | n/a | void** args, ffi_cif* cif); |
---|
261 | n/a | /* This function is jumped to by the trampoline */ |
---|
262 | n/a | |
---|
263 | n/a | #ifdef _WIN64 |
---|
264 | n/a | void * |
---|
265 | n/a | #else |
---|
266 | n/a | static void __fastcall |
---|
267 | n/a | #endif |
---|
268 | n/a | ffi_closure_SYSV (ffi_closure *closure, char *argp) |
---|
269 | n/a | { |
---|
270 | n/a | // this is our return value storage |
---|
271 | n/a | long double res; |
---|
272 | n/a | |
---|
273 | n/a | // our various things... |
---|
274 | n/a | ffi_cif *cif; |
---|
275 | n/a | void **arg_area; |
---|
276 | n/a | unsigned short rtype; |
---|
277 | n/a | void *resp = (void*)&res; |
---|
278 | n/a | void *args = argp + sizeof(void*); |
---|
279 | n/a | |
---|
280 | n/a | cif = closure->cif; |
---|
281 | n/a | arg_area = (void**) alloca (cif->nargs * sizeof (void*)); |
---|
282 | n/a | |
---|
283 | n/a | /* this call will initialize ARG_AREA, such that each |
---|
284 | n/a | * element in that array points to the corresponding |
---|
285 | n/a | * value on the stack; and if the function returns |
---|
286 | n/a | * a structure, it will re-set RESP to point to the |
---|
287 | n/a | * structure return address. */ |
---|
288 | n/a | |
---|
289 | n/a | ffi_prep_incoming_args_SYSV(args, (void**)&resp, arg_area, cif); |
---|
290 | n/a | |
---|
291 | n/a | (closure->fun) (cif, resp, arg_area, closure->user_data); |
---|
292 | n/a | |
---|
293 | n/a | rtype = cif->flags; |
---|
294 | n/a | |
---|
295 | n/a | #if defined(_WIN32) && !defined(_WIN64) |
---|
296 | n/a | #ifdef _MSC_VER |
---|
297 | n/a | /* now, do a generic return based on the value of rtype */ |
---|
298 | n/a | if (rtype == FFI_TYPE_INT) |
---|
299 | n/a | { |
---|
300 | n/a | _asm mov eax, resp ; |
---|
301 | n/a | _asm mov eax, [eax] ; |
---|
302 | n/a | } |
---|
303 | n/a | else if (rtype == FFI_TYPE_FLOAT) |
---|
304 | n/a | { |
---|
305 | n/a | _asm mov eax, resp ; |
---|
306 | n/a | _asm fld DWORD PTR [eax] ; |
---|
307 | n/a | // asm ("flds (%0)" : : "r" (resp) : "st" ); |
---|
308 | n/a | } |
---|
309 | n/a | else if (rtype == FFI_TYPE_DOUBLE) |
---|
310 | n/a | { |
---|
311 | n/a | _asm mov eax, resp ; |
---|
312 | n/a | _asm fld QWORD PTR [eax] ; |
---|
313 | n/a | // asm ("fldl (%0)" : : "r" (resp) : "st", "st(1)" ); |
---|
314 | n/a | } |
---|
315 | n/a | else if (rtype == FFI_TYPE_LONGDOUBLE) |
---|
316 | n/a | { |
---|
317 | n/a | // asm ("fldt (%0)" : : "r" (resp) : "st", "st(1)" ); |
---|
318 | n/a | } |
---|
319 | n/a | else if (rtype == FFI_TYPE_SINT64) |
---|
320 | n/a | { |
---|
321 | n/a | _asm mov edx, resp ; |
---|
322 | n/a | _asm mov eax, [edx] ; |
---|
323 | n/a | _asm mov edx, [edx + 4] ; |
---|
324 | n/a | // asm ("movl 0(%0),%%eax;" |
---|
325 | n/a | // "movl 4(%0),%%edx" |
---|
326 | n/a | // : : "r"(resp) |
---|
327 | n/a | // : "eax", "edx"); |
---|
328 | n/a | } |
---|
329 | n/a | #else |
---|
330 | n/a | /* now, do a generic return based on the value of rtype */ |
---|
331 | n/a | if (rtype == FFI_TYPE_INT) |
---|
332 | n/a | { |
---|
333 | n/a | asm ("movl (%0),%%eax" : : "r" (resp) : "eax"); |
---|
334 | n/a | } |
---|
335 | n/a | else if (rtype == FFI_TYPE_FLOAT) |
---|
336 | n/a | { |
---|
337 | n/a | asm ("flds (%0)" : : "r" (resp) : "st" ); |
---|
338 | n/a | } |
---|
339 | n/a | else if (rtype == FFI_TYPE_DOUBLE) |
---|
340 | n/a | { |
---|
341 | n/a | asm ("fldl (%0)" : : "r" (resp) : "st", "st(1)" ); |
---|
342 | n/a | } |
---|
343 | n/a | else if (rtype == FFI_TYPE_LONGDOUBLE) |
---|
344 | n/a | { |
---|
345 | n/a | asm ("fldt (%0)" : : "r" (resp) : "st", "st(1)" ); |
---|
346 | n/a | } |
---|
347 | n/a | else if (rtype == FFI_TYPE_SINT64) |
---|
348 | n/a | { |
---|
349 | n/a | asm ("movl 0(%0),%%eax;" |
---|
350 | n/a | "movl 4(%0),%%edx" |
---|
351 | n/a | : : "r"(resp) |
---|
352 | n/a | : "eax", "edx"); |
---|
353 | n/a | } |
---|
354 | n/a | #endif |
---|
355 | n/a | #endif |
---|
356 | n/a | |
---|
357 | n/a | #ifdef _WIN64 |
---|
358 | n/a | /* The result is returned in rax. This does the right thing for |
---|
359 | n/a | result types except for floats; we have to 'mov xmm0, rax' in the |
---|
360 | n/a | caller to correct this. |
---|
361 | n/a | */ |
---|
362 | n/a | return *(void **)resp; |
---|
363 | n/a | #endif |
---|
364 | n/a | } |
---|
365 | n/a | |
---|
366 | n/a | /*@-exportheader@*/ |
---|
367 | n/a | static void |
---|
368 | n/a | ffi_prep_incoming_args_SYSV(char *stack, void **rvalue, |
---|
369 | n/a | void **avalue, ffi_cif *cif) |
---|
370 | n/a | /*@=exportheader@*/ |
---|
371 | n/a | { |
---|
372 | n/a | register unsigned int i; |
---|
373 | n/a | register void **p_argv; |
---|
374 | n/a | register char *argp; |
---|
375 | n/a | register ffi_type **p_arg; |
---|
376 | n/a | |
---|
377 | n/a | argp = stack; |
---|
378 | n/a | |
---|
379 | n/a | if ( cif->rtype->type == FFI_TYPE_STRUCT ) { |
---|
380 | n/a | *rvalue = *(void **) argp; |
---|
381 | n/a | argp += sizeof(void *); |
---|
382 | n/a | } |
---|
383 | n/a | |
---|
384 | n/a | p_argv = avalue; |
---|
385 | n/a | |
---|
386 | n/a | for (i = cif->nargs, p_arg = cif->arg_types; (i != 0); i--, p_arg++) |
---|
387 | n/a | { |
---|
388 | n/a | size_t z; |
---|
389 | n/a | |
---|
390 | n/a | /* Align if necessary */ |
---|
391 | n/a | if ((sizeof(char *) - 1) & (size_t) argp) { |
---|
392 | n/a | argp = (char *) ALIGN(argp, sizeof(char*)); |
---|
393 | n/a | } |
---|
394 | n/a | |
---|
395 | n/a | z = (*p_arg)->size; |
---|
396 | n/a | |
---|
397 | n/a | /* because we're little endian, this is what it turns into. */ |
---|
398 | n/a | |
---|
399 | n/a | #ifdef _WIN64 |
---|
400 | n/a | if (z > 8) { |
---|
401 | n/a | /* On Win64, if a single argument takes more than 8 bytes, |
---|
402 | n/a | * then it is always passed by reference. |
---|
403 | n/a | */ |
---|
404 | n/a | *p_argv = *((void**) argp); |
---|
405 | n/a | z = 8; |
---|
406 | n/a | } |
---|
407 | n/a | else |
---|
408 | n/a | #endif |
---|
409 | n/a | *p_argv = (void*) argp; |
---|
410 | n/a | |
---|
411 | n/a | p_argv++; |
---|
412 | n/a | argp += z; |
---|
413 | n/a | } |
---|
414 | n/a | |
---|
415 | n/a | return; |
---|
416 | n/a | } |
---|
417 | n/a | |
---|
418 | n/a | /* the cif must already be prep'ed */ |
---|
419 | n/a | extern void ffi_closure_OUTER(); |
---|
420 | n/a | |
---|
421 | n/a | ffi_status |
---|
422 | n/a | ffi_prep_closure_loc (ffi_closure* closure, |
---|
423 | n/a | ffi_cif* cif, |
---|
424 | n/a | void (*fun)(ffi_cif*,void*,void**,void*), |
---|
425 | n/a | void *user_data, |
---|
426 | n/a | void *codeloc) |
---|
427 | n/a | { |
---|
428 | n/a | short bytes; |
---|
429 | n/a | char *tramp; |
---|
430 | n/a | #ifdef _WIN64 |
---|
431 | n/a | int mask = 0; |
---|
432 | n/a | #endif |
---|
433 | n/a | FFI_ASSERT (cif->abi == FFI_SYSV); |
---|
434 | n/a | |
---|
435 | n/a | if (cif->abi == FFI_SYSV) |
---|
436 | n/a | bytes = 0; |
---|
437 | n/a | #if !defined(_WIN64) |
---|
438 | n/a | else if (cif->abi == FFI_STDCALL) |
---|
439 | n/a | bytes = cif->bytes; |
---|
440 | n/a | #endif |
---|
441 | n/a | else |
---|
442 | n/a | return FFI_BAD_ABI; |
---|
443 | n/a | |
---|
444 | n/a | tramp = &closure->tramp[0]; |
---|
445 | n/a | |
---|
446 | n/a | #define BYTES(text) memcpy(tramp, text, sizeof(text)), tramp += sizeof(text)-1 |
---|
447 | n/a | #define POINTER(x) *(void**)tramp = (void*)(x), tramp += sizeof(void*) |
---|
448 | n/a | #define SHORT(x) *(short*)tramp = x, tramp += sizeof(short) |
---|
449 | n/a | #define INT(x) *(int*)tramp = x, tramp += sizeof(int) |
---|
450 | n/a | |
---|
451 | n/a | #ifdef _WIN64 |
---|
452 | n/a | if (cif->nargs >= 1 && |
---|
453 | n/a | (cif->arg_types[0]->type == FFI_TYPE_FLOAT |
---|
454 | n/a | || cif->arg_types[0]->type == FFI_TYPE_DOUBLE)) |
---|
455 | n/a | mask |= 1; |
---|
456 | n/a | if (cif->nargs >= 2 && |
---|
457 | n/a | (cif->arg_types[1]->type == FFI_TYPE_FLOAT |
---|
458 | n/a | || cif->arg_types[1]->type == FFI_TYPE_DOUBLE)) |
---|
459 | n/a | mask |= 2; |
---|
460 | n/a | if (cif->nargs >= 3 && |
---|
461 | n/a | (cif->arg_types[2]->type == FFI_TYPE_FLOAT |
---|
462 | n/a | || cif->arg_types[2]->type == FFI_TYPE_DOUBLE)) |
---|
463 | n/a | mask |= 4; |
---|
464 | n/a | if (cif->nargs >= 4 && |
---|
465 | n/a | (cif->arg_types[3]->type == FFI_TYPE_FLOAT |
---|
466 | n/a | || cif->arg_types[3]->type == FFI_TYPE_DOUBLE)) |
---|
467 | n/a | mask |= 8; |
---|
468 | n/a | |
---|
469 | n/a | /* 41 BB ---- mov r11d,mask */ |
---|
470 | n/a | BYTES("\x41\xBB"); INT(mask); |
---|
471 | n/a | |
---|
472 | n/a | /* 48 B8 -------- mov rax, closure */ |
---|
473 | n/a | BYTES("\x48\xB8"); POINTER(closure); |
---|
474 | n/a | |
---|
475 | n/a | /* 49 BA -------- mov r10, ffi_closure_OUTER */ |
---|
476 | n/a | BYTES("\x49\xBA"); POINTER(ffi_closure_OUTER); |
---|
477 | n/a | |
---|
478 | n/a | /* 41 FF E2 jmp r10 */ |
---|
479 | n/a | BYTES("\x41\xFF\xE2"); |
---|
480 | n/a | |
---|
481 | n/a | #else |
---|
482 | n/a | |
---|
483 | n/a | /* mov ecx, closure */ |
---|
484 | n/a | BYTES("\xb9"); POINTER(closure); |
---|
485 | n/a | |
---|
486 | n/a | /* mov edx, esp */ |
---|
487 | n/a | BYTES("\x8b\xd4"); |
---|
488 | n/a | |
---|
489 | n/a | /* call ffi_closure_SYSV */ |
---|
490 | n/a | BYTES("\xe8"); POINTER((char*)&ffi_closure_SYSV - (tramp + 4)); |
---|
491 | n/a | |
---|
492 | n/a | /* ret bytes */ |
---|
493 | n/a | BYTES("\xc2"); |
---|
494 | n/a | SHORT(bytes); |
---|
495 | n/a | |
---|
496 | n/a | #endif |
---|
497 | n/a | |
---|
498 | n/a | if (tramp - &closure->tramp[0] > FFI_TRAMPOLINE_SIZE) |
---|
499 | n/a | Py_FatalError("FFI_TRAMPOLINE_SIZE too small in " __FILE__); |
---|
500 | n/a | |
---|
501 | n/a | closure->cif = cif; |
---|
502 | n/a | closure->user_data = user_data; |
---|
503 | n/a | closure->fun = fun; |
---|
504 | n/a | return FFI_OK; |
---|
505 | n/a | } |
---|