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

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

decouple llvm types from zig types

9 files changed, 2017 insertions(+), 1689 deletions(-)

doc/langref.html.in+4-4
...@@ -2112,8 +2112,8 @@ test "linked list" {...@@ -2112,8 +2112,8 @@ test "linked list" {
2112 <li>A {#link|packed enum#} field uses exactly the bit width of its integer tag type.</li>2112 <li>A {#link|packed enum#} field uses exactly the bit width of its integer tag type.</li>
2113 <li>A {#link|packed union#} field uses exactly the bit width of the union field with2113 <li>A {#link|packed union#} field uses exactly the bit width of the union field with
2114 the largest bit width.</li>2114 the largest bit width.</li>
2115 <li>Non-byte-aligned fields are packed into the smallest possible2115 <li>Non-ABI-aligned fields are packed into the smallest possible
2116 byte-aligned integers in accordance with the target endianness.2116 ABI-aligned integers in accordance with the target endianness.
2117 </li>2117 </li>
2118 </ul>2118 </ul>
2119 <p>2119 <p>
...@@ -2213,10 +2213,10 @@ fn bar(x: *const u3) u3 {...@@ -2213,10 +2213,10 @@ fn bar(x: *const u3) u3 {
2213 {#code_end#}2213 {#code_end#}
2214 <p>2214 <p>
2215 In this case, the function {#syntax#}bar{#endsyntax#} cannot be called becuse the pointer2215 In this case, the function {#syntax#}bar{#endsyntax#} cannot be called becuse the pointer
2216 to the non-byte-aligned field mentions the bit offset, but the function expects a byte-aligned pointer.2216 to the non-ABI-aligned field mentions the bit offset, but the function expects an ABI-aligned pointer.
2217 </p>2217 </p>
2218 <p>2218 <p>
2219 Pointers to non-byte-aligned fields share the same address as the other fields within their host integer:2219 Pointers to non-ABI-aligned fields share the same address as the other fields within their host integer:
2220 </p>2220 </p>
2221 {#code_begin|test#}2221 {#code_begin|test#}
2222const std = @import("std");2222const std = @import("std");
src/all_types.hpp+32-34
...@@ -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 {
...@@ -1077,6 +1079,8 @@ enum ResolveStatus {...@@ -1077,6 +1079,8 @@ enum ResolveStatus {
1077 ResolveStatusZeroBitsKnown,1079 ResolveStatusZeroBitsKnown,
1078 ResolveStatusAlignmentKnown,1080 ResolveStatusAlignmentKnown,
1079 ResolveStatusSizeKnown,1081 ResolveStatusSizeKnown,
1082 ResolveStatusLLVMFwdDecl,
1083 ResolveStatusLLVMFull,
1080};1084};
10811085
1082struct ZigPackage {1086struct ZigPackage {
...@@ -1101,14 +1105,13 @@ struct ZigTypeStruct {...@@ -1101,14 +1105,13 @@ struct ZigTypeStruct {
1101 AstNode *decl_node;1105 AstNode *decl_node;
1102 TypeStructField *fields;1106 TypeStructField *fields;
1103 ScopeDecls *decls_scope;1107 ScopeDecls *decls_scope;
1104 uint64_t size_bytes;
1105 HashMap<Buf *, TypeStructField *, buf_hash, buf_eql_buf> fields_by_name;1108 HashMap<Buf *, TypeStructField *, buf_hash, buf_eql_buf> fields_by_name;
1106 RootStruct *root_struct;1109 RootStruct *root_struct;
1110 uint32_t *host_int_bytes; // available for packed structs, indexed by gen_index
11071111
1108 uint32_t src_field_count;1112 uint32_t src_field_count;
1109 uint32_t gen_field_count;1113 uint32_t gen_field_count;
11101114
1111 uint32_t abi_alignment; // known after ResolveStatusAlignmentKnown
1112 ContainerLayout layout;1115 ContainerLayout layout;
1113 ResolveStatus resolve_status;1116 ResolveStatus resolve_status;
11141117
...@@ -1164,39 +1167,28 @@ bool type_ptr_eql(const ZigType *a, const ZigType *b);...@@ -1164,39 +1167,28 @@ bool type_ptr_eql(const ZigType *a, const ZigType *b);
11641167
1165struct ZigTypeUnion {1168struct ZigTypeUnion {
1166 AstNode *decl_node;1169 AstNode *decl_node;
1167 ContainerLayout layout;
1168 uint32_t src_field_count;
1169 uint32_t gen_field_count;
1170 TypeUnionField *fields;1170 TypeUnionField *fields;
1171 bool is_invalid; // true if any fields are invalid1171 ScopeDecls *decls_scope;
1172 HashMap<Buf *, TypeUnionField *, buf_hash, buf_eql_buf> fields_by_name;
1172 ZigType *tag_type; // always an enum or null1173 ZigType *tag_type; // always an enum or null
1173 LLVMTypeRef union_type_ref;1174 LLVMTypeRef union_llvm_type;
1175 ZigType *most_aligned_union_member;
1176 size_t gen_union_index;
1177 size_t gen_tag_index;
1178 size_t union_abi_size;
11741179
1175 ScopeDecls *decls_scope;1180 uint32_t src_field_count;
1181 uint32_t gen_field_count;
11761182
1177 // set this flag temporarily to detect infinite loops1183 ContainerLayout layout;
1178 bool embedded_in_current;1184 ResolveStatus resolve_status;
1179 bool reported_infinite_err;
1180 // whether we've finished resolving it
1181 bool complete;
11821185
1186 bool have_explicit_tag_type;
1187 bool resolve_loop_flag; // set this flag temporarily to detect infinite loops
1188 bool reported_infinite_err;
1183 // whether any of the fields require comptime1189 // whether any of the fields require comptime
1184 // the value is not valid until zero_bits_known == true1190 // the value is not valid until zero_bits_known == true
1185 bool requires_comptime;1191 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};1192};
12011193
1202struct FnGenParamInfo {1194struct FnGenParamInfo {
...@@ -1277,8 +1269,11 @@ struct ZigType {...@@ -1277,8 +1269,11 @@ struct ZigType {
1277 ZigTypeId id;1269 ZigTypeId id;
1278 Buf name;1270 Buf name;
12791271
1280 LLVMTypeRef type_ref;1272 // These are not supposed to be accessed directly. They're
1281 ZigLLVMDIType *di_type;1273 // null during semantic analysis, memoized with get_llvm_type
1274 // and get_llvm_di_type
1275 LLVMTypeRef llvm_type;
1276 ZigLLVMDIType *llvm_di_type;
12821277
1283 union {1278 union {
1284 ZigTypePointer pointer;1279 ZigTypePointer pointer;
...@@ -1308,8 +1303,14 @@ struct ZigType {...@@ -1308,8 +1303,14 @@ struct ZigType {
1308 ConstExprValue *cached_const_name_val;1303 ConstExprValue *cached_const_name_val;
13091304
1310 OnePossibleValue one_possible_value;1305 OnePossibleValue one_possible_value;
1306 // Known after ResolveStatusAlignmentKnown.
1307 uint32_t abi_align;
1308 // The offset in bytes between consecutive array elements of this type. Known
1309 // after ResolveStatusSizeKnown.
1310 size_t abi_size;
1311 // Number of bits of information in this type. Known after ResolveStatusSizeKnown.
1312 size_t size_in_bits;
13111313
1312 bool zero_bits; // this is denormalized data
1313 bool gen_h_loop_flag;1314 bool gen_h_loop_flag;
1314};1315};
13151316
...@@ -1700,11 +1701,9 @@ struct CodeGen {...@@ -1700,11 +1701,9 @@ struct CodeGen {
1700 ZigList<AstNode *> use_queue;1701 ZigList<AstNode *> use_queue;
1701 size_t use_queue_index;1702 size_t use_queue_index;
1702 ZigList<TimeEvent> timing_events;1703 ZigList<TimeEvent> timing_events;
1703 ZigList<ZigLLVMDIType **> error_di_types;
1704 ZigList<AstNode *> tld_ref_source_node_stack;1704 ZigList<AstNode *> tld_ref_source_node_stack;
1705 ZigList<ZigFn *> inline_fns;1705 ZigList<ZigFn *> inline_fns;
1706 ZigList<ZigFn *> test_fns;1706 ZigList<ZigFn *> test_fns;
1707 ZigList<ZigLLVMDIEnumerator *> err_enumerators;
1708 ZigList<ErrorTableEntry *> errors_by_index;1707 ZigList<ErrorTableEntry *> errors_by_index;
1709 ZigList<CacheHash *> caches_to_release;1708 ZigList<CacheHash *> caches_to_release;
1710 size_t largest_err_name_len;1709 size_t largest_err_name_len;
...@@ -1896,7 +1895,6 @@ struct ZigVar {...@@ -1896,7 +1895,6 @@ struct ZigVar {
1896 AstNode *decl_node;1895 AstNode *decl_node;
1897 ZigLLVMDILocalVariable *di_loc_var;1896 ZigLLVMDILocalVariable *di_loc_var;
1898 size_t src_arg_index;1897 size_t src_arg_index;
1899 size_t gen_arg_index;
1900 Scope *parent_scope;1898 Scope *parent_scope;
1901 Scope *child_scope;1899 Scope *child_scope;
1902 LLVMValueRef param_value_ref;1900 LLVMValueRef param_value_ref;
src/analyze.cpp+1555-1244
...@@ -19,14 +19,15 @@...@@ -19,14 +19,15 @@
1919
20static const size_t default_backward_branch_quota = 1000;20static const size_t default_backward_branch_quota = 1000;
2121
22static Error resolve_enum_type(CodeGen *g, ZigType *enum_type);
23static Error resolve_struct_type(CodeGen *g, ZigType *struct_type);22static Error resolve_struct_type(CodeGen *g, ZigType *struct_type);
2423
25static Error ATTRIBUTE_MUST_USE resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type);24static Error ATTRIBUTE_MUST_USE resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type);
26static Error ATTRIBUTE_MUST_USE resolve_struct_alignment(CodeGen *g, ZigType *struct_type);25static 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);26static 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);27static Error ATTRIBUTE_MUST_USE resolve_union_zero_bits(CodeGen *g, ZigType *union_type);
28static Error ATTRIBUTE_MUST_USE resolve_union_alignment(CodeGen *g, ZigType *union_type);
29static void analyze_fn_body(CodeGen *g, ZigFn *fn_table_entry);29static void analyze_fn_body(CodeGen *g, ZigFn *fn_table_entry);
30static void resolve_llvm_types(CodeGen *g, ZigType *type, ResolveStatus wanted_resolve_status);
3031
31static bool is_top_level_struct(ZigType *import) {32static bool is_top_level_struct(ZigType *import) {
32 return import->id == ZigTypeIdStruct && import->data.structure.root_struct != nullptr;33 return import->id == ZigTypeIdStruct && import->data.structure.root_struct != nullptr;
...@@ -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:
...@@ -276,20 +279,9 @@ bool type_is_resolved(ZigType *type_entry, ResolveStatus status) {...@@ -276,20 +279,9 @@ bool type_is_resolved(ZigType *type_entry, ResolveStatus status) {
276 return type_entry->data.enumeration.zero_bits_known;279 return type_entry->data.enumeration.zero_bits_known;
277 case ResolveStatusSizeKnown:280 case ResolveStatusSizeKnown:
278 return type_entry->data.enumeration.complete;281 return type_entry->data.enumeration.complete;
279 }282 case ResolveStatusLLVMFwdDecl:
280 zig_unreachable();283 case ResolveStatusLLVMFull:
281 case ZigTypeIdUnion:284 return type_entry->llvm_di_type != nullptr;
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 }285 }
294 zig_unreachable();286 zig_unreachable();
295 case ZigTypeIdOpaque:287 case ZigTypeIdOpaque:
...@@ -325,49 +317,18 @@ bool type_is_complete(ZigType *type_entry) {...@@ -325,49 +317,18 @@ bool type_is_complete(ZigType *type_entry) {
325}317}
326318
327uint64_t type_size(CodeGen *g, ZigType *type_entry) {319uint64_t type_size(CodeGen *g, ZigType *type_entry) {
328 assert(type_is_complete(type_entry));320 assert(type_is_resolved(type_entry, ResolveStatusSizeKnown));
329321 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}322}
348323
349uint64_t type_size_bits(CodeGen *g, ZigType *type_entry) {324uint64_t type_size_bits(CodeGen *g, ZigType *type_entry) {
350 assert(type_is_complete(type_entry));325 assert(type_is_resolved(type_entry, ResolveStatusSizeKnown));
351326 return type_entry->size_in_bits;
352 if (!type_has_bits(type_entry))327}
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 }
369328
370 return LLVMSizeOfTypeInBits(g->target_data_ref, type_entry->type_ref);329uint32_t get_abi_alignment(CodeGen *g, ZigType *type_entry) {
330 assert(type_is_resolved(type_entry, ResolveStatusAlignmentKnown));
331 return type_entry->abi_align;
371}332}
372333
373static bool is_slice(ZigType *type) {334static bool is_slice(ZigType *type) {
...@@ -385,16 +346,15 @@ ZigType *get_promise_type(CodeGen *g, ZigType *result_type) {...@@ -385,16 +346,15 @@ ZigType *get_promise_type(CodeGen *g, ZigType *result_type) {
385 return g->builtin_types.entry_promise;346 return g->builtin_types.entry_promise;
386 }347 }
387348
388 ZigType *u8_ptr_type = get_pointer_to_type(g, g->builtin_types.entry_u8, false);
389 ZigType *entry = new_type_table_entry(ZigTypeIdPromise);349 ZigType *entry = new_type_table_entry(ZigTypeIdPromise);
390 entry->type_ref = u8_ptr_type->type_ref;350 entry->abi_size = g->builtin_types.entry_usize->abi_size;
391 entry->zero_bits = false;351 entry->size_in_bits = g->builtin_types.entry_usize->size_in_bits;
352 entry->abi_align = g->builtin_types.entry_usize->abi_align;
392 entry->data.promise.result_type = result_type;353 entry->data.promise.result_type = result_type;
393 buf_init_from_str(&entry->name, "promise");354 buf_init_from_str(&entry->name, "promise");
394 if (result_type != nullptr) {355 if (result_type != nullptr) {
395 buf_appendf(&entry->name, "->%s", buf_ptr(&result_type->name));356 buf_appendf(&entry->name, "->%s", buf_ptr(&result_type->name));
396 }357 }
397 entry->di_type = u8_ptr_type->di_type;
398358
399 if (result_type != nullptr) {359 if (result_type != nullptr) {
400 result_type->promise_parent = entry;360 result_type->promise_parent = entry;
...@@ -496,36 +456,15 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons...@@ -496,36 +456,15 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons
496456
497 assert(child_type->id != ZigTypeIdInvalid);457 assert(child_type->id != ZigTypeIdInvalid);
498458
499 entry->zero_bits = !type_has_bits(child_type);459 if (type_has_bits(child_type)) {
500460 entry->abi_size = g->builtin_types.entry_usize->abi_size;
501 if (!entry->zero_bits) {461 entry->size_in_bits = g->builtin_types.entry_usize->size_in_bits;
502 if (is_const || is_volatile || byte_alignment != 0 || ptr_len != PtrLenSingle ||462 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 {463 } else {
527 assert(byte_alignment == 0);464 assert(byte_alignment == 0);
528 entry->di_type = g->builtin_types.entry_void->di_type;465 entry->abi_size = 0;
466 entry->size_in_bits = 0;
467 entry->abi_align = 0;
529 }468 }
530469
531 entry->data.pointer.ptr_len = ptr_len;470 entry->data.pointer.ptr_len = ptr_len;
...@@ -586,89 +525,60 @@ ZigType *get_promise_frame_type(CodeGen *g, ZigType *return_type) {...@@ -586,89 +525,60 @@ ZigType *get_promise_frame_type(CodeGen *g, ZigType *return_type) {
586}525}
587526
588ZigType *get_optional_type(CodeGen *g, ZigType *child_type) {527ZigType *get_optional_type(CodeGen *g, ZigType *child_type) {
589 if (child_type->optional_parent) {528 if (child_type->optional_parent != nullptr) {
590 ZigType *entry = child_type->optional_parent;529 return child_type->optional_parent;
591 return entry;530 }
592 } else {
593 assertNoError(ensure_complete_type(g, child_type));
594
595 ZigType *entry = new_type_table_entry(ZigTypeIdOptional);
596 assert(child_type->type_ref || child_type->zero_bits);
597
598 buf_resize(&entry->name, 0);
599 buf_appendf(&entry->name, "?%s", buf_ptr(&child_type->name));
600
601 if (child_type->zero_bits) {
602 entry->type_ref = LLVMInt1Type();
603 entry->di_type = g->builtin_types.entry_bool->di_type;
604 } else if (type_is_nonnull_ptr(child_type) || child_type->id == ZigTypeIdErrorSet) {
605 assert(child_type->di_type);
606 // this is an optimization but also is necessary for calling C
607 // functions where all pointers are maybe pointers
608 // function types are technically pointers
609 entry->type_ref = child_type->type_ref;
610 entry->di_type = child_type->di_type;
611 if (entry->di_type == g->builtin_types.entry_global_error_set->di_type) {
612 g->error_di_types.append(&entry->di_type);
613 }
614 } else {
615 assert(child_type->di_type);
616 // create a struct with a boolean whether this is the null value
617 LLVMTypeRef elem_types[] = {
618 child_type->type_ref,
619 LLVMInt1Type(),
620 };
621 entry->type_ref = LLVMStructType(elem_types, 2, false);
622
623
624 ZigLLVMDIScope *compile_unit_scope = ZigLLVMCompileUnitToScope(g->compile_unit);
625 ZigLLVMDIFile *di_file = nullptr;
626 unsigned line = 0;
627 entry->di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
628 ZigLLVMTag_DW_structure_type(), buf_ptr(&entry->name),
629 compile_unit_scope, di_file, line);
630
631 uint64_t val_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, child_type->type_ref);
632 uint64_t val_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, child_type->type_ref);
633 uint64_t val_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, entry->type_ref, 0);
634
635 ZigType *bool_type = g->builtin_types.entry_bool;
636 uint64_t maybe_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, bool_type->type_ref);
637 uint64_t maybe_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, bool_type->type_ref);
638 uint64_t maybe_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, entry->type_ref, 1);
639
640 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, entry->type_ref);
641 uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, entry->type_ref);
642
643 ZigLLVMDIType *di_element_types[] = {
644 ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(entry->di_type),
645 "val", di_file, line,
646 val_debug_size_in_bits,
647 val_debug_align_in_bits,
648 val_offset_in_bits,
649 0, child_type->di_type),
650 ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(entry->di_type),
651 "maybe", di_file, line,
652 maybe_debug_size_in_bits,
653 maybe_debug_align_in_bits,
654 maybe_offset_in_bits,
655 0, bool_type->di_type),
656 };
657 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
658 compile_unit_scope,
659 buf_ptr(&entry->name),
660 di_file, line, debug_size_in_bits, debug_align_in_bits, 0,
661 nullptr, di_element_types, 2, 0, nullptr, "");
662531
663 ZigLLVMReplaceTemporary(g->dbuilder, entry->di_type, replacement_di_type);532 assert(type_is_resolved(child_type, ResolveStatusSizeKnown));
664 entry->di_type = replacement_di_type;
665 }
666533
667 entry->data.maybe.child_type = child_type;534 ZigType *entry = new_type_table_entry(ZigTypeIdOptional);
668535
669 child_type->optional_parent = entry;536 buf_resize(&entry->name, 0);
670 return entry;537 buf_appendf(&entry->name, "?%s", buf_ptr(&child_type->name));
538
539 if (!type_has_bits(child_type)) {
540 entry->size_in_bits = g->builtin_types.entry_bool->size_in_bits;
541 entry->abi_size = g->builtin_types.entry_bool->abi_size;
542 entry->abi_align = g->builtin_types.entry_bool->abi_align;
543 } else if (type_is_nonnull_ptr(child_type) || child_type->id == ZigTypeIdErrorSet) {
544 // This is an optimization but also is necessary for calling C
545 // functions where all pointers are optional pointers.
546 // Function types are technically pointers.
547 entry->size_in_bits = child_type->size_in_bits;
548 entry->abi_size = child_type->abi_size;
549 entry->abi_align = child_type->abi_align;
550 } else {
551 // This value only matters if the type is legal in a packed struct, which is not
552 // true for optional types which did not fit the above 2 categories (zero bit child type,
553 // or nonnull ptr child type, or error set child type).
554 entry->size_in_bits = child_type->size_in_bits + 1;
555
556 // We're going to make a struct with the child type as the first field,
557 // and a bool as the second. Since the child type's abi alignment is guaranteed
558 // to be >= the bool's abi size (1 byte), the added size is exactly equal to the
559 // child type's ABI alignment.
560 assert(child_type->abi_align >= g->builtin_types.entry_bool->abi_size);
561 entry->abi_align = child_type->abi_align;
562 entry->abi_size = child_type->abi_size + child_type->abi_align;
671 }563 }
564
565 entry->data.maybe.child_type = child_type;
566
567 child_type->optional_parent = entry;
568 return entry;
569}
570
571static size_t align_forward(size_t addr, size_t alignment) {
572 return (addr + alignment - 1) & ~(alignment - 1);
573}
574
575static size_t next_field_offset(size_t offset, size_t align_from_zero, size_t field_size, size_t next_field_align) {
576 // Convert offset to a pretend address which has the specified alignment.
577 size_t addr = offset + align_from_zero;
578 // March the address forward to respect the field alignment.
579 size_t aligned_addr = align_forward(addr + field_size, next_field_align);
580 // Convert back from pretend address to offset.
581 return aligned_addr - align_from_zero;
672}582}
673583
674ZigType *get_error_union_type(CodeGen *g, ZigType *err_set_type, ZigType *payload_type) {584ZigType *get_error_union_type(CodeGen *g, ZigType *err_set_type, ZigType *payload_type) {
...@@ -686,8 +596,7 @@ ZigType *get_error_union_type(CodeGen *g, ZigType *err_set_type, ZigType *payloa...@@ -686,8 +596,7 @@ ZigType *get_error_union_type(CodeGen *g, ZigType *err_set_type, ZigType *payloa
686 }596 }
687597
688 ZigType *entry = new_type_table_entry(ZigTypeIdErrorUnion);598 ZigType *entry = new_type_table_entry(ZigTypeIdErrorUnion);
689 assert(payload_type->di_type);599 assert(type_is_resolved(payload_type, ResolveStatusSizeKnown));
690 assert(type_is_complete(payload_type));
691600
692 buf_resize(&entry->name, 0);601 buf_resize(&entry->name, 0);
693 buf_appendf(&entry->name, "%s!%s", buf_ptr(&err_set_type->name), buf_ptr(&payload_type->name));602 buf_appendf(&entry->name, "%s!%s", buf_ptr(&err_set_type->name), buf_ptr(&payload_type->name));
...@@ -697,68 +606,29 @@ ZigType *get_error_union_type(CodeGen *g, ZigType *err_set_type, ZigType *payloa...@@ -697,68 +606,29 @@ ZigType *get_error_union_type(CodeGen *g, ZigType *err_set_type, ZigType *payloa
697606
698 if (!type_has_bits(payload_type)) {607 if (!type_has_bits(payload_type)) {
699 if (type_has_bits(err_set_type)) {608 if (type_has_bits(err_set_type)) {
700 entry->type_ref = err_set_type->type_ref;609 entry->size_in_bits = err_set_type->size_in_bits;
701 entry->di_type = err_set_type->di_type;610 entry->abi_size = err_set_type->abi_size;
702 g->error_di_types.append(&entry->di_type);611 entry->abi_align = err_set_type->abi_align;
703 } else {612 } else {
704 entry->zero_bits = true;613 entry->size_in_bits = 0;
705 entry->di_type = g->builtin_types.entry_void->di_type;614 entry->abi_size = 0;
615 entry->abi_align = 0;
706 }616 }
707 } else if (!type_has_bits(err_set_type)) {617 } else if (!type_has_bits(err_set_type)) {
708 entry->type_ref = payload_type->type_ref;618 entry->size_in_bits = payload_type->size_in_bits;
709 entry->di_type = payload_type->di_type;619 entry->abi_size = payload_type->abi_size;
620 entry->abi_align = payload_type->abi_align;
710 } else {621 } else {
711 LLVMTypeRef elem_types[] = {622 entry->abi_align = max(err_set_type->abi_align, payload_type->abi_align);
712 err_set_type->type_ref,623 size_t field_sizes[2];
713 payload_type->type_ref,624 size_t field_aligns[2];
714 };625 field_sizes[err_union_err_index] = err_set_type->abi_size;
715 entry->type_ref = LLVMStructType(elem_types, 2, false);626 field_aligns[err_union_err_index] = err_set_type->abi_align;
716627 field_sizes[err_union_payload_index] = payload_type->abi_size;
717 ZigLLVMDIScope *compile_unit_scope = ZigLLVMCompileUnitToScope(g->compile_unit);628 field_aligns[err_union_payload_index] = payload_type->abi_align;
718 ZigLLVMDIFile *di_file = nullptr;629 size_t field2_offset = next_field_offset(0, entry->abi_align, field_sizes[0], field_aligns[1]);
719 unsigned line = 0;630 entry->abi_size = next_field_offset(field2_offset, entry->abi_align, field_sizes[1], entry->abi_align);
720 entry->di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,631 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 }632 }
763633
764 g->type_table.put(type_id, entry);634 g->type_table.put(type_id, entry);
...@@ -772,31 +642,20 @@ ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size) {...@@ -772,31 +642,20 @@ ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size) {
772 type_id.data.array.size = array_size;642 type_id.data.array.size = array_size;
773 auto existing_entry = g->type_table.maybe_get(type_id);643 auto existing_entry = g->type_table.maybe_get(type_id);
774 if (existing_entry) {644 if (existing_entry) {
775 ZigType *entry = existing_entry->value;645 return existing_entry->value;
776 return entry;
777 }646 }
778647
779 assertNoError(ensure_complete_type(g, child_type));648 assert(type_is_resolved(child_type, ResolveStatusSizeKnown));
780649
781 ZigType *entry = new_type_table_entry(ZigTypeIdArray);650 ZigType *entry = new_type_table_entry(ZigTypeIdArray);
782 entry->zero_bits = (array_size == 0) || child_type->zero_bits;
783651
784 buf_resize(&entry->name, 0);652 buf_resize(&entry->name, 0);
785 buf_appendf(&entry->name, "[%" ZIG_PRI_u64 "]%s", array_size, buf_ptr(&child_type->name));653 buf_appendf(&entry->name, "[%" ZIG_PRI_u64 "]%s", array_size, buf_ptr(&child_type->name));
786654
787 if (entry->zero_bits) {655 entry->size_in_bits = child_type->size_in_bits * array_size;
788 entry->di_type = ZigLLVMCreateDebugArrayType(g->dbuilder, 0,656 entry->abi_align = child_type->abi_align;
789 0, child_type->di_type, 0);657 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);
796658
797 entry->di_type = ZigLLVMCreateDebugArrayType(g->dbuilder, debug_size_in_bits,
798 debug_align_in_bits, child_type->di_type, (int)array_size);
799 }
800 entry->data.array.child_type = child_type;659 entry->data.array.child_type = child_type;
801 entry->data.array.len = array_size;660 entry->data.array.len = array_size;
802661
...@@ -804,11 +663,27 @@ ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size) {...@@ -804,11 +663,27 @@ ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size) {
804 return entry;663 return entry;
805}664}
806665
807static void slice_type_common_init(CodeGen *g, ZigType *pointer_type, ZigType *entry) {666ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type) {
667 assert(ptr_type->id == ZigTypeIdPointer);
668 assert(ptr_type->data.pointer.ptr_len == PtrLenUnknown);
669
670 ZigType **parent_pointer = &ptr_type->data.pointer.slice_parent;
671 if (*parent_pointer) {
672 return *parent_pointer;
673 }
674
675 ZigType *entry = new_type_table_entry(ZigTypeIdStruct);
676
677 // replace the & with [] to go from a ptr type name to a slice type name
678 buf_resize(&entry->name, 0);
679 size_t name_offset = (ptr_type->data.pointer.ptr_len == PtrLenSingle) ? 1 : 3;
680 buf_appendf(&entry->name, "[]%s", buf_ptr(&ptr_type->name) + name_offset);
681
808 unsigned element_count = 2;682 unsigned element_count = 2;
809 Buf *ptr_field_name = buf_create_from_str("ptr");683 Buf *ptr_field_name = buf_create_from_str("ptr");
810 Buf *len_field_name = buf_create_from_str("len");684 Buf *len_field_name = buf_create_from_str("len");
811685
686 entry->data.structure.resolve_status = ResolveStatusSizeKnown;
812 entry->data.structure.layout = ContainerLayoutAuto;687 entry->data.structure.layout = ContainerLayoutAuto;
813 entry->data.structure.is_slice = true;688 entry->data.structure.is_slice = true;
814 entry->data.structure.src_field_count = element_count;689 entry->data.structure.src_field_count = element_count;
...@@ -816,7 +691,7 @@ static void slice_type_common_init(CodeGen *g, ZigType *pointer_type, ZigType *e...@@ -816,7 +691,7 @@ static void slice_type_common_init(CodeGen *g, ZigType *pointer_type, ZigType *e
816 entry->data.structure.fields = allocate<TypeStructField>(element_count);691 entry->data.structure.fields = allocate<TypeStructField>(element_count);
817 entry->data.structure.fields_by_name.init(element_count);692 entry->data.structure.fields_by_name.init(element_count);
818 entry->data.structure.fields[slice_ptr_index].name = ptr_field_name;693 entry->data.structure.fields[slice_ptr_index].name = ptr_field_name;
819 entry->data.structure.fields[slice_ptr_index].type_entry = pointer_type;694 entry->data.structure.fields[slice_ptr_index].type_entry = ptr_type;
820 entry->data.structure.fields[slice_ptr_index].src_index = slice_ptr_index;695 entry->data.structure.fields[slice_ptr_index].src_index = slice_ptr_index;
821 entry->data.structure.fields[slice_ptr_index].gen_index = 0;696 entry->data.structure.fields[slice_ptr_index].gen_index = 0;
822 entry->data.structure.fields[slice_len_index].name = len_field_name;697 entry->data.structure.fields[slice_len_index].name = len_field_name;
...@@ -827,28 +702,20 @@ static void slice_type_common_init(CodeGen *g, ZigType *pointer_type, ZigType *e...@@ -827,28 +702,20 @@ 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]);702 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]);703 entry->data.structure.fields_by_name.put(len_field_name, &entry->data.structure.fields[slice_len_index]);
829704
830 if (!type_has_bits(pointer_type->data.pointer.child_type)) {705 switch (type_requires_comptime(g, ptr_type)) {
706 case ReqCompTimeInvalid:
707 zig_unreachable();
708 case ReqCompTimeNo:
709 break;
710 case ReqCompTimeYes:
711 entry->data.structure.requires_comptime = true;
712 }
713
714 if (!type_has_bits(ptr_type)) {
831 entry->data.structure.gen_field_count = 1;715 entry->data.structure.gen_field_count = 1;
832 entry->data.structure.fields[slice_ptr_index].gen_index = SIZE_MAX;716 entry->data.structure.fields[slice_ptr_index].gen_index = SIZE_MAX;
833 entry->data.structure.fields[slice_len_index].gen_index = 0;717 entry->data.structure.fields[slice_len_index].gen_index = 0;
834 }718 }
835}
836
837ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type) {
838 assert(ptr_type->id == ZigTypeIdPointer);
839 assert(ptr_type->data.pointer.ptr_len == PtrLenUnknown);
840
841 ZigType **parent_pointer = &ptr_type->data.pointer.slice_parent;
842 if (*parent_pointer) {
843 return *parent_pointer;
844 }
845
846 ZigType *entry = new_type_table_entry(ZigTypeIdStruct);
847
848 // replace the & with [] to go from a ptr type name to a slice type name
849 buf_resize(&entry->name, 0);
850 size_t name_offset = (ptr_type->data.pointer.ptr_len == PtrLenSingle) ? 1 : 3;
851 buf_appendf(&entry->name, "[]%s", buf_ptr(&ptr_type->name) + name_offset);
852719
853 ZigType *child_type = ptr_type->data.pointer.child_type;720 ZigType *child_type = ptr_type->data.pointer.child_type;
854 if (ptr_type->data.pointer.is_const || ptr_type->data.pointer.is_volatile ||721 if (ptr_type->data.pointer.is_const || ptr_type->data.pointer.is_volatile ||
...@@ -858,134 +725,24 @@ ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type) {...@@ -858,134 +725,24 @@ ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type) {
858 PtrLenUnknown, 0, 0, 0, false);725 PtrLenUnknown, 0, 0, 0, false);
859 ZigType *peer_slice_type = get_slice_type(g, peer_ptr_type);726 ZigType *peer_slice_type = get_slice_type(g, peer_ptr_type);
860727
861 slice_type_common_init(g, ptr_type, entry);728 entry->size_in_bits = peer_slice_type->size_in_bits;
862729 entry->abi_size = peer_slice_type->abi_size;
863 entry->type_ref = peer_slice_type->type_ref;730 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;
867731
868 *parent_pointer = entry;732 *parent_pointer = entry;
869 return entry;733 return entry;
870 }734 }
871735
872 // If the child type is []const T then we need to make sure the type ref736 if (type_has_bits(ptr_type)) {
873 // and debug info is the same as if the child type were []T.737 entry->size_in_bits = ptr_type->size_in_bits + g->builtin_types.entry_usize->size_in_bits;
874 if (is_slice(child_type)) {738 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;739 entry->abi_align = ptr_type->abi_align;
876 assert(child_ptr_type->id == ZigTypeIdPointer);740 } else {
877 if (child_ptr_type->data.pointer.is_const || child_ptr_type->data.pointer.is_volatile ||741 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)742 entry->abi_size = g->builtin_types.entry_usize->abi_size;
879 {743 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 }744 }
985745
986
987 entry->data.structure.resolve_status = ResolveStatusSizeKnown;
988
989 *parent_pointer = entry;746 *parent_pointer = entry;
990 return entry;747 return entry;
991}748}
...@@ -998,15 +755,20 @@ ZigType *get_opaque_type(CodeGen *g, Scope *scope, AstNode *source_node, const c...@@ -998,15 +755,20 @@ ZigType *get_opaque_type(CodeGen *g, Scope *scope, AstNode *source_node, const c
998 ZigType *import = scope ? get_scope_import(scope) : nullptr;755 ZigType *import = scope ? get_scope_import(scope) : nullptr;
999 unsigned line = source_node ? (unsigned)(source_node->line + 1) : 0;756 unsigned line = source_node ? (unsigned)(source_node->line + 1) : 0;
1000757
1001 entry->type_ref = LLVMInt8Type();758 entry->llvm_type = LLVMInt8Type();
1002 entry->di_type = ZigLLVMCreateDebugForwardDeclType(g->dbuilder,759 entry->llvm_di_type = ZigLLVMCreateDebugForwardDeclType(g->dbuilder,
1003 ZigLLVMTag_DW_structure_type(), full_name,760 ZigLLVMTag_DW_structure_type(), full_name,
1004 import ? ZigLLVMFileToScope(import->data.structure.root_struct->di_file) : nullptr,761 import ? ZigLLVMFileToScope(import->data.structure.root_struct->di_file) : nullptr,
1005 import ? import->data.structure.root_struct->di_file : nullptr,762 import ? import->data.structure.root_struct->di_file : nullptr,
1006 line);763 line);
1007 entry->zero_bits = false;
1008 entry->data.opaque.bare_name = bare_name;764 entry->data.opaque.bare_name = bare_name;
1009765
766 // The actual size is unknown, but the value must not be 0 because that
767 // is how type_has_bits is determined.
768 entry->abi_size = SIZE_MAX;
769 entry->size_in_bits = SIZE_MAX;
770 entry->abi_align = 1;
771
1010 return entry;772 return entry;
1011}773}
1012774
...@@ -1018,7 +780,6 @@ ZigType *get_bound_fn_type(CodeGen *g, ZigFn *fn_entry) {...@@ -1018,7 +780,6 @@ ZigType *get_bound_fn_type(CodeGen *g, ZigFn *fn_entry) {
1018780
1019 ZigType *bound_fn_type = new_type_table_entry(ZigTypeIdBoundFn);781 ZigType *bound_fn_type = new_type_table_entry(ZigTypeIdBoundFn);
1020 bound_fn_type->data.bound_fn.fn_type = fn_type;782 bound_fn_type->data.bound_fn.fn_type = fn_type;
1021 bound_fn_type->zero_bits = true;
1022783
1023 buf_resize(&bound_fn_type->name, 0);784 buf_resize(&bound_fn_type->name, 0);
1024 buf_appendf(&bound_fn_type->name, "(bound %s)", buf_ptr(&fn_type->name));785 buf_appendf(&bound_fn_type->name, "(bound %s)", buf_ptr(&fn_type->name));
...@@ -1114,8 +875,6 @@ ZigType *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {...@@ -1114,8 +875,6 @@ ZigType *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
1114 ZigType *fn_type = new_type_table_entry(ZigTypeIdFn);875 ZigType *fn_type = new_type_table_entry(ZigTypeIdFn);
1115 fn_type->data.fn.fn_type_id = *fn_type_id;876 fn_type->data.fn.fn_type_id = *fn_type_id;
1116877
1117 bool skip_debug_info = false;
1118
1119 // populate the name of the type878 // populate the name of the type
1120 buf_resize(&fn_type->name, 0);879 buf_resize(&fn_type->name, 0);
1121 if (fn_type->data.fn.fn_type_id.cc == CallingConventionAsync) {880 if (fn_type->data.fn.fn_type_id.cc == CallingConventionAsync) {
...@@ -1133,8 +892,6 @@ ZigType *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {...@@ -1133,8 +892,6 @@ ZigType *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
1133 const char *comma = (i == 0) ? "" : ", ";892 const char *comma = (i == 0) ? "" : ", ";
1134 const char *noalias_str = param_info->is_noalias ? "noalias " : "";893 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));894 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 }895 }
1139896
1140 if (fn_type_id->is_var_args) {897 if (fn_type_id->is_var_args) {
...@@ -1146,116 +903,11 @@ ZigType *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {...@@ -1146,116 +903,11 @@ ZigType *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
1146 buf_appendf(&fn_type->name, " align(%" PRIu32 ")", fn_type_id->alignment);903 buf_appendf(&fn_type->name, " align(%" PRIu32 ")", fn_type_id->alignment);
1147 }904 }
1148 buf_appendf(&fn_type->name, " %s", buf_ptr(&fn_type_id->return_type->name));905 buf_appendf(&fn_type->name, " %s", buf_ptr(&fn_type_id->return_type->name));
1149 skip_debug_info = skip_debug_info || !fn_type_id->return_type->di_type;
1150
1151 // next, loop over the parameters again and compute debug information
1152 // and codegen information
1153 if (!skip_debug_info) {
1154 bool first_arg_return = want_first_arg_sret(g, fn_type_id);
1155 bool is_async = fn_type_id->cc == CallingConventionAsync;
1156 bool is_c_abi = fn_type_id->cc == CallingConventionC;
1157 bool prefix_arg_error_return_trace = g->have_err_ret_tracing && fn_type_can_fail(fn_type_id);
1158 // +1 for maybe making the first argument the return value
1159 // +1 for maybe first argument the error return trace
1160 // +2 for maybe arguments async allocator and error code pointer
1161 ZigList<LLVMTypeRef> gen_param_types = {};
1162 // +1 because 0 is the return type and
1163 // +1 for maybe making first arg ret val and
1164 // +1 for maybe first argument the error return trace
1165 // +2 for maybe arguments async allocator and error code pointer
1166 ZigList<ZigLLVMDIType *> param_di_types = {};
1167 param_di_types.append(fn_type_id->return_type->di_type);
1168 ZigType *gen_return_type;
1169 if (is_async) {
1170 gen_return_type = get_pointer_to_type(g, g->builtin_types.entry_u8, false);
1171 } else if (!type_has_bits(fn_type_id->return_type)) {
1172 gen_return_type = g->builtin_types.entry_void;
1173 } else if (first_arg_return) {
1174 ZigType *gen_type = get_pointer_to_type(g, fn_type_id->return_type, false);
1175 gen_param_types.append(gen_type->type_ref);
1176 param_di_types.append(gen_type->di_type);
1177 gen_return_type = g->builtin_types.entry_void;
1178 } else {
1179 gen_return_type = fn_type_id->return_type;
1180 }
1181 fn_type->data.fn.gen_return_type = gen_return_type;
1182
1183 if (prefix_arg_error_return_trace) {
1184 ZigType *gen_type = get_ptr_to_stack_trace_type(g);
1185 gen_param_types.append(gen_type->type_ref);
1186 param_di_types.append(gen_type->di_type);
1187 }
1188 if (is_async) {
1189 {
1190 // async allocator param
1191 ZigType *gen_type = fn_type_id->async_allocator_type;
1192 gen_param_types.append(gen_type->type_ref);
1193 param_di_types.append(gen_type->di_type);
1194 }
1195
1196 {
1197 // error code pointer
1198 ZigType *gen_type = get_pointer_to_type(g, g->builtin_types.entry_global_error_set, false);
1199 gen_param_types.append(gen_type->type_ref);
1200 param_di_types.append(gen_type->di_type);
1201 }
1202 }
1203
1204 fn_type->data.fn.gen_param_info = allocate<FnGenParamInfo>(fn_type_id->param_count);
1205 for (size_t i = 0; i < fn_type_id->param_count; i += 1) {
1206 FnTypeParamInfo *src_param_info = &fn_type->data.fn.fn_type_id.param_info[i];
1207 ZigType *type_entry = src_param_info->type;
1208 FnGenParamInfo *gen_param_info = &fn_type->data.fn.gen_param_info[i];
1209
1210 gen_param_info->src_index = i;
1211 gen_param_info->gen_index = SIZE_MAX;
1212
1213 if ((err = type_resolve(g, type_entry, ResolveStatusZeroBitsKnown)))
1214 return g->builtin_types.entry_invalid;
1215
1216 if (is_c_abi) {
1217 if ((err = type_resolve(g, type_entry, ResolveStatusSizeKnown)))
1218 return g->builtin_types.entry_invalid;
1219 continue;
1220 }
1221906
1222 if (type_has_bits(type_entry)) {907 // The fn_type is a pointer; not to be confused with the raw function type.
1223 ZigType *gen_type;908 fn_type->size_in_bits = g->builtin_types.entry_usize->size_in_bits;
1224 if (handle_is_ptr(type_entry)) {909 fn_type->abi_size = g->builtin_types.entry_usize->abi_size;
1225 gen_type = get_pointer_to_type(g, type_entry, true);910 fn_type->abi_align = g->builtin_types.entry_usize->abi_align;
1226 gen_param_info->is_byval = true;
1227 } else {
1228 gen_type = type_entry;
1229 }
1230 gen_param_info->gen_index = gen_param_types.length;
1231 gen_param_info->type = gen_type;
1232 gen_param_types.append(gen_type->type_ref);
1233
1234 param_di_types.append(gen_type->di_type);
1235 }
1236 }
1237
1238 if (is_c_abi) {
1239 FnWalk fn_walk = {};
1240 fn_walk.id = FnWalkIdTypes;
1241 fn_walk.data.types.param_di_types = &param_di_types;
1242 fn_walk.data.types.gen_param_types = &gen_param_types;
1243 walk_function_params(g, fn_type, &fn_walk);
1244 }
1245
1246 fn_type->data.fn.gen_param_count = gen_param_types.length;
1247
1248 for (size_t i = 0; i < gen_param_types.length; i += 1) {
1249 assert(gen_param_types.items[i] != nullptr);
1250 }
1251 fn_type->data.fn.raw_type_ref = LLVMFunctionType(gen_return_type->type_ref,
1252 gen_param_types.items, (unsigned int)gen_param_types.length, fn_type_id->is_var_args);
1253 fn_type->type_ref = LLVMPointerType(fn_type->data.fn.raw_type_ref, 0);
1254 fn_type->data.fn.raw_di_type = ZigLLVMCreateSubroutineType(g->dbuilder, param_di_types.items, (int)param_di_types.length, 0);
1255 fn_type->di_type = ZigLLVMCreateDebugPointerType(g->dbuilder, fn_type->data.fn.raw_di_type,
1256 LLVMStoreSizeOfType(g->target_data_ref, fn_type->type_ref),
1257 LLVMABIAlignmentOfType(g->target_data_ref, fn_type->type_ref), "");
1258 }
1259911
1260 g->fn_type_table.put(&fn_type->data.fn.fn_type_id, fn_type);912 g->fn_type_table.put(&fn_type->data.fn.fn_type_id, fn_type);
1261913
...@@ -1282,14 +934,6 @@ static ZigType *get_root_container_type(CodeGen *g, const char *full_name, Buf *...@@ -1282,14 +934,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);934 entry->data.structure.decls_scope = create_decls_scope(g, nullptr, nullptr, entry, entry, bare_name);
1283 entry->data.structure.root_struct = root_struct;935 entry->data.structure.root_struct = root_struct;
1284 entry->data.structure.layout = ContainerLayoutAuto;936 entry->data.structure.layout = ContainerLayoutAuto;
1285 entry->type_ref = LLVMStructCreateNamed(LLVMGetGlobalContext(), full_name);
1286
1287 size_t line = 0; // root therefore first line
1288 unsigned dwarf_kind = ZigLLVMTag_DW_structure_type();
1289
1290 entry->di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
1291 dwarf_kind, full_name,
1292 ZigLLVMFileToScope(root_struct->di_file), root_struct->di_file, (unsigned)(line + 1));
1293937
1294 buf_init_from_str(&entry->name, full_name);938 buf_init_from_str(&entry->name, full_name);
1295 return entry;939 return entry;
...@@ -1316,16 +960,6 @@ ZigType *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind...@@ -1316,16 +960,6 @@ ZigType *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind
1316 break;960 break;
1317 }961 }
1318962
1319 size_t line = decl_node ? decl_node->line : 0;
1320 unsigned dwarf_kind = ZigLLVMTag_DW_structure_type();
1321
1322 ZigType *import = get_scope_import(scope);
1323 entry->type_ref = LLVMStructCreateNamed(LLVMGetGlobalContext(), full_name);
1324 entry->di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
1325 dwarf_kind, full_name,
1326 ZigLLVMFileToScope(import->data.structure.root_struct->di_file),
1327 import->data.structure.root_struct->di_file, (unsigned)(line + 1));
1328
1329 buf_init_from_str(&entry->name, full_name);963 buf_init_from_str(&entry->name, full_name);
1330964
1331 return entry;965 return entry;
...@@ -1375,7 +1009,9 @@ ZigType *get_generic_fn_type(CodeGen *g, FnTypeId *fn_type_id) {...@@ -1375,7 +1009,9 @@ ZigType *get_generic_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
13751009
1376 fn_type->data.fn.fn_type_id = *fn_type_id;1010 fn_type->data.fn.fn_type_id = *fn_type_id;
1377 fn_type->data.fn.is_generic = true;1011 fn_type->data.fn.is_generic = true;
1378 fn_type->zero_bits = true;1012 fn_type->abi_size = 0;
1013 fn_type->size_in_bits = 0;
1014 fn_type->abi_align = 0;
1379 return fn_type;1015 return fn_type;
1380}1016}
13811017
...@@ -1488,6 +1124,7 @@ static Error emit_error_unless_type_allowed_in_packed_struct(CodeGen *g, ZigType...@@ -1488,6 +1124,7 @@ static Error emit_error_unless_type_allowed_in_packed_struct(CodeGen *g, ZigType
1488 ZigType *elem_type = type_entry->data.array.child_type;1124 ZigType *elem_type = type_entry->data.array.child_type;
1489 if ((err = emit_error_unless_type_allowed_in_packed_struct(g, elem_type, source_node)))1125 if ((err = emit_error_unless_type_allowed_in_packed_struct(g, elem_type, source_node)))
1490 return err;1126 return err;
1127 // TODO revisit this when doing https://github.com/ziglang/zig/issues/1512
1491 if (type_size(g, type_entry) * 8 == type_size_bits(g, type_entry))1128 if (type_size(g, type_entry) * 8 == type_size_bits(g, type_entry))
1492 return ErrorNone;1129 return ErrorNone;
1493 add_node_error(g, source_node,1130 add_node_error(g, source_node,
...@@ -1611,13 +1248,12 @@ ZigType *get_auto_err_set_type(CodeGen *g, ZigFn *fn_entry) {...@@ -1611,13 +1248,12 @@ ZigType *get_auto_err_set_type(CodeGen *g, ZigFn *fn_entry) {
1611 ZigType *err_set_type = new_type_table_entry(ZigTypeIdErrorSet);1248 ZigType *err_set_type = new_type_table_entry(ZigTypeIdErrorSet);
1612 buf_resize(&err_set_type->name, 0);1249 buf_resize(&err_set_type->name, 0);
1613 buf_appendf(&err_set_type->name, "@typeOf(%s).ReturnType.ErrorSet", buf_ptr(&fn_entry->symbol_name));1250 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;1251 err_set_type->data.error_set.err_count = 0;
1617 err_set_type->data.error_set.errors = nullptr;1252 err_set_type->data.error_set.errors = nullptr;
1618 err_set_type->data.error_set.infer_fn = fn_entry;1253 err_set_type->data.error_set.infer_fn = fn_entry;
16191254 err_set_type->size_in_bits = g->builtin_types.entry_global_error_set->size_in_bits;
1620 g->error_di_types.append(&err_set_type->di_type);1255 err_set_type->abi_align = g->builtin_types.entry_global_error_set->abi_align;
1256 err_set_type->abi_size = g->builtin_types.entry_global_error_set->abi_size;
16211257
1622 return err_set_type;1258 return err_set_type;
1623}1259}
...@@ -1858,10 +1494,10 @@ bool type_is_invalid(ZigType *type_entry) {...@@ -1858,10 +1494,10 @@ bool type_is_invalid(ZigType *type_entry) {
1858 return true;1494 return true;
1859 case ZigTypeIdStruct:1495 case ZigTypeIdStruct:
1860 return type_entry->data.structure.resolve_status == ResolveStatusInvalid;1496 return type_entry->data.structure.resolve_status == ResolveStatusInvalid;
1497 case ZigTypeIdUnion:
1498 return type_entry->data.unionation.resolve_status == ResolveStatusInvalid;
1861 case ZigTypeIdEnum:1499 case ZigTypeIdEnum:
1862 return type_entry->data.enumeration.is_invalid;1500 return type_entry->data.enumeration.is_invalid;
1863 case ZigTypeIdUnion:
1864 return type_entry->data.unionation.is_invalid;
1865 default:1501 default:
1866 return false;1502 return false;
1867 }1503 }
...@@ -1869,178 +1505,80 @@ bool type_is_invalid(ZigType *type_entry) {...@@ -1869,178 +1505,80 @@ bool type_is_invalid(ZigType *type_entry) {
1869}1505}
18701506
18711507
1872static Error resolve_enum_type(CodeGen *g, ZigType *enum_type) {1508ZigType *get_struct_type(CodeGen *g, const char *type_name, const char *field_names[],
1873 assert(enum_type->id == ZigTypeIdEnum);1509 ZigType *field_types[], size_t field_count)
18741510{
1875 if (enum_type->data.enumeration.is_invalid)1511 ZigType *struct_type = new_type_table_entry(ZigTypeIdStruct);
1876 return ErrorSemanticAnalyzeFail;
18771512
1878 if (enum_type->data.enumeration.complete)1513 buf_init_from_str(&struct_type->name, type_name);
1879 return ErrorNone;
18801514
1881 Error err;1515 struct_type->data.structure.src_field_count = field_count;
1882 if ((err = resolve_enum_zero_bits(g, enum_type)))1516 struct_type->data.structure.gen_field_count = 0;
1883 return err;1517 struct_type->data.structure.resolve_status = ResolveStatusSizeKnown;
1518 struct_type->data.structure.fields = allocate<TypeStructField>(field_count);
1519 struct_type->data.structure.fields_by_name.init(field_count);
18841520
1885 AstNode *decl_node = enum_type->data.enumeration.decl_node;1521 size_t abi_align = 0;
1522 for (size_t i = 0; i < field_count; i += 1) {
1523 TypeStructField *field = &struct_type->data.structure.fields[i];
1524 field->name = buf_create_from_str(field_names[i]);
1525 field->type_entry = field_types[i];
1526 field->src_index = i;
18861527
1887 if (enum_type->data.enumeration.embedded_in_current) {1528 if (type_has_bits(field->type_entry)) {
1888 if (!enum_type->data.enumeration.reported_infinite_err) {1529 assert(type_is_resolved(field->type_entry, ResolveStatusSizeKnown));
1889 enum_type->data.enumeration.is_invalid = true;1530 if (field->type_entry->abi_align > abi_align) {
1890 enum_type->data.enumeration.reported_infinite_err = true;1531 abi_align = field->type_entry->abi_align;
1891 ErrorMsg *msg = add_node_error(g, decl_node,1532 }
1892 buf_sprintf("enum '%s' contains itself", buf_ptr(&enum_type->name)));1533 field->gen_index = struct_type->data.structure.gen_field_count;
1893 emit_error_notes_for_ref_stack(g, msg);1534 struct_type->data.structure.gen_field_count += 1;
1535 } else {
1536 field->gen_index = SIZE_MAX;
1894 }1537 }
1895 return ErrorSemanticAnalyzeFail;1538
1539 auto prev_entry = struct_type->data.structure.fields_by_name.put_unique(field->name, field);
1540 assert(prev_entry == nullptr);
1896 }1541 }
18971542
1898 assert(!enum_type->data.enumeration.zero_bits_loop_flag);1543 size_t next_offset = 0;
1899 assert(decl_node->type == NodeTypeContainerDecl);1544 for (size_t i = 0; i < field_count; i += 1) {
1900 assert(enum_type->di_type);1545 TypeStructField *field = &struct_type->data.structure.fields[i];
1546 if (field->gen_index == SIZE_MAX)
1547 continue;
1548 field->offset = next_offset;
1549 size_t next_src_field_index = i + 1;
1550 for (; next_src_field_index < field_count; next_src_field_index += 1) {
1551 if (struct_type->data.structure.fields[next_src_field_index].gen_index != SIZE_MAX) {
1552 break;
1553 }
1554 }
1555 size_t next_abi_align = (next_src_field_index == field_count) ?
1556 abi_align : struct_type->data.structure.fields[next_src_field_index].type_entry->abi_align;
1557 next_offset = next_field_offset(next_offset, abi_align, field->type_entry->abi_size, next_abi_align);
1558 }
19011559
1902 uint32_t field_count = enum_type->data.enumeration.src_field_count;1560 struct_type->abi_align = abi_align;
1561 struct_type->abi_size = next_offset;
1562 struct_type->size_in_bits = next_offset * 8;
19031563
1904 assert(enum_type->data.enumeration.fields);1564 return struct_type;
1905 ZigLLVMDIEnumerator **di_enumerators = allocate<ZigLLVMDIEnumerator*>(field_count);
1906
1907 Scope *scope = &enum_type->data.enumeration.decls_scope->base;
1908 ZigType *import = get_scope_import(scope);
1909
1910 // set temporary flag
1911 enum_type->data.enumeration.embedded_in_current = true;
1912
1913 for (uint32_t i = 0; i < field_count; i += 1) {
1914 TypeEnumField *enum_field = &enum_type->data.enumeration.fields[i];
1915
1916 // TODO send patch to LLVM to support APInt in createEnumerator instead of int64_t
1917 // http://lists.llvm.org/pipermail/llvm-dev/2017-December/119456.html
1918 di_enumerators[i] = ZigLLVMCreateDebugEnumerator(g->dbuilder, buf_ptr(enum_field->name),
1919 bigint_as_signed(&enum_field->value));
1920 }
1921
1922 // unset temporary flag
1923 enum_type->data.enumeration.embedded_in_current = false;
1924 enum_type->data.enumeration.complete = true;
1925
1926 if (enum_type->data.enumeration.is_invalid)
1927 return ErrorSemanticAnalyzeFail;
1928
1929 if (enum_type->zero_bits) {
1930 enum_type->type_ref = LLVMVoidType();
1931
1932 uint64_t debug_size_in_bits = 0;
1933 uint64_t debug_align_in_bits = 0;
1934 ZigLLVMDIType **di_root_members = nullptr;
1935 size_t debug_member_count = 0;
1936 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
1937 ZigLLVMFileToScope(import->data.structure.root_struct->di_file),
1938 buf_ptr(&enum_type->name),
1939 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
1940 debug_size_in_bits,
1941 debug_align_in_bits,
1942 0, nullptr, di_root_members, (int)debug_member_count, 0, nullptr, "");
1943
1944 ZigLLVMReplaceTemporary(g->dbuilder, enum_type->di_type, replacement_di_type);
1945 enum_type->di_type = replacement_di_type;
1946 return ErrorNone;
1947 }
1948
1949 ZigType *tag_int_type = enum_type->data.enumeration.tag_int_type;
1950
1951 // create debug type for tag
1952 uint64_t tag_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, tag_int_type->type_ref);
1953 uint64_t tag_debug_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, tag_int_type->type_ref);
1954 ZigLLVMDIType *tag_di_type = ZigLLVMCreateDebugEnumerationType(g->dbuilder,
1955 ZigLLVMFileToScope(import->data.structure.root_struct->di_file), buf_ptr(&enum_type->name),
1956 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
1957 tag_debug_size_in_bits,
1958 tag_debug_align_in_bits,
1959 di_enumerators, field_count,
1960 tag_int_type->di_type, "");
1961
1962 ZigLLVMReplaceTemporary(g->dbuilder, enum_type->di_type, tag_di_type);
1963 enum_type->di_type = tag_di_type;
1964 return ErrorNone;
1965}1565}
19661566
1567static size_t get_store_size_bytes(size_t size_in_bits) {
1568 return (size_in_bits + 7) / 8;
1569}
19671570
1968ZigType *get_struct_type(CodeGen *g, const char *type_name, const char *field_names[],1571static size_t get_abi_align_bytes(size_t size_in_bits, size_t pointer_size_bytes) {
1969 ZigType *field_types[], size_t field_count)1572 size_t store_size_bytes = get_store_size_bytes(size_in_bits);
1970{1573 if (store_size_bytes >= pointer_size_bytes)
1971 ZigType *struct_type = new_type_table_entry(ZigTypeIdStruct);1574 return pointer_size_bytes;
19721575 return round_to_next_power_of_2(store_size_bytes);
1973 buf_init_from_str(&struct_type->name, type_name);1576}
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);
20421577
2043 return struct_type;1578static size_t get_abi_size_bytes(size_t size_in_bits, size_t pointer_size_bytes) {
1579 size_t store_size_bytes = get_store_size_bytes(size_in_bits);
1580 size_t abi_align = get_abi_align_bytes(size_in_bits, pointer_size_bytes);
1581 return align_forward(store_size_bytes, abi_align);
2044}1582}
20451583
2046static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {1584static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
...@@ -2068,454 +1606,313 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {...@@ -2068,454 +1606,313 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
2068 return ErrorSemanticAnalyzeFail;1606 return ErrorSemanticAnalyzeFail;
2069 }1607 }
20701608
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);1609 assert(struct_type->data.structure.fields || struct_type->data.structure.src_field_count == 0);
2074 assert(decl_node->type == NodeTypeContainerDecl);1610 assert(decl_node->type == NodeTypeContainerDecl);
20751611
2076 size_t field_count = struct_type->data.structure.src_field_count;1612 size_t field_count = struct_type->data.structure.src_field_count;
20771613
2078 size_t gen_field_count = struct_type->data.structure.gen_field_count;
2079 LLVMTypeRef *element_types = allocate<LLVMTypeRef>(gen_field_count);
2080
2081 Scope *scope = &struct_type->data.structure.decls_scope->base;
2082
2083 size_t gen_field_index = 0;
2084 bool packed = (struct_type->data.structure.layout == ContainerLayoutPacked);1614 bool packed = (struct_type->data.structure.layout == ContainerLayoutPacked);
2085 size_t packed_bits_offset = 0;1615 struct_type->data.structure.resolve_loop_flag = true;
2086 size_t first_packed_bits_offset_misalign = SIZE_MAX;
2087 size_t debug_field_count = 0;
20881616
2089 for (size_t i = 0; i < field_count; i += 1) {1617 uint32_t *host_int_bytes = packed ? allocate<uint32_t>(struct_type->data.structure.gen_field_count) : nullptr;
2090 TypeStructField *type_struct_field = &struct_type->data.structure.fields[i];
2091 ZigType *field_type = type_struct_field->type_entry;
20921618
2093 if ((err = ensure_complete_type(g, field_type))) {1619 // Resolve sizes of all the field types. Done before the offset loop because the offset
1620 // loop has to look ahead.
1621 for (size_t i = 0; i < field_count; i += 1) {
1622 TypeStructField *field = &struct_type->data.structure.fields[i];
1623 if ((err = type_resolve(g, field->type_entry, ResolveStatusSizeKnown))) {
2094 struct_type->data.structure.resolve_status = ResolveStatusInvalid;1624 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2095 break;1625 return ErrorSemanticAnalyzeFail;
2096 }1626 }
1627 }
20971628
2098 if (struct_type->data.structure.layout == ContainerLayoutExtern) {1629 size_t packed_bits_offset = 0;
2099 if (!type_allowed_in_extern(g, field_type)) {1630 size_t next_offset = 0;
2100 AstNode *field_source_node = decl_node->data.container_decl.fields.at(i);1631 size_t first_packed_bits_offset_misalign = SIZE_MAX;
2101 add_node_error(g, field_source_node,1632 size_t gen_field_index = 0;
2102 buf_sprintf("extern structs cannot contain fields of type '%s'",1633 size_t size_in_bits = 0;
2103 buf_ptr(&field_type->name)));1634 size_t abi_align = struct_type->abi_align;
2104 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2105 break;
2106 }
2107 }
21081635
2109 if (!type_has_bits(field_type))1636 // Calculate offsets
1637 for (size_t i = 0; i < field_count; i += 1) {
1638 TypeStructField *field = &struct_type->data.structure.fields[i];
1639 if (field->gen_index == SIZE_MAX)
2110 continue;1640 continue;
1641 ZigType *field_type = field->type_entry;
1642 assert(field_type != nullptr);
21111643
2112 type_struct_field->gen_index = gen_field_index;1644 field->gen_index = gen_field_index;
1645 field->offset = next_offset;
21131646
2114 if (packed) {1647 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);1648 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;1649 size_t next_packed_bits_offset = packed_bits_offset + field_size_in_bits;
21231650
1651 size_in_bits += field_size_in_bits;
1652
2124 if (first_packed_bits_offset_misalign != SIZE_MAX) {1653 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 offset1654 // this field is not byte-aligned; it is part of the previous field with a bit offset
2126 type_struct_field->bit_offset_in_host = packed_bits_offset - first_packed_bits_offset_misalign;1655 field->bit_offset_in_host = packed_bits_offset - first_packed_bits_offset_misalign;
21271656
2128 size_t full_bit_count = next_packed_bits_offset - first_packed_bits_offset_misalign;1657 size_t full_bit_count = next_packed_bits_offset - first_packed_bits_offset_misalign;
2129 LLVMTypeRef int_type_ref = LLVMIntType((unsigned)(full_bit_count));1658 size_t full_abi_size = get_abi_size_bytes(full_bit_count, g->pointer_size_bytes);
2130 if (8 * LLVMStoreSizeOfType(g->target_data_ref, int_type_ref) == full_bit_count) {1659 if (full_abi_size * 8 == full_bit_count) {
2131 // next field recovers store alignment1660 // next field recovers ABI alignment
2132 element_types[gen_field_index] = int_type_ref;1661 host_int_bytes[gen_field_index] = full_abi_size;
2133 gen_field_index += 1;1662 gen_field_index += 1;
1663 // TODO: https://github.com/ziglang/zig/issues/1512
1664 next_offset = next_field_offset(next_offset, abi_align, full_abi_size, 1);
1665 size_in_bits = next_offset * 8;
21341666
2135 first_packed_bits_offset_misalign = SIZE_MAX;1667 first_packed_bits_offset_misalign = SIZE_MAX;
2136 }1668 }
2137 } else if (8 * LLVMStoreSizeOfType(g->target_data_ref, field_type->type_ref) != field_size_in_bits) {1669 } else if (get_abi_size_bytes(field_type->size_in_bits, g->pointer_size_bytes) * 8 != field_size_in_bits) {
2138 first_packed_bits_offset_misalign = packed_bits_offset;1670 first_packed_bits_offset_misalign = packed_bits_offset;
2139 type_struct_field->bit_offset_in_host = 0;1671 field->bit_offset_in_host = 0;
2140 } else {1672 } else {
2141 // This is a byte-aligned field (both start and end) in a packed struct.1673 // This is a byte-aligned field (both start and end) in a packed struct.
2142 element_types[gen_field_index] = field_type->type_ref;1674 host_int_bytes[gen_field_index] = field_type->size_in_bits / 8;
2143 type_struct_field->bit_offset_in_host = 0;1675 field->bit_offset_in_host = 0;
2144 gen_field_index += 1;1676 gen_field_index += 1;
1677 // TODO: https://github.com/ziglang/zig/issues/1512
1678 next_offset = next_field_offset(next_offset, abi_align, field_type->size_in_bits / 8, 1);
1679 size_in_bits = next_offset * 8;
2145 }1680 }
2146 packed_bits_offset = next_packed_bits_offset;1681 packed_bits_offset = next_packed_bits_offset;
2147 } else {1682 } else {
2148 element_types[gen_field_index] = field_type->type_ref;
2149 assert(element_types[gen_field_index]);
2150
2151 gen_field_index += 1;1683 gen_field_index += 1;
1684 size_t next_src_field_index = i + 1;
1685 for (; next_src_field_index < field_count; next_src_field_index += 1) {
1686 if (struct_type->data.structure.fields[next_src_field_index].gen_index != SIZE_MAX) {
1687 break;
1688 }
1689 }
1690 size_t next_abi_align = (next_src_field_index == field_count) ?
1691 abi_align : struct_type->data.structure.fields[next_src_field_index].type_entry->abi_align;
1692 next_offset = next_field_offset(next_offset, abi_align, field_type->abi_size, next_abi_align);
1693 size_in_bits = next_offset * 8;
2152 }1694 }
2153 debug_field_count += 1;
2154 }1695 }
2155 if (first_packed_bits_offset_misalign != SIZE_MAX) {1696 if (first_packed_bits_offset_misalign != SIZE_MAX) {
2156 size_t full_bit_count = packed_bits_offset - first_packed_bits_offset_misalign;1697 size_t full_bit_count = packed_bits_offset - first_packed_bits_offset_misalign;
2157 LLVMTypeRef int_type_ref = LLVMIntType((unsigned)full_bit_count);1698 size_t full_abi_size = get_abi_size_bytes(full_bit_count, g->pointer_size_bytes);
2158 size_t store_bit_count = 8 * LLVMStoreSizeOfType(g->target_data_ref, int_type_ref);1699 next_offset = next_field_offset(next_offset, abi_align, full_abi_size, abi_align);
2159 element_types[gen_field_index] = LLVMIntType((unsigned)store_bit_count);1700 host_int_bytes[gen_field_index] = full_abi_size;
2160 gen_field_index += 1;1701 gen_field_index += 1;
2161 }1702 }
21621703
1704 struct_type->abi_size = next_offset;
1705 struct_type->size_in_bits = size_in_bits;
1706 struct_type->data.structure.resolve_status = ResolveStatusSizeKnown;
1707 struct_type->data.structure.gen_field_count = (uint32_t)gen_field_index;
2163 struct_type->data.structure.resolve_loop_flag = false;1708 struct_type->data.structure.resolve_loop_flag = false;
1709 struct_type->data.structure.host_int_bytes = host_int_bytes;
21641710
2165 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid)1711 return ErrorNone;
2166 return ErrorSemanticAnalyzeFail;1712}
21671713
2168 struct_type->data.structure.resolve_status = ResolveStatusSizeKnown;1714static Error resolve_union_alignment(CodeGen *g, ZigType *union_type) {
1715 assert(union_type->id == ZigTypeIdUnion);
21691716
2170 if (struct_type->zero_bits) {1717 Error err;
2171 struct_type->type_ref = LLVMVoidType();
21721718
2173 ZigType *import = get_scope_import(scope);1719 if (union_type->data.unionation.resolve_status == ResolveStatusInvalid)
2174 uint64_t debug_size_in_bits = 0;1720 return ErrorSemanticAnalyzeFail;
2175 uint64_t debug_align_in_bits = 0;1721 if (union_type->data.unionation.resolve_status >= ResolveStatusAlignmentKnown)
2176 ZigLLVMDIType **di_element_types = nullptr;1722 return ErrorNone;
2177 size_t debug_field_count = 0;1723 if ((err = resolve_union_zero_bits(g, union_type)))
2178 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,1724 return err;
2179 ZigLLVMFileToScope(import->data.structure.root_struct->di_file),1725 if (union_type->data.unionation.resolve_status >= ResolveStatusAlignmentKnown)
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;1726 return ErrorNone;
1727
1728 if (union_type->data.unionation.resolve_loop_flag) {
1729 if (!union_type->data.unionation.reported_infinite_err) {
1730 AstNode *decl_node = union_type->data.unionation.decl_node;
1731 union_type->data.unionation.reported_infinite_err = true;
1732 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
1733 ErrorMsg *msg = add_node_error(g, decl_node,
1734 buf_sprintf("union '%s' contains itself", buf_ptr(&union_type->name)));
1735 emit_error_notes_for_ref_stack(g, msg);
1736 }
1737 return ErrorSemanticAnalyzeFail;
2188 }1738 }
2189 assert(struct_type->di_type);
21901739
1740 // set temporary flag
1741 union_type->data.unionation.resolve_loop_flag = true;
21911742
2192 // the count may have been adjusting from packing bit fields1743 ZigType *most_aligned_union_member = nullptr;
2193 gen_field_count = gen_field_index;1744 uint32_t field_count = union_type->data.unionation.src_field_count;
2194 struct_type->data.structure.gen_field_count = (uint32_t)gen_field_count;1745 bool packed = union_type->data.unionation.layout == ContainerLayoutPacked;
21951746
2196 LLVMStructSetBody(struct_type->type_ref, element_types, (unsigned)gen_field_count, packed);1747 for (uint32_t i = 0; i < field_count; i += 1) {
1748 TypeUnionField *field = &union_type->data.unionation.fields[i];
1749 if (field->gen_index == UINT32_MAX)
1750 continue;
21971751
2198 // if you hit this assert then probably this type or a related type didn't1752 size_t this_field_align;
2199 // get ensure_complete_type called on it before using it with something that1753 if (packed) {
2200 // requires a complete type1754 // TODO: https://github.com/ziglang/zig/issues/1512
2201 assert(LLVMStoreSizeOfType(g->target_data_ref, struct_type->type_ref) > 0);1755 this_field_align = 1;
1756 // This is the same hack as resolve_struct_alignment. See the comment there.
1757 } else if (field->type_entry == nullptr) {
1758 this_field_align = g->builtin_types.entry_usize->abi_align;
1759 } else {
1760 if ((err = type_resolve(g, field->type_entry, ResolveStatusAlignmentKnown))) {
1761 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
1762 return ErrorSemanticAnalyzeFail;
1763 }
22021764
2203 ZigLLVMDIType **di_element_types = allocate<ZigLLVMDIType*>(debug_field_count);1765 if (union_type->data.unionation.resolve_status == ResolveStatusInvalid)
1766 return ErrorSemanticAnalyzeFail;
22041767
2205 ZigType *import = get_scope_import(scope);1768 this_field_align = field->type_entry->abi_align;
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;
2213 }1769 }
22141770
2215 ZigType *field_type = type_struct_field->type_entry;1771 if (most_aligned_union_member == nullptr ||
22161772 this_field_align > most_aligned_union_member->abi_align)
2217 // if the field is a function, actually the debug info should be a pointer.1773 {
2218 ZigLLVMDIType *field_di_type;1774 most_aligned_union_member = field->type_entry;
2219 if (field_type->id == ZigTypeIdFn) {
2220 ZigType *field_ptr_type = get_pointer_to_type(g, field_type, true);
2221 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, field_ptr_type->type_ref);
2222 uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, field_ptr_type->type_ref);
2223 field_di_type = ZigLLVMCreateDebugPointerType(g->dbuilder, field_type->di_type,
2224 debug_size_in_bits, debug_align_in_bits, buf_ptr(&field_ptr_type->name));
2225 } else {
2226 field_di_type = field_type->di_type;
2227 }1775 }
1776 }
22281777
2229 assert(field_type->type_ref);1778 // unset temporary flag
2230 assert(struct_type->type_ref);1779 union_type->data.unionation.resolve_loop_flag = false;
2231 assert(struct_type->data.structure.resolve_status == ResolveStatusSizeKnown);1780 union_type->data.unionation.resolve_status = ResolveStatusAlignmentKnown;
2232 uint64_t debug_size_in_bits;1781 union_type->data.unionation.most_aligned_union_member = most_aligned_union_member;
2233 uint64_t debug_align_in_bits;1782
2234 uint64_t debug_offset_in_bits;1783 ZigType *tag_type = union_type->data.unionation.tag_type;
2235 if (packed) {1784 if (tag_type != nullptr && type_has_bits(tag_type)) {
2236 debug_size_in_bits = type_size_bits(g, type_struct_field->type_entry);1785 if ((err = type_resolve(g, tag_type, ResolveStatusAlignmentKnown))) {
2237 debug_align_in_bits = 1;1786 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
2238 debug_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, struct_type->type_ref,1787 return ErrorSemanticAnalyzeFail;
2239 (unsigned)gen_field_index) + type_struct_field->bit_offset_in_host;1788 }
1789 if (most_aligned_union_member == nullptr) {
1790 union_type->abi_align = tag_type->abi_align;
1791 union_type->data.unionation.gen_tag_index = SIZE_MAX;
1792 union_type->data.unionation.gen_union_index = SIZE_MAX;
1793 } else if (tag_type->abi_align > most_aligned_union_member->abi_align) {
1794 union_type->abi_align = tag_type->abi_align;
1795 union_type->data.unionation.gen_tag_index = 0;
1796 union_type->data.unionation.gen_union_index = 1;
2240 } else {1797 } else {
2241 debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, field_type->type_ref);1798 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);1799 union_type->data.unionation.gen_union_index = 0;
2243 debug_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, struct_type->type_ref,1800 union_type->data.unionation.gen_tag_index = 1;
2244 (unsigned)gen_field_index);
2245 }1801 }
2246 di_element_types[debug_field_index] = ZigLLVMCreateDebugMemberType(g->dbuilder,1802 } else {
2247 ZigLLVMTypeToScope(struct_type->di_type), buf_ptr(type_struct_field->name),1803 assert(most_aligned_union_member != nullptr);
2248 import->data.structure.root_struct->di_file, (unsigned)(field_node->line + 1),1804 union_type->abi_align = most_aligned_union_member->abi_align;
2249 debug_size_in_bits,1805 union_type->data.unionation.gen_union_index = SIZE_MAX;
2250 debug_align_in_bits,1806 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 }1807 }
22561808
2257
2258 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, struct_type->type_ref);
2259 uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, struct_type->type_ref);
2260 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
2261 ZigLLVMFileToScope(import->data.structure.root_struct->di_file),
2262 buf_ptr(&struct_type->name),
2263 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
2264 debug_size_in_bits,
2265 debug_align_in_bits,
2266 0, nullptr, di_element_types, (int)debug_field_count, 0, nullptr, "");
2267
2268 ZigLLVMReplaceTemporary(g->dbuilder, struct_type->di_type, replacement_di_type);
2269 struct_type->di_type = replacement_di_type;
2270
2271 return ErrorNone;1809 return ErrorNone;
2272}1810}
22731811
2274static Error resolve_union_type(CodeGen *g, ZigType *union_type) {1812static Error resolve_union_type(CodeGen *g, ZigType *union_type) {
2275 assert(union_type->id == ZigTypeIdUnion);1813 assert(union_type->id == ZigTypeIdUnion);
22761814
2277 if (union_type->data.unionation.complete)1815 Error err;
1816
1817 if (union_type->data.unionation.resolve_status == ResolveStatusInvalid)
1818 return ErrorSemanticAnalyzeFail;
1819 if (union_type->data.unionation.resolve_status >= ResolveStatusSizeKnown)
2278 return ErrorNone;1820 return ErrorNone;
22791821
2280 Error err;1822 if ((err = resolve_union_alignment(g, union_type)))
2281 if ((err = resolve_union_zero_bits(g, union_type)))
2282 return err;1823 return err;
22831824
2284 AstNode *decl_node = union_type->data.unionation.decl_node;1825 AstNode *decl_node = union_type->data.unionation.decl_node;
22851826
2286 if (union_type->data.unionation.embedded_in_current) {1827
1828 assert(decl_node->type == NodeTypeContainerDecl);
1829
1830 uint32_t field_count = union_type->data.unionation.src_field_count;
1831 ZigType *most_aligned_union_member = union_type->data.unionation.most_aligned_union_member;
1832
1833 assert(union_type->data.unionation.fields);
1834
1835 size_t union_abi_size = 0;
1836 size_t union_size_in_bits = 0;
1837
1838 if (union_type->data.unionation.resolve_loop_flag) {
2287 if (!union_type->data.unionation.reported_infinite_err) {1839 if (!union_type->data.unionation.reported_infinite_err) {
2288 union_type->data.unionation.reported_infinite_err = true;1840 union_type->data.unionation.reported_infinite_err = true;
2289 union_type->data.unionation.is_invalid = true;1841 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
2290 ErrorMsg *msg = add_node_error(g, decl_node,1842 ErrorMsg *msg = add_node_error(g, decl_node,
2291 buf_sprintf("union '%s' contains itself", buf_ptr(&union_type->name)));1843 buf_sprintf("union '%s' depends on its own size", buf_ptr(&union_type->name)));
2292 emit_error_notes_for_ref_stack(g, msg);1844 emit_error_notes_for_ref_stack(g, msg);
2293 }1845 }
2294 return ErrorSemanticAnalyzeFail;1846 return ErrorSemanticAnalyzeFail;
2295 }1847 }
22961848
2297 assert(!union_type->data.unionation.zero_bits_loop_flag);1849 // set temporary flag
2298 assert(decl_node->type == NodeTypeContainerDecl);1850 union_type->data.unionation.resolve_loop_flag = true;
2299 assert(union_type->di_type);
2300
2301 uint32_t field_count = union_type->data.unionation.src_field_count;
23021851
2303 assert(union_type->data.unionation.fields);1852 for (uint32_t i = 0; i < field_count; i += 1) {
1853 TypeUnionField *union_field = &union_type->data.unionation.fields[i];
1854 ZigType *field_type = union_field->type_entry;
23041855
2305 uint32_t gen_field_count = union_type->data.unionation.gen_field_count;1856 if ((err = type_resolve(g, field_type, ResolveStatusSizeKnown))) {
2306 ZigLLVMDIType **union_inner_di_types = allocate<ZigLLVMDIType*>(gen_field_count);1857 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
23071858 return ErrorSemanticAnalyzeFail;
2308 ZigType *most_aligned_union_member = nullptr;
2309 uint64_t size_of_most_aligned_member_in_bits = 0;
2310 uint64_t biggest_align_in_bits = 0;
2311 uint64_t biggest_size_in_bits = 0;
2312
2313 Scope *scope = &union_type->data.unionation.decls_scope->base;
2314 ZigType *import = get_scope_import(scope);
2315
2316 // set temporary flag
2317 union_type->data.unionation.embedded_in_current = true;
2318
2319
2320 for (uint32_t i = 0; i < field_count; i += 1) {
2321 AstNode *field_node = decl_node->data.container_decl.fields.at(i);
2322 TypeUnionField *union_field = &union_type->data.unionation.fields[i];
2323 ZigType *field_type = union_field->type_entry;
2324
2325 if ((err = ensure_complete_type(g, field_type))) {
2326 union_type->data.unionation.is_invalid = true;
2327 continue;
2328 }1859 }
23291860
1861 if (type_is_invalid(union_type))
1862 return ErrorSemanticAnalyzeFail;
1863
2330 if (!type_has_bits(field_type))1864 if (!type_has_bits(field_type))
2331 continue;1865 continue;
23321866
2333 uint64_t store_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, field_type->type_ref);1867 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);1868 union_size_in_bits = max(union_size_in_bits, field_type->size_in_bits);
2335
2336 assert(store_size_in_bits > 0);
2337 assert(abi_align_in_bits > 0);
2338
2339 union_inner_di_types[union_field->gen_index] = ZigLLVMCreateDebugMemberType(g->dbuilder,
2340 ZigLLVMTypeToScope(union_type->di_type), buf_ptr(union_field->enum_field->name),
2341 import->data.structure.root_struct->di_file, (unsigned)(field_node->line + 1),
2342 store_size_in_bits,
2343 abi_align_in_bits,
2344 0,
2345 0, field_type->di_type);
2346
2347 biggest_size_in_bits = max(biggest_size_in_bits, store_size_in_bits);
2348
2349 if (!most_aligned_union_member || abi_align_in_bits > biggest_align_in_bits) {
2350 most_aligned_union_member = field_type;
2351 biggest_align_in_bits = abi_align_in_bits;
2352 size_of_most_aligned_member_in_bits = store_size_in_bits;
2353 }
2354 }1869 }
23551870
23561871 // The union itself for now has to be treated as being independently aligned.
2357 // unset temporary flag1872 // See https://github.com/ziglang/zig/issues/2166.
2358 union_type->data.unionation.embedded_in_current = false;1873 if (most_aligned_union_member != nullptr) {
2359 union_type->data.unionation.complete = true;1874 union_abi_size = align_forward(union_abi_size, most_aligned_union_member->abi_align);
2360 union_type->data.unionation.union_size_bytes = biggest_size_in_bits / 8;
2361 union_type->data.unionation.most_aligned_union_member = most_aligned_union_member;
2362
2363 if (union_type->data.unionation.is_invalid)
2364 return ErrorSemanticAnalyzeFail;
2365
2366 if (union_type->zero_bits) {
2367 union_type->type_ref = LLVMVoidType();
2368
2369 uint64_t debug_size_in_bits = 0;
2370 uint64_t debug_align_in_bits = 0;
2371 ZigLLVMDIType **di_root_members = nullptr;
2372 size_t debug_member_count = 0;
2373 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugUnionType(g->dbuilder,
2374 ZigLLVMFileToScope(import->data.structure.root_struct->di_file),
2375 buf_ptr(&union_type->name),
2376 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
2377 debug_size_in_bits,
2378 debug_align_in_bits,
2379 0, di_root_members, (int)debug_member_count, 0, "");
2380
2381 ZigLLVMReplaceTemporary(g->dbuilder, union_type->di_type, replacement_di_type);
2382 union_type->di_type = replacement_di_type;
2383 return ErrorNone;
2384 }1875 }
23851876
2386 uint64_t padding_in_bits = biggest_size_in_bits - size_of_most_aligned_member_in_bits;1877 // unset temporary flag
1878 union_type->data.unionation.resolve_loop_flag = false;
1879 union_type->data.unionation.resolve_status = ResolveStatusSizeKnown;
1880 union_type->data.unionation.union_abi_size = union_abi_size;
23871881
2388 ZigType *tag_type = union_type->data.unionation.tag_type;1882 ZigType *tag_type = union_type->data.unionation.tag_type;
2389 if (tag_type == nullptr || tag_type->zero_bits) {1883 if (tag_type != nullptr && type_has_bits(tag_type)) {
2390 assert(most_aligned_union_member != nullptr);1884 if ((err = type_resolve(g, tag_type, ResolveStatusSizeKnown))) {
23911885 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
2392 if (padding_in_bits > 0) {1886 return ErrorSemanticAnalyzeFail;
2393 ZigType *u8_type = get_int_type(g, false, 8);1887 }
2394 ZigType *padding_array = get_array_type(g, u8_type, padding_in_bits / 8);1888 if (most_aligned_union_member == nullptr) {
2395 LLVMTypeRef union_element_types[] = {1889 union_type->abi_size = tag_type->abi_size;
2396 most_aligned_union_member->type_ref,1890 union_type->size_in_bits = tag_type->size_in_bits;
2397 padding_array->type_ref,
2398 };
2399 LLVMStructSetBody(union_type->type_ref, union_element_types, 2, false);
2400 } else {1891 } else {
2401 LLVMStructSetBody(union_type->type_ref, &most_aligned_union_member->type_ref, 1, false);1892 size_t field_sizes[2];
1893 size_t field_aligns[2];
1894 field_sizes[union_type->data.unionation.gen_tag_index] = tag_type->abi_size;
1895 field_aligns[union_type->data.unionation.gen_tag_index] = tag_type->abi_align;
1896 field_sizes[union_type->data.unionation.gen_union_index] = union_abi_size;
1897 field_aligns[union_type->data.unionation.gen_union_index] = most_aligned_union_member->abi_align;
1898 size_t field2_offset = next_field_offset(0, union_type->abi_align, field_sizes[0], field_aligns[1]);
1899 union_type->abi_size = next_field_offset(field2_offset, union_type->abi_align, field_sizes[1], union_type->abi_align);
1900 union_type->size_in_bits = union_type->abi_size * 8;
2402 }1901 }
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 {1902 } else {
2455 union_type->data.unionation.gen_union_index = 0;1903 union_type->abi_size = union_abi_size;
2456 union_type->data.unionation.gen_tag_index = 1;1904 union_type->size_in_bits = union_size_in_bits;
2457 }1905 }
24581906
2459 LLVMTypeRef root_struct_element_types[2];
2460 root_struct_element_types[union_type->data.unionation.gen_tag_index] = tag_type->type_ref;
2461 root_struct_element_types[union_type->data.unionation.gen_union_index] = union_type_ref;
2462 LLVMStructSetBody(union_type->type_ref, root_struct_element_types, 2, false);
2463
2464
2465 // create debug type for union
2466 ZigLLVMDIType *union_di_type = ZigLLVMCreateDebugUnionType(g->dbuilder,
2467 ZigLLVMTypeToScope(union_type->di_type), "AnonUnion",
2468 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
2469 biggest_size_in_bits, biggest_align_in_bits, 0, union_inner_di_types,
2470 gen_field_count, 0, "");
2471
2472 uint64_t union_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, union_type->type_ref,
2473 union_type->data.unionation.gen_union_index);
2474 uint64_t tag_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, union_type->type_ref,
2475 union_type->data.unionation.gen_tag_index);
2476
2477 ZigLLVMDIType *union_member_di_type = ZigLLVMCreateDebugMemberType(g->dbuilder,
2478 ZigLLVMTypeToScope(union_type->di_type), "payload",
2479 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
2480 biggest_size_in_bits,
2481 biggest_align_in_bits,
2482 union_offset_in_bits,
2483 0, union_di_type);
2484
2485 uint64_t tag_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, tag_type->type_ref);
2486 uint64_t tag_debug_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, tag_type->type_ref);
2487
2488 ZigLLVMDIType *tag_member_di_type = ZigLLVMCreateDebugMemberType(g->dbuilder,
2489 ZigLLVMTypeToScope(union_type->di_type), "tag",
2490 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
2491 tag_debug_size_in_bits,
2492 tag_debug_align_in_bits,
2493 tag_offset_in_bits,
2494 0, tag_type->di_type);
2495
2496 ZigLLVMDIType *di_root_members[2];
2497 di_root_members[union_type->data.unionation.gen_tag_index] = tag_member_di_type;
2498 di_root_members[union_type->data.unionation.gen_union_index] = union_member_di_type;
2499
2500 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, union_type->type_ref);
2501 uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, union_type->type_ref);
2502 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
2503 ZigLLVMFileToScope(import->data.structure.root_struct->di_file),
2504 buf_ptr(&union_type->name),
2505 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
2506 debug_size_in_bits,
2507 debug_align_in_bits,
2508 0, nullptr, di_root_members, 2, 0, nullptr, "");
2509
2510 ZigLLVMReplaceTemporary(g->dbuilder, union_type->di_type, replacement_di_type);
2511 union_type->di_type = replacement_di_type;
2512
2513 return ErrorNone;1907 return ErrorNone;
2514}1908}
25151909
2516static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {1910static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
2517 assert(enum_type->id == ZigTypeIdEnum);1911 assert(enum_type->id == ZigTypeIdEnum);
25181912
1913 if (enum_type->data.enumeration.is_invalid)
1914 return ErrorSemanticAnalyzeFail;
1915
2519 if (enum_type->data.enumeration.zero_bits_known)1916 if (enum_type->data.enumeration.zero_bits_known)
2520 return ErrorNone;1917 return ErrorNone;
25211918
...@@ -2531,7 +1928,6 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {...@@ -2531,7 +1928,6 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
25311928
2532 AstNode *decl_node = enum_type->data.enumeration.decl_node;1929 AstNode *decl_node = enum_type->data.enumeration.decl_node;
2533 assert(decl_node->type == NodeTypeContainerDecl);1930 assert(decl_node->type == NodeTypeContainerDecl);
2534 assert(enum_type->di_type);
25351931
2536 assert(!enum_type->data.enumeration.fields);1932 assert(!enum_type->data.enumeration.fields);
2537 uint32_t field_count = (uint32_t)decl_node->data.container_decl.fields.length;1933 uint32_t field_count = (uint32_t)decl_node->data.container_decl.fields.length;
...@@ -2564,6 +1960,10 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {...@@ -2564,6 +1960,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);1960 tag_int_type = get_smallest_unsigned_int_type(g, field_count - 1);
2565 }1961 }
25661962
1963 enum_type->size_in_bits = tag_int_type->size_in_bits;
1964 enum_type->abi_size = tag_int_type->abi_size;
1965 enum_type->abi_align = tag_int_type->abi_align;
1966
2567 // TODO: Are extern enums allowed to have an init_arg_expr?1967 // TODO: Are extern enums allowed to have an init_arg_expr?
2568 if (decl_node->data.container_decl.init_arg_expr != nullptr) {1968 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);1969 ZigType *wanted_tag_int_type = analyze_type_expr(g, scope, decl_node->data.container_decl.init_arg_expr);
...@@ -2587,7 +1987,9 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {...@@ -2587,7 +1987,9 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
2587 }1987 }
2588 }1988 }
2589 enum_type->data.enumeration.tag_int_type = tag_int_type;1989 enum_type->data.enumeration.tag_int_type = tag_int_type;
2590 enum_type->type_ref = tag_int_type->type_ref;1990 enum_type->size_in_bits = tag_int_type->size_in_bits;
1991 enum_type->abi_size = tag_int_type->abi_size;
1992 enum_type->abi_align = tag_int_type->abi_align;
25911993
2592 for (uint32_t field_i = 0; field_i < field_count; field_i += 1) {1994 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);1995 AstNode *field_node = decl_node->data.container_decl.fields.at(field_i);
...@@ -2671,8 +2073,8 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {...@@ -2671,8 +2073,8 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
2671 }2073 }
26722074
2673 enum_type->data.enumeration.zero_bits_loop_flag = false;2075 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;2076 enum_type->data.enumeration.zero_bits_known = true;
2077 enum_type->data.enumeration.complete = true;
26762078
2677 if (enum_type->data.enumeration.is_invalid)2079 if (enum_type->data.enumeration.is_invalid)
2678 return ErrorSemanticAnalyzeFail;2080 return ErrorSemanticAnalyzeFail;
...@@ -2683,12 +2085,20 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {...@@ -2683,12 +2085,20 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
2683static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {2085static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
2684 assert(struct_type->id == ZigTypeIdStruct);2086 assert(struct_type->id == ZigTypeIdStruct);
26852087
2088 Error err;
2089
2686 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid)2090 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid)
2687 return ErrorSemanticAnalyzeFail;2091 return ErrorSemanticAnalyzeFail;
2688 if (struct_type->data.structure.resolve_status >= ResolveStatusZeroBitsKnown)2092 if (struct_type->data.structure.resolve_status >= ResolveStatusZeroBitsKnown)
2689 return ErrorNone;2093 return ErrorNone;
26902094
2095 AstNode *decl_node = struct_type->data.structure.decl_node;
2096 assert(decl_node->type == NodeTypeContainerDecl);
2097
2691 if (struct_type->data.structure.resolve_loop_flag) {2098 if (struct_type->data.structure.resolve_loop_flag) {
2099 // TODO This is a problem. I believe it can be solved with lazy values.
2100 struct_type->size_in_bits = SIZE_MAX;
2101 struct_type->abi_size = SIZE_MAX;
2692 struct_type->data.structure.resolve_status = ResolveStatusZeroBitsKnown;2102 struct_type->data.structure.resolve_status = ResolveStatusZeroBitsKnown;
2693 struct_type->data.structure.resolve_loop_flag = false;2103 struct_type->data.structure.resolve_loop_flag = false;
2694 return ErrorNone;2104 return ErrorNone;
...@@ -2696,10 +2106,6 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {...@@ -2696,10 +2106,6 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
26962106
2697 struct_type->data.structure.resolve_loop_flag = true;2107 struct_type->data.structure.resolve_loop_flag = true;
26982108
2699 AstNode *decl_node = struct_type->data.structure.decl_node;
2700 assert(decl_node->type == NodeTypeContainerDecl);
2701 assert(struct_type->di_type);
2702
2703 assert(!struct_type->data.structure.fields);2109 assert(!struct_type->data.structure.fields);
2704 size_t field_count = decl_node->data.container_decl.fields.length;2110 size_t field_count = decl_node->data.container_decl.fields.length;
2705 struct_type->data.structure.src_field_count = (uint32_t)field_count;2111 struct_type->data.structure.src_field_count = (uint32_t)field_count;
...@@ -2718,7 +2124,7 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {...@@ -2718,7 +2124,7 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
2718 if (field_node->data.struct_field.type == nullptr) {2124 if (field_node->data.struct_field.type == nullptr) {
2719 add_node_error(g, field_node, buf_sprintf("struct field missing type"));2125 add_node_error(g, field_node, buf_sprintf("struct field missing type"));
2720 struct_type->data.structure.resolve_status = ResolveStatusInvalid;2126 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2721 continue;2127 return ErrorSemanticAnalyzeFail;
2722 }2128 }
27232129
2724 auto field_entry = struct_type->data.structure.fields_by_name.put_unique(type_struct_field->name, type_struct_field);2130 auto field_entry = struct_type->data.structure.fields_by_name.put_unique(type_struct_field->name, type_struct_field);
...@@ -2727,11 +2133,33 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {...@@ -2727,11 +2133,33 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
2727 buf_sprintf("duplicate struct field: '%s'", buf_ptr(type_struct_field->name)));2133 buf_sprintf("duplicate struct field: '%s'", buf_ptr(type_struct_field->name)));
2728 add_error_note(g, msg, field_entry->value->decl_node, buf_sprintf("other field here"));2134 add_error_note(g, msg, field_entry->value->decl_node, buf_sprintf("other field here"));
2729 struct_type->data.structure.resolve_status = ResolveStatusInvalid;2135 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2730 continue;2136 return ErrorSemanticAnalyzeFail;
2731 }2137 }
27322138
2733 ZigType *field_type = analyze_type_expr(g, scope, field_node->data.struct_field.type);2139 ZigType *field_type = analyze_type_expr(g, scope, field_node->data.struct_field.type);
2734 type_struct_field->type_entry = field_type;2140 type_struct_field->type_entry = field_type;
2141 if (type_is_invalid(field_type)) {
2142 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2143 return ErrorSemanticAnalyzeFail;
2144 }
2145 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid)
2146 return ErrorSemanticAnalyzeFail;
2147
2148 if (struct_type->data.structure.layout == ContainerLayoutExtern &&
2149 !type_allowed_in_extern(g, field_type))
2150 {
2151 add_node_error(g, field_node,
2152 buf_sprintf("extern structs cannot contain fields of type '%s'",
2153 buf_ptr(&field_type->name)));
2154 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2155 return ErrorSemanticAnalyzeFail;
2156 } else if (struct_type->data.structure.layout == ContainerLayoutPacked) {
2157 if ((err = emit_error_unless_type_allowed_in_packed_struct(g, field_type, field_node))) {
2158 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2159 return ErrorSemanticAnalyzeFail;
2160 }
2161 }
2162
2735 type_struct_field->src_index = i;2163 type_struct_field->src_index = i;
2736 type_struct_field->gen_index = SIZE_MAX;2164 type_struct_field->gen_index = SIZE_MAX;
27372165
...@@ -2739,21 +2167,19 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {...@@ -2739,21 +2167,19 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
2739 add_node_error(g, field_node->data.struct_field.value,2167 add_node_error(g, field_node->data.struct_field.value,
2740 buf_sprintf("enums, not structs, support field assignment"));2168 buf_sprintf("enums, not structs, support field assignment"));
2741 }2169 }
2742
2743 if (field_type->id == ZigTypeIdOpaque) {2170 if (field_type->id == ZigTypeIdOpaque) {
2744 add_node_error(g, field_node->data.struct_field.type,2171 add_node_error(g, field_node->data.struct_field.type,
2745 buf_sprintf("opaque types have unknown size and therefore cannot be directly embedded in structs"));2172 buf_sprintf("opaque types have unknown size and therefore cannot be directly embedded in structs"));
2746 struct_type->data.structure.resolve_status = ResolveStatusInvalid;2173 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2747 continue;2174 return ErrorSemanticAnalyzeFail;
2748 }2175 }
2749
2750 switch (type_requires_comptime(g, field_type)) {2176 switch (type_requires_comptime(g, field_type)) {
2751 case ReqCompTimeYes:2177 case ReqCompTimeYes:
2752 struct_type->data.structure.requires_comptime = true;2178 struct_type->data.structure.requires_comptime = true;
2753 break;2179 break;
2754 case ReqCompTimeInvalid:2180 case ReqCompTimeInvalid:
2755 struct_type->data.structure.resolve_status = ResolveStatusInvalid;2181 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2756 continue;2182 return ErrorSemanticAnalyzeFail;
2757 case ReqCompTimeNo:2183 case ReqCompTimeNo:
2758 break;2184 break;
2759 }2185 }
...@@ -2767,7 +2193,10 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {...@@ -2767,7 +2193,10 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
27672193
2768 struct_type->data.structure.resolve_loop_flag = false;2194 struct_type->data.structure.resolve_loop_flag = false;
2769 struct_type->data.structure.gen_field_count = (uint32_t)gen_field_index;2195 struct_type->data.structure.gen_field_count = (uint32_t)gen_field_index;
2770 struct_type->zero_bits = (gen_field_index == 0);2196 if (gen_field_index != 0) {
2197 struct_type->abi_size = SIZE_MAX;
2198 struct_type->size_in_bits = SIZE_MAX;
2199 }
27712200
2772 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid)2201 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid)
2773 return ErrorSemanticAnalyzeFail;2202 return ErrorSemanticAnalyzeFail;
...@@ -2785,9 +2214,10 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {...@@ -2785,9 +2214,10 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {
2785 return ErrorSemanticAnalyzeFail;2214 return ErrorSemanticAnalyzeFail;
2786 if (struct_type->data.structure.resolve_status >= ResolveStatusAlignmentKnown)2215 if (struct_type->data.structure.resolve_status >= ResolveStatusAlignmentKnown)
2787 return ErrorNone;2216 return ErrorNone;
2788
2789 if ((err = resolve_struct_zero_bits(g, struct_type)))2217 if ((err = resolve_struct_zero_bits(g, struct_type)))
2790 return err;2218 return err;
2219 if (struct_type->data.structure.resolve_status >= ResolveStatusAlignmentKnown)
2220 return ErrorNone;
27912221
2792 AstNode *decl_node = struct_type->data.structure.decl_node;2222 AstNode *decl_node = struct_type->data.structure.decl_node;
27932223
...@@ -2803,22 +2233,19 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {...@@ -2803,22 +2233,19 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {
28032233
2804 struct_type->data.structure.resolve_loop_flag = true;2234 struct_type->data.structure.resolve_loop_flag = true;
2805 assert(decl_node->type == NodeTypeContainerDecl);2235 assert(decl_node->type == NodeTypeContainerDecl);
2806 assert(struct_type->di_type);
28072236
2808 size_t field_count = struct_type->data.structure.src_field_count;2237 size_t field_count = struct_type->data.structure.src_field_count;
2809 if (struct_type->data.structure.layout == ContainerLayoutPacked) {2238 bool packed = struct_type->data.structure.layout == ContainerLayoutPacked;
2810 struct_type->data.structure.abi_alignment = 1;2239
2811 for (size_t i = 0; i < field_count; i += 1) {2240 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];2241 TypeStructField *field = &struct_type->data.structure.fields[i];
2820 uint32_t this_field_align;2242 if (field->gen_index == SIZE_MAX)
2243 continue;
28212244
2245 size_t this_field_align;
2246 if (packed) {
2247 // TODO: https://github.com/ziglang/zig/issues/1512
2248 this_field_align = 1;
2822 // TODO If we have no type_entry for the field, we've already failed to2249 // 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 deeper2250 // compile the program correctly. This stage1 compiler needs a deeper
2824 // reworking to make this correct, or we can ignore the problem2251 // reworking to make this correct, or we can ignore the problem
...@@ -2827,28 +2254,19 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {...@@ -2827,28 +2254,19 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {
2827 // on itself. When this false positive happens we assume a pointer-aligned2254 // on itself. When this false positive happens we assume a pointer-aligned
2828 // field, which is usually fine but could be incorrectly over-aligned or2255 // field, which is usually fine but could be incorrectly over-aligned or
2829 // even under-aligned. See https://github.com/ziglang/zig/issues/15122256 // even under-aligned. See https://github.com/ziglang/zig/issues/1512
2830 if (field->type_entry == nullptr) {2257 } else if (field->type_entry == nullptr) {
2831 this_field_align = LLVMABIAlignmentOfType(g->target_data_ref, LLVMPointerType(LLVMInt8Type(), 0));2258 this_field_align = g->builtin_types.entry_usize->abi_align;
2832 } else {2259 } else {
2833 if (type_is_invalid(field->type_entry)) {
2834 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2835 break;
2836 }
2837
2838 if (!type_has_bits(field->type_entry))
2839 continue;
2840
2841 if ((err = type_resolve(g, field->type_entry, ResolveStatusAlignmentKnown))) {2260 if ((err = type_resolve(g, field->type_entry, ResolveStatusAlignmentKnown))) {
2842 struct_type->data.structure.resolve_status = ResolveStatusInvalid;2261 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2843 break;2262 return ErrorSemanticAnalyzeFail;
2844 }2263 }
28452264 this_field_align = field->type_entry->abi_align;
2846 this_field_align = get_abi_alignment(g, field->type_entry);
2847 assert(this_field_align != 0);
2848 }2265 }
2849 // alignment of structs is the alignment of the most-aligned field2266
2850 if (this_field_align > struct_type->data.structure.abi_alignment) {2267 // TODO: https://github.com/ziglang/zig/issues/1512
2851 struct_type->data.structure.abi_alignment = this_field_align;2268 if (this_field_align > struct_type->abi_align) {
2269 struct_type->abi_align = this_field_align;
2852 }2270 }
2853 }2271 }
28542272
...@@ -2867,57 +2285,41 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -2867,57 +2285,41 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
28672285
2868 Error err;2286 Error err;
28692287
2870 if (union_type->data.unionation.is_invalid)2288 if (union_type->data.unionation.resolve_status == ResolveStatusInvalid)
2871 return ErrorSemanticAnalyzeFail;2289 return ErrorSemanticAnalyzeFail;
28722290
2873 if (union_type->data.unionation.zero_bits_known)2291 if (union_type->data.unionation.resolve_status >= ResolveStatusZeroBitsKnown)
2874 return ErrorNone;2292 return ErrorNone;
28752293
2876 if (type_is_invalid(union_type))2294 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 is2295 // 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 field2296 // not zero bits.
2882 // is a pointer to this very struct, or a function pointer with parameters that2297 // TODO actually it could still be zero bits. Here we should continue analyzing
2883 // reference such a type.2298 // the union from the next field index.
2884 union_type->data.unionation.zero_bits_known = true;2299 union_type->data.unionation.resolve_status = ResolveStatusZeroBitsKnown;
2885 union_type->data.unionation.zero_bits_loop_flag = false;2300 union_type->data.unionation.resolve_loop_flag = false;
2886 if (union_type->data.unionation.abi_alignment == 0) {2301 union_type->abi_size = SIZE_MAX;
2887 if (union_type->data.unionation.layout == ContainerLayoutPacked) {2302 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;2303 return ErrorNone;
2895 }2304 }
28962305
2897 union_type->data.unionation.zero_bits_loop_flag = true;2306 union_type->data.unionation.resolve_loop_flag = true;
28982307
2899 AstNode *decl_node = union_type->data.unionation.decl_node;2308 AstNode *decl_node = union_type->data.unionation.decl_node;
2900 assert(decl_node->type == NodeTypeContainerDecl);2309 assert(decl_node->type == NodeTypeContainerDecl);
2901 assert(union_type->di_type);
29022310
2903 assert(!union_type->data.unionation.fields);2311 assert(union_type->data.unionation.fields == nullptr);
2904 uint32_t field_count = (uint32_t)decl_node->data.container_decl.fields.length;2312 uint32_t field_count = (uint32_t)decl_node->data.container_decl.fields.length;
2905 if (field_count == 0) {2313 if (field_count == 0) {
2906 add_node_error(g, decl_node, buf_sprintf("unions must have 1 or more fields"));2314 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;2315 union_type->data.unionation.src_field_count = field_count;
2909 union_type->data.unionation.fields = nullptr;2316 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;2317 return ErrorSemanticAnalyzeFail;
2914 }2318 }
2915 union_type->data.unionation.src_field_count = field_count;2319 union_type->data.unionation.src_field_count = field_count;
2916 union_type->data.unionation.fields = allocate<TypeUnionField>(field_count);2320 union_type->data.unionation.fields = allocate<TypeUnionField>(field_count);
2917 union_type->data.unionation.fields_by_name.init(field_count);2321 union_type->data.unionation.fields_by_name.init(field_count);
29182322
2919 uint32_t biggest_align_bytes = 0;
2920
2921 Scope *scope = &union_type->data.unionation.decls_scope->base;2323 Scope *scope = &union_type->data.unionation.decls_scope->base;
29222324
2923 HashMap<BigInt, AstNode *, bigint_hash, bigint_eql> occupied_tag_values = {};2325 HashMap<BigInt, AstNode *, bigint_hash, bigint_eql> occupied_tag_values = {};
...@@ -2931,7 +2333,6 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -2931,7 +2333,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);2333 bool create_enum_type = decl_node->data.container_decl.auto_enum || (enum_type_node == nullptr && want_safety);
2932 bool *covered_enum_fields;2334 bool *covered_enum_fields;
2933 ZigLLVMDIEnumerator **di_enumerators;2335 ZigLLVMDIEnumerator **di_enumerators;
2934 uint32_t abi_alignment_so_far;
2935 if (create_enum_type) {2336 if (create_enum_type) {
2936 occupied_tag_values.init(field_count);2337 occupied_tag_values.init(field_count);
29372338
...@@ -2941,13 +2342,13 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -2941,13 +2342,13 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
2941 if (enum_type_node != nullptr) {2342 if (enum_type_node != nullptr) {
2942 tag_int_type = analyze_type_expr(g, scope, enum_type_node);2343 tag_int_type = analyze_type_expr(g, scope, enum_type_node);
2943 if (type_is_invalid(tag_int_type)) {2344 if (type_is_invalid(tag_int_type)) {
2944 union_type->data.unionation.is_invalid = true;2345 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
2945 return ErrorSemanticAnalyzeFail;2346 return ErrorSemanticAnalyzeFail;
2946 }2347 }
2947 if (tag_int_type->id != ZigTypeIdInt) {2348 if (tag_int_type->id != ZigTypeIdInt) {
2948 add_node_error(g, enum_type_node,2349 add_node_error(g, enum_type_node,
2949 buf_sprintf("expected integer tag type, found '%s'", buf_ptr(&tag_int_type->name)));2350 buf_sprintf("expected integer tag type, found '%s'", buf_ptr(&tag_int_type->name)));
2950 union_type->data.unionation.is_invalid = true;2351 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
2951 return ErrorSemanticAnalyzeFail;2352 return ErrorSemanticAnalyzeFail;
2952 }2353 }
2953 } else if (auto_layout && field_count == 1) {2354 } else if (auto_layout && field_count == 1) {
...@@ -2955,13 +2356,15 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -2955,13 +2356,15 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
2955 } else {2356 } else {
2956 tag_int_type = get_smallest_unsigned_int_type(g, field_count - 1);2357 tag_int_type = get_smallest_unsigned_int_type(g, field_count - 1);
2957 }2358 }
2958 abi_alignment_so_far = get_abi_alignment(g, tag_int_type);
29592359
2960 tag_type = new_type_table_entry(ZigTypeIdEnum);2360 tag_type = new_type_table_entry(ZigTypeIdEnum);
2961 buf_resize(&tag_type->name, 0);2361 buf_resize(&tag_type->name, 0);
2962 buf_appendf(&tag_type->name, "@TagType(%s)", buf_ptr(&union_type->name));2362 buf_appendf(&tag_type->name, "@TagType(%s)", buf_ptr(&union_type->name));
2963 tag_type->type_ref = tag_int_type->type_ref;2363 tag_type->llvm_type = tag_int_type->llvm_type;
2964 tag_type->zero_bits = tag_int_type->zero_bits;2364 tag_type->llvm_di_type = tag_int_type->llvm_di_type;
2365 tag_type->abi_size = tag_int_type->abi_size;
2366 tag_type->abi_align = tag_int_type->abi_align;
2367 tag_type->size_in_bits = tag_int_type->size_in_bits;
29652368
2966 tag_type->data.enumeration.tag_int_type = tag_int_type;2369 tag_type->data.enumeration.tag_int_type = tag_int_type;
2967 tag_type->data.enumeration.zero_bits_known = true;2370 tag_type->data.enumeration.zero_bits_known = true;
...@@ -2975,11 +2378,11 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -2975,11 +2378,11 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
2975 } else if (enum_type_node != nullptr) {2378 } else if (enum_type_node != nullptr) {
2976 ZigType *enum_type = analyze_type_expr(g, scope, enum_type_node);2379 ZigType *enum_type = analyze_type_expr(g, scope, enum_type_node);
2977 if (type_is_invalid(enum_type)) {2380 if (type_is_invalid(enum_type)) {
2978 union_type->data.unionation.is_invalid = true;2381 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
2979 return ErrorSemanticAnalyzeFail;2382 return ErrorSemanticAnalyzeFail;
2980 }2383 }
2981 if (enum_type->id != ZigTypeIdEnum) {2384 if (enum_type->id != ZigTypeIdEnum) {
2982 union_type->data.unionation.is_invalid = true;2385 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
2983 add_node_error(g, enum_type_node,2386 add_node_error(g, enum_type_node,
2984 buf_sprintf("expected enum tag type, found '%s'", buf_ptr(&enum_type->name)));2387 buf_sprintf("expected enum tag type, found '%s'", buf_ptr(&enum_type->name)));
2985 return ErrorSemanticAnalyzeFail;2388 return ErrorSemanticAnalyzeFail;
...@@ -2989,11 +2392,9 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -2989,11 +2392,9 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
2989 return err;2392 return err;
2990 }2393 }
2991 tag_type = enum_type;2394 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);2395 covered_enum_fields = allocate<bool>(enum_type->data.enumeration.src_field_count);
2994 } else {2396 } else {
2995 tag_type = nullptr;2397 tag_type = nullptr;
2996 abi_alignment_so_far = 0;
2997 }2398 }
2998 union_type->data.unionation.tag_type = tag_type;2399 union_type->data.unionation.tag_type = tag_type;
29992400
...@@ -3004,14 +2405,15 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -3004,14 +2405,15 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
3004 TypeUnionField *union_field = &union_type->data.unionation.fields[i];2405 TypeUnionField *union_field = &union_type->data.unionation.fields[i];
3005 union_field->name = field_node->data.struct_field.name;2406 union_field->name = field_node->data.struct_field.name;
3006 union_field->decl_node = field_node;2407 union_field->decl_node = field_node;
2408 union_field->gen_index = UINT32_MAX;
30072409
3008 auto field_entry = union_type->data.unionation.fields_by_name.put_unique(union_field->name, union_field);2410 auto field_entry = union_type->data.unionation.fields_by_name.put_unique(union_field->name, union_field);
3009 if (field_entry != nullptr) {2411 if (field_entry != nullptr) {
3010 ErrorMsg *msg = add_node_error(g, field_node,2412 ErrorMsg *msg = add_node_error(g, field_node,
3011 buf_sprintf("duplicate union field: '%s'", buf_ptr(union_field->name)));2413 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"));2414 add_error_note(g, msg, field_entry->value->decl_node, buf_sprintf("other field here"));
3013 union_type->data.unionation.is_invalid = true;2415 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
3014 continue;2416 return ErrorSemanticAnalyzeFail;
3015 }2417 }
30162418
3017 ZigType *field_type;2419 ZigType *field_type;
...@@ -3020,29 +2422,31 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -3020,29 +2422,31 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
3020 field_type = g->builtin_types.entry_void;2422 field_type = g->builtin_types.entry_void;
3021 } else {2423 } else {
3022 add_node_error(g, field_node, buf_sprintf("union field missing type"));2424 add_node_error(g, field_node, buf_sprintf("union field missing type"));
3023 union_type->data.unionation.is_invalid = true;2425 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
3024 continue;2426 return ErrorSemanticAnalyzeFail;
3025 }2427 }
3026 } else {2428 } else {
3027 field_type = analyze_type_expr(g, scope, field_node->data.struct_field.type);2429 field_type = analyze_type_expr(g, scope, field_node->data.struct_field.type);
3028 if ((err = type_resolve(g, field_type, ResolveStatusAlignmentKnown))) {2430 if ((err = type_resolve(g, field_type, ResolveStatusAlignmentKnown))) {
3029 union_type->data.unionation.is_invalid = true;2431 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
3030 continue;2432 return ErrorSemanticAnalyzeFail;
3031 }2433 }
2434 if (union_type->data.unionation.resolve_status == ResolveStatusInvalid)
2435 return ErrorSemanticAnalyzeFail;
3032 }2436 }
3033 union_field->type_entry = field_type;2437 union_field->type_entry = field_type;
30342438
3035 if (field_type->id == ZigTypeIdOpaque) {2439 if (field_type->id == ZigTypeIdOpaque) {
3036 add_node_error(g, field_node->data.struct_field.type,2440 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"));2441 buf_sprintf("opaque types have unknown size and therefore cannot be directly embedded in unions"));
3038 union_type->data.unionation.is_invalid = true;2442 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
3039 continue;2443 return ErrorSemanticAnalyzeFail;
3040 }2444 }
30412445
3042 switch (type_requires_comptime(g, field_type)) {2446 switch (type_requires_comptime(g, field_type)) {
3043 case ReqCompTimeInvalid:2447 case ReqCompTimeInvalid:
3044 union_type->data.unionation.is_invalid = true;2448 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
3045 continue;2449 return ErrorSemanticAnalyzeFail;
3046 case ReqCompTimeYes:2450 case ReqCompTimeYes:
3047 union_type->data.unionation.requires_comptime = true;2451 union_type->data.unionation.requires_comptime = true;
3048 break;2452 break;
...@@ -3074,8 +2478,8 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -3074,8 +2478,8 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
3074 ZigType *tag_int_type = tag_type->data.enumeration.tag_int_type;2478 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);2479 ConstExprValue *result = analyze_const_value(g, scope, tag_value, tag_int_type, nullptr);
3076 if (type_is_invalid(result->type)) {2480 if (type_is_invalid(result->type)) {
3077 union_type->data.unionation.is_invalid = true;2481 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
3078 continue;2482 return ErrorSemanticAnalyzeFail;
3079 }2483 }
3080 assert(result->special != ConstValSpecialRuntime);2484 assert(result->special != ConstValSpecialRuntime);
3081 assert(result->type->id == ZigTypeIdInt);2485 assert(result->type->id == ZigTypeIdInt);
...@@ -3090,8 +2494,8 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -3090,8 +2494,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)));2494 buf_sprintf("enum tag value %s already taken", buf_ptr(val_buf)));
3091 add_error_note(g, msg, entry->value,2495 add_error_note(g, msg, entry->value,
3092 buf_sprintf("other occurrence here"));2496 buf_sprintf("other occurrence here"));
3093 union_type->data.unionation.is_invalid = true;2497 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
3094 continue;2498 return ErrorSemanticAnalyzeFail;
3095 }2499 }
3096 }2500 }
3097 } else if (enum_type_node != nullptr) {2501 } else if (enum_type_node != nullptr) {
...@@ -3101,8 +2505,8 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -3101,8 +2505,8 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
3101 buf_sprintf("enum field not found: '%s'", buf_ptr(field_name)));2505 buf_sprintf("enum field not found: '%s'", buf_ptr(field_name)));
3102 add_error_note(g, msg, tag_type->data.enumeration.decl_node,2506 add_error_note(g, msg, tag_type->data.enumeration.decl_node,
3103 buf_sprintf("enum declared here"));2507 buf_sprintf("enum declared here"));
3104 union_type->data.unionation.is_invalid = true;2508 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
3105 continue;2509 return ErrorSemanticAnalyzeFail;
3106 }2510 }
3107 covered_enum_fields[union_field->enum_field->decl_index] = true;2511 covered_enum_fields[union_field->enum_field->decl_index] = true;
3108 } else {2512 } else {
...@@ -3118,21 +2522,8 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -3118,21 +2522,8 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
31182522
3119 union_field->gen_index = gen_field_index;2523 union_field->gen_index = gen_field_index;
3120 gen_field_index += 1;2524 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 }2525 }
31302526
3131 union_type->data.unionation.abi_alignment = abi_alignment_so_far;
3132
3133 if (union_type->data.unionation.is_invalid)
3134 return ErrorSemanticAnalyzeFail;
3135
3136 bool src_have_tag = decl_node->data.container_decl.auto_enum ||2527 bool src_have_tag = decl_node->data.container_decl.auto_enum ||
3137 decl_node->data.container_decl.init_arg_expr != nullptr;2528 decl_node->data.container_decl.init_arg_expr != nullptr;
31382529
...@@ -3152,7 +2543,7 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -3152,7 +2543,7 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
3152 decl_node->data.container_decl.init_arg_expr : decl_node;2543 decl_node->data.container_decl.init_arg_expr : decl_node;
3153 add_node_error(g, source_node,2544 add_node_error(g, source_node,
3154 buf_sprintf("%s union does not support enum tag type", qual_str));2545 buf_sprintf("%s union does not support enum tag type", qual_str));
3155 union_type->data.unionation.is_invalid = true;2546 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
3156 return ErrorSemanticAnalyzeFail;2547 return ErrorSemanticAnalyzeFail;
3157 }2548 }
31582549
...@@ -3194,33 +2585,24 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -3194,33 +2585,24 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
3194 buf_sprintf("enum field missing: '%s'", buf_ptr(enum_field->name)));2585 buf_sprintf("enum field missing: '%s'", buf_ptr(enum_field->name)));
3195 add_error_note(g, msg, field_node,2586 add_error_note(g, msg, field_node,
3196 buf_sprintf("declared here"));2587 buf_sprintf("declared here"));
3197 union_type->data.unionation.is_invalid = true;2588 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
3198 }2589 }
3199 }2590 }
3200 }2591 }
32012592
3202 if (create_enum_type) {2593 if (union_type->data.unionation.resolve_status == ResolveStatusInvalid) {
3203 ZigType *import = get_scope_import(scope);2594 return ErrorSemanticAnalyzeFail;
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 }2595 }
32162596
3217 union_type->data.unionation.zero_bits_loop_flag = false;2597 union_type->data.unionation.resolve_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;
32212598
3222 if (union_type->data.unionation.is_invalid)2599 union_type->data.unionation.gen_field_count = gen_field_index;
3223 return ErrorSemanticAnalyzeFail;2600 bool zero_bits = gen_field_index == 0 && (field_count < 2 || !src_have_tag);
2601 if (!zero_bits) {
2602 union_type->abi_size = SIZE_MAX;
2603 union_type->size_in_bits = SIZE_MAX;
2604 }
2605 union_type->data.unionation.resolve_status = zero_bits ? ResolveStatusSizeKnown : ResolveStatusZeroBitsKnown;
32242606
3225 return ErrorNone;2607 return ErrorNone;
3226}2608}
...@@ -3630,20 +3012,17 @@ void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {...@@ -3630,20 +3012,17 @@ void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {
3630 }3012 }
3631}3013}
36323014
3633static void resolve_decl_container(CodeGen *g, TldContainer *tld_container) {3015static Error resolve_decl_container(CodeGen *g, TldContainer *tld_container) {
3634 ZigType *type_entry = tld_container->type_entry;3016 ZigType *type_entry = tld_container->type_entry;
3635 assert(type_entry);3017 assert(type_entry);
36363018
3637 switch (type_entry->id) {3019 switch (type_entry->id) {
3638 case ZigTypeIdStruct:3020 case ZigTypeIdStruct:
3639 resolve_struct_type(g, tld_container->type_entry);3021 return resolve_struct_type(g, tld_container->type_entry);
3640 return;
3641 case ZigTypeIdEnum:3022 case ZigTypeIdEnum:
3642 resolve_enum_type(g, tld_container->type_entry);3023 return resolve_enum_zero_bits(g, tld_container->type_entry);
3643 return;
3644 case ZigTypeIdUnion:3024 case ZigTypeIdUnion:
3645 resolve_union_type(g, tld_container->type_entry);3025 return resolve_union_type(g, tld_container->type_entry);
3646 return;
3647 default:3026 default:
3648 zig_unreachable();3027 zig_unreachable();
3649 }3028 }
...@@ -4007,7 +3386,7 @@ TypeStructField *find_struct_type_field(ZigType *type_entry, Buf *name) {...@@ -4007,7 +3386,7 @@ TypeStructField *find_struct_type_field(ZigType *type_entry, Buf *name) {
40073386
4008TypeUnionField *find_union_type_field(ZigType *type_entry, Buf *name) {3387TypeUnionField *find_union_type_field(ZigType *type_entry, Buf *name) {
4009 assert(type_entry->id == ZigTypeIdUnion);3388 assert(type_entry->id == ZigTypeIdUnion);
4010 assert(type_entry->data.unionation.zero_bits_known);3389 assert(type_is_resolved(type_entry, ResolveStatusZeroBitsKnown));
4011 if (type_entry->data.unionation.src_field_count == 0)3390 if (type_entry->data.unionation.src_field_count == 0)
4012 return nullptr;3391 return nullptr;
4013 auto entry = type_entry->data.unionation.fields_by_name.maybe_get(name);3392 auto entry = type_entry->data.unionation.fields_by_name.maybe_get(name);
...@@ -4018,7 +3397,7 @@ TypeUnionField *find_union_type_field(ZigType *type_entry, Buf *name) {...@@ -4018,7 +3397,7 @@ TypeUnionField *find_union_type_field(ZigType *type_entry, Buf *name) {
40183397
4019TypeUnionField *find_union_field_by_tag(ZigType *type_entry, const BigInt *tag) {3398TypeUnionField *find_union_field_by_tag(ZigType *type_entry, const BigInt *tag) {
4020 assert(type_entry->id == ZigTypeIdUnion);3399 assert(type_entry->id == ZigTypeIdUnion);
4021 assert(type_entry->data.unionation.zero_bits_known);3400 assert(type_is_resolved(type_entry, ResolveStatusZeroBitsKnown));
4022 for (uint32_t i = 0; i < type_entry->data.unionation.src_field_count; i += 1) {3401 for (uint32_t i = 0; i < type_entry->data.unionation.src_field_count; i += 1) {
4023 TypeUnionField *field = &type_entry->data.unionation.fields[i];3402 TypeUnionField *field = &type_entry->data.unionation.fields[i];
4024 if (bigint_cmp(&field->enum_field->value, tag) == CmpEQ) {3403 if (bigint_cmp(&field->enum_field->value, tag) == CmpEQ) {
...@@ -4096,17 +3475,14 @@ ZigType *container_ref_type(ZigType *type_entry) {...@@ -4096,17 +3475,14 @@ ZigType *container_ref_type(ZigType *type_entry) {
4096 type_entry->data.pointer.child_type : type_entry;3475 type_entry->data.pointer.child_type : type_entry;
4097}3476}
40983477
4099void resolve_container_type(CodeGen *g, ZigType *type_entry) {3478Error resolve_container_type(CodeGen *g, ZigType *type_entry) {
4100 switch (type_entry->id) {3479 switch (type_entry->id) {
4101 case ZigTypeIdStruct:3480 case ZigTypeIdStruct:
4102 resolve_struct_type(g, type_entry);3481 return resolve_struct_type(g, type_entry);
4103 break;
4104 case ZigTypeIdEnum:3482 case ZigTypeIdEnum:
4105 resolve_enum_type(g, type_entry);3483 return resolve_enum_zero_bits(g, type_entry);
4106 break;
4107 case ZigTypeIdUnion:3484 case ZigTypeIdUnion:
4108 resolve_union_type(g, type_entry);3485 return resolve_union_type(g, type_entry);
4109 break;
4110 case ZigTypeIdPointer:3486 case ZigTypeIdPointer:
4111 case ZigTypeIdMetaType:3487 case ZigTypeIdMetaType:
4112 case ZigTypeIdVoid:3488 case ZigTypeIdVoid:
...@@ -4132,6 +3508,7 @@ void resolve_container_type(CodeGen *g, ZigType *type_entry) {...@@ -4132,6 +3508,7 @@ void resolve_container_type(CodeGen *g, ZigType *type_entry) {
4132 case ZigTypeIdVector:3508 case ZigTypeIdVector:
4133 zig_unreachable();3509 zig_unreachable();
4134 }3510 }
3511 zig_unreachable();
4135}3512}
41363513
4137ZigType *get_src_ptr_type(ZigType *type) {3514ZigType *get_src_ptr_type(ZigType *type) {
...@@ -4233,10 +3610,6 @@ static void define_local_param_variables(CodeGen *g, ZigFn *fn_table_entry) {...@@ -4233,10 +3610,6 @@ static void define_local_param_variables(CodeGen *g, ZigFn *fn_table_entry) {
4233 if (type_has_bits(param_type)) {3610 if (type_has_bits(param_type)) {
4234 fn_table_entry->variable_list.append(var);3611 fn_table_entry->variable_list.append(var);
4235 }3612 }
4236
4237 if (fn_type->data.fn.gen_param_info) {
4238 var->gen_arg_index = fn_type->data.fn.gen_param_info[i].gen_index;
4239 }
4240 }3613 }
4241}3614}
42423615
...@@ -4626,18 +3999,26 @@ ZigType *get_vector_type(CodeGen *g, uint32_t len, ZigType *elem_type) {...@@ -4626,18 +3999,26 @@ ZigType *get_vector_type(CodeGen *g, uint32_t len, ZigType *elem_type) {
4626 }3999 }
46274000
4628 ZigType *entry = new_type_table_entry(ZigTypeIdVector);4001 ZigType *entry = new_type_table_entry(ZigTypeIdVector);
4629 entry->zero_bits = (len == 0) || !type_has_bits(elem_type);4002 if ((len != 0) && type_has_bits(elem_type)) {
4630 entry->type_ref = entry->zero_bits ? LLVMVoidType() : LLVMVectorType(elem_type->type_ref, len);4003 // Vectors can only be ints, floats, or pointers. ints and floats have trivially resolvable
4004 // llvm type refs. pointers we will use usize instead.
4005 LLVMTypeRef example_vector_llvm_type;
4006 if (elem_type->id == ZigTypeIdPointer) {
4007 example_vector_llvm_type = LLVMVectorType(g->builtin_types.entry_usize->llvm_type, len);
4008 } else {
4009 example_vector_llvm_type = LLVMVectorType(elem_type->llvm_type, len);
4010 }
4011 assert(example_vector_llvm_type != nullptr);
4012 entry->size_in_bits = elem_type->size_in_bits * len;
4013 entry->abi_size = LLVMABISizeOfType(g->target_data_ref, example_vector_llvm_type);
4014 entry->abi_align = LLVMABIAlignmentOfType(g->target_data_ref, example_vector_llvm_type);
4015 }
4631 entry->data.vector.len = len;4016 entry->data.vector.len = len;
4632 entry->data.vector.elem_type = elem_type;4017 entry->data.vector.elem_type = elem_type;
46334018
4634 buf_resize(&entry->name, 0);4019 buf_resize(&entry->name, 0);
4635 buf_appendf(&entry->name, "@Vector(%u, %s)", len, buf_ptr(&elem_type->name));4020 buf_appendf(&entry->name, "@Vector(%u, %s)", len, buf_ptr(&elem_type->name));
46364021
4637 entry->di_type = ZigLLVMDIBuilderCreateVectorType(g->dbuilder,
4638 len * type_size_bits(g, elem_type),
4639 LLVMABIAlignmentOfType(g->target_data_ref, entry->type_ref), elem_type->di_type, len);
4640
4641 g->type_table.put(type_id, entry);4022 g->type_table.put(type_id, entry);
4642 return entry;4023 return entry;
4643}4024}
...@@ -4685,12 +4066,7 @@ bool handle_is_ptr(ZigType *type_entry) {...@@ -4685,12 +4066,7 @@ bool handle_is_ptr(ZigType *type_entry) {
4685 !type_is_nonnull_ptr(type_entry->data.maybe.child_type) &&4066 !type_is_nonnull_ptr(type_entry->data.maybe.child_type) &&
4686 type_entry->data.maybe.child_type->id != ZigTypeIdErrorSet;4067 type_entry->data.maybe.child_type->id != ZigTypeIdErrorSet;
4687 case ZigTypeIdUnion:4068 case ZigTypeIdUnion:
4688 assert(type_entry->data.unionation.zero_bits_known);4069 return type_has_bits(type_entry) && type_entry->data.unionation.gen_field_count != 0;
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;
46944070
4695 }4071 }
4696 zig_unreachable();4072 zig_unreachable();
...@@ -5165,10 +4541,10 @@ bool fn_eval_eql(Scope *a, Scope *b) {...@@ -5165,10 +4541,10 @@ bool fn_eval_eql(Scope *a, Scope *b) {
51654541
5166// Whether the type has bits at runtime.4542// Whether the type has bits at runtime.
5167bool type_has_bits(ZigType *type_entry) {4543bool type_has_bits(ZigType *type_entry) {
5168 assert(type_entry);4544 assert(type_entry != nullptr);
5169 assert(!type_is_invalid(type_entry));4545 assert(!type_is_invalid(type_entry));
5170 assert(type_is_resolved(type_entry, ResolveStatusZeroBitsKnown));4546 assert(type_is_resolved(type_entry, ResolveStatusZeroBitsKnown));
5171 return !type_entry->zero_bits;4547 return type_entry->abi_size != 0;
5172}4548}
51734549
5174// Whether you can infer the value based solely on the type.4550// Whether you can infer the value based solely on the type.
...@@ -5629,18 +5005,22 @@ Error type_resolve(CodeGen *g, ZigType *ty, ResolveStatus status) {...@@ -5629,18 +5005,22 @@ Error type_resolve(CodeGen *g, ZigType *ty, ResolveStatus status) {
5629 } else if (ty->id == ZigTypeIdEnum) {5005 } else if (ty->id == ZigTypeIdEnum) {
5630 return resolve_enum_zero_bits(g, ty);5006 return resolve_enum_zero_bits(g, ty);
5631 } else if (ty->id == ZigTypeIdUnion) {5007 } else if (ty->id == ZigTypeIdUnion) {
5632 return resolve_union_zero_bits(g, ty);5008 return resolve_union_alignment(g, ty);
5633 }5009 }
5634 return ErrorNone;5010 return ErrorNone;
5635 case ResolveStatusSizeKnown:5011 case ResolveStatusSizeKnown:
5636 if (ty->id == ZigTypeIdStruct) {5012 if (ty->id == ZigTypeIdStruct) {
5637 return resolve_struct_type(g, ty);5013 return resolve_struct_type(g, ty);
5638 } else if (ty->id == ZigTypeIdEnum) {5014 } else if (ty->id == ZigTypeIdEnum) {
5639 return resolve_enum_type(g, ty);5015 return resolve_enum_zero_bits(g, ty);
5640 } else if (ty->id == ZigTypeIdUnion) {5016 } else if (ty->id == ZigTypeIdUnion) {
5641 return resolve_union_type(g, ty);5017 return resolve_union_type(g, ty);
5642 }5018 }
5643 return ErrorNone;5019 return ErrorNone;
5020 case ResolveStatusLLVMFwdDecl:
5021 case ResolveStatusLLVMFull:
5022 resolve_llvm_types(g, ty, status);
5023 return ErrorNone;
5644 }5024 }
5645 zig_unreachable();5025 zig_unreachable();
5646}5026}
...@@ -6192,31 +5572,18 @@ void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val) {...@@ -6192,31 +5572,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) {5572ZigType *make_int_type(CodeGen *g, bool is_signed, uint32_t size_in_bits) {
6193 assert(size_in_bits <= 65535);5573 assert(size_in_bits <= 65535);
6194 ZigType *entry = new_type_table_entry(ZigTypeIdInt);5574 ZigType *entry = new_type_table_entry(ZigTypeIdInt);
6195 entry->type_ref = (size_in_bits == 0) ? LLVMVoidType() : LLVMIntType(size_in_bits);5575
6196 entry->zero_bits = (size_in_bits == 0);5576 entry->size_in_bits = size_in_bits;
5577 if (size_in_bits != 0) {
5578 entry->llvm_type = LLVMIntType(size_in_bits);
5579 entry->abi_size = LLVMABISizeOfType(g->target_data_ref, entry->llvm_type);
5580 entry->abi_align = LLVMABIAlignmentOfType(g->target_data_ref, entry->llvm_type);
5581 }
61975582
6198 const char u_or_i = is_signed ? 'i' : 'u';5583 const char u_or_i = is_signed ? 'i' : 'u';
6199 buf_resize(&entry->name, 0);5584 buf_resize(&entry->name, 0);
6200 buf_appendf(&entry->name, "%c%" PRIu32, u_or_i, size_in_bits);5585 buf_appendf(&entry->name, "%c%" PRIu32, u_or_i, size_in_bits);
62015586
6202 unsigned dwarf_tag;
6203 if (is_signed) {
6204 if (size_in_bits == 8) {
6205 dwarf_tag = ZigLLVMEncoding_DW_ATE_signed_char();
6206 } else {
6207 dwarf_tag = ZigLLVMEncoding_DW_ATE_signed();
6208 }
6209 } else {
6210 if (size_in_bits == 8) {
6211 dwarf_tag = ZigLLVMEncoding_DW_ATE_unsigned_char();
6212 } else {
6213 dwarf_tag = ZigLLVMEncoding_DW_ATE_unsigned();
6214 }
6215 }
6216
6217 uint64_t debug_size_in_bits = (size_in_bits == 0) ?
6218 0 : (8*LLVMStoreSizeOfType(g->target_data_ref, entry->type_ref));
6219 entry->di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name), debug_size_in_bits, dwarf_tag);
6220 entry->data.integral.is_signed = is_signed;5587 entry->data.integral.is_signed = is_signed;
6221 entry->data.integral.bit_count = size_in_bits;5588 entry->data.integral.bit_count = size_in_bits;
6222 return entry;5589 return entry;
...@@ -6620,33 +5987,13 @@ LinkLib *add_link_lib(CodeGen *g, Buf *name) {...@@ -6620,33 +5987,13 @@ LinkLib *add_link_lib(CodeGen *g, Buf *name) {
6620 return link_lib;5987 return link_lib;
6621}5988}
66225989
6623uint32_t get_abi_alignment(CodeGen *g, ZigType *type_entry) {5990ZigType *get_align_amt_type(CodeGen *g) {
6624 assert(type_is_resolved(type_entry, ResolveStatusAlignmentKnown));5991 if (g->align_amt_type == nullptr) {
6625 if (type_entry->zero_bits) return 0;5992 // according to LLVM the maximum alignment is 1 << 29.
66265993 g->align_amt_type = get_int_type(g, false, 29);
6627 // We need to make this function work without requiring ensure_complete_type5994 }
6628 // so that we can have structs with fields that are pointers to their own type.5995 return g->align_amt_type;
6629 if (type_entry->id == ZigTypeIdStruct) {5996}
6630 assert(type_entry->data.structure.abi_alignment != 0);
6631 return type_entry->data.structure.abi_alignment;
6632 } else if (type_entry->id == ZigTypeIdUnion) {
6633 assert(type_entry->data.unionation.abi_alignment != 0);
6634 return type_entry->data.unionation.abi_alignment;
6635 } else if (type_entry->id == ZigTypeIdOpaque) {
6636 return 1;
6637 } else {
6638 uint32_t llvm_alignment = LLVMABIAlignmentOfType(g->target_data_ref, type_entry->type_ref);
6639 return llvm_alignment;
6640 }
6641}
6642
6643ZigType *get_align_amt_type(CodeGen *g) {
6644 if (g->align_amt_type == nullptr) {
6645 // according to LLVM the maximum alignment is 1 << 29.
6646 g->align_amt_type = get_int_type(g, false, 29);
6647 }
6648 return g->align_amt_type;
6649}
66505997
6651uint32_t type_ptr_hash(const ZigType *ptr) {5998uint32_t type_ptr_hash(const ZigType *ptr) {
6652 return hash_ptr((void*)ptr);5999 return hash_ptr((void*)ptr);
...@@ -6841,11 +6188,10 @@ bool type_is_c_abi_int(CodeGen *g, ZigType *ty) {...@@ -6841,11 +6188,10 @@ bool type_is_c_abi_int(CodeGen *g, ZigType *ty) {
68416188
6842uint32_t get_host_int_bytes(CodeGen *g, ZigType *struct_type, TypeStructField *field) {6189uint32_t get_host_int_bytes(CodeGen *g, ZigType *struct_type, TypeStructField *field) {
6843 assert(struct_type->id == ZigTypeIdStruct);6190 assert(struct_type->id == ZigTypeIdStruct);
6844 if (struct_type->data.structure.layout != ContainerLayoutPacked) {6191 assert(type_is_resolved(struct_type, ResolveStatusSizeKnown));
6192 if (struct_type->data.structure.host_int_bytes == nullptr)
6845 return 0;6193 return 0;
6846 }6194 return struct_type->data.structure.host_int_bytes[field->gen_index];
6847 LLVMTypeRef field_type = LLVMStructGetTypeAtIndex(struct_type->type_ref, field->gen_index);
6848 return LLVMStoreSizeOfType(g->target_data_ref, field_type);
6849}6195}
68506196
6851Error ensure_const_val_repr(IrAnalyze *ira, CodeGen *codegen, AstNode *source_node,6197Error ensure_const_val_repr(IrAnalyze *ira, CodeGen *codegen, AstNode *source_node,
...@@ -6911,3 +6257,968 @@ Buf *type_bare_name(ZigType *type_entry) {...@@ -6911,3 +6257,968 @@ Buf *type_bare_name(ZigType *type_entry) {
6911Buf *type_h_name(ZigType *t) {6257Buf *type_h_name(ZigType *t) {
6912 return type_bare_name(t);6258 return type_bare_name(t);
6913}6259}
6260
6261static void resolve_llvm_types_slice(CodeGen *g, ZigType *type, ResolveStatus wanted_resolve_status) {
6262 if (type->data.structure.resolve_status >= wanted_resolve_status) return;
6263
6264 ZigType *ptr_type = type->data.structure.fields[slice_ptr_index].type_entry;
6265 ZigType *child_type = ptr_type->data.pointer.child_type;
6266 ZigType *usize_type = g->builtin_types.entry_usize;
6267
6268 bool done = false;
6269 if (ptr_type->data.pointer.is_const || ptr_type->data.pointer.is_volatile ||
6270 ptr_type->data.pointer.explicit_alignment != 0 || ptr_type->data.pointer.allow_zero)
6271 {
6272 ZigType *peer_ptr_type = get_pointer_to_type_extra(g, child_type, false, false,
6273 PtrLenUnknown, 0, 0, 0, false);
6274 ZigType *peer_slice_type = get_slice_type(g, peer_ptr_type);
6275
6276 assertNoError(type_resolve(g, peer_slice_type, wanted_resolve_status));
6277 type->llvm_type = peer_slice_type->llvm_type;
6278 type->llvm_di_type = peer_slice_type->llvm_di_type;
6279 type->data.structure.resolve_status = peer_slice_type->data.structure.resolve_status;
6280 done = true;
6281 }
6282
6283 // If the child type is []const T then we need to make sure the type ref
6284 // and debug info is the same as if the child type were []T.
6285 if (is_slice(child_type)) {
6286 ZigType *child_ptr_type = child_type->data.structure.fields[slice_ptr_index].type_entry;
6287 assert(child_ptr_type->id == ZigTypeIdPointer);
6288 if (child_ptr_type->data.pointer.is_const || child_ptr_type->data.pointer.is_volatile ||
6289 child_ptr_type->data.pointer.explicit_alignment != 0 || child_ptr_type->data.pointer.allow_zero)
6290 {
6291 ZigType *grand_child_type = child_ptr_type->data.pointer.child_type;
6292 ZigType *bland_child_ptr_type = get_pointer_to_type_extra(g, grand_child_type, false, false,
6293 PtrLenUnknown, 0, 0, 0, false);
6294 ZigType *bland_child_slice = get_slice_type(g, bland_child_ptr_type);
6295 ZigType *peer_ptr_type = get_pointer_to_type_extra(g, bland_child_slice, false, false,
6296 PtrLenUnknown, 0, 0, 0, false);
6297 ZigType *peer_slice_type = get_slice_type(g, peer_ptr_type);
6298
6299 assertNoError(type_resolve(g, peer_slice_type, wanted_resolve_status));
6300 type->llvm_type = peer_slice_type->llvm_type;
6301 type->llvm_di_type = peer_slice_type->llvm_di_type;
6302 type->data.structure.resolve_status = peer_slice_type->data.structure.resolve_status;
6303 done = true;
6304 }
6305 }
6306
6307 if (done) return;
6308
6309 LLVMTypeRef usize_llvm_type = get_llvm_type(g, usize_type);
6310 ZigLLVMDIType *usize_llvm_di_type = get_llvm_di_type(g, usize_type);
6311 ZigLLVMDIScope *compile_unit_scope = ZigLLVMCompileUnitToScope(g->compile_unit);
6312 ZigLLVMDIFile *di_file = nullptr;
6313 unsigned line = 0;
6314
6315 if (type->data.structure.resolve_status < ResolveStatusLLVMFwdDecl) {
6316 type->llvm_type = LLVMStructCreateNamed(LLVMGetGlobalContext(), buf_ptr(&type->name));
6317
6318 type->llvm_di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
6319 ZigLLVMTag_DW_structure_type(), buf_ptr(&type->name),
6320 compile_unit_scope, di_file, line);
6321
6322 type->data.structure.resolve_status = ResolveStatusLLVMFwdDecl;
6323 if (ResolveStatusLLVMFwdDecl >= wanted_resolve_status) return;
6324 }
6325
6326 if (!type_has_bits(child_type)) {
6327 LLVMTypeRef element_types[] = {
6328 usize_llvm_type,
6329 };
6330 LLVMStructSetBody(type->llvm_type, element_types, 1, false);
6331
6332 uint64_t len_debug_size_in_bits = usize_type->size_in_bits;
6333 uint64_t len_debug_align_in_bits = 8*usize_type->abi_align;
6334 uint64_t len_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, type->llvm_type, 0);
6335
6336 uint64_t debug_size_in_bits = type->size_in_bits;
6337 uint64_t debug_align_in_bits = 8*type->abi_align;
6338
6339 ZigLLVMDIType *di_element_types[] = {
6340 ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(type->llvm_di_type),
6341 "len", di_file, line,
6342 len_debug_size_in_bits,
6343 len_debug_align_in_bits,
6344 len_offset_in_bits,
6345 0, usize_llvm_di_type),
6346 };
6347 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
6348 compile_unit_scope,
6349 buf_ptr(&type->name),
6350 di_file, line, debug_size_in_bits, debug_align_in_bits, 0,
6351 nullptr, di_element_types, 1, 0, nullptr, "");
6352
6353 ZigLLVMReplaceTemporary(g->dbuilder, type->llvm_di_type, replacement_di_type);
6354 type->llvm_di_type = replacement_di_type;
6355 type->data.structure.resolve_status = ResolveStatusLLVMFull;
6356 return;
6357 }
6358
6359 LLVMTypeRef element_types[2];
6360 element_types[slice_ptr_index] = get_llvm_type(g, ptr_type);
6361 element_types[slice_len_index] = get_llvm_type(g, g->builtin_types.entry_usize);
6362 if (type->data.structure.resolve_status >= wanted_resolve_status) return;
6363 LLVMStructSetBody(type->llvm_type, element_types, 2, false);
6364
6365 uint64_t ptr_debug_size_in_bits = ptr_type->size_in_bits;
6366 uint64_t ptr_debug_align_in_bits = 8*ptr_type->abi_align;
6367 uint64_t ptr_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, type->llvm_type, 0);
6368
6369 uint64_t len_debug_size_in_bits = usize_type->size_in_bits;
6370 uint64_t len_debug_align_in_bits = 8*usize_type->abi_align;
6371 uint64_t len_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, type->llvm_type, 1);
6372
6373 uint64_t debug_size_in_bits = type->size_in_bits;
6374 uint64_t debug_align_in_bits = 8*type->abi_align;
6375
6376 ZigLLVMDIType *di_element_types[] = {
6377 ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(type->llvm_di_type),
6378 "ptr", di_file, line,
6379 ptr_debug_size_in_bits,
6380 ptr_debug_align_in_bits,
6381 ptr_offset_in_bits,
6382 0, get_llvm_di_type(g, ptr_type)),
6383 ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(type->llvm_di_type),
6384 "len", di_file, line,
6385 len_debug_size_in_bits,
6386 len_debug_align_in_bits,
6387 len_offset_in_bits,
6388 0, usize_llvm_di_type),
6389 };
6390 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
6391 compile_unit_scope,
6392 buf_ptr(&type->name),
6393 di_file, line, debug_size_in_bits, debug_align_in_bits, 0,
6394 nullptr, di_element_types, 2, 0, nullptr, "");
6395
6396 ZigLLVMReplaceTemporary(g->dbuilder, type->llvm_di_type, replacement_di_type);
6397 type->llvm_di_type = replacement_di_type;
6398 type->data.structure.resolve_status = ResolveStatusLLVMFull;
6399}
6400
6401static void resolve_llvm_types_struct(CodeGen *g, ZigType *struct_type, ResolveStatus wanted_resolve_status) {
6402 assert(struct_type->id == ZigTypeIdStruct);
6403 assert(struct_type->data.structure.resolve_status != ResolveStatusInvalid);
6404 assert(struct_type->data.structure.resolve_status >= ResolveStatusSizeKnown);
6405 assert(struct_type->data.structure.fields || struct_type->data.structure.src_field_count == 0);
6406 if (struct_type->data.structure.resolve_status >= wanted_resolve_status) return;
6407
6408 AstNode *decl_node = struct_type->data.structure.decl_node;
6409 ZigLLVMDIFile *di_file;
6410 ZigLLVMDIScope *di_scope;
6411 unsigned line;
6412 if (decl_node != nullptr) {
6413 assert(decl_node->type == NodeTypeContainerDecl);
6414 Scope *scope = &struct_type->data.structure.decls_scope->base;
6415 ZigType *import = get_scope_import(scope);
6416 di_file = import->data.structure.root_struct->di_file;
6417 di_scope = ZigLLVMFileToScope(di_file);
6418 line = decl_node->line + 1;
6419 } else {
6420 di_file = nullptr;
6421 di_scope = ZigLLVMCompileUnitToScope(g->compile_unit);
6422 line = 0;
6423 }
6424
6425 if (struct_type->data.structure.resolve_status < ResolveStatusLLVMFwdDecl) {
6426 struct_type->llvm_type = type_has_bits(struct_type) ?
6427 LLVMStructCreateNamed(LLVMGetGlobalContext(), buf_ptr(&struct_type->name)) : LLVMVoidType();
6428 unsigned dwarf_kind = ZigLLVMTag_DW_structure_type();
6429 struct_type->llvm_di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
6430 dwarf_kind, buf_ptr(&struct_type->name),
6431 di_scope, di_file, line);
6432
6433 struct_type->data.structure.resolve_status = ResolveStatusLLVMFwdDecl;
6434 if (ResolveStatusLLVMFwdDecl >= wanted_resolve_status) return;
6435 }
6436
6437 size_t field_count = struct_type->data.structure.src_field_count;
6438 size_t gen_field_count = struct_type->data.structure.gen_field_count;
6439 LLVMTypeRef *element_types = allocate<LLVMTypeRef>(gen_field_count);
6440
6441 size_t gen_field_index = 0;
6442 bool packed = (struct_type->data.structure.layout == ContainerLayoutPacked);
6443 size_t packed_bits_offset = 0;
6444 size_t first_packed_bits_offset_misalign = SIZE_MAX;
6445 size_t debug_field_count = 0;
6446
6447 // trigger all the recursive get_llvm_type calls
6448 for (size_t i = 0; i < field_count; i += 1) {
6449 TypeStructField *type_struct_field = &struct_type->data.structure.fields[i];
6450 ZigType *field_type = type_struct_field->type_entry;
6451 if (!type_has_bits(field_type))
6452 continue;
6453 (void)get_llvm_type(g, field_type);
6454 if (struct_type->data.structure.resolve_status >= wanted_resolve_status) return;
6455 }
6456
6457 for (size_t i = 0; i < field_count; i += 1) {
6458 TypeStructField *type_struct_field = &struct_type->data.structure.fields[i];
6459 ZigType *field_type = type_struct_field->type_entry;
6460
6461 if (!type_has_bits(field_type))
6462 continue;
6463
6464 if (packed) {
6465 size_t field_size_in_bits = type_size_bits(g, field_type);
6466 size_t next_packed_bits_offset = packed_bits_offset + field_size_in_bits;
6467
6468 if (first_packed_bits_offset_misalign != SIZE_MAX) {
6469 // this field is not byte-aligned; it is part of the previous field with a bit offset
6470
6471 size_t full_bit_count = next_packed_bits_offset - first_packed_bits_offset_misalign;
6472 size_t full_abi_size = get_abi_size_bytes(full_bit_count, g->pointer_size_bytes);
6473 if (full_abi_size * 8 == full_bit_count) {
6474 // next field recovers ABI alignment
6475 element_types[gen_field_index] = LLVMIntType((unsigned)(full_bit_count));
6476 gen_field_index += 1;
6477
6478 first_packed_bits_offset_misalign = SIZE_MAX;
6479 }
6480 } else if (get_abi_size_bytes(field_type->size_in_bits, g->pointer_size_bytes) * 8 != field_size_in_bits) {
6481 first_packed_bits_offset_misalign = packed_bits_offset;
6482 } else {
6483 // This is a byte-aligned field (both start and end) in a packed struct.
6484 element_types[gen_field_index] = get_llvm_type(g, field_type);
6485 gen_field_index += 1;
6486 }
6487 packed_bits_offset = next_packed_bits_offset;
6488 } else {
6489 element_types[gen_field_index] = get_llvm_type(g, field_type);
6490
6491 gen_field_index += 1;
6492 }
6493 debug_field_count += 1;
6494 }
6495 if (first_packed_bits_offset_misalign != SIZE_MAX) {
6496 size_t full_bit_count = packed_bits_offset - first_packed_bits_offset_misalign;
6497 size_t full_abi_size = get_abi_size_bytes(full_bit_count, g->pointer_size_bytes);
6498 element_types[gen_field_index] = LLVMIntType((unsigned)full_abi_size * 8);
6499 gen_field_index += 1;
6500 }
6501
6502 if (type_has_bits(struct_type)) {
6503 LLVMStructSetBody(struct_type->llvm_type, element_types, (unsigned)gen_field_count, packed);
6504 }
6505
6506 ZigLLVMDIType **di_element_types = allocate<ZigLLVMDIType*>(debug_field_count);
6507 size_t debug_field_index = 0;
6508 for (size_t i = 0; i < field_count; i += 1) {
6509 TypeStructField *type_struct_field = &struct_type->data.structure.fields[i];
6510 size_t gen_field_index = type_struct_field->gen_index;
6511 if (gen_field_index == SIZE_MAX) {
6512 continue;
6513 }
6514
6515 ZigType *field_type = type_struct_field->type_entry;
6516
6517 // if the field is a function, actually the debug info should be a pointer.
6518 ZigLLVMDIType *field_di_type;
6519 if (field_type->id == ZigTypeIdFn) {
6520 ZigType *field_ptr_type = get_pointer_to_type(g, field_type, true);
6521 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, get_llvm_type(g, field_ptr_type));
6522 uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, get_llvm_type(g, field_ptr_type));
6523 field_di_type = ZigLLVMCreateDebugPointerType(g->dbuilder, get_llvm_di_type(g, field_type),
6524 debug_size_in_bits, debug_align_in_bits, buf_ptr(&field_ptr_type->name));
6525 } else {
6526 field_di_type = get_llvm_di_type(g, field_type);
6527 }
6528
6529 uint64_t debug_size_in_bits;
6530 uint64_t debug_align_in_bits;
6531 uint64_t debug_offset_in_bits;
6532 if (packed) {
6533 debug_size_in_bits = type_struct_field->type_entry->size_in_bits;
6534 debug_align_in_bits = 8 * type_struct_field->type_entry->abi_align;
6535 debug_offset_in_bits = 8 * type_struct_field->offset + type_struct_field->bit_offset_in_host;
6536 } else {
6537 debug_size_in_bits = 8 * get_store_size_bytes(field_type->size_in_bits);
6538 debug_align_in_bits = 8 * field_type->abi_align;
6539 debug_offset_in_bits = 8 * type_struct_field->offset;
6540 }
6541 unsigned line;
6542 if (decl_node != nullptr) {
6543 AstNode *field_node = decl_node->data.container_decl.fields.at(i);
6544 line = field_node->line + 1;
6545 } else {
6546 line = 0;
6547 }
6548 di_element_types[debug_field_index] = ZigLLVMCreateDebugMemberType(g->dbuilder,
6549 ZigLLVMTypeToScope(struct_type->llvm_di_type), buf_ptr(type_struct_field->name),
6550 di_file, line,
6551 debug_size_in_bits,
6552 debug_align_in_bits,
6553 debug_offset_in_bits,
6554 0, field_di_type);
6555 assert(di_element_types[debug_field_index]);
6556 debug_field_index += 1;
6557 }
6558
6559 uint64_t debug_size_in_bits = 8*get_store_size_bytes(struct_type->size_in_bits);
6560 uint64_t debug_align_in_bits = 8*struct_type->abi_align;
6561 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
6562 di_scope,
6563 buf_ptr(&struct_type->name),
6564 di_file, line,
6565 debug_size_in_bits,
6566 debug_align_in_bits,
6567 0, nullptr, di_element_types, (int)debug_field_count, 0, nullptr, "");
6568
6569 ZigLLVMReplaceTemporary(g->dbuilder, struct_type->llvm_di_type, replacement_di_type);
6570 struct_type->llvm_di_type = replacement_di_type;
6571 struct_type->data.structure.resolve_status = ResolveStatusLLVMFull;
6572}
6573
6574static void resolve_llvm_types_enum(CodeGen *g, ZigType *enum_type) {
6575 assert(!enum_type->data.enumeration.is_invalid);
6576 assert(enum_type->data.enumeration.complete);
6577 if (enum_type->llvm_di_type != nullptr) return;
6578
6579 Scope *scope = &enum_type->data.enumeration.decls_scope->base;
6580 ZigType *import = get_scope_import(scope);
6581 AstNode *decl_node = enum_type->data.enumeration.decl_node;
6582
6583 if (!type_has_bits(enum_type)) {
6584 enum_type->llvm_type = g->builtin_types.entry_void->llvm_type;
6585
6586 uint64_t debug_size_in_bits = 0;
6587 uint64_t debug_align_in_bits = 0;
6588 ZigLLVMDIType **di_element_types = nullptr;
6589 size_t debug_field_count = 0;
6590 enum_type->llvm_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
6591 ZigLLVMFileToScope(import->data.structure.root_struct->di_file),
6592 buf_ptr(&enum_type->name),
6593 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
6594 debug_size_in_bits,
6595 debug_align_in_bits,
6596 0, nullptr, di_element_types, (int)debug_field_count, 0, nullptr, "");
6597 return;
6598 }
6599
6600 uint32_t field_count = enum_type->data.enumeration.src_field_count;
6601
6602 assert(enum_type->data.enumeration.fields);
6603 ZigLLVMDIEnumerator **di_enumerators = allocate<ZigLLVMDIEnumerator*>(field_count);
6604
6605 for (uint32_t i = 0; i < field_count; i += 1) {
6606 TypeEnumField *enum_field = &enum_type->data.enumeration.fields[i];
6607
6608 // TODO send patch to LLVM to support APInt in createEnumerator instead of int64_t
6609 // http://lists.llvm.org/pipermail/llvm-dev/2017-December/119456.html
6610 di_enumerators[i] = ZigLLVMCreateDebugEnumerator(g->dbuilder, buf_ptr(enum_field->name),
6611 bigint_as_signed(&enum_field->value));
6612 }
6613
6614 ZigType *tag_int_type = enum_type->data.enumeration.tag_int_type;
6615 enum_type->llvm_type = get_llvm_type(g, tag_int_type);
6616
6617 // create debug type for tag
6618 uint64_t tag_debug_size_in_bits = tag_int_type->size_in_bits;
6619 uint64_t tag_debug_align_in_bits = 8*tag_int_type->abi_align;
6620 ZigLLVMDIType *tag_di_type = ZigLLVMCreateDebugEnumerationType(g->dbuilder,
6621 ZigLLVMFileToScope(import->data.structure.root_struct->di_file), buf_ptr(&enum_type->name),
6622 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
6623 tag_debug_size_in_bits,
6624 tag_debug_align_in_bits,
6625 di_enumerators, field_count,
6626 get_llvm_di_type(g, tag_int_type), "");
6627
6628 enum_type->llvm_di_type = tag_di_type;
6629}
6630
6631static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type, ResolveStatus wanted_resolve_status) {
6632 if (union_type->data.unionation.resolve_status >= wanted_resolve_status) return;
6633
6634 ZigType *most_aligned_union_member = union_type->data.unionation.most_aligned_union_member;
6635 ZigType *tag_type = union_type->data.unionation.tag_type;
6636 if (most_aligned_union_member == nullptr) {
6637 union_type->llvm_type = get_llvm_type(g, tag_type);
6638 union_type->llvm_di_type = get_llvm_di_type(g, tag_type);
6639 union_type->data.unionation.resolve_status = ResolveStatusLLVMFull;
6640 return;
6641 }
6642
6643 Scope *scope = &union_type->data.unionation.decls_scope->base;
6644 ZigType *import = get_scope_import(scope);
6645 AstNode *decl_node = union_type->data.unionation.decl_node;
6646
6647 if (union_type->data.unionation.resolve_status < ResolveStatusLLVMFwdDecl) {
6648 union_type->llvm_type = LLVMStructCreateNamed(LLVMGetGlobalContext(), buf_ptr(&union_type->name));
6649 size_t line = decl_node ? decl_node->line : 0;
6650 unsigned dwarf_kind = ZigLLVMTag_DW_structure_type();
6651 union_type->llvm_di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
6652 dwarf_kind, buf_ptr(&union_type->name),
6653 ZigLLVMFileToScope(import->data.structure.root_struct->di_file),
6654 import->data.structure.root_struct->di_file, (unsigned)(line + 1));
6655
6656 union_type->data.unionation.resolve_status = ResolveStatusLLVMFwdDecl;
6657 if (ResolveStatusLLVMFwdDecl >= wanted_resolve_status) return;
6658 }
6659
6660 uint32_t gen_field_count = union_type->data.unionation.gen_field_count;
6661 ZigLLVMDIType **union_inner_di_types = allocate<ZigLLVMDIType*>(gen_field_count);
6662 uint32_t field_count = union_type->data.unionation.src_field_count;
6663 for (uint32_t i = 0; i < field_count; i += 1) {
6664 TypeUnionField *union_field = &union_type->data.unionation.fields[i];
6665 if (!type_has_bits(union_field->type_entry))
6666 continue;
6667
6668 ZigLLVMDIType *field_di_type = get_llvm_di_type(g, union_field->type_entry);
6669 if (union_type->data.unionation.resolve_status >= wanted_resolve_status) return;
6670
6671 uint64_t store_size_in_bits = union_field->type_entry->size_in_bits;
6672 uint64_t abi_align_in_bits = 8*union_field->type_entry->abi_align;
6673 AstNode *field_node = decl_node->data.container_decl.fields.at(i);
6674 union_inner_di_types[union_field->gen_index] = ZigLLVMCreateDebugMemberType(g->dbuilder,
6675 ZigLLVMTypeToScope(union_type->llvm_di_type), buf_ptr(union_field->enum_field->name),
6676 import->data.structure.root_struct->di_file, (unsigned)(field_node->line + 1),
6677 store_size_in_bits,
6678 abi_align_in_bits,
6679 0,
6680 0, field_di_type);
6681
6682 }
6683
6684 if (tag_type == nullptr || !type_has_bits(tag_type)) {
6685 assert(most_aligned_union_member != nullptr);
6686
6687 size_t padding_bytes = union_type->data.unionation.union_abi_size - most_aligned_union_member->abi_size;
6688 if (padding_bytes > 0) {
6689 ZigType *u8_type = get_int_type(g, false, 8);
6690 ZigType *padding_array = get_array_type(g, u8_type, padding_bytes);
6691 LLVMTypeRef union_element_types[] = {
6692 most_aligned_union_member->llvm_type,
6693 get_llvm_type(g, padding_array),
6694 };
6695 LLVMStructSetBody(union_type->llvm_type, union_element_types, 2, false);
6696 } else {
6697 LLVMStructSetBody(union_type->llvm_type, &most_aligned_union_member->llvm_type, 1, false);
6698 }
6699 union_type->data.unionation.union_llvm_type = union_type->llvm_type;
6700 union_type->data.unionation.gen_tag_index = SIZE_MAX;
6701 union_type->data.unionation.gen_union_index = SIZE_MAX;
6702
6703 // create debug type for union
6704 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugUnionType(g->dbuilder,
6705 ZigLLVMFileToScope(import->data.structure.root_struct->di_file), buf_ptr(&union_type->name),
6706 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
6707 union_type->data.unionation.union_abi_size * 8,
6708 most_aligned_union_member->abi_align * 8,
6709 0, union_inner_di_types,
6710 gen_field_count, 0, "");
6711
6712 ZigLLVMReplaceTemporary(g->dbuilder, union_type->llvm_di_type, replacement_di_type);
6713 union_type->llvm_di_type = replacement_di_type;
6714 union_type->data.unionation.resolve_status = ResolveStatusLLVMFull;
6715 return;
6716 }
6717
6718 LLVMTypeRef union_type_ref;
6719 size_t padding_bytes = union_type->data.unionation.union_abi_size - most_aligned_union_member->abi_size;
6720 if (padding_bytes == 0) {
6721 union_type_ref = get_llvm_type(g, most_aligned_union_member);
6722 } else {
6723 ZigType *u8_type = get_int_type(g, false, 8);
6724 ZigType *padding_array = get_array_type(g, u8_type, padding_bytes);
6725 LLVMTypeRef union_element_types[] = {
6726 get_llvm_type(g, most_aligned_union_member),
6727 get_llvm_type(g, padding_array),
6728 };
6729 union_type_ref = LLVMStructType(union_element_types, 2, false);
6730 }
6731 union_type->data.unionation.union_llvm_type = union_type_ref;
6732
6733 LLVMTypeRef root_struct_element_types[2];
6734 root_struct_element_types[union_type->data.unionation.gen_tag_index] = get_llvm_type(g, tag_type);
6735 root_struct_element_types[union_type->data.unionation.gen_union_index] = union_type_ref;
6736 LLVMStructSetBody(union_type->llvm_type, root_struct_element_types, 2, false);
6737
6738 // create debug type for union
6739 ZigLLVMDIType *union_di_type = ZigLLVMCreateDebugUnionType(g->dbuilder,
6740 ZigLLVMTypeToScope(union_type->llvm_di_type), "AnonUnion",
6741 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
6742 most_aligned_union_member->size_in_bits, 8*most_aligned_union_member->abi_align,
6743 0, union_inner_di_types, gen_field_count, 0, "");
6744
6745 uint64_t union_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, union_type->llvm_type,
6746 union_type->data.unionation.gen_union_index);
6747 uint64_t tag_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, union_type->llvm_type,
6748 union_type->data.unionation.gen_tag_index);
6749
6750 ZigLLVMDIType *union_member_di_type = ZigLLVMCreateDebugMemberType(g->dbuilder,
6751 ZigLLVMTypeToScope(union_type->llvm_di_type), "payload",
6752 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
6753 most_aligned_union_member->size_in_bits,
6754 8*most_aligned_union_member->abi_align,
6755 union_offset_in_bits,
6756 0, union_di_type);
6757
6758 uint64_t tag_debug_size_in_bits = tag_type->size_in_bits;
6759 uint64_t tag_debug_align_in_bits = 8*tag_type->abi_align;
6760
6761 ZigLLVMDIType *tag_member_di_type = ZigLLVMCreateDebugMemberType(g->dbuilder,
6762 ZigLLVMTypeToScope(union_type->llvm_di_type), "tag",
6763 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
6764 tag_debug_size_in_bits,
6765 tag_debug_align_in_bits,
6766 tag_offset_in_bits,
6767 0, get_llvm_di_type(g, tag_type));
6768
6769 ZigLLVMDIType *di_root_members[2];
6770 di_root_members[union_type->data.unionation.gen_tag_index] = tag_member_di_type;
6771 di_root_members[union_type->data.unionation.gen_union_index] = union_member_di_type;
6772
6773 uint64_t debug_size_in_bits = union_type->size_in_bits;
6774 uint64_t debug_align_in_bits = 8*union_type->abi_align;
6775 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
6776 ZigLLVMFileToScope(import->data.structure.root_struct->di_file),
6777 buf_ptr(&union_type->name),
6778 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
6779 debug_size_in_bits,
6780 debug_align_in_bits,
6781 0, nullptr, di_root_members, 2, 0, nullptr, "");
6782
6783 ZigLLVMReplaceTemporary(g->dbuilder, union_type->llvm_di_type, replacement_di_type);
6784 union_type->llvm_di_type = replacement_di_type;
6785 union_type->data.unionation.resolve_status = ResolveStatusLLVMFull;
6786}
6787
6788static void resolve_llvm_types_pointer(CodeGen *g, ZigType *type) {
6789 if (type->llvm_di_type != nullptr) return;
6790
6791 if (!type_has_bits(type)) {
6792 type->llvm_type = g->builtin_types.entry_void->llvm_type;
6793 type->llvm_di_type = g->builtin_types.entry_void->llvm_di_type;
6794 return;
6795 }
6796
6797 ZigType *elem_type = type->data.pointer.child_type;
6798
6799 if (type->data.pointer.is_const || type->data.pointer.is_volatile ||
6800 type->data.pointer.explicit_alignment != 0 || type->data.pointer.ptr_len != PtrLenSingle ||
6801 type->data.pointer.bit_offset_in_host != 0 || type->data.pointer.allow_zero)
6802 {
6803 ZigType *peer_type = get_pointer_to_type_extra(g, elem_type, false, false,
6804 PtrLenSingle, 0, 0, type->data.pointer.host_int_bytes, false);
6805 type->llvm_type = get_llvm_type(g, peer_type);
6806 type->llvm_di_type = get_llvm_di_type(g, peer_type);
6807 return;
6808 }
6809
6810 if (type->data.pointer.host_int_bytes == 0) {
6811 assertNoError(type_resolve(g, elem_type, ResolveStatusLLVMFwdDecl));
6812 type->llvm_type = LLVMPointerType(elem_type->llvm_type, 0);
6813 uint64_t debug_size_in_bits = 8*get_store_size_bytes(type->size_in_bits);
6814 uint64_t debug_align_in_bits = 8*type->abi_align;
6815 type->llvm_di_type = ZigLLVMCreateDebugPointerType(g->dbuilder, elem_type->llvm_di_type,
6816 debug_size_in_bits, debug_align_in_bits, buf_ptr(&type->name));
6817 assertNoError(type_resolve(g, elem_type, ResolveStatusLLVMFull));
6818 } else {
6819 ZigType *host_int_type = get_int_type(g, false, type->data.pointer.host_int_bytes * 8);
6820 LLVMTypeRef host_int_llvm_type = get_llvm_type(g, host_int_type);
6821 type->llvm_type = LLVMPointerType(host_int_llvm_type, 0);
6822 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, host_int_llvm_type);
6823 uint64_t debug_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, host_int_llvm_type);
6824 type->llvm_di_type = ZigLLVMCreateDebugPointerType(g->dbuilder, get_llvm_di_type(g, host_int_type),
6825 debug_size_in_bits, debug_align_in_bits, buf_ptr(&type->name));
6826 }
6827}
6828
6829static void resolve_llvm_types_integer(CodeGen *g, ZigType *type) {
6830 if (type->llvm_di_type != nullptr) return;
6831
6832 if (!type_has_bits(type)) {
6833 type->llvm_type = g->builtin_types.entry_void->llvm_type;
6834 type->llvm_di_type = g->builtin_types.entry_void->llvm_di_type;
6835 return;
6836 }
6837
6838 unsigned dwarf_tag;
6839 if (type->data.integral.is_signed) {
6840 if (type->size_in_bits == 8) {
6841 dwarf_tag = ZigLLVMEncoding_DW_ATE_signed_char();
6842 } else {
6843 dwarf_tag = ZigLLVMEncoding_DW_ATE_signed();
6844 }
6845 } else {
6846 if (type->size_in_bits == 8) {
6847 dwarf_tag = ZigLLVMEncoding_DW_ATE_unsigned_char();
6848 } else {
6849 dwarf_tag = ZigLLVMEncoding_DW_ATE_unsigned();
6850 }
6851 }
6852
6853 type->llvm_di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&type->name), type->size_in_bits, dwarf_tag);
6854 type->llvm_type = LLVMIntType(type->size_in_bits);
6855}
6856
6857static void resolve_llvm_types_optional(CodeGen *g, ZigType *type) {
6858 if (type->llvm_di_type != nullptr) return;
6859
6860 LLVMTypeRef bool_llvm_type = get_llvm_type(g, g->builtin_types.entry_bool);
6861 ZigLLVMDIType *bool_llvm_di_type = get_llvm_di_type(g, g->builtin_types.entry_bool);
6862
6863 ZigType *child_type = type->data.maybe.child_type;
6864 if (!type_has_bits(child_type)) {
6865 type->llvm_type = bool_llvm_type;
6866 type->llvm_di_type = bool_llvm_di_type;
6867 return;
6868 }
6869
6870 LLVMTypeRef child_llvm_type = get_llvm_type(g, child_type);
6871 ZigLLVMDIType *child_llvm_di_type = get_llvm_di_type(g, child_type);
6872
6873 if (type_is_nonnull_ptr(child_type) || child_type->id == ZigTypeIdErrorSet) {
6874 type->llvm_type = child_llvm_type;
6875 type->llvm_di_type = child_llvm_di_type;
6876 return;
6877 }
6878
6879 LLVMTypeRef elem_types[] = {
6880 get_llvm_type(g, child_type),
6881 LLVMInt1Type(),
6882 };
6883 type->llvm_type = LLVMStructType(elem_types, 2, false);
6884
6885 ZigLLVMDIScope *compile_unit_scope = ZigLLVMCompileUnitToScope(g->compile_unit);
6886 ZigLLVMDIFile *di_file = nullptr;
6887 unsigned line = 0;
6888 type->llvm_di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
6889 ZigLLVMTag_DW_structure_type(), buf_ptr(&type->name),
6890 compile_unit_scope, di_file, line);
6891
6892 uint64_t val_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, child_llvm_type);
6893 uint64_t val_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, child_llvm_type);
6894 uint64_t val_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, type->llvm_type, 0);
6895
6896 uint64_t maybe_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, bool_llvm_type);
6897 uint64_t maybe_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, bool_llvm_type);
6898 uint64_t maybe_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, type->llvm_type, 1);
6899
6900 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, type->llvm_type);
6901 uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, type->llvm_type);
6902
6903 ZigLLVMDIType *di_element_types[] = {
6904 ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(type->llvm_di_type),
6905 "val", di_file, line,
6906 val_debug_size_in_bits,
6907 val_debug_align_in_bits,
6908 val_offset_in_bits,
6909 0, child_llvm_di_type),
6910 ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(type->llvm_di_type),
6911 "maybe", di_file, line,
6912 maybe_debug_size_in_bits,
6913 maybe_debug_align_in_bits,
6914 maybe_offset_in_bits,
6915 0, bool_llvm_di_type),
6916 };
6917 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
6918 compile_unit_scope,
6919 buf_ptr(&type->name),
6920 di_file, line, debug_size_in_bits, debug_align_in_bits, 0,
6921 nullptr, di_element_types, 2, 0, nullptr, "");
6922
6923 ZigLLVMReplaceTemporary(g->dbuilder, type->llvm_di_type, replacement_di_type);
6924 type->llvm_di_type = replacement_di_type;
6925}
6926
6927static void resolve_llvm_types_error_union(CodeGen *g, ZigType *type) {
6928 if (type->llvm_di_type != nullptr) return;
6929
6930 ZigType *payload_type = type->data.error_union.payload_type;
6931 ZigType *err_set_type = type->data.error_union.err_set_type;
6932
6933 if (!type_has_bits(payload_type)) {
6934 assert(type_has_bits(err_set_type));
6935 type->llvm_type = get_llvm_type(g, err_set_type);
6936 type->llvm_di_type = get_llvm_di_type(g, err_set_type);
6937 } else if (!type_has_bits(err_set_type)) {
6938 type->llvm_type = get_llvm_type(g, payload_type);
6939 type->llvm_di_type = get_llvm_di_type(g, payload_type);
6940 } else {
6941 LLVMTypeRef err_set_llvm_type = get_llvm_type(g, err_set_type);
6942 LLVMTypeRef payload_llvm_type = get_llvm_type(g, payload_type);
6943 LLVMTypeRef elem_types[2];
6944 elem_types[err_union_err_index] = err_set_llvm_type;
6945 elem_types[err_union_payload_index] = payload_llvm_type;
6946 type->llvm_type = LLVMStructType(elem_types, 2, false);
6947
6948 ZigLLVMDIScope *compile_unit_scope = ZigLLVMCompileUnitToScope(g->compile_unit);
6949 ZigLLVMDIFile *di_file = nullptr;
6950 unsigned line = 0;
6951 type->llvm_di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
6952 ZigLLVMTag_DW_structure_type(), buf_ptr(&type->name),
6953 compile_unit_scope, di_file, line);
6954
6955 uint64_t tag_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, err_set_llvm_type);
6956 uint64_t tag_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, err_set_llvm_type);
6957 uint64_t tag_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, type->llvm_type, err_union_err_index);
6958
6959 uint64_t value_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, payload_llvm_type);
6960 uint64_t value_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, payload_llvm_type);
6961 uint64_t value_offset_in_bits = 8*LLVMOffsetOfElement(g->target_data_ref, type->llvm_type,
6962 err_union_payload_index);
6963
6964 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, type->llvm_type);
6965 uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, type->llvm_type);
6966
6967 ZigLLVMDIType *di_element_types[] = {
6968 ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(type->llvm_di_type),
6969 "tag", di_file, line,
6970 tag_debug_size_in_bits,
6971 tag_debug_align_in_bits,
6972 tag_offset_in_bits,
6973 0, get_llvm_di_type(g, err_set_type)),
6974 ZigLLVMCreateDebugMemberType(g->dbuilder, ZigLLVMTypeToScope(type->llvm_di_type),
6975 "value", di_file, line,
6976 value_debug_size_in_bits,
6977 value_debug_align_in_bits,
6978 value_offset_in_bits,
6979 0, get_llvm_di_type(g, payload_type)),
6980 };
6981
6982 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
6983 compile_unit_scope,
6984 buf_ptr(&type->name),
6985 di_file, line,
6986 debug_size_in_bits,
6987 debug_align_in_bits,
6988 0,
6989 nullptr, di_element_types, 2, 0, nullptr, "");
6990
6991 ZigLLVMReplaceTemporary(g->dbuilder, type->llvm_di_type, replacement_di_type);
6992 type->llvm_di_type = replacement_di_type;
6993 }
6994}
6995
6996static void resolve_llvm_types_array(CodeGen *g, ZigType *type) {
6997 if (type->llvm_di_type != nullptr) return;
6998
6999 if (!type_has_bits(type)) {
7000 type->llvm_type = g->builtin_types.entry_void->llvm_type;
7001 type->llvm_di_type = g->builtin_types.entry_void->llvm_di_type;
7002 return;
7003 }
7004
7005 ZigType *elem_type = type->data.array.child_type;
7006
7007 // TODO https://github.com/ziglang/zig/issues/1424
7008 type->llvm_type = LLVMArrayType(get_llvm_type(g, elem_type), (unsigned)type->data.array.len);
7009
7010 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, type->llvm_type);
7011 uint64_t debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, type->llvm_type);
7012
7013 type->llvm_di_type = ZigLLVMCreateDebugArrayType(g->dbuilder, debug_size_in_bits,
7014 debug_align_in_bits, get_llvm_di_type(g, elem_type), (int)type->data.array.len);
7015}
7016
7017static void resolve_llvm_types_fn(CodeGen *g, ZigType *fn_type) {
7018 if (fn_type->llvm_di_type != nullptr) return;
7019
7020 FnTypeId *fn_type_id = &fn_type->data.fn.fn_type_id;
7021 bool first_arg_return = want_first_arg_sret(g, fn_type_id);
7022 bool is_async = fn_type_id->cc == CallingConventionAsync;
7023 bool is_c_abi = fn_type_id->cc == CallingConventionC;
7024 bool prefix_arg_error_return_trace = g->have_err_ret_tracing && fn_type_can_fail(fn_type_id);
7025 // +1 for maybe making the first argument the return value
7026 // +1 for maybe first argument the error return trace
7027 // +2 for maybe arguments async allocator and error code pointer
7028 ZigList<LLVMTypeRef> gen_param_types = {};
7029 // +1 because 0 is the return type and
7030 // +1 for maybe making first arg ret val and
7031 // +1 for maybe first argument the error return trace
7032 // +2 for maybe arguments async allocator and error code pointer
7033 ZigList<ZigLLVMDIType *> param_di_types = {};
7034 param_di_types.append(get_llvm_di_type(g, fn_type_id->return_type));
7035 ZigType *gen_return_type;
7036 if (is_async) {
7037 gen_return_type = get_pointer_to_type(g, g->builtin_types.entry_u8, false);
7038 } else if (!type_has_bits(fn_type_id->return_type)) {
7039 gen_return_type = g->builtin_types.entry_void;
7040 } else if (first_arg_return) {
7041 ZigType *gen_type = get_pointer_to_type(g, fn_type_id->return_type, false);
7042 gen_param_types.append(get_llvm_type(g, gen_type));
7043 param_di_types.append(get_llvm_di_type(g, gen_type));
7044 gen_return_type = g->builtin_types.entry_void;
7045 } else {
7046 gen_return_type = fn_type_id->return_type;
7047 }
7048 fn_type->data.fn.gen_return_type = gen_return_type;
7049
7050 if (prefix_arg_error_return_trace) {
7051 ZigType *gen_type = get_ptr_to_stack_trace_type(g);
7052 gen_param_types.append(get_llvm_type(g, gen_type));
7053 param_di_types.append(get_llvm_di_type(g, gen_type));
7054 }
7055 if (is_async) {
7056 {
7057 // async allocator param
7058 ZigType *gen_type = fn_type_id->async_allocator_type;
7059 gen_param_types.append(get_llvm_type(g, gen_type));
7060 param_di_types.append(get_llvm_di_type(g, gen_type));
7061 }
7062
7063 {
7064 // error code pointer
7065 ZigType *gen_type = get_pointer_to_type(g, g->builtin_types.entry_global_error_set, false);
7066 gen_param_types.append(get_llvm_type(g, gen_type));
7067 param_di_types.append(get_llvm_di_type(g, gen_type));
7068 }
7069 }
7070
7071 fn_type->data.fn.gen_param_info = allocate<FnGenParamInfo>(fn_type_id->param_count);
7072 for (size_t i = 0; i < fn_type_id->param_count; i += 1) {
7073 FnTypeParamInfo *src_param_info = &fn_type->data.fn.fn_type_id.param_info[i];
7074 ZigType *type_entry = src_param_info->type;
7075 FnGenParamInfo *gen_param_info = &fn_type->data.fn.gen_param_info[i];
7076
7077 gen_param_info->src_index = i;
7078 gen_param_info->gen_index = SIZE_MAX;
7079
7080 if (is_c_abi || !type_has_bits(type_entry))
7081 continue;
7082
7083 ZigType *gen_type;
7084 if (handle_is_ptr(type_entry)) {
7085 gen_type = get_pointer_to_type(g, type_entry, true);
7086 gen_param_info->is_byval = true;
7087 } else {
7088 gen_type = type_entry;
7089 }
7090 gen_param_info->gen_index = gen_param_types.length;
7091 gen_param_info->type = gen_type;
7092 gen_param_types.append(get_llvm_type(g, gen_type));
7093
7094 param_di_types.append(get_llvm_di_type(g, gen_type));
7095 }
7096
7097 if (is_c_abi) {
7098 FnWalk fn_walk = {};
7099 fn_walk.id = FnWalkIdTypes;
7100 fn_walk.data.types.param_di_types = &param_di_types;
7101 fn_walk.data.types.gen_param_types = &gen_param_types;
7102 walk_function_params(g, fn_type, &fn_walk);
7103 }
7104
7105 fn_type->data.fn.gen_param_count = gen_param_types.length;
7106
7107 for (size_t i = 0; i < gen_param_types.length; i += 1) {
7108 assert(gen_param_types.items[i] != nullptr);
7109 }
7110 fn_type->data.fn.raw_type_ref = LLVMFunctionType(get_llvm_type(g, gen_return_type),
7111 gen_param_types.items, (unsigned int)gen_param_types.length, fn_type_id->is_var_args);
7112 fn_type->llvm_type = LLVMPointerType(fn_type->data.fn.raw_type_ref, 0);
7113 fn_type->data.fn.raw_di_type = ZigLLVMCreateSubroutineType(g->dbuilder, param_di_types.items, (int)param_di_types.length, 0);
7114 fn_type->llvm_di_type = ZigLLVMCreateDebugPointerType(g->dbuilder, fn_type->data.fn.raw_di_type,
7115 LLVMStoreSizeOfType(g->target_data_ref, fn_type->llvm_type),
7116 LLVMABIAlignmentOfType(g->target_data_ref, fn_type->llvm_type), "");
7117}
7118
7119static void resolve_llvm_types_anyerror(CodeGen *g) {
7120 ZigType *entry = g->builtin_types.entry_global_error_set;
7121 entry->llvm_type = get_llvm_type(g, g->err_tag_type);
7122 ZigList<ZigLLVMDIEnumerator *> err_enumerators = {};
7123 // reserve index 0 to indicate no error
7124 err_enumerators.append(ZigLLVMCreateDebugEnumerator(g->dbuilder, "(none)", 0));
7125 for (size_t i = 1; i < g->errors_by_index.length; i += 1) {
7126 ErrorTableEntry *error_entry = g->errors_by_index.at(i);
7127 err_enumerators.append(ZigLLVMCreateDebugEnumerator(g->dbuilder, buf_ptr(&error_entry->name), i));
7128 }
7129
7130 // create debug type for error sets
7131 uint64_t tag_debug_size_in_bits = g->err_tag_type->size_in_bits;
7132 uint64_t tag_debug_align_in_bits = 8*g->err_tag_type->abi_align;
7133 ZigLLVMDIFile *err_set_di_file = nullptr;
7134 entry->llvm_di_type = ZigLLVMCreateDebugEnumerationType(g->dbuilder,
7135 ZigLLVMCompileUnitToScope(g->compile_unit), buf_ptr(&entry->name),
7136 err_set_di_file, 0,
7137 tag_debug_size_in_bits,
7138 tag_debug_align_in_bits,
7139 err_enumerators.items, err_enumerators.length,
7140 get_llvm_di_type(g, g->err_tag_type), "");
7141}
7142
7143static void resolve_llvm_types(CodeGen *g, ZigType *type, ResolveStatus wanted_resolve_status) {
7144 assert(type->id == ZigTypeIdOpaque || type_is_resolved(type, ResolveStatusSizeKnown));
7145 assert(wanted_resolve_status > ResolveStatusSizeKnown);
7146 switch (type->id) {
7147 case ZigTypeIdInvalid:
7148 case ZigTypeIdMetaType:
7149 case ZigTypeIdComptimeFloat:
7150 case ZigTypeIdComptimeInt:
7151 case ZigTypeIdEnumLiteral:
7152 case ZigTypeIdUndefined:
7153 case ZigTypeIdNull:
7154 case ZigTypeIdBoundFn:
7155 case ZigTypeIdArgTuple:
7156 zig_unreachable();
7157 case ZigTypeIdFloat:
7158 case ZigTypeIdOpaque:
7159 case ZigTypeIdVoid:
7160 case ZigTypeIdBool:
7161 case ZigTypeIdUnreachable:
7162 assert(type->llvm_di_type != nullptr);
7163 return;
7164 case ZigTypeIdStruct:
7165 if (type->data.structure.is_slice)
7166 return resolve_llvm_types_slice(g, type, wanted_resolve_status);
7167 else
7168 return resolve_llvm_types_struct(g, type, wanted_resolve_status);
7169 case ZigTypeIdEnum:
7170 return resolve_llvm_types_enum(g, type);
7171 case ZigTypeIdUnion:
7172 return resolve_llvm_types_union(g, type, wanted_resolve_status);
7173 case ZigTypeIdPointer:
7174 return resolve_llvm_types_pointer(g, type);
7175 case ZigTypeIdPromise: {
7176 if (type->llvm_di_type != nullptr) return;
7177 ZigType *u8_ptr_type = get_pointer_to_type(g, g->builtin_types.entry_u8, false);
7178 type->llvm_type = get_llvm_type(g, u8_ptr_type);
7179 type->llvm_di_type = get_llvm_di_type(g, u8_ptr_type);
7180 return;
7181 }
7182 case ZigTypeIdInt:
7183 return resolve_llvm_types_integer(g, type);
7184 case ZigTypeIdOptional:
7185 return resolve_llvm_types_optional(g, type);
7186 case ZigTypeIdErrorUnion:
7187 return resolve_llvm_types_error_union(g, type);
7188 case ZigTypeIdArray:
7189 return resolve_llvm_types_array(g, type);
7190 case ZigTypeIdFn:
7191 return resolve_llvm_types_fn(g, type);
7192 case ZigTypeIdErrorSet: {
7193 if (type->llvm_di_type != nullptr) return;
7194
7195 if (g->builtin_types.entry_global_error_set->llvm_type == nullptr) {
7196 resolve_llvm_types_anyerror(g);
7197 }
7198 type->llvm_type = g->builtin_types.entry_global_error_set->llvm_type;
7199 type->llvm_di_type = g->builtin_types.entry_global_error_set->llvm_di_type;
7200 return;
7201 }
7202 case ZigTypeIdVector: {
7203 if (type->llvm_di_type != nullptr) return;
7204
7205 type->llvm_type = LLVMVectorType(get_llvm_type(g, type->data.vector.elem_type), type->data.vector.len);
7206 type->llvm_di_type = ZigLLVMDIBuilderCreateVectorType(g->dbuilder, type->size_in_bits,
7207 type->abi_align, get_llvm_di_type(g, type->data.vector.elem_type), type->data.vector.len);
7208 return;
7209 }
7210 }
7211 zig_unreachable();
7212}
7213
7214LLVMTypeRef get_llvm_type(CodeGen *g, ZigType *type) {
7215 assertNoError(type_resolve(g, type, ResolveStatusLLVMFull));
7216 assert(type->abi_size == 0 || type->abi_size == LLVMABISizeOfType(g->target_data_ref, type->llvm_type));
7217 assert(type->abi_align == 0 || type->abi_align == LLVMABIAlignmentOfType(g->target_data_ref, type->llvm_type));
7218 return type->llvm_type;
7219}
7220
7221ZigLLVMDIType *get_llvm_di_type(CodeGen *g, ZigType *type) {
7222 assertNoError(type_resolve(g, type, ResolveStatusLLVMFull));
7223 return type->llvm_di_type;
7224}
src/analyze.hpp+4-1
...@@ -73,7 +73,7 @@ bool type_is_complete(ZigType *type_entry);...@@ -73,7 +73,7 @@ bool type_is_complete(ZigType *type_entry);
73bool type_is_resolved(ZigType *type_entry, ResolveStatus status);73bool type_is_resolved(ZigType *type_entry, ResolveStatus status);
74bool type_is_invalid(ZigType *type_entry);74bool type_is_invalid(ZigType *type_entry);
75bool type_is_global_error_set(ZigType *err_set_type);75bool type_is_global_error_set(ZigType *err_set_type);
76void resolve_container_type(CodeGen *g, ZigType *type_entry);76Error resolve_container_type(CodeGen *g, ZigType *type_entry);
77ScopeDecls *get_container_scope(ZigType *type_entry);77ScopeDecls *get_container_scope(ZigType *type_entry);
78TypeStructField *find_struct_type_field(ZigType *type_entry, Buf *name);78TypeStructField *find_struct_type_field(ZigType *type_entry, Buf *name);
79TypeEnumField *find_enum_type_field(ZigType *enum_type, Buf *name);79TypeEnumField *find_enum_type_field(ZigType *enum_type, Buf *name);
...@@ -244,4 +244,7 @@ Buf *type_bare_name(ZigType *t);...@@ -244,4 +244,7 @@ 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);
249
247#endif250#endif
src/codegen.cpp+351-346
...@@ -483,6 +483,8 @@ static LLVMValueRef fn_llvm_value(CodeGen *g, ZigFn *fn_table_entry) {...@@ -483,6 +483,8 @@ static LLVMValueRef fn_llvm_value(CodeGen *g, ZigFn *fn_table_entry) {
483483
484484
485 ZigType *fn_type = fn_table_entry->type_entry;485 ZigType *fn_type = fn_table_entry->type_entry;
486 // Make the raw_type_ref populated
487 (void)get_llvm_type(g, fn_type);
486 LLVMTypeRef fn_llvm_type = fn_type->data.fn.raw_type_ref;488 LLVMTypeRef fn_llvm_type = fn_type->data.fn.raw_type_ref;
487 if (fn_table_entry->body_node == nullptr) {489 if (fn_table_entry->body_node == nullptr) {
488 LLVMValueRef existing_llvm_fn = LLVMGetNamedFunction(g->module, buf_ptr(symbol_name));490 LLVMValueRef existing_llvm_fn = LLVMGetNamedFunction(g->module, buf_ptr(symbol_name));
...@@ -496,6 +498,8 @@ static LLVMValueRef fn_llvm_value(CodeGen *g, ZigFn *fn_table_entry) {...@@ -496,6 +498,8 @@ static LLVMValueRef fn_llvm_value(CodeGen *g, ZigFn *fn_table_entry) {
496 } else {498 } else {
497 assert(entry->value->id == TldIdFn);499 assert(entry->value->id == TldIdFn);
498 TldFn *tld_fn = reinterpret_cast<TldFn *>(entry->value);500 TldFn *tld_fn = reinterpret_cast<TldFn *>(entry->value);
501 // Make the raw_type_ref populated
502 (void)get_llvm_type(g, tld_fn->fn_entry->type_entry);
499 tld_fn->fn_entry->llvm_value = LLVMAddFunction(g->module, buf_ptr(symbol_name),503 tld_fn->fn_entry->llvm_value = LLVMAddFunction(g->module, buf_ptr(symbol_name),
500 tld_fn->fn_entry->type_entry->data.fn.raw_type_ref);504 tld_fn->fn_entry->type_entry->data.fn.raw_type_ref);
501 fn_table_entry->llvm_value = LLVMConstBitCast(tld_fn->fn_entry->llvm_value,505 fn_table_entry->llvm_value = LLVMConstBitCast(tld_fn->fn_entry->llvm_value,
...@@ -668,7 +672,7 @@ static ZigLLVMDIScope *get_di_scope(CodeGen *g, Scope *scope) {...@@ -668,7 +672,7 @@ static ZigLLVMDIScope *get_di_scope(CodeGen *g, Scope *scope) {
668 if (scope->parent) {672 if (scope->parent) {
669 ScopeDecls *decls_scope = (ScopeDecls *)scope;673 ScopeDecls *decls_scope = (ScopeDecls *)scope;
670 assert(decls_scope->container_type);674 assert(decls_scope->container_type);
671 scope->di_scope = ZigLLVMTypeToScope(decls_scope->container_type->di_type);675 scope->di_scope = ZigLLVMTypeToScope(get_llvm_di_type(g, decls_scope->container_type));
672 } else {676 } else {
673 scope->di_scope = ZigLLVMFileToScope(import->data.structure.root_struct->di_file);677 scope->di_scope = ZigLLVMFileToScope(import->data.structure.root_struct->di_file);
674 }678 }
...@@ -711,8 +715,8 @@ static LLVMValueRef get_arithmetic_overflow_fn(CodeGen *g, ZigType *operand_type...@@ -711,8 +715,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;715 const char *signed_str = int_type->data.integral.is_signed ? signed_name : unsigned_name;
712716
713 LLVMTypeRef param_types[] = {717 LLVMTypeRef param_types[] = {
714 operand_type->type_ref,718 get_llvm_type(g, operand_type),
715 operand_type->type_ref,719 get_llvm_type(g, operand_type),
716 };720 };
717721
718 if (operand_type->id == ZigTypeIdVector) {722 if (operand_type->id == ZigTypeIdVector) {
...@@ -720,7 +724,7 @@ static LLVMValueRef get_arithmetic_overflow_fn(CodeGen *g, ZigType *operand_type...@@ -720,7 +724,7 @@ static LLVMValueRef get_arithmetic_overflow_fn(CodeGen *g, ZigType *operand_type
720 operand_type->data.vector.len, int_type->data.integral.bit_count);724 operand_type->data.vector.len, int_type->data.integral.bit_count);
721725
722 LLVMTypeRef return_elem_types[] = {726 LLVMTypeRef return_elem_types[] = {
723 operand_type->type_ref,727 get_llvm_type(g, operand_type),
724 LLVMVectorType(LLVMInt1Type(), operand_type->data.vector.len),728 LLVMVectorType(LLVMInt1Type(), operand_type->data.vector.len),
725 };729 };
726 LLVMTypeRef return_struct_type = LLVMStructType(return_elem_types, 2, false);730 LLVMTypeRef return_struct_type = LLVMStructType(return_elem_types, 2, false);
...@@ -732,7 +736,7 @@ static LLVMValueRef get_arithmetic_overflow_fn(CodeGen *g, ZigType *operand_type...@@ -732,7 +736,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);736 sprintf(fn_name, "llvm.%s.with.overflow.i%" PRIu32, signed_str, int_type->data.integral.bit_count);
733737
734 LLVMTypeRef return_elem_types[] = {738 LLVMTypeRef return_elem_types[] = {
735 operand_type->type_ref,739 get_llvm_type(g, operand_type),
736 LLVMInt1Type(),740 LLVMInt1Type(),
737 };741 };
738 LLVMTypeRef return_struct_type = LLVMStructType(return_elem_types, 2, false);742 LLVMTypeRef return_struct_type = LLVMStructType(return_elem_types, 2, false);
...@@ -800,7 +804,8 @@ static LLVMValueRef get_float_fn(CodeGen *g, ZigType *type_entry, ZigLLVMFnId fn...@@ -800,7 +804,8 @@ static LLVMValueRef get_float_fn(CodeGen *g, ZigType *type_entry, ZigLLVMFnId fn
800804
801 char fn_name[64];805 char fn_name[64];
802 sprintf(fn_name, "llvm.%s.f%" ZIG_PRI_usize "", name, type_entry->data.floating.bit_count);806 sprintf(fn_name, "llvm.%s.f%" ZIG_PRI_usize "", name, type_entry->data.floating.bit_count);
803 LLVMTypeRef fn_type = LLVMFunctionType(type_entry->type_ref, &type_entry->type_ref, 1, false);807 LLVMTypeRef float_type_ref = get_llvm_type(g, type_entry);
808 LLVMTypeRef fn_type = LLVMFunctionType(float_type_ref, &float_type_ref, 1, false);
804 LLVMValueRef fn_val = LLVMAddFunction(g->module, fn_name, fn_type);809 LLVMValueRef fn_val = LLVMAddFunction(g->module, fn_name, fn_type);
805 assert(LLVMGetIntrinsicID(fn_val));810 assert(LLVMGetIntrinsicID(fn_val));
806811
...@@ -958,7 +963,7 @@ static LLVMValueRef get_panic_msg_ptr_val(CodeGen *g, PanicMsgId msg_id) {...@@ -958,7 +963,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,963 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);964 PtrLenUnknown, get_abi_alignment(g, g->builtin_types.entry_u8), 0, 0, false);
960 ZigType *str_type = get_slice_type(g, u8_ptr_type);965 ZigType *str_type = get_slice_type(g, u8_ptr_type);
961 return LLVMConstBitCast(val->global_refs->llvm_global, LLVMPointerType(str_type->type_ref, 0));966 return LLVMConstBitCast(val->global_refs->llvm_global, LLVMPointerType(get_llvm_type(g, str_type), 0));
962}967}
963968
964static void gen_panic(CodeGen *g, LLVMValueRef msg_arg, LLVMValueRef stack_trace_arg) {969static void gen_panic(CodeGen *g, LLVMValueRef msg_arg, LLVMValueRef stack_trace_arg) {
...@@ -967,7 +972,7 @@ static void gen_panic(CodeGen *g, LLVMValueRef msg_arg, LLVMValueRef stack_trace...@@ -967,7 +972,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);972 LLVMCallConv llvm_cc = get_llvm_cc(g, g->panic_fn->type_entry->data.fn.fn_type_id.cc);
968 if (stack_trace_arg == nullptr) {973 if (stack_trace_arg == nullptr) {
969 ZigType *ptr_to_stack_trace_type = get_ptr_to_stack_trace_type(g);974 ZigType *ptr_to_stack_trace_type = get_ptr_to_stack_trace_type(g);
970 stack_trace_arg = LLVMConstNull(ptr_to_stack_trace_type->type_ref);975 stack_trace_arg = LLVMConstNull(get_llvm_type(g, ptr_to_stack_trace_type));
971 }976 }
972 LLVMValueRef args[] = {977 LLVMValueRef args[] = {
973 msg_arg,978 msg_arg,
...@@ -1081,7 +1086,7 @@ static LLVMValueRef get_coro_size_fn_val(CodeGen *g) {...@@ -1081,7 +1086,7 @@ static LLVMValueRef get_coro_size_fn_val(CodeGen *g) {
1081 if (g->coro_size_fn_val)1086 if (g->coro_size_fn_val)
1082 return g->coro_size_fn_val;1087 return g->coro_size_fn_val;
10831088
1084 LLVMTypeRef fn_type = LLVMFunctionType(g->builtin_types.entry_usize->type_ref, nullptr, 0, false);1089 LLVMTypeRef fn_type = LLVMFunctionType(g->builtin_types.entry_usize->llvm_type, nullptr, 0, false);
1085 Buf *name = buf_sprintf("llvm.coro.size.i%d", g->pointer_size_bytes * 8);1090 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);1091 g->coro_size_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type);
1087 assert(LLVMGetIntrinsicID(g->coro_size_fn_val));1092 assert(LLVMGetIntrinsicID(g->coro_size_fn_val));
...@@ -1206,8 +1211,8 @@ static LLVMValueRef get_return_address_fn_val(CodeGen *g) {...@@ -1206,8 +1211,8 @@ static LLVMValueRef get_return_address_fn_val(CodeGen *g) {
12061211
1207 ZigType *return_type = get_pointer_to_type(g, g->builtin_types.entry_u8, true);1212 ZigType *return_type = get_pointer_to_type(g, g->builtin_types.entry_u8, true);
12081213
1209 LLVMTypeRef fn_type = LLVMFunctionType(return_type->type_ref,1214 LLVMTypeRef fn_type = LLVMFunctionType(get_llvm_type(g, return_type),
1210 &g->builtin_types.entry_i32->type_ref, 1, false);1215 &g->builtin_types.entry_i32->llvm_type, 1, false);
1211 g->return_address_fn_val = LLVMAddFunction(g->module, "llvm.returnaddress", fn_type);1216 g->return_address_fn_val = LLVMAddFunction(g->module, "llvm.returnaddress", fn_type);
1212 assert(LLVMGetIntrinsicID(g->return_address_fn_val));1217 assert(LLVMGetIntrinsicID(g->return_address_fn_val));
12131218
...@@ -1219,8 +1224,8 @@ static LLVMValueRef get_add_error_return_trace_addr_fn(CodeGen *g) {...@@ -1219,8 +1224,8 @@ static LLVMValueRef get_add_error_return_trace_addr_fn(CodeGen *g) {
1219 return g->add_error_return_trace_addr_fn_val;1224 return g->add_error_return_trace_addr_fn_val;
12201225
1221 LLVMTypeRef arg_types[] = {1226 LLVMTypeRef arg_types[] = {
1222 get_ptr_to_stack_trace_type(g)->type_ref,1227 get_llvm_type(g, get_ptr_to_stack_trace_type(g)),
1223 g->builtin_types.entry_usize->type_ref,1228 g->builtin_types.entry_usize->llvm_type,
1224 };1229 };
1225 LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMVoidType(), arg_types, 2, false);1230 LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMVoidType(), arg_types, 2, false);
12261231
...@@ -1245,7 +1250,7 @@ static LLVMValueRef get_add_error_return_trace_addr_fn(CodeGen *g) {...@@ -1245,7 +1250,7 @@ static LLVMValueRef get_add_error_return_trace_addr_fn(CodeGen *g) {
1245 LLVMPositionBuilderAtEnd(g->builder, entry_block);1250 LLVMPositionBuilderAtEnd(g->builder, entry_block);
1246 ZigLLVMClearCurrentDebugLocation(g->builder);1251 ZigLLVMClearCurrentDebugLocation(g->builder);
12471252
1248 LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->type_ref;1253 LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->llvm_type;
12491254
1250 // stack_trace.instruction_addresses[stack_trace.index & (stack_trace.instruction_addresses.len - 1)] = return_address;1255 // stack_trace.instruction_addresses[stack_trace.index & (stack_trace.instruction_addresses.len - 1)] = return_address;
12511256
...@@ -1297,8 +1302,8 @@ static LLVMValueRef get_merge_err_ret_traces_fn_val(CodeGen *g) {...@@ -1297,8 +1302,8 @@ static LLVMValueRef get_merge_err_ret_traces_fn_val(CodeGen *g) {
1297 assert(g->stack_trace_type != nullptr);1302 assert(g->stack_trace_type != nullptr);
12981303
1299 LLVMTypeRef param_types[] = {1304 LLVMTypeRef param_types[] = {
1300 get_ptr_to_stack_trace_type(g)->type_ref,1305 get_llvm_type(g, get_ptr_to_stack_trace_type(g)),
1301 get_ptr_to_stack_trace_type(g)->type_ref,1306 get_llvm_type(g, get_ptr_to_stack_trace_type(g)),
1302 };1307 };
1303 LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMVoidType(), param_types, 2, false);1308 LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMVoidType(), param_types, 2, false);
13041309
...@@ -1350,8 +1355,8 @@ static LLVMValueRef get_merge_err_ret_traces_fn_val(CodeGen *g) {...@@ -1350,8 +1355,8 @@ static LLVMValueRef get_merge_err_ret_traces_fn_val(CodeGen *g) {
1350 // }1355 // }
1351 LLVMBasicBlockRef return_block = LLVMAppendBasicBlock(fn_val, "Return");1356 LLVMBasicBlockRef return_block = LLVMAppendBasicBlock(fn_val, "Return");
13521357
1353 LLVMValueRef frame_index_ptr = LLVMBuildAlloca(g->builder, g->builtin_types.entry_usize->type_ref, "frame_index");1358 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");1359 LLVMValueRef frames_left_ptr = LLVMBuildAlloca(g->builder, g->builtin_types.entry_usize->llvm_type, "frames_left");
13551360
1356 LLVMValueRef dest_stack_trace_ptr = LLVMGetParam(fn_val, 0);1361 LLVMValueRef dest_stack_trace_ptr = LLVMGetParam(fn_val, 0);
1357 LLVMValueRef src_stack_trace_ptr = LLVMGetParam(fn_val, 1);1362 LLVMValueRef src_stack_trace_ptr = LLVMGetParam(fn_val, 1);
...@@ -1377,14 +1382,14 @@ static LLVMValueRef get_merge_err_ret_traces_fn_val(CodeGen *g) {...@@ -1377,14 +1382,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);1382 LLVMBuildCondBr(g->builder, no_wrap_bit, no_wrap_block, yes_wrap_block);
13781383
1379 LLVMPositionBuilderAtEnd(g->builder, no_wrap_block);1384 LLVMPositionBuilderAtEnd(g->builder, no_wrap_block);
1380 LLVMValueRef usize_zero = LLVMConstNull(g->builtin_types.entry_usize->type_ref);1385 LLVMValueRef usize_zero = LLVMConstNull(g->builtin_types.entry_usize->llvm_type);
1381 LLVMBuildStore(g->builder, usize_zero, frame_index_ptr);1386 LLVMBuildStore(g->builder, usize_zero, frame_index_ptr);
1382 LLVMBuildStore(g->builder, src_index_val, frames_left_ptr);1387 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, "");1388 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);1389 LLVMBuildCondBr(g->builder, frames_left_eq_zero_bit, return_block, loop_block);
13851390
1386 LLVMPositionBuilderAtEnd(g->builder, yes_wrap_block);1391 LLVMPositionBuilderAtEnd(g->builder, yes_wrap_block);
1387 LLVMValueRef usize_one = LLVMConstInt(g->builtin_types.entry_usize->type_ref, 1, false);1392 LLVMValueRef usize_one = LLVMConstInt(g->builtin_types.entry_usize->llvm_type, 1, false);
1388 LLVMValueRef plus_one = LLVMBuildNUWAdd(g->builder, src_index_val, usize_one, "");1393 LLVMValueRef plus_one = LLVMBuildNUWAdd(g->builder, src_index_val, usize_one, "");
1389 LLVMValueRef mod_len = LLVMBuildURem(g->builder, plus_one, src_len_val, "");1394 LLVMValueRef mod_len = LLVMBuildURem(g->builder, plus_one, src_len_val, "");
1390 LLVMBuildStore(g->builder, mod_len, frame_index_ptr);1395 LLVMBuildStore(g->builder, mod_len, frame_index_ptr);
...@@ -1430,7 +1435,7 @@ static LLVMValueRef get_return_err_fn(CodeGen *g) {...@@ -1430,7 +1435,7 @@ static LLVMValueRef get_return_err_fn(CodeGen *g) {
14301435
1431 LLVMTypeRef arg_types[] = {1436 LLVMTypeRef arg_types[] = {
1432 // error return trace pointer1437 // error return trace pointer
1433 get_ptr_to_stack_trace_type(g)->type_ref,1438 get_llvm_type(g, get_ptr_to_stack_trace_type(g)),
1434 };1439 };
1435 LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMVoidType(), arg_types, 1, false);1440 LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMVoidType(), arg_types, 1, false);
14361441
...@@ -1461,8 +1466,8 @@ static LLVMValueRef get_return_err_fn(CodeGen *g) {...@@ -1461,8 +1466,8 @@ static LLVMValueRef get_return_err_fn(CodeGen *g) {
14611466
1462 LLVMValueRef err_ret_trace_ptr = LLVMGetParam(fn_val, 0);1467 LLVMValueRef err_ret_trace_ptr = LLVMGetParam(fn_val, 0);
14631468
1464 LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->type_ref;1469 LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->llvm_type;
1465 LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_i32->type_ref);1470 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, "");1471 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, "");1472 LLVMValueRef return_address = LLVMBuildPtrToInt(g->builder, return_address_ptr, usize_type_ref, "");
14681473
...@@ -1508,8 +1513,8 @@ static LLVMValueRef get_safety_crash_err_fn(CodeGen *g) {...@@ -1508,8 +1513,8 @@ static LLVMValueRef get_safety_crash_err_fn(CodeGen *g) {
15081513
1509 ZigType *usize = g->builtin_types.entry_usize;1514 ZigType *usize = g->builtin_types.entry_usize;
1510 LLVMValueRef full_buf_ptr_indices[] = {1515 LLVMValueRef full_buf_ptr_indices[] = {
1511 LLVMConstNull(usize->type_ref),1516 LLVMConstNull(usize->llvm_type),
1512 LLVMConstNull(usize->type_ref),1517 LLVMConstNull(usize->llvm_type),
1513 };1518 };
1514 LLVMValueRef full_buf_ptr = LLVMConstInBoundsGEP(global_array, full_buf_ptr_indices, 2);1519 LLVMValueRef full_buf_ptr = LLVMConstInBoundsGEP(global_array, full_buf_ptr_indices, 2);
15151520
...@@ -1519,9 +1524,9 @@ static LLVMValueRef get_safety_crash_err_fn(CodeGen *g) {...@@ -1519,9 +1524,9 @@ static LLVMValueRef get_safety_crash_err_fn(CodeGen *g) {
1519 ZigType *str_type = get_slice_type(g, u8_ptr_type);1524 ZigType *str_type = get_slice_type(g, u8_ptr_type);
1520 LLVMValueRef global_slice_fields[] = {1525 LLVMValueRef global_slice_fields[] = {
1521 full_buf_ptr,1526 full_buf_ptr,
1522 LLVMConstNull(usize->type_ref),1527 LLVMConstNull(usize->llvm_type),
1523 };1528 };
1524 LLVMValueRef slice_init_value = LLVMConstNamedStruct(str_type->type_ref, global_slice_fields, 2);1529 LLVMValueRef slice_init_value = LLVMConstNamedStruct(get_llvm_type(g, str_type), global_slice_fields, 2);
1525 LLVMValueRef global_slice = LLVMAddGlobal(g->module, LLVMTypeOf(slice_init_value), "");1530 LLVMValueRef global_slice = LLVMAddGlobal(g->module, LLVMTypeOf(slice_init_value), "");
1526 LLVMSetInitializer(global_slice, slice_init_value);1531 LLVMSetInitializer(global_slice, slice_init_value);
1527 LLVMSetLinkage(global_slice, LLVMInternalLinkage);1532 LLVMSetLinkage(global_slice, LLVMInternalLinkage);
...@@ -1530,15 +1535,15 @@ static LLVMValueRef get_safety_crash_err_fn(CodeGen *g) {...@@ -1530,15 +1535,15 @@ static LLVMValueRef get_safety_crash_err_fn(CodeGen *g) {
1530 LLVMSetAlignment(global_slice, get_abi_alignment(g, str_type));1535 LLVMSetAlignment(global_slice, get_abi_alignment(g, str_type));
15311536
1532 LLVMValueRef offset_ptr_indices[] = {1537 LLVMValueRef offset_ptr_indices[] = {
1533 LLVMConstNull(usize->type_ref),1538 LLVMConstNull(usize->llvm_type),
1534 LLVMConstInt(usize->type_ref, unwrap_err_msg_text_len, false),1539 LLVMConstInt(usize->llvm_type, unwrap_err_msg_text_len, false),
1535 };1540 };
1536 LLVMValueRef offset_buf_ptr = LLVMConstInBoundsGEP(global_array, offset_ptr_indices, 2);1541 LLVMValueRef offset_buf_ptr = LLVMConstInBoundsGEP(global_array, offset_ptr_indices, 2);
15371542
1538 Buf *fn_name = get_mangled_name(g, buf_create_from_str("__zig_fail_unwrap"), false);1543 Buf *fn_name = get_mangled_name(g, buf_create_from_str("__zig_fail_unwrap"), false);
1539 LLVMTypeRef arg_types[] = {1544 LLVMTypeRef arg_types[] = {
1540 g->ptr_to_stack_trace_type->type_ref,1545 get_llvm_type(g, g->ptr_to_stack_trace_type),
1541 g->err_tag_type->type_ref,1546 get_llvm_type(g, g->err_tag_type),
1542 };1547 };
1543 LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMVoidType(), arg_types, 2, false);1548 LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMVoidType(), arg_types, 2, false);
1544 LLVMValueRef fn_val = LLVMAddFunction(g->module, buf_ptr(fn_name), fn_type_ref);1549 LLVMValueRef fn_val = LLVMAddFunction(g->module, buf_ptr(fn_name), fn_type_ref);
...@@ -1565,7 +1570,7 @@ static LLVMValueRef get_safety_crash_err_fn(CodeGen *g) {...@@ -1565,7 +1570,7 @@ static LLVMValueRef get_safety_crash_err_fn(CodeGen *g) {
1565 LLVMValueRef err_val = LLVMGetParam(fn_val, 1);1570 LLVMValueRef err_val = LLVMGetParam(fn_val, 1);
15661571
1567 LLVMValueRef err_table_indices[] = {1572 LLVMValueRef err_table_indices[] = {
1568 LLVMConstNull(g->builtin_types.entry_usize->type_ref),1573 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),
1569 err_val,1574 err_val,
1570 };1575 };
1571 LLVMValueRef err_name_val = LLVMBuildInBoundsGEP(g->builder, g->err_name_table, err_table_indices, 2, "");1576 LLVMValueRef err_name_val = LLVMBuildInBoundsGEP(g->builder, g->err_name_table, err_table_indices, 2, "");
...@@ -1623,7 +1628,7 @@ static void gen_safety_crash_for_err(CodeGen *g, LLVMValueRef err_val, Scope *sc...@@ -1623,7 +1628,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);1628 LLVMValueRef err_ret_trace_val = get_cur_err_ret_trace_val(g, scope);
1624 if (err_ret_trace_val == nullptr) {1629 if (err_ret_trace_val == nullptr) {
1625 ZigType *ptr_to_stack_trace_type = get_ptr_to_stack_trace_type(g);1630 ZigType *ptr_to_stack_trace_type = get_ptr_to_stack_trace_type(g);
1626 err_ret_trace_val = LLVMConstNull(ptr_to_stack_trace_type->type_ref);1631 err_ret_trace_val = LLVMConstNull(get_llvm_type(g, ptr_to_stack_trace_type));
1627 }1632 }
1628 LLVMValueRef args[] = {1633 LLVMValueRef args[] = {
1629 err_ret_trace_val,1634 err_ret_trace_val,
...@@ -1669,7 +1674,7 @@ static void add_bounds_check(CodeGen *g, LLVMValueRef target_val,...@@ -1669,7 +1674,7 @@ static void add_bounds_check(CodeGen *g, LLVMValueRef target_val,
1669}1674}
16701675
1671static LLVMValueRef gen_assert_zero(CodeGen *g, LLVMValueRef expr_val, ZigType *int_type) {1676static LLVMValueRef gen_assert_zero(CodeGen *g, LLVMValueRef expr_val, ZigType *int_type) {
1672 LLVMValueRef zero = LLVMConstNull(int_type->type_ref);1677 LLVMValueRef zero = LLVMConstNull(get_llvm_type(g, int_type));
1673 LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, expr_val, zero, "");1678 LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, expr_val, zero, "");
1674 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "CastShortenOk");1679 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "CastShortenOk");
1675 LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn_val, "CastShortenFail");1680 LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn_val, "CastShortenFail");
...@@ -1704,7 +1709,7 @@ static LLVMValueRef gen_widen_or_shorten(CodeGen *g, bool want_runtime_safety, Z...@@ -1704,7 +1709,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 &&1709 !wanted_type->data.integral.is_signed && actual_type->data.integral.is_signed &&
1705 want_runtime_safety)1710 want_runtime_safety)
1706 {1711 {
1707 LLVMValueRef zero = LLVMConstNull(actual_type->type_ref);1712 LLVMValueRef zero = LLVMConstNull(get_llvm_type(g, actual_type));
1708 LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntSGE, expr_val, zero, "");1713 LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntSGE, expr_val, zero, "");
17091714
1710 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "SignCastOk");1715 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "SignCastOk");
...@@ -1721,19 +1726,19 @@ static LLVMValueRef gen_widen_or_shorten(CodeGen *g, bool want_runtime_safety, Z...@@ -1721,19 +1726,19 @@ static LLVMValueRef gen_widen_or_shorten(CodeGen *g, bool want_runtime_safety, Z
1721 return expr_val;1726 return expr_val;
1722 } else if (actual_bits < wanted_bits) {1727 } else if (actual_bits < wanted_bits) {
1723 if (actual_type->id == ZigTypeIdFloat) {1728 if (actual_type->id == ZigTypeIdFloat) {
1724 return LLVMBuildFPExt(g->builder, expr_val, wanted_type->type_ref, "");1729 return LLVMBuildFPExt(g->builder, expr_val, get_llvm_type(g, wanted_type), "");
1725 } else if (actual_type->id == ZigTypeIdInt) {1730 } else if (actual_type->id == ZigTypeIdInt) {
1726 if (actual_type->data.integral.is_signed) {1731 if (actual_type->data.integral.is_signed) {
1727 return LLVMBuildSExt(g->builder, expr_val, wanted_type->type_ref, "");1732 return LLVMBuildSExt(g->builder, expr_val, get_llvm_type(g, wanted_type), "");
1728 } else {1733 } else {
1729 return LLVMBuildZExt(g->builder, expr_val, wanted_type->type_ref, "");1734 return LLVMBuildZExt(g->builder, expr_val, get_llvm_type(g, wanted_type), "");
1730 }1735 }
1731 } else {1736 } else {
1732 zig_unreachable();1737 zig_unreachable();
1733 }1738 }
1734 } else if (actual_bits > wanted_bits) {1739 } else if (actual_bits > wanted_bits) {
1735 if (actual_type->id == ZigTypeIdFloat) {1740 if (actual_type->id == ZigTypeIdFloat) {
1736 return LLVMBuildFPTrunc(g->builder, expr_val, wanted_type->type_ref, "");1741 return LLVMBuildFPTrunc(g->builder, expr_val, get_llvm_type(g, wanted_type), "");
1737 } else if (actual_type->id == ZigTypeIdInt) {1742 } else if (actual_type->id == ZigTypeIdInt) {
1738 if (wanted_bits == 0) {1743 if (wanted_bits == 0) {
1739 if (!want_runtime_safety)1744 if (!want_runtime_safety)
...@@ -1741,15 +1746,15 @@ static LLVMValueRef gen_widen_or_shorten(CodeGen *g, bool want_runtime_safety, Z...@@ -1741,15 +1746,15 @@ static LLVMValueRef gen_widen_or_shorten(CodeGen *g, bool want_runtime_safety, Z
17411746
1742 return gen_assert_zero(g, expr_val, actual_type);1747 return gen_assert_zero(g, expr_val, actual_type);
1743 }1748 }
1744 LLVMValueRef trunc_val = LLVMBuildTrunc(g->builder, expr_val, wanted_type->type_ref, "");1749 LLVMValueRef trunc_val = LLVMBuildTrunc(g->builder, expr_val, get_llvm_type(g, wanted_type), "");
1745 if (!want_runtime_safety) {1750 if (!want_runtime_safety) {
1746 return trunc_val;1751 return trunc_val;
1747 }1752 }
1748 LLVMValueRef orig_val;1753 LLVMValueRef orig_val;
1749 if (wanted_type->data.integral.is_signed) {1754 if (wanted_type->data.integral.is_signed) {
1750 orig_val = LLVMBuildSExt(g->builder, trunc_val, actual_type->type_ref, "");1755 orig_val = LLVMBuildSExt(g->builder, trunc_val, get_llvm_type(g, actual_type), "");
1751 } else {1756 } else {
1752 orig_val = LLVMBuildZExt(g->builder, trunc_val, actual_type->type_ref, "");1757 orig_val = LLVMBuildZExt(g->builder, trunc_val, get_llvm_type(g, actual_type), "");
1753 }1758 }
1754 LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, expr_val, orig_val, "");1759 LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, expr_val, orig_val, "");
1755 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "CastShortenOk");1760 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "CastShortenOk");
...@@ -1793,7 +1798,7 @@ static LLVMValueRef gen_overflow_op(CodeGen *g, ZigType *operand_type, AddSubMul...@@ -1793,7 +1798,7 @@ static LLVMValueRef gen_overflow_op(CodeGen *g, ZigType *operand_type, AddSubMul
1793 LLVMValueRef extended1 = buildExtFn(g->builder, val1, one_more_bit_int_vector, "");1798 LLVMValueRef extended1 = buildExtFn(g->builder, val1, one_more_bit_int_vector, "");
1794 LLVMValueRef extended2 = buildExtFn(g->builder, val2, one_more_bit_int_vector, "");1799 LLVMValueRef extended2 = buildExtFn(g->builder, val2, one_more_bit_int_vector, "");
1795 LLVMValueRef extended_result = wrap_op[op](g->builder, extended1, extended2, "");1800 LLVMValueRef extended_result = wrap_op[op](g->builder, extended1, extended2, "");
1796 result = LLVMBuildTrunc(g->builder, extended_result, operand_type->type_ref, "");1801 result = LLVMBuildTrunc(g->builder, extended_result, get_llvm_type(g, operand_type), "");
17971802
1798 LLVMValueRef re_extended_result = buildExtFn(g->builder, result, one_more_bit_int_vector, "");1803 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, "");1804 LLVMValueRef overflow_vector = LLVMBuildICmp(g->builder, LLVMIntNE, extended_result, re_extended_result, "");
...@@ -1880,12 +1885,13 @@ static LLVMValueRef gen_assign_raw(CodeGen *g, LLVMValueRef ptr, ZigType *ptr_ty...@@ -1880,12 +1885,13 @@ static LLVMValueRef gen_assign_raw(CodeGen *g, LLVMValueRef ptr, ZigType *ptr_ty
1880 LLVMValueRef dest_ptr = LLVMBuildBitCast(g->builder, ptr, ptr_u8, "");1885 LLVMValueRef dest_ptr = LLVMBuildBitCast(g->builder, ptr, ptr_u8, "");
18811886
1882 ZigType *usize = g->builtin_types.entry_usize;1887 ZigType *usize = g->builtin_types.entry_usize;
1883 uint64_t size_bytes = LLVMStoreSizeOfType(g->target_data_ref, child_type->type_ref);1888 uint64_t size_bytes = LLVMStoreSizeOfType(g->target_data_ref, get_llvm_type(g, child_type));
1884 uint64_t align_bytes = get_ptr_align(g, ptr_type);1889 uint64_t align_bytes = get_ptr_align(g, ptr_type);
1885 assert(size_bytes > 0);1890 assert(size_bytes > 0);
1886 assert(align_bytes > 0);1891 assert(align_bytes > 0);
18871892
1888 ZigLLVMBuildMemCpy(g->builder, dest_ptr, align_bytes, src_ptr, align_bytes, LLVMConstInt(usize->type_ref, size_bytes, false),1893 ZigLLVMBuildMemCpy(g->builder, dest_ptr, align_bytes, src_ptr, align_bytes,
1894 LLVMConstInt(usize->llvm_type, size_bytes, false),
1889 ptr_type->data.pointer.is_volatile);1895 ptr_type->data.pointer.is_volatile);
1890 return nullptr;1896 return nullptr;
1891 }1897 }
...@@ -1907,7 +1913,7 @@ static LLVMValueRef gen_assign_raw(CodeGen *g, LLVMValueRef ptr, ZigType *ptr_ty...@@ -1907,7 +1913,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;1913 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);1914 LLVMValueRef shift_amt_val = LLVMConstInt(LLVMTypeOf(containing_int), shift_amt, false);
19091915
1910 LLVMValueRef mask_val = LLVMConstAllOnes(child_type->type_ref);1916 LLVMValueRef mask_val = LLVMConstAllOnes(get_llvm_type(g, child_type));
1911 mask_val = LLVMConstZExt(mask_val, LLVMTypeOf(containing_int));1917 mask_val = LLVMConstZExt(mask_val, LLVMTypeOf(containing_int));
1912 mask_val = LLVMConstShl(mask_val, shift_amt_val);1918 mask_val = LLVMConstShl(mask_val, shift_amt_val);
1913 mask_val = LLVMConstNot(mask_val);1919 mask_val = LLVMConstNot(mask_val);
...@@ -1942,9 +1948,10 @@ static LLVMValueRef ir_llvm_value(CodeGen *g, IrInstruction *instruction) {...@@ -1942,9 +1948,10 @@ static LLVMValueRef ir_llvm_value(CodeGen *g, IrInstruction *instruction) {
1942 if (handle_is_ptr(instruction->value.type)) {1948 if (handle_is_ptr(instruction->value.type)) {
1943 render_const_val_global(g, &instruction->value, "");1949 render_const_val_global(g, &instruction->value, "");
1944 ZigType *ptr_type = get_pointer_to_type(g, instruction->value.type, true);1950 ZigType *ptr_type = get_pointer_to_type(g, instruction->value.type, true);
1945 instruction->llvm_value = LLVMBuildBitCast(g->builder, instruction->value.global_refs->llvm_global, ptr_type->type_ref, "");1951 instruction->llvm_value = LLVMBuildBitCast(g->builder, instruction->value.global_refs->llvm_global, get_llvm_type(g, ptr_type), "");
1946 } else if (instruction->value.type->id == ZigTypeIdPointer) {1952 } else if (instruction->value.type->id == ZigTypeIdPointer) {
1947 instruction->llvm_value = LLVMBuildBitCast(g->builder, instruction->value.global_refs->llvm_value, instruction->value.type->type_ref, "");1953 instruction->llvm_value = LLVMBuildBitCast(g->builder, instruction->value.global_refs->llvm_value,
1954 get_llvm_type(g, instruction->value.type), "");
1948 } else {1955 } else {
1949 instruction->llvm_value = instruction->value.global_refs->llvm_value;1956 instruction->llvm_value = instruction->value.global_refs->llvm_value;
1950 }1957 }
...@@ -1979,7 +1986,7 @@ static void give_up_with_c_abi_error(CodeGen *g, AstNode *source_node) {...@@ -1979,7 +1986,7 @@ static void give_up_with_c_abi_error(CodeGen *g, AstNode *source_node) {
1979}1986}
19801987
1981static LLVMValueRef build_alloca(CodeGen *g, ZigType *type_entry, const char *name, uint32_t alignment) {1988static LLVMValueRef build_alloca(CodeGen *g, ZigType *type_entry, const char *name, uint32_t alignment) {
1982 LLVMValueRef result = LLVMBuildAlloca(g->builder, type_entry->type_ref, name);1989 LLVMValueRef result = LLVMBuildAlloca(g->builder, get_llvm_type(g, type_entry), name);
1983 LLVMSetAlignment(result, (alignment == 0) ? get_abi_alignment(g, type_entry) : alignment);1990 LLVMSetAlignment(result, (alignment == 0) ? get_abi_alignment(g, type_entry) : alignment);
1984 return result;1991 return result;
1985}1992}
...@@ -2062,8 +2069,8 @@ static bool iter_function_params_c_abi(CodeGen *g, ZigType *fn_type, FnWalk *fn_...@@ -2062,8 +2069,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);2069 fn_walk->data.call.gen_param_values->append(val);
2063 break;2070 break;
2064 case FnWalkIdTypes:2071 case FnWalkIdTypes:
2065 fn_walk->data.types.gen_param_types->append(ty->type_ref);2072 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);2073 fn_walk->data.types.param_di_types->append(get_llvm_di_type(g, ty));
2067 break;2074 break;
2068 case FnWalkIdVars: {2075 case FnWalkIdVars: {
2069 var->value_ref = build_alloca(g, ty, buf_ptr(&var->name), var->align_bytes);2076 var->value_ref = build_alloca(g, ty, buf_ptr(&var->name), var->align_bytes);
...@@ -2099,8 +2106,8 @@ static bool iter_function_params_c_abi(CodeGen *g, ZigType *fn_type, FnWalk *fn_...@@ -2099,8 +2106,8 @@ static bool iter_function_params_c_abi(CodeGen *g, ZigType *fn_type, FnWalk *fn_
2099 break;2106 break;
2100 case FnWalkIdTypes: {2107 case FnWalkIdTypes: {
2101 ZigType *gen_type = get_pointer_to_type(g, ty, true);2108 ZigType *gen_type = get_pointer_to_type(g, ty, true);
2102 fn_walk->data.types.gen_param_types->append(gen_type->type_ref);2109 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);2110 fn_walk->data.types.param_di_types->append(get_llvm_di_type(g, gen_type));
2104 break;2111 break;
2105 }2112 }
2106 case FnWalkIdVars: {2113 case FnWalkIdVars: {
...@@ -2138,8 +2145,8 @@ static bool iter_function_params_c_abi(CodeGen *g, ZigType *fn_type, FnWalk *fn_...@@ -2138,8 +2145,8 @@ static bool iter_function_params_c_abi(CodeGen *g, ZigType *fn_type, FnWalk *fn_
2138 break;2145 break;
2139 case FnWalkIdTypes: {2146 case FnWalkIdTypes: {
2140 ZigType *gen_type = get_pointer_to_type(g, ty, true);2147 ZigType *gen_type = get_pointer_to_type(g, ty, true);
2141 fn_walk->data.types.gen_param_types->append(gen_type->type_ref);2148 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);2149 fn_walk->data.types.param_di_types->append(get_llvm_di_type(g, gen_type));
2143 break;2150 break;
2144 }2151 }
2145 case FnWalkIdVars: {2152 case FnWalkIdVars: {
...@@ -2171,8 +2178,8 @@ static bool iter_function_params_c_abi(CodeGen *g, ZigType *fn_type, FnWalk *fn_...@@ -2171,8 +2178,8 @@ static bool iter_function_params_c_abi(CodeGen *g, ZigType *fn_type, FnWalk *fn_
2171 }2178 }
2172 case FnWalkIdTypes: {2179 case FnWalkIdTypes: {
2173 ZigType *gen_type = get_int_type(g, false, ty_size * 8);2180 ZigType *gen_type = get_int_type(g, false, ty_size * 8);
2174 fn_walk->data.types.gen_param_types->append(gen_type->type_ref);2181 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);2182 fn_walk->data.types.param_di_types->append(get_llvm_di_type(g, gen_type));
2176 break;2183 break;
2177 }2184 }
2178 case FnWalkIdVars: {2185 case FnWalkIdVars: {
...@@ -2210,7 +2217,7 @@ var_ok:...@@ -2210,7 +2217,7 @@ var_ok:
2210 var->di_loc_var = ZigLLVMCreateParameterVariable(g->dbuilder, get_di_scope(g, var->parent_scope),2217 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,2218 buf_ptr(&var->name), fn_walk->data.vars.import->data.structure.root_struct->di_file,
2212 (unsigned)(var->decl_node->line + 1),2219 (unsigned)(var->decl_node->line + 1),
2213 dest_ty->di_type, !g->strip_debug_symbols, 0, di_arg_index + 1);2220 get_llvm_di_type(g, dest_ty), !g->strip_debug_symbols, 0, di_arg_index + 1);
2214 }2221 }
2215 return true;2222 return true;
2216}2223}
...@@ -2282,7 +2289,9 @@ void walk_function_params(CodeGen *g, ZigType *fn_type, FnWalk *fn_walk) {...@@ -2282,7 +2289,9 @@ void walk_function_params(CodeGen *g, ZigType *fn_type, FnWalk *fn_walk) {
22822289
2283 if (!handle_is_ptr(variable->var_type)) {2290 if (!handle_is_ptr(variable->var_type)) {
2284 clear_debug_source_node(g);2291 clear_debug_source_node(g);
2285 gen_store_untyped(g, LLVMGetParam(llvm_fn, (unsigned)variable->gen_arg_index),2292 ZigType *fn_type = fn_table_entry->type_entry;
2293 unsigned gen_arg_index = fn_type->data.fn.gen_param_info[variable->src_arg_index].gen_index;
2294 gen_store_untyped(g, LLVMGetParam(llvm_fn, gen_arg_index),
2286 variable->value_ref, variable->align_bytes, false);2295 variable->value_ref, variable->align_bytes, false);
2287 }2296 }
22882297
...@@ -2436,7 +2445,7 @@ static LLVMValueRef gen_div(CodeGen *g, bool want_runtime_safety, bool want_fast...@@ -2436,7 +2445,7 @@ static LLVMValueRef gen_div(CodeGen *g, bool want_runtime_safety, bool want_fast
2436{2445{
2437 ZigLLVMSetFastMath(g->builder, want_fast_math);2446 ZigLLVMSetFastMath(g->builder, want_fast_math);
24382447
2439 LLVMValueRef zero = LLVMConstNull(type_entry->type_ref);2448 LLVMValueRef zero = LLVMConstNull(get_llvm_type(g, type_entry));
2440 if (want_runtime_safety && (want_fast_math || type_entry->id != ZigTypeIdFloat)) {2449 if (want_runtime_safety && (want_fast_math || type_entry->id != ZigTypeIdFloat)) {
2441 LLVMValueRef is_zero_bit;2450 LLVMValueRef is_zero_bit;
2442 if (type_entry->id == ZigTypeIdInt) {2451 if (type_entry->id == ZigTypeIdInt) {
...@@ -2456,10 +2465,10 @@ static LLVMValueRef gen_div(CodeGen *g, bool want_runtime_safety, bool want_fast...@@ -2456,10 +2465,10 @@ static LLVMValueRef gen_div(CodeGen *g, bool want_runtime_safety, bool want_fast
2456 LLVMPositionBuilderAtEnd(g->builder, div_zero_ok_block);2465 LLVMPositionBuilderAtEnd(g->builder, div_zero_ok_block);
24572466
2458 if (type_entry->id == ZigTypeIdInt && type_entry->data.integral.is_signed) {2467 if (type_entry->id == ZigTypeIdInt && type_entry->data.integral.is_signed) {
2459 LLVMValueRef neg_1_value = LLVMConstInt(type_entry->type_ref, -1, true);2468 LLVMValueRef neg_1_value = LLVMConstInt(get_llvm_type(g, type_entry), -1, true);
2460 BigInt int_min_bi = {0};2469 BigInt int_min_bi = {0};
2461 eval_min_max_value_int(g, type_entry, &int_min_bi, false);2470 eval_min_max_value_int(g, type_entry, &int_min_bi, false);
2462 LLVMValueRef int_min_value = bigint_to_llvm_const(type_entry->type_ref, &int_min_bi);2471 LLVMValueRef int_min_value = bigint_to_llvm_const(get_llvm_type(g, type_entry), &int_min_bi);
2463 LLVMBasicBlockRef overflow_ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "DivOverflowOk");2472 LLVMBasicBlockRef overflow_ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "DivOverflowOk");
2464 LLVMBasicBlockRef overflow_fail_block = LLVMAppendBasicBlock(g->cur_fn_val, "DivOverflowFail");2473 LLVMBasicBlockRef overflow_fail_block = LLVMAppendBasicBlock(g->cur_fn_val, "DivOverflowFail");
2465 LLVMValueRef num_is_int_min = LLVMBuildICmp(g->builder, LLVMIntEQ, val1, int_min_value, "");2474 LLVMValueRef num_is_int_min = LLVMBuildICmp(g->builder, LLVMIntEQ, val1, int_min_value, "");
...@@ -2513,7 +2522,7 @@ static LLVMValueRef gen_div(CodeGen *g, bool want_runtime_safety, bool want_fast...@@ -2513,7 +2522,7 @@ static LLVMValueRef gen_div(CodeGen *g, bool want_runtime_safety, bool want_fast
2513 LLVMBuildBr(g->builder, end_block);2522 LLVMBuildBr(g->builder, end_block);
25142523
2515 LLVMPositionBuilderAtEnd(g->builder, end_block);2524 LLVMPositionBuilderAtEnd(g->builder, end_block);
2516 LLVMValueRef phi = LLVMBuildPhi(g->builder, type_entry->type_ref, "");2525 LLVMValueRef phi = LLVMBuildPhi(g->builder, get_llvm_type(g, type_entry), "");
2517 LLVMValueRef incoming_values[] = { ceiled, floored };2526 LLVMValueRef incoming_values[] = { ceiled, floored };
2518 LLVMBasicBlockRef incoming_blocks[] = { ceiled_end_block, floored_end_block };2527 LLVMBasicBlockRef incoming_blocks[] = { ceiled_end_block, floored_end_block };
2519 LLVMAddIncoming(phi, incoming_values, incoming_blocks, 2);2528 LLVMAddIncoming(phi, incoming_values, incoming_blocks, 2);
...@@ -2576,7 +2585,7 @@ static LLVMValueRef gen_div(CodeGen *g, bool want_runtime_safety, bool want_fast...@@ -2576,7 +2585,7 @@ static LLVMValueRef gen_div(CodeGen *g, bool want_runtime_safety, bool want_fast
2576 LLVMValueRef orig_num = LLVMBuildNSWMul(g->builder, result, val2, "");2585 LLVMValueRef orig_num = LLVMBuildNSWMul(g->builder, result, val2, "");
2577 LLVMValueRef orig_ok = LLVMBuildICmp(g->builder, LLVMIntEQ, orig_num, val1, "");2586 LLVMValueRef orig_ok = LLVMBuildICmp(g->builder, LLVMIntEQ, orig_num, val1, "");
2578 LLVMValueRef ok_bit = LLVMBuildOr(g->builder, orig_ok, is_pos, "");2587 LLVMValueRef ok_bit = LLVMBuildOr(g->builder, orig_ok, is_pos, "");
2579 LLVMValueRef one = LLVMConstInt(type_entry->type_ref, 1, true);2588 LLVMValueRef one = LLVMConstInt(get_llvm_type(g, type_entry), 1, true);
2580 LLVMValueRef result_minus_1 = LLVMBuildNSWSub(g->builder, result, one, "");2589 LLVMValueRef result_minus_1 = LLVMBuildNSWSub(g->builder, result, one, "");
2581 return LLVMBuildSelect(g->builder, ok_bit, result, result_minus_1, "");2590 return LLVMBuildSelect(g->builder, ok_bit, result, result_minus_1, "");
2582 }2591 }
...@@ -2595,7 +2604,7 @@ static LLVMValueRef gen_rem(CodeGen *g, bool want_runtime_safety, bool want_fast...@@ -2595,7 +2604,7 @@ static LLVMValueRef gen_rem(CodeGen *g, bool want_runtime_safety, bool want_fast
2595{2604{
2596 ZigLLVMSetFastMath(g->builder, want_fast_math);2605 ZigLLVMSetFastMath(g->builder, want_fast_math);
25972606
2598 LLVMValueRef zero = LLVMConstNull(type_entry->type_ref);2607 LLVMValueRef zero = LLVMConstNull(get_llvm_type(g, type_entry));
2599 if (want_runtime_safety) {2608 if (want_runtime_safety) {
2600 LLVMValueRef is_zero_bit;2609 LLVMValueRef is_zero_bit;
2601 if (type_entry->id == ZigTypeIdInt) {2610 if (type_entry->id == ZigTypeIdInt) {
...@@ -2820,7 +2829,7 @@ static void add_error_range_check(CodeGen *g, ZigType *err_set_type, ZigType *in...@@ -2820,7 +2829,7 @@ static void add_error_range_check(CodeGen *g, ZigType *err_set_type, ZigType *in
2820 assert(err_set_type->id == ZigTypeIdErrorSet);2829 assert(err_set_type->id == ZigTypeIdErrorSet);
28212830
2822 if (type_is_global_error_set(err_set_type)) {2831 if (type_is_global_error_set(err_set_type)) {
2823 LLVMValueRef zero = LLVMConstNull(int_type->type_ref);2832 LLVMValueRef zero = LLVMConstNull(get_llvm_type(g, int_type));
2824 LLVMValueRef neq_zero_bit = LLVMBuildICmp(g->builder, LLVMIntNE, target_val, zero, "");2833 LLVMValueRef neq_zero_bit = LLVMBuildICmp(g->builder, LLVMIntNE, target_val, zero, "");
2825 LLVMValueRef ok_bit;2834 LLVMValueRef ok_bit;
28262835
...@@ -2832,7 +2841,7 @@ static void add_error_range_check(CodeGen *g, ZigType *err_set_type, ZigType *in...@@ -2832,7 +2841,7 @@ static void add_error_range_check(CodeGen *g, ZigType *err_set_type, ZigType *in
2832 {2841 {
2833 ok_bit = neq_zero_bit;2842 ok_bit = neq_zero_bit;
2834 } else {2843 } else {
2835 LLVMValueRef error_value_count = LLVMConstInt(int_type->type_ref, g->errors_by_index.length, false);2844 LLVMValueRef error_value_count = LLVMConstInt(get_llvm_type(g, int_type), g->errors_by_index.length, false);
2836 LLVMValueRef in_bounds_bit = LLVMBuildICmp(g->builder, LLVMIntULT, target_val, error_value_count, "");2845 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, "");2846 ok_bit = LLVMBuildAnd(g->builder, neq_zero_bit, in_bounds_bit, "");
2838 }2847 }
...@@ -2853,7 +2862,8 @@ static void add_error_range_check(CodeGen *g, ZigType *err_set_type, ZigType *in...@@ -2853,7 +2862,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;2862 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);2863 LLVMValueRef switch_instr = LLVMBuildSwitch(g->builder, target_val, fail_block, err_count);
2855 for (uint32_t i = 0; i < err_count; i += 1) {2864 for (uint32_t i = 0; i < err_count; i += 1) {
2856 LLVMValueRef case_value = LLVMConstInt(g->err_tag_type->type_ref, err_set_type->data.error_set.errors[i]->value, false);2865 LLVMValueRef case_value = LLVMConstInt(get_llvm_type(g, g->err_tag_type),
2866 err_set_type->data.error_set.errors[i]->value, false);
2857 LLVMAddCase(switch_instr, case_value, ok_block);2867 LLVMAddCase(switch_instr, case_value, ok_block);
2858 }2868 }
28592869
...@@ -2892,7 +2902,7 @@ static LLVMValueRef ir_render_resize_slice(CodeGen *g, IrExecutable *executable,...@@ -2892,7 +2902,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, "");2902 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, "");2903 LLVMValueRef src_ptr = gen_load_untyped(g, src_ptr_ptr, 0, false, "");
2894 LLVMValueRef src_ptr_casted = LLVMBuildBitCast(g->builder, src_ptr,2904 LLVMValueRef src_ptr_casted = LLVMBuildBitCast(g->builder, src_ptr,
2895 wanted_type->data.structure.fields[0].type_entry->type_ref, "");2905 get_llvm_type(g, wanted_type->data.structure.fields[0].type_entry), "");
2896 LLVMValueRef dest_ptr_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr,2906 LLVMValueRef dest_ptr_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr,
2897 (unsigned)wanted_ptr_index, "");2907 (unsigned)wanted_ptr_index, "");
2898 gen_store_untyped(g, src_ptr_casted, dest_ptr_ptr, 0, false);2908 gen_store_untyped(g, src_ptr_casted, dest_ptr_ptr, 0, false);
...@@ -2904,13 +2914,13 @@ static LLVMValueRef ir_render_resize_slice(CodeGen *g, IrExecutable *executable,...@@ -2904,13 +2914,13 @@ static LLVMValueRef ir_render_resize_slice(CodeGen *g, IrExecutable *executable,
29042914
2905 LLVMValueRef new_len;2915 LLVMValueRef new_len;
2906 if (dest_size == 1) {2916 if (dest_size == 1) {
2907 LLVMValueRef src_size_val = LLVMConstInt(g->builtin_types.entry_usize->type_ref, src_size, false);2917 LLVMValueRef src_size_val = LLVMConstInt(g->builtin_types.entry_usize->llvm_type, src_size, false);
2908 new_len = LLVMBuildMul(g->builder, src_len, src_size_val, "");2918 new_len = LLVMBuildMul(g->builder, src_len, src_size_val, "");
2909 } else if (src_size == 1) {2919 } else if (src_size == 1) {
2910 LLVMValueRef dest_size_val = LLVMConstInt(g->builtin_types.entry_usize->type_ref, dest_size, false);2920 LLVMValueRef dest_size_val = LLVMConstInt(g->builtin_types.entry_usize->llvm_type, dest_size, false);
2911 if (ir_want_runtime_safety(g, &instruction->base)) {2921 if (ir_want_runtime_safety(g, &instruction->base)) {
2912 LLVMValueRef remainder_val = LLVMBuildURem(g->builder, src_len, dest_size_val, "");2922 LLVMValueRef remainder_val = LLVMBuildURem(g->builder, src_len, dest_size_val, "");
2913 LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_usize->type_ref);2923 LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_usize->llvm_type);
2914 LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, remainder_val, zero, "");2924 LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, remainder_val, zero, "");
2915 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "SliceWidenOk");2925 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "SliceWidenOk");
2916 LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn_val, "SliceWidenFail");2926 LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn_val, "SliceWidenFail");
...@@ -2951,9 +2961,9 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,...@@ -2951,9 +2961,9 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,
2951 case CastOpIntToFloat:2961 case CastOpIntToFloat:
2952 assert(actual_type->id == ZigTypeIdInt);2962 assert(actual_type->id == ZigTypeIdInt);
2953 if (actual_type->data.integral.is_signed) {2963 if (actual_type->data.integral.is_signed) {
2954 return LLVMBuildSIToFP(g->builder, expr_val, wanted_type->type_ref, "");2964 return LLVMBuildSIToFP(g->builder, expr_val, get_llvm_type(g, wanted_type), "");
2955 } else {2965 } else {
2956 return LLVMBuildUIToFP(g->builder, expr_val, wanted_type->type_ref, "");2966 return LLVMBuildUIToFP(g->builder, expr_val, get_llvm_type(g, wanted_type), "");
2957 }2967 }
2958 case CastOpFloatToInt: {2968 case CastOpFloatToInt: {
2959 assert(wanted_type->id == ZigTypeIdInt);2969 assert(wanted_type->id == ZigTypeIdInt);
...@@ -2963,9 +2973,9 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,...@@ -2963,9 +2973,9 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,
29632973
2964 LLVMValueRef result;2974 LLVMValueRef result;
2965 if (wanted_type->data.integral.is_signed) {2975 if (wanted_type->data.integral.is_signed) {
2966 result = LLVMBuildFPToSI(g->builder, expr_val, wanted_type->type_ref, "");2976 result = LLVMBuildFPToSI(g->builder, expr_val, get_llvm_type(g, wanted_type), "");
2967 } else {2977 } else {
2968 result = LLVMBuildFPToUI(g->builder, expr_val, wanted_type->type_ref, "");2978 result = LLVMBuildFPToUI(g->builder, expr_val, get_llvm_type(g, wanted_type), "");
2969 }2979 }
29702980
2971 if (want_safety) {2981 if (want_safety) {
...@@ -2993,14 +3003,14 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,...@@ -2993,14 +3003,14 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,
2993 case CastOpBoolToInt:3003 case CastOpBoolToInt:
2994 assert(wanted_type->id == ZigTypeIdInt);3004 assert(wanted_type->id == ZigTypeIdInt);
2995 assert(actual_type->id == ZigTypeIdBool);3005 assert(actual_type->id == ZigTypeIdBool);
2996 return LLVMBuildZExt(g->builder, expr_val, wanted_type->type_ref, "");3006 return LLVMBuildZExt(g->builder, expr_val, get_llvm_type(g, wanted_type), "");
2997 case CastOpErrSet:3007 case CastOpErrSet:
2998 if (ir_want_runtime_safety(g, &cast_instruction->base)) {3008 if (ir_want_runtime_safety(g, &cast_instruction->base)) {
2999 add_error_range_check(g, wanted_type, g->err_tag_type, expr_val);3009 add_error_range_check(g, wanted_type, g->err_tag_type, expr_val);
3000 }3010 }
3001 return expr_val;3011 return expr_val;
3002 case CastOpBitCast:3012 case CastOpBitCast:
3003 return LLVMBuildBitCast(g->builder, expr_val, wanted_type->type_ref, "");3013 return LLVMBuildBitCast(g->builder, expr_val, get_llvm_type(g, wanted_type), "");
3004 case CastOpPtrOfArrayToSlice: {3014 case CastOpPtrOfArrayToSlice: {
3005 assert(cast_instruction->tmp_ptr);3015 assert(cast_instruction->tmp_ptr);
3006 assert(actual_type->id == ZigTypeIdPointer);3016 assert(actual_type->id == ZigTypeIdPointer);
...@@ -3010,15 +3020,15 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,...@@ -3010,15 +3020,15 @@ static LLVMValueRef ir_render_cast(CodeGen *g, IrExecutable *executable,
3010 LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, cast_instruction->tmp_ptr,3020 LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, cast_instruction->tmp_ptr,
3011 slice_ptr_index, "");3021 slice_ptr_index, "");
3012 LLVMValueRef indices[] = {3022 LLVMValueRef indices[] = {
3013 LLVMConstNull(g->builtin_types.entry_usize->type_ref),3023 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),
3014 LLVMConstInt(g->builtin_types.entry_usize->type_ref, 0, false),3024 LLVMConstInt(g->builtin_types.entry_usize->llvm_type, 0, false),
3015 };3025 };
3016 LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP(g->builder, expr_val, indices, 2, "");3026 LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP(g->builder, expr_val, indices, 2, "");
3017 gen_store_untyped(g, slice_start_ptr, ptr_field_ptr, 0, false);3027 gen_store_untyped(g, slice_start_ptr, ptr_field_ptr, 0, false);
30183028
3019 LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, cast_instruction->tmp_ptr,3029 LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, cast_instruction->tmp_ptr,
3020 slice_len_index, "");3030 slice_len_index, "");
3021 LLVMValueRef len_value = LLVMConstInt(g->builtin_types.entry_usize->type_ref,3031 LLVMValueRef len_value = LLVMConstInt(g->builtin_types.entry_usize->llvm_type,
3022 array_type->data.array.len, false);3032 array_type->data.array.len, false);
3023 gen_store_untyped(g, len_value, len_field_ptr, 0, false);3033 gen_store_untyped(g, len_value, len_field_ptr, 0, false);
30243034
...@@ -3036,7 +3046,7 @@ static LLVMValueRef ir_render_ptr_cast(CodeGen *g, IrExecutable *executable,...@@ -3036,7 +3046,7 @@ static LLVMValueRef ir_render_ptr_cast(CodeGen *g, IrExecutable *executable,
3036 return nullptr;3046 return nullptr;
3037 }3047 }
3038 LLVMValueRef ptr = ir_llvm_value(g, instruction->ptr);3048 LLVMValueRef ptr = ir_llvm_value(g, instruction->ptr);
3039 LLVMValueRef result_ptr = LLVMBuildBitCast(g->builder, ptr, wanted_type->type_ref, "");3049 LLVMValueRef result_ptr = LLVMBuildBitCast(g->builder, ptr, get_llvm_type(g, wanted_type), "");
3040 bool want_safety_check = instruction->safety_check_on && ir_want_runtime_safety(g, &instruction->base);3050 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))3051 if (!want_safety_check || ptr_allows_addr_zero(wanted_type))
3042 return result_ptr;3052 return result_ptr;
...@@ -3066,16 +3076,16 @@ static LLVMValueRef ir_render_bit_cast(CodeGen *g, IrExecutable *executable,...@@ -3066,16 +3076,16 @@ static LLVMValueRef ir_render_bit_cast(CodeGen *g, IrExecutable *executable,
3066 if (wanted_is_ptr == actual_is_ptr) {3076 if (wanted_is_ptr == actual_is_ptr) {
3067 // We either bitcast the value directly or bitcast the pointer which does a pointer cast3077 // We either bitcast the value directly or bitcast the pointer which does a pointer cast
3068 LLVMTypeRef wanted_type_ref = wanted_is_ptr ?3078 LLVMTypeRef wanted_type_ref = wanted_is_ptr ?
3069 LLVMPointerType(wanted_type->type_ref, 0) : wanted_type->type_ref;3079 LLVMPointerType(get_llvm_type(g, wanted_type), 0) : get_llvm_type(g, wanted_type);
3070 return LLVMBuildBitCast(g->builder, value, wanted_type_ref, "");3080 return LLVMBuildBitCast(g->builder, value, wanted_type_ref, "");
3071 } else if (actual_is_ptr) {3081 } else if (actual_is_ptr) {
3072 LLVMTypeRef wanted_ptr_type_ref = LLVMPointerType(wanted_type->type_ref, 0);3082 LLVMTypeRef wanted_ptr_type_ref = LLVMPointerType(get_llvm_type(g, wanted_type), 0);
3073 LLVMValueRef bitcasted_ptr = LLVMBuildBitCast(g->builder, value, wanted_ptr_type_ref, "");3083 LLVMValueRef bitcasted_ptr = LLVMBuildBitCast(g->builder, value, wanted_ptr_type_ref, "");
3074 uint32_t alignment = get_abi_alignment(g, actual_type);3084 uint32_t alignment = get_abi_alignment(g, actual_type);
3075 return gen_load_untyped(g, bitcasted_ptr, alignment, false, "");3085 return gen_load_untyped(g, bitcasted_ptr, alignment, false, "");
3076 } else {3086 } else {
3077 assert(instruction->tmp_ptr != nullptr);3087 assert(instruction->tmp_ptr != nullptr);
3078 LLVMTypeRef wanted_ptr_type_ref = LLVMPointerType(actual_type->type_ref, 0);3088 LLVMTypeRef wanted_ptr_type_ref = LLVMPointerType(get_llvm_type(g, actual_type), 0);
3079 LLVMValueRef bitcasted_ptr = LLVMBuildBitCast(g->builder, instruction->tmp_ptr, wanted_ptr_type_ref, "");3089 LLVMValueRef bitcasted_ptr = LLVMBuildBitCast(g->builder, instruction->tmp_ptr, wanted_ptr_type_ref, "");
3080 uint32_t alignment = get_abi_alignment(g, wanted_type);3090 uint32_t alignment = get_abi_alignment(g, wanted_type);
3081 gen_store_untyped(g, value, bitcasted_ptr, alignment, false);3091 gen_store_untyped(g, value, bitcasted_ptr, alignment, false);
...@@ -3115,13 +3125,13 @@ static LLVMValueRef ir_render_int_to_ptr(CodeGen *g, IrExecutable *executable, I...@@ -3115,13 +3125,13 @@ static LLVMValueRef ir_render_int_to_ptr(CodeGen *g, IrExecutable *executable, I
31153125
3116 LLVMPositionBuilderAtEnd(g->builder, ok_block);3126 LLVMPositionBuilderAtEnd(g->builder, ok_block);
3117 }3127 }
3118 return LLVMBuildIntToPtr(g->builder, target_val, wanted_type->type_ref, "");3128 return LLVMBuildIntToPtr(g->builder, target_val, get_llvm_type(g, wanted_type), "");
3119}3129}
31203130
3121static LLVMValueRef ir_render_ptr_to_int(CodeGen *g, IrExecutable *executable, IrInstructionPtrToInt *instruction) {3131static LLVMValueRef ir_render_ptr_to_int(CodeGen *g, IrExecutable *executable, IrInstructionPtrToInt *instruction) {
3122 ZigType *wanted_type = instruction->base.value.type;3132 ZigType *wanted_type = instruction->base.value.type;
3123 LLVMValueRef target_val = ir_llvm_value(g, instruction->target);3133 LLVMValueRef target_val = ir_llvm_value(g, instruction->target);
3124 return LLVMBuildPtrToInt(g->builder, target_val, wanted_type->type_ref, "");3134 return LLVMBuildPtrToInt(g->builder, target_val, get_llvm_type(g, wanted_type), "");
3125}3135}
31263136
3127static LLVMValueRef ir_render_int_to_enum(CodeGen *g, IrExecutable *executable, IrInstructionIntToEnum *instruction) {3137static LLVMValueRef ir_render_int_to_enum(CodeGen *g, IrExecutable *executable, IrInstructionIntToEnum *instruction) {
...@@ -3139,7 +3149,7 @@ static LLVMValueRef ir_render_int_to_enum(CodeGen *g, IrExecutable *executable,...@@ -3139,7 +3149,7 @@ static LLVMValueRef ir_render_int_to_enum(CodeGen *g, IrExecutable *executable,
3139 size_t field_count = wanted_type->data.enumeration.src_field_count;3149 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);3150 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) {3151 for (size_t field_i = 0; field_i < field_count; field_i += 1) {
3142 LLVMValueRef this_tag_int_value = bigint_to_llvm_const(tag_int_type->type_ref,3152 LLVMValueRef this_tag_int_value = bigint_to_llvm_const(get_llvm_type(g, tag_int_type),
3143 &wanted_type->data.enumeration.fields[field_i].value);3153 &wanted_type->data.enumeration.fields[field_i].value);
3144 LLVMAddCase(switch_instr, this_tag_int_value, ok_value_block);3154 LLVMAddCase(switch_instr, this_tag_int_value, ok_value_block);
3145 }3155 }
...@@ -3321,7 +3331,7 @@ static LLVMValueRef ir_render_load_ptr(CodeGen *g, IrExecutable *executable, IrI...@@ -3321,7 +3331,7 @@ static LLVMValueRef ir_render_load_ptr(CodeGen *g, IrExecutable *executable, IrI
3321 LLVMValueRef shifted_value = LLVMBuildLShr(g->builder, containing_int, shift_amt_val, "");3331 LLVMValueRef shifted_value = LLVMBuildLShr(g->builder, containing_int, shift_amt_val, "");
33223332
3323 if (!handle_is_ptr(child_type))3333 if (!handle_is_ptr(child_type))
3324 return LLVMBuildTrunc(g->builder, shifted_value, child_type->type_ref, "");3334 return LLVMBuildTrunc(g->builder, shifted_value, get_llvm_type(g, child_type), "");
33253335
3326 assert(instruction->tmp_ptr != nullptr);3336 assert(instruction->tmp_ptr != nullptr);
3327 LLVMTypeRef same_size_int = LLVMIntType(size_in_bits);3337 LLVMTypeRef same_size_int = LLVMIntType(size_in_bits);
...@@ -3378,7 +3388,7 @@ static LLVMValueRef gen_valgrind_client_request(CodeGen *g, LLVMValueRef default...@@ -3378,7 +3388,7 @@ static LLVMValueRef gen_valgrind_client_request(CodeGen *g, LLVMValueRef default
3378 if (!target_has_valgrind_support(g->zig_target)) {3388 if (!target_has_valgrind_support(g->zig_target)) {
3379 return default_value;3389 return default_value;
3380 }3390 }
3381 LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->type_ref;3391 LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->llvm_type;
3382 bool asm_has_side_effects = true;3392 bool asm_has_side_effects = true;
3383 bool asm_is_alignstack = false;3393 bool asm_is_alignstack = false;
3384 if (g->zig_target->arch == ZigLLVM_x86_64) {3394 if (g->zig_target->arch == ZigLLVM_x86_64) {
...@@ -3445,7 +3455,7 @@ static bool want_valgrind_support(CodeGen *g) {...@@ -3445,7 +3455,7 @@ static bool want_valgrind_support(CodeGen *g) {
34453455
3446static void gen_undef_init(CodeGen *g, uint32_t ptr_align_bytes, ZigType *value_type, LLVMValueRef ptr) {3456static void gen_undef_init(CodeGen *g, uint32_t ptr_align_bytes, ZigType *value_type, LLVMValueRef ptr) {
3447 assert(type_has_bits(value_type));3457 assert(type_has_bits(value_type));
3448 uint64_t size_bytes = LLVMStoreSizeOfType(g->target_data_ref, value_type->type_ref);3458 uint64_t size_bytes = LLVMStoreSizeOfType(g->target_data_ref, get_llvm_type(g, value_type));
3449 assert(size_bytes > 0);3459 assert(size_bytes > 0);
3450 assert(ptr_align_bytes > 0);3460 assert(ptr_align_bytes > 0);
3451 // memset uninitialized memory to 0xaa3461 // memset uninitialized memory to 0xaa
...@@ -3453,14 +3463,14 @@ static void gen_undef_init(CodeGen *g, uint32_t ptr_align_bytes, ZigType *value_...@@ -3453,14 +3463,14 @@ static void gen_undef_init(CodeGen *g, uint32_t ptr_align_bytes, ZigType *value_
3453 LLVMValueRef fill_char = LLVMConstInt(LLVMInt8Type(), 0xaa, false);3463 LLVMValueRef fill_char = LLVMConstInt(LLVMInt8Type(), 0xaa, false);
3454 LLVMValueRef dest_ptr = LLVMBuildBitCast(g->builder, ptr, ptr_u8, "");3464 LLVMValueRef dest_ptr = LLVMBuildBitCast(g->builder, ptr, ptr_u8, "");
3455 ZigType *usize = g->builtin_types.entry_usize;3465 ZigType *usize = g->builtin_types.entry_usize;
3456 LLVMValueRef byte_count = LLVMConstInt(usize->type_ref, size_bytes, false);3466 LLVMValueRef byte_count = LLVMConstInt(usize->llvm_type, size_bytes, false);
3457 ZigLLVMBuildMemSet(g->builder, dest_ptr, fill_char, byte_count, ptr_align_bytes, false);3467 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 it3468 // then tell valgrind that the memory is undefined even though we just memset it
3459 if (want_valgrind_support(g)) {3469 if (want_valgrind_support(g)) {
3460 static const uint32_t VG_USERREQ__MAKE_MEM_UNDEFINED = 1296236545;3470 static const uint32_t VG_USERREQ__MAKE_MEM_UNDEFINED = 1296236545;
3461 LLVMValueRef zero = LLVMConstInt(usize->type_ref, 0, false);3471 LLVMValueRef zero = LLVMConstInt(usize->llvm_type, 0, false);
3462 LLVMValueRef req = LLVMConstInt(usize->type_ref, VG_USERREQ__MAKE_MEM_UNDEFINED, false);3472 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, "");3473 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);3474 gen_valgrind_client_request(g, zero, req, ptr_as_usize, byte_count, zero, zero, zero);
3465 }3475 }
3466}3476}
...@@ -3515,7 +3525,7 @@ static LLVMValueRef ir_render_elem_ptr(CodeGen *g, IrExecutable *executable, IrI...@@ -3515,7 +3525,7 @@ static LLVMValueRef ir_render_elem_ptr(CodeGen *g, IrExecutable *executable, IrI
3515 array_type = array_type->data.pointer.child_type;3525 array_type = array_type->data.pointer.child_type;
3516 }3526 }
3517 if (safety_check_on) {3527 if (safety_check_on) {
3518 LLVMValueRef end = LLVMConstInt(g->builtin_types.entry_usize->type_ref,3528 LLVMValueRef end = LLVMConstInt(g->builtin_types.entry_usize->llvm_type,
3519 array_type->data.array.len, false);3529 array_type->data.array.len, false);
3520 add_bounds_check(g, subscript_value, LLVMIntEQ, nullptr, LLVMIntULT, end);3530 add_bounds_check(g, subscript_value, LLVMIntEQ, nullptr, LLVMIntULT, end);
3521 }3531 }
...@@ -3533,18 +3543,18 @@ static LLVMValueRef ir_render_elem_ptr(CodeGen *g, IrExecutable *executable, IrI...@@ -3533,18 +3543,18 @@ static LLVMValueRef ir_render_elem_ptr(CodeGen *g, IrExecutable *executable, IrI
3533 LLVMTypeRef ptr_u8_type_ref = LLVMPointerType(LLVMInt8Type(), 0);3543 LLVMTypeRef ptr_u8_type_ref = LLVMPointerType(LLVMInt8Type(), 0);
3534 LLVMValueRef u8_array_ptr = LLVMBuildBitCast(g->builder, array_ptr, ptr_u8_type_ref, "");3544 LLVMValueRef u8_array_ptr = LLVMBuildBitCast(g->builder, array_ptr, ptr_u8_type_ref, "");
3535 assert(size_in_bits % 8 == 0);3545 assert(size_in_bits % 8 == 0);
3536 LLVMValueRef elem_size_bytes = LLVMConstInt(g->builtin_types.entry_usize->type_ref,3546 LLVMValueRef elem_size_bytes = LLVMConstInt(g->builtin_types.entry_usize->llvm_type,
3537 size_in_bits / 8, false);3547 size_in_bits / 8, false);
3538 LLVMValueRef byte_offset = LLVMBuildNUWMul(g->builder, subscript_value, elem_size_bytes, "");3548 LLVMValueRef byte_offset = LLVMBuildNUWMul(g->builder, subscript_value, elem_size_bytes, "");
3539 LLVMValueRef indices[] = {3549 LLVMValueRef indices[] = {
3540 byte_offset3550 byte_offset
3541 };3551 };
3542 LLVMValueRef elem_byte_ptr = LLVMBuildInBoundsGEP(g->builder, u8_array_ptr, indices, 1, "");3552 LLVMValueRef elem_byte_ptr = LLVMBuildInBoundsGEP(g->builder, u8_array_ptr, indices, 1, "");
3543 return LLVMBuildBitCast(g->builder, elem_byte_ptr, LLVMPointerType(child_type->type_ref, 0), "");3553 return LLVMBuildBitCast(g->builder, elem_byte_ptr, LLVMPointerType(get_llvm_type(g, child_type), 0), "");
3544 }3554 }
3545 }3555 }
3546 LLVMValueRef indices[] = {3556 LLVMValueRef indices[] = {
3547 LLVMConstNull(g->builtin_types.entry_usize->type_ref),3557 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),
3548 subscript_value3558 subscript_value
3549 };3559 };
3550 return LLVMBuildInBoundsGEP(g->builder, array_ptr, indices, 2, "");3560 return LLVMBuildInBoundsGEP(g->builder, array_ptr, indices, 2, "");
...@@ -3556,7 +3566,9 @@ static LLVMValueRef ir_render_elem_ptr(CodeGen *g, IrExecutable *executable, IrI...@@ -3556,7 +3566,9 @@ static LLVMValueRef ir_render_elem_ptr(CodeGen *g, IrExecutable *executable, IrI
3556 return LLVMBuildInBoundsGEP(g->builder, array_ptr, indices, 1, "");3566 return LLVMBuildInBoundsGEP(g->builder, array_ptr, indices, 1, "");
3557 } else if (array_type->id == ZigTypeIdStruct) {3567 } else if (array_type->id == ZigTypeIdStruct) {
3558 assert(array_type->data.structure.is_slice);3568 assert(array_type->data.structure.is_slice);
3559 if (!type_has_bits(instruction->base.value.type)) {3569
3570 ZigType *ptr_type = instruction->base.value.type;
3571 if (!type_has_bits(ptr_type)) {
3560 if (safety_check_on) {3572 if (safety_check_on) {
3561 assert(LLVMGetTypeKind(LLVMTypeOf(array_ptr)) == LLVMIntegerTypeKind);3573 assert(LLVMGetTypeKind(LLVMTypeOf(array_ptr)) == LLVMIntegerTypeKind);
3562 add_bounds_check(g, subscript_value, LLVMIntEQ, nullptr, LLVMIntULT, array_ptr);3574 add_bounds_check(g, subscript_value, LLVMIntEQ, nullptr, LLVMIntULT, array_ptr);
...@@ -3773,7 +3785,7 @@ static LLVMValueRef ir_render_union_field_ptr(CodeGen *g, IrExecutable *executab...@@ -3773,7 +3785,7 @@ static LLVMValueRef ir_render_union_field_ptr(CodeGen *g, IrExecutable *executab
3773 return nullptr;3785 return nullptr;
37743786
3775 LLVMValueRef union_ptr = ir_llvm_value(g, instruction->union_ptr);3787 LLVMValueRef union_ptr = ir_llvm_value(g, instruction->union_ptr);
3776 LLVMTypeRef field_type_ref = LLVMPointerType(field->type_entry->type_ref, 0);3788 LLVMTypeRef field_type_ref = LLVMPointerType(get_llvm_type(g, field->type_entry), 0);
37773789
3778 if (union_type->data.unionation.gen_tag_index == SIZE_MAX) {3790 if (union_type->data.unionation.gen_tag_index == SIZE_MAX) {
3779 LLVMValueRef union_field_ptr = LLVMBuildStructGEP(g->builder, union_ptr, 0, "");3791 LLVMValueRef union_field_ptr = LLVMBuildStructGEP(g->builder, union_ptr, 0, "");
...@@ -3786,7 +3798,7 @@ static LLVMValueRef ir_render_union_field_ptr(CodeGen *g, IrExecutable *executab...@@ -3786,7 +3798,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, "");3798 LLVMValueRef tag_value = gen_load_untyped(g, tag_field_ptr, 0, false, "");
37873799
37883800
3789 LLVMValueRef expected_tag_value = bigint_to_llvm_const(union_type->data.unionation.tag_type->type_ref,3801 LLVMValueRef expected_tag_value = bigint_to_llvm_const(get_llvm_type(g, union_type->data.unionation.tag_type),
3790 &field->enum_field->value);3802 &field->enum_field->value);
3791 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "UnionCheckOk");3803 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "UnionCheckOk");
3792 LLVMBasicBlockRef bad_block = LLVMAppendBasicBlock(g->cur_fn_val, "UnionCheckFail");3804 LLVMBasicBlockRef bad_block = LLVMAppendBasicBlock(g->cur_fn_val, "UnionCheckFail");
...@@ -3916,7 +3928,7 @@ static LLVMValueRef ir_render_asm(CodeGen *g, IrExecutable *executable, IrInstru...@@ -3916,7 +3928,7 @@ static LLVMValueRef ir_render_asm(CodeGen *g, IrExecutable *executable, IrInstru
3916 }3928 }
39173929
3918 ZigType *const type = ir_input->value.type;3930 ZigType *const type = ir_input->value.type;
3919 LLVMTypeRef type_ref = type->type_ref;3931 LLVMTypeRef type_ref = get_llvm_type(g, type);
3920 LLVMValueRef value_ref = ir_llvm_value(g, ir_input);3932 LLVMValueRef value_ref = ir_llvm_value(g, ir_input);
3921 // Handle integers of non pot bitsize by widening them.3933 // Handle integers of non pot bitsize by widening them.
3922 if (type->id == ZigTypeIdInt) {3934 if (type->id == ZigTypeIdInt) {
...@@ -3925,7 +3937,7 @@ static LLVMValueRef ir_render_asm(CodeGen *g, IrExecutable *executable, IrInstru...@@ -3925,7 +3937,7 @@ static LLVMValueRef ir_render_asm(CodeGen *g, IrExecutable *executable, IrInstru
3925 const bool is_signed = type->data.integral.is_signed;3937 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);3938 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);3939 ZigType *const wider_type = get_int_type(g, is_signed, wider_bitsize);
3928 type_ref = wider_type->type_ref;3940 type_ref = get_llvm_type(g, wider_type);
3929 value_ref = gen_widen_or_shorten(g, false, type, wider_type, value_ref);3941 value_ref = gen_widen_or_shorten(g, false, type, wider_type, value_ref);
3930 }3942 }
3931 }3943 }
...@@ -3945,7 +3957,7 @@ static LLVMValueRef ir_render_asm(CodeGen *g, IrExecutable *executable, IrInstru...@@ -3945,7 +3957,7 @@ static LLVMValueRef ir_render_asm(CodeGen *g, IrExecutable *executable, IrInstru
3945 if (instruction->return_count == 0) {3957 if (instruction->return_count == 0) {
3946 ret_type = LLVMVoidType();3958 ret_type = LLVMVoidType();
3947 } else {3959 } else {
3948 ret_type = instruction->base.value.type->type_ref;3960 ret_type = get_llvm_type(g, instruction->base.value.type);
3949 }3961 }
3950 LLVMTypeRef function_type = LLVMFunctionType(ret_type, param_types, (unsigned)input_and_output_count, false);3962 LLVMTypeRef function_type = LLVMFunctionType(ret_type, param_types, (unsigned)input_and_output_count, false);
39513963
...@@ -3964,7 +3976,7 @@ static LLVMValueRef gen_non_null_bit(CodeGen *g, ZigType *maybe_type, LLVMValueR...@@ -3964,7 +3976,7 @@ static LLVMValueRef gen_non_null_bit(CodeGen *g, ZigType *maybe_type, LLVMValueR
3964 } else {3976 } else {
3965 bool is_scalar = !handle_is_ptr(maybe_type);3977 bool is_scalar = !handle_is_ptr(maybe_type);
3966 if (is_scalar) {3978 if (is_scalar) {
3967 return LLVMBuildICmp(g->builder, LLVMIntNE, maybe_handle, LLVMConstNull(maybe_type->type_ref), "");3979 return LLVMBuildICmp(g->builder, LLVMIntNE, maybe_handle, LLVMConstNull(get_llvm_type(g, maybe_type)), "");
3968 } else {3980 } else {
3969 LLVMValueRef maybe_field_ptr = LLVMBuildStructGEP(g->builder, maybe_handle, maybe_null_index, "");3981 LLVMValueRef maybe_field_ptr = LLVMBuildStructGEP(g->builder, maybe_handle, maybe_null_index, "");
3970 return gen_load_untyped(g, maybe_field_ptr, 0, false, "");3982 return gen_load_untyped(g, maybe_field_ptr, 0, false, "");
...@@ -3999,7 +4011,7 @@ static LLVMValueRef ir_render_optional_unwrap_ptr(CodeGen *g, IrExecutable *exec...@@ -3999,7 +4011,7 @@ static LLVMValueRef ir_render_optional_unwrap_ptr(CodeGen *g, IrExecutable *exec
39994011
4000 LLVMPositionBuilderAtEnd(g->builder, ok_block);4012 LLVMPositionBuilderAtEnd(g->builder, ok_block);
4001 }4013 }
4002 if (child_type->zero_bits) {4014 if (!type_has_bits(child_type)) {
4003 return nullptr;4015 return nullptr;
4004 } else {4016 } else {
4005 bool is_scalar = !handle_is_ptr(maybe_type);4017 bool is_scalar = !handle_is_ptr(maybe_type);
...@@ -4052,10 +4064,10 @@ static LLVMValueRef get_int_builtin_fn(CodeGen *g, ZigType *int_type, BuiltinFnI...@@ -4052,10 +4064,10 @@ static LLVMValueRef get_int_builtin_fn(CodeGen *g, ZigType *int_type, BuiltinFnI
4052 char llvm_name[64];4064 char llvm_name[64];
4053 sprintf(llvm_name, "llvm.%s.i%" PRIu32, fn_name, int_type->data.integral.bit_count);4065 sprintf(llvm_name, "llvm.%s.i%" PRIu32, fn_name, int_type->data.integral.bit_count);
4054 LLVMTypeRef param_types[] = {4066 LLVMTypeRef param_types[] = {
4055 int_type->type_ref,4067 get_llvm_type(g, int_type),
4056 LLVMInt1Type(),4068 LLVMInt1Type(),
4057 };4069 };
4058 LLVMTypeRef fn_type = LLVMFunctionType(int_type->type_ref, param_types, n_args, false);4070 LLVMTypeRef fn_type = LLVMFunctionType(get_llvm_type(g, int_type), param_types, n_args, false);
4059 LLVMValueRef fn_val = LLVMAddFunction(g->module, llvm_name, fn_type);4071 LLVMValueRef fn_val = LLVMAddFunction(g->module, llvm_name, fn_type);
4060 assert(LLVMGetIntrinsicID(fn_val));4072 assert(LLVMGetIntrinsicID(fn_val));
40614073
...@@ -4114,9 +4126,9 @@ static LLVMValueRef ir_render_phi(CodeGen *g, IrExecutable *executable, IrInstru...@@ -4114,9 +4126,9 @@ static LLVMValueRef ir_render_phi(CodeGen *g, IrExecutable *executable, IrInstru
41144126
4115 LLVMTypeRef phi_type;4127 LLVMTypeRef phi_type;
4116 if (handle_is_ptr(instruction->base.value.type)) {4128 if (handle_is_ptr(instruction->base.value.type)) {
4117 phi_type = LLVMPointerType(instruction->base.value.type->type_ref, 0);4129 phi_type = LLVMPointerType(get_llvm_type(g,instruction->base.value.type), 0);
4118 } else {4130 } else {
4119 phi_type = instruction->base.value.type->type_ref;4131 phi_type = get_llvm_type(g, instruction->base.value.type);
4120 }4132 }
41214133
4122 LLVMValueRef phi = LLVMBuildPhi(g->builder, phi_type, "");4134 LLVMValueRef phi = LLVMBuildPhi(g->builder, phi_type, "");
...@@ -4160,7 +4172,7 @@ static LLVMValueRef ir_render_err_name(CodeGen *g, IrExecutable *executable, IrI...@@ -4160,7 +4172,7 @@ static LLVMValueRef ir_render_err_name(CodeGen *g, IrExecutable *executable, IrI
4160 }4172 }
41614173
4162 LLVMValueRef indices[] = {4174 LLVMValueRef indices[] = {
4163 LLVMConstNull(g->builtin_types.entry_usize->type_ref),4175 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),
4164 err_val,4176 err_val,
4165 };4177 };
4166 return LLVMBuildInBoundsGEP(g->builder, g->err_name_table, indices, 2, "");4178 return LLVMBuildInBoundsGEP(g->builder, g->err_name_table, indices, 2, "");
...@@ -4176,8 +4188,9 @@ static LLVMValueRef get_enum_tag_name_function(CodeGen *g, ZigType *enum_type) {...@@ -4176,8 +4188,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);4188 ZigType *u8_slice_type = get_slice_type(g, u8_ptr_type);
4177 ZigType *tag_int_type = enum_type->data.enumeration.tag_int_type;4189 ZigType *tag_int_type = enum_type->data.enumeration.tag_int_type;
41784190
4179 LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMPointerType(u8_slice_type->type_ref, 0),4191 LLVMTypeRef tag_int_llvm_type = get_llvm_type(g, tag_int_type);
4180 &tag_int_type->type_ref, 1, false);4192 LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMPointerType(get_llvm_type(g, u8_slice_type), 0),
4193 &tag_int_llvm_type, 1, false);
4181 4194
4182 Buf *fn_name = get_mangled_name(g, buf_sprintf("__zig_tag_name_%s", buf_ptr(&enum_type->name)), false);4195 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);4196 LLVMValueRef fn_val = LLVMAddFunction(g->module, buf_ptr(fn_name), fn_type_ref);
...@@ -4209,8 +4222,8 @@ static LLVMValueRef get_enum_tag_name_function(CodeGen *g, ZigType *enum_type) {...@@ -4209,8 +4222,8 @@ static LLVMValueRef get_enum_tag_name_function(CodeGen *g, ZigType *enum_type) {
42094222
4210 ZigType *usize = g->builtin_types.entry_usize;4223 ZigType *usize = g->builtin_types.entry_usize;
4211 LLVMValueRef array_ptr_indices[] = {4224 LLVMValueRef array_ptr_indices[] = {
4212 LLVMConstNull(usize->type_ref),4225 LLVMConstNull(usize->llvm_type),
4213 LLVMConstNull(usize->type_ref),4226 LLVMConstNull(usize->llvm_type),
4214 };4227 };
42154228
4216 for (size_t field_i = 0; field_i < field_count; field_i += 1) {4229 for (size_t field_i = 0; field_i < field_count; field_i += 1) {
...@@ -4225,9 +4238,9 @@ static LLVMValueRef get_enum_tag_name_function(CodeGen *g, ZigType *enum_type) {...@@ -4225,9 +4238,9 @@ static LLVMValueRef get_enum_tag_name_function(CodeGen *g, ZigType *enum_type) {
42254238
4226 LLVMValueRef fields[] = {4239 LLVMValueRef fields[] = {
4227 LLVMConstGEP(str_global, array_ptr_indices, 2),4240 LLVMConstGEP(str_global, array_ptr_indices, 2),
4228 LLVMConstInt(g->builtin_types.entry_usize->type_ref, buf_len(name), false),4241 LLVMConstInt(g->builtin_types.entry_usize->llvm_type, buf_len(name), false),
4229 };4242 };
4230 LLVMValueRef slice_init_value = LLVMConstNamedStruct(u8_slice_type->type_ref, fields, 2);4243 LLVMValueRef slice_init_value = LLVMConstNamedStruct(get_llvm_type(g, u8_slice_type), fields, 2);
42314244
4232 LLVMValueRef slice_global = LLVMAddGlobal(g->module, LLVMTypeOf(slice_init_value), "");4245 LLVMValueRef slice_global = LLVMAddGlobal(g->module, LLVMTypeOf(slice_init_value), "");
4233 LLVMSetInitializer(slice_global, slice_init_value);4246 LLVMSetInitializer(slice_global, slice_init_value);
...@@ -4237,7 +4250,7 @@ static LLVMValueRef get_enum_tag_name_function(CodeGen *g, ZigType *enum_type) {...@@ -4237,7 +4250,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)));4250 LLVMSetAlignment(slice_global, LLVMABIAlignmentOfType(g->target_data_ref, LLVMTypeOf(slice_init_value)));
42384251
4239 LLVMBasicBlockRef return_block = LLVMAppendBasicBlock(g->cur_fn_val, "Name");4252 LLVMBasicBlockRef return_block = LLVMAppendBasicBlock(g->cur_fn_val, "Name");
4240 LLVMValueRef this_tag_int_value = bigint_to_llvm_const(tag_int_type->type_ref,4253 LLVMValueRef this_tag_int_value = bigint_to_llvm_const(get_llvm_type(g, tag_int_type),
4241 &enum_type->data.enumeration.fields[field_i].value);4254 &enum_type->data.enumeration.fields[field_i].value);
4242 LLVMAddCase(switch_instr, this_tag_int_value, return_block);4255 LLVMAddCase(switch_instr, this_tag_int_value, return_block);
42434256
...@@ -4283,22 +4296,21 @@ static LLVMValueRef ir_render_field_parent_ptr(CodeGen *g, IrExecutable *executa...@@ -4283,22 +4296,21 @@ static LLVMValueRef ir_render_field_parent_ptr(CodeGen *g, IrExecutable *executa
4283 ZigType *container_type = container_ptr_type->data.pointer.child_type;4296 ZigType *container_type = container_ptr_type->data.pointer.child_type;
42844297
4285 size_t byte_offset = LLVMOffsetOfElement(g->target_data_ref,4298 size_t byte_offset = LLVMOffsetOfElement(g->target_data_ref,
4286 container_type->type_ref, instruction->field->gen_index);4299 get_llvm_type(g, container_type), instruction->field->gen_index);
42874300
4288 LLVMValueRef field_ptr_val = ir_llvm_value(g, instruction->field_ptr);4301 LLVMValueRef field_ptr_val = ir_llvm_value(g, instruction->field_ptr);
42894302
4290 if (byte_offset == 0) {4303 if (byte_offset == 0) {
4291 return LLVMBuildBitCast(g->builder, field_ptr_val, container_ptr_type->type_ref, "");4304 return LLVMBuildBitCast(g->builder, field_ptr_val, get_llvm_type(g, container_ptr_type), "");
4292 } else {4305 } else {
4293 ZigType *usize = g->builtin_types.entry_usize;4306 ZigType *usize = g->builtin_types.entry_usize;
42944307
4295 LLVMValueRef field_ptr_int = LLVMBuildPtrToInt(g->builder, field_ptr_val,4308 LLVMValueRef field_ptr_int = LLVMBuildPtrToInt(g->builder, field_ptr_val, usize->llvm_type, "");
4296 usize->type_ref, "");
42974309
4298 LLVMValueRef base_ptr_int = LLVMBuildNUWSub(g->builder, field_ptr_int,4310 LLVMValueRef base_ptr_int = LLVMBuildNUWSub(g->builder, field_ptr_int,
4299 LLVMConstInt(usize->type_ref, byte_offset, false), "");4311 LLVMConstInt(usize->llvm_type, byte_offset, false), "");
43004312
4301 return LLVMBuildIntToPtr(g->builder, base_ptr_int, container_ptr_type->type_ref, "");4313 return LLVMBuildIntToPtr(g->builder, base_ptr_int, get_llvm_type(g, container_ptr_type), "");
4302 }4314 }
4303}4315}
43044316
...@@ -4349,10 +4361,10 @@ static LLVMValueRef ir_render_align_cast(CodeGen *g, IrExecutable *executable, I...@@ -4349,10 +4361,10 @@ static LLVMValueRef ir_render_align_cast(CodeGen *g, IrExecutable *executable, I
4349 assert(align_bytes != 1);4361 assert(align_bytes != 1);
43504362
4351 ZigType *usize = g->builtin_types.entry_usize;4363 ZigType *usize = g->builtin_types.entry_usize;
4352 LLVMValueRef ptr_as_int_val = LLVMBuildPtrToInt(g->builder, ptr_val, usize->type_ref, "");4364 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);4365 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, "");4366 LLVMValueRef anded_val = LLVMBuildAnd(g->builder, ptr_as_int_val, alignment_minus_1, "");
4355 LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, anded_val, LLVMConstNull(usize->type_ref), "");4367 LLVMValueRef ok_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, anded_val, LLVMConstNull(usize->llvm_type), "");
43564368
4357 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "AlignCastOk");4369 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "AlignCastOk");
4358 LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn_val, "AlignCastFail");4370 LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn_val, "AlignCastFail");
...@@ -4373,7 +4385,7 @@ static LLVMValueRef ir_render_error_return_trace(CodeGen *g, IrExecutable *execu...@@ -4373,7 +4385,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);4385 LLVMValueRef cur_err_ret_trace_val = get_cur_err_ret_trace_val(g, instruction->base.scope);
4374 if (cur_err_ret_trace_val == nullptr) {4386 if (cur_err_ret_trace_val == nullptr) {
4375 ZigType *ptr_to_stack_trace_type = get_ptr_to_stack_trace_type(g);4387 ZigType *ptr_to_stack_trace_type = get_ptr_to_stack_trace_type(g);
4376 return LLVMConstNull(ptr_to_stack_trace_type->type_ref);4388 return LLVMConstNull(get_llvm_type(g, ptr_to_stack_trace_type));
4377 }4389 }
4378 return cur_err_ret_trace_val;4390 return cur_err_ret_trace_val;
4379}4391}
...@@ -4439,7 +4451,7 @@ static LLVMValueRef ir_render_cmpxchg(CodeGen *g, IrExecutable *executable, IrIn...@@ -4439,7 +4451,7 @@ static LLVMValueRef ir_render_cmpxchg(CodeGen *g, IrExecutable *executable, IrIn
4439 if (!handle_is_ptr(maybe_type)) {4451 if (!handle_is_ptr(maybe_type)) {
4440 LLVMValueRef payload_val = LLVMBuildExtractValue(g->builder, result_val, 0, "");4452 LLVMValueRef payload_val = LLVMBuildExtractValue(g->builder, result_val, 0, "");
4441 LLVMValueRef success_bit = LLVMBuildExtractValue(g->builder, result_val, 1, "");4453 LLVMValueRef success_bit = LLVMBuildExtractValue(g->builder, result_val, 1, "");
4442 return LLVMBuildSelect(g->builder, success_bit, LLVMConstNull(child_type->type_ref), payload_val, "");4454 return LLVMBuildSelect(g->builder, success_bit, LLVMConstNull(get_llvm_type(g, child_type)), payload_val, "");
4443 }4455 }
44444456
4445 assert(instruction->tmp_ptr != nullptr);4457 assert(instruction->tmp_ptr != nullptr);
...@@ -4470,10 +4482,10 @@ static LLVMValueRef ir_render_truncate(CodeGen *g, IrExecutable *executable, IrI...@@ -4470,10 +4482,10 @@ static LLVMValueRef ir_render_truncate(CodeGen *g, IrExecutable *executable, IrI
4470 // no-op4482 // no-op
4471 return target_val;4483 return target_val;
4472 } if (src_type->data.integral.bit_count == dest_type->data.integral.bit_count) {4484 } if (src_type->data.integral.bit_count == dest_type->data.integral.bit_count) {
4473 return LLVMBuildBitCast(g->builder, target_val, dest_type->type_ref, "");4485 return LLVMBuildBitCast(g->builder, target_val, get_llvm_type(g, dest_type), "");
4474 } else {4486 } else {
4475 LLVMValueRef target_val = ir_llvm_value(g, instruction->target);4487 LLVMValueRef target_val = ir_llvm_value(g, instruction->target);
4476 return LLVMBuildTrunc(g->builder, target_val, dest_type->type_ref, "");4488 return LLVMBuildTrunc(g->builder, target_val, get_llvm_type(g, dest_type), "");
4477 }4489 }
4478}4490}
44794491
...@@ -4539,12 +4551,12 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutable *executable, IrInst...@@ -4539,12 +4551,12 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutable *executable, IrInst
4539 if (instruction->end) {4551 if (instruction->end) {
4540 end_val = ir_llvm_value(g, instruction->end);4552 end_val = ir_llvm_value(g, instruction->end);
4541 } else {4553 } else {
4542 end_val = LLVMConstInt(g->builtin_types.entry_usize->type_ref, array_type->data.array.len, false);4554 end_val = LLVMConstInt(g->builtin_types.entry_usize->llvm_type, array_type->data.array.len, false);
4543 }4555 }
4544 if (want_runtime_safety) {4556 if (want_runtime_safety) {
4545 add_bounds_check(g, start_val, LLVMIntEQ, nullptr, LLVMIntULE, end_val);4557 add_bounds_check(g, start_val, LLVMIntEQ, nullptr, LLVMIntULE, end_val);
4546 if (instruction->end) {4558 if (instruction->end) {
4547 LLVMValueRef array_end = LLVMConstInt(g->builtin_types.entry_usize->type_ref,4559 LLVMValueRef array_end = LLVMConstInt(g->builtin_types.entry_usize->llvm_type,
4548 array_type->data.array.len, false);4560 array_type->data.array.len, false);
4549 add_bounds_check(g, end_val, LLVMIntEQ, nullptr, LLVMIntULE, array_end);4561 add_bounds_check(g, end_val, LLVMIntEQ, nullptr, LLVMIntULE, array_end);
4550 }4562 }
...@@ -4561,7 +4573,7 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutable *executable, IrInst...@@ -4561,7 +4573,7 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutable *executable, IrInst
45614573
4562 LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, slice_ptr_index, "");4574 LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, slice_ptr_index, "");
4563 LLVMValueRef indices[] = {4575 LLVMValueRef indices[] = {
4564 LLVMConstNull(g->builtin_types.entry_usize->type_ref),4576 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),
4565 start_val,4577 start_val,
4566 };4578 };
4567 LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP(g->builder, array_ptr, indices, 2, "");4579 LLVMValueRef slice_start_ptr = LLVMBuildInBoundsGEP(g->builder, array_ptr, indices, 2, "");
...@@ -4663,9 +4675,9 @@ static LLVMValueRef ir_render_breakpoint(CodeGen *g, IrExecutable *executable, I...@@ -4663,9 +4675,9 @@ static LLVMValueRef ir_render_breakpoint(CodeGen *g, IrExecutable *executable, I
4663static LLVMValueRef ir_render_return_address(CodeGen *g, IrExecutable *executable,4675static LLVMValueRef ir_render_return_address(CodeGen *g, IrExecutable *executable,
4664 IrInstructionReturnAddress *instruction)4676 IrInstructionReturnAddress *instruction)
4665{4677{
4666 LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_i32->type_ref);4678 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, "");4679 LLVMValueRef ptr_val = LLVMBuildCall(g->builder, get_return_address_fn_val(g), &zero, 1, "");
4668 return LLVMBuildPtrToInt(g->builder, ptr_val, g->builtin_types.entry_usize->type_ref, "");4680 return LLVMBuildPtrToInt(g->builder, ptr_val, g->builtin_types.entry_usize->llvm_type, "");
4669}4681}
46704682
4671static LLVMValueRef get_frame_address_fn_val(CodeGen *g) {4683static LLVMValueRef get_frame_address_fn_val(CodeGen *g) {
...@@ -4674,8 +4686,8 @@ static LLVMValueRef get_frame_address_fn_val(CodeGen *g) {...@@ -4674,8 +4686,8 @@ static LLVMValueRef get_frame_address_fn_val(CodeGen *g) {
46744686
4675 ZigType *return_type = get_pointer_to_type(g, g->builtin_types.entry_u8, true);4687 ZigType *return_type = get_pointer_to_type(g, g->builtin_types.entry_u8, true);
46764688
4677 LLVMTypeRef fn_type = LLVMFunctionType(return_type->type_ref,4689 LLVMTypeRef fn_type = LLVMFunctionType(get_llvm_type(g, return_type),
4678 &g->builtin_types.entry_i32->type_ref, 1, false);4690 &g->builtin_types.entry_i32->llvm_type, 1, false);
4679 g->frame_address_fn_val = LLVMAddFunction(g->module, "llvm.frameaddress", fn_type);4691 g->frame_address_fn_val = LLVMAddFunction(g->module, "llvm.frameaddress", fn_type);
4680 assert(LLVMGetIntrinsicID(g->frame_address_fn_val));4692 assert(LLVMGetIntrinsicID(g->frame_address_fn_val));
46814693
...@@ -4685,9 +4697,9 @@ static LLVMValueRef get_frame_address_fn_val(CodeGen *g) {...@@ -4685,9 +4697,9 @@ static LLVMValueRef get_frame_address_fn_val(CodeGen *g) {
4685static LLVMValueRef ir_render_frame_address(CodeGen *g, IrExecutable *executable,4697static LLVMValueRef ir_render_frame_address(CodeGen *g, IrExecutable *executable,
4686 IrInstructionFrameAddress *instruction)4698 IrInstructionFrameAddress *instruction)
4687{4699{
4688 LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_i32->type_ref);4700 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, "");4701 LLVMValueRef ptr_val = LLVMBuildCall(g->builder, get_frame_address_fn_val(g), &zero, 1, "");
4690 return LLVMBuildPtrToInt(g->builder, ptr_val, g->builtin_types.entry_usize->type_ref, "");4702 return LLVMBuildPtrToInt(g->builder, ptr_val, g->builtin_types.entry_usize->llvm_type, "");
4691}4703}
46924704
4693static LLVMValueRef get_handle_fn_val(CodeGen *g) {4705static LLVMValueRef get_handle_fn_val(CodeGen *g) {
...@@ -4706,7 +4718,7 @@ static LLVMValueRef get_handle_fn_val(CodeGen *g) {...@@ -4706,7 +4718,7 @@ static LLVMValueRef get_handle_fn_val(CodeGen *g) {
4706static LLVMValueRef ir_render_handle(CodeGen *g, IrExecutable *executable,4718static LLVMValueRef ir_render_handle(CodeGen *g, IrExecutable *executable,
4707 IrInstructionHandle *instruction)4719 IrInstructionHandle *instruction)
4708{4720{
4709 LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_promise->type_ref);4721 LLVMValueRef zero = LLVMConstNull(get_llvm_type(g, g->builtin_types.entry_promise));
4710 return LLVMBuildCall(g->builder, get_handle_fn_val(g), &zero, 0, "");4722 return LLVMBuildCall(g->builder, get_handle_fn_val(g), &zero, 0, "");
4711}4723}
47124724
...@@ -4786,7 +4798,7 @@ static LLVMValueRef ir_render_test_err(CodeGen *g, IrExecutable *executable, IrI...@@ -4786,7 +4798,7 @@ static LLVMValueRef ir_render_test_err(CodeGen *g, IrExecutable *executable, IrI
4786 err_val = err_union_handle;4798 err_val = err_union_handle;
4787 }4799 }
47884800
4789 LLVMValueRef zero = LLVMConstNull(g->err_tag_type->type_ref);4801 LLVMValueRef zero = LLVMConstNull(get_llvm_type(g, g->err_tag_type));
4790 return LLVMBuildICmp(g->builder, LLVMIntNE, err_val, zero, "");4802 return LLVMBuildICmp(g->builder, LLVMIntNE, err_val, zero, "");
4791}4803}
47924804
...@@ -4834,7 +4846,7 @@ static LLVMValueRef ir_render_unwrap_err_payload(CodeGen *g, IrExecutable *execu...@@ -4834,7 +4846,7 @@ static LLVMValueRef ir_render_unwrap_err_payload(CodeGen *g, IrExecutable *execu
4834 } else {4846 } else {
4835 err_val = err_union_handle;4847 err_val = err_union_handle;
4836 }4848 }
4837 LLVMValueRef zero = LLVMConstNull(g->err_tag_type->type_ref);4849 LLVMValueRef zero = LLVMConstNull(get_llvm_type(g, g->err_tag_type));
4838 LLVMValueRef cond_val = LLVMBuildICmp(g->builder, LLVMIntEQ, err_val, zero, "");4850 LLVMValueRef cond_val = LLVMBuildICmp(g->builder, LLVMIntEQ, err_val, zero, "");
4839 LLVMBasicBlockRef err_block = LLVMAppendBasicBlock(g->cur_fn_val, "UnwrapErrError");4851 LLVMBasicBlockRef err_block = LLVMAppendBasicBlock(g->cur_fn_val, "UnwrapErrError");
4840 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "UnwrapErrOk");4852 LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "UnwrapErrOk");
...@@ -4860,7 +4872,7 @@ static LLVMValueRef ir_render_maybe_wrap(CodeGen *g, IrExecutable *executable, I...@@ -4860,7 +4872,7 @@ static LLVMValueRef ir_render_maybe_wrap(CodeGen *g, IrExecutable *executable, I
48604872
4861 ZigType *child_type = wanted_type->data.maybe.child_type;4873 ZigType *child_type = wanted_type->data.maybe.child_type;
48624874
4863 if (child_type->zero_bits) {4875 if (!type_has_bits(child_type)) {
4864 return LLVMConstInt(LLVMInt1Type(), 1, false);4876 return LLVMConstInt(LLVMInt1Type(), 1, false);
4865 }4877 }
48664878
...@@ -4913,7 +4925,7 @@ static LLVMValueRef ir_render_err_wrap_payload(CodeGen *g, IrExecutable *executa...@@ -4913,7 +4925,7 @@ static LLVMValueRef ir_render_err_wrap_payload(CodeGen *g, IrExecutable *executa
4913 return ir_llvm_value(g, instruction->value);4925 return ir_llvm_value(g, instruction->value);
4914 }4926 }
49154927
4916 LLVMValueRef ok_err_val = LLVMConstNull(g->err_tag_type->type_ref);4928 LLVMValueRef ok_err_val = LLVMConstNull(get_llvm_type(g, g->err_tag_type));
49174929
4918 if (!type_has_bits(payload_type))4930 if (!type_has_bits(payload_type))
4919 return ok_err_val;4931 return ok_err_val;
...@@ -4991,7 +5003,7 @@ static LLVMValueRef ir_render_union_init(CodeGen *g, IrExecutable *executable, I...@@ -4991,7 +5003,7 @@ static LLVMValueRef ir_render_union_init(CodeGen *g, IrExecutable *executable, I
4991 LLVMValueRef tag_field_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr,5003 LLVMValueRef tag_field_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr,
4992 union_type->data.unionation.gen_tag_index, "");5004 union_type->data.unionation.gen_tag_index, "");
49935005
4994 LLVMValueRef tag_value = bigint_to_llvm_const(union_type->data.unionation.tag_type->type_ref,5006 LLVMValueRef tag_value = bigint_to_llvm_const(get_llvm_type(g, union_type->data.unionation.tag_type),
4995 &type_union_field->enum_field->value);5007 &type_union_field->enum_field->value);
4996 gen_store_untyped(g, tag_value, tag_field_ptr, 0, false);5008 gen_store_untyped(g, tag_value, tag_field_ptr, 0, false);
49975009
...@@ -5001,7 +5013,7 @@ static LLVMValueRef ir_render_union_init(CodeGen *g, IrExecutable *executable, I...@@ -5001,7 +5013,7 @@ static LLVMValueRef ir_render_union_init(CodeGen *g, IrExecutable *executable, I
5001 uncasted_union_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, (unsigned)0, "");5013 uncasted_union_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, (unsigned)0, "");
5002 }5014 }
50035015
5004 LLVMValueRef field_ptr = LLVMBuildBitCast(g->builder, uncasted_union_ptr, ptr_type->type_ref, "");5016 LLVMValueRef field_ptr = LLVMBuildBitCast(g->builder, uncasted_union_ptr, get_llvm_type(g, ptr_type), "");
5005 LLVMValueRef value = ir_llvm_value(g, instruction->init_value);5017 LLVMValueRef value = ir_llvm_value(g, instruction->init_value);
50065018
5007 gen_assign_raw(g, field_ptr, ptr_type, value);5019 gen_assign_raw(g, field_ptr, ptr_type, value);
...@@ -5023,8 +5035,8 @@ static LLVMValueRef ir_render_container_init_list(CodeGen *g, IrExecutable *exec...@@ -5023,8 +5035,8 @@ static LLVMValueRef ir_render_container_init_list(CodeGen *g, IrExecutable *exec
5023 for (size_t i = 0; i < field_count; i += 1) {5035 for (size_t i = 0; i < field_count; i += 1) {
5024 LLVMValueRef elem_val = ir_llvm_value(g, instruction->items[i]);5036 LLVMValueRef elem_val = ir_llvm_value(g, instruction->items[i]);
5025 LLVMValueRef indices[] = {5037 LLVMValueRef indices[] = {
5026 LLVMConstNull(g->builtin_types.entry_usize->type_ref),5038 LLVMConstNull(g->builtin_types.entry_usize->llvm_type),
5027 LLVMConstInt(g->builtin_types.entry_usize->type_ref, i, false),5039 LLVMConstInt(g->builtin_types.entry_usize->llvm_type, i, false),
5028 };5040 };
5029 LLVMValueRef elem_ptr = LLVMBuildInBoundsGEP(g->builder, tmp_array_ptr, indices, 2, "");5041 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);5042 gen_assign_raw(g, elem_ptr, get_pointer_to_type(g, child_type, false), elem_val);
...@@ -5041,7 +5053,7 @@ static LLVMValueRef ir_render_panic(CodeGen *g, IrExecutable *executable, IrInst...@@ -5041,7 +5053,7 @@ static LLVMValueRef ir_render_panic(CodeGen *g, IrExecutable *executable, IrInst
5041static LLVMValueRef ir_render_coro_id(CodeGen *g, IrExecutable *executable, IrInstructionCoroId *instruction) {5053static LLVMValueRef ir_render_coro_id(CodeGen *g, IrExecutable *executable, IrInstructionCoroId *instruction) {
5042 LLVMValueRef promise_ptr = ir_llvm_value(g, instruction->promise_ptr);5054 LLVMValueRef promise_ptr = ir_llvm_value(g, instruction->promise_ptr);
5043 LLVMValueRef align_val = LLVMConstInt(LLVMInt32Type(), get_coro_frame_align_bytes(g), false);5055 LLVMValueRef align_val = LLVMConstInt(LLVMInt32Type(), get_coro_frame_align_bytes(g), false);
5044 LLVMValueRef null = LLVMConstIntToPtr(LLVMConstNull(g->builtin_types.entry_usize->type_ref),5056 LLVMValueRef null = LLVMConstIntToPtr(LLVMConstNull(g->builtin_types.entry_usize->llvm_type),
5045 LLVMPointerType(LLVMInt8Type(), 0));5057 LLVMPointerType(LLVMInt8Type(), 0));
5046 LLVMValueRef params[] = {5058 LLVMValueRef params[] = {
5047 align_val,5059 align_val,
...@@ -5140,7 +5152,7 @@ static LLVMValueRef ir_render_coro_promise(CodeGen *g, IrExecutable *executable,...@@ -5140,7 +5152,7 @@ static LLVMValueRef ir_render_coro_promise(CodeGen *g, IrExecutable *executable,
5140 LLVMConstNull(LLVMInt1Type()),5152 LLVMConstNull(LLVMInt1Type()),
5141 };5153 };
5142 LLVMValueRef uncasted_result = LLVMBuildCall(g->builder, get_coro_promise_fn_val(g), params, 3, "");5154 LLVMValueRef uncasted_result = LLVMBuildCall(g->builder, get_coro_promise_fn_val(g), params, 3, "");
5143 return LLVMBuildBitCast(g->builder, uncasted_result, instruction->base.value.type->type_ref, "");5155 return LLVMBuildBitCast(g->builder, uncasted_result, get_llvm_type(g, instruction->base.value.type), "");
5144}5156}
51455157
5146static LLVMValueRef get_coro_alloc_helper_fn_val(CodeGen *g, LLVMTypeRef alloc_fn_type_ref, ZigType *fn_type) {5158static LLVMValueRef get_coro_alloc_helper_fn_val(CodeGen *g, LLVMTypeRef alloc_fn_type_ref, ZigType *fn_type) {
...@@ -5161,8 +5173,8 @@ static LLVMValueRef get_coro_alloc_helper_fn_val(CodeGen *g, LLVMTypeRef alloc_f...@@ -5161,8 +5173,8 @@ static LLVMValueRef get_coro_alloc_helper_fn_val(CodeGen *g, LLVMTypeRef alloc_f
5161 arg_types.append(alloc_fn_arg_types[1]);5173 arg_types.append(alloc_fn_arg_types[1]);
5162 }5174 }
5163 arg_types.append(alloc_fn_arg_types[g->have_err_ret_tracing ? 2 : 1]);5175 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);5176 arg_types.append(get_llvm_type(g, ptr_to_err_code_type));
5165 arg_types.append(g->builtin_types.entry_usize->type_ref);5177 arg_types.append(g->builtin_types.entry_usize->llvm_type);
51665178
5167 LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMPointerType(LLVMInt8Type(), 0),5179 LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMPointerType(LLVMInt8Type(), 0),
5168 arg_types.items, arg_types.length, false);5180 arg_types.items, arg_types.length, false);
...@@ -5204,7 +5216,7 @@ static LLVMValueRef get_coro_alloc_helper_fn_val(CodeGen *g, LLVMTypeRef alloc_f...@@ -5204,7 +5216,7 @@ static LLVMValueRef get_coro_alloc_helper_fn_val(CodeGen *g, LLVMTypeRef alloc_f
5204 next_arg += 1;5216 next_arg += 1;
5205 LLVMValueRef coro_size = LLVMGetParam(fn_val, next_arg);5217 LLVMValueRef coro_size = LLVMGetParam(fn_val, next_arg);
5206 next_arg += 1;5218 next_arg += 1;
5207 LLVMValueRef alignment_val = LLVMConstInt(g->builtin_types.entry_u29->type_ref,5219 LLVMValueRef alignment_val = LLVMConstInt(g->builtin_types.entry_u29->llvm_type,
5208 get_coro_frame_align_bytes(g), false);5220 get_coro_frame_align_bytes(g), false);
52095221
5210 ConstExprValue *zero_array = create_const_str_lit(g, buf_create_from_str(""));5222 ConstExprValue *zero_array = create_const_str_lit(g, buf_create_from_str(""));
...@@ -5219,7 +5231,7 @@ static LLVMValueRef get_coro_alloc_helper_fn_val(CodeGen *g, LLVMTypeRef alloc_f...@@ -5219,7 +5231,7 @@ static LLVMValueRef get_coro_alloc_helper_fn_val(CodeGen *g, LLVMTypeRef alloc_f
5219 }5231 }
5220 args.append(allocator_val);5232 args.append(allocator_val);
5221 args.append(undef_slice_zero->global_refs->llvm_global);5233 args.append(undef_slice_zero->global_refs->llvm_global);
5222 args.append(LLVMGetUndef(g->builtin_types.entry_u29->type_ref));5234 args.append(LLVMGetUndef(g->builtin_types.entry_u29->llvm_type));
5223 args.append(coro_size);5235 args.append(coro_size);
5224 args.append(alignment_val);5236 args.append(alignment_val);
5225 LLVMValueRef call_instruction = ZigLLVMBuildCall(g->builder, realloc_fn_val, args.items, args.length,5237 LLVMValueRef call_instruction = ZigLLVMBuildCall(g->builder, realloc_fn_val, args.items, args.length,
...@@ -5299,10 +5311,10 @@ static LLVMValueRef ir_render_atomic_rmw(CodeGen *g, IrExecutable *executable,...@@ -5299,10 +5311,10 @@ static LLVMValueRef ir_render_atomic_rmw(CodeGen *g, IrExecutable *executable,
52995311
5300 // it's a pointer but we need to treat it as an int5312 // it's a pointer but we need to treat it as an int
5301 LLVMValueRef casted_ptr = LLVMBuildBitCast(g->builder, ptr,5313 LLVMValueRef casted_ptr = LLVMBuildBitCast(g->builder, ptr,
5302 LLVMPointerType(g->builtin_types.entry_usize->type_ref, 0), "");5314 LLVMPointerType(g->builtin_types.entry_usize->llvm_type, 0), "");
5303 LLVMValueRef casted_operand = LLVMBuildPtrToInt(g->builder, operand, g->builtin_types.entry_usize->type_ref, "");5315 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);5316 LLVMValueRef uncasted_result = LLVMBuildAtomicRMW(g->builder, op, casted_ptr, casted_operand, ordering, false);
5305 return LLVMBuildIntToPtr(g->builder, uncasted_result, operand_type->type_ref, "");5317 return LLVMBuildIntToPtr(g->builder, uncasted_result, get_llvm_type(g, operand_type), "");
5306}5318}
53075319
5308static LLVMValueRef ir_render_atomic_load(CodeGen *g, IrExecutable *executable,5320static LLVMValueRef ir_render_atomic_load(CodeGen *g, IrExecutable *executable,
...@@ -5355,15 +5367,15 @@ static LLVMValueRef ir_render_bswap(CodeGen *g, IrExecutable *executable, IrInst...@@ -5355,15 +5367,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,5367 ZigType *extended_type = get_int_type(g, int_type->data.integral.is_signed,
5356 int_type->data.integral.bit_count + 8);5368 int_type->data.integral.bit_count + 8);
5357 // aabbcc5369 // aabbcc
5358 LLVMValueRef extended = LLVMBuildZExt(g->builder, op, extended_type->type_ref, "");5370 LLVMValueRef extended = LLVMBuildZExt(g->builder, op, get_llvm_type(g, extended_type), "");
5359 // 00aabbcc5371 // 00aabbcc
5360 LLVMValueRef fn_val = get_int_builtin_fn(g, extended_type, BuiltinFnIdBswap);5372 LLVMValueRef fn_val = get_int_builtin_fn(g, extended_type, BuiltinFnIdBswap);
5361 LLVMValueRef swapped = LLVMBuildCall(g->builder, fn_val, &extended, 1, "");5373 LLVMValueRef swapped = LLVMBuildCall(g->builder, fn_val, &extended, 1, "");
5362 // ccbbaa005374 // ccbbaa00
5363 LLVMValueRef shifted = ZigLLVMBuildLShrExact(g->builder, swapped,5375 LLVMValueRef shifted = ZigLLVMBuildLShrExact(g->builder, swapped,
5364 LLVMConstInt(extended_type->type_ref, 8, false), "");5376 LLVMConstInt(get_llvm_type(g, extended_type), 8, false), "");
5365 // 00ccbbaa5377 // 00ccbbaa
5366 return LLVMBuildTrunc(g->builder, shifted, int_type->type_ref, "");5378 return LLVMBuildTrunc(g->builder, shifted, get_llvm_type(g, int_type), "");
5367}5379}
53685380
5369static LLVMValueRef ir_render_bit_reverse(CodeGen *g, IrExecutable *executable, IrInstructionBitReverse *instruction) {5381static LLVMValueRef ir_render_bit_reverse(CodeGen *g, IrExecutable *executable, IrInstructionBitReverse *instruction) {
...@@ -5383,7 +5395,7 @@ static LLVMValueRef ir_render_vector_to_array(CodeGen *g, IrExecutable *executab...@@ -5383,7 +5395,7 @@ static LLVMValueRef ir_render_vector_to_array(CodeGen *g, IrExecutable *executab
5383 assert(instruction->tmp_ptr);5395 assert(instruction->tmp_ptr);
5384 LLVMValueRef vector = ir_llvm_value(g, instruction->vector);5396 LLVMValueRef vector = ir_llvm_value(g, instruction->vector);
5385 LLVMValueRef casted_ptr = LLVMBuildBitCast(g->builder, instruction->tmp_ptr,5397 LLVMValueRef casted_ptr = LLVMBuildBitCast(g->builder, instruction->tmp_ptr,
5386 LLVMPointerType(instruction->vector->value.type->type_ref, 0), "");5398 LLVMPointerType(get_llvm_type(g, instruction->vector->value.type), 0), "");
5387 gen_store_untyped(g, vector, casted_ptr, 0, false);5399 gen_store_untyped(g, vector, casted_ptr, 0, false);
5388 return instruction->tmp_ptr;5400 return instruction->tmp_ptr;
5389}5401}
...@@ -5396,7 +5408,7 @@ static LLVMValueRef ir_render_array_to_vector(CodeGen *g, IrExecutable *executab...@@ -5396,7 +5408,7 @@ static LLVMValueRef ir_render_array_to_vector(CodeGen *g, IrExecutable *executab
5396 assert(!handle_is_ptr(vector_type));5408 assert(!handle_is_ptr(vector_type));
5397 LLVMValueRef array_ptr = ir_llvm_value(g, instruction->array);5409 LLVMValueRef array_ptr = ir_llvm_value(g, instruction->array);
5398 LLVMValueRef casted_ptr = LLVMBuildBitCast(g->builder, array_ptr,5410 LLVMValueRef casted_ptr = LLVMBuildBitCast(g->builder, array_ptr,
5399 LLVMPointerType(vector_type->type_ref, 0), "");5411 LLVMPointerType(get_llvm_type(g, vector_type), 0), "");
5400 return gen_load_untyped(g, casted_ptr, 0, false, "");5412 return gen_load_untyped(g, casted_ptr, 0, false, "");
5401}5413}
54025414
...@@ -5685,6 +5697,7 @@ static void ir_render(CodeGen *g, ZigFn *fn_entry) {...@@ -5685,6 +5697,7 @@ static void ir_render(CodeGen *g, ZigFn *fn_entry) {
5685 IrInstruction *instruction = current_block->instruction_list.at(instr_i);5697 IrInstruction *instruction = current_block->instruction_list.at(instr_i);
5686 if (instruction->ref_count == 0 && !ir_has_side_effects(instruction))5698 if (instruction->ref_count == 0 && !ir_has_side_effects(instruction))
5687 continue;5699 continue;
5700
5688 instruction->llvm_value = ir_render_instruction(g, executable, instruction);5701 instruction->llvm_value = ir_render_instruction(g, executable, instruction);
5689 }5702 }
5690 current_block->llvm_exit_block = LLVMGetInsertBlock(g->builder);5703 current_block->llvm_exit_block = LLVMGetInsertBlock(g->builder);
...@@ -5735,15 +5748,15 @@ static LLVMValueRef gen_const_ptr_array_recursive(CodeGen *g, ConstExprValue *ar...@@ -5735,15 +5748,15 @@ static LLVMValueRef gen_const_ptr_array_recursive(CodeGen *g, ConstExprValue *ar
5735 if (el_type == LLVMArrayTypeKind) {5748 if (el_type == LLVMArrayTypeKind) {
5736 ZigType *usize = g->builtin_types.entry_usize;5749 ZigType *usize = g->builtin_types.entry_usize;
5737 LLVMValueRef indices[] = {5750 LLVMValueRef indices[] = {
5738 LLVMConstNull(usize->type_ref),5751 LLVMConstNull(usize->llvm_type),
5739 LLVMConstInt(usize->type_ref, index, false),5752 LLVMConstInt(usize->llvm_type, index, false),
5740 };5753 };
5741 return LLVMConstInBoundsGEP(base_ptr, indices, 2);5754 return LLVMConstInBoundsGEP(base_ptr, indices, 2);
5742 } else if (el_type == LLVMStructTypeKind) {5755 } else if (el_type == LLVMStructTypeKind) {
5743 ZigType *u32 = g->builtin_types.entry_u32;5756 ZigType *u32 = g->builtin_types.entry_u32;
5744 LLVMValueRef indices[] = {5757 LLVMValueRef indices[] = {
5745 LLVMConstNull(u32->type_ref),5758 LLVMConstNull(get_llvm_type(g, u32)),
5746 LLVMConstInt(u32->type_ref, index, false),5759 LLVMConstInt(get_llvm_type(g, u32), index, false),
5747 };5760 };
5748 return LLVMConstInBoundsGEP(base_ptr, indices, 2);5761 return LLVMConstInBoundsGEP(base_ptr, indices, 2);
5749 } else {5762 } else {
...@@ -5758,8 +5771,8 @@ static LLVMValueRef gen_const_ptr_struct_recursive(CodeGen *g, ConstExprValue *s...@@ -5758,8 +5771,8 @@ static LLVMValueRef gen_const_ptr_struct_recursive(CodeGen *g, ConstExprValue *s
57585771
5759 ZigType *u32 = g->builtin_types.entry_u32;5772 ZigType *u32 = g->builtin_types.entry_u32;
5760 LLVMValueRef indices[] = {5773 LLVMValueRef indices[] = {
5761 LLVMConstNull(u32->type_ref),5774 LLVMConstNull(get_llvm_type(g, u32)),
5762 LLVMConstInt(u32->type_ref, field_index, false),5775 LLVMConstInt(get_llvm_type(g, u32), field_index, false),
5763 };5776 };
5764 return LLVMConstInBoundsGEP(base_ptr, indices, 2);5777 return LLVMConstInBoundsGEP(base_ptr, indices, 2);
5765}5778}
...@@ -5770,8 +5783,8 @@ static LLVMValueRef gen_const_ptr_err_union_code_recursive(CodeGen *g, ConstExpr...@@ -5770,8 +5783,8 @@ static LLVMValueRef gen_const_ptr_err_union_code_recursive(CodeGen *g, ConstExpr
57705783
5771 ZigType *u32 = g->builtin_types.entry_u32;5784 ZigType *u32 = g->builtin_types.entry_u32;
5772 LLVMValueRef indices[] = {5785 LLVMValueRef indices[] = {
5773 LLVMConstNull(u32->type_ref),5786 LLVMConstNull(get_llvm_type(g, u32)),
5774 LLVMConstInt(u32->type_ref, err_union_err_index, false),5787 LLVMConstInt(get_llvm_type(g, u32), err_union_err_index, false),
5775 };5788 };
5776 return LLVMConstInBoundsGEP(base_ptr, indices, 2);5789 return LLVMConstInBoundsGEP(base_ptr, indices, 2);
5777}5790}
...@@ -5782,8 +5795,8 @@ static LLVMValueRef gen_const_ptr_err_union_payload_recursive(CodeGen *g, ConstE...@@ -5782,8 +5795,8 @@ static LLVMValueRef gen_const_ptr_err_union_payload_recursive(CodeGen *g, ConstE
57825795
5783 ZigType *u32 = g->builtin_types.entry_u32;5796 ZigType *u32 = g->builtin_types.entry_u32;
5784 LLVMValueRef indices[] = {5797 LLVMValueRef indices[] = {
5785 LLVMConstNull(u32->type_ref),5798 LLVMConstNull(get_llvm_type(g, u32)),
5786 LLVMConstInt(u32->type_ref, err_union_payload_index, false),5799 LLVMConstInt(get_llvm_type(g, u32), err_union_payload_index, false),
5787 };5800 };
5788 return LLVMConstInBoundsGEP(base_ptr, indices, 2);5801 return LLVMConstInBoundsGEP(base_ptr, indices, 2);
5789}5802}
...@@ -5794,8 +5807,8 @@ static LLVMValueRef gen_const_ptr_optional_payload_recursive(CodeGen *g, ConstEx...@@ -5794,8 +5807,8 @@ static LLVMValueRef gen_const_ptr_optional_payload_recursive(CodeGen *g, ConstEx
57945807
5795 ZigType *u32 = g->builtin_types.entry_u32;5808 ZigType *u32 = g->builtin_types.entry_u32;
5796 LLVMValueRef indices[] = {5809 LLVMValueRef indices[] = {
5797 LLVMConstNull(u32->type_ref),5810 LLVMConstNull(get_llvm_type(g, u32)),
5798 LLVMConstInt(u32->type_ref, maybe_child_index, false),5811 LLVMConstInt(get_llvm_type(g, u32), maybe_child_index, false),
5799 };5812 };
5800 return LLVMConstInBoundsGEP(base_ptr, indices, 2);5813 return LLVMConstInBoundsGEP(base_ptr, indices, 2);
5801}5814}
...@@ -5806,8 +5819,8 @@ static LLVMValueRef gen_const_ptr_union_recursive(CodeGen *g, ConstExprValue *un...@@ -5806,8 +5819,8 @@ static LLVMValueRef gen_const_ptr_union_recursive(CodeGen *g, ConstExprValue *un
58065819
5807 ZigType *u32 = g->builtin_types.entry_u32;5820 ZigType *u32 = g->builtin_types.entry_u32;
5808 LLVMValueRef indices[] = {5821 LLVMValueRef indices[] = {
5809 LLVMConstNull(u32->type_ref),5822 LLVMConstNull(get_llvm_type(g, u32)),
5810 LLVMConstInt(u32->type_ref, 0, false), // TODO test const union with more aligned tag type than payload5823 LLVMConstInt(get_llvm_type(g, u32), 0, false), // TODO test const union with more aligned tag type than payload
5811 };5824 };
5812 return LLVMConstInBoundsGEP(base_ptr, indices, 2);5825 return LLVMConstInBoundsGEP(base_ptr, indices, 2);
5813}5826}
...@@ -5823,7 +5836,7 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con...@@ -5823,7 +5836,7 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con
5823 }5836 }
58245837
5825 ZigType *type_entry = const_val->type;5838 ZigType *type_entry = const_val->type;
5826 assert(!type_entry->zero_bits);5839 assert(type_has_bits(type_entry));
5827 switch (type_entry->id) {5840 switch (type_entry->id) {
5828 case ZigTypeIdInvalid:5841 case ZigTypeIdInvalid:
5829 case ZigTypeIdMetaType:5842 case ZigTypeIdMetaType:
...@@ -5866,7 +5879,7 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con...@@ -5866,7 +5879,7 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con
5866 case ZigTypeIdPromise:5879 case ZigTypeIdPromise:
5867 {5880 {
5868 LLVMValueRef ptr_val = gen_const_val(g, const_val, "");5881 LLVMValueRef ptr_val = gen_const_val(g, const_val, "");
5869 LLVMValueRef ptr_size_int_val = LLVMConstPtrToInt(ptr_val, g->builtin_types.entry_usize->type_ref);5882 LLVMValueRef ptr_size_int_val = LLVMConstPtrToInt(ptr_val, g->builtin_types.entry_usize->llvm_type);
5870 return LLVMConstZExt(ptr_size_int_val, big_int_type_ref);5883 return LLVMConstZExt(ptr_size_int_val, big_int_type_ref);
5871 }5884 }
5872 case ZigTypeIdArray: {5885 case ZigTypeIdArray: {
...@@ -5933,8 +5946,8 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con...@@ -5933,8 +5946,8 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con
59335946
5934// We have this because union constants can't be represented by the official union type,5947// 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 constant5948// 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) {5949static bool is_llvm_value_unnamed_type(CodeGen *g, ZigType *type_entry, LLVMValueRef val) {
5937 return LLVMTypeOf(val) != type_entry->type_ref;5950 return LLVMTypeOf(val) != get_llvm_type(g, type_entry);
5938}5951}
59395952
5940static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, const char *name) {5953static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, const char *name) {
...@@ -5948,7 +5961,8 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con...@@ -5948,7 +5961,8 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
5948 ConstExprValue *pointee = const_val->data.x_ptr.data.ref.pointee;5961 ConstExprValue *pointee = const_val->data.x_ptr.data.ref.pointee;
5949 render_const_val(g, pointee, "");5962 render_const_val(g, pointee, "");
5950 render_const_val_global(g, pointee, "");5963 render_const_val_global(g, pointee, "");
5951 const_val->global_refs->llvm_value = LLVMConstBitCast(pointee->global_refs->llvm_global, const_val->type->type_ref);5964 const_val->global_refs->llvm_value = LLVMConstBitCast(pointee->global_refs->llvm_global,
5965 get_llvm_type(g, const_val->type));
5952 return const_val->global_refs->llvm_value;5966 return const_val->global_refs->llvm_value;
5953 }5967 }
5954 case ConstPtrSpecialBaseArray:5968 case ConstPtrSpecialBaseArray:
...@@ -5959,13 +5973,13 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con...@@ -5959,13 +5973,13 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
5959 if (!type_has_bits(array_const_val->type)) {5973 if (!type_has_bits(array_const_val->type)) {
5960 // make this a null pointer5974 // make this a null pointer
5961 ZigType *usize = g->builtin_types.entry_usize;5975 ZigType *usize = g->builtin_types.entry_usize;
5962 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->type_ref),5976 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type),
5963 const_val->type->type_ref);5977 get_llvm_type(g, const_val->type));
5964 return const_val->global_refs->llvm_value;5978 return const_val->global_refs->llvm_value;
5965 }5979 }
5966 size_t elem_index = const_val->data.x_ptr.data.base_array.elem_index;5980 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);5981 LLVMValueRef uncasted_ptr_val = gen_const_ptr_array_recursive(g, array_const_val, elem_index);
5968 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, const_val->type->type_ref);5982 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, get_llvm_type(g, const_val->type));
5969 const_val->global_refs->llvm_value = ptr_val;5983 const_val->global_refs->llvm_value = ptr_val;
5970 return ptr_val;5984 return ptr_val;
5971 }5985 }
...@@ -5977,15 +5991,15 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con...@@ -5977,15 +5991,15 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
5977 if (!type_has_bits(struct_const_val->type)) {5991 if (!type_has_bits(struct_const_val->type)) {
5978 // make this a null pointer5992 // make this a null pointer
5979 ZigType *usize = g->builtin_types.entry_usize;5993 ZigType *usize = g->builtin_types.entry_usize;
5980 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->type_ref),5994 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type),
5981 const_val->type->type_ref);5995 get_llvm_type(g, const_val->type));
5982 return const_val->global_refs->llvm_value;5996 return const_val->global_refs->llvm_value;
5983 }5997 }
5984 size_t src_field_index = const_val->data.x_ptr.data.base_struct.field_index;5998 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;5999 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,6000 LLVMValueRef uncasted_ptr_val = gen_const_ptr_struct_recursive(g, struct_const_val,
5987 gen_field_index);6001 gen_field_index);
5988 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, const_val->type->type_ref);6002 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, get_llvm_type(g, const_val->type));
5989 const_val->global_refs->llvm_value = ptr_val;6003 const_val->global_refs->llvm_value = ptr_val;
5990 return ptr_val;6004 return ptr_val;
5991 }6005 }
...@@ -5997,12 +6011,12 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con...@@ -5997,12 +6011,12 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
5997 if (!type_has_bits(err_union_const_val->type)) {6011 if (!type_has_bits(err_union_const_val->type)) {
5998 // make this a null pointer6012 // make this a null pointer
5999 ZigType *usize = g->builtin_types.entry_usize;6013 ZigType *usize = g->builtin_types.entry_usize;
6000 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->type_ref),6014 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type),
6001 const_val->type->type_ref);6015 get_llvm_type(g, const_val->type));
6002 return const_val->global_refs->llvm_value;6016 return const_val->global_refs->llvm_value;
6003 }6017 }
6004 LLVMValueRef uncasted_ptr_val = gen_const_ptr_err_union_code_recursive(g, err_union_const_val);6018 LLVMValueRef uncasted_ptr_val = gen_const_ptr_err_union_code_recursive(g, err_union_const_val);
6005 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, const_val->type->type_ref);6019 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, get_llvm_type(g, const_val->type));
6006 const_val->global_refs->llvm_value = ptr_val;6020 const_val->global_refs->llvm_value = ptr_val;
6007 return ptr_val;6021 return ptr_val;
6008 }6022 }
...@@ -6011,15 +6025,15 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con...@@ -6011,15 +6025,15 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
6011 assert(const_val->global_refs != nullptr);6025 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;6026 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);6027 assert(err_union_const_val->type->id == ZigTypeIdErrorUnion);
6014 if (err_union_const_val->type->zero_bits) {6028 if (!type_has_bits(err_union_const_val->type)) {
6015 // make this a null pointer6029 // make this a null pointer
6016 ZigType *usize = g->builtin_types.entry_usize;6030 ZigType *usize = g->builtin_types.entry_usize;
6017 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->type_ref),6031 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type),
6018 const_val->type->type_ref);6032 get_llvm_type(g, const_val->type));
6019 return const_val->global_refs->llvm_value;6033 return const_val->global_refs->llvm_value;
6020 }6034 }
6021 LLVMValueRef uncasted_ptr_val = gen_const_ptr_err_union_payload_recursive(g, err_union_const_val);6035 LLVMValueRef uncasted_ptr_val = gen_const_ptr_err_union_payload_recursive(g, err_union_const_val);
6022 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, const_val->type->type_ref);6036 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, get_llvm_type(g, const_val->type));
6023 const_val->global_refs->llvm_value = ptr_val;6037 const_val->global_refs->llvm_value = ptr_val;
6024 return ptr_val;6038 return ptr_val;
6025 }6039 }
...@@ -6028,15 +6042,15 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con...@@ -6028,15 +6042,15 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
6028 assert(const_val->global_refs != nullptr);6042 assert(const_val->global_refs != nullptr);
6029 ConstExprValue *optional_const_val = const_val->data.x_ptr.data.base_optional_payload.optional_val;6043 ConstExprValue *optional_const_val = const_val->data.x_ptr.data.base_optional_payload.optional_val;
6030 assert(optional_const_val->type->id == ZigTypeIdOptional);6044 assert(optional_const_val->type->id == ZigTypeIdOptional);
6031 if (optional_const_val->type->zero_bits) {6045 if (!type_has_bits(optional_const_val->type)) {
6032 // make this a null pointer6046 // make this a null pointer
6033 ZigType *usize = g->builtin_types.entry_usize;6047 ZigType *usize = g->builtin_types.entry_usize;
6034 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->type_ref),6048 const_val->global_refs->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->llvm_type),
6035 const_val->type->type_ref);6049 get_llvm_type(g, const_val->type));
6036 return const_val->global_refs->llvm_value;6050 return const_val->global_refs->llvm_value;
6037 }6051 }
6038 LLVMValueRef uncasted_ptr_val = gen_const_ptr_optional_payload_recursive(g, optional_const_val);6052 LLVMValueRef uncasted_ptr_val = gen_const_ptr_optional_payload_recursive(g, optional_const_val);
6039 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, const_val->type->type_ref);6053 LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, get_llvm_type(g, const_val->type));
6040 const_val->global_refs->llvm_value = ptr_val;6054 const_val->global_refs->llvm_value = ptr_val;
6041 return ptr_val;6055 return ptr_val;
6042 }6056 }
...@@ -6046,50 +6060,51 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con...@@ -6046,50 +6060,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;6060 uint64_t addr_value = const_val->data.x_ptr.data.hard_coded_addr.addr;
6047 ZigType *usize = g->builtin_types.entry_usize;6061 ZigType *usize = g->builtin_types.entry_usize;
6048 const_val->global_refs->llvm_value = LLVMConstIntToPtr(6062 const_val->global_refs->llvm_value = LLVMConstIntToPtr(
6049 LLVMConstInt(usize->type_ref, addr_value, false), const_val->type->type_ref);6063 LLVMConstInt(usize->llvm_type, addr_value, false), get_llvm_type(g, const_val->type));
6050 return const_val->global_refs->llvm_value;6064 return const_val->global_refs->llvm_value;
6051 }6065 }
6052 case ConstPtrSpecialFunction:6066 case ConstPtrSpecialFunction:
6053 return LLVMConstBitCast(fn_llvm_value(g, const_val->data.x_ptr.data.fn.fn_entry), const_val->type->type_ref);6067 return LLVMConstBitCast(fn_llvm_value(g, const_val->data.x_ptr.data.fn.fn_entry),
6068 get_llvm_type(g, const_val->type));
6054 case ConstPtrSpecialNull:6069 case ConstPtrSpecialNull:
6055 return LLVMConstNull(const_val->type->type_ref);6070 return LLVMConstNull(get_llvm_type(g, const_val->type));
6056 }6071 }
6057 zig_unreachable();6072 zig_unreachable();
6058}6073}
60596074
6060static LLVMValueRef gen_const_val_err_set(CodeGen *g, ConstExprValue *const_val, const char *name) {6075static 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;6076 uint64_t value = (const_val->data.x_err_set == nullptr) ? 0 : const_val->data.x_err_set->value;
6062 return LLVMConstInt(g->builtin_types.entry_global_error_set->type_ref, value, false);6077 return LLVMConstInt(get_llvm_type(g, g->builtin_types.entry_global_error_set), value, false);
6063}6078}
60646079
6065static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const char *name) {6080static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const char *name) {
6066 Error err;6081 Error err;
60676082
6068 ZigType *type_entry = const_val->type;6083 ZigType *type_entry = const_val->type;
6069 assert(!type_entry->zero_bits);6084 assert(type_has_bits(type_entry));
60706085
6071 switch (const_val->special) {6086 switch (const_val->special) {
6072 case ConstValSpecialRuntime:6087 case ConstValSpecialRuntime:
6073 zig_unreachable();6088 zig_unreachable();
6074 case ConstValSpecialUndef:6089 case ConstValSpecialUndef:
6075 return LLVMGetUndef(type_entry->type_ref);6090 return LLVMGetUndef(get_llvm_type(g, type_entry));
6076 case ConstValSpecialStatic:6091 case ConstValSpecialStatic:
6077 break;6092 break;
6078 }6093 }
60796094
6080 switch (type_entry->id) {6095 switch (type_entry->id) {
6081 case ZigTypeIdInt:6096 case ZigTypeIdInt:
6082 return bigint_to_llvm_const(type_entry->type_ref, &const_val->data.x_bigint);6097 return bigint_to_llvm_const(get_llvm_type(g, type_entry), &const_val->data.x_bigint);
6083 case ZigTypeIdErrorSet:6098 case ZigTypeIdErrorSet:
6084 return gen_const_val_err_set(g, const_val, name);6099 return gen_const_val_err_set(g, const_val, name);
6085 case ZigTypeIdFloat:6100 case ZigTypeIdFloat:
6086 switch (type_entry->data.floating.bit_count) {6101 switch (type_entry->data.floating.bit_count) {
6087 case 16:6102 case 16:
6088 return LLVMConstReal(type_entry->type_ref, zig_f16_to_double(const_val->data.x_f16));6103 return LLVMConstReal(get_llvm_type(g, type_entry), zig_f16_to_double(const_val->data.x_f16));
6089 case 32:6104 case 32:
6090 return LLVMConstReal(type_entry->type_ref, const_val->data.x_f32);6105 return LLVMConstReal(get_llvm_type(g, type_entry), const_val->data.x_f32);
6091 case 64:6106 case 64:
6092 return LLVMConstReal(type_entry->type_ref, const_val->data.x_f64);6107 return LLVMConstReal(get_llvm_type(g, type_entry), const_val->data.x_f64);
6093 case 128:6108 case 128:
6094 {6109 {
6095 // TODO make sure this is correct on big endian targets too6110 // TODO make sure this is correct on big endian targets too
...@@ -6097,7 +6112,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c...@@ -6097,7 +6112,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
6097 memcpy(buf, &const_val->data.x_f128, 16);6112 memcpy(buf, &const_val->data.x_f128, 16);
6098 LLVMValueRef as_int = LLVMConstIntOfArbitraryPrecision(LLVMInt128Type(), 2,6113 LLVMValueRef as_int = LLVMConstIntOfArbitraryPrecision(LLVMInt128Type(), 2,
6099 (uint64_t*)buf);6114 (uint64_t*)buf);
6100 return LLVMConstBitCast(as_int, type_entry->type_ref);6115 return LLVMConstBitCast(as_int, get_llvm_type(g, type_entry));
6101 }6116 }
6102 default:6117 default:
6103 zig_unreachable();6118 zig_unreachable();
...@@ -6125,9 +6140,9 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c...@@ -6125,9 +6140,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, "");6140 child_val = gen_const_val(g, const_val->data.x_optional, "");
6126 maybe_val = LLVMConstAllOnes(LLVMInt1Type());6141 maybe_val = LLVMConstAllOnes(LLVMInt1Type());
61276142
6128 make_unnamed_struct = is_llvm_value_unnamed_type(const_val->type, child_val);6143 make_unnamed_struct = is_llvm_value_unnamed_type(g, const_val->type, child_val);
6129 } else {6144 } else {
6130 child_val = LLVMGetUndef(child_type->type_ref);6145 child_val = LLVMGetUndef(get_llvm_type(g, child_type));
6131 maybe_val = LLVMConstNull(LLVMInt1Type());6146 maybe_val = LLVMConstNull(LLVMInt1Type());
61326147
6133 make_unnamed_struct = false;6148 make_unnamed_struct = false;
...@@ -6139,7 +6154,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c...@@ -6139,7 +6154,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
6139 if (make_unnamed_struct) {6154 if (make_unnamed_struct) {
6140 return LLVMConstStruct(fields, 2, false);6155 return LLVMConstStruct(fields, 2, false);
6141 } else {6156 } else {
6142 return LLVMConstNamedStruct(type_entry->type_ref, fields, 2);6157 return LLVMConstNamedStruct(get_llvm_type(g, type_entry), fields, 2);
6143 }6158 }
6144 }6159 }
6145 }6160 }
...@@ -6168,10 +6183,10 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c...@@ -6168,10 +6183,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];6183 ConstExprValue *field_val = &const_val->data.x_struct.fields[src_field_index];
6169 LLVMValueRef val = gen_const_val(g, field_val, "");6184 LLVMValueRef val = gen_const_val(g, field_val, "");
6170 fields[type_struct_field->gen_index] = val;6185 fields[type_struct_field->gen_index] = val;
6171 make_unnamed_struct = make_unnamed_struct || is_llvm_value_unnamed_type(field_val->type, val);6186 make_unnamed_struct = make_unnamed_struct || is_llvm_value_unnamed_type(g, field_val->type, val);
6172 } else {6187 } else {
6173 bool is_big_endian = g->is_big_endian; // TODO get endianness from struct type6188 bool is_big_endian = g->is_big_endian; // TODO get endianness from struct type
6174 LLVMTypeRef big_int_type_ref = LLVMStructGetTypeAtIndex(type_entry->type_ref,6189 LLVMTypeRef big_int_type_ref = LLVMStructGetTypeAtIndex(get_llvm_type(g, type_entry),
6175 (unsigned)type_struct_field->gen_index);6190 (unsigned)type_struct_field->gen_index);
6176 LLVMValueRef val = LLVMConstInt(big_int_type_ref, 0, false);6191 LLVMValueRef val = LLVMConstInt(big_int_type_ref, 0, false);
6177 size_t used_bits = 0;6192 size_t used_bits = 0;
...@@ -6216,14 +6231,14 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c...@@ -6216,14 +6231,14 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
62166231
6217 LLVMValueRef val = gen_const_val(g, field_val, "");6232 LLVMValueRef val = gen_const_val(g, field_val, "");
6218 fields[type_struct_field->gen_index] = val;6233 fields[type_struct_field->gen_index] = val;
6219 make_unnamed_struct = make_unnamed_struct || is_llvm_value_unnamed_type(field_val->type, val);6234 make_unnamed_struct = make_unnamed_struct || is_llvm_value_unnamed_type(g, field_val->type, val);
6220 }6235 }
6221 }6236 }
6222 if (make_unnamed_struct) {6237 if (make_unnamed_struct) {
6223 return LLVMConstStruct(fields, type_entry->data.structure.gen_field_count,6238 return LLVMConstStruct(fields, type_entry->data.structure.gen_field_count,
6224 type_entry->data.structure.layout == ContainerLayoutPacked);6239 type_entry->data.structure.layout == ContainerLayoutPacked);
6225 } else {6240 } else {
6226 return LLVMConstNamedStruct(type_entry->type_ref, fields, type_entry->data.structure.gen_field_count);6241 return LLVMConstNamedStruct(get_llvm_type(g, type_entry), fields, type_entry->data.structure.gen_field_count);
6227 }6242 }
6228 }6243 }
6229 case ZigTypeIdArray:6244 case ZigTypeIdArray:
...@@ -6231,16 +6246,16 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c...@@ -6231,16 +6246,16 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
6231 uint64_t len = type_entry->data.array.len;6246 uint64_t len = type_entry->data.array.len;
6232 switch (const_val->data.x_array.special) {6247 switch (const_val->data.x_array.special) {
6233 case ConstArraySpecialUndef:6248 case ConstArraySpecialUndef:
6234 return LLVMGetUndef(type_entry->type_ref);6249 return LLVMGetUndef(get_llvm_type(g, type_entry));
6235 case ConstArraySpecialNone: {6250 case ConstArraySpecialNone: {
6236 LLVMValueRef *values = allocate<LLVMValueRef>(len);6251 LLVMValueRef *values = allocate<LLVMValueRef>(len);
6237 LLVMTypeRef element_type_ref = type_entry->data.array.child_type->type_ref;6252 LLVMTypeRef element_type_ref = get_llvm_type(g, type_entry->data.array.child_type);
6238 bool make_unnamed_struct = false;6253 bool make_unnamed_struct = false;
6239 for (uint64_t i = 0; i < len; i += 1) {6254 for (uint64_t i = 0; i < len; i += 1) {
6240 ConstExprValue *elem_value = &const_val->data.x_array.data.s_none.elements[i];6255 ConstExprValue *elem_value = &const_val->data.x_array.data.s_none.elements[i];
6241 LLVMValueRef val = gen_const_val(g, elem_value, "");6256 LLVMValueRef val = gen_const_val(g, elem_value, "");
6242 values[i] = val;6257 values[i] = val;
6243 make_unnamed_struct = make_unnamed_struct || is_llvm_value_unnamed_type(elem_value->type, val);6258 make_unnamed_struct = make_unnamed_struct || is_llvm_value_unnamed_type(g, elem_value->type, val);
6244 }6259 }
6245 if (make_unnamed_struct) {6260 if (make_unnamed_struct) {
6246 return LLVMConstStruct(values, len, true);6261 return LLVMConstStruct(values, len, true);
...@@ -6259,7 +6274,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c...@@ -6259,7 +6274,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
6259 uint32_t len = type_entry->data.vector.len;6274 uint32_t len = type_entry->data.vector.len;
6260 switch (const_val->data.x_array.special) {6275 switch (const_val->data.x_array.special) {
6261 case ConstArraySpecialUndef:6276 case ConstArraySpecialUndef:
6262 return LLVMGetUndef(type_entry->type_ref);6277 return LLVMGetUndef(get_llvm_type(g, type_entry));
6263 case ConstArraySpecialNone: {6278 case ConstArraySpecialNone: {
6264 LLVMValueRef *values = allocate<LLVMValueRef>(len);6279 LLVMValueRef *values = allocate<LLVMValueRef>(len);
6265 for (uint64_t i = 0; i < len; i += 1) {6280 for (uint64_t i = 0; i < len; i += 1) {
...@@ -6273,7 +6288,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c...@@ -6273,7 +6288,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
6273 assert(buf_len(buf) == len);6288 assert(buf_len(buf) == len);
6274 LLVMValueRef *values = allocate<LLVMValueRef>(len);6289 LLVMValueRef *values = allocate<LLVMValueRef>(len);
6275 for (uint64_t i = 0; i < len; i += 1) {6290 for (uint64_t i = 0; i < len; i += 1) {
6276 values[i] = LLVMConstInt(g->builtin_types.entry_u8->type_ref, buf_ptr(buf)[i], false);6291 values[i] = LLVMConstInt(g->builtin_types.entry_u8->llvm_type, buf_ptr(buf)[i], false);
6277 }6292 }
6278 return LLVMConstVector(values, len);6293 return LLVMConstVector(values, len);
6279 }6294 }
...@@ -6282,31 +6297,36 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c...@@ -6282,31 +6297,36 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
6282 }6297 }
6283 case ZigTypeIdUnion:6298 case ZigTypeIdUnion:
6284 {6299 {
6285 LLVMTypeRef union_type_ref = type_entry->data.unionation.union_type_ref;6300 // Force type_entry->data.unionation.union_llvm_type to get resolved
6301 (void)get_llvm_type(g, type_entry);
62866302
6287 if (type_entry->data.unionation.gen_field_count == 0) {6303 if (type_entry->data.unionation.gen_field_count == 0) {
6288 if (type_entry->data.unionation.tag_type == nullptr) {6304 if (type_entry->data.unionation.tag_type == nullptr) {
6289 return nullptr;6305 return nullptr;
6290 } else {6306 } else {
6291 return bigint_to_llvm_const(type_entry->data.unionation.tag_type->type_ref,6307 return bigint_to_llvm_const(get_llvm_type(g, type_entry->data.unionation.tag_type),
6292 &const_val->data.x_union.tag);6308 &const_val->data.x_union.tag);
6293 }6309 }
6294 }6310 }
62956311
6312 LLVMTypeRef union_type_ref = type_entry->data.unionation.union_llvm_type;
6313 assert(union_type_ref != nullptr);
6314
6296 LLVMValueRef union_value_ref;6315 LLVMValueRef union_value_ref;
6297 bool make_unnamed_struct;6316 bool make_unnamed_struct;
6298 ConstExprValue *payload_value = const_val->data.x_union.payload;6317 ConstExprValue *payload_value = const_val->data.x_union.payload;
6299 if (payload_value == nullptr || !type_has_bits(payload_value->type)) {6318 if (payload_value == nullptr || !type_has_bits(payload_value->type)) {
6300 if (type_entry->data.unionation.gen_tag_index == SIZE_MAX)6319 if (type_entry->data.unionation.gen_tag_index == SIZE_MAX)
6301 return LLVMGetUndef(type_entry->type_ref);6320 return LLVMGetUndef(get_llvm_type(g, type_entry));
63026321
6303 union_value_ref = LLVMGetUndef(union_type_ref);6322 union_value_ref = LLVMGetUndef(union_type_ref);
6304 make_unnamed_struct = false;6323 make_unnamed_struct = false;
6305 } else {6324 } else {
6306 uint64_t field_type_bytes = LLVMStoreSizeOfType(g->target_data_ref, payload_value->type->type_ref);6325 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;6326 get_llvm_type(g, payload_value->type));
6327 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, "");6328 LLVMValueRef correctly_typed_value = gen_const_val(g, payload_value, "");
6309 make_unnamed_struct = is_llvm_value_unnamed_type(payload_value->type, correctly_typed_value) ||6329 make_unnamed_struct = is_llvm_value_unnamed_type(g, payload_value->type, correctly_typed_value) ||
6310 payload_value->type != type_entry->data.unionation.most_aligned_union_member;6330 payload_value->type != type_entry->data.unionation.most_aligned_union_member;
63116331
6312 {6332 {
...@@ -6329,7 +6349,8 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c...@@ -6329,7 +6349,8 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
6329 }6349 }
6330 }6350 }
63316351
6332 LLVMValueRef tag_value = bigint_to_llvm_const(type_entry->data.unionation.tag_type->type_ref,6352 LLVMValueRef tag_value = bigint_to_llvm_const(
6353 get_llvm_type(g, type_entry->data.unionation.tag_type),
6333 &const_val->data.x_union.tag);6354 &const_val->data.x_union.tag);
63346355
6335 LLVMValueRef fields[3];6356 LLVMValueRef fields[3];
...@@ -6341,7 +6362,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c...@@ -6341,7 +6362,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);6362 uint64_t last_field_offset = LLVMOffsetOfElement(g->target_data_ref, LLVMTypeOf(result), 1);
6342 uint64_t end_offset = last_field_offset +6363 uint64_t end_offset = last_field_offset +
6343 LLVMStoreSizeOfType(g->target_data_ref, LLVMTypeOf(fields[1]));6364 LLVMStoreSizeOfType(g->target_data_ref, LLVMTypeOf(fields[1]));
6344 uint64_t expected_sz = LLVMStoreSizeOfType(g->target_data_ref, type_entry->type_ref);6365 uint64_t expected_sz = LLVMStoreSizeOfType(g->target_data_ref, get_llvm_type(g, type_entry));
6345 unsigned pad_sz = expected_sz - end_offset;6366 unsigned pad_sz = expected_sz - end_offset;
6346 if (pad_sz != 0) {6367 if (pad_sz != 0) {
6347 fields[2] = LLVMGetUndef(LLVMArrayType(LLVMInt8Type(), pad_sz));6368 fields[2] = LLVMGetUndef(LLVMArrayType(LLVMInt8Type(), pad_sz));
...@@ -6351,21 +6372,21 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c...@@ -6351,21 +6372,21 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
6351 assert(actual_sz == expected_sz);6372 assert(actual_sz == expected_sz);
6352 return result;6373 return result;
6353 } else {6374 } else {
6354 return LLVMConstNamedStruct(type_entry->type_ref, fields, 2);6375 return LLVMConstNamedStruct(get_llvm_type(g, type_entry), fields, 2);
6355 }6376 }
63566377
6357 }6378 }
63586379
6359 case ZigTypeIdEnum:6380 case ZigTypeIdEnum:
6360 return bigint_to_llvm_const(type_entry->type_ref, &const_val->data.x_enum_tag);6381 return bigint_to_llvm_const(get_llvm_type(g, type_entry), &const_val->data.x_enum_tag);
6361 case ZigTypeIdFn:6382 case ZigTypeIdFn:
6362 if (const_val->data.x_ptr.special == ConstPtrSpecialFunction) {6383 if (const_val->data.x_ptr.special == ConstPtrSpecialFunction) {
6363 assert(const_val->data.x_ptr.mut == ConstPtrMutComptimeConst);6384 assert(const_val->data.x_ptr.mut == ConstPtrMutComptimeConst);
6364 return fn_llvm_value(g, const_val->data.x_ptr.data.fn.fn_entry);6385 return fn_llvm_value(g, const_val->data.x_ptr.data.fn.fn_entry);
6365 } else if (const_val->data.x_ptr.special == ConstPtrSpecialHardCodedAddr) {6386 } else if (const_val->data.x_ptr.special == ConstPtrSpecialHardCodedAddr) {
6366 LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->type_ref;6387 LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->llvm_type;
6367 uint64_t addr = const_val->data.x_ptr.data.hard_coded_addr.addr;6388 uint64_t addr = const_val->data.x_ptr.data.hard_coded_addr.addr;
6368 return LLVMConstIntToPtr(LLVMConstInt(usize_type_ref, addr, false), type_entry->type_ref);6389 return LLVMConstIntToPtr(LLVMConstInt(usize_type_ref, addr, false), get_llvm_type(g, type_entry));
6369 } else {6390 } else {
6370 zig_unreachable();6391 zig_unreachable();
6371 }6392 }
...@@ -6379,7 +6400,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c...@@ -6379,7 +6400,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
6379 assert(type_has_bits(err_set_type));6400 assert(type_has_bits(err_set_type));
6380 ErrorTableEntry *err_set = const_val->data.x_err_union.error_set->data.x_err_set;6401 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;6402 uint64_t value = (err_set == nullptr) ? 0 : err_set->value;
6382 return LLVMConstInt(g->err_tag_type->type_ref, value, false);6403 return LLVMConstInt(get_llvm_type(g, g->err_tag_type), value, false);
6383 } else if (!type_has_bits(err_set_type)) {6404 } else if (!type_has_bits(err_set_type)) {
6384 assert(type_has_bits(payload_type));6405 assert(type_has_bits(payload_type));
6385 return gen_const_val(g, const_val->data.x_err_union.payload, "");6406 return gen_const_val(g, const_val->data.x_err_union.payload, "");
...@@ -6389,17 +6410,17 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c...@@ -6389,17 +6410,17 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
6389 bool make_unnamed_struct;6410 bool make_unnamed_struct;
6390 ErrorTableEntry *err_set = const_val->data.x_err_union.error_set->data.x_err_set;6411 ErrorTableEntry *err_set = const_val->data.x_err_union.error_set->data.x_err_set;
6391 if (err_set != nullptr) {6412 if (err_set != nullptr) {
6392 err_tag_value = LLVMConstInt(g->err_tag_type->type_ref, err_set->value, false);6413 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);6414 err_payload_value = LLVMConstNull(get_llvm_type(g, payload_type));
6394 make_unnamed_struct = false;6415 make_unnamed_struct = false;
6395 } else {6416 } else {
6396 err_tag_value = LLVMConstNull(g->err_tag_type->type_ref);6417 err_tag_value = LLVMConstNull(get_llvm_type(g, g->err_tag_type));
6397 ConstExprValue *payload_val = const_val->data.x_err_union.payload;6418 ConstExprValue *payload_val = const_val->data.x_err_union.payload;
6398 err_payload_value = gen_const_val(g, payload_val, "");6419 err_payload_value = gen_const_val(g, payload_val, "");
6399 make_unnamed_struct = is_llvm_value_unnamed_type(payload_val->type, err_payload_value);6420 make_unnamed_struct = is_llvm_value_unnamed_type(g, payload_val->type, err_payload_value);
6400 }6421 }
6401 if (make_unnamed_struct) {6422 if (make_unnamed_struct) {
6402 uint64_t payload_off = LLVMOffsetOfElement(g->target_data_ref, type_entry->type_ref, 1);6423 uint64_t payload_off = LLVMOffsetOfElement(g->target_data_ref, get_llvm_type(g, type_entry), 1);
6403 uint64_t err_sz = LLVMStoreSizeOfType(g->target_data_ref, LLVMTypeOf(err_tag_value));6424 uint64_t err_sz = LLVMStoreSizeOfType(g->target_data_ref, LLVMTypeOf(err_tag_value));
6404 unsigned pad_sz = payload_off - err_sz;6425 unsigned pad_sz = payload_off - err_sz;
6405 if (pad_sz == 0) {6426 if (pad_sz == 0) {
...@@ -6421,7 +6442,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c...@@ -6421,7 +6442,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
6421 err_tag_value,6442 err_tag_value,
6422 err_payload_value,6443 err_payload_value,
6423 };6444 };
6424 return LLVMConstNamedStruct(type_entry->type_ref, fields, 2);6445 return LLVMConstNamedStruct(get_llvm_type(g, type_entry), fields, 2);
6425 }6446 }
6426 }6447 }
6427 }6448 }
...@@ -6460,7 +6481,8 @@ static void render_const_val_global(CodeGen *g, ConstExprValue *const_val, const...@@ -6460,7 +6481,8 @@ static void render_const_val_global(CodeGen *g, ConstExprValue *const_val, const
6460 const_val->global_refs = allocate<ConstGlobalRefs>(1);6481 const_val->global_refs = allocate<ConstGlobalRefs>(1);
64616482
6462 if (!const_val->global_refs->llvm_global) {6483 if (!const_val->global_refs->llvm_global) {
6463 LLVMTypeRef type_ref = const_val->global_refs->llvm_value ? LLVMTypeOf(const_val->global_refs->llvm_value) : const_val->type->type_ref;6484 LLVMTypeRef type_ref = const_val->global_refs->llvm_value ?
6485 LLVMTypeOf(const_val->global_refs->llvm_value) : get_llvm_type(g, const_val->type);
6464 LLVMValueRef global_value = LLVMAddGlobal(g->module, type_ref, name);6486 LLVMValueRef global_value = LLVMAddGlobal(g->module, type_ref, name);
6465 LLVMSetLinkage(global_value, LLVMInternalLinkage);6487 LLVMSetLinkage(global_value, LLVMInternalLinkage);
6466 LLVMSetGlobalConstant(global_value, true);6488 LLVMSetGlobalConstant(global_value, true);
...@@ -6486,7 +6508,7 @@ static void generate_error_name_table(CodeGen *g) {...@@ -6486,7 +6508,7 @@ static void generate_error_name_table(CodeGen *g) {
6486 ZigType *str_type = get_slice_type(g, u8_ptr_type);6508 ZigType *str_type = get_slice_type(g, u8_ptr_type);
64876509
6488 LLVMValueRef *values = allocate<LLVMValueRef>(g->errors_by_index.length);6510 LLVMValueRef *values = allocate<LLVMValueRef>(g->errors_by_index.length);
6489 values[0] = LLVMGetUndef(str_type->type_ref);6511 values[0] = LLVMGetUndef(get_llvm_type(g, str_type));
6490 for (size_t i = 1; i < g->errors_by_index.length; i += 1) {6512 for (size_t i = 1; i < g->errors_by_index.length; i += 1) {
6491 ErrorTableEntry *err_entry = g->errors_by_index.at(i);6513 ErrorTableEntry *err_entry = g->errors_by_index.at(i);
6492 Buf *name = &err_entry->name;6514 Buf *name = &err_entry->name;
...@@ -6502,13 +6524,13 @@ static void generate_error_name_table(CodeGen *g) {...@@ -6502,13 +6524,13 @@ static void generate_error_name_table(CodeGen *g) {
6502 LLVMSetAlignment(str_global, LLVMABIAlignmentOfType(g->target_data_ref, LLVMTypeOf(str_init)));6524 LLVMSetAlignment(str_global, LLVMABIAlignmentOfType(g->target_data_ref, LLVMTypeOf(str_init)));
65036525
6504 LLVMValueRef fields[] = {6526 LLVMValueRef fields[] = {
6505 LLVMConstBitCast(str_global, u8_ptr_type->type_ref),6527 LLVMConstBitCast(str_global, get_llvm_type(g, u8_ptr_type)),
6506 LLVMConstInt(g->builtin_types.entry_usize->type_ref, buf_len(name), false),6528 LLVMConstInt(g->builtin_types.entry_usize->llvm_type, buf_len(name), false),
6507 };6529 };
6508 values[i] = LLVMConstNamedStruct(str_type->type_ref, fields, 2);6530 values[i] = LLVMConstNamedStruct(get_llvm_type(g, str_type), fields, 2);
6509 }6531 }
65106532
6511 LLVMValueRef err_name_table_init = LLVMConstArray(str_type->type_ref, values, (unsigned)g->errors_by_index.length);6533 LLVMValueRef err_name_table_init = LLVMConstArray(get_llvm_type(g, str_type), values, (unsigned)g->errors_by_index.length);
65126534
6513 g->err_name_table = LLVMAddGlobal(g->module, LLVMTypeOf(err_name_table_init),6535 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)));6536 buf_ptr(get_mangled_name(g, buf_create_from_str("__zig_err_name_table"), false)));
...@@ -6547,7 +6569,7 @@ static void gen_global_var(CodeGen *g, ZigVar *var, LLVMValueRef init_val,...@@ -6547,7 +6569,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),6569 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,6570 buf_ptr(&var->name), import->data.structure.root_struct->di_file,
6549 (unsigned)(var->decl_node->line + 1),6571 (unsigned)(var->decl_node->line + 1),
6550 type_entry->di_type, is_local_to_unit);6572 get_llvm_di_type(g, type_entry), is_local_to_unit);
65516573
6552 // TODO ^^ make an actual global variable6574 // TODO ^^ make an actual global variable
6553}6575}
...@@ -6588,28 +6610,6 @@ static LLVMLinkage var_linkage_to_llvm(VarLinkage var_linkage) {...@@ -6588,28 +6610,6 @@ static LLVMLinkage var_linkage_to_llvm(VarLinkage var_linkage) {
6588static void do_code_gen(CodeGen *g) {6610static void do_code_gen(CodeGen *g) {
6589 assert(!g->errors.length);6611 assert(!g->errors.length);
65906612
6591 {
6592 // create debug type for error sets
6593 assert(g->err_enumerators.length == g->errors_by_index.length);
6594 uint64_t tag_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, g->err_tag_type->type_ref);
6595 uint64_t tag_debug_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, g->err_tag_type->type_ref);
6596 ZigLLVMDIFile *err_set_di_file = nullptr;
6597 ZigLLVMDIType *err_set_di_type = ZigLLVMCreateDebugEnumerationType(g->dbuilder,
6598 ZigLLVMCompileUnitToScope(g->compile_unit), buf_ptr(&g->builtin_types.entry_global_error_set->name),
6599 err_set_di_file, 0,
6600 tag_debug_size_in_bits,
6601 tag_debug_align_in_bits,
6602 g->err_enumerators.items, g->err_enumerators.length,
6603 g->err_tag_type->di_type, "");
6604 ZigLLVMReplaceTemporary(g->dbuilder, g->builtin_types.entry_global_error_set->di_type, err_set_di_type);
6605 g->builtin_types.entry_global_error_set->di_type = err_set_di_type;
6606
6607 for (size_t i = 0; i < g->error_di_types.length; i += 1) {
6608 ZigLLVMDIType **di_type_ptr = g->error_di_types.at(i);
6609 *di_type_ptr = err_set_di_type;
6610 }
6611 }
6612
6613 generate_error_name_table(g);6613 generate_error_name_table(g);
66146614
6615 // Generate module level variables6615 // Generate module level variables
...@@ -6646,7 +6646,7 @@ static void do_code_gen(CodeGen *g) {...@@ -6646,7 +6646,7 @@ static void do_code_gen(CodeGen *g) {
6646 bits_needed = 8;6646 bits_needed = 8;
6647 }6647 }
6648 ZigType *var_type = get_int_type(g, const_val->data.x_bigint.is_negative, bits_needed);6648 ZigType *var_type = get_int_type(g, const_val->data.x_bigint.is_negative, bits_needed);
6649 LLVMValueRef init_val = bigint_to_llvm_const(var_type->type_ref, &const_val->data.x_bigint);6649 LLVMValueRef init_val = bigint_to_llvm_const(get_llvm_type(g, var_type), &const_val->data.x_bigint);
6650 gen_global_var(g, var, init_val, var_type);6650 gen_global_var(g, var, init_val, var_type);
6651 continue;6651 continue;
6652 }6652 }
...@@ -6660,9 +6660,10 @@ static void do_code_gen(CodeGen *g) {...@@ -6660,9 +6660,10 @@ static void do_code_gen(CodeGen *g) {
6660 if (var->linkage == VarLinkageExternal) {6660 if (var->linkage == VarLinkageExternal) {
6661 LLVMValueRef existing_llvm_var = LLVMGetNamedGlobal(g->module, buf_ptr(&var->name));6661 LLVMValueRef existing_llvm_var = LLVMGetNamedGlobal(g->module, buf_ptr(&var->name));
6662 if (existing_llvm_var) {6662 if (existing_llvm_var) {
6663 global_value = LLVMConstBitCast(existing_llvm_var, LLVMPointerType(var->var_type->type_ref, 0));6663 global_value = LLVMConstBitCast(existing_llvm_var,
6664 LLVMPointerType(get_llvm_type(g, var->var_type), 0));
6664 } else {6665 } else {
6665 global_value = LLVMAddGlobal(g->module, var->var_type->type_ref, buf_ptr(&var->name));6666 global_value = LLVMAddGlobal(g->module, get_llvm_type(g, var->var_type), buf_ptr(&var->name));
6666 // TODO debug info for the extern variable6667 // TODO debug info for the extern variable
66676668
6668 LLVMSetLinkage(global_value, var_linkage_to_llvm(var->linkage));6669 LLVMSetLinkage(global_value, var_linkage_to_llvm(var->linkage));
...@@ -6736,6 +6737,9 @@ static void do_code_gen(CodeGen *g) {...@@ -6736,6 +6737,9 @@ static void do_code_gen(CodeGen *g) {
6736 if (have_err_ret_trace_stack) {6737 if (have_err_ret_trace_stack) {
6737 ZigType *array_type = get_array_type(g, g->builtin_types.entry_usize, stack_trace_ptr_count);6738 ZigType *array_type = get_array_type(g, g->builtin_types.entry_usize, stack_trace_ptr_count);
6738 err_ret_array_val = build_alloca(g, array_type, "error_return_trace_addresses", get_abi_alignment(g, array_type));6739 err_ret_array_val = build_alloca(g, array_type, "error_return_trace_addresses", get_abi_alignment(g, array_type));
6740
6741 // populate g->stack_trace_type
6742 (void)get_ptr_to_stack_trace_type(g);
6739 g->cur_err_ret_trace_val_stack = build_alloca(g, g->stack_trace_type, "error_return_trace", get_abi_alignment(g, g->stack_trace_type));6743 g->cur_err_ret_trace_val_stack = build_alloca(g, g->stack_trace_type, "error_return_trace", get_abi_alignment(g, g->stack_trace_type));
6740 } else {6744 } else {
6741 g->cur_err_ret_trace_val_stack = nullptr;6745 g->cur_err_ret_trace_val_stack = nullptr;
...@@ -6834,15 +6838,15 @@ static void do_code_gen(CodeGen *g) {...@@ -6834,15 +6838,15 @@ static void do_code_gen(CodeGen *g) {
68346838
6835 var->di_loc_var = ZigLLVMCreateAutoVariable(g->dbuilder, get_di_scope(g, var->parent_scope),6839 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),6840 buf_ptr(&var->name), import->data.structure.root_struct->di_file, (unsigned)(var->decl_node->line + 1),
6837 var->var_type->di_type, !g->strip_debug_symbols, 0);6841 get_llvm_di_type(g, var->var_type), !g->strip_debug_symbols, 0);
68386842
6839 } else if (is_c_abi) {6843 } else if (is_c_abi) {
6840 fn_walk_var.data.vars.var = var;6844 fn_walk_var.data.vars.var = var;
6841 iter_function_params_c_abi(g, fn_table_entry->type_entry, &fn_walk_var, var->src_arg_index);6845 iter_function_params_c_abi(g, fn_table_entry->type_entry, &fn_walk_var, var->src_arg_index);
6842 } else {6846 } else {
6843 assert(var->gen_arg_index != SIZE_MAX);
6844 ZigType *gen_type;6847 ZigType *gen_type;
6845 FnGenParamInfo *gen_info = &fn_table_entry->type_entry->data.fn.gen_param_info[var->src_arg_index];6848 FnGenParamInfo *gen_info = &fn_table_entry->type_entry->data.fn.gen_param_info[var->src_arg_index];
6849 assert(gen_info->gen_index != SIZE_MAX);
68466850
6847 if (handle_is_ptr(var->var_type)) {6851 if (handle_is_ptr(var->var_type)) {
6848 if (gen_info->is_byval) {6852 if (gen_info->is_byval) {
...@@ -6850,16 +6854,16 @@ static void do_code_gen(CodeGen *g) {...@@ -6850,16 +6854,16 @@ static void do_code_gen(CodeGen *g) {
6850 } else {6854 } else {
6851 gen_type = gen_info->type;6855 gen_type = gen_info->type;
6852 }6856 }
6853 var->value_ref = LLVMGetParam(fn, (unsigned)var->gen_arg_index);6857 var->value_ref = LLVMGetParam(fn, gen_info->gen_index);
6854 } else {6858 } else {
6855 gen_type = var->var_type;6859 gen_type = var->var_type;
6856 var->value_ref = build_alloca(g, var->var_type, buf_ptr(&var->name), var->align_bytes);6860 var->value_ref = build_alloca(g, var->var_type, buf_ptr(&var->name), var->align_bytes);
6857 }6861 }
6858 if (var->decl_node) {6862 if (var->decl_node) {
6859 var->di_loc_var = ZigLLVMCreateParameterVariable(g->dbuilder, get_di_scope(g, var->parent_scope),6863 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,6864 buf_ptr(&var->name), import->data.structure.root_struct->di_file,
6861 (unsigned)(var->decl_node->line + 1),6865 (unsigned)(var->decl_node->line + 1),
6862 gen_type->di_type, !g->strip_debug_symbols, 0, (unsigned)(var->gen_arg_index + 1));6866 get_llvm_di_type(g, gen_type), !g->strip_debug_symbols, 0, (unsigned)(gen_info->gen_index+1));
6863 }6867 }
68646868
6865 }6869 }
...@@ -6870,7 +6874,7 @@ static void do_code_gen(CodeGen *g) {...@@ -6870,7 +6874,7 @@ static void do_code_gen(CodeGen *g) {
6870 ZigType *usize = g->builtin_types.entry_usize;6874 ZigType *usize = g->builtin_types.entry_usize;
6871 size_t index_field_index = g->stack_trace_type->data.structure.fields[0].gen_index;6875 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, "");6876 LLVMValueRef index_field_ptr = LLVMBuildStructGEP(g->builder, g->cur_err_ret_trace_val_stack, (unsigned)index_field_index, "");
6873 gen_store_untyped(g, LLVMConstNull(usize->type_ref), index_field_ptr, 0, false);6877 gen_store_untyped(g, LLVMConstNull(usize->llvm_type), index_field_ptr, 0, false);
68746878
6875 size_t addresses_field_index = g->stack_trace_type->data.structure.fields[1].gen_index;6879 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, "");6880 LLVMValueRef addresses_field_ptr = LLVMBuildStructGEP(g->builder, g->cur_err_ret_trace_val_stack, (unsigned)addresses_field_index, "");
...@@ -6878,7 +6882,7 @@ static void do_code_gen(CodeGen *g) {...@@ -6878,7 +6882,7 @@ static void do_code_gen(CodeGen *g) {
6878 ZigType *slice_type = g->stack_trace_type->data.structure.fields[1].type_entry;6882 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;6883 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, "");6884 LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, addresses_field_ptr, (unsigned)ptr_field_index, "");
6881 LLVMValueRef zero = LLVMConstNull(usize->type_ref);6885 LLVMValueRef zero = LLVMConstNull(usize->llvm_type);
6882 LLVMValueRef indices[] = {zero, zero};6886 LLVMValueRef indices[] = {zero, zero};
6883 LLVMValueRef err_ret_array_val_elem0_ptr = LLVMBuildInBoundsGEP(g->builder, err_ret_array_val,6887 LLVMValueRef err_ret_array_val_elem0_ptr = LLVMBuildInBoundsGEP(g->builder, err_ret_array_val,
6884 indices, 2, "");6888 indices, 2, "");
...@@ -6887,7 +6891,7 @@ static void do_code_gen(CodeGen *g) {...@@ -6887,7 +6891,7 @@ static void do_code_gen(CodeGen *g) {
68876891
6888 size_t len_field_index = slice_type->data.structure.fields[slice_len_index].gen_index;6892 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, "");6893 LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, addresses_field_ptr, (unsigned)len_field_index, "");
6890 gen_store(g, LLVMConstInt(usize->type_ref, stack_trace_ptr_count, false), len_field_ptr, get_pointer_to_type(g, usize, false));6894 gen_store(g, LLVMConstInt(usize->llvm_type, stack_trace_ptr_count, false), len_field_ptr, get_pointer_to_type(g, usize, false));
6891 }6895 }
68926896
6893 // create debug variable declarations for parameters6897 // create debug variable declarations for parameters
...@@ -6997,50 +7001,60 @@ static const GlobalLinkageValue global_linkage_values[] = {...@@ -6997,50 +7001,60 @@ static const GlobalLinkageValue global_linkage_values[] = {
6997 {GlobalLinkageIdLinkOnce, "LinkOnce"},7001 {GlobalLinkageIdLinkOnce, "LinkOnce"},
6998};7002};
69997003
7004static void add_fp_entry(CodeGen *g, const char *name, uint32_t bit_count, LLVMTypeRef type_ref,
7005 ZigType **field)
7006{
7007 ZigType *entry = new_type_table_entry(ZigTypeIdFloat);
7008 entry->llvm_type = type_ref;
7009 entry->size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, entry->llvm_type);
7010 entry->abi_size = LLVMABISizeOfType(g->target_data_ref, entry->llvm_type);
7011 entry->abi_align = LLVMABIAlignmentOfType(g->target_data_ref, entry->llvm_type);
7012 buf_init_from_str(&entry->name, name);
7013 entry->data.floating.bit_count = bit_count;
7014
7015 entry->llvm_di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
7016 entry->size_in_bits, ZigLLVMEncoding_DW_ATE_float());
7017 *field = entry;
7018 g->primitive_type_table.put(&entry->name, entry);
7019}
7020
7000static void define_builtin_types(CodeGen *g) {7021static void define_builtin_types(CodeGen *g) {
7001 {7022 {
7002 // if this type is anywhere in the AST, we should never hit codegen.7023 // if this type is anywhere in the AST, we should never hit codegen.
7003 ZigType *entry = new_type_table_entry(ZigTypeIdInvalid);7024 ZigType *entry = new_type_table_entry(ZigTypeIdInvalid);
7004 buf_init_from_str(&entry->name, "(invalid)");7025 buf_init_from_str(&entry->name, "(invalid)");
7005 entry->zero_bits = true;
7006 g->builtin_types.entry_invalid = entry;7026 g->builtin_types.entry_invalid = entry;
7007 }7027 }
7008 {7028 {
7009 ZigType *entry = new_type_table_entry(ZigTypeIdComptimeFloat);7029 ZigType *entry = new_type_table_entry(ZigTypeIdComptimeFloat);
7010 buf_init_from_str(&entry->name, "comptime_float");7030 buf_init_from_str(&entry->name, "comptime_float");
7011 entry->zero_bits = true;
7012 g->builtin_types.entry_num_lit_float = entry;7031 g->builtin_types.entry_num_lit_float = entry;
7013 g->primitive_type_table.put(&entry->name, entry);7032 g->primitive_type_table.put(&entry->name, entry);
7014 }7033 }
7015 {7034 {
7016 ZigType *entry = new_type_table_entry(ZigTypeIdComptimeInt);7035 ZigType *entry = new_type_table_entry(ZigTypeIdComptimeInt);
7017 buf_init_from_str(&entry->name, "comptime_int");7036 buf_init_from_str(&entry->name, "comptime_int");
7018 entry->zero_bits = true;
7019 g->builtin_types.entry_num_lit_int = entry;7037 g->builtin_types.entry_num_lit_int = entry;
7020 g->primitive_type_table.put(&entry->name, entry);7038 g->primitive_type_table.put(&entry->name, entry);
7021 }7039 }
7022 {7040 {
7023 ZigType *entry = new_type_table_entry(ZigTypeIdEnumLiteral);7041 ZigType *entry = new_type_table_entry(ZigTypeIdEnumLiteral);
7024 buf_init_from_str(&entry->name, "(enum literal)");7042 buf_init_from_str(&entry->name, "(enum literal)");
7025 entry->zero_bits = true;
7026 g->builtin_types.entry_enum_literal = entry;7043 g->builtin_types.entry_enum_literal = entry;
7027 }7044 }
7028 {7045 {
7029 ZigType *entry = new_type_table_entry(ZigTypeIdUndefined);7046 ZigType *entry = new_type_table_entry(ZigTypeIdUndefined);
7030 buf_init_from_str(&entry->name, "(undefined)");7047 buf_init_from_str(&entry->name, "(undefined)");
7031 entry->zero_bits = true;
7032 g->builtin_types.entry_undef = entry;7048 g->builtin_types.entry_undef = entry;
7033 }7049 }
7034 {7050 {
7035 ZigType *entry = new_type_table_entry(ZigTypeIdNull);7051 ZigType *entry = new_type_table_entry(ZigTypeIdNull);
7036 buf_init_from_str(&entry->name, "(null)");7052 buf_init_from_str(&entry->name, "(null)");
7037 entry->zero_bits = true;
7038 g->builtin_types.entry_null = entry;7053 g->builtin_types.entry_null = entry;
7039 }7054 }
7040 {7055 {
7041 ZigType *entry = new_type_table_entry(ZigTypeIdArgTuple);7056 ZigType *entry = new_type_table_entry(ZigTypeIdArgTuple);
7042 buf_init_from_str(&entry->name, "(args)");7057 buf_init_from_str(&entry->name, "(args)");
7043 entry->zero_bits = true;
7044 g->builtin_types.entry_arg_tuple = entry;7058 g->builtin_types.entry_arg_tuple = entry;
7045 }7059 }
70467060
...@@ -7050,14 +7064,15 @@ static void define_builtin_types(CodeGen *g) {...@@ -7050,14 +7064,15 @@ static void define_builtin_types(CodeGen *g) {
7050 bool is_signed = info->is_signed;7064 bool is_signed = info->is_signed;
70517065
7052 ZigType *entry = new_type_table_entry(ZigTypeIdInt);7066 ZigType *entry = new_type_table_entry(ZigTypeIdInt);
7053 entry->type_ref = LLVMIntType(size_in_bits);7067 entry->llvm_type = LLVMIntType(size_in_bits);
7068 entry->size_in_bits = size_in_bits;
7069 entry->abi_size = LLVMABISizeOfType(g->target_data_ref, entry->llvm_type);
7070 entry->abi_align = LLVMABIAlignmentOfType(g->target_data_ref, entry->llvm_type);
70547071
7055 buf_init_from_str(&entry->name, info->name);7072 buf_init_from_str(&entry->name, info->name);
70567073
7057 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, entry->type_ref);7074 entry->llvm_di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
7058 entry->di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),7075 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;7076 entry->data.integral.is_signed = is_signed;
7062 entry->data.integral.bit_count = size_in_bits;7077 entry->data.integral.bit_count = size_in_bits;
7063 g->primitive_type_table.put(&entry->name, entry);7078 g->primitive_type_table.put(&entry->name, entry);
...@@ -7067,12 +7082,13 @@ static void define_builtin_types(CodeGen *g) {...@@ -7067,12 +7082,13 @@ static void define_builtin_types(CodeGen *g) {
70677082
7068 {7083 {
7069 ZigType *entry = new_type_table_entry(ZigTypeIdBool);7084 ZigType *entry = new_type_table_entry(ZigTypeIdBool);
7070 entry->type_ref = LLVMInt1Type();7085 entry->llvm_type = LLVMInt1Type();
7086 entry->size_in_bits = 1;
7087 entry->abi_size = LLVMABISizeOfType(g->target_data_ref, entry->llvm_type);
7088 entry->abi_align = LLVMABIAlignmentOfType(g->target_data_ref, entry->llvm_type);
7071 buf_init_from_str(&entry->name, "bool");7089 buf_init_from_str(&entry->name, "bool");
7072 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, entry->type_ref);7090 entry->llvm_di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
7073 entry->di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),7091 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;7092 g->builtin_types.entry_bool = entry;
7077 g->primitive_type_table.put(&entry->name, entry);7093 g->primitive_type_table.put(&entry->name, entry);
7078 }7094 }
...@@ -7081,7 +7097,10 @@ static void define_builtin_types(CodeGen *g) {...@@ -7081,7 +7097,10 @@ static void define_builtin_types(CodeGen *g) {
7081 bool is_signed = is_signed_list[sign_i];7097 bool is_signed = is_signed_list[sign_i];
70827098
7083 ZigType *entry = new_type_table_entry(ZigTypeIdInt);7099 ZigType *entry = new_type_table_entry(ZigTypeIdInt);
7084 entry->type_ref = LLVMIntType(g->pointer_size_bytes * 8);7100 entry->llvm_type = LLVMIntType(g->pointer_size_bytes * 8);
7101 entry->size_in_bits = g->pointer_size_bytes * 8;
7102 entry->abi_size = LLVMABISizeOfType(g->target_data_ref, entry->llvm_type);
7103 entry->abi_align = LLVMABIAlignmentOfType(g->target_data_ref, entry->llvm_type);
70857104
7086 const char u_or_i = is_signed ? 'i' : 'u';7105 const char u_or_i = is_signed ? 'i' : 'u';
7087 buf_resize(&entry->name, 0);7106 buf_resize(&entry->name, 0);
...@@ -7090,9 +7109,8 @@ static void define_builtin_types(CodeGen *g) {...@@ -7090,9 +7109,8 @@ static void define_builtin_types(CodeGen *g) {
7090 entry->data.integral.is_signed = is_signed;7109 entry->data.integral.is_signed = is_signed;
7091 entry->data.integral.bit_count = g->pointer_size_bytes * 8;7110 entry->data.integral.bit_count = g->pointer_size_bytes * 8;
70927111
7093 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, entry->type_ref);7112 entry->llvm_di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
7094 entry->di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),7113 entry->size_in_bits,
7095 debug_size_in_bits,
7096 is_signed ? ZigLLVMEncoding_DW_ATE_signed() : ZigLLVMEncoding_DW_ATE_unsigned());7114 is_signed ? ZigLLVMEncoding_DW_ATE_signed() : ZigLLVMEncoding_DW_ATE_unsigned());
7097 g->primitive_type_table.put(&entry->name, entry);7115 g->primitive_type_table.put(&entry->name, entry);
70987116
...@@ -7103,23 +7121,6 @@ static void define_builtin_types(CodeGen *g) {...@@ -7103,23 +7121,6 @@ static void define_builtin_types(CodeGen *g) {
7103 }7121 }
7104 }7122 }
71057123
7106 auto add_fp_entry = [] (CodeGen *g,
7107 const char *name,
7108 uint32_t bit_count,
7109 LLVMTypeRef type_ref,
7110 ZigType **field) {
7111 ZigType *entry = new_type_table_entry(ZigTypeIdFloat);
7112 entry->type_ref = type_ref;
7113 buf_init_from_str(&entry->name, name);
7114 entry->data.floating.bit_count = bit_count;
7115
7116 uint64_t debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, entry->type_ref);
7117 entry->di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
7118 debug_size_in_bits,
7119 ZigLLVMEncoding_DW_ATE_float());
7120 *field = entry;
7121 g->primitive_type_table.put(&entry->name, entry);
7122 };
7123 add_fp_entry(g, "f16", 16, LLVMHalfType(), &g->builtin_types.entry_f16);7124 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);7125 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);7126 add_fp_entry(g, "f64", 64, LLVMDoubleType(), &g->builtin_types.entry_f64);
...@@ -7128,10 +7129,9 @@ static void define_builtin_types(CodeGen *g) {...@@ -7128,10 +7129,9 @@ static void define_builtin_types(CodeGen *g) {
71287129
7129 {7130 {
7130 ZigType *entry = new_type_table_entry(ZigTypeIdVoid);7131 ZigType *entry = new_type_table_entry(ZigTypeIdVoid);
7131 entry->type_ref = LLVMVoidType();7132 entry->llvm_type = LLVMVoidType();
7132 entry->zero_bits = true;
7133 buf_init_from_str(&entry->name, "void");7133 buf_init_from_str(&entry->name, "void");
7134 entry->di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),7134 entry->llvm_di_type = ZigLLVMCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name),
7135 0,7135 0,
7136 ZigLLVMEncoding_DW_ATE_unsigned());7136 ZigLLVMEncoding_DW_ATE_unsigned());
7137 g->builtin_types.entry_void = entry;7137 g->builtin_types.entry_void = entry;
...@@ -7139,17 +7139,15 @@ static void define_builtin_types(CodeGen *g) {...@@ -7139,17 +7139,15 @@ static void define_builtin_types(CodeGen *g) {
7139 }7139 }
7140 {7140 {
7141 ZigType *entry = new_type_table_entry(ZigTypeIdUnreachable);7141 ZigType *entry = new_type_table_entry(ZigTypeIdUnreachable);
7142 entry->type_ref = LLVMVoidType();7142 entry->llvm_type = LLVMVoidType();
7143 entry->zero_bits = true;
7144 buf_init_from_str(&entry->name, "noreturn");7143 buf_init_from_str(&entry->name, "noreturn");
7145 entry->di_type = g->builtin_types.entry_void->di_type;7144 entry->llvm_di_type = g->builtin_types.entry_void->llvm_di_type;
7146 g->builtin_types.entry_unreachable = entry;7145 g->builtin_types.entry_unreachable = entry;
7147 g->primitive_type_table.put(&entry->name, entry);7146 g->primitive_type_table.put(&entry->name, entry);
7148 }7147 }
7149 {7148 {
7150 ZigType *entry = new_type_table_entry(ZigTypeIdMetaType);7149 ZigType *entry = new_type_table_entry(ZigTypeIdMetaType);
7151 buf_init_from_str(&entry->name, "type");7150 buf_init_from_str(&entry->name, "type");
7152 entry->zero_bits = true;
7153 g->builtin_types.entry_type = entry;7151 g->builtin_types.entry_type = entry;
7154 g->primitive_type_table.put(&entry->name, entry);7152 g->primitive_type_table.put(&entry->name, entry);
7155 }7153 }
...@@ -7174,19 +7172,15 @@ static void define_builtin_types(CodeGen *g) {...@@ -7174,19 +7172,15 @@ static void define_builtin_types(CodeGen *g) {
7174 buf_init_from_str(&entry->name, "anyerror");7172 buf_init_from_str(&entry->name, "anyerror");
7175 entry->data.error_set.err_count = UINT32_MAX;7173 entry->data.error_set.err_count = UINT32_MAX;
71767174
7177 // TODO allow overriding this type and keep track of max value and emit an7175 // 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;7176 g->err_tag_type = g->builtin_types.entry_u16;
71807177
7181 g->builtin_types.entry_global_error_set = entry;7178 entry->size_in_bits = g->err_tag_type->size_in_bits;
7182 entry->type_ref = g->err_tag_type->type_ref;7179 entry->abi_align = g->err_tag_type->abi_align;
7180 entry->abi_size = g->err_tag_type->abi_size;
71837181
7184 entry->di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,7182 g->builtin_types.entry_global_error_set = entry;
7185 ZigLLVMTag_DW_enumeration_type(), "anyerror",
7186 ZigLLVMCompileUnitToScope(g->compile_unit), nullptr, 0);
71877183
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);7184 g->errors_by_index.append(nullptr);
71917185
7192 g->primitive_type_table.put(&entry->name, entry);7186 g->primitive_type_table.put(&entry->name, entry);
...@@ -7194,6 +7188,9 @@ static void define_builtin_types(CodeGen *g) {...@@ -7194,6 +7188,9 @@ static void define_builtin_types(CodeGen *g) {
7194 {7188 {
7195 ZigType *entry = get_promise_type(g, nullptr);7189 ZigType *entry = get_promise_type(g, nullptr);
7196 g->primitive_type_table.put(&entry->name, entry);7190 g->primitive_type_table.put(&entry->name, entry);
7191 entry->size_in_bits = g->builtin_types.entry_usize->size_in_bits;
7192 entry->abi_align = g->builtin_types.entry_usize->abi_align;
7193 entry->abi_size = g->builtin_types.entry_usize->abi_size;
7197 }7194 }
7198}7195}
71997196
...@@ -7749,8 +7746,15 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {...@@ -7749,8 +7746,15 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
7749 buf_appendf(contents, "pub const valgrind_support = %s;\n", bool_to_str(want_valgrind_support(g)));7746 buf_appendf(contents, "pub const valgrind_support = %s;\n", bool_to_str(want_valgrind_support(g)));
7750 buf_appendf(contents, "pub const position_independent_code = %s;\n", bool_to_str(g->have_pic));7747 buf_appendf(contents, "pub const position_independent_code = %s;\n", bool_to_str(g->have_pic));
77517748
7752 buf_appendf(contents, "pub const __zig_test_fn_slice = {}; // overwritten later\n");7749 if (g->is_test_build) {
77537750 buf_appendf(contents,
7751 "const TestFn = struct {\n"
7752 "name: []const u8,\n"
7753 "func: fn()anyerror!void,\n"
7754 "};\n"
7755 "pub const test_functions = {}; // overwritten later\n"
7756 );
7757 }
77547758
7755 return contents;7759 return contents;
7756}7760}
...@@ -8157,6 +8161,8 @@ static ZigPackage *create_panic_pkg(CodeGen *g) {...@@ -8157,6 +8161,8 @@ static ZigPackage *create_panic_pkg(CodeGen *g) {
8157}8161}
81588162
8159static void create_test_compile_var_and_add_test_runner(CodeGen *g) {8163static void create_test_compile_var_and_add_test_runner(CodeGen *g) {
8164 Error err;
8165
8160 assert(g->is_test_build);8166 assert(g->is_test_build);
81618167
8162 if (g->test_fns.length == 0) {8168 if (g->test_fns.length == 0) {
...@@ -8164,14 +8170,13 @@ static void create_test_compile_var_and_add_test_runner(CodeGen *g) {...@@ -8164,14 +8170,13 @@ static void create_test_compile_var_and_add_test_runner(CodeGen *g) {
8164 exit(0);8170 exit(0);
8165 }8171 }
81668172
8167 ZigType *u8_ptr_type = get_pointer_to_type_extra(g, g->builtin_types.entry_u8, true, false,
8168 PtrLenUnknown, get_abi_alignment(g, g->builtin_types.entry_u8), 0, 0, false);
8169 ZigType *str_type = get_slice_type(g, u8_ptr_type);
8170 ZigType *fn_type = get_test_fn_type(g);8173 ZigType *fn_type = get_test_fn_type(g);
81718174
8172 const char *field_names[] = { "name", "func", };8175 ConstExprValue *test_fn_type_val = get_builtin_value(g, "TestFn");
8173 ZigType *field_types[] = { str_type, fn_type, };8176 assert(test_fn_type_val->type->id == ZigTypeIdMetaType);
8174 ZigType *struct_type = get_struct_type(g, "ZigTestFn", field_names, field_types, 2);8177 ZigType *struct_type = test_fn_type_val->data.x_type;
8178 if ((err = type_resolve(g, struct_type, ResolveStatusSizeKnown)))
8179 zig_unreachable();
81758180
8176 ConstExprValue *test_fn_array = create_const_vals(1);8181 ConstExprValue *test_fn_array = create_const_vals(1);
8177 test_fn_array->type = get_array_type(g, struct_type, g->test_fns.length);8182 test_fn_array->type = get_array_type(g, struct_type, g->test_fns.length);
...@@ -8203,7 +8208,7 @@ static void create_test_compile_var_and_add_test_runner(CodeGen *g) {...@@ -8203,7 +8208,7 @@ static void create_test_compile_var_and_add_test_runner(CodeGen *g) {
82038208
8204 ConstExprValue *test_fn_slice = create_const_slice(g, test_fn_array, 0, g->test_fns.length, true);8209 ConstExprValue *test_fn_slice = create_const_slice(g, test_fn_array, 0, g->test_fns.length, true);
82058210
8206 update_compile_var(g, buf_create_from_str("__zig_test_fn_slice"), test_fn_slice);8211 update_compile_var(g, buf_create_from_str("test_functions"), test_fn_slice);
8207 g->test_runner_package = create_test_runner_pkg(g);8212 g->test_runner_package = create_test_runner_pkg(g);
8208 g->test_runner_import = add_special_code(g, g->test_runner_package, "test_runner.zig");8213 g->test_runner_import = add_special_code(g, g->test_runner_package, "test_runner.zig");
8209}8214}
src/ir.cpp+58-54
...@@ -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,14 @@ static ZigType *get_error_set_intersection(IrAnalyze *ira, ZigType *set1, ZigTyp...@@ -8947,16 +8943,14 @@ 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 err_set_type->size_in_bits = ira->codegen->builtin_types.entry_global_error_set->size_in_bits;
8949 err_set_type->abi_align = ira->codegen->builtin_types.entry_global_error_set->abi_align;
8950 err_set_type->abi_size = ira->codegen->builtin_types.entry_global_error_set->abi_size;
89558951
8956 buf_appendf(&err_set_type->name, "}");8952 buf_appendf(&err_set_type->name, "}");
89578953
8958 ira->codegen->error_di_types.append(&err_set_type->di_type);
8959
8960 return err_set_type;8954 return err_set_type;
8961}8955}
89628956
...@@ -13968,20 +13962,19 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod...@@ -13968,20 +13962,19 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod
13968}13962}
1396913963
13970static ZigVar *get_fn_var_by_index(ZigFn *fn_entry, size_t index) {13964static ZigVar *get_fn_var_by_index(ZigFn *fn_entry, size_t index) {
13965 FnTypeParamInfo *src_param_info = &fn_entry->type_entry->data.fn.fn_type_id.param_info[index];
13966 if (!type_has_bits(src_param_info->type))
13967 return nullptr;
13968
13971 size_t next_var_i = 0;13969 size_t next_var_i = 0;
13972 FnGenParamInfo *gen_param_info = fn_entry->type_entry->data.fn.gen_param_info;
13973 assert(gen_param_info != nullptr);
13974 for (size_t param_i = 0; param_i < index; param_i += 1) {13970 for (size_t param_i = 0; param_i < index; param_i += 1) {
13975 FnGenParamInfo *info = &gen_param_info[param_i];13971 FnTypeParamInfo *src_param_info = &fn_entry->type_entry->data.fn.fn_type_id.param_info[param_i];
13976 if (info->gen_index == SIZE_MAX)13972 if (!type_has_bits(src_param_info->type)) {
13977 continue;13973 continue;
13974 }
1397813975
13979 next_var_i += 1;13976 next_var_i += 1;
13980 }13977 }
13981 FnGenParamInfo *info = &gen_param_info[index];
13982 if (info->gen_index == SIZE_MAX)
13983 return nullptr;
13984
13985 return fn_entry->variable_list.at(next_var_i);13978 return fn_entry->variable_list.at(next_var_i);
13986}13979}
1398713980
...@@ -14855,7 +14848,7 @@ static IrInstruction *ir_analyze_maybe(IrAnalyze *ira, IrInstructionUnOp *un_op_...@@ -14855,7 +14848,7 @@ static IrInstruction *ir_analyze_maybe(IrAnalyze *ira, IrInstructionUnOp *un_op_
14855 ZigType *type_entry = ir_resolve_type(ira, value);14848 ZigType *type_entry = ir_resolve_type(ira, value);
14856 if (type_is_invalid(type_entry))14849 if (type_is_invalid(type_entry))
14857 return ira->codegen->invalid_instruction;14850 return ira->codegen->invalid_instruction;
14858 if ((err = ensure_complete_type(ira->codegen, type_entry)))14851 if ((err = type_resolve(ira->codegen, type_entry, ResolveStatusSizeKnown)))
14859 return ira->codegen->invalid_instruction;14852 return ira->codegen->invalid_instruction;
1486014853
14861 switch (type_entry->id) {14854 switch (type_entry->id) {
...@@ -16051,8 +16044,6 @@ static IrInstruction *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstruc...@@ -16051,8 +16044,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));16044 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;16045 err_entry->value = error_value_count;
16053 ira->codegen->errors_by_index.append(err_entry);16046 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);16047 ira->codegen->error_table.put(field_name, err_entry);
16057 }16048 }
16058 if (err_entry->set_with_only_this_in_it == nullptr) {16049 if (err_entry->set_with_only_this_in_it == nullptr) {
...@@ -17519,6 +17510,8 @@ static IrInstruction *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstruc...@@ -17519,6 +17510,8 @@ static IrInstruction *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstruc
17519static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,17510static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,
17520 IrInstructionContainerInitList *instruction)17511 IrInstructionContainerInitList *instruction)
17521{17512{
17513 Error err;
17514
17522 ZigType *container_type = ir_resolve_type(ira, instruction->container_type->child);17515 ZigType *container_type = ir_resolve_type(ira, instruction->container_type->child);
17523 if (type_is_invalid(container_type))17516 if (type_is_invalid(container_type))
17524 return ira->codegen->invalid_instruction;17517 return ira->codegen->invalid_instruction;
...@@ -17547,6 +17540,10 @@ static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,...@@ -17547,6 +17540,10 @@ static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,
17547 child_type = pointer_type->data.pointer.child_type;17540 child_type = pointer_type->data.pointer.child_type;
17548 }17541 }
1754917542
17543 if ((err = type_resolve(ira->codegen, child_type, ResolveStatusSizeKnown))) {
17544 return ira->codegen->invalid_instruction;
17545 }
17546
17550 ZigType *fixed_size_array_type = get_array_type(ira->codegen, child_type, elem_count);17547 ZigType *fixed_size_array_type = get_array_type(ira->codegen, child_type, elem_count);
1755117548
17552 ConstExprValue const_val = {};17549 ConstExprValue const_val = {};
...@@ -17873,7 +17870,7 @@ static TypeStructField *validate_byte_offset(IrAnalyze *ira,...@@ -17873,7 +17870,7 @@ static TypeStructField *validate_byte_offset(IrAnalyze *ira,
17873 return nullptr;17870 return nullptr;
17874 }17871 }
1787517872
17876 *byte_offset = LLVMOffsetOfElement(ira->codegen->target_data_ref, container_type->type_ref, field->gen_index);17873 *byte_offset = field->offset;
17877 return field;17874 return field;
17878}17875}
1787917876
...@@ -17929,10 +17926,11 @@ static ZigType *ir_type_info_get_type(IrAnalyze *ira, const char *type_name, Zig...@@ -17929,10 +17926,11 @@ static ZigType *ir_type_info_get_type(IrAnalyze *ira, const char *type_name, Zig
17929 Error err;17926 Error err;
17930 ConstExprValue *type_info_var = get_builtin_value(ira->codegen, "TypeInfo");17927 ConstExprValue *type_info_var = get_builtin_value(ira->codegen, "TypeInfo");
17931 assert(type_info_var->type->id == ZigTypeIdMetaType);17928 assert(type_info_var->type->id == ZigTypeIdMetaType);
17932 assertNoError(ensure_complete_type(ira->codegen, type_info_var->data.x_type));
17933
17934 ZigType *type_info_type = type_info_var->data.x_type;17929 ZigType *type_info_type = type_info_var->data.x_type;
17935 assert(type_info_type->id == ZigTypeIdUnion);17930 assert(type_info_type->id == ZigTypeIdUnion);
17931 if ((err = type_resolve(ira->codegen, type_info_type, ResolveStatusSizeKnown))) {
17932 zig_unreachable();
17933 }
1793617934
17937 if (type_name == nullptr && root == nullptr)17935 if (type_name == nullptr && root == nullptr)
17938 return type_info_type;17936 return type_info_type;
...@@ -17966,7 +17964,7 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, IrInstruction *source_instr,...@@ -17966,7 +17964,7 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, IrInstruction *source_instr,
17966{17964{
17967 Error err;17965 Error err;
17968 ZigType *type_info_definition_type = ir_type_info_get_type(ira, "Definition", nullptr);17966 ZigType *type_info_definition_type = ir_type_info_get_type(ira, "Definition", nullptr);
17969 if ((err = ensure_complete_type(ira->codegen, type_info_definition_type)))17967 if ((err = type_resolve(ira->codegen, type_info_definition_type, ResolveStatusSizeKnown)))
17970 return err;17968 return err;
1797117969
17972 ensure_field_index(type_info_definition_type, "name", 0);17970 ensure_field_index(type_info_definition_type, "name", 0);
...@@ -18486,6 +18484,9 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr...@@ -18486,6 +18484,9 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
18486 ensure_field_index(result->type, "fields", 2);18484 ensure_field_index(result->type, "fields", 2);
1848718485
18488 ZigType *type_info_enum_field_type = ir_type_info_get_type(ira, "EnumField", nullptr);18486 ZigType *type_info_enum_field_type = ir_type_info_get_type(ira, "EnumField", nullptr);
18487 if ((err = type_resolve(ira->codegen, type_info_enum_field_type, ResolveStatusSizeKnown))) {
18488 zig_unreachable();
18489 }
18489 uint32_t enum_field_count = type_entry->data.enumeration.src_field_count;18490 uint32_t enum_field_count = type_entry->data.enumeration.src_field_count;
1849018491
18491 ConstExprValue *enum_field_array = create_const_vals(1);18492 ConstExprValue *enum_field_array = create_const_vals(1);
...@@ -18529,6 +18530,9 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr...@@ -18529,6 +18530,9 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
18529 result->data.x_optional = nullptr;18530 result->data.x_optional = nullptr;
18530 break;18531 break;
18531 }18532 }
18533 if ((err = type_resolve(ira->codegen, type_info_error_type, ResolveStatusSizeKnown))) {
18534 zig_unreachable();
18535 }
18532 ConstExprValue *slice_val = create_const_vals(1);18536 ConstExprValue *slice_val = create_const_vals(1);
18533 result->data.x_optional = slice_val;18537 result->data.x_optional = slice_val;
1853418538
...@@ -18625,6 +18629,8 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr...@@ -18625,6 +18629,8 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
18625 ensure_field_index(result->type, "fields", 2);18629 ensure_field_index(result->type, "fields", 2);
1862618630
18627 ZigType *type_info_union_field_type = ir_type_info_get_type(ira, "UnionField", nullptr);18631 ZigType *type_info_union_field_type = ir_type_info_get_type(ira, "UnionField", nullptr);
18632 if ((err = type_resolve(ira->codegen, type_info_union_field_type, ResolveStatusSizeKnown)))
18633 zig_unreachable();
18628 uint32_t union_field_count = type_entry->data.unionation.src_field_count;18634 uint32_t union_field_count = type_entry->data.unionation.src_field_count;
1862918635
18630 ConstExprValue *union_field_array = create_const_vals(1);18636 ConstExprValue *union_field_array = create_const_vals(1);
...@@ -18702,6 +18708,9 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr...@@ -18702,6 +18708,9 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
18702 ensure_field_index(result->type, "fields", 1);18708 ensure_field_index(result->type, "fields", 1);
1870318709
18704 ZigType *type_info_struct_field_type = ir_type_info_get_type(ira, "StructField", nullptr);18710 ZigType *type_info_struct_field_type = ir_type_info_get_type(ira, "StructField", nullptr);
18711 if ((err = type_resolve(ira->codegen, type_info_struct_field_type, ResolveStatusSizeKnown))) {
18712 zig_unreachable();
18713 }
18705 uint32_t struct_field_count = type_entry->data.structure.src_field_count;18714 uint32_t struct_field_count = type_entry->data.structure.src_field_count;
1870618715
18707 ConstExprValue *struct_field_array = create_const_vals(1);18716 ConstExprValue *struct_field_array = create_const_vals(1);
...@@ -18726,7 +18735,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr...@@ -18726,7 +18735,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
18726 if (!type_has_bits(struct_field->type_entry)) {18735 if (!type_has_bits(struct_field->type_entry)) {
18727 inner_fields[1].data.x_optional = nullptr;18736 inner_fields[1].data.x_optional = nullptr;
18728 } else {18737 } else {
18729 size_t byte_offset = LLVMOffsetOfElement(ira->codegen->target_data_ref, type_entry->type_ref, struct_field->gen_index);18738 size_t byte_offset = struct_field->offset;
18730 inner_fields[1].data.x_optional = create_const_vals(1);18739 inner_fields[1].data.x_optional = create_const_vals(1);
18731 inner_fields[1].data.x_optional->special = ConstValSpecialStatic;18740 inner_fields[1].data.x_optional->special = ConstValSpecialStatic;
18732 inner_fields[1].data.x_optional->type = ira->codegen->builtin_types.entry_num_lit_int;18741 inner_fields[1].data.x_optional->type = ira->codegen->builtin_types.entry_num_lit_int;
...@@ -18809,6 +18818,9 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr...@@ -18809,6 +18818,9 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
18809 }18818 }
18810 // args: []TypeInfo.FnArg18819 // args: []TypeInfo.FnArg
18811 ZigType *type_info_fn_arg_type = ir_type_info_get_type(ira, "FnArg", nullptr);18820 ZigType *type_info_fn_arg_type = ir_type_info_get_type(ira, "FnArg", nullptr);
18821 if ((err = type_resolve(ira->codegen, type_info_fn_arg_type, ResolveStatusSizeKnown))) {
18822 zig_unreachable();
18823 }
18812 size_t fn_arg_count = type_entry->data.fn.fn_type_id.param_count -18824 size_t fn_arg_count = type_entry->data.fn.fn_type_id.param_count -
18813 (is_varargs && type_entry->data.fn.fn_type_id.cc != CallingConventionC);18825 (is_varargs && type_entry->data.fn.fn_type_id.cc != CallingConventionC);
1881418826
...@@ -21425,12 +21437,11 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue...@@ -21425,12 +21437,11 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue
21425 case ContainerLayoutExtern: {21437 case ContainerLayoutExtern: {
21426 size_t src_field_count = val->type->data.structure.src_field_count;21438 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) {21439 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];21440 TypeStructField *struct_field = &val->type->data.structure.fields[field_i];
21429 if (type_field->gen_index == SIZE_MAX)21441 if (struct_field->gen_index == SIZE_MAX)
21430 continue;21442 continue;
21431 ConstExprValue *field_val = &val->data.x_struct.fields[field_i];21443 ConstExprValue *field_val = &val->data.x_struct.fields[field_i];
21432 size_t offset = LLVMOffsetOfElement(codegen->target_data_ref, val->type->type_ref,21444 size_t offset = struct_field->offset;
21433 type_field->gen_index);
21434 buf_write_value_bytes(codegen, buf + offset, field_val);21445 buf_write_value_bytes(codegen, buf + offset, field_val);
21435 }21446 }
21436 return;21447 return;
...@@ -21446,10 +21457,7 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue...@@ -21446,10 +21457,7 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue
21446 size_t child_buf_len = 16;21457 size_t child_buf_len = 16;
21447 uint8_t *child_buf = child_buf_prealloc;21458 uint8_t *child_buf = child_buf_prealloc;
21448 while (gen_i < gen_field_count) {21459 while (gen_i < gen_field_count) {
21449 LLVMTypeRef gen_llvm_int_type = LLVMStructGetTypeAtIndex(val->type->type_ref,21460 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) {21461 if (big_int_byte_count > child_buf_len) {
21454 child_buf = allocate_nonzero<uint8_t>(big_int_byte_count);21462 child_buf = allocate_nonzero<uint8_t>(big_int_byte_count);
21455 child_buf_len = big_int_byte_count;21463 child_buf_len = big_int_byte_count;
...@@ -21485,7 +21493,7 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue...@@ -21485,7 +21493,7 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue
21485 }21493 }
21486 src_i += 1;21494 src_i += 1;
21487 }21495 }
21488 bigint_write_twos_complement(&big_int, buf + offset, big_int_bit_count, is_big_endian);21496 bigint_write_twos_complement(&big_int, buf + offset, big_int_byte_count * 8, is_big_endian);
21489 offset += big_int_byte_count;21497 offset += big_int_byte_count;
21490 gen_i += 1;21498 gen_i += 1;
21491 }21499 }
...@@ -21606,12 +21614,11 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou...@@ -21606,12 +21614,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) {21614 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];21615 ConstExprValue *field_val = &val->data.x_struct.fields[field_i];
21608 field_val->special = ConstValSpecialStatic;21616 field_val->special = ConstValSpecialStatic;
21609 TypeStructField *type_field = &val->type->data.structure.fields[field_i];21617 TypeStructField *struct_field = &val->type->data.structure.fields[field_i];
21610 field_val->type = type_field->type_entry;21618 field_val->type = struct_field->type_entry;
21611 if (type_field->gen_index == SIZE_MAX)21619 if (struct_field->gen_index == SIZE_MAX)
21612 continue;21620 continue;
21613 size_t offset = LLVMOffsetOfElement(codegen->target_data_ref, val->type->type_ref,21621 size_t offset = struct_field->offset;
21614 type_field->gen_index);
21615 uint8_t *new_buf = buf + offset;21622 uint8_t *new_buf = buf + offset;
21616 if ((err = buf_read_value_bytes(ira, codegen, source_node, new_buf, field_val)))21623 if ((err = buf_read_value_bytes(ira, codegen, source_node, new_buf, field_val)))
21617 return err;21624 return err;
...@@ -21630,16 +21637,13 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou...@@ -21630,16 +21637,13 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou
21630 size_t child_buf_len = 16;21637 size_t child_buf_len = 16;
21631 uint8_t *child_buf = child_buf_prealloc;21638 uint8_t *child_buf = child_buf_prealloc;
21632 while (gen_i < gen_field_count) {21639 while (gen_i < gen_field_count) {
21633 LLVMTypeRef gen_llvm_int_type = LLVMStructGetTypeAtIndex(val->type->type_ref,21640 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) {21641 if (big_int_byte_count > child_buf_len) {
21638 child_buf = allocate_nonzero<uint8_t>(big_int_byte_count);21642 child_buf = allocate_nonzero<uint8_t>(big_int_byte_count);
21639 child_buf_len = big_int_byte_count;21643 child_buf_len = big_int_byte_count;
21640 }21644 }
21641 BigInt big_int;21645 BigInt big_int;
21642 bigint_read_twos_complement(&big_int, buf + offset, big_int_bit_count, is_big_endian, false);21646 bigint_read_twos_complement(&big_int, buf + offset, big_int_byte_count * 8, is_big_endian, false);
21643 while (src_i < src_field_count) {21647 while (src_i < src_field_count) {
21644 TypeStructField *field = &val->type->data.structure.fields[src_i];21648 TypeStructField *field = &val->type->data.structure.fields[src_i];
21645 assert(field->gen_index != SIZE_MAX);21649 assert(field->gen_index != SIZE_MAX);
...@@ -21662,7 +21666,7 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou...@@ -21662,7 +21666,7 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou
21662 big_int = tmp;21666 big_int = tmp;
21663 }21667 }
2166421668
21665 bigint_write_twos_complement(&child_val, child_buf, big_int_bit_count, is_big_endian);21669 bigint_write_twos_complement(&child_val, child_buf, big_int_byte_count * 8, is_big_endian);
21666 if ((err = buf_read_value_bytes(ira, codegen, source_node, child_buf, field_val))) {21670 if ((err = buf_read_value_bytes(ira, codegen, source_node, child_buf, field_val))) {
21667 return err;21671 return err;
21668 }21672 }
src/zig_llvm.cpp+1
...@@ -548,6 +548,7 @@ ZigLLVMDILocalVariable *ZigLLVMCreateParameterVariable(ZigLLVMDIBuilder *dbuilde...@@ -548,6 +548,7 @@ ZigLLVMDILocalVariable *ZigLLVMCreateParameterVariable(ZigLLVMDIBuilder *dbuilde
548 ZigLLVMDIType *type, bool always_preserve, unsigned flags, unsigned arg_no)548 ZigLLVMDIType *type, bool always_preserve, unsigned flags, unsigned arg_no)
549{549{
550 assert(flags == 0);550 assert(flags == 0);
551 assert(arg_no != 0);
551 DILocalVariable *result = reinterpret_cast<DIBuilder*>(dbuilder)->createParameterVariable(552 DILocalVariable *result = reinterpret_cast<DIBuilder*>(dbuilder)->createParameterVariable(
552 reinterpret_cast<DIScope*>(scope),553 reinterpret_cast<DIScope*>(scope),
553 name,554 name,
std/special/test_runner.zig+1-1
...@@ -1,7 +1,7 @@...@@ -1,7 +1,7 @@
1const std = @import("std");1const std = @import("std");
2const io = std.io;2const io = std.io;
3const builtin = @import("builtin");3const builtin = @import("builtin");
4const test_fn_list = builtin.__zig_test_fn_slice;4const test_fn_list = builtin.test_functions;
5const warn = std.debug.warn;5const warn = std.debug.warn;
66
7pub fn main() !void {7pub fn main() !void {
test/stage1/behavior/struct.zig+11-5
...@@ -256,8 +256,8 @@ const Foo96Bits = packed struct {...@@ -256,8 +256,8 @@ const Foo96Bits = packed struct {
256256
257test "packed struct 24bits" {257test "packed struct 24bits" {
258 comptime {258 comptime {
259 expect(@sizeOf(Foo24Bits) == 3);259 expect(@sizeOf(Foo24Bits) == 4);
260 expect(@sizeOf(Foo96Bits) == 12);260 expect(@sizeOf(Foo96Bits) == 16);
261 }261 }
262262
263 var value = Foo96Bits{263 var value = Foo96Bits{
...@@ -291,16 +291,22 @@ test "packed struct 24bits" {...@@ -291,16 +291,22 @@ test "packed struct 24bits" {
291 expect(value.d == 1);291 expect(value.d == 1);
292}292}
293293
294const Foo32Bits = packed struct {
295 field: u24,
296 pad: u8,
297};
298
294const FooArray24Bits = packed struct {299const FooArray24Bits = packed struct {
295 a: u16,300 a: u16,
296 b: [2]Foo24Bits,301 b: [2]Foo32Bits,
297 c: u16,302 c: u16,
298};303};
299304
305// TODO revisit this test when doing https://github.com/ziglang/zig/issues/1512
300test "packed array 24bits" {306test "packed array 24bits" {
301 comptime {307 comptime {
302 expect(@sizeOf([9]Foo24Bits) == 9 * 3);308 expect(@sizeOf([9]Foo32Bits) == 9 * 4);
303 expect(@sizeOf(FooArray24Bits) == 2 + 2 * 3 + 2);309 expect(@sizeOf(FooArray24Bits) == 2 + 2 * 4 + 2);
304 }310 }
305311
306 var bytes = []u8{0} ** (@sizeOf(FooArray24Bits) + 1);312 var bytes = []u8{0} ** (@sizeOf(FooArray24Bits) + 1);