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