2018-11-05 11:29:15 -06:00
|
|
|
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
use crate::prelude::*;
|
|
|
|
|
2018-11-07 06:29:38 -06:00
|
|
|
use rustc::middle::allocator::AllocatorKind;
|
2018-11-07 06:32:02 -06:00
|
|
|
use rustc_allocator::{AllocatorTy, ALLOCATOR_METHODS};
|
2018-11-05 11:29:15 -06:00
|
|
|
|
2018-12-13 08:16:46 -06:00
|
|
|
/// Returns whether an allocator shim was created
|
|
|
|
pub fn codegen(sess: &Session, module: &mut Module<impl Backend + 'static>) -> bool {
|
2018-12-12 09:01:34 -06:00
|
|
|
let any_dynamic_crate = sess
|
|
|
|
.dependency_formats
|
|
|
|
.borrow()
|
|
|
|
.iter()
|
|
|
|
.any(|(_, list)| {
|
|
|
|
use rustc::middle::dependency_format::Linkage;
|
|
|
|
list.iter().any(|&linkage| linkage == Linkage::Dynamic)
|
|
|
|
});
|
|
|
|
if any_dynamic_crate {
|
2018-12-13 08:16:46 -06:00
|
|
|
false
|
2018-12-12 09:01:34 -06:00
|
|
|
} else if let Some(kind) = *sess.allocator_kind.get() {
|
|
|
|
codegen_inner(module, kind);
|
2018-12-13 08:16:46 -06:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
2018-12-12 09:01:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn codegen_inner(module: &mut Module<impl Backend + 'static>, kind: AllocatorKind) {
|
2018-11-05 11:29:15 -06:00
|
|
|
let usize_ty = module.target_config().pointer_type();
|
|
|
|
|
|
|
|
for method in ALLOCATOR_METHODS {
|
|
|
|
let mut arg_tys = Vec::with_capacity(method.inputs.len());
|
|
|
|
for ty in method.inputs.iter() {
|
|
|
|
match *ty {
|
|
|
|
AllocatorTy::Layout => {
|
|
|
|
arg_tys.push(usize_ty); // size
|
|
|
|
arg_tys.push(usize_ty); // align
|
|
|
|
}
|
|
|
|
AllocatorTy::Ptr => arg_tys.push(usize_ty),
|
|
|
|
AllocatorTy::Usize => arg_tys.push(usize_ty),
|
|
|
|
|
2018-11-07 06:32:02 -06:00
|
|
|
AllocatorTy::ResultPtr | AllocatorTy::Unit => panic!("invalid allocator arg"),
|
2018-11-05 11:29:15 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
let output = match method.output {
|
|
|
|
AllocatorTy::ResultPtr => Some(usize_ty),
|
|
|
|
AllocatorTy::Unit => None,
|
|
|
|
|
2018-11-07 06:32:02 -06:00
|
|
|
AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
|
|
|
|
panic!("invalid allocator output")
|
|
|
|
}
|
2018-11-05 11:29:15 -06:00
|
|
|
};
|
|
|
|
|
2018-12-11 08:13:45 -06:00
|
|
|
let sig = Signature {
|
|
|
|
call_conv: CallConv::SystemV,
|
2018-11-05 11:29:15 -06:00
|
|
|
params: arg_tys.iter().cloned().map(AbiParam::new).collect(),
|
|
|
|
returns: output.into_iter().map(AbiParam::new).collect(),
|
|
|
|
};
|
|
|
|
|
2018-11-14 08:04:20 -06:00
|
|
|
let caller_name = format!("__rust_{}", method.name);
|
|
|
|
let callee_name = kind.fn_name(method.name);
|
|
|
|
//eprintln!("Codegen allocator shim {} -> {} ({:?} -> {:?})", caller_name, callee_name, sig.params, sig.returns);
|
|
|
|
|
2018-11-05 11:29:15 -06:00
|
|
|
let func_id = module
|
2018-11-14 08:04:20 -06:00
|
|
|
.declare_function(&caller_name, Linkage::Export, &sig)
|
2018-11-05 11:29:15 -06:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let callee_func_id = module
|
2018-11-14 08:04:20 -06:00
|
|
|
.declare_function(&callee_name, Linkage::Import, &sig)
|
2018-11-05 11:29:15 -06:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mut ctx = Context::new();
|
|
|
|
ctx.func = Function::with_name_signature(ExternalName::user(0, 0), sig.clone());
|
|
|
|
{
|
|
|
|
let mut func_ctx = FunctionBuilderContext::new();
|
2018-11-17 11:23:52 -06:00
|
|
|
let mut bcx = FunctionBuilder::new(&mut ctx.func, &mut func_ctx);
|
2018-11-05 11:29:15 -06:00
|
|
|
|
|
|
|
let ebb = bcx.create_ebb();
|
|
|
|
bcx.switch_to_block(ebb);
|
|
|
|
let args = arg_tys
|
|
|
|
.into_iter()
|
|
|
|
.map(|ty| bcx.append_ebb_param(ebb, ty))
|
|
|
|
.collect::<Vec<Value>>();
|
|
|
|
|
|
|
|
let callee_func_ref = module.declare_func_in_func(callee_func_id, &mut bcx.func);
|
|
|
|
|
|
|
|
let call_inst = bcx.ins().call(callee_func_ref, &args);
|
|
|
|
|
|
|
|
let results = bcx.inst_results(call_inst).to_vec(); // Clone to prevent borrow error
|
|
|
|
bcx.ins().return_(&results);
|
|
|
|
bcx.seal_all_blocks();
|
|
|
|
bcx.finalize();
|
|
|
|
}
|
|
|
|
module.define_function(func_id, &mut ctx).unwrap();
|
|
|
|
}
|
|
|
|
}
|