33
44use cranelift_frontend:: { FunctionBuilder , FunctionBuilderContext } ;
55use rustc_ast:: expand:: allocator:: {
6- ALLOCATOR_METHODS , AllocatorKind , AllocatorTy , NO_ALLOC_SHIM_IS_UNSTABLE ,
7- alloc_error_handler_name, default_fn_name, global_fn_name,
6+ AllocatorMethod , AllocatorTy , NO_ALLOC_SHIM_IS_UNSTABLE , default_fn_name, global_fn_name,
87} ;
9- use rustc_codegen_ssa:: base:: allocator_kind_for_codegen;
8+ use rustc_codegen_ssa:: base:: { allocator_kind_for_codegen, allocator_shim_contents } ;
109use rustc_session:: config:: OomStrategy ;
1110use rustc_symbol_mangling:: mangle_internal_symbol;
1211
@@ -15,75 +14,57 @@ use crate::prelude::*;
1514/// Returns whether an allocator shim was created
1615pub ( crate ) fn codegen ( tcx : TyCtxt < ' _ > , module : & mut dyn Module ) -> bool {
1716 let Some ( kind) = allocator_kind_for_codegen ( tcx) else { return false } ;
18- codegen_inner (
19- tcx,
20- module,
21- kind,
22- tcx. alloc_error_handler_kind ( ( ) ) . unwrap ( ) ,
23- tcx. sess . opts . unstable_opts . oom ,
24- ) ;
17+ let methods = allocator_shim_contents ( tcx, kind) ;
18+ codegen_inner ( tcx, module, & methods, tcx. sess . opts . unstable_opts . oom ) ;
2519 true
2620}
2721
2822fn codegen_inner (
2923 tcx : TyCtxt < ' _ > ,
3024 module : & mut dyn Module ,
31- kind : AllocatorKind ,
32- alloc_error_handler_kind : AllocatorKind ,
25+ methods : & [ AllocatorMethod ] ,
3326 oom_strategy : OomStrategy ,
3427) {
3528 let usize_ty = module. target_config ( ) . pointer_type ( ) ;
3629
37- if kind == AllocatorKind :: Default {
38- for method in ALLOCATOR_METHODS {
39- let mut arg_tys = Vec :: with_capacity ( method. inputs . len ( ) ) ;
40- for input in method. inputs . iter ( ) {
41- match input. ty {
42- AllocatorTy :: Layout => {
43- arg_tys. push ( usize_ty) ; // size
44- arg_tys. push ( usize_ty) ; // align
45- }
46- AllocatorTy :: Ptr => arg_tys. push ( usize_ty) ,
47- AllocatorTy :: Usize => arg_tys. push ( usize_ty) ,
30+ for method in methods {
31+ let mut arg_tys = Vec :: with_capacity ( method. inputs . len ( ) ) ;
32+ for input in method. inputs . iter ( ) {
33+ match input. ty {
34+ AllocatorTy :: Layout => {
35+ arg_tys. push ( usize_ty) ; // size
36+ arg_tys. push ( usize_ty) ; // align
37+ }
38+ AllocatorTy :: Ptr => arg_tys. push ( usize_ty) ,
39+ AllocatorTy :: Usize => arg_tys. push ( usize_ty) ,
4840
49- AllocatorTy :: ResultPtr | AllocatorTy :: Unit => panic ! ( "invalid allocator arg" ) ,
41+ AllocatorTy :: Never | AllocatorTy :: ResultPtr | AllocatorTy :: Unit => {
42+ panic ! ( "invalid allocator arg" )
5043 }
5144 }
52- let output = match method. output {
53- AllocatorTy :: ResultPtr => Some ( usize_ty) ,
54- AllocatorTy :: Unit => None ,
45+ }
46+ let output = match method. output {
47+ AllocatorTy :: ResultPtr => Some ( usize_ty) ,
48+ AllocatorTy :: Never | AllocatorTy :: Unit => None ,
5549
56- AllocatorTy :: Layout | AllocatorTy :: Usize | AllocatorTy :: Ptr => {
57- panic ! ( "invalid allocator output" )
58- }
59- } ;
50+ AllocatorTy :: Layout | AllocatorTy :: Usize | AllocatorTy :: Ptr => {
51+ panic ! ( "invalid allocator output" )
52+ }
53+ } ;
6054
61- let sig = Signature {
62- call_conv : module. target_config ( ) . default_call_conv ,
63- params : arg_tys. iter ( ) . cloned ( ) . map ( AbiParam :: new) . collect ( ) ,
64- returns : output. into_iter ( ) . map ( AbiParam :: new) . collect ( ) ,
65- } ;
66- crate :: common:: create_wrapper_function (
67- module,
68- sig,
69- & mangle_internal_symbol ( tcx, & global_fn_name ( method. name ) ) ,
70- & mangle_internal_symbol ( tcx, & default_fn_name ( method. name ) ) ,
71- ) ;
72- }
55+ let sig = Signature {
56+ call_conv : module. target_config ( ) . default_call_conv ,
57+ params : arg_tys. iter ( ) . cloned ( ) . map ( AbiParam :: new) . collect ( ) ,
58+ returns : output. into_iter ( ) . map ( AbiParam :: new) . collect ( ) ,
59+ } ;
60+ crate :: common:: create_wrapper_function (
61+ module,
62+ sig,
63+ & mangle_internal_symbol ( tcx, & global_fn_name ( method. name ) ) ,
64+ & mangle_internal_symbol ( tcx, & default_fn_name ( method. name ) ) ,
65+ ) ;
7366 }
7467
75- let sig = Signature {
76- call_conv : module. target_config ( ) . default_call_conv ,
77- params : vec ! [ AbiParam :: new( usize_ty) , AbiParam :: new( usize_ty) ] ,
78- returns : vec ! [ ] ,
79- } ;
80- crate :: common:: create_wrapper_function (
81- module,
82- sig,
83- & mangle_internal_symbol ( tcx, "__rust_alloc_error_handler" ) ,
84- & mangle_internal_symbol ( tcx, alloc_error_handler_name ( alloc_error_handler_kind) ) ,
85- ) ;
86-
8768 {
8869 let sig = Signature {
8970 call_conv : module. target_config ( ) . default_call_conv ,
0 commit comments