| ... | ... | @@ -8,7 +8,6 @@ const link = @import("../../link.zig"); |
| 8 | 8 | const log = std.log.scoped(.codegen); |
| 9 | 9 | const math = std.math; |
| 10 | 10 | const mem = std.mem; |
| 11 | | const print_air = @import("../../print_air.zig"); |
| 12 | 11 | const trace = @import("../../tracy.zig").trace; |
| 13 | 12 | |
| 14 | 13 | const Air = @import("../../Air.zig"); |
| ... | ... | @@ -107,12 +106,8 @@ air_bookkeeping: @TypeOf(air_bookkeeping_init) = air_bookkeeping_init, |
| 107 | 106 | /// For mir debug info, maps a mir index to a air index |
| 108 | 107 | mir_to_air_map: if (builtin.mode == .Debug) std.AutoHashMap(Mir.Inst.Index, Air.Inst.Index) else void, |
| 109 | 108 | |
| 110 | | debug_wip_mir_inst: @TypeOf(debug_wip_mir_inst_init) = debug_wip_mir_inst_init, |
| 111 | | |
| 112 | 109 | const air_bookkeeping_init = if (std.debug.runtime_safety) @as(usize, 0) else {}; |
| 113 | 110 | |
| 114 | | const debug_wip_mir_inst_init = if (debug_wip_mir) @as(Mir.Inst.Index, 0) else {}; |
| 115 | | |
| 116 | 111 | pub const MCValue = union(enum) { |
| 117 | 112 | /// No runtime bits. `void` types, empty structs, u0, enums with 1 tag, etc. |
| 118 | 113 | /// TODO Look into deleting this tag and using `dead` instead, since every use |
| ... | ... | @@ -395,11 +390,49 @@ pub fn generate( |
| 395 | 390 | } |
| 396 | 391 | } |
| 397 | 392 | |
| 393 | fn 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 | |
| 398 | 430 | fn addInst(self: *Self, inst: Mir.Inst) error{OutOfMemory}!Mir.Inst.Index { |
| 399 | 431 | const gpa = self.gpa; |
| 400 | 432 | try self.mir_instructions.ensureUnusedCapacity(gpa, 1); |
| 401 | 433 | const result_index = @intCast(Mir.Inst.Index, self.mir_instructions.len); |
| 402 | 434 | self.mir_instructions.appendAssumeCapacity(inst); |
| 435 | self.dumpWipMir(inst) catch {}; |
| 403 | 436 | return result_index; |
| 404 | 437 | } |
| 405 | 438 | |
| ... | ... | @@ -697,56 +730,6 @@ fn asmMemoryRegisterImmediate( |
| 697 | 730 | }); |
| 698 | 731 | } |
| 699 | 732 | |
| 700 | | fn 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 | | |
| 746 | | fn dumpWipMir(self: *Self, air_inst: ?Air.Inst.Index) void { |
| 747 | | self.printWipMir(std.io.getStdErr().writer(), air_inst) catch return; |
| 748 | | } |
| 749 | | |
| 750 | 733 | fn gen(self: *Self) InnerError!void { |
| 751 | 734 | const cc = self.fn_type.fnCallingConvention(); |
| 752 | 735 | if (cc != .Naked) { |
| ... | ... | @@ -900,8 +883,6 @@ fn gen(self: *Self) InnerError!void { |
| 900 | 883 | .ops = undefined, |
| 901 | 884 | .data = .{ .payload = payload }, |
| 902 | 885 | }); |
| 903 | | |
| 904 | | self.dumpWipMir(null); |
| 905 | 886 | } |
| 906 | 887 | |
| 907 | 888 | 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 | 894 | if (builtin.mode == .Debug) { |
| 914 | 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 | ); |
| 917 | 903 | |
| 918 | 904 | switch (air_tags[inst]) { |
| 919 | 905 | // zig fmt: off |