- size_t i;
-
- for (i = 0; i < impl->func->args->count; ++i ) {
- impl_arg *iarg = impl->func->args->args[i];
-
- switch (iarg->type->type) {
- case PSI_T_STRING:
- if (iarg->val.zend.str) {
- zend_string_release(iarg->val.zend.str);
- }
- break;
- }
- }
-
- if (impl->decl->args) for (i = 0; i < impl->decl->args->count; ++i) {
- decl_arg *darg = impl->decl->args->args[i];
-
- if (darg->mem) {
- decl_type *type = real_decl_type(darg->type);
-
- if (type->type == PSI_T_STRUCT) {
- void **ptr = (void **) ((char *) darg->mem + type->strct->size);
-
- while (*ptr) {
- efree(*ptr++);
- }
- }
- efree(darg->mem);
- darg->mem = NULL;
- }
- }
-
- if (impl->func->args->vararg.args) {
- free_impl_args(impl->func->args->vararg.args);
- impl->func->args->vararg.args = NULL;
- }
- if (impl->func->args->vararg.types) {
- efree(impl->func->args->vararg.types);
- impl->func->args->vararg.types = NULL;
- }
- if (impl->func->args->vararg.values) {
- efree(impl->func->args->vararg.values);
- impl->func->args->vararg.values = NULL;
- }
- if (impl->func->args->vararg.free_list) {
- void **list = impl->func->args->vararg.free_list;
-
- while (*list) {
- efree(*list++);
- }
-
- efree(impl->func->args->vararg.free_list);
- impl->func->args->vararg.free_list = NULL;
- }
-}
-
-static inline int psi_calc_num_exp_value(num_exp *exp, impl_val *strct, impl_val *res) {
- impl_val *ref, *tmp = NULL;
-
- switch (exp->t) {
- case PSI_T_NUMBER:
- switch (is_numeric_string(exp->u.numb, strlen(exp->u.numb), (zend_long *) res, (double *) res, 0)) {
- case IS_LONG:
- return PSI_T_INT64;
- case IS_DOUBLE:
- return PSI_T_DOUBLE;
- }
- break;
-
- case PSI_T_NSNAME:
- switch (exp->u.cnst->type->type) {
- case PSI_T_INT:
- res->i64 = zend_get_constant_str(exp->u.cnst->name, strlen(exp->u.cnst->name))->value.lval;
- return PSI_T_INT64;
- case PSI_T_FLOAT:
- res->dval = zend_get_constant_str(exp->u.cnst->name, strlen(exp->u.cnst->name))->value.dval;
- return PSI_T_DOUBLE;
- default:
- return 0;
- }
- break;
-
- case PSI_T_NAME:
- if (strct) {
- ref = struct_member_ref(exp->u.dvar->arg, strct, &tmp);
- } else {
- ref = exp->u.dvar->arg->ptr;
- }
- switch (real_decl_type(exp->u.dvar->arg->type)->type) {
- case PSI_T_INT8:
- case PSI_T_UINT8:
- case PSI_T_INT16:
- case PSI_T_UINT16:
- case PSI_T_INT32:
- case PSI_T_UINT32:
- case PSI_T_INT64:
- case PSI_T_UINT64:
- memcpy(res, deref_impl_val(ref, exp->u.dvar), sizeof(*res));
- if (tmp) {
- free(tmp);
- }
- return real_decl_type(exp->u.dvar->arg->type)->type;
-
- case PSI_T_FLOAT:
- case PSI_T_DOUBLE:
- memcpy(res, deref_impl_val(ref, exp->u.dvar), sizeof(*res));
- if (tmp) {
- free(tmp);
- }
- return real_decl_type(exp->u.dvar->arg->type)->type;
-
- EMPTY_SWITCH_DEFAULT_CASE();
- }
- break;
-
- EMPTY_SWITCH_DEFAULT_CASE();
- }
- return 0;
-}
-
-int psi_calc_num_exp(num_exp *exp, impl_val *strct, impl_val *res) {
- impl_val num = {0};
- int num_type = psi_calc_num_exp_value(exp, strct, &num);
-
- if (exp->operand) {
- impl_val tmp = {0};
- int tmp_type = psi_calc_num_exp(exp->operand, strct, &tmp);
-
- return exp->calculator(num_type, &num, tmp_type, &tmp, res);
- }
-
- memcpy(res, &num, sizeof(*res));
- return num_type;
-}
-
-#define PRIfval "f"
-#define PRIdval "lf"
-
-#define PSI_CALC_OP(var) do { \
- const char *fmt = "calc %" PRI##var ", %" PRI##var ": %" PRI##var "\n"; \
- res->var = PSI_CALC(v1->var, v2->var); \
- if (!res->var) fprintf(stderr, fmt, v1->var, v2->var, res->var); \
-} while (0)
-#define PSI_CALC_OP2(vres, var1, var2) do { \
- const char *fmt = "calc %" PRI##var1 ", %" PRI##var2 ": %" PRI##vres "\n"; \
- res->vres = PSI_CALC(v1->var1, v2->var2); \
- if (!res->vres) fprintf(stderr, fmt, v1->var1, v2->var2, res->vres); \
-} while(0)
-#define PSI_CALC_FN(op) int psi_calc_##op(int t1, impl_val *v1, int t2, impl_val *v2, impl_val *res) \
-{ \
- if (t1 == t2) { \
- switch (t1) { \
- case PSI_T_FLOAT: PSI_CALC_OP(fval); break; \
- case PSI_T_DOUBLE: PSI_CALC_OP(dval); break; \
- case PSI_T_INT8: PSI_CALC_OP(i8); break; \
- case PSI_T_UINT8: PSI_CALC_OP(u8); break; \
- case PSI_T_INT16: PSI_CALC_OP(i16); break; \
- case PSI_T_UINT16: PSI_CALC_OP(u16); break; \
- case PSI_T_INT32: PSI_CALC_OP(i32); break; \
- case PSI_T_UINT32: PSI_CALC_OP(u32); break; \
- case PSI_T_INT64: PSI_CALC_OP(i64); break; \
- case PSI_T_UINT64: PSI_CALC_OP(u64); break; \
- EMPTY_SWITCH_DEFAULT_CASE(); \
- } \
- return t1; \
- } else if (t1 == PSI_T_DOUBLE) { \
- switch (t2) { \
- case PSI_T_FLOAT: PSI_CALC_OP2(dval, dval, fval); break; \
- case PSI_T_INT8: PSI_CALC_OP2(dval, dval, i8); break; \
- case PSI_T_UINT8: PSI_CALC_OP2(dval, dval, u8); break; \
- case PSI_T_INT16: PSI_CALC_OP2(dval, dval, i16); break; \
- case PSI_T_UINT16: PSI_CALC_OP2(dval, dval, u16); break; \
- case PSI_T_INT32: PSI_CALC_OP2(dval, dval, i32); break; \
- case PSI_T_UINT32: PSI_CALC_OP2(dval, dval, u32); break; \
- case PSI_T_INT64: PSI_CALC_OP2(dval, dval, i64); break; \
- case PSI_T_UINT64: PSI_CALC_OP2(dval, dval, u64); break; \
- EMPTY_SWITCH_DEFAULT_CASE(); \
- } \
- return t1; \
- } else if (t2 == PSI_T_DOUBLE) { \
- switch (t1) { \
- case PSI_T_FLOAT: PSI_CALC_OP2(dval, fval, dval); break; \
- case PSI_T_INT8: PSI_CALC_OP2(dval, i8, dval); break; \
- case PSI_T_UINT8: PSI_CALC_OP2(dval, u8, dval); break; \
- case PSI_T_INT16: PSI_CALC_OP2(dval, i16, dval); break; \
- case PSI_T_UINT16: PSI_CALC_OP2(dval, u16, dval); break; \
- case PSI_T_INT32: PSI_CALC_OP2(dval, i32, dval); break; \
- case PSI_T_UINT32: PSI_CALC_OP2(dval, u32, dval); break; \
- case PSI_T_INT64: PSI_CALC_OP2(dval, i64, dval); break; \
- case PSI_T_UINT64: PSI_CALC_OP2(dval, u64, dval); break; \
- EMPTY_SWITCH_DEFAULT_CASE(); \
- } \
- return t2; \
- } else if (t1 == PSI_T_FLOAT) { \
- switch (t2) { \
- case PSI_T_DOUBLE: PSI_CALC_OP2(dval, fval, dval); return t2; \
- case PSI_T_INT8: PSI_CALC_OP2(fval, fval, i8); break; \
- case PSI_T_UINT8: PSI_CALC_OP2(fval, fval, u8); break; \
- case PSI_T_INT16: PSI_CALC_OP2(fval, fval, i16); break; \
- case PSI_T_UINT16: PSI_CALC_OP2(fval, fval, u16); break; \
- case PSI_T_INT32: PSI_CALC_OP2(fval, fval, i32); break; \
- case PSI_T_UINT32: PSI_CALC_OP2(fval, fval, u32); break; \
- case PSI_T_INT64: PSI_CALC_OP2(fval, fval, i64); break; \
- case PSI_T_UINT64: PSI_CALC_OP2(fval, fval, u64); break; \
- EMPTY_SWITCH_DEFAULT_CASE(); \
- } \
- return t1; \
- } else if (t2 == PSI_T_FLOAT) { \
- switch (t1) { \
- case PSI_T_DOUBLE: PSI_CALC_OP2(dval, dval, fval); return t1; \
- case PSI_T_INT8: PSI_CALC_OP2(fval, i8, fval); break; \
- case PSI_T_UINT8: PSI_CALC_OP2(fval, u8, fval); break; \
- case PSI_T_INT16: PSI_CALC_OP2(fval, i16, fval); break; \
- case PSI_T_UINT16: PSI_CALC_OP2(fval, u16, fval); break; \
- case PSI_T_INT32: PSI_CALC_OP2(fval, i32, fval); break; \
- case PSI_T_UINT32: PSI_CALC_OP2(fval, u32, fval); break; \
- case PSI_T_INT64: PSI_CALC_OP2(fval, i64, fval); break; \
- case PSI_T_UINT64: PSI_CALC_OP2(fval, u64, fval); break; \
- EMPTY_SWITCH_DEFAULT_CASE(); \
- } \
- return t2; \
- } else { \
- int64_t sval1 = v1->i64, sval2 = v2->i64; \
- uint64_t uval1 = v1->u64, uval2 = v2->u64; \
- switch (t1) { \
- case PSI_T_INT8: sval1 >>= 8; \
- case PSI_T_INT16: sval1 >>= 8; \
- case PSI_T_INT32: sval1 >>= 8; \
- case PSI_T_INT64: \
- switch (t2) { \
- case PSI_T_INT8: sval2 >>= 8; \
- case PSI_T_INT16: sval2 >>= 8; \
- case PSI_T_INT32: sval2 >>= 8; \
- case PSI_T_INT64: \
- res->i64 = PSI_CALC(sval1 , sval2); \
- return PSI_T_INT64; \
- case PSI_T_UINT8: uval2 >>= 8; \
- case PSI_T_UINT16: uval2 >>= 8; \
- case PSI_T_UINT32: uval2 >>= 8; \
- case PSI_T_UINT64: \
- res->i64 = PSI_CALC(sval1, uval2); \
- return PSI_T_INT64; \
- } \
- break; \
- case PSI_T_UINT8: uval1 >>= 8; \
- case PSI_T_UINT16: uval1 >>= 8; \
- case PSI_T_UINT32: uval1 >>= 8; \
- case PSI_T_UINT64: \
- switch (t2) { \
- case PSI_T_INT8: sval2 >>= 8; \
- case PSI_T_INT16: sval2 >>= 8; \
- case PSI_T_INT32: sval2 >>= 8; \
- case PSI_T_INT64: \
- res->i64 = PSI_CALC(uval1, sval2); \
- return PSI_T_INT64; \
- case PSI_T_UINT8: uval2 >>= 8; \
- case PSI_T_UINT16: uval2 >>= 8; \
- case PSI_T_UINT32: uval2 >>= 8; \
- case PSI_T_UINT64: \
- res->u64 = PSI_CALC(uval1, uval2); \
- return PSI_T_UINT64; \
- } \
- break; \
- } \
- } \
- ZEND_ASSERT(0); \
- return 0; \
-}
-
-#undef PSI_CALC
-#define PSI_CALC(var1, var2) (var1) + (var2)
-PSI_CALC_FN(add)
-#undef PSI_CALC
-#define PSI_CALC(var1, var2) (var1) * (var2)
-PSI_CALC_FN(mul)
-#undef PSI_CALC
-#define PSI_CALC(var1, var2) (var1) - (var2)
-PSI_CALC_FN(sub)
-#undef PSI_CALC
-#define PSI_CALC(var1, var2) (var1) / (var2)
-PSI_CALC_FN(div)
-
-static inline void psi_do_args(impl *impl) {
- size_t i;
-
- for (i = 0; i < impl->decl->args->count; ++i) {
- impl->decl->call.args[i] = impl->decl->args->args[i]->let->ptr;
- }
-}
-
-static inline impl_vararg *psi_do_varargs(impl *impl) {
- size_t i, j;
- impl_vararg *va = &impl->func->args->vararg;
- size_t vacount = va->args->count;
-
-
- if (!vacount) {
- return NULL;
- }
-
- va->types = ecalloc(vacount, sizeof(*va->types));
- va->values = ecalloc(vacount, sizeof(*va->values));
-
- for (i = 0, j = 0; i < vacount; ++i) {
- impl_arg *vaarg = va->args->args[i];
- void *to_free = NULL;
- token_t let_fn, vatype = va->name->type->type;
-
- if (vatype == PSI_T_MIXED) {
- switch (Z_TYPE_P(vaarg->_zv)) {
- case IS_TRUE:
- case IS_FALSE: vatype = PSI_T_BOOL; break;
- case IS_LONG: vatype = PSI_T_INT; break;
- case IS_DOUBLE: vatype = PSI_T_FLOAT; break;
- default: vatype = PSI_T_STRING; break;
- }
- }
-
-
- switch (vatype) {
- case PSI_T_BOOL: let_fn = PSI_T_BOOLVAL; break;
- case PSI_T_INT: let_fn = PSI_T_INTVAL; break;
- case PSI_T_FLOAT:
- case PSI_T_DOUBLE: let_fn = PSI_T_FLOATVAL;break;
- case PSI_T_STRING: let_fn = PSI_T_STRVAL; break;
- EMPTY_SWITCH_DEFAULT_CASE();
- }
-
- va->types[i] = vatype;
- psi_let_val(let_fn, vaarg, &va->values[i], NULL, &to_free);
-
- if (to_free) {
- if (!va->free_list) {
- va->free_list = ecalloc(vacount - i + 1, sizeof(*va->free_list));
- }
- va->free_list[j++] = to_free;
- }
- }
-
- return va;
-}
-
-void psi_call(zend_execute_data *execute_data, zval *return_value, impl *impl)
-{
- size_t i;
- impl_vararg *va = NULL;
-
- memset(impl->decl->func->ptr, 0, sizeof(impl_val));
-
- if (SUCCESS != psi_parse_args(execute_data, impl)) {
- return;
- }
-
- for (i = 0; i < impl->stmts->let.count; ++i) {
- let_stmt *let = impl->stmts->let.list[i];
-
- if (!psi_do_let(let)) {
- psi_do_return(return_value, impl->stmts->ret.list[0]);
- psi_do_clean(impl);
- return;
- }
- }
-
- if (impl->decl->args) {
- psi_do_args(impl);
-
- if (impl->func->args->vararg.args) {
- va = psi_do_varargs(impl);
- }
- }
-
- PSI_ContextCall(&PSI_G(context), &impl->decl->call, va);
- psi_do_return(return_value, impl->stmts->ret.list[0]);
-
- for (i = 0; i < impl->stmts->set.count; ++i) {
- set_stmt *set = impl->stmts->set.list[i];
-
- if (set->arg->_zv) {
- psi_do_set(set->arg->_zv, set->val);
- }
- }
-
- for (i = 0; i < impl->stmts->fre.count; ++i) {
- free_stmt *fre = impl->stmts->fre.list[i];
-
- psi_do_free(fre);
- }
- psi_do_clean(impl);
-}
-
-static void psi_object_free(zend_object *o)
-{
- psi_object *obj = PSI_OBJ(NULL, o);
-
- if (obj->data) {
- // free(obj->data);
- obj->data = NULL;
- }
- zend_object_std_dtor(o);
-}
-
-static zend_object *psi_object_init(zend_class_entry *ce)
-{
- psi_object *o = ecalloc(1, sizeof(*o) + zend_object_properties_size(ce));
-
- zend_object_std_init(&o->std, ce);
- object_properties_init(&o->std, ce);
- o->std.handlers = &psi_object_handlers;
- return &o->std;
-}
-
-PHP_MINIT_FUNCTION(psi)
-{
- PSI_ContextOps *ops = NULL;