X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=src%2Fmodule.c;h=184cf0c558ee0a88a6be03ab0d5b5899639888fa;hb=2454ceb434f8190937580d1cca96f5737908224e;hp=152c949dabedd873dc9076a62ea96ea2aa54536b;hpb=cfebc76ed7426836f21296cca9a14b422bfa04cf;p=m6w6%2Fext-psi diff --git a/src/module.c b/src/module.c index 152c949..184cf0c 100644 --- a/src/module.c +++ b/src/module.c @@ -23,6 +23,8 @@ # define PSI_ENGINE "ffi" #endif +#include + ZEND_DECLARE_MODULE_GLOBALS(psi); PHP_INI_BEGIN() @@ -106,6 +108,15 @@ size_t psi_t_align(token_t t, size_t s) return ((s - 1) | (a - 1)) + 1; } +size_t psi_offset_padding(size_t diff, size_t alignment) +{ + if (diff && diff <= ((diff - 1) | (alignment -1)) + 1) { + diff = 0; + } + + return diff; +} + int psi_internal_type(impl_type *type) { switch (type->type) { @@ -144,6 +155,7 @@ zend_internal_arg_info *psi_internal_arginfo(impl *impl) zend_internal_arg_info *ai = &aip[impl->func->args->count]; ai->name = vararg->var->name; + ai->allow_null = 1; ai->type_hint = psi_internal_type(vararg->type); if (vararg->var->reference) { ai->pass_by_reference = 1; @@ -160,9 +172,9 @@ zend_internal_arg_info *psi_internal_arginfo(impl *impl) if (iarg->var->reference) { ai->pass_by_reference = 1; } - if (iarg->var->reference || (iarg->def && iarg->def->type == PSI_T_NULL)) { + //if (iarg->var->reference || (iarg->def && iarg->def->type == PSI_T_NULL)) { ai->allow_null = 1; - } + //} } return aip; @@ -263,6 +275,11 @@ void psi_to_double(zval *return_value, set_value *set, impl_val *ret_val) case PSI_T_DOUBLE: RETVAL_DOUBLE(v->dval); break; +#ifdef HAVE_LONG_DOUBLE + case PSI_T_LONG_DOUBLE: + RETVAL_DOUBLE((double) v->ldval); + break; +#endif case PSI_T_INT8: RETVAL_DOUBLE((double) v->i8); break; @@ -304,6 +321,11 @@ void psi_to_string(zval *return_value, set_value *set, impl_val *ret_val) case PSI_T_DOUBLE: RETVAL_DOUBLE(deref_impl_val(ret_val, var)->dval); break; +#ifdef HAVE_LONG_DOUBLE + case PSI_T_LONG_DOUBLE: + RETVAL_DOUBLE((double) deref_impl_val(ret_val, var)->ldval); + break; +#endif default: if (!var->arg->var->pointer_level) { RETVAL_STRINGL(&ret_val->cval, 1); @@ -349,6 +371,7 @@ void psi_from_zval(impl_val *mem, decl_arg *spec, zval *zv, void **tmp) case PSI_T_DOUBLE: mem->dval = zval_get_double(zv); break; + case PSI_T_VOID: case PSI_T_INT8: case PSI_T_UINT8: if (spec->var->pointer_level) { @@ -608,7 +631,7 @@ static inline void *psi_do_calloc(let_calloc *alloc) return mem; } -static inline ZEND_RESULT_CODE psi_let_val(token_t let_func, impl_arg *iarg, impl_val *arg_val, decl_struct *strct, void **to_free) +static inline impl_val *psi_let_val(token_t let_func, impl_arg *iarg, impl_val *arg_val, decl_struct *strct, void **to_free) { switch (let_func) { case PSI_T_BOOLVAL: @@ -636,7 +659,7 @@ static inline ZEND_RESULT_CODE psi_let_val(token_t let_func, impl_arg *iarg, imp case PSI_T_STRVAL: if (iarg->type->type == PSI_T_STRING) { if (iarg->val.zend.str) { - arg_val->ptr = estrdup(iarg->val.zend.str->val); + arg_val->ptr = estrndup(iarg->val.zend.str->val, iarg->val.zend.str->len); *to_free = arg_val->ptr; } else { arg_val->ptr = ""; @@ -650,7 +673,7 @@ static inline ZEND_RESULT_CODE psi_let_val(token_t let_func, impl_arg *iarg, imp if (PSI_T_PATHVAL == let_func) { if (SUCCESS != php_check_open_basedir(arg_val->ptr)) { efree(arg_val->ptr); - return FAILURE; + return NULL; } } break; @@ -669,8 +692,8 @@ static inline ZEND_RESULT_CODE psi_let_val(token_t let_func, impl_arg *iarg, imp break; case PSI_T_ARRVAL: if (iarg->type->type == PSI_T_ARRAY) { - arg_val->ptr = psi_array_to_struct(strct, HASH_OF(iarg->_zv)); - *to_free = arg_val->ptr; + arg_val = psi_array_to_struct(strct, HASH_OF(iarg->_zv)); + *to_free = arg_val; } break; case PSI_T_OBJVAL: @@ -678,7 +701,7 @@ static inline ZEND_RESULT_CODE psi_let_val(token_t let_func, impl_arg *iarg, imp psi_object *obj; if (!instanceof_function(Z_OBJCE_P(iarg->_zv), psi_class_entry)) { - return FAILURE; + return NULL; } obj = PSI_OBJ(iarg->_zv, NULL); @@ -687,7 +710,7 @@ static inline ZEND_RESULT_CODE psi_let_val(token_t let_func, impl_arg *iarg, imp break; EMPTY_SWITCH_DEFAULT_CASE(); } - return SUCCESS; + return arg_val; } static inline void *psi_do_let(let_stmt *let) @@ -723,7 +746,7 @@ static inline void *psi_do_let(let_stmt *let) case PSI_LET_FUNC: iarg = let->val->data.func->arg; - if (SUCCESS != psi_let_val(let->val->data.func->type, iarg, darg->ptr, real_decl_type(darg->type)->strct, &darg->mem)) { + if (!(darg->ptr = psi_let_val(let->val->data.func->type, iarg, darg->ptr, real_decl_type(darg->type)->strct, &darg->mem))) { return NULL; } } @@ -758,8 +781,9 @@ static inline void psi_do_free(free_stmt *fre) for (j = 0; j < f->vars->count; ++j) { decl_var *dvar = f->vars->vars[j]; decl_arg *darg = dvar->arg; + impl_val *fval = darg->let ? darg->let->ptr : darg->ptr; - f->decl->call.args[j] = &darg->val; + f->decl->call.args[j] = deref_impl_val(fval, dvar); } /* FIXME: check in validate_* that free functions return scalar */ @@ -767,10 +791,30 @@ static inline void psi_do_free(free_stmt *fre) } } +static inline void psi_clean_array_struct(decl_arg *darg) { + if (darg->let + && darg->let->val->kind == PSI_LET_FUNC + && darg->let->val->data.func->type == PSI_T_ARRVAL) { + decl_type *type = real_decl_type(darg->type); + + if (type->type == PSI_T_STRUCT) { + void **ptr = (void **) ((char *) darg->mem + type->strct->size); + + while (*ptr) { + efree(*ptr++); + } + } + } +} + static inline void psi_do_clean(impl *impl) { size_t i; + if (impl->decl->func->ptr != &impl->decl->func->val) { + efree(impl->decl->func->ptr); + impl->decl->func->ptr = &impl->decl->func->val; + } for (i = 0; i < impl->func->args->count; ++i ) { impl_arg *iarg = impl->func->args->args[i]; @@ -787,18 +831,11 @@ static inline void psi_do_clean(impl *impl) decl_arg *darg = impl->decl->args->args[i]; if (darg->mem) { - decl_type *type = real_decl_type(darg->type); - - if (type->type == PSI_T_STRUCT) { - void **ptr = (void **) ((char *) darg->mem + type->strct->size); - - while (*ptr) { - efree(*ptr++); - } - } + psi_clean_array_struct(darg); efree(darg->mem); darg->mem = NULL; } + darg->ptr = &darg->val; } if (impl->func->args->vararg.args) { @@ -908,6 +945,7 @@ int psi_calc_num_exp(num_exp *exp, impl_val *strct, impl_val *res) { #define PRIfval "f" #define PRIdval "lf" +#define PRIldval "Lf" #define PSI_CALC_OP(var) do { \ const char *fmt = "calc %" PRI##var ", %" PRI##var ": %" PRI##var "\n"; \ @@ -919,12 +957,26 @@ int psi_calc_num_exp(num_exp *exp, impl_val *strct, impl_val *res) { res->vres = PSI_CALC(v1->var1, v2->var2); \ if (!res->vres) fprintf(stderr, fmt, v1->var1, v2->var2, res->vres); \ } while(0) + +#ifdef HAVE_LONG_DOUBLE +#define PSI_CALC_NO_LD +#define PSI_CALC_OP_LD PSI_CALC_OP(ldval) +#define PSI_CALC_OP2_LD2(var1) PSI_CALC_OP2(ldval, var1, ldval) +#define PSI_CALC_OP2_LD1(var2) PSI_CALC_OP2(ldval, ldval, var2) +#else +#define PSI_CALC_NO_LD abort() +#define PSI_CALC_OP_LD PSI_CALC_NO_LD +#define PSI_CALC_OP2_LD2(var) PSI_CALC_NO_LD +#define PSI_CALC_OP2_LD1(var) PSI_CALC_NO_LD +#endif + #define PSI_CALC_FN(op) int psi_calc_##op(int t1, impl_val *v1, int t2, impl_val *v2, impl_val *res) \ { \ if (t1 == t2) { \ switch (t1) { \ case PSI_T_FLOAT: PSI_CALC_OP(fval); break; \ case PSI_T_DOUBLE: PSI_CALC_OP(dval); break; \ + case PSI_T_LONG_DOUBLE: PSI_CALC_OP_LD; break; \ case PSI_T_INT8: PSI_CALC_OP(i8); break; \ case PSI_T_UINT8: PSI_CALC_OP(u8); break; \ case PSI_T_INT16: PSI_CALC_OP(i16); break; \ @@ -938,6 +990,7 @@ int psi_calc_num_exp(num_exp *exp, impl_val *strct, impl_val *res) { return t1; \ } else if (t1 == PSI_T_DOUBLE) { \ switch (t2) { \ + case PSI_T_LONG_DOUBLE: PSI_CALC_OP2_LD2(dval); return t2; \ case PSI_T_FLOAT: PSI_CALC_OP2(dval, dval, fval); break; \ case PSI_T_INT8: PSI_CALC_OP2(dval, dval, i8); break; \ case PSI_T_UINT8: PSI_CALC_OP2(dval, dval, u8); break; \ @@ -952,6 +1005,7 @@ int psi_calc_num_exp(num_exp *exp, impl_val *strct, impl_val *res) { return t1; \ } else if (t2 == PSI_T_DOUBLE) { \ switch (t1) { \ + case PSI_T_LONG_DOUBLE: PSI_CALC_OP2_LD1(dval); return t1; \ case PSI_T_FLOAT: PSI_CALC_OP2(dval, fval, dval); break; \ case PSI_T_INT8: PSI_CALC_OP2(dval, i8, dval); break; \ case PSI_T_UINT8: PSI_CALC_OP2(dval, u8, dval); break; \ @@ -964,8 +1018,41 @@ int psi_calc_num_exp(num_exp *exp, impl_val *strct, impl_val *res) { EMPTY_SWITCH_DEFAULT_CASE(); \ } \ return t2; \ + } else if (t1 == PSI_T_LONG_DOUBLE) { \ + PSI_CALC_NO_LD; \ + switch (t2) { \ + case PSI_T_DOUBLE: PSI_CALC_OP2_LD1(dval); break; \ + case PSI_T_FLOAT: PSI_CALC_OP2_LD1(fval); break; \ + case PSI_T_INT8: PSI_CALC_OP2_LD1(i8); break; \ + case PSI_T_UINT8: PSI_CALC_OP2_LD1(u8); break; \ + case PSI_T_INT16: PSI_CALC_OP2_LD1(i16); break; \ + case PSI_T_UINT16: PSI_CALC_OP2_LD1(u16); break; \ + case PSI_T_INT32: PSI_CALC_OP2_LD1(i32); break; \ + case PSI_T_UINT32: PSI_CALC_OP2_LD1(u32); break; \ + case PSI_T_INT64: PSI_CALC_OP2_LD1(i64); break; \ + case PSI_T_UINT64: PSI_CALC_OP2_LD1(u64); break; \ + EMPTY_SWITCH_DEFAULT_CASE(); \ + } \ + return t1; \ + } else if (t2 == PSI_T_LONG_DOUBLE) { \ + PSI_CALC_NO_LD; \ + switch (t1) { \ + case PSI_T_DOUBLE: PSI_CALC_OP2_LD2(dval); break; \ + case PSI_T_FLOAT: PSI_CALC_OP2_LD2(fval); break; \ + case PSI_T_INT8: PSI_CALC_OP2_LD2(i8); break; \ + case PSI_T_UINT8: PSI_CALC_OP2_LD2(u8); break; \ + case PSI_T_INT16: PSI_CALC_OP2_LD2(i16); break; \ + case PSI_T_UINT16: PSI_CALC_OP2_LD2(u16); break; \ + case PSI_T_INT32: PSI_CALC_OP2_LD2(i32); break; \ + case PSI_T_UINT32: PSI_CALC_OP2_LD2(u32); break; \ + case PSI_T_INT64: PSI_CALC_OP2_LD2(i64); break; \ + case PSI_T_UINT64: PSI_CALC_OP2_LD2(u64); break; \ + EMPTY_SWITCH_DEFAULT_CASE(); \ + } \ + return t2; \ } else if (t1 == PSI_T_FLOAT) { \ switch (t2) { \ + case PSI_T_LONG_DOUBLE: PSI_CALC_OP2_LD2(fval); return t2; \ case PSI_T_DOUBLE: PSI_CALC_OP2(dval, fval, dval); return t2; \ case PSI_T_INT8: PSI_CALC_OP2(fval, fval, i8); break; \ case PSI_T_UINT8: PSI_CALC_OP2(fval, fval, u8); break; \ @@ -980,6 +1067,7 @@ int psi_calc_num_exp(num_exp *exp, impl_val *strct, impl_val *res) { return t1; \ } else if (t2 == PSI_T_FLOAT) { \ switch (t1) { \ + case PSI_T_LONG_DOUBLE: PSI_CALC_OP2_LD1(fval); return t1; \ case PSI_T_DOUBLE: PSI_CALC_OP2(dval, dval, fval); return t1; \ case PSI_T_INT8: PSI_CALC_OP2(fval, i8, fval); break; \ case PSI_T_UINT8: PSI_CALC_OP2(fval, u8, fval); break; \ @@ -1059,6 +1147,16 @@ static inline void psi_do_args(impl *impl) { for (i = 0; i < impl->decl->args->count; ++i) { impl->decl->call.args[i] = impl->decl->args->args[i]->let->ptr; } + + if (!impl->decl->func->var->pointer_level) { + decl_type *real = real_decl_type(impl->decl->func->type); + + switch (real->type) { + case PSI_T_STRUCT: + impl->decl->func->ptr = psi_array_to_struct(real->strct, NULL); + break; + } + } } static inline impl_vararg *psi_do_varargs(impl *impl) { @@ -1100,7 +1198,10 @@ static inline impl_vararg *psi_do_varargs(impl *impl) { } va->types[i] = vatype; - psi_let_val(let_fn, vaarg, &va->values[i], NULL, &to_free); + /* FIXME: varargs with struct-by-value :) */ + if (!psi_let_val(let_fn, vaarg, &va->values[i], NULL, &to_free)) { + return NULL; + } if (to_free) { if (!va->free_list) {