1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
/* -----------------------------------------------------------------------
ffi.c - Copyright (c) 2012 Anthony Green
Copyright (c) 1998, 2001, 2007, 2008 Red Hat, Inc.
Alpha Foreign Function Interface
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
``Software''), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
----------------------------------------------------------------------- */
#include <ffi.h>
#include <ffi_common.h>
#include <stdlib.h>
#include "internal.h"
/* Force FFI_TYPE_LONGDOUBLE to be different than FFI_TYPE_DOUBLE;
all further uses in this file will refer to the 128-bit type. */
#if defined(__LONG_DOUBLE_128__)
# if FFI_TYPE_LONGDOUBLE != 4
# error FFI_TYPE_LONGDOUBLE out of date
# endif
#else
# undef FFI_TYPE_LONGDOUBLE
# define FFI_TYPE_LONGDOUBLE 4
#endif
extern void ffi_call_osf(void *stack, void *frame, unsigned flags,
void *raddr, void (*fn)(void), void *closure)
FFI_HIDDEN;
extern void ffi_closure_osf(void) FFI_HIDDEN;
extern void ffi_go_closure_osf(void) FFI_HIDDEN;
/* Promote a float value to its in-register double representation.
Unlike actually casting to double, this does not trap on NaN. */
static inline UINT64 lds(void *ptr)
{
UINT64 ret;
asm("lds %0,%1" : "=f"(ret) : "m"(*(UINT32 *)ptr));
return ret;
}
/* And the reverse. */
static inline void sts(void *ptr, UINT64 val)
{
asm("sts %1,%0" : "=m"(*(UINT32 *)ptr) : "f"(val));
}
ffi_status FFI_HIDDEN
ffi_prep_cif_machdep(ffi_cif *cif)
{
size_t bytes = 0;
int flags, i, avn;
ffi_type *rtype, *itype;
if (cif->abi != FFI_OSF)
return FFI_BAD_ABI;
/* Compute the size of the argument area. */
for (i = 0, avn = cif->nargs; i < avn; i++)
{
itype = cif->arg_types[i];
switch (itype->type)
{
case FFI_TYPE_INT:
case FFI_TYPE_SINT8:
case FFI_TYPE_UINT8:
case FFI_TYPE_SINT16:
case FFI_TYPE_UINT16:
case FFI_TYPE_SINT32:
case FFI_TYPE_UINT32:
case FFI_TYPE_SINT64:
case FFI_TYPE_UINT64:
case FFI_TYPE_POINTER:
case FFI_TYPE_FLOAT:
case FFI_TYPE_DOUBLE:
case FFI_TYPE_LONGDOUBLE:
/* All take one 8 byte slot. */
bytes += 8;
break;
case FFI_TYPE_VOID:
case FFI_TYPE_STRUCT:
/* Passed by value in N slots. */
bytes += FFI_ALIGN(itype->size, FFI_SIZEOF_ARG);
break;
case FFI_TYPE_COMPLEX:
/* _Complex long double passed by reference; others in 2 slots. */
if (itype->elements[0]->type == FFI_TYPE_LONGDOUBLE)
bytes += 8;
else
bytes += 16;
break;
default:
abort();
}
}
/* Set the return type flag */
rtype = cif->rtype;
switch (rtype->type)
{
case FFI_TYPE_VOID:
flags = ALPHA_FLAGS(ALPHA_ST_VOID, ALPHA_LD_VOID);
break;
case FFI_TYPE_INT:
case FFI_TYPE_UINT32:
case FFI_TYPE_SINT32:
flags = ALPHA_FLAGS(ALPHA_ST_INT, ALPHA_LD_INT32);
break;
case FFI_TYPE_FLOAT:
flags = ALPHA_FLAGS(ALPHA_ST_FLOAT, ALPHA_LD_FLOAT);
break;
case FFI_TYPE_DOUBLE:
flags = ALPHA_FLAGS(ALPHA_ST_DOUBLE, ALPHA_LD_DOUBLE);
break;
case FFI_TYPE_UINT8:
flags = ALPHA_FLAGS(ALPHA_ST_INT, ALPHA_LD_UINT8);
break;
case FFI_TYPE_SINT8:
flags = ALPHA_FLAGS(ALPHA_ST_INT, ALPHA_LD_SINT8);
break;
case FFI_TYPE_UINT16:
flags = ALPHA_FLAGS(ALPHA_ST_INT, ALPHA_LD_UINT16);
break;
case FFI_TYPE_SINT16:
flags = ALPHA_FLAGS(ALPHA_ST_INT, ALPHA_LD_SINT16);
break;
case FFI_TYPE_UINT64:
case FFI_TYPE_SINT64:
case FFI_TYPE_POINTER:
flags = ALPHA_FLAGS(ALPHA_ST_INT, ALPHA_LD_INT64);
break;
case FFI_TYPE_LONGDOUBLE:
case FFI_TYPE_STRUCT:
/* Passed in memory, with a hidden pointer. */
flags = ALPHA_RET_IN_MEM;
break;
case FFI_TYPE_COMPLEX:
itype = rtype->elements[0];
switch (itype->type)
{
case FFI_TYPE_FLOAT:
flags = ALPHA_FLAGS(ALPHA_ST_CPLXF, ALPHA_LD_CPLXF);
break;
case FFI_TYPE_DOUBLE:
flags = ALPHA_FLAGS(ALPHA_ST_CPLXD, ALPHA_LD_CPLXD);
break;
default:
if (rtype->size <= 8)
flags = ALPHA_FLAGS(ALPHA_ST_INT, ALPHA_LD_INT64);
else
flags = ALPHA_RET_IN_MEM;
break;
}
break;
default:
abort();
}
cif->flags = flags;
/* Include the hidden structure pointer in args requirement. */
if (flags == ALPHA_RET_IN_MEM)
bytes += 8;
/* Minimum size is 6 slots, so that ffi_call_osf can pop them. */
if (bytes < 6*8)
bytes = 6*8;
cif->bytes = bytes;
return FFI_OK;
}
static unsigned long
extend_basic_type(void *valp, int type, int argn)
{
switch (type)
{
case FFI_TYPE_SINT8:
return *(SINT8 *)valp;
case FFI_TYPE_UINT8:
return *(UINT8 *)valp;
case FFI_TYPE_SINT16:
return *(SINT16 *)valp;
case FFI_TYPE_UINT16:
return *(UINT16 *)valp;
case FFI_TYPE_FLOAT:
if (argn < 6)
return lds(valp);
/* FALLTHRU */
case FFI_TYPE_INT:
case FFI_TYPE_SINT32:
case FFI_TYPE_UINT32:
/* Note that unsigned 32-bit quantities are sign extended. */
return *(SINT32 *)valp;
case FFI_TYPE_SINT64:
case FFI_TYPE_UINT64:
case FFI_TYPE_POINTER:
case FFI_TYPE_DOUBLE:
return *(UINT64 *)valp;
default:
abort();
}
}
static void
ffi_call_int (ffi_cif *cif, void (*fn)(void), void *rvalue,
void **avalue, void *closure)
{
unsigned long *argp;
long i, avn, argn, flags = cif->flags;
ffi_type **arg_types;
void *frame;
/* If the return value is a struct and we don't have a return
value address then we need to make one. */
if (rvalue == NULL && flags == ALPHA_RET_IN_MEM)
rvalue = alloca(cif->rtype->size);
/* Allocate the space for the arguments, plus 4 words of temp
space for ffi_call_osf. */
argp = frame = alloca(cif->bytes + 4*FFI_SIZEOF_ARG);
frame += cif->bytes;
argn = 0;
if (flags == ALPHA_RET_IN_MEM)
argp[argn++] = (unsigned long)rvalue;
avn = cif->nargs;
arg_types = cif->arg_types;
for (i = 0, avn = cif->nargs; i < avn; i++)
{
ffi_type *ty = arg_types[i];
void *valp = avalue[i];
int type = ty->type;
size_t size;
switch (type)
{
case FFI_TYPE_INT:
case FFI_TYPE_SINT8:
case FFI_TYPE_UINT8:
case FFI_TYPE_SINT16:
case FFI_TYPE_UINT16:
case FFI_TYPE_SINT32:
case FFI_TYPE_UINT32:
case FFI_TYPE_SINT64:
case FFI_TYPE_UINT64:
case FFI_TYPE_POINTER:
case FFI_TYPE_FLOAT:
case FFI_TYPE_DOUBLE:
argp[argn] = extend_basic_type(valp, type, argn);
argn++;
break;
case FFI_TYPE_LONGDOUBLE:
by_reference:
/* Note that 128-bit long double is passed by reference. */
argp[argn++] = (unsigned long)valp;
break;
case FFI_TYPE_VOID:
case FFI_TYPE_STRUCT:
size = ty->size;
memcpy(argp + argn, valp, size);
argn += FFI_ALIGN(size, FFI_SIZEOF_ARG) / FFI_SIZEOF_ARG;
break;
case FFI_TYPE_COMPLEX:
type = ty->elements[0]->type;
if (type == FFI_TYPE_LONGDOUBLE)
goto by_reference;
/* Most complex types passed as two separate arguments. */
size = ty->elements[0]->size;
argp[argn] = extend_basic_type(valp, type, argn);
argp[argn + 1] = extend_basic_type(valp + size, type, argn + 1);
argn += 2;
break;
default:
abort();
}
}
flags = (flags >> ALPHA_ST_SHIFT) & 0xff;
ffi_call_osf(argp, frame, flags, rvalue, fn, closure);
}
void
ffi_call (ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue)
{
ffi_call_int(cif, fn, rvalue, avalue, NULL);
}
void
ffi_call_go (ffi_cif *cif, void (*fn)(void), void *rvalue,
void **avalue, void *closure)
{
ffi_call_int(cif, fn, rvalue, avalue, closure);
}
ffi_status
ffi_prep_closure_loc (ffi_closure* closure,
ffi_cif* cif,
void (*fun)(ffi_cif*, void*, void**, void*),
void *user_data,
void *codeloc)
{
unsigned int *tramp;
if (cif->abi != FFI_OSF)
return FFI_BAD_ABI;
tramp = (unsigned int *) &closure->tramp[0];
tramp[0] = 0x47fb0401; /* mov $27,$1 */
tramp[1] = 0xa77b0010; /* ldq $27,16($27) */
tramp[2] = 0x6bfb0000; /* jmp $31,($27),0 */
tramp[3] = 0x47ff041f; /* nop */
*(void **) &tramp[4] = ffi_closure_osf;
closure->cif = cif;
closure->fun = fun;
closure->user_data = user_data;
/* Flush the Icache.
Tru64 UNIX as doesn't understand the imb mnemonic, so use call_pal
instead, since both Compaq as and gas can handle it.
0x86 is PAL_imb in Tru64 UNIX <alpha/pal.h>. */
asm volatile ("call_pal 0x86" : : : "memory");
return FFI_OK;
}
ffi_status
ffi_prep_go_closure (ffi_go_closure* closure,
ffi_cif* cif,
void (*fun)(ffi_cif*, void*, void**, void*))
{
if (cif->abi != FFI_OSF)
return FFI_BAD_ABI;
closure->tramp = (void *)ffi_go_closure_osf;
closure->cif = cif;
closure->fun = fun;
return FFI_OK;
}
long FFI_HIDDEN
ffi_closure_osf_inner (ffi_cif *cif,
void (*fun)(ffi_cif*, void*, void**, void*),
void *user_data,
void *rvalue, unsigned long *argp)
{
void **avalue;
ffi_type **arg_types;
long i, avn, argn, flags;
avalue = alloca(cif->nargs * sizeof(void *));
flags = cif->flags;
argn = 0;
/* Copy the caller's structure return address to that the closure
returns the data directly to the caller. */
if (flags == ALPHA_RET_IN_MEM)
{
rvalue = (void *) argp[0];
argn = 1;
}
arg_types = cif->arg_types;
/* Grab the addresses of the arguments from the stack frame. */
for (i = 0, avn = cif->nargs; i < avn; i++)
{
ffi_type *ty = arg_types[i];
int type = ty->type;
void *valp = &argp[argn];
size_t size;
switch (type)
{
case FFI_TYPE_INT:
case FFI_TYPE_SINT8:
case FFI_TYPE_UINT8:
case FFI_TYPE_SINT16:
case FFI_TYPE_UINT16:
case FFI_TYPE_SINT32:
case FFI_TYPE_UINT32:
case FFI_TYPE_SINT64:
case FFI_TYPE_UINT64:
case FFI_TYPE_POINTER:
argn += 1;
break;
case FFI_TYPE_VOID:
case FFI_TYPE_STRUCT:
size = ty->size;
argn += FFI_ALIGN(size, FFI_SIZEOF_ARG) / FFI_SIZEOF_ARG;
break;
case FFI_TYPE_FLOAT:
/* Floats coming from registers need conversion from double
back to float format. */
if (argn < 6)
{
valp = &argp[argn - 6];
sts(valp, argp[argn - 6]);
}
argn += 1;
break;
case FFI_TYPE_DOUBLE:
if (argn < 6)
valp = &argp[argn - 6];
argn += 1;
break;
case FFI_TYPE_LONGDOUBLE:
by_reference:
/* 128-bit long double is passed by reference. */
valp = (void *)argp[argn];
argn += 1;
break;
case FFI_TYPE_COMPLEX:
type = ty->elements[0]->type;
switch (type)
{
case FFI_TYPE_SINT64:
case FFI_TYPE_UINT64:
/* Passed as separate arguments, but they wind up sequential. */
break;
case FFI_TYPE_INT:
case FFI_TYPE_SINT8:
case FFI_TYPE_UINT8:
case FFI_TYPE_SINT16:
case FFI_TYPE_UINT16:
case FFI_TYPE_SINT32:
case FFI_TYPE_UINT32:
/* Passed as separate arguments. Disjoint, but there's room
enough in one slot to hold the pair. */
size = ty->elements[0]->size;
memcpy(valp + size, valp + 8, size);
break;
case FFI_TYPE_FLOAT:
/* Passed as separate arguments. Disjoint, and each piece
may need conversion back to float. */
if (argn < 6)
{
valp = &argp[argn - 6];
sts(valp, argp[argn - 6]);
}
if (argn + 1 < 6)
sts(valp + 4, argp[argn + 1 - 6]);
else
*(UINT32 *)(valp + 4) = argp[argn + 1];
break;
case FFI_TYPE_DOUBLE:
/* Passed as separate arguments. Only disjoint if one part
is in fp regs and the other is on the stack. */
if (argn < 5)
valp = &argp[argn - 6];
else if (argn == 5)
{
valp = alloca(16);
((UINT64 *)valp)[0] = argp[5 - 6];
((UINT64 *)valp)[1] = argp[6];
}
break;
case FFI_TYPE_LONGDOUBLE:
goto by_reference;
default:
abort();
}
argn += 2;
break;
default:
abort ();
}
avalue[i] = valp;
}
/* Invoke the closure. */
fun (cif, rvalue, avalue, user_data);
/* Tell ffi_closure_osf how to perform return type promotions. */
return (flags >> ALPHA_LD_SHIFT) & 0xff;
}