authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-04-17 13:43:01+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-04-17 13:43:01+02:00
log13503b7cba2c826633017e34e8bda2bf5072e7f6
treedff34ef3b5a8b511537dd8f1dea6b527e73ef13a
parent5bc4417d2a5155ceb10c73b5f56229c8f66539fc

x86_64: clean up formatting functions for Instruction and Operand


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

src/arch/x86_64/CodeGen.zig+1-3
......@@ -439,9 +439,7 @@ fn dumpWipMir(self: *Self, inst: Mir.Inst) !void {
439439 },
440440 else => |e| return e,
441441 }) |lower_inst| {
442 try stderr.writeAll(" | ");
443 try lower_inst.fmtPrint(stderr);
444 try stderr.writeByte('\n');
442 try stderr.print(" | {}\n", .{lower_inst});
445443 }
446444}
447445
src/arch/x86_64/encoder.zig+44-3
......@@ -54,7 +54,34 @@ pub const Instruction = struct {
5454 };
5555 }
5656
57 pub fn fmtPrint(op: Operand, enc_op: Encoding.Op, writer: anytype) @TypeOf(writer).Error!void {
57 fn format(
58 op: Operand,
59 comptime unused_format_string: []const u8,
60 options: std.fmt.FormatOptions,
61 writer: anytype,
62 ) !void {
63 _ = op;
64 _ = unused_format_string;
65 _ = options;
66 _ = writer;
67 @compileError("do not format Operand directly; use fmtPrint() instead");
68 }
69
70 const FormatContext = struct {
71 op: Operand,
72 enc_op: Encoding.Op,
73 };
74
75 fn fmt(
76 ctx: FormatContext,
77 comptime unused_format_string: []const u8,
78 options: std.fmt.FormatOptions,
79 writer: anytype,
80 ) @TypeOf(writer).Error!void {
81 _ = unused_format_string;
82 _ = options;
83 const op = ctx.op;
84 const enc_op = ctx.enc_op;
5885 switch (op) {
5986 .none => {},
6087 .reg => |reg| try writer.writeAll(@tagName(reg)),
......@@ -105,6 +132,13 @@ pub const Instruction = struct {
105132 .imm => |imm| try writer.print("0x{x}", .{imm.asUnsigned(enc_op.bitSize())}),
106133 }
107134 }
135
136 pub fn fmtPrint(op: Operand, enc_op: Encoding.Op) std.fmt.Formatter(fmt) {
137 return .{ .data = .{
138 .op = op,
139 .enc_op = enc_op,
140 } };
141 }
108142 };
109143
110144 pub fn new(prefix: Prefix, mnemonic: Mnemonic, ops: []const Operand) !Instruction {
......@@ -130,14 +164,21 @@ pub const Instruction = struct {
130164 return inst;
131165 }
132166
133 pub fn fmtPrint(inst: Instruction, writer: anytype) @TypeOf(writer).Error!void {
167 pub fn format(
168 inst: Instruction,
169 comptime unused_format_string: []const u8,
170 options: std.fmt.FormatOptions,
171 writer: anytype,
172 ) @TypeOf(writer).Error!void {
173 _ = unused_format_string;
174 _ = options;
134175 if (inst.prefix != .none) try writer.print("{s} ", .{@tagName(inst.prefix)});
135176 try writer.print("{s}", .{@tagName(inst.encoding.mnemonic)});
136177 for (inst.ops, inst.encoding.data.ops, 0..) |op, enc, i| {
137178 if (op == .none) break;
138179 if (i > 0) try writer.writeByte(',');
139180 try writer.writeByte(' ');
140 try op.fmtPrint(enc, writer);
181 try writer.print("{}", .{op.fmtPrint(enc)});
141182 }
142183 }
143184