rustc_codegen_llvm/
allocator.rs

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_middle::bug;
7use rustc_middle::ty::TyCtxt;
8use rustc_session::config::{DebugInfo, OomStrategy};
9
10use crate::common::AsCCharPtr;
11use crate::llvm::{self, Context, False, Module, True, Type};
12use crate::{ModuleLlvm, attributes, debuginfo};
13
14pub(crate) unsafe fn codegen(
15    tcx: TyCtxt<'_>,
16    module_llvm: &mut ModuleLlvm,
17    module_name: &str,
18    kind: AllocatorKind,
19    alloc_error_handler_kind: AllocatorKind,
20) {
21    let llcx = &*module_llvm.llcx;
22    let llmod = module_llvm.llmod();
23    let usize = unsafe {
24        match tcx.sess.target.pointer_width {
25            16 => llvm::LLVMInt16TypeInContext(llcx),
26            32 => llvm::LLVMInt32TypeInContext(llcx),
27            64 => llvm::LLVMInt64TypeInContext(llcx),
28            tws => bug!("Unsupported target word size for int: {}", tws),
29        }
30    };
31    let i8 = unsafe { llvm::LLVMInt8TypeInContext(llcx) };
32    let i8p = unsafe { llvm::LLVMPointerTypeInContext(llcx, 0) };
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); // size
41                        args.push(usize); // align
42                    }
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 = global_fn_name(method.name);
59            let to_name = default_fn_name(method.name);
60
61            create_wrapper_function(tcx, llcx, llmod, &from_name, &to_name, &args, output, false);
62        }
63    }
64
65    // rust alloc error handler
66    create_wrapper_function(
67        tcx,
68        llcx,
69        llmod,
70        "__rust_alloc_error_handler",
71        alloc_error_handler_name(alloc_error_handler_kind),
72        &[usize, usize], // size, align
73        None,
74        true,
75    );
76
77    unsafe {
78        // __rust_alloc_error_handler_should_panic
79        let name = OomStrategy::SYMBOL;
80        let ll_g = llvm::LLVMRustGetOrInsertGlobal(llmod, name.as_c_char_ptr(), name.len(), i8);
81        llvm::set_visibility(ll_g, llvm::Visibility::from_generic(tcx.sess.default_visibility()));
82        let val = tcx.sess.opts.unstable_opts.oom.should_panic();
83        let llval = llvm::LLVMConstInt(i8, val as u64, False);
84        llvm::set_initializer(ll_g, llval);
85
86        let name = NO_ALLOC_SHIM_IS_UNSTABLE;
87        let ll_g = llvm::LLVMRustGetOrInsertGlobal(llmod, name.as_c_char_ptr(), name.len(), i8);
88        llvm::set_visibility(ll_g, llvm::Visibility::from_generic(tcx.sess.default_visibility()));
89        let llval = llvm::LLVMConstInt(i8, 0, False);
90        llvm::set_initializer(ll_g, llval);
91    }
92
93    if tcx.sess.opts.debuginfo != DebugInfo::None {
94        let dbg_cx = debuginfo::CodegenUnitDebugContext::new(llmod);
95        debuginfo::metadata::build_compile_unit_di_node(tcx, module_name, &dbg_cx);
96        dbg_cx.finalize(tcx.sess);
97    }
98}
99
100fn create_wrapper_function(
101    tcx: TyCtxt<'_>,
102    llcx: &Context,
103    llmod: &Module,
104    from_name: &str,
105    to_name: &str,
106    args: &[&Type],
107    output: Option<&Type>,
108    no_return: bool,
109) {
110    unsafe {
111        let ty = llvm::LLVMFunctionType(
112            output.unwrap_or_else(|| llvm::LLVMVoidTypeInContext(llcx)),
113            args.as_ptr(),
114            args.len() as c_uint,
115            False,
116        );
117        let llfn = llvm::LLVMRustGetOrInsertFunction(
118            llmod,
119            from_name.as_c_char_ptr(),
120            from_name.len(),
121            ty,
122        );
123        let no_return = if no_return {
124            // -> ! DIFlagNoReturn
125            let no_return = llvm::AttributeKind::NoReturn.create_attr(llcx);
126            attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[no_return]);
127            Some(no_return)
128        } else {
129            None
130        };
131
132        llvm::set_visibility(llfn, llvm::Visibility::from_generic(tcx.sess.default_visibility()));
133
134        if tcx.sess.must_emit_unwind_tables() {
135            let uwtable =
136                attributes::uwtable_attr(llcx, tcx.sess.opts.unstable_opts.use_sync_unwind);
137            attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[uwtable]);
138        }
139
140        let callee =
141            llvm::LLVMRustGetOrInsertFunction(llmod, to_name.as_c_char_ptr(), to_name.len(), ty);
142        if let Some(no_return) = no_return {
143            // -> ! DIFlagNoReturn
144            attributes::apply_to_llfn(callee, llvm::AttributePlace::Function, &[no_return]);
145        }
146        llvm::set_visibility(callee, llvm::Visibility::Hidden);
147
148        let llbb = llvm::LLVMAppendBasicBlockInContext(llcx, llfn, c"entry".as_ptr());
149
150        let llbuilder = llvm::LLVMCreateBuilderInContext(llcx);
151        llvm::LLVMPositionBuilderAtEnd(llbuilder, llbb);
152        let args = args
153            .iter()
154            .enumerate()
155            .map(|(i, _)| llvm::LLVMGetParam(llfn, i as c_uint))
156            .collect::<Vec<_>>();
157        let ret = llvm::LLVMBuildCallWithOperandBundles(
158            llbuilder,
159            ty,
160            callee,
161            args.as_ptr(),
162            args.len() as c_uint,
163            [].as_ptr(),
164            0 as c_uint,
165            c"".as_ptr(),
166        );
167        llvm::LLVMSetTailCall(ret, True);
168        if output.is_some() {
169            llvm::LLVMBuildRet(llbuilder, ret);
170        } else {
171            llvm::LLVMBuildRetVoid(llbuilder);
172        }
173        llvm::LLVMDisposeBuilder(llbuilder);
174    }
175}