progress towards compiler building again

This commit is contained in:
Andrew Kelley 2025-10-10 22:48:12 -07:00
parent 3b34622368
commit d40803284e
4 changed files with 41 additions and 41 deletions

View File

@ -6,12 +6,13 @@
//! There is one special encoding for this data structure. If both arrays are //! There is one special encoding for this data structure. If both arrays are
//! empty, it means there are no errors. This special encoding exists so that //! empty, it means there are no errors. This special encoding exists so that
//! heap allocation is not needed in the common case of no errors. //! heap allocation is not needed in the common case of no errors.
const ErrorBundle = @This();
const std = @import("std"); const std = @import("std");
const ErrorBundle = @This(); const Io = std.Io;
const Writer = std.Io.Writer;
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const assert = std.debug.assert; const assert = std.debug.assert;
const Writer = std.Io.Writer;
string_bytes: []const u8, string_bytes: []const u8,
/// The first thing in this array is an `ErrorMessageList`. /// The first thing in this array is an `ErrorMessageList`.
@ -156,7 +157,7 @@ pub fn nullTerminatedString(eb: ErrorBundle, index: String) [:0]const u8 {
} }
pub const RenderOptions = struct { pub const RenderOptions = struct {
ttyconf: std.Io.tty.Config, ttyconf: Io.tty.Config,
include_reference_trace: bool = true, include_reference_trace: bool = true,
include_source_line: bool = true, include_source_line: bool = true,
include_log_text: bool = true, include_log_text: bool = true,
@ -190,7 +191,7 @@ fn renderErrorMessageToWriter(
err_msg_index: MessageIndex, err_msg_index: MessageIndex,
w: *Writer, w: *Writer,
kind: []const u8, kind: []const u8,
color: std.Io.tty.Color, color: Io.tty.Color,
indent: usize, indent: usize,
) (Writer.Error || std.posix.UnexpectedError)!void { ) (Writer.Error || std.posix.UnexpectedError)!void {
const ttyconf = options.ttyconf; const ttyconf = options.ttyconf;
@ -320,6 +321,7 @@ fn writeMsg(eb: ErrorBundle, err_msg: ErrorMessage, w: *Writer, indent: usize) !
pub const Wip = struct { pub const Wip = struct {
gpa: Allocator, gpa: Allocator,
io: Io,
string_bytes: std.ArrayListUnmanaged(u8), string_bytes: std.ArrayListUnmanaged(u8),
/// The first thing in this array is a ErrorMessageList. /// The first thing in this array is a ErrorMessageList.
extra: std.ArrayListUnmanaged(u32), extra: std.ArrayListUnmanaged(u32),
@ -806,7 +808,7 @@ pub const Wip = struct {
}; };
defer bundle.deinit(std.testing.allocator); defer bundle.deinit(std.testing.allocator);
const ttyconf: std.Io.tty.Config = .no_color; const ttyconf: Io.tty.Config = .no_color;
var bundle_buf: Writer.Allocating = .init(std.testing.allocator); var bundle_buf: Writer.Allocating = .init(std.testing.allocator);
const bundle_bw = &bundle_buf.interface; const bundle_bw = &bundle_buf.interface;

View File

@ -1,7 +1,9 @@
const Compilation = @This(); const Compilation = @This();
const builtin = @import("builtin");
const std = @import("std"); const std = @import("std");
const builtin = @import("builtin"); const Io = std.Io;
const Writer = std.Io.Writer;
const fs = std.fs; const fs = std.fs;
const mem = std.mem; const mem = std.mem;
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
@ -12,7 +14,6 @@ const ThreadPool = std.Thread.Pool;
const WaitGroup = std.Thread.WaitGroup; const WaitGroup = std.Thread.WaitGroup;
const ErrorBundle = std.zig.ErrorBundle; const ErrorBundle = std.zig.ErrorBundle;
const fatal = std.process.fatal; const fatal = std.process.fatal;
const Writer = std.Io.Writer;
const Value = @import("Value.zig"); const Value = @import("Value.zig");
const Type = @import("Type.zig"); const Type = @import("Type.zig");
@ -1095,6 +1096,7 @@ pub const CObject = struct {
bundle: Bundle, bundle: Bundle,
notes_len: u32, notes_len: u32,
) !ErrorBundle.ErrorMessage { ) !ErrorBundle.ErrorMessage {
const io = eb.io;
var start = diag.src_loc.offset; var start = diag.src_loc.offset;
var end = diag.src_loc.offset; var end = diag.src_loc.offset;
for (diag.src_ranges) |src_range| { for (diag.src_ranges) |src_range| {
@ -1117,7 +1119,7 @@ pub const CObject = struct {
const file = fs.cwd().openFile(file_name, .{}) catch break :source_line 0; const file = fs.cwd().openFile(file_name, .{}) catch break :source_line 0;
defer file.close(); defer file.close();
var buffer: [1024]u8 = undefined; var buffer: [1024]u8 = undefined;
var file_reader = file.reader(&buffer); var file_reader = file.reader(io, &buffer);
file_reader.seekTo(diag.src_loc.offset + 1 - diag.src_loc.column) catch break :source_line 0; file_reader.seekTo(diag.src_loc.offset + 1 - diag.src_loc.column) catch break :source_line 0;
var aw: Writer.Allocating = .init(eb.gpa); var aw: Writer.Allocating = .init(eb.gpa);
defer aw.deinit(); defer aw.deinit();
@ -1155,7 +1157,7 @@ pub const CObject = struct {
gpa.destroy(bundle); gpa.destroy(bundle);
} }
pub fn parse(gpa: Allocator, path: []const u8) !*Bundle { pub fn parse(gpa: Allocator, io: Io, path: []const u8) !*Bundle {
const BlockId = enum(u32) { const BlockId = enum(u32) {
Meta = 8, Meta = 8,
Diag, Diag,
@ -1191,7 +1193,7 @@ pub const CObject = struct {
var buffer: [1024]u8 = undefined; var buffer: [1024]u8 = undefined;
const file = try fs.cwd().openFile(path, .{}); const file = try fs.cwd().openFile(path, .{});
defer file.close(); defer file.close();
var file_reader = file.reader(&buffer); var file_reader = file.reader(io, &buffer);
var bc = std.zig.llvm.BitcodeReader.init(gpa, .{ .reader = &file_reader.interface }); var bc = std.zig.llvm.BitcodeReader.init(gpa, .{ .reader = &file_reader.interface });
defer bc.deinit(); defer bc.deinit();

View File

@ -319,7 +319,7 @@ fn mainArgs(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
.root_src_path = "objcopy.zig", .root_src_path = "objcopy.zig",
}); });
} else if (mem.eql(u8, cmd, "fetch")) { } else if (mem.eql(u8, cmd, "fetch")) {
return cmdFetch(gpa, arena, cmd_args); return cmdFetch(gpa, arena, io, cmd_args);
} else if (mem.eql(u8, cmd, "libc")) { } else if (mem.eql(u8, cmd, "libc")) {
return jitCmd(gpa, arena, io, cmd_args, .{ return jitCmd(gpa, arena, io, cmd_args, .{
.cmd_name = "libc", .cmd_name = "libc",
@ -348,12 +348,14 @@ fn mainArgs(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
return; return;
} else if (mem.eql(u8, cmd, "env")) { } else if (mem.eql(u8, cmd, "env")) {
dev.check(.env_command); dev.check(.env_command);
const host = std.zig.resolveTargetQueryOrFatal(io, .{});
var stdout_writer = fs.File.stdout().writer(&stdout_buffer); var stdout_writer = fs.File.stdout().writer(&stdout_buffer);
try @import("print_env.zig").cmdEnv( try @import("print_env.zig").cmdEnv(
arena, arena,
&stdout_writer.interface, &stdout_writer.interface,
args, args,
if (native_os == .wasi) wasi_preopens, if (native_os == .wasi) wasi_preopens,
&host,
); );
return stdout_writer.interface.flush(); return stdout_writer.interface.flush();
} else if (mem.eql(u8, cmd, "reduce")) { } else if (mem.eql(u8, cmd, "reduce")) {
@ -368,11 +370,11 @@ fn mainArgs(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
dev.check(.help_command); dev.check(.help_command);
return fs.File.stdout().writeAll(usage); return fs.File.stdout().writeAll(usage);
} else if (mem.eql(u8, cmd, "ast-check")) { } else if (mem.eql(u8, cmd, "ast-check")) {
return cmdAstCheck(arena, cmd_args); return cmdAstCheck(arena, io, cmd_args);
} else if (mem.eql(u8, cmd, "detect-cpu")) { } else if (mem.eql(u8, cmd, "detect-cpu")) {
return cmdDetectCpu(io, cmd_args); return cmdDetectCpu(io, cmd_args);
} else if (build_options.enable_debug_extensions and mem.eql(u8, cmd, "changelist")) { } else if (build_options.enable_debug_extensions and mem.eql(u8, cmd, "changelist")) {
return cmdChangelist(arena, cmd_args); return cmdChangelist(arena, io, cmd_args);
} else if (build_options.enable_debug_extensions and mem.eql(u8, cmd, "dump-zir")) { } else if (build_options.enable_debug_extensions and mem.eql(u8, cmd, "dump-zir")) {
return cmdDumpZir(arena, cmd_args); return cmdDumpZir(arena, cmd_args);
} else if (build_options.enable_debug_extensions and mem.eql(u8, cmd, "llvm-ints")) { } else if (build_options.enable_debug_extensions and mem.eql(u8, cmd, "llvm-ints")) {
@ -741,7 +743,7 @@ const ArgMode = union(enum) {
const Listen = union(enum) { const Listen = union(enum) {
none, none,
stdio: if (dev.env.supports(.stdio_listen)) void else noreturn, stdio: if (dev.env.supports(.stdio_listen)) void else noreturn,
ip4: if (dev.env.supports(.network_listen)) std.net.Ip4Address else noreturn, ip4: if (dev.env.supports(.network_listen)) Io.net.Ip4Address else noreturn,
}; };
const ArgsIterator = struct { const ArgsIterator = struct {
@ -1335,7 +1337,7 @@ fn buildOutputType(
const host, const port_text = mem.cutScalar(u8, next_arg, ':') orelse .{ next_arg, "14735" }; const host, const port_text = mem.cutScalar(u8, next_arg, ':') orelse .{ next_arg, "14735" };
const port = std.fmt.parseInt(u16, port_text, 10) catch |err| const port = std.fmt.parseInt(u16, port_text, 10) catch |err|
fatal("invalid port number: '{s}': {s}", .{ port_text, @errorName(err) }); fatal("invalid port number: '{s}': {s}", .{ port_text, @errorName(err) });
listen = .{ .ip4 = std.net.Ip4Address.parse(host, port) catch |err| listen = .{ .ip4 = Io.net.Ip4Address.parse(host, port) catch |err|
fatal("invalid host: '{s}': {s}", .{ host, @errorName(err) }) }; fatal("invalid host: '{s}': {s}", .{ host, @errorName(err) }) };
} }
} else if (mem.eql(u8, arg, "--listen=-")) { } else if (mem.eql(u8, arg, "--listen=-")) {
@ -3318,7 +3320,7 @@ fn buildOutputType(
var file_writer = f.writer(&.{}); var file_writer = f.writer(&.{});
var buffer: [1000]u8 = undefined; var buffer: [1000]u8 = undefined;
var hasher = file_writer.interface.hashed(Cache.Hasher.init("0123456789abcdef"), &buffer); var hasher = file_writer.interface.hashed(Cache.Hasher.init("0123456789abcdef"), &buffer);
var stdin_reader = fs.File.stdin().readerStreaming(&.{}); var stdin_reader = fs.File.stdin().readerStreaming(io, &.{});
_ = hasher.writer.sendFileAll(&stdin_reader, .unlimited) catch |err| switch (err) { _ = hasher.writer.sendFileAll(&stdin_reader, .unlimited) catch |err| switch (err) {
error.WriteFailed => fatal("failed to write {s}: {t}", .{ dump_path, file_writer.err.? }), error.WriteFailed => fatal("failed to write {s}: {t}", .{ dump_path, file_writer.err.? }),
else => fatal("failed to pipe stdin to {s}: {t}", .{ dump_path, err }), else => fatal("failed to pipe stdin to {s}: {t}", .{ dump_path, err }),
@ -3549,7 +3551,7 @@ fn buildOutputType(
switch (listen) { switch (listen) {
.none => {}, .none => {},
.stdio => { .stdio => {
var stdin_reader = fs.File.stdin().reader(&stdin_buffer); var stdin_reader = fs.File.stdin().reader(io, &stdin_buffer);
var stdout_writer = fs.File.stdout().writer(&stdout_buffer); var stdout_writer = fs.File.stdout().writer(&stdout_buffer);
try serve( try serve(
io, io,
@ -3565,23 +3567,23 @@ fn buildOutputType(
return cleanExit(); return cleanExit();
}, },
.ip4 => |ip4_addr| { .ip4 => |ip4_addr| {
const addr: std.net.Address = .{ .in = ip4_addr }; const addr: Io.net.IpAddress = .{ .ip4 = ip4_addr };
var server = try addr.listen(.{ var server = try addr.listen(io, .{
.reuse_address = true, .reuse_address = true,
}); });
defer server.deinit(); defer server.deinit(io);
const conn = try server.accept(); var stream = try server.accept(io);
defer conn.stream.close(); defer stream.close(io);
var input = conn.stream.reader(&stdin_buffer); var input = stream.reader(io, &stdin_buffer);
var output = conn.stream.writer(&stdout_buffer); var output = stream.writer(io, &stdout_buffer);
try serve( try serve(
io, io,
comp, comp,
input.interface(), &input.interface,
&output.interface, &output.interface,
test_exec_args.items, test_exec_args.items,
self_exe_path, self_exe_path,
@ -5062,7 +5064,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8)
var http_client: if (dev.env.supports(.fetch_command)) std.http.Client else struct { var http_client: if (dev.env.supports(.fetch_command)) std.http.Client else struct {
allocator: Allocator, allocator: Allocator,
fn deinit(_: @This()) void {} fn deinit(_: @This()) void {}
} = .{ .allocator = gpa }; } = .{ .allocator = gpa, .io = io };
defer http_client.deinit(); defer http_client.deinit();
var unlazy_set: Package.Fetch.JobQueue.UnlazySet = .{}; var unlazy_set: Package.Fetch.JobQueue.UnlazySet = .{};
@ -5600,7 +5602,7 @@ fn jitCmd(
try child.spawn(); try child.spawn();
if (options.capture) |ptr| { if (options.capture) |ptr| {
var stdout_reader = child.stdout.?.readerStreaming(&.{}); var stdout_reader = child.stdout.?.readerStreaming(io, &.{});
ptr.* = try stdout_reader.interface.allocRemaining(arena, .limited(std.math.maxInt(u32))); ptr.* = try stdout_reader.interface.allocRemaining(arena, .limited(std.math.maxInt(u32)));
} }
@ -6055,10 +6057,7 @@ const usage_ast_check =
\\ \\
; ;
fn cmdAstCheck( fn cmdAstCheck(arena: Allocator, io: Io, args: []const []const u8) !void {
arena: Allocator,
args: []const []const u8,
) !void {
dev.check(.ast_check_command); dev.check(.ast_check_command);
const Zir = std.zig.Zir; const Zir = std.zig.Zir;
@ -6106,7 +6105,7 @@ fn cmdAstCheck(
}; };
} else fs.File.stdin(); } else fs.File.stdin();
defer if (zig_source_path != null) f.close(); defer if (zig_source_path != null) f.close();
var file_reader: fs.File.Reader = f.reader(&stdin_buffer); var file_reader: fs.File.Reader = f.reader(io, &stdin_buffer);
break :s std.zig.readSourceFileToEndAlloc(arena, &file_reader) catch |err| { break :s std.zig.readSourceFileToEndAlloc(arena, &file_reader) catch |err| {
fatal("unable to load file '{s}' for ast-check: {s}", .{ display_path, @errorName(err) }); fatal("unable to load file '{s}' for ast-check: {s}", .{ display_path, @errorName(err) });
}; };
@ -6448,10 +6447,7 @@ fn cmdDumpZir(
} }
/// This is only enabled for debug builds. /// This is only enabled for debug builds.
fn cmdChangelist( fn cmdChangelist(arena: Allocator, io: Io, args: []const []const u8) !void {
arena: Allocator,
args: []const []const u8,
) !void {
dev.check(.changelist_command); dev.check(.changelist_command);
const color: Color = .auto; const color: Color = .auto;
@ -6464,7 +6460,7 @@ fn cmdChangelist(
var f = fs.cwd().openFile(old_source_path, .{}) catch |err| var f = fs.cwd().openFile(old_source_path, .{}) catch |err|
fatal("unable to open old source file '{s}': {s}", .{ old_source_path, @errorName(err) }); fatal("unable to open old source file '{s}': {s}", .{ old_source_path, @errorName(err) });
defer f.close(); defer f.close();
var file_reader: fs.File.Reader = f.reader(&stdin_buffer); var file_reader: fs.File.Reader = f.reader(io, &stdin_buffer);
break :source std.zig.readSourceFileToEndAlloc(arena, &file_reader) catch |err| break :source std.zig.readSourceFileToEndAlloc(arena, &file_reader) catch |err|
fatal("unable to read old source file '{s}': {s}", .{ old_source_path, @errorName(err) }); fatal("unable to read old source file '{s}': {s}", .{ old_source_path, @errorName(err) });
}; };
@ -6472,7 +6468,7 @@ fn cmdChangelist(
var f = fs.cwd().openFile(new_source_path, .{}) catch |err| var f = fs.cwd().openFile(new_source_path, .{}) catch |err|
fatal("unable to open new source file '{s}': {s}", .{ new_source_path, @errorName(err) }); fatal("unable to open new source file '{s}': {s}", .{ new_source_path, @errorName(err) });
defer f.close(); defer f.close();
var file_reader: fs.File.Reader = f.reader(&stdin_buffer); var file_reader: fs.File.Reader = f.reader(io, &stdin_buffer);
break :source std.zig.readSourceFileToEndAlloc(arena, &file_reader) catch |err| break :source std.zig.readSourceFileToEndAlloc(arena, &file_reader) catch |err|
fatal("unable to read new source file '{s}': {s}", .{ new_source_path, @errorName(err) }); fatal("unable to read new source file '{s}': {s}", .{ new_source_path, @errorName(err) });
}; };
@ -6829,6 +6825,7 @@ const usage_fetch =
fn cmdFetch( fn cmdFetch(
gpa: Allocator, gpa: Allocator,
arena: Allocator, arena: Allocator,
io: Io,
args: []const []const u8, args: []const []const u8,
) !void { ) !void {
dev.check(.fetch_command); dev.check(.fetch_command);
@ -6884,7 +6881,7 @@ fn cmdFetch(
try thread_pool.init(.{ .allocator = gpa }); try thread_pool.init(.{ .allocator = gpa });
defer thread_pool.deinit(); defer thread_pool.deinit();
var http_client: std.http.Client = .{ .allocator = gpa }; var http_client: std.http.Client = .{ .allocator = gpa, .io = io };
defer http_client.deinit(); defer http_client.deinit();
try http_client.initDefaultProxies(arena); try http_client.initDefaultProxies(arena);

View File

@ -14,6 +14,7 @@ pub fn cmdEnv(
.wasi => std.fs.wasi.Preopens, .wasi => std.fs.wasi.Preopens,
else => void, else => void,
}, },
host: *const std.Target,
) !void { ) !void {
const override_lib_dir: ?[]const u8 = try EnvVar.ZIG_LIB_DIR.get(arena); const override_lib_dir: ?[]const u8 = try EnvVar.ZIG_LIB_DIR.get(arena);
const override_global_cache_dir: ?[]const u8 = try EnvVar.ZIG_GLOBAL_CACHE_DIR.get(arena); const override_global_cache_dir: ?[]const u8 = try EnvVar.ZIG_GLOBAL_CACHE_DIR.get(arena);
@ -38,8 +39,6 @@ pub fn cmdEnv(
const zig_lib_dir = dirs.zig_lib.path orelse ""; const zig_lib_dir = dirs.zig_lib.path orelse "";
const zig_std_dir = try dirs.zig_lib.join(arena, &.{"std"}); const zig_std_dir = try dirs.zig_lib.join(arena, &.{"std"});
const global_cache_dir = dirs.global_cache.path orelse ""; const global_cache_dir = dirs.global_cache.path orelse "";
const host = try std.zig.system.resolveTargetQuery(.{});
const triple = try host.zigTriple(arena); const triple = try host.zigTriple(arena);
var serializer: std.zon.Serializer = .{ .writer = out }; var serializer: std.zon.Serializer = .{ .writer = out };