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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
use lib;
|
|
|
|
use middle::trans::build::*;
|
|
|
|
use middle::trans::callee;
|
|
|
|
use middle::trans::common::*;
|
2014-01-15 13:39:08 -06:00
|
|
|
use middle::trans::cleanup;
|
|
|
|
use middle::trans::cleanup::CleanupMethods;
|
|
|
|
use middle::trans::expr;
|
|
|
|
use middle::trans::type_of;
|
2013-06-16 05:52:44 -05:00
|
|
|
use middle::trans::type_::Type;
|
|
|
|
|
2014-03-08 14:36:22 -06:00
|
|
|
use std::c_str::ToCStr;
|
2014-05-22 18:57:53 -05:00
|
|
|
use std::string::String;
|
2013-03-27 14:50:57 -05:00
|
|
|
use syntax::ast;
|
|
|
|
|
|
|
|
// Take an inline assembly expression and splat it out via LLVM
|
2014-01-09 07:05:33 -06:00
|
|
|
pub fn trans_inline_asm<'a>(bcx: &'a Block<'a>, ia: &ast::InlineAsm)
|
2014-01-07 10:54:58 -06:00
|
|
|
-> &'a Block<'a> {
|
2014-01-15 13:39:08 -06:00
|
|
|
let fcx = bcx.fcx;
|
2013-03-27 14:50:57 -05:00
|
|
|
let mut bcx = bcx;
|
2014-03-04 12:02:49 -06:00
|
|
|
let mut constraints = Vec::new();
|
|
|
|
let mut output_types = Vec::new();
|
2013-03-27 14:50:57 -05:00
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
let temp_scope = fcx.push_custom_cleanup_scope();
|
|
|
|
|
2013-03-27 14:50:57 -05:00
|
|
|
// Prepare the output operands
|
2014-03-28 14:42:34 -05:00
|
|
|
let outputs = ia.outputs.iter().map(|&(ref c, out)| {
|
2014-01-15 20:30:40 -06:00
|
|
|
constraints.push((*c).clone());
|
2013-03-27 14:50:57 -05:00
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
let out_datum = unpack_datum!(bcx, expr::trans(bcx, out));
|
|
|
|
output_types.push(type_of::type_of(bcx.ccx(), out_datum.ty));
|
2013-10-17 13:24:41 -05:00
|
|
|
out_datum.val
|
2013-03-27 14:50:57 -05:00
|
|
|
|
2014-03-28 14:42:34 -05:00
|
|
|
}).collect::<Vec<_>>();
|
2013-03-27 14:50:57 -05:00
|
|
|
|
|
|
|
// Now the input operands
|
2014-03-28 14:42:34 -05:00
|
|
|
let inputs = ia.inputs.iter().map(|&(ref c, input)| {
|
2014-01-15 20:30:40 -06:00
|
|
|
constraints.push((*c).clone());
|
2013-03-27 14:50:57 -05:00
|
|
|
|
2014-03-06 11:24:11 -06:00
|
|
|
let in_datum = unpack_datum!(bcx, expr::trans(bcx, input));
|
2013-03-27 14:50:57 -05:00
|
|
|
unpack_result!(bcx, {
|
2014-03-06 11:24:11 -06:00
|
|
|
callee::trans_arg_datum(bcx,
|
2013-07-31 16:59:59 -05:00
|
|
|
expr_ty(bcx, input),
|
2014-03-06 11:24:11 -06:00
|
|
|
in_datum,
|
2014-01-15 13:39:08 -06:00
|
|
|
cleanup::CustomScope(temp_scope),
|
2013-04-24 03:29:46 -05:00
|
|
|
callee::DontAutorefArg)
|
2013-03-27 14:50:57 -05:00
|
|
|
})
|
2014-03-28 14:42:34 -05:00
|
|
|
}).collect::<Vec<_>>();
|
2013-03-27 14:50:57 -05:00
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
// no failure occurred preparing operands, no need to cleanup
|
|
|
|
fcx.pop_custom_cleanup_scope(temp_scope);
|
2013-03-27 14:50:57 -05:00
|
|
|
|
2014-04-02 18:54:22 -05:00
|
|
|
let mut constraints =
|
2014-05-22 18:57:53 -05:00
|
|
|
String::from_str(constraints.iter()
|
2014-05-25 05:17:19 -05:00
|
|
|
.map(|s| s.get().to_string())
|
2014-05-22 18:57:53 -05:00
|
|
|
.collect::<Vec<String>>()
|
2014-05-19 19:23:26 -05:00
|
|
|
.connect(",")
|
|
|
|
.as_slice());
|
2013-03-27 14:50:57 -05:00
|
|
|
|
2014-05-09 20:45:36 -05:00
|
|
|
let mut clobbers = getClobbers();
|
2014-01-15 20:30:40 -06:00
|
|
|
if !ia.clobbers.get().is_empty() && !clobbers.is_empty() {
|
2014-05-09 20:45:36 -05:00
|
|
|
clobbers = format_strbuf!("{},{}", ia.clobbers.get(), clobbers);
|
2013-03-27 17:37:28 -05:00
|
|
|
} else {
|
2014-01-15 20:30:40 -06:00
|
|
|
clobbers.push_str(ia.clobbers.get());
|
2013-10-17 13:24:41 -05:00
|
|
|
}
|
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-11 21:13:42 -05:00
|
|
|
if clobbers.len() != 0 && constraints.len() != 0 {
|
|
|
|
constraints.push_char(',');
|
2014-04-02 18:54:22 -05:00
|
|
|
constraints.push_str(clobbers.as_slice());
|
2013-03-27 14:50:57 -05:00
|
|
|
} else {
|
2014-04-02 18:54:22 -05:00
|
|
|
constraints.push_str(clobbers.as_slice());
|
2013-03-27 14:50:57 -05:00
|
|
|
}
|
|
|
|
|
2014-04-02 18:54:22 -05:00
|
|
|
debug!("Asm Constraints: {:?}", constraints.as_slice());
|
2013-03-27 14:50:57 -05:00
|
|
|
|
2014-02-15 15:15:03 -06:00
|
|
|
let num_outputs = outputs.len();
|
2013-03-27 14:50:57 -05:00
|
|
|
|
|
|
|
// Depending on how many outputs we have, the return type is different
|
2014-02-15 15:15:03 -06:00
|
|
|
let output_type = if num_outputs == 0 {
|
2014-03-15 15:29:34 -05:00
|
|
|
Type::void(bcx.ccx())
|
2014-02-15 15:15:03 -06:00
|
|
|
} else if num_outputs == 1 {
|
2014-03-08 14:36:22 -06:00
|
|
|
*output_types.get(0)
|
2013-03-27 14:50:57 -05:00
|
|
|
} else {
|
2014-03-15 15:29:34 -05:00
|
|
|
Type::struct_(bcx.ccx(), output_types.as_slice(), false)
|
2013-03-27 14:50:57 -05:00
|
|
|
};
|
|
|
|
|
2013-03-27 16:42:19 -05:00
|
|
|
let dialect = match ia.dialect {
|
2014-01-09 07:05:33 -06:00
|
|
|
ast::AsmAtt => lib::llvm::AD_ATT,
|
|
|
|
ast::AsmIntel => lib::llvm::AD_Intel
|
2013-03-27 16:42:19 -05:00
|
|
|
};
|
|
|
|
|
2014-01-15 20:30:40 -06:00
|
|
|
let r = ia.asm.get().with_c_str(|a| {
|
2014-04-02 18:54:22 -05:00
|
|
|
constraints.as_slice().with_c_str(|c| {
|
2014-02-28 17:25:15 -06:00
|
|
|
InlineAsmCall(bcx,
|
|
|
|
a,
|
|
|
|
c,
|
|
|
|
inputs.as_slice(),
|
|
|
|
output_type,
|
|
|
|
ia.volatile,
|
|
|
|
ia.alignstack,
|
|
|
|
dialect)
|
2013-11-21 17:42:55 -06:00
|
|
|
})
|
|
|
|
});
|
2013-03-27 14:50:57 -05:00
|
|
|
|
2013-03-28 18:34:10 -05:00
|
|
|
// Again, based on how many outputs we have
|
2014-02-15 15:15:03 -06:00
|
|
|
if num_outputs == 1 {
|
2014-02-28 17:25:15 -06:00
|
|
|
Store(bcx, r, *outputs.get(0));
|
2013-03-27 14:50:57 -05:00
|
|
|
} else {
|
2013-10-17 13:24:41 -05:00
|
|
|
for (i, o) in outputs.iter().enumerate() {
|
2013-03-27 14:50:57 -05:00
|
|
|
let v = ExtractValue(bcx, r, i);
|
2013-10-17 13:24:41 -05:00
|
|
|
Store(bcx, v, *o);
|
2013-03-27 14:50:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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")]
|
2014-05-22 18:57:53 -05:00
|
|
|
fn getClobbers() -> String {
|
2014-05-25 05:17:19 -05:00
|
|
|
"".to_string()
|
2013-03-27 17:37:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_arch = "x86")]
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
2014-05-22 18:57:53 -05:00
|
|
|
fn getClobbers() -> String {
|
2014-05-25 05:17:19 -05:00
|
|
|
"~{dirflag},~{fpsr},~{flags}".to_string()
|
2013-03-27 17:37:28 -05:00
|
|
|
}
|