|
| 1 | +//! Contains loongarch-specific types |
| 2 | +
|
| 3 | +use core::convert::{From, TryInto}; |
| 4 | +use core::{cmp, fmt, slice}; |
| 5 | + |
| 6 | +pub use capstone_sys::loongarch_insn as LoongArchInsn; |
| 7 | +pub use capstone_sys::loongarch_reg as LoongArchReg; |
| 8 | +use capstone_sys::{ |
| 9 | + cs_ac_type, cs_loongarch, cs_loongarch_op, cs_loongarch_op__bindgen_ty_1, loongarch_op_mem, loongarch_op_type |
| 10 | +}; |
| 11 | + |
| 12 | +pub use crate::arch::arch_builder::loongarch::*; |
| 13 | +use crate::arch::DetailsArchInsn; |
| 14 | +use crate::instruction::{RegId, RegIdInt}; |
| 15 | +use crate::RegAccessType; |
| 16 | + |
| 17 | +/// Contains loongarch-specific details for an instruction |
| 18 | +pub struct LoongArchInsnDetail<'a>(pub(crate) &'a cs_loongarch); |
| 19 | + |
| 20 | +impl_PartialEq_repr_fields!(LoongArchInsnDetail<'a> [ 'a ]; |
| 21 | + operands |
| 22 | +); |
| 23 | + |
| 24 | +/// loongarch operand |
| 25 | +#[derive(Clone, Debug, Eq, PartialEq, Default)] |
| 26 | +pub struct LoongArchOperand { |
| 27 | + /// Operand type |
| 28 | + pub op_type: LoongArchOperandType, |
| 29 | + |
| 30 | + /// How is this operand accessed? |
| 31 | + /// |
| 32 | + /// NOTE: this field is always `None` if the "full" feataure is not enabled. |
| 33 | + pub access: Option<RegAccessType>, |
| 34 | +} |
| 35 | + |
| 36 | +impl From<&cs_loongarch_op> for LoongArchOperand { |
| 37 | + fn from(insn: &cs_loongarch_op) -> LoongArchOperand { |
| 38 | + let op_type = LoongArchOperandType::new(insn.type_, insn.__bindgen_anon_1); |
| 39 | + LoongArchOperand { |
| 40 | + op_type, |
| 41 | + access: cs_ac_type(insn.access as _).try_into().ok(), |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/// LoongArch operand |
| 47 | +#[derive(Clone, Debug, PartialEq)] |
| 48 | +pub enum LoongArchOperandType { |
| 49 | + /// Register |
| 50 | + Reg(RegId), |
| 51 | + |
| 52 | + /// Immediate |
| 53 | + Imm(i64), |
| 54 | + |
| 55 | + /// Memory |
| 56 | + Mem(LoongArchOpMem), |
| 57 | + |
| 58 | + /// Invalid |
| 59 | + Invalid, |
| 60 | +} |
| 61 | + |
| 62 | +impl LoongArchOperandType { |
| 63 | + fn new( |
| 64 | + op_type: loongarch_op_type, |
| 65 | + value: cs_loongarch_op__bindgen_ty_1, |
| 66 | + ) -> LoongArchOperandType { |
| 67 | + match op_type { |
| 68 | + loongarch_op_type::LOONGARCH_OP_REG => { |
| 69 | + LoongArchOperandType::Reg(RegId(unsafe { value.reg } as RegIdInt)) |
| 70 | + } |
| 71 | + loongarch_op_type::LOONGARCH_OP_IMM => LoongArchOperandType::Imm(unsafe { value.imm }), |
| 72 | + loongarch_op_type::LOONGARCH_OP_MEM => { |
| 73 | + LoongArchOperandType::Mem(LoongArchOpMem(unsafe { value.mem })) |
| 74 | + } |
| 75 | + loongarch_op_type::LOONGARCH_OP_INVALID => LoongArchOperandType::Invalid, |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +impl cmp::Eq for LoongArchOperandType {} |
| 81 | + |
| 82 | +impl Default for LoongArchOperandType { |
| 83 | + fn default() -> Self { |
| 84 | + LoongArchOperandType::Invalid |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +/// loongarch memory operand |
| 89 | +#[derive(Debug, Copy, Clone)] |
| 90 | +pub struct LoongArchOpMem(pub(crate) loongarch_op_mem); |
| 91 | + |
| 92 | +impl LoongArchOpMem { |
| 93 | + /// Base register |
| 94 | + pub fn base(&self) -> RegId { |
| 95 | + RegId(self.0.base as RegIdInt) |
| 96 | + } |
| 97 | + |
| 98 | + /// Index register |
| 99 | + pub fn index(&self) -> RegId { |
| 100 | + RegId(self.0.index as RegIdInt) |
| 101 | + } |
| 102 | + |
| 103 | + /// Displacement/offset |
| 104 | + pub fn disp(&self) -> i64 { |
| 105 | + self.0.disp |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +impl_PartialEq_repr_fields!(LoongArchOpMem; |
| 110 | + base, index, disp |
| 111 | +); |
| 112 | + |
| 113 | +impl cmp::Eq for LoongArchOpMem {} |
| 114 | + |
| 115 | +def_arch_details_struct!( |
| 116 | + InsnDetail = LoongArchInsnDetail; |
| 117 | + Operand = LoongArchOperand; |
| 118 | + OperandIterator = LoongArchOperandIterator; |
| 119 | + OperandIteratorLife = LoongArchOperandIterator<'a>; |
| 120 | + [ pub struct LoongArchOperandIterator<'a>(slice::Iter<'a, cs_loongarch_op>); ] |
| 121 | + cs_arch_op = cs_loongarch_op; |
| 122 | + cs_arch = cs_loongarch; |
| 123 | +); |
0 commit comments