2020-09-23 15:13:49 +02:00
//! This module provides the [CommentWriter] which makes it possible
//! to add comments to the written cranelift ir.
//!
//! # Example
//!
//! ```clif
//! test compile
//! target x86_64
//!
//! function u0:0(i64, i64, i64) system_v {
//! ; symbol _ZN119_$LT$example..IsNotEmpty$u20$as$u20$mini_core..FnOnce$LT$$LP$$RF$$u27$a$u20$$RF$$u27$b$u20$$u5b$u16$u5d$$C$$RP$$GT$$GT$9call_once17he85059d5e6a760a0E
//! ; instance Instance { def: Item(DefId(0/0:29 ~ example[8787]::{{impl}}[0]::call_once[0])), substs: [ReErased, ReErased] }
//! ; sig ([IsNotEmpty, (&&[u16],)]; c_variadic: false)->(u8, u8)
//!
//! ; ssa {_2: NOT_SSA, _4: NOT_SSA, _0: NOT_SSA, _3: (empty), _1: NOT_SSA}
//! ; msg loc.idx param pass mode ssa flags ty
//! ; ret _0 = v0 ByRef NOT_SSA (u8, u8)
//! ; arg _1 = v1 ByRef NOT_SSA IsNotEmpty
//! ; arg _2.0 = v2 ByVal(types::I64) NOT_SSA &&[u16]
//!
//! ss0 = explicit_slot 0 ; _1: IsNotEmpty size=0 align=1,8
//! ss1 = explicit_slot 8 ; _2: (&&[u16],) size=8 align=8,8
//! ss2 = explicit_slot 8 ; _4: (&&[u16],) size=8 align=8,8
//! sig0 = (i64, i64, i64) system_v
//! sig1 = (i64, i64, i64) system_v
//! fn0 = colocated u0:6 sig1 ; Instance { def: Item(DefId(0/0:31 ~ example[8787]::{{impl}}[1]::call_mut[0])), substs: [ReErased, ReErased] }
//!
//! block0(v0: i64, v1: i64, v2: i64):
//! v3 = stack_addr.i64 ss0
//! v4 = stack_addr.i64 ss1
//! store v2, v4
//! v5 = stack_addr.i64 ss2
//! jump block1
//!
//! block1:
//! nop
//! ; _3 = &mut _1
//! ; _4 = _2
//! v6 = load.i64 v4
//! store v6, v5
//! ;
//! ; _0 = const mini_core::FnMut::call_mut(move _3, move _4)
//! v7 = load.i64 v5
//! call fn0(v0, v3, v7)
//! jump block2
//!
//! block2:
//! nop
//! ;
//! ; return
//! return
//! }
//! ```
2018-07-14 11:59:42 +02:00
use std ::fmt ;
2020-12-27 10:30:38 +01:00
use std ::io ::Write ;
2018-07-14 11:59:42 +02:00
2019-12-24 12:40:18 +01:00
use cranelift_codegen ::{
2019-05-14 16:12:58 +02:00
entity ::SecondaryMap ,
2020-03-24 13:41:19 +01:00
ir ::{ entities ::AnyEntity , function ::DisplayFunctionAnnotations } ,
2019-05-14 16:12:58 +02:00
write ::{ FuncWriter , PlainWriter } ,
} ;
2018-07-20 13:51:34 +02:00
2021-09-02 00:09:34 +03:00
use rustc_middle ::ty ::layout ::FnAbiOf ;
2020-05-29 07:25:28 +03:00
use rustc_session ::config ::OutputType ;
2018-07-30 16:57:40 +02:00
use crate ::prelude ::* ;
2018-07-14 11:59:42 +02:00
2018-12-21 21:42:29 +01:00
#[ derive(Debug) ]
2020-03-27 12:14:45 +01:00
pub ( crate ) struct CommentWriter {
2021-03-29 10:45:09 +02:00
enabled : bool ,
2018-12-21 21:42:29 +01:00
global_comments : Vec < String > ,
2020-04-05 14:01:02 +02:00
entity_comments : FxHashMap < AnyEntity , String > ,
2018-12-21 21:42:29 +01:00
}
impl CommentWriter {
2020-03-27 12:14:45 +01:00
pub ( crate ) fn new < ' tcx > ( tcx : TyCtxt < ' tcx > , instance : Instance < ' tcx > ) -> Self {
2021-03-29 10:45:09 +02:00
let enabled = should_write_ir ( tcx ) ;
let global_comments = if enabled {
2019-11-28 20:34:55 +01:00
vec! [
2020-07-17 19:15:33 +02:00
format! ( " symbol {} " , tcx . symbol_name ( instance ) . name ) ,
2018-12-21 21:42:29 +01:00
format! ( " instance {:?} " , instance ) ,
2021-09-02 00:29:15 +03:00
format! (
" abi {:?} " ,
RevealAllLayoutCx ( tcx ) . fn_abi_of_instance ( instance , ty ::List ::empty ( ) )
) ,
2018-12-21 21:42:29 +01:00
String ::new ( ) ,
2019-11-28 20:34:55 +01:00
]
} else {
vec! [ ]
} ;
2021-03-29 10:45:09 +02:00
CommentWriter { enabled , global_comments , entity_comments : FxHashMap ::default ( ) }
2020-03-20 12:18:40 +01:00
}
}
impl CommentWriter {
2021-03-29 10:45:09 +02:00
pub ( crate ) fn enabled ( & self ) -> bool {
self . enabled
}
2020-03-27 12:14:45 +01:00
pub ( crate ) fn add_global_comment < S : Into < String > > ( & mut self , comment : S ) {
2021-03-29 10:45:09 +02:00
debug_assert! ( self . enabled ) ;
2020-03-20 12:18:40 +01:00
self . global_comments . push ( comment . into ( ) ) ;
}
2020-03-27 12:14:45 +01:00
pub ( crate ) fn add_comment < S : Into < String > + AsRef < str > , E : Into < AnyEntity > > (
2020-03-20 12:18:40 +01:00
& mut self ,
entity : E ,
comment : S ,
) {
2021-03-29 10:45:09 +02:00
debug_assert! ( self . enabled ) ;
2020-03-20 12:18:40 +01:00
use std ::collections ::hash_map ::Entry ;
match self . entity_comments . entry ( entity . into ( ) ) {
Entry ::Occupied ( mut occ ) = > {
occ . get_mut ( ) . push ( '\n' ) ;
2020-03-24 13:41:19 +01:00
occ . get_mut ( ) . push_str ( comment . as_ref ( ) ) ;
2020-03-20 12:18:40 +01:00
}
Entry ::Vacant ( vac ) = > {
2020-03-24 13:41:19 +01:00
vac . insert ( comment . into ( ) ) ;
2020-03-20 12:18:40 +01:00
}
2018-12-21 21:42:29 +01:00
}
}
}
2018-07-14 11:59:42 +02:00
2019-06-16 11:13:49 +02:00
impl FuncWriter for & '_ CommentWriter {
2018-11-15 10:55:40 +01:00
fn write_preamble (
& mut self ,
w : & mut dyn fmt ::Write ,
func : & Function ,
reg_info : Option < & isa ::RegInfo > ,
) -> Result < bool , fmt ::Error > {
2018-12-21 21:42:29 +01:00
for comment in & self . global_comments {
if ! comment . is_empty ( ) {
writeln! ( w , " ; {} " , comment ) ? ;
} else {
2020-11-03 11:00:04 +01:00
writeln! ( w ) ? ;
2018-12-21 21:42:29 +01:00
}
}
if ! self . global_comments . is_empty ( ) {
2020-11-03 11:00:04 +01:00
writeln! ( w ) ? ;
2018-12-21 21:42:29 +01:00
}
2018-12-27 10:59:01 +01:00
self . super_preamble ( w , func , reg_info )
}
fn write_entity_definition (
& mut self ,
w : & mut dyn fmt ::Write ,
_func : & Function ,
entity : AnyEntity ,
2019-05-31 10:56:55 +02:00
value : & dyn fmt ::Display ,
2018-12-27 10:59:01 +01:00
) -> fmt ::Result {
write! ( w , " {} = {} " , entity , value ) ? ;
if let Some ( comment ) = self . entity_comments . get ( & entity ) {
writeln! ( w , " ; {} " , comment . replace ( '\n' , " \n ; " ) )
} else {
2020-11-03 11:00:04 +01:00
writeln! ( w )
2018-12-27 10:59:01 +01:00
}
2018-11-15 10:55:40 +01:00
}
2020-02-14 18:23:29 +01:00
fn write_block_header (
2018-11-15 10:55:40 +01:00
& mut self ,
w : & mut dyn fmt ::Write ,
func : & Function ,
isa : Option < & dyn isa ::TargetIsa > ,
2020-02-14 18:23:29 +01:00
block : Block ,
2018-11-15 10:55:40 +01:00
indent : usize ,
) -> fmt ::Result {
2020-02-14 18:23:29 +01:00
PlainWriter . write_block_header ( w , func , isa , block , indent )
2018-11-15 10:55:40 +01:00
}
2018-07-14 11:59:42 +02:00
fn write_instruction (
& mut self ,
w : & mut dyn fmt ::Write ,
func : & Function ,
2018-09-27 19:19:16 +02:00
aliases : & SecondaryMap < Value , Vec < Value > > ,
2018-07-14 11:59:42 +02:00
isa : Option < & dyn isa ::TargetIsa > ,
inst : Inst ,
indent : usize ,
) -> fmt ::Result {
2018-09-05 19:43:42 +02:00
PlainWriter . write_instruction ( w , func , aliases , isa , inst , indent ) ? ;
2020-03-20 12:18:40 +01:00
if let Some ( comment ) = self . entity_comments . get ( & inst . into ( ) ) {
2018-07-14 12:21:45 +02:00
writeln! ( w , " ; {} " , comment . replace ( '\n' , " \n ; " ) ) ? ;
}
2018-07-20 13:51:34 +02:00
Ok ( ( ) )
2018-07-14 11:59:42 +02:00
}
}
2018-07-20 13:51:34 +02:00
2021-03-05 19:12:59 +01:00
impl FunctionCx < '_ , '_ , '_ > {
2020-03-27 12:14:45 +01:00
pub ( crate ) fn add_global_comment < S : Into < String > > ( & mut self , comment : S ) {
2020-03-20 12:18:40 +01:00
self . clif_comments . add_global_comment ( comment ) ;
2018-08-15 16:17:59 +02:00
}
2020-03-27 12:14:45 +01:00
pub ( crate ) fn add_comment < S : Into < String > + AsRef < str > , E : Into < AnyEntity > > (
2019-02-21 15:06:09 +01:00
& mut self ,
entity : E ,
comment : S ,
) {
2020-08-28 12:10:48 +02:00
self . clif_comments . add_comment ( entity , comment ) ;
2018-07-20 13:51:34 +02:00
}
2019-05-14 16:12:58 +02:00
}
2020-12-27 10:30:38 +01:00
pub ( crate ) fn should_write_ir ( tcx : TyCtxt < '_ > ) -> bool {
2021-03-05 19:12:59 +01:00
tcx . sess . opts . output_types . contains_key ( & OutputType ::LlvmAssembly )
2020-12-27 10:30:38 +01:00
}
2021-03-29 10:45:09 +02:00
pub ( crate ) fn write_ir_file (
tcx : TyCtxt < '_ > ,
2021-04-30 14:49:58 +02:00
name : impl FnOnce ( ) -> String ,
2020-12-27 10:30:38 +01:00
write : impl FnOnce ( & mut dyn Write ) -> std ::io ::Result < ( ) > ,
) {
if ! should_write_ir ( tcx ) {
2020-05-29 07:25:28 +03:00
return ;
}
2021-05-11 14:39:04 +02:00
let clif_output_dir = tcx . output_filenames ( ( ) ) . with_extension ( " clif " ) ;
2020-10-15 10:57:56 +02:00
match std ::fs ::create_dir ( & clif_output_dir ) {
Ok ( ( ) ) = > { }
Err ( err ) if err . kind ( ) = = std ::io ::ErrorKind ::AlreadyExists = > { }
res @ Err ( _ ) = > res . unwrap ( ) ,
}
2021-04-30 14:49:58 +02:00
let clif_file_name = clif_output_dir . join ( name ( ) ) ;
2019-05-14 16:12:58 +02:00
2021-03-29 10:45:09 +02:00
let res = std ::fs ::File ::create ( clif_file_name ) . and_then ( | mut file | write ( & mut file ) ) ;
2020-08-08 16:14:11 +02:00
if let Err ( err ) = res {
2020-12-27 10:30:38 +01:00
tcx . sess . warn ( & format! ( " error writing ir file: {} " , err ) ) ;
2019-05-14 16:12:58 +02:00
}
}
2018-12-21 21:42:29 +01:00
2020-12-27 10:30:38 +01:00
pub ( crate ) fn write_clif_file < ' tcx > (
tcx : TyCtxt < ' tcx > ,
postfix : & str ,
2021-07-07 11:14:20 +02:00
isa : & dyn cranelift_codegen ::isa ::TargetIsa ,
2020-12-27 10:30:38 +01:00
instance : Instance < ' tcx > ,
context : & cranelift_codegen ::Context ,
mut clif_comments : & CommentWriter ,
) {
2021-04-30 14:49:58 +02:00
write_ir_file (
tcx ,
| | format! ( " {} . {} .clif " , tcx . symbol_name ( instance ) . name , postfix ) ,
| file | {
let mut clif = String ::new ( ) ;
cranelift_codegen ::write ::decorate_function (
& mut clif_comments ,
& mut clif ,
& context . func ,
2021-07-07 11:14:20 +02:00
& DisplayFunctionAnnotations { isa : Some ( isa ) , value_ranges : None } ,
2021-04-30 14:49:58 +02:00
)
. unwrap ( ) ;
2020-12-27 10:30:38 +01:00
2021-07-07 11:14:20 +02:00
for flag in isa . flags ( ) . iter ( ) {
writeln! ( file , " set {} " , flag ) ? ;
}
write! ( file , " target {} " , isa . triple ( ) . architecture . to_string ( ) ) ? ;
for isa_flag in isa . isa_flags ( ) . iter ( ) {
write! ( file , " {} " , isa_flag ) ? ;
}
writeln! ( file , " \n " ) ? ;
2021-04-30 14:49:58 +02:00
writeln! ( file ) ? ;
file . write_all ( clif . as_bytes ( ) ) ? ;
Ok ( ( ) )
} ,
) ;
2020-12-27 10:30:38 +01:00
}
2021-03-05 19:12:59 +01:00
impl fmt ::Debug for FunctionCx < '_ , '_ , '_ > {
2020-04-05 13:48:26 +02:00
fn fmt ( & self , f : & mut fmt ::Formatter < '_ > ) -> fmt ::Result {
2019-05-14 16:12:58 +02:00
writeln! ( f , " {:?} " , self . instance . substs ) ? ;
writeln! ( f , " {:?} " , self . local_map ) ? ;
2018-12-21 21:42:29 +01:00
let mut clif = String ::new ( ) ;
2019-12-24 12:40:18 +01:00
::cranelift_codegen ::write ::decorate_function (
2019-02-21 15:06:09 +01:00
& mut & self . clif_comments ,
& mut clif ,
& self . bcx . func ,
2019-10-04 14:57:07 +02:00
& DisplayFunctionAnnotations ::default ( ) ,
2019-02-21 15:06:09 +01:00
)
. unwrap ( ) ;
2019-05-14 16:12:58 +02:00
writeln! ( f , " \n {} " , clif )
2018-12-21 21:42:29 +01:00
}
2018-07-20 13:51:34 +02:00
}