CPP builtins
[m6w6/ext-psi] / src / libjit.c
index 5c4a7b9313c1f4cebcebc15234264ce666561c87..942696670724d907957850bb2d52be46e82efba1 100644 (file)
+/*******************************************************************************
+ 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"
-#include "libjit.h"
-#include "parser_proc.h"
 
-static void init(PSI_Context *C)
-{
-       C->opaque = jit_context_create();
-}
+#ifdef HAVE_LIBJIT
 
-static void dtor(PSI_Context *C)
-{
-       jit_context_destroy(C->opaque);
-}
+#include "libjit.h"
+#include <jit/jit.h>
+
+#if HAVE_INT128
+static jit_type_t jit_type_llong;
+static jit_type_t jit_type_ullong;
+#endif
 
-typedef struct PSI_ClosureData {
-       void *context;
-       impl *impl;
+struct psi_jit_context {
+       jit_context_t jit;
        jit_type_t signature;
-       zval return_value;
-} PSI_ClosureData;
+};
 
-static inline PSI_ClosureData *PSI_ClosureDataAlloc(void *context, impl *impl) {
-       PSI_ClosureData *data = malloc(sizeof(*data));
+struct psi_jit_callback_info {
+       struct psi_jit_impl_info *impl_info;
+       struct psi_let_exp *let_exp;
 
-       data->context = context;
-       data->impl = impl;
+       void *closure;
+};
 
-       return data;
-}
+struct psi_jit_decl_info {
+       jit_type_t signature;
+       struct psi_jit_struct_info *rv_array;
+       void *params[1];
+};
 
-static inline size_t impl_num_min_args(impl *impl) {
-       size_t i, n = impl->func->args->count;
+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;
+};
 
-       for (i = 0; i < impl->func->args->count; ++i) {
-               if (impl->func->args->args[i]->def) {
-                       --n;
-               }
-       }
-       return n;
-}
+struct psi_jit_impl_info {
+       struct psi_context *context;
+       struct psi_call_frame *frame;
 
-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) {
+       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:
+               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;
+#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_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;
        case PSI_T_FLOAT:
                return jit_type_sys_float;
        case PSI_T_DOUBLE:
                return jit_type_sys_double;
-       default:
-               abort();
+#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;
 }
-static void handler(jit_type_t _sig, void *result, void **_args, void *_data)
+
+static void psi_jit_type_free(jit_type_t *typ_ptr)
 {
-       zend_execute_data *execute_data = *(zend_execute_data **)_args[0];
-       zval *return_value = *(zval **)_args[1];
-       PSI_ClosureData *data = _data;
-       impl_arg *iarg;
-       size_t i;
-       void **arg_ptr = NULL, **arg_prm = NULL;
-       impl_val ret_val, *arg_val = NULL;
-       jit_type_t signature, *sig_prm;
-
-       if (!data->impl->func->args->count) {
-               if (SUCCESS != zend_parse_parameters_none()) {
-                       return;
-               }
-       } else
-       ZEND_PARSE_PARAMETERS_START(impl_num_min_args(data->impl), data->impl->func->args->count)
-       nextarg:
-               iarg = data->impl->func->args->args[_i];
-               if (iarg->def) {
-                       Z_PARAM_OPTIONAL;
-               }
-               if (PSI_T_BOOL == iarg->type->type) {
-                       if (iarg->def) {
-                               iarg->val.bval = iarg->def->type == PSI_T_TRUE ? 1 : 0;
-                       }
-                       Z_PARAM_BOOL(iarg->val.bval);
-               } 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) {
-                       if (iarg->def) {
-                               /* FIXME */
-                               iarg->val.str.len = strlen(iarg->def->text) - 2;
-                               iarg->val.str.val = &iarg->def->text[1];
-                       }
-                       Z_PARAM_STRING(iarg->val.str.val, iarg->val.str.len);
-               } else {
-                       error_code = ZPP_ERROR_FAILURE;
-                       break;
-               }
-               iarg->_zv = _arg;
-               if (_i < _max_num_args) {
-                       goto nextarg;
+       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;
+
+       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 bool 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 true;
+}
+
+static void psi_jit_free(void)
+{
+#if HAVE_INT128
+       jit_type_free(jit_type_llong);
+       jit_type_free(jit_type_ullong);
+#endif
+}
+
+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};
+
+       context->jit = jit_context_create();
+       if (!context->jit) {
+               pefree(context, 1);
+               return false;
+       }
+
+       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;
+       }
+
+       C->context = context;
+       return true;
+}
+
+static void psi_jit_dtor(struct psi_context *C)
+{
+       if (C->context) {
+               struct psi_jit_context *context = C->context;
+
+               jit_type_free(context->signature);
+               jit_context_destroy(context->jit);
+
+               pefree(C->context, 1);
+               C->context = NULL;
+       }
+}
+
+
+static bool psi_jit_composite_init(struct psi_context *C,
+               struct psi_decl_arg *darg)
+{
+       struct psi_jit_struct_info *info;
+
+       if (darg->engine.type) {
+               return true;
+       }
+
+       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);
+
+       darg->engine.info = info;
+       darg->engine.type = info->strct;
+
+       return true;
+}
+
+static void psi_jit_composite_dtor(struct psi_context *C,
+               struct psi_decl_arg *darg)
+{
+       struct psi_jit_struct_info *info = darg->engine.info;
+
+       if (info) {
+               darg->engine.info = NULL;
+               darg->engine.type = NULL;
+
+               jit_type_free(info->strct);
+               /* just free */
+               pefree(info->eles, 1);
+               pefree(info, 1);
+       }
+}
+
+static void psi_jit_extvar_get(jit_type_t sig, void *result, void **args, void *data)
+{
+       struct psi_decl_extvar *evar = data;
+
+       psi_decl_extvar_get(evar, result);
+}
+
+static void psi_jit_extvar_set(jit_type_t sig, void *result, void **args, void *data)
+{
+       struct psi_decl_extvar *evar = data;
+
+       psi_decl_extvar_set(evar, args[0]);
+}
+
+static bool psi_jit_decl_init(struct psi_context *, struct psi_decl *);
+
+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);
+
+       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;
+       }
+
+       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;
+       }
+
+       evar->getter->sym = info->get.closure;
+       evar->setter->sym = info->set.closure;
+
+       jit_context_build_end(ctx->jit);
+       return true;
+failure: ;
+       jit_context_build_end(ctx->jit);
+       return false;
+}
+
+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 bool psi_jit_decl_init(struct psi_context *C, struct psi_decl *decl)
+{
+       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);
+
+               decl->info = info;
+
+               jit_context_build_start(ctx->jit);
+
+               for (i = 0; psi_plist_get(decl->args, i, &arg); ++i) {
+                       info->params[i] = psi_context_decl_arg_call_type(C, arg);
                }
-       ZEND_PARSE_PARAMETERS_END();
-
-       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));
-               arg_val = malloc(data->impl->decl->args->count * sizeof(*arg_val));
-               sig_prm = malloc(data->impl->decl->args->count * sizeof(*sig_prm));
-
-               for (i = 0; i < data->impl->decl->args->count; ++i) {
-                       decl_arg *darg = data->impl->decl->args->args[i];
-                       impl_arg *iarg = darg->let->arg;
-
-                       switch (darg->let->val->func->type) {
-                       case PSI_T_BOOLVAL:
-                               if (iarg->type->type == PSI_T_BOOL) {
-                                       arg_val[i].bval = iarg->val.bval;
-                               } else {
-                                       arg_val[i].bval = zend_is_true(iarg->_zv);
-                               }
-                               break;
-                       case PSI_T_INTVAL:
-                               if (iarg->type->type == PSI_T_INT) {
-                                       arg_val[i].lval = iarg->val.lval;
-                               } else {
-                                       arg_val[i].lval = zval_get_long(iarg->_zv);
-                               }
-                               break;
-                       case PSI_T_STRVAL:
-                               if (iarg->type->type == PSI_T_STRING) {
-                                       arg_val[i].str.val = estrndup(iarg->val.str.val, iarg->val.str.len);
-                               } else {
-                                       zend_string *zs = zval_get_string(iarg->_zv);
-                                       arg_val[i].str.val = estrndup(zs->val, zs->len);
-                                       zend_string_release(zs);
-                               }
-                               break;
-                       case PSI_T_STRLEN:
-                               if (iarg->type->type == PSI_T_STRING) {
-                                       arg_val[i].lval =iarg->val.str.len;
-                               } else {
-                                       zend_string *zs = zval_get_string(iarg->_zv);
-                                       arg_val[i].lval = zs->len;
-                                       zend_string_release(zs);
-                               }
-                               break;
-                       }
-                       arg_ptr[i] = &arg_val[i];
-                       arg_prm[i] = darg->let->val->is_reference ? &arg_ptr[i] : arg_ptr[i];
-                       sig_prm[i] = psi_jit_decl_arg_type(darg);
+               info->params[c] = NULL;
+
+               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);
+
+               jit_context_build_end(ctx->jit);
+
+               if (!info->signature) {
+                       pefree(info, 1);
+                       decl->info = NULL;
+                       return false;
                }
        }
 
-       signature = jit_type_create_signature(
-                       psi_jit_abi(data->impl->decl->abi->convention),
-                       psi_jit_decl_arg_type(data->impl->decl->func),
-                       sig_prm,
-                       data->impl->decl->args->count,
-                       1);
-       jit_apply(signature, data->impl->decl->dlptr, arg_prm, data->impl->decl->args->count, &ret_val);
-
-       switch (data->impl->stmts->ret.list[0]->func->type) {
-       case PSI_T_TO_STRING:
-               if (data->impl->decl->func->var->pointer_level) {
-                       switch (real_decl_type(data->impl->decl->func->type)->type) {
-                       case PSI_T_CHAR:
-                       case PSI_T_SINT8:
-                       case PSI_T_UINT8:
-                               RETVAL_STRING(ret_val.str.val);
-                               break;
-                       }
-               }
-               break;
+       return true;
+}
+
+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 zend_function_entry *compile(PSI_Context *C, PSI_Data *D)
+static bool psi_jit_impl_init(struct psi_context *C,
+               struct psi_impl *impl, zif_handler *zh)
 {
-       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_context *context = C->context;
+       struct psi_jit_impl_info *info = calloc(1, sizeof(*info));
 
-       jit_context_build_start(C->opaque);
+       impl->info = info;
+       info->context = C;
 
-       for (i = 0; i < D->impls->count; ++i) {
-               zend_function_entry *zf;
-               PSI_ClosureData *data;
+       info->closure = jit_closure_create(context->jit, context->signature,
+                       &psi_jit_handler, impl);
 
-               if (!D->impls->list[i]->decl) {
-                       continue;
-               }
+       if (!info->closure) {
+               goto failure;
+       }
 
-               zf = &zfe[j++];
-               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->opaque, signature, &handler, data);
+       *zh = info->closure;
+       return true;
+
+failure: ;
+       impl->info = NULL;
+       pefree(info, 1);
+       return false;
+}
+
+
+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;
        }
+}
 
-       jit_context_build_end(C->opaque);
+static bool psi_jit_cb_init(struct psi_context *C,
+               struct psi_let_exp *exp, struct psi_impl *impl)
+{
+       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 (!psi_jit_decl_init(C, exp->data.callback->decl)) {
+               return false;
+       }
+
+       cb_info = pecalloc(1, sizeof(*cb_info), 1);
+       cb_info->impl_info = impl->info;
+       cb_info->let_exp = exp;
+
+       decl_info = exp->data.callback->decl->info;
+       cb_info->closure = jit_closure_create(context->jit, decl_info->signature,
+                       &psi_jit_callback, cb_info);
+
+       if (!cb_info->closure) {
+               free(cb_info);
+               return false;
+       }
+
+       assert(!exp->data.callback->decl->sym);
+       exp->data.callback->info = cb_info;
+       exp->data.callback->decl->sym = cb_info->closure;
+
+       return true;
+}
+
+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;
+       }
+}
 
-       return zfe;
+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;
+       }
 }
 
-static PSI_ContextOps ops = {
-       init,
-       dtor,
-       compile,
+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);
+       }
+
+       signature = jit_type_create_signature(jit_abi_vararg,
+                       jit_type_get_return(decl_info->signature),
+                       param_types, argc + va_count, 1);
+       assert(signature);
+
+       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_type_free(signature);
+
+       efree(param_types);
+}
+
+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_typeof_decl(struct psi_context *C, token_t decl_type)
+{
+       return psi_jit_token_type(decl_type);
+}
+
+static void *psi_jit_copyof_type(struct psi_context *C, void *orig_type)
+{
+       return jit_type_copy(orig_type);
+}
+
+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);
+
+       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_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,
 };
 
-PSI_ContextOps *PSI_Libjit(void)
+struct psi_context_ops *psi_libjit_ops(void)
 {
        return &ops;
 }
+
+#endif /* HAVE_LIBJIT */