more verbose context ops
authorMichael Wallner <mike@php.net>
Tue, 27 Nov 2018 19:27:15 +0000 (20:27 +0100)
committerMichael Wallner <mike@php.net>
Tue, 4 Dec 2018 11:13:50 +0000 (12:13 +0100)
13 files changed:
TODO
m4/psi/psi.m4
src/context.c
src/context.h
src/libffi.c
src/libffi_compat.h [new file with mode: 0644]
src/libjit.c
src/types/decl.h
src/types/decl_arg.h
src/types/decl_struct.c
src/types/decl_struct.h
src/types/decl_type.c
tests/parser/validate003.phpt

diff --git a/TODO b/TODO
index e029536..ae6f541 100644 (file)
--- a/TODO
+++ b/TODO
@@ -18,3 +18,5 @@
 * int128
 * very small real numbers
 * cache tokens of files and copy tokens if used for further processing
+
+* optimized inlines like htonl
index bb8254b..2ad2499 100644 (file)
@@ -50,6 +50,7 @@ AC_DEFUN(PSI_CONFIG_INIT, [
        if test "$PHP_PSI_MAINTAINER_MODE" = "yes"; then
                PSI_DEPS=true
                PHP_SUBST(PSI_DEPS)
+               EXTRA_CFLAGS="-Wall -Wextra $EXTRA_CFLAGS"
        else
                PSI_DEPS=false
        fi
index 9b5730e..ecccdb0 100644 (file)
@@ -78,7 +78,7 @@ PHP_MINIT_FUNCTION(psi_context)
        }
 
        PSI_G(ops) = ops;
-       if (ops->load && SUCCESS != ops->load()) {
+       if (ops->load && !ops->load()) {
                return FAILURE;
        }
 
@@ -122,13 +122,10 @@ struct psi_context *psi_context_init(struct psi_context *C, struct psi_context_o
        psi_data_ctor(PSI_DATA(C), error, flags);
        C->ops = ops;
 
-       if (ops->init) {
-               ops->init(C);
+       if (ops->init && !ops->init(C)) {
+               return NULL;
        }
 
-       assert(ops->call != NULL);
-       assert(ops->compile != NULL);
-
        return C;
 }
 
@@ -210,17 +207,9 @@ void psi_context_build(struct psi_context *C, const char *paths)
                ptr = sep + 1;
        } while (sep);
 
-
-       if (psi_context_compile(C)) {
-               /* zend_register_functions depends on EG(current_module) pointing into module */
-               EG(current_module) = zend_hash_str_find_ptr(&module_registry, "psi", sizeof("psi") - 1);
-               if (SUCCESS != zend_register_functions(NULL, C->closures, NULL, MODULE_PERSISTENT)) {
-                       C->error(PSI_DATA(C), NULL, PSI_WARNING, "Failed to register functions!");
-               }
-               EG(current_module) = NULL;
-       }
-
        free(cpy);
+
+       psi_context_compile(C);
 }
 
 #include <ctype.h>
@@ -240,7 +229,7 @@ static inline bool prefix_match(zend_string *a, zend_string *b)
        return true;
 }
 
-zend_function_entry *psi_context_compile(struct psi_context *C)
+static inline void psi_context_consts_init(struct psi_context *C)
 {
        zend_constant zc;
 
@@ -262,7 +251,6 @@ zend_function_entry *psi_context_compile(struct psi_context *C)
                        zend_register_constant(&zc);
                }
        }
-
        if (C->enums) {
                size_t i = 0;
                struct psi_decl_enum *e;
@@ -294,10 +282,327 @@ zend_function_entry *psi_context_compile(struct psi_context *C)
                        }
                }
        }
+}
+
+static inline void psi_context_extvars_init(struct psi_context *C)
+{
+       if (C->vars) {
+               size_t v = 0;
+               struct psi_decl_extvar *evar;
 
-       return C->closures = C->ops->compile(C);
+               while (psi_plist_get(C->vars, v++, &evar)) {
+                       C->ops->extvar_init(C, evar);
+               }
+       }
 }
 
+static inline void psi_context_callback_init(struct psi_context *C,
+               struct psi_let_exp *let_exp, struct psi_impl *impl)
+{
+       struct psi_let_func *fn = let_exp->data.func;
+
+       switch (let_exp->kind) {
+       case PSI_LET_CALLBACK:
+               C->ops->cb_init(C, let_exp, impl);
+               /* override fn */
+               fn = let_exp->data.callback->func;
+               /* no break */
+       case PSI_LET_FUNC:
+               if (fn->inner) {
+                       size_t i = 0;
+                       struct psi_let_exp *inner_let;
+
+                       while (psi_plist_get(fn->inner, i++, &inner_let)) {
+                               psi_context_callback_init(C, inner_let, impl);
+                       }
+               }
+               break;
+       default:
+               break;
+       }
+}
+
+static inline void psi_context_callback_dtor(struct psi_context *C,
+               struct psi_let_exp *let_exp, struct psi_impl *impl)
+{
+       struct psi_let_func *fn = let_exp->data.func;
+
+       switch (let_exp->kind) {
+       case PSI_LET_CALLBACK:
+               C->ops->cb_dtor(C, let_exp, impl);
+               /* override func */
+               fn = let_exp->data.callback->func;
+               /* no break */
+       case PSI_LET_FUNC:
+               if (fn->inner) {
+                       size_t i = 0;
+                       struct psi_let_exp *cb;
+
+                       while (psi_plist_get(fn->inner, i++, &cb)) {
+                               psi_context_callback_dtor(C, cb, impl);
+                       }
+               }
+               break;
+       default:
+               break;
+       }
+}
+
+static inline void psi_context_impls_init(struct psi_context *C)
+{
+       size_t nf = 0;
+       zend_function_entry *zfe = NULL;
+
+       if (C->impls) {
+               size_t i = 0;
+               struct psi_impl *impl;
+
+               zfe = pecalloc(psi_plist_count(C->impls) + 1, sizeof(*zfe), 1);
+
+               while (psi_plist_get(C->impls, i++, &impl)) {
+                       zend_function_entry *zf = &zfe[nf];
+                       struct psi_let_stmt *let;
+                       size_t l = 0;
+
+                       if (!impl->decl) {
+                               continue;
+                       }
+                       if (!C->ops->decl_init(C, impl->decl)) {
+                               continue;
+                       }
+                       if (!C->ops->impl_init(C, impl, &zf->handler)) {
+                               continue;
+                       }
+                       while (psi_plist_get(impl->stmts.let, l++, &let)) {
+                               psi_context_callback_init(C, let->exp, impl);
+                       }
+
+                       zf->fname = impl->func->name->val + (impl->func->name->val[0] == '\\');
+                       zf->num_args = psi_plist_count(impl->func->args);
+                       zf->arg_info = psi_internal_arginfo(impl);
+                       ++nf;
+               }
+       }
+
+       C->closures = zfe;
+}
+
+static inline void psi_context_decls_init(struct psi_context *C)
+{
+       if (C->decls) {
+               size_t d = 0;
+               struct psi_decl *decl;
+
+               while (psi_plist_get(C->decls, d++, &decl)) {
+                       if (!decl->info) {
+                               C->ops->decl_init(C, decl);
+                       }
+               }
+       }
+}
+
+struct psi_struct_type_data {
+       struct psi_plist *els;
+       size_t offset;
+       size_t max_align;
+};
+
+static inline void psi_struct_type_pad(struct psi_context *C,
+               struct psi_struct_type_data *data, size_t padding)
+{
+       void *ele = C->ops->typeof_decl(C, PSI_T_INT8);
+
+       while (padding--) {
+               void *pad = C->ops->copyof_type(C, ele);
+               data->els = psi_plist_add(data->els, &pad);
+       }
+}
+
+static inline void psi_struct_type_element(struct psi_context *C,
+               struct psi_struct_type_data *data, struct psi_decl_arg *darg)
+{
+       void *type, *copy;
+       size_t i;
+       struct psi_layout type_layout;
+
+       if (darg->layout->pos) {
+               assert(data->offset <= darg->layout->pos);
+               psi_struct_type_pad(C, data, darg->layout->pos - data->offset);
+               data->offset = darg->layout->pos;
+       }
+
+       type = psi_context_decl_arg_full_type(C, darg);
+       C->ops->layoutof_type(C, type, &type_layout);
+
+       if (type_layout.pos > data->max_align) {
+               data->max_align = type_layout.pos;
+       }
+
+       assert(type_layout.len <= darg->layout->len);
+
+       for (i = 0; i < (darg->var->array_size ?: 1); ++i) {
+               copy = C->ops->copyof_type(C, type);
+               data->els = psi_plist_add(data->els, &copy);
+       }
+       assert(darg->layout->len == type_layout.len * (darg->var->array_size ?: 1));
+       data->offset += darg->layout->len;
+}
+
+static inline void psi_context_decl_struct_type_elements(struct psi_context *C,
+               struct psi_decl_struct *strct, struct psi_plist **els)
+{
+       size_t i = 0;
+       struct psi_decl_arg *darg, *prev = NULL;
+       struct psi_struct_type_data data = {0};
+
+       data.els = *els;
+       while (psi_plist_get(strct->args, i++, &darg)) {
+               if (prev && prev->layout->pos == darg->layout->pos) {
+                       /* skip bit fields */
+                       continue;
+               }
+               psi_struct_type_element(C, &data, darg);
+       }
+
+       data.offset = (data.offset + data.max_align - 1) & ~(data.max_align - 1);
+       assert(data.offset <= strct->size);
+       psi_struct_type_pad(C, &data, strct->size - data.offset);
+
+       *els = data.els;
+}
+
+static inline void *psi_context_decl_arg_type(struct psi_context *C,
+               struct psi_decl_arg *darg)
+{
+       struct psi_decl_type *real = psi_decl_type_get_real(darg->type);
+
+       if (real != darg->type && darg->type->real.def->var->pointer_level) {
+               return C->ops->typeof_decl(C, PSI_T_POINTER);
+       }
+
+       if (real->type == PSI_T_UNION) {
+               struct psi_decl_arg *arg;
+               psi_plist_get(real->real.unn->args, 0, &arg);
+               return psi_context_decl_arg_full_type(C, arg);
+       }
+
+       if (real->type == PSI_T_STRUCT) {
+               C->ops->composite_init(C, darg);
+               return darg->engine.type;
+       }
+
+       return C->ops->typeof_decl(C, real->type);
+}
+
+void *psi_context_decl_arg_call_type(struct psi_context *C,
+               struct psi_decl_arg *darg)
+{
+       if (darg->var->pointer_level) {
+               return C->ops->typeof_decl(C, PSI_T_POINTER);
+       }
+
+       return psi_context_decl_arg_type(C, darg);
+}
+
+void *psi_context_decl_arg_full_type(struct psi_context *C,
+               struct psi_decl_arg *darg)
+{
+       if (darg->var->array_size) {
+               C->ops->composite_init(C, darg);
+               return darg->engine.type;
+       }
+       if (darg->var->pointer_level) {
+               return C->ops->typeof_decl(C, PSI_T_POINTER);
+       }
+
+       return psi_context_decl_arg_type(C, darg);
+}
+
+void **psi_context_composite_type_elements(struct psi_context *C,
+               struct psi_decl_arg *darg, struct psi_plist **eles)
+{
+       struct psi_decl_type *dtype;
+       struct psi_decl_arg *tmp;
+       void *type, *copy;
+
+       dtype = psi_decl_type_get_real(darg->type);
+
+       switch (dtype->type) {
+       case PSI_T_STRUCT:
+               psi_context_decl_struct_type_elements(C, dtype->real.strct, eles);
+               break;
+       case PSI_T_UNION:
+               if (psi_plist_bottom(dtype->real.unn->args, &tmp)) {
+                       type = psi_context_decl_arg_full_type(C, tmp);
+                       copy = C->ops->copyof_type(C, type);
+                       *eles = psi_plist_add(*eles, &copy);
+               }
+               break;
+       default:
+               type = psi_context_decl_arg_type(C, darg);
+               for (size_t i = 0; i < darg->var->array_size; ++i) {
+                       copy = C->ops->copyof_type(C, type);
+                       *eles = psi_plist_add(*eles, &copy);
+               }
+       }
+
+       return psi_plist_eles(*eles);
+}
+
+/*
+void psi_context_decl_func_array_elements(struct psi_context *C,
+               struct psi_decl *fn, struct psi_plist **els)
+{
+       void *type;
+       size_t i;
+
+       if (fn->func->var->pointer_level > 1) {
+               type = C->ops->typeof_decl(C, PSI_T_POINTER);
+       } else {
+               type = psi_context_decl_type(C, fn->func->type);
+       }
+
+       for (i = 0; i < fn->func->var->array_size; ++i) {
+               void *copy = C->ops->copyof_type(C, type);
+               *els = psi_plist_add(*els, &copy);
+       }
+}
+
+void *psi_context_decl_func_type(struct psi_context *C, struct psi_decl *fn)
+{
+       struct psi_decl_arg *darg = fn->func;
+
+       if (darg->engine.type) {
+               return darg->engine.type;
+       }
+
+       if (darg->var->pointer_level) {
+               if (!darg->var->array_size) {
+                       return C->ops->typeof_decl(C, PSI_T_POINTER);
+               } else {
+                       C->ops->composite_init(C, darg);
+                       return darg->engine.type;
+               }
+       }
+
+       return psi_context_decl_type(C, darg->type);
+}
+*/
+
+void psi_context_compile(struct psi_context *C)
+{
+       psi_context_consts_init(C);
+       psi_context_extvars_init(C);
+       psi_context_impls_init(C);
+       psi_context_decls_init(C);
+
+       /* zend_register_functions depends on EG(current_module) pointing into module */
+       EG(current_module) = zend_hash_str_find_ptr(&module_registry, "psi", sizeof("psi") - 1);
+       if (SUCCESS != zend_register_functions(NULL, C->closures, NULL, MODULE_PERSISTENT)) {
+               C->error(PSI_DATA(C), NULL, PSI_WARNING, "Failed to register functions!");
+       }
+       EG(current_module) = NULL;
+}
 
 ZEND_RESULT_CODE psi_context_call(struct psi_context *C, zend_execute_data *execute_data, zval *return_value, struct psi_impl *impl)
 {
@@ -327,7 +632,11 @@ ZEND_RESULT_CODE psi_context_call(struct psi_context *C, zend_execute_data *exec
                return FAILURE;
        }
 
-       C->ops->call(frame);
+       if (psi_call_frame_num_var_args(frame)) {
+               C->ops->call_va(frame);
+       } else {
+               C->ops->call(frame);
+       }
 
        if (SUCCESS != psi_call_frame_do_assert(frame, PSI_ASSERT_POST)) {
                psi_call_frame_do_return(frame, return_value);
@@ -350,6 +659,48 @@ void psi_context_dtor(struct psi_context *C)
        size_t i;
        zend_function_entry *zfe;
 
+       if (C->decls) {
+               size_t i = 0;
+               struct psi_decl *decl;
+
+               while (psi_plist_get(C->decls, i++, &decl)) {
+                       size_t j = 0;
+                       struct psi_decl_arg *darg;
+
+                       while (psi_plist_get(decl->args, j++, &darg)) {
+                               C->ops->composite_dtor(C, darg);
+                       }
+                       C->ops->composite_dtor(C, decl->func);
+                       C->ops->decl_dtor(C, decl);
+               }
+
+       }
+       if (C->vars) {
+               size_t i = 0;
+               struct psi_decl_extvar *evar;
+
+               while (psi_plist_get(C->vars, i++, &evar)) {
+                       C->ops->composite_dtor(C, evar->getter->func);
+                       C->ops->composite_dtor(C, evar->arg);
+                       C->ops->extvar_dtor(C, evar);
+               }
+       }
+       if (C->impls) {
+               size_t i = 0;
+               struct psi_impl *impl;
+
+               while (psi_plist_get(C->impls, i++, &impl)) {
+                       struct psi_let_stmt *let;
+                       size_t j = 0;
+
+                       while (psi_plist_get(impl->stmts.let, j++, &let)) {
+                               psi_context_callback_dtor(C, let->exp, impl);
+                       }
+
+                       C->ops->impl_dtor(C, impl);
+               }
+       }
+
        if (C->ops->dtor) {
                C->ops->dtor(C);
        }
@@ -365,9 +716,9 @@ void psi_context_dtor(struct psi_context *C)
 
        if (C->closures) {
                for (zfe = C->closures; zfe->fname; ++zfe) {
-                       free((void *) zfe->arg_info);
+                       pefree((void *) zfe->arg_info, 1);
                }
-               free(C->closures);
+               pefree(C->closures, 1);
        }
 }
 
@@ -383,8 +734,7 @@ void psi_context_free(struct psi_context **C)
 void psi_context_dump(struct psi_dump *dump, struct psi_context *C)
 {
        PSI_DUMP(dump, "// psi.engine=%s\n// %lu files\n",
-                       (char *) C->ops->query(C, PSI_CONTEXT_QUERY_SELF, NULL),
-                       C->count);
+                       C->ops->name, C->count);
 
        psi_data_dump(dump, PSI_DATA(C));
 
index 248efbb..62d2a12 100644 (file)
@@ -26,6 +26,8 @@
 #ifndef PSI_CONTEXT_H
 #define PSI_CONTEXT_H
 
+#include "token.h"
+
 /* zend_function_entry */
 #include "Zend/zend_API.h"
 
@@ -33,23 +35,40 @@ struct psi_context;
 struct psi_token;
 struct psi_parser;
 struct psi_call_frame;
-struct impl_vararg;
 struct psi_decl;
 struct psi_impl;
-
-enum psi_context_query {
-       PSI_CONTEXT_QUERY_SELF, /* ffi/jit */
-       PSI_CONTEXT_QUERY_TYPE, /* impl type token_t to native ffi_type/jit_type */
-};
+struct psi_decl_arg;
+struct psi_decl_extvar;
+struct psi_let_exp;
 
 struct psi_context_ops {
-       ZEND_RESULT_CODE (*load)(void);
+       /* library */
+       const char *name;
+       /* called once */
+       bool (*load)(void);
        void (*free)(void);
-       void (*init)(struct psi_context *C);
+       /* called for each new context */
+       bool (*init)(struct psi_context *C);
        void (*dtor)(struct psi_context *C);
-       zend_function_entry *(*compile)(struct psi_context *C);
+       /* compiler */
+       bool (*composite_init)(struct psi_context *C, struct psi_decl_arg *darg);
+       void (*composite_dtor)(struct psi_context *C, struct psi_decl_arg *darg);
+       bool (*extvar_init)(struct psi_context *C, struct psi_decl_extvar *evar);
+       void (*extvar_dtor)(struct psi_context *C, struct psi_decl_extvar *evar);
+       bool (*decl_init)(struct psi_context *C, struct psi_decl *decl);
+       void (*decl_dtor)(struct psi_context *C, struct psi_decl *decl);
+       bool (*impl_init)(struct psi_context *C, struct psi_impl *impl, zif_handler *zh);
+       void (*impl_dtor)(struct psi_context *C, struct psi_impl *impl);
+       bool (*cb_init)(struct psi_context *C, struct psi_let_exp *cb, struct psi_impl *impl);
+       void (*cb_dtor)(struct psi_context *C, struct psi_let_exp *cb, struct psi_impl *impl);
+       /* calls */
        void (*call)(struct psi_call_frame *frame);
-       void *(*query)(struct psi_context *C, enum psi_context_query q, void *arg);
+       void (*call_va)(struct psi_call_frame *frame);
+       /* types */
+       void *(*typeof_impl)(struct psi_context *C, token_t impl_type);
+       void *(*typeof_decl)(struct psi_context *C, token_t decl_type);
+       void *(*copyof_type)(struct psi_context *C, void *orig_type);
+       void (*layoutof_type)(struct psi_context *C, void *orig_type, struct psi_layout *l);
 };
 
 #include "data.h"
@@ -73,7 +92,13 @@ struct psi_context_call_data {
 
 struct psi_context *psi_context_init(struct psi_context *C, struct psi_context_ops *ops, psi_error_cb error, unsigned flags);
 void psi_context_build(struct psi_context *C, const char *path);
-zend_function_entry *psi_context_compile(struct psi_context *C);
+void psi_context_compile(struct psi_context *C);
+
+void **psi_context_composite_type_elements(struct psi_context *C,
+               struct psi_decl_arg *darg, struct psi_plist **eles);
+void *psi_context_decl_arg_call_type(struct psi_context *C, struct psi_decl_arg *arg);
+void *psi_context_decl_arg_full_type(struct psi_context *C, struct psi_decl_arg *arg);
+
 ZEND_RESULT_CODE psi_context_call(struct psi_context *C, zend_execute_data *execute_data, zval *return_value, struct psi_impl *impl);
 void psi_context_dump(struct psi_dump *dump, struct psi_context *C);
 void psi_context_dtor(struct psi_context *C);
index 30210cf..6f808d4 100644 (file)
 
 #ifdef HAVE_LIBFFI
 
-#undef PACKAGE
-#undef PACKAGE_BUGREPORT
-#undef PACKAGE_NAME
-#undef PACKAGE_STRING
-#undef PACKAGE_TARNAME
-#undef PACKAGE_VERSION
-
-#include <ffi.h>
-
-#ifndef PSI_HAVE_FFI_CLOSURE_ALLOC
-# if HAVE_UNISTD_H
-#  include <unistd.h>
-# endif
-# if HAVE_SYS_MMAN_H
-#  include <sys/mman.h>
-#  ifndef MAP_ANONYMOUS
-#   define MAP_ANONYMOUS MAP_ANON
-#  endif
-# endif
+#include "libffi_compat.h"
+
+#if HAVE_INT128
+static ffi_type *ffi_type_sint128;
+static ffi_type *ffi_type_uint128;
 #endif
 
 struct psi_ffi_context {
@@ -56,14 +42,6 @@ struct psi_ffi_context {
        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;
@@ -74,72 +52,39 @@ struct psi_ffi_callback_info {
 
 struct psi_ffi_decl_info {
        ffi_cif signature;
-       ffi_type *rv_array;
+       struct psi_ffi_struct_info *rv_array;
        ffi_type *params[1];
 };
 
-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"
-#endif
-}
+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;
+};
 
-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_impl_info {
+       struct psi_context *context;
+       struct psi_call_frame *frame;
 
-#if PSI_HAVE_FFI_PREP_CLOSURE_LOC
-       return ffi_prep_closure_loc(*closure, sig, handler, data, *code);
+       void *code;
+       ffi_closure *closure;
+};
 
-#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
-}
+struct psi_ffi_struct_info {
+       ffi_type strct;
+       struct psi_plist *eles;
+};
 
-static void psi_ffi_closure_free(void *c)
+static inline ffi_type *psi_ffi_token_type(token_t t)
 {
-#ifdef PSI_HAVE_FFI_CLOSURE_ALLOC
-       ffi_closure_free(c);
-#elif HAVE_MMAP
-       munmap(c, sizeof(ffi_closure));
-#endif
-}
-
-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;
-
-#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);
-}
-
-#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:
                assert(0);
@@ -185,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;
@@ -200,204 +146,10 @@ static inline ffi_type *psi_ffi_impl_type(token_t impl_type) {
        }
        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 = pemalloc(sizeof(*pad), 1);
-
-               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;
-       size_t padding;
-
-       if (darg->layout->pos == s->last_arg_pos) {
-               /* skip bit fields */
-               return;
-       }
-       s->last_arg_pos = darg->layout->pos;
-
-       type = pemalloc(sizeof(*type), 1);
-       *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;
-                       s->els = safe_perealloc(s->els, (s->argc + 1), sizeof(*s->els), 0, 1);
-                       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;
-       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 = pecalloc(s.argc + 1, sizeof(*s.els), 1);
-
-       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;
-
-               s.els = safe_perealloc(s.els, (padding + s.argc + 1), sizeof(*s.els), 0, 1);
-               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 = pecalloc(1, sizeof(ffi_type), 1);
-
-                       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 = pecalloc(s.argc + 1, sizeof(*s.els), 1);
-
-       info->rv_array = pecalloc(1, sizeof(ffi_type), 1);
-       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_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_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);
-       }
+static void psi_ffi_type_free(ffi_type **typ_ptr)
+{
+       pefree(*typ_ptr, 1);
 }
 
 static inline ffi_abi psi_ffi_abi(zend_string *convention) {
@@ -416,44 +168,6 @@ static inline ffi_abi psi_ffi_abi(zend_string *convention) {
        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 = pecalloc(1, sizeof(*info) + 2 * c * sizeof(void *), 1);
-
-               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;
@@ -477,138 +191,125 @@ static void psi_ffi_callback(ffi_cif *sig, void *result, void **args, void *data
        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 = pecalloc(1, sizeof(*cb_info), 1);
-               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);
+static bool psi_ffi_load()
+{
+#if HAVE_INT128
+       ffi_type *i128, *u128;
 
-               if (FFI_OK != rc) {
-                       free(cb_info);
-                       break;
-               }
-               cb->info = cb_info;
+       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;
 
-               assert(!cb->decl->sym);
-               cb->decl->sym = cb_info->code;
-               fn = cb->func;
-               /* no break */
+       ffi_type_sint128 = i128;
 
-       case PSI_LET_FUNC:
-               if (!fn) {
-                       fn = let_exp->data.func;
-               }
-               if (fn->inner) {
-                       size_t i = 0;
-                       struct psi_let_exp *inner_let;
+       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;
 
-                       while (psi_plist_get(fn->inner, i++, &inner_let)) {
-                               psi_ffi_callback_init(impl_info, inner_let);
-                       }
-               }
-               break;
-       default:
-               break;
-       }
+       ffi_type_uint128 = u128;
+#endif
+       return true;
 }
 
-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;
+static void psi_ffi_free()
+{
+#if HAVE_INT128
+       free(ffi_type_sint128);
+       free(ffi_type_uint128);
+#endif
+}
 
-               psi_ffi_decl_dtor(cb->decl);
+static bool psi_ffi_init(struct psi_context *C)
+{
+       ffi_status rc;
+       struct psi_ffi_context *context = pecalloc(1, sizeof(*context), 1);
 
-               if (cb->info) {
-                       struct psi_ffi_callback_info *info = cb->info;
+       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);
 
-                       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(context, 1);
+               return false;
+       }
 
-               if (fn->inner) {
-                       size_t i = 0;
-                       struct psi_let_exp *cb;
+       C->context = context;
+       return true;
+}
 
-                       while (psi_plist_get(fn->inner, i++, &cb)) {
-                               psi_ffi_callback_dtor(cb);
-                       }
-               }
-               break;
-       default:
-               break;
+static void psi_ffi_dtor(struct psi_context *C)
+{
+       if (C->context) {
+               pefree(C->context, 1);
+               C->context = NULL;
        }
 }
 
-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 = pecalloc(1, sizeof(*info), 1);
-       struct psi_let_stmt *let;
-       ffi_status rc;
-       size_t l = 0;
+static bool psi_ffi_composite_init(struct psi_context *C,
+               struct psi_decl_arg *darg)
+{
+       struct psi_ffi_struct_info *info;
 
-       info->context = C;
+       if (darg->engine.type) {
+               return true;
+       }
 
-       rc = psi_ffi_prep_closure(&info->closure, &info->code,
-                       &context->signature, psi_ffi_handler, impl);
+       info = pecalloc(1, sizeof(*info), 1);
+       info->eles = psi_plist_init((psi_plist_dtor) psi_ffi_type_free);
 
-       if (FFI_OK != rc) {
-               free(info);
-               return NULL;
-       }
+       psi_context_composite_type_elements(C, darg, &info->eles);
 
-       while (psi_plist_get(impl->stmts.let, l++, &let)) {
-               psi_ffi_callback_init(info, let->exp);
+       /* add terminating NULL; libffi structs do not have an element count */
+       {
+               void *null_ptr = NULL;
+               info->eles = psi_plist_add(info->eles, &null_ptr);
        }
 
-       return impl->info = info;
-}
+       info->strct.type = FFI_TYPE_STRUCT;
+       info->strct.alignment = 0;
+       info->strct.size = 0;
+       info->strct.elements = (ffi_type **) psi_plist_eles(info->eles);
 
-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;
+       darg->engine.info = info;
+       darg->engine.type = &info->strct;
 
-       while (psi_plist_get(impl->stmts.let, j++, &let)) {
-               psi_ffi_callback_dtor(let->exp);
-       }
+       return true;
+}
+
+static void psi_ffi_composite_dtor(struct psi_context *C,
+               struct psi_decl_arg *darg)
+{
+       struct psi_ffi_struct_info *info = darg->engine.info;
 
        if (info) {
-               if (info->closure) {
-                       psi_ffi_closure_free(info->closure);
+               darg->engine.info = NULL;
+               darg->engine.type = NULL;
+
+               struct psi_plist *args = NULL;
+               struct psi_decl_type *dtype = psi_decl_type_get_real(darg->type);
+
+               if (dtype->type == PSI_T_STRUCT) {
+                       args = dtype->real.strct->args;
+               } else if (dtype->type == PSI_T_UNION) {
+                       args = dtype->real.unn->args;
                }
-               free(info);
-               impl->info = NULL;
+
+               size_t i = 0;
+               struct psi_decl_arg *tmp;
+
+               while (psi_plist_get(args, i++, &tmp)) {
+                       psi_ffi_composite_dtor(C, tmp);
+               }
+
+               psi_plist_free(info->eles);
+               pefree(info, 1);
        }
 }
 
@@ -624,174 +325,185 @@ static void psi_ffi_extvar_set(ffi_cif *sig, void *result, void **args, void *da
        psi_decl_extvar_set(evar, args[0]);
 }
 
-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;
-};
+static bool psi_ffi_decl_init(struct psi_context *, struct psi_decl *);
 
-static inline ffi_status psi_ffi_extvar_init(struct psi_decl_extvar *evar) {
+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;
 
        evar->info = info;
 
-       psi_ffi_decl_init(evar->getter);
-       psi_ffi_decl_init(evar->setter);
+       psi_ffi_decl_init(C, evar->getter);
+       psi_ffi_decl_init(C, evar->setter);
 
        rc = ffi_prep_cif(&info->get.signature, FFI_DEFAULT_ABI, 0,
-                       psi_ffi_decl_func_type(evar->getter), NULL);
+                       psi_context_decl_arg_call_type(C, evar->getter->func), NULL);
        if (FFI_OK != rc) {
-               return 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 rc;
+               return false;
        }
 
-       info->set.params[0] = psi_ffi_decl_arg_type(evar->arg);
+       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 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 rc;
+               return false;
        }
 
        evar->getter->sym = info->get.code;
        evar->setter->sym = info->set.code;
 
-       return FFI_OK;
+       return true;
 }
 
-static inline void psi_ffi_extvar_dtor(struct psi_decl_extvar *evar) {
+static void psi_ffi_extvar_dtor(struct psi_context *C,
+               struct psi_decl_extvar *evar) {
        if (evar->info) {
-               free(evar->info);
+               pefree(evar->info, 1);
                evar->info = NULL;
        }
 }
 
-static inline struct psi_ffi_context *psi_ffi_context_init(struct psi_ffi_context *L) {
-       ffi_status rc;
+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);
 
-       if (!L) {
-               L = pemalloc(sizeof(*L), 1);
-       }
-       memset(L, 0, sizeof(*L));
+               decl->info = info;
 
-       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);
+               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;
 
-       return L;
-}
+               rc = ffi_prep_cif(&info->signature, psi_ffi_abi(decl->abi->convention),
+                               c, psi_context_decl_arg_call_type(C, decl->func), info->params);
 
-static inline void psi_ffi_context_free(struct psi_ffi_context **L) {
-       if (*L) {
-               free(*L);
-               *L = NULL;
+               if (FFI_OK != rc) {
+                       pefree(info, 1);
+                       decl->info = NULL;
+                       return false;
+               }
        }
+
+       return true;
 }
 
-static void psi_ffi_init(struct psi_context *C)
+static void psi_ffi_decl_dtor(struct psi_context *C,
+               struct psi_decl *decl)
 {
-       C->context = psi_ffi_context_init(NULL);
+       if (decl->info) {
+               pefree(decl->info, 1);
+               decl->info = NULL;
+       }
 }
 
-static void psi_ffi_dtor(struct psi_context *C)
+static bool psi_ffi_impl_init(struct psi_context *C,
+               struct psi_impl *impl, zif_handler *zh)
 {
-       if (C->decls) {
-               size_t i = 0;
-               struct psi_decl *decl;
+       struct psi_ffi_context *context = C->context;
+       struct psi_ffi_impl_info *info = pecalloc(1, sizeof(*info), 1);
+       ffi_status rc;
 
-               while (psi_plist_get(C->decls, i++, &decl)) {
-                       psi_ffi_decl_dtor(decl);
-               }
+       impl->info = info;
+       info->context = C;
 
-       }
-       if (C->vars) {
-               size_t i = 0;
-               struct psi_decl_extvar *evar;
+       rc = psi_ffi_prep_closure(&info->closure, &info->code,
+                       &context->signature, psi_ffi_handler, impl);
 
-               while (psi_plist_get(C->vars, i++, &evar)) {
-                       psi_ffi_extvar_dtor(evar);
-               }
+       if (FFI_OK != rc) {
+               goto failure;
        }
-       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);
-}
+       *zh = info->code;
+       return true;
 
+failure: ;
+       impl->info = NULL;
+       free(info);
+       return false;
+}
 
-static zend_function_entry *psi_ffi_compile(struct psi_context *C)
+static void psi_ffi_impl_dtor(struct psi_context *C, struct psi_impl *impl)
 {
-       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)) {
-                       /* */
+       struct psi_ffi_impl_info *info = impl->info;
+
+       if (info) {
+               if (info->closure) {
+                       psi_ffi_closure_free(info->closure);
                }
+               free(info);
+               impl->info = NULL;
        }
+}
 
-       if (C->impls) {
-               zfe = pecalloc(psi_plist_count(C->impls) + 1, sizeof(*zfe), 1);
-
-               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->val + (impl->func->name->val[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;
-               }
+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;
+
+       assert(exp->kind == PSI_LET_CALLBACK);
+
+       if (!psi_ffi_decl_init(C, exp->data.callback->decl)) {
+               return false;
        }
 
-       while (psi_plist_get(C->decls, d++, &decl)) {
-               if (decl->info) {
-                       continue;
-               }
+       cb_info = pecalloc(1, sizeof(*cb_info), 1);
+       cb_info->impl_info = impl->info;
+       cb_info->let_exp = exp;
 
-               psi_ffi_decl_init(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;
        }
 
-       return zfe;
+       assert(!exp->data.callback->decl->sym);
+       exp->data.callback->info = cb_info;
+       exp->data.callback->decl->sym = cb_info->code;
+
+       return true;
+}
+
+static void psi_ffi_cb_dtor(struct psi_context *C,
+               struct psi_let_exp *let_exp, struct psi_impl *impl)
+{
+       assert(let_exp->kind == PSI_LET_CALLBACK);
+
+       psi_ffi_decl_dtor(C, let_exp->data.callback->decl);
+
+       if (let_exp->data.callback->info) {
+               struct psi_ffi_callback_info *info = let_exp->data.callback->info;
+
+               if (info->closure) {
+                       psi_ffi_closure_free(info->closure);
+               }
+               pefree(info, 1);
+               let_exp->data.callback->info = NULL;
+       }
 }
 
-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;
@@ -811,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);
@@ -849,66 +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_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 void *psi_ffi_typeof_decl(struct psi_context *C, token_t decl_type)
+{
+       return psi_ffi_token_type(decl_type);
 }
 
-static ZEND_RESULT_CODE psi_ffi_load()
+static void *psi_ffi_copyof_type(struct psi_context *C, void *orig_type)
 {
-#if HAVE_INT128
-       ffi_type *i128, *u128;
+       ffi_type *type = pemalloc(sizeof(*type), 1);
 
-       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 SUCCESS;
+       *type = *(ffi_type *) orig_type;
+       return type;
 }
 
-static void psi_ffi_free()
+static void psi_ffi_layoutof_type(struct psi_context *C, void *orig_type,
+               struct psi_layout *l)
 {
-#if HAVE_INT128
-       free(ffi_type_sint128);
-       free(ffi_type_uint128);
-#endif
+       ffi_type *type = orig_type;
+
+       if (!type->size || !type->alignment) {
+               ffi_cif tmp;
+               ffi_prep_cif(&tmp, FFI_DEFAULT_ABI, 0, type, 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)
diff --git a/src/libffi_compat.h b/src/libffi_compat.h
new file mode 100644 (file)
index 0000000..f939c86
--- /dev/null
@@ -0,0 +1,108 @@
+/*******************************************************************************
+ Copyright (c) 2018, 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.
+*******************************************************************************/
+
+#ifndef PSI_LIBFFI_COMPAT_H
+#define PSI_LIBFFI_COMPAT_H
+
+#include "php_psi_stdinc.h"
+
+#undef PACKAGE
+#undef PACKAGE_BUGREPORT
+#undef PACKAGE_NAME
+#undef PACKAGE_STRING
+#undef PACKAGE_TARNAME
+#undef PACKAGE_VERSION
+
+#include <ffi.h>
+
+#ifndef PSI_HAVE_FFI_CLOSURE_ALLOC
+# if HAVE_UNISTD_H
+#  include <unistd.h>
+# endif
+# if HAVE_SYS_MMAN_H
+#  include <sys/mman.h>
+#  ifndef MAP_ANONYMOUS
+#   define MAP_ANONYMOUS MAP_ANON
+#  endif
+# endif
+#endif
+
+static inline 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"
+#endif
+}
+
+static inline 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"
+       abort(); /* fix IDE warning */
+#endif
+}
+
+static inline 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
+}
+
+static inline 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;
+
+#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);
+}
+
+
+#endif /* LIBFFI_COMPAT_H */
index 22266ba..0b4989b 100644 (file)
@@ -37,7 +37,47 @@ static jit_type_t jit_type_llong;
 static jit_type_t jit_type_ullong;
 #endif
 
-static inline jit_type_t psi_jit_decl_arg_type(struct psi_decl_arg *darg);
+struct psi_jit_context {
+       jit_context_t jit;
+       jit_type_t signature;
+};
+
+struct psi_jit_callback_info {
+       struct psi_jit_impl_info *impl_info;
+       struct psi_let_exp *let_exp;
+
+       void *closure;
+};
+
+struct psi_jit_decl_info {
+       jit_type_t signature;
+       struct psi_jit_struct_info *rv_array;
+       void *params[1];
+};
+
+struct psi_jit_extvar_info {
+       struct {
+               jit_type_t signature;
+               void *closure;
+       } get;
+       struct {
+               jit_type_t signature;
+               jit_type_t params[1];
+               void *closure;
+       } set;
+};
+
+struct psi_jit_impl_info {
+       struct psi_context *context;
+       struct psi_call_frame *frame;
+
+       void *closure;
+};
+
+struct psi_jit_struct_info {
+       jit_type_t strct;
+       struct psi_plist *eles;
+};
 
 static inline jit_type_t psi_jit_token_type(token_t t)
 {
@@ -98,479 +138,351 @@ static inline jit_type_t psi_jit_impl_type(token_t impl_type)
        case PSI_T_FLOAT:
        case PSI_T_DOUBLE:
                return jit_type_sys_double;
-       EMPTY_SWITCH_DEFAULT_CASE()
-               ;
+       EMPTY_SWITCH_DEFAULT_CASE();
        }
        return NULL;
 }
 
-struct psi_jit_struct_type {
-       jit_type_t strct;
-       jit_type_t *fields;
-};
-
-static void psi_jit_struct_type_dtor(void *ptr)
+static void psi_jit_type_free(jit_type_t *typ_ptr)
 {
-       struct psi_jit_struct_type *type = ptr;
-       unsigned i, n = jit_type_num_fields(type->strct);
-
-       for (i = 0; i < n; ++i) {
-               jit_type_free(jit_type_get_field(type->strct, i));
-       }
-       jit_type_free(type->strct);
-       free(type->fields);
-       free(type);
+       jit_type_free(*typ_ptr);
 }
 
-static size_t psi_jit_struct_type_pad(jit_type_t *els, size_t padding)
+static inline jit_abi_t psi_jit_abi(zend_string *convention)
 {
-       size_t i;
-
-       for (i = 0; i < padding; ++i) {
-               *els++ = jit_type_copy(jit_type_sys_char);
+       if (zend_string_equals_literal(convention, "stdcall")) {
+               return jit_abi_stdcall;
        }
-
-       return padding;
+       if (zend_string_equals_literal(convention, "fastcall")) {
+               return jit_abi_fastcall;
+       }
+       return jit_abi_cdecl;
 }
 
-static unsigned psi_jit_struct_type_elements(struct psi_decl_struct *strct,
-               jit_type_t **fields)
+static void psi_jit_handler(jit_type_t sig, void *result, void **args, void *data)
 {
-       size_t i = 0, argc = psi_plist_count(strct->args), nels = 0, offset = 0,
-                       maxalign = 0, last_arg_pos = -1;
-       struct psi_decl_arg *darg;
-       jit_type_t *tmp;
-
-       *fields = calloc(argc + 1, sizeof(*fields));
-
-       while (psi_plist_get(strct->args, i++, &darg)) {
-               jit_type_t type;
-               size_t padding, alignment;
-
-               if (darg->layout->pos == last_arg_pos) {
-                       /* skip bit fields */
-                       continue;
-               }
-               last_arg_pos = darg->layout->pos;
-
-               type = jit_type_copy(psi_jit_decl_arg_type(darg));
-
-               if ((alignment = jit_type_get_alignment(type)) > maxalign) {
-                       maxalign = alignment;
-               }
-
-               assert(jit_type_get_size(type) <= darg->layout->len);
-               if ((padding = psi_offset_padding(darg->layout->pos - offset, alignment))) {
-                       if (nels + padding > argc) {
-                               argc += padding;
-                               tmp = realloc(*fields, (argc + 1) * sizeof(*fields));
-                               if (tmp) {
-                                       *fields = tmp;
-                               } else {
-                                       free(*fields);
-                                       return 0;
-                               }
-                       }
-                       psi_jit_struct_type_pad(&(*fields)[nels], padding);
-                       nels += padding;
-                       offset += padding;
-               }
-               assert(offset == darg->layout->pos);
-
-               offset = (offset + darg->layout->len + alignment - 1)
-                               & ~(alignment - 1);
-               (*fields)[nels++] = type;
-       }
-
-       /* apply struct alignment padding */
-       offset = (offset + maxalign - 1) & ~(maxalign - 1);
-
-       assert(offset <= strct->size);
-       if (offset < strct->size) {
-               nels += psi_jit_struct_type_pad(&(*fields)[nels], strct->size - offset);
-       }
+       struct psi_impl *impl = data;
+       struct psi_jit_impl_info *info = impl->info;
 
-       return nels;
+       psi_context_call(info->context, *(zend_execute_data **)args[0], *(zval **) args[1], impl);
 }
-static inline jit_type_t psi_jit_decl_type(struct psi_decl_type *type)
+
+static void psi_jit_callback(jit_type_t sig, void *result, void **args, void *data)
 {
-       struct psi_decl_type *real = psi_decl_type_get_real(type);
+       struct psi_jit_callback_info *cb_info = data;
+       struct psi_call_frame_callback cb_data;
 
-       switch (real->type) {
-       case PSI_T_STRUCT:
-               if (!real->real.strct->engine.type) {
-                       unsigned count;
-                       struct psi_jit_struct_type *type = calloc(1, sizeof(*type));
+       assert(cb_info->impl_info->frame);
 
-                       count = psi_jit_struct_type_elements(real->real.strct, &type->fields);
-                       type->strct = jit_type_create_struct(type->fields, count, 0);
+       cb_data.cb = cb_info->let_exp;
+       cb_data.argc = jit_type_num_params(sig);
+       cb_data.argv = args;
+       cb_data.rval = result;
 
-                       real->real.strct->engine.type = type;
-                       real->real.strct->engine.dtor = psi_jit_struct_type_dtor;
-               }
+       psi_call_frame_do_callback(cb_info->impl_info->frame, &cb_data);
+}
 
-               return ((struct psi_jit_struct_type *) real->real.strct->engine.type)->strct;
+static bool psi_jit_load(void)
+{
+#if HAVE_INT128
+       jit_type_t ll_fields[2], ull_fields[2];
 
-       case PSI_T_UNION:
-               {
-                       struct psi_decl_arg *arg;
-                       psi_plist_get(real->real.unn->args, 0, &arg);
-                       return psi_jit_decl_arg_type(arg);
-               }
+       ll_fields[0] = ll_fields[1] = jit_type_long;
+       jit_type_llong = jit_type_create_struct(ll_fields, 2, 1);
 
-       default:
-               return psi_jit_token_type(real->type);
-       }
+       ull_fields[0] = ull_fields[1] = jit_type_ulong;
+       jit_type_ullong = jit_type_create_struct(ull_fields, 2, 1);
+#endif
+       return true;
 }
-static inline jit_type_t psi_jit_decl_arg_type(struct psi_decl_arg *darg)
+
+static void psi_jit_free(void)
 {
-       if (darg->var->pointer_level) {
-               return jit_type_void_ptr;
-       } else {
-               return psi_jit_decl_type(darg->type);
-       }
+#if HAVE_INT128
+       jit_type_free(jit_type_llong);
+       jit_type_free(jit_type_ullong);
+#endif
 }
 
-static inline jit_abi_t psi_jit_abi(zend_string *convention)
+static bool psi_jit_init(struct psi_context *C)
 {
-       if (zend_string_equals_literal(convention, "stdcall")) {
-               return jit_abi_stdcall;
-       }
-       if (zend_string_equals_literal(convention, "fastcall")) {
-               return jit_abi_fastcall;
+       struct psi_jit_context *context = pecalloc(1, sizeof(*context), 1);
+       jit_type_t params[] = {jit_type_void_ptr, jit_type_void_ptr};
+
+       context->jit = jit_context_create();
+       if (!context->jit) {
+               pefree(context, 1);
+               return false;
        }
-       return jit_abi_cdecl;
-}
 
-struct psi_jit_context {
-       jit_context_t jit;
-       jit_type_t signature;
-};
+       context->signature = jit_type_create_signature(jit_abi_cdecl, jit_type_void,
+                       params, 2, 1);
+       if (!context->signature) {
+               jit_context_destroy(context->jit);
+               pefree(context, 1);
+               return false;
+       }
 
-struct psi_jit_impl_info {
-       struct psi_context *context;
-       struct psi_call_frame *frame;
+       C->context = context;
+       return true;
+}
 
-       void *closure;
-};
+static void psi_jit_dtor(struct psi_context *C)
+{
+       if (C->context) {
+               struct psi_jit_context *context = C->context;
 
-struct psi_jit_callback_info {
-       struct psi_jit_impl_info *impl_info;
-       struct psi_let_exp *let_exp;
+               jit_type_free(context->signature);
+               jit_context_destroy(context->jit);
 
-       void *closure;
-};
+               pefree(C->context, 1);
+               C->context = NULL;
+       }
+}
 
-struct psi_jit_decl_info {
-       jit_type_t signature;
-       void *params[1];
-};
 
-static inline struct psi_jit_decl_info *psi_jit_decl_init(struct psi_decl *decl) {
-       if (!decl->info) {
-               size_t i, c = psi_plist_count(decl->args);
-               struct psi_decl_arg *arg;
-               struct psi_jit_decl_info *info = calloc(1, sizeof(*info) + 2 * c * sizeof(void *));
+static bool psi_jit_composite_init(struct psi_context *C,
+               struct psi_decl_arg *darg)
+{
+       struct psi_jit_struct_info *info;
 
-               for (i = 0; psi_plist_get(decl->args, i, &arg); ++i) {
-                       info->params[i] = psi_jit_decl_arg_type(arg);
-               }
-               info->params[c] = NULL;
+       if (darg->engine.type) {
+               return true;
+       }
 
-               info->signature = jit_type_create_signature(
-                               psi_jit_abi(decl->abi->convention),
-                               psi_jit_decl_arg_type(decl->func),
-                               (jit_type_t *) info->params,
-                               c, 1);
+       info = pecalloc(1, sizeof(*info), 1);
+       info->eles = psi_plist_init((psi_plist_dtor) psi_jit_type_free);
+       info->strct = jit_type_create_struct((jit_type_t *)
+                       psi_context_composite_type_elements(C, darg, &info->eles),
+                       psi_plist_count(info->eles), 0);
 
-               if (!info->signature) {
-                       free(info);
-               } else {
-                       decl->info = info;
-               }
-       }
+       darg->engine.info = info;
+       darg->engine.type = info->strct;
 
-       return decl->info;
+       return true;
 }
 
-static inline void psi_jit_decl_dtor(struct psi_decl *decl) {
-       if (decl->info) {
-               struct psi_jit_decl_info *info = decl->info;
+static void psi_jit_composite_dtor(struct psi_context *C,
+               struct psi_decl_arg *darg)
+{
+       struct psi_jit_struct_info *info = darg->engine.info;
 
-               jit_type_free(info->signature);
-               free(info);
-               decl->info = NULL;
+       if (info) {
+               darg->engine.info = NULL;
+               darg->engine.type = NULL;
+
+               jit_type_free(info->strct);
+               psi_plist_free(info->eles);
+               pefree(info, 1);
        }
 }
 
-static void psi_jit_handler(jit_type_t sig, void *result, void **args, void *data)
+static void psi_jit_extvar_get(jit_type_t sig, void *result, void **args, void *data)
 {
-       struct psi_impl *impl = data;
-       struct psi_jit_impl_info *info = impl->info;
+       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_jit_callback(jit_type_t sig, void *result, void **args, void *data)
+static void psi_jit_extvar_set(jit_type_t sig, void *result, void **args, void *data)
 {
-       struct psi_jit_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 = jit_type_num_params(sig);
-       cb_data.argv = args;
-       cb_data.rval = result;
+       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_jit_callback_init(struct psi_jit_impl_info *impl_info,
-               struct psi_let_exp *let_exp)
+static bool psi_jit_decl_init(struct psi_context *, struct psi_decl *);
+
+static bool psi_jit_extvar_init(struct psi_context *C,
+               struct psi_decl_extvar *evar)
 {
-       struct psi_jit_context *context = impl_info->context->context;
-       struct psi_jit_callback_info *cb_info;
-       struct psi_jit_decl_info *decl_info;
-       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->info) {
-                       decl_info = cb->decl->info;
-               } else {
-                       decl_info = psi_jit_decl_init(cb->decl);
-               }
+       struct psi_jit_context *ctx = C->context;
+       struct psi_jit_extvar_info *info = pecalloc(1, sizeof(*info), 1);
 
-               cb_info = calloc(1, sizeof(*cb_info));
-               cb_info->impl_info = impl_info;
-               cb_info->let_exp = let_exp;
-               cb_info->closure = jit_closure_create(context->jit, decl_info->signature,
-                               &psi_jit_callback, cb_info);
+       evar->info = info;
 
-               if (!cb_info->closure) {
-                       free(cb_info);
-                       break;
-               }
-               cb->info = cb_info;
+       psi_jit_decl_init(C, evar->getter);
+       psi_jit_decl_init(C, evar->setter);
 
-               assert(!cb->decl->sym);
-               cb->decl->sym = cb_info->closure;
-               fn = cb->func;
-               /* no break */
+       jit_context_build_start(ctx->jit);
 
-       case PSI_LET_FUNC:
-               if (!fn) {
-                       fn = let_exp->data.func;
-               }
-               if (fn->inner) {
-                       size_t i = 0;
-                       struct psi_let_exp *inner_let;
+       info->get.signature = jit_type_create_signature(jit_abi_cdecl,
+                       psi_context_decl_arg_full_type(C, evar->getter->func), NULL, 0, 1);
+       if (!info->get.signature) {
+               goto failure;
+       }
+       info->get.closure = jit_closure_create(ctx->jit, info->get.signature,
+                       psi_jit_extvar_get, evar);
+       if (!info->get.closure) {
+               goto failure;
+       }
 
-                       while (psi_plist_get(fn->inner, i++, &inner_let)) {
-                               psi_jit_callback_init(impl_info, inner_let);
-                       }
-               }
-               break;
-       default:
-               break;
+       info->set.params[0] = psi_context_decl_arg_call_type(C, evar->arg);
+       info->set.signature = jit_type_create_signature(jit_abi_cdecl,
+                       psi_context_decl_arg_full_type(C, evar->setter->func),
+                       info->set.params, 1, 1);
+       if (!info->set.signature) {
+               goto failure;
+       }
+       info->set.closure = jit_closure_create(ctx->jit, info->set.signature,
+                       psi_jit_extvar_set, evar);
+       if (!info->set.closure) {
+               goto failure;
        }
+
+       evar->getter->sym = info->get.closure;
+       evar->setter->sym = info->set.closure;
+
+       jit_context_build_end(ctx->jit);
+       return true;
+failure: ;
+       jit_context_build_end(ctx->jit);
+       return false;
 }
 
-static inline void psi_jit_callback_dtor(struct psi_let_exp *let_exp) {
-       struct psi_let_callback *cb;
-       struct psi_let_func *fn = NULL;
+static void psi_jit_extvar_dtor(struct psi_context *C,
+               struct psi_decl_extvar *evar) {
+       if (evar->info) {
+               pefree(evar->info, 1);
+               evar->info = NULL;
+       }
+}
 
-       switch (let_exp->kind) {
-       case PSI_LET_CALLBACK:
-               cb = let_exp->data.callback;
+static bool psi_jit_decl_init(struct psi_context *C, struct psi_decl *decl)
+{
+       if (!decl->info) {
+               struct psi_jit_context *ctx = C->context;
+               size_t i, c = psi_plist_count(decl->args);
+               struct psi_decl_arg *arg;
+               struct psi_jit_decl_info *info = pecalloc(1,
+                               sizeof(*info) + 2 * c * sizeof(void *), 1);
 
-               psi_jit_decl_dtor(cb->decl);
+               decl->info = info;
 
-               if (cb->info) {
-                       struct psi_jit_callback_info *info = cb->info;
+               jit_context_build_start(ctx->jit);
 
-                       if (info->closure) {
-                               /* The memory for the closure will be reclaimed when the context is destroyed.
-                               free(info->closure); */
-                       }
-                       free(info);
-                       cb->info = NULL;
-               }
-               fn = cb->func;
-               /* no break */
-       case PSI_LET_FUNC:
-               if (!fn) {
-                       fn = let_exp->data.func;
+               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;
+
+               info->signature = jit_type_create_signature(
+                               psi_jit_abi(decl->abi->convention),
+                               psi_context_decl_arg_full_type(C, decl->func),
+                               (jit_type_t *) info->params, c, 1);
 
-               if (fn->inner) {
-                       size_t i = 0;
-                       struct psi_let_exp *cb;
+               jit_context_build_end(ctx->jit);
 
-                       while (psi_plist_get(fn->inner, i++, &cb)) {
-                               psi_jit_callback_dtor(cb);
-                       }
+               if (!info->signature) {
+                       pefree(info, 1);
+                       decl->info = NULL;
+                       return false;
                }
-               break;
-       default:
-               break;
+       }
+
+       return true;
+}
+
+static void psi_jit_decl_dtor(struct psi_context *C, struct psi_decl *decl) {
+       if (decl->info) {
+               struct psi_jit_decl_info *info = decl->info;
+
+               jit_type_free(info->signature);
+               pefree(info, 1);
+               decl->info = NULL;
        }
 }
 
-static inline struct psi_jit_impl_info *psi_jit_impl_init(struct psi_impl * impl,
-               struct psi_context *C)
+static bool psi_jit_impl_init(struct psi_context *C,
+               struct psi_impl *impl, zif_handler *zh)
 {
        struct psi_jit_context *context = C->context;
        struct psi_jit_impl_info *info = calloc(1, sizeof(*info));
-       struct psi_let_stmt *let;
-       size_t l = 0;
 
+       impl->info = info;
        info->context = C;
+
        info->closure = jit_closure_create(context->jit, context->signature,
                        &psi_jit_handler, impl);
 
        if (!info->closure) {
-               free(info);
-               return NULL;
+               goto failure;
        }
 
-       while (psi_plist_get(impl->stmts.let, l++, &let)) {
-               psi_jit_callback_init(info, let->exp);
-       }
+       *zh = info->closure;
+       return true;
 
-       return impl->info = info;
+failure: ;
+       impl->info = NULL;
+       pefree(info, 1);
+       return false;
 }
 
 
-static inline void psi_jit_impl_dtor(struct psi_impl *impl) {
+static void psi_jit_impl_dtor(struct psi_context *C, struct psi_impl *impl) {
        struct psi_jit_impl_info *info = impl->info;
-       struct psi_let_stmt *let;
-       size_t j = 0;
-
-       while (psi_plist_get(impl->stmts.let, j++, &let)) {
-               psi_jit_callback_dtor(let->exp);
-       }
 
        if (info) {
-               if (info->closure) {
-                       /* The memory for the closure will be reclaimed when the context is destroyed.
-                       free(info->closure); */
-               }
-               free(info);
+               /* The memory for the closure will be reclaimed
+                * when the context is destroyed.
+                */
+               pefree(info, 1);
                impl->info = NULL;
        }
 }
 
-static inline struct psi_jit_context *psi_jit_context_init(
-               struct psi_jit_context *L)
+static bool psi_jit_cb_init(struct psi_context *C,
+               struct psi_let_exp *exp, struct psi_impl *impl)
 {
-       jit_type_t params[] = {jit_type_void_ptr, jit_type_void_ptr};
-
-       if (!L) {
-               L = malloc(sizeof(*L));
-       }
-       memset(L, 0, sizeof(*L));
-
-       L->jit = jit_context_create();
-       L->signature = jit_type_create_signature(jit_abi_cdecl, jit_type_void,
-                       params, 2, 1);
-
-       return L;
-}
+       struct psi_jit_context *context = C->context;
+       struct psi_jit_callback_info *cb_info;
+       struct psi_jit_decl_info *decl_info;
 
-static inline void psi_jit_context_dtor(struct psi_jit_context *L)
-{
-       jit_type_free(L->signature);
-       jit_context_destroy(L->jit);
-}
+       assert(exp->kind == PSI_LET_CALLBACK);
 
-static inline void psi_jit_context_free(struct psi_jit_context **L)
-{
-       if (*L) {
-               psi_jit_context_dtor(*L);
-               free(*L);
-               *L = NULL;
+       if (!psi_jit_decl_init(C, exp->data.callback->decl)) {
+               return false;
        }
-}
 
-static void psi_jit_init(struct psi_context *C)
-{
-       C->context = psi_jit_context_init(NULL);
-}
+       cb_info = pecalloc(1, sizeof(*cb_info), 1);
+       cb_info->impl_info = impl->info;
+       cb_info->let_exp = exp;
 
-static void psi_jit_dtor(struct psi_context *C)
-{
-       if (C->decls) {
-               size_t i = 0;
-               struct psi_decl *decl;
+       decl_info = exp->data.callback->decl->info;
+       cb_info->closure = jit_closure_create(context->jit, decl_info->signature,
+                       &psi_jit_callback, cb_info);
 
-               while (psi_plist_get(C->decls, i++, &decl)) {
-                       psi_jit_decl_dtor(decl);
-               }
+       if (!cb_info->closure) {
+               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_jit_impl_dtor(impl);
-               }
-       }
-       psi_jit_context_free((void *) &C->context);
+       assert(!exp->data.callback->decl->sym);
+       exp->data.callback->info = cb_info;
+       exp->data.callback->decl->sym = cb_info->closure;
+
+       return true;
 }
 
-static zend_function_entry *psi_jit_compile(struct psi_context *C)
+static void psi_jit_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;
-       struct psi_jit_context *ctx = C->context;
-
-       if (!C->impls) {
-               return NULL;
-       }
+       assert(let_exp->kind == PSI_LET_CALLBACK);
 
-       zfe = calloc(psi_plist_count(C->impls) + 1, sizeof(*zfe));
-       jit_context_build_start(ctx->jit);
+       psi_jit_decl_dtor(C, let_exp->data.callback->decl);
 
-       while (psi_plist_get(C->impls, i++, &impl)) {
-               zend_function_entry *zf = &zfe[nf];
+       if (let_exp->data.callback->info) {
+               struct psi_jit_callback_info *info = let_exp->data.callback->info;
 
-               if (!impl->decl) {
-                       continue;
-               }
-               if (!psi_jit_decl_init(impl->decl)) {
-                       continue;
-               }
-               if (!psi_jit_impl_init(impl, C)) {
-                       continue;
-               }
-
-               zf->fname = impl->func->name->val + (impl->func->name->val[0] == '\\');
-               zf->handler = ((struct psi_jit_impl_info *) impl->info)->closure;
-               zf->num_args = psi_plist_count(impl->func->args);
-               zf->arg_info = psi_internal_arginfo(impl);
-               ++nf;
+               /* The memory for the closure will be reclaimed
+                * when the context is destroyed.
+                */
+               pefree(info, 1);
+               let_exp->data.callback->info = NULL;
        }
-
-       while (psi_plist_get(C->decls, d++, &decl)) {
-               if (decl->info) {
-                       continue;
-               }
-
-               psi_jit_decl_init(decl);
-       }
-
-       jit_context_build_end(ctx->jit);
-
-       return zfe;
 }
 
-static inline void psi_jit_call_ex(struct psi_call_frame *frame) {
+static void psi_jit_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_jit_decl_info *decl_info = decl->info;
@@ -590,7 +502,7 @@ static inline void psi_jit_call_ex(struct psi_call_frame *frame) {
        }
 }
 
-static inline void psi_jit_call_va(struct psi_call_frame *frame) {
+static void psi_jit_call_va(struct psi_call_frame *frame) {
        jit_type_t signature;
        struct psi_call_frame *prev;
        struct psi_decl *decl = psi_call_frame_get_decl(frame);
@@ -613,8 +525,7 @@ static inline void psi_jit_call_va(struct psi_call_frame *frame) {
 
        signature = jit_type_create_signature(jit_abi_vararg,
                        jit_type_get_return(decl_info->signature),
-                       param_types, argc + va_count,
-                       1);
+                       param_types, argc + va_count, 1);
        assert(signature);
 
        if (impl) {
@@ -634,56 +545,50 @@ static inline void psi_jit_call_va(struct psi_call_frame *frame) {
        efree(param_types);
 }
 
-static void psi_jit_call(struct psi_call_frame *frame) {
-       if (psi_call_frame_num_var_args(frame)) {
-               psi_jit_call_va(frame);
-       } else {
-               psi_jit_call_ex(frame);
-       }
+static void *psi_jit_typeof_impl(struct psi_context *C, token_t impl_type)
+{
+       return psi_jit_impl_type(impl_type);
 }
 
-static void *psi_jit_query(struct psi_context *C, enum psi_context_query q,
-               void *arg)
+static void *psi_jit_typeof_decl(struct psi_context *C, token_t decl_type)
 {
-       switch (q) {
-       case PSI_CONTEXT_QUERY_SELF:
-               return "jit";
-       case PSI_CONTEXT_QUERY_TYPE:
-               return psi_jit_impl_type(*(token_t *) arg);
-       }
-       return NULL;
+       return psi_jit_token_type(decl_type);
 }
 
-static ZEND_RESULT_CODE psi_jit_load(void)
+static void *psi_jit_copyof_type(struct psi_context *C, void *orig_type)
 {
-#if HAVE_INT128
-       jit_type_t ll_fields[2], ull_fields[2];
-
-       ll_fields[0] = ll_fields[1] = jit_type_long;
-       jit_type_llong = jit_type_create_struct(ll_fields, 2, 1);
-
-       ull_fields[0] = ull_fields[1] = jit_type_ulong;
-       jit_type_ullong = jit_type_create_struct(ull_fields, 2, 1);
-#endif
-       return SUCCESS;
+       return jit_type_copy(orig_type);
 }
 
-static void psi_jit_free(void)
+static void psi_jit_layoutof_type(struct psi_context *C, void *orig_type,
+               struct psi_layout *l)
 {
-#if HAVE_INT128
-       jit_type_free(jit_type_llong);
-       jit_type_free(jit_type_ullong);
-#endif
+       l->pos = jit_type_get_alignment(orig_type);
+       l->len = jit_type_get_size(orig_type);
 }
 
 static struct psi_context_ops ops = {
+       "libjit",
        psi_jit_load,
        psi_jit_free,
        psi_jit_init,
        psi_jit_dtor,
-       psi_jit_compile,
+       psi_jit_composite_init,
+       psi_jit_composite_dtor,
+       psi_jit_extvar_init,
+       psi_jit_extvar_dtor,
+       psi_jit_decl_init,
+       psi_jit_decl_dtor,
+       psi_jit_impl_init,
+       psi_jit_impl_dtor,
+       psi_jit_cb_init,
+       psi_jit_cb_dtor,
        psi_jit_call,
-       psi_jit_query
+       psi_jit_call_va,
+       psi_jit_typeof_impl,
+       psi_jit_typeof_decl,
+       psi_jit_copyof_type,
+       psi_jit_layoutof_type,
 };
 
 struct psi_context_ops *psi_libjit_ops(void)
index 08380b1..c859daf 100644 (file)
@@ -38,6 +38,7 @@ struct psi_decl {
        zend_string *redir;
        void *sym;
        void *info;
+       void *type;
        unsigned varargs:1;
        unsigned extvar:1;
 };
index b48125b..143c8fe 100644 (file)
@@ -39,6 +39,10 @@ struct psi_decl_arg {
        struct psi_decl_type *type;
        struct psi_decl_var *var;
        struct psi_layout *layout;
+       struct {
+               void *info;
+               void *type;
+       } engine;
 };
 
 struct psi_decl_arg *psi_decl_arg_init(struct psi_decl_type *type, struct psi_decl_var *var);
index d17895a..d2a211d 100644 (file)
@@ -47,9 +47,6 @@ void psi_decl_struct_free(struct psi_decl_struct **s_ptr)
                if (s->args) {
                        psi_plist_free(s->args);
                }
-               if (s->engine.type && s->engine.dtor) {
-                       s->engine.dtor(s->engine.type);
-               }
                zend_string_release(s->name);
                free(s);
        }
index 01f6595..7c379aa 100644 (file)
@@ -38,10 +38,6 @@ struct psi_decl_struct {
        struct psi_plist *args;
        size_t size;
        size_t align;
-       struct {
-               void *type;
-               void (*dtor)(void *type);
-       } engine;
 };
 
 struct psi_decl_struct *psi_decl_struct_init(zend_string *name, struct psi_plist *args);
index 1445c35..66fb5f4 100644 (file)
@@ -336,6 +336,7 @@ void psi_decl_type_dump(struct psi_dump *dump, struct psi_decl_type *t, unsigned
        case PSI_T_STRUCT:
                PSI_DUMP(dump, "struct ");
                if (psi_decl_type_is_anon(t->name, "struct")) {
+                       PSI_DUMP(dump, "/*::(%zu, %zu)*/", t->real.strct->align, t->real.strct->size);
                        psi_decl_type_dump_args_with_layout(dump, t->real.strct->args, level);
                        return;
                }
index f597d24..30e1cdc 100644 (file)
@@ -15,4 +15,12 @@ psi_validate_string("typedef long int;");
 ===TEST===
 
 Warning: PSI syntax error, unexpected ;, expecting %s at col 17 in %s on line 1
+
+Warning: PSI preceding token 'typedef' at col 1 in <stdin> on line 1
+
+Warning: PSI preceding token 'long' at col 9 in <stdin> on line 1
+
+Warning: PSI preceding token 'int' at col 14 in <stdin> on line 1
+
+Warning: PSI offending token ';' at col 17 in <stdin> on line 1
 ===DONE===