| ... | @@ -1,11 +1,50 @@ | ... | @@ -1,11 +1,50 @@ |
| 1 | const link = @import("link.zig"); | 1 | const link = @import("link.zig"); |
| 2 | const Module = @import("Module.zig"); | 2 | const Module = @import("Module.zig"); |
| | 3 | const std = @import("std"); |
| | 4 | const Value = @import("value.zig").Value; |
| 3 | | 5 | |
| 4 | const C = link.File.C; | 6 | const C = link.File.C; |
| 5 | const Decl = Module.Decl; | 7 | const Decl = Module.Decl; |
| 6 | const CStandard = Module.CStandard; | 8 | const CStandard = Module.CStandard; |
| | 9 | const 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. |
| | 13 | fn map(name: []const u8) ![]const u8 { |
| | 14 | return name; |
| | 15 | } |
| 7 | | 16 | |
| 8 | pub fn generate(file: *C, decl: *Decl, standard: CStandard) !void { | 17 | pub fn generate(file: *C, decl: *Decl, standard: CStandard) !void { |
| 9 | const writer = file.file.?.writer(); | 18 | 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 | } |
| 11 | } | 50 | } |