8 #include "ext/standard/info.h"
9 #include "zend_exceptions.h"
10 #include "zend_constants.h"
11 #include "zend_operators.h"
18 # define PSI_ENGINE "jit"
23 # define PSI_ENGINE "ffi"
26 ZEND_DECLARE_MODULE_GLOBALS(psi
);
29 STD_PHP_INI_ENTRY("psi.engine", PSI_ENGINE
, PHP_INI_SYSTEM
, OnUpdateString
, engine
, zend_psi_globals
, psi_globals
)
30 STD_PHP_INI_ENTRY("psi.directory", "psi.d", PHP_INI_SYSTEM
, OnUpdateString
, directory
, zend_psi_globals
, psi_globals
)
33 static zend_object_handlers psi_object_handlers
;
34 static zend_class_entry
*psi_class_entry
;
36 void psi_error(int type
, const char *msg
, ...)
42 vslprintf(buf
, 0x1000, msg
, argv
);
48 size_t psi_t_alignment(token_t t
)
50 #define PSI_ALIGNOF(T) case PSI_T_## T: return ALIGNOF_## T ##_T;
63 return ALIGNOF_DOUBLE
;
65 return ALIGNOF_VOID_P
;
66 EMPTY_SWITCH_DEFAULT_CASE();
71 size_t psi_t_size(token_t t
)
73 #define PSI_SIZEOF(T) case PSI_T_## T : return SIZEOF_## T ##_T;
89 EMPTY_SWITCH_DEFAULT_CASE();
94 size_t psi_t_align(token_t t
, size_t s
)
96 size_t a
= psi_t_alignment(t
);
97 return ((s
- 1) | (a
- 1)) + 1;
100 int psi_internal_type(impl_type
*type
)
102 switch (type
->type
) {
119 zend_internal_arg_info
*psi_internal_arginfo(impl
*impl
)
122 zend_internal_arg_info
*aip
;
123 zend_internal_function_info
*fi
;
125 aip
= calloc(impl
->func
->args
->count
+ 1 + !!impl
->func
->args
->vararg
.name
, sizeof(*aip
));
127 fi
= (zend_internal_function_info
*) &aip
[0];
129 fi
->required_num_args
= psi_num_min_args(impl
);
130 fi
->return_reference
= impl
->func
->return_reference
;
131 fi
->type_hint
= psi_internal_type(impl
->func
->return_type
);
133 if (impl
->func
->args
->vararg
.name
) {
134 impl_arg
*vararg
= impl
->func
->args
->vararg
.name
;
135 zend_internal_arg_info
*ai
= &aip
[impl
->func
->args
->count
];
137 ai
->name
= vararg
->var
->name
;
138 ai
->type_hint
= psi_internal_type(vararg
->type
);
139 if (vararg
->var
->reference
) {
140 ai
->pass_by_reference
= 1;
145 for (i
= 0; i
< impl
->func
->args
->count
; ++i
) {
146 impl_arg
*iarg
= impl
->func
->args
->args
[i
];
147 zend_internal_arg_info
*ai
= &aip
[i
+1];
149 ai
->name
= iarg
->var
->name
;
150 ai
->type_hint
= psi_internal_type(iarg
->type
);
151 if (iarg
->var
->reference
) {
152 ai
->pass_by_reference
= 1;
154 if (iarg
->var
->reference
|| (iarg
->def
&& iarg
->def
->type
== PSI_T_NULL
)) {
162 size_t psi_num_min_args(impl
*impl
)
164 size_t i
, n
= impl
->func
->args
->count
;
166 for (i
= 0; i
< impl
->func
->args
->count
; ++i
) {
167 if (impl
->func
->args
->args
[i
]->def
) {
174 void psi_to_void(zval
*return_value
, set_value
*set
, impl_val
*ret_val
)
179 void psi_to_bool(zval
*return_value
, set_value
*set
, impl_val
*ret_val
)
181 psi_to_int(return_value
, set
, ret_val
);
182 convert_to_boolean(return_value
);
185 void psi_to_int(zval
*return_value
, set_value
*set
, impl_val
*ret_val
)
187 decl_var
*var
= set
->vars
->vars
[0];
188 token_t t
= real_decl_type(var
->arg
->type
)->type
;
189 impl_val
*v
= deref_impl_val(ret_val
, var
);
193 RETVAL_DOUBLE((double) v
->fval
);
194 convert_to_long(return_value
);
197 RETVAL_DOUBLE(v
->dval
);
198 convert_to_long(return_value
);
216 #if UINT32_MAX >= ZEND_LONG_MAX
217 if (v
->u32
> ZEND_LONG_MAX
) {
220 RETVAL_STRING(zend_print_ulong_to_buf(&d
[10], v
->u32
));
224 #if UINT32_MAX >= ZEND_LONG_MAX
232 if (v
->u64
> ZEND_LONG_MAX
) {
235 RETVAL_STRING(zend_print_ulong_to_buf(&d
[22], v
->u64
));
240 EMPTY_SWITCH_DEFAULT_CASE();
244 void psi_to_double(zval
*return_value
, set_value
*set
, impl_val
*ret_val
)
246 decl_var
*var
= set
->vars
->vars
[0];
247 token_t t
= real_decl_type(var
->arg
->type
)->type
;
248 impl_val
*v
= deref_impl_val(ret_val
, var
);
252 RETVAL_DOUBLE((double) v
->fval
);
255 RETVAL_DOUBLE(v
->dval
);
258 RETVAL_DOUBLE((double) v
->i8
);
261 RETVAL_DOUBLE((double) v
->u8
);
264 RETVAL_DOUBLE((double) v
->i16
);
267 RETVAL_DOUBLE((double) v
->u16
);
270 RETVAL_DOUBLE((double) v
->i32
);
273 RETVAL_DOUBLE((double) v
->u32
);
276 RETVAL_DOUBLE((double) v
->i64
);
279 RETVAL_DOUBLE((double) v
->u64
);
281 EMPTY_SWITCH_DEFAULT_CASE();
285 void psi_to_string(zval
*return_value
, set_value
*set
, impl_val
*ret_val
)
287 decl_var
*var
= set
->vars
->vars
[0];
288 token_t t
= real_decl_type(var
->arg
->type
)->type
;
294 if (!var
->arg
->var
->pointer_level
) {
295 RETVAL_STRINGL(&ret_val
->cval
, 1);
297 ret_val
= deref_impl_val(ret_val
, var
);
298 if (ret_val
&& ret_val
->ptr
) {
300 RETVAL_STRINGL(ret_val
->ptr
, psi_long_num_exp(set
->num
, set
->outer
.val
));
302 RETVAL_STRING(ret_val
->ptr
);
305 RETVAL_EMPTY_STRING();
310 RETVAL_DOUBLE((double) deref_impl_val(ret_val
, var
)->fval
);
313 RETVAL_DOUBLE(deref_impl_val(ret_val
, var
)->dval
);
316 psi_to_int(return_value
, set
, ret_val
);
319 convert_to_string(return_value
);
323 static impl_val
*iterate(impl_val
*val
, size_t size
, unsigned i
, impl_val
*tmp
)
325 memset(tmp
, 0, sizeof(*tmp
));
326 memcpy(tmp
, ((void*) val
->ptr
) + size
* i
, size
);
330 void psi_from_zval(impl_val
*mem
, decl_arg
*spec
, zval
*zv
, void **tmp
)
332 decl_type
*type
= real_decl_type(spec
->type
);
334 switch (type
->type
) {
336 mem
->fval
= (float) zval_get_double(zv
);
339 mem
->dval
= zval_get_double(zv
);
343 if (spec
->var
->pointer_level
) {
344 zend_string
*zs
= zval_get_string(zv
);
345 *tmp
= mem
->ptr
= estrndup(zs
->val
, zs
->len
);
346 zend_string_release(zs
);
351 mem
->zend
.lval
= zval_get_long(zv
);
356 void *psi_array_to_struct(decl_struct
*s
, HashTable
*arr
)
359 char *mem
= ecalloc(1, s
->size
+ s
->args
->count
* sizeof(void *));
361 if (arr
) for (i
= 0; i
< s
->args
->count
; ++i
) {
362 decl_arg
*darg
= s
->args
->args
[i
];
363 zval
*entry
= zend_hash_str_find_ind(arr
, darg
->var
->name
, strlen(darg
->var
->name
));
369 memset(&tmp
, 0, sizeof(tmp
));
370 psi_from_zval(&val
, darg
, entry
, &tmp
);
371 memcpy(mem
+ darg
->layout
->pos
, &val
, darg
->layout
->len
);
373 ((void **)(mem
+ s
->size
))[j
++] = tmp
;
380 void psi_to_array(zval
*return_value
, set_value
*set
, impl_val
*r_val
)
383 decl_var
*var
= set
->vars
->vars
[0];
384 token_t t
= real_decl_type(var
->arg
->type
)->type
;
385 impl_val tmp
, *ret_val
= deref_impl_val(r_val
, var
);
387 if ((intptr_t) ret_val
->ptr
<= (intptr_t) 0) {
391 array_init(return_value
);
393 if (t
== PSI_T_STRUCT
) {
394 // decl_struct *s = real_decl_type(var->arg->type)->strct;
397 /* explicit member casts */
398 for (i
= 0; i
< set
->count
; ++i
) {
399 set_value
*sub_set
= set
->inner
[i
];
400 decl_var
*sub_var
= sub_set
->vars
->vars
[0];
402 sub_set
->outer
.val
= r_val
;
405 impl_val
*tmp
= NULL
, *val
;
408 val
= deref_impl_val(struct_member_ref(sub_var
->arg
, ret_val
, &tmp
), sub_var
);
409 sub_set
->func
->handler(&ztmp
, sub_set
, val
);
410 add_assoc_zval(return_value
, sub_var
->name
, &ztmp
);
421 if (var
->arg
->var
->array_size
) {
422 /* to_array(foo[NUMBER]) */
423 for (i
= 0; i
< var
->arg
->var
->array_size
; ++i
) {
424 size_t size
= psi_t_size(var
->arg
->var
->pointer_level
> 1 ? PSI_T_POINTER
: t
);
425 impl_val
*ptr
= iterate(ret_val
, size
, i
, &tmp
);
430 ZVAL_DOUBLE(&ele
, (double) ptr
->fval
);
433 ZVAL_DOUBLE(&ele
, ptr
->dval
);
436 ZVAL_LONG(&ele
, ptr
->lval
);
440 add_next_index_zval(return_value
, &ele
);
443 } else if (set
->num
) {
444 /* to_array(arr_var, num_expr, to_int(*arr_var))
447 zend_long i
, n
= psi_long_num_exp(set
->num
, set
->outer
.val
);
449 for (i
= 0; i
< n
; ++i
) {
450 size_t size
= psi_t_size(var
->arg
->var
->pointer_level
? PSI_T_POINTER
: t
);
451 impl_val
*ptr
= iterate(ret_val
, size
, i
, &tmp
);
453 set
->inner
[0]->func
->handler(&ele
, set
->inner
[0], ptr
);
454 add_next_index_zval(return_value
, &ele
);
461 void psi_to_object(zval
*return_value
, set_value
*set
, impl_val
*r_val
)
463 decl_var
*var
= set
->vars
->vars
[0];
464 impl_val
*ret_val
= deref_impl_val(r_val
, var
);
467 if ((intptr_t) ret_val
->ptr
> (intptr_t) 0) {
468 object_init_ex(return_value
, psi_class_entry
);
469 obj
= PSI_OBJ(return_value
, NULL
);
470 obj
->data
= ret_val
->ptr
;
476 static inline ZEND_RESULT_CODE
psi_parse_args(zend_execute_data
*execute_data
, impl
*impl
)
479 zval
*zarg
= ZEND_CALL_ARG(execute_data
, 0);
481 zend_error_handling zeh
;
483 zend_replace_error_handling(EH_THROW
, zend_exception_get_default(), &zeh
);
485 if (!impl
->func
->args
->count
) {
488 rv
= zend_parse_parameters_none();
489 zend_restore_error_handling(&zeh
);
493 ZEND_PARSE_PARAMETERS_START(psi_num_min_args(impl
), impl
->func
->args
->vararg
.name
? -1 : impl
->func
->args
->count
)
495 if (impl
->func
->args
->vararg
.name
&& _i
>= impl
->func
->args
->count
) {
496 impl_arg
*varg
= impl
->func
->args
->vararg
.name
;
497 iarg
= init_impl_arg(
498 init_impl_type(varg
->type
->type
, varg
->type
->name
),
499 init_impl_var(varg
->var
->name
, varg
->var
->reference
),
503 if (_i
== impl
->func
->args
->count
) {
504 impl
->func
->args
->vararg
.args
= init_impl_args(iarg
);
506 add_impl_arg(impl
->func
->args
->vararg
.args
, iarg
);
509 iarg
= impl
->func
->args
->args
[_i
];
514 if (PSI_T_BOOL
== iarg
->type
->type
) {
515 Z_PARAM_BOOL(iarg
->val
.zend
.bval
);
516 } else if (PSI_T_INT
== iarg
->type
->type
) {
517 Z_PARAM_LONG(iarg
->val
.zend
.lval
);
518 } else if (PSI_T_FLOAT
== iarg
->type
->type
|| PSI_T_DOUBLE
== iarg
->type
->type
) {
519 Z_PARAM_DOUBLE(iarg
->val
.dval
);
520 } else if (PSI_T_STRING
== iarg
->type
->type
) {
521 Z_PARAM_STR_EX(iarg
->val
.zend
.str
, 1, iarg
->var
->reference
);
522 if (iarg
->val
.zend
.str
) {
523 zend_string_addref(iarg
->val
.zend
.str
);
525 } else if (PSI_T_ARRAY
== iarg
->type
->type
) {
527 } else if (PSI_T_OBJECT
== iarg
->type
->type
) {
529 } else if (PSI_T_MIXED
== iarg
->type
->type
) {
532 error_code
= ZPP_ERROR_FAILURE
;
535 if (_i
< _num_args
) {
538 ZEND_PARSE_PARAMETERS_END_EX(
539 zend_restore_error_handling(&zeh
);
543 /* set up defaults */
544 for (i
= 0; i
< impl
->func
->args
->count
; ++i
) {
545 iarg
= impl
->func
->args
->args
[i
];
547 if (i
< EX_NUM_ARGS()) {
549 ZVAL_DEREF(iarg
->_zv
);
550 } else if (iarg
->def
) {
551 switch (iarg
->type
->type
) {
553 iarg
->val
.zend
.bval
= iarg
->def
->type
== PSI_T_TRUE
? 1 : 0;
556 iarg
->val
.zend
.lval
= zend_atol(iarg
->def
->text
, strlen(iarg
->def
->text
));
560 iarg
->val
.dval
= zend_strtod(iarg
->def
->text
, NULL
);
564 iarg
->val
.zend
.str
= zend_string_init(&iarg
->def
->text
[1], strlen(iarg
->def
->text
) - 2, 0);
570 zend_restore_error_handling(&zeh
);
574 static inline void *psi_do_calloc(let_calloc
*alloc
)
576 zend_long n
= psi_long_num_exp(alloc
->nmemb
, NULL
), s
= psi_long_num_exp(alloc
->size
, NULL
);
577 void *mem
= safe_emalloc(n
, s
, sizeof(void *));
578 memset(mem
, 0, n
* s
+ sizeof(void *));
582 static inline token_t
psi_let_fn(token_t impl_type
) {
585 return PSI_T_BOOLVAL
;
590 return PSI_T_FLOATVAL
;
597 static inline ZEND_RESULT_CODE
psi_let_val(token_t let_func
, impl_arg
*iarg
, impl_val
*arg_val
, decl_struct
*strct
, void **to_free
)
601 if (iarg
->type
->type
== PSI_T_BOOL
) {
602 arg_val
->cval
= iarg
->val
.zend
.bval
;
604 arg_val
->cval
= zend_is_true(iarg
->_zv
);
608 if (iarg
->type
->type
== PSI_T_INT
) {
609 arg_val
->lval
= iarg
->val
.zend
.lval
;
611 arg_val
->lval
= zval_get_long(iarg
->_zv
);
615 if (iarg
->type
->type
== PSI_T_FLOAT
|| iarg
->type
->type
== PSI_T_DOUBLE
) {
616 arg_val
->dval
= iarg
->val
.dval
;
618 arg_val
->dval
= zval_get_double(iarg
->_zv
);
623 if (iarg
->type
->type
== PSI_T_STRING
) {
624 if (iarg
->val
.zend
.str
) {
625 arg_val
->ptr
= estrdup(iarg
->val
.zend
.str
->val
);
626 *to_free
= arg_val
->ptr
;
631 zend_string
*zs
= zval_get_string(iarg
->_zv
);
632 arg_val
->ptr
= estrdup(zs
->val
);
633 *to_free
= arg_val
->ptr
;
634 zend_string_release(zs
);
636 if (PSI_T_PATHVAL
== let_func
) {
637 if (SUCCESS
!= php_check_open_basedir(arg_val
->ptr
)) {
644 if (iarg
->type
->type
== PSI_T_STRING
) {
645 if (iarg
->val
.zend
.str
) {
646 arg_val
->lval
= iarg
->val
.zend
.str
->len
;
651 zend_string
*zs
= zval_get_string(iarg
->_zv
);
652 arg_val
->lval
= zs
->len
;
653 zend_string_release(zs
);
657 if (iarg
->type
->type
== PSI_T_ARRAY
) {
658 arg_val
->ptr
= psi_array_to_struct(strct
, HASH_OF(iarg
->_zv
));
659 *to_free
= arg_val
->ptr
;
663 if (iarg
->type
->type
== PSI_T_OBJECT
) {
666 if (!instanceof_function(Z_OBJCE_P(iarg
->_zv
), psi_class_entry
)) {
670 obj
= PSI_OBJ(iarg
->_zv
, NULL
);
671 arg_val
->ptr
= obj
->data
;
674 EMPTY_SWITCH_DEFAULT_CASE();
679 static inline void *psi_do_let(let_stmt
*let
)
681 decl_arg
*darg
= let
->var
->arg
;
682 impl_val
*arg_val
= darg
->ptr
;
685 switch (let
->val
? let
->val
->kind
: PSI_LET_NULL
) {
687 arg_val
->ptr
= deref_impl_val(let
->val
->data
.var
->arg
->val
.ptr
, let
->var
);
690 if (darg
->var
->array_size
) {
691 arg_val
->ptr
= ecalloc(darg
->var
->array_size
, sizeof(*arg_val
));
692 darg
->mem
= arg_val
->ptr
;
694 memset(arg_val
, 0, sizeof(*arg_val
));
698 arg_val
->ptr
= psi_do_calloc(let
->val
->data
.alloc
);
699 darg
->mem
= arg_val
->ptr
;
702 arg_val
->zend
.lval
= psi_long_num_exp(let
->val
->data
.num
, NULL
);
705 iarg
= let
->val
->data
.func
->arg
;
707 if (SUCCESS
!= psi_let_val(let
->val
->data
.func
->type
, iarg
, real_decl_type(darg
->type
)->strct
, darg
->ptr
, &darg
->mem
)) {
712 if (let
->val
&& let
->val
->flags
.one
.is_reference
) {
713 return let
->ptr
= &darg
->ptr
;
715 return let
->ptr
= darg
->ptr
;
719 static inline void psi_do_set(zval
*return_value
, set_value
*set
)
721 zval_dtor(return_value
);
722 set
->func
->handler(return_value
, set
, set
->vars
->vars
[0]->arg
->ptr
);
725 static inline void psi_do_return(zval
*return_value
, return_stmt
*ret
)
727 ret
->set
->func
->handler(return_value
, ret
->set
, ret
->set
->vars
->vars
[0]->arg
->ptr
);
730 static inline void psi_do_free(free_stmt
*fre
)
734 for (i
= 0; i
< fre
->calls
->count
; ++i
) {
735 free_call
*f
= fre
->calls
->list
[i
];
737 for (j
= 0; j
< f
->vars
->count
; ++j
) {
738 decl_var
*dvar
= f
->vars
->vars
[j
];
739 decl_arg
*darg
= dvar
->arg
;
741 f
->decl
->call
.args
[j
] = &darg
->val
;
744 /* FIXME: check in validate_* that free functions return scalar */
745 PSI_ContextCall(&PSI_G(context
), &f
->decl
->call
);
749 static inline void psi_do_clean(impl
*impl
)
753 for (i
= 0; i
< impl
->func
->args
->count
; ++i
) {
754 impl_arg
*iarg
= impl
->func
->args
->args
[i
];
756 switch (iarg
->type
->type
) {
758 if (iarg
->val
.zend
.str
) {
759 zend_string_release(iarg
->val
.zend
.str
);
765 if (impl
->decl
->args
) for (i
= 0; i
< impl
->decl
->args
->count
; ++i
) {
766 decl_arg
*darg
= impl
->decl
->args
->args
[i
];
769 decl_type
*type
= real_decl_type(darg
->type
);
771 if (type
->type
== PSI_T_STRUCT
) {
772 void **ptr
= (void **) ((char *) darg
->mem
+ type
->strct
->size
);
784 static inline int psi_calc_num_exp_value(num_exp
*exp
, impl_val
*strct
, impl_val
*res
) {
785 impl_val
*ref
, *tmp
= NULL
;
789 switch (is_numeric_string(exp
->u
.numb
, strlen(exp
->u
.numb
), (zend_long
*) res
, (double *) res
, 0)) {
798 switch (exp
->u
.cnst
->type
->type
) {
800 res
->i64
= zend_get_constant_str(exp
->u
.cnst
->name
, strlen(exp
->u
.cnst
->name
))->value
.lval
;
803 res
->dval
= zend_get_constant_str(exp
->u
.cnst
->name
, strlen(exp
->u
.cnst
->name
))->value
.dval
;
812 ref
= struct_member_ref(exp
->u
.dvar
->arg
, strct
, &tmp
);
814 ref
= exp
->u
.dvar
->arg
->ptr
;
816 switch (real_decl_type(exp
->u
.dvar
->arg
->type
)->type
) {
825 memcpy(res
, deref_impl_val(ref
, exp
->u
.dvar
), sizeof(*res
));
829 return real_decl_type(exp
->u
.dvar
->arg
->type
)->type
;
833 memcpy(res
, deref_impl_val(ref
, exp
->u
.dvar
), sizeof(*res
));
837 return real_decl_type(exp
->u
.dvar
->arg
->type
)->type
;
839 EMPTY_SWITCH_DEFAULT_CASE();
843 EMPTY_SWITCH_DEFAULT_CASE();
848 int psi_calc_num_exp(num_exp
*exp
, impl_val
*strct
, impl_val
*res
) {
850 int num_type
= psi_calc_num_exp_value(exp
, strct
, &num
);
854 int tmp_type
= psi_calc_num_exp(exp
->operand
, strct
, &tmp
);
856 return exp
->calculator(num_type
, &num
, tmp_type
, &tmp
, res
);
859 memcpy(res
, &num
, sizeof(*res
));
866 #define PSI_CALC_OP(var) do { \
867 const char *fmt = "calc %" PRI##var ", %" PRI##var ": %" PRI##var "\n"; \
868 res->var = PSI_CALC(v1->var, v2->var); \
869 if (!res->var) fprintf(stderr, fmt, v1->var, v2->var, res->var); \
871 #define PSI_CALC_OP2(vres, var1, var2) do { \
872 const char *fmt = "calc %" PRI##var1 ", %" PRI##var2 ": %" PRI##vres "\n"; \
873 res->vres = PSI_CALC(v1->var1, v2->var2); \
874 if (!res->vres) fprintf(stderr, fmt, v1->var1, v2->var2, res->vres); \
876 #define PSI_CALC_FN(op) int psi_calc_##op(int t1, impl_val *v1, int t2, impl_val *v2, impl_val *res) \
880 case PSI_T_FLOAT: PSI_CALC_OP(fval); break; \
881 case PSI_T_DOUBLE: PSI_CALC_OP(dval); break; \
882 case PSI_T_INT8: PSI_CALC_OP(i8); break; \
883 case PSI_T_UINT8: PSI_CALC_OP(u8); break; \
884 case PSI_T_INT16: PSI_CALC_OP(i16); break; \
885 case PSI_T_UINT16: PSI_CALC_OP(u16); break; \
886 case PSI_T_INT32: PSI_CALC_OP(i32); break; \
887 case PSI_T_UINT32: PSI_CALC_OP(u32); break; \
888 case PSI_T_INT64: PSI_CALC_OP(i64); break; \
889 case PSI_T_UINT64: PSI_CALC_OP(u64); break; \
890 EMPTY_SWITCH_DEFAULT_CASE(); \
893 } else if (t1 == PSI_T_DOUBLE) { \
895 case PSI_T_FLOAT: PSI_CALC_OP2(dval, dval, fval); break; \
896 case PSI_T_INT8: PSI_CALC_OP2(dval, dval, i8); break; \
897 case PSI_T_UINT8: PSI_CALC_OP2(dval, dval, u8); break; \
898 case PSI_T_INT16: PSI_CALC_OP2(dval, dval, i16); break; \
899 case PSI_T_UINT16: PSI_CALC_OP2(dval, dval, u16); break; \
900 case PSI_T_INT32: PSI_CALC_OP2(dval, dval, i32); break; \
901 case PSI_T_UINT32: PSI_CALC_OP2(dval, dval, u32); break; \
902 case PSI_T_INT64: PSI_CALC_OP2(dval, dval, i64); break; \
903 case PSI_T_UINT64: PSI_CALC_OP2(dval, dval, u64); break; \
904 EMPTY_SWITCH_DEFAULT_CASE(); \
907 } else if (t2 == PSI_T_DOUBLE) { \
909 case PSI_T_FLOAT: PSI_CALC_OP2(dval, fval, dval); break; \
910 case PSI_T_INT8: PSI_CALC_OP2(dval, i8, dval); break; \
911 case PSI_T_UINT8: PSI_CALC_OP2(dval, u8, dval); break; \
912 case PSI_T_INT16: PSI_CALC_OP2(dval, i16, dval); break; \
913 case PSI_T_UINT16: PSI_CALC_OP2(dval, u16, dval); break; \
914 case PSI_T_INT32: PSI_CALC_OP2(dval, i32, dval); break; \
915 case PSI_T_UINT32: PSI_CALC_OP2(dval, u32, dval); break; \
916 case PSI_T_INT64: PSI_CALC_OP2(dval, i64, dval); break; \
917 case PSI_T_UINT64: PSI_CALC_OP2(dval, u64, dval); break; \
918 EMPTY_SWITCH_DEFAULT_CASE(); \
921 } else if (t1 == PSI_T_FLOAT) { \
923 case PSI_T_DOUBLE: PSI_CALC_OP2(dval, fval, dval); return t2; \
924 case PSI_T_INT8: PSI_CALC_OP2(fval, fval, i8); break; \
925 case PSI_T_UINT8: PSI_CALC_OP2(fval, fval, u8); break; \
926 case PSI_T_INT16: PSI_CALC_OP2(fval, fval, i16); break; \
927 case PSI_T_UINT16: PSI_CALC_OP2(fval, fval, u16); break; \
928 case PSI_T_INT32: PSI_CALC_OP2(fval, fval, i32); break; \
929 case PSI_T_UINT32: PSI_CALC_OP2(fval, fval, u32); break; \
930 case PSI_T_INT64: PSI_CALC_OP2(fval, fval, i64); break; \
931 case PSI_T_UINT64: PSI_CALC_OP2(fval, fval, u64); break; \
932 EMPTY_SWITCH_DEFAULT_CASE(); \
935 } else if (t2 == PSI_T_FLOAT) { \
937 case PSI_T_DOUBLE: PSI_CALC_OP2(dval, dval, fval); return t1; \
938 case PSI_T_INT8: PSI_CALC_OP2(fval, i8, fval); break; \
939 case PSI_T_UINT8: PSI_CALC_OP2(fval, u8, fval); break; \
940 case PSI_T_INT16: PSI_CALC_OP2(fval, i16, fval); break; \
941 case PSI_T_UINT16: PSI_CALC_OP2(fval, u16, fval); break; \
942 case PSI_T_INT32: PSI_CALC_OP2(fval, i32, fval); break; \
943 case PSI_T_UINT32: PSI_CALC_OP2(fval, u32, fval); break; \
944 case PSI_T_INT64: PSI_CALC_OP2(fval, i64, fval); break; \
945 case PSI_T_UINT64: PSI_CALC_OP2(fval, u64, fval); break; \
946 EMPTY_SWITCH_DEFAULT_CASE(); \
950 int64_t sval1 = v1->i64, sval2 = v2->i64; \
951 uint64_t uval1 = v1->u64, uval2 = v2->u64; \
953 case PSI_T_INT8: sval1 >>= 8; \
954 case PSI_T_INT16: sval1 >>= 8; \
955 case PSI_T_INT32: sval1 >>= 8; \
958 case PSI_T_INT8: sval2 >>= 8; \
959 case PSI_T_INT16: sval2 >>= 8; \
960 case PSI_T_INT32: sval2 >>= 8; \
962 res->i64 = PSI_CALC(sval1 , sval2); \
963 return PSI_T_INT64; \
964 case PSI_T_UINT8: uval2 >>= 8; \
965 case PSI_T_UINT16: uval2 >>= 8; \
966 case PSI_T_UINT32: uval2 >>= 8; \
968 res->i64 = PSI_CALC(sval1, uval2); \
969 return PSI_T_INT64; \
972 case PSI_T_UINT8: uval1 >>= 8; \
973 case PSI_T_UINT16: uval1 >>= 8; \
974 case PSI_T_UINT32: uval1 >>= 8; \
977 case PSI_T_INT8: sval2 >>= 8; \
978 case PSI_T_INT16: sval2 >>= 8; \
979 case PSI_T_INT32: sval2 >>= 8; \
981 res->i64 = PSI_CALC(uval1, sval2); \
982 return PSI_T_INT64; \
983 case PSI_T_UINT8: uval2 >>= 8; \
984 case PSI_T_UINT16: uval2 >>= 8; \
985 case PSI_T_UINT32: uval2 >>= 8; \
987 res->u64 = PSI_CALC(uval1, uval2); \
988 return PSI_T_UINT64; \
998 #define PSI_CALC(var1, var2) (var1) + (var2)
1001 #define PSI_CALC(var1, var2) (var1) * (var2)
1004 #define PSI_CALC(var1, var2) (var1) - (var2)
1007 #define PSI_CALC(var1, var2) (var1) / (var2)
1010 static inline void psi_do_args(impl
*impl
) {
1013 for (i
= 0; i
< impl
->decl
->args
->count
; ++i
) {
1014 impl
->decl
->call
.args
[i
] = impl
->decl
->args
->args
[i
]->let
->ptr
;
1018 static inline void psi_do_varargs(impl
*impl
, void ***free_list
) {
1020 impl_vararg
*va
= &impl
->func
->args
->vararg
;
1021 size_t vacount
= va
->args
->count
;
1022 void **tmpvals
= calloc(vacount
, sizeof(*tmpvals
));
1023 token_t type
= psi_let_fn(va
->name
->type
->type
);
1025 va
->types
= calloc(vacount
, sizeof(*va
->types
));
1026 va
->values
= calloc(vacount
, sizeof(*va
->values
));
1028 for (i
= 0, j
= 0; i
< vacount
; ++i
) {
1029 impl_arg
*vaarg
= va
->args
->args
[i
];
1030 void *to_free
= NULL
;
1032 psi_let_val(type
, vaarg
, &va
->values
[i
], NULL
, &to_free
);
1034 *free_list
= realloc(*free_list
, (j
+ 1) * sizeof(*free_list
));
1035 (*free_list
)[j
++] = to_free
;
1040 void psi_call(zend_execute_data
*execute_data
, zval
*return_value
, impl
*impl
)
1043 decl_callinfo
*call
= &impl
->decl
->call
;
1044 void **free_list
= NULL
;
1046 memset(impl
->decl
->func
->ptr
, 0, sizeof(impl_val
));
1048 if (SUCCESS
!= psi_parse_args(execute_data
, impl
)) {
1052 for (i
= 0; i
< impl
->stmts
->let
.count
; ++i
) {
1053 let_stmt
*let
= impl
->stmts
->let
.list
[i
];
1055 if (!psi_do_let(let
)) {
1056 psi_do_return(return_value
, impl
->stmts
->ret
.list
[0]);
1062 if (impl
->decl
->args
) {
1065 if (impl
->func
->args
->vararg
.args
) {
1066 call
= psi_do_varargs(impl
, &free_list
);
1070 if (impl
->func
->args
->vararg
.args
) {
1071 PSI_ContextCallVarargs(&PSI_G(context
), &impl
->decl
->call
)
1073 PSI_ContextCall(&PSI_G(context
), &impl
->decl
->call
);
1075 psi_do_return(return_value
, impl
->stmts
->ret
.list
[0]);
1077 for (i
= 0; i
< impl
->stmts
->set
.count
; ++i
) {
1078 set_stmt
*set
= impl
->stmts
->set
.list
[i
];
1080 if (set
->arg
->_zv
) {
1081 psi_do_set(set
->arg
->_zv
, set
->val
);
1085 for (i
= 0; i
< impl
->stmts
->fre
.count
; ++i
) {
1086 free_stmt
*fre
= impl
->stmts
->fre
.list
[i
];
1093 static void psi_object_free(zend_object
*o
)
1095 psi_object
*obj
= PSI_OBJ(NULL
, o
);
1101 zend_object_std_dtor(o
);
1104 static zend_object
*psi_object_init(zend_class_entry
*ce
)
1106 psi_object
*o
= ecalloc(1, sizeof(*o
) + zend_object_properties_size(ce
));
1108 zend_object_std_init(&o
->std
, ce
);
1109 object_properties_init(&o
->std
, ce
);
1110 o
->std
.handlers
= &psi_object_handlers
;
1114 PHP_MINIT_FUNCTION(psi
)
1116 PSI_ContextOps
*ops
= NULL
;
1117 zend_class_entry ce
= {0};
1119 REGISTER_INI_ENTRIES();
1121 INIT_NS_CLASS_ENTRY(ce
, "psi", "object", NULL
);
1122 psi_class_entry
= zend_register_internal_class_ex(&ce
, NULL
);
1123 psi_class_entry
->create_object
= psi_object_init
;
1125 memcpy(&psi_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
1126 psi_object_handlers
.offset
= XtOffsetOf(psi_object
, std
);
1127 psi_object_handlers
.free_obj
= psi_object_free
;
1128 psi_object_handlers
.clone_obj
= NULL
;
1131 if (!strcasecmp(PSI_G(engine
), "jit")) {
1140 php_error(E_WARNING
, "No PSI engine found");
1144 PSI_ContextInit(&PSI_G(context
), ops
, psi_error
);
1145 PSI_ContextBuild(&PSI_G(context
), PSI_G(directory
));
1147 if (psi_check_env("PSI_DUMP")) {
1148 PSI_ContextDump(&PSI_G(context
), STDOUT_FILENO
);
1154 PHP_MSHUTDOWN_FUNCTION(psi
)
1156 PSI_ContextDtor(&PSI_G(context
));
1158 UNREGISTER_INI_ENTRIES();
1163 #if defined(COMPILE_DL_PSI) && defined(ZTS)
1164 PHP_RINIT_FUNCTION(psi
)
1166 ZEND_TSRMLS_CACHE_UPDATE();
1171 PHP_MINFO_FUNCTION(psi
)
1173 php_info_print_table_start();
1174 php_info_print_table_header(2, "psi support", "enabled");
1175 php_info_print_table_end();
1177 DISPLAY_INI_ENTRIES();
1179 const zend_function_entry psi_functions
[] = {
1183 zend_module_entry psi_module_entry
= {
1184 STANDARD_MODULE_HEADER
,
1189 #if defined(COMPILE_DL_PSI) && defined(ZTS)
1190 PHP_RINIT(psi
), /* Replace with NULL if there's nothing to do at request start */
1197 STANDARD_MODULE_PROPERTIES
1200 #ifdef COMPILE_DL_PSI
1202 ZEND_TSRMLS_CACHE_DEFINE();
1204 ZEND_GET_MODULE(psi
)
1212 * vim600: noet sw=4 ts=4 fdm=marker
1213 * vim<600: noet sw=4 ts=4