]> git.lizzy.rs Git - rust.git/blob - src/abi/comments.rs
Sync from rust 4137088d9da94f693b287f35e2b17782c0b1a283
[rust.git] / src / abi / comments.rs
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 cranelift_codegen::entity::EntityRef;
10
11 use crate::prelude::*;
12
13 pub(super) fn add_args_header_comment(fx: &mut FunctionCx<'_, '_, '_>) {
14     if fx.clif_comments.enabled() {
15         fx.add_global_comment(
16             "kind  loc.idx   param    pass mode                            ty".to_string(),
17         );
18     }
19 }
20
21 pub(super) fn add_arg_comment<'tcx>(
22     fx: &mut FunctionCx<'_, '_, 'tcx>,
23     kind: &str,
24     local: Option<mir::Local>,
25     local_field: Option<usize>,
26     params: &[Value],
27     arg_abi_mode: PassMode,
28     arg_layout: TyAndLayout<'tcx>,
29 ) {
30     if !fx.clif_comments.enabled() {
31         return;
32     }
33
34     let local = if let Some(local) = local {
35         Cow::Owned(format!("{:?}", local))
36     } else {
37         Cow::Borrowed("???")
38     };
39     let local_field = if let Some(local_field) = local_field {
40         Cow::Owned(format!(".{}", local_field))
41     } else {
42         Cow::Borrowed("")
43     };
44
45     let params = match params {
46         [] => Cow::Borrowed("-"),
47         [param] => Cow::Owned(format!("= {:?}", param)),
48         [param_a, param_b] => Cow::Owned(format!("= {:?},{:?}", param_a, param_b)),
49         params => Cow::Owned(format!(
50             "= {}",
51             params.iter().map(ToString::to_string).collect::<Vec<_>>().join(",")
52         )),
53     };
54
55     let pass_mode = format!("{:?}", arg_abi_mode);
56     fx.add_global_comment(format!(
57         "{kind:5}{local:>3}{local_field:<5} {params:10} {pass_mode:36} {ty:?}",
58         kind = kind,
59         local = local,
60         local_field = local_field,
61         params = params,
62         pass_mode = pass_mode,
63         ty = arg_layout.ty,
64     ));
65 }
66
67 pub(super) fn add_locals_header_comment(fx: &mut FunctionCx<'_, '_, '_>) {
68     if fx.clif_comments.enabled() {
69         fx.add_global_comment(String::new());
70         fx.add_global_comment(
71             "kind  local ty                              size align (abi,pref)".to_string(),
72         );
73     }
74 }
75
76 pub(super) fn add_local_place_comments<'tcx>(
77     fx: &mut FunctionCx<'_, '_, 'tcx>,
78     place: CPlace<'tcx>,
79     local: Local,
80 ) {
81     if !fx.clif_comments.enabled() {
82         return;
83     }
84     let TyAndLayout { ty, layout } = place.layout();
85     let rustc_target::abi::Layout { size, align, abi: _, variants: _, fields: _, largest_niche: _ } =
86         layout;
87
88     let (kind, extra) = match *place.inner() {
89         CPlaceInner::Var(place_local, var) => {
90             assert_eq!(local, place_local);
91             ("ssa", Cow::Owned(format!(",var={}", var.index())))
92         }
93         CPlaceInner::VarPair(place_local, var1, var2) => {
94             assert_eq!(local, place_local);
95             ("ssa", Cow::Owned(format!(",var=({}, {})", var1.index(), var2.index())))
96         }
97         CPlaceInner::VarLane(_local, _var, _lane) => unreachable!(),
98         CPlaceInner::Addr(ptr, meta) => {
99             let meta = if let Some(meta) = meta {
100                 Cow::Owned(format!(",meta={}", meta))
101             } else {
102                 Cow::Borrowed("")
103             };
104             match ptr.debug_base_and_offset() {
105                 (crate::pointer::PointerBase::Addr(addr), offset) => {
106                     ("reuse", format!("storage={}{}{}", addr, offset, meta).into())
107                 }
108                 (crate::pointer::PointerBase::Stack(stack_slot), offset) => {
109                     ("stack", format!("storage={}{}{}", stack_slot, offset, meta).into())
110                 }
111                 (crate::pointer::PointerBase::Dangling(align), offset) => {
112                     ("zst", format!("align={},offset={}", align.bytes(), offset).into())
113                 }
114             }
115         }
116     };
117
118     fx.add_global_comment(format!(
119         "{:<5} {:5} {:30} {:4}b {}, {}{}{}",
120         kind,
121         format!("{:?}", local),
122         format!("{:?}", ty),
123         size.bytes(),
124         align.abi.bytes(),
125         align.pref.bytes(),
126         if extra.is_empty() { "" } else { "              " },
127         extra,
128     ));
129 }