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.
15 use std::c_str::ToCStr;
18 use middle::trans::build::*;
19 use middle::trans::callee;
20 use middle::trans::common::*;
21 use middle::trans::cleanup;
22 use middle::trans::cleanup::CleanupMethods;
23 use middle::trans::expr;
24 use middle::trans::type_of;
26 use middle::trans::type_::Type;
30 // Take an inline assembly expression and splat it out via LLVM
31 pub fn trans_inline_asm<'a>(bcx: &'a Block<'a>, ia: &ast::InlineAsm)
35 let mut constraints = ~[];
36 let mut output_types = ~[];
38 let temp_scope = fcx.push_custom_cleanup_scope();
40 // Prepare the output operands
41 let outputs = ia.outputs.map(|&(ref c, out)| {
42 constraints.push((*c).clone());
44 let out_datum = unpack_datum!(bcx, expr::trans(bcx, out));
45 output_types.push(type_of::type_of(bcx.ccx(), out_datum.ty));
50 // Now the input operands
51 let inputs = ia.inputs.map(|&(ref c, input)| {
52 constraints.push((*c).clone());
55 callee::trans_arg_expr(bcx,
58 cleanup::CustomScope(temp_scope),
59 callee::DontAutorefArg)
63 // no failure occurred preparing operands, no need to cleanup
64 fcx.pop_custom_cleanup_scope(temp_scope);
66 let mut constraints = constraints.map(|s| s.get().to_str()).connect(",");
68 let mut clobbers = getClobbers();
69 if !ia.clobbers.get().is_empty() && !clobbers.is_empty() {
70 clobbers = format!("{},{}", ia.clobbers.get(), clobbers);
72 clobbers.push_str(ia.clobbers.get());
75 // Add the clobbers to our constraints list
76 if clobbers.len() != 0 && constraints.len() != 0 {
77 constraints.push_char(',');
78 constraints.push_str(clobbers);
80 constraints.push_str(clobbers);
83 debug!("Asm Constraints: {:?}", constraints);
85 let numOutputs = outputs.len();
87 // Depending on how many outputs we have, the return type is different
88 let output_type = if numOutputs == 0 {
90 } else if numOutputs == 1 {
93 Type::struct_(output_types, false)
96 let dialect = match ia.dialect {
97 ast::AsmAtt => lib::llvm::AD_ATT,
98 ast::AsmIntel => lib::llvm::AD_Intel
101 let r = ia.asm.get().with_c_str(|a| {
102 constraints.with_c_str(|c| {
114 // Again, based on how many outputs we have
116 Store(bcx, r, *outputs.get(0));
118 for (i, o) in outputs.iter().enumerate() {
119 let v = ExtractValue(bcx, r, i);
128 // Default per-arch clobbers
129 // Basically what clang does
131 #[cfg(target_arch = "arm")]
132 #[cfg(target_arch = "mips")]
133 fn getClobbers() -> ~str {
137 #[cfg(target_arch = "x86")]
138 #[cfg(target_arch = "x86_64")]
139 fn getClobbers() -> ~str {
140 ~"~{dirflag},~{fpsr},~{flags}"