Skip to content

Commit

Permalink
feat(compiler) Windows ABI needs RSI and RDI to be saved.So save them…
Browse files Browse the repository at this point in the history
… uncoditionnaly for now
  • Loading branch information
ptitSeb committed Sep 17, 2021
1 parent c914b48 commit 45f56d0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/compiler-singlepass/src/codegen_x64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6696,7 +6696,7 @@ impl<'a> FuncGen<'a> {
if self.control_stack.is_empty() {
self.assembler.emit_label(frame.label);
self.machine
.finalize_locals(&mut self.assembler, &self.locals);
.finalize_locals(&mut self.assembler, &self.locals, self.config.calling_convention);
self.assembler.emit_mov(
Size::S64,
Location::GPR(GPR::RBP),
Expand Down
35 changes: 34 additions & 1 deletion lib/compiler-singlepass/src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,11 @@ impl Machine {
// Callee-saved R15 for vmctx.
static_area_size += 8;

// For Windows ABI, save RDI and RSI
if calling_convention == CallingConvention::WindowsFastcall {
static_area_size += 8 * 2;
}

// Total size of callee saved registers.
let callee_saved_regs_size = static_area_size;

Expand Down Expand Up @@ -413,6 +418,29 @@ impl Machine {
X64Register::GPR(GPR::R15).to_index(),
));

if calling_convention == CallingConvention::WindowsFastcall {
// Save RDI
self.stack_offset.0 += 8;
a.emit_mov(
Size::S64,
Location::GPR(GPR::RDI),
Location::Memory(GPR::RBP, -(self.stack_offset.0 as i32)),
);
self.state.stack_values.push(MachineValue::PreserveRegister(
X64Register::GPR(GPR::RDI).to_index(),
));
// Save RSI
self.stack_offset.0 += 8;
a.emit_mov(
Size::S64,
Location::GPR(GPR::RSI),
Location::Memory(GPR::RBP, -(self.stack_offset.0 as i32)),
);
self.state.stack_values.push(MachineValue::PreserveRegister(
X64Register::GPR(GPR::RSI).to_index(),
));
}

// Save the offset of register save area.
self.save_area_offset = Some(MachineStackOffset(self.stack_offset.0));

Expand Down Expand Up @@ -501,7 +529,7 @@ impl Machine {
locations
}

pub fn finalize_locals<E: Emitter>(&mut self, a: &mut E, locations: &[Location]) {
pub fn finalize_locals<E: Emitter>(&mut self, a: &mut E, locations: &[Location], calling_convention: CallingConvention) {
// Unwind stack to the "save area".
a.emit_lea(
Size::S64,
Expand All @@ -512,6 +540,11 @@ impl Machine {
Location::GPR(GPR::RSP),
);

if calling_convention == CallingConvention::WindowsFastcall {
// Restore RSI and RDI
a.emit_pop(Size::S64, Location::GPR(GPR::RSI));
a.emit_pop(Size::S64, Location::GPR(GPR::RDI));
}
// Restore R15 used by vmctx.
a.emit_pop(Size::S64, Location::GPR(GPR::R15));

Expand Down

0 comments on commit 45f56d0

Please sign in to comment.