fixed more binutils issues (newer gcc/libc)
[zpugcc/jano.git] / toolchain / gcc / libffi / src / arm / ffi.c
blob37e3838246ad6fcc56728905d4f255d65d15af61
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 /*@-exportheader@*/
35 void ffi_prep_args(char *stack, extended_cif *ecif)
36 /*@=exportheader@*/
38 register unsigned int i;
39 register void **p_argv;
40 register char *argp;
41 register ffi_type **p_arg;
43 argp = stack;
45 if ( ecif->cif->rtype->type == FFI_TYPE_STRUCT ) {
46 *(void **) argp = ecif->rvalue;
47 argp += 4;
50 p_argv = ecif->avalue;
52 for (i = ecif->cif->nargs, p_arg = ecif->cif->arg_types;
53 (i != 0);
54 i--, p_arg++)
56 size_t z;
58 /* Align if necessary */
59 if (((*p_arg)->alignment - 1) & (unsigned) argp) {
60 argp = (char *) ALIGN(argp, (*p_arg)->alignment);
63 z = (*p_arg)->size;
64 if (z < sizeof(int))
66 z = sizeof(int);
67 switch ((*p_arg)->type)
69 case FFI_TYPE_SINT8:
70 *(signed int *) argp = (signed int)*(SINT8 *)(* p_argv);
71 break;
73 case FFI_TYPE_UINT8:
74 *(unsigned int *) argp = (unsigned int)*(UINT8 *)(* p_argv);
75 break;
77 case FFI_TYPE_SINT16:
78 *(signed int *) argp = (signed int)*(SINT16 *)(* p_argv);
79 break;
81 case FFI_TYPE_UINT16:
82 *(unsigned int *) argp = (unsigned int)*(UINT16 *)(* p_argv);
83 break;
85 case FFI_TYPE_STRUCT:
86 *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
87 break;
89 default:
90 FFI_ASSERT(0);
93 else if (z == sizeof(int))
95 *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
97 else
99 memcpy(argp, *p_argv, z);
101 p_argv++;
102 argp += z;
105 return;
108 /* Perform machine dependent cif processing */
109 ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
111 /* Set the return type flag */
112 switch (cif->rtype->type)
114 case FFI_TYPE_VOID:
115 case FFI_TYPE_STRUCT:
116 case FFI_TYPE_FLOAT:
117 case FFI_TYPE_DOUBLE:
118 cif->flags = (unsigned) cif->rtype->type;
119 break;
121 default:
122 cif->flags = FFI_TYPE_INT;
123 break;
126 return FFI_OK;
129 /*@-declundef@*/
130 /*@-exportheader@*/
131 extern void ffi_call_SYSV(void (*)(char *, extended_cif *),
132 /*@out@*/ extended_cif *,
133 unsigned, unsigned,
134 /*@out@*/ unsigned *,
135 void (*fn)());
136 /*@=declundef@*/
137 /*@=exportheader@*/
139 void ffi_call(/*@dependent@*/ ffi_cif *cif,
140 void (*fn)(),
141 /*@out@*/ void *rvalue,
142 /*@dependent@*/ void **avalue)
144 extended_cif ecif;
146 ecif.cif = cif;
147 ecif.avalue = avalue;
149 /* If the return value is a struct and we don't have a return */
150 /* value address then we need to make one */
152 if ((rvalue == NULL) &&
153 (cif->rtype->type == FFI_TYPE_STRUCT))
155 /*@-sysunrecog@*/
156 ecif.rvalue = alloca(cif->rtype->size);
157 /*@=sysunrecog@*/
159 else
160 ecif.rvalue = rvalue;
163 switch (cif->abi)
165 case FFI_SYSV:
166 /*@-usedef@*/
167 ffi_call_SYSV(ffi_prep_args, &ecif, cif->bytes,
168 cif->flags, ecif.rvalue, fn);
169 /*@=usedef@*/
170 break;
171 default:
172 FFI_ASSERT(0);
173 break;