flush
[m6w6/ext-psi] / src / libjit.c
index 736b54dbbbd67e7a2a61b12b44b9cba7934d90b3..8a766f33ca2a8d59c801e9cd35f28c3ebd8a7923 100644 (file)
@@ -1,4 +1,11 @@
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
 #include "php.h"
+
+#ifdef HAVE_LIBJIT
+
 #include "php_psi.h"
 #include "libjit.h"
 
@@ -38,6 +45,20 @@ static inline jit_type_t psi_jit_token_type(token_t t) {
        EMPTY_SWITCH_DEFAULT_CASE();
        }
 }
+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();
+       }
+}
 static inline jit_type_t psi_jit_decl_type(decl_type *type) {
        return psi_jit_token_type(real_decl_type(type)->type);
 }
@@ -61,7 +82,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 {
@@ -80,12 +101,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;
 }
 
@@ -97,6 +120,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) {
@@ -142,12 +166,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);
 }
@@ -199,11 +225,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 *));
+
+               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(call->signature, decl->call.sym, decl->call.args,
-                       decl->args->count, ret_val);
+               jit_apply(signature, decl_call->sym, &params[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 = {
@@ -217,3 +269,5 @@ PSI_ContextOps *PSI_Libjit(void)
 {
        return &ops;
 }
+
+#endif /* HAVE_LIBJIT */