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,
47 callee::DontAutorefArg)
50 let e = match out.node {
51 ast::expr_addr_of(_, e) => e,
52 _ => fail!("Expression must be addr of")
56 callee::trans_arg_expr(bcx,
62 callee::DontAutorefArg)
67 for cleanups.iter().advance |c| {
68 revoke_clean(bcx, *c);
72 // Now the input operands
73 let inputs = do ia.inputs.map |&(c, in)| {
77 callee::trans_arg_expr(bcx,
83 callee::DontAutorefArg)
88 for cleanups.iter().advance |c| {
89 revoke_clean(bcx, *c);
92 let mut constraints = constraints.connect(",");
94 let mut clobbers = getClobbers();
95 if !ia.clobbers.is_empty() && !clobbers.is_empty() {
96 clobbers = fmt!("%s,%s", ia.clobbers, clobbers);
98 clobbers += ia.clobbers;
101 // Add the clobbers to our constraints list
102 if !clobbers.is_empty() && !constraints.is_empty() {
104 constraints += clobbers;
106 constraints += clobbers;
109 debug!("Asm Constraints: %?", constraints);
111 let numOutputs = outputs.len();
113 // Depending on how many outputs we have, the return type is different
114 let output = if numOutputs == 0 {
116 } else if numOutputs == 1 {
119 Type::struct_(outputs.map(|o| val_ty(*o)), false)
122 let dialect = match ia.dialect {
123 ast::asm_att => lib::llvm::AD_ATT,
124 ast::asm_intel => lib::llvm::AD_Intel
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)
133 // Again, based on how many outputs we have
135 let op = PointerCast(bcx, aoutputs[0], val_ty(outputs[0]).ptr_to());
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());
149 // Default per-arch clobbers
150 // Basically what clang does
152 #[cfg(target_arch = "arm")]
153 #[cfg(target_arch = "mips")]
154 fn getClobbers() -> ~str {
158 #[cfg(target_arch = "x86")]
159 #[cfg(target_arch = "x86_64")]
160 fn getClobbers() -> ~str {
161 ~"~{dirflag},~{fpsr},~{flags}"