2013-04-10 07:47:22 -05:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2013-03-27 14:50:57 -05:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
/*!
|
|
|
|
# Translation of inline assembly.
|
|
|
|
*/
|
|
|
|
|
2013-05-17 17:28:44 -05:00
|
|
|
use core::prelude::*;
|
|
|
|
|
2013-03-27 14:50:57 -05:00
|
|
|
use lib;
|
|
|
|
use middle::trans::build::*;
|
|
|
|
use middle::trans::callee;
|
|
|
|
use middle::trans::common::*;
|
|
|
|
use middle::ty;
|
|
|
|
|
2013-06-16 05:52:44 -05:00
|
|
|
use middle::trans::type_::Type;
|
|
|
|
|
2013-05-24 21:35:29 -05:00
|
|
|
use core::str;
|
2013-03-27 14:50:57 -05:00
|
|
|
use syntax::ast;
|
|
|
|
|
|
|
|
// Take an inline assembly expression and splat it out via LLVM
|
2013-03-27 15:42:21 -05:00
|
|
|
pub fn trans_inline_asm(bcx: block, ia: &ast::inline_asm) -> block {
|
2013-03-27 14:50:57 -05:00
|
|
|
|
|
|
|
let mut bcx = bcx;
|
|
|
|
let mut constraints = ~[];
|
|
|
|
let mut cleanups = ~[];
|
|
|
|
let mut aoutputs = ~[];
|
|
|
|
|
|
|
|
// Prepare the output operands
|
2013-03-27 15:42:21 -05:00
|
|
|
let outputs = do ia.outputs.map |&(c, out)| {
|
2013-06-12 12:02:55 -05:00
|
|
|
constraints.push(c);
|
2013-03-27 14:50:57 -05:00
|
|
|
|
|
|
|
aoutputs.push(unpack_result!(bcx, {
|
2013-04-24 03:29:46 -05:00
|
|
|
callee::trans_arg_expr(bcx,
|
2013-04-26 21:13:38 -05:00
|
|
|
expr_ty(bcx, out),
|
2013-04-24 03:29:46 -05:00
|
|
|
ty::ByCopy,
|
|
|
|
out,
|
|
|
|
&mut cleanups,
|
|
|
|
None,
|
|
|
|
callee::DontAutorefArg)
|
2013-03-27 14:50:57 -05:00
|
|
|
}));
|
|
|
|
|
|
|
|
let e = match out.node {
|
|
|
|
ast::expr_addr_of(_, e) => e,
|
2013-05-05 17:18:51 -05:00
|
|
|
_ => fail!("Expression must be addr of")
|
2013-03-27 14:50:57 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
unpack_result!(bcx, {
|
2013-04-24 03:29:46 -05:00
|
|
|
callee::trans_arg_expr(bcx,
|
2013-04-26 21:13:38 -05:00
|
|
|
expr_ty(bcx, e),
|
2013-04-24 03:29:46 -05:00
|
|
|
ty::ByCopy,
|
|
|
|
e,
|
|
|
|
&mut cleanups,
|
|
|
|
None,
|
|
|
|
callee::DontAutorefArg)
|
2013-03-27 14:50:57 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
for cleanups.each |c| {
|
|
|
|
revoke_clean(bcx, *c);
|
|
|
|
}
|
|
|
|
cleanups.clear();
|
|
|
|
|
|
|
|
// Now the input operands
|
2013-03-27 15:42:21 -05:00
|
|
|
let inputs = do ia.inputs.map |&(c, in)| {
|
2013-06-12 12:02:55 -05:00
|
|
|
constraints.push(c);
|
2013-03-27 14:50:57 -05:00
|
|
|
|
|
|
|
unpack_result!(bcx, {
|
2013-04-24 03:29:46 -05:00
|
|
|
callee::trans_arg_expr(bcx,
|
2013-04-26 21:13:38 -05:00
|
|
|
expr_ty(bcx, in),
|
2013-04-24 03:29:46 -05:00
|
|
|
ty::ByCopy,
|
|
|
|
in,
|
|
|
|
&mut cleanups,
|
|
|
|
None,
|
|
|
|
callee::DontAutorefArg)
|
2013-03-27 14:50:57 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
for cleanups.each |c| {
|
|
|
|
revoke_clean(bcx, *c);
|
|
|
|
}
|
|
|
|
|
2013-06-10 08:25:25 -05:00
|
|
|
let mut constraints = constraints.connect(",");
|
2013-03-27 14:50:57 -05:00
|
|
|
|
2013-03-27 17:37:28 -05:00
|
|
|
let mut clobbers = getClobbers();
|
2013-06-12 12:02:55 -05:00
|
|
|
if !ia.clobbers.is_empty() && !clobbers.is_empty() {
|
|
|
|
clobbers = fmt!("%s,%s", ia.clobbers, clobbers);
|
2013-03-27 17:37:28 -05:00
|
|
|
} else {
|
2013-06-12 12:02:55 -05:00
|
|
|
clobbers += ia.clobbers;
|
2013-03-27 17:37:28 -05:00
|
|
|
};
|
|
|
|
|
2013-03-27 14:50:57 -05:00
|
|
|
// Add the clobbers to our constraints list
|
2013-06-12 12:02:55 -05:00
|
|
|
if !clobbers.is_empty() && !constraints.is_empty() {
|
2013-05-29 13:10:16 -05:00
|
|
|
constraints += ",";
|
|
|
|
constraints += clobbers;
|
2013-03-27 14:50:57 -05:00
|
|
|
} else {
|
2013-03-27 17:37:28 -05:00
|
|
|
constraints += clobbers;
|
2013-03-27 14:50:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
debug!("Asm Constraints: %?", constraints);
|
|
|
|
|
|
|
|
let numOutputs = outputs.len();
|
|
|
|
|
|
|
|
// Depending on how many outputs we have, the return type is different
|
|
|
|
let output = if numOutputs == 0 {
|
2013-06-15 09:29:52 -05:00
|
|
|
Type::void()
|
2013-03-27 14:50:57 -05:00
|
|
|
} else if numOutputs == 1 {
|
|
|
|
val_ty(outputs[0])
|
|
|
|
} else {
|
2013-06-15 09:29:52 -05:00
|
|
|
Type::struct_(outputs.map(|o| val_ty(*o)), false)
|
2013-03-27 14:50:57 -05:00
|
|
|
};
|
|
|
|
|
2013-03-27 16:42:19 -05:00
|
|
|
let dialect = match ia.dialect {
|
|
|
|
ast::asm_att => lib::llvm::AD_ATT,
|
|
|
|
ast::asm_intel => lib::llvm::AD_Intel
|
|
|
|
};
|
|
|
|
|
2013-06-12 12:02:55 -05:00
|
|
|
let r = do str::as_c_str(ia.asm) |a| {
|
2013-03-27 14:50:57 -05:00
|
|
|
do str::as_c_str(constraints) |c| {
|
2013-03-27 16:42:19 -05:00
|
|
|
InlineAsmCall(bcx, a, c, inputs, output, ia.volatile, ia.alignstack, dialect)
|
2013-03-27 14:50:57 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-03-28 18:34:10 -05:00
|
|
|
// Again, based on how many outputs we have
|
2013-03-27 14:50:57 -05:00
|
|
|
if numOutputs == 1 {
|
2013-06-15 09:29:52 -05:00
|
|
|
let op = PointerCast(bcx, aoutputs[0], val_ty(outputs[0]).ptr_to());
|
2013-03-27 14:50:57 -05:00
|
|
|
Store(bcx, r, op);
|
|
|
|
} else {
|
2013-06-17 18:43:22 -05:00
|
|
|
for aoutputs.iter().enumerate().advance |(i, o)| {
|
2013-03-27 14:50:57 -05:00
|
|
|
let v = ExtractValue(bcx, r, i);
|
2013-06-15 09:29:52 -05:00
|
|
|
let op = PointerCast(bcx, *o, val_ty(outputs[i]).ptr_to());
|
2013-03-27 14:50:57 -05:00
|
|
|
Store(bcx, v, op);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bcx;
|
|
|
|
|
|
|
|
}
|
2013-03-27 17:37:28 -05:00
|
|
|
|
|
|
|
// Default per-arch clobbers
|
|
|
|
// Basically what clang does
|
|
|
|
|
|
|
|
#[cfg(target_arch = "arm")]
|
|
|
|
#[cfg(target_arch = "mips")]
|
|
|
|
fn getClobbers() -> ~str {
|
|
|
|
~""
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_arch = "x86")]
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
fn getClobbers() -> ~str {
|
|
|
|
~"~{dirflag},~{fpsr},~{flags}"
|
|
|
|
}
|