capstone/arch/
riscv.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//! Contains riscv-specific types

use core::convert::From;
use core::{cmp, fmt, slice};

// XXX todo(tmfink): create rusty versions
pub use capstone_sys::riscv_insn_group as RiscVInsnGroup;
pub use capstone_sys::riscv_insn as RiscVInsn;
pub use capstone_sys::riscv_reg as RiscVReg;
use capstone_sys::{cs_riscv, cs_riscv_op, riscv_op_mem, riscv_op_type};

pub use crate::arch::arch_builder::riscv::*;
use crate::arch::DetailsArchInsn;
use crate::instruction::{RegId, RegIdInt};

/// Contains RISCV-specific details for an instruction
pub struct RiscVInsnDetail<'a>(pub(crate) &'a cs_riscv);

impl_PartialEq_repr_fields!(RiscVInsnDetail<'a> [ 'a ];
    operands
);

/// RISCV operand
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum RiscVOperand {
    /// Register
    Reg(RegId),

    /// Immediate
    Imm(i64),

    /// Memory
    Mem(RiscVOpMem),

    /// Invalid
    Invalid,
}

impl Default for RiscVOperand {
    fn default() -> Self {
        RiscVOperand::Invalid
    }
}

/// RISCV memory operand
#[derive(Debug, Copy, Clone)]
pub struct RiscVOpMem(pub(crate) riscv_op_mem);

impl RiscVOpMem {
    /// Base register
    pub fn base(&self) -> RegId {
        RegId(self.0.base as RegIdInt)
    }

    /// Disp value
    pub fn disp(&self) -> i64 {
        self.0.disp
    }
}

impl_PartialEq_repr_fields!(RiscVOpMem;
    base, disp
);

impl cmp::Eq for RiscVOpMem {}

impl<'a> From<&'a cs_riscv_op> for RiscVOperand {
    fn from(insn: &cs_riscv_op) -> RiscVOperand {
        match insn.type_ {
            riscv_op_type::RISCV_OP_REG => {
                RiscVOperand::Reg(RegId(unsafe { insn.__bindgen_anon_1.reg } as RegIdInt))
            }
            riscv_op_type::RISCV_OP_IMM => RiscVOperand::Imm(unsafe { insn.__bindgen_anon_1.imm }),
            riscv_op_type::RISCV_OP_MEM => {
                RiscVOperand::Mem(RiscVOpMem(unsafe { insn.__bindgen_anon_1.mem }))
            }
            riscv_op_type::RISCV_OP_INVALID => RiscVOperand::Invalid,
        }
    }
}

def_arch_details_struct!(
    InsnDetail = RiscVInsnDetail;
    Operand = RiscVOperand;
    OperandIterator = RiscVOperandIterator;
    OperandIteratorLife = RiscVOperandIterator<'a>;
    [ pub struct RiscVOperandIterator<'a>(slice::Iter<'a, cs_riscv_op>); ]
    cs_arch_op = cs_riscv_op;
    cs_arch = cs_riscv;
);