From 0f65cc9275cde61fe20f28e4f059c8af4c63b051 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 23 Apr 2023 13:28:44 -0700 Subject: 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. --- src/codegen/c.zig | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'src/codegen/c.zig') diff --git a/src/codegen/c.zig b/src/codegen/c.zig index 2823cbef05..582b4bf086 100644 --- a/src/codegen/c.zig +++ b/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); -- cgit v1.2.3