diff options
author | Mathias Magnusson <mathias@magnusson.space> | 2025-07-30 14:04:03 +0200 |
---|---|---|
committer | Mathias Magnusson <mathias@magnusson.space> | 2025-07-30 14:04:03 +0200 |
commit | 580405d656bc896f1e086b72f734a1e61d7c8f50 (patch) | |
tree | db6db88759e5e1dd151b43eace98b0ac8c2a7546 /src/compile.zig | |
parent | b368fe65bb45dc4414c9cc2dabc7d84fa7690c36 (diff) | |
download | huginn-580405d656bc896f1e086b72f734a1e61d7c8f50.tar.gz |
store parameter on the stack
Diffstat (limited to 'src/compile.zig')
-rw-r--r-- | src/compile.zig | 25 |
1 files changed, 10 insertions, 15 deletions
diff --git a/src/compile.zig b/src/compile.zig index 636cf01..ded8a45 100644 --- a/src/compile.zig +++ b/src/compile.zig @@ -55,25 +55,24 @@ pub const Module = struct { pub const Procedure = struct { name: []const u8, blocks: []BasicBlock, - param_reg: ?VReg, + param_lvar: ?LVar, locals: std.AutoHashMap(LVar, void), fn init( allocator: Allocator, name: []const u8, blocks: []BasicBlock, - param_reg: ?VReg, + param_lvar: ?LVar, locals: std.AutoHashMap(LVar, void), ) !Procedure { for (blocks) |*block| { try block.finalize(allocator); - if (param_reg) |p| _ = block.vreg_last_use.remove(p); } return .{ .name = name, .blocks = blocks, - .param_reg = param_reg, + .param_lvar = param_lvar, .locals = locals, }; } @@ -81,7 +80,7 @@ pub const Procedure = struct { pub fn format(self: Procedure, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { _ = .{ fmt, options }; try writer.print("{s}(", .{self.name}); - if (self.param_reg) |reg| try writer.print("%{}", .{@intFromEnum(reg)}); + if (self.param_lvar) |lvar| try writer.print("%{}", .{@intFromEnum(lvar)}); try writer.print("):\n", .{}); for (self.blocks) |block| { try writer.print("{}", .{block}); @@ -365,15 +364,15 @@ fn compileProcedure( .lvar_ctr = .init, .scope = .{ .locals = .empty, .parent = null }, .locals = .empty, - .param = .{ .name = "", .reg = undefined }, .blocks = try .init(ctx.allocator, &.{first_block}, &.{.{ .ref = first_block }}), .current_block = first_block, }; - const param_reg = if (proc.param) |param| blk: { - const reg = pctx.vreg_ctr.get(); + const param_lvar = if (proc.param) |param| blk: { const param_name = param.getIdent(ctx.source); - pctx.param = .{ .name = param_name, .reg = reg }; - break :blk reg; + const lvar = pctx.lvar_ctr.get(); + try pctx.scope.locals.putNoClobber(ctx.allocator, param_name, lvar); + try pctx.locals.putNoClobber(ctx.allocator, lvar, {}); + break :blk lvar; } else null; try pctx.compileBlock(proc.body); const proc_res = pctx.vreg_ctr.get(); @@ -396,7 +395,7 @@ fn compileProcedure( ctx.allocator, name.getIdent(ctx.source), blocks, - param_reg, + param_lvar, pctx.locals.promote(ctx.allocator), ); } @@ -409,7 +408,6 @@ const ProcedureContext = struct { scope: Scope, locals: std.AutoHashMapUnmanaged(LVar, void), - param: struct { name: []const u8, reg: VReg }, blocks: std.AutoArrayHashMapUnmanaged(BlockRef, BasicBlock), current_block: BlockRef, @@ -540,9 +538,6 @@ const ProcedureContext = struct { }, .identifier => { const ident = expr.loc.getIdent(self.ctx.source); - if (std.mem.eql(u8, ident, self.param.name)) { - return self.param.reg; - } var scope: ?*Scope = &self.scope; const local: LVar = blk: { while (scope) |s| : (scope = s.parent) { |