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
use crate::{BinaryReader, FromReader, Result, SectionLimited};

/// A reader for the `metadata.code.branch_hint` custom section.
pub type BranchHintSectionReader<'a> = SectionLimited<'a, BranchHintFunction<'a>>;

/// Branch hints for a single function.
///
/// Produced from [`BranchHintSectionReader`].
#[derive(Debug, Clone)]
pub struct BranchHintFunction<'a> {
    /// The function that these branch hints apply to.
    pub func: u32,
    /// The branch hints available for this function.
    pub hints: SectionLimited<'a, BranchHint>,
}

impl<'a> FromReader<'a> for BranchHintFunction<'a> {
    fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
        let func = reader.read_var_u32()?;
        // FIXME(#188) ideally wouldn't have to do skips here
        let hints = reader.skip(|reader| {
            let items_count = reader.read_var_u32()?;
            for _ in 0..items_count {
                reader.read::<BranchHint>()?;
            }
            Ok(())
        })?;
        Ok(BranchHintFunction {
            func,
            hints: SectionLimited::new(hints.remaining_buffer(), hints.original_position())?,
        })
    }
}

/// A hint for a single branch.
#[derive(Debug, Copy, Clone)]
pub struct BranchHint {
    /// The byte offset, from the start of the function's body, of where the
    /// hinted instruction lives.
    pub func_offset: u32,
    /// Whether or not the branch is hinted to be taken or not.
    pub taken: bool,
}

impl<'a> FromReader<'a> for BranchHint {
    fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
        let func_offset = reader.read_var_u32()?;
        match reader.read_u8()? {
            1 => {}
            n => reader.invalid_leading_byte(n, "invalid branch hint byte")?,
        }
        let taken = match reader.read_u8()? {
            0 => false,
            1 => true,
            n => reader.invalid_leading_byte(n, "invalid branch hint taken byte")?,
        };
        Ok(BranchHint { func_offset, taken })
    }
}