fix dump
[m6w6/ext-psi] / src / context_dump.c
index cb1825f51b773cb4f48cbf87786cca80d04ac4af..72c82b37f3649e9b17e0c141ab236bf6f521cd0f 100644 (file)
@@ -4,15 +4,46 @@
 #include "libjit.h"
 #include "libffi.h"
 
+static inline void dump_decl_arg(int fd, decl_arg *darg);
+
 static inline void dump_decl_type(int fd, decl_type *t) {
        const char *pre;
+       size_t j;
 
        switch (t->type) {
+       case PSI_T_POINTER:
+               dprintf(fd, "%s *", t->name);
+               return;
+
+       case PSI_T_FUNCTION:
+               dump_decl_arg(fd, t->func->func);
+               dprintf(fd, "(");
+               if (t->func->args) {
+                       for (j = 0; j < t->func->args->count; ++j) {
+                               if (j) {
+                                       dprintf(fd, ", ");
+                               }
+                               dump_decl_arg(fd, t->func->args->args[j]);
+                       }
+                       if (t->func->args->varargs) {
+                               dprintf(fd, ", ...");
+                       }
+               }
+               dprintf(fd, ")");
+               return;
+
        case PSI_T_STRUCT:
                pre = "struct ";
                break;
+       case PSI_T_ENUM:
+               pre = "enum ";
+               break;
+       case PSI_T_UNION:
+               pre = "union ";
+               break;
        default:
                pre = "";
+               break;
        }
        dprintf(fd, "%s%s", pre, t->name);
 }
@@ -26,8 +57,10 @@ 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);
+       if (a->type->type != PSI_T_FUNCTION) {
+               dprintf(fd, " ");
+               dump_decl_var(fd, a->var);
+       }
 }
 
 static inline void dump_level(int fd, unsigned level) {
@@ -96,10 +129,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);
@@ -111,18 +144,22 @@ static inline void dump_impl_set_value(int fd, set_value *set, unsigned level, i
        }
 }
 
-static inline void dump_typedef(int fd, decl_typedef *tdef) {
+static inline void dump_typedef(int fd, decl_arg *tdef) {
        dprintf(fd, "typedef ");
+       dump_decl_arg(fd, tdef);
+       dprintf(fd, ";");
+       /*
        dump_decl_type(fd, tdef->type);
        dprintf(fd, " %s%s;", tdef->type->type == PSI_T_POINTER ? "*":"",
                        tdef->alias);
+       */
 }
 
 static inline void dump_typedefs(int fd, decl_typedefs *defs) {
        size_t i;
 
        for (i = 0; i < defs->count; ++i) {
-               decl_typedef *tdef = defs->list[i];
+               decl_arg *tdef = defs->list[i];
 
                dump_typedef(fd, tdef);
                dprintf(fd, "\n");
@@ -132,7 +169,7 @@ static inline void dump_typedefs(int fd, decl_typedefs *defs) {
 static inline void dump_struct(int fd, decl_struct *strct) {
        size_t j;
 
-       dprintf(fd, "struct %s::(%zu)", strct->name, strct->size);
+       dprintf(fd, "struct %s::(%zu, %zu)", strct->name, strct->align, strct->size);
        if (strct->args && strct->args->count) {
                dprintf(fd, " {\n");
                for (j = 0; j < strct->args->count; ++j) {
@@ -159,6 +196,31 @@ static inline void dump_structs(int fd, decl_structs *structs) {
        }
 }
 
+static inline void dump_union(int fd, decl_union *unn) {
+       size_t 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];
+
+               dprintf(fd, "\t");
+               dump_decl_arg(fd, uarg);
+               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];
+
+               dump_union(fd, unn);
+               dprintf(fd, "\n");
+       }
+}
+
 static inline void dump_enum(int fd, decl_enum *e) {
        size_t j;
 
@@ -247,7 +309,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 ? "&" : "",
@@ -259,7 +321,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);
@@ -288,8 +350,23 @@ static inline void dump_impl_let_stmt(int fd, let_stmt *let) {
                        dump_num_exp(fd, let->val->data.alloc->size);
                        dprintf(fd, ")");
                        break;
+               case PSI_LET_CALLBACK:
+                       dprintf(fd, "callback %s(%s(", let->val->data.callback->func->name,
+                                       let->val->data.callback->func->var->name);
+                       if (let->val->data.callback->args) {
+                               size_t i, c = let->val->data.callback->args->count;
+
+                               dprintf(fd, "\n");
+                               for (i = 0; i < c; ++i) {
+                                       set_value *set = let->val->data.callback->args->vals[i];
+                                       dump_impl_set_value(fd, set, 2, i + 1 == c);
+                               }
+                               dprintf(fd, "\t");
+                       }
+                       dprintf(fd, "));");
+                       break;
                case PSI_LET_FUNC:
-                       dprintf(fd, "%s($%s)", let->val->data.func->name,
+                       dprintf(fd, "%s(%s)", let->val->data.func->name,
                                        let->val->data.func->var->name);
                        break;
                case PSI_LET_NUMEXP:
@@ -308,7 +385,7 @@ static inline void dump_impl_return_stmt(int fd, return_stmt *ret) {
 }
 
 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);
 }
 
@@ -404,7 +481,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");