pulley_interpreter/
imms.rs

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
//! Immediates.

use core::fmt;

/// A PC-relative offset.
///
/// This is relative to the start of this offset's containing instruction.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PcRelOffset(i32);

#[cfg(feature = "arbitrary")]
impl<'a> arbitrary::Arbitrary<'a> for PcRelOffset {
    fn arbitrary(_u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
        // We can't possibly choose valid offsets for jumping to, so just use
        // zero as the least dangerous option. It is up to whoever is generating
        // arbitrary ops to clean this up.
        Ok(Self(0))
    }
}

impl From<i32> for PcRelOffset {
    #[inline]
    fn from(offset: i32) -> Self {
        PcRelOffset(offset)
    }
}

impl From<PcRelOffset> for i32 {
    #[inline]
    fn from(offset: PcRelOffset) -> Self {
        offset.0
    }
}

/// A 6-byte unsigned integer.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct U6(u8);

impl U6 {
    /// Attempts to create a new `U6` from the provided byte
    pub fn new(val: u8) -> Option<U6> {
        if val << 2 >> 2 == val {
            Some(U6(val))
        } else {
            None
        }
    }
}

#[cfg(feature = "arbitrary")]
impl<'a> arbitrary::Arbitrary<'a> for U6 {
    fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
        let byte = u.arbitrary::<u8>()?;
        Ok(U6(byte << 2 >> 2))
    }
}

impl From<U6> for u8 {
    #[inline]
    fn from(val: U6) -> Self {
        val.0
    }
}

impl fmt::Display for U6 {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        u8::from(*self).fmt(f)
    }
}