From a1549be770d0fdce5d02e1b98b3b54f334293f45 Mon Sep 17 00:00:00 2001 From: garnt Date: Fri, 10 May 2024 11:39:50 -0400 Subject: [PATCH] Implemented details for sysz --- capstone-rs/src/arch/mod.rs | 7 +++ capstone-rs/src/arch/sysz.rs | 103 +++++++++++++++++++++++++++++++++ capstone-rs/src/instruction.rs | 2 + 3 files changed, 112 insertions(+) diff --git a/capstone-rs/src/arch/mod.rs b/capstone-rs/src/arch/mod.rs index a66c794c..fa74bfc3 100644 --- a/capstone-rs/src/arch/mod.rs +++ b/capstone-rs/src/arch/mod.rs @@ -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, + ] ); }; } diff --git a/capstone-rs/src/arch/sysz.rs b/capstone-rs/src/arch/sysz.rs index bdbb13cc..20ebdb4e 100644 --- a/capstone-rs/src/arch/sysz.rs +++ b/capstone-rs/src/arch/sysz.rs @@ -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; +); diff --git a/capstone-rs/src/instruction.rs b/capstone-rs/src/instruction.rs index c0bfbd20..b6856ed9 100644 --- a/capstone-rs/src/instruction.rs +++ b/capstone-rs/src/instruction.rs @@ -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"), } } @@ -431,6 +432,7 @@ impl<'a> InsnDetail<'a> { [X86, X86Detail, X86InsnDetail, x86] [XCORE, XcoreDetail, XcoreInsnDetail, xcore] [BPF, BpfDetail, BpfInsnDetail, bpf] + [SYSZ, SysZDetail, SysZInsnDetail, sysz] ); } }