ffi: improve support for functions returning arrays
[m6w6/ext-psi] / src / libffi.c
index 34a15987a729143b2272c0bafd64f4b53154f090..f14a9cd651645cb380a8f65e82d795aefb49b488 100644 (file)
@@ -1,6 +1,34 @@
+/*******************************************************************************
+ Copyright (c) 2016, Michael Wallner <mike@php.net>.
+ 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"
-#include "php_psi.h"
-#include "libffi.h"
+
+#ifdef HAVE_LIBFFI
 
 #undef PACKAGE
 #undef PACKAGE_BUGREPORT
 # endif
 #endif
 
+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;
+
+       void *code;
+       ffi_closure *closure;
+};
+
+struct psi_ffi_callback_info {
+       struct psi_ffi_impl_info *impl_info;
+       struct psi_let_exp *let_exp;
+
+       void *code;
+       ffi_closure *closure;
+};
+
+struct psi_ffi_decl_info {
+       ffi_cif signature;
+       ffi_type *rv_array;
+       ffi_type *params[1];
+};
+
 static void *psi_ffi_closure_alloc(size_t s, void **code)
 {
 #ifdef PSI_HAVE_FFI_CLOSURE_ALLOC
@@ -35,7 +90,21 @@ 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);
+       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
 }
 
@@ -48,15 +117,32 @@ 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_prep_va(ffi_cif *base, ffi_cif *signature, size_t argc, size_t va_count,
+               ffi_type **param_types) {
+       ffi_status rc;
 
-static inline ffi_abi psi_ffi_abi(const char *convention) {
-       return FFI_DEFAULT_ABI;
+#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
+
+       assert(FFI_OK == rc);
 }
-static inline ffi_type *psi_ffi_type(token_t t) {
+
+#if HAVE_INT128
+static ffi_type *ffi_type_sint128;
+static ffi_type *ffi_type_uint128;
+#endif
+
+static inline ffi_type *psi_ffi_decl_arg_type(struct psi_decl_arg *darg);
+
+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;
@@ -76,97 +162,541 @@ static inline ffi_type *psi_ffi_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_INT:
+       case PSI_T_ENUM:
                return &ffi_type_sint;
        case PSI_T_FLOAT:
                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;
+       }
+}
+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();
+       }
+       return NULL;
+}
+static void psi_ffi_type_dtor(void *type) {
+       ffi_type *strct = type;
+
+       if (strct->elements) {
+               ffi_type **ptr;
+
+               for (ptr = strct->elements; *ptr; ++ptr) {
+                       free(*ptr);
+               }
+               free(strct->elements);
+       }
+       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;
+}
+
+struct psi_ffi_struct_element_storage {
+       ffi_type **els;
+       size_t nels;
+       size_t argc;
+       size_t offset;
+       size_t max_align;
+       size_t last_arg_pos;
+};
+
+static inline void psi_ffi_struct_type_element(
+               struct psi_ffi_struct_element_storage *s, struct psi_decl_arg *darg,
+               ffi_type *darg_type) {
+
+       ffi_type *type, **tmp;
+       size_t padding;
+
+       if (darg->layout->pos == s->last_arg_pos) {
+               /* skip bit fields */
+               return;
+       }
+       s->last_arg_pos = darg->layout->pos;
+
+       type = malloc(sizeof(*type));
+       *type = *darg_type;
+
+       if (type->alignment > s->max_align) {
+               s->max_align = type->alignment;
+       }
+
+       assert(type->size <= darg->layout->len);
+       if ((padding = psi_offset_padding(darg->layout->pos - s->offset, type->alignment))) {
+               if (s->nels + padding + 1 > s->argc) {
+                       s->argc += padding;
+                       tmp = realloc(s->els, (s->argc + 1) * sizeof(*s->els));
+                       if (tmp) {
+                               s->els = tmp;
+                       } else {
+                               free(s->els);
+                               abort();
+                       }
+                       s->els[s->argc] = NULL;
+               }
+               psi_ffi_struct_type_pad(&s->els[s->nels], padding);
+               s->nels += padding;
+               s->offset += padding;
+       }
+       assert(s->offset == darg->layout->pos);
+
+       s->offset = (s->offset + darg->layout->len + type->alignment - 1) & ~(type->alignment - 1);
+       s->els[s->nels++] = type;
+}
+
+static ffi_type **psi_ffi_struct_type_elements(struct psi_decl_struct *strct) {
+       size_t i = 0;
+       ffi_type **tmp;
+       struct psi_decl_arg *darg;
+       struct psi_ffi_struct_element_storage s = {0};
+
+       s.last_arg_pos = -1;
+       s.argc = psi_plist_count(strct->args);
+       s.els = calloc(s.argc + 1, sizeof(*s.els));
+
+       while (psi_plist_get(strct->args, i++, &darg)) {
+               psi_ffi_struct_type_element(&s, darg, psi_ffi_decl_arg_type(darg));
+       }
+
+       /* apply struct alignment padding */
+       s.offset = (s.offset + s.max_align - 1) & ~(s.max_align - 1);
+
+       assert(s.offset <= strct->size);
+       if (s.offset < strct->size) { /* WTF? */
+               size_t padding = strct->size - s.offset;
+
+               tmp = realloc(s.els, (padding + s.argc + 1) * sizeof(*s.els));
+               if (tmp) {
+                       s.els = tmp;
+               } else {
+                       free(s.els);
+                       return NULL;
+               }
+               psi_ffi_struct_type_pad(&s.els[s.nels], padding);
+               s.els[s.argc + padding] = NULL;
+       }
+
+       return s.els;
+}
+
+static inline ffi_type *psi_ffi_decl_type(struct psi_decl_type *type) {
+       struct psi_decl_type *real = psi_decl_type_get_real(type);
+
+       if (real != type && type->real.def->var->pointer_level) {
+               return &ffi_type_pointer;
+       }
+
+       switch (real->type) {
+       case PSI_T_STRUCT:
+               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->real.strct);
+
+                       real->real.strct->engine.type = strct;
+                       real->real.strct->engine.dtor = psi_ffi_type_dtor;
+               }
+
+               return real->real.strct->engine.type;
+
+       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);
+               }
+
+       default:
+               break;
+       }
+
+       return psi_ffi_token_type(real->type);
+}
+
+static inline ffi_type *psi_ffi_decl_func_array_type(struct psi_decl *fn) {
+       struct psi_ffi_decl_info *info = fn->info;
+       struct psi_ffi_struct_element_storage s = {0};
+       struct psi_layout l;
+       ffi_type *type;
+       size_t i;
+
+       if (info->rv_array) {
+               return info->rv_array;
+       }
+
+       s.last_arg_pos = -1;
+       s.argc = fn->func->var->array_size;
+       s.els = calloc(s.argc + 1, sizeof(*s.els));
+
+       info->rv_array = calloc(1, sizeof(ffi_type));
+       info->rv_array->type = FFI_TYPE_STRUCT;
+       info->rv_array->size = 0;
+       info->rv_array->elements = s.els;
+
+       l.pos = 0;
+       if (fn->func->var->pointer_level > 1) {
+               l.len = SIZEOF_VOID_P;
+               type = &ffi_type_pointer;
+       } else {
+               l.len = psi_decl_type_get_size(fn->func->type, NULL);
+               type = psi_ffi_decl_type(fn->func->type);
        }
+
+       assert(!fn->func->layout);
+       fn->func->layout = &l;
+       for (i = 0; i < fn->func->var->array_size; ++i) {
+               psi_ffi_struct_type_element(&s, fn->func, type);
+               info->rv_array->elements = s.els;
+               l.pos += l.len;
+       }
+       fn->func->layout = NULL;
+
+       return info->rv_array;
 }
-static inline ffi_type *psi_ffi_decl_type(decl_type *type) {
-       return psi_ffi_type(real_decl_type(type)->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);
+       }
 }
-static inline ffi_type *psi_ffi_decl_arg_type(decl_arg *darg) {
+
+static inline ffi_type *psi_ffi_decl_func_type(struct psi_decl *fn) {
+       struct psi_decl_arg *darg = fn->func;
+
        if (darg->var->pointer_level) {
+               if (darg->var->array_size) {
+                       /* mimic a struct resembling the array return type of fn */
+                       return psi_ffi_decl_func_array_type(fn);
+               }
                return &ffi_type_pointer;
        } else {
                return psi_ffi_decl_type(darg->type);
        }
 }
 
-typedef struct PSI_LibffiContext {
-       ffi_cif signature;
-       ffi_type *params[2];
+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 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 *));
+
+               decl->info = info;
+
+               for (i = 0; psi_plist_get(decl->args, i, &arg); ++i) {
+                       info->params[i] = psi_ffi_decl_arg_type(arg);
+               }
+               info->params[c] = NULL;
+
+               rc = ffi_prep_cif(&info->signature, psi_ffi_abi(decl->abi->convention),
+                               c, psi_ffi_decl_func_type(decl), info->params);
+
+               if (FFI_OK != rc) {
+                       free(info);
+                       decl->info = NULL;
+               }
+       }
+
+       return decl->info;
+}
+
+static inline void psi_ffi_decl_dtor(struct psi_decl *decl) {
+       if (decl->info) {
+               struct psi_ffi_decl_info *info = decl->info;
+
+               if (info->rv_array) {
+                       psi_ffi_type_dtor(info->rv_array);
+               }
+               free(decl->info);
+               decl->info = NULL;
+       }
+}
+
+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;
+
+       psi_context_call(info->context, *(zend_execute_data **)args[0], *(zval **)args[1], impl);
+}
+
+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;
+
+       psi_call_frame_do_callback(cb_info->impl_info->frame, &cb_data);
+}
+
+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;
+       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);
+               }
+
+               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);
+
+               if (FFI_OK != rc) {
+                       free(cb_info);
+                       break;
+               }
+               cb->info = cb_info;
+
+               assert(!cb->decl->sym);
+               cb->decl->sym = cb_info->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_callback_init(impl_info, inner_let);
+                       }
+               }
+               break;
+       default:
+               break;
+       }
+}
+
+static inline void psi_ffi_callback_dtor(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;
+
+               psi_ffi_decl_dtor(cb->decl);
+
+               if (cb->info) {
+                       struct psi_ffi_callback_info *info = cb->info;
+
+                       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 (fn->inner) {
+                       size_t i = 0;
+                       struct psi_let_exp *cb;
+
+                       while (psi_plist_get(fn->inner, i++, &cb)) {
+                               psi_ffi_callback_dtor(cb);
+                       }
+               }
+               break;
+       default:
+               break;
+       }
+}
+
+static inline struct psi_ffi_impl_info *psi_ffi_impl_init(struct psi_impl *impl,
+               struct psi_context *C) {
+       struct psi_ffi_context *context = C->context;
+       struct psi_ffi_impl_info *info = calloc(1, sizeof(*info));
+       struct psi_let_stmt *let;
+       ffi_status rc;
+       size_t l = 0;
+
+       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;
+       }
+
+       while (psi_plist_get(impl->stmts.let, l++, &let)) {
+               psi_ffi_callback_init(info, let->exp);
+       }
+
+       return impl->info = info;
+}
+
+static inline void psi_ffi_impl_dtor(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) {
+                       psi_ffi_closure_free(info->closure);
+               }
+               free(info);
+               impl->info = NULL;
+       }
+}
+
+static void psi_ffi_extvar_get(ffi_cif *sig, void *result, void **args, void *data) {
+       struct psi_decl_extvar *evar = data;
+
+       psi_decl_extvar_get(evar, result);
+}
+
+static void psi_ffi_extvar_set(ffi_cif *sig, void *result, void **args, void *data) {
+       struct psi_decl_extvar *evar = data;
+
+       psi_decl_extvar_set(evar, args[0]);
+}
+
+struct psi_ffi_extvar_info {
        struct {
-               struct PSI_LibffiData **list;
-               size_t count;
-       } data;
-} PSI_LibffiContext;
-
-typedef struct PSI_LibffiData {
-       PSI_LibffiContext *context;
-       impl *impl;
-       zend_internal_arg_info *arginfo;
-       void *code;
-       ffi_closure *closure;
-       ffi_cif signature;
-       ffi_type *params[1];
-} PSI_LibffiData;
+               ffi_cif signature;
+               void *code;
+               ffi_closure *closure;
+       } get;
+       struct {
+               ffi_cif signature;
+               ffi_type *params[1];
+               void *code;
+               ffi_closure *closure;
+       } set;
+};
 
-static inline PSI_LibffiData *PSI_LibffiDataAlloc(PSI_LibffiContext *context, impl *impl) {
+static inline ffi_status psi_ffi_extvar_init(struct psi_decl_extvar *evar) {
+       struct psi_ffi_extvar_info *info = calloc(1, sizeof(*info));
        ffi_status rc;
-       size_t i, c = impl->decl->args ? impl->decl->args->count : 0;
-       PSI_LibffiData *data = malloc(sizeof(*data) + c * sizeof(ffi_type *));
-
-       data->context = context;
-       data->impl = impl;
-       data->arginfo = psi_internal_arginfo(impl);
-       for (i = 0; i < c; ++i) {
-               data->params[i] = psi_ffi_decl_arg_type(impl->decl->args->args[i]);
-       }
-       data->params[c] = NULL;
-
-       rc = ffi_prep_cif(
-                       &data->signature,
-                       psi_ffi_abi(data->impl->decl->abi->convention),
-                       c,
-                       psi_ffi_decl_arg_type(data->impl->decl->func),
-                       data->params);
-       ZEND_ASSERT(FFI_OK == rc);
-
-       data->closure = psi_ffi_closure_alloc(sizeof(ffi_closure), &data->code);
-       ZEND_ASSERT(data->closure != NULL);
-#if PSI_HAVE_FFI_PREP_CLOSURE_LOC
-       rc = ffi_prep_closure_loc(
-                       data->closure,
-                       &context->signature,
-                       psi_ffi_handler,
-                       data,
-                       data->code);
-       ZEND_ASSERT(FFI_OK == rc);
-#elif PSI_HAVE_FFI_PREP_CLOSURE
-       rc = ffi_prep_closure(data->code, &context->signature, psi_ffi_handler, data);
-       ZEND_ASSERT(FFI_OK == rc);
-#else
-# error "Neither ffi_prep_closure() nor ffi_prep_closure_loc() available"
-#endif
 
-       context->data.list = realloc(context->data.list, ++context->data.count * sizeof(*context->data.list));
-       context->data.list[context->data.count-1] = data;
+       evar->info = info;
+
+       psi_ffi_decl_init(evar->getter);
+       psi_ffi_decl_init(evar->setter);
+
+       rc = ffi_prep_cif(&info->get.signature, FFI_DEFAULT_ABI, 0,
+                       psi_ffi_decl_func_type(evar->getter), NULL);
+       if (FFI_OK != rc) {
+               return rc;
+       }
+       rc = psi_ffi_prep_closure(&info->get.closure, &info->get.code,
+                       &info->get.signature, psi_ffi_extvar_get, evar);
+       if (FFI_OK != rc) {
+               return rc;
+       }
+
+       info->set.params[0] = psi_ffi_decl_arg_type(evar->arg);
+       rc = ffi_prep_cif(&info->set.signature, FFI_DEFAULT_ABI, 1,
+                       &ffi_type_void, info->set.params);
+       if (FFI_OK != rc) {
+               return rc;
+       }
+       rc = psi_ffi_prep_closure(&info->set.closure, &info->set.code,
+                       &info->set.signature, psi_ffi_extvar_set, evar);
+       if (FFI_OK != rc) {
+               return rc;
+       }
+
+       evar->getter->sym = info->get.code;
+       evar->setter->sym = info->set.code;
 
-       return data;
+       return FFI_OK;
 }
 
-static inline void PSI_LibffiDataFree(PSI_LibffiData *data) {
-       psi_ffi_closure_free(data->closure);
-       free(data->arginfo);
-       free(data);
+static inline void psi_ffi_extvar_dtor(struct psi_decl_extvar *evar) {
+       if (evar->info) {
+               free(evar->info);
+               evar->info = NULL;
+       }
 }
 
-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) {
@@ -177,123 +707,226 @@ 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 inline void PSI_LibffiContextDtor(PSI_LibffiContext *L) {
-       size_t i;
-
-       for (i = 0; i < L->data.count; ++i) {
-               PSI_LibffiDataFree(L->data.list[i]);
-       }
-       if (L->data.list) {
-               free(L->data.list);
-       }
-}
-
-static inline void PSI_LibffiContextFree(PSI_LibffiContext **L) {
+static inline void psi_ffi_context_free(struct psi_ffi_context **L) {
        if (*L) {
-               PSI_LibffiContextDtor(*L);
                free(*L);
                *L = NULL;
        }
 }
 
-static void psi_ffi_handler(ffi_cif *_sig, void *_result, void **_args, void *_data)
+static void psi_ffi_init(struct psi_context *C)
 {
-       PSI_LibffiData *data = _data;
-       size_t i;
-       void **arg_prm = NULL;
-       impl_val ret_val;
+       C->context = psi_ffi_context_init(NULL);
+}
 
-       if (SUCCESS != psi_parse_args(*(zend_execute_data **)_args[0], data->impl)) {
-               return;
-       }
+static void psi_ffi_dtor(struct psi_context *C)
+{
+       if (C->decls) {
+               size_t i = 0;
+               struct psi_decl *decl;
 
-       if (data->impl->decl->args) {
-               arg_prm = malloc(data->impl->decl->args->count * sizeof(*arg_prm));
+               while (psi_plist_get(C->decls, i++, &decl)) {
+                       psi_ffi_decl_dtor(decl);
+               }
 
-               for (i = 0; i < data->impl->decl->args->count; ++i) {
-                       decl_arg *darg = data->impl->decl->args->args[i];
+       }
+       if (C->vars) {
+               size_t i = 0;
+               struct psi_decl_extvar *evar;
 
-                       arg_prm[i] = psi_do_let(darg);
+               while (psi_plist_get(C->vars, i++, &evar)) {
+                       psi_ffi_extvar_dtor(evar);
                }
        }
+       if (C->impls) {
+               size_t i = 0;
+               struct psi_impl *impl;
 
-       ffi_call(&data->signature, FFI_FN(data->impl->decl->dlptr), &ret_val, arg_prm);
-
-       psi_do_return(*(zval **)_args[1], data->impl->stmts->ret.list[0], &ret_val);
+               while (psi_plist_get(C->impls, i++, &impl)) {
+                       psi_ffi_impl_dtor(impl);
+               }
+       }
+       psi_ffi_context_free((void *) &C->context);
+}
 
-       for (i = 0; i < data->impl->stmts->set.count; ++i) {
-               set_stmt *set = data->impl->stmts->set.list[i];
 
-               if (set->arg->_zv) {
-                       psi_do_set(set->arg->_zv, set->val);
+static zend_function_entry *psi_ffi_compile(struct psi_context *C)
+{
+       size_t i = 0, d = 0, v = 0, nf = 0;
+       struct psi_impl *impl;
+       struct psi_decl *decl;
+       struct psi_decl_extvar *evar;
+       zend_function_entry *zfe = NULL;
+
+       while (psi_plist_get(C->vars, v++, &evar)) {
+               if (FFI_OK == psi_ffi_extvar_init(evar)) {
+                       /* */
                }
        }
 
-       for (i = 0; i < data->impl->stmts->fre.count; ++i) {
-               free_stmt *fre = data->impl->stmts->fre.list[i];
-
-               psi_do_free(fre);
+       if (C->impls) {
+               zfe = calloc(psi_plist_count(C->impls) + 1, sizeof(*zfe));
+
+               while (psi_plist_get(C->impls, i++, &impl)) {
+                       zend_function_entry *zf = &zfe[nf];
+
+                       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;
+               }
        }
 
-       psi_do_clean(data->impl);
+       while (psi_plist_get(C->decls, d++, &decl)) {
+               if (decl->info) {
+                       continue;
+               }
 
-       if (arg_prm) {
-               free(arg_prm);
+               psi_ffi_decl_init(decl);
        }
-}
 
-static void psi_ffi_init(PSI_Context *C)
-{
-       C->context = PSI_LibffiContextInit(NULL);
+       return zfe;
 }
 
-static void psi_ffi_dtor(PSI_Context *C)
-{
-       PSI_LibffiContextFree((void *) &C->context);
+static inline void psi_ffi_call_ex(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;
+       struct psi_ffi_impl_info *impl_info;
+       struct psi_call_frame *prev;
+
+       if (impl) {
+               impl_info = impl->info;
+               prev = impl_info->frame;
+               impl_info->frame = frame;
+       }
+       ffi_call(&decl_info->signature, FFI_FN(decl->sym),
+                       psi_call_frame_get_rpointer(frame),
+                       psi_call_frame_get_arg_pointers(frame));
+       if (impl) {
+               impl_info->frame = prev;
+       }
 }
 
-static zend_function_entry *psi_ffi_compile(PSI_Context *C)
-{
-       size_t i, j = 0;
-       zend_function_entry *zfe;
-       PSI_LibffiContext *ctx = C->context;
+static inline 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);
+       struct psi_impl *impl = psi_call_frame_get_impl(frame);
+       struct psi_ffi_decl_info *decl_info = decl->info;
+       struct psi_ffi_impl_info *impl_info;
+       size_t i, va_count, argc;
+       ffi_type **param_types;
+
+       argc = psi_plist_count(decl->args);
+       va_count = psi_call_frame_num_var_args(frame);
+       param_types = ecalloc(argc + va_count + 1, sizeof(ffi_type *));
+       memcpy(param_types, decl_info->params, argc * sizeof(ffi_type *));
+       for (i = 0; i < va_count; ++i) {
+               struct psi_call_frame_argument *frame_arg;
+
+               frame_arg = psi_call_frame_get_var_argument(frame, i);
+               param_types[argc + i] = psi_ffi_impl_type(frame_arg->va_type);
+       }
 
-       if (!C->impls) {
-               return NULL;
+       psi_ffi_prep_va(&decl_info->signature, &signature, argc, va_count, param_types);
+
+       if (impl) {
+               impl_info = impl->info;
+               prev = impl_info->frame;
+               impl_info->frame = frame;
+       }
+       ffi_call(&signature, FFI_FN(decl->sym),
+                       psi_call_frame_get_rpointer(frame),
+                       psi_call_frame_get_arg_pointers(frame));
+       if (impl) {
+               impl_info->frame = prev;
        }
 
-       zfe = calloc(C->impls->count + 1, sizeof(*zfe));
-       for (i = 0; i < C->impls->count; ++i) {
-               zend_function_entry *zf = &zfe[j];
-               PSI_LibffiData *data;
+       efree(param_types);
+}
 
-               if (!C->impls->list[i]->decl) {
-                       continue;
-               }
+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);
+       }
+}
 
-               data = PSI_LibffiDataAlloc(ctx, C->impls->list[i]);
-               zf->fname = C->impls->list[i]->func->name + (C->impls->list[i]->func->name[0] == '\\');
-               zf->num_args = C->impls->list[i]->func->args->count;
-               zf->handler = data->code;
-               zf->arg_info = data->arginfo;
-               ++j;
+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;
+}
 
-       return zfe;
+static ZEND_RESULT_CODE psi_ffi_load()
+{
+#if HAVE_INT128
+       ffi_type *i128, *u128;
+
+       i128 = calloc(1, 3*sizeof(ffi_type));
+       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 = calloc(1, 3*sizeof(ffi_type));
+       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 SUCCESS;
 }
 
-static PSI_ContextOps ops = {
+static void psi_ffi_free()
+{
+#if HAVE_INT128
+       free(ffi_type_sint128);
+       free(ffi_type_uint128);
+#endif
+}
+
+static struct psi_context_ops ops = {
+       psi_ffi_load,
+       psi_ffi_free,
        psi_ffi_init,
        psi_ffi_dtor,
        psi_ffi_compile,
+       psi_ffi_call,
+       psi_ffi_query,
 };
 
-PSI_ContextOps *PSI_Libffi(void)
+struct psi_context_ops *psi_libffi_ops(void)
 {
        return &ops;
 }
+
+#endif /* HAVE_LIBFFI */