Skip to content

Use the reference grammar for inline assembly #1807

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 61 additions & 16 deletions src/inline-assembly.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Support for inline assembly is stable on the following architectures:
- LoongArch
- s390x

The compiler will emit an error if `asm!` is used on an unsupported target.
The compiler will emit an error if an assembly macro is used on an unsupported target.

r[asm.example]
## Example
Expand Down Expand Up @@ -46,21 +46,66 @@ assert_eq!(x, 4 * 6);
r[asm.syntax]
## Syntax

The following ABNF specifies the general syntax:

```text
format_string := STRING_LITERAL / RAW_STRING_LITERAL
dir_spec := "in" / "out" / "lateout" / "inout" / "inlateout"
reg_spec := <register class> / "\"" <explicit register> "\""
operand_expr := expr / "_" / expr "=>" expr / expr "=>" "_"
reg_operand := [ident "="] dir_spec "(" reg_spec ")" operand_expr / sym <path> / const <expr> / label <block>
clobber_abi := "clobber_abi(" <abi> *("," <abi>) [","] ")"
option := "pure" / "nomem" / "readonly" / "preserves_flags" / "noreturn" / "nostack" / "att_syntax" / "raw"
options := "options(" option *("," option) [","] ")"
operand := reg_operand / clobber_abi / options
asm := "asm!(" format_string *("," format_string) *("," operand) [","] ")"
naked_asm := "naked_asm!(" format_string *("," format_string) *("," operand) [","] ")"
global_asm := "global_asm!(" format_string *("," format_string) *("," operand) [","] ")"
The following grammar specifies the arguments that can be passed to the `asm!`, `global_asm!` and `naked_asm!` macros.

```grammar,assembly
@root AsmArgs -> FormatString (`,` FormatString)* (`,` Operand)* `,`?

FormatString -> StringLiteral | MacroInvocation

StringLiteral -> STRING_LITERAL | RAW_STRING_LITERAL

Operand ->
ClobberAbi
| AsmOptions
| RegOperand

ClobberAbi -> `clobber_abi` `(` AbiString (`,` AbiString)* `,`? `)`

AbiString -> StringLiteral

AsmOptions ->
`options` `(` AsmOption (`,` AsmOption)* `,`? `)`

AsmOption ->
`pure`
| `nomem`
| `readonly`
| `preserves_flags`
| `noreturn`
| `nostack`
| `att_syntax`
| `raw`

RegOperand -> (ArgName `=`)?
(
DirSpec `(` RegSpec `)` Expression
| DualDirSpec `(` RegSpec `)` DualDirSpecExpression
| `sym` PathExpression
| `const` Expression
| `label` BlockExpression _no [InnerAttribute]s_
)

ArgName -> IDENTIFIER_OR_KEYWORD | RAW_IDENTIFIER

DualDirSpecExpression ->
Expression
| Expression `=>` Expression

RegSpec -> RegisterClass | ExplicitRegister

RegisterClass -> IDENTIFIER_OR_KEYWORD

ExplicitRegister -> STRING_LITERAL

DirSpec ->
`in`
| `out`
| `lateout`

DualDirSpec ->
`inout`
| `inlateout`
```

r[asm.scope]
Expand Down
Loading