Commit 0f65cc9275

Andrew Kelley <andrew@ziglang.org>
2023-04-23 22:28:44
C backend: fix memset for loop lowering
Previously, this code casted the array pointer to u8 pointer, but I removed that in a different commit. This commit restores the cast, but instead of hard-coding u8, it uses the destination element pointer, since memset now supports arbitrary element types.
1 parent 057c950
Changed files (1)
src
codegen
src/codegen/c.zig
@@ -6233,6 +6233,15 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
     }
 
     if (elem_abi_size > 1 or dest_ty.isVolatilePtr()) {
+        // For the assignment in this loop, the array pointer needs to get
+        // casted to a regular pointer, otherwise an error like this occurs:
+        // error: array type 'uint32_t[20]' (aka 'unsigned int[20]') is not assignable
+        var elem_ptr_ty_pl: Type.Payload.ElemType = .{
+            .base = .{ .tag = .c_mut_pointer },
+            .data = elem_ty,
+        };
+        const elem_ptr_ty = Type.initPayload(&elem_ptr_ty_pl.base);
+
         const index = try f.allocLocal(inst, Type.usize);
 
         try writer.writeAll("for (");
@@ -6256,7 +6265,9 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
         try f.writeCValue(writer, index, .Other);
         try writer.writeAll(" += ");
         try f.object.dg.renderValue(writer, Type.usize, Value.one, .Other);
-        try writer.writeAll(") (");
+        try writer.writeAll(") ((");
+        try f.renderType(writer, elem_ptr_ty);
+        try writer.writeByte(')');
         try writeSliceOrPtr(f, writer, dest_slice, dest_ty);
         try writer.writeAll(")[");
         try f.writeCValue(writer, index, .Other);