authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-07-09 03:32:34+00:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-07-09 03:32:34+00:00
log0e1c7209e8632ebf398e60de9053e2e0fe8b5661
treefb9978ab75adea5e3ec876776e101c548bc8e22a
parent12a7dedb1f1ce34b16993d33aa97d7b78d5d5ca2
parentd060be880460598dcf89cb3d59bfcdf5059a923d
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #5822 from pixelherodev/cbe

CBE cleanup

8 files changed, 48 insertions(+), 26 deletions(-)

src-self-hosted/Module.zig+1-1
......@@ -2456,7 +2456,7 @@ fn createAnonymousDecl(
24562456) !*Decl {
24572457 const name_index = self.getNextAnonNameIndex();
24582458 const scope_decl = scope.decl().?;
2459 const name = try std.fmt.allocPrint(self.allocator, "{}${}", .{ scope_decl.name, name_index });
2459 const name = try std.fmt.allocPrint(self.allocator, "{}__anon_{}", .{ scope_decl.name, name_index });
24602460 defer self.allocator.free(name);
24612461 const name_hash = scope.namespace().fullyQualifiedNameHash(name);
24622462 const src_hash: std.zig.SrcHash = undefined;
src-self-hosted/cbe.h+1-1
......@@ -1,6 +1,6 @@
11#if __STDC_VERSION__ >= 201112L
22#define noreturn _Noreturn
3#elif !__STRICT_ANSI__
3#elif __GNUC__ && !__STRICT_ANSI__
44#define noreturn __attribute__ ((noreturn))
55#else
66#define noreturn
src-self-hosted/cgen.zig+17-10
......@@ -11,8 +11,8 @@ const mem = std.mem;
1111
1212/// Maps a name from Zig source to C. This will always give the same output for
1313/// any given input.
14fn map(name: []const u8) ![]const u8 {
15 return name;
14fn map(allocator: *std.mem.Allocator, name: []const u8) ![]const u8 {
15 return allocator.dupe(u8, name);
1616}
1717
1818fn renderType(file: *C, writer: std.ArrayList(u8).Writer, T: Type, src: usize) !void {
......@@ -34,7 +34,8 @@ fn renderType(file: *C, writer: std.ArrayList(u8).Writer, T: Type, src: usize) !
3434fn renderFunctionSignature(file: *C, writer: std.ArrayList(u8).Writer, decl: *Decl) !void {
3535 const tv = decl.typed_value.most_recent.typed_value;
3636 try renderType(file, writer, tv.ty.fnReturnType(), decl.src());
37 const name = try map(mem.spanZ(decl.name));
37 const name = try map(file.allocator, mem.spanZ(decl.name));
38 defer file.allocator.free(name);
3839 try writer.print(" {}(", .{name});
3940 if (tv.ty.fnParamLen() == 0) {
4041 try writer.writeAll("void)");
......@@ -143,15 +144,21 @@ pub fn generate(file: *C, decl: *Decl) !void {
143144 try writer.writeAll("}\n\n");
144145 },
145146 .Array => {
146 if (mem.indexOf(u8, mem.span(decl.name), "$") == null) {
147 // TODO: prevent inline asm constants from being emitted
148 if (tv.val.cast(Value.Payload.Bytes)) |payload| {
149 try writer.print("const char *const {} = \"{}\";\n", .{ decl.name, payload.data });
150 std.debug.warn("\n\nARRAYTRANS\n", .{});
151 if (tv.ty.arraySentinel()) |sentinel| {}
147 // TODO: prevent inline asm constants from being emitted
148 const name = try map(file.allocator, mem.span(decl.name));
149 defer file.allocator.free(name);
150 if (tv.val.cast(Value.Payload.Bytes)) |payload| {
151 if (tv.ty.arraySentinel()) |sentinel| {
152 if (sentinel.toUnsignedInt() == 0) {
153 try file.constants.writer().print("const char *const {} = \"{}\";\n", .{ name, payload.data });
154 } else {
155 return file.fail(decl.src(), "TODO byte arrays with non-zero sentinels", .{});
156 }
152157 } else {
153 return file.fail(decl.src(), "TODO non-byte arrays", .{});
158 return file.fail(decl.src(), "TODO byte arrays without sentinels", .{});
154159 }
160 } else {
161 return file.fail(decl.src(), "TODO non-byte arrays", .{});
155162 }
156163 },
157164 else => |e| {
src-self-hosted/link.zig+9-1
......@@ -86,13 +86,14 @@ pub fn writeFilePath(
8686 return result;
8787}
8888
89pub fn openCFile(allocator: *Allocator, file: fs.File, options: Options) !File.C {
89fn openCFile(allocator: *Allocator, file: fs.File, options: Options) !File.C {
9090 return File.C{
9191 .allocator = allocator,
9292 .file = file,
9393 .options = options,
9494 .main = std.ArrayList(u8).init(allocator),
9595 .header = std.ArrayList(u8).init(allocator),
96 .constants = std.ArrayList(u8).init(allocator),
9697 .called = std.StringHashMap(void).init(allocator),
9798 };
9899}
......@@ -220,6 +221,7 @@ pub const File = struct {
220221
221222 allocator: *Allocator,
222223 header: std.ArrayList(u8),
224 constants: std.ArrayList(u8),
223225 main: std.ArrayList(u8),
224226 file: ?fs.File,
225227 options: Options,
......@@ -237,6 +239,7 @@ pub const File = struct {
237239 pub fn deinit(self: *File.C) void {
238240 self.main.deinit();
239241 self.header.deinit();
242 self.constants.deinit();
240243 self.called.deinit();
241244 if (self.file) |f|
242245 f.close();
......@@ -269,6 +272,9 @@ pub const File = struct {
269272 if (self.header.items.len > 0) {
270273 try writer.print("{}\n", .{self.header.items});
271274 }
275 if (self.constants.items.len > 0) {
276 try writer.print("{}\n", .{self.constants.items});
277 }
272278 if (self.main.items.len > 1) {
273279 const last_two = self.main.items[self.main.items.len - 2 ..];
274280 if (std.mem.eql(u8, last_two, "\n\n")) {
......@@ -276,6 +282,8 @@ pub const File = struct {
276282 }
277283 }
278284 try writer.writeAll(self.main.items);
285 self.file.?.close();
286 self.file = null;
279287 }
280288 };
281289
src-self-hosted/main.zig+4-1
......@@ -433,7 +433,10 @@ fn buildOutputType(
433433 std.debug.print("-fno-emit-bin not supported yet", .{});
434434 process.exit(1);
435435 },
436 .yes_default_path => try std.fmt.allocPrint(arena, "{}.c", .{root_name}),
436 .yes_default_path => if (cbe)
437 try std.fmt.allocPrint(arena, "{}.c", .{root_name})
438 else
439 try std.zig.binNameAlloc(arena, root_name, target_info.target, output_mode, link_mode),
437440
438441 .yes => |p| p,
439442 };
src-self-hosted/test.zig+2-3
......@@ -480,9 +480,8 @@ pub const TestContext = struct {
480480 switch (update.case) {
481481 .Transformation => |expected_output| {
482482 if (case.cbe) {
483 var cfile: *link.File.C = module.bin_file.cast(link.File.C).?;
484 cfile.file.?.close();
485 cfile.file = null;
483 // The C file is always closed after an update, because we don't support
484 // incremental updates
486485 var file = try tmp.dir.openFile(bin_name, .{ .read = true });
487486 defer file.close();
488487 var out = file.reader().readAllAlloc(allocator, 1024 * 1024) catch @panic("Unable to read C output!");
test/stage2/cbe.zig+5
......@@ -32,6 +32,7 @@ pub fn addCases(ctx: *TestContext) !void {
3232 \\
3333 );
3434 // TODO: implement return values
35 // TODO: figure out a way to prevent asm constants from being generated
3536 ctx.c("inline asm", linux_x64,
3637 \\fn exitGood() void {
3738 \\ asm volatile ("syscall"
......@@ -49,6 +50,10 @@ pub fn addCases(ctx: *TestContext) !void {
4950 \\
5051 \\void exitGood(void);
5152 \\
53 \\const char *const exitGood__anon_0 = "{rax}";
54 \\const char *const exitGood__anon_1 = "{rdi}";
55 \\const char *const exitGood__anon_2 = "syscall";
56 \\
5257 \\noreturn void _start(void) {
5358 \\ exitGood();
5459 \\}
test/stage2/zir.zig+9-9
......@@ -22,8 +22,8 @@ pub fn addCases(ctx: *TestContext) !void {
2222 ,
2323 \\@void = primitive(void)
2424 \\@fnty = fntype([], @void, cc=C)
25 \\@9 = declref("9$0")
26 \\@9$0 = str("entry")
25 \\@9 = declref("9__anon_0")
26 \\@9__anon_0 = str("entry")
2727 \\@unnamed$4 = str("entry")
2828 \\@unnamed$5 = export(@unnamed$4, "entry")
2929 \\@unnamed$6 = fntype([], @void, cc=C)
......@@ -77,9 +77,9 @@ pub fn addCases(ctx: *TestContext) !void {
7777 \\@entry = fn(@unnamed$6, {
7878 \\ %0 = returnvoid()
7979 \\})
80 \\@entry$1 = str("2\x08\x01\n")
81 \\@9 = declref("9$0")
82 \\@9$0 = str("entry")
80 \\@entry__anon_1 = str("2\x08\x01\n")
81 \\@9 = declref("9__anon_0")
82 \\@9__anon_0 = str("entry")
8383 \\@unnamed$11 = str("entry")
8484 \\@unnamed$12 = export(@unnamed$11, "entry")
8585 \\
......@@ -111,8 +111,8 @@ pub fn addCases(ctx: *TestContext) !void {
111111 ,
112112 \\@void = primitive(void)
113113 \\@fnty = fntype([], @void, cc=C)
114 \\@9 = declref("9$0")
115 \\@9$0 = str("entry")
114 \\@9 = declref("9__anon_0")
115 \\@9__anon_0 = str("entry")
116116 \\@unnamed$4 = str("entry")
117117 \\@unnamed$5 = export(@unnamed$4, "entry")
118118 \\@unnamed$6 = fntype([], @void, cc=C)
......@@ -187,8 +187,8 @@ pub fn addCases(ctx: *TestContext) !void {
187187 ,
188188 \\@void = primitive(void)
189189 \\@fnty = fntype([], @void, cc=C)
190 \\@9 = declref("9$2")
191 \\@9$2 = str("entry")
190 \\@9 = declref("9__anon_2")
191 \\@9__anon_2 = str("entry")
192192 \\@unnamed$4 = str("entry")
193193 \\@unnamed$5 = export(@unnamed$4, "entry")
194194 \\@unnamed$6 = fntype([], @void, cc=C)