authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-02 11:11:42-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-02 18:31:18-04:00
logddb8aa73f542d3432538e6de466ac216c89fd12b
treeed11f5829357daf610b3cb47c74c21a91dddd453
parent30b2fb2fb545794beb44f80af308c4280d94deba
signature Commit is signed but in an unrecognized format.

more regression fixes


4 files changed, 117 insertions(+), 50 deletions(-)

src/analyze.cpp+91-30
......@@ -343,8 +343,9 @@ ZigType *get_promise_type(CodeGen *g, ZigType *result_type) {
343343 }
344344
345345 ZigType *entry = new_type_table_entry(ZigTypeIdPromise);
346 entry->abi_size = g->pointer_size_bytes;
347 entry->size_in_bits = g->pointer_size_bytes * 8;
346 entry->abi_size = g->builtin_types.entry_usize->abi_size;
347 entry->size_in_bits = g->builtin_types.entry_usize->size_in_bits;
348 entry->abi_align = g->builtin_types.entry_usize->abi_align;
348349 entry->data.promise.result_type = result_type;
349350 buf_init_from_str(&entry->name, "promise");
350351 if (result_type != nullptr) {
......@@ -775,9 +776,6 @@ ZigType *get_bound_fn_type(CodeGen *g, ZigFn *fn_entry) {
775776
776777 ZigType *bound_fn_type = new_type_table_entry(ZigTypeIdBoundFn);
777778 bound_fn_type->data.bound_fn.fn_type = fn_type;
778 bound_fn_type->abi_size = 0;
779 bound_fn_type->size_in_bits = 0;
780 bound_fn_type->abi_align = 0;
781779
782780 buf_resize(&bound_fn_type->name, 0);
783781 buf_appendf(&bound_fn_type->name, "(bound %s)", buf_ptr(&fn_type->name));
......@@ -1248,6 +1246,9 @@ ZigType *get_auto_err_set_type(CodeGen *g, ZigFn *fn_entry) {
12481246 err_set_type->data.error_set.err_count = 0;
12491247 err_set_type->data.error_set.errors = nullptr;
12501248 err_set_type->data.error_set.infer_fn = fn_entry;
1249 err_set_type->size_in_bits = g->builtin_types.entry_global_error_set->size_in_bits;
1250 err_set_type->abi_align = g->builtin_types.entry_global_error_set->abi_align;
1251 err_set_type->abi_size = g->builtin_types.entry_global_error_set->abi_size;
12511252
12521253 return err_set_type;
12531254}
......@@ -1597,7 +1598,16 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
15971598
15981599 uint32_t *host_int_bytes = packed ? allocate<uint32_t>(struct_type->data.structure.gen_field_count) : nullptr;
15991600
1600 // Compute offsets for all the fields.
1601 // Resolve sizes of all the field types. Done before the offset loop because the offset
1602 // loop has to look ahead.
1603 for (size_t i = 0; i < field_count; i += 1) {
1604 TypeStructField *field = &struct_type->data.structure.fields[i];
1605 if ((err = type_resolve(g, field->type_entry, ResolveStatusSizeKnown))) {
1606 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
1607 return ErrorSemanticAnalyzeFail;
1608 }
1609 }
1610
16011611 size_t packed_bits_offset = 0;
16021612 size_t next_offset = 0;
16031613 size_t first_packed_bits_offset_misalign = SIZE_MAX;
......@@ -1739,6 +1749,7 @@ static Error resolve_union_alignment(CodeGen *g, ZigType *union_type) {
17391749 // unset temporary flag
17401750 union_type->data.unionation.resolve_loop_flag = false;
17411751 union_type->data.unionation.resolve_status = ResolveStatusAlignmentKnown;
1752 union_type->data.unionation.most_aligned_union_member = most_aligned_union_member;
17421753
17431754 ZigType *tag_type = union_type->data.unionation.tag_type;
17441755 if (tag_type != nullptr && type_has_bits(tag_type)) {
......@@ -1788,6 +1799,7 @@ static Error resolve_union_type(CodeGen *g, ZigType *union_type) {
17881799 assert(decl_node->type == NodeTypeContainerDecl);
17891800
17901801 uint32_t field_count = union_type->data.unionation.src_field_count;
1802 ZigType *most_aligned_union_member = union_type->data.unionation.most_aligned_union_member;
17911803
17921804 assert(union_type->data.unionation.fields);
17931805
......@@ -1827,13 +1839,18 @@ static Error resolve_union_type(CodeGen *g, ZigType *union_type) {
18271839 union_size_in_bits = max(union_size_in_bits, field_type->size_in_bits);
18281840 }
18291841
1842 // The union itself for now has to be treated as being independently aligned.
1843 // See https://github.com/ziglang/zig/issues/2166.
1844 if (most_aligned_union_member != nullptr) {
1845 union_abi_size = align_forward(union_abi_size, most_aligned_union_member->abi_align);
1846 }
1847
18301848 // unset temporary flag
18311849 union_type->data.unionation.resolve_loop_flag = false;
18321850 union_type->data.unionation.resolve_status = ResolveStatusSizeKnown;
18331851 union_type->data.unionation.union_abi_size = union_abi_size;
18341852
18351853 ZigType *tag_type = union_type->data.unionation.tag_type;
1836 ZigType *most_aligned_union_member = union_type->data.unionation.most_aligned_union_member;
18371854 if (tag_type != nullptr && type_has_bits(tag_type)) {
18381855 if ((err = type_resolve(g, tag_type, ResolveStatusSizeKnown))) {
18391856 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
......@@ -6340,16 +6357,25 @@ static void resolve_llvm_types_struct(CodeGen *g, ZigType *struct_type) {
63406357 struct_type->llvm_type = type_has_bits(struct_type) ?
63416358 LLVMStructCreateNamed(LLVMGetGlobalContext(), buf_ptr(&struct_type->name)) : LLVMVoidType();
63426359 AstNode *decl_node = struct_type->data.structure.decl_node;
6343 assert(decl_node->type == NodeTypeContainerDecl);
6344 Scope *scope = &struct_type->data.structure.decls_scope->base;
6345 ZigType *import = get_scope_import(scope);
6360 ZigLLVMDIFile *di_file;
6361 ZigLLVMDIScope *di_scope;
6362 unsigned line;
6363 if (decl_node != nullptr) {
6364 assert(decl_node->type == NodeTypeContainerDecl);
6365 Scope *scope = &struct_type->data.structure.decls_scope->base;
6366 ZigType *import = get_scope_import(scope);
6367 di_file = import->data.structure.root_struct->di_file;
6368 di_scope = ZigLLVMFileToScope(di_file);
6369 line = decl_node->line + 1;
6370 } else {
6371 di_file = nullptr;
6372 di_scope = ZigLLVMCompileUnitToScope(g->compile_unit);
6373 line = 0;
6374 }
63466375 unsigned dwarf_kind = ZigLLVMTag_DW_structure_type();
63476376 struct_type->llvm_di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
63486377 dwarf_kind, buf_ptr(&struct_type->name),
6349 ZigLLVMFileToScope(import->data.structure.root_struct->di_file),
6350 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1));
6351
6352
6378 di_scope, di_file, line);
63536379
63546380 size_t field_count = struct_type->data.structure.src_field_count;
63556381 size_t gen_field_count = struct_type->data.structure.gen_field_count;
......@@ -6412,7 +6438,6 @@ static void resolve_llvm_types_struct(CodeGen *g, ZigType *struct_type) {
64126438 ZigLLVMDIType **di_element_types = allocate<ZigLLVMDIType*>(debug_field_count);
64136439 size_t debug_field_index = 0;
64146440 for (size_t i = 0; i < field_count; i += 1) {
6415 AstNode *field_node = decl_node->data.container_decl.fields.at(i);
64166441 TypeStructField *type_struct_field = &struct_type->data.structure.fields[i];
64176442 size_t gen_field_index = type_struct_field->gen_index;
64186443 if (gen_field_index == SIZE_MAX) {
......@@ -6445,9 +6470,16 @@ static void resolve_llvm_types_struct(CodeGen *g, ZigType *struct_type) {
64456470 debug_align_in_bits = 8 * field_type->abi_align;
64466471 debug_offset_in_bits = 8 * type_struct_field->offset;
64476472 }
6473 unsigned line;
6474 if (decl_node != nullptr) {
6475 AstNode *field_node = decl_node->data.container_decl.fields.at(i);
6476 line = field_node->line + 1;
6477 } else {
6478 line = 0;
6479 }
64486480 di_element_types[debug_field_index] = ZigLLVMCreateDebugMemberType(g->dbuilder,
64496481 ZigLLVMTypeToScope(struct_type->llvm_di_type), buf_ptr(type_struct_field->name),
6450 import->data.structure.root_struct->di_file, (unsigned)(field_node->line + 1),
6482 di_file, line,
64516483 debug_size_in_bits,
64526484 debug_align_in_bits,
64536485 debug_offset_in_bits,
......@@ -6459,9 +6491,9 @@ static void resolve_llvm_types_struct(CodeGen *g, ZigType *struct_type) {
64596491 uint64_t debug_size_in_bits = get_store_size_in_bits(struct_type->size_in_bits);
64606492 uint64_t debug_align_in_bits = 8*struct_type->abi_align;
64616493 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugStructType(g->dbuilder,
6462 ZigLLVMFileToScope(import->data.structure.root_struct->di_file),
6494 di_scope,
64636495 buf_ptr(&struct_type->name),
6464 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
6496 di_file, line,
64656497 debug_size_in_bits,
64666498 debug_align_in_bits,
64676499 0, nullptr, di_element_types, (int)debug_field_count, 0, nullptr, "");
......@@ -6512,19 +6544,14 @@ static void resolve_llvm_types_enum(CodeGen *g, ZigType *enum_type) {
65126544static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type) {
65136545 ZigType *most_aligned_union_member = union_type->data.unionation.most_aligned_union_member;
65146546 ZigType *tag_type = union_type->data.unionation.tag_type;
6515 if (tag_type == nullptr || !type_has_bits(tag_type)) {
6516 assert(most_aligned_union_member != nullptr);
6517 assert(union_type->data.unionation.union_abi_size >= most_aligned_union_member->abi_size);
6518 union_type->llvm_type = get_llvm_type(g, most_aligned_union_member);
6519 union_type->llvm_di_type = get_llvm_di_type(g, most_aligned_union_member);
6520 return;
6521 }
65226547 if (most_aligned_union_member == nullptr) {
65236548 union_type->llvm_type = get_llvm_type(g, tag_type);
65246549 union_type->llvm_di_type = get_llvm_di_type(g, tag_type);
65256550 return;
65266551 }
65276552
6553 // Do this first for the benefit of recursive calls.
6554 union_type->llvm_type = LLVMStructCreateNamed(LLVMGetGlobalContext(), buf_ptr(&union_type->name));
65286555 Scope *scope = &union_type->data.unionation.decls_scope->base;
65296556 ZigType *import = get_scope_import(scope);
65306557 AstNode *decl_node = union_type->data.unionation.decl_node;
......@@ -6535,6 +6562,7 @@ static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type) {
65356562 ZigLLVMFileToScope(import->data.structure.root_struct->di_file),
65366563 import->data.structure.root_struct->di_file, (unsigned)(line + 1));
65376564
6565
65386566 uint32_t gen_field_count = union_type->data.unionation.gen_field_count;
65396567 ZigLLVMDIType **union_inner_di_types = allocate<ZigLLVMDIType*>(gen_field_count);
65406568 uint32_t field_count = union_type->data.unionation.src_field_count;
......@@ -6555,7 +6583,40 @@ static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type) {
65556583 0, get_llvm_di_type(g, union_field->type_entry));
65566584
65576585 }
6558 union_type->llvm_type = LLVMStructCreateNamed(LLVMGetGlobalContext(), buf_ptr(&union_type->name));
6586
6587 if (tag_type == nullptr || !type_has_bits(tag_type)) {
6588 assert(most_aligned_union_member != nullptr);
6589
6590 size_t padding_bytes = union_type->data.unionation.union_abi_size - most_aligned_union_member->abi_size;
6591 (void)get_llvm_type(g, most_aligned_union_member);
6592 if (padding_bytes > 0) {
6593 ZigType *u8_type = get_int_type(g, false, 8);
6594 ZigType *padding_array = get_array_type(g, u8_type, padding_bytes);
6595 LLVMTypeRef union_element_types[] = {
6596 most_aligned_union_member->llvm_type,
6597 get_llvm_type(g, padding_array),
6598 };
6599 LLVMStructSetBody(union_type->llvm_type, union_element_types, 2, false);
6600 } else {
6601 LLVMStructSetBody(union_type->llvm_type, &most_aligned_union_member->llvm_type, 1, false);
6602 }
6603 union_type->data.unionation.union_llvm_type = union_type->llvm_type;
6604 union_type->data.unionation.gen_tag_index = SIZE_MAX;
6605 union_type->data.unionation.gen_union_index = SIZE_MAX;
6606
6607 // create debug type for union
6608 ZigLLVMDIType *replacement_di_type = ZigLLVMCreateDebugUnionType(g->dbuilder,
6609 ZigLLVMFileToScope(import->data.structure.root_struct->di_file), buf_ptr(&union_type->name),
6610 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1),
6611 union_type->data.unionation.union_abi_size * 8,
6612 most_aligned_union_member->abi_align * 8,
6613 0, union_inner_di_types,
6614 gen_field_count, 0, "");
6615
6616 ZigLLVMReplaceTemporary(g->dbuilder, union_type->llvm_di_type, replacement_di_type);
6617 union_type->llvm_di_type = replacement_di_type;
6618 return;
6619 }
65596620
65606621 LLVMTypeRef union_type_ref;
65616622 size_t padding_bytes = union_type->data.unionation.union_abi_size - most_aligned_union_member->abi_size;
......@@ -7018,8 +7079,8 @@ LLVMTypeRef get_llvm_type(CodeGen *g, ZigType *type) {
70187079 resolve_llvm_types(g, type);
70197080 assert(type->llvm_type != nullptr);
70207081 assert(type->llvm_di_type != nullptr);
7021 assert(type->abi_size == LLVMABISizeOfType(g->target_data_ref, type->llvm_type));
7022 assert(type->abi_align == LLVMABIAlignmentOfType(g->target_data_ref, type->llvm_type));
7082 assert(type->abi_size == 0 || type->abi_size == LLVMABISizeOfType(g->target_data_ref, type->llvm_type));
7083 assert(type->abi_align == 0 || type->abi_align == LLVMABIAlignmentOfType(g->target_data_ref, type->llvm_type));
70237084 return type->llvm_type;
70247085}
70257086
......@@ -7029,7 +7090,7 @@ ZigLLVMDIType *get_llvm_di_type(CodeGen *g, ZigType *type) {
70297090 resolve_llvm_types(g, type);
70307091 assert(type->llvm_type != nullptr);
70317092 assert(type->llvm_di_type != nullptr);
7032 assert(type->abi_size == LLVMABISizeOfType(g->target_data_ref, type->llvm_type));
7033 assert(type->abi_align == LLVMABIAlignmentOfType(g->target_data_ref, type->llvm_type));
7093 assert(type->abi_size == 0 || type->abi_size == LLVMABISizeOfType(g->target_data_ref, type->llvm_type));
7094 assert(type->abi_align == 0 || type->abi_align == LLVMABIAlignmentOfType(g->target_data_ref, type->llvm_type));
70347095 return type->llvm_di_type;
70357096}
src/codegen.cpp+22-14
......@@ -498,6 +498,8 @@ static LLVMValueRef fn_llvm_value(CodeGen *g, ZigFn *fn_table_entry) {
498498 } else {
499499 assert(entry->value->id == TldIdFn);
500500 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);
501503 tld_fn->fn_entry->llvm_value = LLVMAddFunction(g->module, buf_ptr(symbol_name),
502504 tld_fn->fn_entry->type_entry->data.fn.raw_type_ref);
503505 fn_table_entry->llvm_value = LLVMConstBitCast(tld_fn->fn_entry->llvm_value,
......@@ -6292,14 +6294,9 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
62926294 }
62936295 case ZigTypeIdUnion:
62946296 {
6295 BREAKPOINT; // TODO rework this logic to take into account the new layout
6296
62976297 // Force type_entry->data.unionation.union_llvm_type to get resolved
62986298 (void)get_llvm_type(g, type_entry);
62996299
6300 LLVMTypeRef union_type_ref = type_entry->data.unionation.union_llvm_type;
6301 assert(union_type_ref != nullptr);
6302
63036300 if (type_entry->data.unionation.gen_field_count == 0) {
63046301 if (type_entry->data.unionation.tag_type == nullptr) {
63056302 return nullptr;
......@@ -6309,6 +6306,9 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
63096306 }
63106307 }
63116308
6309 LLVMTypeRef union_type_ref = type_entry->data.unionation.union_llvm_type;
6310 assert(union_type_ref != nullptr);
6311
63126312 LLVMValueRef union_value_ref;
63136313 bool make_unnamed_struct;
63146314 ConstExprValue *payload_value = const_val->data.x_union.payload;
......@@ -7740,8 +7740,15 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
77407740 buf_appendf(contents, "pub const valgrind_support = %s;\n", bool_to_str(want_valgrind_support(g)));
77417741 buf_appendf(contents, "pub const position_independent_code = %s;\n", bool_to_str(g->have_pic));
77427742
7743 buf_appendf(contents, "pub const __zig_test_fn_slice = {}; // overwritten later\n");
7744
7743 if (g->is_test_build) {
7744 buf_appendf(contents,
7745 "const TestFn = struct {\n"
7746 "name: []const u8,\n"
7747 "func: fn()anyerror!void,\n"
7748 "};\n"
7749 "pub const test_functions = {}; // overwritten later\n"
7750 );
7751 }
77457752
77467753 return contents;
77477754}
......@@ -8148,6 +8155,8 @@ static ZigPackage *create_panic_pkg(CodeGen *g) {
81488155}
81498156
81508157static void create_test_compile_var_and_add_test_runner(CodeGen *g) {
8158 Error err;
8159
81518160 assert(g->is_test_build);
81528161
81538162 if (g->test_fns.length == 0) {
......@@ -8155,14 +8164,13 @@ static void create_test_compile_var_and_add_test_runner(CodeGen *g) {
81558164 exit(0);
81568165 }
81578166
8158 ZigType *u8_ptr_type = get_pointer_to_type_extra(g, g->builtin_types.entry_u8, true, false,
8159 PtrLenUnknown, get_abi_alignment(g, g->builtin_types.entry_u8), 0, 0, false);
8160 ZigType *str_type = get_slice_type(g, u8_ptr_type);
81618167 ZigType *fn_type = get_test_fn_type(g);
81628168
8163 const char *field_names[] = { "name", "func", };
8164 ZigType *field_types[] = { str_type, fn_type, };
8165 ZigType *struct_type = get_struct_type(g, "ZigTestFn", field_names, field_types, 2);
8169 ConstExprValue *test_fn_type_val = get_builtin_value(g, "TestFn");
8170 assert(test_fn_type_val->type->id == ZigTypeIdMetaType);
8171 ZigType *struct_type = test_fn_type_val->data.x_type;
8172 if ((err = type_resolve(g, struct_type, ResolveStatusSizeKnown)))
8173 zig_unreachable();
81668174
81678175 ConstExprValue *test_fn_array = create_const_vals(1);
81688176 test_fn_array->type = get_array_type(g, struct_type, g->test_fns.length);
......@@ -8194,7 +8202,7 @@ static void create_test_compile_var_and_add_test_runner(CodeGen *g) {
81948202
81958203 ConstExprValue *test_fn_slice = create_const_slice(g, test_fn_array, 0, g->test_fns.length, true);
81968204
8197 update_compile_var(g, buf_create_from_str("__zig_test_fn_slice"), test_fn_slice);
8205 update_compile_var(g, buf_create_from_str("test_functions"), test_fn_slice);
81988206 g->test_runner_package = create_test_runner_pkg(g);
81998207 g->test_runner_import = add_special_code(g, g->test_runner_package, "test_runner.zig");
82008208}
src/ir.cpp+3-5
......@@ -8945,11 +8945,9 @@ static ZigType *get_error_set_intersection(IrAnalyze *ira, ZigType *set1, ZigTyp
89458945
89468946 err_set_type->data.error_set.err_count = intersection_list.length;
89478947 err_set_type->data.error_set.errors = intersection_list.items;
8948 if (intersection_list.length != 0) {
8949 err_set_type->size_in_bits = ira->codegen->builtin_types.entry_global_error_set->size_in_bits;
8950 err_set_type->abi_align = ira->codegen->builtin_types.entry_global_error_set->abi_align;
8951 err_set_type->abi_size = ira->codegen->builtin_types.entry_global_error_set->abi_size;
8952 }
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;
89538951
89548952 buf_appendf(&err_set_type->name, "}");
89558953
std/special/test_runner.zig+1-1
......@@ -1,7 +1,7 @@
11const std = @import("std");
22const io = std.io;
33const builtin = @import("builtin");
4const test_fn_list = builtin.__zig_test_fn_slice;
4const test_fn_list = builtin.test_functions;
55const warn = std.debug.warn;
66
77pub fn main() !void {