authorgravatar for david@vortan.devDavid Rubin <david@vortan.dev> 2024-07-02 03:14:51-07:00
committergravatar for david@vortan.devDavid Rubin <david@vortan.dev> 2024-07-14 23:04:05-07:00
log5ec926cdbf05312111cb5451bf152ee054f9ef1f
treedbd77c69c7e4f7bec6e4cf0c3a7cd95a22dc8057
parent5a2c547fc1eedcec52e9e7259449d9a093e21ab1
signaturelock-open Commit is signed but in an unrecognized format.

riscv: refactor `bin_file` and `zcu` usage


1 files changed, 29 insertions(+), 38 deletions(-)

src/arch/riscv64/CodeGen.zig+29-38
...@@ -1,8 +1,12 @@...@@ -1,8 +1,12 @@
1const std = @import("std");1const std = @import("std");
2const builtin = @import("builtin");2const builtin = @import("builtin");
3const build_options = @import("build_options");
4
3const mem = std.mem;5const mem = std.mem;
4const math = std.math;6const math = std.math;
5const assert = std.debug.assert;7const assert = std.debug.assert;
8const Allocator = mem.Allocator;
9
6const Air = @import("../../Air.zig");10const Air = @import("../../Air.zig");
7const Mir = @import("Mir.zig");11const Mir = @import("Mir.zig");
8const Emit = @import("Emit.zig");12const Emit = @import("Emit.zig");
...@@ -14,18 +18,16 @@ const Zcu = @import("../../Zcu.zig");...@@ -14,18 +18,16 @@ const Zcu = @import("../../Zcu.zig");
14const Package = @import("../../Package.zig");18const Package = @import("../../Package.zig");
15const InternPool = @import("../../InternPool.zig");19const InternPool = @import("../../InternPool.zig");
16const Compilation = @import("../../Compilation.zig");20const Compilation = @import("../../Compilation.zig");
21const trace = @import("../../tracy.zig").trace;
22const codegen = @import("../../codegen.zig");
23
17const ErrorMsg = Zcu.ErrorMsg;24const ErrorMsg = Zcu.ErrorMsg;
18const Target = std.Target;25const Target = std.Target;
19const Allocator = mem.Allocator;26
20const trace = @import("../../tracy.zig").trace;
21const DW = std.dwarf;
22const leb128 = std.leb;
23const log = std.log.scoped(.riscv_codegen);27const log = std.log.scoped(.riscv_codegen);
24const tracking_log = std.log.scoped(.tracking);28const tracking_log = std.log.scoped(.tracking);
25const verbose_tracking_log = std.log.scoped(.verbose_tracking);29const verbose_tracking_log = std.log.scoped(.verbose_tracking);
26const wip_mir_log = std.log.scoped(.wip_mir);30const wip_mir_log = std.log.scoped(.wip_mir);
27const build_options = @import("build_options");
28const codegen = @import("../../codegen.zig");
29const Alignment = InternPool.Alignment;31const Alignment = InternPool.Alignment;
3032
31const CodeGenError = codegen.CodeGenError;33const CodeGenError = codegen.CodeGenError;
...@@ -46,15 +48,16 @@ const RegisterLock = RegisterManager.RegisterLock;...@@ -46,15 +48,16 @@ const RegisterLock = RegisterManager.RegisterLock;
4648
47const InnerError = CodeGenError || error{OutOfRegisters};49const InnerError = CodeGenError || error{OutOfRegisters};
4850
49gpa: Allocator,
50pt: Zcu.PerThread,51pt: Zcu.PerThread,
51air: Air,52air: Air,
52mod: *Package.Module,
53liveness: Liveness,53liveness: Liveness,
54zcu: *Zcu,
54bin_file: *link.File,55bin_file: *link.File,
56gpa: Allocator,
57
58mod: *Package.Module,
55target: *const std.Target,59target: *const std.Target,
56func_index: InternPool.Index,60func_index: InternPool.Index,
57code: *std.ArrayList(u8),
58debug_output: DebugInfoOutput,61debug_output: DebugInfoOutput,
59err_msg: ?*ErrorMsg,62err_msg: ?*ErrorMsg,
60args: []MCValue,63args: []MCValue,
...@@ -63,9 +66,7 @@ fn_type: Type,...@@ -63,9 +66,7 @@ fn_type: Type,
63arg_index: usize,66arg_index: usize,
64src_loc: Zcu.LazySrcLoc,67src_loc: Zcu.LazySrcLoc,
6568
66/// MIR Instructions
67mir_instructions: std.MultiArrayList(Mir.Inst) = .{},69mir_instructions: std.MultiArrayList(Mir.Inst) = .{},
68/// MIR extra data
69mir_extra: std.ArrayListUnmanaged(u32) = .{},70mir_extra: std.ArrayListUnmanaged(u32) = .{},
7071
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(.{});
733734
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 };
827828
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);
834835
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}
933934
934const FormatDeclData = struct {935const FormatDeclData = struct {
935 mod: *Zcu,936 zcu: *Zcu,
936 decl_index: InternPool.DeclIndex,937 decl_index: InternPool.DeclIndex,
937};938};
938fn formatDecl(939fn 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}
946fn fmtDecl(func: *Func, decl_index: InternPool.DeclIndex) std.fmt.Formatter(formatDecl) {947fn 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}
18941890
1895fn allocFrameIndex(func: *Func, alloc: FrameAlloc) !FrameIndex {1891fn 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}
19411937
1942fn regGeneralClassForType(func: *Func, ty: Type) RegisterManager.RegisterBitSet {1938fn 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}
19511945
1952fn regTempClassForType(func: *Func, ty: Type) RegisterManager.RegisterBitSet {1946fn 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 registers1949 .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 {
19611953
1962fn allocRegOrMem(func: *Func, elem_ty: Type, inst: ?Air.Inst.Index, reg_ok: bool) !MCValue {1954fn 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;
19641957
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 * vsew1961 .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 }
19791972
...@@ -7270,9 +7263,7 @@ fn parseRegName(name: []const u8) ?Register {...@@ -7270,9 +7263,7 @@ fn parseRegName(name: []const u8) ?Register {
7270}7263}
72717264
7272fn typeOf(func: *Func, inst: Air.Inst.Ref) Type {7265fn 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}
72777268
7278fn typeOfIndex(func: *Func, inst: Air.Inst.Index) Type {7269fn typeOfIndex(func: *Func, inst: Air.Inst.Index) Type {