* winch(x64): Initial implementation for function calls This change adds the main building blocks for calling locally defined functions. Support for function imports will be added iteratively after this change lands and once trampolines are supported. To support function calls, this change introduces the following functionality to the MacroAssembler: * `pop` to pop the machine stack into a given register, which in the case of this change, translates to the x64 pop instruction. * `call` to a emit a call to locally defined functions. * `address_from_sp` to construct memory addresses with the SP as a base. * `free_stack` to emit the necessary instrunctions to claim stack space. The heavy lifting of setting up and emitting the function call is done through the implementation of `FnCall`. * Fix spill behaviour in function calls and add more documentation This commits adds a more detailed documentation to the `call.rs` module. It also fixes a couple of bugs, mainly: * The previous commit didn't account for memory addresses used as arguments for the function call, any memory entry in the value stack used as a function argument should be tracked and then used to claim that memory when the function call ends. We could `pop` and do this implicitly, but we can also track this down and emit a single instruction to decrement the stack pointer, which will result in better code. * Introduce a differentiator between addresses relative or absolute to the stack pointer. When passing arguments in the stack -- assuming that SP at that point is aligned for the function call -- we should store the arguments relative to the absolute position of the stack pointer and when addressing a memory entry in the Wasm value stack, we should use an address relative to the offset and the position of the stack pointer. * Simplify tracking of the stack space needed for emitting a function call
62 lines
1.8 KiB
Rust
62 lines
1.8 KiB
Rust
use crate::{isa::reg::Reg, regset::RegSet};
|
|
|
|
/// The register allocator.
|
|
///
|
|
/// The register allocator uses a single-pass algorithm;
|
|
/// its implementation uses a bitset as a freelist
|
|
/// to track per-class register availability.
|
|
///
|
|
/// If a particular register is not available upon request
|
|
/// the register allocation will perform a "spill", essentially
|
|
/// moving Local and Register values in the stack to memory.
|
|
/// This processs ensures that whenever a register is requested,
|
|
/// it is going to be available.
|
|
pub(crate) struct RegAlloc {
|
|
pub scratch: Reg,
|
|
regset: RegSet,
|
|
}
|
|
|
|
impl RegAlloc {
|
|
/// Create a new register allocator
|
|
/// from a register set.
|
|
pub fn new(regset: RegSet, scratch: Reg) -> Self {
|
|
Self { regset, scratch }
|
|
}
|
|
|
|
/// Allocate the next available general purpose register,
|
|
/// spilling if none available.
|
|
pub fn any_gpr<F>(&mut self, spill: &mut F) -> Reg
|
|
where
|
|
F: FnMut(&mut RegAlloc),
|
|
{
|
|
self.regset.any_gpr().unwrap_or_else(|| {
|
|
spill(self);
|
|
self.regset.any_gpr().expect("any gpr to be available")
|
|
})
|
|
}
|
|
|
|
/// Checks if a general purpose register is avaiable.
|
|
pub fn gpr_available(&self, reg: Reg) -> bool {
|
|
self.regset.named_gpr_available(reg.hw_enc() as u32)
|
|
}
|
|
|
|
/// Request a specific general purpose register,
|
|
/// spilling if not available.
|
|
pub fn gpr<F>(&mut self, named: Reg, spill: &mut F) -> Reg
|
|
where
|
|
F: FnMut(&mut RegAlloc),
|
|
{
|
|
self.regset.gpr(named).unwrap_or_else(|| {
|
|
spill(self);
|
|
self.regset
|
|
.gpr(named)
|
|
.expect(&format!("gpr {:?} to be available", named))
|
|
})
|
|
}
|
|
|
|
/// Mark a particular general purpose register as available.
|
|
pub fn free_gpr(&mut self, reg: Reg) {
|
|
self.regset.free_gpr(reg);
|
|
}
|
|
}
|