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