Skip to content
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

Relax ordering rules for asm! operands #1323

Merged
merged 1 commit into from
Mar 26, 2023
Merged
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
11 changes: 5 additions & 6 deletions src/inline-assembly.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ 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 := dir_spec "(" reg_spec ")" operand_expr
operand := reg_operand
reg_operand := [ident "="] dir_spec "(" reg_spec ")" operand_expr
clobber_abi := "clobber_abi(" <abi> *("," <abi>) [","] ")"
option := "pure" / "nomem" / "readonly" / "preserves_flags" / "noreturn" / "nostack" / "att_syntax" / "raw"
options := "options(" option *("," option) [","] ")"
asm := "asm!(" format_string *("," format_string) *("," [ident "="] operand) *("," clobber_abi) *("," options) [","] ")"
global_asm := "global_asm!(" format_string *("," format_string) *("," [ident "="] operand) *("," options) [","] ")"
operand := reg_operand / clobber_abi / options
asm := "asm!(" format_string *("," format_string) *("," operand) [","] ")"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This grammar doesn't seem to capture the order requirements for how it is parsed. Am I missing something for how this is defined? That is, why is it not something closer to:

asm := "asm!(" format_string *("," format_string) *("," operand) *("," ident "=" operand) *("," options) [","] ")"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new rules allow options and operands to be freely mixed, so there are no order requirements anymore (except that the template strings must come first).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what I'm getting after is to capture the rule that positional operands must come before named operands. This grammar seems to allow:

let x: i64;
asm!("add {}, {foo}, {}", out(reg) x, foo=in(reg) 2, in(reg) 1);

But that generates an error "positional arguments cannot follow named arguments or explicit register arguments".

Copy link
Member Author

@Amanieu Amanieu Mar 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but then again options and clobber_abi can appear in any position between named & positional arguments. It's a bit difficult to express all the details in the grammar.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand how it parses correctly, the following doesn't seem too unwieldy:

asm := "asm!(" format_string *("," format_string) operands ")"
operands := *("," reg_operand / clobber_abi / options) *("," ident "=" reg_operand) *("," clobber_abi / options)

Does that seem like it would be OK to have?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I gave this a try but I think it just makes the grammar harder to read overall. I prefer leaving it as it currently is and describing the operand ordering constraints in the text.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example you gave isn't actually correct since it doesn't cover explicit register operands which don't have names and can be mixed with named operands.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fair. Thanks for taking a look!

global_asm := "global_asm!(" format_string *("," format_string) *("," operand) [","] ")"
```


Expand All @@ -74,8 +74,7 @@ An `asm!` invocation may have one or more template string arguments; an `asm!` w
The expected usage is for each template string argument to correspond to a line of assembly code.
All template string arguments must appear before any other arguments.

As with format strings, named arguments must appear after positional arguments.
Explicit [register operands](#register-operands) must appear at the end of the operand list, after named arguments if any.
As with format strings, positional arguments must appear before named arguments and explicit [register operands](#register-operands).

Explicit register operands cannot be used by placeholders in the template string.
All other named and positional operands must appear at least once in the template string, otherwise a compiler error is generated.
Expand Down