1 /* -----------------------------------------------------------------------
2 ffi.c - Copyright (c) 1998 Red Hat, Inc.
4 ARM 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(char *stack
, extended_cif
*ecif
)
36 register unsigned int i
;
37 register void **p_argv
;
39 register ffi_type
**p_arg
;
43 if ( ecif
->cif
->rtype
->type
== FFI_TYPE_STRUCT
) {
44 *(void **) argp
= ecif
->rvalue
;
48 p_argv
= ecif
->avalue
;
50 for (i
= ecif
->cif
->nargs
, p_arg
= ecif
->cif
->arg_types
;
56 /* Align if necessary */
57 if (((*p_arg
)->alignment
- 1) & (unsigned) argp
) {
58 argp
= (char *) ALIGN(argp
, (*p_arg
)->alignment
);
65 switch ((*p_arg
)->type
)
68 *(signed int *) argp
= (signed int)*(SINT8
*)(* p_argv
);
72 *(unsigned int *) argp
= (unsigned int)*(UINT8
*)(* p_argv
);
76 *(signed int *) argp
= (signed int)*(SINT16
*)(* p_argv
);
80 *(unsigned int *) argp
= (unsigned int)*(UINT16
*)(* p_argv
);
84 *(unsigned int *) argp
= (unsigned int)*(UINT32
*)(* p_argv
);
91 else if (z
== sizeof(int))
93 *(unsigned int *) argp
= (unsigned int)*(UINT32
*)(* p_argv
);
97 memcpy(argp
, *p_argv
, z
);
106 /* Perform machine dependent cif processing */
107 ffi_status
ffi_prep_cif_machdep(ffi_cif
*cif
)
109 /* Round the stack up to a multiple of 8 bytes. This isn't needed
110 everywhere, but it is on some platforms, and it doesn't harm anything
111 when it isn't needed. */
112 cif
->bytes
= (cif
->bytes
+ 7) & ~7;
114 /* Set the return type flag */
115 switch (cif
->rtype
->type
)
118 case FFI_TYPE_STRUCT
:
120 case FFI_TYPE_DOUBLE
:
121 cif
->flags
= (unsigned) cif
->rtype
->type
;
124 case FFI_TYPE_SINT64
:
125 case FFI_TYPE_UINT64
:
126 cif
->flags
= (unsigned) FFI_TYPE_SINT64
;
130 cif
->flags
= FFI_TYPE_INT
;
137 extern void ffi_call_SYSV(void (*)(char *, extended_cif
*), extended_cif
*,
138 unsigned, unsigned, unsigned *, void (*fn
)());
140 void ffi_call(ffi_cif
*cif
, void (*fn
)(), void *rvalue
, void **avalue
)
145 ecif
.avalue
= avalue
;
147 /* If the return value is a struct and we don't have a return */
148 /* value address then we need to make one */
150 if ((rvalue
== NULL
) &&
151 (cif
->rtype
->type
== FFI_TYPE_STRUCT
))
153 ecif
.rvalue
= alloca(cif
->rtype
->size
);
156 ecif
.rvalue
= rvalue
;
162 ffi_call_SYSV(ffi_prep_args
, &ecif
, cif
->bytes
, cif
->flags
, ecif
.rvalue
,