]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_llvm/allocator.rs
Rollup merge of #67233 - Luro02:cursor_traits, r=sfackler
[rust.git] / src / librustc_codegen_llvm / allocator.rs
1 use std::ffi::CString;
2
3 use crate::attributes;
4 use libc::c_uint;
5 use rustc::bug;
6 use rustc::ty::TyCtxt;
7 use syntax::expand::allocator::{AllocatorKind, AllocatorTy, ALLOCATOR_METHODS};
8
9 use crate::llvm::{self, False, True};
10 use crate::ModuleLlvm;
11
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),
20     };
21     let i8 = llvm::LLVMInt8TypeInContext(llcx);
22     let i8p = llvm::LLVMPointerType(i8, 0);
23     let void = llvm::LLVMVoidTypeInContext(llcx);
24
25     for method in ALLOCATOR_METHODS {
26         let mut args = Vec::with_capacity(method.inputs.len());
27         for ty in method.inputs.iter() {
28             match *ty {
29                 AllocatorTy::Layout => {
30                     args.push(usize); // size
31                     args.push(usize); // align
32                 }
33                 AllocatorTy::Ptr => args.push(i8p),
34                 AllocatorTy::Usize => args.push(usize),
35
36                 AllocatorTy::ResultPtr | AllocatorTy::Unit => panic!("invalid allocator arg"),
37             }
38         }
39         let output = match method.output {
40             AllocatorTy::ResultPtr => Some(i8p),
41             AllocatorTy::Unit => None,
42
43             AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
44                 panic!("invalid allocator output")
45             }
46         };
47         let ty = llvm::LLVMFunctionType(
48             output.unwrap_or(void),
49             args.as_ptr(),
50             args.len() as c_uint,
51             False,
52         );
53         let name = CString::new(format!("__rust_{}", method.name)).unwrap();
54         let llfn = llvm::LLVMRustGetOrInsertFunction(llmod, name.as_ptr(), ty);
55
56         if tcx.sess.target.target.options.default_hidden_visibility {
57             llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
58         }
59         if tcx.sess.target.target.options.requires_uwtable {
60             attributes::emit_uwtable(llfn, true);
61         }
62
63         let callee = CString::new(kind.fn_name(method.name)).unwrap();
64         let callee = llvm::LLVMRustGetOrInsertFunction(llmod, callee.as_ptr(), ty);
65         llvm::LLVMRustSetVisibility(callee, llvm::Visibility::Hidden);
66
67         let llbb = llvm::LLVMAppendBasicBlockInContext(llcx, llfn, "entry\0".as_ptr().cast());
68
69         let llbuilder = llvm::LLVMCreateBuilderInContext(llcx);
70         llvm::LLVMPositionBuilderAtEnd(llbuilder, llbb);
71         let args = args
72             .iter()
73             .enumerate()
74             .map(|(i, _)| llvm::LLVMGetParam(llfn, i as c_uint))
75             .collect::<Vec<_>>();
76         let ret = llvm::LLVMRustBuildCall(
77             llbuilder,
78             callee,
79             args.as_ptr(),
80             args.len() as c_uint,
81             None,
82             "\0".as_ptr().cast(),
83         );
84         llvm::LLVMSetTailCall(ret, True);
85         if output.is_some() {
86             llvm::LLVMBuildRet(llbuilder, ret);
87         } else {
88             llvm::LLVMBuildRetVoid(llbuilder);
89         }
90         llvm::LLVMDisposeBuilder(llbuilder);
91     }
92 }