1
/* -----------------------------------------------------------------------
2
ffi.c - Copyright (c) 2004 Renesas Technology
3
Copyright (c) 2008 Red Hat, Inc.
5
M32R Foreign Function Interface
7
Permission is hereby granted, free of charge, to any person obtaining
8
a copy of this software and associated documentation files (the
9
``Software''), to deal in the Software without restriction, including
10
without limitation the rights to use, copy, modify, merge, publish,
11
distribute, sublicense, and/or sell copies of the Software, and to
12
permit persons to whom the Software is furnished to do so, subject to
13
the following conditions:
15
The above copyright notice and this permission notice shall be included
16
in all copies or substantial portions of the Software.
18
THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
19
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21
IN NO EVENT SHALL RENESAS TECHNOLOGY BE LIABLE FOR ANY CLAIM, DAMAGES OR
22
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24
OTHER DEALINGS IN THE SOFTWARE.
25
----------------------------------------------------------------------- */
28
#include <ffi_common.h>
32
/* ffi_prep_args is called by the assembly routine once stack
33
space has been allocated for the function's arguments. */
35
void ffi_prep_args(char *stack, extended_cif *ecif)
47
if (ecif->cif->rtype->type == FFI_TYPE_STRUCT && ecif->cif->rtype->size > 8)
49
*(void **) argp = ecif->rvalue;
53
avn = ecif->cif->nargs;
54
p_argv = ecif->avalue;
56
for (i = ecif->cif->nargs, p_arg = ecif->cif->arg_types;
57
(i != 0) && (avn != 0);
62
/* Align if necessary. */
63
if (((*p_arg)->alignment - 1) & (unsigned) argp)
64
argp = (char *) ALIGN (argp, (*p_arg)->alignment);
74
switch ((*p_arg)->type)
77
*(signed int *) argp = (signed int)*(SINT8 *)(* p_argv);
81
*(unsigned int *) argp = (unsigned int)*(UINT8 *)(* p_argv);
85
*(signed int *) argp = (signed int)*(SINT16 *)(* p_argv);
89
*(unsigned int *) argp = (unsigned int)*(UINT16 *)(* p_argv);
94
if ((*p_arg)->alignment != 1)
95
memcpy (argp, *p_argv, z);
97
memcpy (argp + 4 - z, *p_argv, z);
105
else if (z == sizeof (int))
107
*(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
111
if ((*p_arg)->type == FFI_TYPE_STRUCT)
115
*(unsigned int *) argp = (unsigned int)(void *)(* p_argv);
120
memcpy(argp, *p_argv, z);
126
/* Double or long long 64bit. */
127
memcpy (argp, *p_argv, z);
138
/* Perform machine dependent cif processing. */
140
ffi_prep_cif_machdep(ffi_cif *cif)
142
/* Set the return type flag. */
143
switch (cif->rtype->type)
146
cif->flags = (unsigned) cif->rtype->type;
149
case FFI_TYPE_STRUCT:
150
if (cif->rtype->size <= 4)
151
cif->flags = FFI_TYPE_INT;
153
else if (cif->rtype->size <= 8)
154
cif->flags = FFI_TYPE_DOUBLE;
157
cif->flags = (unsigned) cif->rtype->type;
160
case FFI_TYPE_SINT64:
161
case FFI_TYPE_UINT64:
162
case FFI_TYPE_DOUBLE:
163
cif->flags = FFI_TYPE_DOUBLE;
168
cif->flags = FFI_TYPE_INT;
175
extern void ffi_call_SYSV(void (*)(char *, extended_cif *), extended_cif *,
176
unsigned, unsigned, unsigned *, void (*fn)(void));
178
void ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
183
ecif.avalue = avalue;
185
/* If the return value is a struct and we don't have
186
a return value address then we need to make one. */
187
if ((rvalue == NULL) &&
188
(cif->rtype->type == FFI_TYPE_STRUCT))
190
ecif.rvalue = alloca (cif->rtype->size);
193
ecif.rvalue = rvalue;
198
ffi_call_SYSV(ffi_prep_args, &ecif, cif->bytes,
199
cif->flags, ecif.rvalue, fn);
200
if (cif->rtype->type == FFI_TYPE_STRUCT)
202
int size = cif->rtype->size;
203
int align = cif->rtype->alignment;
208
*(unsigned long *)(ecif.rvalue) <<= (4 - size) * 8;
210
else if (4 < size && size < 8)
214
memcpy (ecif.rvalue, ecif.rvalue + 8-size, size);
222
memcpy (ecif.rvalue, ecif.rvalue + 8-size, size);