+/*******************************************************************************
+ 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.
+*******************************************************************************/
+
#ifdef HAVE_CONFIG_H
# include "config.h"
+#else
+# include "php_config.h"
#endif
-
+#include "context.h"
+#include "call.h"
#include "php.h"
#ifdef HAVE_LIBFFI
-#include "php_psi.h"
+#include "libffi_compat.h"
#include "libffi.h"
-#include "engine.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 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 ffi_status psi_ffi_prep_closure(ffi_closure **closure, void **code, ffi_cif *sig, void (*handler)(ffi_cif*,void*,void**,void*), void *data) {
- *closure = psi_ffi_closure_alloc(sizeof(ffi_closure), code);
- ZEND_ASSERT(*closure != NULL);
-#if PSI_HAVE_FFI_PREP_CLOSURE_LOC
- return ffi_prep_closure_loc(*closure, sig, handler, data, *code);
-
-#elif PSI_HAVE_FFI_PREP_CLOSURE
- return ffi_prep_closure(*code, sig, handler, data);
-#else
-# error "Neither ffi_prep_closure() nor ffi_prep_closure_loc() is available"
+#if HAVE_INT128
+static ffi_type *ffi_type_sint128;
+static ffi_type *ffi_type_uint128;
#endif
-}
-
-static void psi_ffi_closure_free(void *c)
-{
-#ifdef PSI_HAVE_FFI_CLOSURE_ALLOC
- ffi_closure_free(c);
-#elif HAVE_MMAP
- munmap(c, sizeof(ffi_closure));
-#endif
-}
-
-static void psi_ffi_handler(ffi_cif *_sig, void *_result, void **_args, void *_data)
-{
- psi_call(*(zend_execute_data **)_args[0], *(zval **)_args[1], _data);
-}
-
-static void psi_ffi_callback(ffi_cif *_sig, void *_result, void **_args, void *_data)
-{
- size_t i;
- unsigned argc = _sig->nargs;
- void **argv = _args;
- let_callback *cb = _data;
- decl *decl_cb = cb->decl;
- impl_arg *iarg = cb->func->var->arg;
- zval return_value, *zargv = calloc(argc, sizeof(*zargv));
- void *result, *to_free = NULL;
-
- ZEND_ASSERT(argc == cb->decl->args->count);
-
- /* prepare args for the userland call */
- for (i = 0; i < argc; ++i) {
- cb->decl->args->args[i]->ptr = argv[i];
- }
- for (i = 0; i < cb->args->count; ++i) {
- psi_do_set(&zargv[i], cb->args->vals[i]);
- }
- zend_fcall_info_argp(&iarg->val.zend.cb->fci, cb->args->count, zargv);
-
- /* callback into userland */
- ZVAL_UNDEF(&return_value);
- iarg->_zv = &return_value;
- zend_fcall_info_call(&iarg->val.zend.cb->fci, &iarg->val.zend.cb->fcc, iarg->_zv, NULL);
-
- /* marshal return value of the userland call */
- switch (iarg->type->type) {
- case PSI_T_BOOL: zend_parse_arg_bool(iarg->_zv, &iarg->val.zend.bval, NULL, 0); break;
- case PSI_T_LONG: zend_parse_arg_long(iarg->_zv, &iarg->val.zend.lval, NULL, 0, 1); break;
- case PSI_T_FLOAT:
- case PSI_T_DOUBLE: zend_parse_arg_double(iarg->_zv, &iarg->val.dval, NULL, 0); break;
- case PSI_T_STRING: zend_parse_arg_str(iarg->_zv, &iarg->val.zend.str, 0); break;
- }
- result = cb->func->handler(_result, decl_cb->func->type, iarg, &to_free);
-
- if (result != _result) {
- *(void **)_result = result;
- }
-}
-
-static inline ffi_type *psi_ffi_decl_arg_type(decl_arg *darg);
-
-typedef struct PSI_LibffiContext {
+struct psi_ffi_context {
ffi_cif signature;
ffi_type *params[2];
-} PSI_LibffiContext;
+};
+
+struct psi_ffi_callback_info {
+ struct psi_ffi_impl_info *impl_info;
+ struct psi_let_exp *let_exp;
-typedef struct PSI_LibffiCall {
void *code;
ffi_closure *closure;
- ffi_cif signature;
- void *params[1]; /* [type1, type2, NULL, arg1, arg2] ... */
-} PSI_LibffiCall;
-
-static inline ffi_abi psi_ffi_abi(const char *convention) {
- return FFI_DEFAULT_ABI;
-}
-
-static inline PSI_LibffiCall *PSI_LibffiCallAlloc(PSI_Context *C, decl *decl) {
- int rc;
- size_t i, c = decl->args ? decl->args->count : 0;
- PSI_LibffiCall *call = calloc(1, sizeof(*call) + 2 * c * sizeof(void *));
-
- for (i = 0; i < c; ++i) {
- call->params[i] = psi_ffi_decl_arg_type(decl->args->args[i]);
- }
- call->params[c] = NULL;
-
- decl->call.info = call;
- decl->call.rval = &decl->func->ptr;
- decl->call.argc = c;
- decl->call.args = (void **) &call->params[c+1];
-
- rc = ffi_prep_cif(&call->signature, psi_ffi_abi(decl->abi->convention),
- c, psi_ffi_decl_arg_type(decl->func), (ffi_type **) call->params);
- ZEND_ASSERT(FFI_OK == rc);
+};
- return call;
-}
+struct psi_ffi_decl_info {
+ ffi_cif signature;
+ struct psi_ffi_struct_info *rv_array;
+ ffi_type *params[1];
+};
-static inline ffi_status PSI_LibffiCallInitClosure(PSI_Context *C, PSI_LibffiCall *call, impl *impl) {
- PSI_LibffiContext *context = C->context;
+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;
+};
- return psi_ffi_prep_closure(&call->closure, &call->code, &context->signature, psi_ffi_handler, impl);
-}
+struct psi_ffi_impl_info {
+ struct psi_context *context;
+ struct psi_call_frame *frame;
-static inline ffi_status PSI_LibffiCallInitCallbackClosure(PSI_Context *C, PSI_LibffiCall *call, let_callback *cb) {
- return psi_ffi_prep_closure(&call->closure, &call->code, &call->signature, psi_ffi_callback, cb);
-}
+ void *code;
+ ffi_closure *closure;
+};
-static inline void PSI_LibffiCallFree(PSI_LibffiCall *call) {
- if (call->closure) {
- psi_ffi_closure_free(call->closure);
- }
- free(call);
-}
+struct psi_ffi_struct_info {
+ ffi_type strct;
+ struct psi_plist *eles;
+};
-static inline ffi_type *psi_ffi_token_type(token_t t) {
+static inline ffi_type *psi_ffi_token_type(token_t t)
+{
switch (t) {
default:
- ZEND_ASSERT(0);
+ assert(0);
/* no break */
case PSI_T_VOID:
return &ffi_type_void;
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_LONG:
- return &ffi_type_slong;
case PSI_T_FLOAT:
return &ffi_type_float;
case PSI_T_DOUBLE:
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;
}
return NULL;
}
-static void psi_ffi_struct_type_dtor(void *type) {
- ffi_type *strct = type;
- if (strct->elements) {
- ffi_type **ptr;
+static void psi_ffi_type_free(ffi_type **typ_ptr)
+{
+ pefree(*typ_ptr, 1);
+}
- for (ptr = strct->elements; *ptr; ++ptr) {
- free(*ptr);
+static inline ffi_abi psi_ffi_abi(zend_string *convention) {
+ if (FFI_LAST_ABI - 2 != FFI_FIRST_ABI) {
+#ifdef HAVE_FFI_STDCALL
+ if (zend_string_equals_literal(convention, "stdcall")) {
+ return FFI_STDCALL;
}
- free(strct->elements);
+#endif
+#ifdef HAVE_FFI_FASTCALL
+ if (zend_string_equals_literal(convention, "fastcall")) {
+ return FFI_FASTCALL;
+ }
+#endif
}
- free(strct);
+ return FFI_DEFAULT_ABI;
}
-static size_t psi_ffi_struct_type_pad(ffi_type **els, size_t padding) {
- size_t i;
+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;
- for (i = 0; i < padding; ++i) {
- ffi_type *pad = malloc(sizeof(*pad));
+ psi_context_call(info->context, *(zend_execute_data **)args[0], *(zval **)args[1], impl);
+}
- memcpy(pad, &ffi_type_schar, sizeof(*pad));
- *els++ = pad;
- }
+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);
- return padding;
+ 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 ffi_type **psi_ffi_struct_type_elements(decl_struct *strct) {
- size_t i, argc = strct->args->count, nels = 0, offset = 0, maxalign = 0;
- ffi_type **els = calloc(argc + 1, sizeof(*els));
+static bool psi_ffi_load()
+{
+#if HAVE_INT128
+ ffi_type *i128, *u128;
+
+ i128 = pecalloc(1, 3*sizeof(ffi_type), 1);
+ i128->type = FFI_TYPE_STRUCT;
+ i128->size = 0;
+ i128->elements = (ffi_type **) (i128 + 1);
+ i128->elements[0] = &ffi_type_sint64;
+ i128->elements[1] = &ffi_type_sint64;
+
+ ffi_type_sint128 = i128;
+
+ u128 = pecalloc(1, 3*sizeof(ffi_type), 1);
+ u128->type = FFI_TYPE_STRUCT;
+ u128->size = 0;
+ u128->elements = (ffi_type **) (u128 + 1);
+ u128->elements[0] = &ffi_type_uint64;
+ u128->elements[1] = &ffi_type_uint64;
+
+ ffi_type_uint128 = u128;
+#endif
+ return true;
+}
- for (i = 0; i < strct->args->count; ++i) {
- decl_arg *darg = strct->args->args[i];
- ffi_type *type = malloc(sizeof(*type));
- size_t padding;
+static void psi_ffi_free()
+{
+#if HAVE_INT128
+ free(ffi_type_sint128);
+ free(ffi_type_uint128);
+#endif
+}
- memcpy(type, psi_ffi_decl_arg_type(darg), sizeof(*type));
+static bool psi_ffi_init(struct psi_context *C)
+{
+ ffi_status rc;
+ struct psi_ffi_context *context = pecalloc(1, sizeof(*context), 1);
+ ffi_abi abi = FFI_DEFAULT_ABI;
- ZEND_ASSERT(type->size == darg->layout->len);
+#if HAVE_FFI_FASTCALL
+ abi = FFI_FASTCALL;
+#endif
- if (type->alignment > maxalign) {
- maxalign = type->alignment;
- }
+ context->params[0] = &ffi_type_pointer;
+ context->params[1] = &ffi_type_pointer;
+ rc = ffi_prep_cif(&context->signature, abi, 2, &ffi_type_void,
+ context->params);
- if ((padding = psi_offset_padding(darg->layout->pos - offset, type->alignment))) {
- if (nels + padding + 1 > argc) {
- argc += padding;
- els = realloc(els, (argc + 1) * sizeof(*els));
- els[argc] = NULL;
- }
- psi_ffi_struct_type_pad(&els[nels], padding);
- nels += padding;
- offset += padding;
- }
- ZEND_ASSERT(offset == darg->layout->pos);
+ if (FFI_OK != rc) {
+ pefree(context, 1);
+ return false;
+ }
+
+ C->context = context;
+ return true;
+}
- offset = (offset + darg->layout->len + type->alignment - 1) & ~(type->alignment - 1);
- els[nels++] = type;
+static void psi_ffi_dtor(struct psi_context *C)
+{
+ if (C->context) {
+ pefree(C->context, 1);
+ C->context = NULL;
}
+}
- /* apply struct alignment padding */
- offset = (offset + maxalign - 1) & ~(maxalign - 1);
+static bool psi_ffi_composite_init(struct psi_context *C,
+ struct psi_decl_arg *darg)
+{
+ struct psi_ffi_struct_info *info;
- ZEND_ASSERT(offset <= strct->size);
- if (offset < strct->size) {
- psi_ffi_struct_type_pad(&els[nels], strct->size - offset);
+ if (darg->engine.type) {
+ return true;
}
- return els;
-}
-static inline ffi_type *psi_ffi_decl_type(decl_type *type) {
- decl_type *real = real_decl_type(type);
+ info = pecalloc(1, sizeof(*info), 1);
+ info->eles = psi_plist_init((psi_plist_dtor) psi_ffi_type_free);
- switch (real->type) {
- case PSI_T_FUNCTION:
- return &ffi_type_pointer;
+ psi_context_composite_type_elements(C, darg, &info->eles);
- case PSI_T_STRUCT:
- if (!real->strct->engine.type) {
- ffi_type *strct = calloc(1, sizeof(ffi_type));
+ /* add terminating NULL; libffi structs do not have an element count */
+ {
+ void *null_ptr = NULL;
+ info->eles = psi_plist_add(info->eles, &null_ptr);
+ }
- strct->type = FFI_TYPE_STRUCT;
- strct->size = 0;
- strct->elements = psi_ffi_struct_type_elements(real->strct);
+ info->strct.type = FFI_TYPE_STRUCT;
+ info->strct.alignment = 0;
+ info->strct.size = 0;
+ info->strct.elements = (ffi_type **) psi_plist_eles(info->eles);
- real->strct->engine.type = strct;
- real->strct->engine.dtor = psi_ffi_struct_type_dtor;
- }
+ darg->engine.info = info;
+ darg->engine.type = &info->strct;
- return real->strct->engine.type;
+ return true;
+}
- case PSI_T_UNION:
- return psi_ffi_decl_arg_type(real->unn->args->args[0]);
+static void psi_ffi_composite_dtor(struct psi_context *C,
+ struct psi_decl_arg *darg)
+{
+ struct psi_ffi_struct_info *info = darg->engine.info;
- default:
- return psi_ffi_token_type(real->type);
+ if (info) {
+ darg->engine.info = NULL;
+ darg->engine.type = NULL;
+
+ psi_plist_free(info->eles);
+ pefree(info, 1);
}
}
-static inline ffi_type *psi_ffi_decl_arg_type(decl_arg *darg) {
- if (darg->var->pointer_level) {
- return &ffi_type_pointer;
- } else {
- return psi_ffi_decl_type(darg->type);
- }
+
+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]);
}
+static bool psi_ffi_decl_init(struct psi_context *, struct psi_decl *);
-static inline PSI_LibffiContext *PSI_LibffiContextInit(PSI_LibffiContext *L) {
+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;
- if (!L) {
- L = malloc(sizeof(*L));
+ evar->info = info;
+
+ 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_context_decl_arg_call_type(C, evar->getter->func), NULL);
+ if (FFI_OK != rc) {
+ return false;
+ }
+ rc = psi_ffi_prep_closure(&info->get.closure, &info->get.code,
+ &info->get.signature, psi_ffi_extvar_get, evar);
+ if (FFI_OK != rc) {
+ return false;
}
- memset(L, 0, sizeof(*L));
- L->params[0] = &ffi_type_pointer;
- L->params[1] = &ffi_type_pointer;
- rc = ffi_prep_cif(&L->signature, FFI_DEFAULT_ABI, 2, &ffi_type_void, L->params);
- ZEND_ASSERT(rc == FFI_OK);
+ info->set.params[0] = psi_context_decl_arg_call_type(C, evar->arg);
+ rc = ffi_prep_cif(&info->set.signature, FFI_DEFAULT_ABI, 1,
+ &ffi_type_void, info->set.params);
+ if (FFI_OK != rc) {
+ return false;
+ }
+ rc = psi_ffi_prep_closure(&info->set.closure, &info->set.code,
+ &info->set.signature, psi_ffi_extvar_set, evar);
+ if (FFI_OK != rc) {
+ return false;
+ }
- return L;
+ evar->getter->sym = info->get.code;
+ evar->setter->sym = info->set.code;
+
+ return true;
}
-static void psi_ffi_init(PSI_Context *C)
-{
- C->context = PSI_LibffiContextInit(NULL);
+static void psi_ffi_extvar_dtor(struct psi_context *C,
+ struct psi_decl_extvar *evar) {
+ if (evar->info) {
+ pefree(evar->info, 1);
+ evar->info = NULL;
+ }
}
-static void psi_ffi_dtor(PSI_Context *C)
+static bool psi_ffi_decl_init(struct psi_context *C, struct psi_decl *decl)
{
- if (C->decls) {
- size_t i;
+ 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);
- for (i = 0; i < C->decls->count; ++i) {
- decl *decl = C->decls->list[i];
+ decl->info = info;
- if (decl->call.info) {
- PSI_LibffiCallFree(decl->call.info);
- }
+ 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;
+
+ rc = ffi_prep_cif(&info->signature, psi_ffi_abi(decl->abi->convention),
+ c, psi_context_decl_arg_call_type(C, decl->func), info->params);
+
+ if (FFI_OK != rc) {
+ pefree(info, 1);
+ decl->info = NULL;
+ return false;
+ }
+ }
+
+ return true;
+}
+
+static void psi_ffi_decl_dtor(struct psi_context *C,
+ struct psi_decl *decl)
+{
+ if (decl->info) {
+ pefree(decl->info, 1);
+ decl->info = NULL;
}
- free(C->context);
}
-static zend_function_entry *psi_ffi_compile(PSI_Context *C)
+static bool psi_ffi_impl_init(struct psi_context *C,
+ struct psi_impl *impl, zif_handler *zh)
{
- size_t c, i, j = 0;
- zend_function_entry *zfe;
+ struct psi_ffi_context *context = C->context;
+ struct psi_ffi_impl_info *info = pecalloc(1, sizeof(*info), 1);
+ ffi_status rc;
+
+ impl->info = info;
+ info->context = C;
- if (!C->impls) {
- return NULL;
+ rc = psi_ffi_prep_closure(&info->closure, &info->code,
+ &context->signature, psi_ffi_handler, impl);
+
+ if (FFI_OK != rc) {
+ goto failure;
}
- zfe = calloc(C->impls->count + 1, sizeof(*zfe));
- for (i = 0; i < C->impls->count; ++i) {
- zend_function_entry *zf = &zfe[j];
- PSI_LibffiCall *call;
- impl *impl = C->impls->list[i];
+ *zh = info->code;
+ return true;
- if (!impl->decl) {
- continue;
- }
+failure: ;
+ impl->info = NULL;
+ free(info);
+ return false;
+}
+
+static void psi_ffi_impl_dtor(struct psi_context *C, struct psi_impl *impl)
+{
+ struct psi_ffi_impl_info *info = impl->info;
- call = PSI_LibffiCallAlloc(C, impl->decl);
- if (FFI_OK != PSI_LibffiCallInitClosure(C, call, impl)) {
- PSI_LibffiCallFree(call);
- continue;
+ if (info) {
+ if (info->closure) {
+ psi_ffi_closure_free(info->closure);
}
+ free(info);
+ impl->info = NULL;
+ }
+}
- zf->fname = impl->func->name + (impl->func->name[0] == '\\');
- zf->num_args = impl->func->args->count;
- zf->handler = call->code;
- zf->arg_info = psi_internal_arginfo(impl);
- ++j;
+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;
- for (c = 0; c < impl->stmts->let.count; ++c) {
- let_stmt *let = impl->stmts->let.list[c];
+ assert(exp->kind == PSI_LET_CALLBACK);
- if (let->val->kind == PSI_LET_CALLBACK) {
- let_callback *cb = let->val->data.callback;
+ if (!psi_ffi_decl_init(C, exp->data.callback->decl)) {
+ return false;
+ }
- call = PSI_LibffiCallAlloc(C, cb->decl);
- if (FFI_OK != PSI_LibffiCallInitCallbackClosure(C, call, cb)) {
- PSI_LibffiCallFree(call);
- continue;
- }
+ cb_info = pecalloc(1, sizeof(*cb_info), 1);
+ cb_info->impl_info = impl->info;
+ cb_info->let_exp = exp;
- cb->decl->call.sym = call->code;
- }
- }
+ 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;
}
- for (i = 0; i < C->decls->count; ++i) {
- decl *decl = C->decls->list[i];
+ 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);
-// if (decl->impl) {
-// continue;
-// }
- if (decl->call.info) {
- continue;
+ 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;
+ }
+}
- PSI_LibffiCallAlloc(C, decl);
+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;
+ 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;
}
+}
- return zfe;
+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);
+ 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);
+ }
+
+ 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;
+ }
+
+ efree(param_types);
}
-static void psi_ffi_call(PSI_Context *C, decl_callinfo *decl_call, impl_vararg *va) {
- PSI_LibffiCall *call = decl_call->info;
+static void *psi_ffi_typeof_impl(struct psi_context *C, token_t impl_type)
+{
+ return psi_ffi_impl_type(impl_type);
+}
- if (va) {
- ffi_status rc;
- ffi_cif signature;
- size_t i, nfixedargs = decl_call->argc, ntotalargs = nfixedargs + va->args->count;
- void **params = calloc(2 * ntotalargs + 2, sizeof(void *));
+static void *psi_ffi_typeof_decl(struct psi_context *C, token_t decl_type)
+{
+ return psi_ffi_token_type(decl_type);
+}
- for (i = 0; i < nfixedargs; ++i) {
- params[i] = call->params[i];
- params[i + ntotalargs + 1] = call->params[i + nfixedargs + 1];
- }
- for (i = 0; i < va->args->count; ++i) {
- params[nfixedargs + i] = psi_ffi_impl_type(va->types[i]);
- params[nfixedargs + i + ntotalargs + 1] = &va->values[i];
- }
-#ifdef PSI_HAVE_FFI_PREP_CIF_VAR
- rc = ffi_prep_cif_var(&signature, call->signature.abi,
- nfixedargs, ntotalargs,
- call->signature.rtype, (ffi_type **) params);
-#else
- /* FIXME: test in config.m4; assume we can just call anyway */
- rc = ffi_prep_cif(&signature, call->signature.abi, ntotalargs,
- call->signature.rtype, (ffi_type **) params);
-#endif
- ZEND_ASSERT(FFI_OK == rc);
- ffi_call(&signature, FFI_FN(decl_call->sym), *decl_call->rval, ¶ms[ntotalargs + 1]);
- free(params);
- } else {
- ffi_call(&call->signature, FFI_FN(decl_call->sym), *decl_call->rval, decl_call->args);
+static void *psi_ffi_copyof_type(struct psi_context *C, void *orig_type)
+{
+ ffi_type *type = pemalloc(sizeof(*type), 1);
+
+ *type = *(ffi_type *) orig_type;
+ return type;
+}
+
+static void psi_ffi_layoutof_type(struct psi_context *C, void *orig_type,
+ struct psi_layout *l)
+{
+ ffi_type *type = orig_type;
+
+ if (!type->size || !type->alignment) {
+ ffi_cif tmp;
+ ffi_prep_cif(&tmp, FFI_DEFAULT_ABI, 0, type, NULL);
}
+
+ l->pos = type->alignment;
+ l->len = type->size;
}
-static PSI_ContextOps ops = {
+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_call_va,
+ psi_ffi_typeof_impl,
+ psi_ffi_typeof_decl,
+ psi_ffi_copyof_type,
+ psi_ffi_layoutof_type,
};
-PSI_ContextOps *PSI_Libffi(void)
+struct psi_context_ops *psi_libffi_ops(void)
{
return &ops;
}