| author | |
| committer | |
| log | 0e1c7209e8632ebf398e60de9053e2e0fe8b5661 |
| tree | fb9978ab75adea5e3ec876776e101c548bc8e22a |
| parent | 12a7dedb1f1ce34b16993d33aa97d7b78d5d5ca2 |
| parent | d060be880460598dcf89cb3d59bfcdf5059a923d |
| signature |
CBE cleanup8 files changed, 48 insertions(+), 26 deletions(-)
src-self-hosted/Module.zig+1-1| ... | @@ -2456,7 +2456,7 @@ fn createAnonymousDecl( | ... | @@ -2456,7 +2456,7 @@ fn createAnonymousDecl( |
| 2456 | ) !*Decl { | 2456 | ) !*Decl { |
| 2457 | const name_index = self.getNextAnonNameIndex(); | 2457 | const name_index = self.getNextAnonNameIndex(); |
| 2458 | const scope_decl = scope.decl().?; | 2458 | 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 }); |
| 2460 | defer self.allocator.free(name); | 2460 | defer self.allocator.free(name); |
| 2461 | const name_hash = scope.namespace().fullyQualifiedNameHash(name); | 2461 | const name_hash = scope.namespace().fullyQualifiedNameHash(name); |
| 2462 | const src_hash: std.zig.SrcHash = undefined; | 2462 | const src_hash: std.zig.SrcHash = undefined; |
src-self-hosted/cbe.h+1-1| ... | @@ -1,6 +1,6 @@ | ... | @@ -1,6 +1,6 @@ |
| 1 | #if __STDC_VERSION__ >= 201112L | 1 | #if __STDC_VERSION__ >= 201112L |
| 2 | #define noreturn _Noreturn | 2 | #define noreturn _Noreturn |
| 3 | #elif !__STRICT_ANSI__ | 3 | #elif __GNUC__ && !__STRICT_ANSI__ |
| 4 | #define noreturn __attribute__ ((noreturn)) | 4 | #define noreturn __attribute__ ((noreturn)) |
| 5 | #else | 5 | #else |
| 6 | #define noreturn | 6 | #define noreturn |
src-self-hosted/cgen.zig+17-10| ... | @@ -11,8 +11,8 @@ const mem = std.mem; | ... | @@ -11,8 +11,8 @@ const mem = std.mem; |
| 11 | 11 | ||
| 12 | /// Maps a name from Zig source to C. This will always give the same output for | 12 | /// Maps a name from Zig source to C. This will always give the same output for |
| 13 | /// any given input. | 13 | /// any given input. |
| 14 | fn map(name: []const u8) ![]const u8 { | 14 | fn map(allocator: *std.mem.Allocator, name: []const u8) ![]const u8 { |
| 15 | return name; | 15 | return allocator.dupe(u8, name); |
| 16 | } | 16 | } |
| 17 | 17 | ||
| 18 | fn renderType(file: *C, writer: std.ArrayList(u8).Writer, T: Type, src: usize) !void { | 18 | fn 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) ! | ... | @@ -34,7 +34,8 @@ fn renderType(file: *C, writer: std.ArrayList(u8).Writer, T: Type, src: usize) ! |
| 34 | fn renderFunctionSignature(file: *C, writer: std.ArrayList(u8).Writer, decl: *Decl) !void { | 34 | fn renderFunctionSignature(file: *C, writer: std.ArrayList(u8).Writer, decl: *Decl) !void { |
| 35 | const tv = decl.typed_value.most_recent.typed_value; | 35 | const tv = decl.typed_value.most_recent.typed_value; |
| 36 | try renderType(file, writer, tv.ty.fnReturnType(), decl.src()); | 36 | 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); | ||
| 38 | try writer.print(" {}(", .{name}); | 39 | try writer.print(" {}(", .{name}); |
| 39 | if (tv.ty.fnParamLen() == 0) { | 40 | if (tv.ty.fnParamLen() == 0) { |
| 40 | try writer.writeAll("void)"); | 41 | try writer.writeAll("void)"); |
| ... | @@ -143,15 +144,21 @@ pub fn generate(file: *C, decl: *Decl) !void { | ... | @@ -143,15 +144,21 @@ pub fn generate(file: *C, decl: *Decl) !void { |
| 143 | try writer.writeAll("}\n\n"); | 144 | try writer.writeAll("}\n\n"); |
| 144 | }, | 145 | }, |
| 145 | .Array => { | 146 | .Array => { |
| 146 | if (mem.indexOf(u8, mem.span(decl.name), "$") == null) { | 147 | // TODO: prevent inline asm constants from being emitted |
| 147 | // TODO: prevent inline asm constants from being emitted | 148 | const name = try map(file.allocator, mem.span(decl.name)); |
| 148 | if (tv.val.cast(Value.Payload.Bytes)) |payload| { | 149 | defer file.allocator.free(name); |
| 149 | try writer.print("const char *const {} = \"{}\";\n", .{ decl.name, payload.data }); | 150 | if (tv.val.cast(Value.Payload.Bytes)) |payload| { |
| 150 | std.debug.warn("\n\nARRAYTRANS\n", .{}); | 151 | if (tv.ty.arraySentinel()) |sentinel| { |
| 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 | } | ||
| 152 | } else { | 157 | } else { |
| 153 | return file.fail(decl.src(), "TODO non-byte arrays", .{}); | 158 | return file.fail(decl.src(), "TODO byte arrays without sentinels", .{}); |
| 154 | } | 159 | } |
| 160 | } else { | ||
| 161 | return file.fail(decl.src(), "TODO non-byte arrays", .{}); | ||
| 155 | } | 162 | } |
| 156 | }, | 163 | }, |
| 157 | else => |e| { | 164 | else => |e| { |
src-self-hosted/link.zig+9-1| ... | @@ -86,13 +86,14 @@ pub fn writeFilePath( | ... | @@ -86,13 +86,14 @@ pub fn writeFilePath( |
| 86 | return result; | 86 | return result; |
| 87 | } | 87 | } |
| 88 | 88 | ||
| 89 | pub fn openCFile(allocator: *Allocator, file: fs.File, options: Options) !File.C { | 89 | fn openCFile(allocator: *Allocator, file: fs.File, options: Options) !File.C { |
| 90 | return File.C{ | 90 | return File.C{ |
| 91 | .allocator = allocator, | 91 | .allocator = allocator, |
| 92 | .file = file, | 92 | .file = file, |
| 93 | .options = options, | 93 | .options = options, |
| 94 | .main = std.ArrayList(u8).init(allocator), | 94 | .main = std.ArrayList(u8).init(allocator), |
| 95 | .header = std.ArrayList(u8).init(allocator), | 95 | .header = std.ArrayList(u8).init(allocator), |
| 96 | .constants = std.ArrayList(u8).init(allocator), | ||
| 96 | .called = std.StringHashMap(void).init(allocator), | 97 | .called = std.StringHashMap(void).init(allocator), |
| 97 | }; | 98 | }; |
| 98 | } | 99 | } |
| ... | @@ -220,6 +221,7 @@ pub const File = struct { | ... | @@ -220,6 +221,7 @@ pub const File = struct { |
| 220 | 221 | ||
| 221 | allocator: *Allocator, | 222 | allocator: *Allocator, |
| 222 | header: std.ArrayList(u8), | 223 | header: std.ArrayList(u8), |
| 224 | constants: std.ArrayList(u8), | ||
| 223 | main: std.ArrayList(u8), | 225 | main: std.ArrayList(u8), |
| 224 | file: ?fs.File, | 226 | file: ?fs.File, |
| 225 | options: Options, | 227 | options: Options, |
| ... | @@ -237,6 +239,7 @@ pub const File = struct { | ... | @@ -237,6 +239,7 @@ pub const File = struct { |
| 237 | pub fn deinit(self: *File.C) void { | 239 | pub fn deinit(self: *File.C) void { |
| 238 | self.main.deinit(); | 240 | self.main.deinit(); |
| 239 | self.header.deinit(); | 241 | self.header.deinit(); |
| 242 | self.constants.deinit(); | ||
| 240 | self.called.deinit(); | 243 | self.called.deinit(); |
| 241 | if (self.file) |f| | 244 | if (self.file) |f| |
| 242 | f.close(); | 245 | f.close(); |
| ... | @@ -269,6 +272,9 @@ pub const File = struct { | ... | @@ -269,6 +272,9 @@ pub const File = struct { |
| 269 | if (self.header.items.len > 0) { | 272 | if (self.header.items.len > 0) { |
| 270 | try writer.print("{}\n", .{self.header.items}); | 273 | try writer.print("{}\n", .{self.header.items}); |
| 271 | } | 274 | } |
| 275 | if (self.constants.items.len > 0) { | ||
| 276 | try writer.print("{}\n", .{self.constants.items}); | ||
| 277 | } | ||
| 272 | if (self.main.items.len > 1) { | 278 | if (self.main.items.len > 1) { |
| 273 | const last_two = self.main.items[self.main.items.len - 2 ..]; | 279 | const last_two = self.main.items[self.main.items.len - 2 ..]; |
| 274 | if (std.mem.eql(u8, last_two, "\n\n")) { | 280 | if (std.mem.eql(u8, last_two, "\n\n")) { |
| ... | @@ -276,6 +282,8 @@ pub const File = struct { | ... | @@ -276,6 +282,8 @@ pub const File = struct { |
| 276 | } | 282 | } |
| 277 | } | 283 | } |
| 278 | try writer.writeAll(self.main.items); | 284 | try writer.writeAll(self.main.items); |
| 285 | self.file.?.close(); | ||
| 286 | self.file = null; | ||
| 279 | } | 287 | } |
| 280 | }; | 288 | }; |
| 281 | 289 |
src-self-hosted/main.zig+4-1| ... | @@ -433,7 +433,10 @@ fn buildOutputType( | ... | @@ -433,7 +433,10 @@ fn buildOutputType( |
| 433 | std.debug.print("-fno-emit-bin not supported yet", .{}); | 433 | std.debug.print("-fno-emit-bin not supported yet", .{}); |
| 434 | process.exit(1); | 434 | process.exit(1); |
| 435 | }, | 435 | }, |
| 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), | ||
| 437 | 440 | ||
| 438 | .yes => |p| p, | 441 | .yes => |p| p, |
| 439 | }; | 442 | }; |
src-self-hosted/test.zig+2-3| ... | @@ -480,9 +480,8 @@ pub const TestContext = struct { | ... | @@ -480,9 +480,8 @@ pub const TestContext = struct { |
| 480 | switch (update.case) { | 480 | switch (update.case) { |
| 481 | .Transformation => |expected_output| { | 481 | .Transformation => |expected_output| { |
| 482 | if (case.cbe) { | 482 | if (case.cbe) { |
| 483 | var cfile: *link.File.C = module.bin_file.cast(link.File.C).?; | 483 | // The C file is always closed after an update, because we don't support |
| 484 | cfile.file.?.close(); | 484 | // incremental updates |
| 485 | cfile.file = null; | ||
| 486 | var file = try tmp.dir.openFile(bin_name, .{ .read = true }); | 485 | var file = try tmp.dir.openFile(bin_name, .{ .read = true }); |
| 487 | defer file.close(); | 486 | defer file.close(); |
| 488 | var out = file.reader().readAllAlloc(allocator, 1024 * 1024) catch @panic("Unable to read C output!"); | 487 | 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 { | ... | @@ -32,6 +32,7 @@ pub fn addCases(ctx: *TestContext) !void { |
| 32 | \\ | 32 | \\ |
| 33 | ); | 33 | ); |
| 34 | // TODO: implement return values | 34 | // TODO: implement return values |
| 35 | // TODO: figure out a way to prevent asm constants from being generated | ||
| 35 | ctx.c("inline asm", linux_x64, | 36 | ctx.c("inline asm", linux_x64, |
| 36 | \\fn exitGood() void { | 37 | \\fn exitGood() void { |
| 37 | \\	asm volatile ("syscall" | 38 | \\	asm volatile ("syscall" |
| ... | @@ -49,6 +50,10 @@ pub fn addCases(ctx: *TestContext) !void { | ... | @@ -49,6 +50,10 @@ pub fn addCases(ctx: *TestContext) !void { |
| 49 | \\ | 50 | \\ |
| 50 | \\void exitGood(void); | 51 | \\void exitGood(void); |
| 51 | \\ | 52 | \\ |
| 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 | \\ | ||
| 52 | \\noreturn void _start(void) { | 57 | \\noreturn void _start(void) { |
| 53 | \\	exitGood(); | 58 | \\	exitGood(); |
| 54 | \\} | 59 | \\} |
test/stage2/zir.zig+9-9| ... | @@ -22,8 +22,8 @@ pub fn addCases(ctx: *TestContext) !void { | ... | @@ -22,8 +22,8 @@ pub fn addCases(ctx: *TestContext) !void { |
| 22 | , | 22 | , |
| 23 | \\@void = primitive(void) | 23 | \\@void = primitive(void) |
| 24 | \\@fnty = fntype([], @void, cc=C) | 24 | \\@fnty = fntype([], @void, cc=C) |
| 25 | \\@9 = declref("9$0") | 25 | \\@9 = declref("9__anon_0") |
| 26 | \\@9$0 = str("entry") | 26 | \\@9__anon_0 = str("entry") |
| 27 | \\@unnamed$4 = str("entry") | 27 | \\@unnamed$4 = str("entry") |
| 28 | \\@unnamed$5 = export(@unnamed$4, "entry") | 28 | \\@unnamed$5 = export(@unnamed$4, "entry") |
| 29 | \\@unnamed$6 = fntype([], @void, cc=C) | 29 | \\@unnamed$6 = fntype([], @void, cc=C) |
| ... | @@ -77,9 +77,9 @@ pub fn addCases(ctx: *TestContext) !void { | ... | @@ -77,9 +77,9 @@ pub fn addCases(ctx: *TestContext) !void { |
| 77 | \\@entry = fn(@unnamed$6, { | 77 | \\@entry = fn(@unnamed$6, { |
| 78 | \\ %0 = returnvoid() | 78 | \\ %0 = returnvoid() |
| 79 | \\}) | 79 | \\}) |
| 80 | \\@entry$1 = str("2\x08\x01\n") | 80 | \\@entry__anon_1 = str("2\x08\x01\n") |
| 81 | \\@9 = declref("9$0") | 81 | \\@9 = declref("9__anon_0") |
| 82 | \\@9$0 = str("entry") | 82 | \\@9__anon_0 = str("entry") |
| 83 | \\@unnamed$11 = str("entry") | 83 | \\@unnamed$11 = str("entry") |
| 84 | \\@unnamed$12 = export(@unnamed$11, "entry") | 84 | \\@unnamed$12 = export(@unnamed$11, "entry") |
| 85 | \\ | 85 | \\ |
| ... | @@ -111,8 +111,8 @@ pub fn addCases(ctx: *TestContext) !void { | ... | @@ -111,8 +111,8 @@ pub fn addCases(ctx: *TestContext) !void { |
| 111 | , | 111 | , |
| 112 | \\@void = primitive(void) | 112 | \\@void = primitive(void) |
| 113 | \\@fnty = fntype([], @void, cc=C) | 113 | \\@fnty = fntype([], @void, cc=C) |
| 114 | \\@9 = declref("9$0") | 114 | \\@9 = declref("9__anon_0") |
| 115 | \\@9$0 = str("entry") | 115 | \\@9__anon_0 = str("entry") |
| 116 | \\@unnamed$4 = str("entry") | 116 | \\@unnamed$4 = str("entry") |
| 117 | \\@unnamed$5 = export(@unnamed$4, "entry") | 117 | \\@unnamed$5 = export(@unnamed$4, "entry") |
| 118 | \\@unnamed$6 = fntype([], @void, cc=C) | 118 | \\@unnamed$6 = fntype([], @void, cc=C) |
| ... | @@ -187,8 +187,8 @@ pub fn addCases(ctx: *TestContext) !void { | ... | @@ -187,8 +187,8 @@ pub fn addCases(ctx: *TestContext) !void { |
| 187 | , | 187 | , |
| 188 | \\@void = primitive(void) | 188 | \\@void = primitive(void) |
| 189 | \\@fnty = fntype([], @void, cc=C) | 189 | \\@fnty = fntype([], @void, cc=C) |
| 190 | \\@9 = declref("9$2") | 190 | \\@9 = declref("9__anon_2") |
| 191 | \\@9$2 = str("entry") | 191 | \\@9__anon_2 = str("entry") |
| 192 | \\@unnamed$4 = str("entry") | 192 | \\@unnamed$4 = str("entry") |
| 193 | \\@unnamed$5 = export(@unnamed$4, "entry") | 193 | \\@unnamed$5 = export(@unnamed$4, "entry") |
| 194 | \\@unnamed$6 = fntype([], @void, cc=C) | 194 | \\@unnamed$6 = fntype([], @void, cc=C) |