33
44use cranelift_frontend:: { FunctionBuilder , FunctionBuilderContext } ;
55use 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,
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,74 +14,54 @@ 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) ,
48-
49- AllocatorTy :: ResultPtr | AllocatorTy :: Unit => panic ! ( "invalid allocator arg" ) ,
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
5037 }
51- }
52- let output = match method. output {
53- AllocatorTy :: ResultPtr => Some ( usize_ty) ,
54- AllocatorTy :: Unit => None ,
38+ AllocatorTy :: Ptr => arg_tys. push ( usize_ty) ,
39+ AllocatorTy :: Usize => arg_tys. push ( usize_ty) ,
5540
56- AllocatorTy :: Layout | AllocatorTy :: Usize | AllocatorTy :: Ptr => {
57- panic ! ( "invalid allocator output " )
41+ AllocatorTy :: Never | AllocatorTy :: ResultPtr | AllocatorTy :: Unit => {
42+ panic ! ( "invalid allocator arg " )
5843 }
59- } ;
60-
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- ) ;
44+ }
7245 }
73- }
46+ let output = match method. output {
47+ AllocatorTy :: ResultPtr => Some ( usize_ty) ,
48+ AllocatorTy :: Never | AllocatorTy :: Unit => None ,
49+
50+ AllocatorTy :: Layout | AllocatorTy :: Usize | AllocatorTy :: Ptr => {
51+ panic ! ( "invalid allocator output" )
52+ }
53+ } ;
7454
75- if alloc_error_handler_kind == AllocatorKind :: Default {
7655 let sig = Signature {
7756 call_conv : module. target_config ( ) . default_call_conv ,
78- params : vec ! [ AbiParam :: new ( usize_ty ) , AbiParam :: new( usize_ty ) ] ,
79- returns : vec ! [ ] ,
57+ params : arg_tys . iter ( ) . cloned ( ) . map ( AbiParam :: new) . collect ( ) ,
58+ returns : output . into_iter ( ) . map ( AbiParam :: new ) . collect ( ) ,
8059 } ;
8160 crate :: common:: create_wrapper_function (
8261 module,
8362 sig,
84- & mangle_internal_symbol ( tcx, & global_fn_name ( ALLOC_ERROR_HANDLER ) ) ,
85- & mangle_internal_symbol ( tcx, & default_fn_name ( ALLOC_ERROR_HANDLER ) ) ,
63+ & mangle_internal_symbol ( tcx, & global_fn_name ( method . name ) ) ,
64+ & mangle_internal_symbol ( tcx, & default_fn_name ( method . name ) ) ,
8665 ) ;
8766 }
8867
0 commit comments