Commit 379d7bc9f6

Andrew Kelley <andrew@ziglang.org>
2025-08-17 05:01:46
compiler: update to not use GenericWriter
1 parent 6713745
src/arch/riscv64/Emit.zig
@@ -31,7 +31,7 @@ pub fn emitMir(emit: *Emit) Error!void {
         var lowered_relocs = lowered.relocs;
         for (lowered.insts, 0..) |lowered_inst, lowered_index| {
             const start_offset: u32 = @intCast(emit.code.items.len);
-            try lowered_inst.encode(emit.code.writer(gpa));
+            std.mem.writeInt(u32, try emit.code.addManyAsArray(gpa, 4), lowered_inst.toU32(), .little);
 
             while (lowered_relocs.len > 0 and
                 lowered_relocs[0].lowered_inst_index == lowered_index) : ({
src/arch/riscv64/encoding.zig
@@ -518,7 +518,7 @@ pub const Instruction = union(Lir.Format) {
         };
     }
 
-    pub fn encode(inst: Instruction, writer: anytype) !void {
+    pub fn encode(inst: Instruction, writer: *std.Io.Writer) !void {
         try writer.writeInt(u32, inst.toU32(), .little);
     }
 
src/arch/wasm/Mir.zig
@@ -675,10 +675,13 @@ pub fn lower(mir: *const Mir, wasm: *Wasm, code: *std.ArrayListUnmanaged(u8)) st
     // Write the locals in the prologue of the function body.
     try code.ensureUnusedCapacity(gpa, 5 + mir.locals.len * 6 + 38);
 
-    std.leb.writeUleb128(code.fixedWriter(), @as(u32, @intCast(mir.locals.len))) catch unreachable;
+    var w: std.Io.Writer = .fixed(code.unusedCapacitySlice());
+
+    w.writeLeb128(@as(u32, @intCast(mir.locals.len))) catch unreachable;
+
     for (mir.locals) |local| {
-        std.leb.writeUleb128(code.fixedWriter(), @as(u32, 1)) catch unreachable;
-        code.appendAssumeCapacity(@intFromEnum(local));
+        w.writeLeb128(@as(u32, 1)) catch unreachable;
+        w.writeByte(@intFromEnum(local)) catch unreachable;
     }
 
     // Stack management section of function prologue.
@@ -686,33 +689,35 @@ pub fn lower(mir: *const Mir, wasm: *Wasm, code: *std.ArrayListUnmanaged(u8)) st
     if (stack_alignment.toByteUnits()) |align_bytes| {
         const sp_global: Wasm.GlobalIndex = .stack_pointer;
         // load stack pointer
-        code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.global_get));
-        std.leb.writeUleb128(code.fixedWriter(), @intFromEnum(sp_global)) catch unreachable;
+        w.writeByte(@intFromEnum(std.wasm.Opcode.global_get)) catch unreachable;
+        w.writeUleb128(@intFromEnum(sp_global)) catch unreachable;
         // store stack pointer so we can restore it when we return from the function
-        code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.local_tee));
-        leb.writeUleb128(code.fixedWriter(), mir.prologue.sp_local) catch unreachable;
+        w.writeByte(@intFromEnum(std.wasm.Opcode.local_tee)) catch unreachable;
+        w.writeUleb128(mir.prologue.sp_local) catch unreachable;
         // get the total stack size
         const aligned_stack: i32 = @intCast(stack_alignment.forward(mir.prologue.stack_size));
-        code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_const));
-        leb.writeIleb128(code.fixedWriter(), aligned_stack) catch unreachable;
+        w.writeByte(@intFromEnum(std.wasm.Opcode.i32_const)) catch unreachable;
+        w.writeSleb128(aligned_stack) catch unreachable;
         // subtract it from the current stack pointer
-        code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_sub));
+        w.writeByte(@intFromEnum(std.wasm.Opcode.i32_sub)) catch unreachable;
         // Get negative stack alignment
         const neg_stack_align = @as(i32, @intCast(align_bytes)) * -1;
-        code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_const));
-        leb.writeIleb128(code.fixedWriter(), neg_stack_align) catch unreachable;
+        w.writeByte(@intFromEnum(std.wasm.Opcode.i32_const)) catch unreachable;
+        w.writeSleb128(neg_stack_align) catch unreachable;
         // Bitwise-and the value to get the new stack pointer to ensure the
         // pointers are aligned with the abi alignment.
-        code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_and));
+        w.writeByte(@intFromEnum(std.wasm.Opcode.i32_and)) catch unreachable;
         // The bottom will be used to calculate all stack pointer offsets.
-        code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.local_tee));
-        leb.writeUleb128(code.fixedWriter(), mir.prologue.bottom_stack_local) catch unreachable;
+        w.writeByte(@intFromEnum(std.wasm.Opcode.local_tee)) catch unreachable;
+        w.writeUleb128(mir.prologue.bottom_stack_local) catch unreachable;
         // Store the current stack pointer value into the global stack pointer so other function calls will
         // start from this value instead and not overwrite the current stack.
-        code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.global_set));
-        std.leb.writeUleb128(code.fixedWriter(), @intFromEnum(sp_global)) catch unreachable;
+        w.writeByte(@intFromEnum(std.wasm.Opcode.global_set)) catch unreachable;
+        w.writeUleb128(@intFromEnum(sp_global)) catch unreachable;
     }
 
+    code.items.len += w.end;
+
     var emit: Emit = .{
         .mir = mir.*,
         .wasm = wasm,
src/libs/mingw.zig
@@ -304,9 +304,8 @@ pub fn buildImportLib(comp: *Compilation, lib_name: []const u8) !void {
     const include_dir = try comp.dirs.zig_lib.join(arena, &.{ "libc", "mingw", "def-include" });
 
     if (comp.verbose_cc) print: {
-        std.debug.lockStdErr();
-        defer std.debug.unlockStdErr();
-        const stderr = std.fs.File.stderr().deprecatedWriter();
+        var stderr = std.debug.lockStderrWriter();
+        defer std.debug.unlockStderrWriter();
         nosuspend stderr.print("def file: {s}\n", .{def_file_path}) catch break :print;
         nosuspend stderr.print("include dir: {s}\n", .{include_dir}) catch break :print;
         nosuspend stderr.print("output path: {s}\n", .{def_final_path}) catch break :print;
@@ -410,9 +409,9 @@ fn findDef(
         // Try the archtecture-specific path first.
         const fmt_path = "libc" ++ s ++ "mingw" ++ s ++ "{s}" ++ s ++ "{s}.def";
         if (zig_lib_directory.path) |p| {
-            try override_path.writer().print("{s}" ++ s ++ fmt_path, .{ p, lib_path, lib_name });
+            try override_path.print("{s}" ++ s ++ fmt_path, .{ p, lib_path, lib_name });
         } else {
-            try override_path.writer().print(fmt_path, .{ lib_path, lib_name });
+            try override_path.print(fmt_path, .{ lib_path, lib_name });
         }
         if (std.fs.cwd().access(override_path.items, .{})) |_| {
             return override_path.toOwnedSlice();
@@ -427,9 +426,9 @@ fn findDef(
         override_path.shrinkRetainingCapacity(0);
         const fmt_path = "libc" ++ s ++ "mingw" ++ s ++ "lib-common" ++ s ++ "{s}.def";
         if (zig_lib_directory.path) |p| {
-            try override_path.writer().print("{s}" ++ s ++ fmt_path, .{ p, lib_name });
+            try override_path.print("{s}" ++ s ++ fmt_path, .{ p, lib_name });
         } else {
-            try override_path.writer().print(fmt_path, .{lib_name});
+            try override_path.print(fmt_path, .{lib_name});
         }
         if (std.fs.cwd().access(override_path.items, .{})) |_| {
             return override_path.toOwnedSlice();
@@ -444,9 +443,9 @@ fn findDef(
         override_path.shrinkRetainingCapacity(0);
         const fmt_path = "libc" ++ s ++ "mingw" ++ s ++ "lib-common" ++ s ++ "{s}.def.in";
         if (zig_lib_directory.path) |p| {
-            try override_path.writer().print("{s}" ++ s ++ fmt_path, .{ p, lib_name });
+            try override_path.print("{s}" ++ s ++ fmt_path, .{ p, lib_name });
         } else {
-            try override_path.writer().print(fmt_path, .{lib_name});
+            try override_path.print(fmt_path, .{lib_name});
         }
         if (std.fs.cwd().access(override_path.items, .{})) |_| {
             return override_path.toOwnedSlice();
src/Package/Fetch.zig
@@ -200,7 +200,7 @@ pub const JobQueue = struct {
 
             const hash_slice = hash.toSlice();
 
-            try buf.writer().print(
+            try buf.print(
                 \\    pub const {f} = struct {{
                 \\
             , .{std.zig.fmtId(hash_slice)});
@@ -226,13 +226,13 @@ pub const JobQueue = struct {
                 }
             }
 
-            try buf.writer().print(
+            try buf.print(
                 \\        pub const build_root = "{f}";
                 \\
             , .{std.fmt.alt(fetch.package_root, .formatEscapeString)});
 
             if (fetch.has_build_zig) {
-                try buf.writer().print(
+                try buf.print(
                     \\        pub const build_zig = @import("{f}");
                     \\
                 , .{std.zig.fmtString(hash_slice)});
@@ -245,7 +245,7 @@ pub const JobQueue = struct {
                 );
                 for (manifest.dependencies.keys(), manifest.dependencies.values()) |name, dep| {
                     const h = depDigest(fetch.package_root, jq.global_cache, dep) orelse continue;
-                    try buf.writer().print(
+                    try buf.print(
                         "            .{{ \"{f}\", \"{f}\" }},\n",
                         .{ std.zig.fmtString(name), std.zig.fmtString(h.toSlice()) },
                     );
@@ -277,7 +277,7 @@ pub const JobQueue = struct {
 
         for (root_manifest.dependencies.keys(), root_manifest.dependencies.values()) |name, dep| {
             const h = depDigest(root_fetch.package_root, jq.global_cache, dep) orelse continue;
-            try buf.writer().print(
+            try buf.print(
                 "    .{{ \"{f}\", \"{f}\" }},\n",
                 .{ std.zig.fmtString(name), std.zig.fmtString(h.toSlice()) },
             );
src/IncrementalDebugServer.zig
@@ -76,7 +76,9 @@ fn runThread(ids: *IncrementalDebugServer) void {
                 ids.mutex.lock();
             }
             defer ids.mutex.unlock();
-            handleCommand(ids.zcu, &text_out, cmd, arg) catch @panic("IncrementalDebugServer: out of memory");
+            var allocating: std.Io.Writer.Allocating = .fromArrayList(gpa, &text_out);
+            defer text_out = allocating.toArrayList();
+            handleCommand(ids.zcu, &allocating.writer, cmd, arg) catch @panic("IncrementalDebugServer: out of memory");
         }
         text_out.append(gpa, '\n') catch @panic("IncrementalDebugServer: out of memory");
         conn.stream.writeAll(text_out.items) catch @panic("IncrementalDebugServer: failed to write");
@@ -119,10 +121,8 @@ const help_str: []const u8 =
     \\
 ;
 
-fn handleCommand(zcu: *Zcu, output: *std.ArrayListUnmanaged(u8), cmd_str: []const u8, arg_str: []const u8) Allocator.Error!void {
+fn handleCommand(zcu: *Zcu, w: *std.Io.Writer, cmd_str: []const u8, arg_str: []const u8) error{ WriteFailed, OutOfMemory }!void {
     const ip = &zcu.intern_pool;
-    const gpa = zcu.gpa;
-    const w = output.writer(gpa);
     if (std.mem.eql(u8, cmd_str, "help")) {
         try w.writeAll(help_str);
     } else if (std.mem.eql(u8, cmd_str, "summary")) {
src/link.zig
@@ -1976,7 +1976,7 @@ fn resolveLibInput(
             .root_dir = lib_directory,
             .sub_path = try std.fmt.allocPrint(arena, "lib{s}.tbd", .{lib_name}),
         };
-        try checked_paths.writer(gpa).print("\n  {f}", .{test_path});
+        try checked_paths.print(gpa, "\n  {f}", .{test_path});
         var file = test_path.root_dir.handle.openFile(test_path.sub_path, .{}) catch |err| switch (err) {
             error.FileNotFound => break :tbd,
             else => |e| fatal("unable to search for tbd library '{f}': {s}", .{ test_path, @errorName(e) }),
@@ -1995,7 +1995,7 @@ fn resolveLibInput(
                 },
             }),
         };
-        try checked_paths.writer(gpa).print("\n  {f}", .{test_path});
+        try checked_paths.print(gpa, "\n  {f}", .{test_path});
         switch (try resolvePathInputLib(gpa, arena, unresolved_inputs, resolved_inputs, ld_script_bytes, target, .{
             .path = test_path,
             .query = name_query.query,
@@ -2012,7 +2012,7 @@ fn resolveLibInput(
             .root_dir = lib_directory,
             .sub_path = try std.fmt.allocPrint(arena, "lib{s}.so", .{lib_name}),
         };
-        try checked_paths.writer(gpa).print("\n  {f}", .{test_path});
+        try checked_paths.print(gpa, "\n  {f}", .{test_path});
         var file = test_path.root_dir.handle.openFile(test_path.sub_path, .{}) catch |err| switch (err) {
             error.FileNotFound => break :so,
             else => |e| fatal("unable to search for so library '{f}': {s}", .{
@@ -2030,7 +2030,7 @@ fn resolveLibInput(
             .root_dir = lib_directory,
             .sub_path = try std.fmt.allocPrint(arena, "lib{s}.a", .{lib_name}),
         };
-        try checked_paths.writer(gpa).print("\n  {f}", .{test_path});
+        try checked_paths.print(gpa, "\n  {f}", .{test_path});
         var file = test_path.root_dir.handle.openFile(test_path.sub_path, .{}) catch |err| switch (err) {
             error.FileNotFound => break :mingw,
             else => |e| fatal("unable to search for static library '{f}': {s}", .{ test_path, @errorName(e) }),