1 /* -----------------------------------------------------------------------
2 ffi.c - Copyright (c) 1998 Red Hat, Inc.
3 Copyright (c) 2000 Hewlett Packard Company
5 IA64 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, EXPRESS
19 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL CYGNUS SOLUTIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 OTHER DEALINGS IN THE SOFTWARE.
25 ----------------------------------------------------------------------- */
28 #include <ffi_common.h>
34 #include "ia64_flags.h"
36 /* A 64-bit pointer value. In LP64 mode, this is effectively a plain
37 pointer. In ILP32 mode, it's a pointer that's been extended to
38 64 bits by "addp4". */
39 typedef void *PTR64
__attribute__((mode(DI
)));
41 /* Memory image of fp register contents. This is the implementation
42 specific format used by ldf.fill/stf.spill. All we care about is
43 that it wants a 16 byte aligned slot. */
46 UINT64 x
[2] __attribute__((aligned(16)));
50 /* The stack layout given to ffi_call_unix and ffi_closure_unix_inner. */
54 fpreg fp_regs
[8]; /* Contents of 8 fp arg registers. */
55 UINT64 gp_regs
[8]; /* Contents of 8 gp arg registers. */
56 UINT64 other_args
[]; /* Arguments passed on stack, variable size. */
60 /* Adjust ADDR, a pointer to an 8 byte slot, to point to the low LEN bytes. */
63 endian_adjust (void *addr
, size_t len
)
66 return addr
+ (8 - len
);
72 /* Store VALUE to ADDR in the current cpu implementation's fp spill format. */
75 stf_spill(fpreg
*addr
, __float80 value
)
77 asm ("stf.spill %0 = %1%P0" : "=m" (*addr
) : "f"(value
));
80 /* Load a value from ADDR, which is in the current cpu implementation's
83 static inline __float80
87 asm ("ldf.fill %0 = %1%P1" : "=f"(ret
) : "m"(*addr
));
91 /* Return the size of the C type associated with with TYPE. Which will
92 be one of the FFI_IA64_TYPE_HFA_* values. */
95 hfa_type_size (int type
)
99 case FFI_IA64_TYPE_HFA_FLOAT
:
100 return sizeof(float);
101 case FFI_IA64_TYPE_HFA_DOUBLE
:
102 return sizeof(double);
103 case FFI_IA64_TYPE_HFA_LDOUBLE
:
104 return sizeof(__float80
);
110 /* Load from ADDR a value indicated by TYPE. Which will be one of
111 the FFI_IA64_TYPE_HFA_* values. */
114 hfa_type_load (int type
, void *addr
)
118 case FFI_IA64_TYPE_HFA_FLOAT
:
119 return *(float *) addr
;
120 case FFI_IA64_TYPE_HFA_DOUBLE
:
121 return *(double *) addr
;
122 case FFI_IA64_TYPE_HFA_LDOUBLE
:
123 return *(__float80
*) addr
;
129 /* Load VALUE into ADDR as indicated by TYPE. Which will be one of
130 the FFI_IA64_TYPE_HFA_* values. */
133 hfa_type_store (int type
, void *addr
, __float80 value
)
137 case FFI_IA64_TYPE_HFA_FLOAT
:
138 *(float *) addr
= value
;
140 case FFI_IA64_TYPE_HFA_DOUBLE
:
141 *(double *) addr
= value
;
143 case FFI_IA64_TYPE_HFA_LDOUBLE
:
144 *(__float80
*) addr
= value
;
151 /* Is TYPE a struct containing floats, doubles, or extended doubles,
152 all of the same fp type? If so, return the element type. Return
153 FFI_TYPE_VOID if not. */
156 hfa_element_type (ffi_type
*type
, int nested
)
158 int element
= FFI_TYPE_VOID
;
163 /* We want to return VOID for raw floating-point types, but the
164 synthetic HFA type if we're nested within an aggregate. */
166 element
= FFI_IA64_TYPE_HFA_FLOAT
;
169 case FFI_TYPE_DOUBLE
:
172 element
= FFI_IA64_TYPE_HFA_DOUBLE
;
175 case FFI_TYPE_LONGDOUBLE
:
176 /* Similarly, except that that HFA is true for double extended,
177 but not quad precision. Both have sizeof == 16, so tell the
178 difference based on the precision. */
179 if (LDBL_MANT_DIG
== 64 && nested
)
180 element
= FFI_IA64_TYPE_HFA_LDOUBLE
;
183 case FFI_TYPE_STRUCT
:
185 ffi_type
**ptr
= &type
->elements
[0];
187 for (ptr
= &type
->elements
[0]; *ptr
; ptr
++)
189 int sub_element
= hfa_element_type (*ptr
, 1);
190 if (sub_element
== FFI_TYPE_VOID
)
191 return FFI_TYPE_VOID
;
193 if (element
== FFI_TYPE_VOID
)
194 element
= sub_element
;
195 else if (element
!= sub_element
)
196 return FFI_TYPE_VOID
;
202 return FFI_TYPE_VOID
;
209 /* Perform machine dependent cif processing. */
212 ffi_prep_cif_machdep(ffi_cif
*cif
)
216 /* Adjust cif->bytes to include space for the bits of the ia64_args frame
217 that preceeds the integer register portion. The estimate that the
218 generic bits did for the argument space required is good enough for the
219 integer component. */
220 cif
->bytes
+= offsetof(struct ia64_args
, gp_regs
[0]);
221 if (cif
->bytes
< sizeof(struct ia64_args
))
222 cif
->bytes
= sizeof(struct ia64_args
);
224 /* Set the return type flag. */
225 flags
= cif
->rtype
->type
;
226 switch (cif
->rtype
->type
)
228 case FFI_TYPE_LONGDOUBLE
:
229 /* Leave FFI_TYPE_LONGDOUBLE as meaning double extended precision,
230 and encode quad precision as a two-word integer structure. */
231 if (LDBL_MANT_DIG
!= 64)
232 flags
= FFI_IA64_TYPE_SMALL_STRUCT
| (16 << 8);
235 case FFI_TYPE_STRUCT
:
237 size_t size
= cif
->rtype
->size
;
238 int hfa_type
= hfa_element_type (cif
->rtype
, 0);
240 if (hfa_type
!= FFI_TYPE_VOID
)
242 size_t nelts
= size
/ hfa_type_size (hfa_type
);
244 flags
= hfa_type
| (size
<< 8);
249 flags
= FFI_IA64_TYPE_SMALL_STRUCT
| (size
<< 8);
262 extern int ffi_call_unix (struct ia64_args
*, PTR64
, void (*)(), UINT64
);
265 ffi_call(ffi_cif
*cif
, void (*fn
)(), void *rvalue
, void **avalue
)
267 struct ia64_args
*stack
;
268 long i
, avn
, gpcount
, fpcount
;
271 FFI_ASSERT (cif
->abi
== FFI_UNIX
);
273 /* If we have no spot for a return value, make one. */
274 if (rvalue
== NULL
&& cif
->rtype
->type
!= FFI_TYPE_VOID
)
275 rvalue
= alloca (cif
->rtype
->size
);
277 /* Allocate the stack frame. */
278 stack
= alloca (cif
->bytes
);
280 gpcount
= fpcount
= 0;
282 for (i
= 0, p_arg
= cif
->arg_types
; i
< avn
; i
++, p_arg
++)
284 switch ((*p_arg
)->type
)
287 stack
->gp_regs
[gpcount
++] = *(SINT8
*)avalue
[i
];
290 stack
->gp_regs
[gpcount
++] = *(UINT8
*)avalue
[i
];
292 case FFI_TYPE_SINT16
:
293 stack
->gp_regs
[gpcount
++] = *(SINT16
*)avalue
[i
];
295 case FFI_TYPE_UINT16
:
296 stack
->gp_regs
[gpcount
++] = *(UINT16
*)avalue
[i
];
298 case FFI_TYPE_SINT32
:
299 stack
->gp_regs
[gpcount
++] = *(SINT32
*)avalue
[i
];
301 case FFI_TYPE_UINT32
:
302 stack
->gp_regs
[gpcount
++] = *(UINT32
*)avalue
[i
];
304 case FFI_TYPE_SINT64
:
305 case FFI_TYPE_UINT64
:
306 stack
->gp_regs
[gpcount
++] = *(UINT64
*)avalue
[i
];
309 case FFI_TYPE_POINTER
:
310 stack
->gp_regs
[gpcount
++] = (UINT64
)(PTR64
) *(void **)avalue
[i
];
314 if (gpcount
< 8 && fpcount
< 8)
315 stf_spill (&stack
->fp_regs
[fpcount
++], *(float *)avalue
[i
]);
316 stack
->gp_regs
[gpcount
++] = *(UINT32
*)avalue
[i
];
319 case FFI_TYPE_DOUBLE
:
320 if (gpcount
< 8 && fpcount
< 8)
321 stf_spill (&stack
->fp_regs
[fpcount
++], *(double *)avalue
[i
]);
322 stack
->gp_regs
[gpcount
++] = *(UINT64
*)avalue
[i
];
325 case FFI_TYPE_LONGDOUBLE
:
328 if (LDBL_MANT_DIG
== 64 && gpcount
< 8 && fpcount
< 8)
329 stf_spill (&stack
->fp_regs
[fpcount
++], *(__float80
*)avalue
[i
]);
330 memcpy (&stack
->gp_regs
[gpcount
], avalue
[i
], 16);
334 case FFI_TYPE_STRUCT
:
336 size_t size
= (*p_arg
)->size
;
337 size_t align
= (*p_arg
)->alignment
;
338 int hfa_type
= hfa_element_type (*p_arg
, 0);
340 FFI_ASSERT (align
<= 16);
341 if (align
== 16 && (gpcount
& 1))
344 if (hfa_type
!= FFI_TYPE_VOID
)
346 size_t hfa_size
= hfa_type_size (hfa_type
);
348 size_t gp_offset
= gpcount
* 8;
352 && gp_offset
< 8 * 8)
354 stf_spill (&stack
->fp_regs
[fpcount
],
355 hfa_type_load (hfa_type
, avalue
[i
] + offset
));
357 gp_offset
+= hfa_size
;
362 memcpy (&stack
->gp_regs
[gpcount
], avalue
[i
], size
);
363 gpcount
+= (size
+ 7) / 8;
372 ffi_call_unix (stack
, rvalue
, fn
, cif
->flags
);
375 /* Closures represent a pair consisting of a function pointer, and
376 some user data. A closure is invoked by reinterpreting the closure
377 as a function pointer, and branching to it. Thus we can make an
378 interpreted function callable as a C function: We turn the
379 interpreter itself, together with a pointer specifying the
380 interpreted procedure, into a closure.
382 For IA64, function pointer are already pairs consisting of a code
383 pointer, and a gp pointer. The latter is needed to access global
384 variables. Here we set up such a pair as the first two words of
385 the closure (in the "trampoline" area), but we replace the gp
386 pointer with a pointer to the closure itself. We also add the real
387 gp pointer to the closure. This allows the function entry code to
388 both retrieve the user data, and to restire the correct gp pointer. */
390 extern void ffi_closure_unix ();
393 ffi_prep_closure (ffi_closure
* closure
,
395 void (*fun
)(ffi_cif
*,void*,void**,void*),
398 /* The layout of a function descriptor. A C function pointer really
399 points to one of these. */
406 struct ffi_ia64_trampoline_struct
408 UINT64 code_pointer
; /* Pointer to ffi_closure_unix. */
409 UINT64 fake_gp
; /* Pointer to closure, installed as gp. */
410 UINT64 real_gp
; /* Real gp value. */
413 struct ffi_ia64_trampoline_struct
*tramp
;
416 FFI_ASSERT (cif
->abi
== FFI_UNIX
);
418 tramp
= (struct ffi_ia64_trampoline_struct
*)closure
->tramp
;
419 fd
= (struct ia64_fd
*)(void *)ffi_closure_unix
;
421 tramp
->code_pointer
= fd
->code_pointer
;
422 tramp
->real_gp
= fd
->gp
;
423 tramp
->fake_gp
= (UINT64
)(PTR64
)closure
;
425 closure
->user_data
= user_data
;
433 ffi_closure_unix_inner (ffi_closure
*closure
, struct ia64_args
*stack
,
434 void *rvalue
, void *r8
)
439 long i
, avn
, gpcount
, fpcount
;
443 avalue
= alloca (avn
* sizeof (void *));
445 /* If the structure return value is passed in memory get that location
446 from r8 so as to pass the value directly back to the caller. */
447 if (cif
->flags
== FFI_TYPE_STRUCT
)
450 gpcount
= fpcount
= 0;
451 for (i
= 0, p_arg
= cif
->arg_types
; i
< avn
; i
++, p_arg
++)
453 switch ((*p_arg
)->type
)
457 avalue
[i
] = endian_adjust(&stack
->gp_regs
[gpcount
++], 1);
459 case FFI_TYPE_SINT16
:
460 case FFI_TYPE_UINT16
:
461 avalue
[i
] = endian_adjust(&stack
->gp_regs
[gpcount
++], 2);
463 case FFI_TYPE_SINT32
:
464 case FFI_TYPE_UINT32
:
465 avalue
[i
] = endian_adjust(&stack
->gp_regs
[gpcount
++], 4);
467 case FFI_TYPE_SINT64
:
468 case FFI_TYPE_UINT64
:
469 avalue
[i
] = &stack
->gp_regs
[gpcount
++];
471 case FFI_TYPE_POINTER
:
472 avalue
[i
] = endian_adjust(&stack
->gp_regs
[gpcount
++], sizeof(void*));
476 if (gpcount
< 8 && fpcount
< 8)
478 void *addr
= &stack
->fp_regs
[fpcount
++];
480 *(float *)addr
= ldf_fill (addr
);
483 avalue
[i
] = endian_adjust(&stack
->gp_regs
[gpcount
], 4);
487 case FFI_TYPE_DOUBLE
:
488 if (gpcount
< 8 && fpcount
< 8)
490 void *addr
= &stack
->fp_regs
[fpcount
++];
492 *(double *)addr
= ldf_fill (addr
);
495 avalue
[i
] = &stack
->gp_regs
[gpcount
];
499 case FFI_TYPE_LONGDOUBLE
:
502 if (LDBL_MANT_DIG
== 64 && gpcount
< 8 && fpcount
< 8)
504 void *addr
= &stack
->fp_regs
[fpcount
++];
506 *(__float80
*)addr
= ldf_fill (addr
);
509 avalue
[i
] = &stack
->gp_regs
[gpcount
];
513 case FFI_TYPE_STRUCT
:
515 size_t size
= (*p_arg
)->size
;
516 size_t align
= (*p_arg
)->alignment
;
517 int hfa_type
= hfa_element_type (*p_arg
, 0);
519 FFI_ASSERT (align
<= 16);
520 if (align
== 16 && (gpcount
& 1))
523 if (hfa_type
!= FFI_TYPE_VOID
)
525 size_t hfa_size
= hfa_type_size (hfa_type
);
527 size_t gp_offset
= gpcount
* 8;
528 void *addr
= alloca (size
);
534 && gp_offset
< 8 * 8)
536 hfa_type_store (hfa_type
, addr
+ offset
,
537 ldf_fill (&stack
->fp_regs
[fpcount
]));
539 gp_offset
+= hfa_size
;
544 memcpy (addr
+ offset
, (char *)stack
->gp_regs
+ gp_offset
,
548 avalue
[i
] = &stack
->gp_regs
[gpcount
];
550 gpcount
+= (size
+ 7) / 8;
559 closure
->fun (cif
, rvalue
, avalue
, closure
->user_data
);