]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_cranelift/src/abi/comments.rs
Auto merge of #82102 - nagisa:nagisa/fix-dwo-name, r=davidtwco
[rust.git] / compiler / rustc_codegen_cranelift / 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<'_, '_, impl Module>) {
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, impl Module>,
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
46                 .iter()
47                 .map(ToString::to_string)
48                 .collect::<Vec<_>>()
49                 .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
65 pub(super) fn add_locals_header_comment(fx: &mut FunctionCx<'_, '_, impl Module>) {
66     fx.add_global_comment(String::new());
67     fx.add_global_comment(
68         "kind  local ty                              size align (abi,pref)".to_string(),
69     );
70 }
71
72 pub(super) fn add_local_place_comments<'tcx>(
73     fx: &mut FunctionCx<'_, 'tcx, impl Module>,
74     place: CPlace<'tcx>,
75     local: Local,
76 ) {
77     let TyAndLayout { ty, layout } = place.layout();
78     let rustc_target::abi::Layout {
79         size,
80         align,
81         abi: _,
82         variants: _,
83         fields: _,
84         largest_niche: _,
85     } = layout;
86
87     let (kind, extra) = match *place.inner() {
88         CPlaceInner::Var(place_local, var) => {
89             assert_eq!(local, place_local);
90             ("ssa", Cow::Owned(format!(",var={}", var.index())))
91         }
92         CPlaceInner::VarPair(place_local, var1, var2) => {
93             assert_eq!(local, place_local);
94             (
95                 "ssa",
96                 Cow::Owned(format!(",var=({}, {})", var1.index(), var2.index())),
97             )
98         }
99         CPlaceInner::VarLane(_local, _var, _lane) => unreachable!(),
100         CPlaceInner::Addr(ptr, meta) => {
101             let meta = if let Some(meta) = meta {
102                 Cow::Owned(format!(",meta={}", meta))
103             } else {
104                 Cow::Borrowed("")
105             };
106             match ptr.base_and_offset() {
107                 (crate::pointer::PointerBase::Addr(addr), offset) => (
108                     "reuse",
109                     format!("storage={}{}{}", addr, offset, meta).into(),
110                 ),
111                 (crate::pointer::PointerBase::Stack(stack_slot), offset) => (
112                     "stack",
113                     format!("storage={}{}{}", stack_slot, offset, meta).into(),
114                 ),
115                 (crate::pointer::PointerBase::Dangling(align), offset) => (
116                     "zst",
117                     format!("align={},offset={}", align.bytes(), offset).into(),
118                 ),
119             }
120         }
121     };
122
123     fx.add_global_comment(format!(
124         "{:<5} {:5} {:30} {:4}b {}, {}{}{}",
125         kind,
126         format!("{:?}", local),
127         format!("{:?}", ty),
128         size.bytes(),
129         align.abi.bytes(),
130         align.pref.bytes(),
131         if extra.is_empty() {
132             ""
133         } else {
134             "              "
135         },
136         extra,
137     ));
138 }