diff options
| author | Timon Kruiper <timonkruiper@gmail.com> | 2021-04-05 22:50:54 +0200 |
|---|---|---|
| committer | Timon Kruiper <timonkruiper@gmail.com> | 2021-04-08 14:23:18 +0200 |
| commit | ac14b52e85f857f7f70846d22ea18ea265acb91a (patch) | |
| tree | bceefa15a211ba323fe7645dd69ab6b768f59555 /src/Compilation.zig | |
| parent | e85cd616ef6439fdb9e7ac118251bb7c2296e553 (diff) | |
| download | zig-ac14b52e85f857f7f70846d22ea18ea265acb91a.tar.gz zig-ac14b52e85f857f7f70846d22ea18ea265acb91a.zip | |
stage2: add support for start.zig
This adds a simplified start2.zig that the current stage2 compiler is
able to generate code for.
Diffstat (limited to 'src/Compilation.zig')
| -rw-r--r-- | src/Compilation.zig | 70 |
1 files changed, 40 insertions, 30 deletions
diff --git a/src/Compilation.zig b/src/Compilation.zig index c0409fcb3b..2a3d9659b0 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -908,41 +908,45 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation { }; const builtin_pkg = try Package.create(gpa, zig_cache_artifact_directory.path.?, "builtin2.zig"); + + const std_dir_path = try options.zig_lib_directory.join(gpa, &[_][]const u8{"std"}); + defer gpa.free(std_dir_path); + const start_pkg = try Package.create(gpa, std_dir_path, "start2.zig"); + try root_pkg.add(gpa, "builtin", builtin_pkg); try root_pkg.add(gpa, "root", root_pkg); + try start_pkg.add(gpa, "builtin", builtin_pkg); + try start_pkg.add(gpa, "root", root_pkg); + // TODO when we implement serialization and deserialization of incremental compilation metadata, // this is where we would load it. We have open a handle to the directory where // the output either already is, or will be. // However we currently do not have serialization of such metadata, so for now // we set up an empty Module that does the entire compilation fresh. - const root_scope = rs: { - if (mem.endsWith(u8, root_pkg.root_src_path, ".zig")) { - const root_scope = try gpa.create(Module.Scope.File); - const struct_ty = try Type.Tag.empty_struct.create( - gpa, - &root_scope.root_container, - ); - root_scope.* = .{ - // TODO this is duped so it can be freed in Container.deinit - .sub_file_path = try gpa.dupe(u8, root_pkg.root_src_path), - .source = .{ .unloaded = {} }, - .tree = undefined, - .status = .never_loaded, - .pkg = root_pkg, - .root_container = .{ - .file_scope = root_scope, - .decls = .{}, - .ty = struct_ty, - }, - }; - break :rs root_scope; - } else if (mem.endsWith(u8, root_pkg.root_src_path, ".zir")) { - return error.ZirFilesUnsupported; - } else { - unreachable; - } + if (mem.endsWith(u8, root_pkg.root_src_path, ".zir")) return error.ZirFilesUnsupported; + + const start_scope = ss: { + const start_scope = try gpa.create(Module.Scope.File); + const struct_ty = try Type.Tag.empty_struct.create( + gpa, + &start_scope.root_container, + ); + start_scope.* = .{ + // TODO this is duped so it can be freed in Container.deinit + .sub_file_path = try gpa.dupe(u8, start_pkg.root_src_path), + .source = .{ .unloaded = {} }, + .tree = undefined, + .status = .never_loaded, + .pkg = start_pkg, + .root_container = .{ + .file_scope = start_scope, + .decls = .{}, + .ty = struct_ty, + }, + }; + break :ss start_scope; }; const module = try arena.create(Module); @@ -951,7 +955,9 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation { .gpa = gpa, .comp = comp, .root_pkg = root_pkg, - .root_scope = root_scope, + .root_scope = null, + .start_pkg = start_pkg, + .start_scope = start_scope, .zig_cache_artifact_directory = zig_cache_artifact_directory, .emit_h = options.emit_h, .error_name_list = try std.ArrayListUnmanaged([]const u8).initCapacity(gpa, 1), @@ -1353,9 +1359,9 @@ pub fn update(self: *Compilation) !void { // TODO Detect which source files changed. // Until then we simulate a full cache miss. Source files could have been loaded // for any reason; to force a refresh we unload now. - module.unloadFile(module.root_scope); + module.unloadFile(module.start_scope); module.failed_root_src_file = null; - module.analyzeContainer(&module.root_scope.root_container) catch |err| switch (err) { + module.analyzeContainer(&module.start_scope.root_container) catch |err| switch (err) { error.AnalysisFail => { assert(self.totalErrorCount() != 0); }, @@ -1416,7 +1422,7 @@ pub fn update(self: *Compilation) !void { // to report error messages. Otherwise we unload all source files to save memory. if (self.totalErrorCount() == 0 and !self.keep_source_files_loaded) { if (self.bin_file.options.module) |module| { - module.root_scope.unload(self.gpa); + module.start_scope.unload(self.gpa); } } } @@ -2851,11 +2857,15 @@ fn generateBuiltin2ZigSource(comp: *Compilation, allocator: *Allocator) ![]u8 { \\pub const link_libc = {}; \\pub const arch = {}; \\pub const os = {}; + \\pub const output_mode = {}; + \\pub const object_format = {}; \\ , .{ comp.bin_file.options.link_libc, @enumToInt(target.cpu.arch), @enumToInt(target.os.tag), + @enumToInt(comp.bin_file.options.output_mode), + @enumToInt(comp.bin_file.options.object_format), }); return buffer.toOwnedSlice(); |
