2020-09-23 15:13:49 +02:00
|
|
|
//! Allocator shim
|
|
|
|
// Adapted from rustc
|
2018-11-05 18:29:15 +01:00
|
|
|
|
|
|
|
use crate::prelude::*;
|
|
|
|
|
2020-03-04 15:04:28 +01:00
|
|
|
use rustc_ast::expand::allocator::{AllocatorKind, AllocatorTy, ALLOCATOR_METHODS};
|
2021-10-06 15:52:54 +01:00
|
|
|
use rustc_session::config::OomStrategy;
|
2022-10-14 02:24:58 +01:00
|
|
|
use rustc_span::symbol::sym;
|
2018-11-05 18:29:15 +01:00
|
|
|
|
2018-12-13 15:16:46 +01:00
|
|
|
/// Returns whether an allocator shim was created
|
2020-06-12 19:31:35 +02:00
|
|
|
pub(crate) fn codegen(
|
|
|
|
tcx: TyCtxt<'_>,
|
2020-10-01 10:38:23 +02:00
|
|
|
module: &mut impl Module,
|
2021-04-30 14:49:58 +02:00
|
|
|
unwind_context: &mut UnwindContext,
|
2020-06-12 19:31:35 +02:00
|
|
|
) -> bool {
|
2021-05-11 11:26:52 +02:00
|
|
|
let any_dynamic_crate = tcx.dependency_formats(()).iter().any(|(_, list)| {
|
2020-03-31 13:20:19 +02:00
|
|
|
use rustc_middle::middle::dependency_format::Linkage;
|
2019-02-21 15:06:09 +01:00
|
|
|
list.iter().any(|&linkage| linkage == Linkage::Dynamic)
|
|
|
|
});
|
2018-12-12 16:01:34 +01:00
|
|
|
if any_dynamic_crate {
|
2018-12-13 15:16:46 +01:00
|
|
|
false
|
2021-05-11 22:05:54 +02:00
|
|
|
} else if let Some(kind) = tcx.allocator_kind(()) {
|
2021-10-06 15:52:54 +01:00
|
|
|
codegen_inner(
|
|
|
|
module,
|
|
|
|
unwind_context,
|
|
|
|
kind,
|
2022-10-14 02:24:58 +01:00
|
|
|
tcx.alloc_error_handler_kind(()).unwrap(),
|
2022-07-06 07:44:47 -05:00
|
|
|
tcx.sess.opts.unstable_opts.oom,
|
2021-10-06 15:52:54 +01:00
|
|
|
);
|
2018-12-13 15:16:46 +01:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
2018-12-12 16:01:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-12 19:31:35 +02:00
|
|
|
fn codegen_inner(
|
2020-10-01 10:38:23 +02:00
|
|
|
module: &mut impl Module,
|
2021-04-30 14:49:58 +02:00
|
|
|
unwind_context: &mut UnwindContext,
|
2020-06-12 19:31:35 +02:00
|
|
|
kind: AllocatorKind,
|
2022-10-14 02:24:58 +01:00
|
|
|
alloc_error_handler_kind: AllocatorKind,
|
2021-10-06 15:52:54 +01:00
|
|
|
oom_strategy: OomStrategy,
|
2020-06-12 19:31:35 +02:00
|
|
|
) {
|
2018-11-05 18:29:15 +01: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 13:32:02 +01:00
|
|
|
AllocatorTy::ResultPtr | AllocatorTy::Unit => panic!("invalid allocator arg"),
|
2018-11-05 18:29:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
let output = match method.output {
|
|
|
|
AllocatorTy::ResultPtr => Some(usize_ty),
|
|
|
|
AllocatorTy::Unit => None,
|
|
|
|
|
2018-11-07 13:32:02 +01:00
|
|
|
AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
|
|
|
|
panic!("invalid allocator output")
|
|
|
|
}
|
2018-11-05 18:29:15 +01:00
|
|
|
};
|
|
|
|
|
2018-12-11 15:13:45 +01:00
|
|
|
let sig = Signature {
|
2019-12-17 15:03:32 +01:00
|
|
|
call_conv: CallConv::triple_default(module.isa().triple()),
|
2018-11-05 18:29:15 +01:00
|
|
|
params: arg_tys.iter().cloned().map(AbiParam::new).collect(),
|
|
|
|
returns: output.into_iter().map(AbiParam::new).collect(),
|
|
|
|
};
|
|
|
|
|
2018-11-14 15:04:20 +01:00
|
|
|
let caller_name = format!("__rust_{}", method.name);
|
|
|
|
let callee_name = kind.fn_name(method.name);
|
|
|
|
|
2021-03-05 19:12:59 +01:00
|
|
|
let func_id = module.declare_function(&caller_name, Linkage::Export, &sig).unwrap();
|
2018-11-05 18:29:15 +01:00
|
|
|
|
2021-03-05 19:12:59 +01:00
|
|
|
let callee_func_id = module.declare_function(&callee_name, Linkage::Import, &sig).unwrap();
|
2018-11-05 18:29:15 +01:00
|
|
|
|
|
|
|
let mut ctx = Context::new();
|
2022-10-23 16:22:55 +02:00
|
|
|
ctx.func.signature = sig.clone();
|
2019-09-28 17:59:27 +02:00
|
|
|
{
|
|
|
|
let mut func_ctx = FunctionBuilderContext::new();
|
|
|
|
let mut bcx = FunctionBuilder::new(&mut ctx.func, &mut func_ctx);
|
2018-11-05 18:29:15 +01:00
|
|
|
|
2020-02-14 18:23:29 +01:00
|
|
|
let block = bcx.create_block();
|
|
|
|
bcx.switch_to_block(block);
|
2018-11-05 18:29:15 +01:00
|
|
|
let args = arg_tys
|
|
|
|
.into_iter()
|
2020-02-14 18:23:29 +01:00
|
|
|
.map(|ty| bcx.append_block_param(block, ty))
|
2018-11-05 18:29:15 +01: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 11:12:41 +02:00
|
|
|
|
2018-11-05 18:29:15 +01:00
|
|
|
bcx.ins().return_(&results);
|
|
|
|
bcx.seal_all_blocks();
|
2019-09-28 17:59:27 +02:00
|
|
|
bcx.finalize();
|
|
|
|
}
|
2022-02-23 11:49:34 +01:00
|
|
|
module.define_function(func_id, &mut ctx).unwrap();
|
2020-06-12 19:31:35 +02:00
|
|
|
unwind_context.add_function(func_id, &ctx, module.isa());
|
2018-11-05 18:29:15 +01:00
|
|
|
}
|
2020-10-05 11:12:41 +02:00
|
|
|
|
|
|
|
let sig = Signature {
|
|
|
|
call_conv: CallConv::triple_default(module.isa().triple()),
|
|
|
|
params: vec![AbiParam::new(usize_ty), AbiParam::new(usize_ty)],
|
|
|
|
returns: vec![],
|
|
|
|
};
|
|
|
|
|
2022-10-14 02:24:58 +01:00
|
|
|
let callee_name = alloc_error_handler_kind.fn_name(sym::oom);
|
2020-10-05 11:12:41 +02:00
|
|
|
|
2021-03-05 19:12:59 +01:00
|
|
|
let func_id =
|
|
|
|
module.declare_function("__rust_alloc_error_handler", Linkage::Export, &sig).unwrap();
|
2020-10-05 11:12:41 +02:00
|
|
|
|
2022-10-14 02:24:58 +01:00
|
|
|
let callee_func_id = module.declare_function(&callee_name, Linkage::Import, &sig).unwrap();
|
2020-10-05 11:12:41 +02:00
|
|
|
|
|
|
|
let mut ctx = Context::new();
|
2022-10-23 16:22:55 +02:00
|
|
|
ctx.func.signature = sig;
|
2020-10-05 11:12:41 +02:00
|
|
|
{
|
|
|
|
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])
|
2020-11-03 11:00:04 +01:00
|
|
|
.iter()
|
2020-10-05 11:12:41 +02:00
|
|
|
.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();
|
|
|
|
}
|
2022-02-23 11:49:34 +01:00
|
|
|
module.define_function(func_id, &mut ctx).unwrap();
|
2020-10-05 11:12:41 +02:00
|
|
|
unwind_context.add_function(func_id, &ctx, module.isa());
|
2021-10-06 15:52:54 +01:00
|
|
|
|
|
|
|
let data_id = module.declare_data(OomStrategy::SYMBOL, Linkage::Export, false, false).unwrap();
|
|
|
|
let mut data_ctx = DataContext::new();
|
|
|
|
data_ctx.set_align(1);
|
|
|
|
let val = oom_strategy.should_panic();
|
|
|
|
data_ctx.define(Box::new([val]));
|
|
|
|
module.define_data(data_id, &data_ctx).unwrap();
|
2018-11-05 18:29:15 +01:00
|
|
|
}
|