5 use rustc::middle::allocator::AllocatorKind;
7 use rustc_allocator::{ALLOCATOR_METHODS, AllocatorTy};
10 use crate::llvm::{self, False, True};
12 pub(crate) unsafe fn codegen(tcx: TyCtxt<'_, '_, '_>, mods: &mut ModuleLlvm, kind: AllocatorKind) {
13 let llcx = &*mods.llcx;
14 let llmod = mods.llmod();
15 let usize = match &tcx.sess.target.target.target_pointer_width[..] {
16 "16" => llvm::LLVMInt16TypeInContext(llcx),
17 "32" => llvm::LLVMInt32TypeInContext(llcx),
18 "64" => llvm::LLVMInt64TypeInContext(llcx),
19 tws => bug!("Unsupported target word size for int: {}", tws),
21 let i8 = llvm::LLVMInt8TypeInContext(llcx);
22 let i8p = llvm::LLVMPointerType(i8, 0);
23 let void = llvm::LLVMVoidTypeInContext(llcx);
25 for method in ALLOCATOR_METHODS {
26 let mut args = Vec::with_capacity(method.inputs.len());
27 for ty in method.inputs.iter() {
29 AllocatorTy::Layout => {
30 args.push(usize); // size
31 args.push(usize); // align
33 AllocatorTy::Ptr => args.push(i8p),
34 AllocatorTy::Usize => args.push(usize),
36 AllocatorTy::ResultPtr |
37 AllocatorTy::Unit => panic!("invalid allocator arg"),
40 let output = match method.output {
41 AllocatorTy::ResultPtr => Some(i8p),
42 AllocatorTy::Unit => None,
46 AllocatorTy::Ptr => panic!("invalid allocator output"),
48 let ty = llvm::LLVMFunctionType(output.unwrap_or(void),
52 let name = CString::new(format!("__rust_{}", method.name)).unwrap();
53 let llfn = llvm::LLVMRustGetOrInsertFunction(llmod,
57 if tcx.sess.target.target.options.default_hidden_visibility {
58 llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
60 if tcx.sess.target.target.options.requires_uwtable {
61 attributes::emit_uwtable(llfn, true);
64 let callee = CString::new(kind.fn_name(method.name)).unwrap();
65 let callee = llvm::LLVMRustGetOrInsertFunction(llmod,
68 llvm::LLVMRustSetVisibility(callee, llvm::Visibility::Hidden);
70 let llbb = llvm::LLVMAppendBasicBlockInContext(llcx,
72 "entry\0".as_ptr() as *const _);
74 let llbuilder = llvm::LLVMCreateBuilderInContext(llcx);
75 llvm::LLVMPositionBuilderAtEnd(llbuilder, llbb);
76 let args = args.iter().enumerate().map(|(i, _)| {
77 llvm::LLVMGetParam(llfn, i as c_uint)
78 }).collect::<Vec<_>>();
79 let ret = llvm::LLVMRustBuildCall(llbuilder,
84 "\0".as_ptr() as *const _);
85 llvm::LLVMSetTailCall(ret, True);
87 llvm::LLVMBuildRet(llbuilder, ret);
89 llvm::LLVMBuildRetVoid(llbuilder);
91 llvm::LLVMDisposeBuilder(llbuilder);