Merge -r 127928:132243 from trunk
[official-gcc.git] / libffi / src / arm / ffi.c
blob35b2c3477a485f1425c7ba27bc689decc707befc
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->flags == 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 if ((*p_arg)->type == FFI_TYPE_STRUCT)
62 argp = (char *) ALIGN(argp, 4);
64 z = (*p_arg)->size;
65 if (z < sizeof(int))
67 z = sizeof(int);
68 switch ((*p_arg)->type)
70 case FFI_TYPE_SINT8:
71 *(signed int *) argp = (signed int)*(SINT8 *)(* p_argv);
72 break;
74 case FFI_TYPE_UINT8:
75 *(unsigned int *) argp = (unsigned int)*(UINT8 *)(* p_argv);
76 break;
78 case FFI_TYPE_SINT16:
79 *(signed int *) argp = (signed int)*(SINT16 *)(* p_argv);
80 break;
82 case FFI_TYPE_UINT16:
83 *(unsigned int *) argp = (unsigned int)*(UINT16 *)(* p_argv);
84 break;
86 case FFI_TYPE_STRUCT:
87 memcpy(argp, *p_argv, (*p_arg)->size);
88 break;
90 default:
91 FFI_ASSERT(0);
94 else if (z == sizeof(int))
96 *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
98 else
100 memcpy(argp, *p_argv, z);
102 p_argv++;
103 argp += z;
106 return;
109 /* Perform machine dependent cif processing */
110 ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
112 /* Round the stack up to a multiple of 8 bytes. This isn't needed
113 everywhere, but it is on some platforms, and it doesn't harm anything
114 when it isn't needed. */
115 cif->bytes = (cif->bytes + 7) & ~7;
117 /* Set the return type flag */
118 switch (cif->rtype->type)
120 case FFI_TYPE_VOID:
121 case FFI_TYPE_FLOAT:
122 case FFI_TYPE_DOUBLE:
123 cif->flags = (unsigned) cif->rtype->type;
124 break;
126 case FFI_TYPE_SINT64:
127 case FFI_TYPE_UINT64:
128 cif->flags = (unsigned) FFI_TYPE_SINT64;
129 break;
131 case FFI_TYPE_STRUCT:
132 if (cif->rtype->size <= 4)
133 /* A Composite Type not larger than 4 bytes is returned in r0. */
134 cif->flags = (unsigned)FFI_TYPE_INT;
135 else
136 /* A Composite Type larger than 4 bytes, or whose size cannot
137 be determined statically ... is stored in memory at an
138 address passed [in r0]. */
139 cif->flags = (unsigned)FFI_TYPE_STRUCT;
140 break;
142 default:
143 cif->flags = FFI_TYPE_INT;
144 break;
147 return FFI_OK;
150 extern void ffi_call_SYSV(void (*)(char *, extended_cif *), extended_cif *,
151 unsigned, unsigned, unsigned *, void (*fn)());
153 void ffi_call(ffi_cif *cif, void (*fn)(), void *rvalue, void **avalue)
155 extended_cif ecif;
157 int small_struct = (cif->flags == FFI_TYPE_INT
158 && cif->rtype->type == FFI_TYPE_STRUCT);
160 ecif.cif = cif;
161 ecif.avalue = avalue;
163 unsigned int temp;
165 /* If the return value is a struct and we don't have a return */
166 /* value address then we need to make one */
168 if ((rvalue == NULL) &&
169 (cif->flags == FFI_TYPE_STRUCT))
171 ecif.rvalue = alloca(cif->rtype->size);
173 else if (small_struct)
174 ecif.rvalue = &temp;
175 else
176 ecif.rvalue = rvalue;
178 switch (cif->abi)
180 case FFI_SYSV:
181 ffi_call_SYSV(ffi_prep_args, &ecif, cif->bytes, cif->flags, ecif.rvalue,
182 fn);
184 break;
185 default:
186 FFI_ASSERT(0);
187 break;
189 if (small_struct)
190 memcpy (rvalue, &temp, cif->rtype->size);
193 /** private members **/
195 static void ffi_prep_incoming_args_SYSV (char *stack, void **ret,
196 void** args, ffi_cif* cif);
198 void ffi_closure_SYSV (ffi_closure *);
200 /* This function is jumped to by the trampoline */
202 unsigned int
203 ffi_closure_SYSV_inner (closure, respp, args)
204 ffi_closure *closure;
205 void **respp;
206 void *args;
208 // our various things...
209 ffi_cif *cif;
210 void **arg_area;
212 cif = closure->cif;
213 arg_area = (void**) alloca (cif->nargs * sizeof (void*));
215 /* this call will initialize ARG_AREA, such that each
216 * element in that array points to the corresponding
217 * value on the stack; and if the function returns
218 * a structure, it will re-set RESP to point to the
219 * structure return address. */
221 ffi_prep_incoming_args_SYSV(args, respp, arg_area, cif);
223 (closure->fun) (cif, *respp, arg_area, closure->user_data);
225 return cif->flags;
228 /*@-exportheader@*/
229 static void
230 ffi_prep_incoming_args_SYSV(char *stack, void **rvalue,
231 void **avalue, ffi_cif *cif)
232 /*@=exportheader@*/
234 register unsigned int i;
235 register void **p_argv;
236 register char *argp;
237 register ffi_type **p_arg;
239 argp = stack;
241 if ( cif->flags == FFI_TYPE_STRUCT ) {
242 *rvalue = *(void **) argp;
243 argp += 4;
246 p_argv = avalue;
248 for (i = cif->nargs, p_arg = cif->arg_types; (i != 0); i--, p_arg++)
250 size_t z;
252 size_t alignment = (*p_arg)->alignment;
253 if (alignment < 4)
254 alignment = 4;
255 /* Align if necessary */
256 if ((alignment - 1) & (unsigned) argp) {
257 argp = (char *) ALIGN(argp, alignment);
260 z = (*p_arg)->size;
262 /* because we're little endian, this is what it turns into. */
264 *p_argv = (void*) argp;
266 p_argv++;
267 argp += z;
270 return;
273 /* How to make a trampoline. */
275 #define FFI_INIT_TRAMPOLINE(TRAMP,FUN,CTX) \
276 ({ unsigned char *__tramp = (unsigned char*)(TRAMP); \
277 unsigned int __fun = (unsigned int)(FUN); \
278 unsigned int __ctx = (unsigned int)(CTX); \
279 *(unsigned int*) &__tramp[0] = 0xe92d000f; /* stmfd sp!, {r0-r3} */ \
280 *(unsigned int*) &__tramp[4] = 0xe59f0000; /* ldr r0, [pc] */ \
281 *(unsigned int*) &__tramp[8] = 0xe59ff000; /* ldr pc, [pc] */ \
282 *(unsigned int*) &__tramp[12] = __ctx; \
283 *(unsigned int*) &__tramp[16] = __fun; \
284 __clear_cache((&__tramp[0]), (&__tramp[19])); \
288 /* the cif must already be prep'ed */
290 ffi_status
291 ffi_prep_closure_loc (ffi_closure* closure,
292 ffi_cif* cif,
293 void (*fun)(ffi_cif*,void*,void**,void*),
294 void *user_data,
295 void *codeloc)
297 FFI_ASSERT (cif->abi == FFI_SYSV);
299 FFI_INIT_TRAMPOLINE (&closure->tramp[0], \
300 &ffi_closure_SYSV, \
301 codeloc);
303 closure->cif = cif;
304 closure->user_data = user_data;
305 closure->fun = fun;
307 return FFI_OK;