2014-12-12 23:39:27 +00:00
|
|
|
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
|
2013-03-27 12:50:57 -07: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 21:17:11 -05:00
|
|
|
//! # Translation of inline assembly.
|
2013-03-27 12:50:57 -07:00
|
|
|
|
2016-03-06 13:23:20 +02:00
|
|
|
use llvm::{self, ValueRef};
|
2016-03-22 19:23:36 +02:00
|
|
|
use base;
|
|
|
|
use common::*;
|
|
|
|
use type_of;
|
|
|
|
use type_::Type;
|
2016-12-31 16:00:24 -07:00
|
|
|
use builder::Builder;
|
2013-06-16 22:52:44 +12:00
|
|
|
|
2016-08-16 17:41:38 +03:00
|
|
|
use rustc::hir;
|
|
|
|
use rustc::ty::Ty;
|
|
|
|
|
2014-11-25 13:28:35 -08:00
|
|
|
use std::ffi::CString;
|
2015-09-21 16:12:53 +02:00
|
|
|
use syntax::ast::AsmDialect;
|
2014-09-27 01:33:36 -07:00
|
|
|
use libc::{c_uint, c_char};
|
2013-03-27 12:50:57 -07:00
|
|
|
|
|
|
|
// Take an inline assembly expression and splat it out via LLVM
|
2016-12-17 19:54:32 -07:00
|
|
|
pub fn trans_inline_asm<'a, 'tcx>(
|
2016-12-31 16:00:24 -07:00
|
|
|
bcx: &Builder<'a, 'tcx>,
|
2016-12-17 19:54:32 -07:00
|
|
|
ia: &hir::InlineAsm,
|
|
|
|
outputs: Vec<(ValueRef, Ty<'tcx>)>,
|
|
|
|
mut inputs: Vec<ValueRef>
|
|
|
|
) {
|
2016-03-09 22:17:02 +02:00
|
|
|
let mut ext_constraints = vec![];
|
|
|
|
let mut output_types = vec![];
|
2014-08-19 20:39:26 +01:00
|
|
|
|
2013-03-27 12:50:57 -07:00
|
|
|
// Prepare the output operands
|
2016-03-09 22:17:02 +02:00
|
|
|
let mut indirect_outputs = vec![];
|
2016-08-16 17:41:38 +03:00
|
|
|
for (i, (out, &(val, ty))) in ia.outputs.iter().zip(&outputs).enumerate() {
|
2016-03-09 22:17:02 +02:00
|
|
|
let val = if out.is_rw || out.is_indirect {
|
2016-08-16 17:41:38 +03:00
|
|
|
Some(base::load_ty(bcx, val, ty))
|
2016-03-09 22:17:02 +02:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
if out.is_rw {
|
|
|
|
inputs.push(val.unwrap());
|
|
|
|
ext_constraints.push(i.to_string());
|
|
|
|
}
|
2015-12-05 08:18:24 +00:00
|
|
|
if out.is_indirect {
|
2016-03-09 22:17:02 +02:00
|
|
|
indirect_outputs.push(val.unwrap());
|
2015-11-06 13:31:02 +00:00
|
|
|
} else {
|
2016-12-19 16:25:00 -07:00
|
|
|
output_types.push(type_of::type_of(bcx.ccx, ty));
|
2014-08-19 20:39:26 +01:00
|
|
|
}
|
2015-11-06 13:31:02 +00:00
|
|
|
}
|
2016-03-09 22:17:02 +02:00
|
|
|
if !indirect_outputs.is_empty() {
|
|
|
|
indirect_outputs.extend_from_slice(&inputs);
|
|
|
|
inputs = indirect_outputs;
|
2015-06-18 21:16:47 +02:00
|
|
|
}
|
2013-03-27 12:50:57 -07:00
|
|
|
|
2015-03-17 23:23:13 -06:00
|
|
|
let clobbers = ia.clobbers.iter()
|
2015-03-18 07:43:01 -06:00
|
|
|
.map(|s| format!("~{{{}}}", &s));
|
2015-03-17 23:23:13 -06:00
|
|
|
|
|
|
|
// Default per-arch clobbers
|
|
|
|
// Basically what clang does
|
2017-01-01 08:46:34 -07:00
|
|
|
let arch_clobbers = match &bcx.sess().target.target.arch[..] {
|
2016-10-29 22:54:04 +01:00
|
|
|
"x86" | "x86_64" => vec!["~{dirflag}", "~{fpsr}", "~{flags}"],
|
2015-03-17 23:23:13 -06:00
|
|
|
_ => Vec::new()
|
|
|
|
};
|
2013-03-27 12:50:57 -07:00
|
|
|
|
2016-03-09 22:17:02 +02:00
|
|
|
let all_constraints =
|
|
|
|
ia.outputs.iter().map(|out| out.constraint.to_string())
|
|
|
|
.chain(ia.inputs.iter().map(|s| s.to_string()))
|
|
|
|
.chain(ext_constraints)
|
|
|
|
.chain(clobbers)
|
|
|
|
.chain(arch_clobbers.iter().map(|s| s.to_string()))
|
|
|
|
.collect::<Vec<String>>().join(",");
|
2013-03-27 12:50:57 -07:00
|
|
|
|
2015-03-17 23:23:13 -06:00
|
|
|
debug!("Asm Constraints: {}", &all_constraints[..]);
|
2013-03-27 12:50:57 -07:00
|
|
|
|
|
|
|
// Depending on how many outputs we have, the return type is different
|
2016-03-09 22:17:02 +02:00
|
|
|
let num_outputs = output_types.len();
|
2015-03-17 23:23:13 -06:00
|
|
|
let output_type = match num_outputs {
|
2016-12-19 16:25:00 -07:00
|
|
|
0 => Type::void(bcx.ccx),
|
2015-03-17 23:23:13 -06:00
|
|
|
1 => output_types[0],
|
2016-12-19 16:25:00 -07:00
|
|
|
_ => Type::struct_(bcx.ccx, &output_types[..], false)
|
2013-03-27 12:50:57 -07:00
|
|
|
};
|
|
|
|
|
2013-03-27 14:42:19 -07:00
|
|
|
let dialect = match ia.dialect {
|
2016-08-03 00:25:19 +03:00
|
|
|
AsmDialect::Att => llvm::AsmDialect::Att,
|
|
|
|
AsmDialect::Intel => llvm::AsmDialect::Intel,
|
2013-03-27 14:42:19 -07:00
|
|
|
};
|
|
|
|
|
2016-11-16 10:52:37 +00:00
|
|
|
let asm = CString::new(ia.asm.as_str().as_bytes()).unwrap();
|
2015-03-17 23:23:13 -06:00
|
|
|
let constraint_cstr = CString::new(all_constraints).unwrap();
|
2016-12-11 08:59:20 -07:00
|
|
|
let r = bcx.inline_asm_call(
|
|
|
|
asm.as_ptr(),
|
|
|
|
constraint_cstr.as_ptr(),
|
|
|
|
&inputs,
|
|
|
|
output_type,
|
|
|
|
ia.volatile,
|
|
|
|
ia.alignstack,
|
|
|
|
dialect
|
|
|
|
);
|
2013-03-27 12:50:57 -07:00
|
|
|
|
2013-03-28 16:34:10 -07:00
|
|
|
// Again, based on how many outputs we have
|
2016-03-09 22:17:02 +02:00
|
|
|
let outputs = ia.outputs.iter().zip(&outputs).filter(|&(ref o, _)| !o.is_indirect);
|
2016-08-16 17:41:38 +03:00
|
|
|
for (i, (_, &(val, _))) in outputs.enumerate() {
|
2016-12-11 08:59:20 -07:00
|
|
|
let v = if num_outputs == 1 { r } else { bcx.extract_value(r, i) };
|
2016-12-29 02:20:26 +01:00
|
|
|
bcx.store(v, val, None);
|
2013-03-27 12:50:57 -07:00
|
|
|
}
|
|
|
|
|
2014-09-27 01:33:36 -07:00
|
|
|
// Store expn_id in a metadata node so we can map LLVM errors
|
|
|
|
// back to source locations. See #17552.
|
|
|
|
unsafe {
|
|
|
|
let key = "srcloc";
|
2016-12-19 16:25:00 -07:00
|
|
|
let kind = llvm::LLVMGetMDKindIDInContext(bcx.ccx.llcx(),
|
2014-09-27 01:33:36 -07:00
|
|
|
key.as_ptr() as *const c_char, key.len() as c_uint);
|
|
|
|
|
2016-12-19 16:25:00 -07:00
|
|
|
let val: llvm::ValueRef = C_i32(bcx.ccx, ia.expn_id.into_u32() as i32);
|
2014-09-27 01:33:36 -07:00
|
|
|
|
|
|
|
llvm::LLVMSetMetadata(r, kind,
|
2016-12-19 16:25:00 -07:00
|
|
|
llvm::LLVMMDNodeInContext(bcx.ccx.llcx(), &val, 1));
|
2014-09-27 01:33:36 -07:00
|
|
|
}
|
2013-03-27 12:50:57 -07:00
|
|
|
}
|