osom_encoders_x86_64/encoders/
nop.rs1#![allow(unused_imports)]
3
4use crate::models::{
7 EncodedX86_64Instruction, GPR, GPROrMemory, Immediate8, Immediate16, Immediate32, Immediate64, Memory, Offset,
8 Scale, Size,
9};
10
11#[inline]
17pub const unsafe fn encode() -> EncodedX86_64Instruction {
18 unsafe { crate::partial_encoders::zo::encode([0x90]) }
19}
20
21#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
24#[repr(transparent)]
25#[must_use]
26pub struct NopLength {
27 len: u8,
28}
29
30#[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 #[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 #[inline(always)]
62 pub const unsafe fn new_unchecked(len: u8) -> Self {
63 Self { len }
64 }
65
66 #[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#[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}