authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-03-27 05:03:28-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-03-27 05:58:00-04:00
logfd13e44e0c69465cf9ea9ef4fd17dfebf5cffd45
tree518b954519c6b26aab9aea84f10fa49bfec82fac
parent802c2e4fae4450679c995767d6409dbc9cf568b3

x86_64: cleanup debug mir dumping


1 files changed, 44 insertions(+), 58 deletions(-)

src/arch/x86_64/CodeGen.zig+44-58
...@@ -8,7 +8,6 @@ const link = @import("../../link.zig");...@@ -8,7 +8,6 @@ const link = @import("../../link.zig");
8const log = std.log.scoped(.codegen);8const log = std.log.scoped(.codegen);
9const math = std.math;9const math = std.math;
10const mem = std.mem;10const mem = std.mem;
11const print_air = @import("../../print_air.zig");
12const trace = @import("../../tracy.zig").trace;11const trace = @import("../../tracy.zig").trace;
1312
14const Air = @import("../../Air.zig");13const Air = @import("../../Air.zig");
...@@ -107,12 +106,8 @@ air_bookkeeping: @TypeOf(air_bookkeeping_init) = air_bookkeeping_init,...@@ -107,12 +106,8 @@ air_bookkeeping: @TypeOf(air_bookkeeping_init) = air_bookkeeping_init,
107/// For mir debug info, maps a mir index to a air index106/// For mir debug info, maps a mir index to a air index
108mir_to_air_map: if (builtin.mode == .Debug) std.AutoHashMap(Mir.Inst.Index, Air.Inst.Index) else void,107mir_to_air_map: if (builtin.mode == .Debug) std.AutoHashMap(Mir.Inst.Index, Air.Inst.Index) else void,
109108
110debug_wip_mir_inst: @TypeOf(debug_wip_mir_inst_init) = debug_wip_mir_inst_init,
111
112const air_bookkeeping_init = if (std.debug.runtime_safety) @as(usize, 0) else {};109const air_bookkeeping_init = if (std.debug.runtime_safety) @as(usize, 0) else {};
113110
114const debug_wip_mir_inst_init = if (debug_wip_mir) @as(Mir.Inst.Index, 0) else {};
115
116pub const MCValue = union(enum) {111pub const MCValue = union(enum) {
117 /// No runtime bits. `void` types, empty structs, u0, enums with 1 tag, etc.112 /// No runtime bits. `void` types, empty structs, u0, enums with 1 tag, etc.
118 /// TODO Look into deleting this tag and using `dead` instead, since every use113 /// TODO Look into deleting this tag and using `dead` instead, since every use
...@@ -395,11 +390,49 @@ pub fn generate(...@@ -395,11 +390,49 @@ pub fn generate(
395 }390 }
396}391}
397392
393fn dumpWipMir(self: *Self, inst: Mir.Inst) !void {
394 if (!debug_wip_mir) return;
395 const stderr = std.io.getStdErr().writer();
396
397 var lower = Lower{
398 .allocator = self.gpa,
399 .mir = .{
400 .instructions = self.mir_instructions.slice(),
401 .extra = self.mir_extra.items,
402 },
403 .target = self.target,
404 .src_loc = self.src_loc,
405 };
406 for (lower.lowerMir(inst) catch |err| switch (err) {
407 error.LowerFail => {
408 defer {
409 lower.err_msg.?.deinit(self.gpa);
410 lower.err_msg = null;
411 }
412 try stderr.print("{s}\n", .{lower.err_msg.?.msg});
413 return;
414 },
415 error.InvalidInstruction, error.CannotEncode => |e| {
416 try stderr.writeAll(switch (e) {
417 error.InvalidInstruction => "CodeGen failed to find a viable instruction.\n",
418 error.CannotEncode => "CodeGen failed to encode the instruction.\n",
419 });
420 return;
421 },
422 else => |e| return e,
423 }) |lower_inst| {
424 try stderr.writeAll(" | ");
425 try lower_inst.fmtPrint(stderr);
426 try stderr.writeByte('\n');
427 }
428}
429
398fn addInst(self: *Self, inst: Mir.Inst) error{OutOfMemory}!Mir.Inst.Index {430fn addInst(self: *Self, inst: Mir.Inst) error{OutOfMemory}!Mir.Inst.Index {
399 const gpa = self.gpa;431 const gpa = self.gpa;
400 try self.mir_instructions.ensureUnusedCapacity(gpa, 1);432 try self.mir_instructions.ensureUnusedCapacity(gpa, 1);
401 const result_index = @intCast(Mir.Inst.Index, self.mir_instructions.len);433 const result_index = @intCast(Mir.Inst.Index, self.mir_instructions.len);
402 self.mir_instructions.appendAssumeCapacity(inst);434 self.mir_instructions.appendAssumeCapacity(inst);
435 self.dumpWipMir(inst) catch {};
403 return result_index;436 return result_index;
404}437}
405438
...@@ -697,56 +730,6 @@ fn asmMemoryRegisterImmediate(...@@ -697,56 +730,6 @@ fn asmMemoryRegisterImmediate(
697 });730 });
698}731}
699732
700fn printWipMir(self: *Self, stream: anytype, air_inst: ?Air.Inst.Index) !void {
701 const mod = self.bin_file.options.module.?;
702 if (!debug_wip_mir) return;
703
704 var lower = Lower{
705 .allocator = self.gpa,
706 .mir = .{
707 .instructions = self.mir_instructions.slice(),
708 .extra = self.mir_extra.items,
709 },
710 .target = self.target,
711 .src_loc = self.src_loc,
712 };
713 var mir_inst = self.debug_wip_mir_inst;
714 var mir_end = @intCast(Mir.Inst.Index, self.mir_instructions.len);
715 defer self.debug_wip_mir_inst = mir_end;
716 while (mir_inst < mir_end) : (mir_inst += 1) {
717 for (lower.lowerMir(lower.mir.instructions.get(mir_inst)) catch |err| switch (err) {
718 error.LowerFail => {
719 defer {
720 lower.err_msg.?.deinit(self.gpa);
721 lower.err_msg = null;
722 }
723 try stream.print("{s}\n", .{lower.err_msg.?.msg});
724 continue;
725 },
726 error.InvalidInstruction, error.CannotEncode => |e| {
727 try stream.writeAll(switch (e) {
728 error.InvalidInstruction => "CodeGen failed to find a viable instruction.\n",
729 error.CannotEncode => "CodeGen failed to encode the instruction.\n",
730 });
731 continue;
732 },
733 else => |e| return e,
734 }) |lower_inst| {
735 try stream.writeAll(" | ");
736 try lower_inst.fmtPrint(stream);
737 try stream.writeByte('\n');
738 }
739 }
740
741 if (air_inst) |inst| {
742 print_air.writeInst(stream, inst, mod, self.air, self.liveness);
743 }
744}
745
746fn dumpWipMir(self: *Self, air_inst: ?Air.Inst.Index) void {
747 self.printWipMir(std.io.getStdErr().writer(), air_inst) catch return;
748}
749
750fn gen(self: *Self) InnerError!void {733fn gen(self: *Self) InnerError!void {
751 const cc = self.fn_type.fnCallingConvention();734 const cc = self.fn_type.fnCallingConvention();
752 if (cc != .Naked) {735 if (cc != .Naked) {
...@@ -900,8 +883,6 @@ fn gen(self: *Self) InnerError!void {...@@ -900,8 +883,6 @@ fn gen(self: *Self) InnerError!void {
900 .ops = undefined,883 .ops = undefined,
901 .data = .{ .payload = payload },884 .data = .{ .payload = payload },
902 });885 });
903
904 self.dumpWipMir(null);
905}886}
906887
907fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {888fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
...@@ -913,7 +894,12 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {...@@ -913,7 +894,12 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
913 if (builtin.mode == .Debug) {894 if (builtin.mode == .Debug) {
914 try self.mir_to_air_map.put(@intCast(Mir.Inst.Index, self.mir_instructions.len), inst);895 try self.mir_to_air_map.put(@intCast(Mir.Inst.Index, self.mir_instructions.len), inst);
915 }896 }
916 self.dumpWipMir(inst);897 if (debug_wip_mir) @import("../../print_air.zig").dumpInst(
898 inst,
899 self.bin_file.options.module.?,
900 self.air,
901 self.liveness,
902 );
917903
918 switch (air_tags[inst]) {904 switch (air_tags[inst]) {
919 // zig fmt: off905 // zig fmt: off