X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-psi;a=blobdiff_plain;f=src%2Fmodule.c;h=20b25ead45afe0d7b248dbc82c3aea537b1cee3f;hp=508fdea64e5da754e3c2283423a90cef33a0187f;hb=7a717dad32d06fe5273ad3a9ce755908723f6685;hpb=a6ffb2bfbf83ef3511cc356bd931d460feabe7a2 diff --git a/src/module.c b/src/module.c index 508fdea..20b25ea 100644 --- a/src/module.c +++ b/src/module.c @@ -10,14 +10,22 @@ #include "php_psi.h" #include "parser.h" -#include "libjit.h" -#include "libffi.h" +#if HAVE_LIBJIT +# include "libjit.h" +# ifndef HAVE_LIBFFI +# define PSI_ENGINE "jit" +# endif +#endif +#if HAVE_LIBFFI +# include "libffi.h" +# define PSI_ENGINE "ffi" +#endif ZEND_DECLARE_MODULE_GLOBALS(psi); PHP_INI_BEGIN() - STD_PHP_INI_ENTRY("psi.engine", "ffi", PHP_INI_SYSTEM, OnUpdateString, engine, zend_psi_globals, psi_globals) - STD_PHP_INI_ENTRY("psi.directory", "psis", PHP_INI_SYSTEM, OnUpdateString, directory, zend_psi_globals, psi_globals) + STD_PHP_INI_ENTRY("psi.engine", PSI_ENGINE, PHP_INI_SYSTEM, OnUpdateString, engine, zend_psi_globals, psi_globals) + STD_PHP_INI_ENTRY("psi.directory", "psi.d", PHP_INI_SYSTEM, OnUpdateString, directory, zend_psi_globals, psi_globals) PHP_INI_END(); void psi_error(int type, const char *msg, ...) @@ -439,7 +447,7 @@ void psi_to_array(zval *return_value, token_t t, impl_val *ret_val, set_value *s } } -ZEND_RESULT_CODE psi_parse_args(zend_execute_data *execute_data, impl *impl) +static inline ZEND_RESULT_CODE psi_parse_args(zend_execute_data *execute_data, impl *impl) { impl_arg *iarg; @@ -497,7 +505,7 @@ ZEND_RESULT_CODE psi_parse_args(zend_execute_data *execute_data, impl *impl) return SUCCESS; } -void *psi_do_calloc(let_calloc *alloc) +static inline void *psi_do_calloc(let_calloc *alloc) { decl_type *type = real_decl_type(alloc->type); size_t size; @@ -512,7 +520,7 @@ void *psi_do_calloc(let_calloc *alloc) return ecalloc(alloc->n, size); } -void *psi_do_let(decl_arg *darg) +static inline void *psi_do_let(decl_arg *darg) { impl_arg *iarg = darg->let->arg; impl_val *arg_val; @@ -596,7 +604,7 @@ void *psi_do_let(decl_arg *darg) } } -void psi_do_set(zval *return_value, set_value *set) +static inline void psi_do_set(zval *return_value, set_value *set) { impl_val *val = (impl_val *) &set->vars->vars[0]->arg->let->ptr; token_t t = real_decl_type(set->vars->vars[0]->arg->type)->type; @@ -607,28 +615,34 @@ void psi_do_set(zval *return_value, set_value *set) set->func->handler(return_value, t, val, set, set->vars->vars[0]); } -void psi_do_return(zval *return_value, return_stmt *ret, impl_val *ret_val) +static inline void psi_do_return(zval *return_value, return_stmt *ret, impl_val *ret_val) { token_t t = real_decl_type(ret->decl->type)->type; ret->set->func->handler(return_value, t, ret_val, ret->set, ret->decl->var); } -void psi_do_free(free_stmt *fre) +static inline void psi_do_free(free_stmt *fre) { - size_t i; + size_t i, j; + impl_val dummy; + + for (i = 0; i < fre->calls->count; ++i) { + free_call *f = fre->calls->list[i]; - for (i = 0; i < fre->vars->count; ++i) { - decl_var *dvar = fre->vars->vars[i]; + for (j = 0; j < f->vars->count; ++j) { + decl_var *dvar = f->vars->vars[j]; + decl_arg *darg = dvar->arg; - if (dvar->arg && dvar->arg->let->out.ptr) { - free(dvar->arg->let->out.ptr); - dvar->arg->let->out.ptr = NULL; + f->decl->call.args[j] = &darg->let->out; } + + /* FIXME: check in validate_* that free functions return scalar */ + PSI_ContextCall(&PSI_G(context), &dummy, f->decl); } } -void psi_do_clean(impl *impl) +static inline void psi_do_clean(impl *impl) { size_t i; @@ -663,16 +677,63 @@ void psi_do_clean(impl *impl) } } +void psi_call(zend_execute_data *execute_data, zval *return_value, impl *impl) +{ + impl_val ret_val; + size_t i; + + if (SUCCESS != psi_parse_args(execute_data, impl)) { + return; + } + + if (impl->decl->args) { + for (i = 0; i < impl->decl->args->count; ++i) { + decl_arg *darg = impl->decl->args->args[i]; + + impl->decl->call.args[i] = psi_do_let(darg); + } + } + + memset(&ret_val, 0, sizeof(ret_val)); + PSI_ContextCall(&PSI_G(context), &ret_val, impl->decl); + + psi_do_return(return_value, impl->stmts->ret.list[0], &ret_val); + + for (i = 0; i < impl->stmts->set.count; ++i) { + set_stmt *set = impl->stmts->set.list[i]; + + if (set->arg->_zv) { + psi_do_set(set->arg->_zv, set->val); + } + } + + for (i = 0; i < impl->stmts->fre.count; ++i) { + free_stmt *fre = impl->stmts->fre.list[i]; + + psi_do_free(fre); + } + + psi_do_clean(impl); +} + PHP_MINIT_FUNCTION(psi) { - PSI_ContextOps *ops; + PSI_ContextOps *ops = NULL; REGISTER_INI_ENTRIES(); +#ifdef HAVE_LIBJIT if (!strcasecmp(PSI_G(engine), "jit")) { ops = PSI_Libjit(); - } else { + } else +#endif +#ifdef HAVE_LIBFFI ops = PSI_Libffi(); +#endif + + if (!ops) { + php_error(E_WARNING, "No PSI engine found"); + return FAILURE; } PSI_ContextInit(&PSI_G(context), ops, psi_error);