Skip to content

Commit 5ec926c

Browse files
committed
riscv: refactor bin_file and zcu usage
1 parent 5a2c547 commit 5ec926c

File tree

1 file changed

+29
-38
lines changed

1 file changed

+29
-38
lines changed

src/arch/riscv64/CodeGen.zig

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
const std = @import("std");
22
const builtin = @import("builtin");
3+
const build_options = @import("build_options");
4+
35
const mem = std.mem;
46
const math = std.math;
57
const assert = std.debug.assert;
8+
const Allocator = mem.Allocator;
9+
610
const Air = @import("../../Air.zig");
711
const Mir = @import("Mir.zig");
812
const Emit = @import("Emit.zig");
@@ -14,18 +18,16 @@ const Zcu = @import("../../Zcu.zig");
1418
const Package = @import("../../Package.zig");
1519
const InternPool = @import("../../InternPool.zig");
1620
const Compilation = @import("../../Compilation.zig");
21+
const trace = @import("../../tracy.zig").trace;
22+
const codegen = @import("../../codegen.zig");
23+
1724
const ErrorMsg = Zcu.ErrorMsg;
1825
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+
2327
const log = std.log.scoped(.riscv_codegen);
2428
const tracking_log = std.log.scoped(.tracking);
2529
const verbose_tracking_log = std.log.scoped(.verbose_tracking);
2630
const wip_mir_log = std.log.scoped(.wip_mir);
27-
const build_options = @import("build_options");
28-
const codegen = @import("../../codegen.zig");
2931
const Alignment = InternPool.Alignment;
3032

3133
const CodeGenError = codegen.CodeGenError;
@@ -46,15 +48,16 @@ const RegisterLock = RegisterManager.RegisterLock;
4648

4749
const InnerError = CodeGenError || error{OutOfRegisters};
4850

49-
gpa: Allocator,
5051
pt: Zcu.PerThread,
5152
air: Air,
52-
mod: *Package.Module,
5353
liveness: Liveness,
54+
zcu: *Zcu,
5455
bin_file: *link.File,
56+
gpa: Allocator,
57+
58+
mod: *Package.Module,
5559
target: *const std.Target,
5660
func_index: InternPool.Index,
57-
code: *std.ArrayList(u8),
5861
debug_output: DebugInfoOutput,
5962
err_msg: ?*ErrorMsg,
6063
args: []MCValue,
@@ -63,9 +66,7 @@ fn_type: Type,
6366
arg_index: usize,
6467
src_loc: Zcu.LazySrcLoc,
6568

66-
/// MIR Instructions
6769
mir_instructions: std.MultiArrayList(Mir.Inst) = .{},
68-
/// MIR extra data
6970
mir_extra: std.ArrayListUnmanaged(u32) = .{},
7071

7172
/// Byte offset within the source file of the ending curly.
@@ -731,16 +732,16 @@ pub fn generate(
731732
}
732733
try branch_stack.append(.{});
733734

734-
var function = Func{
735+
var function: Func = .{
735736
.gpa = gpa,
736737
.air = air,
737738
.pt = pt,
738739
.mod = mod,
740+
.zcu = zcu,
741+
.bin_file = bin_file,
739742
.liveness = liveness,
740743
.target = target,
741-
.bin_file = bin_file,
742744
.func_index = func_index,
743-
.code = code,
744745
.debug_output = debug_output,
745746
.err_msg = null,
746747
.args = undefined, // populated after `resolveCallingConventionValues`
@@ -825,15 +826,14 @@ pub fn generate(
825826
else => |e| return e,
826827
};
827828

828-
var mir = Mir{
829+
var mir: Mir = .{
829830
.instructions = function.mir_instructions.toOwnedSlice(),
830831
.extra = try function.mir_extra.toOwnedSlice(gpa),
831832
.frame_locs = function.frame_locs.toOwnedSlice(),
832833
};
833834
defer mir.deinit(gpa);
834835

835836
var emit: Emit = .{
836-
.bin_file = bin_file,
837837
.lower = .{
838838
.pt = pt,
839839
.allocator = gpa,
@@ -844,6 +844,7 @@ pub fn generate(
844844
.link_mode = comp.config.link_mode,
845845
.pic = mod.pic,
846846
},
847+
.bin_file = bin_file,
847848
.debug_output = debug_output,
848849
.code = code,
849850
.prev_di_pc = 0,
@@ -932,7 +933,7 @@ fn fmtWipMir(func: *Func, inst: Mir.Inst.Index) std.fmt.Formatter(formatWipMir)
932933
}
933934

934935
const FormatDeclData = struct {
935-
mod: *Zcu,
936+
zcu: *Zcu,
936937
decl_index: InternPool.DeclIndex,
937938
};
938939
fn formatDecl(
@@ -941,11 +942,11 @@ fn formatDecl(
941942
_: std.fmt.FormatOptions,
942943
writer: anytype,
943944
) @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)});
945946
}
946947
fn fmtDecl(func: *Func, decl_index: InternPool.DeclIndex) std.fmt.Formatter(formatDecl) {
947948
return .{ .data = .{
948-
.mod = func.pt.zcu,
949+
.zcu = func.zcu,
949950
.decl_index = decl_index,
950951
} };
951952
}
@@ -1882,14 +1883,9 @@ fn symbolIndex(func: *Func) !u32 {
18821883
const pt = func.pt;
18831884
const zcu = pt.zcu;
18841885
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;
18931889
}
18941890

18951891
fn allocFrameIndex(func: *Func, alloc: FrameAlloc) !FrameIndex {
@@ -1940,19 +1936,15 @@ fn typeRegClass(func: *Func, ty: Type) abi.RegisterClass {
19401936
}
19411937

19421938
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)) {
19461940
.Float => abi.Registers.Float.general_purpose,
19471941
.Vector => abi.Registers.Vector.general_purpose,
19481942
else => abi.Registers.Integer.general_purpose,
19491943
};
19501944
}
19511945

19521946
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)) {
19561948
.Float => abi.Registers.Float.temporary,
19571949
.Vector => abi.Registers.Vector.general_purpose, // there are no temporary vector registers
19581950
else => abi.Registers.Integer.temporary,
@@ -1961,9 +1953,10 @@ fn regTempClassForType(func: *Func, ty: Type) RegisterManager.RegisterBitSet {
19611953

19621954
fn allocRegOrMem(func: *Func, elem_ty: Type, inst: ?Air.Inst.Index, reg_ok: bool) !MCValue {
19631955
const pt = func.pt;
1956+
const zcu = pt.zcu;
19641957

19651958
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)) {
19671960
.Float => if (func.hasFeature(.d)) 64 else 32,
19681961
.Vector => 256, // TODO: calculate it from avl * vsew
19691962
else => 64,
@@ -1973,7 +1966,7 @@ fn allocRegOrMem(func: *Func, elem_ty: Type, inst: ?Air.Inst.Index, reg_ok: bool
19731966
if (func.register_manager.tryAllocReg(inst, func.regGeneralClassForType(elem_ty))) |reg| {
19741967
return .{ .register = reg };
19751968
}
1976-
} else if (reg_ok and elem_ty.zigTypeTag(pt.zcu) == .Vector) {
1969+
} else if (reg_ok and elem_ty.zigTypeTag(zcu) == .Vector) {
19771970
return func.fail("did you forget to extend vector registers before allocating", .{});
19781971
}
19791972

@@ -7270,9 +7263,7 @@ fn parseRegName(name: []const u8) ?Register {
72707263
}
72717264

72727265
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);
72767267
}
72777268

72787269
fn typeOfIndex(func: *Func, inst: Air.Inst.Index) Type {

0 commit comments

Comments
 (0)