17699a49a591db9fcbbc03a013a9df2a15681872
[m6w6/ext-psi] / src / context.c
1 #ifdef HAVE_CONFIG_H
2 # include "config.h"
3 #endif
4
5 #include "php.h"
6
7 #ifdef HAVE_DIRENT_H
8 # include <dirent.h>
9 # define NAMLEN(dirent) strlen ((dirent)->d_name)
10 #else
11 # define dirent direct
12 # define NAMLEN(dirent) ((dirent)->d_namlen)
13 # ifdef HAVE_SYS_NDIR_H
14 # include <sys/ndir.h>
15 # endif
16 # ifdef HAVE_SYS_DIR_H
17 # include <sys/dir.h>
18 # endif
19 # ifdef HAVE_NDIR_H
20 # include <ndir.h>
21 # endif
22 #endif
23
24 #include <fnmatch.h>
25
26 #include "php.h"
27 #include "php_scandir.h"
28 #include "php_psi.h"
29 #include "calc.h"
30 #include "libjit.h"
31 #include "libffi.h"
32
33 #include "php_psi_types.h"
34 #include "php_psi_consts.h"
35 #include "php_psi_decls.h"
36 #include "php_psi_va_decls.h"
37 #include "php_psi_structs.h"
38 #include "php_psi_unions.h"
39
40 PSI_Context *PSI_ContextInit(PSI_Context *C, PSI_ContextOps *ops, PSI_ContextErrorFunc error, unsigned flags)
41 {
42 PSI_Data T;
43 struct psi_predef_type *predef_type;
44 struct psi_predef_const *predef_const;
45 struct psi_predef_struct *predef_struct;
46 struct psi_predef_union *predef_union;
47 struct psi_predef_decl *predef_decl;
48
49 if (!C) {
50 C = malloc(sizeof(*C));
51 }
52 memset(C, 0, sizeof(*C));
53
54 C->error = error;
55 C->flags = flags;
56 C->ops = ops;
57
58 if (ops->init) {
59 ops->init(C);
60 }
61
62 ZEND_ASSERT(ops->call != NULL);
63 ZEND_ASSERT(ops->compile != NULL);
64
65 /* build up predefs in a temporary PSI_Data for validation */
66 memset(&T, 0, sizeof(T));
67 T.error = error;
68
69 for (predef_type = &psi_predef_types[0]; predef_type->type_tag; ++predef_type) {
70 decl_type *type = init_decl_type(predef_type->type_tag, predef_type->type_name);
71 decl_var *var = init_decl_var(predef_type->alias, 0, 0); /* FIXME: indirection */
72 decl_arg *def = init_decl_arg(type, var);
73
74 T.defs = add_decl_typedef(T.defs, def);
75 }
76 for (predef_const = &psi_predef_consts[0]; predef_const->type_tag; ++predef_const) {
77 impl_def_val *val = init_impl_def_val(predef_const->val_type_tag, predef_const->val_text);
78 const_type *type = init_const_type(predef_const->type_tag, predef_const->type_name);
79 constant *constant = init_constant(type, predef_const->var_name, val);
80
81 T.consts = add_constant(T.consts, constant);
82 }
83 for (predef_struct = &psi_predef_structs[0]; predef_struct->type_tag; ++predef_struct) {
84 struct psi_predef_struct *member;
85 decl_args *dargs = init_decl_args(NULL);
86 decl_struct *dstruct = init_decl_struct(predef_struct->var_name, dargs);
87
88 dstruct->size = predef_struct->size;
89 dstruct->align = predef_struct->offset;
90 for (member = &predef_struct[1]; member->type_tag; ++member) {
91 decl_type *type;
92 decl_var *dvar;
93 decl_arg *darg;
94
95 type = init_decl_type(member->type_tag, member->type_name);
96 dvar = init_decl_var(member->var_name, member->pointer_level, member->array_size);
97 darg = init_decl_arg(type, dvar);
98 darg->layout = init_decl_struct_layout(member->offset, member->size);
99 dargs = add_decl_arg(dargs, darg);
100 }
101
102 T.structs = add_decl_struct(T.structs, dstruct);
103 predef_struct = member;
104 }
105 for (predef_union = &psi_predef_unions[0]; predef_union->type_tag; ++predef_union) {
106 struct psi_predef_union *member;
107 decl_args *dargs = init_decl_args(NULL);
108 decl_union *dunion = init_decl_union(predef_union->var_name, dargs);
109
110 dunion->size = predef_union->size;
111 dunion->align = dunion->offset;
112 for (member = &predef_union[1]; member->type_tag; ++member) {
113 decl_type *type;
114 decl_var *dvar;
115 decl_arg *darg;
116
117 type = init_decl_type(member->type_tag, member->type_name);
118 dvar = init_decl_var(member->var_name, member->pointer_level, member->array_size);
119 darg = init_decl_arg(type, dvar);
120 darg->layout = init_decl_struct_layout(member->offset, member->size);
121 dargs = add_decl_arg(dargs, darg);
122 }
123
124 T.unions = add_decl_union(T.unions, dunion);
125 predef_union = member;
126 }
127 for (predef_decl = &psi_predef_decls[0]; predef_decl->type_tag; ++predef_decl) {
128 struct psi_predef_decl *farg;
129 decl_type *ftype = init_decl_type(predef_decl->type_tag, predef_decl->type_name);
130 decl_var *fname = init_decl_var(predef_decl->var_name, predef_decl->pointer_level, predef_decl->array_size);
131 decl_arg *func = init_decl_arg(ftype, fname);
132 decl_args *args = init_decl_args(NULL);
133 decl *decl = init_decl(init_decl_abi("default"), func, args);
134
135 for (farg = &predef_decl[1]; farg->type_tag; ++farg) {
136 decl_type *arg_type = init_decl_type(farg->type_tag, farg->type_name);
137 decl_var *arg_var = init_decl_var(farg->var_name, farg->pointer_level, farg->array_size);
138 decl_arg *darg = init_decl_arg(arg_type, arg_var);
139 args = add_decl_arg(args, darg);
140 }
141
142 T.decls = add_decl(T.decls, decl);
143 predef_decl = farg;
144 }
145
146 for (predef_decl = &psi_predef_vararg_decls[0]; predef_decl->type_tag; ++predef_decl) {
147 struct psi_predef_decl *farg;
148 decl_type *ftype = init_decl_type(predef_decl->type_tag, predef_decl->type_name);
149 decl_var *fname = init_decl_var(predef_decl->var_name, predef_decl->pointer_level, predef_decl->array_size);
150 decl_arg *func = init_decl_arg(ftype, fname);
151 decl_args *args = init_decl_args(NULL);
152 decl *decl = init_decl(init_decl_abi("default"), func, args);
153
154 for (farg = &predef_decl[1]; farg->type_tag; ++farg) {
155 decl_type *arg_type = init_decl_type(farg->type_tag, farg->type_name);
156 decl_var *arg_var = init_decl_var(farg->var_name, farg->pointer_level, farg->array_size);
157 decl_arg *darg = init_decl_arg(arg_type, arg_var);
158 args = add_decl_arg(args, darg);
159 }
160 args->varargs = 1;
161
162 T.decls = add_decl(T.decls, decl);
163 predef_decl = farg;
164 }
165
166 PSI_ContextValidateData(PSI_DATA(C), &T);
167
168 C->count = 1;
169 C->data = malloc(sizeof(*C->data));
170 PSI_DataExchange(C->data, &T);
171
172 return C;
173 }
174
175 static int psi_select_dirent(const struct dirent *entry)
176 {
177 #ifndef FNM_CASEFOLD
178 #define FNM_CASEFOLD 0
179 #endif
180 return 0 == fnmatch("*.psi", entry->d_name, FNM_CASEFOLD);
181 }
182
183 void PSI_ContextBuild(PSI_Context *C, const char *paths)
184 {
185 int i, n;
186 char *sep = NULL, *cpy = strdup(paths), *ptr = cpy;
187 struct dirent **entries = NULL;
188
189 do {
190 sep = strchr(ptr, ':');
191
192 if (sep) {
193 *sep = 0;
194 }
195
196 n = php_scandir(ptr, &entries, psi_select_dirent, alphasort);
197
198 if (n > 0) {
199 for (i = 0; i < n; ++i) {
200 char psi[MAXPATHLEN];
201 PSI_Parser P;
202
203 if (MAXPATHLEN <= slprintf(psi, MAXPATHLEN, "%s/%s", ptr, entries[i]->d_name)) {
204 C->error(C, NULL, PSI_WARNING, "Path to PSI file too long: %s/%s",
205 ptr, entries[i]->d_name);
206 }
207 if (!PSI_ParserInit(&P, psi, C->error, C->flags)) {
208 C->error(C, NULL, PSI_WARNING, "Failed to init PSI parser (%s): %s",
209 psi, strerror(errno));
210 continue;
211 }
212
213 while (0 < PSI_ParserScan(&P)) {
214 PSI_ParserParse(&P, PSI_TokenAlloc(&P));
215 if (P.num == PSI_T_EOF) {
216 break;
217 }
218 }
219
220 PSI_ParserParse(&P, NULL);
221 PSI_ContextValidate(C, &P);
222 PSI_ParserDtor(&P);
223 }
224 }
225
226 if (entries) {
227 for (i = 0; i < n; ++i) {
228 free(entries[i]);
229 }
230 free(entries);
231 }
232
233 ptr = sep + 1;
234 } while (sep);
235
236
237 if (PSI_ContextCompile(C) && SUCCESS != zend_register_functions(NULL, C->closures, NULL, MODULE_PERSISTENT)) {
238 C->error(C, NULL, PSI_WARNING, "Failed to register functions!");
239 }
240
241 free(cpy);
242
243 }
244
245 zend_function_entry *PSI_ContextCompile(PSI_Context *C)
246 {
247 size_t i;
248 zend_constant zc;
249
250 zc.flags = CONST_PERSISTENT|CONST_CS;
251 zc.module_number = EG(current_module)->module_number;
252
253 if (C->consts) {
254 for (i = 0; i < C->consts->count; ++i) {
255 constant *c = C->consts->list[i];
256
257 zc.name = zend_string_init(c->name + (c->name[0] == '\\'), strlen(c->name) - (c->name[0] == '\\'), 1);
258 ZVAL_NEW_STR(&zc.value, zend_string_init(c->val->text, strlen(c->val->text), 1));
259
260 switch (c->type->type) {
261 case PSI_T_BOOL:
262 convert_to_boolean(&zc.value);
263 break;
264 case PSI_T_INT:
265 convert_to_long(&zc.value);
266 break;
267 case PSI_T_FLOAT:
268 convert_to_double(&zc.value);
269 break;
270 }
271 zend_register_constant(&zc);
272 }
273 }
274 if (C->enums) {
275 for (i = 0; i < C->enums->count; ++i) {
276 decl_enum *e = C->enums->list[i];
277 size_t j;
278
279 for (j = 0; j < e->items->count; ++j) {
280 decl_enum_item *i = e->items->list[j];
281 zend_string *name = strpprintf(0, "psi\\%s\\%s", e->name, i->name);
282
283 zc.name = zend_string_dup(name, 1);
284 ZVAL_LONG(&zc.value, psi_long_num_exp(i->num, NULL));
285 zend_register_constant(&zc);
286 zend_string_release(name);
287 }
288 }
289 }
290
291 return C->closures = C->ops->compile(C);
292 }
293
294
295 void PSI_ContextCall(PSI_Context *C, decl_callinfo *decl_call, impl_vararg *va)
296 {
297 C->ops->call(C, decl_call, va);
298 }
299
300
301 void PSI_ContextDtor(PSI_Context *C)
302 {
303 size_t i;
304 zend_function_entry *zfe;
305
306 if (C->ops->dtor) {
307 C->ops->dtor(C);
308 }
309
310 free_decl_libs(&C->psi.libs);
311
312 if (C->data) {
313 for (i = 0; i < C->count; ++i) {
314 PSI_DataDtor(&C->data[i]);
315 }
316 free(C->data);
317 }
318
319 if (C->closures) {
320 for (zfe = C->closures; zfe->fname; ++zfe) {
321 free((void *) zfe->arg_info);
322 }
323 free(C->closures);
324 }
325
326 if (C->consts) {
327 if (C->consts->list) {
328 free(C->consts->list);
329 }
330 free(C->consts);
331 }
332 if (C->defs) {
333 if (C->defs->list) {
334 free(C->defs->list);
335 }
336 free(C->defs);
337 }
338 if (C->structs) {
339 if (C->structs->list) {
340 free(C->structs->list);
341 }
342 free(C->structs);
343 }
344 if (C->enums) {
345 if (C->enums->list) {
346 free(C->enums->list);
347 }
348 free(C->enums);
349 }
350 if (C->decls) {
351 if (C->decls->list) {
352 free(C->decls->list);
353 }
354 free(C->decls);
355 }
356 if (C->impls) {
357 if (C->impls->list) {
358 free(C->impls->list);
359 }
360 free(C->impls);
361 }
362
363 memset(C, 0, sizeof(*C));
364 }
365
366 void PSI_ContextFree(PSI_Context **C)
367 {
368 if (*C) {
369 PSI_ContextDtor(*C);
370 free(*C);
371 *C = NULL;
372 }
373 }