Commit 68cfa736df

Jacob Young <jacobly0@users.noreply.github.com>
2025-07-26 12:23:31
x86_64: fix switch on mod result
Closes #24541
1 parent fc4b7c9
Changed files (2)
src
arch
test
behavior
src/arch/x86_64/CodeGen.zig
@@ -1103,11 +1103,7 @@ const FormatAirData = struct {
     inst: Air.Inst.Index,
 };
 fn formatAir(data: FormatAirData, w: *std.io.Writer) Writer.Error!void {
-    // not acceptable implementation because it ignores `w`:
-    //data.self.air.dumpInst(data.inst, data.self.pt, data.self.liveness);
-    _ = data;
-    _ = w;
-    @panic("TODO: unimplemented");
+    data.self.air.writeInst(w, data.inst, data.self.pt, data.self.liveness);
 }
 fn fmtAir(self: *CodeGen, inst: Air.Inst.Index) std.fmt.Formatter(FormatAirData, formatAir) {
     return .{ .data = .{ .self = self, .inst = inst } };
@@ -179300,10 +179296,13 @@ fn lowerSwitchBr(
         } else undefined;
         const table_start: u31 = @intCast(cg.mir_table.items.len);
         {
-            const condition_index_reg = if (condition_index.isRegister())
-                condition_index.getReg().?
-            else
-                try cg.copyToTmpRegister(.usize, condition_index);
+            const condition_index_reg = condition_index_reg: {
+                if (condition_index.isRegister()) {
+                    const condition_index_reg = condition_index.getReg().?;
+                    if (condition_index_reg.isClass(.general_purpose)) break :condition_index_reg condition_index_reg;
+                }
+                break :condition_index_reg try cg.copyToTmpRegister(.usize, condition_index);
+            };
             const condition_index_lock = cg.register_manager.lockReg(condition_index_reg);
             defer if (condition_index_lock) |lock| cg.register_manager.unlockReg(lock);
             try cg.truncateRegister(condition_ty, condition_index_reg);
test/behavior/switch.zig
@@ -1072,3 +1072,13 @@ test "switch on a signed value smaller than the smallest prong value" {
         else => {},
     }
 }
+
+test "switch on 8-bit mod result" {
+    var x: u8 = undefined;
+    x = 16;
+    switch (x % 4) {
+        0 => {},
+        1, 2, 3 => return error.TestFailed,
+        else => unreachable,
+    }
+}