2019-02-18 03:58:58 +09:00
|
|
|
use crate::attributes;
|
2017-06-03 14:54:08 -07:00
|
|
|
use libc::c_uint;
|
2019-12-22 17:42:04 -05:00
|
|
|
use rustc::bug;
|
2017-06-03 14:54:08 -07:00
|
|
|
use rustc::ty::TyCtxt;
|
2020-02-29 20:37:32 +03:00
|
|
|
use rustc_ast::expand::allocator::{AllocatorKind, AllocatorTy, ALLOCATOR_METHODS};
|
2017-06-03 14:54:08 -07:00
|
|
|
|
2019-02-18 03:58:58 +09:00
|
|
|
use crate::llvm::{self, False, True};
|
2019-12-22 17:42:04 -05:00
|
|
|
use crate::ModuleLlvm;
|
2017-06-03 14:54:08 -07:00
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut ModuleLlvm, kind: AllocatorKind) {
|
2018-06-27 17:57:25 +03:00
|
|
|
let llcx = &*mods.llcx;
|
|
|
|
let llmod = mods.llmod();
|
2017-06-03 14:54:08 -07:00
|
|
|
let usize = match &tcx.sess.target.target.target_pointer_width[..] {
|
|
|
|
"16" => llvm::LLVMInt16TypeInContext(llcx),
|
|
|
|
"32" => llvm::LLVMInt32TypeInContext(llcx),
|
|
|
|
"64" => llvm::LLVMInt64TypeInContext(llcx),
|
|
|
|
tws => bug!("Unsupported target word size for int: {}", tws),
|
|
|
|
};
|
|
|
|
let i8 = llvm::LLVMInt8TypeInContext(llcx);
|
|
|
|
let i8p = llvm::LLVMPointerType(i8, 0);
|
|
|
|
let void = llvm::LLVMVoidTypeInContext(llcx);
|
|
|
|
|
|
|
|
for method in ALLOCATOR_METHODS {
|
2018-10-08 16:55:04 +02:00
|
|
|
let mut args = Vec::with_capacity(method.inputs.len());
|
2017-06-03 14:54:08 -07:00
|
|
|
for ty in method.inputs.iter() {
|
|
|
|
match *ty {
|
|
|
|
AllocatorTy::Layout => {
|
|
|
|
args.push(usize); // size
|
|
|
|
args.push(usize); // align
|
|
|
|
}
|
|
|
|
AllocatorTy::Ptr => args.push(i8p),
|
2018-04-03 17:12:57 +02:00
|
|
|
AllocatorTy::Usize => args.push(usize),
|
2017-06-03 14:54:08 -07:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
AllocatorTy::ResultPtr | AllocatorTy::Unit => panic!("invalid allocator arg"),
|
2017-06-03 14:54:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
let output = match method.output {
|
2018-04-03 17:12:57 +02:00
|
|
|
AllocatorTy::ResultPtr => Some(i8p),
|
2017-06-03 14:54:08 -07:00
|
|
|
AllocatorTy::Unit => None,
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
|
|
|
|
panic!("invalid allocator output")
|
|
|
|
}
|
2017-06-03 14:54:08 -07:00
|
|
|
};
|
2019-12-22 17:42:04 -05:00
|
|
|
let ty = llvm::LLVMFunctionType(
|
|
|
|
output.unwrap_or(void),
|
|
|
|
args.as_ptr(),
|
|
|
|
args.len() as c_uint,
|
|
|
|
False,
|
|
|
|
);
|
2020-03-10 00:00:00 +00:00
|
|
|
let name = format!("__rust_{}", method.name);
|
|
|
|
let llfn = llvm::LLVMRustGetOrInsertFunction(llmod, name.as_ptr().cast(), name.len(), ty);
|
2017-06-03 14:54:08 -07:00
|
|
|
|
2018-01-30 11:54:07 -08:00
|
|
|
if tcx.sess.target.target.options.default_hidden_visibility {
|
|
|
|
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
|
|
|
|
}
|
2018-08-02 12:30:43 -07:00
|
|
|
if tcx.sess.target.target.options.requires_uwtable {
|
|
|
|
attributes::emit_uwtable(llfn, true);
|
|
|
|
}
|
2018-01-30 11:54:07 -08:00
|
|
|
|
2020-03-10 00:00:00 +00:00
|
|
|
let callee = kind.fn_name(method.name);
|
|
|
|
let callee =
|
|
|
|
llvm::LLVMRustGetOrInsertFunction(llmod, callee.as_ptr().cast(), callee.len(), ty);
|
2018-08-02 12:30:43 -07:00
|
|
|
llvm::LLVMRustSetVisibility(callee, llvm::Visibility::Hidden);
|
2017-06-03 14:54:08 -07:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
let llbb = llvm::LLVMAppendBasicBlockInContext(llcx, llfn, "entry\0".as_ptr().cast());
|
2017-06-03 14:54:08 -07:00
|
|
|
|
|
|
|
let llbuilder = llvm::LLVMCreateBuilderInContext(llcx);
|
|
|
|
llvm::LLVMPositionBuilderAtEnd(llbuilder, llbb);
|
2019-12-22 17:42:04 -05:00
|
|
|
let args = args
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(i, _)| llvm::LLVMGetParam(llfn, i as c_uint))
|
|
|
|
.collect::<Vec<_>>();
|
2020-03-10 00:00:00 +00:00
|
|
|
let ret =
|
|
|
|
llvm::LLVMRustBuildCall(llbuilder, callee, args.as_ptr(), args.len() as c_uint, None);
|
2017-06-03 14:54:08 -07:00
|
|
|
llvm::LLVMSetTailCall(ret, True);
|
|
|
|
if output.is_some() {
|
|
|
|
llvm::LLVMBuildRet(llbuilder, ret);
|
|
|
|
} else {
|
|
|
|
llvm::LLVMBuildRetVoid(llbuilder);
|
|
|
|
}
|
|
|
|
llvm::LLVMDisposeBuilder(llbuilder);
|
|
|
|
}
|
|
|
|
}
|