f1386710aa3eebab1d91cb92a7ad54634b4346ab
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
) {
21 dprintf(fd
, "%s *", t
->name
);
25 dprintf(fd
, "struct ");
26 if (!strncmp(t
->name
, "struct@", sizeof("struct"))) {
27 dump_struct_args(fd
, t
->real
.strct
->args
, level
);
34 if (!strncmp(t
->name
, "enum@", sizeof("enum"))) {
35 dump_enum_items(fd
, t
->real
.enm
->items
, level
);
41 dprintf(fd
, "union ");
42 if (!strncmp(t
->name
, "union@", sizeof("union"))) {
43 dump_struct_args(fd
, t
->real
.unn
->args
, level
);
48 dprintf(fd
, "%s", 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
, unsigned level
) {
59 if (a
->type
->type
== PSI_T_FUNCTION
) {
60 dump_decl_type(fd
, a
->type
->real
.func
->func
->type
, level
);
62 dump_decl_var(fd
, a
->var
);
64 if (a
->type
->real
.func
->args
) {
67 for (j
= 0; j
< a
->type
->real
.func
->args
->count
; ++j
) {
71 dump_decl_arg(fd
, a
->type
->real
.func
->args
->args
[j
], level
+1);
73 if (a
->type
->real
.func
->args
->varargs
) {
79 dump_decl_type(fd
, a
->type
, level
);
81 dump_decl_var(fd
, a
->var
);
85 static inline void dump_num_exp(int fd
, num_exp
*exp
) {
89 dprintf(fd
, "%s", exp
->u
.numb
);
92 dprintf(fd
, "%s", exp
->u
.cnst
->name
);
95 dump_decl_var(fd
, exp
->u
.dvar
);
98 dprintf(fd
, "%s", exp
->u
.enm
->name
);
100 EMPTY_SWITCH_DEFAULT_CASE();
105 switch (exp
->operator) {
106 case PSI_T_PLUS
: op
= '+'; break;
107 case PSI_T_MINUS
: op
= '-'; break;
108 case PSI_T_ASTERISK
:op
= '*'; break;
109 case PSI_T_SLASH
: op
= '/'; break;
110 EMPTY_SWITCH_DEFAULT_CASE();
112 dprintf(fd
, " %c ", op
);
118 static inline void dump_impl_set_value(int fd
, set_value
*set
, unsigned level
, int last
) {
122 /* only if not directly after `set ...` */
123 dump_level(fd
, level
);
126 if (set
->func
->type
== PSI_T_ELLIPSIS
) {
127 dprintf(fd
, "%s(", set
->outer
.set
->func
->name
);
129 dprintf(fd
, "%s(", set
->func
->name
);
132 for (i
= 0; i
< set
->vars
->count
; ++i
) {
133 decl_var
*svar
= set
->vars
->vars
[i
];
137 dump_decl_var(fd
, svar
);
140 if (set
->func
->type
== PSI_T_ELLIPSIS
) {
141 dprintf(fd
, ", ...");
145 dump_num_exp(fd
, set
->num
);
147 if (set
->inner
&& set
->inner
->vals
&& set
->func
->type
!= PSI_T_ELLIPSIS
) {
149 for (i
= 0; i
< set
->inner
->count
; ++i
) {
150 dump_impl_set_value(fd
, set
->inner
->vals
[i
], level
+1, i
== (set
->inner
->count
- 1));
152 /* only if inner stmts, i.e. with new lines, were dumped */
153 dump_level(fd
, level
);
156 dprintf(fd
, ")%s\n", last
? "" : ",");
162 static inline void dump_typedef(int fd
, decl_arg
*tdef
) {
163 dprintf(fd
, "typedef ");
164 dump_decl_arg(fd
, tdef
, 0);
168 static inline void dump_typedefs(int fd
, decl_typedefs
*defs
) {
171 for (i
= 0; i
< defs
->count
; ++i
) {
172 decl_arg
*tdef
= defs
->list
[i
];
174 dump_typedef(fd
, tdef
);
179 static inline void dump_struct_args(int fd
, decl_args
*args
, unsigned level
) {
185 for (j
= 0; j
< args
->count
; ++j
) {
186 decl_arg
*sarg
= args
->args
[j
];
188 dump_level(fd
, level
);
189 dump_decl_arg(fd
, sarg
, level
);
190 dprintf(fd
, "::(%zu, %zu);\n", sarg
->layout
->pos
, sarg
->layout
->len
);
194 dump_level(fd
, level
);
198 static inline void dump_struct(int fd
, decl_struct
*strct
) {
199 dprintf(fd
, "struct %s::(%zu, %zu)", strct
->name
, strct
->align
, strct
->size
);
200 if (strct
->args
&& strct
->args
->count
) {
201 dump_struct_args(fd
, strct
->args
, 0);
207 static inline void dump_structs(int fd
, decl_structs
*structs
) {
210 for (i
= 0; i
< structs
->count
; ++i
) {
211 decl_struct
*strct
= structs
->list
[i
];
213 if (!is_anon_type(strct
->name
, "struct")) {
214 dump_struct(fd
, strct
);
220 static inline void dump_union(int fd
, decl_union
*unn
) {
223 dprintf(fd
, "union %s::(%zu, %zu) {\n", unn
->name
, unn
->align
, unn
->size
);
224 for (j
= 0; j
< unn
->args
->count
; ++j
) {
225 decl_arg
*uarg
= unn
->args
->args
[j
];
228 dump_decl_arg(fd
, uarg
, 0);
229 dprintf(fd
, "::(%zu, %zu);\n", uarg
->layout
->pos
, uarg
->layout
->len
);
234 static inline void dump_unions(int fd
, decl_unions
*unions
) {
237 for (i
= 0; i
< unions
->count
; ++i
) {
238 decl_union
*unn
= unions
->list
[i
];
240 if (!is_anon_type(unn
->name
, "union")) {
247 static inline void dump_enum_items(int fd
, decl_enum_items
*items
, unsigned level
) {
252 for (j
= 0; j
< items
->count
; ++j
) {
253 decl_enum_item
*i
= items
->list
[j
];
258 dump_level(fd
, level
);
259 dprintf(fd
, "%s", i
->name
);
260 if (i
->num
&& i
->num
!= &i
->inc
) {
262 dump_num_exp(fd
, i
->num
);
269 static inline void dump_enum(int fd
, decl_enum
*e
) {
270 dprintf(fd
, "enum %s {\n", e
->name
);
271 dump_enum_items(fd
, e
->items
, 0);
275 static inline void dump_enums(int fd
, decl_enums
*enums
) {
278 for (i
= 0; i
< enums
->count
; ++i
) {
279 decl_enum
*e
= enums
->list
[i
];
281 if (!is_anon_type(e
->name
, "enum")) {
287 static inline void dump_constant(int fd
, constant
*cnst
) {
288 dprintf(fd
, "const %s %s = ", cnst
->type
->name
, cnst
->name
);
289 if (cnst
->val
->type
== PSI_T_QUOTED_STRING
) {
290 dprintf(fd
, "\"%s\";", cnst
->val
->text
);
292 dprintf(fd
, "%s;", cnst
->val
->text
);
296 static inline void dump_constants(int fd
, constants
*consts
) {
299 for (i
= 0; i
< consts
->count
; ++i
) {
300 constant
*cnst
= consts
->list
[i
];
302 dump_constant(fd
, cnst
);
307 static inline void dump_decl(int fd
, decl
*decl
) {
310 dprintf(fd
, "%s ", decl
->abi
->convention
);
311 dump_decl_arg(fd
, decl
->func
, 0);
314 for (j
= 0; j
< decl
->args
->count
; ++j
) {
318 dump_decl_arg(fd
, decl
->args
->args
[j
], 0);
320 if (decl
->args
->varargs
) {
321 dprintf(fd
, ", ...");
327 static inline void dump_decls(int fd
, decls
*decls
) {
330 for (i
= 0; i
< decls
->count
; ++i
) {
331 decl
*decl
= decls
->list
[i
];
338 static inline void dump_impl_func(int fd
, impl_func
*func
) {
341 dprintf(fd
, "function %s(", func
->name
);
343 for (j
= 0; j
< func
->args
->count
; ++j
) {
344 impl_arg
*iarg
= func
->args
->args
[j
];
346 dprintf(fd
, "%s%s %s%s",
349 iarg
->var
->reference
? "&" : "",
352 dprintf(fd
, " = %s", iarg
->def
->text
);
355 if (func
->args
->vararg
.name
) {
356 impl_arg
*vararg
= func
->args
->vararg
.name
;
358 dprintf(fd
, ", %s %s...%s",
360 vararg
->var
->reference
? "&" : "",
364 dprintf(fd
, ") : %s%s",
365 func
->return_reference
? "&":"",
366 func
->return_type
->name
);
369 static inline void dump_impl_let_stmt(int fd
, let_stmt
*let
) {
370 dprintf(fd
, "\tlet %s", let
->var
->name
);
372 dprintf(fd
, " = %s", let
->val
->flags
.one
.is_reference
? "&" : "");
373 switch (let
->val
->kind
) {
378 dump_decl_var(fd
, let
->val
->data
.var
);
381 dprintf(fd
, "calloc(");
382 dump_num_exp(fd
, let
->val
->data
.alloc
->nmemb
);
384 dump_num_exp(fd
, let
->val
->data
.alloc
->size
);
387 case PSI_LET_CALLBACK
:
388 dprintf(fd
, "callback %s(%s(", let
->val
->data
.callback
->func
->name
,
389 let
->val
->data
.callback
->func
->var
->name
);
390 if (let
->val
->data
.callback
->args
) {
391 size_t i
, c
= let
->val
->data
.callback
->args
->count
;
394 for (i
= 0; i
< c
; ++i
) {
395 set_value
*set
= let
->val
->data
.callback
->args
->vals
[i
];
396 dump_impl_set_value(fd
, set
, 2, i
+ 1 == c
);
403 dprintf(fd
, "%s(%s)", let
->val
->data
.func
->name
,
404 let
->val
->data
.func
->var
->name
);
407 dump_num_exp(fd
, let
->val
->data
.num
);
410 EMPTY_SWITCH_DEFAULT_CASE();
416 static inline void dump_impl_return_stmt(int fd
, return_stmt
*ret
) {
417 dprintf(fd
, "\treturn ");
418 dump_impl_set_value(fd
, ret
->set
, 1, 0);
421 static inline void dump_impl_set_stmt(int fd
, set_stmt
*set
) {
422 dprintf(fd
, "\tset %s = ", set
->var
->name
);
423 dump_impl_set_value(fd
, set
->val
, 1, 0);
426 static inline void dump_impl_free_call(int fd
, free_call
*call
) {
429 dprintf(fd
, "%s(", call
->func
);
430 for (l
= 0; l
< call
->vars
->count
; ++l
) {
431 decl_var
*fvar
= call
->vars
->vars
[l
];
433 dump_decl_var(fd
, fvar
);
438 static inline void dump_impl_free_stmt(int fd
, free_stmt
*fre
) {
441 dprintf(fd
, "\tfree ");
442 for (k
= 0; k
< fre
->calls
->count
; ++k
) {
443 free_call
*call
= fre
->calls
->list
[k
];
448 dump_impl_free_call(fd
, call
);
452 static inline void dump_impl_stmts(int fd
, impl_stmts
*stmts
) {
455 for (j
= 0; j
< stmts
->let
.count
; ++j
) {
456 let_stmt
*let
= stmts
->let
.list
[j
];
457 dump_impl_let_stmt(fd
, let
);
460 for (j
= 0; j
< stmts
->ret
.count
; ++j
) {
461 return_stmt
*ret
= stmts
->ret
.list
[j
];
462 dump_impl_return_stmt(fd
, ret
);
465 for (j
= 0; j
< stmts
->set
.count
; ++j
) {
466 set_stmt
*set
= stmts
->set
.list
[j
];
468 dump_impl_set_stmt(fd
, set
);
471 for (j
= 0; j
< stmts
->fre
.count
; ++j
) {
472 free_stmt
*fre
= stmts
->fre
.list
[j
];
474 dump_impl_free_stmt(fd
, fre
);
479 static inline void dump_impl(int fd
, impl
*impl
) {
481 dump_impl_func(fd
, impl
->func
);
484 dump_impl_stmts(fd
, impl
->stmts
);
489 static inline void dump_impls(int fd
, impls
*impls
) {
492 for (i
= 0; i
< impls
->count
; ++i
) {
493 impl
*impl
= impls
->list
[i
];
500 void PSI_ContextDump(PSI_Context
*C
, int fd
)
503 if (C
->ops
== PSI_Libjit()) {
504 dprintf(fd
, "// psi.engine=jit\n");
508 if (C
->ops
== PSI_Libffi()) {
509 dprintf(fd
, "// psi.engine=ffi\n");
515 dump_typedefs(fd
, C
->defs
);
519 dump_unions(fd
, C
->unions
);
523 dump_structs(fd
, C
->structs
);
527 dump_enums(fd
, C
->enums
);
531 dump_constants(fd
, C
->consts
);
535 dump_decls(fd
, C
->decls
);
539 dump_impls(fd
, C
->impls
);