@@ -9,14 +9,13 @@ pub fn trans_fn<'a, 'clif, 'tcx: 'a, B: Backend + 'static>(
99) {
1010 let tcx = cx. tcx ;
1111
12- // Step 1. Get mir
1312 let mir = tcx. instance_mir ( instance. def ) ;
1413
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.
1615 {
1716 // FIXME implement u128 and i128 support
1817
19- // Step 2a. Check sig for u128 and i128
18+ // Check sig for u128 and i128
2019 let fn_sig = tcx. normalize_erasing_late_bound_regions ( ParamEnv :: reveal_all ( ) , & instance. fn_sig ( tcx) ) ;
2120
2221 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>(
3534 let mut visitor = UI128Visitor ( tcx, false ) ;
3635 fn_sig. visit_with ( & mut visitor) ;
3736
38- // Step 2b. If found replace function with a trap.
37+ //If found replace function with a trap.
3938 if visitor. 1 {
4039 tcx. sess . warn ( "u128 and i128 are not yet supported. \
4140 Functions using these as args will be replaced with a trap.") ;
4241
43- // Step 2b1. Declare function with fake signature
42+ // Declare function with fake signature
4443 let sig = Signature {
4544 params : vec ! [ AbiParam :: new( types:: INVALID ) ] ,
4645 returns : vec ! [ ] ,
@@ -49,7 +48,7 @@ pub fn trans_fn<'a, 'clif, 'tcx: 'a, B: Backend + 'static>(
4948 let name = tcx. symbol_name ( instance) . as_str ( ) ;
5049 let func_id = cx. module . declare_function ( & * name, linkage, & sig) . unwrap ( ) ;
5150
52- // Step 2b2. Create trapping function
51+ // Create trapping function
5352 let mut func = Function :: with_name_signature ( ExternalName :: user ( 0 , 0 ) , sig) ;
5453 let mut func_ctx = FunctionBuilderContext :: new ( ) ;
5554 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>(
7978 fx. bcx . seal_all_blocks ( ) ;
8079 fx. bcx . finalize ( ) ;
8180
82- // Step 2b3. Define function
81+ // Define function
8382 cx. caches . context . func = func;
8483 cx. module
8584 . define_function ( func_id, & mut cx. caches . context )
@@ -89,27 +88,27 @@ pub fn trans_fn<'a, 'clif, 'tcx: 'a, B: Backend + 'static>(
8988 }
9089 }
9190
92- // Step 3. Declare function
91+ // Declare function
9392 let ( name, sig) = get_function_name_and_sig ( tcx, instance, false ) ;
9493 let func_id = cx. module . declare_function ( & name, linkage, & sig) . unwrap ( ) ;
9594 let mut debug_context = cx
9695 . debug_context
9796 . as_mut ( )
9897 . map ( |debug_context| FunctionDebugContext :: new ( tcx, debug_context, mir, & name, & sig) ) ;
9998
100- // Step 4. Make FunctionBuilder
99+ // Make FunctionBuilder
101100 let mut func = Function :: with_name_signature ( ExternalName :: user ( 0 , 0 ) , sig) ;
102101 let mut func_ctx = FunctionBuilderContext :: new ( ) ;
103102 let mut bcx = FunctionBuilder :: new ( & mut func, & mut func_ctx) ;
104103
105- // Step 5. Predefine ebb's
104+ // Predefine ebb's
106105 let start_ebb = bcx. create_ebb ( ) ;
107106 let mut ebb_map: HashMap < BasicBlock , Ebb > = HashMap :: new ( ) ;
108107 for ( bb, _bb_data) in mir. basic_blocks ( ) . iter_enumerated ( ) {
109108 ebb_map. insert ( bb, bcx. create_ebb ( ) ) ;
110109 }
111110
112- // Step 6. Make FunctionCx
111+ // Make FunctionCx
113112 let pointer_type = cx. module . target_config ( ) . pointer_type ( ) ;
114113 let clif_comments = crate :: pretty_clif:: CommentWriter :: new ( tcx, instance) ;
115114
@@ -131,38 +130,46 @@ pub fn trans_fn<'a, 'clif, 'tcx: 'a, B: Backend + 'static>(
131130 source_info_set : indexmap:: IndexSet :: new ( ) ,
132131 } ;
133132
134- // Step 7. Codegen function
135133 with_unimpl_span ( fx. mir . span , || {
136134 crate :: abi:: codegen_fn_prelude ( & mut fx, start_ebb) ;
137135 codegen_fn_content ( & mut fx) ;
138136 } ) ;
139- let source_info_set = fx. source_info_set . clone ( ) ;
140137
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+
142143 #[ cfg( debug_assertions) ]
143- fx . write_clif_file ( ) ;
144+ crate :: pretty_clif :: write_clif_file ( cx . tcx , "unopt" , instance , & func , & clif_comments , None ) ;
144145
145- // Step 9. Verify function
146- verify_func ( tcx, fx . clif_comments , & func) ;
146+ // Verify function
147+ verify_func ( tcx, & clif_comments, & func) ;
147148
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;
150152 cx. module
151- . define_function ( func_id, & mut cx . caches . context )
153+ . define_function ( func_id, context)
152154 . unwrap ( ) ;
153155
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
156163 let isa = cx. module . isa ( ) ;
157164 debug_context
158165 . as_mut ( )
159166 . map ( |x| x. define ( tcx, context, isa, & source_info_set) ) ;
160167
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 ( ) ;
163170}
164171
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 ) {
166173 let flags = settings:: Flags :: new ( settings:: builder ( ) ) ;
167174 match :: cranelift:: codegen:: verify_function ( & func, & flags) {
168175 Ok ( _) => { }
@@ -171,7 +178,7 @@ fn verify_func(tcx: TyCtxt, writer: crate::pretty_clif::CommentWriter, func: &Fu
171178 let pretty_error = :: cranelift:: codegen:: print_errors:: pretty_verifier_error (
172179 & func,
173180 None ,
174- Some ( Box :: new ( & writer) ) ,
181+ Some ( Box :: new ( writer) ) ,
175182 err,
176183 ) ;
177184 tcx. sess
0 commit comments