diff options
| author | mlugg <mlugg@mlugg.co.uk> | 2025-02-03 20:10:44 +0000 |
|---|---|---|
| committer | mlugg <mlugg@mlugg.co.uk> | 2025-02-04 16:20:29 +0000 |
| commit | d3ca10d5d8bc92280a14f9e40dc41d6accc1b4c2 (patch) | |
| tree | 20306667045b1136e12399d5fe13612925f0677c /src/main.zig | |
| parent | 3a4bb47fedbb890dc149622e31c75101b14c3b16 (diff) | |
| download | zig-d3ca10d5d8bc92280a14f9e40dc41d6accc1b4c2.tar.gz zig-d3ca10d5d8bc92280a14f9e40dc41d6accc1b4c2.zip | |
Zcu: remove `*_loaded` fields on `File`
Instead, `source`, `tree`, and `zir` should all be optional. This is
precisely what we're actually trying to model here; and `File` isn't
optimized for memory consumption or serializability anyway, so it's fine
to use a couple of extra bytes on actual optionals here.
Diffstat (limited to 'src/main.zig')
| -rw-r--r-- | src/main.zig | 112 |
1 files changed, 49 insertions, 63 deletions
diff --git a/src/main.zig b/src/main.zig index ba5ebf8efd..8d30bef237 100644 --- a/src/main.zig +++ b/src/main.zig @@ -3636,7 +3636,7 @@ fn buildOutputType( if (show_builtin) { const builtin_mod = comp.root_mod.getBuiltinDependency(); - const source = builtin_mod.builtin_file.?.source; + const source = builtin_mod.builtin_file.?.source.?; return std.io.getStdOut().writeAll(source); } switch (listen) { @@ -6135,14 +6135,12 @@ fn cmdAstCheck( var file: Zcu.File = .{ .status = .never_loaded, .prev_status = .never_loaded, - .source_loaded = false, - .tree_loaded = false, - .zir_loaded = false, .sub_file_path = undefined, - .source = undefined, .stat = undefined, - .tree = undefined, - .zir = undefined, + .source = null, + .tree = null, + .zir = null, + .zoir = null, .mod = undefined, }; if (zig_source_file) |file_name| { @@ -6163,7 +6161,6 @@ fn cmdAstCheck( file.sub_file_path = file_name; file.source = source; - file.source_loaded = true; file.stat = .{ .size = stat.size, .inode = stat.inode, @@ -6176,7 +6173,6 @@ fn cmdAstCheck( }; file.sub_file_path = "<stdin>"; file.source = source; - file.source_loaded = true; file.stat.size = source.len; } @@ -6196,17 +6192,15 @@ fn cmdAstCheck( .fully_qualified_name = "root", }); - file.tree = try Ast.parse(gpa, file.source, mode); - file.tree_loaded = true; - defer file.tree.deinit(gpa); + file.tree = try Ast.parse(gpa, file.source.?, mode); + defer file.tree.?.deinit(gpa); switch (mode) { .zig => { - file.zir = try AstGen.generate(gpa, file.tree); - file.zir_loaded = true; - defer file.zir.deinit(gpa); + file.zir = try AstGen.generate(gpa, file.tree.?); + defer file.zir.?.deinit(gpa); - if (file.zir.hasCompileErrors()) { + if (file.zir.?.hasCompileErrors()) { var wip_errors: std.zig.ErrorBundle.Wip = undefined; try wip_errors.init(gpa); defer wip_errors.deinit(); @@ -6215,13 +6209,13 @@ fn cmdAstCheck( defer error_bundle.deinit(gpa); error_bundle.renderToStdErr(color.renderOptions()); - if (file.zir.loweringFailed()) { + if (file.zir.?.loweringFailed()) { process.exit(1); } } if (!want_output_text) { - if (file.zir.hasCompileErrors()) { + if (file.zir.?.hasCompileErrors()) { process.exit(1); } else { return cleanExit(); @@ -6233,18 +6227,18 @@ fn cmdAstCheck( { const token_bytes = @sizeOf(Ast.TokenList) + - file.tree.tokens.len * (@sizeOf(std.zig.Token.Tag) + @sizeOf(Ast.ByteOffset)); - const tree_bytes = @sizeOf(Ast) + file.tree.nodes.len * + file.tree.?.tokens.len * (@sizeOf(std.zig.Token.Tag) + @sizeOf(Ast.ByteOffset)); + const tree_bytes = @sizeOf(Ast) + file.tree.?.nodes.len * (@sizeOf(Ast.Node.Tag) + @sizeOf(Ast.Node.Data) + @sizeOf(Ast.TokenIndex)); - const instruction_bytes = file.zir.instructions.len * + const instruction_bytes = file.zir.?.instructions.len * // Here we don't use @sizeOf(Zir.Inst.Data) because it would include // the debug safety tag but we want to measure release size. (@sizeOf(Zir.Inst.Tag) + 8); - const extra_bytes = file.zir.extra.len * @sizeOf(u32); + const extra_bytes = file.zir.?.extra.len * @sizeOf(u32); const total_bytes = @sizeOf(Zir) + instruction_bytes + extra_bytes + - file.zir.string_bytes.len * @sizeOf(u8); + file.zir.?.string_bytes.len * @sizeOf(u8); const stdout = io.getStdOut(); const fmtIntSizeBin = std.fmt.fmtIntSizeBin; // zig fmt: off @@ -6258,27 +6252,27 @@ fn cmdAstCheck( \\# Extra Data Items: {d} ({}) \\ , .{ - fmtIntSizeBin(file.source.len), - file.tree.tokens.len, fmtIntSizeBin(token_bytes), - file.tree.nodes.len, fmtIntSizeBin(tree_bytes), + fmtIntSizeBin(file.source.?.len), + file.tree.?.tokens.len, fmtIntSizeBin(token_bytes), + file.tree.?.nodes.len, fmtIntSizeBin(tree_bytes), fmtIntSizeBin(total_bytes), - file.zir.instructions.len, fmtIntSizeBin(instruction_bytes), - fmtIntSizeBin(file.zir.string_bytes.len), - file.zir.extra.len, fmtIntSizeBin(extra_bytes), + file.zir.?.instructions.len, fmtIntSizeBin(instruction_bytes), + fmtIntSizeBin(file.zir.?.string_bytes.len), + file.zir.?.extra.len, fmtIntSizeBin(extra_bytes), }); // zig fmt: on } try @import("print_zir.zig").renderAsTextToFile(gpa, &file, io.getStdOut()); - if (file.zir.hasCompileErrors()) { + if (file.zir.?.hasCompileErrors()) { process.exit(1); } else { return cleanExit(); } }, .zon => { - const zoir = try ZonGen.generate(gpa, file.tree, .{}); + const zoir = try ZonGen.generate(gpa, file.tree.?, .{}); defer zoir.deinit(gpa); if (zoir.hasCompileErrors()) { @@ -6289,7 +6283,7 @@ fn cmdAstCheck( { const src_path = try file.fullPath(gpa); defer gpa.free(src_path); - try wip_errors.addZoirErrorMessages(zoir, file.tree, file.source, src_path); + try wip_errors.addZoirErrorMessages(zoir, file.tree.?, file.source.?, src_path); } var error_bundle = try wip_errors.toOwnedBundle(""); @@ -6519,26 +6513,24 @@ fn cmdDumpZir( var file: Zcu.File = .{ .status = .never_loaded, .prev_status = .never_loaded, - .source_loaded = false, - .tree_loaded = false, - .zir_loaded = true, .sub_file_path = undefined, - .source = undefined, .stat = undefined, - .tree = undefined, + .source = null, + .tree = null, .zir = try Zcu.loadZirCache(gpa, f), + .zoir = null, .mod = undefined, }; - defer file.zir.deinit(gpa); + defer file.zir.?.deinit(gpa); { - const instruction_bytes = file.zir.instructions.len * + const instruction_bytes = file.zir.?.instructions.len * // Here we don't use @sizeOf(Zir.Inst.Data) because it would include // the debug safety tag but we want to measure release size. (@sizeOf(Zir.Inst.Tag) + 8); - const extra_bytes = file.zir.extra.len * @sizeOf(u32); + const extra_bytes = file.zir.?.extra.len * @sizeOf(u32); const total_bytes = @sizeOf(Zir) + instruction_bytes + extra_bytes + - file.zir.string_bytes.len * @sizeOf(u8); + file.zir.?.string_bytes.len * @sizeOf(u8); const stdout = io.getStdOut(); const fmtIntSizeBin = std.fmt.fmtIntSizeBin; // zig fmt: off @@ -6550,9 +6542,9 @@ fn cmdDumpZir( \\ , .{ fmtIntSizeBin(total_bytes), - file.zir.instructions.len, fmtIntSizeBin(instruction_bytes), - fmtIntSizeBin(file.zir.string_bytes.len), - file.zir.extra.len, fmtIntSizeBin(extra_bytes), + file.zir.?.instructions.len, fmtIntSizeBin(instruction_bytes), + fmtIntSizeBin(file.zir.?.string_bytes.len), + file.zir.?.extra.len, fmtIntSizeBin(extra_bytes), }); // zig fmt: on } @@ -6587,18 +6579,16 @@ fn cmdChangelist( var file: Zcu.File = .{ .status = .never_loaded, .prev_status = .never_loaded, - .source_loaded = false, - .tree_loaded = false, - .zir_loaded = false, .sub_file_path = old_source_file, - .source = undefined, .stat = .{ .size = stat.size, .inode = stat.inode, .mtime = stat.mtime, }, - .tree = undefined, - .zir = undefined, + .source = null, + .tree = null, + .zir = null, + .zoir = null, .mod = undefined, }; @@ -6613,17 +6603,14 @@ fn cmdChangelist( if (amt != stat.size) return error.UnexpectedEndOfFile; file.source = source; - file.source_loaded = true; - file.tree = try Ast.parse(gpa, file.source, .zig); - file.tree_loaded = true; - defer file.tree.deinit(gpa); + file.tree = try Ast.parse(gpa, file.source.?, .zig); + defer file.tree.?.deinit(gpa); - file.zir = try AstGen.generate(gpa, file.tree); - file.zir_loaded = true; - defer file.zir.deinit(gpa); + file.zir = try AstGen.generate(gpa, file.tree.?); + defer file.zir.?.deinit(gpa); - if (file.zir.loweringFailed()) { + if (file.zir.?.loweringFailed()) { var wip_errors: std.zig.ErrorBundle.Wip = undefined; try wip_errors.init(gpa); defer wip_errors.deinit(); @@ -6652,13 +6639,12 @@ fn cmdChangelist( var new_tree = try Ast.parse(gpa, new_source, .zig); defer new_tree.deinit(gpa); - var old_zir = file.zir; + var old_zir = file.zir.?; defer old_zir.deinit(gpa); - file.zir_loaded = false; + file.zir = null; file.zir = try AstGen.generate(gpa, new_tree); - file.zir_loaded = true; - if (file.zir.loweringFailed()) { + if (file.zir.?.loweringFailed()) { var wip_errors: std.zig.ErrorBundle.Wip = undefined; try wip_errors.init(gpa); defer wip_errors.deinit(); @@ -6672,7 +6658,7 @@ fn cmdChangelist( var inst_map: std.AutoHashMapUnmanaged(Zir.Inst.Index, Zir.Inst.Index) = .empty; defer inst_map.deinit(gpa); - try Zcu.mapOldZirToNew(gpa, old_zir, file.zir, &inst_map); + try Zcu.mapOldZirToNew(gpa, old_zir, file.zir.?, &inst_map); var bw = io.bufferedWriter(io.getStdOut().writer()); const stdout = bw.writer(); |
