1 /* -----------------------------------------------------------------------
2 ffi.c - Copyright (c) 1998, 2001 Red Hat, Inc.
4 Alpha 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 ----------------------------------------------------------------------- */
27 #include <ffi_common.h>
31 extern void ffi_call_osf(void *, unsigned long, unsigned, void *, void (*)());
32 extern void ffi_closure_osf(void);
36 ffi_prep_cif_machdep(ffi_cif
*cif
)
38 /* Adjust cif->bytes to represent a minimum 6 words for the temporary
39 register argument loading area. */
40 if (cif
->bytes
< 6*FFI_SIZEOF_ARG
)
41 cif
->bytes
= 6*FFI_SIZEOF_ARG
;
43 /* Set the return type flag */
44 switch (cif
->rtype
->type
)
49 cif
->flags
= cif
->rtype
->type
;
53 cif
->flags
= FFI_TYPE_INT
;
61 ffi_call(ffi_cif
*cif
, void (*fn
)(), void *rvalue
, void **avalue
)
63 unsigned long *stack
, *argp
;
67 FFI_ASSERT (cif
->abi
== FFI_OSF
);
69 /* If the return value is a struct and we don't have a return
70 value address then we need to make one. */
71 if (rvalue
== NULL
&& cif
->flags
== FFI_TYPE_STRUCT
)
72 rvalue
= alloca(cif
->rtype
->size
);
74 /* Allocate the space for the arguments, plus 4 words of temp
75 space for ffi_call_osf. */
76 argp
= stack
= alloca(cif
->bytes
+ 4*FFI_SIZEOF_ARG
);
78 if (cif
->flags
== FFI_TYPE_STRUCT
)
79 *(void **) argp
++ = rvalue
;
83 arg_types
= cif
->arg_types
;
87 switch ((*arg_types
)->type
)
90 *(SINT64
*) argp
= *(SINT8
*)(* avalue
);
94 *(SINT64
*) argp
= *(UINT8
*)(* avalue
);
98 *(SINT64
*) argp
= *(SINT16
*)(* avalue
);
101 case FFI_TYPE_UINT16
:
102 *(SINT64
*) argp
= *(UINT16
*)(* avalue
);
105 case FFI_TYPE_SINT32
:
106 case FFI_TYPE_UINT32
:
107 /* Note that unsigned 32-bit quantities are sign extended. */
108 *(SINT64
*) argp
= *(SINT32
*)(* avalue
);
111 case FFI_TYPE_SINT64
:
112 case FFI_TYPE_UINT64
:
113 case FFI_TYPE_POINTER
:
114 *(UINT64
*) argp
= *(UINT64
*)(* avalue
);
118 if (argp
- stack
< 6)
120 /* Note the conversion -- all the fp regs are loaded as
121 doubles. The in-register format is the same. */
122 *(double *) argp
= *(float *)(* avalue
);
125 *(float *) argp
= *(float *)(* avalue
);
128 case FFI_TYPE_DOUBLE
:
129 *(double *) argp
= *(double *)(* avalue
);
132 case FFI_TYPE_STRUCT
:
133 memcpy(argp
, *avalue
, (*arg_types
)->size
);
140 argp
+= ALIGN((*arg_types
)->size
, FFI_SIZEOF_ARG
) / FFI_SIZEOF_ARG
;
141 i
++, arg_types
++, avalue
++;
144 ffi_call_osf(stack
, cif
->bytes
, cif
->flags
, rvalue
, fn
);
149 ffi_prep_closure (ffi_closure
* closure
,
151 void (*fun
)(ffi_cif
*, void*, void**, void*),
156 FFI_ASSERT (cif
->abi
== FFI_OSF
);
158 tramp
= (unsigned int *) &closure
->tramp
[0];
159 tramp
[0] = 0x47fb0401; /* mov $27,$1 */
160 tramp
[1] = 0xa77b0010; /* ldq $27,16($27) */
161 tramp
[2] = 0x6bfb0000; /* jmp $31,($27),0 */
162 tramp
[3] = 0x47ff041f; /* nop */
163 *(void **) &tramp
[4] = ffi_closure_osf
;
167 closure
->user_data
= user_data
;
171 Tru64 UNIX as doesn't understand the imb mnemonic, so use call_pal
172 instead, since both Compaq as and gas can handle it.
174 0x86 is PAL_imb in Tru64 UNIX <alpha/pal.h>. */
175 asm volatile ("call_pal 0x86" : : : "memory");
181 ffi_closure_osf_inner(ffi_closure
*closure
, void *rvalue
, unsigned long *argp
)
185 ffi_type
**arg_types
;
189 avalue
= alloca(cif
->nargs
* sizeof(void *));
193 /* Copy the caller's structure return address to that the closure
194 returns the data directly to the caller. */
195 if (cif
->flags
== FFI_TYPE_STRUCT
)
197 rvalue
= (void *) argp
[0];
203 arg_types
= cif
->arg_types
;
205 /* Grab the addresses of the arguments from the stack frame. */
208 switch (arg_types
[i
]->type
)
212 case FFI_TYPE_SINT16
:
213 case FFI_TYPE_UINT16
:
214 case FFI_TYPE_SINT32
:
215 case FFI_TYPE_UINT32
:
216 case FFI_TYPE_SINT64
:
217 case FFI_TYPE_UINT64
:
218 case FFI_TYPE_POINTER
:
219 case FFI_TYPE_STRUCT
:
220 avalue
[i
] = &argp
[argn
];
226 /* Floats coming from registers need conversion from double
227 back to float format. */
228 *(float *)&argp
[argn
- 6] = *(double *)&argp
[argn
- 6];
229 avalue
[i
] = &argp
[argn
- 6];
232 avalue
[i
] = &argp
[argn
];
235 case FFI_TYPE_DOUBLE
:
236 avalue
[i
] = &argp
[argn
- (argn
< 6 ? 6 : 0)];
243 argn
+= ALIGN(arg_types
[i
]->size
, FFI_SIZEOF_ARG
) / FFI_SIZEOF_ARG
;
247 /* Invoke the closure. */
248 (closure
->fun
) (cif
, rvalue
, avalue
, closure
->user_data
);
250 /* Tell ffi_closure_osf how to perform return type promotions. */
251 return cif
->rtype
->type
;