6 use rustc_ast::expand::allocator::{AllocatorKind, AllocatorTy, ALLOCATOR_METHODS};
7 use rustc_session::config::OomStrategy;
8 use rustc_span::symbol::sym;
10 /// Returns whether an allocator shim was created
11 pub(crate) fn codegen(
13 module: &mut impl Module,
14 unwind_context: &mut UnwindContext,
16 let any_dynamic_crate = tcx.dependency_formats(()).iter().any(|(_, list)| {
17 use rustc_middle::middle::dependency_format::Linkage;
18 list.iter().any(|&linkage| linkage == Linkage::Dynamic)
20 if any_dynamic_crate {
22 } else if let Some(kind) = tcx.allocator_kind(()) {
27 tcx.alloc_error_handler_kind(()).unwrap(),
28 tcx.sess.opts.unstable_opts.oom,
37 module: &mut impl Module,
38 unwind_context: &mut UnwindContext,
40 alloc_error_handler_kind: AllocatorKind,
41 oom_strategy: OomStrategy,
43 let usize_ty = module.target_config().pointer_type();
45 for method in ALLOCATOR_METHODS {
46 let mut arg_tys = Vec::with_capacity(method.inputs.len());
47 for ty in method.inputs.iter() {
49 AllocatorTy::Layout => {
50 arg_tys.push(usize_ty); // size
51 arg_tys.push(usize_ty); // align
53 AllocatorTy::Ptr => arg_tys.push(usize_ty),
54 AllocatorTy::Usize => arg_tys.push(usize_ty),
56 AllocatorTy::ResultPtr | AllocatorTy::Unit => panic!("invalid allocator arg"),
59 let output = match method.output {
60 AllocatorTy::ResultPtr => Some(usize_ty),
61 AllocatorTy::Unit => None,
63 AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
64 panic!("invalid allocator output")
69 call_conv: module.target_config().default_call_conv,
70 params: arg_tys.iter().cloned().map(AbiParam::new).collect(),
71 returns: output.into_iter().map(AbiParam::new).collect(),
74 let caller_name = format!("__rust_{}", method.name);
75 let callee_name = kind.fn_name(method.name);
77 let func_id = module.declare_function(&caller_name, Linkage::Export, &sig).unwrap();
79 let callee_func_id = module.declare_function(&callee_name, Linkage::Import, &sig).unwrap();
81 let mut ctx = Context::new();
82 ctx.func.signature = sig.clone();
84 let mut func_ctx = FunctionBuilderContext::new();
85 let mut bcx = FunctionBuilder::new(&mut ctx.func, &mut func_ctx);
87 let block = bcx.create_block();
88 bcx.switch_to_block(block);
91 .map(|ty| bcx.append_block_param(block, ty))
92 .collect::<Vec<Value>>();
94 let callee_func_ref = module.declare_func_in_func(callee_func_id, &mut bcx.func);
95 let call_inst = bcx.ins().call(callee_func_ref, &args);
96 let results = bcx.inst_results(call_inst).to_vec(); // Clone to prevent borrow error
98 bcx.ins().return_(&results);
99 bcx.seal_all_blocks();
102 module.define_function(func_id, &mut ctx).unwrap();
103 unwind_context.add_function(func_id, &ctx, module.isa());
106 let sig = Signature {
107 call_conv: module.target_config().default_call_conv,
108 params: vec![AbiParam::new(usize_ty), AbiParam::new(usize_ty)],
112 let callee_name = alloc_error_handler_kind.fn_name(sym::oom);
115 module.declare_function("__rust_alloc_error_handler", Linkage::Export, &sig).unwrap();
117 let callee_func_id = module.declare_function(&callee_name, Linkage::Import, &sig).unwrap();
119 let mut ctx = Context::new();
120 ctx.func.signature = sig;
122 let mut func_ctx = FunctionBuilderContext::new();
123 let mut bcx = FunctionBuilder::new(&mut ctx.func, &mut func_ctx);
125 let block = bcx.create_block();
126 bcx.switch_to_block(block);
127 let args = (&[usize_ty, usize_ty])
129 .map(|&ty| bcx.append_block_param(block, ty))
130 .collect::<Vec<Value>>();
132 let callee_func_ref = module.declare_func_in_func(callee_func_id, &mut bcx.func);
133 bcx.ins().call(callee_func_ref, &args);
135 bcx.ins().trap(TrapCode::UnreachableCodeReached);
136 bcx.seal_all_blocks();
139 module.define_function(func_id, &mut ctx).unwrap();
140 unwind_context.add_function(func_id, &ctx, module.isa());
142 let data_id = module.declare_data(OomStrategy::SYMBOL, Linkage::Export, false, false).unwrap();
143 let mut data_ctx = DataContext::new();
144 data_ctx.set_align(1);
145 let val = oom_strategy.should_panic();
146 data_ctx.define(Box::new([val]));
147 module.define_data(data_id, &data_ctx).unwrap();