Commit 4e3573955f

Jakub Konka <kubkon@jakubkonka.com>
2023-04-12 10:32:08
x86_64: simplify genInlineMemcpy() when copying from memory
1 parent aab0039
Changed files (1)
src
arch
src/arch/x86_64/CodeGen.zig
@@ -7457,11 +7457,25 @@ fn genInlineMemcpy(
     try self.spillRegisters(&.{ .rdi, .rsi, .rcx });
 
     switch (dst_ptr) {
-        .memory, .linker_load => {
-            try self.loadMemPtrIntoRegister(.rdi, Type.usize, dst_ptr);
+        .memory => |addr| {
+            try self.genSetReg(Type.usize, .rdi, .{ .immediate = addr });
             // Load the pointer, which is stored in memory
             try self.asmRegisterMemory(.mov, .rdi, Memory.sib(.qword, .{ .base = .rdi }));
         },
+        .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, .direct => try self.asmMovLinker(.rdi, atom_index, load_struct),
+            }
+        },
         .stack_offset, .ptr_stack_offset => |off| {
             try self.asmRegisterMemory(switch (dst_ptr) {
                 .stack_offset => .mov,
@@ -7485,11 +7499,25 @@ fn genInlineMemcpy(
     }
 
     switch (src_ptr) {
-        .memory, .linker_load => {
-            try self.loadMemPtrIntoRegister(.rsi, Type.usize, src_ptr);
+        .memory => |addr| {
+            try self.genSetReg(Type.usize, .rsi, .{ .immediate = addr });
             // Load the pointer, which is stored in memory
             try self.asmRegisterMemory(.mov, .rsi, Memory.sib(.qword, .{ .base = .rsi }));
         },
+        .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, .direct => try self.asmMovLinker(.rsi, atom_index, load_struct),
+            }
+        },
         .stack_offset, .ptr_stack_offset => |off| {
             try self.asmRegisterMemory(switch (src_ptr) {
                 .stack_offset => .mov,