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