From e2e77b4b06e51c7f7d3ea187defaf1ad08e513c1 Mon Sep 17 00:00:00 2001 From: Mathias Magnusson Date: Mon, 2 Jun 2025 21:55:11 +0200 Subject: remove the need for explicit discard instructions by also considering an instruction's destination register(s) to be uses and when cleaning up registers, also cleaning up those from previous instructions (specifically this is the output of the last instruction if it is not used anywhere) --- src/compile.zig | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) (limited to 'src/compile.zig') diff --git a/src/compile.zig b/src/compile.zig index c837495..ac7768e 100644 --- a/src/compile.zig +++ b/src/compile.zig @@ -15,7 +15,6 @@ pub const Instr = struct { constant: Constant, bin_op: BinOp, proc_call: ProcCall, - discard: Discard, }; pub const Constant = struct { @@ -58,14 +57,6 @@ pub const Instr = struct { } }; - pub const Discard = struct { - vreg: VReg, - - pub fn sources(self: Discard) Sources { - return Sources.fromSlice(&.{self.vreg}) catch unreachable; - } - }; - pub fn sources(self: Instr) Sources { return switch (self.type) { inline else => |instr| instr.sources(), @@ -75,7 +66,6 @@ pub const Instr = struct { pub fn dest(self: *const Instr) ?VReg { return switch (self.type) { inline .constant, .bin_op, .proc_call => |s| s.dest, - // inline .x => null, }; } @@ -85,17 +75,15 @@ pub const Instr = struct { pub const Block = struct { // arguments: []Reg, instrs: []Instr, - vreg_last_use: std.AutoHashMap(usize, std.ArrayList(VReg)), + vreg_last_use: std.AutoHashMap(VReg, usize), fn init(allocator: Allocator, instrs: []Instr) !Block { - var vreg_last_use: std.AutoHashMap(usize, std.ArrayList(VReg)) = .init(allocator); + var vreg_last_use: std.AutoHashMap(VReg, usize) = .init(allocator); for (0.., instrs) |i, instr| { - const kv = try vreg_last_use.getOrPut(i); - if (!kv.found_existing) kv.value_ptr.* = .init(allocator); - for (instr.sources().slice()) |src| { - if (std.mem.indexOfScalar(VReg, kv.value_ptr.items, src) == null) - try kv.value_ptr.append(src); - } + for (instr.sources().slice()) |src| + try vreg_last_use.put(src, i); + if (instr.dest()) |dest| + try vreg_last_use.put(dest, i); } return .{ .instrs = instrs, @@ -134,10 +122,7 @@ const CompileContext = struct { fn compileStmt(self: *Self, stmt: parse.Stmt) !void { switch (stmt.type) { - .expr => |expr| try self.addInstr(.{ - .loc = stmt.loc, - .type = .{ .discard = .{ .vreg = try self.compileExpr(expr) } }, - }), + .expr => |expr| _ = try self.compileExpr(expr), .declare_var => |declare_var| { const val = try self.compileExpr(declare_var.value); const name = declare_var.ident.getIdent(self.source); -- cgit v1.2.3