1use libc::c_uint;
2use rustc_ast::expand::allocator::{
3 ALLOCATOR_METHODS, AllocatorKind, AllocatorTy, NO_ALLOC_SHIM_IS_UNSTABLE,
4 alloc_error_handler_name, default_fn_name, global_fn_name,
5};
6use rustc_codegen_ssa::traits::BaseTypeCodegenMethods as _;
7use rustc_middle::bug;
8use rustc_middle::ty::TyCtxt;
9use rustc_session::config::{DebugInfo, OomStrategy};
10use rustc_symbol_mangling::mangle_internal_symbol;
11use smallvec::SmallVec;
12
13use crate::builder::SBuilder;
14use crate::declare::declare_simple_fn;
15use crate::llvm::{self, FALSE, TRUE, Type, Value};
16use crate::{SimpleCx, attributes, debuginfo, llvm_util};
17
18pub(crate) unsafe fn codegen(
19 tcx: TyCtxt<'_>,
20 cx: SimpleCx<'_>,
21 module_name: &str,
22 kind: AllocatorKind,
23 alloc_error_handler_kind: AllocatorKind,
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 i8 = cx.type_i8();
32 let i8p = cx.type_ptr();
33
34 if kind == AllocatorKind::Default {
35 for method in ALLOCATOR_METHODS {
36 let mut args = Vec::with_capacity(method.inputs.len());
37 for input in method.inputs.iter() {
38 match input.ty {
39 AllocatorTy::Layout => {
40 args.push(usize); args.push(usize); }
43 AllocatorTy::Ptr => args.push(i8p),
44 AllocatorTy::Usize => args.push(usize),
45
46 AllocatorTy::ResultPtr | AllocatorTy::Unit => panic!("invalid allocator arg"),
47 }
48 }
49 let output = match method.output {
50 AllocatorTy::ResultPtr => Some(i8p),
51 AllocatorTy::Unit => None,
52
53 AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
54 panic!("invalid allocator output")
55 }
56 };
57
58 let from_name = mangle_internal_symbol(tcx, &global_fn_name(method.name));
59 let to_name = mangle_internal_symbol(tcx, &default_fn_name(method.name));
60
61 create_wrapper_function(tcx, &cx, &from_name, Some(&to_name), &args, output, false);
62 }
63 }
64
65 create_wrapper_function(
67 tcx,
68 &cx,
69 &mangle_internal_symbol(tcx, "__rust_alloc_error_handler"),
70 Some(&mangle_internal_symbol(tcx, alloc_error_handler_name(alloc_error_handler_kind))),
71 &[usize, usize], None,
73 true,
74 );
75
76 unsafe {
77 create_const_value_function(
79 tcx,
80 &cx,
81 &mangle_internal_symbol(tcx, OomStrategy::SYMBOL),
82 &i8,
83 &llvm::LLVMConstInt(i8, tcx.sess.opts.unstable_opts.oom.should_panic() as u64, FALSE),
84 );
85
86 create_wrapper_function(
88 tcx,
89 &cx,
90 &mangle_internal_symbol(tcx, NO_ALLOC_SHIM_IS_UNSTABLE),
91 None,
92 &[],
93 None,
94 false,
95 );
96 }
97
98 if tcx.sess.opts.debuginfo != DebugInfo::None {
99 let dbg_cx = debuginfo::CodegenUnitDebugContext::new(cx.llmod);
100 debuginfo::metadata::build_compile_unit_di_node(tcx, module_name, &dbg_cx);
101 dbg_cx.finalize(tcx.sess);
102 }
103}
104
105fn create_const_value_function(
106 tcx: TyCtxt<'_>,
107 cx: &SimpleCx<'_>,
108 name: &str,
109 output: &Type,
110 value: &Value,
111) {
112 let ty = cx.type_func(&[], output);
113 let llfn = declare_simple_fn(
114 &cx,
115 name,
116 llvm::CallConv::CCallConv,
117 llvm::UnnamedAddr::Global,
118 llvm::Visibility::from_generic(tcx.sess.default_visibility()),
119 ty,
120 );
121
122 attributes::apply_to_llfn(
123 llfn,
124 llvm::AttributePlace::Function,
125 &[llvm::AttributeKind::AlwaysInline.create_attr(cx.llcx)],
126 );
127
128 let llbb = unsafe { llvm::LLVMAppendBasicBlockInContext(cx.llcx, llfn, c"entry".as_ptr()) };
129 let mut bx = SBuilder::build(&cx, llbb);
130 bx.ret(value);
131}
132
133fn create_wrapper_function(
134 tcx: TyCtxt<'_>,
135 cx: &SimpleCx<'_>,
136 from_name: &str,
137 to_name: Option<&str>,
138 args: &[&Type],
139 output: Option<&Type>,
140 no_return: bool,
141) {
142 let ty = cx.type_func(args, output.unwrap_or_else(|| cx.type_void()));
143 let llfn = declare_simple_fn(
144 &cx,
145 from_name,
146 llvm::CallConv::CCallConv,
147 llvm::UnnamedAddr::Global,
148 llvm::Visibility::from_generic(tcx.sess.default_visibility()),
149 ty,
150 );
151
152 let mut attrs = SmallVec::<[_; 2]>::new();
153
154 let target_cpu = llvm_util::target_cpu(tcx.sess);
155 let target_cpu_attr = llvm::CreateAttrStringValue(cx.llcx, "target-cpu", target_cpu);
156
157 let tune_cpu_attr = llvm_util::tune_cpu(tcx.sess)
158 .map(|tune_cpu| llvm::CreateAttrStringValue(cx.llcx, "tune-cpu", tune_cpu));
159
160 attrs.push(target_cpu_attr);
161 attrs.extend(tune_cpu_attr);
162
163 attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &attrs);
164
165 let no_return = if no_return {
166 let no_return = llvm::AttributeKind::NoReturn.create_attr(cx.llcx);
168 attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[no_return]);
169 Some(no_return)
170 } else {
171 None
172 };
173
174 if tcx.sess.must_emit_unwind_tables() {
175 let uwtable =
176 attributes::uwtable_attr(cx.llcx, tcx.sess.opts.unstable_opts.use_sync_unwind);
177 attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[uwtable]);
178 }
179
180 let llbb = unsafe { llvm::LLVMAppendBasicBlockInContext(cx.llcx, llfn, c"entry".as_ptr()) };
181 let mut bx = SBuilder::build(&cx, llbb);
182
183 if let Some(to_name) = to_name {
184 let callee = declare_simple_fn(
185 &cx,
186 to_name,
187 llvm::CallConv::CCallConv,
188 llvm::UnnamedAddr::Global,
189 llvm::Visibility::Hidden,
190 ty,
191 );
192 if let Some(no_return) = no_return {
193 attributes::apply_to_llfn(callee, llvm::AttributePlace::Function, &[no_return]);
195 }
196 llvm::set_visibility(callee, llvm::Visibility::Hidden);
197
198 let args = args
199 .iter()
200 .enumerate()
201 .map(|(i, _)| llvm::get_param(llfn, i as c_uint))
202 .collect::<Vec<_>>();
203 let ret = bx.call(ty, callee, &args, None);
204 llvm::LLVMSetTailCall(ret, TRUE);
205 if output.is_some() {
206 bx.ret(ret);
207 } else {
208 bx.ret_void()
209 }
210 } else {
211 assert!(output.is_none());
212 bx.ret_void()
213 }
214}