use crate::types::branch;
use super::{Decode, Decoder, Error};
pub fn read_address<U>(decoder: &mut Decoder<U>) -> Result<u64, Error> {
let lsb = decoder.field_widths.iaddress_lsb.get();
let width = decoder.field_widths.iaddress.get().saturating_sub(lsb);
decoder.read_bits::<u64>(width).map(|v| v << lsb)
}
pub fn read_implicit_return<U>(decoder: &mut Decoder<U>) -> Result<Option<usize>, Error> {
let report = decoder.read_differential_bit()?;
let depth = decoder
.field_widths
.stack_depth
.map(|w| decoder.read_bits(w.get()))
.transpose()?;
if report {
Ok(depth)
} else {
Ok(None)
}
}
#[derive(Copy, Clone, Debug)]
pub struct BranchCount(pub u8);
impl BranchCount {
pub fn is_zero(self) -> bool {
self.0 == 0
}
pub fn read_branch_map<U>(self, decoder: &mut Decoder<U>) -> Result<branch::Map, Error> {
let length = core::iter::successors(Some(31), |l| (*l > 0).then_some(l >> 1))
.take_while(|l| *l >= self.0)
.last()
.expect("Could not determine length");
let mut map = decoder.read_bits(length)?;
map &= !0u64.checked_shl(self.0.into()).unwrap_or_default();
Ok(branch::Map::new(self.0, map))
}
pub const FULL: Self = Self(31);
}
impl<U> Decode<'_, '_, U> for BranchCount {
fn decode(decoder: &mut Decoder<U>) -> Result<Self, Error> {
decoder.read_bits(5).map(Self)
}
}