authorgravatar for timonkruiper@gmail.comTimon Kruiper <timonkruiper@gmail.com> 2021-04-05 22:50:54+02:00
committergravatar for timonkruiper@gmail.comTimon Kruiper <timonkruiper@gmail.com> 2021-04-08 14:23:18+02:00
logac14b52e85f857f7f70846d22ea18ea265acb91a
treebceefa15a211ba323fe7645dd69ab6b768f59555
parente85cd616ef6439fdb9e7ac118251bb7c2296e553

stage2: add support for start.zig

This adds a simplified start2.zig that the current stage2 compiler is able to generate code for.

6 files changed, 136 insertions(+), 34 deletions(-)

lib/std/start2.zig created+58
......@@ -0,0 +1,58 @@
1const root = @import("root");
2const builtin = @import("builtin");
3
4comptime {
5 if (builtin.output_mode == 0) { // OutputMode.Exe
6 if (builtin.link_libc or builtin.object_format == 5) { // ObjectFormat.c
7 if (!@hasDecl(root, "main")) {
8 @export(otherMain, "main");
9 }
10 } else {
11 if (!@hasDecl(root, "_start")) {
12 @export(otherStart, "_start");
13 }
14 }
15 }
16}
17
18// FIXME: Cannot call this function `main`, because `fully qualified names`
19// have not been implemented yet.
20fn otherMain() callconv(.C) c_int {
21 root.zigMain();
22 return 0;
23}
24
25// FIXME: Cannot call this function `_start`, because `fully qualified names`
26// have not been implemented yet.
27fn otherStart() callconv(.Naked) noreturn {
28 root.zigMain();
29 otherExit();
30}
31
32// FIXME: Cannot call this function `exit`, because `fully qualified names`
33// have not been implemented yet.
34fn otherExit() noreturn {
35 if (builtin.arch == 31) { // x86_64
36 asm volatile ("syscall"
37 :
38 : [number] "{rax}" (231),
39 [arg1] "{rdi}" (0)
40 : "rcx", "r11", "memory"
41 );
42 } else if (builtin.arch == 0) { // arm
43 asm volatile ("svc #0"
44 :
45 : [number] "{r7}" (1),
46 [arg1] "{r0}" (0)
47 : "memory"
48 );
49 } else if (builtin.arch == 2) { // aarch64
50 asm volatile ("svc #0"
51 :
52 : [number] "{x8}" (93),
53 [arg1] "{x0}" (0)
54 : "memory", "cc"
55 );
56 } else @compileError("not yet supported!");
57 unreachable;
58}
src/Compilation.zig+40-30
......@@ -908,41 +908,45 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
908908 };
909909
910910 const builtin_pkg = try Package.create(gpa, zig_cache_artifact_directory.path.?, "builtin2.zig");
911
912 const std_dir_path = try options.zig_lib_directory.join(gpa, &[_][]const u8{"std"});
913 defer gpa.free(std_dir_path);
914 const start_pkg = try Package.create(gpa, std_dir_path, "start2.zig");
915
911916 try root_pkg.add(gpa, "builtin", builtin_pkg);
912917 try root_pkg.add(gpa, "root", root_pkg);
913918
919 try start_pkg.add(gpa, "builtin", builtin_pkg);
920 try start_pkg.add(gpa, "root", root_pkg);
921
914922 // TODO when we implement serialization and deserialization of incremental compilation metadata,
915923 // this is where we would load it. We have open a handle to the directory where
916924 // the output either already is, or will be.
917925 // However we currently do not have serialization of such metadata, so for now
918926 // we set up an empty Module that does the entire compilation fresh.
919927
920 const root_scope = rs: {
921 if (mem.endsWith(u8, root_pkg.root_src_path, ".zig")) {
922 const root_scope = try gpa.create(Module.Scope.File);
923 const struct_ty = try Type.Tag.empty_struct.create(
924 gpa,
925 &root_scope.root_container,
926 );
927 root_scope.* = .{
928 // TODO this is duped so it can be freed in Container.deinit
929 .sub_file_path = try gpa.dupe(u8, root_pkg.root_src_path),
930 .source = .{ .unloaded = {} },
931 .tree = undefined,
932 .status = .never_loaded,
933 .pkg = root_pkg,
934 .root_container = .{
935 .file_scope = root_scope,
936 .decls = .{},
937 .ty = struct_ty,
938 },
939 };
940 break :rs root_scope;
941 } else if (mem.endsWith(u8, root_pkg.root_src_path, ".zir")) {
942 return error.ZirFilesUnsupported;
943 } else {
944 unreachable;
945 }
928 if (mem.endsWith(u8, root_pkg.root_src_path, ".zir")) return error.ZirFilesUnsupported;
929
930 const start_scope = ss: {
931 const start_scope = try gpa.create(Module.Scope.File);
932 const struct_ty = try Type.Tag.empty_struct.create(
933 gpa,
934 &start_scope.root_container,
935 );
936 start_scope.* = .{
937 // TODO this is duped so it can be freed in Container.deinit
938 .sub_file_path = try gpa.dupe(u8, start_pkg.root_src_path),
939 .source = .{ .unloaded = {} },
940 .tree = undefined,
941 .status = .never_loaded,
942 .pkg = start_pkg,
943 .root_container = .{
944 .file_scope = start_scope,
945 .decls = .{},
946 .ty = struct_ty,
947 },
948 };
949 break :ss start_scope;
946950 };
947951
948952 const module = try arena.create(Module);
......@@ -951,7 +955,9 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
951955 .gpa = gpa,
952956 .comp = comp,
953957 .root_pkg = root_pkg,
954 .root_scope = root_scope,
958 .root_scope = null,
959 .start_pkg = start_pkg,
960 .start_scope = start_scope,
955961 .zig_cache_artifact_directory = zig_cache_artifact_directory,
956962 .emit_h = options.emit_h,
957963 .error_name_list = try std.ArrayListUnmanaged([]const u8).initCapacity(gpa, 1),
......@@ -1353,9 +1359,9 @@ pub fn update(self: *Compilation) !void {
13531359 // TODO Detect which source files changed.
13541360 // Until then we simulate a full cache miss. Source files could have been loaded
13551361 // for any reason; to force a refresh we unload now.
1356 module.unloadFile(module.root_scope);
1362 module.unloadFile(module.start_scope);
13571363 module.failed_root_src_file = null;
1358 module.analyzeContainer(&module.root_scope.root_container) catch |err| switch (err) {
1364 module.analyzeContainer(&module.start_scope.root_container) catch |err| switch (err) {
13591365 error.AnalysisFail => {
13601366 assert(self.totalErrorCount() != 0);
13611367 },
......@@ -1416,7 +1422,7 @@ pub fn update(self: *Compilation) !void {
14161422 // to report error messages. Otherwise we unload all source files to save memory.
14171423 if (self.totalErrorCount() == 0 and !self.keep_source_files_loaded) {
14181424 if (self.bin_file.options.module) |module| {
1419 module.root_scope.unload(self.gpa);
1425 module.start_scope.unload(self.gpa);
14201426 }
14211427 }
14221428}
......@@ -2851,11 +2857,15 @@ fn generateBuiltin2ZigSource(comp: *Compilation, allocator: *Allocator) ![]u8 {
28512857 \\pub const link_libc = {};
28522858 \\pub const arch = {};
28532859 \\pub const os = {};
2860 \\pub const output_mode = {};
2861 \\pub const object_format = {};
28542862 \\
28552863 , .{
28562864 comp.bin_file.options.link_libc,
28572865 @enumToInt(target.cpu.arch),
28582866 @enumToInt(target.os.tag),
2867 @enumToInt(comp.bin_file.options.output_mode),
2868 @enumToInt(comp.bin_file.options.object_format),
28592869 });
28602870
28612871 return buffer.toOwnedSlice();
src/Module.zig+7-2
......@@ -35,8 +35,11 @@ comp: *Compilation,
3535zig_cache_artifact_directory: Compilation.Directory,
3636/// Pointer to externally managed resource. `null` if there is no zig file being compiled.
3737root_pkg: *Package,
38/// This is populated when `@import("root")` is analysed.
39root_scope: ?*Scope.File,
40start_pkg: *Package,
3841/// Module owns this resource.
39root_scope: *Scope.File,
42start_scope: *Scope.File,
4043/// It's rare for a decl to be exported, so we save memory by having a sparse map of
4144/// Decl pointers to details about them being exported.
4245/// The Export memory is owned by the `export_owners` table; the slice itself is owned by this table.
......@@ -2341,7 +2344,9 @@ pub fn deinit(mod: *Module) void {
23412344 mod.export_owners.deinit(gpa);
23422345
23432346 mod.symbol_exports.deinit(gpa);
2344 mod.root_scope.destroy(gpa);
2347
2348 mod.start_scope.destroy(gpa);
2349 mod.start_pkg.destroy(gpa);
23452350
23462351 var it = mod.global_error_set.iterator();
23472352 while (it.next()) |entry| {
src/Package.zig+25
......@@ -15,6 +15,9 @@ root_src_path: []const u8,
1515table: Table = .{},
1616parent: ?*Package = null,
1717
18// Used when freeing packages
19seen: bool = false,
20
1821/// Allocate a Package. No references to the slices passed are kept.
1922pub fn create(
2023 gpa: *Allocator,
......@@ -55,6 +58,14 @@ pub fn destroy(pkg: *Package, gpa: *Allocator) void {
5558 pkg.root_src_directory.handle.close();
5659 }
5760
61 // First we recurse into all the packages and remove packages from the tables
62 // once we have seen it before. We do this to make sure that that
63 // a package can only be found once in the whole tree.
64 if (!pkg.seen) {
65 pkg.seen = true;
66 pkg.markSeen(gpa);
67 }
68
5869 {
5970 var it = pkg.table.iterator();
6071 while (it.next()) |kv| {
......@@ -69,6 +80,20 @@ pub fn destroy(pkg: *Package, gpa: *Allocator) void {
6980 gpa.destroy(pkg);
7081}
7182
83fn markSeen(pkg: *Package, gpa: *Allocator) void {
84 var it = pkg.table.iterator();
85 while (it.next()) |kv| {
86 if (pkg != kv.value) {
87 if (kv.value.seen) {
88 pkg.table.removeAssertDiscard(kv.key);
89 } else {
90 kv.value.seen = true;
91 kv.value.markSeen(gpa);
92 }
93 }
94 }
95}
96
7297pub fn add(pkg: *Package, gpa: *Allocator, name: []const u8, package: *Package) !void {
7398 try pkg.table.ensureCapacity(gpa, pkg.table.count() + 1);
7499 const name_dupe = try mem.dupe(gpa, u8, name);
src/Sema.zig+4-1
......@@ -4699,7 +4699,7 @@ fn namedFieldPtr(
46994699 }
47004700
47014701 // TODO this will give false positives for structs inside the root file
4702 if (container_scope.file_scope == mod.root_scope) {
4702 if (container_scope.file_scope == mod.root_scope.?) {
47034703 return mod.fail(
47044704 &block.base,
47054705 src,
......@@ -5338,6 +5338,9 @@ fn analyzeImport(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, target_strin
53385338 .ty = struct_ty,
53395339 },
53405340 };
5341 if (mem.eql(u8, target_string, "root")) {
5342 sema.mod.root_scope = file_scope;
5343 }
53415344 sema.mod.analyzeContainer(&file_scope.root_container) catch |err| switch (err) {
53425345 error.AnalysisFail => {
53435346 assert(sema.mod.comp.totalErrorCount() != 0);
src/main.zig+2-1
......@@ -1732,6 +1732,8 @@ fn buildOutputType(
17321732 },
17331733 }
17341734
1735 // This gets cleaned up, because root_pkg becomes part of the
1736 // package table of the start_pkg.
17351737 const root_pkg: ?*Package = if (root_src_file) |src_path| blk: {
17361738 if (main_pkg_path) |p| {
17371739 const rel_src_path = try fs.path.relative(gpa, p, src_path);
......@@ -1741,7 +1743,6 @@ fn buildOutputType(
17411743 break :blk try Package.create(gpa, fs.path.dirname(src_path), fs.path.basename(src_path));
17421744 }
17431745 } else null;
1744 defer if (root_pkg) |p| p.destroy(gpa);
17451746
17461747 // Transfer packages added with --pkg-begin/--pkg-end to the root package
17471748 if (root_pkg) |pkg| {