authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-07-07 03:48:20+00:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-07-07 03:48:20+00:00
log4d01385e147ae36ad96a1c28d2b6fdec69ce69c9
treed7cb1e0603857a77cb99dd5f02c79c8bd1b9c54a
parent12737c9a3071a49f8c05348fadd0b602b6952542

fix liveness analysis and not correctly propagating link errors

We still flush the ELF file even when there are compile errors.

4 files changed, 40 insertions(+), 29 deletions(-)

src-self-hosted/Module.zig+7-5
......@@ -867,15 +867,16 @@ pub fn update(self: *Module) !void {
867867 try self.deleteDecl(decl);
868868 }
869869
870 // This is needed before reading the error flags.
871 try self.bin_file.flush();
872
870873 self.link_error_flags = self.bin_file.error_flags;
874 std.log.debug(.module, "link_error_flags: {}\n", .{self.link_error_flags});
871875
872876 // If there are any errors, we anticipate the source files being loaded
873877 // to report error messages. Otherwise we unload all source files to save memory.
874 if (self.totalErrorCount() == 0) {
875 if (!self.keep_source_files_loaded) {
876 self.root_scope.unload(self.gpa);
877 }
878 try self.bin_file.flush();
878 if (self.totalErrorCount() == 0 and !self.keep_source_files_loaded) {
879 self.root_scope.unload(self.gpa);
879880 }
880881}
881882
......@@ -975,6 +976,7 @@ pub fn performAllTheWork(self: *Module) error{OutOfMemory}!void {
975976 // lifetime annotations in the ZIR.
976977 var decl_arena = decl.typed_value.most_recent.arena.?.promote(self.gpa);
977978 defer decl.typed_value.most_recent.arena.?.* = decl_arena.state;
979 std.log.debug(.module, "analyze liveness of {}\n", .{decl.name});
978980 try liveness.analyze(self.gpa, &decl_arena.allocator, payload.func.analysis.success);
979981 }
980982
src-self-hosted/link.zig+12-11
......@@ -369,7 +369,7 @@ pub const ElfFile = struct {
369369 const file_size = self.options.program_code_size_hint;
370370 const p_align = 0x1000;
371371 const off = self.findFreeSpace(file_size, p_align);
372 //std.log.debug(.link, "found PT_LOAD free space 0x{x} to 0x{x}\n", .{ off, off + file_size });
372 std.log.debug(.link, "found PT_LOAD free space 0x{x} to 0x{x}\n", .{ off, off + file_size });
373373 try self.program_headers.append(self.allocator, .{
374374 .p_type = elf.PT_LOAD,
375375 .p_offset = off,
......@@ -390,7 +390,7 @@ pub const ElfFile = struct {
390390 // page align.
391391 const p_align = 0x1000;
392392 const off = self.findFreeSpace(file_size, p_align);
393 //std.log.debug(.link, "found PT_LOAD free space 0x{x} to 0x{x}\n", .{ off, off + file_size });
393 std.log.debug(.link, "found PT_LOAD free space 0x{x} to 0x{x}\n", .{ off, off + file_size });
394394 // TODO instead of hard coding the vaddr, make a function to find a vaddr to put things at.
395395 // we'll need to re-use that function anyway, in case the GOT grows and overlaps something
396396 // else in virtual memory.
......@@ -412,7 +412,7 @@ pub const ElfFile = struct {
412412 assert(self.shstrtab.items.len == 0);
413413 try self.shstrtab.append(self.allocator, 0); // need a 0 at position 0
414414 const off = self.findFreeSpace(self.shstrtab.items.len, 1);
415 //std.log.debug(.link, "found shstrtab free space 0x{x} to 0x{x}\n", .{ off, off + self.shstrtab.items.len });
415 std.log.debug(.link, "found shstrtab free space 0x{x} to 0x{x}\n", .{ off, off + self.shstrtab.items.len });
416416 try self.sections.append(self.allocator, .{
417417 .sh_name = try self.makeString(".shstrtab"),
418418 .sh_type = elf.SHT_STRTAB,
......@@ -470,7 +470,7 @@ pub const ElfFile = struct {
470470 const each_size: u64 = if (small_ptr) @sizeOf(elf.Elf32_Sym) else @sizeOf(elf.Elf64_Sym);
471471 const file_size = self.options.symbol_count_hint * each_size;
472472 const off = self.findFreeSpace(file_size, min_align);
473 //std.log.debug(.link, "found symtab free space 0x{x} to 0x{x}\n", .{ off, off + file_size });
473 std.log.debug(.link, "found symtab free space 0x{x} to 0x{x}\n", .{ off, off + file_size });
474474
475475 try self.sections.append(self.allocator, .{
476476 .sh_name = try self.makeString(".symtab"),
......@@ -586,7 +586,7 @@ pub const ElfFile = struct {
586586 shstrtab_sect.sh_offset = self.findFreeSpace(needed_size, 1);
587587 }
588588 shstrtab_sect.sh_size = needed_size;
589 //std.log.debug(.link, "shstrtab start=0x{x} end=0x{x}\n", .{ shstrtab_sect.sh_offset, shstrtab_sect.sh_offset + needed_size });
589 std.log.debug(.link, "shstrtab start=0x{x} end=0x{x}\n", .{ shstrtab_sect.sh_offset, shstrtab_sect.sh_offset + needed_size });
590590
591591 try self.file.?.pwriteAll(self.shstrtab.items, shstrtab_sect.sh_offset);
592592 if (!self.shdr_table_dirty) {
......@@ -632,7 +632,7 @@ pub const ElfFile = struct {
632632
633633 for (buf) |*shdr, i| {
634634 shdr.* = self.sections.items[i];
635 //std.log.debug(.link, "writing section {}\n", .{shdr.*});
635 std.log.debug(.link, "writing section {}\n", .{shdr.*});
636636 if (foreign_endian) {
637637 bswapAllFields(elf.Elf64_Shdr, shdr);
638638 }
......@@ -643,6 +643,7 @@ pub const ElfFile = struct {
643643 self.shdr_table_dirty = false;
644644 }
645645 if (self.entry_addr == null and self.options.output_mode == .Exe) {
646 std.log.debug(.link, "no_entry_point_found = true\n", .{});
646647 self.error_flags.no_entry_point_found = true;
647648 } else {
648649 self.error_flags.no_entry_point_found = false;
......@@ -956,10 +957,10 @@ pub const ElfFile = struct {
956957 try self.offset_table_free_list.ensureCapacity(self.allocator, self.local_symbols.items.len);
957958
958959 if (self.local_symbol_free_list.popOrNull()) |i| {
959 //std.log.debug(.link, "reusing symbol index {} for {}\n", .{i, decl.name});
960 std.log.debug(.link, "reusing symbol index {} for {}\n", .{i, decl.name});
960961 decl.link.local_sym_index = i;
961962 } else {
962 //std.log.debug(.link, "allocating symbol index {} for {}\n", .{self.local_symbols.items.len, decl.name});
963 std.log.debug(.link, "allocating symbol index {} for {}\n", .{self.local_symbols.items.len, decl.name});
963964 decl.link.local_sym_index = @intCast(u32, self.local_symbols.items.len);
964965 _ = self.local_symbols.addOneAssumeCapacity();
965966 }
......@@ -1027,11 +1028,11 @@ pub const ElfFile = struct {
10271028 !mem.isAlignedGeneric(u64, local_sym.st_value, required_alignment);
10281029 if (need_realloc) {
10291030 const vaddr = try self.growTextBlock(&decl.link, code.len, required_alignment);
1030 //std.log.debug(.link, "growing {} from 0x{x} to 0x{x}\n", .{ decl.name, local_sym.st_value, vaddr });
1031 std.log.debug(.link, "growing {} from 0x{x} to 0x{x}\n", .{ decl.name, local_sym.st_value, vaddr });
10311032 if (vaddr != local_sym.st_value) {
10321033 local_sym.st_value = vaddr;
10331034
1034 //std.log.debug(.link, " (writing new offset table entry)\n", .{});
1035 std.log.debug(.link, " (writing new offset table entry)\n", .{});
10351036 self.offset_table.items[decl.link.offset_table_index] = vaddr;
10361037 try self.writeOffsetTableEntry(decl.link.offset_table_index);
10371038 }
......@@ -1049,7 +1050,7 @@ pub const ElfFile = struct {
10491050 const decl_name = mem.spanZ(decl.name);
10501051 const name_str_index = try self.makeString(decl_name);
10511052 const vaddr = try self.allocateTextBlock(&decl.link, code.len, required_alignment);
1052 //std.log.debug(.link, "allocated text block for {} at 0x{x}\n", .{ decl_name, vaddr });
1053 std.log.debug(.link, "allocated text block for {} at 0x{x}\n", .{ decl_name, vaddr });
10531054 errdefer self.freeTextBlock(&decl.link);
10541055
10551056 local_sym.* = .{
src-self-hosted/liveness.zig+17-12
......@@ -25,22 +25,25 @@ fn analyzeWithTable(arena: *std.mem.Allocator, table: *std.AutoHashMap(*ir.Inst,
2525 while (i != 0) {
2626 i -= 1;
2727 const base = body.instructions[i];
28 try analyzeInstGeneric(arena, table, base);
29 }
30}
2831
29 // Obtain the corresponding instruction type based on the tag type.
30 inline for (std.meta.declarations(ir.Inst)) |decl| {
31 switch (decl.data) {
32 .Type => |T| {
33 if (@hasDecl(T, "base_tag")) {
34 if (T.base_tag == base.tag) {
35 return analyzeInst(arena, table, T, @fieldParentPtr(T, "base", base));
36 }
32fn analyzeInstGeneric(arena: *std.mem.Allocator, table: *std.AutoHashMap(*ir.Inst, void), base: *ir.Inst) error{OutOfMemory}!void {
33 // Obtain the corresponding instruction type based on the tag type.
34 inline for (std.meta.declarations(ir.Inst)) |decl| {
35 switch (decl.data) {
36 .Type => |T| {
37 if (@hasDecl(T, "base_tag")) {
38 if (T.base_tag == base.tag) {
39 return analyzeInst(arena, table, T, @fieldParentPtr(T, "base", base));
3740 }
38 },
39 else => continue,
40 }
41 }
42 },
43 else => {},
4144 }
42 unreachable;
4345 }
46 unreachable;
4447}
4548
4649fn analyzeInst(arena: *std.mem.Allocator, table: *std.AutoHashMap(*ir.Inst, void), comptime T: type, inst: *T) error{OutOfMemory}!void {
......@@ -131,4 +134,6 @@ fn analyzeInst(arena: *std.mem.Allocator, table: *std.AutoHashMap(*ir.Inst, void
131134 arg_index += 1;
132135 }
133136 }
137
138 std.log.debug(.liveness, "analyze {}: 0b{b}\n", .{inst.base.tag, inst.base.deaths});
134139}
src-self-hosted/main.zig+4-1
......@@ -50,7 +50,10 @@ pub fn log(
5050 const scope_prefix = "(" ++ switch (scope) {
5151 // Uncomment to hide logs
5252 //.compiler,
53 .link => return,
53 .module,
54 .liveness,
55 .link,
56 => return,
5457
5558 else => @tagName(scope),
5659 } ++ "): ";