flush
[m6w6/ext-psi] / src / libffi.c
index ac018a119640d8ea0deed03ba479cf1e6c18a260..301995e5903d9bc377c3d2630f1be48fed9e0513 100644 (file)
@@ -8,6 +8,7 @@
 
 #include "php_psi.h"
 #include "libffi.h"
+#include "engine.h"
 
 #undef PACKAGE
 #undef PACKAGE_BUGREPORT
@@ -56,6 +57,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;
@@ -86,11 +88,18 @@ static inline ffi_type *psi_ffi_token_type(token_t t) {
        case PSI_T_BOOL:
                return &ffi_type_uchar;
        case PSI_T_INT:
+       case PSI_T_ENUM:
                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;
+#ifdef HAVE_LONG_DOUBLE
+       case PSI_T_LONG_DOUBLE:
+               return &ffi_type_longdouble;
+#endif
        case PSI_T_POINTER:
                return &ffi_type_pointer;
        }
@@ -108,37 +117,88 @@ static inline ffi_type *psi_ffi_impl_type(token_t impl_type) {
                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 inline ffi_type *psi_ffi_decl_arg_type(decl_arg *darg);
-static ffi_type **psi_ffi_struct_type_elements(decl_struct *strct) {
-       ffi_type **els = calloc(strct->args->count + 1, sizeof(*els));
+
+static size_t psi_ffi_struct_type_pad(ffi_type **els, size_t padding) {
        size_t i;
 
+       for (i = 0; i < padding; ++i) {
+               ffi_type *pad = malloc(sizeof(*pad));
+
+               memcpy(pad, &ffi_type_schar, sizeof(*pad));
+               *els++ = pad;
+       }
+
+       return padding;
+}
+
+static ffi_type **psi_ffi_struct_type_elements(decl_struct *strct) {
+       size_t i, argc = strct->args->count, nels = 0, offset = 0, maxalign = 0;
+       ffi_type **els = calloc(argc + 1, sizeof(*els));
+
        for (i = 0; i < strct->args->count; ++i) {
-               els[i] = psi_ffi_decl_arg_type(strct->args->args[i]);
+               decl_arg *darg = strct->args->args[i];
+               ffi_type *type = malloc(sizeof(*type));
+               size_t padding;
+
+               memcpy(type, psi_ffi_decl_arg_type(darg), sizeof(*type));
+
+               ZEND_ASSERT(type->size == darg->layout->len);
+
+               if (type->alignment > maxalign) {
+                       maxalign = type->alignment;
+               }
+
+               if ((padding = psi_offset_padding(darg->layout->pos - offset, type->alignment))) {
+                       if (nels + padding + 1 > argc) {
+                               argc += padding;
+                               els = realloc(els, (argc + 1) * sizeof(*els));
+                               els[argc] = NULL;
+                       }
+                       psi_ffi_struct_type_pad(&els[nels], padding);
+                       nels += padding;
+                       offset += padding;
+               }
+               ZEND_ASSERT(offset == darg->layout->pos);
+
+               offset = (offset + darg->layout->len + type->alignment - 1) & ~(type->alignment - 1);
+               els[nels++] = type;
+       }
+
+       /* apply struct alignment padding */
+       offset = (offset + maxalign - 1) & ~(maxalign - 1);
+
+       ZEND_ASSERT(offset <= strct->size);
+       if (offset < strct->size) {
+               psi_ffi_struct_type_pad(&els[nels], strct->size - offset);
        }
-       els[i] = NULL;
 
        return els;
 }
 static inline ffi_type *psi_ffi_decl_type(decl_type *type) {
        decl_type *real = real_decl_type(type);
 
-       if (real->type == PSI_T_STRUCT) {
+       switch (real->type) {
+       case PSI_T_STRUCT:
                if (!real->strct->engine.type) {
                        ffi_type *strct = calloc(1, sizeof(ffi_type));
 
                        strct->type = FFI_TYPE_STRUCT;
-                       strct->size = real->strct->size;
-                       strct->alignment = 0;
+                       strct->size = 0;
                        strct->elements = psi_ffi_struct_type_elements(real->strct);
 
                        real->strct->engine.type = strct;
@@ -146,8 +206,13 @@ static inline ffi_type *psi_ffi_decl_type(decl_type *type) {
                }
 
                return real->strct->engine.type;
+
+       case PSI_T_UNION:
+               return psi_ffi_decl_arg_type(real->unn->args->args[0]);
+
+       default:
+               return psi_ffi_token_type(real->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) {
@@ -180,7 +245,7 @@ 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.rval = &decl->func->ptr;
        decl->call.argc = c;
        decl->call.args = (void **) &call->params[c+1];
 
@@ -332,10 +397,10 @@ static void psi_ffi_call(PSI_Context *C, decl_callinfo *decl_call, impl_vararg *
                                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]);
+               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);
+               ffi_call(&call->signature, FFI_FN(decl_call->sym), *decl_call->rval, decl_call->args);
        }
 }