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...@@ -546,7 +546,7 @@ pub fn generateBuiltinMacros(comp: *Compilation, system_defines_mode: SystemDefi
546 }546 }
547547
548 try buf.appendSlice("#define __STDC__ 1\n");548 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
551 // standard macros551 // standard macros
552 try buf.appendSlice(552 try buf.appendSlice(
src/arch/wasm/Emit.zig+46-34
...@@ -3,7 +3,7 @@ const Emit = @This();...@@ -3,7 +3,7 @@ const Emit = @This();
3const std = @import("std");3const std = @import("std");
4const assert = std.debug.assert;4const assert = std.debug.assert;
5const Allocator = std.mem.Allocator;5const Allocator = std.mem.Allocator;
6const leb = std.leb;6const ArrayList = std.ArrayList;
77
8const Wasm = link.File.Wasm;8const Wasm = link.File.Wasm;
9const Mir = @import("Mir.zig");9const Mir = @import("Mir.zig");
...@@ -15,7 +15,7 @@ const codegen = @import("../../codegen.zig");...@@ -15,7 +15,7 @@ const codegen = @import("../../codegen.zig");
15mir: Mir,15mir: Mir,
16wasm: *Wasm,16wasm: *Wasm,
17/// The binary representation that will be emitted by this module.17/// The binary representation that will be emitted by this module.
18code: *std.ArrayListUnmanaged(u8),18code: *ArrayList(u8),
1919
20pub const Error = error{20pub const Error = error{
21 OutOfMemory,21 OutOfMemory,
...@@ -85,7 +85,7 @@ pub fn lowerToCode(emit: *Emit) Error!void {...@@ -85,7 +85,7 @@ pub fn lowerToCode(emit: *Emit) Error!void {
85 if (is_obj) {85 if (is_obj) {
86 @panic("TODO");86 @panic("TODO");
87 } else {87 } else {
88 leb.writeUleb128(code.fixedWriter(), 1 + @intFromEnum(indirect_func_idx)) catch unreachable;88 writeUleb128(code, 1 + @intFromEnum(indirect_func_idx));
89 }89 }
90 inst += 1;90 inst += 1;
91 continue :loop tags[inst];91 continue :loop tags[inst];
...@@ -99,7 +99,7 @@ pub fn lowerToCode(emit: *Emit) Error!void {...@@ -99,7 +99,7 @@ pub fn lowerToCode(emit: *Emit) Error!void {
99 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_const));99 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_const));
100 // MIR is lowered during flush, so there is indeed only one thread at this time.100 // MIR is lowered during flush, so there is indeed only one thread at this time.
101 const errors_len = 1 + comp.zcu.?.intern_pool.global_error_set.getNamesFromMainThread().len;101 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
104 inst += 1;104 inst += 1;
105 continue :loop tags[inst];105 continue :loop tags[inst];
...@@ -122,7 +122,7 @@ pub fn lowerToCode(emit: *Emit) Error!void {...@@ -122,7 +122,7 @@ pub fn lowerToCode(emit: *Emit) Error!void {
122 continue :loop tags[inst];122 continue :loop tags[inst];
123 } else {123 } else {
124 const addr: u32 = wasm.errorNameTableAddr();124 const addr: u32 = wasm.errorNameTableAddr();
125 leb.writeIleb128(code.fixedWriter(), addr) catch unreachable;125 writeSleb128(code, addr);
126126
127 inst += 1;127 inst += 1;
128 continue :loop tags[inst];128 continue :loop tags[inst];
...@@ -131,7 +131,7 @@ pub fn lowerToCode(emit: *Emit) Error!void {...@@ -131,7 +131,7 @@ pub fn lowerToCode(emit: *Emit) Error!void {
131 .br_if, .br, .memory_grow, .memory_size => {131 .br_if, .br, .memory_grow, .memory_size => {
132 try code.ensureUnusedCapacity(gpa, 11);132 try code.ensureUnusedCapacity(gpa, 11);
133 code.appendAssumeCapacity(@intFromEnum(tags[inst]));133 code.appendAssumeCapacity(@intFromEnum(tags[inst]));
134 leb.writeUleb128(code.fixedWriter(), datas[inst].label) catch unreachable;134 writeUleb128(code, datas[inst].label);
135135
136 inst += 1;136 inst += 1;
137 continue :loop tags[inst];137 continue :loop tags[inst];
...@@ -140,7 +140,7 @@ pub fn lowerToCode(emit: *Emit) Error!void {...@@ -140,7 +140,7 @@ pub fn lowerToCode(emit: *Emit) Error!void {
140 .local_get, .local_set, .local_tee => {140 .local_get, .local_set, .local_tee => {
141 try code.ensureUnusedCapacity(gpa, 11);141 try code.ensureUnusedCapacity(gpa, 11);
142 code.appendAssumeCapacity(@intFromEnum(tags[inst]));142 code.appendAssumeCapacity(@intFromEnum(tags[inst]));
143 leb.writeUleb128(code.fixedWriter(), datas[inst].local) catch unreachable;143 writeUleb128(code, datas[inst].local);
144144
145 inst += 1;145 inst += 1;
146 continue :loop tags[inst];146 continue :loop tags[inst];
...@@ -153,8 +153,8 @@ pub fn lowerToCode(emit: *Emit) Error!void {...@@ -153,8 +153,8 @@ pub fn lowerToCode(emit: *Emit) Error!void {
153 try code.ensureUnusedCapacity(gpa, 11 + 10 * labels.len);153 try code.ensureUnusedCapacity(gpa, 11 + 10 * labels.len);
154 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.br_table));154 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.br_table));
155 // -1 because default label is not part of length/depth.155 // -1 because default label is not part of length/depth.
156 leb.writeUleb128(code.fixedWriter(), extra.data.length - 1) catch unreachable;156 writeUleb128(code, extra.data.length - 1);
157 for (labels) |label| leb.writeUleb128(code.fixedWriter(), label) catch unreachable;157 for (labels) |label| writeUleb128(code, label);
158158
159 inst += 1;159 inst += 1;
160 continue :loop tags[inst];160 continue :loop tags[inst];
...@@ -199,9 +199,9 @@ pub fn lowerToCode(emit: *Emit) Error!void {...@@ -199,9 +199,9 @@ pub fn lowerToCode(emit: *Emit) Error!void {
199 code.appendNTimesAssumeCapacity(0, 5);199 code.appendNTimesAssumeCapacity(0, 5);
200 } else {200 } else {
201 const index: Wasm.Flush.FuncTypeIndex = .fromTypeIndex(func_ty_index, &wasm.flush_buffer);201 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));
203 }203 }
204 leb.writeUleb128(code.fixedWriter(), @as(u32, 0)) catch unreachable; // table index204 writeUleb128(code, @as(u32, 0)); // table index
205205
206 inst += 1;206 inst += 1;
207 continue :loop tags[inst];207 continue :loop tags[inst];
...@@ -263,7 +263,7 @@ pub fn lowerToCode(emit: *Emit) Error!void {...@@ -263,7 +263,7 @@ pub fn lowerToCode(emit: *Emit) Error!void {
263 code.appendNTimesAssumeCapacity(0, 5);263 code.appendNTimesAssumeCapacity(0, 5);
264 } else {264 } else {
265 const sp_global: Wasm.GlobalIndex = .stack_pointer;265 const sp_global: Wasm.GlobalIndex = .stack_pointer;
266 std.leb.writeUleb128(code.fixedWriter(), @intFromEnum(sp_global)) catch unreachable;266 writeUleb128(code, @intFromEnum(sp_global));
267 }267 }
268268
269 inst += 1;269 inst += 1;
...@@ -291,7 +291,7 @@ pub fn lowerToCode(emit: *Emit) Error!void {...@@ -291,7 +291,7 @@ pub fn lowerToCode(emit: *Emit) Error!void {
291 .i32_const => {291 .i32_const => {
292 try code.ensureUnusedCapacity(gpa, 6);292 try code.ensureUnusedCapacity(gpa, 6);
293 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i32_const));293 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
296 inst += 1;296 inst += 1;
297 continue :loop tags[inst];297 continue :loop tags[inst];
...@@ -300,7 +300,7 @@ pub fn lowerToCode(emit: *Emit) Error!void {...@@ -300,7 +300,7 @@ pub fn lowerToCode(emit: *Emit) Error!void {
300 try code.ensureUnusedCapacity(gpa, 11);300 try code.ensureUnusedCapacity(gpa, 11);
301 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i64_const));301 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.i64_const));
302 const int64: i64 = @bitCast(mir.extraData(Mir.Imm64, datas[inst].payload).data.toInt());302 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
305 inst += 1;305 inst += 1;
306 continue :loop tags[inst];306 continue :loop tags[inst];
...@@ -476,33 +476,33 @@ pub fn lowerToCode(emit: *Emit) Error!void {...@@ -476,33 +476,33 @@ pub fn lowerToCode(emit: *Emit) Error!void {
476 const extra_index = datas[inst].payload;476 const extra_index = datas[inst].payload;
477 const opcode = mir.extra[extra_index];477 const opcode = mir.extra[extra_index];
478 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.misc_prefix));478 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.misc_prefix));
479 leb.writeUleb128(code.fixedWriter(), opcode) catch unreachable;479 writeUleb128(code, opcode);
480 switch (@as(std.wasm.MiscOpcode, @enumFromInt(opcode))) {480 switch (@as(std.wasm.MiscOpcode, @enumFromInt(opcode))) {
481 // bulk-memory opcodes481 // bulk-memory opcodes
482 .data_drop => {482 .data_drop => {
483 const segment = mir.extra[extra_index + 1];483 const segment = mir.extra[extra_index + 1];
484 leb.writeUleb128(code.fixedWriter(), segment) catch unreachable;484 writeUleb128(code, segment);
485485
486 inst += 1;486 inst += 1;
487 continue :loop tags[inst];487 continue :loop tags[inst];
488 },488 },
489 .memory_init => {489 .memory_init => {
490 const segment = mir.extra[extra_index + 1];490 const segment = mir.extra[extra_index + 1];
491 leb.writeUleb128(code.fixedWriter(), segment) catch unreachable;491 writeUleb128(code, segment);
492 leb.writeUleb128(code.fixedWriter(), @as(u32, 0)) catch unreachable; // memory index492 writeUleb128(code, @as(u32, 0)); // memory index
493493
494 inst += 1;494 inst += 1;
495 continue :loop tags[inst];495 continue :loop tags[inst];
496 },496 },
497 .memory_fill => {497 .memory_fill => {
498 leb.writeUleb128(code.fixedWriter(), @as(u32, 0)) catch unreachable; // memory index498 writeUleb128(code, @as(u32, 0)); // memory index
499499
500 inst += 1;500 inst += 1;
501 continue :loop tags[inst];501 continue :loop tags[inst];
502 },502 },
503 .memory_copy => {503 .memory_copy => {
504 leb.writeUleb128(code.fixedWriter(), @as(u32, 0)) catch unreachable; // dst memory index504 writeUleb128(code, @as(u32, 0)); // dst memory index
505 leb.writeUleb128(code.fixedWriter(), @as(u32, 0)) catch unreachable; // src memory index505 writeUleb128(code, @as(u32, 0)); // src memory index
506506
507 inst += 1;507 inst += 1;
508 continue :loop tags[inst];508 continue :loop tags[inst];
...@@ -538,7 +538,7 @@ pub fn lowerToCode(emit: *Emit) Error!void {...@@ -538,7 +538,7 @@ pub fn lowerToCode(emit: *Emit) Error!void {
538 const extra_index = datas[inst].payload;538 const extra_index = datas[inst].payload;
539 const opcode = mir.extra[extra_index];539 const opcode = mir.extra[extra_index];
540 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.simd_prefix));540 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.simd_prefix));
541 leb.writeUleb128(code.fixedWriter(), opcode) catch unreachable;541 writeUleb128(code, opcode);
542 switch (@as(std.wasm.SimdOpcode, @enumFromInt(opcode))) {542 switch (@as(std.wasm.SimdOpcode, @enumFromInt(opcode))) {
543 .v128_store,543 .v128_store,
544 .v128_load,544 .v128_load,
...@@ -824,7 +824,7 @@ pub fn lowerToCode(emit: *Emit) Error!void {...@@ -824,7 +824,7 @@ pub fn lowerToCode(emit: *Emit) Error!void {
824 const extra_index = datas[inst].payload;824 const extra_index = datas[inst].payload;
825 const opcode = mir.extra[extra_index];825 const opcode = mir.extra[extra_index];
826 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.atomics_prefix));826 code.appendAssumeCapacity(@intFromEnum(std.wasm.Opcode.atomics_prefix));
827 leb.writeUleb128(code.fixedWriter(), opcode) catch unreachable;827 writeUleb128(code, opcode);
828 switch (@as(std.wasm.AtomicsOpcode, @enumFromInt(opcode))) {828 switch (@as(std.wasm.AtomicsOpcode, @enumFromInt(opcode))) {
829 .i32_atomic_load,829 .i32_atomic_load,
830 .i64_atomic_load,830 .i64_atomic_load,
...@@ -900,7 +900,7 @@ pub fn lowerToCode(emit: *Emit) Error!void {...@@ -900,7 +900,7 @@ pub fn lowerToCode(emit: *Emit) Error!void {
900 // Hard-codes memory index 0 since multi-memory proposal is900 // Hard-codes memory index 0 since multi-memory proposal is
901 // not yet accepted nor implemented.901 // not yet accepted nor implemented.
902 const memory_index: u32 = 0;902 const memory_index: u32 = 0;
903 leb.writeUleb128(code.fixedWriter(), memory_index) catch unreachable;903 writeUleb128(code, memory_index);
904 inst += 1;904 inst += 1;
905 continue :loop tags[inst];905 continue :loop tags[inst];
906 },906 },
...@@ -915,15 +915,15 @@ pub fn lowerToCode(emit: *Emit) Error!void {...@@ -915,15 +915,15 @@ pub fn lowerToCode(emit: *Emit) Error!void {
915}915}
916916
917/// Asserts 20 unused capacity.917/// 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 {
919 assert(code.unusedCapacitySlice().len >= 20);919 assert(code.unusedCapacitySlice().len >= 20);
920 // Wasm encodes alignment as power of 2, rather than natural alignment.920 // Wasm encodes alignment as power of 2, rather than natural alignment.
921 const encoded_alignment = @ctz(mem_arg.alignment);921 const encoded_alignment = @ctz(mem_arg.alignment);
922 leb.writeUleb128(code.fixedWriter(), encoded_alignment) catch unreachable;922 writeUleb128(code, encoded_alignment);
923 leb.writeUleb128(code.fixedWriter(), mem_arg.offset) catch unreachable;923 writeUleb128(code, mem_arg.offset);
924}924}
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 {
927 const comp = wasm.base.comp;927 const comp = wasm.base.comp;
928 const gpa = comp.gpa;928 const gpa = comp.gpa;
929 const opcode: std.wasm.Opcode = if (is_wasm32) .i32_const else .i64_const;929 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...@@ -940,7 +940,7 @@ fn uavRefObj(wasm: *Wasm, code: *std.ArrayListUnmanaged(u8), value: InternPool.I
940 code.appendNTimesAssumeCapacity(0, if (is_wasm32) 5 else 10);940 code.appendNTimesAssumeCapacity(0, if (is_wasm32) 5 else 10);
941}941}
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 {
944 const comp = wasm.base.comp;944 const comp = wasm.base.comp;
945 const gpa = comp.gpa;945 const gpa = comp.gpa;
946 const opcode: std.wasm.Opcode = if (is_wasm32) .i32_const else .i64_const;946 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...@@ -949,10 +949,10 @@ fn uavRefExe(wasm: *Wasm, code: *std.ArrayListUnmanaged(u8), value: InternPool.I
949 code.appendAssumeCapacity(@intFromEnum(opcode));949 code.appendAssumeCapacity(@intFromEnum(opcode));
950950
951 const addr = wasm.uavAddr(value);951 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)));
953}953}
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 {
956 const comp = wasm.base.comp;956 const comp = wasm.base.comp;
957 const zcu = comp.zcu.?;957 const zcu = comp.zcu.?;
958 const ip = &zcu.intern_pool;958 const ip = &zcu.intern_pool;
...@@ -975,10 +975,22 @@ fn navRefOff(wasm: *Wasm, code: *std.ArrayListUnmanaged(u8), data: Mir.NavRefOff...@@ -975,10 +975,22 @@ fn navRefOff(wasm: *Wasm, code: *std.ArrayListUnmanaged(u8), data: Mir.NavRefOff
975 code.appendNTimesAssumeCapacity(0, if (is_wasm32) 5 else 10);975 code.appendNTimesAssumeCapacity(0, if (is_wasm32) 5 else 10);
976 } else {976 } else {
977 const addr = wasm.navAddr(data.nav_index);977 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)));
979 }979 }
980}980}
981981
982fn appendOutputFunctionIndex(code: *std.ArrayListUnmanaged(u8), i: Wasm.OutputFunctionIndex) void {982fn appendOutputFunctionIndex(code: *ArrayList(u8), i: Wasm.OutputFunctionIndex) void {
983 leb.writeUleb128(code.fixedWriter(), @intFromEnum(i)) catch unreachable;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;
984}996}
src/codegen.zig+13-12
...@@ -6,6 +6,7 @@ const link = @import("link.zig");...@@ -6,6 +6,7 @@ const link = @import("link.zig");
6const log = std.log.scoped(.codegen);6const log = std.log.scoped(.codegen);
7const mem = std.mem;7const mem = std.mem;
8const math = std.math;8const math = std.math;
9const ArrayList = std.ArrayList;
9const target_util = @import("target.zig");10const target_util = @import("target.zig");
10const trace = @import("tracy.zig").trace;11const trace = @import("tracy.zig").trace;
1112
...@@ -179,7 +180,7 @@ pub fn emitFunction(...@@ -179,7 +180,7 @@ pub fn emitFunction(
179 src_loc: Zcu.LazySrcLoc,180 src_loc: Zcu.LazySrcLoc,
180 func_index: InternPool.Index,181 func_index: InternPool.Index,
181 any_mir: *const AnyMir,182 any_mir: *const AnyMir,
182 code: *std.ArrayListUnmanaged(u8),183 code: *ArrayList(u8),
183 debug_output: link.File.DebugInfoOutput,184 debug_output: link.File.DebugInfoOutput,
184) CodeGenError!void {185) CodeGenError!void {
185 const zcu = pt.zcu;186 const zcu = pt.zcu;
...@@ -204,7 +205,7 @@ pub fn generateLazyFunction(...@@ -204,7 +205,7 @@ pub fn generateLazyFunction(
204 pt: Zcu.PerThread,205 pt: Zcu.PerThread,
205 src_loc: Zcu.LazySrcLoc,206 src_loc: Zcu.LazySrcLoc,
206 lazy_sym: link.File.LazySymbol,207 lazy_sym: link.File.LazySymbol,
207 code: *std.ArrayListUnmanaged(u8),208 code: *ArrayList(u8),
208 debug_output: link.File.DebugInfoOutput,209 debug_output: link.File.DebugInfoOutput,
209) CodeGenError!void {210) CodeGenError!void {
210 const zcu = pt.zcu;211 const zcu = pt.zcu;
...@@ -236,7 +237,7 @@ pub fn generateLazySymbol(...@@ -236,7 +237,7 @@ pub fn generateLazySymbol(
236 lazy_sym: link.File.LazySymbol,237 lazy_sym: link.File.LazySymbol,
237 // TODO don't use an "out" parameter like this; put it in the result instead238 // TODO don't use an "out" parameter like this; put it in the result instead
238 alignment: *Alignment,239 alignment: *Alignment,
239 code: *std.ArrayListUnmanaged(u8),240 code: *ArrayList(u8),
240 debug_output: link.File.DebugInfoOutput,241 debug_output: link.File.DebugInfoOutput,
241 reloc_parent: link.File.RelocInfo.Parent,242 reloc_parent: link.File.RelocInfo.Parent,
242) CodeGenError!void {243) CodeGenError!void {
...@@ -311,7 +312,7 @@ pub fn generateSymbol(...@@ -311,7 +312,7 @@ pub fn generateSymbol(
311 pt: Zcu.PerThread,312 pt: Zcu.PerThread,
312 src_loc: Zcu.LazySrcLoc,313 src_loc: Zcu.LazySrcLoc,
313 val: Value,314 val: Value,
314 code: *std.ArrayListUnmanaged(u8),315 code: *ArrayList(u8),
315 reloc_parent: link.File.RelocInfo.Parent,316 reloc_parent: link.File.RelocInfo.Parent,
316) GenerateSymbolError!void {317) GenerateSymbolError!void {
317 const tracy = trace(@src());318 const tracy = trace(@src());
...@@ -379,7 +380,7 @@ pub fn generateSymbol(...@@ -379,7 +380,7 @@ pub fn generateSymbol(
379 },380 },
380 .err => |err| {381 .err => |err| {
381 const int = try pt.getErrorValue(err.name);382 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);
383 },384 },
384 .error_union => |error_union| {385 .error_union => |error_union| {
385 const payload_ty = ty.errorUnionPayload(zcu);386 const payload_ty = ty.errorUnionPayload(zcu);
...@@ -389,7 +390,7 @@ pub fn generateSymbol(...@@ -389,7 +390,7 @@ pub fn generateSymbol(
389 };390 };
390391
391 if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) {392 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);
393 return;394 return;
394 }395 }
395396
...@@ -399,7 +400,7 @@ pub fn generateSymbol(...@@ -399,7 +400,7 @@ pub fn generateSymbol(
399400
400 // error value first when its type is larger than the error union's payload401 // error value first when its type is larger than the error union's payload
401 if (error_align.order(payload_align) == .gt) {402 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);
403 }404 }
404405
405 // emit payload part of the error union406 // emit payload part of the error union
...@@ -421,7 +422,7 @@ pub fn generateSymbol(...@@ -421,7 +422,7 @@ pub fn generateSymbol(
421 // Payload size is larger than error set, so emit our error set last422 // Payload size is larger than error set, so emit our error set last
422 if (error_align.compare(.lte, payload_align)) {423 if (error_align.compare(.lte, payload_align)) {
423 const begin = code.items.len;424 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);
425 const unpadded_end = code.items.len - begin;426 const unpadded_end = code.items.len - begin;
426 const padded_end = abi_align.forward(unpadded_end);427 const padded_end = abi_align.forward(unpadded_end);
427 const padding = math.cast(usize, padded_end - unpadded_end) orelse return error.Overflow;428 const padding = math.cast(usize, padded_end - unpadded_end) orelse return error.Overflow;
...@@ -476,7 +477,7 @@ pub fn generateSymbol(...@@ -476,7 +477,7 @@ pub fn generateSymbol(
476 }));477 }));
477 try generateSymbol(bin_file, pt, src_loc, value, code, reloc_parent);478 try generateSymbol(bin_file, pt, src_loc, value, code, reloc_parent);
478 }479 }
479 try code.writer(gpa).writeByte(@intFromBool(payload_val != null));480 try code.append(gpa, @intFromBool(payload_val != null));
480 try code.appendNTimes(gpa, 0, padding);481 try code.appendNTimes(gpa, 0, padding);
481 }482 }
482 },483 },
...@@ -721,7 +722,7 @@ fn lowerPtr(...@@ -721,7 +722,7 @@ fn lowerPtr(
721 pt: Zcu.PerThread,722 pt: Zcu.PerThread,
722 src_loc: Zcu.LazySrcLoc,723 src_loc: Zcu.LazySrcLoc,
723 ptr_val: InternPool.Index,724 ptr_val: InternPool.Index,
724 code: *std.ArrayListUnmanaged(u8),725 code: *ArrayList(u8),
725 reloc_parent: link.File.RelocInfo.Parent,726 reloc_parent: link.File.RelocInfo.Parent,
726 prev_offset: u64,727 prev_offset: u64,
727) GenerateSymbolError!void {728) GenerateSymbolError!void {
...@@ -774,7 +775,7 @@ fn lowerUavRef(...@@ -774,7 +775,7 @@ fn lowerUavRef(
774 pt: Zcu.PerThread,775 pt: Zcu.PerThread,
775 src_loc: Zcu.LazySrcLoc,776 src_loc: Zcu.LazySrcLoc,
776 uav: InternPool.Key.Ptr.BaseAddr.Uav,777 uav: InternPool.Key.Ptr.BaseAddr.Uav,
777 code: *std.ArrayListUnmanaged(u8),778 code: *ArrayList(u8),
778 reloc_parent: link.File.RelocInfo.Parent,779 reloc_parent: link.File.RelocInfo.Parent,
779 offset: u64,780 offset: u64,
780) GenerateSymbolError!void {781) GenerateSymbolError!void {
...@@ -834,7 +835,7 @@ fn lowerNavRef(...@@ -834,7 +835,7 @@ fn lowerNavRef(
834 lf: *link.File,835 lf: *link.File,
835 pt: Zcu.PerThread,836 pt: Zcu.PerThread,
836 nav_index: InternPool.Nav.Index,837 nav_index: InternPool.Nav.Index,
837 code: *std.ArrayListUnmanaged(u8),838 code: *ArrayList(u8),
838 reloc_parent: link.File.RelocInfo.Parent,839 reloc_parent: link.File.RelocInfo.Parent,
839 offset: u64,840 offset: u64,
840) GenerateSymbolError!void {841) GenerateSymbolError!void {
src/libs/mingw.zig+1-1
...@@ -304,7 +304,7 @@ pub fn buildImportLib(comp: *Compilation, lib_name: []const u8) !void {...@@ -304,7 +304,7 @@ 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 var stderr = std.debug.lockStderrWriter();307 var stderr = std.debug.lockStderrWriter(&.{});
308 defer std.debug.unlockStderrWriter();308 defer std.debug.unlockStderrWriter();
309 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;
310 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;
src/link/Coff.zig+4-4
...@@ -2179,13 +2179,13 @@ fn writeDataDirectoriesHeaders(coff: *Coff) !void {...@@ -2179,13 +2179,13 @@ fn writeDataDirectoriesHeaders(coff: *Coff) !void {
2179fn writeHeader(coff: *Coff) !void {2179fn writeHeader(coff: *Coff) !void {
2180 const target = &coff.base.comp.root_mod.resolved_target.result;2180 const target = &coff.base.comp.root_mod.resolved_target.result;
2181 const gpa = coff.base.comp.gpa;2181 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);
2183 defer buffer.deinit();2183 defer buffer.deinit();
2184 const writer = buffer.writer();2184 const writer = &buffer.writer;
21852185
2186 try buffer.ensureTotalCapacity(coff.getSizeOfHeaders());2186 try buffer.ensureTotalCapacity(coff.getSizeOfHeaders());
2187 writer.writeAll(&msdos_stub) catch unreachable;2187 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
2190 writer.writeAll("PE\x00\x00") catch unreachable;2190 writer.writeAll("PE\x00\x00") catch unreachable;
2191 var flags = coff_util.CoffHeaderFlags{2191 var flags = coff_util.CoffHeaderFlags{
...@@ -2313,7 +2313,7 @@ fn writeHeader(coff: *Coff) !void {...@@ -2313,7 +2313,7 @@ fn writeHeader(coff: *Coff) !void {
2313 },2313 },
2314 }2314 }
23152315
2316 try coff.pwriteAll(buffer.items, 0);2316 try coff.pwriteAll(buffer.written(), 0);
2317}2317}
23182318
2319pub fn padToIdeal(actual_size: anytype) @TypeOf(actual_size) {2319pub fn padToIdeal(actual_size: anytype) @TypeOf(actual_size) {