flush
[m6w6/ext-psi] / src / module.c
index 873d56bc10192521c211dd55cde3de1e0e6f42f2..c820f4f1bd0ed47b6b2a873790a78fd3d6f8dbbd 100644 (file)
@@ -11,7 +11,6 @@
 #include "zend_operators.h"
 
 #include "php_psi.h"
-#include "parser.h"
 
 #if HAVE_LIBJIT
 # include "libjit.h"
@@ -66,6 +65,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 +88,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)
@@ -121,13 +122,26 @@ zend_internal_arg_info *psi_internal_arginfo(impl *impl)
        zend_internal_arg_info *aip;
        zend_internal_function_info *fi;
 
-       aip = calloc(impl->func->args->count + 1, sizeof(*aip));
+       aip = calloc(impl->func->args->count + 1 + !!impl->func->args->vararg, sizeof(*aip));
 
        fi = (zend_internal_function_info *) &aip[0];
+       fi->allow_null = 1;
        fi->required_num_args = psi_num_min_args(impl);
        fi->return_reference = impl->func->return_reference;
        fi->type_hint = psi_internal_type(impl->func->return_type);
 
+       if (impl->func->args->vararg) {
+               impl_arg *vararg = impl->func->args->vararg;
+               zend_internal_arg_info *ai = &aip[impl->func->args->count];
+
+               ai->name = vararg->var->name;
+               ai->type_hint = psi_internal_type(vararg->type);
+               if (vararg->var->reference) {
+                       ai->pass_by_reference = 1;
+               }
+               ai->is_variadic = 1;
+       }
+
        for (i = 0; i < impl->func->args->count; ++i) {
                impl_arg *iarg = impl->func->args->args[i];
                zend_internal_arg_info *ai = &aip[i+1];
@@ -282,7 +296,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, set->outer.val));
+                               } else {
+                                       RETVAL_STRING(ret_val->ptr);
+                               }
                        } else {
                                RETVAL_EMPTY_STRING();
                        }
@@ -359,16 +377,6 @@ void *psi_array_to_struct(decl_struct *s, HashTable *arr)
        return mem;
 }
 
-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;
-}
 void psi_to_array(zval *return_value, set_value *set, impl_val *r_val)
 {
        size_t i;
@@ -376,6 +384,10 @@ void psi_to_array(zval *return_value, set_value *set, impl_val *r_val)
        token_t t = real_decl_type(var->arg->type)->type;
        impl_val tmp, *ret_val = deref_impl_val(r_val, var);
 
+       if ((intptr_t) ret_val->ptr <= (intptr_t) 0) {
+               RETURN_NULL();
+       }
+
        array_init(return_value);
 
        if (t == PSI_T_STRUCT) {
@@ -428,31 +440,13 @@ void psi_to_array(zval *return_value, set_value *set, impl_val *r_val)
                        add_next_index_zval(return_value, &ele);
                }
                return;
-       } else if (set->vars->count > 1) {
-               /* to_array(arr_var, cnt_var[, cnt_var...], to_int(*arr_var))
-                * check for length in second var
+       } else if (set->num) {
+               /* to_array(arr_var, num_expr, to_int(*arr_var))
                 */
-               size_t count = 0;
                zval ele;
+               zend_long i, n = psi_long_num_exp(set->num, set->outer.val);
 
-               if (set->outer.set) {
-                       /* struct */
-                       for (i = 1; i < set->vars->count; ++i) {
-                               impl_val *tmp = NULL, *cnt_val;
-                               decl_var *cnt_var = set->vars->vars[i];
-
-                               cnt_val = struct_member_ref(cnt_var->arg, set->outer.val, &tmp);
-                               count += deref_impl_val(cnt_val, cnt_var)->lval;
-
-                               if (tmp) {
-                                       free(tmp);
-                               }
-                       }
-               } else {
-                       ZEND_ASSERT(0);
-               }
-
-               for (i = 0; i < count; ++i) {
+               for (i = 0; i < n; ++i) {
                        size_t size = psi_t_size(var->arg->var->pointer_level ? PSI_T_POINTER : t);
                        impl_val *ptr = iterate(ret_val, size, i, &tmp);
 
@@ -470,7 +464,7 @@ void psi_to_object(zval *return_value, set_value *set, impl_val *r_val)
        impl_val *ret_val = deref_impl_val(r_val, var);
        psi_object *obj;
 
-       if (ret_val->ptr) {
+       if ((intptr_t) ret_val->ptr > (intptr_t) 0) {
                object_init_ex(return_value, psi_class_entry);
                obj = PSI_OBJ(return_value, NULL);
                obj->data = ret_val->ptr;
@@ -481,6 +475,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;
 
@@ -494,51 +490,46 @@ static inline ZEND_RESULT_CODE psi_parse_args(zend_execute_data *execute_data, i
                return rv;
        }
 
-       ZEND_PARSE_PARAMETERS_START(psi_num_min_args(impl), impl->func->args->count)
+       ZEND_PARSE_PARAMETERS_START(psi_num_min_args(impl), impl->func->args->vararg ? -1 : impl->func->args->count)
        nextarg:
                iarg = impl->func->args->args[_i];
                if (iarg->def) {
                        Z_PARAM_OPTIONAL;
                }
                if (PSI_T_BOOL == iarg->type->type) {
-                       if (iarg->def) {
-                               iarg->val.zend.bval = iarg->def->type == PSI_T_TRUE ? 1 : 0;
-                       }
                        Z_PARAM_BOOL(iarg->val.zend.bval);
                } else if (PSI_T_INT == iarg->type->type) {
-                       if (iarg->def) {
-                               iarg->val.zend.lval = zend_atol(iarg->def->text, strlen(iarg->def->text));
-                       }
                        Z_PARAM_LONG(iarg->val.zend.lval);
                } else if (PSI_T_FLOAT == iarg->type->type || PSI_T_DOUBLE == iarg->type->type) {
-                       if (iarg->def) {
-                               iarg->val.dval = zend_strtod(iarg->def->text, NULL);
-                       }
                        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);
+                       Z_PARAM_STR_EX(iarg->val.zend.str, 1, iarg->var->reference);
                        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);
                        }
                } 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 (impl->func->args->vararg) {
+                       iarg = impl->func->args->vararg;
+
+                       if (_i == impl->func->args->count) {
+                               zval *ptr = iarg->_zv = calloc(_num_args - _i + 1, sizeof(zval));
+
+                               _optional = 1;
+                               while (_i < _num_args) {
+                                       Z_PARAM_PROLOGUE(0);
+                                       ZVAL_COPY_VALUE(ptr++, _arg);
+                               }
+                               break;
+                       }
+               }
+               if (_i < _num_args) {
                        goto nextarg;
                }
        ZEND_PARSE_PARAMETERS_END_EX(
@@ -546,43 +537,74 @@ static inline ZEND_RESULT_CODE psi_parse_args(zend_execute_data *execute_data, i
                return FAILURE
        );
 
+       /* 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);
+               } else 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_restore_error_handling(&zeh);
        return SUCCESS;
 }
 
 static inline void *psi_do_calloc(let_calloc *alloc)
 {
-       zend_long n = psi_long_num_exp(alloc->nmemb), s = psi_long_num_exp(alloc->size);
+       zend_long n = psi_long_num_exp(alloc->nmemb, NULL), s = psi_long_num_exp(alloc->size, NULL);
        void *mem = safe_emalloc(n, s, sizeof(void *));
        memset(mem, 0, n * s + sizeof(void *));
        return mem;
 }
 
-static inline void *psi_do_let(decl_arg *darg)
+static inline void *psi_do_let(let_stmt *let)
 {
-       impl_arg *iarg = darg->let->arg;
-       impl_val *arg_val;
-
-       darg->let->ptr = &darg->let->out;
-       arg_val = darg->let->ptr;
+       decl_arg *darg = let->var->arg;
+       impl_val *arg_val = darg->ptr;
+       impl_arg *iarg;
 
-       if (!iarg) {
-               /* let foo = calloc(1, long);
-                * let foo = NULL;
-                * let foo;
-                */
-               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;
-               } else if (darg->var->array_size) {
+       switch (let->val ? let->val->kind : PSI_LET_NULL) {
+       case PSI_LET_TMP:
+               arg_val->ptr = deref_impl_val(let->val->data.var->arg->val.ptr, let->var);
+               break;
+       case PSI_LET_NULL:
+               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));
                }
-       } else {
+               break;
+       case PSI_LET_CALLOC:
+               arg_val->ptr = psi_do_calloc(let->val->data.alloc);
+               darg->mem = arg_val->ptr;
+               break;
+       case PSI_LET_NUMEXP:
+               arg_val->zend.lval = psi_long_num_exp(let->val->data.num, NULL);
+               break;
+       case PSI_LET_FUNC:
+               iarg = let->val->data.func->arg;
 
-               switch (darg->let->val->func->type) {
+               switch (let->val->data.func->type) {
                case PSI_T_BOOLVAL:
                        if (iarg->type->type == PSI_T_BOOL) {
                                arg_val->cval = iarg->val.zend.bval;
@@ -600,16 +622,19 @@ static inline void *psi_do_let(decl_arg *darg)
                case PSI_T_PATHVAL:
                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;
-                               zend_string_release(iarg->val.zend.str);
+                               if (iarg->val.zend.str) {
+                                       arg_val->ptr = estrdup(iarg->val.zend.str->val);
+                                       darg->mem = arg_val->ptr;
+                               } else {
+                                       arg_val->ptr = "";
+                               }
                        } 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) {
+                       if (PSI_T_PATHVAL == darg->let->val->data.func->type) {
                                if (SUCCESS != php_check_open_basedir(arg_val->ptr)) {
                                        efree(arg_val->ptr);
                                        return NULL;
@@ -618,8 +643,11 @@ static inline void *psi_do_let(decl_arg *darg)
                        break;
                case PSI_T_STRLEN:
                        if (iarg->type->type == PSI_T_STRING) {
-                               arg_val->lval = iarg->val.zend.str->len;
-                               zend_string_release(iarg->val.zend.str);
+                               if (iarg->val.zend.str) {
+                                       arg_val->lval = iarg->val.zend.str->len;
+                               } else {
+                                       arg_val->lval = 0;
+                               }
                        } else {
                                zend_string *zs = zval_get_string(iarg->_zv);
                                arg_val->lval = zs->len;
@@ -633,7 +661,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;
                                }
                        }
@@ -654,24 +682,22 @@ static inline void *psi_do_let(decl_arg *darg)
                }
        }
 
-       if (darg->let->val && darg->let->val->is_reference) {
-               return &darg->let->ptr;
+       if (let->val && let->val->flags.one.is_reference) {
+               return let->ptr = &darg->ptr;
        } else {
-               return darg->let->ptr;
+               return 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->let->ptr);
+       set->func->handler(return_value, set, 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_return(zval *return_value, return_stmt *ret)
 {
-       ret->set->func->handler(return_value, ret->set, ret_val);
+       ret->set->func->handler(return_value, ret->set, ret->set->vars->vars[0]->arg->ptr);
 }
 
 static inline void psi_do_free(free_stmt *fre)
@@ -686,7 +712,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 */
@@ -713,173 +739,276 @@ 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;
                }
        }
 }
 
+static inline int psi_calc_num_exp_value(num_exp *exp, impl_val *strct, impl_val *res) {
+       impl_val *ref, *tmp = NULL;
 
-#define PSI_CALC_OP(var) res->var = PSI_CALC(v1->var, v2->var)
-#define PSI_CALC_OP2(vres, var1, var2) res->vres = PSI_CALC(v1->var1, v2->var2)
-
-int psi_calc_plus(unsigned char t1, impl_val *v1, unsigned char t2, impl_val *v2, impl_val *res)
-{
-#undef PSI_CALC
-#define PSI_CALC(var1, var2) (var1) + (var2)
-       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_INT8:        PSI_CALC_OP(i8);        break;
-               case PSI_T_UINT8:       PSI_CALC_OP(u8);        break;
-               case PSI_T_INT16:       PSI_CALC_OP(i16);       break;
-               case PSI_T_UINT16:      PSI_CALC_OP(u16);       break;
-               case PSI_T_INT32:       PSI_CALC_OP(i32);       break;
-               case PSI_T_UINT32:      PSI_CALC_OP(u32);       break;
-               case PSI_T_INT64:       PSI_CALC_OP(i64);       break;
-               case PSI_T_UINT64:      PSI_CALC_OP(u64);       break;
-               EMPTY_SWITCH_DEFAULT_CASE();
-               }
-               return t1;
-       } else if (t1 == PSI_T_DOUBLE) {
-               switch (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;
-               case PSI_T_INT16:       PSI_CALC_OP2(dval, dval, i16);  break;
-               case PSI_T_UINT16:      PSI_CALC_OP2(dval, dval, u16);  break;
-               case PSI_T_INT32:       PSI_CALC_OP2(dval, dval, i32);  break;
-               case PSI_T_UINT32:      PSI_CALC_OP2(dval, dval, u32);  break;
-               case PSI_T_INT64:       PSI_CALC_OP2(dval, dval, i64);  break;
-               case PSI_T_UINT64:      PSI_CALC_OP2(dval, dval, u64);  break;
-               EMPTY_SWITCH_DEFAULT_CASE();
-               }
-               return t1;
-       } else if (t2 == PSI_T_DOUBLE) {
-               switch (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;
-               case PSI_T_INT16:       PSI_CALC_OP2(dval, i16, dval);  break;
-               case PSI_T_UINT16:      PSI_CALC_OP2(dval, u16, dval);  break;
-               case PSI_T_INT32:       PSI_CALC_OP2(dval, i32, dval);  break;
-               case PSI_T_UINT32:      PSI_CALC_OP2(dval, u32, dval);  break;
-               case PSI_T_INT64:       PSI_CALC_OP2(dval, i64, dval);  break;
-               case PSI_T_UINT64:      PSI_CALC_OP2(dval, u64, dval);  break;
-               EMPTY_SWITCH_DEFAULT_CASE();
-               }
-               return t2;
-       } else if (t1 == PSI_T_FLOAT) {
-               switch (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;
-               case PSI_T_INT16:       PSI_CALC_OP2(fval, fval, i16);  break;
-               case PSI_T_UINT16:      PSI_CALC_OP2(fval, fval, u16);  break;
-               case PSI_T_INT32:       PSI_CALC_OP2(fval, fval, i32);  break;
-               case PSI_T_UINT32:      PSI_CALC_OP2(fval, fval, u32);  break;
-               case PSI_T_INT64:       PSI_CALC_OP2(fval, fval, i64);  break;
-               case PSI_T_UINT64:      PSI_CALC_OP2(fval, fval, u64);  break;
-               EMPTY_SWITCH_DEFAULT_CASE();
+       switch (exp->t) {
+       case PSI_T_NUMBER:
+               switch (is_numeric_string(exp->u.numb, strlen(exp->u.numb), (zend_long *) res, (double *) res, 0)) {
+               case IS_LONG:
+                       return PSI_T_INT64;
+               case IS_DOUBLE:
+                       return PSI_T_DOUBLE;
                }
-               return t1;
-       } else if (t2 == PSI_T_FLOAT) {
-               switch (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;
-               case PSI_T_INT16:       PSI_CALC_OP2(fval, i16, fval);  break;
-               case PSI_T_UINT16:      PSI_CALC_OP2(fval, u16, fval);  break;
-               case PSI_T_INT32:       PSI_CALC_OP2(fval, i32, fval);  break;
-               case PSI_T_UINT32:      PSI_CALC_OP2(fval, u32, fval);  break;
-               case PSI_T_INT64:       PSI_CALC_OP2(fval, i64, fval);  break;
-               case PSI_T_UINT64:      PSI_CALC_OP2(fval, u64, fval);  break;
-               EMPTY_SWITCH_DEFAULT_CASE();
+               break;
+
+       case PSI_T_NSNAME:
+               switch (exp->u.cnst->type->type) {
+               case PSI_T_INT:
+                       res->i64 = zend_get_constant_str(exp->u.cnst->name, strlen(exp->u.cnst->name))->value.lval;
+                       return PSI_T_INT64;
+               case PSI_T_FLOAT:
+                       res->dval = zend_get_constant_str(exp->u.cnst->name, strlen(exp->u.cnst->name))->value.dval;
+                       return PSI_T_DOUBLE;
+               default:
+                       return 0;
                }
-               return t2;
-       } else {
-               int64_t sval1 = v1->i64, sval2 = v2->i64;
-               uint64_t uval1 = v1->u64, uval2 = v2->u64;
+               break;
 
-               switch (t1) {
-               case PSI_T_INT8:        sval1 >>= 8;
-               case PSI_T_INT16:       sval1 >>= 8;
-               case PSI_T_INT32:       sval1 >>= 8;
+       case PSI_T_NAME:
+               if (strct) {
+                       ref = struct_member_ref(exp->u.dvar->arg, strct, &tmp);
+               } else {
+                       ref = exp->u.dvar->arg->ptr;
+               }
+               switch (real_decl_type(exp->u.dvar->arg->type)->type) {
+               case PSI_T_INT8:
+               case PSI_T_UINT8:
+               case PSI_T_INT16:
+               case PSI_T_UINT16:
+               case PSI_T_INT32:
+               case PSI_T_UINT32:
                case PSI_T_INT64:
-                       switch (t2) {
-                       case PSI_T_INT8:        sval2 >>= 8;
-                       case PSI_T_INT16:       sval2 >>= 8;
-                       case PSI_T_INT32:       sval2 >>= 8;
-                       case PSI_T_INT64:
-                               res->i64 = PSI_CALC(sval1 , sval2);
-                               return PSI_T_INT64;
-                       case PSI_T_UINT8:       uval2 >>= 8;
-                       case PSI_T_UINT16:      uval2 >>= 8;
-                       case PSI_T_UINT32:      uval2 >>= 8;
-                       case PSI_T_UINT64:
-                               res->i64 = PSI_CALC(sval1, uval2);
-                               return PSI_T_INT64;
-                       }
-                       break;
-               case PSI_T_UINT8:       uval1 >>= 8;
-               case PSI_T_UINT16:      uval1 >>= 8;
-               case PSI_T_UINT32:      uval1 >>= 8;
                case PSI_T_UINT64:
-                       switch (t2) {
-                       case PSI_T_INT8:        sval2 >>= 8;
-                       case PSI_T_INT16:       sval2 >>= 8;
-                       case PSI_T_INT32:       sval2 >>= 8;
-                       case PSI_T_INT64:
-                               res->i64 = PSI_CALC(uval1, sval2);
-                               return PSI_T_INT64;
-                       case PSI_T_UINT8:       uval2 >>= 8;
-                       case PSI_T_UINT16:      uval2 >>= 8;
-                       case PSI_T_UINT32:      uval2 >>= 8;
-                       case PSI_T_UINT64:
-                               res->u64 = PSI_CALC(uval1, uval2);
-                               return PSI_T_UINT64;
+                       memcpy(res, deref_impl_val(ref, exp->u.dvar), sizeof(*res));
+                       if (tmp) {
+                               free(tmp);
                        }
-                       break;
+                       return real_decl_type(exp->u.dvar->arg->type)->type;
+
+               case PSI_T_FLOAT:
+               case PSI_T_DOUBLE:
+                       memcpy(res, deref_impl_val(ref, exp->u.dvar), sizeof(*res));
+                       if (tmp) {
+                               free(tmp);
+                       }
+                       return real_decl_type(exp->u.dvar->arg->type)->type;
+
+               EMPTY_SWITCH_DEFAULT_CASE();
                }
+               break;
+
+       EMPTY_SWITCH_DEFAULT_CASE();
        }
-       ZEND_ASSERT(0);
-       return 0;
+       return  0;
+}
+
+int psi_calc_num_exp(num_exp *exp, impl_val *strct, impl_val *res) {
+       impl_val num = {0};
+       int num_type = psi_calc_num_exp_value(exp, strct, &num);
+
+       if (exp->operand) {
+               impl_val tmp = {0};
+               int tmp_type = psi_calc_num_exp(exp->operand, strct, &tmp);
+
+               return exp->calculator(num_type, &num, tmp_type, &tmp, res);
+       }
+
+       memcpy(res, &num, sizeof(*res));
+       return num_type;
+}
+
+#define PRIfval "f"
+#define PRIdval "lf"
+
+#define PSI_CALC_OP(var) do { \
+       const char *fmt = "calc %" PRI##var ", %" PRI##var ": %" PRI##var "\n"; \
+       res->var = PSI_CALC(v1->var, v2->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"; \
+       res->vres = PSI_CALC(v1->var1, v2->var2); \
+       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) \
+{ \
+       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_INT8:        PSI_CALC_OP(i8);        break; \
+               case PSI_T_UINT8:       PSI_CALC_OP(u8);        break; \
+               case PSI_T_INT16:       PSI_CALC_OP(i16);       break; \
+               case PSI_T_UINT16:      PSI_CALC_OP(u16);       break; \
+               case PSI_T_INT32:       PSI_CALC_OP(i32);       break; \
+               case PSI_T_UINT32:      PSI_CALC_OP(u32);       break; \
+               case PSI_T_INT64:       PSI_CALC_OP(i64);       break; \
+               case PSI_T_UINT64:      PSI_CALC_OP(u64);       break; \
+               EMPTY_SWITCH_DEFAULT_CASE(); \
+               } \
+               return t1; \
+       } else if (t1 == PSI_T_DOUBLE) { \
+               switch (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; \
+               case PSI_T_INT16:       PSI_CALC_OP2(dval, dval, i16);  break; \
+               case PSI_T_UINT16:      PSI_CALC_OP2(dval, dval, u16);  break; \
+               case PSI_T_INT32:       PSI_CALC_OP2(dval, dval, i32);  break; \
+               case PSI_T_UINT32:      PSI_CALC_OP2(dval, dval, u32);  break; \
+               case PSI_T_INT64:       PSI_CALC_OP2(dval, dval, i64);  break; \
+               case PSI_T_UINT64:      PSI_CALC_OP2(dval, dval, u64);  break; \
+               EMPTY_SWITCH_DEFAULT_CASE(); \
+               } \
+               return t1; \
+       } else if (t2 == PSI_T_DOUBLE) { \
+               switch (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; \
+               case PSI_T_INT16:       PSI_CALC_OP2(dval, i16, dval);  break; \
+               case PSI_T_UINT16:      PSI_CALC_OP2(dval, u16, dval);  break; \
+               case PSI_T_INT32:       PSI_CALC_OP2(dval, i32, dval);  break; \
+               case PSI_T_UINT32:      PSI_CALC_OP2(dval, u32, dval);  break; \
+               case PSI_T_INT64:       PSI_CALC_OP2(dval, i64, dval);  break; \
+               case PSI_T_UINT64:      PSI_CALC_OP2(dval, u64, dval);  break; \
+               EMPTY_SWITCH_DEFAULT_CASE(); \
+               } \
+               return t2; \
+       } else if (t1 == PSI_T_FLOAT) { \
+               switch (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; \
+               case PSI_T_INT16:       PSI_CALC_OP2(fval, fval, i16);  break; \
+               case PSI_T_UINT16:      PSI_CALC_OP2(fval, fval, u16);  break; \
+               case PSI_T_INT32:       PSI_CALC_OP2(fval, fval, i32);  break; \
+               case PSI_T_UINT32:      PSI_CALC_OP2(fval, fval, u32);  break; \
+               case PSI_T_INT64:       PSI_CALC_OP2(fval, fval, i64);  break; \
+               case PSI_T_UINT64:      PSI_CALC_OP2(fval, fval, u64);  break; \
+               EMPTY_SWITCH_DEFAULT_CASE(); \
+               } \
+               return t1; \
+       } else if (t2 == PSI_T_FLOAT) { \
+               switch (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; \
+               case PSI_T_INT16:       PSI_CALC_OP2(fval, i16, fval);  break; \
+               case PSI_T_UINT16:      PSI_CALC_OP2(fval, u16, fval);  break; \
+               case PSI_T_INT32:       PSI_CALC_OP2(fval, i32, fval);  break; \
+               case PSI_T_UINT32:      PSI_CALC_OP2(fval, u32, fval);  break; \
+               case PSI_T_INT64:       PSI_CALC_OP2(fval, i64, fval);  break; \
+               case PSI_T_UINT64:      PSI_CALC_OP2(fval, u64, fval);  break; \
+               EMPTY_SWITCH_DEFAULT_CASE(); \
+               } \
+               return t2; \
+       } else { \
+               int64_t sval1 = v1->i64, sval2 = v2->i64; \
+               uint64_t uval1 = v1->u64, uval2 = v2->u64; \
+               switch (t1) { \
+               case PSI_T_INT8:        sval1 >>= 8; \
+               case PSI_T_INT16:       sval1 >>= 8; \
+               case PSI_T_INT32:       sval1 >>= 8; \
+               case PSI_T_INT64: \
+                       switch (t2) { \
+                       case PSI_T_INT8:        sval2 >>= 8; \
+                       case PSI_T_INT16:       sval2 >>= 8; \
+                       case PSI_T_INT32:       sval2 >>= 8; \
+                       case PSI_T_INT64: \
+                               res->i64 = PSI_CALC(sval1 , sval2); \
+                               return PSI_T_INT64; \
+                       case PSI_T_UINT8:       uval2 >>= 8; \
+                       case PSI_T_UINT16:      uval2 >>= 8; \
+                       case PSI_T_UINT32:      uval2 >>= 8; \
+                       case PSI_T_UINT64: \
+                               res->i64 = PSI_CALC(sval1, uval2); \
+                               return PSI_T_INT64; \
+                       } \
+                       break; \
+               case PSI_T_UINT8:       uval1 >>= 8; \
+               case PSI_T_UINT16:      uval1 >>= 8; \
+               case PSI_T_UINT32:      uval1 >>= 8; \
+               case PSI_T_UINT64: \
+                       switch (t2) { \
+                       case PSI_T_INT8:        sval2 >>= 8; \
+                       case PSI_T_INT16:       sval2 >>= 8; \
+                       case PSI_T_INT32:       sval2 >>= 8; \
+                       case PSI_T_INT64: \
+                               res->i64 = PSI_CALC(uval1, sval2); \
+                               return PSI_T_INT64; \
+                       case PSI_T_UINT8:       uval2 >>= 8; \
+                       case PSI_T_UINT16:      uval2 >>= 8; \
+                       case PSI_T_UINT32:      uval2 >>= 8; \
+                       case PSI_T_UINT64: \
+                               res->u64 = PSI_CALC(uval1, uval2); \
+                               return PSI_T_UINT64; \
+                       } \
+                       break; \
+               } \
+       } \
+       ZEND_ASSERT(0); \
+       return 0; \
 }
 
+#undef PSI_CALC
+#define PSI_CALC(var1, var2) (var1) + (var2)
+PSI_CALC_FN(add)
+#undef PSI_CALC
+#define PSI_CALC(var1, var2) (var1) * (var2)
+PSI_CALC_FN(mul)
+#undef PSI_CALC
+#define PSI_CALC(var1, var2) (var1) - (var2)
+PSI_CALC_FN(sub)
+#undef PSI_CALC
+#define PSI_CALC(var1, var2) (var1) / (var2)
+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];
@@ -895,12 +1024,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)
@@ -963,6 +1086,7 @@ PHP_MINIT_FUNCTION(psi)
 
        return SUCCESS;
 }
+
 PHP_MSHUTDOWN_FUNCTION(psi)
 {
        PSI_ContextDtor(&PSI_G(context));
@@ -972,26 +1096,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)
 {
@@ -1011,8 +1122,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