Skip to content

Commit b0d3075

Browse files
committed
Use the reference grammar for inline assembly
This updates the inline assembly grammar description to use the grammar style used in the rest of the reference, which also supports diagrams. I also appreciate that this is more precisely defined using existing grammar productions. I realize that this is not as compressed as before. If that is a real problem, then I think we can look at improving that throughout the reference in general. I did not change other parts of the chapter even though they mention things like `in(<reg>) <expr>`. I could rewrite those, though I'm not sure if that would help a lot. There are restrictions not captured in the grammar which are explained in the text. For example, global_asm and naked_asm do not allow clobber_abi. I think I'm fine with the grammar not including everything since there isn't the concept of an AST here. This is not a direct translation because the old grammar was not entirely precise or match with the existing grammar. This also renames the rules to use PascalCase. The exhaustive list of differences: Removed: - Removed the `asm`, `naked_asm` and `global_asm` rules since I don't think they really added anything, and the macro names aren't exactly part of the grammar being described here. Instead it is just `AsmArgs`. - Removed the explicit `_` since that is now a normal Expression. New: - FormatString added MacroInvocation because that is supported. - Defined RegisterClass. - Defined ExplicitRegister. Changed: - Renamed option and options to AsmOption and AsmOptions, and operand to AsmOperand (since our namespace is global). - Clarified the meaning of "ident". - ArgName allows ident, keywords, and raw identifiers. - RegisterClass allows ident, keywords, but NOT raw identifiers. - Split DirSpec into two, since there is a separate `Expression => Expression` syntax that is only valid for a subset of DirSpecs. - Clarified that `sym` takes a PathExpression. - Clarified that label's block doesn't allow inner attributes. - Fixed sym/const/label which weren't properly quoted. - Fixed some ambiguous precedence like alternation in reg_operand. - Fixed `options` that it can be empty.
1 parent 5db783a commit b0d3075

File tree

1 file changed

+56
-15
lines changed

1 file changed

+56
-15
lines changed

src/inline-assembly.md

+56-15
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,62 @@ assert_eq!(x, 4 * 6);
4646
r[asm.syntax]
4747
## Syntax
4848

49-
The following ABNF specifies the general syntax:
50-
51-
```text
52-
format_string := STRING_LITERAL / RAW_STRING_LITERAL
53-
dir_spec := "in" / "out" / "lateout" / "inout" / "inlateout"
54-
reg_spec := <register class> / "\"" <explicit register> "\""
55-
operand_expr := expr / "_" / expr "=>" expr / expr "=>" "_"
56-
reg_operand := [ident "="] dir_spec "(" reg_spec ")" operand_expr / sym <path> / const <expr> / label <block>
57-
clobber_abi := "clobber_abi(" <abi> *("," <abi>) [","] ")"
58-
option := "pure" / "nomem" / "readonly" / "preserves_flags" / "noreturn" / "nostack" / "att_syntax" / "raw"
59-
options := "options(" option *("," option) [","] ")"
60-
operand := reg_operand / clobber_abi / options
61-
asm := "asm!(" format_string *("," format_string) *("," operand) [","] ")"
62-
naked_asm := "naked_asm!(" format_string *("," format_string) *("," operand) [","] ")"
63-
global_asm := "global_asm!(" format_string *("," format_string) *("," operand) [","] ")"
49+
The following grammar specifies the arguments that can be passed to the `asm!`, `global_asm!` and `naked_asm!` macros.
50+
51+
```grammar,assembly
52+
@root AsmArgs -> FormatString (`,` FormatString)* (`,` AsmOperand)* `,`?
53+
54+
FormatString -> STRING_LITERAL | RAW_STRING_LITERAL | MacroInvocation
55+
56+
AsmOperand ->
57+
ClobberAbi
58+
| AsmOptions
59+
| RegOperand
60+
61+
ClobberAbi -> `clobber_abi` `(` Abi (`,` Abi)* `,`? `)`
62+
63+
AsmOptions ->
64+
`options` `(` ( AsmOption (`,` AsmOption)* `,`? )? `)`
65+
66+
AsmOption ->
67+
`pure`
68+
| `nomem`
69+
| `readonly`
70+
| `preserves_flags`
71+
| `noreturn`
72+
| `nostack`
73+
| `att_syntax`
74+
| `raw`
75+
76+
RegOperand -> (ParamName `=`)?
77+
(
78+
DirSpec `(` RegSpec `)` Expression
79+
| DualDirSpec `(` RegSpec `)` DualDirSpecExpression
80+
| `sym` PathExpression
81+
| `const` Expression
82+
| `label` `{` Statements? `}`
83+
)
84+
85+
ParamName -> IDENTIFIER_OR_KEYWORD | RAW_IDENTIFIER
86+
87+
DualDirSpecExpression ->
88+
Expression
89+
| Expression `=>` Expression
90+
91+
RegSpec -> RegisterClass | ExplicitRegister
92+
93+
RegisterClass -> IDENTIFIER_OR_KEYWORD
94+
95+
ExplicitRegister -> STRING_LITERAL
96+
97+
DirSpec ->
98+
`in`
99+
| `out`
100+
| `lateout`
101+
102+
DualDirSpec ->
103+
`inout`
104+
| `inlateout`
64105
```
65106

66107
r[asm.scope]

0 commit comments

Comments
 (0)