]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/trans/asm.rs
core: rename strbuf::StrBuf to string::String
[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 lib;
16 use middle::trans::build::*;
17 use middle::trans::callee;
18 use middle::trans::common::*;
19 use middle::trans::cleanup;
20 use middle::trans::cleanup::CleanupMethods;
21 use middle::trans::expr;
22 use middle::trans::type_of;
23 use middle::trans::type_::Type;
24
25 use std::c_str::ToCStr;
26 use std::string::String;
27 use syntax::ast;
28
29 // Take an inline assembly expression and splat it out via LLVM
30 pub fn trans_inline_asm<'a>(bcx: &'a Block<'a>, ia: &ast::InlineAsm)
31                         -> &'a Block<'a> {
32     let fcx = bcx.fcx;
33     let mut bcx = bcx;
34     let mut constraints = Vec::new();
35     let mut output_types = Vec::new();
36
37     let temp_scope = fcx.push_custom_cleanup_scope();
38
39     // Prepare the output operands
40     let outputs = ia.outputs.iter().map(|&(ref c, out)| {
41         constraints.push((*c).clone());
42
43         let out_datum = unpack_datum!(bcx, expr::trans(bcx, out));
44         output_types.push(type_of::type_of(bcx.ccx(), out_datum.ty));
45         out_datum.val
46
47     }).collect::<Vec<_>>();
48
49     // Now the input operands
50     let inputs = ia.inputs.iter().map(|&(ref c, input)| {
51         constraints.push((*c).clone());
52
53         let in_datum = unpack_datum!(bcx, expr::trans(bcx, input));
54         unpack_result!(bcx, {
55             callee::trans_arg_datum(bcx,
56                                    expr_ty(bcx, input),
57                                    in_datum,
58                                    cleanup::CustomScope(temp_scope),
59                                    callee::DontAutorefArg)
60         })
61     }).collect::<Vec<_>>();
62
63     // no failure occurred preparing operands, no need to cleanup
64     fcx.pop_custom_cleanup_scope(temp_scope);
65
66     let mut constraints =
67         String::from_str(constraints.iter()
68                                     .map(|s| s.get().to_strbuf())
69                                     .collect::<Vec<String>>()
70                                     .connect(",")
71                                     .as_slice());
72
73     let mut clobbers = getClobbers();
74     if !ia.clobbers.get().is_empty() && !clobbers.is_empty() {
75         clobbers = format_strbuf!("{},{}", ia.clobbers.get(), clobbers);
76     } else {
77         clobbers.push_str(ia.clobbers.get());
78     }
79
80     // Add the clobbers to our constraints list
81     if clobbers.len() != 0 && constraints.len() != 0 {
82         constraints.push_char(',');
83         constraints.push_str(clobbers.as_slice());
84     } else {
85         constraints.push_str(clobbers.as_slice());
86     }
87
88     debug!("Asm Constraints: {:?}", constraints.as_slice());
89
90     let num_outputs = outputs.len();
91
92     // Depending on how many outputs we have, the return type is different
93     let output_type = if num_outputs == 0 {
94         Type::void(bcx.ccx())
95     } else if num_outputs == 1 {
96         *output_types.get(0)
97     } else {
98         Type::struct_(bcx.ccx(), output_types.as_slice(), false)
99     };
100
101     let dialect = match ia.dialect {
102         ast::AsmAtt   => lib::llvm::AD_ATT,
103         ast::AsmIntel => lib::llvm::AD_Intel
104     };
105
106     let r = ia.asm.get().with_c_str(|a| {
107         constraints.as_slice().with_c_str(|c| {
108             InlineAsmCall(bcx,
109                           a,
110                           c,
111                           inputs.as_slice(),
112                           output_type,
113                           ia.volatile,
114                           ia.alignstack,
115                           dialect)
116         })
117     });
118
119     // Again, based on how many outputs we have
120     if num_outputs == 1 {
121         Store(bcx, r, *outputs.get(0));
122     } else {
123         for (i, o) in outputs.iter().enumerate() {
124             let v = ExtractValue(bcx, r, i);
125             Store(bcx, v, *o);
126         }
127     }
128
129     return bcx;
130
131 }
132
133 // Default per-arch clobbers
134 // Basically what clang does
135
136 #[cfg(target_arch = "arm")]
137 #[cfg(target_arch = "mips")]
138 fn getClobbers() -> String {
139     "".to_strbuf()
140 }
141
142 #[cfg(target_arch = "x86")]
143 #[cfg(target_arch = "x86_64")]
144 fn getClobbers() -> String {
145     "~{dirflag},~{fpsr},~{flags}".to_strbuf()
146 }