1
- #[ cfg( feature = "master" ) ]
2
- use gccjit:: FnAttribute ;
3
- use gccjit:: { Context , FunctionType , GlobalKind , ToRValue , Type } ;
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,
7
- } ;
8
- use rustc_middle:: bug;
1
+ use gccjit:: GlobalKind ;
2
+ use rustc_ast:: expand:: allocator:: NO_ALLOC_SHIM_IS_UNSTABLE ;
9
3
use rustc_middle:: ty:: TyCtxt ;
10
4
use rustc_session:: config:: OomStrategy ;
11
5
12
6
use crate :: GccContext ;
13
7
14
- pub ( crate ) unsafe fn codegen (
15
- tcx : TyCtxt < ' _ > ,
16
- mods : & mut GccContext ,
17
- _module_name : & str ,
18
- kind : AllocatorKind ,
19
- alloc_error_handler_kind : AllocatorKind ,
20
- ) {
8
+ pub ( crate ) unsafe fn codegen ( tcx : TyCtxt < ' _ > , mods : & mut GccContext , _module_name : & str ) {
21
9
let context = & mods. context ;
22
- let usize = match tcx. sess . target . pointer_width {
23
- 16 => context. new_type :: < u16 > ( ) ,
24
- 32 => context. new_type :: < u32 > ( ) ,
25
- 64 => context. new_type :: < u64 > ( ) ,
26
- tws => bug ! ( "Unsupported target word size for int: {}" , tws) ,
27
- } ;
28
10
let i8 = context. new_type :: < i8 > ( ) ;
29
- let i8p = i8. make_pointer ( ) ;
30
-
31
- if kind == AllocatorKind :: Default {
32
- for method in ALLOCATOR_METHODS {
33
- let mut types = Vec :: with_capacity ( method. inputs . len ( ) ) ;
34
- for input in method. inputs . iter ( ) {
35
- match input. ty {
36
- AllocatorTy :: Layout => {
37
- types. push ( usize) ;
38
- types. push ( usize) ;
39
- }
40
- AllocatorTy :: Ptr => types. push ( i8p) ,
41
- AllocatorTy :: Usize => types. push ( usize) ,
42
-
43
- AllocatorTy :: ResultPtr | AllocatorTy :: Unit => panic ! ( "invalid allocator arg" ) ,
44
- }
45
- }
46
- let output = match method. output {
47
- AllocatorTy :: ResultPtr => Some ( i8p) ,
48
- AllocatorTy :: Unit => None ,
49
-
50
- AllocatorTy :: Layout | AllocatorTy :: Usize | AllocatorTy :: Ptr => {
51
- panic ! ( "invalid allocator output" )
52
- }
53
- } ;
54
- let from_name = global_fn_name ( method. name ) ;
55
- let to_name = default_fn_name ( method. name ) ;
56
-
57
- create_wrapper_function ( tcx, context, & from_name, & to_name, & types, output) ;
58
- }
59
- }
60
-
61
- // FIXME(bjorn3): Add noreturn attribute
62
- create_wrapper_function (
63
- tcx,
64
- context,
65
- "__rust_alloc_error_handler" ,
66
- alloc_error_handler_name ( alloc_error_handler_kind) ,
67
- & [ usize, usize] ,
68
- None ,
69
- ) ;
70
11
71
12
let name = OomStrategy :: SYMBOL . to_string ( ) ;
72
13
let global = context. new_global ( None , GlobalKind :: Exported , i8, name) ;
@@ -79,77 +20,3 @@ pub(crate) unsafe fn codegen(
79
20
let value = context. new_rvalue_from_int ( i8, 0 ) ;
80
21
global. global_set_initializer_rvalue ( value) ;
81
22
}
82
-
83
- fn create_wrapper_function (
84
- tcx : TyCtxt < ' _ > ,
85
- context : & Context < ' _ > ,
86
- from_name : & str ,
87
- to_name : & str ,
88
- types : & [ Type < ' _ > ] ,
89
- output : Option < Type < ' _ > > ,
90
- ) {
91
- let void = context. new_type :: < ( ) > ( ) ;
92
-
93
- let args: Vec < _ > = types
94
- . iter ( )
95
- . enumerate ( )
96
- . map ( |( index, typ) | context. new_parameter ( None , * typ, format ! ( "param{}" , index) ) )
97
- . collect ( ) ;
98
- let func = context. new_function (
99
- None ,
100
- FunctionType :: Exported ,
101
- output. unwrap_or ( void) ,
102
- & args,
103
- from_name,
104
- false ,
105
- ) ;
106
-
107
- #[ cfg( feature = "master" ) ]
108
- match tcx. sess . default_visibility ( ) {
109
- rustc_target:: spec:: SymbolVisibility :: Hidden => {
110
- func. add_attribute ( FnAttribute :: Visibility ( gccjit:: Visibility :: Hidden ) )
111
- }
112
- rustc_target:: spec:: SymbolVisibility :: Protected => {
113
- func. add_attribute ( FnAttribute :: Visibility ( gccjit:: Visibility :: Protected ) )
114
- }
115
- rustc_target:: spec:: SymbolVisibility :: Interposable => { }
116
- }
117
-
118
- if tcx. sess . must_emit_unwind_tables ( ) {
119
- // TODO(antoyo): emit unwind tables.
120
- }
121
-
122
- let args: Vec < _ > = types
123
- . iter ( )
124
- . enumerate ( )
125
- . map ( |( index, typ) | context. new_parameter ( None , * typ, format ! ( "param{}" , index) ) )
126
- . collect ( ) ;
127
- let callee = context. new_function (
128
- None ,
129
- FunctionType :: Extern ,
130
- output. unwrap_or ( void) ,
131
- & args,
132
- to_name,
133
- false ,
134
- ) ;
135
- #[ cfg( feature = "master" ) ]
136
- callee. add_attribute ( FnAttribute :: Visibility ( gccjit:: Visibility :: Hidden ) ) ;
137
-
138
- let block = func. new_block ( "entry" ) ;
139
-
140
- let args = args
141
- . iter ( )
142
- . enumerate ( )
143
- . map ( |( i, _) | func. get_param ( i as i32 ) . to_rvalue ( ) )
144
- . collect :: < Vec < _ > > ( ) ;
145
- let ret = context. new_call ( None , callee, & args) ;
146
- //llvm::LLVMSetTailCall(ret, True);
147
- if output. is_some ( ) {
148
- block. end_with_return ( None , ret) ;
149
- } else {
150
- block. end_with_void_return ( None ) ;
151
- }
152
-
153
- // TODO(@Commeownist): Check if we need to emit some extra debugging info in certain circumstances
154
- // as described in https://github.com/rust-lang/rust/commit/77a96ed5646f7c3ee8897693decc4626fe380643
155
- }
0 commit comments