Skip to content

Commit 2ca2191

Browse files
committed
Add naked example
1 parent e11adf6 commit 2ca2191

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/attributes/codegen.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,25 @@ r[attributes.codegen.naked]
125125
r[attributes.codegen.naked.intro]
126126
The *`naked` [attribute]* prevents the compiler from emitting a function prologue and epilogue for the attributed function.
127127
128+
> [!EXAMPLE]
129+
> ```rust
130+
> # #[cfg(target_arch = "x86_64")] {
131+
> /// Adds 3 to the given number.
132+
> ///
133+
> /// SAFETY: The validity of the used registers
134+
> /// is guaranteed according to the "sysv64" ABI.
135+
> #[unsafe(naked)]
136+
> pub extern "sysv64" fn add_n(number: usize) -> usize {
137+
> core::arch::naked_asm!(
138+
> "add rdi, {}",
139+
> "mov rax, rdi",
140+
> "ret",
141+
> const 3,
142+
> )
143+
> }
144+
> # }
145+
> ```
146+
128147
r[attributes.codegen.naked.body]
129148
The [function body] must consist of exactly one [`naked_asm!`] macro invocation.
130149

0 commit comments

Comments
 (0)