7 static inline void dump_decl_arg(int fd
, decl_arg
*darg
);
9 static inline void dump_decl_type(int fd
, decl_type
*t
) {
15 dprintf(fd
, "%s *", t
->name
);
19 dump_decl_arg(fd
, t
->func
->func
);
22 for (j
= 0; j
< t
->func
->args
->count
; ++j
) {
26 dump_decl_arg(fd
, t
->func
->args
->args
[j
]);
28 if (t
->func
->args
->varargs
) {
48 dprintf(fd
, "%s%s", pre
, t
->name
);
51 static inline void dump_decl_var(int fd
, decl_var
*v
) {
52 dprintf(fd
, "%.*s%s", v
->pointer_level
-!!v
->array_size
, "**********", v
->name
);
54 dprintf(fd
, "[%u]", v
->array_size
);
58 static inline void dump_decl_arg(int fd
, decl_arg
*a
) {
59 dump_decl_type(fd
, a
->type
);
60 if (a
->type
->type
!= PSI_T_FUNCTION
) {
62 dump_decl_var(fd
, a
->var
);
66 static inline void dump_level(int fd
, unsigned level
) {
67 dprintf(fd
, "%.*s", level
> 10 ? 10 : level
, "\t\t\t\t\t\t\t\t\t\t");
70 static inline void dump_num_exp(int fd
, num_exp
*exp
) {
74 dprintf(fd
, "%s", exp
->u
.numb
);
77 dprintf(fd
, "%s", exp
->u
.cnst
->name
);
80 dump_decl_var(fd
, exp
->u
.dvar
);
83 dprintf(fd
, "%s", exp
->u
.enm
->name
);
85 EMPTY_SWITCH_DEFAULT_CASE();
90 switch (exp
->operator) {
91 case PSI_T_PLUS
: op
= '+'; break;
92 case PSI_T_MINUS
: op
= '-'; break;
93 case PSI_T_ASTERISK
:op
= '*'; break;
94 case PSI_T_SLASH
: op
= '/'; break;
95 EMPTY_SWITCH_DEFAULT_CASE();
97 dprintf(fd
, " %c ", op
);
103 static inline void dump_impl_set_value(int fd
, set_value
*set
, unsigned level
, int last
) {
107 /* only if not directly after `set ...` */
108 dump_level(fd
, level
);
111 if (set
->func
->type
== PSI_T_ELLIPSIS
) {
112 dprintf(fd
, "%s(", set
->outer
.set
->func
->name
);
114 dprintf(fd
, "%s(", set
->func
->name
);
117 for (i
= 0; i
< set
->vars
->count
; ++i
) {
118 decl_var
*svar
= set
->vars
->vars
[i
];
122 dump_decl_var(fd
, svar
);
125 if (set
->func
->type
== PSI_T_ELLIPSIS
) {
126 dprintf(fd
, ", ...");
130 dump_num_exp(fd
, set
->num
);
132 if (set
->inner
&& set
->inner
->vals
&& set
->func
->type
!= PSI_T_ELLIPSIS
) {
134 for (i
= 0; i
< set
->inner
->count
; ++i
) {
135 dump_impl_set_value(fd
, set
->inner
->vals
[i
], level
+1, i
== (set
->inner
->count
- 1));
137 /* only if inner stmts, i.e. with new lines, were dumped */
138 dump_level(fd
, level
);
141 dprintf(fd
, ")%s\n", last
? "" : ",");
147 static inline void dump_typedef(int fd
, decl_arg
*tdef
) {
148 dprintf(fd
, "typedef ");
149 dump_decl_arg(fd
, tdef
);
152 dump_decl_type(fd, tdef->type);
153 dprintf(fd, " %s%s;", tdef->type->type == PSI_T_POINTER ? "*":"",
158 static inline void dump_typedefs(int fd
, decl_typedefs
*defs
) {
161 for (i
= 0; i
< defs
->count
; ++i
) {
162 decl_arg
*tdef
= defs
->list
[i
];
164 dump_typedef(fd
, tdef
);
169 static inline void dump_struct(int fd
, decl_struct
*strct
) {
172 dprintf(fd
, "struct %s::(%zu, %zu)", strct
->name
, strct
->align
, strct
->size
);
173 if (strct
->args
&& strct
->args
->count
) {
175 for (j
= 0; j
< strct
->args
->count
; ++j
) {
176 decl_arg
*sarg
= strct
->args
->args
[j
];
179 dump_decl_arg(fd
, sarg
);
180 dprintf(fd
, "::(%zu, %zu);\n", sarg
->layout
->pos
, sarg
->layout
->len
);
188 static inline void dump_structs(int fd
, decl_structs
*structs
) {
191 for (i
= 0; i
< structs
->count
; ++i
) {
192 decl_struct
*strct
= structs
->list
[i
];
194 dump_struct(fd
, strct
);
199 static inline void dump_union(int fd
, decl_union
*unn
) {
202 dprintf(fd
, "union %s::(%zu, %zu) {\n", unn
->name
, unn
->align
, unn
->size
);
203 for (j
= 0; j
< unn
->args
->count
; ++j
) {
204 decl_arg
*uarg
= unn
->args
->args
[j
];
207 dump_decl_arg(fd
, uarg
);
208 dprintf(fd
, "::(%zu, %zu);\n", uarg
->layout
->pos
, uarg
->layout
->len
);
213 static inline void dump_unions(int fd
, decl_unions
*unions
) {
216 for (i
= 0; i
< unions
->count
; ++i
) {
217 decl_union
*unn
= unions
->list
[i
];
224 static inline void dump_enum(int fd
, decl_enum
*e
) {
227 dprintf(fd
, "enum %s {\n", e
->name
);
228 for (j
= 0; j
< e
->items
->count
; ++j
) {
229 decl_enum_item
*i
= e
->items
->list
[j
];
234 dprintf(fd
, "\t%s", i
->name
);
235 if (i
->num
&& i
->num
!= &i
->inc
) {
237 dump_num_exp(fd
, i
->num
);
243 static inline void dump_enums(int fd
, decl_enums
*enums
) {
246 for (i
= 0; i
< enums
->count
; ++i
) {
247 decl_enum
*e
= enums
->list
[i
];
253 static inline void dump_constant(int fd
, constant
*cnst
) {
254 dprintf(fd
, "const %s %s = ", cnst
->type
->name
, cnst
->name
);
255 if (cnst
->val
->type
== PSI_T_QUOTED_STRING
) {
256 dprintf(fd
, "\"%s\";", cnst
->val
->text
);
258 dprintf(fd
, "%s;", cnst
->val
->text
);
262 static inline void dump_constants(int fd
, constants
*consts
) {
265 for (i
= 0; i
< consts
->count
; ++i
) {
266 constant
*cnst
= consts
->list
[i
];
268 dump_constant(fd
, cnst
);
273 static inline void dump_decl(int fd
, decl
*decl
) {
276 dprintf(fd
, "%s ", decl
->abi
->convention
);
277 dump_decl_arg(fd
, decl
->func
);
280 for (j
= 0; j
< decl
->args
->count
; ++j
) {
284 dump_decl_arg(fd
, decl
->args
->args
[j
]);
286 if (decl
->args
->varargs
) {
287 dprintf(fd
, ", ...");
293 static inline void dump_decls(int fd
, decls
*decls
) {
296 for (i
= 0; i
< decls
->count
; ++i
) {
297 decl
*decl
= decls
->list
[i
];
304 static inline void dump_impl_func(int fd
, impl_func
*func
) {
307 dprintf(fd
, "function %s(", func
->name
);
309 for (j
= 0; j
< func
->args
->count
; ++j
) {
310 impl_arg
*iarg
= func
->args
->args
[j
];
312 dprintf(fd
, "%s%s %s%s",
315 iarg
->var
->reference
? "&" : "",
318 dprintf(fd
, " = %s", iarg
->def
->text
);
321 if (func
->args
->vararg
.name
) {
322 impl_arg
*vararg
= func
->args
->vararg
.name
;
324 dprintf(fd
, ", %s %s...%s",
326 vararg
->var
->reference
? "&" : "",
330 dprintf(fd
, ") : %s%s",
331 func
->return_reference
? "&":"",
332 func
->return_type
->name
);
335 static inline void dump_impl_let_stmt(int fd
, let_stmt
*let
) {
336 dprintf(fd
, "\tlet %s", let
->var
->name
);
338 dprintf(fd
, " = %s", let
->val
->flags
.one
.is_reference
? "&" : "");
339 switch (let
->val
->kind
) {
344 dump_decl_var(fd
, let
->val
->data
.var
);
347 dprintf(fd
, "calloc(");
348 dump_num_exp(fd
, let
->val
->data
.alloc
->nmemb
);
350 dump_num_exp(fd
, let
->val
->data
.alloc
->size
);
353 case PSI_LET_CALLBACK
:
354 dprintf(fd
, "callback %s(%s(", let
->val
->data
.callback
->func
->name
,
355 let
->val
->data
.callback
->func
->var
->name
);
356 if (let
->val
->data
.callback
->args
) {
357 size_t i
, c
= let
->val
->data
.callback
->args
->count
;
360 for (i
= 0; i
< c
; ++i
) {
361 set_value
*set
= let
->val
->data
.callback
->args
->vals
[i
];
362 dump_impl_set_value(fd
, set
, 2, i
+ 1 == c
);
369 dprintf(fd
, "%s(%s)", let
->val
->data
.func
->name
,
370 let
->val
->data
.func
->var
->name
);
373 dump_num_exp(fd
, let
->val
->data
.num
);
376 EMPTY_SWITCH_DEFAULT_CASE();
382 static inline void dump_impl_return_stmt(int fd
, return_stmt
*ret
) {
383 dprintf(fd
, "\treturn ");
384 dump_impl_set_value(fd
, ret
->set
, 1, 0);
387 static inline void dump_impl_set_stmt(int fd
, set_stmt
*set
) {
388 dprintf(fd
, "\tset %s = ", set
->var
->name
);
389 dump_impl_set_value(fd
, set
->val
, 1, 0);
392 static inline void dump_impl_free_call(int fd
, free_call
*call
) {
395 dprintf(fd
, "%s(", call
->func
);
396 for (l
= 0; l
< call
->vars
->count
; ++l
) {
397 decl_var
*fvar
= call
->vars
->vars
[l
];
399 dump_decl_var(fd
, fvar
);
404 static inline void dump_impl_free_stmt(int fd
, free_stmt
*fre
) {
407 dprintf(fd
, "\tfree ");
408 for (k
= 0; k
< fre
->calls
->count
; ++k
) {
409 free_call
*call
= fre
->calls
->list
[k
];
414 dump_impl_free_call(fd
, call
);
418 static inline void dump_impl_stmts(int fd
, impl_stmts
*stmts
) {
421 for (j
= 0; j
< stmts
->let
.count
; ++j
) {
422 let_stmt
*let
= stmts
->let
.list
[j
];
423 dump_impl_let_stmt(fd
, let
);
426 for (j
= 0; j
< stmts
->ret
.count
; ++j
) {
427 return_stmt
*ret
= stmts
->ret
.list
[j
];
428 dump_impl_return_stmt(fd
, ret
);
431 for (j
= 0; j
< stmts
->set
.count
; ++j
) {
432 set_stmt
*set
= stmts
->set
.list
[j
];
434 dump_impl_set_stmt(fd
, set
);
437 for (j
= 0; j
< stmts
->fre
.count
; ++j
) {
438 free_stmt
*fre
= stmts
->fre
.list
[j
];
440 dump_impl_free_stmt(fd
, fre
);
445 static inline void dump_impl(int fd
, impl
*impl
) {
447 dump_impl_func(fd
, impl
->func
);
450 dump_impl_stmts(fd
, impl
->stmts
);
455 static inline void dump_impls(int fd
, impls
*impls
) {
458 for (i
= 0; i
< impls
->count
; ++i
) {
459 impl
*impl
= impls
->list
[i
];
466 void PSI_ContextDump(PSI_Context
*C
, int fd
)
469 if (C
->ops
== PSI_Libjit()) {
470 dprintf(fd
, "// psi.engine=jit\n");
474 if (C
->ops
== PSI_Libffi()) {
475 dprintf(fd
, "// psi.engine=ffi\n");
481 dump_typedefs(fd
, C
->defs
);
485 dump_unions(fd
, C
->unions
);
489 dump_structs(fd
, C
->structs
);
493 dump_enums(fd
, C
->enums
);
497 dump_constants(fd
, C
->consts
);
501 dump_decls(fd
, C
->decls
);
505 dump_impls(fd
, C
->impls
);