From fed87e2006790fd3d0dbf41dc77ee4e4cc47fb16 Mon Sep 17 00:00:00 2001 From: Mathias Magnusson Date: Sun, 1 Jun 2025 00:54:42 +0200 Subject: codegen: make register allocation fail with error rather than returning null --- src/codegen.zig | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/codegen.zig b/src/codegen.zig index 368309c..0696d3a 100644 --- a/src/codegen.zig +++ b/src/codegen.zig @@ -398,7 +398,7 @@ const Instruction = packed union { imm20: u1, fn init(opcode: Opcode, rd: Register, imm: i21) Self { - std.debug.assert(imm % 2 == 0); + std.debug.assert(imm & 1 == 0); const umm: u21 = @bitCast(imm); return .{ .j = .{ .opcode = opcode, @@ -406,7 +406,7 @@ const Instruction = packed union { .imm12_19 = @truncate(umm >> 12), .imm11 = @truncate(umm >> 11), .imm1_10 = @truncate(umm >> 1), - .imm20 = umm >> 20, + .imm20 = @intCast(umm >> 20), } }; } }; @@ -445,8 +445,8 @@ const RegisterAllocator = struct { return self.allocated.get(vreg).?; } - fn allocate(self: *RegisterAllocator, vreg: compile.VReg) ?Register { - const reg = self.available.pop() orelse return null; + fn allocate(self: *RegisterAllocator, vreg: compile.VReg) !Register { + const reg = self.available.pop() orelse return error.OutOfRegisters; self.allocated.putAssumeCapacityNoClobber(vreg, reg); return reg; } @@ -511,7 +511,7 @@ const Context = struct { } fn genConstant(self: *Context, constant: compile.Instr.Constant) !void { - const reg = self.register_allocator.allocate(constant.dest) orelse return error.OutOfRegisters; + const reg = try self.register_allocator.allocate(constant.dest); try self.genConstantInner(reg, constant.value); } @@ -519,7 +519,7 @@ const Context = struct { const lhs = self.register_allocator.get(bin_op.lhs); const rhs = self.register_allocator.get(bin_op.rhs); try self.maybeFreeSources(bin_op.sources()); - const reg = self.register_allocator.allocate(bin_op.dest) orelse return error.OutOfRegisters; + const reg = try self.register_allocator.allocate(bin_op.dest); switch (bin_op.op) { .add => try self.emit(.add(reg, lhs, rhs)), } -- cgit v1.2.3