X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-psi;a=blobdiff_plain;f=src%2Flibffi.c;h=644b0fed5d8b6d70f64dd34205d62081653b34cd;hp=78b02391f6cbcd3b4adcfb725341d4fe86e5b932;hb=370510d04a5e22140e1e7f90f5d1f0002e6dbee6;hpb=c264e2866e9d509e4ed73e2542e80d4c7fb0a92b diff --git a/src/libffi.c b/src/libffi.c index 78b0239..644b0fe 100644 --- a/src/libffi.c +++ b/src/libffi.c @@ -1,15 +1,35 @@ -#ifdef HAVE_CONFIG_H -# include "config.h" -#endif - +/******************************************************************************* + Copyright (c) 2016, Michael Wallner . + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#include "php_psi_stdinc.h" +#include "context.h" +#include "call.h" #include "php.h" #ifdef HAVE_LIBFFI -#include "php_psi.h" -#include "libffi.h" -#include "engine.h" - #undef PACKAGE #undef PACKAGE_BUGREPORT #undef PACKAGE_NAME @@ -49,7 +69,7 @@ static void *psi_ffi_closure_alloc(size_t s, void **code) 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); + assert(*closure != NULL); #if PSI_HAVE_FFI_PREP_CLOSURE_LOC return ffi_prep_closure_loc(*closure, sig, handler, data, *code); @@ -59,7 +79,6 @@ static ffi_status psi_ffi_prep_closure(ffi_closure **closure, void **code, ffi_c #else # error "Neither ffi_prep_closure() nor ffi_prep_closure_loc() is available" #endif - } static void psi_ffi_closure_free(void *c) @@ -71,110 +90,124 @@ static void psi_ffi_closure_free(void *c) #endif } -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) -{ - size_t i; - unsigned argc = _sig->nargs; - void **argv = _args; - let_callback *cb = _data; - decl *decl_cb = cb->decl; - impl_arg *iarg = cb->func->var->arg; - zval return_value, *zargv = calloc(argc, sizeof(*zargv)); - void *result, *to_free = NULL; - - ZEND_ASSERT(argc == cb->decl->args->count); - - /* prepare args for the userland call */ - for (i = 0; i < argc; ++i) { - cb->decl->args->args[i]->let = argv[i]; - } - for (i = 0; i < cb->args->count; ++i) { - psi_do_set(&zargv[i], cb->args->vals[i]); - } - zend_fcall_info_argp(&iarg->val.zend.cb->fci, cb->args->count, zargv); - - /* callback into userland */ - ZVAL_UNDEF(&return_value); - iarg->_zv = &return_value; - zend_fcall_info_call(&iarg->val.zend.cb->fci, &iarg->val.zend.cb->fcc, iarg->_zv, NULL); - - /* marshal return value of the userland call */ - switch (iarg->type->type) { - case PSI_T_BOOL: zend_parse_arg_bool(iarg->_zv, &iarg->val.zend.bval, NULL, 0); break; - case PSI_T_LONG: zend_parse_arg_long(iarg->_zv, &iarg->val.zend.lval, NULL, 0, 1); break; - case PSI_T_FLOAT: - case PSI_T_DOUBLE: zend_parse_arg_double(iarg->_zv, &iarg->val.dval, NULL, 0); break; - case PSI_T_STRING: zend_parse_arg_str(iarg->_zv, &iarg->val.zend.str, 0); break; - } - result = cb->func->handler(_result, decl_cb->func->type, iarg, &to_free); +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; - if (result != _result) { - *(void **)_result = result; - } +#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 - zend_fcall_info_args_clear(&iarg->val.zend.cb->fci, 0); - for (i = 0; i < cb->args->count; ++i) { - zval_ptr_dtor(&zargv[i]); - } - free(zargv); + assert(FFI_OK == rc); } -static inline ffi_type *psi_ffi_decl_arg_type(decl_arg *darg); +static inline ffi_type *psi_ffi_decl_arg_type(struct psi_decl_arg *darg); -typedef struct PSI_LibffiContext { +struct psi_ffi_context { ffi_cif signature; ffi_type *params[2]; -} PSI_LibffiContext; +}; -typedef struct PSI_LibffiCall { +struct psi_ffi_call { + struct psi_context *context; + union { + struct { + struct psi_impl *impl; + struct psi_call_frame *frame; + } fn; + struct { + struct psi_let_exp *let_exp; + struct psi_ffi_call *impl_call; + } cb; + } impl; void *code; ffi_closure *closure; ffi_cif signature; - void *params[1]; /* [type1, type2, NULL, arg1, arg2] ... */ -} PSI_LibffiCall; + ffi_type *params[1]; /* [type1, type2, ... ] */ +}; + +static void psi_ffi_handler(ffi_cif *sig, void *result, void **args, void *data) +{ + struct psi_ffi_call *call = data; + + psi_context_call(call->context, *(zend_execute_data **)args[0], *(zval **)args[1], call->impl.fn.impl); +} + +static void psi_ffi_callback(ffi_cif *sig, void *result, void **args, void *data) +{ + struct psi_ffi_call *call = data, *impl_call = call->impl.cb.impl_call; + + if (impl_call->impl.fn.frame) { + struct psi_call_frame_callback cbdata; + + cbdata.cb = call->impl.cb.let_exp; + cbdata.argc = sig->nargs; + cbdata.argv = args; + cbdata.rval = result; + + psi_call_frame_do_callback(impl_call->impl.fn.frame, &cbdata); + } else { + assert(0); + } +} 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; } -static inline PSI_LibffiCall *PSI_LibffiCallAlloc(PSI_Context *C, decl *decl) { +static inline struct psi_ffi_call *psi_ffi_call_alloc(struct psi_context *C, struct psi_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 *)); + size_t i, c = psi_plist_count(decl->args); + struct psi_ffi_call *call = calloc(1, sizeof(*call) + 2 * c * sizeof(void *)); + struct psi_decl_arg *arg; - for (i = 0; i < c; ++i) { - call->params[i] = psi_ffi_decl_arg_type(decl->args->args[i]); + decl->info = call; + call->context = C; + + for (i = 0; psi_plist_get(decl->args, i, &arg); ++i) { + call->params[i] = psi_ffi_decl_arg_type(arg); } 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); + c, psi_ffi_decl_arg_type(decl->func), call->params); + assert(FFI_OK == rc); return call; } -static inline ffi_status PSI_LibffiCallInitClosure(PSI_Context *C, PSI_LibffiCall *call, impl *impl) { - PSI_LibffiContext *context = C->context; +static inline ffi_status psi_ffi_call_init_closure(struct psi_context *C, struct psi_ffi_call *call, struct psi_impl *impl) { + struct psi_ffi_context *context = C->context; - return psi_ffi_prep_closure(&call->closure, &call->code, &context->signature, psi_ffi_handler, impl); + call->impl.fn.impl = impl; + return psi_ffi_prep_closure(&call->closure, &call->code, &context->signature, psi_ffi_handler, call); } -static inline ffi_status PSI_LibffiCallInitCallbackClosure(PSI_Context *C, PSI_LibffiCall *call, let_callback *cb) { - return psi_ffi_prep_closure(&call->closure, &call->code, &call->signature, psi_ffi_callback, cb); +static inline ffi_status psi_ffi_call_init_callback_closure(struct psi_context *C, + struct psi_ffi_call *call, struct psi_ffi_call *impl_call, + struct psi_let_exp *cb) { + call->impl.cb.let_exp = cb; + call->impl.cb.impl_call = impl_call; + return psi_ffi_prep_closure(&call->closure, &call->code, &call->signature, psi_ffi_callback, call); } -static inline void PSI_LibffiCallFree(PSI_LibffiCall *call) { +static inline void psi_ffi_call_free(struct psi_ffi_call *call) { if (call->closure) { psi_ffi_closure_free(call->closure); } @@ -184,7 +217,7 @@ static inline void PSI_LibffiCallFree(PSI_LibffiCall *call) { static inline ffi_type *psi_ffi_token_type(token_t t) { switch (t) { default: - ZEND_ASSERT(0); + assert(0); /* no break */ case PSI_T_VOID: return &ffi_type_void; @@ -266,34 +299,39 @@ static size_t psi_ffi_struct_type_pad(ffi_type **els, size_t padding) { return padding; } -static ffi_type **psi_ffi_struct_type_elements(decl_struct *strct) { - size_t i, argc = strct->args->count, nels = 0, offset = 0, maxalign = 0; - ffi_type **els = calloc(argc + 1, sizeof(*els)); +static ffi_type **psi_ffi_struct_type_elements(struct psi_decl_struct *strct) { + size_t i = 0, argc = psi_plist_count(strct->args), nels = 0, offset = 0, maxalign = 0; + ffi_type **tmp, **els = calloc(argc + 1, sizeof(*els)); + struct psi_decl_arg *darg; - for (i = 0; i < strct->args->count; ++i) { - decl_arg *darg = strct->args->args[i]; + while (psi_plist_get(strct->args, i++, &darg)) { ffi_type *type = malloc(sizeof(*type)); size_t padding; - memcpy(type, psi_ffi_decl_arg_type(darg), sizeof(*type)); - - ZEND_ASSERT(type->size == darg->layout->len); + *type = *psi_ffi_decl_arg_type(darg); if (type->alignment > maxalign) { maxalign = type->alignment; } + assert(type->size == darg->layout->len); 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)); + 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; } - ZEND_ASSERT(offset == darg->layout->pos); + assert(offset == darg->layout->pos); offset = (offset + darg->layout->len + type->alignment - 1) & ~(type->alignment - 1); els[nels++] = type; @@ -302,42 +340,43 @@ static ffi_type **psi_ffi_struct_type_elements(decl_struct *strct) { /* apply struct alignment padding */ offset = (offset + maxalign - 1) & ~(maxalign - 1); - ZEND_ASSERT(offset <= strct->size); + assert(offset <= strct->size); if (offset < strct->size) { psi_ffi_struct_type_pad(&els[nels], strct->size - offset); } return els; } -static inline ffi_type *psi_ffi_decl_type(decl_type *type) { - decl_type *real = real_decl_type(type); +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_FUNCTION: - return &ffi_type_pointer; - case PSI_T_STRUCT: - if (!real->strct->engine.type) { + if (!real->real.strct->engine.type) { ffi_type *strct = calloc(1, sizeof(ffi_type)); strct->type = FFI_TYPE_STRUCT; strct->size = 0; - strct->elements = psi_ffi_struct_type_elements(real->strct); + strct->elements = psi_ffi_struct_type_elements(real->real.strct); - real->strct->engine.type = strct; - real->strct->engine.dtor = psi_ffi_struct_type_dtor; + real->real.strct->engine.type = strct; + real->real.strct->engine.dtor = psi_ffi_struct_type_dtor; } - return real->strct->engine.type; + return real->real.strct->engine.type; case PSI_T_UNION: - return psi_ffi_decl_arg_type(real->unn->args->args[0]); + { + struct psi_decl_arg *arg; + psi_plist_get(real->real.unn->args, 0, &arg); + return psi_ffi_decl_arg_type(arg); + } default: return psi_ffi_token_type(real->type); } } -static inline ffi_type *psi_ffi_decl_arg_type(decl_arg *darg) { +static inline ffi_type *psi_ffi_decl_arg_type(struct psi_decl_arg *darg) { if (darg->var->pointer_level) { return &ffi_type_pointer; } else { @@ -346,7 +385,7 @@ static inline ffi_type *psi_ffi_decl_arg_type(decl_arg *darg) { } -static inline PSI_LibffiContext *PSI_LibffiContextInit(PSI_LibffiContext *L) { +static inline struct psi_ffi_context *psi_ffi_context_init(struct psi_ffi_context *L) { ffi_status rc; if (!L) { @@ -357,158 +396,220 @@ static inline PSI_LibffiContext *PSI_LibffiContextInit(PSI_LibffiContext *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); - ZEND_ASSERT(rc == FFI_OK); + assert(rc == FFI_OK); return L; } -static void psi_ffi_init(PSI_Context *C) +static inline void psi_ffi_context_free(struct psi_ffi_context **L) { + if (*L) { + free(*L); + *L = NULL; + } +} + +static void psi_ffi_init(struct psi_context *C) { - C->context = PSI_LibffiContextInit(NULL); + C->context = psi_ffi_context_init(NULL); +} + +static inline void psi_ffi_destroy_callbacks(struct psi_context *C, struct psi_let_exp *let_exp) { + struct psi_let_callback *cb; + struct psi_let_func *fn = NULL; + + switch (let_exp->kind) { + case PSI_LET_CALLBACK: + cb = let_exp->data.callback; + + if (cb->decl && cb->decl->info) { + psi_ffi_call_free(cb->decl->info); + } + fn = cb->func; + /* no break */ + case PSI_LET_FUNC: + if (!fn) { + fn = let_exp->data.func; + } + + if (fn->inner) { + size_t i = 0; + struct psi_let_exp *cb; + + while (psi_plist_get(fn->inner, i++, &cb)) { + psi_ffi_destroy_callbacks(C, cb); + } + } + break; + default: + break; + } } -static void psi_ffi_dtor(PSI_Context *C) +static void psi_ffi_dtor(struct psi_context *C) { if (C->decls) { - size_t i; + size_t i = 0; + struct psi_decl *decl; - for (i = 0; i < C->decls->count; ++i) { - decl *decl = C->decls->list[i]; - - if (decl->call.info) { - PSI_LibffiCallFree(decl->call.info); + while (psi_plist_get(C->decls, i++, &decl)) { + if (decl->info) { + psi_ffi_call_free(decl->info); } } } if (C->impls) { - size_t i, j; + size_t i = 0; + struct psi_impl *impl; - for (i = 0; i < C->impls->count; ++i) { - impl *impl = C->impls->list[i]; + while (psi_plist_get(C->impls, i++, &impl)) { + size_t j = 0; + struct psi_let_stmt *let; - for (j = 0; j < impl->stmts->let.count; ++j) { - let_stmt *let = impl->stmts->let.list[j]; + while (psi_plist_get(impl->stmts.let, j++, &let)) { + psi_ffi_destroy_callbacks(C, let->exp); + } + } + } + psi_ffi_context_free((void *) &C->context); +} - if (let->val && let->val->kind == PSI_LET_CALLBACK) { - let_callback *cb = let->val->data.callback; +static inline void psi_ffi_compile_callbacks(struct psi_context *C, + struct psi_ffi_call *impl_call, struct psi_let_exp *let_exp) { + struct psi_ffi_call *call; + struct psi_let_callback *cb; + struct psi_let_func *fn = NULL; + + switch (let_exp->kind) { + case PSI_LET_CALLBACK: + cb = let_exp->data.callback; + if ((call = psi_ffi_call_alloc(C, cb->decl))) { + if (FFI_OK != psi_ffi_call_init_callback_closure(C, call, impl_call, let_exp)) { + psi_ffi_call_free(call); + break; + } - if (cb->decl && cb->decl->call.info) { - PSI_LibffiCallFree(cb->decl->call.info); - } - } + cb->decl->sym = call->code; + } + fn = cb->func; + /* no break */ + case PSI_LET_FUNC: + if (!fn) { + fn = let_exp->data.func; + } + if (fn->inner) { + size_t i = 0; + struct psi_let_exp *inner_let; + + while (psi_plist_get(fn->inner, i++, &inner_let)) { + psi_ffi_compile_callbacks(C, impl_call, inner_let); } } + break; + default: + break; } - free(C->context); } -static zend_function_entry *psi_ffi_compile(PSI_Context *C) +static zend_function_entry *psi_ffi_compile(struct psi_context *C) { - size_t c, i, j = 0; + 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(C->impls->count + 1, sizeof(*zfe)); - for (i = 0; i < C->impls->count; ++i) { - zend_function_entry *zf = &zfe[j]; - PSI_LibffiCall *call; - impl *impl = C->impls->list[i]; + zfe = calloc(psi_plist_count(C->impls) + 1, sizeof(*zfe)); + + while (psi_plist_get(C->impls, i++, &impl)) { + size_t l = 0; + struct psi_let_stmt *let; + struct psi_ffi_call *call; + zend_function_entry *zf = &zfe[nf]; if (!impl->decl) { continue; } - - call = PSI_LibffiCallAlloc(C, impl->decl); - if (FFI_OK != PSI_LibffiCallInitClosure(C, call, impl)) { - PSI_LibffiCallFree(call); + if (!(call = psi_ffi_call_alloc(C, impl->decl))) { + continue; + } + if (FFI_OK != psi_ffi_call_init_closure(C, call, impl)) { + psi_ffi_call_free(call); continue; } zf->fname = impl->func->name + (impl->func->name[0] == '\\'); - zf->num_args = impl->func->args->count; zf->handler = call->code; + zf->num_args = psi_plist_count(impl->func->args); zf->arg_info = psi_internal_arginfo(impl); - ++j; - - for (c = 0; c < impl->stmts->let.count; ++c) { - let_stmt *let = impl->stmts->let.list[c]; - - if (let->val->kind == PSI_LET_CALLBACK) { - let_callback *cb = let->val->data.callback; + ++nf; - call = PSI_LibffiCallAlloc(C, cb->decl); - if (FFI_OK != PSI_LibffiCallInitCallbackClosure(C, call, cb)) { - PSI_LibffiCallFree(call); - continue; - } - - cb->decl->call.sym = call->code; - } + while (psi_plist_get(impl->stmts.let, l++, &let)) { + psi_ffi_compile_callbacks(C, call, let->exp); } } - for (i = 0; i < C->decls->count; ++i) { - decl *decl = C->decls->list[i]; - -// if (decl->impl) { -// continue; -// } - if (decl->call.info) { + while (psi_plist_get(C->decls, d++, &decl)) { + if (decl->info) { continue; } - PSI_LibffiCallAlloc(C, decl); + psi_ffi_call_alloc(C, decl); } return zfe; } -static void psi_ffi_call(PSI_Context *C, decl_callinfo *decl_call, impl_vararg *va) { - PSI_LibffiCall *call = decl_call->info; +static void psi_ffi_call(struct psi_context *C, struct psi_call_frame *frame, struct psi_decl *decl, void *rval, void **args) { + struct psi_ffi_call *info = decl->info; + struct psi_call_frame *prev = info->impl.fn.frame; - 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 *)); + info->impl.fn.frame = frame; + ffi_call(&info->signature, FFI_FN(decl->sym), rval, args); + info->impl.fn.frame = prev; +} - 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 void psi_ffi_call_va(struct psi_context *C, struct psi_call_frame *frame, struct psi_decl *decl, void *rval, void **args, + size_t va_count, void **va_types) { + ffi_cif signature; + struct psi_ffi_call *info = decl->info; + struct psi_call_frame *prev = info->impl.fn.frame; + size_t argc = psi_plist_count(decl->args); + ffi_type **param_types = ecalloc(argc + va_count + 1, sizeof(ffi_type *)); + + memcpy(param_types, info->params, argc * sizeof(ffi_type *)); + memcpy(param_types + argc, va_types, va_count * sizeof(ffi_type *)); + + psi_ffi_prep_va(&info->signature, &signature, argc, va_count, param_types); + info->impl.fn.frame = frame; + ffi_call(&signature, FFI_FN(decl->sym), rval, args); + info->impl.fn.frame = prev; + efree(param_types); +} + +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); } + return NULL; } -static PSI_ContextOps ops = { +static struct psi_context_ops ops = { psi_ffi_init, psi_ffi_dtor, psi_ffi_compile, psi_ffi_call, + psi_ffi_call_va, + psi_ffi_query, }; -PSI_ContextOps *PSI_Libffi(void) +struct psi_context_ops *psi_libffi_ops(void) { return &ops; }