2020-09-23 08:13:49 -05:00
|
|
|
//! Allocator shim
|
|
|
|
// Adapted from rustc
|
2018-11-05 11:29:15 -06:00
|
|
|
|
2021-07-03 09:46:41 -05:00
|
|
|
use rustc_ast::expand::allocator::{
|
2021-07-06 11:56:01 -05:00
|
|
|
alloc_error_handler_name, default_fn_name, global_fn_name, AllocatorKind, AllocatorTy,
|
2022-09-10 06:33:44 -05:00
|
|
|
ALLOCATOR_METHODS, NO_ALLOC_SHIM_IS_UNSTABLE,
|
2021-07-03 09:46:41 -05:00
|
|
|
};
|
2023-02-13 17:48:06 -06:00
|
|
|
use rustc_codegen_ssa::base::allocator_kind_for_codegen;
|
2021-10-06 09:52:54 -05:00
|
|
|
use rustc_session::config::OomStrategy;
|
2018-11-05 11:29:15 -06:00
|
|
|
|
2023-10-09 03:52:46 -05:00
|
|
|
use crate::prelude::*;
|
|
|
|
|
2018-12-13 08:16:46 -06:00
|
|
|
/// Returns whether an allocator shim was created
|
2024-06-30 06:28:14 -05:00
|
|
|
pub(crate) fn codegen(tcx: TyCtxt<'_>, module: &mut dyn Module) -> bool {
|
2023-02-13 17:48:06 -06:00
|
|
|
let Some(kind) = allocator_kind_for_codegen(tcx) else { return false };
|
|
|
|
codegen_inner(
|
|
|
|
module,
|
|
|
|
kind,
|
|
|
|
tcx.alloc_error_handler_kind(()).unwrap(),
|
|
|
|
tcx.sess.opts.unstable_opts.oom,
|
|
|
|
);
|
|
|
|
true
|
2018-12-12 09:01:34 -06:00
|
|
|
}
|
|
|
|
|
2020-06-12 12:31:35 -05:00
|
|
|
fn codegen_inner(
|
2024-06-30 06:28:14 -05:00
|
|
|
module: &mut dyn Module,
|
2020-06-12 12:31:35 -05:00
|
|
|
kind: AllocatorKind,
|
2022-10-13 20:24:58 -05:00
|
|
|
alloc_error_handler_kind: AllocatorKind,
|
2021-10-06 09:52:54 -05:00
|
|
|
oom_strategy: OomStrategy,
|
2020-06-12 12:31:35 -05:00
|
|
|
) {
|
2018-11-05 11:29:15 -06:00
|
|
|
let usize_ty = module.target_config().pointer_type();
|
|
|
|
|
2021-07-03 09:46:41 -05:00
|
|
|
if kind == AllocatorKind::Default {
|
|
|
|
for method in ALLOCATOR_METHODS {
|
|
|
|
let mut arg_tys = Vec::with_capacity(method.inputs.len());
|
2023-08-06 09:20:31 -05:00
|
|
|
for input in method.inputs.iter() {
|
|
|
|
match input.ty {
|
2021-07-03 09:46:41 -05:00
|
|
|
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-05 11:29:15 -06:00
|
|
|
|
2021-07-03 09:46:41 -05:00
|
|
|
AllocatorTy::ResultPtr | AllocatorTy::Unit => panic!("invalid allocator arg"),
|
|
|
|
}
|
2018-11-05 11:29:15 -06:00
|
|
|
}
|
2021-07-03 09:46:41 -05:00
|
|
|
let output = match method.output {
|
|
|
|
AllocatorTy::ResultPtr => Some(usize_ty),
|
|
|
|
AllocatorTy::Unit => None,
|
2018-11-05 11:29:15 -06:00
|
|
|
|
2021-07-03 09:46:41 -05:00
|
|
|
AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
|
|
|
|
panic!("invalid allocator output")
|
|
|
|
}
|
|
|
|
};
|
2018-11-05 11:29:15 -06:00
|
|
|
|
2021-07-03 09:46:41 -05:00
|
|
|
let sig = Signature {
|
|
|
|
call_conv: module.target_config().default_call_conv,
|
|
|
|
params: arg_tys.iter().cloned().map(AbiParam::new).collect(),
|
|
|
|
returns: output.into_iter().map(AbiParam::new).collect(),
|
|
|
|
};
|
|
|
|
crate::common::create_wrapper_function(
|
|
|
|
module,
|
|
|
|
sig,
|
2021-07-06 11:56:01 -05:00
|
|
|
&global_fn_name(method.name),
|
2021-07-03 10:50:53 -05:00
|
|
|
&default_fn_name(method.name),
|
2021-07-03 09:46:41 -05:00
|
|
|
);
|
|
|
|
}
|
2018-11-05 11:29:15 -06:00
|
|
|
}
|
2020-10-05 04:12:41 -05:00
|
|
|
|
|
|
|
let sig = Signature {
|
2022-12-14 12:30:46 -06:00
|
|
|
call_conv: module.target_config().default_call_conv,
|
2020-10-05 04:12:41 -05:00
|
|
|
params: vec![AbiParam::new(usize_ty), AbiParam::new(usize_ty)],
|
|
|
|
returns: vec![],
|
|
|
|
};
|
2023-02-09 05:38:16 -06:00
|
|
|
crate::common::create_wrapper_function(
|
|
|
|
module,
|
|
|
|
sig,
|
|
|
|
"__rust_alloc_error_handler",
|
2021-07-03 09:46:41 -05:00
|
|
|
&alloc_error_handler_name(alloc_error_handler_kind),
|
2023-02-09 05:38:16 -06:00
|
|
|
);
|
2021-10-06 09:52:54 -05:00
|
|
|
|
|
|
|
let data_id = module.declare_data(OomStrategy::SYMBOL, Linkage::Export, false, false).unwrap();
|
2023-06-15 12:56:01 -05:00
|
|
|
let mut data = DataDescription::new();
|
|
|
|
data.set_align(1);
|
2021-10-06 09:52:54 -05:00
|
|
|
let val = oom_strategy.should_panic();
|
2023-06-15 12:56:01 -05:00
|
|
|
data.define(Box::new([val]));
|
|
|
|
module.define_data(data_id, &data).unwrap();
|
2022-09-10 06:33:44 -05:00
|
|
|
|
|
|
|
let data_id =
|
|
|
|
module.declare_data(NO_ALLOC_SHIM_IS_UNSTABLE, Linkage::Export, false, false).unwrap();
|
2023-06-15 12:56:01 -05:00
|
|
|
let mut data = DataDescription::new();
|
|
|
|
data.set_align(1);
|
|
|
|
data.define(Box::new([0]));
|
|
|
|
module.define_data(data_id, &data).unwrap();
|
2018-11-05 11:29:15 -06:00
|
|
|
}
|