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
);
61 dump_decl_var(fd
, a
->var
);
64 static inline void dump_level(int fd
, unsigned level
) {
65 dprintf(fd
, "%.*s", level
> 10 ? 10 : level
, "\t\t\t\t\t\t\t\t\t\t");
68 static inline void dump_num_exp(int fd
, num_exp
*exp
) {
72 dprintf(fd
, "%s", exp
->u
.numb
);
75 dprintf(fd
, "%s", exp
->u
.cnst
->name
);
78 dump_decl_var(fd
, exp
->u
.dvar
);
81 dprintf(fd
, "%s", exp
->u
.enm
->name
);
83 EMPTY_SWITCH_DEFAULT_CASE();
88 switch (exp
->operator) {
89 case PSI_T_PLUS
: op
= '+'; break;
90 case PSI_T_MINUS
: op
= '-'; break;
91 case PSI_T_ASTERISK
:op
= '*'; break;
92 case PSI_T_SLASH
: op
= '/'; break;
93 EMPTY_SWITCH_DEFAULT_CASE();
95 dprintf(fd
, " %c ", op
);
101 static inline void dump_impl_set_value(int fd
, set_value
*set
, unsigned level
, int last
) {
105 /* only if not directly after `set ...` */
106 dump_level(fd
, level
);
109 if (set
->func
->type
== PSI_T_ELLIPSIS
) {
110 dprintf(fd
, "%s(", set
->outer
.set
->func
->name
);
112 dprintf(fd
, "%s(", set
->func
->name
);
115 for (i
= 0; i
< set
->vars
->count
; ++i
) {
116 decl_var
*svar
= set
->vars
->vars
[i
];
120 dump_decl_var(fd
, svar
);
123 if (set
->func
->type
== PSI_T_ELLIPSIS
) {
124 dprintf(fd
, ", ...");
128 dump_num_exp(fd
, set
->num
);
130 if (set
->inner
&& set
->inner
->vals
&& set
->func
->type
!= PSI_T_ELLIPSIS
) {
132 for (i
= 0; i
< set
->inner
->count
; ++i
) {
133 dump_impl_set_value(fd
, set
->inner
->vals
[i
], level
+1, i
== (set
->inner
->count
- 1));
135 /* only if inner stmts, i.e. with new lines, were dumped */
136 dump_level(fd
, level
);
139 dprintf(fd
, ")%s\n", last
? "" : ",");
145 static inline void dump_typedef(int fd
, decl_arg
*tdef
) {
146 dprintf(fd
, "typedef ");
147 dump_decl_arg(fd
, tdef
);
150 dump_decl_type(fd, tdef->type);
151 dprintf(fd, " %s%s;", tdef->type->type == PSI_T_POINTER ? "*":"",
156 static inline void dump_typedefs(int fd
, decl_typedefs
*defs
) {
159 for (i
= 0; i
< defs
->count
; ++i
) {
160 decl_arg
*tdef
= defs
->list
[i
];
162 dump_typedef(fd
, tdef
);
167 static inline void dump_struct(int fd
, decl_struct
*strct
) {
170 dprintf(fd
, "struct %s::(%zu, %zu)", strct
->name
, strct
->align
, strct
->size
);
171 if (strct
->args
&& strct
->args
->count
) {
173 for (j
= 0; j
< strct
->args
->count
; ++j
) {
174 decl_arg
*sarg
= strct
->args
->args
[j
];
177 dump_decl_arg(fd
, sarg
);
178 dprintf(fd
, "::(%zu, %zu);\n", sarg
->layout
->pos
, sarg
->layout
->len
);
186 static inline void dump_structs(int fd
, decl_structs
*structs
) {
189 for (i
= 0; i
< structs
->count
; ++i
) {
190 decl_struct
*strct
= structs
->list
[i
];
192 dump_struct(fd
, strct
);
197 static inline void dump_union(int fd
, decl_union
*unn
) {
200 dprintf(fd
, "union %s::(%zu, %zu) {\n", unn
->name
, unn
->align
, unn
->size
);
201 for (j
= 0; j
< unn
->args
->count
; ++j
) {
202 decl_arg
*uarg
= unn
->args
->args
[j
];
205 dump_decl_arg(fd
, uarg
);
206 dprintf(fd
, "::(%zu, %zu);\n", uarg
->layout
->pos
, uarg
->layout
->len
);
211 static inline void dump_unions(int fd
, decl_unions
*unions
) {
214 for (i
= 0; i
< unions
->count
; ++i
) {
215 decl_union
*unn
= unions
->list
[i
];
222 static inline void dump_enum(int fd
, decl_enum
*e
) {
225 dprintf(fd
, "enum %s {\n", e
->name
);
226 for (j
= 0; j
< e
->items
->count
; ++j
) {
227 decl_enum_item
*i
= e
->items
->list
[j
];
232 dprintf(fd
, "\t%s", i
->name
);
233 if (i
->num
&& i
->num
!= &i
->inc
) {
235 dump_num_exp(fd
, i
->num
);
241 static inline void dump_enums(int fd
, decl_enums
*enums
) {
244 for (i
= 0; i
< enums
->count
; ++i
) {
245 decl_enum
*e
= enums
->list
[i
];
251 static inline void dump_constant(int fd
, constant
*cnst
) {
252 dprintf(fd
, "const %s %s = ", cnst
->type
->name
, cnst
->name
);
253 if (cnst
->val
->type
== PSI_T_QUOTED_STRING
) {
254 dprintf(fd
, "\"%s\";", cnst
->val
->text
);
256 dprintf(fd
, "%s;", cnst
->val
->text
);
260 static inline void dump_constants(int fd
, constants
*consts
) {
263 for (i
= 0; i
< consts
->count
; ++i
) {
264 constant
*cnst
= consts
->list
[i
];
266 dump_constant(fd
, cnst
);
271 static inline void dump_decl(int fd
, decl
*decl
) {
274 dprintf(fd
, "%s ", decl
->abi
->convention
);
275 dump_decl_arg(fd
, decl
->func
);
278 for (j
= 0; j
< decl
->args
->count
; ++j
) {
282 dump_decl_arg(fd
, decl
->args
->args
[j
]);
284 if (decl
->args
->varargs
) {
285 dprintf(fd
, ", ...");
291 static inline void dump_decls(int fd
, decls
*decls
) {
294 for (i
= 0; i
< decls
->count
; ++i
) {
295 decl
*decl
= decls
->list
[i
];
302 static inline void dump_impl_func(int fd
, impl_func
*func
) {
305 dprintf(fd
, "function %s(", func
->name
);
307 for (j
= 0; j
< func
->args
->count
; ++j
) {
308 impl_arg
*iarg
= func
->args
->args
[j
];
310 dprintf(fd
, "%s%s %s$%s",
313 iarg
->var
->reference
? "&" : "",
316 dprintf(fd
, " = %s", iarg
->def
->text
);
319 if (func
->args
->vararg
.name
) {
320 impl_arg
*vararg
= func
->args
->vararg
.name
;
322 dprintf(fd
, ", %s %s...$%s",
324 vararg
->var
->reference
? "&" : "",
328 dprintf(fd
, ") : %s%s",
329 func
->return_reference
? "&":"",
330 func
->return_type
->name
);
333 static inline void dump_impl_let_stmt(int fd
, let_stmt
*let
) {
334 dprintf(fd
, "\tlet %s", let
->var
->name
);
336 dprintf(fd
, " = %s", let
->val
->flags
.one
.is_reference
? "&" : "");
337 switch (let
->val
->kind
) {
342 dump_decl_var(fd
, let
->val
->data
.var
);
345 dprintf(fd
, "calloc(");
346 dump_num_exp(fd
, let
->val
->data
.alloc
->nmemb
);
348 dump_num_exp(fd
, let
->val
->data
.alloc
->size
);
352 dprintf(fd
, "%s($%s)", let
->val
->data
.func
->name
,
353 let
->val
->data
.func
->var
->name
);
356 dump_num_exp(fd
, let
->val
->data
.num
);
359 EMPTY_SWITCH_DEFAULT_CASE();
365 static inline void dump_impl_return_stmt(int fd
, return_stmt
*ret
) {
366 dprintf(fd
, "\treturn ");
367 dump_impl_set_value(fd
, ret
->set
, 1, 0);
370 static inline void dump_impl_set_stmt(int fd
, set_stmt
*set
) {
371 dprintf(fd
, "\tset $%s = ", set
->var
->name
);
372 dump_impl_set_value(fd
, set
->val
, 1, 0);
375 static inline void dump_impl_free_call(int fd
, free_call
*call
) {
378 dprintf(fd
, "%s(", call
->func
);
379 for (l
= 0; l
< call
->vars
->count
; ++l
) {
380 decl_var
*fvar
= call
->vars
->vars
[l
];
382 dump_decl_var(fd
, fvar
);
387 static inline void dump_impl_free_stmt(int fd
, free_stmt
*fre
) {
390 dprintf(fd
, "\tfree ");
391 for (k
= 0; k
< fre
->calls
->count
; ++k
) {
392 free_call
*call
= fre
->calls
->list
[k
];
397 dump_impl_free_call(fd
, call
);
401 static inline void dump_impl_stmts(int fd
, impl_stmts
*stmts
) {
404 for (j
= 0; j
< stmts
->let
.count
; ++j
) {
405 let_stmt
*let
= stmts
->let
.list
[j
];
406 dump_impl_let_stmt(fd
, let
);
409 for (j
= 0; j
< stmts
->ret
.count
; ++j
) {
410 return_stmt
*ret
= stmts
->ret
.list
[j
];
411 dump_impl_return_stmt(fd
, ret
);
414 for (j
= 0; j
< stmts
->set
.count
; ++j
) {
415 set_stmt
*set
= stmts
->set
.list
[j
];
417 dump_impl_set_stmt(fd
, set
);
420 for (j
= 0; j
< stmts
->fre
.count
; ++j
) {
421 free_stmt
*fre
= stmts
->fre
.list
[j
];
423 dump_impl_free_stmt(fd
, fre
);
428 static inline void dump_impl(int fd
, impl
*impl
) {
430 dump_impl_func(fd
, impl
->func
);
433 dump_impl_stmts(fd
, impl
->stmts
);
438 static inline void dump_impls(int fd
, impls
*impls
) {
441 for (i
= 0; i
< impls
->count
; ++i
) {
442 impl
*impl
= impls
->list
[i
];
449 void PSI_ContextDump(PSI_Context
*C
, int fd
)
452 if (C
->ops
== PSI_Libjit()) {
453 dprintf(fd
, "// psi.engine=jit\n");
457 if (C
->ops
== PSI_Libffi()) {
458 dprintf(fd
, "// psi.engine=ffi\n");
464 dump_typedefs(fd
, C
->defs
);
468 dump_unions(fd
, C
->unions
);
472 dump_structs(fd
, C
->structs
);
476 dump_enums(fd
, C
->enums
);
480 dump_constants(fd
, C
->consts
);
484 dump_decls(fd
, C
->decls
);
488 dump_impls(fd
, C
->impls
);