Skip to content

Commit

Permalink
arm: Rewrite ffi_closure
Browse files Browse the repository at this point in the history
Move the push of the argument registers into ffi_closure_SYSV,
reducing the size of the trampoline.
  • Loading branch information
rth7680 committed Nov 12, 2014
1 parent e7f15f6 commit a4b785e
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 4,837 deletions.
7 changes: 2 additions & 5 deletions Makefile.am
Expand Up @@ -48,9 +48,9 @@ EXTRA_DIST = LICENSE ChangeLog.v1 ChangeLog.libgcj \
src/xtensa/ffitarget.h src/xtensa/ffi.c src/xtensa/sysv.S \
ChangeLog.libffi m4/libtool.m4 m4/lt~obsolete.m4 \
m4/ltoptions.m4 m4/ltsugar.m4 m4/ltversion.m4 \
m4/ltversion.m4 src/arm/gentramp.sh src/debug.c msvcc.sh \
m4/ltversion.m4 src/debug.c msvcc.sh \
generate-darwin-source-and-headers.py \
libffi.xcodeproj/project.pbxproj src/arm/trampoline.S \
libffi.xcodeproj/project.pbxproj \
libtool-ldflags ChangeLog.libffi-3.1

info_TEXINFOS = doc/libffi.texi
Expand Down Expand Up @@ -190,9 +190,6 @@ nodist_libffi_la_SOURCES += src/arc/arcompact.S src/arc/ffi.c
endif
if ARM
nodist_libffi_la_SOURCES += src/arm/sysv.S src/arm/ffi.c
if FFI_EXEC_TRAMPOLINE_TABLE
nodist_libffi_la_SOURCES += src/arm/trampoline.S
endif
endif
if AVR32
nodist_libffi_la_SOURCES += src/avr32/sysv.S src/avr32/ffi.c
Expand Down
220 changes: 78 additions & 142 deletions src/arm/ffi.c
Expand Up @@ -364,157 +364,87 @@ ffi_call (ffi_cif * cif, void (*fn) (void), void *rvalue, void **avalue)
ffi_prep_args_SYSV (cif, flags, new_rvalue, avalue, stack);
ffi_call_SYSV (stack, frame, fn);
}

if (rvalue && rvalue != new_rvalue)
memcpy (rvalue, new_rvalue, rtype->size);
}

/** private members **/

static void ffi_prep_incoming_args_SYSV (char *stack, void **ret,
void **args, ffi_cif *cif,
float *vfp_stack);

static void ffi_prep_incoming_args_VFP (char *stack, void **ret,
void **args, ffi_cif *cif,
float *vfp_stack);

void ffi_closure_SYSV (ffi_closure *);

void ffi_closure_VFP (ffi_closure *);

/* This function is jumped to by the trampoline */

unsigned int FFI_HIDDEN
ffi_closure_inner (ffi_closure *closure,
void **respp, void *args, void *vfp_args)
{
// our various things...
ffi_cif *cif;
void **arg_area;

cif = closure->cif;
arg_area = (void **) alloca (cif->nargs * sizeof (void *));

/* this call will initialize ARG_AREA, such that each
* element in that array points to the corresponding
* value on the stack; and if the function returns
* a structure, it will re-set RESP to point to the
* structure return address. */
if (cif->abi == FFI_VFP)
ffi_prep_incoming_args_VFP (args, respp, arg_area, cif, vfp_args);
else
ffi_prep_incoming_args_SYSV (args, respp, arg_area, cif, vfp_args);

(closure->fun) (cif, *respp, arg_area, closure->user_data);

return cif->flags;
}

/*@-exportheader@*/
static void
ffi_prep_incoming_args_SYSV (char *stack, void **rvalue,
void **avalue, ffi_cif *cif,
/* Used only under VFP hard-float ABI. */
float *vfp_stack)
/*@=exportheader@*/
static void *
ffi_prep_incoming_args_SYSV (ffi_cif *cif, void *rvalue,
char *argp, void **avalue)
{
register unsigned int i;
register void **p_argv;
register char *argp;
register ffi_type **p_arg;

argp = stack;
ffi_type **arg_types = cif->arg_types;
int i, n;

if (cif->flags == FFI_TYPE_STRUCT)
if (cif->flags == ARM_TYPE_STRUCT)
{
*rvalue = *(void **) argp;
rvalue = *(void **) argp;
argp += 4;
}

p_argv = avalue;

for (i = cif->nargs, p_arg = cif->arg_types; (i != 0); i--, p_arg++)
for (i = 0, n = cif->nargs; i < n; i++)
{
size_t z;

argp = ffi_align (*p_arg, argp);

z = (*p_arg)->size;

/* because we're little endian, this is what it turns into. */

*p_argv = (void *) argp;
ffi_type *ty = arg_types[i];
size_t z = ty->size;

p_argv++;
argp = ffi_align (ty, argp);
avalue[i] = (void *) argp;
argp += z;
}

return;
return rvalue;
}

/*@-exportheader@*/
static void
ffi_prep_incoming_args_VFP (char *stack, void **rvalue,
void **avalue, ffi_cif * cif,
/* Used only under VFP hard-float ABI. */
float *vfp_stack)
/*@=exportheader@*/
static void *
ffi_prep_incoming_args_VFP (ffi_cif *cif, void *rvalue, char *stack,
char *vfp_space, void **avalue)
{
register unsigned int i, vi = 0;
register void **p_argv;
register char *argp, *regp, *eo_regp;
register ffi_type **p_arg;
ffi_type **arg_types = cif->arg_types;
int i, n, vi = 0;
char *argp, *regp, *eo_regp;
char done_with_regs = 0;
char stack_used = 0;

FFI_ASSERT (cif->abi == FFI_VFP);
regp = stack;
eo_regp = argp = regp + 16;

if (cif->flags == FFI_TYPE_STRUCT)
if (cif->flags == ARM_TYPE_STRUCT)
{
*rvalue = *(void **) regp;
rvalue = *(void **) regp;
regp += 4;
}

p_argv = avalue;

for (i = cif->nargs, p_arg = cif->arg_types; (i != 0); i--, p_arg++)
for (i = 0, n = cif->nargs; i < n; i++)
{
int is_vfp_type = vfp_type_p (*p_arg);
size_t z;
ffi_type *ty = arg_types[i];
int is_vfp_type = vfp_type_p (ty);
size_t z = ty->size;

if (vi < cif->vfp_nargs && is_vfp_type)
{
*p_argv++ = (void *) (vfp_stack + cif->vfp_args[vi++]);
avalue[i] = vfp_space + cif->vfp_args[vi++] * 4;
continue;
}
else if (!done_with_regs && !is_vfp_type)
{
char *tregp = ffi_align (*p_arg, regp);
char *tregp = ffi_align (ty, regp);

z = (*p_arg)->size;
z = (z < 4) ? 4 : z; // pad

/* if the arguments either fits into the registers or uses registers
* and stack, while we haven't read other things from the stack */
/* If the arguments either fits into the registers or uses registers
and stack, while we haven't read other things from the stack */
if (tregp + z <= eo_regp || !stack_used)
{
/* because we're little endian, this is what it turns into. */
*p_argv = (void *) tregp;

p_argv++;
/* Because we're little endian, this is what it turns into. */
avalue[i] = (void *) tregp;
regp = tregp + z;
// if we read past the last core register, make sure we have not read
// from the stack before and continue reading after regp

/* If we read past the last core register, make sure we
have not read from the stack before and continue
reading after regp. */
if (regp > eo_regp)
{
if (stack_used)
{
abort (); // we should never read past the end of the register
// are if the stack is already in use
}
FFI_ASSERT (!stack_used);
argp = regp;
}
if (regp >= eo_regp)
Expand All @@ -525,26 +455,41 @@ ffi_prep_incoming_args_VFP (char *stack, void **rvalue,
continue;
}
}
stack_used = 1;

argp = ffi_align (*p_arg, argp);
stack_used = 1;
argp = ffi_align (ty, argp);
avalue[i] = (void *) argp;
argp += z;
}

z = (*p_arg)->size;
return rvalue;
}

/* because we're little endian, this is what it turns into. */
int FFI_HIDDEN
ffi_closure_inner_SYSV (ffi_closure *closure, void *rvalue, char *argp)
{
ffi_cif *cif = closure->cif;
void **avalue = (void **) alloca (cif->nargs * sizeof (void *));

*p_argv = (void *) argp;
rvalue = ffi_prep_incoming_args_SYSV (cif, rvalue, argp, avalue);
closure->fun (cif, rvalue, avalue, closure->user_data);
return cif->flags;
}

p_argv++;
argp += z;
}
int FFI_HIDDEN
ffi_closure_inner_VFP (ffi_closure *closure, void *rvalue,
char *argp, char *vfp_space)
{
ffi_cif *cif = closure->cif;
void **avalue = (void **) alloca (cif->nargs * sizeof (void *));

return;
rvalue = ffi_prep_incoming_args_VFP (cif, rvalue, argp, vfp_space, avalue);
closure->fun (cif, rvalue, avalue, closure->user_data);
return cif->flags;
}

/* How to make a trampoline. */

extern unsigned int ffi_arm_trampoline[3];
void ffi_closure_SYSV (void) FFI_HIDDEN;
void ffi_closure_VFP (void) FFI_HIDDEN;

#if FFI_EXEC_TRAMPOLINE_TABLE

Expand Down Expand Up @@ -788,19 +733,7 @@ ffi_closure_free (void *ptr)

#else

#define FFI_INIT_TRAMPOLINE(TRAMP,FUN,CTX) \
({ unsigned char *__tramp = (unsigned char*)(TRAMP); \
unsigned int __fun = (unsigned int)(FUN); \
unsigned int __ctx = (unsigned int)(CTX); \
unsigned char *insns = (unsigned char *)(CTX); \
memcpy (__tramp, ffi_arm_trampoline, sizeof ffi_arm_trampoline); \
*(unsigned int*) &__tramp[12] = __ctx; \
*(unsigned int*) &__tramp[16] = __fun; \
__clear_cache((&__tramp[0]), (&__tramp[19])); /* Clear data mapping. */ \
__clear_cache(insns, insns + 3 * sizeof (unsigned int)); \
/* Clear instruction \
mapping. */ \
})
extern unsigned int ffi_arm_trampoline[2] FFI_HIDDEN;

#endif

Expand All @@ -812,28 +745,31 @@ ffi_prep_closure_loc (ffi_closure * closure,
void (*fun) (ffi_cif *, void *, void **, void *),
void *user_data, void *codeloc)
{
void (*closure_func) (ffi_closure *) = NULL;
void (*closure_func) (void) = ffi_closure_SYSV;

if (cif->abi == FFI_SYSV)
closure_func = &ffi_closure_SYSV;
#ifdef __ARM_EABI__
else if (cif->abi == FFI_VFP)
closure_func = &ffi_closure_VFP;
#endif
else
if (cif->abi == FFI_VFP)
{
/* We only need take the vfp path if there are vfp arguments. */
if (cif->vfp_used)
closure_func = ffi_closure_VFP;
}
else if (cif->abi != FFI_SYSV)
return FFI_BAD_ABI;

#if FFI_EXEC_TRAMPOLINE_TABLE
void **config = FFI_TRAMPOLINE_CODELOC_CONFIG (codeloc);
config[0] = closure;
config[1] = closure_func;
#else
FFI_INIT_TRAMPOLINE (&closure->tramp[0], closure_func, codeloc);
memcpy (closure->tramp, ffi_arm_trampoline, 8);
__clear_cache(closure->tramp, closure->tramp + 8); /* clear data map */
__clear_cache(codeloc, codeloc + 8); /* clear insn map */
*(void (**)(void))(closure->tramp + 8) = closure_func;
#endif

closure->cif = cif;
closure->user_data = user_data;
closure->fun = fun;
closure->user_data = user_data;

return FFI_OK;
}
Expand Down
2 changes: 1 addition & 1 deletion src/arm/ffitarget.h
Expand Up @@ -65,7 +65,7 @@ typedef enum ffi_abi {
/* ---- Definitions for closures ----------------------------------------- */

#define FFI_CLOSURES 1
#define FFI_TRAMPOLINE_SIZE 20
#define FFI_TRAMPOLINE_SIZE 12
#define FFI_NATIVE_RAW_API 0

#endif

0 comments on commit a4b785e

Please sign in to comment.