| author | |
| committer | |
| log | 6b4863416611253c68539136ae9e4b83359efe74 |
| tree | af65c62efa74a46046a31ccc8cf074813ffb0a03 |
| parent | 9d92d62525324453694190528021ab0216af0ed5 |
| signature | Commit is signed but in an unrecognized format. |
5 files changed, 38 insertions(+), 20 deletions(-)
src-self-hosted/Module.zig+1-1| ... | ... | @@ -2456,7 +2456,7 @@ fn createAnonymousDecl( |
| 2456 | 2456 | ) !*Decl { |
| 2457 | 2457 | const name_index = self.getNextAnonNameIndex(); |
| 2458 | 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 | 2460 | defer self.allocator.free(name); |
| 2461 | 2461 | const name_hash = scope.namespace().fullyQualifiedNameHash(name); |
| 2462 | 2462 | const src_hash: std.zig.SrcHash = undefined; |
src-self-hosted/cgen.zig+17-10| ... | ... | @@ -11,8 +11,8 @@ const mem = std.mem; |
| 11 | 11 | |
| 12 | 12 | /// Maps a name from Zig source to C. This will always give the same output for |
| 13 | 13 | /// any given input. |
| 14 | fn map(name: []const u8) ![]const u8 { | |
| 15 | return name; | |
| 14 | fn map(allocator: *std.mem.Allocator, name: []const u8) ![]const u8 { | |
| 15 | return allocator.dupe(u8, name); | |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 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 | 34 | fn renderFunctionSignature(file: *C, writer: std.ArrayList(u8).Writer, decl: *Decl) !void { |
| 35 | 35 | const tv = decl.typed_value.most_recent.typed_value; |
| 36 | 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 | 39 | try writer.print(" {}(", .{name}); |
| 39 | 40 | if (tv.ty.fnParamLen() == 0) { |
| 40 | 41 | try writer.writeAll("void)"); |
| ... | ... | @@ -143,15 +144,21 @@ pub fn generate(file: *C, decl: *Decl) !void { |
| 143 | 144 | try writer.writeAll("}\n\n"); |
| 144 | 145 | }, |
| 145 | 146 | .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 | } | |
| 152 | 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 | 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 | 93 | .options = options, |
| 94 | 94 | .main = std.ArrayList(u8).init(allocator), |
| 95 | 95 | .header = std.ArrayList(u8).init(allocator), |
| 96 | .constants = std.ArrayList(u8).init(allocator), | |
| 96 | 97 | .called = std.StringHashMap(void).init(allocator), |
| 97 | 98 | }; |
| 98 | 99 | } |
| ... | ... | @@ -220,6 +221,7 @@ pub const File = struct { |
| 220 | 221 | |
| 221 | 222 | allocator: *Allocator, |
| 222 | 223 | header: std.ArrayList(u8), |
| 224 | constants: std.ArrayList(u8), | |
| 223 | 225 | main: std.ArrayList(u8), |
| 224 | 226 | file: ?fs.File, |
| 225 | 227 | options: Options, |
| ... | ... | @@ -237,6 +239,7 @@ pub const File = struct { |
| 237 | 239 | pub fn deinit(self: *File.C) void { |
| 238 | 240 | self.main.deinit(); |
| 239 | 241 | self.header.deinit(); |
| 242 | self.constants.deinit(); | |
| 240 | 243 | self.called.deinit(); |
| 241 | 244 | if (self.file) |f| |
| 242 | 245 | f.close(); |
| ... | ... | @@ -269,6 +272,9 @@ pub const File = struct { |
| 269 | 272 | if (self.header.items.len > 0) { |
| 270 | 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 | 278 | if (self.main.items.len > 1) { |
| 273 | 279 | const last_two = self.main.items[self.main.items.len - 2 ..]; |
| 274 | 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 | 32 | \\ |
| 33 | 33 | ); |
| 34 | 34 | // TODO: implement return values |
| 35 | // TODO: figure out a way to prevent asm constants from being generated | |
| 35 | 36 | ctx.c("inline asm", linux_x64, |
| 36 | 37 | \\fn exitGood() void { |
| 37 | 38 | \\	asm volatile ("syscall" |
| ... | ... | @@ -49,6 +50,10 @@ pub fn addCases(ctx: *TestContext) !void { |
| 49 | 50 | \\ |
| 50 | 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 | 57 | \\noreturn void _start(void) { |
| 53 | 58 | \\	exitGood(); |
| 54 | 59 | \\} |
test/stage2/zir.zig+9-9| ... | ... | @@ -22,8 +22,8 @@ pub fn addCases(ctx: *TestContext) !void { |
| 22 | 22 | , |
| 23 | 23 | \\@void = primitive(void) |
| 24 | 24 | \\@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") | |
| 27 | 27 | \\@unnamed$4 = str("entry") |
| 28 | 28 | \\@unnamed$5 = export(@unnamed$4, "entry") |
| 29 | 29 | \\@unnamed$6 = fntype([], @void, cc=C) |
| ... | ... | @@ -77,9 +77,9 @@ pub fn addCases(ctx: *TestContext) !void { |
| 77 | 77 | \\@entry = fn(@unnamed$6, { |
| 78 | 78 | \\ %0 = returnvoid() |
| 79 | 79 | \\}) |
| 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") | |
| 83 | 83 | \\@unnamed$11 = str("entry") |
| 84 | 84 | \\@unnamed$12 = export(@unnamed$11, "entry") |
| 85 | 85 | \\ |
| ... | ... | @@ -111,8 +111,8 @@ pub fn addCases(ctx: *TestContext) !void { |
| 111 | 111 | , |
| 112 | 112 | \\@void = primitive(void) |
| 113 | 113 | \\@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") | |
| 116 | 116 | \\@unnamed$4 = str("entry") |
| 117 | 117 | \\@unnamed$5 = export(@unnamed$4, "entry") |
| 118 | 118 | \\@unnamed$6 = fntype([], @void, cc=C) |
| ... | ... | @@ -187,8 +187,8 @@ pub fn addCases(ctx: *TestContext) !void { |
| 187 | 187 | , |
| 188 | 188 | \\@void = primitive(void) |
| 189 | 189 | \\@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") | |
| 192 | 192 | \\@unnamed$4 = str("entry") |
| 193 | 193 | \\@unnamed$5 = export(@unnamed$4, "entry") |
| 194 | 194 | \\@unnamed$6 = fntype([], @void, cc=C) |