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