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.
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.
12 # Translation of inline assembly.
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;
25 use std::c_str::ToCStr;
26 use std::string::String;
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)
34 let mut constraints = Vec::new();
35 let mut output_types = Vec::new();
37 let temp_scope = fcx.push_custom_cleanup_scope();
39 // Prepare the output operands
40 let outputs = ia.outputs.iter().map(|&(ref c, out)| {
41 constraints.push((*c).clone());
43 let out_datum = unpack_datum!(bcx, expr::trans(bcx, out));
44 output_types.push(type_of::type_of(bcx.ccx(), out_datum.ty));
47 }).collect::<Vec<_>>();
49 // Now the input operands
50 let inputs = ia.inputs.iter().map(|&(ref c, input)| {
51 constraints.push((*c).clone());
53 let in_datum = unpack_datum!(bcx, expr::trans(bcx, input));
55 callee::trans_arg_datum(bcx,
58 cleanup::CustomScope(temp_scope),
59 callee::DontAutorefArg)
61 }).collect::<Vec<_>>();
63 // no failure occurred preparing operands, no need to cleanup
64 fcx.pop_custom_cleanup_scope(temp_scope);
67 String::from_str(constraints.iter()
68 .map(|s| s.get().to_string())
69 .collect::<Vec<String>>()
73 let mut clobbers = getClobbers();
74 if !ia.clobbers.get().is_empty() && !clobbers.is_empty() {
75 clobbers = format_strbuf!("{},{}", ia.clobbers.get(), clobbers);
77 clobbers.push_str(ia.clobbers.get());
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());
85 constraints.push_str(clobbers.as_slice());
88 debug!("Asm Constraints: {:?}", constraints.as_slice());
90 let num_outputs = outputs.len();
92 // Depending on how many outputs we have, the return type is different
93 let output_type = if num_outputs == 0 {
95 } else if num_outputs == 1 {
98 Type::struct_(bcx.ccx(), output_types.as_slice(), false)
101 let dialect = match ia.dialect {
102 ast::AsmAtt => lib::llvm::AD_ATT,
103 ast::AsmIntel => lib::llvm::AD_Intel
106 let r = ia.asm.get().with_c_str(|a| {
107 constraints.as_slice().with_c_str(|c| {
119 // Again, based on how many outputs we have
120 if num_outputs == 1 {
121 Store(bcx, r, *outputs.get(0));
123 for (i, o) in outputs.iter().enumerate() {
124 let v = ExtractValue(bcx, r, i);
133 // Default per-arch clobbers
134 // Basically what clang does
136 #[cfg(target_arch = "arm")]
137 #[cfg(target_arch = "mips")]
138 fn getClobbers() -> String {
142 #[cfg(target_arch = "x86")]
143 #[cfg(target_arch = "x86_64")]
144 fn getClobbers() -> String {
145 "~{dirflag},~{fpsr},~{flags}".to_string()