| 1 |
/* ----------------------------------------------------------------------- |
| 2 |
ffi_darwin.c |
| 3 |
|
| 4 |
Copyright (C) 1998 Geoffrey Keating |
| 5 |
Copyright (C) 2001 John Hornkvist |
| 6 |
Copyright (C) 2002, 2006, 2007, 2009, 2010 Free Software Foundation, Inc. |
| 7 |
|
| 8 |
FFI support for Darwin and AIX. |
| 9 |
|
| 10 |
Permission is hereby granted, free of charge, to any person obtaining |
| 11 |
a copy of this software and associated documentation files (the |
| 12 |
``Software''), to deal in the Software without restriction, including |
| 13 |
without limitation the rights to use, copy, modify, merge, publish, |
| 14 |
distribute, sublicense, and/or sell copies of the Software, and to |
| 15 |
permit persons to whom the Software is furnished to do so, subject to |
| 16 |
the following conditions: |
| 17 |
|
| 18 |
The above copyright notice and this permission notice shall be included |
| 19 |
in all copies or substantial portions of the Software. |
| 20 |
|
| 21 |
THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 22 |
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 23 |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 24 |
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 25 |
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 26 |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 27 |
OTHER DEALINGS IN THE SOFTWARE. |
| 28 |
----------------------------------------------------------------------- */ |
| 29 |
|
| 30 |
#include <ffi.h> |
| 31 |
#include <ffi_common.h> |
| 32 |
|
| 33 |
#include <stdlib.h> |
| 34 |
|
| 35 |
extern void ffi_closure_ASM (void); |
| 36 |
|
| 37 |
#if defined (FFI_GO_CLOSURES) |
| 38 |
extern void ffi_go_closure_ASM (void); |
| 39 |
#endif |
| 40 |
|
| 41 |
enum { |
| 42 |
/* The assembly depends on these exact flags. |
| 43 |
For Darwin64 (when FLAG_RETURNS_STRUCT is set): |
| 44 |
FLAG_RETURNS_FP indicates that the structure embeds FP data. |
| 45 |
FLAG_RETURNS_128BITS signals a special struct size that is not |
| 46 |
expanded for float content. */ |
| 47 |
FLAG_RETURNS_128BITS = 1 << (31-31), /* These go in cr7 */ |
| 48 |
FLAG_RETURNS_NOTHING = 1 << (31-30), |
| 49 |
FLAG_RETURNS_FP = 1 << (31-29), |
| 50 |
FLAG_RETURNS_64BITS = 1 << (31-28), |
| 51 |
|
| 52 |
FLAG_RETURNS_STRUCT = 1 << (31-27), /* This goes in cr6 */ |
| 53 |
|
| 54 |
FLAG_ARG_NEEDS_COPY = 1 << (31- 7), |
| 55 |
FLAG_FP_ARGUMENTS = 1 << (31- 6), /* cr1.eq; specified by ABI */ |
| 56 |
FLAG_4_GPR_ARGUMENTS = 1 << (31- 5), |
| 57 |
FLAG_RETVAL_REFERENCE = 1 << (31- 4) |
| 58 |
}; |
| 59 |
|
| 60 |
/* About the DARWIN ABI. */ |
| 61 |
enum { |
| 62 |
NUM_GPR_ARG_REGISTERS = 8, |
| 63 |
NUM_FPR_ARG_REGISTERS = 13, |
| 64 |
LINKAGE_AREA_GPRS = 6 |
| 65 |
}; |
| 66 |
|
| 67 |
enum { ASM_NEEDS_REGISTERS = 4 }; /* r28-r31 */ |
| 68 |
|
| 69 |
/* ffi_prep_args is called by the assembly routine once stack space |
| 70 |
has been allocated for the function's arguments. |
| 71 |
|
| 72 |
m32/m64 |
| 73 |
|
| 74 |
The stack layout we want looks like this: |
| 75 |
|
| 76 |
| Return address from ffi_call_DARWIN | higher addresses |
| 77 |
|--------------------------------------------| |
| 78 |
| Previous backchain pointer 4/8 | stack pointer here |
| 79 |
|--------------------------------------------|<+ <<< on entry to |
| 80 |
| ASM_NEEDS_REGISTERS=r28-r31 4*(4/8) | | ffi_call_DARWIN |
| 81 |
|--------------------------------------------| | |
| 82 |
| When we have any FP activity... the | | |
| 83 |
| FPRs occupy NUM_FPR_ARG_REGISTERS slots | | |
| 84 |
| here fp13 .. fp1 from high to low addr. | | |
| 85 |
~ ~ ~ |
| 86 |
| Parameters (at least 8*4/8=32/64) | | NUM_GPR_ARG_REGISTERS |
| 87 |
|--------------------------------------------| | |
| 88 |
| TOC=R2 (AIX) Reserved (Darwin) 4/8 | | |
| 89 |
|--------------------------------------------| | stack | |
| 90 |
| Reserved 2*4/8 | | grows | |
| 91 |
|--------------------------------------------| | down V |
| 92 |
| Space for callee's LR 4/8 | | |
| 93 |
|--------------------------------------------| | lower addresses |
| 94 |
| Saved CR [low word for m64] 4/8 | | |
| 95 |
|--------------------------------------------| | stack pointer here |
| 96 |
| Current backchain pointer 4/8 |-/ during |
| 97 |
|--------------------------------------------| <<< ffi_call_DARWIN |
| 98 |
|
| 99 |
*/ |
| 100 |
|
| 101 |
#if defined(POWERPC_DARWIN64) |
| 102 |
static void |
| 103 |
darwin64_pass_struct_by_value |
| 104 |
(ffi_type *, char *, unsigned, unsigned *, double **, unsigned long **); |
| 105 |
#endif |
| 106 |
|
| 107 |
/* This depends on GPR_SIZE = sizeof (unsigned long) */ |
| 108 |
|
| 109 |
void |
| 110 |
ffi_prep_args (extended_cif *ecif, unsigned long *const stack) |
| 111 |
{ |
| 112 |
const unsigned bytes = ecif->cif->bytes; |
| 113 |
const unsigned flags = ecif->cif->flags; |
| 114 |
const unsigned nargs = ecif->cif->nargs; |
| 115 |
#if !defined(POWERPC_DARWIN64) |
| 116 |
const ffi_abi abi = ecif->cif->abi; |
| 117 |
#endif |
| 118 |
|
| 119 |
/* 'stacktop' points at the previous backchain pointer. */ |
| 120 |
unsigned long *const stacktop = stack + (bytes / sizeof(unsigned long)); |
| 121 |
|
| 122 |
/* 'fpr_base' points at the space for fpr1, and grows upwards as |
| 123 |
we use FPR registers. */ |
| 124 |
double *fpr_base = (double *) (stacktop - ASM_NEEDS_REGISTERS) - NUM_FPR_ARG_REGISTERS; |
| 125 |
int gp_count = 0, fparg_count = 0; |
| 126 |
|
| 127 |
/* 'next_arg' grows up as we put parameters in it. */ |
| 128 |
unsigned long *next_arg = stack + LINKAGE_AREA_GPRS; /* 6 reserved positions. */ |
| 129 |
|
| 130 |
int i; |
| 131 |
double double_tmp; |
| 132 |
void **p_argv = ecif->avalue; |
| 133 |
unsigned long gprvalue; |
| 134 |
ffi_type** ptr = ecif->cif->arg_types; |
| 135 |
#if !defined(POWERPC_DARWIN64) |
| 136 |
char *dest_cpy; |
| 137 |
#endif |
| 138 |
unsigned size_al = 0; |
| 139 |
|
| 140 |
/* Check that everything starts aligned properly. */ |
| 141 |
FFI_ASSERT(((unsigned) (char *) stack & 0xF) == 0); |
| 142 |
FFI_ASSERT(((unsigned) (char *) stacktop & 0xF) == 0); |
| 143 |
FFI_ASSERT((bytes & 0xF) == 0); |
| 144 |
|
| 145 |
/* Deal with return values that are actually pass-by-reference. |
| 146 |
Rule: |
| 147 |
Return values are referenced by r3, so r4 is the first parameter. */ |
| 148 |
|
| 149 |
if (flags & FLAG_RETVAL_REFERENCE) |
| 150 |
*next_arg++ = (unsigned long) (char *) ecif->rvalue; |
| 151 |
|
| 152 |
/* Now for the arguments. */ |
| 153 |
for (i = nargs; i > 0; i--, ptr++, p_argv++) |
| 154 |
{ |
| 155 |
switch ((*ptr)->type) |
| 156 |
{ |
| 157 |
/* If a floating-point parameter appears before all of the general- |
| 158 |
purpose registers are filled, the corresponding GPRs that match |
| 159 |
the size of the floating-point parameter are skipped. */ |
| 160 |
case FFI_TYPE_FLOAT: |
| 161 |
double_tmp = *(float *) *p_argv; |
| 162 |
if (fparg_count < NUM_FPR_ARG_REGISTERS) |
| 163 |
*fpr_base++ = double_tmp; |
| 164 |
#if defined(POWERPC_DARWIN) |
| 165 |
*(float *)next_arg = *(float *) *p_argv; |
| 166 |
#else |
| 167 |
*(double *)next_arg = double_tmp; |
| 168 |
#endif |
| 169 |
next_arg++; |
| 170 |
gp_count++; |
| 171 |
fparg_count++; |
| 172 |
FFI_ASSERT(flags & FLAG_FP_ARGUMENTS); |
| 173 |
break; |
| 174 |
|
| 175 |
case FFI_TYPE_DOUBLE: |
| 176 |
double_tmp = *(double *) *p_argv; |
| 177 |
if (fparg_count < NUM_FPR_ARG_REGISTERS) |
| 178 |
*fpr_base++ = double_tmp; |
| 179 |
*(double *)next_arg = double_tmp; |
| 180 |
#ifdef POWERPC64 |
| 181 |
next_arg++; |
| 182 |
gp_count++; |
| 183 |
#else |
| 184 |
next_arg += 2; |
| 185 |
gp_count += 2; |
| 186 |
#endif |
| 187 |
fparg_count++; |
| 188 |
FFI_ASSERT(flags & FLAG_FP_ARGUMENTS); |
| 189 |
break; |
| 190 |
|
| 191 |
#if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE |
| 192 |
|
| 193 |
case FFI_TYPE_LONGDOUBLE: |
| 194 |
# if defined(POWERPC64) && !defined(POWERPC_DARWIN64) |
| 195 |
/* ??? This will exceed the regs count when the value starts at fp13 |
| 196 |
and it will not put the extra bit on the stack. */ |
| 197 |
if (fparg_count < NUM_FPR_ARG_REGISTERS) |
| 198 |
*(long double *) fpr_base++ = *(long double *) *p_argv; |
| 199 |
else |
| 200 |
*(long double *) next_arg = *(long double *) *p_argv; |
| 201 |
next_arg += 2; |
| 202 |
fparg_count += 2; |
| 203 |
# else |
| 204 |
double_tmp = ((double *) *p_argv)[0]; |
| 205 |
if (fparg_count < NUM_FPR_ARG_REGISTERS) |
| 206 |
*fpr_base++ = double_tmp; |
| 207 |
*(double *) next_arg = double_tmp; |
| 208 |
# if defined(POWERPC_DARWIN64) |
| 209 |
next_arg++; |
| 210 |
gp_count++; |
| 211 |
# else |
| 212 |
next_arg += 2; |
| 213 |
gp_count += 2; |
| 214 |
# endif |
| 215 |
fparg_count++; |
| 216 |
double_tmp = ((double *) *p_argv)[1]; |
| 217 |
if (fparg_count < NUM_FPR_ARG_REGISTERS) |
| 218 |
*fpr_base++ = double_tmp; |
| 219 |
*(double *) next_arg = double_tmp; |
| 220 |
# if defined(POWERPC_DARWIN64) |
| 221 |
next_arg++; |
| 222 |
gp_count++; |
| 223 |
# else |
| 224 |
next_arg += 2; |
| 225 |
gp_count += 2; |
| 226 |
# endif |
| 227 |
fparg_count++; |
| 228 |
# endif |
| 229 |
FFI_ASSERT(flags & FLAG_FP_ARGUMENTS); |
| 230 |
break; |
| 231 |
#endif |
| 232 |
case FFI_TYPE_UINT64: |
| 233 |
case FFI_TYPE_SINT64: |
| 234 |
#ifdef POWERPC64 |
| 235 |
gprvalue = *(long long *) *p_argv; |
| 236 |
goto putgpr; |
| 237 |
#else |
| 238 |
*(long long *) next_arg = *(long long *) *p_argv; |
| 239 |
next_arg += 2; |
| 240 |
gp_count += 2; |
| 241 |
#endif |
| 242 |
break; |
| 243 |
case FFI_TYPE_POINTER: |
| 244 |
gprvalue = *(unsigned long *) *p_argv; |
| 245 |
goto putgpr; |
| 246 |
case FFI_TYPE_UINT8: |
| 247 |
gprvalue = *(unsigned char *) *p_argv; |
| 248 |
goto putgpr; |
| 249 |
case FFI_TYPE_SINT8: |
| 250 |
gprvalue = *(signed char *) *p_argv; |
| 251 |
goto putgpr; |
| 252 |
case FFI_TYPE_UINT16: |
| 253 |
gprvalue = *(unsigned short *) *p_argv; |
| 254 |
goto putgpr; |
| 255 |
case FFI_TYPE_SINT16: |
| 256 |
gprvalue = *(signed short *) *p_argv; |
| 257 |
goto putgpr; |
| 258 |
|
| 259 |
case FFI_TYPE_STRUCT: |
| 260 |
size_al = (*ptr)->size; |
| 261 |
#if defined(POWERPC_DARWIN64) |
| 262 |
next_arg = (unsigned long *)FFI_ALIGN((char *)next_arg, (*ptr)->alignment); |
| 263 |
darwin64_pass_struct_by_value (*ptr, (char *) *p_argv, |
| 264 |
(unsigned) size_al, |
| 265 |
(unsigned int *) &fparg_count, |
| 266 |
&fpr_base, &next_arg); |
| 267 |
#else |
| 268 |
dest_cpy = (char *) next_arg; |
| 269 |
|
| 270 |
/* If the first member of the struct is a double, then include enough |
| 271 |
padding in the struct size to align it to double-word. */ |
| 272 |
if ((*ptr)->elements[0]->type == FFI_TYPE_DOUBLE) |
| 273 |
size_al = FFI_ALIGN((*ptr)->size, 8); |
| 274 |
|
| 275 |
# if defined(POWERPC64) |
| 276 |
FFI_ASSERT (abi != FFI_DARWIN); |
| 277 |
memcpy ((char *) dest_cpy, (char *) *p_argv, size_al); |
| 278 |
next_arg += (size_al + 7) / 8; |
| 279 |
# else |
| 280 |
/* Structures that match the basic modes (QI 1 byte, HI 2 bytes, |
| 281 |
SI 4 bytes) are aligned as if they were those modes. |
| 282 |
Structures with 3 byte in size are padded upwards. */ |
| 283 |
if (size_al < 3 && abi == FFI_DARWIN) |
| 284 |
dest_cpy += 4 - size_al; |
| 285 |
|
| 286 |
memcpy((char *) dest_cpy, (char *) *p_argv, size_al); |
| 287 |
next_arg += (size_al + 3) / 4; |
| 288 |
# endif |
| 289 |
#endif |
| 290 |
break; |
| 291 |
|
| 292 |
case FFI_TYPE_INT: |
| 293 |
case FFI_TYPE_SINT32: |
| 294 |
gprvalue = *(signed int *) *p_argv; |
| 295 |
goto putgpr; |
| 296 |
|
| 297 |
case FFI_TYPE_UINT32: |
| 298 |
gprvalue = *(unsigned int *) *p_argv; |
| 299 |
putgpr: |
| 300 |
*next_arg++ = gprvalue; |
| 301 |
gp_count++; |
| 302 |
break; |
| 303 |
default: |
| 304 |
break; |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
/* Check that we didn't overrun the stack... */ |
| 309 |
/* FFI_ASSERT(gpr_base <= stacktop - ASM_NEEDS_REGISTERS); |
| 310 |
FFI_ASSERT((unsigned *)fpr_base |
| 311 |
<= stacktop - ASM_NEEDS_REGISTERS - NUM_GPR_ARG_REGISTERS); |
| 312 |
FFI_ASSERT(flags & FLAG_4_GPR_ARGUMENTS || intarg_count <= 4); */ |
| 313 |
} |
| 314 |
|
| 315 |
#if defined(POWERPC_DARWIN64) |
| 316 |
|
| 317 |
/* See if we can put some of the struct into fprs. |
| 318 |
This should not be called for structures of size 16 bytes, since these are not |
| 319 |
broken out this way. */ |
| 320 |
static void |
| 321 |
darwin64_scan_struct_for_floats (ffi_type *s, unsigned *nfpr) |
| 322 |
{ |
| 323 |
int i; |
| 324 |
|
| 325 |
FFI_ASSERT (s->type == FFI_TYPE_STRUCT) |
| 326 |
|
| 327 |
for (i = 0; s->elements[i] != NULL; i++) |
| 328 |
{ |
| 329 |
ffi_type *p = s->elements[i]; |
| 330 |
switch (p->type) |
| 331 |
{ |
| 332 |
case FFI_TYPE_STRUCT: |
| 333 |
darwin64_scan_struct_for_floats (p, nfpr); |
| 334 |
break; |
| 335 |
case FFI_TYPE_LONGDOUBLE: |
| 336 |
(*nfpr) += 2; |
| 337 |
break; |
| 338 |
case FFI_TYPE_DOUBLE: |
| 339 |
case FFI_TYPE_FLOAT: |
| 340 |
(*nfpr) += 1; |
| 341 |
break; |
| 342 |
default: |
| 343 |
break; |
| 344 |
} |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
static int |
| 349 |
darwin64_struct_size_exceeds_gprs_p (ffi_type *s, char *src, unsigned *nfpr) |
| 350 |
{ |
| 351 |
unsigned struct_offset=0, i; |
| 352 |
|
| 353 |
for (i = 0; s->elements[i] != NULL; i++) |
| 354 |
{ |
| 355 |
char *item_base; |
| 356 |
ffi_type *p = s->elements[i]; |
| 357 |
/* Find the start of this item (0 for the first one). */ |
| 358 |
if (i > 0) |
| 359 |
struct_offset = FFI_ALIGN(struct_offset, p->alignment); |
| 360 |
|
| 361 |
item_base = src + struct_offset; |
| 362 |
|
| 363 |
switch (p->type) |
| 364 |
{ |
| 365 |
case FFI_TYPE_STRUCT: |
| 366 |
if (darwin64_struct_size_exceeds_gprs_p (p, item_base, nfpr)) |
| 367 |
return 1; |
| 368 |
break; |
| 369 |
case FFI_TYPE_LONGDOUBLE: |
| 370 |
if (*nfpr >= NUM_FPR_ARG_REGISTERS) |
| 371 |
return 1; |
| 372 |
(*nfpr) += 1; |
| 373 |
item_base += 8; |
| 374 |
/* FALL THROUGH */ |
| 375 |
case FFI_TYPE_DOUBLE: |
| 376 |
if (*nfpr >= NUM_FPR_ARG_REGISTERS) |
| 377 |
return 1; |
| 378 |
(*nfpr) += 1; |
| 379 |
break; |
| 380 |
case FFI_TYPE_FLOAT: |
| 381 |
if (*nfpr >= NUM_FPR_ARG_REGISTERS) |
| 382 |
return 1; |
| 383 |
(*nfpr) += 1; |
| 384 |
break; |
| 385 |
default: |
| 386 |
/* If we try and place any item, that is non-float, once we've |
| 387 |
exceeded the 8 GPR mark, then we can't fit the struct. */ |
| 388 |
if ((unsigned long)item_base >= 8*8) |
| 389 |
return 1; |
| 390 |
break; |
| 391 |
} |
| 392 |
/* now count the size of what we just used. */ |
| 393 |
struct_offset += p->size; |
| 394 |
} |
| 395 |
return 0; |
| 396 |
} |
| 397 |
|
| 398 |
/* Can this struct be returned by value? */ |
| 399 |
int |
| 400 |
darwin64_struct_ret_by_value_p (ffi_type *s) |
| 401 |
{ |
| 402 |
unsigned nfp = 0; |
| 403 |
|
| 404 |
FFI_ASSERT (s && s->type == FFI_TYPE_STRUCT); |
| 405 |
|
| 406 |
/* The largest structure we can return is 8long + 13 doubles. */ |
| 407 |
if (s->size > 168) |
| 408 |
return 0; |
| 409 |
|
| 410 |
/* We can't pass more than 13 floats. */ |
| 411 |
darwin64_scan_struct_for_floats (s, &nfp); |
| 412 |
if (nfp > 13) |
| 413 |
return 0; |
| 414 |
|
| 415 |
/* If there are not too many floats, and the struct is |
| 416 |
small enough to accommodate in the GPRs, then it must be OK. */ |
| 417 |
if (s->size <= 64) |
| 418 |
return 1; |
| 419 |
|
| 420 |
/* Well, we have to look harder. */ |
| 421 |
nfp = 0; |
| 422 |
if (darwin64_struct_size_exceeds_gprs_p (s, NULL, &nfp)) |
| 423 |
return 0; |
| 424 |
|
| 425 |
return 1; |
| 426 |
} |
| 427 |
|
| 428 |
void |
| 429 |
darwin64_pass_struct_floats (ffi_type *s, char *src, |
| 430 |
unsigned *nfpr, double **fprs) |
| 431 |
{ |
| 432 |
int i; |
| 433 |
double *fpr_base = *fprs; |
| 434 |
unsigned struct_offset = 0; |
| 435 |
|
| 436 |
/* We don't assume anything about the alignment of the source. */ |
| 437 |
for (i = 0; s->elements[i] != NULL; i++) |
| 438 |
{ |
| 439 |
char *item_base; |
| 440 |
ffi_type *p = s->elements[i]; |
| 441 |
/* Find the start of this item (0 for the first one). */ |
| 442 |
if (i > 0) |
| 443 |
struct_offset = FFI_ALIGN(struct_offset, p->alignment); |
| 444 |
item_base = src + struct_offset; |
| 445 |
|
| 446 |
switch (p->type) |
| 447 |
{ |
| 448 |
case FFI_TYPE_STRUCT: |
| 449 |
darwin64_pass_struct_floats (p, item_base, nfpr, |
| 450 |
&fpr_base); |
| 451 |
break; |
| 452 |
case FFI_TYPE_LONGDOUBLE: |
| 453 |
if (*nfpr < NUM_FPR_ARG_REGISTERS) |
| 454 |
*fpr_base++ = *(double *)item_base; |
| 455 |
(*nfpr) += 1; |
| 456 |
item_base += 8; |
| 457 |
/* FALL THROUGH */ |
| 458 |
case FFI_TYPE_DOUBLE: |
| 459 |
if (*nfpr < NUM_FPR_ARG_REGISTERS) |
| 460 |
*fpr_base++ = *(double *)item_base; |
| 461 |
(*nfpr) += 1; |
| 462 |
break; |
| 463 |
case FFI_TYPE_FLOAT: |
| 464 |
if (*nfpr < NUM_FPR_ARG_REGISTERS) |
| 465 |
*fpr_base++ = (double) *(float *)item_base; |
| 466 |
(*nfpr) += 1; |
| 467 |
break; |
| 468 |
default: |
| 469 |
break; |
| 470 |
} |
| 471 |
/* now count the size of what we just used. */ |
| 472 |
struct_offset += p->size; |
| 473 |
} |
| 474 |
/* Update the scores. */ |
| 475 |
*fprs = fpr_base; |
| 476 |
} |
| 477 |
|
| 478 |
/* Darwin64 special rules. |
| 479 |
Break out a struct into params and float registers. */ |
| 480 |
static void |
| 481 |
darwin64_pass_struct_by_value (ffi_type *s, char *src, unsigned size, |
| 482 |
unsigned *nfpr, double **fprs, unsigned long **arg) |
| 483 |
{ |
| 484 |
unsigned long *next_arg = *arg; |
| 485 |
char *dest_cpy = (char *)next_arg; |
| 486 |
|
| 487 |
FFI_ASSERT (s->type == FFI_TYPE_STRUCT) |
| 488 |
|
| 489 |
if (!size) |
| 490 |
return; |
| 491 |
|
| 492 |
/* First... special cases. */ |
| 493 |
if (size < 3 |
| 494 |
|| (size == 4 |
| 495 |
&& s->elements[0] |
| 496 |
&& s->elements[0]->type != FFI_TYPE_FLOAT)) |
| 497 |
{ |
| 498 |
/* Must be at least one GPR, padding is unspecified in value, |
| 499 |
let's make it zero. */ |
| 500 |
*next_arg = 0UL; |
| 501 |
dest_cpy += 8 - size; |
| 502 |
memcpy ((char *) dest_cpy, src, size); |
| 503 |
next_arg++; |
| 504 |
} |
| 505 |
else if (size == 16) |
| 506 |
{ |
| 507 |
memcpy ((char *) dest_cpy, src, size); |
| 508 |
next_arg += 2; |
| 509 |
} |
| 510 |
else |
| 511 |
{ |
| 512 |
/* now the general case, we consider embedded floats. */ |
| 513 |
memcpy ((char *) dest_cpy, src, size); |
| 514 |
darwin64_pass_struct_floats (s, src, nfpr, fprs); |
| 515 |
next_arg += (size+7)/8; |
| 516 |
} |
| 517 |
|
| 518 |
*arg = next_arg; |
| 519 |
} |
| 520 |
|
| 521 |
double * |
| 522 |
darwin64_struct_floats_to_mem (ffi_type *s, char *dest, double *fprs, unsigned *nf) |
| 523 |
{ |
| 524 |
int i; |
| 525 |
unsigned struct_offset = 0; |
| 526 |
|
| 527 |
/* We don't assume anything about the alignment of the source. */ |
| 528 |
for (i = 0; s->elements[i] != NULL; i++) |
| 529 |
{ |
| 530 |
char *item_base; |
| 531 |
ffi_type *p = s->elements[i]; |
| 532 |
/* Find the start of this item (0 for the first one). */ |
| 533 |
if (i > 0) |
| 534 |
struct_offset = FFI_ALIGN(struct_offset, p->alignment); |
| 535 |
item_base = dest + struct_offset; |
| 536 |
|
| 537 |
switch (p->type) |
| 538 |
{ |
| 539 |
case FFI_TYPE_STRUCT: |
| 540 |
fprs = darwin64_struct_floats_to_mem (p, item_base, fprs, nf); |
| 541 |
break; |
| 542 |
case FFI_TYPE_LONGDOUBLE: |
| 543 |
if (*nf < NUM_FPR_ARG_REGISTERS) |
| 544 |
{ |
| 545 |
*(double *)item_base = *fprs++ ; |
| 546 |
(*nf) += 1; |
| 547 |
} |
| 548 |
item_base += 8; |
| 549 |
/* FALL THROUGH */ |
| 550 |
case FFI_TYPE_DOUBLE: |
| 551 |
if (*nf < NUM_FPR_ARG_REGISTERS) |
| 552 |
{ |
| 553 |
*(double *)item_base = *fprs++ ; |
| 554 |
(*nf) += 1; |
| 555 |
} |
| 556 |
break; |
| 557 |
case FFI_TYPE_FLOAT: |
| 558 |
if (*nf < NUM_FPR_ARG_REGISTERS) |
| 559 |
{ |
| 560 |
*(float *)item_base = (float) *fprs++ ; |
| 561 |
(*nf) += 1; |
| 562 |
} |
| 563 |
break; |
| 564 |
default: |
| 565 |
break; |
| 566 |
} |
| 567 |
/* now count the size of what we just used. */ |
| 568 |
struct_offset += p->size; |
| 569 |
} |
| 570 |
return fprs; |
| 571 |
} |
| 572 |
|
| 573 |
#endif |
| 574 |
|
| 575 |
/* Adjust the size of S to be correct for Darwin. |
| 576 |
On Darwin m32, the first field of a structure has natural alignment. |
| 577 |
On Darwin m64, all fields have natural alignment. */ |
| 578 |
|
| 579 |
static void |
| 580 |
darwin_adjust_aggregate_sizes (ffi_type *s) |
| 581 |
{ |
| 582 |
int i; |
| 583 |
|
| 584 |
if (s->type != FFI_TYPE_STRUCT) |
| 585 |
return; |
| 586 |
|
| 587 |
s->size = 0; |
| 588 |
for (i = 0; s->elements[i] != NULL; i++) |
| 589 |
{ |
| 590 |
ffi_type *p; |
| 591 |
int align; |
| 592 |
|
| 593 |
p = s->elements[i]; |
| 594 |
if (p->type == FFI_TYPE_STRUCT) |
| 595 |
darwin_adjust_aggregate_sizes (p); |
| 596 |
#if defined(POWERPC_DARWIN64) |
| 597 |
/* Natural alignment for all items. */ |
| 598 |
align = p->alignment; |
| 599 |
#else |
| 600 |
/* Natural alignment for the first item... */ |
| 601 |
if (i == 0) |
| 602 |
align = p->alignment; |
| 603 |
else if (p->alignment == 16 || p->alignment < 4) |
| 604 |
/* .. subsequent items with vector or align < 4 have natural align. */ |
| 605 |
align = p->alignment; |
| 606 |
else |
| 607 |
/* .. or align is 4. */ |
| 608 |
align = 4; |
| 609 |
#endif |
| 610 |
/* Pad, if necessary, before adding the current item. */ |
| 611 |
s->size = FFI_ALIGN(s->size, align) + p->size; |
| 612 |
} |
| 613 |
|
| 614 |
s->size = FFI_ALIGN(s->size, s->alignment); |
| 615 |
|
| 616 |
/* This should not be necessary on m64, but harmless. */ |
| 617 |
if (s->elements[0]->type == FFI_TYPE_UINT64 |
| 618 |
|| s->elements[0]->type == FFI_TYPE_SINT64 |
| 619 |
|| s->elements[0]->type == FFI_TYPE_DOUBLE |
| 620 |
|| s->elements[0]->alignment == 8) |
| 621 |
s->alignment = s->alignment > 8 ? s->alignment : 8; |
| 622 |
/* Do not add additional tail padding. */ |
| 623 |
} |
| 624 |
|
| 625 |
/* Adjust the size of S to be correct for AIX. |
| 626 |
Word-align double unless it is the first member of a structure recursively. |
| 627 |
Return non-zero if we found a recursive first member aggregate of interest. */ |
| 628 |
|
| 629 |
static int |
| 630 |
aix_adjust_aggregate_sizes (ffi_type *s, int outer_most_type_or_first_member) |
| 631 |
{ |
| 632 |
int i, nested_first_member=0, final_align, rc=0; |
| 633 |
|
| 634 |
if (s->type != FFI_TYPE_STRUCT) |
| 635 |
return 0; |
| 636 |
|
| 637 |
s->size = 0; |
| 638 |
for (i = 0; s->elements[i] != NULL; i++) |
| 639 |
{ |
| 640 |
ffi_type p; |
| 641 |
int align; |
| 642 |
|
| 643 |
/* nested aggregates layout differently on AIX, so take a copy of the type */ |
| 644 |
p = *(s->elements[i]); |
| 645 |
if (i == 0) |
| 646 |
nested_first_member = aix_adjust_aggregate_sizes(&p, outer_most_type_or_first_member); |
| 647 |
else |
| 648 |
aix_adjust_aggregate_sizes(&p, 0); |
| 649 |
align = p.alignment; |
| 650 |
if (i != 0 && p.type == FFI_TYPE_DOUBLE) |
| 651 |
align = 4; |
| 652 |
s->size = FFI_ALIGN(s->size, align) + p.size; |
| 653 |
} |
| 654 |
|
| 655 |
final_align=s->alignment; |
| 656 |
if ((s->elements[0]->type == FFI_TYPE_UINT64 |
| 657 |
|| s->elements[0]->type == FFI_TYPE_SINT64 |
| 658 |
|| s->elements[0]->type == FFI_TYPE_DOUBLE |
| 659 |
|| s->elements[0]->alignment == 8 || nested_first_member)) { |
| 660 |
final_align = s->alignment > 8 ? s->alignment : 8; |
| 661 |
rc=1; |
| 662 |
/* still use the adjusted alignment to calculate tail padding, but don't adjust the types alignment if |
| 663 |
we aren't in the recursive first position */ |
| 664 |
if (outer_most_type_or_first_member) |
| 665 |
s->alignment=final_align; |
| 666 |
} |
| 667 |
|
| 668 |
s->size = FFI_ALIGN(s->size, final_align); |
| 669 |
return rc; |
| 670 |
} |
| 671 |
|
| 672 |
/* Perform machine dependent cif processing. */ |
| 673 |
ffi_status |
| 674 |
ffi_prep_cif_machdep (ffi_cif *cif) |
| 675 |
{ |
| 676 |
/* All this is for the DARWIN ABI. */ |
| 677 |
unsigned i; |
| 678 |
ffi_type **ptr; |
| 679 |
unsigned bytes; |
| 680 |
unsigned fparg_count = 0, intarg_count = 0; |
| 681 |
unsigned flags = 0; |
| 682 |
unsigned size_al = 0; |
| 683 |
|
| 684 |
/* All the machine-independent calculation of cif->bytes will be wrong. |
| 685 |
All the calculation of structure sizes will also be wrong. |
| 686 |
Redo the calculation for DARWIN. */ |
| 687 |
|
| 688 |
if (cif->abi == FFI_DARWIN) |
| 689 |
{ |
| 690 |
darwin_adjust_aggregate_sizes (cif->rtype); |
| 691 |
for (i = 0; i < cif->nargs; i++) |
| 692 |
darwin_adjust_aggregate_sizes (cif->arg_types[i]); |
| 693 |
} |
| 694 |
|
| 695 |
if (cif->abi == FFI_AIX) |
| 696 |
{ |
| 697 |
aix_adjust_aggregate_sizes (cif->rtype, 1); |
| 698 |
for (i = 0; i < cif->nargs; i++) |
| 699 |
aix_adjust_aggregate_sizes (cif->arg_types[i], 1); |
| 700 |
} |
| 701 |
|
| 702 |
/* Space for the frame pointer, callee's LR, CR, etc, and for |
| 703 |
the asm's temp regs. */ |
| 704 |
|
| 705 |
bytes = (LINKAGE_AREA_GPRS + ASM_NEEDS_REGISTERS) * sizeof(unsigned long); |
| 706 |
|
| 707 |
/* Return value handling. |
| 708 |
The rules m32 are as follows: |
| 709 |
- 32-bit (or less) integer values are returned in gpr3; |
| 710 |
- structures of size <= 4 bytes also returned in gpr3; |
| 711 |
- 64-bit integer values [??? and structures between 5 and 8 bytes] are |
| 712 |
returned in gpr3 and gpr4; |
| 713 |
- Single/double FP values are returned in fpr1; |
| 714 |
- Long double FP (if not equivalent to double) values are returned in |
| 715 |
fpr1 and fpr2; |
| 716 |
m64: |
| 717 |
- 64-bit or smaller integral values are returned in GPR3 |
| 718 |
- Single/double FP values are returned in fpr1; |
| 719 |
- Long double FP values are returned in fpr1 and fpr2; |
| 720 |
m64 Structures: |
| 721 |
- If the structure could be accommodated in registers were it to be the |
| 722 |
first argument to a routine, then it is returned in those registers. |
| 723 |
m32/m64 structures otherwise: |
| 724 |
- Larger structures values are allocated space and a pointer is passed |
| 725 |
as the first argument. */ |
| 726 |
switch (cif->rtype->type) |
| 727 |
{ |
| 728 |
|
| 729 |
#if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE |
| 730 |
case FFI_TYPE_LONGDOUBLE: |
| 731 |
flags |= FLAG_RETURNS_128BITS; |
| 732 |
flags |= FLAG_RETURNS_FP; |
| 733 |
break; |
| 734 |
#endif |
| 735 |
|
| 736 |
case FFI_TYPE_DOUBLE: |
| 737 |
flags |= FLAG_RETURNS_64BITS; |
| 738 |
/* Fall through. */ |
| 739 |
case FFI_TYPE_FLOAT: |
| 740 |
flags |= FLAG_RETURNS_FP; |
| 741 |
break; |
| 742 |
|
| 743 |
case FFI_TYPE_UINT64: |
| 744 |
case FFI_TYPE_SINT64: |
| 745 |
#ifdef POWERPC64 |
| 746 |
case FFI_TYPE_POINTER: |
| 747 |
#endif |
| 748 |
flags |= FLAG_RETURNS_64BITS; |
| 749 |
break; |
| 750 |
|
| 751 |
case FFI_TYPE_STRUCT: |
| 752 |
#if defined(POWERPC_DARWIN64) |
| 753 |
{ |
| 754 |
/* Can we fit the struct into regs? */ |
| 755 |
if (darwin64_struct_ret_by_value_p (cif->rtype)) |
| 756 |
{ |
| 757 |
unsigned nfpr = 0; |
| 758 |
flags |= FLAG_RETURNS_STRUCT; |
| 759 |
if (cif->rtype->size != 16) |
| 760 |
darwin64_scan_struct_for_floats (cif->rtype, &nfpr) ; |
| 761 |
else |
| 762 |
flags |= FLAG_RETURNS_128BITS; |
| 763 |
/* Will be 0 for 16byte struct. */ |
| 764 |
if (nfpr) |
| 765 |
flags |= FLAG_RETURNS_FP; |
| 766 |
} |
| 767 |
else /* By ref. */ |
| 768 |
{ |
| 769 |
flags |= FLAG_RETVAL_REFERENCE; |
| 770 |
flags |= FLAG_RETURNS_NOTHING; |
| 771 |
intarg_count++; |
| 772 |
} |
| 773 |
} |
| 774 |
#elif defined(DARWIN_PPC) |
| 775 |
if (cif->rtype->size <= 4) |
| 776 |
flags |= FLAG_RETURNS_STRUCT; |
| 777 |
else /* else by reference. */ |
| 778 |
{ |
| 779 |
flags |= FLAG_RETVAL_REFERENCE; |
| 780 |
flags |= FLAG_RETURNS_NOTHING; |
| 781 |
intarg_count++; |
| 782 |
} |
| 783 |
#else /* assume we pass by ref. */ |
| 784 |
flags |= FLAG_RETVAL_REFERENCE; |
| 785 |
flags |= FLAG_RETURNS_NOTHING; |
| 786 |
intarg_count++; |
| 787 |
#endif |
| 788 |
break; |
| 789 |
case FFI_TYPE_VOID: |
| 790 |
flags |= FLAG_RETURNS_NOTHING; |
| 791 |
break; |
| 792 |
|
| 793 |
default: |
| 794 |
/* Returns 32-bit integer, or similar. Nothing to do here. */ |
| 795 |
break; |
| 796 |
} |
| 797 |
|
| 798 |
/* The first NUM_GPR_ARG_REGISTERS words of integer arguments, and the |
| 799 |
first NUM_FPR_ARG_REGISTERS fp arguments, go in registers; the rest |
| 800 |
goes on the stack. |
| 801 |
??? Structures are passed as a pointer to a copy of the structure. |
| 802 |
Stuff on the stack needs to keep proper alignment. |
| 803 |
For m64 the count is effectively of half-GPRs. */ |
| 804 |
for (ptr = cif->arg_types, i = cif->nargs; i > 0; i--, ptr++) |
| 805 |
{ |
| 806 |
unsigned align_words; |
| 807 |
switch ((*ptr)->type) |
| 808 |
{ |
| 809 |
case FFI_TYPE_FLOAT: |
| 810 |
case FFI_TYPE_DOUBLE: |
| 811 |
fparg_count++; |
| 812 |
#if !defined(POWERPC_DARWIN64) |
| 813 |
/* If this FP arg is going on the stack, it must be |
| 814 |
8-byte-aligned. */ |
| 815 |
if (fparg_count > NUM_FPR_ARG_REGISTERS |
| 816 |
&& (intarg_count & 0x01) != 0) |
| 817 |
intarg_count++; |
| 818 |
#endif |
| 819 |
break; |
| 820 |
|
| 821 |
#if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE |
| 822 |
case FFI_TYPE_LONGDOUBLE: |
| 823 |
fparg_count += 2; |
| 824 |
/* If this FP arg is going on the stack, it must be |
| 825 |
16-byte-aligned. */ |
| 826 |
if (fparg_count >= NUM_FPR_ARG_REGISTERS) |
| 827 |
#if defined (POWERPC64) |
| 828 |
intarg_count = FFI_ALIGN(intarg_count, 2); |
| 829 |
#else |
| 830 |
intarg_count = FFI_ALIGN(intarg_count, 4); |
| 831 |
#endif |
| 832 |
break; |
| 833 |
#endif |
| 834 |
|
| 835 |
case FFI_TYPE_UINT64: |
| 836 |
case FFI_TYPE_SINT64: |
| 837 |
#if defined(POWERPC64) |
| 838 |
intarg_count++; |
| 839 |
#else |
| 840 |
/* 'long long' arguments are passed as two words, but |
| 841 |
either both words must fit in registers or both go |
| 842 |
on the stack. If they go on the stack, they must |
| 843 |
be 8-byte-aligned. */ |
| 844 |
if (intarg_count == NUM_GPR_ARG_REGISTERS-1 |
| 845 |
|| (intarg_count >= NUM_GPR_ARG_REGISTERS |
| 846 |
&& (intarg_count & 0x01) != 0)) |
| 847 |
intarg_count++; |
| 848 |
intarg_count += 2; |
| 849 |
#endif |
| 850 |
break; |
| 851 |
|
| 852 |
case FFI_TYPE_STRUCT: |
| 853 |
size_al = (*ptr)->size; |
| 854 |
#if defined(POWERPC_DARWIN64) |
| 855 |
align_words = (*ptr)->alignment >> 3; |
| 856 |
if (align_words) |
| 857 |
intarg_count = FFI_ALIGN(intarg_count, align_words); |
| 858 |
/* Base size of the struct. */ |
| 859 |
intarg_count += (size_al + 7) / 8; |
| 860 |
/* If 16 bytes then don't worry about floats. */ |
| 861 |
if (size_al != 16) |
| 862 |
/* Scan through for floats to be placed in regs. */ |
| 863 |
darwin64_scan_struct_for_floats (*ptr, &fparg_count) ; |
| 864 |
#else |
| 865 |
align_words = (*ptr)->alignment >> 2; |
| 866 |
if (align_words) |
| 867 |
intarg_count = FFI_ALIGN(intarg_count, align_words); |
| 868 |
/* If the first member of the struct is a double, then align |
| 869 |
the struct to double-word. |
| 870 |
if ((*ptr)->elements[0]->type == FFI_TYPE_DOUBLE) |
| 871 |
size_al = FFI_ALIGN((*ptr)->size, 8); */ |
| 872 |
# ifdef POWERPC64 |
| 873 |
intarg_count += (size_al + 7) / 8; |
| 874 |
# else |
| 875 |
intarg_count += (size_al + 3) / 4; |
| 876 |
# endif |
| 877 |
#endif |
| 878 |
break; |
| 879 |
|
| 880 |
default: |
| 881 |
/* Everything else is passed as a 4-byte word in a GPR, either |
| 882 |
the object itself or a pointer to it. */ |
| 883 |
intarg_count++; |
| 884 |
break; |
| 885 |
} |
| 886 |
} |
| 887 |
|
| 888 |
if (fparg_count != 0) |
| 889 |
flags |= FLAG_FP_ARGUMENTS; |
| 890 |
|
| 891 |
#if defined(POWERPC_DARWIN64) |
| 892 |
/* Space to image the FPR registers, if needed - which includes when they might be |
| 893 |
used in a struct return. */ |
| 894 |
if (fparg_count != 0 |
| 895 |
|| ((flags & FLAG_RETURNS_STRUCT) |
| 896 |
&& (flags & FLAG_RETURNS_FP))) |
| 897 |
bytes += NUM_FPR_ARG_REGISTERS * sizeof(double); |
| 898 |
#else |
| 899 |
/* Space for the FPR registers, if needed. */ |
| 900 |
if (fparg_count != 0) |
| 901 |
bytes += NUM_FPR_ARG_REGISTERS * sizeof(double); |
| 902 |
#endif |
| 903 |
|
| 904 |
/* Stack space. */ |
| 905 |
#ifdef POWERPC64 |
| 906 |
if ((intarg_count + fparg_count) > NUM_GPR_ARG_REGISTERS) |
| 907 |
bytes += (intarg_count + fparg_count) * sizeof(long); |
| 908 |
#else |
| 909 |
if ((intarg_count + 2 * fparg_count) > NUM_GPR_ARG_REGISTERS) |
| 910 |
bytes += (intarg_count + 2 * fparg_count) * sizeof(long); |
| 911 |
#endif |
| 912 |
else |
| 913 |
bytes += NUM_GPR_ARG_REGISTERS * sizeof(long); |
| 914 |
|
| 915 |
/* The stack space allocated needs to be a multiple of 16 bytes. */ |
| 916 |
bytes = FFI_ALIGN(bytes, 16) ; |
| 917 |
|
| 918 |
cif->flags = flags; |
| 919 |
cif->bytes = bytes; |
| 920 |
|
| 921 |
return FFI_OK; |
| 922 |
} |
| 923 |
|
| 924 |
extern void ffi_call_AIX(extended_cif *, long, unsigned, unsigned *, |
| 925 |
void (*fn)(void), void (*fn2)(void)); |
| 926 |
|
| 927 |
#if defined (FFI_GO_CLOSURES) |
| 928 |
extern void ffi_call_go_AIX(extended_cif *, long, unsigned, unsigned *, |
| 929 |
void (*fn)(void), void (*fn2)(void), void *closure); |
| 930 |
#endif |
| 931 |
|
| 932 |
extern void ffi_call_DARWIN(extended_cif *, long, unsigned, unsigned *, |
| 933 |
void (*fn)(void), void (*fn2)(void), ffi_type*); |
| 934 |
|
| 935 |
void |
| 936 |
ffi_call (ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue) |
| 937 |
{ |
| 938 |
extended_cif ecif; |
| 939 |
|
| 940 |
ecif.cif = cif; |
| 941 |
ecif.avalue = avalue; |
| 942 |
|
| 943 |
/* If the return value is a struct and we don't have a return |
| 944 |
value address then we need to make one. */ |
| 945 |
|
| 946 |
if ((rvalue == NULL) && |
| 947 |
(cif->rtype->type == FFI_TYPE_STRUCT)) |
| 948 |
{ |
| 949 |
ecif.rvalue = alloca (cif->rtype->size); |
| 950 |
} |
| 951 |
else |
| 952 |
ecif.rvalue = rvalue; |
| 953 |
|
| 954 |
switch (cif->abi) |
| 955 |
{ |
| 956 |
case FFI_AIX: |
| 957 |
ffi_call_AIX(&ecif, -(long)cif->bytes, cif->flags, ecif.rvalue, fn, |
| 958 |
FFI_FN(ffi_prep_args)); |
| 959 |
break; |
| 960 |
case FFI_DARWIN: |
| 961 |
ffi_call_DARWIN(&ecif, -(long)cif->bytes, cif->flags, ecif.rvalue, fn, |
| 962 |
FFI_FN(ffi_prep_args), cif->rtype); |
| 963 |
break; |
| 964 |
default: |
| 965 |
FFI_ASSERT(0); |
| 966 |
break; |
| 967 |
} |
| 968 |
} |
| 969 |
|
| 970 |
#if defined (FFI_GO_CLOSURES) |
| 971 |
void |
| 972 |
ffi_call_go (ffi_cif *cif, void (*fn) (void), void *rvalue, void **avalue, |
| 973 |
void *closure) |
| 974 |
{ |
| 975 |
extended_cif ecif; |
| 976 |
|
| 977 |
ecif.cif = cif; |
| 978 |
ecif.avalue = avalue; |
| 979 |
|
| 980 |
/* If the return value is a struct and we don't have a return |
| 981 |
value address then we need to make one. */ |
| 982 |
|
| 983 |
if ((rvalue == NULL) && |
| 984 |
(cif->rtype->type == FFI_TYPE_STRUCT)) |
| 985 |
{ |
| 986 |
ecif.rvalue = alloca (cif->rtype->size); |
| 987 |
} |
| 988 |
else |
| 989 |
ecif.rvalue = rvalue; |
| 990 |
|
| 991 |
switch (cif->abi) |
| 992 |
{ |
| 993 |
case FFI_AIX: |
| 994 |
ffi_call_go_AIX(&ecif, -(long)cif->bytes, cif->flags, ecif.rvalue, fn, |
| 995 |
FFI_FN(ffi_prep_args), closure); |
| 996 |
break; |
| 997 |
default: |
| 998 |
FFI_ASSERT(0); |
| 999 |
break; |
| 1000 |
} |
| 1001 |
} |
| 1002 |
#endif |
| 1003 |
|
| 1004 |
static void flush_icache(char *); |
| 1005 |
static void flush_range(char *, int); |
| 1006 |
|
| 1007 |
/* The layout of a function descriptor. A C function pointer really |
| 1008 |
points to one of these. */ |
| 1009 |
|
| 1010 |
typedef struct aix_fd_struct { |
| 1011 |
void *code_pointer; |
| 1012 |
void *toc; |
| 1013 |
} aix_fd; |
| 1014 |
|
| 1015 |
/* here I'd like to add the stack frame layout we use in darwin_closure.S |
| 1016 |
and aix_closure.S |
| 1017 |
|
| 1018 |
m32/m64 |
| 1019 |
|
| 1020 |
The stack layout looks like this: |
| 1021 |
|
| 1022 |
| Additional params... | | Higher address |
| 1023 |
~ ~ ~ |
| 1024 |
| Parameters (at least 8*4/8=32/64) | | NUM_GPR_ARG_REGISTERS |
| 1025 |
|--------------------------------------------| | |
| 1026 |
| TOC=R2 (AIX) Reserved (Darwin) 4/8 | | |
| 1027 |
|--------------------------------------------| | |
| 1028 |
| Reserved 2*4/8 | | |
| 1029 |
|--------------------------------------------| | |
| 1030 |
| Space for callee's LR 4/8 | | |
| 1031 |
|--------------------------------------------| | |
| 1032 |
| Saved CR [low word for m64] 4/8 | | |
| 1033 |
|--------------------------------------------| | |
| 1034 |
| Current backchain pointer 4/8 |-/ Parent's frame. |
| 1035 |
|--------------------------------------------| <+ <<< on entry to ffi_closure_ASM |
| 1036 |
| Result Bytes 16 | | |
| 1037 |
|--------------------------------------------| | |
| 1038 |
~ padding to 16-byte alignment ~ ~ |
| 1039 |
|--------------------------------------------| | |
| 1040 |
| NUM_FPR_ARG_REGISTERS slots | | |
| 1041 |
| here fp13 .. fp1 13*8 | | |
| 1042 |
|--------------------------------------------| | |
| 1043 |
| R3..R10 8*4/8=32/64 | | NUM_GPR_ARG_REGISTERS |
| 1044 |
|--------------------------------------------| | |
| 1045 |
| TOC=R2 (AIX) Reserved (Darwin) 4/8 | | |
| 1046 |
|--------------------------------------------| | stack | |
| 1047 |
| Reserved [compiler,binder] 2*4/8 | | grows | |
| 1048 |
|--------------------------------------------| | down V |
| 1049 |
| Space for callee's LR 4/8 | | |
| 1050 |
|--------------------------------------------| | lower addresses |
| 1051 |
| Saved CR [low word for m64] 4/8 | | |
| 1052 |
|--------------------------------------------| | stack pointer here |
| 1053 |
| Current backchain pointer 4/8 |-/ during |
| 1054 |
|--------------------------------------------| <<< ffi_closure_ASM. |
| 1055 |
|
| 1056 |
*/ |
| 1057 |
|
| 1058 |
ffi_status |
| 1059 |
ffi_prep_closure_loc (ffi_closure* closure, |
| 1060 |
ffi_cif* cif, |
| 1061 |
void (*fun)(ffi_cif*, void*, void**, void*), |
| 1062 |
void *user_data, |
| 1063 |
void *codeloc) |
| 1064 |
{ |
| 1065 |
unsigned int *tramp; |
| 1066 |
struct ffi_aix_trampoline_struct *tramp_aix; |
| 1067 |
aix_fd *fd; |
| 1068 |
|
| 1069 |
switch (cif->abi) |
| 1070 |
{ |
| 1071 |
case FFI_DARWIN: |
| 1072 |
|
| 1073 |
FFI_ASSERT (cif->abi == FFI_DARWIN); |
| 1074 |
|
| 1075 |
tramp = (unsigned int *) &closure->tramp[0]; |
| 1076 |
#if defined(POWERPC_DARWIN64) |
| 1077 |
tramp[0] = 0x7c0802a6; /* mflr r0 */ |
| 1078 |
tramp[1] = 0x429f0015; /* bcl- 20,4*cr7+so, +0x18 (L1) */ |
| 1079 |
/* We put the addresses here. */ |
| 1080 |
tramp[6] = 0x7d6802a6; /*L1: mflr r11 */ |
| 1081 |
tramp[7] = 0xe98b0000; /* ld r12,0(r11) function address */ |
| 1082 |
tramp[8] = 0x7c0803a6; /* mtlr r0 */ |
| 1083 |
tramp[9] = 0x7d8903a6; /* mtctr r12 */ |
| 1084 |
tramp[10] = 0xe96b0008; /* lwz r11,8(r11) static chain */ |
| 1085 |
tramp[11] = 0x4e800420; /* bctr */ |
| 1086 |
|
| 1087 |
*((unsigned long *)&tramp[2]) = (unsigned long) ffi_closure_ASM; /* function */ |
| 1088 |
*((unsigned long *)&tramp[4]) = (unsigned long) codeloc; /* context */ |
| 1089 |
#else |
| 1090 |
tramp[0] = 0x7c0802a6; /* mflr r0 */ |
| 1091 |
tramp[1] = 0x429f000d; /* bcl- 20,4*cr7+so,0x10 */ |
| 1092 |
tramp[4] = 0x7d6802a6; /* mflr r11 */ |
| 1093 |
tramp[5] = 0x818b0000; /* lwz r12,0(r11) function address */ |
| 1094 |
tramp[6] = 0x7c0803a6; /* mtlr r0 */ |
| 1095 |
tramp[7] = 0x7d8903a6; /* mtctr r12 */ |
| 1096 |
tramp[8] = 0x816b0004; /* lwz r11,4(r11) static chain */ |
| 1097 |
tramp[9] = 0x4e800420; /* bctr */ |
| 1098 |
tramp[2] = (unsigned long) ffi_closure_ASM; /* function */ |
| 1099 |
tramp[3] = (unsigned long) codeloc; /* context */ |
| 1100 |
#endif |
| 1101 |
closure->cif = cif; |
| 1102 |
closure->fun = fun; |
| 1103 |
closure->user_data = user_data; |
| 1104 |
|
| 1105 |
/* Flush the icache. Only necessary on Darwin. */ |
| 1106 |
flush_range(codeloc, FFI_TRAMPOLINE_SIZE); |
| 1107 |
|
| 1108 |
break; |
| 1109 |
|
| 1110 |
case FFI_AIX: |
| 1111 |
|
| 1112 |
tramp_aix = (struct ffi_aix_trampoline_struct *) (closure->tramp); |
| 1113 |
fd = (aix_fd *)(void *)ffi_closure_ASM; |
| 1114 |
|
| 1115 |
FFI_ASSERT (cif->abi == FFI_AIX); |
| 1116 |
|
| 1117 |
tramp_aix->code_pointer = fd->code_pointer; |
| 1118 |
tramp_aix->toc = fd->toc; |
| 1119 |
tramp_aix->static_chain = codeloc; |
| 1120 |
closure->cif = cif; |
| 1121 |
closure->fun = fun; |
| 1122 |
closure->user_data = user_data; |
| 1123 |
break; |
| 1124 |
|
| 1125 |
default: |
| 1126 |
return FFI_BAD_ABI; |
| 1127 |
break; |
| 1128 |
} |
| 1129 |
return FFI_OK; |
| 1130 |
} |
| 1131 |
|
| 1132 |
#if defined (FFI_GO_CLOSURES) |
| 1133 |
ffi_status |
| 1134 |
ffi_prep_go_closure (ffi_go_closure* closure, |
| 1135 |
ffi_cif* cif, |
| 1136 |
void (*fun)(ffi_cif*, void*, void**, void*)) |
| 1137 |
{ |
| 1138 |
switch (cif->abi) |
| 1139 |
{ |
| 1140 |
case FFI_AIX: |
| 1141 |
|
| 1142 |
FFI_ASSERT (cif->abi == FFI_AIX); |
| 1143 |
|
| 1144 |
closure->tramp = (void *)ffi_go_closure_ASM; |
| 1145 |
closure->cif = cif; |
| 1146 |
closure->fun = fun; |
| 1147 |
return FFI_OK; |
| 1148 |
|
| 1149 |
// For now, ffi_prep_go_closure is only implemented for AIX, not for Darwin |
| 1150 |
default: |
| 1151 |
return FFI_BAD_ABI; |
| 1152 |
break; |
| 1153 |
} |
| 1154 |
return FFI_OK; |
| 1155 |
} |
| 1156 |
#endif |
| 1157 |
|
| 1158 |
static void |
| 1159 |
flush_icache(char *addr) |
| 1160 |
{ |
| 1161 |
#ifndef _AIX |
| 1162 |
__asm__ volatile ( |
| 1163 |
"dcbf 0,%0\n" |
| 1164 |
"\tsync\n" |
| 1165 |
"\ticbi 0,%0\n" |
| 1166 |
"\tsync\n" |
| 1167 |
"\tisync" |
| 1168 |
: : "r"(addr) : "memory"); |
| 1169 |
#endif |
| 1170 |
} |
| 1171 |
|
| 1172 |
static void |
| 1173 |
flush_range(char * addr1, int size) |
| 1174 |
{ |
| 1175 |
#define MIN_LINE_SIZE 32 |
| 1176 |
int i; |
| 1177 |
for (i = 0; i < size; i += MIN_LINE_SIZE) |
| 1178 |
flush_icache(addr1+i); |
| 1179 |
flush_icache(addr1+size-1); |
| 1180 |
} |
| 1181 |
|
| 1182 |
typedef union |
| 1183 |
{ |
| 1184 |
float f; |
| 1185 |
double d; |
| 1186 |
} ffi_dblfl; |
| 1187 |
|
| 1188 |
ffi_type * |
| 1189 |
ffi_closure_helper_DARWIN (ffi_closure *, void *, |
| 1190 |
unsigned long *, ffi_dblfl *); |
| 1191 |
|
| 1192 |
#if defined (FFI_GO_CLOSURES) |
| 1193 |
ffi_type * |
| 1194 |
ffi_go_closure_helper_DARWIN (ffi_go_closure*, void *, |
| 1195 |
unsigned long *, ffi_dblfl *); |
| 1196 |
#endif |
| 1197 |
|
| 1198 |
/* Basically the trampoline invokes ffi_closure_ASM, and on |
| 1199 |
entry, r11 holds the address of the closure. |
| 1200 |
After storing the registers that could possibly contain |
| 1201 |
parameters to be passed into the stack frame and setting |
| 1202 |
up space for a return value, ffi_closure_ASM invokes the |
| 1203 |
following helper function to do most of the work. */ |
| 1204 |
|
| 1205 |
static ffi_type * |
| 1206 |
ffi_closure_helper_common (ffi_cif* cif, |
| 1207 |
void (*fun)(ffi_cif*, void*, void**, void*), |
| 1208 |
void *user_data, void *rvalue, |
| 1209 |
unsigned long *pgr, ffi_dblfl *pfr) |
| 1210 |
{ |
| 1211 |
/* rvalue is the pointer to space for return value in closure assembly |
| 1212 |
pgr is the pointer to where r3-r10 are stored in ffi_closure_ASM |
| 1213 |
pfr is the pointer to where f1-f13 are stored in ffi_closure_ASM. */ |
| 1214 |
|
| 1215 |
typedef double ldbits[2]; |
| 1216 |
|
| 1217 |
union ldu |
| 1218 |
{ |
| 1219 |
ldbits lb; |
| 1220 |
long double ld; |
| 1221 |
}; |
| 1222 |
|
| 1223 |
void ** avalue; |
| 1224 |
ffi_type ** arg_types; |
| 1225 |
long i, avn; |
| 1226 |
ffi_dblfl * end_pfr = pfr + NUM_FPR_ARG_REGISTERS; |
| 1227 |
unsigned size_al; |
| 1228 |
#if defined(POWERPC_DARWIN64) |
| 1229 |
unsigned fpsused = 0; |
| 1230 |
#endif |
| 1231 |
|
| 1232 |
avalue = alloca (cif->nargs * sizeof(void *)); |
| 1233 |
|
| 1234 |
if (cif->rtype->type == FFI_TYPE_STRUCT) |
| 1235 |
{ |
| 1236 |
#if defined(POWERPC_DARWIN64) |
| 1237 |
if (!darwin64_struct_ret_by_value_p (cif->rtype)) |
| 1238 |
{ |
| 1239 |
/* Won't fit into the regs - return by ref. */ |
| 1240 |
rvalue = (void *) *pgr; |
| 1241 |
pgr++; |
| 1242 |
} |
| 1243 |
#elif defined(DARWIN_PPC) |
| 1244 |
if (cif->rtype->size > 4) |
| 1245 |
{ |
| 1246 |
rvalue = (void *) *pgr; |
| 1247 |
pgr++; |
| 1248 |
} |
| 1249 |
#else /* assume we return by ref. */ |
| 1250 |
rvalue = (void *) *pgr; |
| 1251 |
pgr++; |
| 1252 |
#endif |
| 1253 |
} |
| 1254 |
|
| 1255 |
i = 0; |
| 1256 |
avn = cif->nargs; |
| 1257 |
arg_types = cif->arg_types; |
| 1258 |
|
| 1259 |
/* Grab the addresses of the arguments from the stack frame. */ |
| 1260 |
while (i < avn) |
| 1261 |
{ |
| 1262 |
switch (arg_types[i]->type) |
| 1263 |
{ |
| 1264 |
case FFI_TYPE_SINT8: |
| 1265 |
case FFI_TYPE_UINT8: |
| 1266 |
#if defined(POWERPC64) |
| 1267 |
avalue[i] = (char *) pgr + 7; |
| 1268 |
#else |
| 1269 |
avalue[i] = (char *) pgr + 3; |
| 1270 |
#endif |
| 1271 |
pgr++; |
| 1272 |
break; |
| 1273 |
|
| 1274 |
case FFI_TYPE_SINT16: |
| 1275 |
case FFI_TYPE_UINT16: |
| 1276 |
#if defined(POWERPC64) |
| 1277 |
avalue[i] = (char *) pgr + 6; |
| 1278 |
#else |
| 1279 |
avalue[i] = (char *) pgr + 2; |
| 1280 |
#endif |
| 1281 |
pgr++; |
| 1282 |
break; |
| 1283 |
|
| 1284 |
case FFI_TYPE_SINT32: |
| 1285 |
case FFI_TYPE_UINT32: |
| 1286 |
#if defined(POWERPC64) |
| 1287 |
avalue[i] = (char *) pgr + 4; |
| 1288 |
#else |
| 1289 |
case FFI_TYPE_POINTER: |
| 1290 |
avalue[i] = pgr; |
| 1291 |
#endif |
| 1292 |
pgr++; |
| 1293 |
break; |
| 1294 |
|
| 1295 |
case FFI_TYPE_STRUCT: |
| 1296 |
size_al = arg_types[i]->size; |
| 1297 |
#if defined(POWERPC_DARWIN64) |
| 1298 |
pgr = (unsigned long *)FFI_ALIGN((char *)pgr, arg_types[i]->alignment); |
| 1299 |
if (size_al < 3 || size_al == 4) |
| 1300 |
{ |
| 1301 |
avalue[i] = ((char *)pgr)+8-size_al; |
| 1302 |
if (arg_types[i]->elements[0]->type == FFI_TYPE_FLOAT |
| 1303 |
&& fpsused < NUM_FPR_ARG_REGISTERS) |
| 1304 |
{ |
| 1305 |
*(float *)pgr = (float) *(double *)pfr; |
| 1306 |
pfr++; |
| 1307 |
fpsused++; |
| 1308 |
} |
| 1309 |
} |
| 1310 |
else |
| 1311 |
{ |
| 1312 |
if (size_al != 16) |
| 1313 |
pfr = (ffi_dblfl *) |
| 1314 |
darwin64_struct_floats_to_mem (arg_types[i], (char *)pgr, |
| 1315 |
(double *)pfr, &fpsused); |
| 1316 |
avalue[i] = pgr; |
| 1317 |
} |
| 1318 |
pgr += (size_al + 7) / 8; |
| 1319 |
#else |
| 1320 |
/* If the first member of the struct is a double, then align |
| 1321 |
the struct to double-word. */ |
| 1322 |
if (arg_types[i]->elements[0]->type == FFI_TYPE_DOUBLE) |
| 1323 |
size_al = FFI_ALIGN(arg_types[i]->size, 8); |
| 1324 |
# if defined(POWERPC64) |
| 1325 |
FFI_ASSERT (cif->abi != FFI_DARWIN); |
| 1326 |
avalue[i] = pgr; |
| 1327 |
pgr += (size_al + 7) / 8; |
| 1328 |
# else |
| 1329 |
/* Structures that match the basic modes (QI 1 byte, HI 2 bytes, |
| 1330 |
SI 4 bytes) are aligned as if they were those modes. */ |
| 1331 |
if (size_al < 3 && cif->abi == FFI_DARWIN) |
| 1332 |
avalue[i] = (char*) pgr + 4 - size_al; |
| 1333 |
else |
| 1334 |
avalue[i] = pgr; |
| 1335 |
pgr += (size_al + 3) / 4; |
| 1336 |
# endif |
| 1337 |
#endif |
| 1338 |
break; |
| 1339 |
|
| 1340 |
case FFI_TYPE_SINT64: |
| 1341 |
case FFI_TYPE_UINT64: |
| 1342 |
#if defined(POWERPC64) |
| 1343 |
case FFI_TYPE_POINTER: |
| 1344 |
avalue[i] = pgr; |
| 1345 |
pgr++; |
| 1346 |
break; |
| 1347 |
#else |
| 1348 |
/* Long long ints are passed in two gpr's. */ |
| 1349 |
avalue[i] = pgr; |
| 1350 |
pgr += 2; |
| 1351 |
break; |
| 1352 |
#endif |
| 1353 |
|
| 1354 |
case FFI_TYPE_FLOAT: |
| 1355 |
/* A float value consumes a GPR. |
| 1356 |
There are 13 64bit floating point registers. */ |
| 1357 |
if (pfr < end_pfr) |
| 1358 |
{ |
| 1359 |
double temp = pfr->d; |
| 1360 |
pfr->f = (float) temp; |
| 1361 |
avalue[i] = pfr; |
| 1362 |
pfr++; |
| 1363 |
} |
| 1364 |
else |
| 1365 |
{ |
| 1366 |
avalue[i] = pgr; |
| 1367 |
} |
| 1368 |
pgr++; |
| 1369 |
break; |
| 1370 |
|
| 1371 |
case FFI_TYPE_DOUBLE: |
| 1372 |
/* A double value consumes two GPRs. |
| 1373 |
There are 13 64bit floating point registers. */ |
| 1374 |
if (pfr < end_pfr) |
| 1375 |
{ |
| 1376 |
avalue[i] = pfr; |
| 1377 |
pfr++; |
| 1378 |
} |
| 1379 |
else |
| 1380 |
{ |
| 1381 |
avalue[i] = pgr; |
| 1382 |
} |
| 1383 |
#ifdef POWERPC64 |
| 1384 |
pgr++; |
| 1385 |
#else |
| 1386 |
pgr += 2; |
| 1387 |
#endif |
| 1388 |
break; |
| 1389 |
|
| 1390 |
#if FFI_TYPE_LONGDOUBLE != FFI_TYPE_DOUBLE |
| 1391 |
|
| 1392 |
case FFI_TYPE_LONGDOUBLE: |
| 1393 |
#ifdef POWERPC64 |
| 1394 |
if (pfr + 1 < end_pfr) |
| 1395 |
{ |
| 1396 |
avalue[i] = pfr; |
| 1397 |
pfr += 2; |
| 1398 |
} |
| 1399 |
else |
| 1400 |
{ |
| 1401 |
if (pfr < end_pfr) |
| 1402 |
{ |
| 1403 |
*pgr = *(unsigned long *) pfr; |
| 1404 |
pfr++; |
| 1405 |
} |
| 1406 |
avalue[i] = pgr; |
| 1407 |
} |
| 1408 |
pgr += 2; |
| 1409 |
#else /* POWERPC64 */ |
| 1410 |
/* A long double value consumes four GPRs and two FPRs. |
| 1411 |
There are 13 64bit floating point registers. */ |
| 1412 |
if (pfr + 1 < end_pfr) |
| 1413 |
{ |
| 1414 |
avalue[i] = pfr; |
| 1415 |
pfr += 2; |
| 1416 |
} |
| 1417 |
/* Here we have the situation where one part of the long double |
| 1418 |
is stored in fpr13 and the other part is already on the stack. |
| 1419 |
We use a union to pass the long double to avalue[i]. */ |
| 1420 |
else if (pfr + 1 == end_pfr) |
| 1421 |
{ |
| 1422 |
union ldu temp_ld; |
| 1423 |
memcpy (&temp_ld.lb[0], pfr, sizeof(ldbits)); |
| 1424 |
memcpy (&temp_ld.lb[1], pgr + 2, sizeof(ldbits)); |
| 1425 |
avalue[i] = &temp_ld.ld; |
| 1426 |
pfr++; |
| 1427 |
} |
| 1428 |
else |
| 1429 |
{ |
| 1430 |
avalue[i] = pgr; |
| 1431 |
} |
| 1432 |
pgr += 4; |
| 1433 |
#endif /* POWERPC64 */ |
| 1434 |
break; |
| 1435 |
#endif |
| 1436 |
default: |
| 1437 |
FFI_ASSERT(0); |
| 1438 |
} |
| 1439 |
i++; |
| 1440 |
} |
| 1441 |
|
| 1442 |
(fun) (cif, rvalue, avalue, user_data); |
| 1443 |
|
| 1444 |
/* Tell ffi_closure_ASM to perform return type promotions. */ |
| 1445 |
return cif->rtype; |
| 1446 |
} |
| 1447 |
|
| 1448 |
ffi_type * |
| 1449 |
ffi_closure_helper_DARWIN (ffi_closure *closure, void *rvalue, |
| 1450 |
unsigned long *pgr, ffi_dblfl *pfr) |
| 1451 |
{ |
| 1452 |
return ffi_closure_helper_common (closure->cif, closure->fun, |
| 1453 |
closure->user_data, rvalue, pgr, pfr); |
| 1454 |
} |
| 1455 |
|
| 1456 |
#if defined (FFI_GO_CLOSURES) |
| 1457 |
ffi_type * |
| 1458 |
ffi_go_closure_helper_DARWIN (ffi_go_closure *closure, void *rvalue, |
| 1459 |
unsigned long *pgr, ffi_dblfl *pfr) |
| 1460 |
{ |
| 1461 |
return ffi_closure_helper_common (closure->cif, closure->fun, |
| 1462 |
closure, rvalue, pgr, pfr); |
| 1463 |
} |
| 1464 |
#endif |