* 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
91 lines
2.2 KiB
Rust
91 lines
2.2 KiB
Rust
use crate::isa::reg::Reg;
|
|
|
|
/// A bit set to track regiter availability.
|
|
pub(crate) struct RegSet {
|
|
/// Bitset to track general purpose register availability.
|
|
gpr: u32,
|
|
/// Bitset to track floating-point register availability.
|
|
_fpr: u32,
|
|
}
|
|
|
|
impl RegSet {
|
|
/// Create a new register set.
|
|
pub fn new(gpr: u32, fpr: u32) -> Self {
|
|
Self { gpr, _fpr: fpr }
|
|
}
|
|
|
|
/// Request a general purpose register.
|
|
pub fn any_gpr(&mut self) -> Option<Reg> {
|
|
self.gpr_available().then(|| {
|
|
let index = self.gpr.trailing_zeros();
|
|
self.allocate(index);
|
|
Reg::int(index as usize)
|
|
})
|
|
}
|
|
|
|
/// Request a specific general purpose register.
|
|
pub fn gpr(&mut self, reg: Reg) -> Option<Reg> {
|
|
let index = reg.hw_enc();
|
|
self.named_gpr_available(index as u32).then(|| {
|
|
self.allocate(index as u32);
|
|
Reg::int(index as usize)
|
|
})
|
|
}
|
|
|
|
/// Free the given general purpose register.
|
|
pub fn free_gpr(&mut self, reg: Reg) {
|
|
let index = reg.hw_enc() as u32;
|
|
self.gpr |= 1 << index;
|
|
}
|
|
|
|
/// Returns true if the given general purpose register
|
|
/// is available.
|
|
pub fn named_gpr_available(&self, index: u32) -> bool {
|
|
let index = 1 << index;
|
|
(!self.gpr & index) == 0
|
|
}
|
|
|
|
fn gpr_available(&self) -> bool {
|
|
self.gpr != 0
|
|
}
|
|
|
|
fn allocate(&mut self, index: u32) {
|
|
self.gpr &= !(1 << index);
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::{Reg, RegSet};
|
|
|
|
const UNIVERSE: u32 = (1 << 16) - 1;
|
|
|
|
#[test]
|
|
fn test_any_gpr() {
|
|
let mut set = RegSet::new(UNIVERSE, 0);
|
|
for _ in 0..16 {
|
|
let gpr = set.any_gpr();
|
|
assert!(gpr.is_some())
|
|
}
|
|
|
|
assert!(!set.gpr_available());
|
|
assert!(set.any_gpr().is_none())
|
|
}
|
|
|
|
#[test]
|
|
fn test_gpr() {
|
|
let all = UNIVERSE & !(1 << 5);
|
|
let target = Reg::int(5);
|
|
let mut set = RegSet::new(all, 0);
|
|
assert!(set.gpr(target).is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn test_free_gpr() {
|
|
let mut set = RegSet::new(UNIVERSE, 0);
|
|
let gpr = set.any_gpr().unwrap();
|
|
set.free_gpr(gpr);
|
|
assert!(set.gpr(gpr).is_some());
|
|
}
|
|
}
|