Skip to content

Commit 50a02b4

Browse files
committed
Auto merge of #29735 - Amanieu:asm_indirect_constraint, r=pnkfelix
This PR reverts #29543 and instead implements proper support for "=*m" and "+*m" indirect output operands. This provides a framework on top of which support for plain memory operands ("m", "=m" and "+m") can be implemented. This also fixes the liveness analysis pass not handling read/write operands correctly.
2 parents 6b3a3f2 + 65707df commit 50a02b4

File tree

17 files changed

+132
-57
lines changed

17 files changed

+132
-57
lines changed

src/librustc/middle/cfg/construct.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,7 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
368368
}), pred);
369369
let post_outputs = self.exprs(outputs.map(|a| {
370370
debug!("cfg::construct InlineAsm id:{} output:{:?}", expr.id, a);
371-
let &(_, ref expr, _) = a;
372-
&**expr
371+
&*a.expr
373372
}), post_inputs);
374373
self.add_ast_node(expr.id, &[post_outputs])
375374
}

src/librustc/middle/expr_use_visitor.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -458,9 +458,13 @@ impl<'d,'t,'a,'tcx> ExprUseVisitor<'d,'t,'a,'tcx> {
458458
self.consume_expr(&**input);
459459
}
460460

461-
for &(_, ref output, is_rw) in &ia.outputs {
462-
self.mutate_expr(expr, &**output,
463-
if is_rw { WriteAndRead } else { JustWrite });
461+
for output in &ia.outputs {
462+
if output.is_indirect {
463+
self.consume_expr(&*output.expr);
464+
} else {
465+
self.mutate_expr(expr, &*output.expr,
466+
if output.is_rw { WriteAndRead } else { JustWrite });
467+
}
464468
}
465469
}
466470

src/librustc/middle/liveness.rs

+18-9
Original file line numberDiff line numberDiff line change
@@ -1166,12 +1166,19 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
11661166

11671167
hir::ExprInlineAsm(ref ia) => {
11681168

1169-
let succ = ia.outputs.iter().rev().fold(succ, |succ, &(_, ref expr, _)| {
1170-
// see comment on lvalues
1171-
// in propagate_through_lvalue_components()
1172-
let succ = self.write_lvalue(&**expr, succ, ACC_WRITE);
1173-
self.propagate_through_lvalue_components(&**expr, succ)
1174-
});
1169+
let succ = ia.outputs.iter().rev().fold(succ,
1170+
|succ, out| {
1171+
// see comment on lvalues
1172+
// in propagate_through_lvalue_components()
1173+
if out.is_indirect {
1174+
self.propagate_through_expr(&*out.expr, succ)
1175+
} else {
1176+
let acc = if out.is_rw { ACC_WRITE|ACC_READ } else { ACC_WRITE };
1177+
let succ = self.write_lvalue(&*out.expr, succ, acc);
1178+
self.propagate_through_lvalue_components(&*out.expr, succ)
1179+
}
1180+
}
1181+
);
11751182
// Inputs are executed first. Propagate last because of rev order
11761183
ia.inputs.iter().rev().fold(succ, |succ, &(_, ref expr)| {
11771184
self.propagate_through_expr(&**expr, succ)
@@ -1416,9 +1423,11 @@ fn check_expr(this: &mut Liveness, expr: &Expr) {
14161423
}
14171424

14181425
// Output operands must be lvalues
1419-
for &(_, ref out, _) in &ia.outputs {
1420-
this.check_lvalue(&**out);
1421-
this.visit_expr(&**out);
1426+
for out in &ia.outputs {
1427+
if !out.is_indirect {
1428+
this.check_lvalue(&*out.expr);
1429+
}
1430+
this.visit_expr(&*out.expr);
14221431
}
14231432

14241433
intravisit::walk_expr(this, expr);

src/librustc_front/fold.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1117,7 +1117,14 @@ pub fn noop_fold_expr<T: Folder>(Expr { id, node, span, attrs }: Expr, folder: &
11171117
expn_id,
11181118
}) => ExprInlineAsm(InlineAsm {
11191119
inputs: inputs.move_map(|(c, input)| (c, folder.fold_expr(input))),
1120-
outputs: outputs.move_map(|(c, out, is_rw)| (c, folder.fold_expr(out), is_rw)),
1120+
outputs: outputs.move_map(|out| {
1121+
InlineAsmOutput {
1122+
constraint: out.constraint,
1123+
expr: folder.fold_expr(out.expr),
1124+
is_rw: out.is_rw,
1125+
is_indirect: out.is_indirect,
1126+
}
1127+
}),
11211128
asm: asm,
11221129
asm_str_style: asm_str_style,
11231130
clobbers: clobbers,

src/librustc_front/hir.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -953,11 +953,19 @@ pub enum Ty_ {
953953
TyInfer,
954954
}
955955

956+
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
957+
pub struct InlineAsmOutput {
958+
pub constraint: InternedString,
959+
pub expr: P<Expr>,
960+
pub is_rw: bool,
961+
pub is_indirect: bool,
962+
}
963+
956964
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
957965
pub struct InlineAsm {
958966
pub asm: InternedString,
959967
pub asm_str_style: StrStyle,
960-
pub outputs: Vec<(InternedString, P<Expr>, bool)>,
968+
pub outputs: Vec<InlineAsmOutput>,
961969
pub inputs: Vec<(InternedString, P<Expr>)>,
962970
pub clobbers: Vec<InternedString>,
963971
pub volatile: bool,

src/librustc_front/intravisit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -803,8 +803,8 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
803803
for &(_, ref input) in &ia.inputs {
804804
visitor.visit_expr(&input)
805805
}
806-
for &(_, ref output, _) in &ia.outputs {
807-
visitor.visit_expr(&output)
806+
for output in &ia.outputs {
807+
visitor.visit_expr(&output.expr)
808808
}
809809
}
810810
}

src/librustc_front/lowering.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1228,8 +1228,13 @@ pub fn lower_expr(lctx: &LoweringContext, e: &Expr) -> P<hir::Expr> {
12281228
.map(|&(ref c, ref input)| (c.clone(), lower_expr(lctx, input)))
12291229
.collect(),
12301230
outputs: outputs.iter()
1231-
.map(|&(ref c, ref out, ref is_rw)| {
1232-
(c.clone(), lower_expr(lctx, out), *is_rw)
1231+
.map(|out| {
1232+
hir::InlineAsmOutput {
1233+
constraint: out.constraint.clone(),
1234+
expr: lower_expr(lctx, &out.expr),
1235+
is_rw: out.is_rw,
1236+
is_indirect: out.is_indirect,
1237+
}
12331238
})
12341239
.collect(),
12351240
asm: asm.clone(),

src/librustc_front/print/pprust.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1502,15 +1502,15 @@ impl<'a> State<'a> {
15021502
try!(self.print_string(&a.asm, a.asm_str_style));
15031503
try!(self.word_space(":"));
15041504

1505-
try!(self.commasep(Inconsistent, &a.outputs, |s, &(ref co, ref o, is_rw)| {
1506-
match co.slice_shift_char() {
1507-
Some(('=', operand)) if is_rw => {
1505+
try!(self.commasep(Inconsistent, &a.outputs, |s, out| {
1506+
match out.constraint.slice_shift_char() {
1507+
Some(('=', operand)) if out.is_rw => {
15081508
try!(s.print_string(&format!("+{}", operand), ast::CookedStr))
15091509
}
1510-
_ => try!(s.print_string(&co, ast::CookedStr)),
1510+
_ => try!(s.print_string(&out.constraint, ast::CookedStr)),
15111511
}
15121512
try!(s.popen());
1513-
try!(s.print_expr(&**o));
1513+
try!(s.print_expr(&*out.expr));
15141514
try!(s.pclose());
15151515
Ok(())
15161516
}));

src/librustc_trans/trans/asm.rs

+25-13
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,39 @@ pub fn trans_inline_asm<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, ia: &ast::InlineAsm)
3939
let mut ext_constraints = Vec::new();
4040

4141
// Prepare the output operands
42-
let outputs = ia.outputs.iter().enumerate().map(|(i, &(ref c, ref out, is_rw))| {
43-
constraints.push((*c).clone());
42+
let mut outputs = Vec::new();
43+
let mut inputs = Vec::new();
44+
for (i, out) in ia.outputs.iter().enumerate() {
45+
constraints.push(out.constraint.clone());
4446

45-
let out_datum = unpack_datum!(bcx, expr::trans(bcx, &**out));
46-
output_types.push(type_of::type_of(bcx.ccx(), out_datum.ty));
47-
let val = out_datum.val;
48-
if is_rw {
47+
let out_datum = unpack_datum!(bcx, expr::trans(bcx, &*out.expr));
48+
if out.is_indirect {
4949
bcx = callee::trans_arg_datum(bcx,
50-
expr_ty(bcx, &**out),
50+
expr_ty(bcx, &*out.expr),
5151
out_datum,
5252
cleanup::CustomScope(temp_scope),
5353
callee::DontAutorefArg,
54-
&mut ext_inputs);
55-
ext_constraints.push(i.to_string());
54+
&mut inputs);
55+
if out.is_rw {
56+
ext_inputs.push(*inputs.last().unwrap());
57+
ext_constraints.push(i.to_string());
58+
}
59+
} else {
60+
output_types.push(type_of::type_of(bcx.ccx(), out_datum.ty));
61+
outputs.push(out_datum.val);
62+
if out.is_rw {
63+
bcx = callee::trans_arg_datum(bcx,
64+
expr_ty(bcx, &*out.expr),
65+
out_datum,
66+
cleanup::CustomScope(temp_scope),
67+
callee::DontAutorefArg,
68+
&mut ext_inputs);
69+
ext_constraints.push(i.to_string());
70+
}
5671
}
57-
val
58-
59-
}).collect::<Vec<_>>();
72+
}
6073

6174
// Now the input operands
62-
let mut inputs = Vec::new();
6375
for &(ref c, ref input) in &ia.inputs {
6476
constraints.push((*c).clone());
6577

src/librustc_trans/trans/debuginfo/create_scope_map.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -480,8 +480,8 @@ fn walk_expr(cx: &CrateContext,
480480
walk_expr(cx, &**exp, scope_stack, scope_map);
481481
}
482482

483-
for &(_, ref exp, _) in outputs {
484-
walk_expr(cx, &**exp, scope_stack, scope_map);
483+
for out in outputs {
484+
walk_expr(cx, &*out.expr, scope_stack, scope_map);
485485
}
486486
}
487487
}

src/librustc_typeck/check/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3394,8 +3394,8 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
33943394
for &(_, ref input) in &ia.inputs {
33953395
check_expr(fcx, &**input);
33963396
}
3397-
for &(_, ref out, _) in &ia.outputs {
3398-
check_expr(fcx, &**out);
3397+
for out in &ia.outputs {
3398+
check_expr(fcx, &*out.expr);
33993399
}
34003400
fcx.write_nil(id);
34013401
}

src/libsyntax/ast.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1458,11 +1458,19 @@ pub enum AsmDialect {
14581458
Intel,
14591459
}
14601460

1461+
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1462+
pub struct InlineAsmOutput {
1463+
pub constraint: InternedString,
1464+
pub expr: P<Expr>,
1465+
pub is_rw: bool,
1466+
pub is_indirect: bool,
1467+
}
1468+
14611469
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
14621470
pub struct InlineAsm {
14631471
pub asm: InternedString,
14641472
pub asm_str_style: StrStyle,
1465-
pub outputs: Vec<(InternedString, P<Expr>, bool)>,
1473+
pub outputs: Vec<InlineAsmOutput>,
14661474
pub inputs: Vec<(InternedString, P<Expr>)>,
14671475
pub clobbers: Vec<InternedString>,
14681476
pub volatile: bool,

src/libsyntax/ext/asm.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,13 @@ pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
125125
};
126126

127127
let is_rw = output.is_some();
128-
outputs.push((output.unwrap_or(constraint), out, is_rw));
128+
let is_indirect = constraint.contains("*");
129+
outputs.push(ast::InlineAsmOutput {
130+
constraint: output.unwrap_or(constraint),
131+
expr: out,
132+
is_rw: is_rw,
133+
is_indirect: is_indirect,
134+
});
129135
}
130136
}
131137
Inputs => {
@@ -139,9 +145,9 @@ pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
139145

140146
let (constraint, _str_style) = panictry!(p.parse_str());
141147

142-
if constraint.starts_with("=") && !constraint.contains("*") {
148+
if constraint.starts_with("=") {
143149
cx.span_err(p.last_span, "input operand constraint contains '='");
144-
} else if constraint.starts_with("+") && !constraint.contains("*") {
150+
} else if constraint.starts_with("+") {
145151
cx.span_err(p.last_span, "input operand constraint contains '+'");
146152
}
147153

src/libsyntax/fold.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1303,8 +1303,13 @@ pub fn noop_fold_expr<T: Folder>(Expr {id, node, span, attrs}: Expr, folder: &mu
13031303
inputs: inputs.move_map(|(c, input)| {
13041304
(c, folder.fold_expr(input))
13051305
}),
1306-
outputs: outputs.move_map(|(c, out, is_rw)| {
1307-
(c, folder.fold_expr(out), is_rw)
1306+
outputs: outputs.move_map(|out| {
1307+
InlineAsmOutput {
1308+
constraint: out.constraint,
1309+
expr: folder.fold_expr(out.expr),
1310+
is_rw: out.is_rw,
1311+
is_indirect: out.is_indirect,
1312+
}
13081313
}),
13091314
asm: asm,
13101315
asm_str_style: asm_str_style,

src/libsyntax/print/pprust.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -2219,16 +2219,16 @@ impl<'a> State<'a> {
22192219
try!(self.word_space(":"));
22202220

22212221
try!(self.commasep(Inconsistent, &a.outputs,
2222-
|s, &(ref co, ref o, is_rw)| {
2223-
match co.slice_shift_char() {
2224-
Some(('=', operand)) if is_rw => {
2222+
|s, out| {
2223+
match out.constraint.slice_shift_char() {
2224+
Some(('=', operand)) if out.is_rw => {
22252225
try!(s.print_string(&format!("+{}", operand),
22262226
ast::CookedStr))
22272227
}
2228-
_ => try!(s.print_string(&co, ast::CookedStr))
2228+
_ => try!(s.print_string(&out.constraint, ast::CookedStr))
22292229
}
22302230
try!(s.popen());
2231-
try!(s.print_expr(&**o));
2231+
try!(s.print_expr(&*out.expr));
22322232
try!(s.pclose());
22332233
Ok(())
22342234
}));

src/libsyntax/visit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -786,8 +786,8 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
786786
for &(_, ref input) in &ia.inputs {
787787
visitor.visit_expr(&input)
788788
}
789-
for &(_, ref output, _) in &ia.outputs {
790-
visitor.visit_expr(&output)
789+
for output in &ia.outputs {
790+
visitor.visit_expr(&output.expr)
791791
}
792792
}
793793
}

src/test/run-pass/asm-indirect-memory.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,29 @@ fn read(ptr: &u32) -> u32 {
2222
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
2323
fn write(ptr: &mut u32, val: u32) {
2424
unsafe {
25-
asm!("mov $1, $0" :: "=*m" (ptr), "r" (val));
25+
asm!("mov $1, $0" : "=*m" (ptr) : "r" (val));
2626
}
2727
}
2828

29+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
30+
fn replace(ptr: &mut u32, val: u32) -> u32 {
31+
let out: u32;
32+
unsafe {
33+
asm!("mov $0, $1; mov $2, $0" : "+*m" (ptr), "=&r" (out) : "r" (val));
34+
}
35+
out
36+
}
37+
2938
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
3039
pub fn main() {
3140
let a = 1;
32-
let mut b = 2;
3341
assert_eq!(read(&a), 1);
42+
let mut b = 2;
3443
write(&mut b, 3);
3544
assert_eq!(b, 3);
45+
let mut c = 4;
46+
assert_eq!(replace(&mut c, 5), 4);
47+
assert_eq!(c, 5);
3648
}
3749

3850
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]

0 commit comments

Comments
 (0)