xref: /wasmtime-44.0.1/winch/codegen/src/abi/local.rs (revision 72004aad)
1 use 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)]
9 enum 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 /// A [crate::masm::StackSlot] is a generalized form of a [LocalSlot]: they
25 /// represent dynamic chunks of memory that get created throughout the function
26 /// compilation lifetime when spilling values (register and locals) into the
27 /// machine stack. A [LocalSlot] on the other hand gets created at the beginning
28 /// of a function compilation and gets cleaned up at the end.
29 #[derive(Clone, Copy, Debug)]
30 pub(crate) struct LocalSlot {
31     /// The offset of the local slot.
32     pub offset: u32,
33     /// The type contained by this local slot.
34     pub ty: WasmValType,
35     /// Base register associated to this local slot.
36     base: Base,
37 }
38 
39 impl LocalSlot {
40     /// Creates a local slot for a function defined local or
41     /// for a spilled argument register.
42     pub fn new(ty: WasmValType, offset: u32) -> Self {
43         Self {
44             ty,
45             offset,
46             base: Base::SP,
47         }
48     }
49 
50     /// Int32 shortcut for `new`.
51     pub fn i32(offset: u32) -> Self {
52         Self {
53             ty: WasmValType::I32,
54             offset,
55             base: Base::SP,
56         }
57     }
58 
59     /// Int64 shortcut for `new`.
60     pub fn i64(offset: u32) -> Self {
61         Self {
62             ty: WasmValType::I64,
63             offset,
64             base: Base::SP,
65         }
66     }
67 
68     /// Creates a local slot for a stack function argument.
69     pub fn stack_arg(ty: WasmValType, offset: u32) -> Self {
70         Self {
71             ty,
72             offset,
73             base: Base::FP,
74         }
75     }
76 
77     /// Check if the local is addressed from the stack pointer.
78     pub fn addressed_from_sp(&self) -> bool {
79         self.base == Base::SP
80     }
81 }
82