missing m4
[m6w6/ext-psi] / src / libffi.c
index bb1f34f7cd536ed9d72acb5afce08144191f3569..d59712cd9a595abe30307e43e4fa85a4a5a8573b 100644 (file)
@@ -56,6 +56,7 @@ 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);
 
 static inline ffi_abi psi_ffi_abi(const char *convention) {
        return FFI_DEFAULT_ABI;
@@ -87,14 +88,114 @@ static inline ffi_type *psi_ffi_token_type(token_t t) {
                return &ffi_type_uchar;
        case PSI_T_INT:
                return &ffi_type_sint;
+       case PSI_T_LONG:
+               return &ffi_type_slong;
        case PSI_T_FLOAT:
                return &ffi_type_float;
        case PSI_T_DOUBLE:
                return &ffi_type_double;
+       case PSI_T_POINTER:
+               return &ffi_type_pointer;
+       }
+}
+static inline ffi_type *psi_ffi_impl_type(token_t impl_type) {
+       switch (impl_type) {
+       case PSI_T_BOOL:
+               return &ffi_type_sint8;
+       case PSI_T_INT:
+               return &ffi_type_sint64;
+       case PSI_T_STRING:
+               return &ffi_type_pointer;
+       case PSI_T_FLOAT:
+       case PSI_T_DOUBLE:
+               return &ffi_type_double;
+       EMPTY_SWITCH_DEFAULT_CASE();
+       }
+       return NULL;
+}
+static void psi_ffi_struct_type_dtor(void *type) {
+       ffi_type *strct = type;
+
+       if (strct->elements) {
+               ffi_type **ptr;
+
+               for (ptr = strct->elements; *ptr; ++ptr) {
+                       free(*ptr);
+               }
+               free(strct->elements);
+       }
+       free(strct);
+}
+
+static ffi_type **psi_ffi_struct_type_elements(decl_struct *strct) {
+       size_t i, j, argc = strct->args->count << 2, nels = 0, offset = 0, align, padding;
+       ffi_type **els = calloc(argc + 1, sizeof(*els));
+
+       for (i = 0; i < strct->args->count; ++i) {
+               decl_arg *darg = strct->args->args[i];
+               ffi_type *type = malloc(sizeof(*type));
+
+               memcpy(type, psi_ffi_decl_arg_type(darg), sizeof(*type));
+
+               if (darg->layout->pos > offset) {
+                       padding = darg->layout->pos - offset;
+                       align = ((padding - 1) | (type->alignment - 1)) + 1;
+                       if (align >= padding) {
+                               padding = 0;
+                       }
+               } else {
+                       align = 0;
+                       padding = 0;
+               }
+
+               if (padding) {
+                       for (j = 0; j < padding; ++j) {
+                               ffi_type *pad = malloc(sizeof(*pad));
+
+                               ZEND_ASSERT(nels + 1 < argc);
+                               memcpy(pad, &ffi_type_schar, sizeof(*pad));
+                               els[nels++] = pad;
+                       }
+               }
+
+               ZEND_ASSERT(nels + 1 < argc);
+               els[nels++] = type;
+//fprintf(stderr, "%s o:%d, a:%d, p:%d l:%d\n", darg->var->name, offset, align, padding, darg->layout->len);
+               offset += MAX(align, padding) + darg->layout->len;
        }
+//fprintf(stderr, "%s s:%d o=%d\n", strct->name, strct->size, offset);
+       ZEND_ASSERT(offset <= strct->size);
+       if (offset < strct->size) {
+               padding = strct->size - offset;
+               for (j = 0; j < padding; ++j) {
+                       ffi_type *pad = malloc(sizeof(*pad));
+
+                       ZEND_ASSERT(nels + 1 < argc);
+                       memcpy(pad, &ffi_type_schar, sizeof(*pad));
+                       els[nels++] = pad;
+               }
+       }
+
+       return els;
 }
 static inline ffi_type *psi_ffi_decl_type(decl_type *type) {
-       return psi_ffi_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) {
+                       ffi_type *strct = calloc(1, sizeof(ffi_type));
+
+                       strct->type = FFI_TYPE_STRUCT;
+                       strct->size = 0;//real->strct->size;
+                       strct->elements = psi_ffi_struct_type_elements(real->strct);
+
+                       real->strct->engine.type = strct;
+                       real->strct->engine.dtor = psi_ffi_struct_type_dtor;
+               }
+
+               return real->strct->engine.type;
+       }
+       return psi_ffi_token_type(real->type);
 }
 static inline ffi_type *psi_ffi_decl_arg_type(decl_arg *darg) {
        if (darg->var->pointer_level) {
@@ -127,6 +228,8 @@ static inline PSI_LibffiCall *PSI_LibffiCallAlloc(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];
 
        rc = ffi_prep_cif(&call->signature, psi_ffi_abi(decl->abi->convention),
@@ -152,7 +255,7 @@ static inline void PSI_LibffiCallInitClosure(PSI_Context *C, PSI_LibffiCall *cal
                        call->code);
 
 #elif PSI_HAVE_FFI_PREP_CLOSURE
-       rc = ffi_prep_closure(data->code, &context->signature, psi_ffi_handler, data);
+       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
@@ -192,14 +295,17 @@ static void psi_ffi_init(PSI_Context *C)
        C->context = PSI_LibffiContextInit(NULL);
 }
 
-static void psi_ffi_dtor(PSI_Context *C) {
-       size_t i;
+static void psi_ffi_dtor(PSI_Context *C)
+{
+       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];
 
-               if (decl->call.info) {
-                       PSI_LibffiCallFree(decl->call.info);
+                       if (decl->call.info) {
+                               PSI_LibffiCallFree(decl->call.info);
+                       }
                }
        }
        free(C->context);
@@ -247,10 +353,38 @@ static zend_function_entry *psi_ffi_compile(PSI_Context *C)
        return zfe;
 }
 
-static void psi_ffi_call(PSI_Context *C, impl_val *ret_val, decl *decl) {
-       PSI_LibffiCall *call = decl->call.info;
+static void psi_ffi_call(PSI_Context *C, decl_callinfo *decl_call, impl_vararg *va) {
+       PSI_LibffiCall *call = decl_call->info;
+
+       if (va) {
+               ffi_status rc;
+               ffi_cif signature;
+               size_t i, nfixedargs = decl_call->argc, ntotalargs = nfixedargs + va->args->count;
+               void **params = calloc(2 * ntotalargs + 2, sizeof(void *));
 
-       ffi_call(&call->signature, FFI_FN(decl->call.sym), ret_val, decl->call.args);
+               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_ffi_impl_type(va->types[i]);
+                       params[nfixedargs + i + ntotalargs + 1] = &va->values[i];
+               }
+#ifdef PSI_HAVE_FFI_PREP_CIF_VAR
+               rc = ffi_prep_cif_var(&signature, call->signature.abi,
+                               nfixedargs, ntotalargs,
+                               call->signature.rtype, (ffi_type **) params);
+#else
+               /* FIXME: test in config.m4; assume we can just call anyway */
+               rc = ffi_prep_cif(&signature, call->signature.abi, ntotalargs,
+                               call->signature.rtype, (ffi_type **) params);
+#endif
+               ZEND_ASSERT(FFI_OK == rc);
+               ffi_call(&signature, FFI_FN(decl_call->sym), *decl_call->rval, &params[ntotalargs + 1]);
+               free(params);
+       } else {
+               ffi_call(&call->signature, FFI_FN(decl_call->sym), *decl_call->rval, decl_call->args);
+       }
 }
 
 static PSI_ContextOps ops = {