-
Notifications
You must be signed in to change notification settings - Fork 601
Add extern "custom"
#2300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Add extern "custom"
#2300
Changes from all commits
0a83cc3
2890548
c979f98
38b00b1
20aeb84
9ea9daf
ae9e6b9
66b3bc5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
traviscross marked this conversation as resolved.
|
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking through the list at https://doc.rust-lang.org/reference/items/functions.html#attributes-on-functions, also noticed that
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't do harm either, so should we really make an exception here? I'm not against it per se, just wondering if we should make this calling convention even more special. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -239,6 +239,55 @@ With `panic=unwind`, when a `panic` is turned into an abort by a non-unwinding A | |
|
|
||
| For other considerations and limitations regarding unwinding across FFI boundaries, see the [relevant section in the Panic documentation][panic-ffi]. | ||
|
|
||
| r[items.fn.extern.custom] | ||
| ### Extern "custom" | ||
|
|
||
| r[items.fn.extern.custom.intro] | ||
| An `extern "custom"` function has an unknown, custom ABI. The only way to call such a function is via [inline assembly]. | ||
|
|
||
| > [!EXAMPLE] | ||
| > ```rust | ||
| > # #[cfg(target_arch = "x86_64")] { | ||
| > # use core::arch::{asm, naked_asm}; | ||
| > # | ||
| > /// Adds 1 to `rax`. | ||
| > /// | ||
| > /// This function uses a custom calling convention: the argument is | ||
| > /// passed in `rax`, the result is returned in `rax`, the flags may | ||
| > /// be clobbered, and all other registers are preserved. | ||
| > #[unsafe(naked)] | ||
| > unsafe extern "custom" fn increment() { | ||
| > naked_asm!( | ||
| > "add rax, 1", | ||
| > "ret", | ||
| > ) | ||
| > } | ||
| > | ||
| > let mut x: u64 = 41; | ||
| > // SAFETY: The inline assembly respects the calling convention of | ||
| > // `increment`: the argument is passed in `rax`, the result is read | ||
| > // from `rax`, and no other registers are affected. | ||
| > unsafe { | ||
| > asm!( | ||
| > "call {}", | ||
| > sym increment, | ||
| > inout("rax") x, | ||
| > ); | ||
| > } | ||
| > assert_eq!(x, 42); | ||
| > # } | ||
| > ``` | ||
|
|
||
| r[items.fn.extern.custom.signature] | ||
| An `extern "custom"` function must: | ||
|
|
||
| - Be `unsafe`. | ||
| - Not have any parameters. | ||
| - Return the [unit type]. | ||
|
Comment on lines
+284
to
+286
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs some consideration for generics, rust-lang/rust#158504 (comment) and the comment below
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess that generics are probably okay and allowed by default if not disallowed? |
||
|
|
||
| r[items.fn.extern.custom.naked] | ||
| An `extern "custom"` function definition must be a [naked function]. | ||
|
|
||
| [forced-unwinding]: https://rust-lang.github.io/rfcs/2945-c-unwind-abi.html#forced-unwinding | ||
| [panic handler]: ../panic.md#the-panic_handler-attribute | ||
| [panic-ffi]: ../panic.md#unwinding-across-ffi-boundaries | ||
|
|
@@ -411,6 +460,7 @@ fn foo_oof(#[some_inert_attribute] arg: u8) { | |
| [testing attributes]: ../attributes/testing.md | ||
| [`cold`]: ../attributes/codegen.md#the-cold-attribute | ||
| [`inline`]: ../attributes/codegen.md#the-inline-attribute | ||
| [naked function]: ../attributes/codegen.md#the-naked-attribute | ||
| [`deprecated`]: ../attributes/diagnostics.md#the-deprecated-attribute | ||
| [`doc`]: ../../rustdoc/the-doc-attribute.html | ||
| [`must_use`]: ../attributes/diagnostics.md#the-must_use-attribute | ||
|
|
@@ -427,3 +477,4 @@ fn foo_oof(#[some_inert_attribute] arg: u8) { | |
| [variadic function]: external-blocks.md#variadic-functions | ||
| [`extern` block]: external-blocks.md | ||
| [zero-sized]: glossary.zst | ||
| [inline assembly]: ../inline-assembly.md | ||
Uh oh!
There was an error while loading. Please reload this page.