+
+static void psi_jit_struct_type_dtor(void *type)
+{
+ jit_type_t strct = type;
+
+ jit_type_free(strct);
+}
+
+static size_t psi_jit_struct_type_pad(jit_type_t *els, size_t padding)
+{
+ size_t i;
+
+ for (i = 0; i < padding; ++i) {
+ *els++ = jit_type_copy(jit_type_sys_char);
+ }
+
+ return padding;
+}
+
+static unsigned psi_jit_struct_type_elements(struct psi_decl_struct *strct,
+ jit_type_t **fields)
+{
+ size_t i = 0, argc = psi_plist_count(strct->args), nels = 0, offset = 0,
+ maxalign, last_arg_pos = -1;
+ struct psi_decl_arg *darg;
+
+ *fields = calloc(argc + 1, sizeof(*fields));
+
+ while (psi_plist_get(strct->args, i++, &darg)) {
+ jit_type_t type;
+ size_t padding, alignment;
+
+ if (darg->layout->pos == last_arg_pos) {
+ /* skip bit fields */
+ continue;
+ }
+ last_arg_pos = darg->layout->pos;
+
+ type = jit_type_copy(psi_jit_decl_arg_type(darg));
+
+ if ((alignment = jit_type_get_alignment(type)) > maxalign) {
+ maxalign = alignment;
+ }
+
+ assert(jit_type_get_size(type) == darg->layout->len);
+ if ((padding = psi_offset_padding(darg->layout->pos - offset, alignment))) {
+ if (nels + padding > argc) {
+ argc += padding;
+ *fields = realloc(*fields, (argc + 1) * sizeof(*fields));
+ }
+ psi_jit_struct_type_pad(&(*fields)[nels], padding);
+ nels += padding;
+ offset += padding;
+ }
+ assert(offset == darg->layout->pos);
+
+ offset = (offset + darg->layout->len + alignment - 1)
+ & ~(alignment - 1);
+ (*fields)[nels++] = type;
+ }
+
+ /* apply struct alignment padding */
+ offset = (offset + maxalign - 1) & ~(maxalign - 1);
+
+ assert(offset <= strct->size);
+ if (offset < strct->size) {
+ nels += psi_jit_struct_type_pad(&(*fields)[nels], strct->size - offset);
+ }
+
+ return nels;
+}
+static inline jit_type_t psi_jit_decl_type(struct psi_decl_type *type)
+{
+ struct psi_decl_type *real = psi_decl_type_get_real(type);
+
+ switch (real->type) {
+ case PSI_T_STRUCT:
+ if (!real->real.strct->engine.type) {
+ unsigned count;
+ jit_type_t strct, *fields = NULL;
+
+ count = psi_jit_struct_type_elements(real->real.strct, &fields);
+ strct = jit_type_create_struct(fields, count, 0);
+
+ real->real.strct->engine.type = strct;
+ real->real.strct->engine.dtor = psi_jit_struct_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_jit_decl_arg_type(arg);
+ }
+
+ default:
+ return psi_jit_token_type(real->type);
+ }
+}
+static inline jit_type_t psi_jit_decl_arg_type(struct psi_decl_arg *darg)
+{