authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-02 23:17:17-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2019-04-02 23:17:17-04:00
log85edf55b73acda669a2388949d7cf31118d70764
tree9b997e75f19a6379a284e65fa9d66519df214b0c
parent2f96c55095514de35a25ec2cf84a79e6c5e3a646
parent025a1475c45f0ba9d196e6584ea70d793a921c40
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #2135 from ziglang/decouple-llvm-types

decouple llvm types from zig types

9 files changed, 2017 insertions(+), 1689 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+32-34
......@@ -1067,8 +1067,10 @@ struct TypeStructField {
10671067 ZigType *type_entry;
10681068 size_t src_index;
10691069 size_t gen_index;
1070 uint32_t bit_offset_in_host; // offset from the memory at gen_index
1070 size_t offset; // byte offset from beginning of struct
10711071 AstNode *decl_node;
1072 uint32_t bit_offset_in_host; // offset from the memory at gen_index
1073 uint32_t host_int_bytes; // size of host integer
10721074};
10731075
10741076enum ResolveStatus {
......@@ -1077,6 +1079,8 @@ enum ResolveStatus {
10771079 ResolveStatusZeroBitsKnown,
10781080 ResolveStatusAlignmentKnown,
10791081 ResolveStatusSizeKnown,
1082 ResolveStatusLLVMFwdDecl,
1083 ResolveStatusLLVMFull,
10801084};
10811085
10821086struct ZigPackage {
......@@ -1101,14 +1105,13 @@ struct ZigTypeStruct {
11011105 AstNode *decl_node;
11021106 TypeStructField *fields;
11031107 ScopeDecls *decls_scope;
1104 uint64_t size_bytes;
11051108 HashMap<Buf *, TypeStructField *, buf_hash, buf_eql_buf> fields_by_name;
11061109 RootStruct *root_struct;
1110 uint32_t *host_int_bytes; // available for packed structs, indexed by gen_index
11071111
11081112 uint32_t src_field_count;
11091113 uint32_t gen_field_count;
11101114
1111 uint32_t abi_alignment; // known after ResolveStatusAlignmentKnown
11121115 ContainerLayout layout;
11131116 ResolveStatus resolve_status;
11141117
......@@ -1164,39 +1167,28 @@ bool type_ptr_eql(const ZigType *a, const ZigType *b);
11641167
11651168struct ZigTypeUnion {
11661169 AstNode *decl_node;
1167 ContainerLayout layout;
1168 uint32_t src_field_count;
1169 uint32_t gen_field_count;
11701170 TypeUnionField *fields;
1171 bool is_invalid; // true if any fields are invalid
1171 ScopeDecls *decls_scope;
1172 HashMap<Buf *, TypeUnionField *, buf_hash, buf_eql_buf> fields_by_name;
11721173 ZigType *tag_type; // always an enum or null
1173 LLVMTypeRef union_type_ref;
1174 LLVMTypeRef union_llvm_type;
1175 ZigType *most_aligned_union_member;
1176 size_t gen_union_index;
1177 size_t gen_tag_index;
1178 size_t union_abi_size;
11741179
1175 ScopeDecls *decls_scope;
1180 uint32_t src_field_count;
1181 uint32_t gen_field_count;
11761182
1177 // set this flag temporarily to detect infinite loops
1178 bool embedded_in_current;
1179 bool reported_infinite_err;
1180 // whether we've finished resolving it
1181 bool complete;
1183 ContainerLayout layout;
1184 ResolveStatus resolve_status;
11821185
1186 bool have_explicit_tag_type;
1187 bool resolve_loop_flag; // set this flag temporarily to detect infinite loops
1188 bool reported_infinite_err;
11831189 // whether any of the fields require comptime
11841190 // the value is not valid until zero_bits_known == true
11851191 bool requires_comptime;
1186
1187 bool zero_bits_loop_flag;
1188 bool zero_bits_known;
1189 uint32_t abi_alignment; // also figured out with zero_bits pass
1190
1191 size_t gen_union_index;
1192 size_t gen_tag_index;
1193
1194 bool have_explicit_tag_type;
1195
1196 uint32_t union_size_bytes;
1197 ZigType *most_aligned_union_member;
1198
1199 HashMap<Buf *, TypeUnionField *, buf_hash, buf_eql_buf> fields_by_name;
12001192};
12011193
12021194struct FnGenParamInfo {
......@@ -1277,8 +1269,11 @@ struct ZigType {
12771269 ZigTypeId id;
12781270 Buf name;
12791271
1280 LLVMTypeRef type_ref;
1281 ZigLLVMDIType *di_type;
1272 // These are not supposed to be accessed directly. They're
1273 // null during semantic analysis, memoized with get_llvm_type
1274 // and get_llvm_di_type
1275 LLVMTypeRef llvm_type;
1276 ZigLLVMDIType *llvm_di_type;
12821277
12831278 union {
12841279 ZigTypePointer pointer;
......@@ -1308,8 +1303,14 @@ struct ZigType {
13081303 ConstExprValue *cached_const_name_val;
13091304
13101305 OnePossibleValue one_possible_value;
1306 // Known after ResolveStatusAlignmentKnown.
1307 uint32_t abi_align;
1308 // The offset in bytes between consecutive array elements of this type. Known
1309 // after ResolveStatusSizeKnown.
1310 size_t abi_size;
1311 // Number of bits of information in this type. Known after ResolveStatusSizeKnown.
1312 size_t size_in_bits;
13111313
1312 bool zero_bits; // this is denormalized data
13131314 bool gen_h_loop_flag;
13141315};
13151316
......@@ -1700,11 +1701,9 @@ struct CodeGen {
17001701 ZigList<AstNode *> use_queue;
17011702 size_t use_queue_index;
17021703 ZigList<TimeEvent> timing_events;
1703 ZigList<ZigLLVMDIType **> error_di_types;
17041704 ZigList<AstNode *> tld_ref_source_node_stack;
17051705 ZigList<ZigFn *> inline_fns;
17061706 ZigList<ZigFn *> test_fns;
1707 ZigList<ZigLLVMDIEnumerator *> err_enumerators;
17081707 ZigList<ErrorTableEntry *> errors_by_index;
17091708 ZigList<CacheHash *> caches_to_release;
17101709 size_t largest_err_name_len;
......@@ -1896,7 +1895,6 @@ struct ZigVar {
18961895 AstNode *decl_node;
18971896 ZigLLVMDILocalVariable *di_loc_var;
18981897 size_t src_arg_index;
1899 size_t gen_arg_index;
19001898 Scope *parent_scope;
19011899 Scope *child_scope;
19021900 LLVMValueRef param_value_ref;
src/analyze.cpp+1555-1244
......@@ -19,14 +19,15 @@
1919
2020static const size_t default_backward_branch_quota = 1000;
2121
22static Error resolve_enum_type(CodeGen *g, ZigType *enum_type);
2322static Error resolve_struct_type(CodeGen *g, ZigType *struct_type);
2423
2524static Error ATTRIBUTE_MUST_USE resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type);
2625static Error ATTRIBUTE_MUST_USE resolve_struct_alignment(CodeGen *g, ZigType *struct_type);
2726static Error ATTRIBUTE_MUST_USE resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type);
2827static Error ATTRIBUTE_MUST_USE resolve_union_zero_bits(CodeGen *g, ZigType *union_type);
28static 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;
......@@ -264,6 +265,8 @@ bool type_is_resolved(ZigType *type_entry, ResolveStatus status) {
264265 zig_unreachable();
265266 case ZigTypeIdStruct:
266267 return type_entry->data.structure.resolve_status >= status;
268 case ZigTypeIdUnion:
269 return type_entry->data.unionation.resolve_status >= status;
267270 case ZigTypeIdEnum:
268271 switch (status) {
269272 case ResolveStatusUnstarted:
......@@ -276,20 +279,9 @@ bool type_is_resolved(ZigType *type_entry, ResolveStatus status) {
276279 return type_entry->data.enumeration.zero_bits_known;
277280 case ResolveStatusSizeKnown:
278281 return type_entry->data.enumeration.complete;
279 }
280 zig_unreachable();
281 case ZigTypeIdUnion:
282 switch (status) {
283 case ResolveStatusUnstarted:
284 return true;
285 case ResolveStatusInvalid:
286 zig_unreachable();
287 case ResolveStatusZeroBitsKnown:
288 return type_entry->data.unionation.zero_bits_known;
289 case ResolveStatusAlignmentKnown:
290 return type_entry->data.unionation.zero_bits_known;
291 case ResolveStatusSizeKnown:
292 return type_entry->data.unionation.complete;
282 case ResolveStatusLLVMFwdDecl:
283 case ResolveStatusLLVMFull:
284 return type_entry->llvm_di_type != nullptr;
293285 }
294286 zig_unreachable();
295287 case ZigTypeIdOpaque:
......@@ -325,49 +317,18 @@ bool type_is_complete(ZigType *type_entry) {
325317}
326318
327319uint64_t type_size(CodeGen *g, ZigType *type_entry) {
328 assert(type_is_complete(type_entry));
329
330 if (!type_has_bits(type_entry))
331 return 0;
332
333 if (type_entry->id == ZigTypeIdStruct && type_entry->data.structure.layout == ContainerLayoutPacked) {
334 uint64_t size_in_bits = type_size_bits(g, type_entry);
335 return (size_in_bits + 7) / 8;
336 } else if (type_entry->id == ZigTypeIdArray) {
337 ZigType *child_type = type_entry->data.array.child_type;
338 if (child_type->id == ZigTypeIdStruct &&
339 child_type->data.structure.layout == ContainerLayoutPacked)
340 {
341 uint64_t size_in_bits = type_size_bits(g, type_entry);
342 return (size_in_bits + 7) / 8;
343 }
344 }
345
346 return LLVMABISizeOfType(g->target_data_ref, type_entry->type_ref);
320 assert(type_is_resolved(type_entry, ResolveStatusSizeKnown));
321 return type_entry->abi_size;
347322}
348323
349324uint64_t type_size_bits(CodeGen *g, ZigType *type_entry) {
350 assert(type_is_complete(type_entry));
351
352 if (!type_has_bits(type_entry))
353 return 0;
354
355 if (type_entry->id == ZigTypeIdStruct) {
356 if (type_entry->data.structure.layout == ContainerLayoutPacked) {
357 uint64_t result = 0;
358 for (size_t i = 0; i < type_entry->data.structure.src_field_count; i += 1) {
359 result += type_size_bits(g, type_entry->data.structure.fields[i].type_entry);
360 }
361 return result;
362 } else if (type_entry->data.structure.layout == ContainerLayoutExtern) {
363 return type_size(g, type_entry) * 8;
364 }
365 } else if (type_entry->id == ZigTypeIdArray) {
366 ZigType *child_type = type_entry->data.array.child_type;
367 return type_entry->data.array.len * type_size_bits(g, child_type);
368 }
325 assert(type_is_resolved(type_entry, ResolveStatusSizeKnown));
326 return type_entry->size_in_bits;
327}
369328
370 return LLVMSizeOfTypeInBits(g->target_data_ref, type_entry->type_ref);
329uint32_t get_abi_alignment(CodeGen *g, ZigType *type_entry) {
330 assert(type_is_resolved(type_entry, ResolveStatusAlignmentKnown));
331 return type_entry->abi_align;
371332}
372333
373334static bool is_slice(ZigType *type) {
......@@ -385,16 +346,15 @@ ZigType *get_promise_type(CodeGen *g, ZigType *result_type) {
385346 return g->builtin_types.entry_promise;
386347 }
387348
388 ZigType *u8_ptr_type = get_pointer_to_type(g, g->builtin_types.entry_u8, false);
389349 ZigType *entry = new_type_table_entry(ZigTypeIdPromise);
390 entry->type_ref = u8_ptr_type->type_ref;
391 entry->zero_bits = false;
350 entry->abi_size = g->builtin_types.entry_usize->abi_size;
351 entry->size_in_bits = g->builtin_types.entry_usize->size_in_bits;
352 entry->abi_align = g->builtin_types.entry_usize->abi_align;
392353 entry->data.promise.result_type = result_type;
393354 buf_init_from_str(&entry->name, "promise");
394355 if (result_type != nullptr) {
395356 buf_appendf(&entry->name, "->%s", buf_ptr(&result_type->name));
396357 }
397 entry->di_type = u8_ptr_type->di_type;
398358
399359 if (result_type != nullptr) {
400360 result_type->promise_parent = entry;
......@@ -496,36 +456,15 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons
496456
497457 assert(child_type->id != ZigTypeIdInvalid);
498458
499 entry->zero_bits = !type_has_bits(child_type);
500
501 if (!entry->zero_bits) {
502 if (is_const || is_volatile || byte_alignment != 0 || ptr_len != PtrLenSingle ||
503 bit_offset_in_host != 0 || allow_zero)
504 {
505 ZigType *peer_type = get_pointer_to_type_extra(g, child_type, false, false,
506 PtrLenSingle, 0, 0, host_int_bytes, false);
507 entry->type_ref = peer_type->type_ref;
508 entry->di_type = peer_type->di_type;
509 } else {
510 if (host_int_bytes == 0) {
511 entry->type_ref = LLVMPointerType(child_type->type_ref, 0);
512 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, entry->type_ref);
513 uint64_t debug_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, entry->type_ref);
514 assert(child_type->di_type);
515 entry->di_type = ZigLLVMCreateDebugPointerType(g->dbuilder, child_type->di_type,
516 debug_size_in_bits, debug_align_in_bits, buf_ptr(&entry->name));
517 } else {
518 ZigType *host_int_type = get_int_type(g, false, host_int_bytes * 8);
519 entry->type_ref = LLVMPointerType(host_int_type->type_ref, 0);
520 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, host_int_type->type_ref);
521 uint64_t debug_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, host_int_type->type_ref);
522 entry->di_type = ZigLLVMCreateDebugPointerType(g->dbuilder, host_int_type->di_type,
523 debug_size_in_bits, debug_align_in_bits, buf_ptr(&entry->name));
524 }
525 }
459 if (type_has_bits(child_type)) {
460 entry->abi_size = g->builtin_types.entry_usize->abi_size;
461 entry->size_in_bits = g->builtin_types.entry_usize->size_in_bits;
462 entry->abi_align = g->builtin_types.entry_usize->abi_align;
526463 } else {
527464 assert(byte_alignment == 0);
528 entry->di_type = g->builtin_types.entry_void->di_type;
465 entry->abi_size = 0;
466 entry->size_in_bits = 0;
467 entry->abi_align = 0;
529468 }
530469
531470 entry->data.pointer.ptr_len = ptr_len;
......@@ -586,89 +525,60 @@ ZigType *get_promise_frame_type(CodeGen *g, ZigType *return_type) {
586525}
587526
588527ZigType *get_optional_type(CodeGen *g, ZigType *child_type) {
589 if (child_type->optional_parent) {
590 ZigType *entry = child_type->optional_parent;
591 return entry;
592 } else {
593 assertNoError(ensure_complete_type(g, child_type));
594
595 ZigType *entry = new_type_table_entry(ZigTypeIdOptional);
596 assert(child_type->type_ref || child_type->zero_bits);
597
598 buf_resize(&entry->name, 0);
599 buf_appendf(&entry->name, "?%s", buf_ptr(&child_type->name));
600
601 if (child_type->zero_bits) {
602 entry->type_ref = LLVMInt1Type();
603 entry->di_type = g->builtin_types.entry_bool->di_type;
604 } else if (type_is_nonnull_ptr(child_type) || child_type->id == ZigTypeIdErrorSet) {
605 assert(child_type->di_type);
606 // this is an optimization but also is necessary for calling C
607 // functions where all pointers are maybe pointers
608 // function types are technically pointers
609 entry->type_ref = child_type->type_ref;
610 entry->di_type = child_type->di_type;
611 if (entry->di_type == g->builtin_types.entry_global_error_set->di_type) {
612 g->error_di_types.append(&entry->di_type);
613 }
614 } else {
615 assert(child_type->di_type);
616 // create a struct with a boolean whether this is the null value
617 LLVMTypeRef elem_types[] = {
618 child_type->type_ref,
619 LLVMInt1Type(),
620 };
621 entry->type_ref = LLVMStructType(elem_types, 2, false);
622
623
624 ZigLLVMDIScope *compile_unit_scope = ZigLLVMCompileUnitToScope(g->compile_unit);
625 ZigLLVMDIFile *di_file = nullptr;
626 unsigned line = 0;
627 entry->di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
628 ZigLLVMTag_DW_structure_type(), buf_ptr(&entry->name),
629 compile_unit_scope, di_file, line);
630
631 uint64_t val_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, child_type->type_ref);
632 uint64_t val_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, child_type->type_ref);
633 uint64_t val_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, entry->type_ref, 0);
634
635 ZigType *bool_type = g->builtin_types.entry_bool;
636 uint64_t maybe_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, bool_type->type_ref);
637 uint64_t maybe_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, bool_type->type_ref);
638 uint64_t maybe_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, entry->type_ref, 1);
639
640 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, entry->type_ref);
641 uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, entry->type_ref);
642
643 ZigLLVMDIType *di_element_types[] = {
644 ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(entry->di_type),
645 "val", di_file, line,
646 val_debug_size_in_bits,
647 val_debug_align_in_bits,
648 val_offset_in_bits,
649 0, child_type->di_type),
650 ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(entry->di_type),
651 "maybe", di_file, line,
652 maybe_debug_size_in_bits,
653 maybe_debug_align_in_bits,
654 maybe_offset_in_bits,
655 0, bool_type->di_type),
656 };
657 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
658 compile_unit_scope,
659 buf_ptr(&entry->name),
660 di_file, line, debug_size_in_bits, debug_align_in_bits, 0,
661 nullptr, di_element_types, 2, 0, nullptr, "");
528 if (child_type->optional_parent != nullptr) {
529 return child_type->optional_parent;
530 }
662531
663 ZigLLVMReplaceTemporary(g->dbuilder, entry->di_type, replacement_di_type);
664 entry->di_type = replacement_di_type;
665 }
532 assert(type_is_resolved(child_type, ResolveStatusSizeKnown));
666533
667 entry->data.maybe.child_type = child_type;
534 ZigType *entry = new_type_table_entry(ZigTypeIdOptional);
668535
669 child_type->optional_parent = entry;
670 return entry;
536 buf_resize(&entry->name, 0);
537 buf_appendf(&entry->name, "?%s", buf_ptr(&child_type->name));
538
539 if (!type_has_bits(child_type)) {
540 entry->size_in_bits = g->builtin_types.entry_bool->size_in_bits;
541 entry->abi_size = g->builtin_types.entry_bool->abi_size;
542 entry->abi_align = g->builtin_types.entry_bool->abi_align;
543 } else if (type_is_nonnull_ptr(child_type) || child_type->id == ZigTypeIdErrorSet) {
544 // This is an optimization but also is necessary for calling C
545 // functions where all pointers are optional pointers.
546 // Function types are technically pointers.
547 entry->size_in_bits = child_type->size_in_bits;
548 entry->abi_size = child_type->abi_size;
549 entry->abi_align = child_type->abi_align;
550 } else {
551 // This value only matters if the type is legal in a packed struct, which is not
552 // true for optional types which did not fit the above 2 categories (zero bit child type,
553 // or nonnull ptr child type, or error set child type).
554 entry->size_in_bits = child_type->size_in_bits + 1;
555
556 // We're going to make a struct with the child type as the first field,
557 // and a bool as the second. Since the child type's abi alignment is guaranteed
558 // to be >= the bool's abi size (1 byte), the added size is exactly equal to the
559 // child type's ABI alignment.
560 assert(child_type->abi_align >= g->builtin_types.entry_bool->abi_size);
561 entry->abi_align = child_type->abi_align;
562 entry->abi_size = child_type->abi_size + child_type->abi_align;
671563 }
564
565 entry->data.maybe.child_type = child_type;
566
567 child_type->optional_parent = entry;
568 return entry;
569}
570
571static size_t align_forward(size_t addr, size_t alignment) {
572 return (addr + alignment - 1) & ~(alignment - 1);
573}
574
575static size_t next_field_offset(size_t offset, size_t align_from_zero, size_t field_size, size_t next_field_align) {
576 // Convert offset to a pretend address which has the specified alignment.
577 size_t addr = offset + align_from_zero;
578 // March the address forward to respect the field alignment.
579 size_t aligned_addr = align_forward(addr + field_size, next_field_align);
580 // Convert back from pretend address to offset.
581 return aligned_addr - align_from_zero;
672582}
673583
674584ZigType *get_error_union_type(CodeGen *g, ZigType *err_set_type, ZigType *payload_type) {
......@@ -686,8 +596,7 @@ ZigType *get_error_union_type(CodeGen *g, ZigType *err_set_type, ZigType *payloa
686596 }
687597
688598 ZigType *entry = new_type_table_entry(ZigTypeIdErrorUnion);
689 assert(payload_type->di_type);
690 assert(type_is_complete(payload_type));
599 assert(type_is_resolved(payload_type, ResolveStatusSizeKnown));
691600
692601 buf_resize(&entry->name, 0);
693602 buf_appendf(&entry->name, "%s!%s", buf_ptr(&err_set_type->name), buf_ptr(&payload_type->name));
......@@ -697,68 +606,29 @@ ZigType *get_error_union_type(CodeGen *g, ZigType *err_set_type, ZigType *payloa
697606
698607 if (!type_has_bits(payload_type)) {
699608 if (type_has_bits(err_set_type)) {
700 entry->type_ref = err_set_type->type_ref;
701 entry->di_type = err_set_type->di_type;
702 g->error_di_types.append(&entry->di_type);
609 entry->size_in_bits = err_set_type->size_in_bits;
610 entry->abi_size = err_set_type->abi_size;
611 entry->abi_align = err_set_type->abi_align;
703612 } else {
704 entry->zero_bits = true;
705 entry->di_type = g->builtin_types.entry_void->di_type;
613 entry->size_in_bits = 0;
614 entry->abi_size = 0;
615 entry->abi_align = 0;
706616 }
707617 } else if (!type_has_bits(err_set_type)) {
708 entry->type_ref = payload_type->type_ref;
709 entry->di_type = payload_type->di_type;
618 entry->size_in_bits = payload_type->size_in_bits;
619 entry->abi_size = payload_type->abi_size;
620 entry->abi_align = payload_type->abi_align;
710621 } else {
711 LLVMTypeRef elem_types[] = {
712 err_set_type->type_ref,
713 payload_type->type_ref,
714 };
715 entry->type_ref = LLVMStructType(elem_types, 2, false);
716
717 ZigLLVMDIScope *compile_unit_scope = ZigLLVMCompileUnitToScope(g->compile_unit);
718 ZigLLVMDIFile *di_file = nullptr;
719 unsigned line = 0;
720 entry->di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
721 ZigLLVMTag_DW_structure_type(), buf_ptr(&entry->name),
722 compile_unit_scope, di_file, line);
723
724 uint64_t tag_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, err_set_type->type_ref);
725 uint64_t tag_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, err_set_type->type_ref);
726 uint64_t tag_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, entry->type_ref, err_union_err_index);
727
728 uint64_t value_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, payload_type->type_ref);
729 uint64_t value_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, payload_type->type_ref);
730 uint64_t value_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, entry->type_ref,
731 err_union_payload_index);
732
733 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, entry->type_ref);
734 uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, entry->type_ref);
735
736 ZigLLVMDIType *di_element_types[] = {
737 ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(entry->di_type),
738 "tag", di_file, line,
739 tag_debug_size_in_bits,
740 tag_debug_align_in_bits,
741 tag_offset_in_bits,
742 0, err_set_type->di_type),
743 ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(entry->di_type),
744 "value", di_file, line,
745 value_debug_size_in_bits,
746 value_debug_align_in_bits,
747 value_offset_in_bits,
748 0, payload_type->di_type),
749 };
750
751 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
752 compile_unit_scope,
753 buf_ptr(&entry->name),
754 di_file, line,
755 debug_size_in_bits,
756 debug_align_in_bits,
757 0,
758 nullptr, di_element_types, 2, 0, nullptr, "");
759
760 ZigLLVMReplaceTemporary(g->dbuilder, entry->di_type, replacement_di_type);
761 entry->di_type = replacement_di_type;
622 entry->abi_align = max(err_set_type->abi_align, payload_type->abi_align);
623 size_t field_sizes[2];
624 size_t field_aligns[2];
625 field_sizes[err_union_err_index] = err_set_type->abi_size;
626 field_aligns[err_union_err_index] = err_set_type->abi_align;
627 field_sizes[err_union_payload_index] = payload_type->abi_size;
628 field_aligns[err_union_payload_index] = payload_type->abi_align;
629 size_t field2_offset = next_field_offset(0, entry->abi_align, field_sizes[0], field_aligns[1]);
630 entry->abi_size = next_field_offset(field2_offset, entry->abi_align, field_sizes[1], entry->abi_align);
631 entry->size_in_bits = entry->abi_size * 8;
762632 }
763633
764634 g->type_table.put(type_id, entry);
......@@ -772,31 +642,20 @@ ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size) {
772642 type_id.data.array.size = array_size;
773643 auto existing_entry = g->type_table.maybe_get(type_id);
774644 if (existing_entry) {
775 ZigType *entry = existing_entry->value;
776 return entry;
645 return existing_entry->value;
777646 }
778647
779 assertNoError(ensure_complete_type(g, child_type));
648 assert(type_is_resolved(child_type, ResolveStatusSizeKnown));
780649
781650 ZigType *entry = new_type_table_entry(ZigTypeIdArray);
782 entry->zero_bits = (array_size == 0) || child_type->zero_bits;
783651
784652 buf_resize(&entry->name, 0);
785653 buf_appendf(&entry->name, "[%" ZIG_PRI_u64 "]%s", array_size, buf_ptr(&child_type->name));
786654
787 if (entry->zero_bits) {
788 entry->di_type = ZigLLVMCreateDebugArrayType(g->dbuilder, 0,
789 0, child_type->di_type, 0);
790 } else {
791 entry->type_ref = child_type->type_ref ? LLVMArrayType(child_type->type_ref,
792 (unsigned int)array_size) : nullptr;
793
794 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, entry->type_ref);
795 uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, entry->type_ref);
655 entry->size_in_bits = child_type->size_in_bits * array_size;
656 entry->abi_align = child_type->abi_align;
657 entry->abi_size = child_type->abi_size * array_size;
796658
797 entry->di_type = ZigLLVMCreateDebugArrayType(g->dbuilder, debug_size_in_bits,
798 debug_align_in_bits, child_type->di_type, (int)array_size);
799 }
800659 entry->data.array.child_type = child_type;
801660 entry->data.array.len = array_size;
802661
......@@ -804,11 +663,27 @@ ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size) {
804663 return entry;
805664}
806665
807static void slice_type_common_init(CodeGen *g, ZigType *pointer_type, ZigType *entry) {
666ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type) {
667 assert(ptr_type->id == ZigTypeIdPointer);
668 assert(ptr_type->data.pointer.ptr_len == PtrLenUnknown);
669
670 ZigType **parent_pointer = &ptr_type->data.pointer.slice_parent;
671 if (*parent_pointer) {
672 return *parent_pointer;
673 }
674
675 ZigType *entry = new_type_table_entry(ZigTypeIdStruct);
676
677 // replace the & with [] to go from a ptr type name to a slice type name
678 buf_resize(&entry->name, 0);
679 size_t name_offset = (ptr_type->data.pointer.ptr_len == PtrLenSingle) ? 1 : 3;
680 buf_appendf(&entry->name, "[]%s", buf_ptr(&ptr_type->name) + name_offset);
681
808682 unsigned element_count = 2;
809683 Buf *ptr_field_name = buf_create_from_str("ptr");
810684 Buf *len_field_name = buf_create_from_str("len");
811685
686 entry->data.structure.resolve_status = ResolveStatusSizeKnown;
812687 entry->data.structure.layout = ContainerLayoutAuto;
813688 entry->data.structure.is_slice = true;
814689 entry->data.structure.src_field_count = element_count;
......@@ -816,7 +691,7 @@ static void slice_type_common_init(CodeGen *g, ZigType *pointer_type, ZigType *e
816691 entry->data.structure.fields = allocate<TypeStructField>(element_count);
817692 entry->data.structure.fields_by_name.init(element_count);
818693 entry->data.structure.fields[slice_ptr_index].name = ptr_field_name;
819 entry->data.structure.fields[slice_ptr_index].type_entry = pointer_type;
694 entry->data.structure.fields[slice_ptr_index].type_entry = ptr_type;
820695 entry->data.structure.fields[slice_ptr_index].src_index = slice_ptr_index;
821696 entry->data.structure.fields[slice_ptr_index].gen_index = 0;
822697 entry->data.structure.fields[slice_len_index].name = len_field_name;
......@@ -827,28 +702,20 @@ static void slice_type_common_init(CodeGen *g, ZigType *pointer_type, ZigType *e
827702 entry->data.structure.fields_by_name.put(ptr_field_name, &entry->data.structure.fields[slice_ptr_index]);
828703 entry->data.structure.fields_by_name.put(len_field_name, &entry->data.structure.fields[slice_len_index]);
829704
830 if (!type_has_bits(pointer_type->data.pointer.child_type)) {
705 switch (type_requires_comptime(g, ptr_type)) {
706 case ReqCompTimeInvalid:
707 zig_unreachable();
708 case ReqCompTimeNo:
709 break;
710 case ReqCompTimeYes:
711 entry->data.structure.requires_comptime = true;
712 }
713
714 if (!type_has_bits(ptr_type)) {
831715 entry->data.structure.gen_field_count = 1;
832716 entry->data.structure.fields[slice_ptr_index].gen_index = SIZE_MAX;
833717 entry->data.structure.fields[slice_len_index].gen_index = 0;
834718 }
835}
836
837ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type) {
838 assert(ptr_type->id == ZigTypeIdPointer);
839 assert(ptr_type->data.pointer.ptr_len == PtrLenUnknown);
840
841 ZigType **parent_pointer = &ptr_type->data.pointer.slice_parent;
842 if (*parent_pointer) {
843 return *parent_pointer;
844 }
845
846 ZigType *entry = new_type_table_entry(ZigTypeIdStruct);
847
848 // replace the & with [] to go from a ptr type name to a slice type name
849 buf_resize(&entry->name, 0);
850 size_t name_offset = (ptr_type->data.pointer.ptr_len == PtrLenSingle) ? 1 : 3;
851 buf_appendf(&entry->name, "[]%s", buf_ptr(&ptr_type->name) + name_offset);
852719
853720 ZigType *child_type = ptr_type->data.pointer.child_type;
854721 if (ptr_type->data.pointer.is_const || ptr_type->data.pointer.is_volatile ||
......@@ -858,134 +725,24 @@ ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type) {
858725 PtrLenUnknown, 0, 0, 0, false);
859726 ZigType *peer_slice_type = get_slice_type(g, peer_ptr_type);
860727
861 slice_type_common_init(g, ptr_type, entry);
862
863 entry->type_ref = peer_slice_type->type_ref;
864 entry->di_type = peer_slice_type->di_type;
865 entry->data.structure.resolve_status = ResolveStatusSizeKnown;
866 entry->data.structure.abi_alignment = peer_slice_type->data.structure.abi_alignment;
728 entry->size_in_bits = peer_slice_type->size_in_bits;
729 entry->abi_size = peer_slice_type->abi_size;
730 entry->abi_align = peer_slice_type->abi_align;
867731
868732 *parent_pointer = entry;
869733 return entry;
870734 }
871735
872 // If the child type is []const T then we need to make sure the type ref
873 // and debug info is the same as if the child type were []T.
874 if (is_slice(child_type)) {
875 ZigType *child_ptr_type = child_type->data.structure.fields[slice_ptr_index].type_entry;
876 assert(child_ptr_type->id == ZigTypeIdPointer);
877 if (child_ptr_type->data.pointer.is_const || child_ptr_type->data.pointer.is_volatile ||
878 child_ptr_type->data.pointer.explicit_alignment != 0 || child_ptr_type->data.pointer.allow_zero)
879 {
880 ZigType *grand_child_type = child_ptr_type->data.pointer.child_type;
881 ZigType *bland_child_ptr_type = get_pointer_to_type_extra(g, grand_child_type, false, false,
882 PtrLenUnknown, 0, 0, 0, false);
883 ZigType *bland_child_slice = get_slice_type(g, bland_child_ptr_type);
884 ZigType *peer_ptr_type = get_pointer_to_type_extra(g, bland_child_slice, false, false,
885 PtrLenUnknown, 0, 0, 0, false);
886 ZigType *peer_slice_type = get_slice_type(g, peer_ptr_type);
887
888 entry->type_ref = peer_slice_type->type_ref;
889 entry->di_type = peer_slice_type->di_type;
890 entry->data.structure.abi_alignment = peer_slice_type->data.structure.abi_alignment;
891 }
892 }
893
894 slice_type_common_init(g, ptr_type, entry);
895
896 if (!entry->type_ref) {
897 entry->type_ref = LLVMStructCreateNamed(LLVMGetGlobalContext(), buf_ptr(&entry->name));
898
899 ZigLLVMDIScope *compile_unit_scope = ZigLLVMCompileUnitToScope(g->compile_unit);
900 ZigLLVMDIFile *di_file = nullptr;
901 unsigned line = 0;
902 entry->di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
903 ZigLLVMTag_DW_structure_type(), buf_ptr(&entry->name),
904 compile_unit_scope, di_file, line);
905
906 if (child_type->zero_bits) {
907 LLVMTypeRef element_types[] = {
908 g->builtin_types.entry_usize->type_ref,
909 };
910 LLVMStructSetBody(entry->type_ref, element_types, 1, false);
911
912 ZigType *usize_type = g->builtin_types.entry_usize;
913 uint64_t len_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, usize_type->type_ref);
914 uint64_t len_debug_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, usize_type->type_ref);
915 uint64_t len_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, entry->type_ref, 0);
916
917 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, entry->type_ref);
918 uint64_t debug_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, entry->type_ref);
919
920 ZigLLVMDIType *di_element_types[] = {
921 ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(entry->di_type),
922 "len", di_file, line,
923 len_debug_size_in_bits,
924 len_debug_align_in_bits,
925 len_offset_in_bits,
926 0, usize_type->di_type),
927 };
928 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
929 compile_unit_scope,
930 buf_ptr(&entry->name),
931 di_file, line, debug_size_in_bits, debug_align_in_bits, 0,
932 nullptr, di_element_types, 1, 0, nullptr, "");
933
934 ZigLLVMReplaceTemporary(g->dbuilder, entry->di_type, replacement_di_type);
935 entry->di_type = replacement_di_type;
936
937 entry->data.structure.abi_alignment = LLVMABIAlignmentOfType(g->target_data_ref, usize_type->type_ref);
938 } else {
939 unsigned element_count = 2;
940 LLVMTypeRef element_types[] = {
941 ptr_type->type_ref,
942 g->builtin_types.entry_usize->type_ref,
943 };
944 LLVMStructSetBody(entry->type_ref, element_types, element_count, false);
945
946
947 uint64_t ptr_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, ptr_type->type_ref);
948 uint64_t ptr_debug_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, ptr_type->type_ref);
949 uint64_t ptr_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, entry->type_ref, 0);
950
951 ZigType *usize_type = g->builtin_types.entry_usize;
952 uint64_t len_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, usize_type->type_ref);
953 uint64_t len_debug_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, usize_type->type_ref);
954 uint64_t len_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, entry->type_ref, 1);
955
956 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, entry->type_ref);
957 uint64_t debug_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, entry->type_ref);
958
959 ZigLLVMDIType *di_element_types[] = {
960 ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(entry->di_type),
961 "ptr", di_file, line,
962 ptr_debug_size_in_bits,
963 ptr_debug_align_in_bits,
964 ptr_offset_in_bits,
965 0, ptr_type->di_type),
966 ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(entry->di_type),
967 "len", di_file, line,
968 len_debug_size_in_bits,
969 len_debug_align_in_bits,
970 len_offset_in_bits,
971 0, usize_type->di_type),
972 };
973 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
974 compile_unit_scope,
975 buf_ptr(&entry->name),
976 di_file, line, debug_size_in_bits, debug_align_in_bits, 0,
977 nullptr, di_element_types, 2, 0, nullptr, "");
978
979 ZigLLVMReplaceTemporary(g->dbuilder, entry->di_type, replacement_di_type);
980 entry->di_type = replacement_di_type;
981
982 entry->data.structure.abi_alignment = LLVMABIAlignmentOfType(g->target_data_ref, entry->type_ref);
983 }
736 if (type_has_bits(ptr_type)) {
737 entry->size_in_bits = ptr_type->size_in_bits + g->builtin_types.entry_usize->size_in_bits;
738 entry->abi_size = ptr_type->abi_size + g->builtin_types.entry_usize->abi_size;
739 entry->abi_align = ptr_type->abi_align;
740 } else {
741 entry->size_in_bits = g->builtin_types.entry_usize->size_in_bits;
742 entry->abi_size = g->builtin_types.entry_usize->abi_size;
743 entry->abi_align = g->builtin_types.entry_usize->abi_align;
984744 }
985745
986
987 entry->data.structure.resolve_status = ResolveStatusSizeKnown;
988
989746 *parent_pointer = entry;
990747 return entry;
991748}
......@@ -998,15 +755,20 @@ ZigType *get_opaque_type(CodeGen *g, Scope *scope, AstNode *source_node, const c
998755 ZigType *import = scope ? get_scope_import(scope) : nullptr;
999756 unsigned line = source_node ? (unsigned)(source_node->line + 1) : 0;
1000757
1001 entry->type_ref = LLVMInt8Type();
1002 entry->di_type = ZigLLVMCreateDebugForwardDeclType(g->dbuilder,
758 entry->llvm_type = LLVMInt8Type();
759 entry->llvm_di_type = ZigLLVMCreateDebugForwardDeclType(g->dbuilder,
1003760 ZigLLVMTag_DW_structure_type(), full_name,
1004761 import ? ZigLLVMFileToScope(import->data.structure.root_struct->di_file) : nullptr,
1005762 import ? import->data.structure.root_struct->di_file : nullptr,
1006763 line);
1007 entry->zero_bits = false;
1008764 entry->data.opaque.bare_name = bare_name;
1009765
766 // The actual size is unknown, but the value must not be 0 because that
767 // is how type_has_bits is determined.
768 entry->abi_size = SIZE_MAX;
769 entry->size_in_bits = SIZE_MAX;
770 entry->abi_align = 1;
771
1010772 return entry;
1011773}
1012774
......@@ -1018,7 +780,6 @@ ZigType *get_bound_fn_type(CodeGen *g, ZigFn *fn_entry) {
1018780
1019781 ZigType *bound_fn_type = new_type_table_entry(ZigTypeIdBoundFn);
1020782 bound_fn_type->data.bound_fn.fn_type = fn_type;
1021 bound_fn_type->zero_bits = true;
1022783
1023784 buf_resize(&bound_fn_type->name, 0);
1024785 buf_appendf(&bound_fn_type->name, "(bound %s)", buf_ptr(&fn_type->name));
......@@ -1114,8 +875,6 @@ ZigType *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
1114875 ZigType *fn_type = new_type_table_entry(ZigTypeIdFn);
1115876 fn_type->data.fn.fn_type_id = *fn_type_id;
1116877
1117 bool skip_debug_info = false;
1118
1119878 // populate the name of the type
1120879 buf_resize(&fn_type->name, 0);
1121880 if (fn_type->data.fn.fn_type_id.cc == CallingConventionAsync) {
......@@ -1133,8 +892,6 @@ ZigType *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
1133892 const char *comma = (i == 0) ? "" : ", ";
1134893 const char *noalias_str = param_info->is_noalias ? "noalias " : "";
1135894 buf_appendf(&fn_type->name, "%s%s%s", comma, noalias_str, buf_ptr(&param_type->name));
1136
1137 skip_debug_info = skip_debug_info || !param_type->di_type;
1138895 }
1139896
1140897 if (fn_type_id->is_var_args) {
......@@ -1146,116 +903,11 @@ ZigType *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
1146903 buf_appendf(&fn_type->name, " align(%" PRIu32 ")", fn_type_id->alignment);
1147904 }
1148905 buf_appendf(&fn_type->name, " %s", buf_ptr(&fn_type_id->return_type->name));
1149 skip_debug_info = skip_debug_info || !fn_type_id->return_type->di_type;
1150
1151 // next, loop over the parameters again and compute debug information
1152 // and codegen information
1153 if (!skip_debug_info) {
1154 bool first_arg_return = want_first_arg_sret(g, fn_type_id);
1155 bool is_async = fn_type_id->cc == CallingConventionAsync;
1156 bool is_c_abi = fn_type_id->cc == CallingConventionC;
1157 bool prefix_arg_error_return_trace = g->have_err_ret_tracing && fn_type_can_fail(fn_type_id);
1158 // +1 for maybe making the first argument the return value
1159 // +1 for maybe first argument the error return trace
1160 // +2 for maybe arguments async allocator and error code pointer
1161 ZigList<LLVMTypeRef> gen_param_types = {};
1162 // +1 because 0 is the return type and
1163 // +1 for maybe making first arg ret val and
1164 // +1 for maybe first argument the error return trace
1165 // +2 for maybe arguments async allocator and error code pointer
1166 ZigList<ZigLLVMDIType *> param_di_types = {};
1167 param_di_types.append(fn_type_id->return_type->di_type);
1168 ZigType *gen_return_type;
1169 if (is_async) {
1170 gen_return_type = get_pointer_to_type(g, g->builtin_types.entry_u8, false);
1171 } else if (!type_has_bits(fn_type_id->return_type)) {
1172 gen_return_type = g->builtin_types.entry_void;
1173 } else if (first_arg_return) {
1174 ZigType *gen_type = get_pointer_to_type(g, fn_type_id->return_type, false);
1175 gen_param_types.append(gen_type->type_ref);
1176 param_di_types.append(gen_type->di_type);
1177 gen_return_type = g->builtin_types.entry_void;
1178 } else {
1179 gen_return_type = fn_type_id->return_type;
1180 }
1181 fn_type->data.fn.gen_return_type = gen_return_type;
1182
1183 if (prefix_arg_error_return_trace) {
1184 ZigType *gen_type = get_ptr_to_stack_trace_type(g);
1185 gen_param_types.append(gen_type->type_ref);
1186 param_di_types.append(gen_type->di_type);
1187 }
1188 if (is_async) {
1189 {
1190 // async allocator param
1191 ZigType *gen_type = fn_type_id->async_allocator_type;
1192 gen_param_types.append(gen_type->type_ref);
1193 param_di_types.append(gen_type->di_type);
1194 }
1195
1196 {
1197 // error code pointer
1198 ZigType *gen_type = get_pointer_to_type(g, g->builtin_types.entry_global_error_set, false);
1199 gen_param_types.append(gen_type->type_ref);
1200 param_di_types.append(gen_type->di_type);
1201 }
1202 }
1203
1204 fn_type->data.fn.gen_param_info = allocate<FnGenParamInfo>(fn_type_id->param_count);
1205 for (size_t i = 0; i < fn_type_id->param_count; i += 1) {
1206 FnTypeParamInfo *src_param_info = &fn_type->data.fn.fn_type_id.param_info[i];
1207 ZigType *type_entry = src_param_info->type;
1208 FnGenParamInfo *gen_param_info = &fn_type->data.fn.gen_param_info[i];
1209
1210 gen_param_info->src_index = i;
1211 gen_param_info->gen_index = SIZE_MAX;
1212
1213 if ((err = type_resolve(g, type_entry, ResolveStatusZeroBitsKnown)))
1214 return g->builtin_types.entry_invalid;
1215
1216 if (is_c_abi) {
1217 if ((err = type_resolve(g, type_entry, ResolveStatusSizeKnown)))
1218 return g->builtin_types.entry_invalid;
1219 continue;
1220 }
1221906
1222 if (type_has_bits(type_entry)) {
1223 ZigType *gen_type;
1224 if (handle_is_ptr(type_entry)) {
1225 gen_type = get_pointer_to_type(g, type_entry, true);
1226 gen_param_info->is_byval = true;
1227 } else {
1228 gen_type = type_entry;
1229 }
1230 gen_param_info->gen_index = gen_param_types.length;
1231 gen_param_info->type = gen_type;
1232 gen_param_types.append(gen_type->type_ref);
1233
1234 param_di_types.append(gen_type->di_type);
1235 }
1236 }
1237
1238 if (is_c_abi) {
1239 FnWalk fn_walk = {};
1240 fn_walk.id = FnWalkIdTypes;
1241 fn_walk.data.types.param_di_types = &param_di_types;
1242 fn_walk.data.types.gen_param_types = &gen_param_types;
1243 walk_function_params(g, fn_type, &fn_walk);
1244 }
1245
1246 fn_type->data.fn.gen_param_count = gen_param_types.length;
1247
1248 for (size_t i = 0; i < gen_param_types.length; i += 1) {
1249 assert(gen_param_types.items[i] != nullptr);
1250 }
1251 fn_type->data.fn.raw_type_ref = LLVMFunctionType(gen_return_type->type_ref,
1252 gen_param_types.items, (unsigned int)gen_param_types.length, fn_type_id->is_var_args);
1253 fn_type->type_ref = LLVMPointerType(fn_type->data.fn.raw_type_ref, 0);
1254 fn_type->data.fn.raw_di_type = ZigLLVMCreateSubroutineType(g->dbuilder, param_di_types.items, (int)param_di_types.length, 0);
1255 fn_type->di_type = ZigLLVMCreateDebugPointerType(g->dbuilder, fn_type->data.fn.raw_di_type,
1256 LLVMStoreSizeOfType(g->target_data_ref, fn_type->type_ref),
1257 LLVMABIAlignmentOfType(g->target_data_ref, fn_type->type_ref), "");
1258 }
907 // The fn_type is a pointer; not to be confused with the raw function type.
908 fn_type->size_in_bits = g->builtin_types.entry_usize->size_in_bits;
909 fn_type->abi_size = g->builtin_types.entry_usize->abi_size;
910 fn_type->abi_align = g->builtin_types.entry_usize->abi_align;
1259911
1260912 g->fn_type_table.put(&fn_type->data.fn.fn_type_id, fn_type);
1261913
......@@ -1282,14 +934,6 @@ static ZigType *get_root_container_type(CodeGen *g, const char *full_name, Buf *
1282934 entry->data.structure.decls_scope = create_decls_scope(g, nullptr, nullptr, entry, entry, bare_name);
1283935 entry->data.structure.root_struct = root_struct;
1284936 entry->data.structure.layout = ContainerLayoutAuto;
1285 entry->type_ref = LLVMStructCreateNamed(LLVMGetGlobalContext(), full_name);
1286
1287 size_t line = 0; // root therefore first line
1288 unsigned dwarf_kind = ZigLLVMTag_DW_structure_type();
1289
1290 entry->di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
1291 dwarf_kind, full_name,
1292 ZigLLVMFileToScope(root_struct->di_file), root_struct->di_file, (unsigned)(line + 1));
1293937
1294938 buf_init_from_str(&entry->name, full_name);
1295939 return entry;
......@@ -1316,16 +960,6 @@ ZigType *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind
1316960 break;
1317961 }
1318962
1319 size_t line = decl_node ? decl_node->line : 0;
1320 unsigned dwarf_kind = ZigLLVMTag_DW_structure_type();
1321
1322 ZigType *import = get_scope_import(scope);
1323 entry->type_ref = LLVMStructCreateNamed(LLVMGetGlobalContext(), full_name);
1324 entry->di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
1325 dwarf_kind, full_name,
1326 ZigLLVMFileToScope(import->data.structure.root_struct->di_file),
1327 import->data.structure.root_struct->di_file, (unsigned)(line + 1));
1328
1329963 buf_init_from_str(&entry->name, full_name);
1330964
1331965 return entry;
......@@ -1375,7 +1009,9 @@ ZigType *get_generic_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
13751009
13761010 fn_type->data.fn.fn_type_id = *fn_type_id;
13771011 fn_type->data.fn.is_generic = true;
1378 fn_type->zero_bits = true;
1012 fn_type->abi_size = 0;
1013 fn_type->size_in_bits = 0;
1014 fn_type->abi_align = 0;
13791015 return fn_type;
13801016}
13811017
......@@ -1488,6 +1124,7 @@ static Error emit_error_unless_type_allowed_in_packed_struct(CodeGen *g, ZigType
14881124 ZigType *elem_type = type_entry->data.array.child_type;
14891125 if ((err = emit_error_unless_type_allowed_in_packed_struct(g, elem_type, source_node)))
14901126 return err;
1127 // TODO revisit this when doing https://github.com/ziglang/zig/issues/1512
14911128 if (type_size(g, type_entry) * 8 == type_size_bits(g, type_entry))
14921129 return ErrorNone;
14931130 add_node_error(g, source_node,
......@@ -1611,13 +1248,12 @@ ZigType *get_auto_err_set_type(CodeGen *g, ZigFn *fn_entry) {
16111248 ZigType *err_set_type = new_type_table_entry(ZigTypeIdErrorSet);
16121249 buf_resize(&err_set_type->name, 0);
16131250 buf_appendf(&err_set_type->name, "@typeOf(%s).ReturnType.ErrorSet", buf_ptr(&fn_entry->symbol_name));
1614 err_set_type->type_ref = g->builtin_types.entry_global_error_set->type_ref;
1615 err_set_type->di_type = g->builtin_types.entry_global_error_set->di_type;
16161251 err_set_type->data.error_set.err_count = 0;
16171252 err_set_type->data.error_set.errors = nullptr;
16181253 err_set_type->data.error_set.infer_fn = fn_entry;
1619
1620 g->error_di_types.append(&err_set_type->di_type);
1254 err_set_type->size_in_bits = g->builtin_types.entry_global_error_set->size_in_bits;
1255 err_set_type->abi_align = g->builtin_types.entry_global_error_set->abi_align;
1256 err_set_type->abi_size = g->builtin_types.entry_global_error_set->abi_size;
16211257
16221258 return err_set_type;
16231259}
......@@ -1858,10 +1494,10 @@ bool type_is_invalid(ZigType *type_entry) {
18581494 return true;
18591495 case ZigTypeIdStruct:
18601496 return type_entry->data.structure.resolve_status == ResolveStatusInvalid;
1497 case ZigTypeIdUnion:
1498 return type_entry->data.unionation.resolve_status == ResolveStatusInvalid;
18611499 case ZigTypeIdEnum:
18621500 return type_entry->data.enumeration.is_invalid;
1863 case ZigTypeIdUnion:
1864 return type_entry->data.unionation.is_invalid;
18651501 default:
18661502 return false;
18671503 }
......@@ -1869,178 +1505,80 @@ bool type_is_invalid(ZigType *type_entry) {
18691505}
18701506
18711507
1872static Error resolve_enum_type(CodeGen *g, ZigType *enum_type) {
1873 assert(enum_type->id == ZigTypeIdEnum);
1874
1875 if (enum_type->data.enumeration.is_invalid)
1876 return ErrorSemanticAnalyzeFail;
1508ZigType *get_struct_type(CodeGen *g, const char *type_name, const char *field_names[],
1509 ZigType *field_types[], size_t field_count)
1510{
1511 ZigType *struct_type = new_type_table_entry(ZigTypeIdStruct);
18771512
1878 if (enum_type->data.enumeration.complete)
1879 return ErrorNone;
1513 buf_init_from_str(&struct_type->name, type_name);
18801514
1881 Error err;
1882 if ((err = resolve_enum_zero_bits(g, enum_type)))
1883 return err;
1515 struct_type->data.structure.src_field_count = field_count;
1516 struct_type->data.structure.gen_field_count = 0;
1517 struct_type->data.structure.resolve_status = ResolveStatusSizeKnown;
1518 struct_type->data.structure.fields = allocate<TypeStructField>(field_count);
1519 struct_type->data.structure.fields_by_name.init(field_count);
18841520
1885 AstNode *decl_node = enum_type->data.enumeration.decl_node;
1521 size_t abi_align = 0;
1522 for (size_t i = 0; i < field_count; i += 1) {
1523 TypeStructField *field = &struct_type->data.structure.fields[i];
1524 field->name = buf_create_from_str(field_names[i]);
1525 field->type_entry = field_types[i];
1526 field->src_index = i;
18861527
1887 if (enum_type->data.enumeration.embedded_in_current) {
1888 if (!enum_type->data.enumeration.reported_infinite_err) {
1889 enum_type->data.enumeration.is_invalid = true;
1890 enum_type->data.enumeration.reported_infinite_err = true;
1891 ErrorMsg *msg = add_node_error(g, decl_node,
1892 buf_sprintf("enum '%s' contains itself", buf_ptr(&enum_type->name)));
1893 emit_error_notes_for_ref_stack(g, msg);
1528 if (type_has_bits(field->type_entry)) {
1529 assert(type_is_resolved(field->type_entry, ResolveStatusSizeKnown));
1530 if (field->type_entry->abi_align > abi_align) {
1531 abi_align = field->type_entry->abi_align;
1532 }
1533 field->gen_index = struct_type->data.structure.gen_field_count;
1534 struct_type->data.structure.gen_field_count += 1;
1535 } else {
1536 field->gen_index = SIZE_MAX;
18941537 }
1895 return ErrorSemanticAnalyzeFail;
1538
1539 auto prev_entry = struct_type->data.structure.fields_by_name.put_unique(field->name, field);
1540 assert(prev_entry == nullptr);
18961541 }
18971542
1898 assert(!enum_type->data.enumeration.zero_bits_loop_flag);
1899 assert(decl_node->type == NodeTypeContainerDecl);
1900 assert(enum_type->di_type);
1543 size_t next_offset = 0;
1544 for (size_t i = 0; i < field_count; i += 1) {
1545 TypeStructField *field = &struct_type->data.structure.fields[i];
1546 if (field->gen_index == SIZE_MAX)
1547 continue;
1548 field->offset = next_offset;
1549 size_t next_src_field_index = i + 1;
1550 for (; next_src_field_index < field_count; next_src_field_index += 1) {
1551 if (struct_type->data.structure.fields[next_src_field_index].gen_index != SIZE_MAX) {
1552 break;
1553 }
1554 }
1555 size_t next_abi_align = (next_src_field_index == field_count) ?
1556 abi_align : struct_type->data.structure.fields[next_src_field_index].type_entry->abi_align;
1557 next_offset = next_field_offset(next_offset, abi_align, field->type_entry->abi_size, next_abi_align);
1558 }
19011559
1902 uint32_t field_count = enum_type->data.enumeration.src_field_count;
1560 struct_type->abi_align = abi_align;
1561 struct_type->abi_size = next_offset;
1562 struct_type->size_in_bits = next_offset * 8;
19031563
1904 assert(enum_type->data.enumeration.fields);
1905 ZigLLVMDIEnumerator **di_enumerators = allocate<ZigLLVMDIEnumerator*>(field_count);
1906
1907 Scope *scope = &enum_type->data.enumeration.decls_scope->base;
1908 ZigType *import = get_scope_import(scope);
1909
1910 // set temporary flag
1911 enum_type->data.enumeration.embedded_in_current = true;
1912
1913 for (uint32_t i = 0; i < field_count; i += 1) {
1914 TypeEnumField *enum_field = &enum_type->data.enumeration.fields[i];
1915
1916 // TODO send patch to LLVM to support APInt in createEnumerator instead of int64_t
1917 // http://lists.llvm.org/pipermail/llvm-dev/2017-December/119456.html
1918 di_enumerators[i] = ZigLLVMCreateDebugEnumerator(g->dbuilder, buf_ptr(enum_field->name),
1919 bigint_as_signed(&enum_field->value));
1920 }
1921
1922 // unset temporary flag
1923 enum_type->data.enumeration.embedded_in_current = false;
1924 enum_type->data.enumeration.complete = true;
1925
1926 if (enum_type->data.enumeration.is_invalid)
1927 return ErrorSemanticAnalyzeFail;
1928
1929 if (enum_type->zero_bits) {
1930 enum_type->type_ref = LLVMVoidType();
1931
1932 uint64_t debug_size_in_bits = 0;
1933 uint64_t debug_align_in_bits = 0;
1934 ZigLLVMDIType **di_root_members = nullptr;
1935 size_t debug_member_count = 0;
1936 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
1937 ZigLLVMFileToScope(import->data.structure.root_struct->di_file),
1938 buf_ptr(&enum_type->name),
1939 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
1940 debug_size_in_bits,
1941 debug_align_in_bits,
1942 0, nullptr, di_root_members, (int)debug_member_count, 0, nullptr, "");
1943
1944 ZigLLVMReplaceTemporary(g->dbuilder, enum_type->di_type, replacement_di_type);
1945 enum_type->di_type = replacement_di_type;
1946 return ErrorNone;
1947 }
1948
1949 ZigType *tag_int_type = enum_type->data.enumeration.tag_int_type;
1950
1951 // create debug type for tag
1952 uint64_t tag_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, tag_int_type->type_ref);
1953 uint64_t tag_debug_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, tag_int_type->type_ref);
1954 ZigLLVMDIType *tag_di_type = ZigLLVMCreateDebugEnumerationType(g->dbuilder,
1955 ZigLLVMFileToScope(import->data.structure.root_struct->di_file), buf_ptr(&enum_type->name),
1956 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
1957 tag_debug_size_in_bits,
1958 tag_debug_align_in_bits,
1959 di_enumerators, field_count,
1960 tag_int_type->di_type, "");
1961
1962 ZigLLVMReplaceTemporary(g->dbuilder, enum_type->di_type, tag_di_type);
1963 enum_type->di_type = tag_di_type;
1964 return ErrorNone;
1564 return struct_type;
19651565}
19661566
1567static size_t get_store_size_bytes(size_t size_in_bits) {
1568 return (size_in_bits + 7) / 8;
1569}
19671570
1968ZigType *get_struct_type(CodeGen *g, const char *type_name, const char *field_names[],
1969 ZigType *field_types[], size_t field_count)
1970{
1971 ZigType *struct_type = new_type_table_entry(ZigTypeIdStruct);
1972
1973 buf_init_from_str(&struct_type->name, type_name);
1974
1975 struct_type->data.structure.src_field_count = field_count;
1976 struct_type->data.structure.gen_field_count = 0;
1977 struct_type->data.structure.resolve_status = ResolveStatusSizeKnown;
1978 struct_type->data.structure.fields = allocate<TypeStructField>(field_count);
1979 struct_type->data.structure.fields_by_name.init(field_count);
1980
1981 ZigLLVMDIType **di_element_types = allocate<ZigLLVMDIType*>(field_count);
1982 LLVMTypeRef *element_types = allocate<LLVMTypeRef>(field_count);
1983 for (size_t i = 0; i < field_count; i += 1) {
1984 element_types[struct_type->data.structure.gen_field_count] = field_types[i]->type_ref;
1985
1986 TypeStructField *field = &struct_type->data.structure.fields[i];
1987 field->name = buf_create_from_str(field_names[i]);
1988 field->type_entry = field_types[i];
1989 field->src_index = i;
1990
1991 if (type_has_bits(field->type_entry)) {
1992 field->gen_index = struct_type->data.structure.gen_field_count;
1993 struct_type->data.structure.gen_field_count += 1;
1994 } else {
1995 field->gen_index = SIZE_MAX;
1996 }
1997
1998 auto prev_entry = struct_type->data.structure.fields_by_name.put_unique(field->name, field);
1999 assert(prev_entry == nullptr);
2000 }
2001
2002 struct_type->type_ref = LLVMStructCreateNamed(LLVMGetGlobalContext(), type_name);
2003 LLVMStructSetBody(struct_type->type_ref, element_types, struct_type->data.structure.gen_field_count, false);
2004
2005 struct_type->di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
2006 ZigLLVMTag_DW_structure_type(), type_name,
2007 ZigLLVMCompileUnitToScope(g->compile_unit), nullptr, 0);
2008
2009 for (size_t i = 0; i < field_count; i += 1) {
2010 TypeStructField *type_struct_field = &struct_type->data.structure.fields[i];
2011 if (type_struct_field->gen_index == SIZE_MAX) {
2012 continue;
2013 }
2014 ZigType *field_type = type_struct_field->type_entry;
2015 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, field_type->type_ref);
2016 uint64_t debug_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, field_type->type_ref);
2017 uint64_t debug_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, struct_type->type_ref, type_struct_field->gen_index);
2018 di_element_types[type_struct_field->gen_index] = ZigLLVMCreateDebugMemberType(g->dbuilder,
2019 ZigLLVMTypeToScope(struct_type->di_type), buf_ptr(type_struct_field->name),
2020 nullptr, 0,
2021 debug_size_in_bits,
2022 debug_align_in_bits,
2023 debug_offset_in_bits,
2024 0, field_type->di_type);
2025
2026 assert(di_element_types[type_struct_field->gen_index]);
2027 }
2028
2029 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, struct_type->type_ref);
2030 uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, struct_type->type_ref);
2031 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
2032 ZigLLVMCompileUnitToScope(g->compile_unit),
2033 type_name, nullptr, 0,
2034 debug_size_in_bits,
2035 debug_align_in_bits,
2036 0,
2037 nullptr, di_element_types, struct_type->data.structure.gen_field_count, 0, nullptr, "");
2038
2039 ZigLLVMReplaceTemporary(g->dbuilder, struct_type->di_type, replacement_di_type);
2040 struct_type->di_type = replacement_di_type;
2041 struct_type->data.structure.abi_alignment = LLVMABIAlignmentOfType(g->target_data_ref, struct_type->type_ref);
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}
20421577
2043 return struct_type;
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);
20441582}
20451583
20461584static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
......@@ -2068,454 +1606,313 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
20681606 return ErrorSemanticAnalyzeFail;
20691607 }
20701608
2071 struct_type->data.structure.resolve_loop_flag = true;
2072
20731609 assert(struct_type->data.structure.fields || struct_type->data.structure.src_field_count == 0);
20741610 assert(decl_node->type == NodeTypeContainerDecl);
20751611
20761612 size_t field_count = struct_type->data.structure.src_field_count;
20771613
2078 size_t gen_field_count = struct_type->data.structure.gen_field_count;
2079 LLVMTypeRef *element_types = allocate<LLVMTypeRef>(gen_field_count);
2080
2081 Scope *scope = &struct_type->data.structure.decls_scope->base;
2082
2083 size_t gen_field_index = 0;
20841614 bool packed = (struct_type->data.structure.layout == ContainerLayoutPacked);
2085 size_t packed_bits_offset = 0;
2086 size_t first_packed_bits_offset_misalign = SIZE_MAX;
2087 size_t debug_field_count = 0;
1615 struct_type->data.structure.resolve_loop_flag = true;
20881616
2089 for (size_t i = 0; i < field_count; i += 1) {
2090 TypeStructField *type_struct_field = &struct_type->data.structure.fields[i];
2091 ZigType *field_type = type_struct_field->type_entry;
1617 uint32_t *host_int_bytes = packed ? allocate<uint32_t>(struct_type->data.structure.gen_field_count) : nullptr;
20921618
2093 if ((err = ensure_complete_type(g, field_type))) {
1619 // Resolve sizes of all the field types. Done before the offset loop because the offset
1620 // loop has to look ahead.
1621 for (size_t i = 0; i < field_count; i += 1) {
1622 TypeStructField *field = &struct_type->data.structure.fields[i];
1623 if ((err = type_resolve(g, field->type_entry, ResolveStatusSizeKnown))) {
20941624 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2095 break;
1625 return ErrorSemanticAnalyzeFail;
20961626 }
1627 }
20971628
2098 if (struct_type->data.structure.layout == ContainerLayoutExtern) {
2099 if (!type_allowed_in_extern(g, field_type)) {
2100 AstNode *field_source_node = decl_node->data.container_decl.fields.at(i);
2101 add_node_error(g, field_source_node,
2102 buf_sprintf("extern structs cannot contain fields of type '%s'",
2103 buf_ptr(&field_type->name)));
2104 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2105 break;
2106 }
2107 }
1629 size_t packed_bits_offset = 0;
1630 size_t next_offset = 0;
1631 size_t first_packed_bits_offset_misalign = SIZE_MAX;
1632 size_t gen_field_index = 0;
1633 size_t size_in_bits = 0;
1634 size_t abi_align = struct_type->abi_align;
21081635
2109 if (!type_has_bits(field_type))
1636 // Calculate offsets
1637 for (size_t i = 0; i < field_count; i += 1) {
1638 TypeStructField *field = &struct_type->data.structure.fields[i];
1639 if (field->gen_index == SIZE_MAX)
21101640 continue;
1641 ZigType *field_type = field->type_entry;
1642 assert(field_type != nullptr);
21111643
2112 type_struct_field->gen_index = gen_field_index;
1644 field->gen_index = gen_field_index;
1645 field->offset = next_offset;
21131646
21141647 if (packed) {
2115 AstNode *field_source_node = decl_node->data.container_decl.fields.at(i);
2116 if ((err = emit_error_unless_type_allowed_in_packed_struct(g, field_type, field_source_node))) {
2117 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2118 break;
2119 }
2120
21211648 size_t field_size_in_bits = type_size_bits(g, field_type);
21221649 size_t next_packed_bits_offset = packed_bits_offset + field_size_in_bits;
21231650
1651 size_in_bits += field_size_in_bits;
1652
21241653 if (first_packed_bits_offset_misalign != SIZE_MAX) {
21251654 // this field is not byte-aligned; it is part of the previous field with a bit offset
2126 type_struct_field->bit_offset_in_host = packed_bits_offset - first_packed_bits_offset_misalign;
1655 field->bit_offset_in_host = packed_bits_offset - first_packed_bits_offset_misalign;
21271656
21281657 size_t full_bit_count = next_packed_bits_offset - first_packed_bits_offset_misalign;
2129 LLVMTypeRef int_type_ref = LLVMIntType((unsigned)(full_bit_count));
2130 if (8 * LLVMStoreSizeOfType(g->target_data_ref, int_type_ref) == full_bit_count) {
2131 // next field recovers store alignment
2132 element_types[gen_field_index] = int_type_ref;
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;
21331662 gen_field_index += 1;
1663 // TODO: https://github.com/ziglang/zig/issues/1512
1664 next_offset = next_field_offset(next_offset, abi_align, full_abi_size, 1);
1665 size_in_bits = next_offset * 8;
21341666
21351667 first_packed_bits_offset_misalign = SIZE_MAX;
21361668 }
2137 } else if (8 * LLVMStoreSizeOfType(g->target_data_ref, field_type->type_ref) != 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) {
21381670 first_packed_bits_offset_misalign = packed_bits_offset;
2139 type_struct_field->bit_offset_in_host = 0;
1671 field->bit_offset_in_host = 0;
21401672 } else {
21411673 // This is a byte-aligned field (both start and end) in a packed struct.
2142 element_types[gen_field_index] = field_type->type_ref;
2143 type_struct_field->bit_offset_in_host = 0;
1674 host_int_bytes[gen_field_index] = field_type->size_in_bits / 8;
1675 field->bit_offset_in_host = 0;
21441676 gen_field_index += 1;
1677 // TODO: https://github.com/ziglang/zig/issues/1512
1678 next_offset = next_field_offset(next_offset, abi_align, field_type->size_in_bits / 8, 1);
1679 size_in_bits = next_offset * 8;
21451680 }
21461681 packed_bits_offset = next_packed_bits_offset;
21471682 } else {
2148 element_types[gen_field_index] = field_type->type_ref;
2149 assert(element_types[gen_field_index]);
2150
21511683 gen_field_index += 1;
1684 size_t next_src_field_index = i + 1;
1685 for (; next_src_field_index < field_count; next_src_field_index += 1) {
1686 if (struct_type->data.structure.fields[next_src_field_index].gen_index != SIZE_MAX) {
1687 break;
1688 }
1689 }
1690 size_t next_abi_align = (next_src_field_index == field_count) ?
1691 abi_align : struct_type->data.structure.fields[next_src_field_index].type_entry->abi_align;
1692 next_offset = next_field_offset(next_offset, abi_align, field_type->abi_size, next_abi_align);
1693 size_in_bits = next_offset * 8;
21521694 }
2153 debug_field_count += 1;
21541695 }
21551696 if (first_packed_bits_offset_misalign != SIZE_MAX) {
21561697 size_t full_bit_count = packed_bits_offset - first_packed_bits_offset_misalign;
2157 LLVMTypeRef int_type_ref = LLVMIntType((unsigned)full_bit_count);
2158 size_t store_bit_count = 8 * LLVMStoreSizeOfType(g->target_data_ref, int_type_ref);
2159 element_types[gen_field_index] = LLVMIntType((unsigned)store_bit_count);
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;
21601701 gen_field_index += 1;
21611702 }
21621703
1704 struct_type->abi_size = next_offset;
1705 struct_type->size_in_bits = size_in_bits;
1706 struct_type->data.structure.resolve_status = ResolveStatusSizeKnown;
1707 struct_type->data.structure.gen_field_count = (uint32_t)gen_field_index;
21631708 struct_type->data.structure.resolve_loop_flag = false;
1709 struct_type->data.structure.host_int_bytes = host_int_bytes;
21641710
2165 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid)
2166 return ErrorSemanticAnalyzeFail;
1711 return ErrorNone;
1712}
21671713
2168 struct_type->data.structure.resolve_status = ResolveStatusSizeKnown;
1714static Error resolve_union_alignment(CodeGen *g, ZigType *union_type) {
1715 assert(union_type->id == ZigTypeIdUnion);
21691716
2170 if (struct_type->zero_bits) {
2171 struct_type->type_ref = LLVMVoidType();
1717 Error err;
21721718
2173 ZigType *import = get_scope_import(scope);
2174 uint64_t debug_size_in_bits = 0;
2175 uint64_t debug_align_in_bits = 0;
2176 ZigLLVMDIType **di_element_types = nullptr;
2177 size_t debug_field_count = 0;
2178 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
2179 ZigLLVMFileToScope(import->data.structure.root_struct->di_file),
2180 buf_ptr(&struct_type->name),
2181 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
2182 debug_size_in_bits,
2183 debug_align_in_bits,
2184 0, nullptr, di_element_types, (int)debug_field_count, 0, nullptr, "");
2185 ZigLLVMReplaceTemporary(g->dbuilder, struct_type->di_type, replacement_di_type);
2186 struct_type->di_type = replacement_di_type;
1719 if (union_type->data.unionation.resolve_status == ResolveStatusInvalid)
1720 return ErrorSemanticAnalyzeFail;
1721 if (union_type->data.unionation.resolve_status >= ResolveStatusAlignmentKnown)
1722 return ErrorNone;
1723 if ((err = resolve_union_zero_bits(g, union_type)))
1724 return err;
1725 if (union_type->data.unionation.resolve_status >= ResolveStatusAlignmentKnown)
21871726 return ErrorNone;
1727
1728 if (union_type->data.unionation.resolve_loop_flag) {
1729 if (!union_type->data.unionation.reported_infinite_err) {
1730 AstNode *decl_node = union_type->data.unionation.decl_node;
1731 union_type->data.unionation.reported_infinite_err = true;
1732 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
1733 ErrorMsg *msg = add_node_error(g, decl_node,
1734 buf_sprintf("union '%s' contains itself", buf_ptr(&union_type->name)));
1735 emit_error_notes_for_ref_stack(g, msg);
1736 }
1737 return ErrorSemanticAnalyzeFail;
21881738 }
2189 assert(struct_type->di_type);
21901739
1740 // set temporary flag
1741 union_type->data.unionation.resolve_loop_flag = true;
21911742
2192 // the count may have been adjusting from packing bit fields
2193 gen_field_count = gen_field_index;
2194 struct_type->data.structure.gen_field_count = (uint32_t)gen_field_count;
1743 ZigType *most_aligned_union_member = nullptr;
1744 uint32_t field_count = union_type->data.unionation.src_field_count;
1745 bool packed = union_type->data.unionation.layout == ContainerLayoutPacked;
21951746
2196 LLVMStructSetBody(struct_type->type_ref, element_types, (unsigned)gen_field_count, packed);
1747 for (uint32_t i = 0; i < field_count; i += 1) {
1748 TypeUnionField *field = &union_type->data.unionation.fields[i];
1749 if (field->gen_index == UINT32_MAX)
1750 continue;
21971751
2198 // if you hit this assert then probably this type or a related type didn't
2199 // get ensure_complete_type called on it before using it with something that
2200 // requires a complete type
2201 assert(LLVMStoreSizeOfType(g->target_data_ref, struct_type->type_ref) > 0);
1752 size_t this_field_align;
1753 if (packed) {
1754 // TODO: https://github.com/ziglang/zig/issues/1512
1755 this_field_align = 1;
1756 // This is the same hack as resolve_struct_alignment. See the comment there.
1757 } else if (field->type_entry == nullptr) {
1758 this_field_align = g->builtin_types.entry_usize->abi_align;
1759 } else {
1760 if ((err = type_resolve(g, field->type_entry, ResolveStatusAlignmentKnown))) {
1761 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
1762 return ErrorSemanticAnalyzeFail;
1763 }
22021764
2203 ZigLLVMDIType **di_element_types = allocate<ZigLLVMDIType*>(debug_field_count);
1765 if (union_type->data.unionation.resolve_status == ResolveStatusInvalid)
1766 return ErrorSemanticAnalyzeFail;
22041767
2205 ZigType *import = get_scope_import(scope);
2206 size_t debug_field_index = 0;
2207 for (size_t i = 0; i < field_count; i += 1) {
2208 AstNode *field_node = decl_node->data.container_decl.fields.at(i);
2209 TypeStructField *type_struct_field = &struct_type->data.structure.fields[i];
2210 size_t gen_field_index = type_struct_field->gen_index;
2211 if (gen_field_index == SIZE_MAX) {
2212 continue;
1768 this_field_align = field->type_entry->abi_align;
22131769 }
22141770
2215 ZigType *field_type = type_struct_field->type_entry;
2216
2217 // if the field is a function, actually the debug info should be a pointer.
2218 ZigLLVMDIType *field_di_type;
2219 if (field_type->id == ZigTypeIdFn) {
2220 ZigType *field_ptr_type = get_pointer_to_type(g, field_type, true);
2221 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, field_ptr_type->type_ref);
2222 uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, field_ptr_type->type_ref);
2223 field_di_type = ZigLLVMCreateDebugPointerType(g->dbuilder, field_type->di_type,
2224 debug_size_in_bits, debug_align_in_bits, buf_ptr(&field_ptr_type->name));
2225 } else {
2226 field_di_type = field_type->di_type;
1771 if (most_aligned_union_member == nullptr ||
1772 this_field_align > most_aligned_union_member->abi_align)
1773 {
1774 most_aligned_union_member = field->type_entry;
22271775 }
1776 }
22281777
2229 assert(field_type->type_ref);
2230 assert(struct_type->type_ref);
2231 assert(struct_type->data.structure.resolve_status == ResolveStatusSizeKnown);
2232 uint64_t debug_size_in_bits;
2233 uint64_t debug_align_in_bits;
2234 uint64_t debug_offset_in_bits;
2235 if (packed) {
2236 debug_size_in_bits = type_size_bits(g, type_struct_field->type_entry);
2237 debug_align_in_bits = 1;
2238 debug_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, struct_type->type_ref,
2239 (unsigned)gen_field_index) + type_struct_field->bit_offset_in_host;
1778 // unset temporary flag
1779 union_type->data.unionation.resolve_loop_flag = false;
1780 union_type->data.unionation.resolve_status = ResolveStatusAlignmentKnown;
1781 union_type->data.unionation.most_aligned_union_member = most_aligned_union_member;
1782
1783 ZigType *tag_type = union_type->data.unionation.tag_type;
1784 if (tag_type != nullptr && type_has_bits(tag_type)) {
1785 if ((err = type_resolve(g, tag_type, ResolveStatusAlignmentKnown))) {
1786 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
1787 return ErrorSemanticAnalyzeFail;
1788 }
1789 if (most_aligned_union_member == nullptr) {
1790 union_type->abi_align = tag_type->abi_align;
1791 union_type->data.unionation.gen_tag_index = SIZE_MAX;
1792 union_type->data.unionation.gen_union_index = SIZE_MAX;
1793 } else if (tag_type->abi_align > most_aligned_union_member->abi_align) {
1794 union_type->abi_align = tag_type->abi_align;
1795 union_type->data.unionation.gen_tag_index = 0;
1796 union_type->data.unionation.gen_union_index = 1;
22401797 } else {
2241 debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, field_type->type_ref);
2242 debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, field_type->type_ref);
2243 debug_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, struct_type->type_ref,
2244 (unsigned)gen_field_index);
1798 union_type->abi_align = most_aligned_union_member->abi_align;
1799 union_type->data.unionation.gen_union_index = 0;
1800 union_type->data.unionation.gen_tag_index = 1;
22451801 }
2246 di_element_types[debug_field_index] = ZigLLVMCreateDebugMemberType(g->dbuilder,
2247 ZigLLVMTypeToScope(struct_type->di_type), buf_ptr(type_struct_field->name),
2248 import->data.structure.root_struct->di_file, (unsigned)(field_node->line + 1),
2249 debug_size_in_bits,
2250 debug_align_in_bits,
2251 debug_offset_in_bits,
2252 0, field_di_type);
2253 assert(di_element_types[debug_field_index]);
2254 debug_field_index += 1;
1802 } else {
1803 assert(most_aligned_union_member != nullptr);
1804 union_type->abi_align = most_aligned_union_member->abi_align;
1805 union_type->data.unionation.gen_union_index = SIZE_MAX;
1806 union_type->data.unionation.gen_tag_index = SIZE_MAX;
22551807 }
22561808
2257
2258 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, struct_type->type_ref);
2259 uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, struct_type->type_ref);
2260 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
2261 ZigLLVMFileToScope(import->data.structure.root_struct->di_file),
2262 buf_ptr(&struct_type->name),
2263 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
2264 debug_size_in_bits,
2265 debug_align_in_bits,
2266 0, nullptr, di_element_types, (int)debug_field_count, 0, nullptr, "");
2267
2268 ZigLLVMReplaceTemporary(g->dbuilder, struct_type->di_type, replacement_di_type);
2269 struct_type->di_type = replacement_di_type;
2270
22711809 return ErrorNone;
22721810}
22731811
22741812static Error resolve_union_type(CodeGen *g, ZigType *union_type) {
22751813 assert(union_type->id == ZigTypeIdUnion);
22761814
2277 if (union_type->data.unionation.complete)
1815 Error err;
1816
1817 if (union_type->data.unionation.resolve_status == ResolveStatusInvalid)
1818 return ErrorSemanticAnalyzeFail;
1819 if (union_type->data.unionation.resolve_status >= ResolveStatusSizeKnown)
22781820 return ErrorNone;
22791821
2280 Error err;
2281 if ((err = resolve_union_zero_bits(g, union_type)))
1822 if ((err = resolve_union_alignment(g, union_type)))
22821823 return err;
22831824
22841825 AstNode *decl_node = union_type->data.unionation.decl_node;
22851826
2286 if (union_type->data.unionation.embedded_in_current) {
1827
1828 assert(decl_node->type == NodeTypeContainerDecl);
1829
1830 uint32_t field_count = union_type->data.unionation.src_field_count;
1831 ZigType *most_aligned_union_member = union_type->data.unionation.most_aligned_union_member;
1832
1833 assert(union_type->data.unionation.fields);
1834
1835 size_t union_abi_size = 0;
1836 size_t union_size_in_bits = 0;
1837
1838 if (union_type->data.unionation.resolve_loop_flag) {
22871839 if (!union_type->data.unionation.reported_infinite_err) {
22881840 union_type->data.unionation.reported_infinite_err = true;
2289 union_type->data.unionation.is_invalid = true;
1841 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
22901842 ErrorMsg *msg = add_node_error(g, decl_node,
2291 buf_sprintf("union '%s' contains itself", buf_ptr(&union_type->name)));
1843 buf_sprintf("union '%s' depends on its own size", buf_ptr(&union_type->name)));
22921844 emit_error_notes_for_ref_stack(g, msg);
22931845 }
22941846 return ErrorSemanticAnalyzeFail;
22951847 }
22961848
2297 assert(!union_type->data.unionation.zero_bits_loop_flag);
2298 assert(decl_node->type == NodeTypeContainerDecl);
2299 assert(union_type->di_type);
2300
2301 uint32_t field_count = union_type->data.unionation.src_field_count;
1849 // set temporary flag
1850 union_type->data.unionation.resolve_loop_flag = true;
23021851
2303 assert(union_type->data.unionation.fields);
1852 for (uint32_t i = 0; i < field_count; i += 1) {
1853 TypeUnionField *union_field = &union_type->data.unionation.fields[i];
1854 ZigType *field_type = union_field->type_entry;
23041855
2305 uint32_t gen_field_count = union_type->data.unionation.gen_field_count;
2306 ZigLLVMDIType **union_inner_di_types = allocate<ZigLLVMDIType*>(gen_field_count);
2307
2308 ZigType *most_aligned_union_member = nullptr;
2309 uint64_t size_of_most_aligned_member_in_bits = 0;
2310 uint64_t biggest_align_in_bits = 0;
2311 uint64_t biggest_size_in_bits = 0;
2312
2313 Scope *scope = &union_type->data.unionation.decls_scope->base;
2314 ZigType *import = get_scope_import(scope);
2315
2316 // set temporary flag
2317 union_type->data.unionation.embedded_in_current = true;
2318
2319
2320 for (uint32_t i = 0; i < field_count; i += 1) {
2321 AstNode *field_node = decl_node->data.container_decl.fields.at(i);
2322 TypeUnionField *union_field = &union_type->data.unionation.fields[i];
2323 ZigType *field_type = union_field->type_entry;
2324
2325 if ((err = ensure_complete_type(g, field_type))) {
2326 union_type->data.unionation.is_invalid = true;
2327 continue;
1856 if ((err = type_resolve(g, field_type, ResolveStatusSizeKnown))) {
1857 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
1858 return ErrorSemanticAnalyzeFail;
23281859 }
23291860
1861 if (type_is_invalid(union_type))
1862 return ErrorSemanticAnalyzeFail;
1863
23301864 if (!type_has_bits(field_type))
23311865 continue;
23321866
2333 uint64_t store_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, field_type->type_ref);
2334 uint64_t abi_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, field_type->type_ref);
2335
2336 assert(store_size_in_bits > 0);
2337 assert(abi_align_in_bits > 0);
2338
2339 union_inner_di_types[union_field->gen_index] = ZigLLVMCreateDebugMemberType(g->dbuilder,
2340 ZigLLVMTypeToScope(union_type->di_type), buf_ptr(union_field->enum_field->name),
2341 import->data.structure.root_struct->di_file, (unsigned)(field_node->line + 1),
2342 store_size_in_bits,
2343 abi_align_in_bits,
2344 0,
2345 0, field_type->di_type);
2346
2347 biggest_size_in_bits = max(biggest_size_in_bits, store_size_in_bits);
2348
2349 if (!most_aligned_union_member || abi_align_in_bits > biggest_align_in_bits) {
2350 most_aligned_union_member = field_type;
2351 biggest_align_in_bits = abi_align_in_bits;
2352 size_of_most_aligned_member_in_bits = store_size_in_bits;
2353 }
1867 union_abi_size = max(union_abi_size, field_type->abi_size);
1868 union_size_in_bits = max(union_size_in_bits, field_type->size_in_bits);
23541869 }
23551870
2356
2357 // unset temporary flag
2358 union_type->data.unionation.embedded_in_current = false;
2359 union_type->data.unionation.complete = true;
2360 union_type->data.unionation.union_size_bytes = biggest_size_in_bits / 8;
2361 union_type->data.unionation.most_aligned_union_member = most_aligned_union_member;
2362
2363 if (union_type->data.unionation.is_invalid)
2364 return ErrorSemanticAnalyzeFail;
2365
2366 if (union_type->zero_bits) {
2367 union_type->type_ref = LLVMVoidType();
2368
2369 uint64_t debug_size_in_bits = 0;
2370 uint64_t debug_align_in_bits = 0;
2371 ZigLLVMDIType **di_root_members = nullptr;
2372 size_t debug_member_count = 0;
2373 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugUnionType(g->dbuilder,
2374 ZigLLVMFileToScope(import->data.structure.root_struct->di_file),
2375 buf_ptr(&union_type->name),
2376 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
2377 debug_size_in_bits,
2378 debug_align_in_bits,
2379 0, di_root_members, (int)debug_member_count, 0, "");
2380
2381 ZigLLVMReplaceTemporary(g->dbuilder, union_type->di_type, replacement_di_type);
2382 union_type->di_type = replacement_di_type;
2383 return ErrorNone;
1871 // The union itself for now has to be treated as being independently aligned.
1872 // See https://github.com/ziglang/zig/issues/2166.
1873 if (most_aligned_union_member != nullptr) {
1874 union_abi_size = align_forward(union_abi_size, most_aligned_union_member->abi_align);
23841875 }
23851876
2386 uint64_t padding_in_bits = biggest_size_in_bits - size_of_most_aligned_member_in_bits;
1877 // unset temporary flag
1878 union_type->data.unionation.resolve_loop_flag = false;
1879 union_type->data.unionation.resolve_status = ResolveStatusSizeKnown;
1880 union_type->data.unionation.union_abi_size = union_abi_size;
23871881
23881882 ZigType *tag_type = union_type->data.unionation.tag_type;
2389 if (tag_type == nullptr || tag_type->zero_bits) {
2390 assert(most_aligned_union_member != nullptr);
2391
2392 if (padding_in_bits > 0) {
2393 ZigType *u8_type = get_int_type(g, false, 8);
2394 ZigType *padding_array = get_array_type(g, u8_type, padding_in_bits / 8);
2395 LLVMTypeRef union_element_types[] = {
2396 most_aligned_union_member->type_ref,
2397 padding_array->type_ref,
2398 };
2399 LLVMStructSetBody(union_type->type_ref, union_element_types, 2, false);
1883 if (tag_type != nullptr && type_has_bits(tag_type)) {
1884 if ((err = type_resolve(g, tag_type, ResolveStatusSizeKnown))) {
1885 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
1886 return ErrorSemanticAnalyzeFail;
1887 }
1888 if (most_aligned_union_member == nullptr) {
1889 union_type->abi_size = tag_type->abi_size;
1890 union_type->size_in_bits = tag_type->size_in_bits;
24001891 } else {
2401 LLVMStructSetBody(union_type->type_ref, &most_aligned_union_member->type_ref, 1, false);
1892 size_t field_sizes[2];
1893 size_t field_aligns[2];
1894 field_sizes[union_type->data.unionation.gen_tag_index] = tag_type->abi_size;
1895 field_aligns[union_type->data.unionation.gen_tag_index] = tag_type->abi_align;
1896 field_sizes[union_type->data.unionation.gen_union_index] = union_abi_size;
1897 field_aligns[union_type->data.unionation.gen_union_index] = most_aligned_union_member->abi_align;
1898 size_t field2_offset = next_field_offset(0, union_type->abi_align, field_sizes[0], field_aligns[1]);
1899 union_type->abi_size = next_field_offset(field2_offset, union_type->abi_align, field_sizes[1], union_type->abi_align);
1900 union_type->size_in_bits = union_type->abi_size * 8;
24021901 }
2403 union_type->data.unionation.union_type_ref = union_type->type_ref;
2404 union_type->data.unionation.gen_tag_index = SIZE_MAX;
2405 union_type->data.unionation.gen_union_index = SIZE_MAX;
2406
2407 assert(8*LLVMABIAlignmentOfType(g->target_data_ref, union_type->type_ref) >= biggest_align_in_bits);
2408 assert(8*LLVMStoreSizeOfType(g->target_data_ref, union_type->type_ref) >= biggest_size_in_bits);
2409
2410 // create debug type for union
2411 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugUnionType(g->dbuilder,
2412 ZigLLVMFileToScope(import->data.structure.root_struct->di_file), buf_ptr(&union_type->name),
2413 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
2414 biggest_size_in_bits, biggest_align_in_bits, 0, union_inner_di_types,
2415 gen_field_count, 0, "");
2416
2417 ZigLLVMReplaceTemporary(g->dbuilder, union_type->di_type, replacement_di_type);
2418 union_type->di_type = replacement_di_type;
2419 return ErrorNone;
2420 }
2421
2422 LLVMTypeRef union_type_ref;
2423 if (padding_in_bits > 0) {
2424 ZigType *u8_type = get_int_type(g, false, 8);
2425 ZigType *padding_array = get_array_type(g, u8_type, padding_in_bits / 8);
2426 LLVMTypeRef union_element_types[] = {
2427 most_aligned_union_member->type_ref,
2428 padding_array->type_ref,
2429 };
2430 union_type_ref = LLVMStructType(union_element_types, 2, false);
2431 } else if (most_aligned_union_member == nullptr) {
2432 union_type->data.unionation.gen_tag_index = SIZE_MAX;
2433 union_type->data.unionation.gen_union_index = SIZE_MAX;
2434 union_type->type_ref = tag_type->type_ref;
2435
2436 ZigLLVMReplaceTemporary(g->dbuilder, union_type->di_type, tag_type->di_type);
2437 union_type->di_type = tag_type->di_type;
2438 return ErrorNone;
2439 } else {
2440 union_type_ref = most_aligned_union_member->type_ref;
2441 }
2442 union_type->data.unionation.union_type_ref = union_type_ref;
2443
2444 assert(8*LLVMABIAlignmentOfType(g->target_data_ref, union_type_ref) >= biggest_align_in_bits);
2445 assert(8*LLVMStoreSizeOfType(g->target_data_ref, union_type_ref) >= biggest_size_in_bits);
2446
2447 // create llvm type for root struct
2448 ZigType *tag_int_type = tag_type->data.enumeration.tag_int_type;
2449 uint64_t align_of_tag_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, tag_int_type->type_ref);
2450
2451 if (align_of_tag_in_bits >= biggest_align_in_bits) {
2452 union_type->data.unionation.gen_tag_index = 0;
2453 union_type->data.unionation.gen_union_index = 1;
24541902 } else {
2455 union_type->data.unionation.gen_union_index = 0;
2456 union_type->data.unionation.gen_tag_index = 1;
1903 union_type->abi_size = union_abi_size;
1904 union_type->size_in_bits = union_size_in_bits;
24571905 }
24581906
2459 LLVMTypeRef root_struct_element_types[2];
2460 root_struct_element_types[union_type->data.unionation.gen_tag_index] = tag_type->type_ref;
2461 root_struct_element_types[union_type->data.unionation.gen_union_index] = union_type_ref;
2462 LLVMStructSetBody(union_type->type_ref, root_struct_element_types, 2, false);
2463
2464
2465 // create debug type for union
2466 ZigLLVMDIType *union_di_type = ZigLLVMCreateDebugUnionType(g->dbuilder,
2467 ZigLLVMTypeToScope(union_type->di_type), "AnonUnion",
2468 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
2469 biggest_size_in_bits, biggest_align_in_bits, 0, union_inner_di_types,
2470 gen_field_count, 0, "");
2471
2472 uint64_t union_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, union_type->type_ref,
2473 union_type->data.unionation.gen_union_index);
2474 uint64_t tag_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, union_type->type_ref,
2475 union_type->data.unionation.gen_tag_index);
2476
2477 ZigLLVMDIType *union_member_di_type = ZigLLVMCreateDebugMemberType(g->dbuilder,
2478 ZigLLVMTypeToScope(union_type->di_type), "payload",
2479 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
2480 biggest_size_in_bits,
2481 biggest_align_in_bits,
2482 union_offset_in_bits,
2483 0, union_di_type);
2484
2485 uint64_t tag_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, tag_type->type_ref);
2486 uint64_t tag_debug_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, tag_type->type_ref);
2487
2488 ZigLLVMDIType *tag_member_di_type = ZigLLVMCreateDebugMemberType(g->dbuilder,
2489 ZigLLVMTypeToScope(union_type->di_type), "tag",
2490 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
2491 tag_debug_size_in_bits,
2492 tag_debug_align_in_bits,
2493 tag_offset_in_bits,
2494 0, tag_type->di_type);
2495
2496 ZigLLVMDIType *di_root_members[2];
2497 di_root_members[union_type->data.unionation.gen_tag_index] = tag_member_di_type;
2498 di_root_members[union_type->data.unionation.gen_union_index] = union_member_di_type;
2499
2500 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, union_type->type_ref);
2501 uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, union_type->type_ref);
2502 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
2503 ZigLLVMFileToScope(import->data.structure.root_struct->di_file),
2504 buf_ptr(&union_type->name),
2505 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
2506 debug_size_in_bits,
2507 debug_align_in_bits,
2508 0, nullptr, di_root_members, 2, 0, nullptr, "");
2509
2510 ZigLLVMReplaceTemporary(g->dbuilder, union_type->di_type, replacement_di_type);
2511 union_type->di_type = replacement_di_type;
2512
25131907 return ErrorNone;
25141908}
25151909
25161910static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
25171911 assert(enum_type->id == ZigTypeIdEnum);
25181912
1913 if (enum_type->data.enumeration.is_invalid)
1914 return ErrorSemanticAnalyzeFail;
1915
25191916 if (enum_type->data.enumeration.zero_bits_known)
25201917 return ErrorNone;
25211918
......@@ -2531,7 +1928,6 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
25311928
25321929 AstNode *decl_node = enum_type->data.enumeration.decl_node;
25331930 assert(decl_node->type == NodeTypeContainerDecl);
2534 assert(enum_type->di_type);
25351931
25361932 assert(!enum_type->data.enumeration.fields);
25371933 uint32_t field_count = (uint32_t)decl_node->data.container_decl.fields.length;
......@@ -2564,6 +1960,10 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
25641960 tag_int_type = get_smallest_unsigned_int_type(g, field_count - 1);
25651961 }
25661962
1963 enum_type->size_in_bits = tag_int_type->size_in_bits;
1964 enum_type->abi_size = tag_int_type->abi_size;
1965 enum_type->abi_align = tag_int_type->abi_align;
1966
25671967 // TODO: Are extern enums allowed to have an init_arg_expr?
25681968 if (decl_node->data.container_decl.init_arg_expr != nullptr) {
25691969 ZigType *wanted_tag_int_type = analyze_type_expr(g, scope, decl_node->data.container_decl.init_arg_expr);
......@@ -2587,7 +1987,9 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
25871987 }
25881988 }
25891989 enum_type->data.enumeration.tag_int_type = tag_int_type;
2590 enum_type->type_ref = tag_int_type->type_ref;
1990 enum_type->size_in_bits = tag_int_type->size_in_bits;
1991 enum_type->abi_size = tag_int_type->abi_size;
1992 enum_type->abi_align = tag_int_type->abi_align;
25911993
25921994 for (uint32_t field_i = 0; field_i < field_count; field_i += 1) {
25931995 AstNode *field_node = decl_node->data.container_decl.fields.at(field_i);
......@@ -2671,8 +2073,8 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
26712073 }
26722074
26732075 enum_type->data.enumeration.zero_bits_loop_flag = false;
2674 enum_type->zero_bits = !type_has_bits(tag_int_type);
26752076 enum_type->data.enumeration.zero_bits_known = true;
2077 enum_type->data.enumeration.complete = true;
26762078
26772079 if (enum_type->data.enumeration.is_invalid)
26782080 return ErrorSemanticAnalyzeFail;
......@@ -2683,12 +2085,20 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
26832085static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
26842086 assert(struct_type->id == ZigTypeIdStruct);
26852087
2088 Error err;
2089
26862090 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid)
26872091 return ErrorSemanticAnalyzeFail;
26882092 if (struct_type->data.structure.resolve_status >= ResolveStatusZeroBitsKnown)
26892093 return ErrorNone;
26902094
2095 AstNode *decl_node = struct_type->data.structure.decl_node;
2096 assert(decl_node->type == NodeTypeContainerDecl);
2097
26912098 if (struct_type->data.structure.resolve_loop_flag) {
2099 // TODO This is a problem. I believe it can be solved with lazy values.
2100 struct_type->size_in_bits = SIZE_MAX;
2101 struct_type->abi_size = SIZE_MAX;
26922102 struct_type->data.structure.resolve_status = ResolveStatusZeroBitsKnown;
26932103 struct_type->data.structure.resolve_loop_flag = false;
26942104 return ErrorNone;
......@@ -2696,10 +2106,6 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
26962106
26972107 struct_type->data.structure.resolve_loop_flag = true;
26982108
2699 AstNode *decl_node = struct_type->data.structure.decl_node;
2700 assert(decl_node->type == NodeTypeContainerDecl);
2701 assert(struct_type->di_type);
2702
27032109 assert(!struct_type->data.structure.fields);
27042110 size_t field_count = decl_node->data.container_decl.fields.length;
27052111 struct_type->data.structure.src_field_count = (uint32_t)field_count;
......@@ -2718,7 +2124,7 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
27182124 if (field_node->data.struct_field.type == nullptr) {
27192125 add_node_error(g, field_node, buf_sprintf("struct field missing type"));
27202126 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2721 continue;
2127 return ErrorSemanticAnalyzeFail;
27222128 }
27232129
27242130 auto field_entry = struct_type->data.structure.fields_by_name.put_unique(type_struct_field->name, type_struct_field);
......@@ -2727,11 +2133,33 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
27272133 buf_sprintf("duplicate struct field: '%s'", buf_ptr(type_struct_field->name)));
27282134 add_error_note(g, msg, field_entry->value->decl_node, buf_sprintf("other field here"));
27292135 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2730 continue;
2136 return ErrorSemanticAnalyzeFail;
27312137 }
27322138
27332139 ZigType *field_type = analyze_type_expr(g, scope, field_node->data.struct_field.type);
27342140 type_struct_field->type_entry = field_type;
2141 if (type_is_invalid(field_type)) {
2142 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2143 return ErrorSemanticAnalyzeFail;
2144 }
2145 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid)
2146 return ErrorSemanticAnalyzeFail;
2147
2148 if (struct_type->data.structure.layout == ContainerLayoutExtern &&
2149 !type_allowed_in_extern(g, field_type))
2150 {
2151 add_node_error(g, field_node,
2152 buf_sprintf("extern structs cannot contain fields of type '%s'",
2153 buf_ptr(&field_type->name)));
2154 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2155 return ErrorSemanticAnalyzeFail;
2156 } else if (struct_type->data.structure.layout == ContainerLayoutPacked) {
2157 if ((err = emit_error_unless_type_allowed_in_packed_struct(g, field_type, field_node))) {
2158 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2159 return ErrorSemanticAnalyzeFail;
2160 }
2161 }
2162
27352163 type_struct_field->src_index = i;
27362164 type_struct_field->gen_index = SIZE_MAX;
27372165
......@@ -2739,21 +2167,19 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
27392167 add_node_error(g, field_node->data.struct_field.value,
27402168 buf_sprintf("enums, not structs, support field assignment"));
27412169 }
2742
27432170 if (field_type->id == ZigTypeIdOpaque) {
27442171 add_node_error(g, field_node->data.struct_field.type,
27452172 buf_sprintf("opaque types have unknown size and therefore cannot be directly embedded in structs"));
27462173 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2747 continue;
2174 return ErrorSemanticAnalyzeFail;
27482175 }
2749
27502176 switch (type_requires_comptime(g, field_type)) {
27512177 case ReqCompTimeYes:
27522178 struct_type->data.structure.requires_comptime = true;
27532179 break;
27542180 case ReqCompTimeInvalid:
27552181 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2756 continue;
2182 return ErrorSemanticAnalyzeFail;
27572183 case ReqCompTimeNo:
27582184 break;
27592185 }
......@@ -2767,7 +2193,10 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
27672193
27682194 struct_type->data.structure.resolve_loop_flag = false;
27692195 struct_type->data.structure.gen_field_count = (uint32_t)gen_field_index;
2770 struct_type->zero_bits = (gen_field_index == 0);
2196 if (gen_field_index != 0) {
2197 struct_type->abi_size = SIZE_MAX;
2198 struct_type->size_in_bits = SIZE_MAX;
2199 }
27712200
27722201 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid)
27732202 return ErrorSemanticAnalyzeFail;
......@@ -2785,9 +2214,10 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {
27852214 return ErrorSemanticAnalyzeFail;
27862215 if (struct_type->data.structure.resolve_status >= ResolveStatusAlignmentKnown)
27872216 return ErrorNone;
2788
27892217 if ((err = resolve_struct_zero_bits(g, struct_type)))
27902218 return err;
2219 if (struct_type->data.structure.resolve_status >= ResolveStatusAlignmentKnown)
2220 return ErrorNone;
27912221
27922222 AstNode *decl_node = struct_type->data.structure.decl_node;
27932223
......@@ -2803,22 +2233,19 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {
28032233
28042234 struct_type->data.structure.resolve_loop_flag = true;
28052235 assert(decl_node->type == NodeTypeContainerDecl);
2806 assert(struct_type->di_type);
28072236
28082237 size_t field_count = struct_type->data.structure.src_field_count;
2809 if (struct_type->data.structure.layout == ContainerLayoutPacked) {
2810 struct_type->data.structure.abi_alignment = 1;
2811 for (size_t i = 0; i < field_count; i += 1) {
2812 TypeStructField *field = &struct_type->data.structure.fields[i];
2813 if (field->type_entry != nullptr && type_is_invalid(field->type_entry)) {
2814 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2815 break;
2816 }
2817 }
2818 } else for (size_t i = 0; i < field_count; i += 1) {
2238 bool packed = struct_type->data.structure.layout == ContainerLayoutPacked;
2239
2240 for (size_t i = 0; i < field_count; i += 1) {
28192241 TypeStructField *field = &struct_type->data.structure.fields[i];
2820 uint32_t this_field_align;
2242 if (field->gen_index == SIZE_MAX)
2243 continue;
28212244
2245 size_t this_field_align;
2246 if (packed) {
2247 // TODO: https://github.com/ziglang/zig/issues/1512
2248 this_field_align = 1;
28222249 // TODO If we have no type_entry for the field, we've already failed to
28232250 // compile the program correctly. This stage1 compiler needs a deeper
28242251 // reworking to make this correct, or we can ignore the problem
......@@ -2827,28 +2254,19 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {
28272254 // on itself. When this false positive happens we assume a pointer-aligned
28282255 // field, which is usually fine but could be incorrectly over-aligned or
28292256 // even under-aligned. See https://github.com/ziglang/zig/issues/1512
2830 if (field->type_entry == nullptr) {
2831 this_field_align = LLVMABIAlignmentOfType(g->target_data_ref, LLVMPointerType(LLVMInt8Type(), 0));
2257 } else if (field->type_entry == nullptr) {
2258 this_field_align = g->builtin_types.entry_usize->abi_align;
28322259 } else {
2833 if (type_is_invalid(field->type_entry)) {
2834 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2835 break;
2836 }
2837
2838 if (!type_has_bits(field->type_entry))
2839 continue;
2840
28412260 if ((err = type_resolve(g, field->type_entry, ResolveStatusAlignmentKnown))) {
28422261 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2843 break;
2262 return ErrorSemanticAnalyzeFail;
28442263 }
2845
2846 this_field_align = get_abi_alignment(g, field->type_entry);
2847 assert(this_field_align != 0);
2264 this_field_align = field->type_entry->abi_align;
28482265 }
2849 // alignment of structs is the alignment of the most-aligned field
2850 if (this_field_align > struct_type->data.structure.abi_alignment) {
2851 struct_type->data.structure.abi_alignment = this_field_align;
2266
2267 // TODO: https://github.com/ziglang/zig/issues/1512
2268 if (this_field_align > struct_type->abi_align) {
2269 struct_type->abi_align = this_field_align;
28522270 }
28532271 }
28542272
......@@ -2867,57 +2285,41 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
28672285
28682286 Error err;
28692287
2870 if (union_type->data.unionation.is_invalid)
2288 if (union_type->data.unionation.resolve_status == ResolveStatusInvalid)
28712289 return ErrorSemanticAnalyzeFail;
28722290
2873 if (union_type->data.unionation.zero_bits_known)
2291 if (union_type->data.unionation.resolve_status >= ResolveStatusZeroBitsKnown)
28742292 return ErrorNone;
28752293
2876 if (type_is_invalid(union_type))
2877 return ErrorSemanticAnalyzeFail;
2878
2879 if (union_type->data.unionation.zero_bits_loop_flag) {
2294 if (union_type->data.unionation.resolve_loop_flag) {
28802295 // If we get here it's due to recursion. From this we conclude that the struct is
2881 // not zero bits, and if abi_alignment == 0 we further conclude that the first field
2882 // is a pointer to this very struct, or a function pointer with parameters that
2883 // reference such a type.
2884 union_type->data.unionation.zero_bits_known = true;
2885 union_type->data.unionation.zero_bits_loop_flag = false;
2886 if (union_type->data.unionation.abi_alignment == 0) {
2887 if (union_type->data.unionation.layout == ContainerLayoutPacked) {
2888 union_type->data.unionation.abi_alignment = 1;
2889 } else {
2890 union_type->data.unionation.abi_alignment = LLVMABIAlignmentOfType(g->target_data_ref,
2891 LLVMPointerType(LLVMInt8Type(), 0));
2892 }
2893 }
2296 // not zero bits.
2297 // TODO actually it could still be zero bits. Here we should continue analyzing
2298 // the union from the next field index.
2299 union_type->data.unionation.resolve_status = ResolveStatusZeroBitsKnown;
2300 union_type->data.unionation.resolve_loop_flag = false;
2301 union_type->abi_size = SIZE_MAX;
2302 union_type->size_in_bits = SIZE_MAX;
28942303 return ErrorNone;
28952304 }
28962305
2897 union_type->data.unionation.zero_bits_loop_flag = true;
2306 union_type->data.unionation.resolve_loop_flag = true;
28982307
28992308 AstNode *decl_node = union_type->data.unionation.decl_node;
29002309 assert(decl_node->type == NodeTypeContainerDecl);
2901 assert(union_type->di_type);
29022310
2903 assert(!union_type->data.unionation.fields);
2311 assert(union_type->data.unionation.fields == nullptr);
29042312 uint32_t field_count = (uint32_t)decl_node->data.container_decl.fields.length;
29052313 if (field_count == 0) {
29062314 add_node_error(g, decl_node, buf_sprintf("unions must have 1 or more fields"));
2907
29082315 union_type->data.unionation.src_field_count = field_count;
2909 union_type->data.unionation.fields = nullptr;
2910 union_type->data.unionation.is_invalid = true;
2911 union_type->data.unionation.zero_bits_loop_flag = false;
2912 union_type->data.unionation.zero_bits_known = true;
2316 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
29132317 return ErrorSemanticAnalyzeFail;
29142318 }
29152319 union_type->data.unionation.src_field_count = field_count;
29162320 union_type->data.unionation.fields = allocate<TypeUnionField>(field_count);
29172321 union_type->data.unionation.fields_by_name.init(field_count);
29182322
2919 uint32_t biggest_align_bytes = 0;
2920
29212323 Scope *scope = &union_type->data.unionation.decls_scope->base;
29222324
29232325 HashMap<BigInt, AstNode *, bigint_hash, bigint_eql> occupied_tag_values = {};
......@@ -2931,7 +2333,6 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
29312333 bool create_enum_type = decl_node->data.container_decl.auto_enum || (enum_type_node == nullptr && want_safety);
29322334 bool *covered_enum_fields;
29332335 ZigLLVMDIEnumerator **di_enumerators;
2934 uint32_t abi_alignment_so_far;
29352336 if (create_enum_type) {
29362337 occupied_tag_values.init(field_count);
29372338
......@@ -2941,13 +2342,13 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
29412342 if (enum_type_node != nullptr) {
29422343 tag_int_type = analyze_type_expr(g, scope, enum_type_node);
29432344 if (type_is_invalid(tag_int_type)) {
2944 union_type->data.unionation.is_invalid = true;
2345 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
29452346 return ErrorSemanticAnalyzeFail;
29462347 }
29472348 if (tag_int_type->id != ZigTypeIdInt) {
29482349 add_node_error(g, enum_type_node,
29492350 buf_sprintf("expected integer tag type, found '%s'", buf_ptr(&tag_int_type->name)));
2950 union_type->data.unionation.is_invalid = true;
2351 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
29512352 return ErrorSemanticAnalyzeFail;
29522353 }
29532354 } else if (auto_layout && field_count == 1) {
......@@ -2955,13 +2356,15 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
29552356 } else {
29562357 tag_int_type = get_smallest_unsigned_int_type(g, field_count - 1);
29572358 }
2958 abi_alignment_so_far = get_abi_alignment(g, tag_int_type);
29592359
29602360 tag_type = new_type_table_entry(ZigTypeIdEnum);
29612361 buf_resize(&tag_type->name, 0);
29622362 buf_appendf(&tag_type->name, "@TagType(%s)", buf_ptr(&union_type->name));
2963 tag_type->type_ref = tag_int_type->type_ref;
2964 tag_type->zero_bits = tag_int_type->zero_bits;
2363 tag_type->llvm_type = tag_int_type->llvm_type;
2364 tag_type->llvm_di_type = tag_int_type->llvm_di_type;
2365 tag_type->abi_size = tag_int_type->abi_size;
2366 tag_type->abi_align = tag_int_type->abi_align;
2367 tag_type->size_in_bits = tag_int_type->size_in_bits;
29652368
29662369 tag_type->data.enumeration.tag_int_type = tag_int_type;
29672370 tag_type->data.enumeration.zero_bits_known = true;
......@@ -2975,11 +2378,11 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
29752378 } else if (enum_type_node != nullptr) {
29762379 ZigType *enum_type = analyze_type_expr(g, scope, enum_type_node);
29772380 if (type_is_invalid(enum_type)) {
2978 union_type->data.unionation.is_invalid = true;
2381 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
29792382 return ErrorSemanticAnalyzeFail;
29802383 }
29812384 if (enum_type->id != ZigTypeIdEnum) {
2982 union_type->data.unionation.is_invalid = true;
2385 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
29832386 add_node_error(g, enum_type_node,
29842387 buf_sprintf("expected enum tag type, found '%s'", buf_ptr(&enum_type->name)));
29852388 return ErrorSemanticAnalyzeFail;
......@@ -2989,11 +2392,9 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
29892392 return err;
29902393 }
29912394 tag_type = enum_type;
2992 abi_alignment_so_far = get_abi_alignment(g, enum_type); // this populates src_field_count
29932395 covered_enum_fields = allocate<bool>(enum_type->data.enumeration.src_field_count);
29942396 } else {
29952397 tag_type = nullptr;
2996 abi_alignment_so_far = 0;
29972398 }
29982399 union_type->data.unionation.tag_type = tag_type;
29992400
......@@ -3004,14 +2405,15 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
30042405 TypeUnionField *union_field = &union_type->data.unionation.fields[i];
30052406 union_field->name = field_node->data.struct_field.name;
30062407 union_field->decl_node = field_node;
2408 union_field->gen_index = UINT32_MAX;
30072409
30082410 auto field_entry = union_type->data.unionation.fields_by_name.put_unique(union_field->name, union_field);
30092411 if (field_entry != nullptr) {
30102412 ErrorMsg *msg = add_node_error(g, field_node,
30112413 buf_sprintf("duplicate union field: '%s'", buf_ptr(union_field->name)));
30122414 add_error_note(g, msg, field_entry->value->decl_node, buf_sprintf("other field here"));
3013 union_type->data.unionation.is_invalid = true;
3014 continue;
2415 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
2416 return ErrorSemanticAnalyzeFail;
30152417 }
30162418
30172419 ZigType *field_type;
......@@ -3020,29 +2422,31 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
30202422 field_type = g->builtin_types.entry_void;
30212423 } else {
30222424 add_node_error(g, field_node, buf_sprintf("union field missing type"));
3023 union_type->data.unionation.is_invalid = true;
3024 continue;
2425 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
2426 return ErrorSemanticAnalyzeFail;
30252427 }
30262428 } else {
30272429 field_type = analyze_type_expr(g, scope, field_node->data.struct_field.type);
30282430 if ((err = type_resolve(g, field_type, ResolveStatusAlignmentKnown))) {
3029 union_type->data.unionation.is_invalid = true;
3030 continue;
2431 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
2432 return ErrorSemanticAnalyzeFail;
30312433 }
2434 if (union_type->data.unionation.resolve_status == ResolveStatusInvalid)
2435 return ErrorSemanticAnalyzeFail;
30322436 }
30332437 union_field->type_entry = field_type;
30342438
30352439 if (field_type->id == ZigTypeIdOpaque) {
30362440 add_node_error(g, field_node->data.struct_field.type,
30372441 buf_sprintf("opaque types have unknown size and therefore cannot be directly embedded in unions"));
3038 union_type->data.unionation.is_invalid = true;
3039 continue;
2442 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
2443 return ErrorSemanticAnalyzeFail;
30402444 }
30412445
30422446 switch (type_requires_comptime(g, field_type)) {
30432447 case ReqCompTimeInvalid:
3044 union_type->data.unionation.is_invalid = true;
3045 continue;
2448 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
2449 return ErrorSemanticAnalyzeFail;
30462450 case ReqCompTimeYes:
30472451 union_type->data.unionation.requires_comptime = true;
30482452 break;
......@@ -3074,8 +2478,8 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
30742478 ZigType *tag_int_type = tag_type->data.enumeration.tag_int_type;
30752479 ConstExprValue *result = analyze_const_value(g, scope, tag_value, tag_int_type, nullptr);
30762480 if (type_is_invalid(result->type)) {
3077 union_type->data.unionation.is_invalid = true;
3078 continue;
2481 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
2482 return ErrorSemanticAnalyzeFail;
30792483 }
30802484 assert(result->special != ConstValSpecialRuntime);
30812485 assert(result->type->id == ZigTypeIdInt);
......@@ -3090,8 +2494,8 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
30902494 buf_sprintf("enum tag value %s already taken", buf_ptr(val_buf)));
30912495 add_error_note(g, msg, entry->value,
30922496 buf_sprintf("other occurrence here"));
3093 union_type->data.unionation.is_invalid = true;
3094 continue;
2497 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
2498 return ErrorSemanticAnalyzeFail;
30952499 }
30962500 }
30972501 } else if (enum_type_node != nullptr) {
......@@ -3101,8 +2505,8 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
31012505 buf_sprintf("enum field not found: '%s'", buf_ptr(field_name)));
31022506 add_error_note(g, msg, tag_type->data.enumeration.decl_node,
31032507 buf_sprintf("enum declared here"));
3104 union_type->data.unionation.is_invalid = true;
3105 continue;
2508 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
2509 return ErrorSemanticAnalyzeFail;
31062510 }
31072511 covered_enum_fields[union_field->enum_field->decl_index] = true;
31082512 } else {
......@@ -3118,21 +2522,8 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
31182522
31192523 union_field->gen_index = gen_field_index;
31202524 gen_field_index += 1;
3121
3122 uint32_t field_align_bytes = get_abi_alignment(g, field_type);
3123 if (field_align_bytes > biggest_align_bytes) {
3124 biggest_align_bytes = field_align_bytes;
3125 if (biggest_align_bytes > abi_alignment_so_far) {
3126 abi_alignment_so_far = biggest_align_bytes;
3127 }
3128 }
31292525 }
31302526
3131 union_type->data.unionation.abi_alignment = abi_alignment_so_far;
3132
3133 if (union_type->data.unionation.is_invalid)
3134 return ErrorSemanticAnalyzeFail;
3135
31362527 bool src_have_tag = decl_node->data.container_decl.auto_enum ||
31372528 decl_node->data.container_decl.init_arg_expr != nullptr;
31382529
......@@ -3152,7 +2543,7 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
31522543 decl_node->data.container_decl.init_arg_expr : decl_node;
31532544 add_node_error(g, source_node,
31542545 buf_sprintf("%s union does not support enum tag type", qual_str));
3155 union_type->data.unionation.is_invalid = true;
2546 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
31562547 return ErrorSemanticAnalyzeFail;
31572548 }
31582549
......@@ -3194,33 +2585,24 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
31942585 buf_sprintf("enum field missing: '%s'", buf_ptr(enum_field->name)));
31952586 add_error_note(g, msg, field_node,
31962587 buf_sprintf("declared here"));
3197 union_type->data.unionation.is_invalid = true;
2588 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
31982589 }
31992590 }
32002591 }
32012592
3202 if (create_enum_type) {
3203 ZigType *import = get_scope_import(scope);
3204 uint64_t tag_debug_size_in_bits = tag_type->zero_bits ? 0 :
3205 8*LLVMStoreSizeOfType(g->target_data_ref, tag_type->type_ref);
3206 uint64_t tag_debug_align_in_bits = tag_type->zero_bits ? 0 :
3207 8*LLVMABIAlignmentOfType(g->target_data_ref, tag_type->type_ref);
3208 // TODO get a more accurate debug scope
3209 ZigLLVMDIType *tag_di_type = ZigLLVMCreateDebugEnumerationType(g->dbuilder,
3210 ZigLLVMFileToScope(import->data.structure.root_struct->di_file), buf_ptr(&tag_type->name),
3211 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
3212 tag_debug_size_in_bits, tag_debug_align_in_bits, di_enumerators, field_count,
3213 tag_type->di_type, "");
3214 tag_type->di_type = tag_di_type;
2593 if (union_type->data.unionation.resolve_status == ResolveStatusInvalid) {
2594 return ErrorSemanticAnalyzeFail;
32152595 }
32162596
3217 union_type->data.unionation.zero_bits_loop_flag = false;
3218 union_type->data.unionation.gen_field_count = gen_field_index;
3219 union_type->zero_bits = (gen_field_index == 0 && (field_count < 2 || !src_have_tag));
3220 union_type->data.unionation.zero_bits_known = true;
2597 union_type->data.unionation.resolve_loop_flag = false;
32212598
3222 if (union_type->data.unionation.is_invalid)
3223 return ErrorSemanticAnalyzeFail;
2599 union_type->data.unionation.gen_field_count = gen_field_index;
2600 bool zero_bits = gen_field_index == 0 && (field_count < 2 || !src_have_tag);
2601 if (!zero_bits) {
2602 union_type->abi_size = SIZE_MAX;
2603 union_type->size_in_bits = SIZE_MAX;
2604 }
2605 union_type->data.unionation.resolve_status = zero_bits ? ResolveStatusSizeKnown : ResolveStatusZeroBitsKnown;
32242606
32252607 return ErrorNone;
32262608}
......@@ -3630,20 +3012,17 @@ void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {
36303012 }
36313013}
36323014
3633static void resolve_decl_container(CodeGen *g, TldContainer *tld_container) {
3015static Error resolve_decl_container(CodeGen *g, TldContainer *tld_container) {
36343016 ZigType *type_entry = tld_container->type_entry;
36353017 assert(type_entry);
36363018
36373019 switch (type_entry->id) {
36383020 case ZigTypeIdStruct:
3639 resolve_struct_type(g, tld_container->type_entry);
3640 return;
3021 return resolve_struct_type(g, tld_container->type_entry);
36413022 case ZigTypeIdEnum:
3642 resolve_enum_type(g, tld_container->type_entry);
3643 return;
3023 return resolve_enum_zero_bits(g, tld_container->type_entry);
36443024 case ZigTypeIdUnion:
3645 resolve_union_type(g, tld_container->type_entry);
3646 return;
3025 return resolve_union_type(g, tld_container->type_entry);
36473026 default:
36483027 zig_unreachable();
36493028 }
......@@ -4007,7 +3386,7 @@ TypeStructField *find_struct_type_field(ZigType *type_entry, Buf *name) {
40073386
40083387TypeUnionField *find_union_type_field(ZigType *type_entry, Buf *name) {
40093388 assert(type_entry->id == ZigTypeIdUnion);
4010 assert(type_entry->data.unionation.zero_bits_known);
3389 assert(type_is_resolved(type_entry, ResolveStatusZeroBitsKnown));
40113390 if (type_entry->data.unionation.src_field_count == 0)
40123391 return nullptr;
40133392 auto entry = type_entry->data.unionation.fields_by_name.maybe_get(name);
......@@ -4018,7 +3397,7 @@ TypeUnionField *find_union_type_field(ZigType *type_entry, Buf *name) {
40183397
40193398TypeUnionField *find_union_field_by_tag(ZigType *type_entry, const BigInt *tag) {
40203399 assert(type_entry->id == ZigTypeIdUnion);
4021 assert(type_entry->data.unionation.zero_bits_known);
3400 assert(type_is_resolved(type_entry, ResolveStatusZeroBitsKnown));
40223401 for (uint32_t i = 0; i < type_entry->data.unionation.src_field_count; i += 1) {
40233402 TypeUnionField *field = &type_entry->data.unionation.fields[i];
40243403 if (bigint_cmp(&field->enum_field->value, tag) == CmpEQ) {
......@@ -4096,17 +3475,14 @@ ZigType *container_ref_type(ZigType *type_entry) {
40963475 type_entry->data.pointer.child_type : type_entry;
40973476}
40983477
4099void resolve_container_type(CodeGen *g, ZigType *type_entry) {
3478Error resolve_container_type(CodeGen *g, ZigType *type_entry) {
41003479 switch (type_entry->id) {
41013480 case ZigTypeIdStruct:
4102 resolve_struct_type(g, type_entry);
4103 break;
3481 return resolve_struct_type(g, type_entry);
41043482 case ZigTypeIdEnum:
4105 resolve_enum_type(g, type_entry);
4106 break;
3483 return resolve_enum_zero_bits(g, type_entry);
41073484 case ZigTypeIdUnion:
4108 resolve_union_type(g, type_entry);
4109 break;
3485 return resolve_union_type(g, type_entry);
41103486 case ZigTypeIdPointer:
41113487 case ZigTypeIdMetaType:
41123488 case ZigTypeIdVoid:
......@@ -4132,6 +3508,7 @@ void resolve_container_type(CodeGen *g, ZigType *type_entry) {
41323508 case ZigTypeIdVector:
41333509 zig_unreachable();
41343510 }
3511 zig_unreachable();
41353512}
41363513
41373514ZigType *get_src_ptr_type(ZigType *type) {
......@@ -4233,10 +3610,6 @@ static void define_local_param_variables(CodeGen *g, ZigFn *fn_table_entry) {
42333610 if (type_has_bits(param_type)) {
42343611 fn_table_entry->variable_list.append(var);
42353612 }
4236
4237 if (fn_type->data.fn.gen_param_info) {
4238 var->gen_arg_index = fn_type->data.fn.gen_param_info[i].gen_index;
4239 }
42403613 }
42413614}
42423615
......@@ -4626,18 +3999,26 @@ ZigType *get_vector_type(CodeGen *g, uint32_t len, ZigType *elem_type) {
46263999 }
46274000
46284001 ZigType *entry = new_type_table_entry(ZigTypeIdVector);
4629 entry->zero_bits = (len == 0) || !type_has_bits(elem_type);
4630 entry->type_ref = entry->zero_bits ? LLVMVoidType() : LLVMVectorType(elem_type->type_ref, len);
4002 if ((len != 0) && type_has_bits(elem_type)) {
4003 // Vectors can only be ints, floats, or pointers. ints and floats have trivially resolvable
4004 // llvm type refs. pointers we will use usize instead.
4005 LLVMTypeRef example_vector_llvm_type;
4006 if (elem_type->id == ZigTypeIdPointer) {
4007 example_vector_llvm_type = LLVMVectorType(g->builtin_types.entry_usize->llvm_type, len);
4008 } else {
4009 example_vector_llvm_type = LLVMVectorType(elem_type->llvm_type, len);
4010 }
4011 assert(example_vector_llvm_type != nullptr);
4012 entry->size_in_bits = elem_type->size_in_bits * len;
4013 entry->abi_size = LLVMABISizeOfType(g->target_data_ref, example_vector_llvm_type);
4014 entry->abi_align = LLVMABIAlignmentOfType(g->target_data_ref, example_vector_llvm_type);
4015 }
46314016 entry->data.vector.len = len;
46324017 entry->data.vector.elem_type = elem_type;
46334018
46344019 buf_resize(&entry->name, 0);
46354020 buf_appendf(&entry->name, "@Vector(%u, %s)", len, buf_ptr(&elem_type->name));
46364021
4637 entry->di_type = ZigLLVMDIBuilderCreateVectorType(g->dbuilder,
4638 len * type_size_bits(g, elem_type),
4639 LLVMABIAlignmentOfType(g->target_data_ref, entry->type_ref), elem_type->di_type, len);
4640
46414022 g->type_table.put(type_id, entry);
46424023 return entry;
46434024}
......@@ -4685,12 +4066,7 @@ bool handle_is_ptr(ZigType *type_entry) {
46854066 !type_is_nonnull_ptr(type_entry->data.maybe.child_type) &&
46864067 type_entry->data.maybe.child_type->id != ZigTypeIdErrorSet;
46874068 case ZigTypeIdUnion:
4688 assert(type_entry->data.unionation.zero_bits_known);
4689 if (type_entry->data.unionation.gen_field_count == 0)
4690 return false;
4691 if (!type_has_bits(type_entry))
4692 return false;
4693 return true;
4069 return type_has_bits(type_entry) && type_entry->data.unionation.gen_field_count != 0;
46944070
46954071 }
46964072 zig_unreachable();
......@@ -5165,10 +4541,10 @@ bool fn_eval_eql(Scope *a, Scope *b) {
51654541
51664542// Whether the type has bits at runtime.
51674543bool type_has_bits(ZigType *type_entry) {
5168 assert(type_entry);
4544 assert(type_entry != nullptr);
51694545 assert(!type_is_invalid(type_entry));
51704546 assert(type_is_resolved(type_entry, ResolveStatusZeroBitsKnown));
5171 return !type_entry->zero_bits;
4547 return type_entry->abi_size != 0;
51724548}
51734549
51744550// Whether you can infer the value based solely on the type.
......@@ -5629,18 +5005,22 @@ Error type_resolve(CodeGen *g, ZigType *ty, ResolveStatus status) {
56295005 } else if (ty->id == ZigTypeIdEnum) {
56305006 return resolve_enum_zero_bits(g, ty);
56315007 } else if (ty->id == ZigTypeIdUnion) {
5632 return resolve_union_zero_bits(g, ty);
5008 return resolve_union_alignment(g, ty);
56335009 }
56345010 return ErrorNone;
56355011 case ResolveStatusSizeKnown:
56365012 if (ty->id == ZigTypeIdStruct) {
56375013 return resolve_struct_type(g, ty);
56385014 } else if (ty->id == ZigTypeIdEnum) {
5639 return resolve_enum_type(g, ty);
5015 return resolve_enum_zero_bits(g, ty);
56405016 } else if (ty->id == ZigTypeIdUnion) {
56415017 return resolve_union_type(g, ty);
56425018 }
56435019 return ErrorNone;
5020 case ResolveStatusLLVMFwdDecl:
5021 case ResolveStatusLLVMFull:
5022 resolve_llvm_types(g, ty, status);
5023 return ErrorNone;
56445024 }
56455025 zig_unreachable();
56465026}
......@@ -6192,31 +5572,18 @@ void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val) {
61925572ZigType *make_int_type(CodeGen *g, bool is_signed, uint32_t size_in_bits) {
61935573 assert(size_in_bits <= 65535);
61945574 ZigType *entry = new_type_table_entry(ZigTypeIdInt);
6195 entry->type_ref = (size_in_bits == 0) ? LLVMVoidType() : LLVMIntType(size_in_bits);
6196 entry->zero_bits = (size_in_bits == 0);
5575
5576 entry->size_in_bits = size_in_bits;
5577 if (size_in_bits != 0) {
5578 entry->llvm_type = LLVMIntType(size_in_bits);
5579 entry->abi_size = LLVMABISizeOfType(g->target_data_ref, entry->llvm_type);
5580 entry->abi_align = LLVMABIAlignmentOfType(g->target_data_ref, entry->llvm_type);
5581 }
61975582
61985583 const char u_or_i = is_signed ? 'i' : 'u';
61995584 buf_resize(&entry->name, 0);
62005585 buf_appendf(&entry->name, "%c%" PRIu32, u_or_i, size_in_bits);
62015586
6202 unsigned dwarf_tag;
6203 if (is_signed) {
6204 if (size_in_bits == 8) {
6205 dwarf_tag = ZigLLVMEncoding_DW_ATE_signed_char();
6206 } else {
6207 dwarf_tag = ZigLLVMEncoding_DW_ATE_signed();
6208 }
6209 } else {
6210 if (size_in_bits == 8) {
6211 dwarf_tag = ZigLLVMEncoding_DW_ATE_unsigned_char();
6212 } else {
6213 dwarf_tag = ZigLLVMEncoding_DW_ATE_unsigned();
6214 }
6215 }
6216
6217 uint64_t debug_size_in_bits = (size_in_bits == 0) ?
6218 0 : (8*LLVMStoreSizeOfType(g->target_data_ref, entry->type_ref));
6219 entry->di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name), debug_size_in_bits, dwarf_tag);
62205587 entry->data.integral.is_signed = is_signed;
62215588 entry->data.integral.bit_count = size_in_bits;
62225589 return entry;
......@@ -6620,33 +5987,13 @@ LinkLib *add_link_lib(CodeGen *g, Buf *name) {
66205987 return link_lib;
66215988}
66225989
6623uint32_t get_abi_alignment(CodeGen *g, ZigType *type_entry) {
6624 assert(type_is_resolved(type_entry, ResolveStatusAlignmentKnown));
6625 if (type_entry->zero_bits) return 0;
6626
6627 // We need to make this function work without requiring ensure_complete_type
6628 // so that we can have structs with fields that are pointers to their own type.
6629 if (type_entry->id == ZigTypeIdStruct) {
6630 assert(type_entry->data.structure.abi_alignment != 0);
6631 return type_entry->data.structure.abi_alignment;
6632 } else if (type_entry->id == ZigTypeIdUnion) {
6633 assert(type_entry->data.unionation.abi_alignment != 0);
6634 return type_entry->data.unionation.abi_alignment;
6635 } else if (type_entry->id == ZigTypeIdOpaque) {
6636 return 1;
6637 } else {
6638 uint32_t llvm_alignment = LLVMABIAlignmentOfType(g->target_data_ref, type_entry->type_ref);
6639 return llvm_alignment;
6640 }
6641}
6642
6643ZigType *get_align_amt_type(CodeGen *g) {
6644 if (g->align_amt_type == nullptr) {
6645 // according to LLVM the maximum alignment is 1 << 29.
6646 g->align_amt_type = get_int_type(g, false, 29);
6647 }
6648 return g->align_amt_type;
6649}
5990ZigType *get_align_amt_type(CodeGen *g) {
5991 if (g->align_amt_type == nullptr) {
5992 // according to LLVM the maximum alignment is 1 << 29.
5993 g->align_amt_type = get_int_type(g, false, 29);
5994 }
5995 return g->align_amt_type;
5996}
66505997
66515998uint32_t type_ptr_hash(const ZigType *ptr) {
66525999 return hash_ptr((void*)ptr);
......@@ -6841,11 +6188,10 @@ bool type_is_c_abi_int(CodeGen *g, ZigType *ty) {
68416188
68426189uint32_t get_host_int_bytes(CodeGen *g, ZigType *struct_type, TypeStructField *field) {
68436190 assert(struct_type->id == ZigTypeIdStruct);
6844 if (struct_type->data.structure.layout != ContainerLayoutPacked) {
6191 assert(type_is_resolved(struct_type, ResolveStatusSizeKnown));
6192 if (struct_type->data.structure.host_int_bytes == nullptr)
68456193 return 0;
6846 }
6847 LLVMTypeRef field_type = LLVMStructGetTypeAtIndex(struct_type->type_ref, field->gen_index);
6848 return LLVMStoreSizeOfType(g->target_data_ref, field_type);
6194 return struct_type->data.structure.host_int_bytes[field->gen_index];
68496195}
68506196
68516197Error ensure_const_val_repr(IrAnalyze *ira, CodeGen *codegen, AstNode *source_node,
......@@ -6911,3 +6257,968 @@ Buf *type_bare_name(ZigType *type_entry) {
69116257Buf *type_h_name(ZigType *t) {
69126258 return type_bare_name(t);
69136259}
6260
6261static void resolve_llvm_types_slice(CodeGen *g, ZigType *type, ResolveStatus wanted_resolve_status) {
6262 if (type->data.structure.resolve_status >= wanted_resolve_status) return;
6263
6264 ZigType *ptr_type = type->data.structure.fields[slice_ptr_index].type_entry;
6265 ZigType *child_type = ptr_type->data.pointer.child_type;
6266 ZigType *usize_type = g->builtin_types.entry_usize;
6267
6268 bool done = false;
6269 if (ptr_type->data.pointer.is_const || ptr_type->data.pointer.is_volatile ||
6270 ptr_type->data.pointer.explicit_alignment != 0 || ptr_type->data.pointer.allow_zero)
6271 {
6272 ZigType *peer_ptr_type = get_pointer_to_type_extra(g, child_type, false, false,
6273 PtrLenUnknown, 0, 0, 0, false);
6274 ZigType *peer_slice_type = get_slice_type(g, peer_ptr_type);
6275
6276 assertNoError(type_resolve(g, peer_slice_type, wanted_resolve_status));
6277 type->llvm_type = peer_slice_type->llvm_type;
6278 type->llvm_di_type = peer_slice_type->llvm_di_type;
6279 type->data.structure.resolve_status = peer_slice_type->data.structure.resolve_status;
6280 done = true;
6281 }
6282
6283 // If the child type is []const T then we need to make sure the type ref
6284 // and debug info is the same as if the child type were []T.
6285 if (is_slice(child_type)) {
6286 ZigType *child_ptr_type = child_type->data.structure.fields[slice_ptr_index].type_entry;
6287 assert(child_ptr_type->id == ZigTypeIdPointer);
6288 if (child_ptr_type->data.pointer.is_const || child_ptr_type->data.pointer.is_volatile ||
6289 child_ptr_type->data.pointer.explicit_alignment != 0 || child_ptr_type->data.pointer.allow_zero)
6290 {
6291 ZigType *grand_child_type = child_ptr_type->data.pointer.child_type;
6292 ZigType *bland_child_ptr_type = get_pointer_to_type_extra(g, grand_child_type, false, false,
6293 PtrLenUnknown, 0, 0, 0, false);
6294 ZigType *bland_child_slice = get_slice_type(g, bland_child_ptr_type);
6295 ZigType *peer_ptr_type = get_pointer_to_type_extra(g, bland_child_slice, false, false,
6296 PtrLenUnknown, 0, 0, 0, false);
6297 ZigType *peer_slice_type = get_slice_type(g, peer_ptr_type);
6298
6299 assertNoError(type_resolve(g, peer_slice_type, wanted_resolve_status));
6300 type->llvm_type = peer_slice_type->llvm_type;
6301 type->llvm_di_type = peer_slice_type->llvm_di_type;
6302 type->data.structure.resolve_status = peer_slice_type->data.structure.resolve_status;
6303 done = true;
6304 }
6305 }
6306
6307 if (done) return;
6308
6309 LLVMTypeRef usize_llvm_type = get_llvm_type(g, usize_type);
6310 ZigLLVMDIType *usize_llvm_di_type = get_llvm_di_type(g, usize_type);
6311 ZigLLVMDIScope *compile_unit_scope = ZigLLVMCompileUnitToScope(g->compile_unit);
6312 ZigLLVMDIFile *di_file = nullptr;
6313 unsigned line = 0;
6314
6315 if (type->data.structure.resolve_status < ResolveStatusLLVMFwdDecl) {
6316 type->llvm_type = LLVMStructCreateNamed(LLVMGetGlobalContext(), buf_ptr(&type->name));
6317
6318 type->llvm_di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
6319 ZigLLVMTag_DW_structure_type(), buf_ptr(&type->name),
6320 compile_unit_scope, di_file, line);
6321
6322 type->data.structure.resolve_status = ResolveStatusLLVMFwdDecl;
6323 if (ResolveStatusLLVMFwdDecl >= wanted_resolve_status) return;
6324 }
6325
6326 if (!type_has_bits(child_type)) {
6327 LLVMTypeRef element_types[] = {
6328 usize_llvm_type,
6329 };
6330 LLVMStructSetBody(type->llvm_type, element_types, 1, false);
6331
6332 uint64_t len_debug_size_in_bits = usize_type->size_in_bits;
6333 uint64_t len_debug_align_in_bits = 8*usize_type->abi_align;
6334 uint64_t len_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, type->llvm_type, 0);
6335
6336 uint64_t debug_size_in_bits = type->size_in_bits;
6337 uint64_t debug_align_in_bits = 8*type->abi_align;
6338
6339 ZigLLVMDIType *di_element_types[] = {
6340 ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(type->llvm_di_type),
6341 "len", di_file, line,
6342 len_debug_size_in_bits,
6343 len_debug_align_in_bits,
6344 len_offset_in_bits,
6345 0, usize_llvm_di_type),
6346 };
6347 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
6348 compile_unit_scope,
6349 buf_ptr(&type->name),
6350 di_file, line, debug_size_in_bits, debug_align_in_bits, 0,
6351 nullptr, di_element_types, 1, 0, nullptr, "");
6352
6353 ZigLLVMReplaceTemporary(g->dbuilder, type->llvm_di_type, replacement_di_type);
6354 type->llvm_di_type = replacement_di_type;
6355 type->data.structure.resolve_status = ResolveStatusLLVMFull;
6356 return;
6357 }
6358
6359 LLVMTypeRef element_types[2];
6360 element_types[slice_ptr_index] = get_llvm_type(g, ptr_type);
6361 element_types[slice_len_index] = get_llvm_type(g, g->builtin_types.entry_usize);
6362 if (type->data.structure.resolve_status >= wanted_resolve_status) return;
6363 LLVMStructSetBody(type->llvm_type, element_types, 2, false);
6364
6365 uint64_t ptr_debug_size_in_bits = ptr_type->size_in_bits;
6366 uint64_t ptr_debug_align_in_bits = 8*ptr_type->abi_align;
6367 uint64_t ptr_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, type->llvm_type, 0);
6368
6369 uint64_t len_debug_size_in_bits = usize_type->size_in_bits;
6370 uint64_t len_debug_align_in_bits = 8*usize_type->abi_align;
6371 uint64_t len_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, type->llvm_type, 1);
6372
6373 uint64_t debug_size_in_bits = type->size_in_bits;
6374 uint64_t debug_align_in_bits = 8*type->abi_align;
6375
6376 ZigLLVMDIType *di_element_types[] = {
6377 ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(type->llvm_di_type),
6378 "ptr", di_file, line,
6379 ptr_debug_size_in_bits,
6380 ptr_debug_align_in_bits,
6381 ptr_offset_in_bits,
6382 0, get_llvm_di_type(g, ptr_type)),
6383 ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(type->llvm_di_type),
6384 "len", di_file, line,
6385 len_debug_size_in_bits,
6386 len_debug_align_in_bits,
6387 len_offset_in_bits,
6388 0, usize_llvm_di_type),
6389 };
6390 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
6391 compile_unit_scope,
6392 buf_ptr(&type->name),
6393 di_file, line, debug_size_in_bits, debug_align_in_bits, 0,
6394 nullptr, di_element_types, 2, 0, nullptr, "");
6395
6396 ZigLLVMReplaceTemporary(g->dbuilder, type->llvm_di_type, replacement_di_type);
6397 type->llvm_di_type = replacement_di_type;
6398 type->data.structure.resolve_status = ResolveStatusLLVMFull;
6399}
6400
6401static void resolve_llvm_types_struct(CodeGen *g, ZigType *struct_type, ResolveStatus wanted_resolve_status) {
6402 assert(struct_type->id == ZigTypeIdStruct);
6403 assert(struct_type->data.structure.resolve_status != ResolveStatusInvalid);
6404 assert(struct_type->data.structure.resolve_status >= ResolveStatusSizeKnown);
6405 assert(struct_type->data.structure.fields || struct_type->data.structure.src_field_count == 0);
6406 if (struct_type->data.structure.resolve_status >= wanted_resolve_status) return;
6407
6408 AstNode *decl_node = struct_type->data.structure.decl_node;
6409 ZigLLVMDIFile *di_file;
6410 ZigLLVMDIScope *di_scope;
6411 unsigned line;
6412 if (decl_node != nullptr) {
6413 assert(decl_node->type == NodeTypeContainerDecl);
6414 Scope *scope = &struct_type->data.structure.decls_scope->base;
6415 ZigType *import = get_scope_import(scope);
6416 di_file = import->data.structure.root_struct->di_file;
6417 di_scope = ZigLLVMFileToScope(di_file);
6418 line = decl_node->line + 1;
6419 } else {
6420 di_file = nullptr;
6421 di_scope = ZigLLVMCompileUnitToScope(g->compile_unit);
6422 line = 0;
6423 }
6424
6425 if (struct_type->data.structure.resolve_status < ResolveStatusLLVMFwdDecl) {
6426 struct_type->llvm_type = type_has_bits(struct_type) ?
6427 LLVMStructCreateNamed(LLVMGetGlobalContext(), buf_ptr(&struct_type->name)) : LLVMVoidType();
6428 unsigned dwarf_kind = ZigLLVMTag_DW_structure_type();
6429 struct_type->llvm_di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
6430 dwarf_kind, buf_ptr(&struct_type->name),
6431 di_scope, di_file, line);
6432
6433 struct_type->data.structure.resolve_status = ResolveStatusLLVMFwdDecl;
6434 if (ResolveStatusLLVMFwdDecl >= wanted_resolve_status) return;
6435 }
6436
6437 size_t field_count = struct_type->data.structure.src_field_count;
6438 size_t gen_field_count = struct_type->data.structure.gen_field_count;
6439 LLVMTypeRef *element_types = allocate<LLVMTypeRef>(gen_field_count);
6440
6441 size_t gen_field_index = 0;
6442 bool packed = (struct_type->data.structure.layout == ContainerLayoutPacked);
6443 size_t packed_bits_offset = 0;
6444 size_t first_packed_bits_offset_misalign = SIZE_MAX;
6445 size_t debug_field_count = 0;
6446
6447 // trigger all the recursive get_llvm_type calls
6448 for (size_t i = 0; i < field_count; i += 1) {
6449 TypeStructField *type_struct_field = &struct_type->data.structure.fields[i];
6450 ZigType *field_type = type_struct_field->type_entry;
6451 if (!type_has_bits(field_type))
6452 continue;
6453 (void)get_llvm_type(g, field_type);
6454 if (struct_type->data.structure.resolve_status >= wanted_resolve_status) return;
6455 }
6456
6457 for (size_t i = 0; i < field_count; i += 1) {
6458 TypeStructField *type_struct_field = &struct_type->data.structure.fields[i];
6459 ZigType *field_type = type_struct_field->type_entry;
6460
6461 if (!type_has_bits(field_type))
6462 continue;
6463
6464 if (packed) {
6465 size_t field_size_in_bits = type_size_bits(g, field_type);
6466 size_t next_packed_bits_offset = packed_bits_offset + field_size_in_bits;
6467
6468 if (first_packed_bits_offset_misalign != SIZE_MAX) {
6469 // this field is not byte-aligned; it is part of the previous field with a bit offset
6470
6471 size_t full_bit_count = next_packed_bits_offset - first_packed_bits_offset_misalign;
6472 size_t full_abi_size = get_abi_size_bytes(full_bit_count, g->pointer_size_bytes);
6473 if (full_abi_size * 8 == full_bit_count) {
6474 // next field recovers ABI alignment
6475 element_types[gen_field_index] = LLVMIntType((unsigned)(full_bit_count));
6476 gen_field_index += 1;
6477
6478 first_packed_bits_offset_misalign = SIZE_MAX;
6479 }
6480 } else if (get_abi_size_bytes(field_type->size_in_bits, g->pointer_size_bytes) * 8 != field_size_in_bits) {
6481 first_packed_bits_offset_misalign = packed_bits_offset;
6482 } else {
6483 // This is a byte-aligned field (both start and end) in a packed struct.
6484 element_types[gen_field_index] = get_llvm_type(g, field_type);
6485 gen_field_index += 1;
6486 }
6487 packed_bits_offset = next_packed_bits_offset;
6488 } else {
6489 element_types[gen_field_index] = get_llvm_type(g, field_type);
6490
6491 gen_field_index += 1;
6492 }
6493 debug_field_count += 1;
6494 }
6495 if (first_packed_bits_offset_misalign != SIZE_MAX) {
6496 size_t full_bit_count = packed_bits_offset - first_packed_bits_offset_misalign;
6497 size_t full_abi_size = get_abi_size_bytes(full_bit_count, g->pointer_size_bytes);
6498 element_types[gen_field_index] = LLVMIntType((unsigned)full_abi_size * 8);
6499 gen_field_index += 1;
6500 }
6501
6502 if (type_has_bits(struct_type)) {
6503 LLVMStructSetBody(struct_type->llvm_type, element_types, (unsigned)gen_field_count, packed);
6504 }
6505
6506 ZigLLVMDIType **di_element_types = allocate<ZigLLVMDIType*>(debug_field_count);
6507 size_t debug_field_index = 0;
6508 for (size_t i = 0; i < field_count; i += 1) {
6509 TypeStructField *type_struct_field = &struct_type->data.structure.fields[i];
6510 size_t gen_field_index = type_struct_field->gen_index;
6511 if (gen_field_index == SIZE_MAX) {
6512 continue;
6513 }
6514
6515 ZigType *field_type = type_struct_field->type_entry;
6516
6517 // if the field is a function, actually the debug info should be a pointer.
6518 ZigLLVMDIType *field_di_type;
6519 if (field_type->id == ZigTypeIdFn) {
6520 ZigType *field_ptr_type = get_pointer_to_type(g, field_type, true);
6521 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, get_llvm_type(g, field_ptr_type));
6522 uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, get_llvm_type(g, field_ptr_type));
6523 field_di_type = ZigLLVMCreateDebugPointerType(g->dbuilder, get_llvm_di_type(g, field_type),
6524 debug_size_in_bits, debug_align_in_bits, buf_ptr(&field_ptr_type->name));
6525 } else {
6526 field_di_type = get_llvm_di_type(g, field_type);
6527 }
6528
6529 uint64_t debug_size_in_bits;
6530 uint64_t debug_align_in_bits;
6531 uint64_t debug_offset_in_bits;
6532 if (packed) {
6533 debug_size_in_bits = type_struct_field->type_entry->size_in_bits;
6534 debug_align_in_bits = 8 * type_struct_field->type_entry->abi_align;
6535 debug_offset_in_bits = 8 * type_struct_field->offset + type_struct_field->bit_offset_in_host;
6536 } else {
6537 debug_size_in_bits = 8 * get_store_size_bytes(field_type->size_in_bits);
6538 debug_align_in_bits = 8 * field_type->abi_align;
6539 debug_offset_in_bits = 8 * type_struct_field->offset;
6540 }
6541 unsigned line;
6542 if (decl_node != nullptr) {
6543 AstNode *field_node = decl_node->data.container_decl.fields.at(i);
6544 line = field_node->line + 1;
6545 } else {
6546 line = 0;
6547 }
6548 di_element_types[debug_field_index] = ZigLLVMCreateDebugMemberType(g->dbuilder,
6549 ZigLLVMTypeToScope(struct_type->llvm_di_type), buf_ptr(type_struct_field->name),
6550 di_file, line,
6551 debug_size_in_bits,
6552 debug_align_in_bits,
6553 debug_offset_in_bits,
6554 0, field_di_type);
6555 assert(di_element_types[debug_field_index]);
6556 debug_field_index += 1;
6557 }
6558
6559 uint64_t debug_size_in_bits = 8*get_store_size_bytes(struct_type->size_in_bits);
6560 uint64_t debug_align_in_bits = 8*struct_type->abi_align;
6561 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
6562 di_scope,
6563 buf_ptr(&struct_type->name),
6564 di_file, line,
6565 debug_size_in_bits,
6566 debug_align_in_bits,
6567 0, nullptr, di_element_types, (int)debug_field_count, 0, nullptr, "");
6568
6569 ZigLLVMReplaceTemporary(g->dbuilder, struct_type->llvm_di_type, replacement_di_type);
6570 struct_type->llvm_di_type = replacement_di_type;
6571 struct_type->data.structure.resolve_status = ResolveStatusLLVMFull;
6572}
6573
6574static void resolve_llvm_types_enum(CodeGen *g, ZigType *enum_type) {
6575 assert(!enum_type->data.enumeration.is_invalid);
6576 assert(enum_type->data.enumeration.complete);
6577 if (enum_type->llvm_di_type != nullptr) return;
6578
6579 Scope *scope = &enum_type->data.enumeration.decls_scope->base;
6580 ZigType *import = get_scope_import(scope);
6581 AstNode *decl_node = enum_type->data.enumeration.decl_node;
6582
6583 if (!type_has_bits(enum_type)) {
6584 enum_type->llvm_type = g->builtin_types.entry_void->llvm_type;
6585
6586 uint64_t debug_size_in_bits = 0;
6587 uint64_t debug_align_in_bits = 0;
6588 ZigLLVMDIType **di_element_types = nullptr;
6589 size_t debug_field_count = 0;
6590 enum_type->llvm_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
6591 ZigLLVMFileToScope(import->data.structure.root_struct->di_file),
6592 buf_ptr(&enum_type->name),
6593 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
6594 debug_size_in_bits,
6595 debug_align_in_bits,
6596 0, nullptr, di_element_types, (int)debug_field_count, 0, nullptr, "");
6597 return;
6598 }
6599
6600 uint32_t field_count = enum_type->data.enumeration.src_field_count;
6601
6602 assert(enum_type->data.enumeration.fields);
6603 ZigLLVMDIEnumerator **di_enumerators = allocate<ZigLLVMDIEnumerator*>(field_count);
6604
6605 for (uint32_t i = 0; i < field_count; i += 1) {
6606 TypeEnumField *enum_field = &enum_type->data.enumeration.fields[i];
6607
6608 // TODO send patch to LLVM to support APInt in createEnumerator instead of int64_t
6609 // http://lists.llvm.org/pipermail/llvm-dev/2017-December/119456.html
6610 di_enumerators[i] = ZigLLVMCreateDebugEnumerator(g->dbuilder, buf_ptr(enum_field->name),
6611 bigint_as_signed(&enum_field->value));
6612 }
6613
6614 ZigType *tag_int_type = enum_type->data.enumeration.tag_int_type;
6615 enum_type->llvm_type = get_llvm_type(g, tag_int_type);
6616
6617 // create debug type for tag
6618 uint64_t tag_debug_size_in_bits = tag_int_type->size_in_bits;
6619 uint64_t tag_debug_align_in_bits = 8*tag_int_type->abi_align;
6620 ZigLLVMDIType *tag_di_type = ZigLLVMCreateDebugEnumerationType(g->dbuilder,
6621 ZigLLVMFileToScope(import->data.structure.root_struct->di_file), buf_ptr(&enum_type->name),
6622 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
6623 tag_debug_size_in_bits,
6624 tag_debug_align_in_bits,
6625 di_enumerators, field_count,
6626 get_llvm_di_type(g, tag_int_type), "");
6627
6628 enum_type->llvm_di_type = tag_di_type;
6629}
6630
6631static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type, ResolveStatus wanted_resolve_status) {
6632 if (union_type->data.unionation.resolve_status >= wanted_resolve_status) return;
6633
6634 ZigType *most_aligned_union_member = union_type->data.unionation.most_aligned_union_member;
6635 ZigType *tag_type = union_type->data.unionation.tag_type;
6636 if (most_aligned_union_member == nullptr) {
6637 union_type->llvm_type = get_llvm_type(g, tag_type);
6638 union_type->llvm_di_type = get_llvm_di_type(g, tag_type);
6639 union_type->data.unionation.resolve_status = ResolveStatusLLVMFull;
6640 return;
6641 }
6642
6643 Scope *scope = &union_type->data.unionation.decls_scope->base;
6644 ZigType *import = get_scope_import(scope);
6645 AstNode *decl_node = union_type->data.unionation.decl_node;
6646
6647 if (union_type->data.unionation.resolve_status < ResolveStatusLLVMFwdDecl) {
6648 union_type->llvm_type = LLVMStructCreateNamed(LLVMGetGlobalContext(), buf_ptr(&union_type->name));
6649 size_t line = decl_node ? decl_node->line : 0;
6650 unsigned dwarf_kind = ZigLLVMTag_DW_structure_type();
6651 union_type->llvm_di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
6652 dwarf_kind, buf_ptr(&union_type->name),
6653 ZigLLVMFileToScope(import->data.structure.root_struct->di_file),
6654 import->data.structure.root_struct->di_file, (unsigned)(line + 1));
6655
6656 union_type->data.unionation.resolve_status = ResolveStatusLLVMFwdDecl;
6657 if (ResolveStatusLLVMFwdDecl >= wanted_resolve_status) return;
6658 }
6659
6660 uint32_t gen_field_count = union_type->data.unionation.gen_field_count;
6661 ZigLLVMDIType **union_inner_di_types = allocate<ZigLLVMDIType*>(gen_field_count);
6662 uint32_t field_count = union_type->data.unionation.src_field_count;
6663 for (uint32_t i = 0; i < field_count; i += 1) {
6664 TypeUnionField *union_field = &union_type->data.unionation.fields[i];
6665 if (!type_has_bits(union_field->type_entry))
6666 continue;
6667
6668 ZigLLVMDIType *field_di_type = get_llvm_di_type(g, union_field->type_entry);
6669 if (union_type->data.unionation.resolve_status >= wanted_resolve_status) return;
6670
6671 uint64_t store_size_in_bits = union_field->type_entry->size_in_bits;
6672 uint64_t abi_align_in_bits = 8*union_field->type_entry->abi_align;
6673 AstNode *field_node = decl_node->data.container_decl.fields.at(i);
6674 union_inner_di_types[union_field->gen_index] = ZigLLVMCreateDebugMemberType(g->dbuilder,
6675 ZigLLVMTypeToScope(union_type->llvm_di_type), buf_ptr(union_field->enum_field->name),
6676 import->data.structure.root_struct->di_file, (unsigned)(field_node->line + 1),
6677 store_size_in_bits,
6678 abi_align_in_bits,
6679 0,
6680 0, field_di_type);
6681
6682 }
6683
6684 if (tag_type == nullptr || !type_has_bits(tag_type)) {
6685 assert(most_aligned_union_member != nullptr);
6686
6687 size_t padding_bytes = union_type->data.unionation.union_abi_size - most_aligned_union_member->abi_size;
6688 if (padding_bytes > 0) {
6689 ZigType *u8_type = get_int_type(g, false, 8);
6690 ZigType *padding_array = get_array_type(g, u8_type, padding_bytes);
6691 LLVMTypeRef union_element_types[] = {
6692 most_aligned_union_member->llvm_type,
6693 get_llvm_type(g, padding_array),
6694 };
6695 LLVMStructSetBody(union_type->llvm_type, union_element_types, 2, false);
6696 } else {
6697 LLVMStructSetBody(union_type->llvm_type, &most_aligned_union_member->llvm_type, 1, false);
6698 }
6699 union_type->data.unionation.union_llvm_type = union_type->llvm_type;
6700 union_type->data.unionation.gen_tag_index = SIZE_MAX;
6701 union_type->data.unionation.gen_union_index = SIZE_MAX;
6702
6703 // create debug type for union
6704 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugUnionType(g->dbuilder,
6705 ZigLLVMFileToScope(import->data.structure.root_struct->di_file), buf_ptr(&union_type->name),
6706 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
6707 union_type->data.unionation.union_abi_size * 8,
6708 most_aligned_union_member->abi_align * 8,
6709 0, union_inner_di_types,
6710 gen_field_count, 0, "");
6711
6712 ZigLLVMReplaceTemporary(g->dbuilder, union_type->llvm_di_type, replacement_di_type);
6713 union_type->llvm_di_type = replacement_di_type;
6714 union_type->data.unionation.resolve_status = ResolveStatusLLVMFull;
6715 return;
6716 }
6717
6718 LLVMTypeRef union_type_ref;
6719 size_t padding_bytes = union_type->data.unionation.union_abi_size - most_aligned_union_member->abi_size;
6720 if (padding_bytes == 0) {
6721 union_type_ref = get_llvm_type(g, most_aligned_union_member);
6722 } else {
6723 ZigType *u8_type = get_int_type(g, false, 8);
6724 ZigType *padding_array = get_array_type(g, u8_type, padding_bytes);
6725 LLVMTypeRef union_element_types[] = {
6726 get_llvm_type(g, most_aligned_union_member),
6727 get_llvm_type(g, padding_array),
6728 };
6729 union_type_ref = LLVMStructType(union_element_types, 2, false);
6730 }
6731 union_type->data.unionation.union_llvm_type = union_type_ref;
6732
6733 LLVMTypeRef root_struct_element_types[2];
6734 root_struct_element_types[union_type->data.unionation.gen_tag_index] = get_llvm_type(g, tag_type);
6735 root_struct_element_types[union_type->data.unionation.gen_union_index] = union_type_ref;
6736 LLVMStructSetBody(union_type->llvm_type, root_struct_element_types, 2, false);
6737
6738 // create debug type for union
6739 ZigLLVMDIType *union_di_type = ZigLLVMCreateDebugUnionType(g->dbuilder,
6740 ZigLLVMTypeToScope(union_type->llvm_di_type), "AnonUnion",
6741 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
6742 most_aligned_union_member->size_in_bits, 8*most_aligned_union_member->abi_align,
6743 0, union_inner_di_types, gen_field_count, 0, "");
6744
6745 uint64_t union_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, union_type->llvm_type,
6746 union_type->data.unionation.gen_union_index);
6747 uint64_t tag_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, union_type->llvm_type,
6748 union_type->data.unionation.gen_tag_index);
6749
6750 ZigLLVMDIType *union_member_di_type = ZigLLVMCreateDebugMemberType(g->dbuilder,
6751 ZigLLVMTypeToScope(union_type->llvm_di_type), "payload",
6752 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
6753 most_aligned_union_member->size_in_bits,
6754 8*most_aligned_union_member->abi_align,
6755 union_offset_in_bits,
6756 0, union_di_type);
6757
6758 uint64_t tag_debug_size_in_bits = tag_type->size_in_bits;
6759 uint64_t tag_debug_align_in_bits = 8*tag_type->abi_align;
6760
6761 ZigLLVMDIType *tag_member_di_type = ZigLLVMCreateDebugMemberType(g->dbuilder,
6762 ZigLLVMTypeToScope(union_type->llvm_di_type), "tag",
6763 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
6764 tag_debug_size_in_bits,
6765 tag_debug_align_in_bits,
6766 tag_offset_in_bits,
6767 0, get_llvm_di_type(g, tag_type));
6768
6769 ZigLLVMDIType *di_root_members[2];
6770 di_root_members[union_type->data.unionation.gen_tag_index] = tag_member_di_type;
6771 di_root_members[union_type->data.unionation.gen_union_index] = union_member_di_type;
6772
6773 uint64_t debug_size_in_bits = union_type->size_in_bits;
6774 uint64_t debug_align_in_bits = 8*union_type->abi_align;
6775 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
6776 ZigLLVMFileToScope(import->data.structure.root_struct->di_file),
6777 buf_ptr(&union_type->name),
6778 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
6779 debug_size_in_bits,
6780 debug_align_in_bits,
6781 0, nullptr, di_root_members, 2, 0, nullptr, "");
6782
6783 ZigLLVMReplaceTemporary(g->dbuilder, union_type->llvm_di_type, replacement_di_type);
6784 union_type->llvm_di_type = replacement_di_type;
6785 union_type->data.unionation.resolve_status = ResolveStatusLLVMFull;
6786}
6787
6788static void resolve_llvm_types_pointer(CodeGen *g, ZigType *type) {
6789 if (type->llvm_di_type != nullptr) return;
6790
6791 if (!type_has_bits(type)) {
6792 type->llvm_type = g->builtin_types.entry_void->llvm_type;
6793 type->llvm_di_type = g->builtin_types.entry_void->llvm_di_type;
6794 return;
6795 }
6796
6797 ZigType *elem_type = type->data.pointer.child_type;
6798
6799 if (type->data.pointer.is_const || type->data.pointer.is_volatile ||
6800 type->data.pointer.explicit_alignment != 0 || type->data.pointer.ptr_len != PtrLenSingle ||
6801 type->data.pointer.bit_offset_in_host != 0 || type->data.pointer.allow_zero)
6802 {
6803 ZigType *peer_type = get_pointer_to_type_extra(g, elem_type, false, false,
6804 PtrLenSingle, 0, 0, type->data.pointer.host_int_bytes, false);
6805 type->llvm_type = get_llvm_type(g, peer_type);
6806 type->llvm_di_type = get_llvm_di_type(g, peer_type);
6807 return;
6808 }
6809
6810 if (type->data.pointer.host_int_bytes == 0) {
6811 assertNoError(type_resolve(g, elem_type, ResolveStatusLLVMFwdDecl));
6812 type->llvm_type = LLVMPointerType(elem_type->llvm_type, 0);
6813 uint64_t debug_size_in_bits = 8*get_store_size_bytes(type->size_in_bits);
6814 uint64_t debug_align_in_bits = 8*type->abi_align;
6815 type->llvm_di_type = ZigLLVMCreateDebugPointerType(g->dbuilder, elem_type->llvm_di_type,
6816 debug_size_in_bits, debug_align_in_bits, buf_ptr(&type->name));
6817 assertNoError(type_resolve(g, elem_type, ResolveStatusLLVMFull));
6818 } else {
6819 ZigType *host_int_type = get_int_type(g, false, type->data.pointer.host_int_bytes * 8);
6820 LLVMTypeRef host_int_llvm_type = get_llvm_type(g, host_int_type);
6821 type->llvm_type = LLVMPointerType(host_int_llvm_type, 0);
6822 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, host_int_llvm_type);
6823 uint64_t debug_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, host_int_llvm_type);
6824 type->llvm_di_type = ZigLLVMCreateDebugPointerType(g->dbuilder, get_llvm_di_type(g, host_int_type),
6825 debug_size_in_bits, debug_align_in_bits, buf_ptr(&type->name));
6826 }
6827}
6828
6829static void resolve_llvm_types_integer(CodeGen *g, ZigType *type) {
6830 if (type->llvm_di_type != nullptr) return;
6831
6832 if (!type_has_bits(type)) {
6833 type->llvm_type = g->builtin_types.entry_void->llvm_type;
6834 type->llvm_di_type = g->builtin_types.entry_void->llvm_di_type;
6835 return;
6836 }
6837
6838 unsigned dwarf_tag;
6839 if (type->data.integral.is_signed) {
6840 if (type->size_in_bits == 8) {
6841 dwarf_tag = ZigLLVMEncoding_DW_ATE_signed_char();
6842 } else {
6843 dwarf_tag = ZigLLVMEncoding_DW_ATE_signed();
6844 }
6845 } else {
6846 if (type->size_in_bits == 8) {
6847 dwarf_tag = ZigLLVMEncoding_DW_ATE_unsigned_char();
6848 } else {
6849 dwarf_tag = ZigLLVMEncoding_DW_ATE_unsigned();
6850 }
6851 }
6852
6853 type->llvm_di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&type->name), type->size_in_bits, dwarf_tag);
6854 type->llvm_type = LLVMIntType(type->size_in_bits);
6855}
6856
6857static void resolve_llvm_types_optional(CodeGen *g, ZigType *type) {
6858 if (type->llvm_di_type != nullptr) return;
6859
6860 LLVMTypeRef bool_llvm_type = get_llvm_type(g, g->builtin_types.entry_bool);
6861 ZigLLVMDIType *bool_llvm_di_type = get_llvm_di_type(g, g->builtin_types.entry_bool);
6862
6863 ZigType *child_type = type->data.maybe.child_type;
6864 if (!type_has_bits(child_type)) {
6865 type->llvm_type = bool_llvm_type;
6866 type->llvm_di_type = bool_llvm_di_type;
6867 return;
6868 }
6869
6870 LLVMTypeRef child_llvm_type = get_llvm_type(g, child_type);
6871 ZigLLVMDIType *child_llvm_di_type = get_llvm_di_type(g, child_type);
6872
6873 if (type_is_nonnull_ptr(child_type) || child_type->id == ZigTypeIdErrorSet) {
6874 type->llvm_type = child_llvm_type;
6875 type->llvm_di_type = child_llvm_di_type;
6876 return;
6877 }
6878
6879 LLVMTypeRef elem_types[] = {
6880 get_llvm_type(g, child_type),
6881 LLVMInt1Type(),
6882 };
6883 type->llvm_type = LLVMStructType(elem_types, 2, false);
6884
6885 ZigLLVMDIScope *compile_unit_scope = ZigLLVMCompileUnitToScope(g->compile_unit);
6886 ZigLLVMDIFile *di_file = nullptr;
6887 unsigned line = 0;
6888 type->llvm_di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
6889 ZigLLVMTag_DW_structure_type(), buf_ptr(&type->name),
6890 compile_unit_scope, di_file, line);
6891
6892 uint64_t val_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, child_llvm_type);
6893 uint64_t val_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, child_llvm_type);
6894 uint64_t val_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, type->llvm_type, 0);
6895
6896 uint64_t maybe_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, bool_llvm_type);
6897 uint64_t maybe_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, bool_llvm_type);
6898 uint64_t maybe_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, type->llvm_type, 1);
6899
6900 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, type->llvm_type);
6901 uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, type->llvm_type);
6902
6903 ZigLLVMDIType *di_element_types[] = {
6904 ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(type->llvm_di_type),
6905 "val", di_file, line,
6906 val_debug_size_in_bits,
6907 val_debug_align_in_bits,
6908 val_offset_in_bits,
6909 0, child_llvm_di_type),
6910 ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(type->llvm_di_type),
6911 "maybe", di_file, line,
6912 maybe_debug_size_in_bits,
6913 maybe_debug_align_in_bits,
6914 maybe_offset_in_bits,
6915 0, bool_llvm_di_type),
6916 };
6917 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
6918 compile_unit_scope,
6919 buf_ptr(&type->name),
6920 di_file, line, debug_size_in_bits, debug_align_in_bits, 0,
6921 nullptr, di_element_types, 2, 0, nullptr, "");
6922
6923 ZigLLVMReplaceTemporary(g->dbuilder, type->llvm_di_type, replacement_di_type);
6924 type->llvm_di_type = replacement_di_type;
6925}
6926
6927static void resolve_llvm_types_error_union(CodeGen *g, ZigType *type) {
6928 if (type->llvm_di_type != nullptr) return;
6929
6930 ZigType *payload_type = type->data.error_union.payload_type;
6931 ZigType *err_set_type = type->data.error_union.err_set_type;
6932
6933 if (!type_has_bits(payload_type)) {
6934 assert(type_has_bits(err_set_type));
6935 type->llvm_type = get_llvm_type(g, err_set_type);
6936 type->llvm_di_type = get_llvm_di_type(g, err_set_type);
6937 } else if (!type_has_bits(err_set_type)) {
6938 type->llvm_type = get_llvm_type(g, payload_type);
6939 type->llvm_di_type = get_llvm_di_type(g, payload_type);
6940 } else {
6941 LLVMTypeRef err_set_llvm_type = get_llvm_type(g, err_set_type);
6942 LLVMTypeRef payload_llvm_type = get_llvm_type(g, payload_type);
6943 LLVMTypeRef elem_types[2];
6944 elem_types[err_union_err_index] = err_set_llvm_type;
6945 elem_types[err_union_payload_index] = payload_llvm_type;
6946 type->llvm_type = LLVMStructType(elem_types, 2, false);
6947
6948 ZigLLVMDIScope *compile_unit_scope = ZigLLVMCompileUnitToScope(g->compile_unit);
6949 ZigLLVMDIFile *di_file = nullptr;
6950 unsigned line = 0;
6951 type->llvm_di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
6952 ZigLLVMTag_DW_structure_type(), buf_ptr(&type->name),
6953 compile_unit_scope, di_file, line);
6954
6955 uint64_t tag_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, err_set_llvm_type);
6956 uint64_t tag_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, err_set_llvm_type);
6957 uint64_t tag_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, type->llvm_type, err_union_err_index);
6958
6959 uint64_t value_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, payload_llvm_type);
6960 uint64_t value_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, payload_llvm_type);
6961 uint64_t value_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, type->llvm_type,
6962 err_union_payload_index);
6963
6964 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, type->llvm_type);
6965 uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, type->llvm_type);
6966
6967 ZigLLVMDIType *di_element_types[] = {
6968 ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(type->llvm_di_type),
6969 "tag", di_file, line,
6970 tag_debug_size_in_bits,
6971 tag_debug_align_in_bits,
6972 tag_offset_in_bits,
6973 0, get_llvm_di_type(g, err_set_type)),
6974 ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(type->llvm_di_type),
6975 "value", di_file, line,
6976 value_debug_size_in_bits,
6977 value_debug_align_in_bits,
6978 value_offset_in_bits,
6979 0, get_llvm_di_type(g, payload_type)),
6980 };
6981
6982 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
6983 compile_unit_scope,
6984 buf_ptr(&type->name),
6985 di_file, line,
6986 debug_size_in_bits,
6987 debug_align_in_bits,
6988 0,
6989 nullptr, di_element_types, 2, 0, nullptr, "");
6990
6991 ZigLLVMReplaceTemporary(g->dbuilder, type->llvm_di_type, replacement_di_type);
6992 type->llvm_di_type = replacement_di_type;
6993 }
6994}
6995
6996static void resolve_llvm_types_array(CodeGen *g, ZigType *type) {
6997 if (type->llvm_di_type != nullptr) return;
6998
6999 if (!type_has_bits(type)) {
7000 type->llvm_type = g->builtin_types.entry_void->llvm_type;
7001 type->llvm_di_type = g->builtin_types.entry_void->llvm_di_type;
7002 return;
7003 }
7004
7005 ZigType *elem_type = type->data.array.child_type;
7006
7007 // TODO https://github.com/ziglang/zig/issues/1424
7008 type->llvm_type = LLVMArrayType(get_llvm_type(g, elem_type), (unsigned)type->data.array.len);
7009
7010 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, type->llvm_type);
7011 uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, type->llvm_type);
7012
7013 type->llvm_di_type = ZigLLVMCreateDebugArrayType(g->dbuilder, debug_size_in_bits,
7014 debug_align_in_bits, get_llvm_di_type(g, elem_type), (int)type->data.array.len);
7015}
7016
7017static void resolve_llvm_types_fn(CodeGen *g, ZigType *fn_type) {
7018 if (fn_type->llvm_di_type != nullptr) return;
7019
7020 FnTypeId *fn_type_id = &fn_type->data.fn.fn_type_id;
7021 bool first_arg_return = want_first_arg_sret(g, fn_type_id);
7022 bool is_async = fn_type_id->cc == CallingConventionAsync;
7023 bool is_c_abi = fn_type_id->cc == CallingConventionC;
7024 bool prefix_arg_error_return_trace = g->have_err_ret_tracing && fn_type_can_fail(fn_type_id);
7025 // +1 for maybe making the first argument the return value
7026 // +1 for maybe first argument the error return trace
7027 // +2 for maybe arguments async allocator and error code pointer
7028 ZigList<LLVMTypeRef> gen_param_types = {};
7029 // +1 because 0 is the return type and
7030 // +1 for maybe making first arg ret val and
7031 // +1 for maybe first argument the error return trace
7032 // +2 for maybe arguments async allocator and error code pointer
7033 ZigList<ZigLLVMDIType *> param_di_types = {};
7034 param_di_types.append(get_llvm_di_type(g, fn_type_id->return_type));
7035 ZigType *gen_return_type;
7036 if (is_async) {
7037 gen_return_type = get_pointer_to_type(g, g->builtin_types.entry_u8, false);
7038 } else if (!type_has_bits(fn_type_id->return_type)) {
7039 gen_return_type = g->builtin_types.entry_void;
7040 } else if (first_arg_return) {
7041 ZigType *gen_type = get_pointer_to_type(g, fn_type_id->return_type, false);
7042 gen_param_types.append(get_llvm_type(g, gen_type));
7043 param_di_types.append(get_llvm_di_type(g, gen_type));
7044 gen_return_type = g->builtin_types.entry_void;
7045 } else {
7046 gen_return_type = fn_type_id->return_type;
7047 }
7048 fn_type->data.fn.gen_return_type = gen_return_type;
7049
7050 if (prefix_arg_error_return_trace) {
7051 ZigType *gen_type = get_ptr_to_stack_trace_type(g);
7052 gen_param_types.append(get_llvm_type(g, gen_type));
7053 param_di_types.append(get_llvm_di_type(g, gen_type));
7054 }
7055 if (is_async) {
7056 {
7057 // async allocator param
7058 ZigType *gen_type = fn_type_id->async_allocator_type;
7059 gen_param_types.append(get_llvm_type(g, gen_type));
7060 param_di_types.append(get_llvm_di_type(g, gen_type));
7061 }
7062
7063 {
7064 // error code pointer
7065 ZigType *gen_type = get_pointer_to_type(g, g->builtin_types.entry_global_error_set, false);
7066 gen_param_types.append(get_llvm_type(g, gen_type));
7067 param_di_types.append(get_llvm_di_type(g, gen_type));
7068 }
7069 }
7070
7071 fn_type->data.fn.gen_param_info = allocate<FnGenParamInfo>(fn_type_id->param_count);
7072 for (size_t i = 0; i < fn_type_id->param_count; i += 1) {
7073 FnTypeParamInfo *src_param_info = &fn_type->data.fn.fn_type_id.param_info[i];
7074 ZigType *type_entry = src_param_info->type;
7075 FnGenParamInfo *gen_param_info = &fn_type->data.fn.gen_param_info[i];
7076
7077 gen_param_info->src_index = i;
7078 gen_param_info->gen_index = SIZE_MAX;
7079
7080 if (is_c_abi || !type_has_bits(type_entry))
7081 continue;
7082
7083 ZigType *gen_type;
7084 if (handle_is_ptr(type_entry)) {
7085 gen_type = get_pointer_to_type(g, type_entry, true);
7086 gen_param_info->is_byval = true;
7087 } else {
7088 gen_type = type_entry;
7089 }
7090 gen_param_info->gen_index = gen_param_types.length;
7091 gen_param_info->type = gen_type;
7092 gen_param_types.append(get_llvm_type(g, gen_type));
7093
7094 param_di_types.append(get_llvm_di_type(g, gen_type));
7095 }
7096
7097 if (is_c_abi) {
7098 FnWalk fn_walk = {};
7099 fn_walk.id = FnWalkIdTypes;
7100 fn_walk.data.types.param_di_types = &param_di_types;
7101 fn_walk.data.types.gen_param_types = &gen_param_types;
7102 walk_function_params(g, fn_type, &fn_walk);
7103 }
7104
7105 fn_type->data.fn.gen_param_count = gen_param_types.length;
7106
7107 for (size_t i = 0; i < gen_param_types.length; i += 1) {
7108 assert(gen_param_types.items[i] != nullptr);
7109 }
7110 fn_type->data.fn.raw_type_ref = LLVMFunctionType(get_llvm_type(g, gen_return_type),
7111 gen_param_types.items, (unsigned int)gen_param_types.length, fn_type_id->is_var_args);
7112 fn_type->llvm_type = LLVMPointerType(fn_type->data.fn.raw_type_ref, 0);
7113 fn_type->data.fn.raw_di_type = ZigLLVMCreateSubroutineType(g->dbuilder, param_di_types.items, (int)param_di_types.length, 0);
7114 fn_type->llvm_di_type = ZigLLVMCreateDebugPointerType(g->dbuilder, fn_type->data.fn.raw_di_type,
7115 LLVMStoreSizeOfType(g->target_data_ref, fn_type->llvm_type),
7116 LLVMABIAlignmentOfType(g->target_data_ref, fn_type->llvm_type), "");
7117}
7118
7119static void resolve_llvm_types_anyerror(CodeGen *g) {
7120 ZigType *entry = g->builtin_types.entry_global_error_set;
7121 entry->llvm_type = get_llvm_type(g, g->err_tag_type);
7122 ZigList<ZigLLVMDIEnumerator *> err_enumerators = {};
7123 // reserve index 0 to indicate no error
7124 err_enumerators.append(ZigLLVMCreateDebugEnumerator(g->dbuilder, "(none)", 0));
7125 for (size_t i = 1; i < g->errors_by_index.length; i += 1) {
7126 ErrorTableEntry *error_entry = g->errors_by_index.at(i);
7127 err_enumerators.append(ZigLLVMCreateDebugEnumerator(g->dbuilder, buf_ptr(&error_entry->name), i));
7128 }
7129
7130 // create debug type for error sets
7131 uint64_t tag_debug_size_in_bits = g->err_tag_type->size_in_bits;
7132 uint64_t tag_debug_align_in_bits = 8*g->err_tag_type->abi_align;
7133 ZigLLVMDIFile *err_set_di_file = nullptr;
7134 entry->llvm_di_type = ZigLLVMCreateDebugEnumerationType(g->dbuilder,
7135 ZigLLVMCompileUnitToScope(g->compile_unit), buf_ptr(&entry->name),
7136 err_set_di_file, 0,
7137 tag_debug_size_in_bits,
7138 tag_debug_align_in_bits,
7139 err_enumerators.items, err_enumerators.length,
7140 get_llvm_di_type(g, g->err_tag_type), "");
7141}
7142
7143static void resolve_llvm_types(CodeGen *g, ZigType *type, ResolveStatus wanted_resolve_status) {
7144 assert(type->id == ZigTypeIdOpaque || type_is_resolved(type, ResolveStatusSizeKnown));
7145 assert(wanted_resolve_status > ResolveStatusSizeKnown);
7146 switch (type->id) {
7147 case ZigTypeIdInvalid:
7148 case ZigTypeIdMetaType:
7149 case ZigTypeIdComptimeFloat:
7150 case ZigTypeIdComptimeInt:
7151 case ZigTypeIdEnumLiteral:
7152 case ZigTypeIdUndefined:
7153 case ZigTypeIdNull:
7154 case ZigTypeIdBoundFn:
7155 case ZigTypeIdArgTuple:
7156 zig_unreachable();
7157 case ZigTypeIdFloat:
7158 case ZigTypeIdOpaque:
7159 case ZigTypeIdVoid:
7160 case ZigTypeIdBool:
7161 case ZigTypeIdUnreachable:
7162 assert(type->llvm_di_type != nullptr);
7163 return;
7164 case ZigTypeIdStruct:
7165 if (type->data.structure.is_slice)
7166 return resolve_llvm_types_slice(g, type, wanted_resolve_status);
7167 else
7168 return resolve_llvm_types_struct(g, type, wanted_resolve_status);
7169 case ZigTypeIdEnum:
7170 return resolve_llvm_types_enum(g, type);
7171 case ZigTypeIdUnion:
7172 return resolve_llvm_types_union(g, type, wanted_resolve_status);
7173 case ZigTypeIdPointer:
7174 return resolve_llvm_types_pointer(g, type);
7175 case ZigTypeIdPromise: {
7176 if (type->llvm_di_type != nullptr) return;
7177 ZigType *u8_ptr_type = get_pointer_to_type(g, g->builtin_types.entry_u8, false);
7178 type->llvm_type = get_llvm_type(g, u8_ptr_type);
7179 type->llvm_di_type = get_llvm_di_type(g, u8_ptr_type);
7180 return;
7181 }
7182 case ZigTypeIdInt:
7183 return resolve_llvm_types_integer(g, type);
7184 case ZigTypeIdOptional:
7185 return resolve_llvm_types_optional(g, type);
7186 case ZigTypeIdErrorUnion:
7187 return resolve_llvm_types_error_union(g, type);
7188 case ZigTypeIdArray:
7189 return resolve_llvm_types_array(g, type);
7190 case ZigTypeIdFn:
7191 return resolve_llvm_types_fn(g, type);
7192 case ZigTypeIdErrorSet: {
7193 if (type->llvm_di_type != nullptr) return;
7194
7195 if (g->builtin_types.entry_global_error_set->llvm_type == nullptr) {
7196 resolve_llvm_types_anyerror(g);
7197 }
7198 type->llvm_type = g->builtin_types.entry_global_error_set->llvm_type;
7199 type->llvm_di_type = g->builtin_types.entry_global_error_set->llvm_di_type;
7200 return;
7201 }
7202 case ZigTypeIdVector: {
7203 if (type->llvm_di_type != nullptr) return;
7204
7205 type->llvm_type = LLVMVectorType(get_llvm_type(g, type->data.vector.elem_type), type->data.vector.len);
7206 type->llvm_di_type = ZigLLVMDIBuilderCreateVectorType(g->dbuilder, type->size_in_bits,
7207 type->abi_align, get_llvm_di_type(g, type->data.vector.elem_type), type->data.vector.len);
7208 return;
7209 }
7210 }
7211 zig_unreachable();
7212}
7213
7214LLVMTypeRef get_llvm_type(CodeGen *g, ZigType *type) {
7215 assertNoError(type_resolve(g, type, ResolveStatusLLVMFull));
7216 assert(type->abi_size == 0 || type->abi_size == LLVMABISizeOfType(g->target_data_ref, type->llvm_type));
7217 assert(type->abi_align == 0 || type->abi_align == LLVMABIAlignmentOfType(g->target_data_ref, type->llvm_type));
7218 return type->llvm_type;
7219}
7220
7221ZigLLVMDIType *get_llvm_di_type(CodeGen *g, ZigType *type) {
7222 assertNoError(type_resolve(g, type, ResolveStatusLLVMFull));
7223 return type->llvm_di_type;
7224}
src/analyze.hpp+4-1
......@@ -73,7 +73,7 @@ bool type_is_complete(ZigType *type_entry);
7373bool type_is_resolved(ZigType *type_entry, ResolveStatus status);
7474bool type_is_invalid(ZigType *type_entry);
7575bool type_is_global_error_set(ZigType *err_set_type);
76void resolve_container_type(CodeGen *g, ZigType *type_entry);
76Error resolve_container_type(CodeGen *g, ZigType *type_entry);
7777ScopeDecls *get_container_scope(ZigType *type_entry);
7878TypeStructField *find_struct_type_field(ZigType *type_entry, Buf *name);
7979TypeEnumField *find_enum_type_field(ZigType *enum_type, Buf *name);
......@@ -244,4 +244,7 @@ Buf *type_bare_name(ZigType *t);
244244Buf *type_h_name(ZigType *t);
245245Error create_c_object_cache(CodeGen *g, CacheHash **out_cache_hash, bool verbose);
246246
247LLVMTypeRef get_llvm_type(CodeGen *g, ZigType *type);
248ZigLLVMDIType *get_llvm_di_type(CodeGen *g, ZigType *type);
249
247250#endif
src/codegen.cpp+351-346
......@@ -483,6 +483,8 @@ static LLVMValueRef fn_llvm_value(CodeGen *g, ZigFn *fn_table_entry) {
483483
484484
485485 ZigType *fn_type = fn_table_entry->type_entry;
486 // Make the raw_type_ref populated
487 (void)get_llvm_type(g, fn_type);
486488 LLVMTypeRef fn_llvm_type = fn_type->data.fn.raw_type_ref;
487489 if (fn_table_entry->body_node == nullptr) {
488490 LLVMValueRef existing_llvm_fn = LLVMGetNamedFunction(g->module, buf_ptr(symbol_name));
......@@ -496,6 +498,8 @@ static LLVMValueRef fn_llvm_value(CodeGen *g, ZigFn *fn_table_entry) {
496498 } else {
497499 assert(entry->value->id == TldIdFn);
498500 TldFn *tld_fn = reinterpret_cast<TldFn *>(entry->value);
501 // Make the raw_type_ref populated
502 (void)get_llvm_type(g, tld_fn->fn_entry->type_entry);
499503 tld_fn->fn_entry->llvm_value = LLVMAddFunction(g->module, buf_ptr(symbol_name),
500504 tld_fn->fn_entry->type_entry->data.fn.raw_type_ref);
501505 fn_table_entry->llvm_value = LLVMConstBitCast(tld_fn->fn_entry->llvm_value,
......@@ -668,7 +672,7 @@ static ZigLLVMDIScope *get_di_scope(CodeGen *g, Scope *scope) {
668672 if (scope->parent) {
669673 ScopeDecls *decls_scope = (ScopeDecls *)scope;
670674 assert(decls_scope->container_type);
671 scope->di_scope = ZigLLVMTypeToScope(decls_scope->container_type->di_type);
675 scope->di_scope = ZigLLVMTypeToScope(get_llvm_di_type(g, decls_scope->container_type));
672676 } else {
673677 scope->di_scope = ZigLLVMFileToScope(import->data.structure.root_struct->di_file);
674678 }
......@@ -711,8 +715,8 @@ static LLVMValueRef get_arithmetic_overflow_fn(CodeGen *g, ZigType *operand_type
711715 const char *signed_str = int_type->data.integral.is_signed ? signed_name : unsigned_name;
712716
713717 LLVMTypeRef param_types[] = {
714 operand_type->type_ref,
715 operand_type->type_ref,
718 get_llvm_type(g, operand_type),
719 get_llvm_type(g, operand_type),
716720 };
717721
718722 if (operand_type->id == ZigTypeIdVector) {
......@@ -720,7 +724,7 @@ static LLVMValueRef get_arithmetic_overflow_fn(CodeGen *g, ZigType *operand_type
720724 operand_type->data.vector.len, int_type->data.integral.bit_count);
721725
722726 LLVMTypeRef return_elem_types[] = {
723 operand_type->type_ref,
727 get_llvm_type(g, operand_type),
724728 LLVMVectorType(LLVMInt1Type(), operand_type->data.vector.len),
725729 };
726730 LLVMTypeRef return_struct_type = LLVMStructType(return_elem_types, 2, false);
......@@ -732,7 +736,7 @@ static LLVMValueRef get_arithmetic_overflow_fn(CodeGen *g, ZigType *operand_type
732736 sprintf(fn_name, "llvm.%s.with.overflow.i%" PRIu32, signed_str, int_type->data.integral.bit_count);
733737
734738 LLVMTypeRef return_elem_types[] = {
735 operand_type->type_ref,
739 get_llvm_type(g, operand_type),
736740 LLVMInt1Type(),
737741 };
738742 LLVMTypeRef return_struct_type = LLVMStructType(return_elem_types, 2, false);
......@@ -800,7 +804,8 @@ static LLVMValueRef get_float_fn(CodeGen *g, ZigType *type_entry, ZigLLVMFnId fn
800804
801805 char fn_name[64];
802806 sprintf(fn_name, "llvm.%s.f%" ZIG_PRI_usize "", name, type_entry->data.floating.bit_count);
803 LLVMTypeRef fn_type = LLVMFunctionType(type_entry->type_ref, &type_entry->type_ref, 1, false);
807 LLVMTypeRef float_type_ref = get_llvm_type(g, type_entry);
808 LLVMTypeRef fn_type = LLVMFunctionType(float_type_ref, &float_type_ref, 1, false);
804809 LLVMValueRef fn_val = LLVMAddFunction(g->module, fn_name, fn_type);
805810 assert(LLVMGetIntrinsicID(fn_val));
806811
......@@ -958,7 +963,7 @@ static LLVMValueRef get_panic_msg_ptr_val(CodeGen *g, PanicMsgId msg_id) {
958963 ZigType *u8_ptr_type = get_pointer_to_type_extra(g, g->builtin_types.entry_u8, true, false,
959964 PtrLenUnknown, get_abi_alignment(g, g->builtin_types.entry_u8), 0, 0, false);
960965 ZigType *str_type = get_slice_type(g, u8_ptr_type);
961 return LLVMConstBitCast(val->global_refs->llvm_global, LLVMPointerType(str_type->type_ref, 0));
966 return LLVMConstBitCast(val->global_refs->llvm_global, LLVMPointerType(get_llvm_type(g, str_type), 0));
962967}
963968
964969static void gen_panic(CodeGen *g, LLVMValueRef msg_arg, LLVMValueRef stack_trace_arg) {
......@@ -967,7 +972,7 @@ static void gen_panic(CodeGen *g, LLVMValueRef msg_arg, LLVMValueRef stack_trace
967972 LLVMCallConv llvm_cc = get_llvm_cc(g, g->panic_fn->type_entry->data.fn.fn_type_id.cc);
968973 if (stack_trace_arg == nullptr) {
969974 ZigType *ptr_to_stack_trace_type = get_ptr_to_stack_trace_type(g);
970 stack_trace_arg = LLVMConstNull(ptr_to_stack_trace_type->type_ref);
975 stack_trace_arg = LLVMConstNull(get_llvm_type(g, ptr_to_stack_trace_type));
971976 }
972977 LLVMValueRef args[] = {
973978 msg_arg,
......@@ -1081,7 +1086,7 @@ static LLVMValueRef get_coro_size_fn_val(CodeGen *g) {
10811086 if (g->coro_size_fn_val)
10821087 return g->coro_size_fn_val;
10831088
1084 LLVMTypeRef fn_type = LLVMFunctionType(g->builtin_types.entry_usize->type_ref, nullptr, 0, false);
1089 LLVMTypeRef fn_type = LLVMFunctionType(g->builtin_types.entry_usize->llvm_type, nullptr, 0, false);
10851090 Buf *name = buf_sprintf("llvm.coro.size.i%d", g->pointer_size_bytes * 8);
10861091 g->coro_size_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type);
10871092 assert(LLVMGetIntrinsicID(g->coro_size_fn_val));
......@@ -1206,8 +1211,8 @@ static LLVMValueRef get_return_address_fn_val(CodeGen *g) {
12061211
12071212 ZigType *return_type = get_pointer_to_type(g, g->builtin_types.entry_u8, true);
12081213
1209 LLVMTypeRef fn_type = LLVMFunctionType(return_type->type_ref,
1210 &g->builtin_types.entry_i32->type_ref, 1, false);
1214 LLVMTypeRef fn_type = LLVMFunctionType(get_llvm_type(g, return_type),
1215 &g->builtin_types.entry_i32->llvm_type, 1, false);
12111216 g->return_address_fn_val = LLVMAddFunction(g->module, "llvm.returnaddress", fn_type);
12121217 assert(LLVMGetIntrinsicID(g->return_address_fn_val));
12131218
......@@ -1219,8 +1224,8 @@ static LLVMValueRef get_add_error_return_trace_addr_fn(CodeGen *g) {
12191224 return g->add_error_return_trace_addr_fn_val;
12201225
12211226 LLVMTypeRef arg_types[] = {
1222 get_ptr_to_stack_trace_type(g)->type_ref,
1223 g->builtin_types.entry_usize->type_ref,
1227 get_llvm_type(g, get_ptr_to_stack_trace_type(g)),
1228 g->builtin_types.entry_usize->llvm_type,
12241229 };
12251230 LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMVoidType(), arg_types, 2, false);
12261231
......@@ -1245,7 +1250,7 @@ static LLVMValueRef get_add_error_return_trace_addr_fn(CodeGen *g) {
12451250 LLVMPositionBuilderAtEnd(g->builder, entry_block);
12461251 ZigLLVMClearCurrentDebugLocation(g->builder);
12471252
1248 LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->type_ref;
1253 LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->llvm_type;
12491254
12501255 // stack_trace.instruction_addresses[stack_trace.index & (stack_trace.instruction_addresses.len - 1)] = return_address;
12511256
......@@ -1297,8 +1302,8 @@ static LLVMValueRef get_merge_err_ret_traces_fn_val(CodeGen *g) {
12971302 assert(g->stack_trace_type != nullptr);
12981303
12991304 LLVMTypeRef param_types[] = {
1300 get_ptr_to_stack_trace_type(g)->type_ref,
1301 get_ptr_to_stack_trace_type(g)->type_ref,
1305 get_llvm_type(g, get_ptr_to_stack_trace_type(g)),
1306 get_llvm_type(g, get_ptr_to_stack_trace_type(g)),
13021307 };
13031308 LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMVoidType(), param_types, 2, false);
13041309
......@@ -1350,8 +1355,8 @@ static LLVMValueRef get_merge_err_ret_traces_fn_val(CodeGen *g) {
13501355 // }
13511356 LLVMBasicBlockRef return_block = LLVMAppendBasicBlock(fn_val, "Return");
13521357
1353 LLVMValueRef frame_index_ptr = LLVMBuildAlloca(g->builder, g->builtin_types.entry_usize->type_ref, "frame_index");
1354 LLVMValueRef frames_left_ptr = LLVMBuildAlloca(g->builder, g->builtin_types.entry_usize->type_ref, "frames_left");
1358 LLVMValueRef frame_index_ptr = LLVMBuildAlloca(g->builder, g->builtin_types.entry_usize->llvm_type, "frame_index");
1359 LLVMValueRef frames_left_ptr = LLVMBuildAlloca(g->builder, g->builtin_types.entry_usize->llvm_type, "frames_left");
13551360
13561361 LLVMValueRef dest_stack_trace_ptr = LLVMGetParam(fn_val, 0);
13571362 LLVMValueRef src_stack_trace_ptr = LLVMGetParam(fn_val, 1);
......@@ -1377,14 +1382,14 @@ static LLVMValueRef get_merge_err_ret_traces_fn_val(CodeGen *g) {
13771382 LLVMBuildCondBr(g->builder, no_wrap_bit, no_wrap_block, yes_wrap_block);
13781383
13791384 LLVMPositionBuilderAtEnd(g->builder, no_wrap_block);
1380 LLVMValueRef usize_zero = LLVMConstNull(g->builtin_types.entry_usize->type_ref);
1385 LLVMValueRef usize_zero = LLVMConstNull(g->builtin_types.entry_usize->llvm_type);
13811386 LLVMBuildStore(g->builder, usize_zero, frame_index_ptr);
13821387 LLVMBuildStore(g->builder, src_index_val, frames_left_ptr);
13831388 LLVMValueRef frames_left_eq_zero_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, src_index_val, usize_zero, "");
13841389 LLVMBuildCondBr(g->builder, frames_left_eq_zero_bit, return_block, loop_block);
13851390
13861391 LLVMPositionBuilderAtEnd(g->builder, yes_wrap_block);
1387 LLVMValueRef usize_one = LLVMConstInt(g->builtin_types.entry_usize->type_ref, 1, false);
1392 LLVMValueRef usize_one = LLVMConstInt(g->builtin_types.entry_usize->llvm_type, 1, false);
13881393 LLVMValueRef plus_one = LLVMBuildNUWAdd(g->builder, src_index_val, usize_one, "");
13891394 LLVMValueRef mod_len = LLVMBuildURem(g->builder, plus_one, src_len_val, "");
13901395 LLVMBuildStore(g->builder, mod_len, frame_index_ptr);
......@@ -1430,7 +1435,7 @@ static LLVMValueRef get_return_err_fn(CodeGen *g) {
14301435
14311436 LLVMTypeRef arg_types[] = {
14321437 // error return trace pointer
1433 get_ptr_to_stack_trace_type(g)->type_ref,
1438 get_llvm_type(g, get_ptr_to_stack_trace_type(g)),
14341439 };
14351440 LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMVoidType(), arg_types, 1, false);
14361441
......@@ -1461,8 +1466,8 @@ static LLVMValueRef get_return_err_fn(CodeGen *g) {
14611466
14621467 LLVMValueRef err_ret_trace_ptr = LLVMGetParam(fn_val, 0);
14631468
1464 LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->type_ref;
1465 LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_i32->type_ref);
1469 LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->llvm_type;
1470 LLVMValueRef zero = LLVMConstNull(get_llvm_type(g, g->builtin_types.entry_i32));
14661471 LLVMValueRef return_address_ptr = LLVMBuildCall(g->builder, get_return_address_fn_val(g), &zero, 1, "");
14671472 LLVMValueRef return_address = LLVMBuildPtrToInt(g->builder, return_address_ptr, usize_type_ref, "");
14681473
......@@ -1508,8 +1513,8 @@ static LLVMValueRef get_safety_crash_err_fn(CodeGen *g) {
15081513
15091514 ZigType *usize = g->builtin_types.entry_usize;
15101515 LLVMValueRef full_buf_ptr_indices[] = {
1511 LLVMConstNull(usize->type_ref),
1512 LLVMConstNull(usize->type_ref),
1516 LLVMConstNull(usize->llvm_type),
1517 LLVMConstNull(usize->llvm_type),
15131518 };
15141519 LLVMValueRef full_buf_ptr = LLVMConstInBoundsGEP(global_array, full_buf_ptr_indices, 2);
15151520
......@@ -1519,9 +1524,9 @@ static LLVMValueRef get_safety_crash_err_fn(CodeGen *g) {
15191524 ZigType *str_type = get_slice_type(g, u8_ptr_type);
15201525 LLVMValueRef global_slice_fields[] = {
15211526 full_buf_ptr,
1522 LLVMConstNull(usize->type_ref),
1527 LLVMConstNull(usize->llvm_type),
15231528 };
1524 LLVMValueRef slice_init_value = LLVMConstNamedStruct(str_type->type_ref, global_slice_fields, 2);
1529 LLVMValueRef slice_init_value = LLVMConstNamedStruct(get_llvm_type(g, str_type), global_slice_fields, 2);
15251530 LLVMValueRef global_slice = LLVMAddGlobal(g->module, LLVMTypeOf(slice_init_value), "");
15261531 LLVMSetInitializer(global_slice, slice_init_value);
15271532 LLVMSetLinkage(global_slice, LLVMInternalLinkage);
......@@ -1530,15 +1535,15 @@ static LLVMValueRef get_safety_crash_err_fn(CodeGen *g) {
15301535 LLVMSetAlignment(global_slice, get_abi_alignment(g, str_type));
15311536
15321537 LLVMValueRef offset_ptr_indices[] = {
1533 LLVMConstNull(usize->type_ref),
1534 LLVMConstInt(usize->type_ref, unwrap_err_msg_text_len, false),
1538 LLVMConstNull(usize->llvm_type),
1539 LLVMConstInt(usize->llvm_type, unwrap_err_msg_text_len, false),
15351540 };
15361541 LLVMValueRef offset_buf_ptr = LLVMConstInBoundsGEP(global_array, offset_ptr_indices, 2);
15371542
15381543 Buf *fn_name = get_mangled_name(g, buf_create_from_str("__zig_fail_unwrap"), false);
15391544 LLVMTypeRef arg_types[] = {
1540 g->ptr_to_stack_trace_type->type_ref,
1541 g->err_tag_type->type_ref,
1545 get_llvm_type(g, g->ptr_to_stack_trace_type),
1546 get_llvm_type(g, g->err_tag_type),
15421547 };
15431548 LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMVoidType(), arg_types, 2, false);
15441549 LLVMValueRef fn_val = LLVMAddFunction(g->module, buf_ptr(fn_name), fn_type_ref);
......@@ -1565,7 +1570,7 @@ static LLVMValueRef get_safety_crash_err_fn(CodeGen *g) {
15651570 LLVMValueRef err_val = LLVMGetParam(fn_val, 1);
15661571
15671572 LLVMValueRef err_table_indices[] = {
1568 LLVMConstNull(g->builtin_types.entry_usize->type_ref),
1573 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),
15691574 err_val,
15701575 };
15711576 LLVMValueRef err_name_val = LLVMBuildInBoundsGEP(g->builder, g->err_name_table, err_table_indices, 2, "");
......@@ -1623,7 +1628,7 @@ static void gen_safety_crash_for_err(CodeGen *g, LLVMValueRef err_val, Scope *sc
16231628 LLVMValueRef err_ret_trace_val = get_cur_err_ret_trace_val(g, scope);
16241629 if (err_ret_trace_val == nullptr) {
16251630 ZigType *ptr_to_stack_trace_type = get_ptr_to_stack_trace_type(g);
1626 err_ret_trace_val = LLVMConstNull(ptr_to_stack_trace_type->type_ref);
1631 err_ret_trace_val = LLVMConstNull(get_llvm_type(g, ptr_to_stack_trace_type));
16271632 }
16281633 LLVMValueRef args[] = {
16291634 err_ret_trace_val,
......@@ -1669,7 +1674,7 @@ static void add_bounds_check(CodeGen *g, LLVMValueRef target_val,
16691674}
16701675
16711676static LLVMValueRef gen_assert_zero(CodeGen *g, LLVMValueRef expr_val, ZigType *int_type) {
1672 LLVMValueRef zero = LLVMConstNull(int_type->type_ref);
1677 LLVMValueRef zero = LLVMConstNull(get_llvm_type(g, int_type));
16731678 LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, expr_val, zero, "");
16741679 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "CastShortenOk");
16751680 LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn_val, "CastShortenFail");
......@@ -1704,7 +1709,7 @@ static LLVMValueRef gen_widen_or_shorten(CodeGen *g, bool want_runtime_safety, Z
17041709 !wanted_type->data.integral.is_signed && actual_type->data.integral.is_signed &&
17051710 want_runtime_safety)
17061711 {
1707 LLVMValueRef zero = LLVMConstNull(actual_type->type_ref);
1712 LLVMValueRef zero = LLVMConstNull(get_llvm_type(g, actual_type));
17081713 LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntSGE, expr_val, zero, "");
17091714
17101715 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "SignCastOk");
......@@ -1721,19 +1726,19 @@ static LLVMValueRef gen_widen_or_shorten(CodeGen *g, bool want_runtime_safety, Z
17211726 return expr_val;
17221727 } else if (actual_bits < wanted_bits) {
17231728 if (actual_type->id == ZigTypeIdFloat) {
1724 return LLVMBuildFPExt(g->builder, expr_val, wanted_type->type_ref, "");
1729 return LLVMBuildFPExt(g->builder, expr_val, get_llvm_type(g, wanted_type), "");
17251730 } else if (actual_type->id == ZigTypeIdInt) {
17261731 if (actual_type->data.integral.is_signed) {
1727 return LLVMBuildSExt(g->builder, expr_val, wanted_type->type_ref, "");
1732 return LLVMBuildSExt(g->builder, expr_val, get_llvm_type(g, wanted_type), "");
17281733 } else {
1729 return LLVMBuildZExt(g->builder, expr_val, wanted_type->type_ref, "");
1734 return LLVMBuildZExt(g->builder, expr_val, get_llvm_type(g, wanted_type), "");
17301735 }
17311736 } else {
17321737 zig_unreachable();
17331738 }
17341739 } else if (actual_bits > wanted_bits) {
17351740 if (actual_type->id == ZigTypeIdFloat) {
1736 return LLVMBuildFPTrunc(g->builder, expr_val, wanted_type->type_ref, "");
1741 return LLVMBuildFPTrunc(g->builder, expr_val, get_llvm_type(g, wanted_type), "");
17371742 } else if (actual_type->id == ZigTypeIdInt) {
17381743 if (wanted_bits == 0) {
17391744 if (!want_runtime_safety)
......@@ -1741,15 +1746,15 @@ static LLVMValueRef gen_widen_or_shorten(CodeGen *g, bool want_runtime_safety, Z
17411746
17421747 return gen_assert_zero(g, expr_val, actual_type);
17431748 }
1744 LLVMValueRef trunc_val = LLVMBuildTrunc(g->builder, expr_val, wanted_type->type_ref, "");
1749 LLVMValueRef trunc_val = LLVMBuildTrunc(g->builder, expr_val, get_llvm_type(g, wanted_type), "");
17451750 if (!want_runtime_safety) {
17461751 return trunc_val;
17471752 }
17481753 LLVMValueRef orig_val;
17491754 if (wanted_type->data.integral.is_signed) {
1750 orig_val = LLVMBuildSExt(g->builder, trunc_val, actual_type->type_ref, "");
1755 orig_val = LLVMBuildSExt(g->builder, trunc_val, get_llvm_type(g, actual_type), "");
17511756 } else {
1752 orig_val = LLVMBuildZExt(g->builder, trunc_val, actual_type->type_ref, "");
1757 orig_val = LLVMBuildZExt(g->builder, trunc_val, get_llvm_type(g, actual_type), "");
17531758 }
17541759 LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, expr_val, orig_val, "");
17551760 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "CastShortenOk");
......@@ -1793,7 +1798,7 @@ static LLVMValueRef gen_overflow_op(CodeGen *g, ZigType *operand_type, AddSubMul
17931798 LLVMValueRef extended1 = buildExtFn(g->builder, val1, one_more_bit_int_vector, "");
17941799 LLVMValueRef extended2 = buildExtFn(g->builder, val2, one_more_bit_int_vector, "");
17951800 LLVMValueRef extended_result = wrap_op[op](g->builder, extended1, extended2, "");
1796 result = LLVMBuildTrunc(g->builder, extended_result, operand_type->type_ref, "");
1801 result = LLVMBuildTrunc(g->builder, extended_result, get_llvm_type(g, operand_type), "");
17971802
17981803 LLVMValueRef re_extended_result = buildExtFn(g->builder, result, one_more_bit_int_vector, "");
17991804 LLVMValueRef overflow_vector = LLVMBuildICmp(g->builder, LLVMIntNE, extended_result, re_extended_result, "");
......@@ -1880,12 +1885,13 @@ static LLVMValueRef gen_assign_raw(CodeGen *g, LLVMValueRef ptr, ZigType *ptr_ty
18801885 LLVMValueRef dest_ptr = LLVMBuildBitCast(g->builder, ptr, ptr_u8, "");
18811886
18821887 ZigType *usize = g->builtin_types.entry_usize;
1883 uint64_t size_bytes = LLVMStoreSizeOfType(g->target_data_ref, child_type->type_ref);
1888 uint64_t size_bytes = LLVMStoreSizeOfType(g->target_data_ref, get_llvm_type(g, child_type));
18841889 uint64_t align_bytes = get_ptr_align(g, ptr_type);
18851890 assert(size_bytes > 0);
18861891 assert(align_bytes > 0);
18871892
1888 ZigLLVMBuildMemCpy(g->builder, dest_ptr, align_bytes, src_ptr, align_bytes, LLVMConstInt(usize->type_ref, size_bytes, false),
1893 ZigLLVMBuildMemCpy(g->builder, dest_ptr, align_bytes, src_ptr, align_bytes,
1894 LLVMConstInt(usize->llvm_type, size_bytes, false),
18891895 ptr_type->data.pointer.is_volatile);
18901896 return nullptr;
18911897 }
......@@ -1907,7 +1913,7 @@ static LLVMValueRef gen_assign_raw(CodeGen *g, LLVMValueRef ptr, ZigType *ptr_ty
19071913 uint32_t shift_amt = big_endian ? host_bit_count - bit_offset - size_in_bits : bit_offset;
19081914 LLVMValueRef shift_amt_val = LLVMConstInt(LLVMTypeOf(containing_int), shift_amt, false);
19091915
1910 LLVMValueRef mask_val = LLVMConstAllOnes(child_type->type_ref);
1916 LLVMValueRef mask_val = LLVMConstAllOnes(get_llvm_type(g, child_type));
19111917 mask_val = LLVMConstZExt(mask_val, LLVMTypeOf(containing_int));
19121918 mask_val = LLVMConstShl(mask_val, shift_amt_val);
19131919 mask_val = LLVMConstNot(mask_val);
......@@ -1942,9 +1948,10 @@ static LLVMValueRef ir_llvm_value(CodeGen *g, IrInstruction *instruction) {
19421948 if (handle_is_ptr(instruction->value.type)) {
19431949 render_const_val_global(g, &instruction->value, "");
19441950 ZigType *ptr_type = get_pointer_to_type(g, instruction->value.type, true);
1945 instruction->llvm_value = LLVMBuildBitCast(g->builder, instruction->value.global_refs->llvm_global, ptr_type->type_ref, "");
1951 instruction->llvm_value = LLVMBuildBitCast(g->builder, instruction->value.global_refs->llvm_global, get_llvm_type(g, ptr_type), "");
19461952 } else if (instruction->value.type->id == ZigTypeIdPointer) {
1947 instruction->llvm_value = LLVMBuildBitCast(g->builder, instruction->value.global_refs->llvm_value, instruction->value.type->type_ref, "");
1953 instruction->llvm_value = LLVMBuildBitCast(g->builder, instruction->value.global_refs->llvm_value,
1954 get_llvm_type(g, instruction->value.type), "");
19481955 } else {
19491956 instruction->llvm_value = instruction->value.global_refs->llvm_value;
19501957 }
......@@ -1979,7 +1986,7 @@ static void give_up_with_c_abi_error(CodeGen *g, AstNode *source_node) {
19791986}
19801987
19811988static LLVMValueRef build_alloca(CodeGen *g, ZigType *type_entry, const char *name, uint32_t alignment) {
1982 LLVMValueRef result = LLVMBuildAlloca(g->builder, type_entry->type_ref, name);
1989 LLVMValueRef result = LLVMBuildAlloca(g->builder, get_llvm_type(g, type_entry), name);
19831990 LLVMSetAlignment(result, (alignment == 0) ? get_abi_alignment(g, type_entry) : alignment);
19841991 return result;
19851992}
......@@ -2062,8 +2069,8 @@ static bool iter_function_params_c_abi(CodeGen *g, ZigType *fn_type, FnWalk *fn_
20622069 fn_walk->data.call.gen_param_values->append(val);
20632070 break;
20642071 case FnWalkIdTypes:
2065 fn_walk->data.types.gen_param_types->append(ty->type_ref);
2066 fn_walk->data.types.param_di_types->append(ty->di_type);
2072 fn_walk->data.types.gen_param_types->append(get_llvm_type(g, ty));
2073 fn_walk->data.types.param_di_types->append(get_llvm_di_type(g, ty));
20672074 break;
20682075 case FnWalkIdVars: {
20692076 var->value_ref = build_alloca(g, ty, buf_ptr(&var->name), var->align_bytes);
......@@ -2099,8 +2106,8 @@ static bool iter_function_params_c_abi(CodeGen *g, ZigType *fn_type, FnWalk *fn_
20992106 break;
21002107 case FnWalkIdTypes: {
21012108 ZigType *gen_type = get_pointer_to_type(g, ty, true);
2102 fn_walk->data.types.gen_param_types->append(gen_type->type_ref);
2103 fn_walk->data.types.param_di_types->append(gen_type->di_type);
2109 fn_walk->data.types.gen_param_types->append(get_llvm_type(g, gen_type));
2110 fn_walk->data.types.param_di_types->append(get_llvm_di_type(g, gen_type));
21042111 break;
21052112 }
21062113 case FnWalkIdVars: {
......@@ -2138,8 +2145,8 @@ static bool iter_function_params_c_abi(CodeGen *g, ZigType *fn_type, FnWalk *fn_
21382145 break;
21392146 case FnWalkIdTypes: {
21402147 ZigType *gen_type = get_pointer_to_type(g, ty, true);
2141 fn_walk->data.types.gen_param_types->append(gen_type->type_ref);
2142 fn_walk->data.types.param_di_types->append(gen_type->di_type);
2148 fn_walk->data.types.gen_param_types->append(get_llvm_type(g, gen_type));
2149 fn_walk->data.types.param_di_types->append(get_llvm_di_type(g, gen_type));
21432150 break;
21442151 }
21452152 case FnWalkIdVars: {
......@@ -2171,8 +2178,8 @@ static bool iter_function_params_c_abi(CodeGen *g, ZigType *fn_type, FnWalk *fn_
21712178 }
21722179 case FnWalkIdTypes: {
21732180 ZigType *gen_type = get_int_type(g, false, ty_size * 8);
2174 fn_walk->data.types.gen_param_types->append(gen_type->type_ref);
2175 fn_walk->data.types.param_di_types->append(gen_type->di_type);
2181 fn_walk->data.types.gen_param_types->append(get_llvm_type(g, gen_type));
2182 fn_walk->data.types.param_di_types->append(get_llvm_di_type(g, gen_type));
21762183 break;
21772184 }
21782185 case FnWalkIdVars: {
......@@ -2210,7 +2217,7 @@ var_ok:
22102217 var->di_loc_var = ZigLLVMCreateParameterVariable(g->dbuilder, get_di_scope(g, var->parent_scope),
22112218 buf_ptr(&var->name), fn_walk->data.vars.import->data.structure.root_struct->di_file,
22122219 (unsigned)(var->decl_node->line + 1),
2213 dest_ty->di_type, !g->strip_debug_symbols, 0, di_arg_index + 1);
2220 get_llvm_di_type(g, dest_ty), !g->strip_debug_symbols, 0, di_arg_index + 1);
22142221 }
22152222 return true;
22162223}
......@@ -2282,7 +2289,9 @@ void walk_function_params(CodeGen *g, ZigType *fn_type, FnWalk *fn_walk) {
22822289
22832290 if (!handle_is_ptr(variable->var_type)) {
22842291 clear_debug_source_node(g);
2285 gen_store_untyped(g, LLVMGetParam(llvm_fn, (unsigned)variable->gen_arg_index),
2292 ZigType *fn_type = fn_table_entry->type_entry;
2293 unsigned gen_arg_index = fn_type->data.fn.gen_param_info[variable->src_arg_index].gen_index;
2294 gen_store_untyped(g, LLVMGetParam(llvm_fn, gen_arg_index),
22862295 variable->value_ref, variable->align_bytes, false);
22872296 }
22882297
......@@ -2436,7 +2445,7 @@ static LLVMValueRef gen_div(CodeGen *g, bool want_runtime_safety, bool want_fast
24362445{
24372446 ZigLLVMSetFastMath(g->builder, want_fast_math);
24382447
2439 LLVMValueRef zero = LLVMConstNull(type_entry->type_ref);
2448 LLVMValueRef zero = LLVMConstNull(get_llvm_type(g, type_entry));
24402449 if (want_runtime_safety && (want_fast_math || type_entry->id != ZigTypeIdFloat)) {
24412450 LLVMValueRef is_zero_bit;
24422451 if (type_entry->id == ZigTypeIdInt) {
......@@ -2456,10 +2465,10 @@ static LLVMValueRef gen_div(CodeGen *g, bool want_runtime_safety, bool want_fast
24562465 LLVMPositionBuilderAtEnd(g->builder, div_zero_ok_block);
24572466
24582467 if (type_entry->id == ZigTypeIdInt && type_entry->data.integral.is_signed) {
2459 LLVMValueRef neg_1_value = LLVMConstInt(type_entry->type_ref, -1, true);
2468 LLVMValueRef neg_1_value = LLVMConstInt(get_llvm_type(g, type_entry), -1, true);
24602469 BigInt int_min_bi = {0};
24612470 eval_min_max_value_int(g, type_entry, &int_min_bi, false);
2462 LLVMValueRef int_min_value = bigint_to_llvm_const(type_entry->type_ref, &int_min_bi);
2471 LLVMValueRef int_min_value = bigint_to_llvm_const(get_llvm_type(g, type_entry), &int_min_bi);
24632472 LLVMBasicBlockRef overflow_ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "DivOverflowOk");
24642473 LLVMBasicBlockRef overflow_fail_block = LLVMAppendBasicBlock(g->cur_fn_val, "DivOverflowFail");
24652474 LLVMValueRef num_is_int_min = LLVMBuildICmp(g->builder, LLVMIntEQ, val1, int_min_value, "");
......@@ -2513,7 +2522,7 @@ static LLVMValueRef gen_div(CodeGen *g, bool want_runtime_safety, bool want_fast
25132522 LLVMBuildBr(g->builder, end_block);
25142523
25152524 LLVMPositionBuilderAtEnd(g->builder, end_block);
2516 LLVMValueRef phi = LLVMBuildPhi(g->builder, type_entry->type_ref, "");
2525 LLVMValueRef phi = LLVMBuildPhi(g->builder, get_llvm_type(g, type_entry), "");
25172526 LLVMValueRef incoming_values[] = { ceiled, floored };
25182527 LLVMBasicBlockRef incoming_blocks[] = { ceiled_end_block, floored_end_block };
25192528 LLVMAddIncoming(phi, incoming_values, incoming_blocks, 2);
......@@ -2576,7 +2585,7 @@ static LLVMValueRef gen_div(CodeGen *g, bool want_runtime_safety, bool want_fast
25762585 LLVMValueRef orig_num = LLVMBuildNSWMul(g->builder, result, val2, "");
25772586 LLVMValueRef orig_ok = LLVMBuildICmp(g->builder, LLVMIntEQ, orig_num, val1, "");
25782587 LLVMValueRef ok_bit = LLVMBuildOr(g->builder, orig_ok, is_pos, "");
2579 LLVMValueRef one = LLVMConstInt(type_entry->type_ref, 1, true);
2588 LLVMValueRef one = LLVMConstInt(get_llvm_type(g, type_entry), 1, true);
25802589 LLVMValueRef result_minus_1 = LLVMBuildNSWSub(g->builder, result, one, "");
25812590 return LLVMBuildSelect(g->builder, ok_bit, result, result_minus_1, "");
25822591 }
......@@ -2595,7 +2604,7 @@ static LLVMValueRef gen_rem(CodeGen *g, bool want_runtime_safety, bool want_fast
25952604{
25962605 ZigLLVMSetFastMath(g->builder, want_fast_math);
25972606
2598 LLVMValueRef zero = LLVMConstNull(type_entry->type_ref);
2607 LLVMValueRef zero = LLVMConstNull(get_llvm_type(g, type_entry));
25992608 if (want_runtime_safety) {
26002609 LLVMValueRef is_zero_bit;
26012610 if (type_entry->id == ZigTypeIdInt) {
......@@ -2820,7 +2829,7 @@ static void add_error_range_check(CodeGen *g, ZigType *err_set_type, ZigType *in
28202829 assert(err_set_type->id == ZigTypeIdErrorSet);
28212830
28222831 if (type_is_global_error_set(err_set_type)) {
2823 LLVMValueRef zero = LLVMConstNull(int_type->type_ref);
2832 LLVMValueRef zero = LLVMConstNull(get_llvm_type(g, int_type));
28242833 LLVMValueRef neq_zero_bit = LLVMBuildICmp(g->builder, LLVMIntNE, target_val, zero, "");
28252834 LLVMValueRef ok_bit;
28262835
......@@ -2832,7 +2841,7 @@ static void add_error_range_check(CodeGen *g, ZigType *err_set_type, ZigType *in
28322841 {
28332842 ok_bit = neq_zero_bit;
28342843 } else {
2835 LLVMValueRef error_value_count = LLVMConstInt(int_type->type_ref, g->errors_by_index.length, false);
2844 LLVMValueRef error_value_count = LLVMConstInt(get_llvm_type(g, int_type), g->errors_by_index.length, false);
28362845 LLVMValueRef in_bounds_bit = LLVMBuildICmp(g->builder, LLVMIntULT, target_val, error_value_count, "");
28372846 ok_bit = LLVMBuildAnd(g->builder, neq_zero_bit, in_bounds_bit, "");
28382847 }
......@@ -2853,7 +2862,8 @@ static void add_error_range_check(CodeGen *g, ZigType *err_set_type, ZigType *in
28532862 uint32_t err_count = err_set_type->data.error_set.err_count;
28542863 LLVMValueRef switch_instr = LLVMBuildSwitch(g->builder, target_val, fail_block, err_count);
28552864 for (uint32_t i = 0; i < err_count; i += 1) {
2856 LLVMValueRef case_value = LLVMConstInt(g->err_tag_type->type_ref, err_set_type->data.error_set.errors[i]->value, false);
2865 LLVMValueRef case_value = LLVMConstInt(get_llvm_type(g, g->err_tag_type),
2866 err_set_type->data.error_set.errors[i]->value, false);
28572867 LLVMAddCase(switch_instr, case_value, ok_block);
28582868 }
28592869
......@@ -2892,7 +2902,7 @@ static LLVMValueRef ir_render_resize_slice(CodeGen *g, IrExecutable *executable,
28922902 LLVMValueRef src_ptr_ptr = LLVMBuildStructGEP(g->builder, expr_val, (unsigned)actual_ptr_index, "");
28932903 LLVMValueRef src_ptr = gen_load_untyped(g, src_ptr_ptr, 0, false, "");
28942904 LLVMValueRef src_ptr_casted = LLVMBuildBitCast(g->builder, src_ptr,
2895 wanted_type->data.structure.fields[0].type_entry->type_ref, "");
2905 get_llvm_type(g, wanted_type->data.structure.fields[0].type_entry), "");
28962906 LLVMValueRef dest_ptr_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr,
28972907 (unsigned)wanted_ptr_index, "");
28982908 gen_store_untyped(g, src_ptr_casted, dest_ptr_ptr, 0, false);
......@@ -2904,13 +2914,13 @@ static LLVMValueRef ir_render_resize_slice(CodeGen *g, IrExecutable *executable,
29042914
29052915 LLVMValueRef new_len;
29062916 if (dest_size == 1) {
2907 LLVMValueRef src_size_val = LLVMConstInt(g->builtin_types.entry_usize->type_ref, src_size, false);
2917 LLVMValueRef src_size_val = LLVMConstInt(g->builtin_types.entry_usize->llvm_type, src_size, false);
29082918 new_len = LLVMBuildMul(g->builder, src_len, src_size_val, "");
29092919 } else if (src_size == 1) {
2910 LLVMValueRef dest_size_val = LLVMConstInt(g->builtin_types.entry_usize->type_ref, dest_size, false);
2920 LLVMValueRef dest_size_val = LLVMConstInt(g->builtin_types.entry_usize->llvm_type, dest_size, false);
29112921 if (ir_want_runtime_safety(g, &instruction->base)) {
29122922 LLVMValueRef remainder_val = LLVMBuildURem(g->builder, src_len, dest_size_val, "");
2913 LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_usize->type_ref);
2923 LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_usize->llvm_type);
29142924 LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, remainder_val, zero, "");
29152925 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "SliceWidenOk");
29162926 LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn_val, "SliceWidenFail");
......@@ -2951,9 +2961,9 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,
29512961 case CastOpIntToFloat:
29522962 assert(actual_type->id == ZigTypeIdInt);
29532963 if (actual_type->data.integral.is_signed) {
2954 return LLVMBuildSIToFP(g->builder, expr_val, wanted_type->type_ref, "");
2964 return LLVMBuildSIToFP(g->builder, expr_val, get_llvm_type(g, wanted_type), "");
29552965 } else {
2956 return LLVMBuildUIToFP(g->builder, expr_val, wanted_type->type_ref, "");
2966 return LLVMBuildUIToFP(g->builder, expr_val, get_llvm_type(g, wanted_type), "");
29572967 }
29582968 case CastOpFloatToInt: {
29592969 assert(wanted_type->id == ZigTypeIdInt);
......@@ -2963,9 +2973,9 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,
29632973
29642974 LLVMValueRef result;
29652975 if (wanted_type->data.integral.is_signed) {
2966 result = LLVMBuildFPToSI(g->builder, expr_val, wanted_type->type_ref, "");
2976 result = LLVMBuildFPToSI(g->builder, expr_val, get_llvm_type(g, wanted_type), "");
29672977 } else {
2968 result = LLVMBuildFPToUI(g->builder, expr_val, wanted_type->type_ref, "");
2978 result = LLVMBuildFPToUI(g->builder, expr_val, get_llvm_type(g, wanted_type), "");
29692979 }
29702980
29712981 if (want_safety) {
......@@ -2993,14 +3003,14 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,
29933003 case CastOpBoolToInt:
29943004 assert(wanted_type->id == ZigTypeIdInt);
29953005 assert(actual_type->id == ZigTypeIdBool);
2996 return LLVMBuildZExt(g->builder, expr_val, wanted_type->type_ref, "");
3006 return LLVMBuildZExt(g->builder, expr_val, get_llvm_type(g, wanted_type), "");
29973007 case CastOpErrSet:
29983008 if (ir_want_runtime_safety(g, &cast_instruction->base)) {
29993009 add_error_range_check(g, wanted_type, g->err_tag_type, expr_val);
30003010 }
30013011 return expr_val;
30023012 case CastOpBitCast:
3003 return LLVMBuildBitCast(g->builder, expr_val, wanted_type->type_ref, "");
3013 return LLVMBuildBitCast(g->builder, expr_val, get_llvm_type(g, wanted_type), "");
30043014 case CastOpPtrOfArrayToSlice: {
30053015 assert(cast_instruction->tmp_ptr);
30063016 assert(actual_type->id == ZigTypeIdPointer);
......@@ -3010,15 +3020,15 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,
30103020 LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, cast_instruction->tmp_ptr,
30113021 slice_ptr_index, "");
30123022 LLVMValueRef indices[] = {
3013 LLVMConstNull(g->builtin_types.entry_usize->type_ref),
3014 LLVMConstInt(g->builtin_types.entry_usize->type_ref, 0, false),
3023 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),
3024 LLVMConstInt(g->builtin_types.entry_usize->llvm_type, 0, false),
30153025 };
30163026 LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP(g->builder, expr_val, indices, 2, "");
30173027 gen_store_untyped(g, slice_start_ptr, ptr_field_ptr, 0, false);
30183028
30193029 LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, cast_instruction->tmp_ptr,
30203030 slice_len_index, "");
3021 LLVMValueRef len_value = LLVMConstInt(g->builtin_types.entry_usize->type_ref,
3031 LLVMValueRef len_value = LLVMConstInt(g->builtin_types.entry_usize->llvm_type,
30223032 array_type->data.array.len, false);
30233033 gen_store_untyped(g, len_value, len_field_ptr, 0, false);
30243034
......@@ -3036,7 +3046,7 @@ static LLVMValueRef ir_render_ptr_cast(CodeGen *g, IrExecutable *executable,
30363046 return nullptr;
30373047 }
30383048 LLVMValueRef ptr = ir_llvm_value(g, instruction->ptr);
3039 LLVMValueRef result_ptr = LLVMBuildBitCast(g->builder, ptr, wanted_type->type_ref, "");
3049 LLVMValueRef result_ptr = LLVMBuildBitCast(g->builder, ptr, get_llvm_type(g, wanted_type), "");
30403050 bool want_safety_check = instruction->safety_check_on && ir_want_runtime_safety(g, &instruction->base);
30413051 if (!want_safety_check || ptr_allows_addr_zero(wanted_type))
30423052 return result_ptr;
......@@ -3066,16 +3076,16 @@ static LLVMValueRef ir_render_bit_cast(CodeGen *g, IrExecutable *executable,
30663076 if (wanted_is_ptr == actual_is_ptr) {
30673077 // We either bitcast the value directly or bitcast the pointer which does a pointer cast
30683078 LLVMTypeRef wanted_type_ref = wanted_is_ptr ?
3069 LLVMPointerType(wanted_type->type_ref, 0) : wanted_type->type_ref;
3079 LLVMPointerType(get_llvm_type(g, wanted_type), 0) : get_llvm_type(g, wanted_type);
30703080 return LLVMBuildBitCast(g->builder, value, wanted_type_ref, "");
30713081 } else if (actual_is_ptr) {
3072 LLVMTypeRef wanted_ptr_type_ref = LLVMPointerType(wanted_type->type_ref, 0);
3082 LLVMTypeRef wanted_ptr_type_ref = LLVMPointerType(get_llvm_type(g, wanted_type), 0);
30733083 LLVMValueRef bitcasted_ptr = LLVMBuildBitCast(g->builder, value, wanted_ptr_type_ref, "");
30743084 uint32_t alignment = get_abi_alignment(g, actual_type);
30753085 return gen_load_untyped(g, bitcasted_ptr, alignment, false, "");
30763086 } else {
30773087 assert(instruction->tmp_ptr != nullptr);
3078 LLVMTypeRef wanted_ptr_type_ref = LLVMPointerType(actual_type->type_ref, 0);
3088 LLVMTypeRef wanted_ptr_type_ref = LLVMPointerType(get_llvm_type(g, actual_type), 0);
30793089 LLVMValueRef bitcasted_ptr = LLVMBuildBitCast(g->builder, instruction->tmp_ptr, wanted_ptr_type_ref, "");
30803090 uint32_t alignment = get_abi_alignment(g, wanted_type);
30813091 gen_store_untyped(g, value, bitcasted_ptr, alignment, false);
......@@ -3115,13 +3125,13 @@ static LLVMValueRef ir_render_int_to_ptr(CodeGen *g, IrExecutable *executable, I
31153125
31163126 LLVMPositionBuilderAtEnd(g->builder, ok_block);
31173127 }
3118 return LLVMBuildIntToPtr(g->builder, target_val, wanted_type->type_ref, "");
3128 return LLVMBuildIntToPtr(g->builder, target_val, get_llvm_type(g, wanted_type), "");
31193129}
31203130
31213131static LLVMValueRef ir_render_ptr_to_int(CodeGen *g, IrExecutable *executable, IrInstructionPtrToInt *instruction) {
31223132 ZigType *wanted_type = instruction->base.value.type;
31233133 LLVMValueRef target_val = ir_llvm_value(g, instruction->target);
3124 return LLVMBuildPtrToInt(g->builder, target_val, wanted_type->type_ref, "");
3134 return LLVMBuildPtrToInt(g->builder, target_val, get_llvm_type(g, wanted_type), "");
31253135}
31263136
31273137static LLVMValueRef ir_render_int_to_enum(CodeGen *g, IrExecutable *executable, IrInstructionIntToEnum *instruction) {
......@@ -3139,7 +3149,7 @@ static LLVMValueRef ir_render_int_to_enum(CodeGen *g, IrExecutable *executable,
31393149 size_t field_count = wanted_type->data.enumeration.src_field_count;
31403150 LLVMValueRef switch_instr = LLVMBuildSwitch(g->builder, tag_int_value, bad_value_block, field_count);
31413151 for (size_t field_i = 0; field_i < field_count; field_i += 1) {
3142 LLVMValueRef this_tag_int_value = bigint_to_llvm_const(tag_int_type->type_ref,
3152 LLVMValueRef this_tag_int_value = bigint_to_llvm_const(get_llvm_type(g, tag_int_type),
31433153 &wanted_type->data.enumeration.fields[field_i].value);
31443154 LLVMAddCase(switch_instr, this_tag_int_value, ok_value_block);
31453155 }
......@@ -3321,7 +3331,7 @@ static LLVMValueRef ir_render_load_ptr(CodeGen *g, IrExecutable *executable, IrI
33213331 LLVMValueRef shifted_value = LLVMBuildLShr(g->builder, containing_int, shift_amt_val, "");
33223332
33233333 if (!handle_is_ptr(child_type))
3324 return LLVMBuildTrunc(g->builder, shifted_value, child_type->type_ref, "");
3334 return LLVMBuildTrunc(g->builder, shifted_value, get_llvm_type(g, child_type), "");
33253335
33263336 assert(instruction->tmp_ptr != nullptr);
33273337 LLVMTypeRef same_size_int = LLVMIntType(size_in_bits);
......@@ -3378,7 +3388,7 @@ static LLVMValueRef gen_valgrind_client_request(CodeGen *g, LLVMValueRef default
33783388 if (!target_has_valgrind_support(g->zig_target)) {
33793389 return default_value;
33803390 }
3381 LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->type_ref;
3391 LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->llvm_type;
33823392 bool asm_has_side_effects = true;
33833393 bool asm_is_alignstack = false;
33843394 if (g->zig_target->arch == ZigLLVM_x86_64) {
......@@ -3445,7 +3455,7 @@ static bool want_valgrind_support(CodeGen *g) {
34453455
34463456static void gen_undef_init(CodeGen *g, uint32_t ptr_align_bytes, ZigType *value_type, LLVMValueRef ptr) {
34473457 assert(type_has_bits(value_type));
3448 uint64_t size_bytes = LLVMStoreSizeOfType(g->target_data_ref, value_type->type_ref);
3458 uint64_t size_bytes = LLVMStoreSizeOfType(g->target_data_ref, get_llvm_type(g, value_type));
34493459 assert(size_bytes > 0);
34503460 assert(ptr_align_bytes > 0);
34513461 // memset uninitialized memory to 0xaa
......@@ -3453,14 +3463,14 @@ static void gen_undef_init(CodeGen *g, uint32_t ptr_align_bytes, ZigType *value_
34533463 LLVMValueRef fill_char = LLVMConstInt(LLVMInt8Type(), 0xaa, false);
34543464 LLVMValueRef dest_ptr = LLVMBuildBitCast(g->builder, ptr, ptr_u8, "");
34553465 ZigType *usize = g->builtin_types.entry_usize;
3456 LLVMValueRef byte_count = LLVMConstInt(usize->type_ref, size_bytes, false);
3466 LLVMValueRef byte_count = LLVMConstInt(usize->llvm_type, size_bytes, false);
34573467 ZigLLVMBuildMemSet(g->builder, dest_ptr, fill_char, byte_count, ptr_align_bytes, false);
34583468 // then tell valgrind that the memory is undefined even though we just memset it
34593469 if (want_valgrind_support(g)) {
34603470 static const uint32_t VG_USERREQ__MAKE_MEM_UNDEFINED = 1296236545;
3461 LLVMValueRef zero = LLVMConstInt(usize->type_ref, 0, false);
3462 LLVMValueRef req = LLVMConstInt(usize->type_ref, VG_USERREQ__MAKE_MEM_UNDEFINED, false);
3463 LLVMValueRef ptr_as_usize = LLVMBuildPtrToInt(g->builder, dest_ptr, usize->type_ref, "");
3471 LLVMValueRef zero = LLVMConstInt(usize->llvm_type, 0, false);
3472 LLVMValueRef req = LLVMConstInt(usize->llvm_type, VG_USERREQ__MAKE_MEM_UNDEFINED, false);
3473 LLVMValueRef ptr_as_usize = LLVMBuildPtrToInt(g->builder, dest_ptr, usize->llvm_type, "");
34643474 gen_valgrind_client_request(g, zero, req, ptr_as_usize, byte_count, zero, zero, zero);
34653475 }
34663476}
......@@ -3515,7 +3525,7 @@ static LLVMValueRef ir_render_elem_ptr(CodeGen *g, IrExecutable *executable, IrI
35153525 array_type = array_type->data.pointer.child_type;
35163526 }
35173527 if (safety_check_on) {
3518 LLVMValueRef end = LLVMConstInt(g->builtin_types.entry_usize->type_ref,
3528 LLVMValueRef end = LLVMConstInt(g->builtin_types.entry_usize->llvm_type,
35193529 array_type->data.array.len, false);
35203530 add_bounds_check(g, subscript_value, LLVMIntEQ, nullptr, LLVMIntULT, end);
35213531 }
......@@ -3533,18 +3543,18 @@ static LLVMValueRef ir_render_elem_ptr(CodeGen *g, IrExecutable *executable, IrI
35333543 LLVMTypeRef ptr_u8_type_ref = LLVMPointerType(LLVMInt8Type(), 0);
35343544 LLVMValueRef u8_array_ptr = LLVMBuildBitCast(g->builder, array_ptr, ptr_u8_type_ref, "");
35353545 assert(size_in_bits % 8 == 0);
3536 LLVMValueRef elem_size_bytes = LLVMConstInt(g->builtin_types.entry_usize->type_ref,
3546 LLVMValueRef elem_size_bytes = LLVMConstInt(g->builtin_types.entry_usize->llvm_type,
35373547 size_in_bits / 8, false);
35383548 LLVMValueRef byte_offset = LLVMBuildNUWMul(g->builder, subscript_value, elem_size_bytes, "");
35393549 LLVMValueRef indices[] = {
35403550 byte_offset
35413551 };
35423552 LLVMValueRef elem_byte_ptr = LLVMBuildInBoundsGEP(g->builder, u8_array_ptr, indices, 1, "");
3543 return LLVMBuildBitCast(g->builder, elem_byte_ptr, LLVMPointerType(child_type->type_ref, 0), "");
3553 return LLVMBuildBitCast(g->builder, elem_byte_ptr, LLVMPointerType(get_llvm_type(g, child_type), 0), "");
35443554 }
35453555 }
35463556 LLVMValueRef indices[] = {
3547 LLVMConstNull(g->builtin_types.entry_usize->type_ref),
3557 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),
35483558 subscript_value
35493559 };
35503560 return LLVMBuildInBoundsGEP(g->builder, array_ptr, indices, 2, "");
......@@ -3556,7 +3566,9 @@ static LLVMValueRef ir_render_elem_ptr(CodeGen *g, IrExecutable *executable, IrI
35563566 return LLVMBuildInBoundsGEP(g->builder, array_ptr, indices, 1, "");
35573567 } else if (array_type->id == ZigTypeIdStruct) {
35583568 assert(array_type->data.structure.is_slice);
3559 if (!type_has_bits(instruction->base.value.type)) {
3569
3570 ZigType *ptr_type = instruction->base.value.type;
3571 if (!type_has_bits(ptr_type)) {
35603572 if (safety_check_on) {
35613573 assert(LLVMGetTypeKind(LLVMTypeOf(array_ptr)) == LLVMIntegerTypeKind);
35623574 add_bounds_check(g, subscript_value, LLVMIntEQ, nullptr, LLVMIntULT, array_ptr);
......@@ -3773,7 +3785,7 @@ static LLVMValueRef ir_render_union_field_ptr(CodeGen *g, IrExecutable *executab
37733785 return nullptr;
37743786
37753787 LLVMValueRef union_ptr = ir_llvm_value(g, instruction->union_ptr);
3776 LLVMTypeRef field_type_ref = LLVMPointerType(field->type_entry->type_ref, 0);
3788 LLVMTypeRef field_type_ref = LLVMPointerType(get_llvm_type(g, field->type_entry), 0);
37773789
37783790 if (union_type->data.unionation.gen_tag_index == SIZE_MAX) {
37793791 LLVMValueRef union_field_ptr = LLVMBuildStructGEP(g->builder, union_ptr, 0, "");
......@@ -3786,7 +3798,7 @@ static LLVMValueRef ir_render_union_field_ptr(CodeGen *g, IrExecutable *executab
37863798 LLVMValueRef tag_value = gen_load_untyped(g, tag_field_ptr, 0, false, "");
37873799
37883800
3789 LLVMValueRef expected_tag_value = bigint_to_llvm_const(union_type->data.unionation.tag_type->type_ref,
3801 LLVMValueRef expected_tag_value = bigint_to_llvm_const(get_llvm_type(g, union_type->data.unionation.tag_type),
37903802 &field->enum_field->value);
37913803 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "UnionCheckOk");
37923804 LLVMBasicBlockRef bad_block = LLVMAppendBasicBlock(g->cur_fn_val, "UnionCheckFail");
......@@ -3916,7 +3928,7 @@ static LLVMValueRef ir_render_asm(CodeGen *g, IrExecutable *executable, IrInstru
39163928 }
39173929
39183930 ZigType *const type = ir_input->value.type;
3919 LLVMTypeRef type_ref = type->type_ref;
3931 LLVMTypeRef type_ref = get_llvm_type(g, type);
39203932 LLVMValueRef value_ref = ir_llvm_value(g, ir_input);
39213933 // Handle integers of non pot bitsize by widening them.
39223934 if (type->id == ZigTypeIdInt) {
......@@ -3925,7 +3937,7 @@ static LLVMValueRef ir_render_asm(CodeGen *g, IrExecutable *executable, IrInstru
39253937 const bool is_signed = type->data.integral.is_signed;
39263938 const size_t wider_bitsize = bitsize < 8 ? 8 : round_to_next_power_of_2(bitsize);
39273939 ZigType *const wider_type = get_int_type(g, is_signed, wider_bitsize);
3928 type_ref = wider_type->type_ref;
3940 type_ref = get_llvm_type(g, wider_type);
39293941 value_ref = gen_widen_or_shorten(g, false, type, wider_type, value_ref);
39303942 }
39313943 }
......@@ -3945,7 +3957,7 @@ static LLVMValueRef ir_render_asm(CodeGen *g, IrExecutable *executable, IrInstru
39453957 if (instruction->return_count == 0) {
39463958 ret_type = LLVMVoidType();
39473959 } else {
3948 ret_type = instruction->base.value.type->type_ref;
3960 ret_type = get_llvm_type(g, instruction->base.value.type);
39493961 }
39503962 LLVMTypeRef function_type = LLVMFunctionType(ret_type, param_types, (unsigned)input_and_output_count, false);
39513963
......@@ -3964,7 +3976,7 @@ static LLVMValueRef gen_non_null_bit(CodeGen *g, ZigType *maybe_type, LLVMValueR
39643976 } else {
39653977 bool is_scalar = !handle_is_ptr(maybe_type);
39663978 if (is_scalar) {
3967 return LLVMBuildICmp(g->builder, LLVMIntNE, maybe_handle, LLVMConstNull(maybe_type->type_ref), "");
3979 return LLVMBuildICmp(g->builder, LLVMIntNE, maybe_handle, LLVMConstNull(get_llvm_type(g, maybe_type)), "");
39683980 } else {
39693981 LLVMValueRef maybe_field_ptr = LLVMBuildStructGEP(g->builder, maybe_handle, maybe_null_index, "");
39703982 return gen_load_untyped(g, maybe_field_ptr, 0, false, "");
......@@ -3999,7 +4011,7 @@ static LLVMValueRef ir_render_optional_unwrap_ptr(CodeGen *g, IrExecutable *exec
39994011
40004012 LLVMPositionBuilderAtEnd(g->builder, ok_block);
40014013 }
4002 if (child_type->zero_bits) {
4014 if (!type_has_bits(child_type)) {
40034015 return nullptr;
40044016 } else {
40054017 bool is_scalar = !handle_is_ptr(maybe_type);
......@@ -4052,10 +4064,10 @@ static LLVMValueRef get_int_builtin_fn(CodeGen *g, ZigType *int_type, BuiltinFnI
40524064 char llvm_name[64];
40534065 sprintf(llvm_name, "llvm.%s.i%" PRIu32, fn_name, int_type->data.integral.bit_count);
40544066 LLVMTypeRef param_types[] = {
4055 int_type->type_ref,
4067 get_llvm_type(g, int_type),
40564068 LLVMInt1Type(),
40574069 };
4058 LLVMTypeRef fn_type = LLVMFunctionType(int_type->type_ref, param_types, n_args, false);
4070 LLVMTypeRef fn_type = LLVMFunctionType(get_llvm_type(g, int_type), param_types, n_args, false);
40594071 LLVMValueRef fn_val = LLVMAddFunction(g->module, llvm_name, fn_type);
40604072 assert(LLVMGetIntrinsicID(fn_val));
40614073
......@@ -4114,9 +4126,9 @@ static LLVMValueRef ir_render_phi(CodeGen *g, IrExecutable *executable, IrInstru
41144126
41154127 LLVMTypeRef phi_type;
41164128 if (handle_is_ptr(instruction->base.value.type)) {
4117 phi_type = LLVMPointerType(instruction->base.value.type->type_ref, 0);
4129 phi_type = LLVMPointerType(get_llvm_type(g,instruction->base.value.type), 0);
41184130 } else {
4119 phi_type = instruction->base.value.type->type_ref;
4131 phi_type = get_llvm_type(g, instruction->base.value.type);
41204132 }
41214133
41224134 LLVMValueRef phi = LLVMBuildPhi(g->builder, phi_type, "");
......@@ -4160,7 +4172,7 @@ static LLVMValueRef ir_render_err_name(CodeGen *g, IrExecutable *executable, IrI
41604172 }
41614173
41624174 LLVMValueRef indices[] = {
4163 LLVMConstNull(g->builtin_types.entry_usize->type_ref),
4175 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),
41644176 err_val,
41654177 };
41664178 return LLVMBuildInBoundsGEP(g->builder, g->err_name_table, indices, 2, "");
......@@ -4176,8 +4188,9 @@ static LLVMValueRef get_enum_tag_name_function(CodeGen *g, ZigType *enum_type) {
41764188 ZigType *u8_slice_type = get_slice_type(g, u8_ptr_type);
41774189 ZigType *tag_int_type = enum_type->data.enumeration.tag_int_type;
41784190
4179 LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMPointerType(u8_slice_type->type_ref, 0),
4180 &tag_int_type->type_ref, 1, false);
4191 LLVMTypeRef tag_int_llvm_type = get_llvm_type(g, tag_int_type);
4192 LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMPointerType(get_llvm_type(g, u8_slice_type), 0),
4193 &tag_int_llvm_type, 1, false);
41814194
41824195 Buf *fn_name = get_mangled_name(g, buf_sprintf("__zig_tag_name_%s", buf_ptr(&enum_type->name)), false);
41834196 LLVMValueRef fn_val = LLVMAddFunction(g->module, buf_ptr(fn_name), fn_type_ref);
......@@ -4209,8 +4222,8 @@ static LLVMValueRef get_enum_tag_name_function(CodeGen *g, ZigType *enum_type) {
42094222
42104223 ZigType *usize = g->builtin_types.entry_usize;
42114224 LLVMValueRef array_ptr_indices[] = {
4212 LLVMConstNull(usize->type_ref),
4213 LLVMConstNull(usize->type_ref),
4225 LLVMConstNull(usize->llvm_type),
4226 LLVMConstNull(usize->llvm_type),
42144227 };
42154228
42164229 for (size_t field_i = 0; field_i < field_count; field_i += 1) {
......@@ -4225,9 +4238,9 @@ static LLVMValueRef get_enum_tag_name_function(CodeGen *g, ZigType *enum_type) {
42254238
42264239 LLVMValueRef fields[] = {
42274240 LLVMConstGEP(str_global, array_ptr_indices, 2),
4228 LLVMConstInt(g->builtin_types.entry_usize->type_ref, buf_len(name), false),
4241 LLVMConstInt(g->builtin_types.entry_usize->llvm_type, buf_len(name), false),
42294242 };
4230 LLVMValueRef slice_init_value = LLVMConstNamedStruct(u8_slice_type->type_ref, fields, 2);
4243 LLVMValueRef slice_init_value = LLVMConstNamedStruct(get_llvm_type(g, u8_slice_type), fields, 2);
42314244
42324245 LLVMValueRef slice_global = LLVMAddGlobal(g->module, LLVMTypeOf(slice_init_value), "");
42334246 LLVMSetInitializer(slice_global, slice_init_value);
......@@ -4237,7 +4250,7 @@ static LLVMValueRef get_enum_tag_name_function(CodeGen *g, ZigType *enum_type) {
42374250 LLVMSetAlignment(slice_global, LLVMABIAlignmentOfType(g->target_data_ref, LLVMTypeOf(slice_init_value)));
42384251
42394252 LLVMBasicBlockRef return_block = LLVMAppendBasicBlock(g->cur_fn_val, "Name");
4240 LLVMValueRef this_tag_int_value = bigint_to_llvm_const(tag_int_type->type_ref,
4253 LLVMValueRef this_tag_int_value = bigint_to_llvm_const(get_llvm_type(g, tag_int_type),
42414254 &enum_type->data.enumeration.fields[field_i].value);
42424255 LLVMAddCase(switch_instr, this_tag_int_value, return_block);
42434256
......@@ -4283,22 +4296,21 @@ static LLVMValueRef ir_render_field_parent_ptr(CodeGen *g, IrExecutable *executa
42834296 ZigType *container_type = container_ptr_type->data.pointer.child_type;
42844297
42854298 size_t byte_offset = LLVMOffsetOfElement(g->target_data_ref,
4286 container_type->type_ref, instruction->field->gen_index);
4299 get_llvm_type(g, container_type), instruction->field->gen_index);
42874300
42884301 LLVMValueRef field_ptr_val = ir_llvm_value(g, instruction->field_ptr);
42894302
42904303 if (byte_offset == 0) {
4291 return LLVMBuildBitCast(g->builder, field_ptr_val, container_ptr_type->type_ref, "");
4304 return LLVMBuildBitCast(g->builder, field_ptr_val, get_llvm_type(g, container_ptr_type), "");
42924305 } else {
42934306 ZigType *usize = g->builtin_types.entry_usize;
42944307
4295 LLVMValueRef field_ptr_int = LLVMBuildPtrToInt(g->builder, field_ptr_val,
4296 usize->type_ref, "");
4308 LLVMValueRef field_ptr_int = LLVMBuildPtrToInt(g->builder, field_ptr_val, usize->llvm_type, "");
42974309
42984310 LLVMValueRef base_ptr_int = LLVMBuildNUWSub(g->builder, field_ptr_int,
4299 LLVMConstInt(usize->type_ref, byte_offset, false), "");
4311 LLVMConstInt(usize->llvm_type, byte_offset, false), "");
43004312
4301 return LLVMBuildIntToPtr(g->builder, base_ptr_int, container_ptr_type->type_ref, "");
4313 return LLVMBuildIntToPtr(g->builder, base_ptr_int, get_llvm_type(g, container_ptr_type), "");
43024314 }
43034315}
43044316
......@@ -4349,10 +4361,10 @@ static LLVMValueRef ir_render_align_cast(CodeGen *g, IrExecutable *executable, I
43494361 assert(align_bytes != 1);
43504362
43514363 ZigType *usize = g->builtin_types.entry_usize;
4352 LLVMValueRef ptr_as_int_val = LLVMBuildPtrToInt(g->builder, ptr_val, usize->type_ref, "");
4353 LLVMValueRef alignment_minus_1 = LLVMConstInt(usize->type_ref, align_bytes - 1, false);
4364 LLVMValueRef ptr_as_int_val = LLVMBuildPtrToInt(g->builder, ptr_val, usize->llvm_type, "");
4365 LLVMValueRef alignment_minus_1 = LLVMConstInt(usize->llvm_type, align_bytes - 1, false);
43544366 LLVMValueRef anded_val = LLVMBuildAnd(g->builder, ptr_as_int_val, alignment_minus_1, "");
4355 LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, anded_val, LLVMConstNull(usize->type_ref), "");
4367 LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, anded_val, LLVMConstNull(usize->llvm_type), "");
43564368
43574369 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "AlignCastOk");
43584370 LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn_val, "AlignCastFail");
......@@ -4373,7 +4385,7 @@ static LLVMValueRef ir_render_error_return_trace(CodeGen *g, IrExecutable *execu
43734385 LLVMValueRef cur_err_ret_trace_val = get_cur_err_ret_trace_val(g, instruction->base.scope);
43744386 if (cur_err_ret_trace_val == nullptr) {
43754387 ZigType *ptr_to_stack_trace_type = get_ptr_to_stack_trace_type(g);
4376 return LLVMConstNull(ptr_to_stack_trace_type->type_ref);
4388 return LLVMConstNull(get_llvm_type(g, ptr_to_stack_trace_type));
43774389 }
43784390 return cur_err_ret_trace_val;
43794391}
......@@ -4439,7 +4451,7 @@ static LLVMValueRef ir_render_cmpxchg(CodeGen *g, IrExecutable *executable, IrIn
44394451 if (!handle_is_ptr(maybe_type)) {
44404452 LLVMValueRef payload_val = LLVMBuildExtractValue(g->builder, result_val, 0, "");
44414453 LLVMValueRef success_bit = LLVMBuildExtractValue(g->builder, result_val, 1, "");
4442 return LLVMBuildSelect(g->builder, success_bit, LLVMConstNull(child_type->type_ref), payload_val, "");
4454 return LLVMBuildSelect(g->builder, success_bit, LLVMConstNull(get_llvm_type(g, child_type)), payload_val, "");
44434455 }
44444456
44454457 assert(instruction->tmp_ptr != nullptr);
......@@ -4470,10 +4482,10 @@ static LLVMValueRef ir_render_truncate(CodeGen *g, IrExecutable *executable, IrI
44704482 // no-op
44714483 return target_val;
44724484 } if (src_type->data.integral.bit_count == dest_type->data.integral.bit_count) {
4473 return LLVMBuildBitCast(g->builder, target_val, dest_type->type_ref, "");
4485 return LLVMBuildBitCast(g->builder, target_val, get_llvm_type(g, dest_type), "");
44744486 } else {
44754487 LLVMValueRef target_val = ir_llvm_value(g, instruction->target);
4476 return LLVMBuildTrunc(g->builder, target_val, dest_type->type_ref, "");
4488 return LLVMBuildTrunc(g->builder, target_val, get_llvm_type(g, dest_type), "");
44774489 }
44784490}
44794491
......@@ -4539,12 +4551,12 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutable *executable, IrInst
45394551 if (instruction->end) {
45404552 end_val = ir_llvm_value(g, instruction->end);
45414553 } else {
4542 end_val = LLVMConstInt(g->builtin_types.entry_usize->type_ref, array_type->data.array.len, false);
4554 end_val = LLVMConstInt(g->builtin_types.entry_usize->llvm_type, array_type->data.array.len, false);
45434555 }
45444556 if (want_runtime_safety) {
45454557 add_bounds_check(g, start_val, LLVMIntEQ, nullptr, LLVMIntULE, end_val);
45464558 if (instruction->end) {
4547 LLVMValueRef array_end = LLVMConstInt(g->builtin_types.entry_usize->type_ref,
4559 LLVMValueRef array_end = LLVMConstInt(g->builtin_types.entry_usize->llvm_type,
45484560 array_type->data.array.len, false);
45494561 add_bounds_check(g, end_val, LLVMIntEQ, nullptr, LLVMIntULE, array_end);
45504562 }
......@@ -4561,7 +4573,7 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutable *executable, IrInst
45614573
45624574 LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, slice_ptr_index, "");
45634575 LLVMValueRef indices[] = {
4564 LLVMConstNull(g->builtin_types.entry_usize->type_ref),
4576 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),
45654577 start_val,
45664578 };
45674579 LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP(g->builder, array_ptr, indices, 2, "");
......@@ -4663,9 +4675,9 @@ static LLVMValueRef ir_render_breakpoint(CodeGen *g, IrExecutable *executable, I
46634675static LLVMValueRef ir_render_return_address(CodeGen *g, IrExecutable *executable,
46644676 IrInstructionReturnAddress *instruction)
46654677{
4666 LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_i32->type_ref);
4678 LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_i32->llvm_type);
46674679 LLVMValueRef ptr_val = LLVMBuildCall(g->builder, get_return_address_fn_val(g), &zero, 1, "");
4668 return LLVMBuildPtrToInt(g->builder, ptr_val, g->builtin_types.entry_usize->type_ref, "");
4680 return LLVMBuildPtrToInt(g->builder, ptr_val, g->builtin_types.entry_usize->llvm_type, "");
46694681}
46704682
46714683static LLVMValueRef get_frame_address_fn_val(CodeGen *g) {
......@@ -4674,8 +4686,8 @@ static LLVMValueRef get_frame_address_fn_val(CodeGen *g) {
46744686
46754687 ZigType *return_type = get_pointer_to_type(g, g->builtin_types.entry_u8, true);
46764688
4677 LLVMTypeRef fn_type = LLVMFunctionType(return_type->type_ref,
4678 &g->builtin_types.entry_i32->type_ref, 1, false);
4689 LLVMTypeRef fn_type = LLVMFunctionType(get_llvm_type(g, return_type),
4690 &g->builtin_types.entry_i32->llvm_type, 1, false);
46794691 g->frame_address_fn_val = LLVMAddFunction(g->module, "llvm.frameaddress", fn_type);
46804692 assert(LLVMGetIntrinsicID(g->frame_address_fn_val));
46814693
......@@ -4685,9 +4697,9 @@ static LLVMValueRef get_frame_address_fn_val(CodeGen *g) {
46854697static LLVMValueRef ir_render_frame_address(CodeGen *g, IrExecutable *executable,
46864698 IrInstructionFrameAddress *instruction)
46874699{
4688 LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_i32->type_ref);
4700 LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_i32->llvm_type);
46894701 LLVMValueRef ptr_val = LLVMBuildCall(g->builder, get_frame_address_fn_val(g), &zero, 1, "");
4690 return LLVMBuildPtrToInt(g->builder, ptr_val, g->builtin_types.entry_usize->type_ref, "");
4702 return LLVMBuildPtrToInt(g->builder, ptr_val, g->builtin_types.entry_usize->llvm_type, "");
46914703}
46924704
46934705static LLVMValueRef get_handle_fn_val(CodeGen *g) {
......@@ -4706,7 +4718,7 @@ static LLVMValueRef get_handle_fn_val(CodeGen *g) {
47064718static LLVMValueRef ir_render_handle(CodeGen *g, IrExecutable *executable,
47074719 IrInstructionHandle *instruction)
47084720{
4709 LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_promise->type_ref);
4721 LLVMValueRef zero = LLVMConstNull(get_llvm_type(g, g->builtin_types.entry_promise));
47104722 return LLVMBuildCall(g->builder, get_handle_fn_val(g), &zero, 0, "");
47114723}
47124724
......@@ -4786,7 +4798,7 @@ static LLVMValueRef ir_render_test_err(CodeGen *g, IrExecutable *executable, IrI
47864798 err_val = err_union_handle;
47874799 }
47884800
4789 LLVMValueRef zero = LLVMConstNull(g->err_tag_type->type_ref);
4801 LLVMValueRef zero = LLVMConstNull(get_llvm_type(g, g->err_tag_type));
47904802 return LLVMBuildICmp(g->builder, LLVMIntNE, err_val, zero, "");
47914803}
47924804
......@@ -4834,7 +4846,7 @@ static LLVMValueRef ir_render_unwrap_err_payload(CodeGen *g, IrExecutable *execu
48344846 } else {
48354847 err_val = err_union_handle;
48364848 }
4837 LLVMValueRef zero = LLVMConstNull(g->err_tag_type->type_ref);
4849 LLVMValueRef zero = LLVMConstNull(get_llvm_type(g, g->err_tag_type));
48384850 LLVMValueRef cond_val = LLVMBuildICmp(g->builder, LLVMIntEQ, err_val, zero, "");
48394851 LLVMBasicBlockRef err_block = LLVMAppendBasicBlock(g->cur_fn_val, "UnwrapErrError");
48404852 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "UnwrapErrOk");
......@@ -4860,7 +4872,7 @@ static LLVMValueRef ir_render_maybe_wrap(CodeGen *g, IrExecutable *executable, I
48604872
48614873 ZigType *child_type = wanted_type->data.maybe.child_type;
48624874
4863 if (child_type->zero_bits) {
4875 if (!type_has_bits(child_type)) {
48644876 return LLVMConstInt(LLVMInt1Type(), 1, false);
48654877 }
48664878
......@@ -4913,7 +4925,7 @@ static LLVMValueRef ir_render_err_wrap_payload(CodeGen *g, IrExecutable *executa
49134925 return ir_llvm_value(g, instruction->value);
49144926 }
49154927
4916 LLVMValueRef ok_err_val = LLVMConstNull(g->err_tag_type->type_ref);
4928 LLVMValueRef ok_err_val = LLVMConstNull(get_llvm_type(g, g->err_tag_type));
49174929
49184930 if (!type_has_bits(payload_type))
49194931 return ok_err_val;
......@@ -4991,7 +5003,7 @@ static LLVMValueRef ir_render_union_init(CodeGen *g, IrExecutable *executable, I
49915003 LLVMValueRef tag_field_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr,
49925004 union_type->data.unionation.gen_tag_index, "");
49935005
4994 LLVMValueRef tag_value = bigint_to_llvm_const(union_type->data.unionation.tag_type->type_ref,
5006 LLVMValueRef tag_value = bigint_to_llvm_const(get_llvm_type(g, union_type->data.unionation.tag_type),
49955007 &type_union_field->enum_field->value);
49965008 gen_store_untyped(g, tag_value, tag_field_ptr, 0, false);
49975009
......@@ -5001,7 +5013,7 @@ static LLVMValueRef ir_render_union_init(CodeGen *g, IrExecutable *executable, I
50015013 uncasted_union_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, (unsigned)0, "");
50025014 }
50035015
5004 LLVMValueRef field_ptr = LLVMBuildBitCast(g->builder, uncasted_union_ptr, ptr_type->type_ref, "");
5016 LLVMValueRef field_ptr = LLVMBuildBitCast(g->builder, uncasted_union_ptr, get_llvm_type(g, ptr_type), "");
50055017 LLVMValueRef value = ir_llvm_value(g, instruction->init_value);
50065018
50075019 gen_assign_raw(g, field_ptr, ptr_type, value);
......@@ -5023,8 +5035,8 @@ static LLVMValueRef ir_render_container_init_list(CodeGen *g, IrExecutable *exec
50235035 for (size_t i = 0; i < field_count; i += 1) {
50245036 LLVMValueRef elem_val = ir_llvm_value(g, instruction->items[i]);
50255037 LLVMValueRef indices[] = {
5026 LLVMConstNull(g->builtin_types.entry_usize->type_ref),
5027 LLVMConstInt(g->builtin_types.entry_usize->type_ref, i, false),
5038 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),
5039 LLVMConstInt(g->builtin_types.entry_usize->llvm_type, i, false),
50285040 };
50295041 LLVMValueRef elem_ptr = LLVMBuildInBoundsGEP(g->builder, tmp_array_ptr, indices, 2, "");
50305042 gen_assign_raw(g, elem_ptr, get_pointer_to_type(g, child_type, false), elem_val);
......@@ -5041,7 +5053,7 @@ static LLVMValueRef ir_render_panic(CodeGen *g, IrExecutable *executable, IrInst
50415053static LLVMValueRef ir_render_coro_id(CodeGen *g, IrExecutable *executable, IrInstructionCoroId *instruction) {
50425054 LLVMValueRef promise_ptr = ir_llvm_value(g, instruction->promise_ptr);
50435055 LLVMValueRef align_val = LLVMConstInt(LLVMInt32Type(), get_coro_frame_align_bytes(g), false);
5044 LLVMValueRef null = LLVMConstIntToPtr(LLVMConstNull(g->builtin_types.entry_usize->type_ref),
5056 LLVMValueRef null = LLVMConstIntToPtr(LLVMConstNull(g->builtin_types.entry_usize->llvm_type),
50455057 LLVMPointerType(LLVMInt8Type(), 0));
50465058 LLVMValueRef params[] = {
50475059 align_val,
......@@ -5140,7 +5152,7 @@ static LLVMValueRef ir_render_coro_promise(CodeGen *g, IrExecutable *executable,
51405152 LLVMConstNull(LLVMInt1Type()),
51415153 };
51425154 LLVMValueRef uncasted_result = LLVMBuildCall(g->builder, get_coro_promise_fn_val(g), params, 3, "");
5143 return LLVMBuildBitCast(g->builder, uncasted_result, instruction->base.value.type->type_ref, "");
5155 return LLVMBuildBitCast(g->builder, uncasted_result, get_llvm_type(g, instruction->base.value.type), "");
51445156}
51455157
51465158static LLVMValueRef get_coro_alloc_helper_fn_val(CodeGen *g, LLVMTypeRef alloc_fn_type_ref, ZigType *fn_type) {
......@@ -5161,8 +5173,8 @@ static LLVMValueRef get_coro_alloc_helper_fn_val(CodeGen *g, LLVMTypeRef alloc_f
51615173 arg_types.append(alloc_fn_arg_types[1]);
51625174 }
51635175 arg_types.append(alloc_fn_arg_types[g->have_err_ret_tracing ? 2 : 1]);
5164 arg_types.append(ptr_to_err_code_type->type_ref);
5165 arg_types.append(g->builtin_types.entry_usize->type_ref);
5176 arg_types.append(get_llvm_type(g, ptr_to_err_code_type));
5177 arg_types.append(g->builtin_types.entry_usize->llvm_type);
51665178
51675179 LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMPointerType(LLVMInt8Type(), 0),
51685180 arg_types.items, arg_types.length, false);
......@@ -5204,7 +5216,7 @@ static LLVMValueRef get_coro_alloc_helper_fn_val(CodeGen *g, LLVMTypeRef alloc_f
52045216 next_arg += 1;
52055217 LLVMValueRef coro_size = LLVMGetParam(fn_val, next_arg);
52065218 next_arg += 1;
5207 LLVMValueRef alignment_val = LLVMConstInt(g->builtin_types.entry_u29->type_ref,
5219 LLVMValueRef alignment_val = LLVMConstInt(g->builtin_types.entry_u29->llvm_type,
52085220 get_coro_frame_align_bytes(g), false);
52095221
52105222 ConstExprValue *zero_array = create_const_str_lit(g, buf_create_from_str(""));
......@@ -5219,7 +5231,7 @@ static LLVMValueRef get_coro_alloc_helper_fn_val(CodeGen *g, LLVMTypeRef alloc_f
52195231 }
52205232 args.append(allocator_val);
52215233 args.append(undef_slice_zero->global_refs->llvm_global);
5222 args.append(LLVMGetUndef(g->builtin_types.entry_u29->type_ref));
5234 args.append(LLVMGetUndef(g->builtin_types.entry_u29->llvm_type));
52235235 args.append(coro_size);
52245236 args.append(alignment_val);
52255237 LLVMValueRef call_instruction = ZigLLVMBuildCall(g->builder, realloc_fn_val, args.items, args.length,
......@@ -5299,10 +5311,10 @@ static LLVMValueRef ir_render_atomic_rmw(CodeGen *g, IrExecutable *executable,
52995311
53005312 // it's a pointer but we need to treat it as an int
53015313 LLVMValueRef casted_ptr = LLVMBuildBitCast(g->builder, ptr,
5302 LLVMPointerType(g->builtin_types.entry_usize->type_ref, 0), "");
5303 LLVMValueRef casted_operand = LLVMBuildPtrToInt(g->builder, operand, g->builtin_types.entry_usize->type_ref, "");
5314 LLVMPointerType(g->builtin_types.entry_usize->llvm_type, 0), "");
5315 LLVMValueRef casted_operand = LLVMBuildPtrToInt(g->builder, operand, g->builtin_types.entry_usize->llvm_type, "");
53045316 LLVMValueRef uncasted_result = LLVMBuildAtomicRMW(g->builder, op, casted_ptr, casted_operand, ordering, false);
5305 return LLVMBuildIntToPtr(g->builder, uncasted_result, operand_type->type_ref, "");
5317 return LLVMBuildIntToPtr(g->builder, uncasted_result, get_llvm_type(g, operand_type), "");
53065318}
53075319
53085320static LLVMValueRef ir_render_atomic_load(CodeGen *g, IrExecutable *executable,
......@@ -5355,15 +5367,15 @@ static LLVMValueRef ir_render_bswap(CodeGen *g, IrExecutable *executable, IrInst
53555367 ZigType *extended_type = get_int_type(g, int_type->data.integral.is_signed,
53565368 int_type->data.integral.bit_count + 8);
53575369 // aabbcc
5358 LLVMValueRef extended = LLVMBuildZExt(g->builder, op, extended_type->type_ref, "");
5370 LLVMValueRef extended = LLVMBuildZExt(g->builder, op, get_llvm_type(g, extended_type), "");
53595371 // 00aabbcc
53605372 LLVMValueRef fn_val = get_int_builtin_fn(g, extended_type, BuiltinFnIdBswap);
53615373 LLVMValueRef swapped = LLVMBuildCall(g->builder, fn_val, &extended, 1, "");
53625374 // ccbbaa00
53635375 LLVMValueRef shifted = ZigLLVMBuildLShrExact(g->builder, swapped,
5364 LLVMConstInt(extended_type->type_ref, 8, false), "");
5376 LLVMConstInt(get_llvm_type(g, extended_type), 8, false), "");
53655377 // 00ccbbaa
5366 return LLVMBuildTrunc(g->builder, shifted, int_type->type_ref, "");
5378 return LLVMBuildTrunc(g->builder, shifted, get_llvm_type(g, int_type), "");
53675379}
53685380
53695381static LLVMValueRef ir_render_bit_reverse(CodeGen *g, IrExecutable *executable, IrInstructionBitReverse *instruction) {
......@@ -5383,7 +5395,7 @@ static LLVMValueRef ir_render_vector_to_array(CodeGen *g, IrExecutable *executab
53835395 assert(instruction->tmp_ptr);
53845396 LLVMValueRef vector = ir_llvm_value(g, instruction->vector);
53855397 LLVMValueRef casted_ptr = LLVMBuildBitCast(g->builder, instruction->tmp_ptr,
5386 LLVMPointerType(instruction->vector->value.type->type_ref, 0), "");
5398 LLVMPointerType(get_llvm_type(g, instruction->vector->value.type), 0), "");
53875399 gen_store_untyped(g, vector, casted_ptr, 0, false);
53885400 return instruction->tmp_ptr;
53895401}
......@@ -5396,7 +5408,7 @@ static LLVMValueRef ir_render_array_to_vector(CodeGen *g, IrExecutable *executab
53965408 assert(!handle_is_ptr(vector_type));
53975409 LLVMValueRef array_ptr = ir_llvm_value(g, instruction->array);
53985410 LLVMValueRef casted_ptr = LLVMBuildBitCast(g->builder, array_ptr,
5399 LLVMPointerType(vector_type->type_ref, 0), "");
5411 LLVMPointerType(get_llvm_type(g, vector_type), 0), "");
54005412 return gen_load_untyped(g, casted_ptr, 0, false, "");
54015413}
54025414
......@@ -5685,6 +5697,7 @@ static void ir_render(CodeGen *g, ZigFn *fn_entry) {
56855697 IrInstruction *instruction = current_block->instruction_list.at(instr_i);
56865698 if (instruction->ref_count == 0 && !ir_has_side_effects(instruction))
56875699 continue;
5700
56885701 instruction->llvm_value = ir_render_instruction(g, executable, instruction);
56895702 }
56905703 current_block->llvm_exit_block = LLVMGetInsertBlock(g->builder);
......@@ -5735,15 +5748,15 @@ static LLVMValueRef gen_const_ptr_array_recursive(CodeGen *g, ConstExprValue *ar
57355748 if (el_type == LLVMArrayTypeKind) {
57365749 ZigType *usize = g->builtin_types.entry_usize;
57375750 LLVMValueRef indices[] = {
5738 LLVMConstNull(usize->type_ref),
5739 LLVMConstInt(usize->type_ref, index, false),
5751 LLVMConstNull(usize->llvm_type),
5752 LLVMConstInt(usize->llvm_type, index, false),
57405753 };
57415754 return LLVMConstInBoundsGEP(base_ptr, indices, 2);
57425755 } else if (el_type == LLVMStructTypeKind) {
57435756 ZigType *u32 = g->builtin_types.entry_u32;
57445757 LLVMValueRef indices[] = {
5745 LLVMConstNull(u32->type_ref),
5746 LLVMConstInt(u32->type_ref, index, false),
5758 LLVMConstNull(get_llvm_type(g, u32)),
5759 LLVMConstInt(get_llvm_type(g, u32), index, false),
57475760 };
57485761 return LLVMConstInBoundsGEP(base_ptr, indices, 2);
57495762 } else {
......@@ -5758,8 +5771,8 @@ static LLVMValueRef gen_const_ptr_struct_recursive(CodeGen *g, ConstExprValue *s
57585771
57595772 ZigType *u32 = g->builtin_types.entry_u32;
57605773 LLVMValueRef indices[] = {
5761 LLVMConstNull(u32->type_ref),
5762 LLVMConstInt(u32->type_ref, field_index, false),
5774 LLVMConstNull(get_llvm_type(g, u32)),
5775 LLVMConstInt(get_llvm_type(g, u32), field_index, false),
57635776 };
57645777 return LLVMConstInBoundsGEP(base_ptr, indices, 2);
57655778}
......@@ -5770,8 +5783,8 @@ static LLVMValueRef gen_const_ptr_err_union_code_recursive(CodeGen *g, ConstExpr
57705783
57715784 ZigType *u32 = g->builtin_types.entry_u32;
57725785 LLVMValueRef indices[] = {
5773 LLVMConstNull(u32->type_ref),
5774 LLVMConstInt(u32->type_ref, err_union_err_index, false),
5786 LLVMConstNull(get_llvm_type(g, u32)),
5787 LLVMConstInt(get_llvm_type(g, u32), err_union_err_index, false),
57755788 };
57765789 return LLVMConstInBoundsGEP(base_ptr, indices, 2);
57775790}
......@@ -5782,8 +5795,8 @@ static LLVMValueRef gen_const_ptr_err_union_payload_recursive(CodeGen *g, ConstE
57825795
57835796 ZigType *u32 = g->builtin_types.entry_u32;
57845797 LLVMValueRef indices[] = {
5785 LLVMConstNull(u32->type_ref),
5786 LLVMConstInt(u32->type_ref, err_union_payload_index, false),
5798 LLVMConstNull(get_llvm_type(g, u32)),
5799 LLVMConstInt(get_llvm_type(g, u32), err_union_payload_index, false),
57875800 };
57885801 return LLVMConstInBoundsGEP(base_ptr, indices, 2);
57895802}
......@@ -5794,8 +5807,8 @@ static LLVMValueRef gen_const_ptr_optional_payload_recursive(CodeGen *g, ConstEx
57945807
57955808 ZigType *u32 = g->builtin_types.entry_u32;
57965809 LLVMValueRef indices[] = {
5797 LLVMConstNull(u32->type_ref),
5798 LLVMConstInt(u32->type_ref, maybe_child_index, false),
5810 LLVMConstNull(get_llvm_type(g, u32)),
5811 LLVMConstInt(get_llvm_type(g, u32), maybe_child_index, false),
57995812 };
58005813 return LLVMConstInBoundsGEP(base_ptr, indices, 2);
58015814}
......@@ -5806,8 +5819,8 @@ static LLVMValueRef gen_const_ptr_union_recursive(CodeGen *g, ConstExprValue *un
58065819
58075820 ZigType *u32 = g->builtin_types.entry_u32;
58085821 LLVMValueRef indices[] = {
5809 LLVMConstNull(u32->type_ref),
5810 LLVMConstInt(u32->type_ref, 0, false), // TODO test const union with more aligned tag type than payload
5822 LLVMConstNull(get_llvm_type(g, u32)),
5823 LLVMConstInt(get_llvm_type(g, u32), 0, false), // TODO test const union with more aligned tag type than payload
58115824 };
58125825 return LLVMConstInBoundsGEP(base_ptr, indices, 2);
58135826}
......@@ -5823,7 +5836,7 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con
58235836 }
58245837
58255838 ZigType *type_entry = const_val->type;
5826 assert(!type_entry->zero_bits);
5839 assert(type_has_bits(type_entry));
58275840 switch (type_entry->id) {
58285841 case ZigTypeIdInvalid:
58295842 case ZigTypeIdMetaType:
......@@ -5866,7 +5879,7 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con
58665879 case ZigTypeIdPromise:
58675880 {
58685881 LLVMValueRef ptr_val = gen_const_val(g, const_val, "");
5869 LLVMValueRef ptr_size_int_val = LLVMConstPtrToInt(ptr_val, g->builtin_types.entry_usize->type_ref);
5882 LLVMValueRef ptr_size_int_val = LLVMConstPtrToInt(ptr_val, g->builtin_types.entry_usize->llvm_type);
58705883 return LLVMConstZExt(ptr_size_int_val, big_int_type_ref);
58715884 }
58725885 case ZigTypeIdArray: {
......@@ -5933,8 +5946,8 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con
59335946
59345947// We have this because union constants can't be represented by the official union type,
59355948// and this property bubbles up in whatever aggregate type contains a union constant
5936static bool is_llvm_value_unnamed_type(ZigType *type_entry, LLVMValueRef val) {
5937 return LLVMTypeOf(val) != type_entry->type_ref;
5949static bool is_llvm_value_unnamed_type(CodeGen *g, ZigType *type_entry, LLVMValueRef val) {
5950 return LLVMTypeOf(val) != get_llvm_type(g, type_entry);
59385951}
59395952
59405953static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, const char *name) {
......@@ -5948,7 +5961,8 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
59485961 ConstExprValue *pointee = const_val->data.x_ptr.data.ref.pointee;
59495962 render_const_val(g, pointee, "");
59505963 render_const_val_global(g, pointee, "");
5951 const_val->global_refs->llvm_value = LLVMConstBitCast(pointee->global_refs->llvm_global, const_val->type->type_ref);
5964 const_val->global_refs->llvm_value = LLVMConstBitCast(pointee->global_refs->llvm_global,
5965 get_llvm_type(g, const_val->type));
59525966 return const_val->global_refs->llvm_value;
59535967 }
59545968 case ConstPtrSpecialBaseArray:
......@@ -5959,13 +5973,13 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
59595973 if (!type_has_bits(array_const_val->type)) {
59605974 // make this a null pointer
59615975 ZigType *usize = g->builtin_types.entry_usize;
5962 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->type_ref),
5963 const_val->type->type_ref);
5976 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type),
5977 get_llvm_type(g, const_val->type));
59645978 return const_val->global_refs->llvm_value;
59655979 }
59665980 size_t elem_index = const_val->data.x_ptr.data.base_array.elem_index;
59675981 LLVMValueRef uncasted_ptr_val = gen_const_ptr_array_recursive(g, array_const_val, elem_index);
5968 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, const_val->type->type_ref);
5982 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, get_llvm_type(g, const_val->type));
59695983 const_val->global_refs->llvm_value = ptr_val;
59705984 return ptr_val;
59715985 }
......@@ -5977,15 +5991,15 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
59775991 if (!type_has_bits(struct_const_val->type)) {
59785992 // make this a null pointer
59795993 ZigType *usize = g->builtin_types.entry_usize;
5980 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->type_ref),
5981 const_val->type->type_ref);
5994 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type),
5995 get_llvm_type(g, const_val->type));
59825996 return const_val->global_refs->llvm_value;
59835997 }
59845998 size_t src_field_index = const_val->data.x_ptr.data.base_struct.field_index;
59855999 size_t gen_field_index = struct_const_val->type->data.structure.fields[src_field_index].gen_index;
59866000 LLVMValueRef uncasted_ptr_val = gen_const_ptr_struct_recursive(g, struct_const_val,
59876001 gen_field_index);
5988 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, const_val->type->type_ref);
6002 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, get_llvm_type(g, const_val->type));
59896003 const_val->global_refs->llvm_value = ptr_val;
59906004 return ptr_val;
59916005 }
......@@ -5997,12 +6011,12 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
59976011 if (!type_has_bits(err_union_const_val->type)) {
59986012 // make this a null pointer
59996013 ZigType *usize = g->builtin_types.entry_usize;
6000 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->type_ref),
6001 const_val->type->type_ref);
6014 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type),
6015 get_llvm_type(g, const_val->type));
60026016 return const_val->global_refs->llvm_value;
60036017 }
60046018 LLVMValueRef uncasted_ptr_val = gen_const_ptr_err_union_code_recursive(g, err_union_const_val);
6005 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, const_val->type->type_ref);
6019 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, get_llvm_type(g, const_val->type));
60066020 const_val->global_refs->llvm_value = ptr_val;
60076021 return ptr_val;
60086022 }
......@@ -6011,15 +6025,15 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
60116025 assert(const_val->global_refs != nullptr);
60126026 ConstExprValue *err_union_const_val = const_val->data.x_ptr.data.base_err_union_payload.err_union_val;
60136027 assert(err_union_const_val->type->id == ZigTypeIdErrorUnion);
6014 if (err_union_const_val->type->zero_bits) {
6028 if (!type_has_bits(err_union_const_val->type)) {
60156029 // make this a null pointer
60166030 ZigType *usize = g->builtin_types.entry_usize;
6017 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->type_ref),
6018 const_val->type->type_ref);
6031 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type),
6032 get_llvm_type(g, const_val->type));
60196033 return const_val->global_refs->llvm_value;
60206034 }
60216035 LLVMValueRef uncasted_ptr_val = gen_const_ptr_err_union_payload_recursive(g, err_union_const_val);
6022 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, const_val->type->type_ref);
6036 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, get_llvm_type(g, const_val->type));
60236037 const_val->global_refs->llvm_value = ptr_val;
60246038 return ptr_val;
60256039 }
......@@ -6028,15 +6042,15 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
60286042 assert(const_val->global_refs != nullptr);
60296043 ConstExprValue *optional_const_val = const_val->data.x_ptr.data.base_optional_payload.optional_val;
60306044 assert(optional_const_val->type->id == ZigTypeIdOptional);
6031 if (optional_const_val->type->zero_bits) {
6045 if (!type_has_bits(optional_const_val->type)) {
60326046 // make this a null pointer
60336047 ZigType *usize = g->builtin_types.entry_usize;
6034 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->type_ref),
6035 const_val->type->type_ref);
6048 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type),
6049 get_llvm_type(g, const_val->type));
60366050 return const_val->global_refs->llvm_value;
60376051 }
60386052 LLVMValueRef uncasted_ptr_val = gen_const_ptr_optional_payload_recursive(g, optional_const_val);
6039 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, const_val->type->type_ref);
6053 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, get_llvm_type(g, const_val->type));
60406054 const_val->global_refs->llvm_value = ptr_val;
60416055 return ptr_val;
60426056 }
......@@ -6046,50 +6060,51 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
60466060 uint64_t addr_value = const_val->data.x_ptr.data.hard_coded_addr.addr;
60476061 ZigType *usize = g->builtin_types.entry_usize;
60486062 const_val->global_refs->llvm_value = LLVMConstIntToPtr(
6049 LLVMConstInt(usize->type_ref, addr_value, false), const_val->type->type_ref);
6063 LLVMConstInt(usize->llvm_type, addr_value, false), get_llvm_type(g, const_val->type));
60506064 return const_val->global_refs->llvm_value;
60516065 }
60526066 case ConstPtrSpecialFunction:
6053 return LLVMConstBitCast(fn_llvm_value(g, const_val->data.x_ptr.data.fn.fn_entry), const_val->type->type_ref);
6067 return LLVMConstBitCast(fn_llvm_value(g, const_val->data.x_ptr.data.fn.fn_entry),
6068 get_llvm_type(g, const_val->type));
60546069 case ConstPtrSpecialNull:
6055 return LLVMConstNull(const_val->type->type_ref);
6070 return LLVMConstNull(get_llvm_type(g, const_val->type));
60566071 }
60576072 zig_unreachable();
60586073}
60596074
60606075static LLVMValueRef gen_const_val_err_set(CodeGen *g, ConstExprValue *const_val, const char *name) {
60616076 uint64_t value = (const_val->data.x_err_set == nullptr) ? 0 : const_val->data.x_err_set->value;
6062 return LLVMConstInt(g->builtin_types.entry_global_error_set->type_ref, value, false);
6077 return LLVMConstInt(get_llvm_type(g, g->builtin_types.entry_global_error_set), value, false);
60636078}
60646079
60656080static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const char *name) {
60666081 Error err;
60676082
60686083 ZigType *type_entry = const_val->type;
6069 assert(!type_entry->zero_bits);
6084 assert(type_has_bits(type_entry));
60706085
60716086 switch (const_val->special) {
60726087 case ConstValSpecialRuntime:
60736088 zig_unreachable();
60746089 case ConstValSpecialUndef:
6075 return LLVMGetUndef(type_entry->type_ref);
6090 return LLVMGetUndef(get_llvm_type(g, type_entry));
60766091 case ConstValSpecialStatic:
60776092 break;
60786093 }
60796094
60806095 switch (type_entry->id) {
60816096 case ZigTypeIdInt:
6082 return bigint_to_llvm_const(type_entry->type_ref, &const_val->data.x_bigint);
6097 return bigint_to_llvm_const(get_llvm_type(g, type_entry), &const_val->data.x_bigint);
60836098 case ZigTypeIdErrorSet:
60846099 return gen_const_val_err_set(g, const_val, name);
60856100 case ZigTypeIdFloat:
60866101 switch (type_entry->data.floating.bit_count) {
60876102 case 16:
6088 return LLVMConstReal(type_entry->type_ref, zig_f16_to_double(const_val->data.x_f16));
6103 return LLVMConstReal(get_llvm_type(g, type_entry), zig_f16_to_double(const_val->data.x_f16));
60896104 case 32:
6090 return LLVMConstReal(type_entry->type_ref, const_val->data.x_f32);
6105 return LLVMConstReal(get_llvm_type(g, type_entry), const_val->data.x_f32);
60916106 case 64:
6092 return LLVMConstReal(type_entry->type_ref, const_val->data.x_f64);
6107 return LLVMConstReal(get_llvm_type(g, type_entry), const_val->data.x_f64);
60936108 case 128:
60946109 {
60956110 // TODO make sure this is correct on big endian targets too
......@@ -6097,7 +6112,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
60976112 memcpy(buf, &const_val->data.x_f128, 16);
60986113 LLVMValueRef as_int = LLVMConstIntOfArbitraryPrecision(LLVMInt128Type(), 2,
60996114 (uint64_t*)buf);
6100 return LLVMConstBitCast(as_int, type_entry->type_ref);
6115 return LLVMConstBitCast(as_int, get_llvm_type(g, type_entry));
61016116 }
61026117 default:
61036118 zig_unreachable();
......@@ -6125,9 +6140,9 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
61256140 child_val = gen_const_val(g, const_val->data.x_optional, "");
61266141 maybe_val = LLVMConstAllOnes(LLVMInt1Type());
61276142
6128 make_unnamed_struct = is_llvm_value_unnamed_type(const_val->type, child_val);
6143 make_unnamed_struct = is_llvm_value_unnamed_type(g, const_val->type, child_val);
61296144 } else {
6130 child_val = LLVMGetUndef(child_type->type_ref);
6145 child_val = LLVMGetUndef(get_llvm_type(g, child_type));
61316146 maybe_val = LLVMConstNull(LLVMInt1Type());
61326147
61336148 make_unnamed_struct = false;
......@@ -6139,7 +6154,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
61396154 if (make_unnamed_struct) {
61406155 return LLVMConstStruct(fields, 2, false);
61416156 } else {
6142 return LLVMConstNamedStruct(type_entry->type_ref, fields, 2);
6157 return LLVMConstNamedStruct(get_llvm_type(g, type_entry), fields, 2);
61436158 }
61446159 }
61456160 }
......@@ -6168,10 +6183,10 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
61686183 ConstExprValue *field_val = &const_val->data.x_struct.fields[src_field_index];
61696184 LLVMValueRef val = gen_const_val(g, field_val, "");
61706185 fields[type_struct_field->gen_index] = val;
6171 make_unnamed_struct = make_unnamed_struct || is_llvm_value_unnamed_type(field_val->type, val);
6186 make_unnamed_struct = make_unnamed_struct || is_llvm_value_unnamed_type(g, field_val->type, val);
61726187 } else {
61736188 bool is_big_endian = g->is_big_endian; // TODO get endianness from struct type
6174 LLVMTypeRef big_int_type_ref = LLVMStructGetTypeAtIndex(type_entry->type_ref,
6189 LLVMTypeRef big_int_type_ref = LLVMStructGetTypeAtIndex(get_llvm_type(g, type_entry),
61756190 (unsigned)type_struct_field->gen_index);
61766191 LLVMValueRef val = LLVMConstInt(big_int_type_ref, 0, false);
61776192 size_t used_bits = 0;
......@@ -6216,14 +6231,14 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
62166231
62176232 LLVMValueRef val = gen_const_val(g, field_val, "");
62186233 fields[type_struct_field->gen_index] = val;
6219 make_unnamed_struct = make_unnamed_struct || is_llvm_value_unnamed_type(field_val->type, val);
6234 make_unnamed_struct = make_unnamed_struct || is_llvm_value_unnamed_type(g, field_val->type, val);
62206235 }
62216236 }
62226237 if (make_unnamed_struct) {
62236238 return LLVMConstStruct(fields, type_entry->data.structure.gen_field_count,
62246239 type_entry->data.structure.layout == ContainerLayoutPacked);
62256240 } else {
6226 return LLVMConstNamedStruct(type_entry->type_ref, fields, type_entry->data.structure.gen_field_count);
6241 return LLVMConstNamedStruct(get_llvm_type(g, type_entry), fields, type_entry->data.structure.gen_field_count);
62276242 }
62286243 }
62296244 case ZigTypeIdArray:
......@@ -6231,16 +6246,16 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
62316246 uint64_t len = type_entry->data.array.len;
62326247 switch (const_val->data.x_array.special) {
62336248 case ConstArraySpecialUndef:
6234 return LLVMGetUndef(type_entry->type_ref);
6249 return LLVMGetUndef(get_llvm_type(g, type_entry));
62356250 case ConstArraySpecialNone: {
62366251 LLVMValueRef *values = allocate<LLVMValueRef>(len);
6237 LLVMTypeRef element_type_ref = type_entry->data.array.child_type->type_ref;
6252 LLVMTypeRef element_type_ref = get_llvm_type(g, type_entry->data.array.child_type);
62386253 bool make_unnamed_struct = false;
62396254 for (uint64_t i = 0; i < len; i += 1) {
62406255 ConstExprValue *elem_value = &const_val->data.x_array.data.s_none.elements[i];
62416256 LLVMValueRef val = gen_const_val(g, elem_value, "");
62426257 values[i] = val;
6243 make_unnamed_struct = make_unnamed_struct || is_llvm_value_unnamed_type(elem_value->type, val);
6258 make_unnamed_struct = make_unnamed_struct || is_llvm_value_unnamed_type(g, elem_value->type, val);
62446259 }
62456260 if (make_unnamed_struct) {
62466261 return LLVMConstStruct(values, len, true);
......@@ -6259,7 +6274,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
62596274 uint32_t len = type_entry->data.vector.len;
62606275 switch (const_val->data.x_array.special) {
62616276 case ConstArraySpecialUndef:
6262 return LLVMGetUndef(type_entry->type_ref);
6277 return LLVMGetUndef(get_llvm_type(g, type_entry));
62636278 case ConstArraySpecialNone: {
62646279 LLVMValueRef *values = allocate<LLVMValueRef>(len);
62656280 for (uint64_t i = 0; i < len; i += 1) {
......@@ -6273,7 +6288,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
62736288 assert(buf_len(buf) == len);
62746289 LLVMValueRef *values = allocate<LLVMValueRef>(len);
62756290 for (uint64_t i = 0; i < len; i += 1) {
6276 values[i] = LLVMConstInt(g->builtin_types.entry_u8->type_ref, buf_ptr(buf)[i], false);
6291 values[i] = LLVMConstInt(g->builtin_types.entry_u8->llvm_type, buf_ptr(buf)[i], false);
62776292 }
62786293 return LLVMConstVector(values, len);
62796294 }
......@@ -6282,31 +6297,36 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
62826297 }
62836298 case ZigTypeIdUnion:
62846299 {
6285 LLVMTypeRef union_type_ref = type_entry->data.unionation.union_type_ref;
6300 // Force type_entry->data.unionation.union_llvm_type to get resolved
6301 (void)get_llvm_type(g, type_entry);
62866302
62876303 if (type_entry->data.unionation.gen_field_count == 0) {
62886304 if (type_entry->data.unionation.tag_type == nullptr) {
62896305 return nullptr;
62906306 } else {
6291 return bigint_to_llvm_const(type_entry->data.unionation.tag_type->type_ref,
6307 return bigint_to_llvm_const(get_llvm_type(g, type_entry->data.unionation.tag_type),
62926308 &const_val->data.x_union.tag);
62936309 }
62946310 }
62956311
6312 LLVMTypeRef union_type_ref = type_entry->data.unionation.union_llvm_type;
6313 assert(union_type_ref != nullptr);
6314
62966315 LLVMValueRef union_value_ref;
62976316 bool make_unnamed_struct;
62986317 ConstExprValue *payload_value = const_val->data.x_union.payload;
62996318 if (payload_value == nullptr || !type_has_bits(payload_value->type)) {
63006319 if (type_entry->data.unionation.gen_tag_index == SIZE_MAX)
6301 return LLVMGetUndef(type_entry->type_ref);
6320 return LLVMGetUndef(get_llvm_type(g, type_entry));
63026321
63036322 union_value_ref = LLVMGetUndef(union_type_ref);
63046323 make_unnamed_struct = false;
63056324 } else {
6306 uint64_t field_type_bytes = LLVMStoreSizeOfType(g->target_data_ref, payload_value->type->type_ref);
6307 uint64_t pad_bytes = type_entry->data.unionation.union_size_bytes - field_type_bytes;
6325 uint64_t field_type_bytes = LLVMStoreSizeOfType(g->target_data_ref,
6326 get_llvm_type(g, payload_value->type));
6327 uint64_t pad_bytes = type_entry->data.unionation.union_abi_size - field_type_bytes;
63086328 LLVMValueRef correctly_typed_value = gen_const_val(g, payload_value, "");
6309 make_unnamed_struct = is_llvm_value_unnamed_type(payload_value->type, correctly_typed_value) ||
6329 make_unnamed_struct = is_llvm_value_unnamed_type(g, payload_value->type, correctly_typed_value) ||
63106330 payload_value->type != type_entry->data.unionation.most_aligned_union_member;
63116331
63126332 {
......@@ -6329,7 +6349,8 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
63296349 }
63306350 }
63316351
6332 LLVMValueRef tag_value = bigint_to_llvm_const(type_entry->data.unionation.tag_type->type_ref,
6352 LLVMValueRef tag_value = bigint_to_llvm_const(
6353 get_llvm_type(g, type_entry->data.unionation.tag_type),
63336354 &const_val->data.x_union.tag);
63346355
63356356 LLVMValueRef fields[3];
......@@ -6341,7 +6362,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
63416362 uint64_t last_field_offset = LLVMOffsetOfElement(g->target_data_ref, LLVMTypeOf(result), 1);
63426363 uint64_t end_offset = last_field_offset +
63436364 LLVMStoreSizeOfType(g->target_data_ref, LLVMTypeOf(fields[1]));
6344 uint64_t expected_sz = LLVMStoreSizeOfType(g->target_data_ref, type_entry->type_ref);
6365 uint64_t expected_sz = LLVMStoreSizeOfType(g->target_data_ref, get_llvm_type(g, type_entry));
63456366 unsigned pad_sz = expected_sz - end_offset;
63466367 if (pad_sz != 0) {
63476368 fields[2] = LLVMGetUndef(LLVMArrayType(LLVMInt8Type(), pad_sz));
......@@ -6351,21 +6372,21 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
63516372 assert(actual_sz == expected_sz);
63526373 return result;
63536374 } else {
6354 return LLVMConstNamedStruct(type_entry->type_ref, fields, 2);
6375 return LLVMConstNamedStruct(get_llvm_type(g, type_entry), fields, 2);
63556376 }
63566377
63576378 }
63586379
63596380 case ZigTypeIdEnum:
6360 return bigint_to_llvm_const(type_entry->type_ref, &const_val->data.x_enum_tag);
6381 return bigint_to_llvm_const(get_llvm_type(g, type_entry), &const_val->data.x_enum_tag);
63616382 case ZigTypeIdFn:
63626383 if (const_val->data.x_ptr.special == ConstPtrSpecialFunction) {
63636384 assert(const_val->data.x_ptr.mut == ConstPtrMutComptimeConst);
63646385 return fn_llvm_value(g, const_val->data.x_ptr.data.fn.fn_entry);
63656386 } else if (const_val->data.x_ptr.special == ConstPtrSpecialHardCodedAddr) {
6366 LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->type_ref;
6387 LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->llvm_type;
63676388 uint64_t addr = const_val->data.x_ptr.data.hard_coded_addr.addr;
6368 return LLVMConstIntToPtr(LLVMConstInt(usize_type_ref, addr, false), type_entry->type_ref);
6389 return LLVMConstIntToPtr(LLVMConstInt(usize_type_ref, addr, false), get_llvm_type(g, type_entry));
63696390 } else {
63706391 zig_unreachable();
63716392 }
......@@ -6379,7 +6400,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
63796400 assert(type_has_bits(err_set_type));
63806401 ErrorTableEntry *err_set = const_val->data.x_err_union.error_set->data.x_err_set;
63816402 uint64_t value = (err_set == nullptr) ? 0 : err_set->value;
6382 return LLVMConstInt(g->err_tag_type->type_ref, value, false);
6403 return LLVMConstInt(get_llvm_type(g, g->err_tag_type), value, false);
63836404 } else if (!type_has_bits(err_set_type)) {
63846405 assert(type_has_bits(payload_type));
63856406 return gen_const_val(g, const_val->data.x_err_union.payload, "");
......@@ -6389,17 +6410,17 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
63896410 bool make_unnamed_struct;
63906411 ErrorTableEntry *err_set = const_val->data.x_err_union.error_set->data.x_err_set;
63916412 if (err_set != nullptr) {
6392 err_tag_value = LLVMConstInt(g->err_tag_type->type_ref, err_set->value, false);
6393 err_payload_value = LLVMConstNull(payload_type->type_ref);
6413 err_tag_value = LLVMConstInt(get_llvm_type(g, g->err_tag_type), err_set->value, false);
6414 err_payload_value = LLVMConstNull(get_llvm_type(g, payload_type));
63946415 make_unnamed_struct = false;
63956416 } else {
6396 err_tag_value = LLVMConstNull(g->err_tag_type->type_ref);
6417 err_tag_value = LLVMConstNull(get_llvm_type(g, g->err_tag_type));
63976418 ConstExprValue *payload_val = const_val->data.x_err_union.payload;
63986419 err_payload_value = gen_const_val(g, payload_val, "");
6399 make_unnamed_struct = is_llvm_value_unnamed_type(payload_val->type, err_payload_value);
6420 make_unnamed_struct = is_llvm_value_unnamed_type(g, payload_val->type, err_payload_value);
64006421 }
64016422 if (make_unnamed_struct) {
6402 uint64_t payload_off = LLVMOffsetOfElement(g->target_data_ref, type_entry->type_ref, 1);
6423 uint64_t payload_off = LLVMOffsetOfElement(g->target_data_ref, get_llvm_type(g, type_entry), 1);
64036424 uint64_t err_sz = LLVMStoreSizeOfType(g->target_data_ref, LLVMTypeOf(err_tag_value));
64046425 unsigned pad_sz = payload_off - err_sz;
64056426 if (pad_sz == 0) {
......@@ -6421,7 +6442,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
64216442 err_tag_value,
64226443 err_payload_value,
64236444 };
6424 return LLVMConstNamedStruct(type_entry->type_ref, fields, 2);
6445 return LLVMConstNamedStruct(get_llvm_type(g, type_entry), fields, 2);
64256446 }
64266447 }
64276448 }
......@@ -6460,7 +6481,8 @@ static void render_const_val_global(CodeGen *g, ConstExprValue *const_val, const
64606481 const_val->global_refs = allocate<ConstGlobalRefs>(1);
64616482
64626483 if (!const_val->global_refs->llvm_global) {
6463 LLVMTypeRef type_ref = const_val->global_refs->llvm_value ? LLVMTypeOf(const_val->global_refs->llvm_value) : const_val->type->type_ref;
6484 LLVMTypeRef type_ref = const_val->global_refs->llvm_value ?
6485 LLVMTypeOf(const_val->global_refs->llvm_value) : get_llvm_type(g, const_val->type);
64646486 LLVMValueRef global_value = LLVMAddGlobal(g->module, type_ref, name);
64656487 LLVMSetLinkage(global_value, LLVMInternalLinkage);
64666488 LLVMSetGlobalConstant(global_value, true);
......@@ -6486,7 +6508,7 @@ static void generate_error_name_table(CodeGen *g) {
64866508 ZigType *str_type = get_slice_type(g, u8_ptr_type);
64876509
64886510 LLVMValueRef *values = allocate<LLVMValueRef>(g->errors_by_index.length);
6489 values[0] = LLVMGetUndef(str_type->type_ref);
6511 values[0] = LLVMGetUndef(get_llvm_type(g, str_type));
64906512 for (size_t i = 1; i < g->errors_by_index.length; i += 1) {
64916513 ErrorTableEntry *err_entry = g->errors_by_index.at(i);
64926514 Buf *name = &err_entry->name;
......@@ -6502,13 +6524,13 @@ static void generate_error_name_table(CodeGen *g) {
65026524 LLVMSetAlignment(str_global, LLVMABIAlignmentOfType(g->target_data_ref, LLVMTypeOf(str_init)));
65036525
65046526 LLVMValueRef fields[] = {
6505 LLVMConstBitCast(str_global, u8_ptr_type->type_ref),
6506 LLVMConstInt(g->builtin_types.entry_usize->type_ref, buf_len(name), false),
6527 LLVMConstBitCast(str_global, get_llvm_type(g, u8_ptr_type)),
6528 LLVMConstInt(g->builtin_types.entry_usize->llvm_type, buf_len(name), false),
65076529 };
6508 values[i] = LLVMConstNamedStruct(str_type->type_ref, fields, 2);
6530 values[i] = LLVMConstNamedStruct(get_llvm_type(g, str_type), fields, 2);
65096531 }
65106532
6511 LLVMValueRef err_name_table_init = LLVMConstArray(str_type->type_ref, values, (unsigned)g->errors_by_index.length);
6533 LLVMValueRef err_name_table_init = LLVMConstArray(get_llvm_type(g, str_type), values, (unsigned)g->errors_by_index.length);
65126534
65136535 g->err_name_table = LLVMAddGlobal(g->module, LLVMTypeOf(err_name_table_init),
65146536 buf_ptr(get_mangled_name(g, buf_create_from_str("__zig_err_name_table"), false)));
......@@ -6547,7 +6569,7 @@ static void gen_global_var(CodeGen *g, ZigVar *var, LLVMValueRef init_val,
65476569 ZigLLVMCreateGlobalVariable(g->dbuilder, get_di_scope(g, var->parent_scope), buf_ptr(&var->name),
65486570 buf_ptr(&var->name), import->data.structure.root_struct->di_file,
65496571 (unsigned)(var->decl_node->line + 1),
6550 type_entry->di_type, is_local_to_unit);
6572 get_llvm_di_type(g, type_entry), is_local_to_unit);
65516573
65526574 // TODO ^^ make an actual global variable
65536575}
......@@ -6588,28 +6610,6 @@ static LLVMLinkage var_linkage_to_llvm(VarLinkage var_linkage) {
65886610static void do_code_gen(CodeGen *g) {
65896611 assert(!g->errors.length);
65906612
6591 {
6592 // create debug type for error sets
6593 assert(g->err_enumerators.length == g->errors_by_index.length);
6594 uint64_t tag_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, g->err_tag_type->type_ref);
6595 uint64_t tag_debug_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, g->err_tag_type->type_ref);
6596 ZigLLVMDIFile *err_set_di_file = nullptr;
6597 ZigLLVMDIType *err_set_di_type = ZigLLVMCreateDebugEnumerationType(g->dbuilder,
6598 ZigLLVMCompileUnitToScope(g->compile_unit), buf_ptr(&g->builtin_types.entry_global_error_set->name),
6599 err_set_di_file, 0,
6600 tag_debug_size_in_bits,
6601 tag_debug_align_in_bits,
6602 g->err_enumerators.items, g->err_enumerators.length,
6603 g->err_tag_type->di_type, "");
6604 ZigLLVMReplaceTemporary(g->dbuilder, g->builtin_types.entry_global_error_set->di_type, err_set_di_type);
6605 g->builtin_types.entry_global_error_set->di_type = err_set_di_type;
6606
6607 for (size_t i = 0; i < g->error_di_types.length; i += 1) {
6608 ZigLLVMDIType **di_type_ptr = g->error_di_types.at(i);
6609 *di_type_ptr = err_set_di_type;
6610 }
6611 }
6612
66136613 generate_error_name_table(g);
66146614
66156615 // Generate module level variables
......@@ -6646,7 +6646,7 @@ static void do_code_gen(CodeGen *g) {
66466646 bits_needed = 8;
66476647 }
66486648 ZigType *var_type = get_int_type(g, const_val->data.x_bigint.is_negative, bits_needed);
6649 LLVMValueRef init_val = bigint_to_llvm_const(var_type->type_ref, &const_val->data.x_bigint);
6649 LLVMValueRef init_val = bigint_to_llvm_const(get_llvm_type(g, var_type), &const_val->data.x_bigint);
66506650 gen_global_var(g, var, init_val, var_type);
66516651 continue;
66526652 }
......@@ -6660,9 +6660,10 @@ static void do_code_gen(CodeGen *g) {
66606660 if (var->linkage == VarLinkageExternal) {
66616661 LLVMValueRef existing_llvm_var = LLVMGetNamedGlobal(g->module, buf_ptr(&var->name));
66626662 if (existing_llvm_var) {
6663 global_value = LLVMConstBitCast(existing_llvm_var, LLVMPointerType(var->var_type->type_ref, 0));
6663 global_value = LLVMConstBitCast(existing_llvm_var,
6664 LLVMPointerType(get_llvm_type(g, var->var_type), 0));
66646665 } else {
6665 global_value = LLVMAddGlobal(g->module, var->var_type->type_ref, buf_ptr(&var->name));
6666 global_value = LLVMAddGlobal(g->module, get_llvm_type(g, var->var_type), buf_ptr(&var->name));
66666667 // TODO debug info for the extern variable
66676668
66686669 LLVMSetLinkage(global_value, var_linkage_to_llvm(var->linkage));
......@@ -6736,6 +6737,9 @@ static void do_code_gen(CodeGen *g) {
67366737 if (have_err_ret_trace_stack) {
67376738 ZigType *array_type = get_array_type(g, g->builtin_types.entry_usize, stack_trace_ptr_count);
67386739 err_ret_array_val = build_alloca(g, array_type, "error_return_trace_addresses", get_abi_alignment(g, array_type));
6740
6741 // populate g->stack_trace_type
6742 (void)get_ptr_to_stack_trace_type(g);
67396743 g->cur_err_ret_trace_val_stack = build_alloca(g, g->stack_trace_type, "error_return_trace", get_abi_alignment(g, g->stack_trace_type));
67406744 } else {
67416745 g->cur_err_ret_trace_val_stack = nullptr;
......@@ -6834,15 +6838,15 @@ static void do_code_gen(CodeGen *g) {
68346838
68356839 var->di_loc_var = ZigLLVMCreateAutoVariable(g->dbuilder, get_di_scope(g, var->parent_scope),
68366840 buf_ptr(&var->name), import->data.structure.root_struct->di_file, (unsigned)(var->decl_node->line + 1),
6837 var->var_type->di_type, !g->strip_debug_symbols, 0);
6841 get_llvm_di_type(g, var->var_type), !g->strip_debug_symbols, 0);
68386842
68396843 } else if (is_c_abi) {
68406844 fn_walk_var.data.vars.var = var;
68416845 iter_function_params_c_abi(g, fn_table_entry->type_entry, &fn_walk_var, var->src_arg_index);
68426846 } else {
6843 assert(var->gen_arg_index != SIZE_MAX);
68446847 ZigType *gen_type;
68456848 FnGenParamInfo *gen_info = &fn_table_entry->type_entry->data.fn.gen_param_info[var->src_arg_index];
6849 assert(gen_info->gen_index != SIZE_MAX);
68466850
68476851 if (handle_is_ptr(var->var_type)) {
68486852 if (gen_info->is_byval) {
......@@ -6850,16 +6854,16 @@ static void do_code_gen(CodeGen *g) {
68506854 } else {
68516855 gen_type = gen_info->type;
68526856 }
6853 var->value_ref = LLVMGetParam(fn, (unsigned)var->gen_arg_index);
6857 var->value_ref = LLVMGetParam(fn, gen_info->gen_index);
68546858 } else {
68556859 gen_type = var->var_type;
68566860 var->value_ref = build_alloca(g, var->var_type, buf_ptr(&var->name), var->align_bytes);
68576861 }
68586862 if (var->decl_node) {
68596863 var->di_loc_var = ZigLLVMCreateParameterVariable(g->dbuilder, get_di_scope(g, var->parent_scope),
6860 buf_ptr(&var->name), import->data.structure.root_struct->di_file,
6861 (unsigned)(var->decl_node->line + 1),
6862 gen_type->di_type, !g->strip_debug_symbols, 0, (unsigned)(var->gen_arg_index + 1));
6864 buf_ptr(&var->name), import->data.structure.root_struct->di_file,
6865 (unsigned)(var->decl_node->line + 1),
6866 get_llvm_di_type(g, gen_type), !g->strip_debug_symbols, 0, (unsigned)(gen_info->gen_index+1));
68636867 }
68646868
68656869 }
......@@ -6870,7 +6874,7 @@ static void do_code_gen(CodeGen *g) {
68706874 ZigType *usize = g->builtin_types.entry_usize;
68716875 size_t index_field_index = g->stack_trace_type->data.structure.fields[0].gen_index;
68726876 LLVMValueRef index_field_ptr = LLVMBuildStructGEP(g->builder, g->cur_err_ret_trace_val_stack, (unsigned)index_field_index, "");
6873 gen_store_untyped(g, LLVMConstNull(usize->type_ref), index_field_ptr, 0, false);
6877 gen_store_untyped(g, LLVMConstNull(usize->llvm_type), index_field_ptr, 0, false);
68746878
68756879 size_t addresses_field_index = g->stack_trace_type->data.structure.fields[1].gen_index;
68766880 LLVMValueRef addresses_field_ptr = LLVMBuildStructGEP(g->builder, g->cur_err_ret_trace_val_stack, (unsigned)addresses_field_index, "");
......@@ -6878,7 +6882,7 @@ static void do_code_gen(CodeGen *g) {
68786882 ZigType *slice_type = g->stack_trace_type->data.structure.fields[1].type_entry;
68796883 size_t ptr_field_index = slice_type->data.structure.fields[slice_ptr_index].gen_index;
68806884 LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, addresses_field_ptr, (unsigned)ptr_field_index, "");
6881 LLVMValueRef zero = LLVMConstNull(usize->type_ref);
6885 LLVMValueRef zero = LLVMConstNull(usize->llvm_type);
68826886 LLVMValueRef indices[] = {zero, zero};
68836887 LLVMValueRef err_ret_array_val_elem0_ptr = LLVMBuildInBoundsGEP(g->builder, err_ret_array_val,
68846888 indices, 2, "");
......@@ -6887,7 +6891,7 @@ static void do_code_gen(CodeGen *g) {
68876891
68886892 size_t len_field_index = slice_type->data.structure.fields[slice_len_index].gen_index;
68896893 LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, addresses_field_ptr, (unsigned)len_field_index, "");
6890 gen_store(g, LLVMConstInt(usize->type_ref, stack_trace_ptr_count, false), len_field_ptr, get_pointer_to_type(g, usize, false));
6894 gen_store(g, LLVMConstInt(usize->llvm_type, stack_trace_ptr_count, false), len_field_ptr, get_pointer_to_type(g, usize, false));
68916895 }
68926896
68936897 // create debug variable declarations for parameters
......@@ -6997,50 +7001,60 @@ static const GlobalLinkageValue global_linkage_values[] = {
69977001 {GlobalLinkageIdLinkOnce, "LinkOnce"},
69987002};
69997003
7004static void add_fp_entry(CodeGen *g, const char *name, uint32_t bit_count, LLVMTypeRef type_ref,
7005 ZigType **field)
7006{
7007 ZigType *entry = new_type_table_entry(ZigTypeIdFloat);
7008 entry->llvm_type = type_ref;
7009 entry->size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, entry->llvm_type);
7010 entry->abi_size = LLVMABISizeOfType(g->target_data_ref, entry->llvm_type);
7011 entry->abi_align = LLVMABIAlignmentOfType(g->target_data_ref, entry->llvm_type);
7012 buf_init_from_str(&entry->name, name);
7013 entry->data.floating.bit_count = bit_count;
7014
7015 entry->llvm_di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
7016 entry->size_in_bits, ZigLLVMEncoding_DW_ATE_float());
7017 *field = entry;
7018 g->primitive_type_table.put(&entry->name, entry);
7019}
7020
70007021static void define_builtin_types(CodeGen *g) {
70017022 {
70027023 // if this type is anywhere in the AST, we should never hit codegen.
70037024 ZigType *entry = new_type_table_entry(ZigTypeIdInvalid);
70047025 buf_init_from_str(&entry->name, "(invalid)");
7005 entry->zero_bits = true;
70067026 g->builtin_types.entry_invalid = entry;
70077027 }
70087028 {
70097029 ZigType *entry = new_type_table_entry(ZigTypeIdComptimeFloat);
70107030 buf_init_from_str(&entry->name, "comptime_float");
7011 entry->zero_bits = true;
70127031 g->builtin_types.entry_num_lit_float = entry;
70137032 g->primitive_type_table.put(&entry->name, entry);
70147033 }
70157034 {
70167035 ZigType *entry = new_type_table_entry(ZigTypeIdComptimeInt);
70177036 buf_init_from_str(&entry->name, "comptime_int");
7018 entry->zero_bits = true;
70197037 g->builtin_types.entry_num_lit_int = entry;
70207038 g->primitive_type_table.put(&entry->name, entry);
70217039 }
70227040 {
70237041 ZigType *entry = new_type_table_entry(ZigTypeIdEnumLiteral);
70247042 buf_init_from_str(&entry->name, "(enum literal)");
7025 entry->zero_bits = true;
70267043 g->builtin_types.entry_enum_literal = entry;
70277044 }
70287045 {
70297046 ZigType *entry = new_type_table_entry(ZigTypeIdUndefined);
70307047 buf_init_from_str(&entry->name, "(undefined)");
7031 entry->zero_bits = true;
70327048 g->builtin_types.entry_undef = entry;
70337049 }
70347050 {
70357051 ZigType *entry = new_type_table_entry(ZigTypeIdNull);
70367052 buf_init_from_str(&entry->name, "(null)");
7037 entry->zero_bits = true;
70387053 g->builtin_types.entry_null = entry;
70397054 }
70407055 {
70417056 ZigType *entry = new_type_table_entry(ZigTypeIdArgTuple);
70427057 buf_init_from_str(&entry->name, "(args)");
7043 entry->zero_bits = true;
70447058 g->builtin_types.entry_arg_tuple = entry;
70457059 }
70467060
......@@ -7050,14 +7064,15 @@ static void define_builtin_types(CodeGen *g) {
70507064 bool is_signed = info->is_signed;
70517065
70527066 ZigType *entry = new_type_table_entry(ZigTypeIdInt);
7053 entry->type_ref = LLVMIntType(size_in_bits);
7067 entry->llvm_type = LLVMIntType(size_in_bits);
7068 entry->size_in_bits = size_in_bits;
7069 entry->abi_size = LLVMABISizeOfType(g->target_data_ref, entry->llvm_type);
7070 entry->abi_align = LLVMABIAlignmentOfType(g->target_data_ref, entry->llvm_type);
70547071
70557072 buf_init_from_str(&entry->name, info->name);
70567073
7057 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, entry->type_ref);
7058 entry->di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
7059 debug_size_in_bits,
7060 is_signed ? ZigLLVMEncoding_DW_ATE_signed() : ZigLLVMEncoding_DW_ATE_unsigned());
7074 entry->llvm_di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
7075 size_in_bits, is_signed ? ZigLLVMEncoding_DW_ATE_signed() : ZigLLVMEncoding_DW_ATE_unsigned());
70617076 entry->data.integral.is_signed = is_signed;
70627077 entry->data.integral.bit_count = size_in_bits;
70637078 g->primitive_type_table.put(&entry->name, entry);
......@@ -7067,12 +7082,13 @@ static void define_builtin_types(CodeGen *g) {
70677082
70687083 {
70697084 ZigType *entry = new_type_table_entry(ZigTypeIdBool);
7070 entry->type_ref = LLVMInt1Type();
7085 entry->llvm_type = LLVMInt1Type();
7086 entry->size_in_bits = 1;
7087 entry->abi_size = LLVMABISizeOfType(g->target_data_ref, entry->llvm_type);
7088 entry->abi_align = LLVMABIAlignmentOfType(g->target_data_ref, entry->llvm_type);
70717089 buf_init_from_str(&entry->name, "bool");
7072 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, entry->type_ref);
7073 entry->di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
7074 debug_size_in_bits,
7075 ZigLLVMEncoding_DW_ATE_boolean());
7090 entry->llvm_di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
7091 entry->size_in_bits, ZigLLVMEncoding_DW_ATE_boolean());
70767092 g->builtin_types.entry_bool = entry;
70777093 g->primitive_type_table.put(&entry->name, entry);
70787094 }
......@@ -7081,7 +7097,10 @@ static void define_builtin_types(CodeGen *g) {
70817097 bool is_signed = is_signed_list[sign_i];
70827098
70837099 ZigType *entry = new_type_table_entry(ZigTypeIdInt);
7084 entry->type_ref = LLVMIntType(g->pointer_size_bytes * 8);
7100 entry->llvm_type = LLVMIntType(g->pointer_size_bytes * 8);
7101 entry->size_in_bits = g->pointer_size_bytes * 8;
7102 entry->abi_size = LLVMABISizeOfType(g->target_data_ref, entry->llvm_type);
7103 entry->abi_align = LLVMABIAlignmentOfType(g->target_data_ref, entry->llvm_type);
70857104
70867105 const char u_or_i = is_signed ? 'i' : 'u';
70877106 buf_resize(&entry->name, 0);
......@@ -7090,9 +7109,8 @@ static void define_builtin_types(CodeGen *g) {
70907109 entry->data.integral.is_signed = is_signed;
70917110 entry->data.integral.bit_count = g->pointer_size_bytes * 8;
70927111
7093 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, entry->type_ref);
7094 entry->di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
7095 debug_size_in_bits,
7112 entry->llvm_di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
7113 entry->size_in_bits,
70967114 is_signed ? ZigLLVMEncoding_DW_ATE_signed() : ZigLLVMEncoding_DW_ATE_unsigned());
70977115 g->primitive_type_table.put(&entry->name, entry);
70987116
......@@ -7103,23 +7121,6 @@ static void define_builtin_types(CodeGen *g) {
71037121 }
71047122 }
71057123
7106 auto add_fp_entry = [] (CodeGen *g,
7107 const char *name,
7108 uint32_t bit_count,
7109 LLVMTypeRef type_ref,
7110 ZigType **field) {
7111 ZigType *entry = new_type_table_entry(ZigTypeIdFloat);
7112 entry->type_ref = type_ref;
7113 buf_init_from_str(&entry->name, name);
7114 entry->data.floating.bit_count = bit_count;
7115
7116 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, entry->type_ref);
7117 entry->di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
7118 debug_size_in_bits,
7119 ZigLLVMEncoding_DW_ATE_float());
7120 *field = entry;
7121 g->primitive_type_table.put(&entry->name, entry);
7122 };
71237124 add_fp_entry(g, "f16", 16, LLVMHalfType(), &g->builtin_types.entry_f16);
71247125 add_fp_entry(g, "f32", 32, LLVMFloatType(), &g->builtin_types.entry_f32);
71257126 add_fp_entry(g, "f64", 64, LLVMDoubleType(), &g->builtin_types.entry_f64);
......@@ -7128,10 +7129,9 @@ static void define_builtin_types(CodeGen *g) {
71287129
71297130 {
71307131 ZigType *entry = new_type_table_entry(ZigTypeIdVoid);
7131 entry->type_ref = LLVMVoidType();
7132 entry->zero_bits = true;
7132 entry->llvm_type = LLVMVoidType();
71337133 buf_init_from_str(&entry->name, "void");
7134 entry->di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
7134 entry->llvm_di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
71357135 0,
71367136 ZigLLVMEncoding_DW_ATE_unsigned());
71377137 g->builtin_types.entry_void = entry;
......@@ -7139,17 +7139,15 @@ static void define_builtin_types(CodeGen *g) {
71397139 }
71407140 {
71417141 ZigType *entry = new_type_table_entry(ZigTypeIdUnreachable);
7142 entry->type_ref = LLVMVoidType();
7143 entry->zero_bits = true;
7142 entry->llvm_type = LLVMVoidType();
71447143 buf_init_from_str(&entry->name, "noreturn");
7145 entry->di_type = g->builtin_types.entry_void->di_type;
7144 entry->llvm_di_type = g->builtin_types.entry_void->llvm_di_type;
71467145 g->builtin_types.entry_unreachable = entry;
71477146 g->primitive_type_table.put(&entry->name, entry);
71487147 }
71497148 {
71507149 ZigType *entry = new_type_table_entry(ZigTypeIdMetaType);
71517150 buf_init_from_str(&entry->name, "type");
7152 entry->zero_bits = true;
71537151 g->builtin_types.entry_type = entry;
71547152 g->primitive_type_table.put(&entry->name, entry);
71557153 }
......@@ -7174,19 +7172,15 @@ static void define_builtin_types(CodeGen *g) {
71747172 buf_init_from_str(&entry->name, "anyerror");
71757173 entry->data.error_set.err_count = UINT32_MAX;
71767174
7177 // TODO allow overriding this type and keep track of max value and emit an
7178 // error if there are too many errors declared
7175 // TODO https://github.com/ziglang/zig/issues/786
71797176 g->err_tag_type = g->builtin_types.entry_u16;
71807177
7181 g->builtin_types.entry_global_error_set = entry;
7182 entry->type_ref = g->err_tag_type->type_ref;
7178 entry->size_in_bits = g->err_tag_type->size_in_bits;
7179 entry->abi_align = g->err_tag_type->abi_align;
7180 entry->abi_size = g->err_tag_type->abi_size;
71837181
7184 entry->di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
7185 ZigLLVMTag_DW_enumeration_type(), "anyerror",
7186 ZigLLVMCompileUnitToScope(g->compile_unit), nullptr, 0);
7182 g->builtin_types.entry_global_error_set = entry;
71877183
7188 // reserve index 0 to indicate no error
7189 g->err_enumerators.append(ZigLLVMCreateDebugEnumerator(g->dbuilder, "(none)", 0));
71907184 g->errors_by_index.append(nullptr);
71917185
71927186 g->primitive_type_table.put(&entry->name, entry);
......@@ -7194,6 +7188,9 @@ static void define_builtin_types(CodeGen *g) {
71947188 {
71957189 ZigType *entry = get_promise_type(g, nullptr);
71967190 g->primitive_type_table.put(&entry->name, entry);
7191 entry->size_in_bits = g->builtin_types.entry_usize->size_in_bits;
7192 entry->abi_align = g->builtin_types.entry_usize->abi_align;
7193 entry->abi_size = g->builtin_types.entry_usize->abi_size;
71977194 }
71987195}
71997196
......@@ -7749,8 +7746,15 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
77497746 buf_appendf(contents, "pub const valgrind_support = %s;\n", bool_to_str(want_valgrind_support(g)));
77507747 buf_appendf(contents, "pub const position_independent_code = %s;\n", bool_to_str(g->have_pic));
77517748
7752 buf_appendf(contents, "pub const __zig_test_fn_slice = {}; // overwritten later\n");
7753
7749 if (g->is_test_build) {
7750 buf_appendf(contents,
7751 "const TestFn = struct {\n"
7752 "name: []const u8,\n"
7753 "func: fn()anyerror!void,\n"
7754 "};\n"
7755 "pub const test_functions = {}; // overwritten later\n"
7756 );
7757 }
77547758
77557759 return contents;
77567760}
......@@ -8157,6 +8161,8 @@ static ZigPackage *create_panic_pkg(CodeGen *g) {
81578161}
81588162
81598163static void create_test_compile_var_and_add_test_runner(CodeGen *g) {
8164 Error err;
8165
81608166 assert(g->is_test_build);
81618167
81628168 if (g->test_fns.length == 0) {
......@@ -8164,14 +8170,13 @@ static void create_test_compile_var_and_add_test_runner(CodeGen *g) {
81648170 exit(0);
81658171 }
81668172
8167 ZigType *u8_ptr_type = get_pointer_to_type_extra(g, g->builtin_types.entry_u8, true, false,
8168 PtrLenUnknown, get_abi_alignment(g, g->builtin_types.entry_u8), 0, 0, false);
8169 ZigType *str_type = get_slice_type(g, u8_ptr_type);
81708173 ZigType *fn_type = get_test_fn_type(g);
81718174
8172 const char *field_names[] = { "name", "func", };
8173 ZigType *field_types[] = { str_type, fn_type, };
8174 ZigType *struct_type = get_struct_type(g, "ZigTestFn", field_names, field_types, 2);
8175 ConstExprValue *test_fn_type_val = get_builtin_value(g, "TestFn");
8176 assert(test_fn_type_val->type->id == ZigTypeIdMetaType);
8177 ZigType *struct_type = test_fn_type_val->data.x_type;
8178 if ((err = type_resolve(g, struct_type, ResolveStatusSizeKnown)))
8179 zig_unreachable();
81758180
81768181 ConstExprValue *test_fn_array = create_const_vals(1);
81778182 test_fn_array->type = get_array_type(g, struct_type, g->test_fns.length);
......@@ -8203,7 +8208,7 @@ static void create_test_compile_var_and_add_test_runner(CodeGen *g) {
82038208
82048209 ConstExprValue *test_fn_slice = create_const_slice(g, test_fn_array, 0, g->test_fns.length, true);
82058210
8206 update_compile_var(g, buf_create_from_str("__zig_test_fn_slice"), test_fn_slice);
8211 update_compile_var(g, buf_create_from_str("test_functions"), test_fn_slice);
82078212 g->test_runner_package = create_test_runner_pkg(g);
82088213 g->test_runner_import = add_special_code(g, g->test_runner_package, "test_runner.zig");
82098214}
src/ir.cpp+58-54
......@@ -6842,6 +6842,9 @@ static ZigType *get_error_set_union(CodeGen *g, ErrorTableEntry **errors, ZigTyp
68426842 assert(set2->id == ZigTypeIdErrorSet);
68436843
68446844 ZigType *err_set_type = new_type_table_entry(ZigTypeIdErrorSet);
6845 err_set_type->size_in_bits = g->builtin_types.entry_global_error_set->size_in_bits;
6846 err_set_type->abi_align = g->builtin_types.entry_global_error_set->abi_align;
6847 err_set_type->abi_size = g->builtin_types.entry_global_error_set->abi_size;
68456848 buf_resize(&err_set_type->name, 0);
68466849 buf_appendf(&err_set_type->name, "error{");
68476850
......@@ -6857,8 +6860,6 @@ static ZigType *get_error_set_union(CodeGen *g, ErrorTableEntry **errors, ZigTyp
68576860 }
68586861 }
68596862
6860 err_set_type->type_ref = g->builtin_types.entry_global_error_set->type_ref;
6861 err_set_type->di_type = g->builtin_types.entry_global_error_set->di_type;
68626863 err_set_type->data.error_set.err_count = count;
68636864 err_set_type->data.error_set.errors = allocate<ErrorTableEntry *>(count);
68646865
......@@ -6883,8 +6884,6 @@ static ZigType *get_error_set_union(CodeGen *g, ErrorTableEntry **errors, ZigTyp
68836884
68846885 buf_appendf(&err_set_type->name, "}");
68856886
6886 g->error_di_types.append(&err_set_type->di_type);
6887
68886887 return err_set_type;
68896888
68906889}
......@@ -6895,13 +6894,12 @@ static ZigType *make_err_set_with_one_item(CodeGen *g, Scope *parent_scope, AstN
68956894 ZigType *err_set_type = new_type_table_entry(ZigTypeIdErrorSet);
68966895 buf_resize(&err_set_type->name, 0);
68976896 buf_appendf(&err_set_type->name, "error{%s}", buf_ptr(&err_entry->name));
6898 err_set_type->type_ref = g->builtin_types.entry_global_error_set->type_ref;
6899 err_set_type->di_type = g->builtin_types.entry_global_error_set->di_type;
6897 err_set_type->size_in_bits = g->builtin_types.entry_global_error_set->size_in_bits;
6898 err_set_type->abi_align = g->builtin_types.entry_global_error_set->abi_align;
6899 err_set_type->abi_size = g->builtin_types.entry_global_error_set->abi_size;
69006900 err_set_type->data.error_set.err_count = 1;
69016901 err_set_type->data.error_set.errors = allocate<ErrorTableEntry *>(1);
69026902
6903 g->error_di_types.append(&err_set_type->di_type);
6904
69056903 err_set_type->data.error_set.errors[0] = err_entry;
69066904
69076905 return err_set_type;
......@@ -6917,9 +6915,9 @@ static IrInstruction *ir_gen_err_set_decl(IrBuilder *irb, Scope *parent_scope, A
69176915 ZigType *err_set_type = new_type_table_entry(ZigTypeIdErrorSet);
69186916 buf_init_from_buf(&err_set_type->name, type_name);
69196917 err_set_type->data.error_set.err_count = err_count;
6920 err_set_type->type_ref = irb->codegen->builtin_types.entry_global_error_set->type_ref;
6921 err_set_type->di_type = irb->codegen->builtin_types.entry_global_error_set->di_type;
6922 irb->codegen->error_di_types.append(&err_set_type->di_type);
6918 err_set_type->size_in_bits = irb->codegen->builtin_types.entry_global_error_set->size_in_bits;
6919 err_set_type->abi_align = irb->codegen->builtin_types.entry_global_error_set->abi_align;
6920 err_set_type->abi_size = irb->codegen->builtin_types.entry_global_error_set->abi_size;
69236921 err_set_type->data.error_set.errors = allocate<ErrorTableEntry *>(err_count);
69246922
69256923 ErrorTableEntry **errors = allocate<ErrorTableEntry *>(irb->codegen->errors_by_index.length + err_count);
......@@ -6940,8 +6938,6 @@ static IrInstruction *ir_gen_err_set_decl(IrBuilder *irb, Scope *parent_scope, A
69406938 assert((uint32_t)error_value_count < (((uint32_t)1) << (uint32_t)irb->codegen->err_tag_type->data.integral.bit_count));
69416939 err->value = error_value_count;
69426940 irb->codegen->errors_by_index.append(err);
6943 irb->codegen->err_enumerators.append(ZigLLVMCreateDebugEnumerator(irb->codegen->dbuilder,
6944 buf_ptr(err_name), error_value_count));
69456941 }
69466942 err_set_type->data.error_set.errors[i] = err;
69476943
......@@ -8947,16 +8943,14 @@ static ZigType *get_error_set_intersection(IrAnalyze *ira, ZigType *set1, ZigTyp
89478943 }
89488944 free(errors);
89498945
8950 err_set_type->type_ref = ira->codegen->builtin_types.entry_global_error_set->type_ref;
8951 err_set_type->di_type = ira->codegen->builtin_types.entry_global_error_set->di_type;
89528946 err_set_type->data.error_set.err_count = intersection_list.length;
89538947 err_set_type->data.error_set.errors = intersection_list.items;
8954 err_set_type->zero_bits = intersection_list.length == 0;
8948 err_set_type->size_in_bits = ira->codegen->builtin_types.entry_global_error_set->size_in_bits;
8949 err_set_type->abi_align = ira->codegen->builtin_types.entry_global_error_set->abi_align;
8950 err_set_type->abi_size = ira->codegen->builtin_types.entry_global_error_set->abi_size;
89558951
89568952 buf_appendf(&err_set_type->name, "}");
89578953
8958 ira->codegen->error_di_types.append(&err_set_type->di_type);
8959
89608954 return err_set_type;
89618955}
89628956
......@@ -13968,20 +13962,19 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod
1396813962}
1396913963
1397013964static ZigVar *get_fn_var_by_index(ZigFn *fn_entry, size_t index) {
13965 FnTypeParamInfo *src_param_info = &fn_entry->type_entry->data.fn.fn_type_id.param_info[index];
13966 if (!type_has_bits(src_param_info->type))
13967 return nullptr;
13968
1397113969 size_t next_var_i = 0;
13972 FnGenParamInfo *gen_param_info = fn_entry->type_entry->data.fn.gen_param_info;
13973 assert(gen_param_info != nullptr);
1397413970 for (size_t param_i = 0; param_i < index; param_i += 1) {
13975 FnGenParamInfo *info = &gen_param_info[param_i];
13976 if (info->gen_index == SIZE_MAX)
13971 FnTypeParamInfo *src_param_info = &fn_entry->type_entry->data.fn.fn_type_id.param_info[param_i];
13972 if (!type_has_bits(src_param_info->type)) {
1397713973 continue;
13974 }
1397813975
1397913976 next_var_i += 1;
1398013977 }
13981 FnGenParamInfo *info = &gen_param_info[index];
13982 if (info->gen_index == SIZE_MAX)
13983 return nullptr;
13984
1398513978 return fn_entry->variable_list.at(next_var_i);
1398613979}
1398713980
......@@ -14855,7 +14848,7 @@ static IrInstruction *ir_analyze_maybe(IrAnalyze *ira, IrInstructionUnOp *un_op_
1485514848 ZigType *type_entry = ir_resolve_type(ira, value);
1485614849 if (type_is_invalid(type_entry))
1485714850 return ira->codegen->invalid_instruction;
14858 if ((err = ensure_complete_type(ira->codegen, type_entry)))
14851 if ((err = type_resolve(ira->codegen, type_entry, ResolveStatusSizeKnown)))
1485914852 return ira->codegen->invalid_instruction;
1486014853
1486114854 switch (type_entry->id) {
......@@ -16051,8 +16044,6 @@ static IrInstruction *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstruc
1605116044 assert((uint32_t)error_value_count < (((uint32_t)1) << (uint32_t)ira->codegen->err_tag_type->data.integral.bit_count));
1605216045 err_entry->value = error_value_count;
1605316046 ira->codegen->errors_by_index.append(err_entry);
16054 ira->codegen->err_enumerators.append(ZigLLVMCreateDebugEnumerator(ira->codegen->dbuilder,
16055 buf_ptr(field_name), error_value_count));
1605616047 ira->codegen->error_table.put(field_name, err_entry);
1605716048 }
1605816049 if (err_entry->set_with_only_this_in_it == nullptr) {
......@@ -17519,6 +17510,8 @@ static IrInstruction *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstruc
1751917510static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,
1752017511 IrInstructionContainerInitList *instruction)
1752117512{
17513 Error err;
17514
1752217515 ZigType *container_type = ir_resolve_type(ira, instruction->container_type->child);
1752317516 if (type_is_invalid(container_type))
1752417517 return ira->codegen->invalid_instruction;
......@@ -17547,6 +17540,10 @@ static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,
1754717540 child_type = pointer_type->data.pointer.child_type;
1754817541 }
1754917542
17543 if ((err = type_resolve(ira->codegen, child_type, ResolveStatusSizeKnown))) {
17544 return ira->codegen->invalid_instruction;
17545 }
17546
1755017547 ZigType *fixed_size_array_type = get_array_type(ira->codegen, child_type, elem_count);
1755117548
1755217549 ConstExprValue const_val = {};
......@@ -17873,7 +17870,7 @@ static TypeStructField *validate_byte_offset(IrAnalyze *ira,
1787317870 return nullptr;
1787417871 }
1787517872
17876 *byte_offset = LLVMOffsetOfElement(ira->codegen->target_data_ref, container_type->type_ref, field->gen_index);
17873 *byte_offset = field->offset;
1787717874 return field;
1787817875}
1787917876
......@@ -17929,10 +17926,11 @@ static ZigType *ir_type_info_get_type(IrAnalyze *ira, const char *type_name, Zig
1792917926 Error err;
1793017927 ConstExprValue *type_info_var = get_builtin_value(ira->codegen, "TypeInfo");
1793117928 assert(type_info_var->type->id == ZigTypeIdMetaType);
17932 assertNoError(ensure_complete_type(ira->codegen, type_info_var->data.x_type));
17933
1793417929 ZigType *type_info_type = type_info_var->data.x_type;
1793517930 assert(type_info_type->id == ZigTypeIdUnion);
17931 if ((err = type_resolve(ira->codegen, type_info_type, ResolveStatusSizeKnown))) {
17932 zig_unreachable();
17933 }
1793617934
1793717935 if (type_name == nullptr && root == nullptr)
1793817936 return type_info_type;
......@@ -17966,7 +17964,7 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, IrInstruction *source_instr,
1796617964{
1796717965 Error err;
1796817966 ZigType *type_info_definition_type = ir_type_info_get_type(ira, "Definition", nullptr);
17969 if ((err = ensure_complete_type(ira->codegen, type_info_definition_type)))
17967 if ((err = type_resolve(ira->codegen, type_info_definition_type, ResolveStatusSizeKnown)))
1797017968 return err;
1797117969
1797217970 ensure_field_index(type_info_definition_type, "name", 0);
......@@ -18486,6 +18484,9 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
1848618484 ensure_field_index(result->type, "fields", 2);
1848718485
1848818486 ZigType *type_info_enum_field_type = ir_type_info_get_type(ira, "EnumField", nullptr);
18487 if ((err = type_resolve(ira->codegen, type_info_enum_field_type, ResolveStatusSizeKnown))) {
18488 zig_unreachable();
18489 }
1848918490 uint32_t enum_field_count = type_entry->data.enumeration.src_field_count;
1849018491
1849118492 ConstExprValue *enum_field_array = create_const_vals(1);
......@@ -18529,6 +18530,9 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
1852918530 result->data.x_optional = nullptr;
1853018531 break;
1853118532 }
18533 if ((err = type_resolve(ira->codegen, type_info_error_type, ResolveStatusSizeKnown))) {
18534 zig_unreachable();
18535 }
1853218536 ConstExprValue *slice_val = create_const_vals(1);
1853318537 result->data.x_optional = slice_val;
1853418538
......@@ -18625,6 +18629,8 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
1862518629 ensure_field_index(result->type, "fields", 2);
1862618630
1862718631 ZigType *type_info_union_field_type = ir_type_info_get_type(ira, "UnionField", nullptr);
18632 if ((err = type_resolve(ira->codegen, type_info_union_field_type, ResolveStatusSizeKnown)))
18633 zig_unreachable();
1862818634 uint32_t union_field_count = type_entry->data.unionation.src_field_count;
1862918635
1863018636 ConstExprValue *union_field_array = create_const_vals(1);
......@@ -18702,6 +18708,9 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
1870218708 ensure_field_index(result->type, "fields", 1);
1870318709
1870418710 ZigType *type_info_struct_field_type = ir_type_info_get_type(ira, "StructField", nullptr);
18711 if ((err = type_resolve(ira->codegen, type_info_struct_field_type, ResolveStatusSizeKnown))) {
18712 zig_unreachable();
18713 }
1870518714 uint32_t struct_field_count = type_entry->data.structure.src_field_count;
1870618715
1870718716 ConstExprValue *struct_field_array = create_const_vals(1);
......@@ -18726,7 +18735,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
1872618735 if (!type_has_bits(struct_field->type_entry)) {
1872718736 inner_fields[1].data.x_optional = nullptr;
1872818737 } else {
18729 size_t byte_offset = LLVMOffsetOfElement(ira->codegen->target_data_ref, type_entry->type_ref, struct_field->gen_index);
18738 size_t byte_offset = struct_field->offset;
1873018739 inner_fields[1].data.x_optional = create_const_vals(1);
1873118740 inner_fields[1].data.x_optional->special = ConstValSpecialStatic;
1873218741 inner_fields[1].data.x_optional->type = ira->codegen->builtin_types.entry_num_lit_int;
......@@ -18809,6 +18818,9 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
1880918818 }
1881018819 // args: []TypeInfo.FnArg
1881118820 ZigType *type_info_fn_arg_type = ir_type_info_get_type(ira, "FnArg", nullptr);
18821 if ((err = type_resolve(ira->codegen, type_info_fn_arg_type, ResolveStatusSizeKnown))) {
18822 zig_unreachable();
18823 }
1881218824 size_t fn_arg_count = type_entry->data.fn.fn_type_id.param_count -
1881318825 (is_varargs && type_entry->data.fn.fn_type_id.cc != CallingConventionC);
1881418826
......@@ -21425,12 +21437,11 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue
2142521437 case ContainerLayoutExtern: {
2142621438 size_t src_field_count = val->type->data.structure.src_field_count;
2142721439 for (size_t field_i = 0; field_i < src_field_count; field_i += 1) {
21428 TypeStructField *type_field = &val->type->data.structure.fields[field_i];
21429 if (type_field->gen_index == SIZE_MAX)
21440 TypeStructField *struct_field = &val->type->data.structure.fields[field_i];
21441 if (struct_field->gen_index == SIZE_MAX)
2143021442 continue;
2143121443 ConstExprValue *field_val = &val->data.x_struct.fields[field_i];
21432 size_t offset = LLVMOffsetOfElement(codegen->target_data_ref, val->type->type_ref,
21433 type_field->gen_index);
21444 size_t offset = struct_field->offset;
2143421445 buf_write_value_bytes(codegen, buf + offset, field_val);
2143521446 }
2143621447 return;
......@@ -21446,10 +21457,7 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue
2144621457 size_t child_buf_len = 16;
2144721458 uint8_t *child_buf = child_buf_prealloc;
2144821459 while (gen_i < gen_field_count) {
21449 LLVMTypeRef gen_llvm_int_type = LLVMStructGetTypeAtIndex(val->type->type_ref,
21450 (unsigned)gen_i);
21451 size_t big_int_bit_count = LLVMGetIntTypeWidth(gen_llvm_int_type);
21452 size_t big_int_byte_count = big_int_bit_count / 8;
21460 size_t big_int_byte_count = val->type->data.structure.host_int_bytes[gen_i];
2145321461 if (big_int_byte_count > child_buf_len) {
2145421462 child_buf = allocate_nonzero<uint8_t>(big_int_byte_count);
2145521463 child_buf_len = big_int_byte_count;
......@@ -21485,7 +21493,7 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue
2148521493 }
2148621494 src_i += 1;
2148721495 }
21488 bigint_write_twos_complement(&big_int, buf + offset, big_int_bit_count, is_big_endian);
21496 bigint_write_twos_complement(&big_int, buf + offset, big_int_byte_count * 8, is_big_endian);
2148921497 offset += big_int_byte_count;
2149021498 gen_i += 1;
2149121499 }
......@@ -21606,12 +21614,11 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou
2160621614 for (size_t field_i = 0; field_i < src_field_count; field_i += 1) {
2160721615 ConstExprValue *field_val = &val->data.x_struct.fields[field_i];
2160821616 field_val->special = ConstValSpecialStatic;
21609 TypeStructField *type_field = &val->type->data.structure.fields[field_i];
21610 field_val->type = type_field->type_entry;
21611 if (type_field->gen_index == SIZE_MAX)
21617 TypeStructField *struct_field = &val->type->data.structure.fields[field_i];
21618 field_val->type = struct_field->type_entry;
21619 if (struct_field->gen_index == SIZE_MAX)
2161221620 continue;
21613 size_t offset = LLVMOffsetOfElement(codegen->target_data_ref, val->type->type_ref,
21614 type_field->gen_index);
21621 size_t offset = struct_field->offset;
2161521622 uint8_t *new_buf = buf + offset;
2161621623 if ((err = buf_read_value_bytes(ira, codegen, source_node, new_buf, field_val)))
2161721624 return err;
......@@ -21630,16 +21637,13 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou
2163021637 size_t child_buf_len = 16;
2163121638 uint8_t *child_buf = child_buf_prealloc;
2163221639 while (gen_i < gen_field_count) {
21633 LLVMTypeRef gen_llvm_int_type = LLVMStructGetTypeAtIndex(val->type->type_ref,
21634 (unsigned)gen_i);
21635 size_t big_int_bit_count = LLVMGetIntTypeWidth(gen_llvm_int_type);
21636 size_t big_int_byte_count = big_int_bit_count / 8;
21640 size_t big_int_byte_count = val->type->data.structure.host_int_bytes[gen_i];
2163721641 if (big_int_byte_count > child_buf_len) {
2163821642 child_buf = allocate_nonzero<uint8_t>(big_int_byte_count);
2163921643 child_buf_len = big_int_byte_count;
2164021644 }
2164121645 BigInt big_int;
21642 bigint_read_twos_complement(&big_int, buf + offset, big_int_bit_count, is_big_endian, false);
21646 bigint_read_twos_complement(&big_int, buf + offset, big_int_byte_count * 8, is_big_endian, false);
2164321647 while (src_i < src_field_count) {
2164421648 TypeStructField *field = &val->type->data.structure.fields[src_i];
2164521649 assert(field->gen_index != SIZE_MAX);
......@@ -21662,7 +21666,7 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou
2166221666 big_int = tmp;
2166321667 }
2166421668
21665 bigint_write_twos_complement(&child_val, child_buf, big_int_bit_count, is_big_endian);
21669 bigint_write_twos_complement(&child_val, child_buf, big_int_byte_count * 8, is_big_endian);
2166621670 if ((err = buf_read_value_bytes(ira, codegen, source_node, child_buf, field_val))) {
2166721671 return err;
2166821672 }
src/zig_llvm.cpp+1
......@@ -548,6 +548,7 @@ ZigLLVMDILocalVariable *ZigLLVMCreateParameterVariable(ZigLLVMDIBuilder *dbuilde
548548 ZigLLVMDIType *type, bool always_preserve, unsigned flags, unsigned arg_no)
549549{
550550 assert(flags == 0);
551 assert(arg_no != 0);
551552 DILocalVariable *result = reinterpret_cast<DIBuilder*>(dbuilder)->createParameterVariable(
552553 reinterpret_cast<DIScope*>(scope),
553554 name,
std/special/test_runner.zig+1-1
......@@ -1,7 +1,7 @@
11const std = @import("std");
22const io = std.io;
33const builtin = @import("builtin");
4const test_fn_list = builtin.__zig_test_fn_slice;
4const test_fn_list = builtin.test_functions;
55const warn = std.debug.warn;
66
77pub fn main() !void {
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);