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 {
7676 ids.mutex.lock();
7777 }
7878 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");
8082 }
8183 text_out.append(gpa, '\n') catch @panic("IncrementalDebugServer: out of memory");
8284 conn.stream.writeAll(text_out.items) catch @panic("IncrementalDebugServer: failed to write");
......@@ -119,10 +121,8 @@ const help_str: []const u8 =
119121 \\
120122;
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 {
123125 const ip = &zcu.intern_pool;
124 const gpa = zcu.gpa;
125 const w = output.writer(gpa);
126126 if (std.mem.eql(u8, cmd_str, "help")) {
127127 try w.writeAll(help_str);
128128 } else if (std.mem.eql(u8, cmd_str, "summary")) {
src/Package/Fetch.zig+5-5
......@@ -200,7 +200,7 @@ pub const JobQueue = struct {
200200
201201 const hash_slice = hash.toSlice();
202202
203 try buf.writer().print(
203 try buf.print(
204204 \\ pub const {f} = struct {{
205205 \\
206206 , .{std.zig.fmtId(hash_slice)});
......@@ -226,13 +226,13 @@ pub const JobQueue = struct {
226226 }
227227 }
228228
229 try buf.writer().print(
229 try buf.print(
230230 \\ pub const build_root = "{f}";
231231 \\
232232 , .{std.fmt.alt(fetch.package_root, .formatEscapeString)});
233233
234234 if (fetch.has_build_zig) {
235 try buf.writer().print(
235 try buf.print(
236236 \\ pub const build_zig = @import("{f}");
237237 \\
238238 , .{std.zig.fmtString(hash_slice)});
......@@ -245,7 +245,7 @@ pub const JobQueue = struct {
245245 );
246246 for (manifest.dependencies.keys(), manifest.dependencies.values()) |name, dep| {
247247 const h = depDigest(fetch.package_root, jq.global_cache, dep) orelse continue;
248 try buf.writer().print(
248 try buf.print(
249249 " .{{ \"{f}\", \"{f}\" }},\n",
250250 .{ std.zig.fmtString(name), std.zig.fmtString(h.toSlice()) },
251251 );
......@@ -277,7 +277,7 @@ pub const JobQueue = struct {
277277
278278 for (root_manifest.dependencies.keys(), root_manifest.dependencies.values()) |name, dep| {
279279 const h = depDigest(root_fetch.package_root, jq.global_cache, dep) orelse continue;
280 try buf.writer().print(
280 try buf.print(
281281 " .{{ \"{f}\", \"{f}\" }},\n",
282282 .{ std.zig.fmtString(name), std.zig.fmtString(h.toSlice()) },
283283 );
src/arch/riscv64/Emit.zig+1-1
......@@ -31,7 +31,7 @@ pub fn emitMir(emit: *Emit) Error!void {
3131 var lowered_relocs = lowered.relocs;
3232 for (lowered.insts, 0..) |lowered_inst, lowered_index| {
3333 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
3636 while (lowered_relocs.len > 0 and
3737 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) {
518518 };
519519 }
520520
521 pub fn encode(inst: Instruction, writer: anytype) !void {
521 pub fn encode(inst: Instruction, writer: *std.Io.Writer) !void {
522522 try writer.writeInt(u32, inst.toU32(), .little);
523523 }
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
675675 // Write the locals in the prologue of the function body.
676676 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
679682 for (mir.locals) |local| {
680 std.leb.writeUleb128(code.fixedWriter(), @as(u32, 1)) catch unreachable;
681 code.appendAssumeCapacity(@intFromEnum(local));
683 w.writeLeb128(@as(u32, 1)) catch unreachable;
684 w.writeByte(@intFromEnum(local)) catch unreachable;
682685 }
683686
684687 // Stack management section of function prologue.
......@@ -686,33 +689,35 @@ pub fn lower(mir: *const Mir, wasm: *Wasm, code: *std.ArrayListUnmanaged(u8)) st
686689 if (stack_alignment.toByteUnits()) |align_bytes| {
687690 const sp_global: Wasm.GlobalIndex = .stack_pointer;
688691 // load stack pointer
689 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.global_get));
690 std.leb.writeUleb128(code.fixedWriter(), @intFromEnum(sp_global)) catch unreachable;
692 w.writeByte(@intFromEnum(std.wasm.Opcode.global_get)) catch unreachable;
693 w.writeUleb128(@intFromEnum(sp_global)) catch unreachable;
691694 // store stack pointer so we can restore it when we return from the function
692 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.local_tee));
693 leb.writeUleb128(code.fixedWriter(), mir.prologue.sp_local) catch unreachable;
695 w.writeByte(@intFromEnum(std.wasm.Opcode.local_tee)) catch unreachable;
696 w.writeUleb128(mir.prologue.sp_local) catch unreachable;
694697 // get the total stack size
695698 const aligned_stack: i32 = @intCast(stack_alignment.forward(mir.prologue.stack_size));
696 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_const));
697 leb.writeIleb128(code.fixedWriter(), aligned_stack) catch unreachable;
699 w.writeByte(@intFromEnum(std.wasm.Opcode.i32_const)) catch unreachable;
700 w.writeSleb128(aligned_stack) catch unreachable;
698701 // 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;
700703 // Get negative stack alignment
701704 const neg_stack_align = @as(i32, @intCast(align_bytes)) * -1;
702 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_const));
703 leb.writeIleb128(code.fixedWriter(), neg_stack_align) catch unreachable;
705 w.writeByte(@intFromEnum(std.wasm.Opcode.i32_const)) catch unreachable;
706 w.writeSleb128(neg_stack_align) catch unreachable;
704707 // Bitwise-and the value to get the new stack pointer to ensure the
705708 // 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;
707710 // The bottom will be used to calculate all stack pointer offsets.
708 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.local_tee));
709 leb.writeUleb128(code.fixedWriter(), mir.prologue.bottom_stack_local) catch unreachable;
711 w.writeByte(@intFromEnum(std.wasm.Opcode.local_tee)) catch unreachable;
712 w.writeUleb128(mir.prologue.bottom_stack_local) catch unreachable;
710713 // Store the current stack pointer value into the global stack pointer so other function calls will
711714 // start from this value instead and not overwrite the current stack.
712 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.global_set));
713 std.leb.writeUleb128(code.fixedWriter(), @intFromEnum(sp_global)) catch unreachable;
715 w.writeByte(@intFromEnum(std.wasm.Opcode.global_set)) catch unreachable;
716 w.writeUleb128(@intFromEnum(sp_global)) catch unreachable;
714717 }
715718
719 code.items.len += w.end;
720
716721 var emit: Emit = .{
717722 .mir = mir.*,
718723 .wasm = wasm,
src/libs/mingw.zig+8-9
......@@ -304,9 +304,8 @@ pub fn buildImportLib(comp: *Compilation, lib_name: []const u8) !void {
304304 const include_dir = try comp.dirs.zig_lib.join(arena, &.{ "libc", "mingw", "def-include" });
305305
306306 if (comp.verbose_cc) print: {
307 std.debug.lockStdErr();
308 defer std.debug.unlockStdErr();
309 const stderr = std.fs.File.stderr().deprecatedWriter();
307 var stderr = std.debug.lockStderrWriter();
308 defer std.debug.unlockStderrWriter();
310309 nosuspend stderr.print("def file: {s}\n", .{def_file_path}) catch break :print;
311310 nosuspend stderr.print("include dir: {s}\n", .{include_dir}) catch break :print;
312311 nosuspend stderr.print("output path: {s}\n", .{def_final_path}) catch break :print;
......@@ -410,9 +409,9 @@ fn findDef(
410409 // Try the archtecture-specific path first.
411410 const fmt_path = "libc" ++ s ++ "mingw" ++ s ++ "{s}" ++ s ++ "{s}.def";
412411 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 });
414413 } else {
415 try override_path.writer().print(fmt_path, .{ lib_path, lib_name });
414 try override_path.print(fmt_path, .{ lib_path, lib_name });
416415 }
417416 if (std.fs.cwd().access(override_path.items, .{})) |_| {
418417 return override_path.toOwnedSlice();
......@@ -427,9 +426,9 @@ fn findDef(
427426 override_path.shrinkRetainingCapacity(0);
428427 const fmt_path = "libc" ++ s ++ "mingw" ++ s ++ "lib-common" ++ s ++ "{s}.def";
429428 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 });
431430 } else {
432 try override_path.writer().print(fmt_path, .{lib_name});
431 try override_path.print(fmt_path, .{lib_name});
433432 }
434433 if (std.fs.cwd().access(override_path.items, .{})) |_| {
435434 return override_path.toOwnedSlice();
......@@ -444,9 +443,9 @@ fn findDef(
444443 override_path.shrinkRetainingCapacity(0);
445444 const fmt_path = "libc" ++ s ++ "mingw" ++ s ++ "lib-common" ++ s ++ "{s}.def.in";
446445 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 });
448447 } else {
449 try override_path.writer().print(fmt_path, .{lib_name});
448 try override_path.print(fmt_path, .{lib_name});
450449 }
451450 if (std.fs.cwd().access(override_path.items, .{})) |_| {
452451 return override_path.toOwnedSlice();
src/link.zig+4-4
......@@ -1976,7 +1976,7 @@ fn resolveLibInput(
19761976 .root_dir = lib_directory,
19771977 .sub_path = try std.fmt.allocPrint(arena, "lib{s}.tbd", .{lib_name}),
19781978 };
1979 try checked_paths.writer(gpa).print("\n {f}", .{test_path});
1979 try checked_paths.print(gpa, "\n {f}", .{test_path});
19801980 var file = test_path.root_dir.handle.openFile(test_path.sub_path, .{}) catch |err| switch (err) {
19811981 error.FileNotFound => break :tbd,
19821982 else => |e| fatal("unable to search for tbd library '{f}': {s}", .{ test_path, @errorName(e) }),
......@@ -1995,7 +1995,7 @@ fn resolveLibInput(
19951995 },
19961996 }),
19971997 };
1998 try checked_paths.writer(gpa).print("\n {f}", .{test_path});
1998 try checked_paths.print(gpa, "\n {f}", .{test_path});
19991999 switch (try resolvePathInputLib(gpa, arena, unresolved_inputs, resolved_inputs, ld_script_bytes, target, .{
20002000 .path = test_path,
20012001 .query = name_query.query,
......@@ -2012,7 +2012,7 @@ fn resolveLibInput(
20122012 .root_dir = lib_directory,
20132013 .sub_path = try std.fmt.allocPrint(arena, "lib{s}.so", .{lib_name}),
20142014 };
2015 try checked_paths.writer(gpa).print("\n {f}", .{test_path});
2015 try checked_paths.print(gpa, "\n {f}", .{test_path});
20162016 var file = test_path.root_dir.handle.openFile(test_path.sub_path, .{}) catch |err| switch (err) {
20172017 error.FileNotFound => break :so,
20182018 else => |e| fatal("unable to search for so library '{f}': {s}", .{
......@@ -2030,7 +2030,7 @@ fn resolveLibInput(
20302030 .root_dir = lib_directory,
20312031 .sub_path = try std.fmt.allocPrint(arena, "lib{s}.a", .{lib_name}),
20322032 };
2033 try checked_paths.writer(gpa).print("\n {f}", .{test_path});
2033 try checked_paths.print(gpa, "\n {f}", .{test_path});
20342034 var file = test_path.root_dir.handle.openFile(test_path.sub_path, .{}) catch |err| switch (err) {
20352035 error.FileNotFound => break :mingw,
20362036 else => |e| fatal("unable to search for static library '{f}': {s}", .{ test_path, @errorName(e) }),