+
+struct psi_ffi_struct_element_storage {
+ ffi_type **els;
+ size_t nels;
+ size_t argc;
+ size_t offset;
+ size_t max_align;
+ size_t last_arg_pos;
+};
+
+static inline void psi_ffi_struct_type_element(
+ struct psi_ffi_struct_element_storage *s, struct psi_decl_arg *darg,
+ ffi_type *darg_type) {
+
+ ffi_type *type, **tmp;
+ size_t padding;
+
+ if (darg->layout->pos == s->last_arg_pos) {
+ /* skip bit fields */
+ return;
+ }
+ s->last_arg_pos = darg->layout->pos;
+
+ type = malloc(sizeof(*type));
+ *type = *darg_type;
+
+ if (type->alignment > s->max_align) {
+ s->max_align = type->alignment;
+ }
+
+ assert(type->size <= darg->layout->len);
+ if ((padding = psi_offset_padding(darg->layout->pos - s->offset, type->alignment))) {
+ if (s->nels + padding + 1 > s->argc) {
+ s->argc += padding;
+ tmp = realloc(s->els, (s->argc + 1) * sizeof(*s->els));
+ if (tmp) {
+ s->els = tmp;
+ } else {
+ free(s->els);
+ abort();
+ }
+ s->els[s->argc] = NULL;
+ }
+ psi_ffi_struct_type_pad(&s->els[s->nels], padding);
+ s->nels += padding;
+ s->offset += padding;
+ }
+ assert(s->offset == darg->layout->pos);
+
+ s->offset = (s->offset + darg->layout->len + type->alignment - 1) & ~(type->alignment - 1);
+ s->els[s->nels++] = type;
+}
+
+static ffi_type **psi_ffi_struct_type_elements(struct psi_decl_struct *strct) {
+ size_t i = 0;
+ ffi_type **tmp;
+ struct psi_decl_arg *darg;
+ struct psi_ffi_struct_element_storage s = {0};
+
+ s.last_arg_pos = -1;
+ s.argc = psi_plist_count(strct->args);
+ s.els = calloc(s.argc + 1, sizeof(*s.els));
+
+ while (psi_plist_get(strct->args, i++, &darg)) {
+ psi_ffi_struct_type_element(&s, darg, psi_ffi_decl_arg_type(darg));
+ }
+
+ /* apply struct alignment padding */
+ s.offset = (s.offset + s.max_align - 1) & ~(s.max_align - 1);
+
+ assert(s.offset <= strct->size);
+ if (s.offset < strct->size) { /* WTF? */
+ size_t padding = strct->size - s.offset;
+
+ tmp = realloc(s.els, (padding + s.argc + 1) * sizeof(*s.els));
+ if (tmp) {
+ s.els = tmp;
+ } else {
+ free(s.els);
+ return NULL;
+ }
+ psi_ffi_struct_type_pad(&s.els[s.nels], padding);
+ s.els[s.argc + padding] = NULL;
+ }
+
+ return s.els;
+}
+
+static inline ffi_type *psi_ffi_decl_type(struct psi_decl_type *type) {
+ struct psi_decl_type *real = psi_decl_type_get_real(type);
+
+ if (real != type && type->real.def->var->pointer_level) {
+ return &ffi_type_pointer;
+ }
+
+ switch (real->type) {
+ case PSI_T_STRUCT:
+ if (!real->real.strct->engine.type) {
+ ffi_type *strct = calloc(1, sizeof(ffi_type));
+
+ strct->type = FFI_TYPE_STRUCT;
+ strct->size = 0;
+ strct->elements = psi_ffi_struct_type_elements(real->real.strct);
+
+ real->real.strct->engine.type = strct;
+ real->real.strct->engine.dtor = psi_ffi_type_dtor;
+ }
+
+ return real->real.strct->engine.type;
+
+ case PSI_T_UNION:
+ {
+ struct psi_decl_arg *arg;
+ psi_plist_get(real->real.unn->args, 0, &arg);
+ return psi_ffi_decl_arg_type(arg);
+ }
+
+ default:
+ break;
+ }
+
+ return psi_ffi_token_type(real->type);
+}
+
+static inline ffi_type *psi_ffi_decl_func_array_type(struct psi_decl *fn) {
+ struct psi_ffi_decl_info *info = fn->info;
+ struct psi_ffi_struct_element_storage s = {0};
+ struct psi_layout l;
+ ffi_type *type;
+ size_t i;
+
+ if (info->rv_array) {
+ return info->rv_array;
+ }
+
+ s.last_arg_pos = -1;
+ s.argc = fn->func->var->array_size;
+ s.els = calloc(s.argc + 1, sizeof(*s.els));
+
+ info->rv_array = calloc(1, sizeof(ffi_type));
+ info->rv_array->type = FFI_TYPE_STRUCT;
+ info->rv_array->size = 0;
+ info->rv_array->elements = s.els;
+
+ l.pos = 0;
+ if (fn->func->var->pointer_level > 1) {
+ l.len = SIZEOF_VOID_P;
+ type = &ffi_type_pointer;
+ } else {
+ l.len = psi_decl_type_get_size(fn->func->type, NULL);
+ type = psi_ffi_decl_type(fn->func->type);
+ }
+
+ assert(!fn->func->layout);
+ fn->func->layout = &l;
+ for (i = 0; i < fn->func->var->array_size; ++i) {
+ psi_ffi_struct_type_element(&s, fn->func, type);
+ info->rv_array->elements = s.els;
+ l.pos += l.len;
+ }
+ fn->func->layout = NULL;
+
+ return info->rv_array;
+}
+
+static inline ffi_type *psi_ffi_decl_arg_type(struct psi_decl_arg *darg) {