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 {...@@ -345,23 +345,23 @@ fn isReservedIdent(ident: []const u8) bool {
345345
346fn formatIdent(346fn formatIdent(
347 ident: []const u8,347 ident: []const u8,
348 bw: *Writer,348 writer: *Writer,
349 comptime fmt_str: []const u8,349 comptime fmt_str: []const u8,
350) Writer.Error!void {350) Writer.Error!void {
351 const solo = fmt_str.len != 0 and fmt_str[0] == ' '; // space means solo; not part of a bigger ident.351 const solo = fmt_str.len != 0 and fmt_str[0] == ' '; // space means solo; not part of a bigger ident.
352 if (solo and isReservedIdent(ident)) {352 if (solo and isReservedIdent(ident)) {
353 try bw.writeAll("zig_e_");353 try writer.writeAll("zig_e_");
354 }354 }
355 for (ident, 0..) |c, i| {355 for (ident, 0..) |c, i| {
356 switch (c) {356 switch (c) {
357 'a'...'z', 'A'...'Z', '_' => try bw.writeByte(c),357 'a'...'z', 'A'...'Z', '_' => try writer.writeByte(c),
358 '.' => try bw.writeByte('_'),358 '.' => try writer.writeByte('_'),
359 '0'...'9' => if (i == 0) {359 '0'...'9' => if (i == 0) {
360 try bw.print("_{x:2}", .{c});360 try writer.print("_{x:2}", .{c});
361 } else {361 } else {
362 try bw.writeByte(c);362 try writer.writeByte(c);
363 },363 },
364 else => try bw.print("_{x:2}", .{c}),364 else => try writer.print("_{x:2}", .{c}),
365 }365 }
366 }366 }
367}367}
...@@ -375,13 +375,13 @@ const CTypePoolStringFormatData = struct {...@@ -375,13 +375,13 @@ const CTypePoolStringFormatData = struct {
375};375};
376fn formatCTypePoolString(376fn formatCTypePoolString(
377 data: CTypePoolStringFormatData,377 data: CTypePoolStringFormatData,
378 bw: *Writer,378 writer: *Writer,
379 comptime fmt_str: []const u8,379 comptime fmt_str: []const u8,
380) Writer.Error!void {380) Writer.Error!void {
381 if (data.ctype_pool_string.toSlice(data.ctype_pool)) |slice|381 if (data.ctype_pool_string.toSlice(data.ctype_pool)) |slice|
382 try formatIdent(slice, bw, fmt_str)382 try formatIdent(slice, writer, fmt_str)
383 else383 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)});
385}385}
386pub fn fmtCTypePoolString(386pub fn fmtCTypePoolString(
387 ctype_pool_string: CType.Pool.String,387 ctype_pool_string: CType.Pool.String,
...@@ -441,18 +441,18 @@ pub const Function = struct {...@@ -441,18 +441,18 @@ pub const Function = struct {
441 const ty = f.typeOf(ref);441 const ty = f.typeOf(ref);
442442
443 const result: CValue = if (lowersToArray(ty, pt)) result: {443 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;
445 const decl_c_value = try f.allocLocalValue(.{445 const decl_c_value = try f.allocLocalValue(.{
446 .ctype = try f.ctypeFromType(ty, .complete),446 .ctype = try f.ctypeFromType(ty, .complete),
447 .alignas = CType.AlignAs.fromAbiAlignment(ty.abiAlignment(pt.zcu)),447 .alignas = CType.AlignAs.fromAbiAlignment(ty.abiAlignment(pt.zcu)),
448 });448 });
449 const gpa = f.object.dg.gpa;449 const gpa = f.object.dg.gpa;
450 try f.allocs.put(gpa, decl_c_value.new_local, false);450 try f.allocs.put(gpa, decl_c_value.new_local, false);
451 try ch.writeAll("static ");451 try writer.writeAll("static ");
452 try f.object.dg.renderTypeAndName(ch, ty, decl_c_value, Const, .none, .complete);452 try f.object.dg.renderTypeAndName(writer, ty, decl_c_value, Const, .none, .complete);
453 try ch.writeAll(" = ");453 try writer.writeAll(" = ");
454 try f.object.dg.renderValue(ch, val, .StaticInitializer);454 try f.object.dg.renderValue(writer, val, .StaticInitializer);
455 try ch.writeAll(";\n ");455 try writer.writeAll(";\n ");
456 break :result .{ .local = decl_c_value.new_local };456 break :result .{ .local = decl_c_value.new_local };
457 } else .{ .constant = val };457 } else .{ .constant = val };
458458
...@@ -505,72 +505,72 @@ pub const Function = struct {...@@ -505,72 +505,72 @@ pub const Function = struct {
505 return result;505 return result;
506 }506 }
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 {
509 switch (c_value) {509 switch (c_value) {
510 .none => unreachable,510 .none => unreachable,
511 .new_local, .local => |i| try bw.print("t{d}", .{i}),511 .new_local, .local => |i| try w.print("t{d}", .{i}),
512 .local_ref => |i| try bw.print("&t{d}", .{i}),512 .local_ref => |i| try w.print("&t{d}", .{i}),
513 .constant => |val| try f.object.dg.renderValue(bw, val, location),513 .constant => |val| try f.object.dg.renderValue(w, val, location),
514 .arg => |i| try bw.print("a{d}", .{i}),514 .arg => |i| try w.print("a{d}", .{i}),
515 .arg_array => |i| try f.writeCValueMember(bw, .{ .arg = i }, .{ .identifier = "array" }),515 .arg_array => |i| try f.writeCValueMember(w, .{ .arg = i }, .{ .identifier = "array" }),
516 .undef => |ty| try f.object.dg.renderUndefValue(bw, ty, location),516 .undef => |ty| try f.object.dg.renderUndefValue(w, ty, location),
517 else => try f.object.dg.writeCValue(bw, c_value),517 else => try f.object.dg.writeCValue(w, c_value),
518 }518 }
519 }519 }
520520
521 fn writeCValueDeref(f: *Function, bw: *Writer, c_value: CValue) !void {521 fn writeCValueDeref(f: *Function, w: *Writer, c_value: CValue) !void {
522 switch (c_value) {522 switch (c_value) {
523 .none => unreachable,523 .none => unreachable,
524 .new_local, .local, .constant => {524 .new_local, .local, .constant => {
525 try bw.writeAll("(*");525 try w.writeAll("(*");
526 try f.writeCValue(bw, c_value, .Other);526 try f.writeCValue(w, c_value, .Other);
527 try bw.writeByte(')');527 try w.writeByte(')');
528 },528 },
529 .local_ref => |i| try bw.print("t{d}", .{i}),529 .local_ref => |i| try w.print("t{d}", .{i}),
530 .arg => |i| try bw.print("(*a{d})", .{i}),530 .arg => |i| try w.print("(*a{d})", .{i}),
531 .arg_array => |i| {531 .arg_array => |i| {
532 try bw.writeAll("(*");532 try w.writeAll("(*");
533 try f.writeCValueMember(bw, .{ .arg = i }, .{ .identifier = "array" });533 try f.writeCValueMember(w, .{ .arg = i }, .{ .identifier = "array" });
534 try bw.writeByte(')');534 try w.writeByte(')');
535 },535 },
536 else => try f.object.dg.writeCValueDeref(bw, c_value),536 else => try f.object.dg.writeCValueDeref(w, c_value),
537 }537 }
538 }538 }
539539
540 fn writeCValueMember(540 fn writeCValueMember(
541 f: *Function,541 f: *Function,
542 bw: *Writer,542 w: *Writer,
543 c_value: CValue,543 c_value: CValue,
544 member: CValue,544 member: CValue,
545 ) Error!void {545 ) Error!void {
546 switch (c_value) {546 switch (c_value) {
547 .new_local, .local, .local_ref, .constant, .arg, .arg_array => {547 .new_local, .local, .local_ref, .constant, .arg, .arg_array => {
548 try f.writeCValue(bw, c_value, .Other);548 try f.writeCValue(w, c_value, .Other);
549 try bw.writeByte('.');549 try w.writeByte('.');
550 try f.writeCValue(bw, member, .Other);550 try f.writeCValue(w, member, .Other);
551 },551 },
552 else => return f.object.dg.writeCValueMember(bw, c_value, member),552 else => return f.object.dg.writeCValueMember(w, c_value, member),
553 }553 }
554 }554 }
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 {
557 switch (c_value) {557 switch (c_value) {
558 .new_local, .local, .arg, .arg_array => {558 .new_local, .local, .arg, .arg_array => {
559 try f.writeCValue(bw, c_value, .Other);559 try f.writeCValue(w, c_value, .Other);
560 try bw.writeAll("->");560 try w.writeAll("->");
561 },561 },
562 .constant => {562 .constant => {
563 try bw.writeByte('(');563 try w.writeByte('(');
564 try f.writeCValue(bw, c_value, .Other);564 try f.writeCValue(w, c_value, .Other);
565 try bw.writeAll(")->");565 try w.writeAll(")->");
566 },566 },
567 .local_ref => {567 .local_ref => {
568 try f.writeCValueDeref(bw, c_value);568 try f.writeCValueDeref(w, c_value);
569 try bw.writeByte('.');569 try w.writeByte('.');
570 },570 },
571 else => return f.object.dg.writeCValueDerefMember(bw, c_value, member),571 else => return f.object.dg.writeCValueDerefMember(w, c_value, member),
572 }572 }
573 try f.writeCValue(bw, member, .Other);573 try f.writeCValue(w, member, .Other);
574 }574 }
575575
576 fn fail(f: *Function, comptime format: []const u8, args: anytype) Error {576 fn fail(f: *Function, comptime format: []const u8, args: anytype) Error {
...@@ -585,16 +585,16 @@ pub const Function = struct {...@@ -585,16 +585,16 @@ pub const Function = struct {
585 return f.object.dg.byteSize(ctype);585 return f.object.dg.byteSize(ctype);
586 }586 }
587587
588 fn renderType(f: *Function, bw: *Writer, ctype: Type) !void {588 fn renderType(f: *Function, w: *Writer, ctype: Type) !void {
589 return f.object.dg.renderType(bw, ctype);589 return f.object.dg.renderType(w, ctype);
590 }590 }
591591
592 fn renderCType(f: *Function, bw: *Writer, ctype: CType) !void {592 fn renderCType(f: *Function, w: *Writer, ctype: CType) !void {
593 return f.object.dg.renderCType(bw, ctype);593 return f.object.dg.renderCType(w, ctype);
594 }594 }
595595
596 fn renderIntCast(f: *Function, bw: *Writer, dest_ty: Type, src: CValue, v: Vectorize, src_ty: Type, location: ValueRenderLocation) !void {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(bw, dest_ty, .{ .c_value = .{ .f = f, .value = src, .v = v } }, src_ty, location);597 return f.object.dg.renderIntCast(w, dest_ty, .{ .c_value = .{ .f = f, .value = src, .v = v } }, src_ty, location);
598 }598 }
599599
600 fn fmtIntLiteral(f: *Function, val: Value) !std.fmt.Formatter(formatIntLiteral) {600 fn fmtIntLiteral(f: *Function, val: Value) !std.fmt.Formatter(formatIntLiteral) {
...@@ -660,12 +660,12 @@ pub const Function = struct {...@@ -660,12 +660,12 @@ pub const Function = struct {
660 },660 },
661 else => {},661 else => {},
662 }662 }
663 const bw = &f.object.code.buffered_writer;663 const w = &f.object.code.buffered_writer;
664 const a = try Assignment.start(f, bw, ctype);664 const a = try Assignment.start(f, w, ctype);
665 try f.writeCValue(bw, dst, .Other);665 try f.writeCValue(w, dst, .Other);
666 try a.assign(f, bw);666 try a.assign(f, w);
667 try f.writeCValue(bw, src, .Other);667 try f.writeCValue(w, src, .Other);
668 try a.end(f, bw);668 try a.end(f, w);
669 }669 }
670670
671 fn moveCValue(f: *Function, inst: Air.Inst.Index, ty: Type, src: CValue) !CValue {671 fn moveCValue(f: *Function, inst: Air.Inst.Index, ty: Type, src: CValue) !CValue {
...@@ -702,9 +702,9 @@ pub const Object = struct {...@@ -702,9 +702,9 @@ pub const Object = struct {
702 const indent_char = ' ';702 const indent_char = ' ';
703703
704 fn newline(o: *Object) !void {704 fn newline(o: *Object) !void {
705 const bw = &o.code.buffered_writer;705 const w = &o.code.buffered_writer;
706 try bw.writeByte('\n');706 try w.writeByte('\n');
707 try bw.splatByteAll(indent_char, o.indent_counter);707 try w.splatByteAll(indent_char, o.indent_counter);
708 }708 }
709 fn indent(o: *Object) void {709 fn indent(o: *Object) void {
710 o.indent_counter += indent_width;710 o.indent_counter += indent_width;
...@@ -758,7 +758,7 @@ pub const DeclGen = struct {...@@ -758,7 +758,7 @@ pub const DeclGen = struct {
758758
759 fn renderUav(759 fn renderUav(
760 dg: *DeclGen,760 dg: *DeclGen,
761 bw: *Writer,761 w: *Writer,
762 uav: InternPool.Key.Ptr.BaseAddr.Uav,762 uav: InternPool.Key.Ptr.BaseAddr.Uav,
763 location: ValueRenderLocation,763 location: ValueRenderLocation,
764 ) Error!void {764 ) Error!void {
...@@ -772,14 +772,14 @@ pub const DeclGen = struct {...@@ -772,14 +772,14 @@ pub const DeclGen = struct {
772 // Render an undefined pointer if we have a pointer to a zero-bit or comptime type.772 // Render an undefined pointer if we have a pointer to a zero-bit or comptime type.
773 const ptr_ty: Type = .fromInterned(uav.orig_ty);773 const ptr_ty: Type = .fromInterned(uav.orig_ty);
774 if (ptr_ty.isPtrAtRuntime(zcu) and !uav_ty.isFnOrHasRuntimeBits(zcu)) {774 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 });
776 }776 }
777777
778 // Chase function values in order to be able to reference the original function.778 // Chase function values in order to be able to reference the original function.
779 switch (ip.indexToKey(uav.val)) {779 switch (ip.indexToKey(uav.val)) {
780 .variable => unreachable,780 .variable => unreachable,
781 .func => |func| return dg.renderNav(bw, func.owner_nav, location),781 .func => |func| return dg.renderNav(w, func.owner_nav, location),
782 .@"extern" => |@"extern"| return dg.renderNav(bw, @"extern".owner_nav, location),782 .@"extern" => |@"extern"| return dg.renderNav(w, @"extern".owner_nav, location),
783 else => {},783 else => {},
784 }784 }
785785
...@@ -793,13 +793,13 @@ pub const DeclGen = struct {...@@ -793,13 +793,13 @@ pub const DeclGen = struct {
793 const need_cast = !elem_ctype.eql(uav_ctype) and793 const need_cast = !elem_ctype.eql(uav_ctype) and
794 (elem_ctype.info(ctype_pool) != .function or uav_ctype.info(ctype_pool) != .function);794 (elem_ctype.info(ctype_pool) != .function or uav_ctype.info(ctype_pool) != .function);
795 if (need_cast) {795 if (need_cast) {
796 try bw.writeAll("((");796 try w.writeAll("((");
797 try dg.renderCType(bw, ptr_ctype);797 try dg.renderCType(w, ptr_ctype);
798 try bw.writeByte(')');798 try w.writeByte(')');
799 }799 }
800 try bw.writeByte('&');800 try w.writeByte('&');
801 try renderUavName(bw, uav_val);801 try renderUavName(w, uav_val);
802 if (need_cast) try bw.writeByte(')');802 if (need_cast) try w.writeByte(')');
803803
804 // Indicate that the anon decl should be rendered to the output so that804 // Indicate that the anon decl should be rendered to the output so that
805 // our reference above is not undefined.805 // our reference above is not undefined.
...@@ -820,7 +820,7 @@ pub const DeclGen = struct {...@@ -820,7 +820,7 @@ pub const DeclGen = struct {
820820
821 fn renderNav(821 fn renderNav(
822 dg: *DeclGen,822 dg: *DeclGen,
823 bw: *Writer,823 w: *Writer,
824 nav_index: InternPool.Nav.Index,824 nav_index: InternPool.Nav.Index,
825 location: ValueRenderLocation,825 location: ValueRenderLocation,
826 ) Error!void {826 ) Error!void {
...@@ -845,7 +845,7 @@ pub const DeclGen = struct {...@@ -845,7 +845,7 @@ pub const DeclGen = struct {
845 const nav_ty: Type = .fromInterned(ip.getNav(owner_nav).typeOf(ip));845 const nav_ty: Type = .fromInterned(ip.getNav(owner_nav).typeOf(ip));
846 const ptr_ty = try pt.navPtrType(owner_nav);846 const ptr_ty = try pt.navPtrType(owner_nav);
847 if (!nav_ty.isFnOrHasRuntimeBits(zcu)) {847 if (!nav_ty.isFnOrHasRuntimeBits(zcu)) {
848 return dg.writeCValue(bw, .{ .undef = ptr_ty });848 return dg.writeCValue(w, .{ .undef = ptr_ty });
849 }849 }
850850
851 // We shouldn't cast C function pointers as this is UB (when you call851 // We shouldn't cast C function pointers as this is UB (when you call
...@@ -858,18 +858,18 @@ pub const DeclGen = struct {...@@ -858,18 +858,18 @@ pub const DeclGen = struct {
858 const need_cast = !elem_ctype.eql(nav_ctype) and858 const need_cast = !elem_ctype.eql(nav_ctype) and
859 (elem_ctype.info(ctype_pool) != .function or nav_ctype.info(ctype_pool) != .function);859 (elem_ctype.info(ctype_pool) != .function or nav_ctype.info(ctype_pool) != .function);
860 if (need_cast) {860 if (need_cast) {
861 try bw.writeAll("((");861 try w.writeAll("((");
862 try dg.renderCType(bw, ctype);862 try dg.renderCType(w, ctype);
863 try bw.writeByte(')');863 try w.writeByte(')');
864 }864 }
865 try bw.writeByte('&');865 try w.writeByte('&');
866 try dg.renderNavName(bw, owner_nav);866 try dg.renderNavName(w, owner_nav);
867 if (need_cast) try bw.writeByte(')');867 if (need_cast) try w.writeByte(')');
868 }868 }
869869
870 fn renderPointer(870 fn renderPointer(
871 dg: *DeclGen,871 dg: *DeclGen,
872 bw: *Writer,872 w: *Writer,
873 derivation: Value.PointerDeriveStep,873 derivation: Value.PointerDeriveStep,
874 location: ValueRenderLocation,874 location: ValueRenderLocation,
875 ) Error!void {875 ) Error!void {
...@@ -880,18 +880,18 @@ pub const DeclGen = struct {...@@ -880,18 +880,18 @@ pub const DeclGen = struct {
880 .int => |int| {880 .int => |int| {
881 const ptr_ctype = try dg.ctypeFromType(int.ptr_ty, .complete);881 const ptr_ctype = try dg.ctypeFromType(int.ptr_ty, .complete);
882 const addr_val = try pt.intValue(.usize, int.addr);882 const addr_val = try pt.intValue(.usize, int.addr);
883 try bw.writeByte('(');883 try w.writeByte('(');
884 try dg.renderCType(bw, ptr_ctype);884 try dg.renderCType(w, ptr_ctype);
885 try bw.print("){fx}", .{try dg.fmtIntLiteral(addr_val, .Other)});885 try w.print("){fx}", .{try dg.fmtIntLiteral(addr_val, .Other)});
886 },886 },
887887
888 .nav_ptr => |nav| try dg.renderNav(bw, nav, location),888 .nav_ptr => |nav| try dg.renderNav(w, nav, location),
889 .uav_ptr => |uav| try dg.renderUav(bw, uav, location),889 .uav_ptr => |uav| try dg.renderUav(w, uav, location),
890890
891 inline .eu_payload_ptr, .opt_payload_ptr => |info| {891 inline .eu_payload_ptr, .opt_payload_ptr => |info| {
892 try bw.writeAll("&(");892 try w.writeAll("&(");
893 try dg.renderPointer(bw, info.parent.*, location);893 try dg.renderPointer(w, info.parent.*, location);
894 try bw.writeAll(")->payload");894 try w.writeAll(")->payload");
895 },895 },
896896
897 .field_ptr => |field| {897 .field_ptr => |field| {
...@@ -903,26 +903,26 @@ pub const DeclGen = struct {...@@ -903,26 +903,26 @@ pub const DeclGen = struct {
903 switch (fieldLocation(parent_ptr_ty, field.result_ptr_ty, field.field_idx, pt)) {903 switch (fieldLocation(parent_ptr_ty, field.result_ptr_ty, field.field_idx, pt)) {
904 .begin => {904 .begin => {
905 const ptr_ctype = try dg.ctypeFromType(field.result_ptr_ty, .complete);905 const ptr_ctype = try dg.ctypeFromType(field.result_ptr_ty, .complete);
906 try bw.writeByte('(');906 try w.writeByte('(');
907 try dg.renderCType(bw, ptr_ctype);907 try dg.renderCType(w, ptr_ctype);
908 try bw.writeByte(')');908 try w.writeByte(')');
909 try dg.renderPointer(bw, field.parent.*, location);909 try dg.renderPointer(w, field.parent.*, location);
910 },910 },
911 .field => |name| {911 .field => |name| {
912 try bw.writeAll("&(");912 try w.writeAll("&(");
913 try dg.renderPointer(bw, field.parent.*, location);913 try dg.renderPointer(w, field.parent.*, location);
914 try bw.writeAll(")->");914 try w.writeAll(")->");
915 try dg.writeCValue(bw, name);915 try dg.writeCValue(w, name);
916 },916 },
917 .byte_offset => |byte_offset| {917 .byte_offset => |byte_offset| {
918 const ptr_ctype = try dg.ctypeFromType(field.result_ptr_ty, .complete);918 const ptr_ctype = try dg.ctypeFromType(field.result_ptr_ty, .complete);
919 try bw.writeByte('(');919 try w.writeByte('(');
920 try dg.renderCType(bw, ptr_ctype);920 try dg.renderCType(w, ptr_ctype);
921 try bw.writeByte(')');921 try w.writeByte(')');
922 const offset_val = try pt.intValue(.usize, byte_offset);922 const offset_val = try pt.intValue(.usize, byte_offset);
923 try bw.writeAll("((char *)");923 try w.writeAll("((char *)");
924 try dg.renderPointer(bw, field.parent.*, location);924 try dg.renderPointer(w, field.parent.*, location);
925 try bw.print(" + {f})", .{try dg.fmtIntLiteral(offset_val, .Other)});925 try w.print(" + {f})", .{try dg.fmtIntLiteral(offset_val, .Other)});
926 },926 },
927 }927 }
928 },928 },
...@@ -930,10 +930,10 @@ pub const DeclGen = struct {...@@ -930,10 +930,10 @@ pub const DeclGen = struct {
930 .elem_ptr => |elem| if (!(try elem.parent.ptrType(pt)).childType(zcu).hasRuntimeBits(zcu)) {930 .elem_ptr => |elem| if (!(try elem.parent.ptrType(pt)).childType(zcu).hasRuntimeBits(zcu)) {
931 // Element type is zero-bit, so lowers to `void`. The index is irrelevant; just cast the pointer.931 // Element type is zero-bit, so lowers to `void`. The index is irrelevant; just cast the pointer.
932 const ptr_ctype = try dg.ctypeFromType(elem.result_ptr_ty, .complete);932 const ptr_ctype = try dg.ctypeFromType(elem.result_ptr_ty, .complete);
933 try bw.writeByte('(');933 try w.writeByte('(');
934 try dg.renderCType(bw, ptr_ctype);934 try dg.renderCType(w, ptr_ctype);
935 try bw.writeByte(')');935 try w.writeByte(')');
936 try dg.renderPointer(bw, elem.parent.*, location);936 try dg.renderPointer(w, elem.parent.*, location);
937 } else {937 } else {
938 const index_val = try pt.intValue(.usize, elem.elem_idx);938 const index_val = try pt.intValue(.usize, elem.elem_idx);
939 // We want to do pointer arithmetic on a pointer to the element type.939 // We want to do pointer arithmetic on a pointer to the element type.
...@@ -942,39 +942,39 @@ pub const DeclGen = struct {...@@ -942,39 +942,39 @@ pub const DeclGen = struct {
942 const parent_ctype = try dg.ctypeFromType(try elem.parent.ptrType(pt), .complete);942 const parent_ctype = try dg.ctypeFromType(try elem.parent.ptrType(pt), .complete);
943 if (result_ctype.eql(parent_ctype)) {943 if (result_ctype.eql(parent_ctype)) {
944 // The pointer already has an appropriate type - just do the arithmetic.944 // The pointer already has an appropriate type - just do the arithmetic.
945 try bw.writeByte('(');945 try w.writeByte('(');
946 try dg.renderPointer(bw, elem.parent.*, location);946 try dg.renderPointer(w, elem.parent.*, location);
947 try bw.print(" + {f})", .{try dg.fmtIntLiteral(index_val, .Other)});947 try w.print(" + {f})", .{try dg.fmtIntLiteral(index_val, .Other)});
948 } else {948 } else {
949 // We probably have an array pointer `T (*)[n]`. Cast to an element pointer,949 // We probably have an array pointer `T (*)[n]`. Cast to an element pointer,
950 // and *then* apply the index.950 // and *then* apply the index.
951 try bw.writeAll("((");951 try w.writeAll("((");
952 try dg.renderCType(bw, result_ctype);952 try dg.renderCType(w, result_ctype);
953 try bw.writeByte(')');953 try w.writeByte(')');
954 try dg.renderPointer(bw, elem.parent.*, location);954 try dg.renderPointer(w, elem.parent.*, location);
955 try bw.print(" + {f})", .{try dg.fmtIntLiteral(index_val, .Other)});955 try w.print(" + {f})", .{try dg.fmtIntLiteral(index_val, .Other)});
956 }956 }
957 },957 },
958958
959 .offset_and_cast => |oac| {959 .offset_and_cast => |oac| {
960 const ptr_ctype = try dg.ctypeFromType(oac.new_ptr_ty, .complete);960 const ptr_ctype = try dg.ctypeFromType(oac.new_ptr_ty, .complete);
961 try bw.writeByte('(');961 try w.writeByte('(');
962 try dg.renderCType(bw, ptr_ctype);962 try dg.renderCType(w, ptr_ctype);
963 try bw.writeByte(')');963 try w.writeByte(')');
964 if (oac.byte_offset == 0) {964 if (oac.byte_offset == 0) {
965 try dg.renderPointer(bw, oac.parent.*, location);965 try dg.renderPointer(w, oac.parent.*, location);
966 } else {966 } else {
967 const offset_val = try pt.intValue(.usize, oac.byte_offset);967 const offset_val = try pt.intValue(.usize, oac.byte_offset);
968 try bw.writeAll("((char *)");968 try w.writeAll("((char *)");
969 try dg.renderPointer(bw, oac.parent.*, location);969 try dg.renderPointer(w, oac.parent.*, location);
970 try bw.print(" + {f})", .{try dg.fmtIntLiteral(offset_val, .Other)});970 try w.print(" + {f})", .{try dg.fmtIntLiteral(offset_val, .Other)});
971 }971 }
972 },972 },
973 }973 }
974 }974 }
975975
976 fn renderErrorName(dg: *DeclGen, bw: *Writer, err_name: InternPool.NullTerminatedString) !void {976 fn renderErrorName(dg: *DeclGen, w: *Writer, err_name: InternPool.NullTerminatedString) !void {
977 try bw.print("zig_error_{f}", .{fmtIdent(err_name.toSlice(&dg.pt.zcu.intern_pool))});977 try w.print("zig_error_{f}", .{fmtIdent(err_name.toSlice(&dg.pt.zcu.intern_pool))});
978 }978 }
979979
980 fn renderValue(980 fn renderValue(
...@@ -1588,7 +1588,7 @@ pub const DeclGen = struct {...@@ -1588,7 +1588,7 @@ pub const DeclGen = struct {
15881588
1589 fn renderUndefValue(1589 fn renderUndefValue(
1590 dg: *DeclGen,1590 dg: *DeclGen,
1591 bw: *Writer,1591 w: *Writer,
1592 ty: Type,1592 ty: Type,
1593 location: ValueRenderLocation,1593 location: ValueRenderLocation,
1594 ) Error!void {1594 ) Error!void {
...@@ -1621,57 +1621,57 @@ pub const DeclGen = struct {...@@ -1621,57 +1621,57 @@ pub const DeclGen = struct {
1621 // All unsigned ints matching float types are pre-allocated.1621 // All unsigned ints matching float types are pre-allocated.
1622 const repr_ty = dg.pt.intType(.unsigned, bits) catch unreachable;1622 const repr_ty = dg.pt.intType(.unsigned, bits) catch unreachable;
16231623
1624 try bw.writeAll("zig_make_");1624 try w.writeAll("zig_make_");
1625 try dg.renderTypeForBuiltinFnName(bw, ty);1625 try dg.renderTypeForBuiltinFnName(w, ty);
1626 try bw.writeByte('(');1626 try w.writeByte('(');
1627 switch (bits) {1627 switch (bits) {
1628 16 => try bw.print("{x}", .{@as(f16, @bitCast(undefPattern(i16)))}),1628 16 => try w.print("{x}", .{@as(f16, @bitCast(undefPattern(i16)))}),
1629 32 => try bw.print("{x}", .{@as(f32, @bitCast(undefPattern(i32)))}),1629 32 => try w.print("{x}", .{@as(f32, @bitCast(undefPattern(i32)))}),
1630 64 => try bw.print("{x}", .{@as(f64, @bitCast(undefPattern(i64)))}),1630 64 => try w.print("{x}", .{@as(f64, @bitCast(undefPattern(i64)))}),
1631 80 => try bw.print("{x}", .{@as(f80, @bitCast(undefPattern(i80)))}),1631 80 => try w.print("{x}", .{@as(f80, @bitCast(undefPattern(i80)))}),
1632 128 => try bw.print("{x}", .{@as(f128, @bitCast(undefPattern(i128)))}),1632 128 => try w.print("{x}", .{@as(f128, @bitCast(undefPattern(i128)))}),
1633 else => unreachable,1633 else => unreachable,
1634 }1634 }
1635 try bw.writeAll(", ");1635 try w.writeAll(", ");
1636 try dg.renderUndefValue(bw, repr_ty, .FunctionArgument);1636 try dg.renderUndefValue(w, repr_ty, .FunctionArgument);
1637 return bw.writeByte(')');1637 return w.writeByte(')');
1638 },1638 },
1639 .bool_type => try bw.writeAll(if (safety_on) "0xaa" else "false"),1639 .bool_type => try w.writeAll(if (safety_on) "0xaa" else "false"),
1640 else => switch (ip.indexToKey(ty.toIntern())) {1640 else => switch (ip.indexToKey(ty.toIntern())) {
1641 .simple_type,1641 .simple_type,
1642 .int_type,1642 .int_type,
1643 .enum_type,1643 .enum_type,
1644 .error_set_type,1644 .error_set_type,
1645 .inferred_error_set_type,1645 .inferred_error_set_type,
1646 => return bw.print("{fx}", .{1646 => return w.print("{fx}", .{
1647 try dg.fmtIntLiteral(try pt.undefValue(ty), location),1647 try dg.fmtIntLiteral(try pt.undefValue(ty), location),
1648 }),1648 }),
1649 .ptr_type => |ptr_type| switch (ptr_type.flags.size) {1649 .ptr_type => |ptr_type| switch (ptr_type.flags.size) {
1650 .one, .many, .c => {1650 .one, .many, .c => {
1651 try bw.writeAll("((");1651 try w.writeAll("((");
1652 try dg.renderCType(bw, ctype);1652 try dg.renderCType(w, ctype);
1653 return bw.print("){fx})", .{1653 return w.print("){fx})", .{
1654 try dg.fmtIntLiteral(.undef_usize, .Other),1654 try dg.fmtIntLiteral(.undef_usize, .Other),
1655 });1655 });
1656 },1656 },
1657 .slice => {1657 .slice => {
1658 if (!location.isInitializer()) {1658 if (!location.isInitializer()) {
1659 try bw.writeByte('(');1659 try w.writeByte('(');
1660 try dg.renderCType(bw, ctype);1660 try dg.renderCType(w, ctype);
1661 try bw.writeByte(')');1661 try w.writeByte(')');
1662 }1662 }
16631663
1664 try bw.writeAll("{(");1664 try w.writeAll("{(");
1665 const ptr_ty = ty.slicePtrFieldType(zcu);1665 const ptr_ty = ty.slicePtrFieldType(zcu);
1666 try dg.renderType(bw, ptr_ty);1666 try dg.renderType(w, ptr_ty);
1667 return bw.print("){fx}, {0fx}}}", .{1667 return w.print("){fx}, {0fx}}}", .{
1668 try dg.fmtIntLiteral(.undef_usize, .Other),1668 try dg.fmtIntLiteral(.undef_usize, .Other),
1669 });1669 });
1670 },1670 },
1671 },1671 },
1672 .opt_type => |child_type| switch (ctype.info(ctype_pool)) {1672 .opt_type => |child_type| switch (ctype.info(ctype_pool)) {
1673 .basic, .pointer => try dg.renderUndefValue(1673 .basic, .pointer => try dg.renderUndefValue(
1674 bw,1674 w,
1675 .fromInterned(if (ctype.isBool()) .bool_type else child_type),1675 .fromInterned(if (ctype.isBool()) .bool_type else child_type),
1676 location,1676 location,
1677 ),1677 ),
...@@ -1680,21 +1680,21 @@ pub const DeclGen = struct {...@@ -1680,21 +1680,21 @@ pub const DeclGen = struct {
1680 switch (aggregate.fields.at(0, ctype_pool).name.index) {1680 switch (aggregate.fields.at(0, ctype_pool).name.index) {
1681 .is_null, .payload => {},1681 .is_null, .payload => {},
1682 .ptr, .len => return dg.renderUndefValue(1682 .ptr, .len => return dg.renderUndefValue(
1683 bw,1683 w,
1684 .fromInterned(child_type),1684 .fromInterned(child_type),
1685 location,1685 location,
1686 ),1686 ),
1687 else => unreachable,1687 else => unreachable,
1688 }1688 }
1689 if (!location.isInitializer()) {1689 if (!location.isInitializer()) {
1690 try bw.writeByte('(');1690 try w.writeByte('(');
1691 try dg.renderCType(bw, ctype);1691 try dg.renderCType(w, ctype);
1692 try bw.writeByte(')');1692 try w.writeByte(')');
1693 }1693 }
1694 try bw.writeByte('{');1694 try w.writeByte('{');
1695 for (0..aggregate.fields.len) |field_index| {1695 for (0..aggregate.fields.len) |field_index| {
1696 if (field_index > 0) try bw.writeByte(',');1696 if (field_index > 0) try w.writeByte(',');
1697 try dg.renderUndefValue(bw, .fromInterned(1697 try dg.renderUndefValue(w, .fromInterned(
1698 switch (aggregate.fields.at(field_index, ctype_pool).name.index) {1698 switch (aggregate.fields.at(field_index, ctype_pool).name.index) {
1699 .is_null => .bool_type,1699 .is_null => .bool_type,
1700 .payload => child_type,1700 .payload => child_type,
...@@ -1702,7 +1702,7 @@ pub const DeclGen = struct {...@@ -1702,7 +1702,7 @@ pub const DeclGen = struct {
1702 },1702 },
1703 ), initializer_type);1703 ), initializer_type);
1704 }1704 }
1705 try bw.writeByte('}');1705 try w.writeByte('}');
1706 },1706 },
1707 },1707 },
1708 .struct_type => {1708 .struct_type => {
...@@ -1710,117 +1710,117 @@ pub const DeclGen = struct {...@@ -1710,117 +1710,117 @@ pub const DeclGen = struct {
1710 switch (loaded_struct.layout) {1710 switch (loaded_struct.layout) {
1711 .auto, .@"extern" => {1711 .auto, .@"extern" => {
1712 if (!location.isInitializer()) {1712 if (!location.isInitializer()) {
1713 try bw.writeByte('(');1713 try w.writeByte('(');
1714 try dg.renderCType(bw, ctype);1714 try dg.renderCType(w, ctype);
1715 try bw.writeByte(')');1715 try w.writeByte(')');
1716 }1716 }
17171717
1718 try bw.writeByte('{');1718 try w.writeByte('{');
1719 var field_it = loaded_struct.iterateRuntimeOrder(ip);1719 var field_it = loaded_struct.iterateRuntimeOrder(ip);
1720 var need_comma = false;1720 var need_comma = false;
1721 while (field_it.next()) |field_index| {1721 while (field_it.next()) |field_index| {
1722 const field_ty: Type = .fromInterned(loaded_struct.field_types.get(ip)[field_index]);1722 const field_ty: Type = .fromInterned(loaded_struct.field_types.get(ip)[field_index]);
1723 if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue;1723 if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue;
17241724
1725 if (need_comma) try bw.writeByte(',');1725 if (need_comma) try w.writeByte(',');
1726 need_comma = true;1726 need_comma = true;
1727 try dg.renderUndefValue(bw, field_ty, initializer_type);1727 try dg.renderUndefValue(w, field_ty, initializer_type);
1728 }1728 }
1729 return bw.writeByte('}');1729 return w.writeByte('}');
1730 },1730 },
1731 .@"packed" => return bw.print("{fx}", .{1731 .@"packed" => return w.print("{fx}", .{
1732 try dg.fmtIntLiteral(try pt.undefValue(ty), .Other),1732 try dg.fmtIntLiteral(try pt.undefValue(ty), .Other),
1733 }),1733 }),
1734 }1734 }
1735 },1735 },
1736 .tuple_type => |tuple_info| {1736 .tuple_type => |tuple_info| {
1737 if (!location.isInitializer()) {1737 if (!location.isInitializer()) {
1738 try bw.writeByte('(');1738 try w.writeByte('(');
1739 try dg.renderCType(bw, ctype);1739 try dg.renderCType(w, ctype);
1740 try bw.writeByte(')');1740 try w.writeByte(')');
1741 }1741 }
17421742
1743 try bw.writeByte('{');1743 try w.writeByte('{');
1744 var need_comma = false;1744 var need_comma = false;
1745 for (0..tuple_info.types.len) |field_index| {1745 for (0..tuple_info.types.len) |field_index| {
1746 if (tuple_info.values.get(ip)[field_index] != .none) continue;1746 if (tuple_info.values.get(ip)[field_index] != .none) continue;
1747 const field_ty: Type = .fromInterned(tuple_info.types.get(ip)[field_index]);1747 const field_ty: Type = .fromInterned(tuple_info.types.get(ip)[field_index]);
1748 if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue;1748 if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue;
17491749
1750 if (need_comma) try bw.writeByte(',');1750 if (need_comma) try w.writeByte(',');
1751 need_comma = true;1751 need_comma = true;
1752 try dg.renderUndefValue(bw, field_ty, initializer_type);1752 try dg.renderUndefValue(w, field_ty, initializer_type);
1753 }1753 }
1754 return bw.writeByte('}');1754 return w.writeByte('}');
1755 },1755 },
1756 .union_type => {1756 .union_type => {
1757 const loaded_union = ip.loadUnionType(ty.toIntern());1757 const loaded_union = ip.loadUnionType(ty.toIntern());
1758 switch (loaded_union.flagsUnordered(ip).layout) {1758 switch (loaded_union.flagsUnordered(ip).layout) {
1759 .auto, .@"extern" => {1759 .auto, .@"extern" => {
1760 if (!location.isInitializer()) {1760 if (!location.isInitializer()) {
1761 try bw.writeByte('(');1761 try w.writeByte('(');
1762 try dg.renderCType(bw, ctype);1762 try dg.renderCType(w, ctype);
1763 try bw.writeByte(')');1763 try w.writeByte(')');
1764 }1764 }
17651765
1766 const has_tag = loaded_union.hasTag(ip);1766 const has_tag = loaded_union.hasTag(ip);
1767 if (has_tag) try bw.writeByte('{');1767 if (has_tag) try w.writeByte('{');
1768 const aggregate = ctype.info(ctype_pool).aggregate;1768 const aggregate = ctype.info(ctype_pool).aggregate;
1769 for (0..if (has_tag) aggregate.fields.len else 1) |outer_field_index| {1769 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(',');
1771 switch (if (has_tag)1771 switch (if (has_tag)
1772 aggregate.fields.at(outer_field_index, ctype_pool).name.index1772 aggregate.fields.at(outer_field_index, ctype_pool).name.index
1773 else1773 else
1774 .payload) {1774 .payload) {
1775 .tag => try dg.renderUndefValue(1775 .tag => try dg.renderUndefValue(
1776 bw,1776 w,
1777 .fromInterned(loaded_union.enum_tag_ty),1777 .fromInterned(loaded_union.enum_tag_ty),
1778 initializer_type,1778 initializer_type,
1779 ),1779 ),
1780 .payload => {1780 .payload => {
1781 try bw.writeByte('{');1781 try w.writeByte('{');
1782 for (0..loaded_union.field_types.len) |inner_field_index| {1782 for (0..loaded_union.field_types.len) |inner_field_index| {
1783 const inner_field_ty: Type = .fromInterned(1783 const inner_field_ty: Type = .fromInterned(
1784 loaded_union.field_types.get(ip)[inner_field_index],1784 loaded_union.field_types.get(ip)[inner_field_index],
1785 );1785 );
1786 if (!inner_field_ty.hasRuntimeBits(pt.zcu)) continue;1786 if (!inner_field_ty.hasRuntimeBits(pt.zcu)) continue;
1787 try dg.renderUndefValue(1787 try dg.renderUndefValue(
1788 bw,1788 w,
1789 inner_field_ty,1789 inner_field_ty,
1790 initializer_type,1790 initializer_type,
1791 );1791 );
1792 break;1792 break;
1793 }1793 }
1794 try bw.writeByte('}');1794 try w.writeByte('}');
1795 },1795 },
1796 else => unreachable,1796 else => unreachable,
1797 }1797 }
1798 }1798 }
1799 if (has_tag) try bw.writeByte('}');1799 if (has_tag) try w.writeByte('}');
1800 },1800 },
1801 .@"packed" => return bw.print("{fx}", .{1801 .@"packed" => return w.print("{fx}", .{
1802 try dg.fmtIntLiteral(try pt.undefValue(ty), .Other),1802 try dg.fmtIntLiteral(try pt.undefValue(ty), .Other),
1803 }),1803 }),
1804 }1804 }
1805 },1805 },
1806 .error_union_type => |error_union_type| switch (ctype.info(ctype_pool)) {1806 .error_union_type => |error_union_type| switch (ctype.info(ctype_pool)) {
1807 .basic => try dg.renderUndefValue(1807 .basic => try dg.renderUndefValue(
1808 bw,1808 w,
1809 .fromInterned(error_union_type.error_set_type),1809 .fromInterned(error_union_type.error_set_type),
1810 location,1810 location,
1811 ),1811 ),
1812 .pointer, .aligned, .array, .vector, .fwd_decl, .function => unreachable,1812 .pointer, .aligned, .array, .vector, .fwd_decl, .function => unreachable,
1813 .aggregate => |aggregate| {1813 .aggregate => |aggregate| {
1814 if (!location.isInitializer()) {1814 if (!location.isInitializer()) {
1815 try bw.writeByte('(');1815 try w.writeByte('(');
1816 try dg.renderCType(bw, ctype);1816 try dg.renderCType(w, ctype);
1817 try bw.writeByte(')');1817 try w.writeByte(')');
1818 }1818 }
1819 try bw.writeByte('{');1819 try w.writeByte('{');
1820 for (0..aggregate.fields.len) |field_index| {1820 for (0..aggregate.fields.len) |field_index| {
1821 if (field_index > 0) try bw.writeByte(',');1821 if (field_index > 0) try w.writeByte(',');
1822 try dg.renderUndefValue(1822 try dg.renderUndefValue(
1823 bw,1823 w,
1824 .fromInterned(1824 .fromInterned(
1825 switch (aggregate.fields.at(field_index, ctype_pool).name.index) {1825 switch (aggregate.fields.at(field_index, ctype_pool).name.index) {
1826 .@"error" => error_union_type.error_set_type,1826 .@"error" => error_union_type.error_set_type,
...@@ -1831,14 +1831,14 @@ pub const DeclGen = struct {...@@ -1831,14 +1831,14 @@ pub const DeclGen = struct {
1831 initializer_type,1831 initializer_type,
1832 );1832 );
1833 }1833 }
1834 try bw.writeByte('}');1834 try w.writeByte('}');
1835 },1835 },
1836 },1836 },
1837 .array_type, .vector_type => {1837 .array_type, .vector_type => {
1838 const ai = ty.arrayInfo(zcu);1838 const ai = ty.arrayInfo(zcu);
1839 if (ai.elem_type.eql(.u8, zcu)) {1839 if (ai.elem_type.eql(.u8, zcu)) {
1840 const c_len = ty.arrayLenIncludingSentinel(zcu);1840 const c_len = ty.arrayLenIncludingSentinel(zcu);
1841 var literal: StringLiteral = .init(bw, c_len);1841 var literal: StringLiteral = .init(w, c_len);
1842 try literal.start();1842 try literal.start();
1843 var index: u64 = 0;1843 var index: u64 = 0;
1844 while (index < c_len) : (index += 1)1844 while (index < c_len) : (index += 1)
...@@ -1846,19 +1846,19 @@ pub const DeclGen = struct {...@@ -1846,19 +1846,19 @@ pub const DeclGen = struct {
1846 return literal.end();1846 return literal.end();
1847 } else {1847 } else {
1848 if (!location.isInitializer()) {1848 if (!location.isInitializer()) {
1849 try bw.writeByte('(');1849 try w.writeByte('(');
1850 try dg.renderCType(bw, ctype);1850 try dg.renderCType(w, ctype);
1851 try bw.writeByte(')');1851 try w.writeByte(')');
1852 }1852 }
18531853
1854 try bw.writeByte('{');1854 try w.writeByte('{');
1855 const c_len = ty.arrayLenIncludingSentinel(zcu);1855 const c_len = ty.arrayLenIncludingSentinel(zcu);
1856 var index: u64 = 0;1856 var index: u64 = 0;
1857 while (index < c_len) : (index += 1) {1857 while (index < c_len) : (index += 1) {
1858 if (index > 0) try bw.writeAll(", ");1858 if (index > 0) try w.writeAll(", ");
1859 try dg.renderUndefValue(bw, ty.childType(zcu), initializer_type);1859 try dg.renderUndefValue(w, ty.childType(zcu), initializer_type);
1860 }1860 }
1861 return bw.writeByte('}');1861 return w.writeByte('}');
1862 }1862 }
1863 },1863 },
1864 .anyframe_type,1864 .anyframe_type,
...@@ -1891,7 +1891,7 @@ pub const DeclGen = struct {...@@ -1891,7 +1891,7 @@ pub const DeclGen = struct {
18911891
1892 fn renderFunctionSignature(1892 fn renderFunctionSignature(
1893 dg: *DeclGen,1893 dg: *DeclGen,
1894 bw: *Writer,1894 w: *Writer,
1895 fn_val: Value,1895 fn_val: Value,
1896 fn_align: InternPool.Alignment,1896 fn_align: InternPool.Alignment,
1897 kind: CType.Kind,1897 kind: CType.Kind,
...@@ -1913,8 +1913,8 @@ pub const DeclGen = struct {...@@ -1913,8 +1913,8 @@ pub const DeclGen = struct {
1913 const fn_info = zcu.typeToFunc(fn_ty).?;1913 const fn_info = zcu.typeToFunc(fn_ty).?;
1914 if (fn_info.cc == .naked) {1914 if (fn_info.cc == .naked) {
1915 switch (kind) {1915 switch (kind) {
1916 .forward => try bw.writeAll("zig_naked_decl "),1916 .forward => try w.writeAll("zig_naked_decl "),
1917 .complete => try bw.writeAll("zig_naked "),1917 .complete => try w.writeAll("zig_naked "),
1918 else => unreachable,1918 else => unreachable,
1919 }1919 }
1920 }1920 }
...@@ -1923,33 +1923,33 @@ pub const DeclGen = struct {...@@ -1923,33 +1923,33 @@ pub const DeclGen = struct {
1923 const func_analysis = func.analysisUnordered(ip);1923 const func_analysis = func.analysisUnordered(ip);
19241924
1925 if (func_analysis.branch_hint == .cold)1925 if (func_analysis.branch_hint == .cold)
1926 try bw.writeAll("zig_cold ");1926 try w.writeAll("zig_cold ");
19271927
1928 if (kind == .complete and func_analysis.disable_intrinsics or dg.mod.no_builtin)1928 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 ");
1930 }1930 }
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
1936 if (toCallingConvention(fn_info.cc, zcu)) |call_conv| {1936 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 });
1938 trailing = .maybe_space;1938 trailing = .maybe_space;
1939 }1939 }
19401940
1941 try bw.print("{f}", .{trailing});1941 try w.print("{f}", .{trailing});
1942 switch (name) {1942 switch (name) {
1943 .nav => |nav| try dg.renderNavName(bw, nav),1943 .nav => |nav| try dg.renderNavName(w, nav),
1944 .fmt_ctype_pool_string => |fmt| try bw.print("{f }", .{fmt}),1944 .fmt_ctype_pool_string => |fmt| try w.print("{f }", .{fmt}),
1945 .@"export" => |@"export"| try bw.print("{f }", .{fmtIdent(@"export".extern_name.toSlice(ip))}),1945 .@"export" => |@"export"| try w.print("{f }", .{fmtIdent(@"export".extern_name.toSlice(ip))}),
1946 }1946 }
19471947
1948 try renderTypeSuffix(1948 try renderTypeSuffix(
1949 dg.pass,1949 dg.pass,
1950 &dg.ctype_pool,1950 &dg.ctype_pool,
1951 zcu,1951 zcu,
1952 bw,1952 w,
1953 fn_ctype,1953 fn_ctype,
1954 .suffix,1954 .suffix,
1955 CQualifiers.init(.{ .@"const" = switch (kind) {1955 CQualifiers.init(.{ .@"const" = switch (kind) {
...@@ -1961,7 +1961,7 @@ pub const DeclGen = struct {...@@ -1961,7 +1961,7 @@ pub const DeclGen = struct {
19611961
1962 switch (kind) {1962 switch (kind) {
1963 .forward => {1963 .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});
1965 switch (name) {1965 switch (name) {
1966 .nav, .fmt_ctype_pool_string => {},1966 .nav, .fmt_ctype_pool_string => {},
1967 .@"export" => |@"export"| {1967 .@"export" => |@"export"| {
...@@ -1969,17 +1969,17 @@ pub const DeclGen = struct {...@@ -1969,17 +1969,17 @@ pub const DeclGen = struct {
1969 const is_mangled = isMangledIdent(extern_name, true);1969 const is_mangled = isMangledIdent(extern_name, true);
1970 const is_export = @"export".extern_name != @"export".main_name;1970 const is_export = @"export".extern_name != @"export".main_name;
1971 if (is_mangled and is_export) {1971 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})", .{
1973 fmtIdent(extern_name),1973 fmtIdent(extern_name),
1974 fmtStringLiteral(extern_name, null),1974 fmtStringLiteral(extern_name, null),
1975 fmtStringLiteral(@"export".main_name.toSlice(ip), null),1975 fmtStringLiteral(@"export".main_name.toSlice(ip), null),
1976 });1976 });
1977 } else if (is_mangled) {1977 } else if (is_mangled) {
1978 try bw.print(" zig_mangled({f }, {fs})", .{1978 try w.print(" zig_mangled({f }, {fs})", .{
1979 fmtIdent(extern_name), fmtStringLiteral(extern_name, null),1979 fmtIdent(extern_name), fmtStringLiteral(extern_name, null),
1980 });1980 });
1981 } else if (is_export) {1981 } else if (is_export) {
1982 try bw.print(" zig_export({fs}, {fs})", .{1982 try w.print(" zig_export({fs}, {fs})", .{
1983 fmtStringLiteral(@"export".main_name.toSlice(ip), null),1983 fmtStringLiteral(@"export".main_name.toSlice(ip), null),
1984 fmtStringLiteral(extern_name, null),1984 fmtStringLiteral(extern_name, null),
1985 });1985 });
...@@ -2012,13 +2012,13 @@ pub const DeclGen = struct {...@@ -2012,13 +2012,13 @@ pub const DeclGen = struct {
2012 /// | `renderTypeAndName` | "uint8_t *name" | "uint8_t *name[10]" |2012 /// | `renderTypeAndName` | "uint8_t *name" | "uint8_t *name[10]" |
2013 /// | `renderType` | "uint8_t *" | "uint8_t *[10]" |2013 /// | `renderType` | "uint8_t *" | "uint8_t *[10]" |
2014 ///2014 ///
2015 fn renderType(dg: *DeclGen, bw: *Writer, t: Type) Error!void {2015 fn renderType(dg: *DeclGen, w: *Writer, t: Type) Error!void {
2016 try dg.renderCType(bw, try dg.ctypeFromType(t, .complete));2016 try dg.renderCType(w, try dg.ctypeFromType(t, .complete));
2017 }2017 }
20182018
2019 fn renderCType(dg: *DeclGen, bw: *Writer, ctype: CType) Error!void {2019 fn renderCType(dg: *DeclGen, w: *Writer, ctype: CType) Error!void {
2020 _ = try renderTypePrefix(dg.pass, &dg.ctype_pool, dg.pt.zcu, bw, ctype, .suffix, .{});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, bw, ctype, .suffix, .{});2021 try renderTypeSuffix(dg.pass, &dg.ctype_pool, dg.pt.zcu, w, ctype, .suffix, .{});
2022 }2022 }
20232023
2024 const IntCastContext = union(enum) {2024 const IntCastContext = union(enum) {
...@@ -2031,13 +2031,13 @@ pub const DeclGen = struct {...@@ -2031,13 +2031,13 @@ pub const DeclGen = struct {
2031 value: Value,2031 value: Value,
2032 },2032 },
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 {
2035 switch (self.*) {2035 switch (self.*) {
2036 .c_value => |v| {2036 .c_value => |v| {
2037 try v.f.writeCValue(bw, v.value, location);2037 try v.f.writeCValue(w, v.value, location);
2038 try v.v.elem(v.f, bw);2038 try v.v.elem(v.f, w);
2039 },2039 },
2040 .value => |v| try dg.renderValue(bw, v.value, location),2040 .value => |v| try dg.renderValue(w, v.value, location),
2041 }2041 }
2042 }2042 }
2043 };2043 };
...@@ -2077,7 +2077,7 @@ pub const DeclGen = struct {...@@ -2077,7 +2077,7 @@ pub const DeclGen = struct {
2077 /// | > 64 bit integer | > 64 bit integer | zig_make_<dest_ty>(zig_hi_<src_ty>(src), zig_lo_<src_ty>(src))2077 /// | > 64 bit integer | > 64 bit integer | zig_make_<dest_ty>(zig_hi_<src_ty>(src), zig_lo_<src_ty>(src))
2078 fn renderIntCast(2078 fn renderIntCast(
2079 dg: *DeclGen,2079 dg: *DeclGen,
2080 bw: *Writer,2080 w: *Writer,
2081 dest_ty: Type,2081 dest_ty: Type,
2082 context: IntCastContext,2082 context: IntCastContext,
2083 src_ty: Type,2083 src_ty: Type,
...@@ -2102,52 +2102,52 @@ pub const DeclGen = struct {...@@ -2102,52 +2102,52 @@ pub const DeclGen = struct {
2102 dest_int_info.signedness != src_int_info.?.signedness);2102 dest_int_info.signedness != src_int_info.?.signedness);
21032103
2104 if (needs_cast) {2104 if (needs_cast) {
2105 try bw.writeByte('(');2105 try w.writeByte('(');
2106 try dg.renderType(bw, dest_ty);2106 try dg.renderType(w, dest_ty);
2107 try bw.writeByte(')');2107 try w.writeByte(')');
2108 }2108 }
2109 if (src_is_ptr) {2109 if (src_is_ptr) {
2110 try bw.writeByte('(');2110 try w.writeByte('(');
2111 try dg.renderType(bw, src_eff_ty);2111 try dg.renderType(w, src_eff_ty);
2112 try bw.writeByte(')');2112 try w.writeByte(')');
2113 }2113 }
2114 try context.writeValue(dg, bw, location);2114 try context.writeValue(dg, w, location);
2115 } else if (dest_bits <= 64 and src_bits > 64) {2115 } else if (dest_bits <= 64 and src_bits > 64) {
2116 assert(!src_is_ptr);2116 assert(!src_is_ptr);
2117 if (dest_bits < 64) {2117 if (dest_bits < 64) {
2118 try bw.writeByte('(');2118 try w.writeByte('(');
2119 try dg.renderType(bw, dest_ty);2119 try dg.renderType(w, dest_ty);
2120 try bw.writeByte(')');2120 try w.writeByte(')');
2121 }2121 }
2122 try bw.writeAll("zig_lo_");2122 try w.writeAll("zig_lo_");
2123 try dg.renderTypeForBuiltinFnName(bw, src_eff_ty);2123 try dg.renderTypeForBuiltinFnName(w, src_eff_ty);
2124 try bw.writeByte('(');2124 try w.writeByte('(');
2125 try context.writeValue(dg, bw, .FunctionArgument);2125 try context.writeValue(dg, w, .FunctionArgument);
2126 try bw.writeByte(')');2126 try w.writeByte(')');
2127 } else if (dest_bits > 64 and src_bits <= 64) {2127 } else if (dest_bits > 64 and src_bits <= 64) {
2128 try bw.writeAll("zig_make_");2128 try w.writeAll("zig_make_");
2129 try dg.renderTypeForBuiltinFnName(bw, dest_ty);2129 try dg.renderTypeForBuiltinFnName(w, dest_ty);
2130 try bw.writeAll("(0, "); // TODO: Should the 0 go through fmtIntLiteral?2130 try w.writeAll("(0, "); // TODO: Should the 0 go through fmtIntLiteral?
2131 if (src_is_ptr) {2131 if (src_is_ptr) {
2132 try bw.writeByte('(');2132 try w.writeByte('(');
2133 try dg.renderType(bw, src_eff_ty);2133 try dg.renderType(w, src_eff_ty);
2134 try bw.writeByte(')');2134 try w.writeByte(')');
2135 }2135 }
2136 try context.writeValue(dg, bw, .FunctionArgument);2136 try context.writeValue(dg, w, .FunctionArgument);
2137 try bw.writeByte(')');2137 try w.writeByte(')');
2138 } else {2138 } else {
2139 assert(!src_is_ptr);2139 assert(!src_is_ptr);
2140 try bw.writeAll("zig_make_");2140 try w.writeAll("zig_make_");
2141 try dg.renderTypeForBuiltinFnName(bw, dest_ty);2141 try dg.renderTypeForBuiltinFnName(w, dest_ty);
2142 try bw.writeAll("(zig_hi_");2142 try w.writeAll("(zig_hi_");
2143 try dg.renderTypeForBuiltinFnName(bw, src_eff_ty);2143 try dg.renderTypeForBuiltinFnName(w, src_eff_ty);
2144 try bw.writeByte('(');2144 try w.writeByte('(');
2145 try context.writeValue(dg, bw, .FunctionArgument);2145 try context.writeValue(dg, w, .FunctionArgument);
2146 try bw.writeAll("), zig_lo_");2146 try w.writeAll("), zig_lo_");
2147 try dg.renderTypeForBuiltinFnName(bw, src_eff_ty);2147 try dg.renderTypeForBuiltinFnName(w, src_eff_ty);
2148 try bw.writeByte('(');2148 try w.writeByte('(');
2149 try context.writeValue(dg, bw, .FunctionArgument);2149 try context.writeValue(dg, w, .FunctionArgument);
2150 try bw.writeAll("))");2150 try w.writeAll("))");
2151 }2151 }
2152 }2152 }
21532153
...@@ -2161,7 +2161,7 @@ pub const DeclGen = struct {...@@ -2161,7 +2161,7 @@ pub const DeclGen = struct {
2161 ///2161 ///
2162 fn renderTypeAndName(2162 fn renderTypeAndName(
2163 dg: *DeclGen,2163 dg: *DeclGen,
2164 bw: *Writer,2164 w: *Writer,
2165 ty: Type,2165 ty: Type,
2166 name: CValue,2166 name: CValue,
2167 qualifiers: CQualifiers,2167 qualifiers: CQualifiers,
...@@ -2169,7 +2169,7 @@ pub const DeclGen = struct {...@@ -2169,7 +2169,7 @@ pub const DeclGen = struct {
2169 kind: CType.Kind,2169 kind: CType.Kind,
2170 ) !void {2170 ) !void {
2171 try dg.renderCTypeAndName(2171 try dg.renderCTypeAndName(
2172 bw,2172 w,
2173 try dg.ctypeFromType(ty, kind),2173 try dg.ctypeFromType(ty, kind),
2174 name,2174 name,
2175 qualifiers,2175 qualifiers,
...@@ -2182,7 +2182,7 @@ pub const DeclGen = struct {...@@ -2182,7 +2182,7 @@ pub const DeclGen = struct {
21822182
2183 fn renderCTypeAndName(2183 fn renderCTypeAndName(
2184 dg: *DeclGen,2184 dg: *DeclGen,
2185 bw: *Writer,2185 w: *Writer,
2186 ctype: CType,2186 ctype: CType,
2187 name: CValue,2187 name: CValue,
2188 qualifiers: CQualifiers,2188 qualifiers: CQualifiers,
...@@ -2190,52 +2190,52 @@ pub const DeclGen = struct {...@@ -2190,52 +2190,52 @@ pub const DeclGen = struct {
2190 ) !void {2190 ) !void {
2191 const zcu = dg.pt.zcu;2191 const zcu = dg.pt.zcu;
2192 switch (alignas.abiOrder()) {2192 switch (alignas.abiOrder()) {
2193 .lt => try bw.print("zig_under_align({}) ", .{alignas.toByteUnits()}),2193 .lt => try w.print("zig_under_align({}) ", .{alignas.toByteUnits()}),
2194 .eq => {},2194 .eq => {},
2195 .gt => try bw.print("zig_align({}) ", .{alignas.toByteUnits()}),2195 .gt => try w.print("zig_align({}) ", .{alignas.toByteUnits()}),
2196 }2196 }
21972197
2198 try bw.print("{f}", .{2198 try w.print("{f}", .{
2199 try renderTypePrefix(dg.pass, &dg.ctype_pool, zcu, bw, ctype, .suffix, qualifiers),2199 try renderTypePrefix(dg.pass, &dg.ctype_pool, zcu, w, ctype, .suffix, qualifiers),
2200 });2200 });
2201 try dg.writeName(bw, name);2201 try dg.writeName(w, name);
2202 try renderTypeSuffix(dg.pass, &dg.ctype_pool, zcu, bw, ctype, .suffix, .{});2202 try renderTypeSuffix(dg.pass, &dg.ctype_pool, zcu, w, ctype, .suffix, .{});
2203 }2203 }
22042204
2205 fn writeName(dg: *DeclGen, bw: *Writer, c_value: CValue) !void {2205 fn writeName(dg: *DeclGen, w: *Writer, c_value: CValue) !void {
2206 switch (c_value) {2206 switch (c_value) {
2207 .new_local, .local => |i| try bw.print("t{d}", .{i}),2207 .new_local, .local => |i| try w.print("t{d}", .{i}),
2208 .constant => |uav| try renderUavName(bw, uav),2208 .constant => |uav| try renderUavName(w, uav),
2209 .nav => |nav| try dg.renderNavName(bw, nav),2209 .nav => |nav| try dg.renderNavName(w, nav),
2210 .identifier => |ident| try bw.print("{f }", .{fmtIdent(ident)}),2210 .identifier => |ident| try w.print("{f }", .{fmtIdent(ident)}),
2211 else => unreachable,2211 else => unreachable,
2212 }2212 }
2213 }2213 }
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 {
2216 switch (c_value) {2216 switch (c_value) {
2217 .none, .new_local, .local, .local_ref => unreachable,2217 .none, .new_local, .local, .local_ref => unreachable,
2218 .constant => |uav| try renderUavName(bw, uav),2218 .constant => |uav| try renderUavName(w, uav),
2219 .arg, .arg_array => unreachable,2219 .arg, .arg_array => unreachable,
2220 .field => |i| try bw.print("f{d}", .{i}),2220 .field => |i| try w.print("f{d}", .{i}),
2221 .nav => |nav| try dg.renderNavName(bw, nav),2221 .nav => |nav| try dg.renderNavName(w, nav),
2222 .nav_ref => |nav| {2222 .nav_ref => |nav| {
2223 try bw.writeByte('&');2223 try w.writeByte('&');
2224 try dg.renderNavName(bw, nav);2224 try dg.renderNavName(w, nav);
2225 },2225 },
2226 .undef => |ty| try dg.renderUndefValue(bw, ty, .Other),2226 .undef => |ty| try dg.renderUndefValue(w, ty, .Other),
2227 .identifier => |ident| try bw.print("{f }", .{fmtIdent(ident)}),2227 .identifier => |ident| try w.print("{f }", .{fmtIdent(ident)}),
2228 .payload_identifier => |ident| try bw.print("{f }.{f }", .{2228 .payload_identifier => |ident| try w.print("{f }.{f }", .{
2229 fmtIdent("payload"),2229 fmtIdent("payload"),
2230 fmtIdent(ident),2230 fmtIdent(ident),
2231 }),2231 }),
2232 .ctype_pool_string => |string| try bw.print("{f }", .{2232 .ctype_pool_string => |string| try w.print("{f }", .{
2233 fmtCTypePoolString(string, &dg.ctype_pool),2233 fmtCTypePoolString(string, &dg.ctype_pool),
2234 }),2234 }),
2235 }2235 }
2236 }2236 }
22372237
2238 fn writeCValueDeref(dg: *DeclGen, bw: *Writer, c_value: CValue) !void {2238 fn writeCValueDeref(dg: *DeclGen, w: *Writer, c_value: CValue) !void {
2239 switch (c_value) {2239 switch (c_value) {
2240 .none,2240 .none,
2241 .new_local,2241 .new_local,
...@@ -2246,16 +2246,16 @@ pub const DeclGen = struct {...@@ -2246,16 +2246,16 @@ pub const DeclGen = struct {
2246 .arg_array,2246 .arg_array,
2247 .ctype_pool_string,2247 .ctype_pool_string,
2248 => unreachable,2248 => unreachable,
2249 .field => |i| try bw.print("f{d}", .{i}),2249 .field => |i| try w.print("f{d}", .{i}),
2250 .nav => |nav| {2250 .nav => |nav| {
2251 try bw.writeAll("(*");2251 try w.writeAll("(*");
2252 try dg.renderNavName(bw, nav);2252 try dg.renderNavName(w, nav);
2253 try bw.writeByte(')');2253 try w.writeByte(')');
2254 },2254 },
2255 .nav_ref => |nav| try dg.renderNavName(bw, nav),2255 .nav_ref => |nav| try dg.renderNavName(w, nav),
2256 .undef => unreachable,2256 .undef => unreachable,
2257 .identifier => |ident| try bw.print("(*{f })", .{fmtIdent(ident)}),2257 .identifier => |ident| try w.print("(*{f })", .{fmtIdent(ident)}),
2258 .payload_identifier => |ident| try bw.print("(*{f }.{f })", .{2258 .payload_identifier => |ident| try w.print("(*{f }.{f })", .{
2259 fmtIdent("payload"),2259 fmtIdent("payload"),
2260 fmtIdent(ident),2260 fmtIdent(ident),
2261 }),2261 }),
...@@ -2264,18 +2264,18 @@ pub const DeclGen = struct {...@@ -2264,18 +2264,18 @@ pub const DeclGen = struct {
22642264
2265 fn writeCValueMember(2265 fn writeCValueMember(
2266 dg: *DeclGen,2266 dg: *DeclGen,
2267 bw: *Writer,2267 w: *Writer,
2268 c_value: CValue,2268 c_value: CValue,
2269 member: CValue,2269 member: CValue,
2270 ) Error!void {2270 ) Error!void {
2271 try dg.writeCValue(bw, c_value);2271 try dg.writeCValue(w, c_value);
2272 try bw.writeByte('.');2272 try w.writeByte('.');
2273 try dg.writeCValue(bw, member);2273 try dg.writeCValue(w, member);
2274 }2274 }
22752275
2276 fn writeCValueDerefMember(2276 fn writeCValueDerefMember(
2277 dg: *DeclGen,2277 dg: *DeclGen,
2278 bw: *Writer,2278 w: *Writer,
2279 c_value: CValue,2279 c_value: CValue,
2280 member: CValue,2280 member: CValue,
2281 ) !void {2281 ) !void {
...@@ -2292,15 +2292,15 @@ pub const DeclGen = struct {...@@ -2292,15 +2292,15 @@ pub const DeclGen = struct {
2292 .ctype_pool_string,2292 .ctype_pool_string,
2293 => unreachable,2293 => unreachable,
2294 .nav, .identifier, .payload_identifier => {2294 .nav, .identifier, .payload_identifier => {
2295 try dg.writeCValue(bw, c_value);2295 try dg.writeCValue(w, c_value);
2296 try bw.writeAll("->");2296 try w.writeAll("->");
2297 },2297 },
2298 .nav_ref => {2298 .nav_ref => {
2299 try dg.writeCValueDeref(bw, c_value);2299 try dg.writeCValueDeref(w, c_value);
2300 try bw.writeByte('.');2300 try w.writeByte('.');
2301 },2301 },
2302 }2302 }
2303 try dg.writeCValue(bw, member);2303 try dg.writeCValue(w, member);
2304 }2304 }
23052305
2306 fn renderFwdDecl(2306 fn renderFwdDecl(
...@@ -2342,36 +2342,36 @@ pub const DeclGen = struct {...@@ -2342,36 +2342,36 @@ pub const DeclGen = struct {
2342 try fwd.writeAll(";\n");2342 try fwd.writeAll(";\n");
2343 }2343 }
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 {
2346 const zcu = dg.pt.zcu;2346 const zcu = dg.pt.zcu;
2347 const ip = &zcu.intern_pool;2347 const ip = &zcu.intern_pool;
2348 const nav = ip.getNav(nav_index);2348 const nav = ip.getNav(nav_index);
2349 if (nav.getExtern(ip)) |@"extern"| {2349 if (nav.getExtern(ip)) |@"extern"| {
2350 try bw.print("{f }", .{2350 try w.print("{f }", .{
2351 fmtIdent(ip.getNav(@"extern".owner_nav).name.toSlice(ip)),2351 fmtIdent(ip.getNav(@"extern".owner_nav).name.toSlice(ip)),
2352 });2352 });
2353 } else {2353 } else {
2354 // MSVC has a limit of 4095 character token length limit, and fmtIdent can (worst case),2354 // MSVC has a limit of 4095 character token length limit, and fmtIdent can (worst case),
2355 // expand to 3x the length of its input, but let's cut it off at a much shorter limit.2355 // expand to 3x the length of its input, but let's cut it off at a much shorter limit.
2356 const fqn_slice = ip.getNav(nav_index).fqn.toSlice(ip);2356 const fqn_slice = ip.getNav(nav_index).fqn.toSlice(ip);
2357 try bw.print("{f}__{d}", .{2357 try w.print("{f}__{d}", .{
2358 fmtIdent(fqn_slice[0..@min(fqn_slice.len, 100)]),2358 fmtIdent(fqn_slice[0..@min(fqn_slice.len, 100)]),
2359 @intFromEnum(nav_index),2359 @intFromEnum(nav_index),
2360 });2360 });
2361 }2361 }
2362 }2362 }
23632363
2364 fn renderUavName(bw: *Writer, uav: Value) !void {2364 fn renderUavName(w: *Writer, uav: Value) !void {
2365 try bw.print("__anon_{d}", .{@intFromEnum(uav.toIntern())});2365 try w.print("__anon_{d}", .{@intFromEnum(uav.toIntern())});
2366 }2366 }
23672367
2368 fn renderTypeForBuiltinFnName(dg: *DeclGen, bw: *Writer, ty: Type) !void {2368 fn renderTypeForBuiltinFnName(dg: *DeclGen, w: *Writer, ty: Type) !void {
2369 try dg.renderCTypeForBuiltinFnName(bw, try dg.ctypeFromType(ty, .complete));2369 try dg.renderCTypeForBuiltinFnName(w, try dg.ctypeFromType(ty, .complete));
2370 }2370 }
23712371
2372 fn renderCTypeForBuiltinFnName(dg: *DeclGen, bw: *Writer, ctype: CType) !void {2372 fn renderCTypeForBuiltinFnName(dg: *DeclGen, w: *Writer, ctype: CType) !void {
2373 switch (ctype.info(&dg.ctype_pool)) {2373 switch (ctype.info(&dg.ctype_pool)) {
2374 else => |ctype_info| try bw.print("{c}{d}", .{2374 else => |ctype_info| try w.print("{c}{d}", .{
2375 if (ctype.isBool())2375 if (ctype.isBool())
2376 signAbbrev(.unsigned)2376 signAbbrev(.unsigned)
2377 else if (ctype.isInteger())2377 else if (ctype.isInteger())
...@@ -2384,11 +2384,11 @@ pub const DeclGen = struct {...@@ -2384,11 +2384,11 @@ pub const DeclGen = struct {
2384 return dg.fail("TODO: CBE: implement renderTypeForBuiltinFnName for {s} type", .{@tagName(ctype_info)}),2384 return dg.fail("TODO: CBE: implement renderTypeForBuiltinFnName for {s} type", .{@tagName(ctype_info)}),
2385 if (ctype.isFloat()) ctype.floatActiveBits(dg.mod) else dg.byteSize(ctype) * 8,2385 if (ctype.isFloat()) ctype.floatActiveBits(dg.mod) else dg.byteSize(ctype) * 8,
2386 }),2386 }),
2387 .array => try bw.writeAll("big"),2387 .array => try w.writeAll("big"),
2388 }2388 }
2389 }2389 }
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 {
2392 const ctype = try dg.ctypeFromType(ty, .complete);2392 const ctype = try dg.ctypeFromType(ty, .complete);
2393 const is_big = ctype.info(&dg.ctype_pool) == .array;2393 const is_big = ctype.info(&dg.ctype_pool) == .array;
2394 switch (info) {2394 switch (info) {
...@@ -2403,8 +2403,8 @@ pub const DeclGen = struct {...@@ -2403,8 +2403,8 @@ pub const DeclGen = struct {
2403 .bits = @intCast(ty.bitSize(zcu)),2403 .bits = @intCast(ty.bitSize(zcu)),
2404 };2404 };
24052405
2406 if (is_big) try bw.print(", {}", .{int_info.signedness == .signed});2406 if (is_big) try w.print(", {}", .{int_info.signedness == .signed});
2407 try bw.print(", {f}", .{try dg.fmtIntLiteral(2407 try w.print(", {f}", .{try dg.fmtIntLiteral(
2408 try pt.intValue(if (is_big) .u16 else .u8, int_info.bits),2408 try pt.intValue(if (is_big) .u16 else .u8, int_info.bits),
2409 .FunctionArgument,2409 .FunctionArgument,
2410 )});2410 )});
...@@ -2437,32 +2437,32 @@ const RenderCTypeTrailing = enum {...@@ -2437,32 +2437,32 @@ const RenderCTypeTrailing = enum {
24372437
2438 pub fn format(2438 pub fn format(
2439 self: @This(),2439 self: @This(),
2440 bw: *Writer,2440 w: *Writer,
2441 comptime fmt: []const u8,2441 comptime fmt: []const u8,
2442 ) Writer.Error!void {2442 ) Writer.Error!void {
2443 if (fmt.len != 0) @compileError("invalid format string '" ++2443 if (fmt.len != 0) @compileError("invalid format string '" ++
2444 fmt ++ "' for type '" ++ @typeName(@This()) ++ "'");2444 fmt ++ "' for type '" ++ @typeName(@This()) ++ "'");
2445 switch (self) {2445 switch (self) {
2446 .no_space => {},2446 .no_space => {},
2447 .maybe_space => try bw.writeByte(' '),2447 .maybe_space => try w.writeByte(' '),
2448 }2448 }
2449 }2449 }
2450};2450};
2451fn renderAlignedTypeName(bw: *Writer, ctype: CType) !void {2451fn renderAlignedTypeName(w: *Writer, ctype: CType) !void {
2452 try bw.print("anon__aligned_{d}", .{@intFromEnum(ctype.index)});2452 try w.print("anon__aligned_{d}", .{@intFromEnum(ctype.index)});
2453}2453}
2454fn renderFwdDeclTypeName(2454fn renderFwdDeclTypeName(
2455 zcu: *Zcu,2455 zcu: *Zcu,
2456 bw: *Writer,2456 w: *Writer,
2457 ctype: CType,2457 ctype: CType,
2458 fwd_decl: CType.Info.FwdDecl,2458 fwd_decl: CType.Info.FwdDecl,
2459 attributes: []const u8,2459 attributes: []const u8,
2460) !void {2460) !void {
2461 const ip = &zcu.intern_pool;2461 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 });
2463 switch (fwd_decl.name) {2463 switch (fwd_decl.name) {
2464 .anon => try bw.print("anon__lazy_{d}", .{@intFromEnum(ctype.index)}),2464 .anon => try w.print("anon__lazy_{d}", .{@intFromEnum(ctype.index)}),
2465 .index => |index| try bw.print("{f}__{d}", .{2465 .index => |index| try w.print("{f}__{d}", .{
2466 fmtIdent(Type.fromInterned(index).containerTypeName(ip).toSlice(&zcu.intern_pool)),2466 fmtIdent(Type.fromInterned(index).containerTypeName(ip).toSlice(&zcu.intern_pool)),
2467 @intFromEnum(index),2467 @intFromEnum(index),
2468 }),2468 }),
...@@ -2472,21 +2472,21 @@ fn renderTypePrefix(...@@ -2472,21 +2472,21 @@ fn renderTypePrefix(
2472 pass: DeclGen.Pass,2472 pass: DeclGen.Pass,
2473 ctype_pool: *const CType.Pool,2473 ctype_pool: *const CType.Pool,
2474 zcu: *Zcu,2474 zcu: *Zcu,
2475 bw: *Writer,2475 w: *Writer,
2476 ctype: CType,2476 ctype: CType,
2477 parent_fix: CTypeFix,2477 parent_fix: CTypeFix,
2478 qualifiers: CQualifiers,2478 qualifiers: CQualifiers,
2479) Writer.Error!RenderCTypeTrailing {2479) Writer.Error!RenderCTypeTrailing {
2480 var trailing = RenderCTypeTrailing.maybe_space;2480 var trailing = RenderCTypeTrailing.maybe_space;
2481 switch (ctype.info(ctype_pool)) {2481 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
2484 .pointer => |pointer_info| {2484 .pointer => |pointer_info| {
2485 try bw.print("{f}*", .{try renderTypePrefix(2485 try w.print("{f}*", .{try renderTypePrefix(
2486 pass,2486 pass,
2487 ctype_pool,2487 ctype_pool,
2488 zcu,2488 zcu,
2489 bw,2489 w,
2490 pointer_info.elem_ctype,2490 pointer_info.elem_ctype,
2491 .prefix,2491 .prefix,
2492 CQualifiers.init(.{2492 CQualifiers.init(.{
...@@ -2498,13 +2498,13 @@ fn renderTypePrefix(...@@ -2498,13 +2498,13 @@ fn renderTypePrefix(
2498 },2498 },
24992499
2500 .aligned => switch (pass) {2500 .aligned => switch (pass) {
2501 .nav => |nav| try bw.print("nav__{d}_{d}", .{2501 .nav => |nav| try w.print("nav__{d}_{d}", .{
2502 @intFromEnum(nav), @intFromEnum(ctype.index),2502 @intFromEnum(nav), @intFromEnum(ctype.index),
2503 }),2503 }),
2504 .uav => |uav| try bw.print("uav__{d}_{d}", .{2504 .uav => |uav| try w.print("uav__{d}_{d}", .{
2505 @intFromEnum(uav), @intFromEnum(ctype.index),2505 @intFromEnum(uav), @intFromEnum(ctype.index),
2506 }),2506 }),
2507 .flush => try renderAlignedTypeName(bw, ctype),2507 .flush => try renderAlignedTypeName(w, ctype),
2508 },2508 },
25092509
2510 .array, .vector => |sequence_info| {2510 .array, .vector => |sequence_info| {
...@@ -2512,14 +2512,14 @@ fn renderTypePrefix(...@@ -2512,14 +2512,14 @@ fn renderTypePrefix(
2512 pass,2512 pass,
2513 ctype_pool,2513 ctype_pool,
2514 zcu,2514 zcu,
2515 bw,2515 w,
2516 sequence_info.elem_ctype,2516 sequence_info.elem_ctype,
2517 .suffix,2517 .suffix,
2518 qualifiers,2518 qualifiers,
2519 );2519 );
2520 switch (parent_fix) {2520 switch (parent_fix) {
2521 .prefix => {2521 .prefix => {
2522 try bw.print("{f}(", .{child_trailing});2522 try w.print("{f}(", .{child_trailing});
2523 return .no_space;2523 return .no_space;
2524 },2524 },
2525 .suffix => return child_trailing,2525 .suffix => return child_trailing,
...@@ -2528,31 +2528,31 @@ fn renderTypePrefix(...@@ -2528,31 +2528,31 @@ fn renderTypePrefix(
25282528
2529 .fwd_decl => |fwd_decl_info| switch (fwd_decl_info.name) {2529 .fwd_decl => |fwd_decl_info| switch (fwd_decl_info.name) {
2530 .anon => switch (pass) {2530 .anon => switch (pass) {
2531 .nav => |nav| try bw.print("nav__{d}_{d}", .{2531 .nav => |nav| try w.print("nav__{d}_{d}", .{
2532 @intFromEnum(nav), @intFromEnum(ctype.index),2532 @intFromEnum(nav), @intFromEnum(ctype.index),
2533 }),2533 }),
2534 .uav => |uav| try bw.print("uav__{d}_{d}", .{2534 .uav => |uav| try w.print("uav__{d}_{d}", .{
2535 @intFromEnum(uav), @intFromEnum(ctype.index),2535 @intFromEnum(uav), @intFromEnum(ctype.index),
2536 }),2536 }),
2537 .flush => try renderFwdDeclTypeName(zcu, bw, ctype, fwd_decl_info, ""),2537 .flush => try renderFwdDeclTypeName(zcu, w, ctype, fwd_decl_info, ""),
2538 },2538 },
2539 .index => try renderFwdDeclTypeName(zcu, bw, ctype, fwd_decl_info, ""),2539 .index => try renderFwdDeclTypeName(zcu, w, ctype, fwd_decl_info, ""),
2540 },2540 },
25412541
2542 .aggregate => |aggregate_info| switch (aggregate_info.name) {2542 .aggregate => |aggregate_info| switch (aggregate_info.name) {
2543 .anon => {2543 .anon => {
2544 try bw.print("{s} {s}", .{2544 try w.print("{s} {s}", .{
2545 @tagName(aggregate_info.tag),2545 @tagName(aggregate_info.tag),
2546 if (aggregate_info.@"packed") "zig_packed(" else "",2546 if (aggregate_info.@"packed") "zig_packed(" else "",
2547 });2547 });
2548 try renderFields(zcu, bw, ctype_pool, aggregate_info, 1);2548 try renderFields(zcu, w, ctype_pool, aggregate_info, 1);
2549 if (aggregate_info.@"packed") try bw.writeByte(')');2549 if (aggregate_info.@"packed") try w.writeByte(')');
2550 },2550 },
2551 .fwd_decl => |fwd_decl| return renderTypePrefix(2551 .fwd_decl => |fwd_decl| return renderTypePrefix(
2552 pass,2552 pass,
2553 ctype_pool,2553 ctype_pool,
2554 zcu,2554 zcu,
2555 bw,2555 w,
2556 fwd_decl,2556 fwd_decl,
2557 parent_fix,2557 parent_fix,
2558 qualifiers,2558 qualifiers,
...@@ -2564,14 +2564,14 @@ fn renderTypePrefix(...@@ -2564,14 +2564,14 @@ fn renderTypePrefix(
2564 pass,2564 pass,
2565 ctype_pool,2565 ctype_pool,
2566 zcu,2566 zcu,
2567 bw,2567 w,
2568 function_info.return_ctype,2568 function_info.return_ctype,
2569 .suffix,2569 .suffix,
2570 .{},2570 .{},
2571 );2571 );
2572 switch (parent_fix) {2572 switch (parent_fix) {
2573 .prefix => {2573 .prefix => {
2574 try bw.print("{f}(", .{child_trailing});2574 try w.print("{f}(", .{child_trailing});
2575 return .no_space;2575 return .no_space;
2576 },2576 },
2577 .suffix => return child_trailing,2577 .suffix => return child_trailing,
...@@ -2580,7 +2580,7 @@ fn renderTypePrefix(...@@ -2580,7 +2580,7 @@ fn renderTypePrefix(
2580 }2580 }
2581 var qualifier_it = qualifiers.iterator();2581 var qualifier_it = qualifiers.iterator();
2582 while (qualifier_it.next()) |qualifier| {2582 while (qualifier_it.next()) |qualifier| {
2583 try bw.print("{f}{s}", .{ trailing, @tagName(qualifier) });2583 try w.print("{f}{s}", .{ trailing, @tagName(qualifier) });
2584 trailing = .maybe_space;2584 trailing = .maybe_space;
2585 }2585 }
2586 return trailing;2586 return trailing;
...@@ -2589,7 +2589,7 @@ fn renderTypeSuffix(...@@ -2589,7 +2589,7 @@ fn renderTypeSuffix(
2589 pass: DeclGen.Pass,2589 pass: DeclGen.Pass,
2590 ctype_pool: *const CType.Pool,2590 ctype_pool: *const CType.Pool,
2591 zcu: *Zcu,2591 zcu: *Zcu,
2592 bw: *Writer,2592 w: *Writer,
2593 ctype: CType,2593 ctype: CType,
2594 parent_fix: CTypeFix,2594 parent_fix: CTypeFix,
2595 qualifiers: CQualifiers,2595 qualifiers: CQualifiers,
...@@ -2600,94 +2600,94 @@ fn renderTypeSuffix(...@@ -2600,94 +2600,94 @@ fn renderTypeSuffix(
2600 pass,2600 pass,
2601 ctype_pool,2601 ctype_pool,
2602 zcu,2602 zcu,
2603 bw,2603 w,
2604 pointer_info.elem_ctype,2604 pointer_info.elem_ctype,
2605 .prefix,2605 .prefix,
2606 .{},2606 .{},
2607 ),2607 ),
2608 .array, .vector => |sequence_info| {2608 .array, .vector => |sequence_info| {
2609 switch (parent_fix) {2609 switch (parent_fix) {
2610 .prefix => try bw.writeByte(')'),2610 .prefix => try w.writeByte(')'),
2611 .suffix => {},2611 .suffix => {},
2612 }2612 }
26132613
2614 try bw.print("[{}]", .{sequence_info.len});2614 try w.print("[{}]", .{sequence_info.len});
2615 try renderTypeSuffix(pass, ctype_pool, zcu, bw, sequence_info.elem_ctype, .suffix, .{});2615 try renderTypeSuffix(pass, ctype_pool, zcu, w, sequence_info.elem_ctype, .suffix, .{});
2616 },2616 },
2617 .function => |function_info| {2617 .function => |function_info| {
2618 switch (parent_fix) {2618 switch (parent_fix) {
2619 .prefix => try bw.writeByte(')'),2619 .prefix => try w.writeByte(')'),
2620 .suffix => {},2620 .suffix => {},
2621 }2621 }
26222622
2623 try bw.writeByte('(');2623 try w.writeByte('(');
2624 var need_comma = false;2624 var need_comma = false;
2625 for (0..function_info.param_ctypes.len) |param_index| {2625 for (0..function_info.param_ctypes.len) |param_index| {
2626 const param_type = function_info.param_ctypes.at(param_index, ctype_pool);2626 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(", ");
2628 need_comma = true;2628 need_comma = true;
2629 const trailing =2629 const trailing =
2630 try renderTypePrefix(pass, ctype_pool, zcu, bw, param_type, .suffix, qualifiers);2630 try renderTypePrefix(pass, ctype_pool, zcu, w, param_type, .suffix, qualifiers);
2631 if (qualifiers.contains(.@"const")) try bw.print("{f}a{d}", .{ trailing, param_index });2631 if (qualifiers.contains(.@"const")) try w.print("{f}a{d}", .{ trailing, param_index });
2632 try renderTypeSuffix(pass, ctype_pool, zcu, bw, param_type, .suffix, .{});2632 try renderTypeSuffix(pass, ctype_pool, zcu, w, param_type, .suffix, .{});
2633 }2633 }
2634 if (function_info.varargs) {2634 if (function_info.varargs) {
2635 if (need_comma) try bw.writeAll(", ");2635 if (need_comma) try w.writeAll(", ");
2636 need_comma = true;2636 need_comma = true;
2637 try bw.writeAll("...");2637 try w.writeAll("...");
2638 }2638 }
2639 if (!need_comma) try bw.writeAll("void");2639 if (!need_comma) try w.writeAll("void");
2640 try bw.writeByte(')');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, .{});
2643 },2643 },
2644 }2644 }
2645}2645}
2646fn renderFields(2646fn renderFields(
2647 zcu: *Zcu,2647 zcu: *Zcu,
2648 bw: *Writer,2648 w: *Writer,
2649 ctype_pool: *const CType.Pool,2649 ctype_pool: *const CType.Pool,
2650 aggregate_info: CType.Info.Aggregate,2650 aggregate_info: CType.Info.Aggregate,
2651 indent: usize,2651 indent: usize,
2652) !void {2652) !void {
2653 try bw.writeAll("{\n");2653 try w.writeAll("{\n");
2654 for (0..aggregate_info.fields.len) |field_index| {2654 for (0..aggregate_info.fields.len) |field_index| {
2655 const field_info = aggregate_info.fields.at(field_index, ctype_pool);2655 const field_info = aggregate_info.fields.at(field_index, ctype_pool);
2656 try bw.splatByteAll(' ', indent + 1);2656 try w.splatByteAll(' ', indent + 1);
2657 switch (field_info.alignas.abiOrder()) {2657 switch (field_info.alignas.abiOrder()) {
2658 .lt => {2658 .lt => {
2659 std.debug.assert(aggregate_info.@"packed");2659 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({}) ", .{
2661 field_info.alignas.toByteUnits(),2661 field_info.alignas.toByteUnits(),
2662 });2662 });
2663 },2663 },
2664 .eq => if (aggregate_info.@"packed" and field_info.alignas.@"align" != .@"1")2664 .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()}),
2666 .gt => {2666 .gt => {
2667 std.debug.assert(field_info.alignas.@"align" != .@"1");2667 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()});
2669 },2669 },
2670 }2670 }
2671 const trailing = try renderTypePrefix(2671 const trailing = try renderTypePrefix(
2672 .flush,2672 .flush,
2673 ctype_pool,2673 ctype_pool,
2674 zcu,2674 zcu,
2675 bw,2675 w,
2676 field_info.ctype,2676 field_info.ctype,
2677 .suffix,2677 .suffix,
2678 .{},2678 .{},
2679 );2679 );
2680 try bw.print("{f}{f }", .{ trailing, fmtCTypePoolString(field_info.name, ctype_pool) });2680 try w.print("{f}{f }", .{ trailing, fmtCTypePoolString(field_info.name, ctype_pool) });
2681 try renderTypeSuffix(.flush, ctype_pool, zcu, bw, field_info.ctype, .suffix, .{});2681 try renderTypeSuffix(.flush, ctype_pool, zcu, w, field_info.ctype, .suffix, .{});
2682 try bw.writeAll(";\n");2682 try w.writeAll(";\n");
2683 }2683 }
2684 try bw.splatByteAll(' ', indent);2684 try w.splatByteAll(' ', indent);
2685 try bw.writeByte('}');2685 try w.writeByte('}');
2686}2686}
26872687
2688pub fn genTypeDecl(2688pub fn genTypeDecl(
2689 zcu: *Zcu,2689 zcu: *Zcu,
2690 bw: *Writer,2690 w: *Writer,
2691 global_ctype_pool: *const CType.Pool,2691 global_ctype_pool: *const CType.Pool,
2692 global_ctype: CType,2692 global_ctype: CType,
2693 pass: DeclGen.Pass,2693 pass: DeclGen.Pass,
...@@ -2700,27 +2700,27 @@ pub fn genTypeDecl(...@@ -2700,27 +2700,27 @@ pub fn genTypeDecl(
2700 .aligned => |aligned_info| {2700 .aligned => |aligned_info| {
2701 if (!found_existing) {2701 if (!found_existing) {
2702 std.debug.assert(aligned_info.alignas.abiOrder().compare(.lt));2702 std.debug.assert(aligned_info.alignas.abiOrder().compare(.lt));
2703 try bw.print("typedef zig_under_align({d}) ", .{aligned_info.alignas.toByteUnits()});2703 try w.print("typedef zig_under_align({d}) ", .{aligned_info.alignas.toByteUnits()});
2704 try bw.print("{f}", .{try renderTypePrefix(2704 try w.print("{f}", .{try renderTypePrefix(
2705 .flush,2705 .flush,
2706 global_ctype_pool,2706 global_ctype_pool,
2707 zcu,2707 zcu,
2708 bw,2708 w,
2709 aligned_info.ctype,2709 aligned_info.ctype,
2710 .suffix,2710 .suffix,
2711 .{},2711 .{},
2712 )});2712 )});
2713 try renderAlignedTypeName(bw, global_ctype);2713 try renderAlignedTypeName(w, global_ctype);
2714 try renderTypeSuffix(.flush, global_ctype_pool, zcu, bw, aligned_info.ctype, .suffix, .{});2714 try renderTypeSuffix(.flush, global_ctype_pool, zcu, w, aligned_info.ctype, .suffix, .{});
2715 try bw.writeAll(";\n");2715 try w.writeAll(";\n");
2716 }2716 }
2717 switch (pass) {2717 switch (pass) {
2718 .nav, .uav => {2718 .nav, .uav => {
2719 try bw.writeAll("typedef ");2719 try w.writeAll("typedef ");
2720 _ = try renderTypePrefix(.flush, global_ctype_pool, zcu, bw, global_ctype, .suffix, .{});2720 _ = try renderTypePrefix(.flush, global_ctype_pool, zcu, w, global_ctype, .suffix, .{});
2721 try bw.writeByte(' ');2721 try w.writeByte(' ');
2722 _ = try renderTypePrefix(pass, decl_ctype_pool, zcu, bw, decl_ctype, .suffix, .{});2722 _ = try renderTypePrefix(pass, decl_ctype_pool, zcu, w, decl_ctype, .suffix, .{});
2723 try bw.writeAll(";\n");2723 try w.writeAll(";\n");
2724 },2724 },
2725 .flush => {},2725 .flush => {},
2726 }2726 }
...@@ -2728,24 +2728,24 @@ pub fn genTypeDecl(...@@ -2728,24 +2728,24 @@ pub fn genTypeDecl(
2728 .fwd_decl => |fwd_decl_info| switch (fwd_decl_info.name) {2728 .fwd_decl => |fwd_decl_info| switch (fwd_decl_info.name) {
2729 .anon => switch (pass) {2729 .anon => switch (pass) {
2730 .nav, .uav => {2730 .nav, .uav => {
2731 try bw.writeAll("typedef ");2731 try w.writeAll("typedef ");
2732 _ = try renderTypePrefix(.flush, global_ctype_pool, zcu, bw, global_ctype, .suffix, .{});2732 _ = try renderTypePrefix(.flush, global_ctype_pool, zcu, w, global_ctype, .suffix, .{});
2733 try bw.writeByte(' ');2733 try w.writeByte(' ');
2734 _ = try renderTypePrefix(pass, decl_ctype_pool, zcu, bw, decl_ctype, .suffix, .{});2734 _ = try renderTypePrefix(pass, decl_ctype_pool, zcu, w, decl_ctype, .suffix, .{});
2735 try bw.writeAll(";\n");2735 try w.writeAll(";\n");
2736 },2736 },
2737 .flush => {},2737 .flush => {},
2738 },2738 },
2739 .index => |index| if (!found_existing) {2739 .index => |index| if (!found_existing) {
2740 const ip = &zcu.intern_pool;2740 const ip = &zcu.intern_pool;
2741 const ty: Type = .fromInterned(index);2741 const ty: Type = .fromInterned(index);
2742 _ = try renderTypePrefix(.flush, global_ctype_pool, zcu, bw, global_ctype, .suffix, .{});2742 _ = try renderTypePrefix(.flush, global_ctype_pool, zcu, w, global_ctype, .suffix, .{});
2743 try bw.writeByte(';');2743 try w.writeByte(';');
2744 const file_scope = ty.typeDeclInstAllowGeneratedTag(zcu).?.resolveFile(ip);2744 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} */", .{
2746 ty.containerTypeName(ip).fmt(ip),2746 ty.containerTypeName(ip).fmt(ip),
2747 });2747 });
2748 try bw.writeByte('\n');2748 try w.writeByte('\n');
2749 },2749 },
2750 },2750 },
2751 .aggregate => |aggregate_info| switch (aggregate_info.name) {2751 .aggregate => |aggregate_info| switch (aggregate_info.name) {
...@@ -2753,23 +2753,23 @@ pub fn genTypeDecl(...@@ -2753,23 +2753,23 @@ pub fn genTypeDecl(
2753 .fwd_decl => |fwd_decl| if (!found_existing) {2753 .fwd_decl => |fwd_decl| if (!found_existing) {
2754 try renderFwdDeclTypeName(2754 try renderFwdDeclTypeName(
2755 zcu,2755 zcu,
2756 bw,2756 w,
2757 fwd_decl,2757 fwd_decl,
2758 fwd_decl.info(global_ctype_pool).fwd_decl,2758 fwd_decl.info(global_ctype_pool).fwd_decl,
2759 if (aggregate_info.@"packed") "zig_packed(" else "",2759 if (aggregate_info.@"packed") "zig_packed(" else "",
2760 );2760 );
2761 try bw.writeByte(' ');2761 try w.writeByte(' ');
2762 try renderFields(zcu, bw, global_ctype_pool, aggregate_info, 0);2762 try renderFields(zcu, w, global_ctype_pool, aggregate_info, 0);
2763 if (aggregate_info.@"packed") try bw.writeByte(')');2763 if (aggregate_info.@"packed") try w.writeByte(')');
2764 try bw.writeAll(";\n");2764 try w.writeAll(";\n");
2765 },2765 },
2766 },2766 },
2767 }2767 }
2768}2768}
27692769
2770pub fn genGlobalAsm(zcu: *Zcu, bw: *Writer) !void {2770pub fn genGlobalAsm(zcu: *Zcu, w: *Writer) !void {
2771 for (zcu.global_assembly.values()) |asm_source| {2771 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)});
2773 }2773 }
2774}2774}
27752775
...@@ -2777,13 +2777,13 @@ pub fn genErrDecls(o: *Object) Error!void {...@@ -2777,13 +2777,13 @@ pub fn genErrDecls(o: *Object) Error!void {
2777 const pt = o.dg.pt;2777 const pt = o.dg.pt;
2778 const zcu = pt.zcu;2778 const zcu = pt.zcu;
2779 const ip = &zcu.intern_pool;2779 const ip = &zcu.intern_pool;
2780 const bw = &o.code.buffered_writer;2780 const w = &o.code.buffered_writer;
27812781
2782 var max_name_len: usize = 0;2782 var max_name_len: usize = 0;
2783 // do not generate an invalid empty enum when the global error set is empty2783 // do not generate an invalid empty enum when the global error set is empty
2784 const names = ip.global_error_set.getNamesFromMainThread();2784 const names = ip.global_error_set.getNamesFromMainThread();
2785 if (names.len > 0) {2785 if (names.len > 0) {
2786 try bw.writeAll("enum {");2786 try w.writeAll("enum {");
2787 o.indent();2787 o.indent();
2788 try o.newline();2788 try o.newline();
2789 for (names, 1..) |name_nts, value| {2789 for (names, 1..) |name_nts, value| {
...@@ -2793,12 +2793,12 @@ pub fn genErrDecls(o: *Object) Error!void {...@@ -2793,12 +2793,12 @@ pub fn genErrDecls(o: *Object) Error!void {
2793 .ty = .anyerror_type,2793 .ty = .anyerror_type,
2794 .name = name_nts,2794 .name = name_nts,
2795 } });2795 } });
2796 try o.dg.renderValue(bw, Value.fromInterned(err_val), .Other);2796 try o.dg.renderValue(w, Value.fromInterned(err_val), .Other);
2797 try bw.print(" = {d}u,", .{value});2797 try w.print(" = {d}u,", .{value});
2798 try o.newline();2798 try o.newline();
2799 }2799 }
2800 try o.outdent();2800 try o.outdent();
2801 try bw.writeAll("};");2801 try w.writeAll("};");
2802 try o.newline();2802 try o.newline();
2803 }2803 }
2804 const array_identifier = "zig_errorName";2804 const array_identifier = "zig_errorName";
...@@ -2822,18 +2822,18 @@ pub fn genErrDecls(o: *Object) Error!void {...@@ -2822,18 +2822,18 @@ pub fn genErrDecls(o: *Object) Error!void {
2822 .storage = .{ .bytes = name.toString() },2822 .storage = .{ .bytes = name.toString() },
2823 } });2823 } });
28242824
2825 try bw.writeAll("static ");2825 try w.writeAll("static ");
2826 try o.dg.renderTypeAndName(2826 try o.dg.renderTypeAndName(
2827 bw,2827 w,
2828 name_ty,2828 name_ty,
2829 .{ .identifier = identifier },2829 .{ .identifier = identifier },
2830 Const,2830 Const,
2831 .none,2831 .none,
2832 .complete,2832 .complete,
2833 );2833 );
2834 try bw.writeAll(" = ");2834 try w.writeAll(" = ");
2835 try o.dg.renderValue(bw, Value.fromInterned(name_val), .StaticInitializer);2835 try o.dg.renderValue(w, Value.fromInterned(name_val), .StaticInitializer);
2836 try bw.writeByte(';');2836 try w.writeByte(';');
2837 try o.newline();2837 try o.newline();
2838 }2838 }
28392839
...@@ -2842,25 +2842,25 @@ pub fn genErrDecls(o: *Object) Error!void {...@@ -2842,25 +2842,25 @@ pub fn genErrDecls(o: *Object) Error!void {
2842 .child = .slice_const_u8_sentinel_0_type,2842 .child = .slice_const_u8_sentinel_0_type,
2843 });2843 });
28442844
2845 try bw.writeAll("static ");2845 try w.writeAll("static ");
2846 try o.dg.renderTypeAndName(2846 try o.dg.renderTypeAndName(
2847 bw,2847 w,
2848 name_array_ty,2848 name_array_ty,
2849 .{ .identifier = array_identifier },2849 .{ .identifier = array_identifier },
2850 Const,2850 Const,
2851 .none,2851 .none,
2852 .complete,2852 .complete,
2853 );2853 );
2854 try bw.writeAll(" = {");2854 try w.writeAll(" = {");
2855 for (names, 1..) |name_nts, val| {2855 for (names, 1..) |name_nts, val| {
2856 const name = name_nts.toSlice(ip);2856 const name = name_nts.toSlice(ip);
2857 if (val > 1) try bw.writeAll(", ");2857 if (val > 1) try w.writeAll(", ");
2858 try bw.print("{{" ++ name_prefix ++ "{f}, {f}}}", .{2858 try w.print("{{" ++ name_prefix ++ "{f}, {f}}}", .{
2859 fmtIdent(name),2859 fmtIdent(name),
2860 try o.dg.fmtIntLiteral(try pt.intValue(.usize, name.len), .StaticInitializer),2860 try o.dg.fmtIntLiteral(try pt.intValue(.usize, name.len), .StaticInitializer),
2861 });2861 });
2862 }2862 }
2863 try bw.writeAll("};");2863 try w.writeAll("};");
2864 try o.newline();2864 try o.newline();
2865}2865}
28662866
...@@ -2869,7 +2869,7 @@ pub fn genLazyFn(o: *Object, lazy_ctype_pool: *const CType.Pool, lazy_fn: LazyFn...@@ -2869,7 +2869,7 @@ pub fn genLazyFn(o: *Object, lazy_ctype_pool: *const CType.Pool, lazy_fn: LazyFn
2869 const zcu = pt.zcu;2869 const zcu = pt.zcu;
2870 const ip = &zcu.intern_pool;2870 const ip = &zcu.intern_pool;
2871 const ctype_pool = &o.dg.ctype_pool;2871 const ctype_pool = &o.dg.ctype_pool;
2872 const bw = &o.code.buffered_writer;2872 const w = &o.code.buffered_writer;
2873 const key = lazy_fn.key_ptr.*;2873 const key = lazy_fn.key_ptr.*;
2874 const val = lazy_fn.value_ptr;2874 const val = lazy_fn.value_ptr;
2875 switch (key) {2875 switch (key) {
...@@ -2877,14 +2877,14 @@ pub fn genLazyFn(o: *Object, lazy_ctype_pool: *const CType.Pool, lazy_fn: LazyFn...@@ -2877,14 +2877,14 @@ pub fn genLazyFn(o: *Object, lazy_ctype_pool: *const CType.Pool, lazy_fn: LazyFn
2877 const enum_ty: Type = .fromInterned(enum_ty_ip);2877 const enum_ty: Type = .fromInterned(enum_ty_ip);
2878 const name_slice_ty: Type = .slice_const_u8_sentinel_0;2878 const name_slice_ty: Type = .slice_const_u8_sentinel_0;
28792879
2880 try bw.writeAll("static ");2880 try w.writeAll("static ");
2881 try o.dg.renderType(bw, name_slice_ty);2881 try o.dg.renderType(w, name_slice_ty);
2882 try bw.print(" {f}(", .{val.fn_name.fmt(lazy_ctype_pool)});2882 try w.print(" {f}(", .{val.fn_name.fmt(lazy_ctype_pool)});
2883 try o.dg.renderTypeAndName(bw, enum_ty, .{ .identifier = "tag" }, Const, .none, .complete);2883 try o.dg.renderTypeAndName(w, enum_ty, .{ .identifier = "tag" }, Const, .none, .complete);
2884 try bw.writeAll(") {");2884 try w.writeAll(") {");
2885 o.indent();2885 o.indent();
2886 try o.newline();2886 try o.newline();
2887 try bw.writeAll("switch (tag) {");2887 try w.writeAll("switch (tag) {");
2888 o.indent();2888 o.indent();
2889 try o.newline();2889 try o.newline();
2890 const tag_names = enum_ty.enumFields(zcu);2890 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...@@ -2903,34 +2903,34 @@ pub fn genLazyFn(o: *Object, lazy_ctype_pool: *const CType.Pool, lazy_fn: LazyFn
2903 .storage = .{ .bytes = tag_name.toString() },2903 .storage = .{ .bytes = tag_name.toString() },
2904 } });2904 } });
29052905
2906 try bw.print("case {f}: {{", .{2906 try w.print("case {f}: {{", .{
2907 try o.dg.fmtIntLiteral(try tag_val.intFromEnum(enum_ty, pt), .Other),2907 try o.dg.fmtIntLiteral(try tag_val.intFromEnum(enum_ty, pt), .Other),
2908 });2908 });
2909 o.indent();2909 o.indent();
2910 try o.newline();2910 try o.newline();
2911 try bw.writeAll("static ");2911 try w.writeAll("static ");
2912 try o.dg.renderTypeAndName(bw, name_ty, .{ .identifier = "name" }, Const, .none, .complete);2912 try o.dg.renderTypeAndName(w, name_ty, .{ .identifier = "name" }, Const, .none, .complete);
2913 try bw.writeAll(" = ");2913 try w.writeAll(" = ");
2914 try o.dg.renderValue(bw, Value.fromInterned(name_val), .StaticInitializer);2914 try o.dg.renderValue(w, Value.fromInterned(name_val), .StaticInitializer);
2915 try bw.writeByte(';');2915 try w.writeByte(';');
2916 try o.newline();2916 try o.newline();
2917 try bw.writeAll("return (");2917 try w.writeAll("return (");
2918 try o.dg.renderType(bw, name_slice_ty);2918 try o.dg.renderType(w, name_slice_ty);
2919 try bw.print("){{{f}, {f}}};", .{2919 try w.print("){{{f}, {f}}};", .{
2920 fmtIdent("name"),2920 fmtIdent("name"),
2921 try o.dg.fmtIntLiteral(try pt.intValue(.usize, tag_name_len), .Other),2921 try o.dg.fmtIntLiteral(try pt.intValue(.usize, tag_name_len), .Other),
2922 });2922 });
2923 try o.newline();2923 try o.newline();
2924 try o.outdent();2924 try o.outdent();
2925 try bw.writeByte('}');2925 try w.writeByte('}');
2926 try o.newline();2926 try o.newline();
2927 }2927 }
2928 try o.outdent();2928 try o.outdent();
2929 try bw.writeByte('}');2929 try w.writeByte('}');
2930 try o.newline();2930 try o.newline();
2931 try airUnreach(o);2931 try airUnreach(o);
2932 try o.outdent();2932 try o.outdent();
2933 try bw.writeByte('}');2933 try w.writeByte('}');
2934 try o.newline();2934 try o.newline();
2935 },2935 },
2936 .never_tail, .never_inline => |fn_nav_index| {2936 .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...@@ -2946,24 +2946,24 @@ pub fn genLazyFn(o: *Object, lazy_ctype_pool: *const CType.Pool, lazy_fn: LazyFn
2946 });2946 });
2947 try fwd.writeAll(";\n");2947 try fwd.writeAll(";\n");
29482948
2949 try bw.print("zig_{s} ", .{@tagName(key)});2949 try w.print("zig_{s} ", .{@tagName(key)});
2950 try o.dg.renderFunctionSignature(bw, fn_val, .none, .complete, .{2950 try o.dg.renderFunctionSignature(w, fn_val, .none, .complete, .{
2951 .fmt_ctype_pool_string = fn_name,2951 .fmt_ctype_pool_string = fn_name,
2952 });2952 });
2953 try bw.writeAll(" {");2953 try w.writeAll(" {");
2954 o.indent();2954 o.indent();
2955 try o.newline();2955 try o.newline();
2956 try bw.writeAll("return ");2956 try w.writeAll("return ");
2957 try o.dg.renderNavName(bw, fn_nav_index);2957 try o.dg.renderNavName(w, fn_nav_index);
2958 try bw.writeByte('(');2958 try w.writeByte('(');
2959 for (0..fn_info.param_ctypes.len) |arg| {2959 for (0..fn_info.param_ctypes.len) |arg| {
2960 if (arg > 0) try bw.writeAll(", ");2960 if (arg > 0) try w.writeAll(", ");
2961 try bw.print("a{d}", .{arg});2961 try w.print("a{d}", .{arg});
2962 }2962 }
2963 try bw.writeAll(");");2963 try w.writeAll(");");
2964 try o.newline();2964 try o.newline();
2965 try o.outdent();2965 try o.outdent();
2966 try bw.writeByte('}');2966 try w.writeByte('}');
2967 try o.newline();2967 try o.newline();
2968 },2968 },
2969 }2969 }
...@@ -3164,21 +3164,21 @@ pub fn genDecl(o: *Object) Error!void {...@@ -3164,21 +3164,21 @@ pub fn genDecl(o: *Object) Error!void {
3164 .linkage = .internal,3164 .linkage = .internal,
3165 .visibility = .default,3165 .visibility = .default,
3166 });3166 });
3167 const bw = &o.code.buffered_writer;3167 const w = &o.code.buffered_writer;
3168 if (variable.is_threadlocal and !o.dg.mod.single_threaded) try bw.writeAll("zig_threadlocal ");3168 if (variable.is_threadlocal and !o.dg.mod.single_threaded) try w.writeAll("zig_threadlocal ");
3169 if (nav.status.fully_resolved.@"linksection".toSlice(&zcu.intern_pool)) |s|3169 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)});
3171 try o.dg.renderTypeAndName(3171 try o.dg.renderTypeAndName(
3172 bw,3172 w,
3173 nav_ty,3173 nav_ty,
3174 .{ .nav = o.dg.pass.nav },3174 .{ .nav = o.dg.pass.nav },
3175 .{},3175 .{},
3176 nav.status.fully_resolved.alignment,3176 nav.status.fully_resolved.alignment,
3177 .complete,3177 .complete,
3178 );3178 );
3179 try bw.writeAll(" = ");3179 try w.writeAll(" = ");
3180 try o.dg.renderValue(bw, Value.fromInterned(variable.init), .StaticInitializer);3180 try o.dg.renderValue(w, Value.fromInterned(variable.init), .StaticInitializer);
3181 try bw.writeByte(';');3181 try w.writeByte(';');
3182 try o.newline();3182 try o.newline();
3183 },3183 },
3184 else => try genDeclValue(3184 else => try genDeclValue(
...@@ -3206,13 +3206,13 @@ pub fn genDeclValue(...@@ -3206,13 +3206,13 @@ pub fn genDeclValue(
3206 try o.dg.renderTypeAndName(fwd, ty, decl_c_value, Const, alignment, .complete);3206 try o.dg.renderTypeAndName(fwd, ty, decl_c_value, Const, alignment, .complete);
3207 try fwd.writeAll(";\n");3207 try fwd.writeAll(";\n");
32083208
3209 const bw = &o.code.buffered_writer;3209 const w = &o.code.buffered_writer;
3210 if (@"linksection".toSlice(&zcu.intern_pool)) |s|3210 if (@"linksection".toSlice(&zcu.intern_pool)) |s|
3211 try bw.print("zig_linksection({fs}) ", .{fmtStringLiteral(s, null)});3211 try w.print("zig_linksection({fs}) ", .{fmtStringLiteral(s, null)});
3212 try o.dg.renderTypeAndName(bw, ty, decl_c_value, Const, alignment, .complete);3212 try o.dg.renderTypeAndName(w, ty, decl_c_value, Const, alignment, .complete);
3213 try bw.writeAll(" = ");3213 try w.writeAll(" = ");
3214 try o.dg.renderValue(bw, val, .StaticInitializer);3214 try o.dg.renderValue(w, val, .StaticInitializer);
3215 try bw.writeByte(';');3215 try w.writeByte(';');
3216 try o.newline();3216 try o.newline();
3217}3217}
32183218
...@@ -3297,16 +3297,16 @@ pub fn genExports(dg: *DeclGen, exported: Zcu.Exported, export_indices: []const...@@ -3297,16 +3297,16 @@ pub fn genExports(dg: *DeclGen, exported: Zcu.Exported, export_indices: []const
3297/// have been added to `free_locals_map`. For a version of this function that restores this state,3297/// have been added to `free_locals_map`. For a version of this function that restores this state,
3298/// see `genBodyResolveState`.3298/// see `genBodyResolveState`.
3299fn genBody(f: *Function, body: []const Air.Inst.Index) Error!void {3299fn 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;
3301 if (body.len == 0) {3301 if (body.len == 0) {
3302 try bw.writeAll("{}");3302 try w.writeAll("{}");
3303 } else {3303 } else {
3304 try bw.writeByte('{');3304 try w.writeByte('{');
3305 f.object.indent();3305 f.object.indent();
3306 try f.object.newline();3306 try f.object.newline();
3307 try genBodyInner(f, body);3307 try genBodyInner(f, body);
3308 try f.object.outdent();3308 try f.object.outdent();
3309 try bw.writeByte('}');3309 try w.writeByte('}');
3310 }3310 }
3311}3311}
33123312
...@@ -3680,16 +3680,16 @@ fn airSliceField(f: *Function, inst: Air.Inst.Index, is_ptr: bool, field_name: [...@@ -3680,16 +3680,16 @@ fn airSliceField(f: *Function, inst: Air.Inst.Index, is_ptr: bool, field_name: [
3680 const operand = try f.resolveInst(ty_op.operand);3680 const operand = try f.resolveInst(ty_op.operand);
3681 try reap(f, inst, &.{ty_op.operand});3681 try reap(f, inst, &.{ty_op.operand});
36823682
3683 const bw = &f.object.code.buffered_writer;3683 const w = &f.object.code.buffered_writer;
3684 const local = try f.allocLocal(inst, inst_ty);3684 const local = try f.allocLocal(inst, inst_ty);
3685 const a = try Assignment.start(f, bw, try f.ctypeFromType(inst_ty, .complete));3685 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete));
3686 try f.writeCValue(bw, local, .Other);3686 try f.writeCValue(w, local, .Other);
3687 try a.assign(f, bw);3687 try a.assign(f, w);
3688 if (is_ptr) {3688 if (is_ptr) {
3689 try bw.writeByte('&');3689 try w.writeByte('&');
3690 try f.writeCValueDerefMember(bw, operand, .{ .identifier = field_name });3690 try f.writeCValueDerefMember(w, operand, .{ .identifier = field_name });
3691 } else try f.writeCValueMember(bw, operand, .{ .identifier = field_name });3691 } else try f.writeCValueMember(w, operand, .{ .identifier = field_name });
3692 try a.end(f, bw);3692 try a.end(f, w);
3693 return local;3693 return local;
3694}3694}
36953695
...@@ -3706,16 +3706,16 @@ fn airPtrElemVal(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -3706,16 +3706,16 @@ fn airPtrElemVal(f: *Function, inst: Air.Inst.Index) !CValue {
3706 const index = try f.resolveInst(bin_op.rhs);3706 const index = try f.resolveInst(bin_op.rhs);
3707 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });3707 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;
3710 const local = try f.allocLocal(inst, inst_ty);3710 const local = try f.allocLocal(inst, inst_ty);
3711 const a = try Assignment.start(f, bw, try f.ctypeFromType(inst_ty, .complete));3711 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete));
3712 try f.writeCValue(bw, local, .Other);3712 try f.writeCValue(w, local, .Other);
3713 try a.assign(f, bw);3713 try a.assign(f, w);
3714 try f.writeCValue(bw, ptr, .Other);3714 try f.writeCValue(w, ptr, .Other);
3715 try bw.writeByte('[');3715 try w.writeByte('[');
3716 try f.writeCValue(bw, index, .Other);3716 try f.writeCValue(w, index, .Other);
3717 try bw.writeByte(']');3717 try w.writeByte(']');
3718 try a.end(f, bw);3718 try a.end(f, w);
3719 return local;3719 return local;
3720}3720}
37213721
...@@ -3733,25 +3733,25 @@ fn airPtrElemPtr(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -3733,25 +3733,25 @@ fn airPtrElemPtr(f: *Function, inst: Air.Inst.Index) !CValue {
3733 const index = try f.resolveInst(bin_op.rhs);3733 const index = try f.resolveInst(bin_op.rhs);
3734 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });3734 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;
3737 const local = try f.allocLocal(inst, inst_ty);3737 const local = try f.allocLocal(inst, inst_ty);
3738 const a = try Assignment.start(f, bw, try f.ctypeFromType(inst_ty, .complete));3738 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete));
3739 try f.writeCValue(bw, local, .Other);3739 try f.writeCValue(w, local, .Other);
3740 try a.assign(f, bw);3740 try a.assign(f, w);
3741 try bw.writeByte('(');3741 try w.writeByte('(');
3742 try f.renderType(bw, inst_ty);3742 try f.renderType(w, inst_ty);
3743 try bw.writeByte(')');3743 try w.writeByte(')');
3744 if (elem_has_bits) try bw.writeByte('&');3744 if (elem_has_bits) try w.writeByte('&');
3745 if (elem_has_bits and ptr_ty.ptrSize(zcu) == .one) {3745 if (elem_has_bits and ptr_ty.ptrSize(zcu) == .one) {
3746 // It's a pointer to an array, so we need to de-reference.3746 // It's a pointer to an array, so we need to de-reference.
3747 try f.writeCValueDeref(bw, ptr);3747 try f.writeCValueDeref(w, ptr);
3748 } else try f.writeCValue(bw, ptr, .Other);3748 } else try f.writeCValue(w, ptr, .Other);
3749 if (elem_has_bits) {3749 if (elem_has_bits) {
3750 try bw.writeByte('[');3750 try w.writeByte('[');
3751 try f.writeCValue(bw, index, .Other);3751 try f.writeCValue(w, index, .Other);
3752 try bw.writeByte(']');3752 try w.writeByte(']');
3753 }3753 }
3754 try a.end(f, bw);3754 try a.end(f, w);
3755 return local;3755 return local;
3756}3756}
37573757
...@@ -3768,16 +3768,16 @@ fn airSliceElemVal(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -3768,16 +3768,16 @@ fn airSliceElemVal(f: *Function, inst: Air.Inst.Index) !CValue {
3768 const index = try f.resolveInst(bin_op.rhs);3768 const index = try f.resolveInst(bin_op.rhs);
3769 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });3769 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;
3772 const local = try f.allocLocal(inst, inst_ty);3772 const local = try f.allocLocal(inst, inst_ty);
3773 const a = try Assignment.start(f, bw, try f.ctypeFromType(inst_ty, .complete));3773 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete));
3774 try f.writeCValue(bw, local, .Other);3774 try f.writeCValue(w, local, .Other);
3775 try a.assign(f, bw);3775 try a.assign(f, w);
3776 try f.writeCValueMember(bw, slice, .{ .identifier = "ptr" });3776 try f.writeCValueMember(w, slice, .{ .identifier = "ptr" });
3777 try bw.writeByte('[');3777 try w.writeByte('[');
3778 try f.writeCValue(bw, index, .Other);3778 try f.writeCValue(w, index, .Other);
3779 try bw.writeByte(']');3779 try w.writeByte(']');
3780 try a.end(f, bw);3780 try a.end(f, w);
3781 return local;3781 return local;
3782}3782}
37833783
...@@ -3796,19 +3796,19 @@ fn airSliceElemPtr(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -3796,19 +3796,19 @@ fn airSliceElemPtr(f: *Function, inst: Air.Inst.Index) !CValue {
3796 const index = try f.resolveInst(bin_op.rhs);3796 const index = try f.resolveInst(bin_op.rhs);
3797 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });3797 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;
3800 const local = try f.allocLocal(inst, inst_ty);3800 const local = try f.allocLocal(inst, inst_ty);
3801 const a = try Assignment.start(f, bw, try f.ctypeFromType(inst_ty, .complete));3801 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete));
3802 try f.writeCValue(bw, local, .Other);3802 try f.writeCValue(w, local, .Other);
3803 try a.assign(f, bw);3803 try a.assign(f, w);
3804 if (elem_has_bits) try bw.writeByte('&');3804 if (elem_has_bits) try w.writeByte('&');
3805 try f.writeCValueMember(bw, slice, .{ .identifier = "ptr" });3805 try f.writeCValueMember(w, slice, .{ .identifier = "ptr" });
3806 if (elem_has_bits) {3806 if (elem_has_bits) {
3807 try bw.writeByte('[');3807 try w.writeByte('[');
3808 try f.writeCValue(bw, index, .Other);3808 try f.writeCValue(w, index, .Other);
3809 try bw.writeByte(']');3809 try w.writeByte(']');
3810 }3810 }
3811 try a.end(f, bw);3811 try a.end(f, w);
3812 return local;3812 return local;
3813}3813}
38143814
...@@ -3825,16 +3825,16 @@ fn airArrayElemVal(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -3825,16 +3825,16 @@ fn airArrayElemVal(f: *Function, inst: Air.Inst.Index) !CValue {
3825 const index = try f.resolveInst(bin_op.rhs);3825 const index = try f.resolveInst(bin_op.rhs);
3826 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });3826 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;
3829 const local = try f.allocLocal(inst, inst_ty);3829 const local = try f.allocLocal(inst, inst_ty);
3830 const a = try Assignment.start(f, bw, try f.ctypeFromType(inst_ty, .complete));3830 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete));
3831 try f.writeCValue(bw, local, .Other);3831 try f.writeCValue(w, local, .Other);
3832 try a.assign(f, bw);3832 try a.assign(f, w);
3833 try f.writeCValue(bw, array, .Other);3833 try f.writeCValue(w, array, .Other);
3834 try bw.writeByte('[');3834 try w.writeByte('[');
3835 try f.writeCValue(bw, index, .Other);3835 try f.writeCValue(w, index, .Other);
3836 try bw.writeByte(']');3836 try w.writeByte(']');
3837 try a.end(f, bw);3837 try a.end(f, w);
3838 return local;3838 return local;
3839}3839}
38403840
...@@ -3888,12 +3888,12 @@ fn airArg(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -3888,12 +3888,12 @@ fn airArg(f: *Function, inst: Air.Inst.Index) !CValue {
3888 .{ .arg_array = i };3888 .{ .arg_array = i };
38893889
3890 if (f.liveness.isUnused(inst)) {3890 if (f.liveness.isUnused(inst)) {
3891 const bw = &f.object.code.buffered_writer;3891 const w = &f.object.code.buffered_writer;
3892 try bw.writeByte('(');3892 try w.writeByte('(');
3893 try f.renderType(bw, .void);3893 try f.renderType(w, .void);
3894 try bw.writeByte(')');3894 try w.writeByte(')');
3895 try f.writeCValue(bw, result, .Other);3895 try f.writeCValue(w, result, .Other);
3896 try bw.writeByte(';');3896 try w.writeByte(';');
3897 try f.object.newline();3897 try f.object.newline();
3898 return .none;3898 return .none;
3899 }3899 }
...@@ -3927,21 +3927,21 @@ fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -3927,21 +3927,21 @@ fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue {
3927 const is_array = lowersToArray(src_ty, pt);3927 const is_array = lowersToArray(src_ty, pt);
3928 const need_memcpy = !is_aligned or is_array;3928 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;
3931 const local = try f.allocLocal(inst, src_ty);3931 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
3934 if (need_memcpy) {3934 if (need_memcpy) {
3935 try bw.writeAll("memcpy(");3935 try w.writeAll("memcpy(");
3936 if (!is_array) try bw.writeByte('&');3936 if (!is_array) try w.writeByte('&');
3937 try f.writeCValue(bw, local, .Other);3937 try f.writeCValue(w, local, .Other);
3938 try v.elem(f, bw);3938 try v.elem(f, w);
3939 try bw.writeAll(", (const char *)");3939 try w.writeAll(", (const char *)");
3940 try f.writeCValue(bw, operand, .Other);3940 try f.writeCValue(w, operand, .Other);
3941 try v.elem(f, bw);3941 try v.elem(f, w);
3942 try bw.writeAll(", sizeof(");3942 try w.writeAll(", sizeof(");
3943 try f.renderType(bw, src_ty);3943 try f.renderType(w, src_ty);
3944 try bw.writeAll("))");3944 try w.writeAll("))");
3945 } else if (ptr_info.packed_offset.host_size > 0 and ptr_info.flags.vector_index == .none) {3945 } else if (ptr_info.packed_offset.host_size > 0 and ptr_info.flags.vector_index == .none) {
3946 const host_bits: u16 = ptr_info.packed_offset.host_size * 8;3946 const host_bits: u16 = ptr_info.packed_offset.host_size * 8;
3947 const host_ty = try pt.intType(.unsigned, host_bits);3947 const host_ty = try pt.intType(.unsigned, host_bits);
...@@ -3951,41 +3951,41 @@ fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -3951,41 +3951,41 @@ fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue {
39513951
3952 const field_ty = try pt.intType(.unsigned, @as(u16, @intCast(src_ty.bitSize(zcu))));3952 const field_ty = try pt.intType(.unsigned, @as(u16, @intCast(src_ty.bitSize(zcu))));
39533953
3954 try f.writeCValue(bw, local, .Other);3954 try f.writeCValue(w, local, .Other);
3955 try v.elem(f, bw);3955 try v.elem(f, w);
3956 try bw.writeAll(" = (");3956 try w.writeAll(" = (");
3957 try f.renderType(bw, src_ty);3957 try f.renderType(w, src_ty);
3958 try bw.writeAll(")zig_wrap_");3958 try w.writeAll(")zig_wrap_");
3959 try f.object.dg.renderTypeForBuiltinFnName(bw, field_ty);3959 try f.object.dg.renderTypeForBuiltinFnName(w, field_ty);
3960 try bw.writeAll("((");3960 try w.writeAll("((");
3961 try f.renderType(bw, field_ty);3961 try f.renderType(w, field_ty);
3962 try bw.writeByte(')');3962 try w.writeByte(')');
3963 const cant_cast = host_ty.isInt(zcu) and host_ty.bitSize(zcu) > 64;3963 const cant_cast = host_ty.isInt(zcu) and host_ty.bitSize(zcu) > 64;
3964 if (cant_cast) {3964 if (cant_cast) {
3965 if (field_ty.bitSize(zcu) > 64) return f.fail("TODO: C backend: implement casting between types > 64 bits", .{});3965 if (field_ty.bitSize(zcu) > 64) return f.fail("TODO: C backend: implement casting between types > 64 bits", .{});
3966 try bw.writeAll("zig_lo_");3966 try w.writeAll("zig_lo_");
3967 try f.object.dg.renderTypeForBuiltinFnName(bw, host_ty);3967 try f.object.dg.renderTypeForBuiltinFnName(w, host_ty);
3968 try bw.writeByte('(');3968 try w.writeByte('(');
3969 }3969 }
3970 try bw.writeAll("zig_shr_");3970 try w.writeAll("zig_shr_");
3971 try f.object.dg.renderTypeForBuiltinFnName(bw, host_ty);3971 try f.object.dg.renderTypeForBuiltinFnName(w, host_ty);
3972 try bw.writeByte('(');3972 try w.writeByte('(');
3973 try f.writeCValueDeref(bw, operand);3973 try f.writeCValueDeref(w, operand);
3974 try v.elem(f, bw);3974 try v.elem(f, w);
3975 try bw.print(", {f})", .{try f.fmtIntLiteral(bit_offset_val)});3975 try w.print(", {f})", .{try f.fmtIntLiteral(bit_offset_val)});
3976 if (cant_cast) try bw.writeByte(')');3976 if (cant_cast) try w.writeByte(')');
3977 try f.object.dg.renderBuiltinInfo(bw, field_ty, .bits);3977 try f.object.dg.renderBuiltinInfo(w, field_ty, .bits);
3978 try bw.writeByte(')');3978 try w.writeByte(')');
3979 } else {3979 } else {
3980 try f.writeCValue(bw, local, .Other);3980 try f.writeCValue(w, local, .Other);
3981 try v.elem(f, bw);3981 try v.elem(f, w);
3982 try bw.writeAll(" = ");3982 try w.writeAll(" = ");
3983 try f.writeCValueDeref(bw, operand);3983 try f.writeCValueDeref(w, operand);
3984 try v.elem(f, bw);3984 try v.elem(f, w);
3985 }3985 }
3986 try bw.writeByte(';');3986 try w.writeByte(';');
3987 try f.object.newline();3987 try f.object.newline();
3988 try v.end(f, inst, bw);3988 try v.end(f, inst, w);
39893989
3990 return local;3990 return local;
3991}3991}
...@@ -3994,7 +3994,7 @@ fn airRet(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !void {...@@ -3994,7 +3994,7 @@ fn airRet(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !void {
3994 const pt = f.object.dg.pt;3994 const pt = f.object.dg.pt;
3995 const zcu = pt.zcu;3995 const zcu = pt.zcu;
3996 const un_op = f.air.instructions.items(.data)[@intFromEnum(inst)].un_op;3996 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;
3998 const op_inst = un_op.toIndex();3998 const op_inst = un_op.toIndex();
3999 const op_ty = f.typeOf(un_op);3999 const op_ty = f.typeOf(un_op);
4000 const ret_ty = if (is_ptr) op_ty.childType(zcu) else op_ty;4000 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 {...@@ -4013,34 +4013,34 @@ fn airRet(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !void {
4013 .ctype = ret_ctype,4013 .ctype = ret_ctype,
4014 .alignas = CType.AlignAs.fromAbiAlignment(ret_ty.abiAlignment(zcu)),4014 .alignas = CType.AlignAs.fromAbiAlignment(ret_ty.abiAlignment(zcu)),
4015 });4015 });
4016 try bw.writeAll("memcpy(");4016 try w.writeAll("memcpy(");
4017 try f.writeCValueMember(bw, array_local, .{ .identifier = "array" });4017 try f.writeCValueMember(w, array_local, .{ .identifier = "array" });
4018 try bw.writeAll(", ");4018 try w.writeAll(", ");
4019 if (deref)4019 if (deref)
4020 try f.writeCValueDeref(bw, operand)4020 try f.writeCValueDeref(w, operand)
4021 else4021 else
4022 try f.writeCValue(bw, operand, .FunctionArgument);4022 try f.writeCValue(w, operand, .FunctionArgument);
4023 deref = false;4023 deref = false;
4024 try bw.writeAll(", sizeof(");4024 try w.writeAll(", sizeof(");
4025 try f.renderType(bw, ret_ty);4025 try f.renderType(w, ret_ty);
4026 try bw.writeAll("));");4026 try w.writeAll("));");
4027 try f.object.newline();4027 try f.object.newline();
4028 break :ret_val array_local;4028 break :ret_val array_local;
4029 } else operand;4029 } else operand;
40304030
4031 try bw.writeAll("return ");4031 try w.writeAll("return ");
4032 if (deref)4032 if (deref)
4033 try f.writeCValueDeref(bw, ret_val)4033 try f.writeCValueDeref(w, ret_val)
4034 else4034 else
4035 try f.writeCValue(bw, ret_val, .Other);4035 try f.writeCValue(w, ret_val, .Other);
4036 try bw.writeAll(";\n");4036 try w.writeAll(";\n");
4037 if (is_array) {4037 if (is_array) {
4038 try freeLocal(f, inst, ret_val.new_local, null);4038 try freeLocal(f, inst, ret_val.new_local, null);
4039 }4039 }
4040 } else {4040 } else {
4041 try reap(f, inst, &.{un_op});4041 try reap(f, inst, &.{un_op});
4042 // Not even allowed to return void in a naked function.4042 // 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");
4044 }4044 }
4045}4045}
40464046
...@@ -4059,16 +4059,16 @@ fn airIntCast(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -4059,16 +4059,16 @@ fn airIntCast(f: *Function, inst: Air.Inst.Index) !CValue {
40594059
4060 if (f.object.dg.intCastIsNoop(inst_scalar_ty, scalar_ty)) return f.moveCValue(inst, inst_ty, operand);4060 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;
4063 const local = try f.allocLocal(inst, inst_ty);4063 const local = try f.allocLocal(inst, inst_ty);
4064 const v = try Vectorize.start(f, inst, bw, operand_ty);4064 const v = try Vectorize.start(f, inst, w, operand_ty);
4065 const a = try Assignment.start(f, bw, try f.ctypeFromType(scalar_ty, .complete));4065 const a = try Assignment.start(f, w, try f.ctypeFromType(scalar_ty, .complete));
4066 try f.writeCValue(bw, local, .Other);4066 try f.writeCValue(w, local, .Other);
4067 try v.elem(f, bw);4067 try v.elem(f, w);
4068 try a.assign(f, bw);4068 try a.assign(f, w);
4069 try f.renderIntCast(bw, inst_scalar_ty, operand, v, scalar_ty, .Other);4069 try f.renderIntCast(w, inst_scalar_ty, operand, v, scalar_ty, .Other);
4070 try a.end(f, bw);4070 try a.end(f, w);
4071 try v.end(f, inst, bw);4071 try v.end(f, inst, w);
4072 return local;4072 return local;
4073}4073}
40744074
...@@ -4095,34 +4095,34 @@ fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -4095,34 +4095,34 @@ fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue {
4095 const need_mask = dest_bits < 8 or !std.math.isPowerOfTwo(dest_bits);4095 const need_mask = dest_bits < 8 or !std.math.isPowerOfTwo(dest_bits);
4096 if (!need_cast and !need_lo and !need_mask) return f.moveCValue(inst, inst_ty, operand);4096 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;
4099 const local = try f.allocLocal(inst, inst_ty);4099 const local = try f.allocLocal(inst, inst_ty);
4100 const v = try Vectorize.start(f, inst, bw, operand_ty);4100 const v = try Vectorize.start(f, inst, w, operand_ty);
4101 const a = try Assignment.start(f, bw, try f.ctypeFromType(inst_scalar_ty, .complete));4101 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_scalar_ty, .complete));
4102 try f.writeCValue(bw, local, .Other);4102 try f.writeCValue(w, local, .Other);
4103 try v.elem(f, bw);4103 try v.elem(f, w);
4104 try a.assign(f, bw);4104 try a.assign(f, w);
4105 if (need_cast) {4105 if (need_cast) {
4106 try bw.writeByte('(');4106 try w.writeByte('(');
4107 try f.renderType(bw, inst_scalar_ty);4107 try f.renderType(w, inst_scalar_ty);
4108 try bw.writeByte(')');4108 try w.writeByte(')');
4109 }4109 }
4110 if (need_lo) {4110 if (need_lo) {
4111 try bw.writeAll("zig_lo_");4111 try w.writeAll("zig_lo_");
4112 try f.object.dg.renderTypeForBuiltinFnName(bw, scalar_ty);4112 try f.object.dg.renderTypeForBuiltinFnName(w, scalar_ty);
4113 try bw.writeByte('(');4113 try w.writeByte('(');
4114 }4114 }
4115 if (!need_mask) {4115 if (!need_mask) {
4116 try f.writeCValue(bw, operand, .Other);4116 try f.writeCValue(w, operand, .Other);
4117 try v.elem(f, bw);4117 try v.elem(f, w);
4118 } else switch (dest_int_info.signedness) {4118 } else switch (dest_int_info.signedness) {
4119 .unsigned => {4119 .unsigned => {
4120 try bw.writeAll("zig_and_");4120 try w.writeAll("zig_and_");
4121 try f.object.dg.renderTypeForBuiltinFnName(bw, scalar_ty);4121 try f.object.dg.renderTypeForBuiltinFnName(w, scalar_ty);
4122 try bw.writeByte('(');4122 try w.writeByte('(');
4123 try f.writeCValue(bw, operand, .FunctionArgument);4123 try f.writeCValue(w, operand, .FunctionArgument);
4124 try v.elem(f, bw);4124 try v.elem(f, w);
4125 try bw.print(", {fx})", .{4125 try w.print(", {fx})", .{
4126 try f.fmtIntLiteral(try inst_scalar_ty.maxIntScalar(pt, scalar_ty)),4126 try f.fmtIntLiteral(try inst_scalar_ty.maxIntScalar(pt, scalar_ty)),
4127 });4127 });
4128 },4128 },
...@@ -4131,30 +4131,30 @@ fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -4131,30 +4131,30 @@ fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue {
4131 return f.fail("TODO: C backend: implement integer types larger than 128 bits", .{});4131 return f.fail("TODO: C backend: implement integer types larger than 128 bits", .{});
4132 const shift_val = try pt.intValue(.u8, c_bits - dest_bits);4132 const shift_val = try pt.intValue(.u8, c_bits - dest_bits);
41334133
4134 try bw.writeAll("zig_shr_");4134 try w.writeAll("zig_shr_");
4135 try f.object.dg.renderTypeForBuiltinFnName(bw, scalar_ty);4135 try f.object.dg.renderTypeForBuiltinFnName(w, scalar_ty);
4136 if (c_bits == 128) {4136 if (c_bits == 128) {
4137 try bw.print("(zig_bitCast_i{d}(", .{c_bits});4137 try w.print("(zig_bitCast_i{d}(", .{c_bits});
4138 } else {4138 } else {
4139 try bw.print("((int{d}_t)", .{c_bits});4139 try w.print("((int{d}_t)", .{c_bits});
4140 }4140 }
4141 try bw.print("zig_shl_u{d}(", .{c_bits});4141 try w.print("zig_shl_u{d}(", .{c_bits});
4142 if (c_bits == 128) {4142 if (c_bits == 128) {
4143 try bw.print("zig_bitCast_u{d}(", .{c_bits});4143 try w.print("zig_bitCast_u{d}(", .{c_bits});
4144 } else {4144 } else {
4145 try bw.print("(uint{d}_t)", .{c_bits});4145 try w.print("(uint{d}_t)", .{c_bits});
4146 }4146 }
4147 try f.writeCValue(bw, operand, .FunctionArgument);4147 try f.writeCValue(w, operand, .FunctionArgument);
4148 try v.elem(f, bw);4148 try v.elem(f, w);
4149 if (c_bits == 128) try bw.writeByte(')');4149 if (c_bits == 128) try w.writeByte(')');
4150 try bw.print(", {f})", .{try f.fmtIntLiteral(shift_val)});4150 try w.print(", {f})", .{try f.fmtIntLiteral(shift_val)});
4151 if (c_bits == 128) try bw.writeByte(')');4151 if (c_bits == 128) try w.writeByte(')');
4152 try bw.print(", {f})", .{try f.fmtIntLiteral(shift_val)});4152 try w.print(", {f})", .{try f.fmtIntLiteral(shift_val)});
4153 },4153 },
4154 }4154 }
4155 if (need_lo) try bw.writeByte(')');4155 if (need_lo) try w.writeByte(')');
4156 try a.end(f, bw);4156 try a.end(f, w);
4157 try v.end(f, inst, bw);4157 try v.end(f, inst, w);
4158 return local;4158 return local;
4159}4159}
41604160
...@@ -4173,15 +4173,15 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {...@@ -4173,15 +4173,15 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
41734173
4174 const val_is_undef = if (try f.air.value(bin_op.rhs, pt)) |v| v.isUndefDeep(zcu) else false;4174 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;
4177 if (val_is_undef) {4177 if (val_is_undef) {
4178 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });4178 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });
4179 if (safety and ptr_info.packed_offset.host_size == 0) {4179 if (safety and ptr_info.packed_offset.host_size == 0) {
4180 try bw.writeAll("memset(");4180 try w.writeAll("memset(");
4181 try f.writeCValue(bw, ptr_val, .FunctionArgument);4181 try f.writeCValue(w, ptr_val, .FunctionArgument);
4182 try bw.writeAll(", 0xaa, sizeof(");4182 try w.writeAll(", 0xaa, sizeof(");
4183 try f.renderType(bw, .fromInterned(ptr_info.child));4183 try f.renderType(w, .fromInterned(ptr_info.child));
4184 try bw.writeAll("));");4184 try w.writeAll("));");
4185 try f.object.newline();4185 try f.object.newline();
4186 }4186 }
4187 return .none;4187 return .none;
...@@ -4208,30 +4208,30 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {...@@ -4208,30 +4208,30 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
4208 // TODO this should be done by manually initializing elements of the dest array4208 // TODO this should be done by manually initializing elements of the dest array
4209 const array_src = if (src_val == .constant) blk: {4209 const array_src = if (src_val == .constant) blk: {
4210 const new_local = try f.allocLocal(inst, src_ty);4210 const new_local = try f.allocLocal(inst, src_ty);
4211 try f.writeCValue(bw, new_local, .Other);4211 try f.writeCValue(w, new_local, .Other);
4212 try bw.writeAll(" = ");4212 try w.writeAll(" = ");
4213 try f.writeCValue(bw, src_val, .Other);4213 try f.writeCValue(w, src_val, .Other);
4214 try bw.writeByte(';');4214 try w.writeByte(';');
4215 try f.object.newline();4215 try f.object.newline();
42164216
4217 break :blk new_local;4217 break :blk new_local;
4218 } else src_val;4218 } else src_val;
42194219
4220 const v = try Vectorize.start(f, inst, bw, ptr_ty);4220 const v = try Vectorize.start(f, inst, w, ptr_ty);
4221 try bw.writeAll("memcpy((char *)");4221 try w.writeAll("memcpy((char *)");
4222 try f.writeCValue(bw, ptr_val, .FunctionArgument);4222 try f.writeCValue(w, ptr_val, .FunctionArgument);
4223 try v.elem(f, bw);4223 try v.elem(f, w);
4224 try bw.writeAll(", ");4224 try w.writeAll(", ");
4225 if (!is_array) try bw.writeByte('&');4225 if (!is_array) try w.writeByte('&');
4226 try f.writeCValue(bw, array_src, .FunctionArgument);4226 try f.writeCValue(w, array_src, .FunctionArgument);
4227 try v.elem(f, bw);4227 try v.elem(f, w);
4228 try bw.writeAll(", sizeof(");4228 try w.writeAll(", sizeof(");
4229 try f.renderType(bw, src_ty);4229 try f.renderType(w, src_ty);
4230 try bw.writeAll("))");4230 try w.writeAll("))");
4231 try f.freeCValue(inst, array_src);4231 try f.freeCValue(inst, array_src);
4232 try bw.writeByte(';');4232 try w.writeByte(';');
4233 try f.object.newline();4233 try f.object.newline();
4234 try v.end(f, inst, bw);4234 try v.end(f, inst, w);
4235 } else if (ptr_info.packed_offset.host_size > 0 and ptr_info.flags.vector_index == .none) {4235 } else if (ptr_info.packed_offset.host_size > 0 and ptr_info.flags.vector_index == .none) {
4236 const host_bits = ptr_info.packed_offset.host_size * 8;4236 const host_bits = ptr_info.packed_offset.host_size * 8;
4237 const host_ty = try pt.intType(.unsigned, host_bits);4237 const host_ty = try pt.intType(.unsigned, host_bits);
...@@ -4254,44 +4254,44 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {...@@ -4254,44 +4254,44 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
42544254
4255 const mask_val = try pt.intValue_big(host_ty, mask.toConst());4255 const mask_val = try pt.intValue_big(host_ty, mask.toConst());
42564256
4257 const v = try Vectorize.start(f, inst, bw, ptr_ty);4257 const v = try Vectorize.start(f, inst, w, ptr_ty);
4258 const a = try Assignment.start(f, bw, src_scalar_ctype);4258 const a = try Assignment.start(f, w, src_scalar_ctype);
4259 try f.writeCValueDeref(bw, ptr_val);4259 try f.writeCValueDeref(w, ptr_val);
4260 try v.elem(f, bw);4260 try v.elem(f, w);
4261 try a.assign(f, bw);4261 try a.assign(f, w);
4262 try bw.writeAll("zig_or_");4262 try w.writeAll("zig_or_");
4263 try f.object.dg.renderTypeForBuiltinFnName(bw, host_ty);4263 try f.object.dg.renderTypeForBuiltinFnName(w, host_ty);
4264 try bw.writeAll("(zig_and_");4264 try w.writeAll("(zig_and_");
4265 try f.object.dg.renderTypeForBuiltinFnName(bw, host_ty);4265 try f.object.dg.renderTypeForBuiltinFnName(w, host_ty);
4266 try bw.writeByte('(');4266 try w.writeByte('(');
4267 try f.writeCValueDeref(bw, ptr_val);4267 try f.writeCValueDeref(w, ptr_val);
4268 try v.elem(f, bw);4268 try v.elem(f, w);
4269 try bw.print(", {fx}), zig_shl_", .{try f.fmtIntLiteral(mask_val)});4269 try w.print(", {fx}), zig_shl_", .{try f.fmtIntLiteral(mask_val)});
4270 try f.object.dg.renderTypeForBuiltinFnName(bw, host_ty);4270 try f.object.dg.renderTypeForBuiltinFnName(w, host_ty);
4271 try bw.writeByte('(');4271 try w.writeByte('(');
4272 const cant_cast = host_ty.isInt(zcu) and host_ty.bitSize(zcu) > 64;4272 const cant_cast = host_ty.isInt(zcu) and host_ty.bitSize(zcu) > 64;
4273 if (cant_cast) {4273 if (cant_cast) {
4274 if (src_ty.bitSize(zcu) > 64) return f.fail("TODO: C backend: implement casting between types > 64 bits", .{});4274 if (src_ty.bitSize(zcu) > 64) return f.fail("TODO: C backend: implement casting between types > 64 bits", .{});
4275 try bw.writeAll("zig_make_");4275 try w.writeAll("zig_make_");
4276 try f.object.dg.renderTypeForBuiltinFnName(bw, host_ty);4276 try f.object.dg.renderTypeForBuiltinFnName(w, host_ty);
4277 try bw.writeAll("(0, ");4277 try w.writeAll("(0, ");
4278 } else {4278 } else {
4279 try bw.writeByte('(');4279 try w.writeByte('(');
4280 try f.renderType(bw, host_ty);4280 try f.renderType(w, host_ty);
4281 try bw.writeByte(')');4281 try w.writeByte(')');
4282 }4282 }
42834283
4284 if (src_ty.isPtrAtRuntime(zcu)) {4284 if (src_ty.isPtrAtRuntime(zcu)) {
4285 try bw.writeByte('(');4285 try w.writeByte('(');
4286 try f.renderType(bw, .usize);4286 try f.renderType(w, .usize);
4287 try bw.writeByte(')');4287 try w.writeByte(')');
4288 }4288 }
4289 try f.writeCValue(bw, src_val, .Other);4289 try f.writeCValue(w, src_val, .Other);
4290 try v.elem(f, bw);4290 try v.elem(f, w);
4291 if (cant_cast) try bw.writeByte(')');4291 if (cant_cast) try w.writeByte(')');
4292 try bw.print(", {f}))", .{try f.fmtIntLiteral(bit_offset_val)});4292 try w.print(", {f}))", .{try f.fmtIntLiteral(bit_offset_val)});
4293 try a.end(f, bw);4293 try a.end(f, w);
4294 try v.end(f, inst, bw);4294 try v.end(f, inst, w);
4295 } else {4295 } else {
4296 switch (ptr_val) {4296 switch (ptr_val) {
4297 .local_ref => |ptr_local_index| switch (src_val) {4297 .local_ref => |ptr_local_index| switch (src_val) {
...@@ -4301,15 +4301,15 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {...@@ -4301,15 +4301,15 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
4301 },4301 },
4302 else => {},4302 else => {},
4303 }4303 }
4304 const v = try Vectorize.start(f, inst, bw, ptr_ty);4304 const v = try Vectorize.start(f, inst, w, ptr_ty);
4305 const a = try Assignment.start(f, bw, src_scalar_ctype);4305 const a = try Assignment.start(f, w, src_scalar_ctype);
4306 try f.writeCValueDeref(bw, ptr_val);4306 try f.writeCValueDeref(w, ptr_val);
4307 try v.elem(f, bw);4307 try v.elem(f, w);
4308 try a.assign(f, bw);4308 try a.assign(f, w);
4309 try f.writeCValue(bw, src_val, .Other);4309 try f.writeCValue(w, src_val, .Other);
4310 try v.elem(f, bw);4310 try v.elem(f, w);
4311 try a.end(f, bw);4311 try a.end(f, w);
4312 try v.end(f, inst, bw);4312 try v.end(f, inst, w);
4313 }4313 }
4314 return .none;4314 return .none;
4315}4315}
...@@ -4328,27 +4328,27 @@ fn airOverflow(f: *Function, inst: Air.Inst.Index, operation: []const u8, info:...@@ -4328,27 +4328,27 @@ fn airOverflow(f: *Function, inst: Air.Inst.Index, operation: []const u8, info:
4328 const operand_ty = f.typeOf(bin_op.lhs);4328 const operand_ty = f.typeOf(bin_op.lhs);
4329 const scalar_ty = operand_ty.scalarType(zcu);4329 const scalar_ty = operand_ty.scalarType(zcu);
43304330
4331 const bw = &f.object.code.buffered_writer;4331 const w = &f.object.code.buffered_writer;
4332 const local = try f.allocLocal(inst, inst_ty);4332 const local = try f.allocLocal(inst, inst_ty);
4333 const v = try Vectorize.start(f, inst, bw, operand_ty);4333 const v = try Vectorize.start(f, inst, w, operand_ty);
4334 try f.writeCValueMember(bw, local, .{ .field = 1 });4334 try f.writeCValueMember(w, local, .{ .field = 1 });
4335 try v.elem(f, bw);4335 try v.elem(f, w);
4336 try bw.writeAll(" = zig_");4336 try w.writeAll(" = zig_");
4337 try bw.writeAll(operation);4337 try w.writeAll(operation);
4338 try bw.writeAll("o_");4338 try w.writeAll("o_");
4339 try f.object.dg.renderTypeForBuiltinFnName(bw, scalar_ty);4339 try f.object.dg.renderTypeForBuiltinFnName(w, scalar_ty);
4340 try bw.writeAll("(&");4340 try w.writeAll("(&");
4341 try f.writeCValueMember(bw, local, .{ .field = 0 });4341 try f.writeCValueMember(w, local, .{ .field = 0 });
4342 try v.elem(f, bw);4342 try v.elem(f, w);
4343 try bw.writeAll(", ");4343 try w.writeAll(", ");
4344 try f.writeCValue(bw, lhs, .FunctionArgument);4344 try f.writeCValue(w, lhs, .FunctionArgument);
4345 try v.elem(f, bw);4345 try v.elem(f, w);
4346 try bw.writeAll(", ");4346 try w.writeAll(", ");
4347 try f.writeCValue(bw, rhs, .FunctionArgument);4347 try f.writeCValue(w, rhs, .FunctionArgument);
4348 if (f.typeOf(bin_op.rhs).isVector(zcu)) try v.elem(f, bw);4348 if (f.typeOf(bin_op.rhs).isVector(zcu)) try v.elem(f, w);
4349 try f.object.dg.renderBuiltinInfo(bw, scalar_ty, info);4349 try f.object.dg.renderBuiltinInfo(w, scalar_ty, info);
4350 try bw.writeAll(");\n");4350 try w.writeAll(");\n");
4351 try v.end(f, inst, bw);4351 try v.end(f, inst, w);
43524352
4353 return local;4353 return local;
4354}4354}
...@@ -4366,18 +4366,18 @@ fn airNot(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -4366,18 +4366,18 @@ fn airNot(f: *Function, inst: Air.Inst.Index) !CValue {
43664366
4367 const inst_ty = f.typeOfIndex(inst);4367 const inst_ty = f.typeOfIndex(inst);
43684368
4369 const bw = &f.object.code.buffered_writer;4369 const w = &f.object.code.buffered_writer;
4370 const local = try f.allocLocal(inst, inst_ty);4370 const local = try f.allocLocal(inst, inst_ty);
4371 const v = try Vectorize.start(f, inst, bw, operand_ty);4371 const v = try Vectorize.start(f, inst, w, operand_ty);
4372 try f.writeCValue(bw, local, .Other);4372 try f.writeCValue(w, local, .Other);
4373 try v.elem(f, bw);4373 try v.elem(f, w);
4374 try bw.writeAll(" = ");4374 try w.writeAll(" = ");
4375 try bw.writeByte('!');4375 try w.writeByte('!');
4376 try f.writeCValue(bw, op, .Other);4376 try f.writeCValue(w, op, .Other);
4377 try v.elem(f, bw);4377 try v.elem(f, w);
4378 try bw.writeByte(';');4378 try w.writeByte(';');
4379 try f.object.newline();4379 try f.object.newline();
4380 try v.end(f, inst, bw);4380 try v.end(f, inst, w);
43814381
4382 return local;4382 return local;
4383}4383}
...@@ -4403,22 +4403,22 @@ fn airBinOp(...@@ -4403,22 +4403,22 @@ fn airBinOp(
44034403
4404 const inst_ty = f.typeOfIndex(inst);4404 const inst_ty = f.typeOfIndex(inst);
44054405
4406 const bw = &f.object.code.buffered_writer;4406 const w = &f.object.code.buffered_writer;
4407 const local = try f.allocLocal(inst, inst_ty);4407 const local = try f.allocLocal(inst, inst_ty);
4408 const v = try Vectorize.start(f, inst, bw, operand_ty);4408 const v = try Vectorize.start(f, inst, w, operand_ty);
4409 try f.writeCValue(bw, local, .Other);4409 try f.writeCValue(w, local, .Other);
4410 try v.elem(f, bw);4410 try v.elem(f, w);
4411 try bw.writeAll(" = ");4411 try w.writeAll(" = ");
4412 try f.writeCValue(bw, lhs, .Other);4412 try f.writeCValue(w, lhs, .Other);
4413 try v.elem(f, bw);4413 try v.elem(f, w);
4414 try bw.writeByte(' ');4414 try w.writeByte(' ');
4415 try bw.writeAll(operator);4415 try w.writeAll(operator);
4416 try bw.writeByte(' ');4416 try w.writeByte(' ');
4417 try f.writeCValue(bw, rhs, .Other);4417 try f.writeCValue(w, rhs, .Other);
4418 try v.elem(f, bw);4418 try v.elem(f, w);
4419 try bw.writeByte(';');4419 try w.writeByte(';');
4420 try f.object.newline();4420 try f.object.newline();
4421 try v.end(f, inst, bw);4421 try v.end(f, inst, w);
44224422
4423 return local;4423 return local;
4424}4424}
...@@ -4454,27 +4454,27 @@ fn airCmpOp(...@@ -4454,27 +4454,27 @@ fn airCmpOp(
44544454
4455 const rhs_ty = f.typeOf(data.rhs);4455 const rhs_ty = f.typeOf(data.rhs);
4456 const need_cast = lhs_ty.isSinglePointer(zcu) or rhs_ty.isSinglePointer(zcu);4456 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;
4458 const local = try f.allocLocal(inst, inst_ty);4458 const local = try f.allocLocal(inst, inst_ty);
4459 const v = try Vectorize.start(f, inst, bw, lhs_ty);4459 const v = try Vectorize.start(f, inst, w, lhs_ty);
4460 const a = try Assignment.start(f, bw, try f.ctypeFromType(scalar_ty, .complete));4460 const a = try Assignment.start(f, w, try f.ctypeFromType(scalar_ty, .complete));
4461 try f.writeCValue(bw, local, .Other);4461 try f.writeCValue(w, local, .Other);
4462 try v.elem(f, bw);4462 try v.elem(f, w);
4463 try a.assign(f, bw);4463 try a.assign(f, w);
4464 if (lhs != .undef and lhs.eql(rhs)) try bw.writeAll(switch (operator) {4464 if (lhs != .undef and lhs.eql(rhs)) try w.writeAll(switch (operator) {
4465 .lt, .neq, .gt => "false",4465 .lt, .neq, .gt => "false",
4466 .lte, .eq, .gte => "true",4466 .lte, .eq, .gte => "true",
4467 }) else {4467 }) else {
4468 if (need_cast) try bw.writeAll("(void*)");4468 if (need_cast) try w.writeAll("(void*)");
4469 try f.writeCValue(bw, lhs, .Other);4469 try f.writeCValue(w, lhs, .Other);
4470 try v.elem(f, bw);4470 try v.elem(f, w);
4471 try bw.writeAll(compareOperatorC(operator));4471 try w.writeAll(compareOperatorC(operator));
4472 if (need_cast) try bw.writeAll("(void*)");4472 if (need_cast) try w.writeAll("(void*)");
4473 try f.writeCValue(bw, rhs, .Other);4473 try f.writeCValue(w, rhs, .Other);
4474 try v.elem(f, bw);4474 try v.elem(f, w);
4475 }4475 }
4476 try a.end(f, bw);4476 try a.end(f, w);
4477 try v.end(f, inst, bw);4477 try v.end(f, inst, w);
44784478
4479 return local;4479 return local;
4480}4480}
...@@ -4507,41 +4507,41 @@ fn airEquality(...@@ -4507,41 +4507,41 @@ fn airEquality(
4507 const rhs = try f.resolveInst(bin_op.rhs);4507 const rhs = try f.resolveInst(bin_op.rhs);
4508 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });4508 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;
4511 const local = try f.allocLocal(inst, .bool);4511 const local = try f.allocLocal(inst, .bool);
4512 const a = try Assignment.start(f, bw, .bool);4512 const a = try Assignment.start(f, w, .bool);
4513 try f.writeCValue(bw, local, .Other);4513 try f.writeCValue(w, local, .Other);
4514 try a.assign(f, bw);4514 try a.assign(f, w);
45154515
4516 const operand_ctype = try f.ctypeFromType(operand_ty, .complete);4516 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) {
4518 .lt, .lte, .gte, .gt => unreachable,4518 .lt, .lte, .gte, .gt => unreachable,
4519 .neq => "false",4519 .neq => "false",
4520 .eq => "true",4520 .eq => "true",
4521 }) else switch (operand_ctype.info(ctype_pool)) {4521 }) else switch (operand_ctype.info(ctype_pool)) {
4522 .basic, .pointer => {4522 .basic, .pointer => {
4523 try f.writeCValue(bw, lhs, .Other);4523 try f.writeCValue(w, lhs, .Other);
4524 try bw.writeAll(compareOperatorC(operator));4524 try w.writeAll(compareOperatorC(operator));
4525 try f.writeCValue(bw, rhs, .Other);4525 try f.writeCValue(w, rhs, .Other);
4526 },4526 },
4527 .aligned, .array, .vector, .fwd_decl, .function => unreachable,4527 .aligned, .array, .vector, .fwd_decl, .function => unreachable,
4528 .aggregate => |aggregate| if (aggregate.fields.len == 2 and4528 .aggregate => |aggregate| if (aggregate.fields.len == 2 and
4529 (aggregate.fields.at(0, ctype_pool).name.index == .is_null or4529 (aggregate.fields.at(0, ctype_pool).name.index == .is_null or
4530 aggregate.fields.at(1, ctype_pool).name.index == .is_null))4530 aggregate.fields.at(1, ctype_pool).name.index == .is_null))
4531 {4531 {
4532 try f.writeCValueMember(bw, lhs, .{ .identifier = "is_null" });4532 try f.writeCValueMember(w, lhs, .{ .identifier = "is_null" });
4533 try bw.writeAll(" || ");4533 try w.writeAll(" || ");
4534 try f.writeCValueMember(bw, rhs, .{ .identifier = "is_null" });4534 try f.writeCValueMember(w, rhs, .{ .identifier = "is_null" });
4535 try bw.writeAll(" ? ");4535 try w.writeAll(" ? ");
4536 try f.writeCValueMember(bw, lhs, .{ .identifier = "is_null" });4536 try f.writeCValueMember(w, lhs, .{ .identifier = "is_null" });
4537 try bw.writeAll(compareOperatorC(operator));4537 try w.writeAll(compareOperatorC(operator));
4538 try f.writeCValueMember(bw, rhs, .{ .identifier = "is_null" });4538 try f.writeCValueMember(w, rhs, .{ .identifier = "is_null" });
4539 try bw.writeAll(" : ");4539 try w.writeAll(" : ");
4540 try f.writeCValueMember(bw, lhs, .{ .identifier = "payload" });4540 try f.writeCValueMember(w, lhs, .{ .identifier = "payload" });
4541 try bw.writeAll(compareOperatorC(operator));4541 try w.writeAll(compareOperatorC(operator));
4542 try f.writeCValueMember(bw, rhs, .{ .identifier = "payload" });4542 try f.writeCValueMember(w, rhs, .{ .identifier = "payload" });
4543 } else for (0..aggregate.fields.len) |field_index| {4543 } 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) {
4545 .lt, .lte, .gte, .gt => unreachable,4545 .lt, .lte, .gte, .gt => unreachable,
4546 .eq => " && ",4546 .eq => " && ",
4547 .neq => " || ",4547 .neq => " || ",
...@@ -4549,12 +4549,12 @@ fn airEquality(...@@ -4549,12 +4549,12 @@ fn airEquality(
4549 const field_name: CValue = .{4549 const field_name: CValue = .{
4550 .ctype_pool_string = aggregate.fields.at(field_index, ctype_pool).name,4550 .ctype_pool_string = aggregate.fields.at(field_index, ctype_pool).name,
4551 };4551 };
4552 try f.writeCValueMember(bw, lhs, field_name);4552 try f.writeCValueMember(w, lhs, field_name);
4553 try bw.writeAll(compareOperatorC(operator));4553 try w.writeAll(compareOperatorC(operator));
4554 try f.writeCValueMember(bw, rhs, field_name);4554 try f.writeCValueMember(w, rhs, field_name);
4555 },4555 },
4556 }4556 }
4557 try a.end(f, bw);4557 try a.end(f, w);
45584558
4559 return local;4559 return local;
4560}4560}
...@@ -4565,12 +4565,12 @@ fn airCmpLtErrorsLen(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -4565,12 +4565,12 @@ fn airCmpLtErrorsLen(f: *Function, inst: Air.Inst.Index) !CValue {
4565 const operand = try f.resolveInst(un_op);4565 const operand = try f.resolveInst(un_op);
4566 try reap(f, inst, &.{un_op});4566 try reap(f, inst, &.{un_op});
45674567
4568 const bw = &f.object.code.buffered_writer;4568 const w = &f.object.code.buffered_writer;
4569 const local = try f.allocLocal(inst, .bool);4569 const local = try f.allocLocal(inst, .bool);
4570 try f.writeCValue(bw, local, .Other);4570 try f.writeCValue(w, local, .Other);
4571 try bw.writeAll(" = ");4571 try w.writeAll(" = ");
4572 try f.writeCValue(bw, operand, .Other);4572 try f.writeCValue(w, operand, .Other);
4573 try bw.print(" < sizeof({f }) / sizeof(*{0f });", .{fmtIdent("zig_errorName")});4573 try w.print(" < sizeof({f }) / sizeof(*{0f });", .{fmtIdent("zig_errorName")});
4574 try f.object.newline();4574 try f.object.newline();
4575 return local;4575 return local;
4576}4576}
...@@ -4592,30 +4592,30 @@ fn airPtrAddSub(f: *Function, inst: Air.Inst.Index, operator: u8) !CValue {...@@ -4592,30 +4592,30 @@ fn airPtrAddSub(f: *Function, inst: Air.Inst.Index, operator: u8) !CValue {
4592 const inst_scalar_ctype = try f.ctypeFromType(inst_scalar_ty, .complete);4592 const inst_scalar_ctype = try f.ctypeFromType(inst_scalar_ty, .complete);
45934593
4594 const local = try f.allocLocal(inst, inst_ty);4594 const local = try f.allocLocal(inst, inst_ty);
4595 const bw = &f.object.code.buffered_writer;4595 const w = &f.object.code.buffered_writer;
4596 const v = try Vectorize.start(f, inst, bw, inst_ty);4596 const v = try Vectorize.start(f, inst, w, inst_ty);
4597 const a = try Assignment.start(f, bw, inst_scalar_ctype);4597 const a = try Assignment.start(f, w, inst_scalar_ctype);
4598 try f.writeCValue(bw, local, .Other);4598 try f.writeCValue(w, local, .Other);
4599 try v.elem(f, bw);4599 try v.elem(f, w);
4600 try a.assign(f, bw);4600 try a.assign(f, w);
4601 // We must convert to and from integer types to prevent UB if the operation4601 // We must convert to and from integer types to prevent UB if the operation
4602 // results in a NULL pointer, or if LHS is NULL. The operation is only UB4602 // results in a NULL pointer, or if LHS is NULL. The operation is only UB
4603 // if the result is NULL and then dereferenced.4603 // if the result is NULL and then dereferenced.
4604 try bw.writeByte('(');4604 try w.writeByte('(');
4605 try f.renderCType(bw, inst_scalar_ctype);4605 try f.renderCType(w, inst_scalar_ctype);
4606 try bw.writeAll(")(((uintptr_t)");4606 try w.writeAll(")(((uintptr_t)");
4607 try f.writeCValue(bw, lhs, .Other);4607 try f.writeCValue(w, lhs, .Other);
4608 try v.elem(f, bw);4608 try v.elem(f, w);
4609 try bw.writeAll(") ");4609 try w.writeAll(") ");
4610 try bw.writeByte(operator);4610 try w.writeByte(operator);
4611 try bw.writeAll(" (");4611 try w.writeAll(" (");
4612 try f.writeCValue(bw, rhs, .Other);4612 try f.writeCValue(w, rhs, .Other);
4613 try v.elem(f, bw);4613 try v.elem(f, w);
4614 try bw.writeAll("*sizeof(");4614 try w.writeAll("*sizeof(");
4615 try f.renderType(bw, elem_ty);4615 try f.renderType(w, elem_ty);
4616 try bw.writeAll(")))");4616 try w.writeAll(")))");
4617 try a.end(f, bw);4617 try a.end(f, w);
4618 try v.end(f, inst, bw);4618 try v.end(f, inst, w);
4619 return local;4619 return local;
4620}4620}
46214621
...@@ -4634,29 +4634,29 @@ fn airMinMax(f: *Function, inst: Air.Inst.Index, operator: u8, operation: []cons...@@ -4634,29 +4634,29 @@ fn airMinMax(f: *Function, inst: Air.Inst.Index, operator: u8, operation: []cons
4634 const rhs = try f.resolveInst(bin_op.rhs);4634 const rhs = try f.resolveInst(bin_op.rhs);
4635 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });4635 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;
4638 const local = try f.allocLocal(inst, inst_ty);4638 const local = try f.allocLocal(inst, inst_ty);
4639 const v = try Vectorize.start(f, inst, bw, inst_ty);4639 const v = try Vectorize.start(f, inst, w, inst_ty);
4640 try f.writeCValue(bw, local, .Other);4640 try f.writeCValue(w, local, .Other);
4641 try v.elem(f, bw);4641 try v.elem(f, w);
4642 // (lhs <> rhs) ? lhs : rhs4642 // (lhs <> rhs) ? lhs : rhs
4643 try bw.writeAll(" = (");4643 try w.writeAll(" = (");
4644 try f.writeCValue(bw, lhs, .Other);4644 try f.writeCValue(w, lhs, .Other);
4645 try v.elem(f, bw);4645 try v.elem(f, w);
4646 try bw.writeByte(' ');4646 try w.writeByte(' ');
4647 try bw.writeByte(operator);4647 try w.writeByte(operator);
4648 try bw.writeByte(' ');4648 try w.writeByte(' ');
4649 try f.writeCValue(bw, rhs, .Other);4649 try f.writeCValue(w, rhs, .Other);
4650 try v.elem(f, bw);4650 try v.elem(f, w);
4651 try bw.writeAll(") ? ");4651 try w.writeAll(") ? ");
4652 try f.writeCValue(bw, lhs, .Other);4652 try f.writeCValue(w, lhs, .Other);
4653 try v.elem(f, bw);4653 try v.elem(f, w);
4654 try bw.writeAll(" : ");4654 try w.writeAll(" : ");
4655 try f.writeCValue(bw, rhs, .Other);4655 try f.writeCValue(w, rhs, .Other);
4656 try v.elem(f, bw);4656 try v.elem(f, w);
4657 try bw.writeByte(';');4657 try w.writeByte(';');
4658 try f.object.newline();4658 try f.object.newline();
4659 try v.end(f, inst, bw);4659 try v.end(f, inst, w);
46604660
4661 return local;4661 return local;
4662}4662}
...@@ -4674,21 +4674,21 @@ fn airSlice(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -4674,21 +4674,21 @@ fn airSlice(f: *Function, inst: Air.Inst.Index) !CValue {
4674 const inst_ty = f.typeOfIndex(inst);4674 const inst_ty = f.typeOfIndex(inst);
4675 const ptr_ty = inst_ty.slicePtrFieldType(zcu);4675 const ptr_ty = inst_ty.slicePtrFieldType(zcu);
46764676
4677 const bw = &f.object.code.buffered_writer;4677 const w = &f.object.code.buffered_writer;
4678 const local = try f.allocLocal(inst, inst_ty);4678 const local = try f.allocLocal(inst, inst_ty);
4679 {4679 {
4680 const a = try Assignment.start(f, bw, try f.ctypeFromType(ptr_ty, .complete));4680 const a = try Assignment.start(f, w, try f.ctypeFromType(ptr_ty, .complete));
4681 try f.writeCValueMember(bw, local, .{ .identifier = "ptr" });4681 try f.writeCValueMember(w, local, .{ .identifier = "ptr" });
4682 try a.assign(f, bw);4682 try a.assign(f, w);
4683 try f.writeCValue(bw, ptr, .Other);4683 try f.writeCValue(w, ptr, .Other);
4684 try a.end(f, bw);4684 try a.end(f, w);
4685 }4685 }
4686 {4686 {
4687 const a = try Assignment.start(f, bw, .usize);4687 const a = try Assignment.start(f, w, .usize);
4688 try f.writeCValueMember(bw, local, .{ .identifier = "len" });4688 try f.writeCValueMember(w, local, .{ .identifier = "len" });
4689 try a.assign(f, bw);4689 try a.assign(f, w);
4690 try f.writeCValue(bw, len, .Other);4690 try f.writeCValue(w, len, .Other);
4691 try a.end(f, bw);4691 try a.end(f, w);
4692 }4692 }
4693 return local;4693 return local;
4694}4694}
...@@ -4705,7 +4705,7 @@ fn airCall(...@@ -4705,7 +4705,7 @@ fn airCall(
4705 if (f.object.dg.is_naked_fn) return .none;4705 if (f.object.dg.is_naked_fn) return .none;
47064706
4707 const gpa = f.object.dg.gpa;4707 const gpa = f.object.dg.gpa;
4708 const bw = &f.object.code.buffered_writer;4708 const w = &f.object.code.buffered_writer;
47094709
4710 const pl_op = f.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;4710 const pl_op = f.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
4711 const extra = f.air.extraData(Air.Call, pl_op.payload);4711 const extra = f.air.extraData(Air.Call, pl_op.payload);
...@@ -4726,13 +4726,13 @@ fn airCall(...@@ -4726,13 +4726,13 @@ fn airCall(
4726 .ctype = arg_ctype,4726 .ctype = arg_ctype,
4727 .alignas = CType.AlignAs.fromAbiAlignment(arg_ty.abiAlignment(zcu)),4727 .alignas = CType.AlignAs.fromAbiAlignment(arg_ty.abiAlignment(zcu)),
4728 });4728 });
4729 try bw.writeAll("memcpy(");4729 try w.writeAll("memcpy(");
4730 try f.writeCValueMember(bw, array_local, .{ .identifier = "array" });4730 try f.writeCValueMember(w, array_local, .{ .identifier = "array" });
4731 try bw.writeAll(", ");4731 try w.writeAll(", ");
4732 try f.writeCValue(bw, resolved_arg.*, .FunctionArgument);4732 try f.writeCValue(w, resolved_arg.*, .FunctionArgument);
4733 try bw.writeAll(", sizeof(");4733 try w.writeAll(", sizeof(");
4734 try f.renderCType(bw, arg_ctype);4734 try f.renderCType(w, arg_ctype);
4735 try bw.writeAll("));");4735 try w.writeAll("));");
4736 try f.object.newline();4736 try f.object.newline();
4737 resolved_arg.* = array_local;4737 resolved_arg.* = array_local;
4738 }4738 }
...@@ -4761,22 +4761,22 @@ fn airCall(...@@ -4761,22 +4761,22 @@ fn airCall(
47614761
4762 const result_local = result: {4762 const result_local = result: {
4763 if (modifier == .always_tail) {4763 if (modifier == .always_tail) {
4764 try bw.writeAll("zig_always_tail return ");4764 try w.writeAll("zig_always_tail return ");
4765 break :result .none;4765 break :result .none;
4766 } else if (ret_ctype.index == .void) {4766 } else if (ret_ctype.index == .void) {
4767 break :result .none;4767 break :result .none;
4768 } else if (f.liveness.isUnused(inst)) {4768 } else if (f.liveness.isUnused(inst)) {
4769 try bw.writeByte('(');4769 try w.writeByte('(');
4770 try f.renderCType(bw, .void);4770 try f.renderCType(w, .void);
4771 try bw.writeByte(')');4771 try w.writeByte(')');
4772 break :result .none;4772 break :result .none;
4773 } else {4773 } else {
4774 const local = try f.allocAlignedLocal(inst, .{4774 const local = try f.allocAlignedLocal(inst, .{
4775 .ctype = ret_ctype,4775 .ctype = ret_ctype,
4776 .alignas = CType.AlignAs.fromAbiAlignment(ret_ty.abiAlignment(zcu)),4776 .alignas = CType.AlignAs.fromAbiAlignment(ret_ty.abiAlignment(zcu)),
4777 });4777 });
4778 try f.writeCValue(bw, local, .Other);4778 try f.writeCValue(w, local, .Other);
4779 try bw.writeAll(" = ");4779 try w.writeAll(" = ");
4780 break :result local;4780 break :result local;
4781 }4781 }
4782 };4782 };
...@@ -4796,17 +4796,17 @@ fn airCall(...@@ -4796,17 +4796,17 @@ fn airCall(
4796 else => break :known,4796 else => break :known,
4797 };4797 };
4798 if (need_cast) {4798 if (need_cast) {
4799 try bw.writeAll("((");4799 try w.writeAll("((");
4800 try f.renderType(bw, if (callee_is_ptr) callee_ty else try pt.singleConstPtrType(callee_ty));4800 try f.renderType(w, if (callee_is_ptr) callee_ty else try pt.singleConstPtrType(callee_ty));
4801 try bw.writeByte(')');4801 try w.writeByte(')');
4802 if (!callee_is_ptr) try bw.writeByte('&');4802 if (!callee_is_ptr) try w.writeByte('&');
4803 }4803 }
4804 switch (modifier) {4804 switch (modifier) {
4805 .auto, .always_tail => try f.object.dg.renderNavName(bw, fn_nav),4805 .auto, .always_tail => try f.object.dg.renderNavName(w, fn_nav),
4806 inline .never_tail, .never_inline => |m| try bw.writeAll(try f.getLazyFnName(@unionInit(LazyFnKey, @tagName(m), fn_nav))),4806 inline .never_tail, .never_inline => |m| try w.writeAll(try f.getLazyFnName(@unionInit(LazyFnKey, @tagName(m), fn_nav))),
4807 else => unreachable,4807 else => unreachable,
4808 }4808 }
4809 if (need_cast) try bw.writeByte(')');4809 if (need_cast) try w.writeByte(')');
4810 break :callee;4810 break :callee;
4811 }4811 }
4812 switch (modifier) {4812 switch (modifier) {
...@@ -4816,21 +4816,21 @@ fn airCall(...@@ -4816,21 +4816,21 @@ fn airCall(
4816 else => unreachable,4816 else => unreachable,
4817 }4817 }
4818 // Fall back to function pointer call.4818 // Fall back to function pointer call.
4819 try f.writeCValue(bw, callee, .Other);4819 try f.writeCValue(w, callee, .Other);
4820 }4820 }
48214821
4822 try bw.writeByte('(');4822 try w.writeByte('(');
4823 var need_comma = false;4823 var need_comma = false;
4824 for (resolved_args) |resolved_arg| {4824 for (resolved_args) |resolved_arg| {
4825 if (resolved_arg == .none) continue;4825 if (resolved_arg == .none) continue;
4826 if (need_comma) try bw.writeAll(", ");4826 if (need_comma) try w.writeAll(", ");
4827 need_comma = true;4827 need_comma = true;
4828 try f.writeCValue(bw, resolved_arg, .FunctionArgument);4828 try f.writeCValue(w, resolved_arg, .FunctionArgument);
4829 try f.freeCValue(inst, resolved_arg);4829 try f.freeCValue(inst, resolved_arg);
4830 }4830 }
4831 try bw.writeAll(");");4831 try w.writeAll(");");
4832 switch (modifier) {4832 switch (modifier) {
4833 .always_tail => try bw.writeByte('\n'),4833 .always_tail => try w.writeByte('\n'),
4834 else => try f.object.newline(),4834 else => try f.object.newline(),
4835 }4835 }
48364836
...@@ -4839,13 +4839,13 @@ fn airCall(...@@ -4839,13 +4839,13 @@ fn airCall(
4839 break :result result_local;4839 break :result result_local;
48404840
4841 const array_local = try f.allocLocal(inst, ret_ty);4841 const array_local = try f.allocLocal(inst, ret_ty);
4842 try bw.writeAll("memcpy(");4842 try w.writeAll("memcpy(");
4843 try f.writeCValue(bw, array_local, .FunctionArgument);4843 try f.writeCValue(w, array_local, .FunctionArgument);
4844 try bw.writeAll(", ");4844 try w.writeAll(", ");
4845 try f.writeCValueMember(bw, result_local, .{ .identifier = "array" });4845 try f.writeCValueMember(w, result_local, .{ .identifier = "array" });
4846 try bw.writeAll(", sizeof(");4846 try w.writeAll(", sizeof(");
4847 try f.renderType(bw, ret_ty);4847 try f.renderType(w, ret_ty);
4848 try bw.writeAll("));");4848 try w.writeAll("));");
4849 try f.object.newline();4849 try f.object.newline();
4850 try freeLocal(f, inst, result_local.new_local, null);4850 try freeLocal(f, inst, result_local.new_local, null);
4851 break :result array_local;4851 break :result array_local;
...@@ -4856,7 +4856,7 @@ fn airCall(...@@ -4856,7 +4856,7 @@ fn airCall(
48564856
4857fn airDbgStmt(f: *Function, inst: Air.Inst.Index) !CValue {4857fn airDbgStmt(f: *Function, inst: Air.Inst.Index) !CValue {
4858 const dbg_stmt = f.air.instructions.items(.data)[@intFromEnum(inst)].dbg_stmt;4858 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;
4860 // TODO re-evaluate whether to emit these or not. If we naively emit4860 // TODO re-evaluate whether to emit these or not. If we naively emit
4861 // these directives, the output file will report bogus line numbers because4861 // these directives, the output file will report bogus line numbers because
4862 // every newline after the #line directive adds one to the line.4862 // every newline after the #line directive adds one to the line.
...@@ -4864,9 +4864,9 @@ fn airDbgStmt(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -4864,9 +4864,9 @@ fn airDbgStmt(f: *Function, inst: Air.Inst.Index) !CValue {
4864 // If we wanted to go this route, we would need to go all the way and not output4864 // If we wanted to go this route, we would need to go all the way and not output
4865 // newlines until the next dbg_stmt occurs.4865 // newlines until the next dbg_stmt occurs.
4866 // Perhaps an additional compilation option is in order?4866 // 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});
4868 //try f.object.newline();4868 //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 });
4870 try f.object.newline();4870 try f.object.newline();
4871 return .none;4871 return .none;
4872}4872}
...@@ -4899,8 +4899,8 @@ fn airDbgVar(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -4899,8 +4899,8 @@ fn airDbgVar(f: *Function, inst: Air.Inst.Index) !CValue {
4899 if (!operand_is_undef) _ = try f.resolveInst(pl_op.operand);4899 if (!operand_is_undef) _ = try f.resolveInst(pl_op.operand);
49004900
4901 try reap(f, inst, &.{pl_op.operand});4901 try reap(f, inst, &.{pl_op.operand});
4902 const bw = &f.object.code.buffered_writer;4902 const w = &f.object.code.buffered_writer;
4903 try bw.print("/* {s}:{s} */", .{ @tagName(tag), name.toSlice(f.air) });4903 try w.print("/* {s}:{s} */", .{ @tagName(tag), name.toSlice(f.air) });
4904 try f.object.newline();4904 try f.object.newline();
4905 return .none;4905 return .none;
4906}4906}
...@@ -4918,7 +4918,7 @@ fn lowerBlock(f: *Function, inst: Air.Inst.Index, body: []const Air.Inst.Index)...@@ -4918,7 +4918,7 @@ fn lowerBlock(f: *Function, inst: Air.Inst.Index, body: []const Air.Inst.Index)
49184918
4919 const block_id = f.next_block_index;4919 const block_id = f.next_block_index;
4920 f.next_block_index += 1;4920 f.next_block_index += 1;
4921 const bw = &f.object.code.buffered_writer;4921 const w = &f.object.code.buffered_writer;
49224922
4923 const inst_ty = f.typeOfIndex(inst);4923 const inst_ty = f.typeOfIndex(inst);
4924 const result = if (inst_ty.hasRuntimeBitsIgnoreComptime(zcu) and !f.liveness.isUnused(inst))4924 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)...@@ -4949,7 +4949,7 @@ fn lowerBlock(f: *Function, inst: Air.Inst.Index, body: []const Air.Inst.Index)
4949 }4949 }
4950 } else if (!f.typeOfIndex(inst).isNoReturn(zcu)) {4950 } else if (!f.typeOfIndex(inst).isNoReturn(zcu)) {
4951 // label must be followed by an expression, include an empty one.4951 // 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});
4953 try f.object.newline();4953 try f.object.newline();
4954 }4954 }
49554955
...@@ -4987,28 +4987,28 @@ fn lowerTry(...@@ -4987,28 +4987,28 @@ fn lowerTry(
4987 const err_union = try f.resolveInst(operand);4987 const err_union = try f.resolveInst(operand);
4988 const inst_ty = f.typeOfIndex(inst);4988 const inst_ty = f.typeOfIndex(inst);
4989 const liveness_condbr = f.liveness.getCondBr(inst);4989 const liveness_condbr = f.liveness.getCondBr(inst);
4990 const bw = &f.object.code.buffered_writer;4990 const w = &f.object.code.buffered_writer;
4991 const payload_ty = err_union_ty.errorUnionPayload(zcu);4991 const payload_ty = err_union_ty.errorUnionPayload(zcu);
4992 const payload_has_bits = payload_ty.hasRuntimeBitsIgnoreComptime(zcu);4992 const payload_has_bits = payload_ty.hasRuntimeBitsIgnoreComptime(zcu);
49934993
4994 if (!err_union_ty.errorUnionSet(zcu).errorSetIsEmpty(zcu)) {4994 if (!err_union_ty.errorUnionSet(zcu).errorSetIsEmpty(zcu)) {
4995 try bw.writeAll("if (");4995 try w.writeAll("if (");
4996 if (!payload_has_bits) {4996 if (!payload_has_bits) {
4997 if (is_ptr)4997 if (is_ptr)
4998 try f.writeCValueDeref(bw, err_union)4998 try f.writeCValueDeref(w, err_union)
4999 else4999 else
5000 try f.writeCValue(bw, err_union, .Other);5000 try f.writeCValue(w, err_union, .Other);
5001 } else {5001 } else {
5002 // Reap the operand so that it can be reused inside genBody.5002 // Reap the operand so that it can be reused inside genBody.
5003 // Remember we must avoid calling reap() twice for the same operand5003 // Remember we must avoid calling reap() twice for the same operand
5004 // in this function.5004 // in this function.
5005 try reap(f, inst, &.{operand});5005 try reap(f, inst, &.{operand});
5006 if (is_ptr)5006 if (is_ptr)
5007 try f.writeCValueDerefMember(bw, err_union, .{ .identifier = "error" })5007 try f.writeCValueDerefMember(w, err_union, .{ .identifier = "error" })
5008 else5008 else
5009 try f.writeCValueMember(bw, err_union, .{ .identifier = "error" });5009 try f.writeCValueMember(w, err_union, .{ .identifier = "error" });
5010 }5010 }
5011 try bw.writeAll(") ");5011 try w.writeAll(") ");
50125012
5013 try genBodyResolveState(f, inst, liveness_condbr.else_deaths, body, false);5013 try genBodyResolveState(f, inst, liveness_condbr.else_deaths, body, false);
5014 try f.object.newline();5014 try f.object.newline();
...@@ -5034,14 +5034,14 @@ fn lowerTry(...@@ -5034,14 +5034,14 @@ fn lowerTry(
5034 if (f.liveness.isUnused(inst)) return .none;5034 if (f.liveness.isUnused(inst)) return .none;
50355035
5036 const local = try f.allocLocal(inst, inst_ty);5036 const local = try f.allocLocal(inst, inst_ty);
5037 const a = try Assignment.start(f, bw, try f.ctypeFromType(inst_ty, .complete));5037 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete));
5038 try f.writeCValue(bw, local, .Other);5038 try f.writeCValue(w, local, .Other);
5039 try a.assign(f, bw);5039 try a.assign(f, w);
5040 if (is_ptr) {5040 if (is_ptr) {
5041 try bw.writeByte('&');5041 try w.writeByte('&');
5042 try f.writeCValueDerefMember(bw, err_union, .{ .identifier = "payload" });5042 try f.writeCValueDerefMember(w, err_union, .{ .identifier = "payload" });
5043 } else try f.writeCValueMember(bw, err_union, .{ .identifier = "payload" });5043 } else try f.writeCValueMember(w, err_union, .{ .identifier = "payload" });
5044 try a.end(f, bw);5044 try a.end(f, w);
5045 return local;5045 return local;
5046}5046}
50475047
...@@ -5049,7 +5049,7 @@ fn airBr(f: *Function, inst: Air.Inst.Index) !void {...@@ -5049,7 +5049,7 @@ fn airBr(f: *Function, inst: Air.Inst.Index) !void {
5049 const branch = f.air.instructions.items(.data)[@intFromEnum(inst)].br;5049 const branch = f.air.instructions.items(.data)[@intFromEnum(inst)].br;
5050 const block = f.blocks.get(branch.block_inst).?;5050 const block = f.blocks.get(branch.block_inst).?;
5051 const result = block.result;5051 const result = block.result;
5052 const bw = &f.object.code.buffered_writer;5052 const w = &f.object.code.buffered_writer;
50535053
5054 if (f.object.dg.is_naked_fn) {5054 if (f.object.dg.is_naked_fn) {
5055 if (result != .none) return f.fail("runtime code not allowed in naked function", .{});5055 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 {...@@ -5063,14 +5063,14 @@ fn airBr(f: *Function, inst: Air.Inst.Index) !void {
5063 const operand = try f.resolveInst(branch.operand);5063 const operand = try f.resolveInst(branch.operand);
5064 try reap(f, inst, &.{branch.operand});5064 try reap(f, inst, &.{branch.operand});
50655065
5066 const a = try Assignment.start(f, bw, try f.ctypeFromType(operand_ty, .complete));5066 const a = try Assignment.start(f, w, try f.ctypeFromType(operand_ty, .complete));
5067 try f.writeCValue(bw, result, .Other);5067 try f.writeCValue(w, result, .Other);
5068 try a.assign(f, bw);5068 try a.assign(f, w);
5069 try f.writeCValue(bw, operand, .Other);5069 try f.writeCValue(w, operand, .Other);
5070 try a.end(f, bw);5070 try a.end(f, w);
5071 }5071 }
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});
5074}5074}
50755075
5076fn airRepeat(f: *Function, inst: Air.Inst.Index) !void {5076fn airRepeat(f: *Function, inst: Air.Inst.Index) !void {
...@@ -5082,7 +5082,7 @@ fn airSwitchDispatch(f: *Function, inst: Air.Inst.Index) !void {...@@ -5082,7 +5082,7 @@ fn airSwitchDispatch(f: *Function, inst: Air.Inst.Index) !void {
5082 const pt = f.object.dg.pt;5082 const pt = f.object.dg.pt;
5083 const zcu = pt.zcu;5083 const zcu = pt.zcu;
5084 const br = f.air.instructions.items(.data)[@intFromEnum(inst)].br;5084 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
5087 if (try f.air.value(br.operand, pt)) |cond_val| {5087 if (try f.air.value(br.operand, pt)) |cond_val| {
5088 // Comptime-known dispatch. Iterate the cases to find the correct5088 // Comptime-known dispatch. Iterate the cases to find the correct
...@@ -5104,19 +5104,19 @@ fn airSwitchDispatch(f: *Function, inst: Air.Inst.Index) !void {...@@ -5104,19 +5104,19 @@ fn airSwitchDispatch(f: *Function, inst: Air.Inst.Index) !void {
5104 }5104 }
5105 }5105 }
5106 } else switch_br.cases_len;5106 } 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 });
5108 return;5108 return;
5109 }5109 }
51105110
5111 // Runtime-known dispatch. Set the switch condition, and branch back.5111 // Runtime-known dispatch. Set the switch condition, and branch back.
5112 const cond = try f.resolveInst(br.operand);5112 const cond = try f.resolveInst(br.operand);
5113 const cond_local = f.loop_switch_conds.get(br.block_inst).?;5113 const cond_local = f.loop_switch_conds.get(br.block_inst).?;
5114 try f.writeCValue(bw, .{ .local = cond_local }, .Other);5114 try f.writeCValue(w, .{ .local = cond_local }, .Other);
5115 try bw.writeAll(" = ");5115 try w.writeAll(" = ");
5116 try f.writeCValue(bw, cond, .Other);5116 try f.writeCValue(w, cond, .Other);
5117 try bw.writeByte(';');5117 try w.writeByte(';');
5118 try f.object.newline();5118 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)});
5120}5120}
51215121
5122fn airBitcast(f: *Function, inst: Air.Inst.Index) !CValue {5122fn 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...@@ -5136,7 +5136,7 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !CVal
5136 const zcu = pt.zcu;5136 const zcu = pt.zcu;
5137 const target = &f.object.dg.mod.resolved_target.result;5137 const target = &f.object.dg.mod.resolved_target.result;
5138 const ctype_pool = &f.object.dg.ctype_pool;5138 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
5141 if (operand_ty.isAbiInt(zcu) and dest_ty.isAbiInt(zcu)) {5141 if (operand_ty.isAbiInt(zcu) and dest_ty.isAbiInt(zcu)) {
5142 const src_info = dest_ty.intInfo(zcu);5142 const src_info = dest_ty.intInfo(zcu);
...@@ -5147,43 +5147,43 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !CVal...@@ -5147,43 +5147,43 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !CVal
51475147
5148 if (dest_ty.isPtrAtRuntime(zcu) or operand_ty.isPtrAtRuntime(zcu)) {5148 if (dest_ty.isPtrAtRuntime(zcu) or operand_ty.isPtrAtRuntime(zcu)) {
5149 const local = try f.allocLocal(null, dest_ty);5149 const local = try f.allocLocal(null, dest_ty);
5150 try f.writeCValue(bw, local, .Other);5150 try f.writeCValue(w, local, .Other);
5151 try bw.writeAll(" = (");5151 try w.writeAll(" = (");
5152 try f.renderType(bw, dest_ty);5152 try f.renderType(w, dest_ty);
5153 try bw.writeByte(')');5153 try w.writeByte(')');
5154 try f.writeCValue(bw, operand, .Other);5154 try f.writeCValue(w, operand, .Other);
5155 try bw.writeByte(';');5155 try w.writeByte(';');
5156 try f.object.newline();5156 try f.object.newline();
5157 return local;5157 return local;
5158 }5158 }
51595159
5160 const operand_lval = if (operand == .constant) blk: {5160 const operand_lval = if (operand == .constant) blk: {
5161 const operand_local = try f.allocLocal(null, operand_ty);5161 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);
5163 if (operand_ty.isAbiInt(zcu)) {5163 if (operand_ty.isAbiInt(zcu)) {
5164 try bw.writeAll(" = ");5164 try w.writeAll(" = ");
5165 } else {5165 } else {
5166 try bw.writeAll(" = (");5166 try w.writeAll(" = (");
5167 try f.renderType(bw, operand_ty);5167 try f.renderType(w, operand_ty);
5168 try bw.writeByte(')');5168 try w.writeByte(')');
5169 }5169 }
5170 try f.writeCValue(bw, operand, .Other);5170 try f.writeCValue(w, operand, .Other);
5171 try bw.writeByte(';');5171 try w.writeByte(';');
5172 try f.object.newline();5172 try f.object.newline();
5173 break :blk operand_local;5173 break :blk operand_local;
5174 } else operand;5174 } else operand;
51755175
5176 const local = try f.allocLocal(null, dest_ty);5176 const local = try f.allocLocal(null, dest_ty);
5177 try bw.writeAll("memcpy(&");5177 try w.writeAll("memcpy(&");
5178 try f.writeCValue(bw, local, .Other);5178 try f.writeCValue(w, local, .Other);
5179 try bw.writeAll(", &");5179 try w.writeAll(", &");
5180 try f.writeCValue(bw, operand_lval, .Other);5180 try f.writeCValue(w, operand_lval, .Other);
5181 try bw.writeAll(", sizeof(");5181 try w.writeAll(", sizeof(");
5182 try f.renderType(5182 try f.renderType(
5183 bw,5183 w,
5184 if (dest_ty.abiSize(zcu) <= operand_ty.abiSize(zcu)) dest_ty else operand_ty,5184 if (dest_ty.abiSize(zcu) <= operand_ty.abiSize(zcu)) dest_ty else operand_ty,
5185 );5185 );
5186 try bw.writeAll("));");5186 try w.writeAll("));");
5187 try f.object.newline();5187 try f.object.newline();
51885188
5189 // Ensure padding bits have the expected value.5189 // Ensure padding bits have the expected value.
...@@ -5194,11 +5194,11 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !CVal...@@ -5194,11 +5194,11 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !CVal
5194 var wrap_ctype: ?CType = null;5194 var wrap_ctype: ?CType = null;
5195 var need_bitcasts = false;5195 var need_bitcasts = false;
51965196
5197 try f.writeCValue(bw, local, .Other);5197 try f.writeCValue(w, local, .Other);
5198 switch (dest_ctype.info(ctype_pool)) {5198 switch (dest_ctype.info(ctype_pool)) {
5199 else => {},5199 else => {},
5200 .array => |array_info| {5200 .array => |array_info| {
5201 try bw.print("[{d}]", .{switch (target.cpu.arch.endian()) {5201 try w.print("[{d}]", .{switch (target.cpu.arch.endian()) {
5202 .little => array_info.len - 1,5202 .little => array_info.len - 1,
5203 .big => 0,5203 .big => 0,
5204 }});5204 }});
...@@ -5209,38 +5209,38 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !CVal...@@ -5209,38 +5209,38 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !CVal
5209 bits += 1;5209 bits += 1;
5210 },5210 },
5211 }5211 }
5212 try bw.writeAll(" = ");5212 try w.writeAll(" = ");
5213 if (need_bitcasts) {5213 if (need_bitcasts) {
5214 try bw.writeAll("zig_bitCast_");5214 try w.writeAll("zig_bitCast_");
5215 try f.object.dg.renderCTypeForBuiltinFnName(bw, wrap_ctype.?.toUnsigned());5215 try f.object.dg.renderCTypeForBuiltinFnName(w, wrap_ctype.?.toUnsigned());
5216 try bw.writeByte('(');5216 try w.writeByte('(');
5217 }5217 }
5218 try bw.writeAll("zig_wrap_");5218 try w.writeAll("zig_wrap_");
5219 const info_ty = try pt.intType(dest_info.signedness, bits);5219 const info_ty = try pt.intType(dest_info.signedness, bits);
5220 if (wrap_ctype) |ctype|5220 if (wrap_ctype) |ctype|
5221 try f.object.dg.renderCTypeForBuiltinFnName(bw, ctype)5221 try f.object.dg.renderCTypeForBuiltinFnName(w, ctype)
5222 else5222 else
5223 try f.object.dg.renderTypeForBuiltinFnName(bw, info_ty);5223 try f.object.dg.renderTypeForBuiltinFnName(w, info_ty);
5224 try bw.writeByte('(');5224 try w.writeByte('(');
5225 if (need_bitcasts) {5225 if (need_bitcasts) {
5226 try bw.writeAll("zig_bitCast_");5226 try w.writeAll("zig_bitCast_");
5227 try f.object.dg.renderCTypeForBuiltinFnName(bw, wrap_ctype.?);5227 try f.object.dg.renderCTypeForBuiltinFnName(w, wrap_ctype.?);
5228 try bw.writeByte('(');5228 try w.writeByte('(');
5229 }5229 }
5230 try f.writeCValue(bw, local, .Other);5230 try f.writeCValue(w, local, .Other);
5231 switch (dest_ctype.info(ctype_pool)) {5231 switch (dest_ctype.info(ctype_pool)) {
5232 else => {},5232 else => {},
5233 .array => |array_info| try bw.print("[{d}]", .{5233 .array => |array_info| try w.print("[{d}]", .{
5234 switch (target.cpu.arch.endian()) {5234 switch (target.cpu.arch.endian()) {
5235 .little => array_info.len - 1,5235 .little => array_info.len - 1,
5236 .big => 0,5236 .big => 0,
5237 },5237 },
5238 }),5238 }),
5239 }5239 }
5240 if (need_bitcasts) try bw.writeByte(')');5240 if (need_bitcasts) try w.writeByte(')');
5241 try f.object.dg.renderBuiltinInfo(bw, info_ty, .bits);5241 try f.object.dg.renderBuiltinInfo(w, info_ty, .bits);
5242 if (need_bitcasts) try bw.writeByte(')');5242 if (need_bitcasts) try w.writeByte(')');
5243 try bw.writeAll(");");5243 try w.writeAll(");");
5244 try f.object.newline();5244 try f.object.newline();
5245 }5245 }
52465246
...@@ -5248,37 +5248,37 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !CVal...@@ -5248,37 +5248,37 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !CVal
5248 return local;5248 return local;
5249}5249}
52505250
5251fn airTrap(f: *Function, bw: *Writer) !void {5251fn airTrap(f: *Function, w: *Writer) !void {
5252 // Not even allowed to call trap in a naked function.5252 // Not even allowed to call trap in a naked function.
5253 if (f.object.dg.is_naked_fn) return;5253 if (f.object.dg.is_naked_fn) return;
5254 try bw.writeAll("zig_trap();\n");5254 try w.writeAll("zig_trap();\n");
5255}5255}
52565256
5257fn airBreakpoint(f: *Function) !CValue {5257fn airBreakpoint(f: *Function) !CValue {
5258 const bw = &f.object.code.buffered_writer;5258 const w = &f.object.code.buffered_writer;
5259 try bw.writeAll("zig_breakpoint();");5259 try w.writeAll("zig_breakpoint();");
5260 try f.object.newline();5260 try f.object.newline();
5261 return .none;5261 return .none;
5262}5262}
52635263
5264fn airRetAddr(f: *Function, inst: Air.Inst.Index) !CValue {5264fn airRetAddr(f: *Function, inst: Air.Inst.Index) !CValue {
5265 const bw = &f.object.code.buffered_writer;5265 const w = &f.object.code.buffered_writer;
5266 const local = try f.allocLocal(inst, .usize);5266 const local = try f.allocLocal(inst, .usize);
5267 try f.writeCValue(bw, local, .Other);5267 try f.writeCValue(w, local, .Other);
5268 try bw.writeAll(" = (");5268 try w.writeAll(" = (");
5269 try f.renderType(bw, .usize);5269 try f.renderType(w, .usize);
5270 try bw.writeAll(")zig_return_address();");5270 try w.writeAll(")zig_return_address();");
5271 try f.object.newline();5271 try f.object.newline();
5272 return local;5272 return local;
5273}5273}
52745274
5275fn airFrameAddress(f: *Function, inst: Air.Inst.Index) !CValue {5275fn airFrameAddress(f: *Function, inst: Air.Inst.Index) !CValue {
5276 const bw = &f.object.code.buffered_writer;5276 const w = &f.object.code.buffered_writer;
5277 const local = try f.allocLocal(inst, .usize);5277 const local = try f.allocLocal(inst, .usize);
5278 try f.writeCValue(bw, local, .Other);5278 try f.writeCValue(w, local, .Other);
5279 try bw.writeAll(" = (");5279 try w.writeAll(" = (");
5280 try f.renderType(bw, .usize);5280 try f.renderType(w, .usize);
5281 try bw.writeAll(")zig_frame_address();");5281 try w.writeAll(")zig_frame_address();");
5282 try f.object.newline();5282 try f.object.newline();
5283 return local;5283 return local;
5284}5284}
...@@ -5293,13 +5293,13 @@ fn airLoop(f: *Function, inst: Air.Inst.Index) !void {...@@ -5293,13 +5293,13 @@ fn airLoop(f: *Function, inst: Air.Inst.Index) !void {
5293 const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;5293 const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
5294 const loop = f.air.extraData(Air.Block, ty_pl.payload);5294 const loop = f.air.extraData(Air.Block, ty_pl.payload);
5295 const body: []const Air.Inst.Index = @ptrCast(f.air.extra.items[loop.end..][0..loop.data.body_len]);5295 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
5298 // `repeat` instructions matching this loop will branch to5298 // `repeat` instructions matching this loop will branch to
5299 // this label. Since we need a label for arbitrary `repeat`5299 // this label. Since we need a label for arbitrary `repeat`
5300 // anyway, there's actually no need to use a "real" looping5300 // anyway, there's actually no need to use a "real" looping
5301 // construct at all!5301 // construct at all!
5302 try bw.print("zig_loop_{d}:", .{@intFromEnum(inst)});5302 try w.print("zig_loop_{d}:", .{@intFromEnum(inst)});
5303 try f.object.newline();5303 try f.object.newline();
5304 try genBodyInner(f, body); // no need to restore state, we're noreturn5304 try genBodyInner(f, body); // no need to restore state, we're noreturn
5305}5305}
...@@ -5312,11 +5312,11 @@ fn airCondBr(f: *Function, inst: Air.Inst.Index) !void {...@@ -5312,11 +5312,11 @@ fn airCondBr(f: *Function, inst: Air.Inst.Index) !void {
5312 const then_body: []const Air.Inst.Index = @ptrCast(f.air.extra.items[extra.end..][0..extra.data.then_body_len]);5312 const then_body: []const Air.Inst.Index = @ptrCast(f.air.extra.items[extra.end..][0..extra.data.then_body_len]);
5313 const else_body: []const Air.Inst.Index = @ptrCast(f.air.extra.items[extra.end + then_body.len ..][0..extra.data.else_body_len]);5313 const else_body: []const Air.Inst.Index = @ptrCast(f.air.extra.items[extra.end + then_body.len ..][0..extra.data.else_body_len]);
5314 const liveness_condbr = f.liveness.getCondBr(inst);5314 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 (");5317 try w.writeAll("if (");
5318 try f.writeCValue(bw, cond, .Other);5318 try f.writeCValue(w, cond, .Other);
5319 try bw.writeAll(") ");5319 try w.writeAll(") ");
53205320
5321 try genBodyResolveState(f, inst, liveness_condbr.then_deaths, then_body, false);5321 try genBodyResolveState(f, inst, liveness_condbr.then_deaths, then_body, false);
5322 try f.object.newline();5322 try f.object.newline();
...@@ -5345,7 +5345,7 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void...@@ -5345,7 +5345,7 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void
5345 const init_condition = try f.resolveInst(switch_br.operand);5345 const init_condition = try f.resolveInst(switch_br.operand);
5346 try reap(f, inst, &.{switch_br.operand});5346 try reap(f, inst, &.{switch_br.operand});
5347 const condition_ty = f.typeOf(switch_br.operand);5347 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
5350 // For dispatches, we will create a local alloc to contain the condition value.5350 // For dispatches, we will create a local alloc to contain the condition value.
5351 // This may not result in optimal codegen for switch loops, but it minimizes the5351 // 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...@@ -5353,7 +5353,7 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void
5353 const condition = if (is_dispatch_loop) cond: {5353 const condition = if (is_dispatch_loop) cond: {
5354 const new_local = try f.allocLocal(inst, condition_ty);5354 const new_local = try f.allocLocal(inst, condition_ty);
5355 try f.copyCValue(try f.ctypeFromType(condition_ty, .complete), new_local, init_condition);5355 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)});
5357 try f.object.newline();5357 try f.object.newline();
5358 try f.loop_switch_conds.put(gpa, inst, new_local.new_local);5358 try f.loop_switch_conds.put(gpa, inst, new_local.new_local);
5359 break :cond new_local;5359 break :cond new_local;
...@@ -5363,7 +5363,7 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void...@@ -5363,7 +5363,7 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void
5363 assert(f.loop_switch_conds.remove(inst));5363 assert(f.loop_switch_conds.remove(inst));
5364 };5364 };
53655365
5366 try bw.writeAll("switch (");5366 try w.writeAll("switch (");
53675367
5368 const lowered_condition_ty: Type = if (condition_ty.toIntern() == .bool_type)5368 const lowered_condition_ty: Type = if (condition_ty.toIntern() == .bool_type)
5369 .u15369 .u1
...@@ -5372,12 +5372,12 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void...@@ -5372,12 +5372,12 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void
5372 else5372 else
5373 condition_ty;5373 condition_ty;
5374 if (condition_ty.toIntern() != lowered_condition_ty.toIntern()) {5374 if (condition_ty.toIntern() != lowered_condition_ty.toIntern()) {
5375 try bw.writeByte('(');5375 try w.writeByte('(');
5376 try f.renderType(bw, lowered_condition_ty);5376 try f.renderType(w, lowered_condition_ty);
5377 try bw.writeByte(')');5377 try w.writeByte(')');
5378 }5378 }
5379 try f.writeCValue(bw, condition, .Other);5379 try f.writeCValue(w, condition, .Other);
5380 try bw.writeAll(") {");5380 try w.writeAll(") {");
5381 f.object.indent();5381 f.object.indent();
53825382
5383 const liveness = try f.liveness.getSwitchBr(gpa, inst, switch_br.cases_len + 1);5383 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...@@ -5392,36 +5392,36 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void
5392 }5392 }
5393 for (case.items) |item| {5393 for (case.items) |item| {
5394 try f.object.newline();5394 try f.object.newline();
5395 try bw.writeAll("case ");5395 try w.writeAll("case ");
5396 const item_value = try f.air.value(item, pt);5396 const item_value = try f.air.value(item, pt);
5397 // If `item_value` is a pointer with a known integer address, print the address5397 // If `item_value` is a pointer with a known integer address, print the address
5398 // with no cast to avoid a warning.5398 // with no cast to avoid a warning.
5399 write_val: {5399 write_val: {
5400 if (condition_ty.isPtrAtRuntime(zcu)) {5400 if (condition_ty.isPtrAtRuntime(zcu)) {
5401 if (item_value.?.getUnsignedInt(zcu)) |item_int| {5401 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))});
5403 break :write_val;5403 break :write_val;
5404 }5404 }
5405 }5405 }
5406 if (condition_ty.isPtrAtRuntime(zcu)) {5406 if (condition_ty.isPtrAtRuntime(zcu)) {
5407 try bw.writeByte('(');5407 try w.writeByte('(');
5408 try f.renderType(bw, .usize);5408 try f.renderType(w, .usize);
5409 try bw.writeByte(')');5409 try w.writeByte(')');
5410 }5410 }
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);
5412 }5412 }
5413 try bw.writeByte(':');5413 try w.writeByte(':');
5414 }5414 }
5415 try bw.writeAll(" {");5415 try w.writeAll(" {");
5416 f.object.indent();5416 f.object.indent();
5417 try f.object.newline();5417 try f.object.newline();
5418 if (is_dispatch_loop) {5418 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 });
5420 try f.object.newline();5420 try f.object.newline();
5421 }5421 }
5422 try genBodyResolveState(f, inst, liveness.deaths[case.idx], case.body, true);5422 try genBodyResolveState(f, inst, liveness.deaths[case.idx], case.body, true);
5423 try f.object.outdent();5423 try f.object.outdent();
5424 try bw.writeByte('}');5424 try w.writeByte('}');
5425 if (f.object.dg.expected_block) |_|5425 if (f.object.dg.expected_block) |_|
5426 return f.fail("runtime code not allowed in naked function", .{});5426 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...@@ -5431,7 +5431,7 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void
5431 const else_body = it.elseBody();5431 const else_body = it.elseBody();
5432 try f.object.newline();5432 try f.object.newline();
54335433
5434 try bw.writeAll("default: ");5434 try w.writeAll("default: ");
5435 if (any_range_cases) {5435 if (any_range_cases) {
5436 // We will iterate the cases again to handle those with ranges, and generate5436 // We will iterate the cases again to handle those with ranges, and generate
5437 // code using conditions rather than switch cases for such cases.5437 // 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...@@ -5439,41 +5439,41 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void
5439 while (it.next()) |case| {5439 while (it.next()) |case| {
5440 if (case.ranges.len == 0) continue; // handled above5440 if (case.ranges.len == 0) continue; // handled above
54415441
5442 try bw.writeAll("if (");5442 try w.writeAll("if (");
5443 for (case.items, 0..) |item, item_i| {5443 for (case.items, 0..) |item, item_i| {
5444 if (item_i != 0) try bw.writeAll(" || ");5444 if (item_i != 0) try w.writeAll(" || ");
5445 try f.writeCValue(bw, condition, .Other);5445 try f.writeCValue(w, condition, .Other);
5446 try bw.writeAll(" == ");5446 try w.writeAll(" == ");
5447 try f.object.dg.renderValue(bw, (try f.air.value(item, pt)).?, .Other);5447 try f.object.dg.renderValue(w, (try f.air.value(item, pt)).?, .Other);
5448 }5448 }
5449 for (case.ranges, 0..) |range, range_i| {5449 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(" || ");
5451 // "(x >= lower && x <= upper)"5451 // "(x >= lower && x <= upper)"
5452 try bw.writeByte('(');5452 try w.writeByte('(');
5453 try f.writeCValue(bw, condition, .Other);5453 try f.writeCValue(w, condition, .Other);
5454 try bw.writeAll(" >= ");5454 try w.writeAll(" >= ");
5455 try f.object.dg.renderValue(bw, (try f.air.value(range[0], pt)).?, .Other);5455 try f.object.dg.renderValue(w, (try f.air.value(range[0], pt)).?, .Other);
5456 try bw.writeAll(" && ");5456 try w.writeAll(" && ");
5457 try f.writeCValue(bw, condition, .Other);5457 try f.writeCValue(w, condition, .Other);
5458 try bw.writeAll(" <= ");5458 try w.writeAll(" <= ");
5459 try f.object.dg.renderValue(bw, (try f.air.value(range[1], pt)).?, .Other);5459 try f.object.dg.renderValue(w, (try f.air.value(range[1], pt)).?, .Other);
5460 try bw.writeByte(')');5460 try w.writeByte(')');
5461 }5461 }
5462 try bw.writeAll(") {");5462 try w.writeAll(") {");
5463 f.object.indent();5463 f.object.indent();
5464 try f.object.newline();5464 try f.object.newline();
5465 if (is_dispatch_loop) {5465 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 });
5467 }5467 }
5468 try genBodyResolveState(f, inst, liveness.deaths[case.idx], case.body, true);5468 try genBodyResolveState(f, inst, liveness.deaths[case.idx], case.body, true);
5469 try f.object.outdent();5469 try f.object.outdent();
5470 try bw.writeByte('}');5470 try w.writeByte('}');
5471 if (f.object.dg.expected_block) |_|5471 if (f.object.dg.expected_block) |_|
5472 return f.fail("runtime code not allowed in naked function", .{});5472 return f.fail("runtime code not allowed in naked function", .{});
5473 }5473 }
5474 }5474 }
5475 if (is_dispatch_loop) {5475 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 });
5477 }5477 }
5478 if (else_body.len > 0) {5478 if (else_body.len > 0) {
5479 // Note that this must be the last case, so we do not need to use `genBodyResolveState` since5479 // 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...@@ -5487,7 +5487,7 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void
5487 } else try airUnreach(&f.object);5487 } else try airUnreach(&f.object);
5488 try f.object.newline();5488 try f.object.newline();
5489 try f.object.outdent();5489 try f.object.outdent();
5490 try bw.writeAll("}\n");5490 try w.writeAll("}\n");
5491}5491}
54925492
5493fn asmInputNeedsLocal(f: *Function, constraint: []const u8, value: CValue) bool {5493fn asmInputNeedsLocal(f: *Function, constraint: []const u8, value: CValue) bool {
...@@ -5525,7 +5525,7 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -5525,7 +5525,7 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
5525 extra_i += inputs.len;5525 extra_i += inputs.len;
55265526
5527 const result = result: {5527 const result = result: {
5528 const bw = &f.object.code.buffered_writer;5528 const w = &f.object.code.buffered_writer;
5529 const inst_ty = f.typeOfIndex(inst);5529 const inst_ty = f.typeOfIndex(inst);
5530 const inst_local = if (inst_ty.hasRuntimeBitsIgnoreComptime(zcu)) local: {5530 const inst_local = if (inst_ty.hasRuntimeBitsIgnoreComptime(zcu)) local: {
5531 const inst_local = try f.allocLocalValue(.{5531 const inst_local = try f.allocLocalValue(.{
...@@ -5533,10 +5533,10 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -5533,10 +5533,10 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
5533 .alignas = CType.AlignAs.fromAbiAlignment(inst_ty.abiAlignment(zcu)),5533 .alignas = CType.AlignAs.fromAbiAlignment(inst_ty.abiAlignment(zcu)),
5534 });5534 });
5535 if (f.wantSafety()) {5535 if (f.wantSafety()) {
5536 try f.writeCValue(bw, inst_local, .Other);5536 try f.writeCValue(w, inst_local, .Other);
5537 try bw.writeAll(" = ");5537 try w.writeAll(" = ");
5538 try f.writeCValue(bw, .{ .undef = inst_ty }, .Other);5538 try f.writeCValue(w, .{ .undef = inst_ty }, .Other);
5539 try bw.writeByte(';');5539 try w.writeByte(';');
5540 try f.object.newline();5540 try f.object.newline();
5541 }5541 }
5542 break :local inst_local;5542 break :local inst_local;
...@@ -5561,21 +5561,21 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -5561,21 +5561,21 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
5561 const is_reg = constraint[1] == '{';5561 const is_reg = constraint[1] == '{';
5562 if (is_reg) {5562 if (is_reg) {
5563 const output_ty = if (output == .none) inst_ty else f.typeOf(output).childType(zcu);5563 const output_ty = if (output == .none) inst_ty else f.typeOf(output).childType(zcu);
5564 try bw.writeAll("register ");5564 try w.writeAll("register ");
5565 const output_local = try f.allocLocalValue(.{5565 const output_local = try f.allocLocalValue(.{
5566 .ctype = try f.ctypeFromType(output_ty, .complete),5566 .ctype = try f.ctypeFromType(output_ty, .complete),
5567 .alignas = CType.AlignAs.fromAbiAlignment(output_ty.abiAlignment(zcu)),5567 .alignas = CType.AlignAs.fromAbiAlignment(output_ty.abiAlignment(zcu)),
5568 });5568 });
5569 try f.allocs.put(gpa, output_local.new_local, false);5569 try f.allocs.put(gpa, output_local.new_local, false);
5570 try f.object.dg.renderTypeAndName(bw, output_ty, output_local, .{}, .none, .complete);5570 try f.object.dg.renderTypeAndName(w, output_ty, output_local, .{}, .none, .complete);
5571 try bw.writeAll(" __asm(\"");5571 try w.writeAll(" __asm(\"");
5572 try bw.writeAll(constraint["={".len .. constraint.len - "}".len]);5572 try w.writeAll(constraint["={".len .. constraint.len - "}".len]);
5573 try bw.writeAll("\")");5573 try w.writeAll("\")");
5574 if (f.wantSafety()) {5574 if (f.wantSafety()) {
5575 try bw.writeAll(" = ");5575 try w.writeAll(" = ");
5576 try f.writeCValue(bw, .{ .undef = output_ty }, .Other);5576 try f.writeCValue(w, .{ .undef = output_ty }, .Other);
5577 }5577 }
5578 try bw.writeByte(';');5578 try w.writeByte(';');
5579 try f.object.newline();5579 try f.object.newline();
5580 }5580 }
5581 }5581 }
...@@ -5597,21 +5597,21 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -5597,21 +5597,21 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
5597 const input_val = try f.resolveInst(input);5597 const input_val = try f.resolveInst(input);
5598 if (asmInputNeedsLocal(f, constraint, input_val)) {5598 if (asmInputNeedsLocal(f, constraint, input_val)) {
5599 const input_ty = f.typeOf(input);5599 const input_ty = f.typeOf(input);
5600 if (is_reg) try bw.writeAll("register ");5600 if (is_reg) try w.writeAll("register ");
5601 const input_local = try f.allocLocalValue(.{5601 const input_local = try f.allocLocalValue(.{
5602 .ctype = try f.ctypeFromType(input_ty, .complete),5602 .ctype = try f.ctypeFromType(input_ty, .complete),
5603 .alignas = CType.AlignAs.fromAbiAlignment(input_ty.abiAlignment(zcu)),5603 .alignas = CType.AlignAs.fromAbiAlignment(input_ty.abiAlignment(zcu)),
5604 });5604 });
5605 try f.allocs.put(gpa, input_local.new_local, false);5605 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);
5607 if (is_reg) {5607 if (is_reg) {
5608 try bw.writeAll(" __asm(\"");5608 try w.writeAll(" __asm(\"");
5609 try bw.writeAll(constraint["{".len .. constraint.len - "}".len]);5609 try w.writeAll(constraint["{".len .. constraint.len - "}".len]);
5610 try bw.writeAll("\")");5610 try w.writeAll("\")");
5611 }5611 }
5612 try bw.writeAll(" = ");5612 try w.writeAll(" = ");
5613 try f.writeCValue(bw, input_val, .Other);5613 try f.writeCValue(w, input_val, .Other);
5614 try bw.writeByte(';');5614 try w.writeByte(';');
5615 try f.object.newline();5615 try f.object.newline();
5616 }5616 }
5617 }5617 }
...@@ -5672,14 +5672,14 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -5672,14 +5672,14 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
5672 }5672 }
5673 }5673 }
56745674
5675 try bw.writeAll("__asm");5675 try w.writeAll("__asm");
5676 if (is_volatile) try bw.writeAll(" volatile");5676 if (is_volatile) try w.writeAll(" volatile");
5677 try bw.print("({fs}", .{fmtStringLiteral(fixed_asm_source[0..dst_i], null)});5677 try w.print("({fs}", .{fmtStringLiteral(fixed_asm_source[0..dst_i], null)});
5678 }5678 }
56795679
5680 extra_i = constraints_extra_begin;5680 extra_i = constraints_extra_begin;
5681 var locals_index = locals_begin;5681 var locals_index = locals_begin;
5682 try bw.writeByte(':');5682 try w.writeByte(':');
5683 for (outputs, 0..) |output, index| {5683 for (outputs, 0..) |output, index| {
5684 const extra_bytes = mem.sliceAsBytes(f.air.extra.items[extra_i..]);5684 const extra_bytes = mem.sliceAsBytes(f.air.extra.items[extra_i..]);
5685 const constraint = mem.sliceTo(extra_bytes, 0);5685 const constraint = mem.sliceTo(extra_bytes, 0);
...@@ -5688,22 +5688,22 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -5688,22 +5688,22 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
5688 // for the string, we still use the next u32 for the null terminator.5688 // for the string, we still use the next u32 for the null terminator.
5689 extra_i += (constraint.len + name.len + (2 + 3)) / 4;5689 extra_i += (constraint.len + name.len + (2 + 3)) / 4;
56905690
5691 if (index > 0) try bw.writeByte(',');5691 if (index > 0) try w.writeByte(',');
5692 try bw.writeByte(' ');5692 try w.writeByte(' ');
5693 if (!mem.eql(u8, name, "_")) try bw.print("[{s}]", .{name});5693 if (!mem.eql(u8, name, "_")) try w.print("[{s}]", .{name});
5694 const is_reg = constraint[1] == '{';5694 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)});
5696 if (is_reg) {5696 if (is_reg) {
5697 try f.writeCValue(bw, .{ .local = locals_index }, .Other);5697 try f.writeCValue(w, .{ .local = locals_index }, .Other);
5698 locals_index += 1;5698 locals_index += 1;
5699 } else if (output == .none) {5699 } else if (output == .none) {
5700 try f.writeCValue(bw, inst_local, .FunctionArgument);5700 try f.writeCValue(w, inst_local, .FunctionArgument);
5701 } else {5701 } else {
5702 try f.writeCValueDeref(bw, try f.resolveInst(output));5702 try f.writeCValueDeref(w, try f.resolveInst(output));
5703 }5703 }
5704 try bw.writeByte(')');5704 try w.writeByte(')');
5705 }5705 }
5706 try bw.writeByte(':');5706 try w.writeByte(':');
5707 for (inputs, 0..) |input, index| {5707 for (inputs, 0..) |input, index| {
5708 const extra_bytes = mem.sliceAsBytes(f.air.extra.items[extra_i..]);5708 const extra_bytes = mem.sliceAsBytes(f.air.extra.items[extra_i..]);
5709 const constraint = mem.sliceTo(extra_bytes, 0);5709 const constraint = mem.sliceTo(extra_bytes, 0);
...@@ -5712,21 +5712,21 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -5712,21 +5712,21 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
5712 // for the string, we still use the next u32 for the null terminator.5712 // for the string, we still use the next u32 for the null terminator.
5713 extra_i += (constraint.len + name.len + (2 + 3)) / 4;5713 extra_i += (constraint.len + name.len + (2 + 3)) / 4;
57145714
5715 if (index > 0) try bw.writeByte(',');5715 if (index > 0) try w.writeByte(',');
5716 try bw.writeByte(' ');5716 try w.writeByte(' ');
5717 if (!mem.eql(u8, name, "_")) try bw.print("[{s}]", .{name});5717 if (!mem.eql(u8, name, "_")) try w.print("[{s}]", .{name});
57185718
5719 const is_reg = constraint[0] == '{';5719 const is_reg = constraint[0] == '{';
5720 const input_val = try f.resolveInst(input);5720 const input_val = try f.resolveInst(input);
5721 try bw.print("{fs}(", .{fmtStringLiteral(if (is_reg) "r" else constraint, null)});5721 try w.print("{fs}(", .{fmtStringLiteral(if (is_reg) "r" else constraint, null)});
5722 try f.writeCValue(bw, if (asmInputNeedsLocal(f, constraint, input_val)) local: {5722 try f.writeCValue(w, if (asmInputNeedsLocal(f, constraint, input_val)) local: {
5723 const input_local_idx = locals_index;5723 const input_local_idx = locals_index;
5724 locals_index += 1;5724 locals_index += 1;
5725 break :local .{ .local = input_local_idx };5725 break :local .{ .local = input_local_idx };
5726 } else input_val, .Other);5726 } else input_val, .Other);
5727 try bw.writeByte(')');5727 try w.writeByte(')');
5728 }5728 }
5729 try bw.writeByte(':');5729 try w.writeByte(':');
5730 for (0..clobbers_len) |clobber_i| {5730 for (0..clobbers_len) |clobber_i| {
5731 const clobber = mem.sliceTo(mem.sliceAsBytes(f.air.extra.items[extra_i..]), 0);5731 const clobber = mem.sliceTo(mem.sliceAsBytes(f.air.extra.items[extra_i..]), 0);
5732 // This equation accounts for the fact that even if we have exactly 4 bytes5732 // 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 {...@@ -5735,10 +5735,10 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
57355735
5736 if (clobber.len == 0) continue;5736 if (clobber.len == 0) continue;
57375737
5738 if (clobber_i > 0) try bw.writeByte(',');5738 if (clobber_i > 0) try w.writeByte(',');
5739 try bw.print(" {fs}", .{fmtStringLiteral(clobber, null)});5739 try w.print(" {fs}", .{fmtStringLiteral(clobber, null)});
5740 }5740 }
5741 try bw.writeAll(");");5741 try w.writeAll(");");
5742 try f.object.newline();5742 try f.object.newline();
57435743
5744 extra_i = constraints_extra_begin;5744 extra_i = constraints_extra_begin;
...@@ -5753,14 +5753,14 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -5753,14 +5753,14 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue {
57535753
5754 const is_reg = constraint[1] == '{';5754 const is_reg = constraint[1] == '{';
5755 if (is_reg) {5755 if (is_reg) {
5756 try f.writeCValueDeref(bw, if (output == .none)5756 try f.writeCValueDeref(w, if (output == .none)
5757 .{ .local_ref = inst_local.new_local }5757 .{ .local_ref = inst_local.new_local }
5758 else5758 else
5759 try f.resolveInst(output));5759 try f.resolveInst(output));
5760 try bw.writeAll(" = ");5760 try w.writeAll(" = ");
5761 try f.writeCValue(bw, .{ .local = locals_index }, .Other);5761 try f.writeCValue(w, .{ .local = locals_index }, .Other);
5762 locals_index += 1;5762 locals_index += 1;
5763 try bw.writeByte(';');5763 try w.writeByte(';');
5764 try f.object.newline();5764 try f.object.newline();
5765 }5765 }
5766 }5766 }
...@@ -5791,14 +5791,14 @@ fn airIsNull(...@@ -5791,14 +5791,14 @@ fn airIsNull(
5791 const ctype_pool = &f.object.dg.ctype_pool;5791 const ctype_pool = &f.object.dg.ctype_pool;
5792 const un_op = f.air.instructions.items(.data)[@intFromEnum(inst)].un_op;5792 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;
5795 const operand = try f.resolveInst(un_op);5795 const operand = try f.resolveInst(un_op);
5796 try reap(f, inst, &.{un_op});5796 try reap(f, inst, &.{un_op});
57975797
5798 const local = try f.allocLocal(inst, .bool);5798 const local = try f.allocLocal(inst, .bool);
5799 const a = try Assignment.start(f, bw, .bool);5799 const a = try Assignment.start(f, w, .bool);
5800 try f.writeCValue(bw, local, .Other);5800 try f.writeCValue(w, local, .Other);
5801 try a.assign(f, bw);5801 try a.assign(f, w);
58025802
5803 const operand_ty = f.typeOf(un_op);5803 const operand_ty = f.typeOf(un_op);
5804 const optional_ty = if (is_ptr) operand_ty.childType(zcu) else operand_ty;5804 const optional_ty = if (is_ptr) operand_ty.childType(zcu) else operand_ty;
...@@ -5806,9 +5806,9 @@ fn airIsNull(...@@ -5806,9 +5806,9 @@ fn airIsNull(
5806 const rhs = switch (opt_ctype.info(ctype_pool)) {5806 const rhs = switch (opt_ctype.info(ctype_pool)) {
5807 .basic, .pointer => rhs: {5807 .basic, .pointer => rhs: {
5808 if (is_ptr)5808 if (is_ptr)
5809 try f.writeCValueDeref(bw, operand)5809 try f.writeCValueDeref(w, operand)
5810 else5810 else
5811 try f.writeCValue(bw, operand, .Other);5811 try f.writeCValue(w, operand, .Other);
5812 break :rhs if (opt_ctype.isBool())5812 break :rhs if (opt_ctype.isBool())
5813 "true"5813 "true"
5814 else if (opt_ctype.isInteger())5814 else if (opt_ctype.isInteger())
...@@ -5820,24 +5820,24 @@ fn airIsNull(...@@ -5820,24 +5820,24 @@ fn airIsNull(
5820 .aggregate => |aggregate| switch (aggregate.fields.at(0, ctype_pool).name.index) {5820 .aggregate => |aggregate| switch (aggregate.fields.at(0, ctype_pool).name.index) {
5821 .is_null, .payload => rhs: {5821 .is_null, .payload => rhs: {
5822 if (is_ptr)5822 if (is_ptr)
5823 try f.writeCValueDerefMember(bw, operand, .{ .identifier = "is_null" })5823 try f.writeCValueDerefMember(w, operand, .{ .identifier = "is_null" })
5824 else5824 else
5825 try f.writeCValueMember(bw, operand, .{ .identifier = "is_null" });5825 try f.writeCValueMember(w, operand, .{ .identifier = "is_null" });
5826 break :rhs "true";5826 break :rhs "true";
5827 },5827 },
5828 .ptr, .len => rhs: {5828 .ptr, .len => rhs: {
5829 if (is_ptr)5829 if (is_ptr)
5830 try f.writeCValueDerefMember(bw, operand, .{ .identifier = "ptr" })5830 try f.writeCValueDerefMember(w, operand, .{ .identifier = "ptr" })
5831 else5831 else
5832 try f.writeCValueMember(bw, operand, .{ .identifier = "ptr" });5832 try f.writeCValueMember(w, operand, .{ .identifier = "ptr" });
5833 break :rhs "NULL";5833 break :rhs "NULL";
5834 },5834 },
5835 else => unreachable,5835 else => unreachable,
5836 },5836 },
5837 };5837 };
5838 try bw.writeAll(compareOperatorC(operator));5838 try w.writeAll(compareOperatorC(operator));
5839 try bw.writeAll(rhs);5839 try w.writeAll(rhs);
5840 try a.end(f, bw);5840 try a.end(f, w);
5841 return local;5841 return local;
5842}5842}
58435843
...@@ -5859,16 +5859,16 @@ fn airOptionalPayload(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValue...@@ -5859,16 +5859,16 @@ fn airOptionalPayload(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValue
5859 .aligned, .array, .vector, .fwd_decl, .function => unreachable,5859 .aligned, .array, .vector, .fwd_decl, .function => unreachable,
5860 .aggregate => |aggregate| switch (aggregate.fields.at(0, ctype_pool).name.index) {5860 .aggregate => |aggregate| switch (aggregate.fields.at(0, ctype_pool).name.index) {
5861 .is_null, .payload => {5861 .is_null, .payload => {
5862 const bw = &f.object.code.buffered_writer;5862 const w = &f.object.code.buffered_writer;
5863 const local = try f.allocLocal(inst, inst_ty);5863 const local = try f.allocLocal(inst, inst_ty);
5864 const a = try Assignment.start(f, bw, try f.ctypeFromType(inst_ty, .complete));5864 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete));
5865 try f.writeCValue(bw, local, .Other);5865 try f.writeCValue(w, local, .Other);
5866 try a.assign(f, bw);5866 try a.assign(f, w);
5867 if (is_ptr) {5867 if (is_ptr) {
5868 try bw.writeByte('&');5868 try w.writeByte('&');
5869 try f.writeCValueDerefMember(bw, operand, .{ .identifier = "payload" });5869 try f.writeCValueDerefMember(w, operand, .{ .identifier = "payload" });
5870 } else try f.writeCValueMember(bw, operand, .{ .identifier = "payload" });5870 } else try f.writeCValueMember(w, operand, .{ .identifier = "payload" });
5871 try a.end(f, bw);5871 try a.end(f, w);
5872 return local;5872 return local;
5873 },5873 },
5874 .ptr, .len => return f.moveCValue(inst, inst_ty, operand),5874 .ptr, .len => return f.moveCValue(inst, inst_ty, operand),
...@@ -5881,7 +5881,7 @@ fn airOptionalPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -5881,7 +5881,7 @@ fn airOptionalPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue {
5881 const pt = f.object.dg.pt;5881 const pt = f.object.dg.pt;
5882 const zcu = pt.zcu;5882 const zcu = pt.zcu;
5883 const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;5883 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;
5885 const operand = try f.resolveInst(ty_op.operand);5885 const operand = try f.resolveInst(ty_op.operand);
5886 try reap(f, inst, &.{ty_op.operand});5886 try reap(f, inst, &.{ty_op.operand});
5887 const operand_ty = f.typeOf(ty_op.operand);5887 const operand_ty = f.typeOf(ty_op.operand);
...@@ -5890,40 +5890,40 @@ fn airOptionalPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -5890,40 +5890,40 @@ fn airOptionalPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue {
5890 const opt_ctype = try f.ctypeFromType(operand_ty.childType(zcu), .complete);5890 const opt_ctype = try f.ctypeFromType(operand_ty.childType(zcu), .complete);
5891 switch (opt_ctype.info(&f.object.dg.ctype_pool)) {5891 switch (opt_ctype.info(&f.object.dg.ctype_pool)) {
5892 .basic => {5892 .basic => {
5893 const a = try Assignment.start(f, bw, opt_ctype);5893 const a = try Assignment.start(f, w, opt_ctype);
5894 try f.writeCValueDeref(bw, operand);5894 try f.writeCValueDeref(w, operand);
5895 try a.assign(f, bw);5895 try a.assign(f, w);
5896 try f.object.dg.renderValue(bw, Value.false, .Other);5896 try f.object.dg.renderValue(w, Value.false, .Other);
5897 try a.end(f, bw);5897 try a.end(f, w);
5898 return .none;5898 return .none;
5899 },5899 },
5900 .pointer => {5900 .pointer => {
5901 if (f.liveness.isUnused(inst)) return .none;5901 if (f.liveness.isUnused(inst)) return .none;
5902 const local = try f.allocLocal(inst, inst_ty);5902 const local = try f.allocLocal(inst, inst_ty);
5903 const a = try Assignment.start(f, bw, opt_ctype);5903 const a = try Assignment.start(f, w, opt_ctype);
5904 try f.writeCValue(bw, local, .Other);5904 try f.writeCValue(w, local, .Other);
5905 try a.assign(f, bw);5905 try a.assign(f, w);
5906 try f.writeCValue(bw, operand, .Other);5906 try f.writeCValue(w, operand, .Other);
5907 try a.end(f, bw);5907 try a.end(f, w);
5908 return local;5908 return local;
5909 },5909 },
5910 .aligned, .array, .vector, .fwd_decl, .function => unreachable,5910 .aligned, .array, .vector, .fwd_decl, .function => unreachable,
5911 .aggregate => {5911 .aggregate => {
5912 {5912 {
5913 const a = try Assignment.start(f, bw, opt_ctype);5913 const a = try Assignment.start(f, w, opt_ctype);
5914 try f.writeCValueDerefMember(bw, operand, .{ .identifier = "is_null" });5914 try f.writeCValueDerefMember(w, operand, .{ .identifier = "is_null" });
5915 try a.assign(f, bw);5915 try a.assign(f, w);
5916 try f.object.dg.renderValue(bw, Value.false, .Other);5916 try f.object.dg.renderValue(w, Value.false, .Other);
5917 try a.end(f, bw);5917 try a.end(f, w);
5918 }5918 }
5919 if (f.liveness.isUnused(inst)) return .none;5919 if (f.liveness.isUnused(inst)) return .none;
5920 const local = try f.allocLocal(inst, inst_ty);5920 const local = try f.allocLocal(inst, inst_ty);
5921 const a = try Assignment.start(f, bw, opt_ctype);5921 const a = try Assignment.start(f, w, opt_ctype);
5922 try f.writeCValue(bw, local, .Other);5922 try f.writeCValue(w, local, .Other);
5923 try a.assign(f, bw);5923 try a.assign(f, w);
5924 try bw.writeByte('&');5924 try w.writeByte('&');
5925 try f.writeCValueDerefMember(bw, operand, .{ .identifier = "payload" });5925 try f.writeCValueDerefMember(w, operand, .{ .identifier = "payload" });
5926 try a.end(f, bw);5926 try a.end(f, w);
5927 return local;5927 return local;
5928 },5928 },
5929 }5929 }
...@@ -6031,42 +6031,42 @@ fn airFieldParentPtr(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -6031,42 +6031,42 @@ fn airFieldParentPtr(f: *Function, inst: Air.Inst.Index) !CValue {
6031 const field_ptr_val = try f.resolveInst(extra.field_ptr);6031 const field_ptr_val = try f.resolveInst(extra.field_ptr);
6032 try reap(f, inst, &.{extra.field_ptr});6032 try reap(f, inst, &.{extra.field_ptr});
60336033
6034 const bw = &f.object.code.buffered_writer;6034 const w = &f.object.code.buffered_writer;
6035 const local = try f.allocLocal(inst, container_ptr_ty);6035 const local = try f.allocLocal(inst, container_ptr_ty);
6036 try f.writeCValue(bw, local, .Other);6036 try f.writeCValue(w, local, .Other);
6037 try bw.writeAll(" = (");6037 try w.writeAll(" = (");
6038 try f.renderType(bw, container_ptr_ty);6038 try f.renderType(w, container_ptr_ty);
6039 try bw.writeByte(')');6039 try w.writeByte(')');
60406040
6041 switch (fieldLocation(container_ptr_ty, field_ptr_ty, extra.field_index, pt)) {6041 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),
6043 .field => |field| {6043 .field => |field| {
6044 const u8_ptr_ty = try pt.adjustPtrTypeChild(field_ptr_ty, .u8);6044 const u8_ptr_ty = try pt.adjustPtrTypeChild(field_ptr_ty, .u8);
60456045
6046 try bw.writeAll("((");6046 try w.writeAll("((");
6047 try f.renderType(bw, u8_ptr_ty);6047 try f.renderType(w, u8_ptr_ty);
6048 try bw.writeByte(')');6048 try w.writeByte(')');
6049 try f.writeCValue(bw, field_ptr_val, .Other);6049 try f.writeCValue(w, field_ptr_val, .Other);
6050 try bw.writeAll(" - offsetof(");6050 try w.writeAll(" - offsetof(");
6051 try f.renderType(bw, container_ty);6051 try f.renderType(w, container_ty);
6052 try bw.writeAll(", ");6052 try w.writeAll(", ");
6053 try f.writeCValue(bw, field, .Other);6053 try f.writeCValue(w, field, .Other);
6054 try bw.writeAll("))");6054 try w.writeAll("))");
6055 },6055 },
6056 .byte_offset => |byte_offset| {6056 .byte_offset => |byte_offset| {
6057 const u8_ptr_ty = try pt.adjustPtrTypeChild(field_ptr_ty, .u8);6057 const u8_ptr_ty = try pt.adjustPtrTypeChild(field_ptr_ty, .u8);
60586058
6059 try bw.writeAll("((");6059 try w.writeAll("((");
6060 try f.renderType(bw, u8_ptr_ty);6060 try f.renderType(w, u8_ptr_ty);
6061 try bw.writeByte(')');6061 try w.writeByte(')');
6062 try f.writeCValue(bw, field_ptr_val, .Other);6062 try f.writeCValue(w, field_ptr_val, .Other);
6063 try bw.print(" - {f})", .{6063 try w.print(" - {f})", .{
6064 try f.fmtIntLiteral(try pt.intValue(.usize, byte_offset)),6064 try f.fmtIntLiteral(try pt.intValue(.usize, byte_offset)),
6065 });6065 });
6066 },6066 },
6067 }6067 }
60686068
6069 try bw.writeByte(';');6069 try w.writeByte(';');
6070 try f.object.newline();6070 try f.object.newline();
6071 return local;6071 return local;
6072}6072}
...@@ -6086,33 +6086,33 @@ fn fieldPtr(...@@ -6086,33 +6086,33 @@ fn fieldPtr(
6086 // Ensure complete type definition is visible before accessing fields.6086 // Ensure complete type definition is visible before accessing fields.
6087 _ = try f.ctypeFromType(container_ty, .complete);6087 _ = try f.ctypeFromType(container_ty, .complete);
60886088
6089 const bw = &f.object.code.buffered_writer;6089 const w = &f.object.code.buffered_writer;
6090 const local = try f.allocLocal(inst, field_ptr_ty);6090 const local = try f.allocLocal(inst, field_ptr_ty);
6091 try f.writeCValue(bw, local, .Other);6091 try f.writeCValue(w, local, .Other);
6092 try bw.writeAll(" = (");6092 try w.writeAll(" = (");
6093 try f.renderType(bw, field_ptr_ty);6093 try f.renderType(w, field_ptr_ty);
6094 try bw.writeByte(')');6094 try w.writeByte(')');
60956095
6096 switch (fieldLocation(container_ptr_ty, field_ptr_ty, field_index, pt)) {6096 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),
6098 .field => |field| {6098 .field => |field| {
6099 try bw.writeByte('&');6099 try w.writeByte('&');
6100 try f.writeCValueDerefMember(bw, container_ptr_val, field);6100 try f.writeCValueDerefMember(w, container_ptr_val, field);
6101 },6101 },
6102 .byte_offset => |byte_offset| {6102 .byte_offset => |byte_offset| {
6103 const u8_ptr_ty = try pt.adjustPtrTypeChild(field_ptr_ty, .u8);6103 const u8_ptr_ty = try pt.adjustPtrTypeChild(field_ptr_ty, .u8);
61046104
6105 try bw.writeAll("((");6105 try w.writeAll("((");
6106 try f.renderType(bw, u8_ptr_ty);6106 try f.renderType(w, u8_ptr_ty);
6107 try bw.writeByte(')');6107 try w.writeByte(')');
6108 try f.writeCValue(bw, container_ptr_val, .Other);6108 try f.writeCValue(w, container_ptr_val, .Other);
6109 try bw.print(" + {f})", .{6109 try w.print(" + {f})", .{
6110 try f.fmtIntLiteral(try pt.intValue(.usize, byte_offset)),6110 try f.fmtIntLiteral(try pt.intValue(.usize, byte_offset)),
6111 });6111 });
6112 },6112 },
6113 }6113 }
61146114
6115 try bw.writeByte(';');6115 try w.writeByte(';');
6116 try f.object.newline();6116 try f.object.newline();
6117 return local;6117 return local;
6118}6118}
...@@ -6133,7 +6133,7 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -6133,7 +6133,7 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue {
6133 const struct_byval = try f.resolveInst(extra.struct_operand);6133 const struct_byval = try f.resolveInst(extra.struct_operand);
6134 try reap(f, inst, &.{extra.struct_operand});6134 try reap(f, inst, &.{extra.struct_operand});
6135 const struct_ty = f.typeOf(extra.struct_operand);6135 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
6138 // Ensure complete type definition is visible before accessing fields.6138 // Ensure complete type definition is visible before accessing fields.
6139 _ = try f.ctypeFromType(struct_ty, .complete);6139 _ = try f.ctypeFromType(struct_ty, .complete);
...@@ -6160,43 +6160,43 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -6160,43 +6160,43 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue {
6160 const field_int_ty = try pt.intType(field_int_signedness, @as(u16, @intCast(inst_ty.bitSize(zcu))));6160 const field_int_ty = try pt.intType(field_int_signedness, @as(u16, @intCast(inst_ty.bitSize(zcu))));
61616161
6162 const temp_local = try f.allocLocal(inst, field_int_ty);6162 const temp_local = try f.allocLocal(inst, field_int_ty);
6163 try f.writeCValue(bw, temp_local, .Other);6163 try f.writeCValue(w, temp_local, .Other);
6164 try bw.writeAll(" = zig_wrap_");6164 try w.writeAll(" = zig_wrap_");
6165 try f.object.dg.renderTypeForBuiltinFnName(bw, field_int_ty);6165 try f.object.dg.renderTypeForBuiltinFnName(w, field_int_ty);
6166 try bw.writeAll("((");6166 try w.writeAll("((");
6167 try f.renderType(bw, field_int_ty);6167 try f.renderType(w, field_int_ty);
6168 try bw.writeByte(')');6168 try w.writeByte(')');
6169 const cant_cast = int_info.bits > 64;6169 const cant_cast = int_info.bits > 64;
6170 if (cant_cast) {6170 if (cant_cast) {
6171 if (field_int_ty.bitSize(zcu) > 64) return f.fail("TODO: C backend: implement casting between types > 64 bits", .{});6171 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_");6172 try w.writeAll("zig_lo_");
6173 try f.object.dg.renderTypeForBuiltinFnName(bw, struct_ty);6173 try f.object.dg.renderTypeForBuiltinFnName(w, struct_ty);
6174 try bw.writeByte('(');6174 try w.writeByte('(');
6175 }6175 }
6176 if (bit_offset > 0) {6176 if (bit_offset > 0) {
6177 try bw.writeAll("zig_shr_");6177 try w.writeAll("zig_shr_");
6178 try f.object.dg.renderTypeForBuiltinFnName(bw, struct_ty);6178 try f.object.dg.renderTypeForBuiltinFnName(w, struct_ty);
6179 try bw.writeByte('(');6179 try w.writeByte('(');
6180 }6180 }
6181 try f.writeCValue(bw, struct_byval, .Other);6181 try f.writeCValue(w, struct_byval, .Other);
6182 if (bit_offset > 0) try bw.print(", {f})", .{6182 if (bit_offset > 0) try w.print(", {f})", .{
6183 try f.fmtIntLiteral(try pt.intValue(bit_offset_ty, bit_offset)),6183 try f.fmtIntLiteral(try pt.intValue(bit_offset_ty, bit_offset)),
6184 });6184 });
6185 if (cant_cast) try bw.writeByte(')');6185 if (cant_cast) try w.writeByte(')');
6186 try f.object.dg.renderBuiltinInfo(bw, field_int_ty, .bits);6186 try f.object.dg.renderBuiltinInfo(w, field_int_ty, .bits);
6187 try bw.writeAll(");");6187 try w.writeAll(");");
6188 try f.object.newline();6188 try f.object.newline();
6189 if (inst_ty.eql(field_int_ty, zcu)) return temp_local;6189 if (inst_ty.eql(field_int_ty, zcu)) return temp_local;
61906190
6191 const local = try f.allocLocal(inst, inst_ty);6191 const local = try f.allocLocal(inst, inst_ty);
6192 if (local.new_local != temp_local.new_local) {6192 if (local.new_local != temp_local.new_local) {
6193 try bw.writeAll("memcpy(");6193 try w.writeAll("memcpy(");
6194 try f.writeCValue(bw, .{ .local_ref = local.new_local }, .FunctionArgument);6194 try f.writeCValue(w, .{ .local_ref = local.new_local }, .FunctionArgument);
6195 try bw.writeAll(", ");6195 try w.writeAll(", ");
6196 try f.writeCValue(bw, .{ .local_ref = temp_local.new_local }, .FunctionArgument);6196 try f.writeCValue(w, .{ .local_ref = temp_local.new_local }, .FunctionArgument);
6197 try bw.writeAll(", sizeof(");6197 try w.writeAll(", sizeof(");
6198 try f.renderType(bw, inst_ty);6198 try f.renderType(w, inst_ty);
6199 try bw.writeAll("));");6199 try w.writeAll("));");
6200 try f.object.newline();6200 try f.object.newline();
6201 }6201 }
6202 try freeLocal(f, inst, temp_local.new_local, null);6202 try freeLocal(f, inst, temp_local.new_local, null);
...@@ -6218,10 +6218,10 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -6218,10 +6218,10 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue {
6218 .@"packed" => {6218 .@"packed" => {
6219 const operand_lval = if (struct_byval == .constant) blk: {6219 const operand_lval = if (struct_byval == .constant) blk: {
6220 const operand_local = try f.allocLocal(inst, struct_ty);6220 const operand_local = try f.allocLocal(inst, struct_ty);
6221 try f.writeCValue(bw, operand_local, .Other);6221 try f.writeCValue(w, operand_local, .Other);
6222 try bw.writeAll(" = ");6222 try w.writeAll(" = ");
6223 try f.writeCValue(bw, struct_byval, .Other);6223 try f.writeCValue(w, struct_byval, .Other);
6224 try bw.writeByte(';');6224 try w.writeByte(';');
6225 try f.object.newline();6225 try f.object.newline();
6226 break :blk operand_local;6226 break :blk operand_local;
6227 } else struct_byval;6227 } else struct_byval;
...@@ -6233,13 +6233,13 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -6233,13 +6233,13 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue {
6233 },6233 },
6234 else => true,6234 else => true,
6235 }) {6235 }) {
6236 try bw.writeAll("memcpy(&");6236 try w.writeAll("memcpy(&");
6237 try f.writeCValue(bw, local, .Other);6237 try f.writeCValue(w, local, .Other);
6238 try bw.writeAll(", &");6238 try w.writeAll(", &");
6239 try f.writeCValue(bw, operand_lval, .Other);6239 try f.writeCValue(w, operand_lval, .Other);
6240 try bw.writeAll(", sizeof(");6240 try w.writeAll(", sizeof(");
6241 try f.renderType(bw, inst_ty);6241 try f.renderType(w, inst_ty);
6242 try bw.writeAll("));");6242 try w.writeAll("));");
6243 try f.object.newline();6243 try f.object.newline();
6244 }6244 }
6245 try f.freeCValue(inst, operand_lval);6245 try f.freeCValue(inst, operand_lval);
...@@ -6251,11 +6251,11 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -6251,11 +6251,11 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue {
6251 };6251 };
62526252
6253 const local = try f.allocLocal(inst, inst_ty);6253 const local = try f.allocLocal(inst, inst_ty);
6254 const a = try Assignment.start(f, bw, try f.ctypeFromType(inst_ty, .complete));6254 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete));
6255 try f.writeCValue(bw, local, .Other);6255 try f.writeCValue(w, local, .Other);
6256 try a.assign(f, bw);6256 try a.assign(f, w);
6257 try f.writeCValueMember(bw, struct_byval, field_name);6257 try f.writeCValueMember(w, struct_byval, field_name);
6258 try a.end(f, bw);6258 try a.end(f, w);
6259 return local;6259 return local;
6260}6260}
62616261
...@@ -6282,21 +6282,21 @@ fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -6282,21 +6282,21 @@ fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue {
6282 return local;6282 return local;
6283 }6283 }
62846284
6285 const bw = &f.object.code.buffered_writer;6285 const w = &f.object.code.buffered_writer;
6286 try f.writeCValue(bw, local, .Other);6286 try f.writeCValue(w, local, .Other);
6287 try bw.writeAll(" = ");6287 try w.writeAll(" = ");
62886288
6289 if (!payload_ty.hasRuntimeBits(zcu))6289 if (!payload_ty.hasRuntimeBits(zcu))
6290 try f.writeCValue(bw, operand, .Other)6290 try f.writeCValue(w, operand, .Other)
6291 else if (error_ty.errorSetIsEmpty(zcu))6291 else if (error_ty.errorSetIsEmpty(zcu))
6292 try bw.print("{f}", .{6292 try w.print("{f}", .{
6293 try f.fmtIntLiteral(try pt.intValue(try pt.errorIntType(), 0)),6293 try f.fmtIntLiteral(try pt.intValue(try pt.errorIntType(), 0)),
6294 })6294 })
6295 else if (operand_is_ptr)6295 else if (operand_is_ptr)
6296 try f.writeCValueDerefMember(bw, operand, .{ .identifier = "error" })6296 try f.writeCValueDerefMember(w, operand, .{ .identifier = "error" })
6297 else6297 else
6298 try f.writeCValueMember(bw, operand, .{ .identifier = "error" });6298 try f.writeCValueMember(w, operand, .{ .identifier = "error" });
6299 try bw.writeByte(';');6299 try w.writeByte(';');
6300 try f.object.newline();6300 try f.object.newline();
6301 return local;6301 return local;
6302}6302}
...@@ -6312,30 +6312,30 @@ fn airUnwrapErrUnionPay(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValu...@@ -6312,30 +6312,30 @@ fn airUnwrapErrUnionPay(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValu
6312 const operand_ty = f.typeOf(ty_op.operand);6312 const operand_ty = f.typeOf(ty_op.operand);
6313 const error_union_ty = if (is_ptr) operand_ty.childType(zcu) else operand_ty;6313 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;
6316 if (!error_union_ty.errorUnionPayload(zcu).hasRuntimeBits(zcu)) {6316 if (!error_union_ty.errorUnionPayload(zcu).hasRuntimeBits(zcu)) {
6317 if (!is_ptr) return .none;6317 if (!is_ptr) return .none;
63186318
6319 const local = try f.allocLocal(inst, inst_ty);6319 const local = try f.allocLocal(inst, inst_ty);
6320 try f.writeCValue(bw, local, .Other);6320 try f.writeCValue(w, local, .Other);
6321 try bw.writeAll(" = (");6321 try w.writeAll(" = (");
6322 try f.renderType(bw, inst_ty);6322 try f.renderType(w, inst_ty);
6323 try bw.writeByte(')');6323 try w.writeByte(')');
6324 try f.writeCValue(bw, operand, .Other);6324 try f.writeCValue(w, operand, .Other);
6325 try bw.writeByte(';');6325 try w.writeByte(';');
6326 try f.object.newline();6326 try f.object.newline();
6327 return local;6327 return local;
6328 }6328 }
63296329
6330 const local = try f.allocLocal(inst, inst_ty);6330 const local = try f.allocLocal(inst, inst_ty);
6331 const a = try Assignment.start(f, bw, try f.ctypeFromType(inst_ty, .complete));6331 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete));
6332 try f.writeCValue(bw, local, .Other);6332 try f.writeCValue(w, local, .Other);
6333 try a.assign(f, bw);6333 try a.assign(f, w);
6334 if (is_ptr) {6334 if (is_ptr) {
6335 try bw.writeByte('&');6335 try w.writeByte('&');
6336 try f.writeCValueDerefMember(bw, operand, .{ .identifier = "payload" });6336 try f.writeCValueDerefMember(w, operand, .{ .identifier = "payload" });
6337 } else try f.writeCValueMember(bw, operand, .{ .identifier = "payload" });6337 } else try f.writeCValueMember(w, operand, .{ .identifier = "payload" });
6338 try a.end(f, bw);6338 try a.end(f, w);
6339 return local;6339 return local;
6340}6340}
63416341
...@@ -6354,21 +6354,21 @@ fn airWrapOptional(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -6354,21 +6354,21 @@ fn airWrapOptional(f: *Function, inst: Air.Inst.Index) !CValue {
6354 .aggregate => |aggregate| switch (aggregate.fields.at(0, ctype_pool).name.index) {6354 .aggregate => |aggregate| switch (aggregate.fields.at(0, ctype_pool).name.index) {
6355 .is_null, .payload => {6355 .is_null, .payload => {
6356 const operand_ctype = try f.ctypeFromType(f.typeOf(ty_op.operand), .complete);6356 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;
6358 const local = try f.allocLocal(inst, inst_ty);6358 const local = try f.allocLocal(inst, inst_ty);
6359 {6359 {
6360 const a = try Assignment.start(f, bw, .bool);6360 const a = try Assignment.start(f, w, .bool);
6361 try f.writeCValueMember(bw, local, .{ .identifier = "is_null" });6361 try f.writeCValueMember(w, local, .{ .identifier = "is_null" });
6362 try a.assign(f, bw);6362 try a.assign(f, w);
6363 try bw.writeAll("false");6363 try w.writeAll("false");
6364 try a.end(f, bw);6364 try a.end(f, w);
6365 }6365 }
6366 {6366 {
6367 const a = try Assignment.start(f, bw, operand_ctype);6367 const a = try Assignment.start(f, w, operand_ctype);
6368 try f.writeCValueMember(bw, local, .{ .identifier = "payload" });6368 try f.writeCValueMember(w, local, .{ .identifier = "payload" });
6369 try a.assign(f, bw);6369 try a.assign(f, w);
6370 try f.writeCValue(bw, operand, .Other);6370 try f.writeCValue(w, operand, .Other);
6371 try a.end(f, bw);6371 try a.end(f, w);
6372 }6372 }
6373 return local;6373 return local;
6374 },6374 },
...@@ -6390,7 +6390,7 @@ fn airWrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -6390,7 +6390,7 @@ fn airWrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue {
6390 const err = try f.resolveInst(ty_op.operand);6390 const err = try f.resolveInst(ty_op.operand);
6391 try reap(f, inst, &.{ty_op.operand});6391 try reap(f, inst, &.{ty_op.operand});
63926392
6393 const bw = &f.object.code.buffered_writer;6393 const w = &f.object.code.buffered_writer;
6394 const local = try f.allocLocal(inst, inst_ty);6394 const local = try f.allocLocal(inst, inst_ty);
63956395
6396 if (repr_is_err and err == .local and err.local == local.new_local) {6396 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 {...@@ -6399,21 +6399,21 @@ fn airWrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue {
6399 }6399 }
64006400
6401 if (!repr_is_err) {6401 if (!repr_is_err) {
6402 const a = try Assignment.start(f, bw, try f.ctypeFromType(payload_ty, .complete));6402 const a = try Assignment.start(f, w, try f.ctypeFromType(payload_ty, .complete));
6403 try f.writeCValueMember(bw, local, .{ .identifier = "payload" });6403 try f.writeCValueMember(w, local, .{ .identifier = "payload" });
6404 try a.assign(f, bw);6404 try a.assign(f, w);
6405 try f.object.dg.renderUndefValue(bw, payload_ty, .Other);6405 try f.object.dg.renderUndefValue(w, payload_ty, .Other);
6406 try a.end(f, bw);6406 try a.end(f, w);
6407 }6407 }
6408 {6408 {
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));
6410 if (repr_is_err)6410 if (repr_is_err)
6411 try f.writeCValue(bw, local, .Other)6411 try f.writeCValue(w, local, .Other)
6412 else6412 else
6413 try f.writeCValueMember(bw, local, .{ .identifier = "error" });6413 try f.writeCValueMember(w, local, .{ .identifier = "error" });
6414 try a.assign(f, bw);6414 try a.assign(f, w);
6415 try f.writeCValue(bw, err, .Other);6415 try f.writeCValue(w, err, .Other);
6416 try a.end(f, bw);6416 try a.end(f, w);
6417 }6417 }
6418 return local;6418 return local;
6419}6419}
...@@ -6421,7 +6421,7 @@ fn airWrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -6421,7 +6421,7 @@ fn airWrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue {
6421fn airErrUnionPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue {6421fn airErrUnionPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue {
6422 const pt = f.object.dg.pt;6422 const pt = f.object.dg.pt;
6423 const zcu = pt.zcu;6423 const zcu = pt.zcu;
6424 const bw = &f.object.code.buffered_writer;6424 const w = &f.object.code.buffered_writer;
6425 const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;6425 const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
6426 const inst_ty = f.typeOfIndex(inst);6426 const inst_ty = f.typeOfIndex(inst);
6427 const operand = try f.resolveInst(ty_op.operand);6427 const operand = try f.resolveInst(ty_op.operand);
...@@ -6435,31 +6435,31 @@ fn airErrUnionPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -6435,31 +6435,31 @@ fn airErrUnionPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue {
64356435
6436 // First, set the non-error value.6436 // First, set the non-error value.
6437 if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) {6437 if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) {
6438 const a = try Assignment.start(f, bw, try f.ctypeFromType(operand_ty, .complete));6438 const a = try Assignment.start(f, w, try f.ctypeFromType(operand_ty, .complete));
6439 try f.writeCValueDeref(bw, operand);6439 try f.writeCValueDeref(w, operand);
6440 try a.assign(f, bw);6440 try a.assign(f, w);
6441 try bw.print("{f}", .{try f.fmtIntLiteral(no_err)});6441 try w.print("{f}", .{try f.fmtIntLiteral(no_err)});
6442 try a.end(f, bw);6442 try a.end(f, w);
6443 return .none;6443 return .none;
6444 }6444 }
6445 {6445 {
6446 const a = try Assignment.start(f, bw, try f.ctypeFromType(err_int_ty, .complete));6446 const a = try Assignment.start(f, w, try f.ctypeFromType(err_int_ty, .complete));
6447 try f.writeCValueDerefMember(bw, operand, .{ .identifier = "error" });6447 try f.writeCValueDerefMember(w, operand, .{ .identifier = "error" });
6448 try a.assign(f, bw);6448 try a.assign(f, w);
6449 try bw.print("{f}", .{try f.fmtIntLiteral(no_err)});6449 try w.print("{f}", .{try f.fmtIntLiteral(no_err)});
6450 try a.end(f, bw);6450 try a.end(f, w);
6451 }6451 }
64526452
6453 // Then return the payload pointer (only if it is used)6453 // Then return the payload pointer (only if it is used)
6454 if (f.liveness.isUnused(inst)) return .none;6454 if (f.liveness.isUnused(inst)) return .none;
64556455
6456 const local = try f.allocLocal(inst, inst_ty);6456 const local = try f.allocLocal(inst, inst_ty);
6457 const a = try Assignment.start(f, bw, try f.ctypeFromType(inst_ty, .complete));6457 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete));
6458 try f.writeCValue(bw, local, .Other);6458 try f.writeCValue(w, local, .Other);
6459 try a.assign(f, bw);6459 try a.assign(f, w);
6460 try bw.writeByte('&');6460 try w.writeByte('&');
6461 try f.writeCValueDerefMember(bw, operand, .{ .identifier = "payload" });6461 try f.writeCValueDerefMember(w, operand, .{ .identifier = "payload" });
6462 try a.end(f, bw);6462 try a.end(f, w);
6463 return local;6463 return local;
6464}6464}
64656465
...@@ -6490,24 +6490,24 @@ fn airWrapErrUnionPay(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -6490,24 +6490,24 @@ fn airWrapErrUnionPay(f: *Function, inst: Air.Inst.Index) !CValue {
6490 const err_ty = inst_ty.errorUnionSet(zcu);6490 const err_ty = inst_ty.errorUnionSet(zcu);
6491 try reap(f, inst, &.{ty_op.operand});6491 try reap(f, inst, &.{ty_op.operand});
64926492
6493 const bw = &f.object.code.buffered_writer;6493 const w = &f.object.code.buffered_writer;
6494 const local = try f.allocLocal(inst, inst_ty);6494 const local = try f.allocLocal(inst, inst_ty);
6495 if (!repr_is_err) {6495 if (!repr_is_err) {
6496 const a = try Assignment.start(f, bw, try f.ctypeFromType(payload_ty, .complete));6496 const a = try Assignment.start(f, w, try f.ctypeFromType(payload_ty, .complete));
6497 try f.writeCValueMember(bw, local, .{ .identifier = "payload" });6497 try f.writeCValueMember(w, local, .{ .identifier = "payload" });
6498 try a.assign(f, bw);6498 try a.assign(f, w);
6499 try f.writeCValue(bw, payload, .Other);6499 try f.writeCValue(w, payload, .Other);
6500 try a.end(f, bw);6500 try a.end(f, w);
6501 }6501 }
6502 {6502 {
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));
6504 if (repr_is_err)6504 if (repr_is_err)
6505 try f.writeCValue(bw, local, .Other)6505 try f.writeCValue(w, local, .Other)
6506 else6506 else
6507 try f.writeCValueMember(bw, local, .{ .identifier = "error" });6507 try f.writeCValueMember(w, local, .{ .identifier = "error" });
6508 try a.assign(f, bw);6508 try a.assign(f, w);
6509 try f.object.dg.renderValue(bw, try pt.intValue(try pt.errorIntType(), 0), .Other);6509 try f.object.dg.renderValue(w, try pt.intValue(try pt.errorIntType(), 0), .Other);
6510 try a.end(f, bw);6510 try a.end(f, w);
6511 }6511 }
6512 return local;6512 return local;
6513}6513}
...@@ -6517,7 +6517,7 @@ fn airIsErr(f: *Function, inst: Air.Inst.Index, is_ptr: bool, operator: []const...@@ -6517,7 +6517,7 @@ fn airIsErr(f: *Function, inst: Air.Inst.Index, is_ptr: bool, operator: []const
6517 const zcu = pt.zcu;6517 const zcu = pt.zcu;
6518 const un_op = f.air.instructions.items(.data)[@intFromEnum(inst)].un_op;6518 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;
6521 const operand = try f.resolveInst(un_op);6521 const operand = try f.resolveInst(un_op);
6522 try reap(f, inst, &.{un_op});6522 try reap(f, inst, &.{un_op});
6523 const operand_ty = f.typeOf(un_op);6523 const operand_ty = f.typeOf(un_op);
...@@ -6526,25 +6526,25 @@ fn airIsErr(f: *Function, inst: Air.Inst.Index, is_ptr: bool, operator: []const...@@ -6526,25 +6526,25 @@ fn airIsErr(f: *Function, inst: Air.Inst.Index, is_ptr: bool, operator: []const
6526 const payload_ty = err_union_ty.errorUnionPayload(zcu);6526 const payload_ty = err_union_ty.errorUnionPayload(zcu);
6527 const error_ty = err_union_ty.errorUnionSet(zcu);6527 const error_ty = err_union_ty.errorUnionSet(zcu);
65286528
6529 const a = try Assignment.start(f, bw, .bool);6529 const a = try Assignment.start(f, w, .bool);
6530 try f.writeCValue(bw, local, .Other);6530 try f.writeCValue(w, local, .Other);
6531 try a.assign(f, bw);6531 try a.assign(f, w);
6532 const err_int_ty = try pt.errorIntType();6532 const err_int_ty = try pt.errorIntType();
6533 if (!error_ty.errorSetIsEmpty(zcu))6533 if (!error_ty.errorSetIsEmpty(zcu))
6534 if (payload_ty.hasRuntimeBits(zcu))6534 if (payload_ty.hasRuntimeBits(zcu))
6535 if (is_ptr)6535 if (is_ptr)
6536 try f.writeCValueDerefMember(bw, operand, .{ .identifier = "error" })6536 try f.writeCValueDerefMember(w, operand, .{ .identifier = "error" })
6537 else6537 else
6538 try f.writeCValueMember(bw, operand, .{ .identifier = "error" })6538 try f.writeCValueMember(w, operand, .{ .identifier = "error" })
6539 else6539 else
6540 try f.writeCValue(bw, operand, .Other)6540 try f.writeCValue(w, operand, .Other)
6541 else6541 else
6542 try f.object.dg.renderValue(bw, try pt.intValue(err_int_ty, 0), .Other);6542 try f.object.dg.renderValue(w, try pt.intValue(err_int_ty, 0), .Other);
6543 try bw.writeByte(' ');6543 try w.writeByte(' ');
6544 try bw.writeAll(operator);6544 try w.writeAll(operator);
6545 try bw.writeByte(' ');6545 try w.writeByte(' ');
6546 try f.object.dg.renderValue(bw, try pt.intValue(err_int_ty, 0), .Other);6546 try f.object.dg.renderValue(w, try pt.intValue(err_int_ty, 0), .Other);
6547 try a.end(f, bw);6547 try a.end(f, w);
6548 return local;6548 return local;
6549}6549}
65506550
...@@ -6558,45 +6558,45 @@ fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -6558,45 +6558,45 @@ fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue {
6558 try reap(f, inst, &.{ty_op.operand});6558 try reap(f, inst, &.{ty_op.operand});
6559 const inst_ty = f.typeOfIndex(inst);6559 const inst_ty = f.typeOfIndex(inst);
6560 const ptr_ty = inst_ty.slicePtrFieldType(zcu);6560 const ptr_ty = inst_ty.slicePtrFieldType(zcu);
6561 const bw = &f.object.code.buffered_writer;6561 const w = &f.object.code.buffered_writer;
6562 const local = try f.allocLocal(inst, inst_ty);6562 const local = try f.allocLocal(inst, inst_ty);
6563 const operand_ty = f.typeOf(ty_op.operand);6563 const operand_ty = f.typeOf(ty_op.operand);
6564 const array_ty = operand_ty.childType(zcu);6564 const array_ty = operand_ty.childType(zcu);
65656565
6566 {6566 {
6567 const a = try Assignment.start(f, bw, try f.ctypeFromType(ptr_ty, .complete));6567 const a = try Assignment.start(f, w, try f.ctypeFromType(ptr_ty, .complete));
6568 try f.writeCValueMember(bw, local, .{ .identifier = "ptr" });6568 try f.writeCValueMember(w, local, .{ .identifier = "ptr" });
6569 try a.assign(f, bw);6569 try a.assign(f, w);
6570 if (operand == .undef) {6570 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);
6572 } else {6572 } else {
6573 const ptr_ctype = try f.ctypeFromType(ptr_ty, .complete);6573 const ptr_ctype = try f.ctypeFromType(ptr_ty, .complete);
6574 const ptr_child_ctype = ptr_ctype.info(ctype_pool).pointer.elem_ctype;6574 const ptr_child_ctype = ptr_ctype.info(ctype_pool).pointer.elem_ctype;
6575 const elem_ty = array_ty.childType(zcu);6575 const elem_ty = array_ty.childType(zcu);
6576 const elem_ctype = try f.ctypeFromType(elem_ty, .complete);6576 const elem_ctype = try f.ctypeFromType(elem_ty, .complete);
6577 if (!ptr_child_ctype.eql(elem_ctype)) {6577 if (!ptr_child_ctype.eql(elem_ctype)) {
6578 try bw.writeByte('(');6578 try w.writeByte('(');
6579 try f.renderCType(bw, ptr_ctype);6579 try f.renderCType(w, ptr_ctype);
6580 try bw.writeByte(')');6580 try w.writeByte(')');
6581 }6581 }
6582 const operand_ctype = try f.ctypeFromType(operand_ty, .complete);6582 const operand_ctype = try f.ctypeFromType(operand_ty, .complete);
6583 const operand_child_ctype = operand_ctype.info(ctype_pool).pointer.elem_ctype;6583 const operand_child_ctype = operand_ctype.info(ctype_pool).pointer.elem_ctype;
6584 if (operand_child_ctype.info(ctype_pool) == .array) {6584 if (operand_child_ctype.info(ctype_pool) == .array) {
6585 try bw.writeByte('&');6585 try w.writeByte('&');
6586 try f.writeCValueDeref(bw, operand);6586 try f.writeCValueDeref(w, operand);
6587 try bw.print("[{f}]", .{try f.fmtIntLiteral(.zero_usize)});6587 try w.print("[{f}]", .{try f.fmtIntLiteral(.zero_usize)});
6588 } else try f.writeCValue(bw, operand, .Other);6588 } else try f.writeCValue(w, operand, .Other);
6589 }6589 }
6590 try a.end(f, bw);6590 try a.end(f, w);
6591 }6591 }
6592 {6592 {
6593 const a = try Assignment.start(f, bw, .usize);6593 const a = try Assignment.start(f, w, .usize);
6594 try f.writeCValueMember(bw, local, .{ .identifier = "len" });6594 try f.writeCValueMember(w, local, .{ .identifier = "len" });
6595 try a.assign(f, bw);6595 try a.assign(f, w);
6596 try bw.print("{f}", .{6596 try w.print("{f}", .{
6597 try f.fmtIntLiteral(try pt.intValue(.usize, array_ty.arrayLen(zcu))),6597 try f.fmtIntLiteral(try pt.intValue(.usize, array_ty.arrayLen(zcu))),
6598 });6598 });
6599 try a.end(f, bw);6599 try a.end(f, w);
6600 }6600 }
66016601
6602 return local;6602 return local;
...@@ -6623,32 +6623,32 @@ fn airFloatCast(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -6623,32 +6623,32 @@ fn airFloatCast(f: *Function, inst: Air.Inst.Index) !CValue {
6623 else6623 else
6624 unreachable;6624 unreachable;
66256625
6626 const bw = &f.object.code.buffered_writer;6626 const w = &f.object.code.buffered_writer;
6627 const local = try f.allocLocal(inst, inst_ty);6627 const local = try f.allocLocal(inst, inst_ty);
6628 const v = try Vectorize.start(f, inst, bw, operand_ty);6628 const v = try Vectorize.start(f, inst, w, operand_ty);
6629 const a = try Assignment.start(f, bw, try f.ctypeFromType(scalar_ty, .complete));6629 const a = try Assignment.start(f, w, try f.ctypeFromType(scalar_ty, .complete));
6630 try f.writeCValue(bw, local, .Other);6630 try f.writeCValue(w, local, .Other);
6631 try v.elem(f, bw);6631 try v.elem(f, w);
6632 try a.assign(f, bw);6632 try a.assign(f, w);
6633 if (inst_scalar_ty.isInt(zcu) and scalar_ty.isRuntimeFloat()) {6633 if (inst_scalar_ty.isInt(zcu) and scalar_ty.isRuntimeFloat()) {
6634 try bw.writeAll("zig_wrap_");6634 try w.writeAll("zig_wrap_");
6635 try f.object.dg.renderTypeForBuiltinFnName(bw, inst_scalar_ty);6635 try f.object.dg.renderTypeForBuiltinFnName(w, inst_scalar_ty);
6636 try bw.writeByte('(');6636 try w.writeByte('(');
6637 }6637 }
6638 try bw.writeAll("zig_");6638 try w.writeAll("zig_");
6639 try bw.writeAll(operation);6639 try w.writeAll(operation);
6640 try bw.writeAll(compilerRtAbbrev(scalar_ty, zcu, target));6640 try w.writeAll(compilerRtAbbrev(scalar_ty, zcu, target));
6641 try bw.writeAll(compilerRtAbbrev(inst_scalar_ty, zcu, target));6641 try w.writeAll(compilerRtAbbrev(inst_scalar_ty, zcu, target));
6642 try bw.writeByte('(');6642 try w.writeByte('(');
6643 try f.writeCValue(bw, operand, .FunctionArgument);6643 try f.writeCValue(w, operand, .FunctionArgument);
6644 try v.elem(f, bw);6644 try v.elem(f, w);
6645 try bw.writeByte(')');6645 try w.writeByte(')');
6646 if (inst_scalar_ty.isInt(zcu) and scalar_ty.isRuntimeFloat()) {6646 if (inst_scalar_ty.isInt(zcu) and scalar_ty.isRuntimeFloat()) {
6647 try f.object.dg.renderBuiltinInfo(bw, inst_scalar_ty, .bits);6647 try f.object.dg.renderBuiltinInfo(w, inst_scalar_ty, .bits);
6648 try bw.writeByte(')');6648 try w.writeByte(')');
6649 }6649 }
6650 try a.end(f, bw);6650 try a.end(f, w);
6651 try v.end(f, inst, bw);6651 try v.end(f, inst, w);
66526652
6653 return local;6653 return local;
6654}6654}
...@@ -6673,28 +6673,28 @@ fn airUnBuiltinCall(...@@ -6673,28 +6673,28 @@ fn airUnBuiltinCall(
6673 const inst_scalar_ctype = try f.ctypeFromType(inst_scalar_ty, .complete);6673 const inst_scalar_ctype = try f.ctypeFromType(inst_scalar_ty, .complete);
6674 const ref_ret = inst_scalar_ctype.info(&f.object.dg.ctype_pool) == .array;6674 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;
6677 const local = try f.allocLocal(inst, inst_ty);6677 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);
6679 if (!ref_ret) {6679 if (!ref_ret) {
6680 try f.writeCValue(bw, local, .Other);6680 try f.writeCValue(w, local, .Other);
6681 try v.elem(f, bw);6681 try v.elem(f, w);
6682 try bw.writeAll(" = ");6682 try w.writeAll(" = ");
6683 }6683 }
6684 try bw.print("zig_{s}_", .{operation});6684 try w.print("zig_{s}_", .{operation});
6685 try f.object.dg.renderTypeForBuiltinFnName(bw, scalar_ty);6685 try f.object.dg.renderTypeForBuiltinFnName(w, scalar_ty);
6686 try bw.writeByte('(');6686 try w.writeByte('(');
6687 if (ref_ret) {6687 if (ref_ret) {
6688 try f.writeCValue(bw, local, .FunctionArgument);6688 try f.writeCValue(w, local, .FunctionArgument);
6689 try v.elem(f, bw);6689 try v.elem(f, w);
6690 try bw.writeAll(", ");6690 try w.writeAll(", ");
6691 }6691 }
6692 try f.writeCValue(bw, operand, .FunctionArgument);6692 try f.writeCValue(w, operand, .FunctionArgument);
6693 try v.elem(f, bw);6693 try v.elem(f, w);
6694 try f.object.dg.renderBuiltinInfo(bw, scalar_ty, info);6694 try f.object.dg.renderBuiltinInfo(w, scalar_ty, info);
6695 try bw.writeAll(");");6695 try w.writeAll(");");
6696 try f.object.newline();6696 try f.object.newline();
6697 try v.end(f, inst, bw);6697 try v.end(f, inst, w);
66986698
6699 return local;6699 return local;
6700}6700}
...@@ -6724,31 +6724,31 @@ fn airBinBuiltinCall(...@@ -6724,31 +6724,31 @@ fn airBinBuiltinCall(
6724 const inst_scalar_ctype = try f.ctypeFromType(inst_scalar_ty, .complete);6724 const inst_scalar_ctype = try f.ctypeFromType(inst_scalar_ty, .complete);
6725 const ref_ret = inst_scalar_ctype.info(&f.object.dg.ctype_pool) == .array;6725 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;
6728 const local = try f.allocLocal(inst, inst_ty);6728 const local = try f.allocLocal(inst, inst_ty);
6729 if (is_big) try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });6729 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);
6731 if (!ref_ret) {6731 if (!ref_ret) {
6732 try f.writeCValue(bw, local, .Other);6732 try f.writeCValue(w, local, .Other);
6733 try v.elem(f, bw);6733 try v.elem(f, w);
6734 try bw.writeAll(" = ");6734 try w.writeAll(" = ");
6735 }6735 }
6736 try bw.print("zig_{s}_", .{operation});6736 try w.print("zig_{s}_", .{operation});
6737 try f.object.dg.renderTypeForBuiltinFnName(bw, scalar_ty);6737 try f.object.dg.renderTypeForBuiltinFnName(w, scalar_ty);
6738 try bw.writeByte('(');6738 try w.writeByte('(');
6739 if (ref_ret) {6739 if (ref_ret) {
6740 try f.writeCValue(bw, local, .FunctionArgument);6740 try f.writeCValue(w, local, .FunctionArgument);
6741 try v.elem(f, bw);6741 try v.elem(f, w);
6742 try bw.writeAll(", ");6742 try w.writeAll(", ");
6743 }6743 }
6744 try f.writeCValue(bw, lhs, .FunctionArgument);6744 try f.writeCValue(w, lhs, .FunctionArgument);
6745 try v.elem(f, bw);6745 try v.elem(f, w);
6746 try bw.writeAll(", ");6746 try w.writeAll(", ");
6747 try f.writeCValue(bw, rhs, .FunctionArgument);6747 try f.writeCValue(w, rhs, .FunctionArgument);
6748 if (f.typeOf(bin_op.rhs).isVector(zcu)) try v.elem(f, bw);6748 if (f.typeOf(bin_op.rhs).isVector(zcu)) try v.elem(f, w);
6749 try f.object.dg.renderBuiltinInfo(bw, scalar_ty, info);6749 try f.object.dg.renderBuiltinInfo(w, scalar_ty, info);
6750 try bw.writeAll(");\n");6750 try w.writeAll(");\n");
6751 try v.end(f, inst, bw);6751 try v.end(f, inst, w);
67526752
6753 return local;6753 return local;
6754}6754}
...@@ -6775,39 +6775,39 @@ fn airCmpBuiltinCall(...@@ -6775,39 +6775,39 @@ fn airCmpBuiltinCall(
6775 const inst_scalar_ctype = try f.ctypeFromType(inst_scalar_ty, .complete);6775 const inst_scalar_ctype = try f.ctypeFromType(inst_scalar_ty, .complete);
6776 const ref_ret = inst_scalar_ctype.info(&f.object.dg.ctype_pool) == .array;6776 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;
6779 const local = try f.allocLocal(inst, inst_ty);6779 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);
6781 if (!ref_ret) {6781 if (!ref_ret) {
6782 try f.writeCValue(bw, local, .Other);6782 try f.writeCValue(w, local, .Other);
6783 try v.elem(f, bw);6783 try v.elem(f, w);
6784 try bw.writeAll(" = ");6784 try w.writeAll(" = ");
6785 }6785 }
6786 try bw.print("zig_{s}_", .{switch (operation) {6786 try w.print("zig_{s}_", .{switch (operation) {
6787 else => @tagName(operation),6787 else => @tagName(operation),
6788 .operator => compareOperatorAbbrev(operator),6788 .operator => compareOperatorAbbrev(operator),
6789 }});6789 }});
6790 try f.object.dg.renderTypeForBuiltinFnName(bw, scalar_ty);6790 try f.object.dg.renderTypeForBuiltinFnName(w, scalar_ty);
6791 try bw.writeByte('(');6791 try w.writeByte('(');
6792 if (ref_ret) {6792 if (ref_ret) {
6793 try f.writeCValue(bw, local, .FunctionArgument);6793 try f.writeCValue(w, local, .FunctionArgument);
6794 try v.elem(f, bw);6794 try v.elem(f, w);
6795 try bw.writeAll(", ");6795 try w.writeAll(", ");
6796 }6796 }
6797 try f.writeCValue(bw, lhs, .FunctionArgument);6797 try f.writeCValue(w, lhs, .FunctionArgument);
6798 try v.elem(f, bw);6798 try v.elem(f, w);
6799 try bw.writeAll(", ");6799 try w.writeAll(", ");
6800 try f.writeCValue(bw, rhs, .FunctionArgument);6800 try f.writeCValue(w, rhs, .FunctionArgument);
6801 try v.elem(f, bw);6801 try v.elem(f, w);
6802 try f.object.dg.renderBuiltinInfo(bw, scalar_ty, info);6802 try f.object.dg.renderBuiltinInfo(w, scalar_ty, info);
6803 try bw.writeByte(')');6803 try w.writeByte(')');
6804 if (!ref_ret) try bw.print("{s}{f}", .{6804 if (!ref_ret) try w.print("{s}{f}", .{
6805 compareOperatorC(operator),6805 compareOperatorC(operator),
6806 try f.fmtIntLiteral(try pt.intValue(.i32, 0)),6806 try f.fmtIntLiteral(try pt.intValue(.i32, 0)),
6807 });6807 });
6808 try bw.writeByte(';');6808 try w.writeByte(';');
6809 try f.object.newline();6809 try f.object.newline();
6810 try v.end(f, inst, bw);6810 try v.end(f, inst, w);
68116811
6812 return local;6812 return local;
6813}6813}
...@@ -6825,7 +6825,7 @@ fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue...@@ -6825,7 +6825,7 @@ fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue
6825 const ty = ptr_ty.childType(zcu);6825 const ty = ptr_ty.childType(zcu);
6826 const ctype = try f.ctypeFromType(ty, .complete);6826 const ctype = try f.ctypeFromType(ty, .complete);
68276827
6828 const bw = &f.object.code.buffered_writer;6828 const w = &f.object.code.buffered_writer;
6829 const new_value_mat = try Materialize.start(f, inst, ty, new_value);6829 const new_value_mat = try Materialize.start(f, inst, ty, new_value);
6830 try reap(f, inst, &.{ extra.ptr, extra.expected_value, extra.new_value });6830 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...@@ -6837,78 +6837,78 @@ fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue
6837 const local = try f.allocLocal(inst, inst_ty);6837 const local = try f.allocLocal(inst, inst_ty);
6838 if (inst_ty.isPtrLikeOptional(zcu)) {6838 if (inst_ty.isPtrLikeOptional(zcu)) {
6839 {6839 {
6840 const a = try Assignment.start(f, bw, ctype);6840 const a = try Assignment.start(f, w, ctype);
6841 try f.writeCValue(bw, local, .Other);6841 try f.writeCValue(w, local, .Other);
6842 try a.assign(f, bw);6842 try a.assign(f, w);
6843 try f.writeCValue(bw, expected_value, .Other);6843 try f.writeCValue(w, expected_value, .Other);
6844 try a.end(f, bw);6844 try a.end(f, w);
6845 }6845 }
68466846
6847 try bw.writeAll("if (");6847 try w.writeAll("if (");
6848 try bw.print("zig_cmpxchg_{s}((zig_atomic(", .{flavor});6848 try w.print("zig_cmpxchg_{s}((zig_atomic(", .{flavor});
6849 try f.renderType(bw, ty);6849 try f.renderType(w, ty);
6850 try bw.writeByte(')');6850 try w.writeByte(')');
6851 if (ptr_ty.isVolatilePtr(zcu)) try bw.writeAll(" volatile");6851 if (ptr_ty.isVolatilePtr(zcu)) try w.writeAll(" volatile");
6852 try bw.writeAll(" *)");6852 try w.writeAll(" *)");
6853 try f.writeCValue(bw, ptr, .Other);6853 try f.writeCValue(w, ptr, .Other);
6854 try bw.writeAll(", ");6854 try w.writeAll(", ");
6855 try f.writeCValue(bw, local, .FunctionArgument);6855 try f.writeCValue(w, local, .FunctionArgument);
6856 try bw.writeAll(", ");6856 try w.writeAll(", ");
6857 try new_value_mat.mat(f, bw);6857 try new_value_mat.mat(f, w);
6858 try bw.writeAll(", ");6858 try w.writeAll(", ");
6859 try writeMemoryOrder(bw, extra.successOrder());6859 try writeMemoryOrder(w, extra.successOrder());
6860 try bw.writeAll(", ");6860 try w.writeAll(", ");
6861 try writeMemoryOrder(bw, extra.failureOrder());6861 try writeMemoryOrder(w, extra.failureOrder());
6862 try bw.writeAll(", ");6862 try w.writeAll(", ");
6863 try f.object.dg.renderTypeForBuiltinFnName(bw, ty);6863 try f.object.dg.renderTypeForBuiltinFnName(w, ty);
6864 try bw.writeAll(", ");6864 try w.writeAll(", ");
6865 try f.renderType(bw, repr_ty);6865 try f.renderType(w, repr_ty);
6866 try bw.writeByte(')');6866 try w.writeByte(')');
6867 try bw.writeAll(") {");6867 try w.writeAll(") {");
6868 f.object.indent();6868 f.object.indent();
6869 try f.object.newline();6869 try f.object.newline();
6870 {6870 {
6871 const a = try Assignment.start(f, bw, ctype);6871 const a = try Assignment.start(f, w, ctype);
6872 try f.writeCValue(bw, local, .Other);6872 try f.writeCValue(w, local, .Other);
6873 try a.assign(f, bw);6873 try a.assign(f, w);
6874 try bw.writeAll("NULL");6874 try w.writeAll("NULL");
6875 try a.end(f, bw);6875 try a.end(f, w);
6876 }6876 }
6877 try f.object.outdent();6877 try f.object.outdent();
6878 try bw.writeByte('}');6878 try w.writeByte('}');
6879 try f.object.newline();6879 try f.object.newline();
6880 } else {6880 } else {
6881 {6881 {
6882 const a = try Assignment.start(f, bw, ctype);6882 const a = try Assignment.start(f, w, ctype);
6883 try f.writeCValueMember(bw, local, .{ .identifier = "payload" });6883 try f.writeCValueMember(w, local, .{ .identifier = "payload" });
6884 try a.assign(f, bw);6884 try a.assign(f, w);
6885 try f.writeCValue(bw, expected_value, .Other);6885 try f.writeCValue(w, expected_value, .Other);
6886 try a.end(f, bw);6886 try a.end(f, w);
6887 }6887 }
6888 {6888 {
6889 const a = try Assignment.start(f, bw, .bool);6889 const a = try Assignment.start(f, w, .bool);
6890 try f.writeCValueMember(bw, local, .{ .identifier = "is_null" });6890 try f.writeCValueMember(w, local, .{ .identifier = "is_null" });
6891 try a.assign(f, bw);6891 try a.assign(f, w);
6892 try bw.print("zig_cmpxchg_{s}((zig_atomic(", .{flavor});6892 try w.print("zig_cmpxchg_{s}((zig_atomic(", .{flavor});
6893 try f.renderType(bw, ty);6893 try f.renderType(w, ty);
6894 try bw.writeByte(')');6894 try w.writeByte(')');
6895 if (ptr_ty.isVolatilePtr(zcu)) try bw.writeAll(" volatile");6895 if (ptr_ty.isVolatilePtr(zcu)) try w.writeAll(" volatile");
6896 try bw.writeAll(" *)");6896 try w.writeAll(" *)");
6897 try f.writeCValue(bw, ptr, .Other);6897 try f.writeCValue(w, ptr, .Other);
6898 try bw.writeAll(", ");6898 try w.writeAll(", ");
6899 try f.writeCValueMember(bw, local, .{ .identifier = "payload" });6899 try f.writeCValueMember(w, local, .{ .identifier = "payload" });
6900 try bw.writeAll(", ");6900 try w.writeAll(", ");
6901 try new_value_mat.mat(f, bw);6901 try new_value_mat.mat(f, w);
6902 try bw.writeAll(", ");6902 try w.writeAll(", ");
6903 try writeMemoryOrder(bw, extra.successOrder());6903 try writeMemoryOrder(w, extra.successOrder());
6904 try bw.writeAll(", ");6904 try w.writeAll(", ");
6905 try writeMemoryOrder(bw, extra.failureOrder());6905 try writeMemoryOrder(w, extra.failureOrder());
6906 try bw.writeAll(", ");6906 try w.writeAll(", ");
6907 try f.object.dg.renderTypeForBuiltinFnName(bw, ty);6907 try f.object.dg.renderTypeForBuiltinFnName(w, ty);
6908 try bw.writeAll(", ");6908 try w.writeAll(", ");
6909 try f.renderType(bw, repr_ty);6909 try f.renderType(w, repr_ty);
6910 try bw.writeByte(')');6910 try w.writeByte(')');
6911 try a.end(f, bw);6911 try a.end(f, w);
6912 }6912 }
6913 }6913 }
6914 try new_value_mat.end(f, inst);6914 try new_value_mat.end(f, inst);
...@@ -6932,7 +6932,7 @@ fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -6932,7 +6932,7 @@ fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue {
6932 const ptr = try f.resolveInst(pl_op.operand);6932 const ptr = try f.resolveInst(pl_op.operand);
6933 const operand = try f.resolveInst(extra.operand);6933 const operand = try f.resolveInst(extra.operand);
69346934
6935 const bw = &f.object.code.buffered_writer;6935 const w = &f.object.code.buffered_writer;
6936 const operand_mat = try Materialize.start(f, inst, ty, operand);6936 const operand_mat = try Materialize.start(f, inst, ty, operand);
6937 try reap(f, inst, &.{ pl_op.operand, extra.operand });6937 try reap(f, inst, &.{ pl_op.operand, extra.operand });
69386938
...@@ -6942,31 +6942,31 @@ fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -6942,31 +6942,31 @@ fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue {
6942 const repr_ty = if (is_float) pt.intType(.unsigned, repr_bits) catch unreachable else ty;6942 const repr_ty = if (is_float) pt.intType(.unsigned, repr_bits) catch unreachable else ty;
69436943
6944 const local = try f.allocLocal(inst, inst_ty);6944 const local = try f.allocLocal(inst, inst_ty);
6945 try bw.print("zig_atomicrmw_{s}", .{toAtomicRmwSuffix(extra.op())});6945 try w.print("zig_atomicrmw_{s}", .{toAtomicRmwSuffix(extra.op())});
6946 if (is_float) try bw.writeAll("_float") else if (is_128) try bw.writeAll("_int128");6946 if (is_float) try w.writeAll("_float") else if (is_128) try w.writeAll("_int128");
6947 try bw.writeByte('(');6947 try w.writeByte('(');
6948 try f.writeCValue(bw, local, .Other);6948 try f.writeCValue(w, local, .Other);
6949 try bw.writeAll(", (");6949 try w.writeAll(", (");
6950 const use_atomic = switch (extra.op()) {6950 const use_atomic = switch (extra.op()) {
6951 else => true,6951 else => true,
6952 // These are missing from stdatomic.h, so no atomic types unless a fallback is used.6952 // These are missing from stdatomic.h, so no atomic types unless a fallback is used.
6953 .Nand, .Min, .Max => is_float or is_128,6953 .Nand, .Min, .Max => is_float or is_128,
6954 };6954 };
6955 if (use_atomic) try bw.writeAll("zig_atomic(");6955 if (use_atomic) try w.writeAll("zig_atomic(");
6956 try f.renderType(bw, ty);6956 try f.renderType(w, ty);
6957 if (use_atomic) try bw.writeByte(')');6957 if (use_atomic) try w.writeByte(')');
6958 if (ptr_ty.isVolatilePtr(zcu)) try bw.writeAll(" volatile");6958 if (ptr_ty.isVolatilePtr(zcu)) try w.writeAll(" volatile");
6959 try bw.writeAll(" *)");6959 try w.writeAll(" *)");
6960 try f.writeCValue(bw, ptr, .Other);6960 try f.writeCValue(w, ptr, .Other);
6961 try bw.writeAll(", ");6961 try w.writeAll(", ");
6962 try operand_mat.mat(f, bw);6962 try operand_mat.mat(f, w);
6963 try bw.writeAll(", ");6963 try w.writeAll(", ");
6964 try writeMemoryOrder(bw, extra.ordering());6964 try writeMemoryOrder(w, extra.ordering());
6965 try bw.writeAll(", ");6965 try w.writeAll(", ");
6966 try f.object.dg.renderTypeForBuiltinFnName(bw, ty);6966 try f.object.dg.renderTypeForBuiltinFnName(w, ty);
6967 try bw.writeAll(", ");6967 try w.writeAll(", ");
6968 try f.renderType(bw, repr_ty);6968 try f.renderType(w, repr_ty);
6969 try bw.writeAll(");");6969 try w.writeAll(");");
6970 try f.object.newline();6970 try f.object.newline();
6971 try operand_mat.end(f, inst);6971 try operand_mat.end(f, inst);
69726972
...@@ -6993,24 +6993,24 @@ fn airAtomicLoad(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -6993,24 +6993,24 @@ fn airAtomicLoad(f: *Function, inst: Air.Inst.Index) !CValue {
6993 ty;6993 ty;
69946994
6995 const inst_ty = f.typeOfIndex(inst);6995 const inst_ty = f.typeOfIndex(inst);
6996 const bw = &f.object.code.buffered_writer;6996 const w = &f.object.code.buffered_writer;
6997 const local = try f.allocLocal(inst, inst_ty);6997 const local = try f.allocLocal(inst, inst_ty);
69986998
6999 try bw.writeAll("zig_atomic_load(");6999 try w.writeAll("zig_atomic_load(");
7000 try f.writeCValue(bw, local, .Other);7000 try f.writeCValue(w, local, .Other);
7001 try bw.writeAll(", (zig_atomic(");7001 try w.writeAll(", (zig_atomic(");
7002 try f.renderType(bw, ty);7002 try f.renderType(w, ty);
7003 try bw.writeByte(')');7003 try w.writeByte(')');
7004 if (ptr_ty.isVolatilePtr(zcu)) try bw.writeAll(" volatile");7004 if (ptr_ty.isVolatilePtr(zcu)) try w.writeAll(" volatile");
7005 try bw.writeAll(" *)");7005 try w.writeAll(" *)");
7006 try f.writeCValue(bw, ptr, .Other);7006 try f.writeCValue(w, ptr, .Other);
7007 try bw.writeAll(", ");7007 try w.writeAll(", ");
7008 try writeMemoryOrder(bw, atomic_load.order);7008 try writeMemoryOrder(w, atomic_load.order);
7009 try bw.writeAll(", ");7009 try w.writeAll(", ");
7010 try f.object.dg.renderTypeForBuiltinFnName(bw, ty);7010 try f.object.dg.renderTypeForBuiltinFnName(w, ty);
7011 try bw.writeAll(", ");7011 try w.writeAll(", ");
7012 try f.renderType(bw, repr_ty);7012 try f.renderType(w, repr_ty);
7013 try bw.writeAll(");");7013 try w.writeAll(");");
7014 try f.object.newline();7014 try f.object.newline();
70157015
7016 return local;7016 return local;
...@@ -7025,7 +7025,7 @@ fn airAtomicStore(f: *Function, inst: Air.Inst.Index, order: [*:0]const u8) !CVa...@@ -7025,7 +7025,7 @@ fn airAtomicStore(f: *Function, inst: Air.Inst.Index, order: [*:0]const u8) !CVa
7025 const ptr = try f.resolveInst(bin_op.lhs);7025 const ptr = try f.resolveInst(bin_op.lhs);
7026 const element = try f.resolveInst(bin_op.rhs);7026 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;
7029 const element_mat = try Materialize.start(f, inst, ty, element);7029 const element_mat = try Materialize.start(f, inst, ty, element);
7030 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });7030 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...@@ -7034,32 +7034,32 @@ fn airAtomicStore(f: *Function, inst: Air.Inst.Index, order: [*:0]const u8) !CVa
7034 else7034 else
7035 ty;7035 ty;
70367036
7037 try bw.writeAll("zig_atomic_store((zig_atomic(");7037 try w.writeAll("zig_atomic_store((zig_atomic(");
7038 try f.renderType(bw, ty);7038 try f.renderType(w, ty);
7039 try bw.writeByte(')');7039 try w.writeByte(')');
7040 if (ptr_ty.isVolatilePtr(zcu)) try bw.writeAll(" volatile");7040 if (ptr_ty.isVolatilePtr(zcu)) try w.writeAll(" volatile");
7041 try bw.writeAll(" *)");7041 try w.writeAll(" *)");
7042 try f.writeCValue(bw, ptr, .Other);7042 try f.writeCValue(w, ptr, .Other);
7043 try bw.writeAll(", ");7043 try w.writeAll(", ");
7044 try element_mat.mat(f, bw);7044 try element_mat.mat(f, w);
7045 try bw.print(", {s}, ", .{order});7045 try w.print(", {s}, ", .{order});
7046 try f.object.dg.renderTypeForBuiltinFnName(bw, ty);7046 try f.object.dg.renderTypeForBuiltinFnName(w, ty);
7047 try bw.writeAll(", ");7047 try w.writeAll(", ");
7048 try f.renderType(bw, repr_ty);7048 try f.renderType(w, repr_ty);
7049 try bw.writeAll(");");7049 try w.writeAll(");");
7050 try f.object.newline();7050 try f.object.newline();
7051 try element_mat.end(f, inst);7051 try element_mat.end(f, inst);
70527052
7053 return .none;7053 return .none;
7054}7054}
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 {
7057 const pt = f.object.dg.pt;7057 const pt = f.object.dg.pt;
7058 const zcu = pt.zcu;7058 const zcu = pt.zcu;
7059 if (ptr_ty.isSlice(zcu)) {7059 if (ptr_ty.isSlice(zcu)) {
7060 try f.writeCValueMember(bw, ptr, .{ .identifier = "ptr" });7060 try f.writeCValueMember(w, ptr, .{ .identifier = "ptr" });
7061 } else {7061 } else {
7062 try f.writeCValue(bw, ptr, .FunctionArgument);7062 try f.writeCValue(w, ptr, .FunctionArgument);
7063 }7063 }
7064}7064}
70657065
...@@ -7073,7 +7073,7 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {...@@ -7073,7 +7073,7 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
7073 const elem_ty = f.typeOf(bin_op.rhs);7073 const elem_ty = f.typeOf(bin_op.rhs);
7074 const elem_abi_size = elem_ty.abiSize(zcu);7074 const elem_abi_size = elem_ty.abiSize(zcu);
7075 const val_is_undef = if (try f.air.value(bin_op.rhs, pt)) |val| val.isUndefDeep(zcu) else false;7075 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
7078 if (val_is_undef) {7078 if (val_is_undef) {
7079 if (!safety) {7079 if (!safety) {
...@@ -7081,24 +7081,24 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {...@@ -7081,24 +7081,24 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
7081 return .none;7081 return .none;
7082 }7082 }
70837083
7084 try bw.writeAll("memset(");7084 try w.writeAll("memset(");
7085 switch (dest_ty.ptrSize(zcu)) {7085 switch (dest_ty.ptrSize(zcu)) {
7086 .slice => {7086 .slice => {
7087 try f.writeCValueMember(bw, dest_slice, .{ .identifier = "ptr" });7087 try f.writeCValueMember(w, dest_slice, .{ .identifier = "ptr" });
7088 try bw.writeAll(", 0xaa, ");7088 try w.writeAll(", 0xaa, ");
7089 try f.writeCValueMember(bw, dest_slice, .{ .identifier = "len" });7089 try f.writeCValueMember(w, dest_slice, .{ .identifier = "len" });
7090 if (elem_abi_size > 1) {7090 if (elem_abi_size > 1) {
7091 try bw.print(" * {d}", .{elem_abi_size});7091 try w.print(" * {d}", .{elem_abi_size});
7092 }7092 }
7093 try bw.writeAll(");");7093 try w.writeAll(");");
7094 try f.object.newline();7094 try f.object.newline();
7095 },7095 },
7096 .one => {7096 .one => {
7097 const array_ty = dest_ty.childType(zcu);7097 const array_ty = dest_ty.childType(zcu);
7098 const len = array_ty.arrayLen(zcu) * elem_abi_size;7098 const len = array_ty.arrayLen(zcu) * elem_abi_size;
70997099
7100 try f.writeCValue(bw, dest_slice, .FunctionArgument);7100 try f.writeCValue(w, dest_slice, .FunctionArgument);
7101 try bw.print(", 0xaa, {d});", .{len});7101 try w.print(", 0xaa, {d});", .{len});
7102 try f.object.newline();7102 try f.object.newline();
7103 },7103 },
7104 .many, .c => unreachable,7104 .many, .c => unreachable,
...@@ -7120,38 +7120,38 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {...@@ -7120,38 +7120,38 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
71207120
7121 const index = try f.allocLocal(inst, .usize);7121 const index = try f.allocLocal(inst, .usize);
71227122
7123 try bw.writeAll("for (");7123 try w.writeAll("for (");
7124 try f.writeCValue(bw, index, .Other);7124 try f.writeCValue(w, index, .Other);
7125 try bw.writeAll(" = ");7125 try w.writeAll(" = ");
7126 try f.object.dg.renderValue(bw, .zero_usize, .Other);7126 try f.object.dg.renderValue(w, .zero_usize, .Other);
7127 try bw.writeAll("; ");7127 try w.writeAll("; ");
7128 try f.writeCValue(bw, index, .Other);7128 try f.writeCValue(w, index, .Other);
7129 try bw.writeAll(" != ");7129 try w.writeAll(" != ");
7130 switch (dest_ty.ptrSize(zcu)) {7130 switch (dest_ty.ptrSize(zcu)) {
7131 .slice => {7131 .slice => {
7132 try f.writeCValueMember(bw, dest_slice, .{ .identifier = "len" });7132 try f.writeCValueMember(w, dest_slice, .{ .identifier = "len" });
7133 },7133 },
7134 .one => {7134 .one => {
7135 const array_ty = dest_ty.childType(zcu);7135 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)});
7137 },7137 },
7138 .many, .c => unreachable,7138 .many, .c => unreachable,
7139 }7139 }
7140 try bw.writeAll("; ++");7140 try w.writeAll("; ++");
7141 try f.writeCValue(bw, index, .Other);7141 try f.writeCValue(w, index, .Other);
7142 try bw.writeAll(") ");7142 try w.writeAll(") ");
71437143
7144 const a = try Assignment.start(f, bw, try f.ctypeFromType(elem_ty, .complete));7144 const a = try Assignment.start(f, w, try f.ctypeFromType(elem_ty, .complete));
7145 try bw.writeAll("((");7145 try w.writeAll("((");
7146 try f.renderType(bw, elem_ptr_ty);7146 try f.renderType(w, elem_ptr_ty);
7147 try bw.writeByte(')');7147 try w.writeByte(')');
7148 try writeSliceOrPtr(f, bw, dest_slice, dest_ty);7148 try writeSliceOrPtr(f, w, dest_slice, dest_ty);
7149 try bw.writeAll(")[");7149 try w.writeAll(")[");
7150 try f.writeCValue(bw, index, .Other);7150 try f.writeCValue(w, index, .Other);
7151 try bw.writeByte(']');7151 try w.writeByte(']');
7152 try a.assign(f, bw);7152 try a.assign(f, w);
7153 try f.writeCValue(bw, value, .Other);7153 try f.writeCValue(w, value, .Other);
7154 try a.end(f, bw);7154 try a.end(f, w);
71557155
7156 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });7156 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });
7157 try freeLocal(f, inst, index.new_local, null);7157 try freeLocal(f, inst, index.new_local, null);
...@@ -7161,25 +7161,25 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {...@@ -7161,25 +7161,25 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
71617161
7162 const bitcasted = try bitcast(f, .u8, value, elem_ty);7162 const bitcasted = try bitcast(f, .u8, value, elem_ty);
71637163
7164 try bw.writeAll("memset(");7164 try w.writeAll("memset(");
7165 switch (dest_ty.ptrSize(zcu)) {7165 switch (dest_ty.ptrSize(zcu)) {
7166 .slice => {7166 .slice => {
7167 try f.writeCValueMember(bw, dest_slice, .{ .identifier = "ptr" });7167 try f.writeCValueMember(w, dest_slice, .{ .identifier = "ptr" });
7168 try bw.writeAll(", ");7168 try w.writeAll(", ");
7169 try f.writeCValue(bw, bitcasted, .FunctionArgument);7169 try f.writeCValue(w, bitcasted, .FunctionArgument);
7170 try bw.writeAll(", ");7170 try w.writeAll(", ");
7171 try f.writeCValueMember(bw, dest_slice, .{ .identifier = "len" });7171 try f.writeCValueMember(w, dest_slice, .{ .identifier = "len" });
7172 try bw.writeAll(");");7172 try w.writeAll(");");
7173 try f.object.newline();7173 try f.object.newline();
7174 },7174 },
7175 .one => {7175 .one => {
7176 const array_ty = dest_ty.childType(zcu);7176 const array_ty = dest_ty.childType(zcu);
7177 const len = array_ty.arrayLen(zcu) * elem_abi_size;7177 const len = array_ty.arrayLen(zcu) * elem_abi_size;
71787178
7179 try f.writeCValue(bw, dest_slice, .FunctionArgument);7179 try f.writeCValue(w, dest_slice, .FunctionArgument);
7180 try bw.writeAll(", ");7180 try w.writeAll(", ");
7181 try f.writeCValue(bw, bitcasted, .FunctionArgument);7181 try f.writeCValue(w, bitcasted, .FunctionArgument);
7182 try bw.print(", {d});", .{len});7182 try w.print(", {d});", .{len});
7183 try f.object.newline();7183 try f.object.newline();
7184 },7184 },
7185 .many, .c => unreachable,7185 .many, .c => unreachable,
...@@ -7197,22 +7197,22 @@ fn airMemcpy(f: *Function, inst: Air.Inst.Index, function_paren: []const u8) !CV...@@ -7197,22 +7197,22 @@ fn airMemcpy(f: *Function, inst: Air.Inst.Index, function_paren: []const u8) !CV
7197 const src_ptr = try f.resolveInst(bin_op.rhs);7197 const src_ptr = try f.resolveInst(bin_op.rhs);
7198 const dest_ty = f.typeOf(bin_op.lhs);7198 const dest_ty = f.typeOf(bin_op.lhs);
7199 const src_ty = f.typeOf(bin_op.rhs);7199 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
7202 if (dest_ty.ptrSize(zcu) != .one) {7202 if (dest_ty.ptrSize(zcu) != .one) {
7203 try bw.writeAll("if (");7203 try w.writeAll("if (");
7204 try writeArrayLen(f, dest_ptr, dest_ty);7204 try writeArrayLen(f, dest_ptr, dest_ty);
7205 try bw.writeAll(" != 0) ");7205 try w.writeAll(" != 0) ");
7206 }7206 }
7207 try bw.writeAll(function_paren);7207 try w.writeAll(function_paren);
7208 try writeSliceOrPtr(f, bw, dest_ptr, dest_ty);7208 try writeSliceOrPtr(f, w, dest_ptr, dest_ty);
7209 try bw.writeAll(", ");7209 try w.writeAll(", ");
7210 try writeSliceOrPtr(f, bw, src_ptr, src_ty);7210 try writeSliceOrPtr(f, w, src_ptr, src_ty);
7211 try bw.writeAll(", ");7211 try w.writeAll(", ");
7212 try writeArrayLen(f, dest_ptr, dest_ty);7212 try writeArrayLen(f, dest_ptr, dest_ty);
7213 try bw.writeAll(" * sizeof(");7213 try w.writeAll(" * sizeof(");
7214 try f.renderType(bw, dest_ty.elemType2(zcu));7214 try f.renderType(w, dest_ty.elemType2(zcu));
7215 try bw.writeAll("));");7215 try w.writeAll("));");
7216 try f.object.newline();7216 try f.object.newline();
72177217
7218 try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });7218 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...@@ -7222,13 +7222,13 @@ fn airMemcpy(f: *Function, inst: Air.Inst.Index, function_paren: []const u8) !CV
7222fn writeArrayLen(f: *Function, dest_ptr: CValue, dest_ty: Type) !void {7222fn writeArrayLen(f: *Function, dest_ptr: CValue, dest_ty: Type) !void {
7223 const pt = f.object.dg.pt;7223 const pt = f.object.dg.pt;
7224 const zcu = pt.zcu;7224 const zcu = pt.zcu;
7225 const bw = &f.object.code.buffered_writer;7225 const w = &f.object.code.buffered_writer;
7226 switch (dest_ty.ptrSize(zcu)) {7226 switch (dest_ty.ptrSize(zcu)) {
7227 .one => try bw.print("{f}", .{7227 .one => try w.print("{f}", .{
7228 try f.fmtIntLiteral(try pt.intValue(.usize, dest_ty.childType(zcu).arrayLen(zcu))),7228 try f.fmtIntLiteral(try pt.intValue(.usize, dest_ty.childType(zcu).arrayLen(zcu))),
7229 }),7229 }),
7230 .many, .c => unreachable,7230 .many, .c => unreachable,
7231 .slice => try f.writeCValueMember(bw, dest_ptr, .{ .identifier = "len" }),7231 .slice => try f.writeCValueMember(w, dest_ptr, .{ .identifier = "len" }),
7232 }7232 }
7233}7233}
72347234
...@@ -7245,12 +7245,12 @@ fn airSetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -7245,12 +7245,12 @@ fn airSetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue {
7245 if (layout.tag_size == 0) return .none;7245 if (layout.tag_size == 0) return .none;
7246 const tag_ty = union_ty.unionTagTypeSafety(zcu).?;7246 const tag_ty = union_ty.unionTagTypeSafety(zcu).?;
72477247
7248 const bw = &f.object.code.buffered_writer;7248 const w = &f.object.code.buffered_writer;
7249 const a = try Assignment.start(f, bw, try f.ctypeFromType(tag_ty, .complete));7249 const a = try Assignment.start(f, w, try f.ctypeFromType(tag_ty, .complete));
7250 try f.writeCValueDerefMember(bw, union_ptr, .{ .identifier = "tag" });7250 try f.writeCValueDerefMember(w, union_ptr, .{ .identifier = "tag" });
7251 try a.assign(f, bw);7251 try a.assign(f, w);
7252 try f.writeCValue(bw, new_tag, .Other);7252 try f.writeCValue(w, new_tag, .Other);
7253 try a.end(f, bw);7253 try a.end(f, w);
7254 return .none;7254 return .none;
7255}7255}
72567256
...@@ -7267,13 +7267,13 @@ fn airGetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -7267,13 +7267,13 @@ fn airGetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue {
7267 if (layout.tag_size == 0) return .none;7267 if (layout.tag_size == 0) return .none;
72687268
7269 const inst_ty = f.typeOfIndex(inst);7269 const inst_ty = f.typeOfIndex(inst);
7270 const bw = &f.object.code.buffered_writer;7270 const w = &f.object.code.buffered_writer;
7271 const local = try f.allocLocal(inst, inst_ty);7271 const local = try f.allocLocal(inst, inst_ty);
7272 const a = try Assignment.start(f, bw, try f.ctypeFromType(inst_ty, .complete));7272 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete));
7273 try f.writeCValue(bw, local, .Other);7273 try f.writeCValue(w, local, .Other);
7274 try a.assign(f, bw);7274 try a.assign(f, w);
7275 try f.writeCValueMember(bw, operand, .{ .identifier = "tag" });7275 try f.writeCValueMember(w, operand, .{ .identifier = "tag" });
7276 try a.end(f, bw);7276 try a.end(f, w);
7277 return local;7277 return local;
7278}7278}
72797279
...@@ -7285,14 +7285,14 @@ fn airTagName(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -7285,14 +7285,14 @@ fn airTagName(f: *Function, inst: Air.Inst.Index) !CValue {
7285 const operand = try f.resolveInst(un_op);7285 const operand = try f.resolveInst(un_op);
7286 try reap(f, inst, &.{un_op});7286 try reap(f, inst, &.{un_op});
72877287
7288 const bw = &f.object.code.buffered_writer;7288 const w = &f.object.code.buffered_writer;
7289 const local = try f.allocLocal(inst, inst_ty);7289 const local = try f.allocLocal(inst, inst_ty);
7290 try f.writeCValue(bw, local, .Other);7290 try f.writeCValue(w, local, .Other);
7291 try bw.print(" = {s}(", .{7291 try w.print(" = {s}(", .{
7292 try f.getLazyFnName(.{ .tag_name = enum_ty.toIntern() }),7292 try f.getLazyFnName(.{ .tag_name = enum_ty.toIntern() }),
7293 });7293 });
7294 try f.writeCValue(bw, operand, .Other);7294 try f.writeCValue(w, operand, .Other);
7295 try bw.writeAll(");");7295 try w.writeAll(");");
7296 try f.object.newline();7296 try f.object.newline();
72977297
7298 return local;7298 return local;
...@@ -7301,16 +7301,16 @@ fn airTagName(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -7301,16 +7301,16 @@ fn airTagName(f: *Function, inst: Air.Inst.Index) !CValue {
7301fn airErrorName(f: *Function, inst: Air.Inst.Index) !CValue {7301fn airErrorName(f: *Function, inst: Air.Inst.Index) !CValue {
7302 const un_op = f.air.instructions.items(.data)[@intFromEnum(inst)].un_op;7302 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;
7305 const inst_ty = f.typeOfIndex(inst);7305 const inst_ty = f.typeOfIndex(inst);
7306 const operand = try f.resolveInst(un_op);7306 const operand = try f.resolveInst(un_op);
7307 try reap(f, inst, &.{un_op});7307 try reap(f, inst, &.{un_op});
7308 const local = try f.allocLocal(inst, inst_ty);7308 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[");7311 try w.writeAll(" = zig_errorName[");
7312 try f.writeCValue(bw, operand, .Other);7312 try f.writeCValue(w, operand, .Other);
7313 try bw.writeAll(" - 1];");7313 try w.writeAll(" - 1];");
7314 try f.object.newline();7314 try f.object.newline();
7315 return local;7315 return local;
7316}7316}
...@@ -7326,16 +7326,16 @@ fn airSplat(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -7326,16 +7326,16 @@ fn airSplat(f: *Function, inst: Air.Inst.Index) !CValue {
7326 const inst_ty = f.typeOfIndex(inst);7326 const inst_ty = f.typeOfIndex(inst);
7327 const inst_scalar_ty = inst_ty.scalarType(zcu);7327 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;
7330 const local = try f.allocLocal(inst, inst_ty);7330 const local = try f.allocLocal(inst, inst_ty);
7331 const v = try Vectorize.start(f, inst, bw, inst_ty);7331 const v = try Vectorize.start(f, inst, w, inst_ty);
7332 const a = try Assignment.start(f, bw, try f.ctypeFromType(inst_scalar_ty, .complete));7332 const a = try Assignment.start(f, w, try f.ctypeFromType(inst_scalar_ty, .complete));
7333 try f.writeCValue(bw, local, .Other);7333 try f.writeCValue(w, local, .Other);
7334 try v.elem(f, bw);7334 try v.elem(f, w);
7335 try a.assign(f, bw);7335 try a.assign(f, w);
7336 try f.writeCValue(bw, operand, .Other);7336 try f.writeCValue(w, operand, .Other);
7337 try a.end(f, bw);7337 try a.end(f, w);
7338 try v.end(f, inst, bw);7338 try v.end(f, inst, w);
73397339
7340 return local;7340 return local;
7341}7341}
...@@ -7351,23 +7351,23 @@ fn airSelect(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -7351,23 +7351,23 @@ fn airSelect(f: *Function, inst: Air.Inst.Index) !CValue {
73517351
7352 const inst_ty = f.typeOfIndex(inst);7352 const inst_ty = f.typeOfIndex(inst);
73537353
7354 const bw = &f.object.code.buffered_writer;7354 const w = &f.object.code.buffered_writer;
7355 const local = try f.allocLocal(inst, inst_ty);7355 const local = try f.allocLocal(inst, inst_ty);
7356 const v = try Vectorize.start(f, inst, bw, inst_ty);7356 const v = try Vectorize.start(f, inst, w, inst_ty);
7357 try f.writeCValue(bw, local, .Other);7357 try f.writeCValue(w, local, .Other);
7358 try v.elem(f, bw);7358 try v.elem(f, w);
7359 try bw.writeAll(" = ");7359 try w.writeAll(" = ");
7360 try f.writeCValue(bw, pred, .Other);7360 try f.writeCValue(w, pred, .Other);
7361 try v.elem(f, bw);7361 try v.elem(f, w);
7362 try bw.writeAll(" ? ");7362 try w.writeAll(" ? ");
7363 try f.writeCValue(bw, lhs, .Other);7363 try f.writeCValue(w, lhs, .Other);
7364 try v.elem(f, bw);7364 try v.elem(f, w);
7365 try bw.writeAll(" : ");7365 try w.writeAll(" : ");
7366 try f.writeCValue(bw, rhs, .Other);7366 try f.writeCValue(w, rhs, .Other);
7367 try v.elem(f, bw);7367 try v.elem(f, w);
7368 try bw.writeByte(';');7368 try w.writeByte(';');
7369 try f.object.newline();7369 try f.object.newline();
7370 try v.end(f, inst, bw);7370 try v.end(f, inst, w);
73717371
7372 return local;7372 return local;
7373}7373}
...@@ -7381,24 +7381,24 @@ fn airShuffleOne(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -7381,24 +7381,24 @@ fn airShuffleOne(f: *Function, inst: Air.Inst.Index) !CValue {
7381 const operand = try f.resolveInst(unwrapped.operand);7381 const operand = try f.resolveInst(unwrapped.operand);
7382 const inst_ty = unwrapped.result_ty;7382 const inst_ty = unwrapped.result_ty;
73837383
7384 const bw = &f.object.code.buffered_writer;7384 const w = &f.object.code.buffered_writer;
7385 const local = try f.allocLocal(inst, inst_ty);7385 const local = try f.allocLocal(inst, inst_ty);
7386 try reap(f, inst, &.{unwrapped.operand}); // local cannot alias operand7386 try reap(f, inst, &.{unwrapped.operand}); // local cannot alias operand
7387 for (mask, 0..) |mask_elem, out_idx| {7387 for (mask, 0..) |mask_elem, out_idx| {
7388 try f.writeCValue(bw, local, .Other);7388 try f.writeCValue(w, local, .Other);
7389 try bw.writeByte('[');7389 try w.writeByte('[');
7390 try f.object.dg.renderValue(bw, try pt.intValue(.usize, out_idx), .Other);7390 try f.object.dg.renderValue(w, try pt.intValue(.usize, out_idx), .Other);
7391 try bw.writeAll("] = ");7391 try w.writeAll("] = ");
7392 switch (mask_elem.unwrap()) {7392 switch (mask_elem.unwrap()) {
7393 .elem => |src_idx| {7393 .elem => |src_idx| {
7394 try f.writeCValue(bw, operand, .Other);7394 try f.writeCValue(w, operand, .Other);
7395 try bw.writeByte('[');7395 try w.writeByte('[');
7396 try f.object.dg.renderValue(bw, try pt.intValue(.usize, src_idx), .Other);7396 try f.object.dg.renderValue(w, try pt.intValue(.usize, src_idx), .Other);
7397 try bw.writeByte(']');7397 try w.writeByte(']');
7398 },7398 },
7399 .value => |val| try f.object.dg.renderValue(bw, .fromInterned(val), .Other),7399 .value => |val| try f.object.dg.renderValue(w, .fromInterned(val), .Other),
7400 }7400 }
7401 try bw.writeAll(";\n");7401 try w.writeAll(";\n");
7402 }7402 }
74037403
7404 return local;7404 return local;
...@@ -7453,7 +7453,7 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -7453,7 +7453,7 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue {
7453 const operand = try f.resolveInst(reduce.operand);7453 const operand = try f.resolveInst(reduce.operand);
7454 try reap(f, inst, &.{reduce.operand});7454 try reap(f, inst, &.{reduce.operand});
7455 const operand_ty = f.typeOf(reduce.operand);7455 const operand_ty = f.typeOf(reduce.operand);
7456 const bw = &f.object.code.buffered_writer;7456 const w = &f.object.code.buffered_writer;
74577457
7458 const use_operator = scalar_ty.bitSize(zcu) <= 64;7458 const use_operator = scalar_ty.bitSize(zcu) <= 64;
7459 const op: union(enum) {7459 const op: union(enum) {
...@@ -7500,10 +7500,10 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -7500,10 +7500,10 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue {
7500 // }7500 // }
75017501
7502 const accum = try f.allocLocal(inst, scalar_ty);7502 const accum = try f.allocLocal(inst, scalar_ty);
7503 try f.writeCValue(bw, accum, .Other);7503 try f.writeCValue(w, accum, .Other);
7504 try bw.writeAll(" = ");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) {
7507 .Or, .Xor => switch (scalar_ty.zigTypeTag(zcu)) {7507 .Or, .Xor => switch (scalar_ty.zigTypeTag(zcu)) {
7508 .bool => Value.false,7508 .bool => Value.false,
7509 .int => try pt.intValue(scalar_ty, 0),7509 .int => try pt.intValue(scalar_ty, 0),
...@@ -7540,44 +7540,44 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -7540,44 +7540,44 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue {
7540 else => unreachable,7540 else => unreachable,
7541 },7541 },
7542 }, .Other);7542 }, .Other);
7543 try bw.writeByte(';');7543 try w.writeByte(';');
7544 try f.object.newline();7544 try f.object.newline();
75457545
7546 const v = try Vectorize.start(f, inst, bw, operand_ty);7546 const v = try Vectorize.start(f, inst, w, operand_ty);
7547 try f.writeCValue(bw, accum, .Other);7547 try f.writeCValue(w, accum, .Other);
7548 switch (op) {7548 switch (op) {
7549 .builtin => |func| {7549 .builtin => |func| {
7550 try bw.print(" = zig_{s}_", .{func.operation});7550 try w.print(" = zig_{s}_", .{func.operation});
7551 try f.object.dg.renderTypeForBuiltinFnName(bw, scalar_ty);7551 try f.object.dg.renderTypeForBuiltinFnName(w, scalar_ty);
7552 try bw.writeByte('(');7552 try w.writeByte('(');
7553 try f.writeCValue(bw, accum, .FunctionArgument);7553 try f.writeCValue(w, accum, .FunctionArgument);
7554 try bw.writeAll(", ");7554 try w.writeAll(", ");
7555 try f.writeCValue(bw, operand, .Other);7555 try f.writeCValue(w, operand, .Other);
7556 try v.elem(f, bw);7556 try v.elem(f, w);
7557 try f.object.dg.renderBuiltinInfo(bw, scalar_ty, func.info);7557 try f.object.dg.renderBuiltinInfo(w, scalar_ty, func.info);
7558 try bw.writeByte(')');7558 try w.writeByte(')');
7559 },7559 },
7560 .infix => |ass| {7560 .infix => |ass| {
7561 try bw.writeAll(ass);7561 try w.writeAll(ass);
7562 try f.writeCValue(bw, operand, .Other);7562 try f.writeCValue(w, operand, .Other);
7563 try v.elem(f, bw);7563 try v.elem(f, w);
7564 },7564 },
7565 .ternary => |cmp| {7565 .ternary => |cmp| {
7566 try bw.writeAll(" = ");7566 try w.writeAll(" = ");
7567 try f.writeCValue(bw, accum, .Other);7567 try f.writeCValue(w, accum, .Other);
7568 try bw.writeAll(cmp);7568 try w.writeAll(cmp);
7569 try f.writeCValue(bw, operand, .Other);7569 try f.writeCValue(w, operand, .Other);
7570 try v.elem(f, bw);7570 try v.elem(f, w);
7571 try bw.writeAll(" ? ");7571 try w.writeAll(" ? ");
7572 try f.writeCValue(bw, accum, .Other);7572 try f.writeCValue(w, accum, .Other);
7573 try bw.writeAll(" : ");7573 try w.writeAll(" : ");
7574 try f.writeCValue(bw, operand, .Other);7574 try f.writeCValue(w, operand, .Other);
7575 try v.elem(f, bw);7575 try v.elem(f, w);
7576 },7576 },
7577 }7577 }
7578 try bw.writeByte(';');7578 try w.writeByte(';');
7579 try f.object.newline();7579 try f.object.newline();
7580 try v.end(f, inst, bw);7580 try v.end(f, inst, w);
75817581
7582 return accum;7582 return accum;
7583}7583}
...@@ -7603,7 +7603,7 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -7603,7 +7603,7 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue {
7603 }7603 }
7604 }7604 }
76057605
7606 const bw = &f.object.code.buffered_writer;7606 const w = &f.object.code.buffered_writer;
7607 const local = try f.allocLocal(inst, inst_ty);7607 const local = try f.allocLocal(inst, inst_ty);
7608 switch (ip.indexToKey(inst_ty.toIntern())) {7608 switch (ip.indexToKey(inst_ty.toIntern())) {
7609 inline .array_type, .vector_type => |info, tag| {7609 inline .array_type, .vector_type => |info, tag| {
...@@ -7611,20 +7611,20 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -7611,20 +7611,20 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue {
7611 .ctype = try f.ctypeFromType(.fromInterned(info.child), .complete),7611 .ctype = try f.ctypeFromType(.fromInterned(info.child), .complete),
7612 };7612 };
7613 for (resolved_elements, 0..) |element, i| {7613 for (resolved_elements, 0..) |element, i| {
7614 try a.restart(f, bw);7614 try a.restart(f, w);
7615 try f.writeCValue(bw, local, .Other);7615 try f.writeCValue(w, local, .Other);
7616 try bw.print("[{d}]", .{i});7616 try w.print("[{d}]", .{i});
7617 try a.assign(f, bw);7617 try a.assign(f, w);
7618 try f.writeCValue(bw, element, .Other);7618 try f.writeCValue(w, element, .Other);
7619 try a.end(f, bw);7619 try a.end(f, w);
7620 }7620 }
7621 if (tag == .array_type and info.sentinel != .none) {7621 if (tag == .array_type and info.sentinel != .none) {
7622 try a.restart(f, bw);7622 try a.restart(f, w);
7623 try f.writeCValue(bw, local, .Other);7623 try f.writeCValue(w, local, .Other);
7624 try bw.print("[{d}]", .{info.len});7624 try w.print("[{d}]", .{info.len});
7625 try a.assign(f, bw);7625 try a.assign(f, w);
7626 try f.object.dg.renderValue(bw, Value.fromInterned(info.sentinel), .Other);7626 try f.object.dg.renderValue(w, Value.fromInterned(info.sentinel), .Other);
7627 try a.end(f, bw);7627 try a.end(f, w);
7628 }7628 }
7629 },7629 },
7630 .struct_type => {7630 .struct_type => {
...@@ -7636,19 +7636,19 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -7636,19 +7636,19 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue {
7636 const field_ty: Type = .fromInterned(loaded_struct.field_types.get(ip)[field_index]);7636 const field_ty: Type = .fromInterned(loaded_struct.field_types.get(ip)[field_index]);
7637 if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue;7637 if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue;
76387638
7639 const a = try Assignment.start(f, bw, try f.ctypeFromType(field_ty, .complete));7639 const a = try Assignment.start(f, w, try f.ctypeFromType(field_ty, .complete));
7640 try f.writeCValueMember(bw, local, if (loaded_struct.fieldName(ip, field_index).unwrap()) |field_name|7640 try f.writeCValueMember(w, local, if (loaded_struct.fieldName(ip, field_index).unwrap()) |field_name|
7641 .{ .identifier = field_name.toSlice(ip) }7641 .{ .identifier = field_name.toSlice(ip) }
7642 else7642 else
7643 .{ .field = field_index });7643 .{ .field = field_index });
7644 try a.assign(f, bw);7644 try a.assign(f, w);
7645 try f.writeCValue(bw, resolved_elements[field_index], .Other);7645 try f.writeCValue(w, resolved_elements[field_index], .Other);
7646 try a.end(f, bw);7646 try a.end(f, w);
7647 }7647 }
7648 },7648 },
7649 .@"packed" => {7649 .@"packed" => {
7650 try f.writeCValue(bw, local, .Other);7650 try f.writeCValue(w, local, .Other);
7651 try bw.writeAll(" = ");7651 try w.writeAll(" = ");
76527652
7653 const backing_int_ty: Type = .fromInterned(loaded_struct.backingIntTypeUnordered(ip));7653 const backing_int_ty: Type = .fromInterned(loaded_struct.backingIntTypeUnordered(ip));
7654 const int_info = backing_int_ty.intInfo(zcu);7654 const int_info = backing_int_ty.intInfo(zcu);
...@@ -7664,9 +7664,9 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -7664,9 +7664,9 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue {
7664 if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue;7664 if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue;
76657665
7666 if (!empty) {7666 if (!empty) {
7667 try bw.writeAll("zig_or_");7667 try w.writeAll("zig_or_");
7668 try f.object.dg.renderTypeForBuiltinFnName(bw, inst_ty);7668 try f.object.dg.renderTypeForBuiltinFnName(w, inst_ty);
7669 try bw.writeByte('(');7669 try w.writeByte('(');
7670 }7670 }
7671 empty = false;7671 empty = false;
7672 }7672 }
...@@ -7676,57 +7676,57 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -7676,57 +7676,57 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue {
7676 const field_ty = inst_ty.fieldType(field_index, zcu);7676 const field_ty = inst_ty.fieldType(field_index, zcu);
7677 if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue;7677 if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue;
76787678
7679 if (!empty) try bw.writeAll(", ");7679 if (!empty) try w.writeAll(", ");
7680 // TODO: Skip this entire shift if val is 0?7680 // TODO: Skip this entire shift if val is 0?
7681 try bw.writeAll("zig_shlw_");7681 try w.writeAll("zig_shlw_");
7682 try f.object.dg.renderTypeForBuiltinFnName(bw, inst_ty);7682 try f.object.dg.renderTypeForBuiltinFnName(w, inst_ty);
7683 try bw.writeByte('(');7683 try w.writeByte('(');
76847684
7685 if (field_ty.isAbiInt(zcu)) {7685 if (field_ty.isAbiInt(zcu)) {
7686 try bw.writeAll("zig_and_");7686 try w.writeAll("zig_and_");
7687 try f.object.dg.renderTypeForBuiltinFnName(bw, inst_ty);7687 try f.object.dg.renderTypeForBuiltinFnName(w, inst_ty);
7688 try bw.writeByte('(');7688 try w.writeByte('(');
7689 }7689 }
76907690
7691 if (inst_ty.isAbiInt(zcu) and (field_ty.isAbiInt(zcu) or field_ty.isPtrAtRuntime(zcu))) {7691 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);
7693 } else {7693 } else {
7694 try bw.writeByte('(');7694 try w.writeByte('(');
7695 try f.renderType(bw, inst_ty);7695 try f.renderType(w, inst_ty);
7696 try bw.writeByte(')');7696 try w.writeByte(')');
7697 if (field_ty.isPtrAtRuntime(zcu)) {7697 if (field_ty.isPtrAtRuntime(zcu)) {
7698 try bw.writeByte('(');7698 try w.writeByte('(');
7699 try f.renderType(bw, switch (int_info.signedness) {7699 try f.renderType(w, switch (int_info.signedness) {
7700 .unsigned => .usize,7700 .unsigned => .usize,
7701 .signed => .isize,7701 .signed => .isize,
7702 });7702 });
7703 try bw.writeByte(')');7703 try w.writeByte(')');
7704 }7704 }
7705 try f.writeCValue(bw, element, .Other);7705 try f.writeCValue(w, element, .Other);
7706 }7706 }
77077707
7708 if (field_ty.isAbiInt(zcu)) {7708 if (field_ty.isAbiInt(zcu)) {
7709 try bw.writeAll(", ");7709 try w.writeAll(", ");
7710 const field_int_info = field_ty.intInfo(zcu);7710 const field_int_info = field_ty.intInfo(zcu);
7711 const field_mask = if (int_info.signedness == .signed and int_info.bits == field_int_info.bits)7711 const field_mask = if (int_info.signedness == .signed and int_info.bits == field_int_info.bits)
7712 try pt.intValue(backing_int_ty, -1)7712 try pt.intValue(backing_int_ty, -1)
7713 else7713 else
7714 try (try pt.intType(.unsigned, field_int_info.bits)).maxIntScalar(pt, backing_int_ty);7714 try (try pt.intType(.unsigned, field_int_info.bits)).maxIntScalar(pt, backing_int_ty);
7715 try f.object.dg.renderValue(bw, field_mask, .FunctionArgument);7715 try f.object.dg.renderValue(w, field_mask, .FunctionArgument);
7716 try bw.writeByte(')');7716 try w.writeByte(')');
7717 }7717 }
77187718
7719 try bw.print(", {f}", .{7719 try w.print(", {f}", .{
7720 try f.fmtIntLiteral(try pt.intValue(bit_offset_ty, bit_offset)),7720 try f.fmtIntLiteral(try pt.intValue(bit_offset_ty, bit_offset)),
7721 });7721 });
7722 try f.object.dg.renderBuiltinInfo(bw, inst_ty, .bits);7722 try f.object.dg.renderBuiltinInfo(w, inst_ty, .bits);
7723 try bw.writeByte(')');7723 try w.writeByte(')');
7724 if (!empty) try bw.writeByte(')');7724 if (!empty) try w.writeByte(')');
77257725
7726 bit_offset += field_ty.bitSize(zcu);7726 bit_offset += field_ty.bitSize(zcu);
7727 empty = false;7727 empty = false;
7728 }7728 }
7729 try bw.writeByte(';');7729 try w.writeByte(';');
7730 try f.object.newline();7730 try f.object.newline();
7731 },7731 },
7732 }7732 }
...@@ -7736,11 +7736,11 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -7736,11 +7736,11 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue {
7736 const field_ty: Type = .fromInterned(tuple_info.types.get(ip)[field_index]);7736 const field_ty: Type = .fromInterned(tuple_info.types.get(ip)[field_index]);
7737 if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue;7737 if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue;
77387738
7739 const a = try Assignment.start(f, bw, try f.ctypeFromType(field_ty, .complete));7739 const a = try Assignment.start(f, w, try f.ctypeFromType(field_ty, .complete));
7740 try f.writeCValueMember(bw, local, .{ .field = field_index });7740 try f.writeCValueMember(w, local, .{ .field = field_index });
7741 try a.assign(f, bw);7741 try a.assign(f, w);
7742 try f.writeCValue(bw, resolved_elements[field_index], .Other);7742 try f.writeCValue(w, resolved_elements[field_index], .Other);
7743 try a.end(f, bw);7743 try a.end(f, w);
7744 },7744 },
7745 else => unreachable,7745 else => unreachable,
7746 }7746 }
...@@ -7762,7 +7762,7 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -7762,7 +7762,7 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue {
7762 const payload = try f.resolveInst(extra.init);7762 const payload = try f.resolveInst(extra.init);
7763 try reap(f, inst, &.{extra.init});7763 try reap(f, inst, &.{extra.init});
77647764
7765 const bw = &f.object.code.buffered_writer;7765 const w = &f.object.code.buffered_writer;
7766 const local = try f.allocLocal(inst, union_ty);7766 const local = try f.allocLocal(inst, union_ty);
7767 if (loaded_union.flagsUnordered(ip).layout == .@"packed") return f.moveCValue(inst, union_ty, payload);7767 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 {...@@ -7772,20 +7772,20 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue {
7772 const field_index = tag_ty.enumFieldIndex(field_name, zcu).?;7772 const field_index = tag_ty.enumFieldIndex(field_name, zcu).?;
7773 const tag_val = try pt.enumValueFieldIndex(tag_ty, field_index);7773 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));7775 const a = try Assignment.start(f, w, try f.ctypeFromType(tag_ty, .complete));
7776 try f.writeCValueMember(bw, local, .{ .identifier = "tag" });7776 try f.writeCValueMember(w, local, .{ .identifier = "tag" });
7777 try a.assign(f, bw);7777 try a.assign(f, w);
7778 try bw.print("{f}", .{try f.fmtIntLiteral(try tag_val.intFromEnum(tag_ty, pt))});7778 try w.print("{f}", .{try f.fmtIntLiteral(try tag_val.intFromEnum(tag_ty, pt))});
7779 try a.end(f, bw);7779 try a.end(f, w);
7780 }7780 }
7781 break :field .{ .payload_identifier = field_name.toSlice(ip) };7781 break :field .{ .payload_identifier = field_name.toSlice(ip) };
7782 } else .{ .identifier = field_name.toSlice(ip) };7782 } else .{ .identifier = field_name.toSlice(ip) };
77837783
7784 const a = try Assignment.start(f, bw, try f.ctypeFromType(payload_ty, .complete));7784 const a = try Assignment.start(f, w, try f.ctypeFromType(payload_ty, .complete));
7785 try f.writeCValueMember(bw, local, field);7785 try f.writeCValueMember(w, local, field);
7786 try a.assign(f, bw);7786 try a.assign(f, w);
7787 try f.writeCValue(bw, payload, .Other);7787 try f.writeCValue(w, payload, .Other);
7788 try a.end(f, bw);7788 try a.end(f, w);
7789 return local;7789 return local;
7790}7790}
77917791
...@@ -7798,15 +7798,15 @@ fn airPrefetch(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -7798,15 +7798,15 @@ fn airPrefetch(f: *Function, inst: Air.Inst.Index) !CValue {
7798 const ptr = try f.resolveInst(prefetch.ptr);7798 const ptr = try f.resolveInst(prefetch.ptr);
7799 try reap(f, inst, &.{prefetch.ptr});7799 try reap(f, inst, &.{prefetch.ptr});
78007800
7801 const bw = &f.object.code.buffered_writer;7801 const w = &f.object.code.buffered_writer;
7802 switch (prefetch.cache) {7802 switch (prefetch.cache) {
7803 .data => {7803 .data => {
7804 try bw.writeAll("zig_prefetch(");7804 try w.writeAll("zig_prefetch(");
7805 if (ptr_ty.isSlice(zcu))7805 if (ptr_ty.isSlice(zcu))
7806 try f.writeCValueMember(bw, ptr, .{ .identifier = "ptr" })7806 try f.writeCValueMember(w, ptr, .{ .identifier = "ptr" })
7807 else7807 else
7808 try f.writeCValue(bw, ptr, .FunctionArgument);7808 try f.writeCValue(w, ptr, .FunctionArgument);
7809 try bw.print(", {d}, {d});", .{ @intFromEnum(prefetch.rw), prefetch.locality });7809 try w.print(", {d}, {d});", .{ @intFromEnum(prefetch.rw), prefetch.locality });
7810 try f.object.newline();7810 try f.object.newline();
7811 },7811 },
7812 // The available prefetch intrinsics do not accept a cache argument; only7812 // The available prefetch intrinsics do not accept a cache argument; only
...@@ -7820,13 +7820,13 @@ fn airPrefetch(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -7820,13 +7820,13 @@ fn airPrefetch(f: *Function, inst: Air.Inst.Index) !CValue {
7820fn airWasmMemorySize(f: *Function, inst: Air.Inst.Index) !CValue {7820fn airWasmMemorySize(f: *Function, inst: Air.Inst.Index) !CValue {
7821 const pl_op = f.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;7821 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;
7824 const inst_ty = f.typeOfIndex(inst);7824 const inst_ty = f.typeOfIndex(inst);
7825 const local = try f.allocLocal(inst, inst_ty);7825 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(" = ");7828 try w.writeAll(" = ");
7829 try bw.print("zig_wasm_memory_size({d});", .{pl_op.payload});7829 try w.print("zig_wasm_memory_size({d});", .{pl_op.payload});
7830 try f.object.newline();7830 try f.object.newline();
78317831
7832 return local;7832 return local;
...@@ -7835,17 +7835,17 @@ fn airWasmMemorySize(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -7835,17 +7835,17 @@ fn airWasmMemorySize(f: *Function, inst: Air.Inst.Index) !CValue {
7835fn airWasmMemoryGrow(f: *Function, inst: Air.Inst.Index) !CValue {7835fn airWasmMemoryGrow(f: *Function, inst: Air.Inst.Index) !CValue {
7836 const pl_op = f.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;7836 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;
7839 const inst_ty = f.typeOfIndex(inst);7839 const inst_ty = f.typeOfIndex(inst);
7840 const operand = try f.resolveInst(pl_op.operand);7840 const operand = try f.resolveInst(pl_op.operand);
7841 try reap(f, inst, &.{pl_op.operand});7841 try reap(f, inst, &.{pl_op.operand});
7842 const local = try f.allocLocal(inst, inst_ty);7842 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(" = ");7845 try w.writeAll(" = ");
7846 try bw.print("zig_wasm_memory_grow({d}, ", .{pl_op.payload});7846 try w.print("zig_wasm_memory_grow({d}, ", .{pl_op.payload});
7847 try f.writeCValue(bw, operand, .FunctionArgument);7847 try f.writeCValue(w, operand, .FunctionArgument);
7848 try bw.writeAll(");");7848 try w.writeAll(");");
7849 try f.object.newline();7849 try f.object.newline();
7850 return local;7850 return local;
7851}7851}
...@@ -7864,25 +7864,25 @@ fn airMulAdd(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -7864,25 +7864,25 @@ fn airMulAdd(f: *Function, inst: Air.Inst.Index) !CValue {
7864 const inst_ty = f.typeOfIndex(inst);7864 const inst_ty = f.typeOfIndex(inst);
7865 const inst_scalar_ty = inst_ty.scalarType(zcu);7865 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;
7868 const local = try f.allocLocal(inst, inst_ty);7868 const local = try f.allocLocal(inst, inst_ty);
7869 const v = try Vectorize.start(f, inst, bw, inst_ty);7869 const v = try Vectorize.start(f, inst, w, inst_ty);
7870 try f.writeCValue(bw, local, .Other);7870 try f.writeCValue(w, local, .Other);
7871 try v.elem(f, bw);7871 try v.elem(f, w);
7872 try bw.writeAll(" = zig_fma_");7872 try w.writeAll(" = zig_fma_");
7873 try f.object.dg.renderTypeForBuiltinFnName(bw, inst_scalar_ty);7873 try f.object.dg.renderTypeForBuiltinFnName(w, inst_scalar_ty);
7874 try bw.writeByte('(');7874 try w.writeByte('(');
7875 try f.writeCValue(bw, mulend1, .FunctionArgument);7875 try f.writeCValue(w, mulend1, .FunctionArgument);
7876 try v.elem(f, bw);7876 try v.elem(f, w);
7877 try bw.writeAll(", ");7877 try w.writeAll(", ");
7878 try f.writeCValue(bw, mulend2, .FunctionArgument);7878 try f.writeCValue(w, mulend2, .FunctionArgument);
7879 try v.elem(f, bw);7879 try v.elem(f, w);
7880 try bw.writeAll(", ");7880 try w.writeAll(", ");
7881 try f.writeCValue(bw, addend, .FunctionArgument);7881 try f.writeCValue(w, addend, .FunctionArgument);
7882 try v.elem(f, bw);7882 try v.elem(f, w);
7883 try bw.writeAll(");");7883 try w.writeAll(");");
7884 try f.object.newline();7884 try f.object.newline();
7885 try v.end(f, inst, bw);7885 try v.end(f, inst, w);
78867886
7887 return local;7887 return local;
7888}7888}
...@@ -7906,15 +7906,15 @@ fn airCVaStart(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -7906,15 +7906,15 @@ fn airCVaStart(f: *Function, inst: Air.Inst.Index) !CValue {
7906 const function_info = (try f.ctypeFromType(function_ty, .complete)).info(&f.object.dg.ctype_pool).function;7906 const function_info = (try f.ctypeFromType(function_ty, .complete)).info(&f.object.dg.ctype_pool).function;
7907 assert(function_info.varargs);7907 assert(function_info.varargs);
79087908
7909 const bw = &f.object.code.buffered_writer;7909 const w = &f.object.code.buffered_writer;
7910 const local = try f.allocLocal(inst, inst_ty);7910 const local = try f.allocLocal(inst, inst_ty);
7911 try bw.writeAll("va_start(*(va_list *)&");7911 try w.writeAll("va_start(*(va_list *)&");
7912 try f.writeCValue(bw, local, .Other);7912 try f.writeCValue(w, local, .Other);
7913 if (function_info.param_ctypes.len > 0) {7913 if (function_info.param_ctypes.len > 0) {
7914 try bw.writeAll(", ");7914 try w.writeAll(", ");
7915 try f.writeCValue(bw, .{ .arg = function_info.param_ctypes.len - 1 }, .FunctionArgument);7915 try f.writeCValue(w, .{ .arg = function_info.param_ctypes.len - 1 }, .FunctionArgument);
7916 }7916 }
7917 try bw.writeAll(");");7917 try w.writeAll(");");
7918 try f.object.newline();7918 try f.object.newline();
7919 return local;7919 return local;
7920}7920}
...@@ -7926,14 +7926,14 @@ fn airCVaArg(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -7926,14 +7926,14 @@ fn airCVaArg(f: *Function, inst: Air.Inst.Index) !CValue {
7926 const va_list = try f.resolveInst(ty_op.operand);7926 const va_list = try f.resolveInst(ty_op.operand);
7927 try reap(f, inst, &.{ty_op.operand});7927 try reap(f, inst, &.{ty_op.operand});
79287928
7929 const bw = &f.object.code.buffered_writer;7929 const w = &f.object.code.buffered_writer;
7930 const local = try f.allocLocal(inst, inst_ty);7930 const local = try f.allocLocal(inst, inst_ty);
7931 try f.writeCValue(bw, local, .Other);7931 try f.writeCValue(w, local, .Other);
7932 try bw.writeAll(" = va_arg(*(va_list *)");7932 try w.writeAll(" = va_arg(*(va_list *)");
7933 try f.writeCValue(bw, va_list, .Other);7933 try f.writeCValue(w, va_list, .Other);
7934 try bw.writeAll(", ");7934 try w.writeAll(", ");
7935 try f.renderType(bw, ty_op.ty.toType());7935 try f.renderType(w, ty_op.ty.toType());
7936 try bw.writeAll(");");7936 try w.writeAll(");");
7937 try f.object.newline();7937 try f.object.newline();
7938 return local;7938 return local;
7939}7939}
...@@ -7944,10 +7944,10 @@ fn airCVaEnd(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -7944,10 +7944,10 @@ fn airCVaEnd(f: *Function, inst: Air.Inst.Index) !CValue {
7944 const va_list = try f.resolveInst(un_op);7944 const va_list = try f.resolveInst(un_op);
7945 try reap(f, inst, &.{un_op});7945 try reap(f, inst, &.{un_op});
79467946
7947 const bw = &f.object.code.buffered_writer;7947 const w = &f.object.code.buffered_writer;
7948 try bw.writeAll("va_end(*(va_list *)");7948 try w.writeAll("va_end(*(va_list *)");
7949 try f.writeCValue(bw, va_list, .Other);7949 try f.writeCValue(w, va_list, .Other);
7950 try bw.writeAll(");");7950 try w.writeAll(");");
7951 try f.object.newline();7951 try f.object.newline();
7952 return .none;7952 return .none;
7953}7953}
...@@ -7959,13 +7959,13 @@ fn airCVaCopy(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -7959,13 +7959,13 @@ fn airCVaCopy(f: *Function, inst: Air.Inst.Index) !CValue {
7959 const va_list = try f.resolveInst(ty_op.operand);7959 const va_list = try f.resolveInst(ty_op.operand);
7960 try reap(f, inst, &.{ty_op.operand});7960 try reap(f, inst, &.{ty_op.operand});
79617961
7962 const bw = &f.object.code.buffered_writer;7962 const w = &f.object.code.buffered_writer;
7963 const local = try f.allocLocal(inst, inst_ty);7963 const local = try f.allocLocal(inst, inst_ty);
7964 try bw.writeAll("va_copy(*(va_list *)&");7964 try w.writeAll("va_copy(*(va_list *)&");
7965 try f.writeCValue(bw, local, .Other);7965 try f.writeCValue(w, local, .Other);
7966 try bw.writeAll(", *(va_list *)");7966 try w.writeAll(", *(va_list *)");
7967 try f.writeCValue(bw, va_list, .Other);7967 try f.writeCValue(w, va_list, .Other);
7968 try bw.writeAll(");");7968 try w.writeAll(");");
7969 try f.object.newline();7969 try f.object.newline();
7970 return local;7970 return local;
7971}7971}
...@@ -7981,8 +7981,8 @@ fn toMemoryOrder(order: std.builtin.AtomicOrder) [:0]const u8 {...@@ -7981,8 +7981,8 @@ fn toMemoryOrder(order: std.builtin.AtomicOrder) [:0]const u8 {
7981 };7981 };
7982}7982}
79837983
7984fn writeMemoryOrder(bw: *Writer, order: std.builtin.AtomicOrder) !void {7984fn writeMemoryOrder(w: *Writer, order: std.builtin.AtomicOrder) !void {
7985 return bw.writeAll(toMemoryOrder(order));7985 return w.writeAll(toMemoryOrder(order));
7986}7986}
79877987
7988fn toCallingConvention(cc: std.builtin.CallingConvention, zcu: *Zcu) ?[]const u8 {7988fn toCallingConvention(cc: std.builtin.CallingConvention, zcu: *Zcu) ?[]const u8 {
...@@ -8126,7 +8126,7 @@ const StringLiteral = struct {...@@ -8126,7 +8126,7 @@ const StringLiteral = struct {
8126 len: usize,8126 len: usize,
8127 cur_len: usize,8127 cur_len: usize,
8128 start_count: usize,8128 start_count: usize,
8129 bw: *Writer,8129 w: *Writer,
81308130
8131 // MSVC throws C2078 if an array of size 65536 or greater is initialized with a string literal,8131 // MSVC throws C2078 if an array of size 65536 or greater is initialized with a string literal,
8132 // regardless of the length of the string literal initializing it. Array initializer syntax is8132 // regardless of the length of the string literal initializing it. Array initializer syntax is
...@@ -8139,63 +8139,63 @@ const StringLiteral = struct {...@@ -8139,63 +8139,63 @@ const StringLiteral = struct {
8139 const max_char_len = 4;8139 const max_char_len = 4;
8140 const max_literal_len = @min(16380 - max_char_len, 4095);8140 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 {
8143 return .{8143 return .{
8144 .cur_len = 0,8144 .cur_len = 0,
8145 .len = len,8145 .len = len,
8146 .start_count = bw.count,8146 .start_count = w.count,
8147 .bw = bw,8147 .w = w,
8148 };8148 };
8149 }8149 }
81508150
8151 pub fn start(sl: *StringLiteral) Writer.Error!void {8151 pub fn start(sl: *StringLiteral) Writer.Error!void {
8152 if (sl.len <= max_string_initializer_len) {8152 if (sl.len <= max_string_initializer_len) {
8153 try sl.bw.writeByte('\"');8153 try sl.w.writeByte('\"');
8154 } else {8154 } else {
8155 try sl.bw.writeByte('{');8155 try sl.w.writeByte('{');
8156 }8156 }
8157 }8157 }
81588158
8159 pub fn end(sl: *StringLiteral) Writer.Error!void {8159 pub fn end(sl: *StringLiteral) Writer.Error!void {
8160 if (sl.len <= max_string_initializer_len) {8160 if (sl.len <= max_string_initializer_len) {
8161 try sl.bw.writeByte('\"');8161 try sl.w.writeByte('\"');
8162 } else {8162 } else {
8163 try sl.bw.writeByte('}');8163 try sl.w.writeByte('}');
8164 }8164 }
8165 }8165 }
81668166
8167 fn writeStringLiteralChar(sl: *StringLiteral, c: u8) Writer.Error!void {8167 fn writeStringLiteralChar(sl: *StringLiteral, c: u8) Writer.Error!void {
8168 switch (c) {8168 switch (c) {
8169 7 => try sl.bw.writeAll("\\a"),8169 7 => try sl.w.writeAll("\\a"),
8170 8 => try sl.bw.writeAll("\\b"),8170 8 => try sl.w.writeAll("\\b"),
8171 '\t' => try sl.bw.writeAll("\\t"),8171 '\t' => try sl.w.writeAll("\\t"),
8172 '\n' => try sl.bw.writeAll("\\n"),8172 '\n' => try sl.w.writeAll("\\n"),
8173 11 => try sl.bw.writeAll("\\v"),8173 11 => try sl.w.writeAll("\\v"),
8174 12 => try sl.bw.writeAll("\\f"),8174 12 => try sl.w.writeAll("\\f"),
8175 '\r' => try sl.bw.writeAll("\\r"),8175 '\r' => try sl.w.writeAll("\\r"),
8176 '"', '\'', '?', '\\' => try sl.bw.print("\\{c}", .{c}),8176 '"', '\'', '?', '\\' => try sl.w.print("\\{c}", .{c}),
8177 else => switch (c) {8177 else => switch (c) {
8178 ' '...'~' => try sl.bw.writeByte(c),8178 ' '...'~' => try sl.w.writeByte(c),
8179 else => try sl.bw.print("\\{o:0>3}", .{c}),8179 else => try sl.w.print("\\{o:0>3}", .{c}),
8180 },8180 },
8181 }8181 }
8182 }8182 }
81838183
8184 pub fn writeChar(sl: *StringLiteral, c: u8) Writer.Error!void {8184 pub fn writeChar(sl: *StringLiteral, c: u8) Writer.Error!void {
8185 if (sl.len <= max_string_initializer_len) {8185 if (sl.len <= max_string_initializer_len) {
8186 if (sl.cur_len == 0 and sl.bw.count - sl.start_count > 1)8186 if (sl.cur_len == 0 and sl.w.count - sl.start_count > 1)
8187 try sl.bw.writeAll("\"\"");8187 try sl.w.writeAll("\"\"");
81888188
8189 const count = sl.bw.count;8189 const count = sl.w.count;
8190 try sl.writeStringLiteralChar(c);8190 try sl.writeStringLiteralChar(c);
8191 const char_len = sl.bw.count - count;8191 const char_len = sl.w.count - count;
8192 assert(char_len <= max_char_len);8192 assert(char_len <= max_char_len);
8193 sl.cur_len += char_len;8193 sl.cur_len += char_len;
81948194
8195 if (sl.cur_len >= max_literal_len) sl.cur_len = 0;8195 if (sl.cur_len >= max_literal_len) sl.cur_len = 0;
8196 } else {8196 } else {
8197 if (sl.bw.count - sl.start_count > 1) try sl.bw.writeByte(',');8197 if (sl.w.count - sl.start_count > 1) try sl.w.writeByte(',');
8198 try sl.bw.print("'\\x{x}'", .{c});8198 try sl.w.print("'\\x{x}'", .{c});
8199 }8199 }
8200 }8200 }
8201};8201};
...@@ -8203,12 +8203,12 @@ const StringLiteral = struct {...@@ -8203,12 +8203,12 @@ const StringLiteral = struct {
8203const FormatStringContext = struct { str: []const u8, sentinel: ?u8 };8203const FormatStringContext = struct { str: []const u8, sentinel: ?u8 };
8204fn formatStringLiteral(8204fn formatStringLiteral(
8205 data: FormatStringContext,8205 data: FormatStringContext,
8206 bw: *Writer,8206 w: *Writer,
8207 comptime fmt: []const u8,8207 comptime fmt: []const u8,
8208) Writer.Error!void {8208) Writer.Error!void {
8209 if (fmt.len != 1 or fmt[0] != 's') @compileError("Invalid fmt: " ++ fmt);8209 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));
8212 try literal.start();8212 try literal.start();
8213 for (data.str) |c| try literal.writeChar(c);8213 for (data.str) |c| try literal.writeChar(c);
8214 if (data.sentinel) |sentinel| if (sentinel != 0) try literal.writeChar(sentinel);8214 if (data.sentinel) |sentinel| if (sentinel != 0) try literal.writeChar(sentinel);
...@@ -8234,7 +8234,7 @@ const FormatIntLiteralContext = struct {...@@ -8234,7 +8234,7 @@ const FormatIntLiteralContext = struct {
8234};8234};
8235fn formatIntLiteral(8235fn formatIntLiteral(
8236 data: FormatIntLiteralContext,8236 data: FormatIntLiteralContext,
8237 bw: *Writer,8237 w: *Writer,
8238 comptime fmt: []const u8,8238 comptime fmt: []const u8,
8239) Writer.Error!void {8239) Writer.Error!void {
8240 const pt = data.dg.pt;8240 const pt = data.dg.pt;
...@@ -8316,30 +8316,30 @@ fn formatIntLiteral(...@@ -8316,30 +8316,30 @@ fn formatIntLiteral(
8316 if (c_limb_info.count == 1) {8316 if (c_limb_info.count == 1) {
8317 if (wrap.addWrap(int, one, data.int_info.signedness, c_bits) or8317 if (wrap.addWrap(int, one, data.int_info.signedness, c_bits) or
8318 data.int_info.signedness == .signed and wrap.subWrap(int, one, data.int_info.signedness, c_bits))8318 data.int_info.signedness == .signed and wrap.subWrap(int, one, data.int_info.signedness, c_bits))
8319 return bw.print("{s}_{s}", .{8319 return w.print("{s}_{s}", .{
8320 data.ctype.getStandardDefineAbbrev() orelse return bw.print("zig_{s}Int_{c}{d}", .{8320 data.ctype.getStandardDefineAbbrev() orelse return w.print("zig_{s}Int_{c}{d}", .{
8321 if (int.positive) "max" else "min", signAbbrev(data.int_info.signedness), c_bits,8321 if (int.positive) "max" else "min", signAbbrev(data.int_info.signedness), c_bits,
8322 }),8322 }),
8323 if (int.positive) "MAX" else "MIN",8323 if (int.positive) "MAX" else "MIN",
8324 });8324 });
83258325
8326 if (!int.positive) try bw.writeByte('-');8326 if (!int.positive) try w.writeByte('-');
8327 try data.ctype.renderLiteralPrefix(bw, data.kind, ctype_pool);8327 try data.ctype.renderLiteralPrefix(w, data.kind, ctype_pool);
83288328
8329 const style: struct { base: u8, case: std.fmt.Case = undefined } = switch (fmt.len) {8329 const style: struct { base: u8, case: std.fmt.Case = undefined } = switch (fmt.len) {
8330 0 => .{ .base = 10 },8330 0 => .{ .base = 10 },
8331 1 => switch (fmt[0]) {8331 1 => switch (fmt[0]) {
8332 'b' => style: {8332 'b' => style: {
8333 try bw.writeAll("0b");8333 try w.writeAll("0b");
8334 break :style .{ .base = 2 };8334 break :style .{ .base = 2 };
8335 },8335 },
8336 'o' => style: {8336 'o' => style: {
8337 try bw.writeByte('0');8337 try w.writeByte('0');
8338 break :style .{ .base = 8 };8338 break :style .{ .base = 8 };
8339 },8339 },
8340 'd' => .{ .base = 10 },8340 'd' => .{ .base = 10 },
8341 'x', 'X' => |base| style: {8341 'x', 'X' => |base| style: {
8342 try bw.writeAll("0x");8342 try w.writeAll("0x");
8343 break :style .{ .base = 16, .case = switch (base) {8343 break :style .{ .base = 16, .case = switch (base) {
8344 'x' => .lower,8344 'x' => .lower,
8345 'X' => .upper,8345 'X' => .upper,
...@@ -8354,9 +8354,9 @@ fn formatIntLiteral(...@@ -8354,9 +8354,9 @@ fn formatIntLiteral(
8354 const string = int.abs().toStringAlloc(allocator, style.base, style.case) catch8354 const string = int.abs().toStringAlloc(allocator, style.base, style.case) catch
8355 return error.WriteFailed;8355 return error.WriteFailed;
8356 defer allocator.free(string);8356 defer allocator.free(string);
8357 try bw.writeAll(string);8357 try w.writeAll(string);
8358 } else {8358 } else {
8359 try data.ctype.renderLiteralPrefix(bw, data.kind, ctype_pool);8359 try data.ctype.renderLiteralPrefix(w, data.kind, ctype_pool);
8360 wrap.truncate(int, .unsigned, c_bits);8360 wrap.truncate(int, .unsigned, c_bits);
8361 @memset(wrap.limbs[wrap.len..], 0);8361 @memset(wrap.limbs[wrap.len..], 0);
8362 wrap.len = wrap.limbs.len;8362 wrap.len = wrap.limbs.len;
...@@ -8399,7 +8399,7 @@ fn formatIntLiteral(...@@ -8399,7 +8399,7 @@ fn formatIntLiteral(
8399 c_limb_ctype = c_limb_info.ctype;8399 c_limb_ctype = c_limb_info.ctype;
8400 }8400 }
84018401
8402 if (limb_offset > 0) try bw.writeAll(", ");8402 if (limb_offset > 0) try w.writeAll(", ");
8403 try formatIntLiteral(.{8403 try formatIntLiteral(.{
8404 .dg = data.dg,8404 .dg = data.dg,
8405 .int_info = c_limb_int_info,8405 .int_info = c_limb_int_info,
...@@ -8407,10 +8407,10 @@ fn formatIntLiteral(...@@ -8407,10 +8407,10 @@ fn formatIntLiteral(
8407 .ctype = c_limb_ctype,8407 .ctype = c_limb_ctype,
8408 .val = pt.intValue_big(.comptime_int, c_limb_mut.toConst()) catch8408 .val = pt.intValue_big(.comptime_int, c_limb_mut.toConst()) catch
8409 return error.WriteFailed,8409 return error.WriteFailed,
8410 }, bw, fmt);8410 }, w, fmt);
8411 }8411 }
8412 }8412 }
8413 try data.ctype.renderLiteralSuffix(bw, ctype_pool);8413 try data.ctype.renderLiteralSuffix(w, ctype_pool);
8414}8414}
84158415
8416const Materialize = struct {8416const Materialize = struct {
...@@ -8424,8 +8424,8 @@ const Materialize = struct {...@@ -8424,8 +8424,8 @@ const Materialize = struct {
8424 } };8424 } };
8425 }8425 }
84268426
8427 pub fn mat(self: Materialize, f: *Function, bw: *Writer) !void {8427 pub fn mat(self: Materialize, f: *Function, w: *Writer) !void {
8428 try f.writeCValue(bw, self.local, .Other);8428 try f.writeCValue(w, self.local, .Other);
8429 }8429 }
84308430
8431 pub fn end(self: Materialize, f: *Function, inst: Air.Inst.Index) !void {8431 pub fn end(self: Materialize, f: *Function, inst: Air.Inst.Index) !void {
...@@ -8436,36 +8436,36 @@ const Materialize = struct {...@@ -8436,36 +8436,36 @@ const Materialize = struct {
8436const Assignment = struct {8436const Assignment = struct {
8437 ctype: CType,8437 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 {
8440 const self: Assignment = .{ .ctype = ctype };8440 const self: Assignment = .{ .ctype = ctype };
8441 try self.restart(f, bw);8441 try self.restart(f, w);
8442 return self;8442 return self;
8443 }8443 }
84448444
8445 pub fn restart(self: Assignment, f: *Function, bw: *Writer) !void {8445 pub fn restart(self: Assignment, f: *Function, w: *Writer) !void {
8446 switch (self.strategy(f)) {8446 switch (self.strategy(f)) {
8447 .assign => {},8447 .assign => {},
8448 .memcpy => try bw.writeAll("memcpy("),8448 .memcpy => try w.writeAll("memcpy("),
8449 }8449 }
8450 }8450 }
84518451
8452 pub fn assign(self: Assignment, f: *Function, bw: *Writer) !void {8452 pub fn assign(self: Assignment, f: *Function, w: *Writer) !void {
8453 switch (self.strategy(f)) {8453 switch (self.strategy(f)) {
8454 .assign => try bw.writeAll(" = "),8454 .assign => try w.writeAll(" = "),
8455 .memcpy => try bw.writeAll(", "),8455 .memcpy => try w.writeAll(", "),
8456 }8456 }
8457 }8457 }
84588458
8459 pub fn end(self: Assignment, f: *Function, bw: *Writer) !void {8459 pub fn end(self: Assignment, f: *Function, w: *Writer) !void {
8460 switch (self.strategy(f)) {8460 switch (self.strategy(f)) {
8461 .assign => {},8461 .assign => {},
8462 .memcpy => {8462 .memcpy => {
8463 try bw.writeAll(", sizeof(");8463 try w.writeAll(", sizeof(");
8464 try f.renderCType(bw, self.ctype);8464 try f.renderCType(w, self.ctype);
8465 try bw.writeAll("))");8465 try w.writeAll("))");
8466 },8466 },
8467 }8467 }
8468 try bw.writeByte(';');8468 try w.writeByte(';');
8469 try f.object.newline();8469 try f.object.newline();
8470 }8470 }
84718471
...@@ -8500,18 +8500,18 @@ const Vectorize = struct {...@@ -8500,18 +8500,18 @@ const Vectorize = struct {
8500 } else .{};8500 } else .{};
8501 }8501 }
85028502
8503 pub fn elem(self: Vectorize, f: *Function, bw: *Writer) !void {8503 pub fn elem(self: Vectorize, f: *Function, w: *Writer) !void {
8504 if (self.index != .none) {8504 if (self.index != .none) {
8505 try bw.writeByte('[');8505 try w.writeByte('[');
8506 try f.writeCValue(bw, self.index, .Other);8506 try f.writeCValue(w, self.index, .Other);
8507 try bw.writeByte(']');8507 try w.writeByte(']');
8508 }8508 }
8509 }8509 }
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 {
8512 if (self.index != .none) {8512 if (self.index != .none) {
8513 try f.object.outdent();8513 try f.object.outdent();
8514 try bw.writeByte('}');8514 try w.writeByte('}');
8515 try f.object.newline();8515 try f.object.newline();
8516 try freeLocal(f, inst, self.index.new_local, null);8516 try freeLocal(f, inst, self.index.new_local, null);
8517 }8517 }