7 static inline void dump_level(int fd
, unsigned level
) {
8 dprintf(fd
, "%.*s", level
> 30 ? 30 : level
,
9 "\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");
12 static inline void dump_decl_arg(int fd
, decl_arg
*darg
, unsigned level
);
13 static inline void dump_struct_args(int fd
, decl_args
*args
, unsigned level
);
14 static inline void dump_enum_items(int fd
, decl_enum_items
*items
, unsigned level
);
16 #define is_anon_type(name, type) !strncmp(name, type "@", sizeof(type))
18 static inline void dump_decl_type(int fd
, decl_type
*t
, unsigned level
) {
23 dprintf(fd
, "%s *", t
->name
);
27 dump_decl_arg(fd
, t
->func
->func
, level
);
30 for (j
= 0; j
< t
->func
->args
->count
; ++j
) {
34 dump_decl_arg(fd
, t
->func
->args
->args
[j
], level
+1);
36 if (t
->func
->args
->varargs
) {
44 dprintf(fd
, "struct ");
45 if (!strncmp(t
->name
, "struct@", sizeof("struct"))) {
46 dump_struct_args(fd
, t
->strct
->args
, level
);
53 if (!strncmp(t
->name
, "enum@", sizeof("enum"))) {
54 dump_enum_items(fd
, t
->enm
->items
, level
);
60 dprintf(fd
, "union ");
61 if (!strncmp(t
->name
, "union@", sizeof("union"))) {
62 dump_struct_args(fd
, t
->unn
->args
, level
);
67 dprintf(fd
, "%s", t
->name
);
70 static inline void dump_decl_var(int fd
, decl_var
*v
) {
71 dprintf(fd
, "%.*s%s", v
->pointer_level
-!!v
->array_size
, "**********", v
->name
);
73 dprintf(fd
, "[%u]", v
->array_size
);
77 static inline void dump_decl_arg(int fd
, decl_arg
*a
, unsigned level
) {
78 dump_decl_type(fd
, a
->type
, level
);
80 if (a
->type
->type
!= PSI_T_FUNCTION
) {
82 dump_decl_var(fd
, a
->var
);
86 static inline void dump_num_exp(int fd
, num_exp
*exp
) {
90 dprintf(fd
, "%s", exp
->u
.numb
);
93 dprintf(fd
, "%s", exp
->u
.cnst
->name
);
96 dump_decl_var(fd
, exp
->u
.dvar
);
99 dprintf(fd
, "%s", exp
->u
.enm
->name
);
101 EMPTY_SWITCH_DEFAULT_CASE();
106 switch (exp
->operator) {
107 case PSI_T_PLUS
: op
= '+'; break;
108 case PSI_T_MINUS
: op
= '-'; break;
109 case PSI_T_ASTERISK
:op
= '*'; break;
110 case PSI_T_SLASH
: op
= '/'; break;
111 EMPTY_SWITCH_DEFAULT_CASE();
113 dprintf(fd
, " %c ", op
);
119 static inline void dump_impl_set_value(int fd
, set_value
*set
, unsigned level
, int last
) {
123 /* only if not directly after `set ...` */
124 dump_level(fd
, level
);
127 if (set
->func
->type
== PSI_T_ELLIPSIS
) {
128 dprintf(fd
, "%s(", set
->outer
.set
->func
->name
);
130 dprintf(fd
, "%s(", set
->func
->name
);
133 for (i
= 0; i
< set
->vars
->count
; ++i
) {
134 decl_var
*svar
= set
->vars
->vars
[i
];
138 dump_decl_var(fd
, svar
);
141 if (set
->func
->type
== PSI_T_ELLIPSIS
) {
142 dprintf(fd
, ", ...");
146 dump_num_exp(fd
, set
->num
);
148 if (set
->inner
&& set
->inner
->vals
&& set
->func
->type
!= PSI_T_ELLIPSIS
) {
150 for (i
= 0; i
< set
->inner
->count
; ++i
) {
151 dump_impl_set_value(fd
, set
->inner
->vals
[i
], level
+1, i
== (set
->inner
->count
- 1));
153 /* only if inner stmts, i.e. with new lines, were dumped */
154 dump_level(fd
, level
);
157 dprintf(fd
, ")%s\n", last
? "" : ",");
163 static inline void dump_typedef(int fd
, decl_arg
*tdef
) {
164 dprintf(fd
, "typedef ");
165 dump_decl_arg(fd
, tdef
, 0);
169 static inline void dump_typedefs(int fd
, decl_typedefs
*defs
) {
172 for (i
= 0; i
< defs
->count
; ++i
) {
173 decl_arg
*tdef
= defs
->list
[i
];
175 dump_typedef(fd
, tdef
);
180 static inline void dump_struct_args(int fd
, decl_args
*args
, unsigned level
) {
186 for (j
= 0; j
< args
->count
; ++j
) {
187 decl_arg
*sarg
= args
->args
[j
];
189 dump_level(fd
, level
);
190 dump_decl_arg(fd
, sarg
, level
);
191 dprintf(fd
, "::(%zu, %zu);\n", sarg
->layout
->pos
, sarg
->layout
->len
);
195 dump_level(fd
, level
);
199 static inline void dump_struct(int fd
, decl_struct
*strct
) {
200 dprintf(fd
, "struct %s::(%zu, %zu)", strct
->name
, strct
->align
, strct
->size
);
201 if (strct
->args
&& strct
->args
->count
) {
202 dump_struct_args(fd
, strct
->args
, 0);
208 static inline void dump_structs(int fd
, decl_structs
*structs
) {
211 for (i
= 0; i
< structs
->count
; ++i
) {
212 decl_struct
*strct
= structs
->list
[i
];
214 if (!is_anon_type(strct
->name
, "struct")) {
215 dump_struct(fd
, strct
);
221 static inline void dump_union(int fd
, decl_union
*unn
) {
224 dprintf(fd
, "union %s::(%zu, %zu) {\n", unn
->name
, unn
->align
, unn
->size
);
225 for (j
= 0; j
< unn
->args
->count
; ++j
) {
226 decl_arg
*uarg
= unn
->args
->args
[j
];
229 dump_decl_arg(fd
, uarg
, 0);
230 dprintf(fd
, "::(%zu, %zu);\n", uarg
->layout
->pos
, uarg
->layout
->len
);
235 static inline void dump_unions(int fd
, decl_unions
*unions
) {
238 for (i
= 0; i
< unions
->count
; ++i
) {
239 decl_union
*unn
= unions
->list
[i
];
241 if (!is_anon_type(unn
->name
, "union")) {
248 static inline void dump_enum_items(int fd
, decl_enum_items
*items
, unsigned level
) {
253 for (j
= 0; j
< items
->count
; ++j
) {
254 decl_enum_item
*i
= items
->list
[j
];
259 dump_level(fd
, level
);
260 dprintf(fd
, "%s", i
->name
);
261 if (i
->num
&& i
->num
!= &i
->inc
) {
263 dump_num_exp(fd
, i
->num
);
270 static inline void dump_enum(int fd
, decl_enum
*e
) {
271 dprintf(fd
, "enum %s {\n", e
->name
);
272 dump_enum_items(fd
, e
->items
, 0);
276 static inline void dump_enums(int fd
, decl_enums
*enums
) {
279 for (i
= 0; i
< enums
->count
; ++i
) {
280 decl_enum
*e
= enums
->list
[i
];
282 if (!is_anon_type(e
->name
, "enum")) {
288 static inline void dump_constant(int fd
, constant
*cnst
) {
289 dprintf(fd
, "const %s %s = ", cnst
->type
->name
, cnst
->name
);
290 if (cnst
->val
->type
== PSI_T_QUOTED_STRING
) {
291 dprintf(fd
, "\"%s\";", cnst
->val
->text
);
293 dprintf(fd
, "%s;", cnst
->val
->text
);
297 static inline void dump_constants(int fd
, constants
*consts
) {
300 for (i
= 0; i
< consts
->count
; ++i
) {
301 constant
*cnst
= consts
->list
[i
];
303 dump_constant(fd
, cnst
);
308 static inline void dump_decl(int fd
, decl
*decl
) {
311 dprintf(fd
, "%s ", decl
->abi
->convention
);
312 dump_decl_arg(fd
, decl
->func
, 0);
315 for (j
= 0; j
< decl
->args
->count
; ++j
) {
319 dump_decl_arg(fd
, decl
->args
->args
[j
], 0);
321 if (decl
->args
->varargs
) {
322 dprintf(fd
, ", ...");
328 static inline void dump_decls(int fd
, decls
*decls
) {
331 for (i
= 0; i
< decls
->count
; ++i
) {
332 decl
*decl
= decls
->list
[i
];
339 static inline void dump_impl_func(int fd
, impl_func
*func
) {
342 dprintf(fd
, "function %s(", func
->name
);
344 for (j
= 0; j
< func
->args
->count
; ++j
) {
345 impl_arg
*iarg
= func
->args
->args
[j
];
347 dprintf(fd
, "%s%s %s%s",
350 iarg
->var
->reference
? "&" : "",
353 dprintf(fd
, " = %s", iarg
->def
->text
);
356 if (func
->args
->vararg
.name
) {
357 impl_arg
*vararg
= func
->args
->vararg
.name
;
359 dprintf(fd
, ", %s %s...%s",
361 vararg
->var
->reference
? "&" : "",
365 dprintf(fd
, ") : %s%s",
366 func
->return_reference
? "&":"",
367 func
->return_type
->name
);
370 static inline void dump_impl_let_stmt(int fd
, let_stmt
*let
) {
371 dprintf(fd
, "\tlet %s", let
->var
->name
);
373 dprintf(fd
, " = %s", let
->val
->flags
.one
.is_reference
? "&" : "");
374 switch (let
->val
->kind
) {
379 dump_decl_var(fd
, let
->val
->data
.var
);
382 dprintf(fd
, "calloc(");
383 dump_num_exp(fd
, let
->val
->data
.alloc
->nmemb
);
385 dump_num_exp(fd
, let
->val
->data
.alloc
->size
);
388 case PSI_LET_CALLBACK
:
389 dprintf(fd
, "callback %s(%s(", let
->val
->data
.callback
->func
->name
,
390 let
->val
->data
.callback
->func
->var
->name
);
391 if (let
->val
->data
.callback
->args
) {
392 size_t i
, c
= let
->val
->data
.callback
->args
->count
;
395 for (i
= 0; i
< c
; ++i
) {
396 set_value
*set
= let
->val
->data
.callback
->args
->vals
[i
];
397 dump_impl_set_value(fd
, set
, 2, i
+ 1 == c
);
404 dprintf(fd
, "%s(%s)", let
->val
->data
.func
->name
,
405 let
->val
->data
.func
->var
->name
);
408 dump_num_exp(fd
, let
->val
->data
.num
);
411 EMPTY_SWITCH_DEFAULT_CASE();
417 static inline void dump_impl_return_stmt(int fd
, return_stmt
*ret
) {
418 dprintf(fd
, "\treturn ");
419 dump_impl_set_value(fd
, ret
->set
, 1, 0);
422 static inline void dump_impl_set_stmt(int fd
, set_stmt
*set
) {
423 dprintf(fd
, "\tset %s = ", set
->var
->name
);
424 dump_impl_set_value(fd
, set
->val
, 1, 0);
427 static inline void dump_impl_free_call(int fd
, free_call
*call
) {
430 dprintf(fd
, "%s(", call
->func
);
431 for (l
= 0; l
< call
->vars
->count
; ++l
) {
432 decl_var
*fvar
= call
->vars
->vars
[l
];
434 dump_decl_var(fd
, fvar
);
439 static inline void dump_impl_free_stmt(int fd
, free_stmt
*fre
) {
442 dprintf(fd
, "\tfree ");
443 for (k
= 0; k
< fre
->calls
->count
; ++k
) {
444 free_call
*call
= fre
->calls
->list
[k
];
449 dump_impl_free_call(fd
, call
);
453 static inline void dump_impl_stmts(int fd
, impl_stmts
*stmts
) {
456 for (j
= 0; j
< stmts
->let
.count
; ++j
) {
457 let_stmt
*let
= stmts
->let
.list
[j
];
458 dump_impl_let_stmt(fd
, let
);
461 for (j
= 0; j
< stmts
->ret
.count
; ++j
) {
462 return_stmt
*ret
= stmts
->ret
.list
[j
];
463 dump_impl_return_stmt(fd
, ret
);
466 for (j
= 0; j
< stmts
->set
.count
; ++j
) {
467 set_stmt
*set
= stmts
->set
.list
[j
];
469 dump_impl_set_stmt(fd
, set
);
472 for (j
= 0; j
< stmts
->fre
.count
; ++j
) {
473 free_stmt
*fre
= stmts
->fre
.list
[j
];
475 dump_impl_free_stmt(fd
, fre
);
480 static inline void dump_impl(int fd
, impl
*impl
) {
482 dump_impl_func(fd
, impl
->func
);
485 dump_impl_stmts(fd
, impl
->stmts
);
490 static inline void dump_impls(int fd
, impls
*impls
) {
493 for (i
= 0; i
< impls
->count
; ++i
) {
494 impl
*impl
= impls
->list
[i
];
501 void PSI_ContextDump(PSI_Context
*C
, int fd
)
504 if (C
->ops
== PSI_Libjit()) {
505 dprintf(fd
, "// psi.engine=jit\n");
509 if (C
->ops
== PSI_Libffi()) {
510 dprintf(fd
, "// psi.engine=ffi\n");
516 dump_typedefs(fd
, C
->defs
);
520 dump_unions(fd
, C
->unions
);
524 dump_structs(fd
, C
->structs
);
528 dump_enums(fd
, C
->enums
);
532 dump_constants(fd
, C
->consts
);
536 dump_decls(fd
, C
->decls
);
540 dump_impls(fd
, C
->impls
);