Skip to content

Commit 220ed09

Browse files
committed
Auto merge of #89316 - asquared31415:multiple-clobber-abi, r=Amanieu
Add support for specifying multiple clobber_abi in `asm!` r? `@Amanieu` cc #72016 `@rustbot` label: +A-inline-assembly +F-asm
2 parents f31622a + e80f300 commit 220ed09

File tree

14 files changed

+377
-163
lines changed

14 files changed

+377
-163
lines changed

compiler/rustc_ast/src/ast.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2058,7 +2058,7 @@ pub struct InlineAsm {
20582058
pub template: Vec<InlineAsmTemplatePiece>,
20592059
pub template_strs: Box<[(Symbol, Option<Symbol>, Span)]>,
20602060
pub operands: Vec<(InlineAsmOperand, Span)>,
2061-
pub clobber_abi: Option<(Symbol, Span)>,
2061+
pub clobber_abis: Vec<(Symbol, Span)>,
20622062
pub options: InlineAsmOptions,
20632063
pub line_spans: Vec<Span>,
20642064
}
@@ -2715,7 +2715,7 @@ pub enum ItemKind {
27152715
/// E.g., `extern {}` or `extern "C" {}`.
27162716
ForeignMod(ForeignMod),
27172717
/// Module-level inline assembly (from `global_asm!()`).
2718-
GlobalAsm(InlineAsm),
2718+
GlobalAsm(Box<InlineAsm>),
27192719
/// A type alias (`type`).
27202720
///
27212721
/// E.g., `type Foo = Bar<u8>;`.

compiler/rustc_ast_lowering/src/asm.rs

+40-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use super::LoweringContext;
22

33
use rustc_ast::*;
44
use rustc_data_structures::fx::FxHashMap;
5+
use rustc_data_structures::stable_set::FxHashSet;
56
use rustc_errors::struct_span_err;
67
use rustc_hir as hir;
78
use rustc_session::parse::feature_err;
@@ -49,22 +50,47 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
4950
.emit();
5051
}
5152

52-
let mut clobber_abi = None;
53+
let mut clobber_abis = FxHashMap::default();
5354
if let Some(asm_arch) = asm_arch {
54-
if let Some((abi_name, abi_span)) = asm.clobber_abi {
55-
match asm::InlineAsmClobberAbi::parse(asm_arch, &self.sess.target, abi_name) {
56-
Ok(abi) => clobber_abi = Some((abi, abi_span)),
55+
for (abi_name, abi_span) in &asm.clobber_abis {
56+
match asm::InlineAsmClobberAbi::parse(asm_arch, &self.sess.target, *abi_name) {
57+
Ok(abi) => {
58+
// If the abi was already in the list, emit an error
59+
match clobber_abis.get(&abi) {
60+
Some((prev_name, prev_sp)) => {
61+
let mut err = self.sess.struct_span_err(
62+
*abi_span,
63+
&format!("`{}` ABI specified multiple times", prev_name),
64+
);
65+
err.span_label(*prev_sp, "previously specified here");
66+
67+
// Multiple different abi names may actually be the same ABI
68+
// If the specified ABIs are not the same name, alert the user that they resolve to the same ABI
69+
let source_map = self.sess.source_map();
70+
if source_map.span_to_snippet(*prev_sp)
71+
!= source_map.span_to_snippet(*abi_span)
72+
{
73+
err.note("these ABIs are equivalent on the current target");
74+
}
75+
76+
err.emit();
77+
}
78+
None => {
79+
clobber_abis.insert(abi, (abi_name, *abi_span));
80+
}
81+
}
82+
}
5783
Err(&[]) => {
5884
self.sess
5985
.struct_span_err(
60-
abi_span,
86+
*abi_span,
6187
"`clobber_abi` is not supported on this target",
6288
)
6389
.emit();
6490
}
6591
Err(supported_abis) => {
6692
let mut err =
67-
self.sess.struct_span_err(abi_span, "invalid ABI for `clobber_abi`");
93+
self.sess.struct_span_err(*abi_span, "invalid ABI for `clobber_abi`");
6894
let mut abis = format!("`{}`", supported_abis[0]);
6995
for m in &supported_abis[1..] {
7096
let _ = write!(abis, ", `{}`", m);
@@ -348,8 +374,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
348374

349375
// If a clobber_abi is specified, add the necessary clobbers to the
350376
// operands list.
351-
if let Some((abi, abi_span)) = clobber_abi {
377+
let mut clobbered = FxHashSet::default();
378+
for (abi, (_, abi_span)) in clobber_abis {
352379
for &clobber in abi.clobbered_regs() {
380+
// Don't emit a clobber for a register already clobbered
381+
if clobbered.contains(&clobber) {
382+
continue;
383+
}
384+
353385
let mut output_used = false;
354386
clobber.overlapping_regs(|reg| {
355387
if used_output_regs.contains_key(&reg) {
@@ -366,6 +398,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
366398
},
367399
self.lower_span(abi_span),
368400
));
401+
clobbered.insert(clobber);
369402
}
370403
}
371404
}

compiler/rustc_ast_pretty/src/pprust/state.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2235,8 +2235,8 @@ impl<'a> State<'a> {
22352235

22362236
let mut args = vec![AsmArg::Template(InlineAsmTemplatePiece::to_string(&asm.template))];
22372237
args.extend(asm.operands.iter().map(|(o, _)| AsmArg::Operand(o)));
2238-
if let Some((abi, _)) = asm.clobber_abi {
2239-
args.push(AsmArg::ClobberAbi(abi));
2238+
for (abi, _) in &asm.clobber_abis {
2239+
args.push(AsmArg::ClobberAbi(*abi));
22402240
}
22412241
if !asm.options.is_empty() {
22422242
args.push(AsmArg::Options(asm.options));

compiler/rustc_builtin_macros/src/asm.rs

+61-31
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct AsmArgs {
1919
operands: Vec<(ast::InlineAsmOperand, Span)>,
2020
named_args: FxHashMap<Symbol, usize>,
2121
reg_args: FxHashSet<usize>,
22-
clobber_abi: Option<(Symbol, Span)>,
22+
clobber_abis: Vec<(Symbol, Span)>,
2323
options: ast::InlineAsmOptions,
2424
options_spans: Vec<Span>,
2525
}
@@ -64,7 +64,7 @@ fn parse_args<'a>(
6464
operands: vec![],
6565
named_args: FxHashMap::default(),
6666
reg_args: FxHashSet::default(),
67-
clobber_abi: None,
67+
clobber_abis: Vec::new(),
6868
options: ast::InlineAsmOptions::empty(),
6969
options_spans: vec![],
7070
};
@@ -210,9 +210,9 @@ fn parse_args<'a>(
210210
.span_labels(args.options_spans.clone(), "previous options")
211211
.span_label(span, "argument")
212212
.emit();
213-
} else if let Some((_, abi_span)) = args.clobber_abi {
213+
} else if let Some((_, abi_span)) = args.clobber_abis.last() {
214214
ecx.struct_span_err(span, "arguments are not allowed after clobber_abi")
215-
.span_label(abi_span, "clobber_abi")
215+
.span_label(*abi_span, "clobber_abi")
216216
.span_label(span, "argument")
217217
.emit();
218218
}
@@ -322,10 +322,13 @@ fn parse_args<'a>(
322322
// Bail out now since this is likely to confuse MIR
323323
return Err(err);
324324
}
325-
if let Some((_, abi_span)) = args.clobber_abi {
325+
326+
if args.clobber_abis.len() > 0 {
326327
if is_global_asm {
327-
let err =
328-
ecx.struct_span_err(abi_span, "`clobber_abi` cannot be used with `global_asm!`");
328+
let err = ecx.struct_span_err(
329+
args.clobber_abis.iter().map(|(_, span)| *span).collect::<Vec<Span>>(),
330+
"`clobber_abi` cannot be used with `global_asm!`",
331+
);
329332

330333
// Bail out now since this is likely to confuse later stages
331334
return Err(err);
@@ -335,7 +338,10 @@ fn parse_args<'a>(
335338
regclass_outputs.clone(),
336339
"asm with `clobber_abi` must specify explicit registers for outputs",
337340
)
338-
.span_label(abi_span, "clobber_abi")
341+
.span_labels(
342+
args.clobber_abis.iter().map(|(_, span)| *span).collect::<Vec<Span>>(),
343+
"clobber_abi",
344+
)
339345
.span_labels(regclass_outputs, "generic outputs")
340346
.emit();
341347
}
@@ -439,37 +445,61 @@ fn parse_clobber_abi<'a>(
439445

440446
p.expect(&token::OpenDelim(token::DelimToken::Paren))?;
441447

442-
let clobber_abi = match p.parse_str_lit() {
443-
Ok(str_lit) => str_lit.symbol_unescaped,
444-
Err(opt_lit) => {
445-
let span = opt_lit.map_or(p.token.span, |lit| lit.span);
446-
let mut err = p.sess.span_diagnostic.struct_span_err(span, "expected string literal");
447-
err.span_label(span, "not a string literal");
448-
return Err(err);
449-
}
450-
};
448+
if p.eat(&token::CloseDelim(token::DelimToken::Paren)) {
449+
let err = p.sess.span_diagnostic.struct_span_err(
450+
p.token.span,
451+
"at least one abi must be provided as an argument to `clobber_abi`",
452+
);
453+
return Err(err);
454+
}
451455

452-
p.expect(&token::CloseDelim(token::DelimToken::Paren))?;
456+
let mut new_abis = Vec::new();
457+
loop {
458+
match p.parse_str_lit() {
459+
Ok(str_lit) => {
460+
new_abis.push((str_lit.symbol_unescaped, str_lit.span));
461+
}
462+
Err(opt_lit) => {
463+
// If the non-string literal is a closing paren then it's the end of the list and is fine
464+
if p.eat(&token::CloseDelim(token::DelimToken::Paren)) {
465+
break;
466+
}
467+
let span = opt_lit.map_or(p.token.span, |lit| lit.span);
468+
let mut err =
469+
p.sess.span_diagnostic.struct_span_err(span, "expected string literal");
470+
err.span_label(span, "not a string literal");
471+
return Err(err);
472+
}
473+
};
453474

454-
let new_span = span_start.to(p.prev_token.span);
475+
// Allow trailing commas
476+
if p.eat(&token::CloseDelim(token::DelimToken::Paren)) {
477+
break;
478+
}
479+
p.expect(&token::Comma)?;
480+
}
455481

456-
if let Some((_, prev_span)) = args.clobber_abi {
457-
let mut err = p
458-
.sess
459-
.span_diagnostic
460-
.struct_span_err(new_span, "clobber_abi specified multiple times");
461-
err.span_label(prev_span, "clobber_abi previously specified here");
462-
return Err(err);
463-
} else if !args.options_spans.is_empty() {
482+
let full_span = span_start.to(p.prev_token.span);
483+
484+
if !args.options_spans.is_empty() {
464485
let mut err = p
465486
.sess
466487
.span_diagnostic
467-
.struct_span_err(new_span, "clobber_abi is not allowed after options");
488+
.struct_span_err(full_span, "clobber_abi is not allowed after options");
468489
err.span_labels(args.options_spans.clone(), "options");
469490
return Err(err);
470491
}
471492

472-
args.clobber_abi = Some((clobber_abi, new_span));
493+
match &new_abis[..] {
494+
// should have errored above during parsing
495+
[] => unreachable!(),
496+
[(abi, _span)] => args.clobber_abis.push((*abi, full_span)),
497+
[abis @ ..] => {
498+
for (abi, span) in abis {
499+
args.clobber_abis.push((*abi, *span));
500+
}
501+
}
502+
}
473503

474504
Ok(())
475505
}
@@ -770,7 +800,7 @@ fn expand_preparsed_asm(ecx: &mut ExtCtxt<'_>, args: AsmArgs) -> Option<ast::Inl
770800
template,
771801
template_strs: template_strs.into_boxed_slice(),
772802
operands: args.operands,
773-
clobber_abi: args.clobber_abi,
803+
clobber_abis: args.clobber_abis,
774804
options: args.options,
775805
line_spans,
776806
})
@@ -815,7 +845,7 @@ pub fn expand_global_asm<'cx>(
815845
ident: Ident::empty(),
816846
attrs: Vec::new(),
817847
id: ast::DUMMY_NODE_ID,
818-
kind: ast::ItemKind::GlobalAsm(inline_asm),
848+
kind: ast::ItemKind::GlobalAsm(Box::new(inline_asm)),
819849
vis: ast::Visibility {
820850
span: sp.shrink_to_lo(),
821851
kind: ast::VisibilityKind::Inherited,

src/doc/unstable-book/src/library-features/asm.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ fn call_foo(arg: i32) -> i32 {
319319

320320
Note that the `fn` or `static` item does not need to be public or `#[no_mangle]`: the compiler will automatically insert the appropriate mangled symbol name into the assembly code.
321321

322-
By default, `asm!` assumes that any register not specified as an output will have its contents preserved by the assembly code. The [`clobber_abi`](#abi-clobbers) argument to `asm!` tells the compiler to automatically insert the necessary clobber operands according to the given calling convention ABI: any register which is not fully preserved in that ABI will be treated as clobbered.
322+
By default, `asm!` assumes that any register not specified as an output will have its contents preserved by the assembly code. The [`clobber_abi`](#abi-clobbers) argument to `asm!` tells the compiler to automatically insert the necessary clobber operands according to the given calling convention ABI: any register which is not fully preserved in that ABI will be treated as clobbered. Multiple `clobber_abi` arguments may be provided and all clobbers from all specified ABIs will be inserted.
323323

324324
## Register template modifiers
325325

@@ -453,10 +453,10 @@ reg_spec := <register class> / "<explicit register>"
453453
operand_expr := expr / "_" / expr "=>" expr / expr "=>" "_"
454454
reg_operand := dir_spec "(" reg_spec ")" operand_expr
455455
operand := reg_operand / "const" const_expr / "sym" path
456-
clobber_abi := "clobber_abi(" <abi> ")"
456+
clobber_abi := "clobber_abi(" <abi> *["," <abi>] [","] ")"
457457
option := "pure" / "nomem" / "readonly" / "preserves_flags" / "noreturn" / "nostack" / "att_syntax" / "raw"
458458
options := "options(" option *["," option] [","] ")"
459-
asm := "asm!(" format_string *("," format_string) *("," [ident "="] operand) ["," clobber_abi] *("," options) [","] ")"
459+
asm := "asm!(" format_string *("," format_string) *("," [ident "="] operand) *("," clobber_abi) *("," options) [","] ")"
460460
```
461461

462462
Inline assembly is currently supported on the following architectures:
@@ -802,6 +802,8 @@ As stated in the previous section, passing an input value smaller than the regis
802802

803803
The `clobber_abi` keyword can be used to apply a default set of clobbers to an `asm` block. This will automatically insert the necessary clobber constraints as needed for calling a function with a particular calling convention: if the calling convention does not fully preserve the value of a register across a call then a `lateout("reg") _` is implicitly added to the operands list.
804804

805+
`clobber_abi` may be specified any number of times. It will insert a clobber for all unique registers in the union of all specified calling conventions.
806+
805807
Generic register class outputs are disallowed by the compiler when `clobber_abi` is used: all outputs must specify an explicit register. Explicit register outputs have precedence over the implicit clobbers inserted by `clobber_abi`: a clobber will only be inserted for a register if that register is not used as an output.
806808
The following ABIs can be used with `clobber_abi`:
807809

src/test/ui/asm/aarch64/parse-error.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,16 @@ fn main() {
4040
asm!("", clobber_abi(foo));
4141
//~^ ERROR expected string literal
4242
asm!("", clobber_abi("C" foo));
43-
//~^ ERROR expected `)`, found `foo`
43+
//~^ ERROR expected one of `)` or `,`, found `foo`
4444
asm!("", clobber_abi("C", foo));
45-
//~^ ERROR expected `)`, found `,`
45+
//~^ ERROR expected string literal
4646
asm!("{}", clobber_abi("C"), const foo);
4747
//~^ ERROR arguments are not allowed after clobber_abi
4848
//~^^ ERROR attempt to use a non-constant value in a constant
4949
asm!("", options(), clobber_abi("C"));
5050
//~^ ERROR clobber_abi is not allowed after options
5151
asm!("{}", options(), clobber_abi("C"), const foo);
5252
//~^ ERROR clobber_abi is not allowed after options
53-
asm!("", clobber_abi("C"), clobber_abi("C"));
54-
//~^ ERROR clobber_abi specified multiple times
5553
asm!("{a}", a = const foo, a = const bar);
5654
//~^ ERROR duplicate argument named `a`
5755
//~^^ ERROR argument never used
@@ -110,18 +108,16 @@ global_asm!("{}", options(), const FOO);
110108
global_asm!("", clobber_abi(FOO));
111109
//~^ ERROR expected string literal
112110
global_asm!("", clobber_abi("C" FOO));
113-
//~^ ERROR expected `)`, found `FOO`
111+
//~^ ERROR expected one of `)` or `,`, found `FOO`
114112
global_asm!("", clobber_abi("C", FOO));
115-
//~^ ERROR expected `)`, found `,`
113+
//~^ ERROR expected string literal
116114
global_asm!("{}", clobber_abi("C"), const FOO);
117115
//~^ ERROR arguments are not allowed after clobber_abi
118116
//~^^ ERROR `clobber_abi` cannot be used with `global_asm!`
119117
global_asm!("", options(), clobber_abi("C"));
120118
//~^ ERROR clobber_abi is not allowed after options
121119
global_asm!("{}", options(), clobber_abi("C"), const FOO);
122120
//~^ ERROR clobber_abi is not allowed after options
123-
global_asm!("", clobber_abi("C"), clobber_abi("C"));
124-
//~^ ERROR clobber_abi specified multiple times
125121
global_asm!("{a}", a = const FOO, a = const BAR);
126122
//~^ ERROR duplicate argument named `a`
127123
//~^^ ERROR argument never used

0 commit comments

Comments
 (0)