Line data Source code
1 : use lief_ffi as ffi;
2 :
3 : use crate::{assembly::x86::registers::Reg, common::FromFFI};
4 :
5 : use super::Operand;
6 :
7 : /// This structure represents a memory operand.
8 : ///
9 : /// For instance:
10 : ///
11 : /// ```text
12 : /// movq xmm3, qword ptr [rip + 823864];
13 : ///
14 : /// |
15 : /// |
16 : /// Memory
17 : /// |
18 : /// +-----------+-----------+
19 : /// | | |
20 : /// Base: rip Scale: 1 Displacement: 823864
21 : ///
22 : /// ```
23 : pub struct Memory {
24 : ptr: cxx::UniquePtr<ffi::asm_x86_operands_Memory>,
25 : }
26 :
27 : impl FromFFI<ffi::asm_x86_operands_Memory> for Memory {
28 0 : fn from_ffi(ptr: cxx::UniquePtr<ffi::asm_x86_operands_Memory>) -> Self {
29 0 : Self { ptr }
30 0 : }
31 : }
32 :
33 : impl Operand for Memory {
34 : #[doc(hidden)]
35 0 : fn as_generic(&self) -> &ffi::asm_x86_Operand {
36 0 : self.ptr.as_ref().unwrap().as_ref()
37 0 : }
38 : }
39 :
40 : impl Memory {
41 : /// The base register.
42 : ///
43 : /// For `lea rdx, [rip + 244634]` it would return [`Reg::RIP`]
44 0 : pub fn base(&self) -> Reg {
45 0 : Reg::from(self.ptr.base())
46 0 : }
47 :
48 : /// The scaled register.
49 : ///
50 : /// For `mov rdi, qword ptr [r13 + 8*r14]` it would return [`Reg::R14`]
51 0 : pub fn scaled_register(&self) -> Reg {
52 0 : Reg::from(self.ptr.scaled_register())
53 0 : }
54 :
55 : /// The segment register associated with the memory operation.
56 : ///
57 : /// For `mov eax, dword ptr gs:[0]` is would return [`Reg::GS`]
58 0 : pub fn segment_register(&self) -> Reg {
59 0 : Reg::from(self.ptr.segment_register())
60 0 : }
61 :
62 : /// The scale value associated with the [`Memory::scaled_register`]
63 : ///
64 : /// For `mov rdi, qword ptr [r13 + 8*r14]` it would return `8`
65 0 : pub fn scale(&self) -> u64 {
66 0 : self.ptr.scale()
67 0 : }
68 :
69 : /// The displacement value
70 : ///
71 : /// For `call qword ptr [rip + 248779]` it would return `248779`
72 0 : pub fn displacement(&self) -> i64 {
73 0 : self.ptr.displacement()
74 0 : }
75 : }
|