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.
17 use middle::trans::build::*;
18 use middle::trans::callee;
19 use middle::trans::common::*;
22 use middle::trans::type_::Type;
27 // Take an inline assembly expression and splat it out via LLVM
28 pub fn trans_inline_asm(bcx: block, ia: &ast::inline_asm) -> block {
31 let mut constraints = ~[];
32 let mut cleanups = ~[];
33 let mut aoutputs = ~[];
35 // Prepare the output operands
36 let outputs = do ia.outputs.map |&(c, out)| {
39 aoutputs.push(unpack_result!(bcx, {
40 callee::trans_arg_expr(bcx,
46 callee::DontAutorefArg)
49 let e = match out.node {
50 ast::expr_addr_of(_, e) => e,
51 _ => fail!("Expression must be addr of")
55 callee::trans_arg_expr(bcx,
61 callee::DontAutorefArg)
66 for cleanups.iter().advance |c| {
67 revoke_clean(bcx, *c);
71 // Now the input operands
72 let inputs = do ia.inputs.map |&(c, in)| {
76 callee::trans_arg_expr(bcx,
82 callee::DontAutorefArg)
87 for cleanups.iter().advance |c| {
88 revoke_clean(bcx, *c);
91 let mut constraints = constraints.connect(",");
93 let mut clobbers = getClobbers();
94 if !ia.clobbers.is_empty() && !clobbers.is_empty() {
95 clobbers = fmt!("%s,%s", ia.clobbers, clobbers);
97 clobbers.push_str(ia.clobbers);
100 // Add the clobbers to our constraints list
101 if clobbers.len() != 0 && constraints.len() != 0 {
102 constraints.push_char(',');
103 constraints.push_str(clobbers);
105 constraints.push_str(clobbers);
108 debug!("Asm Constraints: %?", constraints);
110 let numOutputs = outputs.len();
112 // Depending on how many outputs we have, the return type is different
113 let output = if numOutputs == 0 {
115 } else if numOutputs == 1 {
118 Type::struct_(outputs.map(|o| val_ty(*o)), false)
121 let dialect = match ia.dialect {
122 ast::asm_att => lib::llvm::AD_ATT,
123 ast::asm_intel => lib::llvm::AD_Intel
126 let r = do str::as_c_str(ia.asm) |a| {
127 do str::as_c_str(constraints) |c| {
128 InlineAsmCall(bcx, a, c, inputs, output, ia.volatile, ia.alignstack, dialect)
132 // Again, based on how many outputs we have
134 let op = PointerCast(bcx, aoutputs[0], val_ty(outputs[0]).ptr_to());
137 for aoutputs.iter().enumerate().advance |(i, o)| {
138 let v = ExtractValue(bcx, r, i);
139 let op = PointerCast(bcx, *o, val_ty(outputs[i]).ptr_to());
148 // Default per-arch clobbers
149 // Basically what clang does
151 #[cfg(target_arch = "arm")]
152 #[cfg(target_arch = "mips")]
153 fn getClobbers() -> ~str {
157 #[cfg(target_arch = "x86")]
158 #[cfg(target_arch = "x86_64")]
159 fn getClobbers() -> ~str {
160 ~"~{dirflag},~{fpsr},~{flags}"