authorgravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2022-11-19 10:54:43+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-11-19 11:59:33-05:00
logadc3fafbc0c46485ae6c218bc791019741b7536c
tree4e1df792c1719d228a2671eb8b3e98e0974b88dd
parentf746e118795af8f8eb16deefc8f6fed26f3be4dd

c backend: Output string literals for array init of bytes


1 files changed, 63 insertions(+), 24 deletions(-)

src/codegen/c.zig+63-24
......@@ -740,14 +740,24 @@ pub const DeclGen = struct {
740740 try writer.writeByte(')');
741741 }
742742
743 try writer.writeByte('{');
744 const c_len = ty.arrayLenIncludingSentinel();
745 var index: usize = 0;
746 while (index < c_len) : (index += 1) {
747 if (index > 0) try writer.writeAll(", ");
748 try dg.renderValue(writer, ty.childType(), val, .Initializer);
743 const ai = ty.arrayInfo();
744 if (ai.elem_type.eql(Type.u8, dg.module)) {
745 try writer.writeByte('"');
746 const c_len = ty.arrayLenIncludingSentinel();
747 var index: usize = 0;
748 while (index < c_len) : (index += 1)
749 try writeStringLiteralChar(writer, 0xaa);
750 return writer.writeByte('"');
751 } else {
752 try writer.writeByte('{');
753 const c_len = ty.arrayLenIncludingSentinel();
754 var index: usize = 0;
755 while (index < c_len) : (index += 1) {
756 if (index > 0) try writer.writeAll(", ");
757 try dg.renderValue(writer, ty.childType(), val, .Initializer);
758 }
759 return writer.writeByte('}');
749760 }
750 return writer.writeByte('}');
751761 },
752762 .ComptimeInt,
753763 .ComptimeFloat,
......@@ -931,25 +941,48 @@ pub const DeclGen = struct {
931941 }
932942 try writer.writeByte('}');
933943 },
944 .bytes => {
945 try writer.print("{s}", .{fmtStringLiteral(val.castTag(.bytes).?.data)});
946 },
947 .str_lit => {
948 const str_lit = val.castTag(.str_lit).?.data;
949 const bytes = dg.module.string_literal_bytes.items[str_lit.index..][0..str_lit.len];
950 try writer.print("{s}", .{fmtStringLiteral(bytes)});
951 },
934952 else => {
935953 // Fall back to generic implementation.
936954 var arena = std.heap.ArenaAllocator.init(dg.gpa);
937955 defer arena.deinit();
938956 const arena_allocator = arena.allocator();
939957
940 try writer.writeByte('{');
941958 const ai = ty.arrayInfo();
942 var index: usize = 0;
943 while (index < ai.len) : (index += 1) {
944 if (index != 0) try writer.writeByte(',');
945 const elem_val = try val.elemValue(dg.module, arena_allocator, index);
946 try dg.renderValue(writer, ai.elem_type, elem_val, .Initializer);
947 }
948 if (ai.sentinel) |s| {
949 if (index != 0) try writer.writeByte(',');
950 try dg.renderValue(writer, ai.elem_type, s, .Initializer);
959 if (ai.elem_type.eql(Type.u8, dg.module)) {
960 try writer.writeByte('"');
961 var index: usize = 0;
962 while (index < ai.len) : (index += 1) {
963 const elem_val = try val.elemValue(dg.module, arena_allocator, index);
964 const elem_val_u8 = @intCast(u8, elem_val.toUnsignedInt(target));
965 try writeStringLiteralChar(writer, elem_val_u8);
966 }
967 if (ai.sentinel) |s| {
968 const s_u8 = @intCast(u8, s.toUnsignedInt(target));
969 try writeStringLiteralChar(writer, s_u8);
970 }
971 try writer.writeByte('"');
972 } else {
973 try writer.writeByte('{');
974 var index: usize = 0;
975 while (index < ai.len) : (index += 1) {
976 if (index != 0) try writer.writeByte(',');
977 const elem_val = try val.elemValue(dg.module, arena_allocator, index);
978 try dg.renderValue(writer, ai.elem_type, elem_val, .Initializer);
979 }
980 if (ai.sentinel) |s| {
981 if (index != 0) try writer.writeByte(',');
982 try dg.renderValue(writer, ai.elem_type, s, .Initializer);
983 }
984 try writer.writeByte('}');
951985 }
952 try writer.writeByte('}');
953986 },
954987 }
955988 },
......@@ -5618,7 +5651,17 @@ fn formatStringLiteral(
56185651) @TypeOf(writer).Error!void {
56195652 if (fmt.len != 1 or fmt[0] != 's') @compileError("Invalid fmt: " ++ fmt);
56205653 try writer.writeByte('\"');
5621 for (str) |c| switch (c) {
5654 for (str) |c|
5655 try writeStringLiteralChar(writer, c);
5656 try writer.writeByte('\"');
5657}
5658
5659fn fmtStringLiteral(str: []const u8) std.fmt.Formatter(formatStringLiteral) {
5660 return .{ .data = str };
5661}
5662
5663fn writeStringLiteralChar(writer: anytype, c: u8) !void {
5664 switch (c) {
56225665 7 => try writer.writeAll("\\a"),
56235666 8 => try writer.writeAll("\\b"),
56245667 '\t' => try writer.writeAll("\\t"),
......@@ -5631,11 +5674,7 @@ fn formatStringLiteral(
56315674 ' '...'~' => try writer.writeByte(c),
56325675 else => try writer.print("\\{o:0>3}", .{c}),
56335676 },
5634 };
5635 try writer.writeByte('\"');
5636}
5637fn fmtStringLiteral(str: []const u8) std.fmt.Formatter(formatStringLiteral) {
5638 return .{ .data = str };
5677 }
56395678}
56405679
56415680fn undefPattern(comptime IntType: type) IntType {