1 /* -----------------------------------------------------------------------
2 ffi.c - Copyright (c) 2012 Alexandre K. I. de Mendonca <alexandre.keunecke@gmail.com>,
3 Paulo Pizarro <paulo.pizarro@gmail.com>
5 Blackfin 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,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.
26 ----------------------------------------------------------------------- */
28 #include <ffi_common.h>
33 /* Maximum number of GPRs available for argument passing. */
39 #define FFIBFIN_RET_VOID 0
40 #define FFIBFIN_RET_BYTE 1
41 #define FFIBFIN_RET_HALFWORD 2
42 #define FFIBFIN_RET_INT64 3
43 #define FFIBFIN_RET_INT32 4
45 /*====================================================================*/
47 /*====================================================================*/
48 void ffi_prep_args(unsigned char *, extended_cif
*);
50 /*====================================================================*/
53 /*====================================================================*/
55 extern void ffi_call_SYSV(unsigned, extended_cif
*, void(*)(unsigned char *, extended_cif
*), unsigned, void *, void(*fn
)(void));
57 /*====================================================================*/
60 /*====================================================================*/
64 * This function calculates the return type (size) based on type.
67 ffi_status
ffi_prep_cif_machdep(ffi_cif
*cif
)
69 /* --------------------------------------*
71 * --------------------------------------*/
72 switch (cif
->rtype
->type
) {
74 cif
->flags
= FFIBFIN_RET_VOID
;
78 cif
->flags
= FFIBFIN_RET_HALFWORD
;
81 cif
->flags
= FFIBFIN_RET_BYTE
;
87 case FFI_TYPE_POINTER
:
89 cif
->flags
= FFIBFIN_RET_INT32
;
94 cif
->flags
= FFIBFIN_RET_INT64
;
97 if (cif
->rtype
->size
<= 4){
98 cif
->flags
= FFIBFIN_RET_INT32
;
99 }else if (cif
->rtype
->size
== 8){
100 cif
->flags
= FFIBFIN_RET_INT64
;
102 //it will return via a hidden pointer in P0
103 cif
->flags
= FFIBFIN_RET_VOID
;
114 * This will prepare the arguments and will call the assembly routine
115 * cif = the call interface
116 * fn = the function to be called
117 * rvalue = the return value
118 * avalue = the arguments
120 void ffi_call(ffi_cif
*cif
, void(*fn
)(void), void *rvalue
, void **avalue
)
122 int ret_type
= cif
->flags
;
125 ecif
.avalue
= avalue
;
126 ecif
.rvalue
= rvalue
;
130 ffi_call_SYSV(cif
->bytes
, &ecif
, ffi_prep_args
, ret_type
, ecif
.rvalue
, fn
);
140 * This function prepares the parameters (copies them from the ecif to the stack)
141 * to call the function (ffi_prep_args is called by the assembly routine in file
142 * sysv.S, which also calls the actual function)
144 void ffi_prep_args(unsigned char *stack
, extended_cif
*ecif
)
146 register unsigned int i
= 0;
151 p_argv
= ecif
->avalue
;
152 for (i
= ecif
->cif
->nargs
, p_arg
= ecif
->cif
->arg_types
;
157 if (z
< sizeof(int)) {
159 switch ((*p_arg
)->type
) {
160 case FFI_TYPE_SINT8
: {
161 signed char v
= *(SINT8
*)(* p_argv
);
163 *(signed int *) argp
= t
;
166 case FFI_TYPE_UINT8
: {
167 unsigned char v
= *(UINT8
*)(* p_argv
);
169 *(unsigned int *) argp
= t
;
172 case FFI_TYPE_SINT16
:
173 *(signed int *) argp
= (signed int) * (SINT16
*)(* p_argv
);
175 case FFI_TYPE_UINT16
:
176 *(unsigned int *) argp
= (unsigned int) * (UINT16
*)(* p_argv
);
178 case FFI_TYPE_STRUCT
:
179 memcpy(argp
, *p_argv
, (*p_arg
)->size
);
185 } else if (z
== sizeof(int)) {
186 *(unsigned int *) argp
= (unsigned int) * (UINT32
*)(* p_argv
);
188 memcpy(argp
, *p_argv
, z
);