authorgravatar for timonkruiper@gmail.comTimon Kruiper <timonkruiper@gmail.com> 2021-04-05 20:44:33+02:00
committergravatar for timonkruiper@gmail.comTimon Kruiper <timonkruiper@gmail.com> 2021-04-08 14:23:17+02:00
logfb16cb9183bfdf5db9448666803943127802317a
treefd918e958927e398c5c9b490246c9fe9f77797bb
parent91e416bbf049b875d090ba050081d9964ed4b452

stage2: add initial support for builtin pkg

We currently emit a simplified builtin2.zig that is able to be parsed and correctly interpreted in stage2.

1 files changed, 28 insertions(+), 0 deletions(-)

src/Compilation.zig+28
...@@ -906,6 +906,9 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {...@@ -906,6 +906,9 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
906 artifact_sub_dir,906 artifact_sub_dir,
907 };907 };
908908
909 const builtin_pkg = try Package.create(gpa, zig_cache_artifact_directory.path.?, "builtin2.zig");
910 try root_pkg.add(gpa, "builtin", builtin_pkg);
911
909 // TODO when we implement serialization and deserialization of incremental compilation metadata,912 // TODO when we implement serialization and deserialization of incremental compilation metadata,
910 // this is where we would load it. We have open a handle to the directory where913 // this is where we would load it. We have open a handle to the directory where
911 // the output either already is, or will be.914 // the output either already is, or will be.
...@@ -2822,6 +2825,11 @@ fn updateBuiltinZigFile(comp: *Compilation, mod: *Module) !void {...@@ -2822,6 +2825,11 @@ fn updateBuiltinZigFile(comp: *Compilation, mod: *Module) !void {
2822 const source = try comp.generateBuiltinZigSource(comp.gpa);2825 const source = try comp.generateBuiltinZigSource(comp.gpa);
2823 defer comp.gpa.free(source);2826 defer comp.gpa.free(source);
2824 try mod.zig_cache_artifact_directory.handle.writeFile("builtin.zig", source);2827 try mod.zig_cache_artifact_directory.handle.writeFile("builtin.zig", source);
2828
2829 // FIXME: Remove builtin2.zig when stage2 can correctly generate code for builtin.zig!
2830 const source2 = try comp.generateBuiltin2ZigSource(comp.gpa);
2831 defer comp.gpa.free(source2);
2832 try mod.zig_cache_artifact_directory.handle.writeFile("builtin2.zig", source2);
2825}2833}
28262834
2827pub fn dump_argv(argv: []const []const u8) void {2835pub fn dump_argv(argv: []const []const u8) void {
...@@ -2831,6 +2839,26 @@ pub fn dump_argv(argv: []const []const u8) void {...@@ -2831,6 +2839,26 @@ pub fn dump_argv(argv: []const []const u8) void {
2831 std.debug.print("{s}\n", .{argv[argv.len - 1]});2839 std.debug.print("{s}\n", .{argv[argv.len - 1]});
2832}2840}
28332841
2842fn generateBuiltin2ZigSource(comp: *Compilation, allocator: *Allocator) ![]u8 {
2843 var buffer = std.ArrayList(u8).init(allocator);
2844 defer buffer.deinit();
2845
2846 const target = comp.getTarget();
2847
2848 try buffer.writer().print(
2849 \\pub const link_libc = {};
2850 \\pub const arch = {};
2851 \\pub const os = {};
2852 \\
2853 , .{
2854 comp.bin_file.options.link_libc,
2855 @enumToInt(target.cpu.arch),
2856 @enumToInt(target.os.tag),
2857 });
2858
2859 return buffer.toOwnedSlice();
2860}
2861
2834pub fn generateBuiltinZigSource(comp: *Compilation, allocator: *Allocator) ![]u8 {2862pub fn generateBuiltinZigSource(comp: *Compilation, allocator: *Allocator) ![]u8 {
2835 const tracy = trace(@src());2863 const tracy = trace(@src());
2836 defer tracy.end();2864 defer tracy.end();