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

asm: Allow multiple template string arguments; interpret them as newline-separated #73364

Merged
merged 4 commits into from
Jun 20, 2020

Commits on Jun 14, 2020

  1. asm: Unify pseudo-keyword parsing using eat, rather than a final `e…

    …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.
    joshtriplett committed Jun 14, 2020
    Configuration menu
    Copy the full SHA
    840176a View commit details
    Browse the repository at this point in the history

Commits on Jun 15, 2020

  1. asm: When pretty-printing, don't escape characters twice

    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...")`
    joshtriplett committed Jun 15, 2020
    Configuration menu
    Copy the full SHA
    50d6d4d View commit details
    Browse the repository at this point in the history
  2. asm: Allow multiple template strings; interpret them as newline-separ…

    …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.
    joshtriplett committed Jun 15, 2020
    Configuration menu
    Copy the full SHA
    1078b6f View commit details
    Browse the repository at this point in the history
  3. asm: Update chapter in unstable book for multiple template string arg…

    …uments
    
    Update all examples to use the new formatting, and update explanations
    to document it.
    joshtriplett committed Jun 15, 2020
    Configuration menu
    Copy the full SHA
    fd9ed30 View commit details
    Browse the repository at this point in the history