Commit 687f929
authored
Rollup merge of rust-lang#73364 - joshtriplett:inline-asm, r=Amanieu
asm: Allow multiple template string arguments; interpret them as newline-separated
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.File tree
10 files changed
+570
-178
lines changed- src
- doc/unstable-book/src/library-features
- librustc_ast
- librustc_builtin_macros
- librustc_parse_format
- test
- pretty
- ui/asm
10 files changed
+570
-178
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
68 | 68 | | |
69 | 69 | | |
70 | 70 | | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
75 | 78 | | |
76 | 79 | | |
77 | 80 | | |
| |||
82 | 85 | | |
83 | 86 | | |
84 | 87 | | |
85 | | - | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
86 | 94 | | |
87 | | - | |
| 95 | + | |
88 | 96 | | |
89 | 97 | | |
90 | 98 | | |
91 | | - | |
| 99 | + | |
92 | 100 | | |
93 | 101 | | |
94 | 102 | | |
| |||
137 | 145 | | |
138 | 146 | | |
139 | 147 | | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
144 | 155 | | |
145 | 156 | | |
146 | 157 | | |
| |||
233 | 244 | | |
234 | 245 | | |
235 | 246 | | |
236 | | - | |
| 247 | + | |
237 | 248 | | |
238 | 249 | | |
239 | 250 | | |
| |||
255 | 266 | | |
256 | 267 | | |
257 | 268 | | |
258 | | - | |
259 | | - | |
260 | | - | |
261 | | - | |
262 | | - | |
263 | | - | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
264 | 277 | | |
265 | 278 | | |
266 | 279 | | |
| |||
338 | 351 | | |
339 | 352 | | |
340 | 353 | | |
341 | | - | |
| 354 | + | |
342 | 355 | | |
343 | 356 | | |
344 | 357 | | |
| |||
371 | 384 | | |
372 | 385 | | |
373 | 386 | | |
374 | | - | |
| 387 | + | |
375 | 388 | | |
376 | 389 | | |
377 | 390 | | |
378 | 391 | | |
379 | 392 | | |
380 | 393 | | |
381 | | - | |
| 394 | + | |
382 | 395 | | |
383 | 396 | | |
384 | 397 | | |
| 398 | + | |
| 399 | + | |
385 | 400 | | |
386 | 401 | | |
387 | 402 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1905 | 1905 | | |
1906 | 1906 | | |
1907 | 1907 | | |
1908 | | - | |
| 1908 | + | |
1909 | 1909 | | |
1910 | 1910 | | |
1911 | 1911 | | |
| |||
0 commit comments