authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-04-26 22:51:35-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-05-01 19:22:52-04:00
log7c9891d7b7d09060630693231702f27669dd0dc9
tree7afde18d6e0ff9e27d00f31a57ebc19e89d95396
parent28923474401051a9aa0bddd60904b9be64943dba

x86_64: use std.log for debug logging


2 files changed, 104 insertions(+), 45 deletions(-)

src/arch/x86_64/CodeGen.zig+95-42
...@@ -7,6 +7,8 @@ const leb128 = std.leb;...@@ -7,6 +7,8 @@ const leb128 = std.leb;
7const link = @import("../../link.zig");7const link = @import("../../link.zig");
8const log = std.log.scoped(.codegen);8const log = std.log.scoped(.codegen);
9const tracking_log = std.log.scoped(.tracking);9const tracking_log = std.log.scoped(.tracking);
10const verbose_tracking_log = std.log.scoped(.verbose_tracking);
11const wip_mir_log = std.log.scoped(.wip_mir);
10const math = std.math;12const math = std.math;
11const mem = std.mem;13const mem = std.mem;
12const trace = @import("../../tracy.zig").trace;14const trace = @import("../../tracy.zig").trace;
...@@ -48,9 +50,6 @@ const sse = abi.RegisterClass.sse;...@@ -48,9 +50,6 @@ const sse = abi.RegisterClass.sse;
4850
49const InnerError = CodeGenError || error{OutOfRegisters};51const InnerError = CodeGenError || error{OutOfRegisters};
5052
51const debug_wip_mir = false;
52const debug_tracking = false;
53
54gpa: Allocator,53gpa: Allocator,
55air: Air,54air: Air,
56liveness: Liveness,55liveness: Liveness,
...@@ -575,12 +574,6 @@ pub fn generate(...@@ -575,12 +574,6 @@ pub fn generate(
575 assert(fn_owner_decl.has_tv);574 assert(fn_owner_decl.has_tv);
576 const fn_type = fn_owner_decl.ty;575 const fn_type = fn_owner_decl.ty;
577576
578 if (debug_wip_mir) {
579 const stderr = std.io.getStdErr().writer();
580 fn_owner_decl.renderFullyQualifiedName(mod, stderr) catch {};
581 stderr.writeAll(":\n") catch {};
582 }
583
584 const gpa = bin_file.allocator;577 const gpa = bin_file.allocator;
585 var function = Self{578 var function = Self{
586 .gpa = gpa,579 .gpa = gpa,
...@@ -614,6 +607,8 @@ pub fn generate(...@@ -614,6 +607,8 @@ pub fn generate(
614 if (builtin.mode == .Debug) function.mir_to_air_map.deinit(gpa);607 if (builtin.mode == .Debug) function.mir_to_air_map.deinit(gpa);
615 }608 }
616609
610 wip_mir_log.debug("{}:", .{function.fmtDecl(module_fn.owner_decl)});
611
617 try function.frame_allocs.resize(gpa, FrameIndex.named_count);612 try function.frame_allocs.resize(gpa, FrameIndex.named_count);
618 function.frame_allocs.set(613 function.frame_allocs.set(
619 @enumToInt(FrameIndex.stack_frame),614 @enumToInt(FrameIndex.stack_frame),
...@@ -715,48 +710,104 @@ pub fn generate(...@@ -715,48 +710,104 @@ pub fn generate(
715 }710 }
716}711}
717712
718fn dumpWipMir(self: *Self, inst: Mir.Inst) !void {713const FormatDeclData = struct {
719 if (!debug_wip_mir) return;714 mod: *Module,
720 const stderr = std.io.getStdErr().writer();715 decl_index: Module.Decl.Index,
716};
717fn formatDecl(
718 data: FormatDeclData,
719 comptime _: []const u8,
720 _: std.fmt.FormatOptions,
721 writer: anytype,
722) @TypeOf(writer).Error!void {
723 try data.mod.declPtr(data.decl_index).renderFullyQualifiedName(data.mod, writer);
724}
725fn fmtDecl(self: *Self, decl_index: Module.Decl.Index) std.fmt.Formatter(formatDecl) {
726 return .{ .data = .{
727 .mod = self.bin_file.options.module.?,
728 .decl_index = decl_index,
729 } };
730}
731
732const FormatAirData = struct {
733 self: *Self,
734 inst: Air.Inst.Index,
735};
736fn formatAir(
737 data: FormatAirData,
738 comptime _: []const u8,
739 _: std.fmt.FormatOptions,
740 writer: anytype,
741) @TypeOf(writer).Error!void {
742 @import("../../print_air.zig").dumpInst(
743 data.inst,
744 data.self.bin_file.options.module.?,
745 data.self.air,
746 data.self.liveness,
747 );
748}
749fn fmtAir(self: *Self, inst: Air.Inst.Index) std.fmt.Formatter(formatAir) {
750 return .{ .data = .{ .self = self, .inst = inst } };
751}
721752
753const FormatWipMirData = struct {
754 self: *Self,
755 inst: Mir.Inst.Index,
756};
757fn formatWipMir(
758 data: FormatWipMirData,
759 comptime _: []const u8,
760 _: std.fmt.FormatOptions,
761 writer: anytype,
762) @TypeOf(writer).Error!void {
722 var lower = Lower{763 var lower = Lower{
723 .allocator = self.gpa,764 .allocator = data.self.gpa,
724 .mir = .{765 .mir = .{
725 .instructions = self.mir_instructions.slice(),766 .instructions = data.self.mir_instructions.slice(),
726 .extra = self.mir_extra.items,767 .extra = data.self.mir_extra.items,
727 .frame_locs = (std.MultiArrayList(Mir.FrameLoc){}).slice(),768 .frame_locs = (std.MultiArrayList(Mir.FrameLoc){}).slice(),
728 },769 },
729 .target = self.target,770 .target = data.self.target,
730 .src_loc = self.src_loc,771 .src_loc = data.self.src_loc,
731 };772 };
732 for (lower.lowerMir(inst) catch |err| switch (err) {773 for (lower.lowerMir(data.self.mir_instructions.get(data.inst)) catch |err| switch (err) {
733 error.LowerFail => {774 error.LowerFail => {
734 defer {775 defer {
735 lower.err_msg.?.deinit(self.gpa);776 lower.err_msg.?.deinit(data.self.gpa);
736 lower.err_msg = null;777 lower.err_msg = null;
737 }778 }
738 try stderr.print("{s}\n", .{lower.err_msg.?.msg});779 try writer.writeAll(lower.err_msg.?.msg);
739 return;780 return;
740 },781 },
741 error.InvalidInstruction, error.CannotEncode => |e| {782 error.OutOfMemory, error.InvalidInstruction, error.CannotEncode => |e| {
742 try stderr.writeAll(switch (e) {783 try writer.writeAll(switch (e) {
743 error.InvalidInstruction => "CodeGen failed to find a viable instruction.\n",784 error.OutOfMemory => "Out of memory",
744 error.CannotEncode => "CodeGen failed to encode the instruction.\n",785 error.InvalidInstruction => "CodeGen failed to find a viable instruction.",
786 error.CannotEncode => "CodeGen failed to encode the instruction.",
745 });787 });
746 return;788 return;
747 },789 },
748 else => |e| return e,790 else => |e| return e,
749 }) |lower_inst| {791 }) |lower_inst| try writer.print(" | {}", .{lower_inst});
750 try stderr.print(" | {}\n", .{lower_inst});792}
751 }793fn fmtWipMir(self: *Self, inst: Mir.Inst.Index) std.fmt.Formatter(formatWipMir) {
794 return .{ .data = .{ .self = self, .inst = inst } };
752}795}
753796
754fn dumpTracking(self: *Self) !void {797const FormatTrackingData = struct {
755 if (!debug_tracking) return;798 self: *Self,
756 const stderr = std.io.getStdErr().writer();799};
757800fn formatTracking(
758 var it = self.inst_tracking.iterator();801 data: FormatTrackingData,
759 while (it.next()) |entry| try stderr.print("%{d} = {}\n", .{ entry.key_ptr.*, entry.value_ptr.* });802 comptime _: []const u8,
803 _: std.fmt.FormatOptions,
804 writer: anytype,
805) @TypeOf(writer).Error!void {
806 var it = data.self.inst_tracking.iterator();
807 while (it.next()) |entry| try writer.print("\n%{d} = {}", .{ entry.key_ptr.*, entry.value_ptr.* });
808}
809fn fmtTracking(self: *Self) std.fmt.Formatter(formatTracking) {
810 return .{ .data = .{ .self = self } };
760}811}
761812
762fn addInst(self: *Self, inst: Mir.Inst) error{OutOfMemory}!Mir.Inst.Index {813fn addInst(self: *Self, inst: Mir.Inst) error{OutOfMemory}!Mir.Inst.Index {
...@@ -764,7 +815,14 @@ fn addInst(self: *Self, inst: Mir.Inst) error{OutOfMemory}!Mir.Inst.Index {...@@ -764,7 +815,14 @@ fn addInst(self: *Self, inst: Mir.Inst) error{OutOfMemory}!Mir.Inst.Index {
764 try self.mir_instructions.ensureUnusedCapacity(gpa, 1);815 try self.mir_instructions.ensureUnusedCapacity(gpa, 1);
765 const result_index = @intCast(Mir.Inst.Index, self.mir_instructions.len);816 const result_index = @intCast(Mir.Inst.Index, self.mir_instructions.len);
766 self.mir_instructions.appendAssumeCapacity(inst);817 self.mir_instructions.appendAssumeCapacity(inst);
767 self.dumpWipMir(inst) catch {};818 switch (inst.tag) {
819 else => wip_mir_log.debug("{}", .{self.fmtWipMir(result_index)}),
820 .dbg_line,
821 .dbg_prologue_end,
822 .dbg_epilogue_begin,
823 .dead,
824 => {},
825 }
768 return result_index;826 return result_index;
769}827}
770828
...@@ -1186,13 +1244,8 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {...@@ -1186,13 +1244,8 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
1186 }1244 }
11871245
1188 if (self.liveness.isUnused(inst) and !self.air.mustLower(inst)) continue;1246 if (self.liveness.isUnused(inst) and !self.air.mustLower(inst)) continue;
1189 if (debug_wip_mir) @import("../../print_air.zig").dumpInst(1247 wip_mir_log.debug("{}", .{self.fmtAir(inst)});
1190 inst,1248 verbose_tracking_log.debug("{}", .{self.fmtTracking()});
1191 self.bin_file.options.module.?,
1192 self.air,
1193 self.liveness,
1194 );
1195 self.dumpTracking() catch {};
11961249
1197 const old_air_bookkeeping = self.air_bookkeeping;1250 const old_air_bookkeeping = self.air_bookkeeping;
1198 try self.inst_tracking.ensureUnusedCapacity(self.gpa, 1);1251 try self.inst_tracking.ensureUnusedCapacity(self.gpa, 1);
...@@ -1453,7 +1506,7 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {...@@ -1453,7 +1506,7 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
1453 }1506 }
1454 }1507 }
1455 }1508 }
1456 self.dumpTracking() catch {};1509 verbose_tracking_log.debug("{}", .{self.fmtTracking()});
1457}1510}
14581511
1459fn getValue(self: *Self, value: MCValue, inst: ?Air.Inst.Index) void {1512fn getValue(self: *Self, value: MCValue, inst: ?Air.Inst.Index) void {
src/print_air.zig+9-3
...@@ -94,14 +94,20 @@ const Writer = struct {...@@ -94,14 +94,20 @@ const Writer = struct {
94 for (w.air.instructions.items(.tag), 0..) |tag, i| {94 for (w.air.instructions.items(.tag), 0..) |tag, i| {
95 const inst = @intCast(Air.Inst.Index, i);95 const inst = @intCast(Air.Inst.Index, i);
96 switch (tag) {96 switch (tag) {
97 .constant, .const_ty => try w.writeInst(s, inst),97 .constant, .const_ty => {
98 try w.writeInst(s, inst);
99 try s.writeByte('\n');
100 },
98 else => continue,101 else => continue,
99 }102 }
100 }103 }
101 }104 }
102105
103 fn writeBody(w: *Writer, s: anytype, body: []const Air.Inst.Index) @TypeOf(s).Error!void {106 fn writeBody(w: *Writer, s: anytype, body: []const Air.Inst.Index) @TypeOf(s).Error!void {
104 for (body) |inst| try w.writeInst(s, inst);107 for (body) |inst| {
108 try w.writeInst(s, inst);
109 try s.writeByte('\n');
110 }
105 }111 }
106112
107 fn writeInst(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {113 fn writeInst(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
...@@ -336,7 +342,7 @@ const Writer = struct {...@@ -336,7 +342,7 @@ const Writer = struct {
336 .work_group_id,342 .work_group_id,
337 => try w.writeWorkDimension(s, inst),343 => try w.writeWorkDimension(s, inst),
338 }344 }
339 try s.writeAll(")\n");345 try s.writeByte(')');
340 }346 }
341347
342 fn writeBinOp(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {348 fn writeBinOp(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {