authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-02-19 11:22:06-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-01 16:35:26-07:00
log2fb6ce2f92ee79fa4171dccc6cb262db2e91d63a
treec793630e7b42ec4a2f48571f958a8fd820e812b5
parent4aed226e07a911be88ed6e2f349796364d3327f7

update some more compiler code


12 files changed, 139 insertions(+), 121 deletions(-)

lib/std/io/AllocatingWriter.zig+6
......@@ -90,6 +90,12 @@ pub fn toOwnedSlice(aw: *AllocatingWriter) error{OutOfMemory}![]u8 {
9090 return list.toOwnedSlice(gpa);
9191}
9292
93pub fn toOwnedSliceSentinel(aw: *AllocatingWriter, comptime sentinel: u8) error{OutOfMemory}![:sentinel]u8 {
94 const gpa = aw.allocator;
95 var list = toArrayList(aw);
96 return list.toOwnedSliceSentinel(gpa, sentinel);
97}
98
9399fn setArrayList(aw: *AllocatingWriter, list: std.ArrayListUnmanaged(u8)) void {
94100 aw.written = list.items;
95101 aw.buffered_writer.buffer = list.unusedCapacitySlice();
src/Air.zig+3-3
......@@ -960,9 +960,9 @@ pub const Inst = struct {
960960 pub fn format(
961961 index: Index,
962962 comptime _: []const u8,
963 _: std.fmt.FormatOptions,
964 writer: anytype,
965 ) @TypeOf(writer).Error!void {
963 _: std.fmt.Options,
964 writer: *std.io.BufferedWriter,
965 ) anyerror!void {
966966 try writer.writeByte('%');
967967 switch (index.unwrap()) {
968968 .ref => {},
src/Air/print.zig+72-67
......@@ -8,7 +8,7 @@ const Type = @import("../Type.zig");
88const Air = @import("../Air.zig");
99const InternPool = @import("../InternPool.zig");
1010
11pub fn write(air: Air, stream: anytype, pt: Zcu.PerThread, liveness: ?Air.Liveness) void {
11pub fn write(air: Air, stream: *std.io.BufferedWriter, pt: Zcu.PerThread, liveness: ?Air.Liveness) void {
1212 comptime std.debug.assert(build_options.enable_debug_extensions);
1313 const instruction_bytes = air.instructions.len *
1414 // Here we don't use @sizeOf(Air.Inst.Data) because it would include
......@@ -54,7 +54,7 @@ pub fn write(air: Air, stream: anytype, pt: Zcu.PerThread, liveness: ?Air.Livene
5454
5555pub fn writeInst(
5656 air: Air,
57 stream: anytype,
57 stream: *std.io.BufferedWriter,
5858 inst: Air.Inst.Index,
5959 pt: Zcu.PerThread,
6060 liveness: ?Air.Liveness,
......@@ -72,11 +72,15 @@ pub fn writeInst(
7272}
7373
7474pub fn dump(air: Air, pt: Zcu.PerThread, liveness: ?Air.Liveness) void {
75 air.write(std.io.getStdErr().writer(), pt, liveness);
75 var bw = std.debug.lockStdErr2();
76 defer std.debug.unlockStdErr();
77 air.write(&bw, pt, liveness);
7678}
7779
7880pub fn dumpInst(air: Air, inst: Air.Inst.Index, pt: Zcu.PerThread, liveness: ?Air.Liveness) void {
79 air.writeInst(std.io.getStdErr().writer(), inst, pt, liveness);
81 var bw = std.debug.lockStdErr2();
82 defer std.debug.unlockStdErr();
83 air.writeInst(&bw, inst, pt, liveness);
8084}
8185
8286const Writer = struct {
......@@ -87,16 +91,16 @@ const Writer = struct {
8791 indent: usize,
8892 skip_body: bool,
8993
90 fn writeBody(w: *Writer, s: anytype, body: []const Air.Inst.Index) @TypeOf(s).Error!void {
94 fn writeBody(w: *Writer, s: *std.io.BufferedWriter, body: []const Air.Inst.Index) anyerror!void {
9195 for (body) |inst| {
9296 try w.writeInst(s, inst);
9397 try s.writeByte('\n');
9498 }
9599 }
96100
97 fn writeInst(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
101 fn writeInst(w: *Writer, s: *std.io.BufferedWriter, inst: Air.Inst.Index) anyerror!void {
98102 const tag = w.air.instructions.items(.tag)[@intFromEnum(inst)];
99 try s.writeByteNTimes(' ', w.indent);
103 try s.splatByteAll(' ', w.indent);
100104 try s.print("{}{c}= {s}(", .{
101105 inst,
102106 @as(u8, if (if (w.liveness) |liveness| liveness.isUnused(inst) else false) '!' else ' '),
......@@ -334,47 +338,48 @@ const Writer = struct {
334338 try s.writeByte(')');
335339 }
336340
337 fn writeBinOp(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
341 fn writeBinOp(w: *Writer, s: *std.io.BufferedWriter, inst: Air.Inst.Index) anyerror!void {
338342 const bin_op = w.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
339343 try w.writeOperand(s, inst, 0, bin_op.lhs);
340344 try s.writeAll(", ");
341345 try w.writeOperand(s, inst, 1, bin_op.rhs);
342346 }
343347
344 fn writeUnOp(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
348 fn writeUnOp(w: *Writer, s: *std.io.BufferedWriter, inst: Air.Inst.Index) anyerror!void {
345349 const un_op = w.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
346350 try w.writeOperand(s, inst, 0, un_op);
347351 }
348352
349 fn writeNoOp(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
353 fn writeNoOp(w: *Writer, s: *std.io.BufferedWriter, inst: Air.Inst.Index) anyerror!void {
350354 _ = w;
355 _ = s;
351356 _ = inst;
352357 // no-op, no argument to write
353358 }
354359
355 fn writeType(w: *Writer, s: anytype, ty: Type) !void {
360 fn writeType(w: *Writer, s: *std.io.BufferedWriter, ty: Type) !void {
356361 return ty.print(s, w.pt);
357362 }
358363
359 fn writeTy(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
364 fn writeTy(w: *Writer, s: *std.io.BufferedWriter, inst: Air.Inst.Index) anyerror!void {
360365 const ty = w.air.instructions.items(.data)[@intFromEnum(inst)].ty;
361366 try w.writeType(s, ty);
362367 }
363368
364 fn writeArg(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
369 fn writeArg(w: *Writer, s: *std.io.BufferedWriter, inst: Air.Inst.Index) anyerror!void {
365370 const arg = w.air.instructions.items(.data)[@intFromEnum(inst)].arg;
366371 try w.writeType(s, arg.ty.toType());
367372 try s.print(", {d}", .{arg.zir_param_index});
368373 }
369374
370 fn writeTyOp(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
375 fn writeTyOp(w: *Writer, s: *std.io.BufferedWriter, inst: Air.Inst.Index) anyerror!void {
371376 const ty_op = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
372377 try w.writeType(s, ty_op.ty.toType());
373378 try s.writeAll(", ");
374379 try w.writeOperand(s, inst, 0, ty_op.operand);
375380 }
376381
377 fn writeBlock(w: *Writer, s: anytype, tag: Air.Inst.Tag, inst: Air.Inst.Index) @TypeOf(s).Error!void {
382 fn writeBlock(w: *Writer, s: *std.io.BufferedWriter, tag: Air.Inst.Tag, inst: Air.Inst.Index) anyerror!void {
378383 const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
379384 try w.writeType(s, ty_pl.ty.toType());
380385 const body: []const Air.Inst.Index = @ptrCast(switch (tag) {
......@@ -407,7 +412,7 @@ const Writer = struct {
407412 w.indent += 2;
408413 try w.writeBody(s, body);
409414 w.indent = old_indent;
410 try s.writeByteNTimes(' ', w.indent);
415 try s.splatByteAll(' ', w.indent);
411416 try s.writeAll("}");
412417
413418 for (liveness_block.deaths) |operand| {
......@@ -415,7 +420,7 @@ const Writer = struct {
415420 }
416421 }
417422
418 fn writeLoop(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
423 fn writeLoop(w: *Writer, s: *std.io.BufferedWriter, inst: Air.Inst.Index) anyerror!void {
419424 const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
420425 const extra = w.air.extraData(Air.Block, ty_pl.payload);
421426 const body: []const Air.Inst.Index = @ptrCast(w.air.extra.items[extra.end..][0..extra.data.body_len]);
......@@ -427,11 +432,11 @@ const Writer = struct {
427432 w.indent += 2;
428433 try w.writeBody(s, body);
429434 w.indent = old_indent;
430 try s.writeByteNTimes(' ', w.indent);
435 try s.splatByteAll(' ', w.indent);
431436 try s.writeAll("}");
432437 }
433438
434 fn writeAggregateInit(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
439 fn writeAggregateInit(w: *Writer, s: *std.io.BufferedWriter, inst: Air.Inst.Index) anyerror!void {
435440 const zcu = w.pt.zcu;
436441 const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
437442 const vector_ty = ty_pl.ty.toType();
......@@ -447,7 +452,7 @@ const Writer = struct {
447452 try s.writeAll("]");
448453 }
449454
450 fn writeUnionInit(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
455 fn writeUnionInit(w: *Writer, s: *std.io.BufferedWriter, inst: Air.Inst.Index) anyerror!void {
451456 const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
452457 const extra = w.air.extraData(Air.UnionInit, ty_pl.payload).data;
453458
......@@ -455,7 +460,7 @@ const Writer = struct {
455460 try w.writeOperand(s, inst, 0, extra.init);
456461 }
457462
458 fn writeStructField(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
463 fn writeStructField(w: *Writer, s: *std.io.BufferedWriter, inst: Air.Inst.Index) anyerror!void {
459464 const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
460465 const extra = w.air.extraData(Air.StructField, ty_pl.payload).data;
461466
......@@ -463,7 +468,7 @@ const Writer = struct {
463468 try s.print(", {d}", .{extra.field_index});
464469 }
465470
466 fn writeTyPlBin(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
471 fn writeTyPlBin(w: *Writer, s: *std.io.BufferedWriter, inst: Air.Inst.Index) anyerror!void {
467472 const data = w.air.instructions.items(.data);
468473 const ty_pl = data[@intFromEnum(inst)].ty_pl;
469474 const extra = w.air.extraData(Air.Bin, ty_pl.payload).data;
......@@ -476,7 +481,7 @@ const Writer = struct {
476481 try w.writeOperand(s, inst, 1, extra.rhs);
477482 }
478483
479 fn writeCmpxchg(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
484 fn writeCmpxchg(w: *Writer, s: *std.io.BufferedWriter, inst: Air.Inst.Index) anyerror!void {
480485 const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
481486 const extra = w.air.extraData(Air.Cmpxchg, ty_pl.payload).data;
482487
......@@ -490,7 +495,7 @@ const Writer = struct {
490495 });
491496 }
492497
493 fn writeMulAdd(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
498 fn writeMulAdd(w: *Writer, s: *std.io.BufferedWriter, inst: Air.Inst.Index) anyerror!void {
494499 const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
495500 const extra = w.air.extraData(Air.Bin, pl_op.payload).data;
496501
......@@ -501,7 +506,7 @@ const Writer = struct {
501506 try w.writeOperand(s, inst, 2, pl_op.operand);
502507 }
503508
504 fn writeShuffleOne(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
509 fn writeShuffleOne(w: *Writer, s: anytype, inst: Air.Inst.Index) anyerror!void {
505510 const unwrapped = w.air.unwrapShuffleOne(w.pt.zcu, inst);
506511 try w.writeType(s, unwrapped.result_ty);
507512 try s.writeAll(", ");
......@@ -536,7 +541,7 @@ const Writer = struct {
536541 try s.writeByte(']');
537542 }
538543
539 fn writeSelect(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
544 fn writeSelect(w: *Writer, s: *std.io.BufferedWriter, inst: Air.Inst.Index) anyerror!void {
540545 const zcu = w.pt.zcu;
541546 const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
542547 const extra = w.air.extraData(Air.Bin, pl_op.payload).data;
......@@ -551,14 +556,14 @@ const Writer = struct {
551556 try w.writeOperand(s, inst, 2, extra.rhs);
552557 }
553558
554 fn writeReduce(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
559 fn writeReduce(w: *Writer, s: *std.io.BufferedWriter, inst: Air.Inst.Index) anyerror!void {
555560 const reduce = w.air.instructions.items(.data)[@intFromEnum(inst)].reduce;
556561
557562 try w.writeOperand(s, inst, 0, reduce.operand);
558563 try s.print(", {s}", .{@tagName(reduce.operation)});
559564 }
560565
561 fn writeCmpVector(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
566 fn writeCmpVector(w: *Writer, s: *std.io.BufferedWriter, inst: Air.Inst.Index) anyerror!void {
562567 const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
563568 const extra = w.air.extraData(Air.VectorCmp, ty_pl.payload).data;
564569
......@@ -568,7 +573,7 @@ const Writer = struct {
568573 try w.writeOperand(s, inst, 1, extra.rhs);
569574 }
570575
571 fn writeVectorStoreElem(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
576 fn writeVectorStoreElem(w: *Writer, s: *std.io.BufferedWriter, inst: Air.Inst.Index) anyerror!void {
572577 const data = w.air.instructions.items(.data)[@intFromEnum(inst)].vector_store_elem;
573578 const extra = w.air.extraData(Air.VectorCmp, data.payload).data;
574579
......@@ -579,21 +584,21 @@ const Writer = struct {
579584 try w.writeOperand(s, inst, 2, extra.rhs);
580585 }
581586
582 fn writeRuntimeNavPtr(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
587 fn writeRuntimeNavPtr(w: *Writer, s: *std.io.BufferedWriter, inst: Air.Inst.Index) std.io.Writer.Error!void {
583588 const ip = &w.pt.zcu.intern_pool;
584589 const ty_nav = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_nav;
585590 try w.writeType(s, .fromInterned(ty_nav.ty));
586591 try s.print(", '{}'", .{ip.getNav(ty_nav.nav).fqn.fmt(ip)});
587592 }
588593
589 fn writeAtomicLoad(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
594 fn writeAtomicLoad(w: *Writer, s: *std.io.BufferedWriter, inst: Air.Inst.Index) std.io.Writer.Error!void {
590595 const atomic_load = w.air.instructions.items(.data)[@intFromEnum(inst)].atomic_load;
591596
592597 try w.writeOperand(s, inst, 0, atomic_load.ptr);
593598 try s.print(", {s}", .{@tagName(atomic_load.order)});
594599 }
595600
596 fn writePrefetch(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
601 fn writePrefetch(w: *Writer, s: *std.io.BufferedWriter, inst: Air.Inst.Index) anyerror!void {
597602 const prefetch = w.air.instructions.items(.data)[@intFromEnum(inst)].prefetch;
598603
599604 try w.writeOperand(s, inst, 0, prefetch.ptr);
......@@ -604,10 +609,10 @@ const Writer = struct {
604609
605610 fn writeAtomicStore(
606611 w: *Writer,
607 s: anytype,
612 s: *std.io.BufferedWriter,
608613 inst: Air.Inst.Index,
609614 order: std.builtin.AtomicOrder,
610 ) @TypeOf(s).Error!void {
615 ) anyerror!void {
611616 const bin_op = w.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
612617 try w.writeOperand(s, inst, 0, bin_op.lhs);
613618 try s.writeAll(", ");
......@@ -615,7 +620,7 @@ const Writer = struct {
615620 try s.print(", {s}", .{@tagName(order)});
616621 }
617622
618 fn writeAtomicRmw(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
623 fn writeAtomicRmw(w: *Writer, s: *std.io.BufferedWriter, inst: Air.Inst.Index) anyerror!void {
619624 const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
620625 const extra = w.air.extraData(Air.AtomicRmw, pl_op.payload).data;
621626
......@@ -625,7 +630,7 @@ const Writer = struct {
625630 try s.print(", {s}, {s}", .{ @tagName(extra.op()), @tagName(extra.ordering()) });
626631 }
627632
628 fn writeFieldParentPtr(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
633 fn writeFieldParentPtr(w: *Writer, s: *std.io.BufferedWriter, inst: Air.Inst.Index) anyerror!void {
629634 const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
630635 const extra = w.air.extraData(Air.FieldParentPtr, ty_pl.payload).data;
631636
......@@ -633,7 +638,7 @@ const Writer = struct {
633638 try s.print(", {d}", .{extra.field_index});
634639 }
635640
636 fn writeAssembly(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
641 fn writeAssembly(w: *Writer, s: *std.io.BufferedWriter, inst: Air.Inst.Index) anyerror!void {
637642 const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
638643 const extra = w.air.extraData(Air.Asm, ty_pl.payload);
639644 const is_volatile = @as(u1, @truncate(extra.data.flags >> 31)) != 0;
......@@ -706,19 +711,19 @@ const Writer = struct {
706711 try s.print(", \"{}\"", .{std.zig.fmtEscapes(asm_source)});
707712 }
708713
709 fn writeDbgStmt(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
714 fn writeDbgStmt(w: *Writer, s: *std.io.BufferedWriter, inst: Air.Inst.Index) anyerror!void {
710715 const dbg_stmt = w.air.instructions.items(.data)[@intFromEnum(inst)].dbg_stmt;
711716 try s.print("{d}:{d}", .{ dbg_stmt.line + 1, dbg_stmt.column + 1 });
712717 }
713718
714 fn writeDbgVar(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
719 fn writeDbgVar(w: *Writer, s: *std.io.BufferedWriter, inst: Air.Inst.Index) anyerror!void {
715720 const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
716721 try w.writeOperand(s, inst, 0, pl_op.operand);
717722 const name: Air.NullTerminatedString = @enumFromInt(pl_op.payload);
718723 try s.print(", \"{}\"", .{std.zig.fmtEscapes(name.toSlice(w.air))});
719724 }
720725
721 fn writeCall(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
726 fn writeCall(w: *Writer, s: *std.io.BufferedWriter, inst: Air.Inst.Index) anyerror!void {
722727 const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
723728 const extra = w.air.extraData(Air.Call, pl_op.payload);
724729 const args = @as([]const Air.Inst.Ref, @ptrCast(w.air.extra.items[extra.end..][0..extra.data.args_len]));
......@@ -731,19 +736,19 @@ const Writer = struct {
731736 try s.writeAll("]");
732737 }
733738
734 fn writeBr(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
739 fn writeBr(w: *Writer, s: *std.io.BufferedWriter, inst: Air.Inst.Index) anyerror!void {
735740 const br = w.air.instructions.items(.data)[@intFromEnum(inst)].br;
736741 try w.writeInstIndex(s, br.block_inst, false);
737742 try s.writeAll(", ");
738743 try w.writeOperand(s, inst, 0, br.operand);
739744 }
740745
741 fn writeRepeat(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
746 fn writeRepeat(w: *Writer, s: *std.io.BufferedWriter, inst: Air.Inst.Index) anyerror!void {
742747 const repeat = w.air.instructions.items(.data)[@intFromEnum(inst)].repeat;
743748 try w.writeInstIndex(s, repeat.loop_inst, false);
744749 }
745750
746 fn writeTry(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
751 fn writeTry(w: *Writer, s: *std.io.BufferedWriter, inst: Air.Inst.Index) anyerror!void {
747752 const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
748753 const extra = w.air.extraData(Air.Try, pl_op.payload);
749754 const body: []const Air.Inst.Index = @ptrCast(w.air.extra.items[extra.end..][0..extra.data.body_len]);
......@@ -759,7 +764,7 @@ const Writer = struct {
759764 w.indent += 2;
760765
761766 if (liveness_condbr.else_deaths.len != 0) {
762 try s.writeByteNTimes(' ', w.indent);
767 try s.splatByteAll(' ', w.indent);
763768 for (liveness_condbr.else_deaths, 0..) |operand, i| {
764769 if (i != 0) try s.writeAll(" ");
765770 try s.print("{}!", .{operand});
......@@ -769,7 +774,7 @@ const Writer = struct {
769774 try w.writeBody(s, body);
770775
771776 w.indent = old_indent;
772 try s.writeByteNTimes(' ', w.indent);
777 try s.splatByteAll(' ', w.indent);
773778 try s.writeAll("}");
774779
775780 for (liveness_condbr.then_deaths) |operand| {
......@@ -777,7 +782,7 @@ const Writer = struct {
777782 }
778783 }
779784
780 fn writeTryPtr(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
785 fn writeTryPtr(w: *Writer, s: *std.io.BufferedWriter, inst: Air.Inst.Index) anyerror!void {
781786 const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
782787 const extra = w.air.extraData(Air.TryPtr, ty_pl.payload);
783788 const body: []const Air.Inst.Index = @ptrCast(w.air.extra.items[extra.end..][0..extra.data.body_len]);
......@@ -796,7 +801,7 @@ const Writer = struct {
796801 w.indent += 2;
797802
798803 if (liveness_condbr.else_deaths.len != 0) {
799 try s.writeByteNTimes(' ', w.indent);
804 try s.splatByteAll(' ', w.indent);
800805 for (liveness_condbr.else_deaths, 0..) |operand, i| {
801806 if (i != 0) try s.writeAll(" ");
802807 try s.print("{}!", .{operand});
......@@ -806,7 +811,7 @@ const Writer = struct {
806811 try w.writeBody(s, body);
807812
808813 w.indent = old_indent;
809 try s.writeByteNTimes(' ', w.indent);
814 try s.splatByteAll(' ', w.indent);
810815 try s.writeAll("}");
811816
812817 for (liveness_condbr.then_deaths) |operand| {
......@@ -814,7 +819,7 @@ const Writer = struct {
814819 }
815820 }
816821
817 fn writeCondBr(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
822 fn writeCondBr(w: *Writer, s: *std.io.BufferedWriter, inst: Air.Inst.Index) anyerror!void {
818823 const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
819824 const extra = w.air.extraData(Air.CondBr, pl_op.payload);
820825 const then_body: []const Air.Inst.Index = @ptrCast(w.air.extra.items[extra.end..][0..extra.data.then_body_len]);
......@@ -838,7 +843,7 @@ const Writer = struct {
838843 w.indent += 2;
839844
840845 if (liveness_condbr.then_deaths.len != 0) {
841 try s.writeByteNTimes(' ', w.indent);
846 try s.splatByteAll(' ', w.indent);
842847 for (liveness_condbr.then_deaths, 0..) |operand, i| {
843848 if (i != 0) try s.writeAll(" ");
844849 try s.print("{}!", .{operand});
......@@ -847,7 +852,7 @@ const Writer = struct {
847852 }
848853
849854 try w.writeBody(s, then_body);
850 try s.writeByteNTimes(' ', old_indent);
855 try s.splatByteAll(' ', old_indent);
851856 try s.writeAll("},");
852857 if (extra.data.branch_hints.false != .none) {
853858 try s.print(" {s}", .{@tagName(extra.data.branch_hints.false)});
......@@ -858,7 +863,7 @@ const Writer = struct {
858863 try s.writeAll(" {\n");
859864
860865 if (liveness_condbr.else_deaths.len != 0) {
861 try s.writeByteNTimes(' ', w.indent);
866 try s.splatByteAll(' ', w.indent);
862867 for (liveness_condbr.else_deaths, 0..) |operand, i| {
863868 if (i != 0) try s.writeAll(" ");
864869 try s.print("{}!", .{operand});
......@@ -869,11 +874,11 @@ const Writer = struct {
869874 try w.writeBody(s, else_body);
870875 w.indent = old_indent;
871876
872 try s.writeByteNTimes(' ', old_indent);
877 try s.splatByteAll(' ', old_indent);
873878 try s.writeAll("}");
874879 }
875880
876 fn writeSwitchBr(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
881 fn writeSwitchBr(w: *Writer, s: *std.io.BufferedWriter, inst: Air.Inst.Index) anyerror!void {
877882 const switch_br = w.air.unwrapSwitch(inst);
878883
879884 const liveness: Air.Liveness.SwitchBrTable = if (w.liveness) |liveness|
......@@ -915,7 +920,7 @@ const Writer = struct {
915920
916921 const deaths = liveness.deaths[case.idx];
917922 if (deaths.len != 0) {
918 try s.writeByteNTimes(' ', w.indent);
923 try s.splatByteAll(' ', w.indent);
919924 for (deaths, 0..) |operand, i| {
920925 if (i != 0) try s.writeAll(" ");
921926 try s.print("{}!", .{operand});
......@@ -925,7 +930,7 @@ const Writer = struct {
925930
926931 try w.writeBody(s, case.body);
927932 w.indent -= 2;
928 try s.writeByteNTimes(' ', w.indent);
933 try s.splatByteAll(' ', w.indent);
929934 try s.writeAll("}");
930935 }
931936
......@@ -941,7 +946,7 @@ const Writer = struct {
941946
942947 const deaths = liveness.deaths[liveness.deaths.len - 1];
943948 if (deaths.len != 0) {
944 try s.writeByteNTimes(' ', w.indent);
949 try s.splatByteAll(' ', w.indent);
945950 for (deaths, 0..) |operand, i| {
946951 if (i != 0) try s.writeAll(" ");
947952 try s.print("{}!", .{operand});
......@@ -951,37 +956,37 @@ const Writer = struct {
951956
952957 try w.writeBody(s, else_body);
953958 w.indent -= 2;
954 try s.writeByteNTimes(' ', w.indent);
959 try s.splatByteAll(' ', w.indent);
955960 try s.writeAll("}");
956961 }
957962
958963 try s.writeAll("\n");
959 try s.writeByteNTimes(' ', old_indent);
964 try s.splatByteAll(' ', old_indent);
960965 }
961966
962 fn writeWasmMemorySize(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
967 fn writeWasmMemorySize(w: *Writer, s: *std.io.BufferedWriter, inst: Air.Inst.Index) anyerror!void {
963968 const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
964969 try s.print("{d}", .{pl_op.payload});
965970 }
966971
967 fn writeWasmMemoryGrow(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
972 fn writeWasmMemoryGrow(w: *Writer, s: *std.io.BufferedWriter, inst: Air.Inst.Index) anyerror!void {
968973 const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
969974 try s.print("{d}, ", .{pl_op.payload});
970975 try w.writeOperand(s, inst, 0, pl_op.operand);
971976 }
972977
973 fn writeWorkDimension(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void {
978 fn writeWorkDimension(w: *Writer, s: *std.io.BufferedWriter, inst: Air.Inst.Index) anyerror!void {
974979 const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
975980 try s.print("{d}", .{pl_op.payload});
976981 }
977982
978983 fn writeOperand(
979984 w: *Writer,
980 s: anytype,
985 s: *std.io.BufferedWriter,
981986 inst: Air.Inst.Index,
982987 op_index: usize,
983988 operand: Air.Inst.Ref,
984 ) @TypeOf(s).Error!void {
989 ) anyerror!void {
985990 const small_tomb_bits = Air.Liveness.bpi - 1;
986991 const dies = if (w.liveness) |liveness| blk: {
987992 if (op_index < small_tomb_bits)
......@@ -1006,7 +1011,7 @@ const Writer = struct {
10061011 s: anytype,
10071012 operand: Air.Inst.Ref,
10081013 dies: bool,
1009 ) @TypeOf(s).Error!void {
1014 ) anyerror!void {
10101015 if (@intFromEnum(operand) < InternPool.static_len) {
10111016 return s.print("@{}", .{operand});
10121017 } else if (operand.toInterned()) |ip_index| {
......@@ -1023,10 +1028,10 @@ const Writer = struct {
10231028
10241029 fn writeInstIndex(
10251030 w: *Writer,
1026 s: anytype,
1031 s: *std.io.BufferedWriter,
10271032 inst: Air.Inst.Index,
10281033 dies: bool,
1029 ) @TypeOf(s).Error!void {
1034 ) anyerror!void {
10301035 _ = w;
10311036 try s.print("{}", .{inst});
10321037 if (dies) try s.writeByte('!');
src/Package/Fetch/git.zig+14-10
......@@ -825,9 +825,9 @@ pub const Session = struct {
825825 upload_pack_uri.query = null;
826826 upload_pack_uri.fragment = null;
827827
828 var body: std.ArrayListUnmanaged(u8) = .empty;
829 defer body.deinit(session.allocator);
830 const body_writer = body.writer(session.allocator);
828 var body: std.io.AllocatingWriter = undefined;
829 const body_writer = body.init(session.allocator);
830 defer body.deinit();
831831 try Packet.write(.{ .data = "command=ls-refs\n" }, body_writer);
832832 if (session.supports_agent) {
833833 try Packet.write(.{ .data = agent_capability }, body_writer);
......@@ -860,9 +860,11 @@ pub const Session = struct {
860860 },
861861 });
862862 errdefer request.deinit();
863 request.transfer_encoding = .{ .content_length = body.items.len };
863 const written = body.getWritten();
864 request.transfer_encoding = .{ .content_length = written.len };
864865 try request.send();
865 try request.writeAll(body.items);
866 var w = request.writer().unbuffered();
867 try w.writeAll(written);
866868 try request.finish();
867869
868870 try request.wait();
......@@ -940,9 +942,9 @@ pub const Session = struct {
940942 upload_pack_uri.query = null;
941943 upload_pack_uri.fragment = null;
942944
943 var body: std.ArrayListUnmanaged(u8) = .empty;
944 defer body.deinit(session.allocator);
945 const body_writer = body.writer(session.allocator);
945 var body: std.io.AllocatingWriter = undefined;
946 const body_writer = body.init(session.allocator);
947 defer body.deinit();
946948 try Packet.write(.{ .data = "command=fetch\n" }, body_writer);
947949 if (session.supports_agent) {
948950 try Packet.write(.{ .data = agent_capability }, body_writer);
......@@ -977,9 +979,11 @@ pub const Session = struct {
977979 },
978980 });
979981 errdefer request.deinit();
980 request.transfer_encoding = .{ .content_length = body.items.len };
982 const written = body.getWritten();
983 request.transfer_encoding = .{ .content_length = written.len };
981984 try request.send();
982 try request.writeAll(body.items);
985 var w = request.writer().unbuffered();
986 try w.writeAll(written);
983987 try request.finish();
984988
985989 try request.wait();
src/Package/Manifest.zig+5-4
......@@ -471,10 +471,11 @@ const Parse = struct {
471471 offset: u32,
472472 ) InnerError!void {
473473 const raw_string = bytes[offset..];
474 var buf_managed = buf.toManaged(p.gpa);
475 const result = std.zig.string_literal.parseWrite(buf_managed.writer(), raw_string);
476 buf.* = buf_managed.moveToUnmanaged();
477 switch (try result) {
474 var aw: std.io.AllocatingWriter = undefined;
475 const bw = aw.fromArrayList(p.gpa, buf);
476 const result = std.zig.string_literal.parseWrite(bw, raw_string);
477 buf.* = aw.toArrayList();
478 switch (result catch return error.OutOfMemory) {
478479 .success => {},
479480 .failure => |err| try p.appendStrLitError(err, token, bytes, offset),
480481 }
src/Type.zig+1-1
......@@ -173,7 +173,7 @@ pub fn dump(
173173
174174/// Prints a name suitable for `@typeName`.
175175/// TODO: take an `opt_sema` to pass to `fmtValue` when printing sentinels.
176pub fn print(ty: Type, writer: anytype, pt: Zcu.PerThread) @TypeOf(writer).Error!void {
176pub fn print(ty: Type, writer: *std.io.BufferedWriter, pt: Zcu.PerThread) anyerror!void {
177177 const zcu = pt.zcu;
178178 const ip = &zcu.intern_pool;
179179 switch (ip.indexToKey(ty.toIntern())) {
src/codegen/llvm.zig+9-8
......@@ -239,12 +239,12 @@ pub fn targetTriple(allocator: Allocator, target: *const std.Target) ![]const u8
239239 .none,
240240 .windows,
241241 => {},
242 .semver => |ver| try llvm_triple.writer().print("{d}.{d}.{d}", .{
242 .semver => |ver| try llvm_triple.print("{d}.{d}.{d}", .{
243243 ver.min.major,
244244 ver.min.minor,
245245 ver.min.patch,
246246 }),
247 inline .linux, .hurd => |ver| try llvm_triple.writer().print("{d}.{d}.{d}", .{
247 inline .linux, .hurd => |ver| try llvm_triple.print("{d}.{d}.{d}", .{
248248 ver.range.min.major,
249249 ver.range.min.minor,
250250 ver.range.min.patch,
......@@ -295,13 +295,13 @@ pub fn targetTriple(allocator: Allocator, target: *const std.Target) ![]const u8
295295 .windows,
296296 => {},
297297 inline .hurd, .linux => |ver| if (target.abi.isGnu()) {
298 try llvm_triple.writer().print("{d}.{d}.{d}", .{
298 try llvm_triple.print("{d}.{d}.{d}", .{
299299 ver.glibc.major,
300300 ver.glibc.minor,
301301 ver.glibc.patch,
302302 });
303303 } else if (@TypeOf(ver) == std.Target.Os.LinuxVersionRange and target.abi.isAndroid()) {
304 try llvm_triple.writer().print("{d}", .{ver.android});
304 try llvm_triple.print("{d}", .{ver.android});
305305 },
306306 }
307307
......@@ -2676,10 +2676,11 @@ pub const Object = struct {
26762676 }
26772677
26782678 fn allocTypeName(o: *Object, ty: Type) Allocator.Error![:0]const u8 {
2679 var buffer = std.ArrayList(u8).init(o.gpa);
2680 errdefer buffer.deinit();
2681 try ty.print(buffer.writer(), o.pt);
2682 return buffer.toOwnedSliceSentinel(0);
2679 var aw: std.io.AllocatingWriter = undefined;
2680 const bw = aw.init(o.gpa);
2681 defer aw.deinit();
2682 try ty.print(bw, o.pt);
2683 return aw.toOwnedSliceSentinel(0);
26832684 }
26842685
26852686 /// If the llvm function does not exist, create it.
src/libs/mingw.zig+1-2
......@@ -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();
307 var stderr = std.debug.lockStdErr2();
308308 defer std.debug.unlockStdErr();
309 const stderr = std.io.getStdErr().writer();
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;
src/link/Elf/eh_frame.zig+7-7
......@@ -51,7 +51,7 @@ pub const Fde = struct {
5151 fde: Fde,
5252 comptime unused_fmt_string: []const u8,
5353 options: std.fmt.FormatOptions,
54 writer: anytype,
54 writer: *std.io.BufferedWriter,
5555 ) !void {
5656 _ = fde;
5757 _ = unused_fmt_string;
......@@ -76,7 +76,7 @@ pub const Fde = struct {
7676 ctx: FdeFormatContext,
7777 comptime unused_fmt_string: []const u8,
7878 options: std.fmt.FormatOptions,
79 writer: anytype,
79 writer: *std.io.BufferedWriter,
8080 ) !void {
8181 _ = unused_fmt_string;
8282 _ = options;
......@@ -154,7 +154,7 @@ pub const Cie = struct {
154154 cie: Cie,
155155 comptime unused_fmt_string: []const u8,
156156 options: std.fmt.FormatOptions,
157 writer: anytype,
157 writer: *std.io.BufferedWriter,
158158 ) !void {
159159 _ = cie;
160160 _ = unused_fmt_string;
......@@ -179,7 +179,7 @@ pub const Cie = struct {
179179 ctx: CieFormatContext,
180180 comptime unused_fmt_string: []const u8,
181181 options: std.fmt.FormatOptions,
182 writer: anytype,
182 writer: *std.io.BufferedWriter,
183183 ) !void {
184184 _ = unused_fmt_string;
185185 _ = options;
......@@ -332,7 +332,7 @@ fn resolveReloc(rec: anytype, sym: *const Symbol, rel: elf.Elf64_Rela, elf_file:
332332 }
333333}
334334
335pub fn writeEhFrame(elf_file: *Elf, writer: anytype) !void {
335pub fn writeEhFrame(elf_file: *Elf, writer: *std.io.BufferedWriter) !void {
336336 relocs_log.debug("{x}: .eh_frame", .{
337337 elf_file.sections.items(.shdr)[elf_file.section_indexes.eh_frame.?].sh_addr,
338338 });
......@@ -393,7 +393,7 @@ pub fn writeEhFrame(elf_file: *Elf, writer: anytype) !void {
393393 if (has_reloc_errors) return error.RelocFailure;
394394}
395395
396pub fn writeEhFrameRelocatable(elf_file: *Elf, writer: anytype) !void {
396pub fn writeEhFrameRelocatable(elf_file: *Elf, writer: *std.io.BufferedWriter) !void {
397397 for (elf_file.objects.items) |index| {
398398 const object = elf_file.file(index).?.object;
399399
......@@ -495,7 +495,7 @@ pub fn writeEhFrameRelocs(elf_file: *Elf, relocs: *std.ArrayList(elf.Elf64_Rela)
495495 }
496496}
497497
498pub fn writeEhFrameHdr(elf_file: *Elf, writer: anytype) !void {
498pub fn writeEhFrameHdr(elf_file: *Elf, writer: *std.io.BufferedWriter) !void {
499499 const comp = elf_file.base.comp;
500500 const gpa = comp.gpa;
501501
src/link/Elf/relocatable.zig+6-4
......@@ -407,15 +407,17 @@ fn writeSyntheticSections(elf_file: *Elf) !void {
407407 };
408408 const shdr = slice.items(.shdr)[shndx];
409409 const sh_size = math.cast(usize, shdr.sh_size) orelse return error.Overflow;
410 var buffer = try std.ArrayList(u8).initCapacity(gpa, @intCast(sh_size - existing_size));
410 var buffer: std.io.AllocatingWriter = undefined;
411 const bw = buffer.init(gpa);
411412 defer buffer.deinit();
412 try eh_frame.writeEhFrameRelocatable(elf_file, buffer.writer());
413 try buffer.ensureTotalCapacity(gpa, sh_size - existing_size);
414 try eh_frame.writeEhFrameRelocatable(elf_file, bw);
413415 log.debug("writing .eh_frame from 0x{x} to 0x{x}", .{
414416 shdr.sh_offset + existing_size,
415417 shdr.sh_offset + sh_size,
416418 });
417 assert(buffer.items.len == sh_size - existing_size);
418 try elf_file.base.file.?.pwriteAll(buffer.items, shdr.sh_offset + existing_size);
419 assert(buffer.getWritten().len == sh_size - existing_size);
420 try elf_file.base.file.?.pwriteAll(buffer.getWritten(), shdr.sh_offset + existing_size);
419421 }
420422 if (elf_file.section_indexes.eh_frame_rela) |shndx| {
421423 const shdr = slice.items(.shdr)[shndx];
src/link/Wasm/Object.zig+1-1
......@@ -1460,7 +1460,7 @@ fn parseFeatures(
14601460}
14611461
14621462fn readLeb(comptime T: type, bytes: []const u8, pos: usize) struct { T, usize } {
1463 var fbr = std.io.fixedBufferStream(bytes[pos..]);
1463 var fbr: std.io.FixedBufferStream = .{ .buffer = bytes[pos..] };
14641464 return .{
14651465 switch (@typeInfo(T).int.signedness) {
14661466 .signed => std.leb.readIleb128(T, fbr.reader()) catch unreachable,
src/print_value.zig+14-14
......@@ -23,9 +23,9 @@ pub const FormatContext = struct {
2323pub fn formatSema(
2424 ctx: FormatContext,
2525 comptime fmt: []const u8,
26 options: std.fmt.FormatOptions,
27 writer: anytype,
28) !void {
26 options: std.fmt.Options,
27 writer: *std.io.BufferedWriter,
28) anyerror!void {
2929 _ = options;
3030 const sema = ctx.opt_sema.?;
3131 comptime std.debug.assert(fmt.len == 0);
......@@ -40,9 +40,9 @@ pub fn formatSema(
4040pub fn format(
4141 ctx: FormatContext,
4242 comptime fmt: []const u8,
43 options: std.fmt.FormatOptions,
44 writer: anytype,
45) !void {
43 options: std.fmt.Options,
44 writer: *std.io.BufferedWriter,
45) anyerror!void {
4646 _ = options;
4747 std.debug.assert(ctx.opt_sema == null);
4848 comptime std.debug.assert(fmt.len == 0);
......@@ -55,11 +55,11 @@ pub fn format(
5555
5656pub fn print(
5757 val: Value,
58 writer: anytype,
58 writer: *std.io.BufferedWriter,
5959 level: u8,
6060 pt: Zcu.PerThread,
6161 opt_sema: ?*Sema,
62) (@TypeOf(writer).Error || Zcu.CompileError)!void {
62) anyerror!void {
6363 const zcu = pt.zcu;
6464 const ip = &zcu.intern_pool;
6565 switch (ip.indexToKey(val.toIntern())) {
......@@ -197,11 +197,11 @@ fn printAggregate(
197197 val: Value,
198198 aggregate: InternPool.Key.Aggregate,
199199 is_ref: bool,
200 writer: anytype,
200 writer: *std.io.BufferedWriter,
201201 level: u8,
202202 pt: Zcu.PerThread,
203203 opt_sema: ?*Sema,
204) (@TypeOf(writer).Error || Zcu.CompileError)!void {
204) anyerror!void {
205205 if (level == 0) {
206206 if (is_ref) try writer.writeByte('&');
207207 return writer.writeAll(".{ ... }");
......@@ -283,11 +283,11 @@ fn printPtr(
283283 ptr_val: Value,
284284 /// Whether to print `derivation` as an lvalue or rvalue. If `null`, the more concise option is chosen.
285285 want_kind: ?PrintPtrKind,
286 writer: anytype,
286 writer: *std.io.BufferedWriter,
287287 level: u8,
288288 pt: Zcu.PerThread,
289289 opt_sema: ?*Sema,
290) (@TypeOf(writer).Error || Zcu.CompileError)!void {
290) anyerror!void {
291291 const ptr = switch (pt.zcu.intern_pool.indexToKey(ptr_val.toIntern())) {
292292 .undef => return writer.writeAll("undefined"),
293293 .ptr => |ptr| ptr,
......@@ -329,7 +329,7 @@ const PrintPtrKind = enum { lvalue, rvalue };
329329/// Returns the root derivation, which may be ignored.
330330pub fn printPtrDerivation(
331331 derivation: Value.PointerDeriveStep,
332 writer: anytype,
332 writer: *std.io.BufferedWriter,
333333 pt: Zcu.PerThread,
334334 /// Whether to print `derivation` as an lvalue or rvalue. If `null`, the more concise option is chosen.
335335 /// If this is `.rvalue`, the result may look like `&foo`, so it's not necessarily valid to treat it as
......@@ -347,7 +347,7 @@ pub fn printPtrDerivation(
347347 /// The maximum recursion depth. We can never recurse infinitely here, but the depth can be arbitrary,
348348 /// so at this depth we just write "..." to prevent stack overflow.
349349 ptr_depth: u8,
350) !Value.PointerDeriveStep {
350) anyerror!Value.PointerDeriveStep {
351351 const zcu = pt.zcu;
352352 const ip = &zcu.intern_pool;
353353