c62ebf23b4bd5e7d1b2b2d427eccec729a0fceca
[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->func->type != PSI_T_ELLIPSIS) {
131 dprintf(fd, ",\n");
132 for (i = 0; i < set->count; ++i) {
133 dump_impl_set_value(fd, set->inner[i], level+1, i == (set->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)", strct->name, 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
193 dump_struct(fd, strct);
194 dprintf(fd, "\n");
195 }
196 }
197
198 static inline void dump_enum(int fd, decl_enum *e) {
199 size_t j;
200
201 dprintf(fd, "enum %s {\n", e->name);
202 for (j = 0; j < e->items->count; ++j) {
203 decl_enum_item *i = e->items->list[j];
204
205 if (j) {
206 dprintf(fd, ",\n");
207 }
208 dprintf(fd, "\t%s", i->name);
209 if (i->num && i->num != &i->inc) {
210 dprintf(fd, " = ");
211 dump_num_exp(fd, i->num);
212 }
213 }
214 dprintf(fd, "\n}");
215 }
216
217 static inline void dump_enums(int fd, decl_enums *enums) {
218 size_t i;
219
220 for (i = 0; i < enums->count; ++i) {
221 decl_enum *e = enums->list[i];
222
223 dump_enum(fd, e);
224 dprintf(fd, "\n");
225 }
226 }
227 static inline void dump_constant(int fd, constant *cnst) {
228 dprintf(fd, "const %s %s = ", cnst->type->name, cnst->name);
229 if (cnst->val->type == PSI_T_QUOTED_STRING) {
230 dprintf(fd, "\"%s\";", cnst->val->text);
231 } else {
232 dprintf(fd, "%s;", cnst->val->text);
233 }
234 }
235
236 static inline void dump_constants(int fd, constants *consts) {
237 size_t i;
238
239 for (i = 0; i < consts->count; ++i) {
240 constant *cnst = consts->list[i];
241
242 dump_constant(fd, cnst);
243 dprintf(fd, "\n");
244 }
245 }
246
247 static inline void dump_decl(int fd, decl *decl) {
248 size_t j;
249
250 dprintf(fd, "%s ", decl->abi->convention);
251 dump_decl_arg(fd, decl->func);
252 dprintf(fd, "(");
253 if (decl->args) {
254 for (j = 0; j < decl->args->count; ++j) {
255 if (j) {
256 dprintf(fd, ", ");
257 }
258 dump_decl_arg(fd, decl->args->args[j]);
259 }
260 if (decl->args->varargs) {
261 dprintf(fd, ", ...");
262 }
263 }
264 dprintf(fd, ");");
265 }
266
267 static inline void dump_decls(int fd, decls *decls) {
268 size_t i;
269
270 for (i = 0; i < decls->count; ++i) {
271 decl *decl = decls->list[i];
272
273 dump_decl(fd, decl);
274 dprintf(fd, "\n");
275 }
276 }
277
278 static inline void dump_impl_func(int fd, impl_func *func) {
279 size_t j;
280
281 dprintf(fd, "function %s(", func->name);
282 if (func->args) {
283 for (j = 0; j < func->args->count; ++j) {
284 impl_arg *iarg = func->args->args[j];
285
286 dprintf(fd, "%s%s %s$%s",
287 j ? ", " : "",
288 iarg->type->name,
289 iarg->var->reference ? "&" : "",
290 iarg->var->name);
291 if (iarg->def) {
292 dprintf(fd, " = %s", iarg->def->text);
293 }
294 }
295 if (func->args->vararg.name) {
296 impl_arg *vararg = func->args->vararg.name;
297
298 dprintf(fd, ", %s %s...$%s",
299 vararg->type->name,
300 vararg->var->reference ? "&" : "",
301 vararg->var->name);
302 }
303 }
304 dprintf(fd, ") : %s%s",
305 func->return_reference ? "&":"",
306 func->return_type->name);
307 }
308
309 static inline void dump_impl_let_stmt(int fd, let_stmt *let) {
310 dprintf(fd, "\tlet %s", let->var->name);
311 if (let->val) {
312 dprintf(fd, " = %s", let->val->flags.one.is_reference ? "&" : "");
313 switch (let->val->kind) {
314 case PSI_LET_NULL:
315 dprintf(fd, "NULL");
316 break;
317 case PSI_LET_TMP:
318 dump_decl_var(fd, let->val->data.var);
319 break;
320 case PSI_LET_CALLOC:
321 dprintf(fd, "calloc(");
322 dump_num_exp(fd, let->val->data.alloc->nmemb);
323 dprintf(fd, ", ");
324 dump_num_exp(fd, let->val->data.alloc->size);
325 dprintf(fd, ")");
326 break;
327 case PSI_LET_FUNC:
328 dprintf(fd, "%s($%s)", let->val->data.func->name,
329 let->val->data.func->var->name);
330 break;
331 case PSI_LET_NUMEXP:
332 dump_num_exp(fd, let->val->data.num);
333 break;
334
335 EMPTY_SWITCH_DEFAULT_CASE();
336 }
337 dprintf(fd, ";");
338 }
339 }
340
341 static inline void dump_impl_return_stmt(int fd, return_stmt *ret) {
342 dprintf(fd, "\treturn ");
343 dump_impl_set_value(fd, ret->set, 1, 0);
344 }
345
346 static inline void dump_impl_set_stmt(int fd, set_stmt *set) {
347 dprintf(fd, "\tset $%s = ", set->var->name);
348 dump_impl_set_value(fd, set->val, 1, 0);
349 }
350
351 static inline void dump_impl_free_call(int fd, free_call *call) {
352 size_t l;
353
354 dprintf(fd, "%s(", call->func);
355 for (l = 0; l < call->vars->count; ++l) {
356 decl_var *fvar = call->vars->vars[l];
357
358 dump_decl_var(fd, fvar);
359 }
360 dprintf(fd, ");");
361 }
362
363 static inline void dump_impl_free_stmt(int fd, free_stmt *fre) {
364 size_t k;
365
366 dprintf(fd, "\tfree ");
367 for (k = 0; k < fre->calls->count; ++k) {
368 free_call *call = fre->calls->list[k];
369
370 if (k) {
371 dprintf(fd, ", ");
372 }
373 dump_impl_free_call(fd, call);
374 dprintf(fd, "\n");
375 }
376 }
377 static inline void dump_impl_stmts(int fd, impl_stmts *stmts) {
378 size_t j;
379
380 for (j = 0; j < stmts->let.count; ++j) {
381 let_stmt *let = stmts->let.list[j];
382 dump_impl_let_stmt(fd, let);
383 dprintf(fd, "\n");
384 }
385 for (j = 0; j < stmts->ret.count; ++j) {
386 return_stmt *ret = stmts->ret.list[j];
387 dump_impl_return_stmt(fd, ret);
388 dprintf(fd, "\n");
389 }
390 for (j = 0; j < stmts->set.count; ++j) {
391 set_stmt *set = stmts->set.list[j];
392
393 dump_impl_set_stmt(fd, set);
394 dprintf(fd, "\n");
395 }
396 for (j = 0; j < stmts->fre.count; ++j) {
397 free_stmt *fre = stmts->fre.list[j];
398
399 dump_impl_free_stmt(fd, fre);
400 dprintf(fd, "\n");
401 }
402 }
403
404 static inline void dump_impl(int fd, impl *impl) {
405
406 dump_impl_func(fd, impl->func);
407 dprintf(fd, " {\n");
408 if (impl->stmts) {
409 dump_impl_stmts(fd, impl->stmts);
410 }
411 dprintf(fd, "}");
412 }
413
414 static inline void dump_impls(int fd, impls *impls) {
415 size_t i;
416
417 for (i = 0; i < impls->count; ++i) {
418 impl *impl = impls->list[i];
419
420 dump_impl(fd, impl);
421 dprintf(fd, "\n");
422 }
423 }
424
425 void PSI_ContextDump(PSI_Context *C, int fd)
426 {
427 #ifdef HAVE_LIBJIT
428 if (C->ops == PSI_Libjit()) {
429 dprintf(fd, "// psi.engine=jit\n");
430 }
431 #endif
432 #ifdef HAVE_LIBFFI
433 if (C->ops == PSI_Libffi()) {
434 dprintf(fd, "// psi.engine=ffi\n");
435 }
436 #endif
437 dprintf(fd, "\n");
438
439 if (C->defs) {
440 dump_typedefs(fd, C->defs);
441 dprintf(fd, "\n");
442 }
443
444 if (C->structs) {
445 dump_structs(fd, C->structs);
446 dprintf(fd, "\n");
447 }
448 if (C->enums) {
449 dump_enums(fd, C->enums);
450 dprintf(fd, "\n");
451 }
452 if (C->consts) {
453 dump_constants(fd, C->consts);
454 dprintf(fd, "\n");
455 }
456 if (C->decls) {
457 dump_decls(fd, C->decls);
458 dprintf(fd, "\n");
459 }
460 if (C->impls) {
461 dump_impls(fd, C->impls);
462 dprintf(fd, "\n");
463 }
464 }