/* -----------------------------------------------------------------------
ffi.c - Copyright (c) 2000, 2007 Software AG
Copyright (c) 2008 Red Hat, Inc
-
+
S390 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
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.
/* Includes */
/* -------- */
/*====================================================================*/
-
+
#include <ffi.h>
#include <ffi_common.h>
-
+
#include <stdlib.h>
#include <stdio.h>
-
+
/*====================== End of Includes =============================*/
-
+
/*====================================================================*/
/* Defines */
/* ------- */
/*====================================================================*/
-/* Maximum number of GPRs available for argument passing. */
+/* Maximum number of GPRs available for argument passing. */
#define MAX_GPRARGS 5
-/* Maximum number of FPRs available for argument passing. */
+/* Maximum number of FPRs available for argument passing. */
#ifdef __s390x__
#define MAX_FPRARGS 4
#else
#define FFI390_RET_INT64 5
/*===================== End of Defines ===============================*/
-
+
/*====================================================================*/
/* Externals */
/* --------- */
/*====================================================================*/
-
+
extern void ffi_call_SYSV(unsigned,
extended_cif *,
void (*)(unsigned char *, extended_cif *),
extern void ffi_closure_SYSV(void);
extern void ffi_go_closure_SYSV(void);
-
+
/*====================== End of Externals ============================*/
-
+
/*====================================================================*/
/* */
/* Name - ffi_check_struct_type. */
/* general purpose or floating point register. */
/* */
/*====================================================================*/
-
+
static int
ffi_check_struct_type (ffi_type *arg)
{
/* If the struct has just one element, look at that element
to find out whether to consider the struct as floating point. */
- while (arg->type == FFI_TYPE_STRUCT
+ while (arg->type == FFI_TYPE_STRUCT
&& arg->elements[0] && !arg->elements[1])
arg = arg->elements[0];
/* Other structs are passed via a pointer to the data. */
return FFI_TYPE_POINTER;
}
-
+
/*======================== End of Routine ============================*/
-
+
/*====================================================================*/
/* */
/* Name - ffi_prep_args. */
/* has been allocated for the function's arguments. */
/* */
/*====================================================================*/
-
+
static void
ffi_prep_args (unsigned char *stack, extended_cif *ecif)
{
ffi_type **ptr;
void **p_argv = ecif->avalue;
int i;
-
+
/* If we returning a structure then we set the first parameter register
to the address of where we are returning this structure. */
p_gpr[n_gpr++] = (unsigned long) ecif->rvalue;
/* Now for the arguments. */
-
+
for (ptr = ecif->cif->arg_types, i = ecif->cif->nargs;
i > 0;
i--, ptr++, p_argv++)
}
/* Now handle all primitive int/pointer/float data types. */
- switch (type)
+ switch (type)
{
case FFI_TYPE_DOUBLE:
if (n_fpr < MAX_FPRARGS)
p_ov[n_ov++] = ((unsigned long *) arg)[1];
#endif
break;
-
+
case FFI_TYPE_FLOAT:
if (n_fpr < MAX_FPRARGS)
p_fpr[n_fpr++] = (long long) *(unsigned int *) arg << 32;
else
p_ov[n_ov++] = (unsigned long)*(unsigned char **) arg;
break;
-
+
case FFI_TYPE_UINT64:
case FFI_TYPE_SINT64:
#ifdef __s390x__
p_ov[n_ov++] = ((unsigned long *) arg)[1];
#endif
break;
-
+
case FFI_TYPE_UINT32:
if (n_gpr < MAX_GPRARGS)
p_gpr[n_gpr++] = *(unsigned int *) arg;
else
p_ov[n_ov++] = *(unsigned int *) arg;
break;
-
+
case FFI_TYPE_INT:
case FFI_TYPE_SINT32:
if (n_gpr < MAX_GPRARGS)
else
p_ov[n_ov++] = *(signed int *) arg;
break;
-
+
case FFI_TYPE_UINT16:
if (n_gpr < MAX_GPRARGS)
p_gpr[n_gpr++] = *(unsigned short *) arg;
else
p_ov[n_ov++] = *(unsigned short *) arg;
break;
-
+
case FFI_TYPE_SINT16:
if (n_gpr < MAX_GPRARGS)
p_gpr[n_gpr++] = *(signed short *) arg;
else
p_ov[n_ov++] = *(unsigned char *) arg;
break;
-
+
case FFI_TYPE_SINT8:
if (n_gpr < MAX_GPRARGS)
p_gpr[n_gpr++] = *(signed char *) arg;
else
p_ov[n_ov++] = *(signed char *) arg;
break;
-
+
default:
FFI_ASSERT (0);
break;
}
/*======================== End of Routine ============================*/
-
+
/*====================================================================*/
/* */
/* Name - ffi_prep_cif_machdep. */
/* Function - Perform machine dependent CIF processing. */
/* */
/*====================================================================*/
-
+
ffi_status
ffi_prep_cif_machdep(ffi_cif *cif)
{
ffi_type **ptr;
int i;
- /* Determine return value handling. */
+ /* Determine return value handling. */
switch (cif->rtype->type)
{
case FFI_TYPE_COMPLEX:
cif->flags = FFI390_RET_STRUCT;
n_gpr++; /* We need one GPR to pass the pointer. */
- break;
+ break;
/* Floating point values are returned in fpr 0. */
case FFI_TYPE_FLOAT:
cif->flags = FFI390_RET_INT32;
#endif
break;
-
+
default:
FFI_ASSERT (0);
break;
}
/* Now for the arguments. */
-
+
for (ptr = cif->arg_types, i = cif->nargs;
i > 0;
i--, ptr++)
}
/* Now handle all primitive int/float data types. */
- switch (type)
+ switch (type)
{
/* The first MAX_FPRARGS floating point arguments
go in FPRs, the rest overflow to the stack. */
else
n_ov += sizeof (double) / sizeof (long);
break;
-
+
case FFI_TYPE_FLOAT:
if (n_fpr < MAX_FPRARGS)
n_fpr++;
/* On 31-bit machines, 64-bit integers are passed in GPR pairs,
if one is still available, or else on the stack. If only one
- register is free, skip the register (it won't be used for any
+ register is free, skip the register (it won't be used for any
subsequent argument either). */
-
+
#ifndef __s390x__
case FFI_TYPE_UINT64:
case FFI_TYPE_SINT64:
/* Everything else is passed in GPRs (until MAX_GPRARGS
have been used) or overflows to the stack. */
- default:
+ default:
if (n_gpr < MAX_GPRARGS)
n_gpr++;
else
and temporary structure copies. */
cif->bytes = ROUND_SIZE (n_ov * sizeof (long)) + struct_size;
-
+
return FFI_OK;
}
-
+
/*======================== End of Routine ============================*/
-
+
/*====================================================================*/
/* */
/* Name - ffi_call. */
/* Function - Call the FFI routine. */
/* */
/*====================================================================*/
-
+
static void
ffi_call_int(ffi_cif *cif,
void (*fn)(void),
{
int ret_type = cif->flags;
extended_cif ecif;
-
+
ecif.cif = cif;
ecif.avalue = avalue;
ecif.rvalue = rvalue;
ecif.rvalue = alloca (cif->rtype->size);
else
ret_type = FFI390_RET_VOID;
- }
+ }
switch (cif->abi)
{
ffi_call_SYSV (cif->bytes, &ecif, ffi_prep_args,
ret_type, ecif.rvalue, fn, closure);
break;
-
+
default:
FFI_ASSERT (0);
break;
{
ffi_call_int(cif, fn, rvalue, avalue, closure);
}
-
+
/*======================== End of Routine ============================*/
/*====================================================================*/
p_arg = avalue = alloca (cif->nargs * sizeof (void *));
- /* If we returning a structure, pass the structure address
- directly to the target function. Otherwise, have the target
+ /* If we returning a structure, pass the structure address
+ directly to the target function. Otherwise, have the target
function store the return value to the GPR save area. */
if (cif->flags == FFI390_RET_STRUCT)
else
type = ffi_check_struct_type (*ptr);
- /* If we pass the struct via pointer, remember to
+ /* If we pass the struct via pointer, remember to
retrieve the pointer later. */
if (type == FFI_TYPE_POINTER)
deref_struct_pointer = 1;
#endif
/* Now handle all primitive int/float data types. */
- switch (type)
+ switch (type)
{
case FFI_TYPE_DOUBLE:
if (n_fpr < MAX_FPRARGS)
*p_arg = &p_fpr[n_fpr++];
else
- *p_arg = &p_ov[n_ov],
+ *p_arg = &p_ov[n_ov],
n_ov += sizeof (double) / sizeof (long);
break;
-
+
case FFI_TYPE_FLOAT:
if (n_fpr < MAX_FPRARGS)
*p_arg = &p_fpr[n_fpr++];
else
*p_arg = (char *)&p_ov[n_ov++] + sizeof (long) - 4;
break;
-
+
case FFI_TYPE_UINT64:
case FFI_TYPE_SINT64:
#ifdef __s390x__
*p_arg = &p_ov[n_ov], n_ov += 2;
#endif
break;
-
+
case FFI_TYPE_INT:
case FFI_TYPE_UINT32:
case FFI_TYPE_SINT32:
else
*p_arg = (char *)&p_ov[n_ov++] + sizeof (long) - 4;
break;
-
+
case FFI_TYPE_UINT16:
case FFI_TYPE_SINT16:
if (n_gpr < MAX_GPRARGS)
else
*p_arg = (char *)&p_ov[n_ov++] + sizeof (long) - 1;
break;
-
+
default:
FFI_ASSERT (0);
break;
break;
}
}
-
+
/*======================== End of Routine ============================*/
/*====================================================================*/
/* Function - Prepare a FFI closure. */
/* */
/*====================================================================*/
-
+
ffi_status
ffi_prep_closure_loc (ffi_closure *closure,
ffi_cif *cif,
*(short *)&closure->tramp [8] = 0x07f1; /* br %r1 */
*(long *)&closure->tramp[16] = (long)codeloc;
*(long *)&closure->tramp[24] = (long)&ffi_closure_SYSV;
-#endif
-
+#endif
+
closure->cif = cif;
closure->user_data = user_data;
closure->fun = fun;
-
+
return FFI_OK;
}
/*======================== End of Routine ============================*/
-
+
/* Build a Go language closure. */
ffi_status
/* -----------------------------------------------------------------------
sysv.S - Copyright (c) 2000 Software AG
Copyright (c) 2008 Red Hat, Inc.
-
+
S390 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
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
#include <ffi.h>
#ifndef __s390x__
-
+
.text
# r2: cif->bytes
# r4: ffi_prep_args
# r5: ret_type
# r6: ecif.rvalue
- # ov: fn
+ # ov: fn
# ov+8: closure
# This assumes we are using gas.
.cfi_restore_state
# See comment on the nopr above.
nopr
-
+
.LretDouble:
l %r4,48+56(%r11)
std %f0,0(%r8) # Return double
.cfi_restore_state
# See comment on the nopr above.
nopr
-
+
.LretInt64:
l %r4,48+56(%r11)
stm %r2,%r3,0(%r8) # Return long long
.cfi_def_cfa r15, 96
br %r4
.cfi_endproc
-
+
.Ltable:
.byte .LretNone-.Lbase # FFI390_RET_VOID
.byte .LretNone-.Lbase # FFI390_RET_STRUCT
.cfi_endproc
#else
-
+
.text
-
+
# r2: cif->bytes
# r3: &ecif
# r4: ffi_prep_args
# r5: ret_type
# r6: ecif.rvalue
- # ov: fn
+ # ov: fn
# ov+8: closure
# This assumes we are using gas.
.cfi_restore_state
# See comment on the nopr above.
nopr
-
+
.LretDouble:
lg %r4,80+112(%r11)
std %f0,0(%r8) # Return double
.cfi_def_cfa r15, 160
br %r4
.cfi_endproc
-
+
.Ltable:
.byte .LretNone-.Lbase # FFI390_RET_VOID
.byte .LretNone-.Lbase # FFI390_RET_STRUCT
.ffi_closure_SYSV_end:
.size ffi_closure_SYSV,.ffi_closure_SYSV_end-ffi_closure_SYSV
-
+
.globl ffi_go_closure_SYSV
FFI_HIDDEN(ffi_go_closure_SYSV)
.type ffi_go_closure_SYSV,%function