1 /* -----------------------------------------------------------------------
2 ffi.c - Copyright (c) 1996 Cygnus Solutions
4 Sparc 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 ----------------------------------------------------------------------- */
27 #include <ffi_common.h>
31 /* ffi_prep_args is called by the assembly routine once stack space
32 has been allocated for the function's arguments */
34 void ffi_prep_args_v8(char *stack, extended_cif *ecif)
45 /* Skip 16 words for the window save area */
46 argp = stack + 16*sizeof(int);
48 /* This should only really be done when we are returning a structure,
49 however, it's faster just to do it all the time...
51 if ( ecif->cif->rtype->type == FFI_TYPE_STRUCT ) */
52 *(int *) argp = (long)ecif->rvalue;
54 /* And 1 word for the structure return value. */
58 /* Purify will probably complain in our assembly routine, unless we
59 zero out this memory. */
69 avn = ecif->cif->nargs;
70 p_argv = ecif->avalue;
72 for (i = ecif->cif->nargs, p_arg = ecif->cif->arg_types;
81 if ((*p_arg)->type == FFI_TYPE_STRUCT
82 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
83 || (*p_arg)->type == FFI_TYPE_LONGDOUBLE
87 *(unsigned int *) argp = (unsigned long)(* p_argv);
96 switch ((*p_arg)->type)
99 *(signed int *) argp = *(SINT8 *)(* p_argv);
103 *(unsigned int *) argp = *(UINT8 *)(* p_argv);
106 case FFI_TYPE_SINT16:
107 *(signed int *) argp = *(SINT16 *)(* p_argv);
110 case FFI_TYPE_UINT16:
111 *(unsigned int *) argp = *(UINT16 *)(* p_argv);
120 memcpy(argp, *p_argv, z);
131 int ffi_prep_args_v9(char *stack, extended_cif *ecif)
141 /* Skip 16 words for the window save area */
142 argp = stack + 16*sizeof(long long);
145 /* Purify will probably complain in our assembly routine, unless we
146 zero out this memory. */
148 ((long long*)argp)[0] = 0;
149 ((long long*)argp)[1] = 0;
150 ((long long*)argp)[2] = 0;
151 ((long long*)argp)[3] = 0;
152 ((long long*)argp)[4] = 0;
153 ((long long*)argp)[5] = 0;
156 p_argv = ecif->avalue;
158 if (ecif->cif->rtype->type == FFI_TYPE_STRUCT &&
159 ecif->cif->rtype->size > 32)
161 *(unsigned long long *) argp = (unsigned long)ecif->rvalue;
165 for (i = 0, p_arg = ecif->cif->arg_types; i < ecif->cif->nargs;
171 switch ((*p_arg)->type)
173 case FFI_TYPE_STRUCT:
176 /* For structures larger than 16 bytes we pass reference. */
177 *(unsigned long long *) argp = (unsigned long)* p_argv;
178 argp += sizeof(long long);
185 case FFI_TYPE_DOUBLE:
186 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
187 case FFI_TYPE_LONGDOUBLE:
189 ret = 1; /* We should promote into FP regs as well as integer. */
192 if (z < sizeof(long long))
194 switch ((*p_arg)->type)
197 *(signed long long *) argp = *(SINT8 *)(* p_argv);
201 *(unsigned long long *) argp = *(UINT8 *)(* p_argv);
204 case FFI_TYPE_SINT16:
205 *(signed long long *) argp = *(SINT16 *)(* p_argv);
208 case FFI_TYPE_UINT16:
209 *(unsigned long long *) argp = *(UINT16 *)(* p_argv);
212 case FFI_TYPE_SINT32:
213 *(signed long long *) argp = *(SINT32 *)(* p_argv);
216 case FFI_TYPE_UINT32:
217 *(unsigned long long *) argp = *(UINT32 *)(* p_argv);
221 *(float *) (argp + 4) = *(FLOAT32 *)(* p_argv); /* Right justify */
224 case FFI_TYPE_STRUCT:
225 memcpy(argp, *p_argv, z);
231 z = sizeof(long long);
234 else if (z == sizeof(long long))
236 memcpy(argp, *p_argv, z);
237 z = sizeof(long long);
242 if ((tmp & 1) && (*p_arg)->alignment > 8)
245 argp += sizeof(long long);
247 memcpy(argp, *p_argv, z);
248 z = 2 * sizeof(long long);
258 /* Perform machine dependent cif processing */
259 ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
263 if (cif->abi != FFI_V9)
267 /* If we are returning a struct, this will already have been added.
268 Otherwise we need to add it because it's always got to be there! */
270 if (cif->rtype->type != FFI_TYPE_STRUCT)
271 cif->bytes += wordsize;
273 /* sparc call frames require that space is allocated for 6 args,
274 even if they aren't used. Make that space if necessary. */
276 if (cif->bytes < 4*6+4)
283 /* sparc call frames require that space is allocated for 6 args,
284 even if they aren't used. Make that space if necessary. */
286 if (cif->bytes < 8*6)
290 /* Adjust cif->bytes. to include 16 words for the window save area,
291 and maybe the struct/union return pointer area, */
293 cif->bytes += 16 * wordsize;
295 /* The stack must be 2 word aligned, so round bytes up
298 cif->bytes = ALIGN(cif->bytes, 2 * wordsize);
300 /* Set the return type flag */
301 switch (cif->rtype->type)
305 case FFI_TYPE_DOUBLE:
306 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
307 case FFI_TYPE_LONGDOUBLE:
309 cif->flags = cif->rtype->type;
312 case FFI_TYPE_STRUCT:
313 if (cif->abi == FFI_V9 && cif->rtype->size > 32)
314 cif->flags = FFI_TYPE_VOID;
316 cif->flags = FFI_TYPE_STRUCT;
319 case FFI_TYPE_SINT64:
320 case FFI_TYPE_UINT64:
321 if (cif->abi != FFI_V9)
323 cif->flags = FFI_TYPE_SINT64;
328 cif->flags = FFI_TYPE_INT;
334 int ffi_V9_return_struct(ffi_type *arg, int off, char *ret, char *intg, char *flt)
336 ffi_type **ptr = &arg->elements[0];
340 if (off & ((*ptr)->alignment - 1))
341 off = ALIGN(off, (*ptr)->alignment);
343 switch ((*ptr)->type)
345 case FFI_TYPE_STRUCT:
346 off = ffi_V9_return_struct(*ptr, off, ret, intg, flt);
349 case FFI_TYPE_DOUBLE:
350 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
351 case FFI_TYPE_LONGDOUBLE:
353 memcpy(ret + off, flt + off, (*ptr)->size);
357 memcpy(ret + off, intg + off, (*ptr)->size);
366 extern int ffi_call_V8(void *, extended_cif *, unsigned,
367 unsigned, unsigned *, void (*fn)());
368 extern int ffi_call_V9(void *, extended_cif *, unsigned,
369 unsigned, unsigned *, void (*fn)());
371 void ffi_call(ffi_cif *cif, void (*fn)(), void *rvalue, void **avalue)
377 ecif.avalue = avalue;
379 /* If the return value is a struct and we don't have a return */
380 /* value address then we need to make one */
382 ecif.rvalue = rvalue;
383 if (cif->rtype->type == FFI_TYPE_STRUCT)
385 if (cif->rtype->size <= 32)
391 ecif.rvalue = alloca(cif->rtype->size);
399 /* We don't yet support calling 32bit code from 64bit */
402 ffi_call_V8(ffi_prep_args_v8, &ecif, cif->bytes,
403 cif->flags, rvalue, fn);
408 ffi_call_V9(ffi_prep_args_v9, &ecif, cif->bytes,
409 cif->flags, rval, fn);
410 if (rvalue && rval && cif->rtype->type == FFI_TYPE_STRUCT)
411 ffi_V9_return_struct(cif->rtype, 0, (char *)rvalue, (char *)rval, ((char *)rval)+32);