ifdef HAVE_INT128
[m6w6/ext-psi] / src / libjit.c
index 8a766f33ca2a8d59c801e9cd35f28c3ebd8a7923..eaf3b164e4bff18c762d799df84919416aabbb0d 100644 (file)
@@ -1,23 +1,50 @@
-#ifdef HAVE_CONFIG_H
-# include "config.h"
-#endif
-
+/*******************************************************************************
+ 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"
 
 #ifdef HAVE_LIBJIT
 
-#include "php_psi.h"
-#include "libjit.h"
-
 #include <jit/jit.h>
 
-static void psi_jit_handler(jit_type_t _sig, void *result, void **_args, void *_data);
+#if HAVE_INT128
+static jit_type_t jit_type_llong;
+static jit_type_t jit_type_ullong;
+#endif
 
-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) {
+static inline jit_type_t psi_jit_decl_arg_type(struct psi_decl_arg *darg);
+
+static inline jit_type_t psi_jit_token_type(token_t t)
+{
        switch (t) {
+       default:
+               assert(0);
+               /* no break */
        case PSI_T_VOID:
                return jit_type_void;
        case PSI_T_INT8:
@@ -36,16 +63,31 @@ static inline jit_type_t psi_jit_token_type(token_t t) {
                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_ENUM:
+               return jit_type_sys_int;
        case PSI_T_FLOAT:
                return jit_type_sys_float;
        case PSI_T_DOUBLE:
                return jit_type_sys_double;
-       EMPTY_SWITCH_DEFAULT_CASE();
+#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_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;
@@ -56,13 +98,132 @@ static inline jit_type_t psi_jit_impl_type(token_t impl_type) {
        case PSI_T_FLOAT:
        case PSI_T_DOUBLE:
                return jit_type_sys_double;
-       EMPTY_SWITCH_DEFAULT_CASE();
+       EMPTY_SWITCH_DEFAULT_CASE()
+               ;
        }
+       return NULL;
 }
-static inline jit_type_t psi_jit_decl_type(decl_type *type) {
-       return psi_jit_token_type(real_decl_type(type)->type);
+
+struct psi_jit_struct_type {
+       jit_type_t strct;
+       jit_type_t *fields;
+};
+
+static void psi_jit_struct_type_dtor(void *ptr)
+{
+       struct psi_jit_struct_type *type = ptr;
+       unsigned i, n = jit_type_num_fields(type->strct);
+
+       for (i = 0; i < n; ++i) {
+               jit_type_free(jit_type_get_field(type->strct, i));
+       }
+       jit_type_free(type->strct);
+       free(type->fields);
+       free(type);
+}
+
+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);
+       }
+
+       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 = 0, last_arg_pos = -1;
+       struct psi_decl_arg *darg;
+       jit_type_t *tmp;
+
+       *fields = calloc(argc + 1, sizeof(*fields));
+
+       while (psi_plist_get(strct->args, i++, &darg)) {
+               jit_type_t type;
+               size_t padding, alignment;
+
+               if (darg->layout->pos == last_arg_pos) {
+                       /* skip bit fields */
+                       continue;
+               }
+               last_arg_pos = darg->layout->pos;
+
+               type = jit_type_copy(psi_jit_decl_arg_type(darg));
+
+               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;
+                               tmp = realloc(*fields, (argc + 1) * sizeof(*fields));
+                               if (tmp) {
+                                       *fields = tmp;
+                               } else {
+                                       free(*fields);
+                                       return 0;
+                               }
+                       }
+                       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;
+       }
+
+       /* 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 inline jit_type_t psi_jit_decl_arg_type(decl_arg *darg) {
+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;
+                       struct psi_jit_struct_type *type = calloc(1, sizeof(*type));
+
+                       count = psi_jit_struct_type_elements(real->real.strct, &type->fields);
+                       type->strct = jit_type_create_struct(type->fields, count, 0);
+
+                       real->real.strct->engine.type = type;
+                       real->real.strct->engine.dtor = psi_jit_struct_type_dtor;
+               }
+
+               return ((struct psi_jit_struct_type *) real->real.strct->engine.type)->strct;
+
+       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:
+               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 {
@@ -70,64 +231,243 @@ static inline jit_type_t psi_jit_decl_arg_type(decl_arg *darg) {
        }
 }
 
-typedef struct PSI_LibjitContext {
+static inline jit_abi_t psi_jit_abi(const char *convention)
+{
+       if (!strcasecmp(convention, "stdcall")) {
+               return jit_abi_stdcall;
+       }
+       if (!strcasecmp(convention, "fastcall")) {
+               return jit_abi_fastcall;
+       }
+       return jit_abi_cdecl;
+}
+
+struct psi_jit_context {
        jit_context_t jit;
        jit_type_t signature;
-       struct {
-               struct PSI_LibjitData **list;
-               size_t count;
-       } data;
-} PSI_LibjitContext;
+};
+
+struct psi_jit_impl_info {
+       struct psi_context *context;
+       struct psi_call_frame *frame;
+
+       void *closure;
+};
+
+struct psi_jit_callback_info {
+       struct psi_jit_impl_info *impl_info;
+       struct psi_let_exp *let_exp;
 
-typedef struct PSI_LibjitCall {
        void *closure;
+};
+
+struct psi_jit_decl_info {
        jit_type_t signature;
-       void *params[1]; /* [type1, type2, NULL, arg1, arg2] ... */
-} PSI_LibjitCall;
-
-typedef struct PSI_LibjitData {
-       PSI_LibjitContext *context;
-       impl *impl;
-       zend_internal_arg_info *arginfo;
-} PSI_LibjitData;
-
-static inline PSI_LibjitCall *PSI_LibjitCallAlloc(PSI_Context *C, decl *decl) {
-       size_t i, c = decl->args ? decl->args->count : 0;
-       PSI_LibjitCall *call = calloc(1, sizeof(*call) + 2 * c * sizeof(void *));
-
-       for (i = 0; i < c; ++i) {
-               call->params[i] = psi_jit_decl_arg_type(decl->args->args[i]);
-       }
-       call->params[c] = NULL;
-
-       decl->call.info = call;
-       decl->call.rval = decl->func->ptr;
-       decl->call.argc = c;
-       decl->call.args = (void **) &call->params[c+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);
-       return call;
+       void *params[1];
+};
+
+static inline struct psi_jit_decl_info *psi_jit_decl_init(struct psi_decl *decl) {
+       if (!decl->info) {
+               size_t i, c = psi_plist_count(decl->args);
+               struct psi_decl_arg *arg;
+               struct psi_jit_decl_info *info = calloc(1, sizeof(*info) + 2 * c * sizeof(void *));
+
+               for (i = 0; psi_plist_get(decl->args, i, &arg); ++i) {
+                       info->params[i] = psi_jit_decl_arg_type(arg);
+               }
+               info->params[c] = NULL;
+
+               info->signature = jit_type_create_signature(
+                               psi_jit_abi(decl->abi->convention),
+                               psi_jit_decl_arg_type(decl->func),
+                               (jit_type_t *) info->params,
+                               c, 1);
+
+               if (!info->signature) {
+                       free(info);
+               } else {
+                       decl->info = info;
+               }
+       }
+
+       return decl->info;
+}
+
+static inline void psi_jit_decl_dtor(struct psi_decl *decl) {
+       if (decl->info) {
+               struct psi_jit_decl_info *info = decl->info;
+
+               jit_type_free(info->signature);
+               free(info);
+               decl->info = NULL;
+       }
 }
 
-static inline void PSI_LibjitCallInitClosure(PSI_Context *C, PSI_LibjitCall *call, impl *impl) {
-       PSI_LibjitContext *context = C->context;
-       call->closure = jit_closure_create(context->jit, context->signature,
+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;
+
+       psi_context_call(info->context, *(zend_execute_data **)args[0], *(zval **) args[1], impl);
+}
+
+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;
+
+       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;
+
+       psi_call_frame_do_callback(cb_info->impl_info->frame, &cb_data);
+}
+
+static inline void psi_jit_callback_init(struct psi_jit_impl_info *impl_info,
+               struct psi_let_exp *let_exp)
+{
+       struct psi_jit_context *context = impl_info->context->context;
+       struct psi_jit_callback_info *cb_info;
+       struct psi_jit_decl_info *decl_info;
+       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->info) {
+                       decl_info = cb->decl->info;
+               } else {
+                       decl_info = psi_jit_decl_init(cb->decl);
+               }
+
+               cb_info = calloc(1, sizeof(*cb_info));
+               cb_info->impl_info = impl_info;
+               cb_info->let_exp = let_exp;
+               cb_info->closure = jit_closure_create(context->jit, decl_info->signature,
+                               &psi_jit_callback, cb_info);
+
+               if (!cb_info->closure) {
+                       free(cb_info);
+                       break;
+               }
+               cb->info = cb_info;
+
+               assert(!cb->decl->sym);
+               cb->decl->sym = cb_info->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_callback_init(impl_info, inner_let);
+                       }
+               }
+               break;
+       default:
+               break;
+       }
+}
+
+static inline void psi_jit_callback_dtor(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;
+
+               psi_jit_decl_dtor(cb->decl);
+
+               if (cb->info) {
+                       struct psi_jit_callback_info *info = cb->info;
+
+                       if (info->closure) {
+                               /* The memory for the closure will be reclaimed when the context is destroyed.
+                               free(info->closure); */
+                       }
+                       free(info);
+                       cb->info = NULL;
+               }
+               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 *cb;
+
+                       while (psi_plist_get(fn->inner, i++, &cb)) {
+                               psi_jit_callback_dtor(cb);
+                       }
+               }
+               break;
+       default:
+               break;
+       }
+}
+
+static inline struct psi_jit_impl_info *psi_jit_impl_init(struct psi_impl * impl,
+               struct psi_context *C)
+{
+       struct psi_jit_context *context = C->context;
+       struct psi_jit_impl_info *info = calloc(1, sizeof(*info));
+       struct psi_let_stmt *let;
+       size_t l = 0;
+
+       info->context = C;
+       info->closure = jit_closure_create(context->jit, context->signature,
                        &psi_jit_handler, impl);
+
+       if (!info->closure) {
+               free(info);
+               return NULL;
+       }
+
+       while (psi_plist_get(impl->stmts.let, l++, &let)) {
+               psi_jit_callback_init(info, let->exp);
+       }
+
+       return impl->info = info;
 }
 
-static inline void PSI_LibjitCallFree(PSI_LibjitCall *call) {
-       jit_type_free(call->signature);
-       free(call);
+
+static inline void psi_jit_impl_dtor(struct psi_impl *impl) {
+       struct psi_jit_impl_info *info = impl->info;
+       struct psi_let_stmt *let;
+       size_t j = 0;
+
+       while (psi_plist_get(impl->stmts.let, j++, &let)) {
+               psi_jit_callback_dtor(let->exp);
+       }
+
+       if (info) {
+               if (info->closure) {
+                       /* The memory for the closure will be reclaimed when the context is destroyed.
+                       free(info->closure); */
+               }
+               free(info);
+               impl->info = NULL;
+       }
 }
 
-static inline PSI_LibjitContext *PSI_LibjitContextInit(PSI_LibjitContext *L) {
-       jit_type_t params[] = {
-               jit_type_void_ptr,
-               jit_type_void_ptr
-       };
+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 (!L) {
                L = malloc(sizeof(*L));
@@ -141,83 +481,88 @@ static inline PSI_LibjitContext *PSI_LibjitContextInit(PSI_LibjitContext *L) {
        return L;
 }
 
-static inline void PSI_LibjitContextDtor(PSI_LibjitContext *L) {
+static inline void psi_jit_context_dtor(struct psi_jit_context *L)
+{
        jit_type_free(L->signature);
        jit_context_destroy(L->jit);
 }
 
-static inline void PSI_LibjitContextFree(PSI_LibjitContext **L) {
+static inline void psi_jit_context_free(struct psi_jit_context **L)
+{
        if (*L) {
-               PSI_LibjitContextDtor(*L);
+               psi_jit_context_dtor(*L);
                free(*L);
                *L = NULL;
        }
 }
 
-static void psi_jit_handler(jit_type_t _sig, void *result, void **_args, void *_data)
+static void psi_jit_init(struct psi_context *C)
 {
-       psi_call(*(zend_execute_data **)_args[0], *(zval **)_args[1], _data);
+       C->context = psi_jit_context_init(NULL);
 }
 
-static void psi_jit_init(PSI_Context *C)
-{
-       C->context = PSI_LibjitContextInit(NULL);
-}
-
-static void psi_jit_dtor(PSI_Context *C)
+static void psi_jit_dtor(struct psi_context *C)
 {
        if (C->decls) {
-               size_t i;
+               size_t i = 0;
+               struct psi_decl *decl;
 
-               for (i = 0; i < C->decls->count; ++i) {
-                       decl *decl = C->decls->list[i];
+               while (psi_plist_get(C->decls, i++, &decl)) {
+                       psi_jit_decl_dtor(decl);
+               }
+       }
+       if (C->impls) {
+               size_t i = 0;
+               struct psi_impl *impl;
 
-                       PSI_LibjitCallFree(decl->call.info);
+               while (psi_plist_get(C->impls, i++, &impl)) {
+                       psi_jit_impl_dtor(impl);
                }
        }
-       PSI_LibjitContextFree((void *) &C->context);
+       psi_jit_context_free((void *) &C->context);
 }
 
-static zend_function_entry *psi_jit_compile(PSI_Context *C)
+static zend_function_entry *psi_jit_compile(struct psi_context *C)
 {
-       size_t i, j = 0;
+       size_t i = 0, d = 0, nf = 0;
+       struct psi_impl *impl;
+       struct psi_decl *decl;
        zend_function_entry *zfe;
-       PSI_LibjitContext *ctx = C->context;
+       struct psi_jit_context *ctx = C->context;
 
        if (!C->impls) {
                return NULL;
        }
 
-       zfe = calloc(C->impls->count + 1, sizeof(*zfe));
+       zfe = calloc(psi_plist_count(C->impls) + 1, sizeof(*zfe));
        jit_context_build_start(ctx->jit);
 
-       for (i = 0; i < C->impls->count; ++i) {
-               zend_function_entry *zf = &zfe[j];
-               PSI_LibjitCall *call;
-               impl *impl = C->impls->list[i];
+       while (psi_plist_get(C->impls, i++, &impl)) {
+               zend_function_entry *zf = &zfe[nf];
 
                if (!impl->decl) {
                        continue;
                }
-
-               call = PSI_LibjitCallAlloc(C, impl->decl);
-               PSI_LibjitCallInitClosure(C, call, impl);
+               if (!psi_jit_decl_init(impl->decl)) {
+                       continue;
+               }
+               if (!psi_jit_impl_init(impl, C)) {
+                       continue;
+               }
 
                zf->fname = impl->func->name + (impl->func->name[0] == '\\');
-               zf->num_args = impl->func->args->count;
-               zf->handler = call->closure;
+               zf->handler = ((struct psi_jit_impl_info *) impl->info)->closure;
+               zf->num_args = psi_plist_count(impl->func->args);
                zf->arg_info = psi_internal_arginfo(impl);
-               ++j;
+               ++nf;
        }
 
-       for (i = 0; i < C->decls->count; ++i) {
-               decl *decl = C->decls->list[i];
-
-               if (decl->impl) {
+       while (psi_plist_get(C->decls, d++, &decl)) {
+               if (decl->info) {
                        continue;
                }
 
-               PSI_LibjitCallAlloc(C, decl);
+               psi_jit_decl_init(decl);
        }
 
        jit_context_build_end(ctx->jit);
@@ -225,47 +570,123 @@ static zend_function_entry *psi_jit_compile(PSI_Context *C)
        return zfe;
 }
 
-static void psi_jit_call(PSI_Context *C, decl_callinfo *decl_call, impl_vararg *va) {
-       PSI_LibjitCall *call = decl_call->info;
+static inline void psi_jit_call_ex(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;
+       }
+}
+
+static inline 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);
+       }
+
+       signature = jit_type_create_signature(jit_abi_vararg,
+                       jit_type_get_return(decl_info->signature),
+                       param_types, argc + va_count,
+                       1);
+       assert(signature);
 
-       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 *));
+       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;
+       }
 
-               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];
-               }
+       jit_type_free(signature);
 
-               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);
+       efree(param_types);
+}
 
-               jit_apply(signature, decl_call->sym, &params[ntotalargs + 1],
-                                               nfixedargs, decl_call->rval);
-               jit_type_free(signature);
-               free(params);
+static void psi_jit_call(struct psi_call_frame *frame) {
+       if (psi_call_frame_num_var_args(frame)) {
+               psi_jit_call_va(frame);
        } else {
-               jit_apply(call->signature, decl_call->sym, decl_call->args,
-                               decl_call->argc, decl_call->rval);
+               psi_jit_call_ex(frame);
+       }
+}
+
+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 ZEND_RESULT_CODE psi_jit_load(void)
+{
+#if HAVE_INT128
+       jit_type_t ll_fields[2], ull_fields[2];
+
+       ll_fields[0] = ll_fields[1] = jit_type_long;
+       jit_type_llong = jit_type_create_struct(ll_fields, 2, 1);
+
+       ull_fields[0] = ull_fields[1] = jit_type_ulong;
+       jit_type_ullong = jit_type_create_struct(ull_fields, 2, 1);
+#endif
+       return SUCCESS;
+}
+
+static void psi_jit_free(void)
+{
+#if HAVE_INT128
+       jit_type_free(jit_type_llong);
+       jit_type_free(jit_type_ullong);
+#endif
 }
 
-static PSI_ContextOps ops = {
+static struct psi_context_ops ops = {
+       psi_jit_load,
+       psi_jit_free,
        psi_jit_init,
        psi_jit_dtor,
        psi_jit_compile,
        psi_jit_call,
+       psi_jit_query
 };
 
-PSI_ContextOps *PSI_Libjit(void)
+struct psi_context_ops *psi_libjit_ops(void)
 {
        return &ops;
 }