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;
11
12use crate::builder::SBuilder;
13use crate::declare::declare_simple_fn;
14use crate::llvm::{self, False, True, Type};
15use crate::{SimpleCx, attributes, debuginfo};
16
17pub(crate) unsafe fn codegen(
18 tcx: TyCtxt<'_>,
19 cx: SimpleCx<'_>,
20 module_name: &str,
21 kind: AllocatorKind,
22 alloc_error_handler_kind: AllocatorKind,
23) {
24 let usize = match tcx.sess.target.pointer_width {
25 16 => cx.type_i16(),
26 32 => cx.type_i32(),
27 64 => cx.type_i64(),
28 tws => bug!("Unsupported target word size for int: {}", tws),
29 };
30 let i8 = cx.type_i8();
31 let i8p = cx.type_ptr();
32
33 if kind == AllocatorKind::Default {
34 for method in ALLOCATOR_METHODS {
35 let mut args = Vec::with_capacity(method.inputs.len());
36 for input in method.inputs.iter() {
37 match input.ty {
38 AllocatorTy::Layout => {
39 args.push(usize); args.push(usize); }
42 AllocatorTy::Ptr => args.push(i8p),
43 AllocatorTy::Usize => args.push(usize),
44
45 AllocatorTy::ResultPtr | AllocatorTy::Unit => panic!("invalid allocator arg"),
46 }
47 }
48 let output = match method.output {
49 AllocatorTy::ResultPtr => Some(i8p),
50 AllocatorTy::Unit => None,
51
52 AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
53 panic!("invalid allocator output")
54 }
55 };
56
57 let from_name = mangle_internal_symbol(tcx, &global_fn_name(method.name));
58 let to_name = mangle_internal_symbol(tcx, &default_fn_name(method.name));
59
60 create_wrapper_function(tcx, &cx, &from_name, Some(&to_name), &args, output, false);
61 }
62 }
63
64 create_wrapper_function(
66 tcx,
67 &cx,
68 &mangle_internal_symbol(tcx, "__rust_alloc_error_handler"),
69 Some(&mangle_internal_symbol(tcx, alloc_error_handler_name(alloc_error_handler_kind))),
70 &[usize, usize], None,
72 true,
73 );
74
75 unsafe {
76 let name = mangle_internal_symbol(tcx, OomStrategy::SYMBOL);
78 let ll_g = cx.declare_global(&name, i8);
79 llvm::set_visibility(ll_g, llvm::Visibility::from_generic(tcx.sess.default_visibility()));
80 let val = tcx.sess.opts.unstable_opts.oom.should_panic();
81 let llval = llvm::LLVMConstInt(i8, val as u64, False);
82 llvm::set_initializer(ll_g, llval);
83
84 create_wrapper_function(
86 tcx,
87 &cx,
88 &mangle_internal_symbol(tcx, NO_ALLOC_SHIM_IS_UNSTABLE),
89 None,
90 &[],
91 None,
92 false,
93 );
94 }
95
96 if tcx.sess.opts.debuginfo != DebugInfo::None {
97 let dbg_cx = debuginfo::CodegenUnitDebugContext::new(cx.llmod);
98 debuginfo::metadata::build_compile_unit_di_node(tcx, module_name, &dbg_cx);
99 dbg_cx.finalize(tcx.sess);
100 }
101}
102
103fn create_wrapper_function(
104 tcx: TyCtxt<'_>,
105 cx: &SimpleCx<'_>,
106 from_name: &str,
107 to_name: Option<&str>,
108 args: &[&Type],
109 output: Option<&Type>,
110 no_return: bool,
111) {
112 let ty = cx.type_func(args, output.unwrap_or_else(|| cx.type_void()));
113 let llfn = declare_simple_fn(
114 &cx,
115 from_name,
116 llvm::CallConv::CCallConv,
117 llvm::UnnamedAddr::Global,
118 llvm::Visibility::from_generic(tcx.sess.default_visibility()),
119 ty,
120 );
121 let no_return = if no_return {
122 let no_return = llvm::AttributeKind::NoReturn.create_attr(cx.llcx);
124 attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[no_return]);
125 Some(no_return)
126 } else {
127 None
128 };
129
130 if tcx.sess.must_emit_unwind_tables() {
131 let uwtable =
132 attributes::uwtable_attr(cx.llcx, tcx.sess.opts.unstable_opts.use_sync_unwind);
133 attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[uwtable]);
134 }
135
136 let llbb = unsafe { llvm::LLVMAppendBasicBlockInContext(cx.llcx, llfn, c"entry".as_ptr()) };
137 let mut bx = SBuilder::build(&cx, llbb);
138
139 if let Some(to_name) = to_name {
140 let callee = declare_simple_fn(
141 &cx,
142 to_name,
143 llvm::CallConv::CCallConv,
144 llvm::UnnamedAddr::Global,
145 llvm::Visibility::Hidden,
146 ty,
147 );
148 if let Some(no_return) = no_return {
149 attributes::apply_to_llfn(callee, llvm::AttributePlace::Function, &[no_return]);
151 }
152 llvm::set_visibility(callee, llvm::Visibility::Hidden);
153
154 let args = args
155 .iter()
156 .enumerate()
157 .map(|(i, _)| llvm::get_param(llfn, i as c_uint))
158 .collect::<Vec<_>>();
159 let ret = bx.call(ty, callee, &args, None);
160 llvm::LLVMSetTailCall(ret, True);
161 if output.is_some() {
162 bx.ret(ret);
163 } else {
164 bx.ret_void()
165 }
166 } else {
167 assert!(output.is_none());
168 bx.ret_void()
169 }
170}