winch_codegen/abi/
local.rs

1use wasmtime_environ::WasmValType;
2
3/// Base register used to address the local slot.
4///
5/// Slots for stack arguments are addressed from the frame pointer.
6/// Slots for function-defined locals and for registers are addressed
7/// from the stack pointer.
8#[derive(Clone, Eq, PartialEq, Copy, Debug)]
9enum Base {
10    FP,
11    SP,
12}
13
14/// A local slot.
15///
16/// Represents the type, location and addressing mode of a local
17/// in the stack's local and argument area.
18/// LocalSlots are well known slots in the machine stack, and are generally
19/// reference by the stack pointer register (SP) or the base pointer register (FP).
20/// * Local slots that are referenced by the stack pointer register are the
21///   function defined locals and the param locals.
22/// * Local slots that represent arguments in the stack, are referenced through the
23///   base pointer register.
24///
25/// A [crate::masm::StackSlot] is a generalized form of a [LocalSlot]: they
26/// represent dynamic chunks of memory that get created throughout the function
27/// compilation lifetime when spilling values (register and locals) into the
28/// machine stack. A [LocalSlot] on the other hand gets created at the beginning
29/// of a function compilation and gets cleaned up at the end.
30#[derive(Clone, Copy, Debug)]
31pub(crate) struct LocalSlot {
32    /// The offset of the local slot.
33    pub offset: u32,
34    /// The type contained by this local slot.
35    pub ty: WasmValType,
36    /// Base register associated to this local slot.
37    base: Base,
38}
39
40impl LocalSlot {
41    /// Creates a local slot for a function defined local or
42    /// for a spilled argument register.
43    pub fn new(ty: WasmValType, offset: u32) -> Self {
44        Self {
45            ty,
46            offset,
47            base: Base::SP,
48        }
49    }
50
51    /// Int32 shortcut for `new`.
52    pub fn i32(offset: u32) -> Self {
53        Self {
54            ty: WasmValType::I32,
55            offset,
56            base: Base::SP,
57        }
58    }
59
60    /// Int64 shortcut for `new`.
61    pub fn i64(offset: u32) -> Self {
62        Self {
63            ty: WasmValType::I64,
64            offset,
65            base: Base::SP,
66        }
67    }
68
69    /// Creates a local slot for a stack function argument.
70    pub fn stack_arg(ty: WasmValType, offset: u32) -> Self {
71        Self {
72            ty,
73            offset,
74            base: Base::FP,
75        }
76    }
77
78    /// Check if the local is addressed from the stack pointer.
79    pub fn addressed_from_sp(&self) -> bool {
80        self.base == Base::SP
81    }
82}