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