PR libffi/23935
[official-gcc.git] / libffi / src / arm / ffi.c
blob4a5edd3a560d6331a5b05e3ff472c28bb49fd9e7
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 ----------------------------------------------------------------------- */
26 #include <ffi.h>
27 #include <ffi_common.h>
29 #include <stdlib.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;
38 register char *argp;
39 register ffi_type **p_arg;
41 argp = stack;
43 if ( ecif->cif->rtype->type == FFI_TYPE_STRUCT ) {
44 *(void **) argp = ecif->rvalue;
45 argp += 4;
48 p_argv = ecif->avalue;
50 for (i = ecif->cif->nargs, p_arg = ecif->cif->arg_types;
51 (i != 0);
52 i--, p_arg++)
54 size_t z;
56 /* Align if necessary */
57 if (((*p_arg)->alignment - 1) & (unsigned) argp) {
58 argp = (char *) ALIGN(argp, (*p_arg)->alignment);
61 z = (*p_arg)->size;
62 if (z < sizeof(int))
64 z = sizeof(int);
65 switch ((*p_arg)->type)
67 case FFI_TYPE_SINT8:
68 *(signed int *) argp = (signed int)*(SINT8 *)(* p_argv);
69 break;
71 case FFI_TYPE_UINT8:
72 *(unsigned int *) argp = (unsigned int)*(UINT8 *)(* p_argv);
73 break;
75 case FFI_TYPE_SINT16:
76 *(signed int *) argp = (signed int)*(SINT16 *)(* p_argv);
77 break;
79 case FFI_TYPE_UINT16:
80 *(unsigned int *) argp = (unsigned int)*(UINT16 *)(* p_argv);
81 break;
83 case FFI_TYPE_STRUCT:
84 *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
85 break;
87 default:
88 FFI_ASSERT(0);
91 else if (z == sizeof(int))
93 *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
95 else
97 memcpy(argp, *p_argv, z);
99 p_argv++;
100 argp += z;
103 return;
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)
117 case FFI_TYPE_VOID:
118 case FFI_TYPE_STRUCT:
119 case FFI_TYPE_FLOAT:
120 case FFI_TYPE_DOUBLE:
121 cif->flags = (unsigned) cif->rtype->type;
122 break;
124 case FFI_TYPE_SINT64:
125 case FFI_TYPE_UINT64:
126 cif->flags = (unsigned) FFI_TYPE_SINT64;
127 break;
129 default:
130 cif->flags = FFI_TYPE_INT;
131 break;
134 return FFI_OK;
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)
142 extended_cif ecif;
144 ecif.cif = cif;
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);
155 else
156 ecif.rvalue = rvalue;
159 switch (cif->abi)
161 case FFI_SYSV:
162 ffi_call_SYSV(ffi_prep_args, &ecif, cif->bytes, cif->flags, ecif.rvalue,
163 fn);
165 break;
166 default:
167 FFI_ASSERT(0);
168 break;