authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-08-16 20:01:46-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-08-28 18:30:57-07:00
log379d7bc9f64e64818f31520700294f778aa13690
tree1828ce69a4bc7188b55ba3856b54f8639a50c321
parent6713745ec4f76d133eb0d5066c40bba666d0a194

compiler: update to not use GenericWriter


7 files changed, 45 insertions(+), 41 deletions(-)

src/IncrementalDebugServer.zig+4-4
...@@ -76,7 +76,9 @@ fn runThread(ids: *IncrementalDebugServer) void {...@@ -76,7 +76,9 @@ fn runThread(ids: *IncrementalDebugServer) void {
76 ids.mutex.lock();76 ids.mutex.lock();
77 }77 }
78 defer ids.mutex.unlock();78 defer ids.mutex.unlock();
79 handleCommand(ids.zcu, &text_out, cmd, arg) catch @panic("IncrementalDebugServer: out of memory");79 var allocating: std.Io.Writer.Allocating = .fromArrayList(gpa, &text_out);
80 defer text_out = allocating.toArrayList();
81 handleCommand(ids.zcu, &allocating.writer, cmd, arg) catch @panic("IncrementalDebugServer: out of memory");
80 }82 }
81 text_out.append(gpa, '\n') catch @panic("IncrementalDebugServer: out of memory");83 text_out.append(gpa, '\n') catch @panic("IncrementalDebugServer: out of memory");
82 conn.stream.writeAll(text_out.items) catch @panic("IncrementalDebugServer: failed to write");84 conn.stream.writeAll(text_out.items) catch @panic("IncrementalDebugServer: failed to write");
...@@ -119,10 +121,8 @@ const help_str: []const u8 =...@@ -119,10 +121,8 @@ const help_str: []const u8 =
119 \\121 \\
120;122;
121123
122fn handleCommand(zcu: *Zcu, output: *std.ArrayListUnmanaged(u8), cmd_str: []const u8, arg_str: []const u8) Allocator.Error!void {124fn handleCommand(zcu: *Zcu, w: *std.Io.Writer, cmd_str: []const u8, arg_str: []const u8) error{ WriteFailed, OutOfMemory }!void {
123 const ip = &zcu.intern_pool;125 const ip = &zcu.intern_pool;
124 const gpa = zcu.gpa;
125 const w = output.writer(gpa);
126 if (std.mem.eql(u8, cmd_str, "help")) {126 if (std.mem.eql(u8, cmd_str, "help")) {
127 try w.writeAll(help_str);127 try w.writeAll(help_str);
128 } else if (std.mem.eql(u8, cmd_str, "summary")) {128 } else if (std.mem.eql(u8, cmd_str, "summary")) {
src/Package/Fetch.zig+5-5
...@@ -200,7 +200,7 @@ pub const JobQueue = struct {...@@ -200,7 +200,7 @@ pub const JobQueue = struct {
200200
201 const hash_slice = hash.toSlice();201 const hash_slice = hash.toSlice();
202202
203 try buf.writer().print(203 try buf.print(
204 \\ pub const {f} = struct {{204 \\ pub const {f} = struct {{
205 \\205 \\
206 , .{std.zig.fmtId(hash_slice)});206 , .{std.zig.fmtId(hash_slice)});
...@@ -226,13 +226,13 @@ pub const JobQueue = struct {...@@ -226,13 +226,13 @@ pub const JobQueue = struct {
226 }226 }
227 }227 }
228228
229 try buf.writer().print(229 try buf.print(
230 \\ pub const build_root = "{f}";230 \\ pub const build_root = "{f}";
231 \\231 \\
232 , .{std.fmt.alt(fetch.package_root, .formatEscapeString)});232 , .{std.fmt.alt(fetch.package_root, .formatEscapeString)});
233233
234 if (fetch.has_build_zig) {234 if (fetch.has_build_zig) {
235 try buf.writer().print(235 try buf.print(
236 \\ pub const build_zig = @import("{f}");236 \\ pub const build_zig = @import("{f}");
237 \\237 \\
238 , .{std.zig.fmtString(hash_slice)});238 , .{std.zig.fmtString(hash_slice)});
...@@ -245,7 +245,7 @@ pub const JobQueue = struct {...@@ -245,7 +245,7 @@ pub const JobQueue = struct {
245 );245 );
246 for (manifest.dependencies.keys(), manifest.dependencies.values()) |name, dep| {246 for (manifest.dependencies.keys(), manifest.dependencies.values()) |name, dep| {
247 const h = depDigest(fetch.package_root, jq.global_cache, dep) orelse continue;247 const h = depDigest(fetch.package_root, jq.global_cache, dep) orelse continue;
248 try buf.writer().print(248 try buf.print(
249 " .{{ \"{f}\", \"{f}\" }},\n",249 " .{{ \"{f}\", \"{f}\" }},\n",
250 .{ std.zig.fmtString(name), std.zig.fmtString(h.toSlice()) },250 .{ std.zig.fmtString(name), std.zig.fmtString(h.toSlice()) },
251 );251 );
...@@ -277,7 +277,7 @@ pub const JobQueue = struct {...@@ -277,7 +277,7 @@ pub const JobQueue = struct {
277277
278 for (root_manifest.dependencies.keys(), root_manifest.dependencies.values()) |name, dep| {278 for (root_manifest.dependencies.keys(), root_manifest.dependencies.values()) |name, dep| {
279 const h = depDigest(root_fetch.package_root, jq.global_cache, dep) orelse continue;279 const h = depDigest(root_fetch.package_root, jq.global_cache, dep) orelse continue;
280 try buf.writer().print(280 try buf.print(
281 " .{{ \"{f}\", \"{f}\" }},\n",281 " .{{ \"{f}\", \"{f}\" }},\n",
282 .{ std.zig.fmtString(name), std.zig.fmtString(h.toSlice()) },282 .{ std.zig.fmtString(name), std.zig.fmtString(h.toSlice()) },
283 );283 );
src/arch/riscv64/Emit.zig+1-1
...@@ -31,7 +31,7 @@ pub fn emitMir(emit: *Emit) Error!void {...@@ -31,7 +31,7 @@ pub fn emitMir(emit: *Emit) Error!void {
31 var lowered_relocs = lowered.relocs;31 var lowered_relocs = lowered.relocs;
32 for (lowered.insts, 0..) |lowered_inst, lowered_index| {32 for (lowered.insts, 0..) |lowered_inst, lowered_index| {
33 const start_offset: u32 = @intCast(emit.code.items.len);33 const start_offset: u32 = @intCast(emit.code.items.len);
34 try lowered_inst.encode(emit.code.writer(gpa));34 std.mem.writeInt(u32, try emit.code.addManyAsArray(gpa, 4), lowered_inst.toU32(), .little);
3535
36 while (lowered_relocs.len > 0 and36 while (lowered_relocs.len > 0 and
37 lowered_relocs[0].lowered_inst_index == lowered_index) : ({37 lowered_relocs[0].lowered_inst_index == lowered_index) : ({
src/arch/riscv64/encoding.zig+1-1
...@@ -518,7 +518,7 @@ pub const Instruction = union(Lir.Format) {...@@ -518,7 +518,7 @@ pub const Instruction = union(Lir.Format) {
518 };518 };
519 }519 }
520520
521 pub fn encode(inst: Instruction, writer: anytype) !void {521 pub fn encode(inst: Instruction, writer: *std.Io.Writer) !void {
522 try writer.writeInt(u32, inst.toU32(), .little);522 try writer.writeInt(u32, inst.toU32(), .little);
523 }523 }
524524
src/arch/wasm/Mir.zig+22-17
...@@ -675,10 +675,13 @@ pub fn lower(mir: *const Mir, wasm: *Wasm, code: *std.ArrayListUnmanaged(u8)) st...@@ -675,10 +675,13 @@ pub fn lower(mir: *const Mir, wasm: *Wasm, code: *std.ArrayListUnmanaged(u8)) st
675 // Write the locals in the prologue of the function body.675 // Write the locals in the prologue of the function body.
676 try code.ensureUnusedCapacity(gpa, 5 + mir.locals.len * 6 + 38);676 try code.ensureUnusedCapacity(gpa, 5 + mir.locals.len * 6 + 38);
677677
678 std.leb.writeUleb128(code.fixedWriter(), @as(u32, @intCast(mir.locals.len))) catch unreachable;678 var w: std.Io.Writer = .fixed(code.unusedCapacitySlice());
679
680 w.writeLeb128(@as(u32, @intCast(mir.locals.len))) catch unreachable;
681
679 for (mir.locals) |local| {682 for (mir.locals) |local| {
680 std.leb.writeUleb128(code.fixedWriter(), @as(u32, 1)) catch unreachable;683 w.writeLeb128(@as(u32, 1)) catch unreachable;
681 code.appendAssumeCapacity(@intFromEnum(local));684 w.writeByte(@intFromEnum(local)) catch unreachable;
682 }685 }
683686
684 // Stack management section of function prologue.687 // Stack management section of function prologue.
...@@ -686,33 +689,35 @@ pub fn lower(mir: *const Mir, wasm: *Wasm, code: *std.ArrayListUnmanaged(u8)) st...@@ -686,33 +689,35 @@ pub fn lower(mir: *const Mir, wasm: *Wasm, code: *std.ArrayListUnmanaged(u8)) st
686 if (stack_alignment.toByteUnits()) |align_bytes| {689 if (stack_alignment.toByteUnits()) |align_bytes| {
687 const sp_global: Wasm.GlobalIndex = .stack_pointer;690 const sp_global: Wasm.GlobalIndex = .stack_pointer;
688 // load stack pointer691 // load stack pointer
689 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.global_get));692 w.writeByte(@intFromEnum(std.wasm.Opcode.global_get)) catch unreachable;
690 std.leb.writeUleb128(code.fixedWriter(), @intFromEnum(sp_global)) catch unreachable;693 w.writeUleb128(@intFromEnum(sp_global)) catch unreachable;
691 // store stack pointer so we can restore it when we return from the function694 // store stack pointer so we can restore it when we return from the function
692 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.local_tee));695 w.writeByte(@intFromEnum(std.wasm.Opcode.local_tee)) catch unreachable;
693 leb.writeUleb128(code.fixedWriter(), mir.prologue.sp_local) catch unreachable;696 w.writeUleb128(mir.prologue.sp_local) catch unreachable;
694 // get the total stack size697 // get the total stack size
695 const aligned_stack: i32 = @intCast(stack_alignment.forward(mir.prologue.stack_size));698 const aligned_stack: i32 = @intCast(stack_alignment.forward(mir.prologue.stack_size));
696 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_const));699 w.writeByte(@intFromEnum(std.wasm.Opcode.i32_const)) catch unreachable;
697 leb.writeIleb128(code.fixedWriter(), aligned_stack) catch unreachable;700 w.writeSleb128(aligned_stack) catch unreachable;
698 // subtract it from the current stack pointer701 // subtract it from the current stack pointer
699 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_sub));702 w.writeByte(@intFromEnum(std.wasm.Opcode.i32_sub)) catch unreachable;
700 // Get negative stack alignment703 // Get negative stack alignment
701 const neg_stack_align = @as(i32, @intCast(align_bytes)) * -1;704 const neg_stack_align = @as(i32, @intCast(align_bytes)) * -1;
702 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_const));705 w.writeByte(@intFromEnum(std.wasm.Opcode.i32_const)) catch unreachable;
703 leb.writeIleb128(code.fixedWriter(), neg_stack_align) catch unreachable;706 w.writeSleb128(neg_stack_align) catch unreachable;
704 // Bitwise-and the value to get the new stack pointer to ensure the707 // Bitwise-and the value to get the new stack pointer to ensure the
705 // pointers are aligned with the abi alignment.708 // pointers are aligned with the abi alignment.
706 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_and));709 w.writeByte(@intFromEnum(std.wasm.Opcode.i32_and)) catch unreachable;
707 // The bottom will be used to calculate all stack pointer offsets.710 // The bottom will be used to calculate all stack pointer offsets.
708 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.local_tee));711 w.writeByte(@intFromEnum(std.wasm.Opcode.local_tee)) catch unreachable;
709 leb.writeUleb128(code.fixedWriter(), mir.prologue.bottom_stack_local) catch unreachable;712 w.writeUleb128(mir.prologue.bottom_stack_local) catch unreachable;
710 // Store the current stack pointer value into the global stack pointer so other function calls will713 // Store the current stack pointer value into the global stack pointer so other function calls will
711 // start from this value instead and not overwrite the current stack.714 // start from this value instead and not overwrite the current stack.
712 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.global_set));715 w.writeByte(@intFromEnum(std.wasm.Opcode.global_set)) catch unreachable;
713 std.leb.writeUleb128(code.fixedWriter(), @intFromEnum(sp_global)) catch unreachable;716 w.writeUleb128(@intFromEnum(sp_global)) catch unreachable;
714 }717 }
715718
719 code.items.len += w.end;
720
716 var emit: Emit = .{721 var emit: Emit = .{
717 .mir = mir.*,722 .mir = mir.*,
718 .wasm = wasm,723 .wasm = wasm,
src/libs/mingw.zig+8-9
...@@ -304,9 +304,8 @@ pub fn buildImportLib(comp: *Compilation, lib_name: []const u8) !void {...@@ -304,9 +304,8 @@ pub fn buildImportLib(comp: *Compilation, lib_name: []const u8) !void {
304 const include_dir = try comp.dirs.zig_lib.join(arena, &.{ "libc", "mingw", "def-include" });304 const include_dir = try comp.dirs.zig_lib.join(arena, &.{ "libc", "mingw", "def-include" });
305305
306 if (comp.verbose_cc) print: {306 if (comp.verbose_cc) print: {
307 std.debug.lockStdErr();307 var stderr = std.debug.lockStderrWriter();
308 defer std.debug.unlockStdErr();308 defer std.debug.unlockStderrWriter();
309 const stderr = std.fs.File.stderr().deprecatedWriter();
310 nosuspend stderr.print("def file: {s}\n", .{def_file_path}) catch break :print;309 nosuspend stderr.print("def file: {s}\n", .{def_file_path}) catch break :print;
311 nosuspend stderr.print("include dir: {s}\n", .{include_dir}) catch break :print;310 nosuspend stderr.print("include dir: {s}\n", .{include_dir}) catch break :print;
312 nosuspend stderr.print("output path: {s}\n", .{def_final_path}) catch break :print;311 nosuspend stderr.print("output path: {s}\n", .{def_final_path}) catch break :print;
...@@ -410,9 +409,9 @@ fn findDef(...@@ -410,9 +409,9 @@ fn findDef(
410 // Try the archtecture-specific path first.409 // Try the archtecture-specific path first.
411 const fmt_path = "libc" ++ s ++ "mingw" ++ s ++ "{s}" ++ s ++ "{s}.def";410 const fmt_path = "libc" ++ s ++ "mingw" ++ s ++ "{s}" ++ s ++ "{s}.def";
412 if (zig_lib_directory.path) |p| {411 if (zig_lib_directory.path) |p| {
413 try override_path.writer().print("{s}" ++ s ++ fmt_path, .{ p, lib_path, lib_name });412 try override_path.print("{s}" ++ s ++ fmt_path, .{ p, lib_path, lib_name });
414 } else {413 } else {
415 try override_path.writer().print(fmt_path, .{ lib_path, lib_name });414 try override_path.print(fmt_path, .{ lib_path, lib_name });
416 }415 }
417 if (std.fs.cwd().access(override_path.items, .{})) |_| {416 if (std.fs.cwd().access(override_path.items, .{})) |_| {
418 return override_path.toOwnedSlice();417 return override_path.toOwnedSlice();
...@@ -427,9 +426,9 @@ fn findDef(...@@ -427,9 +426,9 @@ fn findDef(
427 override_path.shrinkRetainingCapacity(0);426 override_path.shrinkRetainingCapacity(0);
428 const fmt_path = "libc" ++ s ++ "mingw" ++ s ++ "lib-common" ++ s ++ "{s}.def";427 const fmt_path = "libc" ++ s ++ "mingw" ++ s ++ "lib-common" ++ s ++ "{s}.def";
429 if (zig_lib_directory.path) |p| {428 if (zig_lib_directory.path) |p| {
430 try override_path.writer().print("{s}" ++ s ++ fmt_path, .{ p, lib_name });429 try override_path.print("{s}" ++ s ++ fmt_path, .{ p, lib_name });
431 } else {430 } else {
432 try override_path.writer().print(fmt_path, .{lib_name});431 try override_path.print(fmt_path, .{lib_name});
433 }432 }
434 if (std.fs.cwd().access(override_path.items, .{})) |_| {433 if (std.fs.cwd().access(override_path.items, .{})) |_| {
435 return override_path.toOwnedSlice();434 return override_path.toOwnedSlice();
...@@ -444,9 +443,9 @@ fn findDef(...@@ -444,9 +443,9 @@ fn findDef(
444 override_path.shrinkRetainingCapacity(0);443 override_path.shrinkRetainingCapacity(0);
445 const fmt_path = "libc" ++ s ++ "mingw" ++ s ++ "lib-common" ++ s ++ "{s}.def.in";444 const fmt_path = "libc" ++ s ++ "mingw" ++ s ++ "lib-common" ++ s ++ "{s}.def.in";
446 if (zig_lib_directory.path) |p| {445 if (zig_lib_directory.path) |p| {
447 try override_path.writer().print("{s}" ++ s ++ fmt_path, .{ p, lib_name });446 try override_path.print("{s}" ++ s ++ fmt_path, .{ p, lib_name });
448 } else {447 } else {
449 try override_path.writer().print(fmt_path, .{lib_name});448 try override_path.print(fmt_path, .{lib_name});
450 }449 }
451 if (std.fs.cwd().access(override_path.items, .{})) |_| {450 if (std.fs.cwd().access(override_path.items, .{})) |_| {
452 return override_path.toOwnedSlice();451 return override_path.toOwnedSlice();
src/link.zig+4-4
...@@ -1976,7 +1976,7 @@ fn resolveLibInput(...@@ -1976,7 +1976,7 @@ fn resolveLibInput(
1976 .root_dir = lib_directory,1976 .root_dir = lib_directory,
1977 .sub_path = try std.fmt.allocPrint(arena, "lib{s}.tbd", .{lib_name}),1977 .sub_path = try std.fmt.allocPrint(arena, "lib{s}.tbd", .{lib_name}),
1978 };1978 };
1979 try checked_paths.writer(gpa).print("\n {f}", .{test_path});1979 try checked_paths.print(gpa, "\n {f}", .{test_path});
1980 var file = test_path.root_dir.handle.openFile(test_path.sub_path, .{}) catch |err| switch (err) {1980 var file = test_path.root_dir.handle.openFile(test_path.sub_path, .{}) catch |err| switch (err) {
1981 error.FileNotFound => break :tbd,1981 error.FileNotFound => break :tbd,
1982 else => |e| fatal("unable to search for tbd library '{f}': {s}", .{ test_path, @errorName(e) }),1982 else => |e| fatal("unable to search for tbd library '{f}': {s}", .{ test_path, @errorName(e) }),
...@@ -1995,7 +1995,7 @@ fn resolveLibInput(...@@ -1995,7 +1995,7 @@ fn resolveLibInput(
1995 },1995 },
1996 }),1996 }),
1997 };1997 };
1998 try checked_paths.writer(gpa).print("\n {f}", .{test_path});1998 try checked_paths.print(gpa, "\n {f}", .{test_path});
1999 switch (try resolvePathInputLib(gpa, arena, unresolved_inputs, resolved_inputs, ld_script_bytes, target, .{1999 switch (try resolvePathInputLib(gpa, arena, unresolved_inputs, resolved_inputs, ld_script_bytes, target, .{
2000 .path = test_path,2000 .path = test_path,
2001 .query = name_query.query,2001 .query = name_query.query,
...@@ -2012,7 +2012,7 @@ fn resolveLibInput(...@@ -2012,7 +2012,7 @@ fn resolveLibInput(
2012 .root_dir = lib_directory,2012 .root_dir = lib_directory,
2013 .sub_path = try std.fmt.allocPrint(arena, "lib{s}.so", .{lib_name}),2013 .sub_path = try std.fmt.allocPrint(arena, "lib{s}.so", .{lib_name}),
2014 };2014 };
2015 try checked_paths.writer(gpa).print("\n {f}", .{test_path});2015 try checked_paths.print(gpa, "\n {f}", .{test_path});
2016 var file = test_path.root_dir.handle.openFile(test_path.sub_path, .{}) catch |err| switch (err) {2016 var file = test_path.root_dir.handle.openFile(test_path.sub_path, .{}) catch |err| switch (err) {
2017 error.FileNotFound => break :so,2017 error.FileNotFound => break :so,
2018 else => |e| fatal("unable to search for so library '{f}': {s}", .{2018 else => |e| fatal("unable to search for so library '{f}': {s}", .{
...@@ -2030,7 +2030,7 @@ fn resolveLibInput(...@@ -2030,7 +2030,7 @@ fn resolveLibInput(
2030 .root_dir = lib_directory,2030 .root_dir = lib_directory,
2031 .sub_path = try std.fmt.allocPrint(arena, "lib{s}.a", .{lib_name}),2031 .sub_path = try std.fmt.allocPrint(arena, "lib{s}.a", .{lib_name}),
2032 };2032 };
2033 try checked_paths.writer(gpa).print("\n {f}", .{test_path});2033 try checked_paths.print(gpa, "\n {f}", .{test_path});
2034 var file = test_path.root_dir.handle.openFile(test_path.sub_path, .{}) catch |err| switch (err) {2034 var file = test_path.root_dir.handle.openFile(test_path.sub_path, .{}) catch |err| switch (err) {
2035 error.FileNotFound => break :mingw,2035 error.FileNotFound => break :mingw,
2036 else => |e| fatal("unable to search for static library '{f}': {s}", .{ test_path, @errorName(e) }),2036 else => |e| fatal("unable to search for static library '{f}': {s}", .{ test_path, @errorName(e) }),