2020-09-23 13:13:49 +00:00
|
|
|
//! Allocator shim
|
|
|
|
// Adapted from rustc
|
2018-11-05 17:29:15 +00:00
|
|
|
|
|
|
|
use crate::prelude::*;
|
|
|
|
|
2020-03-04 14:04:28 +00:00
|
|
|
use rustc_ast::expand::allocator::{AllocatorKind, AllocatorTy, ALLOCATOR_METHODS};
|
2021-10-06 14:52:54 +00:00
|
|
|
use rustc_session::config::OomStrategy;
|
2022-10-14 01:24:58 +00:00
|
|
|
use rustc_span::symbol::sym;
|
2018-11-05 17:29:15 +00:00
|
|
|
|
2018-12-13 14:16:46 +00:00
|
|
|
/// Returns whether an allocator shim was created
|
2020-06-12 17:31:35 +00:00
|
|
|
pub(crate) fn codegen(
|
|
|
|
tcx: TyCtxt<'_>,
|
2020-10-01 08:38:23 +00:00
|
|
|
module: &mut impl Module,
|
2021-04-30 12:49:58 +00:00
|
|
|
unwind_context: &mut UnwindContext,
|
2020-06-12 17:31:35 +00:00
|
|
|
) -> bool {
|
2021-05-11 09:26:52 +00:00
|
|
|
let any_dynamic_crate = tcx.dependency_formats(()).iter().any(|(_, list)| {
|
2020-03-31 11:20:19 +00:00
|
|
|
use rustc_middle::middle::dependency_format::Linkage;
|
2019-02-21 14:06:09 +00:00
|
|
|
list.iter().any(|&linkage| linkage == Linkage::Dynamic)
|
|
|
|
});
|
2018-12-12 15:01:34 +00:00
|
|
|
if any_dynamic_crate {
|
2018-12-13 14:16:46 +00:00
|
|
|
false
|
2021-05-11 20:05:54 +00:00
|
|
|
} else if let Some(kind) = tcx.allocator_kind(()) {
|
2021-10-06 14:52:54 +00:00
|
|
|
codegen_inner(
|
|
|
|
module,
|
|
|
|
unwind_context,
|
|
|
|
kind,
|
2022-10-14 01:24:58 +00:00
|
|
|
tcx.alloc_error_handler_kind(()).unwrap(),
|
2022-07-06 12:44:47 +00:00
|
|
|
tcx.sess.opts.unstable_opts.oom,
|
2021-10-06 14:52:54 +00:00
|
|
|
);
|
2018-12-13 14:16:46 +00:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
2018-12-12 15:01:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-12 17:31:35 +00:00
|
|
|
fn codegen_inner(
|
2020-10-01 08:38:23 +00:00
|
|
|
module: &mut impl Module,
|
2021-04-30 12:49:58 +00:00
|
|
|
unwind_context: &mut UnwindContext,
|
2020-06-12 17:31:35 +00:00
|
|
|
kind: AllocatorKind,
|
2022-10-14 01:24:58 +00:00
|
|
|
alloc_error_handler_kind: AllocatorKind,
|
2021-10-06 14:52:54 +00:00
|
|
|
oom_strategy: OomStrategy,
|
2020-06-12 17:31:35 +00:00
|
|
|
) {
|
2018-11-05 17:29:15 +00: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 12:32:02 +00:00
|
|
|
AllocatorTy::ResultPtr | AllocatorTy::Unit => panic!("invalid allocator arg"),
|
2018-11-05 17:29:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
let output = match method.output {
|
|
|
|
AllocatorTy::ResultPtr => Some(usize_ty),
|
|
|
|
AllocatorTy::Unit => None,
|
|
|
|
|
2018-11-07 12:32:02 +00:00
|
|
|
AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
|
|
|
|
panic!("invalid allocator output")
|
|
|
|
}
|
2018-11-05 17:29:15 +00:00
|
|
|
};
|
|
|
|
|
2018-12-11 14:13:45 +00:00
|
|
|
let sig = Signature {
|
2022-12-14 12:25:53 +00:00
|
|
|
call_conv: module.target_config().default_call_conv,
|
2018-11-05 17:29:15 +00:00
|
|
|
params: arg_tys.iter().cloned().map(AbiParam::new).collect(),
|
|
|
|
returns: output.into_iter().map(AbiParam::new).collect(),
|
|
|
|
};
|
2023-02-05 17:24:02 +00:00
|
|
|
crate::common::create_wrapper_function(
|
|
|
|
module,
|
|
|
|
unwind_context,
|
|
|
|
sig,
|
|
|
|
&format!("__rust_{}", method.name),
|
|
|
|
&kind.fn_name(method.name),
|
|
|
|
);
|
2018-11-05 17:29:15 +00:00
|
|
|
}
|
2020-10-05 09:12:41 +00:00
|
|
|
|
|
|
|
let sig = Signature {
|
2022-12-14 12:25:53 +00:00
|
|
|
call_conv: module.target_config().default_call_conv,
|
2020-10-05 09:12:41 +00:00
|
|
|
params: vec![AbiParam::new(usize_ty), AbiParam::new(usize_ty)],
|
|
|
|
returns: vec![],
|
|
|
|
};
|
2023-02-05 17:24:02 +00:00
|
|
|
crate::common::create_wrapper_function(
|
|
|
|
module,
|
|
|
|
unwind_context,
|
|
|
|
sig,
|
|
|
|
"__rust_alloc_error_handler",
|
|
|
|
&alloc_error_handler_kind.fn_name(sym::oom),
|
|
|
|
);
|
2021-10-06 14:52:54 +00: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 17:29:15 +00:00
|
|
|
}
|