+/*******************************************************************************
+ 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.
+ *******************************************************************************/
+
#ifdef HAVE_CONFIG_H
# include "config.h"
+#else
+# include "php_config.h"
#endif
-
+#include "context.h"
+#include "call.h"
#include "php.h"
#ifdef HAVE_LIBJIT
-#include "php_psi.h"
#include "libjit.h"
-#include "engine.h"
-
#include <jit/jit.h>
-static void psi_jit_handler(jit_type_t _sig, void *_result, void **_args, void *_data)
-{
- psi_call(*(zend_execute_data **)_args[0], *(zval **)_args[1], _data);
-}
+#if HAVE_INT128
+static jit_type_t jit_type_llong;
+static jit_type_t jit_type_ullong;
+#endif
-static void psi_jit_callback(jit_type_t _sig, void *_result, void **_args, void *_data)
-{
- psi_callback(_data, _result, jit_type_num_params(_sig), _args);
-}
+struct psi_jit_context {
+ jit_context_t jit;
+ jit_type_t signature;
+};
-static inline jit_type_t psi_jit_decl_arg_type(decl_arg *darg);
+struct psi_jit_callback_info {
+ struct psi_jit_impl_info *impl_info;
+ struct psi_let_exp *let_exp;
-static inline jit_abi_t psi_jit_abi(const char *convention) {
- return jit_abi_cdecl;
-}
-static inline jit_type_t psi_jit_token_type(token_t t) {
+ void *closure;
+};
+
+struct psi_jit_decl_info {
+ jit_type_t signature;
+ struct psi_jit_struct_info *rv_array;
+ void *params[1];
+};
+
+struct psi_jit_extvar_info {
+ struct {
+ jit_type_t signature;
+ void *closure;
+ } get;
+ struct {
+ jit_type_t signature;
+ jit_type_t params[1];
+ void *closure;
+ } set;
+};
+
+struct psi_jit_impl_info {
+ struct psi_context *context;
+ struct psi_call_frame *frame;
+
+ void *closure;
+};
+
+struct psi_jit_struct_info {
+ jit_type_t strct;
+ struct psi_plist *eles;
+};
+
+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;
return jit_type_long;
case PSI_T_UINT64:
return jit_type_ulong;
+#if HAVE_INT128
+ case PSI_T_INT128:
+ return jit_type_llong;
+ case PSI_T_UINT128:
+ return jit_type_ullong;
+#endif
case PSI_T_BOOL:
return jit_type_sys_bool;
- case PSI_T_INT:
case PSI_T_ENUM:
return jit_type_sys_int;
- case PSI_T_LONG:
- return jit_type_sys_long;
case PSI_T_FLOAT:
return jit_type_sys_float;
case PSI_T_DOUBLE:
return jit_type_void_ptr;
}
}
-static inline jit_type_t psi_jit_impl_type(token_t impl_type) {
+static inline jit_type_t psi_jit_impl_type(token_t impl_type)
+{
switch (impl_type) {
case PSI_T_BOOL:
return jit_type_sbyte;
return NULL;
}
-static void psi_jit_struct_type_dtor(void *type) {
- jit_type_t strct = type;
+static void psi_jit_type_free(jit_type_t *typ_ptr)
+{
+ jit_type_free(*typ_ptr);
+}
+
+static inline jit_abi_t psi_jit_abi(zend_string *convention)
+{
+ if (zend_string_equals_literal(convention, "stdcall")) {
+ return jit_abi_stdcall;
+ }
+ if (zend_string_equals_literal(convention, "fastcall")) {
+ return jit_abi_fastcall;
+ }
+ return jit_abi_cdecl;
+}
+
+static void psi_jit_handler(jit_type_t sig, void *result, void **args, void *data)
+{
+ struct psi_impl *impl = data;
+ struct psi_jit_impl_info *info = impl->info;
- jit_type_free(strct);
+ psi_context_call(info->context, *(zend_execute_data **)args[0], *(zval **) args[1], impl);
}
-static size_t psi_jit_struct_type_pad(jit_type_t *els, size_t padding) {
- size_t i;
+static void psi_jit_callback(jit_type_t sig, void *result, void **args, void *data)
+{
+ struct psi_jit_callback_info *cb_info = data;
+ struct psi_call_frame_callback cb_data;
- for (i = 0; i < padding; ++i) {
- *els++ = jit_type_copy(jit_type_sys_char);
- }
+ assert(cb_info->impl_info->frame);
+
+ cb_data.cb = cb_info->let_exp;
+ cb_data.argc = jit_type_num_params(sig);
+ cb_data.argv = args;
+ cb_data.rval = result;
- return padding;
+ psi_call_frame_do_callback(cb_info->impl_info->frame, &cb_data);
}
-static unsigned psi_jit_struct_type_elements(decl_struct *strct, jit_type_t **fields) {
- size_t i, argc = strct->args->count, nels = 0, offset = 0, maxalign;
- *fields = calloc(argc + 1, sizeof(*fields));
+static bool psi_jit_load(void)
+{
+#if HAVE_INT128
+ jit_type_t ll_fields[2], ull_fields[2];
- for (i = 0; i < strct->args->count; ++i) {
- decl_arg *darg = strct->args->args[i];
- jit_type_t type = jit_type_copy(psi_jit_decl_arg_type(darg));
- size_t padding, alignment;
+ ll_fields[0] = ll_fields[1] = jit_type_long;
+ jit_type_llong = jit_type_create_struct(ll_fields, 2, 1);
- ZEND_ASSERT(jit_type_get_size(type) == darg->layout->len);
+ ull_fields[0] = ull_fields[1] = jit_type_ulong;
+ jit_type_ullong = jit_type_create_struct(ull_fields, 2, 1);
+#endif
+ return true;
+}
- if ((alignment = jit_type_get_alignment(type)) > maxalign) {
- maxalign = alignment;
- }
+static void psi_jit_free(void)
+{
+#if HAVE_INT128
+ jit_type_free(jit_type_llong);
+ jit_type_free(jit_type_ullong);
+#endif
+}
- 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;
- }
- ZEND_ASSERT(offset == darg->layout->pos);
+static bool psi_jit_init(struct psi_context *C)
+{
+ struct psi_jit_context *context = pecalloc(1, sizeof(*context), 1);
+ jit_type_t params[] = {jit_type_void_ptr, jit_type_void_ptr};
- offset = (offset + darg->layout->len + alignment - 1) & ~(alignment - 1);
- (*fields)[nels++] = type;
+ context->jit = jit_context_create();
+ if (!context->jit) {
+ pefree(context, 1);
+ return false;
}
- /* apply struct alignment padding */
- offset = (offset + maxalign - 1) & ~(maxalign - 1);
-
- ZEND_ASSERT(offset <= strct->size);
- if (offset < strct->size) {
- nels += psi_jit_struct_type_pad(&(*fields)[nels], strct->size - offset);
+ context->signature = jit_type_create_signature(jit_abi_fastcall, jit_type_void,
+ params, 2, 1);
+ if (!context->signature) {
+ jit_context_destroy(context->jit);
+ pefree(context, 1);
+ return false;
}
- return nels;
+ C->context = context;
+ return true;
}
-static inline jit_type_t psi_jit_decl_type(decl_type *type) {
- decl_type *real = real_decl_type(type);
- switch (real->type) {
- case PSI_T_STRUCT:
- if (!real->real.strct->engine.type) {
- unsigned count;
- jit_type_t strct, *fields = NULL;
+static void psi_jit_dtor(struct psi_context *C)
+{
+ if (C->context) {
+ struct psi_jit_context *context = C->context;
- count = psi_jit_struct_type_elements(real->real.strct, &fields);
- strct = jit_type_create_struct(fields, count, 0);
+ jit_type_free(context->signature);
+ jit_context_destroy(context->jit);
- real->real.strct->engine.type = strct;
- real->real.strct->engine.dtor = psi_jit_struct_type_dtor;
- }
+ pefree(C->context, 1);
+ C->context = NULL;
+ }
+}
- return real->real.strct->engine.type;
- case PSI_T_UNION:
- return psi_jit_decl_arg_type(real->real.unn->args->args[0]);
+static bool psi_jit_composite_init(struct psi_context *C,
+ struct psi_decl_arg *darg)
+{
+ struct psi_jit_struct_info *info;
- default:
- return psi_jit_token_type(real->type);
+ if (darg->engine.type) {
+ return true;
}
-}
-static inline jit_type_t psi_jit_decl_arg_type(decl_arg *darg) {
- if (darg->var->pointer_level) {
- return jit_type_void_ptr;
- } else {
- return psi_jit_decl_type(darg->type);
- }
-}
-struct psi_jit_context {
- jit_context_t jit;
- jit_type_t signature;
- struct {
- struct psi_jit_data **list;
- size_t count;
- } data;
-};
+ info = pecalloc(1, sizeof(*info), 1);
+ info->eles = psi_plist_init((psi_plist_dtor) psi_jit_type_free);
+ psi_context_composite_type_elements(C, darg, &info->eles);
+ info->strct = jit_type_create_struct((jit_type_t *)
+ psi_plist_eles(info->eles),
+ psi_plist_count(info->eles), 0);
-struct psi_jit_call {
- void *closure;
- jit_type_t signature;
- void *params[1]; /* [type1, type2, NULL, arg1, arg2] ... */
-};
+ darg->engine.info = info;
+ darg->engine.type = info->strct;
-struct psi_jit_data {
- struct psi_jit_context *context;
- impl *impl;
- zend_internal_arg_info *arginfo;
-};
+ return true;
+}
-static inline struct psi_jit_call *psi_jit_call_alloc(struct psi_context *C, decl *decl) {
- size_t i, c = decl->args ? decl->args->count : 0;
- struct psi_jit_call *call = calloc(1, sizeof(*call) + 2 * c * sizeof(void *));
+static void psi_jit_composite_dtor(struct psi_context *C,
+ struct psi_decl_arg *darg)
+{
+ struct psi_jit_struct_info *info = darg->engine.info;
- for (i = 0; i < c; ++i) {
- call->params[i] = psi_jit_decl_arg_type(decl->args->args[i]);
- }
- call->params[c] = NULL;
+ if (info) {
+ darg->engine.info = NULL;
+ darg->engine.type = NULL;
- decl->call.info = call;
- decl->call.rval = &decl->func->ptr;
- decl->call.argc = c;
- decl->call.args = (void **) &call->params[c+1];
+ jit_type_free(info->strct);
+ /* just free */
+ pefree(info->eles, 1);
+ pefree(info, 1);
+ }
+}
- 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);
- ZEND_ASSERT(call->signature);
+static void psi_jit_extvar_get(jit_type_t sig, void *result, void **args, void *data)
+{
+ struct psi_decl_extvar *evar = data;
- return call;
+ psi_decl_extvar_get(evar, result);
}
-static inline void *psi_jit_call_init_closure(struct psi_context *C, struct psi_jit_call *call, impl *impl) {
- struct psi_jit_context *context = C->context;
- return call->closure = jit_closure_create(context->jit, context->signature,
- &psi_jit_handler, impl);
-}
+static void psi_jit_extvar_set(jit_type_t sig, void *result, void **args, void *data)
+{
+ struct psi_decl_extvar *evar = data;
-static inline void *psi_jit_call_init_callback_closure(struct psi_context *C, struct psi_jit_call *call, let_callback *cb) {
- struct psi_jit_context *context = C->context;
- return call->closure = jit_closure_create(context->jit, call->signature,
- &psi_jit_callback, cb);
+ psi_decl_extvar_set(evar, args[0]);
}
-static inline void psi_jit_call_free(struct psi_jit_call *call) {
- jit_type_free(call->signature);
- free(call);
-}
+static bool psi_jit_decl_init(struct psi_context *, struct psi_decl *);
-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
- };
+static bool psi_jit_extvar_init(struct psi_context *C,
+ struct psi_decl_extvar *evar)
+{
+ struct psi_jit_context *ctx = C->context;
+ struct psi_jit_extvar_info *info = pecalloc(1, sizeof(*info), 1);
+
+ evar->info = info;
+
+ psi_jit_decl_init(C, evar->getter);
+ psi_jit_decl_init(C, evar->setter);
+
+ jit_context_build_start(ctx->jit);
- if (!L) {
- L = malloc(sizeof(*L));
+ info->get.signature = jit_type_create_signature(jit_abi_cdecl,
+ psi_context_decl_arg_call_type(C, evar->getter->func), NULL, 0, 1);
+ if (!info->get.signature) {
+ goto failure;
+ }
+ info->get.closure = jit_closure_create(ctx->jit, info->get.signature,
+ psi_jit_extvar_get, evar);
+ if (!info->get.closure) {
+ goto failure;
}
- memset(L, 0, sizeof(*L));
- L->jit = jit_context_create();
- L->signature = jit_type_create_signature(jit_abi_cdecl, jit_type_void,
- params, 2, 1);
+ info->set.params[0] = psi_context_decl_arg_call_type(C, evar->arg);
+ info->set.signature = jit_type_create_signature(jit_abi_cdecl,
+ psi_context_decl_arg_call_type(C, evar->setter->func),
+ info->set.params, 1, 1);
+ if (!info->set.signature) {
+ goto failure;
+ }
+ info->set.closure = jit_closure_create(ctx->jit, info->set.signature,
+ psi_jit_extvar_set, evar);
+ if (!info->set.closure) {
+ goto failure;
+ }
- return L;
-}
+ evar->getter->sym = info->get.closure;
+ evar->setter->sym = info->set.closure;
-static inline void psi_jit_context_dtor(struct psi_jit_context *L) {
- jit_type_free(L->signature);
- jit_context_destroy(L->jit);
+ jit_context_build_end(ctx->jit);
+ return true;
+failure: ;
+ jit_context_build_end(ctx->jit);
+ return false;
}
-static inline void psi_jit_context_free(struct psi_jit_context **L) {
- if (*L) {
- psi_jit_context_dtor(*L);
- free(*L);
- *L = NULL;
+static void psi_jit_extvar_dtor(struct psi_context *C,
+ struct psi_decl_extvar *evar) {
+ if (evar->info) {
+ pefree(evar->info, 1);
+ evar->info = NULL;
}
}
-static void psi_jit_init(struct psi_context *C)
+static bool psi_jit_decl_init(struct psi_context *C, struct psi_decl *decl)
{
- C->context = psi_jit_context_init(NULL);
-}
+ if (!decl->info) {
+ struct psi_jit_context *ctx = C->context;
+ size_t i, c = psi_plist_count(decl->args);
+ struct psi_decl_arg *arg;
+ struct psi_jit_decl_info *info = pecalloc(1,
+ sizeof(*info) + 2 * c * sizeof(void *), 1);
-static inline void psi_jit_destroy_callbacks(struct psi_context *C, let_val *let_val) {
- let_callback *cb;
- let_func *fn = NULL;
+ decl->info = info;
- switch (let_val->kind) {
- case PSI_LET_CALLBACK:
- cb = let_val->data.callback;
+ jit_context_build_start(ctx->jit);
- if (cb->decl && cb->decl->call.info) {
- psi_jit_call_free(cb->decl->call.info);
- }
- fn = cb->func;
- /* no break */
- case PSI_LET_FUNC:
- if (!fn) {
- fn = let_val->data.func;
+ for (i = 0; psi_plist_get(decl->args, i, &arg); ++i) {
+ info->params[i] = psi_context_decl_arg_call_type(C, arg);
}
+ info->params[c] = NULL;
- if (fn->inner) {
- size_t i;
+ info->signature = jit_type_create_signature(
+ psi_jit_abi(decl->abi->convention),
+ psi_context_decl_arg_full_type(C, decl->func),
+ (jit_type_t *) info->params, c, 1);
- for (i = 0; i < fn->inner->count; ++i) {
- psi_jit_destroy_callbacks(C, fn->inner->vals[i]);
- }
+ jit_context_build_end(ctx->jit);
+
+ if (!info->signature) {
+ pefree(info, 1);
+ decl->info = NULL;
+ return false;
}
- break;
- default:
- break;
}
+
+ return true;
}
-static void psi_jit_dtor(struct psi_context *C)
+static void psi_jit_decl_dtor(struct psi_context *C, struct psi_decl *decl) {
+ if (decl->info) {
+ struct psi_jit_decl_info *info = decl->info;
+
+ jit_type_free(info->signature);
+ pefree(info, 1);
+ decl->info = NULL;
+ }
+}
+
+static bool psi_jit_impl_init(struct psi_context *C,
+ struct psi_impl *impl, zif_handler *zh)
{
- if (C->decls) {
- size_t i;
+ struct psi_jit_context *context = C->context;
+ struct psi_jit_impl_info *info = calloc(1, sizeof(*info));
- for (i = 0; i < C->decls->count; ++i) {
- decl *decl = C->decls->list[i];
+ impl->info = info;
+ info->context = C;
- if (decl->call.info) {
- psi_jit_call_free(decl->call.info);
- }
- }
+ info->closure = jit_closure_create(context->jit, context->signature,
+ &psi_jit_handler, impl);
+
+ if (!info->closure) {
+ goto failure;
}
- if (C->impls) {
- size_t i, j;
- for (i = 0; i < C->impls->count; ++i) {
- impl *impl = C->impls->list[i];
+ *zh = info->closure;
+ return true;
- for (j = 0; j < impl->stmts->let.count; ++j) {
- psi_jit_destroy_callbacks(C, impl->stmts->let.list[j]->val);
- }
- }
- }
- psi_jit_context_free((void *) &C->context);
+failure: ;
+ impl->info = NULL;
+ pefree(info, 1);
+ return false;
}
-static inline void psi_jit_compile_callbacks(struct psi_context *C, let_val *let_val) {
- struct psi_jit_call *call;
- let_callback *cb;
- let_func *fn = NULL;
-
- switch (let_val->kind) {
- case PSI_LET_CALLBACK:
- cb = let_val->data.callback;
- if ((call = psi_jit_call_alloc(C, cb->decl))) {
- if (!psi_jit_call_init_callback_closure(C, call, cb)) {
- psi_jit_call_free(call);
- break;
- }
-
- cb->decl->call.sym = call->closure;
- }
- fn = cb->func;
- /* no break */
- case PSI_LET_FUNC:
- if (!fn) {
- fn = let_val->data.func;
- }
- if (fn->inner) {
- size_t i;
- for (i = 0; i < fn->inner->count; ++i) {
- psi_jit_compile_callbacks(C, fn->inner->vals[i]);
- }
- }
- break;
- default:
- break;
+static void psi_jit_impl_dtor(struct psi_context *C, struct psi_impl *impl) {
+ struct psi_jit_impl_info *info = impl->info;
+
+ if (info) {
+ /* The memory for the closure will be reclaimed
+ * when the context is destroyed.
+ */
+ pefree(info, 1);
+ impl->info = NULL;
}
}
-static zend_function_entry *psi_jit_compile(struct psi_context *C)
+static bool psi_jit_cb_init(struct psi_context *C,
+ struct psi_let_exp *exp, struct psi_impl *impl)
{
- size_t c, i, j = 0;
- zend_function_entry *zfe;
- struct psi_jit_context *ctx = C->context;
+ struct psi_jit_context *context = C->context;
+ struct psi_jit_callback_info *cb_info;
+ struct psi_jit_decl_info *decl_info;
+
+ assert(exp->kind == PSI_LET_CALLBACK);
- if (!C->impls) {
- return NULL;
+ if (!psi_jit_decl_init(C, exp->data.callback->decl)) {
+ return false;
}
- zfe = calloc(C->impls->count + 1, sizeof(*zfe));
- jit_context_build_start(ctx->jit);
+ cb_info = pecalloc(1, sizeof(*cb_info), 1);
+ cb_info->impl_info = impl->info;
+ cb_info->let_exp = exp;
- for (i = 0; i < C->impls->count; ++i) {
- zend_function_entry *zf = &zfe[j];
- struct psi_jit_call *call;
- impl *impl = C->impls->list[i];
+ decl_info = exp->data.callback->decl->info;
+ cb_info->closure = jit_closure_create(context->jit, decl_info->signature,
+ &psi_jit_callback, cb_info);
- if (!impl->decl) {
- continue;
- }
+ if (!cb_info->closure) {
+ free(cb_info);
+ return false;
+ }
- if ((call = psi_jit_call_alloc(C, impl->decl))) {
- if (!psi_jit_call_init_closure(C, call, impl)) {
- psi_jit_call_free(call);
- continue;
- }
- }
+ assert(!exp->data.callback->decl->sym);
+ exp->data.callback->info = cb_info;
+ exp->data.callback->decl->sym = cb_info->closure;
- zf->fname = impl->func->name + (impl->func->name[0] == '\\');
- zf->num_args = impl->func->args->count;
- zf->handler = call->closure;
- zf->arg_info = psi_internal_arginfo(impl);
- ++j;
+ return true;
+}
- for (c = 0; c < impl->stmts->let.count; ++c) {
- psi_jit_compile_callbacks(C, impl->stmts->let.list[c]->val);
- }
+static void psi_jit_cb_dtor(struct psi_context *C,
+ struct psi_let_exp *let_exp, struct psi_impl *impl)
+{
+ assert(let_exp->kind == PSI_LET_CALLBACK);
+
+ psi_jit_decl_dtor(C, let_exp->data.callback->decl);
+
+ if (let_exp->data.callback->info) {
+ struct psi_jit_callback_info *info = let_exp->data.callback->info;
+
+ /* The memory for the closure will be reclaimed
+ * when the context is destroyed.
+ */
+ pefree(info, 1);
+ let_exp->data.callback->info = NULL;
+ }
+}
+
+static void psi_jit_call(struct psi_call_frame *frame) {
+ struct psi_decl *decl = psi_call_frame_get_decl(frame);
+ struct psi_impl *impl = psi_call_frame_get_impl(frame);
+ struct psi_jit_decl_info *decl_info = decl->info;
+ struct psi_jit_impl_info *impl_info;
+ struct psi_call_frame *prev;
+
+ if (impl) {
+ impl_info = impl->info;
+ prev = impl_info->frame;
+ impl_info->frame = frame;
+ }
+ jit_apply(decl_info->signature, decl->sym,
+ psi_call_frame_get_arg_pointers(frame), psi_plist_count(decl->args),
+ psi_call_frame_get_rpointer(frame));
+ if (impl) {
+ impl_info->frame = prev;
}
+}
- for (i = 0; i < C->decls->count; ++i) {
- decl *decl = C->decls->list[i];
+static void psi_jit_call_va(struct psi_call_frame *frame) {
+ jit_type_t signature;
+ struct psi_call_frame *prev;
+ struct psi_decl *decl = psi_call_frame_get_decl(frame);
+ struct psi_impl *impl = psi_call_frame_get_impl(frame);
+ struct psi_jit_decl_info *decl_info = decl->info;
+ struct psi_jit_impl_info *impl_info;
+ size_t i, va_count, argc;
+ jit_type_t *param_types;
+
+ argc = psi_plist_count(decl->args);
+ va_count = psi_call_frame_num_var_args(frame);
+ param_types = ecalloc(argc + va_count + 1, sizeof(jit_type_t));
+ memcpy(param_types, decl_info->params, argc * sizeof(jit_type_t));
+ for (i = 0; i < va_count; ++i) {
+ struct psi_call_frame_argument *frame_arg;
+
+ frame_arg = psi_call_frame_get_var_argument(frame, i);
+ param_types[argc + i] = psi_jit_impl_type(frame_arg->va_type);
+ }
- if (decl->call.info) {
- continue;
- }
+ signature = jit_type_create_signature(jit_abi_vararg,
+ jit_type_get_return(decl_info->signature),
+ param_types, argc + va_count, 1);
+ assert(signature);
- psi_jit_call_alloc(C, decl);
+ if (impl) {
+ impl_info = impl->info;
+ prev = impl_info->frame;
+ impl_info->frame = frame;
+ }
+ jit_apply(signature, decl->sym,
+ psi_call_frame_get_arg_pointers(frame), argc,
+ psi_call_frame_get_rpointer(frame));
+ if (impl) {
+ impl_info->frame = prev;
}
- jit_context_build_end(ctx->jit);
+ jit_type_free(signature);
+
+ efree(param_types);
+}
- return zfe;
+static void *psi_jit_typeof_impl(struct psi_context *C, token_t impl_type)
+{
+ return psi_jit_impl_type(impl_type);
}
-static void psi_jit_call(struct psi_context *C, decl_callinfo *decl_call, impl_vararg *va) {
- struct psi_jit_call *call = decl_call->info;
+static void *psi_jit_typeof_decl(struct psi_context *C, token_t decl_type)
+{
+ return psi_jit_token_type(decl_type);
+}
- if (va) {
- jit_type_t signature;
- size_t i, nfixedargs = decl_call->argc, ntotalargs = nfixedargs + va->args->count;
- void **params = calloc(2 * ntotalargs + 2, sizeof(void *));
+static void *psi_jit_copyof_type(struct psi_context *C, void *orig_type)
+{
+ return jit_type_copy(orig_type);
+}
- for (i = 0; i < nfixedargs; ++i) {
- params[i] = call->params[i];
- params[i + ntotalargs + 1] = call->params[i + nfixedargs + 1];
- }
- for (i = 0; i < va->args->count; ++i) {
- params[nfixedargs + i] = psi_jit_impl_type(va->types[i]);
- params[nfixedargs + i + ntotalargs + 1] = &va->values[i];
- }
+static void psi_jit_layoutof_type(struct psi_context *C, void *orig_type,
+ struct psi_layout *l)
+{
+ l->pos = jit_type_get_alignment(orig_type);
+ l->len = jit_type_get_size(orig_type);
- signature = jit_type_create_signature(
- jit_type_get_abi(call->signature),
- jit_type_get_return(call->signature),
- (jit_type_t *) params, ntotalargs, 1);
- ZEND_ASSERT(signature);
-
- jit_apply(signature, decl_call->sym, ¶ms[ntotalargs + 1],
- nfixedargs, *decl_call->rval);
- jit_type_free(signature);
- free(params);
- } else {
- jit_apply(call->signature, decl_call->sym, decl_call->args,
- decl_call->argc, *decl_call->rval);
- }
+ assert(l->pos);
+ assert(l->len);
}
static struct psi_context_ops ops = {
+ "libjit",
+ psi_jit_load,
+ psi_jit_free,
psi_jit_init,
psi_jit_dtor,
- psi_jit_compile,
+ psi_jit_composite_init,
+ psi_jit_composite_dtor,
+ psi_jit_extvar_init,
+ psi_jit_extvar_dtor,
+ psi_jit_decl_init,
+ psi_jit_decl_dtor,
+ psi_jit_impl_init,
+ psi_jit_impl_dtor,
+ psi_jit_cb_init,
+ psi_jit_cb_dtor,
psi_jit_call,
+ psi_jit_call_va,
+ psi_jit_typeof_impl,
+ psi_jit_typeof_decl,
+ psi_jit_copyof_type,
+ psi_jit_layoutof_type,
};
struct psi_context_ops *psi_libjit_ops(void)