+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 (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_call(struct psi_call_frame *frame) {
+ if (psi_call_frame_num_var_args(frame)) {
+ psi_jit_call_va(frame);
+ } else {
+ 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 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