authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-02 16:31:48-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-07 22:43:52-07:00
log6963a1c7b97e459be0a5f3ca913ffa0862a099a8
treeae0a804c56aa674e2b9f98f6152a2ce1c2c4334a
parent6314e6f238758ce84a288e9ed990540232d04656

C backend: prepare for merge


1 files changed, 1763 insertions(+), 1762 deletions(-)

src/codegen/c.zig+1763-1762
......@@ -4,6 +4,7 @@ const assert = std.debug.assert;
44const mem = std.mem;
55const log = std.log.scoped(.c);
66const Allocator = mem.Allocator;
7const Writer = std.io.Writer;
78
89const dev = @import("../dev.zig");
910const link = @import("../link.zig");
......@@ -340,28 +341,28 @@ fn isReservedIdent(ident: []const u8) bool {
340341 } else return reserved_idents.has(ident);
341342}
342343
343fn formatIdentSolo(ident: []const u8, writer: *std.io.Writer) std.io.Writer.Error!void {
344 return formatIdentOptions(ident, writer, true);
344fn formatIdentSolo(ident: []const u8, w: *std.io.Writer) std.io.Writer.Error!void {
345 return formatIdentOptions(ident, w, true);
345346}
346347
347fn formatIdentUnsolo(ident: []const u8, writer: *std.io.Writer) std.io.Writer.Error!void {
348 return formatIdentOptions(ident, writer, false);
348fn formatIdentUnsolo(ident: []const u8, w: *std.io.Writer) std.io.Writer.Error!void {
349 return formatIdentOptions(ident, w, false);
349350}
350351
351fn formatIdentOptions(ident: []const u8, writer: *std.io.Writer, solo: bool) std.io.Writer.Error!void {
352fn formatIdentOptions(ident: []const u8, w: *std.io.Writer, solo: bool) std.io.Writer.Error!void {
352353 if (solo and isReservedIdent(ident)) {
353 try writer.writeAll("zig_e_");
354 try w.writeAll("zig_e_");
354355 }
355356 for (ident, 0..) |c, i| {
356357 switch (c) {
357 'a'...'z', 'A'...'Z', '_' => try writer.writeByte(c),
358 '.' => try writer.writeByte('_'),
358 'a'...'z', 'A'...'Z', '_' => try w.writeByte(c),
359 '.' => try w.writeByte('_'),
359360 '0'...'9' => if (i == 0) {
360 try writer.print("_{x:2}", .{c});
361 try w.print("_{x:2}", .{c});
361362 } else {
362 try writer.writeByte(c);
363 try w.writeByte(c);
363364 },
364 else => try writer.print("_{x:2}", .{c}),
365 else => try w.print("_{x:2}", .{c}),
365366 }
366367 }
367368}
......@@ -379,11 +380,11 @@ const CTypePoolStringFormatData = struct {
379380 ctype_pool: *const CType.Pool,
380381 solo: bool,
381382};
382fn formatCTypePoolString(data: CTypePoolStringFormatData, writer: *std.io.Writer) std.io.Writer.Error!void {
383fn formatCTypePoolString(data: CTypePoolStringFormatData, w: *std.io.Writer) std.io.Writer.Error!void {
383384 if (data.ctype_pool_string.toSlice(data.ctype_pool)) |slice|
384 try formatIdentOptions(slice, writer, data.solo)
385 try formatIdentOptions(slice, w, data.solo)
385386 else
386 try writer.print("{}", .{data.ctype_pool_string.fmt(data.ctype_pool)});
387 try w.print("{}", .{data.ctype_pool_string.fmt(data.ctype_pool)});
387388}
388389pub fn fmtCTypePoolString(
389390 ctype_pool_string: CType.Pool.String,
......@@ -448,18 +449,18 @@ pub const Function = struct {
448449 const ty = f.typeOf(ref);
449450
450451 const result: CValue = if (lowersToArray(ty, pt)) result: {
451 const writer = f.object.codeHeaderWriter();
452 const w = f.object.codeHeaderWriter();
452453 const decl_c_value = try f.allocLocalValue(.{
453454 .ctype = try f.ctypeFromType(ty, .complete),
454455 .alignas = CType.AlignAs.fromAbiAlignment(ty.abiAlignment(pt.zcu)),
455456 });
456457 const gpa = f.object.dg.gpa;
457458 try f.allocs.put(gpa, decl_c_value.new_local, false);
458 try writer.writeAll("static ");
459 try f.object.dg.renderTypeAndName(writer, ty, decl_c_value, Const, .none, .complete);
460 try writer.writeAll(" = ");
461 try f.object.dg.renderValue(writer, val, .StaticInitializer);
462 try writer.writeAll(";\n ");
459 try w.writeAll("static ");
460 try f.object.dg.renderTypeAndName(w, ty, decl_c_value, Const, .none, .complete);
461 try w.writeAll(" = ");
462 try f.object.dg.renderValue(w, val, .StaticInitializer);
463 try w.writeAll(";\n ");
463464 break :result .{ .local = decl_c_value.new_local };
464465 } else .{ .constant = val };
465466
......@@ -512,7 +513,7 @@ pub const Function = struct {
512513 return result;
513514 }
514515
515 fn writeCValue(f: *Function, w: anytype, c_value: CValue, location: ValueRenderLocation) !void {
516 fn writeCValue(f: *Function, w: *Writer, c_value: CValue, location: ValueRenderLocation) !void {
516517 switch (c_value) {
517518 .none => unreachable,
518519 .new_local, .local => |i| try w.print("t{d}", .{i}),
......@@ -525,7 +526,7 @@ pub const Function = struct {
525526 }
526527 }
527528
528 fn writeCValueDeref(f: *Function, w: anytype, c_value: CValue) !void {
529 fn writeCValueDeref(f: *Function, w: *Writer, c_value: CValue) !void {
529530 switch (c_value) {
530531 .none => unreachable,
531532 .new_local, .local, .constant => {
......@@ -546,38 +547,38 @@ pub const Function = struct {
546547
547548 fn writeCValueMember(
548549 f: *Function,
549 writer: anytype,
550 w: *Writer,
550551 c_value: CValue,
551552 member: CValue,
552553 ) error{ OutOfMemory, AnalysisFail }!void {
553554 switch (c_value) {
554555 .new_local, .local, .local_ref, .constant, .arg, .arg_array => {
555 try f.writeCValue(writer, c_value, .Other);
556 try writer.writeByte('.');
557 try f.writeCValue(writer, member, .Other);
556 try f.writeCValue(w, c_value, .Other);
557 try w.writeByte('.');
558 try f.writeCValue(w, member, .Other);
558559 },
559 else => return f.object.dg.writeCValueMember(writer, c_value, member),
560 else => return f.object.dg.writeCValueMember(w, c_value, member),
560561 }
561562 }
562563
563 fn writeCValueDerefMember(f: *Function, writer: anytype, c_value: CValue, member: CValue) !void {
564 fn writeCValueDerefMember(f: *Function, w: *Writer, c_value: CValue, member: CValue) !void {
564565 switch (c_value) {
565566 .new_local, .local, .arg, .arg_array => {
566 try f.writeCValue(writer, c_value, .Other);
567 try writer.writeAll("->");
567 try f.writeCValue(w, c_value, .Other);
568 try w.writeAll("->");
568569 },
569570 .constant => {
570 try writer.writeByte('(');
571 try f.writeCValue(writer, c_value, .Other);
572 try writer.writeAll(")->");
571 try w.writeByte('(');
572 try f.writeCValue(w, c_value, .Other);
573 try w.writeAll(")->");
573574 },
574575 .local_ref => {
575 try f.writeCValueDeref(writer, c_value);
576 try writer.writeByte('.');
576 try f.writeCValueDeref(w, c_value);
577 try w.writeByte('.');
577578 },
578 else => return f.object.dg.writeCValueDerefMember(writer, c_value, member),
579 else => return f.object.dg.writeCValueDerefMember(w, c_value, member),
579580 }
580 try f.writeCValue(writer, member, .Other);
581 try f.writeCValue(w, member, .Other);
581582 }
582583
583584 fn fail(f: *Function, comptime format: []const u8, args: anytype) error{ AnalysisFail, OutOfMemory } {
......@@ -592,15 +593,15 @@ pub const Function = struct {
592593 return f.object.dg.byteSize(ctype);
593594 }
594595
595 fn renderType(f: *Function, w: anytype, ctype: Type) !void {
596 fn renderType(f: *Function, w: *Writer, ctype: Type) !void {
596597 return f.object.dg.renderType(w, ctype);
597598 }
598599
599 fn renderCType(f: *Function, w: anytype, ctype: CType) !void {
600 fn renderCType(f: *Function, w: *Writer, ctype: CType) !void {
600601 return f.object.dg.renderCType(w, ctype);
601602 }
602603
603 fn renderIntCast(f: *Function, w: anytype, dest_ty: Type, src: CValue, v: Vectorize, src_ty: Type, location: ValueRenderLocation) !void {
604 fn renderIntCast(f: *Function, w: *Writer, dest_ty: Type, src: CValue, v: Vectorize, src_ty: Type, location: ValueRenderLocation) !void {
604605 return f.object.dg.renderIntCast(w, dest_ty, .{ .c_value = .{ .f = f, .value = src, .v = v } }, src_ty, location);
605606 }
606607
......@@ -671,12 +672,12 @@ pub const Function = struct {
671672 },
672673 else => {},
673674 }
674 const writer = f.object.writer();
675 const a = try Assignment.start(f, writer, ctype);
676 try f.writeCValue(writer, dst, .Other);
677 try a.assign(f, writer);
678 try f.writeCValue(writer, src, .Other);
679 try a.end(f, writer);
675 const w = f.object.writer();
676 const a = try Assignment.start(f, w, ctype);
677 try f.writeCValue(w, dst, .Other);
678 try a.assign(f, w);
679 try f.writeCValue(w, src, .Other);
680 try a.end(f, w);
680681 }
681682
682683 fn moveCValue(f: *Function, inst: Air.Inst.Index, ty: Type, src: CValue) !CValue {
......@@ -711,7 +712,7 @@ pub const Object = struct {
711712 code_header: std.ArrayList(u8) = undefined,
712713 indent_writer: IndentWriter(std.ArrayList(u8).Writer),
713714
714 fn writer(o: *Object) IndentWriter(std.ArrayList(u8).Writer).Writer {
715 fn w(o: *Object) IndentWriter(std.ArrayList(u8).Writer).Writer {
715716 return o.indent_writer.writer();
716717 }
717718
......@@ -760,7 +761,7 @@ pub const DeclGen = struct {
760761
761762 fn renderUav(
762763 dg: *DeclGen,
763 writer: anytype,
764 w: *Writer,
764765 uav: InternPool.Key.Ptr.BaseAddr.Uav,
765766 location: ValueRenderLocation,
766767 ) error{ OutOfMemory, AnalysisFail }!void {
......@@ -774,14 +775,14 @@ pub const DeclGen = struct {
774775 // Render an undefined pointer if we have a pointer to a zero-bit or comptime type.
775776 const ptr_ty: Type = .fromInterned(uav.orig_ty);
776777 if (ptr_ty.isPtrAtRuntime(zcu) and !uav_ty.isFnOrHasRuntimeBits(zcu)) {
777 return dg.writeCValue(writer, .{ .undef = ptr_ty });
778 return dg.writeCValue(w, .{ .undef = ptr_ty });
778779 }
779780
780781 // Chase function values in order to be able to reference the original function.
781782 switch (ip.indexToKey(uav.val)) {
782783 .variable => unreachable,
783 .func => |func| return dg.renderNav(writer, func.owner_nav, location),
784 .@"extern" => |@"extern"| return dg.renderNav(writer, @"extern".owner_nav, location),
784 .func => |func| return dg.renderNav(w, func.owner_nav, location),
785 .@"extern" => |@"extern"| return dg.renderNav(w, @"extern".owner_nav, location),
785786 else => {},
786787 }
787788
......@@ -795,13 +796,13 @@ pub const DeclGen = struct {
795796 const need_cast = !elem_ctype.eql(uav_ctype) and
796797 (elem_ctype.info(ctype_pool) != .function or uav_ctype.info(ctype_pool) != .function);
797798 if (need_cast) {
798 try writer.writeAll("((");
799 try dg.renderCType(writer, ptr_ctype);
800 try writer.writeByte(')');
799 try w.writeAll("((");
800 try dg.renderCType(w, ptr_ctype);
801 try w.writeByte(')');
801802 }
802 try writer.writeByte('&');
803 try renderUavName(writer, uav_val);
804 if (need_cast) try writer.writeByte(')');
803 try w.writeByte('&');
804 try renderUavName(w, uav_val);
805 if (need_cast) try w.writeByte(')');
805806
806807 // Indicate that the anon decl should be rendered to the output so that
807808 // our reference above is not undefined.
......@@ -822,7 +823,7 @@ pub const DeclGen = struct {
822823
823824 fn renderNav(
824825 dg: *DeclGen,
825 writer: anytype,
826 w: *Writer,
826827 nav_index: InternPool.Nav.Index,
827828 location: ValueRenderLocation,
828829 ) error{ OutOfMemory, AnalysisFail }!void {
......@@ -847,7 +848,7 @@ pub const DeclGen = struct {
847848 const nav_ty: Type = .fromInterned(ip.getNav(owner_nav).typeOf(ip));
848849 const ptr_ty = try pt.navPtrType(owner_nav);
849850 if (!nav_ty.isFnOrHasRuntimeBits(zcu)) {
850 return dg.writeCValue(writer, .{ .undef = ptr_ty });
851 return dg.writeCValue(w, .{ .undef = ptr_ty });
851852 }
852853
853854 // We shouldn't cast C function pointers as this is UB (when you call
......@@ -860,18 +861,18 @@ pub const DeclGen = struct {
860861 const need_cast = !elem_ctype.eql(nav_ctype) and
861862 (elem_ctype.info(ctype_pool) != .function or nav_ctype.info(ctype_pool) != .function);
862863 if (need_cast) {
863 try writer.writeAll("((");
864 try dg.renderCType(writer, ctype);
865 try writer.writeByte(')');
864 try w.writeAll("((");
865 try dg.renderCType(w, ctype);
866 try w.writeByte(')');
866867 }
867 try writer.writeByte('&');
868 try dg.renderNavName(writer, owner_nav);
869 if (need_cast) try writer.writeByte(')');
868 try w.writeByte('&');
869 try dg.renderNavName(w, owner_nav);
870 if (need_cast) try w.writeByte(')');
870871 }
871872
872873 fn renderPointer(
873874 dg: *DeclGen,
874 writer: anytype,
875 w: *Writer,
875876 derivation: Value.PointerDeriveStep,
876877 location: ValueRenderLocation,
877878 ) error{ OutOfMemory, AnalysisFail }!void {
......@@ -882,18 +883,18 @@ pub const DeclGen = struct {
882883 .int => |int| {
883884 const ptr_ctype = try dg.ctypeFromType(int.ptr_ty, .complete);
884885 const addr_val = try pt.intValue(.usize, int.addr);
885 try writer.writeByte('(');
886 try dg.renderCType(writer, ptr_ctype);
887 try writer.print("){f}", .{try dg.fmtIntLiteralHex(addr_val, .Other)});
886 try w.writeByte('(');
887 try dg.renderCType(w, ptr_ctype);
888 try w.print("){f}", .{try dg.fmtIntLiteralHex(addr_val, .Other)});
888889 },
889890
890 .nav_ptr => |nav| try dg.renderNav(writer, nav, location),
891 .uav_ptr => |uav| try dg.renderUav(writer, uav, location),
891 .nav_ptr => |nav| try dg.renderNav(w, nav, location),
892 .uav_ptr => |uav| try dg.renderUav(w, uav, location),
892893
893894 inline .eu_payload_ptr, .opt_payload_ptr => |info| {
894 try writer.writeAll("&(");
895 try dg.renderPointer(writer, info.parent.*, location);
896 try writer.writeAll(")->payload");
895 try w.writeAll("&(");
896 try dg.renderPointer(w, info.parent.*, location);
897 try w.writeAll(")->payload");
897898 },
898899
899900 .field_ptr => |field| {
......@@ -905,26 +906,26 @@ pub const DeclGen = struct {
905906 switch (fieldLocation(parent_ptr_ty, field.result_ptr_ty, field.field_idx, pt)) {
906907 .begin => {
907908 const ptr_ctype = try dg.ctypeFromType(field.result_ptr_ty, .complete);
908 try writer.writeByte('(');
909 try dg.renderCType(writer, ptr_ctype);
910 try writer.writeByte(')');
911 try dg.renderPointer(writer, field.parent.*, location);
909 try w.writeByte('(');
910 try dg.renderCType(w, ptr_ctype);
911 try w.writeByte(')');
912 try dg.renderPointer(w, field.parent.*, location);
912913 },
913914 .field => |name| {
914 try writer.writeAll("&(");
915 try dg.renderPointer(writer, field.parent.*, location);
916 try writer.writeAll(")->");
917 try dg.writeCValue(writer, name);
915 try w.writeAll("&(");
916 try dg.renderPointer(w, field.parent.*, location);
917 try w.writeAll(")->");
918 try dg.writeCValue(w, name);
918919 },
919920 .byte_offset => |byte_offset| {
920921 const ptr_ctype = try dg.ctypeFromType(field.result_ptr_ty, .complete);
921 try writer.writeByte('(');
922 try dg.renderCType(writer, ptr_ctype);
923 try writer.writeByte(')');
922 try w.writeByte('(');
923 try dg.renderCType(w, ptr_ctype);
924 try w.writeByte(')');
924925 const offset_val = try pt.intValue(.usize, byte_offset);
925 try writer.writeAll("((char *)");
926 try dg.renderPointer(writer, field.parent.*, location);
927 try writer.print(" + {f})", .{try dg.fmtIntLiteralDec(offset_val, .Other)});
926 try w.writeAll("((char *)");
927 try dg.renderPointer(w, field.parent.*, location);
928 try w.print(" + {f})", .{try dg.fmtIntLiteralDec(offset_val, .Other)});
928929 },
929930 }
930931 },
......@@ -932,10 +933,10 @@ pub const DeclGen = struct {
932933 .elem_ptr => |elem| if (!(try elem.parent.ptrType(pt)).childType(zcu).hasRuntimeBits(zcu)) {
933934 // Element type is zero-bit, so lowers to `void`. The index is irrelevant; just cast the pointer.
934935 const ptr_ctype = try dg.ctypeFromType(elem.result_ptr_ty, .complete);
935 try writer.writeByte('(');
936 try dg.renderCType(writer, ptr_ctype);
937 try writer.writeByte(')');
938 try dg.renderPointer(writer, elem.parent.*, location);
936 try w.writeByte('(');
937 try dg.renderCType(w, ptr_ctype);
938 try w.writeByte(')');
939 try dg.renderPointer(w, elem.parent.*, location);
939940 } else {
940941 const index_val = try pt.intValue(.usize, elem.elem_idx);
941942 // We want to do pointer arithmetic on a pointer to the element type.
......@@ -944,45 +945,45 @@ pub const DeclGen = struct {
944945 const parent_ctype = try dg.ctypeFromType(try elem.parent.ptrType(pt), .complete);
945946 if (result_ctype.eql(parent_ctype)) {
946947 // The pointer already has an appropriate type - just do the arithmetic.
947 try writer.writeByte('(');
948 try dg.renderPointer(writer, elem.parent.*, location);
949 try writer.print(" + {f})", .{try dg.fmtIntLiteralDec(index_val, .Other)});
948 try w.writeByte('(');
949 try dg.renderPointer(w, elem.parent.*, location);
950 try w.print(" + {f})", .{try dg.fmtIntLiteralDec(index_val, .Other)});
950951 } else {
951952 // We probably have an array pointer `T (*)[n]`. Cast to an element pointer,
952953 // and *then* apply the index.
953 try writer.writeAll("((");
954 try dg.renderCType(writer, result_ctype);
955 try writer.writeByte(')');
956 try dg.renderPointer(writer, elem.parent.*, location);
957 try writer.print(" + {f})", .{try dg.fmtIntLiteralDec(index_val, .Other)});
954 try w.writeAll("((");
955 try dg.renderCType(w, result_ctype);
956 try w.writeByte(')');
957 try dg.renderPointer(w, elem.parent.*, location);
958 try w.print(" + {f})", .{try dg.fmtIntLiteralDec(index_val, .Other)});
958959 }
959960 },
960961
961962 .offset_and_cast => |oac| {
962963 const ptr_ctype = try dg.ctypeFromType(oac.new_ptr_ty, .complete);
963 try writer.writeByte('(');
964 try dg.renderCType(writer, ptr_ctype);
965 try writer.writeByte(')');
964 try w.writeByte('(');
965 try dg.renderCType(w, ptr_ctype);
966 try w.writeByte(')');
966967 if (oac.byte_offset == 0) {
967 try dg.renderPointer(writer, oac.parent.*, location);
968 try dg.renderPointer(w, oac.parent.*, location);
968969 } else {
969970 const offset_val = try pt.intValue(.usize, oac.byte_offset);
970 try writer.writeAll("((char *)");
971 try dg.renderPointer(writer, oac.parent.*, location);
972 try writer.print(" + {f})", .{try dg.fmtIntLiteralDec(offset_val, .Other)});
971 try w.writeAll("((char *)");
972 try dg.renderPointer(w, oac.parent.*, location);
973 try w.print(" + {f})", .{try dg.fmtIntLiteralDec(offset_val, .Other)});
973974 }
974975 },
975976 }
976977 }
977978
978 fn renderErrorName(dg: *DeclGen, writer: anytype, err_name: InternPool.NullTerminatedString) !void {
979 fn renderErrorName(dg: *DeclGen, w: *Writer, err_name: InternPool.NullTerminatedString) !void {
979980 const ip = &dg.pt.zcu.intern_pool;
980 try writer.print("zig_error_{}", .{fmtIdentUnsolo(err_name.toSlice(ip))});
981 try w.print("zig_error_{}", .{fmtIdentUnsolo(err_name.toSlice(ip))});
981982 }
982983
983984 fn renderValue(
984985 dg: *DeclGen,
985 writer: anytype,
986 w: *Writer,
986987 val: Value,
987988 location: ValueRenderLocation,
988989 ) error{ OutOfMemory, AnalysisFail }!void {
......@@ -998,7 +999,7 @@ pub const DeclGen = struct {
998999 };
9991000
10001001 const ty = val.typeOf(zcu);
1001 if (val.isUndefDeep(zcu)) return dg.renderUndefValue(writer, ty, location);
1002 if (val.isUndefDeep(zcu)) return dg.renderUndefValue(w, ty, location);
10021003 const ctype = try dg.ctypeFromType(ty, location.toCTypeKind());
10031004 switch (ip.indexToKey(val.toIntern())) {
10041005 // types, not values
......@@ -1031,8 +1032,8 @@ pub const DeclGen = struct {
10311032 .empty_tuple => unreachable,
10321033 .@"unreachable" => unreachable,
10331034
1034 .false => try writer.writeAll("false"),
1035 .true => try writer.writeAll("true"),
1035 .false => try w.writeAll("false"),
1036 .true => try w.writeAll("true"),
10361037 },
10371038 .variable,
10381039 .@"extern",
......@@ -1041,45 +1042,45 @@ pub const DeclGen = struct {
10411042 .empty_enum_value,
10421043 => unreachable, // non-runtime values
10431044 .int => |int| switch (int.storage) {
1044 .u64, .i64, .big_int => try writer.print("{f}", .{try dg.fmtIntLiteralDec(val, location)}),
1045 .u64, .i64, .big_int => try w.print("{f}", .{try dg.fmtIntLiteralDec(val, location)}),
10451046 .lazy_align, .lazy_size => {
1046 try writer.writeAll("((");
1047 try dg.renderCType(writer, ctype);
1048 try writer.print("){f})", .{try dg.fmtIntLiteralHex(
1047 try w.writeAll("((");
1048 try dg.renderCType(w, ctype);
1049 try w.print("){f})", .{try dg.fmtIntLiteralHex(
10491050 try pt.intValue(.usize, val.toUnsignedInt(zcu)),
10501051 .Other,
10511052 )});
10521053 },
10531054 },
1054 .err => |err| try dg.renderErrorName(writer, err.name),
1055 .err => |err| try dg.renderErrorName(w, err.name),
10551056 .error_union => |error_union| switch (ctype.info(ctype_pool)) {
10561057 .basic => switch (error_union.val) {
1057 .err_name => |err_name| try dg.renderErrorName(writer, err_name),
1058 .payload => try writer.writeAll("0"),
1058 .err_name => |err_name| try dg.renderErrorName(w, err_name),
1059 .payload => try w.writeAll("0"),
10591060 },
10601061 .pointer, .aligned, .array, .vector, .fwd_decl, .function => unreachable,
10611062 .aggregate => |aggregate| {
10621063 if (!location.isInitializer()) {
1063 try writer.writeByte('(');
1064 try dg.renderCType(writer, ctype);
1065 try writer.writeByte(')');
1064 try w.writeByte('(');
1065 try dg.renderCType(w, ctype);
1066 try w.writeByte(')');
10661067 }
1067 try writer.writeByte('{');
1068 try w.writeByte('{');
10681069 for (0..aggregate.fields.len) |field_index| {
1069 if (field_index > 0) try writer.writeByte(',');
1070 if (field_index > 0) try w.writeByte(',');
10701071 switch (aggregate.fields.at(field_index, ctype_pool).name.index) {
10711072 .@"error" => switch (error_union.val) {
1072 .err_name => |err_name| try dg.renderErrorName(writer, err_name),
1073 .payload => try writer.writeByte('0'),
1073 .err_name => |err_name| try dg.renderErrorName(w, err_name),
1074 .payload => try w.writeByte('0'),
10741075 },
10751076 .payload => switch (error_union.val) {
10761077 .err_name => try dg.renderUndefValue(
1077 writer,
1078 w,
10781079 ty.errorUnionPayload(zcu),
10791080 initializer_type,
10801081 ),
10811082 .payload => |payload| try dg.renderValue(
1082 writer,
1083 w,
10831084 Value.fromInterned(payload),
10841085 initializer_type,
10851086 ),
......@@ -1087,10 +1088,10 @@ pub const DeclGen = struct {
10871088 else => unreachable,
10881089 }
10891090 }
1090 try writer.writeByte('}');
1091 try w.writeByte('}');
10911092 },
10921093 },
1093 .enum_tag => |enum_tag| try dg.renderValue(writer, Value.fromInterned(enum_tag.int), location),
1094 .enum_tag => |enum_tag| try dg.renderValue(w, Value.fromInterned(enum_tag.int), location),
10941095 .float => {
10951096 const bits = ty.floatBits(target);
10961097 const f128_val = val.toFloat(f128, zcu);
......@@ -1117,18 +1118,18 @@ pub const DeclGen = struct {
11171118
11181119 var empty = true;
11191120 if (std.math.isFinite(f128_val)) {
1120 try writer.writeAll("zig_make_");
1121 try dg.renderTypeForBuiltinFnName(writer, ty);
1122 try writer.writeByte('(');
1121 try w.writeAll("zig_make_");
1122 try dg.renderTypeForBuiltinFnName(w, ty);
1123 try w.writeByte('(');
11231124 switch (bits) {
1124 16 => try writer.print("{x}", .{val.toFloat(f16, zcu)}),
1125 32 => try writer.print("{x}", .{val.toFloat(f32, zcu)}),
1126 64 => try writer.print("{x}", .{val.toFloat(f64, zcu)}),
1127 80 => try writer.print("{x}", .{val.toFloat(f80, zcu)}),
1128 128 => try writer.print("{x}", .{f128_val}),
1125 16 => try w.print("{x}", .{val.toFloat(f16, zcu)}),
1126 32 => try w.print("{x}", .{val.toFloat(f32, zcu)}),
1127 64 => try w.print("{x}", .{val.toFloat(f64, zcu)}),
1128 80 => try w.print("{x}", .{val.toFloat(f80, zcu)}),
1129 128 => try w.print("{x}", .{f128_val}),
11291130 else => unreachable,
11301131 }
1131 try writer.writeAll(", ");
1132 try w.writeAll(", ");
11321133 empty = false;
11331134 } else {
11341135 // isSignalNan is equivalent to isNan currently, and MSVC doesn't have nans, so prefer nan
......@@ -1152,45 +1153,45 @@ pub const DeclGen = struct {
11521153 // return dg.fail("Only quiet nans are supported in global variable initializers", .{});
11531154 }
11541155
1155 try writer.writeAll("zig_");
1156 try writer.writeAll(if (location == .StaticInitializer) "init" else "make");
1157 try writer.writeAll("_special_");
1158 try dg.renderTypeForBuiltinFnName(writer, ty);
1159 try writer.writeByte('(');
1160 if (std.math.signbit(f128_val)) try writer.writeByte('-');
1161 try writer.writeAll(", ");
1162 try writer.writeAll(operation);
1163 try writer.writeAll(", ");
1156 try w.writeAll("zig_");
1157 try w.writeAll(if (location == .StaticInitializer) "init" else "make");
1158 try w.writeAll("_special_");
1159 try dg.renderTypeForBuiltinFnName(w, ty);
1160 try w.writeByte('(');
1161 if (std.math.signbit(f128_val)) try w.writeByte('-');
1162 try w.writeAll(", ");
1163 try w.writeAll(operation);
1164 try w.writeAll(", ");
11641165 if (std.math.isNan(f128_val)) switch (bits) {
11651166 // We only actually need to pass the significand, but it will get
11661167 // properly masked anyway, so just pass the whole value.
1167 16 => try writer.print("\"0x{x}\"", .{@as(u16, @bitCast(val.toFloat(f16, zcu)))}),
1168 32 => try writer.print("\"0x{x}\"", .{@as(u32, @bitCast(val.toFloat(f32, zcu)))}),
1169 64 => try writer.print("\"0x{x}\"", .{@as(u64, @bitCast(val.toFloat(f64, zcu)))}),
1170 80 => try writer.print("\"0x{x}\"", .{@as(u80, @bitCast(val.toFloat(f80, zcu)))}),
1171 128 => try writer.print("\"0x{x}\"", .{@as(u128, @bitCast(f128_val))}),
1168 16 => try w.print("\"0x{x}\"", .{@as(u16, @bitCast(val.toFloat(f16, zcu)))}),
1169 32 => try w.print("\"0x{x}\"", .{@as(u32, @bitCast(val.toFloat(f32, zcu)))}),
1170 64 => try w.print("\"0x{x}\"", .{@as(u64, @bitCast(val.toFloat(f64, zcu)))}),
1171 80 => try w.print("\"0x{x}\"", .{@as(u80, @bitCast(val.toFloat(f80, zcu)))}),
1172 128 => try w.print("\"0x{x}\"", .{@as(u128, @bitCast(f128_val))}),
11721173 else => unreachable,
11731174 };
1174 try writer.writeAll(", ");
1175 try w.writeAll(", ");
11751176 empty = false;
11761177 }
1177 try writer.print("{f}", .{try dg.fmtIntLiteralHex(
1178 try w.print("{f}", .{try dg.fmtIntLiteralHex(
11781179 try pt.intValue_big(repr_ty, repr_val_big.toConst()),
11791180 location,
11801181 )});
1181 if (!empty) try writer.writeByte(')');
1182 if (!empty) try w.writeByte(')');
11821183 },
11831184 .slice => |slice| {
11841185 const aggregate = ctype.info(ctype_pool).aggregate;
11851186 if (!location.isInitializer()) {
1186 try writer.writeByte('(');
1187 try dg.renderCType(writer, ctype);
1188 try writer.writeByte(')');
1187 try w.writeByte('(');
1188 try dg.renderCType(w, ctype);
1189 try w.writeByte(')');
11891190 }
1190 try writer.writeByte('{');
1191 try w.writeByte('{');
11911192 for (0..aggregate.fields.len) |field_index| {
1192 if (field_index > 0) try writer.writeByte(',');
1193 try dg.renderValue(writer, Value.fromInterned(
1193 if (field_index > 0) try w.writeByte(',');
1194 try dg.renderValue(w, Value.fromInterned(
11941195 switch (aggregate.fields.at(field_index, ctype_pool).name.index) {
11951196 .ptr => slice.ptr,
11961197 .len => slice.len,
......@@ -1198,33 +1199,33 @@ pub const DeclGen = struct {
11981199 },
11991200 ), initializer_type);
12001201 }
1201 try writer.writeByte('}');
1202 try w.writeByte('}');
12021203 },
12031204 .ptr => {
12041205 var arena = std.heap.ArenaAllocator.init(zcu.gpa);
12051206 defer arena.deinit();
12061207 const derivation = try val.pointerDerivation(arena.allocator(), pt);
1207 try dg.renderPointer(writer, derivation, location);
1208 try dg.renderPointer(w, derivation, location);
12081209 },
12091210 .opt => |opt| switch (ctype.info(ctype_pool)) {
1210 .basic => if (ctype.isBool()) try writer.writeAll(switch (opt.val) {
1211 .basic => if (ctype.isBool()) try w.writeAll(switch (opt.val) {
12111212 .none => "true",
12121213 else => "false",
12131214 }) else switch (opt.val) {
1214 .none => try writer.writeAll("0"),
1215 .none => try w.writeAll("0"),
12151216 else => |payload| switch (ip.indexToKey(payload)) {
12161217 .undef => |err_ty| try dg.renderUndefValue(
1217 writer,
1218 w,
12181219 .fromInterned(err_ty),
12191220 location,
12201221 ),
1221 .err => |err| try dg.renderErrorName(writer, err.name),
1222 .err => |err| try dg.renderErrorName(w, err.name),
12221223 else => unreachable,
12231224 },
12241225 },
12251226 .pointer => switch (opt.val) {
1226 .none => try writer.writeAll("NULL"),
1227 else => |payload| try dg.renderValue(writer, Value.fromInterned(payload), location),
1227 .none => try w.writeAll("NULL"),
1228 else => |payload| try dg.renderValue(w, Value.fromInterned(payload), location),
12281229 },
12291230 .aligned, .array, .vector, .fwd_decl, .function => unreachable,
12301231 .aggregate => |aggregate| {
......@@ -1233,7 +1234,7 @@ pub const DeclGen = struct {
12331234 else => |payload| switch (aggregate.fields.at(0, ctype_pool).name.index) {
12341235 .is_null, .payload => {},
12351236 .ptr, .len => return dg.renderValue(
1236 writer,
1237 w,
12371238 Value.fromInterned(payload),
12381239 location,
12391240 ),
......@@ -1241,48 +1242,48 @@ pub const DeclGen = struct {
12411242 },
12421243 }
12431244 if (!location.isInitializer()) {
1244 try writer.writeByte('(');
1245 try dg.renderCType(writer, ctype);
1246 try writer.writeByte(')');
1245 try w.writeByte('(');
1246 try dg.renderCType(w, ctype);
1247 try w.writeByte(')');
12471248 }
1248 try writer.writeByte('{');
1249 try w.writeByte('{');
12491250 for (0..aggregate.fields.len) |field_index| {
1250 if (field_index > 0) try writer.writeByte(',');
1251 if (field_index > 0) try w.writeByte(',');
12511252 switch (aggregate.fields.at(field_index, ctype_pool).name.index) {
1252 .is_null => try writer.writeAll(switch (opt.val) {
1253 .is_null => try w.writeAll(switch (opt.val) {
12531254 .none => "true",
12541255 else => "false",
12551256 }),
12561257 .payload => switch (opt.val) {
12571258 .none => try dg.renderUndefValue(
1258 writer,
1259 w,
12591260 ty.optionalChild(zcu),
12601261 initializer_type,
12611262 ),
12621263 else => |payload| try dg.renderValue(
1263 writer,
1264 w,
12641265 Value.fromInterned(payload),
12651266 initializer_type,
12661267 ),
12671268 },
1268 .ptr => try writer.writeAll("NULL"),
1269 .len => try dg.renderUndefValue(writer, .usize, initializer_type),
1269 .ptr => try w.writeAll("NULL"),
1270 .len => try dg.renderUndefValue(w, .usize, initializer_type),
12701271 else => unreachable,
12711272 }
12721273 }
1273 try writer.writeByte('}');
1274 try w.writeByte('}');
12741275 },
12751276 },
12761277 .aggregate => switch (ip.indexToKey(ty.toIntern())) {
12771278 .array_type, .vector_type => {
12781279 if (location == .FunctionArgument) {
1279 try writer.writeByte('(');
1280 try dg.renderCType(writer, ctype);
1281 try writer.writeByte(')');
1280 try w.writeByte('(');
1281 try dg.renderCType(w, ctype);
1282 try w.writeByte(')');
12821283 }
12831284 const ai = ty.arrayInfo(zcu);
12841285 if (ai.elem_type.eql(.u8, zcu)) {
1285 var literal: StringLiteral = .init(writer, ty.arrayLenIncludingSentinel(zcu));
1286 var literal: StringLiteral = .init(w, ty.arrayLenIncludingSentinel(zcu));
12861287 try literal.start();
12871288 var index: usize = 0;
12881289 while (index < ai.len) : (index += 1) {
......@@ -1299,28 +1300,28 @@ pub const DeclGen = struct {
12991300 }
13001301 try literal.end();
13011302 } else {
1302 try writer.writeByte('{');
1303 try w.writeByte('{');
13031304 var index: usize = 0;
13041305 while (index < ai.len) : (index += 1) {
1305 if (index != 0) try writer.writeByte(',');
1306 if (index != 0) try w.writeByte(',');
13061307 const elem_val = try val.elemValue(pt, index);
1307 try dg.renderValue(writer, elem_val, initializer_type);
1308 try dg.renderValue(w, elem_val, initializer_type);
13081309 }
13091310 if (ai.sentinel) |s| {
1310 if (index != 0) try writer.writeByte(',');
1311 try dg.renderValue(writer, s, initializer_type);
1311 if (index != 0) try w.writeByte(',');
1312 try dg.renderValue(w, s, initializer_type);
13121313 }
1313 try writer.writeByte('}');
1314 try w.writeByte('}');
13141315 }
13151316 },
13161317 .tuple_type => |tuple| {
13171318 if (!location.isInitializer()) {
1318 try writer.writeByte('(');
1319 try dg.renderCType(writer, ctype);
1320 try writer.writeByte(')');
1319 try w.writeByte('(');
1320 try dg.renderCType(w, ctype);
1321 try w.writeByte(')');
13211322 }
13221323
1323 try writer.writeByte('{');
1324 try w.writeByte('{');
13241325 var empty = true;
13251326 for (0..tuple.types.len) |field_index| {
13261327 const comptime_val = tuple.values.get(ip)[field_index];
......@@ -1328,7 +1329,7 @@ pub const DeclGen = struct {
13281329 const field_ty: Type = .fromInterned(tuple.types.get(ip)[field_index]);
13291330 if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue;
13301331
1331 if (!empty) try writer.writeByte(',');
1332 if (!empty) try w.writeByte(',');
13321333
13331334 const field_val = Value.fromInterned(
13341335 switch (ip.indexToKey(val.toIntern()).aggregate.storage) {
......@@ -1340,30 +1341,30 @@ pub const DeclGen = struct {
13401341 .repeated_elem => |elem| elem,
13411342 },
13421343 );
1343 try dg.renderValue(writer, field_val, initializer_type);
1344 try dg.renderValue(w, field_val, initializer_type);
13441345
13451346 empty = false;
13461347 }
1347 try writer.writeByte('}');
1348 try w.writeByte('}');
13481349 },
13491350 .struct_type => {
13501351 const loaded_struct = ip.loadStructType(ty.toIntern());
13511352 switch (loaded_struct.layout) {
13521353 .auto, .@"extern" => {
13531354 if (!location.isInitializer()) {
1354 try writer.writeByte('(');
1355 try dg.renderCType(writer, ctype);
1356 try writer.writeByte(')');
1355 try w.writeByte('(');
1356 try dg.renderCType(w, ctype);
1357 try w.writeByte(')');
13571358 }
13581359
1359 try writer.writeByte('{');
1360 try w.writeByte('{');
13601361 var field_it = loaded_struct.iterateRuntimeOrder(ip);
13611362 var need_comma = false;
13621363 while (field_it.next()) |field_index| {
13631364 const field_ty: Type = .fromInterned(loaded_struct.field_types.get(ip)[field_index]);
13641365 if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue;
13651366
1366 if (need_comma) try writer.writeByte(',');
1367 if (need_comma) try w.writeByte(',');
13671368 need_comma = true;
13681369 const field_val = switch (ip.indexToKey(val.toIntern()).aggregate.storage) {
13691370 .bytes => |bytes| try pt.intern(.{ .int = .{
......@@ -1373,9 +1374,9 @@ pub const DeclGen = struct {
13731374 .elems => |elems| elems[field_index],
13741375 .repeated_elem => |elem| elem,
13751376 };
1376 try dg.renderValue(writer, Value.fromInterned(field_val), initializer_type);
1377 try dg.renderValue(w, Value.fromInterned(field_val), initializer_type);
13771378 }
1378 try writer.writeByte('}');
1379 try w.writeByte('}');
13791380 },
13801381 .@"packed" => {
13811382 const int_info = ty.intInfo(zcu);
......@@ -1393,16 +1394,16 @@ pub const DeclGen = struct {
13931394 }
13941395
13951396 if (eff_num_fields == 0) {
1396 try writer.writeByte('(');
1397 try dg.renderUndefValue(writer, ty, location);
1398 try writer.writeByte(')');
1397 try w.writeByte('(');
1398 try dg.renderUndefValue(w, ty, location);
1399 try w.writeByte(')');
13991400 } else if (ty.bitSize(zcu) > 64) {
14001401 // zig_or_u128(zig_or_u128(zig_shl_u128(a, a_off), zig_shl_u128(b, b_off)), zig_shl_u128(c, c_off))
14011402 var num_or = eff_num_fields - 1;
14021403 while (num_or > 0) : (num_or -= 1) {
1403 try writer.writeAll("zig_or_");
1404 try dg.renderTypeForBuiltinFnName(writer, ty);
1405 try writer.writeByte('(');
1404 try w.writeAll("zig_or_");
1405 try dg.renderTypeForBuiltinFnName(w, ty);
1406 try w.writeByte('(');
14061407 }
14071408
14081409 var eff_index: usize = 0;
......@@ -1421,36 +1422,36 @@ pub const DeclGen = struct {
14211422 };
14221423 const cast_context = IntCastContext{ .value = .{ .value = Value.fromInterned(field_val) } };
14231424 if (bit_offset != 0) {
1424 try writer.writeAll("zig_shl_");
1425 try dg.renderTypeForBuiltinFnName(writer, ty);
1426 try writer.writeByte('(');
1427 try dg.renderIntCast(writer, ty, cast_context, field_ty, .FunctionArgument);
1428 try writer.writeAll(", ");
1429 try dg.renderValue(writer, try pt.intValue(bit_offset_ty, bit_offset), .FunctionArgument);
1430 try writer.writeByte(')');
1425 try w.writeAll("zig_shl_");
1426 try dg.renderTypeForBuiltinFnName(w, ty);
1427 try w.writeByte('(');
1428 try dg.renderIntCast(w, ty, cast_context, field_ty, .FunctionArgument);
1429 try w.writeAll(", ");
1430 try dg.renderValue(w, try pt.intValue(bit_offset_ty, bit_offset), .FunctionArgument);
1431 try w.writeByte(')');
14311432 } else {
1432 try dg.renderIntCast(writer, ty, cast_context, field_ty, .FunctionArgument);
1433 try dg.renderIntCast(w, ty, cast_context, field_ty, .FunctionArgument);
14331434 }
14341435
1435 if (needs_closing_paren) try writer.writeByte(')');
1436 if (eff_index != eff_num_fields - 1) try writer.writeAll(", ");
1436 if (needs_closing_paren) try w.writeByte(')');
1437 if (eff_index != eff_num_fields - 1) try w.writeAll(", ");
14371438
14381439 bit_offset += field_ty.bitSize(zcu);
14391440 needs_closing_paren = true;
14401441 eff_index += 1;
14411442 }
14421443 } else {
1443 try writer.writeByte('(');
1444 try w.writeByte('(');
14441445 // a << a_off | b << b_off | c << c_off
14451446 var empty = true;
14461447 for (0..loaded_struct.field_types.len) |field_index| {
14471448 const field_ty: Type = .fromInterned(loaded_struct.field_types.get(ip)[field_index]);
14481449 if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue;
14491450
1450 if (!empty) try writer.writeAll(" | ");
1451 try writer.writeByte('(');
1452 try dg.renderCType(writer, ctype);
1453 try writer.writeByte(')');
1451 if (!empty) try w.writeAll(" | ");
1452 try w.writeByte('(');
1453 try dg.renderCType(w, ctype);
1454 try w.writeByte(')');
14541455
14551456 const field_val = switch (ip.indexToKey(val.toIntern()).aggregate.storage) {
14561457 .bytes => |bytes| try pt.intern(.{ .int = .{
......@@ -1467,24 +1468,24 @@ pub const DeclGen = struct {
14671468 .{ .signedness = .unsigned, .bits = undefined };
14681469 switch (field_int_info.signedness) {
14691470 .signed => {
1470 try writer.writeByte('(');
1471 try dg.renderValue(writer, Value.fromInterned(field_val), .Other);
1472 try writer.writeAll(" & ");
1471 try w.writeByte('(');
1472 try dg.renderValue(w, Value.fromInterned(field_val), .Other);
1473 try w.writeAll(" & ");
14731474 const field_uint_ty = try pt.intType(.unsigned, field_int_info.bits);
1474 try dg.renderValue(writer, try field_uint_ty.maxIntScalar(pt, field_uint_ty), .Other);
1475 try writer.writeByte(')');
1475 try dg.renderValue(w, try field_uint_ty.maxIntScalar(pt, field_uint_ty), .Other);
1476 try w.writeByte(')');
14761477 },
1477 .unsigned => try dg.renderValue(writer, Value.fromInterned(field_val), .Other),
1478 .unsigned => try dg.renderValue(w, Value.fromInterned(field_val), .Other),
14781479 }
14791480 if (bit_offset != 0) {
1480 try writer.writeAll(" << ");
1481 try dg.renderValue(writer, try pt.intValue(bit_offset_ty, bit_offset), .FunctionArgument);
1481 try w.writeAll(" << ");
1482 try dg.renderValue(w, try pt.intValue(bit_offset_ty, bit_offset), .FunctionArgument);
14821483 }
14831484
14841485 bit_offset += field_ty.bitSize(zcu);
14851486 empty = false;
14861487 }
1487 try writer.writeByte(')');
1488 try w.writeByte(')');
14881489 }
14891490 },
14901491 }
......@@ -1498,11 +1499,11 @@ pub const DeclGen = struct {
14981499 switch (loaded_union.flagsUnordered(ip).layout) {
14991500 .@"packed" => {
15001501 if (!location.isInitializer()) {
1501 try writer.writeByte('(');
1502 try dg.renderType(writer, backing_ty);
1503 try writer.writeByte(')');
1502 try w.writeByte('(');
1503 try dg.renderType(w, backing_ty);
1504 try w.writeByte(')');
15041505 }
1505 try dg.renderValue(writer, Value.fromInterned(un.val), location);
1506 try dg.renderValue(w, Value.fromInterned(un.val), location);
15061507 },
15071508 .@"extern" => {
15081509 if (location == .StaticInitializer) {
......@@ -1510,21 +1511,21 @@ pub const DeclGen = struct {
15101511 }
15111512
15121513 const ptr_ty = try pt.singleConstPtrType(ty);
1513 try writer.writeAll("*((");
1514 try dg.renderType(writer, ptr_ty);
1515 try writer.writeAll(")(");
1516 try dg.renderType(writer, backing_ty);
1517 try writer.writeAll("){");
1518 try dg.renderValue(writer, Value.fromInterned(un.val), location);
1519 try writer.writeAll("})");
1514 try w.writeAll("*((");
1515 try dg.renderType(w, ptr_ty);
1516 try w.writeAll(")(");
1517 try dg.renderType(w, backing_ty);
1518 try w.writeAll("){");
1519 try dg.renderValue(w, Value.fromInterned(un.val), location);
1520 try w.writeAll("})");
15201521 },
15211522 else => unreachable,
15221523 }
15231524 } else {
15241525 if (!location.isInitializer()) {
1525 try writer.writeByte('(');
1526 try dg.renderCType(writer, ctype);
1527 try writer.writeByte(')');
1526 try w.writeByte('(');
1527 try dg.renderCType(w, ctype);
1528 try w.writeByte(')');
15281529 }
15291530
15301531 const field_index = zcu.unionTagFieldIndex(loaded_union, Value.fromInterned(un.tag)).?;
......@@ -1533,57 +1534,57 @@ pub const DeclGen = struct {
15331534 if (loaded_union.flagsUnordered(ip).layout == .@"packed") {
15341535 if (field_ty.hasRuntimeBits(zcu)) {
15351536 if (field_ty.isPtrAtRuntime(zcu)) {
1536 try writer.writeByte('(');
1537 try dg.renderCType(writer, ctype);
1538 try writer.writeByte(')');
1537 try w.writeByte('(');
1538 try dg.renderCType(w, ctype);
1539 try w.writeByte(')');
15391540 } else if (field_ty.zigTypeTag(zcu) == .float) {
1540 try writer.writeByte('(');
1541 try dg.renderCType(writer, ctype);
1542 try writer.writeByte(')');
1541 try w.writeByte('(');
1542 try dg.renderCType(w, ctype);
1543 try w.writeByte(')');
15431544 }
1544 try dg.renderValue(writer, Value.fromInterned(un.val), location);
1545 } else try writer.writeAll("0");
1545 try dg.renderValue(w, Value.fromInterned(un.val), location);
1546 } else try w.writeAll("0");
15461547 return;
15471548 }
15481549
15491550 const has_tag = loaded_union.hasTag(ip);
1550 if (has_tag) try writer.writeByte('{');
1551 if (has_tag) try w.writeByte('{');
15511552 const aggregate = ctype.info(ctype_pool).aggregate;
15521553 for (0..if (has_tag) aggregate.fields.len else 1) |outer_field_index| {
1553 if (outer_field_index > 0) try writer.writeByte(',');
1554 if (outer_field_index > 0) try w.writeByte(',');
15541555 switch (if (has_tag)
15551556 aggregate.fields.at(outer_field_index, ctype_pool).name.index
15561557 else
15571558 .payload) {
15581559 .tag => try dg.renderValue(
1559 writer,
1560 w,
15601561 Value.fromInterned(un.tag),
15611562 initializer_type,
15621563 ),
15631564 .payload => {
1564 try writer.writeByte('{');
1565 try w.writeByte('{');
15651566 if (field_ty.hasRuntimeBits(zcu)) {
1566 try writer.print(" .{f} = ", .{fmtIdentSolo(field_name.toSlice(ip))});
1567 try w.print(" .{f} = ", .{fmtIdentSolo(field_name.toSlice(ip))});
15671568 try dg.renderValue(
1568 writer,
1569 w,
15691570 Value.fromInterned(un.val),
15701571 initializer_type,
15711572 );
1572 try writer.writeByte(' ');
1573 try w.writeByte(' ');
15731574 } else for (0..loaded_union.field_types.len) |inner_field_index| {
15741575 const inner_field_ty: Type = .fromInterned(
15751576 loaded_union.field_types.get(ip)[inner_field_index],
15761577 );
15771578 if (!inner_field_ty.hasRuntimeBits(zcu)) continue;
1578 try dg.renderUndefValue(writer, inner_field_ty, initializer_type);
1579 try dg.renderUndefValue(w, inner_field_ty, initializer_type);
15791580 break;
15801581 }
1581 try writer.writeByte('}');
1582 try w.writeByte('}');
15821583 },
15831584 else => unreachable,
15841585 }
15851586 }
1586 if (has_tag) try writer.writeByte('}');
1587 if (has_tag) try w.writeByte('}');
15871588 }
15881589 },
15891590 }
......@@ -1591,7 +1592,7 @@ pub const DeclGen = struct {
15911592
15921593 fn renderUndefValue(
15931594 dg: *DeclGen,
1594 writer: anytype,
1595 w: *Writer,
15951596 ty: Type,
15961597 location: ValueRenderLocation,
15971598 ) error{ OutOfMemory, AnalysisFail }!void {
......@@ -1624,57 +1625,57 @@ pub const DeclGen = struct {
16241625 // All unsigned ints matching float types are pre-allocated.
16251626 const repr_ty = dg.pt.intType(.unsigned, bits) catch unreachable;
16261627
1627 try writer.writeAll("zig_make_");
1628 try dg.renderTypeForBuiltinFnName(writer, ty);
1629 try writer.writeByte('(');
1628 try w.writeAll("zig_make_");
1629 try dg.renderTypeForBuiltinFnName(w, ty);
1630 try w.writeByte('(');
16301631 switch (bits) {
1631 16 => try writer.print("{x}", .{@as(f16, @bitCast(undefPattern(i16)))}),
1632 32 => try writer.print("{x}", .{@as(f32, @bitCast(undefPattern(i32)))}),
1633 64 => try writer.print("{x}", .{@as(f64, @bitCast(undefPattern(i64)))}),
1634 80 => try writer.print("{x}", .{@as(f80, @bitCast(undefPattern(i80)))}),
1635 128 => try writer.print("{x}", .{@as(f128, @bitCast(undefPattern(i128)))}),
1632 16 => try w.print("{x}", .{@as(f16, @bitCast(undefPattern(i16)))}),
1633 32 => try w.print("{x}", .{@as(f32, @bitCast(undefPattern(i32)))}),
1634 64 => try w.print("{x}", .{@as(f64, @bitCast(undefPattern(i64)))}),
1635 80 => try w.print("{x}", .{@as(f80, @bitCast(undefPattern(i80)))}),
1636 128 => try w.print("{x}", .{@as(f128, @bitCast(undefPattern(i128)))}),
16361637 else => unreachable,
16371638 }
1638 try writer.writeAll(", ");
1639 try dg.renderUndefValue(writer, repr_ty, .FunctionArgument);
1640 return writer.writeByte(')');
1639 try w.writeAll(", ");
1640 try dg.renderUndefValue(w, repr_ty, .FunctionArgument);
1641 return w.writeByte(')');
16411642 },
1642 .bool_type => try writer.writeAll(if (safety_on) "0xaa" else "false"),
1643 .bool_type => try w.writeAll(if (safety_on) "0xaa" else "false"),
16431644 else => switch (ip.indexToKey(ty.toIntern())) {
16441645 .simple_type,
16451646 .int_type,
16461647 .enum_type,
16471648 .error_set_type,
16481649 .inferred_error_set_type,
1649 => return writer.print("{f}", .{
1650 => return w.print("{f}", .{
16501651 try dg.fmtIntLiteralHex(try pt.undefValue(ty), location),
16511652 }),
16521653 .ptr_type => |ptr_type| switch (ptr_type.flags.size) {
16531654 .one, .many, .c => {
1654 try writer.writeAll("((");
1655 try dg.renderCType(writer, ctype);
1656 return writer.print("){f})", .{
1655 try w.writeAll("((");
1656 try dg.renderCType(w, ctype);
1657 return w.print("){f})", .{
16571658 try dg.fmtIntLiteralHex(.undef_usize, .Other),
16581659 });
16591660 },
16601661 .slice => {
16611662 if (!location.isInitializer()) {
1662 try writer.writeByte('(');
1663 try dg.renderCType(writer, ctype);
1664 try writer.writeByte(')');
1663 try w.writeByte('(');
1664 try dg.renderCType(w, ctype);
1665 try w.writeByte(')');
16651666 }
16661667
1667 try writer.writeAll("{(");
1668 try w.writeAll("{(");
16681669 const ptr_ty = ty.slicePtrFieldType(zcu);
1669 try dg.renderType(writer, ptr_ty);
1670 return writer.print("){f}, {0fx}}}", .{
1670 try dg.renderType(w, ptr_ty);
1671 return w.print("){f}, {0fx}}}", .{
16711672 try dg.fmtIntLiteralHex(.undef_usize, .Other),
16721673 });
16731674 },
16741675 },
16751676 .opt_type => |child_type| switch (ctype.info(ctype_pool)) {
16761677 .basic, .pointer => try dg.renderUndefValue(
1677 writer,
1678 w,
16781679 .fromInterned(if (ctype.isBool()) .bool_type else child_type),
16791680 location,
16801681 ),
......@@ -1683,21 +1684,21 @@ pub const DeclGen = struct {
16831684 switch (aggregate.fields.at(0, ctype_pool).name.index) {
16841685 .is_null, .payload => {},
16851686 .ptr, .len => return dg.renderUndefValue(
1686 writer,
1687 w,
16871688 .fromInterned(child_type),
16881689 location,
16891690 ),
16901691 else => unreachable,
16911692 }
16921693 if (!location.isInitializer()) {
1693 try writer.writeByte('(');
1694 try dg.renderCType(writer, ctype);
1695 try writer.writeByte(')');
1694 try w.writeByte('(');
1695 try dg.renderCType(w, ctype);
1696 try w.writeByte(')');
16961697 }
1697 try writer.writeByte('{');
1698 try w.writeByte('{');
16981699 for (0..aggregate.fields.len) |field_index| {
1699 if (field_index > 0) try writer.writeByte(',');
1700 try dg.renderUndefValue(writer, .fromInterned(
1700 if (field_index > 0) try w.writeByte(',');
1701 try dg.renderUndefValue(w, .fromInterned(
17011702 switch (aggregate.fields.at(field_index, ctype_pool).name.index) {
17021703 .is_null => .bool_type,
17031704 .payload => child_type,
......@@ -1705,7 +1706,7 @@ pub const DeclGen = struct {
17051706 },
17061707 ), initializer_type);
17071708 }
1708 try writer.writeByte('}');
1709 try w.writeByte('}');
17091710 },
17101711 },
17111712 .struct_type => {
......@@ -1713,117 +1714,117 @@ pub const DeclGen = struct {
17131714 switch (loaded_struct.layout) {
17141715 .auto, .@"extern" => {
17151716 if (!location.isInitializer()) {
1716 try writer.writeByte('(');
1717 try dg.renderCType(writer, ctype);
1718 try writer.writeByte(')');
1717 try w.writeByte('(');
1718 try dg.renderCType(w, ctype);
1719 try w.writeByte(')');
17191720 }
17201721
1721 try writer.writeByte('{');
1722 try w.writeByte('{');
17221723 var field_it = loaded_struct.iterateRuntimeOrder(ip);
17231724 var need_comma = false;
17241725 while (field_it.next()) |field_index| {
17251726 const field_ty: Type = .fromInterned(loaded_struct.field_types.get(ip)[field_index]);
17261727 if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue;
17271728
1728 if (need_comma) try writer.writeByte(',');
1729 if (need_comma) try w.writeByte(',');
17291730 need_comma = true;
1730 try dg.renderUndefValue(writer, field_ty, initializer_type);
1731 try dg.renderUndefValue(w, field_ty, initializer_type);
17311732 }
1732 return writer.writeByte('}');
1733 return w.writeByte('}');
17331734 },
1734 .@"packed" => return writer.print("{f}", .{
1735 .@"packed" => return w.print("{f}", .{
17351736 try dg.fmtIntLiteralHex(try pt.undefValue(ty), .Other),
17361737 }),
17371738 }
17381739 },
17391740 .tuple_type => |tuple_info| {
17401741 if (!location.isInitializer()) {
1741 try writer.writeByte('(');
1742 try dg.renderCType(writer, ctype);
1743 try writer.writeByte(')');
1742 try w.writeByte('(');
1743 try dg.renderCType(w, ctype);
1744 try w.writeByte(')');
17441745 }
17451746
1746 try writer.writeByte('{');
1747 try w.writeByte('{');
17471748 var need_comma = false;
17481749 for (0..tuple_info.types.len) |field_index| {
17491750 if (tuple_info.values.get(ip)[field_index] != .none) continue;
17501751 const field_ty: Type = .fromInterned(tuple_info.types.get(ip)[field_index]);
17511752 if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue;
17521753
1753 if (need_comma) try writer.writeByte(',');
1754 if (need_comma) try w.writeByte(',');
17541755 need_comma = true;
1755 try dg.renderUndefValue(writer, field_ty, initializer_type);
1756 try dg.renderUndefValue(w, field_ty, initializer_type);
17561757 }
1757 return writer.writeByte('}');
1758 return w.writeByte('}');
17581759 },
17591760 .union_type => {
17601761 const loaded_union = ip.loadUnionType(ty.toIntern());
17611762 switch (loaded_union.flagsUnordered(ip).layout) {
17621763 .auto, .@"extern" => {
17631764 if (!location.isInitializer()) {
1764 try writer.writeByte('(');
1765 try dg.renderCType(writer, ctype);
1766 try writer.writeByte(')');
1765 try w.writeByte('(');
1766 try dg.renderCType(w, ctype);
1767 try w.writeByte(')');
17671768 }
17681769
17691770 const has_tag = loaded_union.hasTag(ip);
1770 if (has_tag) try writer.writeByte('{');
1771 if (has_tag) try w.writeByte('{');
17711772 const aggregate = ctype.info(ctype_pool).aggregate;
17721773 for (0..if (has_tag) aggregate.fields.len else 1) |outer_field_index| {
1773 if (outer_field_index > 0) try writer.writeByte(',');
1774 if (outer_field_index > 0) try w.writeByte(',');
17741775 switch (if (has_tag)
17751776 aggregate.fields.at(outer_field_index, ctype_pool).name.index
17761777 else
17771778 .payload) {
17781779 .tag => try dg.renderUndefValue(
1779 writer,
1780 w,
17801781 .fromInterned(loaded_union.enum_tag_ty),
17811782 initializer_type,
17821783 ),
17831784 .payload => {
1784 try writer.writeByte('{');
1785 try w.writeByte('{');
17851786 for (0..loaded_union.field_types.len) |inner_field_index| {
17861787 const inner_field_ty: Type = .fromInterned(
17871788 loaded_union.field_types.get(ip)[inner_field_index],
17881789 );
17891790 if (!inner_field_ty.hasRuntimeBits(pt.zcu)) continue;
17901791 try dg.renderUndefValue(
1791 writer,
1792 w,
17921793 inner_field_ty,
17931794 initializer_type,
17941795 );
17951796 break;
17961797 }
1797 try writer.writeByte('}');
1798 try w.writeByte('}');
17981799 },
17991800 else => unreachable,
18001801 }
18011802 }
1802 if (has_tag) try writer.writeByte('}');
1803 if (has_tag) try w.writeByte('}');
18031804 },
1804 .@"packed" => return writer.print("{f}", .{
1805 .@"packed" => return w.print("{f}", .{
18051806 try dg.fmtIntLiteralHex(try pt.undefValue(ty), .Other),
18061807 }),
18071808 }
18081809 },
18091810 .error_union_type => |error_union_type| switch (ctype.info(ctype_pool)) {
18101811 .basic => try dg.renderUndefValue(
1811 writer,
1812 w,
18121813 .fromInterned(error_union_type.error_set_type),
18131814 location,
18141815 ),
18151816 .pointer, .aligned, .array, .vector, .fwd_decl, .function => unreachable,
18161817 .aggregate => |aggregate| {
18171818 if (!location.isInitializer()) {
1818 try writer.writeByte('(');
1819 try dg.renderCType(writer, ctype);
1820 try writer.writeByte(')');
1819 try w.writeByte('(');
1820 try dg.renderCType(w, ctype);
1821 try w.writeByte(')');
18211822 }
1822 try writer.writeByte('{');
1823 try w.writeByte('{');
18231824 for (0..aggregate.fields.len) |field_index| {
1824 if (field_index > 0) try writer.writeByte(',');
1825 if (field_index > 0) try w.writeByte(',');
18251826 try dg.renderUndefValue(
1826 writer,
1827 w,
18271828 .fromInterned(
18281829 switch (aggregate.fields.at(field_index, ctype_pool).name.index) {
18291830 .@"error" => error_union_type.error_set_type,
......@@ -1834,14 +1835,14 @@ pub const DeclGen = struct {
18341835 initializer_type,
18351836 );
18361837 }
1837 try writer.writeByte('}');
1838 try w.writeByte('}');
18381839 },
18391840 },
18401841 .array_type, .vector_type => {
18411842 const ai = ty.arrayInfo(zcu);
18421843 if (ai.elem_type.eql(.u8, zcu)) {
18431844 const c_len = ty.arrayLenIncludingSentinel(zcu);
1844 var literal: StringLiteral = .init(writer, c_len);
1845 var literal: StringLiteral = .init(w, c_len);
18451846 try literal.start();
18461847 var index: u64 = 0;
18471848 while (index < c_len) : (index += 1)
......@@ -1849,19 +1850,19 @@ pub const DeclGen = struct {
18491850 return literal.end();
18501851 } else {
18511852 if (!location.isInitializer()) {
1852 try writer.writeByte('(');
1853 try dg.renderCType(writer, ctype);
1854 try writer.writeByte(')');
1853 try w.writeByte('(');
1854 try dg.renderCType(w, ctype);
1855 try w.writeByte(')');
18551856 }
18561857
1857 try writer.writeByte('{');
1858 try w.writeByte('{');
18581859 const c_len = ty.arrayLenIncludingSentinel(zcu);
18591860 var index: u64 = 0;
18601861 while (index < c_len) : (index += 1) {
1861 if (index > 0) try writer.writeAll(", ");
1862 try dg.renderUndefValue(writer, ty.childType(zcu), initializer_type);
1862 if (index > 0) try w.writeAll(", ");
1863 try dg.renderUndefValue(w, ty.childType(zcu), initializer_type);
18631864 }
1864 return writer.writeByte('}');
1865 return w.writeByte('}');
18651866 }
18661867 },
18671868 .anyframe_type,
......@@ -1894,7 +1895,7 @@ pub const DeclGen = struct {
18941895
18951896 fn renderFunctionSignature(
18961897 dg: *DeclGen,
1897 w: anytype,
1898 w: *Writer,
18981899 fn_val: Value,
18991900 fn_align: InternPool.Alignment,
19001901 kind: CType.Kind,
......@@ -2015,11 +2016,11 @@ pub const DeclGen = struct {
20152016 /// | `renderTypeAndName` | "uint8_t *name" | "uint8_t *name[10]" |
20162017 /// | `renderType` | "uint8_t *" | "uint8_t *[10]" |
20172018 ///
2018 fn renderType(dg: *DeclGen, w: anytype, t: Type) error{OutOfMemory}!void {
2019 fn renderType(dg: *DeclGen, w: *Writer, t: Type) error{OutOfMemory}!void {
20192020 try dg.renderCType(w, try dg.ctypeFromType(t, .complete));
20202021 }
20212022
2022 fn renderCType(dg: *DeclGen, w: anytype, ctype: CType) error{OutOfMemory}!void {
2023 fn renderCType(dg: *DeclGen, w: *Writer, ctype: CType) error{OutOfMemory}!void {
20232024 _ = try renderTypePrefix(dg.pass, &dg.ctype_pool, dg.pt.zcu, w, ctype, .suffix, .{});
20242025 try renderTypeSuffix(dg.pass, &dg.ctype_pool, dg.pt.zcu, w, ctype, .suffix, .{});
20252026 }
......@@ -2034,7 +2035,7 @@ pub const DeclGen = struct {
20342035 value: Value,
20352036 },
20362037
2037 pub fn writeValue(self: *const IntCastContext, dg: *DeclGen, w: anytype, location: ValueRenderLocation) !void {
2038 pub fn writeValue(self: *const IntCastContext, dg: *DeclGen, w: *Writer, location: ValueRenderLocation) !void {
20382039 switch (self.*) {
20392040 .c_value => |v| {
20402041 try v.f.writeCValue(w, v.value, location);
......@@ -2080,7 +2081,7 @@ pub const DeclGen = struct {
20802081 /// | > 64 bit integer | > 64 bit integer | zig_make_<dest_ty>(zig_hi_<src_ty>(src), zig_lo_<src_ty>(src))
20812082 fn renderIntCast(
20822083 dg: *DeclGen,
2083 w: anytype,
2084 w: *Writer,
20842085 dest_ty: Type,
20852086 context: IntCastContext,
20862087 src_ty: Type,
......@@ -2164,7 +2165,7 @@ pub const DeclGen = struct {
21642165 ///
21652166 fn renderTypeAndName(
21662167 dg: *DeclGen,
2167 w: anytype,
2168 w: *Writer,
21682169 ty: Type,
21692170 name: CValue,
21702171 qualifiers: CQualifiers,
......@@ -2185,7 +2186,7 @@ pub const DeclGen = struct {
21852186
21862187 fn renderCTypeAndName(
21872188 dg: *DeclGen,
2188 w: anytype,
2189 w: *Writer,
21892190 ctype: CType,
21902191 name: CValue,
21912192 qualifiers: CQualifiers,
......@@ -2205,7 +2206,7 @@ pub const DeclGen = struct {
22052206 try renderTypeSuffix(dg.pass, &dg.ctype_pool, zcu, w, ctype, .suffix, .{});
22062207 }
22072208
2208 fn writeName(dg: *DeclGen, w: anytype, c_value: CValue) !void {
2209 fn writeName(dg: *DeclGen, w: *Writer, c_value: CValue) !void {
22092210 switch (c_value) {
22102211 .new_local, .local => |i| try w.print("t{d}", .{i}),
22112212 .constant => |uav| try renderUavName(w, uav),
......@@ -2215,7 +2216,7 @@ pub const DeclGen = struct {
22152216 }
22162217 }
22172218
2218 fn writeCValue(dg: *DeclGen, w: anytype, c_value: CValue) !void {
2219 fn writeCValue(dg: *DeclGen, w: *Writer, c_value: CValue) !void {
22192220 switch (c_value) {
22202221 .none, .new_local, .local, .local_ref => unreachable,
22212222 .constant => |uav| try renderUavName(w, uav),
......@@ -2238,7 +2239,7 @@ pub const DeclGen = struct {
22382239 }
22392240 }
22402241
2241 fn writeCValueDeref(dg: *DeclGen, w: anytype, c_value: CValue) !void {
2242 fn writeCValueDeref(dg: *DeclGen, w: *Writer, c_value: CValue) !void {
22422243 switch (c_value) {
22432244 .none,
22442245 .new_local,
......@@ -2267,16 +2268,16 @@ pub const DeclGen = struct {
22672268
22682269 fn writeCValueMember(
22692270 dg: *DeclGen,
2270 writer: anytype,
2271 w: *Writer,
22712272 c_value: CValue,
22722273 member: CValue,
22732274 ) error{ OutOfMemory, AnalysisFail }!void {
2274 try dg.writeCValue(writer, c_value);
2275 try writer.writeByte('.');
2276 try dg.writeCValue(writer, member);
2275 try dg.writeCValue(w, c_value);
2276 try w.writeByte('.');
2277 try dg.writeCValue(w, member);
22772278 }
22782279
2279 fn writeCValueDerefMember(dg: *DeclGen, writer: anytype, c_value: CValue, member: CValue) !void {
2280 fn writeCValueDerefMember(dg: *DeclGen, w: *Writer, c_value: CValue, member: CValue) !void {
22802281 switch (c_value) {
22812282 .none,
22822283 .new_local,
......@@ -2290,15 +2291,15 @@ pub const DeclGen = struct {
22902291 .ctype_pool_string,
22912292 => unreachable,
22922293 .nav, .identifier, .payload_identifier => {
2293 try dg.writeCValue(writer, c_value);
2294 try writer.writeAll("->");
2294 try dg.writeCValue(w, c_value);
2295 try w.writeAll("->");
22952296 },
22962297 .nav_ref => {
2297 try dg.writeCValueDeref(writer, c_value);
2298 try writer.writeByte('.');
2298 try dg.writeCValueDeref(w, c_value);
2299 try w.writeByte('.');
22992300 },
23002301 }
2301 try dg.writeCValue(writer, member);
2302 try dg.writeCValue(w, member);
23022303 }
23032304
23042305 fn renderFwdDecl(
......@@ -2340,36 +2341,36 @@ pub const DeclGen = struct {
23402341 try fwd.writeAll(";\n");
23412342 }
23422343
2343 fn renderNavName(dg: *DeclGen, writer: anytype, nav_index: InternPool.Nav.Index) !void {
2344 fn renderNavName(dg: *DeclGen, w: *Writer, nav_index: InternPool.Nav.Index) !void {
23442345 const zcu = dg.pt.zcu;
23452346 const ip = &zcu.intern_pool;
23462347 const nav = ip.getNav(nav_index);
23472348 if (nav.getExtern(ip)) |@"extern"| {
2348 try writer.print("{f}", .{
2349 try w.print("{f}", .{
23492350 fmtIdentSolo(ip.getNav(@"extern".owner_nav).name.toSlice(ip)),
23502351 });
23512352 } else {
23522353 // MSVC has a limit of 4095 character token length limit, and fmtIdent can (worst case),
23532354 // expand to 3x the length of its input, but let's cut it off at a much shorter limit.
23542355 const fqn_slice = ip.getNav(nav_index).fqn.toSlice(ip);
2355 try writer.print("{}__{d}", .{
2356 try w.print("{}__{d}", .{
23562357 fmtIdentUnsolo(fqn_slice[0..@min(fqn_slice.len, 100)]),
23572358 @intFromEnum(nav_index),
23582359 });
23592360 }
23602361 }
23612362
2362 fn renderUavName(writer: anytype, uav: Value) !void {
2363 try writer.print("__anon_{d}", .{@intFromEnum(uav.toIntern())});
2363 fn renderUavName(w: *Writer, uav: Value) !void {
2364 try w.print("__anon_{d}", .{@intFromEnum(uav.toIntern())});
23642365 }
23652366
2366 fn renderTypeForBuiltinFnName(dg: *DeclGen, writer: anytype, ty: Type) !void {
2367 try dg.renderCTypeForBuiltinFnName(writer, try dg.ctypeFromType(ty, .complete));
2367 fn renderTypeForBuiltinFnName(dg: *DeclGen, w: *Writer, ty: Type) !void {
2368 try dg.renderCTypeForBuiltinFnName(w, try dg.ctypeFromType(ty, .complete));
23682369 }
23692370
2370 fn renderCTypeForBuiltinFnName(dg: *DeclGen, writer: anytype, ctype: CType) !void {
2371 fn renderCTypeForBuiltinFnName(dg: *DeclGen, w: *Writer, ctype: CType) !void {
23712372 switch (ctype.info(&dg.ctype_pool)) {
2372 else => |ctype_info| try writer.print("{c}{d}", .{
2373 else => |ctype_info| try w.print("{c}{d}", .{
23732374 if (ctype.isBool())
23742375 signAbbrev(.unsigned)
23752376 else if (ctype.isInteger())
......@@ -2382,11 +2383,11 @@ pub const DeclGen = struct {
23822383 return dg.fail("TODO: CBE: implement renderTypeForBuiltinFnName for {s} type", .{@tagName(ctype_info)}),
23832384 if (ctype.isFloat()) ctype.floatActiveBits(dg.mod) else dg.byteSize(ctype) * 8,
23842385 }),
2385 .array => try writer.writeAll("big"),
2386 .array => try w.writeAll("big"),
23862387 }
23872388 }
23882389
2389 fn renderBuiltinInfo(dg: *DeclGen, writer: anytype, ty: Type, info: BuiltinInfo) !void {
2390 fn renderBuiltinInfo(dg: *DeclGen, w: *Writer, ty: Type, info: BuiltinInfo) !void {
23902391 const ctype = try dg.ctypeFromType(ty, .complete);
23912392 const is_big = ctype.info(&dg.ctype_pool) == .array;
23922393 switch (info) {
......@@ -2401,8 +2402,8 @@ pub const DeclGen = struct {
24012402 .bits = @intCast(ty.bitSize(zcu)),
24022403 };
24032404
2404 if (is_big) try writer.print(", {}", .{int_info.signedness == .signed});
2405 try writer.print(", {f}", .{try dg.fmtIntLiteralDec(
2405 if (is_big) try w.print(", {}", .{int_info.signedness == .signed});
2406 try w.print(", {f}", .{try dg.fmtIntLiteralDec(
24062407 try pt.intValue(if (is_big) .u16 else .u8, int_info.bits),
24072408 .FunctionArgument,
24082409 )});
......@@ -2457,7 +2458,7 @@ const RenderCTypeTrailing = enum {
24572458 self: @This(),
24582459 comptime fmt: []const u8,
24592460 _: std.fmt.FormatOptions,
2460 w: anytype,
2461 w: *Writer,
24612462 ) @TypeOf(w).Error!void {
24622463 if (fmt.len != 0)
24632464 @compileError("invalid format string '" ++ fmt ++ "' for type '" ++
......@@ -2469,12 +2470,12 @@ const RenderCTypeTrailing = enum {
24692470 }
24702471 }
24712472};
2472fn renderAlignedTypeName(w: anytype, ctype: CType) !void {
2473fn renderAlignedTypeName(w: *Writer, ctype: CType) !void {
24732474 try w.print("anon__aligned_{d}", .{@intFromEnum(ctype.index)});
24742475}
24752476fn renderFwdDeclTypeName(
24762477 zcu: *Zcu,
2477 w: anytype,
2478 w: *Writer,
24782479 ctype: CType,
24792480 fwd_decl: CType.Info.FwdDecl,
24802481 attributes: []const u8,
......@@ -2493,7 +2494,7 @@ fn renderTypePrefix(
24932494 pass: DeclGen.Pass,
24942495 ctype_pool: *const CType.Pool,
24952496 zcu: *Zcu,
2496 w: anytype,
2497 w: *Writer,
24972498 ctype: CType,
24982499 parent_fix: CTypeFix,
24992500 qualifiers: CQualifiers,
......@@ -2610,7 +2611,7 @@ fn renderTypeSuffix(
26102611 pass: DeclGen.Pass,
26112612 ctype_pool: *const CType.Pool,
26122613 zcu: *Zcu,
2613 w: anytype,
2614 w: *Writer,
26142615 ctype: CType,
26152616 parent_fix: CTypeFix,
26162617 qualifiers: CQualifiers,
......@@ -2666,49 +2667,49 @@ fn renderTypeSuffix(
26662667}
26672668fn renderFields(
26682669 zcu: *Zcu,
2669 writer: anytype,
2670 w: *Writer,
26702671 ctype_pool: *const CType.Pool,
26712672 aggregate_info: CType.Info.Aggregate,
26722673 indent: usize,
26732674) !void {
2674 try writer.writeAll("{\n");
2675 try w.writeAll("{\n");
26752676 for (0..aggregate_info.fields.len) |field_index| {
26762677 const field_info = aggregate_info.fields.at(field_index, ctype_pool);
2677 try writer.writeByteNTimes(' ', indent + 1);
2678 try w.writeByteNTimes(' ', indent + 1);
26782679 switch (field_info.alignas.abiOrder()) {
26792680 .lt => {
26802681 std.debug.assert(aggregate_info.@"packed");
2681 if (field_info.alignas.@"align" != .@"1") try writer.print("zig_under_align({}) ", .{
2682 if (field_info.alignas.@"align" != .@"1") try w.print("zig_under_align({}) ", .{
26822683 field_info.alignas.toByteUnits(),
26832684 });
26842685 },
26852686 .eq => if (aggregate_info.@"packed" and field_info.alignas.@"align" != .@"1")
2686 try writer.print("zig_align({}) ", .{field_info.alignas.toByteUnits()}),
2687 try w.print("zig_align({}) ", .{field_info.alignas.toByteUnits()}),
26872688 .gt => {
26882689 std.debug.assert(field_info.alignas.@"align" != .@"1");
2689 try writer.print("zig_align({}) ", .{field_info.alignas.toByteUnits()});
2690 try w.print("zig_align({}) ", .{field_info.alignas.toByteUnits()});
26902691 },
26912692 }
26922693 const trailing = try renderTypePrefix(
26932694 .flush,
26942695 ctype_pool,
26952696 zcu,
2696 writer,
2697 w,
26972698 field_info.ctype,
26982699 .suffix,
26992700 .{},
27002701 );
2701 try writer.print("{}{f}", .{ trailing, fmtCTypePoolString(field_info.name, ctype_pool, true) });
2702 try renderTypeSuffix(.flush, ctype_pool, zcu, writer, field_info.ctype, .suffix, .{});
2703 try writer.writeAll(";\n");
2702 try w.print("{}{f}", .{ trailing, fmtCTypePoolString(field_info.name, ctype_pool, true) });
2703 try renderTypeSuffix(.flush, ctype_pool, zcu, w, field_info.ctype, .suffix, .{});
2704 try w.writeAll(";\n");
27042705 }
2705 try writer.writeByteNTimes(' ', indent);
2706 try writer.writeByte('}');
2706 try w.writeByteNTimes(' ', indent);
2707 try w.writeByte('}');
27072708}
27082709
27092710pub fn genTypeDecl(
27102711 zcu: *Zcu,
2711 writer: anytype,
2712 w: *Writer,
27122713 global_ctype_pool: *const CType.Pool,
27132714 global_ctype: CType,
27142715 pass: DeclGen.Pass,
......@@ -2721,27 +2722,27 @@ pub fn genTypeDecl(
27212722 .aligned => |aligned_info| {
27222723 if (!found_existing) {
27232724 std.debug.assert(aligned_info.alignas.abiOrder().compare(.lt));
2724 try writer.print("typedef zig_under_align({d}) ", .{aligned_info.alignas.toByteUnits()});
2725 try writer.print("{}", .{try renderTypePrefix(
2725 try w.print("typedef zig_under_align({d}) ", .{aligned_info.alignas.toByteUnits()});
2726 try w.print("{}", .{try renderTypePrefix(
27262727 .flush,
27272728 global_ctype_pool,
27282729 zcu,
2729 writer,
2730 w,
27302731 aligned_info.ctype,
27312732 .suffix,
27322733 .{},
27332734 )});
2734 try renderAlignedTypeName(writer, global_ctype);
2735 try renderTypeSuffix(.flush, global_ctype_pool, zcu, writer, aligned_info.ctype, .suffix, .{});
2736 try writer.writeAll(";\n");
2735 try renderAlignedTypeName(w, global_ctype);
2736 try renderTypeSuffix(.flush, global_ctype_pool, zcu, w, aligned_info.ctype, .suffix, .{});
2737 try w.writeAll(";\n");
27372738 }
27382739 switch (pass) {
27392740 .nav, .uav => {
2740 try writer.writeAll("typedef ");
2741 _ = try renderTypePrefix(.flush, global_ctype_pool, zcu, writer, global_ctype, .suffix, .{});
2742 try writer.writeByte(' ');
2743 _ = try renderTypePrefix(pass, decl_ctype_pool, zcu, writer, decl_ctype, .suffix, .{});
2744 try writer.writeAll(";\n");
2741 try w.writeAll("typedef ");
2742 _ = try renderTypePrefix(.flush, global_ctype_pool, zcu, w, global_ctype, .suffix, .{});
2743 try w.writeByte(' ');
2744 _ = try renderTypePrefix(pass, decl_ctype_pool, zcu, w, decl_ctype, .suffix, .{});
2745 try w.writeAll(";\n");
27452746 },
27462747 .flush => {},
27472748 }
......@@ -2749,24 +2750,24 @@ pub fn genTypeDecl(
27492750 .fwd_decl => |fwd_decl_info| switch (fwd_decl_info.name) {
27502751 .anon => switch (pass) {
27512752 .nav, .uav => {
2752 try writer.writeAll("typedef ");
2753 _ = try renderTypePrefix(.flush, global_ctype_pool, zcu, writer, global_ctype, .suffix, .{});
2754 try writer.writeByte(' ');
2755 _ = try renderTypePrefix(pass, decl_ctype_pool, zcu, writer, decl_ctype, .suffix, .{});
2756 try writer.writeAll(";\n");
2753 try w.writeAll("typedef ");
2754 _ = try renderTypePrefix(.flush, global_ctype_pool, zcu, w, global_ctype, .suffix, .{});
2755 try w.writeByte(' ');
2756 _ = try renderTypePrefix(pass, decl_ctype_pool, zcu, w, decl_ctype, .suffix, .{});
2757 try w.writeAll(";\n");
27572758 },
27582759 .flush => {},
27592760 },
27602761 .index => |index| if (!found_existing) {
27612762 const ip = &zcu.intern_pool;
27622763 const ty: Type = .fromInterned(index);
2763 _ = try renderTypePrefix(.flush, global_ctype_pool, zcu, writer, global_ctype, .suffix, .{});
2764 try writer.writeByte(';');
2764 _ = try renderTypePrefix(.flush, global_ctype_pool, zcu, w, global_ctype, .suffix, .{});
2765 try w.writeByte(';');
27652766 const file_scope = ty.typeDeclInstAllowGeneratedTag(zcu).?.resolveFile(ip);
2766 if (!zcu.fileByIndex(file_scope).mod.?.strip) try writer.print(" /* {} */", .{
2767 if (!zcu.fileByIndex(file_scope).mod.?.strip) try w.print(" /* {} */", .{
27672768 ty.containerTypeName(ip).fmt(ip),
27682769 });
2769 try writer.writeByte('\n');
2770 try w.writeByte('\n');
27702771 },
27712772 },
27722773 .aggregate => |aggregate_info| switch (aggregate_info.name) {
......@@ -2774,23 +2775,23 @@ pub fn genTypeDecl(
27742775 .fwd_decl => |fwd_decl| if (!found_existing) {
27752776 try renderFwdDeclTypeName(
27762777 zcu,
2777 writer,
2778 w,
27782779 fwd_decl,
27792780 fwd_decl.info(global_ctype_pool).fwd_decl,
27802781 if (aggregate_info.@"packed") "zig_packed(" else "",
27812782 );
2782 try writer.writeByte(' ');
2783 try renderFields(zcu, writer, global_ctype_pool, aggregate_info, 0);
2784 if (aggregate_info.@"packed") try writer.writeByte(')');
2785 try writer.writeAll(";\n");
2783 try w.writeByte(' ');
2784 try renderFields(zcu, w, global_ctype_pool, aggregate_info, 0);
2785 if (aggregate_info.@"packed") try w.writeByte(')');
2786 try w.writeAll(";\n");
27862787 },
27872788 },
27882789 }
27892790}
27902791
2791pub fn genGlobalAsm(zcu: *Zcu, writer: anytype) !void {
2792pub fn genGlobalAsm(zcu: *Zcu, w: *Writer) !void {
27922793 for (zcu.global_assembly.values()) |asm_source| {
2793 try writer.print("__asm({f});\n", .{fmtStringLiteral(asm_source, null)});
2794 try w.print("__asm({f});\n", .{fmtStringLiteral(asm_source, null)});
27942795 }
27952796}
27962797
......@@ -2798,13 +2799,13 @@ pub fn genErrDecls(o: *Object) !void {
27982799 const pt = o.dg.pt;
27992800 const zcu = pt.zcu;
28002801 const ip = &zcu.intern_pool;
2801 const writer = o.writer();
2802 const w = o.writer();
28022803
28032804 var max_name_len: usize = 0;
28042805 // do not generate an invalid empty enum when the global error set is empty
28052806 const names = ip.global_error_set.getNamesFromMainThread();
28062807 if (names.len > 0) {
2807 try writer.writeAll("enum {\n");
2808 try w.writeAll("enum {\n");
28082809 o.indent_writer.pushIndent();
28092810 for (names, 1..) |name_nts, value| {
28102811 const name = name_nts.toSlice(ip);
......@@ -2813,11 +2814,11 @@ pub fn genErrDecls(o: *Object) !void {
28132814 .ty = .anyerror_type,
28142815 .name = name_nts,
28152816 } });
2816 try o.dg.renderValue(writer, Value.fromInterned(err_val), .Other);
2817 try writer.print(" = {d}u,\n", .{value});
2817 try o.dg.renderValue(w, Value.fromInterned(err_val), .Other);
2818 try w.print(" = {d}u,\n", .{value});
28182819 }
28192820 o.indent_writer.popIndent();
2820 try writer.writeAll("};\n");
2821 try w.writeAll("};\n");
28212822 }
28222823 const array_identifier = "zig_errorName";
28232824 const name_prefix = array_identifier ++ "_";
......@@ -2840,18 +2841,18 @@ pub fn genErrDecls(o: *Object) !void {
28402841 .storage = .{ .bytes = name.toString() },
28412842 } });
28422843
2843 try writer.writeAll("static ");
2844 try w.writeAll("static ");
28442845 try o.dg.renderTypeAndName(
2845 writer,
2846 w,
28462847 name_ty,
28472848 .{ .identifier = identifier },
28482849 Const,
28492850 .none,
28502851 .complete,
28512852 );
2852 try writer.writeAll(" = ");
2853 try o.dg.renderValue(writer, Value.fromInterned(name_val), .StaticInitializer);
2854 try writer.writeAll(";\n");
2853 try w.writeAll(" = ");
2854 try o.dg.renderValue(w, Value.fromInterned(name_val), .StaticInitializer);
2855 try w.writeAll(";\n");
28552856 }
28562857
28572858 const name_array_ty = try pt.arrayType(.{
......@@ -2859,25 +2860,25 @@ pub fn genErrDecls(o: *Object) !void {
28592860 .child = .slice_const_u8_sentinel_0_type,
28602861 });
28612862
2862 try writer.writeAll("static ");
2863 try w.writeAll("static ");
28632864 try o.dg.renderTypeAndName(
2864 writer,
2865 w,
28652866 name_array_ty,
28662867 .{ .identifier = array_identifier },
28672868 Const,
28682869 .none,
28692870 .complete,
28702871 );
2871 try writer.writeAll(" = {");
2872 try w.writeAll(" = {");
28722873 for (names, 1..) |name_nts, val| {
28732874 const name = name_nts.toSlice(ip);
2874 if (val > 1) try writer.writeAll(", ");
2875 try writer.print("{{" ++ name_prefix ++ "{f}, {f}}}", .{
2875 if (val > 1) try w.writeAll(", ");
2876 try w.print("{{" ++ name_prefix ++ "{f}, {f}}}", .{
28762877 fmtIdentUnsolo(name),
28772878 try o.dg.fmtIntLiteralDec(try pt.intValue(.usize, name.len), .StaticInitializer),
28782879 });
28792880 }
2880 try writer.writeAll("};\n");
2881 try w.writeAll("};\n");
28812882}
28822883
28832884pub fn genLazyFn(o: *Object, lazy_ctype_pool: *const CType.Pool, lazy_fn: LazyFnMap.Entry) !void {
......@@ -3305,15 +3306,15 @@ pub fn genExports(dg: *DeclGen, exported: Zcu.Exported, export_indices: []const
33053306/// have been added to `free_locals_map`. For a version of this function that restores this state,
33063307/// see `genBodyResolveState`.
33073308fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutOfMemory }!void {
3308 const writer = f.object.writer();
3309 const w = f.object.writer();
33093310 if (body.len == 0) {
3310 try writer.writeAll("{}");
3311 try w.writeAll("{}");
33113312 } else {
3312 try writer.writeAll("{\n");
3313 try w.writeAll("{\n");
33133314 f.object.indent_writer.pushIndent();
33143315 try genBodyInner(f, body);
33153316 f.object.indent_writer.popIndent();
3316 try writer.writeByte('}');
3317 try w.writeByte('}');
33173318 }
33183319}
33193320
......@@ -3687,16 +3688,16 @@ fn airSliceField(f: *Function, inst: Air.Inst.Index, is_ptr: bool, field_name: [
36873688 const operand = try f.resolveInst(ty_op.operand);
36883689 try reap(f, inst, &.{ty_op.operand});
36893690
3690 const writer = f.object.writer();
3691 const w = f.object.writer();
36913692 const local = try f.allocLocal(inst, inst_ty);
3692 const a = try Assignment.start(f, writer, try f.ctypeFromType(inst_ty, .complete));
3693 try f.writeCValue(writer, local, .Other);
3694 try a.assign(f, writer);
3693 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete));
3694 try f.writeCValue(w, local, .Other);
3695 try a.assign(f, w);
36953696 if (is_ptr) {
3696 try writer.writeByte('&');
3697 try f.writeCValueDerefMember(writer, operand, .{ .identifier = field_name });
3698 } else try f.writeCValueMember(writer, operand, .{ .identifier = field_name });
3699 try a.end(f, writer);
3697 try w.writeByte('&');
3698 try f.writeCValueDerefMember(w, operand, .{ .identifier = field_name });
3699 } else try f.writeCValueMember(w, operand, .{ .identifier = field_name });
3700 try a.end(f, w);
37003701 return local;
37013702}
37023703
......@@ -3713,16 +3714,16 @@ fn airPtrElemVal(f: *Function, inst: Air.Inst.Index) !CValue {
37133714 const index = try f.resolveInst(bin_op.rhs);
37143715 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });
37153716
3716 const writer = f.object.writer();
3717 const w = f.object.writer();
37173718 const local = try f.allocLocal(inst, inst_ty);
3718 const a = try Assignment.start(f, writer, try f.ctypeFromType(inst_ty, .complete));
3719 try f.writeCValue(writer, local, .Other);
3720 try a.assign(f, writer);
3721 try f.writeCValue(writer, ptr, .Other);
3722 try writer.writeByte('[');
3723 try f.writeCValue(writer, index, .Other);
3724 try writer.writeByte(']');
3725 try a.end(f, writer);
3719 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete));
3720 try f.writeCValue(w, local, .Other);
3721 try a.assign(f, w);
3722 try f.writeCValue(w, ptr, .Other);
3723 try w.writeByte('[');
3724 try f.writeCValue(w, index, .Other);
3725 try w.writeByte(']');
3726 try a.end(f, w);
37263727 return local;
37273728}
37283729
......@@ -3740,25 +3741,25 @@ fn airPtrElemPtr(f: *Function, inst: Air.Inst.Index) !CValue {
37403741 const index = try f.resolveInst(bin_op.rhs);
37413742 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });
37423743
3743 const writer = f.object.writer();
3744 const w = f.object.writer();
37443745 const local = try f.allocLocal(inst, inst_ty);
3745 const a = try Assignment.start(f, writer, try f.ctypeFromType(inst_ty, .complete));
3746 try f.writeCValue(writer, local, .Other);
3747 try a.assign(f, writer);
3748 try writer.writeByte('(');
3749 try f.renderType(writer, inst_ty);
3750 try writer.writeByte(')');
3751 if (elem_has_bits) try writer.writeByte('&');
3746 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete));
3747 try f.writeCValue(w, local, .Other);
3748 try a.assign(f, w);
3749 try w.writeByte('(');
3750 try f.renderType(w, inst_ty);
3751 try w.writeByte(')');
3752 if (elem_has_bits) try w.writeByte('&');
37523753 if (elem_has_bits and ptr_ty.ptrSize(zcu) == .one) {
37533754 // It's a pointer to an array, so we need to de-reference.
3754 try f.writeCValueDeref(writer, ptr);
3755 } else try f.writeCValue(writer, ptr, .Other);
3755 try f.writeCValueDeref(w, ptr);
3756 } else try f.writeCValue(w, ptr, .Other);
37563757 if (elem_has_bits) {
3757 try writer.writeByte('[');
3758 try f.writeCValue(writer, index, .Other);
3759 try writer.writeByte(']');
3758 try w.writeByte('[');
3759 try f.writeCValue(w, index, .Other);
3760 try w.writeByte(']');
37603761 }
3761 try a.end(f, writer);
3762 try a.end(f, w);
37623763 return local;
37633764}
37643765
......@@ -3775,16 +3776,16 @@ fn airSliceElemVal(f: *Function, inst: Air.Inst.Index) !CValue {
37753776 const index = try f.resolveInst(bin_op.rhs);
37763777 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });
37773778
3778 const writer = f.object.writer();
3779 const w = f.object.writer();
37793780 const local = try f.allocLocal(inst, inst_ty);
3780 const a = try Assignment.start(f, writer, try f.ctypeFromType(inst_ty, .complete));
3781 try f.writeCValue(writer, local, .Other);
3782 try a.assign(f, writer);
3783 try f.writeCValueMember(writer, slice, .{ .identifier = "ptr" });
3784 try writer.writeByte('[');
3785 try f.writeCValue(writer, index, .Other);
3786 try writer.writeByte(']');
3787 try a.end(f, writer);
3781 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete));
3782 try f.writeCValue(w, local, .Other);
3783 try a.assign(f, w);
3784 try f.writeCValueMember(w, slice, .{ .identifier = "ptr" });
3785 try w.writeByte('[');
3786 try f.writeCValue(w, index, .Other);
3787 try w.writeByte(']');
3788 try a.end(f, w);
37883789 return local;
37893790}
37903791
......@@ -3803,19 +3804,19 @@ fn airSliceElemPtr(f: *Function, inst: Air.Inst.Index) !CValue {
38033804 const index = try f.resolveInst(bin_op.rhs);
38043805 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });
38053806
3806 const writer = f.object.writer();
3807 const w = f.object.writer();
38073808 const local = try f.allocLocal(inst, inst_ty);
3808 const a = try Assignment.start(f, writer, try f.ctypeFromType(inst_ty, .complete));
3809 try f.writeCValue(writer, local, .Other);
3810 try a.assign(f, writer);
3811 if (elem_has_bits) try writer.writeByte('&');
3812 try f.writeCValueMember(writer, slice, .{ .identifier = "ptr" });
3809 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete));
3810 try f.writeCValue(w, local, .Other);
3811 try a.assign(f, w);
3812 if (elem_has_bits) try w.writeByte('&');
3813 try f.writeCValueMember(w, slice, .{ .identifier = "ptr" });
38133814 if (elem_has_bits) {
3814 try writer.writeByte('[');
3815 try f.writeCValue(writer, index, .Other);
3816 try writer.writeByte(']');
3815 try w.writeByte('[');
3816 try f.writeCValue(w, index, .Other);
3817 try w.writeByte(']');
38173818 }
3818 try a.end(f, writer);
3819 try a.end(f, w);
38193820 return local;
38203821}
38213822
......@@ -3832,16 +3833,16 @@ fn airArrayElemVal(f: *Function, inst: Air.Inst.Index) !CValue {
38323833 const index = try f.resolveInst(bin_op.rhs);
38333834 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });
38343835
3835 const writer = f.object.writer();
3836 const w = f.object.writer();
38363837 const local = try f.allocLocal(inst, inst_ty);
3837 const a = try Assignment.start(f, writer, try f.ctypeFromType(inst_ty, .complete));
3838 try f.writeCValue(writer, local, .Other);
3839 try a.assign(f, writer);
3840 try f.writeCValue(writer, array, .Other);
3841 try writer.writeByte('[');
3842 try f.writeCValue(writer, index, .Other);
3843 try writer.writeByte(']');
3844 try a.end(f, writer);
3838 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete));
3839 try f.writeCValue(w, local, .Other);
3840 try a.assign(f, w);
3841 try f.writeCValue(w, array, .Other);
3842 try w.writeByte('[');
3843 try f.writeCValue(w, index, .Other);
3844 try w.writeByte(']');
3845 try a.end(f, w);
38453846 return local;
38463847}
38473848
......@@ -3895,12 +3896,12 @@ fn airArg(f: *Function, inst: Air.Inst.Index) !CValue {
38953896 .{ .arg_array = i };
38963897
38973898 if (f.liveness.isUnused(inst)) {
3898 const writer = f.object.writer();
3899 try writer.writeByte('(');
3900 try f.renderType(writer, .void);
3901 try writer.writeByte(')');
3902 try f.writeCValue(writer, result, .Other);
3903 try writer.writeAll(";\n");
3899 const w = f.object.writer();
3900 try w.writeByte('(');
3901 try f.renderType(w, .void);
3902 try w.writeByte(')');
3903 try f.writeCValue(w, result, .Other);
3904 try w.writeAll(";\n");
39043905 return .none;
39053906 }
39063907
......@@ -3933,21 +3934,21 @@ fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue {
39333934 const is_array = lowersToArray(src_ty, pt);
39343935 const need_memcpy = !is_aligned or is_array;
39353936
3936 const writer = f.object.writer();
3937 const w = f.object.writer();
39373938 const local = try f.allocLocal(inst, src_ty);
3938 const v = try Vectorize.start(f, inst, writer, ptr_ty);
3939 const v = try Vectorize.start(f, inst, w, ptr_ty);
39393940
39403941 if (need_memcpy) {
3941 try writer.writeAll("memcpy(");
3942 if (!is_array) try writer.writeByte('&');
3943 try f.writeCValue(writer, local, .Other);
3944 try v.elem(f, writer);
3945 try writer.writeAll(", (const char *)");
3946 try f.writeCValue(writer, operand, .Other);
3947 try v.elem(f, writer);
3948 try writer.writeAll(", sizeof(");
3949 try f.renderType(writer, src_ty);
3950 try writer.writeAll("))");
3942 try w.writeAll("memcpy(");
3943 if (!is_array) try w.writeByte('&');
3944 try f.writeCValue(w, local, .Other);
3945 try v.elem(f, w);
3946 try w.writeAll(", (const char *)");
3947 try f.writeCValue(w, operand, .Other);
3948 try v.elem(f, w);
3949 try w.writeAll(", sizeof(");
3950 try f.renderType(w, src_ty);
3951 try w.writeAll("))");
39513952 } else if (ptr_info.packed_offset.host_size > 0 and ptr_info.flags.vector_index == .none) {
39523953 const host_bits: u16 = ptr_info.packed_offset.host_size * 8;
39533954 const host_ty = try pt.intType(.unsigned, host_bits);
......@@ -3957,40 +3958,40 @@ fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue {
39573958
39583959 const field_ty = try pt.intType(.unsigned, @as(u16, @intCast(src_ty.bitSize(zcu))));
39593960
3960 try f.writeCValue(writer, local, .Other);
3961 try v.elem(f, writer);
3962 try writer.writeAll(" = (");
3963 try f.renderType(writer, src_ty);
3964 try writer.writeAll(")zig_wrap_");
3965 try f.object.dg.renderTypeForBuiltinFnName(writer, field_ty);
3966 try writer.writeAll("((");
3967 try f.renderType(writer, field_ty);
3968 try writer.writeByte(')');
3961 try f.writeCValue(w, local, .Other);
3962 try v.elem(f, w);
3963 try w.writeAll(" = (");
3964 try f.renderType(w, src_ty);
3965 try w.writeAll(")zig_wrap_");
3966 try f.object.dg.renderTypeForBuiltinFnName(w, field_ty);
3967 try w.writeAll("((");
3968 try f.renderType(w, field_ty);
3969 try w.writeByte(')');
39693970 const cant_cast = host_ty.isInt(zcu) and host_ty.bitSize(zcu) > 64;
39703971 if (cant_cast) {
39713972 if (field_ty.bitSize(zcu) > 64) return f.fail("TODO: C backend: implement casting between types > 64 bits", .{});
3972 try writer.writeAll("zig_lo_");
3973 try f.object.dg.renderTypeForBuiltinFnName(writer, host_ty);
3974 try writer.writeByte('(');
3973 try w.writeAll("zig_lo_");
3974 try f.object.dg.renderTypeForBuiltinFnName(w, host_ty);
3975 try w.writeByte('(');
39753976 }
3976 try writer.writeAll("zig_shr_");
3977 try f.object.dg.renderTypeForBuiltinFnName(writer, host_ty);
3978 try writer.writeByte('(');
3979 try f.writeCValueDeref(writer, operand);
3980 try v.elem(f, writer);
3981 try writer.print(", {f})", .{try f.fmtIntLiteralDec(bit_offset_val)});
3982 if (cant_cast) try writer.writeByte(')');
3983 try f.object.dg.renderBuiltinInfo(writer, field_ty, .bits);
3984 try writer.writeByte(')');
3977 try w.writeAll("zig_shr_");
3978 try f.object.dg.renderTypeForBuiltinFnName(w, host_ty);
3979 try w.writeByte('(');
3980 try f.writeCValueDeref(w, operand);
3981 try v.elem(f, w);
3982 try w.print(", {f})", .{try f.fmtIntLiteralDec(bit_offset_val)});
3983 if (cant_cast) try w.writeByte(')');
3984 try f.object.dg.renderBuiltinInfo(w, field_ty, .bits);
3985 try w.writeByte(')');
39853986 } else {
3986 try f.writeCValue(writer, local, .Other);
3987 try v.elem(f, writer);
3988 try writer.writeAll(" = ");
3989 try f.writeCValueDeref(writer, operand);
3990 try v.elem(f, writer);
3987 try f.writeCValue(w, local, .Other);
3988 try v.elem(f, w);
3989 try w.writeAll(" = ");
3990 try f.writeCValueDeref(w, operand);
3991 try v.elem(f, w);
39913992 }
3992 try writer.writeAll(";\n");
3993 try v.end(f, inst, writer);
3993 try w.writeAll(";\n");
3994 try v.end(f, inst, w);
39943995
39953996 return local;
39963997}
......@@ -3999,7 +4000,7 @@ fn airRet(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !void {
39994000 const pt = f.object.dg.pt;
40004001 const zcu = pt.zcu;
40014002 const un_op = f.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
4002 const writer = f.object.writer();
4003 const w = f.object.writer();
40034004 const op_inst = un_op.toIndex();
40044005 const op_ty = f.typeOf(un_op);
40054006 const ret_ty = if (is_ptr) op_ty.childType(zcu) else op_ty;
......@@ -4018,33 +4019,33 @@ fn airRet(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !void {
40184019 .ctype = ret_ctype,
40194020 .alignas = CType.AlignAs.fromAbiAlignment(ret_ty.abiAlignment(zcu)),
40204021 });
4021 try writer.writeAll("memcpy(");
4022 try f.writeCValueMember(writer, array_local, .{ .identifier = "array" });
4023 try writer.writeAll(", ");
4022 try w.writeAll("memcpy(");
4023 try f.writeCValueMember(w, array_local, .{ .identifier = "array" });
4024 try w.writeAll(", ");
40244025 if (deref)
4025 try f.writeCValueDeref(writer, operand)
4026 try f.writeCValueDeref(w, operand)
40264027 else
4027 try f.writeCValue(writer, operand, .FunctionArgument);
4028 try f.writeCValue(w, operand, .FunctionArgument);
40284029 deref = false;
4029 try writer.writeAll(", sizeof(");
4030 try f.renderType(writer, ret_ty);
4031 try writer.writeAll("));\n");
4030 try w.writeAll(", sizeof(");
4031 try f.renderType(w, ret_ty);
4032 try w.writeAll("));\n");
40324033 break :ret_val array_local;
40334034 } else operand;
40344035
4035 try writer.writeAll("return ");
4036 try w.writeAll("return ");
40364037 if (deref)
4037 try f.writeCValueDeref(writer, ret_val)
4038 try f.writeCValueDeref(w, ret_val)
40384039 else
4039 try f.writeCValue(writer, ret_val, .Other);
4040 try writer.writeAll(";\n");
4040 try f.writeCValue(w, ret_val, .Other);
4041 try w.writeAll(";\n");
40414042 if (is_array) {
40424043 try freeLocal(f, inst, ret_val.new_local, null);
40434044 }
40444045 } else {
40454046 try reap(f, inst, &.{un_op});
40464047 // Not even allowed to return void in a naked function.
4047 if (!f.object.dg.is_naked_fn) try writer.writeAll("return;\n");
4048 if (!f.object.dg.is_naked_fn) try w.writeAll("return;\n");
40484049 }
40494050}
40504051
......@@ -4063,16 +4064,16 @@ fn airIntCast(f: *Function, inst: Air.Inst.Index) !CValue {
40634064
40644065 if (f.object.dg.intCastIsNoop(inst_scalar_ty, scalar_ty)) return f.moveCValue(inst, inst_ty, operand);
40654066
4066 const writer = f.object.writer();
4067 const w = f.object.writer();
40674068 const local = try f.allocLocal(inst, inst_ty);
4068 const v = try Vectorize.start(f, inst, writer, operand_ty);
4069 const a = try Assignment.start(f, writer, try f.ctypeFromType(scalar_ty, .complete));
4070 try f.writeCValue(writer, local, .Other);
4071 try v.elem(f, writer);
4072 try a.assign(f, writer);
4073 try f.renderIntCast(writer, inst_scalar_ty, operand, v, scalar_ty, .Other);
4074 try a.end(f, writer);
4075 try v.end(f, inst, writer);
4069 const v = try Vectorize.start(f, inst, w, operand_ty);
4070 const a = try Assignment.start(f, w, try f.ctypeFromType(scalar_ty, .complete));
4071 try f.writeCValue(w, local, .Other);
4072 try v.elem(f, w);
4073 try a.assign(f, w);
4074 try f.renderIntCast(w, inst_scalar_ty, operand, v, scalar_ty, .Other);
4075 try a.end(f, w);
4076 try v.end(f, inst, w);
40764077 return local;
40774078}
40784079
......@@ -4099,34 +4100,34 @@ fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue {
40994100 const need_mask = dest_bits < 8 or !std.math.isPowerOfTwo(dest_bits);
41004101 if (!need_cast and !need_lo and !need_mask) return f.moveCValue(inst, inst_ty, operand);
41014102
4102 const writer = f.object.writer();
4103 const w = f.object.writer();
41034104 const local = try f.allocLocal(inst, inst_ty);
4104 const v = try Vectorize.start(f, inst, writer, operand_ty);
4105 const a = try Assignment.start(f, writer, try f.ctypeFromType(inst_scalar_ty, .complete));
4106 try f.writeCValue(writer, local, .Other);
4107 try v.elem(f, writer);
4108 try a.assign(f, writer);
4105 const v = try Vectorize.start(f, inst, w, operand_ty);
4106 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_scalar_ty, .complete));
4107 try f.writeCValue(w, local, .Other);
4108 try v.elem(f, w);
4109 try a.assign(f, w);
41094110 if (need_cast) {
4110 try writer.writeByte('(');
4111 try f.renderType(writer, inst_scalar_ty);
4112 try writer.writeByte(')');
4111 try w.writeByte('(');
4112 try f.renderType(w, inst_scalar_ty);
4113 try w.writeByte(')');
41134114 }
41144115 if (need_lo) {
4115 try writer.writeAll("zig_lo_");
4116 try f.object.dg.renderTypeForBuiltinFnName(writer, scalar_ty);
4117 try writer.writeByte('(');
4116 try w.writeAll("zig_lo_");
4117 try f.object.dg.renderTypeForBuiltinFnName(w, scalar_ty);
4118 try w.writeByte('(');
41184119 }
41194120 if (!need_mask) {
4120 try f.writeCValue(writer, operand, .Other);
4121 try v.elem(f, writer);
4121 try f.writeCValue(w, operand, .Other);
4122 try v.elem(f, w);
41224123 } else switch (dest_int_info.signedness) {
41234124 .unsigned => {
4124 try writer.writeAll("zig_and_");
4125 try f.object.dg.renderTypeForBuiltinFnName(writer, scalar_ty);
4126 try writer.writeByte('(');
4127 try f.writeCValue(writer, operand, .FunctionArgument);
4128 try v.elem(f, writer);
4129 try writer.print(", {f})", .{
4125 try w.writeAll("zig_and_");
4126 try f.object.dg.renderTypeForBuiltinFnName(w, scalar_ty);
4127 try w.writeByte('(');
4128 try f.writeCValue(w, operand, .FunctionArgument);
4129 try v.elem(f, w);
4130 try w.print(", {f})", .{
41304131 try f.fmtIntLiteralHex(try inst_scalar_ty.maxIntScalar(pt, scalar_ty)),
41314132 });
41324133 },
......@@ -4135,30 +4136,30 @@ fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue {
41354136 return f.fail("TODO: C backend: implement integer types larger than 128 bits", .{});
41364137 const shift_val = try pt.intValue(.u8, c_bits - dest_bits);
41374138
4138 try writer.writeAll("zig_shr_");
4139 try f.object.dg.renderTypeForBuiltinFnName(writer, scalar_ty);
4139 try w.writeAll("zig_shr_");
4140 try f.object.dg.renderTypeForBuiltinFnName(w, scalar_ty);
41404141 if (c_bits == 128) {
4141 try writer.print("(zig_bitCast_i{d}(", .{c_bits});
4142 try w.print("(zig_bitCast_i{d}(", .{c_bits});
41424143 } else {
4143 try writer.print("((int{d}_t)", .{c_bits});
4144 try w.print("((int{d}_t)", .{c_bits});
41444145 }
4145 try writer.print("zig_shl_u{d}(", .{c_bits});
4146 try w.print("zig_shl_u{d}(", .{c_bits});
41464147 if (c_bits == 128) {
4147 try writer.print("zig_bitCast_u{d}(", .{c_bits});
4148 try w.print("zig_bitCast_u{d}(", .{c_bits});
41484149 } else {
4149 try writer.print("(uint{d}_t)", .{c_bits});
4150 try w.print("(uint{d}_t)", .{c_bits});
41504151 }
4151 try f.writeCValue(writer, operand, .FunctionArgument);
4152 try v.elem(f, writer);
4153 if (c_bits == 128) try writer.writeByte(')');
4154 try writer.print(", {f})", .{try f.fmtIntLiteralDec(shift_val)});
4155 if (c_bits == 128) try writer.writeByte(')');
4156 try writer.print(", {f})", .{try f.fmtIntLiteralDec(shift_val)});
4152 try f.writeCValue(w, operand, .FunctionArgument);
4153 try v.elem(f, w);
4154 if (c_bits == 128) try w.writeByte(')');
4155 try w.print(", {f})", .{try f.fmtIntLiteralDec(shift_val)});
4156 if (c_bits == 128) try w.writeByte(')');
4157 try w.print(", {f})", .{try f.fmtIntLiteralDec(shift_val)});
41574158 },
41584159 }
4159 if (need_lo) try writer.writeByte(')');
4160 try a.end(f, writer);
4161 try v.end(f, inst, writer);
4160 if (need_lo) try w.writeByte(')');
4161 try a.end(f, w);
4162 try v.end(f, inst, w);
41624163 return local;
41634164}
41644165
......@@ -4180,12 +4181,12 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
41804181 if (val_is_undef) {
41814182 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });
41824183 if (safety and ptr_info.packed_offset.host_size == 0) {
4183 const writer = f.object.writer();
4184 try writer.writeAll("memset(");
4185 try f.writeCValue(writer, ptr_val, .FunctionArgument);
4186 try writer.writeAll(", 0xaa, sizeof(");
4187 try f.renderType(writer, .fromInterned(ptr_info.child));
4188 try writer.writeAll("));\n");
4184 const w = f.object.writer();
4185 try w.writeAll("memset(");
4186 try f.writeCValue(w, ptr_val, .FunctionArgument);
4187 try w.writeAll(", 0xaa, sizeof(");
4188 try f.renderType(w, .fromInterned(ptr_info.child));
4189 try w.writeAll("));\n");
41894190 }
41904191 return .none;
41914192 }
......@@ -4201,7 +4202,7 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
42014202 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });
42024203
42034204 const src_scalar_ctype = try f.ctypeFromType(src_ty.scalarType(zcu), .complete);
4204 const writer = f.object.writer();
4205 const w = f.object.writer();
42054206 if (need_memcpy) {
42064207 // For this memcpy to safely work we need the rhs to have the same
42074208 // underlying type as the lhs (i.e. they must both be arrays of the same underlying type).
......@@ -4212,28 +4213,28 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
42124213 // TODO this should be done by manually initializing elements of the dest array
42134214 const array_src = if (src_val == .constant) blk: {
42144215 const new_local = try f.allocLocal(inst, src_ty);
4215 try f.writeCValue(writer, new_local, .Other);
4216 try writer.writeAll(" = ");
4217 try f.writeCValue(writer, src_val, .Other);
4218 try writer.writeAll(";\n");
4216 try f.writeCValue(w, new_local, .Other);
4217 try w.writeAll(" = ");
4218 try f.writeCValue(w, src_val, .Other);
4219 try w.writeAll(";\n");
42194220
42204221 break :blk new_local;
42214222 } else src_val;
42224223
4223 const v = try Vectorize.start(f, inst, writer, ptr_ty);
4224 try writer.writeAll("memcpy((char *)");
4225 try f.writeCValue(writer, ptr_val, .FunctionArgument);
4226 try v.elem(f, writer);
4227 try writer.writeAll(", ");
4228 if (!is_array) try writer.writeByte('&');
4229 try f.writeCValue(writer, array_src, .FunctionArgument);
4230 try v.elem(f, writer);
4231 try writer.writeAll(", sizeof(");
4232 try f.renderType(writer, src_ty);
4233 try writer.writeAll("))");
4224 const v = try Vectorize.start(f, inst, w, ptr_ty);
4225 try w.writeAll("memcpy((char *)");
4226 try f.writeCValue(w, ptr_val, .FunctionArgument);
4227 try v.elem(f, w);
4228 try w.writeAll(", ");
4229 if (!is_array) try w.writeByte('&');
4230 try f.writeCValue(w, array_src, .FunctionArgument);
4231 try v.elem(f, w);
4232 try w.writeAll(", sizeof(");
4233 try f.renderType(w, src_ty);
4234 try w.writeAll("))");
42344235 try f.freeCValue(inst, array_src);
4235 try writer.writeAll(";\n");
4236 try v.end(f, inst, writer);
4236 try w.writeAll(";\n");
4237 try v.end(f, inst, w);
42374238 } else if (ptr_info.packed_offset.host_size > 0 and ptr_info.flags.vector_index == .none) {
42384239 const host_bits = ptr_info.packed_offset.host_size * 8;
42394240 const host_ty = try pt.intType(.unsigned, host_bits);
......@@ -4256,44 +4257,44 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
42564257
42574258 const mask_val = try pt.intValue_big(host_ty, mask.toConst());
42584259
4259 const v = try Vectorize.start(f, inst, writer, ptr_ty);
4260 const a = try Assignment.start(f, writer, src_scalar_ctype);
4261 try f.writeCValueDeref(writer, ptr_val);
4262 try v.elem(f, writer);
4263 try a.assign(f, writer);
4264 try writer.writeAll("zig_or_");
4265 try f.object.dg.renderTypeForBuiltinFnName(writer, host_ty);
4266 try writer.writeAll("(zig_and_");
4267 try f.object.dg.renderTypeForBuiltinFnName(writer, host_ty);
4268 try writer.writeByte('(');
4269 try f.writeCValueDeref(writer, ptr_val);
4270 try v.elem(f, writer);
4271 try writer.print(", {f}), zig_shl_", .{try f.fmtIntLiteralHex(mask_val)});
4272 try f.object.dg.renderTypeForBuiltinFnName(writer, host_ty);
4273 try writer.writeByte('(');
4260 const v = try Vectorize.start(f, inst, w, ptr_ty);
4261 const a = try Assignment.start(f, w, src_scalar_ctype);
4262 try f.writeCValueDeref(w, ptr_val);
4263 try v.elem(f, w);
4264 try a.assign(f, w);
4265 try w.writeAll("zig_or_");
4266 try f.object.dg.renderTypeForBuiltinFnName(w, host_ty);
4267 try w.writeAll("(zig_and_");
4268 try f.object.dg.renderTypeForBuiltinFnName(w, host_ty);
4269 try w.writeByte('(');
4270 try f.writeCValueDeref(w, ptr_val);
4271 try v.elem(f, w);
4272 try w.print(", {f}), zig_shl_", .{try f.fmtIntLiteralHex(mask_val)});
4273 try f.object.dg.renderTypeForBuiltinFnName(w, host_ty);
4274 try w.writeByte('(');
42744275 const cant_cast = host_ty.isInt(zcu) and host_ty.bitSize(zcu) > 64;
42754276 if (cant_cast) {
42764277 if (src_ty.bitSize(zcu) > 64) return f.fail("TODO: C backend: implement casting between types > 64 bits", .{});
4277 try writer.writeAll("zig_make_");
4278 try f.object.dg.renderTypeForBuiltinFnName(writer, host_ty);
4279 try writer.writeAll("(0, ");
4278 try w.writeAll("zig_make_");
4279 try f.object.dg.renderTypeForBuiltinFnName(w, host_ty);
4280 try w.writeAll("(0, ");
42804281 } else {
4281 try writer.writeByte('(');
4282 try f.renderType(writer, host_ty);
4283 try writer.writeByte(')');
4282 try w.writeByte('(');
4283 try f.renderType(w, host_ty);
4284 try w.writeByte(')');
42844285 }
42854286
42864287 if (src_ty.isPtrAtRuntime(zcu)) {
4287 try writer.writeByte('(');
4288 try f.renderType(writer, .usize);
4289 try writer.writeByte(')');
4288 try w.writeByte('(');
4289 try f.renderType(w, .usize);
4290 try w.writeByte(')');
42904291 }
4291 try f.writeCValue(writer, src_val, .Other);
4292 try v.elem(f, writer);
4293 if (cant_cast) try writer.writeByte(')');
4294 try writer.print(", {f}))", .{try f.fmtIntLiteralDec(bit_offset_val)});
4295 try a.end(f, writer);
4296 try v.end(f, inst, writer);
4292 try f.writeCValue(w, src_val, .Other);
4293 try v.elem(f, w);
4294 if (cant_cast) try w.writeByte(')');
4295 try w.print(", {f}))", .{try f.fmtIntLiteralDec(bit_offset_val)});
4296 try a.end(f, w);
4297 try v.end(f, inst, w);
42974298 } else {
42984299 switch (ptr_val) {
42994300 .local_ref => |ptr_local_index| switch (src_val) {
......@@ -4303,15 +4304,15 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
43034304 },
43044305 else => {},
43054306 }
4306 const v = try Vectorize.start(f, inst, writer, ptr_ty);
4307 const a = try Assignment.start(f, writer, src_scalar_ctype);
4308 try f.writeCValueDeref(writer, ptr_val);
4309 try v.elem(f, writer);
4310 try a.assign(f, writer);
4311 try f.writeCValue(writer, src_val, .Other);
4312 try v.elem(f, writer);
4313 try a.end(f, writer);
4314 try v.end(f, inst, writer);
4307 const v = try Vectorize.start(f, inst, w, ptr_ty);
4308 const a = try Assignment.start(f, w, src_scalar_ctype);
4309 try f.writeCValueDeref(w, ptr_val);
4310 try v.elem(f, w);
4311 try a.assign(f, w);
4312 try f.writeCValue(w, src_val, .Other);
4313 try v.elem(f, w);
4314 try a.end(f, w);
4315 try v.end(f, inst, w);
43154316 }
43164317 return .none;
43174318}
......@@ -4368,17 +4369,17 @@ fn airNot(f: *Function, inst: Air.Inst.Index) !CValue {
43684369
43694370 const inst_ty = f.typeOfIndex(inst);
43704371
4371 const writer = f.object.writer();
4372 const w = f.object.writer();
43724373 const local = try f.allocLocal(inst, inst_ty);
4373 const v = try Vectorize.start(f, inst, writer, operand_ty);
4374 try f.writeCValue(writer, local, .Other);
4375 try v.elem(f, writer);
4376 try writer.writeAll(" = ");
4377 try writer.writeByte('!');
4378 try f.writeCValue(writer, op, .Other);
4379 try v.elem(f, writer);
4380 try writer.writeAll(";\n");
4381 try v.end(f, inst, writer);
4374 const v = try Vectorize.start(f, inst, w, operand_ty);
4375 try f.writeCValue(w, local, .Other);
4376 try v.elem(f, w);
4377 try w.writeAll(" = ");
4378 try w.writeByte('!');
4379 try f.writeCValue(w, op, .Other);
4380 try v.elem(f, w);
4381 try w.writeAll(";\n");
4382 try v.end(f, inst, w);
43824383
43834384 return local;
43844385}
......@@ -4404,21 +4405,21 @@ fn airBinOp(
44044405
44054406 const inst_ty = f.typeOfIndex(inst);
44064407
4407 const writer = f.object.writer();
4408 const w = f.object.writer();
44084409 const local = try f.allocLocal(inst, inst_ty);
4409 const v = try Vectorize.start(f, inst, writer, operand_ty);
4410 try f.writeCValue(writer, local, .Other);
4411 try v.elem(f, writer);
4412 try writer.writeAll(" = ");
4413 try f.writeCValue(writer, lhs, .Other);
4414 try v.elem(f, writer);
4415 try writer.writeByte(' ');
4416 try writer.writeAll(operator);
4417 try writer.writeByte(' ');
4418 try f.writeCValue(writer, rhs, .Other);
4419 try v.elem(f, writer);
4420 try writer.writeAll(";\n");
4421 try v.end(f, inst, writer);
4410 const v = try Vectorize.start(f, inst, w, operand_ty);
4411 try f.writeCValue(w, local, .Other);
4412 try v.elem(f, w);
4413 try w.writeAll(" = ");
4414 try f.writeCValue(w, lhs, .Other);
4415 try v.elem(f, w);
4416 try w.writeByte(' ');
4417 try w.writeAll(operator);
4418 try w.writeByte(' ');
4419 try f.writeCValue(w, rhs, .Other);
4420 try v.elem(f, w);
4421 try w.writeAll(";\n");
4422 try v.end(f, inst, w);
44224423
44234424 return local;
44244425}
......@@ -4454,27 +4455,27 @@ fn airCmpOp(
44544455
44554456 const rhs_ty = f.typeOf(data.rhs);
44564457 const need_cast = lhs_ty.isSinglePointer(zcu) or rhs_ty.isSinglePointer(zcu);
4457 const writer = f.object.writer();
4458 const w = f.object.writer();
44584459 const local = try f.allocLocal(inst, inst_ty);
4459 const v = try Vectorize.start(f, inst, writer, lhs_ty);
4460 const a = try Assignment.start(f, writer, try f.ctypeFromType(scalar_ty, .complete));
4461 try f.writeCValue(writer, local, .Other);
4462 try v.elem(f, writer);
4463 try a.assign(f, writer);
4464 if (lhs != .undef and lhs.eql(rhs)) try writer.writeAll(switch (operator) {
4460 const v = try Vectorize.start(f, inst, w, lhs_ty);
4461 const a = try Assignment.start(f, w, try f.ctypeFromType(scalar_ty, .complete));
4462 try f.writeCValue(w, local, .Other);
4463 try v.elem(f, w);
4464 try a.assign(f, w);
4465 if (lhs != .undef and lhs.eql(rhs)) try w.writeAll(switch (operator) {
44654466 .lt, .neq, .gt => "false",
44664467 .lte, .eq, .gte => "true",
44674468 }) else {
4468 if (need_cast) try writer.writeAll("(void*)");
4469 try f.writeCValue(writer, lhs, .Other);
4470 try v.elem(f, writer);
4471 try writer.writeAll(compareOperatorC(operator));
4472 if (need_cast) try writer.writeAll("(void*)");
4473 try f.writeCValue(writer, rhs, .Other);
4474 try v.elem(f, writer);
4475 }
4476 try a.end(f, writer);
4477 try v.end(f, inst, writer);
4469 if (need_cast) try w.writeAll("(void*)");
4470 try f.writeCValue(w, lhs, .Other);
4471 try v.elem(f, w);
4472 try w.writeAll(compareOperatorC(operator));
4473 if (need_cast) try w.writeAll("(void*)");
4474 try f.writeCValue(w, rhs, .Other);
4475 try v.elem(f, w);
4476 }
4477 try a.end(f, w);
4478 try v.end(f, inst, w);
44784479
44794480 return local;
44804481}
......@@ -4507,41 +4508,41 @@ fn airEquality(
45074508 const rhs = try f.resolveInst(bin_op.rhs);
45084509 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });
45094510
4510 const writer = f.object.writer();
4511 const w = f.object.writer();
45114512 const local = try f.allocLocal(inst, .bool);
4512 const a = try Assignment.start(f, writer, .bool);
4513 try f.writeCValue(writer, local, .Other);
4514 try a.assign(f, writer);
4513 const a = try Assignment.start(f, w, .bool);
4514 try f.writeCValue(w, local, .Other);
4515 try a.assign(f, w);
45154516
45164517 const operand_ctype = try f.ctypeFromType(operand_ty, .complete);
4517 if (lhs != .undef and lhs.eql(rhs)) try writer.writeAll(switch (operator) {
4518 if (lhs != .undef and lhs.eql(rhs)) try w.writeAll(switch (operator) {
45184519 .lt, .lte, .gte, .gt => unreachable,
45194520 .neq => "false",
45204521 .eq => "true",
45214522 }) else switch (operand_ctype.info(ctype_pool)) {
45224523 .basic, .pointer => {
4523 try f.writeCValue(writer, lhs, .Other);
4524 try writer.writeAll(compareOperatorC(operator));
4525 try f.writeCValue(writer, rhs, .Other);
4524 try f.writeCValue(w, lhs, .Other);
4525 try w.writeAll(compareOperatorC(operator));
4526 try f.writeCValue(w, rhs, .Other);
45264527 },
45274528 .aligned, .array, .vector, .fwd_decl, .function => unreachable,
45284529 .aggregate => |aggregate| if (aggregate.fields.len == 2 and
45294530 (aggregate.fields.at(0, ctype_pool).name.index == .is_null or
45304531 aggregate.fields.at(1, ctype_pool).name.index == .is_null))
45314532 {
4532 try f.writeCValueMember(writer, lhs, .{ .identifier = "is_null" });
4533 try writer.writeAll(" || ");
4534 try f.writeCValueMember(writer, rhs, .{ .identifier = "is_null" });
4535 try writer.writeAll(" ? ");
4536 try f.writeCValueMember(writer, lhs, .{ .identifier = "is_null" });
4537 try writer.writeAll(compareOperatorC(operator));
4538 try f.writeCValueMember(writer, rhs, .{ .identifier = "is_null" });
4539 try writer.writeAll(" : ");
4540 try f.writeCValueMember(writer, lhs, .{ .identifier = "payload" });
4541 try writer.writeAll(compareOperatorC(operator));
4542 try f.writeCValueMember(writer, rhs, .{ .identifier = "payload" });
4533 try f.writeCValueMember(w, lhs, .{ .identifier = "is_null" });
4534 try w.writeAll(" || ");
4535 try f.writeCValueMember(w, rhs, .{ .identifier = "is_null" });
4536 try w.writeAll(" ? ");
4537 try f.writeCValueMember(w, lhs, .{ .identifier = "is_null" });
4538 try w.writeAll(compareOperatorC(operator));
4539 try f.writeCValueMember(w, rhs, .{ .identifier = "is_null" });
4540 try w.writeAll(" : ");
4541 try f.writeCValueMember(w, lhs, .{ .identifier = "payload" });
4542 try w.writeAll(compareOperatorC(operator));
4543 try f.writeCValueMember(w, rhs, .{ .identifier = "payload" });
45434544 } else for (0..aggregate.fields.len) |field_index| {
4544 if (field_index > 0) try writer.writeAll(switch (operator) {
4545 if (field_index > 0) try w.writeAll(switch (operator) {
45454546 .lt, .lte, .gte, .gt => unreachable,
45464547 .eq => " && ",
45474548 .neq => " || ",
......@@ -4549,12 +4550,12 @@ fn airEquality(
45494550 const field_name: CValue = .{
45504551 .ctype_pool_string = aggregate.fields.at(field_index, ctype_pool).name,
45514552 };
4552 try f.writeCValueMember(writer, lhs, field_name);
4553 try writer.writeAll(compareOperatorC(operator));
4554 try f.writeCValueMember(writer, rhs, field_name);
4553 try f.writeCValueMember(w, lhs, field_name);
4554 try w.writeAll(compareOperatorC(operator));
4555 try f.writeCValueMember(w, rhs, field_name);
45554556 },
45564557 }
4557 try a.end(f, writer);
4558 try a.end(f, w);
45584559
45594560 return local;
45604561}
......@@ -4565,12 +4566,12 @@ fn airCmpLtErrorsLen(f: *Function, inst: Air.Inst.Index) !CValue {
45654566 const operand = try f.resolveInst(un_op);
45664567 try reap(f, inst, &.{un_op});
45674568
4568 const writer = f.object.writer();
4569 const w = f.object.writer();
45694570 const local = try f.allocLocal(inst, .bool);
4570 try f.writeCValue(writer, local, .Other);
4571 try writer.writeAll(" = ");
4572 try f.writeCValue(writer, operand, .Other);
4573 try writer.print(" < sizeof({f}) / sizeof(*{0f});\n", .{fmtIdentSolo("zig_errorName")});
4571 try f.writeCValue(w, local, .Other);
4572 try w.writeAll(" = ");
4573 try f.writeCValue(w, operand, .Other);
4574 try w.print(" < sizeof({f}) / sizeof(*{0f});\n", .{fmtIdentSolo("zig_errorName")});
45744575 return local;
45754576}
45764577
......@@ -4591,30 +4592,30 @@ fn airPtrAddSub(f: *Function, inst: Air.Inst.Index, operator: u8) !CValue {
45914592 const inst_scalar_ctype = try f.ctypeFromType(inst_scalar_ty, .complete);
45924593
45934594 const local = try f.allocLocal(inst, inst_ty);
4594 const writer = f.object.writer();
4595 const v = try Vectorize.start(f, inst, writer, inst_ty);
4596 const a = try Assignment.start(f, writer, inst_scalar_ctype);
4597 try f.writeCValue(writer, local, .Other);
4598 try v.elem(f, writer);
4599 try a.assign(f, writer);
4595 const w = f.object.writer();
4596 const v = try Vectorize.start(f, inst, w, inst_ty);
4597 const a = try Assignment.start(f, w, inst_scalar_ctype);
4598 try f.writeCValue(w, local, .Other);
4599 try v.elem(f, w);
4600 try a.assign(f, w);
46004601 // We must convert to and from integer types to prevent UB if the operation
46014602 // results in a NULL pointer, or if LHS is NULL. The operation is only UB
46024603 // if the result is NULL and then dereferenced.
4603 try writer.writeByte('(');
4604 try f.renderCType(writer, inst_scalar_ctype);
4605 try writer.writeAll(")(((uintptr_t)");
4606 try f.writeCValue(writer, lhs, .Other);
4607 try v.elem(f, writer);
4608 try writer.writeAll(") ");
4609 try writer.writeByte(operator);
4610 try writer.writeAll(" (");
4611 try f.writeCValue(writer, rhs, .Other);
4612 try v.elem(f, writer);
4613 try writer.writeAll("*sizeof(");
4614 try f.renderType(writer, elem_ty);
4615 try writer.writeAll(")))");
4616 try a.end(f, writer);
4617 try v.end(f, inst, writer);
4604 try w.writeByte('(');
4605 try f.renderCType(w, inst_scalar_ctype);
4606 try w.writeAll(")(((uintptr_t)");
4607 try f.writeCValue(w, lhs, .Other);
4608 try v.elem(f, w);
4609 try w.writeAll(") ");
4610 try w.writeByte(operator);
4611 try w.writeAll(" (");
4612 try f.writeCValue(w, rhs, .Other);
4613 try v.elem(f, w);
4614 try w.writeAll("*sizeof(");
4615 try f.renderType(w, elem_ty);
4616 try w.writeAll(")))");
4617 try a.end(f, w);
4618 try v.end(f, inst, w);
46184619 return local;
46194620}
46204621
......@@ -4633,28 +4634,28 @@ fn airMinMax(f: *Function, inst: Air.Inst.Index, operator: u8, operation: []cons
46334634 const rhs = try f.resolveInst(bin_op.rhs);
46344635 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });
46354636
4636 const writer = f.object.writer();
4637 const w = f.object.writer();
46374638 const local = try f.allocLocal(inst, inst_ty);
4638 const v = try Vectorize.start(f, inst, writer, inst_ty);
4639 try f.writeCValue(writer, local, .Other);
4640 try v.elem(f, writer);
4639 const v = try Vectorize.start(f, inst, w, inst_ty);
4640 try f.writeCValue(w, local, .Other);
4641 try v.elem(f, w);
46414642 // (lhs <> rhs) ? lhs : rhs
4642 try writer.writeAll(" = (");
4643 try f.writeCValue(writer, lhs, .Other);
4644 try v.elem(f, writer);
4645 try writer.writeByte(' ');
4646 try writer.writeByte(operator);
4647 try writer.writeByte(' ');
4648 try f.writeCValue(writer, rhs, .Other);
4649 try v.elem(f, writer);
4650 try writer.writeAll(") ? ");
4651 try f.writeCValue(writer, lhs, .Other);
4652 try v.elem(f, writer);
4653 try writer.writeAll(" : ");
4654 try f.writeCValue(writer, rhs, .Other);
4655 try v.elem(f, writer);
4656 try writer.writeAll(";\n");
4657 try v.end(f, inst, writer);
4643 try w.writeAll(" = (");
4644 try f.writeCValue(w, lhs, .Other);
4645 try v.elem(f, w);
4646 try w.writeByte(' ');
4647 try w.writeByte(operator);
4648 try w.writeByte(' ');
4649 try f.writeCValue(w, rhs, .Other);
4650 try v.elem(f, w);
4651 try w.writeAll(") ? ");
4652 try f.writeCValue(w, lhs, .Other);
4653 try v.elem(f, w);
4654 try w.writeAll(" : ");
4655 try f.writeCValue(w, rhs, .Other);
4656 try v.elem(f, w);
4657 try w.writeAll(";\n");
4658 try v.end(f, inst, w);
46584659
46594660 return local;
46604661}
......@@ -4672,21 +4673,21 @@ fn airSlice(f: *Function, inst: Air.Inst.Index) !CValue {
46724673 const inst_ty = f.typeOfIndex(inst);
46734674 const ptr_ty = inst_ty.slicePtrFieldType(zcu);
46744675
4675 const writer = f.object.writer();
4676 const w = f.object.writer();
46764677 const local = try f.allocLocal(inst, inst_ty);
46774678 {
4678 const a = try Assignment.start(f, writer, try f.ctypeFromType(ptr_ty, .complete));
4679 try f.writeCValueMember(writer, local, .{ .identifier = "ptr" });
4680 try a.assign(f, writer);
4681 try f.writeCValue(writer, ptr, .Other);
4682 try a.end(f, writer);
4679 const a = try Assignment.start(f, w, try f.ctypeFromType(ptr_ty, .complete));
4680 try f.writeCValueMember(w, local, .{ .identifier = "ptr" });
4681 try a.assign(f, w);
4682 try f.writeCValue(w, ptr, .Other);
4683 try a.end(f, w);
46834684 }
46844685 {
4685 const a = try Assignment.start(f, writer, .usize);
4686 try f.writeCValueMember(writer, local, .{ .identifier = "len" });
4687 try a.assign(f, writer);
4688 try f.writeCValue(writer, len, .Other);
4689 try a.end(f, writer);
4686 const a = try Assignment.start(f, w, .usize);
4687 try f.writeCValueMember(w, local, .{ .identifier = "len" });
4688 try a.assign(f, w);
4689 try f.writeCValue(w, len, .Other);
4690 try a.end(f, w);
46904691 }
46914692 return local;
46924693}
......@@ -4703,7 +4704,7 @@ fn airCall(
47034704 if (f.object.dg.is_naked_fn) return .none;
47044705
47054706 const gpa = f.object.dg.gpa;
4706 const writer = f.object.writer();
4707 const w = f.object.writer();
47074708
47084709 const pl_op = f.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
47094710 const extra = f.air.extraData(Air.Call, pl_op.payload);
......@@ -4724,13 +4725,13 @@ fn airCall(
47244725 .ctype = arg_ctype,
47254726 .alignas = CType.AlignAs.fromAbiAlignment(arg_ty.abiAlignment(zcu)),
47264727 });
4727 try writer.writeAll("memcpy(");
4728 try f.writeCValueMember(writer, array_local, .{ .identifier = "array" });
4729 try writer.writeAll(", ");
4730 try f.writeCValue(writer, resolved_arg.*, .FunctionArgument);
4731 try writer.writeAll(", sizeof(");
4732 try f.renderCType(writer, arg_ctype);
4733 try writer.writeAll("));\n");
4728 try w.writeAll("memcpy(");
4729 try f.writeCValueMember(w, array_local, .{ .identifier = "array" });
4730 try w.writeAll(", ");
4731 try f.writeCValue(w, resolved_arg.*, .FunctionArgument);
4732 try w.writeAll(", sizeof(");
4733 try f.renderCType(w, arg_ctype);
4734 try w.writeAll("));\n");
47344735 resolved_arg.* = array_local;
47354736 }
47364737 }
......@@ -4758,22 +4759,22 @@ fn airCall(
47584759
47594760 const result_local = result: {
47604761 if (modifier == .always_tail) {
4761 try writer.writeAll("zig_always_tail return ");
4762 try w.writeAll("zig_always_tail return ");
47624763 break :result .none;
47634764 } else if (ret_ctype.index == .void) {
47644765 break :result .none;
47654766 } else if (f.liveness.isUnused(inst)) {
4766 try writer.writeByte('(');
4767 try f.renderCType(writer, .void);
4768 try writer.writeByte(')');
4767 try w.writeByte('(');
4768 try f.renderCType(w, .void);
4769 try w.writeByte(')');
47694770 break :result .none;
47704771 } else {
47714772 const local = try f.allocAlignedLocal(inst, .{
47724773 .ctype = ret_ctype,
47734774 .alignas = CType.AlignAs.fromAbiAlignment(ret_ty.abiAlignment(zcu)),
47744775 });
4775 try f.writeCValue(writer, local, .Other);
4776 try writer.writeAll(" = ");
4776 try f.writeCValue(w, local, .Other);
4777 try w.writeAll(" = ");
47774778 break :result local;
47784779 }
47794780 };
......@@ -4793,17 +4794,17 @@ fn airCall(
47934794 else => break :known,
47944795 };
47954796 if (need_cast) {
4796 try writer.writeAll("((");
4797 try f.renderType(writer, if (callee_is_ptr) callee_ty else try pt.singleConstPtrType(callee_ty));
4798 try writer.writeByte(')');
4799 if (!callee_is_ptr) try writer.writeByte('&');
4797 try w.writeAll("((");
4798 try f.renderType(w, if (callee_is_ptr) callee_ty else try pt.singleConstPtrType(callee_ty));
4799 try w.writeByte(')');
4800 if (!callee_is_ptr) try w.writeByte('&');
48004801 }
48014802 switch (modifier) {
4802 .auto, .always_tail => try f.object.dg.renderNavName(writer, fn_nav),
4803 inline .never_tail, .never_inline => |m| try writer.writeAll(try f.getLazyFnName(@unionInit(LazyFnKey, @tagName(m), fn_nav))),
4803 .auto, .always_tail => try f.object.dg.renderNavName(w, fn_nav),
4804 inline .never_tail, .never_inline => |m| try w.writeAll(try f.getLazyFnName(@unionInit(LazyFnKey, @tagName(m), fn_nav))),
48044805 else => unreachable,
48054806 }
4806 if (need_cast) try writer.writeByte(')');
4807 if (need_cast) try w.writeByte(')');
48074808 break :callee;
48084809 }
48094810 switch (modifier) {
......@@ -4813,32 +4814,32 @@ fn airCall(
48134814 else => unreachable,
48144815 }
48154816 // Fall back to function pointer call.
4816 try f.writeCValue(writer, callee, .Other);
4817 try f.writeCValue(w, callee, .Other);
48174818 }
48184819
4819 try writer.writeByte('(');
4820 try w.writeByte('(');
48204821 var need_comma = false;
48214822 for (resolved_args) |resolved_arg| {
48224823 if (resolved_arg == .none) continue;
4823 if (need_comma) try writer.writeAll(", ");
4824 if (need_comma) try w.writeAll(", ");
48244825 need_comma = true;
4825 try f.writeCValue(writer, resolved_arg, .FunctionArgument);
4826 try f.writeCValue(w, resolved_arg, .FunctionArgument);
48264827 try f.freeCValue(inst, resolved_arg);
48274828 }
4828 try writer.writeAll(");\n");
4829 try w.writeAll(");\n");
48294830
48304831 const result = result: {
48314832 if (result_local == .none or !lowersToArray(ret_ty, pt))
48324833 break :result result_local;
48334834
48344835 const array_local = try f.allocLocal(inst, ret_ty);
4835 try writer.writeAll("memcpy(");
4836 try f.writeCValue(writer, array_local, .FunctionArgument);
4837 try writer.writeAll(", ");
4838 try f.writeCValueMember(writer, result_local, .{ .identifier = "array" });
4839 try writer.writeAll(", sizeof(");
4840 try f.renderType(writer, ret_ty);
4841 try writer.writeAll("));\n");
4836 try w.writeAll("memcpy(");
4837 try f.writeCValue(w, array_local, .FunctionArgument);
4838 try w.writeAll(", ");
4839 try f.writeCValueMember(w, result_local, .{ .identifier = "array" });
4840 try w.writeAll(", sizeof(");
4841 try f.renderType(w, ret_ty);
4842 try w.writeAll("));\n");
48424843 try freeLocal(f, inst, result_local.new_local, null);
48434844 break :result array_local;
48444845 };
......@@ -4848,7 +4849,7 @@ fn airCall(
48484849
48494850fn airDbgStmt(f: *Function, inst: Air.Inst.Index) !CValue {
48504851 const dbg_stmt = f.air.instructions.items(.data)[@intFromEnum(inst)].dbg_stmt;
4851 const writer = f.object.writer();
4852 const w = f.object.writer();
48524853 // TODO re-evaluate whether to emit these or not. If we naively emit
48534854 // these directives, the output file will report bogus line numbers because
48544855 // every newline after the #line directive adds one to the line.
......@@ -4856,8 +4857,8 @@ fn airDbgStmt(f: *Function, inst: Air.Inst.Index) !CValue {
48564857 // If we wanted to go this route, we would need to go all the way and not output
48574858 // newlines until the next dbg_stmt occurs.
48584859 // Perhaps an additional compilation option is in order?
4859 //try writer.print("#line {d}\n", .{dbg_stmt.line + 1});
4860 try writer.print("/* file:{d}:{d} */\n", .{ dbg_stmt.line + 1, dbg_stmt.column + 1 });
4860 //try w.print("#line {d}\n", .{dbg_stmt.line + 1});
4861 try w.print("/* file:{d}:{d} */\n", .{ dbg_stmt.line + 1, dbg_stmt.column + 1 });
48614862 return .none;
48624863}
48634864
......@@ -4873,8 +4874,8 @@ fn airDbgInlineBlock(f: *Function, inst: Air.Inst.Index) !CValue {
48734874 const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
48744875 const extra = f.air.extraData(Air.DbgInlineBlock, ty_pl.payload);
48754876 const owner_nav = ip.getNav(zcu.funcInfo(extra.data.func).owner_nav);
4876 const writer = f.object.writer();
4877 try writer.print("/* inline:{} */\n", .{owner_nav.fqn.fmt(&zcu.intern_pool)});
4877 const w = f.object.writer();
4878 try w.print("/* inline:{} */\n", .{owner_nav.fqn.fmt(&zcu.intern_pool)});
48784879 return lowerBlock(f, inst, @ptrCast(f.air.extra.items[extra.end..][0..extra.data.body_len]));
48794880}
48804881
......@@ -4888,8 +4889,8 @@ fn airDbgVar(f: *Function, inst: Air.Inst.Index) !CValue {
48884889 if (!operand_is_undef) _ = try f.resolveInst(pl_op.operand);
48894890
48904891 try reap(f, inst, &.{pl_op.operand});
4891 const writer = f.object.writer();
4892 try writer.print("/* {s}:{s} */\n", .{ @tagName(tag), name.toSlice(f.air) });
4892 const w = f.object.writer();
4893 try w.print("/* {s}:{s} */\n", .{ @tagName(tag), name.toSlice(f.air) });
48934894 return .none;
48944895}
48954896
......@@ -4906,7 +4907,7 @@ fn lowerBlock(f: *Function, inst: Air.Inst.Index, body: []const Air.Inst.Index)
49064907
49074908 const block_id = f.next_block_index;
49084909 f.next_block_index += 1;
4909 const writer = f.object.writer();
4910 const w = f.object.writer();
49104911
49114912 const inst_ty = f.typeOfIndex(inst);
49124913 const result = if (inst_ty.hasRuntimeBitsIgnoreComptime(zcu) and !f.liveness.isUnused(inst))
......@@ -4939,7 +4940,7 @@ fn lowerBlock(f: *Function, inst: Air.Inst.Index, body: []const Air.Inst.Index)
49394940 }
49404941 } else if (!f.typeOfIndex(inst).isNoReturn(zcu)) {
49414942 // label must be followed by an expression, include an empty one.
4942 try writer.print("zig_block_{d}:;\n", .{block_id});
4943 try w.print("zig_block_{d}:;\n", .{block_id});
49434944 }
49444945
49454946 return result;
......@@ -4976,28 +4977,28 @@ fn lowerTry(
49764977 const err_union = try f.resolveInst(operand);
49774978 const inst_ty = f.typeOfIndex(inst);
49784979 const liveness_condbr = f.liveness.getCondBr(inst);
4979 const writer = f.object.writer();
4980 const w = f.object.writer();
49804981 const payload_ty = err_union_ty.errorUnionPayload(zcu);
49814982 const payload_has_bits = payload_ty.hasRuntimeBitsIgnoreComptime(zcu);
49824983
49834984 if (!err_union_ty.errorUnionSet(zcu).errorSetIsEmpty(zcu)) {
4984 try writer.writeAll("if (");
4985 try w.writeAll("if (");
49854986 if (!payload_has_bits) {
49864987 if (is_ptr)
4987 try f.writeCValueDeref(writer, err_union)
4988 try f.writeCValueDeref(w, err_union)
49884989 else
4989 try f.writeCValue(writer, err_union, .Other);
4990 try f.writeCValue(w, err_union, .Other);
49904991 } else {
49914992 // Reap the operand so that it can be reused inside genBody.
49924993 // Remember we must avoid calling reap() twice for the same operand
49934994 // in this function.
49944995 try reap(f, inst, &.{operand});
49954996 if (is_ptr)
4996 try f.writeCValueDerefMember(writer, err_union, .{ .identifier = "error" })
4997 try f.writeCValueDerefMember(w, err_union, .{ .identifier = "error" })
49974998 else
4998 try f.writeCValueMember(writer, err_union, .{ .identifier = "error" });
4999 try f.writeCValueMember(w, err_union, .{ .identifier = "error" });
49995000 }
5000 try writer.writeAll(") ");
5001 try w.writeAll(") ");
50015002
50025003 try genBodyResolveState(f, inst, liveness_condbr.else_deaths, body, false);
50035004 try f.object.indent_writer.insertNewline();
......@@ -5023,14 +5024,14 @@ fn lowerTry(
50235024 if (f.liveness.isUnused(inst)) return .none;
50245025
50255026 const local = try f.allocLocal(inst, inst_ty);
5026 const a = try Assignment.start(f, writer, try f.ctypeFromType(inst_ty, .complete));
5027 try f.writeCValue(writer, local, .Other);
5028 try a.assign(f, writer);
5027 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete));
5028 try f.writeCValue(w, local, .Other);
5029 try a.assign(f, w);
50295030 if (is_ptr) {
5030 try writer.writeByte('&');
5031 try f.writeCValueDerefMember(writer, err_union, .{ .identifier = "payload" });
5032 } else try f.writeCValueMember(writer, err_union, .{ .identifier = "payload" });
5033 try a.end(f, writer);
5031 try w.writeByte('&');
5032 try f.writeCValueDerefMember(w, err_union, .{ .identifier = "payload" });
5033 } else try f.writeCValueMember(w, err_union, .{ .identifier = "payload" });
5034 try a.end(f, w);
50345035 return local;
50355036}
50365037
......@@ -5038,7 +5039,7 @@ fn airBr(f: *Function, inst: Air.Inst.Index) !void {
50385039 const branch = f.air.instructions.items(.data)[@intFromEnum(inst)].br;
50395040 const block = f.blocks.get(branch.block_inst).?;
50405041 const result = block.result;
5041 const writer = f.object.writer();
5042 const w = f.object.writer();
50425043
50435044 if (f.object.dg.is_naked_fn) {
50445045 if (result != .none) return f.fail("runtime code not allowed in naked function", .{});
......@@ -5052,27 +5053,27 @@ fn airBr(f: *Function, inst: Air.Inst.Index) !void {
50525053 const operand = try f.resolveInst(branch.operand);
50535054 try reap(f, inst, &.{branch.operand});
50545055
5055 const a = try Assignment.start(f, writer, try f.ctypeFromType(operand_ty, .complete));
5056 try f.writeCValue(writer, result, .Other);
5057 try a.assign(f, writer);
5058 try f.writeCValue(writer, operand, .Other);
5059 try a.end(f, writer);
5056 const a = try Assignment.start(f, w, try f.ctypeFromType(operand_ty, .complete));
5057 try f.writeCValue(w, result, .Other);
5058 try a.assign(f, w);
5059 try f.writeCValue(w, operand, .Other);
5060 try a.end(f, w);
50605061 }
50615062
5062 try writer.print("goto zig_block_{d};\n", .{block.block_id});
5063 try w.print("goto zig_block_{d};\n", .{block.block_id});
50635064}
50645065
50655066fn airRepeat(f: *Function, inst: Air.Inst.Index) !void {
50665067 const repeat = f.air.instructions.items(.data)[@intFromEnum(inst)].repeat;
5067 const writer = f.object.writer();
5068 try writer.print("goto zig_loop_{d};\n", .{@intFromEnum(repeat.loop_inst)});
5068 const w = f.object.writer();
5069 try w.print("goto zig_loop_{d};\n", .{@intFromEnum(repeat.loop_inst)});
50695070}
50705071
50715072fn airSwitchDispatch(f: *Function, inst: Air.Inst.Index) !void {
50725073 const pt = f.object.dg.pt;
50735074 const zcu = pt.zcu;
50745075 const br = f.air.instructions.items(.data)[@intFromEnum(inst)].br;
5075 const writer = f.object.writer();
5076 const w = f.object.writer();
50765077
50775078 if (try f.air.value(br.operand, pt)) |cond_val| {
50785079 // Comptime-known dispatch. Iterate the cases to find the correct
......@@ -5094,18 +5095,18 @@ fn airSwitchDispatch(f: *Function, inst: Air.Inst.Index) !void {
50945095 }
50955096 }
50965097 } else switch_br.cases_len;
5097 try writer.print("goto zig_switch_{d}_dispatch_{d};\n", .{ @intFromEnum(br.block_inst), target_case_idx });
5098 try w.print("goto zig_switch_{d}_dispatch_{d};\n", .{ @intFromEnum(br.block_inst), target_case_idx });
50985099 return;
50995100 }
51005101
51015102 // Runtime-known dispatch. Set the switch condition, and branch back.
51025103 const cond = try f.resolveInst(br.operand);
51035104 const cond_local = f.loop_switch_conds.get(br.block_inst).?;
5104 try f.writeCValue(writer, .{ .local = cond_local }, .Other);
5105 try writer.writeAll(" = ");
5106 try f.writeCValue(writer, cond, .Other);
5107 try writer.writeAll(";\n");
5108 try writer.print("goto zig_switch_{d}_loop;", .{@intFromEnum(br.block_inst)});
5105 try f.writeCValue(w, .{ .local = cond_local }, .Other);
5106 try w.writeAll(" = ");
5107 try f.writeCValue(w, cond, .Other);
5108 try w.writeAll(";\n");
5109 try w.print("goto zig_switch_{d}_loop;", .{@intFromEnum(br.block_inst)});
51095110}
51105111
51115112fn airBitcast(f: *Function, inst: Air.Inst.Index) !CValue {
......@@ -5125,7 +5126,7 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !CVal
51255126 const zcu = pt.zcu;
51265127 const target = &f.object.dg.mod.resolved_target.result;
51275128 const ctype_pool = &f.object.dg.ctype_pool;
5128 const writer = f.object.writer();
5129 const w = f.object.writer();
51295130
51305131 if (operand_ty.isAbiInt(zcu) and dest_ty.isAbiInt(zcu)) {
51315132 const src_info = dest_ty.intInfo(zcu);
......@@ -5136,35 +5137,35 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !CVal
51365137
51375138 if (dest_ty.isPtrAtRuntime(zcu) or operand_ty.isPtrAtRuntime(zcu)) {
51385139 const local = try f.allocLocal(null, dest_ty);
5139 try f.writeCValue(writer, local, .Other);
5140 try writer.writeAll(" = (");
5141 try f.renderType(writer, dest_ty);
5142 try writer.writeByte(')');
5143 try f.writeCValue(writer, operand, .Other);
5144 try writer.writeAll(";\n");
5140 try f.writeCValue(w, local, .Other);
5141 try w.writeAll(" = (");
5142 try f.renderType(w, dest_ty);
5143 try w.writeByte(')');
5144 try f.writeCValue(w, operand, .Other);
5145 try w.writeAll(";\n");
51455146 return local;
51465147 }
51475148
51485149 const operand_lval = if (operand == .constant) blk: {
51495150 const operand_local = try f.allocLocal(null, operand_ty);
5150 try f.writeCValue(writer, operand_local, .Other);
5151 try writer.writeAll(" = ");
5152 try f.writeCValue(writer, operand, .Other);
5153 try writer.writeAll(";\n");
5151 try f.writeCValue(w, operand_local, .Other);
5152 try w.writeAll(" = ");
5153 try f.writeCValue(w, operand, .Other);
5154 try w.writeAll(";\n");
51545155 break :blk operand_local;
51555156 } else operand;
51565157
51575158 const local = try f.allocLocal(null, dest_ty);
5158 try writer.writeAll("memcpy(&");
5159 try f.writeCValue(writer, local, .Other);
5160 try writer.writeAll(", &");
5161 try f.writeCValue(writer, operand_lval, .Other);
5162 try writer.writeAll(", sizeof(");
5159 try w.writeAll("memcpy(&");
5160 try f.writeCValue(w, local, .Other);
5161 try w.writeAll(", &");
5162 try f.writeCValue(w, operand_lval, .Other);
5163 try w.writeAll(", sizeof(");
51635164 try f.renderType(
5164 writer,
5165 w,
51655166 if (dest_ty.abiSize(zcu) <= operand_ty.abiSize(zcu)) dest_ty else operand_ty,
51665167 );
5167 try writer.writeAll("));\n");
5168 try w.writeAll("));\n");
51685169
51695170 // Ensure padding bits have the expected value.
51705171 if (dest_ty.isAbiInt(zcu)) {
......@@ -5174,11 +5175,11 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !CVal
51745175 var wrap_ctype: ?CType = null;
51755176 var need_bitcasts = false;
51765177
5177 try f.writeCValue(writer, local, .Other);
5178 try f.writeCValue(w, local, .Other);
51785179 switch (dest_ctype.info(ctype_pool)) {
51795180 else => {},
51805181 .array => |array_info| {
5181 try writer.print("[{d}]", .{switch (target.cpu.arch.endian()) {
5182 try w.print("[{d}]", .{switch (target.cpu.arch.endian()) {
51825183 .little => array_info.len - 1,
51835184 .big => 0,
51845185 }});
......@@ -5189,72 +5190,72 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !CVal
51895190 bits += 1;
51905191 },
51915192 }
5192 try writer.writeAll(" = ");
5193 try w.writeAll(" = ");
51935194 if (need_bitcasts) {
5194 try writer.writeAll("zig_bitCast_");
5195 try f.object.dg.renderCTypeForBuiltinFnName(writer, wrap_ctype.?.toUnsigned());
5196 try writer.writeByte('(');
5195 try w.writeAll("zig_bitCast_");
5196 try f.object.dg.renderCTypeForBuiltinFnName(w, wrap_ctype.?.toUnsigned());
5197 try w.writeByte('(');
51975198 }
5198 try writer.writeAll("zig_wrap_");
5199 try w.writeAll("zig_wrap_");
51995200 const info_ty = try pt.intType(dest_info.signedness, bits);
52005201 if (wrap_ctype) |ctype|
5201 try f.object.dg.renderCTypeForBuiltinFnName(writer, ctype)
5202 try f.object.dg.renderCTypeForBuiltinFnName(w, ctype)
52025203 else
5203 try f.object.dg.renderTypeForBuiltinFnName(writer, info_ty);
5204 try writer.writeByte('(');
5204 try f.object.dg.renderTypeForBuiltinFnName(w, info_ty);
5205 try w.writeByte('(');
52055206 if (need_bitcasts) {
5206 try writer.writeAll("zig_bitCast_");
5207 try f.object.dg.renderCTypeForBuiltinFnName(writer, wrap_ctype.?);
5208 try writer.writeByte('(');
5207 try w.writeAll("zig_bitCast_");
5208 try f.object.dg.renderCTypeForBuiltinFnName(w, wrap_ctype.?);
5209 try w.writeByte('(');
52095210 }
5210 try f.writeCValue(writer, local, .Other);
5211 try f.writeCValue(w, local, .Other);
52115212 switch (dest_ctype.info(ctype_pool)) {
52125213 else => {},
5213 .array => |array_info| try writer.print("[{d}]", .{
5214 .array => |array_info| try w.print("[{d}]", .{
52145215 switch (target.cpu.arch.endian()) {
52155216 .little => array_info.len - 1,
52165217 .big => 0,
52175218 },
52185219 }),
52195220 }
5220 if (need_bitcasts) try writer.writeByte(')');
5221 try f.object.dg.renderBuiltinInfo(writer, info_ty, .bits);
5222 if (need_bitcasts) try writer.writeByte(')');
5223 try writer.writeAll(");\n");
5221 if (need_bitcasts) try w.writeByte(')');
5222 try f.object.dg.renderBuiltinInfo(w, info_ty, .bits);
5223 if (need_bitcasts) try w.writeByte(')');
5224 try w.writeAll(");\n");
52245225 }
52255226
52265227 try f.freeCValue(null, operand_lval);
52275228 return local;
52285229}
52295230
5230fn airTrap(f: *Function, writer: anytype) !void {
5231fn airTrap(f: *Function, w: *Writer) !void {
52315232 // Not even allowed to call trap in a naked function.
52325233 if (f.object.dg.is_naked_fn) return;
5233 try writer.writeAll("zig_trap();\n");
5234 try w.writeAll("zig_trap();\n");
52345235}
52355236
5236fn airBreakpoint(writer: anytype) !CValue {
5237 try writer.writeAll("zig_breakpoint();\n");
5237fn airBreakpoint(w: *Writer) !CValue {
5238 try w.writeAll("zig_breakpoint();\n");
52385239 return .none;
52395240}
52405241
52415242fn airRetAddr(f: *Function, inst: Air.Inst.Index) !CValue {
5242 const writer = f.object.writer();
5243 const w = f.object.writer();
52435244 const local = try f.allocLocal(inst, .usize);
5244 try f.writeCValue(writer, local, .Other);
5245 try writer.writeAll(" = (");
5246 try f.renderType(writer, .usize);
5247 try writer.writeAll(")zig_return_address();\n");
5245 try f.writeCValue(w, local, .Other);
5246 try w.writeAll(" = (");
5247 try f.renderType(w, .usize);
5248 try w.writeAll(")zig_return_address();\n");
52485249 return local;
52495250}
52505251
52515252fn airFrameAddress(f: *Function, inst: Air.Inst.Index) !CValue {
5252 const writer = f.object.writer();
5253 const w = f.object.writer();
52535254 const local = try f.allocLocal(inst, .usize);
5254 try f.writeCValue(writer, local, .Other);
5255 try writer.writeAll(" = (");
5256 try f.renderType(writer, .usize);
5257 try writer.writeAll(")zig_frame_address();\n");
5255 try f.writeCValue(w, local, .Other);
5256 try w.writeAll(" = (");
5257 try f.renderType(w, .usize);
5258 try w.writeAll(")zig_frame_address();\n");
52585259 return local;
52595260}
52605261
......@@ -5268,13 +5269,13 @@ fn airLoop(f: *Function, inst: Air.Inst.Index) !void {
52685269 const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
52695270 const loop = f.air.extraData(Air.Block, ty_pl.payload);
52705271 const body: []const Air.Inst.Index = @ptrCast(f.air.extra.items[loop.end..][0..loop.data.body_len]);
5271 const writer = f.object.writer();
5272 const w = f.object.writer();
52725273
52735274 // `repeat` instructions matching this loop will branch to
52745275 // this label. Since we need a label for arbitrary `repeat`
52755276 // anyway, there's actually no need to use a "real" looping
52765277 // construct at all!
5277 try writer.print("zig_loop_{d}:\n", .{@intFromEnum(inst)});
5278 try w.print("zig_loop_{d}:\n", .{@intFromEnum(inst)});
52785279 try genBodyInner(f, body); // no need to restore state, we're noreturn
52795280}
52805281
......@@ -5286,14 +5287,14 @@ fn airCondBr(f: *Function, inst: Air.Inst.Index) !void {
52865287 const then_body: []const Air.Inst.Index = @ptrCast(f.air.extra.items[extra.end..][0..extra.data.then_body_len]);
52875288 const else_body: []const Air.Inst.Index = @ptrCast(f.air.extra.items[extra.end + then_body.len ..][0..extra.data.else_body_len]);
52885289 const liveness_condbr = f.liveness.getCondBr(inst);
5289 const writer = f.object.writer();
5290 const w = f.object.writer();
52905291
5291 try writer.writeAll("if (");
5292 try f.writeCValue(writer, cond, .Other);
5293 try writer.writeAll(") ");
5292 try w.writeAll("if (");
5293 try f.writeCValue(w, cond, .Other);
5294 try w.writeAll(") ");
52945295
52955296 try genBodyResolveState(f, inst, liveness_condbr.then_deaths, then_body, false);
5296 try writer.writeByte('\n');
5297 try w.writeByte('\n');
52975298 if (else_body.len > 0) if (f.object.dg.expected_block) |_|
52985299 return f.fail("runtime code not allowed in naked function", .{});
52995300
......@@ -5319,7 +5320,7 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void
53195320 const init_condition = try f.resolveInst(switch_br.operand);
53205321 try reap(f, inst, &.{switch_br.operand});
53215322 const condition_ty = f.typeOf(switch_br.operand);
5322 const writer = f.object.writer();
5323 const w = f.object.writer();
53235324
53245325 // For dispatches, we will create a local alloc to contain the condition value.
53255326 // This may not result in optimal codegen for switch loops, but it minimizes the
......@@ -5327,7 +5328,7 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void
53275328 const condition = if (is_dispatch_loop) cond: {
53285329 const new_local = try f.allocLocal(inst, condition_ty);
53295330 try f.copyCValue(try f.ctypeFromType(condition_ty, .complete), new_local, init_condition);
5330 try writer.print("zig_switch_{d}_loop:\n", .{@intFromEnum(inst)});
5331 try w.print("zig_switch_{d}_loop:\n", .{@intFromEnum(inst)});
53315332 try f.loop_switch_conds.put(gpa, inst, new_local.new_local);
53325333 break :cond new_local;
53335334 } else init_condition;
......@@ -5336,7 +5337,7 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void
53365337 assert(f.loop_switch_conds.remove(inst));
53375338 };
53385339
5339 try writer.writeAll("switch (");
5340 try w.writeAll("switch (");
53405341
53415342 const lowered_condition_ty: Type = if (condition_ty.toIntern() == .bool_type)
53425343 .u1
......@@ -5345,12 +5346,12 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void
53455346 else
53465347 condition_ty;
53475348 if (condition_ty.toIntern() != lowered_condition_ty.toIntern()) {
5348 try writer.writeByte('(');
5349 try f.renderType(writer, lowered_condition_ty);
5350 try writer.writeByte(')');
5349 try w.writeByte('(');
5350 try f.renderType(w, lowered_condition_ty);
5351 try w.writeByte(')');
53515352 }
5352 try f.writeCValue(writer, condition, .Other);
5353 try writer.writeAll(") {");
5353 try f.writeCValue(w, condition, .Other);
5354 try w.writeAll(") {");
53545355 f.object.indent_writer.pushIndent();
53555356
53565357 const liveness = try f.liveness.getSwitchBr(gpa, inst, switch_br.cases_len + 1);
......@@ -5365,34 +5366,34 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void
53655366 }
53665367 for (case.items) |item| {
53675368 try f.object.indent_writer.insertNewline();
5368 try writer.writeAll("case ");
5369 try w.writeAll("case ");
53695370 const item_value = try f.air.value(item, pt);
53705371 // If `item_value` is a pointer with a known integer address, print the address
53715372 // with no cast to avoid a warning.
53725373 write_val: {
53735374 if (condition_ty.isPtrAtRuntime(zcu)) {
53745375 if (item_value.?.getUnsignedInt(zcu)) |item_int| {
5375 try writer.print("{f}", .{try f.fmtIntLiteralDec(try pt.intValue(lowered_condition_ty, item_int))});
5376 try w.print("{f}", .{try f.fmtIntLiteralDec(try pt.intValue(lowered_condition_ty, item_int))});
53765377 break :write_val;
53775378 }
53785379 }
53795380 if (condition_ty.isPtrAtRuntime(zcu)) {
5380 try writer.writeByte('(');
5381 try f.renderType(writer, .usize);
5382 try writer.writeByte(')');
5381 try w.writeByte('(');
5382 try f.renderType(w, .usize);
5383 try w.writeByte(')');
53835384 }
5384 try f.object.dg.renderValue(writer, (try f.air.value(item, pt)).?, .Other);
5385 try f.object.dg.renderValue(w, (try f.air.value(item, pt)).?, .Other);
53855386 }
5386 try writer.writeByte(':');
5387 try w.writeByte(':');
53875388 }
5388 try writer.writeAll(" {\n");
5389 try w.writeAll(" {\n");
53895390 f.object.indent_writer.pushIndent();
53905391 if (is_dispatch_loop) {
5391 try writer.print("zig_switch_{d}_dispatch_{d}: ", .{ @intFromEnum(inst), case.idx });
5392 try w.print("zig_switch_{d}_dispatch_{d}: ", .{ @intFromEnum(inst), case.idx });
53925393 }
53935394 try genBodyResolveState(f, inst, liveness.deaths[case.idx], case.body, true);
53945395 f.object.indent_writer.popIndent();
5395 try writer.writeByte('}');
5396 try w.writeByte('}');
53965397 if (f.object.dg.expected_block) |_|
53975398 return f.fail("runtime code not allowed in naked function", .{});
53985399
......@@ -5402,7 +5403,7 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void
54025403 const else_body = it.elseBody();
54035404 try f.object.indent_writer.insertNewline();
54045405
5405 try writer.writeAll("default: ");
5406 try w.writeAll("default: ");
54065407 if (any_range_cases) {
54075408 // We will iterate the cases again to handle those with ranges, and generate
54085409 // code using conditions rather than switch cases for such cases.
......@@ -5410,40 +5411,40 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void
54105411 while (it.next()) |case| {
54115412 if (case.ranges.len == 0) continue; // handled above
54125413
5413 try writer.writeAll("if (");
5414 try w.writeAll("if (");
54145415 for (case.items, 0..) |item, item_i| {
5415 if (item_i != 0) try writer.writeAll(" || ");
5416 try f.writeCValue(writer, condition, .Other);
5417 try writer.writeAll(" == ");
5418 try f.object.dg.renderValue(writer, (try f.air.value(item, pt)).?, .Other);
5416 if (item_i != 0) try w.writeAll(" || ");
5417 try f.writeCValue(w, condition, .Other);
5418 try w.writeAll(" == ");
5419 try f.object.dg.renderValue(w, (try f.air.value(item, pt)).?, .Other);
54195420 }
54205421 for (case.ranges, 0..) |range, range_i| {
5421 if (case.items.len != 0 or range_i != 0) try writer.writeAll(" || ");
5422 if (case.items.len != 0 or range_i != 0) try w.writeAll(" || ");
54225423 // "(x >= lower && x <= upper)"
5423 try writer.writeByte('(');
5424 try f.writeCValue(writer, condition, .Other);
5425 try writer.writeAll(" >= ");
5426 try f.object.dg.renderValue(writer, (try f.air.value(range[0], pt)).?, .Other);
5427 try writer.writeAll(" && ");
5428 try f.writeCValue(writer, condition, .Other);
5429 try writer.writeAll(" <= ");
5430 try f.object.dg.renderValue(writer, (try f.air.value(range[1], pt)).?, .Other);
5431 try writer.writeByte(')');
5424 try w.writeByte('(');
5425 try f.writeCValue(w, condition, .Other);
5426 try w.writeAll(" >= ");
5427 try f.object.dg.renderValue(w, (try f.air.value(range[0], pt)).?, .Other);
5428 try w.writeAll(" && ");
5429 try f.writeCValue(w, condition, .Other);
5430 try w.writeAll(" <= ");
5431 try f.object.dg.renderValue(w, (try f.air.value(range[1], pt)).?, .Other);
5432 try w.writeByte(')');
54325433 }
5433 try writer.writeAll(") {\n");
5434 try w.writeAll(") {\n");
54345435 f.object.indent_writer.pushIndent();
54355436 if (is_dispatch_loop) {
5436 try writer.print("zig_switch_{d}_dispatch_{d}: ", .{ @intFromEnum(inst), case.idx });
5437 try w.print("zig_switch_{d}_dispatch_{d}: ", .{ @intFromEnum(inst), case.idx });
54375438 }
54385439 try genBodyResolveState(f, inst, liveness.deaths[case.idx], case.body, true);
54395440 f.object.indent_writer.popIndent();
5440 try writer.writeByte('}');
5441 try w.writeByte('}');
54415442 if (f.object.dg.expected_block) |_|
54425443 return f.fail("runtime code not allowed in naked function", .{});
54435444 }
54445445 }
54455446 if (is_dispatch_loop) {
5446 try writer.print("zig_switch_{d}_dispatch_{d}: ", .{ @intFromEnum(inst), switch_br.cases_len });
5447 try w.print("zig_switch_{d}_dispatch_{d}: ", .{ @intFromEnum(inst), switch_br.cases_len });
54475448 }
54485449 if (else_body.len > 0) {
54495450 // Note that this must be the last case, so we do not need to use `genBodyResolveState` since
......@@ -5455,12 +5456,12 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void
54555456 if (f.object.dg.expected_block) |_|
54565457 return f.fail("runtime code not allowed in naked function", .{});
54575458 } else {
5458 try writer.writeAll("zig_unreachable();");
5459 try w.writeAll("zig_unreachable();");
54595460 }
54605461 try f.object.indent_writer.insertNewline();
54615462
54625463 f.object.indent_writer.popIndent();
5463 try writer.writeAll("}\n");
5464 try w.writeAll("}\n");
54645465}
54655466
54665467fn asmInputNeedsLocal(f: *Function, constraint: []const u8, value: CValue) bool {
......@@ -5498,7 +5499,7 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
54985499 extra_i += inputs.len;
54995500
55005501 const result = result: {
5501 const writer = f.object.writer();
5502 const w = f.object.writer();
55025503 const inst_ty = f.typeOfIndex(inst);
55035504 const inst_local = if (inst_ty.hasRuntimeBitsIgnoreComptime(zcu)) local: {
55045505 const inst_local = try f.allocLocalValue(.{
......@@ -5506,10 +5507,10 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
55065507 .alignas = CType.AlignAs.fromAbiAlignment(inst_ty.abiAlignment(zcu)),
55075508 });
55085509 if (f.wantSafety()) {
5509 try f.writeCValue(writer, inst_local, .Other);
5510 try writer.writeAll(" = ");
5511 try f.writeCValue(writer, .{ .undef = inst_ty }, .Other);
5512 try writer.writeAll(";\n");
5510 try f.writeCValue(w, inst_local, .Other);
5511 try w.writeAll(" = ");
5512 try f.writeCValue(w, .{ .undef = inst_ty }, .Other);
5513 try w.writeAll(";\n");
55135514 }
55145515 break :local inst_local;
55155516 } else .none;
......@@ -5533,21 +5534,21 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
55335534 const is_reg = constraint[1] == '{';
55345535 if (is_reg) {
55355536 const output_ty = if (output == .none) inst_ty else f.typeOf(output).childType(zcu);
5536 try writer.writeAll("register ");
5537 try w.writeAll("register ");
55375538 const output_local = try f.allocLocalValue(.{
55385539 .ctype = try f.ctypeFromType(output_ty, .complete),
55395540 .alignas = CType.AlignAs.fromAbiAlignment(output_ty.abiAlignment(zcu)),
55405541 });
55415542 try f.allocs.put(gpa, output_local.new_local, false);
5542 try f.object.dg.renderTypeAndName(writer, output_ty, output_local, .{}, .none, .complete);
5543 try writer.writeAll(" __asm(\"");
5544 try writer.writeAll(constraint["={".len .. constraint.len - "}".len]);
5545 try writer.writeAll("\")");
5543 try f.object.dg.renderTypeAndName(w, output_ty, output_local, .{}, .none, .complete);
5544 try w.writeAll(" __asm(\"");
5545 try w.writeAll(constraint["={".len .. constraint.len - "}".len]);
5546 try w.writeAll("\")");
55465547 if (f.wantSafety()) {
5547 try writer.writeAll(" = ");
5548 try f.writeCValue(writer, .{ .undef = output_ty }, .Other);
5548 try w.writeAll(" = ");
5549 try f.writeCValue(w, .{ .undef = output_ty }, .Other);
55495550 }
5550 try writer.writeAll(";\n");
5551 try w.writeAll(";\n");
55515552 }
55525553 }
55535554 for (inputs) |input| {
......@@ -5568,21 +5569,21 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
55685569 const input_val = try f.resolveInst(input);
55695570 if (asmInputNeedsLocal(f, constraint, input_val)) {
55705571 const input_ty = f.typeOf(input);
5571 if (is_reg) try writer.writeAll("register ");
5572 if (is_reg) try w.writeAll("register ");
55725573 const input_local = try f.allocLocalValue(.{
55735574 .ctype = try f.ctypeFromType(input_ty, .complete),
55745575 .alignas = CType.AlignAs.fromAbiAlignment(input_ty.abiAlignment(zcu)),
55755576 });
55765577 try f.allocs.put(gpa, input_local.new_local, false);
5577 try f.object.dg.renderTypeAndName(writer, input_ty, input_local, Const, .none, .complete);
5578 try f.object.dg.renderTypeAndName(w, input_ty, input_local, Const, .none, .complete);
55785579 if (is_reg) {
5579 try writer.writeAll(" __asm(\"");
5580 try writer.writeAll(constraint["{".len .. constraint.len - "}".len]);
5581 try writer.writeAll("\")");
5580 try w.writeAll(" __asm(\"");
5581 try w.writeAll(constraint["{".len .. constraint.len - "}".len]);
5582 try w.writeAll("\")");
55825583 }
5583 try writer.writeAll(" = ");
5584 try f.writeCValue(writer, input_val, .Other);
5585 try writer.writeAll(";\n");
5584 try w.writeAll(" = ");
5585 try f.writeCValue(w, input_val, .Other);
5586 try w.writeAll(";\n");
55865587 }
55875588 }
55885589 for (0..clobbers_len) |_| {
......@@ -5642,14 +5643,14 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
56425643 }
56435644 }
56445645
5645 try writer.writeAll("__asm");
5646 if (is_volatile) try writer.writeAll(" volatile");
5647 try writer.print("({f}", .{fmtStringLiteral(fixed_asm_source[0..dst_i], null)});
5646 try w.writeAll("__asm");
5647 if (is_volatile) try w.writeAll(" volatile");
5648 try w.print("({f}", .{fmtStringLiteral(fixed_asm_source[0..dst_i], null)});
56485649 }
56495650
56505651 extra_i = constraints_extra_begin;
56515652 var locals_index = locals_begin;
5652 try writer.writeByte(':');
5653 try w.writeByte(':');
56535654 for (outputs, 0..) |output, index| {
56545655 const extra_bytes = mem.sliceAsBytes(f.air.extra.items[extra_i..]);
56555656 const constraint = mem.sliceTo(extra_bytes, 0);
......@@ -5658,22 +5659,22 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
56585659 // for the string, we still use the next u32 for the null terminator.
56595660 extra_i += (constraint.len + name.len + (2 + 3)) / 4;
56605661
5661 if (index > 0) try writer.writeByte(',');
5662 try writer.writeByte(' ');
5663 if (!mem.eql(u8, name, "_")) try writer.print("[{s}]", .{name});
5662 if (index > 0) try w.writeByte(',');
5663 try w.writeByte(' ');
5664 if (!mem.eql(u8, name, "_")) try w.print("[{s}]", .{name});
56645665 const is_reg = constraint[1] == '{';
5665 try writer.print("{f}(", .{fmtStringLiteral(if (is_reg) "=r" else constraint, null)});
5666 try w.print("{f}(", .{fmtStringLiteral(if (is_reg) "=r" else constraint, null)});
56665667 if (is_reg) {
5667 try f.writeCValue(writer, .{ .local = locals_index }, .Other);
5668 try f.writeCValue(w, .{ .local = locals_index }, .Other);
56685669 locals_index += 1;
56695670 } else if (output == .none) {
5670 try f.writeCValue(writer, inst_local, .FunctionArgument);
5671 try f.writeCValue(w, inst_local, .FunctionArgument);
56715672 } else {
5672 try f.writeCValueDeref(writer, try f.resolveInst(output));
5673 try f.writeCValueDeref(w, try f.resolveInst(output));
56735674 }
5674 try writer.writeByte(')');
5675 try w.writeByte(')');
56755676 }
5676 try writer.writeByte(':');
5677 try w.writeByte(':');
56775678 for (inputs, 0..) |input, index| {
56785679 const extra_bytes = mem.sliceAsBytes(f.air.extra.items[extra_i..]);
56795680 const constraint = mem.sliceTo(extra_bytes, 0);
......@@ -5682,21 +5683,21 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
56825683 // for the string, we still use the next u32 for the null terminator.
56835684 extra_i += (constraint.len + name.len + (2 + 3)) / 4;
56845685
5685 if (index > 0) try writer.writeByte(',');
5686 try writer.writeByte(' ');
5687 if (!mem.eql(u8, name, "_")) try writer.print("[{s}]", .{name});
5686 if (index > 0) try w.writeByte(',');
5687 try w.writeByte(' ');
5688 if (!mem.eql(u8, name, "_")) try w.print("[{s}]", .{name});
56885689
56895690 const is_reg = constraint[0] == '{';
56905691 const input_val = try f.resolveInst(input);
5691 try writer.print("{f}(", .{fmtStringLiteral(if (is_reg) "r" else constraint, null)});
5692 try f.writeCValue(writer, if (asmInputNeedsLocal(f, constraint, input_val)) local: {
5692 try w.print("{f}(", .{fmtStringLiteral(if (is_reg) "r" else constraint, null)});
5693 try f.writeCValue(w, if (asmInputNeedsLocal(f, constraint, input_val)) local: {
56935694 const input_local_idx = locals_index;
56945695 locals_index += 1;
56955696 break :local .{ .local = input_local_idx };
56965697 } else input_val, .Other);
5697 try writer.writeByte(')');
5698 try w.writeByte(')');
56985699 }
5699 try writer.writeByte(':');
5700 try w.writeByte(':');
57005701 for (0..clobbers_len) |clobber_i| {
57015702 const clobber = mem.sliceTo(mem.sliceAsBytes(f.air.extra.items[extra_i..]), 0);
57025703 // This equation accounts for the fact that even if we have exactly 4 bytes
......@@ -5705,10 +5706,10 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
57055706
57065707 if (clobber.len == 0) continue;
57075708
5708 if (clobber_i > 0) try writer.writeByte(',');
5709 try writer.print(" {f}", .{fmtStringLiteral(clobber, null)});
5709 if (clobber_i > 0) try w.writeByte(',');
5710 try w.print(" {f}", .{fmtStringLiteral(clobber, null)});
57105711 }
5711 try writer.writeAll(");\n");
5712 try w.writeAll(");\n");
57125713
57135714 extra_i = constraints_extra_begin;
57145715 locals_index = locals_begin;
......@@ -5722,14 +5723,14 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
57225723
57235724 const is_reg = constraint[1] == '{';
57245725 if (is_reg) {
5725 try f.writeCValueDeref(writer, if (output == .none)
5726 try f.writeCValueDeref(w, if (output == .none)
57265727 .{ .local_ref = inst_local.new_local }
57275728 else
57285729 try f.resolveInst(output));
5729 try writer.writeAll(" = ");
5730 try f.writeCValue(writer, .{ .local = locals_index }, .Other);
5730 try w.writeAll(" = ");
5731 try f.writeCValue(w, .{ .local = locals_index }, .Other);
57315732 locals_index += 1;
5732 try writer.writeAll(";\n");
5733 try w.writeAll(";\n");
57335734 }
57345735 }
57355736
......@@ -5759,14 +5760,14 @@ fn airIsNull(
57595760 const ctype_pool = &f.object.dg.ctype_pool;
57605761 const un_op = f.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
57615762
5762 const writer = f.object.writer();
5763 const w = f.object.writer();
57635764 const operand = try f.resolveInst(un_op);
57645765 try reap(f, inst, &.{un_op});
57655766
57665767 const local = try f.allocLocal(inst, .bool);
5767 const a = try Assignment.start(f, writer, .bool);
5768 try f.writeCValue(writer, local, .Other);
5769 try a.assign(f, writer);
5768 const a = try Assignment.start(f, w, .bool);
5769 try f.writeCValue(w, local, .Other);
5770 try a.assign(f, w);
57705771
57715772 const operand_ty = f.typeOf(un_op);
57725773 const optional_ty = if (is_ptr) operand_ty.childType(zcu) else operand_ty;
......@@ -5774,9 +5775,9 @@ fn airIsNull(
57745775 const rhs = switch (opt_ctype.info(ctype_pool)) {
57755776 .basic, .pointer => rhs: {
57765777 if (is_ptr)
5777 try f.writeCValueDeref(writer, operand)
5778 try f.writeCValueDeref(w, operand)
57785779 else
5779 try f.writeCValue(writer, operand, .Other);
5780 try f.writeCValue(w, operand, .Other);
57805781 break :rhs if (opt_ctype.isBool())
57815782 "true"
57825783 else if (opt_ctype.isInteger())
......@@ -5788,24 +5789,24 @@ fn airIsNull(
57885789 .aggregate => |aggregate| switch (aggregate.fields.at(0, ctype_pool).name.index) {
57895790 .is_null, .payload => rhs: {
57905791 if (is_ptr)
5791 try f.writeCValueDerefMember(writer, operand, .{ .identifier = "is_null" })
5792 try f.writeCValueDerefMember(w, operand, .{ .identifier = "is_null" })
57925793 else
5793 try f.writeCValueMember(writer, operand, .{ .identifier = "is_null" });
5794 try f.writeCValueMember(w, operand, .{ .identifier = "is_null" });
57945795 break :rhs "true";
57955796 },
57965797 .ptr, .len => rhs: {
57975798 if (is_ptr)
5798 try f.writeCValueDerefMember(writer, operand, .{ .identifier = "ptr" })
5799 try f.writeCValueDerefMember(w, operand, .{ .identifier = "ptr" })
57995800 else
5800 try f.writeCValueMember(writer, operand, .{ .identifier = "ptr" });
5801 try f.writeCValueMember(w, operand, .{ .identifier = "ptr" });
58015802 break :rhs "NULL";
58025803 },
58035804 else => unreachable,
58045805 },
58055806 };
5806 try writer.writeAll(compareOperatorC(operator));
5807 try writer.writeAll(rhs);
5808 try a.end(f, writer);
5807 try w.writeAll(compareOperatorC(operator));
5808 try w.writeAll(rhs);
5809 try a.end(f, w);
58095810 return local;
58105811}
58115812
......@@ -5827,16 +5828,16 @@ fn airOptionalPayload(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValue
58275828 .aligned, .array, .vector, .fwd_decl, .function => unreachable,
58285829 .aggregate => |aggregate| switch (aggregate.fields.at(0, ctype_pool).name.index) {
58295830 .is_null, .payload => {
5830 const writer = f.object.writer();
5831 const w = f.object.writer();
58315832 const local = try f.allocLocal(inst, inst_ty);
5832 const a = try Assignment.start(f, writer, try f.ctypeFromType(inst_ty, .complete));
5833 try f.writeCValue(writer, local, .Other);
5834 try a.assign(f, writer);
5833 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete));
5834 try f.writeCValue(w, local, .Other);
5835 try a.assign(f, w);
58355836 if (is_ptr) {
5836 try writer.writeByte('&');
5837 try f.writeCValueDerefMember(writer, operand, .{ .identifier = "payload" });
5838 } else try f.writeCValueMember(writer, operand, .{ .identifier = "payload" });
5839 try a.end(f, writer);
5837 try w.writeByte('&');
5838 try f.writeCValueDerefMember(w, operand, .{ .identifier = "payload" });
5839 } else try f.writeCValueMember(w, operand, .{ .identifier = "payload" });
5840 try a.end(f, w);
58405841 return local;
58415842 },
58425843 .ptr, .len => return f.moveCValue(inst, inst_ty, operand),
......@@ -5849,7 +5850,7 @@ fn airOptionalPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue {
58495850 const pt = f.object.dg.pt;
58505851 const zcu = pt.zcu;
58515852 const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
5852 const writer = f.object.writer();
5853 const w = f.object.writer();
58535854 const operand = try f.resolveInst(ty_op.operand);
58545855 try reap(f, inst, &.{ty_op.operand});
58555856 const operand_ty = f.typeOf(ty_op.operand);
......@@ -5858,40 +5859,40 @@ fn airOptionalPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue {
58585859 const opt_ctype = try f.ctypeFromType(operand_ty.childType(zcu), .complete);
58595860 switch (opt_ctype.info(&f.object.dg.ctype_pool)) {
58605861 .basic => {
5861 const a = try Assignment.start(f, writer, opt_ctype);
5862 try f.writeCValueDeref(writer, operand);
5863 try a.assign(f, writer);
5864 try f.object.dg.renderValue(writer, Value.false, .Other);
5865 try a.end(f, writer);
5862 const a = try Assignment.start(f, w, opt_ctype);
5863 try f.writeCValueDeref(w, operand);
5864 try a.assign(f, w);
5865 try f.object.dg.renderValue(w, Value.false, .Other);
5866 try a.end(f, w);
58665867 return .none;
58675868 },
58685869 .pointer => {
58695870 if (f.liveness.isUnused(inst)) return .none;
58705871 const local = try f.allocLocal(inst, inst_ty);
5871 const a = try Assignment.start(f, writer, opt_ctype);
5872 try f.writeCValue(writer, local, .Other);
5873 try a.assign(f, writer);
5874 try f.writeCValue(writer, operand, .Other);
5875 try a.end(f, writer);
5872 const a = try Assignment.start(f, w, opt_ctype);
5873 try f.writeCValue(w, local, .Other);
5874 try a.assign(f, w);
5875 try f.writeCValue(w, operand, .Other);
5876 try a.end(f, w);
58765877 return local;
58775878 },
58785879 .aligned, .array, .vector, .fwd_decl, .function => unreachable,
58795880 .aggregate => {
58805881 {
5881 const a = try Assignment.start(f, writer, opt_ctype);
5882 try f.writeCValueDerefMember(writer, operand, .{ .identifier = "is_null" });
5883 try a.assign(f, writer);
5884 try f.object.dg.renderValue(writer, Value.false, .Other);
5885 try a.end(f, writer);
5882 const a = try Assignment.start(f, w, opt_ctype);
5883 try f.writeCValueDerefMember(w, operand, .{ .identifier = "is_null" });
5884 try a.assign(f, w);
5885 try f.object.dg.renderValue(w, Value.false, .Other);
5886 try a.end(f, w);
58865887 }
58875888 if (f.liveness.isUnused(inst)) return .none;
58885889 const local = try f.allocLocal(inst, inst_ty);
5889 const a = try Assignment.start(f, writer, opt_ctype);
5890 try f.writeCValue(writer, local, .Other);
5891 try a.assign(f, writer);
5892 try writer.writeByte('&');
5893 try f.writeCValueDerefMember(writer, operand, .{ .identifier = "payload" });
5894 try a.end(f, writer);
5890 const a = try Assignment.start(f, w, opt_ctype);
5891 try f.writeCValue(w, local, .Other);
5892 try a.assign(f, w);
5893 try w.writeByte('&');
5894 try f.writeCValueDerefMember(w, operand, .{ .identifier = "payload" });
5895 try a.end(f, w);
58955896 return local;
58965897 },
58975898 }
......@@ -5999,42 +6000,42 @@ fn airFieldParentPtr(f: *Function, inst: Air.Inst.Index) !CValue {
59996000 const field_ptr_val = try f.resolveInst(extra.field_ptr);
60006001 try reap(f, inst, &.{extra.field_ptr});
60016002
6002 const writer = f.object.writer();
6003 const w = f.object.writer();
60036004 const local = try f.allocLocal(inst, container_ptr_ty);
6004 try f.writeCValue(writer, local, .Other);
6005 try writer.writeAll(" = (");
6006 try f.renderType(writer, container_ptr_ty);
6007 try writer.writeByte(')');
6005 try f.writeCValue(w, local, .Other);
6006 try w.writeAll(" = (");
6007 try f.renderType(w, container_ptr_ty);
6008 try w.writeByte(')');
60086009
60096010 switch (fieldLocation(container_ptr_ty, field_ptr_ty, extra.field_index, pt)) {
6010 .begin => try f.writeCValue(writer, field_ptr_val, .Other),
6011 .begin => try f.writeCValue(w, field_ptr_val, .Other),
60116012 .field => |field| {
60126013 const u8_ptr_ty = try pt.adjustPtrTypeChild(field_ptr_ty, .u8);
60136014
6014 try writer.writeAll("((");
6015 try f.renderType(writer, u8_ptr_ty);
6016 try writer.writeByte(')');
6017 try f.writeCValue(writer, field_ptr_val, .Other);
6018 try writer.writeAll(" - offsetof(");
6019 try f.renderType(writer, container_ty);
6020 try writer.writeAll(", ");
6021 try f.writeCValue(writer, field, .Other);
6022 try writer.writeAll("))");
6015 try w.writeAll("((");
6016 try f.renderType(w, u8_ptr_ty);
6017 try w.writeByte(')');
6018 try f.writeCValue(w, field_ptr_val, .Other);
6019 try w.writeAll(" - offsetof(");
6020 try f.renderType(w, container_ty);
6021 try w.writeAll(", ");
6022 try f.writeCValue(w, field, .Other);
6023 try w.writeAll("))");
60236024 },
60246025 .byte_offset => |byte_offset| {
60256026 const u8_ptr_ty = try pt.adjustPtrTypeChild(field_ptr_ty, .u8);
60266027
6027 try writer.writeAll("((");
6028 try f.renderType(writer, u8_ptr_ty);
6029 try writer.writeByte(')');
6030 try f.writeCValue(writer, field_ptr_val, .Other);
6031 try writer.print(" - {f})", .{
6028 try w.writeAll("((");
6029 try f.renderType(w, u8_ptr_ty);
6030 try w.writeByte(')');
6031 try f.writeCValue(w, field_ptr_val, .Other);
6032 try w.print(" - {f})", .{
60326033 try f.fmtIntLiteralDec(try pt.intValue(.usize, byte_offset)),
60336034 });
60346035 },
60356036 }
60366037
6037 try writer.writeAll(";\n");
6038 try w.writeAll(";\n");
60386039 return local;
60396040}
60406041
......@@ -6053,33 +6054,33 @@ fn fieldPtr(
60536054 // Ensure complete type definition is visible before accessing fields.
60546055 _ = try f.ctypeFromType(container_ty, .complete);
60556056
6056 const writer = f.object.writer();
6057 const w = f.object.writer();
60576058 const local = try f.allocLocal(inst, field_ptr_ty);
6058 try f.writeCValue(writer, local, .Other);
6059 try writer.writeAll(" = (");
6060 try f.renderType(writer, field_ptr_ty);
6061 try writer.writeByte(')');
6059 try f.writeCValue(w, local, .Other);
6060 try w.writeAll(" = (");
6061 try f.renderType(w, field_ptr_ty);
6062 try w.writeByte(')');
60626063
60636064 switch (fieldLocation(container_ptr_ty, field_ptr_ty, field_index, pt)) {
6064 .begin => try f.writeCValue(writer, container_ptr_val, .Other),
6065 .begin => try f.writeCValue(w, container_ptr_val, .Other),
60656066 .field => |field| {
6066 try writer.writeByte('&');
6067 try f.writeCValueDerefMember(writer, container_ptr_val, field);
6067 try w.writeByte('&');
6068 try f.writeCValueDerefMember(w, container_ptr_val, field);
60686069 },
60696070 .byte_offset => |byte_offset| {
60706071 const u8_ptr_ty = try pt.adjustPtrTypeChild(field_ptr_ty, .u8);
60716072
6072 try writer.writeAll("((");
6073 try f.renderType(writer, u8_ptr_ty);
6074 try writer.writeByte(')');
6075 try f.writeCValue(writer, container_ptr_val, .Other);
6076 try writer.print(" + {f})", .{
6073 try w.writeAll("((");
6074 try f.renderType(w, u8_ptr_ty);
6075 try w.writeByte(')');
6076 try f.writeCValue(w, container_ptr_val, .Other);
6077 try w.print(" + {f})", .{
60776078 try f.fmtIntLiteralDec(try pt.intValue(.usize, byte_offset)),
60786079 });
60796080 },
60806081 }
60816082
6082 try writer.writeAll(";\n");
6083 try w.writeAll(";\n");
60836084 return local;
60846085}
60856086
......@@ -6099,7 +6100,7 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue {
60996100 const struct_byval = try f.resolveInst(extra.struct_operand);
61006101 try reap(f, inst, &.{extra.struct_operand});
61016102 const struct_ty = f.typeOf(extra.struct_operand);
6102 const writer = f.object.writer();
6103 const w = f.object.writer();
61036104
61046105 // Ensure complete type definition is visible before accessing fields.
61056106 _ = try f.ctypeFromType(struct_ty, .complete);
......@@ -6126,42 +6127,42 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue {
61266127 const field_int_ty = try pt.intType(field_int_signedness, @as(u16, @intCast(inst_ty.bitSize(zcu))));
61276128
61286129 const temp_local = try f.allocLocal(inst, field_int_ty);
6129 try f.writeCValue(writer, temp_local, .Other);
6130 try writer.writeAll(" = zig_wrap_");
6131 try f.object.dg.renderTypeForBuiltinFnName(writer, field_int_ty);
6132 try writer.writeAll("((");
6133 try f.renderType(writer, field_int_ty);
6134 try writer.writeByte(')');
6130 try f.writeCValue(w, temp_local, .Other);
6131 try w.writeAll(" = zig_wrap_");
6132 try f.object.dg.renderTypeForBuiltinFnName(w, field_int_ty);
6133 try w.writeAll("((");
6134 try f.renderType(w, field_int_ty);
6135 try w.writeByte(')');
61356136 const cant_cast = int_info.bits > 64;
61366137 if (cant_cast) {
61376138 if (field_int_ty.bitSize(zcu) > 64) return f.fail("TODO: C backend: implement casting between types > 64 bits", .{});
6138 try writer.writeAll("zig_lo_");
6139 try f.object.dg.renderTypeForBuiltinFnName(writer, struct_ty);
6140 try writer.writeByte('(');
6139 try w.writeAll("zig_lo_");
6140 try f.object.dg.renderTypeForBuiltinFnName(w, struct_ty);
6141 try w.writeByte('(');
61416142 }
61426143 if (bit_offset > 0) {
6143 try writer.writeAll("zig_shr_");
6144 try f.object.dg.renderTypeForBuiltinFnName(writer, struct_ty);
6145 try writer.writeByte('(');
6144 try w.writeAll("zig_shr_");
6145 try f.object.dg.renderTypeForBuiltinFnName(w, struct_ty);
6146 try w.writeByte('(');
61466147 }
6147 try f.writeCValue(writer, struct_byval, .Other);
6148 if (bit_offset > 0) try writer.print(", {f})", .{
6148 try f.writeCValue(w, struct_byval, .Other);
6149 if (bit_offset > 0) try w.print(", {f})", .{
61496150 try f.fmtIntLiteralDec(try pt.intValue(bit_offset_ty, bit_offset)),
61506151 });
6151 if (cant_cast) try writer.writeByte(')');
6152 try f.object.dg.renderBuiltinInfo(writer, field_int_ty, .bits);
6153 try writer.writeAll(");\n");
6152 if (cant_cast) try w.writeByte(')');
6153 try f.object.dg.renderBuiltinInfo(w, field_int_ty, .bits);
6154 try w.writeAll(");\n");
61546155 if (inst_ty.eql(field_int_ty, zcu)) return temp_local;
61556156
61566157 const local = try f.allocLocal(inst, inst_ty);
61576158 if (local.new_local != temp_local.new_local) {
6158 try writer.writeAll("memcpy(");
6159 try f.writeCValue(writer, .{ .local_ref = local.new_local }, .FunctionArgument);
6160 try writer.writeAll(", ");
6161 try f.writeCValue(writer, .{ .local_ref = temp_local.new_local }, .FunctionArgument);
6162 try writer.writeAll(", sizeof(");
6163 try f.renderType(writer, inst_ty);
6164 try writer.writeAll("));\n");
6159 try w.writeAll("memcpy(");
6160 try f.writeCValue(w, .{ .local_ref = local.new_local }, .FunctionArgument);
6161 try w.writeAll(", ");
6162 try f.writeCValue(w, .{ .local_ref = temp_local.new_local }, .FunctionArgument);
6163 try w.writeAll(", sizeof(");
6164 try f.renderType(w, inst_ty);
6165 try w.writeAll("));\n");
61656166 }
61666167 try freeLocal(f, inst, temp_local.new_local, null);
61676168 return local;
......@@ -6182,10 +6183,10 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue {
61826183 .@"packed" => {
61836184 const operand_lval = if (struct_byval == .constant) blk: {
61846185 const operand_local = try f.allocLocal(inst, struct_ty);
6185 try f.writeCValue(writer, operand_local, .Other);
6186 try writer.writeAll(" = ");
6187 try f.writeCValue(writer, struct_byval, .Other);
6188 try writer.writeAll(";\n");
6186 try f.writeCValue(w, operand_local, .Other);
6187 try w.writeAll(" = ");
6188 try f.writeCValue(w, struct_byval, .Other);
6189 try w.writeAll(";\n");
61896190 break :blk operand_local;
61906191 } else struct_byval;
61916192 const local = try f.allocLocal(inst, inst_ty);
......@@ -6196,13 +6197,13 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue {
61966197 },
61976198 else => true,
61986199 }) {
6199 try writer.writeAll("memcpy(&");
6200 try f.writeCValue(writer, local, .Other);
6201 try writer.writeAll(", &");
6202 try f.writeCValue(writer, operand_lval, .Other);
6203 try writer.writeAll(", sizeof(");
6204 try f.renderType(writer, inst_ty);
6205 try writer.writeAll("));\n");
6200 try w.writeAll("memcpy(&");
6201 try f.writeCValue(w, local, .Other);
6202 try w.writeAll(", &");
6203 try f.writeCValue(w, operand_lval, .Other);
6204 try w.writeAll(", sizeof(");
6205 try f.renderType(w, inst_ty);
6206 try w.writeAll("));\n");
62066207 }
62076208 try f.freeCValue(inst, operand_lval);
62086209 return local;
......@@ -6213,11 +6214,11 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue {
62136214 };
62146215
62156216 const local = try f.allocLocal(inst, inst_ty);
6216 const a = try Assignment.start(f, writer, try f.ctypeFromType(inst_ty, .complete));
6217 try f.writeCValue(writer, local, .Other);
6218 try a.assign(f, writer);
6219 try f.writeCValueMember(writer, struct_byval, field_name);
6220 try a.end(f, writer);
6217 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete));
6218 try f.writeCValue(w, local, .Other);
6219 try a.assign(f, w);
6220 try f.writeCValueMember(w, struct_byval, field_name);
6221 try a.end(f, w);
62216222 return local;
62226223}
62236224
......@@ -6244,21 +6245,21 @@ fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue {
62446245 return local;
62456246 }
62466247
6247 const writer = f.object.writer();
6248 try f.writeCValue(writer, local, .Other);
6249 try writer.writeAll(" = ");
6248 const w = f.object.writer();
6249 try f.writeCValue(w, local, .Other);
6250 try w.writeAll(" = ");
62506251
62516252 if (!payload_ty.hasRuntimeBits(zcu))
6252 try f.writeCValue(writer, operand, .Other)
6253 try f.writeCValue(w, operand, .Other)
62536254 else if (error_ty.errorSetIsEmpty(zcu))
6254 try writer.print("{f}", .{
6255 try w.print("{f}", .{
62556256 try f.fmtIntLiteralDec(try pt.intValue(try pt.errorIntType(), 0)),
62566257 })
62576258 else if (operand_is_ptr)
6258 try f.writeCValueDerefMember(writer, operand, .{ .identifier = "error" })
6259 try f.writeCValueDerefMember(w, operand, .{ .identifier = "error" })
62596260 else
6260 try f.writeCValueMember(writer, operand, .{ .identifier = "error" });
6261 try writer.writeAll(";\n");
6261 try f.writeCValueMember(w, operand, .{ .identifier = "error" });
6262 try w.writeAll(";\n");
62626263 return local;
62636264}
62646265
......@@ -6273,29 +6274,29 @@ fn airUnwrapErrUnionPay(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValu
62736274 const operand_ty = f.typeOf(ty_op.operand);
62746275 const error_union_ty = if (is_ptr) operand_ty.childType(zcu) else operand_ty;
62756276
6276 const writer = f.object.writer();
6277 const w = f.object.writer();
62776278 if (!error_union_ty.errorUnionPayload(zcu).hasRuntimeBits(zcu)) {
62786279 if (!is_ptr) return .none;
62796280
62806281 const local = try f.allocLocal(inst, inst_ty);
6281 try f.writeCValue(writer, local, .Other);
6282 try writer.writeAll(" = (");
6283 try f.renderType(writer, inst_ty);
6284 try writer.writeByte(')');
6285 try f.writeCValue(writer, operand, .Other);
6286 try writer.writeAll(";\n");
6282 try f.writeCValue(w, local, .Other);
6283 try w.writeAll(" = (");
6284 try f.renderType(w, inst_ty);
6285 try w.writeByte(')');
6286 try f.writeCValue(w, operand, .Other);
6287 try w.writeAll(";\n");
62876288 return local;
62886289 }
62896290
62906291 const local = try f.allocLocal(inst, inst_ty);
6291 const a = try Assignment.start(f, writer, try f.ctypeFromType(inst_ty, .complete));
6292 try f.writeCValue(writer, local, .Other);
6293 try a.assign(f, writer);
6292 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete));
6293 try f.writeCValue(w, local, .Other);
6294 try a.assign(f, w);
62946295 if (is_ptr) {
6295 try writer.writeByte('&');
6296 try f.writeCValueDerefMember(writer, operand, .{ .identifier = "payload" });
6297 } else try f.writeCValueMember(writer, operand, .{ .identifier = "payload" });
6298 try a.end(f, writer);
6296 try w.writeByte('&');
6297 try f.writeCValueDerefMember(w, operand, .{ .identifier = "payload" });
6298 } else try f.writeCValueMember(w, operand, .{ .identifier = "payload" });
6299 try a.end(f, w);
62996300 return local;
63006301}
63016302
......@@ -6314,21 +6315,21 @@ fn airWrapOptional(f: *Function, inst: Air.Inst.Index) !CValue {
63146315 .aggregate => |aggregate| switch (aggregate.fields.at(0, ctype_pool).name.index) {
63156316 .is_null, .payload => {
63166317 const operand_ctype = try f.ctypeFromType(f.typeOf(ty_op.operand), .complete);
6317 const writer = f.object.writer();
6318 const w = f.object.writer();
63186319 const local = try f.allocLocal(inst, inst_ty);
63196320 {
6320 const a = try Assignment.start(f, writer, .bool);
6321 try f.writeCValueMember(writer, local, .{ .identifier = "is_null" });
6322 try a.assign(f, writer);
6323 try writer.writeAll("false");
6324 try a.end(f, writer);
6321 const a = try Assignment.start(f, w, .bool);
6322 try f.writeCValueMember(w, local, .{ .identifier = "is_null" });
6323 try a.assign(f, w);
6324 try w.writeAll("false");
6325 try a.end(f, w);
63256326 }
63266327 {
6327 const a = try Assignment.start(f, writer, operand_ctype);
6328 try f.writeCValueMember(writer, local, .{ .identifier = "payload" });
6329 try a.assign(f, writer);
6330 try f.writeCValue(writer, operand, .Other);
6331 try a.end(f, writer);
6328 const a = try Assignment.start(f, w, operand_ctype);
6329 try f.writeCValueMember(w, local, .{ .identifier = "payload" });
6330 try a.assign(f, w);
6331 try f.writeCValue(w, operand, .Other);
6332 try a.end(f, w);
63326333 }
63336334 return local;
63346335 },
......@@ -6350,7 +6351,7 @@ fn airWrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue {
63506351 const err = try f.resolveInst(ty_op.operand);
63516352 try reap(f, inst, &.{ty_op.operand});
63526353
6353 const writer = f.object.writer();
6354 const w = f.object.writer();
63546355 const local = try f.allocLocal(inst, inst_ty);
63556356
63566357 if (repr_is_err and err == .local and err.local == local.new_local) {
......@@ -6359,21 +6360,21 @@ fn airWrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue {
63596360 }
63606361
63616362 if (!repr_is_err) {
6362 const a = try Assignment.start(f, writer, try f.ctypeFromType(payload_ty, .complete));
6363 try f.writeCValueMember(writer, local, .{ .identifier = "payload" });
6364 try a.assign(f, writer);
6365 try f.object.dg.renderUndefValue(writer, payload_ty, .Other);
6366 try a.end(f, writer);
6363 const a = try Assignment.start(f, w, try f.ctypeFromType(payload_ty, .complete));
6364 try f.writeCValueMember(w, local, .{ .identifier = "payload" });
6365 try a.assign(f, w);
6366 try f.object.dg.renderUndefValue(w, payload_ty, .Other);
6367 try a.end(f, w);
63676368 }
63686369 {
6369 const a = try Assignment.start(f, writer, try f.ctypeFromType(err_ty, .complete));
6370 const a = try Assignment.start(f, w, try f.ctypeFromType(err_ty, .complete));
63706371 if (repr_is_err)
6371 try f.writeCValue(writer, local, .Other)
6372 try f.writeCValue(w, local, .Other)
63726373 else
6373 try f.writeCValueMember(writer, local, .{ .identifier = "error" });
6374 try a.assign(f, writer);
6375 try f.writeCValue(writer, err, .Other);
6376 try a.end(f, writer);
6374 try f.writeCValueMember(w, local, .{ .identifier = "error" });
6375 try a.assign(f, w);
6376 try f.writeCValue(w, err, .Other);
6377 try a.end(f, w);
63776378 }
63786379 return local;
63796380}
......@@ -6381,7 +6382,7 @@ fn airWrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue {
63816382fn airErrUnionPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue {
63826383 const pt = f.object.dg.pt;
63836384 const zcu = pt.zcu;
6384 const writer = f.object.writer();
6385 const w = f.object.writer();
63856386 const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
63866387 const inst_ty = f.typeOfIndex(inst);
63876388 const operand = try f.resolveInst(ty_op.operand);
......@@ -6395,31 +6396,31 @@ fn airErrUnionPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue {
63956396
63966397 // First, set the non-error value.
63976398 if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) {
6398 const a = try Assignment.start(f, writer, try f.ctypeFromType(operand_ty, .complete));
6399 try f.writeCValueDeref(writer, operand);
6400 try a.assign(f, writer);
6401 try writer.print("{f}", .{try f.fmtIntLiteralDec(no_err)});
6402 try a.end(f, writer);
6399 const a = try Assignment.start(f, w, try f.ctypeFromType(operand_ty, .complete));
6400 try f.writeCValueDeref(w, operand);
6401 try a.assign(f, w);
6402 try w.print("{f}", .{try f.fmtIntLiteralDec(no_err)});
6403 try a.end(f, w);
64036404 return .none;
64046405 }
64056406 {
6406 const a = try Assignment.start(f, writer, try f.ctypeFromType(err_int_ty, .complete));
6407 try f.writeCValueDerefMember(writer, operand, .{ .identifier = "error" });
6408 try a.assign(f, writer);
6409 try writer.print("{f}", .{try f.fmtIntLiteralDec(no_err)});
6410 try a.end(f, writer);
6407 const a = try Assignment.start(f, w, try f.ctypeFromType(err_int_ty, .complete));
6408 try f.writeCValueDerefMember(w, operand, .{ .identifier = "error" });
6409 try a.assign(f, w);
6410 try w.print("{f}", .{try f.fmtIntLiteralDec(no_err)});
6411 try a.end(f, w);
64116412 }
64126413
64136414 // Then return the payload pointer (only if it is used)
64146415 if (f.liveness.isUnused(inst)) return .none;
64156416
64166417 const local = try f.allocLocal(inst, inst_ty);
6417 const a = try Assignment.start(f, writer, try f.ctypeFromType(inst_ty, .complete));
6418 try f.writeCValue(writer, local, .Other);
6419 try a.assign(f, writer);
6420 try writer.writeByte('&');
6421 try f.writeCValueDerefMember(writer, operand, .{ .identifier = "payload" });
6422 try a.end(f, writer);
6418 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete));
6419 try f.writeCValue(w, local, .Other);
6420 try a.assign(f, w);
6421 try w.writeByte('&');
6422 try f.writeCValueDerefMember(w, operand, .{ .identifier = "payload" });
6423 try a.end(f, w);
64236424 return local;
64246425}
64256426
......@@ -6450,24 +6451,24 @@ fn airWrapErrUnionPay(f: *Function, inst: Air.Inst.Index) !CValue {
64506451 const err_ty = inst_ty.errorUnionSet(zcu);
64516452 try reap(f, inst, &.{ty_op.operand});
64526453
6453 const writer = f.object.writer();
6454 const w = f.object.writer();
64546455 const local = try f.allocLocal(inst, inst_ty);
64556456 if (!repr_is_err) {
6456 const a = try Assignment.start(f, writer, try f.ctypeFromType(payload_ty, .complete));
6457 try f.writeCValueMember(writer, local, .{ .identifier = "payload" });
6458 try a.assign(f, writer);
6459 try f.writeCValue(writer, payload, .Other);
6460 try a.end(f, writer);
6457 const a = try Assignment.start(f, w, try f.ctypeFromType(payload_ty, .complete));
6458 try f.writeCValueMember(w, local, .{ .identifier = "payload" });
6459 try a.assign(f, w);
6460 try f.writeCValue(w, payload, .Other);
6461 try a.end(f, w);
64616462 }
64626463 {
6463 const a = try Assignment.start(f, writer, try f.ctypeFromType(err_ty, .complete));
6464 const a = try Assignment.start(f, w, try f.ctypeFromType(err_ty, .complete));
64646465 if (repr_is_err)
6465 try f.writeCValue(writer, local, .Other)
6466 try f.writeCValue(w, local, .Other)
64666467 else
6467 try f.writeCValueMember(writer, local, .{ .identifier = "error" });
6468 try a.assign(f, writer);
6469 try f.object.dg.renderValue(writer, try pt.intValue(try pt.errorIntType(), 0), .Other);
6470 try a.end(f, writer);
6468 try f.writeCValueMember(w, local, .{ .identifier = "error" });
6469 try a.assign(f, w);
6470 try f.object.dg.renderValue(w, try pt.intValue(try pt.errorIntType(), 0), .Other);
6471 try a.end(f, w);
64716472 }
64726473 return local;
64736474}
......@@ -6477,7 +6478,7 @@ fn airIsErr(f: *Function, inst: Air.Inst.Index, is_ptr: bool, operator: []const
64776478 const zcu = pt.zcu;
64786479 const un_op = f.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
64796480
6480 const writer = f.object.writer();
6481 const w = f.object.writer();
64816482 const operand = try f.resolveInst(un_op);
64826483 try reap(f, inst, &.{un_op});
64836484 const operand_ty = f.typeOf(un_op);
......@@ -6486,25 +6487,25 @@ fn airIsErr(f: *Function, inst: Air.Inst.Index, is_ptr: bool, operator: []const
64866487 const payload_ty = err_union_ty.errorUnionPayload(zcu);
64876488 const error_ty = err_union_ty.errorUnionSet(zcu);
64886489
6489 const a = try Assignment.start(f, writer, .bool);
6490 try f.writeCValue(writer, local, .Other);
6491 try a.assign(f, writer);
6490 const a = try Assignment.start(f, w, .bool);
6491 try f.writeCValue(w, local, .Other);
6492 try a.assign(f, w);
64926493 const err_int_ty = try pt.errorIntType();
64936494 if (!error_ty.errorSetIsEmpty(zcu))
64946495 if (payload_ty.hasRuntimeBits(zcu))
64956496 if (is_ptr)
6496 try f.writeCValueDerefMember(writer, operand, .{ .identifier = "error" })
6497 try f.writeCValueDerefMember(w, operand, .{ .identifier = "error" })
64976498 else
6498 try f.writeCValueMember(writer, operand, .{ .identifier = "error" })
6499 try f.writeCValueMember(w, operand, .{ .identifier = "error" })
64996500 else
6500 try f.writeCValue(writer, operand, .Other)
6501 try f.writeCValue(w, operand, .Other)
65016502 else
6502 try f.object.dg.renderValue(writer, try pt.intValue(err_int_ty, 0), .Other);
6503 try writer.writeByte(' ');
6504 try writer.writeAll(operator);
6505 try writer.writeByte(' ');
6506 try f.object.dg.renderValue(writer, try pt.intValue(err_int_ty, 0), .Other);
6507 try a.end(f, writer);
6503 try f.object.dg.renderValue(w, try pt.intValue(err_int_ty, 0), .Other);
6504 try w.writeByte(' ');
6505 try w.writeAll(operator);
6506 try w.writeByte(' ');
6507 try f.object.dg.renderValue(w, try pt.intValue(err_int_ty, 0), .Other);
6508 try a.end(f, w);
65086509 return local;
65096510}
65106511
......@@ -6518,45 +6519,45 @@ fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue {
65186519 try reap(f, inst, &.{ty_op.operand});
65196520 const inst_ty = f.typeOfIndex(inst);
65206521 const ptr_ty = inst_ty.slicePtrFieldType(zcu);
6521 const writer = f.object.writer();
6522 const w = f.object.writer();
65226523 const local = try f.allocLocal(inst, inst_ty);
65236524 const operand_ty = f.typeOf(ty_op.operand);
65246525 const array_ty = operand_ty.childType(zcu);
65256526
65266527 {
6527 const a = try Assignment.start(f, writer, try f.ctypeFromType(ptr_ty, .complete));
6528 try f.writeCValueMember(writer, local, .{ .identifier = "ptr" });
6529 try a.assign(f, writer);
6528 const a = try Assignment.start(f, w, try f.ctypeFromType(ptr_ty, .complete));
6529 try f.writeCValueMember(w, local, .{ .identifier = "ptr" });
6530 try a.assign(f, w);
65306531 if (operand == .undef) {
6531 try f.writeCValue(writer, .{ .undef = inst_ty.slicePtrFieldType(zcu) }, .Other);
6532 try f.writeCValue(w, .{ .undef = inst_ty.slicePtrFieldType(zcu) }, .Other);
65326533 } else {
65336534 const ptr_ctype = try f.ctypeFromType(ptr_ty, .complete);
65346535 const ptr_child_ctype = ptr_ctype.info(ctype_pool).pointer.elem_ctype;
65356536 const elem_ty = array_ty.childType(zcu);
65366537 const elem_ctype = try f.ctypeFromType(elem_ty, .complete);
65376538 if (!ptr_child_ctype.eql(elem_ctype)) {
6538 try writer.writeByte('(');
6539 try f.renderCType(writer, ptr_ctype);
6540 try writer.writeByte(')');
6539 try w.writeByte('(');
6540 try f.renderCType(w, ptr_ctype);
6541 try w.writeByte(')');
65416542 }
65426543 const operand_ctype = try f.ctypeFromType(operand_ty, .complete);
65436544 const operand_child_ctype = operand_ctype.info(ctype_pool).pointer.elem_ctype;
65446545 if (operand_child_ctype.info(ctype_pool) == .array) {
6545 try writer.writeByte('&');
6546 try f.writeCValueDeref(writer, operand);
6547 try writer.print("[{f}]", .{try f.fmtIntLiteralDec(.zero_usize)});
6548 } else try f.writeCValue(writer, operand, .Other);
6546 try w.writeByte('&');
6547 try f.writeCValueDeref(w, operand);
6548 try w.print("[{f}]", .{try f.fmtIntLiteralDec(.zero_usize)});
6549 } else try f.writeCValue(w, operand, .Other);
65496550 }
6550 try a.end(f, writer);
6551 try a.end(f, w);
65516552 }
65526553 {
6553 const a = try Assignment.start(f, writer, .usize);
6554 try f.writeCValueMember(writer, local, .{ .identifier = "len" });
6555 try a.assign(f, writer);
6556 try writer.print("{f}", .{
6554 const a = try Assignment.start(f, w, .usize);
6555 try f.writeCValueMember(w, local, .{ .identifier = "len" });
6556 try a.assign(f, w);
6557 try w.print("{f}", .{
65576558 try f.fmtIntLiteralDec(try pt.intValue(.usize, array_ty.arrayLen(zcu))),
65586559 });
6559 try a.end(f, writer);
6560 try a.end(f, w);
65606561 }
65616562
65626563 return local;
......@@ -6583,32 +6584,32 @@ fn airFloatCast(f: *Function, inst: Air.Inst.Index) !CValue {
65836584 else
65846585 unreachable;
65856586
6586 const writer = f.object.writer();
6587 const w = f.object.writer();
65876588 const local = try f.allocLocal(inst, inst_ty);
6588 const v = try Vectorize.start(f, inst, writer, operand_ty);
6589 const a = try Assignment.start(f, writer, try f.ctypeFromType(scalar_ty, .complete));
6590 try f.writeCValue(writer, local, .Other);
6591 try v.elem(f, writer);
6592 try a.assign(f, writer);
6589 const v = try Vectorize.start(f, inst, w, operand_ty);
6590 const a = try Assignment.start(f, w, try f.ctypeFromType(scalar_ty, .complete));
6591 try f.writeCValue(w, local, .Other);
6592 try v.elem(f, w);
6593 try a.assign(f, w);
65936594 if (inst_scalar_ty.isInt(zcu) and scalar_ty.isRuntimeFloat()) {
6594 try writer.writeAll("zig_wrap_");
6595 try f.object.dg.renderTypeForBuiltinFnName(writer, inst_scalar_ty);
6596 try writer.writeByte('(');
6597 }
6598 try writer.writeAll("zig_");
6599 try writer.writeAll(operation);
6600 try writer.writeAll(compilerRtAbbrev(scalar_ty, zcu, target));
6601 try writer.writeAll(compilerRtAbbrev(inst_scalar_ty, zcu, target));
6602 try writer.writeByte('(');
6603 try f.writeCValue(writer, operand, .FunctionArgument);
6604 try v.elem(f, writer);
6605 try writer.writeByte(')');
6595 try w.writeAll("zig_wrap_");
6596 try f.object.dg.renderTypeForBuiltinFnName(w, inst_scalar_ty);
6597 try w.writeByte('(');
6598 }
6599 try w.writeAll("zig_");
6600 try w.writeAll(operation);
6601 try w.writeAll(compilerRtAbbrev(scalar_ty, zcu, target));
6602 try w.writeAll(compilerRtAbbrev(inst_scalar_ty, zcu, target));
6603 try w.writeByte('(');
6604 try f.writeCValue(w, operand, .FunctionArgument);
6605 try v.elem(f, w);
6606 try w.writeByte(')');
66066607 if (inst_scalar_ty.isInt(zcu) and scalar_ty.isRuntimeFloat()) {
6607 try f.object.dg.renderBuiltinInfo(writer, inst_scalar_ty, .bits);
6608 try writer.writeByte(')');
6608 try f.object.dg.renderBuiltinInfo(w, inst_scalar_ty, .bits);
6609 try w.writeByte(')');
66096610 }
6610 try a.end(f, writer);
6611 try v.end(f, inst, writer);
6611 try a.end(f, w);
6612 try v.end(f, inst, w);
66126613
66136614 return local;
66146615}
......@@ -6633,27 +6634,27 @@ fn airUnBuiltinCall(
66336634 const inst_scalar_ctype = try f.ctypeFromType(inst_scalar_ty, .complete);
66346635 const ref_ret = inst_scalar_ctype.info(&f.object.dg.ctype_pool) == .array;
66356636
6636 const writer = f.object.writer();
6637 const w = f.object.writer();
66376638 const local = try f.allocLocal(inst, inst_ty);
6638 const v = try Vectorize.start(f, inst, writer, operand_ty);
6639 const v = try Vectorize.start(f, inst, w, operand_ty);
66396640 if (!ref_ret) {
6640 try f.writeCValue(writer, local, .Other);
6641 try v.elem(f, writer);
6642 try writer.writeAll(" = ");
6641 try f.writeCValue(w, local, .Other);
6642 try v.elem(f, w);
6643 try w.writeAll(" = ");
66436644 }
6644 try writer.print("zig_{s}_", .{operation});
6645 try f.object.dg.renderTypeForBuiltinFnName(writer, scalar_ty);
6646 try writer.writeByte('(');
6645 try w.print("zig_{s}_", .{operation});
6646 try f.object.dg.renderTypeForBuiltinFnName(w, scalar_ty);
6647 try w.writeByte('(');
66476648 if (ref_ret) {
6648 try f.writeCValue(writer, local, .FunctionArgument);
6649 try v.elem(f, writer);
6650 try writer.writeAll(", ");
6649 try f.writeCValue(w, local, .FunctionArgument);
6650 try v.elem(f, w);
6651 try w.writeAll(", ");
66516652 }
6652 try f.writeCValue(writer, operand, .FunctionArgument);
6653 try v.elem(f, writer);
6654 try f.object.dg.renderBuiltinInfo(writer, scalar_ty, info);
6655 try writer.writeAll(");\n");
6656 try v.end(f, inst, writer);
6653 try f.writeCValue(w, operand, .FunctionArgument);
6654 try v.elem(f, w);
6655 try f.object.dg.renderBuiltinInfo(w, scalar_ty, info);
6656 try w.writeAll(");\n");
6657 try v.end(f, inst, w);
66576658
66586659 return local;
66596660}
......@@ -6683,31 +6684,31 @@ fn airBinBuiltinCall(
66836684 const inst_scalar_ctype = try f.ctypeFromType(inst_scalar_ty, .complete);
66846685 const ref_ret = inst_scalar_ctype.info(&f.object.dg.ctype_pool) == .array;
66856686
6686 const writer = f.object.writer();
6687 const w = f.object.writer();
66876688 const local = try f.allocLocal(inst, inst_ty);
66886689 if (is_big) try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });
6689 const v = try Vectorize.start(f, inst, writer, operand_ty);
6690 const v = try Vectorize.start(f, inst, w, operand_ty);
66906691 if (!ref_ret) {
6691 try f.writeCValue(writer, local, .Other);
6692 try v.elem(f, writer);
6693 try writer.writeAll(" = ");
6692 try f.writeCValue(w, local, .Other);
6693 try v.elem(f, w);
6694 try w.writeAll(" = ");
66946695 }
6695 try writer.print("zig_{s}_", .{operation});
6696 try f.object.dg.renderTypeForBuiltinFnName(writer, scalar_ty);
6697 try writer.writeByte('(');
6696 try w.print("zig_{s}_", .{operation});
6697 try f.object.dg.renderTypeForBuiltinFnName(w, scalar_ty);
6698 try w.writeByte('(');
66986699 if (ref_ret) {
6699 try f.writeCValue(writer, local, .FunctionArgument);
6700 try v.elem(f, writer);
6701 try writer.writeAll(", ");
6702 }
6703 try f.writeCValue(writer, lhs, .FunctionArgument);
6704 try v.elem(f, writer);
6705 try writer.writeAll(", ");
6706 try f.writeCValue(writer, rhs, .FunctionArgument);
6707 if (f.typeOf(bin_op.rhs).isVector(zcu)) try v.elem(f, writer);
6708 try f.object.dg.renderBuiltinInfo(writer, scalar_ty, info);
6709 try writer.writeAll(");\n");
6710 try v.end(f, inst, writer);
6700 try f.writeCValue(w, local, .FunctionArgument);
6701 try v.elem(f, w);
6702 try w.writeAll(", ");
6703 }
6704 try f.writeCValue(w, lhs, .FunctionArgument);
6705 try v.elem(f, w);
6706 try w.writeAll(", ");
6707 try f.writeCValue(w, rhs, .FunctionArgument);
6708 if (f.typeOf(bin_op.rhs).isVector(zcu)) try v.elem(f, w);
6709 try f.object.dg.renderBuiltinInfo(w, scalar_ty, info);
6710 try w.writeAll(");\n");
6711 try v.end(f, inst, w);
67116712
67126713 return local;
67136714}
......@@ -6734,38 +6735,38 @@ fn airCmpBuiltinCall(
67346735 const inst_scalar_ctype = try f.ctypeFromType(inst_scalar_ty, .complete);
67356736 const ref_ret = inst_scalar_ctype.info(&f.object.dg.ctype_pool) == .array;
67366737
6737 const writer = f.object.writer();
6738 const w = f.object.writer();
67386739 const local = try f.allocLocal(inst, inst_ty);
6739 const v = try Vectorize.start(f, inst, writer, operand_ty);
6740 const v = try Vectorize.start(f, inst, w, operand_ty);
67406741 if (!ref_ret) {
6741 try f.writeCValue(writer, local, .Other);
6742 try v.elem(f, writer);
6743 try writer.writeAll(" = ");
6742 try f.writeCValue(w, local, .Other);
6743 try v.elem(f, w);
6744 try w.writeAll(" = ");
67446745 }
6745 try writer.print("zig_{s}_", .{switch (operation) {
6746 try w.print("zig_{s}_", .{switch (operation) {
67466747 else => @tagName(operation),
67476748 .operator => compareOperatorAbbrev(operator),
67486749 }});
6749 try f.object.dg.renderTypeForBuiltinFnName(writer, scalar_ty);
6750 try writer.writeByte('(');
6750 try f.object.dg.renderTypeForBuiltinFnName(w, scalar_ty);
6751 try w.writeByte('(');
67516752 if (ref_ret) {
6752 try f.writeCValue(writer, local, .FunctionArgument);
6753 try v.elem(f, writer);
6754 try writer.writeAll(", ");
6755 }
6756 try f.writeCValue(writer, lhs, .FunctionArgument);
6757 try v.elem(f, writer);
6758 try writer.writeAll(", ");
6759 try f.writeCValue(writer, rhs, .FunctionArgument);
6760 try v.elem(f, writer);
6761 try f.object.dg.renderBuiltinInfo(writer, scalar_ty, info);
6762 try writer.writeByte(')');
6763 if (!ref_ret) try writer.print("{s}{f}", .{
6753 try f.writeCValue(w, local, .FunctionArgument);
6754 try v.elem(f, w);
6755 try w.writeAll(", ");
6756 }
6757 try f.writeCValue(w, lhs, .FunctionArgument);
6758 try v.elem(f, w);
6759 try w.writeAll(", ");
6760 try f.writeCValue(w, rhs, .FunctionArgument);
6761 try v.elem(f, w);
6762 try f.object.dg.renderBuiltinInfo(w, scalar_ty, info);
6763 try w.writeByte(')');
6764 if (!ref_ret) try w.print("{s}{f}", .{
67646765 compareOperatorC(operator),
67656766 try f.fmtIntLiteralDec(try pt.intValue(.i32, 0)),
67666767 });
6767 try writer.writeAll(";\n");
6768 try v.end(f, inst, writer);
6768 try w.writeAll(";\n");
6769 try v.end(f, inst, w);
67696770
67706771 return local;
67716772}
......@@ -6783,7 +6784,7 @@ fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue
67836784 const ty = ptr_ty.childType(zcu);
67846785 const ctype = try f.ctypeFromType(ty, .complete);
67856786
6786 const writer = f.object.writer();
6787 const w = f.object.writer();
67876788 const new_value_mat = try Materialize.start(f, inst, ty, new_value);
67886789 try reap(f, inst, &.{ extra.ptr, extra.expected_value, extra.new_value });
67896790
......@@ -6795,76 +6796,76 @@ fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue
67956796 const local = try f.allocLocal(inst, inst_ty);
67966797 if (inst_ty.isPtrLikeOptional(zcu)) {
67976798 {
6798 const a = try Assignment.start(f, writer, ctype);
6799 try f.writeCValue(writer, local, .Other);
6800 try a.assign(f, writer);
6801 try f.writeCValue(writer, expected_value, .Other);
6802 try a.end(f, writer);
6799 const a = try Assignment.start(f, w, ctype);
6800 try f.writeCValue(w, local, .Other);
6801 try a.assign(f, w);
6802 try f.writeCValue(w, expected_value, .Other);
6803 try a.end(f, w);
68036804 }
68046805
6805 try writer.writeAll("if (");
6806 try writer.print("zig_cmpxchg_{s}((zig_atomic(", .{flavor});
6807 try f.renderType(writer, ty);
6808 try writer.writeByte(')');
6809 if (ptr_ty.isVolatilePtr(zcu)) try writer.writeAll(" volatile");
6810 try writer.writeAll(" *)");
6811 try f.writeCValue(writer, ptr, .Other);
6812 try writer.writeAll(", ");
6813 try f.writeCValue(writer, local, .FunctionArgument);
6814 try writer.writeAll(", ");
6815 try new_value_mat.mat(f, writer);
6816 try writer.writeAll(", ");
6817 try writeMemoryOrder(writer, extra.successOrder());
6818 try writer.writeAll(", ");
6819 try writeMemoryOrder(writer, extra.failureOrder());
6820 try writer.writeAll(", ");
6821 try f.object.dg.renderTypeForBuiltinFnName(writer, ty);
6822 try writer.writeAll(", ");
6823 try f.renderType(writer, repr_ty);
6824 try writer.writeByte(')');
6825 try writer.writeAll(") {\n");
6806 try w.writeAll("if (");
6807 try w.print("zig_cmpxchg_{s}((zig_atomic(", .{flavor});
6808 try f.renderType(w, ty);
6809 try w.writeByte(')');
6810 if (ptr_ty.isVolatilePtr(zcu)) try w.writeAll(" volatile");
6811 try w.writeAll(" *)");
6812 try f.writeCValue(w, ptr, .Other);
6813 try w.writeAll(", ");
6814 try f.writeCValue(w, local, .FunctionArgument);
6815 try w.writeAll(", ");
6816 try new_value_mat.mat(f, w);
6817 try w.writeAll(", ");
6818 try writeMemoryOrder(w, extra.successOrder());
6819 try w.writeAll(", ");
6820 try writeMemoryOrder(w, extra.failureOrder());
6821 try w.writeAll(", ");
6822 try f.object.dg.renderTypeForBuiltinFnName(w, ty);
6823 try w.writeAll(", ");
6824 try f.renderType(w, repr_ty);
6825 try w.writeByte(')');
6826 try w.writeAll(") {\n");
68266827 f.object.indent_writer.pushIndent();
68276828 {
6828 const a = try Assignment.start(f, writer, ctype);
6829 try f.writeCValue(writer, local, .Other);
6830 try a.assign(f, writer);
6831 try writer.writeAll("NULL");
6832 try a.end(f, writer);
6829 const a = try Assignment.start(f, w, ctype);
6830 try f.writeCValue(w, local, .Other);
6831 try a.assign(f, w);
6832 try w.writeAll("NULL");
6833 try a.end(f, w);
68336834 }
68346835 f.object.indent_writer.popIndent();
6835 try writer.writeAll("}\n");
6836 try w.writeAll("}\n");
68366837 } else {
68376838 {
6838 const a = try Assignment.start(f, writer, ctype);
6839 try f.writeCValueMember(writer, local, .{ .identifier = "payload" });
6840 try a.assign(f, writer);
6841 try f.writeCValue(writer, expected_value, .Other);
6842 try a.end(f, writer);
6839 const a = try Assignment.start(f, w, ctype);
6840 try f.writeCValueMember(w, local, .{ .identifier = "payload" });
6841 try a.assign(f, w);
6842 try f.writeCValue(w, expected_value, .Other);
6843 try a.end(f, w);
68436844 }
68446845 {
6845 const a = try Assignment.start(f, writer, .bool);
6846 try f.writeCValueMember(writer, local, .{ .identifier = "is_null" });
6847 try a.assign(f, writer);
6848 try writer.print("zig_cmpxchg_{s}((zig_atomic(", .{flavor});
6849 try f.renderType(writer, ty);
6850 try writer.writeByte(')');
6851 if (ptr_ty.isVolatilePtr(zcu)) try writer.writeAll(" volatile");
6852 try writer.writeAll(" *)");
6853 try f.writeCValue(writer, ptr, .Other);
6854 try writer.writeAll(", ");
6855 try f.writeCValueMember(writer, local, .{ .identifier = "payload" });
6856 try writer.writeAll(", ");
6857 try new_value_mat.mat(f, writer);
6858 try writer.writeAll(", ");
6859 try writeMemoryOrder(writer, extra.successOrder());
6860 try writer.writeAll(", ");
6861 try writeMemoryOrder(writer, extra.failureOrder());
6862 try writer.writeAll(", ");
6863 try f.object.dg.renderTypeForBuiltinFnName(writer, ty);
6864 try writer.writeAll(", ");
6865 try f.renderType(writer, repr_ty);
6866 try writer.writeByte(')');
6867 try a.end(f, writer);
6846 const a = try Assignment.start(f, w, .bool);
6847 try f.writeCValueMember(w, local, .{ .identifier = "is_null" });
6848 try a.assign(f, w);
6849 try w.print("zig_cmpxchg_{s}((zig_atomic(", .{flavor});
6850 try f.renderType(w, ty);
6851 try w.writeByte(')');
6852 if (ptr_ty.isVolatilePtr(zcu)) try w.writeAll(" volatile");
6853 try w.writeAll(" *)");
6854 try f.writeCValue(w, ptr, .Other);
6855 try w.writeAll(", ");
6856 try f.writeCValueMember(w, local, .{ .identifier = "payload" });
6857 try w.writeAll(", ");
6858 try new_value_mat.mat(f, w);
6859 try w.writeAll(", ");
6860 try writeMemoryOrder(w, extra.successOrder());
6861 try w.writeAll(", ");
6862 try writeMemoryOrder(w, extra.failureOrder());
6863 try w.writeAll(", ");
6864 try f.object.dg.renderTypeForBuiltinFnName(w, ty);
6865 try w.writeAll(", ");
6866 try f.renderType(w, repr_ty);
6867 try w.writeByte(')');
6868 try a.end(f, w);
68686869 }
68696870 }
68706871 try new_value_mat.end(f, inst);
......@@ -6888,7 +6889,7 @@ fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue {
68886889 const ptr = try f.resolveInst(pl_op.operand);
68896890 const operand = try f.resolveInst(extra.operand);
68906891
6891 const writer = f.object.writer();
6892 const w = f.object.writer();
68926893 const operand_mat = try Materialize.start(f, inst, ty, operand);
68936894 try reap(f, inst, &.{ pl_op.operand, extra.operand });
68946895
......@@ -6898,31 +6899,31 @@ fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue {
68986899 const repr_ty = if (is_float) pt.intType(.unsigned, repr_bits) catch unreachable else ty;
68996900
69006901 const local = try f.allocLocal(inst, inst_ty);
6901 try writer.print("zig_atomicrmw_{s}", .{toAtomicRmwSuffix(extra.op())});
6902 if (is_float) try writer.writeAll("_float") else if (is_128) try writer.writeAll("_int128");
6903 try writer.writeByte('(');
6904 try f.writeCValue(writer, local, .Other);
6905 try writer.writeAll(", (");
6902 try w.print("zig_atomicrmw_{s}", .{toAtomicRmwSuffix(extra.op())});
6903 if (is_float) try w.writeAll("_float") else if (is_128) try w.writeAll("_int128");
6904 try w.writeByte('(');
6905 try f.writeCValue(w, local, .Other);
6906 try w.writeAll(", (");
69066907 const use_atomic = switch (extra.op()) {
69076908 else => true,
69086909 // These are missing from stdatomic.h, so no atomic types unless a fallback is used.
69096910 .Nand, .Min, .Max => is_float or is_128,
69106911 };
6911 if (use_atomic) try writer.writeAll("zig_atomic(");
6912 try f.renderType(writer, ty);
6913 if (use_atomic) try writer.writeByte(')');
6914 if (ptr_ty.isVolatilePtr(zcu)) try writer.writeAll(" volatile");
6915 try writer.writeAll(" *)");
6916 try f.writeCValue(writer, ptr, .Other);
6917 try writer.writeAll(", ");
6918 try operand_mat.mat(f, writer);
6919 try writer.writeAll(", ");
6920 try writeMemoryOrder(writer, extra.ordering());
6921 try writer.writeAll(", ");
6922 try f.object.dg.renderTypeForBuiltinFnName(writer, ty);
6923 try writer.writeAll(", ");
6924 try f.renderType(writer, repr_ty);
6925 try writer.writeAll(");\n");
6912 if (use_atomic) try w.writeAll("zig_atomic(");
6913 try f.renderType(w, ty);
6914 if (use_atomic) try w.writeByte(')');
6915 if (ptr_ty.isVolatilePtr(zcu)) try w.writeAll(" volatile");
6916 try w.writeAll(" *)");
6917 try f.writeCValue(w, ptr, .Other);
6918 try w.writeAll(", ");
6919 try operand_mat.mat(f, w);
6920 try w.writeAll(", ");
6921 try writeMemoryOrder(w, extra.ordering());
6922 try w.writeAll(", ");
6923 try f.object.dg.renderTypeForBuiltinFnName(w, ty);
6924 try w.writeAll(", ");
6925 try f.renderType(w, repr_ty);
6926 try w.writeAll(");\n");
69266927 try operand_mat.end(f, inst);
69276928
69286929 if (f.liveness.isUnused(inst)) {
......@@ -6948,24 +6949,24 @@ fn airAtomicLoad(f: *Function, inst: Air.Inst.Index) !CValue {
69486949 ty;
69496950
69506951 const inst_ty = f.typeOfIndex(inst);
6951 const writer = f.object.writer();
6952 const w = f.object.writer();
69526953 const local = try f.allocLocal(inst, inst_ty);
69536954
6954 try writer.writeAll("zig_atomic_load(");
6955 try f.writeCValue(writer, local, .Other);
6956 try writer.writeAll(", (zig_atomic(");
6957 try f.renderType(writer, ty);
6958 try writer.writeByte(')');
6959 if (ptr_ty.isVolatilePtr(zcu)) try writer.writeAll(" volatile");
6960 try writer.writeAll(" *)");
6961 try f.writeCValue(writer, ptr, .Other);
6962 try writer.writeAll(", ");
6963 try writeMemoryOrder(writer, atomic_load.order);
6964 try writer.writeAll(", ");
6965 try f.object.dg.renderTypeForBuiltinFnName(writer, ty);
6966 try writer.writeAll(", ");
6967 try f.renderType(writer, repr_ty);
6968 try writer.writeAll(");\n");
6955 try w.writeAll("zig_atomic_load(");
6956 try f.writeCValue(w, local, .Other);
6957 try w.writeAll(", (zig_atomic(");
6958 try f.renderType(w, ty);
6959 try w.writeByte(')');
6960 if (ptr_ty.isVolatilePtr(zcu)) try w.writeAll(" volatile");
6961 try w.writeAll(" *)");
6962 try f.writeCValue(w, ptr, .Other);
6963 try w.writeAll(", ");
6964 try writeMemoryOrder(w, atomic_load.order);
6965 try w.writeAll(", ");
6966 try f.object.dg.renderTypeForBuiltinFnName(w, ty);
6967 try w.writeAll(", ");
6968 try f.renderType(w, repr_ty);
6969 try w.writeAll(");\n");
69696970
69706971 return local;
69716972}
......@@ -6979,7 +6980,7 @@ fn airAtomicStore(f: *Function, inst: Air.Inst.Index, order: [*:0]const u8) !CVa
69796980 const ptr = try f.resolveInst(bin_op.lhs);
69806981 const element = try f.resolveInst(bin_op.rhs);
69816982
6982 const writer = f.object.writer();
6983 const w = f.object.writer();
69836984 const element_mat = try Materialize.start(f, inst, ty, element);
69846985 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });
69856986
......@@ -6988,31 +6989,31 @@ fn airAtomicStore(f: *Function, inst: Air.Inst.Index, order: [*:0]const u8) !CVa
69886989 else
69896990 ty;
69906991
6991 try writer.writeAll("zig_atomic_store((zig_atomic(");
6992 try f.renderType(writer, ty);
6993 try writer.writeByte(')');
6994 if (ptr_ty.isVolatilePtr(zcu)) try writer.writeAll(" volatile");
6995 try writer.writeAll(" *)");
6996 try f.writeCValue(writer, ptr, .Other);
6997 try writer.writeAll(", ");
6998 try element_mat.mat(f, writer);
6999 try writer.print(", {s}, ", .{order});
7000 try f.object.dg.renderTypeForBuiltinFnName(writer, ty);
7001 try writer.writeAll(", ");
7002 try f.renderType(writer, repr_ty);
7003 try writer.writeAll(");\n");
6992 try w.writeAll("zig_atomic_store((zig_atomic(");
6993 try f.renderType(w, ty);
6994 try w.writeByte(')');
6995 if (ptr_ty.isVolatilePtr(zcu)) try w.writeAll(" volatile");
6996 try w.writeAll(" *)");
6997 try f.writeCValue(w, ptr, .Other);
6998 try w.writeAll(", ");
6999 try element_mat.mat(f, w);
7000 try w.print(", {s}, ", .{order});
7001 try f.object.dg.renderTypeForBuiltinFnName(w, ty);
7002 try w.writeAll(", ");
7003 try f.renderType(w, repr_ty);
7004 try w.writeAll(");\n");
70047005 try element_mat.end(f, inst);
70057006
70067007 return .none;
70077008}
70087009
7009fn writeSliceOrPtr(f: *Function, writer: anytype, ptr: CValue, ptr_ty: Type) !void {
7010fn writeSliceOrPtr(f: *Function, w: *Writer, ptr: CValue, ptr_ty: Type) !void {
70107011 const pt = f.object.dg.pt;
70117012 const zcu = pt.zcu;
70127013 if (ptr_ty.isSlice(zcu)) {
7013 try f.writeCValueMember(writer, ptr, .{ .identifier = "ptr" });
7014 try f.writeCValueMember(w, ptr, .{ .identifier = "ptr" });
70147015 } else {
7015 try f.writeCValue(writer, ptr, .FunctionArgument);
7016 try f.writeCValue(w, ptr, .FunctionArgument);
70167017 }
70177018}
70187019
......@@ -7026,7 +7027,7 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
70267027 const elem_ty = f.typeOf(bin_op.rhs);
70277028 const elem_abi_size = elem_ty.abiSize(zcu);
70287029 const val_is_undef = if (try f.air.value(bin_op.rhs, pt)) |val| val.isUndefDeep(zcu) else false;
7029 const writer = f.object.writer();
7030 const w = f.object.writer();
70307031
70317032 if (val_is_undef) {
70327033 if (!safety) {
......@@ -7034,24 +7035,24 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
70347035 return .none;
70357036 }
70367037
7037 try writer.writeAll("memset(");
7038 try w.writeAll("memset(");
70387039 switch (dest_ty.ptrSize(zcu)) {
70397040 .slice => {
7040 try f.writeCValueMember(writer, dest_slice, .{ .identifier = "ptr" });
7041 try writer.writeAll(", 0xaa, ");
7042 try f.writeCValueMember(writer, dest_slice, .{ .identifier = "len" });
7041 try f.writeCValueMember(w, dest_slice, .{ .identifier = "ptr" });
7042 try w.writeAll(", 0xaa, ");
7043 try f.writeCValueMember(w, dest_slice, .{ .identifier = "len" });
70437044 if (elem_abi_size > 1) {
7044 try writer.print(" * {d});\n", .{elem_abi_size});
7045 try w.print(" * {d});\n", .{elem_abi_size});
70457046 } else {
7046 try writer.writeAll(");\n");
7047 try w.writeAll(");\n");
70477048 }
70487049 },
70497050 .one => {
70507051 const array_ty = dest_ty.childType(zcu);
70517052 const len = array_ty.arrayLen(zcu) * elem_abi_size;
70527053
7053 try f.writeCValue(writer, dest_slice, .FunctionArgument);
7054 try writer.print(", 0xaa, {d});\n", .{len});
7054 try f.writeCValue(w, dest_slice, .FunctionArgument);
7055 try w.print(", 0xaa, {d});\n", .{len});
70557056 },
70567057 .many, .c => unreachable,
70577058 }
......@@ -7072,38 +7073,38 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
70727073
70737074 const index = try f.allocLocal(inst, .usize);
70747075
7075 try writer.writeAll("for (");
7076 try f.writeCValue(writer, index, .Other);
7077 try writer.writeAll(" = ");
7078 try f.object.dg.renderValue(writer, .zero_usize, .Other);
7079 try writer.writeAll("; ");
7080 try f.writeCValue(writer, index, .Other);
7081 try writer.writeAll(" != ");
7076 try w.writeAll("for (");
7077 try f.writeCValue(w, index, .Other);
7078 try w.writeAll(" = ");
7079 try f.object.dg.renderValue(w, .zero_usize, .Other);
7080 try w.writeAll("; ");
7081 try f.writeCValue(w, index, .Other);
7082 try w.writeAll(" != ");
70827083 switch (dest_ty.ptrSize(zcu)) {
70837084 .slice => {
7084 try f.writeCValueMember(writer, dest_slice, .{ .identifier = "len" });
7085 try f.writeCValueMember(w, dest_slice, .{ .identifier = "len" });
70857086 },
70867087 .one => {
70877088 const array_ty = dest_ty.childType(zcu);
7088 try writer.print("{d}", .{array_ty.arrayLen(zcu)});
7089 try w.print("{d}", .{array_ty.arrayLen(zcu)});
70897090 },
70907091 .many, .c => unreachable,
70917092 }
7092 try writer.writeAll("; ++");
7093 try f.writeCValue(writer, index, .Other);
7094 try writer.writeAll(") ");
7095
7096 const a = try Assignment.start(f, writer, try f.ctypeFromType(elem_ty, .complete));
7097 try writer.writeAll("((");
7098 try f.renderType(writer, elem_ptr_ty);
7099 try writer.writeByte(')');
7100 try writeSliceOrPtr(f, writer, dest_slice, dest_ty);
7101 try writer.writeAll(")[");
7102 try f.writeCValue(writer, index, .Other);
7103 try writer.writeByte(']');
7104 try a.assign(f, writer);
7105 try f.writeCValue(writer, value, .Other);
7106 try a.end(f, writer);
7093 try w.writeAll("; ++");
7094 try f.writeCValue(w, index, .Other);
7095 try w.writeAll(") ");
7096
7097 const a = try Assignment.start(f, w, try f.ctypeFromType(elem_ty, .complete));
7098 try w.writeAll("((");
7099 try f.renderType(w, elem_ptr_ty);
7100 try w.writeByte(')');
7101 try writeSliceOrPtr(f, w, dest_slice, dest_ty);
7102 try w.writeAll(")[");
7103 try f.writeCValue(w, index, .Other);
7104 try w.writeByte(']');
7105 try a.assign(f, w);
7106 try f.writeCValue(w, value, .Other);
7107 try a.end(f, w);
71077108
71087109 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });
71097110 try freeLocal(f, inst, index.new_local, null);
......@@ -7113,24 +7114,24 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
71137114
71147115 const bitcasted = try bitcast(f, .u8, value, elem_ty);
71157116
7116 try writer.writeAll("memset(");
7117 try w.writeAll("memset(");
71177118 switch (dest_ty.ptrSize(zcu)) {
71187119 .slice => {
7119 try f.writeCValueMember(writer, dest_slice, .{ .identifier = "ptr" });
7120 try writer.writeAll(", ");
7121 try f.writeCValue(writer, bitcasted, .FunctionArgument);
7122 try writer.writeAll(", ");
7123 try f.writeCValueMember(writer, dest_slice, .{ .identifier = "len" });
7124 try writer.writeAll(");\n");
7120 try f.writeCValueMember(w, dest_slice, .{ .identifier = "ptr" });
7121 try w.writeAll(", ");
7122 try f.writeCValue(w, bitcasted, .FunctionArgument);
7123 try w.writeAll(", ");
7124 try f.writeCValueMember(w, dest_slice, .{ .identifier = "len" });
7125 try w.writeAll(");\n");
71257126 },
71267127 .one => {
71277128 const array_ty = dest_ty.childType(zcu);
71287129 const len = array_ty.arrayLen(zcu) * elem_abi_size;
71297130
7130 try f.writeCValue(writer, dest_slice, .FunctionArgument);
7131 try writer.writeAll(", ");
7132 try f.writeCValue(writer, bitcasted, .FunctionArgument);
7133 try writer.print(", {d});\n", .{len});
7131 try f.writeCValue(w, dest_slice, .FunctionArgument);
7132 try w.writeAll(", ");
7133 try f.writeCValue(w, bitcasted, .FunctionArgument);
7134 try w.print(", {d});\n", .{len});
71347135 },
71357136 .many, .c => unreachable,
71367137 }
......@@ -7147,36 +7148,36 @@ fn airMemcpy(f: *Function, inst: Air.Inst.Index, function_paren: []const u8) !CV
71477148 const src_ptr = try f.resolveInst(bin_op.rhs);
71487149 const dest_ty = f.typeOf(bin_op.lhs);
71497150 const src_ty = f.typeOf(bin_op.rhs);
7150 const writer = f.object.writer();
7151 const w = f.object.writer();
71517152
71527153 if (dest_ty.ptrSize(zcu) != .one) {
7153 try writer.writeAll("if (");
7154 try writeArrayLen(f, writer, dest_ptr, dest_ty);
7155 try writer.writeAll(" != 0) ");
7156 }
7157 try writer.writeAll(function_paren);
7158 try writeSliceOrPtr(f, writer, dest_ptr, dest_ty);
7159 try writer.writeAll(", ");
7160 try writeSliceOrPtr(f, writer, src_ptr, src_ty);
7161 try writer.writeAll(", ");
7162 try writeArrayLen(f, writer, dest_ptr, dest_ty);
7163 try writer.writeAll(" * sizeof(");
7164 try f.renderType(writer, dest_ty.elemType2(zcu));
7165 try writer.writeAll("));\n");
7154 try w.writeAll("if (");
7155 try writeArrayLen(f, w, dest_ptr, dest_ty);
7156 try w.writeAll(" != 0) ");
7157 }
7158 try w.writeAll(function_paren);
7159 try writeSliceOrPtr(f, w, dest_ptr, dest_ty);
7160 try w.writeAll(", ");
7161 try writeSliceOrPtr(f, w, src_ptr, src_ty);
7162 try w.writeAll(", ");
7163 try writeArrayLen(f, w, dest_ptr, dest_ty);
7164 try w.writeAll(" * sizeof(");
7165 try f.renderType(w, dest_ty.elemType2(zcu));
7166 try w.writeAll("));\n");
71667167
71677168 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });
71687169 return .none;
71697170}
71707171
7171fn writeArrayLen(f: *Function, writer: ArrayListWriter, dest_ptr: CValue, dest_ty: Type) !void {
7172fn writeArrayLen(f: *Function, alw: ArrayListWriter, dest_ptr: CValue, dest_ty: Type) !void {
71727173 const pt = f.object.dg.pt;
71737174 const zcu = pt.zcu;
71747175 switch (dest_ty.ptrSize(zcu)) {
7175 .one => try writer.print("{f}", .{
7176 .one => try alw.print("{f}", .{
71767177 try f.fmtIntLiteralDec(try pt.intValue(.usize, dest_ty.childType(zcu).arrayLen(zcu))),
71777178 }),
71787179 .many, .c => unreachable,
7179 .slice => try f.writeCValueMember(writer, dest_ptr, .{ .identifier = "len" }),
7180 .slice => try f.writeCValueMember(alw, dest_ptr, .{ .identifier = "len" }),
71807181 }
71817182}
71827183
......@@ -7193,12 +7194,12 @@ fn airSetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue {
71937194 if (layout.tag_size == 0) return .none;
71947195 const tag_ty = union_ty.unionTagTypeSafety(zcu).?;
71957196
7196 const writer = f.object.writer();
7197 const a = try Assignment.start(f, writer, try f.ctypeFromType(tag_ty, .complete));
7198 try f.writeCValueDerefMember(writer, union_ptr, .{ .identifier = "tag" });
7199 try a.assign(f, writer);
7200 try f.writeCValue(writer, new_tag, .Other);
7201 try a.end(f, writer);
7197 const w = f.object.writer();
7198 const a = try Assignment.start(f, w, try f.ctypeFromType(tag_ty, .complete));
7199 try f.writeCValueDerefMember(w, union_ptr, .{ .identifier = "tag" });
7200 try a.assign(f, w);
7201 try f.writeCValue(w, new_tag, .Other);
7202 try a.end(f, w);
72027203 return .none;
72037204}
72047205
......@@ -7215,13 +7216,13 @@ fn airGetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue {
72157216 if (layout.tag_size == 0) return .none;
72167217
72177218 const inst_ty = f.typeOfIndex(inst);
7218 const writer = f.object.writer();
7219 const w = f.object.writer();
72197220 const local = try f.allocLocal(inst, inst_ty);
7220 const a = try Assignment.start(f, writer, try f.ctypeFromType(inst_ty, .complete));
7221 try f.writeCValue(writer, local, .Other);
7222 try a.assign(f, writer);
7223 try f.writeCValueMember(writer, operand, .{ .identifier = "tag" });
7224 try a.end(f, writer);
7221 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete));
7222 try f.writeCValue(w, local, .Other);
7223 try a.assign(f, w);
7224 try f.writeCValueMember(w, operand, .{ .identifier = "tag" });
7225 try a.end(f, w);
72257226 return local;
72267227}
72277228
......@@ -7233,14 +7234,14 @@ fn airTagName(f: *Function, inst: Air.Inst.Index) !CValue {
72337234 const operand = try f.resolveInst(un_op);
72347235 try reap(f, inst, &.{un_op});
72357236
7236 const writer = f.object.writer();
7237 const w = f.object.writer();
72377238 const local = try f.allocLocal(inst, inst_ty);
7238 try f.writeCValue(writer, local, .Other);
7239 try writer.print(" = {s}(", .{
7239 try f.writeCValue(w, local, .Other);
7240 try w.print(" = {s}(", .{
72407241 try f.getLazyFnName(.{ .tag_name = enum_ty.toIntern() }),
72417242 });
7242 try f.writeCValue(writer, operand, .Other);
7243 try writer.writeAll(");\n");
7243 try f.writeCValue(w, operand, .Other);
7244 try w.writeAll(");\n");
72447245
72457246 return local;
72467247}
......@@ -7248,16 +7249,16 @@ fn airTagName(f: *Function, inst: Air.Inst.Index) !CValue {
72487249fn airErrorName(f: *Function, inst: Air.Inst.Index) !CValue {
72497250 const un_op = f.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
72507251
7251 const writer = f.object.writer();
7252 const w = f.object.writer();
72527253 const inst_ty = f.typeOfIndex(inst);
72537254 const operand = try f.resolveInst(un_op);
72547255 try reap(f, inst, &.{un_op});
72557256 const local = try f.allocLocal(inst, inst_ty);
7256 try f.writeCValue(writer, local, .Other);
7257 try f.writeCValue(w, local, .Other);
72577258
7258 try writer.writeAll(" = zig_errorName[");
7259 try f.writeCValue(writer, operand, .Other);
7260 try writer.writeAll(" - 1];\n");
7259 try w.writeAll(" = zig_errorName[");
7260 try f.writeCValue(w, operand, .Other);
7261 try w.writeAll(" - 1];\n");
72617262 return local;
72627263}
72637264
......@@ -7272,16 +7273,16 @@ fn airSplat(f: *Function, inst: Air.Inst.Index) !CValue {
72727273 const inst_ty = f.typeOfIndex(inst);
72737274 const inst_scalar_ty = inst_ty.scalarType(zcu);
72747275
7275 const writer = f.object.writer();
7276 const w = f.object.writer();
72767277 const local = try f.allocLocal(inst, inst_ty);
7277 const v = try Vectorize.start(f, inst, writer, inst_ty);
7278 const a = try Assignment.start(f, writer, try f.ctypeFromType(inst_scalar_ty, .complete));
7279 try f.writeCValue(writer, local, .Other);
7280 try v.elem(f, writer);
7281 try a.assign(f, writer);
7282 try f.writeCValue(writer, operand, .Other);
7283 try a.end(f, writer);
7284 try v.end(f, inst, writer);
7278 const v = try Vectorize.start(f, inst, w, inst_ty);
7279 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_scalar_ty, .complete));
7280 try f.writeCValue(w, local, .Other);
7281 try v.elem(f, w);
7282 try a.assign(f, w);
7283 try f.writeCValue(w, operand, .Other);
7284 try a.end(f, w);
7285 try v.end(f, inst, w);
72857286
72867287 return local;
72877288}
......@@ -7297,22 +7298,22 @@ fn airSelect(f: *Function, inst: Air.Inst.Index) !CValue {
72977298
72987299 const inst_ty = f.typeOfIndex(inst);
72997300
7300 const writer = f.object.writer();
7301 const w = f.object.writer();
73017302 const local = try f.allocLocal(inst, inst_ty);
7302 const v = try Vectorize.start(f, inst, writer, inst_ty);
7303 try f.writeCValue(writer, local, .Other);
7304 try v.elem(f, writer);
7305 try writer.writeAll(" = ");
7306 try f.writeCValue(writer, pred, .Other);
7307 try v.elem(f, writer);
7308 try writer.writeAll(" ? ");
7309 try f.writeCValue(writer, lhs, .Other);
7310 try v.elem(f, writer);
7311 try writer.writeAll(" : ");
7312 try f.writeCValue(writer, rhs, .Other);
7313 try v.elem(f, writer);
7314 try writer.writeAll(";\n");
7315 try v.end(f, inst, writer);
7303 const v = try Vectorize.start(f, inst, w, inst_ty);
7304 try f.writeCValue(w, local, .Other);
7305 try v.elem(f, w);
7306 try w.writeAll(" = ");
7307 try f.writeCValue(w, pred, .Other);
7308 try v.elem(f, w);
7309 try w.writeAll(" ? ");
7310 try f.writeCValue(w, lhs, .Other);
7311 try v.elem(f, w);
7312 try w.writeAll(" : ");
7313 try f.writeCValue(w, rhs, .Other);
7314 try v.elem(f, w);
7315 try w.writeAll(";\n");
7316 try v.end(f, inst, w);
73167317
73177318 return local;
73187319}
......@@ -7326,24 +7327,24 @@ fn airShuffleOne(f: *Function, inst: Air.Inst.Index) !CValue {
73267327 const operand = try f.resolveInst(unwrapped.operand);
73277328 const inst_ty = unwrapped.result_ty;
73287329
7329 const writer = f.object.writer();
7330 const w = f.object.writer();
73307331 const local = try f.allocLocal(inst, inst_ty);
73317332 try reap(f, inst, &.{unwrapped.operand}); // local cannot alias operand
73327333 for (mask, 0..) |mask_elem, out_idx| {
7333 try f.writeCValue(writer, local, .Other);
7334 try writer.writeByte('[');
7335 try f.object.dg.renderValue(writer, try pt.intValue(.usize, out_idx), .Other);
7336 try writer.writeAll("] = ");
7334 try f.writeCValue(w, local, .Other);
7335 try w.writeByte('[');
7336 try f.object.dg.renderValue(w, try pt.intValue(.usize, out_idx), .Other);
7337 try w.writeAll("] = ");
73377338 switch (mask_elem.unwrap()) {
73387339 .elem => |src_idx| {
7339 try f.writeCValue(writer, operand, .Other);
7340 try writer.writeByte('[');
7341 try f.object.dg.renderValue(writer, try pt.intValue(.usize, src_idx), .Other);
7342 try writer.writeByte(']');
7340 try f.writeCValue(w, operand, .Other);
7341 try w.writeByte('[');
7342 try f.object.dg.renderValue(w, try pt.intValue(.usize, src_idx), .Other);
7343 try w.writeByte(']');
73437344 },
7344 .value => |val| try f.object.dg.renderValue(writer, .fromInterned(val), .Other),
7345 .value => |val| try f.object.dg.renderValue(w, .fromInterned(val), .Other),
73457346 }
7346 try writer.writeAll(";\n");
7347 try w.writeAll(";\n");
73477348 }
73487349
73497350 return local;
......@@ -7360,30 +7361,30 @@ fn airShuffleTwo(f: *Function, inst: Air.Inst.Index) !CValue {
73607361 const inst_ty = unwrapped.result_ty;
73617362 const elem_ty = inst_ty.childType(zcu);
73627363
7363 const writer = f.object.writer();
7364 const w = f.object.writer();
73647365 const local = try f.allocLocal(inst, inst_ty);
73657366 try reap(f, inst, &.{ unwrapped.operand_a, unwrapped.operand_b }); // local cannot alias operands
73667367 for (mask, 0..) |mask_elem, out_idx| {
7367 try f.writeCValue(writer, local, .Other);
7368 try writer.writeByte('[');
7369 try f.object.dg.renderValue(writer, try pt.intValue(.usize, out_idx), .Other);
7370 try writer.writeAll("] = ");
7368 try f.writeCValue(w, local, .Other);
7369 try w.writeByte('[');
7370 try f.object.dg.renderValue(w, try pt.intValue(.usize, out_idx), .Other);
7371 try w.writeAll("] = ");
73717372 switch (mask_elem.unwrap()) {
73727373 .a_elem => |src_idx| {
7373 try f.writeCValue(writer, operand_a, .Other);
7374 try writer.writeByte('[');
7375 try f.object.dg.renderValue(writer, try pt.intValue(.usize, src_idx), .Other);
7376 try writer.writeByte(']');
7374 try f.writeCValue(w, operand_a, .Other);
7375 try w.writeByte('[');
7376 try f.object.dg.renderValue(w, try pt.intValue(.usize, src_idx), .Other);
7377 try w.writeByte(']');
73777378 },
73787379 .b_elem => |src_idx| {
7379 try f.writeCValue(writer, operand_b, .Other);
7380 try writer.writeByte('[');
7381 try f.object.dg.renderValue(writer, try pt.intValue(.usize, src_idx), .Other);
7382 try writer.writeByte(']');
7380 try f.writeCValue(w, operand_b, .Other);
7381 try w.writeByte('[');
7382 try f.object.dg.renderValue(w, try pt.intValue(.usize, src_idx), .Other);
7383 try w.writeByte(']');
73837384 },
7384 .undef => try f.object.dg.renderUndefValue(writer, elem_ty, .Other),
7385 .undef => try f.object.dg.renderUndefValue(w, elem_ty, .Other),
73857386 }
7386 try writer.writeAll(";\n");
7387 try w.writeAll(";\n");
73877388 }
73887389
73897390 return local;
......@@ -7398,7 +7399,7 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue {
73987399 const operand = try f.resolveInst(reduce.operand);
73997400 try reap(f, inst, &.{reduce.operand});
74007401 const operand_ty = f.typeOf(reduce.operand);
7401 const writer = f.object.writer();
7402 const w = f.object.writer();
74027403
74037404 const use_operator = scalar_ty.bitSize(zcu) <= 64;
74047405 const op: union(enum) {
......@@ -7445,10 +7446,10 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue {
74457446 // }
74467447
74477448 const accum = try f.allocLocal(inst, scalar_ty);
7448 try f.writeCValue(writer, accum, .Other);
7449 try writer.writeAll(" = ");
7449 try f.writeCValue(w, accum, .Other);
7450 try w.writeAll(" = ");
74507451
7451 try f.object.dg.renderValue(writer, switch (reduce.operation) {
7452 try f.object.dg.renderValue(w, switch (reduce.operation) {
74527453 .Or, .Xor => switch (scalar_ty.zigTypeTag(zcu)) {
74537454 .bool => Value.false,
74547455 .int => try pt.intValue(scalar_ty, 0),
......@@ -7485,42 +7486,42 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue {
74857486 else => unreachable,
74867487 },
74877488 }, .Other);
7488 try writer.writeAll(";\n");
7489 try w.writeAll(";\n");
74897490
7490 const v = try Vectorize.start(f, inst, writer, operand_ty);
7491 try f.writeCValue(writer, accum, .Other);
7491 const v = try Vectorize.start(f, inst, w, operand_ty);
7492 try f.writeCValue(w, accum, .Other);
74927493 switch (op) {
74937494 .builtin => |func| {
7494 try writer.print(" = zig_{s}_", .{func.operation});
7495 try f.object.dg.renderTypeForBuiltinFnName(writer, scalar_ty);
7496 try writer.writeByte('(');
7497 try f.writeCValue(writer, accum, .FunctionArgument);
7498 try writer.writeAll(", ");
7499 try f.writeCValue(writer, operand, .Other);
7500 try v.elem(f, writer);
7501 try f.object.dg.renderBuiltinInfo(writer, scalar_ty, func.info);
7502 try writer.writeByte(')');
7495 try w.print(" = zig_{s}_", .{func.operation});
7496 try f.object.dg.renderTypeForBuiltinFnName(w, scalar_ty);
7497 try w.writeByte('(');
7498 try f.writeCValue(w, accum, .FunctionArgument);
7499 try w.writeAll(", ");
7500 try f.writeCValue(w, operand, .Other);
7501 try v.elem(f, w);
7502 try f.object.dg.renderBuiltinInfo(w, scalar_ty, func.info);
7503 try w.writeByte(')');
75037504 },
75047505 .infix => |ass| {
7505 try writer.writeAll(ass);
7506 try f.writeCValue(writer, operand, .Other);
7507 try v.elem(f, writer);
7506 try w.writeAll(ass);
7507 try f.writeCValue(w, operand, .Other);
7508 try v.elem(f, w);
75087509 },
75097510 .ternary => |cmp| {
7510 try writer.writeAll(" = ");
7511 try f.writeCValue(writer, accum, .Other);
7512 try writer.writeAll(cmp);
7513 try f.writeCValue(writer, operand, .Other);
7514 try v.elem(f, writer);
7515 try writer.writeAll(" ? ");
7516 try f.writeCValue(writer, accum, .Other);
7517 try writer.writeAll(" : ");
7518 try f.writeCValue(writer, operand, .Other);
7519 try v.elem(f, writer);
7511 try w.writeAll(" = ");
7512 try f.writeCValue(w, accum, .Other);
7513 try w.writeAll(cmp);
7514 try f.writeCValue(w, operand, .Other);
7515 try v.elem(f, w);
7516 try w.writeAll(" ? ");
7517 try f.writeCValue(w, accum, .Other);
7518 try w.writeAll(" : ");
7519 try f.writeCValue(w, operand, .Other);
7520 try v.elem(f, w);
75207521 },
75217522 }
7522 try writer.writeAll(";\n");
7523 try v.end(f, inst, writer);
7523 try w.writeAll(";\n");
7524 try v.end(f, inst, w);
75247525
75257526 return accum;
75267527}
......@@ -7546,7 +7547,7 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue {
75467547 }
75477548 }
75487549
7549 const writer = f.object.writer();
7550 const w = f.object.writer();
75507551 const local = try f.allocLocal(inst, inst_ty);
75517552 switch (ip.indexToKey(inst_ty.toIntern())) {
75527553 inline .array_type, .vector_type => |info, tag| {
......@@ -7554,20 +7555,20 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue {
75547555 .ctype = try f.ctypeFromType(.fromInterned(info.child), .complete),
75557556 };
75567557 for (resolved_elements, 0..) |element, i| {
7557 try a.restart(f, writer);
7558 try f.writeCValue(writer, local, .Other);
7559 try writer.print("[{d}]", .{i});
7560 try a.assign(f, writer);
7561 try f.writeCValue(writer, element, .Other);
7562 try a.end(f, writer);
7558 try a.restart(f, w);
7559 try f.writeCValue(w, local, .Other);
7560 try w.print("[{d}]", .{i});
7561 try a.assign(f, w);
7562 try f.writeCValue(w, element, .Other);
7563 try a.end(f, w);
75637564 }
75647565 if (tag == .array_type and info.sentinel != .none) {
7565 try a.restart(f, writer);
7566 try f.writeCValue(writer, local, .Other);
7567 try writer.print("[{d}]", .{info.len});
7568 try a.assign(f, writer);
7569 try f.object.dg.renderValue(writer, Value.fromInterned(info.sentinel), .Other);
7570 try a.end(f, writer);
7566 try a.restart(f, w);
7567 try f.writeCValue(w, local, .Other);
7568 try w.print("[{d}]", .{info.len});
7569 try a.assign(f, w);
7570 try f.object.dg.renderValue(w, Value.fromInterned(info.sentinel), .Other);
7571 try a.end(f, w);
75717572 }
75727573 },
75737574 .struct_type => {
......@@ -7579,19 +7580,19 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue {
75797580 const field_ty: Type = .fromInterned(loaded_struct.field_types.get(ip)[field_index]);
75807581 if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue;
75817582
7582 const a = try Assignment.start(f, writer, try f.ctypeFromType(field_ty, .complete));
7583 try f.writeCValueMember(writer, local, if (loaded_struct.fieldName(ip, field_index).unwrap()) |field_name|
7583 const a = try Assignment.start(f, w, try f.ctypeFromType(field_ty, .complete));
7584 try f.writeCValueMember(w, local, if (loaded_struct.fieldName(ip, field_index).unwrap()) |field_name|
75847585 .{ .identifier = field_name.toSlice(ip) }
75857586 else
75867587 .{ .field = field_index });
7587 try a.assign(f, writer);
7588 try f.writeCValue(writer, resolved_elements[field_index], .Other);
7589 try a.end(f, writer);
7588 try a.assign(f, w);
7589 try f.writeCValue(w, resolved_elements[field_index], .Other);
7590 try a.end(f, w);
75907591 }
75917592 },
75927593 .@"packed" => {
7593 try f.writeCValue(writer, local, .Other);
7594 try writer.writeAll(" = ");
7594 try f.writeCValue(w, local, .Other);
7595 try w.writeAll(" = ");
75957596
75967597 const backing_int_ty: Type = .fromInterned(loaded_struct.backingIntTypeUnordered(ip));
75977598 const int_info = backing_int_ty.intInfo(zcu);
......@@ -7607,9 +7608,9 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue {
76077608 if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue;
76087609
76097610 if (!empty) {
7610 try writer.writeAll("zig_or_");
7611 try f.object.dg.renderTypeForBuiltinFnName(writer, inst_ty);
7612 try writer.writeByte('(');
7611 try w.writeAll("zig_or_");
7612 try f.object.dg.renderTypeForBuiltinFnName(w, inst_ty);
7613 try w.writeByte('(');
76137614 }
76147615 empty = false;
76157616 }
......@@ -7619,57 +7620,57 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue {
76197620 const field_ty = inst_ty.fieldType(field_index, zcu);
76207621 if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue;
76217622
7622 if (!empty) try writer.writeAll(", ");
7623 if (!empty) try w.writeAll(", ");
76237624 // TODO: Skip this entire shift if val is 0?
7624 try writer.writeAll("zig_shlw_");
7625 try f.object.dg.renderTypeForBuiltinFnName(writer, inst_ty);
7626 try writer.writeByte('(');
7625 try w.writeAll("zig_shlw_");
7626 try f.object.dg.renderTypeForBuiltinFnName(w, inst_ty);
7627 try w.writeByte('(');
76277628
76287629 if (field_ty.isAbiInt(zcu)) {
7629 try writer.writeAll("zig_and_");
7630 try f.object.dg.renderTypeForBuiltinFnName(writer, inst_ty);
7631 try writer.writeByte('(');
7630 try w.writeAll("zig_and_");
7631 try f.object.dg.renderTypeForBuiltinFnName(w, inst_ty);
7632 try w.writeByte('(');
76327633 }
76337634
76347635 if (inst_ty.isAbiInt(zcu) and (field_ty.isAbiInt(zcu) or field_ty.isPtrAtRuntime(zcu))) {
7635 try f.renderIntCast(writer, inst_ty, element, .{}, field_ty, .FunctionArgument);
7636 try f.renderIntCast(w, inst_ty, element, .{}, field_ty, .FunctionArgument);
76367637 } else {
7637 try writer.writeByte('(');
7638 try f.renderType(writer, inst_ty);
7639 try writer.writeByte(')');
7638 try w.writeByte('(');
7639 try f.renderType(w, inst_ty);
7640 try w.writeByte(')');
76407641 if (field_ty.isPtrAtRuntime(zcu)) {
7641 try writer.writeByte('(');
7642 try f.renderType(writer, switch (int_info.signedness) {
7642 try w.writeByte('(');
7643 try f.renderType(w, switch (int_info.signedness) {
76437644 .unsigned => .usize,
76447645 .signed => .isize,
76457646 });
7646 try writer.writeByte(')');
7647 try w.writeByte(')');
76477648 }
7648 try f.writeCValue(writer, element, .Other);
7649 try f.writeCValue(w, element, .Other);
76497650 }
76507651
76517652 if (field_ty.isAbiInt(zcu)) {
7652 try writer.writeAll(", ");
7653 try w.writeAll(", ");
76537654 const field_int_info = field_ty.intInfo(zcu);
76547655 const field_mask = if (int_info.signedness == .signed and int_info.bits == field_int_info.bits)
76557656 try pt.intValue(backing_int_ty, -1)
76567657 else
76577658 try (try pt.intType(.unsigned, field_int_info.bits)).maxIntScalar(pt, backing_int_ty);
7658 try f.object.dg.renderValue(writer, field_mask, .FunctionArgument);
7659 try writer.writeByte(')');
7659 try f.object.dg.renderValue(w, field_mask, .FunctionArgument);
7660 try w.writeByte(')');
76607661 }
76617662
7662 try writer.print(", {f}", .{
7663 try w.print(", {f}", .{
76637664 try f.fmtIntLiteralDec(try pt.intValue(bit_offset_ty, bit_offset)),
76647665 });
7665 try f.object.dg.renderBuiltinInfo(writer, inst_ty, .bits);
7666 try writer.writeByte(')');
7667 if (!empty) try writer.writeByte(')');
7666 try f.object.dg.renderBuiltinInfo(w, inst_ty, .bits);
7667 try w.writeByte(')');
7668 if (!empty) try w.writeByte(')');
76687669
76697670 bit_offset += field_ty.bitSize(zcu);
76707671 empty = false;
76717672 }
7672 try writer.writeAll(";\n");
7673 try w.writeAll(";\n");
76737674 },
76747675 }
76757676 },
......@@ -7678,11 +7679,11 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue {
76787679 const field_ty: Type = .fromInterned(tuple_info.types.get(ip)[field_index]);
76797680 if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue;
76807681
7681 const a = try Assignment.start(f, writer, try f.ctypeFromType(field_ty, .complete));
7682 try f.writeCValueMember(writer, local, .{ .field = field_index });
7683 try a.assign(f, writer);
7684 try f.writeCValue(writer, resolved_elements[field_index], .Other);
7685 try a.end(f, writer);
7682 const a = try Assignment.start(f, w, try f.ctypeFromType(field_ty, .complete));
7683 try f.writeCValueMember(w, local, .{ .field = field_index });
7684 try a.assign(f, w);
7685 try f.writeCValue(w, resolved_elements[field_index], .Other);
7686 try a.end(f, w);
76867687 },
76877688 else => unreachable,
76887689 }
......@@ -7704,7 +7705,7 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue {
77047705 const payload = try f.resolveInst(extra.init);
77057706 try reap(f, inst, &.{extra.init});
77067707
7707 const writer = f.object.writer();
7708 const w = f.object.writer();
77087709 const local = try f.allocLocal(inst, union_ty);
77097710 if (loaded_union.flagsUnordered(ip).layout == .@"packed") return f.moveCValue(inst, union_ty, payload);
77107711
......@@ -7714,20 +7715,20 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue {
77147715 const field_index = tag_ty.enumFieldIndex(field_name, zcu).?;
77157716 const tag_val = try pt.enumValueFieldIndex(tag_ty, field_index);
77167717
7717 const a = try Assignment.start(f, writer, try f.ctypeFromType(tag_ty, .complete));
7718 try f.writeCValueMember(writer, local, .{ .identifier = "tag" });
7719 try a.assign(f, writer);
7720 try writer.print("{f}", .{try f.fmtIntLiteralDec(try tag_val.intFromEnum(tag_ty, pt))});
7721 try a.end(f, writer);
7718 const a = try Assignment.start(f, w, try f.ctypeFromType(tag_ty, .complete));
7719 try f.writeCValueMember(w, local, .{ .identifier = "tag" });
7720 try a.assign(f, w);
7721 try w.print("{f}", .{try f.fmtIntLiteralDec(try tag_val.intFromEnum(tag_ty, pt))});
7722 try a.end(f, w);
77227723 }
77237724 break :field .{ .payload_identifier = field_name.toSlice(ip) };
77247725 } else .{ .identifier = field_name.toSlice(ip) };
77257726
7726 const a = try Assignment.start(f, writer, try f.ctypeFromType(payload_ty, .complete));
7727 try f.writeCValueMember(writer, local, field);
7728 try a.assign(f, writer);
7729 try f.writeCValue(writer, payload, .Other);
7730 try a.end(f, writer);
7727 const a = try Assignment.start(f, w, try f.ctypeFromType(payload_ty, .complete));
7728 try f.writeCValueMember(w, local, field);
7729 try a.assign(f, w);
7730 try f.writeCValue(w, payload, .Other);
7731 try a.end(f, w);
77317732 return local;
77327733}
77337734
......@@ -7740,15 +7741,15 @@ fn airPrefetch(f: *Function, inst: Air.Inst.Index) !CValue {
77407741 const ptr = try f.resolveInst(prefetch.ptr);
77417742 try reap(f, inst, &.{prefetch.ptr});
77427743
7743 const writer = f.object.writer();
7744 const w = f.object.writer();
77447745 switch (prefetch.cache) {
77457746 .data => {
7746 try writer.writeAll("zig_prefetch(");
7747 try w.writeAll("zig_prefetch(");
77477748 if (ptr_ty.isSlice(zcu))
7748 try f.writeCValueMember(writer, ptr, .{ .identifier = "ptr" })
7749 try f.writeCValueMember(w, ptr, .{ .identifier = "ptr" })
77497750 else
7750 try f.writeCValue(writer, ptr, .FunctionArgument);
7751 try writer.print(", {d}, {d});\n", .{ @intFromEnum(prefetch.rw), prefetch.locality });
7751 try f.writeCValue(w, ptr, .FunctionArgument);
7752 try w.print(", {d}, {d});\n", .{ @intFromEnum(prefetch.rw), prefetch.locality });
77527753 },
77537754 // The available prefetch intrinsics do not accept a cache argument; only
77547755 // address, rw, and locality.
......@@ -7761,13 +7762,13 @@ fn airPrefetch(f: *Function, inst: Air.Inst.Index) !CValue {
77617762fn airWasmMemorySize(f: *Function, inst: Air.Inst.Index) !CValue {
77627763 const pl_op = f.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
77637764
7764 const writer = f.object.writer();
7765 const w = f.object.writer();
77657766 const inst_ty = f.typeOfIndex(inst);
77667767 const local = try f.allocLocal(inst, inst_ty);
7767 try f.writeCValue(writer, local, .Other);
7768 try f.writeCValue(w, local, .Other);
77687769
7769 try writer.writeAll(" = ");
7770 try writer.print("zig_wasm_memory_size({d});\n", .{pl_op.payload});
7770 try w.writeAll(" = ");
7771 try w.print("zig_wasm_memory_size({d});\n", .{pl_op.payload});
77717772
77727773 return local;
77737774}
......@@ -7775,17 +7776,17 @@ fn airWasmMemorySize(f: *Function, inst: Air.Inst.Index) !CValue {
77757776fn airWasmMemoryGrow(f: *Function, inst: Air.Inst.Index) !CValue {
77767777 const pl_op = f.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
77777778
7778 const writer = f.object.writer();
7779 const w = f.object.writer();
77797780 const inst_ty = f.typeOfIndex(inst);
77807781 const operand = try f.resolveInst(pl_op.operand);
77817782 try reap(f, inst, &.{pl_op.operand});
77827783 const local = try f.allocLocal(inst, inst_ty);
7783 try f.writeCValue(writer, local, .Other);
7784 try f.writeCValue(w, local, .Other);
77847785
7785 try writer.writeAll(" = ");
7786 try writer.print("zig_wasm_memory_grow({d}, ", .{pl_op.payload});
7787 try f.writeCValue(writer, operand, .FunctionArgument);
7788 try writer.writeAll(");\n");
7786 try w.writeAll(" = ");
7787 try w.print("zig_wasm_memory_grow({d}, ", .{pl_op.payload});
7788 try f.writeCValue(w, operand, .FunctionArgument);
7789 try w.writeAll(");\n");
77897790 return local;
77907791}
77917792
......@@ -7803,36 +7804,36 @@ fn airMulAdd(f: *Function, inst: Air.Inst.Index) !CValue {
78037804 const inst_ty = f.typeOfIndex(inst);
78047805 const inst_scalar_ty = inst_ty.scalarType(zcu);
78057806
7806 const writer = f.object.writer();
7807 const w = f.object.writer();
78077808 const local = try f.allocLocal(inst, inst_ty);
7808 const v = try Vectorize.start(f, inst, writer, inst_ty);
7809 try f.writeCValue(writer, local, .Other);
7810 try v.elem(f, writer);
7811 try writer.writeAll(" = zig_fma_");
7812 try f.object.dg.renderTypeForBuiltinFnName(writer, inst_scalar_ty);
7813 try writer.writeByte('(');
7814 try f.writeCValue(writer, mulend1, .FunctionArgument);
7815 try v.elem(f, writer);
7816 try writer.writeAll(", ");
7817 try f.writeCValue(writer, mulend2, .FunctionArgument);
7818 try v.elem(f, writer);
7819 try writer.writeAll(", ");
7820 try f.writeCValue(writer, addend, .FunctionArgument);
7821 try v.elem(f, writer);
7822 try writer.writeAll(");\n");
7823 try v.end(f, inst, writer);
7809 const v = try Vectorize.start(f, inst, w, inst_ty);
7810 try f.writeCValue(w, local, .Other);
7811 try v.elem(f, w);
7812 try w.writeAll(" = zig_fma_");
7813 try f.object.dg.renderTypeForBuiltinFnName(w, inst_scalar_ty);
7814 try w.writeByte('(');
7815 try f.writeCValue(w, mulend1, .FunctionArgument);
7816 try v.elem(f, w);
7817 try w.writeAll(", ");
7818 try f.writeCValue(w, mulend2, .FunctionArgument);
7819 try v.elem(f, w);
7820 try w.writeAll(", ");
7821 try f.writeCValue(w, addend, .FunctionArgument);
7822 try v.elem(f, w);
7823 try w.writeAll(");\n");
7824 try v.end(f, inst, w);
78247825
78257826 return local;
78267827}
78277828
78287829fn airRuntimeNavPtr(f: *Function, inst: Air.Inst.Index) !CValue {
78297830 const ty_nav = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_nav;
7830 const writer = f.object.writer();
7831 const w = f.object.writer();
78317832 const local = try f.allocLocal(inst, .fromInterned(ty_nav.ty));
7832 try f.writeCValue(writer, local, .Other);
7833 try writer.writeAll(" = ");
7834 try f.object.dg.renderNav(writer, ty_nav.nav, .Other);
7835 try writer.writeAll(";\n");
7833 try f.writeCValue(w, local, .Other);
7834 try w.writeAll(" = ");
7835 try f.object.dg.renderNav(w, ty_nav.nav, .Other);
7836 try w.writeAll(";\n");
78367837 return local;
78377838}
78387839
......@@ -7844,15 +7845,15 @@ fn airCVaStart(f: *Function, inst: Air.Inst.Index) !CValue {
78447845 const function_info = (try f.ctypeFromType(function_ty, .complete)).info(&f.object.dg.ctype_pool).function;
78457846 assert(function_info.varargs);
78467847
7847 const writer = f.object.writer();
7848 const w = f.object.writer();
78487849 const local = try f.allocLocal(inst, inst_ty);
7849 try writer.writeAll("va_start(*(va_list *)&");
7850 try f.writeCValue(writer, local, .Other);
7850 try w.writeAll("va_start(*(va_list *)&");
7851 try f.writeCValue(w, local, .Other);
78517852 if (function_info.param_ctypes.len > 0) {
7852 try writer.writeAll(", ");
7853 try f.writeCValue(writer, .{ .arg = function_info.param_ctypes.len - 1 }, .FunctionArgument);
7853 try w.writeAll(", ");
7854 try f.writeCValue(w, .{ .arg = function_info.param_ctypes.len - 1 }, .FunctionArgument);
78547855 }
7855 try writer.writeAll(");\n");
7856 try w.writeAll(");\n");
78567857 return local;
78577858}
78587859
......@@ -7863,14 +7864,14 @@ fn airCVaArg(f: *Function, inst: Air.Inst.Index) !CValue {
78637864 const va_list = try f.resolveInst(ty_op.operand);
78647865 try reap(f, inst, &.{ty_op.operand});
78657866
7866 const writer = f.object.writer();
7867 const w = f.object.writer();
78677868 const local = try f.allocLocal(inst, inst_ty);
7868 try f.writeCValue(writer, local, .Other);
7869 try writer.writeAll(" = va_arg(*(va_list *)");
7870 try f.writeCValue(writer, va_list, .Other);
7871 try writer.writeAll(", ");
7872 try f.renderType(writer, ty_op.ty.toType());
7873 try writer.writeAll(");\n");
7869 try f.writeCValue(w, local, .Other);
7870 try w.writeAll(" = va_arg(*(va_list *)");
7871 try f.writeCValue(w, va_list, .Other);
7872 try w.writeAll(", ");
7873 try f.renderType(w, ty_op.ty.toType());
7874 try w.writeAll(");\n");
78747875 return local;
78757876}
78767877
......@@ -7880,10 +7881,10 @@ fn airCVaEnd(f: *Function, inst: Air.Inst.Index) !CValue {
78807881 const va_list = try f.resolveInst(un_op);
78817882 try reap(f, inst, &.{un_op});
78827883
7883 const writer = f.object.writer();
7884 try writer.writeAll("va_end(*(va_list *)");
7885 try f.writeCValue(writer, va_list, .Other);
7886 try writer.writeAll(");\n");
7884 const w = f.object.writer();
7885 try w.writeAll("va_end(*(va_list *)");
7886 try f.writeCValue(w, va_list, .Other);
7887 try w.writeAll(");\n");
78877888 return .none;
78887889}
78897890
......@@ -7894,13 +7895,13 @@ fn airCVaCopy(f: *Function, inst: Air.Inst.Index) !CValue {
78947895 const va_list = try f.resolveInst(ty_op.operand);
78957896 try reap(f, inst, &.{ty_op.operand});
78967897
7897 const writer = f.object.writer();
7898 const w = f.object.writer();
78987899 const local = try f.allocLocal(inst, inst_ty);
7899 try writer.writeAll("va_copy(*(va_list *)&");
7900 try f.writeCValue(writer, local, .Other);
7901 try writer.writeAll(", *(va_list *)");
7902 try f.writeCValue(writer, va_list, .Other);
7903 try writer.writeAll(");\n");
7900 try w.writeAll("va_copy(*(va_list *)&");
7901 try f.writeCValue(w, local, .Other);
7902 try w.writeAll(", *(va_list *)");
7903 try f.writeCValue(w, va_list, .Other);
7904 try w.writeAll(");\n");
79047905 return local;
79057906}
79067907
......@@ -7915,7 +7916,7 @@ fn toMemoryOrder(order: std.builtin.AtomicOrder) [:0]const u8 {
79157916 };
79167917}
79177918
7918fn writeMemoryOrder(w: anytype, order: std.builtin.AtomicOrder) !void {
7919fn writeMemoryOrder(w: *Writer, order: std.builtin.AtomicOrder) !void {
79197920 return w.writeAll(toMemoryOrder(order));
79207921}
79217922
......@@ -8028,7 +8029,7 @@ fn IndentWriter(comptime UnderlyingWriter: type) type {
80288029 indent_count: usize = 0,
80298030 current_line_empty: bool = true,
80308031
8031 pub fn writer(self: *Self) Writer {
8032 pub fn w(self: *Self) Writer {
80328033 return .{ .context = .{
80338034 .context = self,
80348035 .writeFn = writeAny,
......@@ -8079,7 +8080,7 @@ fn IndentWriter(comptime UnderlyingWriter: type) type {
80798080
80808081/// A wrapper around `std.io.AnyWriter` that maintains a generic error set while
80818082/// erasing the rest of the implementation. This is intended to avoid duplicate
8082/// generic instantiations for writer types which share the same error set, while
8083/// generic instantiations for w types which share the same error set, while
80838084/// maintaining ease of error handling.
80848085fn ErrorOnlyGenericWriter(comptime Error: type) type {
80858086 return std.io.GenericWriter(std.io.AnyWriter, Error, struct {
......@@ -8160,12 +8161,12 @@ const StringLiteral = struct {
81608161 const max_char_len = 4;
81618162 const max_literal_len = @min(16380 - max_char_len, 4095);
81628163
8163 fn init(writer: *std.io.Writer, len: usize) StringLiteral {
8164 fn init(w: *std.io.Writer, len: usize) StringLiteral {
81648165 return .{
81658166 .cur_len = 0,
81668167 .len = len,
8167 .start_count = writer.count,
8168 .writer = writer,
8168 .start_count = w.count,
8169 .writer = w,
81698170 };
81708171 }
81718172
......@@ -8226,8 +8227,8 @@ const FormatStringContext = struct {
82268227 sentinel: ?u8,
82278228};
82288229
8229fn formatStringLiteral(data: FormatStringContext, writer: *std.io.Writer) std.io.Writer.Error!void {
8230 var literal: StringLiteral = .init(writer, data.str.len + @intFromBool(data.sentinel != null));
8230fn formatStringLiteral(data: FormatStringContext, w: *std.io.Writer) std.io.Writer.Error!void {
8231 var literal: StringLiteral = .init(w, data.str.len + @intFromBool(data.sentinel != null));
82318232 try literal.start();
82328233 for (data.str) |c| try literal.writeChar(c);
82338234 if (data.sentinel) |sentinel| if (sentinel != 0) try literal.writeChar(sentinel);
......@@ -8253,7 +8254,7 @@ const FormatIntLiteralContext = struct {
82538254 base: u8,
82548255 case: std.fmt.Case,
82558256};
8256fn formatIntLiteral(data: FormatIntLiteralContext, writer: *std.io.Writer) std.io.Writer.Error!void {
8257fn formatIntLiteral(data: FormatIntLiteralContext, w: *std.io.Writer) std.io.Writer.Error!void {
82578258 const pt = data.dg.pt;
82588259 const zcu = pt.zcu;
82598260 const target = &data.dg.mod.resolved_target.result;
......@@ -8333,28 +8334,28 @@ fn formatIntLiteral(data: FormatIntLiteralContext, writer: *std.io.Writer) std.i
83338334 if (c_limb_info.count == 1) {
83348335 if (wrap.addWrap(int, one, data.int_info.signedness, c_bits) or
83358336 data.int_info.signedness == .signed and wrap.subWrap(int, one, data.int_info.signedness, c_bits))
8336 return writer.print("{s}_{s}", .{
8337 data.ctype.getStandardDefineAbbrev() orelse return writer.print("zig_{s}Int_{c}{d}", .{
8337 return w.print("{s}_{s}", .{
8338 data.ctype.getStandardDefineAbbrev() orelse return w.print("zig_{s}Int_{c}{d}", .{
83388339 if (int.positive) "max" else "min", signAbbrev(data.int_info.signedness), c_bits,
83398340 }),
83408341 if (int.positive) "MAX" else "MIN",
83418342 });
83428343
8343 if (!int.positive) try writer.writeByte('-');
8344 try data.ctype.renderLiteralPrefix(writer, data.kind, ctype_pool);
8344 if (!int.positive) try w.writeByte('-');
8345 try data.ctype.renderLiteralPrefix(w, data.kind, ctype_pool);
83458346
83468347 switch (data.base) {
8347 2 => try writer.writeAll("0b"),
8348 8 => try writer.writeByte('0'),
8348 2 => try w.writeAll("0b"),
8349 8 => try w.writeByte('0'),
83498350 10 => {},
8350 16 => try writer.writeAll("0x"),
8351 16 => try w.writeAll("0x"),
83518352 else => unreachable,
83528353 }
83538354 const string = try oom(int.abs().toStringAlloc(allocator, data.base, data.case));
83548355 defer allocator.free(string);
8355 try writer.writeAll(string);
8356 try w.writeAll(string);
83568357 } else {
8357 try data.ctype.renderLiteralPrefix(writer, data.kind, ctype_pool);
8358 try data.ctype.renderLiteralPrefix(w, data.kind, ctype_pool);
83588359 wrap.truncate(int, .unsigned, c_bits);
83598360 @memset(wrap.limbs[wrap.len..], 0);
83608361 wrap.len = wrap.limbs.len;
......@@ -8397,7 +8398,7 @@ fn formatIntLiteral(data: FormatIntLiteralContext, writer: *std.io.Writer) std.i
83978398 c_limb_ctype = c_limb_info.ctype;
83988399 }
83998400
8400 if (limb_offset > 0) try writer.writeAll(", ");
8401 if (limb_offset > 0) try w.writeAll(", ");
84018402 try formatIntLiteral(.{
84028403 .dg = data.dg,
84038404 .int_info = c_limb_int_info,
......@@ -8406,10 +8407,10 @@ fn formatIntLiteral(data: FormatIntLiteralContext, writer: *std.io.Writer) std.i
84068407 .val = try oom(pt.intValue_big(.comptime_int, c_limb_mut.toConst())),
84078408 .base = data.base,
84088409 .case = data.case,
8409 }, writer);
8410 }, w);
84108411 }
84118412 }
8412 try data.ctype.renderLiteralSuffix(writer, ctype_pool);
8413 try data.ctype.renderLiteralSuffix(w, ctype_pool);
84138414}
84148415
84158416const Materialize = struct {
......@@ -8423,8 +8424,8 @@ const Materialize = struct {
84238424 } };
84248425 }
84258426
8426 pub fn mat(self: Materialize, f: *Function, writer: anytype) !void {
8427 try f.writeCValue(writer, self.local, .Other);
8427 pub fn mat(self: Materialize, f: *Function, w: *Writer) !void {
8428 try f.writeCValue(w, self.local, .Other);
84288429 }
84298430
84308431 pub fn end(self: Materialize, f: *Function, inst: Air.Inst.Index) !void {
......@@ -8435,36 +8436,36 @@ const Materialize = struct {
84358436const Assignment = struct {
84368437 ctype: CType,
84378438
8438 pub fn start(f: *Function, writer: anytype, ctype: CType) !Assignment {
8439 pub fn start(f: *Function, w: *Writer, ctype: CType) !Assignment {
84398440 const self: Assignment = .{ .ctype = ctype };
8440 try self.restart(f, writer);
8441 try self.restart(f, w);
84418442 return self;
84428443 }
84438444
8444 pub fn restart(self: Assignment, f: *Function, writer: anytype) !void {
8445 pub fn restart(self: Assignment, f: *Function, w: *Writer) !void {
84458446 switch (self.strategy(f)) {
84468447 .assign => {},
8447 .memcpy => try writer.writeAll("memcpy("),
8448 .memcpy => try w.writeAll("memcpy("),
84488449 }
84498450 }
84508451
8451 pub fn assign(self: Assignment, f: *Function, writer: anytype) !void {
8452 pub fn assign(self: Assignment, f: *Function, w: *Writer) !void {
84528453 switch (self.strategy(f)) {
8453 .assign => try writer.writeAll(" = "),
8454 .memcpy => try writer.writeAll(", "),
8454 .assign => try w.writeAll(" = "),
8455 .memcpy => try w.writeAll(", "),
84558456 }
84568457 }
84578458
8458 pub fn end(self: Assignment, f: *Function, writer: anytype) !void {
8459 pub fn end(self: Assignment, f: *Function, w: *Writer) !void {
84598460 switch (self.strategy(f)) {
84608461 .assign => {},
84618462 .memcpy => {
8462 try writer.writeAll(", sizeof(");
8463 try f.renderCType(writer, self.ctype);
8464 try writer.writeAll("))");
8463 try w.writeAll(", sizeof(");
8464 try f.renderCType(w, self.ctype);
8465 try w.writeAll("))");
84658466 },
84668467 }
8467 try writer.writeAll(";\n");
8468 try w.writeAll(";\n");
84688469 }
84698470
84708471 fn strategy(self: Assignment, f: *Function) enum { assign, memcpy } {
......@@ -8478,37 +8479,37 @@ const Assignment = struct {
84788479const Vectorize = struct {
84798480 index: CValue = .none,
84808481
8481 pub fn start(f: *Function, inst: Air.Inst.Index, writer: anytype, ty: Type) !Vectorize {
8482 pub fn start(f: *Function, inst: Air.Inst.Index, w: *Writer, ty: Type) !Vectorize {
84828483 const pt = f.object.dg.pt;
84838484 const zcu = pt.zcu;
84848485 return if (ty.zigTypeTag(zcu) == .vector) index: {
84858486 const local = try f.allocLocal(inst, .usize);
84868487
8487 try writer.writeAll("for (");
8488 try f.writeCValue(writer, local, .Other);
8489 try writer.print(" = {f}; ", .{try f.fmtIntLiteralDec(.zero_usize)});
8490 try f.writeCValue(writer, local, .Other);
8491 try writer.print(" < {f}; ", .{try f.fmtIntLiteralDec(try pt.intValue(.usize, ty.vectorLen(zcu)))});
8492 try f.writeCValue(writer, local, .Other);
8493 try writer.print(" += {f}) {{\n", .{try f.fmtIntLiteralDec(.one_usize)});
8488 try w.writeAll("for (");
8489 try f.writeCValue(w, local, .Other);
8490 try w.print(" = {f}; ", .{try f.fmtIntLiteralDec(.zero_usize)});
8491 try f.writeCValue(w, local, .Other);
8492 try w.print(" < {f}; ", .{try f.fmtIntLiteralDec(try pt.intValue(.usize, ty.vectorLen(zcu)))});
8493 try f.writeCValue(w, local, .Other);
8494 try w.print(" += {f}) {{\n", .{try f.fmtIntLiteralDec(.one_usize)});
84948495 f.object.indent_writer.pushIndent();
84958496
84968497 break :index .{ .index = local };
84978498 } else .{};
84988499 }
84998500
8500 pub fn elem(self: Vectorize, f: *Function, writer: anytype) !void {
8501 pub fn elem(self: Vectorize, f: *Function, w: *Writer) !void {
85018502 if (self.index != .none) {
8502 try writer.writeByte('[');
8503 try f.writeCValue(writer, self.index, .Other);
8504 try writer.writeByte(']');
8503 try w.writeByte('[');
8504 try f.writeCValue(w, self.index, .Other);
8505 try w.writeByte(']');
85058506 }
85068507 }
85078508
8508 pub fn end(self: Vectorize, f: *Function, inst: Air.Inst.Index, writer: anytype) !void {
8509 pub fn end(self: Vectorize, f: *Function, inst: Air.Inst.Index, w: *Writer) !void {
85098510 if (self.index != .none) {
85108511 f.object.indent_writer.popIndent();
8511 try writer.writeAll("}\n");
8512 try w.writeAll("}\n");
85128513 try freeLocal(f, inst, self.index.new_local, null);
85138514 }
85148515 }