1use libc::c_uint;
2use rustc_ast::expand::allocator::{
3 AllocatorMethod, AllocatorTy, NO_ALLOC_SHIM_IS_UNSTABLE, SpecialAllocatorMethod,
4 default_fn_name, global_fn_name,
5};
6use rustc_codegen_ssa::traits::BaseTypeCodegenMethods as _;
7use rustc_middle::bug;
8use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
9use rustc_middle::ty::TyCtxt;
10use rustc_session::config::DebugInfo;
11use rustc_symbol_mangling::mangle_internal_symbol;
12
13use crate::attributes::llfn_attrs_from_instance;
14use crate::builder::SBuilder;
15use crate::declare::declare_simple_fn;
16use crate::llvm::{self, FromGeneric, TRUE, Type};
17use crate::{SimpleCx, attributes, debuginfo};
18
19pub(crate) unsafe fn codegen(
20 tcx: TyCtxt<'_>,
21 cx: SimpleCx<'_>,
22 module_name: &str,
23 methods: &[AllocatorMethod],
24) {
25 let usize = match tcx.sess.target.pointer_width {
26 16 => cx.type_i16(),
27 32 => cx.type_i32(),
28 64 => cx.type_i64(),
29 tws => bug!("Unsupported target word size for int: {}", tws),
30 };
31 let i8p = cx.type_ptr();
32
33 for method in methods {
34 let mut args = Vec::with_capacity(method.inputs.len());
35 for input in method.inputs.iter() {
36 match input.ty {
37 AllocatorTy::Layout => {
38 args.push(usize); args.push(usize); }
41 AllocatorTy::Ptr => args.push(i8p),
42 AllocatorTy::Usize => args.push(usize),
43
44 AllocatorTy::Never | AllocatorTy::ResultPtr | AllocatorTy::Unit => {
45 panic!("invalid allocator arg")
46 }
47 }
48 }
49
50 let mut no_return = false;
51 let output = match method.output {
52 AllocatorTy::ResultPtr => Some(i8p),
53 AllocatorTy::Unit => None,
54 AllocatorTy::Never => {
55 no_return = true;
56 None
57 }
58
59 AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
60 panic!("invalid allocator output")
61 }
62 };
63
64 let from_name = mangle_internal_symbol(tcx, &global_fn_name(method.name));
65 let to_name = mangle_internal_symbol(tcx, &default_fn_name(method.name));
66
67 let alloc_attr_flag = match method.special {
68 Some(SpecialAllocatorMethod::Alloc) => CodegenFnAttrFlags::ALLOCATOR,
69 Some(SpecialAllocatorMethod::Dealloc) => CodegenFnAttrFlags::DEALLOCATOR,
70 Some(SpecialAllocatorMethod::Realloc) => CodegenFnAttrFlags::REALLOCATOR,
71 Some(SpecialAllocatorMethod::AllocZeroed) => CodegenFnAttrFlags::ALLOCATOR_ZEROED,
72 None => CodegenFnAttrFlags::empty(),
73 };
74
75 let mut attrs = CodegenFnAttrs::new();
76 attrs.flags |= alloc_attr_flag;
77 create_wrapper_function(
78 tcx,
79 &cx,
80 &from_name,
81 Some(&to_name),
82 &args,
83 output,
84 no_return,
85 &attrs,
86 );
87 }
88
89 create_wrapper_function(
91 tcx,
92 &cx,
93 &mangle_internal_symbol(tcx, NO_ALLOC_SHIM_IS_UNSTABLE),
94 None,
95 &[],
96 None,
97 false,
98 &CodegenFnAttrs::new(),
99 );
100
101 if tcx.sess.opts.debuginfo != DebugInfo::None {
102 let dbg_cx = debuginfo::CodegenUnitDebugContext::new(cx.llmod);
103 debuginfo::metadata::build_compile_unit_di_node(tcx, module_name, &dbg_cx);
104 dbg_cx.finalize(tcx.sess);
105 }
106}
107
108fn create_wrapper_function(
109 tcx: TyCtxt<'_>,
110 cx: &SimpleCx<'_>,
111 from_name: &str,
112 to_name: Option<&str>,
113 args: &[&Type],
114 output: Option<&Type>,
115 no_return: bool,
116 attrs: &CodegenFnAttrs,
117) {
118 let ty = cx.type_func(args, output.unwrap_or_else(|| cx.type_void()));
119 let llfn = declare_simple_fn(
120 &cx,
121 from_name,
122 llvm::CallConv::CCallConv,
123 llvm::UnnamedAddr::Global,
124 llvm::Visibility::from_generic(tcx.sess.default_visibility()),
125 ty,
126 );
127
128 llfn_attrs_from_instance(cx, tcx, llfn, attrs, None);
129
130 let no_return = if no_return {
131 let no_return = llvm::AttributeKind::NoReturn.create_attr(cx.llcx);
133 attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[no_return]);
134 Some(no_return)
135 } else {
136 None
137 };
138
139 let llbb = unsafe { llvm::LLVMAppendBasicBlockInContext(cx.llcx, llfn, c"entry".as_ptr()) };
140 let mut bx = SBuilder::build(&cx, llbb);
141
142 if let Some(to_name) = to_name {
143 let callee = declare_simple_fn(
144 &cx,
145 to_name,
146 llvm::CallConv::CCallConv,
147 llvm::UnnamedAddr::Global,
148 llvm::Visibility::Hidden,
149 ty,
150 );
151 if let Some(no_return) = no_return {
152 attributes::apply_to_llfn(callee, llvm::AttributePlace::Function, &[no_return]);
154 }
155 llvm::set_visibility(callee, llvm::Visibility::Hidden);
156
157 let args = args
158 .iter()
159 .enumerate()
160 .map(|(i, _)| llvm::get_param(llfn, i as c_uint))
161 .collect::<Vec<_>>();
162 let ret = bx.call(ty, callee, &args, None);
163 llvm::LLVMSetTailCall(ret, TRUE);
164 if output.is_some() {
165 bx.ret(ret);
166 } else {
167 bx.ret_void()
168 }
169 } else {
170 assert!(output.is_none());
171 bx.ret_void()
172 }
173}