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