-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_types[] = {
- PSI_TYPES
- {0}
-};
-
-static struct psi_predef_const {
- token_t type_tag;
- const char *type_name;
- const char *var_name;
- const char *val_text;
- token_t val_type_tag;
-} 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}
-};
-
-static struct psi_predef_decl {
- token_t type_tag;
- const char *type_name;
- const char *var_name;
- size_t pointer_level;
- size_t array_size;
-} psi_predef_decls[] = {
- PSI_DECLS
- {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}
-};
-
-static int validate_lib(PSI_Data *data, void **dlopened) {
- char lib[MAXPATHLEN];
- const char *ptr = data->psi.file.ln;
- size_t len;
-
- if (!ptr) {
- /* FIXME: assume stdlib */
- return 1;
- } else if (!strchr(ptr, '/')) {
- len = snprintf(lib, MAXPATHLEN, "lib%s.%s", ptr, PHP_PSI_SHLIB_SUFFIX);
- if (MAXPATHLEN == len) {
- data->error(PSI_WARNING, "Library name too long: '%s'", ptr);
- }
- lib[len] = 0;
- ptr = lib;
- }
- if (!(*dlopened = dlopen(ptr, RTLD_LAZY|RTLD_LOCAL))) {
- data->error(PSI_WARNING, "Could not open library '%s': %s.",
- data->psi.file.ln, dlerror());
- return 0;
- }
- return 1;
-}
-
-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) {
- 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) {
- size_t i;
-
- if (type->strct) {
- return 1;
- }
- for (i = 0; i < structs->count; ++i) {
- if (!strcmp(structs->list[i]->name, type->name)) {
- type->strct = structs->list[i];
- return 1;
- }
- }
- return 0;
-}
-
-static inline int validate_decl_type(PSI_Data *data, decl_type *type) {
- switch (type->type) {
- case PSI_T_NAME:
- if (!data->defs || !locate_decl_type_alias(data->defs, type)) {
- return 0;
- }
- 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;
- }
- break;
- }
- return 1;
-}
-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'%s'",
- def->type->name, def->type->type == PSI_T_STRUCT?"struct ":"",def->alias);
- return 0;
- }
- /* FIXME: check def->alias */
- return 1;
-}
-
-static inline int validate_constant(PSI_Data *data, constant *c) {
- /* FIXME */
- return 1;
-}
-
-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'(%d) as type for '%s'",
- arg->type->name, arg->type->type, arg->var->name);
- return 0;
- }
- return 1;
-}
-
-static inline int validate_decl_struct(PSI_Data *data, decl_struct *s) {
- size_t i;
-
- for (i = 0; i < s->args->count; ++i) {
- if (!validate_decl_arg(data, s->args->args[i])) {
- return 0;
- }
- }
-
- for (i = 0; i < s->args->count; ++i) {
- decl_arg *darg = s->args->args[i];
-
- if (!validate_decl_arg(data, darg)) {
- return 0;
- }
-
- ZEND_ASSERT(!darg->var->arg || darg->var->arg == darg);
- darg->var->arg = darg;
-
- if (!darg->layout) {
- token_t t;
-
- if (darg->var->pointer_level && (!darg->var->array_size || darg->var->pointer_level == 1)) {
- t = PSI_T_POINTER;
- } else {
- t = real_decl_type(darg->type)->type;
- }
-
- if (i) {
- decl_arg *last = s->args->args[i-1];
- darg->layout = init_decl_struct_layout(
- psi_t_align(t, last->layout->pos + last->layout->len),
- psi_t_size(t) * darg->var->array_size);
- } else {
- darg->layout = init_decl_struct_layout(0, psi_t_size(t));
- }
- }
- if (s->size < darg->layout->pos + darg->layout->len) {
- s->size = darg->layout->pos + darg->layout->len;
- }
- }
- return 1;
-}
-
-static const char * const abi_ccs[] = {
- "default", /* \ */
- "extern", /* > - all the same */
- "cdecl", /* / */
- "stdcall",
- "fastcall",
-};
-static inline int validate_decl_abi(PSI_Data *data, decl_abi *abi) {
- size_t i;
-
- for (i = 0; i < sizeof(abi_ccs)/sizeof(char*); ++i) {
- if (strcasecmp(abi->convention, abi_ccs[i])) {
- return 1;
- }
- }
- data->error(PSI_WARNING, "Invalid calling convention: '%s'", abi->convention);
- return 0;
-}
-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;
- }
-
- 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) {
- data->error(PSI_WARNING, "Failed to locate symbol '%s': %s",
- func->var->name, dlerror());
- }
- }
- return 1;
-}
-
-static inline int validate_decl(PSI_Data *data, void *dl, decl *decl) {
- if (!validate_decl_abi(data, decl->abi)) {
- return 0;
- }
- if (!validate_decl_func(data, dl, decl, decl->func)) {
- return 0;
- }
- if (decl->args) {
- size_t i;
-
- for (i = 0; i < decl->args->count; ++i) {
- if (!validate_decl_arg(data, decl->args->args[i])) {
- return 0;
- }
- }
- }
- return 1;
-}
-static inline decl_arg *locate_decl_var_arg(decl_var *var, decl_args *args) {
- size_t i;
-
- for (i = 0; i < args->count; ++i) {
- decl_arg *arg = args->args[i];
-
- if (!strcmp(var->name, arg->var->name)) {
- ZEND_ASSERT(!var->arg || var->arg == arg);
- return var->arg = arg;
- }
- }
-
- return NULL;
-}
-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];
-
- switch (set->func->type) {
- case PSI_T_TO_BOOL:
- set->func->handler = psi_to_bool;
- break;
- case PSI_T_TO_INT:
- set->func->handler = psi_to_int;
- break;
- case PSI_T_TO_FLOAT:
- set->func->handler = psi_to_double;
- break;
- case PSI_T_TO_STRING:
- set->func->handler = psi_to_string;
- break;
- 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();
- }
-
- for (i = 1; i < set->vars->count; ++i) {
- if (!locate_decl_var_arg(set->vars->vars[i], ref_list)) {
- return 0;
- }
- }
- set_var->arg = ref;
-
- 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, 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 = locate_decl_var_arg(sub_var, ref_list);
-
- set->inner[0]->outer.set = set;
- if (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;
- }
- }
- } else if (set->count > 1) {
- data->error(E_WARNING, "Inner `set` statement casts on pointers may only occur once");
- return 0;
- }
-
- return 1;
-}
-static inline decl *locate_impl_decl(decls *decls, return_stmt *ret) {
- if (decls) {
- size_t i;
-
- for (i = 0; i < decls->count; ++i) {
- if (!strcmp(decls->list[i]->func->var->name, ret->set->vars->vars[0]->name)) {
- ret->decl = decls->list[i]->func;
- return decls->list[i];
- }
- }
- }
-
- return NULL;
-}
-static inline int validate_impl_ret_stmt(PSI_Data *data, impl *impl) {
- return_stmt *ret;
-
- /* we must have exactly one ret stmt delcaring the native func to call */
- /* and which type cast to apply */
- if (impl->stmts->ret.count != 1) {
- if (impl->stmts->ret.count > 1) {
- data->error(PSI_WARNING, "Too many `return` statements for implmentation %s;"
- " found %zu, exactly one is needed",
- impl->func->name, impl->stmts->ret.count);
- } else {
- data->error(PSI_WARNING, "Missing `return` statement for implementation %s",
- impl->func->name);
- }
- return 0;
- }
-
- ret = impl->stmts->ret.list[0];
-
- if (!(impl->decl = locate_impl_decl(data->decls, ret))) {
- data->error(PSI_WARNING, "Missing declaration for implementation %s",
- impl->func->name);
- return 0;
- }
-
- if (!validate_set_value(data, ret->set, ret->decl, impl->decl->args)) {
- return 0;
- }
-
- impl->decl->impl = impl;
-
- return 1;
-}
-static inline int validate_impl_let_stmts(PSI_Data *data, impl *impl) {
- size_t i, j;
- /* we can have multiple let stmts */
- /* check that we have a let stmt for every decl arg */
- if (impl->decl->args) for (i = 0; i < impl->decl->args->count; ++i) {
- decl_arg *darg = impl->decl->args->args[i];
- int check = 0;
-
- for (j = 0; j < impl->stmts->let.count; ++j) {
- let_stmt *let = impl->stmts->let.list[j];
-
- if (!strcmp(let->var->name, darg->var->name)) {
- darg->let = let;
- check = 1;
- break;
- }
- }
- if (!check) {
- data->error(PSI_WARNING, "Missing `let` statement for arg '%s %.*s%s'"
- " of declaration '%s' for implementation '%s'",
- darg->type->name, (int) darg->var->pointer_level, "*****",
- darg->var->name, impl->decl->func->var->name, impl->func->name);
- return 0;
- }
- }
- /* check that the let_value references a known variable or NULL */
- for (i = 0; i < impl->stmts->let.count; ++i) {
- let_stmt *let = impl->stmts->let.list[i];
- 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);
- return 0;
- }
- }
- if (let->val && let->val->var) {
- if (impl->func->args) for (j = 0; j < impl->func->args->count; ++j) {
- impl_arg *iarg = impl->func->args->args[j];
-
- if (!strcmp(let->val->var->name, iarg->var->name)) {
- let->arg = iarg;
- check = 1;
- break;
- }
- }
- if (!check) {
- data->error(PSI_WARNING, "Unknown value '$%s' of `let` statement"
- " for variable '%s' of implementation '%s'",
- let->val->var->name, let->var->name, impl->func->name);
- return 0;
- }
- }
- }
- return 1;
-}
-static inline int validate_impl_set_stmts(PSI_Data *data, impl *impl) {
- size_t i, j, k;
- /* we can have any count of set stmts; processing out vars */
- /* check that set stmts reference known variables */
- for (i = 0; i < impl->stmts->set.count; ++i) {
- set_stmt *set = impl->stmts->set.list[i];
- int check = 0;
-
- if (impl->func->args) for (j = 0; j < impl->func->args->count; ++j) {
- impl_arg *iarg = impl->func->args->args[j];
-
- if (!strcmp(set->var->name, iarg->var->name)) {
- set->arg = iarg;
- check = 1;
- break;
- }
- }
- if (!check) {
- data->error(PSI_WARNING, "Unknown variable '$%s' of `set` statement"
- " of implementation '%s'",
- set->var->name, impl->func->name);
- return 0;
- }
-
- for (j = 0; j < set->val->vars->count; ++j) {
- decl_var *set_var = set->val->vars->vars[j];
-
- check = 0;
- if (impl->decl->args) for (k = 0; k < impl->decl->args->count; ++k) {
- decl_arg *set_arg = impl->decl->args->args[k];
-
- if (!strcmp(set_var->name, set_arg->var->name)) {
- check = 1;
- set_var->arg = set_arg;
- if (!validate_set_value(data, set->val, set_arg, impl->decl->args)) {
- return 0;
- }
- break;
- }
- }
-
- if (!check) {
- data->error(PSI_WARNING, "Unknown value '%s' of `set` statement"
- " for variable '$%s' of implementation '%s'",
- set_var->name, set->arg->var->name, impl->func->name);
- return 0;
- }
- }
- }
- return 1;
-}
-static inline decl *locate_free_decl(decls *decls, free_call *f) {
- if (decls) {
- size_t i;
-
- for (i = 0; i < decls->count; ++i) {
- if (!strcmp(decls->list[i]->func->var->name, f->func)) {
- f->decl = decls->list[i];
- return decls->list[i];
- }
- }
- }
-
- return NULL;
-}
-static inline int validate_impl_free_stmts(PSI_Data *data, impl *impl) {
- size_t i, j, k, l;
- /* we can have any count of free stmts; freeing any out vars */
- for (i = 0; i < impl->stmts->fre.count; ++i) {
- free_stmt *fre = impl->stmts->fre.list[i];
-
- for (j = 0; j < fre->calls->count; ++j) {
- free_call *free_call = fre->calls->list[j];
-
- /* first find the decl of the free func */
- if (!locate_free_decl(data->decls, free_call)) {
- data->error(PSI_WARNING, "Unknown function '%s' in `free` statement"
- " of implementation '%s'", free_call->func, impl->func->name);
- return 0;
- }
- if (!impl->decl->args) {
- data->error(PSI_WARNING, "Declaration '%s' of implementation '%s'"
- " does not have any arguments to free",
- impl->decl->func->var->name, impl->func->name);
- }
-
- /* now check for known vars */
- for (l = 0; l < free_call->vars->count; ++l) {
- int check = 0;
- decl_var *free_var = free_call->vars->vars[l];
-
- for (k = 0; k < impl->decl->args->count; ++k) {
- decl_arg *free_arg = impl->decl->args->args[k];
-
- if (!strcmp(free_var->name, free_arg->var->name)) {
- check = 1;
- free_var->arg = free_arg;
- break;
- }
- }
-
- if (!check) {
- data->error(PSI_WARNING, "Unknown variable '%s' of `free` statement"
- " of implementation '%s'",
- free_var->name, impl->func->name);
- return 0;
- }
- }
- }
- }
- return 1;
-}
-static inline int validate_impl_stmts(PSI_Data *data, impl *impl) {
- if (!impl->stmts) {
- data->error(PSI_WARNING, "Missing body for implementation %s!",
- impl->func->name);
- return 0;
- }
-
- if (!validate_impl_ret_stmt(data, impl)) {
- return 0;
- }
-
- if (!validate_impl_let_stmts(data, impl)) {
- return 0;
- }
- if (!validate_impl_set_stmts(data, impl)) {
- return 0;
- }
- if (!validate_impl_free_stmts(data, impl)) {
- return 0;
- }