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