authorgravatar for joachim.schmidt557@outlook.comJoachim Schmidt <joachim.schmidt557@outlook.com> 2020-10-05 10:13:56+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-10-05 04:48:58-04:00
log7c5a24e08cd0bffd2a5cce6d1fd592a7d2bee678
tree40b072ebb155f4c487f7c01b009a79d034e7df73
parent9952a072cc120f5dc134bb1070ad02dfab59cefa

Turn zig fmt back on in various src/ files


3 files changed, 86 insertions(+), 71 deletions(-)

src/codegen.zig+12-18
......@@ -17,9 +17,6 @@ const DW = std.dwarf;
1717const leb128 = std.debug.leb;
1818const log = std.log.scoped(.codegen);
1919
20// TODO Turn back on zig fmt when https://github.com/ziglang/zig/issues/5948 is implemented.
21// zig fmt: off
22
2320/// The codegen-related data that is stored in `ir.Inst.Block` instructions.
2421pub const BlockData = struct {
2522 relocs: std.ArrayListUnmanaged(Reloc) = undefined,
......@@ -170,7 +167,6 @@ pub fn generateSymbol(
170167 },
171168 .Pointer => {
172169 // TODO populate .debug_info for the pointer
173
174170 if (typed_value.val.cast(Value.Payload.DeclRef)) |payload| {
175171 const decl = payload.decl;
176172 if (decl.analysis != .complete) return error.AnalysisFail;
......@@ -206,7 +202,6 @@ pub fn generateSymbol(
206202 },
207203 .Int => {
208204 // TODO populate .debug_info for the integer
209
210205 const info = typed_value.ty.intInfo(bin_file.options.target);
211206 if (info.bits == 8 and !info.signed) {
212207 const x = typed_value.val.toUnsignedInt();
......@@ -399,7 +394,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
399394 self.free_registers &= ~(@as(FreeRegInt, 1) << free_index);
400395 const reg = callee_preserved_regs[free_index];
401396 self.registers.putAssumeCapacityNoClobber(reg, inst);
402 log.debug("alloc {} => {*}", .{reg, inst});
397 log.debug("alloc {} => {*}", .{ reg, inst });
403398 return reg;
404399 }
405400
......@@ -439,7 +434,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
439434 }
440435 try branch_stack.append(.{});
441436
442 const src_data: struct {lbrace_src: usize, rbrace_src: usize, source: []const u8} = blk: {
437 const src_data: struct { lbrace_src: usize, rbrace_src: usize, source: []const u8 } = blk: {
443438 if (module_fn.owner_decl.scope.cast(Module.Scope.Container)) |container_scope| {
444439 const tree = container_scope.file_scope.contents.tree;
445440 const fn_proto = tree.root_node.decls()[module_fn.owner_decl.src_index].castTag(.FnProto).?;
......@@ -619,7 +614,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
619614
620615 const mcv = try self.genFuncInst(inst);
621616 if (!inst.isUnused()) {
622 log.debug("{*} => {}", .{inst, mcv});
617 log.debug("{*} => {}", .{ inst, mcv });
623618 const branch = &self.branch_stack.items[self.branch_stack.items.len - 1];
624619 try branch.inst_table.putNoClobber(self.gpa, inst, mcv);
625620 }
......@@ -884,7 +879,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
884879 // No side effects, so if it's unreferenced, do nothing.
885880 if (inst.base.isUnused())
886881 return MCValue.dead;
887
882
888883 const operand = try self.resolveInst(inst.operand);
889884 const info_a = inst.operand.ty.intInfo(self.target.*);
890885 const info_b = inst.base.ty.intInfo(self.target.*);
......@@ -1005,10 +1000,10 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
10051000 if (self.registers.getEntry(toCanonicalReg(reg))) |entry| {
10061001 entry.value = inst;
10071002 }
1008 log.debug("reusing {} => {*}", .{reg, inst});
1003 log.debug("reusing {} => {*}", .{ reg, inst });
10091004 },
10101005 .stack_offset => |off| {
1011 log.debug("reusing stack offset {} => {*}", .{off, inst});
1006 log.debug("reusing stack offset {} => {*}", .{ off, inst });
10121007 return true;
10131008 },
10141009 else => return false,
......@@ -1307,7 +1302,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
13071302 const result = self.args[self.arg_index];
13081303 self.arg_index += 1;
13091304
1310 const name_with_null = inst.name[0..mem.lenZ(inst.name) + 1];
1305 const name_with_null = inst.name[0 .. mem.lenZ(inst.name) + 1];
13111306 switch (result) {
13121307 .register => |reg| {
13131308 self.registers.putAssumeCapacityNoClobber(toCanonicalReg(reg), &inst.base);
......@@ -1779,7 +1774,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
17791774 self.code.items.len += 4;
17801775 break :reloc reloc;
17811776 },
1782 else => return self.fail(inst.base.src, "TODO implement condbr {}", .{ self.target.cpu.arch }),
1777 else => return self.fail(inst.base.src, "TODO implement condbr {}", .{self.target.cpu.arch}),
17831778 };
17841779
17851780 // Capture the state of register and stack allocation state so that we can revert to it.
......@@ -1859,7 +1854,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
18591854 }
18601855 }
18611856 };
1862 log.debug("consolidating else_entry {*} {}=>{}", .{else_entry.key, else_entry.value, canon_mcv});
1857 log.debug("consolidating else_entry {*} {}=>{}", .{ else_entry.key, else_entry.value, canon_mcv });
18631858 // TODO make sure the destination stack offset / register does not already have something
18641859 // going on there.
18651860 try self.setRegOrMem(inst.base.src, else_entry.key.ty, canon_mcv, else_entry.value);
......@@ -1883,7 +1878,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
18831878 }
18841879 }
18851880 };
1886 log.debug("consolidating then_entry {*} {}=>{}", .{then_entry.key, parent_mcv, then_entry.value});
1881 log.debug("consolidating then_entry {*} {}=>{}", .{ then_entry.key, parent_mcv, then_entry.value });
18871882 // TODO make sure the destination stack offset / register does not already have something
18881883 // going on there.
18891884 try self.setRegOrMem(inst.base.src, then_entry.key.ty, parent_mcv, then_entry.value);
......@@ -1950,7 +1945,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
19501945 // break instruction will choose a MCValue for the block result and overwrite
19511946 // this field. Following break instructions will use that MCValue to put their
19521947 // block results.
1953 .mcv = @bitCast(AnyMCValue, MCValue { .none = {} }),
1948 .mcv = @bitCast(AnyMCValue, MCValue{ .none = {} }),
19541949 };
19551950 defer inst.codegen.relocs.deinit(self.gpa);
19561951
......@@ -2232,7 +2227,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
22322227 mem.writeIntLittle(u64, &buf, x_big);
22332228
22342229 // mov DWORD PTR [rbp+offset+4], immediate
2235 self.code.appendSliceAssumeCapacity(&[_]u8{ 0xc7, 0x45, twos_comp + 4});
2230 self.code.appendSliceAssumeCapacity(&[_]u8{ 0xc7, 0x45, twos_comp + 4 });
22362231 self.code.appendSliceAssumeCapacity(buf[4..8]);
22372232
22382233 // mov DWORD PTR [rbp+offset], immediate
......@@ -2288,7 +2283,6 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
22882283 } else if (x <= math.maxInt(u16)) {
22892284 // TODO Use movw Note: Not supported on
22902285 // all ARM targets!
2291
22922286 mem.writeIntLittle(u32, try self.code.addManyAsArray(4), Instruction.mov(.al, reg, Instruction.Operand.imm(@truncate(u8, x), 0)).toU32());
22932287 mem.writeIntLittle(u32, try self.code.addManyAsArray(4), Instruction.orr(.al, reg, reg, Instruction.Operand.imm(@truncate(u8, x >> 8), 12)).toU32());
22942288 } else if (x <= math.maxInt(u32)) {
src/glibc.zig+18-9
......@@ -689,9 +689,6 @@ pub const BuiltSharedObjects = struct {
689689
690690const all_map_basename = "all.map";
691691
692// TODO Turn back on zig fmt when https://github.com/ziglang/zig/issues/5948 is implemented.
693// zig fmt: off
694
695692pub fn buildSharedObjects(comp: *Compilation) !void {
696693 const tracy = trace(@src());
697694 defer tracy.end();
......@@ -827,8 +824,9 @@ pub fn buildSharedObjects(comp: *Compilation) !void {
827824
828825 if (ver.patch == 0) {
829826 const sym_plus_ver = try std.fmt.allocPrint(
830 arena, "{s}_{d}_{d}",
831 .{sym_name, ver.major, ver.minor},
827 arena,
828 "{s}_{d}_{d}",
829 .{ sym_name, ver.major, ver.minor },
832830 );
833831 try zig_body.writer().print(
834832 \\.globl {s}
......@@ -840,13 +838,19 @@ pub fn buildSharedObjects(comp: *Compilation) !void {
840838 , .{
841839 sym_plus_ver,
842840 sym_plus_ver,
843 sym_plus_ver, sym_name, at_sign_str, ver.major, ver.minor,
841 sym_plus_ver,
842 sym_name,
843 at_sign_str,
844 ver.major,
845 ver.minor,
844846 sym_plus_ver,
845847 sym_plus_ver,
846848 });
847849 } else {
848 const sym_plus_ver = try std.fmt.allocPrint(arena, "{s}_{d}_{d}_{d}",
849 .{sym_name, ver.major, ver.minor, ver.patch},
850 const sym_plus_ver = try std.fmt.allocPrint(
851 arena,
852 "{s}_{d}_{d}_{d}",
853 .{ sym_name, ver.major, ver.minor, ver.patch },
850854 );
851855 try zig_body.writer().print(
852856 \\.globl {s}
......@@ -858,7 +862,12 @@ pub fn buildSharedObjects(comp: *Compilation) !void {
858862 , .{
859863 sym_plus_ver,
860864 sym_plus_ver,
861 sym_plus_ver, sym_name, at_sign_str, ver.major, ver.minor, ver.patch,
865 sym_plus_ver,
866 sym_name,
867 at_sign_str,
868 ver.major,
869 ver.minor,
870 ver.patch,
862871 sym_plus_ver,
863872 sym_plus_ver,
864873 });
src/link/Elf.zig+56-44
......@@ -27,9 +27,6 @@ const Cache = @import("../Cache.zig");
2727
2828const default_entry_addr = 0x8000000;
2929
30// TODO Turn back on zig fmt when https://github.com/ziglang/zig/issues/5948 is implemented.
31// zig fmt: off
32
3330pub const base_tag: File.Tag = .elf;
3431
3532base: File,
......@@ -273,8 +270,8 @@ pub fn openPath(allocator: *Allocator, sub_path: []const u8, options: link.Optio
273270
274271pub fn createEmpty(gpa: *Allocator, options: link.Options) !*Elf {
275272 const ptr_width: PtrWidth = switch (options.target.cpu.arch.ptrBitWidth()) {
276 0 ... 32 => .p32,
277 33 ... 64 => .p64,
273 0...32 => .p32,
274 33...64 => .p64,
278275 else => return error.UnsupportedELFArchitecture,
279276 };
280277 const self = try gpa.create(Elf);
......@@ -752,40 +749,52 @@ pub fn flushModule(self: *Elf, comp: *Compilation) !void {
752749 // These are LEB encoded but since the values are all less than 127
753750 // we can simply append these bytes.
754751 const abbrev_buf = [_]u8{
755 abbrev_compile_unit, DW.TAG_compile_unit, DW.CHILDREN_yes, // header
756 DW.AT_stmt_list, DW.FORM_sec_offset, DW.AT_low_pc,
757 DW.FORM_addr, DW.AT_high_pc, DW.FORM_addr,
758 DW.AT_name, DW.FORM_strp, DW.AT_comp_dir,
759 DW.FORM_strp, DW.AT_producer, DW.FORM_strp,
760 DW.AT_language, DW.FORM_data2, 0,
752 abbrev_compile_unit, DW.TAG_compile_unit, DW.CHILDREN_yes, // header
753 DW.AT_stmt_list, DW.FORM_sec_offset, DW.AT_low_pc,
754 DW.FORM_addr, DW.AT_high_pc, DW.FORM_addr,
755 DW.AT_name, DW.FORM_strp, DW.AT_comp_dir,
756 DW.FORM_strp, DW.AT_producer, DW.FORM_strp,
757 DW.AT_language, DW.FORM_data2, 0,
761758 0, // table sentinel
762 abbrev_subprogram, DW.TAG_subprogram,
759 abbrev_subprogram,
760 DW.TAG_subprogram,
763761 DW.CHILDREN_yes, // header
764 DW.AT_low_pc, DW.FORM_addr,
765 DW.AT_high_pc, DW.FORM_data4, DW.AT_type,
766 DW.FORM_ref4, DW.AT_name, DW.FORM_string,
767 0, 0, // table sentinel
768 abbrev_subprogram_retvoid,
769 DW.TAG_subprogram, DW.CHILDREN_yes, // header
770 DW.AT_low_pc,
771 DW.FORM_addr, DW.AT_high_pc, DW.FORM_data4,
772 DW.AT_name, DW.FORM_string, 0,
762 DW.AT_low_pc,
763 DW.FORM_addr,
764 DW.AT_high_pc,
765 DW.FORM_data4,
766 DW.AT_type,
767 DW.FORM_ref4,
768 DW.AT_name,
769 DW.FORM_string,
770 0, 0, // table sentinel
771 abbrev_subprogram_retvoid,
772 DW.TAG_subprogram, DW.CHILDREN_yes, // header
773 DW.AT_low_pc, DW.FORM_addr,
774 DW.AT_high_pc, DW.FORM_data4,
775 DW.AT_name, DW.FORM_string,
776 0,
773777 0, // table sentinel
774 abbrev_base_type, DW.TAG_base_type,
778 abbrev_base_type,
779 DW.TAG_base_type,
775780 DW.CHILDREN_no, // header
776 DW.AT_encoding, DW.FORM_data1,
777 DW.AT_byte_size, DW.FORM_data1, DW.AT_name,
778 DW.FORM_string, 0, 0, // table sentinel
779
780 abbrev_pad1, DW.TAG_unspecified_type, DW.CHILDREN_no, // header
781 0, 0, // table sentinel
782 abbrev_parameter,
781 DW.AT_encoding,
782 DW.FORM_data1,
783 DW.AT_byte_size,
784 DW.FORM_data1,
785 DW.AT_name,
786 DW.FORM_string, 0, 0, // table sentinel
787 abbrev_pad1, DW.TAG_unspecified_type, DW.CHILDREN_no, // header
788 0, 0, // table sentinel
789 abbrev_parameter,
783790 DW.TAG_formal_parameter, DW.CHILDREN_no, // header
784 DW.AT_location,
785 DW.FORM_exprloc, DW.AT_type, DW.FORM_ref4,
786 DW.AT_name, DW.FORM_string, 0,
791 DW.AT_location, DW.FORM_exprloc,
792 DW.AT_type, DW.FORM_ref4,
793 DW.AT_name, DW.FORM_string,
794 0,
787795 0, // table sentinel
788 0, 0,
796 0,
797 0,
789798 0, // section sentinel
790799 };
791800
......@@ -1021,7 +1030,6 @@ pub fn flushModule(self: *Elf, comp: *Compilation) !void {
10211030 0, // `DW.LNS_set_prologue_end`
10221031 0, // `DW.LNS_set_epilogue_begin`
10231032 1, // `DW.LNS_set_isa`
1024
10251033 0, // include_directories (none except the compilation unit cwd)
10261034 });
10271035 // file_names[0]
......@@ -1319,7 +1327,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
13191327
13201328 var prev_digest_buf: [digest.len]u8 = undefined;
13211329 const prev_digest: []u8 = directory.handle.readLink(id_symlink_basename, &prev_digest_buf) catch |err| blk: {
1322 log.debug("ELF LLD new_digest={} readlink error: {}", .{digest, @errorName(err)});
1330 log.debug("ELF LLD new_digest={} readlink error: {}", .{ digest, @errorName(err) });
13231331 // Handle this as a cache miss.
13241332 break :blk prev_digest_buf[0..0];
13251333 };
......@@ -1329,7 +1337,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
13291337 self.base.lock = man.toOwnedLock();
13301338 return;
13311339 }
1332 log.debug("ELF LLD prev_digest={} new_digest={}", .{prev_digest, digest});
1340 log.debug("ELF LLD prev_digest={} new_digest={}", .{ prev_digest, digest });
13331341
13341342 // We are about to change the output file to be different, so we invalidate the build hash now.
13351343 directory.handle.deleteFile(id_symlink_basename) catch |err| switch (err) {
......@@ -1370,7 +1378,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
13701378 if (self.base.options.eh_frame_hdr) {
13711379 try argv.append("--eh-frame-hdr");
13721380 }
1373
1381
13741382 if (self.base.options.emit_relocs) {
13751383 try argv.append("--emit-relocs");
13761384 }
......@@ -1491,9 +1499,9 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
14911499
14921500 if (is_dyn_lib) {
14931501 const soname = self.base.options.override_soname orelse if (self.base.options.version) |ver|
1494 try std.fmt.allocPrint(arena, "lib{}.so.{}", .{self.base.options.root_name, ver.major})
1495 else
1496 try std.fmt.allocPrint(arena, "lib{}.so", .{self.base.options.root_name});
1502 try std.fmt.allocPrint(arena, "lib{}.so.{}", .{ self.base.options.root_name, ver.major })
1503 else
1504 try std.fmt.allocPrint(arena, "lib{}.so", .{self.base.options.root_name});
14971505 try argv.append("-soname");
14981506 try argv.append(soname);
14991507
......@@ -1616,7 +1624,11 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
16161624 };
16171625 defer stdout_context.data.deinit();
16181626 const llvm = @import("../llvm.zig");
1619 const ok = llvm.Link(.ELF, new_argv.ptr, new_argv.len, append_diagnostic,
1627 const ok = llvm.Link(
1628 .ELF,
1629 new_argv.ptr,
1630 new_argv.len,
1631 append_diagnostic,
16201632 @ptrToInt(&stdout_context),
16211633 @ptrToInt(&stderr_context),
16221634 );
......@@ -1642,7 +1654,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
16421654 };
16431655 // Again failure here only means an unnecessary cache miss.
16441656 man.writeManifest() catch |err| {
1645 std.log.warn("failed to write cache manifest when linking: {}", .{ @errorName(err) });
1657 std.log.warn("failed to write cache manifest when linking: {}", .{@errorName(err)});
16461658 };
16471659 // We hang on to this lock so that the output file path can be used without
16481660 // other processes clobbering it.
......@@ -2877,8 +2889,8 @@ fn dbgLineNeededHeaderBytes(self: Elf) u32 {
28772889 const root_src_dir_path_len = if (self.base.options.module.?.root_pkg.root_src_directory.path) |p| p.len else 1; // "."
28782890 return @intCast(u32, 53 + directory_entry_format_count * 2 + file_name_entry_format_count * 2 +
28792891 directory_count * 8 + file_name_count * 8 +
2880 // These are encoded as DW.FORM_string rather than DW.FORM_strp as we would like
2881 // because of a workaround for readelf and gdb failing to understand DWARFv5 correctly.
2892 // These are encoded as DW.FORM_string rather than DW.FORM_strp as we would like
2893 // because of a workaround for readelf and gdb failing to understand DWARFv5 correctly.
28822894 root_src_dir_path_len +
28832895 self.base.options.module.?.root_pkg.root_src_path.len);
28842896}