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