]> git.lizzy.rs Git - rust.git/blob - src/allocator.rs
Put allocator shim in a different object file
[rust.git] / src / 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 crate::prelude::*;
12
13 use rustc::middle::allocator::AllocatorKind;
14 use rustc_allocator::{AllocatorTy, ALLOCATOR_METHODS};
15
16 /// Returns whether an allocator shim was created
17 pub fn codegen(sess: &Session, module: &mut Module<impl Backend + 'static>) -> bool {
18     let any_dynamic_crate = sess
19         .dependency_formats
20         .borrow()
21         .iter()
22         .any(|(_, list)| {
23             use rustc::middle::dependency_format::Linkage;
24             list.iter().any(|&linkage| linkage == Linkage::Dynamic)
25         });
26     if any_dynamic_crate {
27         false
28     } else if let Some(kind) = *sess.allocator_kind.get() {
29         codegen_inner(module, kind);
30         true
31     } else {
32         false
33     }
34 }
35
36 pub fn codegen_inner(module: &mut Module<impl Backend + 'static>, kind: AllocatorKind) {
37     let usize_ty = module.target_config().pointer_type();
38
39     for method in ALLOCATOR_METHODS {
40         let mut arg_tys = Vec::with_capacity(method.inputs.len());
41         for ty in method.inputs.iter() {
42             match *ty {
43                 AllocatorTy::Layout => {
44                     arg_tys.push(usize_ty); // size
45                     arg_tys.push(usize_ty); // align
46                 }
47                 AllocatorTy::Ptr => arg_tys.push(usize_ty),
48                 AllocatorTy::Usize => arg_tys.push(usize_ty),
49
50                 AllocatorTy::ResultPtr | AllocatorTy::Unit => panic!("invalid allocator arg"),
51             }
52         }
53         let output = match method.output {
54             AllocatorTy::ResultPtr => Some(usize_ty),
55             AllocatorTy::Unit => None,
56
57             AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
58                 panic!("invalid allocator output")
59             }
60         };
61
62         let sig = Signature {
63             call_conv: CallConv::SystemV,
64             params: arg_tys.iter().cloned().map(AbiParam::new).collect(),
65             returns: output.into_iter().map(AbiParam::new).collect(),
66         };
67
68         let caller_name = format!("__rust_{}", method.name);
69         let callee_name = kind.fn_name(method.name);
70         //eprintln!("Codegen allocator shim {} -> {} ({:?} -> {:?})", caller_name, callee_name, sig.params, sig.returns);
71
72         let func_id = module
73             .declare_function(&caller_name, Linkage::Export, &sig)
74             .unwrap();
75
76         let callee_func_id = module
77             .declare_function(&callee_name, Linkage::Import, &sig)
78             .unwrap();
79
80         let mut ctx = Context::new();
81         ctx.func = Function::with_name_signature(ExternalName::user(0, 0), sig.clone());
82         {
83             let mut func_ctx = FunctionBuilderContext::new();
84             let mut bcx = FunctionBuilder::new(&mut ctx.func, &mut func_ctx);
85
86             let ebb = bcx.create_ebb();
87             bcx.switch_to_block(ebb);
88             let args = arg_tys
89                 .into_iter()
90                 .map(|ty| bcx.append_ebb_param(ebb, ty))
91                 .collect::<Vec<Value>>();
92
93             let callee_func_ref = module.declare_func_in_func(callee_func_id, &mut bcx.func);
94
95             let call_inst = bcx.ins().call(callee_func_ref, &args);
96
97             let results = bcx.inst_results(call_inst).to_vec(); // Clone to prevent borrow error
98             bcx.ins().return_(&results);
99             bcx.seal_all_blocks();
100             bcx.finalize();
101         }
102         module.define_function(func_id, &mut ctx).unwrap();
103     }
104 }