7 static inline void dump_decl_type(int fd
, decl_type
*t
) {
17 dprintf(fd
, "%s%s", pre
, t
->name
);
20 static inline void dump_decl_var(int fd
, decl_var
*v
) {
21 dprintf(fd
, "%.*s%s", v
->pointer_level
-!!v
->array_size
, "**********", v
->name
);
23 dprintf(fd
, "[%u]", v
->array_size
);
27 static inline void dump_decl_arg(int fd
, decl_arg
*a
) {
28 dump_decl_type(fd
, a
->type
);
30 dump_decl_var(fd
, a
->var
);
33 static inline void dump_level(int fd
, unsigned level
) {
34 dprintf(fd
, "%.*s", level
> 10 ? 10 : level
, "\t\t\t\t\t\t\t\t\t\t");
37 static inline void dump_num_exp(int fd
, num_exp
*exp
) {
41 dprintf(fd
, "%s", exp
->u
.numb
);
44 dprintf(fd
, "%s", exp
->u
.cnst
->name
);
47 dump_decl_var(fd
, exp
->u
.dvar
);
50 dprintf(fd
, "%s", exp
->u
.enm
->name
);
52 EMPTY_SWITCH_DEFAULT_CASE();
57 switch (exp
->operator) {
58 case PSI_T_PLUS
: op
= '+'; break;
59 case PSI_T_MINUS
: op
= '-'; break;
60 case PSI_T_ASTERISK
:op
= '*'; break;
61 case PSI_T_SLASH
: op
= '/'; break;
62 EMPTY_SWITCH_DEFAULT_CASE();
64 dprintf(fd
, " %c ", op
);
70 static inline void dump_impl_set_value(int fd
, set_value
*set
, unsigned level
, int last
) {
74 /* only if not directly after `set ...` */
75 dump_level(fd
, level
);
78 if (set
->func
->type
== PSI_T_ELLIPSIS
) {
79 dprintf(fd
, "%s(", set
->outer
.set
->func
->name
);
81 dprintf(fd
, "%s(", set
->func
->name
);
84 for (i
= 0; i
< set
->vars
->count
; ++i
) {
85 decl_var
*svar
= set
->vars
->vars
[i
];
89 dump_decl_var(fd
, svar
);
92 if (set
->func
->type
== PSI_T_ELLIPSIS
) {
97 dump_num_exp(fd
, set
->num
);
99 if (set
->inner
&& set
->func
->type
!= PSI_T_ELLIPSIS
) {
101 for (i
= 0; i
< set
->count
; ++i
) {
102 dump_impl_set_value(fd
, set
->inner
[i
], level
+1, i
== (set
->count
- 1));
104 /* only if inner stmts, i.e. with new lines, were dumped */
105 dump_level(fd
, level
);
108 dprintf(fd
, ")%s\n", last
? "" : ",");
114 static inline void dump_typedef(int fd
, decl_arg
*tdef
) {
115 dprintf(fd
, "typedef ");
116 dump_decl_arg(fd
, tdef
);
118 dump_decl_type(fd, tdef->type);
119 dprintf(fd, " %s%s;", tdef->type->type == PSI_T_POINTER ? "*":"",
124 static inline void dump_typedefs(int fd
, decl_typedefs
*defs
) {
127 for (i
= 0; i
< defs
->count
; ++i
) {
128 decl_arg
*tdef
= defs
->list
[i
];
130 dump_typedef(fd
, tdef
);
135 static inline void dump_struct(int fd
, decl_struct
*strct
) {
138 dprintf(fd
, "struct %s::(%zu)", strct
->name
, strct
->size
);
139 if (strct
->args
&& strct
->args
->count
) {
141 for (j
= 0; j
< strct
->args
->count
; ++j
) {
142 decl_arg
*sarg
= strct
->args
->args
[j
];
145 dump_decl_arg(fd
, sarg
);
146 dprintf(fd
, "::(%zu, %zu);\n", sarg
->layout
->pos
, sarg
->layout
->len
);
154 static inline void dump_structs(int fd
, decl_structs
*structs
) {
157 for (i
= 0; i
< structs
->count
; ++i
) {
158 decl_struct
*strct
= structs
->list
[i
];
160 dump_struct(fd
, strct
);
165 static inline void dump_enum(int fd
, decl_enum
*e
) {
168 dprintf(fd
, "enum %s {\n", e
->name
);
169 for (j
= 0; j
< e
->items
->count
; ++j
) {
170 decl_enum_item
*i
= e
->items
->list
[j
];
175 dprintf(fd
, "\t%s", i
->name
);
176 if (i
->num
&& i
->num
!= &i
->inc
) {
178 dump_num_exp(fd
, i
->num
);
184 static inline void dump_enums(int fd
, decl_enums
*enums
) {
187 for (i
= 0; i
< enums
->count
; ++i
) {
188 decl_enum
*e
= enums
->list
[i
];
194 static inline void dump_constant(int fd
, constant
*cnst
) {
195 dprintf(fd
, "const %s %s = ", cnst
->type
->name
, cnst
->name
);
196 if (cnst
->val
->type
== PSI_T_QUOTED_STRING
) {
197 dprintf(fd
, "\"%s\";", cnst
->val
->text
);
199 dprintf(fd
, "%s;", cnst
->val
->text
);
203 static inline void dump_constants(int fd
, constants
*consts
) {
206 for (i
= 0; i
< consts
->count
; ++i
) {
207 constant
*cnst
= consts
->list
[i
];
209 dump_constant(fd
, cnst
);
214 static inline void dump_decl(int fd
, decl
*decl
) {
217 dprintf(fd
, "%s ", decl
->abi
->convention
);
218 dump_decl_arg(fd
, decl
->func
);
221 for (j
= 0; j
< decl
->args
->count
; ++j
) {
225 dump_decl_arg(fd
, decl
->args
->args
[j
]);
227 if (decl
->args
->varargs
) {
228 dprintf(fd
, ", ...");
234 static inline void dump_decls(int fd
, decls
*decls
) {
237 for (i
= 0; i
< decls
->count
; ++i
) {
238 decl
*decl
= decls
->list
[i
];
245 static inline void dump_impl_func(int fd
, impl_func
*func
) {
248 dprintf(fd
, "function %s(", func
->name
);
250 for (j
= 0; j
< func
->args
->count
; ++j
) {
251 impl_arg
*iarg
= func
->args
->args
[j
];
253 dprintf(fd
, "%s%s %s$%s",
256 iarg
->var
->reference
? "&" : "",
259 dprintf(fd
, " = %s", iarg
->def
->text
);
262 if (func
->args
->vararg
.name
) {
263 impl_arg
*vararg
= func
->args
->vararg
.name
;
265 dprintf(fd
, ", %s %s...$%s",
267 vararg
->var
->reference
? "&" : "",
271 dprintf(fd
, ") : %s%s",
272 func
->return_reference
? "&":"",
273 func
->return_type
->name
);
276 static inline void dump_impl_let_stmt(int fd
, let_stmt
*let
) {
277 dprintf(fd
, "\tlet %s", let
->var
->name
);
279 dprintf(fd
, " = %s", let
->val
->flags
.one
.is_reference
? "&" : "");
280 switch (let
->val
->kind
) {
285 dump_decl_var(fd
, let
->val
->data
.var
);
288 dprintf(fd
, "calloc(");
289 dump_num_exp(fd
, let
->val
->data
.alloc
->nmemb
);
291 dump_num_exp(fd
, let
->val
->data
.alloc
->size
);
295 dprintf(fd
, "%s($%s)", let
->val
->data
.func
->name
,
296 let
->val
->data
.func
->var
->name
);
299 dump_num_exp(fd
, let
->val
->data
.num
);
302 EMPTY_SWITCH_DEFAULT_CASE();
308 static inline void dump_impl_return_stmt(int fd
, return_stmt
*ret
) {
309 dprintf(fd
, "\treturn ");
310 dump_impl_set_value(fd
, ret
->set
, 1, 0);
313 static inline void dump_impl_set_stmt(int fd
, set_stmt
*set
) {
314 dprintf(fd
, "\tset $%s = ", set
->var
->name
);
315 dump_impl_set_value(fd
, set
->val
, 1, 0);
318 static inline void dump_impl_free_call(int fd
, free_call
*call
) {
321 dprintf(fd
, "%s(", call
->func
);
322 for (l
= 0; l
< call
->vars
->count
; ++l
) {
323 decl_var
*fvar
= call
->vars
->vars
[l
];
325 dump_decl_var(fd
, fvar
);
330 static inline void dump_impl_free_stmt(int fd
, free_stmt
*fre
) {
333 dprintf(fd
, "\tfree ");
334 for (k
= 0; k
< fre
->calls
->count
; ++k
) {
335 free_call
*call
= fre
->calls
->list
[k
];
340 dump_impl_free_call(fd
, call
);
344 static inline void dump_impl_stmts(int fd
, impl_stmts
*stmts
) {
347 for (j
= 0; j
< stmts
->let
.count
; ++j
) {
348 let_stmt
*let
= stmts
->let
.list
[j
];
349 dump_impl_let_stmt(fd
, let
);
352 for (j
= 0; j
< stmts
->ret
.count
; ++j
) {
353 return_stmt
*ret
= stmts
->ret
.list
[j
];
354 dump_impl_return_stmt(fd
, ret
);
357 for (j
= 0; j
< stmts
->set
.count
; ++j
) {
358 set_stmt
*set
= stmts
->set
.list
[j
];
360 dump_impl_set_stmt(fd
, set
);
363 for (j
= 0; j
< stmts
->fre
.count
; ++j
) {
364 free_stmt
*fre
= stmts
->fre
.list
[j
];
366 dump_impl_free_stmt(fd
, fre
);
371 static inline void dump_impl(int fd
, impl
*impl
) {
373 dump_impl_func(fd
, impl
->func
);
376 dump_impl_stmts(fd
, impl
->stmts
);
381 static inline void dump_impls(int fd
, impls
*impls
) {
384 for (i
= 0; i
< impls
->count
; ++i
) {
385 impl
*impl
= impls
->list
[i
];
392 void PSI_ContextDump(PSI_Context
*C
, int fd
)
395 if (C
->ops
== PSI_Libjit()) {
396 dprintf(fd
, "// psi.engine=jit\n");
400 if (C
->ops
== PSI_Libffi()) {
401 dprintf(fd
, "// psi.engine=ffi\n");
407 dump_typedefs(fd
, C
->defs
);
412 dump_structs(fd
, C
->structs
);
416 dump_enums(fd
, C
->enums
);
420 dump_constants(fd
, C
->consts
);
424 dump_decls(fd
, C
->decls
);
428 dump_impls(fd
, C
->impls
);