Skip to content

Commit b005c00

Browse files
committed
Add naked example
1 parent 9571d4d commit b005c00

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
@@ -52,6 +52,25 @@ r[attributes.codegen.naked]
5252
r[attributes.codegen.naked.intro]
5353
The *`naked` [attribute]* prevents the compiler from emitting a function prologue and epilogue for the attributed function.
5454

55+
> [!EXAMPLE]
56+
> ```rust
57+
> # #[cfg(target_arch = "x86_64")] {
58+
> /// Adds 3 to the given number.
59+
> ///
60+
> /// SAFETY: The validity of the used registers
61+
> /// is guaranteed according to the "sysv64" ABI.
62+
> #[unsafe(naked)]
63+
> pub extern "sysv64" fn add_n(number: usize) -> usize {
64+
> core::arch::naked_asm!(
65+
> "add rdi, {}",
66+
> "mov rax, rdi",
67+
> "ret",
68+
> const 3,
69+
> )
70+
> }
71+
> # }
72+
> ```
73+
5574
r[attributes.codegen.naked.body]
5675
The [function body] must consist of exactly one [`naked_asm!`] macro invocation.
5776

0 commit comments

Comments
 (0)