2019-02-18 03:58:58 +09:00
|
|
|
use crate::llvm;
|
|
|
|
use crate::context::CodegenCx;
|
|
|
|
use crate::type_of::LayoutLlvmExt;
|
|
|
|
use crate::builder::Builder;
|
|
|
|
use crate::value::Value;
|
2013-06-16 22:52:44 +12:00
|
|
|
|
2016-08-16 17:41:38 +03:00
|
|
|
use rustc::hir;
|
2018-11-16 13:45:28 +02:00
|
|
|
use rustc_codegen_ssa::traits::*;
|
2016-08-16 17:41:38 +03:00
|
|
|
|
2018-10-03 16:56:24 +02:00
|
|
|
use rustc_codegen_ssa::mir::place::PlaceRef;
|
|
|
|
use rustc_codegen_ssa::mir::operand::OperandValue;
|
2017-02-06 17:27:09 +01:00
|
|
|
|
2014-11-25 13:28:35 -08:00
|
|
|
use std::ffi::CString;
|
2014-09-27 01:33:36 -07:00
|
|
|
use libc::{c_uint, c_char};
|
2013-03-27 12:50:57 -07:00
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
|
|
|
|
impl AsmBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
|
|
|
|
fn codegen_inline_asm(
|
2018-10-05 15:08:49 +02:00
|
|
|
&mut self,
|
2018-09-20 15:47:22 +02:00
|
|
|
ia: &hir::InlineAsm,
|
|
|
|
outputs: Vec<PlaceRef<'tcx, &'ll Value>>,
|
|
|
|
mut inputs: Vec<&'ll Value>
|
|
|
|
) -> bool {
|
|
|
|
let mut ext_constraints = vec![];
|
|
|
|
let mut output_types = vec![];
|
|
|
|
|
|
|
|
// Prepare the output operands
|
|
|
|
let mut indirect_outputs = vec![];
|
|
|
|
for (i, (out, &place)) in ia.outputs.iter().zip(&outputs).enumerate() {
|
|
|
|
if out.is_rw {
|
|
|
|
inputs.push(self.load_operand(place).immediate());
|
|
|
|
ext_constraints.push(i.to_string());
|
|
|
|
}
|
|
|
|
if out.is_indirect {
|
|
|
|
indirect_outputs.push(self.load_operand(place).immediate());
|
|
|
|
} else {
|
|
|
|
output_types.push(place.layout.llvm_type(self.cx()));
|
|
|
|
}
|
2016-03-09 22:17:02 +02:00
|
|
|
}
|
2018-09-20 15:47:22 +02:00
|
|
|
if !indirect_outputs.is_empty() {
|
|
|
|
indirect_outputs.extend_from_slice(&inputs);
|
|
|
|
inputs = indirect_outputs;
|
2014-08-19 20:39:26 +01:00
|
|
|
}
|
2013-03-27 12:50:57 -07:00
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
let clobbers = ia.clobbers.iter()
|
|
|
|
.map(|s| format!("~{{{}}}", &s));
|
|
|
|
|
|
|
|
// Default per-arch clobbers
|
|
|
|
// Basically what clang does
|
2018-11-27 19:00:25 +01:00
|
|
|
let arch_clobbers = match &self.sess().target.target.arch[..] {
|
2018-09-20 15:47:22 +02:00
|
|
|
"x86" | "x86_64" => vec!["~{dirflag}", "~{fpsr}", "~{flags}"],
|
|
|
|
"mips" | "mips64" => vec!["~{$1}"],
|
|
|
|
_ => Vec::new()
|
|
|
|
};
|
|
|
|
|
|
|
|
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(",");
|
|
|
|
|
|
|
|
debug!("Asm Constraints: {}", &all_constraints);
|
|
|
|
|
|
|
|
// Depending on how many outputs we have, the return type is different
|
|
|
|
let num_outputs = output_types.len();
|
|
|
|
let output_type = match num_outputs {
|
2018-11-27 19:00:25 +01:00
|
|
|
0 => self.type_void(),
|
2018-09-20 15:47:22 +02:00
|
|
|
1 => output_types[0],
|
2018-11-27 19:00:25 +01:00
|
|
|
_ => self.type_struct(&output_types, false)
|
2018-09-20 15:47:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let asm = CString::new(ia.asm.as_str().as_bytes()).unwrap();
|
|
|
|
let constraint_cstr = CString::new(all_constraints).unwrap();
|
|
|
|
let r = self.inline_asm_call(
|
2018-11-16 13:33:28 +02:00
|
|
|
&asm,
|
|
|
|
&constraint_cstr,
|
2018-09-20 15:47:22 +02:00
|
|
|
&inputs,
|
|
|
|
output_type,
|
|
|
|
ia.volatile,
|
|
|
|
ia.alignstack,
|
|
|
|
ia.dialect
|
|
|
|
);
|
|
|
|
if r.is_none() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
let r = r.unwrap();
|
2013-03-27 12:50:57 -07:00
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
// Again, based on how many outputs we have
|
|
|
|
let outputs = ia.outputs.iter().zip(&outputs).filter(|&(ref o, _)| !o.is_indirect);
|
|
|
|
for (i, (_, &place)) in outputs.enumerate() {
|
|
|
|
let v = if num_outputs == 1 { r } else { self.extract_value(r, i as u64) };
|
|
|
|
OperandValue::Immediate(v).store(self, place);
|
|
|
|
}
|
2013-03-27 12:50:57 -07:00
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
// Store mark in a metadata node so we can map LLVM errors
|
|
|
|
// back to source locations. See #17552.
|
|
|
|
unsafe {
|
|
|
|
let key = "srcloc";
|
2018-11-27 19:00:25 +01:00
|
|
|
let kind = llvm::LLVMGetMDKindIDInContext(self.llcx,
|
2018-09-20 15:47:22 +02:00
|
|
|
key.as_ptr() as *const c_char, key.len() as c_uint);
|
2014-09-27 01:33:36 -07:00
|
|
|
|
2018-11-27 19:00:25 +01:00
|
|
|
let val: &'ll Value = self.const_i32(ia.ctxt.outer().as_u32() as i32);
|
2014-09-27 01:33:36 -07:00
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
llvm::LLVMSetMetadata(r, kind,
|
2018-11-27 19:00:25 +01:00
|
|
|
llvm::LLVMMDNodeInContext(self.llcx, &val, 1));
|
2018-09-20 15:47:22 +02:00
|
|
|
}
|
2018-09-25 20:35:19 +02:00
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
true
|
|
|
|
}
|
2013-03-27 12:50:57 -07:00
|
|
|
}
|
2017-03-21 10:03:52 -05:00
|
|
|
|
2018-09-20 15:47:22 +02:00
|
|
|
impl AsmMethods<'tcx> for CodegenCx<'ll, 'tcx> {
|
|
|
|
fn codegen_global_asm(&self, ga: &hir::GlobalAsm) {
|
|
|
|
let asm = CString::new(ga.asm.as_str().as_bytes()).unwrap();
|
|
|
|
unsafe {
|
|
|
|
llvm::LLVMRustAppendModuleInlineAsm(self.llmod, asm.as_ptr());
|
|
|
|
}
|
2017-03-21 10:03:52 -05:00
|
|
|
}
|
|
|
|
}
|