winch(x64): Initial implementation for function calls (#6067)
* 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
This commit is contained in:
@@ -22,7 +22,7 @@ pub(crate) enum RemKind {
|
||||
}
|
||||
|
||||
/// Operand size, in bits.
|
||||
#[derive(Copy, Clone, Eq, PartialEq)]
|
||||
#[derive(Copy, Debug, Clone, Eq, PartialEq)]
|
||||
pub(crate) enum OperandSize {
|
||||
/// 32 bits.
|
||||
S32,
|
||||
@@ -87,9 +87,24 @@ pub(crate) trait MacroAssembler {
|
||||
/// Reserve stack space.
|
||||
fn reserve_stack(&mut self, bytes: u32);
|
||||
|
||||
/// Free stack space.
|
||||
fn free_stack(&mut self, bytes: u32);
|
||||
|
||||
/// Get the address of a local slot.
|
||||
fn local_address(&mut self, local: &LocalSlot) -> Self::Address;
|
||||
|
||||
/// Constructs an address with an offset that is relative to the
|
||||
/// current position of the stack pointer (e.g. [sp + (sp_offset -
|
||||
/// offset)].
|
||||
fn address_from_sp(&self, offset: u32) -> Self::Address;
|
||||
|
||||
/// Constructs an address with an offset that is absolute to the
|
||||
/// current position of the stack pointer (e.g. [sp + offset].
|
||||
fn address_at_sp(&self, offset: u32) -> Self::Address;
|
||||
|
||||
/// Emit a function call to a locally defined function.
|
||||
fn call(&mut self, callee: u32);
|
||||
|
||||
/// Get stack pointer offset.
|
||||
fn sp_offset(&mut self) -> u32;
|
||||
|
||||
@@ -99,6 +114,9 @@ pub(crate) trait MacroAssembler {
|
||||
/// Perform a stack load.
|
||||
fn load(&mut self, src: Self::Address, dst: Reg, size: OperandSize);
|
||||
|
||||
/// Pop a value from the machine stack into the given register.
|
||||
fn pop(&mut self, dst: Reg);
|
||||
|
||||
/// Perform a move.
|
||||
fn mov(&mut self, src: RegImm, dst: RegImm, size: OperandSize);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user