2020-09-23 08:13:49 -05:00
|
|
|
//! Allocator shim
|
|
|
|
// Adapted from rustc
|
2018-11-05 11:29:15 -06:00
|
|
|
|
|
|
|
use crate::prelude::*;
|
|
|
|
|
2020-03-04 08:04:28 -06:00
|
|
|
use rustc_ast::expand::allocator::{AllocatorKind, AllocatorTy, ALLOCATOR_METHODS};
|
2020-10-05 04:12:41 -05:00
|
|
|
use rustc_span::symbol::sym;
|
2018-11-05 11:29:15 -06:00
|
|
|
|
2018-12-13 08:16:46 -06:00
|
|
|
/// Returns whether an allocator shim was created
|
2020-06-12 12:31:35 -05:00
|
|
|
pub(crate) fn codegen(
|
|
|
|
tcx: TyCtxt<'_>,
|
|
|
|
module: &mut Module<impl Backend + 'static>,
|
|
|
|
unwind_context: &mut UnwindContext<'_>,
|
|
|
|
) -> bool {
|
2019-09-28 04:13:40 -05:00
|
|
|
let any_dynamic_crate = tcx.dependency_formats(LOCAL_CRATE).iter().any(|(_, list)| {
|
2020-03-31 06:20:19 -05:00
|
|
|
use rustc_middle::middle::dependency_format::Linkage;
|
2019-02-21 08:06:09 -06:00
|
|
|
list.iter().any(|&linkage| linkage == Linkage::Dynamic)
|
|
|
|
});
|
2018-12-12 09:01:34 -06:00
|
|
|
if any_dynamic_crate {
|
2018-12-13 08:16:46 -06:00
|
|
|
false
|
2019-11-14 14:13:40 -06:00
|
|
|
} else if let Some(kind) = tcx.allocator_kind() {
|
2020-06-12 12:31:35 -05:00
|
|
|
codegen_inner(module, unwind_context, kind);
|
2018-12-13 08:16:46 -06:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
2018-12-12 09:01:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-12 12:31:35 -05:00
|
|
|
fn codegen_inner(
|
|
|
|
module: &mut Module<impl Backend + 'static>,
|
|
|
|
unwind_context: &mut UnwindContext<'_>,
|
|
|
|
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 {
|
2019-12-17 08:03:32 -06:00
|
|
|
call_conv: CallConv::triple_default(module.isa().triple()),
|
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();
|
2019-09-28 10:59:27 -05:00
|
|
|
ctx.func = Function::with_name_signature(ExternalName::user(0, 0), sig.clone());
|
|
|
|
{
|
|
|
|
let mut func_ctx = FunctionBuilderContext::new();
|
|
|
|
let mut bcx = FunctionBuilder::new(&mut ctx.func, &mut func_ctx);
|
2018-11-05 11:29:15 -06:00
|
|
|
|
2020-02-14 11:23:29 -06:00
|
|
|
let block = bcx.create_block();
|
|
|
|
bcx.switch_to_block(block);
|
2018-11-05 11:29:15 -06:00
|
|
|
let args = arg_tys
|
|
|
|
.into_iter()
|
2020-02-14 11:23:29 -06:00
|
|
|
.map(|ty| bcx.append_block_param(block, ty))
|
2018-11-05 11:29:15 -06:00
|
|
|
.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
|
2020-10-05 04:12:41 -05:00
|
|
|
|
2018-11-05 11:29:15 -06:00
|
|
|
bcx.ins().return_(&results);
|
|
|
|
bcx.seal_all_blocks();
|
2019-09-28 10:59:27 -05:00
|
|
|
bcx.finalize();
|
|
|
|
}
|
2020-08-28 05:10:48 -05:00
|
|
|
module
|
|
|
|
.define_function(
|
|
|
|
func_id,
|
|
|
|
&mut ctx,
|
|
|
|
&mut cranelift_codegen::binemit::NullTrapSink {},
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-06-12 12:31:35 -05:00
|
|
|
unwind_context.add_function(func_id, &ctx, module.isa());
|
2018-11-05 11:29:15 -06:00
|
|
|
}
|
2020-10-05 04:12:41 -05:00
|
|
|
|
|
|
|
let sig = Signature {
|
|
|
|
call_conv: CallConv::triple_default(module.isa().triple()),
|
|
|
|
params: vec![AbiParam::new(usize_ty), AbiParam::new(usize_ty)],
|
|
|
|
returns: vec![],
|
|
|
|
};
|
|
|
|
|
|
|
|
let callee_name = kind.fn_name(sym::oom);
|
|
|
|
//eprintln!("Codegen allocator shim {} -> {} ({:?} -> {:?})", caller_name, callee_name, sig.params, sig.returns);
|
|
|
|
|
|
|
|
let func_id = module
|
|
|
|
.declare_function("__rust_alloc_error_handler", Linkage::Export, &sig)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let callee_func_id = module
|
|
|
|
.declare_function(&callee_name, Linkage::Import, &sig)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mut ctx = Context::new();
|
|
|
|
ctx.func = Function::with_name_signature(ExternalName::user(0, 0), sig.clone());
|
|
|
|
{
|
|
|
|
let mut func_ctx = FunctionBuilderContext::new();
|
|
|
|
let mut bcx = FunctionBuilder::new(&mut ctx.func, &mut func_ctx);
|
|
|
|
|
|
|
|
let block = bcx.create_block();
|
|
|
|
bcx.switch_to_block(block);
|
|
|
|
let args = (&[usize_ty, usize_ty])
|
|
|
|
.into_iter()
|
|
|
|
.map(|&ty| bcx.append_block_param(block, ty))
|
|
|
|
.collect::<Vec<Value>>();
|
|
|
|
|
|
|
|
let callee_func_ref = module.declare_func_in_func(callee_func_id, &mut bcx.func);
|
|
|
|
bcx.ins().call(callee_func_ref, &args);
|
|
|
|
|
|
|
|
bcx.ins().trap(TrapCode::UnreachableCodeReached);
|
|
|
|
bcx.seal_all_blocks();
|
|
|
|
bcx.finalize();
|
|
|
|
}
|
|
|
|
module
|
|
|
|
.define_function(
|
|
|
|
func_id,
|
|
|
|
&mut ctx,
|
|
|
|
&mut cranelift_codegen::binemit::NullTrapSink {},
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
unwind_context.add_function(func_id, &ctx, module.isa());
|
2018-11-05 11:29:15 -06:00
|
|
|
}
|