authorgravatar for xavierb@gmail.comXavier Bouchoux <xavierb@gmail.com> 2023-03-19 12:56:37+00:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-03-21 14:56:04+02:00
log898e4473e8acf664d67474716bb9728ed601c5a0
tree3362d654440ad5aee89b1811a53f9fe9a77d30c5
parentf7204c7f37ee69462b9ad41a76454831e0df09d0

CBE: implement aggregateInit() for array of array case.

fixes `error(compilation): clang failed with stderr: error: array type 'uint32_t[10]' (aka 'unsigned int[10]') is not assignable`

2 files changed, 39 insertions(+), 11 deletions(-)

src/codegen/c.zig+29-11
...@@ -6852,17 +6852,35 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -6852,17 +6852,35 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue {
6852 switch (inst_ty.zigTypeTag()) {6852 switch (inst_ty.zigTypeTag()) {
6853 .Array, .Vector => {6853 .Array, .Vector => {
6854 const elem_ty = inst_ty.childType();6854 const elem_ty = inst_ty.childType();
6855 for (resolved_elements, 0..) |element, i| {6855
6856 try f.writeCValue(writer, local, .Other);6856 const is_array = lowersToArray(elem_ty, target);
6857 try writer.print("[{d}] = ", .{i});6857 const need_memcpy = is_array;
6858 try f.writeCValue(writer, element, .Other);6858 if (need_memcpy) {
6859 try writer.writeAll(";\n");6859 for (resolved_elements, 0..) |element, i| {
6860 }6860 try writer.writeAll("memcpy(");
6861 if (inst_ty.sentinel()) |sentinel| {6861 try f.writeCValue(writer, local, .Other);
6862 try f.writeCValue(writer, local, .Other);6862 try writer.print("[{d}]", .{i});
6863 try writer.print("[{d}] = ", .{resolved_elements.len});6863 try writer.writeAll(", ");
6864 try f.object.dg.renderValue(writer, elem_ty, sentinel, .Other);6864 try f.writeCValue(writer, element, .Other);
6865 try writer.writeAll(";\n");6865 try writer.writeAll(", sizeof(");
6866 try f.renderType(writer, elem_ty);
6867 try writer.writeAll("))");
6868 try writer.writeAll(";\n");
6869 }
6870 assert(inst_ty.sentinel() == null);
6871 } else {
6872 for (resolved_elements, 0..) |element, i| {
6873 try f.writeCValue(writer, local, .Other);
6874 try writer.print("[{d}] = ", .{i});
6875 try f.writeCValue(writer, element, .Other);
6876 try writer.writeAll(";\n");
6877 }
6878 if (inst_ty.sentinel()) |sentinel| {
6879 try f.writeCValue(writer, local, .Other);
6880 try writer.print("[{d}] = ", .{resolved_elements.len});
6881 try f.object.dg.renderValue(writer, elem_ty, sentinel, .Other);
6882 try writer.writeAll(";\n");
6883 }
6866 }6884 }
6867 },6885 },
6868 .Struct => switch (inst_ty.containerLayout()) {6886 .Struct => switch (inst_ty.containerLayout()) {
test/behavior/array.zig+10
...@@ -669,3 +669,13 @@ test "runtime initialized sentinel-terminated array literal" {...@@ -669,3 +669,13 @@ test "runtime initialized sentinel-terminated array literal" {
669 try std.testing.expect(g[2] == 0x99);669 try std.testing.expect(g[2] == 0x99);
670 try std.testing.expect(g[3] == 0x99);670 try std.testing.expect(g[3] == 0x99);
671}671}
672
673test "array of array agregate init" {
674 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
675 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
676 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
677
678 var a = [1]u32{11} ** 10;
679 var b = [1][10]u32{a} ** 2;
680 try std.testing.expect(b[1][1] == 11);
681}