authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-04-18 06:24:16+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-04-18 06:24:16+02:00
log8ba937c787efd690a8a984f9ab42cac00fb9b2bf
tree4a84c3da1cac4640565ae1985f8f07fb1431876e
parent40e1fca34b2c0d2cde130fd17331a2935c473644
parent13503b7cba2c826633017e34e8bda2bf5072e7f6
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

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

Misc cleanups

3 files changed, 65 insertions(+), 19 deletions(-)

src/arch/x86_64/CodeGen.zig+1-3
...@@ -439,9 +439,7 @@ fn dumpWipMir(self: *Self, inst: Mir.Inst) !void {...@@ -439,9 +439,7 @@ fn dumpWipMir(self: *Self, inst: Mir.Inst) !void {
439 },439 },
440 else => |e| return e,440 else => |e| return e,
441 }) |lower_inst| {441 }) |lower_inst| {
442 try stderr.writeAll(" | ");442 try stderr.print(" | {}\n", .{lower_inst});
443 try lower_inst.fmtPrint(stderr);
444 try stderr.writeByte('\n');
445 }443 }
446}444}
447445
src/arch/x86_64/encoder.zig+44-3
...@@ -54,7 +54,34 @@ pub const Instruction = struct {...@@ -54,7 +54,34 @@ pub const Instruction = struct {
54 };54 };
55 }55 }
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;
58 switch (op) {85 switch (op) {
59 .none => {},86 .none => {},
60 .reg => |reg| try writer.writeAll(@tagName(reg)),87 .reg => |reg| try writer.writeAll(@tagName(reg)),
...@@ -105,6 +132,13 @@ pub const Instruction = struct {...@@ -105,6 +132,13 @@ pub const Instruction = struct {
105 .imm => |imm| try writer.print("0x{x}", .{imm.asUnsigned(enc_op.bitSize())}),132 .imm => |imm| try writer.print("0x{x}", .{imm.asUnsigned(enc_op.bitSize())}),
106 }133 }
107 }134 }
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 }
108 };142 };
109143
110 pub fn new(prefix: Prefix, mnemonic: Mnemonic, ops: []const Operand) !Instruction {144 pub fn new(prefix: Prefix, mnemonic: Mnemonic, ops: []const Operand) !Instruction {
...@@ -130,14 +164,21 @@ pub const Instruction = struct {...@@ -130,14 +164,21 @@ pub const Instruction = struct {
130 return inst;164 return inst;
131 }165 }
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;
134 if (inst.prefix != .none) try writer.print("{s} ", .{@tagName(inst.prefix)});175 if (inst.prefix != .none) try writer.print("{s} ", .{@tagName(inst.prefix)});
135 try writer.print("{s}", .{@tagName(inst.encoding.mnemonic)});176 try writer.print("{s}", .{@tagName(inst.encoding.mnemonic)});
136 for (inst.ops, inst.encoding.data.ops, 0..) |op, enc, i| {177 for (inst.ops, inst.encoding.data.ops, 0..) |op, enc, i| {
137 if (op == .none) break;178 if (op == .none) break;
138 if (i > 0) try writer.writeByte(',');179 if (i > 0) try writer.writeByte(',');
139 try writer.writeByte(' ');180 try writer.writeByte(' ');
140 try op.fmtPrint(enc, writer);181 try writer.print("{}", .{op.fmtPrint(enc)});
141 }182 }
142 }183 }
143184
src/link/tapi/parse.zig+20-13
...@@ -40,10 +40,26 @@ pub const Node = struct {...@@ -40,10 +40,26 @@ pub const Node = struct {
4040
41 pub fn deinit(self: *Node, allocator: Allocator) void {41 pub fn deinit(self: *Node, allocator: Allocator) void {
42 switch (self.tag) {42 switch (self.tag) {
43 .doc => @fieldParentPtr(Node.Doc, "base", self).deinit(allocator),43 .doc => {
44 .map => @fieldParentPtr(Node.Map, "base", self).deinit(allocator),44 const parent = @fieldParentPtr(Node.Doc, "base", self);
45 .list => @fieldParentPtr(Node.List, "base", self).deinit(allocator),45 parent.deinit(allocator);
46 .value => @fieldParentPtr(Node.Value, "base", self).deinit(allocator),46 allocator.destroy(parent);
47 },
48 .map => {
49 const parent = @fieldParentPtr(Node.Map, "base", self);
50 parent.deinit(allocator);
51 allocator.destroy(parent);
52 },
53 .list => {
54 const parent = @fieldParentPtr(Node.List, "base", self);
55 parent.deinit(allocator);
56 allocator.destroy(parent);
57 },
58 .value => {
59 const parent = @fieldParentPtr(Node.Value, "base", self);
60 parent.deinit(allocator);
61 allocator.destroy(parent);
62 },
47 }63 }
48 }64 }
4965
...@@ -76,7 +92,6 @@ pub const Node = struct {...@@ -76,7 +92,6 @@ pub const Node = struct {
76 pub fn deinit(self: *Doc, allocator: Allocator) void {92 pub fn deinit(self: *Doc, allocator: Allocator) void {
77 if (self.value) |node| {93 if (self.value) |node| {
78 node.deinit(allocator);94 node.deinit(allocator);
79 allocator.destroy(node);
80 }95 }
81 }96 }
8297
...@@ -122,7 +137,6 @@ pub const Node = struct {...@@ -122,7 +137,6 @@ pub const Node = struct {
122 for (self.values.items) |entry| {137 for (self.values.items) |entry| {
123 if (entry.value) |value| {138 if (entry.value) |value| {
124 value.deinit(allocator);139 value.deinit(allocator);
125 allocator.destroy(value);
126 }140 }
127 }141 }
128 self.values.deinit(allocator);142 self.values.deinit(allocator);
...@@ -163,7 +177,6 @@ pub const Node = struct {...@@ -163,7 +177,6 @@ pub const Node = struct {
163 pub fn deinit(self: *List, allocator: Allocator) void {177 pub fn deinit(self: *List, allocator: Allocator) void {
164 for (self.values.items) |node| {178 for (self.values.items) |node| {
165 node.deinit(allocator);179 node.deinit(allocator);
166 allocator.destroy(node);
167 }180 }
168 self.values.deinit(allocator);181 self.values.deinit(allocator);
169 }182 }
...@@ -239,7 +252,6 @@ pub const Tree = struct {...@@ -239,7 +252,6 @@ pub const Tree = struct {
239 self.line_cols.deinit();252 self.line_cols.deinit();
240 for (self.docs.items) |doc| {253 for (self.docs.items) |doc| {
241 doc.deinit(self.allocator);254 doc.deinit(self.allocator);
242 self.allocator.destroy(doc);
243 }255 }
244 self.docs.deinit(self.allocator);256 self.docs.deinit(self.allocator);
245 }257 }
...@@ -386,7 +398,6 @@ const Parser = struct {...@@ -386,7 +398,6 @@ const Parser = struct {
386 }398 }
387 errdefer if (node.value) |val| {399 errdefer if (node.value) |val| {
388 val.deinit(self.allocator);400 val.deinit(self.allocator);
389 self.allocator.destroy(val);
390 };401 };
391402
392 // Parse footer403 // Parse footer
...@@ -426,7 +437,6 @@ const Parser = struct {...@@ -426,7 +437,6 @@ const Parser = struct {
426 for (node.values.items) |entry| {437 for (node.values.items) |entry| {
427 if (entry.value) |val| {438 if (entry.value) |val| {
428 val.deinit(self.allocator);439 val.deinit(self.allocator);
429 self.allocator.destroy(val);
430 }440 }
431 }441 }
432 node.values.deinit(self.allocator);442 node.values.deinit(self.allocator);
...@@ -467,7 +477,6 @@ const Parser = struct {...@@ -467,7 +477,6 @@ const Parser = struct {
467 const val = try self.value();477 const val = try self.value();
468 errdefer if (val) |v| {478 errdefer if (val) |v| {
469 v.deinit(self.allocator);479 v.deinit(self.allocator);
470 self.allocator.destroy(v);
471 };480 };
472481
473 if (val) |v| {482 if (val) |v| {
...@@ -503,7 +512,6 @@ const Parser = struct {...@@ -503,7 +512,6 @@ const Parser = struct {
503 errdefer {512 errdefer {
504 for (node.values.items) |val| {513 for (node.values.items) |val| {
505 val.deinit(self.allocator);514 val.deinit(self.allocator);
506 self.allocator.destroy(val);
507 }515 }
508 node.values.deinit(self.allocator);516 node.values.deinit(self.allocator);
509 }517 }
...@@ -535,7 +543,6 @@ const Parser = struct {...@@ -535,7 +543,6 @@ const Parser = struct {
535 errdefer {543 errdefer {
536 for (node.values.items) |val| {544 for (node.values.items) |val| {
537 val.deinit(self.allocator);545 val.deinit(self.allocator);
538 self.allocator.destroy(val);
539 }546 }
540 node.values.deinit(self.allocator);547 node.values.deinit(self.allocator);
541 }548 }