config:
[official-gcc.git] / libffi / src / x86 / ffi.c
bloba6e73f711e36a34f951ff336dcdcaa35723acfe1
1 /* -----------------------------------------------------------------------
2 ffi.c - Copyright (c) 1996, 1998, 1999 Cygnus Solutions
4 x86 Foreign Function Interface
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 ``Software''), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
14 The above copyright notice and this permission notice shall be included
15 in all copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 IN NO EVENT SHALL CYGNUS SOLUTIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 OTHER DEALINGS IN THE SOFTWARE.
24 ----------------------------------------------------------------------- */
26 #include <ffi.h>
27 #include <ffi_common.h>
29 #include <stdlib.h>
31 /* ffi_prep_args is called by the assembly routine once stack space
32 has been allocated for the function's arguments */
34 /*@-exportheader@*/
35 void ffi_prep_args(char *stack, extended_cif *ecif)
36 /*@=exportheader@*/
38 register unsigned int i;
39 register int tmp;
40 register unsigned int avn;
41 register void **p_argv;
42 register char *argp;
43 register ffi_type **p_arg;
45 tmp = 0;
46 argp = stack;
48 if ( ecif->cif->rtype->type == FFI_TYPE_STRUCT ) {
49 *(void **) argp = ecif->rvalue;
50 argp += 4;
53 avn = ecif->cif->nargs;
54 p_argv = ecif->avalue;
56 for (i = ecif->cif->nargs, p_arg = ecif->cif->arg_types;
57 (i != 0) && (avn != 0);
58 i--, p_arg++)
60 size_t z;
62 /* Align if necessary */
63 if (((*p_arg)->alignment - 1) & (unsigned) argp) {
64 argp = (char *) ALIGN(argp, (*p_arg)->alignment);
67 if (avn != 0)
69 avn--;
70 z = (*p_arg)->size;
71 if (z < sizeof(int))
73 z = sizeof(int);
74 switch ((*p_arg)->type)
76 case FFI_TYPE_SINT8:
77 *(signed int *) argp = (signed int)*(SINT8 *)(* p_argv);
78 break;
80 case FFI_TYPE_UINT8:
81 *(unsigned int *) argp = (unsigned int)*(UINT8 *)(* p_argv);
82 break;
84 case FFI_TYPE_SINT16:
85 *(signed int *) argp = (signed int)*(SINT16 *)(* p_argv);
86 break;
88 case FFI_TYPE_UINT16:
89 *(unsigned int *) argp = (unsigned int)*(UINT16 *)(* p_argv);
90 break;
92 case FFI_TYPE_SINT32:
93 *(signed int *) argp = (signed int)*(SINT32 *)(* p_argv);
94 break;
96 case FFI_TYPE_UINT32:
97 *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
98 break;
100 case FFI_TYPE_STRUCT:
101 *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
102 break;
104 default:
105 FFI_ASSERT(0);
108 else
110 memcpy(argp, *p_argv, z);
112 p_argv++;
113 argp += z;
117 return;
120 /* Perform machine dependent cif processing */
121 ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
123 /* Set the return type flag */
124 switch (cif->rtype->type)
126 case FFI_TYPE_VOID:
127 case FFI_TYPE_STRUCT:
128 case FFI_TYPE_SINT64:
129 case FFI_TYPE_FLOAT:
130 case FFI_TYPE_DOUBLE:
131 case FFI_TYPE_LONGDOUBLE:
132 cif->flags = (unsigned) cif->rtype->type;
133 break;
135 case FFI_TYPE_UINT64:
136 cif->flags = FFI_TYPE_SINT64;
137 break;
139 default:
140 cif->flags = FFI_TYPE_INT;
141 break;
144 return FFI_OK;
147 /*@-declundef@*/
148 /*@-exportheader@*/
149 extern void ffi_call_SYSV(void (*)(char *, extended_cif *),
150 /*@out@*/ extended_cif *,
151 unsigned, unsigned,
152 /*@out@*/ unsigned *,
153 void (*fn)());
154 /*@=declundef@*/
155 /*@=exportheader@*/
157 void ffi_call(/*@dependent@*/ ffi_cif *cif,
158 void (*fn)(),
159 /*@out@*/ void *rvalue,
160 /*@dependent@*/ void **avalue)
162 extended_cif ecif;
164 ecif.cif = cif;
165 ecif.avalue = avalue;
167 /* If the return value is a struct and we don't have a return */
168 /* value address then we need to make one */
170 if ((rvalue == NULL) &&
171 (cif->rtype->type == FFI_TYPE_STRUCT))
173 /*@-sysunrecog@*/
174 ecif.rvalue = alloca(cif->rtype->size);
175 /*@=sysunrecog@*/
177 else
178 ecif.rvalue = rvalue;
181 switch (cif->abi)
183 case FFI_SYSV:
184 /*@-usedef@*/
185 ffi_call_SYSV(ffi_prep_args, &ecif, cif->bytes,
186 cif->flags, ecif.rvalue, fn);
187 /*@=usedef@*/
188 break;
189 default:
190 FFI_ASSERT(0);
191 break;
196 /** private members **/
198 static void ffi_prep_incoming_args_SYSV (char *stack, void **ret,
199 void** args, ffi_cif* cif);
200 static void ffi_closure_SYSV ();
201 static void ffi_closure_raw_SYSV ();
203 /* This function is jumped to by the trampoline, on entry, %ecx (a
204 * caller-save register) holds the address of the closure.
205 * Clearly, this requires __GNUC__, so perhaps we should translate this
206 * into an assembly file if this is to be distributed with ffi.
209 static void
210 ffi_closure_SYSV ()
212 // this is our return value storage
213 long double res;
215 // our various things...
216 void *args;
217 ffi_cif *cif;
218 void **arg_area;
219 ffi_closure *closure;
220 unsigned short rtype;
221 void *resp = (void*)&res;
223 /* grab the trampoline context pointer */
224 asm ("movl %%ecx,%0" : "=r" (closure));
226 cif = closure->cif;
227 arg_area = (void**) alloca (cif->nargs * sizeof (void*));
228 asm ("leal 8(%%ebp),%0" : "=q" (args));
230 /* this call will initialize ARG_AREA, such that each
231 * element in that array points to the corresponding
232 * value on the stack; and if the function returns
233 * a structure, it will re-set RESP to point to the
234 * structure return address. */
236 ffi_prep_incoming_args_SYSV(args, (void**)&resp, arg_area, cif);
238 (closure->fun) (cif, resp, arg_area, closure->user_data);
240 rtype = cif->flags;
242 /* now, do a generic return based on the value of rtype */
243 if (rtype == FFI_TYPE_INT)
245 asm ("movl (%0),%%eax" : : "r" (resp) : "eax");
247 else if (rtype == FFI_TYPE_FLOAT)
249 asm ("flds (%0)" : : "r" (resp) : "st" );
251 else if (rtype == FFI_TYPE_DOUBLE)
253 asm ("fldl (%0)" : : "r" (resp) : "st", "st(1)" );
255 else if (rtype == FFI_TYPE_LONGDOUBLE)
257 asm ("fldt (%0)" : : "r" (resp) : "st", "st(1)" );
259 else if (rtype == FFI_TYPE_SINT64)
261 asm ("movl 0(%0),%%eax;"
262 "movl 4(%0),%%edx"
263 : : "r"(resp)
264 : "eax", "edx");
268 /*@-exportheader@*/
269 static void
270 ffi_prep_incoming_args_SYSV(char *stack, void **rvalue,
271 void **avalue, ffi_cif *cif)
272 /*@=exportheader@*/
274 register unsigned int i;
275 register int tmp;
276 register unsigned int avn;
277 register void **p_argv;
278 register char *argp;
279 register ffi_type **p_arg;
281 tmp = 0;
282 argp = stack;
284 if ( cif->rtype->type == FFI_TYPE_STRUCT ) {
285 *rvalue = *(void **) argp;
286 argp += 4;
289 avn = cif->nargs;
290 p_argv = avalue;
292 for (i = cif->nargs, p_arg = cif->arg_types;
293 (i != 0) && (avn != 0);
294 i--, p_arg++)
296 size_t z;
298 /* Align if necessary */
299 if (((*p_arg)->alignment - 1) & (unsigned) argp) {
300 argp = (char *) ALIGN(argp, (*p_arg)->alignment);
303 if (avn != 0)
305 avn--;
306 z = (*p_arg)->size;
308 /* because we're little endian, this is
309 what it turns into. */
311 *p_argv = (void*) argp;
313 p_argv++;
314 argp += z;
318 return;
321 /* How to make a trampoline. Derived from gcc/config/i386/i386.c. */
323 #define FFI_INIT_TRAMPOLINE(TRAMP,FUN,CTX) \
324 ({ unsigned char *__tramp = (unsigned char*)(TRAMP); \
325 unsigned int __fun = (unsigned int)(FUN); \
326 unsigned int __ctx = (unsigned int)(CTX); \
327 unsigned int __dis = __fun - ((unsigned int) __tramp + 10); \
328 *(unsigned char*) &__tramp[0] = 0xb9; \
329 *(unsigned int*) &__tramp[1] = __ctx; \
330 *(unsigned char*) &__tramp[5] = 0xe9; \
331 *(unsigned int*) &__tramp[6] = __dis; \
335 /* the cif must already be prep'ed */
337 ffi_status
338 ffi_prep_closure (ffi_closure* closure,
339 ffi_cif* cif,
340 void (*fun)(ffi_cif*,void*,void**,void*),
341 void *user_data)
343 FFI_ASSERT (cif->abi == FFI_SYSV);
345 FFI_INIT_TRAMPOLINE (&closure->tramp[0], \
346 &ffi_closure_SYSV, \
347 (void*)closure);
349 closure->cif = cif;
350 closure->user_data = user_data;
351 closure->fun = fun;
353 return FFI_OK;
356 /* ------- Native raw API support -------------------------------- */
358 #if !FFI_NO_RAW_API
360 static void
361 ffi_closure_raw_SYSV ()
363 // this is our return value storage
364 long double res;
366 // our various things...
367 void *args;
368 ffi_raw *raw_args;
369 ffi_cif *cif;
370 ffi_raw_closure *closure;
371 unsigned short rtype;
372 void *resp = (void*)&res;
374 /* grab the trampoline context pointer */
375 asm ("movl %%ecx,%0" : "=r" (closure));
377 /* take the argument pointer */
378 asm ("leal 8(%%ebp),%0" : "=q" (args));
380 /* get the cif */
381 cif = closure->cif;
383 /* the SYSV/X86 abi matches the RAW API exactly, well.. almost */
384 raw_args = (ffi_raw*) args;
386 (closure->fun) (cif, resp, raw_args, closure->user_data);
388 rtype = cif->flags;
390 /* now, do a generic return based on the value of rtype */
391 if (rtype == FFI_TYPE_INT)
393 asm ("movl (%0),%%eax" : : "r" (resp) : "eax");
395 else if (rtype == FFI_TYPE_FLOAT)
397 asm ("flds (%0)" : : "r" (resp) : "st" );
399 else if (rtype == FFI_TYPE_DOUBLE)
401 asm ("fldl (%0)" : : "r" (resp) : "st", "st(1)" );
403 else if (rtype == FFI_TYPE_LONGDOUBLE)
405 asm ("fldt (%0)" : : "r" (resp) : "st", "st(1)" );
407 else if (rtype == FFI_TYPE_SINT64)
409 asm ("movl 0(%0),%%eax; movl 4(%0),%%edx"
410 : : "r"(resp)
411 : "eax", "edx");
418 ffi_status
419 ffi_prep_raw_closure (ffi_raw_closure* closure,
420 ffi_cif* cif,
421 void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
422 void *user_data)
424 int i;
426 FFI_ASSERT (cif->abi == FFI_SYSV);
428 // we currently don't support certain kinds of arguments for raw
429 // closures. This should be implemented by a separate assembly language
430 // routine, since it would require argument processing, something we
431 // don't do now for performance.
433 for (i = cif->nargs-1; i >= 0; i--)
435 FFI_ASSERT (cif->arg_types[i]->type != FFI_TYPE_STRUCT);
436 FFI_ASSERT (cif->arg_types[i]->type != FFI_TYPE_LONGDOUBLE);
440 FFI_INIT_TRAMPOLINE (&closure->tramp[0], &ffi_closure_raw_SYSV,
441 (void*)closure);
443 closure->cif = cif;
444 closure->user_data = user_data;
445 closure->fun = fun;
447 return FFI_OK;
450 static void
451 ffi_prep_args_raw(char *stack, extended_cif *ecif)
453 memcpy (stack, ecif->avalue, ecif->cif->bytes);
456 /* we borrow this routine from libffi (it must be changed, though, to
457 * actually call the function passed in the first argument. as of
458 * libffi-1.20, this is not the case.)
461 extern void
462 ffi_call_SYSV(void (*)(char *, extended_cif *),
463 /*@out@*/ extended_cif *,
464 unsigned, unsigned,
465 /*@out@*/ unsigned *,
466 void (*fn)());
468 void
469 ffi_raw_call(/*@dependent@*/ ffi_cif *cif,
470 void (*fn)(),
471 /*@out@*/ void *rvalue,
472 /*@dependent@*/ ffi_raw *fake_avalue)
474 extended_cif ecif;
475 void **avalue = (void **)fake_avalue;
477 ecif.cif = cif;
478 ecif.avalue = avalue;
480 /* If the return value is a struct and we don't have a return */
481 /* value address then we need to make one */
483 if ((rvalue == NULL) &&
484 (cif->rtype->type == FFI_TYPE_STRUCT))
486 /*@-sysunrecog@*/
487 ecif.rvalue = alloca(cif->rtype->size);
488 /*@=sysunrecog@*/
490 else
491 ecif.rvalue = rvalue;
494 switch (cif->abi)
496 case FFI_SYSV:
497 /*@-usedef@*/
498 ffi_call_SYSV(ffi_prep_args_raw, &ecif, cif->bytes,
499 cif->flags, ecif.rvalue, fn);
500 /*@=usedef@*/
501 break;
502 default:
503 FFI_ASSERT(0);
504 break;
508 #endif