OSDN Git Service

2e47c5dc7bfc6b4e57976c3388af79bee4433d5c
[pf3gnuchains/gcc-fork.git] / libffi / src / x86 / ffi.c
1 /* -----------------------------------------------------------------------
2    ffi.c - Copyright (c) 1996, 1998, 1999  Cygnus Solutions
3    
4    x86 Foreign Function Interface 
5
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:
13
14    The above copyright notice and this permission notice shall be included
15    in all copies or substantial portions of the Software.
16
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    ----------------------------------------------------------------------- */
25
26 #ifndef __x86_64__
27
28 #include <ffi.h>
29 #include <ffi_common.h>
30
31 #include <stdlib.h>
32
33 /* ffi_prep_args is called by the assembly routine once stack space
34    has been allocated for the function's arguments */
35
36 /*@-exportheader@*/
37 void ffi_prep_args(char *stack, extended_cif *ecif)
38 /*@=exportheader@*/
39 {
40   register unsigned int i;
41   register int tmp;
42   register void **p_argv;
43   register char *argp;
44   register ffi_type **p_arg;
45
46   tmp = 0;
47   argp = stack;
48
49   if (ecif->cif->rtype->type == FFI_TYPE_STRUCT)
50     {
51       *(void **) argp = ecif->rvalue;
52       argp += 4;
53     }
54
55   p_argv = ecif->avalue;
56
57   for (i = ecif->cif->nargs, p_arg = ecif->cif->arg_types;
58        i != 0;
59        i--, p_arg++)
60     {
61       size_t z;
62
63       /* Align if necessary */
64       if (((*p_arg)->alignment - 1) & (unsigned) argp)
65         argp = (char *) ALIGN(argp, (*p_arg)->alignment);
66
67       z = (*p_arg)->size;
68       if (z < sizeof(int))
69         {
70           z = sizeof(int);
71           switch ((*p_arg)->type)
72             {
73             case FFI_TYPE_SINT8:
74               *(signed int *) argp = (signed int)*(SINT8 *)(* p_argv);
75               break;
76
77             case FFI_TYPE_UINT8:
78               *(unsigned int *) argp = (unsigned int)*(UINT8 *)(* p_argv);
79               break;
80
81             case FFI_TYPE_SINT16:
82               *(signed int *) argp = (signed int)*(SINT16 *)(* p_argv);
83               break;
84
85             case FFI_TYPE_UINT16:
86               *(unsigned int *) argp = (unsigned int)*(UINT16 *)(* p_argv);
87               break;
88
89             case FFI_TYPE_SINT32:
90               *(signed int *) argp = (signed int)*(SINT32 *)(* p_argv);
91               break;
92
93             case FFI_TYPE_UINT32:
94               *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
95               break;
96
97             case FFI_TYPE_STRUCT:
98               *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
99               break;
100
101             default:
102               FFI_ASSERT(0);
103             }
104         }
105       else
106         {
107           memcpy(argp, *p_argv, z);
108         }
109       p_argv++;
110       argp += z;
111     }
112   
113   return;
114 }
115
116 /* Perform machine dependent cif processing */
117 ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
118 {
119   /* Set the return type flag */
120   switch (cif->rtype->type)
121     {
122     case FFI_TYPE_VOID:
123     case FFI_TYPE_STRUCT:
124     case FFI_TYPE_SINT64:
125     case FFI_TYPE_FLOAT:
126     case FFI_TYPE_DOUBLE:
127     case FFI_TYPE_LONGDOUBLE:
128       cif->flags = (unsigned) cif->rtype->type;
129       break;
130
131     case FFI_TYPE_UINT64:
132       cif->flags = FFI_TYPE_SINT64;
133       break;
134
135     default:
136       cif->flags = FFI_TYPE_INT;
137       break;
138     }
139
140   return FFI_OK;
141 }
142
143 /*@-declundef@*/
144 /*@-exportheader@*/
145 extern void ffi_call_SYSV(void (*)(char *, extended_cif *), 
146                           /*@out@*/ extended_cif *, 
147                           unsigned, unsigned, 
148                           /*@out@*/ unsigned *, 
149                           void (*fn)());
150 /*@=declundef@*/
151 /*@=exportheader@*/
152
153 void ffi_call(/*@dependent@*/ ffi_cif *cif, 
154               void (*fn)(), 
155               /*@out@*/ void *rvalue, 
156               /*@dependent@*/ void **avalue)
157 {
158   extended_cif ecif;
159
160   ecif.cif = cif;
161   ecif.avalue = avalue;
162   
163   /* If the return value is a struct and we don't have a return */
164   /* value address then we need to make one                     */
165
166   if ((rvalue == NULL) && 
167       (cif->rtype->type == FFI_TYPE_STRUCT))
168     {
169       /*@-sysunrecog@*/
170       ecif.rvalue = alloca(cif->rtype->size);
171       /*@=sysunrecog@*/
172     }
173   else
174     ecif.rvalue = rvalue;
175     
176   
177   switch (cif->abi) 
178     {
179     case FFI_SYSV:
180       /*@-usedef@*/
181       ffi_call_SYSV(ffi_prep_args, &ecif, cif->bytes, 
182                     cif->flags, ecif.rvalue, fn);
183       /*@=usedef@*/
184       break;
185     default:
186       FFI_ASSERT(0);
187       break;
188     }
189 }
190
191
192 /** private members **/
193
194 static void ffi_prep_incoming_args_SYSV (char *stack, void **ret,
195                                          void** args, ffi_cif* cif);
196 static void ffi_closure_SYSV ();
197 static void ffi_closure_raw_SYSV ();
198
199 /* This function is jumped to by the trampoline, on entry, %ecx (a
200  * caller-save register) holds the address of the closure.  
201  * Clearly, this requires __GNUC__, so perhaps we should translate this
202  * into an assembly file if this is to be distributed with ffi.
203  */
204
205 static void
206 ffi_closure_SYSV ()
207 {
208   // this is our return value storage
209   long double    res;
210
211   // our various things...
212   void          *args;
213   ffi_cif       *cif;
214   void         **arg_area;
215   ffi_closure   *closure;
216   unsigned short rtype;
217   void          *resp = (void*)&res;
218
219   /* grab the trampoline context pointer */
220   asm ("movl %%ecx,%0" : "=r" (closure));
221   
222   cif         = closure->cif;
223   arg_area    = (void**) alloca (cif->nargs * sizeof (void*));  
224   asm ("leal 8(%%ebp),%0" : "=q" (args));  
225
226   /* this call will initialize ARG_AREA, such that each
227    * element in that array points to the corresponding 
228    * value on the stack; and if the function returns
229    * a structure, it will re-set RESP to point to the
230    * structure return address.  */
231
232   ffi_prep_incoming_args_SYSV(args, (void**)&resp, arg_area, cif);
233   
234   (closure->fun) (cif, resp, arg_area, closure->user_data);
235
236   rtype = cif->flags;
237
238   /* now, do a generic return based on the value of rtype */
239   if (rtype == FFI_TYPE_INT)
240     {
241       asm ("movl (%0),%%eax" : : "r" (resp) : "eax");
242     }
243   else if (rtype == FFI_TYPE_FLOAT)
244     {
245       asm ("flds (%0)" : : "r" (resp) : "st" );
246     }
247   else if (rtype == FFI_TYPE_DOUBLE)
248     {
249       asm ("fldl (%0)" : : "r" (resp) : "st", "st(1)" );
250     }
251   else if (rtype == FFI_TYPE_LONGDOUBLE)
252     {
253       asm ("fldt (%0)" : : "r" (resp) : "st", "st(1)" );
254     }
255   else if (rtype == FFI_TYPE_SINT64)
256     {
257       asm ("movl 0(%0),%%eax;"
258            "movl 4(%0),%%edx" 
259            : : "r"(resp)
260            : "eax", "edx");
261     }
262 }
263
264 /*@-exportheader@*/
265 static void 
266 ffi_prep_incoming_args_SYSV(char *stack, void **rvalue,
267                             void **avalue, ffi_cif *cif)
268 /*@=exportheader@*/
269 {
270   register unsigned int i;
271   register int tmp;
272   register void **p_argv;
273   register char *argp;
274   register ffi_type **p_arg;
275
276   tmp = 0;
277   argp = stack;
278
279   if ( cif->rtype->type == FFI_TYPE_STRUCT ) {
280     *rvalue = *(void **) argp;
281     argp += 4;
282   }
283
284   p_argv = avalue;
285
286   for (i = cif->nargs, p_arg = cif->arg_types; (i != 0); i--, p_arg++)
287     {
288       size_t z;
289
290       /* Align if necessary */
291       if (((*p_arg)->alignment - 1) & (unsigned) argp) {
292         argp = (char *) ALIGN(argp, (*p_arg)->alignment);
293       }
294
295       z = (*p_arg)->size;
296
297       /* because we're little endian, this is what it turns into.   */
298
299       *p_argv = (void*) argp;
300
301       p_argv++;
302       argp += z;
303     }
304   
305   return;
306 }
307
308 /* How to make a trampoline.  Derived from gcc/config/i386/i386.c. */
309
310 #define FFI_INIT_TRAMPOLINE(TRAMP,FUN,CTX) \
311 ({ unsigned char *__tramp = (unsigned char*)(TRAMP); \
312    unsigned int  __fun = (unsigned int)(FUN); \
313    unsigned int  __ctx = (unsigned int)(CTX); \
314    unsigned int  __dis = __fun - ((unsigned int) __tramp + 10); \
315    *(unsigned char*) &__tramp[0] = 0xb9; \
316    *(unsigned int*)  &__tramp[1] = __ctx; \
317    *(unsigned char*) &__tramp[5] = 0xe9; \
318    *(unsigned int*)  &__tramp[6] = __dis; \
319  })
320
321
322 /* the cif must already be prep'ed */
323
324 ffi_status
325 ffi_prep_closure (ffi_closure* closure,
326                   ffi_cif* cif,
327                   void (*fun)(ffi_cif*,void*,void**,void*),
328                   void *user_data)
329 {
330   FFI_ASSERT (cif->abi == FFI_SYSV);
331
332   FFI_INIT_TRAMPOLINE (&closure->tramp[0], \
333                        &ffi_closure_SYSV,  \
334                        (void*)closure);
335     
336   closure->cif  = cif;
337   closure->user_data = user_data;
338   closure->fun  = fun;
339
340   return FFI_OK;
341 }
342
343 /* ------- Native raw API support -------------------------------- */
344
345 #if !FFI_NO_RAW_API
346
347 static void
348 ffi_closure_raw_SYSV ()
349 {
350   // this is our return value storage
351   long double    res;
352
353   // our various things...
354   void            *args;
355   ffi_raw         *raw_args;
356   ffi_cif         *cif;
357   ffi_raw_closure *closure;
358   unsigned short   rtype;
359   void            *resp = (void*)&res;
360
361   /* grab the trampoline context pointer */
362   asm ("movl %%ecx,%0" : "=r" (closure));
363
364   /* take the argument pointer */
365   asm ("leal 8(%%ebp),%0" : "=q" (args));  
366
367   /* get the cif */
368   cif = closure->cif;
369
370   /* the SYSV/X86 abi matches the RAW API exactly, well.. almost */
371   raw_args = (ffi_raw*) args;
372
373   (closure->fun) (cif, resp, raw_args, closure->user_data);
374
375   rtype = cif->flags;
376
377   /* now, do a generic return based on the value of rtype */
378   if (rtype == FFI_TYPE_INT)
379     {
380       asm ("movl (%0),%%eax" : : "r" (resp) : "eax");
381     }
382   else if (rtype == FFI_TYPE_FLOAT)
383     {
384       asm ("flds (%0)" : : "r" (resp) : "st" );
385     }
386   else if (rtype == FFI_TYPE_DOUBLE)
387     {
388       asm ("fldl (%0)" : : "r" (resp) : "st", "st(1)" );
389     }
390   else if (rtype == FFI_TYPE_LONGDOUBLE)
391     {
392       asm ("fldt (%0)" : : "r" (resp) : "st", "st(1)" );
393     }
394   else if (rtype == FFI_TYPE_SINT64)
395     {
396       asm ("movl 0(%0),%%eax; movl 4(%0),%%edx" 
397            : : "r"(resp)
398            : "eax", "edx");
399     }
400 }
401
402  
403
404
405 ffi_status
406 ffi_prep_raw_closure (ffi_raw_closure* closure,
407                       ffi_cif* cif,
408                       void (*fun)(ffi_cif*,void*,ffi_raw*,void*),
409                       void *user_data)
410 {
411   int i;
412
413   FFI_ASSERT (cif->abi == FFI_SYSV);
414
415   // we currently don't support certain kinds of arguments for raw
416   // closures.  This should be implemented by a separate assembly language
417   // routine, since it would require argument processing, something we
418   // don't do now for performance.
419
420   for (i = cif->nargs-1; i >= 0; i--)
421     {
422       FFI_ASSERT (cif->arg_types[i]->type != FFI_TYPE_STRUCT);
423       FFI_ASSERT (cif->arg_types[i]->type != FFI_TYPE_LONGDOUBLE);
424     }
425   
426
427   FFI_INIT_TRAMPOLINE (&closure->tramp[0], &ffi_closure_raw_SYSV,
428                        (void*)closure);
429     
430   closure->cif  = cif;
431   closure->user_data = user_data;
432   closure->fun  = fun;
433
434   return FFI_OK;
435 }
436
437 static void 
438 ffi_prep_args_raw(char *stack, extended_cif *ecif)
439 {
440   memcpy (stack, ecif->avalue, ecif->cif->bytes);
441 }
442
443 /* we borrow this routine from libffi (it must be changed, though, to
444  * actually call the function passed in the first argument.  as of
445  * libffi-1.20, this is not the case.)
446  */
447
448 extern void 
449 ffi_call_SYSV(void (*)(char *, extended_cif *), 
450               /*@out@*/ extended_cif *, 
451               unsigned, unsigned, 
452               /*@out@*/ unsigned *, 
453               void (*fn)());
454
455 void
456 ffi_raw_call(/*@dependent@*/ ffi_cif *cif, 
457              void (*fn)(), 
458              /*@out@*/ void *rvalue, 
459              /*@dependent@*/ ffi_raw *fake_avalue)
460 {
461   extended_cif ecif;
462   void **avalue = (void **)fake_avalue;
463
464   ecif.cif = cif;
465   ecif.avalue = avalue;
466   
467   /* If the return value is a struct and we don't have a return */
468   /* value address then we need to make one                     */
469
470   if ((rvalue == NULL) && 
471       (cif->rtype->type == FFI_TYPE_STRUCT))
472     {
473       /*@-sysunrecog@*/
474       ecif.rvalue = alloca(cif->rtype->size);
475       /*@=sysunrecog@*/
476     }
477   else
478     ecif.rvalue = rvalue;
479     
480   
481   switch (cif->abi) 
482     {
483     case FFI_SYSV:
484       /*@-usedef@*/
485       ffi_call_SYSV(ffi_prep_args_raw, &ecif, cif->bytes, 
486                     cif->flags, ecif.rvalue, fn);
487       /*@=usedef@*/
488       break;
489     default:
490       FFI_ASSERT(0);
491       break;
492     }
493 }
494
495 #endif
496
497 #endif /* __x86_64__  */