|
2 | 2 | // Adapted from rustc |
3 | 3 |
|
4 | 4 | use rustc_ast::expand::allocator::{ |
5 | | - ALLOCATOR_METHODS, AllocatorKind, AllocatorTy, NO_ALLOC_SHIM_IS_UNSTABLE, |
6 | | - alloc_error_handler_name, default_fn_name, global_fn_name, |
| 5 | + AllocatorKind, NO_ALLOC_SHIM_IS_UNSTABLE, alloc_error_handler_name, |
7 | 6 | }; |
8 | | -use rustc_codegen_ssa::base::allocator_kind_for_codegen; |
| 7 | +use rustc_codegen_ssa::base::needs_allocator_shim; |
9 | 8 | use rustc_session::config::OomStrategy; |
10 | 9 | use rustc_symbol_mangling::mangle_internal_symbol; |
11 | 10 |
|
12 | 11 | use crate::prelude::*; |
13 | 12 |
|
14 | 13 | /// Returns whether an allocator shim was created |
15 | 14 | pub(crate) fn codegen(tcx: TyCtxt<'_>, module: &mut dyn Module) -> bool { |
16 | | - let Some(kind) = allocator_kind_for_codegen(tcx) else { return false }; |
17 | | - codegen_inner( |
18 | | - tcx, |
19 | | - module, |
20 | | - kind, |
21 | | - tcx.alloc_error_handler_kind(()).unwrap(), |
22 | | - tcx.sess.opts.unstable_opts.oom, |
23 | | - ); |
24 | | - true |
| 15 | + if needs_allocator_shim(tcx) { |
| 16 | + codegen_inner( |
| 17 | + tcx, |
| 18 | + module, |
| 19 | + tcx.alloc_error_handler_kind(()).unwrap(), |
| 20 | + tcx.sess.opts.unstable_opts.oom, |
| 21 | + ); |
| 22 | + true |
| 23 | + } else { |
| 24 | + false |
| 25 | + } |
25 | 26 | } |
26 | 27 |
|
27 | 28 | fn codegen_inner( |
28 | 29 | tcx: TyCtxt<'_>, |
29 | 30 | module: &mut dyn Module, |
30 | | - kind: AllocatorKind, |
31 | 31 | alloc_error_handler_kind: AllocatorKind, |
32 | 32 | oom_strategy: OomStrategy, |
33 | 33 | ) { |
34 | 34 | let usize_ty = module.target_config().pointer_type(); |
35 | 35 |
|
36 | | - if kind == AllocatorKind::Default { |
37 | | - for method in ALLOCATOR_METHODS { |
38 | | - let mut arg_tys = Vec::with_capacity(method.inputs.len()); |
39 | | - for input in method.inputs.iter() { |
40 | | - match input.ty { |
41 | | - AllocatorTy::Layout => { |
42 | | - arg_tys.push(usize_ty); // size |
43 | | - arg_tys.push(usize_ty); // align |
44 | | - } |
45 | | - AllocatorTy::Ptr => arg_tys.push(usize_ty), |
46 | | - AllocatorTy::Usize => arg_tys.push(usize_ty), |
47 | | - |
48 | | - AllocatorTy::ResultPtr | AllocatorTy::Unit => panic!("invalid allocator arg"), |
49 | | - } |
50 | | - } |
51 | | - let output = match method.output { |
52 | | - AllocatorTy::ResultPtr => Some(usize_ty), |
53 | | - AllocatorTy::Unit => None, |
54 | | - |
55 | | - AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => { |
56 | | - panic!("invalid allocator output") |
57 | | - } |
58 | | - }; |
59 | | - |
60 | | - let sig = Signature { |
61 | | - call_conv: module.target_config().default_call_conv, |
62 | | - params: arg_tys.iter().cloned().map(AbiParam::new).collect(), |
63 | | - returns: output.into_iter().map(AbiParam::new).collect(), |
64 | | - }; |
65 | | - crate::common::create_wrapper_function( |
66 | | - module, |
67 | | - sig, |
68 | | - &mangle_internal_symbol(tcx, &global_fn_name(method.name)), |
69 | | - &mangle_internal_symbol(tcx, &default_fn_name(method.name)), |
70 | | - ); |
71 | | - } |
72 | | - } |
73 | | - |
74 | 36 | let sig = Signature { |
75 | 37 | call_conv: module.target_config().default_call_conv, |
76 | 38 | params: vec![AbiParam::new(usize_ty), AbiParam::new(usize_ty)], |
|
0 commit comments