]> git.lizzy.rs Git - rust.git/commitdiff
Fix double evaluation of read+write operands
authorPiotr Czarnecki <pioczarn@gmail.com>
Tue, 19 Aug 2014 19:39:26 +0000 (20:39 +0100)
committerPiotr Czarnecki <pioczarn@gmail.com>
Tue, 19 Aug 2014 19:39:26 +0000 (20:39 +0100)
Stop read+write expressions from expanding into two occurences
in the AST. Add a bool to indicate whether an operand in output
position if read+write or not.

Fixes #14936

12 files changed:
src/librustc/middle/cfg/construct.rs
src/librustc/middle/expr_use_visitor.rs
src/librustc/middle/liveness.rs
src/librustc/middle/trans/asm.rs
src/librustc/middle/trans/debuginfo.rs
src/librustc/middle/typeck/check/mod.rs
src/libsyntax/ast.rs
src/libsyntax/ext/asm.rs
src/libsyntax/fold.rs
src/libsyntax/print/pprust.rs
src/libsyntax/visit.rs
src/test/run-pass/issue-14936.rs [new file with mode: 0644]

index dba873a72b6b8bfe3fb317577703bc0f3d1db280..fd3074e130e70fd66483d2187ba665ed157f8c2c 100644 (file)
@@ -473,14 +473,15 @@ fn expr(&mut self, expr: Gc<ast::Expr>, pred: CFGIndex) -> CFGIndex {
             ast::ExprInlineAsm(ref inline_asm) => {
                 let inputs = inline_asm.inputs.iter();
                 let outputs = inline_asm.outputs.iter();
-                fn extract_expr<A>(&(_, expr): &(A, Gc<ast::Expr>)) -> Gc<ast::Expr> { expr }
                 let post_inputs = self.exprs(inputs.map(|a| {
                     debug!("cfg::construct InlineAsm id:{} input:{:?}", expr.id, a);
-                    extract_expr(a)
+                    let &(_, expr) = a;
+                    expr
                 }), pred);
                 let post_outputs = self.exprs(outputs.map(|a| {
                     debug!("cfg::construct InlineAsm id:{} output:{:?}", expr.id, a);
-                    extract_expr(a)
+                    let &(_, expr, _) = a;
+                    expr
                 }), post_inputs);
                 self.add_node(expr.id, [post_outputs])
             }
index 6caf54790d1b4d9f2f63c8641e1ee59fa4e2056e..aefd57ab085ad8e981033592864551cd2bacb9e9 100644 (file)
@@ -380,8 +380,9 @@ pub fn walk_expr(&mut self, expr: &ast::Expr) {
                     self.consume_expr(&**input);
                 }
 
-                for &(_, ref output) in ia.outputs.iter() {
-                    self.mutate_expr(expr, &**output, JustWrite);
+                for &(_, ref output, is_rw) in ia.outputs.iter() {
+                    self.mutate_expr(expr, &**output,
+                                           if is_rw { WriteAndRead } else { JustWrite });
                 }
             }
 
index baac68904d553308cc69f260806d4b1232018f8a..2062fa97777d5f58b66cb212d58933b1f3563fdb 100644 (file)
@@ -1192,9 +1192,10 @@ fn propagate_through_expr(&mut self, expr: &Expr, succ: LiveNode)
           }
 
           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 = 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)
             });
@@ -1437,7 +1438,7 @@ fn check_expr(this: &mut Liveness, expr: &Expr) {
         }
 
         // Output operands must be lvalues
-        for &(_, ref out) in ia.outputs.iter() {
+        for &(_, ref out, _) in ia.outputs.iter() {
           this.check_lvalue(&**out);
           this.visit_expr(&**out, ());
         }
index 9760ef07a40e463aa4bb7b5d1aabee8a92a4c1ff..be5f586bb97fe478a666eff1cff63701da5ef262 100644 (file)
@@ -36,13 +36,27 @@ pub fn trans_inline_asm<'a>(bcx: &'a Block<'a>, ia: &ast::InlineAsm)
 
     let temp_scope = fcx.push_custom_cleanup_scope();
 
+    let mut ext_inputs = Vec::new();
+    let mut ext_constraints = Vec::new();
+
     // Prepare the output operands
-    let outputs = ia.outputs.iter().map(|&(ref c, ref out)| {
+    let outputs = ia.outputs.iter().enumerate().map(|(i, &(ref c, ref out, is_rw))| {
         constraints.push((*c).clone());
 
         let out_datum = unpack_datum!(bcx, expr::trans(bcx, &**out));
         output_types.push(type_of::type_of(bcx.ccx(), out_datum.ty));
-        out_datum.val
+        let val = out_datum.val;
+        if is_rw {
+            ext_inputs.push(unpack_result!(bcx, {
+                callee::trans_arg_datum(bcx,
+                                       expr_ty(bcx, &**out),
+                                       out_datum,
+                                       cleanup::CustomScope(temp_scope),
+                                       callee::DontAutorefArg)
+            }));
+            ext_constraints.push(i.to_string());
+        }
+        val
 
     }).collect::<Vec<_>>();
 
@@ -58,7 +72,7 @@ pub fn trans_inline_asm<'a>(bcx: &'a Block<'a>, ia: &ast::InlineAsm)
                                    cleanup::CustomScope(temp_scope),
                                    callee::DontAutorefArg)
         })
-    }).collect::<Vec<_>>();
+    }).collect::<Vec<_>>().append(ext_inputs.as_slice());
 
     // no failure occurred preparing operands, no need to cleanup
     fcx.pop_custom_cleanup_scope(temp_scope);
@@ -66,6 +80,7 @@ pub fn trans_inline_asm<'a>(bcx: &'a Block<'a>, ia: &ast::InlineAsm)
     let mut constraints =
         String::from_str(constraints.iter()
                                     .map(|s| s.get().to_string())
+                                    .chain(ext_constraints.move_iter())
                                     .collect::<Vec<String>>()
                                     .connect(",")
                                     .as_slice());
index 28319a377fa6c3b53eea8a2b3b78402101140b48..88bdb00a7c983888c6d3659ae2f79cfab94cb108 100644 (file)
@@ -3729,7 +3729,7 @@ fn walk_expr(cx: &CrateContext,
                     walk_expr(cx, &**exp, scope_stack, scope_map);
                 }
 
-                for &(_, ref exp) in outputs.iter() {
+                for &(_, ref exp, _) in outputs.iter() {
                     walk_expr(cx, &**exp, scope_stack, scope_map);
                 }
             }
index 7896316a4722947d35aae2f68426740ad8d1fb4a..1cec680ff2cb853adaffb97cac7fa4b94720001f 100644 (file)
@@ -3332,7 +3332,7 @@ fn check_fn_for_vec_elements_expected(fcx: &FnCtxt,
           for &(_, ref input) in ia.inputs.iter() {
               check_expr(fcx, &**input);
           }
-          for &(_, ref out) in ia.outputs.iter() {
+          for &(_, ref out, _) in ia.outputs.iter() {
               check_expr(fcx, &**out);
           }
           fcx.write_nil(id);
index d0adc75e5c8448d4e1f565867f03dda5a4d15bae..42d9430d7320e6c88098c8f59e0a421ceb5f6087 100644 (file)
@@ -958,9 +958,9 @@ pub enum AsmDialect {
 pub struct InlineAsm {
     pub asm: InternedString,
     pub asm_str_style: StrStyle,
-    pub clobbers: InternedString,
+    pub outputs: Vec<(InternedString, Gc<Expr>, bool)>,
     pub inputs: Vec<(InternedString, Gc<Expr>)>,
-    pub outputs: Vec<(InternedString, Gc<Expr>)>,
+    pub clobbers: InternedString,
     pub volatile: bool,
     pub alignstack: bool,
     pub dialect: AsmDialect
index 13738e658e9f97fd7edd5563cab06ec71cf1e8b6..43fa0964f80f94d854fb912a9e9b7df56b42f55b 100644 (file)
@@ -59,8 +59,6 @@ pub fn expand_asm(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
 
     let mut state = Asm;
 
-    let mut read_write_operands = Vec::new();
-
     'statement: loop {
         match state {
             Asm => {
@@ -100,8 +98,6 @@ pub fn expand_asm(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
                     let output = match constraint.get().slice_shift_char() {
                         (Some('='), _) => None,
                         (Some('+'), operand) => {
-                            // Save a reference to the output
-                            read_write_operands.push((outputs.len(), out));
                             Some(token::intern_and_get_ident(format!(
                                         "={}",
                                         operand).as_slice()))
@@ -112,7 +108,8 @@ pub fn expand_asm(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
                         }
                     };
 
-                    outputs.push((output.unwrap_or(constraint), out));
+                    let is_rw = output.is_some();
+                    outputs.push((output.unwrap_or(constraint), out, is_rw));
                 }
             }
             Inputs => {
@@ -202,21 +199,14 @@ pub fn expand_asm(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
         }
     }
 
-    // Append an input operand, with the form of ("0", expr)
-    // that links to an output operand.
-    for &(i, out) in read_write_operands.iter() {
-        inputs.push((token::intern_and_get_ident(i.to_string().as_slice()),
-                                                 out));
-    }
-
     MacExpr::new(box(GC) ast::Expr {
         id: ast::DUMMY_NODE_ID,
         node: ast::ExprInlineAsm(ast::InlineAsm {
             asm: token::intern_and_get_ident(asm.get()),
             asm_str_style: asm_str_style.unwrap(),
-            clobbers: token::intern_and_get_ident(cons.as_slice()),
-            inputs: inputs,
             outputs: outputs,
+            inputs: inputs,
+            clobbers: token::intern_and_get_ident(cons.as_slice()),
             volatile: volatile,
             alignstack: alignstack,
             dialect: dialect
index 9ad28f02e8034813f9e5fb76bcc4d43f273be3e8..fb96b4b83d7f3368b8df93d7778695b8686e09a0 100644 (file)
@@ -1189,8 +1189,8 @@ pub fn noop_fold_expr<T: Folder>(e: Gc<Expr>, folder: &mut T) -> Gc<Expr> {
                 inputs: a.inputs.iter().map(|&(ref c, input)| {
                     ((*c).clone(), folder.fold_expr(input))
                 }).collect(),
-                outputs: a.outputs.iter().map(|&(ref c, out)| {
-                    ((*c).clone(), folder.fold_expr(out))
+                outputs: a.outputs.iter().map(|&(ref c, out, is_rw)| {
+                    ((*c).clone(), folder.fold_expr(out), is_rw)
                 }).collect(),
                 .. (*a).clone()
             })
index 26d425563d09f5c77e12e29d27a2942102390a8f..6fe44078447e59009c96d974283735600130418b 100644 (file)
@@ -1671,8 +1671,14 @@ pub fn print_expr(&mut self, expr: &ast::Expr) -> IoResult<()> {
                 try!(self.word_space(":"));
 
                 try!(self.commasep(Inconsistent, a.outputs.as_slice(),
-                                   |s, &(ref co, ref o)| {
-                    try!(s.print_string(co.get(), ast::CookedStr));
+                                   |s, &(ref co, ref o, is_rw)| {
+                    match co.get().slice_shift_char() {
+                        (Some('='), operand) if is_rw => {
+                            try!(s.print_string(format!("+{}", operand).as_slice(),
+                                                ast::CookedStr))
+                        }
+                        _ => try!(s.print_string(co.get(), ast::CookedStr))
+                    }
                     try!(s.popen());
                     try!(s.print_expr(&**o));
                     try!(s.pclose());
index ac1dbdc439ca0c4fe788c22efd4f8619f64e2d98..9e371143311c27d0975f350689a8350fec8131ed 100644 (file)
@@ -854,11 +854,11 @@ pub fn walk_expr<E: Clone, V: Visitor<E>>(visitor: &mut V, expression: &Expr, en
         ExprParen(ref subexpression) => {
             visitor.visit_expr(&**subexpression, env.clone())
         }
-        ExprInlineAsm(ref assembler) => {
-            for &(_, ref input) in assembler.inputs.iter() {
+        ExprInlineAsm(ref ia) => {
+            for &(_, ref input) in ia.inputs.iter() {
                 visitor.visit_expr(&**input, env.clone())
             }
-            for &(_, ref output) in assembler.outputs.iter() {
+            for &(_, ref output, _) in ia.outputs.iter() {
                 visitor.visit_expr(&**output, env.clone())
             }
         }
diff --git a/src/test/run-pass/issue-14936.rs b/src/test/run-pass/issue-14936.rs
new file mode 100644 (file)
index 0000000..123ad36
--- /dev/null
@@ -0,0 +1,54 @@
+// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![feature(asm, macro_rules)]
+
+type History = Vec<&'static str>;
+
+fn wrap<A>(x:A, which: &'static str, history: &mut History) -> A {
+    history.push(which);
+    x
+}
+
+macro_rules! demo {
+    ( $output_constraint:tt ) => {
+        {
+            let mut x: int = 0;
+            let y: int = 1;
+
+            let mut history: History = vec!();
+            unsafe {
+                asm!("mov ($1), $0"
+                     : $output_constraint (*wrap(&mut x, "out", &mut history))
+                     : "r"(&wrap(y, "in", &mut history)));
+            }
+            assert_eq!((x,y), (1,1));
+            assert_eq!(history.as_slice(), &["out", "in"]);
+        }
+    }
+}
+
+#[cfg(target_arch = "x86")]
+#[cfg(target_arch = "x86_64")]
+fn main() {
+    fn out_write_only_expr_then_in_expr() {
+        demo!("=r")
+    }
+
+    fn out_read_write_expr_then_in_expr() {
+        demo!("+r")
+    }
+
+    out_write_only_expr_then_in_expr();
+    out_read_write_expr_then_in_expr();
+}
+
+#[cfg(not(target_arch = "x86"), not(target_arch = "x86_64"))]
+pub fn main() {}