-
-
Notifications
You must be signed in to change notification settings - Fork 15.1k
Expand file tree
/
Copy pathallocator.rs
More file actions
57 lines (49 loc) · 1.78 KB
/
Copy pathallocator.rs
File metadata and controls
57 lines (49 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//! Allocator shim
// Adapted from rustc
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext};
use rustc_ast::expand::allocator::NO_ALLOC_SHIM_IS_UNSTABLE;
use rustc_codegen_ssa::base::needs_allocator_shim;
use rustc_session::config::OomStrategy;
use rustc_symbol_mangling::mangle_internal_symbol;
use crate::prelude::*;
/// Returns whether an allocator shim was created
pub(crate) fn codegen(tcx: TyCtxt<'_>, module: &mut dyn Module) -> bool {
if needs_allocator_shim(tcx) {
codegen_inner(tcx, module, tcx.sess.opts.unstable_opts.oom);
true
} else {
false
}
}
fn codegen_inner(tcx: TyCtxt<'_>, module: &mut dyn Module, oom_strategy: OomStrategy) {
let data_id = module.declare_data(OomStrategy::SYMBOL, Linkage::Export, false, false).unwrap();
let mut data = DataDescription::new();
data.set_align(1);
let val = oom_strategy.should_panic();
data.define(Box::new([val]));
module.define_data(data_id, &data).unwrap();
{
let sig = Signature {
call_conv: module.target_config().default_call_conv,
params: vec![],
returns: vec![],
};
let func_id = module
.declare_function(
&mangle_internal_symbol(tcx, NO_ALLOC_SHIM_IS_UNSTABLE),
Linkage::Export,
&sig,
)
.unwrap();
let mut ctx = Context::new();
ctx.func.signature = sig;
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);
bcx.ins().return_(&[]);
bcx.seal_all_blocks();
bcx.finalize();
module.define_function(func_id, &mut ctx).unwrap();
}
}