+
+ ZEND_PARSE_PARAMETERS_START(psi_num_min_args(impl), impl->func->args->count)
+ nextarg:
+ iarg = impl->func->args->args[_i];
+ if (iarg->def) {
+ Z_PARAM_OPTIONAL;
+ }
+ if (PSI_T_BOOL == iarg->type->type) {
+ if (iarg->def) {
+ iarg->val.zend.bval = iarg->def->type == PSI_T_TRUE ? 1 : 0;
+ }
+ Z_PARAM_BOOL(iarg->val.zend.bval);
+ } else if (PSI_T_INT == iarg->type->type) {
+ if (iarg->def) {
+ iarg->val.zend.lval = zend_atol(iarg->def->text, strlen(iarg->def->text));
+ }
+ Z_PARAM_LONG(iarg->val.zend.lval);
+ } else if (PSI_T_FLOAT == iarg->type->type || PSI_T_DOUBLE == iarg->type->type) {
+ if (iarg->def) {
+ iarg->val.dval = zend_strtod(iarg->def->text, NULL);
+ }
+ Z_PARAM_DOUBLE(iarg->val.dval);
+ } else if (PSI_T_STRING == iarg->type->type) {
+ struct {char *val; size_t len;} str;
+ if (iarg->def) {
+ /* FIXME */
+ str.len = strlen(iarg->def->text) - 2;
+ str.val = &iarg->def->text[1];
+ }
+ Z_PARAM_STR_EX(iarg->val.zend.str, 1, 0);
+ if (iarg->val.zend.str) {
+ zend_string_addref(iarg->val.zend.str);
+ } else if (iarg->def) {
+ iarg->val.zend.str = zend_string_init(str.val, str.len, 0);
+ }
+ } else if (PSI_T_ARRAY == iarg->type->type) {
+ /* handled as _zv in let or set */
+ Z_PARAM_ARRAY_EX(iarg->_zv, 1, 0);
+ } else if (PSI_T_OBJECT == iarg->type->type) {
+ Z_PARAM_OBJECT_EX(iarg->_zv, 1, 0);
+ } else {
+ error_code = ZPP_ERROR_FAILURE;
+ break;
+ }
+ iarg->_zv = _arg;
+ if (_i < _max_num_args) {
+ goto nextarg;
+ }
+ ZEND_PARSE_PARAMETERS_END_EX(
+ zend_restore_error_handling(&zeh);
+ return FAILURE
+ );
+
+ zend_restore_error_handling(&zeh);
+ return SUCCESS;
+}
+
+static inline void *psi_do_calloc(let_calloc *alloc)
+{
+ zend_long n = psi_long_num_exp(alloc->nmemb), s = psi_long_num_exp(alloc->size);
+ void *mem = safe_emalloc(n, s, sizeof(void *));
+ memset(mem, 0, n * s + sizeof(void *));
+ return mem;
+}
+
+static inline void *psi_do_let(decl_arg *darg)
+{
+ impl_arg *iarg = darg->let->arg;
+ impl_val *arg_val;
+
+ darg->let->ptr = &darg->let->out;
+ arg_val = darg->let->ptr;
+
+ if (!iarg) {
+ /* let foo = calloc(1, long);
+ * let foo = NULL;
+ * let foo;
+ */
+ if (darg->let->val->func && darg->let->val->func->type == PSI_T_CALLOC) {
+ arg_val->ptr = psi_do_calloc(darg->let->val->func->alloc);
+ darg->let->mem = arg_val->ptr;
+ } else if (darg->var->array_size) {
+ arg_val->ptr = ecalloc(darg->var->array_size, sizeof(*arg_val));
+ darg->let->mem = arg_val->ptr;
+ } else {
+ memset(arg_val, 0, sizeof(*arg_val));
+ }
+ } else {
+
+ switch (darg->let->val->func->type) {
+ case PSI_T_BOOLVAL:
+ if (iarg->type->type == PSI_T_BOOL) {
+ arg_val->cval = iarg->val.zend.bval;
+ } else {
+ arg_val->cval = zend_is_true(iarg->_zv);
+ }
+ break;
+ case PSI_T_INTVAL:
+ if (iarg->type->type == PSI_T_INT) {
+ arg_val->lval = iarg->val.zend.lval;
+ } else {
+ arg_val->lval = zval_get_long(iarg->_zv);
+ }
+ break;
+ case PSI_T_PATHVAL:
+ case PSI_T_STRVAL:
+ if (iarg->type->type == PSI_T_STRING) {
+ arg_val->ptr = estrdup(iarg->val.zend.str->val);
+ darg->let->mem = arg_val->ptr;
+ zend_string_release(iarg->val.zend.str);
+ } else {
+ zend_string *zs = zval_get_string(iarg->_zv);
+ arg_val->ptr = estrdup(zs->val);
+ darg->let->mem = arg_val->ptr;
+ zend_string_release(zs);
+ }
+ if (PSI_T_PATHVAL == darg->let->val->func->type) {
+ if (SUCCESS != php_check_open_basedir(arg_val->ptr)) {
+ efree(arg_val->ptr);
+ return NULL;
+ }
+ }
+ break;
+ case PSI_T_STRLEN:
+ if (iarg->type->type == PSI_T_STRING) {
+ arg_val->lval = iarg->val.zend.str->len;
+ zend_string_release(iarg->val.zend.str);
+ } else {
+ zend_string *zs = zval_get_string(iarg->_zv);
+ arg_val->lval = zs->len;
+ zend_string_release(zs);
+ }
+ break;
+ case PSI_T_ARRVAL:
+ if (iarg->type->type == PSI_T_ARRAY) {
+ decl_type *type = real_decl_type(darg->type);
+
+ switch (type->type) {
+ case PSI_T_STRUCT:
+ arg_val->ptr = psi_array_to_struct(type->strct, HASH_OF(iarg->_zv));
+ darg->let->mem = arg_val->ptr;
+ break;
+ }
+ }
+ break;
+ case PSI_T_OBJVAL:
+ if (iarg->type->type == PSI_T_OBJECT) {
+ psi_object *obj;
+
+ if (!instanceof_function(Z_OBJCE_P(iarg->_zv), psi_class_entry)) {
+ return NULL;
+ }
+
+ obj = PSI_OBJ(iarg->_zv, NULL);
+ arg_val->ptr = obj->data;
+ }
+ break;
+ EMPTY_SWITCH_DEFAULT_CASE();
+ }
+ }
+
+ if (darg->let->val && darg->let->val->is_reference) {
+ return &darg->let->ptr;
+ } else {
+ return darg->let->ptr;
+ }
+}
+
+static inline void psi_do_set(zval *return_value, set_value *set)
+{
+ ZVAL_DEREF(return_value);
+ zval_dtor(return_value);
+
+ set->func->handler(return_value, set, set->vars->vars[0]->arg->let->ptr);
+}
+
+static inline void psi_do_return(zval *return_value, return_stmt *ret, impl_val *ret_val)
+{
+ ret->set->func->handler(return_value, ret->set, ret_val);
+}
+
+static inline void psi_do_free(free_stmt *fre)
+{
+ size_t i, j;
+ impl_val dummy;
+
+ for (i = 0; i < fre->calls->count; ++i) {
+ free_call *f = fre->calls->list[i];
+
+ for (j = 0; j < f->vars->count; ++j) {
+ decl_var *dvar = f->vars->vars[j];
+ decl_arg *darg = dvar->arg;
+
+ f->decl->call.args[j] = &darg->let->out;
+ }
+
+ /* FIXME: check in validate_* that free functions return scalar */
+ PSI_ContextCall(&PSI_G(context), &dummy, f->decl);
+ }
+}
+
+static inline void psi_do_clean(impl *impl)
+{
+ 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->let && darg->let->mem) {
+ decl_type *type = real_decl_type(darg->type);
+
+ if (type->type == PSI_T_STRUCT) {
+ void **ptr = (void **) ((char *) darg->let->mem + type->strct->size);
+
+ while (*ptr) {
+ efree(*ptr++);
+ }
+ }
+ efree(darg->let->mem);
+ darg->let->mem = NULL;
+ }
+ }
+}
+
+
+#define PSI_CALC_OP(var) res->var = PSI_CALC(v1->var, v2->var)
+#define PSI_CALC_OP2(vres, var1, var2) res->vres = PSI_CALC(v1->var1, v2->var2)
+
+int psi_calc_plus(unsigned char t1, impl_val *v1, unsigned char t2, impl_val *v2, impl_val *res)
+{
+#undef PSI_CALC
+#define PSI_CALC(var1, var2) (var1) + (var2)
+ 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;
+}
+
+void psi_call(zend_execute_data *execute_data, zval *return_value, impl *impl)
+{
+ impl_val ret_val;
+ size_t i;
+
+ if (SUCCESS != psi_parse_args(execute_data, impl)) {
+ return;
+ }
+
+ if (impl->decl->args) {
+ for (i = 0; i < impl->decl->args->count; ++i) {
+ decl_arg *darg = impl->decl->args->args[i];
+
+ if (!(impl->decl->call.args[i] = psi_do_let(darg))) {
+ goto cleanup;
+ }
+ }
+ }
+
+ memset(&ret_val, 0, sizeof(ret_val));
+ PSI_ContextCall(&PSI_G(context), &ret_val, impl->decl);
+
+ psi_do_return(return_value, impl->stmts->ret.list[0], &ret_val);
+
+ 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);
+ return;
+
+cleanup:
+ memset(&ret_val, 0, sizeof(ret_val));
+ psi_do_return(return_value, impl->stmts->ret.list[0], &ret_val);
+ 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;
+ zend_class_entry ce = {0};
+
+ REGISTER_INI_ENTRIES();
+
+ INIT_NS_CLASS_ENTRY(ce, "psi", "object", NULL);
+ psi_class_entry = zend_register_internal_class_ex(&ce, NULL);
+ psi_class_entry->create_object = psi_object_init;
+
+ memcpy(&psi_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
+ psi_object_handlers.offset = XtOffsetOf(psi_object, std);
+ psi_object_handlers.free_obj = psi_object_free;
+ psi_object_handlers.clone_obj = NULL;
+
+#ifdef HAVE_LIBJIT
+ if (!strcasecmp(PSI_G(engine), "jit")) {
+ ops = PSI_Libjit();
+ } else
+#endif
+#ifdef HAVE_LIBFFI
+ ops = PSI_Libffi();
+#endif
+
+ if (!ops) {
+ php_error(E_WARNING, "No PSI engine found");
+ return FAILURE;
+ }
+
+ PSI_ContextInit(&PSI_G(context), ops, psi_error);
+ PSI_ContextBuild(&PSI_G(context), PSI_G(directory));
+
+ if (psi_check_env("PSI_DUMP")) {
+ PSI_ContextDump(&PSI_G(context), STDOUT_FILENO);
+ }
+