X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-psi;a=blobdiff_plain;f=src%2Fcontext_dump.c;h=b84f6c8f2c63371bec2cc680546f11a505f5eda4;hp=708b36f1e950ab581121ac768c82b15dece6934f;hb=898c6dac30d12d7fe56662d66a8e73c340926d64;hpb=9bd48d16d68944f3a1e9366fe8c6f3c6d985bf56 diff --git a/src/context_dump.c b/src/context_dump.c index 708b36f..b84f6c8 100644 --- a/src/context_dump.c +++ b/src/context_dump.c @@ -1,20 +1,57 @@ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + #include "php.h" #include "php_psi.h" +#include "types.h" + #include "libjit.h" #include "libffi.h" -static inline void dump_decl_type(int fd, decl_type *t) { - const char *pre; +static inline void dump_level(int fd, unsigned level) { + dprintf(fd, "%.*s", level > 30 ? 30 : level, + "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"); +} + +static inline void dump_decl_arg(int fd, decl_arg *darg, unsigned level); +static inline void dump_struct_args(int fd, decl_args *args, unsigned level); +static inline void dump_enum_items(int fd, decl_enum_items *items, unsigned level); + +#define is_anon_type(name, type) !strncmp(name, type "@", sizeof(type)) +static inline void dump_decl_type(int fd, decl_type *t, unsigned level) { switch (t->type) { + case PSI_T_POINTER: + dprintf(fd, "%s *", t->name); + return; + case PSI_T_STRUCT: - pre = "struct "; + dprintf(fd, "struct "); + if (!strncmp(t->name, "struct@", sizeof("struct"))) { + dump_struct_args(fd, t->real.strct->args, level); + return; + } + break; + + case PSI_T_ENUM: + dprintf(fd, "enum "); + if (!strncmp(t->name, "enum@", sizeof("enum"))) { + dump_enum_items(fd, t->real.enm->items, level); + return; + } + break; + + case PSI_T_UNION: + dprintf(fd, "union "); + if (!strncmp(t->name, "union@", sizeof("union"))) { + dump_struct_args(fd, t->real.unn->args, level); + return; + } break; - default: - pre = ""; } - dprintf(fd, "%s%s", pre, t->name); + dprintf(fd, "%s", t->name); } static inline void dump_decl_var(int fd, decl_var *v) { @@ -24,14 +61,31 @@ static inline void dump_decl_var(int fd, decl_var *v) { } } -static inline void dump_decl_arg(int fd, decl_arg *a) { - dump_decl_type(fd, a->type); - dprintf(fd, " "); - dump_decl_var(fd, a->var); -} - -static inline void dump_level(int fd, unsigned level) { - dprintf(fd, "%.*s", level > 10 ? 10 : level, "\t\t\t\t\t\t\t\t\t\t"); +static inline void dump_decl_arg(int fd, decl_arg *a, unsigned level) { + if (a->type->type == PSI_T_FUNCTION) { + dump_decl_type(fd, a->type->real.func->func->type, level); + dprintf(fd, " ("); + dump_decl_var(fd, a->var); + dprintf(fd, ")("); + if (a->type->real.func->args) { + size_t j; + + for (j = 0; j < a->type->real.func->args->count; ++j) { + if (j) { + dprintf(fd, ", "); + } + dump_decl_arg(fd, a->type->real.func->args->args[j], level+1); + } + if (a->type->real.func->args->varargs) { + dprintf(fd, ", ..."); + } + } + dprintf(fd, ")"); + } else { + dump_decl_type(fd, a->type, level); + dprintf(fd, " "); + dump_decl_var(fd, a->var); + } } static inline void dump_num_exp(int fd, num_exp *exp) { @@ -96,10 +150,10 @@ static inline void dump_impl_set_value(int fd, set_value *set, unsigned level, i dprintf(fd, ", "); dump_num_exp(fd, set->num); } - if (set->inner && set->func->type != PSI_T_ELLIPSIS) { + if (set->inner && set->inner->vals && set->func->type != PSI_T_ELLIPSIS) { dprintf(fd, ",\n"); - for (i = 0; i < set->count; ++i) { - dump_impl_set_value(fd, set->inner[i], level+1, i == (set->count - 1)); + for (i = 0; i < set->inner->count; ++i) { + dump_impl_set_value(fd, set->inner->vals[i], level+1, i == (set->inner->count - 1)); } /* only if inner stmts, i.e. with new lines, were dumped */ dump_level(fd, level); @@ -113,12 +167,8 @@ static inline void dump_impl_set_value(int fd, set_value *set, unsigned level, i static inline void dump_typedef(int fd, decl_arg *tdef) { dprintf(fd, "typedef "); - dump_decl_arg(fd, tdef); - /* - dump_decl_type(fd, tdef->type); - dprintf(fd, " %s%s;", tdef->type->type == PSI_T_POINTER ? "*":"", - tdef->alias); - */ + dump_decl_arg(fd, tdef, 0); + dprintf(fd, ";"); } static inline void dump_typedefs(int fd, decl_typedefs *defs) { @@ -132,20 +182,29 @@ static inline void dump_typedefs(int fd, decl_typedefs *defs) { } } -static inline void dump_struct(int fd, decl_struct *strct) { +static inline void dump_struct_args(int fd, decl_args *args, unsigned level) { size_t j; - dprintf(fd, "struct %s::(%zu)", strct->name, strct->size); - if (strct->args && strct->args->count) { - dprintf(fd, " {\n"); - for (j = 0; j < strct->args->count; ++j) { - decl_arg *sarg = strct->args->args[j]; + dprintf(fd, " {\n"); + if (args) { + ++level; + for (j = 0; j < args->count; ++j) { + decl_arg *sarg = args->args[j]; - dprintf(fd, "\t"); - dump_decl_arg(fd, sarg); + dump_level(fd, level); + dump_decl_arg(fd, sarg, level); dprintf(fd, "::(%zu, %zu);\n", sarg->layout->pos, sarg->layout->len); } - dprintf(fd, "}"); + --level; + } + dump_level(fd, level); + dprintf(fd, "}"); +} + +static inline void dump_struct(int fd, decl_struct *strct) { + dprintf(fd, "struct %s::(%zu, %zu)", strct->name, strct->align, strct->size); + if (strct->args && strct->args->count) { + dump_struct_args(fd, strct->args, 0); } else { dprintf(fd, ";"); } @@ -157,27 +216,65 @@ static inline void dump_structs(int fd, decl_structs *structs) { for (i = 0; i < structs->count; ++i) { decl_struct *strct = structs->list[i]; - dump_struct(fd, strct); - dprintf(fd, "\n"); + if (!is_anon_type(strct->name, "struct")) { + dump_struct(fd, strct); + dprintf(fd, "\n"); + } } } -static inline void dump_enum(int fd, decl_enum *e) { +static inline void dump_union(int fd, decl_union *unn) { size_t j; - dprintf(fd, "enum %s {\n", e->name); - for (j = 0; j < e->items->count; ++j) { - decl_enum_item *i = e->items->list[j]; + dprintf(fd, "union %s::(%zu, %zu) {\n", unn->name, unn->align, unn->size); + for (j = 0; j < unn->args->count; ++j) { + decl_arg *uarg = unn->args->args[j]; - if (j) { - dprintf(fd, ",\n"); + dprintf(fd, "\t"); + dump_decl_arg(fd, uarg, 0); + dprintf(fd, "::(%zu, %zu);\n", uarg->layout->pos, uarg->layout->len); + } + dprintf(fd, "}"); +} + +static inline void dump_unions(int fd, decl_unions *unions) { + size_t i; + + for (i = 0; i < unions->count; ++i) { + decl_union *unn = unions->list[i]; + + if (!is_anon_type(unn->name, "union")) { + dump_union(fd, unn); + dprintf(fd, "\n"); } - dprintf(fd, "\t%s", i->name); - if (i->num && i->num != &i->inc) { - dprintf(fd, " = "); - dump_num_exp(fd, i->num); + } +} + +static inline void dump_enum_items(int fd, decl_enum_items *items, unsigned level) { + size_t j; + + if (items) { + ++level; + for (j = 0; j < items->count; ++j) { + decl_enum_item *i = items->list[j]; + + if (j) { + dprintf(fd, ",\n"); + } + dump_level(fd, level); + dprintf(fd, "%s", i->name); + if (i->num && i->num != &i->inc) { + dprintf(fd, " = "); + dump_num_exp(fd, i->num); + } } + --level; } +} + +static inline void dump_enum(int fd, decl_enum *e) { + dprintf(fd, "enum %s {\n", e->name); + dump_enum_items(fd, e->items, 0); dprintf(fd, "\n}"); } @@ -187,8 +284,10 @@ static inline void dump_enums(int fd, decl_enums *enums) { for (i = 0; i < enums->count; ++i) { decl_enum *e = enums->list[i]; - dump_enum(fd, e); - dprintf(fd, "\n"); + if (!is_anon_type(e->name, "enum")) { + dump_enum(fd, e); + dprintf(fd, "\n"); + } } } static inline void dump_constant(int fd, constant *cnst) { @@ -215,14 +314,14 @@ static inline void dump_decl(int fd, decl *decl) { size_t j; dprintf(fd, "%s ", decl->abi->convention); - dump_decl_arg(fd, decl->func); + dump_decl_arg(fd, decl->func, 0); 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]); + dump_decl_arg(fd, decl->args->args[j], 0); } if (decl->args->varargs) { dprintf(fd, ", ..."); @@ -250,7 +349,7 @@ static inline void dump_impl_func(int fd, impl_func *func) { for (j = 0; j < func->args->count; ++j) { impl_arg *iarg = func->args->args[j]; - dprintf(fd, "%s%s %s$%s", + dprintf(fd, "%s%s %s%s", j ? ", " : "", iarg->type->name, iarg->var->reference ? "&" : "", @@ -262,7 +361,7 @@ static inline void dump_impl_func(int fd, impl_func *func) { if (func->args->vararg.name) { impl_arg *vararg = func->args->vararg.name; - dprintf(fd, ", %s %s...$%s", + dprintf(fd, ", %s %s...%s", vararg->type->name, vararg->var->reference ? "&" : "", vararg->var->name); @@ -273,45 +372,97 @@ static inline void dump_impl_func(int fd, impl_func *func) { func->return_type->name); } -static inline void dump_impl_let_stmt(int fd, let_stmt *let) { - dprintf(fd, "\tlet %s", let->var->name); - if (let->val) { - dprintf(fd, " = %s", let->val->flags.one.is_reference ? "&" : ""); - switch (let->val->kind) { - case PSI_LET_NULL: - dprintf(fd, "NULL"); - break; - case PSI_LET_TMP: - dump_decl_var(fd, let->val->data.var); - break; - case PSI_LET_CALLOC: - dprintf(fd, "calloc("); - dump_num_exp(fd, let->val->data.alloc->nmemb); - dprintf(fd, ", "); - dump_num_exp(fd, let->val->data.alloc->size); - dprintf(fd, ")"); - break; - case PSI_LET_FUNC: - dprintf(fd, "%s($%s)", let->val->data.func->name, - let->val->data.func->var->name); - break; - case PSI_LET_NUMEXP: - dump_num_exp(fd, let->val->data.num); - break; +static inline void dump_impl_let_func(int fd, let_func *func, unsigned level); - EMPTY_SWITCH_DEFAULT_CASE(); +static inline void dump_impl_let_val(int fd, let_val *val, unsigned level, int last) { + if (level > 1) { + /* only if not directly after `set ...` */ + dump_level(fd, level); + } + + dprintf(fd, "%s", val->flags.one.is_reference ? "&" : ""); + switch (val->kind) { + case PSI_LET_NULL: + dprintf(fd, "NULL"); + break; + case PSI_LET_TMP: + dump_decl_var(fd, val->data.var); + break; + case PSI_LET_CALLOC: + dprintf(fd, "calloc("); + dump_num_exp(fd, val->data.alloc->nmemb); + dprintf(fd, ", "); + dump_num_exp(fd, val->data.alloc->size); + dprintf(fd, ")"); + break; + case PSI_LET_CALLBACK: + dprintf(fd, "callback %s(%s(", val->data.callback->func->name, + val->data.callback->func->var->name); + if (val->data.callback->args) { + size_t i, c = val->data.callback->args->count; + + dprintf(fd, "\n"); + for (i = 0; i < c; ++i) { + set_value *set = val->data.callback->args->vals[i]; + ++level; + dump_impl_set_value(fd, set, level, i + 1 == c); + --level; + } + dump_level(fd, level); } + dprintf(fd, "))"); + break; + case PSI_LET_FUNC: + dump_impl_let_func(fd, val->data.func, level); + break; + case PSI_LET_NUMEXP: + dump_num_exp(fd, val->data.num); + break; + + EMPTY_SWITCH_DEFAULT_CASE(); + } + if (level > 1) { + if (!last) { + dprintf(fd, ","); + } + } else { dprintf(fd, ";"); } } +static inline void dump_impl_let_func(int fd, let_func *func, unsigned level) { + dprintf(fd, "%s(%s", func->name, + func->var->name); + if (func->inner) { + size_t i; + + dprintf(fd, ","); + ++level; + for (i = 0; i < func->inner->count; ++i) { + dprintf(fd, "\n"); + dump_impl_let_val(fd, func->inner->vals[i], level, i+1 == func->inner->count); + } + --level; + dprintf(fd, "\n"); + dump_level(fd, level); + } + dprintf(fd, ")"); +} +static inline void dump_impl_let_stmt(int fd, let_stmt *let) { + dprintf(fd, "\tlet %s", let->var->name); + if (let->val) { + dprintf(fd, " = "); + dump_impl_let_val(fd, let->val, 1, 1); + } +} + static inline void dump_impl_return_stmt(int fd, return_stmt *ret) { dprintf(fd, "\treturn "); dump_impl_set_value(fd, ret->set, 1, 0); } static inline void dump_impl_set_stmt(int fd, set_stmt *set) { - dprintf(fd, "\tset $%s = ", set->var->name); + dprintf(fd, "\tset %s = ", set->var->name); dump_impl_set_value(fd, set->val, 1, 0); } @@ -389,15 +540,15 @@ static inline void dump_impls(int fd, impls *impls) { } } -void PSI_ContextDump(PSI_Context *C, int fd) +void psi_context_dump(struct psi_context *C, int fd) { #ifdef HAVE_LIBJIT - if (C->ops == PSI_Libjit()) { + if (C->ops == psi_libjit_ops()) { dprintf(fd, "// psi.engine=jit\n"); } #endif #ifdef HAVE_LIBFFI - if (C->ops == PSI_Libffi()) { + if (C->ops == psi_libffi_ops()) { dprintf(fd, "// psi.engine=ffi\n"); } #endif @@ -407,7 +558,10 @@ void PSI_ContextDump(PSI_Context *C, int fd) dump_typedefs(fd, C->defs); dprintf(fd, "\n"); } - + if (C->unions) { + dump_unions(fd, C->unions); + dprintf(fd, "\n"); + } if (C->structs) { dump_structs(fd, C->structs); dprintf(fd, "\n");