1 //! Annotate the clif ir with comments describing how arguments are passed into the current function
2 //! and where all locals are stored.
3
4 use std::borrow::Cow;
5
6 use rustc_middle::mir;
7 use rustc_target::abi::call::PassMode;
8
9 use crate::prelude::*;
10
add_args_header_comment(fx: &mut FunctionCx<'_, '_, '_>)11 pub(super) fn add_args_header_comment(fx: &mut FunctionCx<'_, '_, '_>) {
12 if fx.clif_comments.enabled() {
13 fx.add_global_comment(
14 "kind loc.idx param pass mode ty".to_string(),
15 );
16 }
17 }
18
add_arg_comment<'tcx>( fx: &mut FunctionCx<'_, '_, 'tcx>, kind: &str, local: Option<mir::Local>, local_field: Option<usize>, params: &[Value], arg_abi_mode: &PassMode, arg_layout: TyAndLayout<'tcx>, )19 pub(super) fn add_arg_comment<'tcx>(
20 fx: &mut FunctionCx<'_, '_, 'tcx>,
21 kind: &str,
22 local: Option<mir::Local>,
23 local_field: Option<usize>,
24 params: &[Value],
25 arg_abi_mode: &PassMode,
26 arg_layout: TyAndLayout<'tcx>,
27 ) {
28 if !fx.clif_comments.enabled() {
29 return;
30 }
31
32 let local = if let Some(local) = local {
33 Cow::Owned(format!("{:?}", local))
34 } else {
35 Cow::Borrowed("???")
36 };
37 let local_field = if let Some(local_field) = local_field {
38 Cow::Owned(format!(".{}", local_field))
39 } else {
40 Cow::Borrowed("")
41 };
42
43 let params = match params {
44 [] => Cow::Borrowed("-"),
45 [param] => Cow::Owned(format!("= {:?}", param)),
46 [param_a, param_b] => Cow::Owned(format!("= {:?},{:?}", param_a, param_b)),
47 params => Cow::Owned(format!(
48 "= {}",
49 params.iter().map(ToString::to_string).collect::<Vec<_>>().join(",")
50 )),
51 };
52
53 let pass_mode = format!("{:?}", arg_abi_mode);
54 fx.add_global_comment(format!(
55 "{kind:5}{local:>3}{local_field:<5} {params:10} {pass_mode:36} {ty:?}",
56 kind = kind,
57 local = local,
58 local_field = local_field,
59 params = params,
60 pass_mode = pass_mode,
61 ty = arg_layout.ty,
62 ));
63 }
64
add_locals_header_comment(fx: &mut FunctionCx<'_, '_, '_>)65 pub(super) fn add_locals_header_comment(fx: &mut FunctionCx<'_, '_, '_>) {
66 if fx.clif_comments.enabled() {
67 fx.add_global_comment(String::new());
68 fx.add_global_comment(
69 "kind local ty size align (abi,pref)".to_string(),
70 );
71 }
72 }
73
add_local_place_comments<'tcx>( fx: &mut FunctionCx<'_, '_, 'tcx>, place: CPlace<'tcx>, local: Local, )74 pub(super) fn add_local_place_comments<'tcx>(
75 fx: &mut FunctionCx<'_, '_, 'tcx>,
76 place: CPlace<'tcx>,
77 local: Local,
78 ) {
79 if !fx.clif_comments.enabled() {
80 return;
81 }
82 let TyAndLayout { ty, layout } = place.layout();
83 let rustc_target::abi::LayoutS {
84 size,
85 align,
86 abi: _,
87 variants: _,
88 fields: _,
89 largest_niche: _,
90 } = layout.0.0;
91
92 let (kind, extra) = place.debug_comment();
93
94 fx.add_global_comment(format!(
95 "{:<5} {:5} {:30} {:4}b {}, {}{}{}",
96 kind,
97 format!("{:?}", local),
98 format!("{:?}", ty),
99 size.bytes(),
100 align.abi.bytes(),
101 align.pref.bytes(),
102 if extra.is_empty() { "" } else { " " },
103 extra,
104 ));
105 }
106