8 #include "ext/standard/info.h"
9 #include "zend_exceptions.h"
10 #include "zend_constants.h"
11 #include "zend_operators.h"
20 # define PSI_ENGINE "jit"
25 # define PSI_ENGINE "ffi"
28 ZEND_DECLARE_MODULE_GLOBALS(psi
);
31 STD_PHP_INI_ENTRY("psi.engine", PSI_ENGINE
, PHP_INI_SYSTEM
, OnUpdateString
, engine
, zend_psi_globals
, psi_globals
)
32 STD_PHP_INI_ENTRY("psi.directory", "psi.d", PHP_INI_SYSTEM
, OnUpdateString
, directory
, zend_psi_globals
, psi_globals
)
35 static zend_object_handlers psi_object_handlers
;
36 static zend_class_entry
*psi_class_entry
;
38 void psi_error(int type
, const char *msg
, ...)
44 vslprintf(buf
, 0x1000, msg
, argv
);
50 size_t psi_t_alignment(token_t t
)
52 #define PSI_ALIGNOF(T) case PSI_T_## T: return ALIGNOF_## T ##_T;
65 return ALIGNOF_DOUBLE
;
67 return ALIGNOF_VOID_P
;
68 EMPTY_SWITCH_DEFAULT_CASE();
73 size_t psi_t_size(token_t t
)
75 #define PSI_SIZEOF(T) case PSI_T_## T : return SIZEOF_## T ##_T;
91 EMPTY_SWITCH_DEFAULT_CASE();
96 size_t psi_t_align(token_t t
, size_t s
)
98 size_t a
= psi_t_alignment(t
);
99 return ((s
- 1) | (a
- 1)) + 1;
102 int psi_internal_type(impl_type
*type
)
104 switch (type
->type
) {
121 zend_internal_arg_info
*psi_internal_arginfo(impl
*impl
)
124 zend_internal_arg_info
*aip
;
125 zend_internal_function_info
*fi
;
127 aip
= calloc(impl
->func
->args
->count
+ 1, sizeof(*aip
));
129 fi
= (zend_internal_function_info
*) &aip
[0];
130 fi
->required_num_args
= psi_num_min_args(impl
);
131 fi
->return_reference
= impl
->func
->return_reference
;
132 fi
->type_hint
= psi_internal_type(impl
->func
->return_type
);
134 for (i
= 0; i
< impl
->func
->args
->count
; ++i
) {
135 impl_arg
*iarg
= impl
->func
->args
->args
[i
];
136 zend_internal_arg_info
*ai
= &aip
[i
+1];
138 ai
->name
= iarg
->var
->name
;
139 ai
->type_hint
= psi_internal_type(iarg
->type
);
140 if (iarg
->var
->reference
) {
141 ai
->pass_by_reference
= 1;
143 if (iarg
->var
->reference
|| (iarg
->def
&& iarg
->def
->type
== PSI_T_NULL
)) {
151 size_t psi_num_min_args(impl
*impl
)
153 size_t i
, n
= impl
->func
->args
->count
;
155 for (i
= 0; i
< impl
->func
->args
->count
; ++i
) {
156 if (impl
->func
->args
->args
[i
]->def
) {
163 void psi_to_void(zval
*return_value
, set_value
*set
, impl_val
*ret_val
)
168 void psi_to_bool(zval
*return_value
, set_value
*set
, impl_val
*ret_val
)
170 psi_to_int(return_value
, set
, ret_val
);
171 convert_to_boolean(return_value
);
174 void psi_to_int(zval
*return_value
, set_value
*set
, impl_val
*ret_val
)
176 decl_var
*var
= set
->vars
->vars
[0];
177 token_t t
= real_decl_type(var
->arg
->type
)->type
;
178 impl_val
*v
= deref_impl_val(ret_val
, var
);
182 RETVAL_DOUBLE((double) v
->fval
);
183 convert_to_long(return_value
);
186 RETVAL_DOUBLE(v
->dval
);
187 convert_to_long(return_value
);
205 #if UINT32_MAX >= ZEND_LONG_MAX
206 if (v
->u32
> ZEND_LONG_MAX
) {
209 RETVAL_STRING(zend_print_ulong_to_buf(&d
[10], v
->u32
));
213 #if UINT32_MAX >= ZEND_LONG_MAX
221 if (v
->u64
> ZEND_LONG_MAX
) {
224 RETVAL_STRING(zend_print_ulong_to_buf(&d
[22], v
->u64
));
229 EMPTY_SWITCH_DEFAULT_CASE();
233 void psi_to_double(zval
*return_value
, set_value
*set
, impl_val
*ret_val
)
235 decl_var
*var
= set
->vars
->vars
[0];
236 token_t t
= real_decl_type(var
->arg
->type
)->type
;
237 impl_val
*v
= deref_impl_val(ret_val
, var
);
241 RETVAL_DOUBLE((double) v
->fval
);
244 RETVAL_DOUBLE(v
->dval
);
247 RETVAL_DOUBLE((double) v
->i8
);
250 RETVAL_DOUBLE((double) v
->u8
);
253 RETVAL_DOUBLE((double) v
->i16
);
256 RETVAL_DOUBLE((double) v
->u16
);
259 RETVAL_DOUBLE((double) v
->i32
);
262 RETVAL_DOUBLE((double) v
->u32
);
265 RETVAL_DOUBLE((double) v
->i64
);
268 RETVAL_DOUBLE((double) v
->u64
);
270 EMPTY_SWITCH_DEFAULT_CASE();
274 void psi_to_string(zval
*return_value
, set_value
*set
, impl_val
*ret_val
)
276 decl_var
*var
= set
->vars
->vars
[0];
277 token_t t
= real_decl_type(var
->arg
->type
)->type
;
283 if (!var
->arg
->var
->pointer_level
) {
284 RETVAL_STRINGL(&ret_val
->cval
, 1);
286 ret_val
= deref_impl_val(ret_val
, var
);
287 if (ret_val
&& ret_val
->ptr
) {
289 RETVAL_STRINGL(ret_val
->ptr
, psi_long_num_exp(set
->num
, set
->outer
.val
));
291 RETVAL_STRING(ret_val
->ptr
);
294 RETVAL_EMPTY_STRING();
299 RETVAL_DOUBLE((double) deref_impl_val(ret_val
, var
)->fval
);
302 RETVAL_DOUBLE(deref_impl_val(ret_val
, var
)->dval
);
305 psi_to_int(return_value
, set
, ret_val
);
308 convert_to_string(return_value
);
312 static impl_val
*iterate(impl_val
*val
, size_t size
, unsigned i
, impl_val
*tmp
)
314 memset(tmp
, 0, sizeof(*tmp
));
315 memcpy(tmp
, ((void*) val
->ptr
) + size
* i
, size
);
319 void psi_from_zval(impl_val
*mem
, decl_arg
*spec
, zval
*zv
, void **tmp
)
321 decl_type
*type
= real_decl_type(spec
->type
);
323 switch (type
->type
) {
325 mem
->fval
= (float) zval_get_double(zv
);
328 mem
->dval
= zval_get_double(zv
);
332 if (spec
->var
->pointer_level
) {
333 zend_string
*zs
= zval_get_string(zv
);
334 *tmp
= mem
->ptr
= estrndup(zs
->val
, zs
->len
);
335 zend_string_release(zs
);
340 mem
->zend
.lval
= zval_get_long(zv
);
345 void *psi_array_to_struct(decl_struct
*s
, HashTable
*arr
)
348 char *mem
= ecalloc(1, s
->size
+ s
->args
->count
* sizeof(void *));
350 if (arr
) for (i
= 0; i
< s
->args
->count
; ++i
) {
351 decl_arg
*darg
= s
->args
->args
[i
];
352 zval
*entry
= zend_hash_str_find_ind(arr
, darg
->var
->name
, strlen(darg
->var
->name
));
358 memset(&tmp
, 0, sizeof(tmp
));
359 psi_from_zval(&val
, darg
, entry
, &tmp
);
360 memcpy(mem
+ darg
->layout
->pos
, &val
, darg
->layout
->len
);
362 ((void **)(mem
+ s
->size
))[j
++] = tmp
;
369 void psi_to_array(zval
*return_value
, set_value
*set
, impl_val
*r_val
)
372 decl_var
*var
= set
->vars
->vars
[0];
373 token_t t
= real_decl_type(var
->arg
->type
)->type
;
374 impl_val tmp
, *ret_val
= deref_impl_val(r_val
, var
);
376 if ((intptr_t) ret_val
->ptr
<= (intptr_t) 0) {
380 array_init(return_value
);
382 if (t
== PSI_T_STRUCT
) {
383 // decl_struct *s = real_decl_type(var->arg->type)->strct;
386 /* explicit member casts */
387 for (i
= 0; i
< set
->count
; ++i
) {
388 set_value
*sub_set
= set
->inner
[i
];
389 decl_var
*sub_var
= sub_set
->vars
->vars
[0];
391 sub_set
->outer
.val
= r_val
;
394 impl_val
*tmp
= NULL
, *val
;
397 val
= deref_impl_val(struct_member_ref(sub_var
->arg
, ret_val
, &tmp
), sub_var
);
398 sub_set
->func
->handler(&ztmp
, sub_set
, val
);
399 add_assoc_zval(return_value
, sub_var
->name
, &ztmp
);
410 if (var
->arg
->var
->array_size
) {
411 /* to_array(foo[NUMBER]) */
412 for (i
= 0; i
< var
->arg
->var
->array_size
; ++i
) {
413 size_t size
= psi_t_size(var
->arg
->var
->pointer_level
> 1 ? PSI_T_POINTER
: t
);
414 impl_val
*ptr
= iterate(ret_val
, size
, i
, &tmp
);
419 ZVAL_DOUBLE(&ele
, (double) ptr
->fval
);
422 ZVAL_DOUBLE(&ele
, ptr
->dval
);
425 ZVAL_LONG(&ele
, ptr
->lval
);
429 add_next_index_zval(return_value
, &ele
);
432 } else if (set
->num
) {
433 /* to_array(arr_var, num_expr, to_int(*arr_var))
436 zend_long i
, n
= psi_long_num_exp(set
->num
, set
->outer
.val
);
438 for (i
= 0; i
< n
; ++i
) {
439 size_t size
= psi_t_size(var
->arg
->var
->pointer_level
? PSI_T_POINTER
: t
);
440 impl_val
*ptr
= iterate(ret_val
, size
, i
, &tmp
);
442 set
->inner
[0]->func
->handler(&ele
, set
->inner
[0], ptr
);
443 add_next_index_zval(return_value
, &ele
);
450 void psi_to_object(zval
*return_value
, set_value
*set
, impl_val
*r_val
)
452 decl_var
*var
= set
->vars
->vars
[0];
453 impl_val
*ret_val
= deref_impl_val(r_val
, var
);
456 if ((intptr_t) ret_val
->ptr
> (intptr_t) 0) {
457 object_init_ex(return_value
, psi_class_entry
);
458 obj
= PSI_OBJ(return_value
, NULL
);
459 obj
->data
= ret_val
->ptr
;
465 static inline ZEND_RESULT_CODE
psi_parse_args(zend_execute_data
*execute_data
, impl
*impl
)
468 zval
*zarg
= ZEND_CALL_ARG(execute_data
, 0);
470 zend_error_handling zeh
;
472 zend_replace_error_handling(EH_THROW
, zend_exception_get_default(), &zeh
);
474 if (!impl
->func
->args
->count
) {
477 rv
= zend_parse_parameters_none();
478 zend_restore_error_handling(&zeh
);
482 ZEND_PARSE_PARAMETERS_START(psi_num_min_args(impl
), impl
->func
->args
->count
)
484 iarg
= impl
->func
->args
->args
[_i
];
488 if (PSI_T_BOOL
== iarg
->type
->type
) {
489 Z_PARAM_BOOL(iarg
->val
.zend
.bval
);
490 } else if (PSI_T_INT
== iarg
->type
->type
) {
491 Z_PARAM_LONG(iarg
->val
.zend
.lval
);
492 } else if (PSI_T_FLOAT
== iarg
->type
->type
|| PSI_T_DOUBLE
== iarg
->type
->type
) {
493 Z_PARAM_DOUBLE(iarg
->val
.dval
);
494 } else if (PSI_T_STRING
== iarg
->type
->type
) {
495 Z_PARAM_STR_EX(iarg
->val
.zend
.str
, 1, iarg
->var
->reference
);
496 if (iarg
->val
.zend
.str
) {
497 zend_string_addref(iarg
->val
.zend
.str
);
499 } else if (PSI_T_ARRAY
== iarg
->type
->type
) {
501 } else if (PSI_T_OBJECT
== iarg
->type
->type
) {
504 error_code
= ZPP_ERROR_FAILURE
;
507 if (_i
< _num_args
) {
510 ZEND_PARSE_PARAMETERS_END_EX(
511 zend_restore_error_handling(&zeh
);
515 /* set up defaults */
516 for (i
= 0; i
< impl
->func
->args
->count
; ++i
) {
517 iarg
= impl
->func
->args
->args
[i
];
519 if (i
< EX_NUM_ARGS()) {
521 ZVAL_DEREF(iarg
->_zv
);
522 } else if (iarg
->def
) {
523 switch (iarg
->type
->type
) {
525 iarg
->val
.zend
.bval
= iarg
->def
->type
== PSI_T_TRUE
? 1 : 0;
528 iarg
->val
.zend
.lval
= zend_atol(iarg
->def
->text
, strlen(iarg
->def
->text
));
532 iarg
->val
.dval
= zend_strtod(iarg
->def
->text
, NULL
);
536 iarg
->val
.zend
.str
= zend_string_init(&iarg
->def
->text
[1], strlen(iarg
->def
->text
) - 2, 0);
542 zend_restore_error_handling(&zeh
);
546 static inline void *psi_do_calloc(let_calloc
*alloc
)
548 zend_long n
= psi_long_num_exp(alloc
->nmemb
, NULL
), s
= psi_long_num_exp(alloc
->size
, NULL
);
549 void *mem
= safe_emalloc(n
, s
, sizeof(void *));
550 memset(mem
, 0, n
* s
+ sizeof(void *));
554 static inline void *psi_do_let(let_stmt
*let
)
556 decl_arg
*darg
= let
->var
->arg
;
557 impl_val
*arg_val
= darg
->ptr
;
560 switch (let
->val
? let
->val
->kind
: PSI_LET_NULL
) {
562 arg_val
->ptr
= deref_impl_val(let
->val
->data
.var
->arg
->val
.ptr
, let
->var
);
565 if (darg
->var
->array_size
) {
566 arg_val
->ptr
= ecalloc(darg
->var
->array_size
, sizeof(*arg_val
));
567 darg
->mem
= arg_val
->ptr
;
569 memset(arg_val
, 0, sizeof(*arg_val
));
573 arg_val
->ptr
= psi_do_calloc(let
->val
->data
.alloc
);
574 darg
->mem
= arg_val
->ptr
;
577 arg_val
->zend
.lval
= psi_long_num_exp(let
->val
->data
.num
, NULL
);
580 iarg
= let
->val
->data
.func
->arg
;
582 switch (let
->val
->data
.func
->type
) {
584 if (iarg
->type
->type
== PSI_T_BOOL
) {
585 arg_val
->cval
= iarg
->val
.zend
.bval
;
587 arg_val
->cval
= zend_is_true(iarg
->_zv
);
591 if (iarg
->type
->type
== PSI_T_INT
) {
592 arg_val
->lval
= iarg
->val
.zend
.lval
;
594 arg_val
->lval
= zval_get_long(iarg
->_zv
);
599 if (iarg
->type
->type
== PSI_T_STRING
) {
600 if (iarg
->val
.zend
.str
) {
601 arg_val
->ptr
= estrdup(iarg
->val
.zend
.str
->val
);
602 darg
->mem
= arg_val
->ptr
;
607 zend_string
*zs
= zval_get_string(iarg
->_zv
);
608 arg_val
->ptr
= estrdup(zs
->val
);
609 darg
->mem
= arg_val
->ptr
;
610 zend_string_release(zs
);
612 if (PSI_T_PATHVAL
== darg
->let
->val
->data
.func
->type
) {
613 if (SUCCESS
!= php_check_open_basedir(arg_val
->ptr
)) {
620 if (iarg
->type
->type
== PSI_T_STRING
) {
621 if (iarg
->val
.zend
.str
) {
622 arg_val
->lval
= iarg
->val
.zend
.str
->len
;
627 zend_string
*zs
= zval_get_string(iarg
->_zv
);
628 arg_val
->lval
= zs
->len
;
629 zend_string_release(zs
);
633 if (iarg
->type
->type
== PSI_T_ARRAY
) {
634 decl_type
*type
= real_decl_type(darg
->type
);
636 switch (type
->type
) {
638 arg_val
->ptr
= psi_array_to_struct(type
->strct
, HASH_OF(iarg
->_zv
));
639 darg
->mem
= arg_val
->ptr
;
645 if (iarg
->type
->type
== PSI_T_OBJECT
) {
648 if (!instanceof_function(Z_OBJCE_P(iarg
->_zv
), psi_class_entry
)) {
652 obj
= PSI_OBJ(iarg
->_zv
, NULL
);
653 arg_val
->ptr
= obj
->data
;
656 EMPTY_SWITCH_DEFAULT_CASE();
660 if (let
->val
&& let
->val
->flags
.one
.is_reference
) {
661 return let
->ptr
= &darg
->ptr
;
663 return let
->ptr
= darg
->ptr
;
667 static inline void psi_do_set(zval
*return_value
, set_value
*set
)
669 zval_dtor(return_value
);
670 set
->func
->handler(return_value
, set
, set
->vars
->vars
[0]->arg
->ptr
);
673 static inline void psi_do_return(zval
*return_value
, return_stmt
*ret
)
675 ret
->set
->func
->handler(return_value
, ret
->set
, ret
->set
->vars
->vars
[0]->arg
->ptr
);
678 static inline void psi_do_free(free_stmt
*fre
)
683 for (i
= 0; i
< fre
->calls
->count
; ++i
) {
684 free_call
*f
= fre
->calls
->list
[i
];
686 for (j
= 0; j
< f
->vars
->count
; ++j
) {
687 decl_var
*dvar
= f
->vars
->vars
[j
];
688 decl_arg
*darg
= dvar
->arg
;
690 f
->decl
->call
.args
[j
] = &darg
->val
;
693 /* FIXME: check in validate_* that free functions return scalar */
694 PSI_ContextCall(&PSI_G(context
), &dummy
, f
->decl
);
698 static inline void psi_do_clean(impl
*impl
)
702 for (i
= 0; i
< impl
->func
->args
->count
; ++i
) {
703 impl_arg
*iarg
= impl
->func
->args
->args
[i
];
705 switch (iarg
->type
->type
) {
707 if (iarg
->val
.zend
.str
) {
708 zend_string_release(iarg
->val
.zend
.str
);
714 if (impl
->decl
->args
) for (i
= 0; i
< impl
->decl
->args
->count
; ++i
) {
715 decl_arg
*darg
= impl
->decl
->args
->args
[i
];
718 decl_type
*type
= real_decl_type(darg
->type
);
720 if (type
->type
== PSI_T_STRUCT
) {
721 void **ptr
= (void **) ((char *) darg
->mem
+ type
->strct
->size
);
733 static inline int psi_calc_num_exp_value(num_exp
*exp
, impl_val
*strct
, impl_val
*res
) {
734 impl_val
*ref
, *tmp
= NULL
;
738 switch (is_numeric_string(exp
->u
.numb
, strlen(exp
->u
.numb
), (zend_long
*) res
, (double *) res
, 0)) {
747 switch (exp
->u
.cnst
->type
->type
) {
749 res
->i64
= zend_get_constant_str(exp
->u
.cnst
->name
, strlen(exp
->u
.cnst
->name
))->value
.lval
;
752 res
->dval
= zend_get_constant_str(exp
->u
.cnst
->name
, strlen(exp
->u
.cnst
->name
))->value
.dval
;
761 ref
= struct_member_ref(exp
->u
.dvar
->arg
, strct
, &tmp
);
763 ref
= exp
->u
.dvar
->arg
->ptr
;
765 switch (real_decl_type(exp
->u
.dvar
->arg
->type
)->type
) {
774 memcpy(res
, deref_impl_val(ref
, exp
->u
.dvar
), sizeof(*res
));
778 return real_decl_type(exp
->u
.dvar
->arg
->type
)->type
;
782 memcpy(res
, deref_impl_val(ref
, exp
->u
.dvar
), sizeof(*res
));
786 return real_decl_type(exp
->u
.dvar
->arg
->type
)->type
;
788 EMPTY_SWITCH_DEFAULT_CASE();
792 EMPTY_SWITCH_DEFAULT_CASE();
797 int psi_calc_num_exp(num_exp
*exp
, impl_val
*strct
, impl_val
*res
) {
799 int num_type
= psi_calc_num_exp_value(exp
, strct
, &num
);
803 int tmp_type
= psi_calc_num_exp(exp
->operand
, strct
, &tmp
);
805 return exp
->calculator(num_type
, &num
, tmp_type
, &tmp
, res
);
808 memcpy(res
, &num
, sizeof(*res
));
815 #define PSI_CALC_OP(var) do { \
816 const char *fmt = "calc %" PRI##var ", %" PRI##var ": %" PRI##var "\n"; \
817 res->var = PSI_CALC(v1->var, v2->var); \
818 if (!res->var) fprintf(stderr, fmt, v1->var, v2->var, res->var); \
820 #define PSI_CALC_OP2(vres, var1, var2) do { \
821 const char *fmt = "calc %" PRI##var1 ", %" PRI##var2 ": %" PRI##vres "\n"; \
822 res->vres = PSI_CALC(v1->var1, v2->var2); \
823 if (!res->vres) fprintf(stderr, fmt, v1->var1, v2->var2, res->vres); \
825 #define PSI_CALC_FN(op) int psi_calc_##op(int t1, impl_val *v1, int t2, impl_val *v2, impl_val *res) \
829 case PSI_T_FLOAT: PSI_CALC_OP(fval); break; \
830 case PSI_T_DOUBLE: PSI_CALC_OP(dval); break; \
831 case PSI_T_INT8: PSI_CALC_OP(i8); break; \
832 case PSI_T_UINT8: PSI_CALC_OP(u8); break; \
833 case PSI_T_INT16: PSI_CALC_OP(i16); break; \
834 case PSI_T_UINT16: PSI_CALC_OP(u16); break; \
835 case PSI_T_INT32: PSI_CALC_OP(i32); break; \
836 case PSI_T_UINT32: PSI_CALC_OP(u32); break; \
837 case PSI_T_INT64: PSI_CALC_OP(i64); break; \
838 case PSI_T_UINT64: PSI_CALC_OP(u64); break; \
839 EMPTY_SWITCH_DEFAULT_CASE(); \
842 } else if (t1 == PSI_T_DOUBLE) { \
844 case PSI_T_FLOAT: PSI_CALC_OP2(dval, dval, fval); break; \
845 case PSI_T_INT8: PSI_CALC_OP2(dval, dval, i8); break; \
846 case PSI_T_UINT8: PSI_CALC_OP2(dval, dval, u8); break; \
847 case PSI_T_INT16: PSI_CALC_OP2(dval, dval, i16); break; \
848 case PSI_T_UINT16: PSI_CALC_OP2(dval, dval, u16); break; \
849 case PSI_T_INT32: PSI_CALC_OP2(dval, dval, i32); break; \
850 case PSI_T_UINT32: PSI_CALC_OP2(dval, dval, u32); break; \
851 case PSI_T_INT64: PSI_CALC_OP2(dval, dval, i64); break; \
852 case PSI_T_UINT64: PSI_CALC_OP2(dval, dval, u64); break; \
853 EMPTY_SWITCH_DEFAULT_CASE(); \
856 } else if (t2 == PSI_T_DOUBLE) { \
858 case PSI_T_FLOAT: PSI_CALC_OP2(dval, fval, dval); break; \
859 case PSI_T_INT8: PSI_CALC_OP2(dval, i8, dval); break; \
860 case PSI_T_UINT8: PSI_CALC_OP2(dval, u8, dval); break; \
861 case PSI_T_INT16: PSI_CALC_OP2(dval, i16, dval); break; \
862 case PSI_T_UINT16: PSI_CALC_OP2(dval, u16, dval); break; \
863 case PSI_T_INT32: PSI_CALC_OP2(dval, i32, dval); break; \
864 case PSI_T_UINT32: PSI_CALC_OP2(dval, u32, dval); break; \
865 case PSI_T_INT64: PSI_CALC_OP2(dval, i64, dval); break; \
866 case PSI_T_UINT64: PSI_CALC_OP2(dval, u64, dval); break; \
867 EMPTY_SWITCH_DEFAULT_CASE(); \
870 } else if (t1 == PSI_T_FLOAT) { \
872 case PSI_T_DOUBLE: PSI_CALC_OP2(dval, fval, dval); return t2; \
873 case PSI_T_INT8: PSI_CALC_OP2(fval, fval, i8); break; \
874 case PSI_T_UINT8: PSI_CALC_OP2(fval, fval, u8); break; \
875 case PSI_T_INT16: PSI_CALC_OP2(fval, fval, i16); break; \
876 case PSI_T_UINT16: PSI_CALC_OP2(fval, fval, u16); break; \
877 case PSI_T_INT32: PSI_CALC_OP2(fval, fval, i32); break; \
878 case PSI_T_UINT32: PSI_CALC_OP2(fval, fval, u32); break; \
879 case PSI_T_INT64: PSI_CALC_OP2(fval, fval, i64); break; \
880 case PSI_T_UINT64: PSI_CALC_OP2(fval, fval, u64); break; \
881 EMPTY_SWITCH_DEFAULT_CASE(); \
884 } else if (t2 == PSI_T_FLOAT) { \
886 case PSI_T_DOUBLE: PSI_CALC_OP2(dval, dval, fval); return t1; \
887 case PSI_T_INT8: PSI_CALC_OP2(fval, i8, fval); break; \
888 case PSI_T_UINT8: PSI_CALC_OP2(fval, u8, fval); break; \
889 case PSI_T_INT16: PSI_CALC_OP2(fval, i16, fval); break; \
890 case PSI_T_UINT16: PSI_CALC_OP2(fval, u16, fval); break; \
891 case PSI_T_INT32: PSI_CALC_OP2(fval, i32, fval); break; \
892 case PSI_T_UINT32: PSI_CALC_OP2(fval, u32, fval); break; \
893 case PSI_T_INT64: PSI_CALC_OP2(fval, i64, fval); break; \
894 case PSI_T_UINT64: PSI_CALC_OP2(fval, u64, fval); break; \
895 EMPTY_SWITCH_DEFAULT_CASE(); \
899 int64_t sval1 = v1->i64, sval2 = v2->i64; \
900 uint64_t uval1 = v1->u64, uval2 = v2->u64; \
902 case PSI_T_INT8: sval1 >>= 8; \
903 case PSI_T_INT16: sval1 >>= 8; \
904 case PSI_T_INT32: sval1 >>= 8; \
907 case PSI_T_INT8: sval2 >>= 8; \
908 case PSI_T_INT16: sval2 >>= 8; \
909 case PSI_T_INT32: sval2 >>= 8; \
911 res->i64 = PSI_CALC(sval1 , sval2); \
912 return PSI_T_INT64; \
913 case PSI_T_UINT8: uval2 >>= 8; \
914 case PSI_T_UINT16: uval2 >>= 8; \
915 case PSI_T_UINT32: uval2 >>= 8; \
917 res->i64 = PSI_CALC(sval1, uval2); \
918 return PSI_T_INT64; \
921 case PSI_T_UINT8: uval1 >>= 8; \
922 case PSI_T_UINT16: uval1 >>= 8; \
923 case PSI_T_UINT32: uval1 >>= 8; \
926 case PSI_T_INT8: sval2 >>= 8; \
927 case PSI_T_INT16: sval2 >>= 8; \
928 case PSI_T_INT32: sval2 >>= 8; \
930 res->i64 = PSI_CALC(uval1, sval2); \
931 return PSI_T_INT64; \
932 case PSI_T_UINT8: uval2 >>= 8; \
933 case PSI_T_UINT16: uval2 >>= 8; \
934 case PSI_T_UINT32: uval2 >>= 8; \
936 res->u64 = PSI_CALC(uval1, uval2); \
937 return PSI_T_UINT64; \
947 #define PSI_CALC(var1, var2) (var1) + (var2)
950 #define PSI_CALC(var1, var2) (var1) * (var2)
953 #define PSI_CALC(var1, var2) (var1) - (var2)
956 #define PSI_CALC(var1, var2) (var1) / (var2)
959 void psi_call(zend_execute_data
*execute_data
, zval
*return_value
, impl
*impl
)
962 return_stmt
*ret
= impl
->stmts
->ret
.list
[0];
963 decl_var
*var
= ret
->set
->vars
->vars
[0];
965 memset(var
->arg
->ptr
, 0, sizeof(impl_val
));
967 if (SUCCESS
!= psi_parse_args(execute_data
, impl
)) {
971 for (i
= 0; i
< impl
->stmts
->let
.count
; ++i
) {
972 let_stmt
*let
= impl
->stmts
->let
.list
[i
];
974 if (!psi_do_let(let
)) {
975 psi_do_return(return_value
, ret
);
981 if (impl
->decl
->args
) for (i
= 0; i
< impl
->decl
->args
->count
; ++i
) {
982 impl
->decl
->call
.args
[i
] = impl
->decl
->args
->args
[i
]->let
->ptr
;
985 PSI_ContextCall(&PSI_G(context
), var
->arg
->ptr
, impl
->decl
);
986 psi_do_return(return_value
, ret
);
988 for (i
= 0; i
< impl
->stmts
->set
.count
; ++i
) {
989 set_stmt
*set
= impl
->stmts
->set
.list
[i
];
992 psi_do_set(set
->arg
->_zv
, set
->val
);
996 for (i
= 0; i
< impl
->stmts
->fre
.count
; ++i
) {
997 free_stmt
*fre
= impl
->stmts
->fre
.list
[i
];
1004 static void psi_object_free(zend_object
*o
)
1006 psi_object
*obj
= PSI_OBJ(NULL
, o
);
1012 zend_object_std_dtor(o
);
1015 static zend_object
*psi_object_init(zend_class_entry
*ce
)
1017 psi_object
*o
= ecalloc(1, sizeof(*o
) + zend_object_properties_size(ce
));
1019 zend_object_std_init(&o
->std
, ce
);
1020 object_properties_init(&o
->std
, ce
);
1021 o
->std
.handlers
= &psi_object_handlers
;
1025 PHP_MINIT_FUNCTION(psi
)
1027 PSI_ContextOps
*ops
= NULL
;
1028 zend_class_entry ce
= {0};
1030 REGISTER_INI_ENTRIES();
1032 INIT_NS_CLASS_ENTRY(ce
, "psi", "object", NULL
);
1033 psi_class_entry
= zend_register_internal_class_ex(&ce
, NULL
);
1034 psi_class_entry
->create_object
= psi_object_init
;
1036 memcpy(&psi_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
1037 psi_object_handlers
.offset
= XtOffsetOf(psi_object
, std
);
1038 psi_object_handlers
.free_obj
= psi_object_free
;
1039 psi_object_handlers
.clone_obj
= NULL
;
1042 if (!strcasecmp(PSI_G(engine
), "jit")) {
1051 php_error(E_WARNING
, "No PSI engine found");
1055 PSI_ContextInit(&PSI_G(context
), ops
, psi_error
);
1056 PSI_ContextBuild(&PSI_G(context
), PSI_G(directory
));
1058 if (psi_check_env("PSI_DUMP")) {
1059 PSI_ContextDump(&PSI_G(context
), STDOUT_FILENO
);
1065 PHP_MSHUTDOWN_FUNCTION(psi
)
1067 PSI_ContextDtor(&PSI_G(context
));
1069 UNREGISTER_INI_ENTRIES();
1074 #if defined(COMPILE_DL_PSI) && defined(ZTS)
1075 PHP_RINIT_FUNCTION(psi
)
1077 ZEND_TSRMLS_CACHE_UPDATE();
1082 PHP_MINFO_FUNCTION(psi
)
1084 php_info_print_table_start();
1085 php_info_print_table_header(2, "psi support", "enabled");
1086 php_info_print_table_end();
1088 DISPLAY_INI_ENTRIES();
1090 const zend_function_entry psi_functions
[] = {
1094 zend_module_entry psi_module_entry
= {
1095 STANDARD_MODULE_HEADER
,
1100 #if defined(COMPILE_DL_PSI) && defined(ZTS)
1101 PHP_RINIT(psi
), /* Replace with NULL if there's nothing to do at request start */
1108 STANDARD_MODULE_PROPERTIES
1111 #ifdef COMPILE_DL_PSI
1113 ZEND_TSRMLS_CACHE_DEFINE();
1115 ZEND_GET_MODULE(psi
)
1123 * vim600: noet sw=4 ts=4 fdm=marker
1124 * vim<600: noet sw=4 ts=4