Merge pull request #15339 from ziglang/link-and-x86_64-cleanups

Misc cleanups
This commit is contained in:
Jakub Konka 2023-04-18 06:24:16 +02:00 committed by GitHub
commit 8ba937c787
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 19 deletions

View File

@ -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});
}
}

View File

@ -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)});
}
}

View File

@ -40,10 +40,26 @@ pub const Node = struct {
pub fn deinit(self: *Node, allocator: Allocator) void {
switch (self.tag) {
.doc => @fieldParentPtr(Node.Doc, "base", self).deinit(allocator),
.map => @fieldParentPtr(Node.Map, "base", self).deinit(allocator),
.list => @fieldParentPtr(Node.List, "base", self).deinit(allocator),
.value => @fieldParentPtr(Node.Value, "base", self).deinit(allocator),
.doc => {
const parent = @fieldParentPtr(Node.Doc, "base", self);
parent.deinit(allocator);
allocator.destroy(parent);
},
.map => {
const parent = @fieldParentPtr(Node.Map, "base", self);
parent.deinit(allocator);
allocator.destroy(parent);
},
.list => {
const parent = @fieldParentPtr(Node.List, "base", self);
parent.deinit(allocator);
allocator.destroy(parent);
},
.value => {
const parent = @fieldParentPtr(Node.Value, "base", self);
parent.deinit(allocator);
allocator.destroy(parent);
},
}
}
@ -76,7 +92,6 @@ pub const Node = struct {
pub fn deinit(self: *Doc, allocator: Allocator) void {
if (self.value) |node| {
node.deinit(allocator);
allocator.destroy(node);
}
}
@ -122,7 +137,6 @@ pub const Node = struct {
for (self.values.items) |entry| {
if (entry.value) |value| {
value.deinit(allocator);
allocator.destroy(value);
}
}
self.values.deinit(allocator);
@ -163,7 +177,6 @@ pub const Node = struct {
pub fn deinit(self: *List, allocator: Allocator) void {
for (self.values.items) |node| {
node.deinit(allocator);
allocator.destroy(node);
}
self.values.deinit(allocator);
}
@ -239,7 +252,6 @@ pub const Tree = struct {
self.line_cols.deinit();
for (self.docs.items) |doc| {
doc.deinit(self.allocator);
self.allocator.destroy(doc);
}
self.docs.deinit(self.allocator);
}
@ -386,7 +398,6 @@ const Parser = struct {
}
errdefer if (node.value) |val| {
val.deinit(self.allocator);
self.allocator.destroy(val);
};
// Parse footer
@ -426,7 +437,6 @@ const Parser = struct {
for (node.values.items) |entry| {
if (entry.value) |val| {
val.deinit(self.allocator);
self.allocator.destroy(val);
}
}
node.values.deinit(self.allocator);
@ -467,7 +477,6 @@ const Parser = struct {
const val = try self.value();
errdefer if (val) |v| {
v.deinit(self.allocator);
self.allocator.destroy(v);
};
if (val) |v| {
@ -503,7 +512,6 @@ const Parser = struct {
errdefer {
for (node.values.items) |val| {
val.deinit(self.allocator);
self.allocator.destroy(val);
}
node.values.deinit(self.allocator);
}
@ -535,7 +543,6 @@ const Parser = struct {
errdefer {
for (node.values.items) |val| {
val.deinit(self.allocator);
self.allocator.destroy(val);
}
node.values.deinit(self.allocator);
}