x86_64: simplify airArrayElemVal() when pulling array from memory

This commit is contained in:
Jakub Konka 2023-04-12 10:02:38 +02:00
parent c8c5951e23
commit e05a51cb62

View File

@ -2897,7 +2897,7 @@ fn airArrayElemVal(self: *Self, inst: Air.Inst.Index) !void {
const offset_reg_lock = self.register_manager.lockRegAssumeUnused(offset_reg);
defer self.register_manager.unlockReg(offset_reg_lock);
const addr_reg = try self.register_manager.allocReg(null, gp);
const addr_reg = (try self.register_manager.allocReg(null, gp)).to64();
switch (array) {
.register => {
const off = @intCast(i32, try self.allocMem(
@ -2906,19 +2906,32 @@ fn airArrayElemVal(self: *Self, inst: Air.Inst.Index) !void {
array_ty.abiAlignment(self.target.*),
));
try self.genSetStack(array_ty, off, array, .{});
try self.asmRegisterMemory(.lea, addr_reg.to64(), Memory.sib(.qword, .{
try self.asmRegisterMemory(.lea, addr_reg, Memory.sib(.qword, .{
.base = .rbp,
.disp = -off,
}));
},
.stack_offset => |off| {
try self.asmRegisterMemory(.lea, addr_reg.to64(), Memory.sib(.qword, .{
try self.asmRegisterMemory(.lea, addr_reg, Memory.sib(.qword, .{
.base = .rbp,
.disp = -off,
}));
},
.memory, .linker_load => {
try self.loadMemPtrIntoRegister(addr_reg, Type.usize, array);
.memory => |addr| try self.genSetReg(Type.usize, addr_reg, .{ .immediate = addr }),
.linker_load => |load_struct| {
const atom_index = if (self.bin_file.cast(link.File.MachO)) |macho_file| blk: {
const atom = try macho_file.getOrCreateAtomForDecl(self.mod_fn.owner_decl);
break :blk macho_file.getAtom(atom).getSymbolIndex().?;
} else if (self.bin_file.cast(link.File.Coff)) |coff_file| blk: {
const atom = try coff_file.getOrCreateAtomForDecl(self.mod_fn.owner_decl);
break :blk coff_file.getAtom(atom).getSymbolIndex().?;
} else unreachable;
switch (load_struct.type) {
.import => unreachable,
.got => try self.asmMovLinker(addr_reg, atom_index, load_struct),
.direct => try self.asmLeaLinker(addr_reg, atom_index, load_struct),
}
},
else => return self.fail("TODO implement array_elem_val when array is {}", .{array}),
}