Skip to content

Commit 3ee471c

Browse files
committed
auto merge of #19411 : lifthrasiir/rust/asm-clobbers-expanded, r=alexcrichton
I.e. we should not prematurely build operand constraints at the expansion time. Otherwise `--pretty expanded` diverges: ``` $ cat t.rs #![feature(asm)] pub fn main() { unsafe { asm!("" : : : "hello", "world") }; } $ rustc t.rs --pretty #![feature(asm)] pub fn main() { unsafe { asm!("" : : : "hello" , "world") }; } $ rustc t.rs --pretty expanded #![feature(asm)] #![feature(phase)] #![no_std] #![feature(globs)] #[phase(plugin, link)] extern crate "std" as std; #[prelude_import] use std::prelude::*; pub fn main() { unsafe { asm!("": : : "~{hello},~{world}") }; } ``` (The last code *does* compile, but won't do the expected thing.)
2 parents 52888a7 + 133266f commit 3ee471c

File tree

5 files changed

+39
-22
lines changed

5 files changed

+39
-22
lines changed

src/librustc_trans/trans/asm.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,22 @@ pub fn trans_inline_asm<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, ia: &ast::InlineAsm)
7777
// no failure occurred preparing operands, no need to cleanup
7878
fcx.pop_custom_cleanup_scope(temp_scope);
7979

80-
let mut constraints =
81-
String::from_str(constraints.iter()
82-
.map(|s| s.get().to_string())
83-
.chain(ext_constraints.into_iter())
84-
.collect::<Vec<String>>()
85-
.connect(",")
86-
.as_slice());
87-
88-
let mut clobbers = get_clobbers();
89-
if !ia.clobbers.get().is_empty() && !clobbers.is_empty() {
90-
clobbers = format!("{},{}", ia.clobbers.get(), clobbers);
91-
} else {
92-
clobbers.push_str(ia.clobbers.get());
80+
let mut constraints = constraints.iter()
81+
.map(|s| s.get().to_string())
82+
.chain(ext_constraints.into_iter())
83+
.collect::<Vec<String>>()
84+
.connect(",");
85+
86+
let mut clobbers = ia.clobbers.iter()
87+
.map(|s| format!("~{{{}}}", s.get()))
88+
.collect::<Vec<String>>()
89+
.connect(",");
90+
let more_clobbers = get_clobbers();
91+
if !more_clobbers.is_empty() {
92+
if !clobbers.is_empty() {
93+
clobbers.push(',');
94+
}
95+
clobbers.push_str(more_clobbers.as_slice());
9396
}
9497

9598
// Add the clobbers to our constraints list

src/libsyntax/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1177,7 +1177,7 @@ pub struct InlineAsm {
11771177
pub asm_str_style: StrStyle,
11781178
pub outputs: Vec<(InternedString, P<Expr>, bool)>,
11791179
pub inputs: Vec<(InternedString, P<Expr>)>,
1180-
pub clobbers: InternedString,
1180+
pub clobbers: Vec<InternedString>,
11811181
pub volatile: bool,
11821182
pub alignstack: bool,
11831183
pub dialect: AsmDialect,

src/libsyntax/ext/asm.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
5353
let mut asm_str_style = None;
5454
let mut outputs = Vec::new();
5555
let mut inputs = Vec::new();
56-
let mut cons = "".to_string();
56+
let mut clobs = Vec::new();
5757
let mut volatile = false;
5858
let mut alignstack = false;
5959
let mut dialect = ast::AsmAtt;
@@ -138,7 +138,6 @@ pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
138138
}
139139
}
140140
Clobbers => {
141-
let mut clobs = Vec::new();
142141
while p.token != token::Eof &&
143142
p.token != token::Colon &&
144143
p.token != token::ModSep {
@@ -148,15 +147,12 @@ pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
148147
}
149148

150149
let (s, _str_style) = p.parse_str();
151-
let clob = format!("~{{{}}}", s);
152-
clobs.push(clob);
153150

154151
if OPTIONS.iter().any(|opt| s.equiv(opt)) {
155152
cx.span_warn(p.last_span, "expected a clobber, found an option");
156153
}
154+
clobs.push(s);
157155
}
158-
159-
cons = clobs.connect(",");
160156
}
161157
Options => {
162158
let (option, _str_style) = p.parse_str();
@@ -216,7 +212,7 @@ pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
216212
asm_str_style: asm_str_style.unwrap(),
217213
outputs: outputs,
218214
inputs: inputs,
219-
clobbers: token::intern_and_get_ident(cons.as_slice()),
215+
clobbers: clobs,
220216
volatile: volatile,
221217
alignstack: alignstack,
222218
dialect: dialect,

src/libsyntax/print/pprust.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1839,7 +1839,11 @@ impl<'a> State<'a> {
18391839
try!(space(&mut self.s));
18401840
try!(self.word_space(":"));
18411841

1842-
try!(self.print_string(a.clobbers.get(), ast::CookedStr));
1842+
try!(self.commasep(Inconsistent, a.clobbers.as_slice(),
1843+
|s, co| {
1844+
try!(s.print_string(co.get(), ast::CookedStr));
1845+
Ok(())
1846+
}));
18431847
try!(self.pclose());
18441848
}
18451849
ast::ExprMac(ref m) => try!(self.print_mac(m)),

src/test/pretty/asm-clobbers.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(asm)]
12+
13+
pub fn main() { unsafe { asm!("" : : : "hello", "world") }; }
14+

0 commit comments

Comments
 (0)