wasm/execution/store/
addrs.rs1use core::{cmp::Ordering, marker::PhantomData};
23
24use alloc::vec::Vec;
25
26pub(crate) trait Addr: Copy + core::fmt::Debug + core::fmt::Display + Eq {
30 fn new_unchecked(inner: usize) -> Self;
31
32 fn into_inner(self) -> usize;
33}
34
35pub(crate) struct AddrVec<A: Addr, Inst> {
36 inner: Vec<Inst>,
37 _phantom: PhantomData<A>,
38}
39
40impl<A: Addr, Inst> Default for AddrVec<A, Inst> {
41 fn default() -> Self {
42 Self {
43 inner: Vec::default(),
44 _phantom: PhantomData,
45 }
46 }
47}
48
49impl<A: Addr, Inst> AddrVec<A, Inst> {
50 pub fn get(&self, addr: A) -> &Inst {
52 self.inner
53 .get(addr.into_inner())
54 .expect("addrs to always be valid")
55 }
56
57 pub fn get_mut(&mut self, addr: A) -> &mut Inst {
59 self.inner
60 .get_mut(addr.into_inner())
61 .expect("addrs to always be valid")
62 }
63
64 pub(crate) fn insert(&mut self, instance: Inst) -> A {
68 let new_addr = self.inner.len();
69 self.inner.push(instance);
70 A::new_unchecked(new_addr)
71 }
72
73 pub(crate) fn get_two_mut(
77 &mut self,
78 addr_one: A,
79 addr_two: A,
80 ) -> Option<(&mut Inst, &mut Inst)> {
81 let addr_one = addr_one.into_inner();
82 let addr_two = addr_two.into_inner();
83
84 match addr_one.cmp(&addr_two) {
85 Ordering::Greater => {
86 let (left, right) = self.inner.split_at_mut(addr_one);
87 let one = right.get_mut(0).expect(
88 "this to be exactly the same as addr_one and addresses to always be valid",
89 );
90 let two = left
91 .get_mut(addr_two)
92 .expect("addresses to always be valid");
93
94 Some((one, two))
95 }
96 Ordering::Less => {
97 let (left, right) = self.inner.split_at_mut(addr_two);
98 let one = left
99 .get_mut(addr_one)
100 .expect("addresses to always be valid");
101 let two = right.get_mut(0).expect(
102 "this to be exactly the same as addr_two and addresses to always be valid",
103 );
104
105 Some((one, two))
106 }
107 Ordering::Equal => None,
108 }
109 }
110}
111
112#[derive(Copy, Clone, Debug, PartialEq, Eq)]
114pub struct FuncAddr(usize);
115
116impl FuncAddr {
117 pub(crate) const INVALID: Self = FuncAddr(usize::MAX);
119}
120
121impl core::fmt::Display for FuncAddr {
122 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
123 write!(f, "function address {}", self.0)
124 }
125}
126
127impl Addr for FuncAddr {
128 fn new_unchecked(inner: usize) -> Self {
129 Self(inner)
130 }
131
132 fn into_inner(self) -> usize {
133 self.0
134 }
135}
136
137#[derive(Copy, Clone, Debug, PartialEq, Eq)]
139pub struct TableAddr(usize);
140
141impl core::fmt::Display for TableAddr {
142 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
143 write!(f, "table address {}", self.0)
144 }
145}
146
147impl Addr for TableAddr {
148 fn new_unchecked(inner: usize) -> Self {
149 Self(inner)
150 }
151
152 fn into_inner(self) -> usize {
153 self.0
154 }
155}
156
157#[derive(Copy, Clone, Debug, PartialEq, Eq)]
159pub struct MemAddr(usize);
160
161impl core::fmt::Display for MemAddr {
162 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
163 write!(f, "memory address {}", self.0)
164 }
165}
166
167impl Addr for MemAddr {
168 fn new_unchecked(inner: usize) -> Self {
169 Self(inner)
170 }
171
172 fn into_inner(self) -> usize {
173 self.0
174 }
175}
176
177#[derive(Copy, Clone, Debug, PartialEq, Eq)]
179pub struct GlobalAddr(usize);
180
181impl core::fmt::Display for GlobalAddr {
182 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
183 write!(f, "global address {}", self.0)
184 }
185}
186
187impl Addr for GlobalAddr {
188 fn new_unchecked(inner: usize) -> Self {
189 Self(inner)
190 }
191
192 fn into_inner(self) -> usize {
194 self.0
195 }
196}
197
198#[derive(Copy, Clone, Debug, PartialEq, Eq)]
200pub struct ElemAddr(usize);
201
202impl core::fmt::Display for ElemAddr {
203 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
204 write!(f, "element segment address {}", self.0)
205 }
206}
207
208impl Addr for ElemAddr {
209 fn new_unchecked(inner: usize) -> Self {
210 Self(inner)
211 }
212
213 fn into_inner(self) -> usize {
214 self.0
215 }
216}
217
218#[derive(Copy, Clone, Debug, PartialEq, Eq)]
220pub struct DataAddr(usize);
221
222impl core::fmt::Display for DataAddr {
223 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
224 write!(f, "data segment address {}", self.0)
225 }
226}
227
228impl Addr for DataAddr {
229 fn new_unchecked(inner: usize) -> Self {
230 Self(inner)
231 }
232
233 fn into_inner(self) -> usize {
234 self.0
235 }
236}
237
238#[derive(Copy, Clone, Debug, PartialEq, Eq)]
240pub struct ModuleAddr(usize);
241
242impl core::fmt::Display for ModuleAddr {
243 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
244 write!(f, "module address {}", self.0)
245 }
246}
247
248impl Addr for ModuleAddr {
249 fn new_unchecked(inner: usize) -> Self {
250 Self(inner)
251 }
252
253 fn into_inner(self) -> usize {
254 self.0
255 }
256}