functor types
[m6w6/ext-psi] / src / context_dump.c
1 #ifdef HAVE_CONFIG_H
2 # include "config.h"
3 #endif
4
5 #include "php.h"
6 #include "php_psi.h"
7
8 #include "types.h"
9
10 #include "libjit.h"
11 #include "libffi.h"
12
13 static inline void dump_level(int fd, unsigned level) {
14 dprintf(fd, "%.*s", level > 30 ? 30 : level,
15 "\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");
16 }
17
18 static inline void dump_decl_arg(int fd, decl_arg *darg, unsigned level);
19 static inline void dump_struct_args(int fd, decl_args *args, unsigned level);
20 static inline void dump_enum_items(int fd, decl_enum_items *items, unsigned level);
21
22 #define is_anon_type(name, type) !strncmp(name, type "@", sizeof(type))
23
24 static inline void dump_decl_type(int fd, decl_type *t, unsigned level) {
25 switch (t->type) {
26 case PSI_T_POINTER:
27 dprintf(fd, "%s *", t->name);
28 return;
29
30 case PSI_T_STRUCT:
31 dprintf(fd, "struct ");
32 if (!strncmp(t->name, "struct@", sizeof("struct"))) {
33 dump_struct_args(fd, t->real.strct->args, level);
34 return;
35 }
36 break;
37
38 case PSI_T_ENUM:
39 dprintf(fd, "enum ");
40 if (!strncmp(t->name, "enum@", sizeof("enum"))) {
41 dump_enum_items(fd, t->real.enm->items, level);
42 return;
43 }
44 break;
45
46 case PSI_T_UNION:
47 dprintf(fd, "union ");
48 if (!strncmp(t->name, "union@", sizeof("union"))) {
49 dump_struct_args(fd, t->real.unn->args, level);
50 return;
51 }
52 break;
53 }
54 dprintf(fd, "%s", t->name);
55 }
56
57 static inline void dump_decl_var(int fd, decl_var *v) {
58 dprintf(fd, "%.*s%s", v->pointer_level-!!v->array_size, "**********", v->name);
59 if (v->array_size) {
60 dprintf(fd, "[%u]", v->array_size);
61 }
62 }
63
64 static inline void dump_decl_arg(int fd, decl_arg *a, unsigned level) {
65 if (a->type->type == PSI_T_FUNCTION) {
66 dump_decl_type(fd, a->type->real.func->func->type, level);
67 dprintf(fd, " (");
68 dump_decl_var(fd, a->var);
69 dprintf(fd, ")(");
70 if (a->type->real.func->args) {
71 size_t j;
72
73 for (j = 0; j < a->type->real.func->args->count; ++j) {
74 if (j) {
75 dprintf(fd, ", ");
76 }
77 dump_decl_arg(fd, a->type->real.func->args->args[j], level+1);
78 }
79 if (a->type->real.func->args->varargs) {
80 dprintf(fd, ", ...");
81 }
82 }
83 dprintf(fd, ")");
84 } else {
85 dump_decl_type(fd, a->type, level);
86 dprintf(fd, " ");
87 dump_decl_var(fd, a->var);
88 }
89 }
90
91 static inline void dump_num_exp(int fd, num_exp *exp) {
92 while (exp) {
93 switch (exp->t) {
94 case PSI_T_NUMBER:
95 dprintf(fd, "%s", exp->u.numb);
96 break;
97 case PSI_T_NSNAME:
98 dprintf(fd, "%s", exp->u.cnst->name);
99 break;
100 case PSI_T_NAME:
101 dump_decl_var(fd, exp->u.dvar);
102 break;
103 case PSI_T_ENUM:
104 dprintf(fd, "%s", exp->u.enm->name);
105 break;
106 EMPTY_SWITCH_DEFAULT_CASE();
107 }
108 if (exp->operand) {
109 char op;
110
111 switch (exp->operator) {
112 case PSI_T_PLUS: op = '+'; break;
113 case PSI_T_MINUS: op = '-'; break;
114 case PSI_T_ASTERISK:op = '*'; break;
115 case PSI_T_SLASH: op = '/'; break;
116 EMPTY_SWITCH_DEFAULT_CASE();
117 }
118 dprintf(fd, " %c ", op);
119 }
120 exp = exp->operand;
121 }
122 }
123
124 static inline void dump_impl_set_value(int fd, set_value *set, unsigned level, int last) {
125 size_t i;
126
127 if (level > 1) {
128 /* only if not directly after `set ...` */
129 dump_level(fd, level);
130 }
131
132 if (set->func->type == PSI_T_ELLIPSIS) {
133 dprintf(fd, "%s(", set->outer.set->func->name);
134 } else {
135 dprintf(fd, "%s(", set->func->name);
136 }
137
138 for (i = 0; i < set->vars->count; ++i) {
139 decl_var *svar = set->vars->vars[i];
140 if (i) {
141 dprintf(fd, ", ");
142 }
143 dump_decl_var(fd, svar);
144 }
145
146 if (set->func->type == PSI_T_ELLIPSIS) {
147 dprintf(fd, ", ...");
148 }
149 if (set->num) {
150 dprintf(fd, ", ");
151 dump_num_exp(fd, set->num);
152 }
153 if (set->inner && set->inner->vals && set->func->type != PSI_T_ELLIPSIS) {
154 dprintf(fd, ",\n");
155 for (i = 0; i < set->inner->count; ++i) {
156 dump_impl_set_value(fd, set->inner->vals[i], level+1, i == (set->inner->count - 1));
157 }
158 /* only if inner stmts, i.e. with new lines, were dumped */
159 dump_level(fd, level);
160 }
161 if (level > 1) {
162 dprintf(fd, ")%s\n", last ? "" : ",");
163 } else {
164 dprintf(fd, ");");
165 }
166 }
167
168 static inline void dump_typedef(int fd, decl_arg *tdef) {
169 dprintf(fd, "typedef ");
170 dump_decl_arg(fd, tdef, 0);
171 dprintf(fd, ";");
172 }
173
174 static inline void dump_typedefs(int fd, decl_typedefs *defs) {
175 size_t i;
176
177 for (i = 0; i < defs->count; ++i) {
178 decl_arg *tdef = defs->list[i];
179
180 dump_typedef(fd, tdef);
181 dprintf(fd, "\n");
182 }
183 }
184
185 static inline void dump_struct_args(int fd, decl_args *args, unsigned level) {
186 size_t j;
187
188 dprintf(fd, " {\n");
189 if (args) {
190 ++level;
191 for (j = 0; j < args->count; ++j) {
192 decl_arg *sarg = args->args[j];
193
194 dump_level(fd, level);
195 dump_decl_arg(fd, sarg, level);
196 dprintf(fd, "::(%zu, %zu);\n", sarg->layout->pos, sarg->layout->len);
197 }
198 --level;
199 }
200 dump_level(fd, level);
201 dprintf(fd, "}");
202 }
203
204 static inline void dump_struct(int fd, decl_struct *strct) {
205 dprintf(fd, "struct %s::(%zu, %zu)", strct->name, strct->align, strct->size);
206 if (strct->args && strct->args->count) {
207 dump_struct_args(fd, strct->args, 0);
208 } else {
209 dprintf(fd, ";");
210 }
211 }
212
213 static inline void dump_structs(int fd, decl_structs *structs) {
214 size_t i;
215
216 for (i = 0; i < structs->count; ++i) {
217 decl_struct *strct = structs->list[i];
218
219 if (!is_anon_type(strct->name, "struct")) {
220 dump_struct(fd, strct);
221 dprintf(fd, "\n");
222 }
223 }
224 }
225
226 static inline void dump_union(int fd, decl_union *unn) {
227 size_t j;
228
229 dprintf(fd, "union %s::(%zu, %zu) {\n", unn->name, unn->align, unn->size);
230 for (j = 0; j < unn->args->count; ++j) {
231 decl_arg *uarg = unn->args->args[j];
232
233 dprintf(fd, "\t");
234 dump_decl_arg(fd, uarg, 0);
235 dprintf(fd, "::(%zu, %zu);\n", uarg->layout->pos, uarg->layout->len);
236 }
237 dprintf(fd, "}");
238 }
239
240 static inline void dump_unions(int fd, decl_unions *unions) {
241 size_t i;
242
243 for (i = 0; i < unions->count; ++i) {
244 decl_union *unn = unions->list[i];
245
246 if (!is_anon_type(unn->name, "union")) {
247 dump_union(fd, unn);
248 dprintf(fd, "\n");
249 }
250 }
251 }
252
253 static inline void dump_enum_items(int fd, decl_enum_items *items, unsigned level) {
254 size_t j;
255
256 if (items) {
257 ++level;
258 for (j = 0; j < items->count; ++j) {
259 decl_enum_item *i = items->list[j];
260
261 if (j) {
262 dprintf(fd, ",\n");
263 }
264 dump_level(fd, level);
265 dprintf(fd, "%s", i->name);
266 if (i->num && i->num != &i->inc) {
267 dprintf(fd, " = ");
268 dump_num_exp(fd, i->num);
269 }
270 }
271 --level;
272 }
273 }
274
275 static inline void dump_enum(int fd, decl_enum *e) {
276 dprintf(fd, "enum %s {\n", e->name);
277 dump_enum_items(fd, e->items, 0);
278 dprintf(fd, "\n}");
279 }
280
281 static inline void dump_enums(int fd, decl_enums *enums) {
282 size_t i;
283
284 for (i = 0; i < enums->count; ++i) {
285 decl_enum *e = enums->list[i];
286
287 if (!is_anon_type(e->name, "enum")) {
288 dump_enum(fd, e);
289 dprintf(fd, "\n");
290 }
291 }
292 }
293 static inline void dump_constant(int fd, constant *cnst) {
294 dprintf(fd, "const %s %s = ", cnst->type->name, cnst->name);
295 if (cnst->val->type == PSI_T_QUOTED_STRING) {
296 dprintf(fd, "\"%s\";", cnst->val->text);
297 } else {
298 dprintf(fd, "%s;", cnst->val->text);
299 }
300 }
301
302 static inline void dump_constants(int fd, constants *consts) {
303 size_t i;
304
305 for (i = 0; i < consts->count; ++i) {
306 constant *cnst = consts->list[i];
307
308 dump_constant(fd, cnst);
309 dprintf(fd, "\n");
310 }
311 }
312
313 static inline void dump_decl(int fd, decl *decl) {
314 size_t j;
315
316 dprintf(fd, "%s ", decl->abi->convention);
317 dump_decl_arg(fd, decl->func, 0);
318 dprintf(fd, "(");
319 if (decl->args) {
320 for (j = 0; j < decl->args->count; ++j) {
321 if (j) {
322 dprintf(fd, ", ");
323 }
324 dump_decl_arg(fd, decl->args->args[j], 0);
325 }
326 if (decl->args->varargs) {
327 dprintf(fd, ", ...");
328 }
329 }
330 dprintf(fd, ");");
331 }
332
333 static inline void dump_decls(int fd, decls *decls) {
334 size_t i;
335
336 for (i = 0; i < decls->count; ++i) {
337 decl *decl = decls->list[i];
338
339 dump_decl(fd, decl);
340 dprintf(fd, "\n");
341 }
342 }
343
344 static inline void dump_impl_func(int fd, impl_func *func) {
345 size_t j;
346
347 dprintf(fd, "function %s(", func->name);
348 if (func->args) {
349 for (j = 0; j < func->args->count; ++j) {
350 impl_arg *iarg = func->args->args[j];
351
352 dprintf(fd, "%s%s %s%s",
353 j ? ", " : "",
354 iarg->type->name,
355 iarg->var->reference ? "&" : "",
356 iarg->var->name);
357 if (iarg->def) {
358 dprintf(fd, " = %s", iarg->def->text);
359 }
360 }
361 if (func->args->vararg.name) {
362 impl_arg *vararg = func->args->vararg.name;
363
364 dprintf(fd, ", %s %s...%s",
365 vararg->type->name,
366 vararg->var->reference ? "&" : "",
367 vararg->var->name);
368 }
369 }
370 dprintf(fd, ") : %s%s",
371 func->return_reference ? "&":"",
372 func->return_type->name);
373 }
374
375 static inline void dump_impl_let_func(int fd, let_func *func, unsigned level);
376
377 static inline void dump_impl_let_val(int fd, let_val *val, unsigned level, int last) {
378 if (level > 1) {
379 /* only if not directly after `set ...` */
380 dump_level(fd, level);
381 }
382
383 dprintf(fd, "%s", val->flags.one.is_reference ? "&" : "");
384 switch (val->kind) {
385 case PSI_LET_NULL:
386 dprintf(fd, "NULL");
387 break;
388 case PSI_LET_TMP:
389 dump_decl_var(fd, val->data.var);
390 break;
391 case PSI_LET_CALLOC:
392 dprintf(fd, "calloc(");
393 dump_num_exp(fd, val->data.alloc->nmemb);
394 dprintf(fd, ", ");
395 dump_num_exp(fd, val->data.alloc->size);
396 dprintf(fd, ")");
397 break;
398 case PSI_LET_CALLBACK:
399 dprintf(fd, "callback %s(%s(", val->data.callback->func->name,
400 val->data.callback->func->var->name);
401 if (val->data.callback->args) {
402 size_t i, c = val->data.callback->args->count;
403
404 dprintf(fd, "\n");
405 for (i = 0; i < c; ++i) {
406 set_value *set = val->data.callback->args->vals[i];
407 ++level;
408 dump_impl_set_value(fd, set, level, i + 1 == c);
409 --level;
410 }
411 dump_level(fd, level);
412 }
413 dprintf(fd, "))");
414 break;
415 case PSI_LET_FUNC:
416 dump_impl_let_func(fd, val->data.func, level);
417 break;
418 case PSI_LET_NUMEXP:
419 dump_num_exp(fd, val->data.num);
420 break;
421
422 EMPTY_SWITCH_DEFAULT_CASE();
423 }
424 if (level > 1) {
425 if (!last) {
426 dprintf(fd, ",");
427 }
428 } else {
429 dprintf(fd, ";");
430 }
431 }
432
433 static inline void dump_impl_let_func(int fd, let_func *func, unsigned level) {
434 dprintf(fd, "%s(%s", func->name,
435 func->var->name);
436 if (func->inner) {
437 size_t i;
438
439 dprintf(fd, ",");
440 ++level;
441 for (i = 0; i < func->inner->count; ++i) {
442 dprintf(fd, "\n");
443 dump_impl_let_val(fd, func->inner->vals[i], level, i+1 == func->inner->count);
444 }
445 --level;
446 dprintf(fd, "\n");
447 dump_level(fd, level);
448 }
449 dprintf(fd, ")");
450 }
451 static inline void dump_impl_let_stmt(int fd, let_stmt *let) {
452 dprintf(fd, "\tlet %s", let->var->name);
453 if (let->val) {
454 dprintf(fd, " = ");
455 dump_impl_let_val(fd, let->val, 1, 1);
456 }
457 }
458
459 static inline void dump_impl_return_stmt(int fd, return_stmt *ret) {
460 dprintf(fd, "\treturn ");
461 dump_impl_set_value(fd, ret->set, 1, 0);
462 }
463
464 static inline void dump_impl_set_stmt(int fd, set_stmt *set) {
465 dprintf(fd, "\tset %s = ", set->var->name);
466 dump_impl_set_value(fd, set->val, 1, 0);
467 }
468
469 static inline void dump_impl_free_call(int fd, free_call *call) {
470 size_t l;
471
472 dprintf(fd, "%s(", call->func);
473 for (l = 0; l < call->vars->count; ++l) {
474 decl_var *fvar = call->vars->vars[l];
475
476 dump_decl_var(fd, fvar);
477 }
478 dprintf(fd, ");");
479 }
480
481 static inline void dump_impl_free_stmt(int fd, free_stmt *fre) {
482 size_t k;
483
484 dprintf(fd, "\tfree ");
485 for (k = 0; k < fre->calls->count; ++k) {
486 free_call *call = fre->calls->list[k];
487
488 if (k) {
489 dprintf(fd, ", ");
490 }
491 dump_impl_free_call(fd, call);
492 dprintf(fd, "\n");
493 }
494 }
495 static inline void dump_impl_stmts(int fd, impl_stmts *stmts) {
496 size_t j;
497
498 for (j = 0; j < stmts->let.count; ++j) {
499 let_stmt *let = stmts->let.list[j];
500 dump_impl_let_stmt(fd, let);
501 dprintf(fd, "\n");
502 }
503 for (j = 0; j < stmts->ret.count; ++j) {
504 return_stmt *ret = stmts->ret.list[j];
505 dump_impl_return_stmt(fd, ret);
506 dprintf(fd, "\n");
507 }
508 for (j = 0; j < stmts->set.count; ++j) {
509 set_stmt *set = stmts->set.list[j];
510
511 dump_impl_set_stmt(fd, set);
512 dprintf(fd, "\n");
513 }
514 for (j = 0; j < stmts->fre.count; ++j) {
515 free_stmt *fre = stmts->fre.list[j];
516
517 dump_impl_free_stmt(fd, fre);
518 dprintf(fd, "\n");
519 }
520 }
521
522 static inline void dump_impl(int fd, impl *impl) {
523
524 dump_impl_func(fd, impl->func);
525 dprintf(fd, " {\n");
526 if (impl->stmts) {
527 dump_impl_stmts(fd, impl->stmts);
528 }
529 dprintf(fd, "}");
530 }
531
532 static inline void dump_impls(int fd, impls *impls) {
533 size_t i;
534
535 for (i = 0; i < impls->count; ++i) {
536 impl *impl = impls->list[i];
537
538 dump_impl(fd, impl);
539 dprintf(fd, "\n");
540 }
541 }
542
543 void psi_context_dump(struct psi_context *C, int fd)
544 {
545 #ifdef HAVE_LIBJIT
546 if (C->ops == psi_libjit_ops()) {
547 dprintf(fd, "// psi.engine=jit\n");
548 }
549 #endif
550 #ifdef HAVE_LIBFFI
551 if (C->ops == psi_libffi_ops()) {
552 dprintf(fd, "// psi.engine=ffi\n");
553 }
554 #endif
555 dprintf(fd, "\n");
556
557 if (C->defs) {
558 dump_typedefs(fd, C->defs);
559 dprintf(fd, "\n");
560 }
561 if (C->unions) {
562 dump_unions(fd, C->unions);
563 dprintf(fd, "\n");
564 }
565 if (C->structs) {
566 dump_structs(fd, C->structs);
567 dprintf(fd, "\n");
568 }
569 if (C->enums) {
570 dump_enums(fd, C->enums);
571 dprintf(fd, "\n");
572 }
573 if (C->consts) {
574 dump_constants(fd, C->consts);
575 dprintf(fd, "\n");
576 }
577 if (C->decls) {
578 dump_decls(fd, C->decls);
579 dprintf(fd, "\n");
580 }
581 if (C->impls) {
582 dump_impls(fd, C->impls);
583 dprintf(fd, "\n");
584 }
585 }