aboutsummaryrefslogtreecommitdiff
path: root/src/arch/x86_64/encoder.zig
diff options
context:
space:
mode:
authorJakub Konka <kubkon@jakubkonka.com>2023-04-17 13:43:01 +0200
committerJakub Konka <kubkon@jakubkonka.com>2023-04-17 13:43:01 +0200
commit13503b7cba2c826633017e34e8bda2bf5072e7f6 (patch)
treedff34ef3b5a8b511537dd8f1dea6b527e73ef13a /src/arch/x86_64/encoder.zig
parent5bc4417d2a5155ceb10c73b5f56229c8f66539fc (diff)
downloadzig-13503b7cba2c826633017e34e8bda2bf5072e7f6.tar.gz
zig-13503b7cba2c826633017e34e8bda2bf5072e7f6.zip
x86_64: clean up formatting functions for Instruction and Operand
Diffstat (limited to 'src/arch/x86_64/encoder.zig')
-rw-r--r--src/arch/x86_64/encoder.zig47
1 files changed, 44 insertions, 3 deletions
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)});
}
}