Commit dde5f15b49

Jacob G-W <jacoblevgw@gmail.com>
2021-11-27 16:48:11
interleave Air instructions and tags in printing Mir instructions
1 parent bd19f5e
Changed files (2)
src
src/arch/x86_64/CodeGen.zig
@@ -86,6 +86,9 @@ next_stack_offset: u32 = 0,
 /// Debug field, used to find bugs in the compiler.
 air_bookkeeping: @TypeOf(air_bookkeeping_init) = air_bookkeeping_init,
 
+/// For mir debug info, maps a mir index to a air index
+mir_to_air_map: if (builtin.mode == .Debug) std.AutoHashMap(Mir.Inst.Index, Air.Inst.Index) else void,
+
 const air_bookkeeping_init = if (std.debug.runtime_safety) @as(usize, 0) else {};
 
 pub const MCValue = union(enum) {
@@ -272,12 +275,14 @@ pub fn generate(
         .stack_align = undefined,
         .end_di_line = module_fn.rbrace_line,
         .end_di_column = module_fn.rbrace_column,
+        .mir_to_air_map = if (builtin.mode == .Debug) std.AutoHashMap(Mir.Inst.Index, Air.Inst.Index).init(bin_file.allocator) else {},
     };
     defer function.stack.deinit(bin_file.allocator);
     defer function.blocks.deinit(bin_file.allocator);
     defer function.exitlude_jump_relocs.deinit(bin_file.allocator);
     defer function.mir_instructions.deinit(bin_file.allocator);
     defer function.mir_extra.deinit(bin_file.allocator);
+    defer if (builtin.mode == .Debug) function.mir_to_air_map.deinit();
 
     var call_info = function.resolveCallingConventionValues(fn_type) catch |err| switch (err) {
         error.CodegenFail => return FnResult{ .fail = function.err_msg.? },
@@ -323,8 +328,8 @@ pub fn generate(
         const w = std.io.getStdErr().writer();
         w.print("# Begin Function MIR: {s}:\n", .{module_fn.owner_decl.name}) catch {};
         const print = @import("./PrintMir.zig"){ .mir = mir };
-        print.printMir(w) catch {}; // we don't care if the debug printing fails
-        w.print("# End Function MIR: {s}:\n\n", .{module_fn.owner_decl.name}) catch {};
+        print.printMir(w, function.mir_to_air_map, air) catch {}; // we don't care if the debug printing fails
+        w.print("# End Function MIR: {s}\n\n", .{module_fn.owner_decl.name}) catch {};
     }
 
     if (function.err_msg) |em| {
@@ -525,6 +530,9 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
     for (body) |inst| {
         const old_air_bookkeeping = self.air_bookkeeping;
         try self.ensureProcessDeathCapacity(Liveness.bpi);
+        if (builtin.mode == .Debug) {
+            try self.mir_to_air_map.put(@intCast(u32, self.mir_instructions.len), inst);
+        }
 
         switch (air_tags[inst]) {
             // zig fmt: off
src/arch/x86_64/PrintMir.zig
@@ -26,7 +26,7 @@ const fmtIntSizeBin = std.fmt.fmtIntSizeBin;
 
 mir: Mir,
 
-pub fn printMir(print: *const Print, w: anytype) !void {
+pub fn printMir(print: *const Print, w: anytype, mir_to_air_map: std.AutoHashMap(Mir.Inst.Index, Air.Inst.Index), air: Air) !void {
     const instruction_bytes = print.mir.instructions.len *
         // Here we don't use @sizeOf(Mir.Inst.Data) because it would include
         // the debug safety tag but we want to measure release size.
@@ -49,8 +49,11 @@ pub fn printMir(print: *const Print, w: anytype) !void {
     const mir_tags = print.mir.instructions.items(.tag);
 
     for (mir_tags) |tag, index| {
-        try w.writeAll("  ");
         const inst = @intCast(u32, index);
+        if (mir_to_air_map.get(inst)) |air_index| {
+            try w.print("air index %{} ({}) for following mir inst(s)\n", .{ air_index, air.instructions.items(.tag)[air_index] });
+        }
+        try w.writeAll("  ");
         switch (tag) {
             .adc => try print.mirArith(.adc, inst, w),
             .add => try print.mirArith(.add, inst, w),