fidget_core/compiler/
reg_tape.rs1use crate::compiler::{RegOp, RegisterAllocator, SsaTape};
3use serde::{Deserialize, Serialize};
4
5#[derive(Clone, Default, Serialize, Deserialize)]
8pub struct RegTape {
9 tape: Vec<RegOp>,
10
11 pub(super) slot_count: u32,
13}
14
15impl RegTape {
16 pub fn new<const N: usize>(ssa: &SsaTape) -> Self {
23 let mut alloc = RegisterAllocator::<N>::new(ssa.len());
24 for &op in ssa.iter() {
25 alloc.op(op)
26 }
27 alloc.finalize()
28 }
29
30 pub(crate) fn empty() -> Self {
32 Self {
33 tape: vec![],
34 slot_count: 0,
35 }
36 }
37
38 pub fn reset(&mut self) {
40 self.tape.clear();
41 self.slot_count = 0;
42 }
43
44 #[inline]
47 pub fn slot_count(&self) -> usize {
48 self.slot_count as usize
49 }
50 #[inline]
52 pub fn len(&self) -> usize {
53 self.tape.len()
54 }
55 #[inline]
57 pub fn is_empty(&self) -> bool {
58 self.tape.is_empty()
59 }
60 #[inline]
65 pub fn iter(&self) -> impl DoubleEndedIterator<Item = &RegOp> {
66 self.into_iter()
67 }
68 #[inline]
69 pub(crate) fn push(&mut self, op: RegOp) {
70 self.tape.push(op)
71 }
72}
73
74impl<'a> IntoIterator for &'a RegTape {
75 type Item = &'a RegOp;
76 type IntoIter = std::slice::Iter<'a, RegOp>;
77 fn into_iter(self) -> Self::IntoIter {
78 self.tape.iter()
79 }
80}