6 use rustc_ast::expand::allocator::{AllocatorKind, AllocatorTy, ALLOCATOR_METHODS};
8 /// Returns whether an allocator shim was created
11 module: &mut impl Module,
12 unwind_context: &mut UnwindContext,
14 let any_dynamic_crate = tcx.dependency_formats(()).iter().any(|(_, list)| {
15 use rustc_middle::middle::dependency_format::Linkage;
16 list.iter().any(|&linkage| linkage == Linkage::Dynamic)
18 if any_dynamic_crate {
20 } else if let Some(kind) = tcx.allocator_kind(()) {
21 codegen_inner(module, unwind_context, kind, tcx.lang_items().oom().is_some());
29 module: &mut impl Module,
30 unwind_context: &mut UnwindContext,
32 has_alloc_error_handler: bool,
34 let usize_ty = module.target_config().pointer_type();
36 for method in ALLOCATOR_METHODS {
37 let mut arg_tys = Vec::with_capacity(method.inputs.len());
38 for ty in method.inputs.iter() {
40 AllocatorTy::Layout => {
41 arg_tys.push(usize_ty); // size
42 arg_tys.push(usize_ty); // align
44 AllocatorTy::Ptr => arg_tys.push(usize_ty),
45 AllocatorTy::Usize => arg_tys.push(usize_ty),
47 AllocatorTy::ResultPtr | AllocatorTy::Unit => panic!("invalid allocator arg"),
50 let output = match method.output {
51 AllocatorTy::ResultPtr => Some(usize_ty),
52 AllocatorTy::Unit => None,
54 AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
55 panic!("invalid allocator output")
60 call_conv: CallConv::triple_default(module.isa().triple()),
61 params: arg_tys.iter().cloned().map(AbiParam::new).collect(),
62 returns: output.into_iter().map(AbiParam::new).collect(),
65 let caller_name = format!("__rust_{}", method.name);
66 let callee_name = kind.fn_name(method.name);
68 let func_id = module.declare_function(&caller_name, Linkage::Export, &sig).unwrap();
70 let callee_func_id = module.declare_function(&callee_name, Linkage::Import, &sig).unwrap();
72 let mut ctx = Context::new();
73 ctx.func = Function::with_name_signature(ExternalName::user(0, 0), sig.clone());
75 let mut func_ctx = FunctionBuilderContext::new();
76 let mut bcx = FunctionBuilder::new(&mut ctx.func, &mut func_ctx);
78 let block = bcx.create_block();
79 bcx.switch_to_block(block);
82 .map(|ty| bcx.append_block_param(block, ty))
83 .collect::<Vec<Value>>();
85 let callee_func_ref = module.declare_func_in_func(callee_func_id, &mut bcx.func);
86 let call_inst = bcx.ins().call(callee_func_ref, &args);
87 let results = bcx.inst_results(call_inst).to_vec(); // Clone to prevent borrow error
89 bcx.ins().return_(&results);
90 bcx.seal_all_blocks();
93 module.define_function(func_id, &mut ctx).unwrap();
94 unwind_context.add_function(func_id, &ctx, module.isa());
98 call_conv: CallConv::triple_default(module.isa().triple()),
99 params: vec![AbiParam::new(usize_ty), AbiParam::new(usize_ty)],
103 let callee_name = if has_alloc_error_handler { "__rg_oom" } else { "__rdl_oom" };
106 module.declare_function("__rust_alloc_error_handler", Linkage::Export, &sig).unwrap();
108 let callee_func_id = module.declare_function(callee_name, Linkage::Import, &sig).unwrap();
110 let mut ctx = Context::new();
111 ctx.func = Function::with_name_signature(ExternalName::user(0, 0), sig);
113 let mut func_ctx = FunctionBuilderContext::new();
114 let mut bcx = FunctionBuilder::new(&mut ctx.func, &mut func_ctx);
116 let block = bcx.create_block();
117 bcx.switch_to_block(block);
118 let args = (&[usize_ty, usize_ty])
120 .map(|&ty| bcx.append_block_param(block, ty))
121 .collect::<Vec<Value>>();
123 let callee_func_ref = module.declare_func_in_func(callee_func_id, &mut bcx.func);
124 bcx.ins().call(callee_func_ref, &args);
126 bcx.ins().trap(TrapCode::UnreachableCodeReached);
127 bcx.seal_all_blocks();
130 module.define_function(func_id, &mut ctx).unwrap();
131 unwind_context.add_function(func_id, &ctx, module.isa());