rust/compiler/rustc_codegen_cranelift/src/allocator.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

73 lines
2.4 KiB
Rust
Raw Normal View History

//! Allocator shim
// Adapted from rustc
2018-11-05 18:29:15 +01:00
use crate::prelude::*;
use rustc_ast::expand::allocator::{AllocatorKind, AllocatorTy, ALLOCATOR_METHODS};
use rustc_codegen_ssa::base::allocator_kind_for_codegen;
use rustc_session::config::OomStrategy;
2018-11-05 18:29:15 +01:00
/// Returns whether an allocator shim was created
pub(crate) fn codegen(
tcx: TyCtxt<'_>,
module: &mut impl Module,
unwind_context: &mut UnwindContext,
) -> bool {
let Some(kind) = allocator_kind_for_codegen(tcx) else { return false };
codegen_inner(module, unwind_context, kind, tcx.sess.opts.unstable_opts.oom);
true
}
fn codegen_inner(
module: &mut impl Module,
unwind_context: &mut UnwindContext,
kind: AllocatorKind,
oom_strategy: OomStrategy,
) {
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
};
let sig = Signature {
2022-12-14 12:25:53 +00:00
call_conv: module.target_config().default_call_conv,
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(),
};
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 18:29:15 +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
}