authorgravatar for noam@pixelhero.devNoam Preil <noam@pixelhero.dev> 2020-07-08 14:05:07-04:00
committergravatar for noam@pixelhero.devNoam Preil <noam@pixelhero.dev> 2020-07-08 14:05:07-04:00
log6b4863416611253c68539136ae9e4b83359efe74
treeaf65c62efa74a46046a31ccc8cf074813ffb0a03
parent9d92d62525324453694190528021ab0216af0ed5
signature Commit is signed but in an unrecognized format.

CBE: Emit asm decls for now, but rename to make them valid


5 files changed, 38 insertions(+), 20 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/cgen.zig+17-10
...@@ -11,8 +11,8 @@ const mem = std.mem;...@@ -11,8 +11,8 @@ const mem = std.mem;
1111
12/// Maps a name from Zig source to C. This will always give the same output for12/// Maps a name from Zig source to C. This will always give the same output for
13/// any given input.13/// any given input.
14fn map(name: []const u8) ![]const u8 {14fn map(allocator: *std.mem.Allocator, name: []const u8) ![]const u8 {
15 return name;15 return allocator.dupe(u8, name);
16}16}
1717
18fn renderType(file: *C, writer: std.ArrayList(u8).Writer, T: Type, src: usize) !void {18fn 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) !
34fn renderFunctionSignature(file: *C, writer: std.ArrayList(u8).Writer, decl: *Decl) !void {34fn 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 emitted148 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+6
...@@ -93,6 +93,7 @@ pub fn openCFile(allocator: *Allocator, file: fs.File, options: Options) !File.C...@@ -93,6 +93,7 @@ pub fn openCFile(allocator: *Allocator, file: fs.File, options: Options) !File.C
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 {
220221
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")) {
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 values34 // 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)