+/*******************************************************************************
+ Copyright (c) 2016, Michael Wallner <mike@php.net>.
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *******************************************************************************/
+
+#include "php_psi_stdinc.h"
+#include "context.h"
+#include "call.h"
#include "php.h"
-#include "libjit.h"
-#include "parser_proc.h"
-#include "parser.h"
+
+#ifdef HAVE_LIBJIT
#include <jit/jit.h>
-static void init(PSI_Context *C)
-{
- C->context = jit_context_create();
-}
+static inline jit_type_t psi_jit_decl_arg_type(struct psi_decl_arg *darg);
-static void dtor(PSI_Context *C)
+static inline jit_abi_t psi_jit_abi(const char *convention)
{
- jit_context_destroy(C->context);
- C->context = NULL;
-}
-
-static inline size_t impl_num_min_args(impl *impl) {
- size_t i, n = impl->func->args->count;
-
- for (i = 0; i < impl->func->args->count; ++i) {
- if (impl->func->args->args[i]->def) {
- --n;
- }
- }
- return n;
-}
-
-static inline jit_abi_t psi_jit_abi(const char *convention) {
return jit_abi_cdecl;
}
-static inline jit_type_t psi_jit_type(token_t t) {
+static inline jit_type_t psi_jit_token_type(token_t t)
+{
switch (t) {
default:
- ZEND_ASSERT(0);
+ assert(0);
/* no break */
case PSI_T_VOID:
return jit_type_void;
- case PSI_T_SINT8:
+ case PSI_T_INT8:
return jit_type_sbyte;
case PSI_T_UINT8:
return jit_type_ubyte;
- case PSI_T_SINT16:
+ case PSI_T_INT16:
return jit_type_short;
case PSI_T_UINT16:
return jit_type_ushort;
- case PSI_T_SINT32:
+ case PSI_T_INT32:
return jit_type_int;
case PSI_T_UINT32:
return jit_type_uint;
- case PSI_T_SINT64:
+ case PSI_T_INT64:
return jit_type_long;
case PSI_T_UINT64:
return jit_type_ulong;
case PSI_T_BOOL:
return jit_type_sys_bool;
- case PSI_T_CHAR:
- return jit_type_sys_char;
- case PSI_T_SHORT:
- return jit_type_sys_short;
case PSI_T_INT:
+ case PSI_T_ENUM:
return jit_type_sys_int;
case PSI_T_LONG:
return jit_type_sys_long;
return jit_type_sys_float;
case PSI_T_DOUBLE:
return jit_type_sys_double;
+#ifdef HAVE_LONG_DOUBLE
+ case PSI_T_LONG_DOUBLE:
+ return jit_type_sys_long_double;
+#endif
+ case PSI_T_POINTER:
+ case PSI_T_FUNCTION:
+ return jit_type_void_ptr;
}
}
-static inline jit_type_t psi_jit_decl_type(decl_type *type) {
- return psi_jit_type(real_decl_type(type)->type);
-}
-static inline jit_type_t psi_jit_decl_arg_type(decl_arg *darg) {
- if (darg->var->pointer_level) {
+static inline jit_type_t psi_jit_impl_type(token_t impl_type)
+{
+ switch (impl_type) {
+ case PSI_T_BOOL:
+ return jit_type_sbyte;
+ case PSI_T_INT:
+ return jit_type_long;
+ case PSI_T_STRING:
return jit_type_void_ptr;
- } else {
- return psi_jit_decl_type(darg->type);
+ case PSI_T_FLOAT:
+ case PSI_T_DOUBLE:
+ return jit_type_sys_double;
+ EMPTY_SWITCH_DEFAULT_CASE()
+ ;
}
+ return NULL;
}
-typedef struct PSI_ClosureData {
- void *context;
- impl *impl;
- jit_type_t signature;
- jit_type_t params[1];
-} PSI_ClosureData;
+static void psi_jit_struct_type_dtor(void *type)
+{
+ jit_type_t strct = type;
-static inline PSI_ClosureData *PSI_ClosureDataAlloc(void *context, impl *impl) {
- size_t i, c = impl->decl->args->count;
- PSI_ClosureData *data = malloc(sizeof(*data) + (c ? c-1 : c) * sizeof(jit_type_t));
+ jit_type_free(strct);
+}
- data->context = context;
- data->impl = impl;
- for (i = 0; i < c; ++i) {
- data->params[i] = psi_jit_decl_arg_type(impl->decl->args->args[i]);
+static size_t psi_jit_struct_type_pad(jit_type_t *els, size_t padding)
+{
+ size_t i;
+
+ for (i = 0; i < padding; ++i) {
+ *els++ = jit_type_copy(jit_type_sys_char);
}
- data->signature = jit_type_create_signature(
- psi_jit_abi(data->impl->decl->abi->convention),
- psi_jit_decl_arg_type(data->impl->decl->func),
- data->params,
- data->impl->decl->args->count,
- 1);
- return data;
+
+ return padding;
}
+static unsigned psi_jit_struct_type_elements(struct psi_decl_struct *strct,
+ jit_type_t **fields)
+{
+ size_t i = 0, argc = psi_plist_count(strct->args), nels = 0, offset = 0,
+ maxalign;
+ struct psi_decl_arg *darg;
+
+ *fields = calloc(argc + 1, sizeof(*fields));
-static inline impl_val *deref(unsigned level, impl_val *ret_val, decl_arg *darg) {
- unsigned i;
+ while (psi_plist_get(strct->args, i++, &darg)) {
+ jit_type_t type = jit_type_copy(psi_jit_decl_arg_type(darg));
+ size_t padding, alignment;
- for (i = level; i < darg->var->pointer_level; ++i && ret_val->ptr) {
- ret_val = *(void **)ret_val;
+ if ((alignment = jit_type_get_alignment(type)) > maxalign) {
+ maxalign = alignment;
+ }
+
+ assert(jit_type_get_size(type) == darg->layout->len);
+ if ((padding = psi_offset_padding(darg->layout->pos - offset, alignment))) {
+ if (nels + padding > argc) {
+ argc += padding;
+ *fields = realloc(*fields, (argc + 1) * sizeof(*fields));
+ }
+ psi_jit_struct_type_pad(&(*fields)[nels], padding);
+ nels += padding;
+ offset += padding;
+ }
+ assert(offset == darg->layout->pos);
+
+ offset = (offset + darg->layout->len + alignment - 1)
+ & ~(alignment - 1);
+ (*fields)[nels++] = type;
}
- return ret_val;
-}
-static void to_int(impl_val *ret_val, decl_arg *func, zval *return_value) {
- switch (real_decl_type(func->type)->type) {
- case PSI_T_FLOAT:
- case PSI_T_DOUBLE:
- RETVAL_DOUBLE(deref(0, ret_val, func)->dval);
- convert_to_long(return_value);
- break;
- default:
- RETVAL_LONG(deref(0, ret_val, func)->lval);
+ /* apply struct alignment padding */
+ offset = (offset + maxalign - 1) & ~(maxalign - 1);
+
+ assert(offset <= strct->size);
+ if (offset < strct->size) {
+ nels += psi_jit_struct_type_pad(&(*fields)[nels], strct->size - offset);
}
+
+ return nels;
}
-static void to_string(impl_val *ret_val, decl_arg *func, zval *return_value) {
- switch (real_decl_type(func->type)->type) {
- case PSI_T_CHAR:
- case PSI_T_SINT8:
- case PSI_T_UINT8:
- if (!func->var->pointer_level) {
- char chr = ret_val->lval;
- RETVAL_STRINGL(&chr, 1);
- } else {
- ret_val = deref(1, ret_val, func);
- if (ret_val->ptr) {
- RETVAL_STRING(ret_val->ptr);
- } else {
- RETVAL_EMPTY_STRING();
- }
+static inline jit_type_t psi_jit_decl_type(struct psi_decl_type *type)
+{
+ struct psi_decl_type *real = psi_decl_type_get_real(type);
+
+ switch (real->type) {
+ case PSI_T_STRUCT:
+ if (!real->real.strct->engine.type) {
+ unsigned count;
+ jit_type_t strct, *fields = NULL;
+
+ count = psi_jit_struct_type_elements(real->real.strct, &fields);
+ strct = jit_type_create_struct(fields, count, 0);
+
+ real->real.strct->engine.type = strct;
+ real->real.strct->engine.dtor = psi_jit_struct_type_dtor;
}
- break;
- case PSI_T_FLOAT:
- case PSI_T_DOUBLE:
- RETVAL_DOUBLE(deref(0, ret_val, func)->dval);
- convert_to_string(return_value);
- break;
+
+ return real->real.strct->engine.type;
+
+ case PSI_T_UNION:
+ {
+ struct psi_decl_arg *arg;
+ psi_plist_get(real->real.unn->args, 0, &arg);
+ return psi_jit_decl_arg_type(arg);
+ }
+
default:
- RETVAL_LONG(deref(0, ret_val, func)->lval);
- convert_to_string(return_value);
- break;
+ return psi_jit_token_type(real->type);
+ }
+}
+static inline jit_type_t psi_jit_decl_arg_type(struct psi_decl_arg *darg)
+{
+ if (darg->var->pointer_level) {
+ return jit_type_void_ptr;
+ } else {
+ return psi_jit_decl_type(darg->type);
}
}
-static ZEND_RESULT_CODE handle_args(zend_execute_data *execute_data, impl *impl) {
- impl_arg *iarg;
+struct psi_jit_context {
+ jit_context_t jit;
+ jit_type_t signature;
+};
- if (!impl->func->args->count) {
- return zend_parse_parameters_none();
- }
+struct psi_jit_call {
+ struct psi_context *context;
+ union {
+ struct {
+ struct psi_impl *impl;
+ struct psi_call_frame *frame;
+ } fn;
+ struct {
+ struct psi_let_exp *let_exp;
+ struct psi_jit_call *impl_call;
+ } cb;
+ } impl;
+ void *closure;
+ jit_type_t signature;
+ void *params[1]; /* [type1, type2, ... ] */
+};
- ZEND_PARSE_PARAMETERS_START(impl_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.cval = iarg->def->type == PSI_T_TRUE ? 1 : 0;
- }
- Z_PARAM_BOOL(iarg->val.cval);
- } else if (PSI_T_INT == iarg->type->type) {
- if (iarg->def) {
- iarg->val.lval = zend_atol(iarg->def->text, strlen(iarg->def->text));
- }
- Z_PARAM_LONG(iarg->val.lval);
- } else if (PSI_T_FLOAT == 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.str, 1, 0);
- if (iarg->val.str) {
- zend_string_addref(iarg->val.str);
- } else if (iarg->def) {
- iarg->val.str = zend_string_init(str.val, str.len, 0);
- }
- } else {
- error_code = ZPP_ERROR_FAILURE;
- break;
- }
- iarg->_zv = _arg;
- if (_i < _max_num_args) {
- goto nextarg;
- }
- ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
+static void psi_jit_handler(jit_type_t sig, void *result, void **args, void *data)
+{
+ struct psi_jit_call *call = data;
- return SUCCESS;
+ psi_context_call(call->context, *(zend_execute_data **)args[0], *(zval **) args[1], call->impl.fn.impl);
}
-static impl_val *handle_let(decl_arg *darg, impl_arg *iarg) {
- impl_val *arg_val = &darg->let->out;
+static void psi_jit_callback(jit_type_t sig, void *result, void **args,
+ void *data)
+{
+ struct psi_jit_call *call = data, *impl_call = call->impl.cb.impl_call;
+ struct psi_call_frame_callback cbdata;
- if (!iarg) {
- /* let foo = NULL */
- memset(arg_val, 0, sizeof(*arg_val));
- return arg_val;
- }
- switch (darg->let->val->func->type) {
- case PSI_T_BOOLVAL:
- if (iarg->type->type == PSI_T_BOOL) {
- arg_val->cval = iarg->val.cval;
- } 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.lval;
- } else {
- arg_val->lval = zval_get_long(iarg->_zv);
- }
- break;
- case PSI_T_STRVAL:
- if (iarg->type->type == PSI_T_STRING) {
- arg_val->ptr = estrdup(iarg->val.str->val);
- darg->let->mem = arg_val->ptr;
- zend_string_release(iarg->val.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);
- }
- break;
- case PSI_T_STRLEN:
- if (iarg->type->type == PSI_T_STRING) {
- arg_val->lval = iarg->val.str->len;
- zend_string_release(iarg->val.str);
- } else {
- zend_string *zs = zval_get_string(iarg->_zv);
- arg_val->lval = zs->len;
- zend_string_release(zs);
- }
- break;
- EMPTY_SWITCH_DEFAULT_CASE();
- }
+ cbdata.cb = call->impl.cb.let_exp;
+ cbdata.argc = jit_type_num_params(sig);
+ cbdata.argv = args;
+ cbdata.rval = result;
- return arg_val;
+ psi_call_frame_do_callback(impl_call->impl.fn.frame, &cbdata);
}
-static void handle_rval(impl *impl, impl_val *ret_val, zval *return_value) {
- switch (impl->stmts->ret.list[0]->func->type) {
- case PSI_T_TO_STRING:
- to_string(ret_val, impl->decl->func, return_value);
- break;
- case PSI_T_TO_INT:
- to_int(ret_val, impl->decl->func, return_value);
- break;
- EMPTY_SWITCH_DEFAULT_CASE();
+static inline struct psi_jit_call *psi_jit_call_alloc(struct psi_context *C,
+ struct psi_decl *decl)
+{
+ size_t i, c = psi_plist_count(decl->args);
+ struct psi_jit_call *call = calloc(1, sizeof(*call) + 2 * c * sizeof(void *));
+ struct psi_decl_arg *arg;
+
+ decl->info = call;
+ call->context = C;
+ for (i = 0; psi_plist_get(decl->args, i, &arg); ++i) {
+ call->params[i] = psi_jit_decl_arg_type(arg);
}
-}
+ call->params[c] = NULL;
-static void handle_set(zval *return_value, set_func *func, decl_vars *vars) {
- impl_val *val = &vars->vars[0]->arg->let->ptr;
+ call->signature = jit_type_create_signature(
+ psi_jit_abi(decl->abi->convention),
+ psi_jit_decl_arg_type(decl->func),
+ (jit_type_t *) call->params,
+ c, 1);
+ assert(call->signature);
- ZVAL_DEREF(return_value);
- zval_dtor(return_value);
+ return call;
+}
- switch (func->type) {
- case PSI_T_TO_STRING:
- to_string(val, vars->vars[0]->arg, return_value);
- break;
- EMPTY_SWITCH_DEFAULT_CASE();
- }
+static inline void *psi_jit_call_init_closure(struct psi_context *C,
+ struct psi_jit_call *call, struct psi_impl *impl)
+{
+ struct psi_jit_context *context = C->context;
+
+ call->impl.fn.impl = impl;
+ return call->closure = jit_closure_create(context->jit, context->signature,
+ &psi_jit_handler, call);
}
-static void handle_free(free_stmt *fre) {
- size_t i;
+static inline void *psi_jit_call_init_callback_closure(struct psi_context *C,
+ struct psi_jit_call *call, struct psi_jit_call *impl_call,
+ struct psi_let_exp *cb)
+{
+ struct psi_jit_context *context = C->context;
- for (i = 0; i < fre->vars->count; ++i) {
- decl_var *dvar = fre->vars->vars[i];
+ call->impl.cb.let_exp = cb;
+ call->impl.cb.impl_call = impl_call;
- if (dvar->arg && dvar->arg->let->out.ptr) {
- free(dvar->arg->let->out.ptr);
- dvar->arg->let->out.ptr = NULL;
- }
- }
+ return call->closure = jit_closure_create(context->jit, call->signature,
+ &psi_jit_callback, call);
}
-static void handler(jit_type_t _sig, void *result, void **_args, void *_data)
+static inline void psi_jit_call_free(struct psi_jit_call *call)
{
- PSI_ClosureData *data = _data;
- size_t i;
- void **arg_ptr = NULL, **arg_prm = NULL;
- impl_val ret_val;
+ jit_type_free(call->signature);
+ free(call);
+}
+
+static inline struct psi_jit_context *psi_jit_context_init(
+ struct psi_jit_context *L)
+{
+ jit_type_t params[] = {jit_type_void_ptr, jit_type_void_ptr};
- if (SUCCESS != handle_args(*(zend_execute_data **)_args[0], data->impl)) {
- return;
+ if (!L) {
+ L = malloc(sizeof(*L));
}
+ memset(L, 0, sizeof(*L));
- if (data->impl->decl->args->count) {
- arg_ptr = malloc(data->impl->decl->args->count * sizeof(*arg_ptr));
- arg_prm = malloc(data->impl->decl->args->count * sizeof(*arg_prm));
+ L->jit = jit_context_create();
+ L->signature = jit_type_create_signature(jit_abi_cdecl, jit_type_void,
+ params, 2, 1);
- for (i = 0; i < data->impl->decl->args->count; ++i) {
- decl_arg *darg = data->impl->decl->args->args[i];
- impl_arg *iarg = darg->let ? darg->let->arg : NULL;
+ return L;
+}
- arg_ptr[i] = handle_let(darg, iarg);
- arg_prm[i] = darg->let->val->is_reference ? &arg_ptr[i] : arg_ptr[i];
+static inline void psi_jit_context_dtor(struct psi_jit_context *L)
+{
+ jit_type_free(L->signature);
+ jit_context_destroy(L->jit);
+}
- darg->let->ptr = arg_ptr[i];
- }
+static inline void psi_jit_context_free(struct psi_jit_context **L)
+{
+ if (*L) {
+ psi_jit_context_dtor(*L);
+ free(*L);
+ *L = NULL;
}
+}
- jit_apply(data->signature, data->impl->decl->dlptr, arg_prm, data->impl->decl->args->count, &ret_val);
+static void psi_jit_init(struct psi_context *C)
+{
+ C->context = psi_jit_context_init(NULL);
+}
- handle_rval(data->impl, &ret_val, *(zval **)_args[1]);
+static inline void psi_jit_destroy_callbacks(struct psi_context *C,
+ struct psi_let_exp *let_exp)
+{
+ struct psi_let_callback *cb;
+ struct psi_let_func *fn = NULL;
+
+ switch (let_exp->kind) {
+ case PSI_LET_CALLBACK:
+ cb = let_exp->data.callback;
+
+ if (cb->decl && cb->decl->info) {
+ psi_jit_call_free(cb->decl->info);
+ }
+ fn = cb->func;
+ /* no break */
+ case PSI_LET_FUNC:
+ if (!fn) {
+ fn = let_exp->data.func;
+ }
- for (i = 0; i < data->impl->stmts->set.count; ++i) {
- set_stmt *set = data->impl->stmts->set.list[i];
+ if (fn->inner) {
+ size_t i = 0;
+ struct psi_let_exp *inner_let;
- handle_set(set->arg->_zv, set->val->func, set->val->vars);
+ while (psi_plist_get(fn->inner, i++, &inner_let)) {
+ psi_jit_destroy_callbacks(C, inner_let);
+ }
+ }
+ break;
+ default:
+ break;
}
+}
- if (data->impl->decl->args->count) {
- for (i = 0; i < data->impl->decl->args->count; ++i) {
- decl_arg *darg = data->impl->decl->args->args[i];
+static void psi_jit_dtor(struct psi_context *C)
+{
+ if (C->decls) {
+ size_t i = 0;
+ struct psi_decl *decl;
- if (darg->let && darg->let->mem) {
- efree(darg->let->mem);
- darg->let->mem = NULL;
+ while (psi_plist_get(C->decls, i++, &decl)) {
+ if (decl->info) {
+ psi_jit_call_free(decl->info);
}
}
- free(arg_ptr);
- free(arg_prm);
}
+ if (C->impls) {
+ size_t i = 0;
+ struct psi_impl *impl;
- for (i = 0; i < data->impl->stmts->fre.count; ++i) {
- free_stmt *fre = data->impl->stmts->fre.list[i];
+ while (psi_plist_get(C->impls, i++, &impl)) {
+ size_t l = 0;
+ struct psi_let_stmt *let;
- handle_free(fre);
+ while (psi_plist_get(impl->stmts.let, l++, &let)) {
+ psi_jit_destroy_callbacks(C, let->exp);
+ }
+ }
}
+ psi_jit_context_free((void *) &C->context);
}
-static inline int fill_type_hint(impl_type *type) {
- switch (type->type) {
- case PSI_T_BOOL:
- return _IS_BOOL;
- case PSI_T_INT:
- case PSI_T_LONG:
- return IS_LONG;
- case PSI_T_FLOAT:
- case PSI_T_DOUBLE:
- return IS_DOUBLE;
- case PSI_T_STRING:
- return IS_STRING;
- case PSI_T_ARRAY:
- return IS_ARRAY;
+static inline void psi_jit_compile_callbacks(struct psi_context *C,
+ struct psi_jit_call *impl_call, struct psi_let_exp *let_exp)
+{
+ struct psi_jit_call *call;
+ struct psi_let_callback *cb;
+ struct psi_let_func *fn = NULL;
+
+ switch (let_exp->kind) {
+ case PSI_LET_CALLBACK:
+ cb = let_exp->data.callback;
+ if ((call = psi_jit_call_alloc(C, cb->decl))) {
+ if (!psi_jit_call_init_callback_closure(C, call, impl_call, let_exp)) {
+ psi_jit_call_free(call);
+ break;
+ }
+
+ cb->decl->sym = call->closure;
+ }
+ fn = cb->func;
+ /* no break */
+ case PSI_LET_FUNC:
+ if (!fn) {
+ fn = let_exp->data.func;
+ }
+ if (fn->inner) {
+ size_t i = 0;
+ struct psi_let_exp *inner_let;
+
+ while (psi_plist_get(fn->inner, i++, &inner_let)) {
+ psi_jit_compile_callbacks(C, impl_call, inner_let);
+ }
+ }
+ break;
default:
- return 0;
+ break;
}
}
-static inline zend_internal_arg_info *fill_arginfo(impl *impl) {
- size_t i;
- zend_internal_arg_info *aip;
- zend_internal_function_info *fi;
+static zend_function_entry *psi_jit_compile(struct psi_context *C)
+{
+ size_t i = 0, d = 0, nf = 0;
+ struct psi_impl *impl;
+ struct psi_decl *decl;
+ zend_function_entry *zfe;
+ struct psi_jit_context *ctx = C->context;
+
+ if (!C->impls) {
+ return NULL;
+ }
+
+ zfe = calloc(psi_plist_count(C->impls) + 1, sizeof(*zfe));
+ jit_context_build_start(ctx->jit);
- aip = calloc(impl->func->args->count + 1, sizeof(*aip));
+ while (psi_plist_get(C->impls, i++, &impl)) {
+ zend_function_entry *zf = &zfe[nf];
+ struct psi_jit_call *call;
+ size_t l = 0;
+ struct psi_let_stmt *let;
- fi = (zend_internal_function_info *) &aip[0];
- fi->required_num_args = impl_num_min_args(impl);
- fi->return_reference = impl->func->return_reference;
- fi->type_hint = fill_type_hint(impl->func->return_type);
+ if (!impl->decl) {
+ continue;
+ }
+ if (!(call = psi_jit_call_alloc(C, impl->decl))) {
+ continue;
+ }
+ if (!psi_jit_call_init_closure(C, call, impl)) {
+ psi_jit_call_free(call);
+ continue;
+ }
- for (i = 0; i < impl->func->args->count; ++i) {
- impl_arg *iarg = impl->func->args->args[i];
- zend_internal_arg_info *ai = &aip[i+1];
+ zf->fname = impl->func->name + (impl->func->name[0] == '\\');
+ zf->handler = call->closure;
+ zf->num_args = psi_plist_count(impl->func->args);
+ zf->arg_info = psi_internal_arginfo(impl);
+ ++nf;
- ai->name = iarg->var->name;
- ai->type_hint = fill_type_hint(iarg->type);
- if (iarg->var->reference) {
- ai->pass_by_reference = 1;
+ while (psi_plist_get(impl->stmts.let, l++, &let)) {
+ psi_jit_compile_callbacks(C, call, let->exp);
}
- if (iarg->var->reference || (iarg->def && iarg->def->type == PSI_T_NULL)) {
- ai->allow_null = 1;
+ }
+
+ while (psi_plist_get(C->decls, d++, &decl)) {
+ if (decl->info) {
+ continue;
}
+
+ psi_jit_call_alloc(C, decl);
}
- return aip;
+ jit_context_build_end(ctx->jit);
+
+ return zfe;
}
-static zend_function_entry *compile(PSI_Context *C, PSI_Data *D)
+static void psi_jit_call(struct psi_context *C, struct psi_call_frame *frame,
+ struct psi_decl *decl, void *rval, void **args)
{
- size_t i, j = 0;
- jit_type_t signature, params[] = {
- jit_type_void_ptr,
- jit_type_void_ptr
- };
- zend_function_entry *zfe = calloc(D->impls->count + 1, sizeof(*zfe));
+ struct psi_jit_call *call = decl->info;
+ struct psi_call_frame *prev = call->impl.fn.frame;
- jit_context_build_start(C->context);
+ call->impl.fn.frame = frame;
+ jit_apply(call->signature, decl->sym, args, psi_plist_count(decl->args), rval);
+ call->impl.fn.frame = prev;
+}
- for (i = 0; i < D->impls->count; ++i) {
- zend_function_entry *zf = &zfe[j];
- PSI_ClosureData *data;
+static void psi_jit_call_va(struct psi_context *C, struct psi_call_frame *frame,
+ struct psi_decl *decl, void *rval, void **args, size_t va_count,
+ void **va_types)
+{
+ struct psi_jit_call *info = decl->info;
+ struct psi_call_frame *prev = info->impl.fn.frame;
+ size_t argc = psi_plist_count(decl->args);
+ jit_type_t signature;
+ jit_type_t *param_types = ecalloc(argc + va_count + 1, sizeof(jit_type_t));
- if (!D->impls->list[i]->decl) {
- continue;
- }
+ memcpy(param_types, info->params, argc * sizeof(jit_type_t));
+ memcpy(param_types + argc, va_types, va_count * sizeof(jit_type_t));
- data = PSI_ClosureDataAlloc(C, D->impls->list[i]);
- signature = jit_type_create_signature(jit_abi_cdecl, jit_type_void, params, 2, 1);
- zf->fname = D->impls->list[i]->func->name + (D->impls->list[i]->func->name[0] == '\\');
- zf->handler = jit_closure_create(C->context, signature, &handler, data);
- zf->num_args = D->impls->list[i]->func->args->count;
- zf->arg_info = fill_arginfo(D->impls->list[i]);
- ++j;
- }
+ signature = jit_type_create_signature(jit_abi_vararg,
+ jit_type_get_return(info->signature), param_types, argc + va_count,
+ 1);
+ assert(signature);
- jit_context_build_end(C->context);
+ info->impl.fn.frame = frame;
+ jit_apply(signature, decl->sym, args, argc, rval);
+ info->impl.fn.frame = prev;
+ jit_type_free(signature);
+ efree(param_types);
+}
- return zfe;
+static void *psi_jit_query(struct psi_context *C, enum psi_context_query q,
+ void *arg)
+{
+ switch (q) {
+ case PSI_CONTEXT_QUERY_SELF:
+ return "jit";
+ case PSI_CONTEXT_QUERY_TYPE:
+ return psi_jit_impl_type(*(token_t *) arg);
+ }
+ return NULL;
}
-static PSI_ContextOps ops = {
- init,
- dtor,
- compile,
-};
+static struct psi_context_ops ops = {psi_jit_init, psi_jit_dtor,
+ psi_jit_compile, psi_jit_call, psi_jit_call_va, psi_jit_query};
-PSI_ContextOps *PSI_Libjit(void)
+struct psi_context_ops *psi_libjit_ops(void)
{
return &ops;
}
+
+#endif /* HAVE_LIBJIT */