authorgravatar for noam@pixelhero.devNoam Preil <noam@pixelhero.dev> 2020-07-07 17:51:59-04:00
committergravatar for noam@pixelhero.devNoam Preil <noam@pixelhero.dev> 2020-07-07 17:51:59-04:00
log6ece36a051f5cf2bd2329d3f6512a7e9d55486a3
tree89308f01b6a0ef341647175ecc761024d76e9477
parent2f28ecf946b566f40e648145af18d207d0c5770d
signature Commit is signed but in an unrecognized format.

Working translation of empty function


3 files changed, 46 insertions(+), 3 deletions(-)

src-self-hosted/cgen.zig+40-1
......@@ -1,11 +1,50 @@
11const link = @import("link.zig");
22const Module = @import("Module.zig");
3const std = @import("std");
4const Value = @import("value.zig").Value;
35
46const C = link.File.C;
57const Decl = Module.Decl;
68const CStandard = Module.CStandard;
9const mem = std.mem;
10
11/// Maps a name from Zig source to C. This will always give the same output for
12/// any given input.
13fn map(name: []const u8) ![]const u8 {
14 return name;
15}
716
817pub fn generate(file: *C, decl: *Decl, standard: CStandard) !void {
918 const writer = file.file.?.writer();
10 try writer.print("Generating decl '{}', targeting {}", .{ decl.name, @tagName(standard) });
19 const tv = decl.typed_value.most_recent.typed_value;
20 switch (tv.ty.zigTypeTag()) {
21 .Fn => {
22 const return_type = tv.ty.fnReturnType();
23 switch (return_type.zigTypeTag()) {
24 .NoReturn => try writer.writeAll("_Noreturn void "),
25 else => return error.Unimplemented,
26 }
27
28 const name = try map(mem.spanZ(decl.name));
29 try writer.print("{} (", .{name});
30 if (tv.ty.fnParamLen() == 0) {
31 try writer.writeAll("void){");
32 } else {
33 return error.Unimplemented;
34 }
35
36 const func: *Module.Fn = tv.val.cast(Value.Payload.Function).?.func;
37 const instructions = func.analysis.success.instructions;
38 if (instructions.len > 0) {
39 try writer.writeAll("\n\t");
40 for (instructions) |inst| {
41 std.debug.warn("\nTranslating {}\n", .{inst.*});
42 }
43 try writer.writeAll("\n");
44 }
45
46 try writer.writeAll("}\n");
47 },
48 else => return error.Unimplemented,
49 }
1150}
src-self-hosted/test.zig+1
......@@ -482,6 +482,7 @@ pub const TestContext = struct {
482482 label = @tagName(cstd);
483483 var c: *link.File.C = module.bin_file.cast(link.File.C).?;
484484 c.file.?.close();
485 c.file = null;
485486 var file = try tmp.dir.openFile(bin_name, .{ .read = true });
486487 defer file.close();
487488 var out = file.reader().readAllAlloc(allocator, 1024 * 1024) catch @panic("Unable to read C output!");
test/stage2/cbe.zig+5-2
......@@ -11,8 +11,11 @@ const linux_x64 = std.zig.CrossTarget{
1111pub fn addCases(ctx: *TestContext) !void {
1212 // These tests should work on every platform
1313 ctx.c11("empty start function", linux_x64,
14 \\export fn start() void {}
14 \\export fn _start() noreturn {}
1515 ,
16 \\void start(void) {}
16 // A newline is always generated after every function; this ensures, among
17 // other things, that there is always a newline at the end of the file
18 \\_Noreturn void _start(void) {}
19 \\
1720 );
1821}