authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-29 18:32:16-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-02 18:31:17-04:00
logee5064c053526e9e6a0a94d835cd334eea2e5823
tree414b87cd662fba1f834904a6f28e8d1784e4427d
parent2f96c55095514de35a25ec2cf84a79e6c5e3a646
signaturelock-open Commit is signed but in an unrecognized format.

decouple llvm types from zig types

Not tested yet, but it builds. This closes #761, and lays the groundwork for fixing the remaining false positive "foo depends on itself" bugs, such as #624. It also lays the groundwork for implementing ability to specify alignment of fields (#1512).

5 files changed, 1686 insertions(+), 1626 deletions(-)

src/all_types.hpp+30-33
......@@ -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 {
......@@ -1101,14 +1103,13 @@ struct ZigTypeStruct {
11011103 AstNode *decl_node;
11021104 TypeStructField *fields;
11031105 ScopeDecls *decls_scope;
1104 uint64_t size_bytes;
11051106 HashMap<Buf *, TypeStructField *, buf_hash, buf_eql_buf> fields_by_name;
11061107 RootStruct *root_struct;
1108 uint32_t *host_int_bytes; // available for packed structs, indexed by gen_index
11071109
11081110 uint32_t src_field_count;
11091111 uint32_t gen_field_count;
11101112
1111 uint32_t abi_alignment; // known after ResolveStatusAlignmentKnown
11121113 ContainerLayout layout;
11131114 ResolveStatus resolve_status;
11141115
......@@ -1164,39 +1165,28 @@ bool type_ptr_eql(const ZigType *a, const ZigType *b);
11641165
11651166struct ZigTypeUnion {
11661167 AstNode *decl_node;
1167 ContainerLayout layout;
1168 uint32_t src_field_count;
1169 uint32_t gen_field_count;
11701168 TypeUnionField *fields;
1171 bool is_invalid; // true if any fields are invalid
1169 ScopeDecls *decls_scope;
1170 HashMap<Buf *, TypeUnionField *, buf_hash, buf_eql_buf> fields_by_name;
11721171 ZigType *tag_type; // always an enum or null
1173 LLVMTypeRef union_type_ref;
1172 LLVMTypeRef union_llvm_type;
1173 ZigType *most_aligned_union_member;
1174 size_t gen_union_index;
1175 size_t gen_tag_index;
1176 size_t union_abi_size;
11741177
1175 ScopeDecls *decls_scope;
1178 uint32_t src_field_count;
1179 uint32_t gen_field_count;
11761180
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;
1181 ContainerLayout layout;
1182 ResolveStatus resolve_status;
11821183
1184 bool have_explicit_tag_type;
1185 bool resolve_loop_flag; // set this flag temporarily to detect infinite loops
1186 bool reported_infinite_err;
11831187 // whether any of the fields require comptime
11841188 // the value is not valid until zero_bits_known == true
11851189 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;
12001190};
12011191
12021192struct FnGenParamInfo {
......@@ -1277,8 +1267,11 @@ struct ZigType {
12771267 ZigTypeId id;
12781268 Buf name;
12791269
1280 LLVMTypeRef type_ref;
1281 ZigLLVMDIType *di_type;
1270 // These are not supposed to be accessed directly. They're
1271 // null during semantic analysis, memoized with get_llvm_type
1272 // and get_llvm_di_type
1273 LLVMTypeRef llvm_type;
1274 ZigLLVMDIType *llvm_di_type;
12821275
12831276 union {
12841277 ZigTypePointer pointer;
......@@ -1308,8 +1301,14 @@ struct ZigType {
13081301 ConstExprValue *cached_const_name_val;
13091302
13101303 OnePossibleValue one_possible_value;
1304 // Known after ResolveStatusAlignmentKnown.
1305 uint32_t abi_align;
1306 // The offset in bytes between consecutive array elements of this type. Known
1307 // after ResolveStatusSizeKnown.
1308 size_t abi_size;
1309 // Number of bits of information in this type. Known after ResolveStatusSizeKnown.
1310 size_t size_in_bits;
13111311
1312 bool zero_bits; // this is denormalized data
13131312 bool gen_h_loop_flag;
13141313};
13151314
......@@ -1700,11 +1699,9 @@ struct CodeGen {
17001699 ZigList<AstNode *> use_queue;
17011700 size_t use_queue_index;
17021701 ZigList<TimeEvent> timing_events;
1703 ZigList<ZigLLVMDIType **> error_di_types;
17041702 ZigList<AstNode *> tld_ref_source_node_stack;
17051703 ZigList<ZigFn *> inline_fns;
17061704 ZigList<ZigFn *> test_fns;
1707 ZigList<ZigLLVMDIEnumerator *> err_enumerators;
17081705 ZigList<ErrorTableEntry *> errors_by_index;
17091706 ZigList<CacheHash *> caches_to_release;
17101707 size_t largest_err_name_len;
src/analyze.cpp+1307-1219
......@@ -26,6 +26,7 @@ static Error ATTRIBUTE_MUST_USE resolve_struct_zero_bits(CodeGen *g, ZigType *st
2626static Error ATTRIBUTE_MUST_USE resolve_struct_alignment(CodeGen *g, ZigType *struct_type);
2727static Error ATTRIBUTE_MUST_USE resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type);
2828static Error ATTRIBUTE_MUST_USE resolve_union_zero_bits(CodeGen *g, ZigType *union_type);
29static Error ATTRIBUTE_MUST_USE resolve_union_alignment(CodeGen *g, ZigType *union_type);
2930static void analyze_fn_body(CodeGen *g, ZigFn *fn_table_entry);
3031
3132static bool is_top_level_struct(ZigType *import) {
......@@ -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:
......@@ -278,20 +281,6 @@ bool type_is_resolved(ZigType *type_entry, ResolveStatus status) {
278281 return type_entry->data.enumeration.complete;
279282 }
280283 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;
293 }
294 zig_unreachable();
295284 case ZigTypeIdOpaque:
296285 return status < ResolveStatusSizeKnown;
297286 case ZigTypeIdMetaType:
......@@ -325,49 +314,18 @@ bool type_is_complete(ZigType *type_entry) {
325314}
326315
327316uint64_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);
317 assert(type_is_resolved(type_entry, ResolveStatusSizeKnown));
318 return type_entry->abi_size;
347319}
348320
349321uint64_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 }
322 assert(type_is_resolved(type_entry, ResolveStatusSizeKnown));
323 return type_entry->size_in_bits;
324}
369325
370 return LLVMSizeOfTypeInBits(g->target_data_ref, type_entry->type_ref);
326uint32_t get_abi_alignment(CodeGen *g, ZigType *type_entry) {
327 assert(type_is_resolved(type_entry, ResolveStatusAlignmentKnown));
328 return type_entry->abi_align;
371329}
372330
373331static bool is_slice(ZigType *type) {
......@@ -385,16 +343,14 @@ ZigType *get_promise_type(CodeGen *g, ZigType *result_type) {
385343 return g->builtin_types.entry_promise;
386344 }
387345
388 ZigType *u8_ptr_type = get_pointer_to_type(g, g->builtin_types.entry_u8, false);
389346 ZigType *entry = new_type_table_entry(ZigTypeIdPromise);
390 entry->type_ref = u8_ptr_type->type_ref;
391 entry->zero_bits = false;
347 entry->abi_size = g->pointer_size_bytes;
348 entry->size_in_bits = g->pointer_size_bytes * 8;
392349 entry->data.promise.result_type = result_type;
393350 buf_init_from_str(&entry->name, "promise");
394351 if (result_type != nullptr) {
395352 buf_appendf(&entry->name, "->%s", buf_ptr(&result_type->name));
396353 }
397 entry->di_type = u8_ptr_type->di_type;
398354
399355 if (result_type != nullptr) {
400356 result_type->promise_parent = entry;
......@@ -496,36 +452,15 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons
496452
497453 assert(child_type->id != ZigTypeIdInvalid);
498454
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 }
455 if (type_has_bits(child_type)) {
456 entry->abi_size = g->builtin_types.entry_usize->abi_size;
457 entry->size_in_bits = g->builtin_types.entry_usize->size_in_bits;
458 entry->abi_align = g->builtin_types.entry_usize->abi_align;
526459 } else {
527460 assert(byte_alignment == 0);
528 entry->di_type = g->builtin_types.entry_void->di_type;
461 entry->abi_size = 0;
462 entry->size_in_bits = 0;
463 entry->abi_align = 0;
529464 }
530465
531466 entry->data.pointer.ptr_len = ptr_len;
......@@ -586,89 +521,61 @@ ZigType *get_promise_frame_type(CodeGen *g, ZigType *return_type) {
586521}
587522
588523ZigType *get_optional_type(CodeGen *g, ZigType *child_type) {
589 if (child_type->optional_parent) {
590 ZigType *entry = child_type->optional_parent;
591 return entry;
524 if (child_type->optional_parent != nullptr) {
525 return child_type->optional_parent;
526 }
527
528 assert(type_is_resolved(child_type, ResolveStatusSizeKnown));
529
530 ZigType *entry = new_type_table_entry(ZigTypeIdOptional);
531
532 buf_resize(&entry->name, 0);
533 buf_appendf(&entry->name, "?%s", buf_ptr(&child_type->name));
534
535 if (!type_has_bits(child_type)) {
536 entry->size_in_bits = g->builtin_types.entry_bool->size_in_bits;
537 entry->abi_size = g->builtin_types.entry_bool->abi_size;
538 entry->abi_align = g->builtin_types.entry_bool->abi_align;
539 } else if (type_is_nonnull_ptr(child_type) || child_type->id == ZigTypeIdErrorSet) {
540 // This is an optimization but also is necessary for calling C
541 // functions where all pointers are optional pointers.
542 // Function types are technically pointers.
543 entry->size_in_bits = child_type->size_in_bits;
544 entry->abi_size = child_type->abi_size;
545 entry->abi_align = child_type->abi_align;
592546 } 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, "");
662
663 ZigLLVMReplaceTemporary(g->dbuilder, entry->di_type, replacement_di_type);
664 entry->di_type = replacement_di_type;
665 }
666
667 entry->data.maybe.child_type = child_type;
668
669 child_type->optional_parent = entry;
670 return entry;
547 // This value only matters if the type is legal in a packed struct, which is not
548 // true for optional types which did not fit the above 2 categories (zero bit child type,
549 // or nonnull ptr child type, or error set child type).
550 entry->size_in_bits = child_type->size_in_bits + 1;
551
552 // We're going to make a struct with the child type as the first field,
553 // and a bool as the second. Since the child type's abi alignment is guaranteed
554 // to be >= the bool's abi size (1 byte), the added size is exactly equal to the
555 // child type's ABI alignment.
556 assert(child_type->abi_align >= g->builtin_types.entry_bool->abi_size);
557 entry->abi_align = child_type->abi_align;
558 entry->abi_size = child_type->abi_size + child_type->abi_align;
671559 }
560
561 entry->data.maybe.child_type = child_type;
562
563 child_type->optional_parent = entry;
564 return entry;
565}
566
567static size_t align_forward(size_t addr, size_t alignment) {
568 return (addr + alignment - 1) & ~(alignment - 1);
569}
570
571static size_t next_field_offset(size_t offset, size_t align_from_zero, size_t field_size, size_t field_align) {
572 BREAKPOINT; // TODO test this
573 // Convert offset to a pretend address which has the specified alignment.
574 size_t addr = offset + align_from_zero;
575 // March the address forward to respect the field alignment.
576 size_t aligned_addr = align_forward(addr + field_size, field_align);
577 // Convert back from pretend address to offset.
578 return aligned_addr - align_from_zero;
672579}
673580
674581ZigType *get_error_union_type(CodeGen *g, ZigType *err_set_type, ZigType *payload_type) {
......@@ -686,8 +593,7 @@ ZigType *get_error_union_type(CodeGen *g, ZigType *err_set_type, ZigType *payloa
686593 }
687594
688595 ZigType *entry = new_type_table_entry(ZigTypeIdErrorUnion);
689 assert(payload_type->di_type);
690 assert(type_is_complete(payload_type));
596 assert(type_is_resolved(payload_type, ResolveStatusSizeKnown));
691597
692598 buf_resize(&entry->name, 0);
693599 buf_appendf(&entry->name, "%s!%s", buf_ptr(&err_set_type->name), buf_ptr(&payload_type->name));
......@@ -697,68 +603,29 @@ ZigType *get_error_union_type(CodeGen *g, ZigType *err_set_type, ZigType *payloa
697603
698604 if (!type_has_bits(payload_type)) {
699605 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);
606 entry->size_in_bits = err_set_type->size_in_bits;
607 entry->abi_size = err_set_type->abi_size;
608 entry->abi_align = err_set_type->abi_align;
703609 } else {
704 entry->zero_bits = true;
705 entry->di_type = g->builtin_types.entry_void->di_type;
610 entry->size_in_bits = 0;
611 entry->abi_size = 0;
612 entry->abi_align = 0;
706613 }
707614 } else if (!type_has_bits(err_set_type)) {
708 entry->type_ref = payload_type->type_ref;
709 entry->di_type = payload_type->di_type;
615 entry->size_in_bits = payload_type->size_in_bits;
616 entry->abi_size = payload_type->abi_size;
617 entry->abi_align = payload_type->abi_align;
710618 } 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;
619 entry->abi_align = max(err_set_type->abi_align, payload_type->abi_align);
620 size_t field_sizes[2];
621 size_t field_aligns[2];
622 field_sizes[err_union_err_index] = err_set_type->abi_size;
623 field_aligns[err_union_err_index] = err_set_type->abi_align;
624 field_sizes[err_union_payload_index] = payload_type->abi_size;
625 field_aligns[err_union_payload_index] = payload_type->abi_align;
626 size_t field2_offset = next_field_offset(0, entry->abi_align, field_sizes[0], field_aligns[0]);
627 entry->abi_size = next_field_offset(field2_offset, entry->abi_align, field_sizes[1], field_aligns[1]);
628 entry->size_in_bits = entry->abi_size * 8;
762629 }
763630
764631 g->type_table.put(type_id, entry);
......@@ -772,31 +639,20 @@ ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size) {
772639 type_id.data.array.size = array_size;
773640 auto existing_entry = g->type_table.maybe_get(type_id);
774641 if (existing_entry) {
775 ZigType *entry = existing_entry->value;
776 return entry;
642 return existing_entry->value;
777643 }
778644
779 assertNoError(ensure_complete_type(g, child_type));
645 assert(type_is_resolved(child_type, ResolveStatusSizeKnown));
780646
781647 ZigType *entry = new_type_table_entry(ZigTypeIdArray);
782 entry->zero_bits = (array_size == 0) || child_type->zero_bits;
783648
784649 buf_resize(&entry->name, 0);
785650 buf_appendf(&entry->name, "[%" ZIG_PRI_u64 "]%s", array_size, buf_ptr(&child_type->name));
786651
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);
652 entry->size_in_bits = child_type->size_in_bits * array_size;
653 entry->abi_align = child_type->abi_align;
654 entry->abi_size = child_type->abi_size * array_size;
796655
797 entry->di_type = ZigLLVMCreateDebugArrayType(g->dbuilder, debug_size_in_bits,
798 debug_align_in_bits, child_type->di_type, (int)array_size);
799 }
800656 entry->data.array.child_type = child_type;
801657 entry->data.array.len = array_size;
802658
......@@ -804,11 +660,27 @@ ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size) {
804660 return entry;
805661}
806662
807static void slice_type_common_init(CodeGen *g, ZigType *pointer_type, ZigType *entry) {
663ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type) {
664 assert(ptr_type->id == ZigTypeIdPointer);
665 assert(ptr_type->data.pointer.ptr_len == PtrLenUnknown);
666
667 ZigType **parent_pointer = &ptr_type->data.pointer.slice_parent;
668 if (*parent_pointer) {
669 return *parent_pointer;
670 }
671
672 ZigType *entry = new_type_table_entry(ZigTypeIdStruct);
673
674 // replace the & with [] to go from a ptr type name to a slice type name
675 buf_resize(&entry->name, 0);
676 size_t name_offset = (ptr_type->data.pointer.ptr_len == PtrLenSingle) ? 1 : 3;
677 buf_appendf(&entry->name, "[]%s", buf_ptr(&ptr_type->name) + name_offset);
678
808679 unsigned element_count = 2;
809680 Buf *ptr_field_name = buf_create_from_str("ptr");
810681 Buf *len_field_name = buf_create_from_str("len");
811682
683 entry->data.structure.resolve_status = ResolveStatusSizeKnown;
812684 entry->data.structure.layout = ContainerLayoutAuto;
813685 entry->data.structure.is_slice = true;
814686 entry->data.structure.src_field_count = element_count;
......@@ -816,7 +688,7 @@ static void slice_type_common_init(CodeGen *g, ZigType *pointer_type, ZigType *e
816688 entry->data.structure.fields = allocate<TypeStructField>(element_count);
817689 entry->data.structure.fields_by_name.init(element_count);
818690 entry->data.structure.fields[slice_ptr_index].name = ptr_field_name;
819 entry->data.structure.fields[slice_ptr_index].type_entry = pointer_type;
691 entry->data.structure.fields[slice_ptr_index].type_entry = ptr_type;
820692 entry->data.structure.fields[slice_ptr_index].src_index = slice_ptr_index;
821693 entry->data.structure.fields[slice_ptr_index].gen_index = 0;
822694 entry->data.structure.fields[slice_len_index].name = len_field_name;
......@@ -827,28 +699,11 @@ static void slice_type_common_init(CodeGen *g, ZigType *pointer_type, ZigType *e
827699 entry->data.structure.fields_by_name.put(ptr_field_name, &entry->data.structure.fields[slice_ptr_index]);
828700 entry->data.structure.fields_by_name.put(len_field_name, &entry->data.structure.fields[slice_len_index]);
829701
830 if (!type_has_bits(pointer_type->data.pointer.child_type)) {
702 if (!type_has_bits(ptr_type)) {
831703 entry->data.structure.gen_field_count = 1;
832704 entry->data.structure.fields[slice_ptr_index].gen_index = SIZE_MAX;
833705 entry->data.structure.fields[slice_len_index].gen_index = 0;
834706 }
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);
852707
853708 ZigType *child_type = ptr_type->data.pointer.child_type;
854709 if (ptr_type->data.pointer.is_const || ptr_type->data.pointer.is_volatile ||
......@@ -858,134 +713,24 @@ ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type) {
858713 PtrLenUnknown, 0, 0, 0, false);
859714 ZigType *peer_slice_type = get_slice_type(g, peer_ptr_type);
860715
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;
716 entry->size_in_bits = peer_slice_type->size_in_bits;
717 entry->abi_size = peer_slice_type->abi_size;
718 entry->abi_align = peer_slice_type->abi_align;
867719
868720 *parent_pointer = entry;
869721 return entry;
870722 }
871723
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 }
724 if (type_has_bits(ptr_type)) {
725 entry->size_in_bits = ptr_type->size_in_bits + g->builtin_types.entry_usize->size_in_bits;
726 entry->abi_size = ptr_type->abi_size + g->builtin_types.entry_usize->abi_size;
727 entry->abi_align = ptr_type->abi_align;
728 } else {
729 entry->size_in_bits = g->builtin_types.entry_usize->size_in_bits;
730 entry->abi_size = g->builtin_types.entry_usize->abi_size;
731 entry->abi_align = g->builtin_types.entry_usize->abi_align;
984732 }
985733
986
987 entry->data.structure.resolve_status = ResolveStatusSizeKnown;
988
989734 *parent_pointer = entry;
990735 return entry;
991736}
......@@ -998,15 +743,20 @@ ZigType *get_opaque_type(CodeGen *g, Scope *scope, AstNode *source_node, const c
998743 ZigType *import = scope ? get_scope_import(scope) : nullptr;
999744 unsigned line = source_node ? (unsigned)(source_node->line + 1) : 0;
1000745
1001 entry->type_ref = LLVMInt8Type();
1002 entry->di_type = ZigLLVMCreateDebugForwardDeclType(g->dbuilder,
746 entry->llvm_type = LLVMInt8Type();
747 entry->llvm_di_type = ZigLLVMCreateDebugForwardDeclType(g->dbuilder,
1003748 ZigLLVMTag_DW_structure_type(), full_name,
1004749 import ? ZigLLVMFileToScope(import->data.structure.root_struct->di_file) : nullptr,
1005750 import ? import->data.structure.root_struct->di_file : nullptr,
1006751 line);
1007 entry->zero_bits = false;
1008752 entry->data.opaque.bare_name = bare_name;
1009753
754 // The actual size is unknown, but the value must not be 0 because that
755 // is how type_has_bits is determined.
756 entry->abi_size = SIZE_MAX;
757 entry->size_in_bits = SIZE_MAX;
758 entry->abi_align = 1;
759
1010760 return entry;
1011761}
1012762
......@@ -1018,7 +768,9 @@ ZigType *get_bound_fn_type(CodeGen *g, ZigFn *fn_entry) {
1018768
1019769 ZigType *bound_fn_type = new_type_table_entry(ZigTypeIdBoundFn);
1020770 bound_fn_type->data.bound_fn.fn_type = fn_type;
1021 bound_fn_type->zero_bits = true;
771 bound_fn_type->abi_size = 0;
772 bound_fn_type->size_in_bits = 0;
773 bound_fn_type->abi_align = 0;
1022774
1023775 buf_resize(&bound_fn_type->name, 0);
1024776 buf_appendf(&bound_fn_type->name, "(bound %s)", buf_ptr(&fn_type->name));
......@@ -1114,8 +866,6 @@ ZigType *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
1114866 ZigType *fn_type = new_type_table_entry(ZigTypeIdFn);
1115867 fn_type->data.fn.fn_type_id = *fn_type_id;
1116868
1117 bool skip_debug_info = false;
1118
1119869 // populate the name of the type
1120870 buf_resize(&fn_type->name, 0);
1121871 if (fn_type->data.fn.fn_type_id.cc == CallingConventionAsync) {
......@@ -1133,8 +883,6 @@ ZigType *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
1133883 const char *comma = (i == 0) ? "" : ", ";
1134884 const char *noalias_str = param_info->is_noalias ? "noalias " : "";
1135885 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;
1138886 }
1139887
1140888 if (fn_type_id->is_var_args) {
......@@ -1146,116 +894,11 @@ ZigType *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
1146894 buf_appendf(&fn_type->name, " align(%" PRIu32 ")", fn_type_id->alignment);
1147895 }
1148896 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 }
1221
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 }
1237897
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 }
898 fn_type->size_in_bits = g->builtin_types.entry_usize->size_in_bits;
899 fn_type->abi_size = g->builtin_types.entry_usize->abi_size;
900 fn_type->abi_align = (fn_type_id->alignment == 0) ?
901 g->builtin_types.entry_usize->abi_align : fn_type_id->alignment;
1259902
1260903 g->fn_type_table.put(&fn_type->data.fn.fn_type_id, fn_type);
1261904
......@@ -1282,14 +925,6 @@ static ZigType *get_root_container_type(CodeGen *g, const char *full_name, Buf *
1282925 entry->data.structure.decls_scope = create_decls_scope(g, nullptr, nullptr, entry, entry, bare_name);
1283926 entry->data.structure.root_struct = root_struct;
1284927 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));
1293928
1294929 buf_init_from_str(&entry->name, full_name);
1295930 return entry;
......@@ -1316,16 +951,6 @@ ZigType *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind
1316951 break;
1317952 }
1318953
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
1329954 buf_init_from_str(&entry->name, full_name);
1330955
1331956 return entry;
......@@ -1375,7 +1000,9 @@ ZigType *get_generic_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
13751000
13761001 fn_type->data.fn.fn_type_id = *fn_type_id;
13771002 fn_type->data.fn.is_generic = true;
1378 fn_type->zero_bits = true;
1003 fn_type->abi_size = 0;
1004 fn_type->size_in_bits = 0;
1005 fn_type->abi_align = 0;
13791006 return fn_type;
13801007}
13811008
......@@ -1611,14 +1238,10 @@ ZigType *get_auto_err_set_type(CodeGen *g, ZigFn *fn_entry) {
16111238 ZigType *err_set_type = new_type_table_entry(ZigTypeIdErrorSet);
16121239 buf_resize(&err_set_type->name, 0);
16131240 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;
16161241 err_set_type->data.error_set.err_count = 0;
16171242 err_set_type->data.error_set.errors = nullptr;
16181243 err_set_type->data.error_set.infer_fn = fn_entry;
16191244
1620 g->error_di_types.append(&err_set_type->di_type);
1621
16221245 return err_set_type;
16231246}
16241247
......@@ -1858,10 +1481,10 @@ bool type_is_invalid(ZigType *type_entry) {
18581481 return true;
18591482 case ZigTypeIdStruct:
18601483 return type_entry->data.structure.resolve_status == ResolveStatusInvalid;
1484 case ZigTypeIdUnion:
1485 return type_entry->data.unionation.resolve_status == ResolveStatusInvalid;
18611486 case ZigTypeIdEnum:
18621487 return type_entry->data.enumeration.is_invalid;
1863 case ZigTypeIdUnion:
1864 return type_entry->data.unionation.is_invalid;
18651488 default:
18661489 return false;
18671490 }
......@@ -1870,181 +1493,66 @@ bool type_is_invalid(ZigType *type_entry) {
18701493
18711494
18721495static Error resolve_enum_type(CodeGen *g, ZigType *enum_type) {
1873 assert(enum_type->id == ZigTypeIdEnum);
1496 return resolve_enum_zero_bits(g, enum_type);
1497}
18741498
1875 if (enum_type->data.enumeration.is_invalid)
1876 return ErrorSemanticAnalyzeFail;
18771499
1878 if (enum_type->data.enumeration.complete)
1879 return ErrorNone;
1500ZigType *get_struct_type(CodeGen *g, const char *type_name, const char *field_names[],
1501 ZigType *field_types[], size_t field_count)
1502{
1503 ZigType *struct_type = new_type_table_entry(ZigTypeIdStruct);
18801504
1881 Error err;
1882 if ((err = resolve_enum_zero_bits(g, enum_type)))
1883 return err;
1505 buf_init_from_str(&struct_type->name, type_name);
18841506
1885 AstNode *decl_node = enum_type->data.enumeration.decl_node;
1507 struct_type->data.structure.src_field_count = field_count;
1508 struct_type->data.structure.gen_field_count = 0;
1509 struct_type->data.structure.resolve_status = ResolveStatusSizeKnown;
1510 struct_type->data.structure.fields = allocate<TypeStructField>(field_count);
1511 struct_type->data.structure.fields_by_name.init(field_count);
18861512
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);
1513 size_t abi_align = 0;
1514 for (size_t i = 0; i < field_count; i += 1) {
1515 TypeStructField *field = &struct_type->data.structure.fields[i];
1516 field->name = buf_create_from_str(field_names[i]);
1517 field->type_entry = field_types[i];
1518 field->src_index = i;
1519
1520 if (type_has_bits(field->type_entry)) {
1521 assert(type_is_resolved(field->type_entry, ResolveStatusSizeKnown));
1522 if (field->type_entry->abi_align > abi_align) {
1523 abi_align = field->type_entry->abi_align;
1524 }
1525 field->gen_index = struct_type->data.structure.gen_field_count;
1526 struct_type->data.structure.gen_field_count += 1;
1527 } else {
1528 field->gen_index = SIZE_MAX;
18941529 }
1895 return ErrorSemanticAnalyzeFail;
1530
1531 auto prev_entry = struct_type->data.structure.fields_by_name.put_unique(field->name, field);
1532 assert(prev_entry == nullptr);
18961533 }
18971534
1898 assert(!enum_type->data.enumeration.zero_bits_loop_flag);
1899 assert(decl_node->type == NodeTypeContainerDecl);
1900 assert(enum_type->di_type);
1535 size_t next_offset = 0;
1536 for (size_t i = 0; i < field_count; i += 1) {
1537 TypeStructField *field = &struct_type->data.structure.fields[i];
1538 field->offset = next_offset;
1539 next_offset = next_field_offset(next_offset, abi_align,
1540 field->type_entry->abi_size, field->type_entry->abi_align);
1541 }
19011542
1902 uint32_t field_count = enum_type->data.enumeration.src_field_count;
1543 struct_type->abi_align = abi_align;
1544 struct_type->abi_size = next_offset;
1545 struct_type->size_in_bits = next_offset * 8;
19031546
1904 assert(enum_type->data.enumeration.fields);
1905 ZigLLVMDIEnumerator **di_enumerators = allocate<ZigLLVMDIEnumerator*>(field_count);
1547 return struct_type;
1548}
19061549
1907 Scope *scope = &enum_type->data.enumeration.decls_scope->base;
1908 ZigType *import = get_scope_import(scope);
1550static size_t get_store_size_in_bits(size_t size_in_bits) {
1551 return (size_in_bits + 7) / 8;
1552}
19091553
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;
1965}
1966
1967
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);
2042
2043 return struct_type;
2044}
2045
2046static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
2047 assert(struct_type->id == ZigTypeIdStruct);
1554static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
1555 assert(struct_type->id == ZigTypeIdStruct);
20481556
20491557 Error err;
20501558
......@@ -2068,454 +1576,284 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
20681576 return ErrorSemanticAnalyzeFail;
20691577 }
20701578
2071 struct_type->data.structure.resolve_loop_flag = true;
2072
20731579 assert(struct_type->data.structure.fields || struct_type->data.structure.src_field_count == 0);
20741580 assert(decl_node->type == NodeTypeContainerDecl);
20751581
20761582 size_t field_count = struct_type->data.structure.src_field_count;
20771583
2078 size_t gen_field_count = struct_type->data.structure.gen_field_count;
2079 LLVMTypeRef *element_types = allocate<LLVMTypeRef>(gen_field_count);
1584 bool packed = (struct_type->data.structure.layout == ContainerLayoutPacked);
1585 struct_type->data.structure.resolve_loop_flag = true;
20801586
2081 Scope *scope = &struct_type->data.structure.decls_scope->base;
1587 uint32_t *host_int_bytes = allocate<uint32_t>(struct_type->data.structure.gen_field_count);
20821588
2083 size_t gen_field_index = 0;
2084 bool packed = (struct_type->data.structure.layout == ContainerLayoutPacked);
1589 // Compute offsets for all the fields.
20851590 size_t packed_bits_offset = 0;
1591 size_t next_offset = 0;
20861592 size_t first_packed_bits_offset_misalign = SIZE_MAX;
2087 size_t debug_field_count = 0;
1593 size_t gen_field_index = 0;
1594 size_t size_in_bits = 0;
1595 size_t abi_align = struct_type->abi_align;
20881596
20891597 for (size_t i = 0; i < field_count; i += 1) {
20901598 TypeStructField *type_struct_field = &struct_type->data.structure.fields[i];
20911599 ZigType *field_type = type_struct_field->type_entry;
20921600
2093 if ((err = ensure_complete_type(g, field_type))) {
1601 if (!type_has_bits(field_type))
1602 continue;
1603
1604 if ((err = type_resolve(g, field_type, ResolveStatusSizeKnown))) {
20941605 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2095 break;
1606 return ErrorSemanticAnalyzeFail;
20961607 }
20971608
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 }
1609 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid) {
1610 return ErrorSemanticAnalyzeFail;
21071611 }
21081612
2109 if (!type_has_bits(field_type))
2110 continue;
2111
21121613 type_struct_field->gen_index = gen_field_index;
1614 type_struct_field->offset = next_offset;
21131615
21141616 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
21211617 size_t field_size_in_bits = type_size_bits(g, field_type);
21221618 size_t next_packed_bits_offset = packed_bits_offset + field_size_in_bits;
21231619
1620 size_in_bits += field_size_in_bits;
1621
21241622 if (first_packed_bits_offset_misalign != SIZE_MAX) {
21251623 // this field is not byte-aligned; it is part of the previous field with a bit offset
21261624 type_struct_field->bit_offset_in_host = packed_bits_offset - first_packed_bits_offset_misalign;
21271625
21281626 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) {
1627 if (get_store_size_in_bits(full_bit_count) == full_bit_count) {
21311628 // next field recovers store alignment
2132 element_types[gen_field_index] = int_type_ref;
1629 host_int_bytes[gen_field_index] = full_bit_count / 8;
21331630 gen_field_index += 1;
1631 // TODO: https://github.com/ziglang/zig/issues/1512
1632 next_offset = next_field_offset(next_offset, abi_align, full_bit_count / 8, 1);
1633 size_in_bits = next_offset * 8;
21341634
21351635 first_packed_bits_offset_misalign = SIZE_MAX;
21361636 }
2137 } else if (8 * LLVMStoreSizeOfType(g->target_data_ref, field_type->type_ref) != field_size_in_bits) {
1637 } else if (get_store_size_in_bits(field_type->size_in_bits) != field_size_in_bits) {
21381638 first_packed_bits_offset_misalign = packed_bits_offset;
21391639 type_struct_field->bit_offset_in_host = 0;
21401640 } else {
21411641 // This is a byte-aligned field (both start and end) in a packed struct.
2142 element_types[gen_field_index] = field_type->type_ref;
21431642 type_struct_field->bit_offset_in_host = 0;
21441643 gen_field_index += 1;
1644 // TODO: https://github.com/ziglang/zig/issues/1512
1645 next_offset = next_field_offset(next_offset, abi_align, field_type->size_in_bits / 8, 1);
1646 size_in_bits = next_offset * 8;
21451647 }
21461648 packed_bits_offset = next_packed_bits_offset;
21471649 } else {
2148 element_types[gen_field_index] = field_type->type_ref;
2149 assert(element_types[gen_field_index]);
2150
21511650 gen_field_index += 1;
1651 next_offset = next_field_offset(next_offset, abi_align, field_type->abi_size, field_type->abi_align);
1652 size_in_bits = next_offset * 8;
21521653 }
2153 debug_field_count += 1;
21541654 }
21551655 if (first_packed_bits_offset_misalign != SIZE_MAX) {
21561656 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);
1657 size_t store_bit_count = get_store_size_in_bits(full_bit_count);
1658 next_offset = next_field_offset(next_offset, abi_align, store_bit_count / 8, 1);
1659 host_int_bytes[gen_field_index] = store_bit_count / 8;
21601660 gen_field_index += 1;
21611661 }
21621662
1663 struct_type->abi_size = next_offset;
1664 struct_type->size_in_bits = size_in_bits;
1665 struct_type->data.structure.resolve_status = ResolveStatusSizeKnown;
1666 struct_type->data.structure.gen_field_count = (uint32_t)gen_field_index;
21631667 struct_type->data.structure.resolve_loop_flag = false;
1668 struct_type->data.structure.host_int_bytes = host_int_bytes;
21641669
2165 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid)
2166 return ErrorSemanticAnalyzeFail;
1670 return ErrorNone;
1671}
21671672
2168 struct_type->data.structure.resolve_status = ResolveStatusSizeKnown;
1673static Error resolve_union_alignment(CodeGen *g, ZigType *union_type) {
1674 assert(union_type->id == ZigTypeIdUnion);
21691675
2170 if (struct_type->zero_bits) {
2171 struct_type->type_ref = LLVMVoidType();
1676 Error err;
21721677
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;
1678 if (union_type->data.unionation.resolve_status == ResolveStatusInvalid)
1679 return ErrorSemanticAnalyzeFail;
1680 if (union_type->data.unionation.resolve_status >= ResolveStatusAlignmentKnown)
21871681 return ErrorNone;
1682
1683 if ((err = resolve_union_zero_bits(g, union_type)))
1684 return err;
1685
1686 if (union_type->data.unionation.resolve_loop_flag) {
1687 if (!union_type->data.unionation.reported_infinite_err) {
1688 AstNode *decl_node = union_type->data.unionation.decl_node;
1689 union_type->data.unionation.reported_infinite_err = true;
1690 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
1691 ErrorMsg *msg = add_node_error(g, decl_node,
1692 buf_sprintf("union '%s' depends on its own alignment", buf_ptr(&union_type->name)));
1693 emit_error_notes_for_ref_stack(g, msg);
1694 }
1695 return ErrorSemanticAnalyzeFail;
21881696 }
2189 assert(struct_type->di_type);
21901697
1698 // set temporary flag
1699 union_type->data.unionation.resolve_loop_flag = true;
21911700
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;
1701 ZigType *most_aligned_union_member = nullptr;
1702 uint32_t field_count = union_type->data.unionation.src_field_count;
21951703
2196 LLVMStructSetBody(struct_type->type_ref, element_types, (unsigned)gen_field_count, packed);
1704 for (uint32_t i = 0; i < field_count; i += 1) {
1705 TypeUnionField *union_field = &union_type->data.unionation.fields[i];
1706 ZigType *field_type = union_field->type_entry;
21971707
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);
1708 if ((err = type_resolve(g, field_type, ResolveStatusAlignmentKnown))) {
1709 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
1710 return ErrorSemanticAnalyzeFail;
1711 }
22021712
2203 ZigLLVMDIType **di_element_types = allocate<ZigLLVMDIType*>(debug_field_count);
1713 if (type_is_invalid(union_type))
1714 return ErrorSemanticAnalyzeFail;
22041715
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) {
1716 if (!type_has_bits(field_type))
22121717 continue;
1718
1719 if (most_aligned_union_member == nullptr ||
1720 field_type->abi_align > most_aligned_union_member->abi_align)
1721 {
1722 most_aligned_union_member = field_type;
22131723 }
1724 }
22141725
2215 ZigType *field_type = type_struct_field->type_entry;
1726 // unset temporary flag
1727 union_type->data.unionation.resolve_loop_flag = true;
1728 union_type->data.unionation.resolve_status = ResolveStatusAlignmentKnown;
22161729
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;
1730 ZigType *tag_type = union_type->data.unionation.tag_type;
1731 if (tag_type != nullptr && type_has_bits(tag_type)) {
1732 if ((err = type_resolve(g, tag_type, ResolveStatusAlignmentKnown))) {
1733 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
1734 return ErrorSemanticAnalyzeFail;
22271735 }
2228
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;
1736 if (most_aligned_union_member == nullptr) {
1737 union_type->abi_align = tag_type->abi_align;
1738 union_type->data.unionation.gen_tag_index = SIZE_MAX;
1739 union_type->data.unionation.gen_union_index = SIZE_MAX;
1740 } else if (tag_type->abi_align > most_aligned_union_member->abi_align) {
1741 union_type->abi_align = tag_type->abi_align;
1742 union_type->data.unionation.gen_tag_index = 0;
1743 union_type->data.unionation.gen_union_index = 1;
22401744 } 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);
1745 union_type->abi_align = most_aligned_union_member->abi_align;
1746 union_type->data.unionation.gen_union_index = 0;
1747 union_type->data.unionation.gen_tag_index = 1;
22451748 }
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;
1749 } else {
1750 assert(most_aligned_union_member != nullptr);
1751 union_type->abi_align = most_aligned_union_member->abi_align;
1752 union_type->data.unionation.gen_union_index = SIZE_MAX;
1753 union_type->data.unionation.gen_tag_index = SIZE_MAX;
22551754 }
22561755
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
22711756 return ErrorNone;
22721757}
22731758
22741759static Error resolve_union_type(CodeGen *g, ZigType *union_type) {
22751760 assert(union_type->id == ZigTypeIdUnion);
22761761
2277 if (union_type->data.unionation.complete)
1762 Error err;
1763
1764 if (union_type->data.unionation.resolve_status == ResolveStatusInvalid)
1765 return ErrorSemanticAnalyzeFail;
1766 if (union_type->data.unionation.resolve_status >= ResolveStatusSizeKnown)
22781767 return ErrorNone;
22791768
2280 Error err;
2281 if ((err = resolve_union_zero_bits(g, union_type)))
1769 if ((err = resolve_union_alignment(g, union_type)))
22821770 return err;
22831771
22841772 AstNode *decl_node = union_type->data.unionation.decl_node;
22851773
2286 if (union_type->data.unionation.embedded_in_current) {
2287 if (!union_type->data.unionation.reported_infinite_err) {
2288 union_type->data.unionation.reported_infinite_err = true;
2289 union_type->data.unionation.is_invalid = true;
2290 ErrorMsg *msg = add_node_error(g, decl_node,
2291 buf_sprintf("union '%s' contains itself", buf_ptr(&union_type->name)));
2292 emit_error_notes_for_ref_stack(g, msg);
2293 }
2294 return ErrorSemanticAnalyzeFail;
2295 }
22961774
2297 assert(!union_type->data.unionation.zero_bits_loop_flag);
22981775 assert(decl_node->type == NodeTypeContainerDecl);
2299 assert(union_type->di_type);
23001776
23011777 uint32_t field_count = union_type->data.unionation.src_field_count;
23021778
23031779 assert(union_type->data.unionation.fields);
23041780
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;
1781 size_t union_abi_size = 0;
1782 size_t union_size_in_bits = 0;
23121783
2313 Scope *scope = &union_type->data.unionation.decls_scope->base;
2314 ZigType *import = get_scope_import(scope);
1784 if (union_type->data.unionation.resolve_loop_flag) {
1785 if (!union_type->data.unionation.reported_infinite_err) {
1786 union_type->data.unionation.reported_infinite_err = true;
1787 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
1788 ErrorMsg *msg = add_node_error(g, decl_node,
1789 buf_sprintf("union '%s' depends on its own size", buf_ptr(&union_type->name)));
1790 emit_error_notes_for_ref_stack(g, msg);
1791 }
1792 return ErrorSemanticAnalyzeFail;
1793 }
23151794
23161795 // set temporary flag
2317 union_type->data.unionation.embedded_in_current = true;
2318
1796 union_type->data.unionation.resolve_loop_flag = true;
23191797
23201798 for (uint32_t i = 0; i < field_count; i += 1) {
2321 AstNode *field_node = decl_node->data.container_decl.fields.at(i);
23221799 TypeUnionField *union_field = &union_type->data.unionation.fields[i];
23231800 ZigType *field_type = union_field->type_entry;
23241801
2325 if ((err = ensure_complete_type(g, field_type))) {
2326 union_type->data.unionation.is_invalid = true;
2327 continue;
1802 if ((err = type_resolve(g, field_type, ResolveStatusSizeKnown))) {
1803 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
1804 return ErrorSemanticAnalyzeFail;
23281805 }
23291806
1807 if (type_is_invalid(union_type))
1808 return ErrorSemanticAnalyzeFail;
1809
23301810 if (!type_has_bits(field_type))
23311811 continue;
23321812
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);
1813 union_abi_size = max(union_abi_size, field_type->abi_size);
1814 union_size_in_bits = max(union_size_in_bits, field_type->size_in_bits);
1815 }
23461816
2347 biggest_size_in_bits = max(biggest_size_in_bits, store_size_in_bits);
1817 // unset temporary flag
1818 union_type->data.unionation.resolve_loop_flag = false;
1819 union_type->data.unionation.resolve_status = ResolveStatusSizeKnown;
1820 union_type->data.unionation.union_abi_size = union_abi_size;
23481821
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;
1822 ZigType *tag_type = union_type->data.unionation.tag_type;
1823 ZigType *most_aligned_union_member = union_type->data.unionation.most_aligned_union_member;
1824 if (tag_type != nullptr && type_has_bits(tag_type)) {
1825 if ((err = type_resolve(g, tag_type, ResolveStatusSizeKnown))) {
1826 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
1827 return ErrorSemanticAnalyzeFail;
1828 }
1829 if (most_aligned_union_member == nullptr) {
1830 union_type->abi_size = tag_type->abi_size;
1831 union_type->size_in_bits = tag_type->size_in_bits;
1832 } else {
1833 size_t field_sizes[2];
1834 size_t field_aligns[2];
1835 field_sizes[union_type->data.unionation.gen_tag_index] = tag_type->abi_size;
1836 field_aligns[union_type->data.unionation.gen_tag_index] = tag_type->abi_align;
1837 field_sizes[union_type->data.unionation.gen_union_index] = union_abi_size;
1838 field_aligns[union_type->data.unionation.gen_union_index] = most_aligned_union_member->abi_align;
1839 size_t field2_offset = next_field_offset(0, union_type->abi_align, field_sizes[0], field_aligns[0]);
1840 union_type->abi_size = next_field_offset(field2_offset, union_type->abi_align, field_sizes[1], field_aligns[1]);
1841 union_type->size_in_bits = union_type->abi_size * 8;
23531842 }
1843 } else {
1844 union_type->abi_size = union_abi_size;
1845 union_type->size_in_bits = union_size_in_bits;
23541846 }
23551847
1848 return ErrorNone;
1849}
23561850
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;
1851static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
1852 assert(enum_type->id == ZigTypeIdEnum);
23621853
2363 if (union_type->data.unionation.is_invalid)
1854 if (enum_type->data.enumeration.is_invalid)
23641855 return ErrorSemanticAnalyzeFail;
23651856
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;
2384 }
2385
2386 uint64_t padding_in_bits = biggest_size_in_bits - size_of_most_aligned_member_in_bits;
2387
2388 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);
2400 } else {
2401 LLVMStructSetBody(union_type->type_ref, &most_aligned_union_member->type_ref, 1, false);
2402 }
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;
2454 } else {
2455 union_type->data.unionation.gen_union_index = 0;
2456 union_type->data.unionation.gen_tag_index = 1;
2457 }
2458
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
2513 return ErrorNone;
2514}
2515
2516static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
2517 assert(enum_type->id == ZigTypeIdEnum);
2518
25191857 if (enum_type->data.enumeration.zero_bits_known)
25201858 return ErrorNone;
25211859
......@@ -2531,7 +1869,6 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
25311869
25321870 AstNode *decl_node = enum_type->data.enumeration.decl_node;
25331871 assert(decl_node->type == NodeTypeContainerDecl);
2534 assert(enum_type->di_type);
25351872
25361873 assert(!enum_type->data.enumeration.fields);
25371874 uint32_t field_count = (uint32_t)decl_node->data.container_decl.fields.length;
......@@ -2564,6 +1901,10 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
25641901 tag_int_type = get_smallest_unsigned_int_type(g, field_count - 1);
25651902 }
25661903
1904 enum_type->size_in_bits = tag_int_type->size_in_bits;
1905 enum_type->abi_size = tag_int_type->abi_size;
1906 enum_type->abi_align = tag_int_type->abi_align;
1907
25671908 // TODO: Are extern enums allowed to have an init_arg_expr?
25681909 if (decl_node->data.container_decl.init_arg_expr != nullptr) {
25691910 ZigType *wanted_tag_int_type = analyze_type_expr(g, scope, decl_node->data.container_decl.init_arg_expr);
......@@ -2587,7 +1928,6 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
25871928 }
25881929 }
25891930 enum_type->data.enumeration.tag_int_type = tag_int_type;
2590 enum_type->type_ref = tag_int_type->type_ref;
25911931
25921932 for (uint32_t field_i = 0; field_i < field_count; field_i += 1) {
25931933 AstNode *field_node = decl_node->data.container_decl.fields.at(field_i);
......@@ -2671,7 +2011,6 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
26712011 }
26722012
26732013 enum_type->data.enumeration.zero_bits_loop_flag = false;
2674 enum_type->zero_bits = !type_has_bits(tag_int_type);
26752014 enum_type->data.enumeration.zero_bits_known = true;
26762015
26772016 if (enum_type->data.enumeration.is_invalid)
......@@ -2698,7 +2037,6 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
26982037
26992038 AstNode *decl_node = struct_type->data.structure.decl_node;
27002039 assert(decl_node->type == NodeTypeContainerDecl);
2701 assert(struct_type->di_type);
27022040
27032041 assert(!struct_type->data.structure.fields);
27042042 size_t field_count = decl_node->data.container_decl.fields.length;
......@@ -2767,7 +2105,10 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
27672105
27682106 struct_type->data.structure.resolve_loop_flag = false;
27692107 struct_type->data.structure.gen_field_count = (uint32_t)gen_field_index;
2770 struct_type->zero_bits = (gen_field_index == 0);
2108 if (gen_field_index != 0) {
2109 struct_type->abi_size = SIZE_MAX;
2110 struct_type->size_in_bits = SIZE_MAX;
2111 }
27712112
27722113 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid)
27732114 return ErrorSemanticAnalyzeFail;
......@@ -2803,52 +2144,50 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {
28032144
28042145 struct_type->data.structure.resolve_loop_flag = true;
28052146 assert(decl_node->type == NodeTypeContainerDecl);
2806 assert(struct_type->di_type);
28072147
2148 size_t abi_align = 0;
28082149 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) {
2150 bool packed = struct_type->data.structure.layout == ContainerLayoutPacked;
2151
2152 for (size_t i = 0; i < field_count; i += 1) {
28192153 TypeStructField *field = &struct_type->data.structure.fields[i];
2820 uint32_t this_field_align;
2821
2822 // TODO If we have no type_entry for the field, we've already failed to
2823 // compile the program correctly. This stage1 compiler needs a deeper
2824 // reworking to make this correct, or we can ignore the problem
2825 // and make sure it is fixed in stage2. This workaround is for when
2826 // there is a false positive of a dependency loop, of alignment depending
2827 // on itself. When this false positive happens we assume a pointer-aligned
2828 // field, which is usually fine but could be incorrectly over-aligned or
2829 // 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));
2832 } else {
2833 if (type_is_invalid(field->type_entry)) {
2834 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2835 break;
2836 }
2154 ZigType *field_type = field->type_entry;
2155 assert(field_type != nullptr);
28372156
2838 if (!type_has_bits(field->type_entry))
2839 continue;
2157 if ((err = type_resolve(g, field_type, ResolveStatusAlignmentKnown))) {
2158 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2159 return ErrorSemanticAnalyzeFail;
2160 }
2161
2162 if (struct_type->data.structure.layout == ContainerLayoutExtern &&
2163 !type_allowed_in_extern(g, field_type))
2164 {
2165 AstNode *field_source_node = decl_node->data.container_decl.fields.at(i);
2166 add_node_error(g, field_source_node,
2167 buf_sprintf("extern structs cannot contain fields of type '%s'",
2168 buf_ptr(&field_type->name)));
2169 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2170 return ErrorSemanticAnalyzeFail;
2171 }
2172
2173 if (!type_has_bits(field_type))
2174 continue;
28402175
2841 if ((err = type_resolve(g, field->type_entry, ResolveStatusAlignmentKnown))) {
2176 if (packed) {
2177 AstNode *field_source_node = decl_node->data.container_decl.fields.at(i);
2178 if ((err = emit_error_unless_type_allowed_in_packed_struct(g, field_type, field_source_node))) {
28422179 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2843 break;
2180 return ErrorSemanticAnalyzeFail;
2181 }
2182 // TODO: https://github.com/ziglang/zig/issues/1512
2183 if (1 > abi_align) {
2184 abi_align = 1;
2185 }
2186 } else {
2187 // TODO: https://github.com/ziglang/zig/issues/1512
2188 if (field_type->abi_align > abi_align) {
2189 abi_align = field_type->abi_align;
28442190 }
2845
2846 this_field_align = get_abi_alignment(g, field->type_entry);
2847 assert(this_field_align != 0);
2848 }
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;
28522191 }
28532192 }
28542193
......@@ -2859,6 +2198,7 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {
28592198 }
28602199
28612200 struct_type->data.structure.resolve_status = ResolveStatusAlignmentKnown;
2201 struct_type->abi_align = abi_align;
28622202 return ErrorNone;
28632203}
28642204
......@@ -2867,57 +2207,41 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
28672207
28682208 Error err;
28692209
2870 if (union_type->data.unionation.is_invalid)
2210 if (union_type->data.unionation.resolve_status == ResolveStatusInvalid)
28712211 return ErrorSemanticAnalyzeFail;
28722212
2873 if (union_type->data.unionation.zero_bits_known)
2213 if (union_type->data.unionation.resolve_status >= ResolveStatusZeroBitsKnown)
28742214 return ErrorNone;
28752215
2876 if (type_is_invalid(union_type))
2877 return ErrorSemanticAnalyzeFail;
2878
2879 if (union_type->data.unionation.zero_bits_loop_flag) {
2216 if (union_type->data.unionation.resolve_loop_flag) {
28802217 // 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 }
2218 // not zero bits.
2219 // TODO actually it could still be zero bits. Here we should continue analyzing
2220 // the union from the next field index.
2221 union_type->data.unionation.resolve_status = ResolveStatusZeroBitsKnown;
2222 union_type->data.unionation.resolve_loop_flag = false;
2223 union_type->abi_size = SIZE_MAX;
2224 union_type->size_in_bits = SIZE_MAX;
28942225 return ErrorNone;
28952226 }
28962227
2897 union_type->data.unionation.zero_bits_loop_flag = true;
2228 union_type->data.unionation.resolve_loop_flag = true;
28982229
28992230 AstNode *decl_node = union_type->data.unionation.decl_node;
29002231 assert(decl_node->type == NodeTypeContainerDecl);
2901 assert(union_type->di_type);
29022232
2903 assert(!union_type->data.unionation.fields);
2233 assert(union_type->data.unionation.fields == nullptr);
29042234 uint32_t field_count = (uint32_t)decl_node->data.container_decl.fields.length;
29052235 if (field_count == 0) {
29062236 add_node_error(g, decl_node, buf_sprintf("unions must have 1 or more fields"));
2907
29082237 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;
2238 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
29132239 return ErrorSemanticAnalyzeFail;
29142240 }
29152241 union_type->data.unionation.src_field_count = field_count;
29162242 union_type->data.unionation.fields = allocate<TypeUnionField>(field_count);
29172243 union_type->data.unionation.fields_by_name.init(field_count);
29182244
2919 uint32_t biggest_align_bytes = 0;
2920
29212245 Scope *scope = &union_type->data.unionation.decls_scope->base;
29222246
29232247 HashMap<BigInt, AstNode *, bigint_hash, bigint_eql> occupied_tag_values = {};
......@@ -2931,7 +2255,6 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
29312255 bool create_enum_type = decl_node->data.container_decl.auto_enum || (enum_type_node == nullptr && want_safety);
29322256 bool *covered_enum_fields;
29332257 ZigLLVMDIEnumerator **di_enumerators;
2934 uint32_t abi_alignment_so_far;
29352258 if (create_enum_type) {
29362259 occupied_tag_values.init(field_count);
29372260
......@@ -2941,13 +2264,13 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
29412264 if (enum_type_node != nullptr) {
29422265 tag_int_type = analyze_type_expr(g, scope, enum_type_node);
29432266 if (type_is_invalid(tag_int_type)) {
2944 union_type->data.unionation.is_invalid = true;
2267 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
29452268 return ErrorSemanticAnalyzeFail;
29462269 }
29472270 if (tag_int_type->id != ZigTypeIdInt) {
29482271 add_node_error(g, enum_type_node,
29492272 buf_sprintf("expected integer tag type, found '%s'", buf_ptr(&tag_int_type->name)));
2950 union_type->data.unionation.is_invalid = true;
2273 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
29512274 return ErrorSemanticAnalyzeFail;
29522275 }
29532276 } else if (auto_layout && field_count == 1) {
......@@ -2955,13 +2278,15 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
29552278 } else {
29562279 tag_int_type = get_smallest_unsigned_int_type(g, field_count - 1);
29572280 }
2958 abi_alignment_so_far = get_abi_alignment(g, tag_int_type);
29592281
29602282 tag_type = new_type_table_entry(ZigTypeIdEnum);
29612283 buf_resize(&tag_type->name, 0);
29622284 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;
2285 tag_type->llvm_type = tag_int_type->llvm_type;
2286 tag_type->llvm_di_type = tag_int_type->llvm_di_type;
2287 tag_type->abi_size = tag_int_type->abi_size;
2288 tag_type->abi_align = tag_int_type->abi_align;
2289 tag_type->size_in_bits = tag_int_type->size_in_bits;
29652290
29662291 tag_type->data.enumeration.tag_int_type = tag_int_type;
29672292 tag_type->data.enumeration.zero_bits_known = true;
......@@ -2975,11 +2300,11 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
29752300 } else if (enum_type_node != nullptr) {
29762301 ZigType *enum_type = analyze_type_expr(g, scope, enum_type_node);
29772302 if (type_is_invalid(enum_type)) {
2978 union_type->data.unionation.is_invalid = true;
2303 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
29792304 return ErrorSemanticAnalyzeFail;
29802305 }
29812306 if (enum_type->id != ZigTypeIdEnum) {
2982 union_type->data.unionation.is_invalid = true;
2307 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
29832308 add_node_error(g, enum_type_node,
29842309 buf_sprintf("expected enum tag type, found '%s'", buf_ptr(&enum_type->name)));
29852310 return ErrorSemanticAnalyzeFail;
......@@ -2989,11 +2314,9 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
29892314 return err;
29902315 }
29912316 tag_type = enum_type;
2992 abi_alignment_so_far = get_abi_alignment(g, enum_type); // this populates src_field_count
29932317 covered_enum_fields = allocate<bool>(enum_type->data.enumeration.src_field_count);
29942318 } else {
29952319 tag_type = nullptr;
2996 abi_alignment_so_far = 0;
29972320 }
29982321 union_type->data.unionation.tag_type = tag_type;
29992322
......@@ -3010,8 +2333,8 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
30102333 ErrorMsg *msg = add_node_error(g, field_node,
30112334 buf_sprintf("duplicate union field: '%s'", buf_ptr(union_field->name)));
30122335 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;
2336 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
2337 return ErrorSemanticAnalyzeFail;
30152338 }
30162339
30172340 ZigType *field_type;
......@@ -3020,29 +2343,31 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
30202343 field_type = g->builtin_types.entry_void;
30212344 } else {
30222345 add_node_error(g, field_node, buf_sprintf("union field missing type"));
3023 union_type->data.unionation.is_invalid = true;
3024 continue;
2346 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
2347 return ErrorSemanticAnalyzeFail;
30252348 }
30262349 } else {
30272350 field_type = analyze_type_expr(g, scope, field_node->data.struct_field.type);
30282351 if ((err = type_resolve(g, field_type, ResolveStatusAlignmentKnown))) {
3029 union_type->data.unionation.is_invalid = true;
3030 continue;
2352 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
2353 return ErrorSemanticAnalyzeFail;
30312354 }
2355 if (union_type->data.unionation.resolve_status == ResolveStatusInvalid)
2356 return ErrorSemanticAnalyzeFail;
30322357 }
30332358 union_field->type_entry = field_type;
30342359
30352360 if (field_type->id == ZigTypeIdOpaque) {
30362361 add_node_error(g, field_node->data.struct_field.type,
30372362 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;
2363 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
2364 return ErrorSemanticAnalyzeFail;
30402365 }
30412366
30422367 switch (type_requires_comptime(g, field_type)) {
30432368 case ReqCompTimeInvalid:
3044 union_type->data.unionation.is_invalid = true;
3045 continue;
2369 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
2370 return ErrorSemanticAnalyzeFail;
30462371 case ReqCompTimeYes:
30472372 union_type->data.unionation.requires_comptime = true;
30482373 break;
......@@ -3074,8 +2399,8 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
30742399 ZigType *tag_int_type = tag_type->data.enumeration.tag_int_type;
30752400 ConstExprValue *result = analyze_const_value(g, scope, tag_value, tag_int_type, nullptr);
30762401 if (type_is_invalid(result->type)) {
3077 union_type->data.unionation.is_invalid = true;
3078 continue;
2402 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
2403 return ErrorSemanticAnalyzeFail;
30792404 }
30802405 assert(result->special != ConstValSpecialRuntime);
30812406 assert(result->type->id == ZigTypeIdInt);
......@@ -3090,8 +2415,8 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
30902415 buf_sprintf("enum tag value %s already taken", buf_ptr(val_buf)));
30912416 add_error_note(g, msg, entry->value,
30922417 buf_sprintf("other occurrence here"));
3093 union_type->data.unionation.is_invalid = true;
3094 continue;
2418 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
2419 return ErrorSemanticAnalyzeFail;
30952420 }
30962421 }
30972422 } else if (enum_type_node != nullptr) {
......@@ -3101,8 +2426,8 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
31012426 buf_sprintf("enum field not found: '%s'", buf_ptr(field_name)));
31022427 add_error_note(g, msg, tag_type->data.enumeration.decl_node,
31032428 buf_sprintf("enum declared here"));
3104 union_type->data.unionation.is_invalid = true;
3105 continue;
2429 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
2430 return ErrorSemanticAnalyzeFail;
31062431 }
31072432 covered_enum_fields[union_field->enum_field->decl_index] = true;
31082433 } else {
......@@ -3118,21 +2443,8 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
31182443
31192444 union_field->gen_index = gen_field_index;
31202445 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 }
31292446 }
31302447
3131 union_type->data.unionation.abi_alignment = abi_alignment_so_far;
3132
3133 if (union_type->data.unionation.is_invalid)
3134 return ErrorSemanticAnalyzeFail;
3135
31362448 bool src_have_tag = decl_node->data.container_decl.auto_enum ||
31372449 decl_node->data.container_decl.init_arg_expr != nullptr;
31382450
......@@ -3152,7 +2464,7 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
31522464 decl_node->data.container_decl.init_arg_expr : decl_node;
31532465 add_node_error(g, source_node,
31542466 buf_sprintf("%s union does not support enum tag type", qual_str));
3155 union_type->data.unionation.is_invalid = true;
2467 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
31562468 return ErrorSemanticAnalyzeFail;
31572469 }
31582470
......@@ -3194,33 +2506,24 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
31942506 buf_sprintf("enum field missing: '%s'", buf_ptr(enum_field->name)));
31952507 add_error_note(g, msg, field_node,
31962508 buf_sprintf("declared here"));
3197 union_type->data.unionation.is_invalid = true;
2509 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
31982510 }
31992511 }
32002512 }
32012513
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;
3215 }
3216
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;
3221
3222 if (union_type->data.unionation.is_invalid)
2514 if (union_type->data.unionation.resolve_status == ResolveStatusInvalid) {
32232515 return ErrorSemanticAnalyzeFail;
2516 }
2517
2518 union_type->data.unionation.resolve_loop_flag = false;
2519
2520 union_type->data.unionation.gen_field_count = gen_field_index;
2521 bool zero_bits = gen_field_index == 0 && (field_count < 2 || !src_have_tag);
2522 if (!zero_bits) {
2523 union_type->abi_size = SIZE_MAX;
2524 union_type->size_in_bits = SIZE_MAX;
2525 }
2526 union_type->data.unionation.resolve_status = zero_bits ? ResolveStatusSizeKnown : ResolveStatusZeroBitsKnown;
32242527
32252528 return ErrorNone;
32262529}
......@@ -4007,7 +3310,7 @@ TypeStructField *find_struct_type_field(ZigType *type_entry, Buf *name) {
40073310
40083311TypeUnionField *find_union_type_field(ZigType *type_entry, Buf *name) {
40093312 assert(type_entry->id == ZigTypeIdUnion);
4010 assert(type_entry->data.unionation.zero_bits_known);
3313 assert(type_is_resolved(type_entry, ResolveStatusZeroBitsKnown));
40113314 if (type_entry->data.unionation.src_field_count == 0)
40123315 return nullptr;
40133316 auto entry = type_entry->data.unionation.fields_by_name.maybe_get(name);
......@@ -4018,7 +3321,7 @@ TypeUnionField *find_union_type_field(ZigType *type_entry, Buf *name) {
40183321
40193322TypeUnionField *find_union_field_by_tag(ZigType *type_entry, const BigInt *tag) {
40203323 assert(type_entry->id == ZigTypeIdUnion);
4021 assert(type_entry->data.unionation.zero_bits_known);
3324 assert(type_is_resolved(type_entry, ResolveStatusZeroBitsKnown));
40223325 for (uint32_t i = 0; i < type_entry->data.unionation.src_field_count; i += 1) {
40233326 TypeUnionField *field = &type_entry->data.unionation.fields[i];
40243327 if (bigint_cmp(&field->enum_field->value, tag) == CmpEQ) {
......@@ -4626,18 +3929,26 @@ ZigType *get_vector_type(CodeGen *g, uint32_t len, ZigType *elem_type) {
46263929 }
46273930
46283931 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);
3932 if ((len != 0) && type_has_bits(elem_type)) {
3933 // Vectors can only be ints, floats, or pointers. ints and floats have trivially resolvable
3934 // llvm type refs. pointers we will use usize instead.
3935 LLVMTypeRef example_vector_llvm_type;
3936 if (elem_type->id == ZigTypeIdPointer) {
3937 example_vector_llvm_type = LLVMVectorType(g->builtin_types.entry_usize->llvm_type, len);
3938 } else {
3939 example_vector_llvm_type = LLVMVectorType(elem_type->llvm_type, len);
3940 }
3941 assert(example_vector_llvm_type != nullptr);
3942 entry->size_in_bits = elem_type->size_in_bits * len;
3943 entry->abi_size = LLVMABISizeOfType(g->target_data_ref, example_vector_llvm_type);
3944 entry->abi_align = LLVMABIAlignmentOfType(g->target_data_ref, example_vector_llvm_type);
3945 }
46313946 entry->data.vector.len = len;
46323947 entry->data.vector.elem_type = elem_type;
46333948
46343949 buf_resize(&entry->name, 0);
46353950 buf_appendf(&entry->name, "@Vector(%u, %s)", len, buf_ptr(&elem_type->name));
46363951
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
46413952 g->type_table.put(type_id, entry);
46423953 return entry;
46433954}
......@@ -4677,6 +3988,7 @@ bool handle_is_ptr(ZigType *type_entry) {
46773988 return false;
46783989 case ZigTypeIdArray:
46793990 case ZigTypeIdStruct:
3991 case ZigTypeIdUnion:
46803992 return type_has_bits(type_entry);
46813993 case ZigTypeIdErrorUnion:
46823994 return type_has_bits(type_entry->data.error_union.payload_type);
......@@ -4684,13 +3996,6 @@ bool handle_is_ptr(ZigType *type_entry) {
46843996 return type_has_bits(type_entry->data.maybe.child_type) &&
46853997 !type_is_nonnull_ptr(type_entry->data.maybe.child_type) &&
46863998 type_entry->data.maybe.child_type->id != ZigTypeIdErrorSet;
4687 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;
46943999
46954000 }
46964001 zig_unreachable();
......@@ -5165,10 +4470,10 @@ bool fn_eval_eql(Scope *a, Scope *b) {
51654470
51664471// Whether the type has bits at runtime.
51674472bool type_has_bits(ZigType *type_entry) {
5168 assert(type_entry);
4473 assert(type_entry != nullptr);
51694474 assert(!type_is_invalid(type_entry));
51704475 assert(type_is_resolved(type_entry, ResolveStatusZeroBitsKnown));
5171 return !type_entry->zero_bits;
4476 return type_entry->abi_size != 0;
51724477}
51734478
51744479// Whether you can infer the value based solely on the type.
......@@ -5629,7 +4934,7 @@ Error type_resolve(CodeGen *g, ZigType *ty, ResolveStatus status) {
56294934 } else if (ty->id == ZigTypeIdEnum) {
56304935 return resolve_enum_zero_bits(g, ty);
56314936 } else if (ty->id == ZigTypeIdUnion) {
5632 return resolve_union_zero_bits(g, ty);
4937 return resolve_union_alignment(g, ty);
56334938 }
56344939 return ErrorNone;
56354940 case ResolveStatusSizeKnown:
......@@ -6192,31 +5497,18 @@ void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val) {
61925497ZigType *make_int_type(CodeGen *g, bool is_signed, uint32_t size_in_bits) {
61935498 assert(size_in_bits <= 65535);
61945499 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);
5500
5501 entry->size_in_bits = size_in_bits;
5502 if (size_in_bits != 0) {
5503 entry->llvm_type = LLVMIntType(size_in_bits);
5504 entry->abi_size = LLVMABISizeOfType(g->target_data_ref, entry->llvm_type);
5505 entry->abi_align = LLVMABIAlignmentOfType(g->target_data_ref, entry->llvm_type);
5506 }
61975507
61985508 const char u_or_i = is_signed ? 'i' : 'u';
61995509 buf_resize(&entry->name, 0);
62005510 buf_appendf(&entry->name, "%c%" PRIu32, u_or_i, size_in_bits);
62015511
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);
62205512 entry->data.integral.is_signed = is_signed;
62215513 entry->data.integral.bit_count = size_in_bits;
62225514 return entry;
......@@ -6620,26 +5912,6 @@ LinkLib *add_link_lib(CodeGen *g, Buf *name) {
66205912 return link_lib;
66215913}
66225914
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
66435915ZigType *get_align_amt_type(CodeGen *g) {
66445916 if (g->align_amt_type == nullptr) {
66455917 // according to LLVM the maximum alignment is 1 << 29.
......@@ -6841,11 +6113,8 @@ bool type_is_c_abi_int(CodeGen *g, ZigType *ty) {
68416113
68426114uint32_t get_host_int_bytes(CodeGen *g, ZigType *struct_type, TypeStructField *field) {
68436115 assert(struct_type->id == ZigTypeIdStruct);
6844 if (struct_type->data.structure.layout != ContainerLayoutPacked) {
6845 return 0;
6846 }
6847 LLVMTypeRef field_type = LLVMStructGetTypeAtIndex(struct_type->type_ref, field->gen_index);
6848 return LLVMStoreSizeOfType(g->target_data_ref, field_type);
6116 assert(type_is_resolved(struct_type, ResolveStatusSizeKnown));
6117 return struct_type->data.structure.host_int_bytes[field->gen_index];
68496118}
68506119
68516120Error ensure_const_val_repr(IrAnalyze *ira, CodeGen *codegen, AstNode *source_node,
......@@ -6911,3 +6180,822 @@ Buf *type_bare_name(ZigType *type_entry) {
69116180Buf *type_h_name(ZigType *t) {
69126181 return type_bare_name(t);
69136182}
6183
6184static void resolve_llvm_types_slice(CodeGen *g, ZigType *type) {
6185 ZigType *ptr_type = type->data.structure.fields[slice_ptr_index].type_entry;
6186 ZigType *child_type = ptr_type->data.pointer.child_type;
6187
6188 if (ptr_type->data.pointer.is_const || ptr_type->data.pointer.is_volatile ||
6189 ptr_type->data.pointer.explicit_alignment != 0 || ptr_type->data.pointer.allow_zero)
6190 {
6191 ZigType *peer_ptr_type = get_pointer_to_type_extra(g, child_type, false, false,
6192 PtrLenUnknown, 0, 0, 0, false);
6193 ZigType *peer_slice_type = get_slice_type(g, peer_ptr_type);
6194
6195 type->llvm_type = get_llvm_type(g, peer_slice_type);
6196 type->llvm_di_type = get_llvm_di_type(g, peer_slice_type);
6197 }
6198
6199 // If the child type is []const T then we need to make sure the type ref
6200 // and debug info is the same as if the child type were []T.
6201 if (is_slice(child_type)) {
6202 ZigType *child_ptr_type = child_type->data.structure.fields[slice_ptr_index].type_entry;
6203 assert(child_ptr_type->id == ZigTypeIdPointer);
6204 if (child_ptr_type->data.pointer.is_const || child_ptr_type->data.pointer.is_volatile ||
6205 child_ptr_type->data.pointer.explicit_alignment != 0 || child_ptr_type->data.pointer.allow_zero)
6206 {
6207 ZigType *grand_child_type = child_ptr_type->data.pointer.child_type;
6208 ZigType *bland_child_ptr_type = get_pointer_to_type_extra(g, grand_child_type, false, false,
6209 PtrLenUnknown, 0, 0, 0, false);
6210 ZigType *bland_child_slice = get_slice_type(g, bland_child_ptr_type);
6211 ZigType *peer_ptr_type = get_pointer_to_type_extra(g, bland_child_slice, false, false,
6212 PtrLenUnknown, 0, 0, 0, false);
6213 ZigType *peer_slice_type = get_slice_type(g, peer_ptr_type);
6214
6215 type->llvm_type = get_llvm_type(g, peer_slice_type);
6216 type->llvm_di_type = get_llvm_di_type(g, peer_slice_type);
6217 }
6218 }
6219
6220 if (type->llvm_type != nullptr)
6221 return;
6222
6223 ZigType *usize_type = g->builtin_types.entry_usize;
6224 LLVMTypeRef usize_llvm_type = get_llvm_type(g, usize_type);
6225 ZigLLVMDIType *usize_llvm_di_type = get_llvm_di_type(g, usize_type);
6226
6227 type->llvm_type = LLVMStructCreateNamed(LLVMGetGlobalContext(), buf_ptr(&type->name));
6228
6229 ZigLLVMDIScope *compile_unit_scope = ZigLLVMCompileUnitToScope(g->compile_unit);
6230 ZigLLVMDIFile *di_file = nullptr;
6231 unsigned line = 0;
6232 type->llvm_di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
6233 ZigLLVMTag_DW_structure_type(), buf_ptr(&type->name),
6234 compile_unit_scope, di_file, line);
6235
6236 if (!type_has_bits(child_type)) {
6237 LLVMTypeRef element_types[] = {
6238 usize_llvm_type,
6239 };
6240 LLVMStructSetBody(type->llvm_type, element_types, 1, false);
6241
6242 uint64_t len_debug_size_in_bits = usize_type->size_in_bits;
6243 uint64_t len_debug_align_in_bits = 8*usize_type->abi_align;
6244 uint64_t len_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, type->llvm_type, 0);
6245
6246 uint64_t debug_size_in_bits = type->size_in_bits;
6247 uint64_t debug_align_in_bits = 8*type->abi_align;
6248
6249 ZigLLVMDIType *di_element_types[] = {
6250 ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(type->llvm_di_type),
6251 "len", di_file, line,
6252 len_debug_size_in_bits,
6253 len_debug_align_in_bits,
6254 len_offset_in_bits,
6255 0, usize_llvm_di_type),
6256 };
6257 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
6258 compile_unit_scope,
6259 buf_ptr(&type->name),
6260 di_file, line, debug_size_in_bits, debug_align_in_bits, 0,
6261 nullptr, di_element_types, 1, 0, nullptr, "");
6262
6263 ZigLLVMReplaceTemporary(g->dbuilder, type->llvm_di_type, replacement_di_type);
6264 type->llvm_di_type = replacement_di_type;
6265 return;
6266 }
6267
6268 LLVMTypeRef element_types[2];
6269 element_types[slice_ptr_index] = get_llvm_type(g, ptr_type);
6270 element_types[slice_len_index] = get_llvm_type(g, g->builtin_types.entry_usize);
6271 LLVMStructSetBody(type->llvm_type, element_types, 2, false);
6272
6273 uint64_t ptr_debug_size_in_bits = ptr_type->size_in_bits;
6274 uint64_t ptr_debug_align_in_bits = 8*ptr_type->abi_align;
6275 uint64_t ptr_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, type->llvm_type, 0);
6276
6277 uint64_t len_debug_size_in_bits = usize_type->size_in_bits;
6278 uint64_t len_debug_align_in_bits = 8*usize_type->abi_align;
6279 uint64_t len_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, type->llvm_type, 1);
6280
6281 uint64_t debug_size_in_bits = type->size_in_bits;
6282 uint64_t debug_align_in_bits = 8*type->abi_align;
6283
6284 ZigLLVMDIType *di_element_types[] = {
6285 ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(type->llvm_di_type),
6286 "ptr", di_file, line,
6287 ptr_debug_size_in_bits,
6288 ptr_debug_align_in_bits,
6289 ptr_offset_in_bits,
6290 0, get_llvm_di_type(g, ptr_type)),
6291 ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(type->llvm_di_type),
6292 "len", di_file, line,
6293 len_debug_size_in_bits,
6294 len_debug_align_in_bits,
6295 len_offset_in_bits,
6296 0, usize_llvm_di_type),
6297 };
6298 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
6299 compile_unit_scope,
6300 buf_ptr(&type->name),
6301 di_file, line, debug_size_in_bits, debug_align_in_bits, 0,
6302 nullptr, di_element_types, 2, 0, nullptr, "");
6303
6304 ZigLLVMReplaceTemporary(g->dbuilder, type->llvm_di_type, replacement_di_type);
6305 type->llvm_di_type = replacement_di_type;
6306}
6307
6308static void resolve_llvm_types_struct(CodeGen *g, ZigType *struct_type) {
6309 assert(struct_type->id == ZigTypeIdStruct);
6310 assert(struct_type->data.structure.resolve_status != ResolveStatusInvalid);
6311 assert(struct_type->data.structure.resolve_status >= ResolveStatusSizeKnown);
6312 assert(struct_type->data.structure.fields || struct_type->data.structure.src_field_count == 0);
6313
6314 AstNode *decl_node = struct_type->data.structure.decl_node;
6315 assert(decl_node->type == NodeTypeContainerDecl);
6316
6317 size_t field_count = struct_type->data.structure.src_field_count;
6318 size_t gen_field_count = struct_type->data.structure.gen_field_count;
6319 LLVMTypeRef *element_types = allocate<LLVMTypeRef>(gen_field_count);
6320
6321 Scope *scope = &struct_type->data.structure.decls_scope->base;
6322
6323 size_t gen_field_index = 0;
6324 bool packed = (struct_type->data.structure.layout == ContainerLayoutPacked);
6325 size_t packed_bits_offset = 0;
6326 size_t first_packed_bits_offset_misalign = SIZE_MAX;
6327 size_t debug_field_count = 0;
6328
6329 for (size_t i = 0; i < field_count; i += 1) {
6330 TypeStructField *type_struct_field = &struct_type->data.structure.fields[i];
6331 ZigType *field_type = type_struct_field->type_entry;
6332
6333 if (!type_has_bits(field_type))
6334 continue;
6335
6336 if (packed) {
6337 size_t field_size_in_bits = type_size_bits(g, field_type);
6338 size_t next_packed_bits_offset = packed_bits_offset + field_size_in_bits;
6339
6340 if (first_packed_bits_offset_misalign != SIZE_MAX) {
6341 // this field is not byte-aligned; it is part of the previous field with a bit offset
6342
6343 size_t full_bit_count = next_packed_bits_offset - first_packed_bits_offset_misalign;
6344 if (get_store_size_in_bits(full_bit_count) == full_bit_count) {
6345 // next field recovers store alignment
6346 element_types[gen_field_index] = LLVMIntType((unsigned)(full_bit_count));
6347 gen_field_index += 1;
6348
6349 first_packed_bits_offset_misalign = SIZE_MAX;
6350 }
6351 } else if (get_store_size_in_bits(field_type->size_in_bits) != field_size_in_bits) {
6352 first_packed_bits_offset_misalign = packed_bits_offset;
6353 } else {
6354 // This is a byte-aligned field (both start and end) in a packed struct.
6355 element_types[gen_field_index] = get_llvm_type(g, field_type);
6356 gen_field_index += 1;
6357 }
6358 packed_bits_offset = next_packed_bits_offset;
6359 } else {
6360 element_types[gen_field_index] = get_llvm_type(g, field_type);
6361
6362 gen_field_index += 1;
6363 }
6364 debug_field_count += 1;
6365 }
6366 if (first_packed_bits_offset_misalign != SIZE_MAX) {
6367 size_t full_bit_count = packed_bits_offset - first_packed_bits_offset_misalign;
6368 size_t store_bit_count = get_store_size_in_bits(full_bit_count);
6369 element_types[gen_field_index] = LLVMIntType((unsigned)store_bit_count);
6370 gen_field_index += 1;
6371 }
6372
6373 struct_type->llvm_type = LLVMStructCreateNamed(LLVMGetGlobalContext(), buf_ptr(&struct_type->name));
6374 LLVMStructSetBody(struct_type->llvm_type, element_types, (unsigned)gen_field_count, packed);
6375
6376 ZigLLVMDIType **di_element_types = allocate<ZigLLVMDIType*>(debug_field_count);
6377
6378 ZigType *import = get_scope_import(scope);
6379 size_t debug_field_index = 0;
6380 for (size_t i = 0; i < field_count; i += 1) {
6381 AstNode *field_node = decl_node->data.container_decl.fields.at(i);
6382 TypeStructField *type_struct_field = &struct_type->data.structure.fields[i];
6383 size_t gen_field_index = type_struct_field->gen_index;
6384 if (gen_field_index == SIZE_MAX) {
6385 continue;
6386 }
6387
6388 ZigType *field_type = type_struct_field->type_entry;
6389
6390 // if the field is a function, actually the debug info should be a pointer.
6391 ZigLLVMDIType *field_di_type;
6392 if (field_type->id == ZigTypeIdFn) {
6393 ZigType *field_ptr_type = get_pointer_to_type(g, field_type, true);
6394 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, get_llvm_type(g, field_ptr_type));
6395 uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, get_llvm_type(g, field_ptr_type));
6396 field_di_type = ZigLLVMCreateDebugPointerType(g->dbuilder, get_llvm_di_type(g, field_type),
6397 debug_size_in_bits, debug_align_in_bits, buf_ptr(&field_ptr_type->name));
6398 } else {
6399 field_di_type = get_llvm_di_type(g, field_type);
6400 }
6401
6402 uint64_t debug_size_in_bits;
6403 uint64_t debug_align_in_bits;
6404 uint64_t debug_offset_in_bits;
6405 if (packed) {
6406 debug_size_in_bits = type_struct_field->type_entry->size_in_bits;
6407 debug_align_in_bits = 8 * type_struct_field->type_entry->abi_align;
6408 debug_offset_in_bits = 8 * type_struct_field->offset + type_struct_field->bit_offset_in_host;
6409 } else {
6410 debug_size_in_bits = get_store_size_in_bits(field_type->size_in_bits);
6411 debug_align_in_bits = 8 * field_type->abi_align;
6412 debug_offset_in_bits = 8 * type_struct_field->offset;
6413 }
6414 di_element_types[debug_field_index] = ZigLLVMCreateDebugMemberType(g->dbuilder,
6415 ZigLLVMTypeToScope(struct_type->llvm_di_type), buf_ptr(type_struct_field->name),
6416 import->data.structure.root_struct->di_file, (unsigned)(field_node->line + 1),
6417 debug_size_in_bits,
6418 debug_align_in_bits,
6419 debug_offset_in_bits,
6420 0, field_di_type);
6421 assert(di_element_types[debug_field_index]);
6422 debug_field_index += 1;
6423 }
6424
6425 uint64_t debug_size_in_bits = get_store_size_in_bits(struct_type->size_in_bits);
6426 uint64_t debug_align_in_bits = 8*struct_type->abi_align;
6427 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
6428 ZigLLVMFileToScope(import->data.structure.root_struct->di_file),
6429 buf_ptr(&struct_type->name),
6430 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
6431 debug_size_in_bits,
6432 debug_align_in_bits,
6433 0, nullptr, di_element_types, (int)debug_field_count, 0, nullptr, "");
6434
6435 ZigLLVMReplaceTemporary(g->dbuilder, struct_type->llvm_di_type, replacement_di_type);
6436 struct_type->llvm_di_type = replacement_di_type;
6437}
6438
6439static void resolve_llvm_types_enum(CodeGen *g, ZigType *enum_type) {
6440 assert(!enum_type->data.enumeration.is_invalid);
6441 assert(enum_type->data.enumeration.complete);
6442
6443 uint32_t field_count = enum_type->data.enumeration.src_field_count;
6444
6445 assert(enum_type->data.enumeration.fields);
6446 ZigLLVMDIEnumerator **di_enumerators = allocate<ZigLLVMDIEnumerator*>(field_count);
6447
6448 Scope *scope = &enum_type->data.enumeration.decls_scope->base;
6449 ZigType *import = get_scope_import(scope);
6450
6451 for (uint32_t i = 0; i < field_count; i += 1) {
6452 TypeEnumField *enum_field = &enum_type->data.enumeration.fields[i];
6453
6454 // TODO send patch to LLVM to support APInt in createEnumerator instead of int64_t
6455 // http://lists.llvm.org/pipermail/llvm-dev/2017-December/119456.html
6456 di_enumerators[i] = ZigLLVMCreateDebugEnumerator(g->dbuilder, buf_ptr(enum_field->name),
6457 bigint_as_signed(&enum_field->value));
6458 }
6459
6460 ZigType *tag_int_type = enum_type->data.enumeration.tag_int_type;
6461
6462 // create debug type for tag
6463 AstNode *decl_node = enum_type->data.enumeration.decl_node;
6464 uint64_t tag_debug_size_in_bits = tag_int_type->size_in_bits;
6465 uint64_t tag_debug_align_in_bits = 8*tag_int_type->abi_align;
6466 ZigLLVMDIType *tag_di_type = ZigLLVMCreateDebugEnumerationType(g->dbuilder,
6467 ZigLLVMFileToScope(import->data.structure.root_struct->di_file), buf_ptr(&enum_type->name),
6468 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
6469 tag_debug_size_in_bits,
6470 tag_debug_align_in_bits,
6471 di_enumerators, field_count,
6472 get_llvm_di_type(g, tag_int_type), "");
6473
6474 enum_type->llvm_di_type = tag_di_type;
6475}
6476
6477static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type) {
6478 ZigType *most_aligned_union_member = union_type->data.unionation.most_aligned_union_member;
6479 ZigType *tag_type = union_type->data.unionation.tag_type;
6480 if (tag_type == nullptr || !type_has_bits(tag_type)) {
6481 assert(most_aligned_union_member != nullptr);
6482 assert(union_type->data.unionation.union_abi_size >= most_aligned_union_member->abi_size);
6483 union_type->llvm_type = get_llvm_type(g, most_aligned_union_member);
6484 union_type->llvm_di_type = get_llvm_di_type(g, most_aligned_union_member);
6485 return;
6486 }
6487 if (most_aligned_union_member == nullptr) {
6488 union_type->llvm_type = get_llvm_type(g, tag_type);
6489 union_type->llvm_di_type = get_llvm_di_type(g, tag_type);
6490 return;
6491 }
6492
6493 Scope *scope = &union_type->data.unionation.decls_scope->base;
6494 ZigType *import = get_scope_import(scope);
6495 AstNode *decl_node = union_type->data.unionation.decl_node;
6496 size_t line = decl_node ? decl_node->line : 0;
6497 unsigned dwarf_kind = ZigLLVMTag_DW_structure_type();
6498 union_type->llvm_di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
6499 dwarf_kind, buf_ptr(&union_type->name),
6500 ZigLLVMFileToScope(import->data.structure.root_struct->di_file),
6501 import->data.structure.root_struct->di_file, (unsigned)(line + 1));
6502
6503 uint32_t gen_field_count = union_type->data.unionation.gen_field_count;
6504 ZigLLVMDIType **union_inner_di_types = allocate<ZigLLVMDIType*>(gen_field_count);
6505 uint32_t field_count = union_type->data.unionation.src_field_count;
6506 for (uint32_t i = 0; i < field_count; i += 1) {
6507 TypeUnionField *union_field = &union_type->data.unionation.fields[i];
6508 if (!type_has_bits(union_field->type_entry))
6509 continue;
6510
6511 uint64_t store_size_in_bits = union_field->type_entry->size_in_bits;
6512 uint64_t abi_align_in_bits = 8*union_field->type_entry->abi_align;
6513 AstNode *field_node = decl_node->data.container_decl.fields.at(i);
6514 union_inner_di_types[union_field->gen_index] = ZigLLVMCreateDebugMemberType(g->dbuilder,
6515 ZigLLVMTypeToScope(union_type->llvm_di_type), buf_ptr(union_field->enum_field->name),
6516 import->data.structure.root_struct->di_file, (unsigned)(field_node->line + 1),
6517 store_size_in_bits,
6518 abi_align_in_bits,
6519 0,
6520 0, get_llvm_di_type(g, union_field->type_entry));
6521
6522 }
6523 union_type->llvm_type = LLVMStructCreateNamed(LLVMGetGlobalContext(), buf_ptr(&union_type->name));
6524
6525 LLVMTypeRef union_type_ref;
6526 size_t padding_bytes = union_type->data.unionation.union_abi_size - most_aligned_union_member->abi_size;
6527 if (padding_bytes == 0) {
6528 union_type_ref = get_llvm_type(g, most_aligned_union_member);
6529 } else {
6530 ZigType *u8_type = get_int_type(g, false, 8);
6531 ZigType *padding_array = get_array_type(g, u8_type, padding_bytes);
6532 LLVMTypeRef union_element_types[] = {
6533 get_llvm_type(g, most_aligned_union_member),
6534 get_llvm_type(g, padding_array),
6535 };
6536 union_type_ref = LLVMStructType(union_element_types, 2, false);
6537 }
6538 union_type->data.unionation.union_llvm_type = union_type_ref;
6539
6540 LLVMTypeRef root_struct_element_types[2];
6541 root_struct_element_types[union_type->data.unionation.gen_tag_index] = get_llvm_type(g, tag_type);
6542 root_struct_element_types[union_type->data.unionation.gen_union_index] = union_type_ref;
6543 LLVMStructSetBody(union_type->llvm_type, root_struct_element_types, 2, false);
6544
6545 // create debug type for union
6546 ZigLLVMDIType *union_di_type = ZigLLVMCreateDebugUnionType(g->dbuilder,
6547 ZigLLVMTypeToScope(union_type->llvm_di_type), "AnonUnion",
6548 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
6549 most_aligned_union_member->size_in_bits, 8*most_aligned_union_member->abi_align,
6550 0, union_inner_di_types, gen_field_count, 0, "");
6551
6552 uint64_t union_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, union_type->llvm_type,
6553 union_type->data.unionation.gen_union_index);
6554 uint64_t tag_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, union_type->llvm_type,
6555 union_type->data.unionation.gen_tag_index);
6556
6557 ZigLLVMDIType *union_member_di_type = ZigLLVMCreateDebugMemberType(g->dbuilder,
6558 ZigLLVMTypeToScope(union_type->llvm_di_type), "payload",
6559 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
6560 most_aligned_union_member->size_in_bits,
6561 8*most_aligned_union_member->abi_align,
6562 union_offset_in_bits,
6563 0, union_di_type);
6564
6565 uint64_t tag_debug_size_in_bits = tag_type->size_in_bits;
6566 uint64_t tag_debug_align_in_bits = 8*tag_type->abi_align;
6567
6568 ZigLLVMDIType *tag_member_di_type = ZigLLVMCreateDebugMemberType(g->dbuilder,
6569 ZigLLVMTypeToScope(union_type->llvm_di_type), "tag",
6570 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
6571 tag_debug_size_in_bits,
6572 tag_debug_align_in_bits,
6573 tag_offset_in_bits,
6574 0, get_llvm_di_type(g, tag_type));
6575
6576 ZigLLVMDIType *di_root_members[2];
6577 di_root_members[union_type->data.unionation.gen_tag_index] = tag_member_di_type;
6578 di_root_members[union_type->data.unionation.gen_union_index] = union_member_di_type;
6579
6580 uint64_t debug_size_in_bits = union_type->size_in_bits;
6581 uint64_t debug_align_in_bits = 8*union_type->abi_align;
6582 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
6583 ZigLLVMFileToScope(import->data.structure.root_struct->di_file),
6584 buf_ptr(&union_type->name),
6585 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
6586 debug_size_in_bits,
6587 debug_align_in_bits,
6588 0, nullptr, di_root_members, 2, 0, nullptr, "");
6589
6590 ZigLLVMReplaceTemporary(g->dbuilder, union_type->llvm_di_type, replacement_di_type);
6591 union_type->llvm_di_type = replacement_di_type;
6592}
6593
6594static void resolve_llvm_types_pointer(CodeGen *g, ZigType *type) {
6595 ZigType *elem_type = type->data.pointer.child_type;
6596
6597 if (type->data.pointer.is_const || type->data.pointer.is_volatile ||
6598 type->data.pointer.explicit_alignment != 0 || type->data.pointer.ptr_len != PtrLenSingle ||
6599 type->data.pointer.bit_offset_in_host != 0 || type->data.pointer.allow_zero)
6600 {
6601 ZigType *peer_type = get_pointer_to_type_extra(g, elem_type, false, false,
6602 PtrLenSingle, 0, 0, type->data.pointer.host_int_bytes, false);
6603 type->llvm_type = get_llvm_type(g, peer_type);
6604 type->llvm_di_type = get_llvm_di_type(g, peer_type);
6605 return;
6606 }
6607
6608 if (type->data.pointer.host_int_bytes == 0) {
6609 type->llvm_type = LLVMPointerType(get_llvm_type(g, elem_type), 0);
6610 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, type->llvm_type);
6611 uint64_t debug_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, type->llvm_type);
6612 type->llvm_di_type = ZigLLVMCreateDebugPointerType(g->dbuilder, get_llvm_di_type(g, elem_type),
6613 debug_size_in_bits, debug_align_in_bits, buf_ptr(&type->name));
6614 } else {
6615 ZigType *host_int_type = get_int_type(g, false, type->data.pointer.host_int_bytes * 8);
6616 LLVMTypeRef host_int_llvm_type = get_llvm_type(g, host_int_type);
6617 type->llvm_type = LLVMPointerType(host_int_llvm_type, 0);
6618 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, host_int_llvm_type);
6619 uint64_t debug_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, host_int_llvm_type);
6620 type->llvm_di_type = ZigLLVMCreateDebugPointerType(g->dbuilder, get_llvm_di_type(g, host_int_type),
6621 debug_size_in_bits, debug_align_in_bits, buf_ptr(&type->name));
6622 }
6623}
6624
6625static void resolve_llvm_types_integer(CodeGen *g, ZigType *type) {
6626 unsigned dwarf_tag;
6627 if (type->data.integral.is_signed) {
6628 if (type->size_in_bits == 8) {
6629 dwarf_tag = ZigLLVMEncoding_DW_ATE_signed_char();
6630 } else {
6631 dwarf_tag = ZigLLVMEncoding_DW_ATE_signed();
6632 }
6633 } else {
6634 if (type->size_in_bits == 8) {
6635 dwarf_tag = ZigLLVMEncoding_DW_ATE_unsigned_char();
6636 } else {
6637 dwarf_tag = ZigLLVMEncoding_DW_ATE_unsigned();
6638 }
6639 }
6640
6641 type->llvm_di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&type->name), type->size_in_bits, dwarf_tag);
6642 type->llvm_type = LLVMIntType(type->size_in_bits);
6643}
6644
6645static void resolve_llvm_types_optional(CodeGen *g, ZigType *type) {
6646 LLVMTypeRef bool_llvm_type = get_llvm_type(g, g->builtin_types.entry_bool);
6647 ZigLLVMDIType *bool_llvm_di_type = get_llvm_di_type(g, g->builtin_types.entry_bool);
6648
6649 ZigType *child_type = type->data.maybe.child_type;
6650 if (!type_has_bits(child_type)) {
6651 type->llvm_type = bool_llvm_type;
6652 type->llvm_di_type = bool_llvm_di_type;
6653 return;
6654 }
6655
6656 LLVMTypeRef child_llvm_type = get_llvm_type(g, child_type);
6657 ZigLLVMDIType *child_llvm_di_type = get_llvm_di_type(g, child_type);
6658
6659 if (type_is_nonnull_ptr(child_type) || child_type->id == ZigTypeIdErrorSet) {
6660 type->llvm_type = child_llvm_type;
6661 type->llvm_di_type = child_llvm_di_type;
6662 return;
6663 }
6664
6665 LLVMTypeRef elem_types[] = {
6666 get_llvm_type(g, child_type),
6667 LLVMInt1Type(),
6668 };
6669 type->llvm_type = LLVMStructType(elem_types, 2, false);
6670
6671 ZigLLVMDIScope *compile_unit_scope = ZigLLVMCompileUnitToScope(g->compile_unit);
6672 ZigLLVMDIFile *di_file = nullptr;
6673 unsigned line = 0;
6674 type->llvm_di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
6675 ZigLLVMTag_DW_structure_type(), buf_ptr(&type->name),
6676 compile_unit_scope, di_file, line);
6677
6678 uint64_t val_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, child_llvm_type);
6679 uint64_t val_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, child_llvm_type);
6680 uint64_t val_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, type->llvm_type, 0);
6681
6682 uint64_t maybe_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, bool_llvm_type);
6683 uint64_t maybe_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, bool_llvm_type);
6684 uint64_t maybe_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, type->llvm_type, 1);
6685
6686 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, type->llvm_type);
6687 uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, type->llvm_type);
6688
6689 ZigLLVMDIType *di_element_types[] = {
6690 ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(type->llvm_di_type),
6691 "val", di_file, line,
6692 val_debug_size_in_bits,
6693 val_debug_align_in_bits,
6694 val_offset_in_bits,
6695 0, child_llvm_di_type),
6696 ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(type->llvm_di_type),
6697 "maybe", di_file, line,
6698 maybe_debug_size_in_bits,
6699 maybe_debug_align_in_bits,
6700 maybe_offset_in_bits,
6701 0, bool_llvm_di_type),
6702 };
6703 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
6704 compile_unit_scope,
6705 buf_ptr(&type->name),
6706 di_file, line, debug_size_in_bits, debug_align_in_bits, 0,
6707 nullptr, di_element_types, 2, 0, nullptr, "");
6708
6709 ZigLLVMReplaceTemporary(g->dbuilder, type->llvm_di_type, replacement_di_type);
6710 type->llvm_di_type = replacement_di_type;
6711}
6712
6713static void resolve_llvm_types_error_union(CodeGen *g, ZigType *type) {
6714 ZigType *payload_type = type->data.error_union.payload_type;
6715 ZigType *err_set_type = type->data.error_union.err_set_type;
6716
6717 if (!type_has_bits(payload_type)) {
6718 assert(type_has_bits(err_set_type));
6719 type->llvm_type = get_llvm_type(g, err_set_type);
6720 type->llvm_di_type = get_llvm_di_type(g, err_set_type);
6721 } else if (!type_has_bits(err_set_type)) {
6722 type->llvm_type = get_llvm_type(g, payload_type);
6723 type->llvm_di_type = get_llvm_di_type(g, payload_type);
6724 } else {
6725 LLVMTypeRef err_set_llvm_type = get_llvm_type(g, err_set_type);
6726 LLVMTypeRef payload_llvm_type = get_llvm_type(g, payload_type);
6727 LLVMTypeRef elem_types[2];
6728 elem_types[err_union_err_index] = err_set_llvm_type;
6729 elem_types[err_union_payload_index] = payload_llvm_type;
6730 type->llvm_type = LLVMStructType(elem_types, 2, false);
6731
6732 ZigLLVMDIScope *compile_unit_scope = ZigLLVMCompileUnitToScope(g->compile_unit);
6733 ZigLLVMDIFile *di_file = nullptr;
6734 unsigned line = 0;
6735 type->llvm_di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
6736 ZigLLVMTag_DW_structure_type(), buf_ptr(&type->name),
6737 compile_unit_scope, di_file, line);
6738
6739 uint64_t tag_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, err_set_llvm_type);
6740 uint64_t tag_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, err_set_llvm_type);
6741 uint64_t tag_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, type->llvm_type, err_union_err_index);
6742
6743 uint64_t value_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, payload_llvm_type);
6744 uint64_t value_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, payload_llvm_type);
6745 uint64_t value_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, type->llvm_type,
6746 err_union_payload_index);
6747
6748 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, type->llvm_type);
6749 uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, type->llvm_type);
6750
6751 ZigLLVMDIType *di_element_types[] = {
6752 ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(type->llvm_di_type),
6753 "tag", di_file, line,
6754 tag_debug_size_in_bits,
6755 tag_debug_align_in_bits,
6756 tag_offset_in_bits,
6757 0, get_llvm_di_type(g, err_set_type)),
6758 ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(type->llvm_di_type),
6759 "value", di_file, line,
6760 value_debug_size_in_bits,
6761 value_debug_align_in_bits,
6762 value_offset_in_bits,
6763 0, get_llvm_di_type(g, payload_type)),
6764 };
6765
6766 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
6767 compile_unit_scope,
6768 buf_ptr(&type->name),
6769 di_file, line,
6770 debug_size_in_bits,
6771 debug_align_in_bits,
6772 0,
6773 nullptr, di_element_types, 2, 0, nullptr, "");
6774
6775 ZigLLVMReplaceTemporary(g->dbuilder, type->llvm_di_type, replacement_di_type);
6776 type->llvm_di_type = replacement_di_type;
6777 }
6778}
6779
6780static void resolve_llvm_types_array(CodeGen *g, ZigType *type) {
6781 ZigType *elem_type = type->data.array.child_type;
6782
6783 // TODO https://github.com/ziglang/zig/issues/1424
6784 type->llvm_type = LLVMArrayType(get_llvm_type(g, elem_type), (unsigned)type->data.array.len);
6785
6786 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, type->llvm_type);
6787 uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, type->llvm_type);
6788
6789 type->llvm_di_type = ZigLLVMCreateDebugArrayType(g->dbuilder, debug_size_in_bits,
6790 debug_align_in_bits, get_llvm_di_type(g, elem_type), (int)type->data.array.len);
6791}
6792
6793static void resolve_llvm_types_fn(CodeGen *g, ZigType *fn_type) {
6794 FnTypeId *fn_type_id = &fn_type->data.fn.fn_type_id;
6795 bool first_arg_return = want_first_arg_sret(g, fn_type_id);
6796 bool is_async = fn_type_id->cc == CallingConventionAsync;
6797 bool is_c_abi = fn_type_id->cc == CallingConventionC;
6798 bool prefix_arg_error_return_trace = g->have_err_ret_tracing && fn_type_can_fail(fn_type_id);
6799 // +1 for maybe making the first argument the return value
6800 // +1 for maybe first argument the error return trace
6801 // +2 for maybe arguments async allocator and error code pointer
6802 ZigList<LLVMTypeRef> gen_param_types = {};
6803 // +1 because 0 is the return type and
6804 // +1 for maybe making first arg ret val and
6805 // +1 for maybe first argument the error return trace
6806 // +2 for maybe arguments async allocator and error code pointer
6807 ZigList<ZigLLVMDIType *> param_di_types = {};
6808 param_di_types.append(get_llvm_di_type(g, fn_type_id->return_type));
6809 ZigType *gen_return_type;
6810 if (is_async) {
6811 gen_return_type = get_pointer_to_type(g, g->builtin_types.entry_u8, false);
6812 } else if (!type_has_bits(fn_type_id->return_type)) {
6813 gen_return_type = g->builtin_types.entry_void;
6814 } else if (first_arg_return) {
6815 ZigType *gen_type = get_pointer_to_type(g, fn_type_id->return_type, false);
6816 gen_param_types.append(get_llvm_type(g, gen_type));
6817 param_di_types.append(get_llvm_di_type(g, gen_type));
6818 gen_return_type = g->builtin_types.entry_void;
6819 } else {
6820 gen_return_type = fn_type_id->return_type;
6821 }
6822 fn_type->data.fn.gen_return_type = gen_return_type;
6823
6824 if (prefix_arg_error_return_trace) {
6825 ZigType *gen_type = get_ptr_to_stack_trace_type(g);
6826 gen_param_types.append(get_llvm_type(g, gen_type));
6827 param_di_types.append(get_llvm_di_type(g, gen_type));
6828 }
6829 if (is_async) {
6830 {
6831 // async allocator param
6832 ZigType *gen_type = fn_type_id->async_allocator_type;
6833 gen_param_types.append(get_llvm_type(g, gen_type));
6834 param_di_types.append(get_llvm_di_type(g, gen_type));
6835 }
6836
6837 {
6838 // error code pointer
6839 ZigType *gen_type = get_pointer_to_type(g, g->builtin_types.entry_global_error_set, false);
6840 gen_param_types.append(get_llvm_type(g, gen_type));
6841 param_di_types.append(get_llvm_di_type(g, gen_type));
6842 }
6843 }
6844
6845 fn_type->data.fn.gen_param_info = allocate<FnGenParamInfo>(fn_type_id->param_count);
6846 for (size_t i = 0; i < fn_type_id->param_count; i += 1) {
6847 FnTypeParamInfo *src_param_info = &fn_type->data.fn.fn_type_id.param_info[i];
6848 ZigType *type_entry = src_param_info->type;
6849 FnGenParamInfo *gen_param_info = &fn_type->data.fn.gen_param_info[i];
6850
6851 gen_param_info->src_index = i;
6852 gen_param_info->gen_index = SIZE_MAX;
6853
6854 if (!type_has_bits(type_entry))
6855 continue;
6856
6857 ZigType *gen_type;
6858 if (handle_is_ptr(type_entry)) {
6859 gen_type = get_pointer_to_type(g, type_entry, true);
6860 gen_param_info->is_byval = true;
6861 } else {
6862 gen_type = type_entry;
6863 }
6864 gen_param_info->gen_index = gen_param_types.length;
6865 gen_param_info->type = gen_type;
6866 gen_param_types.append(get_llvm_type(g, gen_type));
6867
6868 param_di_types.append(get_llvm_di_type(g, gen_type));
6869 }
6870
6871 if (is_c_abi) {
6872 FnWalk fn_walk = {};
6873 fn_walk.id = FnWalkIdTypes;
6874 fn_walk.data.types.param_di_types = &param_di_types;
6875 fn_walk.data.types.gen_param_types = &gen_param_types;
6876 walk_function_params(g, fn_type, &fn_walk);
6877 }
6878
6879 fn_type->data.fn.gen_param_count = gen_param_types.length;
6880
6881 for (size_t i = 0; i < gen_param_types.length; i += 1) {
6882 assert(gen_param_types.items[i] != nullptr);
6883 }
6884 fn_type->data.fn.raw_type_ref = LLVMFunctionType(get_llvm_type(g, gen_return_type),
6885 gen_param_types.items, (unsigned int)gen_param_types.length, fn_type_id->is_var_args);
6886 fn_type->llvm_type = LLVMPointerType(fn_type->data.fn.raw_type_ref, 0);
6887 fn_type->data.fn.raw_di_type = ZigLLVMCreateSubroutineType(g->dbuilder, param_di_types.items, (int)param_di_types.length, 0);
6888 fn_type->llvm_di_type = ZigLLVMCreateDebugPointerType(g->dbuilder, fn_type->data.fn.raw_di_type,
6889 LLVMStoreSizeOfType(g->target_data_ref, fn_type->llvm_type),
6890 LLVMABIAlignmentOfType(g->target_data_ref, fn_type->llvm_type), "");
6891}
6892
6893static void resolve_llvm_types_anyerror(CodeGen *g) {
6894 ZigType *entry = g->builtin_types.entry_global_error_set;
6895 entry->llvm_type = get_llvm_type(g, g->err_tag_type);
6896 ZigList<ZigLLVMDIEnumerator *> err_enumerators;
6897 // reserve index 0 to indicate no error
6898 err_enumerators.append(ZigLLVMCreateDebugEnumerator(g->dbuilder, "(none)", 0));
6899 for (size_t i = 1; i < g->errors_by_index.length; i += 1) {
6900 ErrorTableEntry *error_entry = g->errors_by_index.at(i);
6901 err_enumerators.append(ZigLLVMCreateDebugEnumerator(g->dbuilder, buf_ptr(&error_entry->name), i));
6902 }
6903
6904 // create debug type for error sets
6905 uint64_t tag_debug_size_in_bits = g->err_tag_type->size_in_bits;
6906 uint64_t tag_debug_align_in_bits = 8*g->err_tag_type->abi_align;
6907 ZigLLVMDIFile *err_set_di_file = nullptr;
6908 entry->llvm_di_type = ZigLLVMCreateDebugEnumerationType(g->dbuilder,
6909 ZigLLVMCompileUnitToScope(g->compile_unit), buf_ptr(&entry->name),
6910 err_set_di_file, 0,
6911 tag_debug_size_in_bits,
6912 tag_debug_align_in_bits,
6913 err_enumerators.items, err_enumerators.length,
6914 get_llvm_di_type(g, g->err_tag_type), "");
6915}
6916
6917static void resolve_llvm_types(CodeGen *g, ZigType *type) {
6918 assert(type_is_resolved(type, ResolveStatusSizeKnown));
6919 assert(type_has_bits(type));
6920 switch (type->id) {
6921 case ZigTypeIdInvalid:
6922 case ZigTypeIdFloat:
6923 case ZigTypeIdOpaque:
6924 case ZigTypeIdMetaType:
6925 case ZigTypeIdVoid:
6926 case ZigTypeIdBool:
6927 case ZigTypeIdUnreachable:
6928 case ZigTypeIdComptimeFloat:
6929 case ZigTypeIdComptimeInt:
6930 case ZigTypeIdEnumLiteral:
6931 case ZigTypeIdUndefined:
6932 case ZigTypeIdNull:
6933 case ZigTypeIdBoundFn:
6934 case ZigTypeIdArgTuple:
6935 zig_unreachable();
6936 case ZigTypeIdStruct:
6937 if (type->data.structure.is_slice)
6938 return resolve_llvm_types_slice(g, type);
6939 else
6940 return resolve_llvm_types_struct(g, type);
6941 case ZigTypeIdEnum:
6942 return resolve_llvm_types_enum(g, type);
6943 case ZigTypeIdUnion:
6944 return resolve_llvm_types_union(g, type);
6945 case ZigTypeIdPointer:
6946 return resolve_llvm_types_pointer(g, type);
6947 case ZigTypeIdPromise: {
6948 ZigType *u8_ptr_type = get_pointer_to_type(g, g->builtin_types.entry_u8, false);
6949 type->llvm_type = get_llvm_type(g, u8_ptr_type);
6950 type->llvm_di_type = get_llvm_di_type(g, u8_ptr_type);
6951 return;
6952 }
6953 case ZigTypeIdInt:
6954 return resolve_llvm_types_integer(g, type);
6955 case ZigTypeIdOptional:
6956 return resolve_llvm_types_optional(g, type);
6957 case ZigTypeIdErrorUnion:
6958 return resolve_llvm_types_error_union(g, type);
6959 case ZigTypeIdArray:
6960 return resolve_llvm_types_array(g, type);
6961 case ZigTypeIdFn:
6962 return resolve_llvm_types_fn(g, type);
6963 case ZigTypeIdErrorSet: {
6964 if (g->builtin_types.entry_global_error_set->llvm_type == nullptr) {
6965 resolve_llvm_types_anyerror(g);
6966 }
6967 type->llvm_type = g->builtin_types.entry_global_error_set->llvm_type;
6968 type->llvm_di_type = g->builtin_types.entry_global_error_set->llvm_di_type;
6969 return;
6970 }
6971 case ZigTypeIdVector: {
6972 type->llvm_type = LLVMVectorType(get_llvm_type(g, type->data.vector.elem_type), type->data.vector.len);
6973 type->llvm_di_type = ZigLLVMDIBuilderCreateVectorType(g->dbuilder, type->size_in_bits,
6974 type->abi_align, get_llvm_di_type(g, type->data.vector.elem_type), type->data.vector.len);
6975 return;
6976 }
6977 }
6978 zig_unreachable();
6979}
6980
6981LLVMTypeRef get_llvm_type(CodeGen *g, ZigType *type) {
6982 if (type->llvm_type != nullptr)
6983 return type->llvm_type;
6984 resolve_llvm_types(g, type);
6985 assert(type->llvm_type != nullptr);
6986 assert(type->llvm_di_type != nullptr);
6987 assert(type->abi_size == LLVMABISizeOfType(g->target_data_ref, type->llvm_type));
6988 assert(type->abi_align == LLVMABIAlignmentOfType(g->target_data_ref, type->llvm_type));
6989 return type->llvm_type;
6990}
6991
6992ZigLLVMDIType *get_llvm_di_type(CodeGen *g, ZigType *type) {
6993 if (type->llvm_di_type != nullptr)
6994 return type->llvm_di_type;
6995 resolve_llvm_types(g, type);
6996 assert(type->llvm_type != nullptr);
6997 assert(type->llvm_di_type != nullptr);
6998 assert(type->abi_size == LLVMABISizeOfType(g->target_data_ref, type->llvm_type));
6999 assert(type->abi_align == LLVMABIAlignmentOfType(g->target_data_ref, type->llvm_type));
7000 return type->llvm_di_type;
7001}
src/analyze.hpp+2
......@@ -244,4 +244,6 @@ 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);
247249#endif
src/codegen.cpp+318-331
......@@ -668,7 +668,7 @@ static ZigLLVMDIScope *get_di_scope(CodeGen *g, Scope *scope) {
668668 if (scope->parent) {
669669 ScopeDecls *decls_scope = (ScopeDecls *)scope;
670670 assert(decls_scope->container_type);
671 scope->di_scope = ZigLLVMTypeToScope(decls_scope->container_type->di_type);
671 scope->di_scope = ZigLLVMTypeToScope(get_llvm_di_type(g, decls_scope->container_type));
672672 } else {
673673 scope->di_scope = ZigLLVMFileToScope(import->data.structure.root_struct->di_file);
674674 }
......@@ -711,8 +711,8 @@ static LLVMValueRef get_arithmetic_overflow_fn(CodeGen *g, ZigType *operand_type
711711 const char *signed_str = int_type->data.integral.is_signed ? signed_name : unsigned_name;
712712
713713 LLVMTypeRef param_types[] = {
714 operand_type->type_ref,
715 operand_type->type_ref,
714 get_llvm_type(g, operand_type),
715 get_llvm_type(g, operand_type),
716716 };
717717
718718 if (operand_type->id == ZigTypeIdVector) {
......@@ -720,7 +720,7 @@ static LLVMValueRef get_arithmetic_overflow_fn(CodeGen *g, ZigType *operand_type
720720 operand_type->data.vector.len, int_type->data.integral.bit_count);
721721
722722 LLVMTypeRef return_elem_types[] = {
723 operand_type->type_ref,
723 get_llvm_type(g, operand_type),
724724 LLVMVectorType(LLVMInt1Type(), operand_type->data.vector.len),
725725 };
726726 LLVMTypeRef return_struct_type = LLVMStructType(return_elem_types, 2, false);
......@@ -732,7 +732,7 @@ static LLVMValueRef get_arithmetic_overflow_fn(CodeGen *g, ZigType *operand_type
732732 sprintf(fn_name, "llvm.%s.with.overflow.i%" PRIu32, signed_str, int_type->data.integral.bit_count);
733733
734734 LLVMTypeRef return_elem_types[] = {
735 operand_type->type_ref,
735 get_llvm_type(g, operand_type),
736736 LLVMInt1Type(),
737737 };
738738 LLVMTypeRef return_struct_type = LLVMStructType(return_elem_types, 2, false);
......@@ -800,7 +800,8 @@ static LLVMValueRef get_float_fn(CodeGen *g, ZigType *type_entry, ZigLLVMFnId fn
800800
801801 char fn_name[64];
802802 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);
803 LLVMTypeRef float_type_ref = get_llvm_type(g, type_entry);
804 LLVMTypeRef fn_type = LLVMFunctionType(float_type_ref, &float_type_ref, 1, false);
804805 LLVMValueRef fn_val = LLVMAddFunction(g->module, fn_name, fn_type);
805806 assert(LLVMGetIntrinsicID(fn_val));
806807
......@@ -958,7 +959,7 @@ static LLVMValueRef get_panic_msg_ptr_val(CodeGen *g, PanicMsgId msg_id) {
958959 ZigType *u8_ptr_type = get_pointer_to_type_extra(g, g->builtin_types.entry_u8, true, false,
959960 PtrLenUnknown, get_abi_alignment(g, g->builtin_types.entry_u8), 0, 0, false);
960961 ZigType *str_type = get_slice_type(g, u8_ptr_type);
961 return LLVMConstBitCast(val->global_refs->llvm_global, LLVMPointerType(str_type->type_ref, 0));
962 return LLVMConstBitCast(val->global_refs->llvm_global, LLVMPointerType(get_llvm_type(g, str_type), 0));
962963}
963964
964965static void gen_panic(CodeGen *g, LLVMValueRef msg_arg, LLVMValueRef stack_trace_arg) {
......@@ -967,7 +968,7 @@ static void gen_panic(CodeGen *g, LLVMValueRef msg_arg, LLVMValueRef stack_trace
967968 LLVMCallConv llvm_cc = get_llvm_cc(g, g->panic_fn->type_entry->data.fn.fn_type_id.cc);
968969 if (stack_trace_arg == nullptr) {
969970 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);
971 stack_trace_arg = LLVMConstNull(get_llvm_type(g, ptr_to_stack_trace_type));
971972 }
972973 LLVMValueRef args[] = {
973974 msg_arg,
......@@ -1081,7 +1082,7 @@ static LLVMValueRef get_coro_size_fn_val(CodeGen *g) {
10811082 if (g->coro_size_fn_val)
10821083 return g->coro_size_fn_val;
10831084
1084 LLVMTypeRef fn_type = LLVMFunctionType(g->builtin_types.entry_usize->type_ref, nullptr, 0, false);
1085 LLVMTypeRef fn_type = LLVMFunctionType(g->builtin_types.entry_usize->llvm_type, nullptr, 0, false);
10851086 Buf *name = buf_sprintf("llvm.coro.size.i%d", g->pointer_size_bytes * 8);
10861087 g->coro_size_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type);
10871088 assert(LLVMGetIntrinsicID(g->coro_size_fn_val));
......@@ -1206,8 +1207,8 @@ static LLVMValueRef get_return_address_fn_val(CodeGen *g) {
12061207
12071208 ZigType *return_type = get_pointer_to_type(g, g->builtin_types.entry_u8, true);
12081209
1209 LLVMTypeRef fn_type = LLVMFunctionType(return_type->type_ref,
1210 &g->builtin_types.entry_i32->type_ref, 1, false);
1210 LLVMTypeRef fn_type = LLVMFunctionType(get_llvm_type(g, return_type),
1211 &g->builtin_types.entry_i32->llvm_type, 1, false);
12111212 g->return_address_fn_val = LLVMAddFunction(g->module, "llvm.returnaddress", fn_type);
12121213 assert(LLVMGetIntrinsicID(g->return_address_fn_val));
12131214
......@@ -1219,8 +1220,8 @@ static LLVMValueRef get_add_error_return_trace_addr_fn(CodeGen *g) {
12191220 return g->add_error_return_trace_addr_fn_val;
12201221
12211222 LLVMTypeRef arg_types[] = {
1222 get_ptr_to_stack_trace_type(g)->type_ref,
1223 g->builtin_types.entry_usize->type_ref,
1223 get_llvm_type(g, get_ptr_to_stack_trace_type(g)),
1224 g->builtin_types.entry_usize->llvm_type,
12241225 };
12251226 LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMVoidType(), arg_types, 2, false);
12261227
......@@ -1245,7 +1246,7 @@ static LLVMValueRef get_add_error_return_trace_addr_fn(CodeGen *g) {
12451246 LLVMPositionBuilderAtEnd(g->builder, entry_block);
12461247 ZigLLVMClearCurrentDebugLocation(g->builder);
12471248
1248 LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->type_ref;
1249 LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->llvm_type;
12491250
12501251 // stack_trace.instruction_addresses[stack_trace.index & (stack_trace.instruction_addresses.len - 1)] = return_address;
12511252
......@@ -1297,8 +1298,8 @@ static LLVMValueRef get_merge_err_ret_traces_fn_val(CodeGen *g) {
12971298 assert(g->stack_trace_type != nullptr);
12981299
12991300 LLVMTypeRef param_types[] = {
1300 get_ptr_to_stack_trace_type(g)->type_ref,
1301 get_ptr_to_stack_trace_type(g)->type_ref,
1301 get_llvm_type(g, get_ptr_to_stack_trace_type(g)),
1302 get_llvm_type(g, get_ptr_to_stack_trace_type(g)),
13021303 };
13031304 LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMVoidType(), param_types, 2, false);
13041305
......@@ -1350,8 +1351,8 @@ static LLVMValueRef get_merge_err_ret_traces_fn_val(CodeGen *g) {
13501351 // }
13511352 LLVMBasicBlockRef return_block = LLVMAppendBasicBlock(fn_val, "Return");
13521353
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");
1354 LLVMValueRef frame_index_ptr = LLVMBuildAlloca(g->builder, g->builtin_types.entry_usize->llvm_type, "frame_index");
1355 LLVMValueRef frames_left_ptr = LLVMBuildAlloca(g->builder, g->builtin_types.entry_usize->llvm_type, "frames_left");
13551356
13561357 LLVMValueRef dest_stack_trace_ptr = LLVMGetParam(fn_val, 0);
13571358 LLVMValueRef src_stack_trace_ptr = LLVMGetParam(fn_val, 1);
......@@ -1377,14 +1378,14 @@ static LLVMValueRef get_merge_err_ret_traces_fn_val(CodeGen *g) {
13771378 LLVMBuildCondBr(g->builder, no_wrap_bit, no_wrap_block, yes_wrap_block);
13781379
13791380 LLVMPositionBuilderAtEnd(g->builder, no_wrap_block);
1380 LLVMValueRef usize_zero = LLVMConstNull(g->builtin_types.entry_usize->type_ref);
1381 LLVMValueRef usize_zero = LLVMConstNull(g->builtin_types.entry_usize->llvm_type);
13811382 LLVMBuildStore(g->builder, usize_zero, frame_index_ptr);
13821383 LLVMBuildStore(g->builder, src_index_val, frames_left_ptr);
13831384 LLVMValueRef frames_left_eq_zero_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, src_index_val, usize_zero, "");
13841385 LLVMBuildCondBr(g->builder, frames_left_eq_zero_bit, return_block, loop_block);
13851386
13861387 LLVMPositionBuilderAtEnd(g->builder, yes_wrap_block);
1387 LLVMValueRef usize_one = LLVMConstInt(g->builtin_types.entry_usize->type_ref, 1, false);
1388 LLVMValueRef usize_one = LLVMConstInt(g->builtin_types.entry_usize->llvm_type, 1, false);
13881389 LLVMValueRef plus_one = LLVMBuildNUWAdd(g->builder, src_index_val, usize_one, "");
13891390 LLVMValueRef mod_len = LLVMBuildURem(g->builder, plus_one, src_len_val, "");
13901391 LLVMBuildStore(g->builder, mod_len, frame_index_ptr);
......@@ -1430,7 +1431,7 @@ static LLVMValueRef get_return_err_fn(CodeGen *g) {
14301431
14311432 LLVMTypeRef arg_types[] = {
14321433 // error return trace pointer
1433 get_ptr_to_stack_trace_type(g)->type_ref,
1434 get_llvm_type(g, get_ptr_to_stack_trace_type(g)),
14341435 };
14351436 LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMVoidType(), arg_types, 1, false);
14361437
......@@ -1461,8 +1462,8 @@ static LLVMValueRef get_return_err_fn(CodeGen *g) {
14611462
14621463 LLVMValueRef err_ret_trace_ptr = LLVMGetParam(fn_val, 0);
14631464
1464 LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->type_ref;
1465 LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_i32->type_ref);
1465 LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->llvm_type;
1466 LLVMValueRef zero = LLVMConstNull(get_llvm_type(g, g->builtin_types.entry_i32));
14661467 LLVMValueRef return_address_ptr = LLVMBuildCall(g->builder, get_return_address_fn_val(g), &zero, 1, "");
14671468 LLVMValueRef return_address = LLVMBuildPtrToInt(g->builder, return_address_ptr, usize_type_ref, "");
14681469
......@@ -1508,8 +1509,8 @@ static LLVMValueRef get_safety_crash_err_fn(CodeGen *g) {
15081509
15091510 ZigType *usize = g->builtin_types.entry_usize;
15101511 LLVMValueRef full_buf_ptr_indices[] = {
1511 LLVMConstNull(usize->type_ref),
1512 LLVMConstNull(usize->type_ref),
1512 LLVMConstNull(usize->llvm_type),
1513 LLVMConstNull(usize->llvm_type),
15131514 };
15141515 LLVMValueRef full_buf_ptr = LLVMConstInBoundsGEP(global_array, full_buf_ptr_indices, 2);
15151516
......@@ -1519,9 +1520,9 @@ static LLVMValueRef get_safety_crash_err_fn(CodeGen *g) {
15191520 ZigType *str_type = get_slice_type(g, u8_ptr_type);
15201521 LLVMValueRef global_slice_fields[] = {
15211522 full_buf_ptr,
1522 LLVMConstNull(usize->type_ref),
1523 LLVMConstNull(usize->llvm_type),
15231524 };
1524 LLVMValueRef slice_init_value = LLVMConstNamedStruct(str_type->type_ref, global_slice_fields, 2);
1525 LLVMValueRef slice_init_value = LLVMConstNamedStruct(get_llvm_type(g, str_type), global_slice_fields, 2);
15251526 LLVMValueRef global_slice = LLVMAddGlobal(g->module, LLVMTypeOf(slice_init_value), "");
15261527 LLVMSetInitializer(global_slice, slice_init_value);
15271528 LLVMSetLinkage(global_slice, LLVMInternalLinkage);
......@@ -1530,15 +1531,15 @@ static LLVMValueRef get_safety_crash_err_fn(CodeGen *g) {
15301531 LLVMSetAlignment(global_slice, get_abi_alignment(g, str_type));
15311532
15321533 LLVMValueRef offset_ptr_indices[] = {
1533 LLVMConstNull(usize->type_ref),
1534 LLVMConstInt(usize->type_ref, unwrap_err_msg_text_len, false),
1534 LLVMConstNull(usize->llvm_type),
1535 LLVMConstInt(usize->llvm_type, unwrap_err_msg_text_len, false),
15351536 };
15361537 LLVMValueRef offset_buf_ptr = LLVMConstInBoundsGEP(global_array, offset_ptr_indices, 2);
15371538
15381539 Buf *fn_name = get_mangled_name(g, buf_create_from_str("__zig_fail_unwrap"), false);
15391540 LLVMTypeRef arg_types[] = {
1540 g->ptr_to_stack_trace_type->type_ref,
1541 g->err_tag_type->type_ref,
1541 get_llvm_type(g, g->ptr_to_stack_trace_type),
1542 get_llvm_type(g, g->err_tag_type),
15421543 };
15431544 LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMVoidType(), arg_types, 2, false);
15441545 LLVMValueRef fn_val = LLVMAddFunction(g->module, buf_ptr(fn_name), fn_type_ref);
......@@ -1565,7 +1566,7 @@ static LLVMValueRef get_safety_crash_err_fn(CodeGen *g) {
15651566 LLVMValueRef err_val = LLVMGetParam(fn_val, 1);
15661567
15671568 LLVMValueRef err_table_indices[] = {
1568 LLVMConstNull(g->builtin_types.entry_usize->type_ref),
1569 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),
15691570 err_val,
15701571 };
15711572 LLVMValueRef err_name_val = LLVMBuildInBoundsGEP(g->builder, g->err_name_table, err_table_indices, 2, "");
......@@ -1623,7 +1624,7 @@ static void gen_safety_crash_for_err(CodeGen *g, LLVMValueRef err_val, Scope *sc
16231624 LLVMValueRef err_ret_trace_val = get_cur_err_ret_trace_val(g, scope);
16241625 if (err_ret_trace_val == nullptr) {
16251626 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);
1627 err_ret_trace_val = LLVMConstNull(get_llvm_type(g, ptr_to_stack_trace_type));
16271628 }
16281629 LLVMValueRef args[] = {
16291630 err_ret_trace_val,
......@@ -1669,7 +1670,7 @@ static void add_bounds_check(CodeGen *g, LLVMValueRef target_val,
16691670}
16701671
16711672static LLVMValueRef gen_assert_zero(CodeGen *g, LLVMValueRef expr_val, ZigType *int_type) {
1672 LLVMValueRef zero = LLVMConstNull(int_type->type_ref);
1673 LLVMValueRef zero = LLVMConstNull(get_llvm_type(g, int_type));
16731674 LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, expr_val, zero, "");
16741675 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "CastShortenOk");
16751676 LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn_val, "CastShortenFail");
......@@ -1704,7 +1705,7 @@ static LLVMValueRef gen_widen_or_shorten(CodeGen *g, bool want_runtime_safety, Z
17041705 !wanted_type->data.integral.is_signed && actual_type->data.integral.is_signed &&
17051706 want_runtime_safety)
17061707 {
1707 LLVMValueRef zero = LLVMConstNull(actual_type->type_ref);
1708 LLVMValueRef zero = LLVMConstNull(get_llvm_type(g, actual_type));
17081709 LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntSGE, expr_val, zero, "");
17091710
17101711 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "SignCastOk");
......@@ -1721,19 +1722,19 @@ static LLVMValueRef gen_widen_or_shorten(CodeGen *g, bool want_runtime_safety, Z
17211722 return expr_val;
17221723 } else if (actual_bits < wanted_bits) {
17231724 if (actual_type->id == ZigTypeIdFloat) {
1724 return LLVMBuildFPExt(g->builder, expr_val, wanted_type->type_ref, "");
1725 return LLVMBuildFPExt(g->builder, expr_val, get_llvm_type(g, wanted_type), "");
17251726 } else if (actual_type->id == ZigTypeIdInt) {
17261727 if (actual_type->data.integral.is_signed) {
1727 return LLVMBuildSExt(g->builder, expr_val, wanted_type->type_ref, "");
1728 return LLVMBuildSExt(g->builder, expr_val, get_llvm_type(g, wanted_type), "");
17281729 } else {
1729 return LLVMBuildZExt(g->builder, expr_val, wanted_type->type_ref, "");
1730 return LLVMBuildZExt(g->builder, expr_val, get_llvm_type(g, wanted_type), "");
17301731 }
17311732 } else {
17321733 zig_unreachable();
17331734 }
17341735 } else if (actual_bits > wanted_bits) {
17351736 if (actual_type->id == ZigTypeIdFloat) {
1736 return LLVMBuildFPTrunc(g->builder, expr_val, wanted_type->type_ref, "");
1737 return LLVMBuildFPTrunc(g->builder, expr_val, get_llvm_type(g, wanted_type), "");
17371738 } else if (actual_type->id == ZigTypeIdInt) {
17381739 if (wanted_bits == 0) {
17391740 if (!want_runtime_safety)
......@@ -1741,15 +1742,15 @@ static LLVMValueRef gen_widen_or_shorten(CodeGen *g, bool want_runtime_safety, Z
17411742
17421743 return gen_assert_zero(g, expr_val, actual_type);
17431744 }
1744 LLVMValueRef trunc_val = LLVMBuildTrunc(g->builder, expr_val, wanted_type->type_ref, "");
1745 LLVMValueRef trunc_val = LLVMBuildTrunc(g->builder, expr_val, get_llvm_type(g, wanted_type), "");
17451746 if (!want_runtime_safety) {
17461747 return trunc_val;
17471748 }
17481749 LLVMValueRef orig_val;
17491750 if (wanted_type->data.integral.is_signed) {
1750 orig_val = LLVMBuildSExt(g->builder, trunc_val, actual_type->type_ref, "");
1751 orig_val = LLVMBuildSExt(g->builder, trunc_val, get_llvm_type(g, actual_type), "");
17511752 } else {
1752 orig_val = LLVMBuildZExt(g->builder, trunc_val, actual_type->type_ref, "");
1753 orig_val = LLVMBuildZExt(g->builder, trunc_val, get_llvm_type(g, actual_type), "");
17531754 }
17541755 LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, expr_val, orig_val, "");
17551756 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "CastShortenOk");
......@@ -1793,7 +1794,7 @@ static LLVMValueRef gen_overflow_op(CodeGen *g, ZigType *operand_type, AddSubMul
17931794 LLVMValueRef extended1 = buildExtFn(g->builder, val1, one_more_bit_int_vector, "");
17941795 LLVMValueRef extended2 = buildExtFn(g->builder, val2, one_more_bit_int_vector, "");
17951796 LLVMValueRef extended_result = wrap_op[op](g->builder, extended1, extended2, "");
1796 result = LLVMBuildTrunc(g->builder, extended_result, operand_type->type_ref, "");
1797 result = LLVMBuildTrunc(g->builder, extended_result, get_llvm_type(g, operand_type), "");
17971798
17981799 LLVMValueRef re_extended_result = buildExtFn(g->builder, result, one_more_bit_int_vector, "");
17991800 LLVMValueRef overflow_vector = LLVMBuildICmp(g->builder, LLVMIntNE, extended_result, re_extended_result, "");
......@@ -1880,12 +1881,13 @@ static LLVMValueRef gen_assign_raw(CodeGen *g, LLVMValueRef ptr, ZigType *ptr_ty
18801881 LLVMValueRef dest_ptr = LLVMBuildBitCast(g->builder, ptr, ptr_u8, "");
18811882
18821883 ZigType *usize = g->builtin_types.entry_usize;
1883 uint64_t size_bytes = LLVMStoreSizeOfType(g->target_data_ref, child_type->type_ref);
1884 uint64_t size_bytes = LLVMStoreSizeOfType(g->target_data_ref, get_llvm_type(g, child_type));
18841885 uint64_t align_bytes = get_ptr_align(g, ptr_type);
18851886 assert(size_bytes > 0);
18861887 assert(align_bytes > 0);
18871888
1888 ZigLLVMBuildMemCpy(g->builder, dest_ptr, align_bytes, src_ptr, align_bytes, LLVMConstInt(usize->type_ref, size_bytes, false),
1889 ZigLLVMBuildMemCpy(g->builder, dest_ptr, align_bytes, src_ptr, align_bytes,
1890 LLVMConstInt(usize->llvm_type, size_bytes, false),
18891891 ptr_type->data.pointer.is_volatile);
18901892 return nullptr;
18911893 }
......@@ -1907,7 +1909,7 @@ static LLVMValueRef gen_assign_raw(CodeGen *g, LLVMValueRef ptr, ZigType *ptr_ty
19071909 uint32_t shift_amt = big_endian ? host_bit_count - bit_offset - size_in_bits : bit_offset;
19081910 LLVMValueRef shift_amt_val = LLVMConstInt(LLVMTypeOf(containing_int), shift_amt, false);
19091911
1910 LLVMValueRef mask_val = LLVMConstAllOnes(child_type->type_ref);
1912 LLVMValueRef mask_val = LLVMConstAllOnes(get_llvm_type(g, child_type));
19111913 mask_val = LLVMConstZExt(mask_val, LLVMTypeOf(containing_int));
19121914 mask_val = LLVMConstShl(mask_val, shift_amt_val);
19131915 mask_val = LLVMConstNot(mask_val);
......@@ -1942,9 +1944,10 @@ static LLVMValueRef ir_llvm_value(CodeGen *g, IrInstruction *instruction) {
19421944 if (handle_is_ptr(instruction->value.type)) {
19431945 render_const_val_global(g, &instruction->value, "");
19441946 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, "");
1947 instruction->llvm_value = LLVMBuildBitCast(g->builder, instruction->value.global_refs->llvm_global, get_llvm_type(g, ptr_type), "");
19461948 } 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, "");
1949 instruction->llvm_value = LLVMBuildBitCast(g->builder, instruction->value.global_refs->llvm_value,
1950 get_llvm_type(g, instruction->value.type), "");
19481951 } else {
19491952 instruction->llvm_value = instruction->value.global_refs->llvm_value;
19501953 }
......@@ -1979,7 +1982,7 @@ static void give_up_with_c_abi_error(CodeGen *g, AstNode *source_node) {
19791982}
19801983
19811984static 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);
1985 LLVMValueRef result = LLVMBuildAlloca(g->builder, get_llvm_type(g, type_entry), name);
19831986 LLVMSetAlignment(result, (alignment == 0) ? get_abi_alignment(g, type_entry) : alignment);
19841987 return result;
19851988}
......@@ -2062,8 +2065,8 @@ static bool iter_function_params_c_abi(CodeGen *g, ZigType *fn_type, FnWalk *fn_
20622065 fn_walk->data.call.gen_param_values->append(val);
20632066 break;
20642067 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);
2068 fn_walk->data.types.gen_param_types->append(get_llvm_type(g, ty));
2069 fn_walk->data.types.param_di_types->append(get_llvm_di_type(g, ty));
20672070 break;
20682071 case FnWalkIdVars: {
20692072 var->value_ref = build_alloca(g, ty, buf_ptr(&var->name), var->align_bytes);
......@@ -2099,8 +2102,8 @@ static bool iter_function_params_c_abi(CodeGen *g, ZigType *fn_type, FnWalk *fn_
20992102 break;
21002103 case FnWalkIdTypes: {
21012104 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);
2105 fn_walk->data.types.gen_param_types->append(get_llvm_type(g, gen_type));
2106 fn_walk->data.types.param_di_types->append(get_llvm_di_type(g, gen_type));
21042107 break;
21052108 }
21062109 case FnWalkIdVars: {
......@@ -2138,8 +2141,8 @@ static bool iter_function_params_c_abi(CodeGen *g, ZigType *fn_type, FnWalk *fn_
21382141 break;
21392142 case FnWalkIdTypes: {
21402143 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);
2144 fn_walk->data.types.gen_param_types->append(get_llvm_type(g, gen_type));
2145 fn_walk->data.types.param_di_types->append(get_llvm_di_type(g, gen_type));
21432146 break;
21442147 }
21452148 case FnWalkIdVars: {
......@@ -2171,8 +2174,8 @@ static bool iter_function_params_c_abi(CodeGen *g, ZigType *fn_type, FnWalk *fn_
21712174 }
21722175 case FnWalkIdTypes: {
21732176 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);
2177 fn_walk->data.types.gen_param_types->append(get_llvm_type(g, gen_type));
2178 fn_walk->data.types.param_di_types->append(get_llvm_di_type(g, gen_type));
21762179 break;
21772180 }
21782181 case FnWalkIdVars: {
......@@ -2210,7 +2213,7 @@ var_ok:
22102213 var->di_loc_var = ZigLLVMCreateParameterVariable(g->dbuilder, get_di_scope(g, var->parent_scope),
22112214 buf_ptr(&var->name), fn_walk->data.vars.import->data.structure.root_struct->di_file,
22122215 (unsigned)(var->decl_node->line + 1),
2213 dest_ty->di_type, !g->strip_debug_symbols, 0, di_arg_index + 1);
2216 get_llvm_di_type(g, dest_ty), !g->strip_debug_symbols, 0, di_arg_index + 1);
22142217 }
22152218 return true;
22162219}
......@@ -2436,7 +2439,7 @@ static LLVMValueRef gen_div(CodeGen *g, bool want_runtime_safety, bool want_fast
24362439{
24372440 ZigLLVMSetFastMath(g->builder, want_fast_math);
24382441
2439 LLVMValueRef zero = LLVMConstNull(type_entry->type_ref);
2442 LLVMValueRef zero = LLVMConstNull(get_llvm_type(g, type_entry));
24402443 if (want_runtime_safety && (want_fast_math || type_entry->id != ZigTypeIdFloat)) {
24412444 LLVMValueRef is_zero_bit;
24422445 if (type_entry->id == ZigTypeIdInt) {
......@@ -2456,10 +2459,10 @@ static LLVMValueRef gen_div(CodeGen *g, bool want_runtime_safety, bool want_fast
24562459 LLVMPositionBuilderAtEnd(g->builder, div_zero_ok_block);
24572460
24582461 if (type_entry->id == ZigTypeIdInt && type_entry->data.integral.is_signed) {
2459 LLVMValueRef neg_1_value = LLVMConstInt(type_entry->type_ref, -1, true);
2462 LLVMValueRef neg_1_value = LLVMConstInt(get_llvm_type(g, type_entry), -1, true);
24602463 BigInt int_min_bi = {0};
24612464 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);
2465 LLVMValueRef int_min_value = bigint_to_llvm_const(get_llvm_type(g, type_entry), &int_min_bi);
24632466 LLVMBasicBlockRef overflow_ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "DivOverflowOk");
24642467 LLVMBasicBlockRef overflow_fail_block = LLVMAppendBasicBlock(g->cur_fn_val, "DivOverflowFail");
24652468 LLVMValueRef num_is_int_min = LLVMBuildICmp(g->builder, LLVMIntEQ, val1, int_min_value, "");
......@@ -2513,7 +2516,7 @@ static LLVMValueRef gen_div(CodeGen *g, bool want_runtime_safety, bool want_fast
25132516 LLVMBuildBr(g->builder, end_block);
25142517
25152518 LLVMPositionBuilderAtEnd(g->builder, end_block);
2516 LLVMValueRef phi = LLVMBuildPhi(g->builder, type_entry->type_ref, "");
2519 LLVMValueRef phi = LLVMBuildPhi(g->builder, get_llvm_type(g, type_entry), "");
25172520 LLVMValueRef incoming_values[] = { ceiled, floored };
25182521 LLVMBasicBlockRef incoming_blocks[] = { ceiled_end_block, floored_end_block };
25192522 LLVMAddIncoming(phi, incoming_values, incoming_blocks, 2);
......@@ -2576,7 +2579,7 @@ static LLVMValueRef gen_div(CodeGen *g, bool want_runtime_safety, bool want_fast
25762579 LLVMValueRef orig_num = LLVMBuildNSWMul(g->builder, result, val2, "");
25772580 LLVMValueRef orig_ok = LLVMBuildICmp(g->builder, LLVMIntEQ, orig_num, val1, "");
25782581 LLVMValueRef ok_bit = LLVMBuildOr(g->builder, orig_ok, is_pos, "");
2579 LLVMValueRef one = LLVMConstInt(type_entry->type_ref, 1, true);
2582 LLVMValueRef one = LLVMConstInt(get_llvm_type(g, type_entry), 1, true);
25802583 LLVMValueRef result_minus_1 = LLVMBuildNSWSub(g->builder, result, one, "");
25812584 return LLVMBuildSelect(g->builder, ok_bit, result, result_minus_1, "");
25822585 }
......@@ -2595,7 +2598,7 @@ static LLVMValueRef gen_rem(CodeGen *g, bool want_runtime_safety, bool want_fast
25952598{
25962599 ZigLLVMSetFastMath(g->builder, want_fast_math);
25972600
2598 LLVMValueRef zero = LLVMConstNull(type_entry->type_ref);
2601 LLVMValueRef zero = LLVMConstNull(get_llvm_type(g, type_entry));
25992602 if (want_runtime_safety) {
26002603 LLVMValueRef is_zero_bit;
26012604 if (type_entry->id == ZigTypeIdInt) {
......@@ -2820,7 +2823,7 @@ static void add_error_range_check(CodeGen *g, ZigType *err_set_type, ZigType *in
28202823 assert(err_set_type->id == ZigTypeIdErrorSet);
28212824
28222825 if (type_is_global_error_set(err_set_type)) {
2823 LLVMValueRef zero = LLVMConstNull(int_type->type_ref);
2826 LLVMValueRef zero = LLVMConstNull(get_llvm_type(g, int_type));
28242827 LLVMValueRef neq_zero_bit = LLVMBuildICmp(g->builder, LLVMIntNE, target_val, zero, "");
28252828 LLVMValueRef ok_bit;
28262829
......@@ -2832,7 +2835,7 @@ static void add_error_range_check(CodeGen *g, ZigType *err_set_type, ZigType *in
28322835 {
28332836 ok_bit = neq_zero_bit;
28342837 } else {
2835 LLVMValueRef error_value_count = LLVMConstInt(int_type->type_ref, g->errors_by_index.length, false);
2838 LLVMValueRef error_value_count = LLVMConstInt(get_llvm_type(g, int_type), g->errors_by_index.length, false);
28362839 LLVMValueRef in_bounds_bit = LLVMBuildICmp(g->builder, LLVMIntULT, target_val, error_value_count, "");
28372840 ok_bit = LLVMBuildAnd(g->builder, neq_zero_bit, in_bounds_bit, "");
28382841 }
......@@ -2853,7 +2856,8 @@ static void add_error_range_check(CodeGen *g, ZigType *err_set_type, ZigType *in
28532856 uint32_t err_count = err_set_type->data.error_set.err_count;
28542857 LLVMValueRef switch_instr = LLVMBuildSwitch(g->builder, target_val, fail_block, err_count);
28552858 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);
2859 LLVMValueRef case_value = LLVMConstInt(get_llvm_type(g, g->err_tag_type),
2860 err_set_type->data.error_set.errors[i]->value, false);
28572861 LLVMAddCase(switch_instr, case_value, ok_block);
28582862 }
28592863
......@@ -2892,7 +2896,7 @@ static LLVMValueRef ir_render_resize_slice(CodeGen *g, IrExecutable *executable,
28922896 LLVMValueRef src_ptr_ptr = LLVMBuildStructGEP(g->builder, expr_val, (unsigned)actual_ptr_index, "");
28932897 LLVMValueRef src_ptr = gen_load_untyped(g, src_ptr_ptr, 0, false, "");
28942898 LLVMValueRef src_ptr_casted = LLVMBuildBitCast(g->builder, src_ptr,
2895 wanted_type->data.structure.fields[0].type_entry->type_ref, "");
2899 get_llvm_type(g, wanted_type->data.structure.fields[0].type_entry), "");
28962900 LLVMValueRef dest_ptr_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr,
28972901 (unsigned)wanted_ptr_index, "");
28982902 gen_store_untyped(g, src_ptr_casted, dest_ptr_ptr, 0, false);
......@@ -2904,13 +2908,13 @@ static LLVMValueRef ir_render_resize_slice(CodeGen *g, IrExecutable *executable,
29042908
29052909 LLVMValueRef new_len;
29062910 if (dest_size == 1) {
2907 LLVMValueRef src_size_val = LLVMConstInt(g->builtin_types.entry_usize->type_ref, src_size, false);
2911 LLVMValueRef src_size_val = LLVMConstInt(g->builtin_types.entry_usize->llvm_type, src_size, false);
29082912 new_len = LLVMBuildMul(g->builder, src_len, src_size_val, "");
29092913 } else if (src_size == 1) {
2910 LLVMValueRef dest_size_val = LLVMConstInt(g->builtin_types.entry_usize->type_ref, dest_size, false);
2914 LLVMValueRef dest_size_val = LLVMConstInt(g->builtin_types.entry_usize->llvm_type, dest_size, false);
29112915 if (ir_want_runtime_safety(g, &instruction->base)) {
29122916 LLVMValueRef remainder_val = LLVMBuildURem(g->builder, src_len, dest_size_val, "");
2913 LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_usize->type_ref);
2917 LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_usize->llvm_type);
29142918 LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, remainder_val, zero, "");
29152919 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "SliceWidenOk");
29162920 LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn_val, "SliceWidenFail");
......@@ -2951,9 +2955,9 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,
29512955 case CastOpIntToFloat:
29522956 assert(actual_type->id == ZigTypeIdInt);
29532957 if (actual_type->data.integral.is_signed) {
2954 return LLVMBuildSIToFP(g->builder, expr_val, wanted_type->type_ref, "");
2958 return LLVMBuildSIToFP(g->builder, expr_val, get_llvm_type(g, wanted_type), "");
29552959 } else {
2956 return LLVMBuildUIToFP(g->builder, expr_val, wanted_type->type_ref, "");
2960 return LLVMBuildUIToFP(g->builder, expr_val, get_llvm_type(g, wanted_type), "");
29572961 }
29582962 case CastOpFloatToInt: {
29592963 assert(wanted_type->id == ZigTypeIdInt);
......@@ -2963,9 +2967,9 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,
29632967
29642968 LLVMValueRef result;
29652969 if (wanted_type->data.integral.is_signed) {
2966 result = LLVMBuildFPToSI(g->builder, expr_val, wanted_type->type_ref, "");
2970 result = LLVMBuildFPToSI(g->builder, expr_val, get_llvm_type(g, wanted_type), "");
29672971 } else {
2968 result = LLVMBuildFPToUI(g->builder, expr_val, wanted_type->type_ref, "");
2972 result = LLVMBuildFPToUI(g->builder, expr_val, get_llvm_type(g, wanted_type), "");
29692973 }
29702974
29712975 if (want_safety) {
......@@ -2993,14 +2997,14 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,
29932997 case CastOpBoolToInt:
29942998 assert(wanted_type->id == ZigTypeIdInt);
29952999 assert(actual_type->id == ZigTypeIdBool);
2996 return LLVMBuildZExt(g->builder, expr_val, wanted_type->type_ref, "");
3000 return LLVMBuildZExt(g->builder, expr_val, get_llvm_type(g, wanted_type), "");
29973001 case CastOpErrSet:
29983002 if (ir_want_runtime_safety(g, &cast_instruction->base)) {
29993003 add_error_range_check(g, wanted_type, g->err_tag_type, expr_val);
30003004 }
30013005 return expr_val;
30023006 case CastOpBitCast:
3003 return LLVMBuildBitCast(g->builder, expr_val, wanted_type->type_ref, "");
3007 return LLVMBuildBitCast(g->builder, expr_val, get_llvm_type(g, wanted_type), "");
30043008 case CastOpPtrOfArrayToSlice: {
30053009 assert(cast_instruction->tmp_ptr);
30063010 assert(actual_type->id == ZigTypeIdPointer);
......@@ -3010,15 +3014,15 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,
30103014 LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, cast_instruction->tmp_ptr,
30113015 slice_ptr_index, "");
30123016 LLVMValueRef indices[] = {
3013 LLVMConstNull(g->builtin_types.entry_usize->type_ref),
3014 LLVMConstInt(g->builtin_types.entry_usize->type_ref, 0, false),
3017 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),
3018 LLVMConstInt(g->builtin_types.entry_usize->llvm_type, 0, false),
30153019 };
30163020 LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP(g->builder, expr_val, indices, 2, "");
30173021 gen_store_untyped(g, slice_start_ptr, ptr_field_ptr, 0, false);
30183022
30193023 LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, cast_instruction->tmp_ptr,
30203024 slice_len_index, "");
3021 LLVMValueRef len_value = LLVMConstInt(g->builtin_types.entry_usize->type_ref,
3025 LLVMValueRef len_value = LLVMConstInt(g->builtin_types.entry_usize->llvm_type,
30223026 array_type->data.array.len, false);
30233027 gen_store_untyped(g, len_value, len_field_ptr, 0, false);
30243028
......@@ -3036,7 +3040,7 @@ static LLVMValueRef ir_render_ptr_cast(CodeGen *g, IrExecutable *executable,
30363040 return nullptr;
30373041 }
30383042 LLVMValueRef ptr = ir_llvm_value(g, instruction->ptr);
3039 LLVMValueRef result_ptr = LLVMBuildBitCast(g->builder, ptr, wanted_type->type_ref, "");
3043 LLVMValueRef result_ptr = LLVMBuildBitCast(g->builder, ptr, get_llvm_type(g, wanted_type), "");
30403044 bool want_safety_check = instruction->safety_check_on && ir_want_runtime_safety(g, &instruction->base);
30413045 if (!want_safety_check || ptr_allows_addr_zero(wanted_type))
30423046 return result_ptr;
......@@ -3066,16 +3070,16 @@ static LLVMValueRef ir_render_bit_cast(CodeGen *g, IrExecutable *executable,
30663070 if (wanted_is_ptr == actual_is_ptr) {
30673071 // We either bitcast the value directly or bitcast the pointer which does a pointer cast
30683072 LLVMTypeRef wanted_type_ref = wanted_is_ptr ?
3069 LLVMPointerType(wanted_type->type_ref, 0) : wanted_type->type_ref;
3073 LLVMPointerType(get_llvm_type(g, wanted_type), 0) : get_llvm_type(g, wanted_type);
30703074 return LLVMBuildBitCast(g->builder, value, wanted_type_ref, "");
30713075 } else if (actual_is_ptr) {
3072 LLVMTypeRef wanted_ptr_type_ref = LLVMPointerType(wanted_type->type_ref, 0);
3076 LLVMTypeRef wanted_ptr_type_ref = LLVMPointerType(get_llvm_type(g, wanted_type), 0);
30733077 LLVMValueRef bitcasted_ptr = LLVMBuildBitCast(g->builder, value, wanted_ptr_type_ref, "");
30743078 uint32_t alignment = get_abi_alignment(g, actual_type);
30753079 return gen_load_untyped(g, bitcasted_ptr, alignment, false, "");
30763080 } else {
30773081 assert(instruction->tmp_ptr != nullptr);
3078 LLVMTypeRef wanted_ptr_type_ref = LLVMPointerType(actual_type->type_ref, 0);
3082 LLVMTypeRef wanted_ptr_type_ref = LLVMPointerType(get_llvm_type(g, actual_type), 0);
30793083 LLVMValueRef bitcasted_ptr = LLVMBuildBitCast(g->builder, instruction->tmp_ptr, wanted_ptr_type_ref, "");
30803084 uint32_t alignment = get_abi_alignment(g, wanted_type);
30813085 gen_store_untyped(g, value, bitcasted_ptr, alignment, false);
......@@ -3115,13 +3119,13 @@ static LLVMValueRef ir_render_int_to_ptr(CodeGen *g, IrExecutable *executable, I
31153119
31163120 LLVMPositionBuilderAtEnd(g->builder, ok_block);
31173121 }
3118 return LLVMBuildIntToPtr(g->builder, target_val, wanted_type->type_ref, "");
3122 return LLVMBuildIntToPtr(g->builder, target_val, get_llvm_type(g, wanted_type), "");
31193123}
31203124
31213125static LLVMValueRef ir_render_ptr_to_int(CodeGen *g, IrExecutable *executable, IrInstructionPtrToInt *instruction) {
31223126 ZigType *wanted_type = instruction->base.value.type;
31233127 LLVMValueRef target_val = ir_llvm_value(g, instruction->target);
3124 return LLVMBuildPtrToInt(g->builder, target_val, wanted_type->type_ref, "");
3128 return LLVMBuildPtrToInt(g->builder, target_val, get_llvm_type(g, wanted_type), "");
31253129}
31263130
31273131static LLVMValueRef ir_render_int_to_enum(CodeGen *g, IrExecutable *executable, IrInstructionIntToEnum *instruction) {
......@@ -3139,7 +3143,7 @@ static LLVMValueRef ir_render_int_to_enum(CodeGen *g, IrExecutable *executable,
31393143 size_t field_count = wanted_type->data.enumeration.src_field_count;
31403144 LLVMValueRef switch_instr = LLVMBuildSwitch(g->builder, tag_int_value, bad_value_block, field_count);
31413145 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,
3146 LLVMValueRef this_tag_int_value = bigint_to_llvm_const(get_llvm_type(g, tag_int_type),
31433147 &wanted_type->data.enumeration.fields[field_i].value);
31443148 LLVMAddCase(switch_instr, this_tag_int_value, ok_value_block);
31453149 }
......@@ -3321,7 +3325,7 @@ static LLVMValueRef ir_render_load_ptr(CodeGen *g, IrExecutable *executable, IrI
33213325 LLVMValueRef shifted_value = LLVMBuildLShr(g->builder, containing_int, shift_amt_val, "");
33223326
33233327 if (!handle_is_ptr(child_type))
3324 return LLVMBuildTrunc(g->builder, shifted_value, child_type->type_ref, "");
3328 return LLVMBuildTrunc(g->builder, shifted_value, get_llvm_type(g, child_type), "");
33253329
33263330 assert(instruction->tmp_ptr != nullptr);
33273331 LLVMTypeRef same_size_int = LLVMIntType(size_in_bits);
......@@ -3378,7 +3382,7 @@ static LLVMValueRef gen_valgrind_client_request(CodeGen *g, LLVMValueRef default
33783382 if (!target_has_valgrind_support(g->zig_target)) {
33793383 return default_value;
33803384 }
3381 LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->type_ref;
3385 LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->llvm_type;
33823386 bool asm_has_side_effects = true;
33833387 bool asm_is_alignstack = false;
33843388 if (g->zig_target->arch == ZigLLVM_x86_64) {
......@@ -3445,7 +3449,7 @@ static bool want_valgrind_support(CodeGen *g) {
34453449
34463450static void gen_undef_init(CodeGen *g, uint32_t ptr_align_bytes, ZigType *value_type, LLVMValueRef ptr) {
34473451 assert(type_has_bits(value_type));
3448 uint64_t size_bytes = LLVMStoreSizeOfType(g->target_data_ref, value_type->type_ref);
3452 uint64_t size_bytes = LLVMStoreSizeOfType(g->target_data_ref, get_llvm_type(g, value_type));
34493453 assert(size_bytes > 0);
34503454 assert(ptr_align_bytes > 0);
34513455 // memset uninitialized memory to 0xaa
......@@ -3453,14 +3457,14 @@ static void gen_undef_init(CodeGen *g, uint32_t ptr_align_bytes, ZigType *value_
34533457 LLVMValueRef fill_char = LLVMConstInt(LLVMInt8Type(), 0xaa, false);
34543458 LLVMValueRef dest_ptr = LLVMBuildBitCast(g->builder, ptr, ptr_u8, "");
34553459 ZigType *usize = g->builtin_types.entry_usize;
3456 LLVMValueRef byte_count = LLVMConstInt(usize->type_ref, size_bytes, false);
3460 LLVMValueRef byte_count = LLVMConstInt(usize->llvm_type, size_bytes, false);
34573461 ZigLLVMBuildMemSet(g->builder, dest_ptr, fill_char, byte_count, ptr_align_bytes, false);
34583462 // then tell valgrind that the memory is undefined even though we just memset it
34593463 if (want_valgrind_support(g)) {
34603464 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, "");
3465 LLVMValueRef zero = LLVMConstInt(usize->llvm_type, 0, false);
3466 LLVMValueRef req = LLVMConstInt(usize->llvm_type, VG_USERREQ__MAKE_MEM_UNDEFINED, false);
3467 LLVMValueRef ptr_as_usize = LLVMBuildPtrToInt(g->builder, dest_ptr, usize->llvm_type, "");
34643468 gen_valgrind_client_request(g, zero, req, ptr_as_usize, byte_count, zero, zero, zero);
34653469 }
34663470}
......@@ -3515,7 +3519,7 @@ static LLVMValueRef ir_render_elem_ptr(CodeGen *g, IrExecutable *executable, IrI
35153519 array_type = array_type->data.pointer.child_type;
35163520 }
35173521 if (safety_check_on) {
3518 LLVMValueRef end = LLVMConstInt(g->builtin_types.entry_usize->type_ref,
3522 LLVMValueRef end = LLVMConstInt(g->builtin_types.entry_usize->llvm_type,
35193523 array_type->data.array.len, false);
35203524 add_bounds_check(g, subscript_value, LLVMIntEQ, nullptr, LLVMIntULT, end);
35213525 }
......@@ -3533,18 +3537,18 @@ static LLVMValueRef ir_render_elem_ptr(CodeGen *g, IrExecutable *executable, IrI
35333537 LLVMTypeRef ptr_u8_type_ref = LLVMPointerType(LLVMInt8Type(), 0);
35343538 LLVMValueRef u8_array_ptr = LLVMBuildBitCast(g->builder, array_ptr, ptr_u8_type_ref, "");
35353539 assert(size_in_bits % 8 == 0);
3536 LLVMValueRef elem_size_bytes = LLVMConstInt(g->builtin_types.entry_usize->type_ref,
3540 LLVMValueRef elem_size_bytes = LLVMConstInt(g->builtin_types.entry_usize->llvm_type,
35373541 size_in_bits / 8, false);
35383542 LLVMValueRef byte_offset = LLVMBuildNUWMul(g->builder, subscript_value, elem_size_bytes, "");
35393543 LLVMValueRef indices[] = {
35403544 byte_offset
35413545 };
35423546 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), "");
3547 return LLVMBuildBitCast(g->builder, elem_byte_ptr, LLVMPointerType(get_llvm_type(g, child_type), 0), "");
35443548 }
35453549 }
35463550 LLVMValueRef indices[] = {
3547 LLVMConstNull(g->builtin_types.entry_usize->type_ref),
3551 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),
35483552 subscript_value
35493553 };
35503554 return LLVMBuildInBoundsGEP(g->builder, array_ptr, indices, 2, "");
......@@ -3773,7 +3777,7 @@ static LLVMValueRef ir_render_union_field_ptr(CodeGen *g, IrExecutable *executab
37733777 return nullptr;
37743778
37753779 LLVMValueRef union_ptr = ir_llvm_value(g, instruction->union_ptr);
3776 LLVMTypeRef field_type_ref = LLVMPointerType(field->type_entry->type_ref, 0);
3780 LLVMTypeRef field_type_ref = LLVMPointerType(get_llvm_type(g, field->type_entry), 0);
37773781
37783782 if (union_type->data.unionation.gen_tag_index == SIZE_MAX) {
37793783 LLVMValueRef union_field_ptr = LLVMBuildStructGEP(g->builder, union_ptr, 0, "");
......@@ -3786,7 +3790,7 @@ static LLVMValueRef ir_render_union_field_ptr(CodeGen *g, IrExecutable *executab
37863790 LLVMValueRef tag_value = gen_load_untyped(g, tag_field_ptr, 0, false, "");
37873791
37883792
3789 LLVMValueRef expected_tag_value = bigint_to_llvm_const(union_type->data.unionation.tag_type->type_ref,
3793 LLVMValueRef expected_tag_value = bigint_to_llvm_const(get_llvm_type(g, union_type->data.unionation.tag_type),
37903794 &field->enum_field->value);
37913795 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "UnionCheckOk");
37923796 LLVMBasicBlockRef bad_block = LLVMAppendBasicBlock(g->cur_fn_val, "UnionCheckFail");
......@@ -3916,7 +3920,7 @@ static LLVMValueRef ir_render_asm(CodeGen *g, IrExecutable *executable, IrInstru
39163920 }
39173921
39183922 ZigType *const type = ir_input->value.type;
3919 LLVMTypeRef type_ref = type->type_ref;
3923 LLVMTypeRef type_ref = get_llvm_type(g, type);
39203924 LLVMValueRef value_ref = ir_llvm_value(g, ir_input);
39213925 // Handle integers of non pot bitsize by widening them.
39223926 if (type->id == ZigTypeIdInt) {
......@@ -3925,7 +3929,7 @@ static LLVMValueRef ir_render_asm(CodeGen *g, IrExecutable *executable, IrInstru
39253929 const bool is_signed = type->data.integral.is_signed;
39263930 const size_t wider_bitsize = bitsize < 8 ? 8 : round_to_next_power_of_2(bitsize);
39273931 ZigType *const wider_type = get_int_type(g, is_signed, wider_bitsize);
3928 type_ref = wider_type->type_ref;
3932 type_ref = get_llvm_type(g, wider_type);
39293933 value_ref = gen_widen_or_shorten(g, false, type, wider_type, value_ref);
39303934 }
39313935 }
......@@ -3945,7 +3949,7 @@ static LLVMValueRef ir_render_asm(CodeGen *g, IrExecutable *executable, IrInstru
39453949 if (instruction->return_count == 0) {
39463950 ret_type = LLVMVoidType();
39473951 } else {
3948 ret_type = instruction->base.value.type->type_ref;
3952 ret_type = get_llvm_type(g, instruction->base.value.type);
39493953 }
39503954 LLVMTypeRef function_type = LLVMFunctionType(ret_type, param_types, (unsigned)input_and_output_count, false);
39513955
......@@ -3964,7 +3968,7 @@ static LLVMValueRef gen_non_null_bit(CodeGen *g, ZigType *maybe_type, LLVMValueR
39643968 } else {
39653969 bool is_scalar = !handle_is_ptr(maybe_type);
39663970 if (is_scalar) {
3967 return LLVMBuildICmp(g->builder, LLVMIntNE, maybe_handle, LLVMConstNull(maybe_type->type_ref), "");
3971 return LLVMBuildICmp(g->builder, LLVMIntNE, maybe_handle, LLVMConstNull(get_llvm_type(g, maybe_type)), "");
39683972 } else {
39693973 LLVMValueRef maybe_field_ptr = LLVMBuildStructGEP(g->builder, maybe_handle, maybe_null_index, "");
39703974 return gen_load_untyped(g, maybe_field_ptr, 0, false, "");
......@@ -3999,7 +4003,7 @@ static LLVMValueRef ir_render_optional_unwrap_ptr(CodeGen *g, IrExecutable *exec
39994003
40004004 LLVMPositionBuilderAtEnd(g->builder, ok_block);
40014005 }
4002 if (child_type->zero_bits) {
4006 if (!type_has_bits(child_type)) {
40034007 return nullptr;
40044008 } else {
40054009 bool is_scalar = !handle_is_ptr(maybe_type);
......@@ -4052,10 +4056,10 @@ static LLVMValueRef get_int_builtin_fn(CodeGen *g, ZigType *int_type, BuiltinFnI
40524056 char llvm_name[64];
40534057 sprintf(llvm_name, "llvm.%s.i%" PRIu32, fn_name, int_type->data.integral.bit_count);
40544058 LLVMTypeRef param_types[] = {
4055 int_type->type_ref,
4059 get_llvm_type(g, int_type),
40564060 LLVMInt1Type(),
40574061 };
4058 LLVMTypeRef fn_type = LLVMFunctionType(int_type->type_ref, param_types, n_args, false);
4062 LLVMTypeRef fn_type = LLVMFunctionType(get_llvm_type(g, int_type), param_types, n_args, false);
40594063 LLVMValueRef fn_val = LLVMAddFunction(g->module, llvm_name, fn_type);
40604064 assert(LLVMGetIntrinsicID(fn_val));
40614065
......@@ -4114,9 +4118,9 @@ static LLVMValueRef ir_render_phi(CodeGen *g, IrExecutable *executable, IrInstru
41144118
41154119 LLVMTypeRef phi_type;
41164120 if (handle_is_ptr(instruction->base.value.type)) {
4117 phi_type = LLVMPointerType(instruction->base.value.type->type_ref, 0);
4121 phi_type = LLVMPointerType(get_llvm_type(g,instruction->base.value.type), 0);
41184122 } else {
4119 phi_type = instruction->base.value.type->type_ref;
4123 phi_type = get_llvm_type(g, instruction->base.value.type);
41204124 }
41214125
41224126 LLVMValueRef phi = LLVMBuildPhi(g->builder, phi_type, "");
......@@ -4160,7 +4164,7 @@ static LLVMValueRef ir_render_err_name(CodeGen *g, IrExecutable *executable, IrI
41604164 }
41614165
41624166 LLVMValueRef indices[] = {
4163 LLVMConstNull(g->builtin_types.entry_usize->type_ref),
4167 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),
41644168 err_val,
41654169 };
41664170 return LLVMBuildInBoundsGEP(g->builder, g->err_name_table, indices, 2, "");
......@@ -4176,8 +4180,9 @@ static LLVMValueRef get_enum_tag_name_function(CodeGen *g, ZigType *enum_type) {
41764180 ZigType *u8_slice_type = get_slice_type(g, u8_ptr_type);
41774181 ZigType *tag_int_type = enum_type->data.enumeration.tag_int_type;
41784182
4179 LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMPointerType(u8_slice_type->type_ref, 0),
4180 &tag_int_type->type_ref, 1, false);
4183 LLVMTypeRef tag_int_llvm_type = get_llvm_type(g, tag_int_type);
4184 LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMPointerType(get_llvm_type(g, u8_slice_type), 0),
4185 &tag_int_llvm_type, 1, false);
41814186
41824187 Buf *fn_name = get_mangled_name(g, buf_sprintf("__zig_tag_name_%s", buf_ptr(&enum_type->name)), false);
41834188 LLVMValueRef fn_val = LLVMAddFunction(g->module, buf_ptr(fn_name), fn_type_ref);
......@@ -4209,8 +4214,8 @@ static LLVMValueRef get_enum_tag_name_function(CodeGen *g, ZigType *enum_type) {
42094214
42104215 ZigType *usize = g->builtin_types.entry_usize;
42114216 LLVMValueRef array_ptr_indices[] = {
4212 LLVMConstNull(usize->type_ref),
4213 LLVMConstNull(usize->type_ref),
4217 LLVMConstNull(usize->llvm_type),
4218 LLVMConstNull(usize->llvm_type),
42144219 };
42154220
42164221 for (size_t field_i = 0; field_i < field_count; field_i += 1) {
......@@ -4225,9 +4230,9 @@ static LLVMValueRef get_enum_tag_name_function(CodeGen *g, ZigType *enum_type) {
42254230
42264231 LLVMValueRef fields[] = {
42274232 LLVMConstGEP(str_global, array_ptr_indices, 2),
4228 LLVMConstInt(g->builtin_types.entry_usize->type_ref, buf_len(name), false),
4233 LLVMConstInt(g->builtin_types.entry_usize->llvm_type, buf_len(name), false),
42294234 };
4230 LLVMValueRef slice_init_value = LLVMConstNamedStruct(u8_slice_type->type_ref, fields, 2);
4235 LLVMValueRef slice_init_value = LLVMConstNamedStruct(get_llvm_type(g, u8_slice_type), fields, 2);
42314236
42324237 LLVMValueRef slice_global = LLVMAddGlobal(g->module, LLVMTypeOf(slice_init_value), "");
42334238 LLVMSetInitializer(slice_global, slice_init_value);
......@@ -4237,7 +4242,7 @@ static LLVMValueRef get_enum_tag_name_function(CodeGen *g, ZigType *enum_type) {
42374242 LLVMSetAlignment(slice_global, LLVMABIAlignmentOfType(g->target_data_ref, LLVMTypeOf(slice_init_value)));
42384243
42394244 LLVMBasicBlockRef return_block = LLVMAppendBasicBlock(g->cur_fn_val, "Name");
4240 LLVMValueRef this_tag_int_value = bigint_to_llvm_const(tag_int_type->type_ref,
4245 LLVMValueRef this_tag_int_value = bigint_to_llvm_const(get_llvm_type(g, tag_int_type),
42414246 &enum_type->data.enumeration.fields[field_i].value);
42424247 LLVMAddCase(switch_instr, this_tag_int_value, return_block);
42434248
......@@ -4283,22 +4288,21 @@ static LLVMValueRef ir_render_field_parent_ptr(CodeGen *g, IrExecutable *executa
42834288 ZigType *container_type = container_ptr_type->data.pointer.child_type;
42844289
42854290 size_t byte_offset = LLVMOffsetOfElement(g->target_data_ref,
4286 container_type->type_ref, instruction->field->gen_index);
4291 get_llvm_type(g, container_type), instruction->field->gen_index);
42874292
42884293 LLVMValueRef field_ptr_val = ir_llvm_value(g, instruction->field_ptr);
42894294
42904295 if (byte_offset == 0) {
4291 return LLVMBuildBitCast(g->builder, field_ptr_val, container_ptr_type->type_ref, "");
4296 return LLVMBuildBitCast(g->builder, field_ptr_val, get_llvm_type(g, container_ptr_type), "");
42924297 } else {
42934298 ZigType *usize = g->builtin_types.entry_usize;
42944299
4295 LLVMValueRef field_ptr_int = LLVMBuildPtrToInt(g->builder, field_ptr_val,
4296 usize->type_ref, "");
4300 LLVMValueRef field_ptr_int = LLVMBuildPtrToInt(g->builder, field_ptr_val, usize->llvm_type, "");
42974301
42984302 LLVMValueRef base_ptr_int = LLVMBuildNUWSub(g->builder, field_ptr_int,
4299 LLVMConstInt(usize->type_ref, byte_offset, false), "");
4303 LLVMConstInt(usize->llvm_type, byte_offset, false), "");
43004304
4301 return LLVMBuildIntToPtr(g->builder, base_ptr_int, container_ptr_type->type_ref, "");
4305 return LLVMBuildIntToPtr(g->builder, base_ptr_int, get_llvm_type(g, container_ptr_type), "");
43024306 }
43034307}
43044308
......@@ -4349,10 +4353,10 @@ static LLVMValueRef ir_render_align_cast(CodeGen *g, IrExecutable *executable, I
43494353 assert(align_bytes != 1);
43504354
43514355 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);
4356 LLVMValueRef ptr_as_int_val = LLVMBuildPtrToInt(g->builder, ptr_val, usize->llvm_type, "");
4357 LLVMValueRef alignment_minus_1 = LLVMConstInt(usize->llvm_type, align_bytes - 1, false);
43544358 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), "");
4359 LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, anded_val, LLVMConstNull(usize->llvm_type), "");
43564360
43574361 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "AlignCastOk");
43584362 LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn_val, "AlignCastFail");
......@@ -4373,7 +4377,7 @@ static LLVMValueRef ir_render_error_return_trace(CodeGen *g, IrExecutable *execu
43734377 LLVMValueRef cur_err_ret_trace_val = get_cur_err_ret_trace_val(g, instruction->base.scope);
43744378 if (cur_err_ret_trace_val == nullptr) {
43754379 ZigType *ptr_to_stack_trace_type = get_ptr_to_stack_trace_type(g);
4376 return LLVMConstNull(ptr_to_stack_trace_type->type_ref);
4380 return LLVMConstNull(get_llvm_type(g, ptr_to_stack_trace_type));
43774381 }
43784382 return cur_err_ret_trace_val;
43794383}
......@@ -4439,7 +4443,7 @@ static LLVMValueRef ir_render_cmpxchg(CodeGen *g, IrExecutable *executable, IrIn
44394443 if (!handle_is_ptr(maybe_type)) {
44404444 LLVMValueRef payload_val = LLVMBuildExtractValue(g->builder, result_val, 0, "");
44414445 LLVMValueRef success_bit = LLVMBuildExtractValue(g->builder, result_val, 1, "");
4442 return LLVMBuildSelect(g->builder, success_bit, LLVMConstNull(child_type->type_ref), payload_val, "");
4446 return LLVMBuildSelect(g->builder, success_bit, LLVMConstNull(get_llvm_type(g, child_type)), payload_val, "");
44434447 }
44444448
44454449 assert(instruction->tmp_ptr != nullptr);
......@@ -4470,10 +4474,10 @@ static LLVMValueRef ir_render_truncate(CodeGen *g, IrExecutable *executable, IrI
44704474 // no-op
44714475 return target_val;
44724476 } if (src_type->data.integral.bit_count == dest_type->data.integral.bit_count) {
4473 return LLVMBuildBitCast(g->builder, target_val, dest_type->type_ref, "");
4477 return LLVMBuildBitCast(g->builder, target_val, get_llvm_type(g, dest_type), "");
44744478 } else {
44754479 LLVMValueRef target_val = ir_llvm_value(g, instruction->target);
4476 return LLVMBuildTrunc(g->builder, target_val, dest_type->type_ref, "");
4480 return LLVMBuildTrunc(g->builder, target_val, get_llvm_type(g, dest_type), "");
44774481 }
44784482}
44794483
......@@ -4539,12 +4543,12 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutable *executable, IrInst
45394543 if (instruction->end) {
45404544 end_val = ir_llvm_value(g, instruction->end);
45414545 } else {
4542 end_val = LLVMConstInt(g->builtin_types.entry_usize->type_ref, array_type->data.array.len, false);
4546 end_val = LLVMConstInt(g->builtin_types.entry_usize->llvm_type, array_type->data.array.len, false);
45434547 }
45444548 if (want_runtime_safety) {
45454549 add_bounds_check(g, start_val, LLVMIntEQ, nullptr, LLVMIntULE, end_val);
45464550 if (instruction->end) {
4547 LLVMValueRef array_end = LLVMConstInt(g->builtin_types.entry_usize->type_ref,
4551 LLVMValueRef array_end = LLVMConstInt(g->builtin_types.entry_usize->llvm_type,
45484552 array_type->data.array.len, false);
45494553 add_bounds_check(g, end_val, LLVMIntEQ, nullptr, LLVMIntULE, array_end);
45504554 }
......@@ -4561,7 +4565,7 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutable *executable, IrInst
45614565
45624566 LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, slice_ptr_index, "");
45634567 LLVMValueRef indices[] = {
4564 LLVMConstNull(g->builtin_types.entry_usize->type_ref),
4568 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),
45654569 start_val,
45664570 };
45674571 LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP(g->builder, array_ptr, indices, 2, "");
......@@ -4663,9 +4667,9 @@ static LLVMValueRef ir_render_breakpoint(CodeGen *g, IrExecutable *executable, I
46634667static LLVMValueRef ir_render_return_address(CodeGen *g, IrExecutable *executable,
46644668 IrInstructionReturnAddress *instruction)
46654669{
4666 LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_i32->type_ref);
4670 LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_i32->llvm_type);
46674671 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, "");
4672 return LLVMBuildPtrToInt(g->builder, ptr_val, g->builtin_types.entry_usize->llvm_type, "");
46694673}
46704674
46714675static LLVMValueRef get_frame_address_fn_val(CodeGen *g) {
......@@ -4674,8 +4678,8 @@ static LLVMValueRef get_frame_address_fn_val(CodeGen *g) {
46744678
46754679 ZigType *return_type = get_pointer_to_type(g, g->builtin_types.entry_u8, true);
46764680
4677 LLVMTypeRef fn_type = LLVMFunctionType(return_type->type_ref,
4678 &g->builtin_types.entry_i32->type_ref, 1, false);
4681 LLVMTypeRef fn_type = LLVMFunctionType(get_llvm_type(g, return_type),
4682 &g->builtin_types.entry_i32->llvm_type, 1, false);
46794683 g->frame_address_fn_val = LLVMAddFunction(g->module, "llvm.frameaddress", fn_type);
46804684 assert(LLVMGetIntrinsicID(g->frame_address_fn_val));
46814685
......@@ -4685,9 +4689,9 @@ static LLVMValueRef get_frame_address_fn_val(CodeGen *g) {
46854689static LLVMValueRef ir_render_frame_address(CodeGen *g, IrExecutable *executable,
46864690 IrInstructionFrameAddress *instruction)
46874691{
4688 LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_i32->type_ref);
4692 LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_i32->llvm_type);
46894693 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, "");
4694 return LLVMBuildPtrToInt(g->builder, ptr_val, g->builtin_types.entry_usize->llvm_type, "");
46914695}
46924696
46934697static LLVMValueRef get_handle_fn_val(CodeGen *g) {
......@@ -4706,7 +4710,7 @@ static LLVMValueRef get_handle_fn_val(CodeGen *g) {
47064710static LLVMValueRef ir_render_handle(CodeGen *g, IrExecutable *executable,
47074711 IrInstructionHandle *instruction)
47084712{
4709 LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_promise->type_ref);
4713 LLVMValueRef zero = LLVMConstNull(get_llvm_type(g, g->builtin_types.entry_promise));
47104714 return LLVMBuildCall(g->builder, get_handle_fn_val(g), &zero, 0, "");
47114715}
47124716
......@@ -4786,7 +4790,7 @@ static LLVMValueRef ir_render_test_err(CodeGen *g, IrExecutable *executable, IrI
47864790 err_val = err_union_handle;
47874791 }
47884792
4789 LLVMValueRef zero = LLVMConstNull(g->err_tag_type->type_ref);
4793 LLVMValueRef zero = LLVMConstNull(get_llvm_type(g, g->err_tag_type));
47904794 return LLVMBuildICmp(g->builder, LLVMIntNE, err_val, zero, "");
47914795}
47924796
......@@ -4834,7 +4838,7 @@ static LLVMValueRef ir_render_unwrap_err_payload(CodeGen *g, IrExecutable *execu
48344838 } else {
48354839 err_val = err_union_handle;
48364840 }
4837 LLVMValueRef zero = LLVMConstNull(g->err_tag_type->type_ref);
4841 LLVMValueRef zero = LLVMConstNull(get_llvm_type(g, g->err_tag_type));
48384842 LLVMValueRef cond_val = LLVMBuildICmp(g->builder, LLVMIntEQ, err_val, zero, "");
48394843 LLVMBasicBlockRef err_block = LLVMAppendBasicBlock(g->cur_fn_val, "UnwrapErrError");
48404844 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "UnwrapErrOk");
......@@ -4860,7 +4864,7 @@ static LLVMValueRef ir_render_maybe_wrap(CodeGen *g, IrExecutable *executable, I
48604864
48614865 ZigType *child_type = wanted_type->data.maybe.child_type;
48624866
4863 if (child_type->zero_bits) {
4867 if (!type_has_bits(child_type)) {
48644868 return LLVMConstInt(LLVMInt1Type(), 1, false);
48654869 }
48664870
......@@ -4913,7 +4917,7 @@ static LLVMValueRef ir_render_err_wrap_payload(CodeGen *g, IrExecutable *executa
49134917 return ir_llvm_value(g, instruction->value);
49144918 }
49154919
4916 LLVMValueRef ok_err_val = LLVMConstNull(g->err_tag_type->type_ref);
4920 LLVMValueRef ok_err_val = LLVMConstNull(get_llvm_type(g, g->err_tag_type));
49174921
49184922 if (!type_has_bits(payload_type))
49194923 return ok_err_val;
......@@ -4991,7 +4995,7 @@ static LLVMValueRef ir_render_union_init(CodeGen *g, IrExecutable *executable, I
49914995 LLVMValueRef tag_field_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr,
49924996 union_type->data.unionation.gen_tag_index, "");
49934997
4994 LLVMValueRef tag_value = bigint_to_llvm_const(union_type->data.unionation.tag_type->type_ref,
4998 LLVMValueRef tag_value = bigint_to_llvm_const(get_llvm_type(g, union_type->data.unionation.tag_type),
49954999 &type_union_field->enum_field->value);
49965000 gen_store_untyped(g, tag_value, tag_field_ptr, 0, false);
49975001
......@@ -5001,7 +5005,7 @@ static LLVMValueRef ir_render_union_init(CodeGen *g, IrExecutable *executable, I
50015005 uncasted_union_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, (unsigned)0, "");
50025006 }
50035007
5004 LLVMValueRef field_ptr = LLVMBuildBitCast(g->builder, uncasted_union_ptr, ptr_type->type_ref, "");
5008 LLVMValueRef field_ptr = LLVMBuildBitCast(g->builder, uncasted_union_ptr, get_llvm_type(g, ptr_type), "");
50055009 LLVMValueRef value = ir_llvm_value(g, instruction->init_value);
50065010
50075011 gen_assign_raw(g, field_ptr, ptr_type, value);
......@@ -5023,8 +5027,8 @@ static LLVMValueRef ir_render_container_init_list(CodeGen *g, IrExecutable *exec
50235027 for (size_t i = 0; i < field_count; i += 1) {
50245028 LLVMValueRef elem_val = ir_llvm_value(g, instruction->items[i]);
50255029 LLVMValueRef indices[] = {
5026 LLVMConstNull(g->builtin_types.entry_usize->type_ref),
5027 LLVMConstInt(g->builtin_types.entry_usize->type_ref, i, false),
5030 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),
5031 LLVMConstInt(g->builtin_types.entry_usize->llvm_type, i, false),
50285032 };
50295033 LLVMValueRef elem_ptr = LLVMBuildInBoundsGEP(g->builder, tmp_array_ptr, indices, 2, "");
50305034 gen_assign_raw(g, elem_ptr, get_pointer_to_type(g, child_type, false), elem_val);
......@@ -5041,7 +5045,7 @@ static LLVMValueRef ir_render_panic(CodeGen *g, IrExecutable *executable, IrInst
50415045static LLVMValueRef ir_render_coro_id(CodeGen *g, IrExecutable *executable, IrInstructionCoroId *instruction) {
50425046 LLVMValueRef promise_ptr = ir_llvm_value(g, instruction->promise_ptr);
50435047 LLVMValueRef align_val = LLVMConstInt(LLVMInt32Type(), get_coro_frame_align_bytes(g), false);
5044 LLVMValueRef null = LLVMConstIntToPtr(LLVMConstNull(g->builtin_types.entry_usize->type_ref),
5048 LLVMValueRef null = LLVMConstIntToPtr(LLVMConstNull(g->builtin_types.entry_usize->llvm_type),
50455049 LLVMPointerType(LLVMInt8Type(), 0));
50465050 LLVMValueRef params[] = {
50475051 align_val,
......@@ -5140,7 +5144,7 @@ static LLVMValueRef ir_render_coro_promise(CodeGen *g, IrExecutable *executable,
51405144 LLVMConstNull(LLVMInt1Type()),
51415145 };
51425146 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, "");
5147 return LLVMBuildBitCast(g->builder, uncasted_result, get_llvm_type(g, instruction->base.value.type), "");
51445148}
51455149
51465150static LLVMValueRef get_coro_alloc_helper_fn_val(CodeGen *g, LLVMTypeRef alloc_fn_type_ref, ZigType *fn_type) {
......@@ -5161,8 +5165,8 @@ static LLVMValueRef get_coro_alloc_helper_fn_val(CodeGen *g, LLVMTypeRef alloc_f
51615165 arg_types.append(alloc_fn_arg_types[1]);
51625166 }
51635167 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);
5168 arg_types.append(get_llvm_type(g, ptr_to_err_code_type));
5169 arg_types.append(g->builtin_types.entry_usize->llvm_type);
51665170
51675171 LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMPointerType(LLVMInt8Type(), 0),
51685172 arg_types.items, arg_types.length, false);
......@@ -5204,7 +5208,7 @@ static LLVMValueRef get_coro_alloc_helper_fn_val(CodeGen *g, LLVMTypeRef alloc_f
52045208 next_arg += 1;
52055209 LLVMValueRef coro_size = LLVMGetParam(fn_val, next_arg);
52065210 next_arg += 1;
5207 LLVMValueRef alignment_val = LLVMConstInt(g->builtin_types.entry_u29->type_ref,
5211 LLVMValueRef alignment_val = LLVMConstInt(g->builtin_types.entry_u29->llvm_type,
52085212 get_coro_frame_align_bytes(g), false);
52095213
52105214 ConstExprValue *zero_array = create_const_str_lit(g, buf_create_from_str(""));
......@@ -5219,7 +5223,7 @@ static LLVMValueRef get_coro_alloc_helper_fn_val(CodeGen *g, LLVMTypeRef alloc_f
52195223 }
52205224 args.append(allocator_val);
52215225 args.append(undef_slice_zero->global_refs->llvm_global);
5222 args.append(LLVMGetUndef(g->builtin_types.entry_u29->type_ref));
5226 args.append(LLVMGetUndef(g->builtin_types.entry_u29->llvm_type));
52235227 args.append(coro_size);
52245228 args.append(alignment_val);
52255229 LLVMValueRef call_instruction = ZigLLVMBuildCall(g->builder, realloc_fn_val, args.items, args.length,
......@@ -5299,10 +5303,10 @@ static LLVMValueRef ir_render_atomic_rmw(CodeGen *g, IrExecutable *executable,
52995303
53005304 // it's a pointer but we need to treat it as an int
53015305 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, "");
5306 LLVMPointerType(g->builtin_types.entry_usize->llvm_type, 0), "");
5307 LLVMValueRef casted_operand = LLVMBuildPtrToInt(g->builder, operand, g->builtin_types.entry_usize->llvm_type, "");
53045308 LLVMValueRef uncasted_result = LLVMBuildAtomicRMW(g->builder, op, casted_ptr, casted_operand, ordering, false);
5305 return LLVMBuildIntToPtr(g->builder, uncasted_result, operand_type->type_ref, "");
5309 return LLVMBuildIntToPtr(g->builder, uncasted_result, get_llvm_type(g, operand_type), "");
53065310}
53075311
53085312static LLVMValueRef ir_render_atomic_load(CodeGen *g, IrExecutable *executable,
......@@ -5355,15 +5359,15 @@ static LLVMValueRef ir_render_bswap(CodeGen *g, IrExecutable *executable, IrInst
53555359 ZigType *extended_type = get_int_type(g, int_type->data.integral.is_signed,
53565360 int_type->data.integral.bit_count + 8);
53575361 // aabbcc
5358 LLVMValueRef extended = LLVMBuildZExt(g->builder, op, extended_type->type_ref, "");
5362 LLVMValueRef extended = LLVMBuildZExt(g->builder, op, get_llvm_type(g, extended_type), "");
53595363 // 00aabbcc
53605364 LLVMValueRef fn_val = get_int_builtin_fn(g, extended_type, BuiltinFnIdBswap);
53615365 LLVMValueRef swapped = LLVMBuildCall(g->builder, fn_val, &extended, 1, "");
53625366 // ccbbaa00
53635367 LLVMValueRef shifted = ZigLLVMBuildLShrExact(g->builder, swapped,
5364 LLVMConstInt(extended_type->type_ref, 8, false), "");
5368 LLVMConstInt(get_llvm_type(g, extended_type), 8, false), "");
53655369 // 00ccbbaa
5366 return LLVMBuildTrunc(g->builder, shifted, int_type->type_ref, "");
5370 return LLVMBuildTrunc(g->builder, shifted, get_llvm_type(g, int_type), "");
53675371}
53685372
53695373static LLVMValueRef ir_render_bit_reverse(CodeGen *g, IrExecutable *executable, IrInstructionBitReverse *instruction) {
......@@ -5383,7 +5387,7 @@ static LLVMValueRef ir_render_vector_to_array(CodeGen *g, IrExecutable *executab
53835387 assert(instruction->tmp_ptr);
53845388 LLVMValueRef vector = ir_llvm_value(g, instruction->vector);
53855389 LLVMValueRef casted_ptr = LLVMBuildBitCast(g->builder, instruction->tmp_ptr,
5386 LLVMPointerType(instruction->vector->value.type->type_ref, 0), "");
5390 LLVMPointerType(get_llvm_type(g, instruction->vector->value.type), 0), "");
53875391 gen_store_untyped(g, vector, casted_ptr, 0, false);
53885392 return instruction->tmp_ptr;
53895393}
......@@ -5396,7 +5400,7 @@ static LLVMValueRef ir_render_array_to_vector(CodeGen *g, IrExecutable *executab
53965400 assert(!handle_is_ptr(vector_type));
53975401 LLVMValueRef array_ptr = ir_llvm_value(g, instruction->array);
53985402 LLVMValueRef casted_ptr = LLVMBuildBitCast(g->builder, array_ptr,
5399 LLVMPointerType(vector_type->type_ref, 0), "");
5403 LLVMPointerType(get_llvm_type(g, vector_type), 0), "");
54005404 return gen_load_untyped(g, casted_ptr, 0, false, "");
54015405}
54025406
......@@ -5735,15 +5739,15 @@ static LLVMValueRef gen_const_ptr_array_recursive(CodeGen *g, ConstExprValue *ar
57355739 if (el_type == LLVMArrayTypeKind) {
57365740 ZigType *usize = g->builtin_types.entry_usize;
57375741 LLVMValueRef indices[] = {
5738 LLVMConstNull(usize->type_ref),
5739 LLVMConstInt(usize->type_ref, index, false),
5742 LLVMConstNull(usize->llvm_type),
5743 LLVMConstInt(usize->llvm_type, index, false),
57405744 };
57415745 return LLVMConstInBoundsGEP(base_ptr, indices, 2);
57425746 } else if (el_type == LLVMStructTypeKind) {
57435747 ZigType *u32 = g->builtin_types.entry_u32;
57445748 LLVMValueRef indices[] = {
5745 LLVMConstNull(u32->type_ref),
5746 LLVMConstInt(u32->type_ref, index, false),
5749 LLVMConstNull(get_llvm_type(g, u32)),
5750 LLVMConstInt(get_llvm_type(g, u32), index, false),
57475751 };
57485752 return LLVMConstInBoundsGEP(base_ptr, indices, 2);
57495753 } else {
......@@ -5758,8 +5762,8 @@ static LLVMValueRef gen_const_ptr_struct_recursive(CodeGen *g, ConstExprValue *s
57585762
57595763 ZigType *u32 = g->builtin_types.entry_u32;
57605764 LLVMValueRef indices[] = {
5761 LLVMConstNull(u32->type_ref),
5762 LLVMConstInt(u32->type_ref, field_index, false),
5765 LLVMConstNull(get_llvm_type(g, u32)),
5766 LLVMConstInt(get_llvm_type(g, u32), field_index, false),
57635767 };
57645768 return LLVMConstInBoundsGEP(base_ptr, indices, 2);
57655769}
......@@ -5770,8 +5774,8 @@ static LLVMValueRef gen_const_ptr_err_union_code_recursive(CodeGen *g, ConstExpr
57705774
57715775 ZigType *u32 = g->builtin_types.entry_u32;
57725776 LLVMValueRef indices[] = {
5773 LLVMConstNull(u32->type_ref),
5774 LLVMConstInt(u32->type_ref, err_union_err_index, false),
5777 LLVMConstNull(get_llvm_type(g, u32)),
5778 LLVMConstInt(get_llvm_type(g, u32), err_union_err_index, false),
57755779 };
57765780 return LLVMConstInBoundsGEP(base_ptr, indices, 2);
57775781}
......@@ -5782,8 +5786,8 @@ static LLVMValueRef gen_const_ptr_err_union_payload_recursive(CodeGen *g, ConstE
57825786
57835787 ZigType *u32 = g->builtin_types.entry_u32;
57845788 LLVMValueRef indices[] = {
5785 LLVMConstNull(u32->type_ref),
5786 LLVMConstInt(u32->type_ref, err_union_payload_index, false),
5789 LLVMConstNull(get_llvm_type(g, u32)),
5790 LLVMConstInt(get_llvm_type(g, u32), err_union_payload_index, false),
57875791 };
57885792 return LLVMConstInBoundsGEP(base_ptr, indices, 2);
57895793}
......@@ -5794,8 +5798,8 @@ static LLVMValueRef gen_const_ptr_optional_payload_recursive(CodeGen *g, ConstEx
57945798
57955799 ZigType *u32 = g->builtin_types.entry_u32;
57965800 LLVMValueRef indices[] = {
5797 LLVMConstNull(u32->type_ref),
5798 LLVMConstInt(u32->type_ref, maybe_child_index, false),
5801 LLVMConstNull(get_llvm_type(g, u32)),
5802 LLVMConstInt(get_llvm_type(g, u32), maybe_child_index, false),
57995803 };
58005804 return LLVMConstInBoundsGEP(base_ptr, indices, 2);
58015805}
......@@ -5806,8 +5810,8 @@ static LLVMValueRef gen_const_ptr_union_recursive(CodeGen *g, ConstExprValue *un
58065810
58075811 ZigType *u32 = g->builtin_types.entry_u32;
58085812 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
5813 LLVMConstNull(get_llvm_type(g, u32)),
5814 LLVMConstInt(get_llvm_type(g, u32), 0, false), // TODO test const union with more aligned tag type than payload
58115815 };
58125816 return LLVMConstInBoundsGEP(base_ptr, indices, 2);
58135817}
......@@ -5823,7 +5827,7 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con
58235827 }
58245828
58255829 ZigType *type_entry = const_val->type;
5826 assert(!type_entry->zero_bits);
5830 assert(type_has_bits(type_entry));
58275831 switch (type_entry->id) {
58285832 case ZigTypeIdInvalid:
58295833 case ZigTypeIdMetaType:
......@@ -5866,7 +5870,7 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con
58665870 case ZigTypeIdPromise:
58675871 {
58685872 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);
5873 LLVMValueRef ptr_size_int_val = LLVMConstPtrToInt(ptr_val, g->builtin_types.entry_usize->llvm_type);
58705874 return LLVMConstZExt(ptr_size_int_val, big_int_type_ref);
58715875 }
58725876 case ZigTypeIdArray: {
......@@ -5933,8 +5937,8 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con
59335937
59345938// We have this because union constants can't be represented by the official union type,
59355939// 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;
5940static bool is_llvm_value_unnamed_type(CodeGen *g, ZigType *type_entry, LLVMValueRef val) {
5941 return LLVMTypeOf(val) != get_llvm_type(g, type_entry);
59385942}
59395943
59405944static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, const char *name) {
......@@ -5948,7 +5952,8 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
59485952 ConstExprValue *pointee = const_val->data.x_ptr.data.ref.pointee;
59495953 render_const_val(g, pointee, "");
59505954 render_const_val_global(g, pointee, "");
5951 const_val->global_refs->llvm_value = LLVMConstBitCast(pointee->global_refs->llvm_global, const_val->type->type_ref);
5955 const_val->global_refs->llvm_value = LLVMConstBitCast(pointee->global_refs->llvm_global,
5956 get_llvm_type(g, const_val->type));
59525957 return const_val->global_refs->llvm_value;
59535958 }
59545959 case ConstPtrSpecialBaseArray:
......@@ -5959,13 +5964,13 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
59595964 if (!type_has_bits(array_const_val->type)) {
59605965 // make this a null pointer
59615966 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);
5967 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type),
5968 get_llvm_type(g, const_val->type));
59645969 return const_val->global_refs->llvm_value;
59655970 }
59665971 size_t elem_index = const_val->data.x_ptr.data.base_array.elem_index;
59675972 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);
5973 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, get_llvm_type(g, const_val->type));
59695974 const_val->global_refs->llvm_value = ptr_val;
59705975 return ptr_val;
59715976 }
......@@ -5977,15 +5982,15 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
59775982 if (!type_has_bits(struct_const_val->type)) {
59785983 // make this a null pointer
59795984 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);
5985 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type),
5986 get_llvm_type(g, const_val->type));
59825987 return const_val->global_refs->llvm_value;
59835988 }
59845989 size_t src_field_index = const_val->data.x_ptr.data.base_struct.field_index;
59855990 size_t gen_field_index = struct_const_val->type->data.structure.fields[src_field_index].gen_index;
59865991 LLVMValueRef uncasted_ptr_val = gen_const_ptr_struct_recursive(g, struct_const_val,
59875992 gen_field_index);
5988 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, const_val->type->type_ref);
5993 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, get_llvm_type(g, const_val->type));
59895994 const_val->global_refs->llvm_value = ptr_val;
59905995 return ptr_val;
59915996 }
......@@ -5997,12 +6002,12 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
59976002 if (!type_has_bits(err_union_const_val->type)) {
59986003 // make this a null pointer
59996004 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);
6005 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type),
6006 get_llvm_type(g, const_val->type));
60026007 return const_val->global_refs->llvm_value;
60036008 }
60046009 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);
6010 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, get_llvm_type(g, const_val->type));
60066011 const_val->global_refs->llvm_value = ptr_val;
60076012 return ptr_val;
60086013 }
......@@ -6011,15 +6016,15 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
60116016 assert(const_val->global_refs != nullptr);
60126017 ConstExprValue *err_union_const_val = const_val->data.x_ptr.data.base_err_union_payload.err_union_val;
60136018 assert(err_union_const_val->type->id == ZigTypeIdErrorUnion);
6014 if (err_union_const_val->type->zero_bits) {
6019 if (!type_has_bits(err_union_const_val->type)) {
60156020 // make this a null pointer
60166021 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);
6022 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type),
6023 get_llvm_type(g, const_val->type));
60196024 return const_val->global_refs->llvm_value;
60206025 }
60216026 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);
6027 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, get_llvm_type(g, const_val->type));
60236028 const_val->global_refs->llvm_value = ptr_val;
60246029 return ptr_val;
60256030 }
......@@ -6028,15 +6033,15 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
60286033 assert(const_val->global_refs != nullptr);
60296034 ConstExprValue *optional_const_val = const_val->data.x_ptr.data.base_optional_payload.optional_val;
60306035 assert(optional_const_val->type->id == ZigTypeIdOptional);
6031 if (optional_const_val->type->zero_bits) {
6036 if (!type_has_bits(optional_const_val->type)) {
60326037 // make this a null pointer
60336038 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);
6039 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type),
6040 get_llvm_type(g, const_val->type));
60366041 return const_val->global_refs->llvm_value;
60376042 }
60386043 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);
6044 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, get_llvm_type(g, const_val->type));
60406045 const_val->global_refs->llvm_value = ptr_val;
60416046 return ptr_val;
60426047 }
......@@ -6046,50 +6051,51 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
60466051 uint64_t addr_value = const_val->data.x_ptr.data.hard_coded_addr.addr;
60476052 ZigType *usize = g->builtin_types.entry_usize;
60486053 const_val->global_refs->llvm_value = LLVMConstIntToPtr(
6049 LLVMConstInt(usize->type_ref, addr_value, false), const_val->type->type_ref);
6054 LLVMConstInt(usize->llvm_type, addr_value, false), get_llvm_type(g, const_val->type));
60506055 return const_val->global_refs->llvm_value;
60516056 }
60526057 case ConstPtrSpecialFunction:
6053 return LLVMConstBitCast(fn_llvm_value(g, const_val->data.x_ptr.data.fn.fn_entry), const_val->type->type_ref);
6058 return LLVMConstBitCast(fn_llvm_value(g, const_val->data.x_ptr.data.fn.fn_entry),
6059 get_llvm_type(g, const_val->type));
60546060 case ConstPtrSpecialNull:
6055 return LLVMConstNull(const_val->type->type_ref);
6061 return LLVMConstNull(get_llvm_type(g, const_val->type));
60566062 }
60576063 zig_unreachable();
60586064}
60596065
60606066static LLVMValueRef gen_const_val_err_set(CodeGen *g, ConstExprValue *const_val, const char *name) {
60616067 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);
6068 return LLVMConstInt(get_llvm_type(g, g->builtin_types.entry_global_error_set), value, false);
60636069}
60646070
60656071static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const char *name) {
60666072 Error err;
60676073
60686074 ZigType *type_entry = const_val->type;
6069 assert(!type_entry->zero_bits);
6075 assert(type_has_bits(type_entry));
60706076
60716077 switch (const_val->special) {
60726078 case ConstValSpecialRuntime:
60736079 zig_unreachable();
60746080 case ConstValSpecialUndef:
6075 return LLVMGetUndef(type_entry->type_ref);
6081 return LLVMGetUndef(get_llvm_type(g, type_entry));
60766082 case ConstValSpecialStatic:
60776083 break;
60786084 }
60796085
60806086 switch (type_entry->id) {
60816087 case ZigTypeIdInt:
6082 return bigint_to_llvm_const(type_entry->type_ref, &const_val->data.x_bigint);
6088 return bigint_to_llvm_const(get_llvm_type(g, type_entry), &const_val->data.x_bigint);
60836089 case ZigTypeIdErrorSet:
60846090 return gen_const_val_err_set(g, const_val, name);
60856091 case ZigTypeIdFloat:
60866092 switch (type_entry->data.floating.bit_count) {
60876093 case 16:
6088 return LLVMConstReal(type_entry->type_ref, zig_f16_to_double(const_val->data.x_f16));
6094 return LLVMConstReal(get_llvm_type(g, type_entry), zig_f16_to_double(const_val->data.x_f16));
60896095 case 32:
6090 return LLVMConstReal(type_entry->type_ref, const_val->data.x_f32);
6096 return LLVMConstReal(get_llvm_type(g, type_entry), const_val->data.x_f32);
60916097 case 64:
6092 return LLVMConstReal(type_entry->type_ref, const_val->data.x_f64);
6098 return LLVMConstReal(get_llvm_type(g, type_entry), const_val->data.x_f64);
60936099 case 128:
60946100 {
60956101 // TODO make sure this is correct on big endian targets too
......@@ -6097,7 +6103,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
60976103 memcpy(buf, &const_val->data.x_f128, 16);
60986104 LLVMValueRef as_int = LLVMConstIntOfArbitraryPrecision(LLVMInt128Type(), 2,
60996105 (uint64_t*)buf);
6100 return LLVMConstBitCast(as_int, type_entry->type_ref);
6106 return LLVMConstBitCast(as_int, get_llvm_type(g, type_entry));
61016107 }
61026108 default:
61036109 zig_unreachable();
......@@ -6125,9 +6131,9 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
61256131 child_val = gen_const_val(g, const_val->data.x_optional, "");
61266132 maybe_val = LLVMConstAllOnes(LLVMInt1Type());
61276133
6128 make_unnamed_struct = is_llvm_value_unnamed_type(const_val->type, child_val);
6134 make_unnamed_struct = is_llvm_value_unnamed_type(g, const_val->type, child_val);
61296135 } else {
6130 child_val = LLVMGetUndef(child_type->type_ref);
6136 child_val = LLVMGetUndef(get_llvm_type(g, child_type));
61316137 maybe_val = LLVMConstNull(LLVMInt1Type());
61326138
61336139 make_unnamed_struct = false;
......@@ -6139,7 +6145,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
61396145 if (make_unnamed_struct) {
61406146 return LLVMConstStruct(fields, 2, false);
61416147 } else {
6142 return LLVMConstNamedStruct(type_entry->type_ref, fields, 2);
6148 return LLVMConstNamedStruct(get_llvm_type(g, type_entry), fields, 2);
61436149 }
61446150 }
61456151 }
......@@ -6168,10 +6174,10 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
61686174 ConstExprValue *field_val = &const_val->data.x_struct.fields[src_field_index];
61696175 LLVMValueRef val = gen_const_val(g, field_val, "");
61706176 fields[type_struct_field->gen_index] = val;
6171 make_unnamed_struct = make_unnamed_struct || is_llvm_value_unnamed_type(field_val->type, val);
6177 make_unnamed_struct = make_unnamed_struct || is_llvm_value_unnamed_type(g, field_val->type, val);
61726178 } else {
61736179 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,
6180 LLVMTypeRef big_int_type_ref = LLVMStructGetTypeAtIndex(get_llvm_type(g, type_entry),
61756181 (unsigned)type_struct_field->gen_index);
61766182 LLVMValueRef val = LLVMConstInt(big_int_type_ref, 0, false);
61776183 size_t used_bits = 0;
......@@ -6216,14 +6222,14 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
62166222
62176223 LLVMValueRef val = gen_const_val(g, field_val, "");
62186224 fields[type_struct_field->gen_index] = val;
6219 make_unnamed_struct = make_unnamed_struct || is_llvm_value_unnamed_type(field_val->type, val);
6225 make_unnamed_struct = make_unnamed_struct || is_llvm_value_unnamed_type(g, field_val->type, val);
62206226 }
62216227 }
62226228 if (make_unnamed_struct) {
62236229 return LLVMConstStruct(fields, type_entry->data.structure.gen_field_count,
62246230 type_entry->data.structure.layout == ContainerLayoutPacked);
62256231 } else {
6226 return LLVMConstNamedStruct(type_entry->type_ref, fields, type_entry->data.structure.gen_field_count);
6232 return LLVMConstNamedStruct(get_llvm_type(g, type_entry), fields, type_entry->data.structure.gen_field_count);
62276233 }
62286234 }
62296235 case ZigTypeIdArray:
......@@ -6231,16 +6237,16 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
62316237 uint64_t len = type_entry->data.array.len;
62326238 switch (const_val->data.x_array.special) {
62336239 case ConstArraySpecialUndef:
6234 return LLVMGetUndef(type_entry->type_ref);
6240 return LLVMGetUndef(get_llvm_type(g, type_entry));
62356241 case ConstArraySpecialNone: {
62366242 LLVMValueRef *values = allocate<LLVMValueRef>(len);
6237 LLVMTypeRef element_type_ref = type_entry->data.array.child_type->type_ref;
6243 LLVMTypeRef element_type_ref = get_llvm_type(g, type_entry->data.array.child_type);
62386244 bool make_unnamed_struct = false;
62396245 for (uint64_t i = 0; i < len; i += 1) {
62406246 ConstExprValue *elem_value = &const_val->data.x_array.data.s_none.elements[i];
62416247 LLVMValueRef val = gen_const_val(g, elem_value, "");
62426248 values[i] = val;
6243 make_unnamed_struct = make_unnamed_struct || is_llvm_value_unnamed_type(elem_value->type, val);
6249 make_unnamed_struct = make_unnamed_struct || is_llvm_value_unnamed_type(g, elem_value->type, val);
62446250 }
62456251 if (make_unnamed_struct) {
62466252 return LLVMConstStruct(values, len, true);
......@@ -6259,7 +6265,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
62596265 uint32_t len = type_entry->data.vector.len;
62606266 switch (const_val->data.x_array.special) {
62616267 case ConstArraySpecialUndef:
6262 return LLVMGetUndef(type_entry->type_ref);
6268 return LLVMGetUndef(get_llvm_type(g, type_entry));
62636269 case ConstArraySpecialNone: {
62646270 LLVMValueRef *values = allocate<LLVMValueRef>(len);
62656271 for (uint64_t i = 0; i < len; i += 1) {
......@@ -6273,7 +6279,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
62736279 assert(buf_len(buf) == len);
62746280 LLVMValueRef *values = allocate<LLVMValueRef>(len);
62756281 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);
6282 values[i] = LLVMConstInt(g->builtin_types.entry_u8->llvm_type, buf_ptr(buf)[i], false);
62776283 }
62786284 return LLVMConstVector(values, len);
62796285 }
......@@ -6282,13 +6288,19 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
62826288 }
62836289 case ZigTypeIdUnion:
62846290 {
6285 LLVMTypeRef union_type_ref = type_entry->data.unionation.union_type_ref;
6291 BREAKPOINT; // TODO rework this logic to take into account the new layout
6292
6293 // Force type_entry->data.unionation.union_llvm_type to get resolved
6294 (void)get_llvm_type(g, type_entry);
6295
6296 LLVMTypeRef union_type_ref = type_entry->data.unionation.union_llvm_type;
6297 assert(union_type_ref != nullptr);
62866298
62876299 if (type_entry->data.unionation.gen_field_count == 0) {
62886300 if (type_entry->data.unionation.tag_type == nullptr) {
62896301 return nullptr;
62906302 } else {
6291 return bigint_to_llvm_const(type_entry->data.unionation.tag_type->type_ref,
6303 return bigint_to_llvm_const(get_llvm_type(g, type_entry->data.unionation.tag_type),
62926304 &const_val->data.x_union.tag);
62936305 }
62946306 }
......@@ -6298,15 +6310,16 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
62986310 ConstExprValue *payload_value = const_val->data.x_union.payload;
62996311 if (payload_value == nullptr || !type_has_bits(payload_value->type)) {
63006312 if (type_entry->data.unionation.gen_tag_index == SIZE_MAX)
6301 return LLVMGetUndef(type_entry->type_ref);
6313 return LLVMGetUndef(get_llvm_type(g, type_entry));
63026314
63036315 union_value_ref = LLVMGetUndef(union_type_ref);
63046316 make_unnamed_struct = false;
63056317 } 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;
6318 uint64_t field_type_bytes = LLVMStoreSizeOfType(g->target_data_ref,
6319 get_llvm_type(g, payload_value->type));
6320 uint64_t pad_bytes = type_entry->data.unionation.union_abi_size - field_type_bytes;
63086321 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) ||
6322 make_unnamed_struct = is_llvm_value_unnamed_type(g, payload_value->type, correctly_typed_value) ||
63106323 payload_value->type != type_entry->data.unionation.most_aligned_union_member;
63116324
63126325 {
......@@ -6329,7 +6342,8 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
63296342 }
63306343 }
63316344
6332 LLVMValueRef tag_value = bigint_to_llvm_const(type_entry->data.unionation.tag_type->type_ref,
6345 LLVMValueRef tag_value = bigint_to_llvm_const(
6346 get_llvm_type(g, type_entry->data.unionation.tag_type),
63336347 &const_val->data.x_union.tag);
63346348
63356349 LLVMValueRef fields[3];
......@@ -6341,7 +6355,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
63416355 uint64_t last_field_offset = LLVMOffsetOfElement(g->target_data_ref, LLVMTypeOf(result), 1);
63426356 uint64_t end_offset = last_field_offset +
63436357 LLVMStoreSizeOfType(g->target_data_ref, LLVMTypeOf(fields[1]));
6344 uint64_t expected_sz = LLVMStoreSizeOfType(g->target_data_ref, type_entry->type_ref);
6358 uint64_t expected_sz = LLVMStoreSizeOfType(g->target_data_ref, get_llvm_type(g, type_entry));
63456359 unsigned pad_sz = expected_sz - end_offset;
63466360 if (pad_sz != 0) {
63476361 fields[2] = LLVMGetUndef(LLVMArrayType(LLVMInt8Type(), pad_sz));
......@@ -6351,21 +6365,21 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
63516365 assert(actual_sz == expected_sz);
63526366 return result;
63536367 } else {
6354 return LLVMConstNamedStruct(type_entry->type_ref, fields, 2);
6368 return LLVMConstNamedStruct(get_llvm_type(g, type_entry), fields, 2);
63556369 }
63566370
63576371 }
63586372
63596373 case ZigTypeIdEnum:
6360 return bigint_to_llvm_const(type_entry->type_ref, &const_val->data.x_enum_tag);
6374 return bigint_to_llvm_const(get_llvm_type(g, type_entry), &const_val->data.x_enum_tag);
63616375 case ZigTypeIdFn:
63626376 if (const_val->data.x_ptr.special == ConstPtrSpecialFunction) {
63636377 assert(const_val->data.x_ptr.mut == ConstPtrMutComptimeConst);
63646378 return fn_llvm_value(g, const_val->data.x_ptr.data.fn.fn_entry);
63656379 } else if (const_val->data.x_ptr.special == ConstPtrSpecialHardCodedAddr) {
6366 LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->type_ref;
6380 LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->llvm_type;
63676381 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);
6382 return LLVMConstIntToPtr(LLVMConstInt(usize_type_ref, addr, false), get_llvm_type(g, type_entry));
63696383 } else {
63706384 zig_unreachable();
63716385 }
......@@ -6379,7 +6393,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
63796393 assert(type_has_bits(err_set_type));
63806394 ErrorTableEntry *err_set = const_val->data.x_err_union.error_set->data.x_err_set;
63816395 uint64_t value = (err_set == nullptr) ? 0 : err_set->value;
6382 return LLVMConstInt(g->err_tag_type->type_ref, value, false);
6396 return LLVMConstInt(get_llvm_type(g, g->err_tag_type), value, false);
63836397 } else if (!type_has_bits(err_set_type)) {
63846398 assert(type_has_bits(payload_type));
63856399 return gen_const_val(g, const_val->data.x_err_union.payload, "");
......@@ -6389,17 +6403,17 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
63896403 bool make_unnamed_struct;
63906404 ErrorTableEntry *err_set = const_val->data.x_err_union.error_set->data.x_err_set;
63916405 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);
6406 err_tag_value = LLVMConstInt(get_llvm_type(g, g->err_tag_type), err_set->value, false);
6407 err_payload_value = LLVMConstNull(get_llvm_type(g, payload_type));
63946408 make_unnamed_struct = false;
63956409 } else {
6396 err_tag_value = LLVMConstNull(g->err_tag_type->type_ref);
6410 err_tag_value = LLVMConstNull(get_llvm_type(g, g->err_tag_type));
63976411 ConstExprValue *payload_val = const_val->data.x_err_union.payload;
63986412 err_payload_value = gen_const_val(g, payload_val, "");
6399 make_unnamed_struct = is_llvm_value_unnamed_type(payload_val->type, err_payload_value);
6413 make_unnamed_struct = is_llvm_value_unnamed_type(g, payload_val->type, err_payload_value);
64006414 }
64016415 if (make_unnamed_struct) {
6402 uint64_t payload_off = LLVMOffsetOfElement(g->target_data_ref, type_entry->type_ref, 1);
6416 uint64_t payload_off = LLVMOffsetOfElement(g->target_data_ref, get_llvm_type(g, type_entry), 1);
64036417 uint64_t err_sz = LLVMStoreSizeOfType(g->target_data_ref, LLVMTypeOf(err_tag_value));
64046418 unsigned pad_sz = payload_off - err_sz;
64056419 if (pad_sz == 0) {
......@@ -6421,7 +6435,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
64216435 err_tag_value,
64226436 err_payload_value,
64236437 };
6424 return LLVMConstNamedStruct(type_entry->type_ref, fields, 2);
6438 return LLVMConstNamedStruct(get_llvm_type(g, type_entry), fields, 2);
64256439 }
64266440 }
64276441 }
......@@ -6460,7 +6474,8 @@ static void render_const_val_global(CodeGen *g, ConstExprValue *const_val, const
64606474 const_val->global_refs = allocate<ConstGlobalRefs>(1);
64616475
64626476 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;
6477 LLVMTypeRef type_ref = const_val->global_refs->llvm_value ?
6478 LLVMTypeOf(const_val->global_refs->llvm_value) : get_llvm_type(g, const_val->type);
64646479 LLVMValueRef global_value = LLVMAddGlobal(g->module, type_ref, name);
64656480 LLVMSetLinkage(global_value, LLVMInternalLinkage);
64666481 LLVMSetGlobalConstant(global_value, true);
......@@ -6486,7 +6501,7 @@ static void generate_error_name_table(CodeGen *g) {
64866501 ZigType *str_type = get_slice_type(g, u8_ptr_type);
64876502
64886503 LLVMValueRef *values = allocate<LLVMValueRef>(g->errors_by_index.length);
6489 values[0] = LLVMGetUndef(str_type->type_ref);
6504 values[0] = LLVMGetUndef(get_llvm_type(g, str_type));
64906505 for (size_t i = 1; i < g->errors_by_index.length; i += 1) {
64916506 ErrorTableEntry *err_entry = g->errors_by_index.at(i);
64926507 Buf *name = &err_entry->name;
......@@ -6502,13 +6517,13 @@ static void generate_error_name_table(CodeGen *g) {
65026517 LLVMSetAlignment(str_global, LLVMABIAlignmentOfType(g->target_data_ref, LLVMTypeOf(str_init)));
65036518
65046519 LLVMValueRef fields[] = {
6505 LLVMConstBitCast(str_global, u8_ptr_type->type_ref),
6506 LLVMConstInt(g->builtin_types.entry_usize->type_ref, buf_len(name), false),
6520 LLVMConstBitCast(str_global, get_llvm_type(g, u8_ptr_type)),
6521 LLVMConstInt(g->builtin_types.entry_usize->llvm_type, buf_len(name), false),
65076522 };
6508 values[i] = LLVMConstNamedStruct(str_type->type_ref, fields, 2);
6523 values[i] = LLVMConstNamedStruct(get_llvm_type(g, str_type), fields, 2);
65096524 }
65106525
6511 LLVMValueRef err_name_table_init = LLVMConstArray(str_type->type_ref, values, (unsigned)g->errors_by_index.length);
6526 LLVMValueRef err_name_table_init = LLVMConstArray(get_llvm_type(g, str_type), values, (unsigned)g->errors_by_index.length);
65126527
65136528 g->err_name_table = LLVMAddGlobal(g->module, LLVMTypeOf(err_name_table_init),
65146529 buf_ptr(get_mangled_name(g, buf_create_from_str("__zig_err_name_table"), false)));
......@@ -6547,7 +6562,7 @@ static void gen_global_var(CodeGen *g, ZigVar *var, LLVMValueRef init_val,
65476562 ZigLLVMCreateGlobalVariable(g->dbuilder, get_di_scope(g, var->parent_scope), buf_ptr(&var->name),
65486563 buf_ptr(&var->name), import->data.structure.root_struct->di_file,
65496564 (unsigned)(var->decl_node->line + 1),
6550 type_entry->di_type, is_local_to_unit);
6565 get_llvm_di_type(g, type_entry), is_local_to_unit);
65516566
65526567 // TODO ^^ make an actual global variable
65536568}
......@@ -6588,28 +6603,6 @@ static LLVMLinkage var_linkage_to_llvm(VarLinkage var_linkage) {
65886603static void do_code_gen(CodeGen *g) {
65896604 assert(!g->errors.length);
65906605
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
66136606 generate_error_name_table(g);
66146607
66156608 // Generate module level variables
......@@ -6646,7 +6639,7 @@ static void do_code_gen(CodeGen *g) {
66466639 bits_needed = 8;
66476640 }
66486641 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);
6642 LLVMValueRef init_val = bigint_to_llvm_const(get_llvm_type(g, var_type), &const_val->data.x_bigint);
66506643 gen_global_var(g, var, init_val, var_type);
66516644 continue;
66526645 }
......@@ -6660,9 +6653,10 @@ static void do_code_gen(CodeGen *g) {
66606653 if (var->linkage == VarLinkageExternal) {
66616654 LLVMValueRef existing_llvm_var = LLVMGetNamedGlobal(g->module, buf_ptr(&var->name));
66626655 if (existing_llvm_var) {
6663 global_value = LLVMConstBitCast(existing_llvm_var, LLVMPointerType(var->var_type->type_ref, 0));
6656 global_value = LLVMConstBitCast(existing_llvm_var,
6657 LLVMPointerType(get_llvm_type(g, var->var_type), 0));
66646658 } else {
6665 global_value = LLVMAddGlobal(g->module, var->var_type->type_ref, buf_ptr(&var->name));
6659 global_value = LLVMAddGlobal(g->module, get_llvm_type(g, var->var_type), buf_ptr(&var->name));
66666660 // TODO debug info for the extern variable
66676661
66686662 LLVMSetLinkage(global_value, var_linkage_to_llvm(var->linkage));
......@@ -6834,7 +6828,7 @@ static void do_code_gen(CodeGen *g) {
68346828
68356829 var->di_loc_var = ZigLLVMCreateAutoVariable(g->dbuilder, get_di_scope(g, var->parent_scope),
68366830 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);
6831 get_llvm_di_type(g, var->var_type), !g->strip_debug_symbols, 0);
68386832
68396833 } else if (is_c_abi) {
68406834 fn_walk_var.data.vars.var = var;
......@@ -6859,7 +6853,7 @@ static void do_code_gen(CodeGen *g) {
68596853 var->di_loc_var = ZigLLVMCreateParameterVariable(g->dbuilder, get_di_scope(g, var->parent_scope),
68606854 buf_ptr(&var->name), import->data.structure.root_struct->di_file,
68616855 (unsigned)(var->decl_node->line + 1),
6862 gen_type->di_type, !g->strip_debug_symbols, 0, (unsigned)(var->gen_arg_index + 1));
6856 get_llvm_di_type(g, gen_type), !g->strip_debug_symbols, 0, (unsigned)(var->gen_arg_index + 1));
68636857 }
68646858
68656859 }
......@@ -6870,7 +6864,7 @@ static void do_code_gen(CodeGen *g) {
68706864 ZigType *usize = g->builtin_types.entry_usize;
68716865 size_t index_field_index = g->stack_trace_type->data.structure.fields[0].gen_index;
68726866 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);
6867 gen_store_untyped(g, LLVMConstNull(usize->llvm_type), index_field_ptr, 0, false);
68746868
68756869 size_t addresses_field_index = g->stack_trace_type->data.structure.fields[1].gen_index;
68766870 LLVMValueRef addresses_field_ptr = LLVMBuildStructGEP(g->builder, g->cur_err_ret_trace_val_stack, (unsigned)addresses_field_index, "");
......@@ -6878,7 +6872,7 @@ static void do_code_gen(CodeGen *g) {
68786872 ZigType *slice_type = g->stack_trace_type->data.structure.fields[1].type_entry;
68796873 size_t ptr_field_index = slice_type->data.structure.fields[slice_ptr_index].gen_index;
68806874 LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, addresses_field_ptr, (unsigned)ptr_field_index, "");
6881 LLVMValueRef zero = LLVMConstNull(usize->type_ref);
6875 LLVMValueRef zero = LLVMConstNull(usize->llvm_type);
68826876 LLVMValueRef indices[] = {zero, zero};
68836877 LLVMValueRef err_ret_array_val_elem0_ptr = LLVMBuildInBoundsGEP(g->builder, err_ret_array_val,
68846878 indices, 2, "");
......@@ -6887,7 +6881,7 @@ static void do_code_gen(CodeGen *g) {
68876881
68886882 size_t len_field_index = slice_type->data.structure.fields[slice_len_index].gen_index;
68896883 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));
6884 gen_store(g, LLVMConstInt(usize->llvm_type, stack_trace_ptr_count, false), len_field_ptr, get_pointer_to_type(g, usize, false));
68916885 }
68926886
68936887 // create debug variable declarations for parameters
......@@ -6997,50 +6991,60 @@ static const GlobalLinkageValue global_linkage_values[] = {
69976991 {GlobalLinkageIdLinkOnce, "LinkOnce"},
69986992};
69996993
6994static void add_fp_entry(CodeGen *g, const char *name, uint32_t bit_count, LLVMTypeRef type_ref,
6995 ZigType **field)
6996{
6997 ZigType *entry = new_type_table_entry(ZigTypeIdFloat);
6998 entry->llvm_type = type_ref;
6999 entry->size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, entry->llvm_type);
7000 entry->abi_size = LLVMABISizeOfType(g->target_data_ref, entry->llvm_type);
7001 entry->abi_align = LLVMABIAlignmentOfType(g->target_data_ref, entry->llvm_type);
7002 buf_init_from_str(&entry->name, name);
7003 entry->data.floating.bit_count = bit_count;
7004
7005 entry->llvm_di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
7006 entry->size_in_bits, ZigLLVMEncoding_DW_ATE_float());
7007 *field = entry;
7008 g->primitive_type_table.put(&entry->name, entry);
7009}
7010
70007011static void define_builtin_types(CodeGen *g) {
70017012 {
70027013 // if this type is anywhere in the AST, we should never hit codegen.
70037014 ZigType *entry = new_type_table_entry(ZigTypeIdInvalid);
70047015 buf_init_from_str(&entry->name, "(invalid)");
7005 entry->zero_bits = true;
70067016 g->builtin_types.entry_invalid = entry;
70077017 }
70087018 {
70097019 ZigType *entry = new_type_table_entry(ZigTypeIdComptimeFloat);
70107020 buf_init_from_str(&entry->name, "comptime_float");
7011 entry->zero_bits = true;
70127021 g->builtin_types.entry_num_lit_float = entry;
70137022 g->primitive_type_table.put(&entry->name, entry);
70147023 }
70157024 {
70167025 ZigType *entry = new_type_table_entry(ZigTypeIdComptimeInt);
70177026 buf_init_from_str(&entry->name, "comptime_int");
7018 entry->zero_bits = true;
70197027 g->builtin_types.entry_num_lit_int = entry;
70207028 g->primitive_type_table.put(&entry->name, entry);
70217029 }
70227030 {
70237031 ZigType *entry = new_type_table_entry(ZigTypeIdEnumLiteral);
70247032 buf_init_from_str(&entry->name, "(enum literal)");
7025 entry->zero_bits = true;
70267033 g->builtin_types.entry_enum_literal = entry;
70277034 }
70287035 {
70297036 ZigType *entry = new_type_table_entry(ZigTypeIdUndefined);
70307037 buf_init_from_str(&entry->name, "(undefined)");
7031 entry->zero_bits = true;
70327038 g->builtin_types.entry_undef = entry;
70337039 }
70347040 {
70357041 ZigType *entry = new_type_table_entry(ZigTypeIdNull);
70367042 buf_init_from_str(&entry->name, "(null)");
7037 entry->zero_bits = true;
70387043 g->builtin_types.entry_null = entry;
70397044 }
70407045 {
70417046 ZigType *entry = new_type_table_entry(ZigTypeIdArgTuple);
70427047 buf_init_from_str(&entry->name, "(args)");
7043 entry->zero_bits = true;
70447048 g->builtin_types.entry_arg_tuple = entry;
70457049 }
70467050
......@@ -7050,14 +7054,15 @@ static void define_builtin_types(CodeGen *g) {
70507054 bool is_signed = info->is_signed;
70517055
70527056 ZigType *entry = new_type_table_entry(ZigTypeIdInt);
7053 entry->type_ref = LLVMIntType(size_in_bits);
7057 entry->llvm_type = LLVMIntType(size_in_bits);
7058 entry->size_in_bits = size_in_bits;
7059 entry->abi_size = LLVMABISizeOfType(g->target_data_ref, entry->llvm_type);
7060 entry->abi_align = LLVMABIAlignmentOfType(g->target_data_ref, entry->llvm_type);
70547061
70557062 buf_init_from_str(&entry->name, info->name);
70567063
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());
7064 entry->llvm_di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
7065 size_in_bits, is_signed ? ZigLLVMEncoding_DW_ATE_signed() : ZigLLVMEncoding_DW_ATE_unsigned());
70617066 entry->data.integral.is_signed = is_signed;
70627067 entry->data.integral.bit_count = size_in_bits;
70637068 g->primitive_type_table.put(&entry->name, entry);
......@@ -7067,12 +7072,13 @@ static void define_builtin_types(CodeGen *g) {
70677072
70687073 {
70697074 ZigType *entry = new_type_table_entry(ZigTypeIdBool);
7070 entry->type_ref = LLVMInt1Type();
7075 entry->llvm_type = LLVMInt1Type();
7076 entry->size_in_bits = 1;
7077 entry->abi_size = LLVMABISizeOfType(g->target_data_ref, entry->llvm_type);
7078 entry->abi_align = LLVMABIAlignmentOfType(g->target_data_ref, entry->llvm_type);
70717079 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());
7080 entry->llvm_di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
7081 entry->size_in_bits, ZigLLVMEncoding_DW_ATE_boolean());
70767082 g->builtin_types.entry_bool = entry;
70777083 g->primitive_type_table.put(&entry->name, entry);
70787084 }
......@@ -7081,7 +7087,10 @@ static void define_builtin_types(CodeGen *g) {
70817087 bool is_signed = is_signed_list[sign_i];
70827088
70837089 ZigType *entry = new_type_table_entry(ZigTypeIdInt);
7084 entry->type_ref = LLVMIntType(g->pointer_size_bytes * 8);
7090 entry->llvm_type = LLVMIntType(g->pointer_size_bytes * 8);
7091 entry->size_in_bits = g->pointer_size_bytes * 8;
7092 entry->abi_size = LLVMABISizeOfType(g->target_data_ref, entry->llvm_type);
7093 entry->abi_align = LLVMABIAlignmentOfType(g->target_data_ref, entry->llvm_type);
70857094
70867095 const char u_or_i = is_signed ? 'i' : 'u';
70877096 buf_resize(&entry->name, 0);
......@@ -7090,9 +7099,8 @@ static void define_builtin_types(CodeGen *g) {
70907099 entry->data.integral.is_signed = is_signed;
70917100 entry->data.integral.bit_count = g->pointer_size_bytes * 8;
70927101
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,
7102 entry->llvm_di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
7103 entry->size_in_bits,
70967104 is_signed ? ZigLLVMEncoding_DW_ATE_signed() : ZigLLVMEncoding_DW_ATE_unsigned());
70977105 g->primitive_type_table.put(&entry->name, entry);
70987106
......@@ -7103,23 +7111,6 @@ static void define_builtin_types(CodeGen *g) {
71037111 }
71047112 }
71057113
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 };
71237114 add_fp_entry(g, "f16", 16, LLVMHalfType(), &g->builtin_types.entry_f16);
71247115 add_fp_entry(g, "f32", 32, LLVMFloatType(), &g->builtin_types.entry_f32);
71257116 add_fp_entry(g, "f64", 64, LLVMDoubleType(), &g->builtin_types.entry_f64);
......@@ -7128,10 +7119,9 @@ static void define_builtin_types(CodeGen *g) {
71287119
71297120 {
71307121 ZigType *entry = new_type_table_entry(ZigTypeIdVoid);
7131 entry->type_ref = LLVMVoidType();
7132 entry->zero_bits = true;
7122 entry->llvm_type = LLVMVoidType();
71337123 buf_init_from_str(&entry->name, "void");
7134 entry->di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
7124 entry->llvm_di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
71357125 0,
71367126 ZigLLVMEncoding_DW_ATE_unsigned());
71377127 g->builtin_types.entry_void = entry;
......@@ -7139,17 +7129,15 @@ static void define_builtin_types(CodeGen *g) {
71397129 }
71407130 {
71417131 ZigType *entry = new_type_table_entry(ZigTypeIdUnreachable);
7142 entry->type_ref = LLVMVoidType();
7143 entry->zero_bits = true;
7132 entry->llvm_type = LLVMVoidType();
71447133 buf_init_from_str(&entry->name, "noreturn");
7145 entry->di_type = g->builtin_types.entry_void->di_type;
7134 entry->llvm_di_type = g->builtin_types.entry_void->llvm_di_type;
71467135 g->builtin_types.entry_unreachable = entry;
71477136 g->primitive_type_table.put(&entry->name, entry);
71487137 }
71497138 {
71507139 ZigType *entry = new_type_table_entry(ZigTypeIdMetaType);
71517140 buf_init_from_str(&entry->name, "type");
7152 entry->zero_bits = true;
71537141 g->builtin_types.entry_type = entry;
71547142 g->primitive_type_table.put(&entry->name, entry);
71557143 }
......@@ -7174,19 +7162,15 @@ static void define_builtin_types(CodeGen *g) {
71747162 buf_init_from_str(&entry->name, "anyerror");
71757163 entry->data.error_set.err_count = UINT32_MAX;
71767164
7177 // TODO allow overriding this type and keep track of max value and emit an
7178 // error if there are too many errors declared
7165 // TODO https://github.com/ziglang/zig/issues/786
71797166 g->err_tag_type = g->builtin_types.entry_u16;
71807167
7181 g->builtin_types.entry_global_error_set = entry;
7182 entry->type_ref = g->err_tag_type->type_ref;
7168 entry->size_in_bits = g->err_tag_type->size_in_bits;
7169 entry->abi_align = g->err_tag_type->abi_align;
7170 entry->abi_size = g->err_tag_type->abi_size;
71837171
7184 entry->di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
7185 ZigLLVMTag_DW_enumeration_type(), "anyerror",
7186 ZigLLVMCompileUnitToScope(g->compile_unit), nullptr, 0);
7172 g->builtin_types.entry_global_error_set = entry;
71877173
7188 // reserve index 0 to indicate no error
7189 g->err_enumerators.append(ZigLLVMCreateDebugEnumerator(g->dbuilder, "(none)", 0));
71907174 g->errors_by_index.append(nullptr);
71917175
71927176 g->primitive_type_table.put(&entry->name, entry);
......@@ -7194,6 +7178,9 @@ static void define_builtin_types(CodeGen *g) {
71947178 {
71957179 ZigType *entry = get_promise_type(g, nullptr);
71967180 g->primitive_type_table.put(&entry->name, entry);
7181 entry->size_in_bits = g->builtin_types.entry_usize->size_in_bits;
7182 entry->abi_align = g->builtin_types.entry_usize->abi_align;
7183 entry->abi_size = g->builtin_types.entry_usize->abi_size;
71977184 }
71987185}
71997186
src/ir.cpp+29-43
......@@ -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,16 @@ 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 if (intersection_list.length != 0) {
8949 err_set_type->size_in_bits = ira->codegen->builtin_types.entry_global_error_set->size_in_bits;
8950 err_set_type->abi_align = ira->codegen->builtin_types.entry_global_error_set->abi_align;
8951 err_set_type->abi_size = ira->codegen->builtin_types.entry_global_error_set->abi_size;
8952 }
89558953
89568954 buf_appendf(&err_set_type->name, "}");
89578955
8958 ira->codegen->error_di_types.append(&err_set_type->di_type);
8959
89608956 return err_set_type;
89618957}
89628958
......@@ -14855,7 +14851,7 @@ static IrInstruction *ir_analyze_maybe(IrAnalyze *ira, IrInstructionUnOp *un_op_
1485514851 ZigType *type_entry = ir_resolve_type(ira, value);
1485614852 if (type_is_invalid(type_entry))
1485714853 return ira->codegen->invalid_instruction;
14858 if ((err = ensure_complete_type(ira->codegen, type_entry)))
14854 if ((err = type_resolve(ira->codegen, type_entry, ResolveStatusSizeKnown)))
1485914855 return ira->codegen->invalid_instruction;
1486014856
1486114857 switch (type_entry->id) {
......@@ -16051,8 +16047,6 @@ static IrInstruction *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstruc
1605116047 assert((uint32_t)error_value_count < (((uint32_t)1) << (uint32_t)ira->codegen->err_tag_type->data.integral.bit_count));
1605216048 err_entry->value = error_value_count;
1605316049 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));
1605616050 ira->codegen->error_table.put(field_name, err_entry);
1605716051 }
1605816052 if (err_entry->set_with_only_this_in_it == nullptr) {
......@@ -17873,7 +17867,7 @@ static TypeStructField *validate_byte_offset(IrAnalyze *ira,
1787317867 return nullptr;
1787417868 }
1787517869
17876 *byte_offset = LLVMOffsetOfElement(ira->codegen->target_data_ref, container_type->type_ref, field->gen_index);
17870 *byte_offset = field->offset;
1787717871 return field;
1787817872}
1787917873
......@@ -18726,7 +18720,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
1872618720 if (!type_has_bits(struct_field->type_entry)) {
1872718721 inner_fields[1].data.x_optional = nullptr;
1872818722 } else {
18729 size_t byte_offset = LLVMOffsetOfElement(ira->codegen->target_data_ref, type_entry->type_ref, struct_field->gen_index);
18723 size_t byte_offset = struct_field->offset;
1873018724 inner_fields[1].data.x_optional = create_const_vals(1);
1873118725 inner_fields[1].data.x_optional->special = ConstValSpecialStatic;
1873218726 inner_fields[1].data.x_optional->type = ira->codegen->builtin_types.entry_num_lit_int;
......@@ -21425,12 +21419,11 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue
2142521419 case ContainerLayoutExtern: {
2142621420 size_t src_field_count = val->type->data.structure.src_field_count;
2142721421 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)
21422 TypeStructField *struct_field = &val->type->data.structure.fields[field_i];
21423 if (struct_field->gen_index == SIZE_MAX)
2143021424 continue;
2143121425 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);
21426 size_t offset = struct_field->offset;
2143421427 buf_write_value_bytes(codegen, buf + offset, field_val);
2143521428 }
2143621429 return;
......@@ -21446,10 +21439,7 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue
2144621439 size_t child_buf_len = 16;
2144721440 uint8_t *child_buf = child_buf_prealloc;
2144821441 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;
21442 size_t big_int_byte_count = val->type->data.structure.host_int_bytes[gen_i];
2145321443 if (big_int_byte_count > child_buf_len) {
2145421444 child_buf = allocate_nonzero<uint8_t>(big_int_byte_count);
2145521445 child_buf_len = big_int_byte_count;
......@@ -21485,7 +21475,7 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue
2148521475 }
2148621476 src_i += 1;
2148721477 }
21488 bigint_write_twos_complement(&big_int, buf + offset, big_int_bit_count, is_big_endian);
21478 bigint_write_twos_complement(&big_int, buf + offset, big_int_byte_count * 8, is_big_endian);
2148921479 offset += big_int_byte_count;
2149021480 gen_i += 1;
2149121481 }
......@@ -21606,12 +21596,11 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou
2160621596 for (size_t field_i = 0; field_i < src_field_count; field_i += 1) {
2160721597 ConstExprValue *field_val = &val->data.x_struct.fields[field_i];
2160821598 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)
21599 TypeStructField *struct_field = &val->type->data.structure.fields[field_i];
21600 field_val->type = struct_field->type_entry;
21601 if (struct_field->gen_index == SIZE_MAX)
2161221602 continue;
21613 size_t offset = LLVMOffsetOfElement(codegen->target_data_ref, val->type->type_ref,
21614 type_field->gen_index);
21603 size_t offset = struct_field->offset;
2161521604 uint8_t *new_buf = buf + offset;
2161621605 if ((err = buf_read_value_bytes(ira, codegen, source_node, new_buf, field_val)))
2161721606 return err;
......@@ -21630,16 +21619,13 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou
2163021619 size_t child_buf_len = 16;
2163121620 uint8_t *child_buf = child_buf_prealloc;
2163221621 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;
21622 size_t big_int_byte_count = val->type->data.structure.host_int_bytes[gen_i];
2163721623 if (big_int_byte_count > child_buf_len) {
2163821624 child_buf = allocate_nonzero<uint8_t>(big_int_byte_count);
2163921625 child_buf_len = big_int_byte_count;
2164021626 }
2164121627 BigInt big_int;
21642 bigint_read_twos_complement(&big_int, buf + offset, big_int_bit_count, is_big_endian, false);
21628 bigint_read_twos_complement(&big_int, buf + offset, big_int_byte_count * 8, is_big_endian, false);
2164321629 while (src_i < src_field_count) {
2164421630 TypeStructField *field = &val->type->data.structure.fields[src_i];
2164521631 assert(field->gen_index != SIZE_MAX);
......@@ -21662,7 +21648,7 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou
2166221648 big_int = tmp;
2166321649 }
2166421650
21665 bigint_write_twos_complement(&child_val, child_buf, big_int_bit_count, is_big_endian);
21651 bigint_write_twos_complement(&child_val, child_buf, big_int_byte_count * 8, is_big_endian);
2166621652 if ((err = buf_read_value_bytes(ira, codegen, source_node, child_buf, field_val))) {
2166721653 return err;
2166821654 }