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> 2023-01-09 14:29:59-07:00
logdd175e2928c99962b58c1c6725299b3a995900fb
treedfd17baab0b521260ccf7558368df866430375c1
parent90fedf24fcec4bd7a9e71be100e0ae73c7245b33

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 {...@@ -740,14 +740,24 @@ pub const DeclGen = struct {
740 try writer.writeByte(')');740 try writer.writeByte(')');
741 }741 }
742742
743 try writer.writeByte('{');743 const ai = ty.arrayInfo();
744 const c_len = ty.arrayLenIncludingSentinel();744 if (ai.elem_type.eql(Type.u8, dg.module)) {
745 var index: usize = 0;745 try writer.writeByte('"');
746 while (index < c_len) : (index += 1) {746 const c_len = ty.arrayLenIncludingSentinel();
747 if (index > 0) try writer.writeAll(", ");747 var index: usize = 0;
748 try dg.renderValue(writer, ty.childType(), val, .Initializer);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('}');
749 }760 }
750 return writer.writeByte('}');
751 },761 },
752 .ComptimeInt,762 .ComptimeInt,
753 .ComptimeFloat,763 .ComptimeFloat,
...@@ -931,25 +941,48 @@ pub const DeclGen = struct {...@@ -931,25 +941,48 @@ pub const DeclGen = struct {
931 }941 }
932 try writer.writeByte('}');942 try writer.writeByte('}');
933 },943 },
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 },
934 else => {952 else => {
935 // Fall back to generic implementation.953 // Fall back to generic implementation.
936 var arena = std.heap.ArenaAllocator.init(dg.gpa);954 var arena = std.heap.ArenaAllocator.init(dg.gpa);
937 defer arena.deinit();955 defer arena.deinit();
938 const arena_allocator = arena.allocator();956 const arena_allocator = arena.allocator();
939957
940 try writer.writeByte('{');
941 const ai = ty.arrayInfo();958 const ai = ty.arrayInfo();
942 var index: usize = 0;959 if (ai.elem_type.eql(Type.u8, dg.module)) {
943 while (index < ai.len) : (index += 1) {960 try writer.writeByte('"');
944 if (index != 0) try writer.writeByte(',');961 var index: usize = 0;
945 const elem_val = try val.elemValue(dg.module, arena_allocator, index);962 while (index < ai.len) : (index += 1) {
946 try dg.renderValue(writer, ai.elem_type, elem_val, .Initializer);963 const elem_val = try val.elemValue(dg.module, arena_allocator, index);
947 }964 const elem_val_u8 = @intCast(u8, elem_val.toUnsignedInt(target));
948 if (ai.sentinel) |s| {965 try writeStringLiteralChar(writer, elem_val_u8);
949 if (index != 0) try writer.writeByte(',');966 }
950 try dg.renderValue(writer, ai.elem_type, s, .Initializer);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('}');
951 }985 }
952 try writer.writeByte('}');
953 },986 },
954 }987 }
955 },988 },
...@@ -5618,7 +5651,17 @@ fn formatStringLiteral(...@@ -5618,7 +5651,17 @@ fn formatStringLiteral(
5618) @TypeOf(writer).Error!void {5651) @TypeOf(writer).Error!void {
5619 if (fmt.len != 1 or fmt[0] != 's') @compileError("Invalid fmt: " ++ fmt);5652 if (fmt.len != 1 or fmt[0] != 's') @compileError("Invalid fmt: " ++ fmt);
5620 try writer.writeByte('\"');5653 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) {
5622 7 => try writer.writeAll("\\a"),5665 7 => try writer.writeAll("\\a"),
5623 8 => try writer.writeAll("\\b"),5666 8 => try writer.writeAll("\\b"),
5624 '\t' => try writer.writeAll("\\t"),5667 '\t' => try writer.writeAll("\\t"),
...@@ -5631,11 +5674,7 @@ fn formatStringLiteral(...@@ -5631,11 +5674,7 @@ fn formatStringLiteral(
5631 ' '...'~' => try writer.writeByte(c),5674 ' '...'~' => try writer.writeByte(c),
5632 else => try writer.print("\\{o:0>3}", .{c}),5675 else => try writer.print("\\{o:0>3}", .{c}),
5633 },5676 },
5634 };5677 }
5635 try writer.writeByte('\"');
5636}
5637fn fmtStringLiteral(str: []const u8) std.fmt.Formatter(formatStringLiteral) {
5638 return .{ .data = str };
5639}5678}
56405679
5641fn undefPattern(comptime IntType: type) IntType {5680fn undefPattern(comptime IntType: type) IntType {