Skip to content

Commit 260f346

Browse files
committed
Convert arch insn group and reg type to newtype enums
This commit changes the bindgen settings so that bindgen generates newtype enums instead of modules of constants for arch insn group types and reg types. Usages of these binding types are updated as well.
1 parent 182e5a0 commit 260f346

22 files changed

+6910
-3654
lines changed

capstone-rs/src/arch/arm.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ impl ArchTag for ArmArchTag {
3535
type ExtraMode = ArchExtraMode;
3636
type Syntax = ArchSyntax;
3737

38-
type RegId = ArmReg::Type;
38+
type RegId = ArmReg;
3939
type InsnId = ArmInsn;
40-
type InsnGroupId = ArmInsnGroup::Type;
40+
type InsnGroupId = ArmInsnGroup;
4141

4242
type InsnDetail<'a> = ArmInsnDetail<'a>;
4343

@@ -251,12 +251,12 @@ impl_PartialEq_repr_fields!(ArmInsnDetail<'a> [ 'a ];
251251
impl ArmOpMem {
252252
/// Base register
253253
pub fn base(&self) -> RegId {
254-
RegId(self.0.base as RegIdInt)
254+
RegId(self.0.base.0 as RegIdInt)
255255
}
256256

257257
/// Index value
258258
pub fn index(&self) -> RegId {
259-
RegId(self.0.index as RegIdInt)
259+
RegId(self.0.index.0 as RegIdInt)
260260
}
261261

262262
/// Scale for index register (can be 1, or -1)

capstone-rs/src/arch/arm64.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ impl ArchTag for Arm64ArchTag {
4040
type ExtraMode = ArchExtraMode;
4141
type Syntax = ArchSyntax;
4242

43-
type RegId = Arm64Reg::Type;
43+
type RegId = Arm64Reg;
4444
type InsnId = Arm64Insn;
45-
type InsnGroupId = Arm64InsnGroup::Type;
45+
type InsnGroupId = Arm64InsnGroup;
4646

4747
type InsnDetail<'a> = Arm64InsnDetail<'a>;
4848

@@ -88,7 +88,7 @@ impl Arm64OperandType {
8888

8989
match op_type {
9090
ARM64_OP_INVALID => Invalid,
91-
ARM64_OP_REG => Reg(RegId(unsafe { value.reg } as RegIdInt)),
91+
ARM64_OP_REG => Reg(RegId(unsafe { value.reg.0 } as RegIdInt)),
9292
ARM64_OP_IMM => Imm(unsafe { value.imm }),
9393
ARM64_OP_MEM => Mem(Arm64OpMem(unsafe { value.mem })),
9494
ARM64_OP_FP => Fp(unsafe { value.fp }),
@@ -190,12 +190,12 @@ impl_PartialEq_repr_fields!(Arm64InsnDetail<'a> [ 'a ];
190190
impl Arm64OpMem {
191191
/// Base register
192192
pub fn base(&self) -> RegId {
193-
RegId(self.0.base as RegIdInt)
193+
RegId(self.0.base.0 as RegIdInt)
194194
}
195195

196196
/// Index register
197197
pub fn index(&self) -> RegId {
198-
RegId(self.0.index as RegIdInt)
198+
RegId(self.0.index.0 as RegIdInt)
199199
}
200200

201201
/// Disp value
@@ -318,19 +318,19 @@ mod test {
318318
}
319319

320320
t(
321-
(ARM64_OP_INVALID, cs_arm64_op__bindgen_ty_2 { reg: 0 }),
321+
(ARM64_OP_INVALID, cs_arm64_op__bindgen_ty_2 { reg: Arm64Reg::ARM64_REG_INVALID }),
322322
Invalid,
323323
);
324324
t(
325-
(ARM64_OP_REG, cs_arm64_op__bindgen_ty_2 { reg: 0 }),
325+
(ARM64_OP_REG, cs_arm64_op__bindgen_ty_2 { reg: Arm64Reg::ARM64_REG_INVALID }),
326326
Reg(RegId(0)),
327327
);
328328
t(
329329
(ARM64_OP_IMM, cs_arm64_op__bindgen_ty_2 { imm: 42 }),
330330
Imm(42),
331331
);
332332
t(
333-
(ARM64_OP_REG_MRS, cs_arm64_op__bindgen_ty_2 { reg: ARM64_SYSREG_MDRAR_EL1 as u32 }),
333+
(ARM64_OP_REG_MRS, cs_arm64_op__bindgen_ty_2 { reg: Arm64Reg(ARM64_SYSREG_MDRAR_EL1 as u32) }),
334334
RegMrs(ARM64_SYSREG_MDRAR_EL1),
335335
);
336336
t(
@@ -347,8 +347,8 @@ mod test {
347347
);
348348
t(
349349
(ARM64_OP_REG_MSR, cs_arm64_op__bindgen_ty_2 {
350-
reg: arm64_sysreg::ARM64_SYSREG_ICC_EOIR1_EL1 as u32 }),
351-
RegMsr(arm64_sysreg::ARM64_SYSREG_ICC_EOIR1_EL1),
350+
reg: Arm64Reg(ARM64_SYSREG_ICC_EOIR1_EL1 as u32) }),
351+
RegMsr(ARM64_SYSREG_ICC_EOIR1_EL1),
352352
);
353353
t(
354354
(ARM64_OP_SYS, cs_arm64_op__bindgen_ty_2 { sys: arm64_sys_op::ARM64_AT_S1E0R }),

capstone-rs/src/arch/evm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl ArchTag for EvmArchTag {
2626

2727
type RegId = u32;
2828
type InsnId = EvmInsn;
29-
type InsnGroupId = EvmInsnGroup::Type;
29+
type InsnGroupId = EvmInsnGroup;
3030

3131
type InsnDetail<'a> = EvmInsnDetail<'a>;
3232

capstone-rs/src/arch/m680x.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ use capstone_sys::{
1010
// XXX todo(tmfink): create rusty versions
1111
pub use capstone_sys::m680x_insn as M680xInsn;
1212
pub use capstone_sys::m680x_reg as M680xReg;
13+
pub use capstone_sys::m680x_group_type as M680xInsnGroup;
1314

1415
pub use crate::arch::arch_builder::m680x::*;
1516
use crate::arch::{ArchTag, DetailsArchInsn};
1617
use crate::arch::internal::ArchTagSealed;
17-
use crate::instruction::{RegId, RegIdInt};
18+
use crate::instruction::RegId;
1819
use crate::{Arch, InsnDetail};
1920

2021
pub struct M680xArchTag;
@@ -28,9 +29,9 @@ impl ArchTag for M680xArchTag {
2829
type ExtraMode = ArchExtraMode;
2930
type Syntax = ArchSyntax;
3031

31-
type RegId = M680xReg::Type;
32+
type RegId = M680xReg;
3233
type InsnId = M680xInsn;
33-
type InsnGroupId = u32;
34+
type InsnGroupId = M680xInsnGroup;
3435

3536
type InsnDetail<'a> = M680xInsnDetail<'a>;
3637

@@ -88,7 +89,7 @@ macro_rules! define_m680x_register_option_getter {
8889
if (self.0).$field == M680xReg::M680X_REG_INVALID {
8990
None
9091
} else {
91-
Some(RegId((self.0).$field as RegIdInt))
92+
Some(self.0.$field.into())
9293
}
9394
}
9495
}
@@ -214,7 +215,7 @@ impl<'a> From<&'a cs_m680x_op> for M680xOperand {
214215
fn from(op: &cs_m680x_op) -> M680xOperand {
215216
let op_type = match op.type_ {
216217
m680x_op_type::M680X_OP_REGISTER => {
217-
M680xOperandType::Reg(RegId(unsafe { op.__bindgen_anon_1.reg } as RegIdInt))
218+
M680xOperandType::Reg(unsafe { op.__bindgen_anon_1.reg.into() })
218219
}
219220
m680x_op_type::M680X_OP_IMMEDIATE => {
220221
M680xOperandType::Imm(unsafe { op.__bindgen_anon_1.imm })
@@ -281,7 +282,7 @@ mod test {
281282
fn m680x_op_type() {
282283
let op_base = cs_m680x_op {
283284
type_: m680x_op_type::M680X_OP_INVALID,
284-
__bindgen_anon_1: cs_m680x_op__bindgen_ty_1 { reg: 0 },
285+
__bindgen_anon_1: cs_m680x_op__bindgen_ty_1 { reg: M680xReg(0) },
285286
size: 1,
286287
access: 0,
287288
};
@@ -299,7 +300,7 @@ mod test {
299300
..op_base
300301
})
301302
.op_type,
302-
M680xOperandType::Reg(RegId(M680xReg::M680X_REG_E as RegIdInt))
303+
M680xOperandType::Reg(M680xReg::M680X_REG_E.into())
303304
);
304305
assert_eq!(
305306
M680xOperand::from(&cs_m680x_op {
@@ -391,8 +392,8 @@ mod test {
391392
flags: 7,
392393
});
393394

394-
assert_eq!(idx.base_reg(), Some(RegId(base_reg as RegIdInt)));
395-
assert_eq!(idx.offset_reg(), Some(RegId(offset_reg as RegIdInt)));
395+
assert_eq!(idx.base_reg(), Some(base_reg.into()));
396+
assert_eq!(idx.offset_reg(), Some(offset_reg.into()));
396397
assert_eq!(idx.offset(), offset);
397398
assert_eq!(idx.offset_addr(), offset_addr);
398399
assert_eq!(idx.offset_bits(), offset_bits);

0 commit comments

Comments
 (0)