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 {
30 0 : ptr,
31 0 : }
32 0 : }
33 : }
34 :
35 : impl Operand for Memory {
36 : #[doc(hidden)]
37 0 : fn as_generic(&self) -> &ffi::asm_x86_Operand {
38 0 : self.ptr.as_ref().unwrap().as_ref()
39 0 : }
40 : }
41 :
42 : impl Memory {
43 : /// The base register.
44 : ///
45 : /// For `lea rdx, [rip + 244634]` it would return [`Reg::RIP`]
46 0 : pub fn base(&self) -> Reg {
47 0 : Reg::from(self.ptr.base())
48 0 : }
49 :
50 : /// The scaled register.
51 : ///
52 : /// For `mov rdi, qword ptr [r13 + 8*r14]` it would return [`Reg::R14`]
53 0 : pub fn scaled_register(&self) -> Reg {
54 0 : Reg::from(self.ptr.scaled_register())
55 0 : }
56 :
57 : /// The segment register associated with the memory operation.
58 : ///
59 : /// For `mov eax, dword ptr gs:[0]` is would return [`Reg::GS`]
60 0 : pub fn segment_register(&self) -> Reg {
61 0 : Reg::from(self.ptr.segment_register())
62 0 : }
63 :
64 : /// The scale value associated with the [`Memory::scaled_register`]
65 : ///
66 : /// For `mov rdi, qword ptr [r13 + 8*r14]` it would return `8`
67 0 : pub fn scale(&self) -> u64 {
68 0 : self.ptr.scale()
69 0 : }
70 :
71 : /// The displacement value
72 : ///
73 : /// For `call qword ptr [rip + 248779]` it would return `248779`
74 0 : pub fn displacement(&self) -> i64 {
75 0 : self.ptr.displacement()
76 0 : }
77 : }
|