diff options
| author | Noam Preil <pleasantatk@gmail.com> | 2020-07-07 17:51:59 -0400 |
|---|---|---|
| committer | Noam Preil <pleasantatk@gmail.com> | 2020-07-07 17:51:59 -0400 |
| commit | 6ece36a051f5cf2bd2329d3f6512a7e9d55486a3 (patch) | |
| tree | 89308f01b6a0ef341647175ecc761024d76e9477 | |
| parent | 2f28ecf946b566f40e648145af18d207d0c5770d (diff) | |
| download | zig-6ece36a051f5cf2bd2329d3f6512a7e9d55486a3.tar.gz zig-6ece36a051f5cf2bd2329d3f6512a7e9d55486a3.zip | |
Working translation of empty function
| -rw-r--r-- | src-self-hosted/cgen.zig | 41 | ||||
| -rw-r--r-- | src-self-hosted/test.zig | 1 | ||||
| -rw-r--r-- | test/stage2/cbe.zig | 7 |
3 files changed, 46 insertions, 3 deletions
diff --git a/src-self-hosted/cgen.zig b/src-self-hosted/cgen.zig index f06f9df6ac..48b725c988 100644 --- a/src-self-hosted/cgen.zig +++ b/src-self-hosted/cgen.zig @@ -1,11 +1,50 @@ const link = @import("link.zig"); const Module = @import("Module.zig"); +const std = @import("std"); +const Value = @import("value.zig").Value; const C = link.File.C; const Decl = Module.Decl; const CStandard = Module.CStandard; +const mem = std.mem; + +/// Maps a name from Zig source to C. This will always give the same output for +/// any given input. +fn map(name: []const u8) ![]const u8 { + return name; +} pub fn generate(file: *C, decl: *Decl, standard: CStandard) !void { const writer = file.file.?.writer(); - try writer.print("Generating decl '{}', targeting {}", .{ decl.name, @tagName(standard) }); + const tv = decl.typed_value.most_recent.typed_value; + switch (tv.ty.zigTypeTag()) { + .Fn => { + const return_type = tv.ty.fnReturnType(); + switch (return_type.zigTypeTag()) { + .NoReturn => try writer.writeAll("_Noreturn void "), + else => return error.Unimplemented, + } + + const name = try map(mem.spanZ(decl.name)); + try writer.print("{} (", .{name}); + if (tv.ty.fnParamLen() == 0) { + try writer.writeAll("void){"); + } else { + return error.Unimplemented; + } + + const func: *Module.Fn = tv.val.cast(Value.Payload.Function).?.func; + const instructions = func.analysis.success.instructions; + if (instructions.len > 0) { + try writer.writeAll("\n\t"); + for (instructions) |inst| { + std.debug.warn("\nTranslating {}\n", .{inst.*}); + } + try writer.writeAll("\n"); + } + + try writer.writeAll("}\n"); + }, + else => return error.Unimplemented, + } } diff --git a/src-self-hosted/test.zig b/src-self-hosted/test.zig index 153c77ce47..9e7a7bbf50 100644 --- a/src-self-hosted/test.zig +++ b/src-self-hosted/test.zig @@ -482,6 +482,7 @@ pub const TestContext = struct { label = @tagName(cstd); var c: *link.File.C = module.bin_file.cast(link.File.C).?; c.file.?.close(); + c.file = null; var file = try tmp.dir.openFile(bin_name, .{ .read = true }); defer file.close(); var out = file.reader().readAllAlloc(allocator, 1024 * 1024) catch @panic("Unable to read C output!"); diff --git a/test/stage2/cbe.zig b/test/stage2/cbe.zig index a599367aec..86f966af82 100644 --- a/test/stage2/cbe.zig +++ b/test/stage2/cbe.zig @@ -11,8 +11,11 @@ const linux_x64 = std.zig.CrossTarget{ pub fn addCases(ctx: *TestContext) !void { // These tests should work on every platform ctx.c11("empty start function", linux_x64, - \\export fn start() void {} + \\export fn _start() noreturn {} , - \\void start(void) {} + // A newline is always generated after every function; this ensures, among + // other things, that there is always a newline at the end of the file + \\_Noreturn void _start(void) {} + \\ ); } |
