aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMathias Magnusson <mathias@magnusson.space>2025-06-02 21:12:52 +0200
committerMathias Magnusson <mathias@magnusson.space>2025-06-02 21:13:08 +0200
commit5cf5cb8fbf3c53d28c40cb682d5a3bf3db583922 (patch)
tree23300c6e34743b0aaab20763110179076bc44fd2
parent6438ec4624d5cbf4263d5a4d9312b8f067a7c5a5 (diff)
downloadhuginn-5cf5cb8fbf3c53d28c40cb682d5a3bf3db583922.tar.gz
stop freeing vregs twice when they are used twice in an instr
-rw-r--r--src/compile.zig7
-rw-r--r--src/main.zig2
2 files changed, 5 insertions, 4 deletions
diff --git a/src/compile.zig b/src/compile.zig
index c352994..acae7ff 100644
--- a/src/compile.zig
+++ b/src/compile.zig
@@ -84,10 +84,11 @@ pub const Block = struct {
fn init(allocator: Allocator, instrs: []Instr) !Block {
var vreg_last_use: std.AutoHashMap(usize, std.ArrayList(VReg)) = .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| {
- const kv = try vreg_last_use.getOrPut(i);
- if (!kv.found_existing) kv.value_ptr.* = .init(allocator);
- try kv.value_ptr.append(src);
+ if (std.mem.indexOfScalar(VReg, kv.value_ptr.items, src) == null)
+ try kv.value_ptr.append(src);
}
}
return .{
diff --git a/src/main.zig b/src/main.zig
index 918c52d..7e0794a 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -39,7 +39,7 @@ pub fn main() !void {
const source =
\\let x = 1;
\\print(18446744073709551615);
- \\print(print(0 - x));
+ \\print(x + x);
;
var lexer: Lexer = .{ .source = source };
std.debug.print("Tokens:\n", .{});