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;
77const link = @import("../../link.zig");
88const log = std.log.scoped(.codegen);
99const tracking_log = std.log.scoped(.tracking);
10const verbose_tracking_log = std.log.scoped(.verbose_tracking);
11const wip_mir_log = std.log.scoped(.wip_mir);
1012const math = std.math;
1113const mem = std.mem;
1214const trace = @import("../../tracy.zig").trace;
......@@ -48,9 +50,6 @@ const sse = abi.RegisterClass.sse;
4850
4951const InnerError = CodeGenError || error{OutOfRegisters};
5052
51const debug_wip_mir = false;
52const debug_tracking = false;
53
5453gpa: Allocator,
5554air: Air,
5655liveness: Liveness,
......@@ -575,12 +574,6 @@ pub fn generate(
575574 assert(fn_owner_decl.has_tv);
576575 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
584577 const gpa = bin_file.allocator;
585578 var function = Self{
586579 .gpa = gpa,
......@@ -614,6 +607,8 @@ pub fn generate(
614607 if (builtin.mode == .Debug) function.mir_to_air_map.deinit(gpa);
615608 }
616609
610 wip_mir_log.debug("{}:", .{function.fmtDecl(module_fn.owner_decl)});
611
617612 try function.frame_allocs.resize(gpa, FrameIndex.named_count);
618613 function.frame_allocs.set(
619614 @enumToInt(FrameIndex.stack_frame),
......@@ -715,48 +710,104 @@ pub fn generate(
715710 }
716711}
717712
718fn dumpWipMir(self: *Self, inst: Mir.Inst) !void {
719 if (!debug_wip_mir) return;
720 const stderr = std.io.getStdErr().writer();
713const FormatDeclData = struct {
714 mod: *Module,
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 {
722763 var lower = Lower{
723 .allocator = self.gpa,
764 .allocator = data.self.gpa,
724765 .mir = .{
725 .instructions = self.mir_instructions.slice(),
726 .extra = self.mir_extra.items,
766 .instructions = data.self.mir_instructions.slice(),
767 .extra = data.self.mir_extra.items,
727768 .frame_locs = (std.MultiArrayList(Mir.FrameLoc){}).slice(),
728769 },
729 .target = self.target,
730 .src_loc = self.src_loc,
770 .target = data.self.target,
771 .src_loc = data.self.src_loc,
731772 };
732 for (lower.lowerMir(inst) catch |err| switch (err) {
773 for (lower.lowerMir(data.self.mir_instructions.get(data.inst)) catch |err| switch (err) {
733774 error.LowerFail => {
734775 defer {
735 lower.err_msg.?.deinit(self.gpa);
776 lower.err_msg.?.deinit(data.self.gpa);
736777 lower.err_msg = null;
737778 }
738 try stderr.print("{s}\n", .{lower.err_msg.?.msg});
779 try writer.writeAll(lower.err_msg.?.msg);
739780 return;
740781 },
741 error.InvalidInstruction, error.CannotEncode => |e| {
742 try stderr.writeAll(switch (e) {
743 error.InvalidInstruction => "CodeGen failed to find a viable instruction.\n",
744 error.CannotEncode => "CodeGen failed to encode the instruction.\n",
782 error.OutOfMemory, error.InvalidInstruction, error.CannotEncode => |e| {
783 try writer.writeAll(switch (e) {
784 error.OutOfMemory => "Out of memory",
785 error.InvalidInstruction => "CodeGen failed to find a viable instruction.",
786 error.CannotEncode => "CodeGen failed to encode the instruction.",
745787 });
746788 return;
747789 },
748790 else => |e| return e,
749 }) |lower_inst| {
750 try stderr.print(" | {}\n", .{lower_inst});
751 }
791 }) |lower_inst| try writer.print(" | {}", .{lower_inst});
792}
793fn fmtWipMir(self: *Self, inst: Mir.Inst.Index) std.fmt.Formatter(formatWipMir) {
794 return .{ .data = .{ .self = self, .inst = inst } };
752795}
753796
754fn dumpTracking(self: *Self) !void {
755 if (!debug_tracking) return;
756 const stderr = std.io.getStdErr().writer();
757
758 var it = self.inst_tracking.iterator();
759 while (it.next()) |entry| try stderr.print("%{d} = {}\n", .{ entry.key_ptr.*, entry.value_ptr.* });
797const FormatTrackingData = struct {
798 self: *Self,
799};
800fn formatTracking(
801 data: FormatTrackingData,
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 } };
760811}
761812
762813fn 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 {
764815 try self.mir_instructions.ensureUnusedCapacity(gpa, 1);
765816 const result_index = @intCast(Mir.Inst.Index, self.mir_instructions.len);
766817 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 }
768826 return result_index;
769827}
770828
......@@ -1186,13 +1244,8 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
11861244 }
11871245
11881246 if (self.liveness.isUnused(inst) and !self.air.mustLower(inst)) continue;
1189 if (debug_wip_mir) @import("../../print_air.zig").dumpInst(
1190 inst,
1191 self.bin_file.options.module.?,
1192 self.air,
1193 self.liveness,
1194 );
1195 self.dumpTracking() catch {};
1247 wip_mir_log.debug("{}", .{self.fmtAir(inst)});
1248 verbose_tracking_log.debug("{}", .{self.fmtTracking()});
11961249
11971250 const old_air_bookkeeping = self.air_bookkeeping;
11981251 try self.inst_tracking.ensureUnusedCapacity(self.gpa, 1);
......@@ -1453,7 +1506,7 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
14531506 }
14541507 }
14551508 }
1456 self.dumpTracking() catch {};
1509 verbose_tracking_log.debug("{}", .{self.fmtTracking()});
14571510}
14581511
14591512fn getValue(self: *Self, value: MCValue, inst: ?Air.Inst.Index) void {
src/print_air.zig+9-3
......@@ -94,14 +94,20 @@ const Writer = struct {
9494 for (w.air.instructions.items(.tag), 0..) |tag, i| {
9595 const inst = @intCast(Air.Inst.Index, i);
9696 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 },
98101 else => continue,
99102 }
100103 }
101104 }
102105
103106 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 }
105111 }
106112
107113 fn writeInst(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
......@@ -336,7 +342,7 @@ const Writer = struct {
336342 .work_group_id,
337343 => try w.writeWorkDimension(s, inst),
338344 }
339 try s.writeAll(")\n");
345 try s.writeByte(')');
340346 }
341347
342348 fn writeBinOp(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {