2023-06-15 21:45:53 +00:00
|
|
|
// needs-asm-support
|
2020-02-20 09:19:48 +00:00
|
|
|
|
2021-12-10 00:15:33 +00:00
|
|
|
#![feature(asm_const)]
|
|
|
|
|
|
|
|
use std::arch::{asm, global_asm};
|
2020-02-20 09:19:48 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let mut foo = 0;
|
|
|
|
let mut bar = 0;
|
|
|
|
unsafe {
|
|
|
|
asm!();
|
|
|
|
//~^ ERROR requires at least a template string argument
|
|
|
|
asm!(foo);
|
|
|
|
//~^ ERROR asm template must be a string literal
|
|
|
|
asm!("{}" foo);
|
|
|
|
//~^ ERROR expected token: `,`
|
|
|
|
asm!("{}", foo);
|
2021-07-29 11:43:26 +00:00
|
|
|
//~^ ERROR expected operand, clobber_abi, options, or additional template string
|
2020-02-20 09:19:48 +00:00
|
|
|
asm!("{}", in foo);
|
|
|
|
//~^ ERROR expected `(`, found `foo`
|
|
|
|
asm!("{}", in(reg foo));
|
|
|
|
//~^ ERROR expected `)`, found `foo`
|
|
|
|
asm!("{}", in(reg));
|
|
|
|
//~^ ERROR expected expression, found end of macro arguments
|
|
|
|
asm!("{}", inout(=) foo => bar);
|
|
|
|
//~^ ERROR expected register class or explicit register
|
|
|
|
asm!("{}", inout(reg) foo =>);
|
|
|
|
//~^ ERROR expected expression, found end of macro arguments
|
|
|
|
asm!("{}", in(reg) foo => bar);
|
|
|
|
//~^ ERROR expected one of `!`, `,`, `.`, `::`, `?`, `{`, or an operator, found `=>`
|
|
|
|
asm!("{}", sym foo + bar);
|
2022-03-01 00:54:28 +00:00
|
|
|
//~^ ERROR expected a path for argument to `sym`
|
2020-02-20 09:19:48 +00:00
|
|
|
asm!("", options(foo));
|
|
|
|
//~^ ERROR expected one of
|
|
|
|
asm!("", options(nomem foo));
|
|
|
|
//~^ ERROR expected one of
|
|
|
|
asm!("", options(nomem, foo));
|
|
|
|
//~^ ERROR expected one of
|
|
|
|
asm!("{}", options(), const foo);
|
2022-12-16 04:20:34 +00:00
|
|
|
//~^ ERROR attempt to use a non-constant value in a constant
|
2023-06-15 21:45:53 +00:00
|
|
|
|
|
|
|
// test that asm!'s clobber_abi doesn't accept non-string literals
|
|
|
|
// see also https://github.com/rust-lang/rust/issues/112635
|
2021-10-14 07:23:09 +00:00
|
|
|
asm!("", clobber_abi());
|
|
|
|
//~^ ERROR at least one abi must be provided
|
2021-07-29 11:43:26 +00:00
|
|
|
asm!("", clobber_abi(foo));
|
|
|
|
//~^ ERROR expected string literal
|
|
|
|
asm!("", clobber_abi("C" foo));
|
2021-10-14 07:23:09 +00:00
|
|
|
//~^ ERROR expected one of `)` or `,`, found `foo`
|
2021-07-29 11:43:26 +00:00
|
|
|
asm!("", clobber_abi("C", foo));
|
2021-10-14 07:23:09 +00:00
|
|
|
//~^ ERROR expected string literal
|
2023-06-15 21:45:53 +00:00
|
|
|
asm!("", clobber_abi(1));
|
|
|
|
//~^ ERROR expected string literal
|
|
|
|
asm!("", clobber_abi(()));
|
|
|
|
//~^ ERROR expected string literal
|
|
|
|
asm!("", clobber_abi(uwu));
|
|
|
|
//~^ ERROR expected string literal
|
|
|
|
asm!("", clobber_abi({}));
|
|
|
|
//~^ ERROR expected string literal
|
|
|
|
asm!("", clobber_abi(loop {}));
|
|
|
|
//~^ ERROR expected string literal
|
|
|
|
asm!("", clobber_abi(if));
|
|
|
|
//~^ ERROR expected string literal
|
|
|
|
asm!("", clobber_abi(do));
|
|
|
|
//~^ ERROR expected string literal
|
|
|
|
asm!("", clobber_abi(<));
|
|
|
|
//~^ ERROR expected string literal
|
|
|
|
asm!("", clobber_abi(.));
|
|
|
|
//~^ ERROR expected string literal
|
|
|
|
|
2021-07-29 11:43:26 +00:00
|
|
|
asm!("{}", clobber_abi("C"), const foo);
|
2022-12-16 04:20:34 +00:00
|
|
|
//~^ ERROR attempt to use a non-constant value in a constant
|
2021-07-29 11:43:26 +00:00
|
|
|
asm!("", options(), clobber_abi("C"));
|
|
|
|
asm!("{}", options(), clobber_abi("C"), const foo);
|
2022-12-16 04:20:34 +00:00
|
|
|
//~^ ERROR attempt to use a non-constant value in a constant
|
2020-02-20 09:19:48 +00:00
|
|
|
asm!("{a}", a = const foo, a = const bar);
|
|
|
|
//~^ ERROR duplicate argument named `a`
|
|
|
|
//~^^ ERROR argument never used
|
2021-04-06 04:50:55 +00:00
|
|
|
//~^^^ ERROR attempt to use a non-constant value in a constant
|
|
|
|
//~^^^^ ERROR attempt to use a non-constant value in a constant
|
2023-06-15 21:45:53 +00:00
|
|
|
|
asm: Allow multiple template strings; 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.
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.
2020-06-15 06:33:55 +00:00
|
|
|
asm!("", options(), "");
|
|
|
|
//~^ ERROR expected one of
|
|
|
|
asm!("{}", in(reg) foo, "{}", out(reg) foo);
|
|
|
|
//~^ ERROR expected one of
|
|
|
|
asm!(format!("{{{}}}", 0), in(reg) foo);
|
|
|
|
//~^ ERROR asm template must be a string literal
|
|
|
|
asm!("{1}", format!("{{{}}}", 0), in(reg) foo, out(reg) bar);
|
|
|
|
//~^ ERROR asm template must be a string literal
|
2021-08-21 14:47:24 +00:00
|
|
|
asm!("{}", in(reg) _);
|
|
|
|
//~^ ERROR _ cannot be used for input operands
|
|
|
|
asm!("{}", inout(reg) _);
|
|
|
|
//~^ ERROR _ cannot be used for input operands
|
|
|
|
asm!("{}", inlateout(reg) _);
|
|
|
|
//~^ ERROR _ cannot be used for input operands
|
2020-02-20 09:19:48 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-13 17:11:11 +00:00
|
|
|
|
|
|
|
const FOO: i32 = 1;
|
|
|
|
const BAR: i32 = 2;
|
|
|
|
global_asm!();
|
|
|
|
//~^ ERROR requires at least a template string argument
|
|
|
|
global_asm!(FOO);
|
|
|
|
//~^ ERROR asm template must be a string literal
|
|
|
|
global_asm!("{}" FOO);
|
|
|
|
//~^ ERROR expected token: `,`
|
|
|
|
global_asm!("{}", FOO);
|
|
|
|
//~^ ERROR expected operand, options, or additional template string
|
|
|
|
global_asm!("{}", const);
|
|
|
|
//~^ ERROR expected expression, found end of macro arguments
|
|
|
|
global_asm!("{}", const(reg) FOO);
|
|
|
|
//~^ ERROR expected one of
|
|
|
|
global_asm!("", options(FOO));
|
|
|
|
//~^ ERROR expected one of
|
|
|
|
global_asm!("", options(nomem FOO));
|
|
|
|
//~^ ERROR expected one of
|
|
|
|
global_asm!("", options(nomem, FOO));
|
|
|
|
//~^ ERROR expected one of
|
|
|
|
global_asm!("{}", options(), const FOO);
|
2021-07-29 11:43:26 +00:00
|
|
|
global_asm!("", clobber_abi(FOO));
|
|
|
|
//~^ ERROR expected string literal
|
|
|
|
global_asm!("", clobber_abi("C" FOO));
|
2021-10-14 07:23:09 +00:00
|
|
|
//~^ ERROR expected one of `)` or `,`, found `FOO`
|
2021-07-29 11:43:26 +00:00
|
|
|
global_asm!("", clobber_abi("C", FOO));
|
2021-10-14 07:23:09 +00:00
|
|
|
//~^ ERROR expected string literal
|
2021-07-29 11:43:26 +00:00
|
|
|
global_asm!("{}", clobber_abi("C"), const FOO);
|
2022-12-16 04:20:34 +00:00
|
|
|
//~^ ERROR `clobber_abi` cannot be used with `global_asm!`
|
2021-07-29 11:43:26 +00:00
|
|
|
global_asm!("", options(), clobber_abi("C"));
|
2022-12-16 04:20:34 +00:00
|
|
|
//~^ ERROR `clobber_abi` cannot be used with `global_asm!`
|
2021-07-29 11:43:26 +00:00
|
|
|
global_asm!("{}", options(), clobber_abi("C"), const FOO);
|
2022-12-16 04:20:34 +00:00
|
|
|
//~^ ERROR `clobber_abi` cannot be used with `global_asm!`
|
2021-07-29 11:43:26 +00:00
|
|
|
global_asm!("", clobber_abi("C"), clobber_abi("C"));
|
2021-10-14 07:23:09 +00:00
|
|
|
//~^ ERROR `clobber_abi` cannot be used with `global_asm!`
|
2021-04-13 17:11:11 +00:00
|
|
|
global_asm!("{a}", a = const FOO, a = const BAR);
|
|
|
|
//~^ ERROR duplicate argument named `a`
|
|
|
|
//~^^ ERROR argument never used
|
|
|
|
global_asm!("", options(), "");
|
|
|
|
//~^ ERROR expected one of
|
|
|
|
global_asm!("{}", const FOO, "{}", const FOO);
|
|
|
|
//~^ ERROR expected one of
|
|
|
|
global_asm!(format!("{{{}}}", 0), const FOO);
|
|
|
|
//~^ ERROR asm template must be a string literal
|
|
|
|
global_asm!("{1}", format!("{{{}}}", 0), const FOO, const BAR);
|
|
|
|
//~^ ERROR asm template must be a string literal
|