]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_cranelift/src/allocator.rs
Merge commit '7d53619064ab7045c383644cb445052d2a3d46db' into sync_cg_clif-2023-02-09
[rust.git] / compiler / rustc_codegen_cranelift / src / allocator.rs
1 //! Allocator shim
2 // Adapted from rustc
3
4 use crate::prelude::*;
5
6 use rustc_ast::expand::allocator::{AllocatorKind, AllocatorTy, ALLOCATOR_METHODS};
7 use rustc_session::config::OomStrategy;
8 use rustc_span::symbol::sym;
9
10 /// Returns whether an allocator shim was created
11 pub(crate) fn codegen(
12     tcx: TyCtxt<'_>,
13     module: &mut impl Module,
14     unwind_context: &mut UnwindContext,
15 ) -> bool {
16     let any_dynamic_crate = tcx.dependency_formats(()).iter().any(|(_, list)| {
17         use rustc_middle::middle::dependency_format::Linkage;
18         list.iter().any(|&linkage| linkage == Linkage::Dynamic)
19     });
20     if any_dynamic_crate {
21         false
22     } else if let Some(kind) = tcx.allocator_kind(()) {
23         codegen_inner(
24             module,
25             unwind_context,
26             kind,
27             tcx.alloc_error_handler_kind(()).unwrap(),
28             tcx.sess.opts.unstable_opts.oom,
29         );
30         true
31     } else {
32         false
33     }
34 }
35
36 fn codegen_inner(
37     module: &mut impl Module,
38     unwind_context: &mut UnwindContext,
39     kind: AllocatorKind,
40     alloc_error_handler_kind: AllocatorKind,
41     oom_strategy: OomStrategy,
42 ) {
43     let usize_ty = module.target_config().pointer_type();
44
45     for method in ALLOCATOR_METHODS {
46         let mut arg_tys = Vec::with_capacity(method.inputs.len());
47         for ty in method.inputs.iter() {
48             match *ty {
49                 AllocatorTy::Layout => {
50                     arg_tys.push(usize_ty); // size
51                     arg_tys.push(usize_ty); // align
52                 }
53                 AllocatorTy::Ptr => arg_tys.push(usize_ty),
54                 AllocatorTy::Usize => arg_tys.push(usize_ty),
55
56                 AllocatorTy::ResultPtr | AllocatorTy::Unit => panic!("invalid allocator arg"),
57             }
58         }
59         let output = match method.output {
60             AllocatorTy::ResultPtr => Some(usize_ty),
61             AllocatorTy::Unit => None,
62
63             AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
64                 panic!("invalid allocator output")
65             }
66         };
67
68         let sig = Signature {
69             call_conv: module.target_config().default_call_conv,
70             params: arg_tys.iter().cloned().map(AbiParam::new).collect(),
71             returns: output.into_iter().map(AbiParam::new).collect(),
72         };
73         crate::common::create_wrapper_function(
74             module,
75             unwind_context,
76             sig,
77             &format!("__rust_{}", method.name),
78             &kind.fn_name(method.name),
79         );
80     }
81
82     let sig = Signature {
83         call_conv: module.target_config().default_call_conv,
84         params: vec![AbiParam::new(usize_ty), AbiParam::new(usize_ty)],
85         returns: vec![],
86     };
87     crate::common::create_wrapper_function(
88         module,
89         unwind_context,
90         sig,
91         "__rust_alloc_error_handler",
92         &alloc_error_handler_kind.fn_name(sym::oom),
93     );
94
95     let data_id = module.declare_data(OomStrategy::SYMBOL, Linkage::Export, false, false).unwrap();
96     let mut data_ctx = DataContext::new();
97     data_ctx.set_align(1);
98     let val = oom_strategy.should_panic();
99     data_ctx.define(Box::new([val]));
100     module.define_data(data_id, &data_ctx).unwrap();
101 }