iced_x86/constant_offsets.rs
1// SPDX-License-Identifier: MIT
2// Copyright (C) 2018-present iced project and contributors
3
4/// Contains the offsets of the displacement and immediate. Call [`Decoder::get_constant_offsets()`] or
5/// [`Encoder::get_constant_offsets()`] to get the offsets of the constants after the instruction has been
6/// decoded/encoded.
7///
8/// [`Decoder::get_constant_offsets()`]: struct.Decoder.html#method.get_constant_offsets
9/// [`Encoder::get_constant_offsets()`]: struct.Encoder.html#method.get_constant_offsets
10#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Hash)]
11#[allow(dead_code)]
12pub struct ConstantOffsets {
13 pub(crate) displacement_offset: u8,
14 pub(crate) displacement_size: u8,
15 pub(crate) immediate_offset: u8,
16 pub(crate) immediate_size: u8,
17 pub(crate) immediate_offset2: u8,
18 pub(crate) immediate_size2: u8,
19 pad1: u8,
20 pad2: u8,
21}
22
23impl ConstantOffsets {
24 /// The offset of the displacement, if any
25 #[must_use]
26 #[inline]
27 pub const fn displacement_offset(&self) -> usize {
28 self.displacement_offset as usize
29 }
30
31 /// Size in bytes of the displacement, or 0 if there's no displacement
32 #[must_use]
33 #[inline]
34 pub const fn displacement_size(&self) -> usize {
35 self.displacement_size as usize
36 }
37
38 /// The offset of the first immediate, if any.
39 ///
40 /// This field can be invalid even if the operand has an immediate if it's an immediate that isn't part
41 /// of the instruction stream, eg. `SHL AL,1`.
42 #[must_use]
43 #[inline]
44 pub const fn immediate_offset(&self) -> usize {
45 self.immediate_offset as usize
46 }
47
48 /// Size in bytes of the first immediate, or 0 if there's no immediate
49 #[must_use]
50 #[inline]
51 pub const fn immediate_size(&self) -> usize {
52 self.immediate_size as usize
53 }
54
55 /// The offset of the second immediate, if any.
56 #[must_use]
57 #[inline]
58 pub const fn immediate_offset2(&self) -> usize {
59 self.immediate_offset2 as usize
60 }
61
62 /// Size in bytes of the second immediate, or 0 if there's no second immediate
63 #[must_use]
64 #[inline]
65 pub const fn immediate_size2(&self) -> usize {
66 self.immediate_size2 as usize
67 }
68
69 /// `true` if [`displacement_offset()`] and [`displacement_size()`] are valid
70 ///
71 /// [`displacement_offset()`]: #method.displacement_offset
72 /// [`displacement_size()`]: #method.displacement_size
73 #[must_use]
74 #[inline]
75 pub const fn has_displacement(&self) -> bool {
76 self.displacement_size != 0
77 }
78
79 /// `true` if [`immediate_offset()`] and [`immediate_size()`] are valid
80 ///
81 /// [`immediate_offset()`]: #method.immediate_offset
82 /// [`immediate_size()`]: #method.immediate_size
83 #[must_use]
84 #[inline]
85 pub const fn has_immediate(&self) -> bool {
86 self.immediate_size != 0
87 }
88
89 /// `true` if [`immediate_offset2()`] and [`immediate_size2()`] are valid
90 ///
91 /// [`immediate_offset2()`]: #method.immediate_offset2
92 /// [`immediate_size2()`]: #method.immediate_size2
93 #[must_use]
94 #[inline]
95 pub const fn has_immediate2(&self) -> bool {
96 self.immediate_size2 != 0
97 }
98}