authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-08-21 16:50:55-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-08-28 18:30:57-07:00
log2151b10a41aff2b81dbbadf8f823d21d7b80f43b
tree6e90b89804e14e07d0acc183887496e93544db9d
parent379d7bc9f64e64818f31520700294f778aa13690

more updates to not use GenericWriter


5 files changed, 65 insertions(+), 52 deletions(-)

lib/compiler/aro/aro/Compilation.zig+1-1
......@@ -546,7 +546,7 @@ pub fn generateBuiltinMacros(comp: *Compilation, system_defines_mode: SystemDefi
546546 }
547547
548548 try buf.appendSlice("#define __STDC__ 1\n");
549 try buf.writer().print("#define __STDC_HOSTED__ {d}\n", .{@intFromBool(comp.target.os.tag != .freestanding)});
549 try buf.print("#define __STDC_HOSTED__ {d}\n", .{@intFromBool(comp.target.os.tag != .freestanding)});
550550
551551 // standard macros
552552 try buf.appendSlice(
src/arch/wasm/Emit.zig+46-34
......@@ -3,7 +3,7 @@ const Emit = @This();
33const std = @import("std");
44const assert = std.debug.assert;
55const Allocator = std.mem.Allocator;
6const leb = std.leb;
6const ArrayList = std.ArrayList;
77
88const Wasm = link.File.Wasm;
99const Mir = @import("Mir.zig");
......@@ -15,7 +15,7 @@ const codegen = @import("../../codegen.zig");
1515mir: Mir,
1616wasm: *Wasm,
1717/// The binary representation that will be emitted by this module.
18code: *std.ArrayListUnmanaged(u8),
18code: *ArrayList(u8),
1919
2020pub const Error = error{
2121 OutOfMemory,
......@@ -85,7 +85,7 @@ pub fn lowerToCode(emit: *Emit) Error!void {
8585 if (is_obj) {
8686 @panic("TODO");
8787 } else {
88 leb.writeUleb128(code.fixedWriter(), 1 + @intFromEnum(indirect_func_idx)) catch unreachable;
88 writeUleb128(code, 1 + @intFromEnum(indirect_func_idx));
8989 }
9090 inst += 1;
9191 continue :loop tags[inst];
......@@ -99,7 +99,7 @@ pub fn lowerToCode(emit: *Emit) Error!void {
9999 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_const));
100100 // MIR is lowered during flush, so there is indeed only one thread at this time.
101101 const errors_len = 1 + comp.zcu.?.intern_pool.global_error_set.getNamesFromMainThread().len;
102 leb.writeIleb128(code.fixedWriter(), errors_len) catch unreachable;
102 writeSleb128(code, errors_len);
103103
104104 inst += 1;
105105 continue :loop tags[inst];
......@@ -122,7 +122,7 @@ pub fn lowerToCode(emit: *Emit) Error!void {
122122 continue :loop tags[inst];
123123 } else {
124124 const addr: u32 = wasm.errorNameTableAddr();
125 leb.writeIleb128(code.fixedWriter(), addr) catch unreachable;
125 writeSleb128(code, addr);
126126
127127 inst += 1;
128128 continue :loop tags[inst];
......@@ -131,7 +131,7 @@ pub fn lowerToCode(emit: *Emit) Error!void {
131131 .br_if, .br, .memory_grow, .memory_size => {
132132 try code.ensureUnusedCapacity(gpa, 11);
133133 code.appendAssumeCapacity(@intFromEnum(tags[inst]));
134 leb.writeUleb128(code.fixedWriter(), datas[inst].label) catch unreachable;
134 writeUleb128(code, datas[inst].label);
135135
136136 inst += 1;
137137 continue :loop tags[inst];
......@@ -140,7 +140,7 @@ pub fn lowerToCode(emit: *Emit) Error!void {
140140 .local_get, .local_set, .local_tee => {
141141 try code.ensureUnusedCapacity(gpa, 11);
142142 code.appendAssumeCapacity(@intFromEnum(tags[inst]));
143 leb.writeUleb128(code.fixedWriter(), datas[inst].local) catch unreachable;
143 writeUleb128(code, datas[inst].local);
144144
145145 inst += 1;
146146 continue :loop tags[inst];
......@@ -153,8 +153,8 @@ pub fn lowerToCode(emit: *Emit) Error!void {
153153 try code.ensureUnusedCapacity(gpa, 11 + 10 * labels.len);
154154 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.br_table));
155155 // -1 because default label is not part of length/depth.
156 leb.writeUleb128(code.fixedWriter(), extra.data.length - 1) catch unreachable;
157 for (labels) |label| leb.writeUleb128(code.fixedWriter(), label) catch unreachable;
156 writeUleb128(code, extra.data.length - 1);
157 for (labels) |label| writeUleb128(code, label);
158158
159159 inst += 1;
160160 continue :loop tags[inst];
......@@ -199,9 +199,9 @@ pub fn lowerToCode(emit: *Emit) Error!void {
199199 code.appendNTimesAssumeCapacity(0, 5);
200200 } else {
201201 const index: Wasm.Flush.FuncTypeIndex = .fromTypeIndex(func_ty_index, &wasm.flush_buffer);
202 leb.writeUleb128(code.fixedWriter(), @intFromEnum(index)) catch unreachable;
202 writeUleb128(code, @intFromEnum(index));
203203 }
204 leb.writeUleb128(code.fixedWriter(), @as(u32, 0)) catch unreachable; // table index
204 writeUleb128(code, @as(u32, 0)); // table index
205205
206206 inst += 1;
207207 continue :loop tags[inst];
......@@ -263,7 +263,7 @@ pub fn lowerToCode(emit: *Emit) Error!void {
263263 code.appendNTimesAssumeCapacity(0, 5);
264264 } else {
265265 const sp_global: Wasm.GlobalIndex = .stack_pointer;
266 std.leb.writeUleb128(code.fixedWriter(), @intFromEnum(sp_global)) catch unreachable;
266 writeUleb128(code, @intFromEnum(sp_global));
267267 }
268268
269269 inst += 1;
......@@ -291,7 +291,7 @@ pub fn lowerToCode(emit: *Emit) Error!void {
291291 .i32_const => {
292292 try code.ensureUnusedCapacity(gpa, 6);
293293 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_const));
294 leb.writeIleb128(code.fixedWriter(), datas[inst].imm32) catch unreachable;
294 writeSleb128(code, datas[inst].imm32);
295295
296296 inst += 1;
297297 continue :loop tags[inst];
......@@ -300,7 +300,7 @@ pub fn lowerToCode(emit: *Emit) Error!void {
300300 try code.ensureUnusedCapacity(gpa, 11);
301301 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i64_const));
302302 const int64: i64 = @bitCast(mir.extraData(Mir.Imm64, datas[inst].payload).data.toInt());
303 leb.writeIleb128(code.fixedWriter(), int64) catch unreachable;
303 writeSleb128(code, int64);
304304
305305 inst += 1;
306306 continue :loop tags[inst];
......@@ -476,33 +476,33 @@ pub fn lowerToCode(emit: *Emit) Error!void {
476476 const extra_index = datas[inst].payload;
477477 const opcode = mir.extra[extra_index];
478478 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.misc_prefix));
479 leb.writeUleb128(code.fixedWriter(), opcode) catch unreachable;
479 writeUleb128(code, opcode);
480480 switch (@as(std.wasm.MiscOpcode, @enumFromInt(opcode))) {
481481 // bulk-memory opcodes
482482 .data_drop => {
483483 const segment = mir.extra[extra_index + 1];
484 leb.writeUleb128(code.fixedWriter(), segment) catch unreachable;
484 writeUleb128(code, segment);
485485
486486 inst += 1;
487487 continue :loop tags[inst];
488488 },
489489 .memory_init => {
490490 const segment = mir.extra[extra_index + 1];
491 leb.writeUleb128(code.fixedWriter(), segment) catch unreachable;
492 leb.writeUleb128(code.fixedWriter(), @as(u32, 0)) catch unreachable; // memory index
491 writeUleb128(code, segment);
492 writeUleb128(code, @as(u32, 0)); // memory index
493493
494494 inst += 1;
495495 continue :loop tags[inst];
496496 },
497497 .memory_fill => {
498 leb.writeUleb128(code.fixedWriter(), @as(u32, 0)) catch unreachable; // memory index
498 writeUleb128(code, @as(u32, 0)); // memory index
499499
500500 inst += 1;
501501 continue :loop tags[inst];
502502 },
503503 .memory_copy => {
504 leb.writeUleb128(code.fixedWriter(), @as(u32, 0)) catch unreachable; // dst memory index
505 leb.writeUleb128(code.fixedWriter(), @as(u32, 0)) catch unreachable; // src memory index
504 writeUleb128(code, @as(u32, 0)); // dst memory index
505 writeUleb128(code, @as(u32, 0)); // src memory index
506506
507507 inst += 1;
508508 continue :loop tags[inst];
......@@ -538,7 +538,7 @@ pub fn lowerToCode(emit: *Emit) Error!void {
538538 const extra_index = datas[inst].payload;
539539 const opcode = mir.extra[extra_index];
540540 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.simd_prefix));
541 leb.writeUleb128(code.fixedWriter(), opcode) catch unreachable;
541 writeUleb128(code, opcode);
542542 switch (@as(std.wasm.SimdOpcode, @enumFromInt(opcode))) {
543543 .v128_store,
544544 .v128_load,
......@@ -824,7 +824,7 @@ pub fn lowerToCode(emit: *Emit) Error!void {
824824 const extra_index = datas[inst].payload;
825825 const opcode = mir.extra[extra_index];
826826 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.atomics_prefix));
827 leb.writeUleb128(code.fixedWriter(), opcode) catch unreachable;
827 writeUleb128(code, opcode);
828828 switch (@as(std.wasm.AtomicsOpcode, @enumFromInt(opcode))) {
829829 .i32_atomic_load,
830830 .i64_atomic_load,
......@@ -900,7 +900,7 @@ pub fn lowerToCode(emit: *Emit) Error!void {
900900 // Hard-codes memory index 0 since multi-memory proposal is
901901 // not yet accepted nor implemented.
902902 const memory_index: u32 = 0;
903 leb.writeUleb128(code.fixedWriter(), memory_index) catch unreachable;
903 writeUleb128(code, memory_index);
904904 inst += 1;
905905 continue :loop tags[inst];
906906 },
......@@ -915,15 +915,15 @@ pub fn lowerToCode(emit: *Emit) Error!void {
915915}
916916
917917/// Asserts 20 unused capacity.
918fn encodeMemArg(code: *std.ArrayListUnmanaged(u8), mem_arg: Mir.MemArg) void {
918fn encodeMemArg(code: *ArrayList(u8), mem_arg: Mir.MemArg) void {
919919 assert(code.unusedCapacitySlice().len >= 20);
920920 // Wasm encodes alignment as power of 2, rather than natural alignment.
921921 const encoded_alignment = @ctz(mem_arg.alignment);
922 leb.writeUleb128(code.fixedWriter(), encoded_alignment) catch unreachable;
923 leb.writeUleb128(code.fixedWriter(), mem_arg.offset) catch unreachable;
922 writeUleb128(code, encoded_alignment);
923 writeUleb128(code, mem_arg.offset);
924924}
925925
926fn uavRefObj(wasm: *Wasm, code: *std.ArrayListUnmanaged(u8), value: InternPool.Index, offset: i32, is_wasm32: bool) !void {
926fn uavRefObj(wasm: *Wasm, code: *ArrayList(u8), value: InternPool.Index, offset: i32, is_wasm32: bool) !void {
927927 const comp = wasm.base.comp;
928928 const gpa = comp.gpa;
929929 const opcode: std.wasm.Opcode = if (is_wasm32) .i32_const else .i64_const;
......@@ -940,7 +940,7 @@ fn uavRefObj(wasm: *Wasm, code: *std.ArrayListUnmanaged(u8), value: InternPool.I
940940 code.appendNTimesAssumeCapacity(0, if (is_wasm32) 5 else 10);
941941}
942942
943fn uavRefExe(wasm: *Wasm, code: *std.ArrayListUnmanaged(u8), value: InternPool.Index, offset: i32, is_wasm32: bool) !void {
943fn uavRefExe(wasm: *Wasm, code: *ArrayList(u8), value: InternPool.Index, offset: i32, is_wasm32: bool) !void {
944944 const comp = wasm.base.comp;
945945 const gpa = comp.gpa;
946946 const opcode: std.wasm.Opcode = if (is_wasm32) .i32_const else .i64_const;
......@@ -949,10 +949,10 @@ fn uavRefExe(wasm: *Wasm, code: *std.ArrayListUnmanaged(u8), value: InternPool.I
949949 code.appendAssumeCapacity(@intFromEnum(opcode));
950950
951951 const addr = wasm.uavAddr(value);
952 leb.writeUleb128(code.fixedWriter(), @as(u32, @intCast(@as(i64, addr) + offset))) catch unreachable;
952 writeUleb128(code, @as(u32, @intCast(@as(i64, addr) + offset)));
953953}
954954
955fn navRefOff(wasm: *Wasm, code: *std.ArrayListUnmanaged(u8), data: Mir.NavRefOff, is_wasm32: bool) !void {
955fn navRefOff(wasm: *Wasm, code: *ArrayList(u8), data: Mir.NavRefOff, is_wasm32: bool) !void {
956956 const comp = wasm.base.comp;
957957 const zcu = comp.zcu.?;
958958 const ip = &zcu.intern_pool;
......@@ -975,10 +975,22 @@ fn navRefOff(wasm: *Wasm, code: *std.ArrayListUnmanaged(u8), data: Mir.NavRefOff
975975 code.appendNTimesAssumeCapacity(0, if (is_wasm32) 5 else 10);
976976 } else {
977977 const addr = wasm.navAddr(data.nav_index);
978 leb.writeUleb128(code.fixedWriter(), @as(u32, @intCast(@as(i64, addr) + data.offset))) catch unreachable;
978 writeUleb128(code, @as(u32, @intCast(@as(i64, addr) + data.offset)));
979979 }
980980}
981981
982fn appendOutputFunctionIndex(code: *std.ArrayListUnmanaged(u8), i: Wasm.OutputFunctionIndex) void {
983 leb.writeUleb128(code.fixedWriter(), @intFromEnum(i)) catch unreachable;
982fn appendOutputFunctionIndex(code: *ArrayList(u8), i: Wasm.OutputFunctionIndex) void {
983 writeUleb128(code, @intFromEnum(i));
984}
985
986fn writeUleb128(code: *ArrayList(u8), arg: anytype) void {
987 var w: std.Io.Writer = .fixed(code.unusedCapacitySlice());
988 w.writeUleb128(arg) catch unreachable;
989 code.items.len += w.end;
990}
991
992fn writeSleb128(code: *ArrayList(u8), arg: anytype) void {
993 var w: std.Io.Writer = .fixed(code.unusedCapacitySlice());
994 w.writeSleb128(arg) catch unreachable;
995 code.items.len += w.end;
984996}
src/codegen.zig+13-12
......@@ -6,6 +6,7 @@ const link = @import("link.zig");
66const log = std.log.scoped(.codegen);
77const mem = std.mem;
88const math = std.math;
9const ArrayList = std.ArrayList;
910const target_util = @import("target.zig");
1011const trace = @import("tracy.zig").trace;
1112
......@@ -179,7 +180,7 @@ pub fn emitFunction(
179180 src_loc: Zcu.LazySrcLoc,
180181 func_index: InternPool.Index,
181182 any_mir: *const AnyMir,
182 code: *std.ArrayListUnmanaged(u8),
183 code: *ArrayList(u8),
183184 debug_output: link.File.DebugInfoOutput,
184185) CodeGenError!void {
185186 const zcu = pt.zcu;
......@@ -204,7 +205,7 @@ pub fn generateLazyFunction(
204205 pt: Zcu.PerThread,
205206 src_loc: Zcu.LazySrcLoc,
206207 lazy_sym: link.File.LazySymbol,
207 code: *std.ArrayListUnmanaged(u8),
208 code: *ArrayList(u8),
208209 debug_output: link.File.DebugInfoOutput,
209210) CodeGenError!void {
210211 const zcu = pt.zcu;
......@@ -236,7 +237,7 @@ pub fn generateLazySymbol(
236237 lazy_sym: link.File.LazySymbol,
237238 // TODO don't use an "out" parameter like this; put it in the result instead
238239 alignment: *Alignment,
239 code: *std.ArrayListUnmanaged(u8),
240 code: *ArrayList(u8),
240241 debug_output: link.File.DebugInfoOutput,
241242 reloc_parent: link.File.RelocInfo.Parent,
242243) CodeGenError!void {
......@@ -311,7 +312,7 @@ pub fn generateSymbol(
311312 pt: Zcu.PerThread,
312313 src_loc: Zcu.LazySrcLoc,
313314 val: Value,
314 code: *std.ArrayListUnmanaged(u8),
315 code: *ArrayList(u8),
315316 reloc_parent: link.File.RelocInfo.Parent,
316317) GenerateSymbolError!void {
317318 const tracy = trace(@src());
......@@ -379,7 +380,7 @@ pub fn generateSymbol(
379380 },
380381 .err => |err| {
381382 const int = try pt.getErrorValue(err.name);
382 try code.writer(gpa).writeInt(u16, @intCast(int), endian);
383 mem.writeInt(u16, try code.addManyAsArray(gpa, 2), @intCast(int), endian);
383384 },
384385 .error_union => |error_union| {
385386 const payload_ty = ty.errorUnionPayload(zcu);
......@@ -389,7 +390,7 @@ pub fn generateSymbol(
389390 };
390391
391392 if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) {
392 try code.writer(gpa).writeInt(u16, err_val, endian);
393 mem.writeInt(u16, try code.addManyAsArray(gpa, 2), err_val, endian);
393394 return;
394395 }
395396
......@@ -399,7 +400,7 @@ pub fn generateSymbol(
399400
400401 // error value first when its type is larger than the error union's payload
401402 if (error_align.order(payload_align) == .gt) {
402 try code.writer(gpa).writeInt(u16, err_val, endian);
403 mem.writeInt(u16, try code.addManyAsArray(gpa, 2), err_val, endian);
403404 }
404405
405406 // emit payload part of the error union
......@@ -421,7 +422,7 @@ pub fn generateSymbol(
421422 // Payload size is larger than error set, so emit our error set last
422423 if (error_align.compare(.lte, payload_align)) {
423424 const begin = code.items.len;
424 try code.writer(gpa).writeInt(u16, err_val, endian);
425 mem.writeInt(u16, try code.addManyAsArray(gpa, 2), err_val, endian);
425426 const unpadded_end = code.items.len - begin;
426427 const padded_end = abi_align.forward(unpadded_end);
427428 const padding = math.cast(usize, padded_end - unpadded_end) orelse return error.Overflow;
......@@ -476,7 +477,7 @@ pub fn generateSymbol(
476477 }));
477478 try generateSymbol(bin_file, pt, src_loc, value, code, reloc_parent);
478479 }
479 try code.writer(gpa).writeByte(@intFromBool(payload_val != null));
480 try code.append(gpa, @intFromBool(payload_val != null));
480481 try code.appendNTimes(gpa, 0, padding);
481482 }
482483 },
......@@ -721,7 +722,7 @@ fn lowerPtr(
721722 pt: Zcu.PerThread,
722723 src_loc: Zcu.LazySrcLoc,
723724 ptr_val: InternPool.Index,
724 code: *std.ArrayListUnmanaged(u8),
725 code: *ArrayList(u8),
725726 reloc_parent: link.File.RelocInfo.Parent,
726727 prev_offset: u64,
727728) GenerateSymbolError!void {
......@@ -774,7 +775,7 @@ fn lowerUavRef(
774775 pt: Zcu.PerThread,
775776 src_loc: Zcu.LazySrcLoc,
776777 uav: InternPool.Key.Ptr.BaseAddr.Uav,
777 code: *std.ArrayListUnmanaged(u8),
778 code: *ArrayList(u8),
778779 reloc_parent: link.File.RelocInfo.Parent,
779780 offset: u64,
780781) GenerateSymbolError!void {
......@@ -834,7 +835,7 @@ fn lowerNavRef(
834835 lf: *link.File,
835836 pt: Zcu.PerThread,
836837 nav_index: InternPool.Nav.Index,
837 code: *std.ArrayListUnmanaged(u8),
838 code: *ArrayList(u8),
838839 reloc_parent: link.File.RelocInfo.Parent,
839840 offset: u64,
840841) GenerateSymbolError!void {
src/libs/mingw.zig+1-1
......@@ -304,7 +304,7 @@ 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 var stderr = std.debug.lockStderrWriter();
307 var stderr = std.debug.lockStderrWriter(&.{});
308308 defer std.debug.unlockStderrWriter();
309309 nosuspend stderr.print("def file: {s}\n", .{def_file_path}) catch break :print;
310310 nosuspend stderr.print("include dir: {s}\n", .{include_dir}) catch break :print;
src/link/Coff.zig+4-4
......@@ -2179,13 +2179,13 @@ fn writeDataDirectoriesHeaders(coff: *Coff) !void {
21792179fn writeHeader(coff: *Coff) !void {
21802180 const target = &coff.base.comp.root_mod.resolved_target.result;
21812181 const gpa = coff.base.comp.gpa;
2182 var buffer = std.array_list.Managed(u8).init(gpa);
2182 var buffer: std.Io.Writer.Allocating = .init(gpa);
21832183 defer buffer.deinit();
2184 const writer = buffer.writer();
2184 const writer = &buffer.writer;
21852185
21862186 try buffer.ensureTotalCapacity(coff.getSizeOfHeaders());
21872187 writer.writeAll(&msdos_stub) catch unreachable;
2188 mem.writeInt(u32, buffer.items[0x3c..][0..4], msdos_stub.len, .little);
2188 mem.writeInt(u32, buffer.writer.buffer[0x3c..][0..4], msdos_stub.len, .little);
21892189
21902190 writer.writeAll("PE\x00\x00") catch unreachable;
21912191 var flags = coff_util.CoffHeaderFlags{
......@@ -2313,7 +2313,7 @@ fn writeHeader(coff: *Coff) !void {
23132313 },
23142314 }
23152315
2316 try coff.pwriteAll(buffer.items, 0);
2316 try coff.pwriteAll(buffer.written(), 0);
23172317}
23182318
23192319pub fn padToIdeal(actual_size: anytype) @TypeOf(actual_size) {