cb1825f51b773cb4f48cbf87786cca80d04ac4af
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_typedef
*tdef
) {
115 dprintf(fd
, "typedef ");
116 dump_decl_type(fd
, tdef
->type
);
117 dprintf(fd
, " %s%s;", tdef
->type
->type
== PSI_T_POINTER
? "*":"",
121 static inline void dump_typedefs(int fd
, decl_typedefs
*defs
) {
124 for (i
= 0; i
< defs
->count
; ++i
) {
125 decl_typedef
*tdef
= defs
->list
[i
];
127 dump_typedef(fd
, tdef
);
132 static inline void dump_struct(int fd
, decl_struct
*strct
) {
135 dprintf(fd
, "struct %s::(%zu)", strct
->name
, strct
->size
);
136 if (strct
->args
&& strct
->args
->count
) {
138 for (j
= 0; j
< strct
->args
->count
; ++j
) {
139 decl_arg
*sarg
= strct
->args
->args
[j
];
142 dump_decl_arg(fd
, sarg
);
143 dprintf(fd
, "::(%zu, %zu);\n", sarg
->layout
->pos
, sarg
->layout
->len
);
151 static inline void dump_structs(int fd
, decl_structs
*structs
) {
154 for (i
= 0; i
< structs
->count
; ++i
) {
155 decl_struct
*strct
= structs
->list
[i
];
157 dump_struct(fd
, strct
);
162 static inline void dump_enum(int fd
, decl_enum
*e
) {
165 dprintf(fd
, "enum %s {\n", e
->name
);
166 for (j
= 0; j
< e
->items
->count
; ++j
) {
167 decl_enum_item
*i
= e
->items
->list
[j
];
172 dprintf(fd
, "\t%s", i
->name
);
173 if (i
->num
&& i
->num
!= &i
->inc
) {
175 dump_num_exp(fd
, i
->num
);
181 static inline void dump_enums(int fd
, decl_enums
*enums
) {
184 for (i
= 0; i
< enums
->count
; ++i
) {
185 decl_enum
*e
= enums
->list
[i
];
191 static inline void dump_constant(int fd
, constant
*cnst
) {
192 dprintf(fd
, "const %s %s = ", cnst
->type
->name
, cnst
->name
);
193 if (cnst
->val
->type
== PSI_T_QUOTED_STRING
) {
194 dprintf(fd
, "\"%s\";", cnst
->val
->text
);
196 dprintf(fd
, "%s;", cnst
->val
->text
);
200 static inline void dump_constants(int fd
, constants
*consts
) {
203 for (i
= 0; i
< consts
->count
; ++i
) {
204 constant
*cnst
= consts
->list
[i
];
206 dump_constant(fd
, cnst
);
211 static inline void dump_decl(int fd
, decl
*decl
) {
214 dprintf(fd
, "%s ", decl
->abi
->convention
);
215 dump_decl_arg(fd
, decl
->func
);
218 for (j
= 0; j
< decl
->args
->count
; ++j
) {
222 dump_decl_arg(fd
, decl
->args
->args
[j
]);
224 if (decl
->args
->varargs
) {
225 dprintf(fd
, ", ...");
231 static inline void dump_decls(int fd
, decls
*decls
) {
234 for (i
= 0; i
< decls
->count
; ++i
) {
235 decl
*decl
= decls
->list
[i
];
242 static inline void dump_impl_func(int fd
, impl_func
*func
) {
245 dprintf(fd
, "function %s(", func
->name
);
247 for (j
= 0; j
< func
->args
->count
; ++j
) {
248 impl_arg
*iarg
= func
->args
->args
[j
];
250 dprintf(fd
, "%s%s %s$%s",
253 iarg
->var
->reference
? "&" : "",
256 dprintf(fd
, " = %s", iarg
->def
->text
);
259 if (func
->args
->vararg
.name
) {
260 impl_arg
*vararg
= func
->args
->vararg
.name
;
262 dprintf(fd
, ", %s %s...$%s",
264 vararg
->var
->reference
? "&" : "",
268 dprintf(fd
, ") : %s%s",
269 func
->return_reference
? "&":"",
270 func
->return_type
->name
);
273 static inline void dump_impl_let_stmt(int fd
, let_stmt
*let
) {
274 dprintf(fd
, "\tlet %s", let
->var
->name
);
276 dprintf(fd
, " = %s", let
->val
->flags
.one
.is_reference
? "&" : "");
277 switch (let
->val
->kind
) {
282 dump_decl_var(fd
, let
->val
->data
.var
);
285 dprintf(fd
, "calloc(");
286 dump_num_exp(fd
, let
->val
->data
.alloc
->nmemb
);
288 dump_num_exp(fd
, let
->val
->data
.alloc
->size
);
292 dprintf(fd
, "%s($%s)", let
->val
->data
.func
->name
,
293 let
->val
->data
.func
->var
->name
);
296 dump_num_exp(fd
, let
->val
->data
.num
);
299 EMPTY_SWITCH_DEFAULT_CASE();
305 static inline void dump_impl_return_stmt(int fd
, return_stmt
*ret
) {
306 dprintf(fd
, "\treturn ");
307 dump_impl_set_value(fd
, ret
->set
, 1, 0);
310 static inline void dump_impl_set_stmt(int fd
, set_stmt
*set
) {
311 dprintf(fd
, "\tset $%s = ", set
->var
->name
);
312 dump_impl_set_value(fd
, set
->val
, 1, 0);
315 static inline void dump_impl_free_call(int fd
, free_call
*call
) {
318 dprintf(fd
, "%s(", call
->func
);
319 for (l
= 0; l
< call
->vars
->count
; ++l
) {
320 decl_var
*fvar
= call
->vars
->vars
[l
];
322 dump_decl_var(fd
, fvar
);
327 static inline void dump_impl_free_stmt(int fd
, free_stmt
*fre
) {
330 dprintf(fd
, "\tfree ");
331 for (k
= 0; k
< fre
->calls
->count
; ++k
) {
332 free_call
*call
= fre
->calls
->list
[k
];
337 dump_impl_free_call(fd
, call
);
341 static inline void dump_impl_stmts(int fd
, impl_stmts
*stmts
) {
344 for (j
= 0; j
< stmts
->let
.count
; ++j
) {
345 let_stmt
*let
= stmts
->let
.list
[j
];
346 dump_impl_let_stmt(fd
, let
);
349 for (j
= 0; j
< stmts
->ret
.count
; ++j
) {
350 return_stmt
*ret
= stmts
->ret
.list
[j
];
351 dump_impl_return_stmt(fd
, ret
);
354 for (j
= 0; j
< stmts
->set
.count
; ++j
) {
355 set_stmt
*set
= stmts
->set
.list
[j
];
357 dump_impl_set_stmt(fd
, set
);
360 for (j
= 0; j
< stmts
->fre
.count
; ++j
) {
361 free_stmt
*fre
= stmts
->fre
.list
[j
];
363 dump_impl_free_stmt(fd
, fre
);
368 static inline void dump_impl(int fd
, impl
*impl
) {
370 dump_impl_func(fd
, impl
->func
);
373 dump_impl_stmts(fd
, impl
->stmts
);
378 static inline void dump_impls(int fd
, impls
*impls
) {
381 for (i
= 0; i
< impls
->count
; ++i
) {
382 impl
*impl
= impls
->list
[i
];
389 void PSI_ContextDump(PSI_Context
*C
, int fd
)
392 if (C
->ops
== PSI_Libjit()) {
393 dprintf(fd
, "// psi.engine=jit\n");
397 if (C
->ops
== PSI_Libffi()) {
398 dprintf(fd
, "// psi.engine=ffi\n");
404 dump_typedefs(fd
, C
->defs
);
409 dump_structs(fd
, C
->structs
);
413 dump_enums(fd
, C
->enums
);
417 dump_constants(fd
, C
->consts
);
421 dump_decls(fd
, C
->decls
);
425 dump_impls(fd
, C
->impls
);