Closed
Description
This issue tracks the release notes text for #128570.
- Issue is nominated for the responsible team (and T-release nomination is removed).
- Proposed text is drafted by team responsible for underlying change.
- Issue is nominated for release team review of clarity for wider audience.
- Release team includes text in release notes/blog posts.
Release notes text:
The section title will be de-duplicated by the release team with other release notes issues.
Prefer to use the standard titles from previous releases.
More than one section can be included if needed.
# Language
- [Stabilize `const` operands in inline assembly](https://github.com/rust-lang/rust/pull/128570)
Release blog section (if any, leave blank if no section is expected):
# Constants as assembly immediates
The `const` assembly operand now provides a way to use integers as immediates
without first storing them in a register. As an example, we implement a syscall to
[`write`](https://man7.org/linux/man-pages/man2/write.2.html) by hand:
```rust
const WRITE_SYSCALL: c_int = 0x01; // syscall 1 is `write`
const STDOUT_HANDLE: c_int = 0x01; // `stdout` has file handle 1
const MSG: &str = "Hello, world!\n";
let written: usize;
// Signature: `ssize_t write(int fd, const void buf[], size_t count)`
unsafe {
core::arch::asm!(
"mov rax, {SYSCALL} // rax holds the syscall number",
"mov rdi, {OUTPUT} // rdi is `fd` (first argument)",
"mov rdx, {LEN} // rdx is `count` (third argument)",
"syscall // invoke the syscall",
"mov {written}, rax // save the return value",
SYSCALL = const WRITE_SYSCALL,
OUTPUT = const STDOUT_HANDLE,
LEN = const MSG.len(),
in("rsi") MSG.as_ptr(), // rsi is `buf *` (second argument)
written = out(reg) written,
);
}
assert_eq!(written, MSG.len());
```
Output:
```text
Hello, world!
```
[Playground link](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=0cf8e21335b38011b49156c6c65929bc).
In the above, a statement such as `LEN = const MSG.len()` populates the format
specifier `LEN` with an immediate that takes the value of `MSG.len()`. This can be seen
in the generated assembly (the value is `14`):
```asm
lea rsi, [rip + .L__unnamed_3]
mov rax, 1 # rax holds the syscall number
mov rdi, 1 # rdi is `fd` (first argument)
mov rdx, 14 # rdx is `count` (third argument)
syscall # invoke the syscall
mov rax, rax # save the return value
```
See [the reference](https://doc.rust-lang.org/reference/inline-assembly.html)
for more details.