From 29137b5f19713219a1b9b5fdf54ad94a34d6fde0 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Mon, 8 Feb 2016 08:36:36 +0100 Subject: [PATCH] flush --- src/engine.c | 7 +++ src/libffi.c | 144 +++++++++++++++++++++++++--------------------- src/parser.h | 9 +++ src/parser.re | 1 + src/parser_proc.h | 39 +++++++------ src/parser_proc.y | 2 +- 6 files changed, 118 insertions(+), 84 deletions(-) diff --git a/src/engine.c b/src/engine.c index dc9d274..a86cd0b 100644 --- a/src/engine.c +++ b/src/engine.c @@ -193,6 +193,8 @@ static inline ZEND_RESULT_CODE psi_parse_args(zend_execute_data *execute_data, i Z_PARAM_PROLOGUE(0); } else if (PSI_T_MIXED == iarg->type->type) { Z_PARAM_PROLOGUE(0); + } else if (PSI_T_CALLABLE == iarg->type->type) { + Z_PARAM_FUNC_EX(iarg->val.zend.cb.fci, iarg->val.zend.cb.fcc, 1, 0); } else { error_code = ZPP_ERROR_FAILURE; break; @@ -323,6 +325,11 @@ static inline impl_val *psi_let_val(token_t let_func, impl_arg *iarg, impl_val * arg_val->ptr = obj->data; } break; + case PSI_T_CBVAL: + if (iarg->type->type == PSI_T_CALLABLE) { + + } + break; EMPTY_SWITCH_DEFAULT_CASE(); } return arg_val; diff --git a/src/libffi.c b/src/libffi.c index be9edfd..428a7e6 100644 --- a/src/libffi.c +++ b/src/libffi.c @@ -43,10 +43,25 @@ static void *psi_ffi_closure_alloc(size_t s, void **code) } return *code; #else - return NULL; +# error "Neither ffi_closure_alloc() nor mmap() available" #endif } +static ffi_status psi_ffi_prep_closure(ffi_closure **closure, void **code, ffi_cif *sig, void (*handler)(ffi_cif*,void*,void**,void*), void *data) { + *closure = psi_ffi_closure_alloc(sizeof(ffi_closure), code); + ZEND_ASSERT(*closure != NULL); + +#if PSI_HAVE_FFI_PREP_CLOSURE_LOC + return ffi_prep_closure_loc(*closure, sig, handler, data, *code); + +#elif PSI_HAVE_FFI_PREP_CLOSURE + return ffi_prep_closure(*code, sig, handler, data); +#else +# error "Neither ffi_prep_closure() nor ffi_prep_closure_loc() is available" +#endif + +} + static void psi_ffi_closure_free(void *c) { #ifdef PSI_HAVE_FFI_CLOSURE_ALLOC @@ -59,9 +74,59 @@ static void psi_ffi_closure_free(void *c) static void psi_ffi_handler(ffi_cif *signature, void *_result, void **_args, void *_data); static inline ffi_type *psi_ffi_decl_arg_type(decl_arg *darg); +typedef struct PSI_LibffiContext { + ffi_cif signature; + ffi_type *params[2]; +} PSI_LibffiContext; + +typedef struct PSI_LibffiCall { + void *code; + ffi_closure *closure; + ffi_cif signature; + void *params[1]; /* [type1, type2, NULL, arg1, arg2] ... */ +} PSI_LibffiCall; + static inline ffi_abi psi_ffi_abi(const char *convention) { return FFI_DEFAULT_ABI; } + +static inline PSI_LibffiCall *PSI_LibffiCallAlloc(PSI_Context *C, decl *decl) { + int rc; + size_t i, c = decl->args ? decl->args->count : 0; + PSI_LibffiCall *call = calloc(1, sizeof(*call) + 2 * c * sizeof(void *)); + + for (i = 0; i < c; ++i) { + call->params[i] = psi_ffi_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]; + + rc = ffi_prep_cif(&call->signature, psi_ffi_abi(decl->abi->convention), + c, psi_ffi_decl_arg_type(decl->func), (ffi_type **) call->params); + ZEND_ASSERT(FFI_OK == rc); + + return call; +} + +static inline void PSI_LibffiCallInitClosure(PSI_Context *C, PSI_LibffiCall *call, impl *impl) { + PSI_LibffiContext *context = C->context; + ffi_status rc; + + rc = psi_ffi_prep_closure(&call->closure, &call->code, &context->signature, psi_ffi_handler, impl); + ZEND_ASSERT(FFI_OK == rc); +} + +static inline void PSI_LibffiCallFree(PSI_LibffiCall *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: @@ -194,6 +259,20 @@ static inline ffi_type *psi_ffi_decl_type(decl_type *type) { decl_type *real = real_decl_type(type); switch (real->type) { + case PSI_T_FUNCTION: + if (!real->func->call.sym) { + PSI_LibffiCall *call = PSI_LibffiCallAlloc(&PSI_G(context), real->func); + ffi_status rc; + + rc = psi_ffi_prep_closure(&real->func->call.closure.data, &real->func->call.sym, + &call->signature, psi_ffi_handler, NULL); + if (FFI_OK == rc) { + real->func->call.info = call; + real->func->call.closure.dtor = psi_ffi_closure_free; + } + } + return &ffi_type_pointer; + case PSI_T_STRUCT: if (!real->strct->engine.type) { ffi_type *strct = calloc(1, sizeof(ffi_type)); @@ -223,69 +302,6 @@ static inline ffi_type *psi_ffi_decl_arg_type(decl_arg *darg) { } } -typedef struct PSI_LibffiContext { - ffi_cif signature; - ffi_type *params[2]; -} PSI_LibffiContext; - -typedef struct PSI_LibffiCall { - void *code; - ffi_closure *closure; - ffi_cif signature; - void *params[1]; /* [type1, type2, NULL, arg1, arg2] ... */ -} PSI_LibffiCall; - -static inline PSI_LibffiCall *PSI_LibffiCallAlloc(PSI_Context *C, decl *decl) { - int rc; - size_t i, c = decl->args ? decl->args->count : 0; - PSI_LibffiCall *call = calloc(1, sizeof(*call) + 2 * c * sizeof(void *)); - - for (i = 0; i < c; ++i) { - call->params[i] = psi_ffi_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]; - - rc = ffi_prep_cif(&call->signature, psi_ffi_abi(decl->abi->convention), - c, psi_ffi_decl_arg_type(decl->func), (ffi_type **) call->params); - ZEND_ASSERT(FFI_OK == rc); - - return call; -} - -static inline void PSI_LibffiCallInitClosure(PSI_Context *C, PSI_LibffiCall *call, impl *impl) { - PSI_LibffiContext *context = C->context; - int rc; - - call->closure = psi_ffi_closure_alloc(sizeof(ffi_closure), &call->code); - ZEND_ASSERT(call->closure != NULL); - -#if PSI_HAVE_FFI_PREP_CLOSURE_LOC - rc = ffi_prep_closure_loc( - call->closure, - &context->signature, - psi_ffi_handler, - impl, - call->code); - -#elif PSI_HAVE_FFI_PREP_CLOSURE - rc = ffi_prep_closure(call->code, &context->signature, psi_ffi_handler, impl); -#else -# error "Neither ffi_prep_closure() nor ffi_prep_closure_loc() available" -#endif - ZEND_ASSERT(FFI_OK == rc); -} - -static inline void PSI_LibffiCallFree(PSI_LibffiCall *call) { - if (call->closure) { - psi_ffi_closure_free(call->closure); - } - free(call); -} static inline PSI_LibffiContext *PSI_LibffiContextInit(PSI_LibffiContext *L) { ffi_status rc; diff --git a/src/parser.h b/src/parser.h index 94860c5..350213c 100644 --- a/src/parser.h +++ b/src/parser.h @@ -7,6 +7,7 @@ #include #include +#include /* fcall */ #include "parser_proc.h" @@ -51,6 +52,10 @@ typedef union impl_val { zend_bool bval; zend_long lval; zend_string *str; + struct { + zend_fcall_info *fci; + zend_fcall_info_cache *fcc; + } cb; } zend; void *ptr; uint8_t _dbg[sizeof(void *)]; @@ -290,6 +295,10 @@ typedef struct decl_callinfo { size_t argc; void **args; void **rval; + struct { + void *data; + void (*dtor)(void *data); + } closure; } decl_callinfo; typedef struct decl { diff --git a/src/parser.re b/src/parser.re index 7e445fd..bb6ccab 100644 --- a/src/parser.re +++ b/src/parser.re @@ -234,6 +234,7 @@ token_t PSI_ParserScan(PSI_Parser *P) 'BOOLVAL' {RETURN(PSI_T_BOOLVAL);} 'ARRVAL' {RETURN(PSI_T_ARRVAL);} 'OBJVAL' {RETURN(PSI_T_OBJVAL);} + 'CBVAL' {RETURN(PSI_T_CBVAL);} 'CALLOC' {RETURN(PSI_T_CALLOC);} 'TO_OBJECT' {RETURN(PSI_T_TO_OBJECT);} 'TO_ARRAY' {RETURN(PSI_T_TO_ARRAY);} diff --git a/src/parser_proc.h b/src/parser_proc.h index 8471c14..04ebb7e 100644 --- a/src/parser_proc.h +++ b/src/parser_proc.h @@ -54,22 +54,23 @@ #define PSI_T_TRUE 54 #define PSI_T_FALSE 55 #define PSI_T_DOLLAR_NAME 56 -#define PSI_T_OBJVAL 57 -#define PSI_T_ARRVAL 58 -#define PSI_T_PATHVAL 59 -#define PSI_T_STRLEN 60 -#define PSI_T_STRVAL 61 -#define PSI_T_FLOATVAL 62 -#define PSI_T_INTVAL 63 -#define PSI_T_BOOLVAL 64 -#define PSI_T_TO_OBJECT 65 -#define PSI_T_TO_ARRAY 66 -#define PSI_T_TO_STRING 67 -#define PSI_T_TO_INT 68 -#define PSI_T_TO_FLOAT 69 -#define PSI_T_TO_BOOL 70 -#define PSI_T_MIXED 71 -#define PSI_T_ARRAY 72 -#define PSI_T_OBJECT 73 -#define PSI_T_CALLABLE 74 -#define PSI_T_AMPERSAND 75 +#define PSI_T_CBVAL 57 +#define PSI_T_OBJVAL 58 +#define PSI_T_ARRVAL 59 +#define PSI_T_PATHVAL 60 +#define PSI_T_STRLEN 61 +#define PSI_T_STRVAL 62 +#define PSI_T_FLOATVAL 63 +#define PSI_T_INTVAL 64 +#define PSI_T_BOOLVAL 65 +#define PSI_T_TO_OBJECT 66 +#define PSI_T_TO_ARRAY 67 +#define PSI_T_TO_STRING 68 +#define PSI_T_TO_INT 69 +#define PSI_T_TO_FLOAT 70 +#define PSI_T_TO_BOOL 71 +#define PSI_T_MIXED 72 +#define PSI_T_ARRAY 73 +#define PSI_T_OBJECT 74 +#define PSI_T_CALLABLE 75 +#define PSI_T_AMPERSAND 76 diff --git a/src/parser_proc.y b/src/parser_proc.y index b4bf960..87f5eae 100644 --- a/src/parser_proc.y +++ b/src/parser_proc.y @@ -720,7 +720,7 @@ let_val(val) ::= let_func(func). { let_calloc(alloc) ::= num_exp(nmemb) COMMA num_exp(size). { alloc = init_let_calloc(nmemb, size); } -%token_class let_func_token OBJVAL ARRVAL PATHVAL STRLEN STRVAL FLOATVAL INTVAL BOOLVAL. +%token_class let_func_token CBVAL OBJVAL ARRVAL PATHVAL STRLEN STRVAL FLOATVAL INTVAL BOOLVAL. %type let_func {let_func*} %destructor let_func {free_let_func($$);} let_func(func) ::= let_func_token(T) LPAREN impl_var(var) RPAREN. { -- 2.30.2