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 {...@@ -1103,11 +1103,7 @@ const FormatAirData = struct {
1103 inst: Air.Inst.Index,1103 inst: Air.Inst.Index,
1104};1104};
1105fn formatAir(data: FormatAirData, w: *std.io.Writer) Writer.Error!void {1105fn formatAir(data: FormatAirData, w: *std.io.Writer) Writer.Error!void {
1106 // not acceptable implementation because it ignores `w`:1106 data.self.air.writeInst(w, data.inst, data.self.pt, data.self.liveness);
1107 //data.self.air.dumpInst(data.inst, data.self.pt, data.self.liveness);
1108 _ = data;
1109 _ = w;
1110 @panic("TODO: unimplemented");
1111}1107}
1112fn fmtAir(self: *CodeGen, inst: Air.Inst.Index) std.fmt.Formatter(FormatAirData, formatAir) {1108fn fmtAir(self: *CodeGen, inst: Air.Inst.Index) std.fmt.Formatter(FormatAirData, formatAir) {
1113 return .{ .data = .{ .self = self, .inst = inst } };1109 return .{ .data = .{ .self = self, .inst = inst } };
...@@ -179300,10 +179296,13 @@ fn lowerSwitchBr(...@@ -179300,10 +179296,13 @@ fn lowerSwitchBr(
179300 } else undefined;179296 } else undefined;
179301 const table_start: u31 = @intCast(cg.mir_table.items.len);179297 const table_start: u31 = @intCast(cg.mir_table.items.len);
179302 {179298 {
179303 const condition_index_reg = if (condition_index.isRegister())179299 const condition_index_reg = condition_index_reg: {
179304 condition_index.getReg().?179300 if (condition_index.isRegister()) {
179305 else179301 const condition_index_reg = condition_index.getReg().?;
179306 try cg.copyToTmpRegister(.usize, condition_index);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 };
179307 const condition_index_lock = cg.register_manager.lockReg(condition_index_reg);179306 const condition_index_lock = cg.register_manager.lockReg(condition_index_reg);
179308 defer if (condition_index_lock) |lock| cg.register_manager.unlockReg(lock);179307 defer if (condition_index_lock) |lock| cg.register_manager.unlockReg(lock);
179309 try cg.truncateRegister(condition_ty, condition_index_reg);179308 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" {...@@ -1072,3 +1072,13 @@ test "switch on a signed value smaller than the smallest prong value" {
1072 else => {},1072 else => {},
1073 }1073 }
1074}1074}
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}