X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-psi;a=blobdiff_plain;f=src%2Flibffi.c;h=6f808d4ce2360c4d1ba49079682e247a6fa72417;hp=1569693ee559e883e0c573de46f39124dc768281;hb=14969a8b4386d7c38e90775e1c57886c9bf3839e;hpb=440ff658995f1378fd74c0101ff6c2b4951ffdf9 diff --git a/src/libffi.c b/src/libffi.c index 1569693..6f808d4 100644 --- a/src/libffi.c +++ b/src/libffi.c @@ -30,84 +30,61 @@ #ifdef HAVE_LIBFFI -#undef PACKAGE -#undef PACKAGE_BUGREPORT -#undef PACKAGE_NAME -#undef PACKAGE_STRING -#undef PACKAGE_TARNAME -#undef PACKAGE_VERSION - -#include - -#ifndef PSI_HAVE_FFI_CLOSURE_ALLOC -# if HAVE_UNISTD_H -# include -# endif -# if HAVE_SYS_MMAN_H -# include -# ifndef MAP_ANONYMOUS -# define MAP_ANONYMOUS MAP_ANON -# endif -# endif -#endif +#include "libffi_compat.h" -static void *psi_ffi_closure_alloc(size_t s, void **code) -{ -#ifdef PSI_HAVE_FFI_CLOSURE_ALLOC - return ffi_closure_alloc(s, code); -#elif HAVE_MMAP - *code = mmap(NULL, s, PROT_EXEC|PROT_WRITE|PROT_READ, - MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); - if (MAP_FAILED == *code) { - return NULL; - } - return *code; -#else -# error "Neither ffi_closure_alloc() nor mmap() available" +#if HAVE_INT128 +static ffi_type *ffi_type_sint128; +static ffi_type *ffi_type_uint128; #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); - assert(*closure != NULL); +struct psi_ffi_context { + ffi_cif signature; + ffi_type *params[2]; +}; -#if PSI_HAVE_FFI_PREP_CLOSURE_LOC - return ffi_prep_closure_loc(*closure, sig, handler, data, *code); +struct psi_ffi_callback_info { + struct psi_ffi_impl_info *impl_info; + struct psi_let_exp *let_exp; -#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 -} + void *code; + ffi_closure *closure; +}; -static void psi_ffi_closure_free(void *c) -{ -#ifdef PSI_HAVE_FFI_CLOSURE_ALLOC - ffi_closure_free(c); -#elif HAVE_MMAP - munmap(c, sizeof(ffi_closure)); -#endif -} +struct psi_ffi_decl_info { + ffi_cif signature; + struct psi_ffi_struct_info *rv_array; + ffi_type *params[1]; +}; -static void psi_ffi_prep_va(ffi_cif *base, ffi_cif *signature, size_t argc, size_t va_count, - ffi_type **param_types) { - ffi_status rc; +struct psi_ffi_extvar_info { + struct { + ffi_cif signature; + void *code; + ffi_closure *closure; + } get; + struct { + ffi_cif signature; + ffi_type *params[1]; + void *code; + ffi_closure *closure; + } set; +}; -#ifdef PSI_HAVE_FFI_PREP_CIF_VAR - rc = ffi_prep_cif_var(signature, base->abi, argc, argc + va_count, - base->rtype, param_types); -#else - /* FIXME: test in config.m4; assume we can just call anyway */ - rc = ffi_prep_cif(signature, base->abi, argc + va_count, base->rtype, param_types); -#endif +struct psi_ffi_impl_info { + struct psi_context *context; + struct psi_call_frame *frame; - assert(FFI_OK == rc); -} + void *code; + ffi_closure *closure; +}; -static inline ffi_type *psi_ffi_decl_arg_type(struct psi_decl_arg *darg); +struct psi_ffi_struct_info { + ffi_type strct; + struct psi_plist *eles; +}; -static inline ffi_type *psi_ffi_token_type(token_t t) { +static inline ffi_type *psi_ffi_token_type(token_t t) +{ switch (t) { default: assert(0); @@ -130,6 +107,12 @@ static inline ffi_type *psi_ffi_token_type(token_t t) { return &ffi_type_sint64; case PSI_T_UINT64: return &ffi_type_uint64; +#if HAVE_INT128 + case PSI_T_INT128: + return ffi_type_sint128; + case PSI_T_UINT128: + return ffi_type_uint128; +#endif case PSI_T_BOOL: return &ffi_type_uchar; case PSI_T_ENUM: @@ -147,7 +130,8 @@ static inline ffi_type *psi_ffi_token_type(token_t t) { return &ffi_type_pointer; } } -static inline ffi_type *psi_ffi_impl_type(token_t impl_type) { +static inline ffi_type *psi_ffi_impl_type(token_t impl_type) +{ switch (impl_type) { case PSI_T_BOOL: return &ffi_type_sint8; @@ -162,360 +146,302 @@ static inline ffi_type *psi_ffi_impl_type(token_t impl_type) { } return NULL; } -static void psi_ffi_struct_type_dtor(void *type) { - ffi_type *strct = type; - if (strct->elements) { - ffi_type **ptr; +static void psi_ffi_type_free(ffi_type **typ_ptr) +{ + pefree(*typ_ptr, 1); +} - for (ptr = strct->elements; *ptr; ++ptr) { - free(*ptr); +static inline ffi_abi psi_ffi_abi(zend_string *convention) { + if (FFI_LAST_ABI - 2 != FFI_FIRST_ABI) { +#ifdef HAVE_FFI_STDCALL + if (zend_string_equals_literal(convention, "stdcall")) { + return FFI_STDCALL; + } +#endif +#ifdef HAVE_FFI_FASTCALL + if (zend_string_equals_literal(convention, "fastcall")) { + return FFI_FASTCALL; } - free(strct->elements); +#endif } - free(strct); + return FFI_DEFAULT_ABI; } -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; - } +static void psi_ffi_handler(ffi_cif *sig, void *result, void **args, void *data) +{ + struct psi_impl *impl = data; + struct psi_ffi_impl_info *info = impl->info; - return padding; + psi_context_call(info->context, *(zend_execute_data **)args[0], *(zval **)args[1], impl); } -static ffi_type **psi_ffi_struct_type_elements(struct psi_decl_struct *strct) { - size_t i = 0, argc, nels = 0, offset = 0, maxalign = 0, last_arg_pos = -1; - ffi_type **tmp, **els; - struct psi_decl_arg *darg; - - argc = psi_plist_count(strct->args); - els = calloc(argc + 1, sizeof(*els)); - - while (psi_plist_get(strct->args, i++, &darg)) { - ffi_type *type; - size_t padding; +static void psi_ffi_callback(ffi_cif *sig, void *result, void **args, void *data) +{ + struct psi_ffi_callback_info *cb_info = data; + struct psi_call_frame_callback cb_data; - if (darg->layout->pos == last_arg_pos) { - /* skip bit fields */ - continue; - } - last_arg_pos = darg->layout->pos; + assert(cb_info->impl_info->frame); - type = malloc(sizeof(*type)); - *type = *psi_ffi_decl_arg_type(darg); + cb_data.cb = cb_info->let_exp; + cb_data.argc = sig->nargs; + cb_data.argv = args; + cb_data.rval = result; - if (type->alignment > maxalign) { - maxalign = type->alignment; - } + psi_call_frame_do_callback(cb_info->impl_info->frame, &cb_data); +} - assert(type->size <= darg->layout->len); - if ((padding = psi_offset_padding(darg->layout->pos - offset, type->alignment))) { - if (nels + padding + 1 > argc) { - argc += padding; - tmp = realloc(els, (argc + 1) * sizeof(*els)); - if (tmp) { - els = tmp; - } else { - free(els); - return NULL; - } - els[argc] = NULL; - } - psi_ffi_struct_type_pad(&els[nels], padding); - nels += padding; - offset += padding; - } - assert(offset == darg->layout->pos); +static bool psi_ffi_load() +{ +#if HAVE_INT128 + ffi_type *i128, *u128; + + i128 = pecalloc(1, 3*sizeof(ffi_type), 1); + i128->type = FFI_TYPE_STRUCT; + i128->size = 0; + i128->elements = (ffi_type **) (i128 + 1); + i128->elements[0] = &ffi_type_sint64; + i128->elements[1] = &ffi_type_sint64; + + ffi_type_sint128 = i128; + + u128 = pecalloc(1, 3*sizeof(ffi_type), 1); + u128->type = FFI_TYPE_STRUCT; + u128->size = 0; + u128->elements = (ffi_type **) (u128 + 1); + u128->elements[0] = &ffi_type_uint64; + u128->elements[1] = &ffi_type_uint64; + + ffi_type_uint128 = u128; +#endif + return true; +} - offset = (offset + darg->layout->len + type->alignment - 1) & ~(type->alignment - 1); - els[nels++] = type; - } +static void psi_ffi_free() +{ +#if HAVE_INT128 + free(ffi_type_sint128); + free(ffi_type_uint128); +#endif +} - /* apply struct alignment padding */ - offset = (offset + maxalign - 1) & ~(maxalign - 1); +static bool psi_ffi_init(struct psi_context *C) +{ + ffi_status rc; + struct psi_ffi_context *context = pecalloc(1, sizeof(*context), 1); - assert(offset <= strct->size); - if (offset < strct->size) { - size_t padding = strct->size - offset; + context->params[0] = &ffi_type_pointer; + context->params[1] = &ffi_type_pointer; + rc = ffi_prep_cif(&context->signature, FFI_DEFAULT_ABI, 2, &ffi_type_void, + context->params); - tmp = realloc(els, (padding + argc + 1) * sizeof(*els)); - if (tmp) { - els = tmp; - } else { - free(els); - return NULL; - } - psi_ffi_struct_type_pad(&els[nels], padding); - els[argc + padding] = NULL; + if (FFI_OK != rc) { + pefree(context, 1); + return false; } - return els; + C->context = context; + return true; } -static inline ffi_type *psi_ffi_decl_type(struct psi_decl_type *type) { - struct psi_decl_type *real = psi_decl_type_get_real(type); - switch (real->type) { - case PSI_T_STRUCT: - if (!real->real.strct->engine.type) { - ffi_type *strct = calloc(1, sizeof(ffi_type)); +static void psi_ffi_dtor(struct psi_context *C) +{ + if (C->context) { + pefree(C->context, 1); + C->context = NULL; + } +} - strct->type = FFI_TYPE_STRUCT; - strct->size = 0; - strct->elements = psi_ffi_struct_type_elements(real->real.strct); +static bool psi_ffi_composite_init(struct psi_context *C, + struct psi_decl_arg *darg) +{ + struct psi_ffi_struct_info *info; - real->real.strct->engine.type = strct; - real->real.strct->engine.dtor = psi_ffi_struct_type_dtor; - } + if (darg->engine.type) { + return true; + } - return real->real.strct->engine.type; + info = pecalloc(1, sizeof(*info), 1); + info->eles = psi_plist_init((psi_plist_dtor) psi_ffi_type_free); - case PSI_T_UNION: - { - struct psi_decl_arg *arg; - psi_plist_get(real->real.unn->args, 0, &arg); - return psi_ffi_decl_arg_type(arg); - } + psi_context_composite_type_elements(C, darg, &info->eles); - default: - return psi_ffi_token_type(real->type); - } -} -static inline ffi_type *psi_ffi_decl_arg_type(struct psi_decl_arg *darg) { - if (darg->var->pointer_level) { - return &ffi_type_pointer; - } else { - return psi_ffi_decl_type(darg->type); + /* add terminating NULL; libffi structs do not have an element count */ + { + void *null_ptr = NULL; + info->eles = psi_plist_add(info->eles, &null_ptr); } -} -static inline ffi_abi psi_ffi_abi(const char *convention) { - if (FFI_LAST_ABI - 2 != FFI_FIRST_ABI) { -#ifdef HAVE_FFI_STDCALL - if (!strcasecmp(convention, "stdcall")) { - return FFI_STDCALL; - } -#endif -#ifdef HAVE_FFI_FASTCALL - if (!strcasecmp(convention, "fastcall")) { - return FFI_FASTCALL; - } -#endif - } - return FFI_DEFAULT_ABI; -} - -struct psi_ffi_context { - ffi_cif signature; - ffi_type *params[2]; -}; - -struct psi_ffi_impl_info { - struct psi_context *context; - struct psi_call_frame *frame; + info->strct.type = FFI_TYPE_STRUCT; + info->strct.alignment = 0; + info->strct.size = 0; + info->strct.elements = (ffi_type **) psi_plist_eles(info->eles); - void *code; - ffi_closure *closure; -}; + darg->engine.info = info; + darg->engine.type = &info->strct; -struct psi_ffi_callback_info { - struct psi_ffi_impl_info *impl_info; - struct psi_let_exp *let_exp; + return true; +} - void *code; - ffi_closure *closure; -}; +static void psi_ffi_composite_dtor(struct psi_context *C, + struct psi_decl_arg *darg) +{ + struct psi_ffi_struct_info *info = darg->engine.info; -struct psi_ffi_decl_info { - ffi_cif signature; - ffi_type *params[1]; -}; + if (info) { + darg->engine.info = NULL; + darg->engine.type = NULL; -static inline struct psi_ffi_decl_info *psi_ffi_decl_init(struct psi_decl *decl) { - if (!decl->info) { - int rc; - size_t i, c = psi_plist_count(decl->args); - struct psi_decl_arg *arg; - struct psi_ffi_decl_info *info = calloc(1, sizeof(*info) + 2 * c * sizeof(void *)); + struct psi_plist *args = NULL; + struct psi_decl_type *dtype = psi_decl_type_get_real(darg->type); - for (i = 0; psi_plist_get(decl->args, i, &arg); ++i) { - info->params[i] = psi_ffi_decl_arg_type(arg); + if (dtype->type == PSI_T_STRUCT) { + args = dtype->real.strct->args; + } else if (dtype->type == PSI_T_UNION) { + args = dtype->real.unn->args; } - info->params[c] = NULL; - rc = ffi_prep_cif(&info->signature, psi_ffi_abi(decl->abi->convention), - c, psi_ffi_decl_arg_type(decl->func), info->params); + size_t i = 0; + struct psi_decl_arg *tmp; - if (FFI_OK != rc) { - free(info); - } else { - decl->info = info; + while (psi_plist_get(args, i++, &tmp)) { + psi_ffi_composite_dtor(C, tmp); } - } - return decl->info; -} - -static inline void psi_ffi_decl_dtor(struct psi_decl *decl) { - if (decl->info) { - free(decl->info); - decl->info = NULL; + psi_plist_free(info->eles); + pefree(info, 1); } } -static void psi_ffi_handler(ffi_cif *sig, void *result, void **args, void *data) -{ - struct psi_impl *impl = data; - struct psi_ffi_impl_info *info = impl->info; +static void psi_ffi_extvar_get(ffi_cif *sig, void *result, void **args, void *data) { + struct psi_decl_extvar *evar = data; - psi_context_call(info->context, *(zend_execute_data **)args[0], *(zval **)args[1], impl); + psi_decl_extvar_get(evar, result); } -static void psi_ffi_callback(ffi_cif *sig, void *result, void **args, void *data) -{ - struct psi_ffi_callback_info *cb_info = data; - struct psi_call_frame_callback cb_data; - - assert(cb_info->impl_info->frame); - - cb_data.cb = cb_info->let_exp; - cb_data.argc = sig->nargs; - cb_data.argv = args; - cb_data.rval = result; +static void psi_ffi_extvar_set(ffi_cif *sig, void *result, void **args, void *data) { + struct psi_decl_extvar *evar = data; - psi_call_frame_do_callback(cb_info->impl_info->frame, &cb_data); + psi_decl_extvar_set(evar, args[0]); } -static inline void psi_ffi_callback_init(struct psi_ffi_impl_info *impl_info, - struct psi_let_exp *let_exp) { - struct psi_ffi_callback_info *cb_info; - struct psi_ffi_decl_info *decl_info; - struct psi_let_callback *cb; - struct psi_let_func *fn = NULL; +static bool psi_ffi_decl_init(struct psi_context *, struct psi_decl *); + +static bool psi_ffi_extvar_init(struct psi_context *C, + struct psi_decl_extvar *evar) +{ + struct psi_ffi_extvar_info *info = pecalloc(1, sizeof(*info), 1); ffi_status rc; - switch (let_exp->kind) { - case PSI_LET_CALLBACK: - cb = let_exp->data.callback; - if (cb->decl->info) { - decl_info = cb->decl->info; - } else { - decl_info = psi_ffi_decl_init(cb->decl); - } + evar->info = info; - cb_info = calloc(1, sizeof(*cb_info)); - cb_info->impl_info = impl_info; - cb_info->let_exp = let_exp; - rc = psi_ffi_prep_closure(&cb_info->closure, &cb_info->code, - &decl_info->signature, psi_ffi_callback, cb_info); + psi_ffi_decl_init(C, evar->getter); + psi_ffi_decl_init(C, evar->setter); - if (FFI_OK != rc) { - free(cb_info); - break; - } - cb->info = cb_info; + rc = ffi_prep_cif(&info->get.signature, FFI_DEFAULT_ABI, 0, + psi_context_decl_arg_call_type(C, evar->getter->func), NULL); + if (FFI_OK != rc) { + return false; + } + rc = psi_ffi_prep_closure(&info->get.closure, &info->get.code, + &info->get.signature, psi_ffi_extvar_get, evar); + if (FFI_OK != rc) { + return false; + } - assert(!cb->decl->sym); - cb->decl->sym = cb_info->code; - fn = cb->func; - /* no break */ + info->set.params[0] = psi_context_decl_arg_call_type(C, evar->arg); + rc = ffi_prep_cif(&info->set.signature, FFI_DEFAULT_ABI, 1, + &ffi_type_void, info->set.params); + if (FFI_OK != rc) { + return false; + } + rc = psi_ffi_prep_closure(&info->set.closure, &info->set.code, + &info->set.signature, psi_ffi_extvar_set, evar); + if (FFI_OK != rc) { + return false; + } - case PSI_LET_FUNC: - if (!fn) { - fn = let_exp->data.func; - } - if (fn->inner) { - size_t i = 0; - struct psi_let_exp *inner_let; + evar->getter->sym = info->get.code; + evar->setter->sym = info->set.code; - while (psi_plist_get(fn->inner, i++, &inner_let)) { - psi_ffi_callback_init(impl_info, inner_let); - } - } - break; - default: - break; + return true; +} + +static void psi_ffi_extvar_dtor(struct psi_context *C, + struct psi_decl_extvar *evar) { + if (evar->info) { + pefree(evar->info, 1); + evar->info = NULL; } } -static inline void psi_ffi_callback_dtor(struct psi_let_exp *let_exp) { - struct psi_let_callback *cb; - struct psi_let_func *fn = NULL; +static bool psi_ffi_decl_init(struct psi_context *C, struct psi_decl *decl) +{ + if (!decl->info) { + int rc; + size_t i, c = psi_plist_count(decl->args); + struct psi_decl_arg *arg; + struct psi_ffi_decl_info *info = pecalloc(1, + sizeof(*info) + 2 * c * sizeof(void *), 1); - switch (let_exp->kind) { - case PSI_LET_CALLBACK: - cb = let_exp->data.callback; + decl->info = info; - psi_ffi_decl_dtor(cb->decl); + for (i = 0; psi_plist_get(decl->args, i, &arg); ++i) { + info->params[i] = psi_context_decl_arg_call_type(C, arg); + } + info->params[c] = NULL; - if (cb->info) { - struct psi_ffi_callback_info *info = cb->info; + rc = ffi_prep_cif(&info->signature, psi_ffi_abi(decl->abi->convention), + c, psi_context_decl_arg_call_type(C, decl->func), info->params); - if (info->closure) { - psi_ffi_closure_free(info->closure); - } - free(info); - cb->info = NULL; - } - fn = cb->func; - /* no break */ - case PSI_LET_FUNC: - if (!fn) { - fn = let_exp->data.func; + if (FFI_OK != rc) { + pefree(info, 1); + decl->info = NULL; + return false; } + } - if (fn->inner) { - size_t i = 0; - struct psi_let_exp *cb; + return true; +} - while (psi_plist_get(fn->inner, i++, &cb)) { - psi_ffi_callback_dtor(cb); - } - } - break; - default: - break; +static void psi_ffi_decl_dtor(struct psi_context *C, + struct psi_decl *decl) +{ + if (decl->info) { + pefree(decl->info, 1); + decl->info = NULL; } } -static inline struct psi_ffi_impl_info *psi_ffi_impl_init(struct psi_impl *impl, - struct psi_context *C) { +static bool psi_ffi_impl_init(struct psi_context *C, + struct psi_impl *impl, zif_handler *zh) +{ struct psi_ffi_context *context = C->context; - struct psi_ffi_impl_info *info = calloc(1, sizeof(*info)); - struct psi_let_stmt *let; + struct psi_ffi_impl_info *info = pecalloc(1, sizeof(*info), 1); ffi_status rc; - size_t l = 0; + impl->info = info; info->context = C; rc = psi_ffi_prep_closure(&info->closure, &info->code, &context->signature, psi_ffi_handler, impl); if (FFI_OK != rc) { - free(info); - return NULL; + goto failure; } - while (psi_plist_get(impl->stmts.let, l++, &let)) { - psi_ffi_callback_init(info, let->exp); - } + *zh = info->code; + return true; - return impl->info = info; +failure: ; + impl->info = NULL; + free(info); + return false; } -static inline void psi_ffi_impl_dtor(struct psi_impl *impl) { +static void psi_ffi_impl_dtor(struct psi_context *C, struct psi_impl *impl) +{ struct psi_ffi_impl_info *info = impl->info; - struct psi_let_stmt *let; - size_t j = 0; - - while (psi_plist_get(impl->stmts.let, j++, &let)) { - psi_ffi_callback_dtor(let->exp); - } if (info) { if (info->closure) { @@ -526,103 +452,58 @@ static inline void psi_ffi_impl_dtor(struct psi_impl *impl) { } } - -static inline struct psi_ffi_context *psi_ffi_context_init(struct psi_ffi_context *L) { +static bool psi_ffi_cb_init(struct psi_context *C, + struct psi_let_exp *exp, struct psi_impl *impl) +{ + struct psi_ffi_callback_info *cb_info; + struct psi_ffi_decl_info *decl_info; ffi_status rc; - if (!L) { - L = malloc(sizeof(*L)); - } - memset(L, 0, sizeof(*L)); - - L->params[0] = &ffi_type_pointer; - L->params[1] = &ffi_type_pointer; - rc = ffi_prep_cif(&L->signature, FFI_DEFAULT_ABI, 2, &ffi_type_void, L->params); - assert(rc == FFI_OK); - - return L; -} + assert(exp->kind == PSI_LET_CALLBACK); -static inline void psi_ffi_context_free(struct psi_ffi_context **L) { - if (*L) { - free(*L); - *L = NULL; + if (!psi_ffi_decl_init(C, exp->data.callback->decl)) { + return false; } -} -static void psi_ffi_init(struct psi_context *C) -{ - C->context = psi_ffi_context_init(NULL); -} - -static void psi_ffi_dtor(struct psi_context *C) -{ - if (C->decls) { - size_t i = 0; - struct psi_decl *decl; + cb_info = pecalloc(1, sizeof(*cb_info), 1); + cb_info->impl_info = impl->info; + cb_info->let_exp = exp; - while (psi_plist_get(C->decls, i++, &decl)) { - psi_ffi_decl_dtor(decl); - } + decl_info = exp->data.callback->decl->info; + rc = psi_ffi_prep_closure(&cb_info->closure, &cb_info->code, + &decl_info->signature, psi_ffi_callback, cb_info); + if (FFI_OK != rc) { + free(cb_info); + return false; } - if (C->impls) { - size_t i = 0; - struct psi_impl *impl; - while (psi_plist_get(C->impls, i++, &impl)) { - psi_ffi_impl_dtor(impl); - } - } - psi_ffi_context_free((void *) &C->context); -} + assert(!exp->data.callback->decl->sym); + exp->data.callback->info = cb_info; + exp->data.callback->decl->sym = cb_info->code; + return true; +} -static zend_function_entry *psi_ffi_compile(struct psi_context *C) +static void psi_ffi_cb_dtor(struct psi_context *C, + struct psi_let_exp *let_exp, struct psi_impl *impl) { - size_t i = 0, d = 0, nf = 0; - struct psi_impl *impl; - struct psi_decl *decl; - zend_function_entry *zfe; - - if (!C->impls) { - return NULL; - } - - zfe = calloc(psi_plist_count(C->impls) + 1, sizeof(*zfe)); + assert(let_exp->kind == PSI_LET_CALLBACK); - while (psi_plist_get(C->impls, i++, &impl)) { - zend_function_entry *zf = &zfe[nf]; + psi_ffi_decl_dtor(C, let_exp->data.callback->decl); - if (!impl->decl) { - continue; - } - if (!psi_ffi_decl_init(impl->decl)) { - continue; - } - if (!psi_ffi_impl_init(impl, C)) { - continue; - } - - zf->fname = impl->func->name + (impl->func->name[0] == '\\'); - zf->handler = ((struct psi_ffi_impl_info *) impl->info)->code; - zf->num_args = psi_plist_count(impl->func->args); - zf->arg_info = psi_internal_arginfo(impl); - ++nf; - } + if (let_exp->data.callback->info) { + struct psi_ffi_callback_info *info = let_exp->data.callback->info; - while (psi_plist_get(C->decls, d++, &decl)) { - if (decl->info) { - continue; + if (info->closure) { + psi_ffi_closure_free(info->closure); } - - psi_ffi_decl_init(decl); + pefree(info, 1); + let_exp->data.callback->info = NULL; } - - return zfe; } -static inline void psi_ffi_call_ex(struct psi_call_frame *frame) { +static void psi_ffi_call(struct psi_call_frame *frame) { struct psi_decl *decl = psi_call_frame_get_decl(frame); struct psi_impl *impl = psi_call_frame_get_impl(frame); struct psi_ffi_decl_info *decl_info = decl->info; @@ -642,7 +523,7 @@ static inline void psi_ffi_call_ex(struct psi_call_frame *frame) { } } -static inline void psi_ffi_call_va(struct psi_call_frame *frame) { +static void psi_ffi_call_va(struct psi_call_frame *frame) { ffi_cif signature; struct psi_call_frame *prev; struct psi_decl *decl = psi_call_frame_get_decl(frame); @@ -680,30 +561,60 @@ static inline void psi_ffi_call_va(struct psi_call_frame *frame) { efree(param_types); } -static void psi_ffi_call(struct psi_call_frame *frame) { - if (psi_call_frame_num_var_args(frame)) { - psi_ffi_call_va(frame); - } else { - psi_ffi_call_ex(frame); - } +static void *psi_ffi_typeof_impl(struct psi_context *C, token_t impl_type) +{ + return psi_ffi_impl_type(impl_type); +} + +static void *psi_ffi_typeof_decl(struct psi_context *C, token_t decl_type) +{ + return psi_ffi_token_type(decl_type); } -static void *psi_ffi_query(struct psi_context *C, enum psi_context_query q, void *arg) { - switch (q) { - case PSI_CONTEXT_QUERY_SELF: - return "ffi"; - case PSI_CONTEXT_QUERY_TYPE: - return psi_ffi_impl_type(*(token_t *) arg); +static void *psi_ffi_copyof_type(struct psi_context *C, void *orig_type) +{ + ffi_type *type = pemalloc(sizeof(*type), 1); + + *type = *(ffi_type *) orig_type; + return type; +} + +static void psi_ffi_layoutof_type(struct psi_context *C, void *orig_type, + struct psi_layout *l) +{ + ffi_type *type = orig_type; + + if (!type->size || !type->alignment) { + ffi_cif tmp; + ffi_prep_cif(&tmp, FFI_DEFAULT_ABI, 0, type, NULL); } - return NULL; + + l->pos = type->alignment; + l->len = type->size; } static struct psi_context_ops ops = { + "libffi", + psi_ffi_load, + psi_ffi_free, psi_ffi_init, psi_ffi_dtor, - psi_ffi_compile, + psi_ffi_composite_init, + psi_ffi_composite_dtor, + psi_ffi_extvar_init, + psi_ffi_extvar_dtor, + psi_ffi_decl_init, + psi_ffi_decl_dtor, + psi_ffi_impl_init, + psi_ffi_impl_dtor, + psi_ffi_cb_init, + psi_ffi_cb_dtor, psi_ffi_call, - psi_ffi_query, + psi_ffi_call_va, + psi_ffi_typeof_impl, + psi_ffi_typeof_decl, + psi_ffi_copyof_type, + psi_ffi_layoutof_type, }; struct psi_context_ops *psi_libffi_ops(void)