authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-04-25 11:22:15-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-04-25 11:23:41-07:00
logbadad16f88ac7e1eb84eadf76e13b4dc346d4ced
tree990033e793deb56107339304d52fe8ce8bbdfbb3
parent401b7f6f537699771ddbc82d524439c5f3db7ea7

C backend: fix lowering comparison when array ptr meets ptr

Pointer comparisons were triggering `-Wcompare-distinct-pointer-types` before this fix, which adds `(void*)` casts if the lhs type and rhs type do not match pointer sizeness.

1 files changed, 7 insertions(+), 3 deletions(-)

src/codegen/c.zig+7-3
...@@ -3825,8 +3825,8 @@ fn airCmpOp(...@@ -3825,8 +3825,8 @@ fn airCmpOp(
3825 data: anytype,3825 data: anytype,
3826 operator: std.math.CompareOperator,3826 operator: std.math.CompareOperator,
3827) !CValue {3827) !CValue {
3828 const operand_ty = f.air.typeOf(data.lhs);3828 const lhs_ty = f.air.typeOf(data.lhs);
3829 const scalar_ty = operand_ty.scalarType();3829 const scalar_ty = lhs_ty.scalarType();
38303830
3831 const target = f.object.dg.module.getTarget();3831 const target = f.object.dg.module.getTarget();
3832 const scalar_bits = scalar_ty.bitSize(target);3832 const scalar_bits = scalar_ty.bitSize(target);
...@@ -3847,17 +3847,21 @@ fn airCmpOp(...@@ -3847,17 +3847,21 @@ fn airCmpOp(
3847 const rhs = try f.resolveInst(data.rhs);3847 const rhs = try f.resolveInst(data.rhs);
3848 try reap(f, inst, &.{ data.lhs, data.rhs });3848 try reap(f, inst, &.{ data.lhs, data.rhs });
38493849
3850 const rhs_ty = f.air.typeOf(data.rhs);
3851 const need_cast = lhs_ty.isSinglePointer() != rhs_ty.isSinglePointer();
3850 const writer = f.object.writer();3852 const writer = f.object.writer();
3851 const local = try f.allocLocal(inst, inst_ty);3853 const local = try f.allocLocal(inst, inst_ty);
3852 const v = try Vectorize.start(f, inst, writer, operand_ty);3854 const v = try Vectorize.start(f, inst, writer, lhs_ty);
3853 try f.writeCValue(writer, local, .Other);3855 try f.writeCValue(writer, local, .Other);
3854 try v.elem(f, writer);3856 try v.elem(f, writer);
3855 try writer.writeAll(" = ");3857 try writer.writeAll(" = ");
3858 if (need_cast) try writer.writeAll("(void*)");
3856 try f.writeCValue(writer, lhs, .Other);3859 try f.writeCValue(writer, lhs, .Other);
3857 try v.elem(f, writer);3860 try v.elem(f, writer);
3858 try writer.writeByte(' ');3861 try writer.writeByte(' ');
3859 try writer.writeAll(compareOperatorC(operator));3862 try writer.writeAll(compareOperatorC(operator));
3860 try writer.writeByte(' ');3863 try writer.writeByte(' ');
3864 if (need_cast) try writer.writeAll("(void*)");
3861 try f.writeCValue(writer, rhs, .Other);3865 try f.writeCValue(writer, rhs, .Other);
3862 try v.elem(f, writer);3866 try v.elem(f, writer);
3863 try writer.writeAll(";\n");3867 try writer.writeAll(";\n");