-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
asm: Allow multiple template string arguments; interpret them as newline-separated #73364
Conversation
…xpect` Currently, `asm!` parsing uses an `expect` for the last parsed pseudo-keyword (`sym`), which makes it difficult to extend without simultaneously refactoring. Use `eat` for the last pseudo-keyword, and then add an `else` that fails parsing. No change to error output.
pprust uses `print_string` to write out the template string, and `print_string` already calls `escape_debug`, so `impl fmt::Display for InlineAsmTemplatePiece` shouldn't do an additional `escape_debug`. This fixes a pretty-printing bug that translated `asm!("...\n...")` to `asm!("...\\n...")`
r? @eddyb (rust_highfive has picked a reviewer for you, use r? to override) |
r? @Amanieu |
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you checked that this is correctly handling implicitly number arguments in the template over multiple template strings? For example asm!("{}", "{}", in(reg) a, in(reg) b)
.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
f313364
to
407a436
Compare
…ated Allow the `asm!` macro to accept a series of template arguments, and interpret them as if they were concatenated with a '\n' between them. This allows writing an `asm!` where each line of assembly appears in a separate template string argument. This syntax makes it possible for rustfmt to reliably format and indent each line of assembly, without risking changes to the inside of a template string. It also avoids the complexity of having the user carefully format and indent a multi-line string (including where to put the surrounding quotes), and avoids the extra indentation and lines of a call to `concat!`. For example, rewriting the second example from the [blog post on the new inline assembly syntax](https://blog.rust-lang.org/inside-rust/2020/06/08/new-inline-asm.html) using multiple template strings: ```rust fn main() { let mut bits = [0u8; 64]; for value in 0..=1024u64 { let popcnt; unsafe { asm!( " popcnt {popcnt}, {v}", "2:", " blsi rax, {v}", " jz 1f", " xor {v}, rax", " tzcnt rax, rax", " stosb", " jmp 2b", "1:", v = inout(reg) value => _, popcnt = out(reg) popcnt, out("rax") _, // scratch inout("rdi") bits.as_mut_ptr() => _, ); } println!("bits of {}: {:?}", value, &bits[0..popcnt]); } } ``` Note that all the template strings must appear before all other arguments; you cannot, for instance, provide a series of template strings intermixed with the corresponding operands. In order to get srcloc mappings right for macros that generate multi-line string literals, create one line_span for each line in the string literal, each pointing to the macro. Make `rustc_parse_format::Parser::curarg` `pub`, so that we can propagate it from one template string argument to the next.
This comment has been minimized.
This comment has been minimized.
So it's not just C-style concatenation of neighboring string literals for people accustomed to it. |
…uments Update all examples to use the new formatting, and update explanations to document it.
Done; thanks! |
@bors r+ |
📌 Commit fd9ed30 has been approved by |
…arth Rollup of 13 pull requests Successful merges: - rust-lang#71568 (Document unsafety in slice/sort.rs) - rust-lang#72709 (`#[deny(unsafe_op_in_unsafe_fn)]` in liballoc) - rust-lang#73214 (Add asm!() support for hexagon) - rust-lang#73248 (save_analysis: improve handling of enum struct variant) - rust-lang#73257 (ty: projections in `transparent_newtype_field`) - rust-lang#73261 (Suggest `?Sized` when applicable for ADTs) - rust-lang#73300 (Implement crate-level-only lints checking.) - rust-lang#73334 (Note numeric literals that can never fit in an expected type) - rust-lang#73357 (Use `LocalDefId` for import IDs in trait map) - rust-lang#73364 (asm: Allow multiple template string arguments; interpret them as newline-separated) - rust-lang#73382 (Only display other method receiver candidates if they actually apply) - rust-lang#73465 (Add specialization of `ToString for char`) - rust-lang#73489 (Refactor hir::Place) Failed merges: r? @ghost
rust-lang/rust#73364 implemented support for providing multiple lines of assembly as separate arguments to `asm!`; update the blog post to use that new syntax, so that people who find it will use that style as an example.
* inline-asm: Update for new style rust-lang/rust#73364 implemented support for providing multiple lines of assembly as separate arguments to `asm!`; update the blog post to use that new syntax, so that people who find it will use that style as an example. * inline-asm: Outdent * inline asm: Update play link
* inline-asm: Update for new style rust-lang/rust#73364 implemented support for providing multiple lines of assembly as separate arguments to `asm!`; update the blog post to use that new syntax, so that people who find it will use that style as an example. * inline-asm: Outdent * inline asm: Update play link
Allow the
asm!
macro to accept a series of template arguments, and interpret them as if they were concatenated with a '\n' between them. This allows writing anasm!
where each line of assembly appears in a separate template string argument.This syntax makes it possible for rustfmt to reliably format and indent each line of assembly, without risking changes to the inside of a template string. It also avoids the complexity of having the user carefully format and indent a multi-line string (including where to put the surrounding quotes), and avoids the extra indentation and lines of a call to
concat!
.For example, rewriting the second example from the blog post on the new inline assembly syntax using multiple template strings:
Note that all the template strings must appear before all other arguments; you cannot, for instance, provide a series of template strings intermixed with the corresponding operands.