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