wasmi_ir/
sequence.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
use crate::{core::TrapCode, index::*, *};
use ::core::{
    num::{NonZeroI32, NonZeroI64, NonZeroU32, NonZeroU64},
    slice,
};
use std::{boxed::Box, vec::Vec};

/// A sequence of [`Instruction`]s.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct InstrSequence {
    /// The [`Instruction`] that make up all built instructions in sequence.
    instrs: Vec<Instruction>,
}

impl From<InstrSequence> for Vec<Instruction> {
    fn from(sequence: InstrSequence) -> Self {
        sequence.into_vec()
    }
}

impl From<InstrSequence> for Box<[Instruction]> {
    fn from(sequence: InstrSequence) -> Self {
        sequence.into_boxed_slice()
    }
}

impl InstrSequence {
    /// Returns `self` as vector of [`Instruction`]s.
    pub fn into_vec(self) -> Vec<Instruction> {
        self.instrs
    }

    /// Returns `self` as boxed slice of [`Instruction`]s.
    pub fn into_boxed_slice(self) -> Box<[Instruction]> {
        self.instrs.into_boxed_slice()
    }

    /// Clears all [`Instruction`]s from `self` emptying `self` in the process.
    ///
    /// # Note
    ///
    /// This invalidates all [`Instr`] references to `self`.
    pub fn clear(&mut self) {
        self.instrs.clear()
    }

    /// Returns the underlying [`Instruction`]s as shared slice.
    pub fn as_slice(&self) -> &[Instruction] {
        &self.instrs[..]
    }

    /// Returns the underlying [`Instruction`]s as mutable slice.
    pub fn as_slice_mut(&mut self) -> &mut [Instruction] {
        &mut self.instrs[..]
    }

    /// Returns the number of [`Instruction`] in `self`.
    #[inline]
    pub fn len(&self) -> usize {
        self.instrs.len()
    }

    /// Returns `true` if `self` is empty.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns the [`Instruction`] that is associated to `instr`.
    #[inline]
    pub fn get(&self, instr: Instr) -> Option<Instruction> {
        self.instrs.get(instr.into_usize()).copied()
    }

    /// Returns a mutable reference to the [`Instruction`] that is associated to `instr`.
    #[inline]
    pub fn get_mut(&mut self, instr: Instr) -> Option<&mut Instruction> {
        self.instrs.get_mut(instr.into_usize())
    }

    /// Returns an iterator yielding the [`Instruction`] of the [`InstrSequence`].
    pub fn iter(&self) -> InstrIter {
        InstrIter::new(self)
    }

    /// Returns an iterator yielding mutable [`Instruction`] of the [`InstrSequence`].
    pub fn iter_mut(&mut self) -> InstrIterMut {
        InstrIterMut::new(self)
    }

    /// Pops the last [`Instruction`] in `self` if any.
    ///
    /// Returns `None` if `self` is empty.
    pub fn pop(&mut self) -> Option<Instruction> {
        self.instrs.pop()
    }
}

impl<'a> IntoIterator for &'a InstrSequence {
    type Item = &'a Instruction;
    type IntoIter = InstrIter<'a>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl<'a> IntoIterator for &'a mut InstrSequence {
    type Item = &'a mut Instruction;
    type IntoIter = InstrIterMut<'a>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.iter_mut()
    }
}

macro_rules! define_builder {
    (
        $(
            $( #[doc = $doc:literal] )*
            #[snake_name($snake_name:ident)]
            $name:ident
            $(
                {
                    // $( @result )?
                    // $( @results )?
                    $(
                        $( #[$field_docs:meta] )* $(@)?
                        $field_name:ident: $field_ty:ty
                    ),*
                    $(,)?
                }
            )?
        ),* $(,)?
    ) => {
        impl InstrSequence {
            $(
                #[doc = concat!("Pushes an [`Instruction::", stringify!($name), "`].")]
                ///
                /// Returns the [`Instr`] to query the pushed [`Instruction`].
                pub fn $snake_name(
                    &mut self,
                    $( $( $field_name: impl Into<$field_ty> ),* )?
                ) -> Instr {
                    let pos = Instr::from_usize(self.instrs.len());
                    self.instrs.push(Instruction::$name {
                        $( $( $field_name: $field_name.into() ),* )?
                    });
                    pos
                }
            )*
        }
    };
}
for_each_op!(define_builder);

/// Iterator yielding the [`Instruction`]s of an [`InstrSequence`].
#[derive(Debug)]
pub struct InstrIter<'a> {
    instrs: slice::Iter<'a, Instruction>,
}

impl<'a> InstrIter<'a> {
    /// Creates a new [`InstrIter`] for the [`InstrSequence`].
    fn new(builder: &'a InstrSequence) -> Self {
        Self {
            instrs: builder.instrs.iter(),
        }
    }
}

impl<'a> Iterator for InstrIter<'a> {
    type Item = &'a Instruction;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        self.instrs.next()
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.instrs.size_hint()
    }
}

impl DoubleEndedIterator for InstrIter<'_> {
    fn next_back(&mut self) -> Option<Self::Item> {
        self.instrs.next_back()
    }
}

impl ExactSizeIterator for InstrIter<'_> {}

/// Iterator yielding the [`Instruction`]s of an [`InstrSequence`] mutably.
#[derive(Debug)]
pub struct InstrIterMut<'a> {
    instrs: slice::IterMut<'a, Instruction>,
}

impl<'a> InstrIterMut<'a> {
    /// Creates a new [`InstrIter`] for the [`InstrSequence`].
    fn new(builder: &'a mut InstrSequence) -> Self {
        Self {
            instrs: builder.instrs.iter_mut(),
        }
    }
}

impl<'a> Iterator for InstrIterMut<'a> {
    type Item = &'a mut Instruction;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        self.instrs.next()
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.instrs.size_hint()
    }
}

impl DoubleEndedIterator for InstrIterMut<'_> {
    fn next_back(&mut self) -> Option<Self::Item> {
        self.instrs.next_back()
    }
}

impl ExactSizeIterator for InstrIterMut<'_> {}