Skip to main content

osom_encoders_x86_64/encoders/
nop.rs

1//! This module contains the encoders for the `nop` instruction group.
2#![allow(unused_imports)]
3
4// ** This file is automatically generated from x86.yaml schema. Do not modify! **
5
6use crate::models::{
7    EncodedX86_64Instruction, GPR, GPROrMemory, Immediate8, Immediate16, Immediate32, Immediate64, Memory, Offset,
8    Scale, Size,
9};
10
11/// No operation.
12///
13/// # Safety
14///
15/// This function is safe to call. It is marked as unsafe for consistency with other encoders.
16#[inline]
17pub const unsafe fn encode() -> EncodedX86_64Instruction {
18    unsafe { crate::partial_encoders::zo::encode([0x90]) }
19}
20
21/// Represents length for NOP instruction. Internally this is `u8`
22/// but with values in `0..=9` range.
23#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
24#[repr(transparent)]
25#[must_use]
26pub struct NopLength {
27    len: u8,
28}
29
30/// Represents the length out of range error. This happens whenever
31/// the NOP instruction is outside of `0..=9` range.
32#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
33#[repr(transparent)]
34#[must_use]
35pub struct NopLengthOutOfRange {
36    phantom: core::marker::PhantomData<()>,
37}
38
39impl NopLength {
40    /// Creates a new [`NopLength`].
41    ///
42    /// # Errors
43    ///
44    /// Returns [`NopLengthOutOfRange`] if `len > 9`.
45    #[inline]
46    pub const fn new(len: u8) -> Result<Self, NopLengthOutOfRange> {
47        if len > 9 {
48            return Err(NopLengthOutOfRange {
49                phantom: core::marker::PhantomData,
50            });
51        }
52
53        Ok(unsafe { Self::new_unchecked(len) })
54    }
55
56    /// Creates a new [`NopLength`].
57    ///
58    /// # Safety
59    ///
60    /// It is up to the caller to ensure that `len <= 9`.
61    #[inline(always)]
62    pub const unsafe fn new_unchecked(len: u8) -> Self {
63        Self { len }
64    }
65
66    /// Returns the internal length.
67    #[must_use]
68    #[inline(always)]
69    pub const fn as_u8(self) -> u8 {
70        self.len
71    }
72}
73
74impl TryFrom<u8> for NopLength {
75    type Error = NopLengthOutOfRange;
76
77    fn try_from(value: u8) -> Result<Self, Self::Error> {
78        Self::new(value)
79    }
80}
81
82impl From<NopLength> for u8 {
83    fn from(value: NopLength) -> Self {
84        value.len
85    }
86}
87
88/// No operation with a given length.
89///
90/// # Notes
91///
92/// * The [`encode_with_len(0)`][encode_with_len] call returns an empty instruction (of length 0).
93/// * The [`encode_with_len(1)`][encode_with_len] call is equivalent to [`encode()`] call. Except
94///   it is slightly less efficient.
95/// * The [`encode_with_len(n)`][encode_with_len] call returns an instruction of length `n`.
96///
97/// # Safety
98///
99/// This function is safe to call. It is marked as unsafe for consistency with other encoders.
100#[allow(clippy::missing_panics_doc)]
101pub const unsafe fn encode_with_len(len: NopLength) -> EncodedX86_64Instruction {
102    use crate::partial_encoders::zo::encode;
103    unsafe {
104        match len.as_u8() {
105            0 => encode([]),
106            1 => encode([0x90]),
107            2 => encode([0x66, 0x90]),
108            3 => encode([0x0F, 0x1F, 0x00]),
109            4 => encode([0x0F, 0x1F, 0x40, 0x00]),
110            5 => encode([0x0F, 0x1F, 0x44, 0x00, 0x00]),
111            6 => encode([0x66, 0x0F, 0x1F, 0x44, 0x00, 0x00]),
112            7 => encode([0x0F, 0x1F, 0x80, 0x00, 0x00, 0x00, 0x00]),
113            8 => encode([0x0F, 0x1F, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00]),
114            9 => encode([0x66, 0x0F, 0x1F, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00]),
115            _ => panic!("Invalid length for encode_with_len call. Expected u8 in 1..=9 range."),
116        }
117    }
118}