capstone_git/arch/
xcore.rs

1//! Contains xcore-specific types
2
3use core::convert::From;
4use core::{cmp, fmt, slice};
5
6// XXX todo(tmfink): create rusty versions
7pub use capstone_sys::xcore_insn_group as XcoreInsnGroup;
8pub use capstone_sys::xcore_insn as XcoreInsn;
9pub use capstone_sys::xcore_reg as XcoreReg;
10use capstone_sys::{cs_xcore, cs_xcore_op, xcore_op_mem, xcore_op_type};
11
12pub use crate::arch::arch_builder::xcore::*;
13use crate::arch::DetailsArchInsn;
14use crate::instruction::{RegId, RegIdInt};
15
16/// Contains XCORE-specific details for an instruction
17pub struct XcoreInsnDetail<'a>(pub(crate) &'a cs_xcore);
18
19impl_PartialEq_repr_fields!(XcoreInsnDetail<'a> [ 'a ];
20    operands
21);
22
23/// XCORE operand
24#[derive(Clone, Debug, Eq, PartialEq)]
25pub enum XcoreOperand {
26    /// Register
27    Reg(RegId),
28
29    /// Immediate
30    Imm(i32),
31
32    /// Memory
33    Mem(XcoreOpMem),
34
35    /// Invalid
36    Invalid,
37}
38
39impl Default for XcoreOperand {
40    fn default() -> Self {
41        XcoreOperand::Invalid
42    }
43}
44
45/// XCORE memory operand
46#[derive(Debug, Copy, Clone)]
47pub struct XcoreOpMem(pub(crate) xcore_op_mem);
48
49impl XcoreOpMem {
50    /// Base register
51    pub fn base(&self) -> RegId {
52        RegId(RegIdInt::from(self.0.base))
53    }
54
55    /// Index register
56    pub fn index(&self) -> RegId {
57        RegId(RegIdInt::from(self.0.index))
58    }
59
60    /// Disp value
61    pub fn disp(&self) -> i32 {
62        self.0.disp
63    }
64
65    /// Direct value
66    pub fn direct(&self) -> i32 {
67        self.0.direct
68    }
69}
70
71impl_PartialEq_repr_fields!(XcoreOpMem;
72    base, index, disp, direct
73);
74
75impl cmp::Eq for XcoreOpMem {}
76
77impl From<&cs_xcore_op> for XcoreOperand {
78    fn from(insn: &cs_xcore_op) -> XcoreOperand {
79        match insn.type_ {
80            xcore_op_type::XCORE_OP_REG => {
81                XcoreOperand::Reg(RegId(unsafe { insn.__bindgen_anon_1.reg } as RegIdInt))
82            }
83            xcore_op_type::XCORE_OP_IMM => XcoreOperand::Imm(unsafe { insn.__bindgen_anon_1.imm }),
84            xcore_op_type::XCORE_OP_MEM => {
85                XcoreOperand::Mem(XcoreOpMem(unsafe { insn.__bindgen_anon_1.mem }))
86            }
87            xcore_op_type::XCORE_OP_INVALID => XcoreOperand::Invalid,
88        }
89    }
90}
91
92def_arch_details_struct!(
93    InsnDetail = XcoreInsnDetail;
94    Operand = XcoreOperand;
95    OperandIterator = XcoreOperandIterator;
96    OperandIteratorLife = XcoreOperandIterator<'a>;
97    [ pub struct XcoreOperandIterator<'a>(slice::Iter<'a, cs_xcore_op>); ]
98    cs_arch_op = cs_xcore_op;
99    cs_arch = cs_xcore;
100);