refactor to psi_call
authorMichael Wallner <mike@php.net>
Wed, 18 Nov 2015 17:15:10 +0000 (18:15 +0100)
committerMichael Wallner <mike@php.net>
Wed, 18 Nov 2015 17:15:10 +0000 (18:15 +0100)
20 files changed:
php_psi.h
psis/stat.psi [new file with mode: 0644]
psis/stdlib.psi [new file with mode: 0644]
psis/time.psi [new file with mode: 0644]
psis/uname.psi [new file with mode: 0644]
src/context.c
src/context.h
src/libffi.c
src/libjit.c
src/module.c
src/parser.h
tests/stat/stat.psi [deleted file]
tests/stat/stat001.phpt
tests/time/time.psi [deleted file]
tests/time/time001.phpt
tests/time/time002.phpt
tests/time/time003.phpt
tests/time/time004.phpt
tests/uname/uname.psi [deleted file]
tests/uname/uname001.phpt

index 9ebf8e79925fd21d119c679d0a1baa14aab65397..b4b96e872f59ff8cd43cfcaea5f6d8fcdbfdee63 100644 (file)
--- a/php_psi.h
+++ b/php_psi.h
@@ -38,12 +38,7 @@ void psi_to_double(zval *return_value, token_t t, impl_val *ret_val, set_value *
 void psi_to_string(zval *return_value, token_t t, impl_val *ret_val, set_value *set, decl_var *var);
 void psi_to_array(zval *return_value, token_t t, impl_val *ret_val, set_value *set, decl_var *var);
 
-ZEND_RESULT_CODE psi_parse_args(zend_execute_data *execute_data, impl *impl);
-void *psi_do_let(decl_arg *darg);
-void psi_do_set(zval *return_value, set_value *set);
-void psi_do_return(zval *return_value, return_stmt *ret, impl_val *ret_val);
-void psi_do_free(free_stmt *fre);
-void psi_do_clean(impl *impl);
+void psi_call(zend_execute_data *execute_data, zval *return_value, impl *impl);
 
 ZEND_BEGIN_MODULE_GLOBALS(psi)
        char *engine;
diff --git a/psis/stat.psi b/psis/stat.psi
new file mode 100644 (file)
index 0000000..e5d8eba
--- /dev/null
@@ -0,0 +1,48 @@
+extern int stat(char *path, struct stat *buf);
+function psi\stat(string $path, array &$buf = NULL) : int {
+       let path = strval($path);
+       let buf = calloc(1, struct stat);
+       return to_int(stat);
+       set $buf = to_array(*buf,
+               to_int(st_dev),
+               to_int(st_ino),
+               to_int(st_mode),
+               to_int(st_nlink),
+               to_int(st_uid),
+               to_int(st_gid),
+               to_int(st_rdev),
+               to_int(st_size),
+               to_array(st_atim,
+                       to_int(tv_sec),
+                       to_int(tv_nsec)
+               ), 
+               to_array(st_atimespec,
+                       to_int(tv_sec),
+                       to_int(tv_nsec)
+               ),
+               to_array(st_mtim,
+                       to_int(tv_sec),
+                       to_int(tv_nsec)
+               ), 
+               to_array(st_mtimespec,
+                       to_int(tv_sec),
+                       to_int(tv_nsec)
+               ),
+               to_array(st_ctim,
+                       to_int(tv_sec),
+                       to_int(tv_nsec)
+               ), 
+               to_array(st_ctimespec,
+                       to_int(tv_sec),
+                       to_int(tv_nsec)
+               ),
+               to_array(st_birthtimespec,
+                       to_int(tv_sec),
+                       to_int(tv_nsec)
+               ),
+               to_int(st_blksize),
+               to_int(st_blocks),
+               to_int(st_flags),
+               to_int(st_gen)
+       );
+}
diff --git a/psis/stdlib.psi b/psis/stdlib.psi
new file mode 100644 (file)
index 0000000..6044d41
--- /dev/null
@@ -0,0 +1,2 @@
+
+extern void free(void *ptr);
diff --git a/psis/time.psi b/psis/time.psi
new file mode 100644 (file)
index 0000000..e491185
--- /dev/null
@@ -0,0 +1,81 @@
+extern int gettimeofday(struct timeval *tv, struct timezone *tz);
+function psi\gettimeofday(array &$tv = NULL, array &$tz = NULL) : int {
+       let tv = calloc(1, struct timeval);
+       let tz = calloc(1, struct timezone);
+       return to_int(gettimeofday);
+       set $tv = to_array(*tv, 
+               to_int(tv_sec), 
+               to_int(tv_usec));
+       set $tz = to_array(*tz, 
+               to_int(tz_minuteswest), 
+               to_int(tz_dsttime));
+}
+
+extern char *asctime(struct tm *tm);
+function psi\asctime(array $tm = NULL) : string {
+       let tm = arrval($tm);
+       return to_string(asctime);
+}
+
+extern char *asctime_r(struct tm *tm, char *buf);
+function psi\asctime_r(array $tm = NULL) : string {
+       let tm = arrval($tm);
+       let buf = calloc(32, char);
+       return to_string(asctime_r);
+}
+
+extern struct tm *gmtime(time_t *tp);
+function psi\gmtime(int $ts) : array {
+       let tp = &intval($ts);
+       return to_array(*gmtime,
+               to_int(tm_sec),
+               to_int(tm_min),
+               to_int(tm_hour),
+               to_int(tm_mday),
+               to_int(tm_mon),
+               to_int(tm_year),
+               to_int(tm_wday),
+               to_int(tm_yday),
+               to_int(tm_isdst)
+       );
+}
+
+extern struct tm *gmtime_r(time_t *tp, struct tm *buf);
+function psi\gmtime_r(int $ts) : array {
+       let tp = &intval($ts);
+       let buf = calloc(1, struct tm);
+       return to_array(*gmtime_r,
+               to_int(tm_sec),
+               to_int(tm_min),
+               to_int(tm_hour),
+               to_int(tm_mday),
+               to_int(tm_mon),
+               to_int(tm_year),
+               to_int(tm_wday),
+               to_int(tm_yday),
+               to_int(tm_isdst)
+       );
+}
+
+extern int nanosleep(struct timespec *rqtp, struct timespec *rmtp);
+function psi\nanosleep(array $rq = NULL, array &$rm = NULL) : int {
+       let rqtp = arrval($rq);
+       let rmtp = calloc(1, struct timespec);
+       return to_int(nanosleep);
+       set $rm = to_array(*rmtp,
+               to_int(tv_sec),
+               to_int(tv_nsec)
+       );
+}
+
+extern clock_t times(struct tms *buf);
+function psi\times(array &$tms = NULL) : int {
+       let buf = calloc(1, struct tms);
+       return to_int(times);
+       set $tms = to_array(*buf,
+               to_int(tms_utime),
+               to_int(tms_stime),
+               to_int(tms_cutime),
+               to_int(tms_cstime)
+       );
+}
diff --git a/psis/uname.psi b/psis/uname.psi
new file mode 100644 (file)
index 0000000..25abfcb
--- /dev/null
@@ -0,0 +1,12 @@
+extern int uname(struct utsname *u);
+function psi\uname(array &$u = NULL) : int {
+       let u = calloc(1, struct utsname);
+       return to_int(uname);
+       set $u = to_array(*u,
+               to_string(sysname),
+               to_string(nodename),
+               to_string(release),
+               to_string(version), 
+               to_string(machine),
+               to_string(domainname));
+}
\ No newline at end of file
index e499c5f0d24052c0f5a32475f0f0878d2a0b1fc2..987fb9abc0a45a660ba8353d25047a8e75c6feb4 100644 (file)
@@ -235,19 +235,19 @@ static inline int validate_decl_func(PSI_Data *data, void *dl, decl *decl, decl_
 #ifndef RTLD_NEXT
 # define RTLD_NEXT ((void *) -1l)
 #endif
-       decl->dlptr = dlsym(dl ?: RTLD_NEXT, func->var->name);
-       if (!decl->dlptr) {
+       decl->call.sym = dlsym(dl ?: RTLD_NEXT, func->var->name);
+       if (!decl->call.sym) {
                size_t i;
 
                for (i = 0; i < psi_predef_func_count(); ++i) {
                        psi_predef_func *pre = &psi_predef_funcs[i];
 
                        if (!strcmp(func->var->name, pre->name)) {
-                               decl->dlptr = pre->func;
+                               decl->call.sym = pre->func;
                                break;
                        }
                }
-               if (!decl->dlptr) {
+               if (!decl->call.sym) {
                        data->error(PSI_WARNING, "Failed to locate symbol '%s': %s",
                                func->var->name, dlerror());
                }
@@ -368,10 +368,13 @@ static inline int validate_impl_ret_stmt(PSI_Data *data, impl *impl) {
                                impl->func->name);
                return 0;
        }
+
        if (!validate_set_value(data, ret->set, ret->decl)) {
                return 0;
        }
 
+       impl->decl->impl = impl;
+
        return 1;
 }
 static inline int validate_impl_let_stmts(PSI_Data *data, impl *impl) {
@@ -576,7 +579,13 @@ PSI_Context *PSI_ContextInit(PSI_Context *C, PSI_ContextOps *ops, PSI_ContextErr
 
        C->error = error;
        C->ops = ops;
-       ops->init(C);
+
+       if (ops->init) {
+               ops->init(C);
+       }
+
+       ZEND_ASSERT(ops->call != NULL);
+       ZEND_ASSERT(ops->compile != NULL);
 
        /* build up predefs in a temporary PSI_Data for validation */
        memset(&T, 0, sizeof(T));
@@ -818,29 +827,34 @@ zend_function_entry *PSI_ContextCompile(PSI_Context *C)
                }
        }
 
-
        return C->closures = C->ops->compile(C);
 }
 
 
-void PSI_ContextCall(PSI_Context *C, impl_val *ret_val, decl *decl, impl_val **args)
+void PSI_ContextCall(PSI_Context *C, impl_val *ret_val, decl *decl)
 {
-       C->ops->call(C, ret_val, decl, args);
+       C->ops->call(C, ret_val, decl);
 }
 
 void PSI_ContextDtor(PSI_Context *C)
 {
        size_t i;
+       zend_function_entry *zfe;
 
-       C->ops->dtor(C);
+       if (C->ops->dtor) {
+               C->ops->dtor(C);
+       }
 
        free_decl_libs(&C->psi.libs);
 
        for (i = 0; i < C->count; ++i) {
                PSI_DataDtor(&C->data[i]);
        }
-
        free(C->data);
+
+       for (zfe = C->closures; zfe->fname; ++zfe) {
+               free((void *) zfe->arg_info);
+       }
        free(C->closures);
 
        if (C->consts) {
index 77db31a56fbd5aa8df89224b371a55b7f95767df..4f86444770dc36a7a190a7d4d83c8fd868c678ff 100644 (file)
@@ -14,7 +14,7 @@ struct PSI_ContextOps {
        void (*init)(PSI_Context *C);
        void (*dtor)(PSI_Context *C);
        zend_function_entry *(*compile)(PSI_Context *C);
-       void (*call)(PSI_Context *C, impl_val *ret_val, decl *decl, impl_val **args);
+       void (*call)(PSI_Context *C, impl_val *ret_val, decl *decl);
 };
 
 struct PSI_Context {
@@ -30,7 +30,7 @@ PSI_Context *PSI_ContextInit(PSI_Context *C, PSI_ContextOps *ops, PSI_ContextErr
 void PSI_ContextBuild(PSI_Context *C, const char *path);
 int PSI_ContextValidate(PSI_Context *C, PSI_Parser *P);
 zend_function_entry *PSI_ContextCompile(PSI_Context *C);
-void PSI_ContextCall(PSI_Context *C, impl_val *ret_val, decl *decl, impl_val **args);
+void PSI_ContextCall(PSI_Context *C, impl_val *ret_val, decl *decl);
 void PSI_ContextDtor(PSI_Context *C);
 void PSI_ContextFree(PSI_Context **C);
 
index f1d72829ae5c231d7d88c628bdebf5272a0dc39a..d802bd1f293ee09aafabb82ff66a4bd4e02abcc0 100644 (file)
@@ -53,7 +53,7 @@ static void psi_ffi_handler(ffi_cif *signature, void *_result, void **_args, voi
 static inline ffi_abi psi_ffi_abi(const char *convention) {
        return FFI_DEFAULT_ABI;
 }
-static inline ffi_type *psi_ffi_type(token_t t) {
+static inline ffi_type *psi_ffi_token_type(token_t t) {
        switch (t) {
        default:
                ZEND_ASSERT(0);
@@ -87,7 +87,7 @@ static inline ffi_type *psi_ffi_type(token_t t) {
        }
 }
 static inline ffi_type *psi_ffi_decl_type(decl_type *type) {
-       return psi_ffi_type(real_decl_type(type)->type);
+       return psi_ffi_token_type(real_decl_type(type)->type);
 }
 static inline ffi_type *psi_ffi_decl_arg_type(decl_arg *darg) {
        if (darg->var->pointer_level) {
@@ -100,70 +100,63 @@ static inline ffi_type *psi_ffi_decl_arg_type(decl_arg *darg) {
 typedef struct PSI_LibffiContext {
        ffi_cif signature;
        ffi_type *params[2];
-       struct {
-               struct PSI_LibffiData **list;
-               size_t count;
-       } data;
 } PSI_LibffiContext;
 
-typedef struct PSI_LibffiData {
-       PSI_LibffiContext *context;
-       impl *impl;
-       zend_internal_arg_info *arginfo;
+typedef struct PSI_LibffiCall {
        void *code;
        ffi_closure *closure;
        ffi_cif signature;
-       ffi_type *params[1];
-} PSI_LibffiData;
+       void *params[1]; /* [type1, type2, NULL, arg1, arg2] ... */
+} PSI_LibffiCall;
 
-static inline PSI_LibffiData *PSI_LibffiDataAlloc(PSI_LibffiContext *context, impl *impl) {
-       ffi_status rc;
-       size_t i, c = impl->decl->args ? impl->decl->args->count : 0;
-       PSI_LibffiData *data = malloc(sizeof(*data) + c * sizeof(ffi_type *));
+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 *));
 
-       data->context = context;
-       data->impl = impl;
-       data->arginfo = psi_internal_arginfo(impl);
        for (i = 0; i < c; ++i) {
-               data->params[i] = psi_ffi_decl_arg_type(impl->decl->args->args[i]);
+               call->params[i] = psi_ffi_decl_arg_type(decl->args->args[i]);
        }
-       data->params[c] = NULL;
-
-       rc = ffi_prep_cif(
-                       &data->signature,
-                       psi_ffi_abi(data->impl->decl->abi->convention),
-                       c,
-                       psi_ffi_decl_arg_type(data->impl->decl->func),
-                       data->params);
+       call->params[c] = NULL;
+
+       decl->call.info = call;
+       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);
 
-       data->closure = psi_ffi_closure_alloc(sizeof(ffi_closure), &data->code);
-       ZEND_ASSERT(data->closure != NULL);
+       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(
-                       data->closure,
+                       call->closure,
                        &context->signature,
                        psi_ffi_handler,
-                       data,
-                       data->code);
-       ZEND_ASSERT(FFI_OK == rc);
+                       impl,
+                       call->code);
+
 #elif PSI_HAVE_FFI_PREP_CLOSURE
        rc = ffi_prep_closure(data->code, &context->signature, psi_ffi_handler, data);
-       ZEND_ASSERT(FFI_OK == rc);
 #else
 # error "Neither ffi_prep_closure() nor ffi_prep_closure_loc() available"
 #endif
-
-       context->data.list = realloc(context->data.list, ++context->data.count * sizeof(*context->data.list));
-       context->data.list[context->data.count-1] = data;
-
-       return data;
+       ZEND_ASSERT(FFI_OK == rc);
 }
 
-static inline void PSI_LibffiDataFree(PSI_LibffiData *data) {
-       psi_ffi_closure_free(data->closure);
-       free(data->arginfo);
-       free(data);
+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) {
@@ -182,69 +175,9 @@ static inline PSI_LibffiContext *PSI_LibffiContextInit(PSI_LibffiContext *L) {
        return L;
 }
 
-static inline void PSI_LibffiContextDtor(PSI_LibffiContext *L) {
-       size_t i;
-
-       for (i = 0; i < L->data.count; ++i) {
-               PSI_LibffiDataFree(L->data.list[i]);
-       }
-       if (L->data.list) {
-               free(L->data.list);
-       }
-}
-
-static inline void PSI_LibffiContextFree(PSI_LibffiContext **L) {
-       if (*L) {
-               PSI_LibffiContextDtor(*L);
-               free(*L);
-               *L = NULL;
-       }
-}
-
 static void psi_ffi_handler(ffi_cif *_sig, void *_result, void **_args, void *_data)
 {
-       PSI_LibffiData *data = _data;
-       size_t i;
-       void **arg_prm = NULL;
-       impl_val ret_val;
-
-       if (SUCCESS != psi_parse_args(*(zend_execute_data **)_args[0], data->impl)) {
-               return;
-       }
-
-       if (data->impl->decl->args) {
-               arg_prm = malloc(data->impl->decl->args->count * sizeof(*arg_prm));
-
-               for (i = 0; i < data->impl->decl->args->count; ++i) {
-                       decl_arg *darg = data->impl->decl->args->args[i];
-
-                       arg_prm[i] = psi_do_let(darg);
-               }
-       }
-
-       ffi_call(&data->signature, FFI_FN(data->impl->decl->dlptr), &ret_val, arg_prm);
-
-       psi_do_return(*(zval **)_args[1], data->impl->stmts->ret.list[0], &ret_val);
-
-       for (i = 0; i < data->impl->stmts->set.count; ++i) {
-               set_stmt *set = data->impl->stmts->set.list[i];
-
-               if (set->arg->_zv) {
-                       psi_do_set(set->arg->_zv, set->val);
-               }
-       }
-
-       for (i = 0; i < data->impl->stmts->fre.count; ++i) {
-               free_stmt *fre = data->impl->stmts->fre.list[i];
-
-               psi_do_free(fre);
-       }
-
-       psi_do_clean(data->impl);
-
-       if (arg_prm) {
-               free(arg_prm);
-       }
+       psi_call(*(zend_execute_data **)_args[0], *(zval **)_args[1], _data);
 }
 
 static void psi_ffi_init(PSI_Context *C)
@@ -252,16 +185,23 @@ static void psi_ffi_init(PSI_Context *C)
        C->context = PSI_LibffiContextInit(NULL);
 }
 
-static void psi_ffi_dtor(PSI_Context *C)
-{
-       PSI_LibffiContextFree((void *) &C->context);
+static void psi_ffi_dtor(PSI_Context *C) {
+       size_t i;
+
+       for (i = 0; i < C->decls->count; ++i) {
+               decl *decl = C->decls->list[i];
+
+               if (decl->call.info) {
+                       PSI_LibffiCallFree(decl->call.info);
+               }
+       }
+       free(C->context);
 }
 
 static zend_function_entry *psi_ffi_compile(PSI_Context *C)
 {
        size_t i, j = 0;
        zend_function_entry *zfe;
-       PSI_LibffiContext *ctx = C->context;
 
        if (!C->impls) {
                return NULL;
@@ -270,46 +210,40 @@ static zend_function_entry *psi_ffi_compile(PSI_Context *C)
        zfe = calloc(C->impls->count + 1, sizeof(*zfe));
        for (i = 0; i < C->impls->count; ++i) {
                zend_function_entry *zf = &zfe[j];
-               PSI_LibffiData *data;
+               PSI_LibffiCall *call;
+               impl *impl = C->impls->list[i];
 
-               if (!C->impls->list[i]->decl) {
+               if (!impl->decl) {
                        continue;
                }
 
-               data = PSI_LibffiDataAlloc(ctx, C->impls->list[i]);
-               zf->fname = C->impls->list[i]->func->name + (C->impls->list[i]->func->name[0] == '\\');
-               zf->num_args = C->impls->list[i]->func->args->count;
-               zf->handler = data->code;
-               zf->arg_info = data->arginfo;
+               call = PSI_LibffiCallAlloc(C, impl->decl);
+               PSI_LibffiCallInitClosure(C, call, impl);
+
+               zf->fname = impl->func->name + (impl->func->name[0] == '\\');
+               zf->num_args = impl->func->args->count;
+               zf->handler = call->code;
+               zf->arg_info = psi_internal_arginfo(impl);
                ++j;
        }
 
-       return zfe;
-}
+       for (i = 0; i < C->decls->count; ++i) {
+               decl *decl = C->decls->list[i];
 
-static void psi_ffi_call(PSI_Context *C, impl_val *ret_val, decl *decl, impl_val **args) {
-       ffi_cif signature;
-       ffi_type **types;
-       void **argps;
-       size_t i;
-
-       /* FIXME: cache in decl */
-
-       types = calloc(decl->args->count + 1, sizeof(*types));
-       argps = calloc(decl->args->count + 1, sizeof(*argps));
-       for (i = 0; i < decl->args->count; ++i) {
-               decl_arg *darg = decl->args->args[i];
+               if (decl->impl) {
+                       continue;
+               }
 
-               types[i] = psi_ffi_decl_arg_type(darg);
-               argps[i] = args[i];
+               PSI_LibffiCallAlloc(C, decl);
        }
 
-       ffi_prep_cif(&signature, psi_ffi_abi(decl->abi->convention), decl->args->count,
-                       psi_ffi_decl_arg_type(decl->func), types);
-       ffi_call(&signature, FFI_FN(decl->dlptr), ret_val, argps);
+       return zfe;
+}
+
+static void psi_ffi_call(PSI_Context *C, impl_val *ret_val, decl *decl) {
+       PSI_LibffiCall *call = decl->call.info;
 
-       free(types);
-       free(argps);
+       ffi_call(&call->signature, FFI_FN(decl->call.sym), ret_val, decl->call.args);
 }
 
 static PSI_ContextOps ops = {
index e1d6cf866252f9bd8ae07d2685bc73e0a786fc61..4866809392aafea74425e6f7d9e36c812b2f67f7 100644 (file)
@@ -9,7 +9,7 @@ static void psi_jit_handler(jit_type_t _sig, void *result, void **_args, void *_
 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) {
+static inline jit_type_t psi_jit_token_type(token_t t) {
        switch (t) {
        default:
                ZEND_ASSERT(0);
@@ -43,7 +43,7 @@ static inline jit_type_t psi_jit_type(token_t t) {
        }
 }
 static inline jit_type_t psi_jit_decl_type(decl_type *type) {
-       return psi_jit_type(real_decl_type(type)->type);
+       return psi_jit_token_type(real_decl_type(type)->type);
 }
 static inline jit_type_t psi_jit_decl_arg_type(decl_arg *darg) {
        if (darg->var->pointer_level) {
@@ -62,44 +62,45 @@ typedef struct PSI_LibjitContext {
        } data;
 } PSI_LibjitContext;
 
+typedef struct PSI_LibjitCall {
+       void *closure;
+       jit_type_t signature;
+       jit_type_t params[1]; /* [type1, type2, NULL, arg1, arg2] ... */
+} PSI_LibjitCall;
+
 typedef struct PSI_LibjitData {
        PSI_LibjitContext *context;
        impl *impl;
        zend_internal_arg_info *arginfo;
-       void *closure;
-       jit_type_t signature;
-       jit_type_t params[1];
 } PSI_LibjitData;
 
-static inline PSI_LibjitData *PSI_LibjitDataAlloc(PSI_LibjitContext *context, impl *impl) {
-       size_t i, c = impl->decl->args->count;
-       PSI_LibjitData *data = malloc(sizeof(*data) + (c ? c-1 : c) * sizeof(jit_type_t));
+static inline PSI_LibjitCall *PSI_LibjitCallAlloc(PSI_Context *C, decl *decl) {
+       size_t i, c = decl->args ? decl->args->count : 0;
+       PSI_LibjitCall *call = calloc(1, sizeof(*call) + 2 * c * sizeof(void *));
 
-       data->context = context;
-       data->impl = impl;
-       data->arginfo = psi_internal_arginfo(impl);
        for (i = 0; i < c; ++i) {
-               data->params[i] = psi_jit_decl_arg_type(impl->decl->args->args[i]);
+               call->params[i] = psi_jit_decl_arg_type(decl->args->args[i]);
        }
+       call->params[c] = NULL;
 
-       data->signature = jit_type_create_signature(
-               psi_jit_abi(data->impl->decl->abi->convention),
-               psi_jit_decl_arg_type(data->impl->decl->func),
-               data->params,
-               data->impl->decl->args->count,
-               1);
-       data->closure = jit_closure_create(context->jit, context->signature, &psi_jit_handler, data);
+       decl->call.info = call;
+       decl->call.args = (void **) &call->params[c+1];
 
-       context->data.list = realloc(context->data.list, ++context->data.count * sizeof(*context->data.list));
-       context->data.list[context->data.count-1] = data;
+       call->signature = jit_type_create_signature(
+                       psi_jit_abi(decl->abi->convention),
+                       psi_jit_decl_arg_type(decl->func),
+                       call->params, c, 1);
+       return call;
+}
 
-       return data;
+static inline void PSI_LibjitCallInitClosure(PSI_Context *C, PSI_LibjitCall *call, impl *impl) {
+       PSI_LibjitContext *context = C->context;
+       call->closure = jit_closure_create(context->jit, context->signature,
+                       &psi_jit_handler, impl);
 }
 
-static inline void PSI_LibjitDataFree(PSI_LibjitData *data) {
-       free(data->arginfo);
-       jit_type_free(data->signature);
-       free(data);
+static inline void PSI_LibjitCallFree(PSI_LibjitCall *call) {
+       jit_type_free(call->signature);
 }
 
 static inline PSI_LibjitContext *PSI_LibjitContextInit(PSI_LibjitContext *L) {
@@ -114,20 +115,13 @@ static inline PSI_LibjitContext *PSI_LibjitContextInit(PSI_LibjitContext *L) {
        memset(L, 0, sizeof(*L));
 
        L->jit = jit_context_create();
-       L->signature = jit_type_create_signature(jit_abi_cdecl, jit_type_void, params, 2, 1);
+       L->signature = jit_type_create_signature(jit_abi_cdecl, jit_type_void,
+                       params, 2, 1);
 
        return L;
 }
 
 static inline void PSI_LibjitContextDtor(PSI_LibjitContext *L) {
-       size_t i;
-
-       for (i = 0; i < L->data.count; ++i) {
-               PSI_LibjitDataFree(L->data.list[i]);
-       }
-       if (L->data.list) {
-               free(L->data.list);
-       }
        jit_type_free(L->signature);
        jit_context_destroy(L->jit);
 }
@@ -142,48 +136,7 @@ static inline void PSI_LibjitContextFree(PSI_LibjitContext **L) {
 
 static void psi_jit_handler(jit_type_t _sig, void *result, void **_args, void *_data)
 {
-       PSI_LibjitData *data = _data;
-       size_t i;
-       void **arg_prm = NULL;
-       impl_val ret_val;
-
-       if (SUCCESS != psi_parse_args(*(zend_execute_data **)_args[0], data->impl)) {
-               return;
-       }
-
-       if (data->impl->decl->args->count) {
-               arg_prm = malloc(data->impl->decl->args->count * sizeof(*arg_prm));
-
-               for (i = 0; i < data->impl->decl->args->count; ++i) {
-                       decl_arg *darg = data->impl->decl->args->args[i];
-
-                       arg_prm[i] = psi_do_let(darg);
-               }
-       }
-
-       jit_apply(data->signature, data->impl->decl->dlptr, arg_prm, data->impl->decl->args->count, &ret_val);
-
-       psi_do_return(*(zval **)_args[1], data->impl->stmts->ret.list[0], &ret_val);
-
-       for (i = 0; i < data->impl->stmts->set.count; ++i) {
-               set_stmt *set = data->impl->stmts->set.list[i];
-
-               if (set->arg->_zv) {
-                       psi_do_set(set->arg->_zv, set->val);
-               }
-       }
-
-       for (i = 0; i < data->impl->stmts->fre.count; ++i) {
-               free_stmt *fre = data->impl->stmts->fre.list[i];
-
-               psi_do_free(fre);
-       }
-
-       psi_do_clean(data->impl);
-
-       if (arg_prm) {
-               free(arg_prm);
-       }
+       psi_call(*(zend_execute_data **)_args[0], *(zval **)_args[1], _data);
 }
 
 static void psi_jit_init(PSI_Context *C)
@@ -193,6 +146,13 @@ static void psi_jit_init(PSI_Context *C)
 
 static void psi_jit_dtor(PSI_Context *C)
 {
+       size_t i;
+
+       for (i = 0; i < C->decls->count; ++i) {
+               decl *decl = C->decls->list[i];
+
+               PSI_LibjitCallFree(decl->call.info);
+       }
        PSI_LibjitContextFree((void *) &C->context);
 }
 
@@ -211,51 +171,43 @@ static zend_function_entry *psi_jit_compile(PSI_Context *C)
 
        for (i = 0; i < C->impls->count; ++i) {
                zend_function_entry *zf = &zfe[j];
-               PSI_LibjitData *data;
+               PSI_LibjitCall *call;
+               impl *impl = C->impls->list[i];
 
-               if (!C->impls->list[i]->decl) {
+               if (!impl->decl) {
                        continue;
                }
 
-               data = PSI_LibjitDataAlloc(ctx, C->impls->list[i]);
-               zf->fname = C->impls->list[i]->func->name + (C->impls->list[i]->func->name[0] == '\\');
-               zf->num_args = C->impls->list[i]->func->args->count;
-               zf->handler = data->closure;
-               zf->arg_info = data->arginfo;
+               call = PSI_LibjitCallAlloc(C, impl->decl);
+               PSI_LibjitCallInitClosure(C, call, impl);
+
+               zf->fname = impl->func->name + (impl->func->name[0] == '\\');
+               zf->num_args = impl->func->args->count;
+               zf->handler = call->closure;
+               zf->arg_info = psi_internal_arginfo(impl);
                ++j;
        }
 
-       jit_context_build_end(ctx->jit);
-
-       return zfe;
-}
-
-static void psi_jit_call(PSI_Context *C, impl_val *ret_val, decl *decl, impl_val **args) {
-       jit_type_t signature, *types;
-       void **argps;
-       size_t i;
+       for (i = 0; i < C->decls->count; ++i) {
+               decl *decl = C->decls->list[i];
 
-       /* FIXME: cache in decl */
-
-       types = calloc(decl->args->count + 1, sizeof(*types));
-       argps = calloc(decl->args->count + 1, sizeof(*argps));
-       for (i = 0; i < decl->args->count; ++i) {
-               decl_arg *darg = decl->args->args[i];
+               if (decl->impl) {
+                       continue;
+               }
 
-               types[i] = psi_jit_decl_arg_type(darg);
-               argps[i] = args[i];
+               PSI_LibjitCallAlloc(C, decl);
        }
 
-       signature = jit_type_create_signature(
-                       psi_jit_abi(decl->abi->convention),
-                       psi_jit_decl_arg_type(decl->func),
-                       types, decl->args->count, 1);
-       jit_apply(signature, decl->dlptr, argps, decl->args->count, ret_val);
+       jit_context_build_end(ctx->jit);
+
+       return zfe;
+}
 
-       jit_type_free(signature);
-       free(types);
-       free(argps);
+static void psi_jit_call(PSI_Context *C, impl_val *ret_val, decl *decl) {
+       PSI_LibjitCall *call = decl->call.info;
 
+       jit_apply(call->signature, decl->call.sym, decl->call.args,
+                       decl->args->count, ret_val);
 }
 
 static PSI_ContextOps ops = {
index 811d4e40429c713b3619b065fd9776f082e00ae5..8ab22ae1b2133e847c9d43866beb8f195ec3cc14 100644 (file)
@@ -439,7 +439,7 @@ void psi_to_array(zval *return_value, token_t t, impl_val *ret_val, set_value *s
        }
 }
 
-ZEND_RESULT_CODE psi_parse_args(zend_execute_data *execute_data, impl *impl)
+static inline ZEND_RESULT_CODE psi_parse_args(zend_execute_data *execute_data, impl *impl)
 {
        impl_arg *iarg;
 
@@ -497,7 +497,7 @@ ZEND_RESULT_CODE psi_parse_args(zend_execute_data *execute_data, impl *impl)
        return SUCCESS;
 }
 
-void *psi_do_calloc(let_calloc *alloc)
+static inline void *psi_do_calloc(let_calloc *alloc)
 {
        decl_type *type = real_decl_type(alloc->type);
        size_t size;
@@ -512,7 +512,7 @@ void *psi_do_calloc(let_calloc *alloc)
        return ecalloc(alloc->n, size);
 }
 
-void *psi_do_let(decl_arg *darg)
+static inline void *psi_do_let(decl_arg *darg)
 {
        impl_arg *iarg = darg->let->arg;
        impl_val *arg_val;
@@ -596,7 +596,7 @@ void *psi_do_let(decl_arg *darg)
        }
 }
 
-void psi_do_set(zval *return_value, set_value *set)
+static inline void psi_do_set(zval *return_value, set_value *set)
 {
        impl_val *val = (impl_val *) &set->vars->vars[0]->arg->let->ptr;
        token_t t = real_decl_type(set->vars->vars[0]->arg->type)->type;
@@ -607,17 +607,17 @@ void psi_do_set(zval *return_value, set_value *set)
        set->func->handler(return_value, t, val, set, set->vars->vars[0]);
 }
 
-void psi_do_return(zval *return_value, return_stmt *ret, impl_val *ret_val)
+static inline void psi_do_return(zval *return_value, return_stmt *ret, impl_val *ret_val)
 {
        token_t t = real_decl_type(ret->decl->type)->type;
 
        ret->set->func->handler(return_value, t, ret_val, ret->set, ret->decl->var);
 }
 
-void psi_do_free(free_stmt *fre)
+static inline void psi_do_free(free_stmt *fre)
 {
        size_t i, j;
-       impl_val dummy, *argps[0x20];
+       impl_val dummy;
 
        for (i = 0; i < fre->calls->count; ++i) {
                free_call *f = fre->calls->list[i];
@@ -626,14 +626,14 @@ void psi_do_free(free_stmt *fre)
                        decl_var *dvar = f->vars->vars[j];
                        decl_arg *darg = dvar->arg;
 
-                       argps[j] = &darg->let->out;
+                       f->decl->call.args[j] = &darg->let->out;
                }
 
-               PSI_ContextCall(&PSI_G(context), &dummy, f->decl, argps);
+               PSI_ContextCall(&PSI_G(context), &dummy, f->decl);
        }
 }
 
-void psi_do_clean(impl *impl)
+static inline void psi_do_clean(impl *impl)
 {
        size_t i;
 
@@ -668,6 +668,44 @@ void psi_do_clean(impl *impl)
        }
 }
 
+void psi_call(zend_execute_data *execute_data, zval *return_value, impl *impl)
+{
+       impl_val ret_val;
+       size_t i;
+
+       if (SUCCESS != psi_parse_args(execute_data, impl)) {
+               return;
+       }
+
+       if (impl->decl->args) {
+               for (i = 0; i < impl->decl->args->count; ++i) {
+                       decl_arg *darg = impl->decl->args->args[i];
+
+                       impl->decl->call.args[i] = psi_do_let(darg);
+               }
+       }
+
+       PSI_ContextCall(&PSI_G(context), &ret_val, impl->decl);
+
+       psi_do_return(return_value, impl->stmts->ret.list[0], &ret_val);
+
+       for (i = 0; i < impl->stmts->set.count; ++i) {
+               set_stmt *set = impl->stmts->set.list[i];
+
+               if (set->arg->_zv) {
+                       psi_do_set(set->arg->_zv, set->val);
+               }
+       }
+
+       for (i = 0; i < impl->stmts->fre.count; ++i) {
+               free_stmt *fre = impl->stmts->fre.list[i];
+
+               psi_do_free(fre);
+       }
+
+       psi_do_clean(impl);
+}
+
 PHP_MINIT_FUNCTION(psi)
 {
        PSI_ContextOps *ops;
index 67252e24855b9aa5cae8c0a68c01def1e2587d3b..d8955e56d2294394b996f260bc98b040d7ea59fe 100644 (file)
@@ -234,7 +234,12 @@ typedef struct decl {
        decl_abi *abi;
        decl_arg *func;
        decl_args *args;
-       void *dlptr;
+       struct impl *impl;
+       struct {
+               void *sym;
+               void *info;
+               void **args;
+       } call;
 } decl;
 
 static inline decl* init_decl(decl_abi *abi, decl_arg *func, decl_args *args) {
@@ -359,7 +364,7 @@ static inline impl_val *enref_impl_val(void *ptr, decl_var *var) {
        if (!var->pointer_level && real_decl_type(var->arg->type)->type != PSI_T_STRUCT) {
                return ptr;
        }
-       val = val_ptr = calloc(var->pointer_level, sizeof(void *));
+       val = val_ptr = calloc(var->pointer_level + 1, sizeof(void *));
        for (i = 1; i < var->pointer_level; ++i) {
                val_ptr->ptr = (void **) val_ptr + 1;
                val_ptr = val_ptr->ptr;
diff --git a/tests/stat/stat.psi b/tests/stat/stat.psi
deleted file mode 100644 (file)
index e5d8eba..0000000
+++ /dev/null
@@ -1,48 +0,0 @@
-extern int stat(char *path, struct stat *buf);
-function psi\stat(string $path, array &$buf = NULL) : int {
-       let path = strval($path);
-       let buf = calloc(1, struct stat);
-       return to_int(stat);
-       set $buf = to_array(*buf,
-               to_int(st_dev),
-               to_int(st_ino),
-               to_int(st_mode),
-               to_int(st_nlink),
-               to_int(st_uid),
-               to_int(st_gid),
-               to_int(st_rdev),
-               to_int(st_size),
-               to_array(st_atim,
-                       to_int(tv_sec),
-                       to_int(tv_nsec)
-               ), 
-               to_array(st_atimespec,
-                       to_int(tv_sec),
-                       to_int(tv_nsec)
-               ),
-               to_array(st_mtim,
-                       to_int(tv_sec),
-                       to_int(tv_nsec)
-               ), 
-               to_array(st_mtimespec,
-                       to_int(tv_sec),
-                       to_int(tv_nsec)
-               ),
-               to_array(st_ctim,
-                       to_int(tv_sec),
-                       to_int(tv_nsec)
-               ), 
-               to_array(st_ctimespec,
-                       to_int(tv_sec),
-                       to_int(tv_nsec)
-               ),
-               to_array(st_birthtimespec,
-                       to_int(tv_sec),
-                       to_int(tv_nsec)
-               ),
-               to_int(st_blksize),
-               to_int(st_blocks),
-               to_int(st_flags),
-               to_int(st_gen)
-       );
-}
index c4b4b968892e918f62251fbf1ae5ad642f0c8191..3b180f92e881a09c9d25c4a8339c3f6adb12e677 100644 (file)
@@ -1,7 +1,7 @@
 --TEST--
 stat
 --INI--
-psi.directory={PWD}
+psi.directory={PWD}/../../psis:{PWD}
 --SKIPIF--
 <?php 
 extension_loaded("psi") or die("skip - need ext/psi");
diff --git a/tests/time/time.psi b/tests/time/time.psi
deleted file mode 100644 (file)
index e491185..0000000
+++ /dev/null
@@ -1,81 +0,0 @@
-extern int gettimeofday(struct timeval *tv, struct timezone *tz);
-function psi\gettimeofday(array &$tv = NULL, array &$tz = NULL) : int {
-       let tv = calloc(1, struct timeval);
-       let tz = calloc(1, struct timezone);
-       return to_int(gettimeofday);
-       set $tv = to_array(*tv, 
-               to_int(tv_sec), 
-               to_int(tv_usec));
-       set $tz = to_array(*tz, 
-               to_int(tz_minuteswest), 
-               to_int(tz_dsttime));
-}
-
-extern char *asctime(struct tm *tm);
-function psi\asctime(array $tm = NULL) : string {
-       let tm = arrval($tm);
-       return to_string(asctime);
-}
-
-extern char *asctime_r(struct tm *tm, char *buf);
-function psi\asctime_r(array $tm = NULL) : string {
-       let tm = arrval($tm);
-       let buf = calloc(32, char);
-       return to_string(asctime_r);
-}
-
-extern struct tm *gmtime(time_t *tp);
-function psi\gmtime(int $ts) : array {
-       let tp = &intval($ts);
-       return to_array(*gmtime,
-               to_int(tm_sec),
-               to_int(tm_min),
-               to_int(tm_hour),
-               to_int(tm_mday),
-               to_int(tm_mon),
-               to_int(tm_year),
-               to_int(tm_wday),
-               to_int(tm_yday),
-               to_int(tm_isdst)
-       );
-}
-
-extern struct tm *gmtime_r(time_t *tp, struct tm *buf);
-function psi\gmtime_r(int $ts) : array {
-       let tp = &intval($ts);
-       let buf = calloc(1, struct tm);
-       return to_array(*gmtime_r,
-               to_int(tm_sec),
-               to_int(tm_min),
-               to_int(tm_hour),
-               to_int(tm_mday),
-               to_int(tm_mon),
-               to_int(tm_year),
-               to_int(tm_wday),
-               to_int(tm_yday),
-               to_int(tm_isdst)
-       );
-}
-
-extern int nanosleep(struct timespec *rqtp, struct timespec *rmtp);
-function psi\nanosleep(array $rq = NULL, array &$rm = NULL) : int {
-       let rqtp = arrval($rq);
-       let rmtp = calloc(1, struct timespec);
-       return to_int(nanosleep);
-       set $rm = to_array(*rmtp,
-               to_int(tv_sec),
-               to_int(tv_nsec)
-       );
-}
-
-extern clock_t times(struct tms *buf);
-function psi\times(array &$tms = NULL) : int {
-       let buf = calloc(1, struct tms);
-       return to_int(times);
-       set $tms = to_array(*buf,
-               to_int(tms_utime),
-               to_int(tms_stime),
-               to_int(tms_cutime),
-               to_int(tms_cstime)
-       );
-}
index b3728368997126935469e640a77aaf289a525c95..e6e53843c1a56500e2e2037f8a4ade163c3efc23 100644 (file)
@@ -1,7 +1,7 @@
 --TEST--
 gettimeofday
 --INI--
-psi.directory = {PWD}
+psi.directory = {PWD}/../../psis:{PWD}
 --SKIPIF--
 <?php
 extension_loaded("psi") or die("skip - need ext/psi");
index 2bf43228078c5f7069f29a966b9b299dd2923659..f5f93875f97c1d3ddede3dea4d06c4c971857dca 100644 (file)
@@ -7,7 +7,7 @@ extension_loaded("psi") or die("skip - need ext/psi");
 --ENV--
 TZ=UTC
 --INI--
-psi.directory = {PWD}
+psi.directory = {PWD}/../../psis:{PWD}
 --FILE--
 ===TEST===
 <?php
index ec863bfde15ec2b2abf004e0546c654446e5b7c9..a7c1536569f07d68e092abfde3bf2cfdae3308aa 100644 (file)
@@ -7,7 +7,7 @@ extension_loaded("psi") or die("skip - need ext/psi");
 --ENV--
 TZ=UTC
 --INI--
-psi.directory = {PWD}
+psi.directory = {PWD}/../../psis:{PWD}
 --FILE--
 ===TEST===
 <?php
index 678d4bc70716a3c9c17b0e9bca2da2e770095953..15c641ca0bb68a72f94f239ce92fea344e882796 100644 (file)
@@ -1,7 +1,7 @@
 --TEST--
 times
 --INI--
-psi.directory={PWD}
+psi.directory={PWD}/../../psis:{PWD}
 --SKIPIF--
 <?php
 extension_loaded("psi") or die("skip - need ext/psi");
diff --git a/tests/uname/uname.psi b/tests/uname/uname.psi
deleted file mode 100644 (file)
index 25abfcb..0000000
+++ /dev/null
@@ -1,12 +0,0 @@
-extern int uname(struct utsname *u);
-function psi\uname(array &$u = NULL) : int {
-       let u = calloc(1, struct utsname);
-       return to_int(uname);
-       set $u = to_array(*u,
-               to_string(sysname),
-               to_string(nodename),
-               to_string(release),
-               to_string(version), 
-               to_string(machine),
-               to_string(domainname));
-}
\ No newline at end of file
index 7f46723aadada634a47749a0b7db8a016dd8590b..3d4d380d9d3720d1fa6251c36e8179e502aabce3 100644 (file)
@@ -1,7 +1,7 @@
 --TEST--
 uname
 --INI--
-psi.directory={PWD}
+psi.directory={PWD}/../../psis:{PWD}
 --SKIPIF--
 <?php
 extension_loaded("psi") or die("skip - need ext/psi");