1 /* -----------------------------------------------------------------------
2 ffi.c - Copyright (c) 2014 Sebastian Macke <sebastian@macke.de>
4 OpenRISC 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,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 DEALINGS IN THE SOFTWARE.
25 ----------------------------------------------------------------------- */
28 #include "ffi_common.h"
30 /* ffi_prep_args is called by the assembly routine once stack space
31 has been allocated for the function's arguments */
33 void* ffi_prep_args(char *stack
, extended_cif
*ecif
)
35 char *stacktemp
= stack
;
41 nfixedargs
= ecif
->cif
->nfixedargs
;
42 arg
= ecif
->cif
->arg_types
;
43 void **argv
= ecif
->avalue
;
45 if (ecif
->cif
->rtype
->type
== FFI_TYPE_STRUCT
)
47 *(void **) stack
= ecif
->rvalue
;
51 for(i
=0; i
<ecif
->cif
->nargs
; i
++)
54 /* variadic args are saved on stack */
55 if ((nfixedargs
== 0) && (count
< 24))
58 stack
= stacktemp
+ 24;
66 *(void **)stack
= *argv
;
70 *(signed int *) stack
= (signed int)*(SINT8
*)(* argv
);
74 *(unsigned int *) stack
= (unsigned int)*(UINT8
*)(* argv
);
78 *(signed int *) stack
= (signed int)*(SINT16
*)(* argv
);
82 *(unsigned int *) stack
= (unsigned int)*(UINT16
*)(* argv
);
88 case FFI_TYPE_POINTER
:
89 *(int *)stack
= *(int*)(*argv
);
92 default: /* 8 byte types */
93 if (count
== 20) /* never split arguments */
99 memcpy(stack
, *argv
, s
);
108 return stacktemp
+ ((count
>24)?24:0);
111 extern void ffi_call_SYSV(unsigned,
113 void *(*)(int *, extended_cif
*),
119 void ffi_call(ffi_cif
*cif
, void (*fn
)(void), void *rvalue
, void **avalue
)
125 /* Calculate size to allocate on stack */
127 for(i
= 0, arg
= cif
->arg_types
, size
=0; i
< cif
->nargs
; i
++, arg
++)
129 if ((*arg
)->type
== FFI_TYPE_STRUCT
)
132 if ((*arg
)->size
<= 4)
138 /* for variadic functions more space is needed on the stack */
139 if (cif
->nargs
!= cif
->nfixedargs
)
142 if (cif
->rtype
->type
== FFI_TYPE_STRUCT
)
148 ecif
.avalue
= avalue
;
149 ecif
.rvalue
= rvalue
;
154 ffi_call_SYSV(size
, &ecif
, ffi_prep_args
, rvalue
, fn
, cif
->flags
);
163 void ffi_closure_SYSV(unsigned long r3
, unsigned long r4
, unsigned long r5
,
164 unsigned long r6
, unsigned long r7
, unsigned long r8
)
166 register int *sp
__asm__ ("r17");
167 register int *r13
__asm__ ("r13");
169 ffi_closure
* closure
= (ffi_closure
*) r13
;
170 char *stack_args
= sp
;
172 /* Lay the register arguments down in a continuous chunk of memory. */
173 unsigned register_args
[6] =
174 { r3
, r4
, r5
, r6
, r7
, r8
};
176 /* Pointer to a struct return value. */
177 void *struct_rvalue
= (void *) r3
;
179 ffi_cif
*cif
= closure
->cif
;
180 ffi_type
**arg_types
= cif
->arg_types
;
181 void **avalue
= alloca (cif
->nargs
* sizeof(void *));
182 char *ptr
= (char *) register_args
;
184 int nfixedargs
= cif
->nfixedargs
;
187 /* preserve struct type return pointer passing */
189 if ((cif
->rtype
!= NULL
) && (cif
->rtype
->type
== FFI_TYPE_STRUCT
))
195 /* Find the address of each argument. */
196 for (i
= 0; i
< cif
->nargs
; i
++)
199 /* variadic args are saved on stack */
200 if ((nfixedargs
== 0) && (count
< 24))
207 switch (arg_types
[i
]->type
)
214 case FFI_TYPE_SINT16
:
215 case FFI_TYPE_UINT16
:
219 case FFI_TYPE_SINT32
:
220 case FFI_TYPE_UINT32
:
222 case FFI_TYPE_POINTER
:
226 case FFI_TYPE_STRUCT
:
227 avalue
[i
] = *(void**)ptr
;
233 /* arguments are never splitted */
234 if (ptr
== ®ister_args
[5])
244 /* If we've handled more arguments than fit in registers,
245 start looking at the those passed on the stack. */
251 if (cif
->rtype
&& (cif
->rtype
->type
== FFI_TYPE_STRUCT
))
253 (closure
->fun
) (cif
, struct_rvalue
, avalue
, closure
->user_data
);
257 (closure
->fun
) (cif
, &rvalue
, avalue
, closure
->user_data
);
259 asm ("l.ori r12, %0, 0x0\n l.lwz r11, 0(r12)\n l.lwz r12, 4(r12)" : : "r" (&rvalue
));
265 ffi_prep_closure_loc (ffi_closure
* closure
,
267 void (*fun
)(ffi_cif
*,void*,void**,void*),
271 unsigned short *tramp
= (unsigned short *) closure
->tramp
;
272 unsigned long fn
= (unsigned long) ffi_closure_SYSV
;
273 unsigned long cls
= (unsigned long) codeloc
;
275 if (cif
->abi
!= FFI_SYSV
)
279 closure
->user_data
= user_data
;
282 /* write pointers to temporary registers */
283 tramp
[0] = (0x6 << 10) | (13 << 5); /* l.movhi r13, ... */
284 tramp
[1] = cls
>> 16;
285 tramp
[2] = (0x2a << 10) | (13 << 5) | 13; /* l.ori r13, r13, ... */
286 tramp
[3] = cls
& 0xFFFF;
288 tramp
[4] = (0x6 << 10) | (15 << 5); /* l.movhi r15, ... */
290 tramp
[6] = (0x2a << 10) | (15 << 5) | 15; /* l.ori r15, r15 ... */
291 tramp
[7] = fn
& 0xFFFF;
293 tramp
[8] = (0x11 << 10); /* l.jr r15 */
296 tramp
[10] = (0x2a << 10) | (17 << 5) | 1; /* l.ori r17, r1, ... */
303 ffi_status
ffi_prep_cif_machdep (ffi_cif
*cif
)
307 /* structures are returned as pointers */
308 if (cif
->rtype
->type
== FFI_TYPE_STRUCT
)
309 cif
->flags
= FFI_TYPE_STRUCT
;
311 if (cif
->rtype
->size
> 4)
312 cif
->flags
= FFI_TYPE_UINT64
;
314 cif
->nfixedargs
= cif
->nargs
;
320 ffi_status
ffi_prep_cif_machdep_var(ffi_cif
*cif
,
321 unsigned int nfixedargs
, unsigned int ntotalargs
)
325 status
= ffi_prep_cif_machdep (cif
);
326 cif
->nfixedargs
= nfixedargs
;