Skip to content

Commit 68cfa73

Browse files
committed
x86_64: fix switch on mod result
Closes #24541
1 parent fc4b7c9 commit 68cfa73

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/arch/x86_64/CodeGen.zig

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,11 +1103,7 @@ const FormatAirData = struct {
11031103
inst: Air.Inst.Index,
11041104
};
11051105
fn 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
}
11121108
fn 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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,3 +1072,13 @@ test "switch on a signed value smaller than the smallest prong value" {
10721072
else => {},
10731073
}
10741074
}
1075+
1076+
test "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+
}

0 commit comments

Comments
 (0)