diff --git a/src/librustc/middle/cfg/construct.rs b/src/librustc/middle/cfg/construct.rs index 3b436907ac50a..8baae99c2f24b 100644 --- a/src/librustc/middle/cfg/construct.rs +++ b/src/librustc/middle/cfg/construct.rs @@ -368,8 +368,7 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> { }), pred); let post_outputs = self.exprs(outputs.map(|a| { debug!("cfg::construct InlineAsm id:{} output:{:?}", expr.id, a); - let &(_, ref expr, _) = a; - &**expr + &*a.expr }), post_inputs); self.add_ast_node(expr.id, &[post_outputs]) } diff --git a/src/librustc/middle/expr_use_visitor.rs b/src/librustc/middle/expr_use_visitor.rs index 899bad982eba5..4861f0a6b6436 100644 --- a/src/librustc/middle/expr_use_visitor.rs +++ b/src/librustc/middle/expr_use_visitor.rs @@ -458,9 +458,13 @@ impl<'d,'t,'a,'tcx> ExprUseVisitor<'d,'t,'a,'tcx> { self.consume_expr(&**input); } - for &(_, ref output, is_rw) in &ia.outputs { - self.mutate_expr(expr, &**output, - if is_rw { WriteAndRead } else { JustWrite }); + for output in &ia.outputs { + if output.is_indirect { + self.consume_expr(&*output.expr); + } else { + self.mutate_expr(expr, &*output.expr, + if output.is_rw { WriteAndRead } else { JustWrite }); + } } } diff --git a/src/librustc/middle/liveness.rs b/src/librustc/middle/liveness.rs index d983f26daf7ae..627df102b937a 100644 --- a/src/librustc/middle/liveness.rs +++ b/src/librustc/middle/liveness.rs @@ -1166,12 +1166,19 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> { hir::ExprInlineAsm(ref ia) => { - let succ = ia.outputs.iter().rev().fold(succ, |succ, &(_, ref expr, _)| { - // see comment on lvalues - // in propagate_through_lvalue_components() - let succ = self.write_lvalue(&**expr, succ, ACC_WRITE); - self.propagate_through_lvalue_components(&**expr, succ) - }); + let succ = ia.outputs.iter().rev().fold(succ, + |succ, out| { + // see comment on lvalues + // in propagate_through_lvalue_components() + if out.is_indirect { + self.propagate_through_expr(&*out.expr, succ) + } else { + let acc = if out.is_rw { ACC_WRITE|ACC_READ } else { ACC_WRITE }; + let succ = self.write_lvalue(&*out.expr, succ, acc); + self.propagate_through_lvalue_components(&*out.expr, succ) + } + } + ); // Inputs are executed first. Propagate last because of rev order ia.inputs.iter().rev().fold(succ, |succ, &(_, ref expr)| { self.propagate_through_expr(&**expr, succ) @@ -1416,9 +1423,11 @@ fn check_expr(this: &mut Liveness, expr: &Expr) { } // Output operands must be lvalues - for &(_, ref out, _) in &ia.outputs { - this.check_lvalue(&**out); - this.visit_expr(&**out); + for out in &ia.outputs { + if !out.is_indirect { + this.check_lvalue(&*out.expr); + } + this.visit_expr(&*out.expr); } intravisit::walk_expr(this, expr); diff --git a/src/librustc_front/fold.rs b/src/librustc_front/fold.rs index 08280e98ae4c7..88a34b27c3189 100644 --- a/src/librustc_front/fold.rs +++ b/src/librustc_front/fold.rs @@ -1117,7 +1117,14 @@ pub fn noop_fold_expr(Expr { id, node, span, attrs }: Expr, folder: & expn_id, }) => ExprInlineAsm(InlineAsm { inputs: inputs.move_map(|(c, input)| (c, folder.fold_expr(input))), - outputs: outputs.move_map(|(c, out, is_rw)| (c, folder.fold_expr(out), is_rw)), + outputs: outputs.move_map(|out| { + InlineAsmOutput { + constraint: out.constraint, + expr: folder.fold_expr(out.expr), + is_rw: out.is_rw, + is_indirect: out.is_indirect, + } + }), asm: asm, asm_str_style: asm_str_style, clobbers: clobbers, diff --git a/src/librustc_front/hir.rs b/src/librustc_front/hir.rs index 10e5a091c0d2a..d1cb82dbeccbc 100644 --- a/src/librustc_front/hir.rs +++ b/src/librustc_front/hir.rs @@ -953,11 +953,19 @@ pub enum Ty_ { TyInfer, } +#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] +pub struct InlineAsmOutput { + pub constraint: InternedString, + pub expr: P, + pub is_rw: bool, + pub is_indirect: bool, +} + #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] pub struct InlineAsm { pub asm: InternedString, pub asm_str_style: StrStyle, - pub outputs: Vec<(InternedString, P, bool)>, + pub outputs: Vec, pub inputs: Vec<(InternedString, P)>, pub clobbers: Vec, pub volatile: bool, diff --git a/src/librustc_front/intravisit.rs b/src/librustc_front/intravisit.rs index cb7c5be6529e8..13d7e81cc6e01 100644 --- a/src/librustc_front/intravisit.rs +++ b/src/librustc_front/intravisit.rs @@ -803,8 +803,8 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) { for &(_, ref input) in &ia.inputs { visitor.visit_expr(&input) } - for &(_, ref output, _) in &ia.outputs { - visitor.visit_expr(&output) + for output in &ia.outputs { + visitor.visit_expr(&output.expr) } } } diff --git a/src/librustc_front/lowering.rs b/src/librustc_front/lowering.rs index 7dbd6dbf0b8de..c0b10fb89124a 100644 --- a/src/librustc_front/lowering.rs +++ b/src/librustc_front/lowering.rs @@ -1228,8 +1228,13 @@ pub fn lower_expr(lctx: &LoweringContext, e: &Expr) -> P { .map(|&(ref c, ref input)| (c.clone(), lower_expr(lctx, input))) .collect(), outputs: outputs.iter() - .map(|&(ref c, ref out, ref is_rw)| { - (c.clone(), lower_expr(lctx, out), *is_rw) + .map(|out| { + hir::InlineAsmOutput { + constraint: out.constraint.clone(), + expr: lower_expr(lctx, &out.expr), + is_rw: out.is_rw, + is_indirect: out.is_indirect, + } }) .collect(), asm: asm.clone(), diff --git a/src/librustc_front/print/pprust.rs b/src/librustc_front/print/pprust.rs index 721d60999e12d..81076b6e40276 100644 --- a/src/librustc_front/print/pprust.rs +++ b/src/librustc_front/print/pprust.rs @@ -1502,15 +1502,15 @@ impl<'a> State<'a> { try!(self.print_string(&a.asm, a.asm_str_style)); try!(self.word_space(":")); - try!(self.commasep(Inconsistent, &a.outputs, |s, &(ref co, ref o, is_rw)| { - match co.slice_shift_char() { - Some(('=', operand)) if is_rw => { + try!(self.commasep(Inconsistent, &a.outputs, |s, out| { + match out.constraint.slice_shift_char() { + Some(('=', operand)) if out.is_rw => { try!(s.print_string(&format!("+{}", operand), ast::CookedStr)) } - _ => try!(s.print_string(&co, ast::CookedStr)), + _ => try!(s.print_string(&out.constraint, ast::CookedStr)), } try!(s.popen()); - try!(s.print_expr(&**o)); + try!(s.print_expr(&*out.expr)); try!(s.pclose()); Ok(()) })); diff --git a/src/librustc_trans/trans/asm.rs b/src/librustc_trans/trans/asm.rs index 1de94bed3ddf7..69a8a84229d47 100644 --- a/src/librustc_trans/trans/asm.rs +++ b/src/librustc_trans/trans/asm.rs @@ -39,27 +39,39 @@ pub fn trans_inline_asm<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, ia: &ast::InlineAsm) let mut ext_constraints = Vec::new(); // Prepare the output operands - let outputs = ia.outputs.iter().enumerate().map(|(i, &(ref c, ref out, is_rw))| { - constraints.push((*c).clone()); + let mut outputs = Vec::new(); + let mut inputs = Vec::new(); + for (i, out) in ia.outputs.iter().enumerate() { + constraints.push(out.constraint.clone()); - let out_datum = unpack_datum!(bcx, expr::trans(bcx, &**out)); - output_types.push(type_of::type_of(bcx.ccx(), out_datum.ty)); - let val = out_datum.val; - if is_rw { + let out_datum = unpack_datum!(bcx, expr::trans(bcx, &*out.expr)); + if out.is_indirect { bcx = callee::trans_arg_datum(bcx, - expr_ty(bcx, &**out), + expr_ty(bcx, &*out.expr), out_datum, cleanup::CustomScope(temp_scope), callee::DontAutorefArg, - &mut ext_inputs); - ext_constraints.push(i.to_string()); + &mut inputs); + if out.is_rw { + ext_inputs.push(*inputs.last().unwrap()); + ext_constraints.push(i.to_string()); + } + } else { + output_types.push(type_of::type_of(bcx.ccx(), out_datum.ty)); + outputs.push(out_datum.val); + if out.is_rw { + bcx = callee::trans_arg_datum(bcx, + expr_ty(bcx, &*out.expr), + out_datum, + cleanup::CustomScope(temp_scope), + callee::DontAutorefArg, + &mut ext_inputs); + ext_constraints.push(i.to_string()); + } } - val - - }).collect::>(); + } // Now the input operands - let mut inputs = Vec::new(); for &(ref c, ref input) in &ia.inputs { constraints.push((*c).clone()); diff --git a/src/librustc_trans/trans/debuginfo/create_scope_map.rs b/src/librustc_trans/trans/debuginfo/create_scope_map.rs index 7d88e579e5a58..231dec276d99c 100644 --- a/src/librustc_trans/trans/debuginfo/create_scope_map.rs +++ b/src/librustc_trans/trans/debuginfo/create_scope_map.rs @@ -480,8 +480,8 @@ fn walk_expr(cx: &CrateContext, walk_expr(cx, &**exp, scope_stack, scope_map); } - for &(_, ref exp, _) in outputs { - walk_expr(cx, &**exp, scope_stack, scope_map); + for out in outputs { + walk_expr(cx, &*out.expr, scope_stack, scope_map); } } } diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index a985e3552816f..f348d5411c347 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -3394,8 +3394,8 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>, for &(_, ref input) in &ia.inputs { check_expr(fcx, &**input); } - for &(_, ref out, _) in &ia.outputs { - check_expr(fcx, &**out); + for out in &ia.outputs { + check_expr(fcx, &*out.expr); } fcx.write_nil(id); } diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index f11291fc0f7e7..46110a3bee9b1 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -1458,11 +1458,19 @@ pub enum AsmDialect { Intel, } +#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] +pub struct InlineAsmOutput { + pub constraint: InternedString, + pub expr: P, + pub is_rw: bool, + pub is_indirect: bool, +} + #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] pub struct InlineAsm { pub asm: InternedString, pub asm_str_style: StrStyle, - pub outputs: Vec<(InternedString, P, bool)>, + pub outputs: Vec, pub inputs: Vec<(InternedString, P)>, pub clobbers: Vec, pub volatile: bool, diff --git a/src/libsyntax/ext/asm.rs b/src/libsyntax/ext/asm.rs index d968858f634eb..b4f29f837263f 100644 --- a/src/libsyntax/ext/asm.rs +++ b/src/libsyntax/ext/asm.rs @@ -125,7 +125,13 @@ pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) }; let is_rw = output.is_some(); - outputs.push((output.unwrap_or(constraint), out, is_rw)); + let is_indirect = constraint.contains("*"); + outputs.push(ast::InlineAsmOutput { + constraint: output.unwrap_or(constraint), + expr: out, + is_rw: is_rw, + is_indirect: is_indirect, + }); } } Inputs => { @@ -139,9 +145,9 @@ pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) let (constraint, _str_style) = panictry!(p.parse_str()); - if constraint.starts_with("=") && !constraint.contains("*") { + if constraint.starts_with("=") { cx.span_err(p.last_span, "input operand constraint contains '='"); - } else if constraint.starts_with("+") && !constraint.contains("*") { + } else if constraint.starts_with("+") { cx.span_err(p.last_span, "input operand constraint contains '+'"); } diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs index 73f2c51b24622..c637813f07e5b 100644 --- a/src/libsyntax/fold.rs +++ b/src/libsyntax/fold.rs @@ -1303,8 +1303,13 @@ pub fn noop_fold_expr(Expr {id, node, span, attrs}: Expr, folder: &mu inputs: inputs.move_map(|(c, input)| { (c, folder.fold_expr(input)) }), - outputs: outputs.move_map(|(c, out, is_rw)| { - (c, folder.fold_expr(out), is_rw) + outputs: outputs.move_map(|out| { + InlineAsmOutput { + constraint: out.constraint, + expr: folder.fold_expr(out.expr), + is_rw: out.is_rw, + is_indirect: out.is_indirect, + } }), asm: asm, asm_str_style: asm_str_style, diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index f710595129672..475bacbc88642 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -2219,16 +2219,16 @@ impl<'a> State<'a> { try!(self.word_space(":")); try!(self.commasep(Inconsistent, &a.outputs, - |s, &(ref co, ref o, is_rw)| { - match co.slice_shift_char() { - Some(('=', operand)) if is_rw => { + |s, out| { + match out.constraint.slice_shift_char() { + Some(('=', operand)) if out.is_rw => { try!(s.print_string(&format!("+{}", operand), ast::CookedStr)) } - _ => try!(s.print_string(&co, ast::CookedStr)) + _ => try!(s.print_string(&out.constraint, ast::CookedStr)) } try!(s.popen()); - try!(s.print_expr(&**o)); + try!(s.print_expr(&*out.expr)); try!(s.pclose()); Ok(()) })); diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index cdc11fb2c1cb0..22bf135f4f977 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -786,8 +786,8 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) { for &(_, ref input) in &ia.inputs { visitor.visit_expr(&input) } - for &(_, ref output, _) in &ia.outputs { - visitor.visit_expr(&output) + for output in &ia.outputs { + visitor.visit_expr(&output.expr) } } } diff --git a/src/test/run-pass/asm-indirect-memory.rs b/src/test/run-pass/asm-indirect-memory.rs index 80fd548dfe354..d1873afb3a9b7 100644 --- a/src/test/run-pass/asm-indirect-memory.rs +++ b/src/test/run-pass/asm-indirect-memory.rs @@ -22,17 +22,29 @@ fn read(ptr: &u32) -> u32 { #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] fn write(ptr: &mut u32, val: u32) { unsafe { - asm!("mov $1, $0" :: "=*m" (ptr), "r" (val)); + asm!("mov $1, $0" : "=*m" (ptr) : "r" (val)); } } +#[cfg(any(target_arch = "x86", target_arch = "x86_64"))] +fn replace(ptr: &mut u32, val: u32) -> u32 { + let out: u32; + unsafe { + asm!("mov $0, $1; mov $2, $0" : "+*m" (ptr), "=&r" (out) : "r" (val)); + } + out +} + #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] pub fn main() { let a = 1; - let mut b = 2; assert_eq!(read(&a), 1); + let mut b = 2; write(&mut b, 3); assert_eq!(b, 3); + let mut c = 4; + assert_eq!(replace(&mut c, 5), 4); + assert_eq!(c, 5); } #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]