From: Michael Wallner Date: Wed, 16 Dec 2015 14:34:55 +0000 (+0100) Subject: flush X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=481515f92d2baa83dea51d2effcbde711aa99e3b;p=m6w6%2Fext-psi flush --- diff --git a/php_psi.h b/php_psi.h index 6715c22..85a3805 100644 --- a/php_psi.h +++ b/php_psi.h @@ -1,4 +1,3 @@ - #ifndef PHP_PSI_H #define PHP_PSI_H @@ -19,8 +18,8 @@ extern zend_module_entry psi_module_entry; #include "TSRM.h" #endif -#include "context.h" #include "parser.h" +#include "context.h" void psi_error(int type, const char *msg, ...); @@ -78,6 +77,7 @@ static inline zend_long psi_long_num_exp(num_exp *exp, impl_val *ref) { case PSI_T_DOUBLE: return val.dval; EMPTY_SWITCH_DEFAULT_CASE(); } + return 0; } int psi_calc_add(int t1, impl_val *v1, int t2, impl_val *v2, impl_val *res); diff --git a/src/context.c b/src/context.c index ac663af..1a20af9 100644 --- a/src/context.c +++ b/src/context.c @@ -150,10 +150,24 @@ static struct psi_predef_const { PSI_MACROS +size_t psi_fread(void *ptr, size_t size, size_t nmemb, FILE *stream) +{ + size_t rv = fread(ptr, size, nmemb, stream); + return rv; +} + +FILE *psi_fopen(const char *path, const char *mode) +{ + FILE *f = fopen(path, mode); + return f; +} + static struct psi_func_redir { const char *name; void (*func)(void); } psi_func_redirs[] = { + {"fopen", (void (*)(void)) psi_fopen}, + {"fread", (void (*)(void)) psi_fread}, PSI_REDIRS {0} }; @@ -477,11 +491,7 @@ static inline int validate_num_exp(PSI_Data *data, decl_args *dargs, decl_arg *f return 0; } } -static inline int validate_set_value(PSI_Data *data, set_value *set, decl_arg *ref, decl_args *ref_list) { - size_t i; - decl_type *ref_type = real_decl_type(ref->type); - decl_var *set_var = set->vars->vars[0]; - +static inline int validate_set_value_handler(set_value *set) { switch (set->func->type) { case PSI_T_TO_BOOL: set->func->handler = psi_to_bool; @@ -504,15 +514,45 @@ static inline int validate_set_value(PSI_Data *data, set_value *set, decl_arg *r case PSI_T_VOID: set->func->handler = psi_to_void; break; - EMPTY_SWITCH_DEFAULT_CASE(); + default: + return 0; + } + return 1; +} +static inline void decl_var_arg_v(decl_args *args, va_list argp) { + int argc; + decl_arg **argv; + + memset(args, 0, sizeof(*args)); + + while ((argc = va_arg(argp, int))) { + argv = va_arg(argp, decl_arg **); + while (argc--) { + add_decl_arg(args, *argv++); + } + } +} +static inline int validate_set_value_ex(PSI_Data *data, set_value *set, decl_arg *ref, decl_args *ref_list) { + size_t i; + decl_type *ref_type; + decl_var *set_var = set->vars->vars[0]; + + if (!validate_set_value_handler(set)) { + data->error(PSI_WARNING, "Invalid cast '%s'", set->func->name); + return 0; } - for (i = 1; i < set->vars->count; ++i) { - if (!locate_decl_var_arg(set->vars->vars[i], ref_list, ref)) { + for (i = 0; i < set->vars->count; ++i) { + if (!locate_decl_var_arg(set->vars->vars[i], ref_list, NULL)) { + data->error(PSI_WARNING, "Unknown variable '%s'", set->vars->vars[i]->name); return 0; } } - set_var->arg = ref; + + if (!ref) { + ref = set_var->arg; + } + ref_type = real_decl_type(ref->type); if (set->count) { int is_to_array = (set->func->type == PSI_T_TO_ARRAY); @@ -538,7 +578,7 @@ static inline int validate_set_value(PSI_Data *data, set_value *set, decl_arg *r set->inner[i]->outer.set = set; if (sub_ref) { - if (!validate_set_value(data, set->inner[i], sub_ref, ref_type->strct->args)) { + if (!validate_set_value_ex(data, set->inner[i], sub_ref, ref_type->strct->args)) { return 0; } } @@ -554,7 +594,7 @@ static inline int validate_set_value(PSI_Data *data, set_value *set, decl_arg *r data->error(E_WARNING, "Inner `set` statement casts on pointers must reference the same variable"); return 0; } - if (!validate_set_value(data, set->inner[0], sub_ref, ref_list)) { + if (!validate_set_value_ex(data, set->inner[0], sub_ref, ref_list)) { return 0; } } @@ -565,6 +605,21 @@ static inline int validate_set_value(PSI_Data *data, set_value *set, decl_arg *r return 1; } +static inline int validate_set_value(PSI_Data *data, set_value *set, ...) { + va_list argp; + decl_args args = {0}; + int check; + + va_start(argp, set); + decl_var_arg_v(&args, argp); + va_end(argp); + + check = validate_set_value_ex(data, set, NULL, &args); + if (args.args) { + free(args.args); + } + return check; +} static inline decl *locate_impl_decl(decls *decls, return_stmt *ret) { if (decls) { size_t i; @@ -604,7 +659,7 @@ static inline int validate_impl_ret_stmt(PSI_Data *data, impl *impl) { return 0; } - if (!validate_set_value(data, ret->set, ret->decl, impl->decl->args)) { + if (!validate_set_value(data, ret->set, 1, &ret->decl, impl->decl->args ? (int) impl->decl->args->count : 0, impl->decl->args ? impl->decl->args->args : NULL, 0)) { return 0; } @@ -616,6 +671,17 @@ static inline int validate_impl_ret_stmt(PSI_Data *data, impl *impl) { static inline int validate_impl_let_stmts(PSI_Data *data, impl *impl) { size_t i, j; /* we can have multiple let stmts */ + /* check that we have a decl arg for every let stmt */ + for (i = 0; i < impl->stmts->let.count; ++i) { + let_stmt *let = impl->stmts->let.list[i]; + + if (!locate_decl_var_arg(let->var, impl->decl->args, impl->decl->func)) { + data->error(PSI_WARNING, "Unknown variable '%s' in `let` statement" + " of implementation '%s'", let->var->name, impl->func->name); + return 0; + } + } + /* check that we have a let stmt for every decl arg */ if (impl->decl->args) for (i = 0; i < impl->decl->args->count; ++i) { decl_arg *darg = impl->decl->args->args[i]; @@ -705,7 +771,7 @@ static inline int validate_impl_set_stmts(PSI_Data *data, impl *impl) { if (!strcmp(set_var->name, set_arg->var->name)) { check = 1; set_var->arg = set_arg; - if (!validate_set_value(data, set->val, set_arg, impl->decl->args)) { + if (!validate_set_value(data, set->val, 1, &set_arg, 1, &impl->decl->func, impl->decl->args->count, impl->decl->args->args, 0)) { return 0; } break; @@ -1101,7 +1167,6 @@ zend_function_entry *PSI_ContextCompile(PSI_Context *C) void PSI_ContextCall(PSI_Context *C, impl_val *ret_val, decl *decl) { - errno = 0; C->ops->call(C, ret_val, decl); } @@ -1131,6 +1196,35 @@ static inline void dump_decl_arg(int fd, decl_arg *a) { static inline void dump_level(int fd, unsigned level) { dprintf(fd, "%.*s", level, "\t\t\t\t\t\t\t\t\t"); } +static inline void dump_num_exp(int fd, num_exp *exp) { + while (exp) { + switch (exp->t) { + case PSI_T_NUMBER: + dprintf(fd, "%s", exp->u.numb); + break; + case PSI_T_NSNAME: + dprintf(fd, "%s", exp->u.cnst->name); + break; + case PSI_T_NAME: + dump_decl_var(fd, exp->u.dvar); + break; + EMPTY_SWITCH_DEFAULT_CASE(); + } + if (exp->operand) { + char op; + + switch (exp->operator) { + case PSI_T_PLUS: op = '+'; break; + case PSI_T_MINUS: op = '-'; break; + case PSI_T_ASTERISK:op = '*'; break; + case PSI_T_SLASH: op = '/'; break; + EMPTY_SWITCH_DEFAULT_CASE(); + } + dprintf(fd, " %c ", op); + } + exp = exp->operand; + } +} static inline void dump_impl_set_value(int fd, set_value *set, unsigned level) { size_t i; @@ -1147,6 +1241,10 @@ static inline void dump_impl_set_value(int fd, set_value *set, unsigned level) { } dump_decl_var(fd, svar); } + if (set->num) { + dprintf(fd, ", "); + dump_num_exp(fd, set->num); + } if (set->inner) { dprintf(fd, ",\n"); for (i = 0; i < set->count; ++i) { @@ -1161,20 +1259,6 @@ static inline void dump_impl_set_value(int fd, set_value *set, unsigned level) { dprintf(fd, ");\n"); } } -static inline void dump_num_exp(int fd, num_exp *exp) { - switch (exp->t) { - case PSI_T_NUMBER: - dprintf(fd, "%s", exp->u.numb); - break; - case PSI_T_NAME: - dump_decl_var(fd, exp->u.dvar); - break; - case PSI_T_NSNAME: - dprintf(fd, "%s", exp->u.cnst->name); - break; - EMPTY_SWITCH_DEFAULT_CASE(); - } -} void PSI_ContextDump(PSI_Context *C, int fd) { size_t i, j, k, l; diff --git a/src/module.c b/src/module.c index b06e0bd..53079d0 100644 --- a/src/module.c +++ b/src/module.c @@ -12,6 +12,7 @@ #include "php_psi.h" #include "parser.h" +#include "context.h" #if HAVE_LIBJIT # include "libjit.h" @@ -66,6 +67,7 @@ size_t psi_t_alignment(token_t t) return ALIGNOF_VOID_P; EMPTY_SWITCH_DEFAULT_CASE(); } + return 0; } size_t psi_t_size(token_t t) @@ -88,6 +90,7 @@ size_t psi_t_size(token_t t) return SIZEOF_VOID_P; EMPTY_SWITCH_DEFAULT_CASE(); } + return 0; } size_t psi_t_align(token_t t, size_t s) @@ -282,7 +285,11 @@ void psi_to_string(zval *return_value, set_value *set, impl_val *ret_val) } else { ret_val = deref_impl_val(ret_val, var); if (ret_val && ret_val->ptr) { - RETVAL_STRING(ret_val->ptr); + if (set->num) { + RETVAL_STRINGL(ret_val->ptr, psi_long_num_exp(set->num, NULL)); + } else { + RETVAL_STRING(ret_val->ptr); + } } else { RETVAL_EMPTY_STRING(); } @@ -426,7 +433,7 @@ void psi_to_array(zval *return_value, set_value *set, impl_val *r_val) zval ele; if (set->outer.set) { - /* struct *//* + / * struct *//* for (i = 1; i < set->vars->count; ++i) { impl_val *tmp = NULL, *cnt_val; decl_var *cnt_var = set->vars->vars[i]; @@ -485,6 +492,8 @@ void psi_to_object(zval *return_value, set_value *set, impl_val *r_val) static inline ZEND_RESULT_CODE psi_parse_args(zend_execute_data *execute_data, impl *impl) { + size_t i; + zval *zarg = ZEND_CALL_ARG(execute_data, 0); impl_arg *iarg; zend_error_handling zeh; @@ -498,6 +507,39 @@ static inline ZEND_RESULT_CODE psi_parse_args(zend_execute_data *execute_data, i return rv; } + /* set up defaults */ + for (i = 0; i < impl->func->args->count; ++i) { + iarg = impl->func->args->args[i]; + + if (i < EX_NUM_ARGS()) { + iarg->_zv = ++zarg; + ZVAL_DEREF(iarg->_zv); + if (iarg->var->reference) { + zval_dtor(iarg->_zv); + ZVAL_NULL(iarg->_zv); + } + } + + if (iarg->def) { + switch (iarg->type->type) { + case PSI_T_BOOL: + iarg->val.zend.bval = iarg->def->type == PSI_T_TRUE ? 1 : 0; + break; + case PSI_T_INT: + iarg->val.zend.lval = zend_atol(iarg->def->text, strlen(iarg->def->text)); + break; + case PSI_T_FLOAT: + case PSI_T_DOUBLE: + iarg->val.dval = zend_strtod(iarg->def->text, NULL); + break; + case PSI_T_STRING: + /* FIXME */ + iarg->val.zend.str = zend_string_init(&iarg->def->text[1], strlen(iarg->def->text) - 2, 0); + break; + } + } + } + ZEND_PARSE_PARAMETERS_START(psi_num_min_args(impl), impl->func->args->count) nextarg: iarg = impl->func->args->args[_i]; @@ -520,29 +562,20 @@ static inline ZEND_RESULT_CODE psi_parse_args(zend_execute_data *execute_data, i } Z_PARAM_DOUBLE(iarg->val.dval); } else if (PSI_T_STRING == iarg->type->type) { - struct {char *val; size_t len;} str; - if (iarg->def) { - /* FIXME */ - str.len = strlen(iarg->def->text) - 2; - str.val = &iarg->def->text[1]; - } - Z_PARAM_STR_EX(iarg->val.zend.str, 1, 0); - if (iarg->val.zend.str) { - zend_string_addref(iarg->val.zend.str); - } else if (iarg->def) { - iarg->val.zend.str = zend_string_init(str.val, str.len, 0); + zend_string *str = iarg->val.zend.str; + Z_PARAM_STR_EX(iarg->val.zend.str, 1, iarg->var->reference); + if (str && str != iarg->val.zend.str) { + zend_string_release(str); } } else if (PSI_T_ARRAY == iarg->type->type) { - /* handled as _zv in let or set */ - Z_PARAM_ARRAY_EX(iarg->_zv, 1, 0); + Z_PARAM_PROLOGUE(0); } else if (PSI_T_OBJECT == iarg->type->type) { - Z_PARAM_OBJECT_EX(iarg->_zv, 1, 0); + Z_PARAM_PROLOGUE(0); } else { error_code = ZPP_ERROR_FAILURE; break; } - iarg->_zv = _arg; - if (_i < _max_num_args) { + if (_i < _num_args) { goto nextarg; } ZEND_PARSE_PARAMETERS_END_EX( @@ -562,13 +595,11 @@ static inline void *psi_do_calloc(let_calloc *alloc) return mem; } -static inline void *psi_do_let(decl_arg *darg) +static inline void *psi_do_let(let_stmt *let) { + decl_arg *darg = let->var->arg; impl_arg *iarg = darg->let->arg; - impl_val *arg_val; - - darg->let->ptr = &darg->let->out; - arg_val = darg->let->ptr; + impl_val *arg_val = darg->ptr; if (!iarg) { /* let foo = calloc(1, long); @@ -577,10 +608,10 @@ static inline void *psi_do_let(decl_arg *darg) */ if (darg->let->val->func && darg->let->val->func->type == PSI_T_CALLOC) { arg_val->ptr = psi_do_calloc(darg->let->val->func->alloc); - darg->let->mem = arg_val->ptr; + darg->mem = arg_val->ptr; } else if (darg->var->array_size) { arg_val->ptr = ecalloc(darg->var->array_size, sizeof(*arg_val)); - darg->let->mem = arg_val->ptr; + darg->mem = arg_val->ptr; } else { memset(arg_val, 0, sizeof(*arg_val)); } @@ -605,12 +636,12 @@ static inline void *psi_do_let(decl_arg *darg) case PSI_T_STRVAL: if (iarg->type->type == PSI_T_STRING) { arg_val->ptr = estrdup(iarg->val.zend.str->val); - darg->let->mem = arg_val->ptr; + darg->mem = arg_val->ptr; zend_string_release(iarg->val.zend.str); } else { zend_string *zs = zval_get_string(iarg->_zv); arg_val->ptr = estrdup(zs->val); - darg->let->mem = arg_val->ptr; + darg->mem = arg_val->ptr; zend_string_release(zs); } if (PSI_T_PATHVAL == darg->let->val->func->type) { @@ -637,7 +668,7 @@ static inline void *psi_do_let(decl_arg *darg) switch (type->type) { case PSI_T_STRUCT: arg_val->ptr = psi_array_to_struct(type->strct, HASH_OF(iarg->_zv)); - darg->let->mem = arg_val->ptr; + darg->mem = arg_val->ptr; break; } } @@ -659,21 +690,23 @@ static inline void *psi_do_let(decl_arg *darg) } if (darg->let->val && darg->let->val->is_reference) { - return &darg->let->ptr; + return darg->let->ptr = &darg->ptr; } else { - return darg->let->ptr; + return darg->let->ptr = darg->ptr; } } static inline void psi_do_set(zval *return_value, set_value *set) { - ZVAL_DEREF(return_value); - zval_dtor(return_value); + set->func->handler(return_value, set, set->vars->vars[0]->arg->ptr); +} - set->func->handler(return_value, set, set->vars->vars[0]->arg->let->ptr); +static inline void psi_do_return(zval *return_value, return_stmt *ret) +{ + ret->set->func->handler(return_value, ret->set, ret->set->vars->vars[0]->arg->ptr); } -static inline void psi_do_return(zval *return_value, return_stmt *ret, impl_val *ret_val) +static inline void psi_do_return2(zval *return_value, return_stmt *ret, impl_val *ret_val) { ret->set->func->handler(return_value, ret->set, ret_val); } @@ -690,7 +723,7 @@ static inline void psi_do_free(free_stmt *fre) decl_var *dvar = f->vars->vars[j]; decl_arg *darg = dvar->arg; - f->decl->call.args[j] = &darg->let->out; + f->decl->call.args[j] = &darg->val; } /* FIXME: check in validate_* that free functions return scalar */ @@ -717,18 +750,18 @@ static inline void psi_do_clean(impl *impl) if (impl->decl->args) for (i = 0; i < impl->decl->args->count; ++i) { decl_arg *darg = impl->decl->args->args[i]; - if (darg->let && darg->let->mem) { + if (darg->mem) { decl_type *type = real_decl_type(darg->type); if (type->type == PSI_T_STRUCT) { - void **ptr = (void **) ((char *) darg->let->mem + type->strct->size); + void **ptr = (void **) ((char *) darg->mem + type->strct->size); while (*ptr) { efree(*ptr++); } } - efree(darg->let->mem); - darg->let->mem = NULL; + efree(darg->mem); + darg->mem = NULL; } } } @@ -760,8 +793,8 @@ static inline int psi_calc_num_exp_value(num_exp *exp, impl_val *ref, impl_val * break; case PSI_T_NAME: - if (exp->u.dvar->arg->let) { - ref = exp->u.dvar->arg->let->ptr; + if (1) { + ref = exp->u.dvar->arg->ptr; } else { ref = struct_member_ref(exp->u.dvar->arg, ref, &tmp); } @@ -816,14 +849,14 @@ int psi_calc_num_exp(num_exp *exp, impl_val *ref, impl_val *res) { #define PRIdval "lf" #define PSI_CALC_OP(var) do { \ - const char *fmt = "calc: %" PRI##var ", %" PRI##var ": %" PRI##var "\n"; \ + const char *fmt = "calc %" PRI##var ", %" PRI##var ": %" PRI##var "\n"; \ res->var = PSI_CALC(v1->var, v2->var); \ - /*fprintf(stderr, fmt, v1->var, v2->var, res->var);*/ \ + if (!res->var) fprintf(stderr, fmt, v1->var, v2->var, res->var); \ } while (0) #define PSI_CALC_OP2(vres, var1, var2) do { \ - const char *fmt = "calc: %" PRI##var1 ", %" PRI##var2 ": %" PRI##vres "\n"; \ + const char *fmt = "calc %" PRI##var1 ", %" PRI##var2 ": %" PRI##vres "\n"; \ res->vres = PSI_CALC(v1->var1, v2->var2); \ - /*fprintf(stderr, fmt, v1->var1, v2->var2, res->vres);*/ \ + if (!res->vres) fprintf(stderr, fmt, v1->var1, v2->var2, res->vres); \ } while(0) #define PSI_CALC_FN(op) int psi_calc_##op(int t1, impl_val *v1, int t2, impl_val *v2, impl_val *res) \ { \ @@ -961,27 +994,32 @@ PSI_CALC_FN(div) void psi_call(zend_execute_data *execute_data, zval *return_value, impl *impl) { - impl_val ret_val; size_t i; + return_stmt *ret = impl->stmts->ret.list[0]; + decl_var *var = ret->set->vars->vars[0]; + + memset(var->arg->ptr, 0, sizeof(impl_val)); 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]; + for (i = 0; i < impl->stmts->let.count; ++i) { + let_stmt *let = impl->stmts->let.list[i]; - if (!(impl->decl->call.args[i] = psi_do_let(darg))) { - goto cleanup; - } + if (!psi_do_let(let)) { + psi_do_return(return_value, ret); + psi_do_clean(impl); + return; } } - memset(&ret_val, 0, sizeof(ret_val)); - PSI_ContextCall(&PSI_G(context), &ret_val, impl->decl); + if (impl->decl->args) for (i = 0; i < impl->decl->args->count; ++i) { + impl->decl->call.args[i] = impl->decl->args->args[i]->let->ptr; + } - psi_do_return(return_value, impl->stmts->ret.list[0], &ret_val); + PSI_ContextCall(&PSI_G(context), var->arg->ptr, impl->decl); + psi_do_return(return_value, ret); for (i = 0; i < impl->stmts->set.count; ++i) { set_stmt *set = impl->stmts->set.list[i]; @@ -997,12 +1035,6 @@ void psi_call(zend_execute_data *execute_data, zval *return_value, impl *impl) psi_do_free(fre); } psi_do_clean(impl); - return; - -cleanup: - memset(&ret_val, 0, sizeof(ret_val)); - psi_do_return(return_value, impl->stmts->ret.list[0], &ret_val); - psi_do_clean(impl); } static void psi_object_free(zend_object *o) @@ -1065,6 +1097,7 @@ PHP_MINIT_FUNCTION(psi) return SUCCESS; } + PHP_MSHUTDOWN_FUNCTION(psi) { PSI_ContextDtor(&PSI_G(context)); @@ -1074,26 +1107,13 @@ PHP_MSHUTDOWN_FUNCTION(psi) return SUCCESS; } -/* Remove if there's nothing to do at request start */ -/* {{{ PHP_RINIT_FUNCTION - */ +#if defined(COMPILE_DL_PSI) && defined(ZTS) PHP_RINIT_FUNCTION(psi) { -#if defined(COMPILE_DL_PSI) && defined(ZTS) ZEND_TSRMLS_CACHE_UPDATE(); -#endif return SUCCESS; } -/* }}} */ - -/* Remove if there's nothing to do at request end */ -/* {{{ PHP_RSHUTDOWN_FUNCTION - */ -PHP_RSHUTDOWN_FUNCTION(psi) -{ - return SUCCESS; -} -/* }}} */ +#endif PHP_MINFO_FUNCTION(psi) { @@ -1113,8 +1133,12 @@ zend_module_entry psi_module_entry = { psi_functions, PHP_MINIT(psi), PHP_MSHUTDOWN(psi), +#if defined(COMPILE_DL_PSI) && defined(ZTS) PHP_RINIT(psi), /* Replace with NULL if there's nothing to do at request start */ - PHP_RSHUTDOWN(psi), /* Replace with NULL if there's nothing to do at request end */ +#else + NULL, +#endif + NULL, PHP_MINFO(psi), PHP_PSI_VERSION, STANDARD_MODULE_PROPERTIES diff --git a/src/parser.h b/src/parser.h index 2e551dc..4df5354 100644 --- a/src/parser.h +++ b/src/parser.h @@ -26,6 +26,30 @@ typedef struct PSI_Token { char text[1]; } PSI_Token; +typedef union impl_val { + char cval; + int8_t i8; + uint8_t u8; + short sval; + int16_t i16; + uint16_t u16; + int ival; + int32_t i32; + uint32_t u32; + long lval; + int64_t i64; + uint64_t u64; + float fval; + double dval; + union { + zend_bool bval; + zend_long lval; + zend_string *str; + } zend; + void *ptr; + uint8_t _dbg[sizeof(void *)]; +} impl_val; + typedef struct decl_type { char *name; token_t type; @@ -136,6 +160,9 @@ typedef struct decl_arg { decl_var *var; decl_struct_layout *layout; struct let_stmt *let; + impl_val val; + void *ptr; + void *mem; } decl_arg; static inline decl_arg *init_decl_arg(decl_type *type, decl_var *var) { @@ -143,6 +170,7 @@ static inline decl_arg *init_decl_arg(decl_type *type, decl_var *var) { arg->type = type; arg->var = var; var->arg = arg; + arg->ptr = &arg->val; return arg; } @@ -330,55 +358,6 @@ static inline void free_decl_structs(decl_structs *ss) { free(ss); } -typedef union impl_val { - char cval; - int8_t i8; - uint8_t u8; - short sval; - int16_t i16; - uint16_t u16; - int ival; - int32_t i32; - uint32_t u32; - long lval; - int64_t i64; - uint64_t u64; - float fval; - double dval; - union { - zend_bool bval; - zend_long lval; - zend_string *str; - } zend; - void *ptr; - uint8_t _dbg[sizeof(void *)]; -} impl_val; - -static inline impl_val *deref_impl_val(impl_val *ret_val, decl_var *var) { - unsigned i; - - if (var->arg->var != var) for (i = 1; i < var->pointer_level; ++i) { - ret_val = *(void **) ret_val; - } - return ret_val; -} - -static inline impl_val *enref_impl_val(void *ptr, decl_var *var) { - impl_val *val, *val_ptr; - unsigned i; - - if (!var->pointer_level && real_decl_type(var->arg->type)->type != PSI_T_STRUCT) { - return ptr; - } - val = val_ptr = calloc(var->pointer_level + 1, sizeof(void *)); - for (i = 1; i < var->pointer_level; ++i) { - val_ptr->ptr = (void **) val_ptr + 1; - val_ptr = val_ptr->ptr; - } - val_ptr->ptr = ptr; - return val; -} - typedef struct impl_type { char *name; token_t type; @@ -685,9 +664,7 @@ typedef struct let_stmt { decl_var *var; let_value *val; impl_arg *arg; - impl_val out; void *ptr; - void *mem; } let_stmt; static inline let_stmt *init_let_stmt(decl_var *var, let_value *val) { @@ -1013,21 +990,6 @@ static void free_impls(impls *impls) { free(impls); } -static inline impl_val *struct_member_ref(decl_arg *set_arg, impl_val *struct_ptr, impl_val **to_free) { - void *ptr = (char *) struct_ptr->ptr + set_arg->layout->pos; - impl_val *val = enref_impl_val(ptr, set_arg->var); - - if (val != ptr) { - *to_free = val; - } - - return val; -} - -#define PSI_ERROR 16 -#define PSI_WARNING 32 -typedef void (*psi_error_cb)(int type, const char *msg, ...); - typedef struct decl_file { char *ln; char *fn; @@ -1066,6 +1028,46 @@ static inline void add_decl_lib(decl_libs *libs, void *dlopened) { libs->dl[libs->count-1] = dlopened; } +static inline impl_val *deref_impl_val(impl_val *ret_val, decl_var *var) { + unsigned i; + + if (var->arg->var != var) for (i = 1; i < var->pointer_level; ++i) { + ret_val = *(void **) ret_val; + } + return ret_val; +} + +static inline impl_val *enref_impl_val(void *ptr, decl_var *var) { + impl_val *val, *val_ptr; + unsigned i; + + if (!var->pointer_level && real_decl_type(var->arg->type)->type != PSI_T_STRUCT) { + return ptr; + } + val = val_ptr = calloc(var->pointer_level + 1, sizeof(void *)); + for (i = 1; i < var->pointer_level; ++i) { + val_ptr->ptr = (void **) val_ptr + 1; + val_ptr = val_ptr->ptr; + } + val_ptr->ptr = ptr; + return val; +} + +static inline impl_val *struct_member_ref(decl_arg *set_arg, impl_val *struct_ptr, impl_val **to_free) { + void *ptr = (char *) struct_ptr->ptr + set_arg->layout->pos; + impl_val *val = enref_impl_val(ptr, set_arg->var); + + if (val != ptr) { + *to_free = val; + } + + return val; +} + +#define PSI_ERROR 16 +#define PSI_WARNING 32 +typedef void (*psi_error_cb)(int type, const char *msg, ...); + #define PSI_DATA(D) ((PSI_Data *) (D)) #define PSI_DATA_MEMBERS \ constants *consts; \