Skip to content

Commit f2ac173

Browse files
authored
Merge pull request #2063 from folkertdev/asm-cfg
document `cfg` conditions on inline assembly templates and operands
2 parents 9cce498 + ac44a37 commit f2ac173

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/attributes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ Attributes may be applied to many forms in the language:
107107
* [Function][functions], [closure] and [function pointer]
108108
parameters accept outer attributes. This includes attributes on variadic parameters
109109
denoted with `...` in function pointers and [external blocks][variadic functions].
110+
* [Inline assembly] template strings and operands accept outer attributes. Only certain attributes are accepted semantically; for details, see [asm.attributes.supported-attributes].
110111
111112
r[attributes.meta]
112113
## Meta item attribute syntax
@@ -409,3 +410,4 @@ The following is an index of all built-in attributes.
409410
[variadic functions]: items/external-blocks.html#variadic-functions
410411
[`diagnostic::on_unimplemented`]: attributes/diagnostics.md#the-diagnosticon_unimplemented-attribute
411412
[`diagnostic::do_not_recommend`]: attributes/diagnostics.md#the-diagnosticdo_not_recommend-attribute
413+
[Inline assembly]: inline-assembly.md

src/inline-assembly.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,19 @@ r[asm.syntax]
4949
The following grammar specifies the arguments that can be passed to the `asm!`, `global_asm!` and `naked_asm!` macros.
5050

5151
```grammar,assembly
52-
@root AsmArgs -> FormatString (`,` FormatString)* (`,` AsmOperand)* `,`?
52+
@root AsmArgs -> AsmAttrFormatString (`,` AsmAttrFormatString)* (`,` AsmAttrOperand)* `,`?
5353
5454
FormatString -> STRING_LITERAL | RAW_STRING_LITERAL | MacroInvocation
5555
56+
AsmAttrFormatString -> (OuterAttribute)* FormatString
57+
5658
AsmOperand ->
5759
ClobberAbi
5860
| AsmOptions
5961
| RegOperand
6062
63+
AsmAttrOperand -> (OuterAttribute)* AsmOperand
64+
6165
ClobberAbi -> `clobber_abi` `(` Abi (`,` Abi)* `,`? `)`
6266
6367
AsmOptions ->
@@ -266,6 +270,44 @@ Further constraints on the directives used by inline assembly are indicated by [
266270
[format-syntax]: std::fmt#syntax
267271
[rfc-2795]: https://github.com/rust-lang/rfcs/pull/2795
268272

273+
r[asm.attributes]
274+
## Attributes
275+
276+
r[asm.attributes.supported-attributes]
277+
Only the [`cfg`] and [`cfg_attr`] attributes are accepted semantically on inline assembly template strings and operands. Other attributes are parsed but rejected when the assembly macro is expanded.
278+
279+
```rust
280+
# fn main() {}
281+
# #[cfg(target_arch = "x86_64")]
282+
core::arch::global_asm!(
283+
#[cfg(not(panic = "abort"))]
284+
".cfi_startproc",
285+
// ...
286+
"ret",
287+
#[cfg(not(panic = "abort"))]
288+
".cfi_endproc",
289+
);
290+
```
291+
292+
> [!NOTE]
293+
> In `rustc`, the assembly macros implement handling of these attributes separately from the normal system that handles similar attributes in the language. This accounts for the limited kinds of attributes supported and may give rise to subtle differences in behavior.
294+
295+
r[asm.attributes.starts-with-template]
296+
Syntactically there must be at least one template string before the first operand.
297+
298+
```rust,compile_fail
299+
// This is rejected because `a = out(reg) x` does not parse as a
300+
// template string.
301+
core::arch::asm!(
302+
#[cfg(false)]
303+
a = out(reg) x, // ERROR.
304+
"",
305+
);
306+
```
307+
308+
[`cfg`]: conditional-compilation.md#the-cfg-attribute
309+
[`cfg_attr`]: conditional-compilation.md#the-cfg_attr-attribute
310+
269311
r[asm.operand-type]
270312
## Operand type
271313

0 commit comments

Comments
 (0)