zval passthru
[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 = predef_union->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;
188
189 do {
190 sep = strchr(ptr, ':');
191
192 if (sep) {
193 *sep = 0;
194 }
195
196 entries = NULL;
197 n = php_scandir(ptr, &entries, psi_select_dirent, alphasort);
198
199 if (n > 0) {
200 for (i = 0; i < n; ++i) {
201 char psi[MAXPATHLEN];
202 PSI_Parser P;
203
204 if (MAXPATHLEN <= slprintf(psi, MAXPATHLEN, "%s/%s", ptr, entries[i]->d_name)) {
205 C->error(C, NULL, PSI_WARNING, "Path to PSI file too long: %s/%s",
206 ptr, entries[i]->d_name);
207 }
208 if (!PSI_ParserInit(&P, psi, C->error, C->flags)) {
209 C->error(C, NULL, PSI_WARNING, "Failed to init PSI parser (%s): %s",
210 psi, strerror(errno));
211 continue;
212 }
213
214 while (0 < PSI_ParserScan(&P)) {
215 PSI_ParserParse(&P, PSI_TokenAlloc(&P));
216 if (P.num == PSI_T_EOF) {
217 break;
218 }
219 }
220
221 PSI_ParserParse(&P, NULL);
222 PSI_ContextValidate(C, &P);
223 PSI_ParserDtor(&P);
224 }
225 }
226
227 if (entries) {
228 for (i = 0; i < n; ++i) {
229 free(entries[i]);
230 }
231 free(entries);
232 }
233
234 ptr = sep + 1;
235 } while (sep);
236
237
238 if (PSI_ContextCompile(C) && SUCCESS != zend_register_functions(NULL, C->closures, NULL, MODULE_PERSISTENT)) {
239 C->error(C, NULL, PSI_WARNING, "Failed to register functions!");
240 }
241
242 free(cpy);
243
244 }
245
246 zend_function_entry *PSI_ContextCompile(PSI_Context *C)
247 {
248 size_t i;
249 zend_constant zc;
250
251 zc.flags = CONST_PERSISTENT|CONST_CS;
252 zc.module_number = EG(current_module)->module_number;
253
254 if (C->consts) {
255 for (i = 0; i < C->consts->count; ++i) {
256 constant *c = C->consts->list[i];
257
258 zc.name = zend_string_init(c->name + (c->name[0] == '\\'), strlen(c->name) - (c->name[0] == '\\'), 1);
259 ZVAL_NEW_STR(&zc.value, zend_string_init(c->val->text, strlen(c->val->text), 1));
260
261 switch (c->type->type) {
262 case PSI_T_BOOL:
263 convert_to_boolean(&zc.value);
264 break;
265 case PSI_T_INT:
266 convert_to_long(&zc.value);
267 break;
268 case PSI_T_FLOAT:
269 convert_to_double(&zc.value);
270 break;
271 }
272 zend_register_constant(&zc);
273 }
274 }
275 if (C->enums) {
276 for (i = 0; i < C->enums->count; ++i) {
277 decl_enum *e = C->enums->list[i];
278 size_t j;
279
280 for (j = 0; j < e->items->count; ++j) {
281 decl_enum_item *i = e->items->list[j];
282 zend_string *name = strpprintf(0, "psi\\%s\\%s", e->name, i->name);
283
284 zc.name = zend_string_dup(name, 1);
285 ZVAL_LONG(&zc.value, psi_long_num_exp(i->num, NULL));
286 zend_register_constant(&zc);
287 zend_string_release(name);
288 }
289 }
290 }
291
292 return C->closures = C->ops->compile(C);
293 }
294
295
296 void PSI_ContextCall(PSI_Context *C, decl_callinfo *decl_call, impl_vararg *va)
297 {
298 C->ops->call(C, decl_call, va);
299 }
300
301
302 void PSI_ContextDtor(PSI_Context *C)
303 {
304 size_t i;
305 zend_function_entry *zfe;
306
307 if (C->ops->dtor) {
308 C->ops->dtor(C);
309 }
310
311 free_decl_libs(&C->psi.libs);
312
313 if (C->data) {
314 for (i = 0; i < C->count; ++i) {
315 PSI_DataDtor(&C->data[i]);
316 }
317 free(C->data);
318 }
319
320 if (C->closures) {
321 for (zfe = C->closures; zfe->fname; ++zfe) {
322 free((void *) zfe->arg_info);
323 }
324 free(C->closures);
325 }
326
327 if (C->consts) {
328 if (C->consts->list) {
329 free(C->consts->list);
330 }
331 free(C->consts);
332 }
333 if (C->defs) {
334 if (C->defs->list) {
335 free(C->defs->list);
336 }
337 free(C->defs);
338 }
339 if (C->structs) {
340 if (C->structs->list) {
341 free(C->structs->list);
342 }
343 free(C->structs);
344 }
345 if (C->unions) {
346 if (C->unions->list) {
347 free(C->unions->list);
348 }
349 free(C->unions);
350 }
351 if (C->enums) {
352 if (C->enums->list) {
353 free(C->enums->list);
354 }
355 free(C->enums);
356 }
357 if (C->decls) {
358 if (C->decls->list) {
359 free(C->decls->list);
360 }
361 free(C->decls);
362 }
363 if (C->impls) {
364 if (C->impls->list) {
365 free(C->impls->list);
366 }
367 free(C->impls);
368 }
369
370 memset(C, 0, sizeof(*C));
371 }
372
373 void PSI_ContextFree(PSI_Context **C)
374 {
375 if (*C) {
376 PSI_ContextDtor(*C);
377 free(*C);
378 *C = NULL;
379 }
380 }