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
signature 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 {...@@ -1067,8 +1067,10 @@ struct TypeStructField {
1067 ZigType *type_entry;1067 ZigType *type_entry;
1068 size_t src_index;1068 size_t src_index;
1069 size_t gen_index;1069 size_t gen_index;
1070 uint32_t bit_offset_in_host; // offset from the memory at gen_index1070 size_t offset; // byte offset from beginning of struct
1071 AstNode *decl_node;1071 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
1072};1074};
10731075
1074enum ResolveStatus {1076enum ResolveStatus {
...@@ -1101,14 +1103,13 @@ struct ZigTypeStruct {...@@ -1101,14 +1103,13 @@ struct ZigTypeStruct {
1101 AstNode *decl_node;1103 AstNode *decl_node;
1102 TypeStructField *fields;1104 TypeStructField *fields;
1103 ScopeDecls *decls_scope;1105 ScopeDecls *decls_scope;
1104 uint64_t size_bytes;
1105 HashMap<Buf *, TypeStructField *, buf_hash, buf_eql_buf> fields_by_name;1106 HashMap<Buf *, TypeStructField *, buf_hash, buf_eql_buf> fields_by_name;
1106 RootStruct *root_struct;1107 RootStruct *root_struct;
1108 uint32_t *host_int_bytes; // available for packed structs, indexed by gen_index
11071109
1108 uint32_t src_field_count;1110 uint32_t src_field_count;
1109 uint32_t gen_field_count;1111 uint32_t gen_field_count;
11101112
1111 uint32_t abi_alignment; // known after ResolveStatusAlignmentKnown
1112 ContainerLayout layout;1113 ContainerLayout layout;
1113 ResolveStatus resolve_status;1114 ResolveStatus resolve_status;
11141115
...@@ -1164,39 +1165,28 @@ bool type_ptr_eql(const ZigType *a, const ZigType *b);...@@ -1164,39 +1165,28 @@ bool type_ptr_eql(const ZigType *a, const ZigType *b);
11641165
1165struct ZigTypeUnion {1166struct ZigTypeUnion {
1166 AstNode *decl_node;1167 AstNode *decl_node;
1167 ContainerLayout layout;
1168 uint32_t src_field_count;
1169 uint32_t gen_field_count;
1170 TypeUnionField *fields;1168 TypeUnionField *fields;
1171 bool is_invalid; // true if any fields are invalid1169 ScopeDecls *decls_scope;
1170 HashMap<Buf *, TypeUnionField *, buf_hash, buf_eql_buf> fields_by_name;
1172 ZigType *tag_type; // always an enum or null1171 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 loops1181 ContainerLayout layout;
1178 bool embedded_in_current;1182 ResolveStatus resolve_status;
1179 bool reported_infinite_err;
1180 // whether we've finished resolving it
1181 bool complete;
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;
1183 // whether any of the fields require comptime1187 // whether any of the fields require comptime
1184 // the value is not valid until zero_bits_known == true1188 // the value is not valid until zero_bits_known == true
1185 bool requires_comptime;1189 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;
1200};1190};
12011191
1202struct FnGenParamInfo {1192struct FnGenParamInfo {
...@@ -1277,8 +1267,11 @@ struct ZigType {...@@ -1277,8 +1267,11 @@ struct ZigType {
1277 ZigTypeId id;1267 ZigTypeId id;
1278 Buf name;1268 Buf name;
12791269
1280 LLVMTypeRef type_ref;1270 // These are not supposed to be accessed directly. They're
1281 ZigLLVMDIType *di_type;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
1283 union {1276 union {
1284 ZigTypePointer pointer;1277 ZigTypePointer pointer;
...@@ -1308,8 +1301,14 @@ struct ZigType {...@@ -1308,8 +1301,14 @@ struct ZigType {
1308 ConstExprValue *cached_const_name_val;1301 ConstExprValue *cached_const_name_val;
13091302
1310 OnePossibleValue one_possible_value;1303 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
1313 bool gen_h_loop_flag;1312 bool gen_h_loop_flag;
1314};1313};
13151314
...@@ -1700,11 +1699,9 @@ struct CodeGen {...@@ -1700,11 +1699,9 @@ struct CodeGen {
1700 ZigList<AstNode *> use_queue;1699 ZigList<AstNode *> use_queue;
1701 size_t use_queue_index;1700 size_t use_queue_index;
1702 ZigList<TimeEvent> timing_events;1701 ZigList<TimeEvent> timing_events;
1703 ZigList<ZigLLVMDIType **> error_di_types;
1704 ZigList<AstNode *> tld_ref_source_node_stack;1702 ZigList<AstNode *> tld_ref_source_node_stack;
1705 ZigList<ZigFn *> inline_fns;1703 ZigList<ZigFn *> inline_fns;
1706 ZigList<ZigFn *> test_fns;1704 ZigList<ZigFn *> test_fns;
1707 ZigList<ZigLLVMDIEnumerator *> err_enumerators;
1708 ZigList<ErrorTableEntry *> errors_by_index;1705 ZigList<ErrorTableEntry *> errors_by_index;
1709 ZigList<CacheHash *> caches_to_release;1706 ZigList<CacheHash *> caches_to_release;
1710 size_t largest_err_name_len;1707 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...@@ -26,6 +26,7 @@ static Error ATTRIBUTE_MUST_USE resolve_struct_zero_bits(CodeGen *g, ZigType *st
26static Error ATTRIBUTE_MUST_USE resolve_struct_alignment(CodeGen *g, ZigType *struct_type);26static Error ATTRIBUTE_MUST_USE resolve_struct_alignment(CodeGen *g, ZigType *struct_type);
27static Error ATTRIBUTE_MUST_USE resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type);27static Error ATTRIBUTE_MUST_USE resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type);
28static Error ATTRIBUTE_MUST_USE resolve_union_zero_bits(CodeGen *g, ZigType *union_type);28static 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);
29static void analyze_fn_body(CodeGen *g, ZigFn *fn_table_entry);30static void analyze_fn_body(CodeGen *g, ZigFn *fn_table_entry);
3031
31static bool is_top_level_struct(ZigType *import) {32static bool is_top_level_struct(ZigType *import) {
...@@ -264,6 +265,8 @@ bool type_is_resolved(ZigType *type_entry, ResolveStatus status) {...@@ -264,6 +265,8 @@ bool type_is_resolved(ZigType *type_entry, ResolveStatus status) {
264 zig_unreachable();265 zig_unreachable();
265 case ZigTypeIdStruct:266 case ZigTypeIdStruct:
266 return type_entry->data.structure.resolve_status >= status;267 return type_entry->data.structure.resolve_status >= status;
268 case ZigTypeIdUnion:
269 return type_entry->data.unionation.resolve_status >= status;
267 case ZigTypeIdEnum:270 case ZigTypeIdEnum:
268 switch (status) {271 switch (status) {
269 case ResolveStatusUnstarted:272 case ResolveStatusUnstarted:
...@@ -278,20 +281,6 @@ bool type_is_resolved(ZigType *type_entry, ResolveStatus status) {...@@ -278,20 +281,6 @@ bool type_is_resolved(ZigType *type_entry, ResolveStatus status) {
278 return type_entry->data.enumeration.complete;281 return type_entry->data.enumeration.complete;
279 }282 }
280 zig_unreachable();283 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();
295 case ZigTypeIdOpaque:284 case ZigTypeIdOpaque:
296 return status < ResolveStatusSizeKnown;285 return status < ResolveStatusSizeKnown;
297 case ZigTypeIdMetaType:286 case ZigTypeIdMetaType:
...@@ -325,49 +314,18 @@ bool type_is_complete(ZigType *type_entry) {...@@ -325,49 +314,18 @@ bool type_is_complete(ZigType *type_entry) {
325}314}
326315
327uint64_t type_size(CodeGen *g, ZigType *type_entry) {316uint64_t type_size(CodeGen *g, ZigType *type_entry) {
328 assert(type_is_complete(type_entry));317 assert(type_is_resolved(type_entry, ResolveStatusSizeKnown));
329318 return type_entry->abi_size;
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);
347}319}
348320
349uint64_t type_size_bits(CodeGen *g, ZigType *type_entry) {321uint64_t type_size_bits(CodeGen *g, ZigType *type_entry) {
350 assert(type_is_complete(type_entry));322 assert(type_is_resolved(type_entry, ResolveStatusSizeKnown));
351323 return type_entry->size_in_bits;
352 if (!type_has_bits(type_entry))324}
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 }
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;
371}329}
372330
373static bool is_slice(ZigType *type) {331static bool is_slice(ZigType *type) {
...@@ -385,16 +343,14 @@ ZigType *get_promise_type(CodeGen *g, ZigType *result_type) {...@@ -385,16 +343,14 @@ ZigType *get_promise_type(CodeGen *g, ZigType *result_type) {
385 return g->builtin_types.entry_promise;343 return g->builtin_types.entry_promise;
386 }344 }
387345
388 ZigType *u8_ptr_type = get_pointer_to_type(g, g->builtin_types.entry_u8, false);
389 ZigType *entry = new_type_table_entry(ZigTypeIdPromise);346 ZigType *entry = new_type_table_entry(ZigTypeIdPromise);
390 entry->type_ref = u8_ptr_type->type_ref;347 entry->abi_size = g->pointer_size_bytes;
391 entry->zero_bits = false;348 entry->size_in_bits = g->pointer_size_bytes * 8;
392 entry->data.promise.result_type = result_type;349 entry->data.promise.result_type = result_type;
393 buf_init_from_str(&entry->name, "promise");350 buf_init_from_str(&entry->name, "promise");
394 if (result_type != nullptr) {351 if (result_type != nullptr) {
395 buf_appendf(&entry->name, "->%s", buf_ptr(&result_type->name));352 buf_appendf(&entry->name, "->%s", buf_ptr(&result_type->name));
396 }353 }
397 entry->di_type = u8_ptr_type->di_type;
398354
399 if (result_type != nullptr) {355 if (result_type != nullptr) {
400 result_type->promise_parent = entry;356 result_type->promise_parent = entry;
...@@ -496,36 +452,15 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons...@@ -496,36 +452,15 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons
496452
497 assert(child_type->id != ZigTypeIdInvalid);453 assert(child_type->id != ZigTypeIdInvalid);
498454
499 entry->zero_bits = !type_has_bits(child_type);455 if (type_has_bits(child_type)) {
500456 entry->abi_size = g->builtin_types.entry_usize->abi_size;
501 if (!entry->zero_bits) {457 entry->size_in_bits = g->builtin_types.entry_usize->size_in_bits;
502 if (is_const || is_volatile || byte_alignment != 0 || ptr_len != PtrLenSingle ||458 entry->abi_align = g->builtin_types.entry_usize->abi_align;
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 }
526 } else {459 } else {
527 assert(byte_alignment == 0);460 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;
529 }464 }
530465
531 entry->data.pointer.ptr_len = ptr_len;466 entry->data.pointer.ptr_len = ptr_len;
...@@ -586,89 +521,61 @@ ZigType *get_promise_frame_type(CodeGen *g, ZigType *return_type) {...@@ -586,89 +521,61 @@ ZigType *get_promise_frame_type(CodeGen *g, ZigType *return_type) {
586}521}
587522
588ZigType *get_optional_type(CodeGen *g, ZigType *child_type) {523ZigType *get_optional_type(CodeGen *g, ZigType *child_type) {
589 if (child_type->optional_parent) {524 if (child_type->optional_parent != nullptr) {
590 ZigType *entry = child_type->optional_parent;525 return child_type->optional_parent;
591 return entry;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;
592 } else {546 } else {
593 assertNoError(ensure_complete_type(g, child_type));547 // This value only matters if the type is legal in a packed struct, which is not
594548 // true for optional types which did not fit the above 2 categories (zero bit child type,
595 ZigType *entry = new_type_table_entry(ZigTypeIdOptional);549 // or nonnull ptr child type, or error set child type).
596 assert(child_type->type_ref || child_type->zero_bits);550 entry->size_in_bits = child_type->size_in_bits + 1;
597551
598 buf_resize(&entry->name, 0);552 // We're going to make a struct with the child type as the first field,
599 buf_appendf(&entry->name, "?%s", buf_ptr(&child_type->name));553 // and a bool as the second. Since the child type's abi alignment is guaranteed
600554 // to be >= the bool's abi size (1 byte), the added size is exactly equal to the
601 if (child_type->zero_bits) {555 // child type's ABI alignment.
602 entry->type_ref = LLVMInt1Type();556 assert(child_type->abi_align >= g->builtin_types.entry_bool->abi_size);
603 entry->di_type = g->builtin_types.entry_bool->di_type;557 entry->abi_align = child_type->abi_align;
604 } else if (type_is_nonnull_ptr(child_type) || child_type->id == ZigTypeIdErrorSet) {558 entry->abi_size = child_type->abi_size + child_type->abi_align;
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;
671 }559 }
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;
672}579}
673580
674ZigType *get_error_union_type(CodeGen *g, ZigType *err_set_type, ZigType *payload_type) {581ZigType *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...@@ -686,8 +593,7 @@ ZigType *get_error_union_type(CodeGen *g, ZigType *err_set_type, ZigType *payloa
686 }593 }
687594
688 ZigType *entry = new_type_table_entry(ZigTypeIdErrorUnion);595 ZigType *entry = new_type_table_entry(ZigTypeIdErrorUnion);
689 assert(payload_type->di_type);596 assert(type_is_resolved(payload_type, ResolveStatusSizeKnown));
690 assert(type_is_complete(payload_type));
691597
692 buf_resize(&entry->name, 0);598 buf_resize(&entry->name, 0);
693 buf_appendf(&entry->name, "%s!%s", buf_ptr(&err_set_type->name), buf_ptr(&payload_type->name));599 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...@@ -697,68 +603,29 @@ ZigType *get_error_union_type(CodeGen *g, ZigType *err_set_type, ZigType *payloa
697603
698 if (!type_has_bits(payload_type)) {604 if (!type_has_bits(payload_type)) {
699 if (type_has_bits(err_set_type)) {605 if (type_has_bits(err_set_type)) {
700 entry->type_ref = err_set_type->type_ref;606 entry->size_in_bits = err_set_type->size_in_bits;
701 entry->di_type = err_set_type->di_type;607 entry->abi_size = err_set_type->abi_size;
702 g->error_di_types.append(&entry->di_type);608 entry->abi_align = err_set_type->abi_align;
703 } else {609 } else {
704 entry->zero_bits = true;610 entry->size_in_bits = 0;
705 entry->di_type = g->builtin_types.entry_void->di_type;611 entry->abi_size = 0;
612 entry->abi_align = 0;
706 }613 }
707 } else if (!type_has_bits(err_set_type)) {614 } else if (!type_has_bits(err_set_type)) {
708 entry->type_ref = payload_type->type_ref;615 entry->size_in_bits = payload_type->size_in_bits;
709 entry->di_type = payload_type->di_type;616 entry->abi_size = payload_type->abi_size;
617 entry->abi_align = payload_type->abi_align;
710 } else {618 } else {
711 LLVMTypeRef elem_types[] = {619 entry->abi_align = max(err_set_type->abi_align, payload_type->abi_align);
712 err_set_type->type_ref,620 size_t field_sizes[2];
713 payload_type->type_ref,621 size_t field_aligns[2];
714 };622 field_sizes[err_union_err_index] = err_set_type->abi_size;
715 entry->type_ref = LLVMStructType(elem_types, 2, false);623 field_aligns[err_union_err_index] = err_set_type->abi_align;
716624 field_sizes[err_union_payload_index] = payload_type->abi_size;
717 ZigLLVMDIScope *compile_unit_scope = ZigLLVMCompileUnitToScope(g->compile_unit);625 field_aligns[err_union_payload_index] = payload_type->abi_align;
718 ZigLLVMDIFile *di_file = nullptr;626 size_t field2_offset = next_field_offset(0, entry->abi_align, field_sizes[0], field_aligns[0]);
719 unsigned line = 0;627 entry->abi_size = next_field_offset(field2_offset, entry->abi_align, field_sizes[1], field_aligns[1]);
720 entry->di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,628 entry->size_in_bits = entry->abi_size * 8;
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;
762 }629 }
763630
764 g->type_table.put(type_id, entry);631 g->type_table.put(type_id, entry);
...@@ -772,31 +639,20 @@ ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size) {...@@ -772,31 +639,20 @@ ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size) {
772 type_id.data.array.size = array_size;639 type_id.data.array.size = array_size;
773 auto existing_entry = g->type_table.maybe_get(type_id);640 auto existing_entry = g->type_table.maybe_get(type_id);
774 if (existing_entry) {641 if (existing_entry) {
775 ZigType *entry = existing_entry->value;642 return existing_entry->value;
776 return entry;
777 }643 }
778644
779 assertNoError(ensure_complete_type(g, child_type));645 assert(type_is_resolved(child_type, ResolveStatusSizeKnown));
780646
781 ZigType *entry = new_type_table_entry(ZigTypeIdArray);647 ZigType *entry = new_type_table_entry(ZigTypeIdArray);
782 entry->zero_bits = (array_size == 0) || child_type->zero_bits;
783648
784 buf_resize(&entry->name, 0);649 buf_resize(&entry->name, 0);
785 buf_appendf(&entry->name, "[%" ZIG_PRI_u64 "]%s", array_size, buf_ptr(&child_type->name));650 buf_appendf(&entry->name, "[%" ZIG_PRI_u64 "]%s", array_size, buf_ptr(&child_type->name));
786651
787 if (entry->zero_bits) {652 entry->size_in_bits = child_type->size_in_bits * array_size;
788 entry->di_type = ZigLLVMCreateDebugArrayType(g->dbuilder, 0,653 entry->abi_align = child_type->abi_align;
789 0, child_type->di_type, 0);654 entry->abi_size = child_type->abi_size * array_size;
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);
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 }
800 entry->data.array.child_type = child_type;656 entry->data.array.child_type = child_type;
801 entry->data.array.len = array_size;657 entry->data.array.len = array_size;
802658
...@@ -804,11 +660,27 @@ ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size) {...@@ -804,11 +660,27 @@ ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size) {
804 return entry;660 return entry;
805}661}
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
808 unsigned element_count = 2;679 unsigned element_count = 2;
809 Buf *ptr_field_name = buf_create_from_str("ptr");680 Buf *ptr_field_name = buf_create_from_str("ptr");
810 Buf *len_field_name = buf_create_from_str("len");681 Buf *len_field_name = buf_create_from_str("len");
811682
683 entry->data.structure.resolve_status = ResolveStatusSizeKnown;
812 entry->data.structure.layout = ContainerLayoutAuto;684 entry->data.structure.layout = ContainerLayoutAuto;
813 entry->data.structure.is_slice = true;685 entry->data.structure.is_slice = true;
814 entry->data.structure.src_field_count = element_count;686 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...@@ -816,7 +688,7 @@ static void slice_type_common_init(CodeGen *g, ZigType *pointer_type, ZigType *e
816 entry->data.structure.fields = allocate<TypeStructField>(element_count);688 entry->data.structure.fields = allocate<TypeStructField>(element_count);
817 entry->data.structure.fields_by_name.init(element_count);689 entry->data.structure.fields_by_name.init(element_count);
818 entry->data.structure.fields[slice_ptr_index].name = ptr_field_name;690 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;
820 entry->data.structure.fields[slice_ptr_index].src_index = slice_ptr_index;692 entry->data.structure.fields[slice_ptr_index].src_index = slice_ptr_index;
821 entry->data.structure.fields[slice_ptr_index].gen_index = 0;693 entry->data.structure.fields[slice_ptr_index].gen_index = 0;
822 entry->data.structure.fields[slice_len_index].name = len_field_name;694 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...@@ -827,28 +699,11 @@ static void slice_type_common_init(CodeGen *g, ZigType *pointer_type, ZigType *e
827 entry->data.structure.fields_by_name.put(ptr_field_name, &entry->data.structure.fields[slice_ptr_index]);699 entry->data.structure.fields_by_name.put(ptr_field_name, &entry->data.structure.fields[slice_ptr_index]);
828 entry->data.structure.fields_by_name.put(len_field_name, &entry->data.structure.fields[slice_len_index]);700 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)) {
831 entry->data.structure.gen_field_count = 1;703 entry->data.structure.gen_field_count = 1;
832 entry->data.structure.fields[slice_ptr_index].gen_index = SIZE_MAX;704 entry->data.structure.fields[slice_ptr_index].gen_index = SIZE_MAX;
833 entry->data.structure.fields[slice_len_index].gen_index = 0;705 entry->data.structure.fields[slice_len_index].gen_index = 0;
834 }706 }
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
853 ZigType *child_type = ptr_type->data.pointer.child_type;708 ZigType *child_type = ptr_type->data.pointer.child_type;
854 if (ptr_type->data.pointer.is_const || ptr_type->data.pointer.is_volatile ||709 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) {...@@ -858,134 +713,24 @@ ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type) {
858 PtrLenUnknown, 0, 0, 0, false);713 PtrLenUnknown, 0, 0, 0, false);
859 ZigType *peer_slice_type = get_slice_type(g, peer_ptr_type);714 ZigType *peer_slice_type = get_slice_type(g, peer_ptr_type);
860715
861 slice_type_common_init(g, ptr_type, entry);716 entry->size_in_bits = peer_slice_type->size_in_bits;
862717 entry->abi_size = peer_slice_type->abi_size;
863 entry->type_ref = peer_slice_type->type_ref;718 entry->abi_align = peer_slice_type->abi_align;
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;
867719
868 *parent_pointer = entry;720 *parent_pointer = entry;
869 return entry;721 return entry;
870 }722 }
871723
872 // If the child type is []const T then we need to make sure the type ref724 if (type_has_bits(ptr_type)) {
873 // and debug info is the same as if the child type were []T.725 entry->size_in_bits = ptr_type->size_in_bits + g->builtin_types.entry_usize->size_in_bits;
874 if (is_slice(child_type)) {726 entry->abi_size = ptr_type->abi_size + g->builtin_types.entry_usize->abi_size;
875 ZigType *child_ptr_type = child_type->data.structure.fields[slice_ptr_index].type_entry;727 entry->abi_align = ptr_type->abi_align;
876 assert(child_ptr_type->id == ZigTypeIdPointer);728 } else {
877 if (child_ptr_type->data.pointer.is_const || child_ptr_type->data.pointer.is_volatile ||729 entry->size_in_bits = g->builtin_types.entry_usize->size_in_bits;
878 child_ptr_type->data.pointer.explicit_alignment != 0 || child_ptr_type->data.pointer.allow_zero)730 entry->abi_size = g->builtin_types.entry_usize->abi_size;
879 {731 entry->abi_align = g->builtin_types.entry_usize->abi_align;
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 }
984 }732 }
985733
986
987 entry->data.structure.resolve_status = ResolveStatusSizeKnown;
988
989 *parent_pointer = entry;734 *parent_pointer = entry;
990 return entry;735 return entry;
991}736}
...@@ -998,15 +743,20 @@ ZigType *get_opaque_type(CodeGen *g, Scope *scope, AstNode *source_node, const c...@@ -998,15 +743,20 @@ ZigType *get_opaque_type(CodeGen *g, Scope *scope, AstNode *source_node, const c
998 ZigType *import = scope ? get_scope_import(scope) : nullptr;743 ZigType *import = scope ? get_scope_import(scope) : nullptr;
999 unsigned line = source_node ? (unsigned)(source_node->line + 1) : 0;744 unsigned line = source_node ? (unsigned)(source_node->line + 1) : 0;
1000745
1001 entry->type_ref = LLVMInt8Type();746 entry->llvm_type = LLVMInt8Type();
1002 entry->di_type = ZigLLVMCreateDebugForwardDeclType(g->dbuilder,747 entry->llvm_di_type = ZigLLVMCreateDebugForwardDeclType(g->dbuilder,
1003 ZigLLVMTag_DW_structure_type(), full_name,748 ZigLLVMTag_DW_structure_type(), full_name,
1004 import ? ZigLLVMFileToScope(import->data.structure.root_struct->di_file) : nullptr,749 import ? ZigLLVMFileToScope(import->data.structure.root_struct->di_file) : nullptr,
1005 import ? import->data.structure.root_struct->di_file : nullptr,750 import ? import->data.structure.root_struct->di_file : nullptr,
1006 line);751 line);
1007 entry->zero_bits = false;
1008 entry->data.opaque.bare_name = bare_name;752 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
1010 return entry;760 return entry;
1011}761}
1012762
...@@ -1018,7 +768,9 @@ ZigType *get_bound_fn_type(CodeGen *g, ZigFn *fn_entry) {...@@ -1018,7 +768,9 @@ ZigType *get_bound_fn_type(CodeGen *g, ZigFn *fn_entry) {
1018768
1019 ZigType *bound_fn_type = new_type_table_entry(ZigTypeIdBoundFn);769 ZigType *bound_fn_type = new_type_table_entry(ZigTypeIdBoundFn);
1020 bound_fn_type->data.bound_fn.fn_type = fn_type;770 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
1023 buf_resize(&bound_fn_type->name, 0);775 buf_resize(&bound_fn_type->name, 0);
1024 buf_appendf(&bound_fn_type->name, "(bound %s)", buf_ptr(&fn_type->name));776 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) {...@@ -1114,8 +866,6 @@ ZigType *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
1114 ZigType *fn_type = new_type_table_entry(ZigTypeIdFn);866 ZigType *fn_type = new_type_table_entry(ZigTypeIdFn);
1115 fn_type->data.fn.fn_type_id = *fn_type_id;867 fn_type->data.fn.fn_type_id = *fn_type_id;
1116868
1117 bool skip_debug_info = false;
1118
1119 // populate the name of the type869 // populate the name of the type
1120 buf_resize(&fn_type->name, 0);870 buf_resize(&fn_type->name, 0);
1121 if (fn_type->data.fn.fn_type_id.cc == CallingConventionAsync) {871 if (fn_type->data.fn.fn_type_id.cc == CallingConventionAsync) {
...@@ -1133,8 +883,6 @@ ZigType *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {...@@ -1133,8 +883,6 @@ ZigType *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
1133 const char *comma = (i == 0) ? "" : ", ";883 const char *comma = (i == 0) ? "" : ", ";
1134 const char *noalias_str = param_info->is_noalias ? "noalias " : "";884 const char *noalias_str = param_info->is_noalias ? "noalias " : "";
1135 buf_appendf(&fn_type->name, "%s%s%s", comma, noalias_str, buf_ptr(&param_type->name));885 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;
1138 }886 }
1139887
1140 if (fn_type_id->is_var_args) {888 if (fn_type_id->is_var_args) {
...@@ -1146,116 +894,11 @@ ZigType *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {...@@ -1146,116 +894,11 @@ ZigType *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
1146 buf_appendf(&fn_type->name, " align(%" PRIu32 ")", fn_type_id->alignment);894 buf_appendf(&fn_type->name, " align(%" PRIu32 ")", fn_type_id->alignment);
1147 }895 }
1148 buf_appendf(&fn_type->name, " %s", buf_ptr(&fn_type_id->return_type->name));896 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) {898 fn_type->size_in_bits = g->builtin_types.entry_usize->size_in_bits;
1239 FnWalk fn_walk = {};899 fn_type->abi_size = g->builtin_types.entry_usize->abi_size;
1240 fn_walk.id = FnWalkIdTypes;900 fn_type->abi_align = (fn_type_id->alignment == 0) ?
1241 fn_walk.data.types.param_di_types = &param_di_types;901 g->builtin_types.entry_usize->abi_align : fn_type_id->alignment;
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 }
1259902
1260 g->fn_type_table.put(&fn_type->data.fn.fn_type_id, fn_type);903 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 *...@@ -1282,14 +925,6 @@ static ZigType *get_root_container_type(CodeGen *g, const char *full_name, Buf *
1282 entry->data.structure.decls_scope = create_decls_scope(g, nullptr, nullptr, entry, entry, bare_name);925 entry->data.structure.decls_scope = create_decls_scope(g, nullptr, nullptr, entry, entry, bare_name);
1283 entry->data.structure.root_struct = root_struct;926 entry->data.structure.root_struct = root_struct;
1284 entry->data.structure.layout = ContainerLayoutAuto;927 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
1294 buf_init_from_str(&entry->name, full_name);929 buf_init_from_str(&entry->name, full_name);
1295 return entry;930 return entry;
...@@ -1316,16 +951,6 @@ ZigType *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind...@@ -1316,16 +951,6 @@ ZigType *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind
1316 break;951 break;
1317 }952 }
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
1329 buf_init_from_str(&entry->name, full_name);954 buf_init_from_str(&entry->name, full_name);
1330955
1331 return entry;956 return entry;
...@@ -1375,7 +1000,9 @@ ZigType *get_generic_fn_type(CodeGen *g, FnTypeId *fn_type_id) {...@@ -1375,7 +1000,9 @@ ZigType *get_generic_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
13751000
1376 fn_type->data.fn.fn_type_id = *fn_type_id;1001 fn_type->data.fn.fn_type_id = *fn_type_id;
1377 fn_type->data.fn.is_generic = true;1002 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;
1379 return fn_type;1006 return fn_type;
1380}1007}
13811008
...@@ -1611,14 +1238,10 @@ ZigType *get_auto_err_set_type(CodeGen *g, ZigFn *fn_entry) {...@@ -1611,14 +1238,10 @@ ZigType *get_auto_err_set_type(CodeGen *g, ZigFn *fn_entry) {
1611 ZigType *err_set_type = new_type_table_entry(ZigTypeIdErrorSet);1238 ZigType *err_set_type = new_type_table_entry(ZigTypeIdErrorSet);
1612 buf_resize(&err_set_type->name, 0);1239 buf_resize(&err_set_type->name, 0);
1613 buf_appendf(&err_set_type->name, "@typeOf(%s).ReturnType.ErrorSet", buf_ptr(&fn_entry->symbol_name));1240 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;
1616 err_set_type->data.error_set.err_count = 0;1241 err_set_type->data.error_set.err_count = 0;
1617 err_set_type->data.error_set.errors = nullptr;1242 err_set_type->data.error_set.errors = nullptr;
1618 err_set_type->data.error_set.infer_fn = fn_entry;1243 err_set_type->data.error_set.infer_fn = fn_entry;
16191244
1620 g->error_di_types.append(&err_set_type->di_type);
1621
1622 return err_set_type;1245 return err_set_type;
1623}1246}
16241247
...@@ -1858,10 +1481,10 @@ bool type_is_invalid(ZigType *type_entry) {...@@ -1858,10 +1481,10 @@ bool type_is_invalid(ZigType *type_entry) {
1858 return true;1481 return true;
1859 case ZigTypeIdStruct:1482 case ZigTypeIdStruct:
1860 return type_entry->data.structure.resolve_status == ResolveStatusInvalid;1483 return type_entry->data.structure.resolve_status == ResolveStatusInvalid;
1484 case ZigTypeIdUnion:
1485 return type_entry->data.unionation.resolve_status == ResolveStatusInvalid;
1861 case ZigTypeIdEnum:1486 case ZigTypeIdEnum:
1862 return type_entry->data.enumeration.is_invalid;1487 return type_entry->data.enumeration.is_invalid;
1863 case ZigTypeIdUnion:
1864 return type_entry->data.unionation.is_invalid;
1865 default:1488 default:
1866 return false;1489 return false;
1867 }1490 }
...@@ -1870,181 +1493,66 @@ bool type_is_invalid(ZigType *type_entry) {...@@ -1870,181 +1493,66 @@ bool type_is_invalid(ZigType *type_entry) {
18701493
18711494
1872static Error resolve_enum_type(CodeGen *g, ZigType *enum_type) {1495static 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)1500ZigType *get_struct_type(CodeGen *g, const char *type_name, const char *field_names[],
1879 return ErrorNone;1501 ZigType *field_types[], size_t field_count)
1502{
1503 ZigType *struct_type = new_type_table_entry(ZigTypeIdStruct);
18801504
1881 Error err;1505 buf_init_from_str(&struct_type->name, type_name);
1882 if ((err = resolve_enum_zero_bits(g, enum_type)))
1883 return err;
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) {1513 size_t abi_align = 0;
1888 if (!enum_type->data.enumeration.reported_infinite_err) {1514 for (size_t i = 0; i < field_count; i += 1) {
1889 enum_type->data.enumeration.is_invalid = true;1515 TypeStructField *field = &struct_type->data.structure.fields[i];
1890 enum_type->data.enumeration.reported_infinite_err = true;1516 field->name = buf_create_from_str(field_names[i]);
1891 ErrorMsg *msg = add_node_error(g, decl_node,1517 field->type_entry = field_types[i];
1892 buf_sprintf("enum '%s' contains itself", buf_ptr(&enum_type->name)));1518 field->src_index = i;
1893 emit_error_notes_for_ref_stack(g, msg);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;
1894 }1529 }
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);
1896 }1533 }
18971534
1898 assert(!enum_type->data.enumeration.zero_bits_loop_flag);1535 size_t next_offset = 0;
1899 assert(decl_node->type == NodeTypeContainerDecl);1536 for (size_t i = 0; i < field_count; i += 1) {
1900 assert(enum_type->di_type);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);1547 return struct_type;
1905 ZigLLVMDIEnumerator **di_enumerators = allocate<ZigLLVMDIEnumerator*>(field_count);1548}
19061549
1907 Scope *scope = &enum_type->data.enumeration.decls_scope->base;1550static size_t get_store_size_in_bits(size_t size_in_bits) {
1908 ZigType *import = get_scope_import(scope);1551 return (size_in_bits + 7) / 8;
1552}
19091553
1910 // set temporary flag1554static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
1911 enum_type->data.enumeration.embedded_in_current = true;1555 assert(struct_type->id == ZigTypeIdStruct);
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);
20481556
2049 Error err;1557 Error err;
20501558
...@@ -2068,454 +1576,284 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {...@@ -2068,454 +1576,284 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
2068 return ErrorSemanticAnalyzeFail;1576 return ErrorSemanticAnalyzeFail;
2069 }1577 }
20701578
2071 struct_type->data.structure.resolve_loop_flag = true;
2072
2073 assert(struct_type->data.structure.fields || struct_type->data.structure.src_field_count == 0);1579 assert(struct_type->data.structure.fields || struct_type->data.structure.src_field_count == 0);
2074 assert(decl_node->type == NodeTypeContainerDecl);1580 assert(decl_node->type == NodeTypeContainerDecl);
20751581
2076 size_t field_count = struct_type->data.structure.src_field_count;1582 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;1584 bool packed = (struct_type->data.structure.layout == ContainerLayoutPacked);
2079 LLVMTypeRef *element_types = allocate<LLVMTypeRef>(gen_field_count);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;1589 // Compute offsets for all the fields.
2084 bool packed = (struct_type->data.structure.layout == ContainerLayoutPacked);
2085 size_t packed_bits_offset = 0;1590 size_t packed_bits_offset = 0;
1591 size_t next_offset = 0;
2086 size_t first_packed_bits_offset_misalign = SIZE_MAX;1592 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
2089 for (size_t i = 0; i < field_count; i += 1) {1597 for (size_t i = 0; i < field_count; i += 1) {
2090 TypeStructField *type_struct_field = &struct_type->data.structure.fields[i];1598 TypeStructField *type_struct_field = &struct_type->data.structure.fields[i];
2091 ZigType *field_type = type_struct_field->type_entry;1599 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))) {
2094 struct_type->data.structure.resolve_status = ResolveStatusInvalid;1605 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2095 break;1606 return ErrorSemanticAnalyzeFail;
2096 }1607 }
20971608
2098 if (struct_type->data.structure.layout == ContainerLayoutExtern) {1609 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid) {
2099 if (!type_allowed_in_extern(g, field_type)) {1610 return ErrorSemanticAnalyzeFail;
2100 AstNode *field_source_node = decl_node->data.container_decl.fields.at(i);
2101 add_node_error(g, field_source_node,
2102 buf_sprintf("extern structs cannot contain fields of type '%s'",
2103 buf_ptr(&field_type->name)));
2104 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2105 break;
2106 }
2107 }1611 }
21081612
2109 if (!type_has_bits(field_type))
2110 continue;
2111
2112 type_struct_field->gen_index = gen_field_index;1613 type_struct_field->gen_index = gen_field_index;
1614 type_struct_field->offset = next_offset;
21131615
2114 if (packed) {1616 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
2121 size_t field_size_in_bits = type_size_bits(g, field_type);1617 size_t field_size_in_bits = type_size_bits(g, field_type);
2122 size_t next_packed_bits_offset = packed_bits_offset + field_size_in_bits;1618 size_t next_packed_bits_offset = packed_bits_offset + field_size_in_bits;
21231619
1620 size_in_bits += field_size_in_bits;
1621
2124 if (first_packed_bits_offset_misalign != SIZE_MAX) {1622 if (first_packed_bits_offset_misalign != SIZE_MAX) {
2125 // this field is not byte-aligned; it is part of the previous field with a bit offset1623 // this field is not byte-aligned; it is part of the previous field with a bit offset
2126 type_struct_field->bit_offset_in_host = packed_bits_offset - first_packed_bits_offset_misalign;1624 type_struct_field->bit_offset_in_host = packed_bits_offset - first_packed_bits_offset_misalign;
21271625
2128 size_t full_bit_count = next_packed_bits_offset - first_packed_bits_offset_misalign;1626 size_t full_bit_count = next_packed_bits_offset - first_packed_bits_offset_misalign;
2129 LLVMTypeRef int_type_ref = LLVMIntType((unsigned)(full_bit_count));1627 if (get_store_size_in_bits(full_bit_count) == full_bit_count) {
2130 if (8 * LLVMStoreSizeOfType(g->target_data_ref, int_type_ref) == full_bit_count) {
2131 // next field recovers store alignment1628 // 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;
2133 gen_field_index += 1;1630 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
2135 first_packed_bits_offset_misalign = SIZE_MAX;1635 first_packed_bits_offset_misalign = SIZE_MAX;
2136 }1636 }
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) {
2138 first_packed_bits_offset_misalign = packed_bits_offset;1638 first_packed_bits_offset_misalign = packed_bits_offset;
2139 type_struct_field->bit_offset_in_host = 0;1639 type_struct_field->bit_offset_in_host = 0;
2140 } else {1640 } else {
2141 // This is a byte-aligned field (both start and end) in a packed struct.1641 // This is a byte-aligned field (both start and end) in a packed struct.
2142 element_types[gen_field_index] = field_type->type_ref;
2143 type_struct_field->bit_offset_in_host = 0;1642 type_struct_field->bit_offset_in_host = 0;
2144 gen_field_index += 1;1643 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;
2145 }1647 }
2146 packed_bits_offset = next_packed_bits_offset;1648 packed_bits_offset = next_packed_bits_offset;
2147 } else {1649 } else {
2148 element_types[gen_field_index] = field_type->type_ref;
2149 assert(element_types[gen_field_index]);
2150
2151 gen_field_index += 1;1650 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;
2152 }1653 }
2153 debug_field_count += 1;
2154 }1654 }
2155 if (first_packed_bits_offset_misalign != SIZE_MAX) {1655 if (first_packed_bits_offset_misalign != SIZE_MAX) {
2156 size_t full_bit_count = packed_bits_offset - first_packed_bits_offset_misalign;1656 size_t full_bit_count = packed_bits_offset - first_packed_bits_offset_misalign;
2157 LLVMTypeRef int_type_ref = LLVMIntType((unsigned)full_bit_count);1657 size_t store_bit_count = get_store_size_in_bits(full_bit_count);
2158 size_t store_bit_count = 8 * LLVMStoreSizeOfType(g->target_data_ref, int_type_ref);1658 next_offset = next_field_offset(next_offset, abi_align, store_bit_count / 8, 1);
2159 element_types[gen_field_index] = LLVMIntType((unsigned)store_bit_count);1659 host_int_bytes[gen_field_index] = store_bit_count / 8;
2160 gen_field_index += 1;1660 gen_field_index += 1;
2161 }1661 }
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;
2163 struct_type->data.structure.resolve_loop_flag = false;1667 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)1670 return ErrorNone;
2166 return ErrorSemanticAnalyzeFail;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) {1676 Error err;
2171 struct_type->type_ref = LLVMVoidType();
21721677
2173 ZigType *import = get_scope_import(scope);1678 if (union_type->data.unionation.resolve_status == ResolveStatusInvalid)
2174 uint64_t debug_size_in_bits = 0;1679 return ErrorSemanticAnalyzeFail;
2175 uint64_t debug_align_in_bits = 0;1680 if (union_type->data.unionation.resolve_status >= ResolveStatusAlignmentKnown)
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;
2187 return ErrorNone;1681 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;
2188 }1696 }
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 fields1701 ZigType *most_aligned_union_member = nullptr;
2193 gen_field_count = gen_field_index;1702 uint32_t field_count = union_type->data.unionation.src_field_count;
2194 struct_type->data.structure.gen_field_count = (uint32_t)gen_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't1708 if ((err = type_resolve(g, field_type, ResolveStatusAlignmentKnown))) {
2199 // get ensure_complete_type called on it before using it with something that1709 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
2200 // requires a complete type1710 return ErrorSemanticAnalyzeFail;
2201 assert(LLVMStoreSizeOfType(g->target_data_ref, struct_type->type_ref) > 0);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);1716 if (!type_has_bits(field_type))
2206 size_t debug_field_index = 0;
2207 for (size_t i = 0; i < field_count; i += 1) {
2208 AstNode *field_node = decl_node->data.container_decl.fields.at(i);
2209 TypeStructField *type_struct_field = &struct_type->data.structure.fields[i];
2210 size_t gen_field_index = type_struct_field->gen_index;
2211 if (gen_field_index == SIZE_MAX) {
2212 continue;1717 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;
2213 }1723 }
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.1730 ZigType *tag_type = union_type->data.unionation.tag_type;
2218 ZigLLVMDIType *field_di_type;1731 if (tag_type != nullptr && type_has_bits(tag_type)) {
2219 if (field_type->id == ZigTypeIdFn) {1732 if ((err = type_resolve(g, tag_type, ResolveStatusAlignmentKnown))) {
2220 ZigType *field_ptr_type = get_pointer_to_type(g, field_type, true);1733 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
2221 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, field_ptr_type->type_ref);1734 return ErrorSemanticAnalyzeFail;
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;
2227 }1735 }
22281736 if (most_aligned_union_member == nullptr) {
2229 assert(field_type->type_ref);1737 union_type->abi_align = tag_type->abi_align;
2230 assert(struct_type->type_ref);1738 union_type->data.unionation.gen_tag_index = SIZE_MAX;
2231 assert(struct_type->data.structure.resolve_status == ResolveStatusSizeKnown);1739 union_type->data.unionation.gen_union_index = SIZE_MAX;
2232 uint64_t debug_size_in_bits;1740 } else if (tag_type->abi_align > most_aligned_union_member->abi_align) {
2233 uint64_t debug_align_in_bits;1741 union_type->abi_align = tag_type->abi_align;
2234 uint64_t debug_offset_in_bits;1742 union_type->data.unionation.gen_tag_index = 0;
2235 if (packed) {1743 union_type->data.unionation.gen_union_index = 1;
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;
2240 } else {1744 } else {
2241 debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, field_type->type_ref);1745 union_type->abi_align = most_aligned_union_member->abi_align;
2242 debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, field_type->type_ref);1746 union_type->data.unionation.gen_union_index = 0;
2243 debug_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, struct_type->type_ref,1747 union_type->data.unionation.gen_tag_index = 1;
2244 (unsigned)gen_field_index);
2245 }1748 }
2246 di_element_types[debug_field_index] = ZigLLVMCreateDebugMemberType(g->dbuilder,1749 } else {
2247 ZigLLVMTypeToScope(struct_type->di_type), buf_ptr(type_struct_field->name),1750 assert(most_aligned_union_member != nullptr);
2248 import->data.structure.root_struct->di_file, (unsigned)(field_node->line + 1),1751 union_type->abi_align = most_aligned_union_member->abi_align;
2249 debug_size_in_bits,1752 union_type->data.unionation.gen_union_index = SIZE_MAX;
2250 debug_align_in_bits,1753 union_type->data.unionation.gen_tag_index = SIZE_MAX;
2251 debug_offset_in_bits,
2252 0, field_di_type);
2253 assert(di_element_types[debug_field_index]);
2254 debug_field_index += 1;
2255 }1754 }
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
2271 return ErrorNone;1756 return ErrorNone;
2272}1757}
22731758
2274static Error resolve_union_type(CodeGen *g, ZigType *union_type) {1759static Error resolve_union_type(CodeGen *g, ZigType *union_type) {
2275 assert(union_type->id == ZigTypeIdUnion);1760 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)
2278 return ErrorNone;1767 return ErrorNone;
22791768
2280 Error err;1769 if ((err = resolve_union_alignment(g, union_type)))
2281 if ((err = resolve_union_zero_bits(g, union_type)))
2282 return err;1770 return err;
22831771
2284 AstNode *decl_node = union_type->data.unionation.decl_node;1772 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);
2298 assert(decl_node->type == NodeTypeContainerDecl);1775 assert(decl_node->type == NodeTypeContainerDecl);
2299 assert(union_type->di_type);
23001776
2301 uint32_t field_count = union_type->data.unionation.src_field_count;1777 uint32_t field_count = union_type->data.unionation.src_field_count;
23021778
2303 assert(union_type->data.unionation.fields);1779 assert(union_type->data.unionation.fields);
23041780
2305 uint32_t gen_field_count = union_type->data.unionation.gen_field_count;1781 size_t union_abi_size = 0;
2306 ZigLLVMDIType **union_inner_di_types = allocate<ZigLLVMDIType*>(gen_field_count);1782 size_t union_size_in_bits = 0;
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;
23121783
2313 Scope *scope = &union_type->data.unionation.decls_scope->base;1784 if (union_type->data.unionation.resolve_loop_flag) {
2314 ZigType *import = get_scope_import(scope);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
2316 // set temporary flag1795 // set temporary flag
2317 union_type->data.unionation.embedded_in_current = true;1796 union_type->data.unionation.resolve_loop_flag = true;
2318
23191797
2320 for (uint32_t i = 0; i < field_count; i += 1) {1798 for (uint32_t i = 0; i < field_count; i += 1) {
2321 AstNode *field_node = decl_node->data.container_decl.fields.at(i);
2322 TypeUnionField *union_field = &union_type->data.unionation.fields[i];1799 TypeUnionField *union_field = &union_type->data.unionation.fields[i];
2323 ZigType *field_type = union_field->type_entry;1800 ZigType *field_type = union_field->type_entry;
23241801
2325 if ((err = ensure_complete_type(g, field_type))) {1802 if ((err = type_resolve(g, field_type, ResolveStatusSizeKnown))) {
2326 union_type->data.unionation.is_invalid = true;1803 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
2327 continue;1804 return ErrorSemanticAnalyzeFail;
2328 }1805 }
23291806
1807 if (type_is_invalid(union_type))
1808 return ErrorSemanticAnalyzeFail;
1809
2330 if (!type_has_bits(field_type))1810 if (!type_has_bits(field_type))
2331 continue;1811 continue;
23321812
2333 uint64_t store_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, field_type->type_ref);1813 union_abi_size = max(union_abi_size, field_type->abi_size);
2334 uint64_t abi_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, field_type->type_ref);1814 union_size_in_bits = max(union_size_in_bits, field_type->size_in_bits);
23351815 }
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);
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) {1822 ZigType *tag_type = union_type->data.unionation.tag_type;
2350 most_aligned_union_member = field_type;1823 ZigType *most_aligned_union_member = union_type->data.unionation.most_aligned_union_member;
2351 biggest_align_in_bits = abi_align_in_bits;1824 if (tag_type != nullptr && type_has_bits(tag_type)) {
2352 size_of_most_aligned_member_in_bits = store_size_in_bits;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;
2353 }1842 }
1843 } else {
1844 union_type->abi_size = union_abi_size;
1845 union_type->size_in_bits = union_size_in_bits;
2354 }1846 }
23551847
1848 return ErrorNone;
1849}
23561850
2357 // unset temporary flag1851static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
2358 union_type->data.unionation.embedded_in_current = false;1852 assert(enum_type->id == ZigTypeIdEnum);
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;
23621853
2363 if (union_type->data.unionation.is_invalid)1854 if (enum_type->data.enumeration.is_invalid)
2364 return ErrorSemanticAnalyzeFail;1855 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
2519 if (enum_type->data.enumeration.zero_bits_known)1857 if (enum_type->data.enumeration.zero_bits_known)
2520 return ErrorNone;1858 return ErrorNone;
25211859
...@@ -2531,7 +1869,6 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {...@@ -2531,7 +1869,6 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
25311869
2532 AstNode *decl_node = enum_type->data.enumeration.decl_node;1870 AstNode *decl_node = enum_type->data.enumeration.decl_node;
2533 assert(decl_node->type == NodeTypeContainerDecl);1871 assert(decl_node->type == NodeTypeContainerDecl);
2534 assert(enum_type->di_type);
25351872
2536 assert(!enum_type->data.enumeration.fields);1873 assert(!enum_type->data.enumeration.fields);
2537 uint32_t field_count = (uint32_t)decl_node->data.container_decl.fields.length;1874 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) {...@@ -2564,6 +1901,10 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
2564 tag_int_type = get_smallest_unsigned_int_type(g, field_count - 1);1901 tag_int_type = get_smallest_unsigned_int_type(g, field_count - 1);
2565 }1902 }
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
2567 // TODO: Are extern enums allowed to have an init_arg_expr?1908 // TODO: Are extern enums allowed to have an init_arg_expr?
2568 if (decl_node->data.container_decl.init_arg_expr != nullptr) {1909 if (decl_node->data.container_decl.init_arg_expr != nullptr) {
2569 ZigType *wanted_tag_int_type = analyze_type_expr(g, scope, decl_node->data.container_decl.init_arg_expr);1910 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) {...@@ -2587,7 +1928,6 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
2587 }1928 }
2588 }1929 }
2589 enum_type->data.enumeration.tag_int_type = tag_int_type;1930 enum_type->data.enumeration.tag_int_type = tag_int_type;
2590 enum_type->type_ref = tag_int_type->type_ref;
25911931
2592 for (uint32_t field_i = 0; field_i < field_count; field_i += 1) {1932 for (uint32_t field_i = 0; field_i < field_count; field_i += 1) {
2593 AstNode *field_node = decl_node->data.container_decl.fields.at(field_i);1933 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) {...@@ -2671,7 +2011,6 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
2671 }2011 }
26722012
2673 enum_type->data.enumeration.zero_bits_loop_flag = false;2013 enum_type->data.enumeration.zero_bits_loop_flag = false;
2674 enum_type->zero_bits = !type_has_bits(tag_int_type);
2675 enum_type->data.enumeration.zero_bits_known = true;2014 enum_type->data.enumeration.zero_bits_known = true;
26762015
2677 if (enum_type->data.enumeration.is_invalid)2016 if (enum_type->data.enumeration.is_invalid)
...@@ -2698,7 +2037,6 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {...@@ -2698,7 +2037,6 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
26982037
2699 AstNode *decl_node = struct_type->data.structure.decl_node;2038 AstNode *decl_node = struct_type->data.structure.decl_node;
2700 assert(decl_node->type == NodeTypeContainerDecl);2039 assert(decl_node->type == NodeTypeContainerDecl);
2701 assert(struct_type->di_type);
27022040
2703 assert(!struct_type->data.structure.fields);2041 assert(!struct_type->data.structure.fields);
2704 size_t field_count = decl_node->data.container_decl.fields.length;2042 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) {...@@ -2767,7 +2105,10 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
27672105
2768 struct_type->data.structure.resolve_loop_flag = false;2106 struct_type->data.structure.resolve_loop_flag = false;
2769 struct_type->data.structure.gen_field_count = (uint32_t)gen_field_index;2107 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
2772 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid)2113 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid)
2773 return ErrorSemanticAnalyzeFail;2114 return ErrorSemanticAnalyzeFail;
...@@ -2803,52 +2144,50 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {...@@ -2803,52 +2144,50 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {
28032144
2804 struct_type->data.structure.resolve_loop_flag = true;2145 struct_type->data.structure.resolve_loop_flag = true;
2805 assert(decl_node->type == NodeTypeContainerDecl);2146 assert(decl_node->type == NodeTypeContainerDecl);
2806 assert(struct_type->di_type);
28072147
2148 size_t abi_align = 0;
2808 size_t field_count = struct_type->data.structure.src_field_count;2149 size_t field_count = struct_type->data.structure.src_field_count;
2809 if (struct_type->data.structure.layout == ContainerLayoutPacked) {2150 bool packed = struct_type->data.structure.layout == ContainerLayoutPacked;
2810 struct_type->data.structure.abi_alignment = 1;2151
2811 for (size_t i = 0; i < field_count; i += 1) {2152 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) {
2819 TypeStructField *field = &struct_type->data.structure.fields[i];2153 TypeStructField *field = &struct_type->data.structure.fields[i];
2820 uint32_t this_field_align;2154 ZigType *field_type = field->type_entry;
28212155 assert(field_type != nullptr);
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 }
28372156
2838 if (!type_has_bits(field->type_entry))2157 if ((err = type_resolve(g, field_type, ResolveStatusAlignmentKnown))) {
2839 continue;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))) {
2842 struct_type->data.structure.resolve_status = ResolveStatusInvalid;2179 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;
2844 }2190 }
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;
2852 }2191 }
2853 }2192 }
28542193
...@@ -2859,6 +2198,7 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {...@@ -2859,6 +2198,7 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {
2859 }2198 }
28602199
2861 struct_type->data.structure.resolve_status = ResolveStatusAlignmentKnown;2200 struct_type->data.structure.resolve_status = ResolveStatusAlignmentKnown;
2201 struct_type->abi_align = abi_align;
2862 return ErrorNone;2202 return ErrorNone;
2863}2203}
28642204
...@@ -2867,57 +2207,41 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -2867,57 +2207,41 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
28672207
2868 Error err;2208 Error err;
28692209
2870 if (union_type->data.unionation.is_invalid)2210 if (union_type->data.unionation.resolve_status == ResolveStatusInvalid)
2871 return ErrorSemanticAnalyzeFail;2211 return ErrorSemanticAnalyzeFail;
28722212
2873 if (union_type->data.unionation.zero_bits_known)2213 if (union_type->data.unionation.resolve_status >= ResolveStatusZeroBitsKnown)
2874 return ErrorNone;2214 return ErrorNone;
28752215
2876 if (type_is_invalid(union_type))2216 if (union_type->data.unionation.resolve_loop_flag) {
2877 return ErrorSemanticAnalyzeFail;
2878
2879 if (union_type->data.unionation.zero_bits_loop_flag) {
2880 // If we get here it's due to recursion. From this we conclude that the struct is2217 // 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 field2218 // not zero bits.
2882 // is a pointer to this very struct, or a function pointer with parameters that2219 // TODO actually it could still be zero bits. Here we should continue analyzing
2883 // reference such a type.2220 // the union from the next field index.
2884 union_type->data.unionation.zero_bits_known = true;2221 union_type->data.unionation.resolve_status = ResolveStatusZeroBitsKnown;
2885 union_type->data.unionation.zero_bits_loop_flag = false;2222 union_type->data.unionation.resolve_loop_flag = false;
2886 if (union_type->data.unionation.abi_alignment == 0) {2223 union_type->abi_size = SIZE_MAX;
2887 if (union_type->data.unionation.layout == ContainerLayoutPacked) {2224 union_type->size_in_bits = SIZE_MAX;
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 }
2894 return ErrorNone;2225 return ErrorNone;
2895 }2226 }
28962227
2897 union_type->data.unionation.zero_bits_loop_flag = true;2228 union_type->data.unionation.resolve_loop_flag = true;
28982229
2899 AstNode *decl_node = union_type->data.unionation.decl_node;2230 AstNode *decl_node = union_type->data.unionation.decl_node;
2900 assert(decl_node->type == NodeTypeContainerDecl);2231 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);
2904 uint32_t field_count = (uint32_t)decl_node->data.container_decl.fields.length;2234 uint32_t field_count = (uint32_t)decl_node->data.container_decl.fields.length;
2905 if (field_count == 0) {2235 if (field_count == 0) {
2906 add_node_error(g, decl_node, buf_sprintf("unions must have 1 or more fields"));2236 add_node_error(g, decl_node, buf_sprintf("unions must have 1 or more fields"));
2907
2908 union_type->data.unionation.src_field_count = field_count;2237 union_type->data.unionation.src_field_count = field_count;
2909 union_type->data.unionation.fields = nullptr;2238 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
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;
2913 return ErrorSemanticAnalyzeFail;2239 return ErrorSemanticAnalyzeFail;
2914 }2240 }
2915 union_type->data.unionation.src_field_count = field_count;2241 union_type->data.unionation.src_field_count = field_count;
2916 union_type->data.unionation.fields = allocate<TypeUnionField>(field_count);2242 union_type->data.unionation.fields = allocate<TypeUnionField>(field_count);
2917 union_type->data.unionation.fields_by_name.init(field_count);2243 union_type->data.unionation.fields_by_name.init(field_count);
29182244
2919 uint32_t biggest_align_bytes = 0;
2920
2921 Scope *scope = &union_type->data.unionation.decls_scope->base;2245 Scope *scope = &union_type->data.unionation.decls_scope->base;
29222246
2923 HashMap<BigInt, AstNode *, bigint_hash, bigint_eql> occupied_tag_values = {};2247 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) {...@@ -2931,7 +2255,6 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
2931 bool create_enum_type = decl_node->data.container_decl.auto_enum || (enum_type_node == nullptr && want_safety);2255 bool create_enum_type = decl_node->data.container_decl.auto_enum || (enum_type_node == nullptr && want_safety);
2932 bool *covered_enum_fields;2256 bool *covered_enum_fields;
2933 ZigLLVMDIEnumerator **di_enumerators;2257 ZigLLVMDIEnumerator **di_enumerators;
2934 uint32_t abi_alignment_so_far;
2935 if (create_enum_type) {2258 if (create_enum_type) {
2936 occupied_tag_values.init(field_count);2259 occupied_tag_values.init(field_count);
29372260
...@@ -2941,13 +2264,13 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -2941,13 +2264,13 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
2941 if (enum_type_node != nullptr) {2264 if (enum_type_node != nullptr) {
2942 tag_int_type = analyze_type_expr(g, scope, enum_type_node);2265 tag_int_type = analyze_type_expr(g, scope, enum_type_node);
2943 if (type_is_invalid(tag_int_type)) {2266 if (type_is_invalid(tag_int_type)) {
2944 union_type->data.unionation.is_invalid = true;2267 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
2945 return ErrorSemanticAnalyzeFail;2268 return ErrorSemanticAnalyzeFail;
2946 }2269 }
2947 if (tag_int_type->id != ZigTypeIdInt) {2270 if (tag_int_type->id != ZigTypeIdInt) {
2948 add_node_error(g, enum_type_node,2271 add_node_error(g, enum_type_node,
2949 buf_sprintf("expected integer tag type, found '%s'", buf_ptr(&tag_int_type->name)));2272 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;
2951 return ErrorSemanticAnalyzeFail;2274 return ErrorSemanticAnalyzeFail;
2952 }2275 }
2953 } else if (auto_layout && field_count == 1) {2276 } else if (auto_layout && field_count == 1) {
...@@ -2955,13 +2278,15 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -2955,13 +2278,15 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
2955 } else {2278 } else {
2956 tag_int_type = get_smallest_unsigned_int_type(g, field_count - 1);2279 tag_int_type = get_smallest_unsigned_int_type(g, field_count - 1);
2957 }2280 }
2958 abi_alignment_so_far = get_abi_alignment(g, tag_int_type);
29592281
2960 tag_type = new_type_table_entry(ZigTypeIdEnum);2282 tag_type = new_type_table_entry(ZigTypeIdEnum);
2961 buf_resize(&tag_type->name, 0);2283 buf_resize(&tag_type->name, 0);
2962 buf_appendf(&tag_type->name, "@TagType(%s)", buf_ptr(&union_type->name));2284 buf_appendf(&tag_type->name, "@TagType(%s)", buf_ptr(&union_type->name));
2963 tag_type->type_ref = tag_int_type->type_ref;2285 tag_type->llvm_type = tag_int_type->llvm_type;
2964 tag_type->zero_bits = tag_int_type->zero_bits;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
2966 tag_type->data.enumeration.tag_int_type = tag_int_type;2291 tag_type->data.enumeration.tag_int_type = tag_int_type;
2967 tag_type->data.enumeration.zero_bits_known = true;2292 tag_type->data.enumeration.zero_bits_known = true;
...@@ -2975,11 +2300,11 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -2975,11 +2300,11 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
2975 } else if (enum_type_node != nullptr) {2300 } else if (enum_type_node != nullptr) {
2976 ZigType *enum_type = analyze_type_expr(g, scope, enum_type_node);2301 ZigType *enum_type = analyze_type_expr(g, scope, enum_type_node);
2977 if (type_is_invalid(enum_type)) {2302 if (type_is_invalid(enum_type)) {
2978 union_type->data.unionation.is_invalid = true;2303 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
2979 return ErrorSemanticAnalyzeFail;2304 return ErrorSemanticAnalyzeFail;
2980 }2305 }
2981 if (enum_type->id != ZigTypeIdEnum) {2306 if (enum_type->id != ZigTypeIdEnum) {
2982 union_type->data.unionation.is_invalid = true;2307 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
2983 add_node_error(g, enum_type_node,2308 add_node_error(g, enum_type_node,
2984 buf_sprintf("expected enum tag type, found '%s'", buf_ptr(&enum_type->name)));2309 buf_sprintf("expected enum tag type, found '%s'", buf_ptr(&enum_type->name)));
2985 return ErrorSemanticAnalyzeFail;2310 return ErrorSemanticAnalyzeFail;
...@@ -2989,11 +2314,9 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -2989,11 +2314,9 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
2989 return err;2314 return err;
2990 }2315 }
2991 tag_type = enum_type;2316 tag_type = enum_type;
2992 abi_alignment_so_far = get_abi_alignment(g, enum_type); // this populates src_field_count
2993 covered_enum_fields = allocate<bool>(enum_type->data.enumeration.src_field_count);2317 covered_enum_fields = allocate<bool>(enum_type->data.enumeration.src_field_count);
2994 } else {2318 } else {
2995 tag_type = nullptr;2319 tag_type = nullptr;
2996 abi_alignment_so_far = 0;
2997 }2320 }
2998 union_type->data.unionation.tag_type = tag_type;2321 union_type->data.unionation.tag_type = tag_type;
29992322
...@@ -3010,8 +2333,8 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -3010,8 +2333,8 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
3010 ErrorMsg *msg = add_node_error(g, field_node,2333 ErrorMsg *msg = add_node_error(g, field_node,
3011 buf_sprintf("duplicate union field: '%s'", buf_ptr(union_field->name)));2334 buf_sprintf("duplicate union field: '%s'", buf_ptr(union_field->name)));
3012 add_error_note(g, msg, field_entry->value->decl_node, buf_sprintf("other field here"));2335 add_error_note(g, msg, field_entry->value->decl_node, buf_sprintf("other field here"));
3013 union_type->data.unionation.is_invalid = true;2336 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
3014 continue;2337 return ErrorSemanticAnalyzeFail;
3015 }2338 }
30162339
3017 ZigType *field_type;2340 ZigType *field_type;
...@@ -3020,29 +2343,31 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -3020,29 +2343,31 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
3020 field_type = g->builtin_types.entry_void;2343 field_type = g->builtin_types.entry_void;
3021 } else {2344 } else {
3022 add_node_error(g, field_node, buf_sprintf("union field missing type"));2345 add_node_error(g, field_node, buf_sprintf("union field missing type"));
3023 union_type->data.unionation.is_invalid = true;2346 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
3024 continue;2347 return ErrorSemanticAnalyzeFail;
3025 }2348 }
3026 } else {2349 } else {
3027 field_type = analyze_type_expr(g, scope, field_node->data.struct_field.type);2350 field_type = analyze_type_expr(g, scope, field_node->data.struct_field.type);
3028 if ((err = type_resolve(g, field_type, ResolveStatusAlignmentKnown))) {2351 if ((err = type_resolve(g, field_type, ResolveStatusAlignmentKnown))) {
3029 union_type->data.unionation.is_invalid = true;2352 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
3030 continue;2353 return ErrorSemanticAnalyzeFail;
3031 }2354 }
2355 if (union_type->data.unionation.resolve_status == ResolveStatusInvalid)
2356 return ErrorSemanticAnalyzeFail;
3032 }2357 }
3033 union_field->type_entry = field_type;2358 union_field->type_entry = field_type;
30342359
3035 if (field_type->id == ZigTypeIdOpaque) {2360 if (field_type->id == ZigTypeIdOpaque) {
3036 add_node_error(g, field_node->data.struct_field.type,2361 add_node_error(g, field_node->data.struct_field.type,
3037 buf_sprintf("opaque types have unknown size and therefore cannot be directly embedded in unions"));2362 buf_sprintf("opaque types have unknown size and therefore cannot be directly embedded in unions"));
3038 union_type->data.unionation.is_invalid = true;2363 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
3039 continue;2364 return ErrorSemanticAnalyzeFail;
3040 }2365 }
30412366
3042 switch (type_requires_comptime(g, field_type)) {2367 switch (type_requires_comptime(g, field_type)) {
3043 case ReqCompTimeInvalid:2368 case ReqCompTimeInvalid:
3044 union_type->data.unionation.is_invalid = true;2369 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
3045 continue;2370 return ErrorSemanticAnalyzeFail;
3046 case ReqCompTimeYes:2371 case ReqCompTimeYes:
3047 union_type->data.unionation.requires_comptime = true;2372 union_type->data.unionation.requires_comptime = true;
3048 break;2373 break;
...@@ -3074,8 +2399,8 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -3074,8 +2399,8 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
3074 ZigType *tag_int_type = tag_type->data.enumeration.tag_int_type;2399 ZigType *tag_int_type = tag_type->data.enumeration.tag_int_type;
3075 ConstExprValue *result = analyze_const_value(g, scope, tag_value, tag_int_type, nullptr);2400 ConstExprValue *result = analyze_const_value(g, scope, tag_value, tag_int_type, nullptr);
3076 if (type_is_invalid(result->type)) {2401 if (type_is_invalid(result->type)) {
3077 union_type->data.unionation.is_invalid = true;2402 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
3078 continue;2403 return ErrorSemanticAnalyzeFail;
3079 }2404 }
3080 assert(result->special != ConstValSpecialRuntime);2405 assert(result->special != ConstValSpecialRuntime);
3081 assert(result->type->id == ZigTypeIdInt);2406 assert(result->type->id == ZigTypeIdInt);
...@@ -3090,8 +2415,8 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -3090,8 +2415,8 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
3090 buf_sprintf("enum tag value %s already taken", buf_ptr(val_buf)));2415 buf_sprintf("enum tag value %s already taken", buf_ptr(val_buf)));
3091 add_error_note(g, msg, entry->value,2416 add_error_note(g, msg, entry->value,
3092 buf_sprintf("other occurrence here"));2417 buf_sprintf("other occurrence here"));
3093 union_type->data.unionation.is_invalid = true;2418 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
3094 continue;2419 return ErrorSemanticAnalyzeFail;
3095 }2420 }
3096 }2421 }
3097 } else if (enum_type_node != nullptr) {2422 } else if (enum_type_node != nullptr) {
...@@ -3101,8 +2426,8 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -3101,8 +2426,8 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
3101 buf_sprintf("enum field not found: '%s'", buf_ptr(field_name)));2426 buf_sprintf("enum field not found: '%s'", buf_ptr(field_name)));
3102 add_error_note(g, msg, tag_type->data.enumeration.decl_node,2427 add_error_note(g, msg, tag_type->data.enumeration.decl_node,
3103 buf_sprintf("enum declared here"));2428 buf_sprintf("enum declared here"));
3104 union_type->data.unionation.is_invalid = true;2429 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
3105 continue;2430 return ErrorSemanticAnalyzeFail;
3106 }2431 }
3107 covered_enum_fields[union_field->enum_field->decl_index] = true;2432 covered_enum_fields[union_field->enum_field->decl_index] = true;
3108 } else {2433 } else {
...@@ -3118,21 +2443,8 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -3118,21 +2443,8 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
31182443
3119 union_field->gen_index = gen_field_index;2444 union_field->gen_index = gen_field_index;
3120 gen_field_index += 1;2445 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 }
3129 }2446 }
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
3136 bool src_have_tag = decl_node->data.container_decl.auto_enum ||2448 bool src_have_tag = decl_node->data.container_decl.auto_enum ||
3137 decl_node->data.container_decl.init_arg_expr != nullptr;2449 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) {...@@ -3152,7 +2464,7 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
3152 decl_node->data.container_decl.init_arg_expr : decl_node;2464 decl_node->data.container_decl.init_arg_expr : decl_node;
3153 add_node_error(g, source_node,2465 add_node_error(g, source_node,
3154 buf_sprintf("%s union does not support enum tag type", qual_str));2466 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;
3156 return ErrorSemanticAnalyzeFail;2468 return ErrorSemanticAnalyzeFail;
3157 }2469 }
31582470
...@@ -3194,33 +2506,24 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -3194,33 +2506,24 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
3194 buf_sprintf("enum field missing: '%s'", buf_ptr(enum_field->name)));2506 buf_sprintf("enum field missing: '%s'", buf_ptr(enum_field->name)));
3195 add_error_note(g, msg, field_node,2507 add_error_note(g, msg, field_node,
3196 buf_sprintf("declared here"));2508 buf_sprintf("declared here"));
3197 union_type->data.unionation.is_invalid = true;2509 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
3198 }2510 }
3199 }2511 }
3200 }2512 }
32012513
3202 if (create_enum_type) {2514 if (union_type->data.unionation.resolve_status == ResolveStatusInvalid) {
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)
3223 return ErrorSemanticAnalyzeFail;2515 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
3225 return ErrorNone;2528 return ErrorNone;
3226}2529}
...@@ -4007,7 +3310,7 @@ TypeStructField *find_struct_type_field(ZigType *type_entry, Buf *name) {...@@ -4007,7 +3310,7 @@ TypeStructField *find_struct_type_field(ZigType *type_entry, Buf *name) {
40073310
4008TypeUnionField *find_union_type_field(ZigType *type_entry, Buf *name) {3311TypeUnionField *find_union_type_field(ZigType *type_entry, Buf *name) {
4009 assert(type_entry->id == ZigTypeIdUnion);3312 assert(type_entry->id == ZigTypeIdUnion);
4010 assert(type_entry->data.unionation.zero_bits_known);3313 assert(type_is_resolved(type_entry, ResolveStatusZeroBitsKnown));
4011 if (type_entry->data.unionation.src_field_count == 0)3314 if (type_entry->data.unionation.src_field_count == 0)
4012 return nullptr;3315 return nullptr;
4013 auto entry = type_entry->data.unionation.fields_by_name.maybe_get(name);3316 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) {...@@ -4018,7 +3321,7 @@ TypeUnionField *find_union_type_field(ZigType *type_entry, Buf *name) {
40183321
4019TypeUnionField *find_union_field_by_tag(ZigType *type_entry, const BigInt *tag) {3322TypeUnionField *find_union_field_by_tag(ZigType *type_entry, const BigInt *tag) {
4020 assert(type_entry->id == ZigTypeIdUnion);3323 assert(type_entry->id == ZigTypeIdUnion);
4021 assert(type_entry->data.unionation.zero_bits_known);3324 assert(type_is_resolved(type_entry, ResolveStatusZeroBitsKnown));
4022 for (uint32_t i = 0; i < type_entry->data.unionation.src_field_count; i += 1) {3325 for (uint32_t i = 0; i < type_entry->data.unionation.src_field_count; i += 1) {
4023 TypeUnionField *field = &type_entry->data.unionation.fields[i];3326 TypeUnionField *field = &type_entry->data.unionation.fields[i];
4024 if (bigint_cmp(&field->enum_field->value, tag) == CmpEQ) {3327 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) {...@@ -4626,18 +3929,26 @@ ZigType *get_vector_type(CodeGen *g, uint32_t len, ZigType *elem_type) {
4626 }3929 }
46273930
4628 ZigType *entry = new_type_table_entry(ZigTypeIdVector);3931 ZigType *entry = new_type_table_entry(ZigTypeIdVector);
4629 entry->zero_bits = (len == 0) || !type_has_bits(elem_type);3932 if ((len != 0) && type_has_bits(elem_type)) {
4630 entry->type_ref = entry->zero_bits ? LLVMVoidType() : LLVMVectorType(elem_type->type_ref, len);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 }
4631 entry->data.vector.len = len;3946 entry->data.vector.len = len;
4632 entry->data.vector.elem_type = elem_type;3947 entry->data.vector.elem_type = elem_type;
46333948
4634 buf_resize(&entry->name, 0);3949 buf_resize(&entry->name, 0);
4635 buf_appendf(&entry->name, "@Vector(%u, %s)", len, buf_ptr(&elem_type->name));3950 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
4641 g->type_table.put(type_id, entry);3952 g->type_table.put(type_id, entry);
4642 return entry;3953 return entry;
4643}3954}
...@@ -4677,6 +3988,7 @@ bool handle_is_ptr(ZigType *type_entry) {...@@ -4677,6 +3988,7 @@ bool handle_is_ptr(ZigType *type_entry) {
4677 return false;3988 return false;
4678 case ZigTypeIdArray:3989 case ZigTypeIdArray:
4679 case ZigTypeIdStruct:3990 case ZigTypeIdStruct:
3991 case ZigTypeIdUnion:
4680 return type_has_bits(type_entry);3992 return type_has_bits(type_entry);
4681 case ZigTypeIdErrorUnion:3993 case ZigTypeIdErrorUnion:
4682 return type_has_bits(type_entry->data.error_union.payload_type);3994 return type_has_bits(type_entry->data.error_union.payload_type);
...@@ -4684,13 +3996,6 @@ bool handle_is_ptr(ZigType *type_entry) {...@@ -4684,13 +3996,6 @@ bool handle_is_ptr(ZigType *type_entry) {
4684 return type_has_bits(type_entry->data.maybe.child_type) &&3996 return type_has_bits(type_entry->data.maybe.child_type) &&
4685 !type_is_nonnull_ptr(type_entry->data.maybe.child_type) &&3997 !type_is_nonnull_ptr(type_entry->data.maybe.child_type) &&
4686 type_entry->data.maybe.child_type->id != ZigTypeIdErrorSet;3998 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
4695 }4000 }
4696 zig_unreachable();4001 zig_unreachable();
...@@ -5165,10 +4470,10 @@ bool fn_eval_eql(Scope *a, Scope *b) {...@@ -5165,10 +4470,10 @@ bool fn_eval_eql(Scope *a, Scope *b) {
51654470
5166// Whether the type has bits at runtime.4471// Whether the type has bits at runtime.
5167bool type_has_bits(ZigType *type_entry) {4472bool type_has_bits(ZigType *type_entry) {
5168 assert(type_entry);4473 assert(type_entry != nullptr);
5169 assert(!type_is_invalid(type_entry));4474 assert(!type_is_invalid(type_entry));
5170 assert(type_is_resolved(type_entry, ResolveStatusZeroBitsKnown));4475 assert(type_is_resolved(type_entry, ResolveStatusZeroBitsKnown));
5171 return !type_entry->zero_bits;4476 return type_entry->abi_size != 0;
5172}4477}
51734478
5174// Whether you can infer the value based solely on the type.4479// Whether you can infer the value based solely on the type.
...@@ -5629,7 +4934,7 @@ Error type_resolve(CodeGen *g, ZigType *ty, ResolveStatus status) {...@@ -5629,7 +4934,7 @@ Error type_resolve(CodeGen *g, ZigType *ty, ResolveStatus status) {
5629 } else if (ty->id == ZigTypeIdEnum) {4934 } else if (ty->id == ZigTypeIdEnum) {
5630 return resolve_enum_zero_bits(g, ty);4935 return resolve_enum_zero_bits(g, ty);
5631 } else if (ty->id == ZigTypeIdUnion) {4936 } else if (ty->id == ZigTypeIdUnion) {
5632 return resolve_union_zero_bits(g, ty);4937 return resolve_union_alignment(g, ty);
5633 }4938 }
5634 return ErrorNone;4939 return ErrorNone;
5635 case ResolveStatusSizeKnown:4940 case ResolveStatusSizeKnown:
...@@ -6192,31 +5497,18 @@ void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val) {...@@ -6192,31 +5497,18 @@ void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val) {
6192ZigType *make_int_type(CodeGen *g, bool is_signed, uint32_t size_in_bits) {5497ZigType *make_int_type(CodeGen *g, bool is_signed, uint32_t size_in_bits) {
6193 assert(size_in_bits <= 65535);5498 assert(size_in_bits <= 65535);
6194 ZigType *entry = new_type_table_entry(ZigTypeIdInt);5499 ZigType *entry = new_type_table_entry(ZigTypeIdInt);
6195 entry->type_ref = (size_in_bits == 0) ? LLVMVoidType() : LLVMIntType(size_in_bits);5500
6196 entry->zero_bits = (size_in_bits == 0);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
6198 const char u_or_i = is_signed ? 'i' : 'u';5508 const char u_or_i = is_signed ? 'i' : 'u';
6199 buf_resize(&entry->name, 0);5509 buf_resize(&entry->name, 0);
6200 buf_appendf(&entry->name, "%c%" PRIu32, u_or_i, size_in_bits);5510 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);
6220 entry->data.integral.is_signed = is_signed;5512 entry->data.integral.is_signed = is_signed;
6221 entry->data.integral.bit_count = size_in_bits;5513 entry->data.integral.bit_count = size_in_bits;
6222 return entry;5514 return entry;
...@@ -6620,26 +5912,6 @@ LinkLib *add_link_lib(CodeGen *g, Buf *name) {...@@ -6620,26 +5912,6 @@ LinkLib *add_link_lib(CodeGen *g, Buf *name) {
6620 return link_lib;5912 return link_lib;
6621}5913}
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
6643ZigType *get_align_amt_type(CodeGen *g) {5915ZigType *get_align_amt_type(CodeGen *g) {
6644 if (g->align_amt_type == nullptr) {5916 if (g->align_amt_type == nullptr) {
6645 // according to LLVM the maximum alignment is 1 << 29.5917 // according to LLVM the maximum alignment is 1 << 29.
...@@ -6841,11 +6113,8 @@ bool type_is_c_abi_int(CodeGen *g, ZigType *ty) {...@@ -6841,11 +6113,8 @@ bool type_is_c_abi_int(CodeGen *g, ZigType *ty) {
68416113
6842uint32_t get_host_int_bytes(CodeGen *g, ZigType *struct_type, TypeStructField *field) {6114uint32_t get_host_int_bytes(CodeGen *g, ZigType *struct_type, TypeStructField *field) {
6843 assert(struct_type->id == ZigTypeIdStruct);6115 assert(struct_type->id == ZigTypeIdStruct);
6844 if (struct_type->data.structure.layout != ContainerLayoutPacked) {6116 assert(type_is_resolved(struct_type, ResolveStatusSizeKnown));
6845 return 0;6117 return struct_type->data.structure.host_int_bytes[field->gen_index];
6846 }
6847 LLVMTypeRef field_type = LLVMStructGetTypeAtIndex(struct_type->type_ref, field->gen_index);
6848 return LLVMStoreSizeOfType(g->target_data_ref, field_type);
6849}6118}
68506119
6851Error ensure_const_val_repr(IrAnalyze *ira, CodeGen *codegen, AstNode *source_node,6120Error ensure_const_val_repr(IrAnalyze *ira, CodeGen *codegen, AstNode *source_node,
...@@ -6911,3 +6180,822 @@ Buf *type_bare_name(ZigType *type_entry) {...@@ -6911,3 +6180,822 @@ Buf *type_bare_name(ZigType *type_entry) {
6911Buf *type_h_name(ZigType *t) {6180Buf *type_h_name(ZigType *t) {
6912 return type_bare_name(t);6181 return type_bare_name(t);
6913}6182}
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);...@@ -244,4 +244,6 @@ Buf *type_bare_name(ZigType *t);
244Buf *type_h_name(ZigType *t);244Buf *type_h_name(ZigType *t);
245Error create_c_object_cache(CodeGen *g, CacheHash **out_cache_hash, bool verbose);245Error 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);
247#endif249#endif
src/codegen.cpp+318-331
...@@ -668,7 +668,7 @@ static ZigLLVMDIScope *get_di_scope(CodeGen *g, Scope *scope) {...@@ -668,7 +668,7 @@ static ZigLLVMDIScope *get_di_scope(CodeGen *g, Scope *scope) {
668 if (scope->parent) {668 if (scope->parent) {
669 ScopeDecls *decls_scope = (ScopeDecls *)scope;669 ScopeDecls *decls_scope = (ScopeDecls *)scope;
670 assert(decls_scope->container_type);670 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));
672 } else {672 } else {
673 scope->di_scope = ZigLLVMFileToScope(import->data.structure.root_struct->di_file);673 scope->di_scope = ZigLLVMFileToScope(import->data.structure.root_struct->di_file);
674 }674 }
...@@ -711,8 +711,8 @@ static LLVMValueRef get_arithmetic_overflow_fn(CodeGen *g, ZigType *operand_type...@@ -711,8 +711,8 @@ static LLVMValueRef get_arithmetic_overflow_fn(CodeGen *g, ZigType *operand_type
711 const char *signed_str = int_type->data.integral.is_signed ? signed_name : unsigned_name;711 const char *signed_str = int_type->data.integral.is_signed ? signed_name : unsigned_name;
712712
713 LLVMTypeRef param_types[] = {713 LLVMTypeRef param_types[] = {
714 operand_type->type_ref,714 get_llvm_type(g, operand_type),
715 operand_type->type_ref,715 get_llvm_type(g, operand_type),
716 };716 };
717717
718 if (operand_type->id == ZigTypeIdVector) {718 if (operand_type->id == ZigTypeIdVector) {
...@@ -720,7 +720,7 @@ static LLVMValueRef get_arithmetic_overflow_fn(CodeGen *g, ZigType *operand_type...@@ -720,7 +720,7 @@ static LLVMValueRef get_arithmetic_overflow_fn(CodeGen *g, ZigType *operand_type
720 operand_type->data.vector.len, int_type->data.integral.bit_count);720 operand_type->data.vector.len, int_type->data.integral.bit_count);
721721
722 LLVMTypeRef return_elem_types[] = {722 LLVMTypeRef return_elem_types[] = {
723 operand_type->type_ref,723 get_llvm_type(g, operand_type),
724 LLVMVectorType(LLVMInt1Type(), operand_type->data.vector.len),724 LLVMVectorType(LLVMInt1Type(), operand_type->data.vector.len),
725 };725 };
726 LLVMTypeRef return_struct_type = LLVMStructType(return_elem_types, 2, false);726 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...@@ -732,7 +732,7 @@ static LLVMValueRef get_arithmetic_overflow_fn(CodeGen *g, ZigType *operand_type
732 sprintf(fn_name, "llvm.%s.with.overflow.i%" PRIu32, signed_str, int_type->data.integral.bit_count);732 sprintf(fn_name, "llvm.%s.with.overflow.i%" PRIu32, signed_str, int_type->data.integral.bit_count);
733733
734 LLVMTypeRef return_elem_types[] = {734 LLVMTypeRef return_elem_types[] = {
735 operand_type->type_ref,735 get_llvm_type(g, operand_type),
736 LLVMInt1Type(),736 LLVMInt1Type(),
737 };737 };
738 LLVMTypeRef return_struct_type = LLVMStructType(return_elem_types, 2, false);738 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...@@ -800,7 +800,8 @@ static LLVMValueRef get_float_fn(CodeGen *g, ZigType *type_entry, ZigLLVMFnId fn
800800
801 char fn_name[64];801 char fn_name[64];
802 sprintf(fn_name, "llvm.%s.f%" ZIG_PRI_usize "", name, type_entry->data.floating.bit_count);802 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);
804 LLVMValueRef fn_val = LLVMAddFunction(g->module, fn_name, fn_type);805 LLVMValueRef fn_val = LLVMAddFunction(g->module, fn_name, fn_type);
805 assert(LLVMGetIntrinsicID(fn_val));806 assert(LLVMGetIntrinsicID(fn_val));
806807
...@@ -958,7 +959,7 @@ static LLVMValueRef get_panic_msg_ptr_val(CodeGen *g, PanicMsgId msg_id) {...@@ -958,7 +959,7 @@ static LLVMValueRef get_panic_msg_ptr_val(CodeGen *g, PanicMsgId msg_id) {
958 ZigType *u8_ptr_type = get_pointer_to_type_extra(g, g->builtin_types.entry_u8, true, false,959 ZigType *u8_ptr_type = get_pointer_to_type_extra(g, g->builtin_types.entry_u8, true, false,
959 PtrLenUnknown, get_abi_alignment(g, g->builtin_types.entry_u8), 0, 0, false);960 PtrLenUnknown, get_abi_alignment(g, g->builtin_types.entry_u8), 0, 0, false);
960 ZigType *str_type = get_slice_type(g, u8_ptr_type);961 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));
962}963}
963964
964static void gen_panic(CodeGen *g, LLVMValueRef msg_arg, LLVMValueRef stack_trace_arg) {965static 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...@@ -967,7 +968,7 @@ static void gen_panic(CodeGen *g, LLVMValueRef msg_arg, LLVMValueRef stack_trace
967 LLVMCallConv llvm_cc = get_llvm_cc(g, g->panic_fn->type_entry->data.fn.fn_type_id.cc);968 LLVMCallConv llvm_cc = get_llvm_cc(g, g->panic_fn->type_entry->data.fn.fn_type_id.cc);
968 if (stack_trace_arg == nullptr) {969 if (stack_trace_arg == nullptr) {
969 ZigType *ptr_to_stack_trace_type = get_ptr_to_stack_trace_type(g);970 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));
971 }972 }
972 LLVMValueRef args[] = {973 LLVMValueRef args[] = {
973 msg_arg,974 msg_arg,
...@@ -1081,7 +1082,7 @@ static LLVMValueRef get_coro_size_fn_val(CodeGen *g) {...@@ -1081,7 +1082,7 @@ static LLVMValueRef get_coro_size_fn_val(CodeGen *g) {
1081 if (g->coro_size_fn_val)1082 if (g->coro_size_fn_val)
1082 return g->coro_size_fn_val;1083 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);
1085 Buf *name = buf_sprintf("llvm.coro.size.i%d", g->pointer_size_bytes * 8);1086 Buf *name = buf_sprintf("llvm.coro.size.i%d", g->pointer_size_bytes * 8);
1086 g->coro_size_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type);1087 g->coro_size_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type);
1087 assert(LLVMGetIntrinsicID(g->coro_size_fn_val));1088 assert(LLVMGetIntrinsicID(g->coro_size_fn_val));
...@@ -1206,8 +1207,8 @@ static LLVMValueRef get_return_address_fn_val(CodeGen *g) {...@@ -1206,8 +1207,8 @@ static LLVMValueRef get_return_address_fn_val(CodeGen *g) {
12061207
1207 ZigType *return_type = get_pointer_to_type(g, g->builtin_types.entry_u8, true);1208 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 LLVMTypeRef fn_type = LLVMFunctionType(get_llvm_type(g, return_type),
1210 &g->builtin_types.entry_i32->type_ref, 1, false);1211 &g->builtin_types.entry_i32->llvm_type, 1, false);
1211 g->return_address_fn_val = LLVMAddFunction(g->module, "llvm.returnaddress", fn_type);1212 g->return_address_fn_val = LLVMAddFunction(g->module, "llvm.returnaddress", fn_type);
1212 assert(LLVMGetIntrinsicID(g->return_address_fn_val));1213 assert(LLVMGetIntrinsicID(g->return_address_fn_val));
12131214
...@@ -1219,8 +1220,8 @@ static LLVMValueRef get_add_error_return_trace_addr_fn(CodeGen *g) {...@@ -1219,8 +1220,8 @@ static LLVMValueRef get_add_error_return_trace_addr_fn(CodeGen *g) {
1219 return g->add_error_return_trace_addr_fn_val;1220 return g->add_error_return_trace_addr_fn_val;
12201221
1221 LLVMTypeRef arg_types[] = {1222 LLVMTypeRef arg_types[] = {
1222 get_ptr_to_stack_trace_type(g)->type_ref,1223 get_llvm_type(g, get_ptr_to_stack_trace_type(g)),
1223 g->builtin_types.entry_usize->type_ref,1224 g->builtin_types.entry_usize->llvm_type,
1224 };1225 };
1225 LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMVoidType(), arg_types, 2, false);1226 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) {...@@ -1245,7 +1246,7 @@ static LLVMValueRef get_add_error_return_trace_addr_fn(CodeGen *g) {
1245 LLVMPositionBuilderAtEnd(g->builder, entry_block);1246 LLVMPositionBuilderAtEnd(g->builder, entry_block);
1246 ZigLLVMClearCurrentDebugLocation(g->builder);1247 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
1250 // stack_trace.instruction_addresses[stack_trace.index & (stack_trace.instruction_addresses.len - 1)] = return_address;1251 // 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) {...@@ -1297,8 +1298,8 @@ static LLVMValueRef get_merge_err_ret_traces_fn_val(CodeGen *g) {
1297 assert(g->stack_trace_type != nullptr);1298 assert(g->stack_trace_type != nullptr);
12981299
1299 LLVMTypeRef param_types[] = {1300 LLVMTypeRef param_types[] = {
1300 get_ptr_to_stack_trace_type(g)->type_ref,1301 get_llvm_type(g, get_ptr_to_stack_trace_type(g)),
1301 get_ptr_to_stack_trace_type(g)->type_ref,1302 get_llvm_type(g, get_ptr_to_stack_trace_type(g)),
1302 };1303 };
1303 LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMVoidType(), param_types, 2, false);1304 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) {...@@ -1350,8 +1351,8 @@ static LLVMValueRef get_merge_err_ret_traces_fn_val(CodeGen *g) {
1350 // }1351 // }
1351 LLVMBasicBlockRef return_block = LLVMAppendBasicBlock(fn_val, "Return");1352 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 frame_index_ptr = LLVMBuildAlloca(g->builder, g->builtin_types.entry_usize->llvm_type, "frame_index");
1354 LLVMValueRef frames_left_ptr = LLVMBuildAlloca(g->builder, g->builtin_types.entry_usize->type_ref, "frames_left");1355 LLVMValueRef frames_left_ptr = LLVMBuildAlloca(g->builder, g->builtin_types.entry_usize->llvm_type, "frames_left");
13551356
1356 LLVMValueRef dest_stack_trace_ptr = LLVMGetParam(fn_val, 0);1357 LLVMValueRef dest_stack_trace_ptr = LLVMGetParam(fn_val, 0);
1357 LLVMValueRef src_stack_trace_ptr = LLVMGetParam(fn_val, 1);1358 LLVMValueRef src_stack_trace_ptr = LLVMGetParam(fn_val, 1);
...@@ -1377,14 +1378,14 @@ static LLVMValueRef get_merge_err_ret_traces_fn_val(CodeGen *g) {...@@ -1377,14 +1378,14 @@ static LLVMValueRef get_merge_err_ret_traces_fn_val(CodeGen *g) {
1377 LLVMBuildCondBr(g->builder, no_wrap_bit, no_wrap_block, yes_wrap_block);1378 LLVMBuildCondBr(g->builder, no_wrap_bit, no_wrap_block, yes_wrap_block);
13781379
1379 LLVMPositionBuilderAtEnd(g->builder, no_wrap_block);1380 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);
1381 LLVMBuildStore(g->builder, usize_zero, frame_index_ptr);1382 LLVMBuildStore(g->builder, usize_zero, frame_index_ptr);
1382 LLVMBuildStore(g->builder, src_index_val, frames_left_ptr);1383 LLVMBuildStore(g->builder, src_index_val, frames_left_ptr);
1383 LLVMValueRef frames_left_eq_zero_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, src_index_val, usize_zero, "");1384 LLVMValueRef frames_left_eq_zero_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, src_index_val, usize_zero, "");
1384 LLVMBuildCondBr(g->builder, frames_left_eq_zero_bit, return_block, loop_block);1385 LLVMBuildCondBr(g->builder, frames_left_eq_zero_bit, return_block, loop_block);
13851386
1386 LLVMPositionBuilderAtEnd(g->builder, yes_wrap_block);1387 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);
1388 LLVMValueRef plus_one = LLVMBuildNUWAdd(g->builder, src_index_val, usize_one, "");1389 LLVMValueRef plus_one = LLVMBuildNUWAdd(g->builder, src_index_val, usize_one, "");
1389 LLVMValueRef mod_len = LLVMBuildURem(g->builder, plus_one, src_len_val, "");1390 LLVMValueRef mod_len = LLVMBuildURem(g->builder, plus_one, src_len_val, "");
1390 LLVMBuildStore(g->builder, mod_len, frame_index_ptr);1391 LLVMBuildStore(g->builder, mod_len, frame_index_ptr);
...@@ -1430,7 +1431,7 @@ static LLVMValueRef get_return_err_fn(CodeGen *g) {...@@ -1430,7 +1431,7 @@ static LLVMValueRef get_return_err_fn(CodeGen *g) {
14301431
1431 LLVMTypeRef arg_types[] = {1432 LLVMTypeRef arg_types[] = {
1432 // error return trace pointer1433 // 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)),
1434 };1435 };
1435 LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMVoidType(), arg_types, 1, false);1436 LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMVoidType(), arg_types, 1, false);
14361437
...@@ -1461,8 +1462,8 @@ static LLVMValueRef get_return_err_fn(CodeGen *g) {...@@ -1461,8 +1462,8 @@ static LLVMValueRef get_return_err_fn(CodeGen *g) {
14611462
1462 LLVMValueRef err_ret_trace_ptr = LLVMGetParam(fn_val, 0);1463 LLVMValueRef err_ret_trace_ptr = LLVMGetParam(fn_val, 0);
14631464
1464 LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->type_ref;1465 LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->llvm_type;
1465 LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_i32->type_ref);1466 LLVMValueRef zero = LLVMConstNull(get_llvm_type(g, g->builtin_types.entry_i32));
1466 LLVMValueRef return_address_ptr = LLVMBuildCall(g->builder, get_return_address_fn_val(g), &zero, 1, "");1467 LLVMValueRef return_address_ptr = LLVMBuildCall(g->builder, get_return_address_fn_val(g), &zero, 1, "");
1467 LLVMValueRef return_address = LLVMBuildPtrToInt(g->builder, return_address_ptr, usize_type_ref, "");1468 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) {...@@ -1508,8 +1509,8 @@ static LLVMValueRef get_safety_crash_err_fn(CodeGen *g) {
15081509
1509 ZigType *usize = g->builtin_types.entry_usize;1510 ZigType *usize = g->builtin_types.entry_usize;
1510 LLVMValueRef full_buf_ptr_indices[] = {1511 LLVMValueRef full_buf_ptr_indices[] = {
1511 LLVMConstNull(usize->type_ref),1512 LLVMConstNull(usize->llvm_type),
1512 LLVMConstNull(usize->type_ref),1513 LLVMConstNull(usize->llvm_type),
1513 };1514 };
1514 LLVMValueRef full_buf_ptr = LLVMConstInBoundsGEP(global_array, full_buf_ptr_indices, 2);1515 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) {...@@ -1519,9 +1520,9 @@ static LLVMValueRef get_safety_crash_err_fn(CodeGen *g) {
1519 ZigType *str_type = get_slice_type(g, u8_ptr_type);1520 ZigType *str_type = get_slice_type(g, u8_ptr_type);
1520 LLVMValueRef global_slice_fields[] = {1521 LLVMValueRef global_slice_fields[] = {
1521 full_buf_ptr,1522 full_buf_ptr,
1522 LLVMConstNull(usize->type_ref),1523 LLVMConstNull(usize->llvm_type),
1523 };1524 };
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);
1525 LLVMValueRef global_slice = LLVMAddGlobal(g->module, LLVMTypeOf(slice_init_value), "");1526 LLVMValueRef global_slice = LLVMAddGlobal(g->module, LLVMTypeOf(slice_init_value), "");
1526 LLVMSetInitializer(global_slice, slice_init_value);1527 LLVMSetInitializer(global_slice, slice_init_value);
1527 LLVMSetLinkage(global_slice, LLVMInternalLinkage);1528 LLVMSetLinkage(global_slice, LLVMInternalLinkage);
...@@ -1530,15 +1531,15 @@ static LLVMValueRef get_safety_crash_err_fn(CodeGen *g) {...@@ -1530,15 +1531,15 @@ static LLVMValueRef get_safety_crash_err_fn(CodeGen *g) {
1530 LLVMSetAlignment(global_slice, get_abi_alignment(g, str_type));1531 LLVMSetAlignment(global_slice, get_abi_alignment(g, str_type));
15311532
1532 LLVMValueRef offset_ptr_indices[] = {1533 LLVMValueRef offset_ptr_indices[] = {
1533 LLVMConstNull(usize->type_ref),1534 LLVMConstNull(usize->llvm_type),
1534 LLVMConstInt(usize->type_ref, unwrap_err_msg_text_len, false),1535 LLVMConstInt(usize->llvm_type, unwrap_err_msg_text_len, false),
1535 };1536 };
1536 LLVMValueRef offset_buf_ptr = LLVMConstInBoundsGEP(global_array, offset_ptr_indices, 2);1537 LLVMValueRef offset_buf_ptr = LLVMConstInBoundsGEP(global_array, offset_ptr_indices, 2);
15371538
1538 Buf *fn_name = get_mangled_name(g, buf_create_from_str("__zig_fail_unwrap"), false);1539 Buf *fn_name = get_mangled_name(g, buf_create_from_str("__zig_fail_unwrap"), false);
1539 LLVMTypeRef arg_types[] = {1540 LLVMTypeRef arg_types[] = {
1540 g->ptr_to_stack_trace_type->type_ref,1541 get_llvm_type(g, g->ptr_to_stack_trace_type),
1541 g->err_tag_type->type_ref,1542 get_llvm_type(g, g->err_tag_type),
1542 };1543 };
1543 LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMVoidType(), arg_types, 2, false);1544 LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMVoidType(), arg_types, 2, false);
1544 LLVMValueRef fn_val = LLVMAddFunction(g->module, buf_ptr(fn_name), fn_type_ref);1545 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) {...@@ -1565,7 +1566,7 @@ static LLVMValueRef get_safety_crash_err_fn(CodeGen *g) {
1565 LLVMValueRef err_val = LLVMGetParam(fn_val, 1);1566 LLVMValueRef err_val = LLVMGetParam(fn_val, 1);
15661567
1567 LLVMValueRef err_table_indices[] = {1568 LLVMValueRef err_table_indices[] = {
1568 LLVMConstNull(g->builtin_types.entry_usize->type_ref),1569 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),
1569 err_val,1570 err_val,
1570 };1571 };
1571 LLVMValueRef err_name_val = LLVMBuildInBoundsGEP(g->builder, g->err_name_table, err_table_indices, 2, "");1572 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...@@ -1623,7 +1624,7 @@ static void gen_safety_crash_for_err(CodeGen *g, LLVMValueRef err_val, Scope *sc
1623 LLVMValueRef err_ret_trace_val = get_cur_err_ret_trace_val(g, scope);1624 LLVMValueRef err_ret_trace_val = get_cur_err_ret_trace_val(g, scope);
1624 if (err_ret_trace_val == nullptr) {1625 if (err_ret_trace_val == nullptr) {
1625 ZigType *ptr_to_stack_trace_type = get_ptr_to_stack_trace_type(g);1626 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));
1627 }1628 }
1628 LLVMValueRef args[] = {1629 LLVMValueRef args[] = {
1629 err_ret_trace_val,1630 err_ret_trace_val,
...@@ -1669,7 +1670,7 @@ static void add_bounds_check(CodeGen *g, LLVMValueRef target_val,...@@ -1669,7 +1670,7 @@ static void add_bounds_check(CodeGen *g, LLVMValueRef target_val,
1669}1670}
16701671
1671static LLVMValueRef gen_assert_zero(CodeGen *g, LLVMValueRef expr_val, ZigType *int_type) {1672static 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));
1673 LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, expr_val, zero, "");1674 LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, expr_val, zero, "");
1674 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "CastShortenOk");1675 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "CastShortenOk");
1675 LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn_val, "CastShortenFail");1676 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...@@ -1704,7 +1705,7 @@ static LLVMValueRef gen_widen_or_shorten(CodeGen *g, bool want_runtime_safety, Z
1704 !wanted_type->data.integral.is_signed && actual_type->data.integral.is_signed &&1705 !wanted_type->data.integral.is_signed && actual_type->data.integral.is_signed &&
1705 want_runtime_safety)1706 want_runtime_safety)
1706 {1707 {
1707 LLVMValueRef zero = LLVMConstNull(actual_type->type_ref);1708 LLVMValueRef zero = LLVMConstNull(get_llvm_type(g, actual_type));
1708 LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntSGE, expr_val, zero, "");1709 LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntSGE, expr_val, zero, "");
17091710
1710 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "SignCastOk");1711 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...@@ -1721,19 +1722,19 @@ static LLVMValueRef gen_widen_or_shorten(CodeGen *g, bool want_runtime_safety, Z
1721 return expr_val;1722 return expr_val;
1722 } else if (actual_bits < wanted_bits) {1723 } else if (actual_bits < wanted_bits) {
1723 if (actual_type->id == ZigTypeIdFloat) {1724 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), "");
1725 } else if (actual_type->id == ZigTypeIdInt) {1726 } else if (actual_type->id == ZigTypeIdInt) {
1726 if (actual_type->data.integral.is_signed) {1727 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), "");
1728 } else {1729 } 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), "");
1730 }1731 }
1731 } else {1732 } else {
1732 zig_unreachable();1733 zig_unreachable();
1733 }1734 }
1734 } else if (actual_bits > wanted_bits) {1735 } else if (actual_bits > wanted_bits) {
1735 if (actual_type->id == ZigTypeIdFloat) {1736 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), "");
1737 } else if (actual_type->id == ZigTypeIdInt) {1738 } else if (actual_type->id == ZigTypeIdInt) {
1738 if (wanted_bits == 0) {1739 if (wanted_bits == 0) {
1739 if (!want_runtime_safety)1740 if (!want_runtime_safety)
...@@ -1741,15 +1742,15 @@ static LLVMValueRef gen_widen_or_shorten(CodeGen *g, bool want_runtime_safety, Z...@@ -1741,15 +1742,15 @@ static LLVMValueRef gen_widen_or_shorten(CodeGen *g, bool want_runtime_safety, Z
17411742
1742 return gen_assert_zero(g, expr_val, actual_type);1743 return gen_assert_zero(g, expr_val, actual_type);
1743 }1744 }
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), "");
1745 if (!want_runtime_safety) {1746 if (!want_runtime_safety) {
1746 return trunc_val;1747 return trunc_val;
1747 }1748 }
1748 LLVMValueRef orig_val;1749 LLVMValueRef orig_val;
1749 if (wanted_type->data.integral.is_signed) {1750 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), "");
1751 } else {1752 } 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), "");
1753 }1754 }
1754 LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, expr_val, orig_val, "");1755 LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, expr_val, orig_val, "");
1755 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "CastShortenOk");1756 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "CastShortenOk");
...@@ -1793,7 +1794,7 @@ static LLVMValueRef gen_overflow_op(CodeGen *g, ZigType *operand_type, AddSubMul...@@ -1793,7 +1794,7 @@ static LLVMValueRef gen_overflow_op(CodeGen *g, ZigType *operand_type, AddSubMul
1793 LLVMValueRef extended1 = buildExtFn(g->builder, val1, one_more_bit_int_vector, "");1794 LLVMValueRef extended1 = buildExtFn(g->builder, val1, one_more_bit_int_vector, "");
1794 LLVMValueRef extended2 = buildExtFn(g->builder, val2, one_more_bit_int_vector, "");1795 LLVMValueRef extended2 = buildExtFn(g->builder, val2, one_more_bit_int_vector, "");
1795 LLVMValueRef extended_result = wrap_op[op](g->builder, extended1, extended2, "");1796 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
1798 LLVMValueRef re_extended_result = buildExtFn(g->builder, result, one_more_bit_int_vector, "");1799 LLVMValueRef re_extended_result = buildExtFn(g->builder, result, one_more_bit_int_vector, "");
1799 LLVMValueRef overflow_vector = LLVMBuildICmp(g->builder, LLVMIntNE, extended_result, re_extended_result, "");1800 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...@@ -1880,12 +1881,13 @@ static LLVMValueRef gen_assign_raw(CodeGen *g, LLVMValueRef ptr, ZigType *ptr_ty
1880 LLVMValueRef dest_ptr = LLVMBuildBitCast(g->builder, ptr, ptr_u8, "");1881 LLVMValueRef dest_ptr = LLVMBuildBitCast(g->builder, ptr, ptr_u8, "");
18811882
1882 ZigType *usize = g->builtin_types.entry_usize;1883 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));
1884 uint64_t align_bytes = get_ptr_align(g, ptr_type);1885 uint64_t align_bytes = get_ptr_align(g, ptr_type);
1885 assert(size_bytes > 0);1886 assert(size_bytes > 0);
1886 assert(align_bytes > 0);1887 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),
1889 ptr_type->data.pointer.is_volatile);1891 ptr_type->data.pointer.is_volatile);
1890 return nullptr;1892 return nullptr;
1891 }1893 }
...@@ -1907,7 +1909,7 @@ static LLVMValueRef gen_assign_raw(CodeGen *g, LLVMValueRef ptr, ZigType *ptr_ty...@@ -1907,7 +1909,7 @@ static LLVMValueRef gen_assign_raw(CodeGen *g, LLVMValueRef ptr, ZigType *ptr_ty
1907 uint32_t shift_amt = big_endian ? host_bit_count - bit_offset - size_in_bits : bit_offset;1909 uint32_t shift_amt = big_endian ? host_bit_count - bit_offset - size_in_bits : bit_offset;
1908 LLVMValueRef shift_amt_val = LLVMConstInt(LLVMTypeOf(containing_int), shift_amt, false);1910 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));
1911 mask_val = LLVMConstZExt(mask_val, LLVMTypeOf(containing_int));1913 mask_val = LLVMConstZExt(mask_val, LLVMTypeOf(containing_int));
1912 mask_val = LLVMConstShl(mask_val, shift_amt_val);1914 mask_val = LLVMConstShl(mask_val, shift_amt_val);
1913 mask_val = LLVMConstNot(mask_val);1915 mask_val = LLVMConstNot(mask_val);
...@@ -1942,9 +1944,10 @@ static LLVMValueRef ir_llvm_value(CodeGen *g, IrInstruction *instruction) {...@@ -1942,9 +1944,10 @@ static LLVMValueRef ir_llvm_value(CodeGen *g, IrInstruction *instruction) {
1942 if (handle_is_ptr(instruction->value.type)) {1944 if (handle_is_ptr(instruction->value.type)) {
1943 render_const_val_global(g, &instruction->value, "");1945 render_const_val_global(g, &instruction->value, "");
1944 ZigType *ptr_type = get_pointer_to_type(g, instruction->value.type, true);1946 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), "");
1946 } else if (instruction->value.type->id == ZigTypeIdPointer) {1948 } 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), "");
1948 } else {1951 } else {
1949 instruction->llvm_value = instruction->value.global_refs->llvm_value;1952 instruction->llvm_value = instruction->value.global_refs->llvm_value;
1950 }1953 }
...@@ -1979,7 +1982,7 @@ static void give_up_with_c_abi_error(CodeGen *g, AstNode *source_node) {...@@ -1979,7 +1982,7 @@ static void give_up_with_c_abi_error(CodeGen *g, AstNode *source_node) {
1979}1982}
19801983
1981static LLVMValueRef build_alloca(CodeGen *g, ZigType *type_entry, const char *name, uint32_t alignment) {1984static 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);
1983 LLVMSetAlignment(result, (alignment == 0) ? get_abi_alignment(g, type_entry) : alignment);1986 LLVMSetAlignment(result, (alignment == 0) ? get_abi_alignment(g, type_entry) : alignment);
1984 return result;1987 return result;
1985}1988}
...@@ -2062,8 +2065,8 @@ static bool iter_function_params_c_abi(CodeGen *g, ZigType *fn_type, FnWalk *fn_...@@ -2062,8 +2065,8 @@ static bool iter_function_params_c_abi(CodeGen *g, ZigType *fn_type, FnWalk *fn_
2062 fn_walk->data.call.gen_param_values->append(val);2065 fn_walk->data.call.gen_param_values->append(val);
2063 break;2066 break;
2064 case FnWalkIdTypes:2067 case FnWalkIdTypes:
2065 fn_walk->data.types.gen_param_types->append(ty->type_ref);2068 fn_walk->data.types.gen_param_types->append(get_llvm_type(g, ty));
2066 fn_walk->data.types.param_di_types->append(ty->di_type);2069 fn_walk->data.types.param_di_types->append(get_llvm_di_type(g, ty));
2067 break;2070 break;
2068 case FnWalkIdVars: {2071 case FnWalkIdVars: {
2069 var->value_ref = build_alloca(g, ty, buf_ptr(&var->name), var->align_bytes);2072 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_...@@ -2099,8 +2102,8 @@ static bool iter_function_params_c_abi(CodeGen *g, ZigType *fn_type, FnWalk *fn_
2099 break;2102 break;
2100 case FnWalkIdTypes: {2103 case FnWalkIdTypes: {
2101 ZigType *gen_type = get_pointer_to_type(g, ty, true);2104 ZigType *gen_type = get_pointer_to_type(g, ty, true);
2102 fn_walk->data.types.gen_param_types->append(gen_type->type_ref);2105 fn_walk->data.types.gen_param_types->append(get_llvm_type(g, gen_type));
2103 fn_walk->data.types.param_di_types->append(gen_type->di_type);2106 fn_walk->data.types.param_di_types->append(get_llvm_di_type(g, gen_type));
2104 break;2107 break;
2105 }2108 }
2106 case FnWalkIdVars: {2109 case FnWalkIdVars: {
...@@ -2138,8 +2141,8 @@ static bool iter_function_params_c_abi(CodeGen *g, ZigType *fn_type, FnWalk *fn_...@@ -2138,8 +2141,8 @@ static bool iter_function_params_c_abi(CodeGen *g, ZigType *fn_type, FnWalk *fn_
2138 break;2141 break;
2139 case FnWalkIdTypes: {2142 case FnWalkIdTypes: {
2140 ZigType *gen_type = get_pointer_to_type(g, ty, true);2143 ZigType *gen_type = get_pointer_to_type(g, ty, true);
2141 fn_walk->data.types.gen_param_types->append(gen_type->type_ref);2144 fn_walk->data.types.gen_param_types->append(get_llvm_type(g, gen_type));
2142 fn_walk->data.types.param_di_types->append(gen_type->di_type);2145 fn_walk->data.types.param_di_types->append(get_llvm_di_type(g, gen_type));
2143 break;2146 break;
2144 }2147 }
2145 case FnWalkIdVars: {2148 case FnWalkIdVars: {
...@@ -2171,8 +2174,8 @@ static bool iter_function_params_c_abi(CodeGen *g, ZigType *fn_type, FnWalk *fn_...@@ -2171,8 +2174,8 @@ static bool iter_function_params_c_abi(CodeGen *g, ZigType *fn_type, FnWalk *fn_
2171 }2174 }
2172 case FnWalkIdTypes: {2175 case FnWalkIdTypes: {
2173 ZigType *gen_type = get_int_type(g, false, ty_size * 8);2176 ZigType *gen_type = get_int_type(g, false, ty_size * 8);
2174 fn_walk->data.types.gen_param_types->append(gen_type->type_ref);2177 fn_walk->data.types.gen_param_types->append(get_llvm_type(g, gen_type));
2175 fn_walk->data.types.param_di_types->append(gen_type->di_type);2178 fn_walk->data.types.param_di_types->append(get_llvm_di_type(g, gen_type));
2176 break;2179 break;
2177 }2180 }
2178 case FnWalkIdVars: {2181 case FnWalkIdVars: {
...@@ -2210,7 +2213,7 @@ var_ok:...@@ -2210,7 +2213,7 @@ var_ok:
2210 var->di_loc_var = ZigLLVMCreateParameterVariable(g->dbuilder, get_di_scope(g, var->parent_scope),2213 var->di_loc_var = ZigLLVMCreateParameterVariable(g->dbuilder, get_di_scope(g, var->parent_scope),
2211 buf_ptr(&var->name), fn_walk->data.vars.import->data.structure.root_struct->di_file,2214 buf_ptr(&var->name), fn_walk->data.vars.import->data.structure.root_struct->di_file,
2212 (unsigned)(var->decl_node->line + 1),2215 (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);
2214 }2217 }
2215 return true;2218 return true;
2216}2219}
...@@ -2436,7 +2439,7 @@ static LLVMValueRef gen_div(CodeGen *g, bool want_runtime_safety, bool want_fast...@@ -2436,7 +2439,7 @@ static LLVMValueRef gen_div(CodeGen *g, bool want_runtime_safety, bool want_fast
2436{2439{
2437 ZigLLVMSetFastMath(g->builder, want_fast_math);2440 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));
2440 if (want_runtime_safety && (want_fast_math || type_entry->id != ZigTypeIdFloat)) {2443 if (want_runtime_safety && (want_fast_math || type_entry->id != ZigTypeIdFloat)) {
2441 LLVMValueRef is_zero_bit;2444 LLVMValueRef is_zero_bit;
2442 if (type_entry->id == ZigTypeIdInt) {2445 if (type_entry->id == ZigTypeIdInt) {
...@@ -2456,10 +2459,10 @@ static LLVMValueRef gen_div(CodeGen *g, bool want_runtime_safety, bool want_fast...@@ -2456,10 +2459,10 @@ static LLVMValueRef gen_div(CodeGen *g, bool want_runtime_safety, bool want_fast
2456 LLVMPositionBuilderAtEnd(g->builder, div_zero_ok_block);2459 LLVMPositionBuilderAtEnd(g->builder, div_zero_ok_block);
24572460
2458 if (type_entry->id == ZigTypeIdInt && type_entry->data.integral.is_signed) {2461 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);
2460 BigInt int_min_bi = {0};2463 BigInt int_min_bi = {0};
2461 eval_min_max_value_int(g, type_entry, &int_min_bi, false);2464 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);
2463 LLVMBasicBlockRef overflow_ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "DivOverflowOk");2466 LLVMBasicBlockRef overflow_ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "DivOverflowOk");
2464 LLVMBasicBlockRef overflow_fail_block = LLVMAppendBasicBlock(g->cur_fn_val, "DivOverflowFail");2467 LLVMBasicBlockRef overflow_fail_block = LLVMAppendBasicBlock(g->cur_fn_val, "DivOverflowFail");
2465 LLVMValueRef num_is_int_min = LLVMBuildICmp(g->builder, LLVMIntEQ, val1, int_min_value, "");2468 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...@@ -2513,7 +2516,7 @@ static LLVMValueRef gen_div(CodeGen *g, bool want_runtime_safety, bool want_fast
2513 LLVMBuildBr(g->builder, end_block);2516 LLVMBuildBr(g->builder, end_block);
25142517
2515 LLVMPositionBuilderAtEnd(g->builder, end_block);2518 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), "");
2517 LLVMValueRef incoming_values[] = { ceiled, floored };2520 LLVMValueRef incoming_values[] = { ceiled, floored };
2518 LLVMBasicBlockRef incoming_blocks[] = { ceiled_end_block, floored_end_block };2521 LLVMBasicBlockRef incoming_blocks[] = { ceiled_end_block, floored_end_block };
2519 LLVMAddIncoming(phi, incoming_values, incoming_blocks, 2);2522 LLVMAddIncoming(phi, incoming_values, incoming_blocks, 2);
...@@ -2576,7 +2579,7 @@ static LLVMValueRef gen_div(CodeGen *g, bool want_runtime_safety, bool want_fast...@@ -2576,7 +2579,7 @@ static LLVMValueRef gen_div(CodeGen *g, bool want_runtime_safety, bool want_fast
2576 LLVMValueRef orig_num = LLVMBuildNSWMul(g->builder, result, val2, "");2579 LLVMValueRef orig_num = LLVMBuildNSWMul(g->builder, result, val2, "");
2577 LLVMValueRef orig_ok = LLVMBuildICmp(g->builder, LLVMIntEQ, orig_num, val1, "");2580 LLVMValueRef orig_ok = LLVMBuildICmp(g->builder, LLVMIntEQ, orig_num, val1, "");
2578 LLVMValueRef ok_bit = LLVMBuildOr(g->builder, orig_ok, is_pos, "");2581 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);
2580 LLVMValueRef result_minus_1 = LLVMBuildNSWSub(g->builder, result, one, "");2583 LLVMValueRef result_minus_1 = LLVMBuildNSWSub(g->builder, result, one, "");
2581 return LLVMBuildSelect(g->builder, ok_bit, result, result_minus_1, "");2584 return LLVMBuildSelect(g->builder, ok_bit, result, result_minus_1, "");
2582 }2585 }
...@@ -2595,7 +2598,7 @@ static LLVMValueRef gen_rem(CodeGen *g, bool want_runtime_safety, bool want_fast...@@ -2595,7 +2598,7 @@ static LLVMValueRef gen_rem(CodeGen *g, bool want_runtime_safety, bool want_fast
2595{2598{
2596 ZigLLVMSetFastMath(g->builder, want_fast_math);2599 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));
2599 if (want_runtime_safety) {2602 if (want_runtime_safety) {
2600 LLVMValueRef is_zero_bit;2603 LLVMValueRef is_zero_bit;
2601 if (type_entry->id == ZigTypeIdInt) {2604 if (type_entry->id == ZigTypeIdInt) {
...@@ -2820,7 +2823,7 @@ static void add_error_range_check(CodeGen *g, ZigType *err_set_type, ZigType *in...@@ -2820,7 +2823,7 @@ static void add_error_range_check(CodeGen *g, ZigType *err_set_type, ZigType *in
2820 assert(err_set_type->id == ZigTypeIdErrorSet);2823 assert(err_set_type->id == ZigTypeIdErrorSet);
28212824
2822 if (type_is_global_error_set(err_set_type)) {2825 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));
2824 LLVMValueRef neq_zero_bit = LLVMBuildICmp(g->builder, LLVMIntNE, target_val, zero, "");2827 LLVMValueRef neq_zero_bit = LLVMBuildICmp(g->builder, LLVMIntNE, target_val, zero, "");
2825 LLVMValueRef ok_bit;2828 LLVMValueRef ok_bit;
28262829
...@@ -2832,7 +2835,7 @@ static void add_error_range_check(CodeGen *g, ZigType *err_set_type, ZigType *in...@@ -2832,7 +2835,7 @@ static void add_error_range_check(CodeGen *g, ZigType *err_set_type, ZigType *in
2832 {2835 {
2833 ok_bit = neq_zero_bit;2836 ok_bit = neq_zero_bit;
2834 } else {2837 } 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);
2836 LLVMValueRef in_bounds_bit = LLVMBuildICmp(g->builder, LLVMIntULT, target_val, error_value_count, "");2839 LLVMValueRef in_bounds_bit = LLVMBuildICmp(g->builder, LLVMIntULT, target_val, error_value_count, "");
2837 ok_bit = LLVMBuildAnd(g->builder, neq_zero_bit, in_bounds_bit, "");2840 ok_bit = LLVMBuildAnd(g->builder, neq_zero_bit, in_bounds_bit, "");
2838 }2841 }
...@@ -2853,7 +2856,8 @@ static void add_error_range_check(CodeGen *g, ZigType *err_set_type, ZigType *in...@@ -2853,7 +2856,8 @@ static void add_error_range_check(CodeGen *g, ZigType *err_set_type, ZigType *in
2853 uint32_t err_count = err_set_type->data.error_set.err_count;2856 uint32_t err_count = err_set_type->data.error_set.err_count;
2854 LLVMValueRef switch_instr = LLVMBuildSwitch(g->builder, target_val, fail_block, err_count);2857 LLVMValueRef switch_instr = LLVMBuildSwitch(g->builder, target_val, fail_block, err_count);
2855 for (uint32_t i = 0; i < err_count; i += 1) {2858 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);
2857 LLVMAddCase(switch_instr, case_value, ok_block);2861 LLVMAddCase(switch_instr, case_value, ok_block);
2858 }2862 }
28592863
...@@ -2892,7 +2896,7 @@ static LLVMValueRef ir_render_resize_slice(CodeGen *g, IrExecutable *executable,...@@ -2892,7 +2896,7 @@ static LLVMValueRef ir_render_resize_slice(CodeGen *g, IrExecutable *executable,
2892 LLVMValueRef src_ptr_ptr = LLVMBuildStructGEP(g->builder, expr_val, (unsigned)actual_ptr_index, "");2896 LLVMValueRef src_ptr_ptr = LLVMBuildStructGEP(g->builder, expr_val, (unsigned)actual_ptr_index, "");
2893 LLVMValueRef src_ptr = gen_load_untyped(g, src_ptr_ptr, 0, false, "");2897 LLVMValueRef src_ptr = gen_load_untyped(g, src_ptr_ptr, 0, false, "");
2894 LLVMValueRef src_ptr_casted = LLVMBuildBitCast(g->builder, src_ptr,2898 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), "");
2896 LLVMValueRef dest_ptr_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr,2900 LLVMValueRef dest_ptr_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr,
2897 (unsigned)wanted_ptr_index, "");2901 (unsigned)wanted_ptr_index, "");
2898 gen_store_untyped(g, src_ptr_casted, dest_ptr_ptr, 0, false);2902 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,...@@ -2904,13 +2908,13 @@ static LLVMValueRef ir_render_resize_slice(CodeGen *g, IrExecutable *executable,
29042908
2905 LLVMValueRef new_len;2909 LLVMValueRef new_len;
2906 if (dest_size == 1) {2910 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);
2908 new_len = LLVMBuildMul(g->builder, src_len, src_size_val, "");2912 new_len = LLVMBuildMul(g->builder, src_len, src_size_val, "");
2909 } else if (src_size == 1) {2913 } 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);
2911 if (ir_want_runtime_safety(g, &instruction->base)) {2915 if (ir_want_runtime_safety(g, &instruction->base)) {
2912 LLVMValueRef remainder_val = LLVMBuildURem(g->builder, src_len, dest_size_val, "");2916 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);
2914 LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, remainder_val, zero, "");2918 LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, remainder_val, zero, "");
2915 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "SliceWidenOk");2919 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "SliceWidenOk");
2916 LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn_val, "SliceWidenFail");2920 LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn_val, "SliceWidenFail");
...@@ -2951,9 +2955,9 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,...@@ -2951,9 +2955,9 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,
2951 case CastOpIntToFloat:2955 case CastOpIntToFloat:
2952 assert(actual_type->id == ZigTypeIdInt);2956 assert(actual_type->id == ZigTypeIdInt);
2953 if (actual_type->data.integral.is_signed) {2957 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), "");
2955 } else {2959 } 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), "");
2957 }2961 }
2958 case CastOpFloatToInt: {2962 case CastOpFloatToInt: {
2959 assert(wanted_type->id == ZigTypeIdInt);2963 assert(wanted_type->id == ZigTypeIdInt);
...@@ -2963,9 +2967,9 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,...@@ -2963,9 +2967,9 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,
29632967
2964 LLVMValueRef result;2968 LLVMValueRef result;
2965 if (wanted_type->data.integral.is_signed) {2969 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), "");
2967 } else {2971 } 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), "");
2969 }2973 }
29702974
2971 if (want_safety) {2975 if (want_safety) {
...@@ -2993,14 +2997,14 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,...@@ -2993,14 +2997,14 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,
2993 case CastOpBoolToInt:2997 case CastOpBoolToInt:
2994 assert(wanted_type->id == ZigTypeIdInt);2998 assert(wanted_type->id == ZigTypeIdInt);
2995 assert(actual_type->id == ZigTypeIdBool);2999 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), "");
2997 case CastOpErrSet:3001 case CastOpErrSet:
2998 if (ir_want_runtime_safety(g, &cast_instruction->base)) {3002 if (ir_want_runtime_safety(g, &cast_instruction->base)) {
2999 add_error_range_check(g, wanted_type, g->err_tag_type, expr_val);3003 add_error_range_check(g, wanted_type, g->err_tag_type, expr_val);
3000 }3004 }
3001 return expr_val;3005 return expr_val;
3002 case CastOpBitCast:3006 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), "");
3004 case CastOpPtrOfArrayToSlice: {3008 case CastOpPtrOfArrayToSlice: {
3005 assert(cast_instruction->tmp_ptr);3009 assert(cast_instruction->tmp_ptr);
3006 assert(actual_type->id == ZigTypeIdPointer);3010 assert(actual_type->id == ZigTypeIdPointer);
...@@ -3010,15 +3014,15 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,...@@ -3010,15 +3014,15 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,
3010 LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, cast_instruction->tmp_ptr,3014 LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, cast_instruction->tmp_ptr,
3011 slice_ptr_index, "");3015 slice_ptr_index, "");
3012 LLVMValueRef indices[] = {3016 LLVMValueRef indices[] = {
3013 LLVMConstNull(g->builtin_types.entry_usize->type_ref),3017 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),
3014 LLVMConstInt(g->builtin_types.entry_usize->type_ref, 0, false),3018 LLVMConstInt(g->builtin_types.entry_usize->llvm_type, 0, false),
3015 };3019 };
3016 LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP(g->builder, expr_val, indices, 2, "");3020 LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP(g->builder, expr_val, indices, 2, "");
3017 gen_store_untyped(g, slice_start_ptr, ptr_field_ptr, 0, false);3021 gen_store_untyped(g, slice_start_ptr, ptr_field_ptr, 0, false);
30183022
3019 LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, cast_instruction->tmp_ptr,3023 LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, cast_instruction->tmp_ptr,
3020 slice_len_index, "");3024 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,
3022 array_type->data.array.len, false);3026 array_type->data.array.len, false);
3023 gen_store_untyped(g, len_value, len_field_ptr, 0, false);3027 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,...@@ -3036,7 +3040,7 @@ static LLVMValueRef ir_render_ptr_cast(CodeGen *g, IrExecutable *executable,
3036 return nullptr;3040 return nullptr;
3037 }3041 }
3038 LLVMValueRef ptr = ir_llvm_value(g, instruction->ptr);3042 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), "");
3040 bool want_safety_check = instruction->safety_check_on && ir_want_runtime_safety(g, &instruction->base);3044 bool want_safety_check = instruction->safety_check_on && ir_want_runtime_safety(g, &instruction->base);
3041 if (!want_safety_check || ptr_allows_addr_zero(wanted_type))3045 if (!want_safety_check || ptr_allows_addr_zero(wanted_type))
3042 return result_ptr;3046 return result_ptr;
...@@ -3066,16 +3070,16 @@ static LLVMValueRef ir_render_bit_cast(CodeGen *g, IrExecutable *executable,...@@ -3066,16 +3070,16 @@ static LLVMValueRef ir_render_bit_cast(CodeGen *g, IrExecutable *executable,
3066 if (wanted_is_ptr == actual_is_ptr) {3070 if (wanted_is_ptr == actual_is_ptr) {
3067 // We either bitcast the value directly or bitcast the pointer which does a pointer cast3071 // We either bitcast the value directly or bitcast the pointer which does a pointer cast
3068 LLVMTypeRef wanted_type_ref = wanted_is_ptr ?3072 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);
3070 return LLVMBuildBitCast(g->builder, value, wanted_type_ref, "");3074 return LLVMBuildBitCast(g->builder, value, wanted_type_ref, "");
3071 } else if (actual_is_ptr) {3075 } 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);
3073 LLVMValueRef bitcasted_ptr = LLVMBuildBitCast(g->builder, value, wanted_ptr_type_ref, "");3077 LLVMValueRef bitcasted_ptr = LLVMBuildBitCast(g->builder, value, wanted_ptr_type_ref, "");
3074 uint32_t alignment = get_abi_alignment(g, actual_type);3078 uint32_t alignment = get_abi_alignment(g, actual_type);
3075 return gen_load_untyped(g, bitcasted_ptr, alignment, false, "");3079 return gen_load_untyped(g, bitcasted_ptr, alignment, false, "");
3076 } else {3080 } else {
3077 assert(instruction->tmp_ptr != nullptr);3081 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);
3079 LLVMValueRef bitcasted_ptr = LLVMBuildBitCast(g->builder, instruction->tmp_ptr, wanted_ptr_type_ref, "");3083 LLVMValueRef bitcasted_ptr = LLVMBuildBitCast(g->builder, instruction->tmp_ptr, wanted_ptr_type_ref, "");
3080 uint32_t alignment = get_abi_alignment(g, wanted_type);3084 uint32_t alignment = get_abi_alignment(g, wanted_type);
3081 gen_store_untyped(g, value, bitcasted_ptr, alignment, false);3085 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...@@ -3115,13 +3119,13 @@ static LLVMValueRef ir_render_int_to_ptr(CodeGen *g, IrExecutable *executable, I
31153119
3116 LLVMPositionBuilderAtEnd(g->builder, ok_block);3120 LLVMPositionBuilderAtEnd(g->builder, ok_block);
3117 }3121 }
3118 return LLVMBuildIntToPtr(g->builder, target_val, wanted_type->type_ref, "");3122 return LLVMBuildIntToPtr(g->builder, target_val, get_llvm_type(g, wanted_type), "");
3119}3123}
31203124
3121static LLVMValueRef ir_render_ptr_to_int(CodeGen *g, IrExecutable *executable, IrInstructionPtrToInt *instruction) {3125static LLVMValueRef ir_render_ptr_to_int(CodeGen *g, IrExecutable *executable, IrInstructionPtrToInt *instruction) {
3122 ZigType *wanted_type = instruction->base.value.type;3126 ZigType *wanted_type = instruction->base.value.type;
3123 LLVMValueRef target_val = ir_llvm_value(g, instruction->target);3127 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), "");
3125}3129}
31263130
3127static LLVMValueRef ir_render_int_to_enum(CodeGen *g, IrExecutable *executable, IrInstructionIntToEnum *instruction) {3131static 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,...@@ -3139,7 +3143,7 @@ static LLVMValueRef ir_render_int_to_enum(CodeGen *g, IrExecutable *executable,
3139 size_t field_count = wanted_type->data.enumeration.src_field_count;3143 size_t field_count = wanted_type->data.enumeration.src_field_count;
3140 LLVMValueRef switch_instr = LLVMBuildSwitch(g->builder, tag_int_value, bad_value_block, field_count);3144 LLVMValueRef switch_instr = LLVMBuildSwitch(g->builder, tag_int_value, bad_value_block, field_count);
3141 for (size_t field_i = 0; field_i < field_count; field_i += 1) {3145 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),
3143 &wanted_type->data.enumeration.fields[field_i].value);3147 &wanted_type->data.enumeration.fields[field_i].value);
3144 LLVMAddCase(switch_instr, this_tag_int_value, ok_value_block);3148 LLVMAddCase(switch_instr, this_tag_int_value, ok_value_block);
3145 }3149 }
...@@ -3321,7 +3325,7 @@ static LLVMValueRef ir_render_load_ptr(CodeGen *g, IrExecutable *executable, IrI...@@ -3321,7 +3325,7 @@ static LLVMValueRef ir_render_load_ptr(CodeGen *g, IrExecutable *executable, IrI
3321 LLVMValueRef shifted_value = LLVMBuildLShr(g->builder, containing_int, shift_amt_val, "");3325 LLVMValueRef shifted_value = LLVMBuildLShr(g->builder, containing_int, shift_amt_val, "");
33223326
3323 if (!handle_is_ptr(child_type))3327 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
3326 assert(instruction->tmp_ptr != nullptr);3330 assert(instruction->tmp_ptr != nullptr);
3327 LLVMTypeRef same_size_int = LLVMIntType(size_in_bits);3331 LLVMTypeRef same_size_int = LLVMIntType(size_in_bits);
...@@ -3378,7 +3382,7 @@ static LLVMValueRef gen_valgrind_client_request(CodeGen *g, LLVMValueRef default...@@ -3378,7 +3382,7 @@ static LLVMValueRef gen_valgrind_client_request(CodeGen *g, LLVMValueRef default
3378 if (!target_has_valgrind_support(g->zig_target)) {3382 if (!target_has_valgrind_support(g->zig_target)) {
3379 return default_value;3383 return default_value;
3380 }3384 }
3381 LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->type_ref;3385 LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->llvm_type;
3382 bool asm_has_side_effects = true;3386 bool asm_has_side_effects = true;
3383 bool asm_is_alignstack = false;3387 bool asm_is_alignstack = false;
3384 if (g->zig_target->arch == ZigLLVM_x86_64) {3388 if (g->zig_target->arch == ZigLLVM_x86_64) {
...@@ -3445,7 +3449,7 @@ static bool want_valgrind_support(CodeGen *g) {...@@ -3445,7 +3449,7 @@ static bool want_valgrind_support(CodeGen *g) {
34453449
3446static void gen_undef_init(CodeGen *g, uint32_t ptr_align_bytes, ZigType *value_type, LLVMValueRef ptr) {3450static void gen_undef_init(CodeGen *g, uint32_t ptr_align_bytes, ZigType *value_type, LLVMValueRef ptr) {
3447 assert(type_has_bits(value_type));3451 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));
3449 assert(size_bytes > 0);3453 assert(size_bytes > 0);
3450 assert(ptr_align_bytes > 0);3454 assert(ptr_align_bytes > 0);
3451 // memset uninitialized memory to 0xaa3455 // memset uninitialized memory to 0xaa
...@@ -3453,14 +3457,14 @@ static void gen_undef_init(CodeGen *g, uint32_t ptr_align_bytes, ZigType *value_...@@ -3453,14 +3457,14 @@ static void gen_undef_init(CodeGen *g, uint32_t ptr_align_bytes, ZigType *value_
3453 LLVMValueRef fill_char = LLVMConstInt(LLVMInt8Type(), 0xaa, false);3457 LLVMValueRef fill_char = LLVMConstInt(LLVMInt8Type(), 0xaa, false);
3454 LLVMValueRef dest_ptr = LLVMBuildBitCast(g->builder, ptr, ptr_u8, "");3458 LLVMValueRef dest_ptr = LLVMBuildBitCast(g->builder, ptr, ptr_u8, "");
3455 ZigType *usize = g->builtin_types.entry_usize;3459 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);
3457 ZigLLVMBuildMemSet(g->builder, dest_ptr, fill_char, byte_count, ptr_align_bytes, false);3461 ZigLLVMBuildMemSet(g->builder, dest_ptr, fill_char, byte_count, ptr_align_bytes, false);
3458 // then tell valgrind that the memory is undefined even though we just memset it3462 // then tell valgrind that the memory is undefined even though we just memset it
3459 if (want_valgrind_support(g)) {3463 if (want_valgrind_support(g)) {
3460 static const uint32_t VG_USERREQ__MAKE_MEM_UNDEFINED = 1296236545;3464 static const uint32_t VG_USERREQ__MAKE_MEM_UNDEFINED = 1296236545;
3461 LLVMValueRef zero = LLVMConstInt(usize->type_ref, 0, false);3465 LLVMValueRef zero = LLVMConstInt(usize->llvm_type, 0, false);
3462 LLVMValueRef req = LLVMConstInt(usize->type_ref, VG_USERREQ__MAKE_MEM_UNDEFINED, false);3466 LLVMValueRef req = LLVMConstInt(usize->llvm_type, VG_USERREQ__MAKE_MEM_UNDEFINED, false);
3463 LLVMValueRef ptr_as_usize = LLVMBuildPtrToInt(g->builder, dest_ptr, usize->type_ref, "");3467 LLVMValueRef ptr_as_usize = LLVMBuildPtrToInt(g->builder, dest_ptr, usize->llvm_type, "");
3464 gen_valgrind_client_request(g, zero, req, ptr_as_usize, byte_count, zero, zero, zero);3468 gen_valgrind_client_request(g, zero, req, ptr_as_usize, byte_count, zero, zero, zero);
3465 }3469 }
3466}3470}
...@@ -3515,7 +3519,7 @@ static LLVMValueRef ir_render_elem_ptr(CodeGen *g, IrExecutable *executable, IrI...@@ -3515,7 +3519,7 @@ static LLVMValueRef ir_render_elem_ptr(CodeGen *g, IrExecutable *executable, IrI
3515 array_type = array_type->data.pointer.child_type;3519 array_type = array_type->data.pointer.child_type;
3516 }3520 }
3517 if (safety_check_on) {3521 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,
3519 array_type->data.array.len, false);3523 array_type->data.array.len, false);
3520 add_bounds_check(g, subscript_value, LLVMIntEQ, nullptr, LLVMIntULT, end);3524 add_bounds_check(g, subscript_value, LLVMIntEQ, nullptr, LLVMIntULT, end);
3521 }3525 }
...@@ -3533,18 +3537,18 @@ static LLVMValueRef ir_render_elem_ptr(CodeGen *g, IrExecutable *executable, IrI...@@ -3533,18 +3537,18 @@ static LLVMValueRef ir_render_elem_ptr(CodeGen *g, IrExecutable *executable, IrI
3533 LLVMTypeRef ptr_u8_type_ref = LLVMPointerType(LLVMInt8Type(), 0);3537 LLVMTypeRef ptr_u8_type_ref = LLVMPointerType(LLVMInt8Type(), 0);
3534 LLVMValueRef u8_array_ptr = LLVMBuildBitCast(g->builder, array_ptr, ptr_u8_type_ref, "");3538 LLVMValueRef u8_array_ptr = LLVMBuildBitCast(g->builder, array_ptr, ptr_u8_type_ref, "");
3535 assert(size_in_bits % 8 == 0);3539 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,
3537 size_in_bits / 8, false);3541 size_in_bits / 8, false);
3538 LLVMValueRef byte_offset = LLVMBuildNUWMul(g->builder, subscript_value, elem_size_bytes, "");3542 LLVMValueRef byte_offset = LLVMBuildNUWMul(g->builder, subscript_value, elem_size_bytes, "");
3539 LLVMValueRef indices[] = {3543 LLVMValueRef indices[] = {
3540 byte_offset3544 byte_offset
3541 };3545 };
3542 LLVMValueRef elem_byte_ptr = LLVMBuildInBoundsGEP(g->builder, u8_array_ptr, indices, 1, "");3546 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), "");
3544 }3548 }
3545 }3549 }
3546 LLVMValueRef indices[] = {3550 LLVMValueRef indices[] = {
3547 LLVMConstNull(g->builtin_types.entry_usize->type_ref),3551 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),
3548 subscript_value3552 subscript_value
3549 };3553 };
3550 return LLVMBuildInBoundsGEP(g->builder, array_ptr, indices, 2, "");3554 return LLVMBuildInBoundsGEP(g->builder, array_ptr, indices, 2, "");
...@@ -3773,7 +3777,7 @@ static LLVMValueRef ir_render_union_field_ptr(CodeGen *g, IrExecutable *executab...@@ -3773,7 +3777,7 @@ static LLVMValueRef ir_render_union_field_ptr(CodeGen *g, IrExecutable *executab
3773 return nullptr;3777 return nullptr;
37743778
3775 LLVMValueRef union_ptr = ir_llvm_value(g, instruction->union_ptr);3779 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
3778 if (union_type->data.unionation.gen_tag_index == SIZE_MAX) {3782 if (union_type->data.unionation.gen_tag_index == SIZE_MAX) {
3779 LLVMValueRef union_field_ptr = LLVMBuildStructGEP(g->builder, union_ptr, 0, "");3783 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...@@ -3786,7 +3790,7 @@ static LLVMValueRef ir_render_union_field_ptr(CodeGen *g, IrExecutable *executab
3786 LLVMValueRef tag_value = gen_load_untyped(g, tag_field_ptr, 0, false, "");3790 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),
3790 &field->enum_field->value);3794 &field->enum_field->value);
3791 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "UnionCheckOk");3795 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "UnionCheckOk");
3792 LLVMBasicBlockRef bad_block = LLVMAppendBasicBlock(g->cur_fn_val, "UnionCheckFail");3796 LLVMBasicBlockRef bad_block = LLVMAppendBasicBlock(g->cur_fn_val, "UnionCheckFail");
...@@ -3916,7 +3920,7 @@ static LLVMValueRef ir_render_asm(CodeGen *g, IrExecutable *executable, IrInstru...@@ -3916,7 +3920,7 @@ static LLVMValueRef ir_render_asm(CodeGen *g, IrExecutable *executable, IrInstru
3916 }3920 }
39173921
3918 ZigType *const type = ir_input->value.type;3922 ZigType *const type = ir_input->value.type;
3919 LLVMTypeRef type_ref = type->type_ref;3923 LLVMTypeRef type_ref = get_llvm_type(g, type);
3920 LLVMValueRef value_ref = ir_llvm_value(g, ir_input);3924 LLVMValueRef value_ref = ir_llvm_value(g, ir_input);
3921 // Handle integers of non pot bitsize by widening them.3925 // Handle integers of non pot bitsize by widening them.
3922 if (type->id == ZigTypeIdInt) {3926 if (type->id == ZigTypeIdInt) {
...@@ -3925,7 +3929,7 @@ static LLVMValueRef ir_render_asm(CodeGen *g, IrExecutable *executable, IrInstru...@@ -3925,7 +3929,7 @@ static LLVMValueRef ir_render_asm(CodeGen *g, IrExecutable *executable, IrInstru
3925 const bool is_signed = type->data.integral.is_signed;3929 const bool is_signed = type->data.integral.is_signed;
3926 const size_t wider_bitsize = bitsize < 8 ? 8 : round_to_next_power_of_2(bitsize);3930 const size_t wider_bitsize = bitsize < 8 ? 8 : round_to_next_power_of_2(bitsize);
3927 ZigType *const wider_type = get_int_type(g, is_signed, wider_bitsize);3931 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);
3929 value_ref = gen_widen_or_shorten(g, false, type, wider_type, value_ref);3933 value_ref = gen_widen_or_shorten(g, false, type, wider_type, value_ref);
3930 }3934 }
3931 }3935 }
...@@ -3945,7 +3949,7 @@ static LLVMValueRef ir_render_asm(CodeGen *g, IrExecutable *executable, IrInstru...@@ -3945,7 +3949,7 @@ static LLVMValueRef ir_render_asm(CodeGen *g, IrExecutable *executable, IrInstru
3945 if (instruction->return_count == 0) {3949 if (instruction->return_count == 0) {
3946 ret_type = LLVMVoidType();3950 ret_type = LLVMVoidType();
3947 } else {3951 } else {
3948 ret_type = instruction->base.value.type->type_ref;3952 ret_type = get_llvm_type(g, instruction->base.value.type);
3949 }3953 }
3950 LLVMTypeRef function_type = LLVMFunctionType(ret_type, param_types, (unsigned)input_and_output_count, false);3954 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...@@ -3964,7 +3968,7 @@ static LLVMValueRef gen_non_null_bit(CodeGen *g, ZigType *maybe_type, LLVMValueR
3964 } else {3968 } else {
3965 bool is_scalar = !handle_is_ptr(maybe_type);3969 bool is_scalar = !handle_is_ptr(maybe_type);
3966 if (is_scalar) {3970 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)), "");
3968 } else {3972 } else {
3969 LLVMValueRef maybe_field_ptr = LLVMBuildStructGEP(g->builder, maybe_handle, maybe_null_index, "");3973 LLVMValueRef maybe_field_ptr = LLVMBuildStructGEP(g->builder, maybe_handle, maybe_null_index, "");
3970 return gen_load_untyped(g, maybe_field_ptr, 0, false, "");3974 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...@@ -3999,7 +4003,7 @@ static LLVMValueRef ir_render_optional_unwrap_ptr(CodeGen *g, IrExecutable *exec
39994003
4000 LLVMPositionBuilderAtEnd(g->builder, ok_block);4004 LLVMPositionBuilderAtEnd(g->builder, ok_block);
4001 }4005 }
4002 if (child_type->zero_bits) {4006 if (!type_has_bits(child_type)) {
4003 return nullptr;4007 return nullptr;
4004 } else {4008 } else {
4005 bool is_scalar = !handle_is_ptr(maybe_type);4009 bool is_scalar = !handle_is_ptr(maybe_type);
...@@ -4052,10 +4056,10 @@ static LLVMValueRef get_int_builtin_fn(CodeGen *g, ZigType *int_type, BuiltinFnI...@@ -4052,10 +4056,10 @@ static LLVMValueRef get_int_builtin_fn(CodeGen *g, ZigType *int_type, BuiltinFnI
4052 char llvm_name[64];4056 char llvm_name[64];
4053 sprintf(llvm_name, "llvm.%s.i%" PRIu32, fn_name, int_type->data.integral.bit_count);4057 sprintf(llvm_name, "llvm.%s.i%" PRIu32, fn_name, int_type->data.integral.bit_count);
4054 LLVMTypeRef param_types[] = {4058 LLVMTypeRef param_types[] = {
4055 int_type->type_ref,4059 get_llvm_type(g, int_type),
4056 LLVMInt1Type(),4060 LLVMInt1Type(),
4057 };4061 };
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);
4059 LLVMValueRef fn_val = LLVMAddFunction(g->module, llvm_name, fn_type);4063 LLVMValueRef fn_val = LLVMAddFunction(g->module, llvm_name, fn_type);
4060 assert(LLVMGetIntrinsicID(fn_val));4064 assert(LLVMGetIntrinsicID(fn_val));
40614065
...@@ -4114,9 +4118,9 @@ static LLVMValueRef ir_render_phi(CodeGen *g, IrExecutable *executable, IrInstru...@@ -4114,9 +4118,9 @@ static LLVMValueRef ir_render_phi(CodeGen *g, IrExecutable *executable, IrInstru
41144118
4115 LLVMTypeRef phi_type;4119 LLVMTypeRef phi_type;
4116 if (handle_is_ptr(instruction->base.value.type)) {4120 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);
4118 } else {4122 } else {
4119 phi_type = instruction->base.value.type->type_ref;4123 phi_type = get_llvm_type(g, instruction->base.value.type);
4120 }4124 }
41214125
4122 LLVMValueRef phi = LLVMBuildPhi(g->builder, phi_type, "");4126 LLVMValueRef phi = LLVMBuildPhi(g->builder, phi_type, "");
...@@ -4160,7 +4164,7 @@ static LLVMValueRef ir_render_err_name(CodeGen *g, IrExecutable *executable, IrI...@@ -4160,7 +4164,7 @@ static LLVMValueRef ir_render_err_name(CodeGen *g, IrExecutable *executable, IrI
4160 }4164 }
41614165
4162 LLVMValueRef indices[] = {4166 LLVMValueRef indices[] = {
4163 LLVMConstNull(g->builtin_types.entry_usize->type_ref),4167 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),
4164 err_val,4168 err_val,
4165 };4169 };
4166 return LLVMBuildInBoundsGEP(g->builder, g->err_name_table, indices, 2, "");4170 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) {...@@ -4176,8 +4180,9 @@ static LLVMValueRef get_enum_tag_name_function(CodeGen *g, ZigType *enum_type) {
4176 ZigType *u8_slice_type = get_slice_type(g, u8_ptr_type);4180 ZigType *u8_slice_type = get_slice_type(g, u8_ptr_type);
4177 ZigType *tag_int_type = enum_type->data.enumeration.tag_int_type;4181 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),4183 LLVMTypeRef tag_int_llvm_type = get_llvm_type(g, tag_int_type);
4180 &tag_int_type->type_ref, 1, false);4184 LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMPointerType(get_llvm_type(g, u8_slice_type), 0),
4185 &tag_int_llvm_type, 1, false);
4181 4186
4182 Buf *fn_name = get_mangled_name(g, buf_sprintf("__zig_tag_name_%s", buf_ptr(&enum_type->name)), false);4187 Buf *fn_name = get_mangled_name(g, buf_sprintf("__zig_tag_name_%s", buf_ptr(&enum_type->name)), false);
4183 LLVMValueRef fn_val = LLVMAddFunction(g->module, buf_ptr(fn_name), fn_type_ref);4188 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) {...@@ -4209,8 +4214,8 @@ static LLVMValueRef get_enum_tag_name_function(CodeGen *g, ZigType *enum_type) {
42094214
4210 ZigType *usize = g->builtin_types.entry_usize;4215 ZigType *usize = g->builtin_types.entry_usize;
4211 LLVMValueRef array_ptr_indices[] = {4216 LLVMValueRef array_ptr_indices[] = {
4212 LLVMConstNull(usize->type_ref),4217 LLVMConstNull(usize->llvm_type),
4213 LLVMConstNull(usize->type_ref),4218 LLVMConstNull(usize->llvm_type),
4214 };4219 };
42154220
4216 for (size_t field_i = 0; field_i < field_count; field_i += 1) {4221 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) {...@@ -4225,9 +4230,9 @@ static LLVMValueRef get_enum_tag_name_function(CodeGen *g, ZigType *enum_type) {
42254230
4226 LLVMValueRef fields[] = {4231 LLVMValueRef fields[] = {
4227 LLVMConstGEP(str_global, array_ptr_indices, 2),4232 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),
4229 };4234 };
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
4232 LLVMValueRef slice_global = LLVMAddGlobal(g->module, LLVMTypeOf(slice_init_value), "");4237 LLVMValueRef slice_global = LLVMAddGlobal(g->module, LLVMTypeOf(slice_init_value), "");
4233 LLVMSetInitializer(slice_global, slice_init_value);4238 LLVMSetInitializer(slice_global, slice_init_value);
...@@ -4237,7 +4242,7 @@ static LLVMValueRef get_enum_tag_name_function(CodeGen *g, ZigType *enum_type) {...@@ -4237,7 +4242,7 @@ static LLVMValueRef get_enum_tag_name_function(CodeGen *g, ZigType *enum_type) {
4237 LLVMSetAlignment(slice_global, LLVMABIAlignmentOfType(g->target_data_ref, LLVMTypeOf(slice_init_value)));4242 LLVMSetAlignment(slice_global, LLVMABIAlignmentOfType(g->target_data_ref, LLVMTypeOf(slice_init_value)));
42384243
4239 LLVMBasicBlockRef return_block = LLVMAppendBasicBlock(g->cur_fn_val, "Name");4244 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),
4241 &enum_type->data.enumeration.fields[field_i].value);4246 &enum_type->data.enumeration.fields[field_i].value);
4242 LLVMAddCase(switch_instr, this_tag_int_value, return_block);4247 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...@@ -4283,22 +4288,21 @@ static LLVMValueRef ir_render_field_parent_ptr(CodeGen *g, IrExecutable *executa
4283 ZigType *container_type = container_ptr_type->data.pointer.child_type;4288 ZigType *container_type = container_ptr_type->data.pointer.child_type;
42844289
4285 size_t byte_offset = LLVMOffsetOfElement(g->target_data_ref,4290 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
4288 LLVMValueRef field_ptr_val = ir_llvm_value(g, instruction->field_ptr);4293 LLVMValueRef field_ptr_val = ir_llvm_value(g, instruction->field_ptr);
42894294
4290 if (byte_offset == 0) {4295 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), "");
4292 } else {4297 } else {
4293 ZigType *usize = g->builtin_types.entry_usize;4298 ZigType *usize = g->builtin_types.entry_usize;
42944299
4295 LLVMValueRef field_ptr_int = LLVMBuildPtrToInt(g->builder, field_ptr_val,4300 LLVMValueRef field_ptr_int = LLVMBuildPtrToInt(g->builder, field_ptr_val, usize->llvm_type, "");
4296 usize->type_ref, "");
42974301
4298 LLVMValueRef base_ptr_int = LLVMBuildNUWSub(g->builder, field_ptr_int,4302 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), "");
4302 }4306 }
4303}4307}
43044308
...@@ -4349,10 +4353,10 @@ static LLVMValueRef ir_render_align_cast(CodeGen *g, IrExecutable *executable, I...@@ -4349,10 +4353,10 @@ static LLVMValueRef ir_render_align_cast(CodeGen *g, IrExecutable *executable, I
4349 assert(align_bytes != 1);4353 assert(align_bytes != 1);
43504354
4351 ZigType *usize = g->builtin_types.entry_usize;4355 ZigType *usize = g->builtin_types.entry_usize;
4352 LLVMValueRef ptr_as_int_val = LLVMBuildPtrToInt(g->builder, ptr_val, usize->type_ref, "");4356 LLVMValueRef ptr_as_int_val = LLVMBuildPtrToInt(g->builder, ptr_val, usize->llvm_type, "");
4353 LLVMValueRef alignment_minus_1 = LLVMConstInt(usize->type_ref, align_bytes - 1, false);4357 LLVMValueRef alignment_minus_1 = LLVMConstInt(usize->llvm_type, align_bytes - 1, false);
4354 LLVMValueRef anded_val = LLVMBuildAnd(g->builder, ptr_as_int_val, alignment_minus_1, "");4358 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
4357 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "AlignCastOk");4361 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "AlignCastOk");
4358 LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn_val, "AlignCastFail");4362 LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn_val, "AlignCastFail");
...@@ -4373,7 +4377,7 @@ static LLVMValueRef ir_render_error_return_trace(CodeGen *g, IrExecutable *execu...@@ -4373,7 +4377,7 @@ static LLVMValueRef ir_render_error_return_trace(CodeGen *g, IrExecutable *execu
4373 LLVMValueRef cur_err_ret_trace_val = get_cur_err_ret_trace_val(g, instruction->base.scope);4377 LLVMValueRef cur_err_ret_trace_val = get_cur_err_ret_trace_val(g, instruction->base.scope);
4374 if (cur_err_ret_trace_val == nullptr) {4378 if (cur_err_ret_trace_val == nullptr) {
4375 ZigType *ptr_to_stack_trace_type = get_ptr_to_stack_trace_type(g);4379 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));
4377 }4381 }
4378 return cur_err_ret_trace_val;4382 return cur_err_ret_trace_val;
4379}4383}
...@@ -4439,7 +4443,7 @@ static LLVMValueRef ir_render_cmpxchg(CodeGen *g, IrExecutable *executable, IrIn...@@ -4439,7 +4443,7 @@ static LLVMValueRef ir_render_cmpxchg(CodeGen *g, IrExecutable *executable, IrIn
4439 if (!handle_is_ptr(maybe_type)) {4443 if (!handle_is_ptr(maybe_type)) {
4440 LLVMValueRef payload_val = LLVMBuildExtractValue(g->builder, result_val, 0, "");4444 LLVMValueRef payload_val = LLVMBuildExtractValue(g->builder, result_val, 0, "");
4441 LLVMValueRef success_bit = LLVMBuildExtractValue(g->builder, result_val, 1, "");4445 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, "");
4443 }4447 }
44444448
4445 assert(instruction->tmp_ptr != nullptr);4449 assert(instruction->tmp_ptr != nullptr);
...@@ -4470,10 +4474,10 @@ static LLVMValueRef ir_render_truncate(CodeGen *g, IrExecutable *executable, IrI...@@ -4470,10 +4474,10 @@ static LLVMValueRef ir_render_truncate(CodeGen *g, IrExecutable *executable, IrI
4470 // no-op4474 // no-op
4471 return target_val;4475 return target_val;
4472 } if (src_type->data.integral.bit_count == dest_type->data.integral.bit_count) {4476 } 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), "");
4474 } else {4478 } else {
4475 LLVMValueRef target_val = ir_llvm_value(g, instruction->target);4479 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), "");
4477 }4481 }
4478}4482}
44794483
...@@ -4539,12 +4543,12 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutable *executable, IrInst...@@ -4539,12 +4543,12 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutable *executable, IrInst
4539 if (instruction->end) {4543 if (instruction->end) {
4540 end_val = ir_llvm_value(g, instruction->end);4544 end_val = ir_llvm_value(g, instruction->end);
4541 } else {4545 } 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);
4543 }4547 }
4544 if (want_runtime_safety) {4548 if (want_runtime_safety) {
4545 add_bounds_check(g, start_val, LLVMIntEQ, nullptr, LLVMIntULE, end_val);4549 add_bounds_check(g, start_val, LLVMIntEQ, nullptr, LLVMIntULE, end_val);
4546 if (instruction->end) {4550 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,
4548 array_type->data.array.len, false);4552 array_type->data.array.len, false);
4549 add_bounds_check(g, end_val, LLVMIntEQ, nullptr, LLVMIntULE, array_end);4553 add_bounds_check(g, end_val, LLVMIntEQ, nullptr, LLVMIntULE, array_end);
4550 }4554 }
...@@ -4561,7 +4565,7 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutable *executable, IrInst...@@ -4561,7 +4565,7 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutable *executable, IrInst
45614565
4562 LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, slice_ptr_index, "");4566 LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, slice_ptr_index, "");
4563 LLVMValueRef indices[] = {4567 LLVMValueRef indices[] = {
4564 LLVMConstNull(g->builtin_types.entry_usize->type_ref),4568 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),
4565 start_val,4569 start_val,
4566 };4570 };
4567 LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP(g->builder, array_ptr, indices, 2, "");4571 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...@@ -4663,9 +4667,9 @@ static LLVMValueRef ir_render_breakpoint(CodeGen *g, IrExecutable *executable, I
4663static LLVMValueRef ir_render_return_address(CodeGen *g, IrExecutable *executable,4667static LLVMValueRef ir_render_return_address(CodeGen *g, IrExecutable *executable,
4664 IrInstructionReturnAddress *instruction)4668 IrInstructionReturnAddress *instruction)
4665{4669{
4666 LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_i32->type_ref);4670 LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_i32->llvm_type);
4667 LLVMValueRef ptr_val = LLVMBuildCall(g->builder, get_return_address_fn_val(g), &zero, 1, "");4671 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, "");
4669}4673}
46704674
4671static LLVMValueRef get_frame_address_fn_val(CodeGen *g) {4675static LLVMValueRef get_frame_address_fn_val(CodeGen *g) {
...@@ -4674,8 +4678,8 @@ static LLVMValueRef get_frame_address_fn_val(CodeGen *g) {...@@ -4674,8 +4678,8 @@ static LLVMValueRef get_frame_address_fn_val(CodeGen *g) {
46744678
4675 ZigType *return_type = get_pointer_to_type(g, g->builtin_types.entry_u8, true);4679 ZigType *return_type = get_pointer_to_type(g, g->builtin_types.entry_u8, true);
46764680
4677 LLVMTypeRef fn_type = LLVMFunctionType(return_type->type_ref,4681 LLVMTypeRef fn_type = LLVMFunctionType(get_llvm_type(g, return_type),
4678 &g->builtin_types.entry_i32->type_ref, 1, false);4682 &g->builtin_types.entry_i32->llvm_type, 1, false);
4679 g->frame_address_fn_val = LLVMAddFunction(g->module, "llvm.frameaddress", fn_type);4683 g->frame_address_fn_val = LLVMAddFunction(g->module, "llvm.frameaddress", fn_type);
4680 assert(LLVMGetIntrinsicID(g->frame_address_fn_val));4684 assert(LLVMGetIntrinsicID(g->frame_address_fn_val));
46814685
...@@ -4685,9 +4689,9 @@ static LLVMValueRef get_frame_address_fn_val(CodeGen *g) {...@@ -4685,9 +4689,9 @@ static LLVMValueRef get_frame_address_fn_val(CodeGen *g) {
4685static LLVMValueRef ir_render_frame_address(CodeGen *g, IrExecutable *executable,4689static LLVMValueRef ir_render_frame_address(CodeGen *g, IrExecutable *executable,
4686 IrInstructionFrameAddress *instruction)4690 IrInstructionFrameAddress *instruction)
4687{4691{
4688 LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_i32->type_ref);4692 LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_i32->llvm_type);
4689 LLVMValueRef ptr_val = LLVMBuildCall(g->builder, get_frame_address_fn_val(g), &zero, 1, "");4693 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, "");
4691}4695}
46924696
4693static LLVMValueRef get_handle_fn_val(CodeGen *g) {4697static LLVMValueRef get_handle_fn_val(CodeGen *g) {
...@@ -4706,7 +4710,7 @@ static LLVMValueRef get_handle_fn_val(CodeGen *g) {...@@ -4706,7 +4710,7 @@ static LLVMValueRef get_handle_fn_val(CodeGen *g) {
4706static LLVMValueRef ir_render_handle(CodeGen *g, IrExecutable *executable,4710static LLVMValueRef ir_render_handle(CodeGen *g, IrExecutable *executable,
4707 IrInstructionHandle *instruction)4711 IrInstructionHandle *instruction)
4708{4712{
4709 LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_promise->type_ref);4713 LLVMValueRef zero = LLVMConstNull(get_llvm_type(g, g->builtin_types.entry_promise));
4710 return LLVMBuildCall(g->builder, get_handle_fn_val(g), &zero, 0, "");4714 return LLVMBuildCall(g->builder, get_handle_fn_val(g), &zero, 0, "");
4711}4715}
47124716
...@@ -4786,7 +4790,7 @@ static LLVMValueRef ir_render_test_err(CodeGen *g, IrExecutable *executable, IrI...@@ -4786,7 +4790,7 @@ static LLVMValueRef ir_render_test_err(CodeGen *g, IrExecutable *executable, IrI
4786 err_val = err_union_handle;4790 err_val = err_union_handle;
4787 }4791 }
47884792
4789 LLVMValueRef zero = LLVMConstNull(g->err_tag_type->type_ref);4793 LLVMValueRef zero = LLVMConstNull(get_llvm_type(g, g->err_tag_type));
4790 return LLVMBuildICmp(g->builder, LLVMIntNE, err_val, zero, "");4794 return LLVMBuildICmp(g->builder, LLVMIntNE, err_val, zero, "");
4791}4795}
47924796
...@@ -4834,7 +4838,7 @@ static LLVMValueRef ir_render_unwrap_err_payload(CodeGen *g, IrExecutable *execu...@@ -4834,7 +4838,7 @@ static LLVMValueRef ir_render_unwrap_err_payload(CodeGen *g, IrExecutable *execu
4834 } else {4838 } else {
4835 err_val = err_union_handle;4839 err_val = err_union_handle;
4836 }4840 }
4837 LLVMValueRef zero = LLVMConstNull(g->err_tag_type->type_ref);4841 LLVMValueRef zero = LLVMConstNull(get_llvm_type(g, g->err_tag_type));
4838 LLVMValueRef cond_val = LLVMBuildICmp(g->builder, LLVMIntEQ, err_val, zero, "");4842 LLVMValueRef cond_val = LLVMBuildICmp(g->builder, LLVMIntEQ, err_val, zero, "");
4839 LLVMBasicBlockRef err_block = LLVMAppendBasicBlock(g->cur_fn_val, "UnwrapErrError");4843 LLVMBasicBlockRef err_block = LLVMAppendBasicBlock(g->cur_fn_val, "UnwrapErrError");
4840 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "UnwrapErrOk");4844 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "UnwrapErrOk");
...@@ -4860,7 +4864,7 @@ static LLVMValueRef ir_render_maybe_wrap(CodeGen *g, IrExecutable *executable, I...@@ -4860,7 +4864,7 @@ static LLVMValueRef ir_render_maybe_wrap(CodeGen *g, IrExecutable *executable, I
48604864
4861 ZigType *child_type = wanted_type->data.maybe.child_type;4865 ZigType *child_type = wanted_type->data.maybe.child_type;
48624866
4863 if (child_type->zero_bits) {4867 if (!type_has_bits(child_type)) {
4864 return LLVMConstInt(LLVMInt1Type(), 1, false);4868 return LLVMConstInt(LLVMInt1Type(), 1, false);
4865 }4869 }
48664870
...@@ -4913,7 +4917,7 @@ static LLVMValueRef ir_render_err_wrap_payload(CodeGen *g, IrExecutable *executa...@@ -4913,7 +4917,7 @@ static LLVMValueRef ir_render_err_wrap_payload(CodeGen *g, IrExecutable *executa
4913 return ir_llvm_value(g, instruction->value);4917 return ir_llvm_value(g, instruction->value);
4914 }4918 }
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
4918 if (!type_has_bits(payload_type))4922 if (!type_has_bits(payload_type))
4919 return ok_err_val;4923 return ok_err_val;
...@@ -4991,7 +4995,7 @@ static LLVMValueRef ir_render_union_init(CodeGen *g, IrExecutable *executable, I...@@ -4991,7 +4995,7 @@ static LLVMValueRef ir_render_union_init(CodeGen *g, IrExecutable *executable, I
4991 LLVMValueRef tag_field_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr,4995 LLVMValueRef tag_field_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr,
4992 union_type->data.unionation.gen_tag_index, "");4996 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),
4995 &type_union_field->enum_field->value);4999 &type_union_field->enum_field->value);
4996 gen_store_untyped(g, tag_value, tag_field_ptr, 0, false);5000 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...@@ -5001,7 +5005,7 @@ static LLVMValueRef ir_render_union_init(CodeGen *g, IrExecutable *executable, I
5001 uncasted_union_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, (unsigned)0, "");5005 uncasted_union_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, (unsigned)0, "");
5002 }5006 }
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), "");
5005 LLVMValueRef value = ir_llvm_value(g, instruction->init_value);5009 LLVMValueRef value = ir_llvm_value(g, instruction->init_value);
50065010
5007 gen_assign_raw(g, field_ptr, ptr_type, value);5011 gen_assign_raw(g, field_ptr, ptr_type, value);
...@@ -5023,8 +5027,8 @@ static LLVMValueRef ir_render_container_init_list(CodeGen *g, IrExecutable *exec...@@ -5023,8 +5027,8 @@ static LLVMValueRef ir_render_container_init_list(CodeGen *g, IrExecutable *exec
5023 for (size_t i = 0; i < field_count; i += 1) {5027 for (size_t i = 0; i < field_count; i += 1) {
5024 LLVMValueRef elem_val = ir_llvm_value(g, instruction->items[i]);5028 LLVMValueRef elem_val = ir_llvm_value(g, instruction->items[i]);
5025 LLVMValueRef indices[] = {5029 LLVMValueRef indices[] = {
5026 LLVMConstNull(g->builtin_types.entry_usize->type_ref),5030 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),
5027 LLVMConstInt(g->builtin_types.entry_usize->type_ref, i, false),5031 LLVMConstInt(g->builtin_types.entry_usize->llvm_type, i, false),
5028 };5032 };
5029 LLVMValueRef elem_ptr = LLVMBuildInBoundsGEP(g->builder, tmp_array_ptr, indices, 2, "");5033 LLVMValueRef elem_ptr = LLVMBuildInBoundsGEP(g->builder, tmp_array_ptr, indices, 2, "");
5030 gen_assign_raw(g, elem_ptr, get_pointer_to_type(g, child_type, false), elem_val);5034 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...@@ -5041,7 +5045,7 @@ static LLVMValueRef ir_render_panic(CodeGen *g, IrExecutable *executable, IrInst
5041static LLVMValueRef ir_render_coro_id(CodeGen *g, IrExecutable *executable, IrInstructionCoroId *instruction) {5045static LLVMValueRef ir_render_coro_id(CodeGen *g, IrExecutable *executable, IrInstructionCoroId *instruction) {
5042 LLVMValueRef promise_ptr = ir_llvm_value(g, instruction->promise_ptr);5046 LLVMValueRef promise_ptr = ir_llvm_value(g, instruction->promise_ptr);
5043 LLVMValueRef align_val = LLVMConstInt(LLVMInt32Type(), get_coro_frame_align_bytes(g), false);5047 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),
5045 LLVMPointerType(LLVMInt8Type(), 0));5049 LLVMPointerType(LLVMInt8Type(), 0));
5046 LLVMValueRef params[] = {5050 LLVMValueRef params[] = {
5047 align_val,5051 align_val,
...@@ -5140,7 +5144,7 @@ static LLVMValueRef ir_render_coro_promise(CodeGen *g, IrExecutable *executable,...@@ -5140,7 +5144,7 @@ static LLVMValueRef ir_render_coro_promise(CodeGen *g, IrExecutable *executable,
5140 LLVMConstNull(LLVMInt1Type()),5144 LLVMConstNull(LLVMInt1Type()),
5141 };5145 };
5142 LLVMValueRef uncasted_result = LLVMBuildCall(g->builder, get_coro_promise_fn_val(g), params, 3, "");5146 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), "");
5144}5148}
51455149
5146static LLVMValueRef get_coro_alloc_helper_fn_val(CodeGen *g, LLVMTypeRef alloc_fn_type_ref, ZigType *fn_type) {5150static 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...@@ -5161,8 +5165,8 @@ static LLVMValueRef get_coro_alloc_helper_fn_val(CodeGen *g, LLVMTypeRef alloc_f
5161 arg_types.append(alloc_fn_arg_types[1]);5165 arg_types.append(alloc_fn_arg_types[1]);
5162 }5166 }
5163 arg_types.append(alloc_fn_arg_types[g->have_err_ret_tracing ? 2 : 1]);5167 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);5168 arg_types.append(get_llvm_type(g, ptr_to_err_code_type));
5165 arg_types.append(g->builtin_types.entry_usize->type_ref);5169 arg_types.append(g->builtin_types.entry_usize->llvm_type);
51665170
5167 LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMPointerType(LLVMInt8Type(), 0),5171 LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMPointerType(LLVMInt8Type(), 0),
5168 arg_types.items, arg_types.length, false);5172 arg_types.items, arg_types.length, false);
...@@ -5204,7 +5208,7 @@ static LLVMValueRef get_coro_alloc_helper_fn_val(CodeGen *g, LLVMTypeRef alloc_f...@@ -5204,7 +5208,7 @@ static LLVMValueRef get_coro_alloc_helper_fn_val(CodeGen *g, LLVMTypeRef alloc_f
5204 next_arg += 1;5208 next_arg += 1;
5205 LLVMValueRef coro_size = LLVMGetParam(fn_val, next_arg);5209 LLVMValueRef coro_size = LLVMGetParam(fn_val, next_arg);
5206 next_arg += 1;5210 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,
5208 get_coro_frame_align_bytes(g), false);5212 get_coro_frame_align_bytes(g), false);
52095213
5210 ConstExprValue *zero_array = create_const_str_lit(g, buf_create_from_str(""));5214 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...@@ -5219,7 +5223,7 @@ static LLVMValueRef get_coro_alloc_helper_fn_val(CodeGen *g, LLVMTypeRef alloc_f
5219 }5223 }
5220 args.append(allocator_val);5224 args.append(allocator_val);
5221 args.append(undef_slice_zero->global_refs->llvm_global);5225 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));
5223 args.append(coro_size);5227 args.append(coro_size);
5224 args.append(alignment_val);5228 args.append(alignment_val);
5225 LLVMValueRef call_instruction = ZigLLVMBuildCall(g->builder, realloc_fn_val, args.items, args.length,5229 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,...@@ -5299,10 +5303,10 @@ static LLVMValueRef ir_render_atomic_rmw(CodeGen *g, IrExecutable *executable,
52995303
5300 // it's a pointer but we need to treat it as an int5304 // it's a pointer but we need to treat it as an int
5301 LLVMValueRef casted_ptr = LLVMBuildBitCast(g->builder, ptr,5305 LLVMValueRef casted_ptr = LLVMBuildBitCast(g->builder, ptr,
5302 LLVMPointerType(g->builtin_types.entry_usize->type_ref, 0), "");5306 LLVMPointerType(g->builtin_types.entry_usize->llvm_type, 0), "");
5303 LLVMValueRef casted_operand = LLVMBuildPtrToInt(g->builder, operand, g->builtin_types.entry_usize->type_ref, "");5307 LLVMValueRef casted_operand = LLVMBuildPtrToInt(g->builder, operand, g->builtin_types.entry_usize->llvm_type, "");
5304 LLVMValueRef uncasted_result = LLVMBuildAtomicRMW(g->builder, op, casted_ptr, casted_operand, ordering, false);5308 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), "");
5306}5310}
53075311
5308static LLVMValueRef ir_render_atomic_load(CodeGen *g, IrExecutable *executable,5312static LLVMValueRef ir_render_atomic_load(CodeGen *g, IrExecutable *executable,
...@@ -5355,15 +5359,15 @@ static LLVMValueRef ir_render_bswap(CodeGen *g, IrExecutable *executable, IrInst...@@ -5355,15 +5359,15 @@ static LLVMValueRef ir_render_bswap(CodeGen *g, IrExecutable *executable, IrInst
5355 ZigType *extended_type = get_int_type(g, int_type->data.integral.is_signed,5359 ZigType *extended_type = get_int_type(g, int_type->data.integral.is_signed,
5356 int_type->data.integral.bit_count + 8);5360 int_type->data.integral.bit_count + 8);
5357 // aabbcc5361 // 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), "");
5359 // 00aabbcc5363 // 00aabbcc
5360 LLVMValueRef fn_val = get_int_builtin_fn(g, extended_type, BuiltinFnIdBswap);5364 LLVMValueRef fn_val = get_int_builtin_fn(g, extended_type, BuiltinFnIdBswap);
5361 LLVMValueRef swapped = LLVMBuildCall(g->builder, fn_val, &extended, 1, "");5365 LLVMValueRef swapped = LLVMBuildCall(g->builder, fn_val, &extended, 1, "");
5362 // ccbbaa005366 // ccbbaa00
5363 LLVMValueRef shifted = ZigLLVMBuildLShrExact(g->builder, swapped,5367 LLVMValueRef shifted = ZigLLVMBuildLShrExact(g->builder, swapped,
5364 LLVMConstInt(extended_type->type_ref, 8, false), "");5368 LLVMConstInt(get_llvm_type(g, extended_type), 8, false), "");
5365 // 00ccbbaa5369 // 00ccbbaa
5366 return LLVMBuildTrunc(g->builder, shifted, int_type->type_ref, "");5370 return LLVMBuildTrunc(g->builder, shifted, get_llvm_type(g, int_type), "");
5367}5371}
53685372
5369static LLVMValueRef ir_render_bit_reverse(CodeGen *g, IrExecutable *executable, IrInstructionBitReverse *instruction) {5373static 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...@@ -5383,7 +5387,7 @@ static LLVMValueRef ir_render_vector_to_array(CodeGen *g, IrExecutable *executab
5383 assert(instruction->tmp_ptr);5387 assert(instruction->tmp_ptr);
5384 LLVMValueRef vector = ir_llvm_value(g, instruction->vector);5388 LLVMValueRef vector = ir_llvm_value(g, instruction->vector);
5385 LLVMValueRef casted_ptr = LLVMBuildBitCast(g->builder, instruction->tmp_ptr,5389 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), "");
5387 gen_store_untyped(g, vector, casted_ptr, 0, false);5391 gen_store_untyped(g, vector, casted_ptr, 0, false);
5388 return instruction->tmp_ptr;5392 return instruction->tmp_ptr;
5389}5393}
...@@ -5396,7 +5400,7 @@ static LLVMValueRef ir_render_array_to_vector(CodeGen *g, IrExecutable *executab...@@ -5396,7 +5400,7 @@ static LLVMValueRef ir_render_array_to_vector(CodeGen *g, IrExecutable *executab
5396 assert(!handle_is_ptr(vector_type));5400 assert(!handle_is_ptr(vector_type));
5397 LLVMValueRef array_ptr = ir_llvm_value(g, instruction->array);5401 LLVMValueRef array_ptr = ir_llvm_value(g, instruction->array);
5398 LLVMValueRef casted_ptr = LLVMBuildBitCast(g->builder, array_ptr,5402 LLVMValueRef casted_ptr = LLVMBuildBitCast(g->builder, array_ptr,
5399 LLVMPointerType(vector_type->type_ref, 0), "");5403 LLVMPointerType(get_llvm_type(g, vector_type), 0), "");
5400 return gen_load_untyped(g, casted_ptr, 0, false, "");5404 return gen_load_untyped(g, casted_ptr, 0, false, "");
5401}5405}
54025406
...@@ -5735,15 +5739,15 @@ static LLVMValueRef gen_const_ptr_array_recursive(CodeGen *g, ConstExprValue *ar...@@ -5735,15 +5739,15 @@ static LLVMValueRef gen_const_ptr_array_recursive(CodeGen *g, ConstExprValue *ar
5735 if (el_type == LLVMArrayTypeKind) {5739 if (el_type == LLVMArrayTypeKind) {
5736 ZigType *usize = g->builtin_types.entry_usize;5740 ZigType *usize = g->builtin_types.entry_usize;
5737 LLVMValueRef indices[] = {5741 LLVMValueRef indices[] = {
5738 LLVMConstNull(usize->type_ref),5742 LLVMConstNull(usize->llvm_type),
5739 LLVMConstInt(usize->type_ref, index, false),5743 LLVMConstInt(usize->llvm_type, index, false),
5740 };5744 };
5741 return LLVMConstInBoundsGEP(base_ptr, indices, 2);5745 return LLVMConstInBoundsGEP(base_ptr, indices, 2);
5742 } else if (el_type == LLVMStructTypeKind) {5746 } else if (el_type == LLVMStructTypeKind) {
5743 ZigType *u32 = g->builtin_types.entry_u32;5747 ZigType *u32 = g->builtin_types.entry_u32;
5744 LLVMValueRef indices[] = {5748 LLVMValueRef indices[] = {
5745 LLVMConstNull(u32->type_ref),5749 LLVMConstNull(get_llvm_type(g, u32)),
5746 LLVMConstInt(u32->type_ref, index, false),5750 LLVMConstInt(get_llvm_type(g, u32), index, false),
5747 };5751 };
5748 return LLVMConstInBoundsGEP(base_ptr, indices, 2);5752 return LLVMConstInBoundsGEP(base_ptr, indices, 2);
5749 } else {5753 } else {
...@@ -5758,8 +5762,8 @@ static LLVMValueRef gen_const_ptr_struct_recursive(CodeGen *g, ConstExprValue *s...@@ -5758,8 +5762,8 @@ static LLVMValueRef gen_const_ptr_struct_recursive(CodeGen *g, ConstExprValue *s
57585762
5759 ZigType *u32 = g->builtin_types.entry_u32;5763 ZigType *u32 = g->builtin_types.entry_u32;
5760 LLVMValueRef indices[] = {5764 LLVMValueRef indices[] = {
5761 LLVMConstNull(u32->type_ref),5765 LLVMConstNull(get_llvm_type(g, u32)),
5762 LLVMConstInt(u32->type_ref, field_index, false),5766 LLVMConstInt(get_llvm_type(g, u32), field_index, false),
5763 };5767 };
5764 return LLVMConstInBoundsGEP(base_ptr, indices, 2);5768 return LLVMConstInBoundsGEP(base_ptr, indices, 2);
5765}5769}
...@@ -5770,8 +5774,8 @@ static LLVMValueRef gen_const_ptr_err_union_code_recursive(CodeGen *g, ConstExpr...@@ -5770,8 +5774,8 @@ static LLVMValueRef gen_const_ptr_err_union_code_recursive(CodeGen *g, ConstExpr
57705774
5771 ZigType *u32 = g->builtin_types.entry_u32;5775 ZigType *u32 = g->builtin_types.entry_u32;
5772 LLVMValueRef indices[] = {5776 LLVMValueRef indices[] = {
5773 LLVMConstNull(u32->type_ref),5777 LLVMConstNull(get_llvm_type(g, u32)),
5774 LLVMConstInt(u32->type_ref, err_union_err_index, false),5778 LLVMConstInt(get_llvm_type(g, u32), err_union_err_index, false),
5775 };5779 };
5776 return LLVMConstInBoundsGEP(base_ptr, indices, 2);5780 return LLVMConstInBoundsGEP(base_ptr, indices, 2);
5777}5781}
...@@ -5782,8 +5786,8 @@ static LLVMValueRef gen_const_ptr_err_union_payload_recursive(CodeGen *g, ConstE...@@ -5782,8 +5786,8 @@ static LLVMValueRef gen_const_ptr_err_union_payload_recursive(CodeGen *g, ConstE
57825786
5783 ZigType *u32 = g->builtin_types.entry_u32;5787 ZigType *u32 = g->builtin_types.entry_u32;
5784 LLVMValueRef indices[] = {5788 LLVMValueRef indices[] = {
5785 LLVMConstNull(u32->type_ref),5789 LLVMConstNull(get_llvm_type(g, u32)),
5786 LLVMConstInt(u32->type_ref, err_union_payload_index, false),5790 LLVMConstInt(get_llvm_type(g, u32), err_union_payload_index, false),
5787 };5791 };
5788 return LLVMConstInBoundsGEP(base_ptr, indices, 2);5792 return LLVMConstInBoundsGEP(base_ptr, indices, 2);
5789}5793}
...@@ -5794,8 +5798,8 @@ static LLVMValueRef gen_const_ptr_optional_payload_recursive(CodeGen *g, ConstEx...@@ -5794,8 +5798,8 @@ static LLVMValueRef gen_const_ptr_optional_payload_recursive(CodeGen *g, ConstEx
57945798
5795 ZigType *u32 = g->builtin_types.entry_u32;5799 ZigType *u32 = g->builtin_types.entry_u32;
5796 LLVMValueRef indices[] = {5800 LLVMValueRef indices[] = {
5797 LLVMConstNull(u32->type_ref),5801 LLVMConstNull(get_llvm_type(g, u32)),
5798 LLVMConstInt(u32->type_ref, maybe_child_index, false),5802 LLVMConstInt(get_llvm_type(g, u32), maybe_child_index, false),
5799 };5803 };
5800 return LLVMConstInBoundsGEP(base_ptr, indices, 2);5804 return LLVMConstInBoundsGEP(base_ptr, indices, 2);
5801}5805}
...@@ -5806,8 +5810,8 @@ static LLVMValueRef gen_const_ptr_union_recursive(CodeGen *g, ConstExprValue *un...@@ -5806,8 +5810,8 @@ static LLVMValueRef gen_const_ptr_union_recursive(CodeGen *g, ConstExprValue *un
58065810
5807 ZigType *u32 = g->builtin_types.entry_u32;5811 ZigType *u32 = g->builtin_types.entry_u32;
5808 LLVMValueRef indices[] = {5812 LLVMValueRef indices[] = {
5809 LLVMConstNull(u32->type_ref),5813 LLVMConstNull(get_llvm_type(g, u32)),
5810 LLVMConstInt(u32->type_ref, 0, false), // TODO test const union with more aligned tag type than payload5814 LLVMConstInt(get_llvm_type(g, u32), 0, false), // TODO test const union with more aligned tag type than payload
5811 };5815 };
5812 return LLVMConstInBoundsGEP(base_ptr, indices, 2);5816 return LLVMConstInBoundsGEP(base_ptr, indices, 2);
5813}5817}
...@@ -5823,7 +5827,7 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con...@@ -5823,7 +5827,7 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con
5823 }5827 }
58245828
5825 ZigType *type_entry = const_val->type;5829 ZigType *type_entry = const_val->type;
5826 assert(!type_entry->zero_bits);5830 assert(type_has_bits(type_entry));
5827 switch (type_entry->id) {5831 switch (type_entry->id) {
5828 case ZigTypeIdInvalid:5832 case ZigTypeIdInvalid:
5829 case ZigTypeIdMetaType:5833 case ZigTypeIdMetaType:
...@@ -5866,7 +5870,7 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con...@@ -5866,7 +5870,7 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con
5866 case ZigTypeIdPromise:5870 case ZigTypeIdPromise:
5867 {5871 {
5868 LLVMValueRef ptr_val = gen_const_val(g, const_val, "");5872 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);
5870 return LLVMConstZExt(ptr_size_int_val, big_int_type_ref);5874 return LLVMConstZExt(ptr_size_int_val, big_int_type_ref);
5871 }5875 }
5872 case ZigTypeIdArray: {5876 case ZigTypeIdArray: {
...@@ -5933,8 +5937,8 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con...@@ -5933,8 +5937,8 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con
59335937
5934// We have this because union constants can't be represented by the official union type,5938// We have this because union constants can't be represented by the official union type,
5935// and this property bubbles up in whatever aggregate type contains a union constant5939// 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) {5940static bool is_llvm_value_unnamed_type(CodeGen *g, ZigType *type_entry, LLVMValueRef val) {
5937 return LLVMTypeOf(val) != type_entry->type_ref;5941 return LLVMTypeOf(val) != get_llvm_type(g, type_entry);
5938}5942}
59395943
5940static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, const char *name) {5944static 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...@@ -5948,7 +5952,8 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
5948 ConstExprValue *pointee = const_val->data.x_ptr.data.ref.pointee;5952 ConstExprValue *pointee = const_val->data.x_ptr.data.ref.pointee;
5949 render_const_val(g, pointee, "");5953 render_const_val(g, pointee, "");
5950 render_const_val_global(g, pointee, "");5954 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));
5952 return const_val->global_refs->llvm_value;5957 return const_val->global_refs->llvm_value;
5953 }5958 }
5954 case ConstPtrSpecialBaseArray:5959 case ConstPtrSpecialBaseArray:
...@@ -5959,13 +5964,13 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con...@@ -5959,13 +5964,13 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
5959 if (!type_has_bits(array_const_val->type)) {5964 if (!type_has_bits(array_const_val->type)) {
5960 // make this a null pointer5965 // make this a null pointer
5961 ZigType *usize = g->builtin_types.entry_usize;5966 ZigType *usize = g->builtin_types.entry_usize;
5962 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->type_ref),5967 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type),
5963 const_val->type->type_ref);5968 get_llvm_type(g, const_val->type));
5964 return const_val->global_refs->llvm_value;5969 return const_val->global_refs->llvm_value;
5965 }5970 }
5966 size_t elem_index = const_val->data.x_ptr.data.base_array.elem_index;5971 size_t elem_index = const_val->data.x_ptr.data.base_array.elem_index;
5967 LLVMValueRef uncasted_ptr_val = gen_const_ptr_array_recursive(g, array_const_val, elem_index);5972 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));
5969 const_val->global_refs->llvm_value = ptr_val;5974 const_val->global_refs->llvm_value = ptr_val;
5970 return ptr_val;5975 return ptr_val;
5971 }5976 }
...@@ -5977,15 +5982,15 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con...@@ -5977,15 +5982,15 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
5977 if (!type_has_bits(struct_const_val->type)) {5982 if (!type_has_bits(struct_const_val->type)) {
5978 // make this a null pointer5983 // make this a null pointer
5979 ZigType *usize = g->builtin_types.entry_usize;5984 ZigType *usize = g->builtin_types.entry_usize;
5980 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->type_ref),5985 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type),
5981 const_val->type->type_ref);5986 get_llvm_type(g, const_val->type));
5982 return const_val->global_refs->llvm_value;5987 return const_val->global_refs->llvm_value;
5983 }5988 }
5984 size_t src_field_index = const_val->data.x_ptr.data.base_struct.field_index;5989 size_t src_field_index = const_val->data.x_ptr.data.base_struct.field_index;
5985 size_t gen_field_index = struct_const_val->type->data.structure.fields[src_field_index].gen_index;5990 size_t gen_field_index = struct_const_val->type->data.structure.fields[src_field_index].gen_index;
5986 LLVMValueRef uncasted_ptr_val = gen_const_ptr_struct_recursive(g, struct_const_val,5991 LLVMValueRef uncasted_ptr_val = gen_const_ptr_struct_recursive(g, struct_const_val,
5987 gen_field_index);5992 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));
5989 const_val->global_refs->llvm_value = ptr_val;5994 const_val->global_refs->llvm_value = ptr_val;
5990 return ptr_val;5995 return ptr_val;
5991 }5996 }
...@@ -5997,12 +6002,12 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con...@@ -5997,12 +6002,12 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
5997 if (!type_has_bits(err_union_const_val->type)) {6002 if (!type_has_bits(err_union_const_val->type)) {
5998 // make this a null pointer6003 // make this a null pointer
5999 ZigType *usize = g->builtin_types.entry_usize;6004 ZigType *usize = g->builtin_types.entry_usize;
6000 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->type_ref),6005 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type),
6001 const_val->type->type_ref);6006 get_llvm_type(g, const_val->type));
6002 return const_val->global_refs->llvm_value;6007 return const_val->global_refs->llvm_value;
6003 }6008 }
6004 LLVMValueRef uncasted_ptr_val = gen_const_ptr_err_union_code_recursive(g, err_union_const_val);6009 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));
6006 const_val->global_refs->llvm_value = ptr_val;6011 const_val->global_refs->llvm_value = ptr_val;
6007 return ptr_val;6012 return ptr_val;
6008 }6013 }
...@@ -6011,15 +6016,15 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con...@@ -6011,15 +6016,15 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
6011 assert(const_val->global_refs != nullptr);6016 assert(const_val->global_refs != nullptr);
6012 ConstExprValue *err_union_const_val = const_val->data.x_ptr.data.base_err_union_payload.err_union_val;6017 ConstExprValue *err_union_const_val = const_val->data.x_ptr.data.base_err_union_payload.err_union_val;
6013 assert(err_union_const_val->type->id == ZigTypeIdErrorUnion);6018 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)) {
6015 // make this a null pointer6020 // make this a null pointer
6016 ZigType *usize = g->builtin_types.entry_usize;6021 ZigType *usize = g->builtin_types.entry_usize;
6017 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->type_ref),6022 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type),
6018 const_val->type->type_ref);6023 get_llvm_type(g, const_val->type));
6019 return const_val->global_refs->llvm_value;6024 return const_val->global_refs->llvm_value;
6020 }6025 }
6021 LLVMValueRef uncasted_ptr_val = gen_const_ptr_err_union_payload_recursive(g, err_union_const_val);6026 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));
6023 const_val->global_refs->llvm_value = ptr_val;6028 const_val->global_refs->llvm_value = ptr_val;
6024 return ptr_val;6029 return ptr_val;
6025 }6030 }
...@@ -6028,15 +6033,15 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con...@@ -6028,15 +6033,15 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
6028 assert(const_val->global_refs != nullptr);6033 assert(const_val->global_refs != nullptr);
6029 ConstExprValue *optional_const_val = const_val->data.x_ptr.data.base_optional_payload.optional_val;6034 ConstExprValue *optional_const_val = const_val->data.x_ptr.data.base_optional_payload.optional_val;
6030 assert(optional_const_val->type->id == ZigTypeIdOptional);6035 assert(optional_const_val->type->id == ZigTypeIdOptional);
6031 if (optional_const_val->type->zero_bits) {6036 if (!type_has_bits(optional_const_val->type)) {
6032 // make this a null pointer6037 // make this a null pointer
6033 ZigType *usize = g->builtin_types.entry_usize;6038 ZigType *usize = g->builtin_types.entry_usize;
6034 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->type_ref),6039 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type),
6035 const_val->type->type_ref);6040 get_llvm_type(g, const_val->type));
6036 return const_val->global_refs->llvm_value;6041 return const_val->global_refs->llvm_value;
6037 }6042 }
6038 LLVMValueRef uncasted_ptr_val = gen_const_ptr_optional_payload_recursive(g, optional_const_val);6043 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));
6040 const_val->global_refs->llvm_value = ptr_val;6045 const_val->global_refs->llvm_value = ptr_val;
6041 return ptr_val;6046 return ptr_val;
6042 }6047 }
...@@ -6046,50 +6051,51 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con...@@ -6046,50 +6051,51 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
6046 uint64_t addr_value = const_val->data.x_ptr.data.hard_coded_addr.addr;6051 uint64_t addr_value = const_val->data.x_ptr.data.hard_coded_addr.addr;
6047 ZigType *usize = g->builtin_types.entry_usize;6052 ZigType *usize = g->builtin_types.entry_usize;
6048 const_val->global_refs->llvm_value = LLVMConstIntToPtr(6053 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));
6050 return const_val->global_refs->llvm_value;6055 return const_val->global_refs->llvm_value;
6051 }6056 }
6052 case ConstPtrSpecialFunction:6057 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));
6054 case ConstPtrSpecialNull:6060 case ConstPtrSpecialNull:
6055 return LLVMConstNull(const_val->type->type_ref);6061 return LLVMConstNull(get_llvm_type(g, const_val->type));
6056 }6062 }
6057 zig_unreachable();6063 zig_unreachable();
6058}6064}
60596065
6060static LLVMValueRef gen_const_val_err_set(CodeGen *g, ConstExprValue *const_val, const char *name) {6066static LLVMValueRef gen_const_val_err_set(CodeGen *g, ConstExprValue *const_val, const char *name) {
6061 uint64_t value = (const_val->data.x_err_set == nullptr) ? 0 : const_val->data.x_err_set->value;6067 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);
6063}6069}
60646070
6065static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const char *name) {6071static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const char *name) {
6066 Error err;6072 Error err;
60676073
6068 ZigType *type_entry = const_val->type;6074 ZigType *type_entry = const_val->type;
6069 assert(!type_entry->zero_bits);6075 assert(type_has_bits(type_entry));
60706076
6071 switch (const_val->special) {6077 switch (const_val->special) {
6072 case ConstValSpecialRuntime:6078 case ConstValSpecialRuntime:
6073 zig_unreachable();6079 zig_unreachable();
6074 case ConstValSpecialUndef:6080 case ConstValSpecialUndef:
6075 return LLVMGetUndef(type_entry->type_ref);6081 return LLVMGetUndef(get_llvm_type(g, type_entry));
6076 case ConstValSpecialStatic:6082 case ConstValSpecialStatic:
6077 break;6083 break;
6078 }6084 }
60796085
6080 switch (type_entry->id) {6086 switch (type_entry->id) {
6081 case ZigTypeIdInt:6087 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);
6083 case ZigTypeIdErrorSet:6089 case ZigTypeIdErrorSet:
6084 return gen_const_val_err_set(g, const_val, name);6090 return gen_const_val_err_set(g, const_val, name);
6085 case ZigTypeIdFloat:6091 case ZigTypeIdFloat:
6086 switch (type_entry->data.floating.bit_count) {6092 switch (type_entry->data.floating.bit_count) {
6087 case 16:6093 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));
6089 case 32:6095 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);
6091 case 64:6097 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);
6093 case 128:6099 case 128:
6094 {6100 {
6095 // TODO make sure this is correct on big endian targets too6101 // 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...@@ -6097,7 +6103,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
6097 memcpy(buf, &const_val->data.x_f128, 16);6103 memcpy(buf, &const_val->data.x_f128, 16);
6098 LLVMValueRef as_int = LLVMConstIntOfArbitraryPrecision(LLVMInt128Type(), 2,6104 LLVMValueRef as_int = LLVMConstIntOfArbitraryPrecision(LLVMInt128Type(), 2,
6099 (uint64_t*)buf);6105 (uint64_t*)buf);
6100 return LLVMConstBitCast(as_int, type_entry->type_ref);6106 return LLVMConstBitCast(as_int, get_llvm_type(g, type_entry));
6101 }6107 }
6102 default:6108 default:
6103 zig_unreachable();6109 zig_unreachable();
...@@ -6125,9 +6131,9 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c...@@ -6125,9 +6131,9 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
6125 child_val = gen_const_val(g, const_val->data.x_optional, "");6131 child_val = gen_const_val(g, const_val->data.x_optional, "");
6126 maybe_val = LLVMConstAllOnes(LLVMInt1Type());6132 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);
6129 } else {6135 } else {
6130 child_val = LLVMGetUndef(child_type->type_ref);6136 child_val = LLVMGetUndef(get_llvm_type(g, child_type));
6131 maybe_val = LLVMConstNull(LLVMInt1Type());6137 maybe_val = LLVMConstNull(LLVMInt1Type());
61326138
6133 make_unnamed_struct = false;6139 make_unnamed_struct = false;
...@@ -6139,7 +6145,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c...@@ -6139,7 +6145,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
6139 if (make_unnamed_struct) {6145 if (make_unnamed_struct) {
6140 return LLVMConstStruct(fields, 2, false);6146 return LLVMConstStruct(fields, 2, false);
6141 } else {6147 } else {
6142 return LLVMConstNamedStruct(type_entry->type_ref, fields, 2);6148 return LLVMConstNamedStruct(get_llvm_type(g, type_entry), fields, 2);
6143 }6149 }
6144 }6150 }
6145 }6151 }
...@@ -6168,10 +6174,10 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c...@@ -6168,10 +6174,10 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
6168 ConstExprValue *field_val = &const_val->data.x_struct.fields[src_field_index];6174 ConstExprValue *field_val = &const_val->data.x_struct.fields[src_field_index];
6169 LLVMValueRef val = gen_const_val(g, field_val, "");6175 LLVMValueRef val = gen_const_val(g, field_val, "");
6170 fields[type_struct_field->gen_index] = val;6176 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);
6172 } else {6178 } else {
6173 bool is_big_endian = g->is_big_endian; // TODO get endianness from struct type6179 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),
6175 (unsigned)type_struct_field->gen_index);6181 (unsigned)type_struct_field->gen_index);
6176 LLVMValueRef val = LLVMConstInt(big_int_type_ref, 0, false);6182 LLVMValueRef val = LLVMConstInt(big_int_type_ref, 0, false);
6177 size_t used_bits = 0;6183 size_t used_bits = 0;
...@@ -6216,14 +6222,14 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c...@@ -6216,14 +6222,14 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
62166222
6217 LLVMValueRef val = gen_const_val(g, field_val, "");6223 LLVMValueRef val = gen_const_val(g, field_val, "");
6218 fields[type_struct_field->gen_index] = val;6224 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);
6220 }6226 }
6221 }6227 }
6222 if (make_unnamed_struct) {6228 if (make_unnamed_struct) {
6223 return LLVMConstStruct(fields, type_entry->data.structure.gen_field_count,6229 return LLVMConstStruct(fields, type_entry->data.structure.gen_field_count,
6224 type_entry->data.structure.layout == ContainerLayoutPacked);6230 type_entry->data.structure.layout == ContainerLayoutPacked);
6225 } else {6231 } 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);
6227 }6233 }
6228 }6234 }
6229 case ZigTypeIdArray:6235 case ZigTypeIdArray:
...@@ -6231,16 +6237,16 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c...@@ -6231,16 +6237,16 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
6231 uint64_t len = type_entry->data.array.len;6237 uint64_t len = type_entry->data.array.len;
6232 switch (const_val->data.x_array.special) {6238 switch (const_val->data.x_array.special) {
6233 case ConstArraySpecialUndef:6239 case ConstArraySpecialUndef:
6234 return LLVMGetUndef(type_entry->type_ref);6240 return LLVMGetUndef(get_llvm_type(g, type_entry));
6235 case ConstArraySpecialNone: {6241 case ConstArraySpecialNone: {
6236 LLVMValueRef *values = allocate<LLVMValueRef>(len);6242 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);
6238 bool make_unnamed_struct = false;6244 bool make_unnamed_struct = false;
6239 for (uint64_t i = 0; i < len; i += 1) {6245 for (uint64_t i = 0; i < len; i += 1) {
6240 ConstExprValue *elem_value = &const_val->data.x_array.data.s_none.elements[i];6246 ConstExprValue *elem_value = &const_val->data.x_array.data.s_none.elements[i];
6241 LLVMValueRef val = gen_const_val(g, elem_value, "");6247 LLVMValueRef val = gen_const_val(g, elem_value, "");
6242 values[i] = val;6248 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);
6244 }6250 }
6245 if (make_unnamed_struct) {6251 if (make_unnamed_struct) {
6246 return LLVMConstStruct(values, len, true);6252 return LLVMConstStruct(values, len, true);
...@@ -6259,7 +6265,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c...@@ -6259,7 +6265,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
6259 uint32_t len = type_entry->data.vector.len;6265 uint32_t len = type_entry->data.vector.len;
6260 switch (const_val->data.x_array.special) {6266 switch (const_val->data.x_array.special) {
6261 case ConstArraySpecialUndef:6267 case ConstArraySpecialUndef:
6262 return LLVMGetUndef(type_entry->type_ref);6268 return LLVMGetUndef(get_llvm_type(g, type_entry));
6263 case ConstArraySpecialNone: {6269 case ConstArraySpecialNone: {
6264 LLVMValueRef *values = allocate<LLVMValueRef>(len);6270 LLVMValueRef *values = allocate<LLVMValueRef>(len);
6265 for (uint64_t i = 0; i < len; i += 1) {6271 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...@@ -6273,7 +6279,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
6273 assert(buf_len(buf) == len);6279 assert(buf_len(buf) == len);
6274 LLVMValueRef *values = allocate<LLVMValueRef>(len);6280 LLVMValueRef *values = allocate<LLVMValueRef>(len);
6275 for (uint64_t i = 0; i < len; i += 1) {6281 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);
6277 }6283 }
6278 return LLVMConstVector(values, len);6284 return LLVMConstVector(values, len);
6279 }6285 }
...@@ -6282,13 +6288,19 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c...@@ -6282,13 +6288,19 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
6282 }6288 }
6283 case ZigTypeIdUnion:6289 case ZigTypeIdUnion:
6284 {6290 {
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
6287 if (type_entry->data.unionation.gen_field_count == 0) {6299 if (type_entry->data.unionation.gen_field_count == 0) {
6288 if (type_entry->data.unionation.tag_type == nullptr) {6300 if (type_entry->data.unionation.tag_type == nullptr) {
6289 return nullptr;6301 return nullptr;
6290 } else {6302 } 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),
6292 &const_val->data.x_union.tag);6304 &const_val->data.x_union.tag);
6293 }6305 }
6294 }6306 }
...@@ -6298,15 +6310,16 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c...@@ -6298,15 +6310,16 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
6298 ConstExprValue *payload_value = const_val->data.x_union.payload;6310 ConstExprValue *payload_value = const_val->data.x_union.payload;
6299 if (payload_value == nullptr || !type_has_bits(payload_value->type)) {6311 if (payload_value == nullptr || !type_has_bits(payload_value->type)) {
6300 if (type_entry->data.unionation.gen_tag_index == SIZE_MAX)6312 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
6303 union_value_ref = LLVMGetUndef(union_type_ref);6315 union_value_ref = LLVMGetUndef(union_type_ref);
6304 make_unnamed_struct = false;6316 make_unnamed_struct = false;
6305 } else {6317 } else {
6306 uint64_t field_type_bytes = LLVMStoreSizeOfType(g->target_data_ref, payload_value->type->type_ref);6318 uint64_t field_type_bytes = LLVMStoreSizeOfType(g->target_data_ref,
6307 uint64_t pad_bytes = type_entry->data.unionation.union_size_bytes - field_type_bytes;6319 get_llvm_type(g, payload_value->type));
6320 uint64_t pad_bytes = type_entry->data.unionation.union_abi_size - field_type_bytes;
6308 LLVMValueRef correctly_typed_value = gen_const_val(g, payload_value, "");6321 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) ||
6310 payload_value->type != type_entry->data.unionation.most_aligned_union_member;6323 payload_value->type != type_entry->data.unionation.most_aligned_union_member;
63116324
6312 {6325 {
...@@ -6329,7 +6342,8 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c...@@ -6329,7 +6342,8 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
6329 }6342 }
6330 }6343 }
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),
6333 &const_val->data.x_union.tag);6347 &const_val->data.x_union.tag);
63346348
6335 LLVMValueRef fields[3];6349 LLVMValueRef fields[3];
...@@ -6341,7 +6355,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c...@@ -6341,7 +6355,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
6341 uint64_t last_field_offset = LLVMOffsetOfElement(g->target_data_ref, LLVMTypeOf(result), 1);6355 uint64_t last_field_offset = LLVMOffsetOfElement(g->target_data_ref, LLVMTypeOf(result), 1);
6342 uint64_t end_offset = last_field_offset +6356 uint64_t end_offset = last_field_offset +
6343 LLVMStoreSizeOfType(g->target_data_ref, LLVMTypeOf(fields[1]));6357 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));
6345 unsigned pad_sz = expected_sz - end_offset;6359 unsigned pad_sz = expected_sz - end_offset;
6346 if (pad_sz != 0) {6360 if (pad_sz != 0) {
6347 fields[2] = LLVMGetUndef(LLVMArrayType(LLVMInt8Type(), pad_sz));6361 fields[2] = LLVMGetUndef(LLVMArrayType(LLVMInt8Type(), pad_sz));
...@@ -6351,21 +6365,21 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c...@@ -6351,21 +6365,21 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
6351 assert(actual_sz == expected_sz);6365 assert(actual_sz == expected_sz);
6352 return result;6366 return result;
6353 } else {6367 } else {
6354 return LLVMConstNamedStruct(type_entry->type_ref, fields, 2);6368 return LLVMConstNamedStruct(get_llvm_type(g, type_entry), fields, 2);
6355 }6369 }
63566370
6357 }6371 }
63586372
6359 case ZigTypeIdEnum:6373 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);
6361 case ZigTypeIdFn:6375 case ZigTypeIdFn:
6362 if (const_val->data.x_ptr.special == ConstPtrSpecialFunction) {6376 if (const_val->data.x_ptr.special == ConstPtrSpecialFunction) {
6363 assert(const_val->data.x_ptr.mut == ConstPtrMutComptimeConst);6377 assert(const_val->data.x_ptr.mut == ConstPtrMutComptimeConst);
6364 return fn_llvm_value(g, const_val->data.x_ptr.data.fn.fn_entry);6378 return fn_llvm_value(g, const_val->data.x_ptr.data.fn.fn_entry);
6365 } else if (const_val->data.x_ptr.special == ConstPtrSpecialHardCodedAddr) {6379 } 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;
6367 uint64_t addr = const_val->data.x_ptr.data.hard_coded_addr.addr;6381 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));
6369 } else {6383 } else {
6370 zig_unreachable();6384 zig_unreachable();
6371 }6385 }
...@@ -6379,7 +6393,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c...@@ -6379,7 +6393,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
6379 assert(type_has_bits(err_set_type));6393 assert(type_has_bits(err_set_type));
6380 ErrorTableEntry *err_set = const_val->data.x_err_union.error_set->data.x_err_set;6394 ErrorTableEntry *err_set = const_val->data.x_err_union.error_set->data.x_err_set;
6381 uint64_t value = (err_set == nullptr) ? 0 : err_set->value;6395 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);
6383 } else if (!type_has_bits(err_set_type)) {6397 } else if (!type_has_bits(err_set_type)) {
6384 assert(type_has_bits(payload_type));6398 assert(type_has_bits(payload_type));
6385 return gen_const_val(g, const_val->data.x_err_union.payload, "");6399 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...@@ -6389,17 +6403,17 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
6389 bool make_unnamed_struct;6403 bool make_unnamed_struct;
6390 ErrorTableEntry *err_set = const_val->data.x_err_union.error_set->data.x_err_set;6404 ErrorTableEntry *err_set = const_val->data.x_err_union.error_set->data.x_err_set;
6391 if (err_set != nullptr) {6405 if (err_set != nullptr) {
6392 err_tag_value = LLVMConstInt(g->err_tag_type->type_ref, err_set->value, false);6406 err_tag_value = LLVMConstInt(get_llvm_type(g, g->err_tag_type), err_set->value, false);
6393 err_payload_value = LLVMConstNull(payload_type->type_ref);6407 err_payload_value = LLVMConstNull(get_llvm_type(g, payload_type));
6394 make_unnamed_struct = false;6408 make_unnamed_struct = false;
6395 } else {6409 } 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));
6397 ConstExprValue *payload_val = const_val->data.x_err_union.payload;6411 ConstExprValue *payload_val = const_val->data.x_err_union.payload;
6398 err_payload_value = gen_const_val(g, payload_val, "");6412 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);
6400 }6414 }
6401 if (make_unnamed_struct) {6415 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);
6403 uint64_t err_sz = LLVMStoreSizeOfType(g->target_data_ref, LLVMTypeOf(err_tag_value));6417 uint64_t err_sz = LLVMStoreSizeOfType(g->target_data_ref, LLVMTypeOf(err_tag_value));
6404 unsigned pad_sz = payload_off - err_sz;6418 unsigned pad_sz = payload_off - err_sz;
6405 if (pad_sz == 0) {6419 if (pad_sz == 0) {
...@@ -6421,7 +6435,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c...@@ -6421,7 +6435,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
6421 err_tag_value,6435 err_tag_value,
6422 err_payload_value,6436 err_payload_value,
6423 };6437 };
6424 return LLVMConstNamedStruct(type_entry->type_ref, fields, 2);6438 return LLVMConstNamedStruct(get_llvm_type(g, type_entry), fields, 2);
6425 }6439 }
6426 }6440 }
6427 }6441 }
...@@ -6460,7 +6474,8 @@ static void render_const_val_global(CodeGen *g, ConstExprValue *const_val, const...@@ -6460,7 +6474,8 @@ static void render_const_val_global(CodeGen *g, ConstExprValue *const_val, const
6460 const_val->global_refs = allocate<ConstGlobalRefs>(1);6474 const_val->global_refs = allocate<ConstGlobalRefs>(1);
64616475
6462 if (!const_val->global_refs->llvm_global) {6476 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);
6464 LLVMValueRef global_value = LLVMAddGlobal(g->module, type_ref, name);6479 LLVMValueRef global_value = LLVMAddGlobal(g->module, type_ref, name);
6465 LLVMSetLinkage(global_value, LLVMInternalLinkage);6480 LLVMSetLinkage(global_value, LLVMInternalLinkage);
6466 LLVMSetGlobalConstant(global_value, true);6481 LLVMSetGlobalConstant(global_value, true);
...@@ -6486,7 +6501,7 @@ static void generate_error_name_table(CodeGen *g) {...@@ -6486,7 +6501,7 @@ static void generate_error_name_table(CodeGen *g) {
6486 ZigType *str_type = get_slice_type(g, u8_ptr_type);6501 ZigType *str_type = get_slice_type(g, u8_ptr_type);
64876502
6488 LLVMValueRef *values = allocate<LLVMValueRef>(g->errors_by_index.length);6503 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));
6490 for (size_t i = 1; i < g->errors_by_index.length; i += 1) {6505 for (size_t i = 1; i < g->errors_by_index.length; i += 1) {
6491 ErrorTableEntry *err_entry = g->errors_by_index.at(i);6506 ErrorTableEntry *err_entry = g->errors_by_index.at(i);
6492 Buf *name = &err_entry->name;6507 Buf *name = &err_entry->name;
...@@ -6502,13 +6517,13 @@ static void generate_error_name_table(CodeGen *g) {...@@ -6502,13 +6517,13 @@ static void generate_error_name_table(CodeGen *g) {
6502 LLVMSetAlignment(str_global, LLVMABIAlignmentOfType(g->target_data_ref, LLVMTypeOf(str_init)));6517 LLVMSetAlignment(str_global, LLVMABIAlignmentOfType(g->target_data_ref, LLVMTypeOf(str_init)));
65036518
6504 LLVMValueRef fields[] = {6519 LLVMValueRef fields[] = {
6505 LLVMConstBitCast(str_global, u8_ptr_type->type_ref),6520 LLVMConstBitCast(str_global, get_llvm_type(g, u8_ptr_type)),
6506 LLVMConstInt(g->builtin_types.entry_usize->type_ref, buf_len(name), false),6521 LLVMConstInt(g->builtin_types.entry_usize->llvm_type, buf_len(name), false),
6507 };6522 };
6508 values[i] = LLVMConstNamedStruct(str_type->type_ref, fields, 2);6523 values[i] = LLVMConstNamedStruct(get_llvm_type(g, str_type), fields, 2);
6509 }6524 }
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
6513 g->err_name_table = LLVMAddGlobal(g->module, LLVMTypeOf(err_name_table_init),6528 g->err_name_table = LLVMAddGlobal(g->module, LLVMTypeOf(err_name_table_init),
6514 buf_ptr(get_mangled_name(g, buf_create_from_str("__zig_err_name_table"), false)));6529 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,...@@ -6547,7 +6562,7 @@ static void gen_global_var(CodeGen *g, ZigVar *var, LLVMValueRef init_val,
6547 ZigLLVMCreateGlobalVariable(g->dbuilder, get_di_scope(g, var->parent_scope), buf_ptr(&var->name),6562 ZigLLVMCreateGlobalVariable(g->dbuilder, get_di_scope(g, var->parent_scope), buf_ptr(&var->name),
6548 buf_ptr(&var->name), import->data.structure.root_struct->di_file,6563 buf_ptr(&var->name), import->data.structure.root_struct->di_file,
6549 (unsigned)(var->decl_node->line + 1),6564 (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
6552 // TODO ^^ make an actual global variable6567 // TODO ^^ make an actual global variable
6553}6568}
...@@ -6588,28 +6603,6 @@ static LLVMLinkage var_linkage_to_llvm(VarLinkage var_linkage) {...@@ -6588,28 +6603,6 @@ static LLVMLinkage var_linkage_to_llvm(VarLinkage var_linkage) {
6588static void do_code_gen(CodeGen *g) {6603static void do_code_gen(CodeGen *g) {
6589 assert(!g->errors.length);6604 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
6613 generate_error_name_table(g);6606 generate_error_name_table(g);
66146607
6615 // Generate module level variables6608 // Generate module level variables
...@@ -6646,7 +6639,7 @@ static void do_code_gen(CodeGen *g) {...@@ -6646,7 +6639,7 @@ static void do_code_gen(CodeGen *g) {
6646 bits_needed = 8;6639 bits_needed = 8;
6647 }6640 }
6648 ZigType *var_type = get_int_type(g, const_val->data.x_bigint.is_negative, bits_needed);6641 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);
6650 gen_global_var(g, var, init_val, var_type);6643 gen_global_var(g, var, init_val, var_type);
6651 continue;6644 continue;
6652 }6645 }
...@@ -6660,9 +6653,10 @@ static void do_code_gen(CodeGen *g) {...@@ -6660,9 +6653,10 @@ static void do_code_gen(CodeGen *g) {
6660 if (var->linkage == VarLinkageExternal) {6653 if (var->linkage == VarLinkageExternal) {
6661 LLVMValueRef existing_llvm_var = LLVMGetNamedGlobal(g->module, buf_ptr(&var->name));6654 LLVMValueRef existing_llvm_var = LLVMGetNamedGlobal(g->module, buf_ptr(&var->name));
6662 if (existing_llvm_var) {6655 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));
6664 } else {6658 } 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));
6666 // TODO debug info for the extern variable6660 // TODO debug info for the extern variable
66676661
6668 LLVMSetLinkage(global_value, var_linkage_to_llvm(var->linkage));6662 LLVMSetLinkage(global_value, var_linkage_to_llvm(var->linkage));
...@@ -6834,7 +6828,7 @@ static void do_code_gen(CodeGen *g) {...@@ -6834,7 +6828,7 @@ static void do_code_gen(CodeGen *g) {
68346828
6835 var->di_loc_var = ZigLLVMCreateAutoVariable(g->dbuilder, get_di_scope(g, var->parent_scope),6829 var->di_loc_var = ZigLLVMCreateAutoVariable(g->dbuilder, get_di_scope(g, var->parent_scope),
6836 buf_ptr(&var->name), import->data.structure.root_struct->di_file, (unsigned)(var->decl_node->line + 1),6830 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
6839 } else if (is_c_abi) {6833 } else if (is_c_abi) {
6840 fn_walk_var.data.vars.var = var;6834 fn_walk_var.data.vars.var = var;
...@@ -6859,7 +6853,7 @@ static void do_code_gen(CodeGen *g) {...@@ -6859,7 +6853,7 @@ static void do_code_gen(CodeGen *g) {
6859 var->di_loc_var = ZigLLVMCreateParameterVariable(g->dbuilder, get_di_scope(g, var->parent_scope),6853 var->di_loc_var = ZigLLVMCreateParameterVariable(g->dbuilder, get_di_scope(g, var->parent_scope),
6860 buf_ptr(&var->name), import->data.structure.root_struct->di_file,6854 buf_ptr(&var->name), import->data.structure.root_struct->di_file,
6861 (unsigned)(var->decl_node->line + 1),6855 (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));
6863 }6857 }
68646858
6865 }6859 }
...@@ -6870,7 +6864,7 @@ static void do_code_gen(CodeGen *g) {...@@ -6870,7 +6864,7 @@ static void do_code_gen(CodeGen *g) {
6870 ZigType *usize = g->builtin_types.entry_usize;6864 ZigType *usize = g->builtin_types.entry_usize;
6871 size_t index_field_index = g->stack_trace_type->data.structure.fields[0].gen_index;6865 size_t index_field_index = g->stack_trace_type->data.structure.fields[0].gen_index;
6872 LLVMValueRef index_field_ptr = LLVMBuildStructGEP(g->builder, g->cur_err_ret_trace_val_stack, (unsigned)index_field_index, "");6866 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
6875 size_t addresses_field_index = g->stack_trace_type->data.structure.fields[1].gen_index;6869 size_t addresses_field_index = g->stack_trace_type->data.structure.fields[1].gen_index;
6876 LLVMValueRef addresses_field_ptr = LLVMBuildStructGEP(g->builder, g->cur_err_ret_trace_val_stack, (unsigned)addresses_field_index, "");6870 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) {...@@ -6878,7 +6872,7 @@ static void do_code_gen(CodeGen *g) {
6878 ZigType *slice_type = g->stack_trace_type->data.structure.fields[1].type_entry;6872 ZigType *slice_type = g->stack_trace_type->data.structure.fields[1].type_entry;
6879 size_t ptr_field_index = slice_type->data.structure.fields[slice_ptr_index].gen_index;6873 size_t ptr_field_index = slice_type->data.structure.fields[slice_ptr_index].gen_index;
6880 LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, addresses_field_ptr, (unsigned)ptr_field_index, "");6874 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);
6882 LLVMValueRef indices[] = {zero, zero};6876 LLVMValueRef indices[] = {zero, zero};
6883 LLVMValueRef err_ret_array_val_elem0_ptr = LLVMBuildInBoundsGEP(g->builder, err_ret_array_val,6877 LLVMValueRef err_ret_array_val_elem0_ptr = LLVMBuildInBoundsGEP(g->builder, err_ret_array_val,
6884 indices, 2, "");6878 indices, 2, "");
...@@ -6887,7 +6881,7 @@ static void do_code_gen(CodeGen *g) {...@@ -6887,7 +6881,7 @@ static void do_code_gen(CodeGen *g) {
68876881
6888 size_t len_field_index = slice_type->data.structure.fields[slice_len_index].gen_index;6882 size_t len_field_index = slice_type->data.structure.fields[slice_len_index].gen_index;
6889 LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, addresses_field_ptr, (unsigned)len_field_index, "");6883 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));
6891 }6885 }
68926886
6893 // create debug variable declarations for parameters6887 // create debug variable declarations for parameters
...@@ -6997,50 +6991,60 @@ static const GlobalLinkageValue global_linkage_values[] = {...@@ -6997,50 +6991,60 @@ static const GlobalLinkageValue global_linkage_values[] = {
6997 {GlobalLinkageIdLinkOnce, "LinkOnce"},6991 {GlobalLinkageIdLinkOnce, "LinkOnce"},
6998};6992};
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
7000static void define_builtin_types(CodeGen *g) {7011static void define_builtin_types(CodeGen *g) {
7001 {7012 {
7002 // if this type is anywhere in the AST, we should never hit codegen.7013 // if this type is anywhere in the AST, we should never hit codegen.
7003 ZigType *entry = new_type_table_entry(ZigTypeIdInvalid);7014 ZigType *entry = new_type_table_entry(ZigTypeIdInvalid);
7004 buf_init_from_str(&entry->name, "(invalid)");7015 buf_init_from_str(&entry->name, "(invalid)");
7005 entry->zero_bits = true;
7006 g->builtin_types.entry_invalid = entry;7016 g->builtin_types.entry_invalid = entry;
7007 }7017 }
7008 {7018 {
7009 ZigType *entry = new_type_table_entry(ZigTypeIdComptimeFloat);7019 ZigType *entry = new_type_table_entry(ZigTypeIdComptimeFloat);
7010 buf_init_from_str(&entry->name, "comptime_float");7020 buf_init_from_str(&entry->name, "comptime_float");
7011 entry->zero_bits = true;
7012 g->builtin_types.entry_num_lit_float = entry;7021 g->builtin_types.entry_num_lit_float = entry;
7013 g->primitive_type_table.put(&entry->name, entry);7022 g->primitive_type_table.put(&entry->name, entry);
7014 }7023 }
7015 {7024 {
7016 ZigType *entry = new_type_table_entry(ZigTypeIdComptimeInt);7025 ZigType *entry = new_type_table_entry(ZigTypeIdComptimeInt);
7017 buf_init_from_str(&entry->name, "comptime_int");7026 buf_init_from_str(&entry->name, "comptime_int");
7018 entry->zero_bits = true;
7019 g->builtin_types.entry_num_lit_int = entry;7027 g->builtin_types.entry_num_lit_int = entry;
7020 g->primitive_type_table.put(&entry->name, entry);7028 g->primitive_type_table.put(&entry->name, entry);
7021 }7029 }
7022 {7030 {
7023 ZigType *entry = new_type_table_entry(ZigTypeIdEnumLiteral);7031 ZigType *entry = new_type_table_entry(ZigTypeIdEnumLiteral);
7024 buf_init_from_str(&entry->name, "(enum literal)");7032 buf_init_from_str(&entry->name, "(enum literal)");
7025 entry->zero_bits = true;
7026 g->builtin_types.entry_enum_literal = entry;7033 g->builtin_types.entry_enum_literal = entry;
7027 }7034 }
7028 {7035 {
7029 ZigType *entry = new_type_table_entry(ZigTypeIdUndefined);7036 ZigType *entry = new_type_table_entry(ZigTypeIdUndefined);
7030 buf_init_from_str(&entry->name, "(undefined)");7037 buf_init_from_str(&entry->name, "(undefined)");
7031 entry->zero_bits = true;
7032 g->builtin_types.entry_undef = entry;7038 g->builtin_types.entry_undef = entry;
7033 }7039 }
7034 {7040 {
7035 ZigType *entry = new_type_table_entry(ZigTypeIdNull);7041 ZigType *entry = new_type_table_entry(ZigTypeIdNull);
7036 buf_init_from_str(&entry->name, "(null)");7042 buf_init_from_str(&entry->name, "(null)");
7037 entry->zero_bits = true;
7038 g->builtin_types.entry_null = entry;7043 g->builtin_types.entry_null = entry;
7039 }7044 }
7040 {7045 {
7041 ZigType *entry = new_type_table_entry(ZigTypeIdArgTuple);7046 ZigType *entry = new_type_table_entry(ZigTypeIdArgTuple);
7042 buf_init_from_str(&entry->name, "(args)");7047 buf_init_from_str(&entry->name, "(args)");
7043 entry->zero_bits = true;
7044 g->builtin_types.entry_arg_tuple = entry;7048 g->builtin_types.entry_arg_tuple = entry;
7045 }7049 }
70467050
...@@ -7050,14 +7054,15 @@ static void define_builtin_types(CodeGen *g) {...@@ -7050,14 +7054,15 @@ static void define_builtin_types(CodeGen *g) {
7050 bool is_signed = info->is_signed;7054 bool is_signed = info->is_signed;
70517055
7052 ZigType *entry = new_type_table_entry(ZigTypeIdInt);7056 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
7055 buf_init_from_str(&entry->name, info->name);7062 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);7064 entry->llvm_di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
7058 entry->di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),7065 size_in_bits, is_signed ? ZigLLVMEncoding_DW_ATE_signed() : ZigLLVMEncoding_DW_ATE_unsigned());
7059 debug_size_in_bits,
7060 is_signed ? ZigLLVMEncoding_DW_ATE_signed() : ZigLLVMEncoding_DW_ATE_unsigned());
7061 entry->data.integral.is_signed = is_signed;7066 entry->data.integral.is_signed = is_signed;
7062 entry->data.integral.bit_count = size_in_bits;7067 entry->data.integral.bit_count = size_in_bits;
7063 g->primitive_type_table.put(&entry->name, entry);7068 g->primitive_type_table.put(&entry->name, entry);
...@@ -7067,12 +7072,13 @@ static void define_builtin_types(CodeGen *g) {...@@ -7067,12 +7072,13 @@ static void define_builtin_types(CodeGen *g) {
70677072
7068 {7073 {
7069 ZigType *entry = new_type_table_entry(ZigTypeIdBool);7074 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);
7071 buf_init_from_str(&entry->name, "bool");7079 buf_init_from_str(&entry->name, "bool");
7072 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, entry->type_ref);7080 entry->llvm_di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
7073 entry->di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),7081 entry->size_in_bits, ZigLLVMEncoding_DW_ATE_boolean());
7074 debug_size_in_bits,
7075 ZigLLVMEncoding_DW_ATE_boolean());
7076 g->builtin_types.entry_bool = entry;7082 g->builtin_types.entry_bool = entry;
7077 g->primitive_type_table.put(&entry->name, entry);7083 g->primitive_type_table.put(&entry->name, entry);
7078 }7084 }
...@@ -7081,7 +7087,10 @@ static void define_builtin_types(CodeGen *g) {...@@ -7081,7 +7087,10 @@ static void define_builtin_types(CodeGen *g) {
7081 bool is_signed = is_signed_list[sign_i];7087 bool is_signed = is_signed_list[sign_i];
70827088
7083 ZigType *entry = new_type_table_entry(ZigTypeIdInt);7089 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
7086 const char u_or_i = is_signed ? 'i' : 'u';7095 const char u_or_i = is_signed ? 'i' : 'u';
7087 buf_resize(&entry->name, 0);7096 buf_resize(&entry->name, 0);
...@@ -7090,9 +7099,8 @@ static void define_builtin_types(CodeGen *g) {...@@ -7090,9 +7099,8 @@ static void define_builtin_types(CodeGen *g) {
7090 entry->data.integral.is_signed = is_signed;7099 entry->data.integral.is_signed = is_signed;
7091 entry->data.integral.bit_count = g->pointer_size_bytes * 8;7100 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);7102 entry->llvm_di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
7094 entry->di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),7103 entry->size_in_bits,
7095 debug_size_in_bits,
7096 is_signed ? ZigLLVMEncoding_DW_ATE_signed() : ZigLLVMEncoding_DW_ATE_unsigned());7104 is_signed ? ZigLLVMEncoding_DW_ATE_signed() : ZigLLVMEncoding_DW_ATE_unsigned());
7097 g->primitive_type_table.put(&entry->name, entry);7105 g->primitive_type_table.put(&entry->name, entry);
70987106
...@@ -7103,23 +7111,6 @@ static void define_builtin_types(CodeGen *g) {...@@ -7103,23 +7111,6 @@ static void define_builtin_types(CodeGen *g) {
7103 }7111 }
7104 }7112 }
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 };
7123 add_fp_entry(g, "f16", 16, LLVMHalfType(), &g->builtin_types.entry_f16);7114 add_fp_entry(g, "f16", 16, LLVMHalfType(), &g->builtin_types.entry_f16);
7124 add_fp_entry(g, "f32", 32, LLVMFloatType(), &g->builtin_types.entry_f32);7115 add_fp_entry(g, "f32", 32, LLVMFloatType(), &g->builtin_types.entry_f32);
7125 add_fp_entry(g, "f64", 64, LLVMDoubleType(), &g->builtin_types.entry_f64);7116 add_fp_entry(g, "f64", 64, LLVMDoubleType(), &g->builtin_types.entry_f64);
...@@ -7128,10 +7119,9 @@ static void define_builtin_types(CodeGen *g) {...@@ -7128,10 +7119,9 @@ static void define_builtin_types(CodeGen *g) {
71287119
7129 {7120 {
7130 ZigType *entry = new_type_table_entry(ZigTypeIdVoid);7121 ZigType *entry = new_type_table_entry(ZigTypeIdVoid);
7131 entry->type_ref = LLVMVoidType();7122 entry->llvm_type = LLVMVoidType();
7132 entry->zero_bits = true;
7133 buf_init_from_str(&entry->name, "void");7123 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),
7135 0,7125 0,
7136 ZigLLVMEncoding_DW_ATE_unsigned());7126 ZigLLVMEncoding_DW_ATE_unsigned());
7137 g->builtin_types.entry_void = entry;7127 g->builtin_types.entry_void = entry;
...@@ -7139,17 +7129,15 @@ static void define_builtin_types(CodeGen *g) {...@@ -7139,17 +7129,15 @@ static void define_builtin_types(CodeGen *g) {
7139 }7129 }
7140 {7130 {
7141 ZigType *entry = new_type_table_entry(ZigTypeIdUnreachable);7131 ZigType *entry = new_type_table_entry(ZigTypeIdUnreachable);
7142 entry->type_ref = LLVMVoidType();7132 entry->llvm_type = LLVMVoidType();
7143 entry->zero_bits = true;
7144 buf_init_from_str(&entry->name, "noreturn");7133 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;
7146 g->builtin_types.entry_unreachable = entry;7135 g->builtin_types.entry_unreachable = entry;
7147 g->primitive_type_table.put(&entry->name, entry);7136 g->primitive_type_table.put(&entry->name, entry);
7148 }7137 }
7149 {7138 {
7150 ZigType *entry = new_type_table_entry(ZigTypeIdMetaType);7139 ZigType *entry = new_type_table_entry(ZigTypeIdMetaType);
7151 buf_init_from_str(&entry->name, "type");7140 buf_init_from_str(&entry->name, "type");
7152 entry->zero_bits = true;
7153 g->builtin_types.entry_type = entry;7141 g->builtin_types.entry_type = entry;
7154 g->primitive_type_table.put(&entry->name, entry);7142 g->primitive_type_table.put(&entry->name, entry);
7155 }7143 }
...@@ -7174,19 +7162,15 @@ static void define_builtin_types(CodeGen *g) {...@@ -7174,19 +7162,15 @@ static void define_builtin_types(CodeGen *g) {
7174 buf_init_from_str(&entry->name, "anyerror");7162 buf_init_from_str(&entry->name, "anyerror");
7175 entry->data.error_set.err_count = UINT32_MAX;7163 entry->data.error_set.err_count = UINT32_MAX;
71767164
7177 // TODO allow overriding this type and keep track of max value and emit an7165 // TODO https://github.com/ziglang/zig/issues/786
7178 // error if there are too many errors declared
7179 g->err_tag_type = g->builtin_types.entry_u16;7166 g->err_tag_type = g->builtin_types.entry_u16;
71807167
7181 g->builtin_types.entry_global_error_set = entry;7168 entry->size_in_bits = g->err_tag_type->size_in_bits;
7182 entry->type_ref = g->err_tag_type->type_ref;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,7172 g->builtin_types.entry_global_error_set = entry;
7185 ZigLLVMTag_DW_enumeration_type(), "anyerror",
7186 ZigLLVMCompileUnitToScope(g->compile_unit), nullptr, 0);
71877173
7188 // reserve index 0 to indicate no error
7189 g->err_enumerators.append(ZigLLVMCreateDebugEnumerator(g->dbuilder, "(none)", 0));
7190 g->errors_by_index.append(nullptr);7174 g->errors_by_index.append(nullptr);
71917175
7192 g->primitive_type_table.put(&entry->name, entry);7176 g->primitive_type_table.put(&entry->name, entry);
...@@ -7194,6 +7178,9 @@ static void define_builtin_types(CodeGen *g) {...@@ -7194,6 +7178,9 @@ static void define_builtin_types(CodeGen *g) {
7194 {7178 {
7195 ZigType *entry = get_promise_type(g, nullptr);7179 ZigType *entry = get_promise_type(g, nullptr);
7196 g->primitive_type_table.put(&entry->name, entry);7180 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;
7197 }7184 }
7198}7185}
71997186
src/ir.cpp+29-43
...@@ -6842,6 +6842,9 @@ static ZigType *get_error_set_union(CodeGen *g, ErrorTableEntry **errors, ZigTyp...@@ -6842,6 +6842,9 @@ static ZigType *get_error_set_union(CodeGen *g, ErrorTableEntry **errors, ZigTyp
6842 assert(set2->id == ZigTypeIdErrorSet);6842 assert(set2->id == ZigTypeIdErrorSet);
68436843
6844 ZigType *err_set_type = new_type_table_entry(ZigTypeIdErrorSet);6844 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;
6845 buf_resize(&err_set_type->name, 0);6848 buf_resize(&err_set_type->name, 0);
6846 buf_appendf(&err_set_type->name, "error{");6849 buf_appendf(&err_set_type->name, "error{");
68476850
...@@ -6857,8 +6860,6 @@ static ZigType *get_error_set_union(CodeGen *g, ErrorTableEntry **errors, ZigTyp...@@ -6857,8 +6860,6 @@ static ZigType *get_error_set_union(CodeGen *g, ErrorTableEntry **errors, ZigTyp
6857 }6860 }
6858 }6861 }
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;
6862 err_set_type->data.error_set.err_count = count;6863 err_set_type->data.error_set.err_count = count;
6863 err_set_type->data.error_set.errors = allocate<ErrorTableEntry *>(count);6864 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...@@ -6883,8 +6884,6 @@ static ZigType *get_error_set_union(CodeGen *g, ErrorTableEntry **errors, ZigTyp
68836884
6884 buf_appendf(&err_set_type->name, "}");6885 buf_appendf(&err_set_type->name, "}");
68856886
6886 g->error_di_types.append(&err_set_type->di_type);
6887
6888 return err_set_type;6887 return err_set_type;
68896888
6890}6889}
...@@ -6895,13 +6894,12 @@ static ZigType *make_err_set_with_one_item(CodeGen *g, Scope *parent_scope, AstN...@@ -6895,13 +6894,12 @@ static ZigType *make_err_set_with_one_item(CodeGen *g, Scope *parent_scope, AstN
6895 ZigType *err_set_type = new_type_table_entry(ZigTypeIdErrorSet);6894 ZigType *err_set_type = new_type_table_entry(ZigTypeIdErrorSet);
6896 buf_resize(&err_set_type->name, 0);6895 buf_resize(&err_set_type->name, 0);
6897 buf_appendf(&err_set_type->name, "error{%s}", buf_ptr(&err_entry->name));6896 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;6897 err_set_type->size_in_bits = g->builtin_types.entry_global_error_set->size_in_bits;
6899 err_set_type->di_type = g->builtin_types.entry_global_error_set->di_type;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;
6900 err_set_type->data.error_set.err_count = 1;6900 err_set_type->data.error_set.err_count = 1;
6901 err_set_type->data.error_set.errors = allocate<ErrorTableEntry *>(1);6901 err_set_type->data.error_set.errors = allocate<ErrorTableEntry *>(1);
69026902
6903 g->error_di_types.append(&err_set_type->di_type);
6904
6905 err_set_type->data.error_set.errors[0] = err_entry;6903 err_set_type->data.error_set.errors[0] = err_entry;
69066904
6907 return err_set_type;6905 return err_set_type;
...@@ -6917,9 +6915,9 @@ static IrInstruction *ir_gen_err_set_decl(IrBuilder *irb, Scope *parent_scope, A...@@ -6917,9 +6915,9 @@ static IrInstruction *ir_gen_err_set_decl(IrBuilder *irb, Scope *parent_scope, A
6917 ZigType *err_set_type = new_type_table_entry(ZigTypeIdErrorSet);6915 ZigType *err_set_type = new_type_table_entry(ZigTypeIdErrorSet);
6918 buf_init_from_buf(&err_set_type->name, type_name);6916 buf_init_from_buf(&err_set_type->name, type_name);
6919 err_set_type->data.error_set.err_count = err_count;6917 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;6918 err_set_type->size_in_bits = irb->codegen->builtin_types.entry_global_error_set->size_in_bits;
6921 err_set_type->di_type = irb->codegen->builtin_types.entry_global_error_set->di_type;6919 err_set_type->abi_align = irb->codegen->builtin_types.entry_global_error_set->abi_align;
6922 irb->codegen->error_di_types.append(&err_set_type->di_type);6920 err_set_type->abi_size = irb->codegen->builtin_types.entry_global_error_set->abi_size;
6923 err_set_type->data.error_set.errors = allocate<ErrorTableEntry *>(err_count);6921 err_set_type->data.error_set.errors = allocate<ErrorTableEntry *>(err_count);
69246922
6925 ErrorTableEntry **errors = allocate<ErrorTableEntry *>(irb->codegen->errors_by_index.length + err_count);6923 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...@@ -6940,8 +6938,6 @@ static IrInstruction *ir_gen_err_set_decl(IrBuilder *irb, Scope *parent_scope, A
6940 assert((uint32_t)error_value_count < (((uint32_t)1) << (uint32_t)irb->codegen->err_tag_type->data.integral.bit_count));6938 assert((uint32_t)error_value_count < (((uint32_t)1) << (uint32_t)irb->codegen->err_tag_type->data.integral.bit_count));
6941 err->value = error_value_count;6939 err->value = error_value_count;
6942 irb->codegen->errors_by_index.append(err);6940 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));
6945 }6941 }
6946 err_set_type->data.error_set.errors[i] = err;6942 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...@@ -8947,16 +8943,16 @@ static ZigType *get_error_set_intersection(IrAnalyze *ira, ZigType *set1, ZigTyp
8947 }8943 }
8948 free(errors);8944 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;
8952 err_set_type->data.error_set.err_count = intersection_list.length;8946 err_set_type->data.error_set.err_count = intersection_list.length;
8953 err_set_type->data.error_set.errors = intersection_list.items;8947 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
8956 buf_appendf(&err_set_type->name, "}");8954 buf_appendf(&err_set_type->name, "}");
89578955
8958 ira->codegen->error_di_types.append(&err_set_type->di_type);
8959
8960 return err_set_type;8956 return err_set_type;
8961}8957}
89628958
...@@ -14855,7 +14851,7 @@ static IrInstruction *ir_analyze_maybe(IrAnalyze *ira, IrInstructionUnOp *un_op_...@@ -14855,7 +14851,7 @@ static IrInstruction *ir_analyze_maybe(IrAnalyze *ira, IrInstructionUnOp *un_op_
14855 ZigType *type_entry = ir_resolve_type(ira, value);14851 ZigType *type_entry = ir_resolve_type(ira, value);
14856 if (type_is_invalid(type_entry))14852 if (type_is_invalid(type_entry))
14857 return ira->codegen->invalid_instruction;14853 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)))
14859 return ira->codegen->invalid_instruction;14855 return ira->codegen->invalid_instruction;
1486014856
14861 switch (type_entry->id) {14857 switch (type_entry->id) {
...@@ -16051,8 +16047,6 @@ static IrInstruction *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstruc...@@ -16051,8 +16047,6 @@ static IrInstruction *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstruc
16051 assert((uint32_t)error_value_count < (((uint32_t)1) << (uint32_t)ira->codegen->err_tag_type->data.integral.bit_count));16047 assert((uint32_t)error_value_count < (((uint32_t)1) << (uint32_t)ira->codegen->err_tag_type->data.integral.bit_count));
16052 err_entry->value = error_value_count;16048 err_entry->value = error_value_count;
16053 ira->codegen->errors_by_index.append(err_entry);16049 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));
16056 ira->codegen->error_table.put(field_name, err_entry);16050 ira->codegen->error_table.put(field_name, err_entry);
16057 }16051 }
16058 if (err_entry->set_with_only_this_in_it == nullptr) {16052 if (err_entry->set_with_only_this_in_it == nullptr) {
...@@ -17873,7 +17867,7 @@ static TypeStructField *validate_byte_offset(IrAnalyze *ira,...@@ -17873,7 +17867,7 @@ static TypeStructField *validate_byte_offset(IrAnalyze *ira,
17873 return nullptr;17867 return nullptr;
17874 }17868 }
1787517869
17876 *byte_offset = LLVMOffsetOfElement(ira->codegen->target_data_ref, container_type->type_ref, field->gen_index);17870 *byte_offset = field->offset;
17877 return field;17871 return field;
17878}17872}
1787917873
...@@ -18726,7 +18720,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr...@@ -18726,7 +18720,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
18726 if (!type_has_bits(struct_field->type_entry)) {18720 if (!type_has_bits(struct_field->type_entry)) {
18727 inner_fields[1].data.x_optional = nullptr;18721 inner_fields[1].data.x_optional = nullptr;
18728 } else {18722 } 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;
18730 inner_fields[1].data.x_optional = create_const_vals(1);18724 inner_fields[1].data.x_optional = create_const_vals(1);
18731 inner_fields[1].data.x_optional->special = ConstValSpecialStatic;18725 inner_fields[1].data.x_optional->special = ConstValSpecialStatic;
18732 inner_fields[1].data.x_optional->type = ira->codegen->builtin_types.entry_num_lit_int;18726 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...@@ -21425,12 +21419,11 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue
21425 case ContainerLayoutExtern: {21419 case ContainerLayoutExtern: {
21426 size_t src_field_count = val->type->data.structure.src_field_count;21420 size_t src_field_count = val->type->data.structure.src_field_count;
21427 for (size_t field_i = 0; field_i < src_field_count; field_i += 1) {21421 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];21422 TypeStructField *struct_field = &val->type->data.structure.fields[field_i];
21429 if (type_field->gen_index == SIZE_MAX)21423 if (struct_field->gen_index == SIZE_MAX)
21430 continue;21424 continue;
21431 ConstExprValue *field_val = &val->data.x_struct.fields[field_i];21425 ConstExprValue *field_val = &val->data.x_struct.fields[field_i];
21432 size_t offset = LLVMOffsetOfElement(codegen->target_data_ref, val->type->type_ref,21426 size_t offset = struct_field->offset;
21433 type_field->gen_index);
21434 buf_write_value_bytes(codegen, buf + offset, field_val);21427 buf_write_value_bytes(codegen, buf + offset, field_val);
21435 }21428 }
21436 return;21429 return;
...@@ -21446,10 +21439,7 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue...@@ -21446,10 +21439,7 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue
21446 size_t child_buf_len = 16;21439 size_t child_buf_len = 16;
21447 uint8_t *child_buf = child_buf_prealloc;21440 uint8_t *child_buf = child_buf_prealloc;
21448 while (gen_i < gen_field_count) {21441 while (gen_i < gen_field_count) {
21449 LLVMTypeRef gen_llvm_int_type = LLVMStructGetTypeAtIndex(val->type->type_ref,21442 size_t big_int_byte_count = val->type->data.structure.host_int_bytes[gen_i];
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;
21453 if (big_int_byte_count > child_buf_len) {21443 if (big_int_byte_count > child_buf_len) {
21454 child_buf = allocate_nonzero<uint8_t>(big_int_byte_count);21444 child_buf = allocate_nonzero<uint8_t>(big_int_byte_count);
21455 child_buf_len = big_int_byte_count;21445 child_buf_len = big_int_byte_count;
...@@ -21485,7 +21475,7 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue...@@ -21485,7 +21475,7 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue
21485 }21475 }
21486 src_i += 1;21476 src_i += 1;
21487 }21477 }
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);
21489 offset += big_int_byte_count;21479 offset += big_int_byte_count;
21490 gen_i += 1;21480 gen_i += 1;
21491 }21481 }
...@@ -21606,12 +21596,11 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou...@@ -21606,12 +21596,11 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou
21606 for (size_t field_i = 0; field_i < src_field_count; field_i += 1) {21596 for (size_t field_i = 0; field_i < src_field_count; field_i += 1) {
21607 ConstExprValue *field_val = &val->data.x_struct.fields[field_i];21597 ConstExprValue *field_val = &val->data.x_struct.fields[field_i];
21608 field_val->special = ConstValSpecialStatic;21598 field_val->special = ConstValSpecialStatic;
21609 TypeStructField *type_field = &val->type->data.structure.fields[field_i];21599 TypeStructField *struct_field = &val->type->data.structure.fields[field_i];
21610 field_val->type = type_field->type_entry;21600 field_val->type = struct_field->type_entry;
21611 if (type_field->gen_index == SIZE_MAX)21601 if (struct_field->gen_index == SIZE_MAX)
21612 continue;21602 continue;
21613 size_t offset = LLVMOffsetOfElement(codegen->target_data_ref, val->type->type_ref,21603 size_t offset = struct_field->offset;
21614 type_field->gen_index);
21615 uint8_t *new_buf = buf + offset;21604 uint8_t *new_buf = buf + offset;
21616 if ((err = buf_read_value_bytes(ira, codegen, source_node, new_buf, field_val)))21605 if ((err = buf_read_value_bytes(ira, codegen, source_node, new_buf, field_val)))
21617 return err;21606 return err;
...@@ -21630,16 +21619,13 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou...@@ -21630,16 +21619,13 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou
21630 size_t child_buf_len = 16;21619 size_t child_buf_len = 16;
21631 uint8_t *child_buf = child_buf_prealloc;21620 uint8_t *child_buf = child_buf_prealloc;
21632 while (gen_i < gen_field_count) {21621 while (gen_i < gen_field_count) {
21633 LLVMTypeRef gen_llvm_int_type = LLVMStructGetTypeAtIndex(val->type->type_ref,21622 size_t big_int_byte_count = val->type->data.structure.host_int_bytes[gen_i];
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;
21637 if (big_int_byte_count > child_buf_len) {21623 if (big_int_byte_count > child_buf_len) {
21638 child_buf = allocate_nonzero<uint8_t>(big_int_byte_count);21624 child_buf = allocate_nonzero<uint8_t>(big_int_byte_count);
21639 child_buf_len = big_int_byte_count;21625 child_buf_len = big_int_byte_count;
21640 }21626 }
21641 BigInt big_int;21627 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);
21643 while (src_i < src_field_count) {21629 while (src_i < src_field_count) {
21644 TypeStructField *field = &val->type->data.structure.fields[src_i];21630 TypeStructField *field = &val->type->data.structure.fields[src_i];
21645 assert(field->gen_index != SIZE_MAX);21631 assert(field->gen_index != SIZE_MAX);
...@@ -21662,7 +21648,7 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou...@@ -21662,7 +21648,7 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou
21662 big_int = tmp;21648 big_int = tmp;
21663 }21649 }
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);
21666 if ((err = buf_read_value_bytes(ira, codegen, source_node, child_buf, field_val))) {21652 if ((err = buf_read_value_bytes(ira, codegen, source_node, child_buf, field_val))) {
21667 return err;21653 return err;
21668 }21654 }