use crate::compiler::{RegOp, RegisterAllocator, SsaTape};
use serde::{Deserialize, Serialize};
#[derive(Clone, Default, Serialize, Deserialize)]
pub struct RegTape {
tape: Vec<RegOp>,
pub(super) slot_count: u32,
}
impl RegTape {
pub fn new<const N: usize>(ssa: &SsaTape) -> Self {
let mut alloc = RegisterAllocator::<N>::new(ssa.len());
for &op in ssa.iter() {
alloc.op(op)
}
alloc.finalize()
}
pub(crate) fn empty() -> Self {
Self {
tape: vec![],
slot_count: 0,
}
}
pub fn reset(&mut self) {
self.tape.clear();
self.slot_count = 0;
}
#[inline]
pub fn slot_count(&self) -> usize {
self.slot_count as usize
}
#[inline]
pub fn len(&self) -> usize {
self.tape.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.tape.is_empty()
}
#[inline]
pub fn iter(&self) -> impl DoubleEndedIterator<Item = &RegOp> {
self.into_iter()
}
#[inline]
pub(crate) fn push(&mut self, op: RegOp) {
self.tape.push(op)
}
}
impl<'a> IntoIterator for &'a RegTape {
type Item = &'a RegOp;
type IntoIter = std::slice::Iter<'a, RegOp>;
fn into_iter(self) -> Self::IntoIter {
self.tape.iter()
}
}