diff options
| author | Jakub Konka <kubkon@jakubkonka.com> | 2023-04-17 13:43:01 +0200 |
|---|---|---|
| committer | Jakub Konka <kubkon@jakubkonka.com> | 2023-04-17 13:43:01 +0200 |
| commit | 13503b7cba2c826633017e34e8bda2bf5072e7f6 (patch) | |
| tree | dff34ef3b5a8b511537dd8f1dea6b527e73ef13a /src/arch | |
| parent | 5bc4417d2a5155ceb10c73b5f56229c8f66539fc (diff) | |
| download | zig-13503b7cba2c826633017e34e8bda2bf5072e7f6.tar.gz zig-13503b7cba2c826633017e34e8bda2bf5072e7f6.zip | |
x86_64: clean up formatting functions for Instruction and Operand
Diffstat (limited to 'src/arch')
| -rw-r--r-- | src/arch/x86_64/CodeGen.zig | 4 | ||||
| -rw-r--r-- | src/arch/x86_64/encoder.zig | 47 |
2 files changed, 45 insertions, 6 deletions
diff --git a/src/arch/x86_64/CodeGen.zig b/src/arch/x86_64/CodeGen.zig index 20c945e451..5cd05508e4 100644 --- a/src/arch/x86_64/CodeGen.zig +++ b/src/arch/x86_64/CodeGen.zig @@ -439,9 +439,7 @@ fn dumpWipMir(self: *Self, inst: Mir.Inst) !void { }, else => |e| return e, }) |lower_inst| { - try stderr.writeAll(" | "); - try lower_inst.fmtPrint(stderr); - try stderr.writeByte('\n'); + try stderr.print(" | {}\n", .{lower_inst}); } } diff --git a/src/arch/x86_64/encoder.zig b/src/arch/x86_64/encoder.zig index 663dad8727..578dba5f5d 100644 --- a/src/arch/x86_64/encoder.zig +++ b/src/arch/x86_64/encoder.zig @@ -54,7 +54,34 @@ pub const Instruction = struct { }; } - pub fn fmtPrint(op: Operand, enc_op: Encoding.Op, writer: anytype) @TypeOf(writer).Error!void { + fn format( + op: Operand, + comptime unused_format_string: []const u8, + options: std.fmt.FormatOptions, + writer: anytype, + ) !void { + _ = op; + _ = unused_format_string; + _ = options; + _ = writer; + @compileError("do not format Operand directly; use fmtPrint() instead"); + } + + const FormatContext = struct { + op: Operand, + enc_op: Encoding.Op, + }; + + fn fmt( + ctx: FormatContext, + comptime unused_format_string: []const u8, + options: std.fmt.FormatOptions, + writer: anytype, + ) @TypeOf(writer).Error!void { + _ = unused_format_string; + _ = options; + const op = ctx.op; + const enc_op = ctx.enc_op; switch (op) { .none => {}, .reg => |reg| try writer.writeAll(@tagName(reg)), @@ -105,6 +132,13 @@ pub const Instruction = struct { .imm => |imm| try writer.print("0x{x}", .{imm.asUnsigned(enc_op.bitSize())}), } } + + pub fn fmtPrint(op: Operand, enc_op: Encoding.Op) std.fmt.Formatter(fmt) { + return .{ .data = .{ + .op = op, + .enc_op = enc_op, + } }; + } }; pub fn new(prefix: Prefix, mnemonic: Mnemonic, ops: []const Operand) !Instruction { @@ -130,14 +164,21 @@ pub const Instruction = struct { return inst; } - pub fn fmtPrint(inst: Instruction, writer: anytype) @TypeOf(writer).Error!void { + pub fn format( + inst: Instruction, + comptime unused_format_string: []const u8, + options: std.fmt.FormatOptions, + writer: anytype, + ) @TypeOf(writer).Error!void { + _ = unused_format_string; + _ = options; if (inst.prefix != .none) try writer.print("{s} ", .{@tagName(inst.prefix)}); try writer.print("{s}", .{@tagName(inst.encoding.mnemonic)}); for (inst.ops, inst.encoding.data.ops, 0..) |op, enc, i| { if (op == .none) break; if (i > 0) try writer.writeByte(','); try writer.writeByte(' '); - try op.fmtPrint(enc, writer); + try writer.print("{}", .{op.fmtPrint(enc)}); } } |
