Skip to content

Commit b6f2700

Browse files
committed
Add support for specifying multiple clobber_abi in asm!
Allow multiple clobber_abi in asm Update docs Fix aarch64 test Combine abis Emit duplicate ABI error, empty ABI list error multiple clobber_abi
1 parent 00d5e42 commit b6f2700

File tree

14 files changed

+368
-145
lines changed

14 files changed

+368
-145
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
}
@@ -2707,7 +2707,7 @@ pub enum ItemKind {
27072707
/// E.g., `extern {}` or `extern "C" {}`.
27082708
ForeignMod(ForeignMod),
27092709
/// Module-level inline assembly (from `global_asm!()`).
2710-
GlobalAsm(InlineAsm),
2710+
GlobalAsm(Box<InlineAsm>),
27112711
/// A type alias (`type`).
27122712
///
27132713
/// 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_span::{Span, Symbol};
@@ -27,22 +28,47 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
2728
.emit();
2829
}
2930

30-
let mut clobber_abi = None;
31+
let mut clobber_abis = FxHashMap::default();
3132
if let Some(asm_arch) = asm_arch {
32-
if let Some((abi_name, abi_span)) = asm.clobber_abi {
33-
match asm::InlineAsmClobberAbi::parse(asm_arch, &self.sess.target, abi_name) {
34-
Ok(abi) => clobber_abi = Some((abi, abi_span)),
33+
for (abi_name, abi_span) in &asm.clobber_abis {
34+
match asm::InlineAsmClobberAbi::parse(asm_arch, &self.sess.target, *abi_name) {
35+
Ok(abi) => {
36+
// If the abi was already in the list, emit an error
37+
match clobber_abis.get(&abi) {
38+
Some((prev_name, prev_sp)) => {
39+
let mut err = self.sess.struct_span_err(
40+
*abi_span,
41+
&format!("`{}` ABI specified multiple times", prev_name),
42+
);
43+
err.span_label(*prev_sp, "previously specified here");
44+
45+
// Multiple different abi names may actually be the same ABI
46+
// If the specified ABIs are not the same name, alert the user that they resolve to the same ABI
47+
let source_map = self.sess.source_map();
48+
if source_map.span_to_snippet(*prev_sp)
49+
!= source_map.span_to_snippet(*abi_span)
50+
{
51+
err.note("these ABIs are equivalent on the current target");
52+
}
53+
54+
err.emit();
55+
}
56+
None => {
57+
clobber_abis.insert(abi, (abi_name, *abi_span));
58+
}
59+
}
60+
}
3561
Err(&[]) => {
3662
self.sess
3763
.struct_span_err(
38-
abi_span,
64+
*abi_span,
3965
"`clobber_abi` is not supported on this target",
4066
)
4167
.emit();
4268
}
4369
Err(supported_abis) => {
4470
let mut err =
45-
self.sess.struct_span_err(abi_span, "invalid ABI for `clobber_abi`");
71+
self.sess.struct_span_err(*abi_span, "invalid ABI for `clobber_abi`");
4672
let mut abis = format!("`{}`", supported_abis[0]);
4773
for m in &supported_abis[1..] {
4874
let _ = write!(abis, ", `{}`", m);
@@ -308,8 +334,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
308334

309335
// If a clobber_abi is specified, add the necessary clobbers to the
310336
// operands list.
311-
if let Some((abi, abi_span)) = clobber_abi {
337+
let mut clobbered = FxHashSet::default();
338+
for (abi, (_, abi_span)) in clobber_abis {
312339
for &clobber in abi.clobbered_regs() {
340+
// Don't emit a clobber for a register already clobbered
341+
if clobbered.contains(&clobber) {
342+
continue;
343+
}
344+
313345
let mut output_used = false;
314346
clobber.overlapping_regs(|reg| {
315347
if used_output_regs.contains_key(&reg) {
@@ -326,6 +358,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
326358
},
327359
self.lower_span(abi_span),
328360
));
361+
clobbered.insert(clobber);
329362
}
330363
}
331364
}

compiler/rustc_ast_pretty/src/pprust/state.rs

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

22002200
let mut args = vec![AsmArg::Template(InlineAsmTemplatePiece::to_string(&asm.template))];
22012201
args.extend(asm.operands.iter().map(|(o, _)| AsmArg::Operand(o)));
2202-
if let Some((abi, _)) = asm.clobber_abi {
2203-
args.push(AsmArg::ClobberAbi(abi));
2202+
for (abi, _) in &asm.clobber_abis {
2203+
args.push(AsmArg::ClobberAbi(*abi));
22042204
}
22052205
if !asm.options.is_empty() {
22062206
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:
@@ -799,6 +799,8 @@ As stated in the previous section, passing an input value smaller than the regis
799799

800800
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.
801801

802+
`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.
803+
802804
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.
803805
The following ABIs can be used with `clobber_abi`:
804806

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ fn main() {
5151
asm!("{}", options(), clobber_abi("C"), const foo);
5252
//~^ ERROR clobber_abi is not allowed after options
5353
asm!("", clobber_abi("C"), clobber_abi("C"));
54-
//~^ ERROR clobber_abi specified multiple times
5554
asm!("{a}", a = const foo, a = const bar);
5655
//~^ ERROR duplicate argument named `a`
5756
//~^^ ERROR argument never used
@@ -121,7 +120,7 @@ global_asm!("", options(), clobber_abi("C"));
121120
global_asm!("{}", options(), clobber_abi("C"), const FOO);
122121
//~^ ERROR clobber_abi is not allowed after options
123122
global_asm!("", clobber_abi("C"), clobber_abi("C"));
124-
//~^ ERROR clobber_abi specified multiple times
123+
//~^ ERROR `clobber_abi` cannot be used with `global_asm!`
125124
global_asm!("{a}", a = const FOO, a = const BAR);
126125
//~^ ERROR duplicate argument named `a`
127126
//~^^ ERROR argument never used

0 commit comments

Comments
 (0)