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
//! Contains EVM-specific types

use core::fmt;

use capstone_sys::cs_evm;

// XXX todo(tmfink): create rusty versions
pub use capstone_sys::evm_insn_group as EvmInsnGroup;
pub use capstone_sys::evm_insn as EvmInsn;

pub use crate::arch::arch_builder::evm::*;
use crate::arch::DetailsArchInsn;

/// Contains EVM-specific details for an instruction
pub struct EvmInsnDetail<'a>(pub(crate) &'a cs_evm);

impl<'a> EvmInsnDetail<'a> {
    /// Number of items popped from the stack
    pub fn popped_items(&self) -> u8 {
        self.0.pop
    }

    /// Number of items pushed into the stack
    pub fn pushed_items(&self) -> u8 {
        self.0.push
    }

    /// Gas fee for the instruction
    pub fn fee(&self) -> u32 {
        self.0.fee as u32
    }
}

impl_PartialEq_repr_fields!(EvmInsnDetail<'a> [ 'a ];
    popped_items, pushed_items, fee
);

/// EVM has no operands, so this is a zero-size type.
#[derive(Clone, Debug, Eq, PartialEq, Default)]
pub struct EvmOperand(());

// Do not use def_arch_details_struct! since EVM does not have operands

/// Iterates over instruction operands
#[derive(Clone)]
pub struct EvmOperandIterator(());

impl EvmOperandIterator {
    fn new() -> EvmOperandIterator {
        EvmOperandIterator(())
    }
}

impl Iterator for EvmOperandIterator {
    type Item = EvmOperand;

    fn next(&mut self) -> Option<Self::Item> {
        None
    }
}

impl ExactSizeIterator for EvmOperandIterator {
    fn len(&self) -> usize {
        0
    }
}

impl PartialEq for EvmOperandIterator {
    fn eq(&self, _other: &EvmOperandIterator) -> bool {
        false
    }
}

impl fmt::Debug for EvmOperandIterator {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> ::core::fmt::Result {
        fmt.debug_struct("EvmOperandIterator").finish()
    }
}

impl<'a> fmt::Debug for EvmInsnDetail<'a> {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> ::core::fmt::Result {
        fmt.debug_struct("EvmInsnDetail")
            .field("cs_evm", &(self.0 as *const cs_evm))
            .finish()
    }
}

impl<'a> DetailsArchInsn for EvmInsnDetail<'a> {
    type OperandIterator = EvmOperandIterator;
    type Operand = EvmOperand;

    fn operands(&self) -> EvmOperandIterator {
        EvmOperandIterator::new()
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_evm_detail() {
        let cs_evm = cs_evm {
            pop: 1,
            push: 2,
            fee: 42,
        };
        let d = EvmInsnDetail(&cs_evm);
        assert_eq!(d.popped_items(), 1);
        assert_eq!(d.pushed_items(), 2);
        assert_eq!(d.fee(), 42);
    }
}