* config/i386/i386.c (ix86_expand_prologue): Tighten assert
[official-gcc.git] / libffi / src / m32r / ffi.c
blob300006349bf62187c2ebab82959581836dd0dcd0
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 ----------------------------------------------------------------------- */
27 #include <ffi.h>
28 #include <ffi_common.h>
30 #include <stdlib.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)
37 unsigned int i;
38 int tmp;
39 unsigned int avn;
40 void **p_argv;
41 char *argp;
42 ffi_type **p_arg;
44 tmp = 0;
45 argp = stack;
47 if (ecif->cif->rtype->type == FFI_TYPE_STRUCT && ecif->cif->rtype->size > 8)
49 *(void **) argp = ecif->rvalue;
50 argp += 4;
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);
58 i--, p_arg++)
60 size_t z;
62 /* Align if necessary. */
63 if (((*p_arg)->alignment - 1) & (unsigned) argp)
64 argp = (char *) ALIGN (argp, (*p_arg)->alignment);
66 if (avn != 0)
68 avn--;
69 z = (*p_arg)->size;
70 if (z < sizeof (int))
72 z = sizeof (int);
74 switch ((*p_arg)->type)
76 case FFI_TYPE_SINT8:
77 *(signed int *) argp = (signed int)*(SINT8 *)(* p_argv);
78 break;
80 case FFI_TYPE_UINT8:
81 *(unsigned int *) argp = (unsigned int)*(UINT8 *)(* p_argv);
82 break;
84 case FFI_TYPE_SINT16:
85 *(signed int *) argp = (signed int)*(SINT16 *)(* p_argv);
86 break;
88 case FFI_TYPE_UINT16:
89 *(unsigned int *) argp = (unsigned int)*(UINT16 *)(* p_argv);
90 break;
92 case FFI_TYPE_STRUCT:
93 z = (*p_arg)->size;
94 if ((*p_arg)->alignment != 1)
95 memcpy (argp, *p_argv, z);
96 else
97 memcpy (argp + 4 - z, *p_argv, z);
98 z = sizeof (int);
99 break;
101 default:
102 FFI_ASSERT(0);
105 else if (z == sizeof (int))
107 *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
109 else
111 if ((*p_arg)->type == FFI_TYPE_STRUCT)
113 if (z > 8)
115 *(unsigned int *) argp = (unsigned int)(void *)(* p_argv);
116 z = sizeof(void *);
118 else
120 memcpy(argp, *p_argv, z);
121 z = 8;
124 else
126 /* Double or long long 64bit. */
127 memcpy (argp, *p_argv, z);
130 p_argv++;
131 argp += z;
135 return;
138 /* Perform machine dependent cif processing. */
139 ffi_status
140 ffi_prep_cif_machdep(ffi_cif *cif)
142 /* Set the return type flag. */
143 switch (cif->rtype->type)
145 case FFI_TYPE_VOID:
146 cif->flags = (unsigned) cif->rtype->type;
147 break;
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;
156 else
157 cif->flags = (unsigned) cif->rtype->type;
158 break;
160 case FFI_TYPE_SINT64:
161 case FFI_TYPE_UINT64:
162 case FFI_TYPE_DOUBLE:
163 cif->flags = FFI_TYPE_DOUBLE;
164 break;
166 case FFI_TYPE_FLOAT:
167 default:
168 cif->flags = FFI_TYPE_INT;
169 break;
172 return FFI_OK;
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)
180 extended_cif ecif;
182 ecif.cif = cif;
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);
192 else
193 ecif.rvalue = rvalue;
195 switch (cif->abi)
197 case FFI_SYSV:
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;
205 if (size < 4)
207 if (align == 1)
208 *(unsigned long *)(ecif.rvalue) <<= (4 - size) * 8;
210 else if (4 < size && size < 8)
212 if (align == 1)
214 memcpy (ecif.rvalue, ecif.rvalue + 8-size, size);
216 else if (align == 2)
218 if (size & 1)
219 size += 1;
221 if (size != 8)
222 memcpy (ecif.rvalue, ecif.rvalue + 8-size, size);
226 break;
228 default:
229 FFI_ASSERT(0);
230 break;