2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libffi / src / sparc / ffi.c
bloba08e65ac5717cd1932638d27f61927a501b3fda1
1 /* -----------------------------------------------------------------------
2 ffi.c - Copyright (c) 1996, 2003 Red Hat, Inc.
4 Sparc 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 CYGNUS SOLUTIONS 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 ----------------------------------------------------------------------- */
26 #include <ffi.h>
27 #include <ffi_common.h>
29 #include <stdlib.h>
31 #ifdef SPARC64
32 extern void ffi_closure_v9(void);
33 #else
34 extern void ffi_closure_v8(void);
35 #endif
37 /* ffi_prep_args is called by the assembly routine once stack space
38 has been allocated for the function's arguments */
40 void ffi_prep_args_v8(char *stack, extended_cif *ecif)
42 int i;
43 void **p_argv;
44 char *argp;
45 ffi_type **p_arg;
47 /* Skip 16 words for the window save area */
48 argp = stack + 16*sizeof(int);
50 /* This should only really be done when we are returning a structure,
51 however, it's faster just to do it all the time...
53 if ( ecif->cif->rtype->type == FFI_TYPE_STRUCT ) */
54 *(int *) argp = (long)ecif->rvalue;
56 /* And 1 word for the structure return value. */
57 argp += sizeof(int);
59 #ifdef USING_PURIFY
60 /* Purify will probably complain in our assembly routine, unless we
61 zero out this memory. */
63 ((int*)argp)[0] = 0;
64 ((int*)argp)[1] = 0;
65 ((int*)argp)[2] = 0;
66 ((int*)argp)[3] = 0;
67 ((int*)argp)[4] = 0;
68 ((int*)argp)[5] = 0;
69 #endif
71 p_argv = ecif->avalue;
73 for (i = ecif->cif->nargs, p_arg = ecif->cif->arg_types; i; i--, p_arg++)
75 size_t z;
77 if ((*p_arg)->type == FFI_TYPE_STRUCT
78 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
79 || (*p_arg)->type == FFI_TYPE_LONGDOUBLE
80 #endif
83 *(unsigned int *) argp = (unsigned long)(* p_argv);
84 z = sizeof(int);
86 else
88 z = (*p_arg)->size;
89 if (z < sizeof(int))
91 z = sizeof(int);
92 switch ((*p_arg)->type)
94 case FFI_TYPE_SINT8:
95 *(signed int *) argp = *(SINT8 *)(* p_argv);
96 break;
98 case FFI_TYPE_UINT8:
99 *(unsigned int *) argp = *(UINT8 *)(* p_argv);
100 break;
102 case FFI_TYPE_SINT16:
103 *(signed int *) argp = *(SINT16 *)(* p_argv);
104 break;
106 case FFI_TYPE_UINT16:
107 *(unsigned int *) argp = *(UINT16 *)(* p_argv);
108 break;
110 default:
111 FFI_ASSERT(0);
114 else
116 memcpy(argp, *p_argv, z);
119 p_argv++;
120 argp += z;
123 return;
126 int ffi_prep_args_v9(char *stack, extended_cif *ecif)
128 int i, ret = 0;
129 int tmp;
130 void **p_argv;
131 char *argp;
132 ffi_type **p_arg;
134 tmp = 0;
136 /* Skip 16 words for the window save area */
137 argp = stack + 16*sizeof(long long);
139 #ifdef USING_PURIFY
140 /* Purify will probably complain in our assembly routine, unless we
141 zero out this memory. */
143 ((long long*)argp)[0] = 0;
144 ((long long*)argp)[1] = 0;
145 ((long long*)argp)[2] = 0;
146 ((long long*)argp)[3] = 0;
147 ((long long*)argp)[4] = 0;
148 ((long long*)argp)[5] = 0;
149 #endif
151 p_argv = ecif->avalue;
153 if (ecif->cif->rtype->type == FFI_TYPE_STRUCT &&
154 ecif->cif->rtype->size > 32)
156 *(unsigned long long *) argp = (unsigned long)ecif->rvalue;
157 tmp = 1;
160 for (i = 0, p_arg = ecif->cif->arg_types; i < ecif->cif->nargs;
161 i++, p_arg++)
163 size_t z;
165 z = (*p_arg)->size;
166 switch ((*p_arg)->type)
168 case FFI_TYPE_STRUCT:
169 if (z > 16)
171 /* For structures larger than 16 bytes we pass reference. */
172 *(unsigned long long *) argp = (unsigned long)* p_argv;
173 argp += sizeof(long long);
174 tmp++;
175 p_argv++;
176 continue;
178 /* FALLTHROUGH */
179 case FFI_TYPE_FLOAT:
180 case FFI_TYPE_DOUBLE:
181 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
182 case FFI_TYPE_LONGDOUBLE:
183 #endif
184 ret = 1; /* We should promote into FP regs as well as integer. */
185 break;
187 if (z < sizeof(long long))
189 switch ((*p_arg)->type)
191 case FFI_TYPE_SINT8:
192 *(signed long long *) argp = *(SINT8 *)(* p_argv);
193 break;
195 case FFI_TYPE_UINT8:
196 *(unsigned long long *) argp = *(UINT8 *)(* p_argv);
197 break;
199 case FFI_TYPE_SINT16:
200 *(signed long long *) argp = *(SINT16 *)(* p_argv);
201 break;
203 case FFI_TYPE_UINT16:
204 *(unsigned long long *) argp = *(UINT16 *)(* p_argv);
205 break;
207 case FFI_TYPE_SINT32:
208 *(signed long long *) argp = *(SINT32 *)(* p_argv);
209 break;
211 case FFI_TYPE_UINT32:
212 *(unsigned long long *) argp = *(UINT32 *)(* p_argv);
213 break;
215 case FFI_TYPE_FLOAT:
216 *(float *) (argp + 4) = *(FLOAT32 *)(* p_argv); /* Right justify */
217 break;
219 case FFI_TYPE_STRUCT:
220 memcpy(argp, *p_argv, z);
221 break;
223 default:
224 FFI_ASSERT(0);
226 z = sizeof(long long);
227 tmp++;
229 else if (z == sizeof(long long))
231 memcpy(argp, *p_argv, z);
232 z = sizeof(long long);
233 tmp++;
235 else
237 if ((tmp & 1) && (*p_arg)->alignment > 8)
239 tmp++;
240 argp += sizeof(long long);
242 memcpy(argp, *p_argv, z);
243 z = 2 * sizeof(long long);
244 tmp += 2;
246 p_argv++;
247 argp += z;
250 return ret;
253 /* Perform machine dependent cif processing */
254 ffi_status ffi_prep_cif_machdep(ffi_cif *cif)
256 int wordsize;
258 if (cif->abi != FFI_V9)
260 wordsize = 4;
262 /* If we are returning a struct, this will already have been added.
263 Otherwise we need to add it because it's always got to be there! */
265 if (cif->rtype->type != FFI_TYPE_STRUCT)
266 cif->bytes += wordsize;
268 /* sparc call frames require that space is allocated for 6 args,
269 even if they aren't used. Make that space if necessary. */
271 if (cif->bytes < 4*6+4)
272 cif->bytes = 4*6+4;
274 else
276 wordsize = 8;
278 /* sparc call frames require that space is allocated for 6 args,
279 even if they aren't used. Make that space if necessary. */
281 if (cif->bytes < 8*6)
282 cif->bytes = 8*6;
285 /* Adjust cif->bytes. to include 16 words for the window save area,
286 and maybe the struct/union return pointer area, */
288 cif->bytes += 16 * wordsize;
290 /* The stack must be 2 word aligned, so round bytes up
291 appropriately. */
293 cif->bytes = ALIGN(cif->bytes, 2 * wordsize);
295 /* Set the return type flag */
296 switch (cif->rtype->type)
298 case FFI_TYPE_VOID:
299 case FFI_TYPE_FLOAT:
300 case FFI_TYPE_DOUBLE:
301 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
302 case FFI_TYPE_LONGDOUBLE:
303 #endif
304 cif->flags = cif->rtype->type;
305 break;
307 case FFI_TYPE_STRUCT:
308 if (cif->abi == FFI_V9 && cif->rtype->size > 32)
309 cif->flags = FFI_TYPE_VOID;
310 else
311 cif->flags = FFI_TYPE_STRUCT;
312 break;
314 case FFI_TYPE_SINT64:
315 case FFI_TYPE_UINT64:
316 if (cif->abi != FFI_V9)
318 cif->flags = FFI_TYPE_SINT64;
319 break;
321 /* FALLTHROUGH */
322 default:
323 cif->flags = FFI_TYPE_INT;
324 break;
326 return FFI_OK;
329 int ffi_V9_return_struct(ffi_type *arg, int off, char *ret, char *intg, char *flt)
331 ffi_type **ptr = &arg->elements[0];
333 while (*ptr != NULL)
335 if (off & ((*ptr)->alignment - 1))
336 off = ALIGN(off, (*ptr)->alignment);
338 switch ((*ptr)->type)
340 case FFI_TYPE_STRUCT:
341 off = ffi_V9_return_struct(*ptr, off, ret, intg, flt);
342 break;
343 case FFI_TYPE_FLOAT:
344 case FFI_TYPE_DOUBLE:
345 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
346 case FFI_TYPE_LONGDOUBLE:
347 #endif
348 memcpy(ret + off, flt + off, (*ptr)->size);
349 off += (*ptr)->size;
350 break;
351 default:
352 memcpy(ret + off, intg + off, (*ptr)->size);
353 off += (*ptr)->size;
354 break;
356 ptr++;
358 return off;
361 extern int ffi_call_V8(void *, extended_cif *, unsigned,
362 unsigned, unsigned *, void (*fn)());
363 extern int ffi_call_V9(void *, extended_cif *, unsigned,
364 unsigned, unsigned *, void (*fn)());
366 void ffi_call(ffi_cif *cif, void (*fn)(), void *rvalue, void **avalue)
368 extended_cif ecif;
369 void *rval = rvalue;
371 ecif.cif = cif;
372 ecif.avalue = avalue;
374 /* If the return value is a struct and we don't have a return */
375 /* value address then we need to make one */
377 ecif.rvalue = rvalue;
378 if (cif->rtype->type == FFI_TYPE_STRUCT)
380 if (cif->rtype->size <= 32)
381 rval = alloca(64);
382 else
384 rval = NULL;
385 if (rvalue == NULL)
386 ecif.rvalue = alloca(cif->rtype->size);
390 switch (cif->abi)
392 case FFI_V8:
393 #ifdef SPARC64
394 /* We don't yet support calling 32bit code from 64bit */
395 FFI_ASSERT(0);
396 #else
397 ffi_call_V8(ffi_prep_args_v8, &ecif, cif->bytes,
398 cif->flags, rvalue, fn);
399 #endif
400 break;
401 case FFI_V9:
402 #ifdef SPARC64
403 ffi_call_V9(ffi_prep_args_v9, &ecif, cif->bytes,
404 cif->flags, rval, fn);
405 if (rvalue && rval && cif->rtype->type == FFI_TYPE_STRUCT)
406 ffi_V9_return_struct(cif->rtype, 0, (char *)rvalue, (char *)rval, ((char *)rval)+32);
407 #else
408 /* And vice versa */
409 FFI_ASSERT(0);
410 #endif
411 break;
412 default:
413 FFI_ASSERT(0);
414 break;
419 ffi_status
420 ffi_prep_closure (ffi_closure* closure,
421 ffi_cif* cif,
422 void (*fun)(ffi_cif*, void*, void**, void*),
423 void *user_data)
425 unsigned int *tramp = (unsigned int *) &closure->tramp[0];
426 unsigned long fn;
427 unsigned long ctx = (unsigned long) closure;
429 #ifdef SPARC64
430 /* Trampoline address is equal to the closure address. We take advantage
431 of that to reduce the trampoline size by 8 bytes. */
432 FFI_ASSERT (cif->abi == FFI_V9);
433 fn = (unsigned long) ffi_closure_v9;
434 tramp[0] = 0x83414000; /* rd %pc, %g1 */
435 tramp[1] = 0xca586010; /* ldx [%g1+16], %g5 */
436 tramp[2] = 0x81c14000; /* jmp %g5 */
437 tramp[3] = 0x01000000; /* nop */
438 *((unsigned long *) &tramp[4]) = fn;
439 #else
440 FFI_ASSERT (cif->abi == FFI_V8);
441 fn = (unsigned long) ffi_closure_v8;
442 tramp[0] = 0x03000000 | fn >> 10; /* sethi %hi(fn), %g1 */
443 tramp[1] = 0x05000000 | ctx >> 10; /* sethi %hi(ctx), %g2 */
444 tramp[2] = 0x81c06000 | (fn & 0x3ff); /* jmp %g1+%lo(fn) */
445 tramp[3] = 0x8410a000 | (ctx & 0x3ff);/* or %g2, %lo(ctx) */
446 #endif
448 closure->cif = cif;
449 closure->fun = fun;
450 closure->user_data = user_data;
452 /* Flush the Icache. FIXME: alignment isn't certain, assume 8 bytes */
453 #ifdef SPARC64
454 asm volatile ("flush %0" : : "r" (closure) : "memory");
455 asm volatile ("flush %0" : : "r" (((char *) closure) + 8) : "memory");
456 #else
457 asm volatile ("iflush %0" : : "r" (closure) : "memory");
458 asm volatile ("iflush %0" : : "r" (((char *) closure) + 8) : "memory");
459 #endif
461 return FFI_OK;
465 ffi_closure_sparc_inner(ffi_closure *closure,
466 void *rvalue, unsigned long *gpr, double *fpr)
468 ffi_cif *cif;
469 void **avalue;
470 ffi_type **arg_types;
471 int i, avn, argn;
473 cif = closure->cif;
474 avalue = alloca(cif->nargs * sizeof(void *));
476 argn = 0;
478 /* Copy the caller's structure return address to that the closure
479 returns the data directly to the caller. */
480 if (cif->flags == FFI_TYPE_STRUCT)
482 rvalue = (void *) gpr[0];
483 argn = 1;
486 i = 0;
487 avn = cif->nargs;
488 arg_types = cif->arg_types;
490 /* Grab the addresses of the arguments from the stack frame. */
491 while (i < avn)
493 /* Assume big-endian. FIXME */
494 argn += ALIGN(arg_types[i]->size, FFI_SIZEOF_ARG) / FFI_SIZEOF_ARG;
496 #ifdef SPARC64
497 if (i < 16 && (arg_types[i]->type == FFI_TYPE_FLOAT
498 || arg_types[i]->type == FFI_TYPE_DOUBLE
499 #if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE
500 || arg_types[i]->type == FFI_TYPE_LONGDOUBLE
501 #endif
503 avalue[i] = ((char *) &fpr[argn]) - arg_types[i]->size;
504 else
505 #endif
506 avalue[i] = ((char *) &gpr[argn]) - arg_types[i]->size;
507 i++;
510 /* Invoke the closure. */
511 (closure->fun) (cif, rvalue, avalue, closure->user_data);
513 /* Tell ffi_closure_sparc how to perform return type promotions. */
514 return cif->rtype->type;