X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-psi;a=blobdiff_plain;f=src%2Flibjit.c;h=8b9278ce0c072d4b4bc9c61cacb48184ff8b7b7a;hp=4866809392aafea74425e6f7d9e36c812b2f67f7;hb=7e4b0ccdd2123647b6fff8065c0abc61be3fb44d;hpb=6a8a77b52b636041de54d1022016b4f2aa510c05 diff --git a/src/libjit.c b/src/libjit.c index 4866809..8b9278c 100644 --- a/src/libjit.c +++ b/src/libjit.c @@ -1,10 +1,18 @@ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + #include "php.h" + +#ifdef HAVE_LIBJIT + #include "php_psi.h" #include "libjit.h" #include static void psi_jit_handler(jit_type_t _sig, void *result, void **_args, void *_data); +static inline jit_type_t psi_jit_decl_arg_type(decl_arg *darg); static inline jit_abi_t psi_jit_abi(const char *convention) { return jit_abi_cdecl; @@ -36,14 +44,110 @@ static inline jit_type_t psi_jit_token_type(token_t t) { return jit_type_sys_bool; case PSI_T_INT: 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; +#ifdef HAVE_LONG_DOUBLE + case PSI_T_LONG_DOUBLE: + return jit_type_sys_long_double; +#endif + case PSI_T_POINTER: + return jit_type_void_ptr; + } +} +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; + case PSI_T_FLOAT: + case PSI_T_DOUBLE: + return jit_type_sys_double; + EMPTY_SWITCH_DEFAULT_CASE(); } + return NULL; +} + +static void psi_jit_struct_type_dtor(void *type) { + jit_type_t strct = type; + + jit_type_free(strct); +} + +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(decl_struct *strct, jit_type_t **fields) { + size_t i, argc = strct->args->count, nels = 0, offset = 0, maxalign; + *fields = calloc(argc + 1, sizeof(*fields)); + + for (i = 0; i < strct->args->count; ++i) { + decl_arg *darg = strct->args->args[i]; + jit_type_t type = jit_type_copy(psi_jit_decl_arg_type(darg)); + size_t padding, alignment; + + ZEND_ASSERT(jit_type_get_size(type) == darg->layout->len); + + if ((alignment = jit_type_get_alignment(type)) > maxalign) { + maxalign = alignment; + } + + if ((padding = psi_offset_padding(darg->layout->pos - offset, alignment))) { + if (nels + padding > argc) { + argc += padding; + *fields = realloc(*fields, (argc + 1) * sizeof(*fields)); + } + psi_jit_struct_type_pad(&(*fields)[nels], padding); + nels += padding; + offset += padding; + } + ZEND_ASSERT(offset == darg->layout->pos); + + offset = (offset + darg->layout->len + alignment - 1) & ~(alignment - 1); + (*fields)[nels++] = type; + } + + /* apply struct alignment padding */ + offset = (offset + maxalign - 1) & ~(maxalign - 1); + + ZEND_ASSERT(offset <= strct->size); + if (offset < strct->size) { + nels += psi_jit_struct_type_pad(&(*fields)[nels], strct->size - offset); + } + + return nels; } static inline jit_type_t psi_jit_decl_type(decl_type *type) { - return psi_jit_token_type(real_decl_type(type)->type); + decl_type *real = real_decl_type(type); + + if (real->type == PSI_T_STRUCT) { + if (!real->strct->engine.type) { + unsigned count; + jit_type_t strct, *fields = NULL; + + count = psi_jit_struct_type_elements(real->strct, &fields); + strct = jit_type_create_struct(fields, count, 0); + + real->strct->engine.type = strct; + real->strct->engine.dtor = psi_jit_struct_type_dtor; + } + + return real->strct->engine.type; + } + return psi_jit_token_type(real->type); } static inline jit_type_t psi_jit_decl_arg_type(decl_arg *darg) { if (darg->var->pointer_level) { @@ -65,7 +169,7 @@ typedef struct PSI_LibjitContext { typedef struct PSI_LibjitCall { void *closure; jit_type_t signature; - jit_type_t params[1]; /* [type1, type2, NULL, arg1, arg2] ... */ + void *params[1]; /* [type1, type2, NULL, arg1, arg2] ... */ } PSI_LibjitCall; typedef struct PSI_LibjitData { @@ -84,12 +188,14 @@ static inline PSI_LibjitCall *PSI_LibjitCallAlloc(PSI_Context *C, decl *decl) { 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), - call->params, c, 1); + (jit_type_t *) call->params, c, 1); return call; } @@ -101,6 +207,7 @@ static inline void PSI_LibjitCallInitClosure(PSI_Context *C, PSI_LibjitCall *cal static inline void PSI_LibjitCallFree(PSI_LibjitCall *call) { jit_type_free(call->signature); + free(call); } static inline PSI_LibjitContext *PSI_LibjitContextInit(PSI_LibjitContext *L) { @@ -146,12 +253,14 @@ static void psi_jit_init(PSI_Context *C) static void psi_jit_dtor(PSI_Context *C) { - size_t i; + if (C->decls) { + size_t i; - for (i = 0; i < C->decls->count; ++i) { - decl *decl = C->decls->list[i]; + for (i = 0; i < C->decls->count; ++i) { + decl *decl = C->decls->list[i]; - PSI_LibjitCallFree(decl->call.info); + PSI_LibjitCallFree(decl->call.info); + } } PSI_LibjitContextFree((void *) &C->context); } @@ -203,11 +312,37 @@ static zend_function_entry *psi_jit_compile(PSI_Context *C) return zfe; } -static void psi_jit_call(PSI_Context *C, impl_val *ret_val, decl *decl) { - PSI_LibjitCall *call = decl->call.info; +static void psi_jit_call(PSI_Context *C, decl_callinfo *decl_call, impl_vararg *va) { + PSI_LibjitCall *call = decl_call->info; + + 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 *)); - jit_apply(call->signature, decl->call.sym, decl->call.args, - decl->args->count, ret_val); + 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]; + } + + signature = jit_type_create_signature( + jit_type_get_abi(call->signature), + jit_type_get_return(call->signature), + (jit_type_t *) params, ntotalargs, 1); + ZEND_ASSERT(signature); + + jit_apply(signature, decl_call->sym, ¶ms[ntotalargs + 1], + nfixedargs, *decl_call->rval); + jit_type_free(signature); + free(params); + } else { + jit_apply(call->signature, decl_call->sym, decl_call->args, + decl_call->argc, *decl_call->rval); + } } static PSI_ContextOps ops = { @@ -221,3 +356,5 @@ PSI_ContextOps *PSI_Libjit(void) { return &ops; } + +#endif /* HAVE_LIBJIT */