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