Commit 6310186d52

Veikka Tuominen <git@vexu.eu>
2022-11-27 16:16:01
cbe: cast pointer switch target to int
1 parent 8af5648
Changed files (2)
src
codegen
test
behavior
src/codegen/c.zig
@@ -3894,10 +3894,14 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index) !CValue {
     const writer = f.object.writer();
 
     try writer.writeAll("switch (");
-    if (condition_ty.tag() == .bool) {
+    if (condition_ty.zigTypeTag() == .Bool) {
         try writer.writeByte('(');
         try f.renderTypecast(writer, Type.u1);
         try writer.writeByte(')');
+    } else if (condition_ty.isPtrAtRuntime()) {
+        try writer.writeByte('(');
+        try f.renderTypecast(writer, Type.usize);
+        try writer.writeByte(')');
     }
     try f.writeCValue(writer, condition, .Other);
     try writer.writeAll(") {");
@@ -3914,6 +3918,11 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index) !CValue {
         for (items) |item| {
             try f.object.indent_writer.insertNewline();
             try writer.writeAll("case ");
+            if (condition_ty.isPtrAtRuntime()) {
+                try writer.writeByte('(');
+                try f.renderTypecast(writer, Type.usize);
+                try writer.writeByte(')');
+            }
             try f.object.dg.renderValue(writer, condition_ty, f.air.value(item).?, .Other);
             try writer.writeAll(": ");
         }
test/behavior/switch.zig
@@ -548,7 +548,6 @@ test "switch prongs with cases with identical payload types" {
 }
 
 test "switch on pointer type" {
-    if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
     if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
     if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO