source: trunk/gcc/libffi/src/x86/ffi.c

Last change on this file was 1392, checked in by bird, 21 years ago

This commit was generated by cvs2svn to compensate for changes in r1391,
which included commits to RCS files with non-trunk default branches.

  • Property cvs2svn:cvs-rev set to 1.1.1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 12.5 KB
Line 
1/* -----------------------------------------------------------------------
2 ffi.c - Copyright (c) 1996, 1998, 1999, 2001 Red Hat, Inc.
3 Copyright (c) 2002 Ranjit Mathew
4 Copyright (c) 2002 Bo Thorsen
5 Copyright (c) 2002 Roger Sayle
6
7 x86 Foreign Function Interface
8
9 Permission is hereby granted, free of charge, to any person obtaining
10 a copy of this software and associated documentation files (the
11 ``Software''), to deal in the Software without restriction, including
12 without limitation the rights to use, copy, modify, merge, publish,
13 distribute, sublicense, and/or sell copies of the Software, and to
14 permit persons to whom the Software is furnished to do so, subject to
15 the following conditions:
16
17 The above copyright notice and this permission notice shall be included
18 in all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 IN NO EVENT SHALL CYGNUS SOLUTIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26 OTHER DEALINGS IN THE SOFTWARE.
27 ----------------------------------------------------------------------- */
28
29#ifndef __x86_64__
30
31#include <ffi.h>
32#include <ffi_common.h>
33
34#include <stdlib.h>
35
36/* ffi_prep_args is called by the assembly routine once stack space
37 has been allocated for the function's arguments */
38
39/*@-exportheader@*/
40void ffi_prep_args(char *stack, extended_cif *ecif)
41/*@=exportheader@*/
42{
43 register unsigned int i;
44 register void **p_argv;
45 register char *argp;
46 register ffi_type **p_arg;
47
48 argp = stack;
49
50 if (ecif->cif->rtype->type == FFI_TYPE_STRUCT)
51 {
52 *(void **) argp = ecif->rvalue;
53 argp += 4;
54 }
55
56 p_argv = ecif->avalue;
57
58 for (i = ecif->cif->nargs, p_arg = ecif->cif->arg_types;
59 i != 0;
60 i--, p_arg++)
61 {
62 size_t z;
63
64 /* Align if necessary */
65 if (((*p_arg)->alignment - 1) & (unsigned) argp)
66 argp = (char *) ALIGN(argp, (*p_arg)->alignment);
67
68 z = (*p_arg)->size;
69 if (z < sizeof(int))
70 {
71 z = sizeof(int);
72 switch ((*p_arg)->type)
73 {
74 case FFI_TYPE_SINT8:
75 *(signed int *) argp = (signed int)*(SINT8 *)(* p_argv);
76 break;
77
78 case FFI_TYPE_UINT8:
79 *(unsigned int *) argp = (unsigned int)*(UINT8 *)(* p_argv);
80 break;
81
82 case FFI_TYPE_SINT16:
83 *(signed int *) argp = (signed int)*(SINT16 *)(* p_argv);
84 break;
85
86 case FFI_TYPE_UINT16:
87 *(unsigned int *) argp = (unsigned int)*(UINT16 *)(* p_argv);
88 break;
89
90 case FFI_TYPE_SINT32:
91 *(signed int *) argp = (signed int)*(SINT32 *)(* p_argv);
92 break;
93
94 case FFI_TYPE_UINT32:
95 *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
96 break;
97
98 case FFI_TYPE_STRUCT:
99 *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
100 break;
101
102 default:
103 FFI_ASSERT(0);
104 }
105 }
106 else
107 {
108 memcpy(argp, *p_argv, z);
109 }
110 p_argv++;
111 argp += z;
112 }
113
114 return;
115}
116
117/* Perform machine dependent cif processing */
118ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
119{
120 /* Set the return type flag */
121 switch (cif->rtype->type)
122 {
123 case FFI_TYPE_VOID:
124 case FFI_TYPE_STRUCT:
125 case FFI_TYPE_SINT64:
126 case FFI_TYPE_FLOAT:
127 case FFI_TYPE_DOUBLE:
128 case FFI_TYPE_LONGDOUBLE:
129 cif->flags = (unsigned) cif->rtype->type;
130 break;
131
132 case FFI_TYPE_UINT64:
133 cif->flags = FFI_TYPE_SINT64;
134 break;
135
136 default:
137 cif->flags = FFI_TYPE_INT;
138 break;
139 }
140
141 return FFI_OK;
142}
143
144/*@-declundef@*/
145/*@-exportheader@*/
146extern void ffi_call_SYSV(void (*)(char *, extended_cif *),
147 /*@out@*/ extended_cif *,
148 unsigned, unsigned,
149 /*@out@*/ unsigned *,
150 void (*fn)());
151/*@=declundef@*/
152/*@=exportheader@*/
153
154#ifdef X86_WIN32
155/*@-declundef@*/
156/*@-exportheader@*/
157extern void ffi_call_STDCALL(void (*)(char *, extended_cif *),
158 /*@out@*/ extended_cif *,
159 unsigned, unsigned,
160 /*@out@*/ unsigned *,
161 void (*fn)());
162/*@=declundef@*/
163/*@=exportheader@*/
164#endif /* X86_WIN32 */
165
166void ffi_call(/*@dependent@*/ ffi_cif *cif,
167 void (*fn)(),
168 /*@out@*/ void *rvalue,
169 /*@dependent@*/ void **avalue)
170{
171 extended_cif ecif;
172
173 ecif.cif = cif;
174 ecif.avalue = avalue;
175
176 /* If the return value is a struct and we don't have a return */
177 /* value address then we need to make one */
178
179 if ((rvalue == NULL) &&
180 (cif->rtype->type == FFI_TYPE_STRUCT))
181 {
182 /*@-sysunrecog@*/
183 ecif.rvalue = alloca(cif->rtype->size);
184 /*@=sysunrecog@*/
185 }
186 else
187 ecif.rvalue = rvalue;
188
189
190 switch (cif->abi)
191 {
192 case FFI_SYSV:
193 /*@-usedef@*/
194 ffi_call_SYSV(ffi_prep_args, &ecif, cif->bytes,
195 cif->flags, ecif.rvalue, fn);
196 /*@=usedef@*/
197 break;
198#ifdef X86_WIN32
199 case FFI_STDCALL:
200 /*@-usedef@*/
201 ffi_call_STDCALL(ffi_prep_args, &ecif, cif->bytes,
202 cif->flags, ecif.rvalue, fn);
203 /*@=usedef@*/
204 break;
205#endif /* X86_WIN32 */
206 default:
207 FFI_ASSERT(0);
208 break;
209 }
210}
211
212
213/** private members **/
214
215static void ffi_prep_incoming_args_SYSV (char *stack, void **ret,
216 void** args, ffi_cif* cif);
217static void ffi_closure_SYSV (ffi_closure *)
218 __attribute__ ((regparm(1)));
219static void ffi_closure_raw_SYSV (ffi_raw_closure *)
220 __attribute__ ((regparm(1)));
221
222/* This function is jumped to by the trampoline */
223
224static void
225ffi_closure_SYSV (closure)
226 ffi_closure *closure;
227{
228 // this is our return value storage
229 long double res;
230
231 // our various things...
232 ffi_cif *cif;
233 void **arg_area;
234 unsigned short rtype;
235 void *resp = (void*)&res;
236 void *args = __builtin_dwarf_cfa ();
237
238 cif = closure->cif;
239 arg_area = (void**) alloca (cif->nargs * sizeof (void*));
240
241 /* this call will initialize ARG_AREA, such that each
242 * element in that array points to the corresponding
243 * value on the stack; and if the function returns
244 * a structure, it will re-set RESP to point to the
245 * structure return address. */
246
247 ffi_prep_incoming_args_SYSV(args, (void**)&resp, arg_area, cif);
248
249 (closure->fun) (cif, resp, arg_area, closure->user_data);
250
251 rtype = cif->flags;
252
253 /* now, do a generic return based on the value of rtype */
254 if (rtype == FFI_TYPE_INT)
255 {
256 asm ("movl (%0),%%eax" : : "r" (resp) : "eax");
257 }
258 else if (rtype == FFI_TYPE_FLOAT)
259 {
260 asm ("flds (%0)" : : "r" (resp) : "st" );
261 }
262 else if (rtype == FFI_TYPE_DOUBLE)
263 {
264 asm ("fldl (%0)" : : "r" (resp) : "st", "st(1)" );
265 }
266 else if (rtype == FFI_TYPE_LONGDOUBLE)
267 {
268 asm ("fldt (%0)" : : "r" (resp) : "st", "st(1)" );
269 }
270 else if (rtype == FFI_TYPE_SINT64)
271 {
272 asm ("movl 0(%0),%%eax;"
273 "movl 4(%0),%%edx"
274 : : "r"(resp)
275 : "eax", "edx");
276 }
277}
278
279/*@-exportheader@*/
280static void
281ffi_prep_incoming_args_SYSV(char *stack, void **rvalue,
282 void **avalue, ffi_cif *cif)
283/*@=exportheader@*/
284{
285 register unsigned int i;
286 register void **p_argv;
287 register char *argp;
288 register ffi_type **p_arg;
289
290 argp = stack;
291
292 if ( cif->rtype->type == FFI_TYPE_STRUCT ) {
293 *rvalue = *(void **) argp;
294 argp += 4;
295 }
296
297 p_argv = avalue;
298
299 for (i = cif->nargs, p_arg = cif->arg_types; (i != 0); i--, p_arg++)
300 {
301 size_t z;
302
303 /* Align if necessary */
304 if (((*p_arg)->alignment - 1) & (unsigned) argp) {
305 argp = (char *) ALIGN(argp, (*p_arg)->alignment);
306 }
307
308 z = (*p_arg)->size;
309
310 /* because we're little endian, this is what it turns into. */
311
312 *p_argv = (void*) argp;
313
314 p_argv++;
315 argp += z;
316 }
317
318 return;
319}
320
321/* How to make a trampoline. Derived from gcc/config/i386/i386.c. */
322
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 + FFI_TRAMPOLINE_SIZE); \
328 *(unsigned char*) &__tramp[0] = 0xb8; \
329 *(unsigned int*) &__tramp[1] = __ctx; /* movl __ctx, %eax */ \
330 *(unsigned char *) &__tramp[5] = 0xe9; \
331 *(unsigned int*) &__tramp[6] = __dis; /* jmp __fun */ \
332 })
333
334
335/* the cif must already be prep'ed */
336
337ffi_status
338ffi_prep_closure (ffi_closure* closure,
339 ffi_cif* cif,
340 void (*fun)(ffi_cif*,void*,void**,void*),
341 void *user_data)
342{
343 FFI_ASSERT (cif->abi == FFI_SYSV);
344
345 FFI_INIT_TRAMPOLINE (&closure->tramp[0], \
346 &ffi_closure_SYSV, \
347 (void*)closure);
348
349 closure->cif = cif;
350 closure->user_data = user_data;
351 closure->fun = fun;
352
353 return FFI_OK;
354}
355
356/* ------- Native raw API support -------------------------------- */
357
358#if !FFI_NO_RAW_API
359
360static void
361ffi_closure_raw_SYSV (closure)
362 ffi_raw_closure *closure;
363{
364 // this is our return value storage
365 long double res;
366
367 // our various things...
368 ffi_raw *raw_args;
369 ffi_cif *cif;
370 unsigned short rtype;
371 void *resp = (void*)&res;
372
373 /* get the cif */
374 cif = closure->cif;
375
376 /* the SYSV/X86 abi matches the RAW API exactly, well.. almost */
377 raw_args = (ffi_raw*) __builtin_dwarf_cfa ();
378
379 (closure->fun) (cif, resp, raw_args, closure->user_data);
380
381 rtype = cif->flags;
382
383 /* now, do a generic return based on the value of rtype */
384 if (rtype == FFI_TYPE_INT)
385 {
386 asm ("movl (%0),%%eax" : : "r" (resp) : "eax");
387 }
388 else if (rtype == FFI_TYPE_FLOAT)
389 {
390 asm ("flds (%0)" : : "r" (resp) : "st" );
391 }
392 else if (rtype == FFI_TYPE_DOUBLE)
393 {
394 asm ("fldl (%0)" : : "r" (resp) : "st", "st(1)" );
395 }
396 else if (rtype == FFI_TYPE_LONGDOUBLE)
397 {
398 asm ("fldt (%0)" : : "r" (resp) : "st", "st(1)" );
399 }
400 else if (rtype == FFI_TYPE_SINT64)
401 {
402 asm ("movl 0(%0),%%eax; movl 4(%0),%%edx"
403 : : "r"(resp)
404 : "eax", "edx");
405 }
406}
407
408
409
410
411ffi_status
412ffi_prep_raw_closure (ffi_raw_closure* closure,
413 ffi_cif* cif,
414 void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
415 void *user_data)
416{
417 int i;
418
419 FFI_ASSERT (cif->abi == FFI_SYSV);
420
421 // we currently don't support certain kinds of arguments for raw
422 // closures. This should be implemented by a separate assembly language
423 // routine, since it would require argument processing, something we
424 // don't do now for performance.
425
426 for (i = cif->nargs-1; i >= 0; i--)
427 {
428 FFI_ASSERT (cif->arg_types[i]->type != FFI_TYPE_STRUCT);
429 FFI_ASSERT (cif->arg_types[i]->type != FFI_TYPE_LONGDOUBLE);
430 }
431
432
433 FFI_INIT_TRAMPOLINE (&closure->tramp[0], &ffi_closure_raw_SYSV,
434 (void*)closure);
435
436 closure->cif = cif;
437 closure->user_data = user_data;
438 closure->fun = fun;
439
440 return FFI_OK;
441}
442
443static void
444ffi_prep_args_raw(char *stack, extended_cif *ecif)
445{
446 memcpy (stack, ecif->avalue, ecif->cif->bytes);
447}
448
449/* we borrow this routine from libffi (it must be changed, though, to
450 * actually call the function passed in the first argument. as of
451 * libffi-1.20, this is not the case.)
452 */
453
454extern void
455ffi_call_SYSV(void (*)(char *, extended_cif *),
456 /*@out@*/ extended_cif *,
457 unsigned, unsigned,
458 /*@out@*/ unsigned *,
459 void (*fn)());
460
461#ifdef X86_WIN32
462extern void
463ffi_call_STDCALL(void (*)(char *, extended_cif *),
464 /*@out@*/ extended_cif *,
465 unsigned, unsigned,
466 /*@out@*/ unsigned *,
467 void (*fn)());
468#endif /* X86_WIN32 */
469
470void
471ffi_raw_call(/*@dependent@*/ ffi_cif *cif,
472 void (*fn)(),
473 /*@out@*/ void *rvalue,
474 /*@dependent@*/ ffi_raw *fake_avalue)
475{
476 extended_cif ecif;
477 void **avalue = (void **)fake_avalue;
478
479 ecif.cif = cif;
480 ecif.avalue = avalue;
481
482 /* If the return value is a struct and we don't have a return */
483 /* value address then we need to make one */
484
485 if ((rvalue == NULL) &&
486 (cif->rtype->type == FFI_TYPE_STRUCT))
487 {
488 /*@-sysunrecog@*/
489 ecif.rvalue = alloca(cif->rtype->size);
490 /*@=sysunrecog@*/
491 }
492 else
493 ecif.rvalue = rvalue;
494
495
496 switch (cif->abi)
497 {
498 case FFI_SYSV:
499 /*@-usedef@*/
500 ffi_call_SYSV(ffi_prep_args_raw, &ecif, cif->bytes,
501 cif->flags, ecif.rvalue, fn);
502 /*@=usedef@*/
503 break;
504#ifdef X86_WIN32
505 case FFI_STDCALL:
506 /*@-usedef@*/
507 ffi_call_STDCALL(ffi_prep_args_raw, &ecif, cif->bytes,
508 cif->flags, ecif.rvalue, fn);
509 /*@=usedef@*/
510 break;
511#endif /* X86_WIN32 */
512 default:
513 FFI_ASSERT(0);
514 break;
515 }
516}
517
518#endif
519
520#endif /* __x86_64__ */
Note: See TracBrowser for help on using the repository browser.