about summary refs log tree commit diff
path: root/src/libsyntax
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2013-03-15 21:15:46 -0700
committerbors <bors@rust-lang.org>2013-03-15 21:15:46 -0700
commitdc5ad5070d06015d6a45f656882ae245197d0ff8 (patch)
tree977b611b2987dadc410537ffdd32e1671a11b959 /src/libsyntax
parent6f1e8ef71abb390a8f6406292ea06c729dcbf9e5 (diff)
parent83f2d4ab3dbd3b52ea60212a6698c73201b67a34 (diff)
downloadrust-dc5ad5070d06015d6a45f656882ae245197d0ff8.tar.gz
rust-dc5ad5070d06015d6a45f656882ae245197d0ff8.zip
auto merge of #5359 : luqmana/rust/inline-asm, r=pcwalton
Continuation of #5317. Actually use operands properly now, including any number of output operands.

Which means you can do things like call printf:
```Rust
fn main() {
    unsafe {
        do str::as_c_str(~"The answer is %d.\n") |c| {
            let a = 42;
            asm!("mov $0, %rdi\n\t\
                  mov $1, %rsi\n\t\
                  xorl %eax, %eax\n\t\
                  call _printf"
                 :
                 : "r"(c), "r"(a)
                 : "rdi", "rsi", "eax"
                 : "volatile","alignstack"
                 );
        }
    }
}
```

```
% rustc foo.rs
% ./foo
The answer is 42.
```

Or just add 2 numbers:
```Rust
fn add(a: int, b: int) -> int {
    let mut c = 0;
    unsafe {
        asm!("add $2, $0"
             : "=r"(c)
             : "0"(a), "r"(b)
             );
    }
    c
}

fn main() {
    io::println(fmt!("%d", add(1, 2)));
}
```

```
% rustc foo.rs
% ./foo
3
```

Multiple outputs!
```Rust
fn addsub(a: int, b: int) -> (int, int) {
    let mut c = 0;
    let mut d = 0;
    unsafe {
        asm!("add $4, $0\n\t\
              sub $4, $1"
             : "=r"(c), "=r"(d)
             : "0"(a), "1"(a), "r"(b)
             );
    }
    (c, d)
}

fn main() {
    io::println(fmt!("%?", addsub(5, 1)));
}
```
```
% rustc foo.rs
% ./foo
(6, 4)
```

This also classifies inline asm as RvalueStmtExpr instead of the somewhat arbitrary kind I made it initially. There are a few XXX's regarding what to do in the liveness and move passes.
Diffstat (limited to 'src/libsyntax')
-rw-r--r--src/libsyntax/ast.rs6
-rw-r--r--src/libsyntax/ext/asm.rs10
-rw-r--r--src/libsyntax/fold.rs9
-rw-r--r--src/libsyntax/print/pprust.rs20
-rw-r--r--src/libsyntax/visit.rs9
5 files changed, 47 insertions, 7 deletions
diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs
index 298cb241bed..e5fb2ad153c 100644
--- a/src/libsyntax/ast.rs
+++ b/src/libsyntax/ast.rs
@@ -601,8 +601,10 @@ pub enum expr_ {
     expr_ret(Option<@expr>),
     expr_log(log_level, @expr, @expr),
 
-    /* asm, clobbers + constraints, volatile, align stack */
-    expr_inline_asm(@~str, @~str, bool, bool),
+    expr_inline_asm(@~str,              // asm
+                    ~[(@~str, @expr)],  // inputs
+                    ~[(@~str, @expr)],  // outputs
+                    @~str, bool, bool), // clobbers, volatile, align stack
 
     expr_mac(mac),
 
diff --git a/src/libsyntax/ext/asm.rs b/src/libsyntax/ext/asm.rs
index 4f2fd68ff95..a014d8ccb8b 100644
--- a/src/libsyntax/ext/asm.rs
+++ b/src/libsyntax/ext/asm.rs
@@ -75,6 +75,13 @@ pub fn expand_asm(cx: @ext_ctxt, sp: span, tts: &[ast::token_tree])
                     let out = p.parse_expr();
                     p.expect(&token::RPAREN);
 
+                    let out = @ast::expr {
+                        id: cx.next_id(),
+                        callee_id: cx.next_id(),
+                        span: out.span,
+                        node: ast::expr_addr_of(ast::m_mutbl, out)
+                    };
+
                     outputs.push((constraint, out));
                 }
             }
@@ -156,7 +163,8 @@ pub fn expand_asm(cx: @ext_ctxt, sp: span, tts: &[ast::token_tree])
     MRExpr(@ast::expr {
         id: cx.next_id(),
         callee_id: cx.next_id(),
-        node: ast::expr_inline_asm(@asm, @cons, volatile, alignstack),
+        node: ast::expr_inline_asm(@asm, inputs, outputs,
+                                   @cons, volatile, alignstack),
         span: sp
     })
 }
diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs
index 1626c55e721..a8952f313a5 100644
--- a/src/libsyntax/fold.rs
+++ b/src/libsyntax/fold.rs
@@ -560,7 +560,14 @@ pub fn noop_fold_expr(e: &expr_, fld: @ast_fold) -> expr_ {
                 fld.fold_expr(e)
             )
         }
-        expr_inline_asm(*) => copy *e,
+        expr_inline_asm(asm, ins, outs, c, v, a) => {
+            expr_inline_asm(
+                asm,
+                ins.map(|&(c, in)| (c, fld.fold_expr(in))),
+                outs.map(|&(c, out)| (c, fld.fold_expr(out))),
+                c, v, a
+            )
+        }
         expr_mac(ref mac) => expr_mac(fold_mac((*mac))),
         expr_struct(path, ref fields, maybe_expr) => {
             expr_struct(
diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs
index 724e61daea7..370434010b2 100644
--- a/src/libsyntax/print/pprust.rs
+++ b/src/libsyntax/print/pprust.rs
@@ -1403,7 +1403,7 @@ pub fn print_expr(s: @ps, &&expr: @ast::expr) {
           }
         }
       }
-      ast::expr_inline_asm(a, c, v, _) => {
+      ast::expr_inline_asm(a, in, out, c, v, _) => {
         if v {
             word(s.s, ~"__volatile__ asm!");
         } else {
@@ -1411,7 +1411,23 @@ pub fn print_expr(s: @ps, &&expr: @ast::expr) {
         }
         popen(s);
         print_string(s, *a);
-        word_space(s, ~",");
+        word_space(s, ~":");
+        for out.each |&(co, o)| {
+            print_string(s, *co);
+            popen(s);
+            print_expr(s, o);
+            pclose(s);
+            word_space(s, ~",");
+        }
+        word_space(s, ~":");
+        for in.each |&(co, o)| {
+            print_string(s, *co);
+            popen(s);
+            print_expr(s, o);
+            pclose(s);
+            word_space(s, ~",");
+        }
+        word_space(s, ~":");
         print_string(s, *c);
         pclose(s);
       }
diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs
index 95ab603f584..6a0f1a2ec46 100644
--- a/src/libsyntax/visit.rs
+++ b/src/libsyntax/visit.rs
@@ -562,7 +562,14 @@ pub fn visit_expr<E>(ex: @expr, e: E, v: vt<E>) {
         }
         expr_mac(ref mac) => visit_mac((*mac), e, v),
         expr_paren(x) => (v.visit_expr)(x, e, v),
-        expr_inline_asm(*) => (),
+        expr_inline_asm(_, ins, outs, _, _, _) => {
+            for ins.each |&(_, in)| {
+                (v.visit_expr)(in, e, v);
+            }
+            for outs.each |&(_, out)| {
+                (v.visit_expr)(out, e, v);
+            }
+        }
     }
     (v.visit_expr_post)(ex, e, v);
 }