return frame->pointers;
}
+void *psi_call_frame_get_rpointer(struct psi_call_frame *frame) {
+ return frame->rpointer;
+}
+
+struct psi_decl *psi_call_frame_get_decl(struct psi_call_frame *frame) {
+ return frame->decl;
+}
+
+struct psi_impl *psi_call_frame_get_impl(struct psi_call_frame *frame) {
+ return frame->impl;
+}
+
+struct psi_context *psi_call_frame_get_context(struct psi_call_frame *frame) {
+ return frame->context;
+}
+
ZEND_RESULT_CODE psi_call_frame_parse_args(struct psi_call_frame *frame,
zend_execute_data *execute_data) {
size_t i, argc = psi_plist_count(frame->impl->func->args);
}
void psi_call_frame_do_call(struct psi_call_frame *frame) {
- size_t va_count = psi_call_frame_num_var_args(frame);
-
- if (va_count) {
- void **va_types = ecalloc(va_count, sizeof(void *));
- size_t i;
-
- for (i = 0; i < va_count; ++i) {
- struct psi_call_frame_argument *frame_arg;
-
- frame_arg = psi_call_frame_get_var_argument(frame, i);
- va_types[i] = frame->context->ops->query(frame->context,
- PSI_CONTEXT_QUERY_TYPE, &frame_arg->va_type);
- }
-
- frame->context->ops->call_va(frame->context,
- frame,
- frame->decl,
- frame->rpointer,
- frame->pointers,
- va_count,
- va_types);
-
- efree(va_types);
- } else {
- frame->context->ops->call(frame->context,
- frame,
- frame->decl,
- frame->rpointer,
- frame->pointers);
- }
+ frame->context->ops->call(frame);
}
void psi_call_frame_do_callback(struct psi_call_frame *frame, struct psi_call_frame_callback *cbdata)
void psi_call_frame_enter(struct psi_call_frame *frame);
+struct psi_context *psi_call_frame_get_context(struct psi_call_frame *frame);
+struct psi_decl *psi_call_frame_get_decl(struct psi_call_frame *frame);
+struct psi_impl *psi_call_frame_get_impl(struct psi_call_frame *frame);
void **psi_call_frame_get_arg_pointers(struct psi_call_frame *frame);
+void *psi_call_frame_get_rpointer(struct psi_call_frame *frame);
ZEND_RESULT_CODE psi_call_frame_do_let(struct psi_call_frame *frame);
ZEND_RESULT_CODE psi_call_frame_do_assert(struct psi_call_frame *frame, enum psi_assert_kind kind);
return FAILURE;
}
- psi_call_frame_do_call(frame);
+ C->ops->call(frame);
if (SUCCESS != psi_call_frame_do_assert(frame, PSI_ASSERT_POST)) {
psi_call_frame_do_return(frame, return_value);
void (*init)(struct psi_context *C);
void (*dtor)(struct psi_context *C);
zend_function_entry *(*compile)(struct psi_context *C);
- void (*call)(struct psi_context *C, struct psi_call_frame *frame, struct psi_decl *psi_decl, void *rval, void **args);
- void (*call_va)(struct psi_context *C, struct psi_call_frame *frame, struct psi_decl *psi_decl, void *rval, void **args, size_t va_count, void **va_types);
+ void (*call)(struct psi_call_frame *frame);
void *(*query)(struct psi_context *C, enum psi_context_query q, void *arg);
};
static inline ffi_type *psi_ffi_decl_arg_type(struct psi_decl_arg *darg);
-struct psi_ffi_context {
- ffi_cif signature;
- ffi_type *params[2];
-};
-
-struct psi_ffi_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_ffi_call *impl_call;
- } cb;
- } impl;
- void *code;
- ffi_closure *closure;
- ffi_cif signature;
- ffi_type *params[1]; /* [type1, type2, ... ] */
-};
-
-static void psi_ffi_handler(ffi_cif *sig, void *result, void **args, void *data)
-{
- struct psi_ffi_call *call = data;
-
- psi_context_call(call->context, *(zend_execute_data **)args[0], *(zval **)args[1], call->impl.fn.impl);
-}
-
-static void psi_ffi_callback(ffi_cif *sig, void *result, void **args, void *data)
-{
- struct psi_ffi_call *call = data, *impl_call = call->impl.cb.impl_call;
-
- if (impl_call->impl.fn.frame) {
- struct psi_call_frame_callback cbdata;
-
- cbdata.cb = call->impl.cb.let_exp;
- cbdata.argc = sig->nargs;
- cbdata.argv = args;
- cbdata.rval = result;
-
- psi_call_frame_do_callback(impl_call->impl.fn.frame, &cbdata);
- } else {
- assert(0);
- }
-}
-
-static inline ffi_abi psi_ffi_abi(const char *convention) {
- if (FFI_LAST_ABI - 2 != FFI_FIRST_ABI) {
-#ifdef HAVE_FFI_STDCALL
- if (!strcasecmp(convention, "stdcall")) {
- return FFI_STDCALL;
- }
-#endif
-#ifdef HAVE_FFI_FASTCALL
- if (!strcasecmp(convention, "fastcall")) {
- return FFI_FASTCALL;
- }
-#endif
- }
- return FFI_DEFAULT_ABI;
-}
-
-static inline struct psi_ffi_call *psi_ffi_call_alloc(struct psi_context *C, struct psi_decl *decl) {
- int rc;
- size_t i, c = psi_plist_count(decl->args);
- struct psi_ffi_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_ffi_decl_arg_type(arg);
- }
- call->params[c] = NULL;
-
- rc = ffi_prep_cif(&call->signature, psi_ffi_abi(decl->abi->convention),
- c, psi_ffi_decl_arg_type(decl->func), call->params);
- assert(FFI_OK == rc);
-
- return call;
-}
-
-static inline ffi_status psi_ffi_call_init_closure(struct psi_context *C, struct psi_ffi_call *call, struct psi_impl *impl) {
- struct psi_ffi_context *context = C->context;
-
- call->impl.fn.impl = impl;
- return psi_ffi_prep_closure(&call->closure, &call->code, &context->signature, psi_ffi_handler, call);
-}
-
-static inline ffi_status psi_ffi_call_init_callback_closure(struct psi_context *C,
- struct psi_ffi_call *call, struct psi_ffi_call *impl_call,
- struct psi_let_exp *cb) {
- call->impl.cb.let_exp = cb;
- call->impl.cb.impl_call = impl_call;
- return psi_ffi_prep_closure(&call->closure, &call->code, &call->signature, psi_ffi_callback, call);
-}
-
-static inline void psi_ffi_call_free(struct psi_ffi_call *call) {
- if (call->closure) {
- psi_ffi_closure_free(call->closure);
- }
- free(call);
-}
-
static inline ffi_type *psi_ffi_token_type(token_t t) {
switch (t) {
default:
}
}
+static inline ffi_abi psi_ffi_abi(const char *convention) {
+ if (FFI_LAST_ABI - 2 != FFI_FIRST_ABI) {
+#ifdef HAVE_FFI_STDCALL
+ if (!strcasecmp(convention, "stdcall")) {
+ return FFI_STDCALL;
+ }
+#endif
+#ifdef HAVE_FFI_FASTCALL
+ if (!strcasecmp(convention, "fastcall")) {
+ return FFI_FASTCALL;
+ }
+#endif
+ }
+ return FFI_DEFAULT_ABI;
+}
+
+struct psi_ffi_context {
+ ffi_cif signature;
+ ffi_type *params[2];
+};
-static inline struct psi_ffi_context *psi_ffi_context_init(struct psi_ffi_context *L) {
- ffi_status rc;
+struct psi_ffi_impl_info {
+ struct psi_context *context;
+ struct psi_call_frame *frame;
- if (!L) {
- L = malloc(sizeof(*L));
- }
- memset(L, 0, sizeof(*L));
+ void *code;
+ ffi_closure *closure;
+};
- L->params[0] = &ffi_type_pointer;
- L->params[1] = &ffi_type_pointer;
- rc = ffi_prep_cif(&L->signature, FFI_DEFAULT_ABI, 2, &ffi_type_void, L->params);
- assert(rc == FFI_OK);
+struct psi_ffi_callback_info {
+ struct psi_ffi_impl_info *impl_info;
+ struct psi_let_exp *let_exp;
- return L;
+ void *code;
+ ffi_closure *closure;
+};
+
+struct psi_ffi_decl_info {
+ ffi_cif signature;
+ ffi_type *params[1];
+};
+
+static inline struct psi_ffi_decl_info *psi_ffi_decl_init(struct psi_decl *decl) {
+ if (!decl->info) {
+ int rc;
+ size_t i, c = psi_plist_count(decl->args);
+ struct psi_decl_arg *arg;
+ struct psi_ffi_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_ffi_decl_arg_type(arg);
+ }
+ info->params[c] = NULL;
+
+ rc = ffi_prep_cif(&info->signature, psi_ffi_abi(decl->abi->convention),
+ c, psi_ffi_decl_arg_type(decl->func), info->params);
+
+ if (FFI_OK != rc) {
+ free(info);
+ } else {
+ decl->info = info;
+ }
+ }
+
+ return decl->info;
}
-static inline void psi_ffi_context_free(struct psi_ffi_context **L) {
- if (*L) {
- free(*L);
- *L = NULL;
+static inline void psi_ffi_decl_dtor(struct psi_decl *decl) {
+ if (decl->info) {
+ free(decl->info);
+ decl->info = NULL;
}
}
-static void psi_ffi_init(struct psi_context *C)
+static void psi_ffi_handler(ffi_cif *sig, void *result, void **args, void *data)
{
- C->context = psi_ffi_context_init(NULL);
+ struct psi_impl *impl = data;
+ struct psi_ffi_impl_info *info = impl->info;
+
+ psi_context_call(info->context, *(zend_execute_data **)args[0], *(zval **)args[1], impl);
}
-static inline void psi_ffi_destroy_callbacks(struct psi_context *C, struct psi_let_exp *let_exp) {
+static void psi_ffi_callback(ffi_cif *sig, void *result, void **args, void *data)
+{
+ struct psi_ffi_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 = sig->nargs;
+ cb_data.argv = args;
+ cb_data.rval = result;
+
+ psi_call_frame_do_callback(cb_info->impl_info->frame, &cb_data);
+}
+
+static inline void psi_ffi_callback_init(struct psi_ffi_impl_info *impl_info,
+ struct psi_let_exp *let_exp) {
+ struct psi_ffi_callback_info *cb_info;
+ struct psi_ffi_decl_info *decl_info;
struct psi_let_callback *cb;
struct psi_let_func *fn = NULL;
+ ffi_status rc;
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_ffi_decl_init(cb->decl);
+ }
+
+ cb_info = calloc(1, sizeof(*cb_info));
+ cb_info->impl_info = impl_info;
+ cb_info->let_exp = let_exp;
+ rc = psi_ffi_prep_closure(&cb_info->closure, &cb_info->code,
+ &decl_info->signature, psi_ffi_callback, cb_info);
- if (cb->decl && cb->decl->info) {
- psi_ffi_call_free(cb->decl->info);
+ if (FFI_OK != rc) {
+ free(cb_info);
+ break;
}
+ cb->info = cb_info;
+
+ assert(!cb->decl->sym);
+ cb->decl->sym = cb_info->code;
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;
+ struct psi_let_exp *inner_let;
- while (psi_plist_get(fn->inner, i++, &cb)) {
- psi_ffi_destroy_callbacks(C, cb);
+ while (psi_plist_get(fn->inner, i++, &inner_let)) {
+ psi_ffi_callback_init(impl_info, inner_let);
}
}
break;
}
}
-static void psi_ffi_dtor(struct psi_context *C)
-{
- if (C->decls) {
- size_t i = 0;
- struct psi_decl *decl;
-
- while (psi_plist_get(C->decls, i++, &decl)) {
- if (decl->info) {
- psi_ffi_call_free(decl->info);
- }
- }
-
- }
- if (C->impls) {
- size_t i = 0;
- struct psi_impl *impl;
-
- while (psi_plist_get(C->impls, i++, &impl)) {
- size_t j = 0;
- struct psi_let_stmt *let;
-
- while (psi_plist_get(impl->stmts.let, j++, &let)) {
- psi_ffi_destroy_callbacks(C, let->exp);
- }
- }
- }
- psi_ffi_context_free((void *) &C->context);
-}
-
-static inline void psi_ffi_compile_callbacks(struct psi_context *C,
- struct psi_ffi_call *impl_call, struct psi_let_exp *let_exp) {
- struct psi_ffi_call *call;
+static inline void psi_ffi_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;
- if ((call = psi_ffi_call_alloc(C, cb->decl))) {
- if (FFI_OK != psi_ffi_call_init_callback_closure(C, call, impl_call, let_exp)) {
- psi_ffi_call_free(call);
- break;
- }
- cb->decl->sym = call->code;
+ psi_ffi_decl_dtor(cb->decl);
+
+ if (cb->info) {
+ struct psi_ffi_callback_info *info = cb->info;
+
+ if (info->closure) {
+ psi_ffi_closure_free(info->closure);
+ }
+ free(info);
+ cb->info = NULL;
}
fn = cb->func;
/* no break */
if (!fn) {
fn = let_exp->data.func;
}
+
if (fn->inner) {
size_t i = 0;
- struct psi_let_exp *inner_let;
+ struct psi_let_exp *cb;
- while (psi_plist_get(fn->inner, i++, &inner_let)) {
- psi_ffi_compile_callbacks(C, impl_call, inner_let);
+ while (psi_plist_get(fn->inner, i++, &cb)) {
+ psi_ffi_callback_dtor(cb);
}
}
break;
}
}
+static inline struct psi_ffi_impl_info *psi_ffi_impl_init(struct psi_impl *impl,
+ struct psi_context *C) {
+ struct psi_ffi_context *context = C->context;
+ struct psi_ffi_impl_info *info = calloc(1, sizeof(*info));
+ struct psi_let_stmt *let;
+ ffi_status rc;
+ size_t l = 0;
+
+ info->context = C;
+
+ rc = psi_ffi_prep_closure(&info->closure, &info->code,
+ &context->signature, psi_ffi_handler, impl);
+
+ if (FFI_OK != rc) {
+ free(info);
+ return NULL;
+ }
+
+ while (psi_plist_get(impl->stmts.let, l++, &let)) {
+ psi_ffi_callback_init(info, let->exp);
+ }
+
+ return impl->info = info;
+}
+
+static inline void psi_ffi_impl_dtor(struct psi_impl *impl) {
+ struct psi_ffi_impl_info *info = impl->info;
+ struct psi_let_stmt *let;
+ size_t j = 0;
+
+ while (psi_plist_get(impl->stmts.let, j++, &let)) {
+ psi_ffi_callback_dtor(let->exp);
+ }
+
+ if (info) {
+ if (info->closure) {
+ psi_ffi_closure_free(info->closure);
+ }
+ free(info);
+ impl->info = NULL;
+ }
+}
+
+
+static inline struct psi_ffi_context *psi_ffi_context_init(struct psi_ffi_context *L) {
+ ffi_status rc;
+
+ if (!L) {
+ L = malloc(sizeof(*L));
+ }
+ memset(L, 0, sizeof(*L));
+
+ L->params[0] = &ffi_type_pointer;
+ L->params[1] = &ffi_type_pointer;
+ rc = ffi_prep_cif(&L->signature, FFI_DEFAULT_ABI, 2, &ffi_type_void, L->params);
+ assert(rc == FFI_OK);
+
+ return L;
+}
+
+static inline void psi_ffi_context_free(struct psi_ffi_context **L) {
+ if (*L) {
+ free(*L);
+ *L = NULL;
+ }
+}
+
+static void psi_ffi_init(struct psi_context *C)
+{
+ C->context = psi_ffi_context_init(NULL);
+}
+
+static void psi_ffi_dtor(struct psi_context *C)
+{
+ if (C->decls) {
+ size_t i = 0;
+ struct psi_decl *decl;
+
+ while (psi_plist_get(C->decls, i++, &decl)) {
+ psi_ffi_decl_dtor(decl);
+ }
+
+ }
+ if (C->impls) {
+ size_t i = 0;
+ struct psi_impl *impl;
+
+ while (psi_plist_get(C->impls, i++, &impl)) {
+ psi_ffi_impl_dtor(impl);
+ }
+ }
+ psi_ffi_context_free((void *) &C->context);
+}
+
+
static zend_function_entry *psi_ffi_compile(struct psi_context *C)
{
size_t i = 0, d = 0, nf = 0;
zfe = calloc(psi_plist_count(C->impls) + 1, sizeof(*zfe));
while (psi_plist_get(C->impls, i++, &impl)) {
- size_t l = 0;
- struct psi_let_stmt *let;
- struct psi_ffi_call *call;
zend_function_entry *zf = &zfe[nf];
if (!impl->decl) {
continue;
}
- if (!(call = psi_ffi_call_alloc(C, impl->decl))) {
+ if (!psi_ffi_decl_init(impl->decl)) {
continue;
}
- if (FFI_OK != psi_ffi_call_init_closure(C, call, impl)) {
- psi_ffi_call_free(call);
+ if (!psi_ffi_impl_init(impl, C)) {
continue;
}
zf->fname = impl->func->name + (impl->func->name[0] == '\\');
- zf->handler = call->code;
+ zf->handler = ((struct psi_ffi_impl_info *) impl->info)->code;
zf->num_args = psi_plist_count(impl->func->args);
zf->arg_info = psi_internal_arginfo(impl);
++nf;
-
- while (psi_plist_get(impl->stmts.let, l++, &let)) {
- psi_ffi_compile_callbacks(C, call, let->exp);
- }
}
while (psi_plist_get(C->decls, d++, &decl)) {
continue;
}
- psi_ffi_call_alloc(C, decl);
+ psi_ffi_decl_init(decl);
}
return zfe;
}
-static void psi_ffi_call(struct psi_context *C, struct psi_call_frame *frame, struct psi_decl *decl, void *rval, void **args) {
- struct psi_ffi_call *info = decl->info;
- struct psi_call_frame *prev = info->impl.fn.frame;
-
- info->impl.fn.frame = frame;
- ffi_call(&info->signature, FFI_FN(decl->sym), rval, args);
- info->impl.fn.frame = prev;
+static inline void psi_ffi_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_ffi_decl_info *decl_info = decl->info;
+ struct psi_ffi_impl_info *impl_info;
+ struct psi_call_frame *prev;
+
+ if (impl) {
+ impl_info = impl->info;
+ prev = impl_info->frame;
+ impl_info->frame = frame;
+ }
+ ffi_call(&decl_info->signature, FFI_FN(decl->sym),
+ psi_call_frame_get_rpointer(frame),
+ psi_call_frame_get_arg_pointers(frame));
+ if (impl) {
+ impl_info->frame = prev;
+ }
}
-static void psi_ffi_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) {
+static inline void psi_ffi_call_va(struct psi_call_frame *frame) {
ffi_cif signature;
- struct psi_ffi_call *info = decl->info;
- struct psi_call_frame *prev = info->impl.fn.frame;
- size_t argc = psi_plist_count(decl->args);
- ffi_type **param_types = ecalloc(argc + va_count + 1, sizeof(ffi_type *));
-
- memcpy(param_types, info->params, argc * sizeof(ffi_type *));
- memcpy(param_types + argc, va_types, va_count * sizeof(ffi_type *));
-
- psi_ffi_prep_va(&info->signature, &signature, argc, va_count, param_types);
- info->impl.fn.frame = frame;
- ffi_call(&signature, FFI_FN(decl->sym), rval, args);
- info->impl.fn.frame = prev;
+ 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_ffi_decl_info *decl_info = decl->info;
+ struct psi_ffi_impl_info *impl_info;
+ size_t i, va_count, argc;
+ ffi_type **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(ffi_type *));
+ memcpy(param_types, decl_info->params, argc * sizeof(ffi_type *));
+ 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_ffi_impl_type(frame_arg->va_type);
+ }
+
+ psi_ffi_prep_va(&decl_info->signature, &signature, argc, va_count, param_types);
+
+ if (impl) {
+ impl_info = impl->info;
+ prev = impl_info->frame;
+ impl_info->frame = frame;
+ }
+ ffi_call(&signature, FFI_FN(decl->sym),
+ psi_call_frame_get_rpointer(frame),
+ psi_call_frame_get_arg_pointers(frame));
+ if (impl) {
+ impl_info->frame = prev;
+ }
+
efree(param_types);
}
+static void psi_ffi_call(struct psi_call_frame *frame) {
+ if (psi_call_frame_num_var_args(frame)) {
+ psi_ffi_call_va(frame);
+ } else {
+ psi_ffi_call_ex(frame);
+ }
+}
+
static void *psi_ffi_query(struct psi_context *C, enum psi_context_query q, void *arg) {
switch (q) {
case PSI_CONTEXT_QUERY_SELF:
psi_ffi_dtor,
psi_ffi_compile,
psi_ffi_call,
- psi_ffi_call_va,
psi_ffi_query,
};
static inline jit_type_t psi_jit_decl_arg_type(struct psi_decl_arg *darg);
-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;
-}
static inline jit_type_t psi_jit_token_type(token_t t)
{
switch (t) {
static void psi_jit_struct_type_dtor(void *ptr)
{
struct psi_jit_struct_type *type = ptr;
- jit_type_t strct = type->strct;
- unsigned i, n = jit_type_num_fields(strct);
+ unsigned i, n = jit_type_num_fields(type->strct);
for (i = 0; i < n; ++i) {
- jit_type_t field = jit_type_get_field(strct, i);
-
- jit_type_free(field);
+ jit_type_free(jit_type_get_field(type->strct, i));
}
- jit_type_free(strct);
+ jit_type_free(type->strct);
free(type->fields);
free(type);
}
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));
if ((padding = psi_offset_padding(darg->layout->pos - offset, alignment))) {
if (nels + padding > argc) {
argc += padding;
- *fields = realloc(*fields, (argc + 1) * sizeof(*fields));
+ 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;
if (!real->real.strct->engine.type) {
unsigned count;
struct psi_jit_struct_type *type = calloc(1, sizeof(*type));
- jit_type_t strct, *fields = NULL;
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.dtor = psi_jit_struct_type_dtor;
}
- return real->real.strct->engine.type;
+ return ((struct psi_jit_struct_type *) real->real.strct->engine.type)->strct;
case PSI_T_UNION:
{
}
}
+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 psi_jit_call {
+struct psi_jit_impl_info {
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;
+ 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;
+
void *closure;
+};
+
+struct psi_jit_decl_info {
jit_type_t signature;
- void *params[1]; /* [type1, type2, ... ] */
+ 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 void psi_jit_handler(jit_type_t sig, void *result, void **args, void *data)
{
- struct psi_jit_call *call = data;
+ struct psi_impl *impl = data;
+ struct psi_jit_impl_info *info = impl->info;
- psi_context_call(call->context, *(zend_execute_data **)args[0], *(zval **) args[1], call->impl.fn.impl);
+ 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)
+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;
+ struct psi_jit_callback_info *cb_info = data;
+ struct psi_call_frame_callback cb_data;
+
+ assert(cb_info->impl_info->frame);
- cbdata.cb = call->impl.cb.let_exp;
- cbdata.argc = jit_type_num_params(sig);
- cbdata.argv = args;
- cbdata.rval = result;
+ 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(impl_call->impl.fn.frame, &cbdata);
+ psi_call_frame_do_callback(cb_info->impl_info->frame, &cb_data);
}
-static inline struct psi_jit_call *psi_jit_call_alloc(struct psi_context *C,
- struct psi_decl *decl)
+static inline void psi_jit_callback_init(struct psi_jit_impl_info *impl_info,
+ struct psi_let_exp *let_exp)
{
- 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;
+ 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;
- 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;
+ 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 */
- 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);
+ case PSI_LET_FUNC:
+ if (!fn) {
+ fn = let_exp->data.func;
+ }
+ if (fn->inner) {
+ size_t i = 0;
+ struct psi_let_exp *inner_let;
- return call;
+ 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_call_init_closure(struct psi_context *C,
- struct psi_jit_call *call, struct psi_impl *impl)
-{
- struct psi_jit_context *context = C->context;
+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);
- call->impl.fn.impl = impl;
- return call->closure = jit_closure_create(context->jit, context->signature,
- &psi_jit_handler, call);
+ 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 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)
+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;
+ }
- call->impl.cb.let_exp = cb;
- call->impl.cb.impl_call = impl_call;
+ while (psi_plist_get(impl->stmts.let, l++, &let)) {
+ psi_jit_callback_init(info, let->exp);
+ }
- return call->closure = jit_closure_create(context->jit, call->signature,
- &psi_jit_callback, call);
+ return impl->info = info;
}
-static inline void psi_jit_call_free(struct psi_jit_call *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 struct psi_jit_context *psi_jit_context_init(
C->context = psi_jit_context_init(NULL);
}
-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;
- }
-
- if (fn->inner) {
- size_t i = 0;
- struct psi_let_exp *inner_let;
-
- while (psi_plist_get(fn->inner, i++, &inner_let)) {
- psi_jit_destroy_callbacks(C, inner_let);
- }
- }
- break;
- default:
- break;
- }
-}
-
static void psi_jit_dtor(struct psi_context *C)
{
if (C->decls) {
struct psi_decl *decl;
while (psi_plist_get(C->decls, i++, &decl)) {
- if (decl->info) {
- psi_jit_call_free(decl->info);
- }
+ psi_jit_decl_dtor(decl);
}
}
if (C->impls) {
struct psi_impl *impl;
while (psi_plist_get(C->impls, i++, &impl)) {
- size_t l = 0;
- struct psi_let_stmt *let;
-
- while (psi_plist_get(impl->stmts.let, l++, &let)) {
- psi_jit_destroy_callbacks(C, let->exp);
- }
+ psi_jit_impl_dtor(impl);
}
}
psi_jit_context_free((void *) &C->context);
}
-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:
- break;
- }
-}
-
static zend_function_entry *psi_jit_compile(struct psi_context *C)
{
size_t i = 0, d = 0, nf = 0;
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;
if (!impl->decl) {
continue;
}
- if (!(call = psi_jit_call_alloc(C, impl->decl))) {
+ if (!psi_jit_decl_init(impl->decl)) {
continue;
}
- if (!psi_jit_call_init_closure(C, call, impl)) {
- psi_jit_call_free(call);
+ if (!psi_jit_impl_init(impl, C)) {
continue;
}
zf->fname = impl->func->name + (impl->func->name[0] == '\\');
- 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);
++nf;
-
- while (psi_plist_get(impl->stmts.let, l++, &let)) {
- psi_jit_compile_callbacks(C, call, let->exp);
- }
}
while (psi_plist_get(C->decls, d++, &decl)) {
continue;
}
- psi_jit_call_alloc(C, decl);
+ psi_jit_decl_init(decl);
}
jit_context_build_end(ctx->jit);
return zfe;
}
-static void psi_jit_call(struct psi_context *C, struct psi_call_frame *frame,
- struct psi_decl *decl, void *rval, void **args)
-{
- struct psi_jit_call *call = decl->info;
- struct psi_call_frame *prev = call->impl.fn.frame;
-
- call->impl.fn.frame = frame;
- jit_apply(call->signature, decl->sym, args, psi_plist_count(decl->args), rval);
- call->impl.fn.frame = prev;
+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 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);
+static inline void psi_jit_call_va(struct psi_call_frame *frame) {
jit_type_t signature;
- jit_type_t *param_types = ecalloc(argc + va_count + 1, sizeof(jit_type_t));
-
- memcpy(param_types, info->params, argc * sizeof(jit_type_t));
- memcpy(param_types + argc, va_types, va_count * sizeof(jit_type_t));
+ 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(info->signature), param_types, argc + va_count,
+ jit_type_get_return(decl_info->signature),
+ param_types, argc + va_count,
1);
assert(signature);
- info->impl.fn.frame = frame;
- jit_apply(signature, decl->sym, args, argc, rval);
- info->impl.fn.frame = prev;
+ 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)
{
return NULL;
}
-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};
+static struct psi_context_ops ops = {
+ psi_jit_init,
+ psi_jit_dtor,
+ psi_jit_compile,
+ psi_jit_call,
+ psi_jit_query
+};
struct psi_context_ops *psi_libjit_ops(void)
{
void **args;
struct psi_decl_var *dvar;
struct psi_call_frame *free_call;
+ struct psi_context *ctx = psi_call_frame_get_context(frame);
- free_call = psi_call_frame_init(frame->context, f->decl, NULL);
+ free_call = psi_call_frame_init(ctx, f->decl, NULL);
psi_call_frame_enter(free_call);
args = psi_call_frame_get_arg_pointers(free_call);
struct psi_impl {
struct psi_impl_func *func;
struct psi_decl *decl;
+ void *info;
struct {
struct psi_plist *ret;
struct psi_plist *let;
struct psi_let_func *func;
struct psi_plist *args;
struct psi_decl *decl;
+ void *info;
};
struct psi_let_callback *psi_let_callback_init(struct psi_let_func *func, struct psi_plist *args);
case PSI_LET_TMP:
{
- struct psi_let_stmt *let_temp = psi_impl_get_let(frame->impl,
- val->data.var);
+ struct psi_impl *impl = psi_call_frame_get_impl(frame);
+ struct psi_let_stmt *let_temp = psi_impl_get_let(impl, val->data.var);
struct psi_call_frame_symbol *temp_arg;
temp_arg = psi_call_frame_fetch_symbol(frame, let_temp->exp->var);
void psi_return_stmt_exec(struct psi_return_stmt *ret, zval *return_value,
struct psi_call_frame *frame)
{
- psi_set_exp_exec_ex(ret->set, return_value, frame->rpointer, frame);
+ void *rpointer = psi_call_frame_get_rpointer(frame);
+
+ psi_set_exp_exec_ex(ret->set, return_value, rpointer, frame);
}
void psi_return_stmt_free(struct psi_return_stmt **ret_ptr)