api refactoring
[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_stmt(int fd, let_stmt *let) {
376 dprintf(fd, "\tlet %s", let->var->name);
377 if (let->val) {
378 dprintf(fd, " = %s", let->val->flags.one.is_reference ? "&" : "");
379 switch (let->val->kind) {
380 case PSI_LET_NULL:
381 dprintf(fd, "NULL");
382 break;
383 case PSI_LET_TMP:
384 dump_decl_var(fd, let->val->data.var);
385 break;
386 case PSI_LET_CALLOC:
387 dprintf(fd, "calloc(");
388 dump_num_exp(fd, let->val->data.alloc->nmemb);
389 dprintf(fd, ", ");
390 dump_num_exp(fd, let->val->data.alloc->size);
391 dprintf(fd, ")");
392 break;
393 case PSI_LET_CALLBACK:
394 dprintf(fd, "callback %s(%s(", let->val->data.callback->func->name,
395 let->val->data.callback->func->var->name);
396 if (let->val->data.callback->args) {
397 size_t i, c = let->val->data.callback->args->count;
398
399 dprintf(fd, "\n");
400 for (i = 0; i < c; ++i) {
401 set_value *set = let->val->data.callback->args->vals[i];
402 dump_impl_set_value(fd, set, 2, i + 1 == c);
403 }
404 dprintf(fd, "\t");
405 }
406 dprintf(fd, "));");
407 break;
408 case PSI_LET_FUNC:
409 dprintf(fd, "%s(%s)", let->val->data.func->name,
410 let->val->data.func->var->name);
411 break;
412 case PSI_LET_NUMEXP:
413 dump_num_exp(fd, let->val->data.num);
414 break;
415
416 EMPTY_SWITCH_DEFAULT_CASE();
417 }
418 dprintf(fd, ";");
419 }
420 }
421
422 static inline void dump_impl_return_stmt(int fd, return_stmt *ret) {
423 dprintf(fd, "\treturn ");
424 dump_impl_set_value(fd, ret->set, 1, 0);
425 }
426
427 static inline void dump_impl_set_stmt(int fd, set_stmt *set) {
428 dprintf(fd, "\tset %s = ", set->var->name);
429 dump_impl_set_value(fd, set->val, 1, 0);
430 }
431
432 static inline void dump_impl_free_call(int fd, free_call *call) {
433 size_t l;
434
435 dprintf(fd, "%s(", call->func);
436 for (l = 0; l < call->vars->count; ++l) {
437 decl_var *fvar = call->vars->vars[l];
438
439 dump_decl_var(fd, fvar);
440 }
441 dprintf(fd, ");");
442 }
443
444 static inline void dump_impl_free_stmt(int fd, free_stmt *fre) {
445 size_t k;
446
447 dprintf(fd, "\tfree ");
448 for (k = 0; k < fre->calls->count; ++k) {
449 free_call *call = fre->calls->list[k];
450
451 if (k) {
452 dprintf(fd, ", ");
453 }
454 dump_impl_free_call(fd, call);
455 dprintf(fd, "\n");
456 }
457 }
458 static inline void dump_impl_stmts(int fd, impl_stmts *stmts) {
459 size_t j;
460
461 for (j = 0; j < stmts->let.count; ++j) {
462 let_stmt *let = stmts->let.list[j];
463 dump_impl_let_stmt(fd, let);
464 dprintf(fd, "\n");
465 }
466 for (j = 0; j < stmts->ret.count; ++j) {
467 return_stmt *ret = stmts->ret.list[j];
468 dump_impl_return_stmt(fd, ret);
469 dprintf(fd, "\n");
470 }
471 for (j = 0; j < stmts->set.count; ++j) {
472 set_stmt *set = stmts->set.list[j];
473
474 dump_impl_set_stmt(fd, set);
475 dprintf(fd, "\n");
476 }
477 for (j = 0; j < stmts->fre.count; ++j) {
478 free_stmt *fre = stmts->fre.list[j];
479
480 dump_impl_free_stmt(fd, fre);
481 dprintf(fd, "\n");
482 }
483 }
484
485 static inline void dump_impl(int fd, impl *impl) {
486
487 dump_impl_func(fd, impl->func);
488 dprintf(fd, " {\n");
489 if (impl->stmts) {
490 dump_impl_stmts(fd, impl->stmts);
491 }
492 dprintf(fd, "}");
493 }
494
495 static inline void dump_impls(int fd, impls *impls) {
496 size_t i;
497
498 for (i = 0; i < impls->count; ++i) {
499 impl *impl = impls->list[i];
500
501 dump_impl(fd, impl);
502 dprintf(fd, "\n");
503 }
504 }
505
506 void psi_context_dump(struct psi_context *C, int fd)
507 {
508 #ifdef HAVE_LIBJIT
509 if (C->ops == psi_libjit_ops()) {
510 dprintf(fd, "// psi.engine=jit\n");
511 }
512 #endif
513 #ifdef HAVE_LIBFFI
514 if (C->ops == psi_libffi_ops()) {
515 dprintf(fd, "// psi.engine=ffi\n");
516 }
517 #endif
518 dprintf(fd, "\n");
519
520 if (C->defs) {
521 dump_typedefs(fd, C->defs);
522 dprintf(fd, "\n");
523 }
524 if (C->unions) {
525 dump_unions(fd, C->unions);
526 dprintf(fd, "\n");
527 }
528 if (C->structs) {
529 dump_structs(fd, C->structs);
530 dprintf(fd, "\n");
531 }
532 if (C->enums) {
533 dump_enums(fd, C->enums);
534 dprintf(fd, "\n");
535 }
536 if (C->consts) {
537 dump_constants(fd, C->consts);
538 dprintf(fd, "\n");
539 }
540 if (C->decls) {
541 dump_decls(fd, C->decls);
542 dprintf(fd, "\n");
543 }
544 if (C->impls) {
545 dump_impls(fd, C->impls);
546 dprintf(fd, "\n");
547 }
548 }