authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-02 16:04:05-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-02 16:04:05-07:00
log69fd07bd50932405110584c32f6614fa1e247e1c
treeb044d81eb9a6fb7d1e20fe8b1a7dff6c62367681
parent1043c3e2ac55d9a560d18d47fcd9e7b709c8115b

sync


1 files changed, 1789 insertions(+), 1789 deletions(-)

src/codegen/c.zig+1789-1789
......@@ -345,23 +345,23 @@ fn isReservedIdent(ident: []const u8) bool {
345345
346346fn formatIdent(
347347 ident: []const u8,
348 bw: *Writer,
348 writer: *Writer,
349349 comptime fmt_str: []const u8,
350350) Writer.Error!void {
351351 const solo = fmt_str.len != 0 and fmt_str[0] == ' '; // space means solo; not part of a bigger ident.
352352 if (solo and isReservedIdent(ident)) {
353 try bw.writeAll("zig_e_");
353 try writer.writeAll("zig_e_");
354354 }
355355 for (ident, 0..) |c, i| {
356356 switch (c) {
357 'a'...'z', 'A'...'Z', '_' => try bw.writeByte(c),
358 '.' => try bw.writeByte('_'),
357 'a'...'z', 'A'...'Z', '_' => try writer.writeByte(c),
358 '.' => try writer.writeByte('_'),
359359 '0'...'9' => if (i == 0) {
360 try bw.print("_{x:2}", .{c});
360 try writer.print("_{x:2}", .{c});
361361 } else {
362 try bw.writeByte(c);
362 try writer.writeByte(c);
363363 },
364 else => try bw.print("_{x:2}", .{c}),
364 else => try writer.print("_{x:2}", .{c}),
365365 }
366366 }
367367}
......@@ -375,13 +375,13 @@ const CTypePoolStringFormatData = struct {
375375};
376376fn formatCTypePoolString(
377377 data: CTypePoolStringFormatData,
378 bw: *Writer,
378 writer: *Writer,
379379 comptime fmt_str: []const u8,
380380) Writer.Error!void {
381381 if (data.ctype_pool_string.toSlice(data.ctype_pool)) |slice|
382 try formatIdent(slice, bw, fmt_str)
382 try formatIdent(slice, writer, fmt_str)
383383 else
384 try bw.print("{f}", .{data.ctype_pool_string.fmt(data.ctype_pool)});
384 try writer.print("{f}", .{data.ctype_pool_string.fmt(data.ctype_pool)});
385385}
386386pub fn fmtCTypePoolString(
387387 ctype_pool_string: CType.Pool.String,
......@@ -441,18 +441,18 @@ pub const Function = struct {
441441 const ty = f.typeOf(ref);
442442
443443 const result: CValue = if (lowersToArray(ty, pt)) result: {
444 const ch = &f.object.code_header.buffered_writer;
444 const writer = &f.object.code_header.buffered_writer;
445445 const decl_c_value = try f.allocLocalValue(.{
446446 .ctype = try f.ctypeFromType(ty, .complete),
447447 .alignas = CType.AlignAs.fromAbiAlignment(ty.abiAlignment(pt.zcu)),
448448 });
449449 const gpa = f.object.dg.gpa;
450450 try f.allocs.put(gpa, decl_c_value.new_local, false);
451 try ch.writeAll("static ");
452 try f.object.dg.renderTypeAndName(ch, ty, decl_c_value, Const, .none, .complete);
453 try ch.writeAll(" = ");
454 try f.object.dg.renderValue(ch, val, .StaticInitializer);
455 try ch.writeAll(";\n ");
451 try writer.writeAll("static ");
452 try f.object.dg.renderTypeAndName(writer, ty, decl_c_value, Const, .none, .complete);
453 try writer.writeAll(" = ");
454 try f.object.dg.renderValue(writer, val, .StaticInitializer);
455 try writer.writeAll(";\n ");
456456 break :result .{ .local = decl_c_value.new_local };
457457 } else .{ .constant = val };
458458
......@@ -505,72 +505,72 @@ pub const Function = struct {
505505 return result;
506506 }
507507
508 fn writeCValue(f: *Function, bw: *Writer, c_value: CValue, location: ValueRenderLocation) !void {
508 fn writeCValue(f: *Function, w: *Writer, c_value: CValue, location: ValueRenderLocation) !void {
509509 switch (c_value) {
510510 .none => unreachable,
511 .new_local, .local => |i| try bw.print("t{d}", .{i}),
512 .local_ref => |i| try bw.print("&t{d}", .{i}),
513 .constant => |val| try f.object.dg.renderValue(bw, val, location),
514 .arg => |i| try bw.print("a{d}", .{i}),
515 .arg_array => |i| try f.writeCValueMember(bw, .{ .arg = i }, .{ .identifier = "array" }),
516 .undef => |ty| try f.object.dg.renderUndefValue(bw, ty, location),
517 else => try f.object.dg.writeCValue(bw, c_value),
511 .new_local, .local => |i| try w.print("t{d}", .{i}),
512 .local_ref => |i| try w.print("&t{d}", .{i}),
513 .constant => |val| try f.object.dg.renderValue(w, val, location),
514 .arg => |i| try w.print("a{d}", .{i}),
515 .arg_array => |i| try f.writeCValueMember(w, .{ .arg = i }, .{ .identifier = "array" }),
516 .undef => |ty| try f.object.dg.renderUndefValue(w, ty, location),
517 else => try f.object.dg.writeCValue(w, c_value),
518518 }
519519 }
520520
521 fn writeCValueDeref(f: *Function, bw: *Writer, c_value: CValue) !void {
521 fn writeCValueDeref(f: *Function, w: *Writer, c_value: CValue) !void {
522522 switch (c_value) {
523523 .none => unreachable,
524524 .new_local, .local, .constant => {
525 try bw.writeAll("(*");
526 try f.writeCValue(bw, c_value, .Other);
527 try bw.writeByte(')');
525 try w.writeAll("(*");
526 try f.writeCValue(w, c_value, .Other);
527 try w.writeByte(')');
528528 },
529 .local_ref => |i| try bw.print("t{d}", .{i}),
530 .arg => |i| try bw.print("(*a{d})", .{i}),
529 .local_ref => |i| try w.print("t{d}", .{i}),
530 .arg => |i| try w.print("(*a{d})", .{i}),
531531 .arg_array => |i| {
532 try bw.writeAll("(*");
533 try f.writeCValueMember(bw, .{ .arg = i }, .{ .identifier = "array" });
534 try bw.writeByte(')');
532 try w.writeAll("(*");
533 try f.writeCValueMember(w, .{ .arg = i }, .{ .identifier = "array" });
534 try w.writeByte(')');
535535 },
536 else => try f.object.dg.writeCValueDeref(bw, c_value),
536 else => try f.object.dg.writeCValueDeref(w, c_value),
537537 }
538538 }
539539
540540 fn writeCValueMember(
541541 f: *Function,
542 bw: *Writer,
542 w: *Writer,
543543 c_value: CValue,
544544 member: CValue,
545545 ) Error!void {
546546 switch (c_value) {
547547 .new_local, .local, .local_ref, .constant, .arg, .arg_array => {
548 try f.writeCValue(bw, c_value, .Other);
549 try bw.writeByte('.');
550 try f.writeCValue(bw, member, .Other);
548 try f.writeCValue(w, c_value, .Other);
549 try w.writeByte('.');
550 try f.writeCValue(w, member, .Other);
551551 },
552 else => return f.object.dg.writeCValueMember(bw, c_value, member),
552 else => return f.object.dg.writeCValueMember(w, c_value, member),
553553 }
554554 }
555555
556 fn writeCValueDerefMember(f: *Function, bw: *Writer, c_value: CValue, member: CValue) !void {
556 fn writeCValueDerefMember(f: *Function, w: *Writer, c_value: CValue, member: CValue) !void {
557557 switch (c_value) {
558558 .new_local, .local, .arg, .arg_array => {
559 try f.writeCValue(bw, c_value, .Other);
560 try bw.writeAll("->");
559 try f.writeCValue(w, c_value, .Other);
560 try w.writeAll("->");
561561 },
562562 .constant => {
563 try bw.writeByte('(');
564 try f.writeCValue(bw, c_value, .Other);
565 try bw.writeAll(")->");
563 try w.writeByte('(');
564 try f.writeCValue(w, c_value, .Other);
565 try w.writeAll(")->");
566566 },
567567 .local_ref => {
568 try f.writeCValueDeref(bw, c_value);
569 try bw.writeByte('.');
568 try f.writeCValueDeref(w, c_value);
569 try w.writeByte('.');
570570 },
571 else => return f.object.dg.writeCValueDerefMember(bw, c_value, member),
571 else => return f.object.dg.writeCValueDerefMember(w, c_value, member),
572572 }
573 try f.writeCValue(bw, member, .Other);
573 try f.writeCValue(w, member, .Other);
574574 }
575575
576576 fn fail(f: *Function, comptime format: []const u8, args: anytype) Error {
......@@ -585,16 +585,16 @@ pub const Function = struct {
585585 return f.object.dg.byteSize(ctype);
586586 }
587587
588 fn renderType(f: *Function, bw: *Writer, ctype: Type) !void {
589 return f.object.dg.renderType(bw, ctype);
588 fn renderType(f: *Function, w: *Writer, ctype: Type) !void {
589 return f.object.dg.renderType(w, ctype);
590590 }
591591
592 fn renderCType(f: *Function, bw: *Writer, ctype: CType) !void {
593 return f.object.dg.renderCType(bw, ctype);
592 fn renderCType(f: *Function, w: *Writer, ctype: CType) !void {
593 return f.object.dg.renderCType(w, ctype);
594594 }
595595
596 fn renderIntCast(f: *Function, bw: *Writer, dest_ty: Type, src: CValue, v: Vectorize, src_ty: Type, location: ValueRenderLocation) !void {
597 return f.object.dg.renderIntCast(bw, dest_ty, .{ .c_value = .{ .f = f, .value = src, .v = v } }, src_ty, location);
596 fn renderIntCast(f: *Function, w: *Writer, dest_ty: Type, src: CValue, v: Vectorize, src_ty: Type, location: ValueRenderLocation) !void {
597 return f.object.dg.renderIntCast(w, dest_ty, .{ .c_value = .{ .f = f, .value = src, .v = v } }, src_ty, location);
598598 }
599599
600600 fn fmtIntLiteral(f: *Function, val: Value) !std.fmt.Formatter(formatIntLiteral) {
......@@ -660,12 +660,12 @@ pub const Function = struct {
660660 },
661661 else => {},
662662 }
663 const bw = &f.object.code.buffered_writer;
664 const a = try Assignment.start(f, bw, ctype);
665 try f.writeCValue(bw, dst, .Other);
666 try a.assign(f, bw);
667 try f.writeCValue(bw, src, .Other);
668 try a.end(f, bw);
663 const w = &f.object.code.buffered_writer;
664 const a = try Assignment.start(f, w, ctype);
665 try f.writeCValue(w, dst, .Other);
666 try a.assign(f, w);
667 try f.writeCValue(w, src, .Other);
668 try a.end(f, w);
669669 }
670670
671671 fn moveCValue(f: *Function, inst: Air.Inst.Index, ty: Type, src: CValue) !CValue {
......@@ -702,9 +702,9 @@ pub const Object = struct {
702702 const indent_char = ' ';
703703
704704 fn newline(o: *Object) !void {
705 const bw = &o.code.buffered_writer;
706 try bw.writeByte('\n');
707 try bw.splatByteAll(indent_char, o.indent_counter);
705 const w = &o.code.buffered_writer;
706 try w.writeByte('\n');
707 try w.splatByteAll(indent_char, o.indent_counter);
708708 }
709709 fn indent(o: *Object) void {
710710 o.indent_counter += indent_width;
......@@ -758,7 +758,7 @@ pub const DeclGen = struct {
758758
759759 fn renderUav(
760760 dg: *DeclGen,
761 bw: *Writer,
761 w: *Writer,
762762 uav: InternPool.Key.Ptr.BaseAddr.Uav,
763763 location: ValueRenderLocation,
764764 ) Error!void {
......@@ -772,14 +772,14 @@ pub const DeclGen = struct {
772772 // Render an undefined pointer if we have a pointer to a zero-bit or comptime type.
773773 const ptr_ty: Type = .fromInterned(uav.orig_ty);
774774 if (ptr_ty.isPtrAtRuntime(zcu) and !uav_ty.isFnOrHasRuntimeBits(zcu)) {
775 return dg.writeCValue(bw, .{ .undef = ptr_ty });
775 return dg.writeCValue(w, .{ .undef = ptr_ty });
776776 }
777777
778778 // Chase function values in order to be able to reference the original function.
779779 switch (ip.indexToKey(uav.val)) {
780780 .variable => unreachable,
781 .func => |func| return dg.renderNav(bw, func.owner_nav, location),
782 .@"extern" => |@"extern"| return dg.renderNav(bw, @"extern".owner_nav, location),
781 .func => |func| return dg.renderNav(w, func.owner_nav, location),
782 .@"extern" => |@"extern"| return dg.renderNav(w, @"extern".owner_nav, location),
783783 else => {},
784784 }
785785
......@@ -793,13 +793,13 @@ pub const DeclGen = struct {
793793 const need_cast = !elem_ctype.eql(uav_ctype) and
794794 (elem_ctype.info(ctype_pool) != .function or uav_ctype.info(ctype_pool) != .function);
795795 if (need_cast) {
796 try bw.writeAll("((");
797 try dg.renderCType(bw, ptr_ctype);
798 try bw.writeByte(')');
796 try w.writeAll("((");
797 try dg.renderCType(w, ptr_ctype);
798 try w.writeByte(')');
799799 }
800 try bw.writeByte('&');
801 try renderUavName(bw, uav_val);
802 if (need_cast) try bw.writeByte(')');
800 try w.writeByte('&');
801 try renderUavName(w, uav_val);
802 if (need_cast) try w.writeByte(')');
803803
804804 // Indicate that the anon decl should be rendered to the output so that
805805 // our reference above is not undefined.
......@@ -820,7 +820,7 @@ pub const DeclGen = struct {
820820
821821 fn renderNav(
822822 dg: *DeclGen,
823 bw: *Writer,
823 w: *Writer,
824824 nav_index: InternPool.Nav.Index,
825825 location: ValueRenderLocation,
826826 ) Error!void {
......@@ -845,7 +845,7 @@ pub const DeclGen = struct {
845845 const nav_ty: Type = .fromInterned(ip.getNav(owner_nav).typeOf(ip));
846846 const ptr_ty = try pt.navPtrType(owner_nav);
847847 if (!nav_ty.isFnOrHasRuntimeBits(zcu)) {
848 return dg.writeCValue(bw, .{ .undef = ptr_ty });
848 return dg.writeCValue(w, .{ .undef = ptr_ty });
849849 }
850850
851851 // We shouldn't cast C function pointers as this is UB (when you call
......@@ -858,18 +858,18 @@ pub const DeclGen = struct {
858858 const need_cast = !elem_ctype.eql(nav_ctype) and
859859 (elem_ctype.info(ctype_pool) != .function or nav_ctype.info(ctype_pool) != .function);
860860 if (need_cast) {
861 try bw.writeAll("((");
862 try dg.renderCType(bw, ctype);
863 try bw.writeByte(')');
861 try w.writeAll("((");
862 try dg.renderCType(w, ctype);
863 try w.writeByte(')');
864864 }
865 try bw.writeByte('&');
866 try dg.renderNavName(bw, owner_nav);
867 if (need_cast) try bw.writeByte(')');
865 try w.writeByte('&');
866 try dg.renderNavName(w, owner_nav);
867 if (need_cast) try w.writeByte(')');
868868 }
869869
870870 fn renderPointer(
871871 dg: *DeclGen,
872 bw: *Writer,
872 w: *Writer,
873873 derivation: Value.PointerDeriveStep,
874874 location: ValueRenderLocation,
875875 ) Error!void {
......@@ -880,18 +880,18 @@ pub const DeclGen = struct {
880880 .int => |int| {
881881 const ptr_ctype = try dg.ctypeFromType(int.ptr_ty, .complete);
882882 const addr_val = try pt.intValue(.usize, int.addr);
883 try bw.writeByte('(');
884 try dg.renderCType(bw, ptr_ctype);
885 try bw.print("){fx}", .{try dg.fmtIntLiteral(addr_val, .Other)});
883 try w.writeByte('(');
884 try dg.renderCType(w, ptr_ctype);
885 try w.print("){fx}", .{try dg.fmtIntLiteral(addr_val, .Other)});
886886 },
887887
888 .nav_ptr => |nav| try dg.renderNav(bw, nav, location),
889 .uav_ptr => |uav| try dg.renderUav(bw, uav, location),
888 .nav_ptr => |nav| try dg.renderNav(w, nav, location),
889 .uav_ptr => |uav| try dg.renderUav(w, uav, location),
890890
891891 inline .eu_payload_ptr, .opt_payload_ptr => |info| {
892 try bw.writeAll("&(");
893 try dg.renderPointer(bw, info.parent.*, location);
894 try bw.writeAll(")->payload");
892 try w.writeAll("&(");
893 try dg.renderPointer(w, info.parent.*, location);
894 try w.writeAll(")->payload");
895895 },
896896
897897 .field_ptr => |field| {
......@@ -903,26 +903,26 @@ pub const DeclGen = struct {
903903 switch (fieldLocation(parent_ptr_ty, field.result_ptr_ty, field.field_idx, pt)) {
904904 .begin => {
905905 const ptr_ctype = try dg.ctypeFromType(field.result_ptr_ty, .complete);
906 try bw.writeByte('(');
907 try dg.renderCType(bw, ptr_ctype);
908 try bw.writeByte(')');
909 try dg.renderPointer(bw, field.parent.*, location);
906 try w.writeByte('(');
907 try dg.renderCType(w, ptr_ctype);
908 try w.writeByte(')');
909 try dg.renderPointer(w, field.parent.*, location);
910910 },
911911 .field => |name| {
912 try bw.writeAll("&(");
913 try dg.renderPointer(bw, field.parent.*, location);
914 try bw.writeAll(")->");
915 try dg.writeCValue(bw, name);
912 try w.writeAll("&(");
913 try dg.renderPointer(w, field.parent.*, location);
914 try w.writeAll(")->");
915 try dg.writeCValue(w, name);
916916 },
917917 .byte_offset => |byte_offset| {
918918 const ptr_ctype = try dg.ctypeFromType(field.result_ptr_ty, .complete);
919 try bw.writeByte('(');
920 try dg.renderCType(bw, ptr_ctype);
921 try bw.writeByte(')');
919 try w.writeByte('(');
920 try dg.renderCType(w, ptr_ctype);
921 try w.writeByte(')');
922922 const offset_val = try pt.intValue(.usize, byte_offset);
923 try bw.writeAll("((char *)");
924 try dg.renderPointer(bw, field.parent.*, location);
925 try bw.print(" + {f})", .{try dg.fmtIntLiteral(offset_val, .Other)});
923 try w.writeAll("((char *)");
924 try dg.renderPointer(w, field.parent.*, location);
925 try w.print(" + {f})", .{try dg.fmtIntLiteral(offset_val, .Other)});
926926 },
927927 }
928928 },
......@@ -930,10 +930,10 @@ pub const DeclGen = struct {
930930 .elem_ptr => |elem| if (!(try elem.parent.ptrType(pt)).childType(zcu).hasRuntimeBits(zcu)) {
931931 // Element type is zero-bit, so lowers to `void`. The index is irrelevant; just cast the pointer.
932932 const ptr_ctype = try dg.ctypeFromType(elem.result_ptr_ty, .complete);
933 try bw.writeByte('(');
934 try dg.renderCType(bw, ptr_ctype);
935 try bw.writeByte(')');
936 try dg.renderPointer(bw, elem.parent.*, location);
933 try w.writeByte('(');
934 try dg.renderCType(w, ptr_ctype);
935 try w.writeByte(')');
936 try dg.renderPointer(w, elem.parent.*, location);
937937 } else {
938938 const index_val = try pt.intValue(.usize, elem.elem_idx);
939939 // We want to do pointer arithmetic on a pointer to the element type.
......@@ -942,39 +942,39 @@ pub const DeclGen = struct {
942942 const parent_ctype = try dg.ctypeFromType(try elem.parent.ptrType(pt), .complete);
943943 if (result_ctype.eql(parent_ctype)) {
944944 // The pointer already has an appropriate type - just do the arithmetic.
945 try bw.writeByte('(');
946 try dg.renderPointer(bw, elem.parent.*, location);
947 try bw.print(" + {f})", .{try dg.fmtIntLiteral(index_val, .Other)});
945 try w.writeByte('(');
946 try dg.renderPointer(w, elem.parent.*, location);
947 try w.print(" + {f})", .{try dg.fmtIntLiteral(index_val, .Other)});
948948 } else {
949949 // We probably have an array pointer `T (*)[n]`. Cast to an element pointer,
950950 // and *then* apply the index.
951 try bw.writeAll("((");
952 try dg.renderCType(bw, result_ctype);
953 try bw.writeByte(')');
954 try dg.renderPointer(bw, elem.parent.*, location);
955 try bw.print(" + {f})", .{try dg.fmtIntLiteral(index_val, .Other)});
951 try w.writeAll("((");
952 try dg.renderCType(w, result_ctype);
953 try w.writeByte(')');
954 try dg.renderPointer(w, elem.parent.*, location);
955 try w.print(" + {f})", .{try dg.fmtIntLiteral(index_val, .Other)});
956956 }
957957 },
958958
959959 .offset_and_cast => |oac| {
960960 const ptr_ctype = try dg.ctypeFromType(oac.new_ptr_ty, .complete);
961 try bw.writeByte('(');
962 try dg.renderCType(bw, ptr_ctype);
963 try bw.writeByte(')');
961 try w.writeByte('(');
962 try dg.renderCType(w, ptr_ctype);
963 try w.writeByte(')');
964964 if (oac.byte_offset == 0) {
965 try dg.renderPointer(bw, oac.parent.*, location);
965 try dg.renderPointer(w, oac.parent.*, location);
966966 } else {
967967 const offset_val = try pt.intValue(.usize, oac.byte_offset);
968 try bw.writeAll("((char *)");
969 try dg.renderPointer(bw, oac.parent.*, location);
970 try bw.print(" + {f})", .{try dg.fmtIntLiteral(offset_val, .Other)});
968 try w.writeAll("((char *)");
969 try dg.renderPointer(w, oac.parent.*, location);
970 try w.print(" + {f})", .{try dg.fmtIntLiteral(offset_val, .Other)});
971971 }
972972 },
973973 }
974974 }
975975
976 fn renderErrorName(dg: *DeclGen, bw: *Writer, err_name: InternPool.NullTerminatedString) !void {
977 try bw.print("zig_error_{f}", .{fmtIdent(err_name.toSlice(&dg.pt.zcu.intern_pool))});
976 fn renderErrorName(dg: *DeclGen, w: *Writer, err_name: InternPool.NullTerminatedString) !void {
977 try w.print("zig_error_{f}", .{fmtIdent(err_name.toSlice(&dg.pt.zcu.intern_pool))});
978978 }
979979
980980 fn renderValue(
......@@ -1588,7 +1588,7 @@ pub const DeclGen = struct {
15881588
15891589 fn renderUndefValue(
15901590 dg: *DeclGen,
1591 bw: *Writer,
1591 w: *Writer,
15921592 ty: Type,
15931593 location: ValueRenderLocation,
15941594 ) Error!void {
......@@ -1621,57 +1621,57 @@ pub const DeclGen = struct {
16211621 // All unsigned ints matching float types are pre-allocated.
16221622 const repr_ty = dg.pt.intType(.unsigned, bits) catch unreachable;
16231623
1624 try bw.writeAll("zig_make_");
1625 try dg.renderTypeForBuiltinFnName(bw, ty);
1626 try bw.writeByte('(');
1624 try w.writeAll("zig_make_");
1625 try dg.renderTypeForBuiltinFnName(w, ty);
1626 try w.writeByte('(');
16271627 switch (bits) {
1628 16 => try bw.print("{x}", .{@as(f16, @bitCast(undefPattern(i16)))}),
1629 32 => try bw.print("{x}", .{@as(f32, @bitCast(undefPattern(i32)))}),
1630 64 => try bw.print("{x}", .{@as(f64, @bitCast(undefPattern(i64)))}),
1631 80 => try bw.print("{x}", .{@as(f80, @bitCast(undefPattern(i80)))}),
1632 128 => try bw.print("{x}", .{@as(f128, @bitCast(undefPattern(i128)))}),
1628 16 => try w.print("{x}", .{@as(f16, @bitCast(undefPattern(i16)))}),
1629 32 => try w.print("{x}", .{@as(f32, @bitCast(undefPattern(i32)))}),
1630 64 => try w.print("{x}", .{@as(f64, @bitCast(undefPattern(i64)))}),
1631 80 => try w.print("{x}", .{@as(f80, @bitCast(undefPattern(i80)))}),
1632 128 => try w.print("{x}", .{@as(f128, @bitCast(undefPattern(i128)))}),
16331633 else => unreachable,
16341634 }
1635 try bw.writeAll(", ");
1636 try dg.renderUndefValue(bw, repr_ty, .FunctionArgument);
1637 return bw.writeByte(')');
1635 try w.writeAll(", ");
1636 try dg.renderUndefValue(w, repr_ty, .FunctionArgument);
1637 return w.writeByte(')');
16381638 },
1639 .bool_type => try bw.writeAll(if (safety_on) "0xaa" else "false"),
1639 .bool_type => try w.writeAll(if (safety_on) "0xaa" else "false"),
16401640 else => switch (ip.indexToKey(ty.toIntern())) {
16411641 .simple_type,
16421642 .int_type,
16431643 .enum_type,
16441644 .error_set_type,
16451645 .inferred_error_set_type,
1646 => return bw.print("{fx}", .{
1646 => return w.print("{fx}", .{
16471647 try dg.fmtIntLiteral(try pt.undefValue(ty), location),
16481648 }),
16491649 .ptr_type => |ptr_type| switch (ptr_type.flags.size) {
16501650 .one, .many, .c => {
1651 try bw.writeAll("((");
1652 try dg.renderCType(bw, ctype);
1653 return bw.print("){fx})", .{
1651 try w.writeAll("((");
1652 try dg.renderCType(w, ctype);
1653 return w.print("){fx})", .{
16541654 try dg.fmtIntLiteral(.undef_usize, .Other),
16551655 });
16561656 },
16571657 .slice => {
16581658 if (!location.isInitializer()) {
1659 try bw.writeByte('(');
1660 try dg.renderCType(bw, ctype);
1661 try bw.writeByte(')');
1659 try w.writeByte('(');
1660 try dg.renderCType(w, ctype);
1661 try w.writeByte(')');
16621662 }
16631663
1664 try bw.writeAll("{(");
1664 try w.writeAll("{(");
16651665 const ptr_ty = ty.slicePtrFieldType(zcu);
1666 try dg.renderType(bw, ptr_ty);
1667 return bw.print("){fx}, {0fx}}}", .{
1666 try dg.renderType(w, ptr_ty);
1667 return w.print("){fx}, {0fx}}}", .{
16681668 try dg.fmtIntLiteral(.undef_usize, .Other),
16691669 });
16701670 },
16711671 },
16721672 .opt_type => |child_type| switch (ctype.info(ctype_pool)) {
16731673 .basic, .pointer => try dg.renderUndefValue(
1674 bw,
1674 w,
16751675 .fromInterned(if (ctype.isBool()) .bool_type else child_type),
16761676 location,
16771677 ),
......@@ -1680,21 +1680,21 @@ pub const DeclGen = struct {
16801680 switch (aggregate.fields.at(0, ctype_pool).name.index) {
16811681 .is_null, .payload => {},
16821682 .ptr, .len => return dg.renderUndefValue(
1683 bw,
1683 w,
16841684 .fromInterned(child_type),
16851685 location,
16861686 ),
16871687 else => unreachable,
16881688 }
16891689 if (!location.isInitializer()) {
1690 try bw.writeByte('(');
1691 try dg.renderCType(bw, ctype);
1692 try bw.writeByte(')');
1690 try w.writeByte('(');
1691 try dg.renderCType(w, ctype);
1692 try w.writeByte(')');
16931693 }
1694 try bw.writeByte('{');
1694 try w.writeByte('{');
16951695 for (0..aggregate.fields.len) |field_index| {
1696 if (field_index > 0) try bw.writeByte(',');
1697 try dg.renderUndefValue(bw, .fromInterned(
1696 if (field_index > 0) try w.writeByte(',');
1697 try dg.renderUndefValue(w, .fromInterned(
16981698 switch (aggregate.fields.at(field_index, ctype_pool).name.index) {
16991699 .is_null => .bool_type,
17001700 .payload => child_type,
......@@ -1702,7 +1702,7 @@ pub const DeclGen = struct {
17021702 },
17031703 ), initializer_type);
17041704 }
1705 try bw.writeByte('}');
1705 try w.writeByte('}');
17061706 },
17071707 },
17081708 .struct_type => {
......@@ -1710,117 +1710,117 @@ pub const DeclGen = struct {
17101710 switch (loaded_struct.layout) {
17111711 .auto, .@"extern" => {
17121712 if (!location.isInitializer()) {
1713 try bw.writeByte('(');
1714 try dg.renderCType(bw, ctype);
1715 try bw.writeByte(')');
1713 try w.writeByte('(');
1714 try dg.renderCType(w, ctype);
1715 try w.writeByte(')');
17161716 }
17171717
1718 try bw.writeByte('{');
1718 try w.writeByte('{');
17191719 var field_it = loaded_struct.iterateRuntimeOrder(ip);
17201720 var need_comma = false;
17211721 while (field_it.next()) |field_index| {
17221722 const field_ty: Type = .fromInterned(loaded_struct.field_types.get(ip)[field_index]);
17231723 if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue;
17241724
1725 if (need_comma) try bw.writeByte(',');
1725 if (need_comma) try w.writeByte(',');
17261726 need_comma = true;
1727 try dg.renderUndefValue(bw, field_ty, initializer_type);
1727 try dg.renderUndefValue(w, field_ty, initializer_type);
17281728 }
1729 return bw.writeByte('}');
1729 return w.writeByte('}');
17301730 },
1731 .@"packed" => return bw.print("{fx}", .{
1731 .@"packed" => return w.print("{fx}", .{
17321732 try dg.fmtIntLiteral(try pt.undefValue(ty), .Other),
17331733 }),
17341734 }
17351735 },
17361736 .tuple_type => |tuple_info| {
17371737 if (!location.isInitializer()) {
1738 try bw.writeByte('(');
1739 try dg.renderCType(bw, ctype);
1740 try bw.writeByte(')');
1738 try w.writeByte('(');
1739 try dg.renderCType(w, ctype);
1740 try w.writeByte(')');
17411741 }
17421742
1743 try bw.writeByte('{');
1743 try w.writeByte('{');
17441744 var need_comma = false;
17451745 for (0..tuple_info.types.len) |field_index| {
17461746 if (tuple_info.values.get(ip)[field_index] != .none) continue;
17471747 const field_ty: Type = .fromInterned(tuple_info.types.get(ip)[field_index]);
17481748 if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue;
17491749
1750 if (need_comma) try bw.writeByte(',');
1750 if (need_comma) try w.writeByte(',');
17511751 need_comma = true;
1752 try dg.renderUndefValue(bw, field_ty, initializer_type);
1752 try dg.renderUndefValue(w, field_ty, initializer_type);
17531753 }
1754 return bw.writeByte('}');
1754 return w.writeByte('}');
17551755 },
17561756 .union_type => {
17571757 const loaded_union = ip.loadUnionType(ty.toIntern());
17581758 switch (loaded_union.flagsUnordered(ip).layout) {
17591759 .auto, .@"extern" => {
17601760 if (!location.isInitializer()) {
1761 try bw.writeByte('(');
1762 try dg.renderCType(bw, ctype);
1763 try bw.writeByte(')');
1761 try w.writeByte('(');
1762 try dg.renderCType(w, ctype);
1763 try w.writeByte(')');
17641764 }
17651765
17661766 const has_tag = loaded_union.hasTag(ip);
1767 if (has_tag) try bw.writeByte('{');
1767 if (has_tag) try w.writeByte('{');
17681768 const aggregate = ctype.info(ctype_pool).aggregate;
17691769 for (0..if (has_tag) aggregate.fields.len else 1) |outer_field_index| {
1770 if (outer_field_index > 0) try bw.writeByte(',');
1770 if (outer_field_index > 0) try w.writeByte(',');
17711771 switch (if (has_tag)
17721772 aggregate.fields.at(outer_field_index, ctype_pool).name.index
17731773 else
17741774 .payload) {
17751775 .tag => try dg.renderUndefValue(
1776 bw,
1776 w,
17771777 .fromInterned(loaded_union.enum_tag_ty),
17781778 initializer_type,
17791779 ),
17801780 .payload => {
1781 try bw.writeByte('{');
1781 try w.writeByte('{');
17821782 for (0..loaded_union.field_types.len) |inner_field_index| {
17831783 const inner_field_ty: Type = .fromInterned(
17841784 loaded_union.field_types.get(ip)[inner_field_index],
17851785 );
17861786 if (!inner_field_ty.hasRuntimeBits(pt.zcu)) continue;
17871787 try dg.renderUndefValue(
1788 bw,
1788 w,
17891789 inner_field_ty,
17901790 initializer_type,
17911791 );
17921792 break;
17931793 }
1794 try bw.writeByte('}');
1794 try w.writeByte('}');
17951795 },
17961796 else => unreachable,
17971797 }
17981798 }
1799 if (has_tag) try bw.writeByte('}');
1799 if (has_tag) try w.writeByte('}');
18001800 },
1801 .@"packed" => return bw.print("{fx}", .{
1801 .@"packed" => return w.print("{fx}", .{
18021802 try dg.fmtIntLiteral(try pt.undefValue(ty), .Other),
18031803 }),
18041804 }
18051805 },
18061806 .error_union_type => |error_union_type| switch (ctype.info(ctype_pool)) {
18071807 .basic => try dg.renderUndefValue(
1808 bw,
1808 w,
18091809 .fromInterned(error_union_type.error_set_type),
18101810 location,
18111811 ),
18121812 .pointer, .aligned, .array, .vector, .fwd_decl, .function => unreachable,
18131813 .aggregate => |aggregate| {
18141814 if (!location.isInitializer()) {
1815 try bw.writeByte('(');
1816 try dg.renderCType(bw, ctype);
1817 try bw.writeByte(')');
1815 try w.writeByte('(');
1816 try dg.renderCType(w, ctype);
1817 try w.writeByte(')');
18181818 }
1819 try bw.writeByte('{');
1819 try w.writeByte('{');
18201820 for (0..aggregate.fields.len) |field_index| {
1821 if (field_index > 0) try bw.writeByte(',');
1821 if (field_index > 0) try w.writeByte(',');
18221822 try dg.renderUndefValue(
1823 bw,
1823 w,
18241824 .fromInterned(
18251825 switch (aggregate.fields.at(field_index, ctype_pool).name.index) {
18261826 .@"error" => error_union_type.error_set_type,
......@@ -1831,14 +1831,14 @@ pub const DeclGen = struct {
18311831 initializer_type,
18321832 );
18331833 }
1834 try bw.writeByte('}');
1834 try w.writeByte('}');
18351835 },
18361836 },
18371837 .array_type, .vector_type => {
18381838 const ai = ty.arrayInfo(zcu);
18391839 if (ai.elem_type.eql(.u8, zcu)) {
18401840 const c_len = ty.arrayLenIncludingSentinel(zcu);
1841 var literal: StringLiteral = .init(bw, c_len);
1841 var literal: StringLiteral = .init(w, c_len);
18421842 try literal.start();
18431843 var index: u64 = 0;
18441844 while (index < c_len) : (index += 1)
......@@ -1846,19 +1846,19 @@ pub const DeclGen = struct {
18461846 return literal.end();
18471847 } else {
18481848 if (!location.isInitializer()) {
1849 try bw.writeByte('(');
1850 try dg.renderCType(bw, ctype);
1851 try bw.writeByte(')');
1849 try w.writeByte('(');
1850 try dg.renderCType(w, ctype);
1851 try w.writeByte(')');
18521852 }
18531853
1854 try bw.writeByte('{');
1854 try w.writeByte('{');
18551855 const c_len = ty.arrayLenIncludingSentinel(zcu);
18561856 var index: u64 = 0;
18571857 while (index < c_len) : (index += 1) {
1858 if (index > 0) try bw.writeAll(", ");
1859 try dg.renderUndefValue(bw, ty.childType(zcu), initializer_type);
1858 if (index > 0) try w.writeAll(", ");
1859 try dg.renderUndefValue(w, ty.childType(zcu), initializer_type);
18601860 }
1861 return bw.writeByte('}');
1861 return w.writeByte('}');
18621862 }
18631863 },
18641864 .anyframe_type,
......@@ -1891,7 +1891,7 @@ pub const DeclGen = struct {
18911891
18921892 fn renderFunctionSignature(
18931893 dg: *DeclGen,
1894 bw: *Writer,
1894 w: *Writer,
18951895 fn_val: Value,
18961896 fn_align: InternPool.Alignment,
18971897 kind: CType.Kind,
......@@ -1913,8 +1913,8 @@ pub const DeclGen = struct {
19131913 const fn_info = zcu.typeToFunc(fn_ty).?;
19141914 if (fn_info.cc == .naked) {
19151915 switch (kind) {
1916 .forward => try bw.writeAll("zig_naked_decl "),
1917 .complete => try bw.writeAll("zig_naked "),
1916 .forward => try w.writeAll("zig_naked_decl "),
1917 .complete => try w.writeAll("zig_naked "),
19181918 else => unreachable,
19191919 }
19201920 }
......@@ -1923,33 +1923,33 @@ pub const DeclGen = struct {
19231923 const func_analysis = func.analysisUnordered(ip);
19241924
19251925 if (func_analysis.branch_hint == .cold)
1926 try bw.writeAll("zig_cold ");
1926 try w.writeAll("zig_cold ");
19271927
19281928 if (kind == .complete and func_analysis.disable_intrinsics or dg.mod.no_builtin)
1929 try bw.writeAll("zig_no_builtin ");
1929 try w.writeAll("zig_no_builtin ");
19301930 }
19311931
1932 if (fn_info.return_type == .noreturn_type) try bw.writeAll("zig_noreturn ");
1932 if (fn_info.return_type == .noreturn_type) try w.writeAll("zig_noreturn ");
19331933
1934 var trailing = try renderTypePrefix(dg.pass, &dg.ctype_pool, zcu, bw, fn_ctype, .suffix, .{});
1934 var trailing = try renderTypePrefix(dg.pass, &dg.ctype_pool, zcu, w, fn_ctype, .suffix, .{});
19351935
19361936 if (toCallingConvention(fn_info.cc, zcu)) |call_conv| {
1937 try bw.print("{f}zig_callconv({s})", .{ trailing, call_conv });
1937 try w.print("{f}zig_callconv({s})", .{ trailing, call_conv });
19381938 trailing = .maybe_space;
19391939 }
19401940
1941 try bw.print("{f}", .{trailing});
1941 try w.print("{f}", .{trailing});
19421942 switch (name) {
1943 .nav => |nav| try dg.renderNavName(bw, nav),
1944 .fmt_ctype_pool_string => |fmt| try bw.print("{f }", .{fmt}),
1945 .@"export" => |@"export"| try bw.print("{f }", .{fmtIdent(@"export".extern_name.toSlice(ip))}),
1943 .nav => |nav| try dg.renderNavName(w, nav),
1944 .fmt_ctype_pool_string => |fmt| try w.print("{f }", .{fmt}),
1945 .@"export" => |@"export"| try w.print("{f }", .{fmtIdent(@"export".extern_name.toSlice(ip))}),
19461946 }
19471947
19481948 try renderTypeSuffix(
19491949 dg.pass,
19501950 &dg.ctype_pool,
19511951 zcu,
1952 bw,
1952 w,
19531953 fn_ctype,
19541954 .suffix,
19551955 CQualifiers.init(.{ .@"const" = switch (kind) {
......@@ -1961,7 +1961,7 @@ pub const DeclGen = struct {
19611961
19621962 switch (kind) {
19631963 .forward => {
1964 if (fn_align.toByteUnits()) |a| try bw.print(" zig_align_fn({})", .{a});
1964 if (fn_align.toByteUnits()) |a| try w.print(" zig_align_fn({})", .{a});
19651965 switch (name) {
19661966 .nav, .fmt_ctype_pool_string => {},
19671967 .@"export" => |@"export"| {
......@@ -1969,17 +1969,17 @@ pub const DeclGen = struct {
19691969 const is_mangled = isMangledIdent(extern_name, true);
19701970 const is_export = @"export".extern_name != @"export".main_name;
19711971 if (is_mangled and is_export) {
1972 try bw.print(" zig_mangled_export({f }, {fs}, {fs})", .{
1972 try w.print(" zig_mangled_export({f }, {fs}, {fs})", .{
19731973 fmtIdent(extern_name),
19741974 fmtStringLiteral(extern_name, null),
19751975 fmtStringLiteral(@"export".main_name.toSlice(ip), null),
19761976 });
19771977 } else if (is_mangled) {
1978 try bw.print(" zig_mangled({f }, {fs})", .{
1978 try w.print(" zig_mangled({f }, {fs})", .{
19791979 fmtIdent(extern_name), fmtStringLiteral(extern_name, null),
19801980 });
19811981 } else if (is_export) {
1982 try bw.print(" zig_export({fs}, {fs})", .{
1982 try w.print(" zig_export({fs}, {fs})", .{
19831983 fmtStringLiteral(@"export".main_name.toSlice(ip), null),
19841984 fmtStringLiteral(extern_name, null),
19851985 });
......@@ -2012,13 +2012,13 @@ pub const DeclGen = struct {
20122012 /// | `renderTypeAndName` | "uint8_t *name" | "uint8_t *name[10]" |
20132013 /// | `renderType` | "uint8_t *" | "uint8_t *[10]" |
20142014 ///
2015 fn renderType(dg: *DeclGen, bw: *Writer, t: Type) Error!void {
2016 try dg.renderCType(bw, try dg.ctypeFromType(t, .complete));
2015 fn renderType(dg: *DeclGen, w: *Writer, t: Type) Error!void {
2016 try dg.renderCType(w, try dg.ctypeFromType(t, .complete));
20172017 }
20182018
2019 fn renderCType(dg: *DeclGen, bw: *Writer, ctype: CType) Error!void {
2020 _ = try renderTypePrefix(dg.pass, &dg.ctype_pool, dg.pt.zcu, bw, ctype, .suffix, .{});
2021 try renderTypeSuffix(dg.pass, &dg.ctype_pool, dg.pt.zcu, bw, ctype, .suffix, .{});
2019 fn renderCType(dg: *DeclGen, w: *Writer, ctype: CType) Error!void {
2020 _ = try renderTypePrefix(dg.pass, &dg.ctype_pool, dg.pt.zcu, w, ctype, .suffix, .{});
2021 try renderTypeSuffix(dg.pass, &dg.ctype_pool, dg.pt.zcu, w, ctype, .suffix, .{});
20222022 }
20232023
20242024 const IntCastContext = union(enum) {
......@@ -2031,13 +2031,13 @@ pub const DeclGen = struct {
20312031 value: Value,
20322032 },
20332033
2034 pub fn writeValue(self: *const IntCastContext, dg: *DeclGen, bw: *Writer, location: ValueRenderLocation) !void {
2034 pub fn writeValue(self: *const IntCastContext, dg: *DeclGen, w: *Writer, location: ValueRenderLocation) !void {
20352035 switch (self.*) {
20362036 .c_value => |v| {
2037 try v.f.writeCValue(bw, v.value, location);
2038 try v.v.elem(v.f, bw);
2037 try v.f.writeCValue(w, v.value, location);
2038 try v.v.elem(v.f, w);
20392039 },
2040 .value => |v| try dg.renderValue(bw, v.value, location),
2040 .value => |v| try dg.renderValue(w, v.value, location),
20412041 }
20422042 }
20432043 };
......@@ -2077,7 +2077,7 @@ pub const DeclGen = struct {
20772077 /// | > 64 bit integer | > 64 bit integer | zig_make_<dest_ty>(zig_hi_<src_ty>(src), zig_lo_<src_ty>(src))
20782078 fn renderIntCast(
20792079 dg: *DeclGen,
2080 bw: *Writer,
2080 w: *Writer,
20812081 dest_ty: Type,
20822082 context: IntCastContext,
20832083 src_ty: Type,
......@@ -2102,52 +2102,52 @@ pub const DeclGen = struct {
21022102 dest_int_info.signedness != src_int_info.?.signedness);
21032103
21042104 if (needs_cast) {
2105 try bw.writeByte('(');
2106 try dg.renderType(bw, dest_ty);
2107 try bw.writeByte(')');
2105 try w.writeByte('(');
2106 try dg.renderType(w, dest_ty);
2107 try w.writeByte(')');
21082108 }
21092109 if (src_is_ptr) {
2110 try bw.writeByte('(');
2111 try dg.renderType(bw, src_eff_ty);
2112 try bw.writeByte(')');
2110 try w.writeByte('(');
2111 try dg.renderType(w, src_eff_ty);
2112 try w.writeByte(')');
21132113 }
2114 try context.writeValue(dg, bw, location);
2114 try context.writeValue(dg, w, location);
21152115 } else if (dest_bits <= 64 and src_bits > 64) {
21162116 assert(!src_is_ptr);
21172117 if (dest_bits < 64) {
2118 try bw.writeByte('(');
2119 try dg.renderType(bw, dest_ty);
2120 try bw.writeByte(')');
2118 try w.writeByte('(');
2119 try dg.renderType(w, dest_ty);
2120 try w.writeByte(')');
21212121 }
2122 try bw.writeAll("zig_lo_");
2123 try dg.renderTypeForBuiltinFnName(bw, src_eff_ty);
2124 try bw.writeByte('(');
2125 try context.writeValue(dg, bw, .FunctionArgument);
2126 try bw.writeByte(')');
2122 try w.writeAll("zig_lo_");
2123 try dg.renderTypeForBuiltinFnName(w, src_eff_ty);
2124 try w.writeByte('(');
2125 try context.writeValue(dg, w, .FunctionArgument);
2126 try w.writeByte(')');
21272127 } else if (dest_bits > 64 and src_bits <= 64) {
2128 try bw.writeAll("zig_make_");
2129 try dg.renderTypeForBuiltinFnName(bw, dest_ty);
2130 try bw.writeAll("(0, "); // TODO: Should the 0 go through fmtIntLiteral?
2128 try w.writeAll("zig_make_");
2129 try dg.renderTypeForBuiltinFnName(w, dest_ty);
2130 try w.writeAll("(0, "); // TODO: Should the 0 go through fmtIntLiteral?
21312131 if (src_is_ptr) {
2132 try bw.writeByte('(');
2133 try dg.renderType(bw, src_eff_ty);
2134 try bw.writeByte(')');
2132 try w.writeByte('(');
2133 try dg.renderType(w, src_eff_ty);
2134 try w.writeByte(')');
21352135 }
2136 try context.writeValue(dg, bw, .FunctionArgument);
2137 try bw.writeByte(')');
2136 try context.writeValue(dg, w, .FunctionArgument);
2137 try w.writeByte(')');
21382138 } else {
21392139 assert(!src_is_ptr);
2140 try bw.writeAll("zig_make_");
2141 try dg.renderTypeForBuiltinFnName(bw, dest_ty);
2142 try bw.writeAll("(zig_hi_");
2143 try dg.renderTypeForBuiltinFnName(bw, src_eff_ty);
2144 try bw.writeByte('(');
2145 try context.writeValue(dg, bw, .FunctionArgument);
2146 try bw.writeAll("), zig_lo_");
2147 try dg.renderTypeForBuiltinFnName(bw, src_eff_ty);
2148 try bw.writeByte('(');
2149 try context.writeValue(dg, bw, .FunctionArgument);
2150 try bw.writeAll("))");
2140 try w.writeAll("zig_make_");
2141 try dg.renderTypeForBuiltinFnName(w, dest_ty);
2142 try w.writeAll("(zig_hi_");
2143 try dg.renderTypeForBuiltinFnName(w, src_eff_ty);
2144 try w.writeByte('(');
2145 try context.writeValue(dg, w, .FunctionArgument);
2146 try w.writeAll("), zig_lo_");
2147 try dg.renderTypeForBuiltinFnName(w, src_eff_ty);
2148 try w.writeByte('(');
2149 try context.writeValue(dg, w, .FunctionArgument);
2150 try w.writeAll("))");
21512151 }
21522152 }
21532153
......@@ -2161,7 +2161,7 @@ pub const DeclGen = struct {
21612161 ///
21622162 fn renderTypeAndName(
21632163 dg: *DeclGen,
2164 bw: *Writer,
2164 w: *Writer,
21652165 ty: Type,
21662166 name: CValue,
21672167 qualifiers: CQualifiers,
......@@ -2169,7 +2169,7 @@ pub const DeclGen = struct {
21692169 kind: CType.Kind,
21702170 ) !void {
21712171 try dg.renderCTypeAndName(
2172 bw,
2172 w,
21732173 try dg.ctypeFromType(ty, kind),
21742174 name,
21752175 qualifiers,
......@@ -2182,7 +2182,7 @@ pub const DeclGen = struct {
21822182
21832183 fn renderCTypeAndName(
21842184 dg: *DeclGen,
2185 bw: *Writer,
2185 w: *Writer,
21862186 ctype: CType,
21872187 name: CValue,
21882188 qualifiers: CQualifiers,
......@@ -2190,52 +2190,52 @@ pub const DeclGen = struct {
21902190 ) !void {
21912191 const zcu = dg.pt.zcu;
21922192 switch (alignas.abiOrder()) {
2193 .lt => try bw.print("zig_under_align({}) ", .{alignas.toByteUnits()}),
2193 .lt => try w.print("zig_under_align({}) ", .{alignas.toByteUnits()}),
21942194 .eq => {},
2195 .gt => try bw.print("zig_align({}) ", .{alignas.toByteUnits()}),
2195 .gt => try w.print("zig_align({}) ", .{alignas.toByteUnits()}),
21962196 }
21972197
2198 try bw.print("{f}", .{
2199 try renderTypePrefix(dg.pass, &dg.ctype_pool, zcu, bw, ctype, .suffix, qualifiers),
2198 try w.print("{f}", .{
2199 try renderTypePrefix(dg.pass, &dg.ctype_pool, zcu, w, ctype, .suffix, qualifiers),
22002200 });
2201 try dg.writeName(bw, name);
2202 try renderTypeSuffix(dg.pass, &dg.ctype_pool, zcu, bw, ctype, .suffix, .{});
2201 try dg.writeName(w, name);
2202 try renderTypeSuffix(dg.pass, &dg.ctype_pool, zcu, w, ctype, .suffix, .{});
22032203 }
22042204
2205 fn writeName(dg: *DeclGen, bw: *Writer, c_value: CValue) !void {
2205 fn writeName(dg: *DeclGen, w: *Writer, c_value: CValue) !void {
22062206 switch (c_value) {
2207 .new_local, .local => |i| try bw.print("t{d}", .{i}),
2208 .constant => |uav| try renderUavName(bw, uav),
2209 .nav => |nav| try dg.renderNavName(bw, nav),
2210 .identifier => |ident| try bw.print("{f }", .{fmtIdent(ident)}),
2207 .new_local, .local => |i| try w.print("t{d}", .{i}),
2208 .constant => |uav| try renderUavName(w, uav),
2209 .nav => |nav| try dg.renderNavName(w, nav),
2210 .identifier => |ident| try w.print("{f }", .{fmtIdent(ident)}),
22112211 else => unreachable,
22122212 }
22132213 }
22142214
2215 fn writeCValue(dg: *DeclGen, bw: *Writer, c_value: CValue) Error!void {
2215 fn writeCValue(dg: *DeclGen, w: *Writer, c_value: CValue) Error!void {
22162216 switch (c_value) {
22172217 .none, .new_local, .local, .local_ref => unreachable,
2218 .constant => |uav| try renderUavName(bw, uav),
2218 .constant => |uav| try renderUavName(w, uav),
22192219 .arg, .arg_array => unreachable,
2220 .field => |i| try bw.print("f{d}", .{i}),
2221 .nav => |nav| try dg.renderNavName(bw, nav),
2220 .field => |i| try w.print("f{d}", .{i}),
2221 .nav => |nav| try dg.renderNavName(w, nav),
22222222 .nav_ref => |nav| {
2223 try bw.writeByte('&');
2224 try dg.renderNavName(bw, nav);
2223 try w.writeByte('&');
2224 try dg.renderNavName(w, nav);
22252225 },
2226 .undef => |ty| try dg.renderUndefValue(bw, ty, .Other),
2227 .identifier => |ident| try bw.print("{f }", .{fmtIdent(ident)}),
2228 .payload_identifier => |ident| try bw.print("{f }.{f }", .{
2226 .undef => |ty| try dg.renderUndefValue(w, ty, .Other),
2227 .identifier => |ident| try w.print("{f }", .{fmtIdent(ident)}),
2228 .payload_identifier => |ident| try w.print("{f }.{f }", .{
22292229 fmtIdent("payload"),
22302230 fmtIdent(ident),
22312231 }),
2232 .ctype_pool_string => |string| try bw.print("{f }", .{
2232 .ctype_pool_string => |string| try w.print("{f }", .{
22332233 fmtCTypePoolString(string, &dg.ctype_pool),
22342234 }),
22352235 }
22362236 }
22372237
2238 fn writeCValueDeref(dg: *DeclGen, bw: *Writer, c_value: CValue) !void {
2238 fn writeCValueDeref(dg: *DeclGen, w: *Writer, c_value: CValue) !void {
22392239 switch (c_value) {
22402240 .none,
22412241 .new_local,
......@@ -2246,16 +2246,16 @@ pub const DeclGen = struct {
22462246 .arg_array,
22472247 .ctype_pool_string,
22482248 => unreachable,
2249 .field => |i| try bw.print("f{d}", .{i}),
2249 .field => |i| try w.print("f{d}", .{i}),
22502250 .nav => |nav| {
2251 try bw.writeAll("(*");
2252 try dg.renderNavName(bw, nav);
2253 try bw.writeByte(')');
2251 try w.writeAll("(*");
2252 try dg.renderNavName(w, nav);
2253 try w.writeByte(')');
22542254 },
2255 .nav_ref => |nav| try dg.renderNavName(bw, nav),
2255 .nav_ref => |nav| try dg.renderNavName(w, nav),
22562256 .undef => unreachable,
2257 .identifier => |ident| try bw.print("(*{f })", .{fmtIdent(ident)}),
2258 .payload_identifier => |ident| try bw.print("(*{f }.{f })", .{
2257 .identifier => |ident| try w.print("(*{f })", .{fmtIdent(ident)}),
2258 .payload_identifier => |ident| try w.print("(*{f }.{f })", .{
22592259 fmtIdent("payload"),
22602260 fmtIdent(ident),
22612261 }),
......@@ -2264,18 +2264,18 @@ pub const DeclGen = struct {
22642264
22652265 fn writeCValueMember(
22662266 dg: *DeclGen,
2267 bw: *Writer,
2267 w: *Writer,
22682268 c_value: CValue,
22692269 member: CValue,
22702270 ) Error!void {
2271 try dg.writeCValue(bw, c_value);
2272 try bw.writeByte('.');
2273 try dg.writeCValue(bw, member);
2271 try dg.writeCValue(w, c_value);
2272 try w.writeByte('.');
2273 try dg.writeCValue(w, member);
22742274 }
22752275
22762276 fn writeCValueDerefMember(
22772277 dg: *DeclGen,
2278 bw: *Writer,
2278 w: *Writer,
22792279 c_value: CValue,
22802280 member: CValue,
22812281 ) !void {
......@@ -2292,15 +2292,15 @@ pub const DeclGen = struct {
22922292 .ctype_pool_string,
22932293 => unreachable,
22942294 .nav, .identifier, .payload_identifier => {
2295 try dg.writeCValue(bw, c_value);
2296 try bw.writeAll("->");
2295 try dg.writeCValue(w, c_value);
2296 try w.writeAll("->");
22972297 },
22982298 .nav_ref => {
2299 try dg.writeCValueDeref(bw, c_value);
2300 try bw.writeByte('.');
2299 try dg.writeCValueDeref(w, c_value);
2300 try w.writeByte('.');
23012301 },
23022302 }
2303 try dg.writeCValue(bw, member);
2303 try dg.writeCValue(w, member);
23042304 }
23052305
23062306 fn renderFwdDecl(
......@@ -2342,36 +2342,36 @@ pub const DeclGen = struct {
23422342 try fwd.writeAll(";\n");
23432343 }
23442344
2345 fn renderNavName(dg: *DeclGen, bw: *Writer, nav_index: InternPool.Nav.Index) !void {
2345 fn renderNavName(dg: *DeclGen, w: *Writer, nav_index: InternPool.Nav.Index) !void {
23462346 const zcu = dg.pt.zcu;
23472347 const ip = &zcu.intern_pool;
23482348 const nav = ip.getNav(nav_index);
23492349 if (nav.getExtern(ip)) |@"extern"| {
2350 try bw.print("{f }", .{
2350 try w.print("{f }", .{
23512351 fmtIdent(ip.getNav(@"extern".owner_nav).name.toSlice(ip)),
23522352 });
23532353 } else {
23542354 // MSVC has a limit of 4095 character token length limit, and fmtIdent can (worst case),
23552355 // expand to 3x the length of its input, but let's cut it off at a much shorter limit.
23562356 const fqn_slice = ip.getNav(nav_index).fqn.toSlice(ip);
2357 try bw.print("{f}__{d}", .{
2357 try w.print("{f}__{d}", .{
23582358 fmtIdent(fqn_slice[0..@min(fqn_slice.len, 100)]),
23592359 @intFromEnum(nav_index),
23602360 });
23612361 }
23622362 }
23632363
2364 fn renderUavName(bw: *Writer, uav: Value) !void {
2365 try bw.print("__anon_{d}", .{@intFromEnum(uav.toIntern())});
2364 fn renderUavName(w: *Writer, uav: Value) !void {
2365 try w.print("__anon_{d}", .{@intFromEnum(uav.toIntern())});
23662366 }
23672367
2368 fn renderTypeForBuiltinFnName(dg: *DeclGen, bw: *Writer, ty: Type) !void {
2369 try dg.renderCTypeForBuiltinFnName(bw, try dg.ctypeFromType(ty, .complete));
2368 fn renderTypeForBuiltinFnName(dg: *DeclGen, w: *Writer, ty: Type) !void {
2369 try dg.renderCTypeForBuiltinFnName(w, try dg.ctypeFromType(ty, .complete));
23702370 }
23712371
2372 fn renderCTypeForBuiltinFnName(dg: *DeclGen, bw: *Writer, ctype: CType) !void {
2372 fn renderCTypeForBuiltinFnName(dg: *DeclGen, w: *Writer, ctype: CType) !void {
23732373 switch (ctype.info(&dg.ctype_pool)) {
2374 else => |ctype_info| try bw.print("{c}{d}", .{
2374 else => |ctype_info| try w.print("{c}{d}", .{
23752375 if (ctype.isBool())
23762376 signAbbrev(.unsigned)
23772377 else if (ctype.isInteger())
......@@ -2384,11 +2384,11 @@ pub const DeclGen = struct {
23842384 return dg.fail("TODO: CBE: implement renderTypeForBuiltinFnName for {s} type", .{@tagName(ctype_info)}),
23852385 if (ctype.isFloat()) ctype.floatActiveBits(dg.mod) else dg.byteSize(ctype) * 8,
23862386 }),
2387 .array => try bw.writeAll("big"),
2387 .array => try w.writeAll("big"),
23882388 }
23892389 }
23902390
2391 fn renderBuiltinInfo(dg: *DeclGen, bw: *Writer, ty: Type, info: BuiltinInfo) !void {
2391 fn renderBuiltinInfo(dg: *DeclGen, w: *Writer, ty: Type, info: BuiltinInfo) !void {
23922392 const ctype = try dg.ctypeFromType(ty, .complete);
23932393 const is_big = ctype.info(&dg.ctype_pool) == .array;
23942394 switch (info) {
......@@ -2403,8 +2403,8 @@ pub const DeclGen = struct {
24032403 .bits = @intCast(ty.bitSize(zcu)),
24042404 };
24052405
2406 if (is_big) try bw.print(", {}", .{int_info.signedness == .signed});
2407 try bw.print(", {f}", .{try dg.fmtIntLiteral(
2406 if (is_big) try w.print(", {}", .{int_info.signedness == .signed});
2407 try w.print(", {f}", .{try dg.fmtIntLiteral(
24082408 try pt.intValue(if (is_big) .u16 else .u8, int_info.bits),
24092409 .FunctionArgument,
24102410 )});
......@@ -2437,32 +2437,32 @@ const RenderCTypeTrailing = enum {
24372437
24382438 pub fn format(
24392439 self: @This(),
2440 bw: *Writer,
2440 w: *Writer,
24412441 comptime fmt: []const u8,
24422442 ) Writer.Error!void {
24432443 if (fmt.len != 0) @compileError("invalid format string '" ++
24442444 fmt ++ "' for type '" ++ @typeName(@This()) ++ "'");
24452445 switch (self) {
24462446 .no_space => {},
2447 .maybe_space => try bw.writeByte(' '),
2447 .maybe_space => try w.writeByte(' '),
24482448 }
24492449 }
24502450};
2451fn renderAlignedTypeName(bw: *Writer, ctype: CType) !void {
2452 try bw.print("anon__aligned_{d}", .{@intFromEnum(ctype.index)});
2451fn renderAlignedTypeName(w: *Writer, ctype: CType) !void {
2452 try w.print("anon__aligned_{d}", .{@intFromEnum(ctype.index)});
24532453}
24542454fn renderFwdDeclTypeName(
24552455 zcu: *Zcu,
2456 bw: *Writer,
2456 w: *Writer,
24572457 ctype: CType,
24582458 fwd_decl: CType.Info.FwdDecl,
24592459 attributes: []const u8,
24602460) !void {
24612461 const ip = &zcu.intern_pool;
2462 try bw.print("{s} {s}", .{ @tagName(fwd_decl.tag), attributes });
2462 try w.print("{s} {s}", .{ @tagName(fwd_decl.tag), attributes });
24632463 switch (fwd_decl.name) {
2464 .anon => try bw.print("anon__lazy_{d}", .{@intFromEnum(ctype.index)}),
2465 .index => |index| try bw.print("{f}__{d}", .{
2464 .anon => try w.print("anon__lazy_{d}", .{@intFromEnum(ctype.index)}),
2465 .index => |index| try w.print("{f}__{d}", .{
24662466 fmtIdent(Type.fromInterned(index).containerTypeName(ip).toSlice(&zcu.intern_pool)),
24672467 @intFromEnum(index),
24682468 }),
......@@ -2472,21 +2472,21 @@ fn renderTypePrefix(
24722472 pass: DeclGen.Pass,
24732473 ctype_pool: *const CType.Pool,
24742474 zcu: *Zcu,
2475 bw: *Writer,
2475 w: *Writer,
24762476 ctype: CType,
24772477 parent_fix: CTypeFix,
24782478 qualifiers: CQualifiers,
24792479) Writer.Error!RenderCTypeTrailing {
24802480 var trailing = RenderCTypeTrailing.maybe_space;
24812481 switch (ctype.info(ctype_pool)) {
2482 .basic => |basic_info| try bw.writeAll(@tagName(basic_info)),
2482 .basic => |basic_info| try w.writeAll(@tagName(basic_info)),
24832483
24842484 .pointer => |pointer_info| {
2485 try bw.print("{f}*", .{try renderTypePrefix(
2485 try w.print("{f}*", .{try renderTypePrefix(
24862486 pass,
24872487 ctype_pool,
24882488 zcu,
2489 bw,
2489 w,
24902490 pointer_info.elem_ctype,
24912491 .prefix,
24922492 CQualifiers.init(.{
......@@ -2498,13 +2498,13 @@ fn renderTypePrefix(
24982498 },
24992499
25002500 .aligned => switch (pass) {
2501 .nav => |nav| try bw.print("nav__{d}_{d}", .{
2501 .nav => |nav| try w.print("nav__{d}_{d}", .{
25022502 @intFromEnum(nav), @intFromEnum(ctype.index),
25032503 }),
2504 .uav => |uav| try bw.print("uav__{d}_{d}", .{
2504 .uav => |uav| try w.print("uav__{d}_{d}", .{
25052505 @intFromEnum(uav), @intFromEnum(ctype.index),
25062506 }),
2507 .flush => try renderAlignedTypeName(bw, ctype),
2507 .flush => try renderAlignedTypeName(w, ctype),
25082508 },
25092509
25102510 .array, .vector => |sequence_info| {
......@@ -2512,14 +2512,14 @@ fn renderTypePrefix(
25122512 pass,
25132513 ctype_pool,
25142514 zcu,
2515 bw,
2515 w,
25162516 sequence_info.elem_ctype,
25172517 .suffix,
25182518 qualifiers,
25192519 );
25202520 switch (parent_fix) {
25212521 .prefix => {
2522 try bw.print("{f}(", .{child_trailing});
2522 try w.print("{f}(", .{child_trailing});
25232523 return .no_space;
25242524 },
25252525 .suffix => return child_trailing,
......@@ -2528,31 +2528,31 @@ fn renderTypePrefix(
25282528
25292529 .fwd_decl => |fwd_decl_info| switch (fwd_decl_info.name) {
25302530 .anon => switch (pass) {
2531 .nav => |nav| try bw.print("nav__{d}_{d}", .{
2531 .nav => |nav| try w.print("nav__{d}_{d}", .{
25322532 @intFromEnum(nav), @intFromEnum(ctype.index),
25332533 }),
2534 .uav => |uav| try bw.print("uav__{d}_{d}", .{
2534 .uav => |uav| try w.print("uav__{d}_{d}", .{
25352535 @intFromEnum(uav), @intFromEnum(ctype.index),
25362536 }),
2537 .flush => try renderFwdDeclTypeName(zcu, bw, ctype, fwd_decl_info, ""),
2537 .flush => try renderFwdDeclTypeName(zcu, w, ctype, fwd_decl_info, ""),
25382538 },
2539 .index => try renderFwdDeclTypeName(zcu, bw, ctype, fwd_decl_info, ""),
2539 .index => try renderFwdDeclTypeName(zcu, w, ctype, fwd_decl_info, ""),
25402540 },
25412541
25422542 .aggregate => |aggregate_info| switch (aggregate_info.name) {
25432543 .anon => {
2544 try bw.print("{s} {s}", .{
2544 try w.print("{s} {s}", .{
25452545 @tagName(aggregate_info.tag),
25462546 if (aggregate_info.@"packed") "zig_packed(" else "",
25472547 });
2548 try renderFields(zcu, bw, ctype_pool, aggregate_info, 1);
2549 if (aggregate_info.@"packed") try bw.writeByte(')');
2548 try renderFields(zcu, w, ctype_pool, aggregate_info, 1);
2549 if (aggregate_info.@"packed") try w.writeByte(')');
25502550 },
25512551 .fwd_decl => |fwd_decl| return renderTypePrefix(
25522552 pass,
25532553 ctype_pool,
25542554 zcu,
2555 bw,
2555 w,
25562556 fwd_decl,
25572557 parent_fix,
25582558 qualifiers,
......@@ -2564,14 +2564,14 @@ fn renderTypePrefix(
25642564 pass,
25652565 ctype_pool,
25662566 zcu,
2567 bw,
2567 w,
25682568 function_info.return_ctype,
25692569 .suffix,
25702570 .{},
25712571 );
25722572 switch (parent_fix) {
25732573 .prefix => {
2574 try bw.print("{f}(", .{child_trailing});
2574 try w.print("{f}(", .{child_trailing});
25752575 return .no_space;
25762576 },
25772577 .suffix => return child_trailing,
......@@ -2580,7 +2580,7 @@ fn renderTypePrefix(
25802580 }
25812581 var qualifier_it = qualifiers.iterator();
25822582 while (qualifier_it.next()) |qualifier| {
2583 try bw.print("{f}{s}", .{ trailing, @tagName(qualifier) });
2583 try w.print("{f}{s}", .{ trailing, @tagName(qualifier) });
25842584 trailing = .maybe_space;
25852585 }
25862586 return trailing;
......@@ -2589,7 +2589,7 @@ fn renderTypeSuffix(
25892589 pass: DeclGen.Pass,
25902590 ctype_pool: *const CType.Pool,
25912591 zcu: *Zcu,
2592 bw: *Writer,
2592 w: *Writer,
25932593 ctype: CType,
25942594 parent_fix: CTypeFix,
25952595 qualifiers: CQualifiers,
......@@ -2600,94 +2600,94 @@ fn renderTypeSuffix(
26002600 pass,
26012601 ctype_pool,
26022602 zcu,
2603 bw,
2603 w,
26042604 pointer_info.elem_ctype,
26052605 .prefix,
26062606 .{},
26072607 ),
26082608 .array, .vector => |sequence_info| {
26092609 switch (parent_fix) {
2610 .prefix => try bw.writeByte(')'),
2610 .prefix => try w.writeByte(')'),
26112611 .suffix => {},
26122612 }
26132613
2614 try bw.print("[{}]", .{sequence_info.len});
2615 try renderTypeSuffix(pass, ctype_pool, zcu, bw, sequence_info.elem_ctype, .suffix, .{});
2614 try w.print("[{}]", .{sequence_info.len});
2615 try renderTypeSuffix(pass, ctype_pool, zcu, w, sequence_info.elem_ctype, .suffix, .{});
26162616 },
26172617 .function => |function_info| {
26182618 switch (parent_fix) {
2619 .prefix => try bw.writeByte(')'),
2619 .prefix => try w.writeByte(')'),
26202620 .suffix => {},
26212621 }
26222622
2623 try bw.writeByte('(');
2623 try w.writeByte('(');
26242624 var need_comma = false;
26252625 for (0..function_info.param_ctypes.len) |param_index| {
26262626 const param_type = function_info.param_ctypes.at(param_index, ctype_pool);
2627 if (need_comma) try bw.writeAll(", ");
2627 if (need_comma) try w.writeAll(", ");
26282628 need_comma = true;
26292629 const trailing =
2630 try renderTypePrefix(pass, ctype_pool, zcu, bw, param_type, .suffix, qualifiers);
2631 if (qualifiers.contains(.@"const")) try bw.print("{f}a{d}", .{ trailing, param_index });
2632 try renderTypeSuffix(pass, ctype_pool, zcu, bw, param_type, .suffix, .{});
2630 try renderTypePrefix(pass, ctype_pool, zcu, w, param_type, .suffix, qualifiers);
2631 if (qualifiers.contains(.@"const")) try w.print("{f}a{d}", .{ trailing, param_index });
2632 try renderTypeSuffix(pass, ctype_pool, zcu, w, param_type, .suffix, .{});
26332633 }
26342634 if (function_info.varargs) {
2635 if (need_comma) try bw.writeAll(", ");
2635 if (need_comma) try w.writeAll(", ");
26362636 need_comma = true;
2637 try bw.writeAll("...");
2637 try w.writeAll("...");
26382638 }
2639 if (!need_comma) try bw.writeAll("void");
2640 try bw.writeByte(')');
2639 if (!need_comma) try w.writeAll("void");
2640 try w.writeByte(')');
26412641
2642 try renderTypeSuffix(pass, ctype_pool, zcu, bw, function_info.return_ctype, .suffix, .{});
2642 try renderTypeSuffix(pass, ctype_pool, zcu, w, function_info.return_ctype, .suffix, .{});
26432643 },
26442644 }
26452645}
26462646fn renderFields(
26472647 zcu: *Zcu,
2648 bw: *Writer,
2648 w: *Writer,
26492649 ctype_pool: *const CType.Pool,
26502650 aggregate_info: CType.Info.Aggregate,
26512651 indent: usize,
26522652) !void {
2653 try bw.writeAll("{\n");
2653 try w.writeAll("{\n");
26542654 for (0..aggregate_info.fields.len) |field_index| {
26552655 const field_info = aggregate_info.fields.at(field_index, ctype_pool);
2656 try bw.splatByteAll(' ', indent + 1);
2656 try w.splatByteAll(' ', indent + 1);
26572657 switch (field_info.alignas.abiOrder()) {
26582658 .lt => {
26592659 std.debug.assert(aggregate_info.@"packed");
2660 if (field_info.alignas.@"align" != .@"1") try bw.print("zig_under_align({}) ", .{
2660 if (field_info.alignas.@"align" != .@"1") try w.print("zig_under_align({}) ", .{
26612661 field_info.alignas.toByteUnits(),
26622662 });
26632663 },
26642664 .eq => if (aggregate_info.@"packed" and field_info.alignas.@"align" != .@"1")
2665 try bw.print("zig_align({}) ", .{field_info.alignas.toByteUnits()}),
2665 try w.print("zig_align({}) ", .{field_info.alignas.toByteUnits()}),
26662666 .gt => {
26672667 std.debug.assert(field_info.alignas.@"align" != .@"1");
2668 try bw.print("zig_align({}) ", .{field_info.alignas.toByteUnits()});
2668 try w.print("zig_align({}) ", .{field_info.alignas.toByteUnits()});
26692669 },
26702670 }
26712671 const trailing = try renderTypePrefix(
26722672 .flush,
26732673 ctype_pool,
26742674 zcu,
2675 bw,
2675 w,
26762676 field_info.ctype,
26772677 .suffix,
26782678 .{},
26792679 );
2680 try bw.print("{f}{f }", .{ trailing, fmtCTypePoolString(field_info.name, ctype_pool) });
2681 try renderTypeSuffix(.flush, ctype_pool, zcu, bw, field_info.ctype, .suffix, .{});
2682 try bw.writeAll(";\n");
2680 try w.print("{f}{f }", .{ trailing, fmtCTypePoolString(field_info.name, ctype_pool) });
2681 try renderTypeSuffix(.flush, ctype_pool, zcu, w, field_info.ctype, .suffix, .{});
2682 try w.writeAll(";\n");
26832683 }
2684 try bw.splatByteAll(' ', indent);
2685 try bw.writeByte('}');
2684 try w.splatByteAll(' ', indent);
2685 try w.writeByte('}');
26862686}
26872687
26882688pub fn genTypeDecl(
26892689 zcu: *Zcu,
2690 bw: *Writer,
2690 w: *Writer,
26912691 global_ctype_pool: *const CType.Pool,
26922692 global_ctype: CType,
26932693 pass: DeclGen.Pass,
......@@ -2700,27 +2700,27 @@ pub fn genTypeDecl(
27002700 .aligned => |aligned_info| {
27012701 if (!found_existing) {
27022702 std.debug.assert(aligned_info.alignas.abiOrder().compare(.lt));
2703 try bw.print("typedef zig_under_align({d}) ", .{aligned_info.alignas.toByteUnits()});
2704 try bw.print("{f}", .{try renderTypePrefix(
2703 try w.print("typedef zig_under_align({d}) ", .{aligned_info.alignas.toByteUnits()});
2704 try w.print("{f}", .{try renderTypePrefix(
27052705 .flush,
27062706 global_ctype_pool,
27072707 zcu,
2708 bw,
2708 w,
27092709 aligned_info.ctype,
27102710 .suffix,
27112711 .{},
27122712 )});
2713 try renderAlignedTypeName(bw, global_ctype);
2714 try renderTypeSuffix(.flush, global_ctype_pool, zcu, bw, aligned_info.ctype, .suffix, .{});
2715 try bw.writeAll(";\n");
2713 try renderAlignedTypeName(w, global_ctype);
2714 try renderTypeSuffix(.flush, global_ctype_pool, zcu, w, aligned_info.ctype, .suffix, .{});
2715 try w.writeAll(";\n");
27162716 }
27172717 switch (pass) {
27182718 .nav, .uav => {
2719 try bw.writeAll("typedef ");
2720 _ = try renderTypePrefix(.flush, global_ctype_pool, zcu, bw, global_ctype, .suffix, .{});
2721 try bw.writeByte(' ');
2722 _ = try renderTypePrefix(pass, decl_ctype_pool, zcu, bw, decl_ctype, .suffix, .{});
2723 try bw.writeAll(";\n");
2719 try w.writeAll("typedef ");
2720 _ = try renderTypePrefix(.flush, global_ctype_pool, zcu, w, global_ctype, .suffix, .{});
2721 try w.writeByte(' ');
2722 _ = try renderTypePrefix(pass, decl_ctype_pool, zcu, w, decl_ctype, .suffix, .{});
2723 try w.writeAll(";\n");
27242724 },
27252725 .flush => {},
27262726 }
......@@ -2728,24 +2728,24 @@ pub fn genTypeDecl(
27282728 .fwd_decl => |fwd_decl_info| switch (fwd_decl_info.name) {
27292729 .anon => switch (pass) {
27302730 .nav, .uav => {
2731 try bw.writeAll("typedef ");
2732 _ = try renderTypePrefix(.flush, global_ctype_pool, zcu, bw, global_ctype, .suffix, .{});
2733 try bw.writeByte(' ');
2734 _ = try renderTypePrefix(pass, decl_ctype_pool, zcu, bw, decl_ctype, .suffix, .{});
2735 try bw.writeAll(";\n");
2731 try w.writeAll("typedef ");
2732 _ = try renderTypePrefix(.flush, global_ctype_pool, zcu, w, global_ctype, .suffix, .{});
2733 try w.writeByte(' ');
2734 _ = try renderTypePrefix(pass, decl_ctype_pool, zcu, w, decl_ctype, .suffix, .{});
2735 try w.writeAll(";\n");
27362736 },
27372737 .flush => {},
27382738 },
27392739 .index => |index| if (!found_existing) {
27402740 const ip = &zcu.intern_pool;
27412741 const ty: Type = .fromInterned(index);
2742 _ = try renderTypePrefix(.flush, global_ctype_pool, zcu, bw, global_ctype, .suffix, .{});
2743 try bw.writeByte(';');
2742 _ = try renderTypePrefix(.flush, global_ctype_pool, zcu, w, global_ctype, .suffix, .{});
2743 try w.writeByte(';');
27442744 const file_scope = ty.typeDeclInstAllowGeneratedTag(zcu).?.resolveFile(ip);
2745 if (!zcu.fileByIndex(file_scope).mod.?.strip) try bw.print(" /* {f} */", .{
2745 if (!zcu.fileByIndex(file_scope).mod.?.strip) try w.print(" /* {f} */", .{
27462746 ty.containerTypeName(ip).fmt(ip),
27472747 });
2748 try bw.writeByte('\n');
2748 try w.writeByte('\n');
27492749 },
27502750 },
27512751 .aggregate => |aggregate_info| switch (aggregate_info.name) {
......@@ -2753,23 +2753,23 @@ pub fn genTypeDecl(
27532753 .fwd_decl => |fwd_decl| if (!found_existing) {
27542754 try renderFwdDeclTypeName(
27552755 zcu,
2756 bw,
2756 w,
27572757 fwd_decl,
27582758 fwd_decl.info(global_ctype_pool).fwd_decl,
27592759 if (aggregate_info.@"packed") "zig_packed(" else "",
27602760 );
2761 try bw.writeByte(' ');
2762 try renderFields(zcu, bw, global_ctype_pool, aggregate_info, 0);
2763 if (aggregate_info.@"packed") try bw.writeByte(')');
2764 try bw.writeAll(";\n");
2761 try w.writeByte(' ');
2762 try renderFields(zcu, w, global_ctype_pool, aggregate_info, 0);
2763 if (aggregate_info.@"packed") try w.writeByte(')');
2764 try w.writeAll(";\n");
27652765 },
27662766 },
27672767 }
27682768}
27692769
2770pub fn genGlobalAsm(zcu: *Zcu, bw: *Writer) !void {
2770pub fn genGlobalAsm(zcu: *Zcu, w: *Writer) !void {
27712771 for (zcu.global_assembly.values()) |asm_source| {
2772 try bw.print("__asm({fs});\n", .{fmtStringLiteral(asm_source, null)});
2772 try w.print("__asm({fs});\n", .{fmtStringLiteral(asm_source, null)});
27732773 }
27742774}
27752775
......@@ -2777,13 +2777,13 @@ pub fn genErrDecls(o: *Object) Error!void {
27772777 const pt = o.dg.pt;
27782778 const zcu = pt.zcu;
27792779 const ip = &zcu.intern_pool;
2780 const bw = &o.code.buffered_writer;
2780 const w = &o.code.buffered_writer;
27812781
27822782 var max_name_len: usize = 0;
27832783 // do not generate an invalid empty enum when the global error set is empty
27842784 const names = ip.global_error_set.getNamesFromMainThread();
27852785 if (names.len > 0) {
2786 try bw.writeAll("enum {");
2786 try w.writeAll("enum {");
27872787 o.indent();
27882788 try o.newline();
27892789 for (names, 1..) |name_nts, value| {
......@@ -2793,12 +2793,12 @@ pub fn genErrDecls(o: *Object) Error!void {
27932793 .ty = .anyerror_type,
27942794 .name = name_nts,
27952795 } });
2796 try o.dg.renderValue(bw, Value.fromInterned(err_val), .Other);
2797 try bw.print(" = {d}u,", .{value});
2796 try o.dg.renderValue(w, Value.fromInterned(err_val), .Other);
2797 try w.print(" = {d}u,", .{value});
27982798 try o.newline();
27992799 }
28002800 try o.outdent();
2801 try bw.writeAll("};");
2801 try w.writeAll("};");
28022802 try o.newline();
28032803 }
28042804 const array_identifier = "zig_errorName";
......@@ -2822,18 +2822,18 @@ pub fn genErrDecls(o: *Object) Error!void {
28222822 .storage = .{ .bytes = name.toString() },
28232823 } });
28242824
2825 try bw.writeAll("static ");
2825 try w.writeAll("static ");
28262826 try o.dg.renderTypeAndName(
2827 bw,
2827 w,
28282828 name_ty,
28292829 .{ .identifier = identifier },
28302830 Const,
28312831 .none,
28322832 .complete,
28332833 );
2834 try bw.writeAll(" = ");
2835 try o.dg.renderValue(bw, Value.fromInterned(name_val), .StaticInitializer);
2836 try bw.writeByte(';');
2834 try w.writeAll(" = ");
2835 try o.dg.renderValue(w, Value.fromInterned(name_val), .StaticInitializer);
2836 try w.writeByte(';');
28372837 try o.newline();
28382838 }
28392839
......@@ -2842,25 +2842,25 @@ pub fn genErrDecls(o: *Object) Error!void {
28422842 .child = .slice_const_u8_sentinel_0_type,
28432843 });
28442844
2845 try bw.writeAll("static ");
2845 try w.writeAll("static ");
28462846 try o.dg.renderTypeAndName(
2847 bw,
2847 w,
28482848 name_array_ty,
28492849 .{ .identifier = array_identifier },
28502850 Const,
28512851 .none,
28522852 .complete,
28532853 );
2854 try bw.writeAll(" = {");
2854 try w.writeAll(" = {");
28552855 for (names, 1..) |name_nts, val| {
28562856 const name = name_nts.toSlice(ip);
2857 if (val > 1) try bw.writeAll(", ");
2858 try bw.print("{{" ++ name_prefix ++ "{f}, {f}}}", .{
2857 if (val > 1) try w.writeAll(", ");
2858 try w.print("{{" ++ name_prefix ++ "{f}, {f}}}", .{
28592859 fmtIdent(name),
28602860 try o.dg.fmtIntLiteral(try pt.intValue(.usize, name.len), .StaticInitializer),
28612861 });
28622862 }
2863 try bw.writeAll("};");
2863 try w.writeAll("};");
28642864 try o.newline();
28652865}
28662866
......@@ -2869,7 +2869,7 @@ pub fn genLazyFn(o: *Object, lazy_ctype_pool: *const CType.Pool, lazy_fn: LazyFn
28692869 const zcu = pt.zcu;
28702870 const ip = &zcu.intern_pool;
28712871 const ctype_pool = &o.dg.ctype_pool;
2872 const bw = &o.code.buffered_writer;
2872 const w = &o.code.buffered_writer;
28732873 const key = lazy_fn.key_ptr.*;
28742874 const val = lazy_fn.value_ptr;
28752875 switch (key) {
......@@ -2877,14 +2877,14 @@ pub fn genLazyFn(o: *Object, lazy_ctype_pool: *const CType.Pool, lazy_fn: LazyFn
28772877 const enum_ty: Type = .fromInterned(enum_ty_ip);
28782878 const name_slice_ty: Type = .slice_const_u8_sentinel_0;
28792879
2880 try bw.writeAll("static ");
2881 try o.dg.renderType(bw, name_slice_ty);
2882 try bw.print(" {f}(", .{val.fn_name.fmt(lazy_ctype_pool)});
2883 try o.dg.renderTypeAndName(bw, enum_ty, .{ .identifier = "tag" }, Const, .none, .complete);
2884 try bw.writeAll(") {");
2880 try w.writeAll("static ");
2881 try o.dg.renderType(w, name_slice_ty);
2882 try w.print(" {f}(", .{val.fn_name.fmt(lazy_ctype_pool)});
2883 try o.dg.renderTypeAndName(w, enum_ty, .{ .identifier = "tag" }, Const, .none, .complete);
2884 try w.writeAll(") {");
28852885 o.indent();
28862886 try o.newline();
2887 try bw.writeAll("switch (tag) {");
2887 try w.writeAll("switch (tag) {");
28882888 o.indent();
28892889 try o.newline();
28902890 const tag_names = enum_ty.enumFields(zcu);
......@@ -2903,34 +2903,34 @@ pub fn genLazyFn(o: *Object, lazy_ctype_pool: *const CType.Pool, lazy_fn: LazyFn
29032903 .storage = .{ .bytes = tag_name.toString() },
29042904 } });
29052905
2906 try bw.print("case {f}: {{", .{
2906 try w.print("case {f}: {{", .{
29072907 try o.dg.fmtIntLiteral(try tag_val.intFromEnum(enum_ty, pt), .Other),
29082908 });
29092909 o.indent();
29102910 try o.newline();
2911 try bw.writeAll("static ");
2912 try o.dg.renderTypeAndName(bw, name_ty, .{ .identifier = "name" }, Const, .none, .complete);
2913 try bw.writeAll(" = ");
2914 try o.dg.renderValue(bw, Value.fromInterned(name_val), .StaticInitializer);
2915 try bw.writeByte(';');
2911 try w.writeAll("static ");
2912 try o.dg.renderTypeAndName(w, name_ty, .{ .identifier = "name" }, Const, .none, .complete);
2913 try w.writeAll(" = ");
2914 try o.dg.renderValue(w, Value.fromInterned(name_val), .StaticInitializer);
2915 try w.writeByte(';');
29162916 try o.newline();
2917 try bw.writeAll("return (");
2918 try o.dg.renderType(bw, name_slice_ty);
2919 try bw.print("){{{f}, {f}}};", .{
2917 try w.writeAll("return (");
2918 try o.dg.renderType(w, name_slice_ty);
2919 try w.print("){{{f}, {f}}};", .{
29202920 fmtIdent("name"),
29212921 try o.dg.fmtIntLiteral(try pt.intValue(.usize, tag_name_len), .Other),
29222922 });
29232923 try o.newline();
29242924 try o.outdent();
2925 try bw.writeByte('}');
2925 try w.writeByte('}');
29262926 try o.newline();
29272927 }
29282928 try o.outdent();
2929 try bw.writeByte('}');
2929 try w.writeByte('}');
29302930 try o.newline();
29312931 try airUnreach(o);
29322932 try o.outdent();
2933 try bw.writeByte('}');
2933 try w.writeByte('}');
29342934 try o.newline();
29352935 },
29362936 .never_tail, .never_inline => |fn_nav_index| {
......@@ -2946,24 +2946,24 @@ pub fn genLazyFn(o: *Object, lazy_ctype_pool: *const CType.Pool, lazy_fn: LazyFn
29462946 });
29472947 try fwd.writeAll(";\n");
29482948
2949 try bw.print("zig_{s} ", .{@tagName(key)});
2950 try o.dg.renderFunctionSignature(bw, fn_val, .none, .complete, .{
2949 try w.print("zig_{s} ", .{@tagName(key)});
2950 try o.dg.renderFunctionSignature(w, fn_val, .none, .complete, .{
29512951 .fmt_ctype_pool_string = fn_name,
29522952 });
2953 try bw.writeAll(" {");
2953 try w.writeAll(" {");
29542954 o.indent();
29552955 try o.newline();
2956 try bw.writeAll("return ");
2957 try o.dg.renderNavName(bw, fn_nav_index);
2958 try bw.writeByte('(');
2956 try w.writeAll("return ");
2957 try o.dg.renderNavName(w, fn_nav_index);
2958 try w.writeByte('(');
29592959 for (0..fn_info.param_ctypes.len) |arg| {
2960 if (arg > 0) try bw.writeAll(", ");
2961 try bw.print("a{d}", .{arg});
2960 if (arg > 0) try w.writeAll(", ");
2961 try w.print("a{d}", .{arg});
29622962 }
2963 try bw.writeAll(");");
2963 try w.writeAll(");");
29642964 try o.newline();
29652965 try o.outdent();
2966 try bw.writeByte('}');
2966 try w.writeByte('}');
29672967 try o.newline();
29682968 },
29692969 }
......@@ -3164,21 +3164,21 @@ pub fn genDecl(o: *Object) Error!void {
31643164 .linkage = .internal,
31653165 .visibility = .default,
31663166 });
3167 const bw = &o.code.buffered_writer;
3168 if (variable.is_threadlocal and !o.dg.mod.single_threaded) try bw.writeAll("zig_threadlocal ");
3167 const w = &o.code.buffered_writer;
3168 if (variable.is_threadlocal and !o.dg.mod.single_threaded) try w.writeAll("zig_threadlocal ");
31693169 if (nav.status.fully_resolved.@"linksection".toSlice(&zcu.intern_pool)) |s|
3170 try bw.print("zig_linksection({fs}) ", .{fmtStringLiteral(s, null)});
3170 try w.print("zig_linksection({fs}) ", .{fmtStringLiteral(s, null)});
31713171 try o.dg.renderTypeAndName(
3172 bw,
3172 w,
31733173 nav_ty,
31743174 .{ .nav = o.dg.pass.nav },
31753175 .{},
31763176 nav.status.fully_resolved.alignment,
31773177 .complete,
31783178 );
3179 try bw.writeAll(" = ");
3180 try o.dg.renderValue(bw, Value.fromInterned(variable.init), .StaticInitializer);
3181 try bw.writeByte(';');
3179 try w.writeAll(" = ");
3180 try o.dg.renderValue(w, Value.fromInterned(variable.init), .StaticInitializer);
3181 try w.writeByte(';');
31823182 try o.newline();
31833183 },
31843184 else => try genDeclValue(
......@@ -3206,13 +3206,13 @@ pub fn genDeclValue(
32063206 try o.dg.renderTypeAndName(fwd, ty, decl_c_value, Const, alignment, .complete);
32073207 try fwd.writeAll(";\n");
32083208
3209 const bw = &o.code.buffered_writer;
3209 const w = &o.code.buffered_writer;
32103210 if (@"linksection".toSlice(&zcu.intern_pool)) |s|
3211 try bw.print("zig_linksection({fs}) ", .{fmtStringLiteral(s, null)});
3212 try o.dg.renderTypeAndName(bw, ty, decl_c_value, Const, alignment, .complete);
3213 try bw.writeAll(" = ");
3214 try o.dg.renderValue(bw, val, .StaticInitializer);
3215 try bw.writeByte(';');
3211 try w.print("zig_linksection({fs}) ", .{fmtStringLiteral(s, null)});
3212 try o.dg.renderTypeAndName(w, ty, decl_c_value, Const, alignment, .complete);
3213 try w.writeAll(" = ");
3214 try o.dg.renderValue(w, val, .StaticInitializer);
3215 try w.writeByte(';');
32163216 try o.newline();
32173217}
32183218
......@@ -3297,16 +3297,16 @@ pub fn genExports(dg: *DeclGen, exported: Zcu.Exported, export_indices: []const
32973297/// have been added to `free_locals_map`. For a version of this function that restores this state,
32983298/// see `genBodyResolveState`.
32993299fn genBody(f: *Function, body: []const Air.Inst.Index) Error!void {
3300 const bw = &f.object.code.buffered_writer;
3300 const w = &f.object.code.buffered_writer;
33013301 if (body.len == 0) {
3302 try bw.writeAll("{}");
3302 try w.writeAll("{}");
33033303 } else {
3304 try bw.writeByte('{');
3304 try w.writeByte('{');
33053305 f.object.indent();
33063306 try f.object.newline();
33073307 try genBodyInner(f, body);
33083308 try f.object.outdent();
3309 try bw.writeByte('}');
3309 try w.writeByte('}');
33103310 }
33113311}
33123312
......@@ -3680,16 +3680,16 @@ fn airSliceField(f: *Function, inst: Air.Inst.Index, is_ptr: bool, field_name: [
36803680 const operand = try f.resolveInst(ty_op.operand);
36813681 try reap(f, inst, &.{ty_op.operand});
36823682
3683 const bw = &f.object.code.buffered_writer;
3683 const w = &f.object.code.buffered_writer;
36843684 const local = try f.allocLocal(inst, inst_ty);
3685 const a = try Assignment.start(f, bw, try f.ctypeFromType(inst_ty, .complete));
3686 try f.writeCValue(bw, local, .Other);
3687 try a.assign(f, bw);
3685 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete));
3686 try f.writeCValue(w, local, .Other);
3687 try a.assign(f, w);
36883688 if (is_ptr) {
3689 try bw.writeByte('&');
3690 try f.writeCValueDerefMember(bw, operand, .{ .identifier = field_name });
3691 } else try f.writeCValueMember(bw, operand, .{ .identifier = field_name });
3692 try a.end(f, bw);
3689 try w.writeByte('&');
3690 try f.writeCValueDerefMember(w, operand, .{ .identifier = field_name });
3691 } else try f.writeCValueMember(w, operand, .{ .identifier = field_name });
3692 try a.end(f, w);
36933693 return local;
36943694}
36953695
......@@ -3706,16 +3706,16 @@ fn airPtrElemVal(f: *Function, inst: Air.Inst.Index) !CValue {
37063706 const index = try f.resolveInst(bin_op.rhs);
37073707 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });
37083708
3709 const bw = &f.object.code.buffered_writer;
3709 const w = &f.object.code.buffered_writer;
37103710 const local = try f.allocLocal(inst, inst_ty);
3711 const a = try Assignment.start(f, bw, try f.ctypeFromType(inst_ty, .complete));
3712 try f.writeCValue(bw, local, .Other);
3713 try a.assign(f, bw);
3714 try f.writeCValue(bw, ptr, .Other);
3715 try bw.writeByte('[');
3716 try f.writeCValue(bw, index, .Other);
3717 try bw.writeByte(']');
3718 try a.end(f, bw);
3711 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete));
3712 try f.writeCValue(w, local, .Other);
3713 try a.assign(f, w);
3714 try f.writeCValue(w, ptr, .Other);
3715 try w.writeByte('[');
3716 try f.writeCValue(w, index, .Other);
3717 try w.writeByte(']');
3718 try a.end(f, w);
37193719 return local;
37203720}
37213721
......@@ -3733,25 +3733,25 @@ fn airPtrElemPtr(f: *Function, inst: Air.Inst.Index) !CValue {
37333733 const index = try f.resolveInst(bin_op.rhs);
37343734 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });
37353735
3736 const bw = &f.object.code.buffered_writer;
3736 const w = &f.object.code.buffered_writer;
37373737 const local = try f.allocLocal(inst, inst_ty);
3738 const a = try Assignment.start(f, bw, try f.ctypeFromType(inst_ty, .complete));
3739 try f.writeCValue(bw, local, .Other);
3740 try a.assign(f, bw);
3741 try bw.writeByte('(');
3742 try f.renderType(bw, inst_ty);
3743 try bw.writeByte(')');
3744 if (elem_has_bits) try bw.writeByte('&');
3738 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete));
3739 try f.writeCValue(w, local, .Other);
3740 try a.assign(f, w);
3741 try w.writeByte('(');
3742 try f.renderType(w, inst_ty);
3743 try w.writeByte(')');
3744 if (elem_has_bits) try w.writeByte('&');
37453745 if (elem_has_bits and ptr_ty.ptrSize(zcu) == .one) {
37463746 // It's a pointer to an array, so we need to de-reference.
3747 try f.writeCValueDeref(bw, ptr);
3748 } else try f.writeCValue(bw, ptr, .Other);
3747 try f.writeCValueDeref(w, ptr);
3748 } else try f.writeCValue(w, ptr, .Other);
37493749 if (elem_has_bits) {
3750 try bw.writeByte('[');
3751 try f.writeCValue(bw, index, .Other);
3752 try bw.writeByte(']');
3750 try w.writeByte('[');
3751 try f.writeCValue(w, index, .Other);
3752 try w.writeByte(']');
37533753 }
3754 try a.end(f, bw);
3754 try a.end(f, w);
37553755 return local;
37563756}
37573757
......@@ -3768,16 +3768,16 @@ fn airSliceElemVal(f: *Function, inst: Air.Inst.Index) !CValue {
37683768 const index = try f.resolveInst(bin_op.rhs);
37693769 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });
37703770
3771 const bw = &f.object.code.buffered_writer;
3771 const w = &f.object.code.buffered_writer;
37723772 const local = try f.allocLocal(inst, inst_ty);
3773 const a = try Assignment.start(f, bw, try f.ctypeFromType(inst_ty, .complete));
3774 try f.writeCValue(bw, local, .Other);
3775 try a.assign(f, bw);
3776 try f.writeCValueMember(bw, slice, .{ .identifier = "ptr" });
3777 try bw.writeByte('[');
3778 try f.writeCValue(bw, index, .Other);
3779 try bw.writeByte(']');
3780 try a.end(f, bw);
3773 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete));
3774 try f.writeCValue(w, local, .Other);
3775 try a.assign(f, w);
3776 try f.writeCValueMember(w, slice, .{ .identifier = "ptr" });
3777 try w.writeByte('[');
3778 try f.writeCValue(w, index, .Other);
3779 try w.writeByte(']');
3780 try a.end(f, w);
37813781 return local;
37823782}
37833783
......@@ -3796,19 +3796,19 @@ fn airSliceElemPtr(f: *Function, inst: Air.Inst.Index) !CValue {
37963796 const index = try f.resolveInst(bin_op.rhs);
37973797 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });
37983798
3799 const bw = &f.object.code.buffered_writer;
3799 const w = &f.object.code.buffered_writer;
38003800 const local = try f.allocLocal(inst, inst_ty);
3801 const a = try Assignment.start(f, bw, try f.ctypeFromType(inst_ty, .complete));
3802 try f.writeCValue(bw, local, .Other);
3803 try a.assign(f, bw);
3804 if (elem_has_bits) try bw.writeByte('&');
3805 try f.writeCValueMember(bw, slice, .{ .identifier = "ptr" });
3801 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete));
3802 try f.writeCValue(w, local, .Other);
3803 try a.assign(f, w);
3804 if (elem_has_bits) try w.writeByte('&');
3805 try f.writeCValueMember(w, slice, .{ .identifier = "ptr" });
38063806 if (elem_has_bits) {
3807 try bw.writeByte('[');
3808 try f.writeCValue(bw, index, .Other);
3809 try bw.writeByte(']');
3807 try w.writeByte('[');
3808 try f.writeCValue(w, index, .Other);
3809 try w.writeByte(']');
38103810 }
3811 try a.end(f, bw);
3811 try a.end(f, w);
38123812 return local;
38133813}
38143814
......@@ -3825,16 +3825,16 @@ fn airArrayElemVal(f: *Function, inst: Air.Inst.Index) !CValue {
38253825 const index = try f.resolveInst(bin_op.rhs);
38263826 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });
38273827
3828 const bw = &f.object.code.buffered_writer;
3828 const w = &f.object.code.buffered_writer;
38293829 const local = try f.allocLocal(inst, inst_ty);
3830 const a = try Assignment.start(f, bw, try f.ctypeFromType(inst_ty, .complete));
3831 try f.writeCValue(bw, local, .Other);
3832 try a.assign(f, bw);
3833 try f.writeCValue(bw, array, .Other);
3834 try bw.writeByte('[');
3835 try f.writeCValue(bw, index, .Other);
3836 try bw.writeByte(']');
3837 try a.end(f, bw);
3830 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete));
3831 try f.writeCValue(w, local, .Other);
3832 try a.assign(f, w);
3833 try f.writeCValue(w, array, .Other);
3834 try w.writeByte('[');
3835 try f.writeCValue(w, index, .Other);
3836 try w.writeByte(']');
3837 try a.end(f, w);
38383838 return local;
38393839}
38403840
......@@ -3888,12 +3888,12 @@ fn airArg(f: *Function, inst: Air.Inst.Index) !CValue {
38883888 .{ .arg_array = i };
38893889
38903890 if (f.liveness.isUnused(inst)) {
3891 const bw = &f.object.code.buffered_writer;
3892 try bw.writeByte('(');
3893 try f.renderType(bw, .void);
3894 try bw.writeByte(')');
3895 try f.writeCValue(bw, result, .Other);
3896 try bw.writeByte(';');
3891 const w = &f.object.code.buffered_writer;
3892 try w.writeByte('(');
3893 try f.renderType(w, .void);
3894 try w.writeByte(')');
3895 try f.writeCValue(w, result, .Other);
3896 try w.writeByte(';');
38973897 try f.object.newline();
38983898 return .none;
38993899 }
......@@ -3927,21 +3927,21 @@ fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue {
39273927 const is_array = lowersToArray(src_ty, pt);
39283928 const need_memcpy = !is_aligned or is_array;
39293929
3930 const bw = &f.object.code.buffered_writer;
3930 const w = &f.object.code.buffered_writer;
39313931 const local = try f.allocLocal(inst, src_ty);
3932 const v = try Vectorize.start(f, inst, bw, ptr_ty);
3932 const v = try Vectorize.start(f, inst, w, ptr_ty);
39333933
39343934 if (need_memcpy) {
3935 try bw.writeAll("memcpy(");
3936 if (!is_array) try bw.writeByte('&');
3937 try f.writeCValue(bw, local, .Other);
3938 try v.elem(f, bw);
3939 try bw.writeAll(", (const char *)");
3940 try f.writeCValue(bw, operand, .Other);
3941 try v.elem(f, bw);
3942 try bw.writeAll(", sizeof(");
3943 try f.renderType(bw, src_ty);
3944 try bw.writeAll("))");
3935 try w.writeAll("memcpy(");
3936 if (!is_array) try w.writeByte('&');
3937 try f.writeCValue(w, local, .Other);
3938 try v.elem(f, w);
3939 try w.writeAll(", (const char *)");
3940 try f.writeCValue(w, operand, .Other);
3941 try v.elem(f, w);
3942 try w.writeAll(", sizeof(");
3943 try f.renderType(w, src_ty);
3944 try w.writeAll("))");
39453945 } else if (ptr_info.packed_offset.host_size > 0 and ptr_info.flags.vector_index == .none) {
39463946 const host_bits: u16 = ptr_info.packed_offset.host_size * 8;
39473947 const host_ty = try pt.intType(.unsigned, host_bits);
......@@ -3951,41 +3951,41 @@ fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue {
39513951
39523952 const field_ty = try pt.intType(.unsigned, @as(u16, @intCast(src_ty.bitSize(zcu))));
39533953
3954 try f.writeCValue(bw, local, .Other);
3955 try v.elem(f, bw);
3956 try bw.writeAll(" = (");
3957 try f.renderType(bw, src_ty);
3958 try bw.writeAll(")zig_wrap_");
3959 try f.object.dg.renderTypeForBuiltinFnName(bw, field_ty);
3960 try bw.writeAll("((");
3961 try f.renderType(bw, field_ty);
3962 try bw.writeByte(')');
3954 try f.writeCValue(w, local, .Other);
3955 try v.elem(f, w);
3956 try w.writeAll(" = (");
3957 try f.renderType(w, src_ty);
3958 try w.writeAll(")zig_wrap_");
3959 try f.object.dg.renderTypeForBuiltinFnName(w, field_ty);
3960 try w.writeAll("((");
3961 try f.renderType(w, field_ty);
3962 try w.writeByte(')');
39633963 const cant_cast = host_ty.isInt(zcu) and host_ty.bitSize(zcu) > 64;
39643964 if (cant_cast) {
39653965 if (field_ty.bitSize(zcu) > 64) return f.fail("TODO: C backend: implement casting between types > 64 bits", .{});
3966 try bw.writeAll("zig_lo_");
3967 try f.object.dg.renderTypeForBuiltinFnName(bw, host_ty);
3968 try bw.writeByte('(');
3966 try w.writeAll("zig_lo_");
3967 try f.object.dg.renderTypeForBuiltinFnName(w, host_ty);
3968 try w.writeByte('(');
39693969 }
3970 try bw.writeAll("zig_shr_");
3971 try f.object.dg.renderTypeForBuiltinFnName(bw, host_ty);
3972 try bw.writeByte('(');
3973 try f.writeCValueDeref(bw, operand);
3974 try v.elem(f, bw);
3975 try bw.print(", {f})", .{try f.fmtIntLiteral(bit_offset_val)});
3976 if (cant_cast) try bw.writeByte(')');
3977 try f.object.dg.renderBuiltinInfo(bw, field_ty, .bits);
3978 try bw.writeByte(')');
3970 try w.writeAll("zig_shr_");
3971 try f.object.dg.renderTypeForBuiltinFnName(w, host_ty);
3972 try w.writeByte('(');
3973 try f.writeCValueDeref(w, operand);
3974 try v.elem(f, w);
3975 try w.print(", {f})", .{try f.fmtIntLiteral(bit_offset_val)});
3976 if (cant_cast) try w.writeByte(')');
3977 try f.object.dg.renderBuiltinInfo(w, field_ty, .bits);
3978 try w.writeByte(')');
39793979 } else {
3980 try f.writeCValue(bw, local, .Other);
3981 try v.elem(f, bw);
3982 try bw.writeAll(" = ");
3983 try f.writeCValueDeref(bw, operand);
3984 try v.elem(f, bw);
3980 try f.writeCValue(w, local, .Other);
3981 try v.elem(f, w);
3982 try w.writeAll(" = ");
3983 try f.writeCValueDeref(w, operand);
3984 try v.elem(f, w);
39853985 }
3986 try bw.writeByte(';');
3986 try w.writeByte(';');
39873987 try f.object.newline();
3988 try v.end(f, inst, bw);
3988 try v.end(f, inst, w);
39893989
39903990 return local;
39913991}
......@@ -3994,7 +3994,7 @@ fn airRet(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !void {
39943994 const pt = f.object.dg.pt;
39953995 const zcu = pt.zcu;
39963996 const un_op = f.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
3997 const bw = &f.object.code.buffered_writer;
3997 const w = &f.object.code.buffered_writer;
39983998 const op_inst = un_op.toIndex();
39993999 const op_ty = f.typeOf(un_op);
40004000 const ret_ty = if (is_ptr) op_ty.childType(zcu) else op_ty;
......@@ -4013,34 +4013,34 @@ fn airRet(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !void {
40134013 .ctype = ret_ctype,
40144014 .alignas = CType.AlignAs.fromAbiAlignment(ret_ty.abiAlignment(zcu)),
40154015 });
4016 try bw.writeAll("memcpy(");
4017 try f.writeCValueMember(bw, array_local, .{ .identifier = "array" });
4018 try bw.writeAll(", ");
4016 try w.writeAll("memcpy(");
4017 try f.writeCValueMember(w, array_local, .{ .identifier = "array" });
4018 try w.writeAll(", ");
40194019 if (deref)
4020 try f.writeCValueDeref(bw, operand)
4020 try f.writeCValueDeref(w, operand)
40214021 else
4022 try f.writeCValue(bw, operand, .FunctionArgument);
4022 try f.writeCValue(w, operand, .FunctionArgument);
40234023 deref = false;
4024 try bw.writeAll(", sizeof(");
4025 try f.renderType(bw, ret_ty);
4026 try bw.writeAll("));");
4024 try w.writeAll(", sizeof(");
4025 try f.renderType(w, ret_ty);
4026 try w.writeAll("));");
40274027 try f.object.newline();
40284028 break :ret_val array_local;
40294029 } else operand;
40304030
4031 try bw.writeAll("return ");
4031 try w.writeAll("return ");
40324032 if (deref)
4033 try f.writeCValueDeref(bw, ret_val)
4033 try f.writeCValueDeref(w, ret_val)
40344034 else
4035 try f.writeCValue(bw, ret_val, .Other);
4036 try bw.writeAll(";\n");
4035 try f.writeCValue(w, ret_val, .Other);
4036 try w.writeAll(";\n");
40374037 if (is_array) {
40384038 try freeLocal(f, inst, ret_val.new_local, null);
40394039 }
40404040 } else {
40414041 try reap(f, inst, &.{un_op});
40424042 // Not even allowed to return void in a naked function.
4043 if (!f.object.dg.is_naked_fn) try bw.writeAll("return;\n");
4043 if (!f.object.dg.is_naked_fn) try w.writeAll("return;\n");
40444044 }
40454045}
40464046
......@@ -4059,16 +4059,16 @@ fn airIntCast(f: *Function, inst: Air.Inst.Index) !CValue {
40594059
40604060 if (f.object.dg.intCastIsNoop(inst_scalar_ty, scalar_ty)) return f.moveCValue(inst, inst_ty, operand);
40614061
4062 const bw = &f.object.code.buffered_writer;
4062 const w = &f.object.code.buffered_writer;
40634063 const local = try f.allocLocal(inst, inst_ty);
4064 const v = try Vectorize.start(f, inst, bw, operand_ty);
4065 const a = try Assignment.start(f, bw, try f.ctypeFromType(scalar_ty, .complete));
4066 try f.writeCValue(bw, local, .Other);
4067 try v.elem(f, bw);
4068 try a.assign(f, bw);
4069 try f.renderIntCast(bw, inst_scalar_ty, operand, v, scalar_ty, .Other);
4070 try a.end(f, bw);
4071 try v.end(f, inst, bw);
4064 const v = try Vectorize.start(f, inst, w, operand_ty);
4065 const a = try Assignment.start(f, w, try f.ctypeFromType(scalar_ty, .complete));
4066 try f.writeCValue(w, local, .Other);
4067 try v.elem(f, w);
4068 try a.assign(f, w);
4069 try f.renderIntCast(w, inst_scalar_ty, operand, v, scalar_ty, .Other);
4070 try a.end(f, w);
4071 try v.end(f, inst, w);
40724072 return local;
40734073}
40744074
......@@ -4095,34 +4095,34 @@ fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue {
40954095 const need_mask = dest_bits < 8 or !std.math.isPowerOfTwo(dest_bits);
40964096 if (!need_cast and !need_lo and !need_mask) return f.moveCValue(inst, inst_ty, operand);
40974097
4098 const bw = &f.object.code.buffered_writer;
4098 const w = &f.object.code.buffered_writer;
40994099 const local = try f.allocLocal(inst, inst_ty);
4100 const v = try Vectorize.start(f, inst, bw, operand_ty);
4101 const a = try Assignment.start(f, bw, try f.ctypeFromType(inst_scalar_ty, .complete));
4102 try f.writeCValue(bw, local, .Other);
4103 try v.elem(f, bw);
4104 try a.assign(f, bw);
4100 const v = try Vectorize.start(f, inst, w, operand_ty);
4101 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_scalar_ty, .complete));
4102 try f.writeCValue(w, local, .Other);
4103 try v.elem(f, w);
4104 try a.assign(f, w);
41054105 if (need_cast) {
4106 try bw.writeByte('(');
4107 try f.renderType(bw, inst_scalar_ty);
4108 try bw.writeByte(')');
4106 try w.writeByte('(');
4107 try f.renderType(w, inst_scalar_ty);
4108 try w.writeByte(')');
41094109 }
41104110 if (need_lo) {
4111 try bw.writeAll("zig_lo_");
4112 try f.object.dg.renderTypeForBuiltinFnName(bw, scalar_ty);
4113 try bw.writeByte('(');
4111 try w.writeAll("zig_lo_");
4112 try f.object.dg.renderTypeForBuiltinFnName(w, scalar_ty);
4113 try w.writeByte('(');
41144114 }
41154115 if (!need_mask) {
4116 try f.writeCValue(bw, operand, .Other);
4117 try v.elem(f, bw);
4116 try f.writeCValue(w, operand, .Other);
4117 try v.elem(f, w);
41184118 } else switch (dest_int_info.signedness) {
41194119 .unsigned => {
4120 try bw.writeAll("zig_and_");
4121 try f.object.dg.renderTypeForBuiltinFnName(bw, scalar_ty);
4122 try bw.writeByte('(');
4123 try f.writeCValue(bw, operand, .FunctionArgument);
4124 try v.elem(f, bw);
4125 try bw.print(", {fx})", .{
4120 try w.writeAll("zig_and_");
4121 try f.object.dg.renderTypeForBuiltinFnName(w, scalar_ty);
4122 try w.writeByte('(');
4123 try f.writeCValue(w, operand, .FunctionArgument);
4124 try v.elem(f, w);
4125 try w.print(", {fx})", .{
41264126 try f.fmtIntLiteral(try inst_scalar_ty.maxIntScalar(pt, scalar_ty)),
41274127 });
41284128 },
......@@ -4131,30 +4131,30 @@ fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue {
41314131 return f.fail("TODO: C backend: implement integer types larger than 128 bits", .{});
41324132 const shift_val = try pt.intValue(.u8, c_bits - dest_bits);
41334133
4134 try bw.writeAll("zig_shr_");
4135 try f.object.dg.renderTypeForBuiltinFnName(bw, scalar_ty);
4134 try w.writeAll("zig_shr_");
4135 try f.object.dg.renderTypeForBuiltinFnName(w, scalar_ty);
41364136 if (c_bits == 128) {
4137 try bw.print("(zig_bitCast_i{d}(", .{c_bits});
4137 try w.print("(zig_bitCast_i{d}(", .{c_bits});
41384138 } else {
4139 try bw.print("((int{d}_t)", .{c_bits});
4139 try w.print("((int{d}_t)", .{c_bits});
41404140 }
4141 try bw.print("zig_shl_u{d}(", .{c_bits});
4141 try w.print("zig_shl_u{d}(", .{c_bits});
41424142 if (c_bits == 128) {
4143 try bw.print("zig_bitCast_u{d}(", .{c_bits});
4143 try w.print("zig_bitCast_u{d}(", .{c_bits});
41444144 } else {
4145 try bw.print("(uint{d}_t)", .{c_bits});
4145 try w.print("(uint{d}_t)", .{c_bits});
41464146 }
4147 try f.writeCValue(bw, operand, .FunctionArgument);
4148 try v.elem(f, bw);
4149 if (c_bits == 128) try bw.writeByte(')');
4150 try bw.print(", {f})", .{try f.fmtIntLiteral(shift_val)});
4151 if (c_bits == 128) try bw.writeByte(')');
4152 try bw.print(", {f})", .{try f.fmtIntLiteral(shift_val)});
4147 try f.writeCValue(w, operand, .FunctionArgument);
4148 try v.elem(f, w);
4149 if (c_bits == 128) try w.writeByte(')');
4150 try w.print(", {f})", .{try f.fmtIntLiteral(shift_val)});
4151 if (c_bits == 128) try w.writeByte(')');
4152 try w.print(", {f})", .{try f.fmtIntLiteral(shift_val)});
41534153 },
41544154 }
4155 if (need_lo) try bw.writeByte(')');
4156 try a.end(f, bw);
4157 try v.end(f, inst, bw);
4155 if (need_lo) try w.writeByte(')');
4156 try a.end(f, w);
4157 try v.end(f, inst, w);
41584158 return local;
41594159}
41604160
......@@ -4173,15 +4173,15 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
41734173
41744174 const val_is_undef = if (try f.air.value(bin_op.rhs, pt)) |v| v.isUndefDeep(zcu) else false;
41754175
4176 const bw = &f.object.code.buffered_writer;
4176 const w = &f.object.code.buffered_writer;
41774177 if (val_is_undef) {
41784178 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });
41794179 if (safety and ptr_info.packed_offset.host_size == 0) {
4180 try bw.writeAll("memset(");
4181 try f.writeCValue(bw, ptr_val, .FunctionArgument);
4182 try bw.writeAll(", 0xaa, sizeof(");
4183 try f.renderType(bw, .fromInterned(ptr_info.child));
4184 try bw.writeAll("));");
4180 try w.writeAll("memset(");
4181 try f.writeCValue(w, ptr_val, .FunctionArgument);
4182 try w.writeAll(", 0xaa, sizeof(");
4183 try f.renderType(w, .fromInterned(ptr_info.child));
4184 try w.writeAll("));");
41854185 try f.object.newline();
41864186 }
41874187 return .none;
......@@ -4208,30 +4208,30 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
42084208 // TODO this should be done by manually initializing elements of the dest array
42094209 const array_src = if (src_val == .constant) blk: {
42104210 const new_local = try f.allocLocal(inst, src_ty);
4211 try f.writeCValue(bw, new_local, .Other);
4212 try bw.writeAll(" = ");
4213 try f.writeCValue(bw, src_val, .Other);
4214 try bw.writeByte(';');
4211 try f.writeCValue(w, new_local, .Other);
4212 try w.writeAll(" = ");
4213 try f.writeCValue(w, src_val, .Other);
4214 try w.writeByte(';');
42154215 try f.object.newline();
42164216
42174217 break :blk new_local;
42184218 } else src_val;
42194219
4220 const v = try Vectorize.start(f, inst, bw, ptr_ty);
4221 try bw.writeAll("memcpy((char *)");
4222 try f.writeCValue(bw, ptr_val, .FunctionArgument);
4223 try v.elem(f, bw);
4224 try bw.writeAll(", ");
4225 if (!is_array) try bw.writeByte('&');
4226 try f.writeCValue(bw, array_src, .FunctionArgument);
4227 try v.elem(f, bw);
4228 try bw.writeAll(", sizeof(");
4229 try f.renderType(bw, src_ty);
4230 try bw.writeAll("))");
4220 const v = try Vectorize.start(f, inst, w, ptr_ty);
4221 try w.writeAll("memcpy((char *)");
4222 try f.writeCValue(w, ptr_val, .FunctionArgument);
4223 try v.elem(f, w);
4224 try w.writeAll(", ");
4225 if (!is_array) try w.writeByte('&');
4226 try f.writeCValue(w, array_src, .FunctionArgument);
4227 try v.elem(f, w);
4228 try w.writeAll(", sizeof(");
4229 try f.renderType(w, src_ty);
4230 try w.writeAll("))");
42314231 try f.freeCValue(inst, array_src);
4232 try bw.writeByte(';');
4232 try w.writeByte(';');
42334233 try f.object.newline();
4234 try v.end(f, inst, bw);
4234 try v.end(f, inst, w);
42354235 } else if (ptr_info.packed_offset.host_size > 0 and ptr_info.flags.vector_index == .none) {
42364236 const host_bits = ptr_info.packed_offset.host_size * 8;
42374237 const host_ty = try pt.intType(.unsigned, host_bits);
......@@ -4254,44 +4254,44 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
42544254
42554255 const mask_val = try pt.intValue_big(host_ty, mask.toConst());
42564256
4257 const v = try Vectorize.start(f, inst, bw, ptr_ty);
4258 const a = try Assignment.start(f, bw, src_scalar_ctype);
4259 try f.writeCValueDeref(bw, ptr_val);
4260 try v.elem(f, bw);
4261 try a.assign(f, bw);
4262 try bw.writeAll("zig_or_");
4263 try f.object.dg.renderTypeForBuiltinFnName(bw, host_ty);
4264 try bw.writeAll("(zig_and_");
4265 try f.object.dg.renderTypeForBuiltinFnName(bw, host_ty);
4266 try bw.writeByte('(');
4267 try f.writeCValueDeref(bw, ptr_val);
4268 try v.elem(f, bw);
4269 try bw.print(", {fx}), zig_shl_", .{try f.fmtIntLiteral(mask_val)});
4270 try f.object.dg.renderTypeForBuiltinFnName(bw, host_ty);
4271 try bw.writeByte('(');
4257 const v = try Vectorize.start(f, inst, w, ptr_ty);
4258 const a = try Assignment.start(f, w, src_scalar_ctype);
4259 try f.writeCValueDeref(w, ptr_val);
4260 try v.elem(f, w);
4261 try a.assign(f, w);
4262 try w.writeAll("zig_or_");
4263 try f.object.dg.renderTypeForBuiltinFnName(w, host_ty);
4264 try w.writeAll("(zig_and_");
4265 try f.object.dg.renderTypeForBuiltinFnName(w, host_ty);
4266 try w.writeByte('(');
4267 try f.writeCValueDeref(w, ptr_val);
4268 try v.elem(f, w);
4269 try w.print(", {fx}), zig_shl_", .{try f.fmtIntLiteral(mask_val)});
4270 try f.object.dg.renderTypeForBuiltinFnName(w, host_ty);
4271 try w.writeByte('(');
42724272 const cant_cast = host_ty.isInt(zcu) and host_ty.bitSize(zcu) > 64;
42734273 if (cant_cast) {
42744274 if (src_ty.bitSize(zcu) > 64) return f.fail("TODO: C backend: implement casting between types > 64 bits", .{});
4275 try bw.writeAll("zig_make_");
4276 try f.object.dg.renderTypeForBuiltinFnName(bw, host_ty);
4277 try bw.writeAll("(0, ");
4275 try w.writeAll("zig_make_");
4276 try f.object.dg.renderTypeForBuiltinFnName(w, host_ty);
4277 try w.writeAll("(0, ");
42784278 } else {
4279 try bw.writeByte('(');
4280 try f.renderType(bw, host_ty);
4281 try bw.writeByte(')');
4279 try w.writeByte('(');
4280 try f.renderType(w, host_ty);
4281 try w.writeByte(')');
42824282 }
42834283
42844284 if (src_ty.isPtrAtRuntime(zcu)) {
4285 try bw.writeByte('(');
4286 try f.renderType(bw, .usize);
4287 try bw.writeByte(')');
4285 try w.writeByte('(');
4286 try f.renderType(w, .usize);
4287 try w.writeByte(')');
42884288 }
4289 try f.writeCValue(bw, src_val, .Other);
4290 try v.elem(f, bw);
4291 if (cant_cast) try bw.writeByte(')');
4292 try bw.print(", {f}))", .{try f.fmtIntLiteral(bit_offset_val)});
4293 try a.end(f, bw);
4294 try v.end(f, inst, bw);
4289 try f.writeCValue(w, src_val, .Other);
4290 try v.elem(f, w);
4291 if (cant_cast) try w.writeByte(')');
4292 try w.print(", {f}))", .{try f.fmtIntLiteral(bit_offset_val)});
4293 try a.end(f, w);
4294 try v.end(f, inst, w);
42954295 } else {
42964296 switch (ptr_val) {
42974297 .local_ref => |ptr_local_index| switch (src_val) {
......@@ -4301,15 +4301,15 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
43014301 },
43024302 else => {},
43034303 }
4304 const v = try Vectorize.start(f, inst, bw, ptr_ty);
4305 const a = try Assignment.start(f, bw, src_scalar_ctype);
4306 try f.writeCValueDeref(bw, ptr_val);
4307 try v.elem(f, bw);
4308 try a.assign(f, bw);
4309 try f.writeCValue(bw, src_val, .Other);
4310 try v.elem(f, bw);
4311 try a.end(f, bw);
4312 try v.end(f, inst, bw);
4304 const v = try Vectorize.start(f, inst, w, ptr_ty);
4305 const a = try Assignment.start(f, w, src_scalar_ctype);
4306 try f.writeCValueDeref(w, ptr_val);
4307 try v.elem(f, w);
4308 try a.assign(f, w);
4309 try f.writeCValue(w, src_val, .Other);
4310 try v.elem(f, w);
4311 try a.end(f, w);
4312 try v.end(f, inst, w);
43134313 }
43144314 return .none;
43154315}
......@@ -4328,27 +4328,27 @@ fn airOverflow(f: *Function, inst: Air.Inst.Index, operation: []const u8, info:
43284328 const operand_ty = f.typeOf(bin_op.lhs);
43294329 const scalar_ty = operand_ty.scalarType(zcu);
43304330
4331 const bw = &f.object.code.buffered_writer;
4331 const w = &f.object.code.buffered_writer;
43324332 const local = try f.allocLocal(inst, inst_ty);
4333 const v = try Vectorize.start(f, inst, bw, operand_ty);
4334 try f.writeCValueMember(bw, local, .{ .field = 1 });
4335 try v.elem(f, bw);
4336 try bw.writeAll(" = zig_");
4337 try bw.writeAll(operation);
4338 try bw.writeAll("o_");
4339 try f.object.dg.renderTypeForBuiltinFnName(bw, scalar_ty);
4340 try bw.writeAll("(&");
4341 try f.writeCValueMember(bw, local, .{ .field = 0 });
4342 try v.elem(f, bw);
4343 try bw.writeAll(", ");
4344 try f.writeCValue(bw, lhs, .FunctionArgument);
4345 try v.elem(f, bw);
4346 try bw.writeAll(", ");
4347 try f.writeCValue(bw, rhs, .FunctionArgument);
4348 if (f.typeOf(bin_op.rhs).isVector(zcu)) try v.elem(f, bw);
4349 try f.object.dg.renderBuiltinInfo(bw, scalar_ty, info);
4350 try bw.writeAll(");\n");
4351 try v.end(f, inst, bw);
4333 const v = try Vectorize.start(f, inst, w, operand_ty);
4334 try f.writeCValueMember(w, local, .{ .field = 1 });
4335 try v.elem(f, w);
4336 try w.writeAll(" = zig_");
4337 try w.writeAll(operation);
4338 try w.writeAll("o_");
4339 try f.object.dg.renderTypeForBuiltinFnName(w, scalar_ty);
4340 try w.writeAll("(&");
4341 try f.writeCValueMember(w, local, .{ .field = 0 });
4342 try v.elem(f, w);
4343 try w.writeAll(", ");
4344 try f.writeCValue(w, lhs, .FunctionArgument);
4345 try v.elem(f, w);
4346 try w.writeAll(", ");
4347 try f.writeCValue(w, rhs, .FunctionArgument);
4348 if (f.typeOf(bin_op.rhs).isVector(zcu)) try v.elem(f, w);
4349 try f.object.dg.renderBuiltinInfo(w, scalar_ty, info);
4350 try w.writeAll(");\n");
4351 try v.end(f, inst, w);
43524352
43534353 return local;
43544354}
......@@ -4366,18 +4366,18 @@ fn airNot(f: *Function, inst: Air.Inst.Index) !CValue {
43664366
43674367 const inst_ty = f.typeOfIndex(inst);
43684368
4369 const bw = &f.object.code.buffered_writer;
4369 const w = &f.object.code.buffered_writer;
43704370 const local = try f.allocLocal(inst, inst_ty);
4371 const v = try Vectorize.start(f, inst, bw, operand_ty);
4372 try f.writeCValue(bw, local, .Other);
4373 try v.elem(f, bw);
4374 try bw.writeAll(" = ");
4375 try bw.writeByte('!');
4376 try f.writeCValue(bw, op, .Other);
4377 try v.elem(f, bw);
4378 try bw.writeByte(';');
4371 const v = try Vectorize.start(f, inst, w, operand_ty);
4372 try f.writeCValue(w, local, .Other);
4373 try v.elem(f, w);
4374 try w.writeAll(" = ");
4375 try w.writeByte('!');
4376 try f.writeCValue(w, op, .Other);
4377 try v.elem(f, w);
4378 try w.writeByte(';');
43794379 try f.object.newline();
4380 try v.end(f, inst, bw);
4380 try v.end(f, inst, w);
43814381
43824382 return local;
43834383}
......@@ -4403,22 +4403,22 @@ fn airBinOp(
44034403
44044404 const inst_ty = f.typeOfIndex(inst);
44054405
4406 const bw = &f.object.code.buffered_writer;
4406 const w = &f.object.code.buffered_writer;
44074407 const local = try f.allocLocal(inst, inst_ty);
4408 const v = try Vectorize.start(f, inst, bw, operand_ty);
4409 try f.writeCValue(bw, local, .Other);
4410 try v.elem(f, bw);
4411 try bw.writeAll(" = ");
4412 try f.writeCValue(bw, lhs, .Other);
4413 try v.elem(f, bw);
4414 try bw.writeByte(' ');
4415 try bw.writeAll(operator);
4416 try bw.writeByte(' ');
4417 try f.writeCValue(bw, rhs, .Other);
4418 try v.elem(f, bw);
4419 try bw.writeByte(';');
4408 const v = try Vectorize.start(f, inst, w, operand_ty);
4409 try f.writeCValue(w, local, .Other);
4410 try v.elem(f, w);
4411 try w.writeAll(" = ");
4412 try f.writeCValue(w, lhs, .Other);
4413 try v.elem(f, w);
4414 try w.writeByte(' ');
4415 try w.writeAll(operator);
4416 try w.writeByte(' ');
4417 try f.writeCValue(w, rhs, .Other);
4418 try v.elem(f, w);
4419 try w.writeByte(';');
44204420 try f.object.newline();
4421 try v.end(f, inst, bw);
4421 try v.end(f, inst, w);
44224422
44234423 return local;
44244424}
......@@ -4454,27 +4454,27 @@ fn airCmpOp(
44544454
44554455 const rhs_ty = f.typeOf(data.rhs);
44564456 const need_cast = lhs_ty.isSinglePointer(zcu) or rhs_ty.isSinglePointer(zcu);
4457 const bw = &f.object.code.buffered_writer;
4457 const w = &f.object.code.buffered_writer;
44584458 const local = try f.allocLocal(inst, inst_ty);
4459 const v = try Vectorize.start(f, inst, bw, lhs_ty);
4460 const a = try Assignment.start(f, bw, try f.ctypeFromType(scalar_ty, .complete));
4461 try f.writeCValue(bw, local, .Other);
4462 try v.elem(f, bw);
4463 try a.assign(f, bw);
4464 if (lhs != .undef and lhs.eql(rhs)) try bw.writeAll(switch (operator) {
4459 const v = try Vectorize.start(f, inst, w, lhs_ty);
4460 const a = try Assignment.start(f, w, try f.ctypeFromType(scalar_ty, .complete));
4461 try f.writeCValue(w, local, .Other);
4462 try v.elem(f, w);
4463 try a.assign(f, w);
4464 if (lhs != .undef and lhs.eql(rhs)) try w.writeAll(switch (operator) {
44654465 .lt, .neq, .gt => "false",
44664466 .lte, .eq, .gte => "true",
44674467 }) else {
4468 if (need_cast) try bw.writeAll("(void*)");
4469 try f.writeCValue(bw, lhs, .Other);
4470 try v.elem(f, bw);
4471 try bw.writeAll(compareOperatorC(operator));
4472 if (need_cast) try bw.writeAll("(void*)");
4473 try f.writeCValue(bw, rhs, .Other);
4474 try v.elem(f, bw);
4468 if (need_cast) try w.writeAll("(void*)");
4469 try f.writeCValue(w, lhs, .Other);
4470 try v.elem(f, w);
4471 try w.writeAll(compareOperatorC(operator));
4472 if (need_cast) try w.writeAll("(void*)");
4473 try f.writeCValue(w, rhs, .Other);
4474 try v.elem(f, w);
44754475 }
4476 try a.end(f, bw);
4477 try v.end(f, inst, bw);
4476 try a.end(f, w);
4477 try v.end(f, inst, w);
44784478
44794479 return local;
44804480}
......@@ -4507,41 +4507,41 @@ fn airEquality(
45074507 const rhs = try f.resolveInst(bin_op.rhs);
45084508 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });
45094509
4510 const bw = &f.object.code.buffered_writer;
4510 const w = &f.object.code.buffered_writer;
45114511 const local = try f.allocLocal(inst, .bool);
4512 const a = try Assignment.start(f, bw, .bool);
4513 try f.writeCValue(bw, local, .Other);
4514 try a.assign(f, bw);
4512 const a = try Assignment.start(f, w, .bool);
4513 try f.writeCValue(w, local, .Other);
4514 try a.assign(f, w);
45154515
45164516 const operand_ctype = try f.ctypeFromType(operand_ty, .complete);
4517 if (lhs != .undef and lhs.eql(rhs)) try bw.writeAll(switch (operator) {
4517 if (lhs != .undef and lhs.eql(rhs)) try w.writeAll(switch (operator) {
45184518 .lt, .lte, .gte, .gt => unreachable,
45194519 .neq => "false",
45204520 .eq => "true",
45214521 }) else switch (operand_ctype.info(ctype_pool)) {
45224522 .basic, .pointer => {
4523 try f.writeCValue(bw, lhs, .Other);
4524 try bw.writeAll(compareOperatorC(operator));
4525 try f.writeCValue(bw, rhs, .Other);
4523 try f.writeCValue(w, lhs, .Other);
4524 try w.writeAll(compareOperatorC(operator));
4525 try f.writeCValue(w, rhs, .Other);
45264526 },
45274527 .aligned, .array, .vector, .fwd_decl, .function => unreachable,
45284528 .aggregate => |aggregate| if (aggregate.fields.len == 2 and
45294529 (aggregate.fields.at(0, ctype_pool).name.index == .is_null or
45304530 aggregate.fields.at(1, ctype_pool).name.index == .is_null))
45314531 {
4532 try f.writeCValueMember(bw, lhs, .{ .identifier = "is_null" });
4533 try bw.writeAll(" || ");
4534 try f.writeCValueMember(bw, rhs, .{ .identifier = "is_null" });
4535 try bw.writeAll(" ? ");
4536 try f.writeCValueMember(bw, lhs, .{ .identifier = "is_null" });
4537 try bw.writeAll(compareOperatorC(operator));
4538 try f.writeCValueMember(bw, rhs, .{ .identifier = "is_null" });
4539 try bw.writeAll(" : ");
4540 try f.writeCValueMember(bw, lhs, .{ .identifier = "payload" });
4541 try bw.writeAll(compareOperatorC(operator));
4542 try f.writeCValueMember(bw, rhs, .{ .identifier = "payload" });
4532 try f.writeCValueMember(w, lhs, .{ .identifier = "is_null" });
4533 try w.writeAll(" || ");
4534 try f.writeCValueMember(w, rhs, .{ .identifier = "is_null" });
4535 try w.writeAll(" ? ");
4536 try f.writeCValueMember(w, lhs, .{ .identifier = "is_null" });
4537 try w.writeAll(compareOperatorC(operator));
4538 try f.writeCValueMember(w, rhs, .{ .identifier = "is_null" });
4539 try w.writeAll(" : ");
4540 try f.writeCValueMember(w, lhs, .{ .identifier = "payload" });
4541 try w.writeAll(compareOperatorC(operator));
4542 try f.writeCValueMember(w, rhs, .{ .identifier = "payload" });
45434543 } else for (0..aggregate.fields.len) |field_index| {
4544 if (field_index > 0) try bw.writeAll(switch (operator) {
4544 if (field_index > 0) try w.writeAll(switch (operator) {
45454545 .lt, .lte, .gte, .gt => unreachable,
45464546 .eq => " && ",
45474547 .neq => " || ",
......@@ -4549,12 +4549,12 @@ fn airEquality(
45494549 const field_name: CValue = .{
45504550 .ctype_pool_string = aggregate.fields.at(field_index, ctype_pool).name,
45514551 };
4552 try f.writeCValueMember(bw, lhs, field_name);
4553 try bw.writeAll(compareOperatorC(operator));
4554 try f.writeCValueMember(bw, rhs, field_name);
4552 try f.writeCValueMember(w, lhs, field_name);
4553 try w.writeAll(compareOperatorC(operator));
4554 try f.writeCValueMember(w, rhs, field_name);
45554555 },
45564556 }
4557 try a.end(f, bw);
4557 try a.end(f, w);
45584558
45594559 return local;
45604560}
......@@ -4565,12 +4565,12 @@ fn airCmpLtErrorsLen(f: *Function, inst: Air.Inst.Index) !CValue {
45654565 const operand = try f.resolveInst(un_op);
45664566 try reap(f, inst, &.{un_op});
45674567
4568 const bw = &f.object.code.buffered_writer;
4568 const w = &f.object.code.buffered_writer;
45694569 const local = try f.allocLocal(inst, .bool);
4570 try f.writeCValue(bw, local, .Other);
4571 try bw.writeAll(" = ");
4572 try f.writeCValue(bw, operand, .Other);
4573 try bw.print(" < sizeof({f }) / sizeof(*{0f });", .{fmtIdent("zig_errorName")});
4570 try f.writeCValue(w, local, .Other);
4571 try w.writeAll(" = ");
4572 try f.writeCValue(w, operand, .Other);
4573 try w.print(" < sizeof({f }) / sizeof(*{0f });", .{fmtIdent("zig_errorName")});
45744574 try f.object.newline();
45754575 return local;
45764576}
......@@ -4592,30 +4592,30 @@ fn airPtrAddSub(f: *Function, inst: Air.Inst.Index, operator: u8) !CValue {
45924592 const inst_scalar_ctype = try f.ctypeFromType(inst_scalar_ty, .complete);
45934593
45944594 const local = try f.allocLocal(inst, inst_ty);
4595 const bw = &f.object.code.buffered_writer;
4596 const v = try Vectorize.start(f, inst, bw, inst_ty);
4597 const a = try Assignment.start(f, bw, inst_scalar_ctype);
4598 try f.writeCValue(bw, local, .Other);
4599 try v.elem(f, bw);
4600 try a.assign(f, bw);
4595 const w = &f.object.code.buffered_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);
46014601 // We must convert to and from integer types to prevent UB if the operation
46024602 // results in a NULL pointer, or if LHS is NULL. The operation is only UB
46034603 // if the result is NULL and then dereferenced.
4604 try bw.writeByte('(');
4605 try f.renderCType(bw, inst_scalar_ctype);
4606 try bw.writeAll(")(((uintptr_t)");
4607 try f.writeCValue(bw, lhs, .Other);
4608 try v.elem(f, bw);
4609 try bw.writeAll(") ");
4610 try bw.writeByte(operator);
4611 try bw.writeAll(" (");
4612 try f.writeCValue(bw, rhs, .Other);
4613 try v.elem(f, bw);
4614 try bw.writeAll("*sizeof(");
4615 try f.renderType(bw, elem_ty);
4616 try bw.writeAll(")))");
4617 try a.end(f, bw);
4618 try v.end(f, inst, bw);
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);
46194619 return local;
46204620}
46214621
......@@ -4634,29 +4634,29 @@ fn airMinMax(f: *Function, inst: Air.Inst.Index, operator: u8, operation: []cons
46344634 const rhs = try f.resolveInst(bin_op.rhs);
46354635 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });
46364636
4637 const bw = &f.object.code.buffered_writer;
4637 const w = &f.object.code.buffered_writer;
46384638 const local = try f.allocLocal(inst, inst_ty);
4639 const v = try Vectorize.start(f, inst, bw, inst_ty);
4640 try f.writeCValue(bw, local, .Other);
4641 try v.elem(f, bw);
4639 const v = try Vectorize.start(f, inst, w, inst_ty);
4640 try f.writeCValue(w, local, .Other);
4641 try v.elem(f, w);
46424642 // (lhs <> rhs) ? lhs : rhs
4643 try bw.writeAll(" = (");
4644 try f.writeCValue(bw, lhs, .Other);
4645 try v.elem(f, bw);
4646 try bw.writeByte(' ');
4647 try bw.writeByte(operator);
4648 try bw.writeByte(' ');
4649 try f.writeCValue(bw, rhs, .Other);
4650 try v.elem(f, bw);
4651 try bw.writeAll(") ? ");
4652 try f.writeCValue(bw, lhs, .Other);
4653 try v.elem(f, bw);
4654 try bw.writeAll(" : ");
4655 try f.writeCValue(bw, rhs, .Other);
4656 try v.elem(f, bw);
4657 try bw.writeByte(';');
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.writeByte(';');
46584658 try f.object.newline();
4659 try v.end(f, inst, bw);
4659 try v.end(f, inst, w);
46604660
46614661 return local;
46624662}
......@@ -4674,21 +4674,21 @@ fn airSlice(f: *Function, inst: Air.Inst.Index) !CValue {
46744674 const inst_ty = f.typeOfIndex(inst);
46754675 const ptr_ty = inst_ty.slicePtrFieldType(zcu);
46764676
4677 const bw = &f.object.code.buffered_writer;
4677 const w = &f.object.code.buffered_writer;
46784678 const local = try f.allocLocal(inst, inst_ty);
46794679 {
4680 const a = try Assignment.start(f, bw, try f.ctypeFromType(ptr_ty, .complete));
4681 try f.writeCValueMember(bw, local, .{ .identifier = "ptr" });
4682 try a.assign(f, bw);
4683 try f.writeCValue(bw, ptr, .Other);
4684 try a.end(f, bw);
4680 const a = try Assignment.start(f, w, try f.ctypeFromType(ptr_ty, .complete));
4681 try f.writeCValueMember(w, local, .{ .identifier = "ptr" });
4682 try a.assign(f, w);
4683 try f.writeCValue(w, ptr, .Other);
4684 try a.end(f, w);
46854685 }
46864686 {
4687 const a = try Assignment.start(f, bw, .usize);
4688 try f.writeCValueMember(bw, local, .{ .identifier = "len" });
4689 try a.assign(f, bw);
4690 try f.writeCValue(bw, len, .Other);
4691 try a.end(f, bw);
4687 const a = try Assignment.start(f, w, .usize);
4688 try f.writeCValueMember(w, local, .{ .identifier = "len" });
4689 try a.assign(f, w);
4690 try f.writeCValue(w, len, .Other);
4691 try a.end(f, w);
46924692 }
46934693 return local;
46944694}
......@@ -4705,7 +4705,7 @@ fn airCall(
47054705 if (f.object.dg.is_naked_fn) return .none;
47064706
47074707 const gpa = f.object.dg.gpa;
4708 const bw = &f.object.code.buffered_writer;
4708 const w = &f.object.code.buffered_writer;
47094709
47104710 const pl_op = f.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
47114711 const extra = f.air.extraData(Air.Call, pl_op.payload);
......@@ -4726,13 +4726,13 @@ fn airCall(
47264726 .ctype = arg_ctype,
47274727 .alignas = CType.AlignAs.fromAbiAlignment(arg_ty.abiAlignment(zcu)),
47284728 });
4729 try bw.writeAll("memcpy(");
4730 try f.writeCValueMember(bw, array_local, .{ .identifier = "array" });
4731 try bw.writeAll(", ");
4732 try f.writeCValue(bw, resolved_arg.*, .FunctionArgument);
4733 try bw.writeAll(", sizeof(");
4734 try f.renderCType(bw, arg_ctype);
4735 try bw.writeAll("));");
4729 try w.writeAll("memcpy(");
4730 try f.writeCValueMember(w, array_local, .{ .identifier = "array" });
4731 try w.writeAll(", ");
4732 try f.writeCValue(w, resolved_arg.*, .FunctionArgument);
4733 try w.writeAll(", sizeof(");
4734 try f.renderCType(w, arg_ctype);
4735 try w.writeAll("));");
47364736 try f.object.newline();
47374737 resolved_arg.* = array_local;
47384738 }
......@@ -4761,22 +4761,22 @@ fn airCall(
47614761
47624762 const result_local = result: {
47634763 if (modifier == .always_tail) {
4764 try bw.writeAll("zig_always_tail return ");
4764 try w.writeAll("zig_always_tail return ");
47654765 break :result .none;
47664766 } else if (ret_ctype.index == .void) {
47674767 break :result .none;
47684768 } else if (f.liveness.isUnused(inst)) {
4769 try bw.writeByte('(');
4770 try f.renderCType(bw, .void);
4771 try bw.writeByte(')');
4769 try w.writeByte('(');
4770 try f.renderCType(w, .void);
4771 try w.writeByte(')');
47724772 break :result .none;
47734773 } else {
47744774 const local = try f.allocAlignedLocal(inst, .{
47754775 .ctype = ret_ctype,
47764776 .alignas = CType.AlignAs.fromAbiAlignment(ret_ty.abiAlignment(zcu)),
47774777 });
4778 try f.writeCValue(bw, local, .Other);
4779 try bw.writeAll(" = ");
4778 try f.writeCValue(w, local, .Other);
4779 try w.writeAll(" = ");
47804780 break :result local;
47814781 }
47824782 };
......@@ -4796,17 +4796,17 @@ fn airCall(
47964796 else => break :known,
47974797 };
47984798 if (need_cast) {
4799 try bw.writeAll("((");
4800 try f.renderType(bw, if (callee_is_ptr) callee_ty else try pt.singleConstPtrType(callee_ty));
4801 try bw.writeByte(')');
4802 if (!callee_is_ptr) try bw.writeByte('&');
4799 try w.writeAll("((");
4800 try f.renderType(w, if (callee_is_ptr) callee_ty else try pt.singleConstPtrType(callee_ty));
4801 try w.writeByte(')');
4802 if (!callee_is_ptr) try w.writeByte('&');
48034803 }
48044804 switch (modifier) {
4805 .auto, .always_tail => try f.object.dg.renderNavName(bw, fn_nav),
4806 inline .never_tail, .never_inline => |m| try bw.writeAll(try f.getLazyFnName(@unionInit(LazyFnKey, @tagName(m), fn_nav))),
4805 .auto, .always_tail => try f.object.dg.renderNavName(w, fn_nav),
4806 inline .never_tail, .never_inline => |m| try w.writeAll(try f.getLazyFnName(@unionInit(LazyFnKey, @tagName(m), fn_nav))),
48074807 else => unreachable,
48084808 }
4809 if (need_cast) try bw.writeByte(')');
4809 if (need_cast) try w.writeByte(')');
48104810 break :callee;
48114811 }
48124812 switch (modifier) {
......@@ -4816,21 +4816,21 @@ fn airCall(
48164816 else => unreachable,
48174817 }
48184818 // Fall back to function pointer call.
4819 try f.writeCValue(bw, callee, .Other);
4819 try f.writeCValue(w, callee, .Other);
48204820 }
48214821
4822 try bw.writeByte('(');
4822 try w.writeByte('(');
48234823 var need_comma = false;
48244824 for (resolved_args) |resolved_arg| {
48254825 if (resolved_arg == .none) continue;
4826 if (need_comma) try bw.writeAll(", ");
4826 if (need_comma) try w.writeAll(", ");
48274827 need_comma = true;
4828 try f.writeCValue(bw, resolved_arg, .FunctionArgument);
4828 try f.writeCValue(w, resolved_arg, .FunctionArgument);
48294829 try f.freeCValue(inst, resolved_arg);
48304830 }
4831 try bw.writeAll(");");
4831 try w.writeAll(");");
48324832 switch (modifier) {
4833 .always_tail => try bw.writeByte('\n'),
4833 .always_tail => try w.writeByte('\n'),
48344834 else => try f.object.newline(),
48354835 }
48364836
......@@ -4839,13 +4839,13 @@ fn airCall(
48394839 break :result result_local;
48404840
48414841 const array_local = try f.allocLocal(inst, ret_ty);
4842 try bw.writeAll("memcpy(");
4843 try f.writeCValue(bw, array_local, .FunctionArgument);
4844 try bw.writeAll(", ");
4845 try f.writeCValueMember(bw, result_local, .{ .identifier = "array" });
4846 try bw.writeAll(", sizeof(");
4847 try f.renderType(bw, ret_ty);
4848 try bw.writeAll("));");
4842 try w.writeAll("memcpy(");
4843 try f.writeCValue(w, array_local, .FunctionArgument);
4844 try w.writeAll(", ");
4845 try f.writeCValueMember(w, result_local, .{ .identifier = "array" });
4846 try w.writeAll(", sizeof(");
4847 try f.renderType(w, ret_ty);
4848 try w.writeAll("));");
48494849 try f.object.newline();
48504850 try freeLocal(f, inst, result_local.new_local, null);
48514851 break :result array_local;
......@@ -4856,7 +4856,7 @@ fn airCall(
48564856
48574857fn airDbgStmt(f: *Function, inst: Air.Inst.Index) !CValue {
48584858 const dbg_stmt = f.air.instructions.items(.data)[@intFromEnum(inst)].dbg_stmt;
4859 const bw = &f.object.code.buffered_writer;
4859 const w = &f.object.code.buffered_writer;
48604860 // TODO re-evaluate whether to emit these or not. If we naively emit
48614861 // these directives, the output file will report bogus line numbers because
48624862 // every newline after the #line directive adds one to the line.
......@@ -4864,9 +4864,9 @@ fn airDbgStmt(f: *Function, inst: Air.Inst.Index) !CValue {
48644864 // If we wanted to go this route, we would need to go all the way and not output
48654865 // newlines until the next dbg_stmt occurs.
48664866 // Perhaps an additional compilation option is in order?
4867 //try bw.print("#line {d}", .{dbg_stmt.line + 1});
4867 //try w.print("#line {d}", .{dbg_stmt.line + 1});
48684868 //try f.object.newline();
4869 try bw.print("/* file:{d}:{d} */", .{ dbg_stmt.line + 1, dbg_stmt.column + 1 });
4869 try w.print("/* file:{d}:{d} */", .{ dbg_stmt.line + 1, dbg_stmt.column + 1 });
48704870 try f.object.newline();
48714871 return .none;
48724872}
......@@ -4899,8 +4899,8 @@ fn airDbgVar(f: *Function, inst: Air.Inst.Index) !CValue {
48994899 if (!operand_is_undef) _ = try f.resolveInst(pl_op.operand);
49004900
49014901 try reap(f, inst, &.{pl_op.operand});
4902 const bw = &f.object.code.buffered_writer;
4903 try bw.print("/* {s}:{s} */", .{ @tagName(tag), name.toSlice(f.air) });
4902 const w = &f.object.code.buffered_writer;
4903 try w.print("/* {s}:{s} */", .{ @tagName(tag), name.toSlice(f.air) });
49044904 try f.object.newline();
49054905 return .none;
49064906}
......@@ -4918,7 +4918,7 @@ fn lowerBlock(f: *Function, inst: Air.Inst.Index, body: []const Air.Inst.Index)
49184918
49194919 const block_id = f.next_block_index;
49204920 f.next_block_index += 1;
4921 const bw = &f.object.code.buffered_writer;
4921 const w = &f.object.code.buffered_writer;
49224922
49234923 const inst_ty = f.typeOfIndex(inst);
49244924 const result = if (inst_ty.hasRuntimeBitsIgnoreComptime(zcu) and !f.liveness.isUnused(inst))
......@@ -4949,7 +4949,7 @@ fn lowerBlock(f: *Function, inst: Air.Inst.Index, body: []const Air.Inst.Index)
49494949 }
49504950 } else if (!f.typeOfIndex(inst).isNoReturn(zcu)) {
49514951 // label must be followed by an expression, include an empty one.
4952 try bw.print("\nzig_block_{d}:;", .{block_id});
4952 try w.print("\nzig_block_{d}:;", .{block_id});
49534953 try f.object.newline();
49544954 }
49554955
......@@ -4987,28 +4987,28 @@ fn lowerTry(
49874987 const err_union = try f.resolveInst(operand);
49884988 const inst_ty = f.typeOfIndex(inst);
49894989 const liveness_condbr = f.liveness.getCondBr(inst);
4990 const bw = &f.object.code.buffered_writer;
4990 const w = &f.object.code.buffered_writer;
49914991 const payload_ty = err_union_ty.errorUnionPayload(zcu);
49924992 const payload_has_bits = payload_ty.hasRuntimeBitsIgnoreComptime(zcu);
49934993
49944994 if (!err_union_ty.errorUnionSet(zcu).errorSetIsEmpty(zcu)) {
4995 try bw.writeAll("if (");
4995 try w.writeAll("if (");
49964996 if (!payload_has_bits) {
49974997 if (is_ptr)
4998 try f.writeCValueDeref(bw, err_union)
4998 try f.writeCValueDeref(w, err_union)
49994999 else
5000 try f.writeCValue(bw, err_union, .Other);
5000 try f.writeCValue(w, err_union, .Other);
50015001 } else {
50025002 // Reap the operand so that it can be reused inside genBody.
50035003 // Remember we must avoid calling reap() twice for the same operand
50045004 // in this function.
50055005 try reap(f, inst, &.{operand});
50065006 if (is_ptr)
5007 try f.writeCValueDerefMember(bw, err_union, .{ .identifier = "error" })
5007 try f.writeCValueDerefMember(w, err_union, .{ .identifier = "error" })
50085008 else
5009 try f.writeCValueMember(bw, err_union, .{ .identifier = "error" });
5009 try f.writeCValueMember(w, err_union, .{ .identifier = "error" });
50105010 }
5011 try bw.writeAll(") ");
5011 try w.writeAll(") ");
50125012
50135013 try genBodyResolveState(f, inst, liveness_condbr.else_deaths, body, false);
50145014 try f.object.newline();
......@@ -5034,14 +5034,14 @@ fn lowerTry(
50345034 if (f.liveness.isUnused(inst)) return .none;
50355035
50365036 const local = try f.allocLocal(inst, inst_ty);
5037 const a = try Assignment.start(f, bw, try f.ctypeFromType(inst_ty, .complete));
5038 try f.writeCValue(bw, local, .Other);
5039 try a.assign(f, bw);
5037 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete));
5038 try f.writeCValue(w, local, .Other);
5039 try a.assign(f, w);
50405040 if (is_ptr) {
5041 try bw.writeByte('&');
5042 try f.writeCValueDerefMember(bw, err_union, .{ .identifier = "payload" });
5043 } else try f.writeCValueMember(bw, err_union, .{ .identifier = "payload" });
5044 try a.end(f, bw);
5041 try w.writeByte('&');
5042 try f.writeCValueDerefMember(w, err_union, .{ .identifier = "payload" });
5043 } else try f.writeCValueMember(w, err_union, .{ .identifier = "payload" });
5044 try a.end(f, w);
50455045 return local;
50465046}
50475047
......@@ -5049,7 +5049,7 @@ fn airBr(f: *Function, inst: Air.Inst.Index) !void {
50495049 const branch = f.air.instructions.items(.data)[@intFromEnum(inst)].br;
50505050 const block = f.blocks.get(branch.block_inst).?;
50515051 const result = block.result;
5052 const bw = &f.object.code.buffered_writer;
5052 const w = &f.object.code.buffered_writer;
50535053
50545054 if (f.object.dg.is_naked_fn) {
50555055 if (result != .none) return f.fail("runtime code not allowed in naked function", .{});
......@@ -5063,14 +5063,14 @@ fn airBr(f: *Function, inst: Air.Inst.Index) !void {
50635063 const operand = try f.resolveInst(branch.operand);
50645064 try reap(f, inst, &.{branch.operand});
50655065
5066 const a = try Assignment.start(f, bw, try f.ctypeFromType(operand_ty, .complete));
5067 try f.writeCValue(bw, result, .Other);
5068 try a.assign(f, bw);
5069 try f.writeCValue(bw, operand, .Other);
5070 try a.end(f, bw);
5066 const a = try Assignment.start(f, w, try f.ctypeFromType(operand_ty, .complete));
5067 try f.writeCValue(w, result, .Other);
5068 try a.assign(f, w);
5069 try f.writeCValue(w, operand, .Other);
5070 try a.end(f, w);
50715071 }
50725072
5073 try bw.print("goto zig_block_{d};\n", .{block.block_id});
5073 try w.print("goto zig_block_{d};\n", .{block.block_id});
50745074}
50755075
50765076fn airRepeat(f: *Function, inst: Air.Inst.Index) !void {
......@@ -5082,7 +5082,7 @@ fn airSwitchDispatch(f: *Function, inst: Air.Inst.Index) !void {
50825082 const pt = f.object.dg.pt;
50835083 const zcu = pt.zcu;
50845084 const br = f.air.instructions.items(.data)[@intFromEnum(inst)].br;
5085 const bw = &f.object.code.buffered_writer;
5085 const w = &f.object.code.buffered_writer;
50865086
50875087 if (try f.air.value(br.operand, pt)) |cond_val| {
50885088 // Comptime-known dispatch. Iterate the cases to find the correct
......@@ -5104,19 +5104,19 @@ fn airSwitchDispatch(f: *Function, inst: Air.Inst.Index) !void {
51045104 }
51055105 }
51065106 } else switch_br.cases_len;
5107 try bw.print("goto zig_switch_{d}_dispatch_{d};\n", .{ @intFromEnum(br.block_inst), target_case_idx });
5107 try w.print("goto zig_switch_{d}_dispatch_{d};\n", .{ @intFromEnum(br.block_inst), target_case_idx });
51085108 return;
51095109 }
51105110
51115111 // Runtime-known dispatch. Set the switch condition, and branch back.
51125112 const cond = try f.resolveInst(br.operand);
51135113 const cond_local = f.loop_switch_conds.get(br.block_inst).?;
5114 try f.writeCValue(bw, .{ .local = cond_local }, .Other);
5115 try bw.writeAll(" = ");
5116 try f.writeCValue(bw, cond, .Other);
5117 try bw.writeByte(';');
5114 try f.writeCValue(w, .{ .local = cond_local }, .Other);
5115 try w.writeAll(" = ");
5116 try f.writeCValue(w, cond, .Other);
5117 try w.writeByte(';');
51185118 try f.object.newline();
5119 try bw.print("goto zig_switch_{d}_loop;\n", .{@intFromEnum(br.block_inst)});
5119 try w.print("goto zig_switch_{d}_loop;\n", .{@intFromEnum(br.block_inst)});
51205120}
51215121
51225122fn airBitcast(f: *Function, inst: Air.Inst.Index) !CValue {
......@@ -5136,7 +5136,7 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !CVal
51365136 const zcu = pt.zcu;
51375137 const target = &f.object.dg.mod.resolved_target.result;
51385138 const ctype_pool = &f.object.dg.ctype_pool;
5139 const bw = &f.object.code.buffered_writer;
5139 const w = &f.object.code.buffered_writer;
51405140
51415141 if (operand_ty.isAbiInt(zcu) and dest_ty.isAbiInt(zcu)) {
51425142 const src_info = dest_ty.intInfo(zcu);
......@@ -5147,43 +5147,43 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !CVal
51475147
51485148 if (dest_ty.isPtrAtRuntime(zcu) or operand_ty.isPtrAtRuntime(zcu)) {
51495149 const local = try f.allocLocal(null, dest_ty);
5150 try f.writeCValue(bw, local, .Other);
5151 try bw.writeAll(" = (");
5152 try f.renderType(bw, dest_ty);
5153 try bw.writeByte(')');
5154 try f.writeCValue(bw, operand, .Other);
5155 try bw.writeByte(';');
5150 try f.writeCValue(w, local, .Other);
5151 try w.writeAll(" = (");
5152 try f.renderType(w, dest_ty);
5153 try w.writeByte(')');
5154 try f.writeCValue(w, operand, .Other);
5155 try w.writeByte(';');
51565156 try f.object.newline();
51575157 return local;
51585158 }
51595159
51605160 const operand_lval = if (operand == .constant) blk: {
51615161 const operand_local = try f.allocLocal(null, operand_ty);
5162 try f.writeCValue(bw, operand_local, .Other);
5162 try f.writeCValue(w, operand_local, .Other);
51635163 if (operand_ty.isAbiInt(zcu)) {
5164 try bw.writeAll(" = ");
5164 try w.writeAll(" = ");
51655165 } else {
5166 try bw.writeAll(" = (");
5167 try f.renderType(bw, operand_ty);
5168 try bw.writeByte(')');
5166 try w.writeAll(" = (");
5167 try f.renderType(w, operand_ty);
5168 try w.writeByte(')');
51695169 }
5170 try f.writeCValue(bw, operand, .Other);
5171 try bw.writeByte(';');
5170 try f.writeCValue(w, operand, .Other);
5171 try w.writeByte(';');
51725172 try f.object.newline();
51735173 break :blk operand_local;
51745174 } else operand;
51755175
51765176 const local = try f.allocLocal(null, dest_ty);
5177 try bw.writeAll("memcpy(&");
5178 try f.writeCValue(bw, local, .Other);
5179 try bw.writeAll(", &");
5180 try f.writeCValue(bw, operand_lval, .Other);
5181 try bw.writeAll(", sizeof(");
5177 try w.writeAll("memcpy(&");
5178 try f.writeCValue(w, local, .Other);
5179 try w.writeAll(", &");
5180 try f.writeCValue(w, operand_lval, .Other);
5181 try w.writeAll(", sizeof(");
51825182 try f.renderType(
5183 bw,
5183 w,
51845184 if (dest_ty.abiSize(zcu) <= operand_ty.abiSize(zcu)) dest_ty else operand_ty,
51855185 );
5186 try bw.writeAll("));");
5186 try w.writeAll("));");
51875187 try f.object.newline();
51885188
51895189 // Ensure padding bits have the expected value.
......@@ -5194,11 +5194,11 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !CVal
51945194 var wrap_ctype: ?CType = null;
51955195 var need_bitcasts = false;
51965196
5197 try f.writeCValue(bw, local, .Other);
5197 try f.writeCValue(w, local, .Other);
51985198 switch (dest_ctype.info(ctype_pool)) {
51995199 else => {},
52005200 .array => |array_info| {
5201 try bw.print("[{d}]", .{switch (target.cpu.arch.endian()) {
5201 try w.print("[{d}]", .{switch (target.cpu.arch.endian()) {
52025202 .little => array_info.len - 1,
52035203 .big => 0,
52045204 }});
......@@ -5209,38 +5209,38 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !CVal
52095209 bits += 1;
52105210 },
52115211 }
5212 try bw.writeAll(" = ");
5212 try w.writeAll(" = ");
52135213 if (need_bitcasts) {
5214 try bw.writeAll("zig_bitCast_");
5215 try f.object.dg.renderCTypeForBuiltinFnName(bw, wrap_ctype.?.toUnsigned());
5216 try bw.writeByte('(');
5214 try w.writeAll("zig_bitCast_");
5215 try f.object.dg.renderCTypeForBuiltinFnName(w, wrap_ctype.?.toUnsigned());
5216 try w.writeByte('(');
52175217 }
5218 try bw.writeAll("zig_wrap_");
5218 try w.writeAll("zig_wrap_");
52195219 const info_ty = try pt.intType(dest_info.signedness, bits);
52205220 if (wrap_ctype) |ctype|
5221 try f.object.dg.renderCTypeForBuiltinFnName(bw, ctype)
5221 try f.object.dg.renderCTypeForBuiltinFnName(w, ctype)
52225222 else
5223 try f.object.dg.renderTypeForBuiltinFnName(bw, info_ty);
5224 try bw.writeByte('(');
5223 try f.object.dg.renderTypeForBuiltinFnName(w, info_ty);
5224 try w.writeByte('(');
52255225 if (need_bitcasts) {
5226 try bw.writeAll("zig_bitCast_");
5227 try f.object.dg.renderCTypeForBuiltinFnName(bw, wrap_ctype.?);
5228 try bw.writeByte('(');
5226 try w.writeAll("zig_bitCast_");
5227 try f.object.dg.renderCTypeForBuiltinFnName(w, wrap_ctype.?);
5228 try w.writeByte('(');
52295229 }
5230 try f.writeCValue(bw, local, .Other);
5230 try f.writeCValue(w, local, .Other);
52315231 switch (dest_ctype.info(ctype_pool)) {
52325232 else => {},
5233 .array => |array_info| try bw.print("[{d}]", .{
5233 .array => |array_info| try w.print("[{d}]", .{
52345234 switch (target.cpu.arch.endian()) {
52355235 .little => array_info.len - 1,
52365236 .big => 0,
52375237 },
52385238 }),
52395239 }
5240 if (need_bitcasts) try bw.writeByte(')');
5241 try f.object.dg.renderBuiltinInfo(bw, info_ty, .bits);
5242 if (need_bitcasts) try bw.writeByte(')');
5243 try bw.writeAll(");");
5240 if (need_bitcasts) try w.writeByte(')');
5241 try f.object.dg.renderBuiltinInfo(w, info_ty, .bits);
5242 if (need_bitcasts) try w.writeByte(')');
5243 try w.writeAll(");");
52445244 try f.object.newline();
52455245 }
52465246
......@@ -5248,37 +5248,37 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !CVal
52485248 return local;
52495249}
52505250
5251fn airTrap(f: *Function, bw: *Writer) !void {
5251fn airTrap(f: *Function, w: *Writer) !void {
52525252 // Not even allowed to call trap in a naked function.
52535253 if (f.object.dg.is_naked_fn) return;
5254 try bw.writeAll("zig_trap();\n");
5254 try w.writeAll("zig_trap();\n");
52555255}
52565256
52575257fn airBreakpoint(f: *Function) !CValue {
5258 const bw = &f.object.code.buffered_writer;
5259 try bw.writeAll("zig_breakpoint();");
5258 const w = &f.object.code.buffered_writer;
5259 try w.writeAll("zig_breakpoint();");
52605260 try f.object.newline();
52615261 return .none;
52625262}
52635263
52645264fn airRetAddr(f: *Function, inst: Air.Inst.Index) !CValue {
5265 const bw = &f.object.code.buffered_writer;
5265 const w = &f.object.code.buffered_writer;
52665266 const local = try f.allocLocal(inst, .usize);
5267 try f.writeCValue(bw, local, .Other);
5268 try bw.writeAll(" = (");
5269 try f.renderType(bw, .usize);
5270 try bw.writeAll(")zig_return_address();");
5267 try f.writeCValue(w, local, .Other);
5268 try w.writeAll(" = (");
5269 try f.renderType(w, .usize);
5270 try w.writeAll(")zig_return_address();");
52715271 try f.object.newline();
52725272 return local;
52735273}
52745274
52755275fn airFrameAddress(f: *Function, inst: Air.Inst.Index) !CValue {
5276 const bw = &f.object.code.buffered_writer;
5276 const w = &f.object.code.buffered_writer;
52775277 const local = try f.allocLocal(inst, .usize);
5278 try f.writeCValue(bw, local, .Other);
5279 try bw.writeAll(" = (");
5280 try f.renderType(bw, .usize);
5281 try bw.writeAll(")zig_frame_address();");
5278 try f.writeCValue(w, local, .Other);
5279 try w.writeAll(" = (");
5280 try f.renderType(w, .usize);
5281 try w.writeAll(")zig_frame_address();");
52825282 try f.object.newline();
52835283 return local;
52845284}
......@@ -5293,13 +5293,13 @@ fn airLoop(f: *Function, inst: Air.Inst.Index) !void {
52935293 const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
52945294 const loop = f.air.extraData(Air.Block, ty_pl.payload);
52955295 const body: []const Air.Inst.Index = @ptrCast(f.air.extra.items[loop.end..][0..loop.data.body_len]);
5296 const bw = &f.object.code.buffered_writer;
5296 const w = &f.object.code.buffered_writer;
52975297
52985298 // `repeat` instructions matching this loop will branch to
52995299 // this label. Since we need a label for arbitrary `repeat`
53005300 // anyway, there's actually no need to use a "real" looping
53015301 // construct at all!
5302 try bw.print("zig_loop_{d}:", .{@intFromEnum(inst)});
5302 try w.print("zig_loop_{d}:", .{@intFromEnum(inst)});
53035303 try f.object.newline();
53045304 try genBodyInner(f, body); // no need to restore state, we're noreturn
53055305}
......@@ -5312,11 +5312,11 @@ fn airCondBr(f: *Function, inst: Air.Inst.Index) !void {
53125312 const then_body: []const Air.Inst.Index = @ptrCast(f.air.extra.items[extra.end..][0..extra.data.then_body_len]);
53135313 const else_body: []const Air.Inst.Index = @ptrCast(f.air.extra.items[extra.end + then_body.len ..][0..extra.data.else_body_len]);
53145314 const liveness_condbr = f.liveness.getCondBr(inst);
5315 const bw = &f.object.code.buffered_writer;
5315 const w = &f.object.code.buffered_writer;
53165316
5317 try bw.writeAll("if (");
5318 try f.writeCValue(bw, cond, .Other);
5319 try bw.writeAll(") ");
5317 try w.writeAll("if (");
5318 try f.writeCValue(w, cond, .Other);
5319 try w.writeAll(") ");
53205320
53215321 try genBodyResolveState(f, inst, liveness_condbr.then_deaths, then_body, false);
53225322 try f.object.newline();
......@@ -5345,7 +5345,7 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void
53455345 const init_condition = try f.resolveInst(switch_br.operand);
53465346 try reap(f, inst, &.{switch_br.operand});
53475347 const condition_ty = f.typeOf(switch_br.operand);
5348 const bw = &f.object.code.buffered_writer;
5348 const w = &f.object.code.buffered_writer;
53495349
53505350 // For dispatches, we will create a local alloc to contain the condition value.
53515351 // This may not result in optimal codegen for switch loops, but it minimizes the
......@@ -5353,7 +5353,7 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void
53535353 const condition = if (is_dispatch_loop) cond: {
53545354 const new_local = try f.allocLocal(inst, condition_ty);
53555355 try f.copyCValue(try f.ctypeFromType(condition_ty, .complete), new_local, init_condition);
5356 try bw.print("zig_switch_{d}_loop:", .{@intFromEnum(inst)});
5356 try w.print("zig_switch_{d}_loop:", .{@intFromEnum(inst)});
53575357 try f.object.newline();
53585358 try f.loop_switch_conds.put(gpa, inst, new_local.new_local);
53595359 break :cond new_local;
......@@ -5363,7 +5363,7 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void
53635363 assert(f.loop_switch_conds.remove(inst));
53645364 };
53655365
5366 try bw.writeAll("switch (");
5366 try w.writeAll("switch (");
53675367
53685368 const lowered_condition_ty: Type = if (condition_ty.toIntern() == .bool_type)
53695369 .u1
......@@ -5372,12 +5372,12 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void
53725372 else
53735373 condition_ty;
53745374 if (condition_ty.toIntern() != lowered_condition_ty.toIntern()) {
5375 try bw.writeByte('(');
5376 try f.renderType(bw, lowered_condition_ty);
5377 try bw.writeByte(')');
5375 try w.writeByte('(');
5376 try f.renderType(w, lowered_condition_ty);
5377 try w.writeByte(')');
53785378 }
5379 try f.writeCValue(bw, condition, .Other);
5380 try bw.writeAll(") {");
5379 try f.writeCValue(w, condition, .Other);
5380 try w.writeAll(") {");
53815381 f.object.indent();
53825382
53835383 const liveness = try f.liveness.getSwitchBr(gpa, inst, switch_br.cases_len + 1);
......@@ -5392,36 +5392,36 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void
53925392 }
53935393 for (case.items) |item| {
53945394 try f.object.newline();
5395 try bw.writeAll("case ");
5395 try w.writeAll("case ");
53965396 const item_value = try f.air.value(item, pt);
53975397 // If `item_value` is a pointer with a known integer address, print the address
53985398 // with no cast to avoid a warning.
53995399 write_val: {
54005400 if (condition_ty.isPtrAtRuntime(zcu)) {
54015401 if (item_value.?.getUnsignedInt(zcu)) |item_int| {
5402 try bw.print("{f}", .{try f.fmtIntLiteral(try pt.intValue(lowered_condition_ty, item_int))});
5402 try w.print("{f}", .{try f.fmtIntLiteral(try pt.intValue(lowered_condition_ty, item_int))});
54035403 break :write_val;
54045404 }
54055405 }
54065406 if (condition_ty.isPtrAtRuntime(zcu)) {
5407 try bw.writeByte('(');
5408 try f.renderType(bw, .usize);
5409 try bw.writeByte(')');
5407 try w.writeByte('(');
5408 try f.renderType(w, .usize);
5409 try w.writeByte(')');
54105410 }
5411 try f.object.dg.renderValue(bw, (try f.air.value(item, pt)).?, .Other);
5411 try f.object.dg.renderValue(w, (try f.air.value(item, pt)).?, .Other);
54125412 }
5413 try bw.writeByte(':');
5413 try w.writeByte(':');
54145414 }
5415 try bw.writeAll(" {");
5415 try w.writeAll(" {");
54165416 f.object.indent();
54175417 try f.object.newline();
54185418 if (is_dispatch_loop) {
5419 try bw.print("zig_switch_{d}_dispatch_{d}:;", .{ @intFromEnum(inst), case.idx });
5419 try w.print("zig_switch_{d}_dispatch_{d}:;", .{ @intFromEnum(inst), case.idx });
54205420 try f.object.newline();
54215421 }
54225422 try genBodyResolveState(f, inst, liveness.deaths[case.idx], case.body, true);
54235423 try f.object.outdent();
5424 try bw.writeByte('}');
5424 try w.writeByte('}');
54255425 if (f.object.dg.expected_block) |_|
54265426 return f.fail("runtime code not allowed in naked function", .{});
54275427
......@@ -5431,7 +5431,7 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void
54315431 const else_body = it.elseBody();
54325432 try f.object.newline();
54335433
5434 try bw.writeAll("default: ");
5434 try w.writeAll("default: ");
54355435 if (any_range_cases) {
54365436 // We will iterate the cases again to handle those with ranges, and generate
54375437 // code using conditions rather than switch cases for such cases.
......@@ -5439,41 +5439,41 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void
54395439 while (it.next()) |case| {
54405440 if (case.ranges.len == 0) continue; // handled above
54415441
5442 try bw.writeAll("if (");
5442 try w.writeAll("if (");
54435443 for (case.items, 0..) |item, item_i| {
5444 if (item_i != 0) try bw.writeAll(" || ");
5445 try f.writeCValue(bw, condition, .Other);
5446 try bw.writeAll(" == ");
5447 try f.object.dg.renderValue(bw, (try f.air.value(item, pt)).?, .Other);
5444 if (item_i != 0) try w.writeAll(" || ");
5445 try f.writeCValue(w, condition, .Other);
5446 try w.writeAll(" == ");
5447 try f.object.dg.renderValue(w, (try f.air.value(item, pt)).?, .Other);
54485448 }
54495449 for (case.ranges, 0..) |range, range_i| {
5450 if (case.items.len != 0 or range_i != 0) try bw.writeAll(" || ");
5450 if (case.items.len != 0 or range_i != 0) try w.writeAll(" || ");
54515451 // "(x >= lower && x <= upper)"
5452 try bw.writeByte('(');
5453 try f.writeCValue(bw, condition, .Other);
5454 try bw.writeAll(" >= ");
5455 try f.object.dg.renderValue(bw, (try f.air.value(range[0], pt)).?, .Other);
5456 try bw.writeAll(" && ");
5457 try f.writeCValue(bw, condition, .Other);
5458 try bw.writeAll(" <= ");
5459 try f.object.dg.renderValue(bw, (try f.air.value(range[1], pt)).?, .Other);
5460 try bw.writeByte(')');
5452 try w.writeByte('(');
5453 try f.writeCValue(w, condition, .Other);
5454 try w.writeAll(" >= ");
5455 try f.object.dg.renderValue(w, (try f.air.value(range[0], pt)).?, .Other);
5456 try w.writeAll(" && ");
5457 try f.writeCValue(w, condition, .Other);
5458 try w.writeAll(" <= ");
5459 try f.object.dg.renderValue(w, (try f.air.value(range[1], pt)).?, .Other);
5460 try w.writeByte(')');
54615461 }
5462 try bw.writeAll(") {");
5462 try w.writeAll(") {");
54635463 f.object.indent();
54645464 try f.object.newline();
54655465 if (is_dispatch_loop) {
5466 try bw.print("zig_switch_{d}_dispatch_{d}: ", .{ @intFromEnum(inst), case.idx });
5466 try w.print("zig_switch_{d}_dispatch_{d}: ", .{ @intFromEnum(inst), case.idx });
54675467 }
54685468 try genBodyResolveState(f, inst, liveness.deaths[case.idx], case.body, true);
54695469 try f.object.outdent();
5470 try bw.writeByte('}');
5470 try w.writeByte('}');
54715471 if (f.object.dg.expected_block) |_|
54725472 return f.fail("runtime code not allowed in naked function", .{});
54735473 }
54745474 }
54755475 if (is_dispatch_loop) {
5476 try bw.print("zig_switch_{d}_dispatch_{d}: ", .{ @intFromEnum(inst), switch_br.cases_len });
5476 try w.print("zig_switch_{d}_dispatch_{d}: ", .{ @intFromEnum(inst), switch_br.cases_len });
54775477 }
54785478 if (else_body.len > 0) {
54795479 // Note that this must be the last case, so we do not need to use `genBodyResolveState` since
......@@ -5487,7 +5487,7 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void
54875487 } else try airUnreach(&f.object);
54885488 try f.object.newline();
54895489 try f.object.outdent();
5490 try bw.writeAll("}\n");
5490 try w.writeAll("}\n");
54915491}
54925492
54935493fn asmInputNeedsLocal(f: *Function, constraint: []const u8, value: CValue) bool {
......@@ -5525,7 +5525,7 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
55255525 extra_i += inputs.len;
55265526
55275527 const result = result: {
5528 const bw = &f.object.code.buffered_writer;
5528 const w = &f.object.code.buffered_writer;
55295529 const inst_ty = f.typeOfIndex(inst);
55305530 const inst_local = if (inst_ty.hasRuntimeBitsIgnoreComptime(zcu)) local: {
55315531 const inst_local = try f.allocLocalValue(.{
......@@ -5533,10 +5533,10 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
55335533 .alignas = CType.AlignAs.fromAbiAlignment(inst_ty.abiAlignment(zcu)),
55345534 });
55355535 if (f.wantSafety()) {
5536 try f.writeCValue(bw, inst_local, .Other);
5537 try bw.writeAll(" = ");
5538 try f.writeCValue(bw, .{ .undef = inst_ty }, .Other);
5539 try bw.writeByte(';');
5536 try f.writeCValue(w, inst_local, .Other);
5537 try w.writeAll(" = ");
5538 try f.writeCValue(w, .{ .undef = inst_ty }, .Other);
5539 try w.writeByte(';');
55405540 try f.object.newline();
55415541 }
55425542 break :local inst_local;
......@@ -5561,21 +5561,21 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
55615561 const is_reg = constraint[1] == '{';
55625562 if (is_reg) {
55635563 const output_ty = if (output == .none) inst_ty else f.typeOf(output).childType(zcu);
5564 try bw.writeAll("register ");
5564 try w.writeAll("register ");
55655565 const output_local = try f.allocLocalValue(.{
55665566 .ctype = try f.ctypeFromType(output_ty, .complete),
55675567 .alignas = CType.AlignAs.fromAbiAlignment(output_ty.abiAlignment(zcu)),
55685568 });
55695569 try f.allocs.put(gpa, output_local.new_local, false);
5570 try f.object.dg.renderTypeAndName(bw, output_ty, output_local, .{}, .none, .complete);
5571 try bw.writeAll(" __asm(\"");
5572 try bw.writeAll(constraint["={".len .. constraint.len - "}".len]);
5573 try bw.writeAll("\")");
5570 try f.object.dg.renderTypeAndName(w, output_ty, output_local, .{}, .none, .complete);
5571 try w.writeAll(" __asm(\"");
5572 try w.writeAll(constraint["={".len .. constraint.len - "}".len]);
5573 try w.writeAll("\")");
55745574 if (f.wantSafety()) {
5575 try bw.writeAll(" = ");
5576 try f.writeCValue(bw, .{ .undef = output_ty }, .Other);
5575 try w.writeAll(" = ");
5576 try f.writeCValue(w, .{ .undef = output_ty }, .Other);
55775577 }
5578 try bw.writeByte(';');
5578 try w.writeByte(';');
55795579 try f.object.newline();
55805580 }
55815581 }
......@@ -5597,21 +5597,21 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
55975597 const input_val = try f.resolveInst(input);
55985598 if (asmInputNeedsLocal(f, constraint, input_val)) {
55995599 const input_ty = f.typeOf(input);
5600 if (is_reg) try bw.writeAll("register ");
5600 if (is_reg) try w.writeAll("register ");
56015601 const input_local = try f.allocLocalValue(.{
56025602 .ctype = try f.ctypeFromType(input_ty, .complete),
56035603 .alignas = CType.AlignAs.fromAbiAlignment(input_ty.abiAlignment(zcu)),
56045604 });
56055605 try f.allocs.put(gpa, input_local.new_local, false);
5606 try f.object.dg.renderTypeAndName(bw, input_ty, input_local, Const, .none, .complete);
5606 try f.object.dg.renderTypeAndName(w, input_ty, input_local, Const, .none, .complete);
56075607 if (is_reg) {
5608 try bw.writeAll(" __asm(\"");
5609 try bw.writeAll(constraint["{".len .. constraint.len - "}".len]);
5610 try bw.writeAll("\")");
5608 try w.writeAll(" __asm(\"");
5609 try w.writeAll(constraint["{".len .. constraint.len - "}".len]);
5610 try w.writeAll("\")");
56115611 }
5612 try bw.writeAll(" = ");
5613 try f.writeCValue(bw, input_val, .Other);
5614 try bw.writeByte(';');
5612 try w.writeAll(" = ");
5613 try f.writeCValue(w, input_val, .Other);
5614 try w.writeByte(';');
56155615 try f.object.newline();
56165616 }
56175617 }
......@@ -5672,14 +5672,14 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
56725672 }
56735673 }
56745674
5675 try bw.writeAll("__asm");
5676 if (is_volatile) try bw.writeAll(" volatile");
5677 try bw.print("({fs}", .{fmtStringLiteral(fixed_asm_source[0..dst_i], null)});
5675 try w.writeAll("__asm");
5676 if (is_volatile) try w.writeAll(" volatile");
5677 try w.print("({fs}", .{fmtStringLiteral(fixed_asm_source[0..dst_i], null)});
56785678 }
56795679
56805680 extra_i = constraints_extra_begin;
56815681 var locals_index = locals_begin;
5682 try bw.writeByte(':');
5682 try w.writeByte(':');
56835683 for (outputs, 0..) |output, index| {
56845684 const extra_bytes = mem.sliceAsBytes(f.air.extra.items[extra_i..]);
56855685 const constraint = mem.sliceTo(extra_bytes, 0);
......@@ -5688,22 +5688,22 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
56885688 // for the string, we still use the next u32 for the null terminator.
56895689 extra_i += (constraint.len + name.len + (2 + 3)) / 4;
56905690
5691 if (index > 0) try bw.writeByte(',');
5692 try bw.writeByte(' ');
5693 if (!mem.eql(u8, name, "_")) try bw.print("[{s}]", .{name});
5691 if (index > 0) try w.writeByte(',');
5692 try w.writeByte(' ');
5693 if (!mem.eql(u8, name, "_")) try w.print("[{s}]", .{name});
56945694 const is_reg = constraint[1] == '{';
5695 try bw.print("{fs}(", .{fmtStringLiteral(if (is_reg) "=r" else constraint, null)});
5695 try w.print("{fs}(", .{fmtStringLiteral(if (is_reg) "=r" else constraint, null)});
56965696 if (is_reg) {
5697 try f.writeCValue(bw, .{ .local = locals_index }, .Other);
5697 try f.writeCValue(w, .{ .local = locals_index }, .Other);
56985698 locals_index += 1;
56995699 } else if (output == .none) {
5700 try f.writeCValue(bw, inst_local, .FunctionArgument);
5700 try f.writeCValue(w, inst_local, .FunctionArgument);
57015701 } else {
5702 try f.writeCValueDeref(bw, try f.resolveInst(output));
5702 try f.writeCValueDeref(w, try f.resolveInst(output));
57035703 }
5704 try bw.writeByte(')');
5704 try w.writeByte(')');
57055705 }
5706 try bw.writeByte(':');
5706 try w.writeByte(':');
57075707 for (inputs, 0..) |input, index| {
57085708 const extra_bytes = mem.sliceAsBytes(f.air.extra.items[extra_i..]);
57095709 const constraint = mem.sliceTo(extra_bytes, 0);
......@@ -5712,21 +5712,21 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
57125712 // for the string, we still use the next u32 for the null terminator.
57135713 extra_i += (constraint.len + name.len + (2 + 3)) / 4;
57145714
5715 if (index > 0) try bw.writeByte(',');
5716 try bw.writeByte(' ');
5717 if (!mem.eql(u8, name, "_")) try bw.print("[{s}]", .{name});
5715 if (index > 0) try w.writeByte(',');
5716 try w.writeByte(' ');
5717 if (!mem.eql(u8, name, "_")) try w.print("[{s}]", .{name});
57185718
57195719 const is_reg = constraint[0] == '{';
57205720 const input_val = try f.resolveInst(input);
5721 try bw.print("{fs}(", .{fmtStringLiteral(if (is_reg) "r" else constraint, null)});
5722 try f.writeCValue(bw, if (asmInputNeedsLocal(f, constraint, input_val)) local: {
5721 try w.print("{fs}(", .{fmtStringLiteral(if (is_reg) "r" else constraint, null)});
5722 try f.writeCValue(w, if (asmInputNeedsLocal(f, constraint, input_val)) local: {
57235723 const input_local_idx = locals_index;
57245724 locals_index += 1;
57255725 break :local .{ .local = input_local_idx };
57265726 } else input_val, .Other);
5727 try bw.writeByte(')');
5727 try w.writeByte(')');
57285728 }
5729 try bw.writeByte(':');
5729 try w.writeByte(':');
57305730 for (0..clobbers_len) |clobber_i| {
57315731 const clobber = mem.sliceTo(mem.sliceAsBytes(f.air.extra.items[extra_i..]), 0);
57325732 // This equation accounts for the fact that even if we have exactly 4 bytes
......@@ -5735,10 +5735,10 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
57355735
57365736 if (clobber.len == 0) continue;
57375737
5738 if (clobber_i > 0) try bw.writeByte(',');
5739 try bw.print(" {fs}", .{fmtStringLiteral(clobber, null)});
5738 if (clobber_i > 0) try w.writeByte(',');
5739 try w.print(" {fs}", .{fmtStringLiteral(clobber, null)});
57405740 }
5741 try bw.writeAll(");");
5741 try w.writeAll(");");
57425742 try f.object.newline();
57435743
57445744 extra_i = constraints_extra_begin;
......@@ -5753,14 +5753,14 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
57535753
57545754 const is_reg = constraint[1] == '{';
57555755 if (is_reg) {
5756 try f.writeCValueDeref(bw, if (output == .none)
5756 try f.writeCValueDeref(w, if (output == .none)
57575757 .{ .local_ref = inst_local.new_local }
57585758 else
57595759 try f.resolveInst(output));
5760 try bw.writeAll(" = ");
5761 try f.writeCValue(bw, .{ .local = locals_index }, .Other);
5760 try w.writeAll(" = ");
5761 try f.writeCValue(w, .{ .local = locals_index }, .Other);
57625762 locals_index += 1;
5763 try bw.writeByte(';');
5763 try w.writeByte(';');
57645764 try f.object.newline();
57655765 }
57665766 }
......@@ -5791,14 +5791,14 @@ fn airIsNull(
57915791 const ctype_pool = &f.object.dg.ctype_pool;
57925792 const un_op = f.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
57935793
5794 const bw = &f.object.code.buffered_writer;
5794 const w = &f.object.code.buffered_writer;
57955795 const operand = try f.resolveInst(un_op);
57965796 try reap(f, inst, &.{un_op});
57975797
57985798 const local = try f.allocLocal(inst, .bool);
5799 const a = try Assignment.start(f, bw, .bool);
5800 try f.writeCValue(bw, local, .Other);
5801 try a.assign(f, bw);
5799 const a = try Assignment.start(f, w, .bool);
5800 try f.writeCValue(w, local, .Other);
5801 try a.assign(f, w);
58025802
58035803 const operand_ty = f.typeOf(un_op);
58045804 const optional_ty = if (is_ptr) operand_ty.childType(zcu) else operand_ty;
......@@ -5806,9 +5806,9 @@ fn airIsNull(
58065806 const rhs = switch (opt_ctype.info(ctype_pool)) {
58075807 .basic, .pointer => rhs: {
58085808 if (is_ptr)
5809 try f.writeCValueDeref(bw, operand)
5809 try f.writeCValueDeref(w, operand)
58105810 else
5811 try f.writeCValue(bw, operand, .Other);
5811 try f.writeCValue(w, operand, .Other);
58125812 break :rhs if (opt_ctype.isBool())
58135813 "true"
58145814 else if (opt_ctype.isInteger())
......@@ -5820,24 +5820,24 @@ fn airIsNull(
58205820 .aggregate => |aggregate| switch (aggregate.fields.at(0, ctype_pool).name.index) {
58215821 .is_null, .payload => rhs: {
58225822 if (is_ptr)
5823 try f.writeCValueDerefMember(bw, operand, .{ .identifier = "is_null" })
5823 try f.writeCValueDerefMember(w, operand, .{ .identifier = "is_null" })
58245824 else
5825 try f.writeCValueMember(bw, operand, .{ .identifier = "is_null" });
5825 try f.writeCValueMember(w, operand, .{ .identifier = "is_null" });
58265826 break :rhs "true";
58275827 },
58285828 .ptr, .len => rhs: {
58295829 if (is_ptr)
5830 try f.writeCValueDerefMember(bw, operand, .{ .identifier = "ptr" })
5830 try f.writeCValueDerefMember(w, operand, .{ .identifier = "ptr" })
58315831 else
5832 try f.writeCValueMember(bw, operand, .{ .identifier = "ptr" });
5832 try f.writeCValueMember(w, operand, .{ .identifier = "ptr" });
58335833 break :rhs "NULL";
58345834 },
58355835 else => unreachable,
58365836 },
58375837 };
5838 try bw.writeAll(compareOperatorC(operator));
5839 try bw.writeAll(rhs);
5840 try a.end(f, bw);
5838 try w.writeAll(compareOperatorC(operator));
5839 try w.writeAll(rhs);
5840 try a.end(f, w);
58415841 return local;
58425842}
58435843
......@@ -5859,16 +5859,16 @@ fn airOptionalPayload(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValue
58595859 .aligned, .array, .vector, .fwd_decl, .function => unreachable,
58605860 .aggregate => |aggregate| switch (aggregate.fields.at(0, ctype_pool).name.index) {
58615861 .is_null, .payload => {
5862 const bw = &f.object.code.buffered_writer;
5862 const w = &f.object.code.buffered_writer;
58635863 const local = try f.allocLocal(inst, inst_ty);
5864 const a = try Assignment.start(f, bw, try f.ctypeFromType(inst_ty, .complete));
5865 try f.writeCValue(bw, local, .Other);
5866 try a.assign(f, bw);
5864 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete));
5865 try f.writeCValue(w, local, .Other);
5866 try a.assign(f, w);
58675867 if (is_ptr) {
5868 try bw.writeByte('&');
5869 try f.writeCValueDerefMember(bw, operand, .{ .identifier = "payload" });
5870 } else try f.writeCValueMember(bw, operand, .{ .identifier = "payload" });
5871 try a.end(f, bw);
5868 try w.writeByte('&');
5869 try f.writeCValueDerefMember(w, operand, .{ .identifier = "payload" });
5870 } else try f.writeCValueMember(w, operand, .{ .identifier = "payload" });
5871 try a.end(f, w);
58725872 return local;
58735873 },
58745874 .ptr, .len => return f.moveCValue(inst, inst_ty, operand),
......@@ -5881,7 +5881,7 @@ fn airOptionalPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue {
58815881 const pt = f.object.dg.pt;
58825882 const zcu = pt.zcu;
58835883 const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
5884 const bw = &f.object.code.buffered_writer;
5884 const w = &f.object.code.buffered_writer;
58855885 const operand = try f.resolveInst(ty_op.operand);
58865886 try reap(f, inst, &.{ty_op.operand});
58875887 const operand_ty = f.typeOf(ty_op.operand);
......@@ -5890,40 +5890,40 @@ fn airOptionalPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue {
58905890 const opt_ctype = try f.ctypeFromType(operand_ty.childType(zcu), .complete);
58915891 switch (opt_ctype.info(&f.object.dg.ctype_pool)) {
58925892 .basic => {
5893 const a = try Assignment.start(f, bw, opt_ctype);
5894 try f.writeCValueDeref(bw, operand);
5895 try a.assign(f, bw);
5896 try f.object.dg.renderValue(bw, Value.false, .Other);
5897 try a.end(f, bw);
5893 const a = try Assignment.start(f, w, opt_ctype);
5894 try f.writeCValueDeref(w, operand);
5895 try a.assign(f, w);
5896 try f.object.dg.renderValue(w, Value.false, .Other);
5897 try a.end(f, w);
58985898 return .none;
58995899 },
59005900 .pointer => {
59015901 if (f.liveness.isUnused(inst)) return .none;
59025902 const local = try f.allocLocal(inst, inst_ty);
5903 const a = try Assignment.start(f, bw, opt_ctype);
5904 try f.writeCValue(bw, local, .Other);
5905 try a.assign(f, bw);
5906 try f.writeCValue(bw, operand, .Other);
5907 try a.end(f, bw);
5903 const a = try Assignment.start(f, w, opt_ctype);
5904 try f.writeCValue(w, local, .Other);
5905 try a.assign(f, w);
5906 try f.writeCValue(w, operand, .Other);
5907 try a.end(f, w);
59085908 return local;
59095909 },
59105910 .aligned, .array, .vector, .fwd_decl, .function => unreachable,
59115911 .aggregate => {
59125912 {
5913 const a = try Assignment.start(f, bw, opt_ctype);
5914 try f.writeCValueDerefMember(bw, operand, .{ .identifier = "is_null" });
5915 try a.assign(f, bw);
5916 try f.object.dg.renderValue(bw, Value.false, .Other);
5917 try a.end(f, bw);
5913 const a = try Assignment.start(f, w, opt_ctype);
5914 try f.writeCValueDerefMember(w, operand, .{ .identifier = "is_null" });
5915 try a.assign(f, w);
5916 try f.object.dg.renderValue(w, Value.false, .Other);
5917 try a.end(f, w);
59185918 }
59195919 if (f.liveness.isUnused(inst)) return .none;
59205920 const local = try f.allocLocal(inst, inst_ty);
5921 const a = try Assignment.start(f, bw, opt_ctype);
5922 try f.writeCValue(bw, local, .Other);
5923 try a.assign(f, bw);
5924 try bw.writeByte('&');
5925 try f.writeCValueDerefMember(bw, operand, .{ .identifier = "payload" });
5926 try a.end(f, bw);
5921 const a = try Assignment.start(f, w, opt_ctype);
5922 try f.writeCValue(w, local, .Other);
5923 try a.assign(f, w);
5924 try w.writeByte('&');
5925 try f.writeCValueDerefMember(w, operand, .{ .identifier = "payload" });
5926 try a.end(f, w);
59275927 return local;
59285928 },
59295929 }
......@@ -6031,42 +6031,42 @@ fn airFieldParentPtr(f: *Function, inst: Air.Inst.Index) !CValue {
60316031 const field_ptr_val = try f.resolveInst(extra.field_ptr);
60326032 try reap(f, inst, &.{extra.field_ptr});
60336033
6034 const bw = &f.object.code.buffered_writer;
6034 const w = &f.object.code.buffered_writer;
60356035 const local = try f.allocLocal(inst, container_ptr_ty);
6036 try f.writeCValue(bw, local, .Other);
6037 try bw.writeAll(" = (");
6038 try f.renderType(bw, container_ptr_ty);
6039 try bw.writeByte(')');
6036 try f.writeCValue(w, local, .Other);
6037 try w.writeAll(" = (");
6038 try f.renderType(w, container_ptr_ty);
6039 try w.writeByte(')');
60406040
60416041 switch (fieldLocation(container_ptr_ty, field_ptr_ty, extra.field_index, pt)) {
6042 .begin => try f.writeCValue(bw, field_ptr_val, .Other),
6042 .begin => try f.writeCValue(w, field_ptr_val, .Other),
60436043 .field => |field| {
60446044 const u8_ptr_ty = try pt.adjustPtrTypeChild(field_ptr_ty, .u8);
60456045
6046 try bw.writeAll("((");
6047 try f.renderType(bw, u8_ptr_ty);
6048 try bw.writeByte(')');
6049 try f.writeCValue(bw, field_ptr_val, .Other);
6050 try bw.writeAll(" - offsetof(");
6051 try f.renderType(bw, container_ty);
6052 try bw.writeAll(", ");
6053 try f.writeCValue(bw, field, .Other);
6054 try bw.writeAll("))");
6046 try w.writeAll("((");
6047 try f.renderType(w, u8_ptr_ty);
6048 try w.writeByte(')');
6049 try f.writeCValue(w, field_ptr_val, .Other);
6050 try w.writeAll(" - offsetof(");
6051 try f.renderType(w, container_ty);
6052 try w.writeAll(", ");
6053 try f.writeCValue(w, field, .Other);
6054 try w.writeAll("))");
60556055 },
60566056 .byte_offset => |byte_offset| {
60576057 const u8_ptr_ty = try pt.adjustPtrTypeChild(field_ptr_ty, .u8);
60586058
6059 try bw.writeAll("((");
6060 try f.renderType(bw, u8_ptr_ty);
6061 try bw.writeByte(')');
6062 try f.writeCValue(bw, field_ptr_val, .Other);
6063 try bw.print(" - {f})", .{
6059 try w.writeAll("((");
6060 try f.renderType(w, u8_ptr_ty);
6061 try w.writeByte(')');
6062 try f.writeCValue(w, field_ptr_val, .Other);
6063 try w.print(" - {f})", .{
60646064 try f.fmtIntLiteral(try pt.intValue(.usize, byte_offset)),
60656065 });
60666066 },
60676067 }
60686068
6069 try bw.writeByte(';');
6069 try w.writeByte(';');
60706070 try f.object.newline();
60716071 return local;
60726072}
......@@ -6086,33 +6086,33 @@ fn fieldPtr(
60866086 // Ensure complete type definition is visible before accessing fields.
60876087 _ = try f.ctypeFromType(container_ty, .complete);
60886088
6089 const bw = &f.object.code.buffered_writer;
6089 const w = &f.object.code.buffered_writer;
60906090 const local = try f.allocLocal(inst, field_ptr_ty);
6091 try f.writeCValue(bw, local, .Other);
6092 try bw.writeAll(" = (");
6093 try f.renderType(bw, field_ptr_ty);
6094 try bw.writeByte(')');
6091 try f.writeCValue(w, local, .Other);
6092 try w.writeAll(" = (");
6093 try f.renderType(w, field_ptr_ty);
6094 try w.writeByte(')');
60956095
60966096 switch (fieldLocation(container_ptr_ty, field_ptr_ty, field_index, pt)) {
6097 .begin => try f.writeCValue(bw, container_ptr_val, .Other),
6097 .begin => try f.writeCValue(w, container_ptr_val, .Other),
60986098 .field => |field| {
6099 try bw.writeByte('&');
6100 try f.writeCValueDerefMember(bw, container_ptr_val, field);
6099 try w.writeByte('&');
6100 try f.writeCValueDerefMember(w, container_ptr_val, field);
61016101 },
61026102 .byte_offset => |byte_offset| {
61036103 const u8_ptr_ty = try pt.adjustPtrTypeChild(field_ptr_ty, .u8);
61046104
6105 try bw.writeAll("((");
6106 try f.renderType(bw, u8_ptr_ty);
6107 try bw.writeByte(')');
6108 try f.writeCValue(bw, container_ptr_val, .Other);
6109 try bw.print(" + {f})", .{
6105 try w.writeAll("((");
6106 try f.renderType(w, u8_ptr_ty);
6107 try w.writeByte(')');
6108 try f.writeCValue(w, container_ptr_val, .Other);
6109 try w.print(" + {f})", .{
61106110 try f.fmtIntLiteral(try pt.intValue(.usize, byte_offset)),
61116111 });
61126112 },
61136113 }
61146114
6115 try bw.writeByte(';');
6115 try w.writeByte(';');
61166116 try f.object.newline();
61176117 return local;
61186118}
......@@ -6133,7 +6133,7 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue {
61336133 const struct_byval = try f.resolveInst(extra.struct_operand);
61346134 try reap(f, inst, &.{extra.struct_operand});
61356135 const struct_ty = f.typeOf(extra.struct_operand);
6136 const bw = &f.object.code.buffered_writer;
6136 const w = &f.object.code.buffered_writer;
61376137
61386138 // Ensure complete type definition is visible before accessing fields.
61396139 _ = try f.ctypeFromType(struct_ty, .complete);
......@@ -6160,43 +6160,43 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue {
61606160 const field_int_ty = try pt.intType(field_int_signedness, @as(u16, @intCast(inst_ty.bitSize(zcu))));
61616161
61626162 const temp_local = try f.allocLocal(inst, field_int_ty);
6163 try f.writeCValue(bw, temp_local, .Other);
6164 try bw.writeAll(" = zig_wrap_");
6165 try f.object.dg.renderTypeForBuiltinFnName(bw, field_int_ty);
6166 try bw.writeAll("((");
6167 try f.renderType(bw, field_int_ty);
6168 try bw.writeByte(')');
6163 try f.writeCValue(w, temp_local, .Other);
6164 try w.writeAll(" = zig_wrap_");
6165 try f.object.dg.renderTypeForBuiltinFnName(w, field_int_ty);
6166 try w.writeAll("((");
6167 try f.renderType(w, field_int_ty);
6168 try w.writeByte(')');
61696169 const cant_cast = int_info.bits > 64;
61706170 if (cant_cast) {
61716171 if (field_int_ty.bitSize(zcu) > 64) return f.fail("TODO: C backend: implement casting between types > 64 bits", .{});
6172 try bw.writeAll("zig_lo_");
6173 try f.object.dg.renderTypeForBuiltinFnName(bw, struct_ty);
6174 try bw.writeByte('(');
6172 try w.writeAll("zig_lo_");
6173 try f.object.dg.renderTypeForBuiltinFnName(w, struct_ty);
6174 try w.writeByte('(');
61756175 }
61766176 if (bit_offset > 0) {
6177 try bw.writeAll("zig_shr_");
6178 try f.object.dg.renderTypeForBuiltinFnName(bw, struct_ty);
6179 try bw.writeByte('(');
6177 try w.writeAll("zig_shr_");
6178 try f.object.dg.renderTypeForBuiltinFnName(w, struct_ty);
6179 try w.writeByte('(');
61806180 }
6181 try f.writeCValue(bw, struct_byval, .Other);
6182 if (bit_offset > 0) try bw.print(", {f})", .{
6181 try f.writeCValue(w, struct_byval, .Other);
6182 if (bit_offset > 0) try w.print(", {f})", .{
61836183 try f.fmtIntLiteral(try pt.intValue(bit_offset_ty, bit_offset)),
61846184 });
6185 if (cant_cast) try bw.writeByte(')');
6186 try f.object.dg.renderBuiltinInfo(bw, field_int_ty, .bits);
6187 try bw.writeAll(");");
6185 if (cant_cast) try w.writeByte(')');
6186 try f.object.dg.renderBuiltinInfo(w, field_int_ty, .bits);
6187 try w.writeAll(");");
61886188 try f.object.newline();
61896189 if (inst_ty.eql(field_int_ty, zcu)) return temp_local;
61906190
61916191 const local = try f.allocLocal(inst, inst_ty);
61926192 if (local.new_local != temp_local.new_local) {
6193 try bw.writeAll("memcpy(");
6194 try f.writeCValue(bw, .{ .local_ref = local.new_local }, .FunctionArgument);
6195 try bw.writeAll(", ");
6196 try f.writeCValue(bw, .{ .local_ref = temp_local.new_local }, .FunctionArgument);
6197 try bw.writeAll(", sizeof(");
6198 try f.renderType(bw, inst_ty);
6199 try bw.writeAll("));");
6193 try w.writeAll("memcpy(");
6194 try f.writeCValue(w, .{ .local_ref = local.new_local }, .FunctionArgument);
6195 try w.writeAll(", ");
6196 try f.writeCValue(w, .{ .local_ref = temp_local.new_local }, .FunctionArgument);
6197 try w.writeAll(", sizeof(");
6198 try f.renderType(w, inst_ty);
6199 try w.writeAll("));");
62006200 try f.object.newline();
62016201 }
62026202 try freeLocal(f, inst, temp_local.new_local, null);
......@@ -6218,10 +6218,10 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue {
62186218 .@"packed" => {
62196219 const operand_lval = if (struct_byval == .constant) blk: {
62206220 const operand_local = try f.allocLocal(inst, struct_ty);
6221 try f.writeCValue(bw, operand_local, .Other);
6222 try bw.writeAll(" = ");
6223 try f.writeCValue(bw, struct_byval, .Other);
6224 try bw.writeByte(';');
6221 try f.writeCValue(w, operand_local, .Other);
6222 try w.writeAll(" = ");
6223 try f.writeCValue(w, struct_byval, .Other);
6224 try w.writeByte(';');
62256225 try f.object.newline();
62266226 break :blk operand_local;
62276227 } else struct_byval;
......@@ -6233,13 +6233,13 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue {
62336233 },
62346234 else => true,
62356235 }) {
6236 try bw.writeAll("memcpy(&");
6237 try f.writeCValue(bw, local, .Other);
6238 try bw.writeAll(", &");
6239 try f.writeCValue(bw, operand_lval, .Other);
6240 try bw.writeAll(", sizeof(");
6241 try f.renderType(bw, inst_ty);
6242 try bw.writeAll("));");
6236 try w.writeAll("memcpy(&");
6237 try f.writeCValue(w, local, .Other);
6238 try w.writeAll(", &");
6239 try f.writeCValue(w, operand_lval, .Other);
6240 try w.writeAll(", sizeof(");
6241 try f.renderType(w, inst_ty);
6242 try w.writeAll("));");
62436243 try f.object.newline();
62446244 }
62456245 try f.freeCValue(inst, operand_lval);
......@@ -6251,11 +6251,11 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue {
62516251 };
62526252
62536253 const local = try f.allocLocal(inst, inst_ty);
6254 const a = try Assignment.start(f, bw, try f.ctypeFromType(inst_ty, .complete));
6255 try f.writeCValue(bw, local, .Other);
6256 try a.assign(f, bw);
6257 try f.writeCValueMember(bw, struct_byval, field_name);
6258 try a.end(f, bw);
6254 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete));
6255 try f.writeCValue(w, local, .Other);
6256 try a.assign(f, w);
6257 try f.writeCValueMember(w, struct_byval, field_name);
6258 try a.end(f, w);
62596259 return local;
62606260}
62616261
......@@ -6282,21 +6282,21 @@ fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue {
62826282 return local;
62836283 }
62846284
6285 const bw = &f.object.code.buffered_writer;
6286 try f.writeCValue(bw, local, .Other);
6287 try bw.writeAll(" = ");
6285 const w = &f.object.code.buffered_writer;
6286 try f.writeCValue(w, local, .Other);
6287 try w.writeAll(" = ");
62886288
62896289 if (!payload_ty.hasRuntimeBits(zcu))
6290 try f.writeCValue(bw, operand, .Other)
6290 try f.writeCValue(w, operand, .Other)
62916291 else if (error_ty.errorSetIsEmpty(zcu))
6292 try bw.print("{f}", .{
6292 try w.print("{f}", .{
62936293 try f.fmtIntLiteral(try pt.intValue(try pt.errorIntType(), 0)),
62946294 })
62956295 else if (operand_is_ptr)
6296 try f.writeCValueDerefMember(bw, operand, .{ .identifier = "error" })
6296 try f.writeCValueDerefMember(w, operand, .{ .identifier = "error" })
62976297 else
6298 try f.writeCValueMember(bw, operand, .{ .identifier = "error" });
6299 try bw.writeByte(';');
6298 try f.writeCValueMember(w, operand, .{ .identifier = "error" });
6299 try w.writeByte(';');
63006300 try f.object.newline();
63016301 return local;
63026302}
......@@ -6312,30 +6312,30 @@ fn airUnwrapErrUnionPay(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValu
63126312 const operand_ty = f.typeOf(ty_op.operand);
63136313 const error_union_ty = if (is_ptr) operand_ty.childType(zcu) else operand_ty;
63146314
6315 const bw = &f.object.code.buffered_writer;
6315 const w = &f.object.code.buffered_writer;
63166316 if (!error_union_ty.errorUnionPayload(zcu).hasRuntimeBits(zcu)) {
63176317 if (!is_ptr) return .none;
63186318
63196319 const local = try f.allocLocal(inst, inst_ty);
6320 try f.writeCValue(bw, local, .Other);
6321 try bw.writeAll(" = (");
6322 try f.renderType(bw, inst_ty);
6323 try bw.writeByte(')');
6324 try f.writeCValue(bw, operand, .Other);
6325 try bw.writeByte(';');
6320 try f.writeCValue(w, local, .Other);
6321 try w.writeAll(" = (");
6322 try f.renderType(w, inst_ty);
6323 try w.writeByte(')');
6324 try f.writeCValue(w, operand, .Other);
6325 try w.writeByte(';');
63266326 try f.object.newline();
63276327 return local;
63286328 }
63296329
63306330 const local = try f.allocLocal(inst, inst_ty);
6331 const a = try Assignment.start(f, bw, try f.ctypeFromType(inst_ty, .complete));
6332 try f.writeCValue(bw, local, .Other);
6333 try a.assign(f, bw);
6331 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete));
6332 try f.writeCValue(w, local, .Other);
6333 try a.assign(f, w);
63346334 if (is_ptr) {
6335 try bw.writeByte('&');
6336 try f.writeCValueDerefMember(bw, operand, .{ .identifier = "payload" });
6337 } else try f.writeCValueMember(bw, operand, .{ .identifier = "payload" });
6338 try a.end(f, bw);
6335 try w.writeByte('&');
6336 try f.writeCValueDerefMember(w, operand, .{ .identifier = "payload" });
6337 } else try f.writeCValueMember(w, operand, .{ .identifier = "payload" });
6338 try a.end(f, w);
63396339 return local;
63406340}
63416341
......@@ -6354,21 +6354,21 @@ fn airWrapOptional(f: *Function, inst: Air.Inst.Index) !CValue {
63546354 .aggregate => |aggregate| switch (aggregate.fields.at(0, ctype_pool).name.index) {
63556355 .is_null, .payload => {
63566356 const operand_ctype = try f.ctypeFromType(f.typeOf(ty_op.operand), .complete);
6357 const bw = &f.object.code.buffered_writer;
6357 const w = &f.object.code.buffered_writer;
63586358 const local = try f.allocLocal(inst, inst_ty);
63596359 {
6360 const a = try Assignment.start(f, bw, .bool);
6361 try f.writeCValueMember(bw, local, .{ .identifier = "is_null" });
6362 try a.assign(f, bw);
6363 try bw.writeAll("false");
6364 try a.end(f, bw);
6360 const a = try Assignment.start(f, w, .bool);
6361 try f.writeCValueMember(w, local, .{ .identifier = "is_null" });
6362 try a.assign(f, w);
6363 try w.writeAll("false");
6364 try a.end(f, w);
63656365 }
63666366 {
6367 const a = try Assignment.start(f, bw, operand_ctype);
6368 try f.writeCValueMember(bw, local, .{ .identifier = "payload" });
6369 try a.assign(f, bw);
6370 try f.writeCValue(bw, operand, .Other);
6371 try a.end(f, bw);
6367 const a = try Assignment.start(f, w, operand_ctype);
6368 try f.writeCValueMember(w, local, .{ .identifier = "payload" });
6369 try a.assign(f, w);
6370 try f.writeCValue(w, operand, .Other);
6371 try a.end(f, w);
63726372 }
63736373 return local;
63746374 },
......@@ -6390,7 +6390,7 @@ fn airWrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue {
63906390 const err = try f.resolveInst(ty_op.operand);
63916391 try reap(f, inst, &.{ty_op.operand});
63926392
6393 const bw = &f.object.code.buffered_writer;
6393 const w = &f.object.code.buffered_writer;
63946394 const local = try f.allocLocal(inst, inst_ty);
63956395
63966396 if (repr_is_err and err == .local and err.local == local.new_local) {
......@@ -6399,21 +6399,21 @@ fn airWrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue {
63996399 }
64006400
64016401 if (!repr_is_err) {
6402 const a = try Assignment.start(f, bw, try f.ctypeFromType(payload_ty, .complete));
6403 try f.writeCValueMember(bw, local, .{ .identifier = "payload" });
6404 try a.assign(f, bw);
6405 try f.object.dg.renderUndefValue(bw, payload_ty, .Other);
6406 try a.end(f, bw);
6402 const a = try Assignment.start(f, w, try f.ctypeFromType(payload_ty, .complete));
6403 try f.writeCValueMember(w, local, .{ .identifier = "payload" });
6404 try a.assign(f, w);
6405 try f.object.dg.renderUndefValue(w, payload_ty, .Other);
6406 try a.end(f, w);
64076407 }
64086408 {
6409 const a = try Assignment.start(f, bw, try f.ctypeFromType(err_ty, .complete));
6409 const a = try Assignment.start(f, w, try f.ctypeFromType(err_ty, .complete));
64106410 if (repr_is_err)
6411 try f.writeCValue(bw, local, .Other)
6411 try f.writeCValue(w, local, .Other)
64126412 else
6413 try f.writeCValueMember(bw, local, .{ .identifier = "error" });
6414 try a.assign(f, bw);
6415 try f.writeCValue(bw, err, .Other);
6416 try a.end(f, bw);
6413 try f.writeCValueMember(w, local, .{ .identifier = "error" });
6414 try a.assign(f, w);
6415 try f.writeCValue(w, err, .Other);
6416 try a.end(f, w);
64176417 }
64186418 return local;
64196419}
......@@ -6421,7 +6421,7 @@ fn airWrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue {
64216421fn airErrUnionPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue {
64226422 const pt = f.object.dg.pt;
64236423 const zcu = pt.zcu;
6424 const bw = &f.object.code.buffered_writer;
6424 const w = &f.object.code.buffered_writer;
64256425 const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
64266426 const inst_ty = f.typeOfIndex(inst);
64276427 const operand = try f.resolveInst(ty_op.operand);
......@@ -6435,31 +6435,31 @@ fn airErrUnionPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue {
64356435
64366436 // First, set the non-error value.
64376437 if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) {
6438 const a = try Assignment.start(f, bw, try f.ctypeFromType(operand_ty, .complete));
6439 try f.writeCValueDeref(bw, operand);
6440 try a.assign(f, bw);
6441 try bw.print("{f}", .{try f.fmtIntLiteral(no_err)});
6442 try a.end(f, bw);
6438 const a = try Assignment.start(f, w, try f.ctypeFromType(operand_ty, .complete));
6439 try f.writeCValueDeref(w, operand);
6440 try a.assign(f, w);
6441 try w.print("{f}", .{try f.fmtIntLiteral(no_err)});
6442 try a.end(f, w);
64436443 return .none;
64446444 }
64456445 {
6446 const a = try Assignment.start(f, bw, try f.ctypeFromType(err_int_ty, .complete));
6447 try f.writeCValueDerefMember(bw, operand, .{ .identifier = "error" });
6448 try a.assign(f, bw);
6449 try bw.print("{f}", .{try f.fmtIntLiteral(no_err)});
6450 try a.end(f, bw);
6446 const a = try Assignment.start(f, w, try f.ctypeFromType(err_int_ty, .complete));
6447 try f.writeCValueDerefMember(w, operand, .{ .identifier = "error" });
6448 try a.assign(f, w);
6449 try w.print("{f}", .{try f.fmtIntLiteral(no_err)});
6450 try a.end(f, w);
64516451 }
64526452
64536453 // Then return the payload pointer (only if it is used)
64546454 if (f.liveness.isUnused(inst)) return .none;
64556455
64566456 const local = try f.allocLocal(inst, inst_ty);
6457 const a = try Assignment.start(f, bw, try f.ctypeFromType(inst_ty, .complete));
6458 try f.writeCValue(bw, local, .Other);
6459 try a.assign(f, bw);
6460 try bw.writeByte('&');
6461 try f.writeCValueDerefMember(bw, operand, .{ .identifier = "payload" });
6462 try a.end(f, bw);
6457 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete));
6458 try f.writeCValue(w, local, .Other);
6459 try a.assign(f, w);
6460 try w.writeByte('&');
6461 try f.writeCValueDerefMember(w, operand, .{ .identifier = "payload" });
6462 try a.end(f, w);
64636463 return local;
64646464}
64656465
......@@ -6490,24 +6490,24 @@ fn airWrapErrUnionPay(f: *Function, inst: Air.Inst.Index) !CValue {
64906490 const err_ty = inst_ty.errorUnionSet(zcu);
64916491 try reap(f, inst, &.{ty_op.operand});
64926492
6493 const bw = &f.object.code.buffered_writer;
6493 const w = &f.object.code.buffered_writer;
64946494 const local = try f.allocLocal(inst, inst_ty);
64956495 if (!repr_is_err) {
6496 const a = try Assignment.start(f, bw, try f.ctypeFromType(payload_ty, .complete));
6497 try f.writeCValueMember(bw, local, .{ .identifier = "payload" });
6498 try a.assign(f, bw);
6499 try f.writeCValue(bw, payload, .Other);
6500 try a.end(f, bw);
6496 const a = try Assignment.start(f, w, try f.ctypeFromType(payload_ty, .complete));
6497 try f.writeCValueMember(w, local, .{ .identifier = "payload" });
6498 try a.assign(f, w);
6499 try f.writeCValue(w, payload, .Other);
6500 try a.end(f, w);
65016501 }
65026502 {
6503 const a = try Assignment.start(f, bw, try f.ctypeFromType(err_ty, .complete));
6503 const a = try Assignment.start(f, w, try f.ctypeFromType(err_ty, .complete));
65046504 if (repr_is_err)
6505 try f.writeCValue(bw, local, .Other)
6505 try f.writeCValue(w, local, .Other)
65066506 else
6507 try f.writeCValueMember(bw, local, .{ .identifier = "error" });
6508 try a.assign(f, bw);
6509 try f.object.dg.renderValue(bw, try pt.intValue(try pt.errorIntType(), 0), .Other);
6510 try a.end(f, bw);
6507 try f.writeCValueMember(w, local, .{ .identifier = "error" });
6508 try a.assign(f, w);
6509 try f.object.dg.renderValue(w, try pt.intValue(try pt.errorIntType(), 0), .Other);
6510 try a.end(f, w);
65116511 }
65126512 return local;
65136513}
......@@ -6517,7 +6517,7 @@ fn airIsErr(f: *Function, inst: Air.Inst.Index, is_ptr: bool, operator: []const
65176517 const zcu = pt.zcu;
65186518 const un_op = f.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
65196519
6520 const bw = &f.object.code.buffered_writer;
6520 const w = &f.object.code.buffered_writer;
65216521 const operand = try f.resolveInst(un_op);
65226522 try reap(f, inst, &.{un_op});
65236523 const operand_ty = f.typeOf(un_op);
......@@ -6526,25 +6526,25 @@ fn airIsErr(f: *Function, inst: Air.Inst.Index, is_ptr: bool, operator: []const
65266526 const payload_ty = err_union_ty.errorUnionPayload(zcu);
65276527 const error_ty = err_union_ty.errorUnionSet(zcu);
65286528
6529 const a = try Assignment.start(f, bw, .bool);
6530 try f.writeCValue(bw, local, .Other);
6531 try a.assign(f, bw);
6529 const a = try Assignment.start(f, w, .bool);
6530 try f.writeCValue(w, local, .Other);
6531 try a.assign(f, w);
65326532 const err_int_ty = try pt.errorIntType();
65336533 if (!error_ty.errorSetIsEmpty(zcu))
65346534 if (payload_ty.hasRuntimeBits(zcu))
65356535 if (is_ptr)
6536 try f.writeCValueDerefMember(bw, operand, .{ .identifier = "error" })
6536 try f.writeCValueDerefMember(w, operand, .{ .identifier = "error" })
65376537 else
6538 try f.writeCValueMember(bw, operand, .{ .identifier = "error" })
6538 try f.writeCValueMember(w, operand, .{ .identifier = "error" })
65396539 else
6540 try f.writeCValue(bw, operand, .Other)
6540 try f.writeCValue(w, operand, .Other)
65416541 else
6542 try f.object.dg.renderValue(bw, try pt.intValue(err_int_ty, 0), .Other);
6543 try bw.writeByte(' ');
6544 try bw.writeAll(operator);
6545 try bw.writeByte(' ');
6546 try f.object.dg.renderValue(bw, try pt.intValue(err_int_ty, 0), .Other);
6547 try a.end(f, bw);
6542 try f.object.dg.renderValue(w, try pt.intValue(err_int_ty, 0), .Other);
6543 try w.writeByte(' ');
6544 try w.writeAll(operator);
6545 try w.writeByte(' ');
6546 try f.object.dg.renderValue(w, try pt.intValue(err_int_ty, 0), .Other);
6547 try a.end(f, w);
65486548 return local;
65496549}
65506550
......@@ -6558,45 +6558,45 @@ fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue {
65586558 try reap(f, inst, &.{ty_op.operand});
65596559 const inst_ty = f.typeOfIndex(inst);
65606560 const ptr_ty = inst_ty.slicePtrFieldType(zcu);
6561 const bw = &f.object.code.buffered_writer;
6561 const w = &f.object.code.buffered_writer;
65626562 const local = try f.allocLocal(inst, inst_ty);
65636563 const operand_ty = f.typeOf(ty_op.operand);
65646564 const array_ty = operand_ty.childType(zcu);
65656565
65666566 {
6567 const a = try Assignment.start(f, bw, try f.ctypeFromType(ptr_ty, .complete));
6568 try f.writeCValueMember(bw, local, .{ .identifier = "ptr" });
6569 try a.assign(f, bw);
6567 const a = try Assignment.start(f, w, try f.ctypeFromType(ptr_ty, .complete));
6568 try f.writeCValueMember(w, local, .{ .identifier = "ptr" });
6569 try a.assign(f, w);
65706570 if (operand == .undef) {
6571 try f.writeCValue(bw, .{ .undef = inst_ty.slicePtrFieldType(zcu) }, .Other);
6571 try f.writeCValue(w, .{ .undef = inst_ty.slicePtrFieldType(zcu) }, .Other);
65726572 } else {
65736573 const ptr_ctype = try f.ctypeFromType(ptr_ty, .complete);
65746574 const ptr_child_ctype = ptr_ctype.info(ctype_pool).pointer.elem_ctype;
65756575 const elem_ty = array_ty.childType(zcu);
65766576 const elem_ctype = try f.ctypeFromType(elem_ty, .complete);
65776577 if (!ptr_child_ctype.eql(elem_ctype)) {
6578 try bw.writeByte('(');
6579 try f.renderCType(bw, ptr_ctype);
6580 try bw.writeByte(')');
6578 try w.writeByte('(');
6579 try f.renderCType(w, ptr_ctype);
6580 try w.writeByte(')');
65816581 }
65826582 const operand_ctype = try f.ctypeFromType(operand_ty, .complete);
65836583 const operand_child_ctype = operand_ctype.info(ctype_pool).pointer.elem_ctype;
65846584 if (operand_child_ctype.info(ctype_pool) == .array) {
6585 try bw.writeByte('&');
6586 try f.writeCValueDeref(bw, operand);
6587 try bw.print("[{f}]", .{try f.fmtIntLiteral(.zero_usize)});
6588 } else try f.writeCValue(bw, operand, .Other);
6585 try w.writeByte('&');
6586 try f.writeCValueDeref(w, operand);
6587 try w.print("[{f}]", .{try f.fmtIntLiteral(.zero_usize)});
6588 } else try f.writeCValue(w, operand, .Other);
65896589 }
6590 try a.end(f, bw);
6590 try a.end(f, w);
65916591 }
65926592 {
6593 const a = try Assignment.start(f, bw, .usize);
6594 try f.writeCValueMember(bw, local, .{ .identifier = "len" });
6595 try a.assign(f, bw);
6596 try bw.print("{f}", .{
6593 const a = try Assignment.start(f, w, .usize);
6594 try f.writeCValueMember(w, local, .{ .identifier = "len" });
6595 try a.assign(f, w);
6596 try w.print("{f}", .{
65976597 try f.fmtIntLiteral(try pt.intValue(.usize, array_ty.arrayLen(zcu))),
65986598 });
6599 try a.end(f, bw);
6599 try a.end(f, w);
66006600 }
66016601
66026602 return local;
......@@ -6623,32 +6623,32 @@ fn airFloatCast(f: *Function, inst: Air.Inst.Index) !CValue {
66236623 else
66246624 unreachable;
66256625
6626 const bw = &f.object.code.buffered_writer;
6626 const w = &f.object.code.buffered_writer;
66276627 const local = try f.allocLocal(inst, inst_ty);
6628 const v = try Vectorize.start(f, inst, bw, operand_ty);
6629 const a = try Assignment.start(f, bw, try f.ctypeFromType(scalar_ty, .complete));
6630 try f.writeCValue(bw, local, .Other);
6631 try v.elem(f, bw);
6632 try a.assign(f, bw);
6628 const v = try Vectorize.start(f, inst, w, operand_ty);
6629 const a = try Assignment.start(f, w, try f.ctypeFromType(scalar_ty, .complete));
6630 try f.writeCValue(w, local, .Other);
6631 try v.elem(f, w);
6632 try a.assign(f, w);
66336633 if (inst_scalar_ty.isInt(zcu) and scalar_ty.isRuntimeFloat()) {
6634 try bw.writeAll("zig_wrap_");
6635 try f.object.dg.renderTypeForBuiltinFnName(bw, inst_scalar_ty);
6636 try bw.writeByte('(');
6637 }
6638 try bw.writeAll("zig_");
6639 try bw.writeAll(operation);
6640 try bw.writeAll(compilerRtAbbrev(scalar_ty, zcu, target));
6641 try bw.writeAll(compilerRtAbbrev(inst_scalar_ty, zcu, target));
6642 try bw.writeByte('(');
6643 try f.writeCValue(bw, operand, .FunctionArgument);
6644 try v.elem(f, bw);
6645 try bw.writeByte(')');
6634 try w.writeAll("zig_wrap_");
6635 try f.object.dg.renderTypeForBuiltinFnName(w, inst_scalar_ty);
6636 try w.writeByte('(');
6637 }
6638 try w.writeAll("zig_");
6639 try w.writeAll(operation);
6640 try w.writeAll(compilerRtAbbrev(scalar_ty, zcu, target));
6641 try w.writeAll(compilerRtAbbrev(inst_scalar_ty, zcu, target));
6642 try w.writeByte('(');
6643 try f.writeCValue(w, operand, .FunctionArgument);
6644 try v.elem(f, w);
6645 try w.writeByte(')');
66466646 if (inst_scalar_ty.isInt(zcu) and scalar_ty.isRuntimeFloat()) {
6647 try f.object.dg.renderBuiltinInfo(bw, inst_scalar_ty, .bits);
6648 try bw.writeByte(')');
6647 try f.object.dg.renderBuiltinInfo(w, inst_scalar_ty, .bits);
6648 try w.writeByte(')');
66496649 }
6650 try a.end(f, bw);
6651 try v.end(f, inst, bw);
6650 try a.end(f, w);
6651 try v.end(f, inst, w);
66526652
66536653 return local;
66546654}
......@@ -6673,28 +6673,28 @@ fn airUnBuiltinCall(
66736673 const inst_scalar_ctype = try f.ctypeFromType(inst_scalar_ty, .complete);
66746674 const ref_ret = inst_scalar_ctype.info(&f.object.dg.ctype_pool) == .array;
66756675
6676 const bw = &f.object.code.buffered_writer;
6676 const w = &f.object.code.buffered_writer;
66776677 const local = try f.allocLocal(inst, inst_ty);
6678 const v = try Vectorize.start(f, inst, bw, operand_ty);
6678 const v = try Vectorize.start(f, inst, w, operand_ty);
66796679 if (!ref_ret) {
6680 try f.writeCValue(bw, local, .Other);
6681 try v.elem(f, bw);
6682 try bw.writeAll(" = ");
6680 try f.writeCValue(w, local, .Other);
6681 try v.elem(f, w);
6682 try w.writeAll(" = ");
66836683 }
6684 try bw.print("zig_{s}_", .{operation});
6685 try f.object.dg.renderTypeForBuiltinFnName(bw, scalar_ty);
6686 try bw.writeByte('(');
6684 try w.print("zig_{s}_", .{operation});
6685 try f.object.dg.renderTypeForBuiltinFnName(w, scalar_ty);
6686 try w.writeByte('(');
66876687 if (ref_ret) {
6688 try f.writeCValue(bw, local, .FunctionArgument);
6689 try v.elem(f, bw);
6690 try bw.writeAll(", ");
6691 }
6692 try f.writeCValue(bw, operand, .FunctionArgument);
6693 try v.elem(f, bw);
6694 try f.object.dg.renderBuiltinInfo(bw, scalar_ty, info);
6695 try bw.writeAll(");");
6688 try f.writeCValue(w, local, .FunctionArgument);
6689 try v.elem(f, w);
6690 try w.writeAll(", ");
6691 }
6692 try f.writeCValue(w, operand, .FunctionArgument);
6693 try v.elem(f, w);
6694 try f.object.dg.renderBuiltinInfo(w, scalar_ty, info);
6695 try w.writeAll(");");
66966696 try f.object.newline();
6697 try v.end(f, inst, bw);
6697 try v.end(f, inst, w);
66986698
66996699 return local;
67006700}
......@@ -6724,31 +6724,31 @@ fn airBinBuiltinCall(
67246724 const inst_scalar_ctype = try f.ctypeFromType(inst_scalar_ty, .complete);
67256725 const ref_ret = inst_scalar_ctype.info(&f.object.dg.ctype_pool) == .array;
67266726
6727 const bw = &f.object.code.buffered_writer;
6727 const w = &f.object.code.buffered_writer;
67286728 const local = try f.allocLocal(inst, inst_ty);
67296729 if (is_big) try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });
6730 const v = try Vectorize.start(f, inst, bw, operand_ty);
6730 const v = try Vectorize.start(f, inst, w, operand_ty);
67316731 if (!ref_ret) {
6732 try f.writeCValue(bw, local, .Other);
6733 try v.elem(f, bw);
6734 try bw.writeAll(" = ");
6732 try f.writeCValue(w, local, .Other);
6733 try v.elem(f, w);
6734 try w.writeAll(" = ");
67356735 }
6736 try bw.print("zig_{s}_", .{operation});
6737 try f.object.dg.renderTypeForBuiltinFnName(bw, scalar_ty);
6738 try bw.writeByte('(');
6736 try w.print("zig_{s}_", .{operation});
6737 try f.object.dg.renderTypeForBuiltinFnName(w, scalar_ty);
6738 try w.writeByte('(');
67396739 if (ref_ret) {
6740 try f.writeCValue(bw, local, .FunctionArgument);
6741 try v.elem(f, bw);
6742 try bw.writeAll(", ");
6743 }
6744 try f.writeCValue(bw, lhs, .FunctionArgument);
6745 try v.elem(f, bw);
6746 try bw.writeAll(", ");
6747 try f.writeCValue(bw, rhs, .FunctionArgument);
6748 if (f.typeOf(bin_op.rhs).isVector(zcu)) try v.elem(f, bw);
6749 try f.object.dg.renderBuiltinInfo(bw, scalar_ty, info);
6750 try bw.writeAll(");\n");
6751 try v.end(f, inst, bw);
6740 try f.writeCValue(w, local, .FunctionArgument);
6741 try v.elem(f, w);
6742 try w.writeAll(", ");
6743 }
6744 try f.writeCValue(w, lhs, .FunctionArgument);
6745 try v.elem(f, w);
6746 try w.writeAll(", ");
6747 try f.writeCValue(w, rhs, .FunctionArgument);
6748 if (f.typeOf(bin_op.rhs).isVector(zcu)) try v.elem(f, w);
6749 try f.object.dg.renderBuiltinInfo(w, scalar_ty, info);
6750 try w.writeAll(");\n");
6751 try v.end(f, inst, w);
67526752
67536753 return local;
67546754}
......@@ -6775,39 +6775,39 @@ fn airCmpBuiltinCall(
67756775 const inst_scalar_ctype = try f.ctypeFromType(inst_scalar_ty, .complete);
67766776 const ref_ret = inst_scalar_ctype.info(&f.object.dg.ctype_pool) == .array;
67776777
6778 const bw = &f.object.code.buffered_writer;
6778 const w = &f.object.code.buffered_writer;
67796779 const local = try f.allocLocal(inst, inst_ty);
6780 const v = try Vectorize.start(f, inst, bw, operand_ty);
6780 const v = try Vectorize.start(f, inst, w, operand_ty);
67816781 if (!ref_ret) {
6782 try f.writeCValue(bw, local, .Other);
6783 try v.elem(f, bw);
6784 try bw.writeAll(" = ");
6782 try f.writeCValue(w, local, .Other);
6783 try v.elem(f, w);
6784 try w.writeAll(" = ");
67856785 }
6786 try bw.print("zig_{s}_", .{switch (operation) {
6786 try w.print("zig_{s}_", .{switch (operation) {
67876787 else => @tagName(operation),
67886788 .operator => compareOperatorAbbrev(operator),
67896789 }});
6790 try f.object.dg.renderTypeForBuiltinFnName(bw, scalar_ty);
6791 try bw.writeByte('(');
6790 try f.object.dg.renderTypeForBuiltinFnName(w, scalar_ty);
6791 try w.writeByte('(');
67926792 if (ref_ret) {
6793 try f.writeCValue(bw, local, .FunctionArgument);
6794 try v.elem(f, bw);
6795 try bw.writeAll(", ");
6796 }
6797 try f.writeCValue(bw, lhs, .FunctionArgument);
6798 try v.elem(f, bw);
6799 try bw.writeAll(", ");
6800 try f.writeCValue(bw, rhs, .FunctionArgument);
6801 try v.elem(f, bw);
6802 try f.object.dg.renderBuiltinInfo(bw, scalar_ty, info);
6803 try bw.writeByte(')');
6804 if (!ref_ret) try bw.print("{s}{f}", .{
6793 try f.writeCValue(w, local, .FunctionArgument);
6794 try v.elem(f, w);
6795 try w.writeAll(", ");
6796 }
6797 try f.writeCValue(w, lhs, .FunctionArgument);
6798 try v.elem(f, w);
6799 try w.writeAll(", ");
6800 try f.writeCValue(w, rhs, .FunctionArgument);
6801 try v.elem(f, w);
6802 try f.object.dg.renderBuiltinInfo(w, scalar_ty, info);
6803 try w.writeByte(')');
6804 if (!ref_ret) try w.print("{s}{f}", .{
68056805 compareOperatorC(operator),
68066806 try f.fmtIntLiteral(try pt.intValue(.i32, 0)),
68076807 });
6808 try bw.writeByte(';');
6808 try w.writeByte(';');
68096809 try f.object.newline();
6810 try v.end(f, inst, bw);
6810 try v.end(f, inst, w);
68116811
68126812 return local;
68136813}
......@@ -6825,7 +6825,7 @@ fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue
68256825 const ty = ptr_ty.childType(zcu);
68266826 const ctype = try f.ctypeFromType(ty, .complete);
68276827
6828 const bw = &f.object.code.buffered_writer;
6828 const w = &f.object.code.buffered_writer;
68296829 const new_value_mat = try Materialize.start(f, inst, ty, new_value);
68306830 try reap(f, inst, &.{ extra.ptr, extra.expected_value, extra.new_value });
68316831
......@@ -6837,78 +6837,78 @@ fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue
68376837 const local = try f.allocLocal(inst, inst_ty);
68386838 if (inst_ty.isPtrLikeOptional(zcu)) {
68396839 {
6840 const a = try Assignment.start(f, bw, ctype);
6841 try f.writeCValue(bw, local, .Other);
6842 try a.assign(f, bw);
6843 try f.writeCValue(bw, expected_value, .Other);
6844 try a.end(f, bw);
6840 const a = try Assignment.start(f, w, ctype);
6841 try f.writeCValue(w, local, .Other);
6842 try a.assign(f, w);
6843 try f.writeCValue(w, expected_value, .Other);
6844 try a.end(f, w);
68456845 }
68466846
6847 try bw.writeAll("if (");
6848 try bw.print("zig_cmpxchg_{s}((zig_atomic(", .{flavor});
6849 try f.renderType(bw, ty);
6850 try bw.writeByte(')');
6851 if (ptr_ty.isVolatilePtr(zcu)) try bw.writeAll(" volatile");
6852 try bw.writeAll(" *)");
6853 try f.writeCValue(bw, ptr, .Other);
6854 try bw.writeAll(", ");
6855 try f.writeCValue(bw, local, .FunctionArgument);
6856 try bw.writeAll(", ");
6857 try new_value_mat.mat(f, bw);
6858 try bw.writeAll(", ");
6859 try writeMemoryOrder(bw, extra.successOrder());
6860 try bw.writeAll(", ");
6861 try writeMemoryOrder(bw, extra.failureOrder());
6862 try bw.writeAll(", ");
6863 try f.object.dg.renderTypeForBuiltinFnName(bw, ty);
6864 try bw.writeAll(", ");
6865 try f.renderType(bw, repr_ty);
6866 try bw.writeByte(')');
6867 try bw.writeAll(") {");
6847 try w.writeAll("if (");
6848 try w.print("zig_cmpxchg_{s}((zig_atomic(", .{flavor});
6849 try f.renderType(w, ty);
6850 try w.writeByte(')');
6851 if (ptr_ty.isVolatilePtr(zcu)) try w.writeAll(" volatile");
6852 try w.writeAll(" *)");
6853 try f.writeCValue(w, ptr, .Other);
6854 try w.writeAll(", ");
6855 try f.writeCValue(w, local, .FunctionArgument);
6856 try w.writeAll(", ");
6857 try new_value_mat.mat(f, w);
6858 try w.writeAll(", ");
6859 try writeMemoryOrder(w, extra.successOrder());
6860 try w.writeAll(", ");
6861 try writeMemoryOrder(w, extra.failureOrder());
6862 try w.writeAll(", ");
6863 try f.object.dg.renderTypeForBuiltinFnName(w, ty);
6864 try w.writeAll(", ");
6865 try f.renderType(w, repr_ty);
6866 try w.writeByte(')');
6867 try w.writeAll(") {");
68686868 f.object.indent();
68696869 try f.object.newline();
68706870 {
6871 const a = try Assignment.start(f, bw, ctype);
6872 try f.writeCValue(bw, local, .Other);
6873 try a.assign(f, bw);
6874 try bw.writeAll("NULL");
6875 try a.end(f, bw);
6871 const a = try Assignment.start(f, w, ctype);
6872 try f.writeCValue(w, local, .Other);
6873 try a.assign(f, w);
6874 try w.writeAll("NULL");
6875 try a.end(f, w);
68766876 }
68776877 try f.object.outdent();
6878 try bw.writeByte('}');
6878 try w.writeByte('}');
68796879 try f.object.newline();
68806880 } else {
68816881 {
6882 const a = try Assignment.start(f, bw, ctype);
6883 try f.writeCValueMember(bw, local, .{ .identifier = "payload" });
6884 try a.assign(f, bw);
6885 try f.writeCValue(bw, expected_value, .Other);
6886 try a.end(f, bw);
6882 const a = try Assignment.start(f, w, ctype);
6883 try f.writeCValueMember(w, local, .{ .identifier = "payload" });
6884 try a.assign(f, w);
6885 try f.writeCValue(w, expected_value, .Other);
6886 try a.end(f, w);
68876887 }
68886888 {
6889 const a = try Assignment.start(f, bw, .bool);
6890 try f.writeCValueMember(bw, local, .{ .identifier = "is_null" });
6891 try a.assign(f, bw);
6892 try bw.print("zig_cmpxchg_{s}((zig_atomic(", .{flavor});
6893 try f.renderType(bw, ty);
6894 try bw.writeByte(')');
6895 if (ptr_ty.isVolatilePtr(zcu)) try bw.writeAll(" volatile");
6896 try bw.writeAll(" *)");
6897 try f.writeCValue(bw, ptr, .Other);
6898 try bw.writeAll(", ");
6899 try f.writeCValueMember(bw, local, .{ .identifier = "payload" });
6900 try bw.writeAll(", ");
6901 try new_value_mat.mat(f, bw);
6902 try bw.writeAll(", ");
6903 try writeMemoryOrder(bw, extra.successOrder());
6904 try bw.writeAll(", ");
6905 try writeMemoryOrder(bw, extra.failureOrder());
6906 try bw.writeAll(", ");
6907 try f.object.dg.renderTypeForBuiltinFnName(bw, ty);
6908 try bw.writeAll(", ");
6909 try f.renderType(bw, repr_ty);
6910 try bw.writeByte(')');
6911 try a.end(f, bw);
6889 const a = try Assignment.start(f, w, .bool);
6890 try f.writeCValueMember(w, local, .{ .identifier = "is_null" });
6891 try a.assign(f, w);
6892 try w.print("zig_cmpxchg_{s}((zig_atomic(", .{flavor});
6893 try f.renderType(w, ty);
6894 try w.writeByte(')');
6895 if (ptr_ty.isVolatilePtr(zcu)) try w.writeAll(" volatile");
6896 try w.writeAll(" *)");
6897 try f.writeCValue(w, ptr, .Other);
6898 try w.writeAll(", ");
6899 try f.writeCValueMember(w, local, .{ .identifier = "payload" });
6900 try w.writeAll(", ");
6901 try new_value_mat.mat(f, w);
6902 try w.writeAll(", ");
6903 try writeMemoryOrder(w, extra.successOrder());
6904 try w.writeAll(", ");
6905 try writeMemoryOrder(w, extra.failureOrder());
6906 try w.writeAll(", ");
6907 try f.object.dg.renderTypeForBuiltinFnName(w, ty);
6908 try w.writeAll(", ");
6909 try f.renderType(w, repr_ty);
6910 try w.writeByte(')');
6911 try a.end(f, w);
69126912 }
69136913 }
69146914 try new_value_mat.end(f, inst);
......@@ -6932,7 +6932,7 @@ fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue {
69326932 const ptr = try f.resolveInst(pl_op.operand);
69336933 const operand = try f.resolveInst(extra.operand);
69346934
6935 const bw = &f.object.code.buffered_writer;
6935 const w = &f.object.code.buffered_writer;
69366936 const operand_mat = try Materialize.start(f, inst, ty, operand);
69376937 try reap(f, inst, &.{ pl_op.operand, extra.operand });
69386938
......@@ -6942,31 +6942,31 @@ fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue {
69426942 const repr_ty = if (is_float) pt.intType(.unsigned, repr_bits) catch unreachable else ty;
69436943
69446944 const local = try f.allocLocal(inst, inst_ty);
6945 try bw.print("zig_atomicrmw_{s}", .{toAtomicRmwSuffix(extra.op())});
6946 if (is_float) try bw.writeAll("_float") else if (is_128) try bw.writeAll("_int128");
6947 try bw.writeByte('(');
6948 try f.writeCValue(bw, local, .Other);
6949 try bw.writeAll(", (");
6945 try w.print("zig_atomicrmw_{s}", .{toAtomicRmwSuffix(extra.op())});
6946 if (is_float) try w.writeAll("_float") else if (is_128) try w.writeAll("_int128");
6947 try w.writeByte('(');
6948 try f.writeCValue(w, local, .Other);
6949 try w.writeAll(", (");
69506950 const use_atomic = switch (extra.op()) {
69516951 else => true,
69526952 // These are missing from stdatomic.h, so no atomic types unless a fallback is used.
69536953 .Nand, .Min, .Max => is_float or is_128,
69546954 };
6955 if (use_atomic) try bw.writeAll("zig_atomic(");
6956 try f.renderType(bw, ty);
6957 if (use_atomic) try bw.writeByte(')');
6958 if (ptr_ty.isVolatilePtr(zcu)) try bw.writeAll(" volatile");
6959 try bw.writeAll(" *)");
6960 try f.writeCValue(bw, ptr, .Other);
6961 try bw.writeAll(", ");
6962 try operand_mat.mat(f, bw);
6963 try bw.writeAll(", ");
6964 try writeMemoryOrder(bw, extra.ordering());
6965 try bw.writeAll(", ");
6966 try f.object.dg.renderTypeForBuiltinFnName(bw, ty);
6967 try bw.writeAll(", ");
6968 try f.renderType(bw, repr_ty);
6969 try bw.writeAll(");");
6955 if (use_atomic) try w.writeAll("zig_atomic(");
6956 try f.renderType(w, ty);
6957 if (use_atomic) try w.writeByte(')');
6958 if (ptr_ty.isVolatilePtr(zcu)) try w.writeAll(" volatile");
6959 try w.writeAll(" *)");
6960 try f.writeCValue(w, ptr, .Other);
6961 try w.writeAll(", ");
6962 try operand_mat.mat(f, w);
6963 try w.writeAll(", ");
6964 try writeMemoryOrder(w, extra.ordering());
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(");");
69706970 try f.object.newline();
69716971 try operand_mat.end(f, inst);
69726972
......@@ -6993,24 +6993,24 @@ fn airAtomicLoad(f: *Function, inst: Air.Inst.Index) !CValue {
69936993 ty;
69946994
69956995 const inst_ty = f.typeOfIndex(inst);
6996 const bw = &f.object.code.buffered_writer;
6996 const w = &f.object.code.buffered_writer;
69976997 const local = try f.allocLocal(inst, inst_ty);
69986998
6999 try bw.writeAll("zig_atomic_load(");
7000 try f.writeCValue(bw, local, .Other);
7001 try bw.writeAll(", (zig_atomic(");
7002 try f.renderType(bw, ty);
7003 try bw.writeByte(')');
7004 if (ptr_ty.isVolatilePtr(zcu)) try bw.writeAll(" volatile");
7005 try bw.writeAll(" *)");
7006 try f.writeCValue(bw, ptr, .Other);
7007 try bw.writeAll(", ");
7008 try writeMemoryOrder(bw, atomic_load.order);
7009 try bw.writeAll(", ");
7010 try f.object.dg.renderTypeForBuiltinFnName(bw, ty);
7011 try bw.writeAll(", ");
7012 try f.renderType(bw, repr_ty);
7013 try bw.writeAll(");");
6999 try w.writeAll("zig_atomic_load(");
7000 try f.writeCValue(w, local, .Other);
7001 try w.writeAll(", (zig_atomic(");
7002 try f.renderType(w, ty);
7003 try w.writeByte(')');
7004 if (ptr_ty.isVolatilePtr(zcu)) try w.writeAll(" volatile");
7005 try w.writeAll(" *)");
7006 try f.writeCValue(w, ptr, .Other);
7007 try w.writeAll(", ");
7008 try writeMemoryOrder(w, atomic_load.order);
7009 try w.writeAll(", ");
7010 try f.object.dg.renderTypeForBuiltinFnName(w, ty);
7011 try w.writeAll(", ");
7012 try f.renderType(w, repr_ty);
7013 try w.writeAll(");");
70147014 try f.object.newline();
70157015
70167016 return local;
......@@ -7025,7 +7025,7 @@ fn airAtomicStore(f: *Function, inst: Air.Inst.Index, order: [*:0]const u8) !CVa
70257025 const ptr = try f.resolveInst(bin_op.lhs);
70267026 const element = try f.resolveInst(bin_op.rhs);
70277027
7028 const bw = &f.object.code.buffered_writer;
7028 const w = &f.object.code.buffered_writer;
70297029 const element_mat = try Materialize.start(f, inst, ty, element);
70307030 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });
70317031
......@@ -7034,32 +7034,32 @@ fn airAtomicStore(f: *Function, inst: Air.Inst.Index, order: [*:0]const u8) !CVa
70347034 else
70357035 ty;
70367036
7037 try bw.writeAll("zig_atomic_store((zig_atomic(");
7038 try f.renderType(bw, ty);
7039 try bw.writeByte(')');
7040 if (ptr_ty.isVolatilePtr(zcu)) try bw.writeAll(" volatile");
7041 try bw.writeAll(" *)");
7042 try f.writeCValue(bw, ptr, .Other);
7043 try bw.writeAll(", ");
7044 try element_mat.mat(f, bw);
7045 try bw.print(", {s}, ", .{order});
7046 try f.object.dg.renderTypeForBuiltinFnName(bw, ty);
7047 try bw.writeAll(", ");
7048 try f.renderType(bw, repr_ty);
7049 try bw.writeAll(");");
7037 try w.writeAll("zig_atomic_store((zig_atomic(");
7038 try f.renderType(w, ty);
7039 try w.writeByte(')');
7040 if (ptr_ty.isVolatilePtr(zcu)) try w.writeAll(" volatile");
7041 try w.writeAll(" *)");
7042 try f.writeCValue(w, ptr, .Other);
7043 try w.writeAll(", ");
7044 try element_mat.mat(f, w);
7045 try w.print(", {s}, ", .{order});
7046 try f.object.dg.renderTypeForBuiltinFnName(w, ty);
7047 try w.writeAll(", ");
7048 try f.renderType(w, repr_ty);
7049 try w.writeAll(");");
70507050 try f.object.newline();
70517051 try element_mat.end(f, inst);
70527052
70537053 return .none;
70547054}
70557055
7056fn writeSliceOrPtr(f: *Function, bw: *Writer, ptr: CValue, ptr_ty: Type) !void {
7056fn writeSliceOrPtr(f: *Function, w: *Writer, ptr: CValue, ptr_ty: Type) !void {
70577057 const pt = f.object.dg.pt;
70587058 const zcu = pt.zcu;
70597059 if (ptr_ty.isSlice(zcu)) {
7060 try f.writeCValueMember(bw, ptr, .{ .identifier = "ptr" });
7060 try f.writeCValueMember(w, ptr, .{ .identifier = "ptr" });
70617061 } else {
7062 try f.writeCValue(bw, ptr, .FunctionArgument);
7062 try f.writeCValue(w, ptr, .FunctionArgument);
70637063 }
70647064}
70657065
......@@ -7073,7 +7073,7 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
70737073 const elem_ty = f.typeOf(bin_op.rhs);
70747074 const elem_abi_size = elem_ty.abiSize(zcu);
70757075 const val_is_undef = if (try f.air.value(bin_op.rhs, pt)) |val| val.isUndefDeep(zcu) else false;
7076 const bw = &f.object.code.buffered_writer;
7076 const w = &f.object.code.buffered_writer;
70777077
70787078 if (val_is_undef) {
70797079 if (!safety) {
......@@ -7081,24 +7081,24 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
70817081 return .none;
70827082 }
70837083
7084 try bw.writeAll("memset(");
7084 try w.writeAll("memset(");
70857085 switch (dest_ty.ptrSize(zcu)) {
70867086 .slice => {
7087 try f.writeCValueMember(bw, dest_slice, .{ .identifier = "ptr" });
7088 try bw.writeAll(", 0xaa, ");
7089 try f.writeCValueMember(bw, dest_slice, .{ .identifier = "len" });
7087 try f.writeCValueMember(w, dest_slice, .{ .identifier = "ptr" });
7088 try w.writeAll(", 0xaa, ");
7089 try f.writeCValueMember(w, dest_slice, .{ .identifier = "len" });
70907090 if (elem_abi_size > 1) {
7091 try bw.print(" * {d}", .{elem_abi_size});
7091 try w.print(" * {d}", .{elem_abi_size});
70927092 }
7093 try bw.writeAll(");");
7093 try w.writeAll(");");
70947094 try f.object.newline();
70957095 },
70967096 .one => {
70977097 const array_ty = dest_ty.childType(zcu);
70987098 const len = array_ty.arrayLen(zcu) * elem_abi_size;
70997099
7100 try f.writeCValue(bw, dest_slice, .FunctionArgument);
7101 try bw.print(", 0xaa, {d});", .{len});
7100 try f.writeCValue(w, dest_slice, .FunctionArgument);
7101 try w.print(", 0xaa, {d});", .{len});
71027102 try f.object.newline();
71037103 },
71047104 .many, .c => unreachable,
......@@ -7120,38 +7120,38 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
71207120
71217121 const index = try f.allocLocal(inst, .usize);
71227122
7123 try bw.writeAll("for (");
7124 try f.writeCValue(bw, index, .Other);
7125 try bw.writeAll(" = ");
7126 try f.object.dg.renderValue(bw, .zero_usize, .Other);
7127 try bw.writeAll("; ");
7128 try f.writeCValue(bw, index, .Other);
7129 try bw.writeAll(" != ");
7123 try w.writeAll("for (");
7124 try f.writeCValue(w, index, .Other);
7125 try w.writeAll(" = ");
7126 try f.object.dg.renderValue(w, .zero_usize, .Other);
7127 try w.writeAll("; ");
7128 try f.writeCValue(w, index, .Other);
7129 try w.writeAll(" != ");
71307130 switch (dest_ty.ptrSize(zcu)) {
71317131 .slice => {
7132 try f.writeCValueMember(bw, dest_slice, .{ .identifier = "len" });
7132 try f.writeCValueMember(w, dest_slice, .{ .identifier = "len" });
71337133 },
71347134 .one => {
71357135 const array_ty = dest_ty.childType(zcu);
7136 try bw.print("{d}", .{array_ty.arrayLen(zcu)});
7136 try w.print("{d}", .{array_ty.arrayLen(zcu)});
71377137 },
71387138 .many, .c => unreachable,
71397139 }
7140 try bw.writeAll("; ++");
7141 try f.writeCValue(bw, index, .Other);
7142 try bw.writeAll(") ");
7143
7144 const a = try Assignment.start(f, bw, try f.ctypeFromType(elem_ty, .complete));
7145 try bw.writeAll("((");
7146 try f.renderType(bw, elem_ptr_ty);
7147 try bw.writeByte(')');
7148 try writeSliceOrPtr(f, bw, dest_slice, dest_ty);
7149 try bw.writeAll(")[");
7150 try f.writeCValue(bw, index, .Other);
7151 try bw.writeByte(']');
7152 try a.assign(f, bw);
7153 try f.writeCValue(bw, value, .Other);
7154 try a.end(f, bw);
7140 try w.writeAll("; ++");
7141 try f.writeCValue(w, index, .Other);
7142 try w.writeAll(") ");
7143
7144 const a = try Assignment.start(f, w, try f.ctypeFromType(elem_ty, .complete));
7145 try w.writeAll("((");
7146 try f.renderType(w, elem_ptr_ty);
7147 try w.writeByte(')');
7148 try writeSliceOrPtr(f, w, dest_slice, dest_ty);
7149 try w.writeAll(")[");
7150 try f.writeCValue(w, index, .Other);
7151 try w.writeByte(']');
7152 try a.assign(f, w);
7153 try f.writeCValue(w, value, .Other);
7154 try a.end(f, w);
71557155
71567156 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });
71577157 try freeLocal(f, inst, index.new_local, null);
......@@ -7161,25 +7161,25 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
71617161
71627162 const bitcasted = try bitcast(f, .u8, value, elem_ty);
71637163
7164 try bw.writeAll("memset(");
7164 try w.writeAll("memset(");
71657165 switch (dest_ty.ptrSize(zcu)) {
71667166 .slice => {
7167 try f.writeCValueMember(bw, dest_slice, .{ .identifier = "ptr" });
7168 try bw.writeAll(", ");
7169 try f.writeCValue(bw, bitcasted, .FunctionArgument);
7170 try bw.writeAll(", ");
7171 try f.writeCValueMember(bw, dest_slice, .{ .identifier = "len" });
7172 try bw.writeAll(");");
7167 try f.writeCValueMember(w, dest_slice, .{ .identifier = "ptr" });
7168 try w.writeAll(", ");
7169 try f.writeCValue(w, bitcasted, .FunctionArgument);
7170 try w.writeAll(", ");
7171 try f.writeCValueMember(w, dest_slice, .{ .identifier = "len" });
7172 try w.writeAll(");");
71737173 try f.object.newline();
71747174 },
71757175 .one => {
71767176 const array_ty = dest_ty.childType(zcu);
71777177 const len = array_ty.arrayLen(zcu) * elem_abi_size;
71787178
7179 try f.writeCValue(bw, dest_slice, .FunctionArgument);
7180 try bw.writeAll(", ");
7181 try f.writeCValue(bw, bitcasted, .FunctionArgument);
7182 try bw.print(", {d});", .{len});
7179 try f.writeCValue(w, dest_slice, .FunctionArgument);
7180 try w.writeAll(", ");
7181 try f.writeCValue(w, bitcasted, .FunctionArgument);
7182 try w.print(", {d});", .{len});
71837183 try f.object.newline();
71847184 },
71857185 .many, .c => unreachable,
......@@ -7197,22 +7197,22 @@ fn airMemcpy(f: *Function, inst: Air.Inst.Index, function_paren: []const u8) !CV
71977197 const src_ptr = try f.resolveInst(bin_op.rhs);
71987198 const dest_ty = f.typeOf(bin_op.lhs);
71997199 const src_ty = f.typeOf(bin_op.rhs);
7200 const bw = &f.object.code.buffered_writer;
7200 const w = &f.object.code.buffered_writer;
72017201
72027202 if (dest_ty.ptrSize(zcu) != .one) {
7203 try bw.writeAll("if (");
7203 try w.writeAll("if (");
72047204 try writeArrayLen(f, dest_ptr, dest_ty);
7205 try bw.writeAll(" != 0) ");
7205 try w.writeAll(" != 0) ");
72067206 }
7207 try bw.writeAll(function_paren);
7208 try writeSliceOrPtr(f, bw, dest_ptr, dest_ty);
7209 try bw.writeAll(", ");
7210 try writeSliceOrPtr(f, bw, src_ptr, src_ty);
7211 try bw.writeAll(", ");
7207 try w.writeAll(function_paren);
7208 try writeSliceOrPtr(f, w, dest_ptr, dest_ty);
7209 try w.writeAll(", ");
7210 try writeSliceOrPtr(f, w, src_ptr, src_ty);
7211 try w.writeAll(", ");
72127212 try writeArrayLen(f, dest_ptr, dest_ty);
7213 try bw.writeAll(" * sizeof(");
7214 try f.renderType(bw, dest_ty.elemType2(zcu));
7215 try bw.writeAll("));");
7213 try w.writeAll(" * sizeof(");
7214 try f.renderType(w, dest_ty.elemType2(zcu));
7215 try w.writeAll("));");
72167216 try f.object.newline();
72177217
72187218 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });
......@@ -7222,13 +7222,13 @@ fn airMemcpy(f: *Function, inst: Air.Inst.Index, function_paren: []const u8) !CV
72227222fn writeArrayLen(f: *Function, dest_ptr: CValue, dest_ty: Type) !void {
72237223 const pt = f.object.dg.pt;
72247224 const zcu = pt.zcu;
7225 const bw = &f.object.code.buffered_writer;
7225 const w = &f.object.code.buffered_writer;
72267226 switch (dest_ty.ptrSize(zcu)) {
7227 .one => try bw.print("{f}", .{
7227 .one => try w.print("{f}", .{
72287228 try f.fmtIntLiteral(try pt.intValue(.usize, dest_ty.childType(zcu).arrayLen(zcu))),
72297229 }),
72307230 .many, .c => unreachable,
7231 .slice => try f.writeCValueMember(bw, dest_ptr, .{ .identifier = "len" }),
7231 .slice => try f.writeCValueMember(w, dest_ptr, .{ .identifier = "len" }),
72327232 }
72337233}
72347234
......@@ -7245,12 +7245,12 @@ fn airSetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue {
72457245 if (layout.tag_size == 0) return .none;
72467246 const tag_ty = union_ty.unionTagTypeSafety(zcu).?;
72477247
7248 const bw = &f.object.code.buffered_writer;
7249 const a = try Assignment.start(f, bw, try f.ctypeFromType(tag_ty, .complete));
7250 try f.writeCValueDerefMember(bw, union_ptr, .{ .identifier = "tag" });
7251 try a.assign(f, bw);
7252 try f.writeCValue(bw, new_tag, .Other);
7253 try a.end(f, bw);
7248 const w = &f.object.code.buffered_writer;
7249 const a = try Assignment.start(f, w, try f.ctypeFromType(tag_ty, .complete));
7250 try f.writeCValueDerefMember(w, union_ptr, .{ .identifier = "tag" });
7251 try a.assign(f, w);
7252 try f.writeCValue(w, new_tag, .Other);
7253 try a.end(f, w);
72547254 return .none;
72557255}
72567256
......@@ -7267,13 +7267,13 @@ fn airGetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue {
72677267 if (layout.tag_size == 0) return .none;
72687268
72697269 const inst_ty = f.typeOfIndex(inst);
7270 const bw = &f.object.code.buffered_writer;
7270 const w = &f.object.code.buffered_writer;
72717271 const local = try f.allocLocal(inst, inst_ty);
7272 const a = try Assignment.start(f, bw, try f.ctypeFromType(inst_ty, .complete));
7273 try f.writeCValue(bw, local, .Other);
7274 try a.assign(f, bw);
7275 try f.writeCValueMember(bw, operand, .{ .identifier = "tag" });
7276 try a.end(f, bw);
7272 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete));
7273 try f.writeCValue(w, local, .Other);
7274 try a.assign(f, w);
7275 try f.writeCValueMember(w, operand, .{ .identifier = "tag" });
7276 try a.end(f, w);
72777277 return local;
72787278}
72797279
......@@ -7285,14 +7285,14 @@ fn airTagName(f: *Function, inst: Air.Inst.Index) !CValue {
72857285 const operand = try f.resolveInst(un_op);
72867286 try reap(f, inst, &.{un_op});
72877287
7288 const bw = &f.object.code.buffered_writer;
7288 const w = &f.object.code.buffered_writer;
72897289 const local = try f.allocLocal(inst, inst_ty);
7290 try f.writeCValue(bw, local, .Other);
7291 try bw.print(" = {s}(", .{
7290 try f.writeCValue(w, local, .Other);
7291 try w.print(" = {s}(", .{
72927292 try f.getLazyFnName(.{ .tag_name = enum_ty.toIntern() }),
72937293 });
7294 try f.writeCValue(bw, operand, .Other);
7295 try bw.writeAll(");");
7294 try f.writeCValue(w, operand, .Other);
7295 try w.writeAll(");");
72967296 try f.object.newline();
72977297
72987298 return local;
......@@ -7301,16 +7301,16 @@ fn airTagName(f: *Function, inst: Air.Inst.Index) !CValue {
73017301fn airErrorName(f: *Function, inst: Air.Inst.Index) !CValue {
73027302 const un_op = f.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
73037303
7304 const bw = &f.object.code.buffered_writer;
7304 const w = &f.object.code.buffered_writer;
73057305 const inst_ty = f.typeOfIndex(inst);
73067306 const operand = try f.resolveInst(un_op);
73077307 try reap(f, inst, &.{un_op});
73087308 const local = try f.allocLocal(inst, inst_ty);
7309 try f.writeCValue(bw, local, .Other);
7309 try f.writeCValue(w, local, .Other);
73107310
7311 try bw.writeAll(" = zig_errorName[");
7312 try f.writeCValue(bw, operand, .Other);
7313 try bw.writeAll(" - 1];");
7311 try w.writeAll(" = zig_errorName[");
7312 try f.writeCValue(w, operand, .Other);
7313 try w.writeAll(" - 1];");
73147314 try f.object.newline();
73157315 return local;
73167316}
......@@ -7326,16 +7326,16 @@ fn airSplat(f: *Function, inst: Air.Inst.Index) !CValue {
73267326 const inst_ty = f.typeOfIndex(inst);
73277327 const inst_scalar_ty = inst_ty.scalarType(zcu);
73287328
7329 const bw = &f.object.code.buffered_writer;
7329 const w = &f.object.code.buffered_writer;
73307330 const local = try f.allocLocal(inst, inst_ty);
7331 const v = try Vectorize.start(f, inst, bw, inst_ty);
7332 const a = try Assignment.start(f, bw, try f.ctypeFromType(inst_scalar_ty, .complete));
7333 try f.writeCValue(bw, local, .Other);
7334 try v.elem(f, bw);
7335 try a.assign(f, bw);
7336 try f.writeCValue(bw, operand, .Other);
7337 try a.end(f, bw);
7338 try v.end(f, inst, bw);
7331 const v = try Vectorize.start(f, inst, w, inst_ty);
7332 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_scalar_ty, .complete));
7333 try f.writeCValue(w, local, .Other);
7334 try v.elem(f, w);
7335 try a.assign(f, w);
7336 try f.writeCValue(w, operand, .Other);
7337 try a.end(f, w);
7338 try v.end(f, inst, w);
73397339
73407340 return local;
73417341}
......@@ -7351,23 +7351,23 @@ fn airSelect(f: *Function, inst: Air.Inst.Index) !CValue {
73517351
73527352 const inst_ty = f.typeOfIndex(inst);
73537353
7354 const bw = &f.object.code.buffered_writer;
7354 const w = &f.object.code.buffered_writer;
73557355 const local = try f.allocLocal(inst, inst_ty);
7356 const v = try Vectorize.start(f, inst, bw, inst_ty);
7357 try f.writeCValue(bw, local, .Other);
7358 try v.elem(f, bw);
7359 try bw.writeAll(" = ");
7360 try f.writeCValue(bw, pred, .Other);
7361 try v.elem(f, bw);
7362 try bw.writeAll(" ? ");
7363 try f.writeCValue(bw, lhs, .Other);
7364 try v.elem(f, bw);
7365 try bw.writeAll(" : ");
7366 try f.writeCValue(bw, rhs, .Other);
7367 try v.elem(f, bw);
7368 try bw.writeByte(';');
7356 const v = try Vectorize.start(f, inst, w, inst_ty);
7357 try f.writeCValue(w, local, .Other);
7358 try v.elem(f, w);
7359 try w.writeAll(" = ");
7360 try f.writeCValue(w, pred, .Other);
7361 try v.elem(f, w);
7362 try w.writeAll(" ? ");
7363 try f.writeCValue(w, lhs, .Other);
7364 try v.elem(f, w);
7365 try w.writeAll(" : ");
7366 try f.writeCValue(w, rhs, .Other);
7367 try v.elem(f, w);
7368 try w.writeByte(';');
73697369 try f.object.newline();
7370 try v.end(f, inst, bw);
7370 try v.end(f, inst, w);
73717371
73727372 return local;
73737373}
......@@ -7381,24 +7381,24 @@ fn airShuffleOne(f: *Function, inst: Air.Inst.Index) !CValue {
73817381 const operand = try f.resolveInst(unwrapped.operand);
73827382 const inst_ty = unwrapped.result_ty;
73837383
7384 const bw = &f.object.code.buffered_writer;
7384 const w = &f.object.code.buffered_writer;
73857385 const local = try f.allocLocal(inst, inst_ty);
73867386 try reap(f, inst, &.{unwrapped.operand}); // local cannot alias operand
73877387 for (mask, 0..) |mask_elem, out_idx| {
7388 try f.writeCValue(bw, local, .Other);
7389 try bw.writeByte('[');
7390 try f.object.dg.renderValue(bw, try pt.intValue(.usize, out_idx), .Other);
7391 try bw.writeAll("] = ");
7388 try f.writeCValue(w, local, .Other);
7389 try w.writeByte('[');
7390 try f.object.dg.renderValue(w, try pt.intValue(.usize, out_idx), .Other);
7391 try w.writeAll("] = ");
73927392 switch (mask_elem.unwrap()) {
73937393 .elem => |src_idx| {
7394 try f.writeCValue(bw, operand, .Other);
7395 try bw.writeByte('[');
7396 try f.object.dg.renderValue(bw, try pt.intValue(.usize, src_idx), .Other);
7397 try bw.writeByte(']');
7394 try f.writeCValue(w, operand, .Other);
7395 try w.writeByte('[');
7396 try f.object.dg.renderValue(w, try pt.intValue(.usize, src_idx), .Other);
7397 try w.writeByte(']');
73987398 },
7399 .value => |val| try f.object.dg.renderValue(bw, .fromInterned(val), .Other),
7399 .value => |val| try f.object.dg.renderValue(w, .fromInterned(val), .Other),
74007400 }
7401 try bw.writeAll(";\n");
7401 try w.writeAll(";\n");
74027402 }
74037403
74047404 return local;
......@@ -7453,7 +7453,7 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue {
74537453 const operand = try f.resolveInst(reduce.operand);
74547454 try reap(f, inst, &.{reduce.operand});
74557455 const operand_ty = f.typeOf(reduce.operand);
7456 const bw = &f.object.code.buffered_writer;
7456 const w = &f.object.code.buffered_writer;
74577457
74587458 const use_operator = scalar_ty.bitSize(zcu) <= 64;
74597459 const op: union(enum) {
......@@ -7500,10 +7500,10 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue {
75007500 // }
75017501
75027502 const accum = try f.allocLocal(inst, scalar_ty);
7503 try f.writeCValue(bw, accum, .Other);
7504 try bw.writeAll(" = ");
7503 try f.writeCValue(w, accum, .Other);
7504 try w.writeAll(" = ");
75057505
7506 try f.object.dg.renderValue(bw, switch (reduce.operation) {
7506 try f.object.dg.renderValue(w, switch (reduce.operation) {
75077507 .Or, .Xor => switch (scalar_ty.zigTypeTag(zcu)) {
75087508 .bool => Value.false,
75097509 .int => try pt.intValue(scalar_ty, 0),
......@@ -7540,44 +7540,44 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue {
75407540 else => unreachable,
75417541 },
75427542 }, .Other);
7543 try bw.writeByte(';');
7543 try w.writeByte(';');
75447544 try f.object.newline();
75457545
7546 const v = try Vectorize.start(f, inst, bw, operand_ty);
7547 try f.writeCValue(bw, accum, .Other);
7546 const v = try Vectorize.start(f, inst, w, operand_ty);
7547 try f.writeCValue(w, accum, .Other);
75487548 switch (op) {
75497549 .builtin => |func| {
7550 try bw.print(" = zig_{s}_", .{func.operation});
7551 try f.object.dg.renderTypeForBuiltinFnName(bw, scalar_ty);
7552 try bw.writeByte('(');
7553 try f.writeCValue(bw, accum, .FunctionArgument);
7554 try bw.writeAll(", ");
7555 try f.writeCValue(bw, operand, .Other);
7556 try v.elem(f, bw);
7557 try f.object.dg.renderBuiltinInfo(bw, scalar_ty, func.info);
7558 try bw.writeByte(')');
7550 try w.print(" = zig_{s}_", .{func.operation});
7551 try f.object.dg.renderTypeForBuiltinFnName(w, scalar_ty);
7552 try w.writeByte('(');
7553 try f.writeCValue(w, accum, .FunctionArgument);
7554 try w.writeAll(", ");
7555 try f.writeCValue(w, operand, .Other);
7556 try v.elem(f, w);
7557 try f.object.dg.renderBuiltinInfo(w, scalar_ty, func.info);
7558 try w.writeByte(')');
75597559 },
75607560 .infix => |ass| {
7561 try bw.writeAll(ass);
7562 try f.writeCValue(bw, operand, .Other);
7563 try v.elem(f, bw);
7561 try w.writeAll(ass);
7562 try f.writeCValue(w, operand, .Other);
7563 try v.elem(f, w);
75647564 },
75657565 .ternary => |cmp| {
7566 try bw.writeAll(" = ");
7567 try f.writeCValue(bw, accum, .Other);
7568 try bw.writeAll(cmp);
7569 try f.writeCValue(bw, operand, .Other);
7570 try v.elem(f, bw);
7571 try bw.writeAll(" ? ");
7572 try f.writeCValue(bw, accum, .Other);
7573 try bw.writeAll(" : ");
7574 try f.writeCValue(bw, operand, .Other);
7575 try v.elem(f, bw);
7566 try w.writeAll(" = ");
7567 try f.writeCValue(w, accum, .Other);
7568 try w.writeAll(cmp);
7569 try f.writeCValue(w, operand, .Other);
7570 try v.elem(f, w);
7571 try w.writeAll(" ? ");
7572 try f.writeCValue(w, accum, .Other);
7573 try w.writeAll(" : ");
7574 try f.writeCValue(w, operand, .Other);
7575 try v.elem(f, w);
75767576 },
75777577 }
7578 try bw.writeByte(';');
7578 try w.writeByte(';');
75797579 try f.object.newline();
7580 try v.end(f, inst, bw);
7580 try v.end(f, inst, w);
75817581
75827582 return accum;
75837583}
......@@ -7603,7 +7603,7 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue {
76037603 }
76047604 }
76057605
7606 const bw = &f.object.code.buffered_writer;
7606 const w = &f.object.code.buffered_writer;
76077607 const local = try f.allocLocal(inst, inst_ty);
76087608 switch (ip.indexToKey(inst_ty.toIntern())) {
76097609 inline .array_type, .vector_type => |info, tag| {
......@@ -7611,20 +7611,20 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue {
76117611 .ctype = try f.ctypeFromType(.fromInterned(info.child), .complete),
76127612 };
76137613 for (resolved_elements, 0..) |element, i| {
7614 try a.restart(f, bw);
7615 try f.writeCValue(bw, local, .Other);
7616 try bw.print("[{d}]", .{i});
7617 try a.assign(f, bw);
7618 try f.writeCValue(bw, element, .Other);
7619 try a.end(f, bw);
7614 try a.restart(f, w);
7615 try f.writeCValue(w, local, .Other);
7616 try w.print("[{d}]", .{i});
7617 try a.assign(f, w);
7618 try f.writeCValue(w, element, .Other);
7619 try a.end(f, w);
76207620 }
76217621 if (tag == .array_type and info.sentinel != .none) {
7622 try a.restart(f, bw);
7623 try f.writeCValue(bw, local, .Other);
7624 try bw.print("[{d}]", .{info.len});
7625 try a.assign(f, bw);
7626 try f.object.dg.renderValue(bw, Value.fromInterned(info.sentinel), .Other);
7627 try a.end(f, bw);
7622 try a.restart(f, w);
7623 try f.writeCValue(w, local, .Other);
7624 try w.print("[{d}]", .{info.len});
7625 try a.assign(f, w);
7626 try f.object.dg.renderValue(w, Value.fromInterned(info.sentinel), .Other);
7627 try a.end(f, w);
76287628 }
76297629 },
76307630 .struct_type => {
......@@ -7636,19 +7636,19 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue {
76367636 const field_ty: Type = .fromInterned(loaded_struct.field_types.get(ip)[field_index]);
76377637 if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue;
76387638
7639 const a = try Assignment.start(f, bw, try f.ctypeFromType(field_ty, .complete));
7640 try f.writeCValueMember(bw, local, if (loaded_struct.fieldName(ip, field_index).unwrap()) |field_name|
7639 const a = try Assignment.start(f, w, try f.ctypeFromType(field_ty, .complete));
7640 try f.writeCValueMember(w, local, if (loaded_struct.fieldName(ip, field_index).unwrap()) |field_name|
76417641 .{ .identifier = field_name.toSlice(ip) }
76427642 else
76437643 .{ .field = field_index });
7644 try a.assign(f, bw);
7645 try f.writeCValue(bw, resolved_elements[field_index], .Other);
7646 try a.end(f, bw);
7644 try a.assign(f, w);
7645 try f.writeCValue(w, resolved_elements[field_index], .Other);
7646 try a.end(f, w);
76477647 }
76487648 },
76497649 .@"packed" => {
7650 try f.writeCValue(bw, local, .Other);
7651 try bw.writeAll(" = ");
7650 try f.writeCValue(w, local, .Other);
7651 try w.writeAll(" = ");
76527652
76537653 const backing_int_ty: Type = .fromInterned(loaded_struct.backingIntTypeUnordered(ip));
76547654 const int_info = backing_int_ty.intInfo(zcu);
......@@ -7664,9 +7664,9 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue {
76647664 if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue;
76657665
76667666 if (!empty) {
7667 try bw.writeAll("zig_or_");
7668 try f.object.dg.renderTypeForBuiltinFnName(bw, inst_ty);
7669 try bw.writeByte('(');
7667 try w.writeAll("zig_or_");
7668 try f.object.dg.renderTypeForBuiltinFnName(w, inst_ty);
7669 try w.writeByte('(');
76707670 }
76717671 empty = false;
76727672 }
......@@ -7676,57 +7676,57 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue {
76767676 const field_ty = inst_ty.fieldType(field_index, zcu);
76777677 if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue;
76787678
7679 if (!empty) try bw.writeAll(", ");
7679 if (!empty) try w.writeAll(", ");
76807680 // TODO: Skip this entire shift if val is 0?
7681 try bw.writeAll("zig_shlw_");
7682 try f.object.dg.renderTypeForBuiltinFnName(bw, inst_ty);
7683 try bw.writeByte('(');
7681 try w.writeAll("zig_shlw_");
7682 try f.object.dg.renderTypeForBuiltinFnName(w, inst_ty);
7683 try w.writeByte('(');
76847684
76857685 if (field_ty.isAbiInt(zcu)) {
7686 try bw.writeAll("zig_and_");
7687 try f.object.dg.renderTypeForBuiltinFnName(bw, inst_ty);
7688 try bw.writeByte('(');
7686 try w.writeAll("zig_and_");
7687 try f.object.dg.renderTypeForBuiltinFnName(w, inst_ty);
7688 try w.writeByte('(');
76897689 }
76907690
76917691 if (inst_ty.isAbiInt(zcu) and (field_ty.isAbiInt(zcu) or field_ty.isPtrAtRuntime(zcu))) {
7692 try f.renderIntCast(bw, inst_ty, element, .{}, field_ty, .FunctionArgument);
7692 try f.renderIntCast(w, inst_ty, element, .{}, field_ty, .FunctionArgument);
76937693 } else {
7694 try bw.writeByte('(');
7695 try f.renderType(bw, inst_ty);
7696 try bw.writeByte(')');
7694 try w.writeByte('(');
7695 try f.renderType(w, inst_ty);
7696 try w.writeByte(')');
76977697 if (field_ty.isPtrAtRuntime(zcu)) {
7698 try bw.writeByte('(');
7699 try f.renderType(bw, switch (int_info.signedness) {
7698 try w.writeByte('(');
7699 try f.renderType(w, switch (int_info.signedness) {
77007700 .unsigned => .usize,
77017701 .signed => .isize,
77027702 });
7703 try bw.writeByte(')');
7703 try w.writeByte(')');
77047704 }
7705 try f.writeCValue(bw, element, .Other);
7705 try f.writeCValue(w, element, .Other);
77067706 }
77077707
77087708 if (field_ty.isAbiInt(zcu)) {
7709 try bw.writeAll(", ");
7709 try w.writeAll(", ");
77107710 const field_int_info = field_ty.intInfo(zcu);
77117711 const field_mask = if (int_info.signedness == .signed and int_info.bits == field_int_info.bits)
77127712 try pt.intValue(backing_int_ty, -1)
77137713 else
77147714 try (try pt.intType(.unsigned, field_int_info.bits)).maxIntScalar(pt, backing_int_ty);
7715 try f.object.dg.renderValue(bw, field_mask, .FunctionArgument);
7716 try bw.writeByte(')');
7715 try f.object.dg.renderValue(w, field_mask, .FunctionArgument);
7716 try w.writeByte(')');
77177717 }
77187718
7719 try bw.print(", {f}", .{
7719 try w.print(", {f}", .{
77207720 try f.fmtIntLiteral(try pt.intValue(bit_offset_ty, bit_offset)),
77217721 });
7722 try f.object.dg.renderBuiltinInfo(bw, inst_ty, .bits);
7723 try bw.writeByte(')');
7724 if (!empty) try bw.writeByte(')');
7722 try f.object.dg.renderBuiltinInfo(w, inst_ty, .bits);
7723 try w.writeByte(')');
7724 if (!empty) try w.writeByte(')');
77257725
77267726 bit_offset += field_ty.bitSize(zcu);
77277727 empty = false;
77287728 }
7729 try bw.writeByte(';');
7729 try w.writeByte(';');
77307730 try f.object.newline();
77317731 },
77327732 }
......@@ -7736,11 +7736,11 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue {
77367736 const field_ty: Type = .fromInterned(tuple_info.types.get(ip)[field_index]);
77377737 if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue;
77387738
7739 const a = try Assignment.start(f, bw, try f.ctypeFromType(field_ty, .complete));
7740 try f.writeCValueMember(bw, local, .{ .field = field_index });
7741 try a.assign(f, bw);
7742 try f.writeCValue(bw, resolved_elements[field_index], .Other);
7743 try a.end(f, bw);
7739 const a = try Assignment.start(f, w, try f.ctypeFromType(field_ty, .complete));
7740 try f.writeCValueMember(w, local, .{ .field = field_index });
7741 try a.assign(f, w);
7742 try f.writeCValue(w, resolved_elements[field_index], .Other);
7743 try a.end(f, w);
77447744 },
77457745 else => unreachable,
77467746 }
......@@ -7762,7 +7762,7 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue {
77627762 const payload = try f.resolveInst(extra.init);
77637763 try reap(f, inst, &.{extra.init});
77647764
7765 const bw = &f.object.code.buffered_writer;
7765 const w = &f.object.code.buffered_writer;
77667766 const local = try f.allocLocal(inst, union_ty);
77677767 if (loaded_union.flagsUnordered(ip).layout == .@"packed") return f.moveCValue(inst, union_ty, payload);
77687768
......@@ -7772,20 +7772,20 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue {
77727772 const field_index = tag_ty.enumFieldIndex(field_name, zcu).?;
77737773 const tag_val = try pt.enumValueFieldIndex(tag_ty, field_index);
77747774
7775 const a = try Assignment.start(f, bw, try f.ctypeFromType(tag_ty, .complete));
7776 try f.writeCValueMember(bw, local, .{ .identifier = "tag" });
7777 try a.assign(f, bw);
7778 try bw.print("{f}", .{try f.fmtIntLiteral(try tag_val.intFromEnum(tag_ty, pt))});
7779 try a.end(f, bw);
7775 const a = try Assignment.start(f, w, try f.ctypeFromType(tag_ty, .complete));
7776 try f.writeCValueMember(w, local, .{ .identifier = "tag" });
7777 try a.assign(f, w);
7778 try w.print("{f}", .{try f.fmtIntLiteral(try tag_val.intFromEnum(tag_ty, pt))});
7779 try a.end(f, w);
77807780 }
77817781 break :field .{ .payload_identifier = field_name.toSlice(ip) };
77827782 } else .{ .identifier = field_name.toSlice(ip) };
77837783
7784 const a = try Assignment.start(f, bw, try f.ctypeFromType(payload_ty, .complete));
7785 try f.writeCValueMember(bw, local, field);
7786 try a.assign(f, bw);
7787 try f.writeCValue(bw, payload, .Other);
7788 try a.end(f, bw);
7784 const a = try Assignment.start(f, w, try f.ctypeFromType(payload_ty, .complete));
7785 try f.writeCValueMember(w, local, field);
7786 try a.assign(f, w);
7787 try f.writeCValue(w, payload, .Other);
7788 try a.end(f, w);
77897789 return local;
77907790}
77917791
......@@ -7798,15 +7798,15 @@ fn airPrefetch(f: *Function, inst: Air.Inst.Index) !CValue {
77987798 const ptr = try f.resolveInst(prefetch.ptr);
77997799 try reap(f, inst, &.{prefetch.ptr});
78007800
7801 const bw = &f.object.code.buffered_writer;
7801 const w = &f.object.code.buffered_writer;
78027802 switch (prefetch.cache) {
78037803 .data => {
7804 try bw.writeAll("zig_prefetch(");
7804 try w.writeAll("zig_prefetch(");
78057805 if (ptr_ty.isSlice(zcu))
7806 try f.writeCValueMember(bw, ptr, .{ .identifier = "ptr" })
7806 try f.writeCValueMember(w, ptr, .{ .identifier = "ptr" })
78077807 else
7808 try f.writeCValue(bw, ptr, .FunctionArgument);
7809 try bw.print(", {d}, {d});", .{ @intFromEnum(prefetch.rw), prefetch.locality });
7808 try f.writeCValue(w, ptr, .FunctionArgument);
7809 try w.print(", {d}, {d});", .{ @intFromEnum(prefetch.rw), prefetch.locality });
78107810 try f.object.newline();
78117811 },
78127812 // The available prefetch intrinsics do not accept a cache argument; only
......@@ -7820,13 +7820,13 @@ fn airPrefetch(f: *Function, inst: Air.Inst.Index) !CValue {
78207820fn airWasmMemorySize(f: *Function, inst: Air.Inst.Index) !CValue {
78217821 const pl_op = f.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
78227822
7823 const bw = &f.object.code.buffered_writer;
7823 const w = &f.object.code.buffered_writer;
78247824 const inst_ty = f.typeOfIndex(inst);
78257825 const local = try f.allocLocal(inst, inst_ty);
7826 try f.writeCValue(bw, local, .Other);
7826 try f.writeCValue(w, local, .Other);
78277827
7828 try bw.writeAll(" = ");
7829 try bw.print("zig_wasm_memory_size({d});", .{pl_op.payload});
7828 try w.writeAll(" = ");
7829 try w.print("zig_wasm_memory_size({d});", .{pl_op.payload});
78307830 try f.object.newline();
78317831
78327832 return local;
......@@ -7835,17 +7835,17 @@ fn airWasmMemorySize(f: *Function, inst: Air.Inst.Index) !CValue {
78357835fn airWasmMemoryGrow(f: *Function, inst: Air.Inst.Index) !CValue {
78367836 const pl_op = f.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
78377837
7838 const bw = &f.object.code.buffered_writer;
7838 const w = &f.object.code.buffered_writer;
78397839 const inst_ty = f.typeOfIndex(inst);
78407840 const operand = try f.resolveInst(pl_op.operand);
78417841 try reap(f, inst, &.{pl_op.operand});
78427842 const local = try f.allocLocal(inst, inst_ty);
7843 try f.writeCValue(bw, local, .Other);
7843 try f.writeCValue(w, local, .Other);
78447844
7845 try bw.writeAll(" = ");
7846 try bw.print("zig_wasm_memory_grow({d}, ", .{pl_op.payload});
7847 try f.writeCValue(bw, operand, .FunctionArgument);
7848 try bw.writeAll(");");
7845 try w.writeAll(" = ");
7846 try w.print("zig_wasm_memory_grow({d}, ", .{pl_op.payload});
7847 try f.writeCValue(w, operand, .FunctionArgument);
7848 try w.writeAll(");");
78497849 try f.object.newline();
78507850 return local;
78517851}
......@@ -7864,25 +7864,25 @@ fn airMulAdd(f: *Function, inst: Air.Inst.Index) !CValue {
78647864 const inst_ty = f.typeOfIndex(inst);
78657865 const inst_scalar_ty = inst_ty.scalarType(zcu);
78667866
7867 const bw = &f.object.code.buffered_writer;
7867 const w = &f.object.code.buffered_writer;
78687868 const local = try f.allocLocal(inst, inst_ty);
7869 const v = try Vectorize.start(f, inst, bw, inst_ty);
7870 try f.writeCValue(bw, local, .Other);
7871 try v.elem(f, bw);
7872 try bw.writeAll(" = zig_fma_");
7873 try f.object.dg.renderTypeForBuiltinFnName(bw, inst_scalar_ty);
7874 try bw.writeByte('(');
7875 try f.writeCValue(bw, mulend1, .FunctionArgument);
7876 try v.elem(f, bw);
7877 try bw.writeAll(", ");
7878 try f.writeCValue(bw, mulend2, .FunctionArgument);
7879 try v.elem(f, bw);
7880 try bw.writeAll(", ");
7881 try f.writeCValue(bw, addend, .FunctionArgument);
7882 try v.elem(f, bw);
7883 try bw.writeAll(");");
7869 const v = try Vectorize.start(f, inst, w, inst_ty);
7870 try f.writeCValue(w, local, .Other);
7871 try v.elem(f, w);
7872 try w.writeAll(" = zig_fma_");
7873 try f.object.dg.renderTypeForBuiltinFnName(w, inst_scalar_ty);
7874 try w.writeByte('(');
7875 try f.writeCValue(w, mulend1, .FunctionArgument);
7876 try v.elem(f, w);
7877 try w.writeAll(", ");
7878 try f.writeCValue(w, mulend2, .FunctionArgument);
7879 try v.elem(f, w);
7880 try w.writeAll(", ");
7881 try f.writeCValue(w, addend, .FunctionArgument);
7882 try v.elem(f, w);
7883 try w.writeAll(");");
78847884 try f.object.newline();
7885 try v.end(f, inst, bw);
7885 try v.end(f, inst, w);
78867886
78877887 return local;
78887888}
......@@ -7906,15 +7906,15 @@ fn airCVaStart(f: *Function, inst: Air.Inst.Index) !CValue {
79067906 const function_info = (try f.ctypeFromType(function_ty, .complete)).info(&f.object.dg.ctype_pool).function;
79077907 assert(function_info.varargs);
79087908
7909 const bw = &f.object.code.buffered_writer;
7909 const w = &f.object.code.buffered_writer;
79107910 const local = try f.allocLocal(inst, inst_ty);
7911 try bw.writeAll("va_start(*(va_list *)&");
7912 try f.writeCValue(bw, local, .Other);
7911 try w.writeAll("va_start(*(va_list *)&");
7912 try f.writeCValue(w, local, .Other);
79137913 if (function_info.param_ctypes.len > 0) {
7914 try bw.writeAll(", ");
7915 try f.writeCValue(bw, .{ .arg = function_info.param_ctypes.len - 1 }, .FunctionArgument);
7914 try w.writeAll(", ");
7915 try f.writeCValue(w, .{ .arg = function_info.param_ctypes.len - 1 }, .FunctionArgument);
79167916 }
7917 try bw.writeAll(");");
7917 try w.writeAll(");");
79187918 try f.object.newline();
79197919 return local;
79207920}
......@@ -7926,14 +7926,14 @@ fn airCVaArg(f: *Function, inst: Air.Inst.Index) !CValue {
79267926 const va_list = try f.resolveInst(ty_op.operand);
79277927 try reap(f, inst, &.{ty_op.operand});
79287928
7929 const bw = &f.object.code.buffered_writer;
7929 const w = &f.object.code.buffered_writer;
79307930 const local = try f.allocLocal(inst, inst_ty);
7931 try f.writeCValue(bw, local, .Other);
7932 try bw.writeAll(" = va_arg(*(va_list *)");
7933 try f.writeCValue(bw, va_list, .Other);
7934 try bw.writeAll(", ");
7935 try f.renderType(bw, ty_op.ty.toType());
7936 try bw.writeAll(");");
7931 try f.writeCValue(w, local, .Other);
7932 try w.writeAll(" = va_arg(*(va_list *)");
7933 try f.writeCValue(w, va_list, .Other);
7934 try w.writeAll(", ");
7935 try f.renderType(w, ty_op.ty.toType());
7936 try w.writeAll(");");
79377937 try f.object.newline();
79387938 return local;
79397939}
......@@ -7944,10 +7944,10 @@ fn airCVaEnd(f: *Function, inst: Air.Inst.Index) !CValue {
79447944 const va_list = try f.resolveInst(un_op);
79457945 try reap(f, inst, &.{un_op});
79467946
7947 const bw = &f.object.code.buffered_writer;
7948 try bw.writeAll("va_end(*(va_list *)");
7949 try f.writeCValue(bw, va_list, .Other);
7950 try bw.writeAll(");");
7947 const w = &f.object.code.buffered_writer;
7948 try w.writeAll("va_end(*(va_list *)");
7949 try f.writeCValue(w, va_list, .Other);
7950 try w.writeAll(");");
79517951 try f.object.newline();
79527952 return .none;
79537953}
......@@ -7959,13 +7959,13 @@ fn airCVaCopy(f: *Function, inst: Air.Inst.Index) !CValue {
79597959 const va_list = try f.resolveInst(ty_op.operand);
79607960 try reap(f, inst, &.{ty_op.operand});
79617961
7962 const bw = &f.object.code.buffered_writer;
7962 const w = &f.object.code.buffered_writer;
79637963 const local = try f.allocLocal(inst, inst_ty);
7964 try bw.writeAll("va_copy(*(va_list *)&");
7965 try f.writeCValue(bw, local, .Other);
7966 try bw.writeAll(", *(va_list *)");
7967 try f.writeCValue(bw, va_list, .Other);
7968 try bw.writeAll(");");
7964 try w.writeAll("va_copy(*(va_list *)&");
7965 try f.writeCValue(w, local, .Other);
7966 try w.writeAll(", *(va_list *)");
7967 try f.writeCValue(w, va_list, .Other);
7968 try w.writeAll(");");
79697969 try f.object.newline();
79707970 return local;
79717971}
......@@ -7981,8 +7981,8 @@ fn toMemoryOrder(order: std.builtin.AtomicOrder) [:0]const u8 {
79817981 };
79827982}
79837983
7984fn writeMemoryOrder(bw: *Writer, order: std.builtin.AtomicOrder) !void {
7985 return bw.writeAll(toMemoryOrder(order));
7984fn writeMemoryOrder(w: *Writer, order: std.builtin.AtomicOrder) !void {
7985 return w.writeAll(toMemoryOrder(order));
79867986}
79877987
79887988fn toCallingConvention(cc: std.builtin.CallingConvention, zcu: *Zcu) ?[]const u8 {
......@@ -8126,7 +8126,7 @@ const StringLiteral = struct {
81268126 len: usize,
81278127 cur_len: usize,
81288128 start_count: usize,
8129 bw: *Writer,
8129 w: *Writer,
81308130
81318131 // MSVC throws C2078 if an array of size 65536 or greater is initialized with a string literal,
81328132 // regardless of the length of the string literal initializing it. Array initializer syntax is
......@@ -8139,63 +8139,63 @@ const StringLiteral = struct {
81398139 const max_char_len = 4;
81408140 const max_literal_len = @min(16380 - max_char_len, 4095);
81418141
8142 fn init(bw: *Writer, len: usize) StringLiteral {
8142 fn init(w: *Writer, len: usize) StringLiteral {
81438143 return .{
81448144 .cur_len = 0,
81458145 .len = len,
8146 .start_count = bw.count,
8147 .bw = bw,
8146 .start_count = w.count,
8147 .w = w,
81488148 };
81498149 }
81508150
81518151 pub fn start(sl: *StringLiteral) Writer.Error!void {
81528152 if (sl.len <= max_string_initializer_len) {
8153 try sl.bw.writeByte('\"');
8153 try sl.w.writeByte('\"');
81548154 } else {
8155 try sl.bw.writeByte('{');
8155 try sl.w.writeByte('{');
81568156 }
81578157 }
81588158
81598159 pub fn end(sl: *StringLiteral) Writer.Error!void {
81608160 if (sl.len <= max_string_initializer_len) {
8161 try sl.bw.writeByte('\"');
8161 try sl.w.writeByte('\"');
81628162 } else {
8163 try sl.bw.writeByte('}');
8163 try sl.w.writeByte('}');
81648164 }
81658165 }
81668166
81678167 fn writeStringLiteralChar(sl: *StringLiteral, c: u8) Writer.Error!void {
81688168 switch (c) {
8169 7 => try sl.bw.writeAll("\\a"),
8170 8 => try sl.bw.writeAll("\\b"),
8171 '\t' => try sl.bw.writeAll("\\t"),
8172 '\n' => try sl.bw.writeAll("\\n"),
8173 11 => try sl.bw.writeAll("\\v"),
8174 12 => try sl.bw.writeAll("\\f"),
8175 '\r' => try sl.bw.writeAll("\\r"),
8176 '"', '\'', '?', '\\' => try sl.bw.print("\\{c}", .{c}),
8169 7 => try sl.w.writeAll("\\a"),
8170 8 => try sl.w.writeAll("\\b"),
8171 '\t' => try sl.w.writeAll("\\t"),
8172 '\n' => try sl.w.writeAll("\\n"),
8173 11 => try sl.w.writeAll("\\v"),
8174 12 => try sl.w.writeAll("\\f"),
8175 '\r' => try sl.w.writeAll("\\r"),
8176 '"', '\'', '?', '\\' => try sl.w.print("\\{c}", .{c}),
81778177 else => switch (c) {
8178 ' '...'~' => try sl.bw.writeByte(c),
8179 else => try sl.bw.print("\\{o:0>3}", .{c}),
8178 ' '...'~' => try sl.w.writeByte(c),
8179 else => try sl.w.print("\\{o:0>3}", .{c}),
81808180 },
81818181 }
81828182 }
81838183
81848184 pub fn writeChar(sl: *StringLiteral, c: u8) Writer.Error!void {
81858185 if (sl.len <= max_string_initializer_len) {
8186 if (sl.cur_len == 0 and sl.bw.count - sl.start_count > 1)
8187 try sl.bw.writeAll("\"\"");
8186 if (sl.cur_len == 0 and sl.w.count - sl.start_count > 1)
8187 try sl.w.writeAll("\"\"");
81888188
8189 const count = sl.bw.count;
8189 const count = sl.w.count;
81908190 try sl.writeStringLiteralChar(c);
8191 const char_len = sl.bw.count - count;
8191 const char_len = sl.w.count - count;
81928192 assert(char_len <= max_char_len);
81938193 sl.cur_len += char_len;
81948194
81958195 if (sl.cur_len >= max_literal_len) sl.cur_len = 0;
81968196 } else {
8197 if (sl.bw.count - sl.start_count > 1) try sl.bw.writeByte(',');
8198 try sl.bw.print("'\\x{x}'", .{c});
8197 if (sl.w.count - sl.start_count > 1) try sl.w.writeByte(',');
8198 try sl.w.print("'\\x{x}'", .{c});
81998199 }
82008200 }
82018201};
......@@ -8203,12 +8203,12 @@ const StringLiteral = struct {
82038203const FormatStringContext = struct { str: []const u8, sentinel: ?u8 };
82048204fn formatStringLiteral(
82058205 data: FormatStringContext,
8206 bw: *Writer,
8206 w: *Writer,
82078207 comptime fmt: []const u8,
82088208) Writer.Error!void {
82098209 if (fmt.len != 1 or fmt[0] != 's') @compileError("Invalid fmt: " ++ fmt);
82108210
8211 var literal: StringLiteral = .init(bw, data.str.len + @intFromBool(data.sentinel != null));
8211 var literal: StringLiteral = .init(w, data.str.len + @intFromBool(data.sentinel != null));
82128212 try literal.start();
82138213 for (data.str) |c| try literal.writeChar(c);
82148214 if (data.sentinel) |sentinel| if (sentinel != 0) try literal.writeChar(sentinel);
......@@ -8234,7 +8234,7 @@ const FormatIntLiteralContext = struct {
82348234};
82358235fn formatIntLiteral(
82368236 data: FormatIntLiteralContext,
8237 bw: *Writer,
8237 w: *Writer,
82388238 comptime fmt: []const u8,
82398239) Writer.Error!void {
82408240 const pt = data.dg.pt;
......@@ -8316,30 +8316,30 @@ fn formatIntLiteral(
83168316 if (c_limb_info.count == 1) {
83178317 if (wrap.addWrap(int, one, data.int_info.signedness, c_bits) or
83188318 data.int_info.signedness == .signed and wrap.subWrap(int, one, data.int_info.signedness, c_bits))
8319 return bw.print("{s}_{s}", .{
8320 data.ctype.getStandardDefineAbbrev() orelse return bw.print("zig_{s}Int_{c}{d}", .{
8319 return w.print("{s}_{s}", .{
8320 data.ctype.getStandardDefineAbbrev() orelse return w.print("zig_{s}Int_{c}{d}", .{
83218321 if (int.positive) "max" else "min", signAbbrev(data.int_info.signedness), c_bits,
83228322 }),
83238323 if (int.positive) "MAX" else "MIN",
83248324 });
83258325
8326 if (!int.positive) try bw.writeByte('-');
8327 try data.ctype.renderLiteralPrefix(bw, data.kind, ctype_pool);
8326 if (!int.positive) try w.writeByte('-');
8327 try data.ctype.renderLiteralPrefix(w, data.kind, ctype_pool);
83288328
83298329 const style: struct { base: u8, case: std.fmt.Case = undefined } = switch (fmt.len) {
83308330 0 => .{ .base = 10 },
83318331 1 => switch (fmt[0]) {
83328332 'b' => style: {
8333 try bw.writeAll("0b");
8333 try w.writeAll("0b");
83348334 break :style .{ .base = 2 };
83358335 },
83368336 'o' => style: {
8337 try bw.writeByte('0');
8337 try w.writeByte('0');
83388338 break :style .{ .base = 8 };
83398339 },
83408340 'd' => .{ .base = 10 },
83418341 'x', 'X' => |base| style: {
8342 try bw.writeAll("0x");
8342 try w.writeAll("0x");
83438343 break :style .{ .base = 16, .case = switch (base) {
83448344 'x' => .lower,
83458345 'X' => .upper,
......@@ -8354,9 +8354,9 @@ fn formatIntLiteral(
83548354 const string = int.abs().toStringAlloc(allocator, style.base, style.case) catch
83558355 return error.WriteFailed;
83568356 defer allocator.free(string);
8357 try bw.writeAll(string);
8357 try w.writeAll(string);
83588358 } else {
8359 try data.ctype.renderLiteralPrefix(bw, data.kind, ctype_pool);
8359 try data.ctype.renderLiteralPrefix(w, data.kind, ctype_pool);
83608360 wrap.truncate(int, .unsigned, c_bits);
83618361 @memset(wrap.limbs[wrap.len..], 0);
83628362 wrap.len = wrap.limbs.len;
......@@ -8399,7 +8399,7 @@ fn formatIntLiteral(
83998399 c_limb_ctype = c_limb_info.ctype;
84008400 }
84018401
8402 if (limb_offset > 0) try bw.writeAll(", ");
8402 if (limb_offset > 0) try w.writeAll(", ");
84038403 try formatIntLiteral(.{
84048404 .dg = data.dg,
84058405 .int_info = c_limb_int_info,
......@@ -8407,10 +8407,10 @@ fn formatIntLiteral(
84078407 .ctype = c_limb_ctype,
84088408 .val = pt.intValue_big(.comptime_int, c_limb_mut.toConst()) catch
84098409 return error.WriteFailed,
8410 }, bw, fmt);
8410 }, w, fmt);
84118411 }
84128412 }
8413 try data.ctype.renderLiteralSuffix(bw, ctype_pool);
8413 try data.ctype.renderLiteralSuffix(w, ctype_pool);
84148414}
84158415
84168416const Materialize = struct {
......@@ -8424,8 +8424,8 @@ const Materialize = struct {
84248424 } };
84258425 }
84268426
8427 pub fn mat(self: Materialize, f: *Function, bw: *Writer) !void {
8428 try f.writeCValue(bw, self.local, .Other);
8427 pub fn mat(self: Materialize, f: *Function, w: *Writer) !void {
8428 try f.writeCValue(w, self.local, .Other);
84298429 }
84308430
84318431 pub fn end(self: Materialize, f: *Function, inst: Air.Inst.Index) !void {
......@@ -8436,36 +8436,36 @@ const Materialize = struct {
84368436const Assignment = struct {
84378437 ctype: CType,
84388438
8439 pub fn start(f: *Function, bw: *Writer, ctype: CType) !Assignment {
8439 pub fn start(f: *Function, w: *Writer, ctype: CType) !Assignment {
84408440 const self: Assignment = .{ .ctype = ctype };
8441 try self.restart(f, bw);
8441 try self.restart(f, w);
84428442 return self;
84438443 }
84448444
8445 pub fn restart(self: Assignment, f: *Function, bw: *Writer) !void {
8445 pub fn restart(self: Assignment, f: *Function, w: *Writer) !void {
84468446 switch (self.strategy(f)) {
84478447 .assign => {},
8448 .memcpy => try bw.writeAll("memcpy("),
8448 .memcpy => try w.writeAll("memcpy("),
84498449 }
84508450 }
84518451
8452 pub fn assign(self: Assignment, f: *Function, bw: *Writer) !void {
8452 pub fn assign(self: Assignment, f: *Function, w: *Writer) !void {
84538453 switch (self.strategy(f)) {
8454 .assign => try bw.writeAll(" = "),
8455 .memcpy => try bw.writeAll(", "),
8454 .assign => try w.writeAll(" = "),
8455 .memcpy => try w.writeAll(", "),
84568456 }
84578457 }
84588458
8459 pub fn end(self: Assignment, f: *Function, bw: *Writer) !void {
8459 pub fn end(self: Assignment, f: *Function, w: *Writer) !void {
84608460 switch (self.strategy(f)) {
84618461 .assign => {},
84628462 .memcpy => {
8463 try bw.writeAll(", sizeof(");
8464 try f.renderCType(bw, self.ctype);
8465 try bw.writeAll("))");
8463 try w.writeAll(", sizeof(");
8464 try f.renderCType(w, self.ctype);
8465 try w.writeAll("))");
84668466 },
84678467 }
8468 try bw.writeByte(';');
8468 try w.writeByte(';');
84698469 try f.object.newline();
84708470 }
84718471
......@@ -8500,18 +8500,18 @@ const Vectorize = struct {
85008500 } else .{};
85018501 }
85028502
8503 pub fn elem(self: Vectorize, f: *Function, bw: *Writer) !void {
8503 pub fn elem(self: Vectorize, f: *Function, w: *Writer) !void {
85048504 if (self.index != .none) {
8505 try bw.writeByte('[');
8506 try f.writeCValue(bw, self.index, .Other);
8507 try bw.writeByte(']');
8505 try w.writeByte('[');
8506 try f.writeCValue(w, self.index, .Other);
8507 try w.writeByte(']');
85088508 }
85098509 }
85108510
8511 pub fn end(self: Vectorize, f: *Function, inst: Air.Inst.Index, bw: *Writer) !void {
8511 pub fn end(self: Vectorize, f: *Function, inst: Air.Inst.Index, w: *Writer) !void {
85128512 if (self.index != .none) {
85138513 try f.object.outdent();
8514 try bw.writeByte('}');
8514 try w.writeByte('}');
85158515 try f.object.newline();
85168516 try freeLocal(f, inst, self.index.new_local, null);
85178517 }