]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_llvm/allocator.rs
Rollup merge of #65136 - ehuss:update-codegen-options, r=Dylan-DPC
[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
8 use crate::ModuleLlvm;
9 use crate::llvm::{self, False, True};
10
11 pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut ModuleLlvm, kind: AllocatorKind) {
12     let llcx = &*mods.llcx;
13     let llmod = mods.llmod();
14     let usize = match &tcx.sess.target.target.target_pointer_width[..] {
15         "16" => llvm::LLVMInt16TypeInContext(llcx),
16         "32" => llvm::LLVMInt32TypeInContext(llcx),
17         "64" => llvm::LLVMInt64TypeInContext(llcx),
18         tws => bug!("Unsupported target word size for int: {}", tws),
19     };
20     let i8 = llvm::LLVMInt8TypeInContext(llcx);
21     let i8p = llvm::LLVMPointerType(i8, 0);
22     let void = llvm::LLVMVoidTypeInContext(llcx);
23
24     for method in ALLOCATOR_METHODS {
25         let mut args = Vec::with_capacity(method.inputs.len());
26         for ty in method.inputs.iter() {
27             match *ty {
28                 AllocatorTy::Layout => {
29                     args.push(usize); // size
30                     args.push(usize); // align
31                 }
32                 AllocatorTy::Ptr => args.push(i8p),
33                 AllocatorTy::Usize => args.push(usize),
34
35                 AllocatorTy::ResultPtr |
36                 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 |
44             AllocatorTy::Usize |
45             AllocatorTy::Ptr => panic!("invalid allocator output"),
46         };
47         let ty = llvm::LLVMFunctionType(output.unwrap_or(void),
48                                         args.as_ptr(),
49                                         args.len() as c_uint,
50                                         False);
51         let name = CString::new(format!("__rust_{}", method.name)).unwrap();
52         let llfn = llvm::LLVMRustGetOrInsertFunction(llmod,
53                                                      name.as_ptr(),
54                                                      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,
65                                                        callee.as_ptr(),
66                                                        ty);
67         llvm::LLVMRustSetVisibility(callee, llvm::Visibility::Hidden);
68
69         let llbb = llvm::LLVMAppendBasicBlockInContext(llcx,
70                                                        llfn,
71                                                        "entry\0".as_ptr().cast());
72
73         let llbuilder = llvm::LLVMCreateBuilderInContext(llcx);
74         llvm::LLVMPositionBuilderAtEnd(llbuilder, llbb);
75         let args = args.iter().enumerate().map(|(i, _)| {
76             llvm::LLVMGetParam(llfn, i as c_uint)
77         }).collect::<Vec<_>>();
78         let ret = llvm::LLVMRustBuildCall(llbuilder,
79                                           callee,
80                                           args.as_ptr(),
81                                           args.len() as c_uint,
82                                           None,
83                                           "\0".as_ptr().cast());
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 }