]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libffi/src/arc/ffi.c
4d10b21a5d95d9a7a4ec1fa2991d2dd2680d8b97
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 ----------------------------------------------------------------------- */
27 #include <ffi_common.h>
32 #include <sys/cachectl.h>
34 /* for little endian ARC, the code is in fact stored as mixed endian for
35 performance reasons */
37 #define CODE_ENDIAN(x) (x)
39 #define CODE_ENDIAN(x) ( (((uint32_t) (x)) << 16) | (((uint32_t) (x)) >> 16))
42 /* ffi_prep_args is called by the assembly routine once stack
43 space has been allocated for the function's arguments. */
46 ffi_prep_args (char *stack
, extended_cif
* ecif
)
55 if (ecif
->cif
->rtype
->type
== FFI_TYPE_STRUCT
)
57 *(void **) argp
= ecif
->rvalue
;
61 p_argv
= ecif
->avalue
;
63 for (i
= ecif
->cif
->nargs
, p_arg
= ecif
->cif
->arg_types
;
64 (i
!= 0); i
--, p_arg
++)
69 /* align alignment to 4 */
70 alignment
= (((*p_arg
)->alignment
- 1) | 3) + 1;
72 /* Align if necessary. */
73 if ((alignment
- 1) & (unsigned) argp
)
74 argp
= (char *) FFI_ALIGN (argp
, alignment
);
81 switch ((*p_arg
)->type
)
84 *(signed int *) argp
= (signed int) *(SINT8
*) (*p_argv
);
88 *(unsigned int *) argp
= (unsigned int) *(UINT8
*) (*p_argv
);
92 *(signed int *) argp
= (signed int) *(SINT16
*) (*p_argv
);
96 *(unsigned int *) argp
= (unsigned int) *(UINT16
*) (*p_argv
);
100 memcpy (argp
, *p_argv
, (*p_arg
)->size
);
107 else if (z
== sizeof (int))
109 *(unsigned int *) argp
= (unsigned int) *(UINT32
*) (*p_argv
);
113 if ((*p_arg
)->type
== FFI_TYPE_STRUCT
)
115 memcpy (argp
, *p_argv
, z
);
119 /* Double or long long 64bit. */
120 memcpy (argp
, *p_argv
, z
);
130 /* Perform machine dependent cif processing. */
132 ffi_prep_cif_machdep (ffi_cif
* cif
)
134 /* Set the return type flag. */
135 switch (cif
->rtype
->type
)
138 cif
->flags
= (unsigned) cif
->rtype
->type
;
141 case FFI_TYPE_STRUCT
:
142 cif
->flags
= (unsigned) cif
->rtype
->type
;
145 case FFI_TYPE_SINT64
:
146 case FFI_TYPE_UINT64
:
147 case FFI_TYPE_DOUBLE
:
148 cif
->flags
= FFI_TYPE_DOUBLE
;
153 cif
->flags
= FFI_TYPE_INT
;
160 extern void ffi_call_ARCompact (void (*)(char *, extended_cif
*),
161 extended_cif
*, unsigned, unsigned,
162 unsigned *, void (*fn
) (void));
165 ffi_call (ffi_cif
* cif
, void (*fn
) (void), void *rvalue
, void **avalue
)
170 ecif
.avalue
= avalue
;
172 /* If the return value is a struct and we don't have
173 a return value address then we need to make one. */
174 if ((rvalue
== NULL
) && (cif
->rtype
->type
== FFI_TYPE_STRUCT
))
176 ecif
.rvalue
= alloca (cif
->rtype
->size
);
179 ecif
.rvalue
= rvalue
;
184 ffi_call_ARCompact (ffi_prep_args
, &ecif
, cif
->bytes
,
185 cif
->flags
, ecif
.rvalue
, fn
);
195 ffi_closure_inner_ARCompact (ffi_closure
* closure
, void *rvalue
,
198 void **arg_area
, **p_argv
;
199 ffi_cif
*cif
= closure
->cif
;
200 char *argp
= (char *) args
;
204 arg_area
= (void **) alloca (cif
->nargs
* sizeof (void *));
206 /* handle hidden argument */
207 if (cif
->flags
== FFI_TYPE_STRUCT
)
209 rvalue
= *(void **) argp
;
215 for (i
= 0, p_argt
= cif
->arg_types
; i
< cif
->nargs
;
216 i
++, p_argt
++, p_argv
++)
221 /* align alignment to 4 */
222 alignment
= (((*p_argt
)->alignment
- 1) | 3) + 1;
224 /* Align if necessary. */
225 if ((alignment
- 1) & (unsigned) argp
)
226 argp
= (char *) FFI_ALIGN (argp
, alignment
);
229 *p_argv
= (void *) argp
;
233 (closure
->fun
) (cif
, rvalue
, arg_area
, closure
->user_data
);
238 extern void ffi_closure_ARCompact (void);
241 ffi_prep_closure_loc (ffi_closure
* closure
, ffi_cif
* cif
,
242 void (*fun
) (ffi_cif
*, void *, void **, void *),
243 void *user_data
, void *codeloc
)
245 uint32_t *tramp
= (uint32_t *) & (closure
->tramp
[0]);
250 FFI_ASSERT (tramp
== codeloc
);
251 tramp
[0] = CODE_ENDIAN (0x200a1fc0); /* mov r8, pcl */
252 tramp
[1] = CODE_ENDIAN (0x20200f80); /* j [long imm] */
253 tramp
[2] = CODE_ENDIAN (ffi_closure_ARCompact
);
262 closure
->user_data
= user_data
;
263 cacheflush (codeloc
, FFI_TRAMPOLINE_SIZE
, BCACHE
);