X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-psi;a=blobdiff_plain;f=src%2Flibffi.c;h=ac018a119640d8ea0deed03ba479cf1e6c18a260;hp=ee077af19b4b2af2cbba62b6c41dc2961355eaa8;hb=b0a8b273be0744ed445fbe6c935a04ee7a749f14;hpb=6f10fb5abd8d46fbfa5b447983f6101fc170585c diff --git a/src/libffi.c b/src/libffi.c index ee077af..ac018a1 100644 --- a/src/libffi.c +++ b/src/libffi.c @@ -91,10 +91,63 @@ static inline ffi_type *psi_ffi_token_type(token_t t) { return &ffi_type_float; case PSI_T_DOUBLE: return &ffi_type_double; + case PSI_T_POINTER: + return &ffi_type_pointer; + } +} +static inline ffi_type *psi_ffi_impl_type(token_t impl_type) { + switch (impl_type) { + case PSI_T_BOOL: + return &ffi_type_sint8; + case PSI_T_INT: + return &ffi_type_sint64; + case PSI_T_STRING: + return &ffi_type_pointer; + case PSI_T_FLOAT: + case PSI_T_DOUBLE: + return &ffi_type_double; + EMPTY_SWITCH_DEFAULT_CASE(); + } +} +static void psi_ffi_struct_type_dtor(void *type) { + ffi_type *strct = type; + + if (strct->elements) { + free(strct->elements); } + free(strct); +} +static inline ffi_type *psi_ffi_decl_arg_type(decl_arg *darg); +static ffi_type **psi_ffi_struct_type_elements(decl_struct *strct) { + ffi_type **els = calloc(strct->args->count + 1, sizeof(*els)); + size_t i; + + for (i = 0; i < strct->args->count; ++i) { + els[i] = psi_ffi_decl_arg_type(strct->args->args[i]); + } + els[i] = NULL; + + return els; } static inline ffi_type *psi_ffi_decl_type(decl_type *type) { - return psi_ffi_token_type(real_decl_type(type)->type); + decl_type *real = real_decl_type(type); + + if (real->type == PSI_T_STRUCT) { + if (!real->strct->engine.type) { + ffi_type *strct = calloc(1, sizeof(ffi_type)); + + strct->type = FFI_TYPE_STRUCT; + strct->size = real->strct->size; + strct->alignment = 0; + strct->elements = psi_ffi_struct_type_elements(real->strct); + + real->strct->engine.type = strct; + real->strct->engine.dtor = psi_ffi_struct_type_dtor; + } + + return real->strct->engine.type; + } + return psi_ffi_token_type(real->type); } static inline ffi_type *psi_ffi_decl_arg_type(decl_arg *darg) { if (darg->var->pointer_level) { @@ -127,6 +180,8 @@ static inline PSI_LibffiCall *PSI_LibffiCallAlloc(PSI_Context *C, decl *decl) { call->params[c] = NULL; decl->call.info = call; + decl->call.rval = decl->func->ptr; + decl->call.argc = c; decl->call.args = (void **) &call->params[c+1]; rc = ffi_prep_cif(&call->signature, psi_ffi_abi(decl->abi->convention), @@ -250,10 +305,38 @@ static zend_function_entry *psi_ffi_compile(PSI_Context *C) return zfe; } -static void psi_ffi_call(PSI_Context *C, impl_val *ret_val, decl *decl) { - PSI_LibffiCall *call = decl->call.info; +static void psi_ffi_call(PSI_Context *C, decl_callinfo *decl_call, impl_vararg *va) { + PSI_LibffiCall *call = decl_call->info; - ffi_call(&call->signature, FFI_FN(decl->call.sym), ret_val, decl->call.args); + if (va) { + ffi_status rc; + ffi_cif signature; + size_t i, nfixedargs = decl_call->argc, ntotalargs = nfixedargs + va->args->count; + void **params = calloc(2 * ntotalargs + 2, sizeof(void *)); + + for (i = 0; i < nfixedargs; ++i) { + params[i] = call->params[i]; + params[i + ntotalargs + 1] = call->params[i + nfixedargs + 1]; + } + for (i = 0; i < va->args->count; ++i) { + params[nfixedargs + i] = psi_ffi_impl_type(va->types[i]); + params[nfixedargs + i + ntotalargs + 1] = &va->values[i]; + } +#ifdef PSI_HAVE_FFI_PREP_CIF_VAR + rc = ffi_prep_cif_var(&signature, call->signature.abi, + nfixedargs, ntotalargs, + call->signature.rtype, (ffi_type **) params); +#else + /* FIXME: test in config.m4; assume we can just call anyway */ + rc = ffi_prep_cif(&signature, call->signature.abi, ntotalargs, + call->signature.rtype, (ffi_type **) params); +#endif + ZEND_ASSERT(FFI_OK == rc); + ffi_call(&signature, FFI_FN(decl_call->sym), decl_call->rval, ¶ms[ntotalargs + 1]); + free(params); + } else { + ffi_call(&call->signature, FFI_FN(decl_call->sym), decl_call->rval, decl_call->args); + } } static PSI_ContextOps ops = {