1 /* -----------------------------------------------------------------------
2 ffi.c - Copyright (c) 1998 Cygnus Solutions
4 Alpha Foreign Function Interface
6 $Id: ffi.c,v 1.1.1.1 1998/11/29 16:48:16 green Exp $
8 Permission is hereby granted, free of charge, to any person obtaining
9 a copy of this software and associated documentation files (the
10 ``Software''), to deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense, and/or sell copies of the Software, and to
13 permit persons to whom the Software is furnished to do so, subject to
14 the following conditions:
16 The above copyright notice and this permission notice shall be included
17 in all copies or substantial portions of the Software.
19 THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22 IN NO EVENT SHALL CYGNUS SOLUTIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 OTHER DEALINGS IN THE SOFTWARE.
26 ----------------------------------------------------------------------- */
29 #include <ffi_common.h>
33 extern void ffi_call_osf(void *, unsigned long, unsigned, void *, void (*)());
34 extern void ffi_closure_osf(void);
38 ffi_prep_cif_machdep(ffi_cif *cif)
40 /* Adjust cif->bytes to represent a minimum 6 words for the temporary
41 register argument loading area. */
42 if (cif->bytes < 6*SIZEOF_ARG)
43 cif->bytes = 6*SIZEOF_ARG;
45 /* Set the return type flag */
46 switch (cif->rtype->type)
51 cif->flags = cif->rtype->type;
55 cif->flags = FFI_TYPE_INT;
63 ffi_call(ffi_cif *cif, void (*fn)(), void *rvalue, void **avalue)
65 unsigned long *stack, *argp;
69 FFI_ASSERT (cif->abi == FFI_OSF);
71 /* If the return value is a struct and we don't have a return
72 value address then we need to make one. */
73 if (rvalue == NULL && cif->flags == FFI_TYPE_STRUCT)
74 rvalue = alloca(cif->rtype->size);
76 /* Allocate the space for the arguments, plus 4 words of temp
77 space for ffi_call_osf. */
78 argp = stack = alloca(cif->bytes + 4*SIZEOF_ARG);
80 if (cif->flags == FFI_TYPE_STRUCT)
81 *(void **) argp++ = rvalue;
85 arg_types = cif->arg_types;
89 switch ((*arg_types)->type)
92 *(SINT64 *) argp = *(SINT8 *)(* avalue);
96 *(SINT64 *) argp = *(UINT8 *)(* avalue);
100 *(SINT64 *) argp = *(SINT16 *)(* avalue);
103 case FFI_TYPE_UINT16:
104 *(SINT64 *) argp = *(UINT16 *)(* avalue);
107 case FFI_TYPE_SINT32:
108 case FFI_TYPE_UINT32:
109 /* Note that unsigned 32-bit quantities are sign extended. */
110 *(SINT64 *) argp = *(SINT32 *)(* avalue);
113 case FFI_TYPE_SINT64:
114 case FFI_TYPE_UINT64:
115 case FFI_TYPE_POINTER:
116 *(UINT64 *) argp = *(UINT64 *)(* avalue);
120 if (argp - stack < 6)
122 /* Note the conversion -- all the fp regs are loaded as
123 doubles. The in-register format is the same. */
124 *(double *) argp = *(float *)(* avalue);
127 *(float *) argp = *(float *)(* avalue);
130 case FFI_TYPE_DOUBLE:
131 *(double *) argp = *(double *)(* avalue);
134 case FFI_TYPE_STRUCT:
135 memcpy(argp, *avalue, (*arg_types)->size);
142 argp += ALIGN((*arg_types)->size, SIZEOF_ARG) / SIZEOF_ARG;
143 i++, arg_types++, avalue++;
146 ffi_call_osf(stack, cif->bytes, cif->flags, rvalue, fn);
151 ffi_prep_closure (ffi_closure* closure,
153 void (*fun)(ffi_cif*, void*, void**, void*),
158 FFI_ASSERT (cif->abi == FFI_OSF);
160 tramp = (unsigned int *) &closure->tramp[0];
161 tramp[0] = 0x47fb0401; /* mov $27,$1 */
162 tramp[1] = 0xa77b0010; /* ldq $27,16($27) */
163 tramp[2] = 0x6bfb0000; /* jmp $31,($27),0 */
164 tramp[3] = 0x47ff041f; /* nop */
165 *(void **) &tramp[4] = ffi_closure_osf;
169 closure->user_data = user_data;
171 /* Flush the Icache. */
172 asm volatile ("imb" : : : "memory");
178 ffi_closure_osf_inner(ffi_closure *closure, void *rvalue, unsigned long *argp)
182 ffi_type **arg_types;
186 avalue = alloca(cif->nargs * sizeof(void *));
190 /* Copy the caller's structure return address to that the closure
191 returns the data directly to the caller. */
192 if (cif->flags == FFI_TYPE_STRUCT)
194 rvalue = (void *) argp[0];
200 arg_types = cif->arg_types;
202 /* Grab the addresses of the arguments from the stack frame. */
205 switch (arg_types[i]->type)
209 case FFI_TYPE_SINT16:
210 case FFI_TYPE_UINT16:
211 case FFI_TYPE_SINT32:
212 case FFI_TYPE_UINT32:
213 case FFI_TYPE_SINT64:
214 case FFI_TYPE_UINT64:
215 case FFI_TYPE_POINTER:
216 case FFI_TYPE_STRUCT:
217 avalue[i] = &argp[argn];
223 /* Floats coming from registers need conversion from double
224 back to float format. */
225 *(float *)&argp[argn - 6] = *(double *)&argp[argn - 6];
226 avalue[i] = &argp[argn - 6];
229 avalue[i] = &argp[argn];
232 case FFI_TYPE_DOUBLE:
233 avalue[i] = &argp[argn - (argn < 6 ? 6 : 0)];
240 argn += ALIGN(arg_types[i]->size, SIZEOF_ARG) / SIZEOF_ARG;
244 /* Invoke the closure. */
245 (closure->fun) (cif, rvalue, avalue, closure->user_data);
247 /* Tell ffi_closure_osf how to perform return type promotions. */
248 return cif->rtype->type;