flush
[m6w6/ext-psi] / src / context.c
index d7eabb56e19e535be2265d106c118defe4bd599f..1e8204ac9e7d4d500229d060a4580dbb24531858 100644 (file)
-#include <sys/param.h>
-#include <dlfcn.h>
-#include <dirent.h>
-#include <fnmatch.h>
-#include <errno.h>
-
 #ifdef HAVE_CONFIG_H
 # include "config.h"
 #endif
 
+#include "php.h"
+
+#include <stdio.h>
+#ifdef HAVE_SYS_TYPES_H
+# include <sys/types.h>
+#endif
+#ifdef HAVE_SYS_STAT_H
+# include <sys/stat.h>
+#endif
+#ifdef STDC_HEADERS
+# include <stdlib.h>
+# include <stddef.h>
+#else
+# ifdef HAVE_STDLIB_H
+#  include <stdlib.h>
+# endif
+#endif
+#ifdef HAVE_STRING_H
+# if !defined STDC_HEADERS && defined HAVE_MEMORY_H
+#  include <memory.h>
+# endif
+# include <string.h>
+#endif
+#ifdef HAVE_STRINGS_H
+# include <strings.h>
+#endif
+#ifdef HAVE_INTTYPES_H
+# include <inttypes.h>
+#endif
+#ifdef HAVE_STDINT_H
+# include <stdint.h>
+#endif
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+
+#ifdef HAVE_ERRNO_H
+# include <errno.h>
+#endif
+#ifdef HAVE_GLOB_H
+# include <glob.h>
+#endif
+#ifdef HAVE_NETINET_IN_H
+# include <netinet/in.h>
+#endif
+#ifdef HAVE_ARPA_NAMESER_H
+# include <arpa/nameser.h>
+#endif
+#ifdef HAVE_NETDB_H
+# include <netdb.h>
+#endif
+#ifdef HAVE_RESOLV_H
+# include <resolv.h>
+#endif
+#ifdef HAVE_SYS_SELECT_H
+# include <sys/select.h>
+#endif
+#ifdef HAVE_SYS_SOCKET_H
+# include <sys/socket.h>
+#endif
+#ifdef HAVE_SYS_TIME_H
+# include <sys/time.h>
+#endif
+#ifdef HAVE_SYS_TIMES_H
+# include <sys/times.h>
+#endif
+#ifdef HAVE_SYS_UIO_H
+# include <sys/uio.h>
+#endif
+#ifdef HAVE_SYS_UTSNAME_H
+# include <sys/utsname.h>
+#endif
+#ifdef HAVE_TIME_H
+# include <time.h>
+#endif
+#ifdef HAVE_WCHAR_H
+# include <wchar.h>
+#endif
+
+#ifdef HAVE_DIRENT_H
+# include <dirent.h>
+# define NAMLEN(dirent) strlen ((dirent)->d_name)
+#else
+# define dirent direct
+# define NAMLEN(dirent) ((dirent)->d_namlen)
+# ifdef HAVE_SYS_NDIR_H
+#  include <sys/ndir.h>
+# endif
+# ifdef HAVE_SYS_DIR_H
+#  include <sys/dir.h>
+# endif
+# ifdef HAVE_NDIR_H
+#  include <ndir.h>
+# endif
+#endif
+
+#include <sys/param.h>
+#include <dlfcn.h>
+#include <fnmatch.h>
+
 #include "php.h"
 #include "php_scandir.h"
 #include "php_psi.h"
 #include "libjit.h"
 #include "libffi.h"
 
-#define psi_predef_count(of) ((sizeof(psi_predef ##of## s)/sizeof(psi_predef ##of))-1)
-typedef struct psi_predef_type {
+static struct psi_std_type {
+       token_t type_tag;
+       const char *type_name;
+} psi_std_types[] = {
+       {PSI_T_FLOAT, "float"},
+       {PSI_T_DOUBLE, "double"},
+       {PSI_T_INT8, "int8_t"},
+       {PSI_T_INT16, "int16_t"},
+       {PSI_T_INT32, "int32_t"},
+       {PSI_T_INT64, "int64_t"},
+       {PSI_T_UINT8, "uint8_t"},
+       {PSI_T_UINT16, "uint16_t"},
+       {PSI_T_UINT32, "uint32_t"},
+       {PSI_T_UINT64, "uint64_t"},
+       {0}
+};
+
+static struct psi_predef_type {
        token_t type_tag;
        const char *type_name;
        const char *alias;
-} psi_predef_type;
-static const psi_predef_type psi_predef_types[] = {
-       PHP_PSI_TYPES{0}
+} psi_predef_types[] = {
+       PSI_TYPES
+       {0}
 };
-#define psi_predef_type_count() psi_predef_count(_type)
 
-typedef struct psi_predef_const {
+static struct psi_predef_const {
        token_t type_tag;
        const char *type_name;
-       const char *name;
+       const char *var_name;
        const char *val_text;
        token_t val_type_tag;
-} psi_predef_const;
-static const psi_predef_const psi_predef_consts[] = {
-       PHP_PSI_CONSTS{0}
+} psi_predef_consts[] = {
+       PSI_CONSTS
+       {0}
+};
+
+PSI_MACROS
+
+static struct psi_func_redir {
+       const char *name;
+       void (*func)(void);
+} psi_func_redirs[] = {
+       PSI_REDIRS
+       {0}
 };
-#define psi_predef_const_count() psi_predef_count(_const)
 
-typedef struct psi_predef_struct_member {
+static struct psi_predef_decl {
        token_t type_tag;
        const char *type_name;
-       const char *name;
-       size_t off;
-       size_t len;
+       const char *var_name;
        size_t pointer_level;
        size_t array_size;
-} psi_predef_struct_member;
-#define PSI_PREDEF_STRUCT_MEMBERS 32
-typedef struct psi_predef_struct {
-       const char *name;
-       size_t size;
-       psi_predef_struct_member members[PSI_PREDEF_STRUCT_MEMBERS];
-} psi_predef_struct;
-static const psi_predef_struct psi_predef_structs[] = {
-       PHP_PSI_STRUCTS{0}
+} psi_predef_decls[] = {
+       PSI_DECLS
+       {0}
 };
-#define psi_predef_struct_count() psi_predef_count(_struct)
-
-PHP_PSI_MACROS
 
-typedef struct psi_predef_func {
-       const char *name;
-       void (*func)(void);
-} psi_predef_func;
-static psi_predef_func psi_predef_funcs[] = {
-       PHP_PSI_FUNCS{0}
+static struct psi_predef_struct {
+       token_t type_tag;
+       const char *type_name;
+       const char *var_name;
+       size_t offset;
+       size_t size;
+       size_t pointer_level;
+       size_t array_size;
+} psi_predef_structs[] = {
+       PSI_STRUCTS
+       {0}
 };
-#define psi_predef_func_count() psi_predef_count(_func)
 
 static int validate_lib(PSI_Data *data, void **dlopened) {
        char lib[MAXPATHLEN];
@@ -97,16 +208,26 @@ static int validate_lib(PSI_Data *data, void **dlopened) {
 
 static inline int locate_decl_type_alias(decl_typedefs *defs, decl_type *type) {
        size_t i;
+       struct psi_std_type *stdtyp;
 
        if (type->real) {
                return 1;
        }
        for (i = 0; i < defs->count; ++i) {
-               if (!strcmp(defs->list[i]->alias, type->name)) {
-                       type->real = defs->list[i]->type;
+               decl_typedef *def = defs->list[i];
+
+               if (def->type->type != type->type && !strcmp(def->alias, type->name)) {
+                       type->real = def->type;
+                       return 1;
+               }
+       }
+       for (stdtyp = &psi_std_types[0]; stdtyp->type_tag; ++stdtyp) {
+               if (!strcmp(type->name, stdtyp->type_name)) {
+                       type->type = stdtyp->type_tag;
                        return 1;
                }
        }
+
        return 0;
 }
 static inline int locate_decl_type_struct(decl_structs *structs, decl_type *type) {
@@ -130,7 +251,10 @@ static inline int validate_decl_type(PSI_Data *data, decl_type *type) {
                if (!data->defs || !locate_decl_type_alias(data->defs, type)) {
                        return 0;
                }
-               return validate_decl_type(data, type->real);
+               if (type->real) {
+                       return validate_decl_type(data, type->real);
+               }
+               return 1;
        case PSI_T_STRUCT:
                if (!data->structs || !locate_decl_type_struct(data->structs, type)) {
                        return 0;
@@ -141,8 +265,8 @@ static inline int validate_decl_type(PSI_Data *data, decl_type *type) {
 }
 static inline int validate_decl_typedef(PSI_Data *data, decl_typedef *def) {
        if (!validate_decl_type(data, def->type)) {
-               data->error(PSI_WARNING, "Type '%s' cannot be aliased to '%s'",
-                       def->type->name, def->alias);
+               data->error(PSI_WARNING, "Type '%s' cannot be aliased to %s'%s'",
+                       def->type->name, def->type->type == PSI_T_STRUCT?"struct ":"",def->alias);
                return 0;
        }
        /* FIXME: check def->alias */
@@ -156,8 +280,8 @@ static inline int validate_constant(PSI_Data *data, constant *c) {
 
 static inline int validate_decl_arg(PSI_Data *data, decl_arg *arg) {
        if (!validate_decl_type(data, arg->type)) {
-               data->error(PSI_WARNING, "Cannot use '%s' as type for '%s'",
-                       arg->type->name, arg->var->name);
+               data->error(PSI_WARNING, "Cannot use '%s'(%d) as type for '%s'",
+                       arg->type->name, arg->type->type, arg->var->name);
                return 0;
        }
        return 1;
@@ -227,6 +351,8 @@ static inline int validate_decl_abi(PSI_Data *data, decl_abi *abi) {
 }
 static inline int validate_decl_func(PSI_Data *data, void *dl, decl *decl, decl_arg *func)
 {
+       struct psi_func_redir *redir;
+
        if (!strcmp(func->var->name, "dlsym")) {
                data->error(PSI_WARNING, "Cannot dlsym dlsym (sic!)");
                return 0;
@@ -235,21 +361,16 @@ static inline int validate_decl_func(PSI_Data *data, void *dl, decl *decl, decl_
        if (!validate_decl_arg(data, func)) {
                return 0;
        }
+       for (redir = &psi_func_redirs[0]; redir->name; ++redir) {
+               if (!strcmp(func->var->name, redir->name)) {
+                       decl->call.sym = redir->func;
+               }
+       }
+       if (!decl->call.sym) {
 #ifndef RTLD_NEXT
 # define RTLD_NEXT ((void *) -1l)
 #endif
-       decl->call.sym = dlsym(dl ?: RTLD_NEXT, func->var->name);
-       if (!decl->call.sym) {
-               size_t i;
-
-               for (i = 0; i < psi_predef_func_count(); ++i) {
-                       psi_predef_func *pre = &psi_predef_funcs[i];
-
-                       if (!strcmp(func->var->name, pre->name)) {
-                               decl->call.sym = pre->func;
-                               break;
-                       }
-               }
+               decl->call.sym = dlsym(dl ?: RTLD_NEXT, func->var->name);
                if (!decl->call.sym) {
                        data->error(PSI_WARNING, "Failed to locate symbol '%s': %s",
                                func->var->name, dlerror());
@@ -276,23 +397,28 @@ static inline int validate_decl(PSI_Data *data, void *dl, decl *decl) {
        }
        return 1;
 }
+static inline decl_arg *locate_decl_var_arg(decl_var *var, decl_args *args) {
+       size_t i;
 
-static inline decl_arg *locate_struct_member(decl_struct *s, decl_var *var) {
-       if (s->args) {
-               size_t i;
-
-               for (i = 0; i < s->args->count; ++i) {
-                       decl_arg *darg = s->args->args[i];
+       for (i = 0; i < args->count; ++i) {
+               decl_arg *arg = args->args[i];
 
-                       if (!strcmp(var->name, darg->var->name)) {
-                               return var->arg = darg;
-                       }
+               if (!strcmp(var->name, arg->var->name)) {
+                       ZEND_ASSERT(!var->arg || var->arg == arg);
+                       return var->arg = arg;
                }
        }
 
        return NULL;
 }
-static inline int validate_set_value(PSI_Data *data, set_value *set, decl_arg *ref) {
+static inline decl_arg *locate_struct_member(decl_struct *s, decl_var *var) {
+       if (s->args) {
+               return locate_decl_var_arg(var, s->args);
+       }
+
+       return NULL;
+}
+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];
@@ -313,37 +439,58 @@ static inline int validate_set_value(PSI_Data *data, set_value *set, decl_arg *r
        case PSI_T_TO_ARRAY:
                set->func->handler = psi_to_array;
                break;
+       case PSI_T_TO_OBJECT:
+               set->func->handler = psi_to_object;
+               break;
+       case PSI_T_VOID:
+               set->func->handler = psi_to_void;
+               break;
        EMPTY_SWITCH_DEFAULT_CASE();
        }
 
-       if (strcmp(set_var->name, ref->var->name)) {
-               return 0;
+       for (i = 1; i < set->vars->count; ++i) {
+               if (!locate_decl_var_arg(set->vars->vars[i], ref_list)) {
+                       return 0;
+               }
        }
-       ZEND_ASSERT(!set_var->arg || set_var->arg == ref);
        set_var->arg = ref;
 
-       if (set->count && (set->func->type != PSI_T_TO_ARRAY || (ref_type->type != PSI_T_STRUCT && !ref->var->arg->var->pointer_level))) {
-               data->error(E_WARNING, "Inner `set` statement casts only work with to_array() casts on structs or pointers");
-               return 0;
+       if (set->count) {
+               int is_to_array = (set->func->type == PSI_T_TO_ARRAY);
+               int is_pointer_to_struct = (ref_type->type == PSI_T_STRUCT && ref->var->pointer_level);
+
+               if (!is_to_array && !is_pointer_to_struct) {
+                       data->error(E_WARNING, "Inner `set` statement casts only work with "
+                                       "to_array() casts on structs or pointers: %s(%s...", set->func->name, set->vars->vars[0]->name);
+                       return 0;
+               }
        }
 
        if (ref_type->type == PSI_T_STRUCT) {
+               /* to_array(struct, to_...) */
                for (i = 0; i < set->count; ++i) {
                        decl_var *sub_var = set->inner[i]->vars->vars[0];
                        decl_arg *sub_ref = locate_struct_member(ref_type->strct, sub_var);
 
+                       set->inner[i]->outer.set = set;
                        if (sub_ref) {
-                               if (!validate_set_value(data, set->inner[i], sub_ref)) {
+                               if (!validate_set_value(data, set->inner[i], sub_ref, ref_type->strct->args)) {
                                        return 0;
                                }
                        }
                }
        } else if (set->count == 1) {
+               /* to_array(ptr, to_string(*ptr)) */
                decl_var *sub_var = set->inner[0]->vars->vars[0];
-               decl_arg *sub_ref = sub_var->arg;
+               decl_arg *sub_ref = locate_decl_var_arg(sub_var, ref_list);
 
+               set->inner[0]->outer.set = set;
                if (sub_ref) {
-                       if (!validate_set_value(data, set->inner[0], sub_ref)) {
+                       if (strcmp(sub_var->name, set_var->name)) {
+                               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)) {
                                return 0;
                        }
                }
@@ -393,7 +540,7 @@ static inline int validate_impl_ret_stmt(PSI_Data *data, impl *impl) {
                return 0;
        }
 
-       if (!validate_set_value(data, ret->set, ret->decl)) {
+       if (!validate_set_value(data, ret->set, ret->decl, impl->decl->args)) {
                return 0;
        }
 
@@ -401,6 +548,42 @@ static inline int validate_impl_ret_stmt(PSI_Data *data, impl *impl) {
 
        return 1;
 }
+static inline constant *locate_num_exp_constant(num_exp *exp, constants *consts) {
+       size_t i;
+
+       for (i = 0; i < consts->count; ++i) {
+               constant *cnst = consts->list[i];
+
+               if (!strcmp(cnst->name, exp->u.numb)) {
+                       free(exp->u.numb);
+                       return exp->u.cnst = cnst;
+               }
+       }
+
+       return NULL;
+}
+static inline int validate_num_exp(PSI_Data *data, impl *impl, num_exp *exp) {
+       switch (exp->t) {
+       case PSI_T_NAME:
+               if (!locate_decl_var_arg(exp->u.dvar, impl->decl->args)) {
+                       data->error(PSI_WARNING, "Unknown variable '%s' in numeric expression"
+                                       " of implementation '%s'", exp->u.dvar->name, impl->func->name);
+                       return 0;
+               }
+               return 1;
+       case PSI_T_NSNAME:
+               if (!locate_num_exp_constant(exp, data->consts)) {
+                       data->error(PSI_WARNING, "Unknown constant '%s' in numeric expression"
+                                       " of implementation '%s'", exp->u.numb, impl->func->name);
+                       return 0;
+               }
+               return 1;
+       case PSI_T_NUMBER:
+               return 1;
+       default:
+               return 0;
+       }
+}
 static inline int validate_impl_let_stmts(PSI_Data *data, impl *impl) {
        size_t i, j;
        /* we can have multiple let stmts */
@@ -432,9 +615,10 @@ static inline int validate_impl_let_stmts(PSI_Data *data, impl *impl) {
                int check = 0;
 
                if (let->val && let->val->func && let->val->func->alloc) {
-                       if (!validate_decl_type(data, let->val->func->alloc->type)) {
-                               data->error(PSI_WARNING, "Cannot use '%s' as type for calloc in `let` statement",
-                                       let->val->func->alloc->type->name);
+                       if (!validate_num_exp(data, impl, let->val->func->alloc->nmemb)) {
+                               return 0;
+                       }
+                       if (!validate_num_exp(data, impl, let->val->func->alloc->size)) {
                                return 0;
                        }
                }
@@ -491,10 +675,10 @@ static inline int validate_impl_set_stmts(PSI_Data *data, impl *impl) {
 
                                if (!strcmp(set_var->name, set_arg->var->name)) {
                                        check = 1;
-                                       if (!validate_set_value(data, set->val, set_arg)) {
+                                       set_var->arg = set_arg;
+                                       if (!validate_set_value(data, set->val, set_arg, impl->decl->args)) {
                                                return 0;
                                        }
-                                       set_var->arg = set_arg;
                                        break;
                                }
                        }
@@ -596,8 +780,12 @@ static inline int validate_impl_stmts(PSI_Data *data, impl *impl) {
 
 PSI_Context *PSI_ContextInit(PSI_Context *C, PSI_ContextOps *ops, PSI_ContextErrorFunc error)
 {
-       size_t i, j;
+       size_t i;
        PSI_Data T;
+       struct psi_predef_type *predef_type;
+       struct psi_predef_const *predef_const;
+       struct psi_predef_struct *predef_struct;
+       struct psi_predef_decl *predef_decl;
 
        if (!C) {
                C = malloc(sizeof(*C));
@@ -618,49 +806,61 @@ PSI_Context *PSI_ContextInit(PSI_Context *C, PSI_ContextOps *ops, PSI_ContextErr
        memset(&T, 0, sizeof(T));
        T.error = error;
 
-       for (i = 0; i < psi_predef_type_count(); ++i) {
-               const psi_predef_type *pre = &psi_predef_types[i];
-               decl_type *type = init_decl_type(pre->type_tag, pre->type_name);
-               decl_typedef *def = init_decl_typedef(pre->alias, type);
+       for (predef_type = &psi_predef_types[0]; predef_type->type_tag; ++predef_type) {
+               decl_type *type = init_decl_type(predef_type->type_tag, predef_type->type_name);
+               decl_typedef *def = init_decl_typedef(predef_type->alias, type);
 
                T.defs = add_decl_typedef(T.defs, def);
        }
-       for (i = 0; i < psi_predef_const_count(); ++i) {
-               const psi_predef_const *pre = &psi_predef_consts[i];
-               impl_def_val *val = init_impl_def_val(pre->val_type_tag, pre->val_text);
-               const_type *type = init_const_type(pre->type_tag, pre->type_name);
-               constant *constant = init_constant(type, pre->name, val);
+       for (predef_const = &psi_predef_consts[0]; predef_const->type_tag; ++predef_const) {
+               impl_def_val *val = init_impl_def_val(predef_const->val_type_tag, predef_const->val_text);
+               const_type *type = init_const_type(predef_const->type_tag, predef_const->type_name);
+               constant *constant = init_constant(type, predef_const->var_name, val);
 
                T.consts = add_constant(T.consts, constant);
        }
-       for (i = 0; i < psi_predef_struct_count(); ++i) {
-               const psi_predef_struct *pre = &psi_predef_structs[i];
+       for (predef_struct = &psi_predef_structs[0]; predef_struct->type_tag; ++predef_struct) {
+               struct psi_predef_struct *member;
                decl_args *dargs = init_decl_args(NULL);
-               decl_struct *dstruct;
+               decl_struct *dstruct = init_decl_struct(predef_struct->var_name, dargs);
 
-               for (j = 0; j < PSI_PREDEF_STRUCT_MEMBERS; ++j) {
-                       const psi_predef_struct_member *member = &pre->members[j];
+               dstruct->size = predef_struct->size;
+               for (member = &predef_struct[1]; member->type_tag; ++member) {
                        decl_type *type;
                        decl_var *dvar;
                        decl_arg *darg;
 
-                       if (!member->name) {
-                               break;
-                       }
-
                        type = init_decl_type(member->type_tag, member->type_name);
-                       dvar = init_decl_var(member->name, member->pointer_level, member->array_size);
+                       dvar = init_decl_var(member->var_name, member->pointer_level, member->array_size);
                        darg = init_decl_arg(type, dvar);
-                       darg->layout = init_decl_struct_layout(member->off, member->len);
+                       darg->layout = init_decl_struct_layout(member->offset, member->size);
                        dargs = add_decl_arg(dargs, darg);
                }
 
-               dstruct = init_decl_struct(pre->name, dargs);
-               dstruct->size = pre->size;
                T.structs = add_decl_struct(T.structs, dstruct);
+               predef_struct = member;
+       }
+       for (predef_decl = &psi_predef_decls[0]; predef_decl->type_tag; ++predef_decl) {
+               struct psi_predef_decl *farg;
+               decl_type *ftype = init_decl_type(predef_decl->type_tag, predef_decl->type_name);
+               decl_var *fname = init_decl_var(predef_decl->var_name, predef_decl->pointer_level, predef_decl->array_size);
+               decl_arg *func = init_decl_arg(ftype, fname);
+               decl_args *args = init_decl_args(NULL);
+               decl *decl = init_decl(init_decl_abi("default"), func, args);
+
+
+               for (farg = &predef_decl[1]; farg->type_tag; ++farg) {
+                       decl_type *arg_type = init_decl_type(farg->type_tag, farg->type_name);
+                       decl_var *arg_var = init_decl_var(farg->var_name, farg->pointer_level, farg->array_size);
+                       decl_arg *darg = init_decl_arg(arg_type, arg_var);
+                       args = add_decl_arg(args, darg);
+               }
+
+               T.decls = add_decl(T.decls, decl);
+               predef_decl = farg;
        }
 
-       for (i = 0; i < psi_predef_type_count(); ++i) {
+       for (i = 0; i < T.defs->count; ++i) {
                decl_typedef *def = T.defs->list[i];
 
                if (validate_decl_typedef(&T, def)) {
@@ -668,7 +868,7 @@ PSI_Context *PSI_ContextInit(PSI_Context *C, PSI_ContextOps *ops, PSI_ContextErr
                }
        }
 
-       for (i = 0; i < psi_predef_const_count(); ++i) {
+       for (i = 0; i < T.consts->count; ++i) {
                constant *constant = T.consts->list[i];
 
                if (validate_constant(&T, constant)) {
@@ -676,7 +876,7 @@ PSI_Context *PSI_ContextInit(PSI_Context *C, PSI_ContextOps *ops, PSI_ContextErr
                }
        }
 
-       for (i = 0; i < psi_predef_struct_count(); ++i) {
+       for (i = 0; i < T.structs->count; ++i) {
                decl_struct *dstruct = T.structs->list[i];
 
                if (validate_decl_struct(&T, dstruct)) {
@@ -684,6 +884,14 @@ PSI_Context *PSI_ContextInit(PSI_Context *C, PSI_ContextOps *ops, PSI_ContextErr
                }
        }
 
+       for (i = 0; i < T.decls->count; ++i) {
+               decl *decl = T.decls->list[i];
+
+               if (validate_decl(&T, NULL, decl)) {
+                       C->decls = add_decl(C->decls, decl);
+               }
+       }
+
        C->count = 1;
        C->data = malloc(sizeof(*C->data));
        PSI_DataExchange(C->data, &T);
@@ -766,7 +974,7 @@ static int psi_select_dirent(const struct dirent *entry)
 
 void PSI_ContextBuild(PSI_Context *C, const char *paths)
 {
-       int i, n, flags = getenv("PSI_DEBUG") ? PSI_PARSER_DEBUG : 0;
+       int i, n, flags = psi_check_env("PSI_DEBUG") ? PSI_PARSER_DEBUG : 0;
        char *sep = NULL, *cpy = strdup(paths), *ptr = cpy;
        struct dirent **entries = NULL;
 
@@ -795,9 +1003,13 @@ void PSI_ContextBuild(PSI_Context *C, const char *paths)
                                        continue;
                                }
 
-                               while (-1 != PSI_ParserScan(&P)) {
+                               while (0 < PSI_ParserScan(&P)) {
                                        PSI_ParserParse(&P, PSI_TokenAlloc(&P));
-                               };
+                                       if (P.num == PSI_T_EOF) {
+                                               break;
+                                       }
+                               }
+
                                PSI_ParserParse(&P, NULL);
                                PSI_ContextValidate(C, &P);
                                PSI_ParserDtor(&P);
@@ -860,6 +1072,7 @@ 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);
 }
 
@@ -886,43 +1099,204 @@ static inline void dump_decl_arg(int fd, decl_arg *a) {
        dprintf(fd, " ");
        dump_decl_var(fd, a->var);
 }
+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_impl_set_value(int fd, set_value *set, unsigned level) {
+       size_t i;
+
+       if (level > 1) {
+               /* only if not directly after `set ...` */
+               dump_level(fd, level);
+       }
+       dprintf(fd, "%s(", set->func->name);
+
+       for (i = 0; i < set->vars->count; ++i) {
+               decl_var *svar = set->vars->vars[i];
+               if (i) {
+                       dprintf(fd, ", ");
+               }
+               dump_decl_var(fd, svar);
+       }
+       if (set->inner) {
+               dprintf(fd, ",\n");
+               for (i = 0; i < set->count; ++i) {
+                       dump_impl_set_value(fd, set->inner[i], level+1);
+               }
+               /* only if inner stmts, i.e. with new lines, were dumped */
+               dump_level(fd, level);
+       }
+       if (level > 1) {
+               dprintf(fd, "),\n");
+       } else {
+               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;
+       size_t i, j, k, l;
 
 #ifdef HAVE_LIBJIT
        if (C->ops == PSI_Libjit()) {
-               dprintf(fd, "#PSI(libjit)\n");
+               dprintf(fd, "// psi.engine=jit\n");
        }
 #endif
 #ifdef HAVE_LIBFFI
        if (C->ops == PSI_Libffi()) {
-               dprintf(fd, "#PSI(libffi)\n");
+               dprintf(fd, "// psi.engine=ffi\n");
        }
 #endif
+       dprintf(fd, "\n");
 
-       if (C->defs) for (i = 0; i < C->defs->count; ++i) {
-               decl_typedef *tdef = C->defs->list[i];
+       if (C->defs) {
+               for (i = 0; i < C->defs->count; ++i) {
+                       decl_typedef *tdef = C->defs->list[i];
 
-               dprintf(fd, "typedef ");
-               dump_decl_type(fd, tdef->type);
-               dprintf(fd, " %s;\n", tdef->alias);
+                       dprintf(fd, "typedef ");
+                       dump_decl_type(fd, tdef->type);
+                       dprintf(fd, " %s;\n", tdef->alias);
+               }
+               dprintf(fd, "\n");
        }
-       if (C->structs) for (i = 0; i < C->structs->count; ++i) {
-               decl_struct *strct = C->structs->list[i];
-               decl_arg *sarg = NULL;
 
-               dprintf(fd, "struct %s::(%zu) {\n", strct->name, strct->size);
-               for (j = 0; j < strct->args->count; ++j) {
-                       sarg = strct->args->args[j];
-                       dprintf(fd, "\t");
-                       dump_decl_arg(fd, sarg);
-                       dprintf(fd, "::(%zu, %zu);\n", sarg->layout->pos, sarg->layout->len);
+       if (C->structs) {
+               for (i = 0; i < C->structs->count; ++i) {
+                       decl_struct *strct = C->structs->list[i];
+
+                       dprintf(fd, "struct %s::(%zu) {\n", strct->name, strct->size);
+                       if (strct->args) for (j = 0; j < strct->args->count; ++j) {
+                               decl_arg *sarg = strct->args->args[j];
+
+                               dprintf(fd, "\t");
+                               dump_decl_arg(fd, sarg);
+                               dprintf(fd, "::(%zu, %zu);\n", sarg->layout->pos, sarg->layout->len);
+                       }
+                       dprintf(fd, "}\n");
                }
-               dprintf(fd, "}\n");
+               dprintf(fd, "\n");
        }
+       if (C->consts) {
+               for (i = 0; i < C->consts->count; ++i) {
+                       constant *cnst = C->consts->list[i];
 
-       dprintf(fd, "\n");
+                       dprintf(fd, "const %s %s = ", cnst->type->name, cnst->name);
+                       if (cnst->val->type == PSI_T_QUOTED_STRING) {
+                               dprintf(fd, "\"%s\";\n", cnst->val->text);
+                       } else {
+                               dprintf(fd, "%s;\n", cnst->val->text);
+                       }
+               }
+               dprintf(fd, "\n");
+       }
+       if (C->decls) {
+               for (i = 0; i < C->decls->count; ++i) {
+                       decl *decl = C->decls->list[i];
+
+                       dprintf(fd, "%s ", decl->abi->convention);
+                       dump_decl_arg(fd, decl->func);
+                       dprintf(fd, "(");
+                       if (decl->args) for (j = 0; j < decl->args->count; ++j) {
+                               if (j) {
+                                       dprintf(fd, ", ");
+                               }
+                               dump_decl_arg(fd, decl->args->args[j]);
+                       }
+                       dprintf(fd, ");\n");
+               }
+               dprintf(fd, "\n");
+       }
+       if (C->impls) {
+               for (i = 0; i < C->impls->count; ++i) {
+                       impl *impl = C->impls->list[i];
+
+                       dprintf(fd, "function %s(", impl->func->name);
+                       if (impl->func->args) for (j = 0; j < impl->func->args->count; ++j) {
+                               impl_arg *iarg = impl->func->args->args[j];
+
+                               dprintf(fd, "%s%s %s$%s",
+                                               j ? ", " : "",
+                                               iarg->type->name,
+                                               iarg->var->reference ? "&" : "",
+                                               iarg->var->name);
+                               if (iarg->def) {
+                                       dprintf(fd, " = %s", iarg->def->text);
+                               }
+                       }
+                       dprintf(fd, ") : %s%s {\n",
+                                       impl->func->return_reference ? "&":"",
+                                       impl->func->return_type->name);
+                       if (impl->stmts) {
+                               for (j = 0; j < impl->stmts->let.count; ++j) {
+                                       let_stmt *let = impl->stmts->let.list[j];
+
+                                       dprintf(fd, "\tlet %s", let->var->name);
+                                       if (let->val) {
+                                               dprintf(fd, " = %s", let->val->is_reference ? "&" : "");
+                                               if (let->val->func) {
+                                                       dprintf(fd, "%s(", let->val->func->name);
+                                                       if (let->val->func->alloc) {
+                                                               dump_num_exp(fd, let->val->func->alloc->nmemb);
+                                                               dprintf(fd, ", ");
+                                                               dump_num_exp(fd, let->val->func->alloc->size);
+                                                       } else {
+                                                               dprintf(fd, "$%s", let->val->var->name);
+                                                       }
+                                                       dprintf(fd, ");\n");
+                                               } else {
+                                                       dprintf(fd, "NULL;\n");
+                                               }
+                                       }
+                               }
+                               for (j = 0; j < impl->stmts->ret.count; ++j) {
+                                       return_stmt *ret = impl->stmts->ret.list[j];
+
+                                       dprintf(fd, "\treturn ");
+                                       dump_impl_set_value(fd, ret->set, 1);
+                               }
+                               for (j = 0; j < impl->stmts->set.count; ++j) {
+                                       set_stmt *set = impl->stmts->set.list[j];
+
+                                       dprintf(fd, "\tset $%s = ", set->var->name);
+                                       dump_impl_set_value(fd, set->val, 1);
+                               }
+                               for (j = 0; j < impl->stmts->fre.count; ++j) {
+                                       free_stmt *fre = impl->stmts->fre.list[j];
+
+                                       dprintf(fd, "\tfree ");
+                                       for (k = 0; k < fre->calls->count; ++k) {
+                                               free_call *call = fre->calls->list[k];
+
+                                               if (k) {
+                                                       dprintf(fd, ", ");
+                                               }
+                                               dprintf(fd, "%s(", call->func);
+                                               for (l = 0; l < call->vars->count; ++l) {
+                                                       decl_var *fvar = call->vars->vars[l];
+
+                                                       dump_decl_var(fd, fvar);
+                                               }
+                                               dprintf(fd, ");\n");
+                                       }
+                               }
+                       }
+                       dprintf(fd, "}\n");
+               }
+               dprintf(fd, "\n");
+       }
 }
 
 void PSI_ContextDtor(PSI_Context *C)