Skip to content

Commit

Permalink
Implemented details for sysz
Browse files Browse the repository at this point in the history
  • Loading branch information
garnt committed May 10, 2024
1 parent 57bedec commit a1549be
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
7 changes: 7 additions & 0 deletions capstone-rs/src/arch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,13 @@ macro_rules! detail_arch_base {
/// Returns the BPF details, if any
=> arch_name = bpf,
]
[
detail = SysZDetail,
insn_detail = SysZInsnDetail<'a>,
op = SysZOperand,
/// Returns the SysZ details, if any
=> arch_name = sysz,
]
);
};
}
Expand Down
103 changes: 103 additions & 0 deletions capstone-rs/src/arch/sysz.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,106 @@
//! Contains sysz-specific types
use core::convert::From;
use core::{cmp, fmt, slice};

// XXX todo(garnt): create rusty versions
pub use capstone_sys::sysz_insn_group as SysZInsnGroup;
pub use capstone_sys::sysz_insn as SysZInsn;
pub use capstone_sys::sysz_reg as SysZReg;
use capstone_sys::{cs_sysz, cs_sysz_op, sysz_op_mem, sysz_op_type};

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

/// Contains sysz-specific details for an instruction
pub struct SysZInsnDetail<'a>(pub(crate) &'a cs_sysz);

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

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

/// Immediate
Imm(i64),

/// Memory
Mem(SysZOpMem),

/// Access Register
AcReg(RegId),

/// Invalid
Invalid,
}

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

/// SysZ memory operand
#[derive(Debug, Copy, Clone)]
pub struct SysZOpMem(pub(crate) sysz_op_mem);

impl SysZOpMem {
/// Base register
pub fn base(&self) -> u8 {
self.0.base
}

/// Index register
pub fn index(&self) -> u8 {
self.0.index
}

/// BDLAddr operand
pub fn length(&self) -> u64 {
self.0.length
}

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

impl_PartialEq_repr_fields!(SysZOpMem;
base, index, length, disp
);

impl cmp::Eq for SysZOpMem {}

impl <'a> From<&'a cs_sysz_op> for SysZOperand {
fn from(insn: &cs_sysz_op) -> SysZOperand {
match insn.type_ {
sysz_op_type::SYSZ_OP_REG => {
SysZOperand::Reg(RegId(unsafe { insn.__bindgen_anon_1.reg } as RegIdInt))
},
sysz_op_type::SYSZ_OP_IMM => SysZOperand::Imm(unsafe { insn.__bindgen_anon_1.imm }),
sysz_op_type::SYSZ_OP_MEM => {
SysZOperand::Mem(SysZOpMem(unsafe { insn.__bindgen_anon_1.mem }))
},
sysz_op_type::SYSZ_OP_ACREG => {
SysZOperand::AcReg(RegId(unsafe { insn.__bindgen_anon_1.reg } as RegIdInt))
},
sysz_op_type::SYSZ_OP_INVALID => SysZOperand::Invalid,
}
}
}

def_arch_details_struct!(
InsnDetail = SysZInsnDetail;
Operand = SysZOperand;
OperandIterator = SysZOperandIterator;
OperandIteratorLife = SysZOperandIterator<'a>;
[ pub struct SysZOperandIterator<'a>(slice::Iter<'a, cs_sysz_op>); ]
cs_arch_op = cs_sysz_op;
cs_arch = cs_sysz;
);
2 changes: 2 additions & 0 deletions capstone-rs/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ impl<'a> InsnDetail<'a> {
$detail($insn_detail(unsafe { &self.0.__bindgen_anon_1.$arch }))
}
)*
#[allow(unreachable_patterns)]
_ => panic!("Unsupported detail arch"),
}
}
Expand All @@ -431,6 +432,7 @@ impl<'a> InsnDetail<'a> {
[X86, X86Detail, X86InsnDetail, x86]
[XCORE, XcoreDetail, XcoreInsnDetail, xcore]
[BPF, BpfDetail, BpfInsnDetail, bpf]
[SYSZ, SysZDetail, SysZInsnDetail, sysz]
);
}
}
Expand Down

0 comments on commit a1549be

Please sign in to comment.