@@ -9,14 +9,13 @@ pub fn trans_fn<'a, 'clif, 'tcx: 'a, B: Backend + 'static>(
9
9
) {
10
10
let tcx = cx. tcx ;
11
11
12
- // Step 1. Get mir
13
12
let mir = tcx. instance_mir ( instance. def ) ;
14
13
15
- // Step 2. Check fn sig for u128 and i128 and replace those functions with a trap.
14
+ // Check fn sig for u128 and i128 and replace those functions with a trap.
16
15
{
17
16
// FIXME implement u128 and i128 support
18
17
19
- // Step 2a. Check sig for u128 and i128
18
+ // Check sig for u128 and i128
20
19
let fn_sig = tcx. normalize_erasing_late_bound_regions ( ParamEnv :: reveal_all ( ) , & instance. fn_sig ( tcx) ) ;
21
20
22
21
struct UI128Visitor < ' a , ' tcx : ' a > ( TyCtxt < ' a , ' tcx , ' tcx > , bool ) ;
@@ -35,12 +34,12 @@ pub fn trans_fn<'a, 'clif, 'tcx: 'a, B: Backend + 'static>(
35
34
let mut visitor = UI128Visitor ( tcx, false ) ;
36
35
fn_sig. visit_with ( & mut visitor) ;
37
36
38
- // Step 2b. If found replace function with a trap.
37
+ //If found replace function with a trap.
39
38
if visitor. 1 {
40
39
tcx. sess . warn ( "u128 and i128 are not yet supported. \
41
40
Functions using these as args will be replaced with a trap.") ;
42
41
43
- // Step 2b1. Declare function with fake signature
42
+ // Declare function with fake signature
44
43
let sig = Signature {
45
44
params : vec ! [ AbiParam :: new( types:: INVALID ) ] ,
46
45
returns : vec ! [ ] ,
@@ -49,7 +48,7 @@ pub fn trans_fn<'a, 'clif, 'tcx: 'a, B: Backend + 'static>(
49
48
let name = tcx. symbol_name ( instance) . as_str ( ) ;
50
49
let func_id = cx. module . declare_function ( & * name, linkage, & sig) . unwrap ( ) ;
51
50
52
- // Step 2b2. Create trapping function
51
+ // Create trapping function
53
52
let mut func = Function :: with_name_signature ( ExternalName :: user ( 0 , 0 ) , sig) ;
54
53
let mut func_ctx = FunctionBuilderContext :: new ( ) ;
55
54
let mut bcx = FunctionBuilder :: new ( & mut func, & mut func_ctx) ;
@@ -79,7 +78,7 @@ pub fn trans_fn<'a, 'clif, 'tcx: 'a, B: Backend + 'static>(
79
78
fx. bcx . seal_all_blocks ( ) ;
80
79
fx. bcx . finalize ( ) ;
81
80
82
- // Step 2b3. Define function
81
+ // Define function
83
82
cx. caches . context . func = func;
84
83
cx. module
85
84
. define_function ( func_id, & mut cx. caches . context )
@@ -89,27 +88,27 @@ pub fn trans_fn<'a, 'clif, 'tcx: 'a, B: Backend + 'static>(
89
88
}
90
89
}
91
90
92
- // Step 3. Declare function
91
+ // Declare function
93
92
let ( name, sig) = get_function_name_and_sig ( tcx, instance, false ) ;
94
93
let func_id = cx. module . declare_function ( & name, linkage, & sig) . unwrap ( ) ;
95
94
let mut debug_context = cx
96
95
. debug_context
97
96
. as_mut ( )
98
97
. map ( |debug_context| FunctionDebugContext :: new ( tcx, debug_context, mir, & name, & sig) ) ;
99
98
100
- // Step 4. Make FunctionBuilder
99
+ // Make FunctionBuilder
101
100
let mut func = Function :: with_name_signature ( ExternalName :: user ( 0 , 0 ) , sig) ;
102
101
let mut func_ctx = FunctionBuilderContext :: new ( ) ;
103
102
let mut bcx = FunctionBuilder :: new ( & mut func, & mut func_ctx) ;
104
103
105
- // Step 5. Predefine ebb's
104
+ // Predefine ebb's
106
105
let start_ebb = bcx. create_ebb ( ) ;
107
106
let mut ebb_map: HashMap < BasicBlock , Ebb > = HashMap :: new ( ) ;
108
107
for ( bb, _bb_data) in mir. basic_blocks ( ) . iter_enumerated ( ) {
109
108
ebb_map. insert ( bb, bcx. create_ebb ( ) ) ;
110
109
}
111
110
112
- // Step 6. Make FunctionCx
111
+ // Make FunctionCx
113
112
let pointer_type = cx. module . target_config ( ) . pointer_type ( ) ;
114
113
let clif_comments = crate :: pretty_clif:: CommentWriter :: new ( tcx, instance) ;
115
114
@@ -131,38 +130,46 @@ pub fn trans_fn<'a, 'clif, 'tcx: 'a, B: Backend + 'static>(
131
130
source_info_set : indexmap:: IndexSet :: new ( ) ,
132
131
} ;
133
132
134
- // Step 7. Codegen function
135
133
with_unimpl_span ( fx. mir . span , || {
136
134
crate :: abi:: codegen_fn_prelude ( & mut fx, start_ebb) ;
137
135
codegen_fn_content ( & mut fx) ;
138
136
} ) ;
139
- let source_info_set = fx. source_info_set . clone ( ) ;
140
137
141
- // Step 8. Write function to file for debugging
138
+ // Recover all necessary data from fx, before accessing func will prevent future access to it.
139
+ let instance = fx. instance ;
140
+ let clif_comments = fx. clif_comments ;
141
+ let source_info_set = fx. source_info_set ;
142
+
142
143
#[ cfg( debug_assertions) ]
143
- fx . write_clif_file ( ) ;
144
+ crate :: pretty_clif :: write_clif_file ( cx . tcx , "unopt" , instance , & func , & clif_comments , None ) ;
144
145
145
- // Step 9. Verify function
146
- verify_func ( tcx, fx . clif_comments , & func) ;
146
+ // Verify function
147
+ verify_func ( tcx, & clif_comments, & func) ;
147
148
148
- // Step 10. Define function
149
- cx. caches . context . func = func;
149
+ // Define function
150
+ let context = & mut cx. caches . context ;
151
+ context. func = func;
150
152
cx. module
151
- . define_function ( func_id, & mut cx . caches . context )
153
+ . define_function ( func_id, context)
152
154
. unwrap ( ) ;
153
155
154
- // Step 11. Define debuginfo for function
155
- let context = & cx. caches . context ;
156
+ let value_ranges = context. build_value_labels_ranges ( cx. module . isa ( ) ) . expect ( "value location ranges" ) ;
157
+
158
+ // Write optimized function to file for debugging
159
+ #[ cfg( debug_assertions) ]
160
+ crate :: pretty_clif:: write_clif_file ( cx. tcx , "opt" , instance, & context. func , & clif_comments, Some ( & value_ranges) ) ;
161
+
162
+ // Define debuginfo for function
156
163
let isa = cx. module . isa ( ) ;
157
164
debug_context
158
165
. as_mut ( )
159
166
. map ( |x| x. define ( tcx, context, isa, & source_info_set) ) ;
160
167
161
- // Step 12. Clear context to make it usable for the next function
162
- cx . caches . context . clear ( ) ;
168
+ // Clear context to make it usable for the next function
169
+ context. clear ( ) ;
163
170
}
164
171
165
- fn verify_func ( tcx : TyCtxt , writer : crate :: pretty_clif:: CommentWriter , func : & Function ) {
172
+ fn verify_func ( tcx : TyCtxt , writer : & crate :: pretty_clif:: CommentWriter , func : & Function ) {
166
173
let flags = settings:: Flags :: new ( settings:: builder ( ) ) ;
167
174
match :: cranelift:: codegen:: verify_function ( & func, & flags) {
168
175
Ok ( _) => { }
@@ -171,7 +178,7 @@ fn verify_func(tcx: TyCtxt, writer: crate::pretty_clif::CommentWriter, func: &Fu
171
178
let pretty_error = :: cranelift:: codegen:: print_errors:: pretty_verifier_error (
172
179
& func,
173
180
None ,
174
- Some ( Box :: new ( & writer) ) ,
181
+ Some ( Box :: new ( writer) ) ,
175
182
err,
176
183
) ;
177
184
tcx. sess
0 commit comments