authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-02 14:44:25-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-02 18:31:19-04:00
log5aee17e888c0db7ed0d220334f3adeff6e323cb2
tree27699680c82b9aadbb3b2b0e2b53bf74d5536af3
parentddb8aa73f542d3432538e6de466ac216c89fd12b
signaturelock-open Commit is signed but in an unrecognized format.

regression fixes and fix packed struct abi size


4 files changed, 179 insertions(+), 106 deletions(-)

doc/langref.html.in+4-4
......@@ -2112,8 +2112,8 @@ test "linked list" {
21122112 <li>A {#link|packed enum#} field uses exactly the bit width of its integer tag type.</li>
21132113 <li>A {#link|packed union#} field uses exactly the bit width of the union field with
21142114 the largest bit width.</li>
2115 <li>Non-byte-aligned fields are packed into the smallest possible
2116 byte-aligned integers in accordance with the target endianness.
2115 <li>Non-ABI-aligned fields are packed into the smallest possible
2116 ABI-aligned integers in accordance with the target endianness.
21172117 </li>
21182118 </ul>
21192119 <p>
......@@ -2213,10 +2213,10 @@ fn bar(x: *const u3) u3 {
22132213 {#code_end#}
22142214 <p>
22152215 In this case, the function {#syntax#}bar{#endsyntax#} cannot be called becuse the pointer
2216 to the non-byte-aligned field mentions the bit offset, but the function expects a byte-aligned pointer.
2216 to the non-ABI-aligned field mentions the bit offset, but the function expects an ABI-aligned pointer.
22172217 </p>
22182218 <p>
2219 Pointers to non-byte-aligned fields share the same address as the other fields within their host integer:
2219 Pointers to non-ABI-aligned fields share the same address as the other fields within their host integer:
22202220 </p>
22212221 {#code_begin|test#}
22222222const std = @import("std");
src/all_types.hpp+2
......@@ -1079,6 +1079,8 @@ enum ResolveStatus {
10791079 ResolveStatusZeroBitsKnown,
10801080 ResolveStatusAlignmentKnown,
10811081 ResolveStatusSizeKnown,
1082 ResolveStatusLLVMFwdDecl,
1083 ResolveStatusLLVMFull,
10821084};
10831085
10841086struct ZigPackage {
src/analyze.cpp+162-97
......@@ -27,6 +27,7 @@ static Error ATTRIBUTE_MUST_USE resolve_enum_zero_bits(CodeGen *g, ZigType *enum
2727static Error ATTRIBUTE_MUST_USE resolve_union_zero_bits(CodeGen *g, ZigType *union_type);
2828static Error ATTRIBUTE_MUST_USE resolve_union_alignment(CodeGen *g, ZigType *union_type);
2929static void analyze_fn_body(CodeGen *g, ZigFn *fn_table_entry);
30static void resolve_llvm_types(CodeGen *g, ZigType *type, ResolveStatus wanted_resolve_status);
3031
3132static bool is_top_level_struct(ZigType *import) {
3233 return import->id == ZigTypeIdStruct && import->data.structure.root_struct != nullptr;
......@@ -278,6 +279,9 @@ bool type_is_resolved(ZigType *type_entry, ResolveStatus status) {
278279 return type_entry->data.enumeration.zero_bits_known;
279280 case ResolveStatusSizeKnown:
280281 return type_entry->data.enumeration.complete;
282 case ResolveStatusLLVMFwdDecl:
283 case ResolveStatusLLVMFull:
284 return type_entry->llvm_di_type != nullptr;
281285 }
282286 zig_unreachable();
283287 case ZigTypeIdOpaque:
......@@ -1120,6 +1124,7 @@ static Error emit_error_unless_type_allowed_in_packed_struct(CodeGen *g, ZigType
11201124 ZigType *elem_type = type_entry->data.array.child_type;
11211125 if ((err = emit_error_unless_type_allowed_in_packed_struct(g, elem_type, source_node)))
11221126 return err;
1127 // TODO revisit this when doing https://github.com/ziglang/zig/issues/1512
11231128 if (type_size(g, type_entry) * 8 == type_size_bits(g, type_entry))
11241129 return ErrorNone;
11251130 add_node_error(g, source_node,
......@@ -1559,8 +1564,21 @@ ZigType *get_struct_type(CodeGen *g, const char *type_name, const char *field_na
15591564 return struct_type;
15601565}
15611566
1562static size_t get_store_size_in_bits(size_t size_in_bits) {
1563 return ((size_in_bits + 7) / 8) * 8;
1567static size_t get_store_size_bytes(size_t size_in_bits) {
1568 return (size_in_bits + 7) / 8;
1569}
1570
1571static size_t get_abi_align_bytes(size_t size_in_bits, size_t pointer_size_bytes) {
1572 size_t store_size_bytes = get_store_size_bytes(size_in_bits);
1573 if (store_size_bytes >= pointer_size_bytes)
1574 return pointer_size_bytes;
1575 return round_to_next_power_of_2(store_size_bytes);
1576}
1577
1578static size_t get_abi_size_bytes(size_t size_in_bits, size_t pointer_size_bytes) {
1579 size_t store_size_bytes = get_store_size_bytes(size_in_bits);
1580 size_t abi_align = get_abi_align_bytes(size_in_bits, pointer_size_bytes);
1581 return align_forward(store_size_bytes, abi_align);
15641582}
15651583
15661584static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
......@@ -1637,17 +1655,18 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
16371655 field->bit_offset_in_host = packed_bits_offset - first_packed_bits_offset_misalign;
16381656
16391657 size_t full_bit_count = next_packed_bits_offset - first_packed_bits_offset_misalign;
1640 if (get_store_size_in_bits(full_bit_count) == full_bit_count) {
1641 // next field recovers store alignment
1642 host_int_bytes[gen_field_index] = full_bit_count / 8;
1658 size_t full_abi_size = get_abi_size_bytes(full_bit_count, g->pointer_size_bytes);
1659 if (full_abi_size * 8 == full_bit_count) {
1660 // next field recovers ABI alignment
1661 host_int_bytes[gen_field_index] = full_abi_size;
16431662 gen_field_index += 1;
16441663 // TODO: https://github.com/ziglang/zig/issues/1512
1645 next_offset = next_field_offset(next_offset, abi_align, full_bit_count / 8, 1);
1664 next_offset = next_field_offset(next_offset, abi_align, full_abi_size, 1);
16461665 size_in_bits = next_offset * 8;
16471666
16481667 first_packed_bits_offset_misalign = SIZE_MAX;
16491668 }
1650 } else if (get_store_size_in_bits(field_type->size_in_bits) != field_size_in_bits) {
1669 } else if (get_abi_size_bytes(field_type->size_in_bits, g->pointer_size_bytes) * 8 != field_size_in_bits) {
16511670 first_packed_bits_offset_misalign = packed_bits_offset;
16521671 field->bit_offset_in_host = 0;
16531672 } else {
......@@ -1676,9 +1695,9 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
16761695 }
16771696 if (first_packed_bits_offset_misalign != SIZE_MAX) {
16781697 size_t full_bit_count = packed_bits_offset - first_packed_bits_offset_misalign;
1679 size_t store_bit_count = get_store_size_in_bits(full_bit_count);
1680 next_offset = next_field_offset(next_offset, abi_align, store_bit_count / 8, 1);
1681 host_int_bytes[gen_field_index] = store_bit_count / 8;
1698 size_t full_abi_size = get_abi_size_bytes(full_bit_count, g->pointer_size_bytes);
1699 next_offset = next_field_offset(next_offset, abi_align, full_abi_size, abi_align);
1700 host_int_bytes[gen_field_index] = full_abi_size;
16821701 gen_field_index += 1;
16831702 }
16841703
......@@ -4986,6 +5005,10 @@ Error type_resolve(CodeGen *g, ZigType *ty, ResolveStatus status) {
49865005 return resolve_union_type(g, ty);
49875006 }
49885007 return ErrorNone;
5008 case ResolveStatusLLVMFwdDecl:
5009 case ResolveStatusLLVMFull:
5010 resolve_llvm_types(g, ty, status);
5011 return ErrorNone;
49895012 }
49905013 zig_unreachable();
49915014}
......@@ -6223,57 +6246,68 @@ Buf *type_h_name(ZigType *t) {
62236246 return type_bare_name(t);
62246247}
62256248
6226static void resolve_llvm_types_slice(CodeGen *g, ZigType *type) {
6249static void resolve_llvm_types_slice(CodeGen *g, ZigType *type, ResolveStatus wanted_resolve_status) {
6250 if (type->data.structure.resolve_status >= wanted_resolve_status) return;
6251
62276252 ZigType *ptr_type = type->data.structure.fields[slice_ptr_index].type_entry;
62286253 ZigType *child_type = ptr_type->data.pointer.child_type;
6254 ZigType *usize_type = g->builtin_types.entry_usize;
6255 LLVMTypeRef usize_llvm_type = get_llvm_type(g, usize_type);
6256 ZigLLVMDIType *usize_llvm_di_type = get_llvm_di_type(g, usize_type);
6257 ZigLLVMDIScope *compile_unit_scope = ZigLLVMCompileUnitToScope(g->compile_unit);
6258 ZigLLVMDIFile *di_file = nullptr;
6259 unsigned line = 0;
62296260
6230 if (ptr_type->data.pointer.is_const || ptr_type->data.pointer.is_volatile ||
6231 ptr_type->data.pointer.explicit_alignment != 0 || ptr_type->data.pointer.allow_zero)
6232 {
6233 ZigType *peer_ptr_type = get_pointer_to_type_extra(g, child_type, false, false,
6234 PtrLenUnknown, 0, 0, 0, false);
6235 ZigType *peer_slice_type = get_slice_type(g, peer_ptr_type);
6236
6237 type->llvm_type = get_llvm_type(g, peer_slice_type);
6238 type->llvm_di_type = get_llvm_di_type(g, peer_slice_type);
6239 }
6240
6241 // If the child type is []const T then we need to make sure the type ref
6242 // and debug info is the same as if the child type were []T.
6243 if (is_slice(child_type)) {
6244 ZigType *child_ptr_type = child_type->data.structure.fields[slice_ptr_index].type_entry;
6245 assert(child_ptr_type->id == ZigTypeIdPointer);
6246 if (child_ptr_type->data.pointer.is_const || child_ptr_type->data.pointer.is_volatile ||
6247 child_ptr_type->data.pointer.explicit_alignment != 0 || child_ptr_type->data.pointer.allow_zero)
6261 if (type->data.structure.resolve_status < ResolveStatusLLVMFwdDecl) {
6262 bool done = false;
6263 if (ptr_type->data.pointer.is_const || ptr_type->data.pointer.is_volatile ||
6264 ptr_type->data.pointer.explicit_alignment != 0 || ptr_type->data.pointer.allow_zero)
62486265 {
6249 ZigType *grand_child_type = child_ptr_type->data.pointer.child_type;
6250 ZigType *bland_child_ptr_type = get_pointer_to_type_extra(g, grand_child_type, false, false,
6251 PtrLenUnknown, 0, 0, 0, false);
6252 ZigType *bland_child_slice = get_slice_type(g, bland_child_ptr_type);
6253 ZigType *peer_ptr_type = get_pointer_to_type_extra(g, bland_child_slice, false, false,
6266 ZigType *peer_ptr_type = get_pointer_to_type_extra(g, child_type, false, false,
62546267 PtrLenUnknown, 0, 0, 0, false);
62556268 ZigType *peer_slice_type = get_slice_type(g, peer_ptr_type);
62566269
62576270 type->llvm_type = get_llvm_type(g, peer_slice_type);
62586271 type->llvm_di_type = get_llvm_di_type(g, peer_slice_type);
6272 done = true;
62596273 }
6260 }
62616274
6262 if (type->llvm_type != nullptr)
6263 return;
6275 // If the child type is []const T then we need to make sure the type ref
6276 // and debug info is the same as if the child type were []T.
6277 if (is_slice(child_type)) {
6278 ZigType *child_ptr_type = child_type->data.structure.fields[slice_ptr_index].type_entry;
6279 assert(child_ptr_type->id == ZigTypeIdPointer);
6280 if (child_ptr_type->data.pointer.is_const || child_ptr_type->data.pointer.is_volatile ||
6281 child_ptr_type->data.pointer.explicit_alignment != 0 || child_ptr_type->data.pointer.allow_zero)
6282 {
6283 ZigType *grand_child_type = child_ptr_type->data.pointer.child_type;
6284 ZigType *bland_child_ptr_type = get_pointer_to_type_extra(g, grand_child_type, false, false,
6285 PtrLenUnknown, 0, 0, 0, false);
6286 ZigType *bland_child_slice = get_slice_type(g, bland_child_ptr_type);
6287 ZigType *peer_ptr_type = get_pointer_to_type_extra(g, bland_child_slice, false, false,
6288 PtrLenUnknown, 0, 0, 0, false);
6289 ZigType *peer_slice_type = get_slice_type(g, peer_ptr_type);
6290
6291 type->llvm_type = get_llvm_type(g, peer_slice_type);
6292 type->llvm_di_type = get_llvm_di_type(g, peer_slice_type);
6293 done = true;
6294 }
6295 }
62646296
6265 ZigType *usize_type = g->builtin_types.entry_usize;
6266 LLVMTypeRef usize_llvm_type = get_llvm_type(g, usize_type);
6267 ZigLLVMDIType *usize_llvm_di_type = get_llvm_di_type(g, usize_type);
6297 if (done) {
6298 type->data.structure.resolve_status = ResolveStatusLLVMFull;
6299 return;
6300 }
62686301
6269 type->llvm_type = LLVMStructCreateNamed(LLVMGetGlobalContext(), buf_ptr(&type->name));
6302 type->llvm_type = LLVMStructCreateNamed(LLVMGetGlobalContext(), buf_ptr(&type->name));
62706303
6271 ZigLLVMDIScope *compile_unit_scope = ZigLLVMCompileUnitToScope(g->compile_unit);
6272 ZigLLVMDIFile *di_file = nullptr;
6273 unsigned line = 0;
6274 type->llvm_di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
6275 ZigLLVMTag_DW_structure_type(), buf_ptr(&type->name),
6276 compile_unit_scope, di_file, line);
6304 type->llvm_di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
6305 ZigLLVMTag_DW_structure_type(), buf_ptr(&type->name),
6306 compile_unit_scope, di_file, line);
6307
6308 type->data.structure.resolve_status = ResolveStatusLLVMFwdDecl;
6309 if (ResolveStatusLLVMFwdDecl >= wanted_resolve_status) return;
6310 }
62776311
62786312 if (!type_has_bits(child_type)) {
62796313 LLVMTypeRef element_types[] = {
......@@ -6304,6 +6338,7 @@ static void resolve_llvm_types_slice(CodeGen *g, ZigType *type) {
63046338
63056339 ZigLLVMReplaceTemporary(g->dbuilder, type->llvm_di_type, replacement_di_type);
63066340 type->llvm_di_type = replacement_di_type;
6341 type->data.structure.resolve_status = ResolveStatusLLVMFull;
63076342 return;
63086343 }
63096344
......@@ -6345,17 +6380,16 @@ static void resolve_llvm_types_slice(CodeGen *g, ZigType *type) {
63456380
63466381 ZigLLVMReplaceTemporary(g->dbuilder, type->llvm_di_type, replacement_di_type);
63476382 type->llvm_di_type = replacement_di_type;
6383 type->data.structure.resolve_status = ResolveStatusLLVMFull;
63486384}
63496385
6350static void resolve_llvm_types_struct(CodeGen *g, ZigType *struct_type) {
6386static void resolve_llvm_types_struct(CodeGen *g, ZigType *struct_type, ResolveStatus wanted_resolve_status) {
63516387 assert(struct_type->id == ZigTypeIdStruct);
63526388 assert(struct_type->data.structure.resolve_status != ResolveStatusInvalid);
63536389 assert(struct_type->data.structure.resolve_status >= ResolveStatusSizeKnown);
63546390 assert(struct_type->data.structure.fields || struct_type->data.structure.src_field_count == 0);
6391 if (struct_type->data.structure.resolve_status >= wanted_resolve_status) return;
63556392
6356 // Do this early for the benefit of recursion.
6357 struct_type->llvm_type = type_has_bits(struct_type) ?
6358 LLVMStructCreateNamed(LLVMGetGlobalContext(), buf_ptr(&struct_type->name)) : LLVMVoidType();
63596393 AstNode *decl_node = struct_type->data.structure.decl_node;
63606394 ZigLLVMDIFile *di_file;
63616395 ZigLLVMDIScope *di_scope;
......@@ -6372,10 +6406,18 @@ static void resolve_llvm_types_struct(CodeGen *g, ZigType *struct_type) {
63726406 di_scope = ZigLLVMCompileUnitToScope(g->compile_unit);
63736407 line = 0;
63746408 }
6375 unsigned dwarf_kind = ZigLLVMTag_DW_structure_type();
6376 struct_type->llvm_di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
6377 dwarf_kind, buf_ptr(&struct_type->name),
6378 di_scope, di_file, line);
6409
6410 if (struct_type->data.structure.resolve_status < ResolveStatusLLVMFwdDecl) {
6411 struct_type->llvm_type = type_has_bits(struct_type) ?
6412 LLVMStructCreateNamed(LLVMGetGlobalContext(), buf_ptr(&struct_type->name)) : LLVMVoidType();
6413 unsigned dwarf_kind = ZigLLVMTag_DW_structure_type();
6414 struct_type->llvm_di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
6415 dwarf_kind, buf_ptr(&struct_type->name),
6416 di_scope, di_file, line);
6417
6418 struct_type->data.structure.resolve_status = ResolveStatusLLVMFwdDecl;
6419 if (ResolveStatusLLVMFwdDecl >= wanted_resolve_status) return;
6420 }
63796421
63806422 size_t field_count = struct_type->data.structure.src_field_count;
63816423 size_t gen_field_count = struct_type->data.structure.gen_field_count;
......@@ -6402,14 +6444,15 @@ static void resolve_llvm_types_struct(CodeGen *g, ZigType *struct_type) {
64026444 // this field is not byte-aligned; it is part of the previous field with a bit offset
64036445
64046446 size_t full_bit_count = next_packed_bits_offset - first_packed_bits_offset_misalign;
6405 if (get_store_size_in_bits(full_bit_count) == full_bit_count) {
6406 // next field recovers store alignment
6447 size_t full_abi_size = get_abi_size_bytes(full_bit_count, g->pointer_size_bytes);
6448 if (full_abi_size * 8 == full_bit_count) {
6449 // next field recovers ABI alignment
64076450 element_types[gen_field_index] = LLVMIntType((unsigned)(full_bit_count));
64086451 gen_field_index += 1;
64096452
64106453 first_packed_bits_offset_misalign = SIZE_MAX;
64116454 }
6412 } else if (get_store_size_in_bits(field_type->size_in_bits) != field_size_in_bits) {
6455 } else if (get_abi_size_bytes(field_type->size_in_bits, g->pointer_size_bytes) * 8 != field_size_in_bits) {
64136456 first_packed_bits_offset_misalign = packed_bits_offset;
64146457 } else {
64156458 // This is a byte-aligned field (both start and end) in a packed struct.
......@@ -6426,8 +6469,8 @@ static void resolve_llvm_types_struct(CodeGen *g, ZigType *struct_type) {
64266469 }
64276470 if (first_packed_bits_offset_misalign != SIZE_MAX) {
64286471 size_t full_bit_count = packed_bits_offset - first_packed_bits_offset_misalign;
6429 size_t store_bit_count = get_store_size_in_bits(full_bit_count);
6430 element_types[gen_field_index] = LLVMIntType((unsigned)store_bit_count);
6472 size_t full_abi_size = get_abi_size_bytes(full_bit_count, g->pointer_size_bytes);
6473 element_types[gen_field_index] = LLVMIntType((unsigned)full_abi_size * 8);
64316474 gen_field_index += 1;
64326475 }
64336476
......@@ -6466,7 +6509,7 @@ static void resolve_llvm_types_struct(CodeGen *g, ZigType *struct_type) {
64666509 debug_align_in_bits = 8 * type_struct_field->type_entry->abi_align;
64676510 debug_offset_in_bits = 8 * type_struct_field->offset + type_struct_field->bit_offset_in_host;
64686511 } else {
6469 debug_size_in_bits = get_store_size_in_bits(field_type->size_in_bits);
6512 debug_size_in_bits = 8 * get_store_size_bytes(field_type->size_in_bits);
64706513 debug_align_in_bits = 8 * field_type->abi_align;
64716514 debug_offset_in_bits = 8 * type_struct_field->offset;
64726515 }
......@@ -6488,7 +6531,7 @@ static void resolve_llvm_types_struct(CodeGen *g, ZigType *struct_type) {
64886531 debug_field_index += 1;
64896532 }
64906533
6491 uint64_t debug_size_in_bits = get_store_size_in_bits(struct_type->size_in_bits);
6534 uint64_t debug_size_in_bits = 8*get_store_size_bytes(struct_type->size_in_bits);
64926535 uint64_t debug_align_in_bits = 8*struct_type->abi_align;
64936536 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
64946537 di_scope,
......@@ -6500,11 +6543,13 @@ static void resolve_llvm_types_struct(CodeGen *g, ZigType *struct_type) {
65006543
65016544 ZigLLVMReplaceTemporary(g->dbuilder, struct_type->llvm_di_type, replacement_di_type);
65026545 struct_type->llvm_di_type = replacement_di_type;
6546 struct_type->data.structure.resolve_status = ResolveStatusLLVMFull;
65036547}
65046548
65056549static void resolve_llvm_types_enum(CodeGen *g, ZigType *enum_type) {
65066550 assert(!enum_type->data.enumeration.is_invalid);
65076551 assert(enum_type->data.enumeration.complete);
6552 if (enum_type->llvm_di_type != nullptr) return;
65086553
65096554 uint32_t field_count = enum_type->data.enumeration.src_field_count;
65106555
......@@ -6541,27 +6586,34 @@ static void resolve_llvm_types_enum(CodeGen *g, ZigType *enum_type) {
65416586 enum_type->llvm_type = get_llvm_type(g, tag_int_type);
65426587}
65436588
6544static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type) {
6589static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type, ResolveStatus wanted_resolve_status) {
6590 if (union_type->data.unionation.resolve_status >= wanted_resolve_status) return;
6591
65456592 ZigType *most_aligned_union_member = union_type->data.unionation.most_aligned_union_member;
65466593 ZigType *tag_type = union_type->data.unionation.tag_type;
65476594 if (most_aligned_union_member == nullptr) {
65486595 union_type->llvm_type = get_llvm_type(g, tag_type);
65496596 union_type->llvm_di_type = get_llvm_di_type(g, tag_type);
6597 union_type->data.unionation.resolve_status = ResolveStatusLLVMFull;
65506598 return;
65516599 }
65526600
6553 // Do this first for the benefit of recursive calls.
6554 union_type->llvm_type = LLVMStructCreateNamed(LLVMGetGlobalContext(), buf_ptr(&union_type->name));
65556601 Scope *scope = &union_type->data.unionation.decls_scope->base;
65566602 ZigType *import = get_scope_import(scope);
65576603 AstNode *decl_node = union_type->data.unionation.decl_node;
6558 size_t line = decl_node ? decl_node->line : 0;
6559 unsigned dwarf_kind = ZigLLVMTag_DW_structure_type();
6560 union_type->llvm_di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
6561 dwarf_kind, buf_ptr(&union_type->name),
6562 ZigLLVMFileToScope(import->data.structure.root_struct->di_file),
6563 import->data.structure.root_struct->di_file, (unsigned)(line + 1));
65646604
6605 if (union_type->data.unionation.resolve_status < ResolveStatusLLVMFwdDecl) {
6606 union_type->llvm_type = LLVMStructCreateNamed(LLVMGetGlobalContext(), buf_ptr(&union_type->name));
6607 size_t line = decl_node ? decl_node->line : 0;
6608 unsigned dwarf_kind = ZigLLVMTag_DW_structure_type();
6609 union_type->llvm_di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
6610 dwarf_kind, buf_ptr(&union_type->name),
6611 ZigLLVMFileToScope(import->data.structure.root_struct->di_file),
6612 import->data.structure.root_struct->di_file, (unsigned)(line + 1));
6613
6614 union_type->data.unionation.resolve_status = ResolveStatusLLVMFwdDecl;
6615 if (ResolveStatusLLVMFwdDecl >= wanted_resolve_status) return;
6616 }
65656617
65666618 uint32_t gen_field_count = union_type->data.unionation.gen_field_count;
65676619 ZigLLVMDIType **union_inner_di_types = allocate<ZigLLVMDIType*>(gen_field_count);
......@@ -6615,6 +6667,7 @@ static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type) {
66156667
66166668 ZigLLVMReplaceTemporary(g->dbuilder, union_type->llvm_di_type, replacement_di_type);
66176669 union_type->llvm_di_type = replacement_di_type;
6670 union_type->data.unionation.resolve_status = ResolveStatusLLVMFull;
66186671 return;
66196672 }
66206673
......@@ -6685,9 +6738,12 @@ static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type) {
66856738
66866739 ZigLLVMReplaceTemporary(g->dbuilder, union_type->llvm_di_type, replacement_di_type);
66876740 union_type->llvm_di_type = replacement_di_type;
6741 union_type->data.unionation.resolve_status = ResolveStatusLLVMFull;
66886742}
66896743
66906744static void resolve_llvm_types_pointer(CodeGen *g, ZigType *type) {
6745 if (type->llvm_di_type != nullptr) return;
6746
66916747 ZigType *elem_type = type->data.pointer.child_type;
66926748
66936749 if (type->data.pointer.is_const || type->data.pointer.is_volatile ||
......@@ -6702,10 +6758,11 @@ static void resolve_llvm_types_pointer(CodeGen *g, ZigType *type) {
67026758 }
67036759
67046760 if (type->data.pointer.host_int_bytes == 0) {
6705 type->llvm_type = LLVMPointerType(get_llvm_type(g, elem_type), 0);
6706 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, type->llvm_type);
6707 uint64_t debug_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, type->llvm_type);
6708 type->llvm_di_type = ZigLLVMCreateDebugPointerType(g->dbuilder, get_llvm_di_type(g, elem_type),
6761 assertNoError(type_resolve(g, elem_type, ResolveStatusLLVMFwdDecl));
6762 type->llvm_type = LLVMPointerType(elem_type->llvm_type, 0);
6763 uint64_t debug_size_in_bits = 8*get_store_size_bytes(type->size_in_bits);
6764 uint64_t debug_align_in_bits = 8*type->abi_align;
6765 type->llvm_di_type = ZigLLVMCreateDebugPointerType(g->dbuilder, elem_type->llvm_di_type,
67096766 debug_size_in_bits, debug_align_in_bits, buf_ptr(&type->name));
67106767 } else {
67116768 ZigType *host_int_type = get_int_type(g, false, type->data.pointer.host_int_bytes * 8);
......@@ -6719,6 +6776,8 @@ static void resolve_llvm_types_pointer(CodeGen *g, ZigType *type) {
67196776}
67206777
67216778static void resolve_llvm_types_integer(CodeGen *g, ZigType *type) {
6779 if (type->llvm_di_type != nullptr) return;
6780
67226781 unsigned dwarf_tag;
67236782 if (type->data.integral.is_signed) {
67246783 if (type->size_in_bits == 8) {
......@@ -6739,6 +6798,8 @@ static void resolve_llvm_types_integer(CodeGen *g, ZigType *type) {
67396798}
67406799
67416800static void resolve_llvm_types_optional(CodeGen *g, ZigType *type) {
6801 if (type->llvm_di_type != nullptr) return;
6802
67426803 LLVMTypeRef bool_llvm_type = get_llvm_type(g, g->builtin_types.entry_bool);
67436804 ZigLLVMDIType *bool_llvm_di_type = get_llvm_di_type(g, g->builtin_types.entry_bool);
67446805
......@@ -6807,6 +6868,8 @@ static void resolve_llvm_types_optional(CodeGen *g, ZigType *type) {
68076868}
68086869
68096870static void resolve_llvm_types_error_union(CodeGen *g, ZigType *type) {
6871 if (type->llvm_di_type != nullptr) return;
6872
68106873 ZigType *payload_type = type->data.error_union.payload_type;
68116874 ZigType *err_set_type = type->data.error_union.err_set_type;
68126875
......@@ -6874,6 +6937,8 @@ static void resolve_llvm_types_error_union(CodeGen *g, ZigType *type) {
68746937}
68756938
68766939static void resolve_llvm_types_array(CodeGen *g, ZigType *type) {
6940 if (type->llvm_di_type != nullptr) return;
6941
68776942 ZigType *elem_type = type->data.array.child_type;
68786943
68796944 // TODO https://github.com/ziglang/zig/issues/1424
......@@ -6887,6 +6952,8 @@ static void resolve_llvm_types_array(CodeGen *g, ZigType *type) {
68876952}
68886953
68896954static void resolve_llvm_types_fn(CodeGen *g, ZigType *fn_type) {
6955 if (fn_type->llvm_di_type != nullptr) return;
6956
68906957 FnTypeId *fn_type_id = &fn_type->data.fn.fn_type_id;
68916958 bool first_arg_return = want_first_arg_sret(g, fn_type_id);
68926959 bool is_async = fn_type_id->cc == CallingConventionAsync;
......@@ -7010,16 +7077,12 @@ static void resolve_llvm_types_anyerror(CodeGen *g) {
70107077 get_llvm_di_type(g, g->err_tag_type), "");
70117078}
70127079
7013static void resolve_llvm_types(CodeGen *g, ZigType *type) {
7014 assert(type_is_resolved(type, ResolveStatusSizeKnown));
7080static void resolve_llvm_types(CodeGen *g, ZigType *type, ResolveStatus wanted_resolve_status) {
7081 assert(type->id == ZigTypeIdOpaque || type_is_resolved(type, ResolveStatusSizeKnown));
7082 assert(wanted_resolve_status > ResolveStatusSizeKnown);
70157083 switch (type->id) {
70167084 case ZigTypeIdInvalid:
7017 case ZigTypeIdFloat:
7018 case ZigTypeIdOpaque:
70197085 case ZigTypeIdMetaType:
7020 case ZigTypeIdVoid:
7021 case ZigTypeIdBool:
7022 case ZigTypeIdUnreachable:
70237086 case ZigTypeIdComptimeFloat:
70247087 case ZigTypeIdComptimeInt:
70257088 case ZigTypeIdEnumLiteral:
......@@ -7028,18 +7091,26 @@ static void resolve_llvm_types(CodeGen *g, ZigType *type) {
70287091 case ZigTypeIdBoundFn:
70297092 case ZigTypeIdArgTuple:
70307093 zig_unreachable();
7094 case ZigTypeIdFloat:
7095 case ZigTypeIdOpaque:
7096 case ZigTypeIdVoid:
7097 case ZigTypeIdBool:
7098 case ZigTypeIdUnreachable:
7099 assert(type->llvm_di_type != nullptr);
7100 return;
70317101 case ZigTypeIdStruct:
70327102 if (type->data.structure.is_slice)
7033 return resolve_llvm_types_slice(g, type);
7103 return resolve_llvm_types_slice(g, type, wanted_resolve_status);
70347104 else
7035 return resolve_llvm_types_struct(g, type);
7105 return resolve_llvm_types_struct(g, type, wanted_resolve_status);
70367106 case ZigTypeIdEnum:
70377107 return resolve_llvm_types_enum(g, type);
70387108 case ZigTypeIdUnion:
7039 return resolve_llvm_types_union(g, type);
7109 return resolve_llvm_types_union(g, type, wanted_resolve_status);
70407110 case ZigTypeIdPointer:
70417111 return resolve_llvm_types_pointer(g, type);
70427112 case ZigTypeIdPromise: {
7113 if (type->llvm_di_type != nullptr) return;
70437114 ZigType *u8_ptr_type = get_pointer_to_type(g, g->builtin_types.entry_u8, false);
70447115 type->llvm_type = get_llvm_type(g, u8_ptr_type);
70457116 type->llvm_di_type = get_llvm_di_type(g, u8_ptr_type);
......@@ -7056,6 +7127,8 @@ static void resolve_llvm_types(CodeGen *g, ZigType *type) {
70567127 case ZigTypeIdFn:
70577128 return resolve_llvm_types_fn(g, type);
70587129 case ZigTypeIdErrorSet: {
7130 if (type->llvm_di_type != nullptr) return;
7131
70597132 if (g->builtin_types.entry_global_error_set->llvm_type == nullptr) {
70607133 resolve_llvm_types_anyerror(g);
70617134 }
......@@ -7064,6 +7137,8 @@ static void resolve_llvm_types(CodeGen *g, ZigType *type) {
70647137 return;
70657138 }
70667139 case ZigTypeIdVector: {
7140 if (type->llvm_di_type != nullptr) return;
7141
70677142 type->llvm_type = LLVMVectorType(get_llvm_type(g, type->data.vector.elem_type), type->data.vector.len);
70687143 type->llvm_di_type = ZigLLVMDIBuilderCreateVectorType(g->dbuilder, type->size_in_bits,
70697144 type->abi_align, get_llvm_di_type(g, type->data.vector.elem_type), type->data.vector.len);
......@@ -7074,23 +7149,13 @@ static void resolve_llvm_types(CodeGen *g, ZigType *type) {
70747149}
70757150
70767151LLVMTypeRef get_llvm_type(CodeGen *g, ZigType *type) {
7077 if (type->llvm_type != nullptr)
7078 return type->llvm_type;
7079 resolve_llvm_types(g, type);
7080 assert(type->llvm_type != nullptr);
7081 assert(type->llvm_di_type != nullptr);
7152 assertNoError(type_resolve(g, type, ResolveStatusLLVMFull));
70827153 assert(type->abi_size == 0 || type->abi_size == LLVMABISizeOfType(g->target_data_ref, type->llvm_type));
70837154 assert(type->abi_align == 0 || type->abi_align == LLVMABIAlignmentOfType(g->target_data_ref, type->llvm_type));
70847155 return type->llvm_type;
70857156}
70867157
70877158ZigLLVMDIType *get_llvm_di_type(CodeGen *g, ZigType *type) {
7088 if (type->llvm_di_type != nullptr)
7089 return type->llvm_di_type;
7090 resolve_llvm_types(g, type);
7091 assert(type->llvm_type != nullptr);
7092 assert(type->llvm_di_type != nullptr);
7093 assert(type->abi_size == 0 || type->abi_size == LLVMABISizeOfType(g->target_data_ref, type->llvm_type));
7094 assert(type->abi_align == 0 || type->abi_align == LLVMABIAlignmentOfType(g->target_data_ref, type->llvm_type));
7159 assertNoError(type_resolve(g, type, ResolveStatusLLVMFull));
70957160 return type->llvm_di_type;
70967161}
test/stage1/behavior/struct.zig+11-5
......@@ -256,8 +256,8 @@ const Foo96Bits = packed struct {
256256
257257test "packed struct 24bits" {
258258 comptime {
259 expect(@sizeOf(Foo24Bits) == 3);
260 expect(@sizeOf(Foo96Bits) == 12);
259 expect(@sizeOf(Foo24Bits) == 4);
260 expect(@sizeOf(Foo96Bits) == 16);
261261 }
262262
263263 var value = Foo96Bits{
......@@ -291,16 +291,22 @@ test "packed struct 24bits" {
291291 expect(value.d == 1);
292292}
293293
294const Foo32Bits = packed struct {
295 field: u24,
296 pad: u8,
297};
298
294299const FooArray24Bits = packed struct {
295300 a: u16,
296 b: [2]Foo24Bits,
301 b: [2]Foo32Bits,
297302 c: u16,
298303};
299304
305// TODO revisit this test when doing https://github.com/ziglang/zig/issues/1512
300306test "packed array 24bits" {
301307 comptime {
302 expect(@sizeOf([9]Foo24Bits) == 9 * 3);
303 expect(@sizeOf(FooArray24Bits) == 2 + 2 * 3 + 2);
308 expect(@sizeOf([9]Foo32Bits) == 9 * 4);
309 expect(@sizeOf(FooArray24Bits) == 2 + 2 * 4 + 2);
304310 }
305311
306312 var bytes = []u8{0} ** (@sizeOf(FooArray24Bits) + 1);