2014-12-12 17:39:27 -06:00
|
|
|
// Copyright 2012-2015 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.
|
|
|
|
|
2014-11-25 20:17:11 -06:00
|
|
|
//! # Translation of inline assembly.
|
2013-03-27 14:50:57 -05:00
|
|
|
|
2014-07-07 19:58:01 -05:00
|
|
|
use llvm;
|
2014-11-15 19:30:33 -06:00
|
|
|
use trans::build::*;
|
|
|
|
use trans::callee;
|
|
|
|
use trans::common::*;
|
|
|
|
use trans::cleanup;
|
|
|
|
use trans::cleanup::CleanupMethods;
|
|
|
|
use trans::expr;
|
|
|
|
use trans::type_of;
|
|
|
|
use trans::type_::Type;
|
2013-06-16 05:52:44 -05:00
|
|
|
|
2013-03-27 14:50:57 -05:00
|
|
|
use syntax::ast;
|
2014-11-25 15:28:35 -06:00
|
|
|
use std::ffi::CString;
|
2014-09-27 03:33:36 -05:00
|
|
|
use libc::{c_uint, c_char};
|
2013-03-27 14:50:57 -05:00
|
|
|
|
|
|
|
// Take an inline assembly expression and splat it out via LLVM
|
2014-09-06 11:13:04 -05:00
|
|
|
pub fn trans_inline_asm<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, ia: &ast::InlineAsm)
|
|
|
|
-> Block<'blk, 'tcx> {
|
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();
|
|
|
|
|
2014-08-19 14:39:26 -05:00
|
|
|
let mut ext_inputs = Vec::new();
|
|
|
|
let mut ext_constraints = Vec::new();
|
|
|
|
|
2013-03-27 14:50:57 -05:00
|
|
|
// Prepare the output operands
|
2014-08-19 14:39:26 -05:00
|
|
|
let outputs = ia.outputs.iter().enumerate().map(|(i, &(ref c, ref out, is_rw))| {
|
2014-01-15 20:30:40 -06:00
|
|
|
constraints.push((*c).clone());
|
2013-03-27 14:50:57 -05:00
|
|
|
|
2014-05-16 12:15:33 -05:00
|
|
|
let out_datum = unpack_datum!(bcx, expr::trans(bcx, &**out));
|
2014-01-15 13:39:08 -06:00
|
|
|
output_types.push(type_of::type_of(bcx.ccx(), out_datum.ty));
|
2014-08-19 14:39:26 -05:00
|
|
|
let val = out_datum.val;
|
|
|
|
if is_rw {
|
|
|
|
ext_inputs.push(unpack_result!(bcx, {
|
|
|
|
callee::trans_arg_datum(bcx,
|
|
|
|
expr_ty(bcx, &**out),
|
|
|
|
out_datum,
|
|
|
|
cleanup::CustomScope(temp_scope),
|
|
|
|
callee::DontAutorefArg)
|
|
|
|
}));
|
|
|
|
ext_constraints.push(i.to_string());
|
|
|
|
}
|
|
|
|
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-10-15 01:05:01 -05:00
|
|
|
let mut inputs = ia.inputs.iter().map(|&(ref c, ref input)| {
|
2014-01-15 20:30:40 -06:00
|
|
|
constraints.push((*c).clone());
|
2013-03-27 14:50:57 -05:00
|
|
|
|
2014-05-16 12:15:33 -05: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,
|
2014-09-07 12:09:06 -05:00
|
|
|
expr_ty(bcx, &**input),
|
|
|
|
in_datum,
|
|
|
|
cleanup::CustomScope(temp_scope),
|
|
|
|
callee::DontAutorefArg)
|
2013-03-27 14:50:57 -05:00
|
|
|
})
|
2014-10-15 01:05:01 -05:00
|
|
|
}).collect::<Vec<_>>();
|
2015-02-18 13:48:57 -06:00
|
|
|
inputs.push_all(&ext_inputs[..]);
|
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
|
|
|
|
2015-03-18 00:23:13 -05:00
|
|
|
let clobbers = ia.clobbers.iter()
|
2015-03-18 08:43:01 -05:00
|
|
|
.map(|s| format!("~{{{}}}", &s));
|
2015-03-18 00:23:13 -05:00
|
|
|
|
|
|
|
// Default per-arch clobbers
|
|
|
|
// Basically what clang does
|
2015-03-23 17:54:39 -05:00
|
|
|
let arch_clobbers = match &bcx.sess().target.target.arch[..] {
|
2015-03-18 00:23:13 -05:00
|
|
|
"x86" | "x86_64" => vec!("~{dirflag}", "~{fpsr}", "~{flags}"),
|
|
|
|
_ => Vec::new()
|
|
|
|
};
|
2013-03-27 14:50:57 -05:00
|
|
|
|
2015-03-18 00:23:13 -05:00
|
|
|
let all_constraints= constraints.iter()
|
|
|
|
.map(|s| s.to_string())
|
|
|
|
.chain(ext_constraints.into_iter())
|
2015-03-18 08:43:01 -05:00
|
|
|
.chain(clobbers)
|
|
|
|
.chain(arch_clobbers.iter()
|
2015-03-18 00:23:13 -05:00
|
|
|
.map(|s| s.to_string()))
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.connect(",");
|
2013-03-27 14:50:57 -05:00
|
|
|
|
2015-03-18 00:23:13 -05:00
|
|
|
debug!("Asm Constraints: {}", &all_constraints[..]);
|
2013-03-27 14:50:57 -05:00
|
|
|
|
|
|
|
// Depending on how many outputs we have, the return type is different
|
2015-03-18 00:23:13 -05:00
|
|
|
let num_outputs = outputs.len();
|
|
|
|
let output_type = match num_outputs {
|
|
|
|
0 => Type::void(bcx.ccx()),
|
|
|
|
1 => output_types[0],
|
|
|
|
_ => Type::struct_(bcx.ccx(), &output_types[..], false)
|
2013-03-27 14:50:57 -05:00
|
|
|
};
|
|
|
|
|
2013-03-27 16:42:19 -05:00
|
|
|
let dialect = match ia.dialect {
|
2014-07-07 19:58:01 -05:00
|
|
|
ast::AsmAtt => llvm::AD_ATT,
|
|
|
|
ast::AsmIntel => llvm::AD_Intel
|
2013-03-27 16:42:19 -05:00
|
|
|
};
|
|
|
|
|
2015-02-18 00:47:40 -06:00
|
|
|
let asm = CString::new(ia.asm.as_bytes()).unwrap();
|
2015-03-18 00:23:13 -05:00
|
|
|
let constraint_cstr = CString::new(all_constraints).unwrap();
|
2014-11-25 15:28:35 -06:00
|
|
|
let r = InlineAsmCall(bcx,
|
|
|
|
asm.as_ptr(),
|
2015-03-18 00:23:13 -05:00
|
|
|
constraint_cstr.as_ptr(),
|
2015-02-01 20:53:25 -06:00
|
|
|
&inputs,
|
2014-02-28 17:25:15 -06:00
|
|
|
output_type,
|
|
|
|
ia.volatile,
|
|
|
|
ia.alignstack,
|
2014-11-25 15:28:35 -06:00
|
|
|
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
|
2014-02-15 15:15:03 -06:00
|
|
|
if num_outputs == 1 {
|
2014-10-15 01:05:01 -05:00
|
|
|
Store(bcx, r, outputs[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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-27 03:33:36 -05:00
|
|
|
// Store expn_id in a metadata node so we can map LLVM errors
|
|
|
|
// back to source locations. See #17552.
|
|
|
|
unsafe {
|
|
|
|
let key = "srcloc";
|
|
|
|
let kind = llvm::LLVMGetMDKindIDInContext(bcx.ccx().llcx(),
|
|
|
|
key.as_ptr() as *const c_char, key.len() as c_uint);
|
|
|
|
|
2015-04-20 19:51:10 -05:00
|
|
|
let val: llvm::ValueRef = C_i32(bcx.ccx(), ia.expn_id.into_u32() as i32);
|
2014-09-27 03:33:36 -05:00
|
|
|
|
|
|
|
llvm::LLVMSetMetadata(r, kind,
|
|
|
|
llvm::LLVMMDNodeInContext(bcx.ccx().llcx(), &val, 1));
|
|
|
|
}
|
|
|
|
|
2013-03-27 14:50:57 -05:00
|
|
|
return bcx;
|
|
|
|
|
|
|
|
}
|
2013-03-27 17:37:28 -05:00
|
|
|
|