| ... | ... | @@ -56,6 +56,7 @@ pub const Mir = struct { |
| 56 | 56 | /// less than the natural alignment. |
| 57 | 57 | uavs: std.AutoArrayHashMapUnmanaged(InternPool.Index, Alignment), |
| 58 | 58 | // These remaining fields are essentially just an owned version of `link.C.AvBlock`. |
| 59 | code_header: []u8, |
| 59 | 60 | code: []u8, |
| 60 | 61 | fwd_decl: []u8, |
| 61 | 62 | ctype_pool: CType.Pool, |
| ... | ... | @@ -63,6 +64,7 @@ pub const Mir = struct { |
| 63 | 64 | |
| 64 | 65 | pub fn deinit(mir: *Mir, gpa: Allocator) void { |
| 65 | 66 | mir.uavs.deinit(gpa); |
| 67 | gpa.free(mir.code_header); |
| 66 | 68 | gpa.free(mir.code); |
| 67 | 69 | gpa.free(mir.fwd_decl); |
| 68 | 70 | mir.ctype_pool.deinit(gpa); |
| ... | ... | @@ -70,6 +72,8 @@ pub const Mir = struct { |
| 70 | 72 | } |
| 71 | 73 | }; |
| 72 | 74 | |
| 75 | pub const Error = Writer.Error || std.mem.Allocator.Error || error{AnalysisFail}; |
| 76 | |
| 73 | 77 | pub const CType = @import("c/Type.zig"); |
| 74 | 78 | |
| 75 | 79 | pub const CValue = union(enum) { |
| ... | ... | @@ -449,18 +453,18 @@ pub const Function = struct { |
| 449 | 453 | const ty = f.typeOf(ref); |
| 450 | 454 | |
| 451 | 455 | const result: CValue = if (lowersToArray(ty, pt)) result: { |
| 452 | | const w = f.object.codeHeaderWriter(); |
| 456 | const ch = &f.object.code_header.writer; |
| 453 | 457 | const decl_c_value = try f.allocLocalValue(.{ |
| 454 | 458 | .ctype = try f.ctypeFromType(ty, .complete), |
| 455 | 459 | .alignas = CType.AlignAs.fromAbiAlignment(ty.abiAlignment(pt.zcu)), |
| 456 | 460 | }); |
| 457 | 461 | const gpa = f.object.dg.gpa; |
| 458 | 462 | try f.allocs.put(gpa, decl_c_value.new_local, false); |
| 459 | | try w.writeAll("static "); |
| 460 | | try f.object.dg.renderTypeAndName(w, ty, decl_c_value, Const, .none, .complete); |
| 461 | | try w.writeAll(" = "); |
| 462 | | try f.object.dg.renderValue(w, val, .StaticInitializer); |
| 463 | | try w.writeAll(";\n "); |
| 463 | try ch.writeAll("static "); |
| 464 | try f.object.dg.renderTypeAndName(ch, ty, decl_c_value, Const, .none, .complete); |
| 465 | try ch.writeAll(" = "); |
| 466 | try f.object.dg.renderValue(ch, val, .StaticInitializer); |
| 467 | try ch.writeAll(";\n "); |
| 464 | 468 | break :result .{ .local = decl_c_value.new_local }; |
| 465 | 469 | } else .{ .constant = val }; |
| 466 | 470 | |
| ... | ... | @@ -550,7 +554,7 @@ pub const Function = struct { |
| 550 | 554 | w: *Writer, |
| 551 | 555 | c_value: CValue, |
| 552 | 556 | member: CValue, |
| 553 | | ) error{ OutOfMemory, AnalysisFail }!void { |
| 557 | ) Error!void { |
| 554 | 558 | switch (c_value) { |
| 555 | 559 | .new_local, .local, .local_ref, .constant, .arg, .arg_array => { |
| 556 | 560 | try f.writeCValue(w, c_value, .Other); |
| ... | ... | @@ -581,7 +585,7 @@ pub const Function = struct { |
| 581 | 585 | try f.writeCValue(w, member, .Other); |
| 582 | 586 | } |
| 583 | 587 | |
| 584 | | fn fail(f: *Function, comptime format: []const u8, args: anytype) error{ AnalysisFail, OutOfMemory } { |
| 588 | fn fail(f: *Function, comptime format: []const u8, args: anytype) Error { |
| 585 | 589 | return f.object.dg.fail(format, args); |
| 586 | 590 | } |
| 587 | 591 | |
| ... | ... | @@ -672,7 +676,7 @@ pub const Function = struct { |
| 672 | 676 | }, |
| 673 | 677 | else => {}, |
| 674 | 678 | } |
| 675 | | const w = f.object.writer(); |
| 679 | const w = &f.object.code.writer; |
| 676 | 680 | const a = try Assignment.start(f, w, ctype); |
| 677 | 681 | try f.writeCValue(w, dst, .Other); |
| 678 | 682 | try a.assign(f, w); |
| ... | ... | @@ -706,18 +710,32 @@ pub const Function = struct { |
| 706 | 710 | /// It is not available when generating .h file. |
| 707 | 711 | pub const Object = struct { |
| 708 | 712 | dg: DeclGen, |
| 709 | | /// This is a borrowed reference from `link.C`. |
| 710 | | code: std.ArrayList(u8), |
| 711 | | /// Goes before code. Initialized and deinitialized in `genFunc`. |
| 712 | | code_header: std.ArrayList(u8) = undefined, |
| 713 | | indent_writer: IndentWriter(std.ArrayList(u8).Writer), |
| 714 | | |
| 715 | | fn w(o: *Object) IndentWriter(std.ArrayList(u8).Writer).Writer { |
| 716 | | return o.indent_writer.writer(); |
| 717 | | } |
| 718 | | |
| 719 | | fn codeHeaderWriter(o: *Object) ArrayListWriter { |
| 720 | | return arrayListWriter(&o.code_header); |
| 713 | code_header: std.io.Writer.Allocating, |
| 714 | code: std.io.Writer.Allocating, |
| 715 | indent_counter: usize, |
| 716 | |
| 717 | const indent_width = 1; |
| 718 | const indent_char = ' '; |
| 719 | |
| 720 | fn newline(o: *Object) !void { |
| 721 | const w = &o.code.writer; |
| 722 | try w.writeByte('\n'); |
| 723 | try w.splatByteAll(indent_char, o.indent_counter); |
| 724 | } |
| 725 | fn indent(o: *Object) void { |
| 726 | o.indent_counter += indent_width; |
| 727 | } |
| 728 | fn outdent(o: *Object) !void { |
| 729 | o.indent_counter -= indent_width; |
| 730 | const written = o.code.getWritten(); |
| 731 | switch (written[written.len - 1]) { |
| 732 | indent_char => o.code.shrinkRetainingCapacity(written.len - indent_width), |
| 733 | '\n' => try o.code.writer.splatByteAll(indent_char, o.indent_counter), |
| 734 | else => { |
| 735 | std.debug.print("\"{f}\"\n", .{std.zig.fmtEscapes(written[written.len -| 100..])}); |
| 736 | unreachable; |
| 737 | }, |
| 738 | } |
| 721 | 739 | } |
| 722 | 740 | }; |
| 723 | 741 | |
| ... | ... | @@ -729,8 +747,7 @@ pub const DeclGen = struct { |
| 729 | 747 | pass: Pass, |
| 730 | 748 | is_naked_fn: bool, |
| 731 | 749 | expected_block: ?u32, |
| 732 | | /// This is a borrowed reference from `link.C`. |
| 733 | | fwd_decl: std.ArrayList(u8), |
| 750 | fwd_decl: std.io.Writer.Allocating, |
| 734 | 751 | error_msg: ?*Zcu.ErrorMsg, |
| 735 | 752 | ctype_pool: CType.Pool, |
| 736 | 753 | scratch: std.ArrayListUnmanaged(u32), |
| ... | ... | @@ -747,11 +764,7 @@ pub const DeclGen = struct { |
| 747 | 764 | flush, |
| 748 | 765 | }; |
| 749 | 766 | |
| 750 | | fn fwdDeclWriter(dg: *DeclGen) ArrayListWriter { |
| 751 | | return arrayListWriter(&dg.fwd_decl); |
| 752 | | } |
| 753 | | |
| 754 | | fn fail(dg: *DeclGen, comptime format: []const u8, args: anytype) error{ AnalysisFail, OutOfMemory } { |
| 767 | fn fail(dg: *DeclGen, comptime format: []const u8, args: anytype) Error { |
| 755 | 768 | @branchHint(.cold); |
| 756 | 769 | const zcu = dg.pt.zcu; |
| 757 | 770 | const src_loc = zcu.navSrcLoc(dg.pass.nav); |
| ... | ... | @@ -764,7 +777,7 @@ pub const DeclGen = struct { |
| 764 | 777 | w: *Writer, |
| 765 | 778 | uav: InternPool.Key.Ptr.BaseAddr.Uav, |
| 766 | 779 | location: ValueRenderLocation, |
| 767 | | ) error{ OutOfMemory, AnalysisFail }!void { |
| 780 | ) Error!void { |
| 768 | 781 | const pt = dg.pt; |
| 769 | 782 | const zcu = pt.zcu; |
| 770 | 783 | const ip = &zcu.intern_pool; |
| ... | ... | @@ -826,7 +839,7 @@ pub const DeclGen = struct { |
| 826 | 839 | w: *Writer, |
| 827 | 840 | nav_index: InternPool.Nav.Index, |
| 828 | 841 | location: ValueRenderLocation, |
| 829 | | ) error{ OutOfMemory, AnalysisFail }!void { |
| 842 | ) Error!void { |
| 830 | 843 | _ = location; |
| 831 | 844 | const pt = dg.pt; |
| 832 | 845 | const zcu = pt.zcu; |
| ... | ... | @@ -875,7 +888,7 @@ pub const DeclGen = struct { |
| 875 | 888 | w: *Writer, |
| 876 | 889 | derivation: Value.PointerDeriveStep, |
| 877 | 890 | location: ValueRenderLocation, |
| 878 | | ) error{ OutOfMemory, AnalysisFail }!void { |
| 891 | ) Error!void { |
| 879 | 892 | const pt = dg.pt; |
| 880 | 893 | const zcu = pt.zcu; |
| 881 | 894 | switch (derivation) { |
| ... | ... | @@ -977,8 +990,7 @@ pub const DeclGen = struct { |
| 977 | 990 | } |
| 978 | 991 | |
| 979 | 992 | fn renderErrorName(dg: *DeclGen, w: *Writer, err_name: InternPool.NullTerminatedString) !void { |
| 980 | | const ip = &dg.pt.zcu.intern_pool; |
| 981 | | try w.print("zig_error_{}", .{fmtIdentUnsolo(err_name.toSlice(ip))}); |
| 993 | try w.print("zig_error_{f}", .{fmtIdentUnsolo(err_name.toSlice(&dg.pt.zcu.intern_pool))}); |
| 982 | 994 | } |
| 983 | 995 | |
| 984 | 996 | fn renderValue( |
| ... | ... | @@ -986,7 +998,7 @@ pub const DeclGen = struct { |
| 986 | 998 | w: *Writer, |
| 987 | 999 | val: Value, |
| 988 | 1000 | location: ValueRenderLocation, |
| 989 | | ) error{ OutOfMemory, AnalysisFail }!void { |
| 1001 | ) Error!void { |
| 990 | 1002 | const pt = dg.pt; |
| 991 | 1003 | const zcu = pt.zcu; |
| 992 | 1004 | const ip = &zcu.intern_pool; |
| ... | ... | @@ -1056,7 +1068,7 @@ pub const DeclGen = struct { |
| 1056 | 1068 | .error_union => |error_union| switch (ctype.info(ctype_pool)) { |
| 1057 | 1069 | .basic => switch (error_union.val) { |
| 1058 | 1070 | .err_name => |err_name| try dg.renderErrorName(w, err_name), |
| 1059 | | .payload => try w.writeAll("0"), |
| 1071 | .payload => try w.writeByte('0'), |
| 1060 | 1072 | }, |
| 1061 | 1073 | .pointer, .aligned, .array, .vector, .fwd_decl, .function => unreachable, |
| 1062 | 1074 | .aggregate => |aggregate| { |
| ... | ... | @@ -1212,7 +1224,7 @@ pub const DeclGen = struct { |
| 1212 | 1224 | .none => "true", |
| 1213 | 1225 | else => "false", |
| 1214 | 1226 | }) else switch (opt.val) { |
| 1215 | | .none => try w.writeAll("0"), |
| 1227 | .none => try w.writeByte('0'), |
| 1216 | 1228 | else => |payload| switch (ip.indexToKey(payload)) { |
| 1217 | 1229 | .undef => |err_ty| try dg.renderUndefValue( |
| 1218 | 1230 | w, |
| ... | ... | @@ -1543,7 +1555,7 @@ pub const DeclGen = struct { |
| 1543 | 1555 | try w.writeByte(')'); |
| 1544 | 1556 | } |
| 1545 | 1557 | try dg.renderValue(w, Value.fromInterned(un.val), location); |
| 1546 | | } else try w.writeAll("0"); |
| 1558 | } else try w.writeByte('0'); |
| 1547 | 1559 | return; |
| 1548 | 1560 | } |
| 1549 | 1561 | |
| ... | ... | @@ -1595,7 +1607,7 @@ pub const DeclGen = struct { |
| 1595 | 1607 | w: *Writer, |
| 1596 | 1608 | ty: Type, |
| 1597 | 1609 | location: ValueRenderLocation, |
| 1598 | | ) error{ OutOfMemory, AnalysisFail }!void { |
| 1610 | ) Error!void { |
| 1599 | 1611 | const pt = dg.pt; |
| 1600 | 1612 | const zcu = pt.zcu; |
| 1601 | 1613 | const ip = &zcu.intern_pool; |
| ... | ... | @@ -1938,11 +1950,11 @@ pub const DeclGen = struct { |
| 1938 | 1950 | var trailing = try renderTypePrefix(dg.pass, &dg.ctype_pool, zcu, w, fn_ctype, .suffix, .{}); |
| 1939 | 1951 | |
| 1940 | 1952 | if (toCallingConvention(fn_info.cc, zcu)) |call_conv| { |
| 1941 | | try w.print("{}zig_callconv({s})", .{ trailing, call_conv }); |
| 1953 | try w.print("{f}zig_callconv({s})", .{ trailing, call_conv }); |
| 1942 | 1954 | trailing = .maybe_space; |
| 1943 | 1955 | } |
| 1944 | 1956 | |
| 1945 | | try w.print("{}", .{trailing}); |
| 1957 | try w.print("{f}", .{trailing}); |
| 1946 | 1958 | switch (name) { |
| 1947 | 1959 | .nav => |nav| try dg.renderNavName(w, nav), |
| 1948 | 1960 | .fmt_ctype_pool_string => |fmt| try w.print("{f}", .{fmt}), |
| ... | ... | @@ -2016,11 +2028,11 @@ pub const DeclGen = struct { |
| 2016 | 2028 | /// | `renderTypeAndName` | "uint8_t *name" | "uint8_t *name[10]" | |
| 2017 | 2029 | /// | `renderType` | "uint8_t *" | "uint8_t *[10]" | |
| 2018 | 2030 | /// |
| 2019 | | fn renderType(dg: *DeclGen, w: *Writer, t: Type) error{OutOfMemory}!void { |
| 2031 | fn renderType(dg: *DeclGen, w: *Writer, t: Type) Error!void { |
| 2020 | 2032 | try dg.renderCType(w, try dg.ctypeFromType(t, .complete)); |
| 2021 | 2033 | } |
| 2022 | 2034 | |
| 2023 | | fn renderCType(dg: *DeclGen, w: *Writer, ctype: CType) error{OutOfMemory}!void { |
| 2035 | fn renderCType(dg: *DeclGen, w: *Writer, ctype: CType) Error!void { |
| 2024 | 2036 | _ = try renderTypePrefix(dg.pass, &dg.ctype_pool, dg.pt.zcu, w, ctype, .suffix, .{}); |
| 2025 | 2037 | try renderTypeSuffix(dg.pass, &dg.ctype_pool, dg.pt.zcu, w, ctype, .suffix, .{}); |
| 2026 | 2038 | } |
| ... | ... | @@ -2171,7 +2183,7 @@ pub const DeclGen = struct { |
| 2171 | 2183 | qualifiers: CQualifiers, |
| 2172 | 2184 | alignment: Alignment, |
| 2173 | 2185 | kind: CType.Kind, |
| 2174 | | ) error{ OutOfMemory, AnalysisFail }!void { |
| 2186 | ) !void { |
| 2175 | 2187 | try dg.renderCTypeAndName( |
| 2176 | 2188 | w, |
| 2177 | 2189 | try dg.ctypeFromType(ty, kind), |
| ... | ... | @@ -2191,7 +2203,7 @@ pub const DeclGen = struct { |
| 2191 | 2203 | name: CValue, |
| 2192 | 2204 | qualifiers: CQualifiers, |
| 2193 | 2205 | alignas: CType.AlignAs, |
| 2194 | | ) error{ OutOfMemory, AnalysisFail }!void { |
| 2206 | ) !void { |
| 2195 | 2207 | const zcu = dg.pt.zcu; |
| 2196 | 2208 | switch (alignas.abiOrder()) { |
| 2197 | 2209 | .lt => try w.print("zig_under_align({}) ", .{alignas.toByteUnits()}), |
| ... | ... | @@ -2199,7 +2211,7 @@ pub const DeclGen = struct { |
| 2199 | 2211 | .gt => try w.print("zig_align({}) ", .{alignas.toByteUnits()}), |
| 2200 | 2212 | } |
| 2201 | 2213 | |
| 2202 | | try w.print("{}", .{ |
| 2214 | try w.print("{f}", .{ |
| 2203 | 2215 | try renderTypePrefix(dg.pass, &dg.ctype_pool, zcu, w, ctype, .suffix, qualifiers), |
| 2204 | 2216 | }); |
| 2205 | 2217 | try dg.writeName(w, name); |
| ... | ... | @@ -2216,7 +2228,7 @@ pub const DeclGen = struct { |
| 2216 | 2228 | } |
| 2217 | 2229 | } |
| 2218 | 2230 | |
| 2219 | | fn writeCValue(dg: *DeclGen, w: *Writer, c_value: CValue) !void { |
| 2231 | fn writeCValue(dg: *DeclGen, w: *Writer, c_value: CValue) Error!void { |
| 2220 | 2232 | switch (c_value) { |
| 2221 | 2233 | .none, .new_local, .local, .local_ref => unreachable, |
| 2222 | 2234 | .constant => |uav| try renderUavName(w, uav), |
| ... | ... | @@ -2271,13 +2283,18 @@ pub const DeclGen = struct { |
| 2271 | 2283 | w: *Writer, |
| 2272 | 2284 | c_value: CValue, |
| 2273 | 2285 | member: CValue, |
| 2274 | | ) error{ OutOfMemory, AnalysisFail }!void { |
| 2286 | ) Error!void { |
| 2275 | 2287 | try dg.writeCValue(w, c_value); |
| 2276 | 2288 | try w.writeByte('.'); |
| 2277 | 2289 | try dg.writeCValue(w, member); |
| 2278 | 2290 | } |
| 2279 | 2291 | |
| 2280 | | fn writeCValueDerefMember(dg: *DeclGen, w: *Writer, c_value: CValue, member: CValue) !void { |
| 2292 | fn writeCValueDerefMember( |
| 2293 | dg: *DeclGen, |
| 2294 | w: *Writer, |
| 2295 | c_value: CValue, |
| 2296 | member: CValue, |
| 2297 | ) !void { |
| 2281 | 2298 | switch (c_value) { |
| 2282 | 2299 | .none, |
| 2283 | 2300 | .new_local, |
| ... | ... | @@ -2315,7 +2332,7 @@ pub const DeclGen = struct { |
| 2315 | 2332 | const zcu = dg.pt.zcu; |
| 2316 | 2333 | const ip = &zcu.intern_pool; |
| 2317 | 2334 | const nav = ip.getNav(nav_index); |
| 2318 | | const fwd = dg.fwdDeclWriter(); |
| 2335 | const fwd = &dg.fwd_decl.writer; |
| 2319 | 2336 | try fwd.writeAll(switch (flags.linkage) { |
| 2320 | 2337 | .internal => "static ", |
| 2321 | 2338 | .strong, .weak, .link_once => "zig_extern ", |
| ... | ... | @@ -2353,7 +2370,7 @@ pub const DeclGen = struct { |
| 2353 | 2370 | // MSVC has a limit of 4095 character token length limit, and fmtIdent can (worst case), |
| 2354 | 2371 | // expand to 3x the length of its input, but let's cut it off at a much shorter limit. |
| 2355 | 2372 | const fqn_slice = ip.getNav(nav_index).fqn.toSlice(ip); |
| 2356 | | try w.print("{}__{d}", .{ |
| 2373 | try w.print("{f}__{d}", .{ |
| 2357 | 2374 | fmtIdentUnsolo(fqn_slice[0..@min(fqn_slice.len, 100)]), |
| 2358 | 2375 | @intFromEnum(nav_index), |
| 2359 | 2376 | }); |
| ... | ... | @@ -2484,7 +2501,7 @@ fn renderFwdDeclTypeName( |
| 2484 | 2501 | try w.print("{s} {s}", .{ @tagName(fwd_decl.tag), attributes }); |
| 2485 | 2502 | switch (fwd_decl.name) { |
| 2486 | 2503 | .anon => try w.print("anon__lazy_{d}", .{@intFromEnum(ctype.index)}), |
| 2487 | | .index => |index| try w.print("{}__{d}", .{ |
| 2504 | .index => |index| try w.print("{f}__{d}", .{ |
| 2488 | 2505 | fmtIdentUnsolo(Type.fromInterned(index).containerTypeName(ip).toSlice(&zcu.intern_pool)), |
| 2489 | 2506 | @intFromEnum(index), |
| 2490 | 2507 | }), |
| ... | ... | @@ -2498,13 +2515,13 @@ fn renderTypePrefix( |
| 2498 | 2515 | ctype: CType, |
| 2499 | 2516 | parent_fix: CTypeFix, |
| 2500 | 2517 | qualifiers: CQualifiers, |
| 2501 | | ) @TypeOf(w).Error!RenderCTypeTrailing { |
| 2518 | ) Writer.Error!RenderCTypeTrailing { |
| 2502 | 2519 | var trailing = RenderCTypeTrailing.maybe_space; |
| 2503 | 2520 | switch (ctype.info(ctype_pool)) { |
| 2504 | 2521 | .basic => |basic_info| try w.writeAll(@tagName(basic_info)), |
| 2505 | 2522 | |
| 2506 | 2523 | .pointer => |pointer_info| { |
| 2507 | | try w.print("{}*", .{try renderTypePrefix( |
| 2524 | try w.print("{f}*", .{try renderTypePrefix( |
| 2508 | 2525 | pass, |
| 2509 | 2526 | ctype_pool, |
| 2510 | 2527 | zcu, |
| ... | ... | @@ -2541,7 +2558,7 @@ fn renderTypePrefix( |
| 2541 | 2558 | ); |
| 2542 | 2559 | switch (parent_fix) { |
| 2543 | 2560 | .prefix => { |
| 2544 | | try w.print("{}(", .{child_trailing}); |
| 2561 | try w.print("{f}(", .{child_trailing}); |
| 2545 | 2562 | return .no_space; |
| 2546 | 2563 | }, |
| 2547 | 2564 | .suffix => return child_trailing, |
| ... | ... | @@ -2593,7 +2610,7 @@ fn renderTypePrefix( |
| 2593 | 2610 | ); |
| 2594 | 2611 | switch (parent_fix) { |
| 2595 | 2612 | .prefix => { |
| 2596 | | try w.print("{}(", .{child_trailing}); |
| 2613 | try w.print("{f}(", .{child_trailing}); |
| 2597 | 2614 | return .no_space; |
| 2598 | 2615 | }, |
| 2599 | 2616 | .suffix => return child_trailing, |
| ... | ... | @@ -2602,7 +2619,7 @@ fn renderTypePrefix( |
| 2602 | 2619 | } |
| 2603 | 2620 | var qualifier_it = qualifiers.iterator(); |
| 2604 | 2621 | while (qualifier_it.next()) |qualifier| { |
| 2605 | | try w.print("{}{s}", .{ trailing, @tagName(qualifier) }); |
| 2622 | try w.print("{f}{s}", .{ trailing, @tagName(qualifier) }); |
| 2606 | 2623 | trailing = .maybe_space; |
| 2607 | 2624 | } |
| 2608 | 2625 | return trailing; |
| ... | ... | @@ -2615,7 +2632,7 @@ fn renderTypeSuffix( |
| 2615 | 2632 | ctype: CType, |
| 2616 | 2633 | parent_fix: CTypeFix, |
| 2617 | 2634 | qualifiers: CQualifiers, |
| 2618 | | ) @TypeOf(w).Error!void { |
| 2635 | ) Writer.Error!void { |
| 2619 | 2636 | switch (ctype.info(ctype_pool)) { |
| 2620 | 2637 | .basic, .aligned, .fwd_decl, .aggregate => {}, |
| 2621 | 2638 | .pointer => |pointer_info| try renderTypeSuffix( |
| ... | ... | @@ -2650,7 +2667,7 @@ fn renderTypeSuffix( |
| 2650 | 2667 | need_comma = true; |
| 2651 | 2668 | const trailing = |
| 2652 | 2669 | try renderTypePrefix(pass, ctype_pool, zcu, w, param_type, .suffix, qualifiers); |
| 2653 | | if (qualifiers.contains(.@"const")) try w.print("{}a{d}", .{ trailing, param_index }); |
| 2670 | if (qualifiers.contains(.@"const")) try w.print("{f}a{d}", .{ trailing, param_index }); |
| 2654 | 2671 | try renderTypeSuffix(pass, ctype_pool, zcu, w, param_type, .suffix, .{}); |
| 2655 | 2672 | } |
| 2656 | 2673 | if (function_info.varargs) { |
| ... | ... | @@ -2675,7 +2692,7 @@ fn renderFields( |
| 2675 | 2692 | try w.writeAll("{\n"); |
| 2676 | 2693 | for (0..aggregate_info.fields.len) |field_index| { |
| 2677 | 2694 | const field_info = aggregate_info.fields.at(field_index, ctype_pool); |
| 2678 | | try w.writeByteNTimes(' ', indent + 1); |
| 2695 | try w.splatByteAll(' ', indent + 1); |
| 2679 | 2696 | switch (field_info.alignas.abiOrder()) { |
| 2680 | 2697 | .lt => { |
| 2681 | 2698 | std.debug.assert(aggregate_info.@"packed"); |
| ... | ... | @@ -2699,11 +2716,11 @@ fn renderFields( |
| 2699 | 2716 | .suffix, |
| 2700 | 2717 | .{}, |
| 2701 | 2718 | ); |
| 2702 | | try w.print("{}{f}", .{ trailing, fmtCTypePoolString(field_info.name, ctype_pool, true) }); |
| 2719 | try w.print("{f}{f}", .{ trailing, fmtCTypePoolString(field_info.name, ctype_pool, true) }); |
| 2703 | 2720 | try renderTypeSuffix(.flush, ctype_pool, zcu, w, field_info.ctype, .suffix, .{}); |
| 2704 | 2721 | try w.writeAll(";\n"); |
| 2705 | 2722 | } |
| 2706 | | try w.writeByteNTimes(' ', indent); |
| 2723 | try w.splatByteAll(' ', indent); |
| 2707 | 2724 | try w.writeByte('}'); |
| 2708 | 2725 | } |
| 2709 | 2726 | |
| ... | ... | @@ -2723,7 +2740,7 @@ pub fn genTypeDecl( |
| 2723 | 2740 | if (!found_existing) { |
| 2724 | 2741 | std.debug.assert(aligned_info.alignas.abiOrder().compare(.lt)); |
| 2725 | 2742 | try w.print("typedef zig_under_align({d}) ", .{aligned_info.alignas.toByteUnits()}); |
| 2726 | | try w.print("{}", .{try renderTypePrefix( |
| 2743 | try w.print("{f}", .{try renderTypePrefix( |
| 2727 | 2744 | .flush, |
| 2728 | 2745 | global_ctype_pool, |
| 2729 | 2746 | zcu, |
| ... | ... | @@ -2764,7 +2781,7 @@ pub fn genTypeDecl( |
| 2764 | 2781 | _ = try renderTypePrefix(.flush, global_ctype_pool, zcu, w, global_ctype, .suffix, .{}); |
| 2765 | 2782 | try w.writeByte(';'); |
| 2766 | 2783 | const file_scope = ty.typeDeclInstAllowGeneratedTag(zcu).?.resolveFile(ip); |
| 2767 | | if (!zcu.fileByIndex(file_scope).mod.?.strip) try w.print(" /* {} */", .{ |
| 2784 | if (!zcu.fileByIndex(file_scope).mod.?.strip) try w.print(" /* {f} */", .{ |
| 2768 | 2785 | ty.containerTypeName(ip).fmt(ip), |
| 2769 | 2786 | }); |
| 2770 | 2787 | try w.writeByte('\n'); |
| ... | ... | @@ -2795,18 +2812,19 @@ pub fn genGlobalAsm(zcu: *Zcu, w: *Writer) !void { |
| 2795 | 2812 | } |
| 2796 | 2813 | } |
| 2797 | 2814 | |
| 2798 | | pub fn genErrDecls(o: *Object) !void { |
| 2815 | pub fn genErrDecls(o: *Object) Error!void { |
| 2799 | 2816 | const pt = o.dg.pt; |
| 2800 | 2817 | const zcu = pt.zcu; |
| 2801 | 2818 | const ip = &zcu.intern_pool; |
| 2802 | | const w = o.writer(); |
| 2819 | const w = &o.code.writer; |
| 2803 | 2820 | |
| 2804 | 2821 | var max_name_len: usize = 0; |
| 2805 | 2822 | // do not generate an invalid empty enum when the global error set is empty |
| 2806 | 2823 | const names = ip.global_error_set.getNamesFromMainThread(); |
| 2807 | 2824 | if (names.len > 0) { |
| 2808 | | try w.writeAll("enum {\n"); |
| 2809 | | o.indent_writer.pushIndent(); |
| 2825 | try w.writeAll("enum {"); |
| 2826 | o.indent(); |
| 2827 | try o.newline(); |
| 2810 | 2828 | for (names, 1..) |name_nts, value| { |
| 2811 | 2829 | const name = name_nts.toSlice(ip); |
| 2812 | 2830 | max_name_len = @max(name.len, max_name_len); |
| ... | ... | @@ -2815,10 +2833,12 @@ pub fn genErrDecls(o: *Object) !void { |
| 2815 | 2833 | .name = name_nts, |
| 2816 | 2834 | } }); |
| 2817 | 2835 | try o.dg.renderValue(w, Value.fromInterned(err_val), .Other); |
| 2818 | | try w.print(" = {d}u,\n", .{value}); |
| 2836 | try w.print(" = {d}u,", .{value}); |
| 2837 | try o.newline(); |
| 2819 | 2838 | } |
| 2820 | | o.indent_writer.popIndent(); |
| 2821 | | try w.writeAll("};\n"); |
| 2839 | try o.outdent(); |
| 2840 | try w.writeAll("};"); |
| 2841 | try o.newline(); |
| 2822 | 2842 | } |
| 2823 | 2843 | const array_identifier = "zig_errorName"; |
| 2824 | 2844 | const name_prefix = array_identifier ++ "_"; |
| ... | ... | @@ -2852,7 +2872,8 @@ pub fn genErrDecls(o: *Object) !void { |
| 2852 | 2872 | ); |
| 2853 | 2873 | try w.writeAll(" = "); |
| 2854 | 2874 | try o.dg.renderValue(w, Value.fromInterned(name_val), .StaticInitializer); |
| 2855 | | try w.writeAll(";\n"); |
| 2875 | try w.writeByte(';'); |
| 2876 | try o.newline(); |
| 2856 | 2877 | } |
| 2857 | 2878 | |
| 2858 | 2879 | const name_array_ty = try pt.arrayType(.{ |
| ... | ... | @@ -2878,15 +2899,16 @@ pub fn genErrDecls(o: *Object) !void { |
| 2878 | 2899 | try o.dg.fmtIntLiteralDec(try pt.intValue(.usize, name.len), .StaticInitializer), |
| 2879 | 2900 | }); |
| 2880 | 2901 | } |
| 2881 | | try w.writeAll("};\n"); |
| 2902 | try w.writeAll("};"); |
| 2903 | try o.newline(); |
| 2882 | 2904 | } |
| 2883 | 2905 | |
| 2884 | | pub fn genLazyFn(o: *Object, lazy_ctype_pool: *const CType.Pool, lazy_fn: LazyFnMap.Entry) !void { |
| 2906 | pub fn genLazyFn(o: *Object, lazy_ctype_pool: *const CType.Pool, lazy_fn: LazyFnMap.Entry) Error!void { |
| 2885 | 2907 | const pt = o.dg.pt; |
| 2886 | 2908 | const zcu = pt.zcu; |
| 2887 | 2909 | const ip = &zcu.intern_pool; |
| 2888 | 2910 | const ctype_pool = &o.dg.ctype_pool; |
| 2889 | | const w = o.writer(); |
| 2911 | const w = &o.code.writer; |
| 2890 | 2912 | const key = lazy_fn.key_ptr.*; |
| 2891 | 2913 | const val = lazy_fn.value_ptr; |
| 2892 | 2914 | switch (key) { |
| ... | ... | @@ -2896,9 +2918,14 @@ pub fn genLazyFn(o: *Object, lazy_ctype_pool: *const CType.Pool, lazy_fn: LazyFn |
| 2896 | 2918 | |
| 2897 | 2919 | try w.writeAll("static "); |
| 2898 | 2920 | try o.dg.renderType(w, name_slice_ty); |
| 2899 | | try w.print(" {}(", .{val.fn_name.fmt(lazy_ctype_pool)}); |
| 2921 | try w.print(" {f}(", .{val.fn_name.fmt(lazy_ctype_pool)}); |
| 2900 | 2922 | try o.dg.renderTypeAndName(w, enum_ty, .{ .identifier = "tag" }, Const, .none, .complete); |
| 2901 | | try w.writeAll(") {\n switch (tag) {\n"); |
| 2923 | try w.writeAll(") {"); |
| 2924 | o.indent(); |
| 2925 | try o.newline(); |
| 2926 | try w.writeAll("switch (tag) {"); |
| 2927 | o.indent(); |
| 2928 | try o.newline(); |
| 2902 | 2929 | const tag_names = enum_ty.enumFields(zcu); |
| 2903 | 2930 | for (0..tag_names.len) |tag_index| { |
| 2904 | 2931 | const tag_name = tag_names.get(ip)[tag_index]; |
| ... | ... | @@ -2915,26 +2942,35 @@ pub fn genLazyFn(o: *Object, lazy_ctype_pool: *const CType.Pool, lazy_fn: LazyFn |
| 2915 | 2942 | .storage = .{ .bytes = tag_name.toString() }, |
| 2916 | 2943 | } }); |
| 2917 | 2944 | |
| 2918 | | try w.print(" case {f}: {{\n static ", .{ |
| 2945 | try w.print("case {f}: {{", .{ |
| 2919 | 2946 | try o.dg.fmtIntLiteralDec(try tag_val.intFromEnum(enum_ty, pt), .Other), |
| 2920 | 2947 | }); |
| 2948 | o.indent(); |
| 2949 | try o.newline(); |
| 2950 | try w.writeAll("static "); |
| 2921 | 2951 | try o.dg.renderTypeAndName(w, name_ty, .{ .identifier = "name" }, Const, .none, .complete); |
| 2922 | 2952 | try w.writeAll(" = "); |
| 2923 | 2953 | try o.dg.renderValue(w, Value.fromInterned(name_val), .StaticInitializer); |
| 2924 | | try w.writeAll(";\n return ("); |
| 2954 | try w.writeByte(';'); |
| 2955 | try o.newline(); |
| 2956 | try w.writeAll("return ("); |
| 2925 | 2957 | try o.dg.renderType(w, name_slice_ty); |
| 2926 | | try w.print("){{{f}, {f}}};\n", .{ |
| 2958 | try w.print("){{{f}, {f}}};", .{ |
| 2927 | 2959 | fmtIdentUnsolo("name"), |
| 2928 | 2960 | try o.dg.fmtIntLiteralDec(try pt.intValue(.usize, tag_name_len), .Other), |
| 2929 | 2961 | }); |
| 2930 | | |
| 2931 | | try w.writeAll(" }\n"); |
| 2962 | try o.newline(); |
| 2963 | try o.outdent(); |
| 2964 | try w.writeByte('}'); |
| 2965 | try o.newline(); |
| 2932 | 2966 | } |
| 2933 | | try w.writeAll(" }\n while ("); |
| 2934 | | try o.dg.renderValue(w, Value.true, .Other); |
| 2935 | | try w.writeAll(") "); |
| 2936 | | _ = try airBreakpoint(w); |
| 2937 | | try w.writeAll("}\n"); |
| 2967 | try o.outdent(); |
| 2968 | try w.writeByte('}'); |
| 2969 | try o.newline(); |
| 2970 | try airUnreach(o); |
| 2971 | try o.outdent(); |
| 2972 | try w.writeByte('}'); |
| 2973 | try o.newline(); |
| 2938 | 2974 | }, |
| 2939 | 2975 | .never_tail, .never_inline => |fn_nav_index| { |
| 2940 | 2976 | const fn_val = zcu.navValue(fn_nav_index); |
| ... | ... | @@ -2942,7 +2978,7 @@ pub fn genLazyFn(o: *Object, lazy_ctype_pool: *const CType.Pool, lazy_fn: LazyFn |
| 2942 | 2978 | const fn_info = fn_ctype.info(ctype_pool).function; |
| 2943 | 2979 | const fn_name = fmtCTypePoolString(val.fn_name, lazy_ctype_pool, true); |
| 2944 | 2980 | |
| 2945 | | const fwd = o.dg.fwdDeclWriter(); |
| 2981 | const fwd = &o.dg.fwd_decl.writer; |
| 2946 | 2982 | try fwd.print("static zig_{s} ", .{@tagName(key)}); |
| 2947 | 2983 | try o.dg.renderFunctionSignature(fwd, fn_val, ip.getNav(fn_nav_index).getAlignment(), .forward, .{ |
| 2948 | 2984 | .fmt_ctype_pool_string = fn_name, |
| ... | ... | @@ -2953,14 +2989,21 @@ pub fn genLazyFn(o: *Object, lazy_ctype_pool: *const CType.Pool, lazy_fn: LazyFn |
| 2953 | 2989 | try o.dg.renderFunctionSignature(w, fn_val, .none, .complete, .{ |
| 2954 | 2990 | .fmt_ctype_pool_string = fn_name, |
| 2955 | 2991 | }); |
| 2956 | | try w.writeAll(" {\n return "); |
| 2992 | try w.writeAll(" {"); |
| 2993 | o.indent(); |
| 2994 | try o.newline(); |
| 2995 | try w.writeAll("return "); |
| 2957 | 2996 | try o.dg.renderNavName(w, fn_nav_index); |
| 2958 | 2997 | try w.writeByte('('); |
| 2959 | 2998 | for (0..fn_info.param_ctypes.len) |arg| { |
| 2960 | 2999 | if (arg > 0) try w.writeAll(", "); |
| 2961 | 3000 | try w.print("a{d}", .{arg}); |
| 2962 | 3001 | } |
| 2963 | | try w.writeAll(");\n}\n"); |
| 3002 | try w.writeAll(");"); |
| 3003 | try o.newline(); |
| 3004 | try o.outdent(); |
| 3005 | try w.writeByte('}'); |
| 3006 | try o.newline(); |
| 2964 | 3007 | }, |
| 2965 | 3008 | } |
| 2966 | 3009 | } |
| ... | ... | @@ -2995,18 +3038,20 @@ pub fn generate( |
| 2995 | 3038 | .pass = .{ .nav = func.owner_nav }, |
| 2996 | 3039 | .is_naked_fn = Type.fromInterned(func.ty).fnCallingConvention(zcu) == .naked, |
| 2997 | 3040 | .expected_block = null, |
| 2998 | | .fwd_decl = .init(gpa), |
| 3041 | .fwd_decl = undefined, |
| 2999 | 3042 | .ctype_pool = .empty, |
| 3000 | 3043 | .scratch = .empty, |
| 3001 | 3044 | .uavs = .empty, |
| 3002 | 3045 | }, |
| 3003 | | .code = .init(gpa), |
| 3004 | | .indent_writer = undefined, // set later so we can get a pointer to object.code |
| 3046 | .code_header = undefined, |
| 3047 | .code = undefined, |
| 3048 | .indent_counter = 0, |
| 3005 | 3049 | }, |
| 3006 | 3050 | .lazy_fns = .empty, |
| 3007 | 3051 | }; |
| 3008 | 3052 | defer { |
| 3009 | | function.object.code.deinit(); |
| 3053 | function.object.code_header.init(gpa); |
| 3054 | function.object.code.init(gpa); |
| 3010 | 3055 | function.object.dg.fwd_decl.deinit(); |
| 3011 | 3056 | function.object.dg.ctype_pool.deinit(gpa); |
| 3012 | 3057 | function.object.dg.scratch.deinit(gpa); |
| ... | ... | @@ -3014,7 +3059,9 @@ pub fn generate( |
| 3014 | 3059 | function.deinit(); |
| 3015 | 3060 | } |
| 3016 | 3061 | try function.object.dg.ctype_pool.init(gpa); |
| 3017 | | function.object.indent_writer = .{ .underlying_writer = function.object.code.writer() }; |
| 3062 | function.object.dg.fwd_decl.init(gpa); |
| 3063 | function.object.code_header.init(gpa); |
| 3064 | function.object.code.init(gpa); |
| 3018 | 3065 | |
| 3019 | 3066 | genFunc(&function) catch |err| switch (err) { |
| 3020 | 3067 | error.AnalysisFail => return zcu.codegenFailMsg(func.owner_nav, function.object.dg.error_msg.?), |
| ... | ... | @@ -3030,6 +3077,7 @@ pub fn generate( |
| 3030 | 3077 | }; |
| 3031 | 3078 | errdefer mir.deinit(gpa); |
| 3032 | 3079 | mir.uavs = function.object.dg.uavs.move(); |
| 3080 | mir.code_header = try function.object.code_header.toOwnedSlice(); |
| 3033 | 3081 | mir.code = try function.object.code.toOwnedSlice(); |
| 3034 | 3082 | mir.fwd_decl = try function.object.dg.fwd_decl.toOwnedSlice(); |
| 3035 | 3083 | mir.ctype_pool = function.object.dg.ctype_pool.move(); |
| ... | ... | @@ -3037,7 +3085,7 @@ pub fn generate( |
| 3037 | 3085 | return mir; |
| 3038 | 3086 | } |
| 3039 | 3087 | |
| 3040 | | fn genFunc(f: *Function) !void { |
| 3088 | pub fn genFunc(f: *Function) Error!void { |
| 3041 | 3089 | const tracy = trace(@src()); |
| 3042 | 3090 | defer tracy.end(); |
| 3043 | 3091 | |
| ... | ... | @@ -3049,10 +3097,7 @@ fn genFunc(f: *Function) !void { |
| 3049 | 3097 | const nav_val = zcu.navValue(nav_index); |
| 3050 | 3098 | const nav = ip.getNav(nav_index); |
| 3051 | 3099 | |
| 3052 | | o.code_header = std.ArrayList(u8).init(gpa); |
| 3053 | | defer o.code_header.deinit(); |
| 3054 | | |
| 3055 | | const fwd = o.dg.fwdDeclWriter(); |
| 3100 | const fwd = &o.dg.fwd_decl.writer; |
| 3056 | 3101 | try fwd.writeAll("static "); |
| 3057 | 3102 | try o.dg.renderFunctionSignature( |
| 3058 | 3103 | fwd, |
| ... | ... | @@ -3063,29 +3108,26 @@ fn genFunc(f: *Function) !void { |
| 3063 | 3108 | ); |
| 3064 | 3109 | try fwd.writeAll(";\n"); |
| 3065 | 3110 | |
| 3111 | const ch = &o.code_header.writer; |
| 3066 | 3112 | if (nav.status.fully_resolved.@"linksection".toSlice(ip)) |s| |
| 3067 | | try o.writer().print("zig_linksection_fn({f}) ", .{fmtStringLiteral(s, null)}); |
| 3113 | try ch.print("zig_linksection_fn({f}) ", .{fmtStringLiteral(s, null)}); |
| 3068 | 3114 | try o.dg.renderFunctionSignature( |
| 3069 | | o.writer(), |
| 3115 | ch, |
| 3070 | 3116 | nav_val, |
| 3071 | 3117 | .none, |
| 3072 | 3118 | .complete, |
| 3073 | 3119 | .{ .nav = nav_index }, |
| 3074 | 3120 | ); |
| 3075 | | try o.writer().writeByte(' '); |
| 3076 | | |
| 3077 | | // In case we need to use the header, populate it with a copy of the function |
| 3078 | | // signature here. We anticipate a brace, newline, and space. |
| 3079 | | try o.code_header.ensureUnusedCapacity(o.code.items.len + 3); |
| 3080 | | o.code_header.appendSliceAssumeCapacity(o.code.items); |
| 3081 | | o.code_header.appendSliceAssumeCapacity("{\n "); |
| 3082 | | const empty_header_len = o.code_header.items.len; |
| 3121 | try ch.writeAll(" {\n "); |
| 3083 | 3122 | |
| 3084 | 3123 | f.free_locals_map.clearRetainingCapacity(); |
| 3085 | 3124 | |
| 3086 | 3125 | const main_body = f.air.getMainBody(); |
| 3087 | | try genBodyResolveState(f, undefined, &.{}, main_body, false); |
| 3088 | | try o.indent_writer.insertNewline(); |
| 3126 | o.indent(); |
| 3127 | try genBodyResolveState(f, undefined, &.{}, main_body, true); |
| 3128 | try o.outdent(); |
| 3129 | try o.code.writer.writeByte('}'); |
| 3130 | try o.newline(); |
| 3089 | 3131 | if (o.dg.expected_block) |_| |
| 3090 | 3132 | return f.fail("runtime code not allowed in naked function", .{}); |
| 3091 | 3133 | |
| ... | ... | @@ -3116,24 +3158,16 @@ fn genFunc(f: *Function) !void { |
| 3116 | 3158 | }; |
| 3117 | 3159 | free_locals.sort(SortContext{ .keys = free_locals.keys() }); |
| 3118 | 3160 | |
| 3119 | | const w = o.codeHeaderWriter(); |
| 3120 | 3161 | for (free_locals.values()) |list| { |
| 3121 | 3162 | for (list.keys()) |local_index| { |
| 3122 | 3163 | const local = f.locals.items[local_index]; |
| 3123 | | try o.dg.renderCTypeAndName(w, local.ctype, .{ .local = local_index }, .{}, local.flags.alignas); |
| 3124 | | try w.writeAll(";\n "); |
| 3164 | try o.dg.renderCTypeAndName(ch, local.ctype, .{ .local = local_index }, .{}, local.flags.alignas); |
| 3165 | try ch.writeAll(";\n "); |
| 3125 | 3166 | } |
| 3126 | 3167 | } |
| 3127 | | |
| 3128 | | // If we have a header to insert, append the body to the header |
| 3129 | | // and then return the result, freeing the body. |
| 3130 | | if (o.code_header.items.len > empty_header_len) { |
| 3131 | | try o.code_header.appendSlice(o.code.items[empty_header_len..]); |
| 3132 | | mem.swap(std.ArrayList(u8), &o.code, &o.code_header); |
| 3133 | | } |
| 3134 | 3168 | } |
| 3135 | 3169 | |
| 3136 | | pub fn genDecl(o: *Object) !void { |
| 3170 | pub fn genDecl(o: *Object) Error!void { |
| 3137 | 3171 | const tracy = trace(@src()); |
| 3138 | 3172 | defer tracy.end(); |
| 3139 | 3173 | |
| ... | ... | @@ -3153,7 +3187,7 @@ pub fn genDecl(o: *Object) !void { |
| 3153 | 3187 | .visibility = @"extern".visibility, |
| 3154 | 3188 | }); |
| 3155 | 3189 | |
| 3156 | | const fwd = o.dg.fwdDeclWriter(); |
| 3190 | const fwd = &o.dg.fwd_decl.writer; |
| 3157 | 3191 | try fwd.writeAll("zig_extern "); |
| 3158 | 3192 | try o.dg.renderFunctionSignature( |
| 3159 | 3193 | fwd, |
| ... | ... | @@ -3174,7 +3208,7 @@ pub fn genDecl(o: *Object) !void { |
| 3174 | 3208 | .linkage = .internal, |
| 3175 | 3209 | .visibility = .default, |
| 3176 | 3210 | }); |
| 3177 | | const w = o.writer(); |
| 3211 | const w = &o.code.writer; |
| 3178 | 3212 | if (variable.is_threadlocal and !o.dg.mod.single_threaded) try w.writeAll("zig_threadlocal "); |
| 3179 | 3213 | if (nav.status.fully_resolved.@"linksection".toSlice(&zcu.intern_pool)) |s| |
| 3180 | 3214 | try w.print("zig_linksection({f}) ", .{fmtStringLiteral(s, null)}); |
| ... | ... | @@ -3189,7 +3223,7 @@ pub fn genDecl(o: *Object) !void { |
| 3189 | 3223 | try w.writeAll(" = "); |
| 3190 | 3224 | try o.dg.renderValue(w, Value.fromInterned(variable.init), .StaticInitializer); |
| 3191 | 3225 | try w.writeByte(';'); |
| 3192 | | try o.indent_writer.insertNewline(); |
| 3226 | try o.newline(); |
| 3193 | 3227 | }, |
| 3194 | 3228 | else => try genDeclValue( |
| 3195 | 3229 | o, |
| ... | ... | @@ -3207,28 +3241,29 @@ pub fn genDeclValue( |
| 3207 | 3241 | decl_c_value: CValue, |
| 3208 | 3242 | alignment: Alignment, |
| 3209 | 3243 | @"linksection": InternPool.OptionalNullTerminatedString, |
| 3210 | | ) !void { |
| 3244 | ) Error!void { |
| 3211 | 3245 | const zcu = o.dg.pt.zcu; |
| 3212 | 3246 | const ty = val.typeOf(zcu); |
| 3213 | 3247 | |
| 3214 | | const fwd = o.dg.fwdDeclWriter(); |
| 3248 | const fwd = &o.dg.fwd_decl.writer; |
| 3215 | 3249 | try fwd.writeAll("static "); |
| 3216 | 3250 | try o.dg.renderTypeAndName(fwd, ty, decl_c_value, Const, alignment, .complete); |
| 3217 | 3251 | try fwd.writeAll(";\n"); |
| 3218 | 3252 | |
| 3219 | | const w = o.writer(); |
| 3253 | const w = &o.code.writer; |
| 3220 | 3254 | if (@"linksection".toSlice(&zcu.intern_pool)) |s| |
| 3221 | 3255 | try w.print("zig_linksection({f}) ", .{fmtStringLiteral(s, null)}); |
| 3222 | 3256 | try o.dg.renderTypeAndName(w, ty, decl_c_value, Const, alignment, .complete); |
| 3223 | 3257 | try w.writeAll(" = "); |
| 3224 | 3258 | try o.dg.renderValue(w, val, .StaticInitializer); |
| 3225 | | try w.writeAll(";\n"); |
| 3259 | try w.writeByte(';'); |
| 3260 | try o.newline(); |
| 3226 | 3261 | } |
| 3227 | 3262 | |
| 3228 | 3263 | pub fn genExports(dg: *DeclGen, exported: Zcu.Exported, export_indices: []const Zcu.Export.Index) !void { |
| 3229 | 3264 | const zcu = dg.pt.zcu; |
| 3230 | 3265 | const ip = &zcu.intern_pool; |
| 3231 | | const fwd = dg.fwdDeclWriter(); |
| 3266 | const fwd = &dg.fwd_decl.writer; |
| 3232 | 3267 | |
| 3233 | 3268 | const main_name = export_indices[0].ptr(zcu).opts.name; |
| 3234 | 3269 | try fwd.writeAll("#define "); |
| ... | ... | @@ -3305,15 +3340,16 @@ pub fn genExports(dg: *DeclGen, exported: Zcu.Exported, export_indices: []const |
| 3305 | 3340 | /// `value_map` and `free_locals_map` are undefined after the generation, and new locals may not |
| 3306 | 3341 | /// have been added to `free_locals_map`. For a version of this function that restores this state, |
| 3307 | 3342 | /// see `genBodyResolveState`. |
| 3308 | | fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutOfMemory }!void { |
| 3309 | | const w = f.object.writer(); |
| 3343 | fn genBody(f: *Function, body: []const Air.Inst.Index) Error!void { |
| 3344 | const w = &f.object.code.writer; |
| 3310 | 3345 | if (body.len == 0) { |
| 3311 | 3346 | try w.writeAll("{}"); |
| 3312 | 3347 | } else { |
| 3313 | | try w.writeAll("{\n"); |
| 3314 | | f.object.indent_writer.pushIndent(); |
| 3348 | try w.writeByte('{'); |
| 3349 | f.object.indent(); |
| 3350 | try f.object.newline(); |
| 3315 | 3351 | try genBodyInner(f, body); |
| 3316 | | f.object.indent_writer.popIndent(); |
| 3352 | try f.object.outdent(); |
| 3317 | 3353 | try w.writeByte('}'); |
| 3318 | 3354 | } |
| 3319 | 3355 | } |
| ... | ... | @@ -3324,10 +3360,10 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO |
| 3324 | 3360 | /// `leading_deaths` have their deaths processed before the body is generated. |
| 3325 | 3361 | /// A scope is introduced (using braces) only if `inner` is `false`. |
| 3326 | 3362 | /// If `leading_deaths` is empty, `inst` may be `undefined`. |
| 3327 | | fn genBodyResolveState(f: *Function, inst: Air.Inst.Index, leading_deaths: []const Air.Inst.Index, body: []const Air.Inst.Index, inner: bool) error{ AnalysisFail, OutOfMemory }!void { |
| 3363 | fn genBodyResolveState(f: *Function, inst: Air.Inst.Index, leading_deaths: []const Air.Inst.Index, body: []const Air.Inst.Index, inner: bool) Error!void { |
| 3328 | 3364 | if (body.len == 0) { |
| 3329 | 3365 | // Don't go to the expense of cloning everything! |
| 3330 | | if (!inner) try f.object.writer().writeAll("{}"); |
| 3366 | if (!inner) try f.object.code.writer.writeAll("{}"); |
| 3331 | 3367 | return; |
| 3332 | 3368 | } |
| 3333 | 3369 | |
| ... | ... | @@ -3373,7 +3409,7 @@ fn genBodyResolveState(f: *Function, inst: Air.Inst.Index, leading_deaths: []con |
| 3373 | 3409 | } |
| 3374 | 3410 | } |
| 3375 | 3411 | |
| 3376 | | fn genBodyInner(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutOfMemory }!void { |
| 3412 | fn genBodyInner(f: *Function, body: []const Air.Inst.Index) Error!void { |
| 3377 | 3413 | const zcu = f.object.dg.pt.zcu; |
| 3378 | 3414 | const ip = &zcu.intern_pool; |
| 3379 | 3415 | const air_tags = f.air.instructions.items(.tag); |
| ... | ... | @@ -3391,7 +3427,7 @@ fn genBodyInner(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, |
| 3391 | 3427 | |
| 3392 | 3428 | .arg => try airArg(f, inst), |
| 3393 | 3429 | |
| 3394 | | .breakpoint => try airBreakpoint(f.object.writer()), |
| 3430 | .breakpoint => try airBreakpoint(f), |
| 3395 | 3431 | .ret_addr => try airRetAddr(f, inst), |
| 3396 | 3432 | .frame_addr => try airFrameAddress(f, inst), |
| 3397 | 3433 | |
| ... | ... | @@ -3644,8 +3680,8 @@ fn genBodyInner(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, |
| 3644 | 3680 | .ret => return airRet(f, inst, false), |
| 3645 | 3681 | .ret_safe => return airRet(f, inst, false), // TODO |
| 3646 | 3682 | .ret_load => return airRet(f, inst, true), |
| 3647 | | .trap => return airTrap(f, f.object.writer()), |
| 3648 | | .unreach => return airUnreach(f), |
| 3683 | .trap => return airTrap(f, &f.object.code.writer), |
| 3684 | .unreach => return airUnreach(&f.object), |
| 3649 | 3685 | |
| 3650 | 3686 | // Instructions which may be `noreturn`. |
| 3651 | 3687 | .block => res: { |
| ... | ... | @@ -3688,7 +3724,7 @@ fn airSliceField(f: *Function, inst: Air.Inst.Index, is_ptr: bool, field_name: [ |
| 3688 | 3724 | const operand = try f.resolveInst(ty_op.operand); |
| 3689 | 3725 | try reap(f, inst, &.{ty_op.operand}); |
| 3690 | 3726 | |
| 3691 | | const w = f.object.writer(); |
| 3727 | const w = &f.object.code.writer; |
| 3692 | 3728 | const local = try f.allocLocal(inst, inst_ty); |
| 3693 | 3729 | const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete)); |
| 3694 | 3730 | try f.writeCValue(w, local, .Other); |
| ... | ... | @@ -3714,7 +3750,7 @@ fn airPtrElemVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3714 | 3750 | const index = try f.resolveInst(bin_op.rhs); |
| 3715 | 3751 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 3716 | 3752 | |
| 3717 | | const w = f.object.writer(); |
| 3753 | const w = &f.object.code.writer; |
| 3718 | 3754 | const local = try f.allocLocal(inst, inst_ty); |
| 3719 | 3755 | const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete)); |
| 3720 | 3756 | try f.writeCValue(w, local, .Other); |
| ... | ... | @@ -3741,7 +3777,7 @@ fn airPtrElemPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3741 | 3777 | const index = try f.resolveInst(bin_op.rhs); |
| 3742 | 3778 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 3743 | 3779 | |
| 3744 | | const w = f.object.writer(); |
| 3780 | const w = &f.object.code.writer; |
| 3745 | 3781 | const local = try f.allocLocal(inst, inst_ty); |
| 3746 | 3782 | const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete)); |
| 3747 | 3783 | try f.writeCValue(w, local, .Other); |
| ... | ... | @@ -3776,7 +3812,7 @@ fn airSliceElemVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3776 | 3812 | const index = try f.resolveInst(bin_op.rhs); |
| 3777 | 3813 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 3778 | 3814 | |
| 3779 | | const w = f.object.writer(); |
| 3815 | const w = &f.object.code.writer; |
| 3780 | 3816 | const local = try f.allocLocal(inst, inst_ty); |
| 3781 | 3817 | const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete)); |
| 3782 | 3818 | try f.writeCValue(w, local, .Other); |
| ... | ... | @@ -3804,7 +3840,7 @@ fn airSliceElemPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3804 | 3840 | const index = try f.resolveInst(bin_op.rhs); |
| 3805 | 3841 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 3806 | 3842 | |
| 3807 | | const w = f.object.writer(); |
| 3843 | const w = &f.object.code.writer; |
| 3808 | 3844 | const local = try f.allocLocal(inst, inst_ty); |
| 3809 | 3845 | const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete)); |
| 3810 | 3846 | try f.writeCValue(w, local, .Other); |
| ... | ... | @@ -3833,7 +3869,7 @@ fn airArrayElemVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3833 | 3869 | const index = try f.resolveInst(bin_op.rhs); |
| 3834 | 3870 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 3835 | 3871 | |
| 3836 | | const w = f.object.writer(); |
| 3872 | const w = &f.object.code.writer; |
| 3837 | 3873 | const local = try f.allocLocal(inst, inst_ty); |
| 3838 | 3874 | const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete)); |
| 3839 | 3875 | try f.writeCValue(w, local, .Other); |
| ... | ... | @@ -3896,12 +3932,13 @@ fn airArg(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3896 | 3932 | .{ .arg_array = i }; |
| 3897 | 3933 | |
| 3898 | 3934 | if (f.liveness.isUnused(inst)) { |
| 3899 | | const w = f.object.writer(); |
| 3935 | const w = &f.object.code.writer; |
| 3900 | 3936 | try w.writeByte('('); |
| 3901 | 3937 | try f.renderType(w, .void); |
| 3902 | 3938 | try w.writeByte(')'); |
| 3903 | 3939 | try f.writeCValue(w, result, .Other); |
| 3904 | | try w.writeAll(";\n"); |
| 3940 | try w.writeByte(';'); |
| 3941 | try f.object.newline(); |
| 3905 | 3942 | return .none; |
| 3906 | 3943 | } |
| 3907 | 3944 | |
| ... | ... | @@ -3934,7 +3971,7 @@ fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3934 | 3971 | const is_array = lowersToArray(src_ty, pt); |
| 3935 | 3972 | const need_memcpy = !is_aligned or is_array; |
| 3936 | 3973 | |
| 3937 | | const w = f.object.writer(); |
| 3974 | const w = &f.object.code.writer; |
| 3938 | 3975 | const local = try f.allocLocal(inst, src_ty); |
| 3939 | 3976 | const v = try Vectorize.start(f, inst, w, ptr_ty); |
| 3940 | 3977 | |
| ... | ... | @@ -3979,7 +4016,7 @@ fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3979 | 4016 | try w.writeByte('('); |
| 3980 | 4017 | try f.writeCValueDeref(w, operand); |
| 3981 | 4018 | try v.elem(f, w); |
| 3982 | | try w.print(", {f})", .{try f.fmtIntLiteralDec(bit_offset_val)}); |
| 4019 | try w.print(", {f})", .{try f.fmtIntLiteral(bit_offset_val)}); |
| 3983 | 4020 | if (cant_cast) try w.writeByte(')'); |
| 3984 | 4021 | try f.object.dg.renderBuiltinInfo(w, field_ty, .bits); |
| 3985 | 4022 | try w.writeByte(')'); |
| ... | ... | @@ -3990,7 +4027,8 @@ fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3990 | 4027 | try f.writeCValueDeref(w, operand); |
| 3991 | 4028 | try v.elem(f, w); |
| 3992 | 4029 | } |
| 3993 | | try w.writeAll(";\n"); |
| 4030 | try w.writeByte(';'); |
| 4031 | try f.object.newline(); |
| 3994 | 4032 | try v.end(f, inst, w); |
| 3995 | 4033 | |
| 3996 | 4034 | return local; |
| ... | ... | @@ -4000,7 +4038,7 @@ fn airRet(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !void { |
| 4000 | 4038 | const pt = f.object.dg.pt; |
| 4001 | 4039 | const zcu = pt.zcu; |
| 4002 | 4040 | const un_op = f.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4003 | | const w = f.object.writer(); |
| 4041 | const w = &f.object.code.writer; |
| 4004 | 4042 | const op_inst = un_op.toIndex(); |
| 4005 | 4043 | const op_ty = f.typeOf(un_op); |
| 4006 | 4044 | const ret_ty = if (is_ptr) op_ty.childType(zcu) else op_ty; |
| ... | ... | @@ -4029,7 +4067,8 @@ fn airRet(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !void { |
| 4029 | 4067 | deref = false; |
| 4030 | 4068 | try w.writeAll(", sizeof("); |
| 4031 | 4069 | try f.renderType(w, ret_ty); |
| 4032 | | try w.writeAll("));\n"); |
| 4070 | try w.writeAll("));"); |
| 4071 | try f.object.newline(); |
| 4033 | 4072 | break :ret_val array_local; |
| 4034 | 4073 | } else operand; |
| 4035 | 4074 | |
| ... | ... | @@ -4038,7 +4077,7 @@ fn airRet(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !void { |
| 4038 | 4077 | try f.writeCValueDeref(w, ret_val) |
| 4039 | 4078 | else |
| 4040 | 4079 | try f.writeCValue(w, ret_val, .Other); |
| 4041 | | try w.writeAll(";\n"); |
| 4080 | try w.write(";\n"); |
| 4042 | 4081 | if (is_array) { |
| 4043 | 4082 | try freeLocal(f, inst, ret_val.new_local, null); |
| 4044 | 4083 | } |
| ... | ... | @@ -4064,7 +4103,7 @@ fn airIntCast(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4064 | 4103 | |
| 4065 | 4104 | if (f.object.dg.intCastIsNoop(inst_scalar_ty, scalar_ty)) return f.moveCValue(inst, inst_ty, operand); |
| 4066 | 4105 | |
| 4067 | | const w = f.object.writer(); |
| 4106 | const w = &f.object.code.writer; |
| 4068 | 4107 | const local = try f.allocLocal(inst, inst_ty); |
| 4069 | 4108 | const v = try Vectorize.start(f, inst, w, operand_ty); |
| 4070 | 4109 | const a = try Assignment.start(f, w, try f.ctypeFromType(scalar_ty, .complete)); |
| ... | ... | @@ -4100,7 +4139,7 @@ fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4100 | 4139 | const need_mask = dest_bits < 8 or !std.math.isPowerOfTwo(dest_bits); |
| 4101 | 4140 | if (!need_cast and !need_lo and !need_mask) return f.moveCValue(inst, inst_ty, operand); |
| 4102 | 4141 | |
| 4103 | | const w = f.object.writer(); |
| 4142 | const w = &f.object.code.writer; |
| 4104 | 4143 | const local = try f.allocLocal(inst, inst_ty); |
| 4105 | 4144 | const v = try Vectorize.start(f, inst, w, operand_ty); |
| 4106 | 4145 | const a = try Assignment.start(f, w, try f.ctypeFromType(inst_scalar_ty, .complete)); |
| ... | ... | @@ -4178,15 +4217,16 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 4178 | 4217 | |
| 4179 | 4218 | const val_is_undef = if (try f.air.value(bin_op.rhs, pt)) |v| v.isUndefDeep(zcu) else false; |
| 4180 | 4219 | |
| 4220 | const w = &f.object.code.writer; |
| 4181 | 4221 | if (val_is_undef) { |
| 4182 | 4222 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 4183 | 4223 | if (safety and ptr_info.packed_offset.host_size == 0) { |
| 4184 | | const w = f.object.writer(); |
| 4185 | 4224 | try w.writeAll("memset("); |
| 4186 | 4225 | try f.writeCValue(w, ptr_val, .FunctionArgument); |
| 4187 | 4226 | try w.writeAll(", 0xaa, sizeof("); |
| 4188 | 4227 | try f.renderType(w, .fromInterned(ptr_info.child)); |
| 4189 | | try w.writeAll("));\n"); |
| 4228 | try w.writeAll("));"); |
| 4229 | try f.object.newline(); |
| 4190 | 4230 | } |
| 4191 | 4231 | return .none; |
| 4192 | 4232 | } |
| ... | ... | @@ -4202,7 +4242,6 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 4202 | 4242 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 4203 | 4243 | |
| 4204 | 4244 | const src_scalar_ctype = try f.ctypeFromType(src_ty.scalarType(zcu), .complete); |
| 4205 | | const w = f.object.writer(); |
| 4206 | 4245 | if (need_memcpy) { |
| 4207 | 4246 | // For this memcpy to safely work we need the rhs to have the same |
| 4208 | 4247 | // underlying type as the lhs (i.e. they must both be arrays of the same underlying type). |
| ... | ... | @@ -4216,7 +4255,8 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 4216 | 4255 | try f.writeCValue(w, new_local, .Other); |
| 4217 | 4256 | try w.writeAll(" = "); |
| 4218 | 4257 | try f.writeCValue(w, src_val, .Other); |
| 4219 | | try w.writeAll(";\n"); |
| 4258 | try w.writeByte(';'); |
| 4259 | try f.object.newline(); |
| 4220 | 4260 | |
| 4221 | 4261 | break :blk new_local; |
| 4222 | 4262 | } else src_val; |
| ... | ... | @@ -4233,7 +4273,8 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 4233 | 4273 | try f.renderType(w, src_ty); |
| 4234 | 4274 | try w.writeAll("))"); |
| 4235 | 4275 | try f.freeCValue(inst, array_src); |
| 4236 | | try w.writeAll(";\n"); |
| 4276 | try w.writeByte(';'); |
| 4277 | try f.object.newline(); |
| 4237 | 4278 | try v.end(f, inst, w); |
| 4238 | 4279 | } else if (ptr_info.packed_offset.host_size > 0 and ptr_info.flags.vector_index == .none) { |
| 4239 | 4280 | const host_bits = ptr_info.packed_offset.host_size * 8; |
| ... | ... | @@ -4251,7 +4292,7 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 4251 | 4292 | var mask = try BigInt.Managed.initCapacity(stack.get(), BigInt.calcTwosCompLimbCount(host_bits)); |
| 4252 | 4293 | defer mask.deinit(); |
| 4253 | 4294 | |
| 4254 | | try mask.setTwosCompIntLimit(.max, .unsigned, @as(usize, @intCast(src_bits))); |
| 4295 | try mask.setTwosCompIntLimit(.max, .unsigned, @intCast(src_bits)); |
| 4255 | 4296 | try mask.shiftLeft(&mask, ptr_info.packed_offset.bit_offset); |
| 4256 | 4297 | try mask.bitNotWrap(&mask, .unsigned, host_bits); |
| 4257 | 4298 | |
| ... | ... | @@ -4331,7 +4372,7 @@ fn airOverflow(f: *Function, inst: Air.Inst.Index, operation: []const u8, info: |
| 4331 | 4372 | const operand_ty = f.typeOf(bin_op.lhs); |
| 4332 | 4373 | const scalar_ty = operand_ty.scalarType(zcu); |
| 4333 | 4374 | |
| 4334 | | const w = f.object.writer(); |
| 4375 | const w = &f.object.code.writer; |
| 4335 | 4376 | const local = try f.allocLocal(inst, inst_ty); |
| 4336 | 4377 | const v = try Vectorize.start(f, inst, w, operand_ty); |
| 4337 | 4378 | try f.writeCValueMember(w, local, .{ .field = 1 }); |
| ... | ... | @@ -4350,7 +4391,8 @@ fn airOverflow(f: *Function, inst: Air.Inst.Index, operation: []const u8, info: |
| 4350 | 4391 | try f.writeCValue(w, rhs, .FunctionArgument); |
| 4351 | 4392 | if (f.typeOf(bin_op.rhs).isVector(zcu)) try v.elem(f, w); |
| 4352 | 4393 | try f.object.dg.renderBuiltinInfo(w, scalar_ty, info); |
| 4353 | | try w.writeAll(");\n"); |
| 4394 | try w.writeAll(");"); |
| 4395 | try f.object.newline(); |
| 4354 | 4396 | try v.end(f, inst, w); |
| 4355 | 4397 | |
| 4356 | 4398 | return local; |
| ... | ... | @@ -4369,7 +4411,7 @@ fn airNot(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4369 | 4411 | |
| 4370 | 4412 | const inst_ty = f.typeOfIndex(inst); |
| 4371 | 4413 | |
| 4372 | | const w = f.object.writer(); |
| 4414 | const w = &f.object.code.writer; |
| 4373 | 4415 | const local = try f.allocLocal(inst, inst_ty); |
| 4374 | 4416 | const v = try Vectorize.start(f, inst, w, operand_ty); |
| 4375 | 4417 | try f.writeCValue(w, local, .Other); |
| ... | ... | @@ -4378,7 +4420,8 @@ fn airNot(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4378 | 4420 | try w.writeByte('!'); |
| 4379 | 4421 | try f.writeCValue(w, op, .Other); |
| 4380 | 4422 | try v.elem(f, w); |
| 4381 | | try w.writeAll(";\n"); |
| 4423 | try w.writeByte(';'); |
| 4424 | try f.object.newline(); |
| 4382 | 4425 | try v.end(f, inst, w); |
| 4383 | 4426 | |
| 4384 | 4427 | return local; |
| ... | ... | @@ -4405,7 +4448,7 @@ fn airBinOp( |
| 4405 | 4448 | |
| 4406 | 4449 | const inst_ty = f.typeOfIndex(inst); |
| 4407 | 4450 | |
| 4408 | | const w = f.object.writer(); |
| 4451 | const w = &f.object.code.writer; |
| 4409 | 4452 | const local = try f.allocLocal(inst, inst_ty); |
| 4410 | 4453 | const v = try Vectorize.start(f, inst, w, operand_ty); |
| 4411 | 4454 | try f.writeCValue(w, local, .Other); |
| ... | ... | @@ -4418,7 +4461,8 @@ fn airBinOp( |
| 4418 | 4461 | try w.writeByte(' '); |
| 4419 | 4462 | try f.writeCValue(w, rhs, .Other); |
| 4420 | 4463 | try v.elem(f, w); |
| 4421 | | try w.writeAll(";\n"); |
| 4464 | try w.writeByte(';'); |
| 4465 | try f.object.newline(); |
| 4422 | 4466 | try v.end(f, inst, w); |
| 4423 | 4467 | |
| 4424 | 4468 | return local; |
| ... | ... | @@ -4455,7 +4499,7 @@ fn airCmpOp( |
| 4455 | 4499 | |
| 4456 | 4500 | const rhs_ty = f.typeOf(data.rhs); |
| 4457 | 4501 | const need_cast = lhs_ty.isSinglePointer(zcu) or rhs_ty.isSinglePointer(zcu); |
| 4458 | | const w = f.object.writer(); |
| 4502 | const w = &f.object.code.writer; |
| 4459 | 4503 | const local = try f.allocLocal(inst, inst_ty); |
| 4460 | 4504 | const v = try Vectorize.start(f, inst, w, lhs_ty); |
| 4461 | 4505 | const a = try Assignment.start(f, w, try f.ctypeFromType(scalar_ty, .complete)); |
| ... | ... | @@ -4508,7 +4552,7 @@ fn airEquality( |
| 4508 | 4552 | const rhs = try f.resolveInst(bin_op.rhs); |
| 4509 | 4553 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 4510 | 4554 | |
| 4511 | | const w = f.object.writer(); |
| 4555 | const w = &f.object.code.writer; |
| 4512 | 4556 | const local = try f.allocLocal(inst, .bool); |
| 4513 | 4557 | const a = try Assignment.start(f, w, .bool); |
| 4514 | 4558 | try f.writeCValue(w, local, .Other); |
| ... | ... | @@ -4566,12 +4610,13 @@ fn airCmpLtErrorsLen(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4566 | 4610 | const operand = try f.resolveInst(un_op); |
| 4567 | 4611 | try reap(f, inst, &.{un_op}); |
| 4568 | 4612 | |
| 4569 | | const w = f.object.writer(); |
| 4613 | const w = &f.object.code.writer; |
| 4570 | 4614 | const local = try f.allocLocal(inst, .bool); |
| 4571 | 4615 | try f.writeCValue(w, local, .Other); |
| 4572 | 4616 | try w.writeAll(" = "); |
| 4573 | 4617 | try f.writeCValue(w, operand, .Other); |
| 4574 | | try w.print(" < sizeof({f}) / sizeof(*{0f});\n", .{fmtIdentSolo("zig_errorName")}); |
| 4618 | try w.print(" < sizeof({f}) / sizeof(*{0f});", .{fmtIdentSolo("zig_errorName")}); |
| 4619 | try f.object.newline(); |
| 4575 | 4620 | return local; |
| 4576 | 4621 | } |
| 4577 | 4622 | |
| ... | ... | @@ -4592,7 +4637,7 @@ fn airPtrAddSub(f: *Function, inst: Air.Inst.Index, operator: u8) !CValue { |
| 4592 | 4637 | const inst_scalar_ctype = try f.ctypeFromType(inst_scalar_ty, .complete); |
| 4593 | 4638 | |
| 4594 | 4639 | const local = try f.allocLocal(inst, inst_ty); |
| 4595 | | const w = f.object.writer(); |
| 4640 | const w = &f.object.code.writer; |
| 4596 | 4641 | const v = try Vectorize.start(f, inst, w, inst_ty); |
| 4597 | 4642 | const a = try Assignment.start(f, w, inst_scalar_ctype); |
| 4598 | 4643 | try f.writeCValue(w, local, .Other); |
| ... | ... | @@ -4634,7 +4679,7 @@ fn airMinMax(f: *Function, inst: Air.Inst.Index, operator: u8, operation: []cons |
| 4634 | 4679 | const rhs = try f.resolveInst(bin_op.rhs); |
| 4635 | 4680 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 4636 | 4681 | |
| 4637 | | const w = f.object.writer(); |
| 4682 | const w = &f.object.code.writer; |
| 4638 | 4683 | const local = try f.allocLocal(inst, inst_ty); |
| 4639 | 4684 | const v = try Vectorize.start(f, inst, w, inst_ty); |
| 4640 | 4685 | try f.writeCValue(w, local, .Other); |
| ... | ... | @@ -4654,7 +4699,8 @@ fn airMinMax(f: *Function, inst: Air.Inst.Index, operator: u8, operation: []cons |
| 4654 | 4699 | try w.writeAll(" : "); |
| 4655 | 4700 | try f.writeCValue(w, rhs, .Other); |
| 4656 | 4701 | try v.elem(f, w); |
| 4657 | | try w.writeAll(";\n"); |
| 4702 | try w.writeByte(';'); |
| 4703 | try f.object.newline(); |
| 4658 | 4704 | try v.end(f, inst, w); |
| 4659 | 4705 | |
| 4660 | 4706 | return local; |
| ... | ... | @@ -4673,7 +4719,7 @@ fn airSlice(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4673 | 4719 | const inst_ty = f.typeOfIndex(inst); |
| 4674 | 4720 | const ptr_ty = inst_ty.slicePtrFieldType(zcu); |
| 4675 | 4721 | |
| 4676 | | const w = f.object.writer(); |
| 4722 | const w = &f.object.code.writer; |
| 4677 | 4723 | const local = try f.allocLocal(inst, inst_ty); |
| 4678 | 4724 | { |
| 4679 | 4725 | const a = try Assignment.start(f, w, try f.ctypeFromType(ptr_ty, .complete)); |
| ... | ... | @@ -4704,7 +4750,7 @@ fn airCall( |
| 4704 | 4750 | if (f.object.dg.is_naked_fn) return .none; |
| 4705 | 4751 | |
| 4706 | 4752 | const gpa = f.object.dg.gpa; |
| 4707 | | const w = f.object.writer(); |
| 4753 | const w = &f.object.code.writer; |
| 4708 | 4754 | |
| 4709 | 4755 | const pl_op = f.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 4710 | 4756 | const extra = f.air.extraData(Air.Call, pl_op.payload); |
| ... | ... | @@ -4731,7 +4777,8 @@ fn airCall( |
| 4731 | 4777 | try f.writeCValue(w, resolved_arg.*, .FunctionArgument); |
| 4732 | 4778 | try w.writeAll(", sizeof("); |
| 4733 | 4779 | try f.renderCType(w, arg_ctype); |
| 4734 | | try w.writeAll("));\n"); |
| 4780 | try w.writeAll("));"); |
| 4781 | try f.object.newline(); |
| 4735 | 4782 | resolved_arg.* = array_local; |
| 4736 | 4783 | } |
| 4737 | 4784 | } |
| ... | ... | @@ -4826,7 +4873,11 @@ fn airCall( |
| 4826 | 4873 | try f.writeCValue(w, resolved_arg, .FunctionArgument); |
| 4827 | 4874 | try f.freeCValue(inst, resolved_arg); |
| 4828 | 4875 | } |
| 4829 | | try w.writeAll(");\n"); |
| 4876 | try w.writeAll(");"); |
| 4877 | switch (modifier) { |
| 4878 | .always_tail => try w.writeByte('\n'), |
| 4879 | else => try f.object.newline(), |
| 4880 | } |
| 4830 | 4881 | |
| 4831 | 4882 | const result = result: { |
| 4832 | 4883 | if (result_local == .none or !lowersToArray(ret_ty, pt)) |
| ... | ... | @@ -4839,7 +4890,8 @@ fn airCall( |
| 4839 | 4890 | try f.writeCValueMember(w, result_local, .{ .identifier = "array" }); |
| 4840 | 4891 | try w.writeAll(", sizeof("); |
| 4841 | 4892 | try f.renderType(w, ret_ty); |
| 4842 | | try w.writeAll("));\n"); |
| 4893 | try w.writeAll("));"); |
| 4894 | try f.object.newline(); |
| 4843 | 4895 | try freeLocal(f, inst, result_local.new_local, null); |
| 4844 | 4896 | break :result array_local; |
| 4845 | 4897 | }; |
| ... | ... | @@ -4849,7 +4901,7 @@ fn airCall( |
| 4849 | 4901 | |
| 4850 | 4902 | fn airDbgStmt(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4851 | 4903 | const dbg_stmt = f.air.instructions.items(.data)[@intFromEnum(inst)].dbg_stmt; |
| 4852 | | const w = f.object.writer(); |
| 4904 | const w = &f.object.code.writer; |
| 4853 | 4905 | // TODO re-evaluate whether to emit these or not. If we naively emit |
| 4854 | 4906 | // these directives, the output file will report bogus line numbers because |
| 4855 | 4907 | // every newline after the #line directive adds one to the line. |
| ... | ... | @@ -4857,13 +4909,16 @@ fn airDbgStmt(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4857 | 4909 | // If we wanted to go this route, we would need to go all the way and not output |
| 4858 | 4910 | // newlines until the next dbg_stmt occurs. |
| 4859 | 4911 | // Perhaps an additional compilation option is in order? |
| 4860 | | //try w.print("#line {d}\n", .{dbg_stmt.line + 1}); |
| 4861 | | try w.print("/* file:{d}:{d} */\n", .{ dbg_stmt.line + 1, dbg_stmt.column + 1 }); |
| 4912 | //try w.print("#line {d}", .{dbg_stmt.line + 1}); |
| 4913 | //try f.object.newline(); |
| 4914 | try w.print("/* file:{d}:{d} */", .{ dbg_stmt.line + 1, dbg_stmt.column + 1 }); |
| 4915 | try f.object.newline(); |
| 4862 | 4916 | return .none; |
| 4863 | 4917 | } |
| 4864 | 4918 | |
| 4865 | 4919 | fn airDbgEmptyStmt(f: *Function, _: Air.Inst.Index) !CValue { |
| 4866 | | try f.object.writer().writeAll("(void)0;\n"); |
| 4920 | try f.object.code.writer.writeAll("(void)0;"); |
| 4921 | try f.object.newline(); |
| 4867 | 4922 | return .none; |
| 4868 | 4923 | } |
| 4869 | 4924 | |
| ... | ... | @@ -4874,8 +4929,9 @@ fn airDbgInlineBlock(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4874 | 4929 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4875 | 4930 | const extra = f.air.extraData(Air.DbgInlineBlock, ty_pl.payload); |
| 4876 | 4931 | const owner_nav = ip.getNav(zcu.funcInfo(extra.data.func).owner_nav); |
| 4877 | | const w = f.object.writer(); |
| 4878 | | try w.print("/* inline:{} */\n", .{owner_nav.fqn.fmt(&zcu.intern_pool)}); |
| 4932 | const w = &f.object.code.writer; |
| 4933 | try w.print("/* inline:{f} */", .{owner_nav.fqn.fmt(&zcu.intern_pool)}); |
| 4934 | try f.object.newline(); |
| 4879 | 4935 | return lowerBlock(f, inst, @ptrCast(f.air.extra.items[extra.end..][0..extra.data.body_len])); |
| 4880 | 4936 | } |
| 4881 | 4937 | |
| ... | ... | @@ -4889,8 +4945,9 @@ fn airDbgVar(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4889 | 4945 | if (!operand_is_undef) _ = try f.resolveInst(pl_op.operand); |
| 4890 | 4946 | |
| 4891 | 4947 | try reap(f, inst, &.{pl_op.operand}); |
| 4892 | | const w = f.object.writer(); |
| 4893 | | try w.print("/* {s}:{s} */\n", .{ @tagName(tag), name.toSlice(f.air) }); |
| 4948 | const w = &f.object.code.writer; |
| 4949 | try w.print("/* {s}:{s} */", .{ @tagName(tag), name.toSlice(f.air) }); |
| 4950 | try f.object.newline(); |
| 4894 | 4951 | return .none; |
| 4895 | 4952 | } |
| 4896 | 4953 | |
| ... | ... | @@ -4907,7 +4964,7 @@ fn lowerBlock(f: *Function, inst: Air.Inst.Index, body: []const Air.Inst.Index) |
| 4907 | 4964 | |
| 4908 | 4965 | const block_id = f.next_block_index; |
| 4909 | 4966 | f.next_block_index += 1; |
| 4910 | | const w = f.object.writer(); |
| 4967 | const w = &f.object.code.writer; |
| 4911 | 4968 | |
| 4912 | 4969 | const inst_ty = f.typeOfIndex(inst); |
| 4913 | 4970 | const result = if (inst_ty.hasRuntimeBitsIgnoreComptime(zcu) and !f.liveness.isUnused(inst)) |
| ... | ... | @@ -4929,8 +4986,6 @@ fn lowerBlock(f: *Function, inst: Air.Inst.Index, body: []const Air.Inst.Index) |
| 4929 | 4986 | try die(f, inst, death.toRef()); |
| 4930 | 4987 | } |
| 4931 | 4988 | |
| 4932 | | try f.object.indent_writer.insertNewline(); |
| 4933 | | |
| 4934 | 4989 | // noreturn blocks have no `br` instructions reaching them, so we don't want a label |
| 4935 | 4990 | if (f.object.dg.is_naked_fn) { |
| 4936 | 4991 | if (f.object.dg.expected_block) |expected_block| { |
| ... | ... | @@ -4940,7 +4995,8 @@ fn lowerBlock(f: *Function, inst: Air.Inst.Index, body: []const Air.Inst.Index) |
| 4940 | 4995 | } |
| 4941 | 4996 | } else if (!f.typeOfIndex(inst).isNoReturn(zcu)) { |
| 4942 | 4997 | // label must be followed by an expression, include an empty one. |
| 4943 | | try w.print("zig_block_{d}:;\n", .{block_id}); |
| 4998 | try w.print("\nzig_block_{d}:;", .{block_id}); |
| 4999 | try f.object.newline(); |
| 4944 | 5000 | } |
| 4945 | 5001 | |
| 4946 | 5002 | return result; |
| ... | ... | @@ -4977,7 +5033,7 @@ fn lowerTry( |
| 4977 | 5033 | const err_union = try f.resolveInst(operand); |
| 4978 | 5034 | const inst_ty = f.typeOfIndex(inst); |
| 4979 | 5035 | const liveness_condbr = f.liveness.getCondBr(inst); |
| 4980 | | const w = f.object.writer(); |
| 5036 | const w = &f.object.code.writer; |
| 4981 | 5037 | const payload_ty = err_union_ty.errorUnionPayload(zcu); |
| 4982 | 5038 | const payload_has_bits = payload_ty.hasRuntimeBitsIgnoreComptime(zcu); |
| 4983 | 5039 | |
| ... | ... | @@ -5001,7 +5057,7 @@ fn lowerTry( |
| 5001 | 5057 | try w.writeAll(") "); |
| 5002 | 5058 | |
| 5003 | 5059 | try genBodyResolveState(f, inst, liveness_condbr.else_deaths, body, false); |
| 5004 | | try f.object.indent_writer.insertNewline(); |
| 5060 | try f.object.newline(); |
| 5005 | 5061 | if (f.object.dg.expected_block) |_| |
| 5006 | 5062 | return f.fail("runtime code not allowed in naked function", .{}); |
| 5007 | 5063 | } |
| ... | ... | @@ -5039,7 +5095,7 @@ fn airBr(f: *Function, inst: Air.Inst.Index) !void { |
| 5039 | 5095 | const branch = f.air.instructions.items(.data)[@intFromEnum(inst)].br; |
| 5040 | 5096 | const block = f.blocks.get(branch.block_inst).?; |
| 5041 | 5097 | const result = block.result; |
| 5042 | | const w = f.object.writer(); |
| 5098 | const w = &f.object.code.writer; |
| 5043 | 5099 | |
| 5044 | 5100 | if (f.object.dg.is_naked_fn) { |
| 5045 | 5101 | if (result != .none) return f.fail("runtime code not allowed in naked function", .{}); |
| ... | ... | @@ -5065,15 +5121,14 @@ fn airBr(f: *Function, inst: Air.Inst.Index) !void { |
| 5065 | 5121 | |
| 5066 | 5122 | fn airRepeat(f: *Function, inst: Air.Inst.Index) !void { |
| 5067 | 5123 | const repeat = f.air.instructions.items(.data)[@intFromEnum(inst)].repeat; |
| 5068 | | const w = f.object.writer(); |
| 5069 | | try w.print("goto zig_loop_{d};\n", .{@intFromEnum(repeat.loop_inst)}); |
| 5124 | try f.object.code.writer.print("goto zig_loop_{d};\n", .{@intFromEnum(repeat.loop_inst)}); |
| 5070 | 5125 | } |
| 5071 | 5126 | |
| 5072 | 5127 | fn airSwitchDispatch(f: *Function, inst: Air.Inst.Index) !void { |
| 5073 | 5128 | const pt = f.object.dg.pt; |
| 5074 | 5129 | const zcu = pt.zcu; |
| 5075 | 5130 | const br = f.air.instructions.items(.data)[@intFromEnum(inst)].br; |
| 5076 | | const w = f.object.writer(); |
| 5131 | const w = &f.object.code.writer; |
| 5077 | 5132 | |
| 5078 | 5133 | if (try f.air.value(br.operand, pt)) |cond_val| { |
| 5079 | 5134 | // Comptime-known dispatch. Iterate the cases to find the correct |
| ... | ... | @@ -5105,8 +5160,9 @@ fn airSwitchDispatch(f: *Function, inst: Air.Inst.Index) !void { |
| 5105 | 5160 | try f.writeCValue(w, .{ .local = cond_local }, .Other); |
| 5106 | 5161 | try w.writeAll(" = "); |
| 5107 | 5162 | try f.writeCValue(w, cond, .Other); |
| 5108 | | try w.writeAll(";\n"); |
| 5109 | | try w.print("goto zig_switch_{d}_loop;", .{@intFromEnum(br.block_inst)}); |
| 5163 | try w.writeByte(';'); |
| 5164 | try f.object.newline(); |
| 5165 | try w.print("goto zig_switch_{d}_loop;\n", .{@intFromEnum(br.block_inst)}); |
| 5110 | 5166 | } |
| 5111 | 5167 | |
| 5112 | 5168 | fn airBitcast(f: *Function, inst: Air.Inst.Index) !CValue { |
| ... | ... | @@ -5126,7 +5182,7 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !CVal |
| 5126 | 5182 | const zcu = pt.zcu; |
| 5127 | 5183 | const target = &f.object.dg.mod.resolved_target.result; |
| 5128 | 5184 | const ctype_pool = &f.object.dg.ctype_pool; |
| 5129 | | const w = f.object.writer(); |
| 5185 | const w = &f.object.code.writer; |
| 5130 | 5186 | |
| 5131 | 5187 | if (operand_ty.isAbiInt(zcu) and dest_ty.isAbiInt(zcu)) { |
| 5132 | 5188 | const src_info = dest_ty.intInfo(zcu); |
| ... | ... | @@ -5142,16 +5198,24 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !CVal |
| 5142 | 5198 | try f.renderType(w, dest_ty); |
| 5143 | 5199 | try w.writeByte(')'); |
| 5144 | 5200 | try f.writeCValue(w, operand, .Other); |
| 5145 | | try w.writeAll(";\n"); |
| 5201 | try w.writeByte(';'); |
| 5202 | try f.object.newline(); |
| 5146 | 5203 | return local; |
| 5147 | 5204 | } |
| 5148 | 5205 | |
| 5149 | 5206 | const operand_lval = if (operand == .constant) blk: { |
| 5150 | 5207 | const operand_local = try f.allocLocal(null, operand_ty); |
| 5151 | 5208 | try f.writeCValue(w, operand_local, .Other); |
| 5152 | | try w.writeAll(" = "); |
| 5209 | if (operand_ty.isAbiInt(zcu)) { |
| 5210 | try w.writeAll(" = "); |
| 5211 | } else { |
| 5212 | try w.writeAll(" = ("); |
| 5213 | try f.renderType(w, operand_ty); |
| 5214 | try w.writeByte(')'); |
| 5215 | } |
| 5153 | 5216 | try f.writeCValue(w, operand, .Other); |
| 5154 | | try w.writeAll(";\n"); |
| 5217 | try w.writeByte(';'); |
| 5218 | try f.object.newline(); |
| 5155 | 5219 | break :blk operand_local; |
| 5156 | 5220 | } else operand; |
| 5157 | 5221 | |
| ... | ... | @@ -5165,7 +5229,8 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !CVal |
| 5165 | 5229 | w, |
| 5166 | 5230 | if (dest_ty.abiSize(zcu) <= operand_ty.abiSize(zcu)) dest_ty else operand_ty, |
| 5167 | 5231 | ); |
| 5168 | | try w.writeAll("));\n"); |
| 5232 | try w.writeAll("));"); |
| 5233 | try f.object.newline(); |
| 5169 | 5234 | |
| 5170 | 5235 | // Ensure padding bits have the expected value. |
| 5171 | 5236 | if (dest_ty.isAbiInt(zcu)) { |
| ... | ... | @@ -5221,7 +5286,8 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !CVal |
| 5221 | 5286 | if (need_bitcasts) try w.writeByte(')'); |
| 5222 | 5287 | try f.object.dg.renderBuiltinInfo(w, info_ty, .bits); |
| 5223 | 5288 | if (need_bitcasts) try w.writeByte(')'); |
| 5224 | | try w.writeAll(");\n"); |
| 5289 | try w.writeAll(");"); |
| 5290 | try f.object.newline(); |
| 5225 | 5291 | } |
| 5226 | 5292 | |
| 5227 | 5293 | try f.freeCValue(null, operand_lval); |
| ... | ... | @@ -5234,48 +5300,53 @@ fn airTrap(f: *Function, w: *Writer) !void { |
| 5234 | 5300 | try w.writeAll("zig_trap();\n"); |
| 5235 | 5301 | } |
| 5236 | 5302 | |
| 5237 | | fn airBreakpoint(w: *Writer) !CValue { |
| 5238 | | try w.writeAll("zig_breakpoint();\n"); |
| 5303 | fn airBreakpoint(f: *Function) !CValue { |
| 5304 | const w = &f.object.code.writer; |
| 5305 | try w.writeAll("zig_breakpoint();"); |
| 5306 | try f.object.newline(); |
| 5239 | 5307 | return .none; |
| 5240 | 5308 | } |
| 5241 | 5309 | |
| 5242 | 5310 | fn airRetAddr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5243 | | const w = f.object.writer(); |
| 5311 | const w = &f.object.code.writer; |
| 5244 | 5312 | const local = try f.allocLocal(inst, .usize); |
| 5245 | 5313 | try f.writeCValue(w, local, .Other); |
| 5246 | 5314 | try w.writeAll(" = ("); |
| 5247 | 5315 | try f.renderType(w, .usize); |
| 5248 | | try w.writeAll(")zig_return_address();\n"); |
| 5316 | try w.writeAll(")zig_return_address();"); |
| 5317 | try f.object.newline(); |
| 5249 | 5318 | return local; |
| 5250 | 5319 | } |
| 5251 | 5320 | |
| 5252 | 5321 | fn airFrameAddress(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5253 | | const w = f.object.writer(); |
| 5322 | const w = &f.object.code.writer; |
| 5254 | 5323 | const local = try f.allocLocal(inst, .usize); |
| 5255 | 5324 | try f.writeCValue(w, local, .Other); |
| 5256 | 5325 | try w.writeAll(" = ("); |
| 5257 | 5326 | try f.renderType(w, .usize); |
| 5258 | | try w.writeAll(")zig_frame_address();\n"); |
| 5327 | try w.writeAll(")zig_frame_address();"); |
| 5328 | try f.object.newline(); |
| 5259 | 5329 | return local; |
| 5260 | 5330 | } |
| 5261 | 5331 | |
| 5262 | | fn airUnreach(f: *Function) !void { |
| 5332 | fn airUnreach(o: *Object) !void { |
| 5263 | 5333 | // Not even allowed to call unreachable in a naked function. |
| 5264 | | if (f.object.dg.is_naked_fn) return; |
| 5265 | | try f.object.writer().writeAll("zig_unreachable();\n"); |
| 5334 | if (o.dg.is_naked_fn) return; |
| 5335 | try o.code.writer.writeAll("zig_unreachable();\n"); |
| 5266 | 5336 | } |
| 5267 | 5337 | |
| 5268 | 5338 | fn airLoop(f: *Function, inst: Air.Inst.Index) !void { |
| 5269 | 5339 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 5270 | 5340 | const loop = f.air.extraData(Air.Block, ty_pl.payload); |
| 5271 | 5341 | const body: []const Air.Inst.Index = @ptrCast(f.air.extra.items[loop.end..][0..loop.data.body_len]); |
| 5272 | | const w = f.object.writer(); |
| 5342 | const w = &f.object.code.writer; |
| 5273 | 5343 | |
| 5274 | 5344 | // `repeat` instructions matching this loop will branch to |
| 5275 | 5345 | // this label. Since we need a label for arbitrary `repeat` |
| 5276 | 5346 | // anyway, there's actually no need to use a "real" looping |
| 5277 | 5347 | // construct at all! |
| 5278 | | try w.print("zig_loop_{d}:\n", .{@intFromEnum(inst)}); |
| 5348 | try w.print("zig_loop_{d}:", .{@intFromEnum(inst)}); |
| 5349 | try f.object.newline(); |
| 5279 | 5350 | try genBodyInner(f, body); // no need to restore state, we're noreturn |
| 5280 | 5351 | } |
| 5281 | 5352 | |
| ... | ... | @@ -5287,14 +5358,14 @@ fn airCondBr(f: *Function, inst: Air.Inst.Index) !void { |
| 5287 | 5358 | const then_body: []const Air.Inst.Index = @ptrCast(f.air.extra.items[extra.end..][0..extra.data.then_body_len]); |
| 5288 | 5359 | const else_body: []const Air.Inst.Index = @ptrCast(f.air.extra.items[extra.end + then_body.len ..][0..extra.data.else_body_len]); |
| 5289 | 5360 | const liveness_condbr = f.liveness.getCondBr(inst); |
| 5290 | | const w = f.object.writer(); |
| 5361 | const w = &f.object.code.writer; |
| 5291 | 5362 | |
| 5292 | 5363 | try w.writeAll("if ("); |
| 5293 | 5364 | try f.writeCValue(w, cond, .Other); |
| 5294 | 5365 | try w.writeAll(") "); |
| 5295 | 5366 | |
| 5296 | 5367 | try genBodyResolveState(f, inst, liveness_condbr.then_deaths, then_body, false); |
| 5297 | | try w.writeByte('\n'); |
| 5368 | try f.object.newline(); |
| 5298 | 5369 | if (else_body.len > 0) if (f.object.dg.expected_block) |_| |
| 5299 | 5370 | return f.fail("runtime code not allowed in naked function", .{}); |
| 5300 | 5371 | |
| ... | ... | @@ -5320,7 +5391,7 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void |
| 5320 | 5391 | const init_condition = try f.resolveInst(switch_br.operand); |
| 5321 | 5392 | try reap(f, inst, &.{switch_br.operand}); |
| 5322 | 5393 | const condition_ty = f.typeOf(switch_br.operand); |
| 5323 | | const w = f.object.writer(); |
| 5394 | const w = &f.object.code.writer; |
| 5324 | 5395 | |
| 5325 | 5396 | // For dispatches, we will create a local alloc to contain the condition value. |
| 5326 | 5397 | // This may not result in optimal codegen for switch loops, but it minimizes the |
| ... | ... | @@ -5328,7 +5399,8 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void |
| 5328 | 5399 | const condition = if (is_dispatch_loop) cond: { |
| 5329 | 5400 | const new_local = try f.allocLocal(inst, condition_ty); |
| 5330 | 5401 | try f.copyCValue(try f.ctypeFromType(condition_ty, .complete), new_local, init_condition); |
| 5331 | | try w.print("zig_switch_{d}_loop:\n", .{@intFromEnum(inst)}); |
| 5402 | try w.print("zig_switch_{d}_loop:", .{@intFromEnum(inst)}); |
| 5403 | try f.object.newline(); |
| 5332 | 5404 | try f.loop_switch_conds.put(gpa, inst, new_local.new_local); |
| 5333 | 5405 | break :cond new_local; |
| 5334 | 5406 | } else init_condition; |
| ... | ... | @@ -5352,7 +5424,7 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void |
| 5352 | 5424 | } |
| 5353 | 5425 | try f.writeCValue(w, condition, .Other); |
| 5354 | 5426 | try w.writeAll(") {"); |
| 5355 | | f.object.indent_writer.pushIndent(); |
| 5427 | f.object.indent(); |
| 5356 | 5428 | |
| 5357 | 5429 | const liveness = try f.liveness.getSwitchBr(gpa, inst, switch_br.cases_len + 1); |
| 5358 | 5430 | defer gpa.free(liveness.deaths); |
| ... | ... | @@ -5365,7 +5437,7 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void |
| 5365 | 5437 | continue; |
| 5366 | 5438 | } |
| 5367 | 5439 | for (case.items) |item| { |
| 5368 | | try f.object.indent_writer.insertNewline(); |
| 5440 | try f.object.newline(); |
| 5369 | 5441 | try w.writeAll("case "); |
| 5370 | 5442 | const item_value = try f.air.value(item, pt); |
| 5371 | 5443 | // If `item_value` is a pointer with a known integer address, print the address |
| ... | ... | @@ -5386,13 +5458,15 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void |
| 5386 | 5458 | } |
| 5387 | 5459 | try w.writeByte(':'); |
| 5388 | 5460 | } |
| 5389 | | try w.writeAll(" {\n"); |
| 5390 | | f.object.indent_writer.pushIndent(); |
| 5461 | try w.writeAll(" {"); |
| 5462 | f.object.indent(); |
| 5463 | try f.object.newline(); |
| 5391 | 5464 | if (is_dispatch_loop) { |
| 5392 | | try w.print("zig_switch_{d}_dispatch_{d}: ", .{ @intFromEnum(inst), case.idx }); |
| 5465 | try w.print("zig_switch_{d}_dispatch_{d}:;", .{ @intFromEnum(inst), case.idx }); |
| 5466 | try f.object.newline(); |
| 5393 | 5467 | } |
| 5394 | 5468 | try genBodyResolveState(f, inst, liveness.deaths[case.idx], case.body, true); |
| 5395 | | f.object.indent_writer.popIndent(); |
| 5469 | try f.object.outdent(); |
| 5396 | 5470 | try w.writeByte('}'); |
| 5397 | 5471 | if (f.object.dg.expected_block) |_| |
| 5398 | 5472 | return f.fail("runtime code not allowed in naked function", .{}); |
| ... | ... | @@ -5401,7 +5475,7 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void |
| 5401 | 5475 | } |
| 5402 | 5476 | |
| 5403 | 5477 | const else_body = it.elseBody(); |
| 5404 | | try f.object.indent_writer.insertNewline(); |
| 5478 | try f.object.newline(); |
| 5405 | 5479 | |
| 5406 | 5480 | try w.writeAll("default: "); |
| 5407 | 5481 | if (any_range_cases) { |
| ... | ... | @@ -5431,13 +5505,14 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void |
| 5431 | 5505 | try f.object.dg.renderValue(w, (try f.air.value(range[1], pt)).?, .Other); |
| 5432 | 5506 | try w.writeByte(')'); |
| 5433 | 5507 | } |
| 5434 | | try w.writeAll(") {\n"); |
| 5435 | | f.object.indent_writer.pushIndent(); |
| 5508 | try w.writeAll(") {"); |
| 5509 | f.object.indent(); |
| 5510 | try f.object.newline(); |
| 5436 | 5511 | if (is_dispatch_loop) { |
| 5437 | 5512 | try w.print("zig_switch_{d}_dispatch_{d}: ", .{ @intFromEnum(inst), case.idx }); |
| 5438 | 5513 | } |
| 5439 | 5514 | try genBodyResolveState(f, inst, liveness.deaths[case.idx], case.body, true); |
| 5440 | | f.object.indent_writer.popIndent(); |
| 5515 | try f.object.outdent(); |
| 5441 | 5516 | try w.writeByte('}'); |
| 5442 | 5517 | if (f.object.dg.expected_block) |_| |
| 5443 | 5518 | return f.fail("runtime code not allowed in naked function", .{}); |
| ... | ... | @@ -5455,12 +5530,9 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void |
| 5455 | 5530 | try genBody(f, else_body); |
| 5456 | 5531 | if (f.object.dg.expected_block) |_| |
| 5457 | 5532 | return f.fail("runtime code not allowed in naked function", .{}); |
| 5458 | | } else { |
| 5459 | | try w.writeAll("zig_unreachable();"); |
| 5460 | | } |
| 5461 | | try f.object.indent_writer.insertNewline(); |
| 5462 | | |
| 5463 | | f.object.indent_writer.popIndent(); |
| 5533 | } else try airUnreach(&f.object); |
| 5534 | try f.object.newline(); |
| 5535 | try f.object.outdent(); |
| 5464 | 5536 | try w.writeAll("}\n"); |
| 5465 | 5537 | } |
| 5466 | 5538 | |
| ... | ... | @@ -5499,7 +5571,7 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5499 | 5571 | extra_i += inputs.len; |
| 5500 | 5572 | |
| 5501 | 5573 | const result = result: { |
| 5502 | | const w = f.object.writer(); |
| 5574 | const w = &f.object.code.writer; |
| 5503 | 5575 | const inst_ty = f.typeOfIndex(inst); |
| 5504 | 5576 | const inst_local = if (inst_ty.hasRuntimeBitsIgnoreComptime(zcu)) local: { |
| 5505 | 5577 | const inst_local = try f.allocLocalValue(.{ |
| ... | ... | @@ -5510,7 +5582,8 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5510 | 5582 | try f.writeCValue(w, inst_local, .Other); |
| 5511 | 5583 | try w.writeAll(" = "); |
| 5512 | 5584 | try f.writeCValue(w, .{ .undef = inst_ty }, .Other); |
| 5513 | | try w.writeAll(";\n"); |
| 5585 | try w.writeByte(';'); |
| 5586 | try f.object.newline(); |
| 5514 | 5587 | } |
| 5515 | 5588 | break :local inst_local; |
| 5516 | 5589 | } else .none; |
| ... | ... | @@ -5548,7 +5621,8 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5548 | 5621 | try w.writeAll(" = "); |
| 5549 | 5622 | try f.writeCValue(w, .{ .undef = output_ty }, .Other); |
| 5550 | 5623 | } |
| 5551 | | try w.writeAll(";\n"); |
| 5624 | try w.writeByte(';'); |
| 5625 | try f.object.newline(); |
| 5552 | 5626 | } |
| 5553 | 5627 | } |
| 5554 | 5628 | for (inputs) |input| { |
| ... | ... | @@ -5583,7 +5657,8 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5583 | 5657 | } |
| 5584 | 5658 | try w.writeAll(" = "); |
| 5585 | 5659 | try f.writeCValue(w, input_val, .Other); |
| 5586 | | try w.writeAll(";\n"); |
| 5660 | try w.writeByte(';'); |
| 5661 | try f.object.newline(); |
| 5587 | 5662 | } |
| 5588 | 5663 | } |
| 5589 | 5664 | for (0..clobbers_len) |_| { |
| ... | ... | @@ -5709,7 +5784,8 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5709 | 5784 | if (clobber_i > 0) try w.writeByte(','); |
| 5710 | 5785 | try w.print(" {f}", .{fmtStringLiteral(clobber, null)}); |
| 5711 | 5786 | } |
| 5712 | | try w.writeAll(");\n"); |
| 5787 | try w.writeAll(");"); |
| 5788 | try f.object.newline(); |
| 5713 | 5789 | |
| 5714 | 5790 | extra_i = constraints_extra_begin; |
| 5715 | 5791 | locals_index = locals_begin; |
| ... | ... | @@ -5730,7 +5806,8 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5730 | 5806 | try w.writeAll(" = "); |
| 5731 | 5807 | try f.writeCValue(w, .{ .local = locals_index }, .Other); |
| 5732 | 5808 | locals_index += 1; |
| 5733 | | try w.writeAll(";\n"); |
| 5809 | try w.writeByte(';'); |
| 5810 | try f.object.newline(); |
| 5734 | 5811 | } |
| 5735 | 5812 | } |
| 5736 | 5813 | |
| ... | ... | @@ -5760,7 +5837,7 @@ fn airIsNull( |
| 5760 | 5837 | const ctype_pool = &f.object.dg.ctype_pool; |
| 5761 | 5838 | const un_op = f.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 5762 | 5839 | |
| 5763 | | const w = f.object.writer(); |
| 5840 | const w = &f.object.code.writer; |
| 5764 | 5841 | const operand = try f.resolveInst(un_op); |
| 5765 | 5842 | try reap(f, inst, &.{un_op}); |
| 5766 | 5843 | |
| ... | ... | @@ -5828,7 +5905,7 @@ fn airOptionalPayload(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValue |
| 5828 | 5905 | .aligned, .array, .vector, .fwd_decl, .function => unreachable, |
| 5829 | 5906 | .aggregate => |aggregate| switch (aggregate.fields.at(0, ctype_pool).name.index) { |
| 5830 | 5907 | .is_null, .payload => { |
| 5831 | | const w = f.object.writer(); |
| 5908 | const w = &f.object.code.writer; |
| 5832 | 5909 | const local = try f.allocLocal(inst, inst_ty); |
| 5833 | 5910 | const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete)); |
| 5834 | 5911 | try f.writeCValue(w, local, .Other); |
| ... | ... | @@ -5850,7 +5927,7 @@ fn airOptionalPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5850 | 5927 | const pt = f.object.dg.pt; |
| 5851 | 5928 | const zcu = pt.zcu; |
| 5852 | 5929 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5853 | | const w = f.object.writer(); |
| 5930 | const w = &f.object.code.writer; |
| 5854 | 5931 | const operand = try f.resolveInst(ty_op.operand); |
| 5855 | 5932 | try reap(f, inst, &.{ty_op.operand}); |
| 5856 | 5933 | const operand_ty = f.typeOf(ty_op.operand); |
| ... | ... | @@ -6000,7 +6077,7 @@ fn airFieldParentPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6000 | 6077 | const field_ptr_val = try f.resolveInst(extra.field_ptr); |
| 6001 | 6078 | try reap(f, inst, &.{extra.field_ptr}); |
| 6002 | 6079 | |
| 6003 | | const w = f.object.writer(); |
| 6080 | const w = &f.object.code.writer; |
| 6004 | 6081 | const local = try f.allocLocal(inst, container_ptr_ty); |
| 6005 | 6082 | try f.writeCValue(w, local, .Other); |
| 6006 | 6083 | try w.writeAll(" = ("); |
| ... | ... | @@ -6035,7 +6112,8 @@ fn airFieldParentPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6035 | 6112 | }, |
| 6036 | 6113 | } |
| 6037 | 6114 | |
| 6038 | | try w.writeAll(";\n"); |
| 6115 | try w.writeByte(';'); |
| 6116 | try f.object.newline(); |
| 6039 | 6117 | return local; |
| 6040 | 6118 | } |
| 6041 | 6119 | |
| ... | ... | @@ -6054,7 +6132,7 @@ fn fieldPtr( |
| 6054 | 6132 | // Ensure complete type definition is visible before accessing fields. |
| 6055 | 6133 | _ = try f.ctypeFromType(container_ty, .complete); |
| 6056 | 6134 | |
| 6057 | | const w = f.object.writer(); |
| 6135 | const w = &f.object.code.writer; |
| 6058 | 6136 | const local = try f.allocLocal(inst, field_ptr_ty); |
| 6059 | 6137 | try f.writeCValue(w, local, .Other); |
| 6060 | 6138 | try w.writeAll(" = ("); |
| ... | ... | @@ -6080,7 +6158,8 @@ fn fieldPtr( |
| 6080 | 6158 | }, |
| 6081 | 6159 | } |
| 6082 | 6160 | |
| 6083 | | try w.writeAll(";\n"); |
| 6161 | try w.writeByte(';'); |
| 6162 | try f.object.newline(); |
| 6084 | 6163 | return local; |
| 6085 | 6164 | } |
| 6086 | 6165 | |
| ... | ... | @@ -6100,7 +6179,7 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6100 | 6179 | const struct_byval = try f.resolveInst(extra.struct_operand); |
| 6101 | 6180 | try reap(f, inst, &.{extra.struct_operand}); |
| 6102 | 6181 | const struct_ty = f.typeOf(extra.struct_operand); |
| 6103 | | const w = f.object.writer(); |
| 6182 | const w = &f.object.code.writer; |
| 6104 | 6183 | |
| 6105 | 6184 | // Ensure complete type definition is visible before accessing fields. |
| 6106 | 6185 | _ = try f.ctypeFromType(struct_ty, .complete); |
| ... | ... | @@ -6151,7 +6230,8 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6151 | 6230 | }); |
| 6152 | 6231 | if (cant_cast) try w.writeByte(')'); |
| 6153 | 6232 | try f.object.dg.renderBuiltinInfo(w, field_int_ty, .bits); |
| 6154 | | try w.writeAll(");\n"); |
| 6233 | try w.writeAll(");"); |
| 6234 | try f.object.newline(); |
| 6155 | 6235 | if (inst_ty.eql(field_int_ty, zcu)) return temp_local; |
| 6156 | 6236 | |
| 6157 | 6237 | const local = try f.allocLocal(inst, inst_ty); |
| ... | ... | @@ -6162,7 +6242,8 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6162 | 6242 | try f.writeCValue(w, .{ .local_ref = temp_local.new_local }, .FunctionArgument); |
| 6163 | 6243 | try w.writeAll(", sizeof("); |
| 6164 | 6244 | try f.renderType(w, inst_ty); |
| 6165 | | try w.writeAll("));\n"); |
| 6245 | try w.writeAll("));"); |
| 6246 | try f.object.newline(); |
| 6166 | 6247 | } |
| 6167 | 6248 | try freeLocal(f, inst, temp_local.new_local, null); |
| 6168 | 6249 | return local; |
| ... | ... | @@ -6186,7 +6267,8 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6186 | 6267 | try f.writeCValue(w, operand_local, .Other); |
| 6187 | 6268 | try w.writeAll(" = "); |
| 6188 | 6269 | try f.writeCValue(w, struct_byval, .Other); |
| 6189 | | try w.writeAll(";\n"); |
| 6270 | try w.writeByte(';'); |
| 6271 | try f.object.newline(); |
| 6190 | 6272 | break :blk operand_local; |
| 6191 | 6273 | } else struct_byval; |
| 6192 | 6274 | const local = try f.allocLocal(inst, inst_ty); |
| ... | ... | @@ -6203,7 +6285,8 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6203 | 6285 | try f.writeCValue(w, operand_lval, .Other); |
| 6204 | 6286 | try w.writeAll(", sizeof("); |
| 6205 | 6287 | try f.renderType(w, inst_ty); |
| 6206 | | try w.writeAll("));\n"); |
| 6288 | try w.writeAll("));"); |
| 6289 | try f.object.newline(); |
| 6207 | 6290 | } |
| 6208 | 6291 | try f.freeCValue(inst, operand_lval); |
| 6209 | 6292 | return local; |
| ... | ... | @@ -6245,7 +6328,7 @@ fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6245 | 6328 | return local; |
| 6246 | 6329 | } |
| 6247 | 6330 | |
| 6248 | | const w = f.object.writer(); |
| 6331 | const w = &f.object.code.writer; |
| 6249 | 6332 | try f.writeCValue(w, local, .Other); |
| 6250 | 6333 | try w.writeAll(" = "); |
| 6251 | 6334 | |
| ... | ... | @@ -6259,7 +6342,8 @@ fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6259 | 6342 | try f.writeCValueDerefMember(w, operand, .{ .identifier = "error" }) |
| 6260 | 6343 | else |
| 6261 | 6344 | try f.writeCValueMember(w, operand, .{ .identifier = "error" }); |
| 6262 | | try w.writeAll(";\n"); |
| 6345 | try w.writeByte(';'); |
| 6346 | try f.object.newline(); |
| 6263 | 6347 | return local; |
| 6264 | 6348 | } |
| 6265 | 6349 | |
| ... | ... | @@ -6274,7 +6358,7 @@ fn airUnwrapErrUnionPay(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValu |
| 6274 | 6358 | const operand_ty = f.typeOf(ty_op.operand); |
| 6275 | 6359 | const error_union_ty = if (is_ptr) operand_ty.childType(zcu) else operand_ty; |
| 6276 | 6360 | |
| 6277 | | const w = f.object.writer(); |
| 6361 | const w = &f.object.code.writer; |
| 6278 | 6362 | if (!error_union_ty.errorUnionPayload(zcu).hasRuntimeBits(zcu)) { |
| 6279 | 6363 | if (!is_ptr) return .none; |
| 6280 | 6364 | |
| ... | ... | @@ -6284,7 +6368,8 @@ fn airUnwrapErrUnionPay(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValu |
| 6284 | 6368 | try f.renderType(w, inst_ty); |
| 6285 | 6369 | try w.writeByte(')'); |
| 6286 | 6370 | try f.writeCValue(w, operand, .Other); |
| 6287 | | try w.writeAll(";\n"); |
| 6371 | try w.writeByte(';'); |
| 6372 | try f.object.newline(); |
| 6288 | 6373 | return local; |
| 6289 | 6374 | } |
| 6290 | 6375 | |
| ... | ... | @@ -6315,7 +6400,7 @@ fn airWrapOptional(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6315 | 6400 | .aggregate => |aggregate| switch (aggregate.fields.at(0, ctype_pool).name.index) { |
| 6316 | 6401 | .is_null, .payload => { |
| 6317 | 6402 | const operand_ctype = try f.ctypeFromType(f.typeOf(ty_op.operand), .complete); |
| 6318 | | const w = f.object.writer(); |
| 6403 | const w = &f.object.code.writer; |
| 6319 | 6404 | const local = try f.allocLocal(inst, inst_ty); |
| 6320 | 6405 | { |
| 6321 | 6406 | const a = try Assignment.start(f, w, .bool); |
| ... | ... | @@ -6351,7 +6436,7 @@ fn airWrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6351 | 6436 | const err = try f.resolveInst(ty_op.operand); |
| 6352 | 6437 | try reap(f, inst, &.{ty_op.operand}); |
| 6353 | 6438 | |
| 6354 | | const w = f.object.writer(); |
| 6439 | const w = &f.object.code.writer; |
| 6355 | 6440 | const local = try f.allocLocal(inst, inst_ty); |
| 6356 | 6441 | |
| 6357 | 6442 | if (repr_is_err and err == .local and err.local == local.new_local) { |
| ... | ... | @@ -6382,7 +6467,7 @@ fn airWrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6382 | 6467 | fn airErrUnionPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6383 | 6468 | const pt = f.object.dg.pt; |
| 6384 | 6469 | const zcu = pt.zcu; |
| 6385 | | const w = f.object.writer(); |
| 6470 | const w = &f.object.code.writer; |
| 6386 | 6471 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6387 | 6472 | const inst_ty = f.typeOfIndex(inst); |
| 6388 | 6473 | const operand = try f.resolveInst(ty_op.operand); |
| ... | ... | @@ -6451,7 +6536,7 @@ fn airWrapErrUnionPay(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6451 | 6536 | const err_ty = inst_ty.errorUnionSet(zcu); |
| 6452 | 6537 | try reap(f, inst, &.{ty_op.operand}); |
| 6453 | 6538 | |
| 6454 | | const w = f.object.writer(); |
| 6539 | const w = &f.object.code.writer; |
| 6455 | 6540 | const local = try f.allocLocal(inst, inst_ty); |
| 6456 | 6541 | if (!repr_is_err) { |
| 6457 | 6542 | const a = try Assignment.start(f, w, try f.ctypeFromType(payload_ty, .complete)); |
| ... | ... | @@ -6478,7 +6563,7 @@ fn airIsErr(f: *Function, inst: Air.Inst.Index, is_ptr: bool, operator: []const |
| 6478 | 6563 | const zcu = pt.zcu; |
| 6479 | 6564 | const un_op = f.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 6480 | 6565 | |
| 6481 | | const w = f.object.writer(); |
| 6566 | const w = &f.object.code.writer; |
| 6482 | 6567 | const operand = try f.resolveInst(un_op); |
| 6483 | 6568 | try reap(f, inst, &.{un_op}); |
| 6484 | 6569 | const operand_ty = f.typeOf(un_op); |
| ... | ... | @@ -6519,7 +6604,7 @@ fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6519 | 6604 | try reap(f, inst, &.{ty_op.operand}); |
| 6520 | 6605 | const inst_ty = f.typeOfIndex(inst); |
| 6521 | 6606 | const ptr_ty = inst_ty.slicePtrFieldType(zcu); |
| 6522 | | const w = f.object.writer(); |
| 6607 | const w = &f.object.code.writer; |
| 6523 | 6608 | const local = try f.allocLocal(inst, inst_ty); |
| 6524 | 6609 | const operand_ty = f.typeOf(ty_op.operand); |
| 6525 | 6610 | const array_ty = operand_ty.childType(zcu); |
| ... | ... | @@ -6584,7 +6669,7 @@ fn airFloatCast(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6584 | 6669 | else |
| 6585 | 6670 | unreachable; |
| 6586 | 6671 | |
| 6587 | | const w = f.object.writer(); |
| 6672 | const w = &f.object.code.writer; |
| 6588 | 6673 | const local = try f.allocLocal(inst, inst_ty); |
| 6589 | 6674 | const v = try Vectorize.start(f, inst, w, operand_ty); |
| 6590 | 6675 | const a = try Assignment.start(f, w, try f.ctypeFromType(scalar_ty, .complete)); |
| ... | ... | @@ -6634,7 +6719,7 @@ fn airUnBuiltinCall( |
| 6634 | 6719 | const inst_scalar_ctype = try f.ctypeFromType(inst_scalar_ty, .complete); |
| 6635 | 6720 | const ref_ret = inst_scalar_ctype.info(&f.object.dg.ctype_pool) == .array; |
| 6636 | 6721 | |
| 6637 | | const w = f.object.writer(); |
| 6722 | const w = &f.object.code.writer; |
| 6638 | 6723 | const local = try f.allocLocal(inst, inst_ty); |
| 6639 | 6724 | const v = try Vectorize.start(f, inst, w, operand_ty); |
| 6640 | 6725 | if (!ref_ret) { |
| ... | ... | @@ -6653,7 +6738,8 @@ fn airUnBuiltinCall( |
| 6653 | 6738 | try f.writeCValue(w, operand, .FunctionArgument); |
| 6654 | 6739 | try v.elem(f, w); |
| 6655 | 6740 | try f.object.dg.renderBuiltinInfo(w, scalar_ty, info); |
| 6656 | | try w.writeAll(");\n"); |
| 6741 | try w.writeAll(");"); |
| 6742 | try f.object.newline(); |
| 6657 | 6743 | try v.end(f, inst, w); |
| 6658 | 6744 | |
| 6659 | 6745 | return local; |
| ... | ... | @@ -6684,7 +6770,7 @@ fn airBinBuiltinCall( |
| 6684 | 6770 | const inst_scalar_ctype = try f.ctypeFromType(inst_scalar_ty, .complete); |
| 6685 | 6771 | const ref_ret = inst_scalar_ctype.info(&f.object.dg.ctype_pool) == .array; |
| 6686 | 6772 | |
| 6687 | | const w = f.object.writer(); |
| 6773 | const w = &f.object.code.writer; |
| 6688 | 6774 | const local = try f.allocLocal(inst, inst_ty); |
| 6689 | 6775 | if (is_big) try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 6690 | 6776 | const v = try Vectorize.start(f, inst, w, operand_ty); |
| ... | ... | @@ -6735,7 +6821,7 @@ fn airCmpBuiltinCall( |
| 6735 | 6821 | const inst_scalar_ctype = try f.ctypeFromType(inst_scalar_ty, .complete); |
| 6736 | 6822 | const ref_ret = inst_scalar_ctype.info(&f.object.dg.ctype_pool) == .array; |
| 6737 | 6823 | |
| 6738 | | const w = f.object.writer(); |
| 6824 | const w = &f.object.code.writer; |
| 6739 | 6825 | const local = try f.allocLocal(inst, inst_ty); |
| 6740 | 6826 | const v = try Vectorize.start(f, inst, w, operand_ty); |
| 6741 | 6827 | if (!ref_ret) { |
| ... | ... | @@ -6765,7 +6851,8 @@ fn airCmpBuiltinCall( |
| 6765 | 6851 | compareOperatorC(operator), |
| 6766 | 6852 | try f.fmtIntLiteralDec(try pt.intValue(.i32, 0)), |
| 6767 | 6853 | }); |
| 6768 | | try w.writeAll(";\n"); |
| 6854 | try w.writeByte(';'); |
| 6855 | try f.object.newline(); |
| 6769 | 6856 | try v.end(f, inst, w); |
| 6770 | 6857 | |
| 6771 | 6858 | return local; |
| ... | ... | @@ -6784,7 +6871,7 @@ fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue |
| 6784 | 6871 | const ty = ptr_ty.childType(zcu); |
| 6785 | 6872 | const ctype = try f.ctypeFromType(ty, .complete); |
| 6786 | 6873 | |
| 6787 | | const w = f.object.writer(); |
| 6874 | const w = &f.object.code.writer; |
| 6788 | 6875 | const new_value_mat = try Materialize.start(f, inst, ty, new_value); |
| 6789 | 6876 | try reap(f, inst, &.{ extra.ptr, extra.expected_value, extra.new_value }); |
| 6790 | 6877 | |
| ... | ... | @@ -6823,8 +6910,9 @@ fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue |
| 6823 | 6910 | try w.writeAll(", "); |
| 6824 | 6911 | try f.renderType(w, repr_ty); |
| 6825 | 6912 | try w.writeByte(')'); |
| 6826 | | try w.writeAll(") {\n"); |
| 6827 | | f.object.indent_writer.pushIndent(); |
| 6913 | try w.writeAll(") {"); |
| 6914 | f.object.indent(); |
| 6915 | try f.object.newline(); |
| 6828 | 6916 | { |
| 6829 | 6917 | const a = try Assignment.start(f, w, ctype); |
| 6830 | 6918 | try f.writeCValue(w, local, .Other); |
| ... | ... | @@ -6832,8 +6920,9 @@ fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue |
| 6832 | 6920 | try w.writeAll("NULL"); |
| 6833 | 6921 | try a.end(f, w); |
| 6834 | 6922 | } |
| 6835 | | f.object.indent_writer.popIndent(); |
| 6836 | | try w.writeAll("}\n"); |
| 6923 | try f.object.outdent(); |
| 6924 | try w.writeByte('}'); |
| 6925 | try f.object.newline(); |
| 6837 | 6926 | } else { |
| 6838 | 6927 | { |
| 6839 | 6928 | const a = try Assignment.start(f, w, ctype); |
| ... | ... | @@ -6889,7 +6978,7 @@ fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6889 | 6978 | const ptr = try f.resolveInst(pl_op.operand); |
| 6890 | 6979 | const operand = try f.resolveInst(extra.operand); |
| 6891 | 6980 | |
| 6892 | | const w = f.object.writer(); |
| 6981 | const w = &f.object.code.writer; |
| 6893 | 6982 | const operand_mat = try Materialize.start(f, inst, ty, operand); |
| 6894 | 6983 | try reap(f, inst, &.{ pl_op.operand, extra.operand }); |
| 6895 | 6984 | |
| ... | ... | @@ -6923,7 +7012,8 @@ fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6923 | 7012 | try f.object.dg.renderTypeForBuiltinFnName(w, ty); |
| 6924 | 7013 | try w.writeAll(", "); |
| 6925 | 7014 | try f.renderType(w, repr_ty); |
| 6926 | | try w.writeAll(");\n"); |
| 7015 | try w.writeAll(");"); |
| 7016 | try f.object.newline(); |
| 6927 | 7017 | try operand_mat.end(f, inst); |
| 6928 | 7018 | |
| 6929 | 7019 | if (f.liveness.isUnused(inst)) { |
| ... | ... | @@ -6949,7 +7039,7 @@ fn airAtomicLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6949 | 7039 | ty; |
| 6950 | 7040 | |
| 6951 | 7041 | const inst_ty = f.typeOfIndex(inst); |
| 6952 | | const w = f.object.writer(); |
| 7042 | const w = &f.object.code.writer; |
| 6953 | 7043 | const local = try f.allocLocal(inst, inst_ty); |
| 6954 | 7044 | |
| 6955 | 7045 | try w.writeAll("zig_atomic_load("); |
| ... | ... | @@ -6966,7 +7056,8 @@ fn airAtomicLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6966 | 7056 | try f.object.dg.renderTypeForBuiltinFnName(w, ty); |
| 6967 | 7057 | try w.writeAll(", "); |
| 6968 | 7058 | try f.renderType(w, repr_ty); |
| 6969 | | try w.writeAll(");\n"); |
| 7059 | try w.writeAll(");"); |
| 7060 | try f.object.newline(); |
| 6970 | 7061 | |
| 6971 | 7062 | return local; |
| 6972 | 7063 | } |
| ... | ... | @@ -6980,7 +7071,7 @@ fn airAtomicStore(f: *Function, inst: Air.Inst.Index, order: [*:0]const u8) !CVa |
| 6980 | 7071 | const ptr = try f.resolveInst(bin_op.lhs); |
| 6981 | 7072 | const element = try f.resolveInst(bin_op.rhs); |
| 6982 | 7073 | |
| 6983 | | const w = f.object.writer(); |
| 7074 | const w = &f.object.code.writer; |
| 6984 | 7075 | const element_mat = try Materialize.start(f, inst, ty, element); |
| 6985 | 7076 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 6986 | 7077 | |
| ... | ... | @@ -7001,7 +7092,8 @@ fn airAtomicStore(f: *Function, inst: Air.Inst.Index, order: [*:0]const u8) !CVa |
| 7001 | 7092 | try f.object.dg.renderTypeForBuiltinFnName(w, ty); |
| 7002 | 7093 | try w.writeAll(", "); |
| 7003 | 7094 | try f.renderType(w, repr_ty); |
| 7004 | | try w.writeAll(");\n"); |
| 7095 | try w.writeAll(");"); |
| 7096 | try f.object.newline(); |
| 7005 | 7097 | try element_mat.end(f, inst); |
| 7006 | 7098 | |
| 7007 | 7099 | return .none; |
| ... | ... | @@ -7027,7 +7119,7 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 7027 | 7119 | const elem_ty = f.typeOf(bin_op.rhs); |
| 7028 | 7120 | const elem_abi_size = elem_ty.abiSize(zcu); |
| 7029 | 7121 | const val_is_undef = if (try f.air.value(bin_op.rhs, pt)) |val| val.isUndefDeep(zcu) else false; |
| 7030 | | const w = f.object.writer(); |
| 7122 | const w = &f.object.code.writer; |
| 7031 | 7123 | |
| 7032 | 7124 | if (val_is_undef) { |
| 7033 | 7125 | if (!safety) { |
| ... | ... | @@ -7042,17 +7134,18 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 7042 | 7134 | try w.writeAll(", 0xaa, "); |
| 7043 | 7135 | try f.writeCValueMember(w, dest_slice, .{ .identifier = "len" }); |
| 7044 | 7136 | if (elem_abi_size > 1) { |
| 7045 | | try w.print(" * {d});\n", .{elem_abi_size}); |
| 7046 | | } else { |
| 7047 | | try w.writeAll(");\n"); |
| 7137 | try w.print(" * {d}", .{elem_abi_size}); |
| 7048 | 7138 | } |
| 7139 | try w.writeAll(");"); |
| 7140 | try f.object.newline(); |
| 7049 | 7141 | }, |
| 7050 | 7142 | .one => { |
| 7051 | 7143 | const array_ty = dest_ty.childType(zcu); |
| 7052 | 7144 | const len = array_ty.arrayLen(zcu) * elem_abi_size; |
| 7053 | 7145 | |
| 7054 | 7146 | try f.writeCValue(w, dest_slice, .FunctionArgument); |
| 7055 | | try w.print(", 0xaa, {d});\n", .{len}); |
| 7147 | try w.print(", 0xaa, {d});", .{len}); |
| 7148 | try f.object.newline(); |
| 7056 | 7149 | }, |
| 7057 | 7150 | .many, .c => unreachable, |
| 7058 | 7151 | } |
| ... | ... | @@ -7122,7 +7215,8 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 7122 | 7215 | try f.writeCValue(w, bitcasted, .FunctionArgument); |
| 7123 | 7216 | try w.writeAll(", "); |
| 7124 | 7217 | try f.writeCValueMember(w, dest_slice, .{ .identifier = "len" }); |
| 7125 | | try w.writeAll(");\n"); |
| 7218 | try w.writeAll(");"); |
| 7219 | try f.object.newline(); |
| 7126 | 7220 | }, |
| 7127 | 7221 | .one => { |
| 7128 | 7222 | const array_ty = dest_ty.childType(zcu); |
| ... | ... | @@ -7131,7 +7225,8 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 7131 | 7225 | try f.writeCValue(w, dest_slice, .FunctionArgument); |
| 7132 | 7226 | try w.writeAll(", "); |
| 7133 | 7227 | try f.writeCValue(w, bitcasted, .FunctionArgument); |
| 7134 | | try w.print(", {d});\n", .{len}); |
| 7228 | try w.print(", {d});", .{len}); |
| 7229 | try f.object.newline(); |
| 7135 | 7230 | }, |
| 7136 | 7231 | .many, .c => unreachable, |
| 7137 | 7232 | } |
| ... | ... | @@ -7148,11 +7243,11 @@ fn airMemcpy(f: *Function, inst: Air.Inst.Index, function_paren: []const u8) !CV |
| 7148 | 7243 | const src_ptr = try f.resolveInst(bin_op.rhs); |
| 7149 | 7244 | const dest_ty = f.typeOf(bin_op.lhs); |
| 7150 | 7245 | const src_ty = f.typeOf(bin_op.rhs); |
| 7151 | | const w = f.object.writer(); |
| 7246 | const w = &f.object.code.writer; |
| 7152 | 7247 | |
| 7153 | 7248 | if (dest_ty.ptrSize(zcu) != .one) { |
| 7154 | 7249 | try w.writeAll("if ("); |
| 7155 | | try writeArrayLen(f, w, dest_ptr, dest_ty); |
| 7250 | try writeArrayLen(f, dest_ptr, dest_ty); |
| 7156 | 7251 | try w.writeAll(" != 0) "); |
| 7157 | 7252 | } |
| 7158 | 7253 | try w.writeAll(function_paren); |
| ... | ... | @@ -7160,24 +7255,26 @@ fn airMemcpy(f: *Function, inst: Air.Inst.Index, function_paren: []const u8) !CV |
| 7160 | 7255 | try w.writeAll(", "); |
| 7161 | 7256 | try writeSliceOrPtr(f, w, src_ptr, src_ty); |
| 7162 | 7257 | try w.writeAll(", "); |
| 7163 | | try writeArrayLen(f, w, dest_ptr, dest_ty); |
| 7258 | try writeArrayLen(f, dest_ptr, dest_ty); |
| 7164 | 7259 | try w.writeAll(" * sizeof("); |
| 7165 | 7260 | try f.renderType(w, dest_ty.elemType2(zcu)); |
| 7166 | | try w.writeAll("));\n"); |
| 7261 | try w.writeAll("));"); |
| 7262 | try f.object.newline(); |
| 7167 | 7263 | |
| 7168 | 7264 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 7169 | 7265 | return .none; |
| 7170 | 7266 | } |
| 7171 | 7267 | |
| 7172 | | fn writeArrayLen(f: *Function, alw: ArrayListWriter, dest_ptr: CValue, dest_ty: Type) !void { |
| 7268 | fn writeArrayLen(f: *Function, dest_ptr: CValue, dest_ty: Type) !void { |
| 7173 | 7269 | const pt = f.object.dg.pt; |
| 7174 | 7270 | const zcu = pt.zcu; |
| 7271 | const w = &f.object.code.writer; |
| 7175 | 7272 | switch (dest_ty.ptrSize(zcu)) { |
| 7176 | | .one => try alw.print("{f}", .{ |
| 7273 | .one => try w.print("{f}", .{ |
| 7177 | 7274 | try f.fmtIntLiteralDec(try pt.intValue(.usize, dest_ty.childType(zcu).arrayLen(zcu))), |
| 7178 | 7275 | }), |
| 7179 | 7276 | .many, .c => unreachable, |
| 7180 | | .slice => try f.writeCValueMember(alw, dest_ptr, .{ .identifier = "len" }), |
| 7277 | .slice => try f.writeCValueMember(w, dest_ptr, .{ .identifier = "len" }), |
| 7181 | 7278 | } |
| 7182 | 7279 | } |
| 7183 | 7280 | |
| ... | ... | @@ -7194,7 +7291,7 @@ fn airSetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7194 | 7291 | if (layout.tag_size == 0) return .none; |
| 7195 | 7292 | const tag_ty = union_ty.unionTagTypeSafety(zcu).?; |
| 7196 | 7293 | |
| 7197 | | const w = f.object.writer(); |
| 7294 | const w = &f.object.code.writer; |
| 7198 | 7295 | const a = try Assignment.start(f, w, try f.ctypeFromType(tag_ty, .complete)); |
| 7199 | 7296 | try f.writeCValueDerefMember(w, union_ptr, .{ .identifier = "tag" }); |
| 7200 | 7297 | try a.assign(f, w); |
| ... | ... | @@ -7216,7 +7313,7 @@ fn airGetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7216 | 7313 | if (layout.tag_size == 0) return .none; |
| 7217 | 7314 | |
| 7218 | 7315 | const inst_ty = f.typeOfIndex(inst); |
| 7219 | | const w = f.object.writer(); |
| 7316 | const w = &f.object.code.writer; |
| 7220 | 7317 | const local = try f.allocLocal(inst, inst_ty); |
| 7221 | 7318 | const a = try Assignment.start(f, w, try f.ctypeFromType(inst_ty, .complete)); |
| 7222 | 7319 | try f.writeCValue(w, local, .Other); |
| ... | ... | @@ -7234,14 +7331,15 @@ fn airTagName(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7234 | 7331 | const operand = try f.resolveInst(un_op); |
| 7235 | 7332 | try reap(f, inst, &.{un_op}); |
| 7236 | 7333 | |
| 7237 | | const w = f.object.writer(); |
| 7334 | const w = &f.object.code.writer; |
| 7238 | 7335 | const local = try f.allocLocal(inst, inst_ty); |
| 7239 | 7336 | try f.writeCValue(w, local, .Other); |
| 7240 | 7337 | try w.print(" = {s}(", .{ |
| 7241 | 7338 | try f.getLazyFnName(.{ .tag_name = enum_ty.toIntern() }), |
| 7242 | 7339 | }); |
| 7243 | 7340 | try f.writeCValue(w, operand, .Other); |
| 7244 | | try w.writeAll(");\n"); |
| 7341 | try w.writeAll(");"); |
| 7342 | try f.object.newline(); |
| 7245 | 7343 | |
| 7246 | 7344 | return local; |
| 7247 | 7345 | } |
| ... | ... | @@ -7249,7 +7347,7 @@ fn airTagName(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7249 | 7347 | fn airErrorName(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7250 | 7348 | const un_op = f.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 7251 | 7349 | |
| 7252 | | const w = f.object.writer(); |
| 7350 | const w = &f.object.code.writer; |
| 7253 | 7351 | const inst_ty = f.typeOfIndex(inst); |
| 7254 | 7352 | const operand = try f.resolveInst(un_op); |
| 7255 | 7353 | try reap(f, inst, &.{un_op}); |
| ... | ... | @@ -7258,7 +7356,8 @@ fn airErrorName(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7258 | 7356 | |
| 7259 | 7357 | try w.writeAll(" = zig_errorName["); |
| 7260 | 7358 | try f.writeCValue(w, operand, .Other); |
| 7261 | | try w.writeAll(" - 1];\n"); |
| 7359 | try w.writeAll(" - 1];"); |
| 7360 | try f.object.newline(); |
| 7262 | 7361 | return local; |
| 7263 | 7362 | } |
| 7264 | 7363 | |
| ... | ... | @@ -7273,7 +7372,7 @@ fn airSplat(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7273 | 7372 | const inst_ty = f.typeOfIndex(inst); |
| 7274 | 7373 | const inst_scalar_ty = inst_ty.scalarType(zcu); |
| 7275 | 7374 | |
| 7276 | | const w = f.object.writer(); |
| 7375 | const w = &f.object.code.writer; |
| 7277 | 7376 | const local = try f.allocLocal(inst, inst_ty); |
| 7278 | 7377 | const v = try Vectorize.start(f, inst, w, inst_ty); |
| 7279 | 7378 | const a = try Assignment.start(f, w, try f.ctypeFromType(inst_scalar_ty, .complete)); |
| ... | ... | @@ -7298,7 +7397,7 @@ fn airSelect(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7298 | 7397 | |
| 7299 | 7398 | const inst_ty = f.typeOfIndex(inst); |
| 7300 | 7399 | |
| 7301 | | const w = f.object.writer(); |
| 7400 | const w = &f.object.code.writer; |
| 7302 | 7401 | const local = try f.allocLocal(inst, inst_ty); |
| 7303 | 7402 | const v = try Vectorize.start(f, inst, w, inst_ty); |
| 7304 | 7403 | try f.writeCValue(w, local, .Other); |
| ... | ... | @@ -7312,7 +7411,8 @@ fn airSelect(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7312 | 7411 | try w.writeAll(" : "); |
| 7313 | 7412 | try f.writeCValue(w, rhs, .Other); |
| 7314 | 7413 | try v.elem(f, w); |
| 7315 | | try w.writeAll(";\n"); |
| 7414 | try w.writeByte(';'); |
| 7415 | try f.object.newline(); |
| 7316 | 7416 | try v.end(f, inst, w); |
| 7317 | 7417 | |
| 7318 | 7418 | return local; |
| ... | ... | @@ -7327,7 +7427,7 @@ fn airShuffleOne(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7327 | 7427 | const operand = try f.resolveInst(unwrapped.operand); |
| 7328 | 7428 | const inst_ty = unwrapped.result_ty; |
| 7329 | 7429 | |
| 7330 | | const w = f.object.writer(); |
| 7430 | const w = &f.object.code.writer; |
| 7331 | 7431 | const local = try f.allocLocal(inst, inst_ty); |
| 7332 | 7432 | try reap(f, inst, &.{unwrapped.operand}); // local cannot alias operand |
| 7333 | 7433 | for (mask, 0..) |mask_elem, out_idx| { |
| ... | ... | @@ -7361,7 +7461,7 @@ fn airShuffleTwo(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7361 | 7461 | const inst_ty = unwrapped.result_ty; |
| 7362 | 7462 | const elem_ty = inst_ty.childType(zcu); |
| 7363 | 7463 | |
| 7364 | | const w = f.object.writer(); |
| 7464 | const w = &f.object.code.writer; |
| 7365 | 7465 | const local = try f.allocLocal(inst, inst_ty); |
| 7366 | 7466 | try reap(f, inst, &.{ unwrapped.operand_a, unwrapped.operand_b }); // local cannot alias operands |
| 7367 | 7467 | for (mask, 0..) |mask_elem, out_idx| { |
| ... | ... | @@ -7384,7 +7484,8 @@ fn airShuffleTwo(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7384 | 7484 | }, |
| 7385 | 7485 | .undef => try f.object.dg.renderUndefValue(w, elem_ty, .Other), |
| 7386 | 7486 | } |
| 7387 | | try w.writeAll(";\n"); |
| 7487 | try w.writeByte(';'); |
| 7488 | try f.object.newline(); |
| 7388 | 7489 | } |
| 7389 | 7490 | |
| 7390 | 7491 | return local; |
| ... | ... | @@ -7399,7 +7500,7 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7399 | 7500 | const operand = try f.resolveInst(reduce.operand); |
| 7400 | 7501 | try reap(f, inst, &.{reduce.operand}); |
| 7401 | 7502 | const operand_ty = f.typeOf(reduce.operand); |
| 7402 | | const w = f.object.writer(); |
| 7503 | const w = &f.object.code.writer; |
| 7403 | 7504 | |
| 7404 | 7505 | const use_operator = scalar_ty.bitSize(zcu) <= 64; |
| 7405 | 7506 | const op: union(enum) { |
| ... | ... | @@ -7486,7 +7587,8 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7486 | 7587 | else => unreachable, |
| 7487 | 7588 | }, |
| 7488 | 7589 | }, .Other); |
| 7489 | | try w.writeAll(";\n"); |
| 7590 | try w.writeByte(';'); |
| 7591 | try f.object.newline(); |
| 7490 | 7592 | |
| 7491 | 7593 | const v = try Vectorize.start(f, inst, w, operand_ty); |
| 7492 | 7594 | try f.writeCValue(w, accum, .Other); |
| ... | ... | @@ -7520,7 +7622,8 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7520 | 7622 | try v.elem(f, w); |
| 7521 | 7623 | }, |
| 7522 | 7624 | } |
| 7523 | | try w.writeAll(";\n"); |
| 7625 | try w.writeByte(';'); |
| 7626 | try f.object.newline(); |
| 7524 | 7627 | try v.end(f, inst, w); |
| 7525 | 7628 | |
| 7526 | 7629 | return accum; |
| ... | ... | @@ -7547,7 +7650,7 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7547 | 7650 | } |
| 7548 | 7651 | } |
| 7549 | 7652 | |
| 7550 | | const w = f.object.writer(); |
| 7653 | const w = &f.object.code.writer; |
| 7551 | 7654 | const local = try f.allocLocal(inst, inst_ty); |
| 7552 | 7655 | switch (ip.indexToKey(inst_ty.toIntern())) { |
| 7553 | 7656 | inline .array_type, .vector_type => |info, tag| { |
| ... | ... | @@ -7670,7 +7773,8 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7670 | 7773 | bit_offset += field_ty.bitSize(zcu); |
| 7671 | 7774 | empty = false; |
| 7672 | 7775 | } |
| 7673 | | try w.writeAll(";\n"); |
| 7776 | try w.writeByte(';'); |
| 7777 | try f.object.newline(); |
| 7674 | 7778 | }, |
| 7675 | 7779 | } |
| 7676 | 7780 | }, |
| ... | ... | @@ -7705,7 +7809,7 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7705 | 7809 | const payload = try f.resolveInst(extra.init); |
| 7706 | 7810 | try reap(f, inst, &.{extra.init}); |
| 7707 | 7811 | |
| 7708 | | const w = f.object.writer(); |
| 7812 | const w = &f.object.code.writer; |
| 7709 | 7813 | const local = try f.allocLocal(inst, union_ty); |
| 7710 | 7814 | if (loaded_union.flagsUnordered(ip).layout == .@"packed") return f.moveCValue(inst, union_ty, payload); |
| 7711 | 7815 | |
| ... | ... | @@ -7741,7 +7845,7 @@ fn airPrefetch(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7741 | 7845 | const ptr = try f.resolveInst(prefetch.ptr); |
| 7742 | 7846 | try reap(f, inst, &.{prefetch.ptr}); |
| 7743 | 7847 | |
| 7744 | | const w = f.object.writer(); |
| 7848 | const w = &f.object.code.writer; |
| 7745 | 7849 | switch (prefetch.cache) { |
| 7746 | 7850 | .data => { |
| 7747 | 7851 | try w.writeAll("zig_prefetch("); |
| ... | ... | @@ -7749,7 +7853,8 @@ fn airPrefetch(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7749 | 7853 | try f.writeCValueMember(w, ptr, .{ .identifier = "ptr" }) |
| 7750 | 7854 | else |
| 7751 | 7855 | try f.writeCValue(w, ptr, .FunctionArgument); |
| 7752 | | try w.print(", {d}, {d});\n", .{ @intFromEnum(prefetch.rw), prefetch.locality }); |
| 7856 | try w.print(", {d}, {d});", .{ @intFromEnum(prefetch.rw), prefetch.locality }); |
| 7857 | try f.object.newline(); |
| 7753 | 7858 | }, |
| 7754 | 7859 | // The available prefetch intrinsics do not accept a cache argument; only |
| 7755 | 7860 | // address, rw, and locality. |
| ... | ... | @@ -7762,13 +7867,14 @@ fn airPrefetch(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7762 | 7867 | fn airWasmMemorySize(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7763 | 7868 | const pl_op = f.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 7764 | 7869 | |
| 7765 | | const w = f.object.writer(); |
| 7870 | const w = &f.object.code.writer; |
| 7766 | 7871 | const inst_ty = f.typeOfIndex(inst); |
| 7767 | 7872 | const local = try f.allocLocal(inst, inst_ty); |
| 7768 | 7873 | try f.writeCValue(w, local, .Other); |
| 7769 | 7874 | |
| 7770 | 7875 | try w.writeAll(" = "); |
| 7771 | | try w.print("zig_wasm_memory_size({d});\n", .{pl_op.payload}); |
| 7876 | try w.print("zig_wasm_memory_size({d});", .{pl_op.payload}); |
| 7877 | try f.object.newline(); |
| 7772 | 7878 | |
| 7773 | 7879 | return local; |
| 7774 | 7880 | } |
| ... | ... | @@ -7776,7 +7882,7 @@ fn airWasmMemorySize(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7776 | 7882 | fn airWasmMemoryGrow(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7777 | 7883 | const pl_op = f.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 7778 | 7884 | |
| 7779 | | const w = f.object.writer(); |
| 7885 | const w = &f.object.code.writer; |
| 7780 | 7886 | const inst_ty = f.typeOfIndex(inst); |
| 7781 | 7887 | const operand = try f.resolveInst(pl_op.operand); |
| 7782 | 7888 | try reap(f, inst, &.{pl_op.operand}); |
| ... | ... | @@ -7786,7 +7892,8 @@ fn airWasmMemoryGrow(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7786 | 7892 | try w.writeAll(" = "); |
| 7787 | 7893 | try w.print("zig_wasm_memory_grow({d}, ", .{pl_op.payload}); |
| 7788 | 7894 | try f.writeCValue(w, operand, .FunctionArgument); |
| 7789 | | try w.writeAll(");\n"); |
| 7895 | try w.writeAll(");"); |
| 7896 | try f.object.newline(); |
| 7790 | 7897 | return local; |
| 7791 | 7898 | } |
| 7792 | 7899 | |
| ... | ... | @@ -7804,7 +7911,7 @@ fn airMulAdd(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7804 | 7911 | const inst_ty = f.typeOfIndex(inst); |
| 7805 | 7912 | const inst_scalar_ty = inst_ty.scalarType(zcu); |
| 7806 | 7913 | |
| 7807 | | const w = f.object.writer(); |
| 7914 | const w = &f.object.code.writer; |
| 7808 | 7915 | const local = try f.allocLocal(inst, inst_ty); |
| 7809 | 7916 | const v = try Vectorize.start(f, inst, w, inst_ty); |
| 7810 | 7917 | try f.writeCValue(w, local, .Other); |
| ... | ... | @@ -7820,7 +7927,8 @@ fn airMulAdd(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7820 | 7927 | try w.writeAll(", "); |
| 7821 | 7928 | try f.writeCValue(w, addend, .FunctionArgument); |
| 7822 | 7929 | try v.elem(f, w); |
| 7823 | | try w.writeAll(");\n"); |
| 7930 | try w.writeAll(");"); |
| 7931 | try f.object.newline(); |
| 7824 | 7932 | try v.end(f, inst, w); |
| 7825 | 7933 | |
| 7826 | 7934 | return local; |
| ... | ... | @@ -7828,12 +7936,13 @@ fn airMulAdd(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7828 | 7936 | |
| 7829 | 7937 | fn airRuntimeNavPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7830 | 7938 | const ty_nav = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_nav; |
| 7831 | | const w = f.object.writer(); |
| 7939 | const w = &f.object.code.writer; |
| 7832 | 7940 | const local = try f.allocLocal(inst, .fromInterned(ty_nav.ty)); |
| 7833 | 7941 | try f.writeCValue(w, local, .Other); |
| 7834 | 7942 | try w.writeAll(" = "); |
| 7835 | 7943 | try f.object.dg.renderNav(w, ty_nav.nav, .Other); |
| 7836 | | try w.writeAll(";\n"); |
| 7944 | try w.writeByte(';'); |
| 7945 | try f.object.newline(); |
| 7837 | 7946 | return local; |
| 7838 | 7947 | } |
| 7839 | 7948 | |
| ... | ... | @@ -7845,7 +7954,7 @@ fn airCVaStart(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7845 | 7954 | const function_info = (try f.ctypeFromType(function_ty, .complete)).info(&f.object.dg.ctype_pool).function; |
| 7846 | 7955 | assert(function_info.varargs); |
| 7847 | 7956 | |
| 7848 | | const w = f.object.writer(); |
| 7957 | const w = &f.object.code.writer; |
| 7849 | 7958 | const local = try f.allocLocal(inst, inst_ty); |
| 7850 | 7959 | try w.writeAll("va_start(*(va_list *)&"); |
| 7851 | 7960 | try f.writeCValue(w, local, .Other); |
| ... | ... | @@ -7853,7 +7962,8 @@ fn airCVaStart(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7853 | 7962 | try w.writeAll(", "); |
| 7854 | 7963 | try f.writeCValue(w, .{ .arg = function_info.param_ctypes.len - 1 }, .FunctionArgument); |
| 7855 | 7964 | } |
| 7856 | | try w.writeAll(");\n"); |
| 7965 | try w.writeAll(");"); |
| 7966 | try f.object.newline(); |
| 7857 | 7967 | return local; |
| 7858 | 7968 | } |
| 7859 | 7969 | |
| ... | ... | @@ -7864,14 +7974,15 @@ fn airCVaArg(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7864 | 7974 | const va_list = try f.resolveInst(ty_op.operand); |
| 7865 | 7975 | try reap(f, inst, &.{ty_op.operand}); |
| 7866 | 7976 | |
| 7867 | | const w = f.object.writer(); |
| 7977 | const w = &f.object.code.writer; |
| 7868 | 7978 | const local = try f.allocLocal(inst, inst_ty); |
| 7869 | 7979 | try f.writeCValue(w, local, .Other); |
| 7870 | 7980 | try w.writeAll(" = va_arg(*(va_list *)"); |
| 7871 | 7981 | try f.writeCValue(w, va_list, .Other); |
| 7872 | 7982 | try w.writeAll(", "); |
| 7873 | 7983 | try f.renderType(w, ty_op.ty.toType()); |
| 7874 | | try w.writeAll(");\n"); |
| 7984 | try w.writeAll(");"); |
| 7985 | try f.object.newline(); |
| 7875 | 7986 | return local; |
| 7876 | 7987 | } |
| 7877 | 7988 | |
| ... | ... | @@ -7881,10 +7992,11 @@ fn airCVaEnd(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7881 | 7992 | const va_list = try f.resolveInst(un_op); |
| 7882 | 7993 | try reap(f, inst, &.{un_op}); |
| 7883 | 7994 | |
| 7884 | | const w = f.object.writer(); |
| 7995 | const w = &f.object.code.writer; |
| 7885 | 7996 | try w.writeAll("va_end(*(va_list *)"); |
| 7886 | 7997 | try f.writeCValue(w, va_list, .Other); |
| 7887 | | try w.writeAll(");\n"); |
| 7998 | try w.writeAll(");"); |
| 7999 | try f.object.newline(); |
| 7888 | 8000 | return .none; |
| 7889 | 8001 | } |
| 7890 | 8002 | |
| ... | ... | @@ -7895,13 +8007,14 @@ fn airCVaCopy(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7895 | 8007 | const va_list = try f.resolveInst(ty_op.operand); |
| 7896 | 8008 | try reap(f, inst, &.{ty_op.operand}); |
| 7897 | 8009 | |
| 7898 | | const w = f.object.writer(); |
| 8010 | const w = &f.object.code.writer; |
| 7899 | 8011 | const local = try f.allocLocal(inst, inst_ty); |
| 7900 | 8012 | try w.writeAll("va_copy(*(va_list *)&"); |
| 7901 | 8013 | try f.writeCValue(w, local, .Other); |
| 7902 | 8014 | try w.writeAll(", *(va_list *)"); |
| 7903 | 8015 | try f.writeCValue(w, va_list, .Other); |
| 7904 | | try w.writeAll(");\n"); |
| 8016 | try w.writeAll(");"); |
| 8017 | try f.object.newline(); |
| 7905 | 8018 | return local; |
| 7906 | 8019 | } |
| 7907 | 8020 | |
| ... | ... | @@ -8003,93 +8116,6 @@ fn toAtomicRmwSuffix(order: std.builtin.AtomicRmwOp) []const u8 { |
| 8003 | 8116 | }; |
| 8004 | 8117 | } |
| 8005 | 8118 | |
| 8006 | | const ArrayListWriter = ErrorOnlyGenericWriter(std.ArrayList(u8).Writer.Error); |
| 8007 | | |
| 8008 | | fn arrayListWriter(list: *std.ArrayList(u8)) ArrayListWriter { |
| 8009 | | return .{ .context = .{ |
| 8010 | | .context = list, |
| 8011 | | .writeFn = struct { |
| 8012 | | fn write(context: *const anyopaque, bytes: []const u8) anyerror!usize { |
| 8013 | | const l: *std.ArrayList(u8) = @alignCast(@constCast(@ptrCast(context))); |
| 8014 | | return l.writer().write(bytes); |
| 8015 | | } |
| 8016 | | }.write, |
| 8017 | | } }; |
| 8018 | | } |
| 8019 | | |
| 8020 | | fn IndentWriter(comptime UnderlyingWriter: type) type { |
| 8021 | | return struct { |
| 8022 | | const Self = @This(); |
| 8023 | | pub const Error = UnderlyingWriter.Error; |
| 8024 | | pub const Writer = ErrorOnlyGenericWriter(Error); |
| 8025 | | |
| 8026 | | pub const indent_delta = 1; |
| 8027 | | |
| 8028 | | underlying_writer: UnderlyingWriter, |
| 8029 | | indent_count: usize = 0, |
| 8030 | | current_line_empty: bool = true, |
| 8031 | | |
| 8032 | | pub fn w(self: *Self) Writer { |
| 8033 | | return .{ .context = .{ |
| 8034 | | .context = self, |
| 8035 | | .writeFn = writeAny, |
| 8036 | | } }; |
| 8037 | | } |
| 8038 | | |
| 8039 | | pub fn write(self: *Self, bytes: []const u8) Error!usize { |
| 8040 | | if (bytes.len == 0) return 0; |
| 8041 | | |
| 8042 | | const current_indent = self.indent_count * Self.indent_delta; |
| 8043 | | if (self.current_line_empty and current_indent > 0) { |
| 8044 | | try self.underlying_writer.writeByteNTimes(' ', current_indent); |
| 8045 | | } |
| 8046 | | self.current_line_empty = false; |
| 8047 | | |
| 8048 | | return self.writeNoIndent(bytes); |
| 8049 | | } |
| 8050 | | |
| 8051 | | fn writeAny(context: *const anyopaque, bytes: []const u8) anyerror!usize { |
| 8052 | | const self: *Self = @alignCast(@constCast(@ptrCast(context))); |
| 8053 | | return self.write(bytes); |
| 8054 | | } |
| 8055 | | |
| 8056 | | pub fn insertNewline(self: *Self) Error!void { |
| 8057 | | _ = try self.writeNoIndent("\n"); |
| 8058 | | } |
| 8059 | | |
| 8060 | | pub fn pushIndent(self: *Self) void { |
| 8061 | | self.indent_count += 1; |
| 8062 | | } |
| 8063 | | |
| 8064 | | pub fn popIndent(self: *Self) void { |
| 8065 | | assert(self.indent_count != 0); |
| 8066 | | self.indent_count -= 1; |
| 8067 | | } |
| 8068 | | |
| 8069 | | fn writeNoIndent(self: *Self, bytes: []const u8) Error!usize { |
| 8070 | | if (bytes.len == 0) return 0; |
| 8071 | | |
| 8072 | | try self.underlying_writer.writeAll(bytes); |
| 8073 | | if (bytes[bytes.len - 1] == '\n') { |
| 8074 | | self.current_line_empty = true; |
| 8075 | | } |
| 8076 | | return bytes.len; |
| 8077 | | } |
| 8078 | | }; |
| 8079 | | } |
| 8080 | | |
| 8081 | | /// A wrapper around `std.io.AnyWriter` that maintains a generic error set while |
| 8082 | | /// erasing the rest of the implementation. This is intended to avoid duplicate |
| 8083 | | /// generic instantiations for w types which share the same error set, while |
| 8084 | | /// maintaining ease of error handling. |
| 8085 | | fn ErrorOnlyGenericWriter(comptime Error: type) type { |
| 8086 | | return std.io.GenericWriter(std.io.AnyWriter, Error, struct { |
| 8087 | | fn write(context: std.io.AnyWriter, bytes: []const u8) Error!usize { |
| 8088 | | return @errorCast(context.write(bytes)); |
| 8089 | | } |
| 8090 | | }.write); |
| 8091 | | } |
| 8092 | | |
| 8093 | 8119 | fn toCIntBits(zig_bits: u32) ?u32 { |
| 8094 | 8120 | for (&[_]u8{ 8, 16, 32, 64, 128 }) |c_bits| { |
| 8095 | 8121 | if (zig_bits <= c_bits) { |
| ... | ... | @@ -8148,7 +8174,7 @@ const StringLiteral = struct { |
| 8148 | 8174 | len: usize, |
| 8149 | 8175 | cur_len: usize, |
| 8150 | 8176 | start_count: usize, |
| 8151 | | writer: *std.io.Writer, |
| 8177 | w: *Writer, |
| 8152 | 8178 | |
| 8153 | 8179 | // MSVC throws C2078 if an array of size 65536 or greater is initialized with a string literal, |
| 8154 | 8180 | // regardless of the length of the string literal initializing it. Array initializer syntax is |
| ... | ... | @@ -8161,63 +8187,63 @@ const StringLiteral = struct { |
| 8161 | 8187 | const max_char_len = 4; |
| 8162 | 8188 | const max_literal_len = @min(16380 - max_char_len, 4095); |
| 8163 | 8189 | |
| 8164 | | fn init(w: *std.io.Writer, len: usize) StringLiteral { |
| 8190 | fn init(w: *Writer, len: usize) StringLiteral { |
| 8165 | 8191 | return .{ |
| 8166 | 8192 | .cur_len = 0, |
| 8167 | 8193 | .len = len, |
| 8168 | 8194 | .start_count = w.count, |
| 8169 | | .writer = w, |
| 8195 | .w = w, |
| 8170 | 8196 | }; |
| 8171 | 8197 | } |
| 8172 | 8198 | |
| 8173 | | pub fn start(sl: *StringLiteral) std.io.Writer.Error!void { |
| 8199 | pub fn start(sl: *StringLiteral) Writer.Error!void { |
| 8174 | 8200 | if (sl.len <= max_string_initializer_len) { |
| 8175 | | try sl.writer.writeByte('\"'); |
| 8201 | try sl.w.writeByte('\"'); |
| 8176 | 8202 | } else { |
| 8177 | | try sl.writer.writeByte('{'); |
| 8203 | try sl.w.writeByte('{'); |
| 8178 | 8204 | } |
| 8179 | 8205 | } |
| 8180 | 8206 | |
| 8181 | | pub fn end(sl: *StringLiteral) std.io.Writer.Error!void { |
| 8207 | pub fn end(sl: *StringLiteral) Writer.Error!void { |
| 8182 | 8208 | if (sl.len <= max_string_initializer_len) { |
| 8183 | | try sl.writer.writeByte('\"'); |
| 8209 | try sl.w.writeByte('\"'); |
| 8184 | 8210 | } else { |
| 8185 | | try sl.writer.writeByte('}'); |
| 8211 | try sl.w.writeByte('}'); |
| 8186 | 8212 | } |
| 8187 | 8213 | } |
| 8188 | 8214 | |
| 8189 | | fn writeStringLiteralChar(sl: *StringLiteral, c: u8) std.io.Writer.Error!void { |
| 8215 | fn writeStringLiteralChar(sl: *StringLiteral, c: u8) Writer.Error!void { |
| 8190 | 8216 | switch (c) { |
| 8191 | | 7 => try sl.writer.writeAll("\\a"), |
| 8192 | | 8 => try sl.writer.writeAll("\\b"), |
| 8193 | | '\t' => try sl.writer.writeAll("\\t"), |
| 8194 | | '\n' => try sl.writer.writeAll("\\n"), |
| 8195 | | 11 => try sl.writer.writeAll("\\v"), |
| 8196 | | 12 => try sl.writer.writeAll("\\f"), |
| 8197 | | '\r' => try sl.writer.writeAll("\\r"), |
| 8198 | | '"', '\'', '?', '\\' => try sl.writer.print("\\{c}", .{c}), |
| 8217 | 7 => try sl.w.writeAll("\\a"), |
| 8218 | 8 => try sl.w.writeAll("\\b"), |
| 8219 | '\t' => try sl.w.writeAll("\\t"), |
| 8220 | '\n' => try sl.w.writeAll("\\n"), |
| 8221 | 11 => try sl.w.writeAll("\\v"), |
| 8222 | 12 => try sl.w.writeAll("\\f"), |
| 8223 | '\r' => try sl.w.writeAll("\\r"), |
| 8224 | '"', '\'', '?', '\\' => try sl.w.print("\\{c}", .{c}), |
| 8199 | 8225 | else => switch (c) { |
| 8200 | | ' '...'~' => try sl.writer.writeByte(c), |
| 8201 | | else => try sl.writer.print("\\{o:0>3}", .{c}), |
| 8226 | ' '...'~' => try sl.w.writeByte(c), |
| 8227 | else => try sl.w.print("\\{o:0>3}", .{c}), |
| 8202 | 8228 | }, |
| 8203 | 8229 | } |
| 8204 | 8230 | } |
| 8205 | 8231 | |
| 8206 | | pub fn writeChar(sl: *StringLiteral, c: u8) std.io.Writer.Error!void { |
| 8232 | pub fn writeChar(sl: *StringLiteral, c: u8) Writer.Error!void { |
| 8207 | 8233 | if (sl.len <= max_string_initializer_len) { |
| 8208 | | if (sl.cur_len == 0 and sl.writer.count - sl.start_count > 1) |
| 8209 | | try sl.writer.writeAll("\"\""); |
| 8234 | if (sl.cur_len == 0 and sl.w.count - sl.start_count > 1) |
| 8235 | try sl.w.writeAll("\"\""); |
| 8210 | 8236 | |
| 8211 | | const count = sl.writer.count; |
| 8237 | const count = sl.w.count; |
| 8212 | 8238 | try sl.writeStringLiteralChar(c); |
| 8213 | | const char_len = sl.writer.count - count; |
| 8239 | const char_len = sl.w.count - count; |
| 8214 | 8240 | assert(char_len <= max_char_len); |
| 8215 | 8241 | sl.cur_len += char_len; |
| 8216 | 8242 | |
| 8217 | 8243 | if (sl.cur_len >= max_literal_len) sl.cur_len = 0; |
| 8218 | 8244 | } else { |
| 8219 | | if (sl.writer.count - sl.start_count > 1) try sl.writer.writeByte(','); |
| 8220 | | try sl.writer.print("'\\x{x}'", .{c}); |
| 8245 | if (sl.w.count - sl.start_count > 1) try sl.w.writeByte(','); |
| 8246 | try sl.w.print("'\\x{x}'", .{c}); |
| 8221 | 8247 | } |
| 8222 | 8248 | } |
| 8223 | 8249 | }; |
| ... | ... | @@ -8279,7 +8305,7 @@ fn formatIntLiteral(data: FormatIntLiteralContext, w: *std.io.Writer) std.io.Wri |
| 8279 | 8305 | |
| 8280 | 8306 | var int_buf: Value.BigIntSpace = undefined; |
| 8281 | 8307 | const int = if (data.val.isUndefDeep(zcu)) blk: { |
| 8282 | | undef_limbs = try oom(allocator.alloc(BigIntLimb, BigInt.calcTwosCompLimbCount(data.int_info.bits))); |
| 8308 | undef_limbs = allocator.alloc(BigIntLimb, BigInt.calcTwosCompLimbCount(data.int_info.bits)) catch return error.WriteFailed; |
| 8283 | 8309 | @memset(undef_limbs, undefPattern(BigIntLimb)); |
| 8284 | 8310 | |
| 8285 | 8311 | var undef_int = BigInt.Mutable{ |
| ... | ... | @@ -8297,7 +8323,7 @@ fn formatIntLiteral(data: FormatIntLiteralContext, w: *std.io.Writer) std.io.Wri |
| 8297 | 8323 | const one = BigInt.Mutable.init(&one_limbs, 1).toConst(); |
| 8298 | 8324 | |
| 8299 | 8325 | var wrap = BigInt.Mutable{ |
| 8300 | | .limbs = try oom(allocator.alloc(BigIntLimb, BigInt.calcTwosCompLimbCount(c_bits))), |
| 8326 | .limbs = allocator.alloc(BigIntLimb, BigInt.calcTwosCompLimbCount(c_bits)) catch return error.WriteFailed, |
| 8301 | 8327 | .len = undefined, |
| 8302 | 8328 | .positive = undefined, |
| 8303 | 8329 | }; |
| ... | ... | @@ -8351,7 +8377,8 @@ fn formatIntLiteral(data: FormatIntLiteralContext, w: *std.io.Writer) std.io.Wri |
| 8351 | 8377 | 16 => try w.writeAll("0x"), |
| 8352 | 8378 | else => unreachable, |
| 8353 | 8379 | } |
| 8354 | | const string = try oom(int.abs().toStringAlloc(allocator, data.base, data.case)); |
| 8380 | const string = int.abs().toStringAlloc(allocator, data.base, data.case) catch |
| 8381 | return error.WriteFailed; |
| 8355 | 8382 | defer allocator.free(string); |
| 8356 | 8383 | try w.writeAll(string); |
| 8357 | 8384 | } else { |
| ... | ... | @@ -8404,7 +8431,8 @@ fn formatIntLiteral(data: FormatIntLiteralContext, w: *std.io.Writer) std.io.Wri |
| 8404 | 8431 | .int_info = c_limb_int_info, |
| 8405 | 8432 | .kind = data.kind, |
| 8406 | 8433 | .ctype = c_limb_ctype, |
| 8407 | | .val = try oom(pt.intValue_big(.comptime_int, c_limb_mut.toConst())), |
| 8434 | .val = pt.intValue_big(.comptime_int, c_limb_mut.toConst()) catch |
| 8435 | return error.WriteFailed, |
| 8408 | 8436 | .base = data.base, |
| 8409 | 8437 | .case = data.case, |
| 8410 | 8438 | }, w); |
| ... | ... | @@ -8465,7 +8493,8 @@ const Assignment = struct { |
| 8465 | 8493 | try w.writeAll("))"); |
| 8466 | 8494 | }, |
| 8467 | 8495 | } |
| 8468 | | try w.writeAll(";\n"); |
| 8496 | try w.writeByte(';'); |
| 8497 | try f.object.newline(); |
| 8469 | 8498 | } |
| 8470 | 8499 | |
| 8471 | 8500 | fn strategy(self: Assignment, f: *Function) enum { assign, memcpy } { |
| ... | ... | @@ -8492,7 +8521,8 @@ const Vectorize = struct { |
| 8492 | 8521 | try w.print(" < {f}; ", .{try f.fmtIntLiteralDec(try pt.intValue(.usize, ty.vectorLen(zcu)))}); |
| 8493 | 8522 | try f.writeCValue(w, local, .Other); |
| 8494 | 8523 | try w.print(" += {f}) {{\n", .{try f.fmtIntLiteralDec(.one_usize)}); |
| 8495 | | f.object.indent_writer.pushIndent(); |
| 8524 | f.object.indent(); |
| 8525 | try f.object.newline(); |
| 8496 | 8526 | |
| 8497 | 8527 | break :index .{ .index = local }; |
| 8498 | 8528 | } else .{}; |
| ... | ... | @@ -8508,8 +8538,9 @@ const Vectorize = struct { |
| 8508 | 8538 | |
| 8509 | 8539 | pub fn end(self: Vectorize, f: *Function, inst: Air.Inst.Index, w: *Writer) !void { |
| 8510 | 8540 | if (self.index != .none) { |
| 8511 | | f.object.indent_writer.popIndent(); |
| 8512 | | try w.writeAll("}\n"); |
| 8541 | try f.object.outdent(); |
| 8542 | try w.writeByte('}'); |
| 8543 | try f.object.newline(); |
| 8513 | 8544 | try freeLocal(f, inst, self.index.new_local, null); |
| 8514 | 8545 | } |
| 8515 | 8546 | } |
| ... | ... | @@ -8617,9 +8648,3 @@ fn deinitFreeLocalsMap(gpa: Allocator, map: *LocalsMap) void { |
| 8617 | 8648 | } |
| 8618 | 8649 | map.deinit(gpa); |
| 8619 | 8650 | } |
| 8620 | | |
| 8621 | | fn oom(x: anytype) error{WriteFailed}!@typeInfo(@TypeOf(x)).error_union.payload { |
| 8622 | | return x catch |err| switch (err) { |
| 8623 | | error.OutOfMemory => error.WriteFailed, |
| 8624 | | }; |
| 8625 | | } |