1 /* -----------------------------------------------------------------------
2 ffi.c - Copyright (C) 2012, 2013 Anthony Green
4 Moxie 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>
32 /* ffi_prep_args is called by the assembly routine once stack space
33 has been allocated for the function's arguments */
35 void *ffi_prep_args(char *stack, extended_cif *ecif)
37 register unsigned int i;
38 register void **p_argv;
40 register ffi_type **p_arg;
41 register int count = 0;
43 p_argv = ecif->avalue;
46 if (ecif->cif->rtype->type == FFI_TYPE_STRUCT)
48 *(void **) argp = ecif->rvalue;
52 for (i = ecif->cif->nargs, p_arg = ecif->cif->arg_types;
60 if ((*p_arg)->type == FFI_TYPE_STRUCT)
63 *(void **) argp = *p_argv;
65 else if (z < sizeof(int))
68 switch ((*p_arg)->type)
71 *(signed int *) argp = (signed int)*(SINT8 *)(* p_argv);
75 *(unsigned int *) argp = (unsigned int)*(UINT8 *)(* p_argv);
79 *(signed int *) argp = (signed int)*(SINT16 *)(* p_argv);
83 *(unsigned int *) argp = (unsigned int)*(UINT16 *)(* p_argv);
90 else if (z == sizeof(int))
92 *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
96 memcpy(argp, *p_argv, z);
103 return (stack + ((count > 24) ? 24 : ALIGN_DOWN(count, 8)));
106 /* Perform machine dependent cif processing */
107 ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
109 if (cif->rtype->type == FFI_TYPE_STRUCT)
112 cif->flags = cif->rtype->size;
114 cif->bytes = ALIGN (cif->bytes, 8);
119 extern void ffi_call_EABI(void *(*)(char *, extended_cif *),
125 void ffi_call(ffi_cif *cif,
133 ecif.avalue = avalue;
135 /* If the return value is a struct and we don't have a return */
136 /* value address then we need to make one */
138 if ((rvalue == NULL) &&
139 (cif->rtype->type == FFI_TYPE_STRUCT))
141 ecif.rvalue = alloca(cif->rtype->size);
144 ecif.rvalue = rvalue;
149 ffi_call_EABI(ffi_prep_args, &ecif, cif->bytes,
150 cif->flags, ecif.rvalue, fn);
158 void ffi_closure_eabi (unsigned arg1, unsigned arg2, unsigned arg3,
159 unsigned arg4, unsigned arg5, unsigned arg6)
161 /* This function is called by a trampoline. The trampoline stows a
162 pointer to the ffi_closure object in $r7. We must save this
163 pointer in a place that will persist while we do our work. */
164 register ffi_closure *creg __asm__ ("$r12");
165 ffi_closure *closure = creg;
167 /* Arguments that don't fit in registers are found on the stack
168 at a fixed offset above the current frame pointer. */
169 register char *frame_pointer __asm__ ("$fp");
171 /* Pointer to a struct return value. */
172 void *struct_rvalue = (void *) arg1;
174 /* 6 words reserved for register args + 3 words from jsr */
175 char *stack_args = frame_pointer + 9*4;
177 /* Lay the register arguments down in a continuous chunk of memory. */
178 unsigned register_args[6] =
179 { arg1, arg2, arg3, arg4, arg5, arg6 };
180 char *register_args_ptr = (char *) register_args;
182 ffi_cif *cif = closure->cif;
183 ffi_type **arg_types = cif->arg_types;
184 void **avalue = alloca (cif->nargs * sizeof(void *));
185 char *ptr = (char *) register_args;
188 /* preserve struct type return pointer passing */
189 if ((cif->rtype != NULL) && (cif->rtype->type == FFI_TYPE_STRUCT)) {
191 register_args_ptr = (char *)®ister_args[1];
194 /* Find the address of each argument. */
195 for (i = 0; i < cif->nargs; i++)
197 switch (arg_types[i]->type)
203 case FFI_TYPE_SINT16:
204 case FFI_TYPE_UINT16:
207 case FFI_TYPE_SINT32:
208 case FFI_TYPE_UINT32:
210 case FFI_TYPE_POINTER:
213 case FFI_TYPE_STRUCT:
214 avalue[i] = *(void**)ptr;
217 /* This is an 8-byte value. */
224 /* If we've handled more arguments than fit in registers,
225 start looking at the those passed on the stack. */
226 if (ptr == ®ister_args[6])
230 /* Invoke the closure. */
231 if (cif->rtype && (cif->rtype->type == FFI_TYPE_STRUCT))
233 (closure->fun) (cif, struct_rvalue, avalue, closure->user_data);
237 /* Allocate space for the return value and call the function. */
239 (closure->fun) (cif, &rvalue, avalue, closure->user_data);
240 asm ("mov $r12, %0\n ld.l $r0, ($r12)\n ldo.l $r1, 4($r12)" : : "r" (&rvalue));
245 ffi_prep_closure_loc (ffi_closure* closure,
247 void (*fun)(ffi_cif*, void*, void**, void*),
251 unsigned short *tramp = (unsigned short *) &closure->tramp[0];
252 unsigned long fn = (long) ffi_closure_eabi;
253 unsigned long cls = (long) codeloc;
255 if (cif->abi != FFI_EABI)
258 fn = (unsigned long) ffi_closure_eabi;
260 tramp[0] = 0x01e0; /* ldi.l $r7, .... */
261 tramp[1] = cls >> 16;
262 tramp[2] = cls & 0xffff;
263 tramp[3] = 0x1a00; /* jmpa .... */
265 tramp[5] = fn & 0xffff;
269 closure->user_data = user_data;