authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2025-07-26 06:23:31-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2025-07-26 06:24:03-04:00
log68cfa736dfd38cc151af1f9e1b0edb3041bc237c
tree8ad7d54f9e83d9139eec47e52a09a9412a7b9718
parentfc4b7c968afa6fa0780a011f3c8cfeaea38b7b98

x86_64: fix switch on mod result

Closes #24541

2 files changed, 18 insertions(+), 9 deletions(-)

src/arch/x86_64/CodeGen.zig+8-9
......@@ -1103,11 +1103,7 @@ const FormatAirData = struct {
11031103 inst: Air.Inst.Index,
11041104};
11051105fn formatAir(data: FormatAirData, w: *std.io.Writer) Writer.Error!void {
1106 // not acceptable implementation because it ignores `w`:
1107 //data.self.air.dumpInst(data.inst, data.self.pt, data.self.liveness);
1108 _ = data;
1109 _ = w;
1110 @panic("TODO: unimplemented");
1106 data.self.air.writeInst(w, data.inst, data.self.pt, data.self.liveness);
11111107}
11121108fn fmtAir(self: *CodeGen, inst: Air.Inst.Index) std.fmt.Formatter(FormatAirData, formatAir) {
11131109 return .{ .data = .{ .self = self, .inst = inst } };
......@@ -179300,10 +179296,13 @@ fn lowerSwitchBr(
179300179296 } else undefined;
179301179297 const table_start: u31 = @intCast(cg.mir_table.items.len);
179302179298 {
179303 const condition_index_reg = if (condition_index.isRegister())
179304 condition_index.getReg().?
179305 else
179306 try cg.copyToTmpRegister(.usize, condition_index);
179299 const condition_index_reg = condition_index_reg: {
179300 if (condition_index.isRegister()) {
179301 const condition_index_reg = condition_index.getReg().?;
179302 if (condition_index_reg.isClass(.general_purpose)) break :condition_index_reg condition_index_reg;
179303 }
179304 break :condition_index_reg try cg.copyToTmpRegister(.usize, condition_index);
179305 };
179307179306 const condition_index_lock = cg.register_manager.lockReg(condition_index_reg);
179308179307 defer if (condition_index_lock) |lock| cg.register_manager.unlockReg(lock);
179309179308 try cg.truncateRegister(condition_ty, condition_index_reg);
test/behavior/switch.zig+10
......@@ -1072,3 +1072,13 @@ test "switch on a signed value smaller than the smallest prong value" {
10721072 else => {},
10731073 }
10741074}
1075
1076test "switch on 8-bit mod result" {
1077 var x: u8 = undefined;
1078 x = 16;
1079 switch (x % 4) {
1080 0 => {},
1081 1, 2, 3 => return error.TestFailed,
1082 else => unreachable,
1083 }
1084}