]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_llvm/allocator.rs
Rollup merge of #67235 - jonas-schievink:vecdeque-leak, r=KodrAus
[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::ty::TyCtxt;
6 use syntax::expand::allocator::{AllocatorKind, AllocatorTy, ALLOCATOR_METHODS};
7 use rustc::bug;
8
9 use crate::ModuleLlvm;
10 use crate::llvm::{self, False, True};
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 |
37                 AllocatorTy::Unit => panic!("invalid allocator arg"),
38             }
39         }
40         let output = match method.output {
41             AllocatorTy::ResultPtr => Some(i8p),
42             AllocatorTy::Unit => None,
43
44             AllocatorTy::Layout |
45             AllocatorTy::Usize |
46             AllocatorTy::Ptr => panic!("invalid allocator output"),
47         };
48         let ty = llvm::LLVMFunctionType(output.unwrap_or(void),
49                                         args.as_ptr(),
50                                         args.len() as c_uint,
51                                         False);
52         let name = CString::new(format!("__rust_{}", method.name)).unwrap();
53         let llfn = llvm::LLVMRustGetOrInsertFunction(llmod,
54                                                      name.as_ptr(),
55                                                      ty);
56
57         if tcx.sess.target.target.options.default_hidden_visibility {
58             llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
59         }
60         if tcx.sess.target.target.options.requires_uwtable {
61             attributes::emit_uwtable(llfn, true);
62         }
63
64         let callee = CString::new(kind.fn_name(method.name)).unwrap();
65         let callee = llvm::LLVMRustGetOrInsertFunction(llmod,
66                                                        callee.as_ptr(),
67                                                        ty);
68         llvm::LLVMRustSetVisibility(callee, llvm::Visibility::Hidden);
69
70         let llbb = llvm::LLVMAppendBasicBlockInContext(llcx,
71                                                        llfn,
72                                                        "entry\0".as_ptr().cast());
73
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,
80                                           callee,
81                                           args.as_ptr(),
82                                           args.len() as c_uint,
83                                           None,
84                                           "\0".as_ptr().cast());
85         llvm::LLVMSetTailCall(ret, True);
86         if output.is_some() {
87             llvm::LLVMBuildRet(llbuilder, ret);
88         } else {
89             llvm::LLVMBuildRetVoid(llbuilder);
90         }
91         llvm::LLVMDisposeBuilder(llbuilder);
92     }
93 }