rune/compile/v1/
linear.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
use core::array;
use core::fmt;
use core::ops::{Deref, DerefMut};
use core::slice;

use crate::alloc::{vec, Vec};
use crate::compile;
use crate::runtime::InstAddress;

use super::Address;

enum Addresses<'a, 'hir> {
    Single(Address<'a, 'hir>),
    List(Vec<Address<'a, 'hir>>),
}

#[must_use = "This should be freed with a call to Scopes::free_linear"]
pub(super) struct Linear<'a, 'hir> {
    addresses: Addresses<'a, 'hir>,
}

impl<'a, 'hir> Linear<'a, 'hir> {
    /// Construct a new linear address space.
    pub(super) fn new(list: Vec<Address<'a, 'hir>>) -> Self {
        Self {
            addresses: Addresses::List(list),
        }
    }

    /// Construct an empty linear allocation.
    pub(super) const fn empty() -> Self {
        Self {
            addresses: Addresses::List(Vec::new()),
        }
    }

    /// Represent a single address.
    pub(super) fn single(address: Address<'a, 'hir>) -> Self {
        Self {
            addresses: Addresses::Single(address),
        }
    }

    #[inline]
    pub(super) fn addr(&self) -> InstAddress {
        match &self.addresses {
            Addresses::Single(address) => address.addr(),
            Addresses::List(list) => list.first().map_or(InstAddress::INVALID, Address::addr),
        }
    }

    #[inline]
    pub(super) fn iter(&self) -> slice::Iter<'_, Address<'a, 'hir>> {
        <[_]>::iter(self)
    }

    #[inline]
    pub(super) fn iter_mut(&mut self) -> slice::IterMut<'_, Address<'a, 'hir>> {
        <[_]>::iter_mut(self)
    }

    #[inline]
    pub(super) fn free(self) -> compile::Result<()> {
        for addr in self.into_iter().rev() {
            addr.free()?;
        }

        Ok(())
    }

    #[inline]
    pub(super) fn free_non_dangling(self) -> compile::Result<()> {
        for addr in self.into_iter().rev() {
            addr.free_non_dangling()?;
        }

        Ok(())
    }

    #[inline]
    pub(super) fn forget(self) -> compile::Result<()> {
        for var in self {
            var.forget()?;
        }

        Ok(())
    }
}

impl<'b, 'a, 'hir> IntoIterator for &'b Linear<'a, 'hir> {
    type Item = &'b Address<'a, 'hir>;
    type IntoIter = slice::Iter<'b, Address<'a, 'hir>>;

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

impl<'b, 'a, 'hir> IntoIterator for &'b mut Linear<'a, 'hir> {
    type Item = &'b mut Address<'a, 'hir>;
    type IntoIter = slice::IterMut<'b, Address<'a, 'hir>>;

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

impl<'a, 'hir> Deref for Linear<'a, 'hir> {
    type Target = [Address<'a, 'hir>];

    #[inline]
    fn deref(&self) -> &Self::Target {
        match &self.addresses {
            Addresses::Single(address) => slice::from_ref(address),
            Addresses::List(list) => list,
        }
    }
}

impl DerefMut for Linear<'_, '_> {
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        match &mut self.addresses {
            Addresses::Single(address) => slice::from_mut(address),
            Addresses::List(list) => list,
        }
    }
}

impl fmt::Debug for Linear<'_, '_> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_list().entries(self.iter()).finish()
    }
}

impl<'a, 'hir> IntoIterator for Linear<'a, 'hir> {
    type Item = Address<'a, 'hir>;
    type IntoIter = IntoIter<'a, 'hir>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        IntoIter {
            inner: match self.addresses {
                Addresses::Single(address) => IntoIterRepr::Single([address].into_iter()),
                Addresses::List(list) => IntoIterRepr::Vec(list.into_iter()),
            },
        }
    }
}

enum IntoIterRepr<'a, 'hir> {
    Vec(vec::IntoIter<Address<'a, 'hir>>),
    Single(array::IntoIter<Address<'a, 'hir>, 1>),
}

pub(super) struct IntoIter<'a, 'hir> {
    inner: IntoIterRepr<'a, 'hir>,
}

impl<'a, 'hir> Iterator for IntoIter<'a, 'hir> {
    type Item = Address<'a, 'hir>;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        match &mut self.inner {
            IntoIterRepr::Vec(iter) => iter.next(),
            IntoIterRepr::Single(iter) => iter.next(),
        }
    }
}

impl ExactSizeIterator for IntoIter<'_, '_> {
    #[inline]
    fn len(&self) -> usize {
        match &self.inner {
            IntoIterRepr::Vec(iter) => iter.len(),
            IntoIterRepr::Single(iter) => iter.len(),
        }
    }
}

impl DoubleEndedIterator for IntoIter<'_, '_> {
    #[inline]
    fn next_back(&mut self) -> Option<Self::Item> {
        match &mut self.inner {
            IntoIterRepr::Vec(iter) => iter.next_back(),
            IntoIterRepr::Single(iter) => iter.next_back(),
        }
    }
}