authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-04-22 10:55:41-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-04-22 10:55:41-04:00
log42e81cd81b8a0167c1a21aadf554cea2d882eadd
treee19d5d14323ae797495027f33fbb63120fce660b
parent5da0e0355399acdb0896ad4ac691ecfc9d32222f
parent0e830b16306945ffc74ef95198eb74743ab9f184
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #11377 from AnnikaCodes/11368

C backend: Fix array declarations

3 files changed, 81 insertions(+), 32 deletions(-)

src/codegen/c.zig+39-27
......@@ -61,6 +61,11 @@ const FormatTypeAsCIdentContext = struct {
6161 mod: *Module,
6262};
6363
64const ValueRenderLocation = enum {
65 FunctionArgument,
66 Other,
67};
68
6469/// TODO make this not cut off at 128 bytes
6570fn formatTypeAsCIdentifier(
6671 data: FormatTypeAsCIdentContext,
......@@ -259,7 +264,7 @@ pub const Function = struct {
259264 0,
260265 );
261266 try writer.writeAll(" = ");
262 try f.object.dg.renderValue(writer, ty, val);
267 try f.object.dg.renderValue(writer, ty, val, .Other);
263268 try writer.writeAll(";\n ");
264269 return decl_c_value;
265270 },
......@@ -298,7 +303,7 @@ pub const Function = struct {
298303 .constant => |inst| {
299304 const ty = f.air.typeOf(inst);
300305 const val = f.air.value(inst).?;
301 return f.object.dg.renderValue(w, ty, val);
306 return f.object.dg.renderValue(w, ty, val, .Other);
302307 },
303308 else => return f.object.dg.writeCValue(w, c_value),
304309 }
......@@ -310,7 +315,7 @@ pub const Function = struct {
310315 const ty = f.air.typeOf(inst);
311316 const val = f.air.value(inst).?;
312317 try w.writeAll("(*");
313 try f.object.dg.renderValue(w, ty, val);
318 try f.object.dg.renderValue(w, ty, val, .Other);
314319 return w.writeByte(')');
315320 },
316321 else => return f.object.dg.writeCValueDeref(w, c_value),
......@@ -384,7 +389,7 @@ pub const DeclGen = struct {
384389 try dg.renderTypecast(writer, ty);
385390 try writer.writeAll("){");
386391 var buf: Type.SlicePtrFieldTypeBuffer = undefined;
387 try dg.renderValue(writer, ty.slicePtrFieldType(&buf), val.slicePtr());
392 try dg.renderValue(writer, ty.slicePtrFieldType(&buf), val.slicePtr(), .Other);
388393 try writer.writeAll(", ");
389394 try writer.print("{d}", .{val.sliceLen(dg.module)});
390395 try writer.writeAll("}");
......@@ -544,6 +549,7 @@ pub const DeclGen = struct {
544549 writer: anytype,
545550 ty: Type,
546551 val: Value,
552 location: ValueRenderLocation,
547553 ) error{ OutOfMemory, AnalysisFail }!void {
548554 const target = dg.module.getTarget();
549555 if (val.isUndefDeep()) {
......@@ -636,9 +642,9 @@ pub const DeclGen = struct {
636642 try writer.writeByte('(');
637643 try dg.renderTypecast(writer, ty);
638644 try writer.writeAll("){");
639 try dg.renderValue(writer, ty.slicePtrFieldType(&buf), slice.ptr);
645 try dg.renderValue(writer, ty.slicePtrFieldType(&buf), slice.ptr, location);
640646 try writer.writeAll(", ");
641 try dg.renderValue(writer, Type.usize, slice.len);
647 try dg.renderValue(writer, Type.usize, slice.len, location);
642648 try writer.writeAll("}");
643649 },
644650 .function => {
......@@ -670,7 +676,7 @@ pub const DeclGen = struct {
670676 try writer.writeByte('{');
671677 const ai = ty.arrayInfo();
672678 if (ai.sentinel) |s| {
673 try dg.renderValue(writer, ai.elem_type, s);
679 try dg.renderValue(writer, ai.elem_type, s, location);
674680 }
675681 try writer.writeByte('}');
676682 },
......@@ -680,17 +686,23 @@ pub const DeclGen = struct {
680686 defer arena.deinit();
681687 const arena_allocator = arena.allocator();
682688
689 if (location == .FunctionArgument) {
690 try writer.writeByte('(');
691 try dg.renderTypecast(writer, ty);
692 try writer.writeByte(')');
693 }
694
683695 try writer.writeByte('{');
684696 const ai = ty.arrayInfo();
685697 var index: usize = 0;
686698 while (index < ai.len) : (index += 1) {
687699 if (index != 0) try writer.writeAll(",");
688700 const elem_val = try val.elemValue(dg.module, arena_allocator, index);
689 try dg.renderValue(writer, ai.elem_type, elem_val);
701 try dg.renderValue(writer, ai.elem_type, elem_val, .Other);
690702 }
691703 if (ai.sentinel) |s| {
692704 if (index != 0) try writer.writeAll(",");
693 try dg.renderValue(writer, ai.elem_type, s);
705 try dg.renderValue(writer, ai.elem_type, s, .Other);
694706 }
695707 try writer.writeByte('}');
696708 },
......@@ -701,7 +713,7 @@ pub const DeclGen = struct {
701713 var opt_buf: Type.Payload.ElemType = undefined;
702714 const payload_type = ty.optionalChild(&opt_buf);
703715 if (ty.isPtrLikeOptional()) {
704 return dg.renderValue(writer, payload_type, val);
716 return dg.renderValue(writer, payload_type, val, location);
705717 }
706718 if (payload_type.abiSize(target) == 0) {
707719 const is_null = val.castTag(.opt_payload) == null;
......@@ -713,7 +725,7 @@ pub const DeclGen = struct {
713725 if (val.castTag(.opt_payload)) |pl| {
714726 const payload_val = pl.data;
715727 try writer.writeAll(" .is_null = false, .payload = ");
716 try dg.renderValue(writer, payload_type, payload_val);
728 try dg.renderValue(writer, payload_type, payload_val, location);
717729 try writer.writeAll(" }");
718730 } else {
719731 try writer.writeAll(" .is_null = true }");
......@@ -740,7 +752,7 @@ pub const DeclGen = struct {
740752 if (!payload_type.hasRuntimeBits()) {
741753 // We use the error type directly as the type.
742754 const err_val = if (val.errorUnionIsPayload()) Value.initTag(.zero) else val;
743 return dg.renderValue(writer, error_type, err_val);
755 return dg.renderValue(writer, error_type, err_val, location);
744756 }
745757
746758 try writer.writeByte('(');
......@@ -749,11 +761,11 @@ pub const DeclGen = struct {
749761 if (val.castTag(.eu_payload)) |pl| {
750762 const payload_val = pl.data;
751763 try writer.writeAll(" .payload = ");
752 try dg.renderValue(writer, payload_type, payload_val);
764 try dg.renderValue(writer, payload_type, payload_val, location);
753765 try writer.writeAll(", .error = 0 }");
754766 } else {
755767 try writer.writeAll(" .error = ");
756 try dg.renderValue(writer, error_type, val);
768 try dg.renderValue(writer, error_type, val, location);
757769 try writer.writeAll(" }");
758770 }
759771 },
......@@ -767,7 +779,7 @@ pub const DeclGen = struct {
767779 const enum_full = ty.cast(Type.Payload.EnumFull).?.data;
768780 if (enum_full.values.count() != 0) {
769781 const tag_val = enum_full.values.keys()[field_index];
770 return dg.renderValue(writer, enum_full.tag_ty, tag_val);
782 return dg.renderValue(writer, enum_full.tag_ty, tag_val, location);
771783 } else {
772784 return writer.print("{d}", .{field_index});
773785 }
......@@ -776,7 +788,7 @@ pub const DeclGen = struct {
776788 const enum_obj = ty.castTag(.enum_numbered).?.data;
777789 if (enum_obj.values.count() != 0) {
778790 const tag_val = enum_obj.values.keys()[field_index];
779 return dg.renderValue(writer, enum_obj.tag_ty, tag_val);
791 return dg.renderValue(writer, enum_obj.tag_ty, tag_val, location);
780792 } else {
781793 return writer.print("{d}", .{field_index});
782794 }
......@@ -787,7 +799,7 @@ pub const DeclGen = struct {
787799 else => {
788800 var int_tag_ty_buffer: Type.Payload.Bits = undefined;
789801 const int_tag_ty = ty.intTagType(&int_tag_ty_buffer);
790 return dg.renderValue(writer, int_tag_ty, val);
802 return dg.renderValue(writer, int_tag_ty, val, location);
791803 },
792804 }
793805 },
......@@ -814,7 +826,7 @@ pub const DeclGen = struct {
814826 if (!field_ty.hasRuntimeBits()) continue;
815827
816828 if (i != 0) try writer.writeAll(",");
817 try dg.renderValue(writer, field_ty, field_val);
829 try dg.renderValue(writer, field_ty, field_val, location);
818830 }
819831
820832 try writer.writeAll("}");
......@@ -831,7 +843,7 @@ pub const DeclGen = struct {
831843 if (ty.unionTagType()) |tag_ty| {
832844 if (layout.tag_size != 0) {
833845 try writer.writeAll(".tag = ");
834 try dg.renderValue(writer, tag_ty, union_obj.tag);
846 try dg.renderValue(writer, tag_ty, union_obj.tag, location);
835847 try writer.writeAll(", ");
836848 }
837849 try writer.writeAll(".payload = {");
......@@ -842,7 +854,7 @@ pub const DeclGen = struct {
842854 const field_name = ty.unionFields().keys()[index];
843855 if (field_ty.hasRuntimeBits()) {
844856 try writer.print(".{ } = ", .{fmtIdent(field_name)});
845 try dg.renderValue(writer, field_ty, union_obj.val);
857 try dg.renderValue(writer, field_ty, union_obj.val, location);
846858 }
847859 if (ty.unionTagType()) |_| {
848860 try writer.writeAll("}");
......@@ -988,7 +1000,7 @@ pub const DeclGen = struct {
9881000 }
9891001 if (ptr_sentinel) |s| {
9901002 try bw.writeAll("_s_");
991 try dg.renderValue(bw, child_type, s);
1003 try dg.renderValue(bw, child_type, s, .Other);
9921004 }
9931005 try bw.writeAll(";\n");
9941006
......@@ -1629,7 +1641,7 @@ pub fn genDecl(o: *Object) !void {
16291641 try o.dg.renderTypeAndName(w, o.dg.decl.ty, decl_c_value, .Mut, o.dg.decl.@"align");
16301642 try w.writeAll(" = ");
16311643 if (variable.init.tag() != .unreachable_value) {
1632 try o.dg.renderValue(w, tv.ty, variable.init);
1644 try o.dg.renderValue(w, tv.ty, variable.init, .Other);
16331645 }
16341646 try w.writeAll(";");
16351647 try o.indent_writer.insertNewline();
......@@ -1644,7 +1656,7 @@ pub fn genDecl(o: *Object) !void {
16441656 try o.dg.renderTypeAndName(writer, tv.ty, decl_c_value, .Mut, o.dg.decl.@"align");
16451657
16461658 try writer.writeAll(" = ");
1647 try o.dg.renderValue(writer, tv.ty, tv.val);
1659 try o.dg.renderValue(writer, tv.ty, tv.val, .Other);
16481660 try writer.writeAll(";\n");
16491661 }
16501662}
......@@ -2748,7 +2760,7 @@ fn airCall(
27482760 try writer.writeAll(", ");
27492761 }
27502762 if (f.air.value(arg)) |val| {
2751 try f.object.dg.renderValue(writer, f.air.typeOf(arg), val);
2763 try f.object.dg.renderValue(writer, f.air.typeOf(arg), val, .FunctionArgument);
27522764 } else {
27532765 const val = try f.resolveInst(arg);
27542766 try f.writeCValue(writer, val);
......@@ -2964,7 +2976,7 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index) !CValue {
29642976 for (items) |item| {
29652977 try f.object.indent_writer.insertNewline();
29662978 try writer.writeAll("case ");
2967 try f.object.dg.renderValue(writer, condition_ty, f.air.value(item).?);
2979 try f.object.dg.renderValue(writer, condition_ty, f.air.value(item).?, .Other);
29682980 try writer.writeAll(": ");
29692981 }
29702982 // The case body must be noreturn so we don't need to insert a break.
......@@ -3414,14 +3426,14 @@ fn airErrUnionPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue {
34143426 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) {
34153427 try f.writeCValueDeref(writer, operand);
34163428 try writer.writeAll(" = ");
3417 try f.object.dg.renderValue(writer, error_ty, Value.zero);
3429 try f.object.dg.renderValue(writer, error_ty, Value.zero, .Other);
34183430 try writer.writeAll(";\n ");
34193431
34203432 return operand;
34213433 }
34223434 try f.writeCValueDeref(writer, operand);
34233435 try writer.writeAll(".error = ");
3424 try f.object.dg.renderValue(writer, error_ty, Value.zero);
3436 try f.object.dg.renderValue(writer, error_ty, Value.zero, .Other);
34253437 try writer.writeAll(";\n");
34263438
34273439 // Then return the payload pointer (only if it is used)
test/behavior/array.zig+17-1
......@@ -148,7 +148,7 @@ test "void arrays" {
148148 try expect(array.len == 4);
149149}
150150
151test "nested arrays" {
151test "nested arrays of strings" {
152152 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
153153 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
154154
......@@ -162,6 +162,22 @@ test "nested arrays" {
162162 }
163163}
164164
165test "nested arrays of integers" {
166 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
167 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
168 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
169
170 const array_of_numbers = [_][2]u8{
171 [2]u8{ 1, 2 },
172 [2]u8{ 3, 4 },
173 };
174
175 try expect(array_of_numbers[0][0] == 1);
176 try expect(array_of_numbers[0][1] == 2);
177 try expect(array_of_numbers[1][0] == 3);
178 try expect(array_of_numbers[1][1] == 4);
179}
180
165181test "implicit comptime in array type size" {
166182 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
167183 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
test/behavior/fn.zig+25-4
......@@ -349,10 +349,9 @@ fn numberLiteralArg(a: anytype) !void {
349349}
350350
351351test "function call with anon list literal" {
352 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
353 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
354 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
355 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
352 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
353 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
354 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
356355
357356 const S = struct {
358357 fn doTheTest() !void {
......@@ -369,6 +368,28 @@ test "function call with anon list literal" {
369368 comptime try S.doTheTest();
370369}
371370
371test "function call with anon list literal - 2D" {
372 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
373 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
374 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
375 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
376
377 const S = struct {
378 fn doTheTest() !void {
379 try consumeVec(.{ .{ 9, 8 }, .{ 7, 6 } });
380 }
381
382 fn consumeVec(vec: [2][2]f32) !void {
383 try expect(vec[0][0] == 9);
384 try expect(vec[0][1] == 8);
385 try expect(vec[1][0] == 7);
386 try expect(vec[1][1] == 6);
387 }
388 };
389 try S.doTheTest();
390 comptime try S.doTheTest();
391}
392
372393test "ability to give comptime types and non comptime types to same parameter" {
373394 const S = struct {
374395 fn doTheTest() !void {