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