+static entity_t *pack_bitfield_members_big_endian(il_size_t *struct_offset,
+ il_alignment_t *struct_alignment, bool packed, entity_t *first)
+{
+ type_t *current_base_type = NULL;
+ il_size_t offset = *struct_offset;
+ il_alignment_t alignment = *struct_alignment;
+ size_t bit_offset = 0;
+
+ if (packed)
+ panic("packed bitfields on big-endian arch not supported yet");
+
+ entity_t *member;
+ for (member = first; member != NULL; member = member->base.next) {
+ if (member->kind != ENTITY_COMPOUND_MEMBER)
+ continue;
+
+ type_t *type = member->declaration.type;
+ if (type->kind != TYPE_BITFIELD)
+ break;
+
+ size_t bit_size = type->bitfield.bit_size;
+ type_t *base_type = skip_typeref(type->bitfield.base_type);
+
+ /* see if we need to start a new "bucket" */
+ if (base_type != current_base_type || bit_size > bit_offset) {
+ if (current_base_type != NULL)
+ offset += get_type_size(current_base_type);
+
+ current_base_type = base_type;
+ il_alignment_t base_alignment = get_type_alignment(base_type);
+ il_alignment_t alignment_mask = base_alignment-1;
+ if (base_alignment > alignment)
+ alignment = base_alignment;
+ offset = (offset + base_alignment-1) & ~alignment_mask;
+ bit_offset = get_type_size(base_type) * BITS_PER_BYTE;
+ assert(bit_offset >= bit_size);
+ }
+
+ bit_offset -= bit_size;
+ member->compound_member.offset = offset;
+ member->compound_member.bit_offset = bit_offset;
+ }
+
+ if (current_base_type != NULL)
+ offset += get_type_size(current_base_type);
+
+ *struct_offset = offset;
+ *struct_alignment = alignment;
+ return member;
+}
+
+static entity_t *pack_bitfield_members(il_size_t *struct_offset,
+ il_alignment_t *struct_alignment,
+ bool packed, entity_t *first)
+{
+ il_size_t offset = *struct_offset;
+ il_alignment_t alignment = *struct_alignment;
+ size_t bit_offset = 0;
+
+ entity_t *member;
+ for (member = first; member != NULL; member = member->base.next) {
+ if (member->kind != ENTITY_COMPOUND_MEMBER)
+ continue;
+
+ type_t *type = member->declaration.type;
+ if (type->kind != TYPE_BITFIELD)
+ break;
+
+ type_t *base_type = skip_typeref(type->bitfield.base_type);
+ il_alignment_t base_alignment = get_type_alignment(base_type);
+ il_alignment_t alignment_mask = base_alignment-1;
+ if (base_alignment > alignment)
+ alignment = base_alignment;
+
+ size_t bit_size = type->bitfield.bit_size;
+ if (!packed) {
+ bit_offset += (offset & alignment_mask) * BITS_PER_BYTE;
+ offset &= ~alignment_mask;
+ size_t base_size = get_type_size(base_type) * BITS_PER_BYTE;
+
+ if (bit_offset + bit_size > base_size || bit_size == 0) {
+ offset += (bit_offset+BITS_PER_BYTE-1) / BITS_PER_BYTE;
+ offset = (offset + base_alignment-1) & ~alignment_mask;
+ bit_offset = 0;
+ }
+ }
+
+ member->compound_member.offset = offset;
+ member->compound_member.bit_offset = bit_offset;
+
+ bit_offset += bit_size;
+ offset += bit_offset / BITS_PER_BYTE;
+ bit_offset %= BITS_PER_BYTE;
+ }
+
+ if (bit_offset > 0)
+ offset += 1;
+
+ *struct_offset = offset;
+ *struct_alignment = alignment;
+ return member;
+}
+
+void layout_struct_type(compound_type_t *type)
+{
+ assert(type->compound != NULL);
+
+ compound_t *compound = type->compound;
+ if (!compound->complete)
+ return;
+ if (type->compound->layouted)
+ return;
+
+ il_size_t offset = 0;
+ il_alignment_t alignment = compound->alignment;
+ bool need_pad = false;
+
+ entity_t *entry = compound->members.entities;
+ while (entry != NULL) {
+ if (entry->kind != ENTITY_COMPOUND_MEMBER) {
+ entry = entry->base.next;
+ continue;
+ }
+
+ type_t *m_type = entry->declaration.type;
+ type_t *skipped = skip_typeref(m_type);
+ if (! is_type_valid(skipped)) {
+ entry = entry->base.next;
+ continue;
+ }
+
+ if (skipped->kind == TYPE_BITFIELD) {
+ if (byte_order_big_endian) {
+ entry = pack_bitfield_members_big_endian(&offset, &alignment,
+ compound->packed,
+ entry);
+ } else {
+ entry = pack_bitfield_members(&offset, &alignment,
+ compound->packed, entry);
+ }
+ continue;
+ }
+
+ il_alignment_t m_alignment = get_type_alignment(m_type);
+ if (m_alignment > alignment)
+ alignment = m_alignment;
+
+ if (!compound->packed) {
+ il_size_t new_offset = (offset + m_alignment-1) & -m_alignment;
+
+ if (new_offset > offset) {
+ need_pad = true;
+ offset = new_offset;
+ }
+ }
+
+ entry->compound_member.offset = offset;
+ offset += get_type_size(m_type);
+
+ entry = entry->base.next;
+ }
+
+ if (!compound->packed) {
+ il_size_t new_offset = (offset + alignment-1) & -alignment;
+ if (new_offset > offset) {
+ need_pad = true;
+ offset = new_offset;
+ }
+ }
+
+ if (need_pad) {
+ if (warning.padded) {
+ warningf(&compound->base.source_position, "'%T' needs padding",
+ type);
+ }
+ } else if (compound->packed && warning.packed) {
+ warningf(&compound->base.source_position,
+ "superfluous packed attribute on '%T'", type);
+ }
+
+ compound->size = offset;
+ compound->alignment = alignment;
+ compound->layouted = true;
+}
+
+void layout_union_type(compound_type_t *type)
+{
+ assert(type->compound != NULL);
+
+ compound_t *compound = type->compound;
+ if (! compound->complete)
+ return;
+
+ il_size_t size = 0;
+ il_alignment_t alignment = compound->alignment;
+
+ entity_t *entry = compound->members.entities;
+ for (; entry != NULL; entry = entry->base.next) {
+ if (entry->kind != ENTITY_COMPOUND_MEMBER)
+ continue;
+
+ type_t *m_type = entry->declaration.type;
+ if (! is_type_valid(skip_typeref(m_type)))
+ continue;
+
+ entry->compound_member.offset = 0;
+ il_size_t m_size = get_type_size(m_type);
+ if (m_size > size)
+ size = m_size;
+ il_alignment_t m_alignment = get_type_alignment(m_type);
+ if (m_alignment > alignment)
+ alignment = m_alignment;
+ }
+ size = (size + alignment - 1) & -alignment;
+
+ compound->size = size;
+ compound->alignment = alignment;
+}
+
+static function_parameter_t *allocate_parameter(type_t *const type)
+{
+ function_parameter_t *const param
+ = obstack_alloc(type_obst, sizeof(*param));
+ memset(param, 0, sizeof(*param));
+ param->type = type;
+ return param;
+}
+
+type_t *make_function_2_type(type_t *return_type, type_t *argument_type1,
+ type_t *argument_type2)
+{
+ function_parameter_t *const parameter2 = allocate_parameter(argument_type2);
+ function_parameter_t *const parameter1 = allocate_parameter(argument_type1);
+ parameter1->next = parameter2;
+
+ type_t *type = allocate_type_zero(TYPE_FUNCTION);
+ type->function.return_type = return_type;
+ type->function.parameters = parameter1;
+ type->function.linkage = LINKAGE_C;
+
+ return identify_new_type(type);
+}
+
+type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
+{
+ function_parameter_t *const parameter = allocate_parameter(argument_type);
+
+ type_t *type = allocate_type_zero(TYPE_FUNCTION);
+ type->function.return_type = return_type;
+ type->function.parameters = parameter;
+ type->function.linkage = LINKAGE_C;
+
+ return identify_new_type(type);
+}
+
+type_t *make_function_1_type_variadic(type_t *return_type,
+ type_t *argument_type)
+{
+ function_parameter_t *const parameter = allocate_parameter(argument_type);
+
+ type_t *type = allocate_type_zero(TYPE_FUNCTION);
+ type->function.return_type = return_type;
+ type->function.parameters = parameter;
+ type->function.variadic = true;
+ type->function.linkage = LINKAGE_C;
+
+ return identify_new_type(type);
+}
+
+type_t *make_function_0_type(type_t *return_type)
+{
+ type_t *type = allocate_type_zero(TYPE_FUNCTION);
+ type->function.return_type = return_type;
+ type->function.parameters = NULL;
+ type->function.linkage = LINKAGE_C;
+
+ return identify_new_type(type);
+}
+
+type_t *make_function_type(type_t *return_type, int n_types,
+ type_t *const *argument_types,
+ decl_modifiers_t modifiers)
+{
+ type_t *type = allocate_type_zero(TYPE_FUNCTION);
+ type->function.return_type = return_type;
+ type->function.modifiers |= modifiers;
+ type->function.linkage = LINKAGE_C;
+
+ function_parameter_t *last = NULL;
+ for (int i = 0; i < n_types; ++i) {
+ function_parameter_t *parameter = allocate_parameter(argument_types[i]);
+ if (last == NULL) {
+ type->function.parameters = parameter;
+ } else {
+ last->next = parameter;
+ }
+ last = parameter;
+ }
+
+ return identify_new_type(type);
+}
+