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
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//! Intrinsics are instructions we cannot model with Falcon.

use il::*;
use std::fmt;

/// An Instrinsic is a lifted instruction we cannot model.
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub struct Intrinsic {
    mnemonic: String,
    instruction_str: String,
    arguments: Vec<Expression>,
    written_expressions: Option<Vec<Expression>>,
    read_expressions: Option<Vec<Expression>>,
    bytes: Vec<u8>
}


impl Intrinsic {
    pub fn new<S: Into<String>, SS: Into<String>>(
        mnemonic: S,
        instruction_str: SS,
        arguments: Vec<Expression>,
        written_expressions: Option<Vec<Expression>>,
        read_expressions: Option<Vec<Expression>>,
        bytes: Vec<u8>
    ) -> Intrinsic {
        Intrinsic {
            mnemonic: mnemonic.into(),
            instruction_str: instruction_str.into(),
            arguments: arguments,
            written_expressions: written_expressions,
            read_expressions: read_expressions,
            bytes: bytes.clone()
        }
    }

    pub fn mnemonic(&self) -> &str {
        &self.mnemonic
    }

    pub fn instruction_str(&self) -> &str {
        &self.instruction_str
    }

    pub fn arguments(&self) -> &[Expression] {
        &self.arguments
    }

    pub fn written_expressions(&self) -> Option<&[Expression]> {
        self.written_expressions.as_ref().map(|x| x.as_slice())
    }

    pub fn written_expressions_mut(&mut self) -> Option<&mut [Expression]> {
        self.written_expressions.as_mut().map(|x| x.as_mut_slice())
    }

    pub fn read_expressions(&self) -> Option<&[Expression]> {
        self.read_expressions.as_ref().map(|x| x.as_slice())
    }

    pub fn read_expressions_mut(&mut self) -> Option<&mut [Expression]> {
        self.read_expressions.as_mut().map(|x| x.as_mut_slice())
    }

    pub fn scalars_written(&self) -> Vec<&Scalar> {
        self.written_expressions()
            .map(|written_expressions|
                written_expressions
                    .iter()
                    .flat_map(|expression| expression.scalars())
                    .collect::<Vec<&Scalar>>())
            .unwrap_or(Vec::new())
    }

    pub fn scalars_written_mut(&mut self) -> Vec<&mut Scalar> {
        self.written_expressions_mut()
            .map(|written_expressions|
                written_expressions
                    .iter_mut()
                    .flat_map(|expression| expression.scalars_mut())
                    .collect::<Vec<&mut Scalar>>())
            .unwrap_or(Vec::new())
    }

    pub fn scalars_read(&self) -> Vec<&Scalar> {
        self.read_expressions()
            .map(|read_expressions|
                read_expressions
                    .iter()
                    .flat_map(|expression| expression.scalars())
                    .collect::<Vec<&Scalar>>())
            .unwrap_or(Vec::new())
    }

    pub fn scalars_read_mut(&mut self) -> Vec<&mut Scalar> {
        self.read_expressions_mut()
            .map(|read_expressions|
                read_expressions
                    .iter_mut()
                    .flat_map(|expression| expression.scalars_mut())
                    .collect::<Vec<&mut Scalar>>())
            .unwrap_or(Vec::new())
    }

    pub fn bytes(&self) -> &[u8] {
        &self.bytes
    }
}


impl fmt::Display for Intrinsic {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let bytes = self.bytes()
            .into_iter()
            .map(|byte| format!("{:02x}", byte))
            .collect::<Vec<String>>()
            .join("");
        write!(f, "{} {}", bytes, self.instruction_str())
    }
}