midenc-hir 0.8.1

High-level Intermediate Representation for Miden Assembly
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
use core::borrow::Borrow;

use smallvec::{SmallVec, smallvec};

use super::*;
use crate::adt::SmallSet;

/// An abstraction over the different type of [Value]-derived entity ranges, e.g. operands, results,
/// block arguments, vs type-erased collections.
///
/// This range types supports fewer conveniences than [EntityRange] or raw slices provide, as we
/// are not able to handle all ranges exactly the same (for example, borrowing an element from the
/// range works for all but op operands, as those have an extra layer of indirection).
///
/// In general, this should be used in only narrow circumstances where a more specific range type
/// cannot be used.
#[derive(Default)]
pub enum ValueRange<'a, const N: usize = 2> {
    /// A default-initialized empty range
    #[default]
    Empty,
    /// The values in the range are type-erased, but owned
    Owned(SmallVec<[ValueRef; N]>),
    /// The values in the range are type-erased, but borrowed
    Borrowed(&'a [ValueRef]),
    /// The value range contains block arguments
    BlockArguments(&'a [BlockArgumentRef]),
    /// The value range contains operands
    Operands(&'a [OpOperand]),
    /// The value range contains results
    Results(&'a [OpResultRef]),
}

impl<'values, const N: usize> ValueRange<'values, N> {
    /// Returns true if this range is empty
    pub fn is_empty(&self) -> bool {
        match self {
            Self::Empty => true,
            Self::Owned(values) => values.is_empty(),
            Self::Borrowed(values) => values.is_empty(),
            Self::BlockArguments(range) => range.is_empty(),
            Self::Operands(range) => range.is_empty(),
            Self::Results(range) => range.is_empty(),
        }
    }

    /// Returns the number of values in the range
    pub fn len(&self) -> usize {
        match self {
            Self::Empty => 0,
            Self::Owned(values) => values.len(),
            Self::Borrowed(values) => values.len(),
            Self::BlockArguments(range) => range.len(),
            Self::Operands(range) => range.len(),
            Self::Results(range) => range.len(),
        }
    }

    pub fn slice<'a, 'b: 'a + 'values, R>(&'b self, range: R) -> ValueRange<'a, N>
    where
        R: core::slice::SliceIndex<[ValueRef], Output = [ValueRef]>,
        R: core::slice::SliceIndex<[BlockArgumentRef], Output = [BlockArgumentRef]>,
        R: core::slice::SliceIndex<[OpOperand], Output = [OpOperand]>,
        R: core::slice::SliceIndex<[OpResultRef], Output = [OpResultRef]>,
    {
        match self {
            Self::Empty => ValueRange::Empty,
            Self::Owned(values) => Self::Borrowed(&values[range]),
            Self::Borrowed(values) => Self::Borrowed(&values[range]),
            Self::BlockArguments(values) => Self::BlockArguments(&values[range]),
            Self::Operands(values) => Self::Operands(&values[range]),
            Self::Results(values) => Self::Results(&values[range]),
        }
    }

    /// Returns the value at `index` in the range
    pub fn get(&self, index: usize) -> Option<ValueRef> {
        match self {
            Self::Empty => None,
            Self::Owned(values) => values.get(index).cloned(),
            Self::Borrowed(values) => values.get(index).cloned(),
            Self::BlockArguments(range) => {
                range.get(index).map(|operand| operand.borrow().as_value_ref())
            }
            Self::Operands(range) => {
                range.get(index).map(|operand| operand.borrow().as_value_ref())
            }
            Self::Results(range) => range.get(index).map(|result| result.borrow().as_value_ref()),
        }
    }

    /// Returns true if `value` is present in this range
    pub fn contains<V>(&self, value: V) -> bool
    where
        V: Borrow<ValueRef>,
    {
        let value = value.borrow();
        match self {
            Self::Empty => false,
            Self::Owned(values) => values.contains(value),
            Self::Borrowed(values) => values.contains(value),
            Self::BlockArguments(args) => args.iter().copied().any(|arg| arg as ValueRef == *value),
            Self::Operands(operands) => {
                operands.iter().any(|operand| operand.borrow().as_value_ref() == *value)
            }
            Self::Results(results) => {
                results.iter().copied().any(|result| result as ValueRef == *value)
            }
        }
    }

    /// Iterate over the values in this range as [ValueRef]s
    pub fn iter(&self) -> ValueRangeIter<'_, N> {
        match self {
            Self::Empty => ValueRangeIter::new(ValueRange::Borrowed(&[])),
            Self::Owned(values) => ValueRangeIter::new(ValueRange::Borrowed(values.as_slice())),
            Self::Borrowed(values) => ValueRangeIter::new(ValueRange::Borrowed(values)),
            Self::BlockArguments(values) => ValueRangeIter::new(ValueRange::BlockArguments(values)),
            Self::Operands(values) => ValueRangeIter::new(ValueRange::Operands(values)),
            Self::Results(values) => ValueRangeIter::new(ValueRange::Results(values)),
        }
    }

    /// Convert this into an owned [ValueRange] with static lifetime
    pub fn into_owned(self) -> ValueRange<'static, N> {
        ValueRange::Owned(self.into_smallvec())
    }

    /// Convert this into an owned [SmallVec] of the underlying values.
    pub fn into_smallvec(self) -> SmallVec<[ValueRef; N]> {
        match self {
            Self::Empty => smallvec![],
            Self::Owned(values) => values,
            Self::Borrowed(values) => SmallVec::from_slice(values),
            Self::BlockArguments(args) => args.iter().copied().map(|arg| arg as ValueRef).collect(),
            Self::Operands(operands) => {
                operands.iter().map(|operand| operand.borrow().as_value_ref()).collect()
            }
            Self::Results(results) => {
                results.iter().copied().map(|result| result as ValueRef).collect()
            }
        }
    }

    /// Obtain a [alloc::vec::Vec] from this range.
    pub fn to_vec(&self) -> alloc::vec::Vec<ValueRef> {
        match self {
            Self::Empty => Default::default(),
            Self::Owned(values) => values.to_vec(),
            Self::Borrowed(values) => values.to_vec(),
            Self::BlockArguments(args) => args.iter().copied().map(|arg| arg as ValueRef).collect(),
            Self::Operands(operands) => {
                operands.iter().map(|operand| operand.borrow().as_value_ref()).collect()
            }
            Self::Results(results) => {
                results.iter().copied().map(|result| result as ValueRef).collect()
            }
        }
    }
}

impl<const N: usize> core::fmt::Debug for ValueRange<'_, N> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        fmt::Display::fmt(self, f)
    }
}

impl<const N: usize> core::fmt::Display for ValueRange<'_, N> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let mut builder = f.debug_list();
        for value in self.iter() {
            builder.entry_with(|f| write!(f, "{value}"));
        }

        builder.finish()
    }
}

impl<const N: usize> FromIterator<ValueRef> for ValueRange<'static, N> {
    fn from_iter<I>(iter: I) -> Self
    where
        I: IntoIterator<Item = ValueRef>,
    {
        Self::Owned(SmallVec::from_iter(iter))
    }
}

impl<const N: usize> FromIterator<BlockArgumentRef> for ValueRange<'static, N> {
    fn from_iter<I>(iter: I) -> Self
    where
        I: IntoIterator<Item = BlockArgumentRef>,
    {
        Self::from_iter(iter.into_iter().map(|arg| arg as ValueRef))
    }
}

impl<const N: usize> FromIterator<OpResultRef> for ValueRange<'static, N> {
    fn from_iter<I>(iter: I) -> Self
    where
        I: IntoIterator<Item = OpResultRef>,
    {
        Self::from_iter(iter.into_iter().map(|result| result as ValueRef))
    }
}

impl<const N: usize> FromIterator<OpOperand> for ValueRange<'static, N> {
    fn from_iter<I>(iter: I) -> Self
    where
        I: IntoIterator<Item = OpOperand>,
    {
        Self::from_iter(iter.into_iter().map(|operand| operand.borrow().as_value_ref()))
    }
}

impl<'a, const N: usize> IntoIterator for ValueRange<'a, N> {
    type IntoIter = ValueRangeIter<'a, N>;
    type Item = ValueRef;

    fn into_iter(self) -> Self::IntoIter {
        ValueRangeIter::new(self)
    }
}

impl<'a, 'b: 'a, const N: usize> IntoIterator for &'a ValueRange<'b, N> {
    type IntoIter = ValueRangeIter<'a, N>;
    type Item = ValueRef;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl<const N: usize> From<SmallVec<[ValueRef; N]>> for ValueRange<'static, N> {
    fn from(values: SmallVec<[ValueRef; N]>) -> Self {
        Self::Owned(SmallVec::from_slice(&values))
    }
}

impl<'a, const M: usize, const N: usize> From<&'a SmallVec<[ValueRef; M]>> for ValueRange<'a, N> {
    fn from(values: &'a SmallVec<[ValueRef; M]>) -> Self {
        Self::Borrowed(values.as_slice())
    }
}

impl<const N: usize> From<SmallSet<ValueRef, N>> for ValueRange<'static, N> {
    fn from(values: SmallSet<ValueRef, N>) -> Self {
        Self::Owned(values.into_vec())
    }
}

impl<'a, const M: usize, const N: usize> From<&'a SmallSet<ValueRef, M>> for ValueRange<'a, N> {
    fn from(values: &'a SmallSet<ValueRef, M>) -> Self {
        Self::Borrowed(values.as_slice())
    }
}

impl<'a, const N: usize> From<&'a [ValueRef]> for ValueRange<'a, N> {
    fn from(values: &'a [ValueRef]) -> Self {
        Self::Borrowed(values)
    }
}

impl<'a, const N: usize> From<BlockArgumentRange<'a>> for ValueRange<'a, N> {
    fn from(range: BlockArgumentRange<'a>) -> Self {
        Self::BlockArguments(range.as_slice())
    }
}

impl<'a, const N: usize> From<&'a [BlockArgumentRef]> for ValueRange<'a, N> {
    fn from(range: &'a [BlockArgumentRef]) -> Self {
        Self::BlockArguments(range)
    }
}

impl<'a, const N: usize> From<OpOperandRange<'a>> for ValueRange<'a, N> {
    fn from(range: OpOperandRange<'a>) -> Self {
        Self::Operands(range.as_slice())
    }
}

impl<'a, const N: usize> From<&'a [OpOperand]> for ValueRange<'a, N> {
    fn from(range: &'a [OpOperand]) -> Self {
        Self::Operands(range)
    }
}

impl<'a, const N: usize> From<OpResultRange<'a>> for ValueRange<'a, N> {
    fn from(range: OpResultRange<'a>) -> Self {
        Self::Results(range.as_slice())
    }
}

impl<'a, const N: usize> From<&'a [OpResultRef]> for ValueRange<'a, N> {
    fn from(range: &'a [OpResultRef]) -> Self {
        Self::Results(range)
    }
}

impl<'a, const N: usize> From<SuccessorOperandRange<'a>> for ValueRange<'a, N> {
    fn from(range: SuccessorOperandRange<'a>) -> Self {
        Self::from(range.into_forwarded())
    }
}

pub trait AsValueRange {
    fn as_value_range(&self) -> ValueRange<'_, 2>;
}

impl<T: AsValueRange> AsValueRange for Option<T> {
    fn as_value_range(&self) -> ValueRange<'_, 2> {
        match self.as_ref() {
            Some(values) => values.as_value_range(),
            None => ValueRange::Empty,
        }
    }
}

impl AsValueRange for [ValueRef] {
    fn as_value_range(&self) -> ValueRange<'_, 2> {
        ValueRange::Borrowed(self)
    }
}

impl AsValueRange for [BlockArgumentRef] {
    fn as_value_range(&self) -> ValueRange<'_, 2> {
        ValueRange::BlockArguments(self)
    }
}

impl AsValueRange for [OpOperand] {
    fn as_value_range(&self) -> ValueRange<'_, 2> {
        ValueRange::Operands(self)
    }
}

impl AsValueRange for [OpResultRef] {
    fn as_value_range(&self) -> ValueRange<'_, 2> {
        ValueRange::Results(self)
    }
}

impl AsValueRange for alloc::vec::Vec<ValueRef> {
    fn as_value_range(&self) -> ValueRange<'_, 2> {
        ValueRange::Borrowed(self.as_slice())
    }
}

impl<const N: usize> AsValueRange for SmallVec<[ValueRef; N]> {
    fn as_value_range(&self) -> ValueRange<'_, 2> {
        ValueRange::Borrowed(self.as_slice())
    }
}

impl AsValueRange for OpOperandStorage {
    fn as_value_range(&self) -> ValueRange<'_, 2> {
        ValueRange::Operands(self.all().as_slice())
    }
}

impl AsValueRange for OpResultStorage {
    fn as_value_range(&self) -> ValueRange<'_, 2> {
        ValueRange::Results(self.all().as_slice())
    }
}

impl AsValueRange for OpOperandRange<'_> {
    fn as_value_range(&self) -> ValueRange<'_, 2> {
        ValueRange::Operands(self.as_slice())
    }
}

impl AsValueRange for OpOperandRangeMut<'_> {
    fn as_value_range(&self) -> ValueRange<'_, 2> {
        ValueRange::Operands(self.as_slice())
    }
}

impl AsValueRange for OpResultRange<'_> {
    fn as_value_range(&self) -> ValueRange<'_, 2> {
        ValueRange::Results(self.as_slice())
    }
}

impl AsValueRange for OpResultRangeMut<'_> {
    fn as_value_range(&self) -> ValueRange<'_, 2> {
        ValueRange::Results(self.as_slice())
    }
}

impl AsValueRange for BlockArgumentRange<'_> {
    fn as_value_range(&self) -> ValueRange<'_, 2> {
        ValueRange::BlockArguments(self.as_slice())
    }
}

impl AsValueRange for BlockArgumentRangeMut<'_> {
    fn as_value_range(&self) -> ValueRange<'_, 2> {
        ValueRange::BlockArguments(self.as_slice())
    }
}

/// An iterator consuming the contents of a [ValueRange]
pub struct ValueRangeIter<'a, const N: usize> {
    range: ValueRange<'a, N>,
    index: usize,
}

impl<'a, const N: usize> ValueRangeIter<'a, N> {
    pub fn new(range: ValueRange<'a, N>) -> Self {
        Self { range, index: 0 }
    }
}

impl<const N: usize> core::iter::FusedIterator for ValueRangeIter<'_, N> {}
impl<const N: usize> ExactSizeIterator for ValueRangeIter<'_, N> {
    fn len(&self) -> usize {
        let len = self.range.len();
        len.saturating_sub(self.index)
    }
}
impl<const N: usize> Iterator for ValueRangeIter<'_, N> {
    type Item = ValueRef;

    fn next(&mut self) -> Option<Self::Item> {
        let len = self.range.len();
        if self.index >= len {
            return None;
        }

        let index = self.index;
        self.index += 1;
        match &self.range {
            ValueRange::Empty => None,
            ValueRange::Owned(values) => values.get(index).cloned(),
            ValueRange::Borrowed(values) => values.get(index).cloned(),
            ValueRange::BlockArguments(range) => {
                range.get(index).map(|o| o.borrow().as_value_ref())
            }
            ValueRange::Operands(range) => range.get(index).map(|o| o.borrow().as_value_ref()),
            ValueRange::Results(range) => range.get(index).map(|o| o.borrow().as_value_ref()),
        }
    }
}

impl<const N: usize> DoubleEndedIterator for ValueRangeIter<'_, N> {
    fn next_back(&mut self) -> Option<Self::Item> {
        let len = self.range.len();
        let index = len.checked_sub(self.index + 1)?;

        self.index += 1;
        match &self.range {
            ValueRange::Empty => None,
            ValueRange::Owned(values) => values.get(index).cloned(),
            ValueRange::Borrowed(values) => values.get(index).cloned(),
            ValueRange::BlockArguments(range) => {
                range.get(index).map(|o| o.borrow().as_value_ref())
            }
            ValueRange::Operands(range) => range.get(index).map(|o| o.borrow().as_value_ref()),
            ValueRange::Results(range) => range.get(index).map(|o| o.borrow().as_value_ref()),
        }
    }
}