]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/trans/asm.rs
3c263b1c01eb6bb16478b0894b6ad8634997fba6
[rust.git] / src / librustc / middle / trans / asm.rs
1 // Copyright 2012-2013 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 /*!
12 # Translation of inline assembly.
13 */
14
15 use core::prelude::*;
16
17 use lib;
18 use middle::trans::build::*;
19 use middle::trans::callee;
20 use middle::trans::common::*;
21 use middle::ty;
22
23 use middle::trans::type_::Type;
24
25 use core::str;
26 use syntax::ast;
27
28 // Take an inline assembly expression and splat it out via LLVM
29 pub fn trans_inline_asm(bcx: block, ia: &ast::inline_asm) -> block {
30
31     let mut bcx = bcx;
32     let mut constraints = ~[];
33     let mut cleanups = ~[];
34     let mut aoutputs = ~[];
35
36     // Prepare the output operands
37     let outputs = do ia.outputs.map |&(c, out)| {
38         constraints.push(c);
39
40         aoutputs.push(unpack_result!(bcx, {
41             callee::trans_arg_expr(bcx,
42                                    expr_ty(bcx, out),
43                                    ty::ByCopy,
44                                    out,
45                                    &mut cleanups,
46                                    None,
47                                    callee::DontAutorefArg)
48         }));
49
50         let e = match out.node {
51             ast::expr_addr_of(_, e) => e,
52             _ => fail!("Expression must be addr of")
53         };
54
55         unpack_result!(bcx, {
56             callee::trans_arg_expr(bcx,
57                                    expr_ty(bcx, e),
58                                    ty::ByCopy,
59                                    e,
60                                    &mut cleanups,
61                                    None,
62                                    callee::DontAutorefArg)
63         })
64
65     };
66
67     for cleanups.iter().advance |c| {
68         revoke_clean(bcx, *c);
69     }
70     cleanups.clear();
71
72     // Now the input operands
73     let inputs = do ia.inputs.map |&(c, in)| {
74         constraints.push(c);
75
76         unpack_result!(bcx, {
77             callee::trans_arg_expr(bcx,
78                                    expr_ty(bcx, in),
79                                    ty::ByCopy,
80                                    in,
81                                    &mut cleanups,
82                                    None,
83                                    callee::DontAutorefArg)
84         })
85
86     };
87
88     for cleanups.iter().advance |c| {
89         revoke_clean(bcx, *c);
90     }
91
92     let mut constraints = constraints.connect(",");
93
94     let mut clobbers = getClobbers();
95     if !ia.clobbers.is_empty() && !clobbers.is_empty() {
96         clobbers = fmt!("%s,%s", ia.clobbers, clobbers);
97     } else {
98         clobbers += ia.clobbers;
99     };
100
101     // Add the clobbers to our constraints list
102     if !clobbers.is_empty() && !constraints.is_empty() {
103         constraints += ",";
104         constraints += clobbers;
105     } else {
106         constraints += clobbers;
107     }
108
109     debug!("Asm Constraints: %?", constraints);
110
111     let numOutputs = outputs.len();
112
113     // Depending on how many outputs we have, the return type is different
114     let output = if numOutputs == 0 {
115         Type::void()
116     } else if numOutputs == 1 {
117         val_ty(outputs[0])
118     } else {
119         Type::struct_(outputs.map(|o| val_ty(*o)), false)
120     };
121
122     let dialect = match ia.dialect {
123         ast::asm_att   => lib::llvm::AD_ATT,
124         ast::asm_intel => lib::llvm::AD_Intel
125     };
126
127     let r = do str::as_c_str(ia.asm) |a| {
128         do str::as_c_str(constraints) |c| {
129             InlineAsmCall(bcx, a, c, inputs, output, ia.volatile, ia.alignstack, dialect)
130         }
131     };
132
133     // Again, based on how many outputs we have
134     if numOutputs == 1 {
135         let op = PointerCast(bcx, aoutputs[0], val_ty(outputs[0]).ptr_to());
136         Store(bcx, r, op);
137     } else {
138         for aoutputs.iter().enumerate().advance |(i, o)| {
139             let v = ExtractValue(bcx, r, i);
140             let op = PointerCast(bcx, *o, val_ty(outputs[i]).ptr_to());
141             Store(bcx, v, op);
142         }
143     }
144
145     return bcx;
146
147 }
148
149 // Default per-arch clobbers
150 // Basically what clang does
151
152 #[cfg(target_arch = "arm")]
153 #[cfg(target_arch = "mips")]
154 fn getClobbers() -> ~str {
155     ~""
156 }
157
158 #[cfg(target_arch = "x86")]
159 #[cfg(target_arch = "x86_64")]
160 fn getClobbers() -> ~str {
161     ~"~{dirflag},~{fpsr},~{flags}"
162 }