]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_llvm/allocator.rs
Account for --remap-path-prefix in save-analysis
[rust.git] / src / librustc_codegen_llvm / allocator.rs
1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use std::ffi::CString;
12
13 use attributes;
14 use libc::c_uint;
15 use rustc::middle::allocator::AllocatorKind;
16 use rustc::ty::TyCtxt;
17 use rustc_allocator::{ALLOCATOR_METHODS, AllocatorTy};
18
19 use ModuleLlvm;
20 use llvm::{self, False, True};
21
22 pub(crate) unsafe fn codegen(tcx: TyCtxt, mods: &ModuleLlvm, kind: AllocatorKind) {
23     let llcx = &*mods.llcx;
24     let llmod = mods.llmod();
25     let usize = match &tcx.sess.target.target.target_pointer_width[..] {
26         "16" => llvm::LLVMInt16TypeInContext(llcx),
27         "32" => llvm::LLVMInt32TypeInContext(llcx),
28         "64" => llvm::LLVMInt64TypeInContext(llcx),
29         tws => bug!("Unsupported target word size for int: {}", tws),
30     };
31     let i8 = llvm::LLVMInt8TypeInContext(llcx);
32     let i8p = llvm::LLVMPointerType(i8, 0);
33     let void = llvm::LLVMVoidTypeInContext(llcx);
34
35     for method in ALLOCATOR_METHODS {
36         let mut args = Vec::new();
37         for ty in method.inputs.iter() {
38             match *ty {
39                 AllocatorTy::Layout => {
40                     args.push(usize); // size
41                     args.push(usize); // align
42                 }
43                 AllocatorTy::Ptr => args.push(i8p),
44                 AllocatorTy::Usize => args.push(usize),
45
46                 AllocatorTy::ResultPtr |
47                 AllocatorTy::Unit => panic!("invalid allocator arg"),
48             }
49         }
50         let output = match method.output {
51             AllocatorTy::ResultPtr => Some(i8p),
52             AllocatorTy::Unit => None,
53
54             AllocatorTy::Layout |
55             AllocatorTy::Usize |
56             AllocatorTy::Ptr => panic!("invalid allocator output"),
57         };
58         let ty = llvm::LLVMFunctionType(output.unwrap_or(void),
59                                         args.as_ptr(),
60                                         args.len() as c_uint,
61                                         False);
62         let name = CString::new(format!("__rust_{}", method.name)).unwrap();
63         let llfn = llvm::LLVMRustGetOrInsertFunction(llmod,
64                                                      name.as_ptr(),
65                                                      ty);
66
67         if tcx.sess.target.target.options.default_hidden_visibility {
68             llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
69         }
70        if tcx.sess.target.target.options.requires_uwtable {
71            attributes::emit_uwtable(llfn, true);
72        }
73
74         let callee = CString::new(kind.fn_name(method.name)).unwrap();
75         let callee = llvm::LLVMRustGetOrInsertFunction(llmod,
76                                                        callee.as_ptr(),
77                                                        ty);
78
79         let llbb = llvm::LLVMAppendBasicBlockInContext(llcx,
80                                                        llfn,
81                                                        "entry\0".as_ptr() as *const _);
82
83         let llbuilder = llvm::LLVMCreateBuilderInContext(llcx);
84         llvm::LLVMPositionBuilderAtEnd(llbuilder, llbb);
85         let args = args.iter().enumerate().map(|(i, _)| {
86             llvm::LLVMGetParam(llfn, i as c_uint)
87         }).collect::<Vec<_>>();
88         let ret = llvm::LLVMRustBuildCall(llbuilder,
89                                           callee,
90                                           args.as_ptr(),
91                                           args.len() as c_uint,
92                                           None,
93                                           "\0".as_ptr() as *const _);
94         llvm::LLVMSetTailCall(ret, True);
95         if output.is_some() {
96             llvm::LLVMBuildRet(llbuilder, ret);
97         } else {
98             llvm::LLVMBuildRetVoid(llbuilder);
99         }
100         llvm::LLVMDisposeBuilder(llbuilder);
101     }
102 }