Skip to content

Commit 841e6ba

Browse files
committed
document cfg conditions on inline assembly templates and operands
1 parent 76d5c46 commit 841e6ba

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/attributes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ Attributes may be applied to many forms in the language:
108108
* [Function][functions], [closure] and [function pointer]
109109
parameters accept outer attributes. This includes attributes on variadic parameters
110110
denoted with `...` in function pointers and [external blocks][variadic functions].
111+
* [Inline assembly] template strings and operands accept outer attributes.
111112
112113
r[attributes.meta]
113114
## Meta item attribute syntax
@@ -410,3 +411,4 @@ The following is an index of all built-in attributes.
410411
[variadic functions]: items/external-blocks.html#variadic-functions
411412
[`diagnostic::on_unimplemented`]: attributes/diagnostics.md#the-diagnosticon_unimplemented-attribute
412413
[`diagnostic::do_not_recommend`]: attributes/diagnostics.md#the-diagnosticdo_not_recommend-attribute
414+
[Inline assembly]: inline-assembly.md

src/inline-assembly.md

Lines changed: 38 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,39 @@ 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+
# #[cfg(target_arch = "x86_64")]
281+
core::arch::global_asm!(
282+
#[cfg(not(panic = "abort"))]
283+
".cfi_startproc",
284+
// ...
285+
"ret",
286+
#[cfg(not(panic = "abort"))]
287+
".cfi_endproc",
288+
);
289+
```
290+
291+
r[asm.attributes.starts-with-template]
292+
Syntactically there must be at least one template string before the first operand.
293+
294+
```rust, ignore
295+
// This is rejected because `a = out(reg) x` does not parse as a template string.
296+
core::arch::asm!(
297+
#[cfg(false)]
298+
a = out(reg) x, //~ ERROR expected token: `,`
299+
"",
300+
);
301+
```
302+
303+
[`cfg`]: conditional-compilation.md#the-cfg-attribute
304+
[`cfg_attr`]: conditional-compilation.md#the-cfg_attr-attribute
305+
269306
r[asm.operand-type]
270307
## Operand type
271308

0 commit comments

Comments
 (0)