2009-06-04 Andrew Haley <aph@redhat.com>
[official-gcc.git] / libffi / src / frv / ffi.c
blobb79e99089a4b9b36a76d966a03aaeebc13745a2f
1 /* -----------------------------------------------------------------------
2 ffi.c - Copyright (c) 2004 Anthony Green
3 Copyright (C) 2007 Free Software Foundation, Inc.
5 FR-V 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.h>
29 #include <ffi_common.h>
31 #include <stdlib.h>
33 /* ffi_prep_args is called by the assembly routine once stack space
34 has been allocated for the function's arguments */
36 void *ffi_prep_args(char *stack, extended_cif *ecif)
38 register unsigned int i;
39 register void **p_argv;
40 register char *argp;
41 register ffi_type **p_arg;
42 register int count = 0;
44 p_argv = ecif->avalue;
45 argp = stack;
47 for (i = ecif->cif->nargs, p_arg = ecif->cif->arg_types;
48 (i != 0);
49 i--, p_arg++)
51 size_t z;
53 z = (*p_arg)->size;
55 if ((*p_arg)->type == FFI_TYPE_STRUCT)
57 z = sizeof(void*);
58 *(void **) argp = *p_argv;
60 /* if ((*p_arg)->type == FFI_TYPE_FLOAT)
62 if (count > 24)
64 // This is going on the stack. Turn it into a double.
65 *(double *) argp = (double) *(float*)(* p_argv);
66 z = sizeof(double);
68 else
69 *(void **) argp = *(void **)(* p_argv);
70 } */
71 else if (z < sizeof(int))
73 z = sizeof(int);
74 switch ((*p_arg)->type)
76 case FFI_TYPE_SINT8:
77 *(signed int *) argp = (signed int)*(SINT8 *)(* p_argv);
78 break;
80 case FFI_TYPE_UINT8:
81 *(unsigned int *) argp = (unsigned int)*(UINT8 *)(* p_argv);
82 break;
84 case FFI_TYPE_SINT16:
85 *(signed int *) argp = (signed int)*(SINT16 *)(* p_argv);
86 break;
88 case FFI_TYPE_UINT16:
89 *(unsigned int *) argp = (unsigned int)*(UINT16 *)(* p_argv);
90 break;
92 default:
93 FFI_ASSERT(0);
96 else if (z == sizeof(int))
98 *(unsigned int *) argp = (unsigned int)*(UINT32 *)(* p_argv);
100 else
102 memcpy(argp, *p_argv, z);
104 p_argv++;
105 argp += z;
106 count += z;
109 return (stack + ((count > 24) ? 24 : ALIGN_DOWN(count, 8)));
112 /* Perform machine dependent cif processing */
113 ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
115 if (cif->rtype->type == FFI_TYPE_STRUCT)
116 cif->flags = -1;
117 else
118 cif->flags = cif->rtype->size;
120 cif->bytes = ALIGN (cif->bytes, 8);
122 return FFI_OK;
125 extern void ffi_call_EABI(void *(*)(char *, extended_cif *),
126 extended_cif *,
127 unsigned, unsigned,
128 unsigned *,
129 void (*fn)());
131 void ffi_call(ffi_cif *cif,
132 void (*fn)(),
133 void *rvalue,
134 void **avalue)
136 extended_cif ecif;
138 ecif.cif = cif;
139 ecif.avalue = avalue;
141 /* If the return value is a struct and we don't have a return */
142 /* value address then we need to make one */
144 if ((rvalue == NULL) &&
145 (cif->rtype->type == FFI_TYPE_STRUCT))
147 ecif.rvalue = alloca(cif->rtype->size);
149 else
150 ecif.rvalue = rvalue;
153 switch (cif->abi)
155 case FFI_EABI:
156 ffi_call_EABI(ffi_prep_args, &ecif, cif->bytes,
157 cif->flags, ecif.rvalue, fn);
158 break;
159 default:
160 FFI_ASSERT(0);
161 break;
165 void ffi_closure_eabi (unsigned arg1, unsigned arg2, unsigned arg3,
166 unsigned arg4, unsigned arg5, unsigned arg6)
168 /* This function is called by a trampoline. The trampoline stows a
169 pointer to the ffi_closure object in gr7. We must save this
170 pointer in a place that will persist while we do our work. */
171 register ffi_closure *creg __asm__ ("gr7");
172 ffi_closure *closure = creg;
174 /* Arguments that don't fit in registers are found on the stack
175 at a fixed offset above the current frame pointer. */
176 register char *frame_pointer __asm__ ("fp");
177 char *stack_args = frame_pointer + 16;
179 /* Lay the register arguments down in a continuous chunk of memory. */
180 unsigned register_args[6] =
181 { arg1, arg2, arg3, arg4, arg5, arg6 };
183 ffi_cif *cif = closure->cif;
184 ffi_type **arg_types = cif->arg_types;
185 void **avalue = alloca (cif->nargs * sizeof(void *));
186 char *ptr = (char *) register_args;
187 int i;
189 /* Find the address of each argument. */
190 for (i = 0; i < cif->nargs; i++)
192 switch (arg_types[i]->type)
194 case FFI_TYPE_SINT8:
195 case FFI_TYPE_UINT8:
196 avalue[i] = ptr + 3;
197 break;
198 case FFI_TYPE_SINT16:
199 case FFI_TYPE_UINT16:
200 avalue[i] = ptr + 2;
201 break;
202 case FFI_TYPE_SINT32:
203 case FFI_TYPE_UINT32:
204 case FFI_TYPE_FLOAT:
205 avalue[i] = ptr;
206 break;
207 case FFI_TYPE_STRUCT:
208 avalue[i] = *(void**)ptr;
209 break;
210 default:
211 /* This is an 8-byte value. */
212 avalue[i] = ptr;
213 ptr += 4;
214 break;
216 ptr += 4;
218 /* If we've handled more arguments than fit in registers,
219 start looking at the those passed on the stack. */
220 if (ptr == ((char *)register_args + (6*4)))
221 ptr = stack_args;
224 /* Invoke the closure. */
225 if (cif->rtype->type == FFI_TYPE_STRUCT)
227 /* The caller allocates space for the return structure, and
228 passes a pointer to this space in gr3. Use this value directly
229 as the return value. */
230 register void *return_struct_ptr __asm__("gr3");
231 (closure->fun) (cif, return_struct_ptr, avalue, closure->user_data);
233 else
235 /* Allocate space for the return value and call the function. */
236 long long rvalue;
237 (closure->fun) (cif, &rvalue, avalue, closure->user_data);
239 /* Functions return 4-byte or smaller results in gr8. 8-byte
240 values also use gr9. We fill the both, even for small return
241 values, just to avoid a branch. */
242 asm ("ldi @(%0, #0), gr8" : : "r" (&rvalue));
243 asm ("ldi @(%0, #0), gr9" : : "r" (&((int *) &rvalue)[1]));
247 ffi_status
248 ffi_prep_closure_loc (ffi_closure* closure,
249 ffi_cif* cif,
250 void (*fun)(ffi_cif*, void*, void**, void*),
251 void *user_data,
252 void *codeloc)
254 unsigned int *tramp = (unsigned int *) &closure->tramp[0];
255 unsigned long fn = (long) ffi_closure_eabi;
256 unsigned long cls = (long) codeloc;
257 #ifdef __FRV_FDPIC__
258 register void *got __asm__("gr15");
259 #endif
260 int i;
262 fn = (unsigned long) ffi_closure_eabi;
264 #ifdef __FRV_FDPIC__
265 tramp[0] = &((unsigned int *)codeloc)[2];
266 tramp[1] = got;
267 tramp[2] = 0x8cfc0000 + (fn & 0xffff); /* setlos lo(fn), gr6 */
268 tramp[3] = 0x8efc0000 + (cls & 0xffff); /* setlos lo(cls), gr7 */
269 tramp[4] = 0x8cf80000 + (fn >> 16); /* sethi hi(fn), gr6 */
270 tramp[5] = 0x8ef80000 + (cls >> 16); /* sethi hi(cls), gr7 */
271 tramp[6] = 0x9cc86000; /* ldi @(gr6, #0), gr14 */
272 tramp[7] = 0x8030e000; /* jmpl @(gr14, gr0) */
273 #else
274 tramp[0] = 0x8cfc0000 + (fn & 0xffff); /* setlos lo(fn), gr6 */
275 tramp[1] = 0x8efc0000 + (cls & 0xffff); /* setlos lo(cls), gr7 */
276 tramp[2] = 0x8cf80000 + (fn >> 16); /* sethi hi(fn), gr6 */
277 tramp[3] = 0x8ef80000 + (cls >> 16); /* sethi hi(cls), gr7 */
278 tramp[4] = 0x80300006; /* jmpl @(gr0, gr6) */
279 #endif
281 closure->cif = cif;
282 closure->fun = fun;
283 closure->user_data = user_data;
285 /* Cache flushing. */
286 for (i = 0; i < FFI_TRAMPOLINE_SIZE; i++)
287 __asm__ volatile ("dcf @(%0,%1)\n\tici @(%2,%1)" :: "r" (tramp), "r" (i),
288 "r" (codeloc));
290 return FFI_OK;