3
3
4
4
use crate :: prelude:: * ;
5
5
6
- use rustc_ast:: expand:: allocator:: { AllocatorKind , AllocatorTy , ALLOCATOR_METHODS } ;
6
+ use rustc_ast:: expand:: allocator:: {
7
+ alloc_error_handler_name, default_fn_name, global_fn_name, AllocatorKind , AllocatorTy ,
8
+ ALLOCATOR_METHODS , NO_ALLOC_SHIM_IS_UNSTABLE ,
9
+ } ;
7
10
use rustc_codegen_ssa:: base:: allocator_kind_for_codegen;
8
11
use rustc_session:: config:: OomStrategy ;
9
- use rustc_span:: symbol:: sym;
10
12
11
13
/// Returns whether an allocator shim was created
12
14
pub ( crate ) fn codegen (
@@ -34,41 +36,43 @@ fn codegen_inner(
34
36
) {
35
37
let usize_ty = module. target_config ( ) . pointer_type ( ) ;
36
38
37
- for method in ALLOCATOR_METHODS {
38
- let mut arg_tys = Vec :: with_capacity ( method. inputs . len ( ) ) ;
39
- for ty in method. inputs . iter ( ) {
40
- match * 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) ,
39
+ if kind == AllocatorKind :: Default {
40
+ for method in ALLOCATOR_METHODS {
41
+ let mut arg_tys = Vec :: with_capacity ( method. inputs . len ( ) ) ;
42
+ for ty in method. inputs . iter ( ) {
43
+ match * ty {
44
+ AllocatorTy :: Layout => {
45
+ arg_tys. push ( usize_ty) ; // size
46
+ arg_tys. push ( usize_ty) ; // align
47
+ }
48
+ AllocatorTy :: Ptr => arg_tys. push ( usize_ty) ,
49
+ AllocatorTy :: Usize => arg_tys. push ( usize_ty) ,
47
50
48
- AllocatorTy :: ResultPtr | AllocatorTy :: Unit => panic ! ( "invalid allocator arg" ) ,
51
+ AllocatorTy :: ResultPtr | AllocatorTy :: Unit => panic ! ( "invalid allocator arg" ) ,
52
+ }
49
53
}
50
- }
51
- let output = match method. output {
52
- AllocatorTy :: ResultPtr => Some ( usize_ty) ,
53
- AllocatorTy :: Unit => None ,
54
+ let output = match method. output {
55
+ AllocatorTy :: ResultPtr => Some ( usize_ty) ,
56
+ AllocatorTy :: Unit => None ,
54
57
55
- AllocatorTy :: Layout | AllocatorTy :: Usize | AllocatorTy :: Ptr => {
56
- panic ! ( "invalid allocator output" )
57
- }
58
- } ;
58
+ AllocatorTy :: Layout | AllocatorTy :: Usize | AllocatorTy :: Ptr => {
59
+ panic ! ( "invalid allocator output" )
60
+ }
61
+ } ;
59
62
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
- unwind_context,
68
- sig,
69
- & format ! ( "__rust_{}" , method. name) ,
70
- & kind. fn_name ( method. name ) ,
71
- ) ;
63
+ let sig = Signature {
64
+ call_conv : module. target_config ( ) . default_call_conv ,
65
+ params : arg_tys. iter ( ) . cloned ( ) . map ( AbiParam :: new) . collect ( ) ,
66
+ returns : output. into_iter ( ) . map ( AbiParam :: new) . collect ( ) ,
67
+ } ;
68
+ crate :: common:: create_wrapper_function (
69
+ module,
70
+ unwind_context,
71
+ sig,
72
+ & global_fn_name ( method. name ) ,
73
+ & default_fn_name ( method. name ) ,
74
+ ) ;
75
+ }
72
76
}
73
77
74
78
let sig = Signature {
@@ -81,7 +85,7 @@ fn codegen_inner(
81
85
unwind_context,
82
86
sig,
83
87
"__rust_alloc_error_handler" ,
84
- & alloc_error_handler_kind . fn_name ( sym :: oom ) ,
88
+ & alloc_error_handler_name ( alloc_error_handler_kind ) ,
85
89
) ;
86
90
87
91
let data_id = module. declare_data ( OomStrategy :: SYMBOL , Linkage :: Export , false , false ) . unwrap ( ) ;
@@ -90,4 +94,11 @@ fn codegen_inner(
90
94
let val = oom_strategy. should_panic ( ) ;
91
95
data. define ( Box :: new ( [ val] ) ) ;
92
96
module. define_data ( data_id, & data) . unwrap ( ) ;
97
+
98
+ let data_id =
99
+ module. declare_data ( NO_ALLOC_SHIM_IS_UNSTABLE , Linkage :: Export , false , false ) . unwrap ( ) ;
100
+ let mut data = DataDescription :: new ( ) ;
101
+ data. set_align ( 1 ) ;
102
+ data. define ( Box :: new ( [ 0 ] ) ) ;
103
+ module. define_data ( data_id, & data) . unwrap ( ) ;
93
104
}
0 commit comments