LWG 3075 basic_string needs deduction guides from basic_string_view
[official-gcc.git] / libffi / src / arc / ffi.c
blob32f82a7d5bb54fcb674c43590f4d9bfa646e69ad
1 /* -----------------------------------------------------------------------
2 ffi.c - Copyright (c) 2013 Synopsys, Inc. (www.synopsys.com)
4 ARC 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 RENESAS TECHNOLOGY 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>
30 #include <stdint.h>
32 #include <sys/cachectl.h>
34 /* for little endian ARC, the code is in fact stored as mixed endian for
35 performance reasons */
36 #if __BIG_ENDIAN__
37 #define CODE_ENDIAN(x) (x)
38 #else
39 #define CODE_ENDIAN(x) ( (((uint32_t) (x)) << 16) | (((uint32_t) (x)) >> 16))
40 #endif
42 /* ffi_prep_args is called by the assembly routine once stack
43 space has been allocated for the function's arguments. */
45 void
46 ffi_prep_args (char *stack, extended_cif * ecif)
48 unsigned int i;
49 int tmp;
50 void **p_argv;
51 char *argp;
52 ffi_type **p_arg;
54 tmp = 0;
55 argp = stack;
57 if (ecif->cif->rtype->type == FFI_TYPE_STRUCT)
59 *(void **) argp = ecif->rvalue;
60 argp += 4;
63 p_argv = ecif->avalue;
65 for (i = ecif->cif->nargs, p_arg = ecif->cif->arg_types;
66 (i != 0); i--, p_arg++)
68 size_t z;
69 int alignment;
71 /* align alignment to 4 */
72 alignment = (((*p_arg)->alignment - 1) | 3) + 1;
74 /* Align if necessary. */
75 if ((alignment - 1) & (unsigned) argp)
76 argp = (char *) ALIGN (argp, alignment);
78 z = (*p_arg)->size;
79 if (z < sizeof (int))
81 z = sizeof (int);
83 switch ((*p_arg)->type)
85 case FFI_TYPE_SINT8:
86 *(signed int *) argp = (signed int) *(SINT8 *) (*p_argv);
87 break;
89 case FFI_TYPE_UINT8:
90 *(unsigned int *) argp = (unsigned int) *(UINT8 *) (*p_argv);
91 break;
93 case FFI_TYPE_SINT16:
94 *(signed int *) argp = (signed int) *(SINT16 *) (*p_argv);
95 break;
97 case FFI_TYPE_UINT16:
98 *(unsigned int *) argp = (unsigned int) *(UINT16 *) (*p_argv);
99 break;
101 case FFI_TYPE_STRUCT:
102 memcpy (argp, *p_argv, (*p_arg)->size);
103 break;
105 default:
106 FFI_ASSERT (0);
109 else if (z == sizeof (int))
111 *(unsigned int *) argp = (unsigned int) *(UINT32 *) (*p_argv);
113 else
115 if ((*p_arg)->type == FFI_TYPE_STRUCT)
117 memcpy (argp, *p_argv, z);
119 else
121 /* Double or long long 64bit. */
122 memcpy (argp, *p_argv, z);
125 p_argv++;
126 argp += z;
129 return;
132 /* Perform machine dependent cif processing. */
133 ffi_status
134 ffi_prep_cif_machdep (ffi_cif * cif)
136 /* Set the return type flag. */
137 switch (cif->rtype->type)
139 case FFI_TYPE_VOID:
140 cif->flags = (unsigned) cif->rtype->type;
141 break;
143 case FFI_TYPE_STRUCT:
144 cif->flags = (unsigned) cif->rtype->type;
145 break;
147 case FFI_TYPE_SINT64:
148 case FFI_TYPE_UINT64:
149 case FFI_TYPE_DOUBLE:
150 cif->flags = FFI_TYPE_DOUBLE;
151 break;
153 case FFI_TYPE_FLOAT:
154 default:
155 cif->flags = FFI_TYPE_INT;
156 break;
159 return FFI_OK;
162 extern void ffi_call_ARCompact (void (*)(char *, extended_cif *),
163 extended_cif *, unsigned, unsigned,
164 unsigned *, void (*fn) (void));
166 void
167 ffi_call (ffi_cif * cif, void (*fn) (void), void *rvalue, void **avalue)
169 extended_cif ecif;
171 ecif.cif = cif;
172 ecif.avalue = avalue;
174 /* If the return value is a struct and we don't have
175 a return value address then we need to make one. */
176 if ((rvalue == NULL) && (cif->rtype->type == FFI_TYPE_STRUCT))
178 ecif.rvalue = alloca (cif->rtype->size);
180 else
181 ecif.rvalue = rvalue;
183 switch (cif->abi)
185 case FFI_ARCOMPACT:
186 ffi_call_ARCompact (ffi_prep_args, &ecif, cif->bytes,
187 cif->flags, ecif.rvalue, fn);
188 break;
190 default:
191 FFI_ASSERT (0);
192 break;
197 ffi_closure_inner_ARCompact (ffi_closure * closure, void *rvalue,
198 ffi_arg * args)
200 void **arg_area, **p_argv;
201 ffi_cif *cif = closure->cif;
202 char *argp = (char *) args;
203 ffi_type **p_argt;
204 int i;
206 arg_area = (void **) alloca (cif->nargs * sizeof (void *));
208 /* handle hidden argument */
209 if (cif->flags == FFI_TYPE_STRUCT)
211 rvalue = *(void **) argp;
212 argp += 4;
215 p_argv = arg_area;
217 for (i = 0, p_argt = cif->arg_types; i < cif->nargs;
218 i++, p_argt++, p_argv++)
220 size_t z;
221 int alignment;
223 /* align alignment to 4 */
224 alignment = (((*p_argt)->alignment - 1) | 3) + 1;
226 /* Align if necessary. */
227 if ((alignment - 1) & (unsigned) argp)
228 argp = (char *) ALIGN (argp, alignment);
230 z = (*p_argt)->size;
231 *p_argv = (void *) argp;
232 argp += z;
235 (closure->fun) (cif, rvalue, arg_area, closure->user_data);
237 return cif->flags;
240 extern void ffi_closure_ARCompact (void);
242 ffi_status
243 ffi_prep_closure_loc (ffi_closure * closure, ffi_cif * cif,
244 void (*fun) (ffi_cif *, void *, void **, void *),
245 void *user_data, void *codeloc)
247 uint32_t *tramp = (uint32_t *) & (closure->tramp[0]);
249 switch (cif->abi)
251 case FFI_ARCOMPACT:
252 FFI_ASSERT (tramp == codeloc);
253 tramp[0] = CODE_ENDIAN (0x200a1fc0); /* mov r8, pcl */
254 tramp[1] = CODE_ENDIAN (0x20200f80); /* j [long imm] */
255 tramp[2] = CODE_ENDIAN (ffi_closure_ARCompact);
256 break;
258 default:
259 return FFI_BAD_ABI;
262 closure->cif = cif;
263 closure->fun = fun;
264 closure->user_data = user_data;
265 cacheflush (codeloc, FFI_TRAMPOLINE_SIZE, BCACHE);
267 return FFI_OK;