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.
18 use middle::trans::build::*;
19 use middle::trans::callee;
20 use middle::trans::common::*;
23 use middle::trans::type_::Type;
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 {
32 let mut constraints = ~[];
33 let mut cleanups = ~[];
34 let mut aoutputs = ~[];
36 // Prepare the output operands
37 let outputs = do ia.outputs.map |&(c, out)| {
40 aoutputs.push(unpack_result!(bcx, {
41 callee::trans_arg_expr(bcx,
48 callee::DontAutorefArg)
51 let e = match out.node {
52 ast::expr_addr_of(_, e) => e,
53 _ => fail!("Expression must be addr of")
57 callee::trans_arg_expr(bcx,
64 callee::DontAutorefArg)
69 for cleanups.iter().advance |c| {
70 revoke_clean(bcx, *c);
74 // Now the input operands
75 let inputs = do ia.inputs.map |&(c, in)| {
79 callee::trans_arg_expr(bcx,
86 callee::DontAutorefArg)
91 for cleanups.iter().advance |c| {
92 revoke_clean(bcx, *c);
95 let mut constraints = constraints.connect(",");
97 let mut clobbers = getClobbers();
98 if !ia.clobbers.is_empty() && !clobbers.is_empty() {
99 clobbers = fmt!("%s,%s", ia.clobbers, clobbers);
101 clobbers += ia.clobbers;
104 // Add the clobbers to our constraints list
105 if !clobbers.is_empty() && !constraints.is_empty() {
107 constraints += clobbers;
109 constraints += clobbers;
112 debug!("Asm Constraints: %?", constraints);
114 let numOutputs = outputs.len();
116 // Depending on how many outputs we have, the return type is different
117 let output = if numOutputs == 0 {
119 } else if numOutputs == 1 {
122 Type::struct_(outputs.map(|o| val_ty(*o)), false)
125 let dialect = match ia.dialect {
126 ast::asm_att => lib::llvm::AD_ATT,
127 ast::asm_intel => lib::llvm::AD_Intel
130 let r = do str::as_c_str(ia.asm) |a| {
131 do str::as_c_str(constraints) |c| {
132 InlineAsmCall(bcx, a, c, inputs, output, ia.volatile, ia.alignstack, dialect)
136 // Again, based on how many outputs we have
138 let op = PointerCast(bcx, aoutputs[0], val_ty(outputs[0]).ptr_to());
141 for aoutputs.iter().enumerate().advance |(i, o)| {
142 let v = ExtractValue(bcx, r, i);
143 let op = PointerCast(bcx, *o, val_ty(outputs[i]).ptr_to());
152 // Default per-arch clobbers
153 // Basically what clang does
155 #[cfg(target_arch = "arm")]
156 #[cfg(target_arch = "mips")]
157 fn getClobbers() -> ~str {
161 #[cfg(target_arch = "x86")]
162 #[cfg(target_arch = "x86_64")]
163 fn getClobbers() -> ~str {
164 ~"~{dirflag},~{fpsr},~{flags}"