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 */
35 void ffi_prep_args(char *stack
, extended_cif
*ecif
)
38 register unsigned int i
;
39 register void **p_argv
;
41 register ffi_type
**p_arg
;
45 if ( ecif
->cif
->rtype
->type
== FFI_TYPE_STRUCT
) {
46 *(void **) argp
= ecif
->rvalue
;
50 p_argv
= ecif
->avalue
;
52 for (i
= ecif
->cif
->nargs
, p_arg
= ecif
->cif
->arg_types
;
58 /* Align if necessary */
59 if (((*p_arg
)->alignment
- 1) & (unsigned) argp
) {
60 argp
= (char *) ALIGN(argp
, (*p_arg
)->alignment
);
67 switch ((*p_arg
)->type
)
70 *(signed int *) argp
= (signed int)*(SINT8
*)(* p_argv
);
74 *(unsigned int *) argp
= (unsigned int)*(UINT8
*)(* p_argv
);
78 *(signed int *) argp
= (signed int)*(SINT16
*)(* p_argv
);
82 *(unsigned int *) argp
= (unsigned int)*(UINT16
*)(* p_argv
);
86 *(unsigned int *) argp
= (unsigned int)*(UINT32
*)(* p_argv
);
93 else if (z
== sizeof(int))
95 *(unsigned int *) argp
= (unsigned int)*(UINT32
*)(* p_argv
);
99 memcpy(argp
, *p_argv
, z
);
108 /* Perform machine dependent cif processing */
109 ffi_status
ffi_prep_cif_machdep(ffi_cif
*cif
)
111 /* Round the stack up to a multiple of 8 bytes. This isn't needed
112 everywhere, but it is on some platforms, and it doesn't harm anything
113 when it isn't needed. */
114 cif
->bytes
= (cif
->bytes
+ 7) & ~7;
116 /* Set the return type flag */
117 switch (cif
->rtype
->type
)
120 case FFI_TYPE_STRUCT
:
122 case FFI_TYPE_DOUBLE
:
123 cif
->flags
= (unsigned) cif
->rtype
->type
;
126 case FFI_TYPE_SINT64
:
127 case FFI_TYPE_UINT64
:
128 cif
->flags
= (unsigned) FFI_TYPE_SINT64
;
132 cif
->flags
= FFI_TYPE_INT
;
141 extern void ffi_call_SYSV(void (*)(char *, extended_cif
*),
142 /*@out@*/ extended_cif
*,
144 /*@out@*/ unsigned *,
149 void ffi_call(/*@dependent@*/ ffi_cif
*cif
,
151 /*@out@*/ void *rvalue
,
152 /*@dependent@*/ void **avalue
)
157 ecif
.avalue
= avalue
;
159 /* If the return value is a struct and we don't have a return */
160 /* value address then we need to make one */
162 if ((rvalue
== NULL
) &&
163 (cif
->rtype
->type
== FFI_TYPE_STRUCT
))
166 ecif
.rvalue
= alloca(cif
->rtype
->size
);
170 ecif
.rvalue
= rvalue
;
177 ffi_call_SYSV(ffi_prep_args
, &ecif
, cif
->bytes
,
178 cif
->flags
, ecif
.rvalue
, fn
);