]> git.lizzy.rs Git - rust.git/blob - src/allocator.rs
Fix allocator shim abi
[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 pub fn codegen(module: &mut Module<impl Backend + 'static>, kind: AllocatorKind) {
17     let usize_ty = module.target_config().pointer_type();
18
19     for method in ALLOCATOR_METHODS {
20         let mut arg_tys = Vec::with_capacity(method.inputs.len());
21         for ty in method.inputs.iter() {
22             match *ty {
23                 AllocatorTy::Layout => {
24                     arg_tys.push(usize_ty); // size
25                     arg_tys.push(usize_ty); // align
26                 }
27                 AllocatorTy::Ptr => arg_tys.push(usize_ty),
28                 AllocatorTy::Usize => arg_tys.push(usize_ty),
29
30                 AllocatorTy::ResultPtr | AllocatorTy::Unit => panic!("invalid allocator arg"),
31             }
32         }
33         let output = match method.output {
34             AllocatorTy::ResultPtr => Some(usize_ty),
35             AllocatorTy::Unit => None,
36
37             AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
38                 panic!("invalid allocator output")
39             }
40         };
41
42         let mut sig = Signature {
43             call_conv: CallConv::Fast,
44             params: arg_tys.iter().cloned().map(AbiParam::new).collect(),
45             returns: output.into_iter().map(AbiParam::new).collect(),
46         };
47
48         let caller_name = format!("__rust_{}", method.name);
49         let callee_name = kind.fn_name(method.name);
50         //eprintln!("Codegen allocator shim {} -> {} ({:?} -> {:?})", caller_name, callee_name, sig.params, sig.returns);
51
52         sig.call_conv = CallConv::Fast; // "rust" abi
53         let func_id = module
54             .declare_function(&caller_name, Linkage::Export, &sig)
55             .unwrap();
56
57         sig.call_conv = CallConv::SystemV; // "C" abi
58         let callee_func_id = module
59             .declare_function(&callee_name, Linkage::Import, &sig)
60             .unwrap();
61
62         let mut ctx = Context::new();
63         ctx.func = Function::with_name_signature(ExternalName::user(0, 0), sig.clone());
64         {
65             let mut func_ctx = FunctionBuilderContext::new();
66             let mut bcx: FunctionBuilder = FunctionBuilder::new(&mut ctx.func, &mut func_ctx);
67
68             let ebb = bcx.create_ebb();
69             bcx.switch_to_block(ebb);
70             let args = arg_tys
71                 .into_iter()
72                 .map(|ty| bcx.append_ebb_param(ebb, ty))
73                 .collect::<Vec<Value>>();
74
75             let callee_func_ref = module.declare_func_in_func(callee_func_id, &mut bcx.func);
76
77             let call_inst = bcx.ins().call(callee_func_ref, &args);
78
79             let results = bcx.inst_results(call_inst).to_vec(); // Clone to prevent borrow error
80             bcx.ins().return_(&results);
81             bcx.seal_all_blocks();
82             bcx.finalize();
83         }
84         module.define_function(func_id, &mut ctx).unwrap();
85     }
86 }