X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-psi;a=blobdiff_plain;f=src%2Flibffi.c;h=221e2c24507689b02549a5fd65f6a3551c853685;hp=d59712cd9a595abe30307e43e4fa85a4a5a8573b;hb=8b652242a4ef920f2ab82bfb822596de25bc5a63;hpb=77a446cbcdce6558c00066e5f13e43e8b1b18ff7 diff --git a/src/libffi.c b/src/libffi.c index d59712c..221e2c2 100644 --- a/src/libffi.c +++ b/src/libffi.c @@ -8,6 +8,7 @@ #include "php_psi.h" #include "libffi.h" +#include "engine.h" #undef PACKAGE #undef PACKAGE_BUGREPORT @@ -42,8 +43,23 @@ static void *psi_ffi_closure_alloc(size_t s, void **code) } return *code; #else - return NULL; +# error "Neither ffi_closure_alloc() nor mmap() available" +#endif +} + +static ffi_status psi_ffi_prep_closure(ffi_closure **closure, void **code, ffi_cif *sig, void (*handler)(ffi_cif*,void*,void**,void*), void *data) { + *closure = psi_ffi_closure_alloc(sizeof(ffi_closure), code); + ZEND_ASSERT(*closure != NULL); + +#if PSI_HAVE_FFI_PREP_CLOSURE_LOC + return ffi_prep_closure_loc(*closure, sig, handler, data, *code); + +#elif PSI_HAVE_FFI_PREP_CLOSURE + return ffi_prep_closure(*code, sig, handler, data); +#else +# error "Neither ffi_prep_closure() nor ffi_prep_closure_loc() is available" #endif + } static void psi_ffi_closure_free(void *c) @@ -55,12 +71,113 @@ static void psi_ffi_closure_free(void *c) #endif } -static void psi_ffi_handler(ffi_cif *signature, void *_result, void **_args, void *_data); +static void psi_ffi_handler(ffi_cif *_sig, void *_result, void **_args, void *_data) +{ + psi_call(*(zend_execute_data **)_args[0], *(zval **)_args[1], _data); +} + +static void psi_ffi_callback(ffi_cif *_sig, void *_result, void **_args, void *_data) +{ + unsigned argc = _sig->nargs; + void **argv = _args; + ffi_arg *res = _result; + let_stmt *let; + decl_arg *darg = let->var->arg; + decl *decl_cb = darg->type->func; + let_callback *cb = let->val->data.callback; + impl_arg *iarg = cb->func->arg; + size_t i, argc = cb->args->count; + zval return_value, *argv = calloc(argc, sizeof(*argv)); + + // prepare args for the userland call + for (i = 0; i < decl_cb->args->count; ++i) { + + } + for (i = 0; i < cb->args->count; ++i) { + psi_do_set(&argv[i], cb->args->vals[i]); + } + zend_fcall_info_argp(iarg->val.zend.cb->fci, argc, argv); + zend_fcall_info_call(&iarg->val.zend.cb->fci, &iarg->val.zend.cb->fcc, + &return_value, NULL); + // marshal return value of the userland call + switch (cb->func->type) { + case PSI_T_BOOLVAL: + break; + case PSI_T_INTVAL: + break; + case PSI_T_FLOATVAL: + break; + case PSI_T_PATHVAL: + case PSI_T_STRVAL: + break; + case PSI_T_STRLEN: + break; + case PSI_T_ARRVAL: + break; + case PSI_T_OBJVAL: + break; + case PSI_T_CALLBACK: + break; + EMPTY_SWITCH_DEFAULT_CASE(); + } + darg->ptr = psi_let_val(cb->func->type, iarg, darg->ptr, real_decl_type(darg->type)->strct, &darg->mem); +} + static inline ffi_type *psi_ffi_decl_arg_type(decl_arg *darg); +typedef struct PSI_LibffiContext { + ffi_cif signature; + ffi_type *params[2]; +} PSI_LibffiContext; + +typedef struct PSI_LibffiCall { + void *code; + ffi_closure *closure; + ffi_cif signature; + void *params[1]; /* [type1, type2, NULL, arg1, arg2] ... */ +} PSI_LibffiCall; + static inline ffi_abi psi_ffi_abi(const char *convention) { return FFI_DEFAULT_ABI; } + +static inline PSI_LibffiCall *PSI_LibffiCallAlloc(PSI_Context *C, decl *decl) { + int rc; + size_t i, c = decl->args ? decl->args->count : 0; + PSI_LibffiCall *call = calloc(1, sizeof(*call) + 2 * c * sizeof(void *)); + + for (i = 0; i < c; ++i) { + call->params[i] = psi_ffi_decl_arg_type(decl->args->args[i]); + } + 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), + c, psi_ffi_decl_arg_type(decl->func), (ffi_type **) call->params); + ZEND_ASSERT(FFI_OK == rc); + + return call; +} + +static inline void PSI_LibffiCallInitClosure(PSI_Context *C, PSI_LibffiCall *call, impl *impl) { + PSI_LibffiContext *context = C->context; + ffi_status rc; + + rc = psi_ffi_prep_closure(&call->closure, &call->code, &context->signature, psi_ffi_handler, impl); + ZEND_ASSERT(FFI_OK == rc); +} + +static inline void PSI_LibffiCallFree(PSI_LibffiCall *call) { + if (call->closure) { + psi_ffi_closure_free(call->closure); + } + free(call); +} + static inline ffi_type *psi_ffi_token_type(token_t t) { switch (t) { default: @@ -87,6 +204,7 @@ static inline ffi_type *psi_ffi_token_type(token_t t) { case PSI_T_BOOL: return &ffi_type_uchar; case PSI_T_INT: + case PSI_T_ENUM: return &ffi_type_sint; case PSI_T_LONG: return &ffi_type_slong; @@ -94,7 +212,12 @@ static inline ffi_type *psi_ffi_token_type(token_t t) { return &ffi_type_float; case PSI_T_DOUBLE: return &ffi_type_double; +#ifdef HAVE_LONG_DOUBLE + case PSI_T_LONG_DOUBLE: + return &ffi_type_longdouble; +#endif case PSI_T_POINTER: + case PSI_T_FUNCTION: return &ffi_type_pointer; } } @@ -127,53 +250,58 @@ static void psi_ffi_struct_type_dtor(void *type) { free(strct); } +static size_t psi_ffi_struct_type_pad(ffi_type **els, size_t padding) { + size_t i; + + for (i = 0; i < padding; ++i) { + ffi_type *pad = malloc(sizeof(*pad)); + + memcpy(pad, &ffi_type_schar, sizeof(*pad)); + *els++ = pad; + } + + return padding; +} + static ffi_type **psi_ffi_struct_type_elements(decl_struct *strct) { - size_t i, j, argc = strct->args->count << 2, nels = 0, offset = 0, align, padding; + size_t i, argc = strct->args->count, nels = 0, offset = 0, maxalign = 0; ffi_type **els = calloc(argc + 1, sizeof(*els)); for (i = 0; i < strct->args->count; ++i) { decl_arg *darg = strct->args->args[i]; ffi_type *type = malloc(sizeof(*type)); + size_t padding; memcpy(type, psi_ffi_decl_arg_type(darg), sizeof(*type)); - if (darg->layout->pos > offset) { - padding = darg->layout->pos - offset; - align = ((padding - 1) | (type->alignment - 1)) + 1; - if (align >= padding) { - padding = 0; - } - } else { - align = 0; - padding = 0; - } + ZEND_ASSERT(type->size == darg->layout->len); - if (padding) { - for (j = 0; j < padding; ++j) { - ffi_type *pad = malloc(sizeof(*pad)); + if (type->alignment > maxalign) { + maxalign = type->alignment; + } - ZEND_ASSERT(nels + 1 < argc); - memcpy(pad, &ffi_type_schar, sizeof(*pad)); - els[nels++] = pad; + if ((padding = psi_offset_padding(darg->layout->pos - offset, type->alignment))) { + if (nels + padding + 1 > argc) { + argc += padding; + els = realloc(els, (argc + 1) * sizeof(*els)); + els[argc] = NULL; } + psi_ffi_struct_type_pad(&els[nels], padding); + nels += padding; + offset += padding; } + ZEND_ASSERT(offset == darg->layout->pos); - ZEND_ASSERT(nels + 1 < argc); + offset = (offset + darg->layout->len + type->alignment - 1) & ~(type->alignment - 1); els[nels++] = type; -//fprintf(stderr, "%s o:%d, a:%d, p:%d l:%d\n", darg->var->name, offset, align, padding, darg->layout->len); - offset += MAX(align, padding) + darg->layout->len; } -//fprintf(stderr, "%s s:%d o=%d\n", strct->name, strct->size, offset); + + /* apply struct alignment padding */ + offset = (offset + maxalign - 1) & ~(maxalign - 1); + ZEND_ASSERT(offset <= strct->size); if (offset < strct->size) { - padding = strct->size - offset; - for (j = 0; j < padding; ++j) { - ffi_type *pad = malloc(sizeof(*pad)); - - ZEND_ASSERT(nels + 1 < argc); - memcpy(pad, &ffi_type_schar, sizeof(*pad)); - els[nels++] = pad; - } + psi_ffi_struct_type_pad(&els[nels], strct->size - offset); } return els; @@ -181,12 +309,28 @@ static ffi_type **psi_ffi_struct_type_elements(decl_struct *strct) { static inline ffi_type *psi_ffi_decl_type(decl_type *type) { decl_type *real = real_decl_type(type); - if (real->type == PSI_T_STRUCT) { + switch (real->type) { + case PSI_T_FUNCTION: + if (!real->func->call.sym) { + PSI_LibffiCall *call = PSI_LibffiCallAlloc(&PSI_G(context), real->func); + ffi_status rc; + + rc = psi_ffi_prep_closure( + (void *) &real->func->call.closure.data, + &real->func->call.sym, &call->signature, psi_ffi_handler, NULL); + if (FFI_OK == rc) { + real->func->call.info = call; + real->func->call.closure.dtor = psi_ffi_closure_free; + } + } + return &ffi_type_pointer; + + case PSI_T_STRUCT: if (!real->strct->engine.type) { ffi_type *strct = calloc(1, sizeof(ffi_type)); strct->type = FFI_TYPE_STRUCT; - strct->size = 0;//real->strct->size; + strct->size = 0; strct->elements = psi_ffi_struct_type_elements(real->strct); real->strct->engine.type = strct; @@ -194,8 +338,13 @@ static inline ffi_type *psi_ffi_decl_type(decl_type *type) { } return real->strct->engine.type; + + case PSI_T_UNION: + return psi_ffi_decl_arg_type(real->unn->args->args[0]); + + default: + return psi_ffi_token_type(real->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) { @@ -205,69 +354,6 @@ static inline ffi_type *psi_ffi_decl_arg_type(decl_arg *darg) { } } -typedef struct PSI_LibffiContext { - ffi_cif signature; - ffi_type *params[2]; -} PSI_LibffiContext; - -typedef struct PSI_LibffiCall { - void *code; - ffi_closure *closure; - ffi_cif signature; - void *params[1]; /* [type1, type2, NULL, arg1, arg2] ... */ -} PSI_LibffiCall; - -static inline PSI_LibffiCall *PSI_LibffiCallAlloc(PSI_Context *C, decl *decl) { - int rc; - size_t i, c = decl->args ? decl->args->count : 0; - PSI_LibffiCall *call = calloc(1, sizeof(*call) + 2 * c * sizeof(void *)); - - for (i = 0; i < c; ++i) { - call->params[i] = psi_ffi_decl_arg_type(decl->args->args[i]); - } - 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), - c, psi_ffi_decl_arg_type(decl->func), (ffi_type **) call->params); - ZEND_ASSERT(FFI_OK == rc); - - return call; -} - -static inline void PSI_LibffiCallInitClosure(PSI_Context *C, PSI_LibffiCall *call, impl *impl) { - PSI_LibffiContext *context = C->context; - int rc; - - call->closure = psi_ffi_closure_alloc(sizeof(ffi_closure), &call->code); - ZEND_ASSERT(call->closure != NULL); - -#if PSI_HAVE_FFI_PREP_CLOSURE_LOC - rc = ffi_prep_closure_loc( - call->closure, - &context->signature, - psi_ffi_handler, - impl, - call->code); - -#elif PSI_HAVE_FFI_PREP_CLOSURE - rc = ffi_prep_closure(call->code, &context->signature, psi_ffi_handler, impl); -#else -# error "Neither ffi_prep_closure() nor ffi_prep_closure_loc() available" -#endif - ZEND_ASSERT(FFI_OK == rc); -} - -static inline void PSI_LibffiCallFree(PSI_LibffiCall *call) { - if (call->closure) { - psi_ffi_closure_free(call->closure); - } - free(call); -} static inline PSI_LibffiContext *PSI_LibffiContextInit(PSI_LibffiContext *L) { ffi_status rc; @@ -285,11 +371,6 @@ static inline PSI_LibffiContext *PSI_LibffiContextInit(PSI_LibffiContext *L) { return L; } -static void psi_ffi_handler(ffi_cif *_sig, void *_result, void **_args, void *_data) -{ - psi_call(*(zend_execute_data **)_args[0], *(zval **)_args[1], _data); -} - static void psi_ffi_init(PSI_Context *C) { C->context = PSI_LibffiContextInit(NULL);