type parser fixes
[m6w6/ext-psi] / src / module.c
index 607015d29e1412ee70ec207a14c57048e10b4d9c..84d3e95d034de9f12e61e18c45fd23d1d2dd56fd 100644 (file)
@@ -23,6 +23,8 @@
 # define PSI_ENGINE "ffi"
 #endif
 
+#include <ndbm.h>
+
 ZEND_DECLARE_MODULE_GLOBALS(psi);
 
 PHP_INI_BEGIN()
@@ -38,7 +40,7 @@ void psi_error_wrapper(PSI_Token *t, int type, const char *msg, ...)
        va_list argv;
 
        va_start(argv, msg);
-       psi_verror(type, t?t->file:"Unknown", t?*t->line:0, msg, argv);
+       psi_verror(type, t?t->file:"Unknown", t?t->line:0, msg, argv);
        va_end(argv);
 }
 void psi_error(int type, const char *fn, unsigned ln, const char *msg, ...)
@@ -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;
@@ -293,6 +310,7 @@ void psi_to_double(zval *return_value, set_value *set, impl_val *ret_val)
 
 void psi_to_string(zval *return_value, set_value *set, impl_val *ret_val)
 {
+       char *str;
        decl_var *var = set->vars->vars[0];
        token_t t = real_decl_type(var->arg->type)->type;
 
@@ -303,27 +321,33 @@ 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:
-       case PSI_T_VOID:
-       case PSI_T_INT8:
-       case PSI_T_UINT8:
                if (!var->arg->var->pointer_level) {
                        RETVAL_STRINGL(&ret_val->cval, 1);
                } else {
                        ret_val = deref_impl_val(ret_val, var);
-                       if (ret_val && ret_val->ptr) {
+                       if (var->arg->var->array_size) {
+                               str = (char *) ret_val;
+                       } else {
+                               str = ret_val->ptr;
+                       }
+                       if (str) {
                                if (set->num) {
-                                       RETVAL_STRINGL(ret_val->ptr, psi_long_num_exp(set->num, set->outer.val));
+                                       zend_long n = psi_long_num_exp(set->num, set->outer.val);
+                                       RETVAL_STRINGL(str, n);
                                } else {
-                                       RETVAL_STRING(ret_val->ptr);
+                                       RETVAL_STRING(str);
                                }
                        } else {
                                RETVAL_EMPTY_STRING();
                        }
                }
                return;
-               psi_to_int(return_value, set, ret_val);
-               break;
        }
        convert_to_string(return_value);
 }
@@ -332,7 +356,7 @@ void psi_to_string(zval *return_value, set_value *set, impl_val *ret_val)
 static impl_val *iterate(impl_val *val, size_t size, unsigned i, impl_val *tmp)
 {
        memset(tmp, 0, sizeof(*tmp));
-       memcpy(tmp, ((void*) val->ptr) + size * i, size);
+       memcpy(tmp, ((void*) val) + size * i, size);
        return tmp;
 }
 
@@ -347,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) {
@@ -388,7 +413,7 @@ void *psi_array_to_struct(decl_struct *s, HashTable *arr)
 
 void psi_to_recursive(zval *return_value, set_value *set, impl_val *r_val)
 {
-       set->outer.set->func->handler(return_value, set->outer.set, r_val->ptr);
+       set->outer.set->func->handler(return_value, set, r_val);
 }
 
 void psi_to_array(zval *return_value, set_value *set, impl_val *r_val)
@@ -398,7 +423,7 @@ 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) {
+       if ((intptr_t) ret_val <= (intptr_t) 0) {
                RETURN_NULL();
        }
 
@@ -413,13 +438,13 @@ void psi_to_array(zval *return_value, set_value *set, impl_val *r_val)
                                set_value *sub_set = set->inner[i];
                                decl_var *sub_var = sub_set->vars->vars[0];
 
-                               sub_set->outer.val = r_val;
+                               sub_set->outer.val = ret_val;
 
                                if (sub_var->arg) {
                                        impl_val *tmp = NULL, *val;
                                        zval ztmp;
 
-                                       val = deref_impl_val(struct_member_ref(sub_var->arg, ret_val, &tmp), sub_var);
+                                       val = struct_member_ref(sub_var->arg, ret_val, &tmp);
                                        sub_set->func->handler(&ztmp, sub_set, val);
                                        add_assoc_zval(return_value, sub_var->name, &ztmp);
 
@@ -455,20 +480,32 @@ void psi_to_array(zval *return_value, set_value *set, impl_val *r_val)
                }
                return;
        } else if (set->num) {
-               /* to_array(arr_var, num_expr, to_int(*arr_var))
-                */
+               /* to_array(arr_var, num_expr, to_int(*arr_var)) */
                zval ele;
+               char *ptr;
                zend_long i, n = psi_long_num_exp(set->num, set->outer.val);
+               size_t size = psi_t_size(var->arg->var->pointer_level ? PSI_T_POINTER : t);
+               set_value *sub_set = set->inner[0];
 
+               sub_set->outer.val = set->outer.val;
                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);
-
-                       set->inner[0]->func->handler(&ele, set->inner[0], ptr);
+                       ptr = (char *) ret_val->ptr + i * size;
+                       sub_set->func->handler(&ele, sub_set, (void *) ptr);
                        add_next_index_zval(return_value, &ele);
                }
        } else {
-               ZEND_ASSERT(0);
+               /* to_array(arr_var, to_int(*arr_var)) */
+               zval ele;
+               char *ptr = ret_val->ptr;
+               size_t size = psi_t_size(var->arg->var->pointer_level ? PSI_T_POINTER : t);
+               set_value *sub_set = set->inner[0];
+
+               sub_set->outer.val = set->outer.val;
+               while (*(void **) ptr) {
+                       sub_set->func->handler(&ele, sub_set, (void *) ptr);
+                       add_next_index_zval(return_value, &ele);
+                       ptr += size;
+               }
        }
 }
 
@@ -588,10 +625,13 @@ static inline void *psi_do_calloc(let_calloc *alloc)
        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 *));
+#if 0
+       fprintf(stderr, "calloc: %p\n", mem);
+#endif
        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:
@@ -619,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 = "";
@@ -633,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;
@@ -652,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:
@@ -661,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);
@@ -670,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)
@@ -681,7 +721,12 @@ static inline void *psi_do_let(let_stmt *let)
 
        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);
+               memcpy(arg_val, deref_impl_val(let->val->data.var->arg->let->ptr, let->val->data.var), sizeof(*arg_val));
+#if 0
+               fprintf(stderr, "LET TMP: %p -> %p\n",
+                               let->val->data.var->arg->let->ptr,
+                               arg_val->ptr);
+#endif
                break;
        case PSI_LET_NULL:
                if (darg->var->array_size) {
@@ -701,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;
                }
        }
@@ -715,8 +760,10 @@ static inline void *psi_do_let(let_stmt *let)
 
 static inline void psi_do_set(zval *return_value, set_value *set)
 {
+       decl_arg *set_arg = set->vars->vars[0]->arg;
+
        zval_dtor(return_value);
-       set->func->handler(return_value, set, set->vars->vars[0]->arg->ptr);
+       set->func->handler(return_value, set, set_arg->let ? set_arg->let->ptr : set_arg->ptr);
 }
 
 static inline void psi_do_return(zval *return_value, return_stmt *ret)
@@ -734,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 */
@@ -743,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];
 
@@ -763,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) {
@@ -830,6 +891,8 @@ static inline int psi_calc_num_exp_value(num_exp *exp, impl_val *strct, impl_val
        case PSI_T_NAME:
                if (strct) {
                        ref = struct_member_ref(exp->u.dvar->arg, strct, &tmp);
+               } else if (exp->u.dvar->arg->let) {
+                       ref = exp->u.dvar->arg->let->ptr;
                } else {
                        ref = exp->u.dvar->arg->ptr;
                }
@@ -882,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"; \
@@ -893,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; \
@@ -912,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; \
@@ -926,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; \
@@ -938,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; \
@@ -954,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; \
@@ -1033,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) {
@@ -1074,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) {