]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/allocator.rs
rustc: Implement the #[global_allocator] attribute
[rust.git] / src / librustc_trans / 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 use std::ptr;
13
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 unsafe fn trans(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 usizep = llvm::LLVMPointerType(usize, 0);
34     let void = llvm::LLVMVoidTypeInContext(llcx);
35
36     for method in ALLOCATOR_METHODS {
37         let mut args = Vec::new();
38         for ty in method.inputs.iter() {
39             match *ty {
40                 AllocatorTy::Layout => {
41                     args.push(usize); // size
42                     args.push(usize); // align
43                 }
44                 AllocatorTy::LayoutRef => args.push(i8p),
45                 AllocatorTy::Ptr => args.push(i8p),
46                 AllocatorTy::AllocErr => args.push(i8p),
47
48                 AllocatorTy::Bang |
49                 AllocatorTy::ResultExcess |
50                 AllocatorTy::ResultPtr |
51                 AllocatorTy::ResultUnit |
52                 AllocatorTy::UsizePair |
53                 AllocatorTy::Unit => panic!("invalid allocator arg"),
54             }
55         }
56         let output = match method.output {
57             AllocatorTy::UsizePair => {
58                 args.push(usizep); // min
59                 args.push(usizep); // max
60                 None
61             }
62             AllocatorTy::Bang => None,
63             AllocatorTy::ResultExcess => {
64                 args.push(i8p); // excess_ptr
65                 args.push(i8p); // err_ptr
66                 Some(i8p)
67             }
68             AllocatorTy::ResultPtr => {
69                 args.push(i8p); // err_ptr
70                 Some(i8p)
71             }
72             AllocatorTy::ResultUnit => Some(i8),
73             AllocatorTy::Unit => None,
74
75             AllocatorTy::AllocErr |
76             AllocatorTy::Layout |
77             AllocatorTy::LayoutRef |
78             AllocatorTy::Ptr => panic!("invalid allocator output"),
79         };
80         let ty = llvm::LLVMFunctionType(output.unwrap_or(void),
81                                         args.as_ptr(),
82                                         args.len() as c_uint,
83                                         False);
84         let name = CString::new(format!("__rust_{}", method.name)).unwrap();
85         let llfn = llvm::LLVMRustGetOrInsertFunction(llmod,
86                                                      name.as_ptr(),
87                                                      ty);
88
89         let callee = CString::new(kind.fn_name(method.name)).unwrap();
90         let callee = llvm::LLVMRustGetOrInsertFunction(llmod,
91                                                        callee.as_ptr(),
92                                                        ty);
93
94         let llbb = llvm::LLVMAppendBasicBlockInContext(llcx,
95                                                        llfn,
96                                                        "entry\0".as_ptr() as *const _);
97
98         let llbuilder = llvm::LLVMCreateBuilderInContext(llcx);
99         llvm::LLVMPositionBuilderAtEnd(llbuilder, llbb);
100         let args = args.iter().enumerate().map(|(i, _)| {
101             llvm::LLVMGetParam(llfn, i as c_uint)
102         }).collect::<Vec<_>>();
103         let ret = llvm::LLVMRustBuildCall(llbuilder,
104                                           callee,
105                                           args.as_ptr(),
106                                           args.len() as c_uint,
107                                           ptr::null_mut(),
108                                           "\0".as_ptr() as *const _);
109         llvm::LLVMSetTailCall(ret, True);
110         if output.is_some() {
111             llvm::LLVMBuildRet(llbuilder, ret);
112         } else {
113             llvm::LLVMBuildRetVoid(llbuilder);
114         }
115         llvm::LLVMDisposeBuilder(llbuilder);
116     }
117 }