8 #ifdef HAVE_SYS_TYPES_H
9 # include <sys/types.h>
11 #ifdef HAVE_SYS_STAT_H
12 # include <sys/stat.h>
23 # if !defined STDC_HEADERS && defined HAVE_MEMORY_H
31 #ifdef HAVE_INTTYPES_H
32 # include <inttypes.h>
47 #ifdef HAVE_NETINET_IN_H
48 # include <netinet/in.h>
50 #ifdef HAVE_ARPA_NAMESER_H
51 # include <arpa/nameser.h>
59 #ifdef HAVE_SYS_SELECT_H
60 # include <sys/select.h>
62 #ifdef HAVE_SYS_SOCKET_H
63 # include <sys/socket.h>
65 #ifdef HAVE_SYS_TIME_H
66 # include <sys/time.h>
68 #ifdef HAVE_SYS_TIMES_H
69 # include <sys/times.h>
74 #ifdef HAVE_SYS_UTSNAME_H
75 # include <sys/utsname.h>
86 # define NAMLEN(dirent) strlen ((dirent)->d_name)
88 # define dirent direct
89 # define NAMLEN(dirent) ((dirent)->d_namlen)
90 # ifdef HAVE_SYS_NDIR_H
91 # include <sys/ndir.h>
93 # ifdef HAVE_SYS_DIR_H
101 #include <sys/param.h>
106 #include "php_scandir.h"
114 static struct psi_std_type
{
116 const char *type_name
;
117 } psi_std_types
[] = {
118 {PSI_T_FLOAT
, "float"},
119 {PSI_T_DOUBLE
, "double"},
120 {PSI_T_INT8
, "int8_t"},
121 {PSI_T_INT16
, "int16_t"},
122 {PSI_T_INT32
, "int32_t"},
123 {PSI_T_INT64
, "int64_t"},
124 {PSI_T_UINT8
, "uint8_t"},
125 {PSI_T_UINT16
, "uint16_t"},
126 {PSI_T_UINT32
, "uint32_t"},
127 {PSI_T_UINT64
, "uint64_t"},
131 static struct psi_predef_type
{
133 const char *type_name
;
135 } psi_predef_types
[] = {
140 static struct psi_predef_const
{
142 const char *type_name
;
143 const char *var_name
;
144 const char *val_text
;
145 token_t val_type_tag
;
146 } psi_predef_consts
[] = {
153 static struct psi_func_redir
{
156 } psi_func_redirs
[] = {
161 static struct psi_predef_decl
{
163 const char *type_name
;
164 const char *var_name
;
165 size_t pointer_level
;
167 } psi_predef_decls
[] = {
172 static struct psi_predef_struct
{
174 const char *type_name
;
175 const char *var_name
;
178 size_t pointer_level
;
180 } psi_predef_structs
[] = {
185 static int validate_lib(PSI_Data
*data
, void **dlopened
) {
186 char lib
[MAXPATHLEN
];
187 const char *ptr
= data
->psi
.file
.ln
;
191 /* FIXME: assume stdlib */
193 } else if (!strchr(ptr
, '/')) {
194 len
= snprintf(lib
, MAXPATHLEN
, "lib%s.%s", ptr
, PHP_PSI_SHLIB_SUFFIX
);
195 if (MAXPATHLEN
== len
) {
196 data
->error(PSI_WARNING
, "Library name too long: '%s'", ptr
);
201 if (!(*dlopened
= dlopen(ptr
, RTLD_LAZY
|RTLD_LOCAL
))) {
202 data
->error(PSI_WARNING
, "Could not open library '%s': %s.",
203 data
->psi
.file
.ln
, dlerror());
209 static inline int locate_decl_type_alias(decl_typedefs
*defs
, decl_type
*type
) {
211 struct psi_std_type
*stdtyp
;
216 for (i
= 0; i
< defs
->count
; ++i
) {
217 decl_typedef
*def
= defs
->list
[i
];
219 if (def
->type
->type
!= type
->type
&& !strcmp(def
->alias
, type
->name
)) {
220 type
->real
= def
->type
;
224 for (stdtyp
= &psi_std_types
[0]; stdtyp
->type_tag
; ++stdtyp
) {
225 if (!strcmp(type
->name
, stdtyp
->type_name
)) {
226 type
->type
= stdtyp
->type_tag
;
233 static inline int locate_decl_type_struct(decl_structs
*structs
, decl_type
*type
) {
239 for (i
= 0; i
< structs
->count
; ++i
) {
240 if (!strcmp(structs
->list
[i
]->name
, type
->name
)) {
241 type
->strct
= structs
->list
[i
];
248 static inline int validate_decl_type(PSI_Data
*data
, decl_type
*type
) {
249 switch (type
->type
) {
251 if (!data
->defs
|| !locate_decl_type_alias(data
->defs
, type
)) {
255 return validate_decl_type(data
, type
->real
);
259 if (!data
->structs
|| !locate_decl_type_struct(data
->structs
, type
)) {
266 static inline int validate_decl_typedef(PSI_Data
*data
, decl_typedef
*def
) {
267 if (!validate_decl_type(data
, def
->type
)) {
268 data
->error(PSI_WARNING
, "Type '%s' cannot be aliased to %s'%s'",
269 def
->type
->name
, def
->type
->type
== PSI_T_STRUCT
?"struct ":"",def
->alias
);
272 /* FIXME: check def->alias */
276 static inline int validate_constant(PSI_Data
*data
, constant
*c
) {
281 static inline int validate_decl_arg(PSI_Data
*data
, decl_arg
*arg
) {
282 if (!validate_decl_type(data
, arg
->type
)) {
283 data
->error(PSI_WARNING
, "Cannot use '%s'(%d) as type for '%s'",
284 arg
->type
->name
, arg
->type
->type
, arg
->var
->name
);
290 static inline int validate_decl_struct(PSI_Data
*data
, decl_struct
*s
) {
293 for (i
= 0; i
< s
->args
->count
; ++i
) {
294 if (!validate_decl_arg(data
, s
->args
->args
[i
])) {
299 for (i
= 0; i
< s
->args
->count
; ++i
) {
300 decl_arg
*darg
= s
->args
->args
[i
];
302 if (!validate_decl_arg(data
, darg
)) {
306 ZEND_ASSERT(!darg
->var
->arg
|| darg
->var
->arg
== darg
);
307 darg
->var
->arg
= darg
;
312 if (darg
->var
->pointer_level
&& (!darg
->var
->array_size
|| darg
->var
->pointer_level
== 1)) {
315 t
= real_decl_type(darg
->type
)->type
;
319 decl_arg
*last
= s
->args
->args
[i
-1];
320 darg
->layout
= init_decl_struct_layout(
321 psi_t_align(t
, last
->layout
->pos
+ last
->layout
->len
),
322 psi_t_size(t
) * darg
->var
->array_size
);
324 darg
->layout
= init_decl_struct_layout(0, psi_t_size(t
));
327 if (s
->size
< darg
->layout
->pos
+ darg
->layout
->len
) {
328 s
->size
= darg
->layout
->pos
+ darg
->layout
->len
;
334 static const char * const abi_ccs
[] = {
336 "extern", /* > - all the same */
341 static inline int validate_decl_abi(PSI_Data
*data
, decl_abi
*abi
) {
344 for (i
= 0; i
< sizeof(abi_ccs
)/sizeof(char*); ++i
) {
345 if (strcasecmp(abi
->convention
, abi_ccs
[i
])) {
349 data
->error(PSI_WARNING
, "Invalid calling convention: '%s'", abi
->convention
);
352 static inline int validate_decl_func(PSI_Data
*data
, void *dl
, decl
*decl
, decl_arg
*func
)
354 struct psi_func_redir
*redir
;
356 if (!strcmp(func
->var
->name
, "dlsym")) {
357 data
->error(PSI_WARNING
, "Cannot dlsym dlsym (sic!)");
361 if (!validate_decl_arg(data
, func
)) {
364 for (redir
= &psi_func_redirs
[0]; redir
->name
; ++redir
) {
365 if (!strcmp(func
->var
->name
, redir
->name
)) {
366 decl
->call
.sym
= redir
->func
;
369 if (!decl
->call
.sym
) {
371 # define RTLD_NEXT ((void *) -1l)
373 decl
->call
.sym
= dlsym(dl
?: RTLD_NEXT
, func
->var
->name
);
374 if (!decl
->call
.sym
) {
375 data
->error(PSI_WARNING
, "Failed to locate symbol '%s': %s",
376 func
->var
->name
, dlerror());
382 static inline int validate_decl(PSI_Data
*data
, void *dl
, decl
*decl
) {
383 if (!validate_decl_abi(data
, decl
->abi
)) {
386 if (!validate_decl_func(data
, dl
, decl
, decl
->func
)) {
392 for (i
= 0; i
< decl
->args
->count
; ++i
) {
393 if (!validate_decl_arg(data
, decl
->args
->args
[i
])) {
400 static inline decl_arg
*locate_decl_var_arg(decl_var
*var
, decl_args
*args
) {
403 for (i
= 0; i
< args
->count
; ++i
) {
404 decl_arg
*arg
= args
->args
[i
];
406 if (!strcmp(var
->name
, arg
->var
->name
)) {
407 ZEND_ASSERT(!var
->arg
|| var
->arg
== arg
);
408 return var
->arg
= arg
;
414 static inline decl_arg
*locate_struct_member(decl_struct
*s
, decl_var
*var
) {
416 return locate_decl_var_arg(var
, s
->args
);
421 static inline int validate_set_value(PSI_Data
*data
, set_value
*set
, decl_arg
*ref
, decl_args
*ref_list
) {
423 decl_type
*ref_type
= real_decl_type(ref
->type
);
424 decl_var
*set_var
= set
->vars
->vars
[0];
426 switch (set
->func
->type
) {
428 set
->func
->handler
= psi_to_bool
;
431 set
->func
->handler
= psi_to_int
;
434 set
->func
->handler
= psi_to_double
;
436 case PSI_T_TO_STRING
:
437 set
->func
->handler
= psi_to_string
;
440 set
->func
->handler
= psi_to_array
;
442 case PSI_T_TO_OBJECT
:
443 set
->func
->handler
= psi_to_object
;
446 set
->func
->handler
= psi_to_void
;
448 EMPTY_SWITCH_DEFAULT_CASE();
451 for (i
= 1; i
< set
->vars
->count
; ++i
) {
452 if (!locate_decl_var_arg(set
->vars
->vars
[i
], ref_list
)) {
459 int is_to_array
= (set
->func
->type
== PSI_T_TO_ARRAY
);
460 int is_pointer_to_struct
= (ref_type
->type
== PSI_T_STRUCT
&& ref
->var
->pointer_level
);
462 if (!is_to_array
&& !is_pointer_to_struct
) {
463 data
->error(E_WARNING
, "Inner `set` statement casts only work with "
464 "to_array() casts on structs or pointers: %s(%s...", set
->func
->name
, set
->vars
->vars
[0]->name
);
469 if (ref_type
->type
== PSI_T_STRUCT
) {
470 /* to_array(struct, to_...) */
471 for (i
= 0; i
< set
->count
; ++i
) {
472 decl_var
*sub_var
= set
->inner
[i
]->vars
->vars
[0];
473 decl_arg
*sub_ref
= locate_struct_member(ref_type
->strct
, sub_var
);
475 set
->inner
[i
]->outer
.set
= set
;
477 if (!validate_set_value(data
, set
->inner
[i
], sub_ref
, ref_type
->strct
->args
)) {
482 } else if (set
->count
== 1) {
483 /* to_array(ptr, to_string(*ptr)) */
484 decl_var
*sub_var
= set
->inner
[0]->vars
->vars
[0];
485 decl_arg
*sub_ref
= locate_decl_var_arg(sub_var
, ref_list
);
487 set
->inner
[0]->outer
.set
= set
;
489 if (strcmp(sub_var
->name
, set_var
->name
)) {
490 data
->error(E_WARNING
, "Inner `set` statement casts on pointers must reference the same variable");
493 if (!validate_set_value(data
, set
->inner
[0], sub_ref
, ref_list
)) {
497 } else if (set
->count
> 1) {
498 data
->error(E_WARNING
, "Inner `set` statement casts on pointers may only occur once");
504 static inline decl
*locate_impl_decl(decls
*decls
, return_stmt
*ret
) {
508 for (i
= 0; i
< decls
->count
; ++i
) {
509 if (!strcmp(decls
->list
[i
]->func
->var
->name
, ret
->set
->vars
->vars
[0]->name
)) {
510 ret
->decl
= decls
->list
[i
]->func
;
511 return decls
->list
[i
];
518 static inline int validate_impl_ret_stmt(PSI_Data
*data
, impl
*impl
) {
521 /* we must have exactly one ret stmt delcaring the native func to call */
522 /* and which type cast to apply */
523 if (impl
->stmts
->ret
.count
!= 1) {
524 if (impl
->stmts
->ret
.count
> 1) {
525 data
->error(PSI_WARNING
, "Too many `return` statements for implmentation %s;"
526 " found %zu, exactly one is needed",
527 impl
->func
->name
, impl
->stmts
->ret
.count
);
529 data
->error(PSI_WARNING
, "Missing `return` statement for implementation %s",
535 ret
= impl
->stmts
->ret
.list
[0];
537 if (!(impl
->decl
= locate_impl_decl(data
->decls
, ret
))) {
538 data
->error(PSI_WARNING
, "Missing declaration for implementation %s",
543 if (!validate_set_value(data
, ret
->set
, ret
->decl
, impl
->decl
->args
)) {
547 impl
->decl
->impl
= impl
;
551 static inline int validate_impl_let_stmts(PSI_Data
*data
, impl
*impl
) {
553 /* we can have multiple let stmts */
554 /* check that we have a let stmt for every decl arg */
555 if (impl
->decl
->args
) for (i
= 0; i
< impl
->decl
->args
->count
; ++i
) {
556 decl_arg
*darg
= impl
->decl
->args
->args
[i
];
559 for (j
= 0; j
< impl
->stmts
->let
.count
; ++j
) {
560 let_stmt
*let
= impl
->stmts
->let
.list
[j
];
562 if (!strcmp(let
->var
->name
, darg
->var
->name
)) {
569 data
->error(PSI_WARNING
, "Missing `let` statement for arg '%s %.*s%s'"
570 " of declaration '%s' for implementation '%s'",
571 darg
->type
->name
, (int) darg
->var
->pointer_level
, "*****",
572 darg
->var
->name
, impl
->decl
->func
->var
->name
, impl
->func
->name
);
576 /* check that the let_value references a known variable or NULL */
577 for (i
= 0; i
< impl
->stmts
->let
.count
; ++i
) {
578 let_stmt
*let
= impl
->stmts
->let
.list
[i
];
581 if (let
->val
&& let
->val
->func
&& let
->val
->func
->alloc
) {
582 if (!validate_decl_type(data
, let
->val
->func
->alloc
->type
)) {
583 data
->error(PSI_WARNING
, "Cannot use '%s' as type for calloc in `let` statement",
584 let
->val
->func
->alloc
->type
->name
);
588 if (let
->val
&& let
->val
->var
) {
589 if (impl
->func
->args
) for (j
= 0; j
< impl
->func
->args
->count
; ++j
) {
590 impl_arg
*iarg
= impl
->func
->args
->args
[j
];
592 if (!strcmp(let
->val
->var
->name
, iarg
->var
->name
)) {
599 data
->error(PSI_WARNING
, "Unknown value '$%s' of `let` statement"
600 " for variable '%s' of implementation '%s'",
601 let
->val
->var
->name
, let
->var
->name
, impl
->func
->name
);
608 static inline int validate_impl_set_stmts(PSI_Data
*data
, impl
*impl
) {
610 /* we can have any count of set stmts; processing out vars */
611 /* check that set stmts reference known variables */
612 for (i
= 0; i
< impl
->stmts
->set
.count
; ++i
) {
613 set_stmt
*set
= impl
->stmts
->set
.list
[i
];
616 if (impl
->func
->args
) for (j
= 0; j
< impl
->func
->args
->count
; ++j
) {
617 impl_arg
*iarg
= impl
->func
->args
->args
[j
];
619 if (!strcmp(set
->var
->name
, iarg
->var
->name
)) {
626 data
->error(PSI_WARNING
, "Unknown variable '$%s' of `set` statement"
627 " of implementation '%s'",
628 set
->var
->name
, impl
->func
->name
);
632 for (j
= 0; j
< set
->val
->vars
->count
; ++j
) {
633 decl_var
*set_var
= set
->val
->vars
->vars
[j
];
636 if (impl
->decl
->args
) for (k
= 0; k
< impl
->decl
->args
->count
; ++k
) {
637 decl_arg
*set_arg
= impl
->decl
->args
->args
[k
];
639 if (!strcmp(set_var
->name
, set_arg
->var
->name
)) {
641 set_var
->arg
= set_arg
;
642 if (!validate_set_value(data
, set
->val
, set_arg
, impl
->decl
->args
)) {
650 data
->error(PSI_WARNING
, "Unknown value '%s' of `set` statement"
651 " for variable '$%s' of implementation '%s'",
652 set_var
->name
, set
->arg
->var
->name
, impl
->func
->name
);
659 static inline decl
*locate_free_decl(decls
*decls
, free_call
*f
) {
663 for (i
= 0; i
< decls
->count
; ++i
) {
664 if (!strcmp(decls
->list
[i
]->func
->var
->name
, f
->func
)) {
665 f
->decl
= decls
->list
[i
];
666 return decls
->list
[i
];
673 static inline int validate_impl_free_stmts(PSI_Data
*data
, impl
*impl
) {
675 /* we can have any count of free stmts; freeing any out vars */
676 for (i
= 0; i
< impl
->stmts
->fre
.count
; ++i
) {
677 free_stmt
*fre
= impl
->stmts
->fre
.list
[i
];
679 for (j
= 0; j
< fre
->calls
->count
; ++j
) {
680 free_call
*free_call
= fre
->calls
->list
[j
];
682 /* first find the decl of the free func */
683 if (!locate_free_decl(data
->decls
, free_call
)) {
684 data
->error(PSI_WARNING
, "Unknown function '%s' in `free` statement"
685 " of implementation '%s'", free_call
->func
, impl
->func
->name
);
688 if (!impl
->decl
->args
) {
689 data
->error(PSI_WARNING
, "Declaration '%s' of implementation '%s'"
690 " does not have any arguments to free",
691 impl
->decl
->func
->var
->name
, impl
->func
->name
);
694 /* now check for known vars */
695 for (l
= 0; l
< free_call
->vars
->count
; ++l
) {
697 decl_var
*free_var
= free_call
->vars
->vars
[l
];
699 for (k
= 0; k
< impl
->decl
->args
->count
; ++k
) {
700 decl_arg
*free_arg
= impl
->decl
->args
->args
[k
];
702 if (!strcmp(free_var
->name
, free_arg
->var
->name
)) {
704 free_var
->arg
= free_arg
;
710 data
->error(PSI_WARNING
, "Unknown variable '%s' of `free` statement"
711 " of implementation '%s'",
712 free_var
->name
, impl
->func
->name
);
720 static inline int validate_impl_stmts(PSI_Data
*data
, impl
*impl
) {
722 data
->error(PSI_WARNING
, "Missing body for implementation %s!",
727 if (!validate_impl_ret_stmt(data
, impl
)) {
731 if (!validate_impl_let_stmts(data
, impl
)) {
734 if (!validate_impl_set_stmts(data
, impl
)) {
737 if (!validate_impl_free_stmts(data
, impl
)) {
744 PSI_Context
*PSI_ContextInit(PSI_Context
*C
, PSI_ContextOps
*ops
, PSI_ContextErrorFunc error
)
748 struct psi_predef_type
*predef_type
;
749 struct psi_predef_const
*predef_const
;
750 struct psi_predef_struct
*predef_struct
;
751 struct psi_predef_decl
*predef_decl
;
754 C
= malloc(sizeof(*C
));
756 memset(C
, 0, sizeof(*C
));
765 ZEND_ASSERT(ops
->call
!= NULL
);
766 ZEND_ASSERT(ops
->compile
!= NULL
);
768 /* build up predefs in a temporary PSI_Data for validation */
769 memset(&T
, 0, sizeof(T
));
772 for (predef_type
= &psi_predef_types
[0]; predef_type
->type_tag
; ++predef_type
) {
773 decl_type
*type
= init_decl_type(predef_type
->type_tag
, predef_type
->type_name
);
774 decl_typedef
*def
= init_decl_typedef(predef_type
->alias
, type
);
776 T
.defs
= add_decl_typedef(T
.defs
, def
);
778 for (predef_const
= &psi_predef_consts
[0]; predef_const
->type_tag
; ++predef_const
) {
779 impl_def_val
*val
= init_impl_def_val(predef_const
->val_type_tag
, predef_const
->val_text
);
780 const_type
*type
= init_const_type(predef_const
->type_tag
, predef_const
->type_name
);
781 constant
*constant
= init_constant(type
, predef_const
->var_name
, val
);
783 T
.consts
= add_constant(T
.consts
, constant
);
785 for (predef_struct
= &psi_predef_structs
[0]; predef_struct
->type_tag
; ++predef_struct
) {
786 struct psi_predef_struct
*member
;
787 decl_args
*dargs
= init_decl_args(NULL
);
788 decl_struct
*dstruct
= init_decl_struct(predef_struct
->var_name
, dargs
);
790 dstruct
->size
= predef_struct
->size
;
791 for (member
= &predef_struct
[1]; member
->type_tag
; ++member
) {
796 type
= init_decl_type(member
->type_tag
, member
->type_name
);
797 dvar
= init_decl_var(member
->var_name
, member
->pointer_level
, member
->array_size
);
798 darg
= init_decl_arg(type
, dvar
);
799 darg
->layout
= init_decl_struct_layout(member
->offset
, member
->size
);
800 dargs
= add_decl_arg(dargs
, darg
);
803 T
.structs
= add_decl_struct(T
.structs
, dstruct
);
804 predef_struct
= member
;
806 for (predef_decl
= &psi_predef_decls
[0]; predef_decl
->type_tag
; ++predef_decl
) {
807 struct psi_predef_decl
*farg
;
808 decl_type
*ftype
= init_decl_type(predef_decl
->type_tag
, predef_decl
->type_name
);
809 decl_var
*fname
= init_decl_var(predef_decl
->var_name
, predef_decl
->pointer_level
, predef_decl
->array_size
);
810 decl_arg
*func
= init_decl_arg(ftype
, fname
);
811 decl_args
*args
= init_decl_args(NULL
);
812 decl
*decl
= init_decl(init_decl_abi("default"), func
, args
);
815 for (farg
= &predef_decl
[1]; farg
->type_tag
; ++farg
) {
816 decl_type
*arg_type
= init_decl_type(farg
->type_tag
, farg
->type_name
);
817 decl_var
*arg_var
= init_decl_var(farg
->var_name
, farg
->pointer_level
, farg
->array_size
);
818 decl_arg
*darg
= init_decl_arg(arg_type
, arg_var
);
819 args
= add_decl_arg(args
, darg
);
822 T
.decls
= add_decl(T
.decls
, decl
);
826 for (i
= 0; i
< T
.defs
->count
; ++i
) {
827 decl_typedef
*def
= T
.defs
->list
[i
];
829 if (validate_decl_typedef(&T
, def
)) {
830 C
->defs
= add_decl_typedef(C
->defs
, def
);
834 for (i
= 0; i
< T
.consts
->count
; ++i
) {
835 constant
*constant
= T
.consts
->list
[i
];
837 if (validate_constant(&T
, constant
)) {
838 C
->consts
= add_constant(C
->consts
, constant
);
842 for (i
= 0; i
< T
.structs
->count
; ++i
) {
843 decl_struct
*dstruct
= T
.structs
->list
[i
];
845 if (validate_decl_struct(&T
, dstruct
)) {
846 C
->structs
= add_decl_struct(C
->structs
, dstruct
);
850 for (i
= 0; i
< T
.decls
->count
; ++i
) {
851 decl
*decl
= T
.decls
->list
[i
];
853 if (validate_decl(&T
, NULL
, decl
)) {
854 C
->decls
= add_decl(C
->decls
, decl
);
859 C
->data
= malloc(sizeof(*C
->data
));
860 PSI_DataExchange(C
->data
, &T
);
865 int PSI_ContextValidate(PSI_Context
*C
, PSI_Parser
*P
)
868 void *dlopened
= NULL
;
869 size_t count
= C
->count
++;
871 C
->data
= realloc(C
->data
, C
->count
* sizeof(*C
->data
));
872 D
= PSI_DataExchange(&C
->data
[count
], PSI_DATA(P
));
877 for (i
= 0; i
< D
->defs
->count
; ++i
) {
878 if (validate_decl_typedef(PSI_DATA(C
), D
->defs
->list
[i
])) {
879 C
->defs
= add_decl_typedef(C
->defs
, D
->defs
->list
[i
]);
886 for (i
= 0; i
< D
->structs
->count
; ++i
) {
887 if (validate_decl_struct(PSI_DATA(C
), D
->structs
->list
[i
])) {
888 C
->structs
= add_decl_struct(C
->structs
, D
->structs
->list
[i
]);
895 for (i
= 0; i
< D
->consts
->count
; ++i
) {
896 if (validate_constant(PSI_DATA(C
), D
->consts
->list
[i
])) {
897 C
->consts
= add_constant(C
->consts
, D
->consts
->list
[i
]);
902 if (!validate_lib(D
, &dlopened
)) {
906 add_decl_lib(&C
->psi
.libs
, dlopened
);
911 for (i
= 0; i
< D
->decls
->count
; ++i
) {
912 if (validate_decl(PSI_DATA(C
), dlopened
, D
->decls
->list
[i
])) {
913 C
->decls
= add_decl(C
->decls
, D
->decls
->list
[i
]);
920 for (i
= 0; i
< D
->impls
->count
; ++i
) {
921 if (validate_impl_stmts(PSI_DATA(C
), D
->impls
->list
[i
])) {
922 C
->impls
= add_impl(C
->impls
, D
->impls
->list
[i
]);
930 static int psi_select_dirent(const struct dirent
*entry
)
933 #define FNM_CASEFOLD 0
935 return 0 == fnmatch("*.psi", entry
->d_name
, FNM_CASEFOLD
);
938 void PSI_ContextBuild(PSI_Context
*C
, const char *paths
)
940 int i
, n
, flags
= psi_check_env("PSI_DEBUG") ? PSI_PARSER_DEBUG
: 0;
941 char *sep
= NULL
, *cpy
= strdup(paths
), *ptr
= cpy
;
942 struct dirent
**entries
= NULL
;
946 sep
= strchr(ptr
, ':');
952 n
= php_scandir(ptr
, &entries
, psi_select_dirent
, alphasort
);
955 for (i
= 0; i
< n
; ++i
) {
956 char psi
[MAXPATHLEN
];
959 if (MAXPATHLEN
<= slprintf(psi
, MAXPATHLEN
, "%s/%s", ptr
, entries
[i
]->d_name
)) {
960 C
->error(PSI_WARNING
, "Path to PSI file too long: %s/%s",
961 ptr
, entries
[i
]->d_name
);
963 if (!PSI_ParserInit(&P
, psi
, C
->error
, flags
)) {
964 C
->error(PSI_WARNING
, "Failed to init PSI parser (%s): %s",
965 psi
, strerror(errno
));
969 while (0 < PSI_ParserScan(&P
)) {
970 PSI_ParserParse(&P
, PSI_TokenAlloc(&P
));
971 if (P
.num
== PSI_T_EOF
) {
976 PSI_ParserParse(&P
, NULL
);
977 PSI_ContextValidate(C
, &P
);
983 for (i
= 0; i
< n
; ++i
) {
993 if (PSI_ContextCompile(C
) && SUCCESS
!= zend_register_functions(NULL
, C
->closures
, NULL
, MODULE_PERSISTENT
)) {
994 C
->error(PSI_WARNING
, "Failed to register functions!");
1001 zend_function_entry
*PSI_ContextCompile(PSI_Context
*C
)
1008 zc
.flags
= CONST_PERSISTENT
|CONST_CS
;
1009 zc
.module_number
= EG(current_module
)->module_number
;
1011 for (i
= 0; i
< C
->consts
->count
; ++i
) {
1012 constant
*c
= C
->consts
->list
[i
];
1014 zc
.name
= zend_string_init(c
->name
+ (c
->name
[0] == '\\'), strlen(c
->name
) - (c
->name
[0] == '\\'), 1);
1015 ZVAL_NEW_STR(&zc
.value
, zend_string_init(c
->val
->text
, strlen(c
->val
->text
), 1));
1017 switch (c
->type
->type
) {
1019 convert_to_boolean(&zc
.value
);
1022 convert_to_long(&zc
.value
);
1025 convert_to_double(&zc
.value
);
1028 zend_register_constant(&zc
);
1032 return C
->closures
= C
->ops
->compile(C
);
1036 void PSI_ContextCall(PSI_Context
*C
, impl_val
*ret_val
, decl
*decl
)
1039 C
->ops
->call(C
, ret_val
, decl
);
1042 static inline void dump_decl_type(int fd
, decl_type
*t
) {
1052 dprintf(fd
, "%s%s", pre
, t
->name
);
1054 static inline void dump_decl_var(int fd
, decl_var
*v
) {
1055 dprintf(fd
, "%.*s%s", v
->pointer_level
-!!v
->array_size
, "**********", v
->name
);
1056 if (v
->array_size
) {
1057 dprintf(fd
, "[%u]", v
->array_size
);
1060 static inline void dump_decl_arg(int fd
, decl_arg
*a
) {
1061 dump_decl_type(fd
, a
->type
);
1063 dump_decl_var(fd
, a
->var
);
1065 static inline void dump_level(int fd
, unsigned level
) {
1066 dprintf(fd
, "%.*s", level
, "\t\t\t\t\t\t\t\t\t");
1068 static inline void dump_impl_set_value(int fd
, set_value
*set
, unsigned level
) {
1072 /* only if not directly after `set ...` */
1073 dump_level(fd
, level
);
1075 dprintf(fd
, "%s(", set
->func
->name
);
1077 for (i
= 0; i
< set
->vars
->count
; ++i
) {
1078 decl_var
*svar
= set
->vars
->vars
[i
];
1082 dump_decl_var(fd
, svar
);
1086 for (i
= 0; i
< set
->count
; ++i
) {
1087 dump_impl_set_value(fd
, set
->inner
[i
], level
+1);
1089 /* only if inner stmts, i.e. with new lines, were dumped */
1090 dump_level(fd
, level
);
1093 dprintf(fd
, "),\n");
1095 dprintf(fd
, ");\n");
1098 void PSI_ContextDump(PSI_Context
*C
, int fd
)
1103 if (C
->ops
== PSI_Libjit()) {
1104 dprintf(fd
, "// psi.engine=jit\n");
1108 if (C
->ops
== PSI_Libffi()) {
1109 dprintf(fd
, "// psi.engine=ffi\n");
1115 for (i
= 0; i
< C
->defs
->count
; ++i
) {
1116 decl_typedef
*tdef
= C
->defs
->list
[i
];
1118 dprintf(fd
, "typedef ");
1119 dump_decl_type(fd
, tdef
->type
);
1120 dprintf(fd
, " %s;\n", tdef
->alias
);
1126 for (i
= 0; i
< C
->structs
->count
; ++i
) {
1127 decl_struct
*strct
= C
->structs
->list
[i
];
1129 dprintf(fd
, "struct %s::(%zu) {\n", strct
->name
, strct
->size
);
1130 if (strct
->args
) for (j
= 0; j
< strct
->args
->count
; ++j
) {
1131 decl_arg
*sarg
= strct
->args
->args
[j
];
1134 dump_decl_arg(fd
, sarg
);
1135 dprintf(fd
, "::(%zu, %zu);\n", sarg
->layout
->pos
, sarg
->layout
->len
);
1142 for (i
= 0; i
< C
->consts
->count
; ++i
) {
1143 constant
*cnst
= C
->consts
->list
[i
];
1145 dprintf(fd
, "const %s %s = ", cnst
->type
->name
, cnst
->name
);
1146 if (cnst
->val
->type
== PSI_T_QUOTED_STRING
) {
1147 dprintf(fd
, "\"%s\";\n", cnst
->val
->text
);
1149 dprintf(fd
, "%s;\n", cnst
->val
->text
);
1155 for (i
= 0; i
< C
->decls
->count
; ++i
) {
1156 decl
*decl
= C
->decls
->list
[i
];
1158 dprintf(fd
, "%s ", decl
->abi
->convention
);
1159 dump_decl_arg(fd
, decl
->func
);
1161 if (decl
->args
) for (j
= 0; j
< decl
->args
->count
; ++j
) {
1165 dump_decl_arg(fd
, decl
->args
->args
[j
]);
1167 dprintf(fd
, ");\n");
1172 for (i
= 0; i
< C
->impls
->count
; ++i
) {
1173 impl
*impl
= C
->impls
->list
[i
];
1175 dprintf(fd
, "function %s(", impl
->func
->name
);
1176 if (impl
->func
->args
) for (j
= 0; j
< impl
->func
->args
->count
; ++j
) {
1177 impl_arg
*iarg
= impl
->func
->args
->args
[j
];
1179 dprintf(fd
, "%s%s %s$%s",
1182 iarg
->var
->reference
? "&" : "",
1185 dprintf(fd
, " = %s", iarg
->def
->text
);
1188 dprintf(fd
, ") : %s%s {\n",
1189 impl
->func
->return_reference
? "&":"",
1190 impl
->func
->return_type
->name
);
1192 for (j
= 0; j
< impl
->stmts
->let
.count
; ++j
) {
1193 let_stmt
*let
= impl
->stmts
->let
.list
[j
];
1195 dprintf(fd
, "\tlet %s", let
->var
->name
);
1197 dprintf(fd
, " = %s", let
->val
->is_reference
? "&" : "");
1198 if (let
->val
->func
) {
1199 dprintf(fd
, "%s(", let
->val
->func
->name
);
1200 if (let
->val
->func
->alloc
) {
1201 dprintf(fd
, "%zu, ", let
->val
->func
->alloc
->n
);
1202 dump_decl_type(fd
, let
->val
->func
->alloc
->type
);
1204 dprintf(fd
, "$%s", let
->val
->var
->name
);
1206 dprintf(fd
, ");\n");
1208 dprintf(fd
, "NULL;\n");
1212 for (j
= 0; j
< impl
->stmts
->ret
.count
; ++j
) {
1213 return_stmt
*ret
= impl
->stmts
->ret
.list
[j
];
1215 dprintf(fd
, "\treturn ");
1216 dump_impl_set_value(fd
, ret
->set
, 1);
1218 for (j
= 0; j
< impl
->stmts
->set
.count
; ++j
) {
1219 set_stmt
*set
= impl
->stmts
->set
.list
[j
];
1221 dprintf(fd
, "\tset $%s = ", set
->var
->name
);
1222 dump_impl_set_value(fd
, set
->val
, 1);
1224 for (j
= 0; j
< impl
->stmts
->fre
.count
; ++j
) {
1225 free_stmt
*fre
= impl
->stmts
->fre
.list
[j
];
1227 dprintf(fd
, "\tfree ");
1228 for (k
= 0; k
< fre
->calls
->count
; ++k
) {
1229 free_call
*call
= fre
->calls
->list
[k
];
1234 dprintf(fd
, "%s(", call
->func
);
1235 for (l
= 0; l
< call
->vars
->count
; ++l
) {
1236 decl_var
*fvar
= call
->vars
->vars
[l
];
1238 dump_decl_var(fd
, fvar
);
1240 dprintf(fd
, ");\n");
1250 void PSI_ContextDtor(PSI_Context
*C
)
1253 zend_function_entry
*zfe
;
1259 free_decl_libs(&C
->psi
.libs
);
1262 for (i
= 0; i
< C
->count
; ++i
) {
1263 PSI_DataDtor(&C
->data
[i
]);
1269 for (zfe
= C
->closures
; zfe
->fname
; ++zfe
) {
1270 free((void *) zfe
->arg_info
);
1276 if (C
->consts
->list
) {
1277 free(C
->consts
->list
);
1282 if (C
->defs
->list
) {
1283 free(C
->defs
->list
);
1288 if (C
->structs
->list
) {
1289 free(C
->structs
->list
);
1294 if (C
->decls
->list
) {
1295 free(C
->decls
->list
);
1300 if (C
->impls
->list
) {
1301 free(C
->impls
->list
);
1306 memset(C
, 0, sizeof(*C
));
1309 void PSI_ContextFree(PSI_Context
**C
)
1312 PSI_ContextDtor(*C
);