| ... | ... | @@ -1,8 +1,12 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | 2 | const builtin = @import("builtin"); |
| 3 | const build_options = @import("build_options"); |
| 4 | |
| 3 | 5 | const mem = std.mem; |
| 4 | 6 | const math = std.math; |
| 5 | 7 | const assert = std.debug.assert; |
| 8 | const Allocator = mem.Allocator; |
| 9 | |
| 6 | 10 | const Air = @import("../../Air.zig"); |
| 7 | 11 | const Mir = @import("Mir.zig"); |
| 8 | 12 | const Emit = @import("Emit.zig"); |
| ... | ... | @@ -14,18 +18,16 @@ const Zcu = @import("../../Zcu.zig"); |
| 14 | 18 | const Package = @import("../../Package.zig"); |
| 15 | 19 | const InternPool = @import("../../InternPool.zig"); |
| 16 | 20 | const Compilation = @import("../../Compilation.zig"); |
| 21 | const trace = @import("../../tracy.zig").trace; |
| 22 | const codegen = @import("../../codegen.zig"); |
| 23 | |
| 17 | 24 | const ErrorMsg = Zcu.ErrorMsg; |
| 18 | 25 | const Target = std.Target; |
| 19 | | const Allocator = mem.Allocator; |
| 20 | | const trace = @import("../../tracy.zig").trace; |
| 21 | | const DW = std.dwarf; |
| 22 | | const leb128 = std.leb; |
| 26 | |
| 23 | 27 | const log = std.log.scoped(.riscv_codegen); |
| 24 | 28 | const tracking_log = std.log.scoped(.tracking); |
| 25 | 29 | const verbose_tracking_log = std.log.scoped(.verbose_tracking); |
| 26 | 30 | const wip_mir_log = std.log.scoped(.wip_mir); |
| 27 | | const build_options = @import("build_options"); |
| 28 | | const codegen = @import("../../codegen.zig"); |
| 29 | 31 | const Alignment = InternPool.Alignment; |
| 30 | 32 | |
| 31 | 33 | const CodeGenError = codegen.CodeGenError; |
| ... | ... | @@ -46,15 +48,16 @@ const RegisterLock = RegisterManager.RegisterLock; |
| 46 | 48 | |
| 47 | 49 | const InnerError = CodeGenError || error{OutOfRegisters}; |
| 48 | 50 | |
| 49 | | gpa: Allocator, |
| 50 | 51 | pt: Zcu.PerThread, |
| 51 | 52 | air: Air, |
| 52 | | mod: *Package.Module, |
| 53 | 53 | liveness: Liveness, |
| 54 | zcu: *Zcu, |
| 54 | 55 | bin_file: *link.File, |
| 56 | gpa: Allocator, |
| 57 | |
| 58 | mod: *Package.Module, |
| 55 | 59 | target: *const std.Target, |
| 56 | 60 | func_index: InternPool.Index, |
| 57 | | code: *std.ArrayList(u8), |
| 58 | 61 | debug_output: DebugInfoOutput, |
| 59 | 62 | err_msg: ?*ErrorMsg, |
| 60 | 63 | args: []MCValue, |
| ... | ... | @@ -63,9 +66,7 @@ fn_type: Type, |
| 63 | 66 | arg_index: usize, |
| 64 | 67 | src_loc: Zcu.LazySrcLoc, |
| 65 | 68 | |
| 66 | | /// MIR Instructions |
| 67 | 69 | mir_instructions: std.MultiArrayList(Mir.Inst) = .{}, |
| 68 | | /// MIR extra data |
| 69 | 70 | mir_extra: std.ArrayListUnmanaged(u32) = .{}, |
| 70 | 71 | |
| 71 | 72 | /// Byte offset within the source file of the ending curly. |
| ... | ... | @@ -731,16 +732,16 @@ pub fn generate( |
| 731 | 732 | } |
| 732 | 733 | try branch_stack.append(.{}); |
| 733 | 734 | |
| 734 | | var function = Func{ |
| 735 | var function: Func = .{ |
| 735 | 736 | .gpa = gpa, |
| 736 | 737 | .air = air, |
| 737 | 738 | .pt = pt, |
| 738 | 739 | .mod = mod, |
| 740 | .zcu = zcu, |
| 741 | .bin_file = bin_file, |
| 739 | 742 | .liveness = liveness, |
| 740 | 743 | .target = target, |
| 741 | | .bin_file = bin_file, |
| 742 | 744 | .func_index = func_index, |
| 743 | | .code = code, |
| 744 | 745 | .debug_output = debug_output, |
| 745 | 746 | .err_msg = null, |
| 746 | 747 | .args = undefined, // populated after `resolveCallingConventionValues` |
| ... | ... | @@ -825,7 +826,7 @@ pub fn generate( |
| 825 | 826 | else => |e| return e, |
| 826 | 827 | }; |
| 827 | 828 | |
| 828 | | var mir = Mir{ |
| 829 | var mir: Mir = .{ |
| 829 | 830 | .instructions = function.mir_instructions.toOwnedSlice(), |
| 830 | 831 | .extra = try function.mir_extra.toOwnedSlice(gpa), |
| 831 | 832 | .frame_locs = function.frame_locs.toOwnedSlice(), |
| ... | ... | @@ -833,7 +834,6 @@ pub fn generate( |
| 833 | 834 | defer mir.deinit(gpa); |
| 834 | 835 | |
| 835 | 836 | var emit: Emit = .{ |
| 836 | | .bin_file = bin_file, |
| 837 | 837 | .lower = .{ |
| 838 | 838 | .pt = pt, |
| 839 | 839 | .allocator = gpa, |
| ... | ... | @@ -844,6 +844,7 @@ pub fn generate( |
| 844 | 844 | .link_mode = comp.config.link_mode, |
| 845 | 845 | .pic = mod.pic, |
| 846 | 846 | }, |
| 847 | .bin_file = bin_file, |
| 847 | 848 | .debug_output = debug_output, |
| 848 | 849 | .code = code, |
| 849 | 850 | .prev_di_pc = 0, |
| ... | ... | @@ -932,7 +933,7 @@ fn fmtWipMir(func: *Func, inst: Mir.Inst.Index) std.fmt.Formatter(formatWipMir) |
| 932 | 933 | } |
| 933 | 934 | |
| 934 | 935 | const FormatDeclData = struct { |
| 935 | | mod: *Zcu, |
| 936 | zcu: *Zcu, |
| 936 | 937 | decl_index: InternPool.DeclIndex, |
| 937 | 938 | }; |
| 938 | 939 | fn formatDecl( |
| ... | ... | @@ -941,11 +942,11 @@ fn formatDecl( |
| 941 | 942 | _: std.fmt.FormatOptions, |
| 942 | 943 | writer: anytype, |
| 943 | 944 | ) @TypeOf(writer).Error!void { |
| 944 | | try writer.print("{}", .{data.mod.declPtr(data.decl_index).fqn.fmt(&data.mod.intern_pool)}); |
| 945 | try writer.print("{}", .{data.zcu.declPtr(data.decl_index).fqn.fmt(&data.zcu.intern_pool)}); |
| 945 | 946 | } |
| 946 | 947 | fn fmtDecl(func: *Func, decl_index: InternPool.DeclIndex) std.fmt.Formatter(formatDecl) { |
| 947 | 948 | return .{ .data = .{ |
| 948 | | .mod = func.pt.zcu, |
| 949 | .zcu = func.zcu, |
| 949 | 950 | .decl_index = decl_index, |
| 950 | 951 | } }; |
| 951 | 952 | } |
| ... | ... | @@ -1882,14 +1883,9 @@ fn symbolIndex(func: *Func) !u32 { |
| 1882 | 1883 | const pt = func.pt; |
| 1883 | 1884 | const zcu = pt.zcu; |
| 1884 | 1885 | const decl_index = zcu.funcOwnerDeclIndex(func.func_index); |
| 1885 | | return switch (func.bin_file.tag) { |
| 1886 | | .elf => blk: { |
| 1887 | | const elf_file = func.bin_file.cast(link.File.Elf).?; |
| 1888 | | const atom_index = try elf_file.zigObjectPtr().?.getOrCreateMetadataForDecl(elf_file, decl_index); |
| 1889 | | break :blk atom_index; |
| 1890 | | }, |
| 1891 | | else => return func.fail("TODO symbolIndex {s}", .{@tagName(func.bin_file.tag)}), |
| 1892 | | }; |
| 1886 | const elf_file = func.bin_file.cast(link.File.Elf).?; |
| 1887 | const atom_index = try elf_file.zigObjectPtr().?.getOrCreateMetadataForDecl(elf_file, decl_index); |
| 1888 | return atom_index; |
| 1893 | 1889 | } |
| 1894 | 1890 | |
| 1895 | 1891 | fn allocFrameIndex(func: *Func, alloc: FrameAlloc) !FrameIndex { |
| ... | ... | @@ -1940,9 +1936,7 @@ fn typeRegClass(func: *Func, ty: Type) abi.RegisterClass { |
| 1940 | 1936 | } |
| 1941 | 1937 | |
| 1942 | 1938 | fn regGeneralClassForType(func: *Func, ty: Type) RegisterManager.RegisterBitSet { |
| 1943 | | const pt = func.pt; |
| 1944 | | const zcu = pt.zcu; |
| 1945 | | return switch (ty.zigTypeTag(zcu)) { |
| 1939 | return switch (ty.zigTypeTag(func.pt.zcu)) { |
| 1946 | 1940 | .Float => abi.Registers.Float.general_purpose, |
| 1947 | 1941 | .Vector => abi.Registers.Vector.general_purpose, |
| 1948 | 1942 | else => abi.Registers.Integer.general_purpose, |
| ... | ... | @@ -1950,9 +1944,7 @@ fn regGeneralClassForType(func: *Func, ty: Type) RegisterManager.RegisterBitSet |
| 1950 | 1944 | } |
| 1951 | 1945 | |
| 1952 | 1946 | fn regTempClassForType(func: *Func, ty: Type) RegisterManager.RegisterBitSet { |
| 1953 | | const pt = func.pt; |
| 1954 | | const zcu = pt.zcu; |
| 1955 | | return switch (ty.zigTypeTag(zcu)) { |
| 1947 | return switch (ty.zigTypeTag(func.pt.zcu)) { |
| 1956 | 1948 | .Float => abi.Registers.Float.temporary, |
| 1957 | 1949 | .Vector => abi.Registers.Vector.general_purpose, // there are no temporary vector registers |
| 1958 | 1950 | else => abi.Registers.Integer.temporary, |
| ... | ... | @@ -1961,9 +1953,10 @@ fn regTempClassForType(func: *Func, ty: Type) RegisterManager.RegisterBitSet { |
| 1961 | 1953 | |
| 1962 | 1954 | fn allocRegOrMem(func: *Func, elem_ty: Type, inst: ?Air.Inst.Index, reg_ok: bool) !MCValue { |
| 1963 | 1955 | const pt = func.pt; |
| 1956 | const zcu = pt.zcu; |
| 1964 | 1957 | |
| 1965 | 1958 | const bit_size = elem_ty.bitSize(pt); |
| 1966 | | const min_size: u64 = switch (elem_ty.zigTypeTag(pt.zcu)) { |
| 1959 | const min_size: u64 = switch (elem_ty.zigTypeTag(zcu)) { |
| 1967 | 1960 | .Float => if (func.hasFeature(.d)) 64 else 32, |
| 1968 | 1961 | .Vector => 256, // TODO: calculate it from avl * vsew |
| 1969 | 1962 | else => 64, |
| ... | ... | @@ -1973,7 +1966,7 @@ fn allocRegOrMem(func: *Func, elem_ty: Type, inst: ?Air.Inst.Index, reg_ok: bool |
| 1973 | 1966 | if (func.register_manager.tryAllocReg(inst, func.regGeneralClassForType(elem_ty))) |reg| { |
| 1974 | 1967 | return .{ .register = reg }; |
| 1975 | 1968 | } |
| 1976 | | } else if (reg_ok and elem_ty.zigTypeTag(pt.zcu) == .Vector) { |
| 1969 | } else if (reg_ok and elem_ty.zigTypeTag(zcu) == .Vector) { |
| 1977 | 1970 | return func.fail("did you forget to extend vector registers before allocating", .{}); |
| 1978 | 1971 | } |
| 1979 | 1972 | |
| ... | ... | @@ -7270,9 +7263,7 @@ fn parseRegName(name: []const u8) ?Register { |
| 7270 | 7263 | } |
| 7271 | 7264 | |
| 7272 | 7265 | fn typeOf(func: *Func, inst: Air.Inst.Ref) Type { |
| 7273 | | const pt = func.pt; |
| 7274 | | const zcu = pt.zcu; |
| 7275 | | return func.air.typeOf(inst, &zcu.intern_pool); |
| 7266 | return func.air.typeOf(inst, &func.pt.zcu.intern_pool); |
| 7276 | 7267 | } |
| 7277 | 7268 | |
| 7278 | 7269 | fn typeOfIndex(func: *Func, inst: Air.Inst.Index) Type { |