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