]> git.lizzy.rs Git - rust.git/blob - src/abi/comments.rs
Use the new cranelift-module interface
[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
8 use cranelift_codegen::entity::EntityRef;
9
10 use crate::abi::pass_mode::*;
11 use crate::prelude::*;
12
13 pub(super) fn add_args_header_comment(fx: &mut FunctionCx<'_, '_, impl Module>) {
14     fx.add_global_comment(format!(
15         "kind  loc.idx   param    pass mode                            ty"
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: EmptySinglePair<Value>,
25     pass_mode: PassMode,
26     ty: Ty<'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         Empty => Cow::Borrowed("-"),
41         Single(param) => Cow::Owned(format!("= {:?}", param)),
42         Pair(param_a, param_b) => Cow::Owned(format!("= {:?}, {:?}", param_a, param_b)),
43     };
44
45     let pass_mode = format!("{:?}", pass_mode);
46     fx.add_global_comment(format!(
47         "{kind:5}{local:>3}{local_field:<5} {params:10} {pass_mode:36} {ty:?}",
48         kind = kind,
49         local = local,
50         local_field = local_field,
51         params = params,
52         pass_mode = pass_mode,
53         ty = ty,
54     ));
55 }
56
57 pub(super) fn add_locals_header_comment(fx: &mut FunctionCx<'_, '_, impl Module>) {
58     fx.add_global_comment(String::new());
59     fx.add_global_comment(format!(
60         "kind  local ty                              size align (abi,pref)"
61     ));
62 }
63
64 pub(super) fn add_local_place_comments<'tcx>(
65     fx: &mut FunctionCx<'_, 'tcx, impl Module>,
66     place: CPlace<'tcx>,
67     local: Local,
68 ) {
69     let TyAndLayout { ty, layout } = place.layout();
70     let rustc_target::abi::Layout {
71         size,
72         align,
73         abi: _,
74         variants: _,
75         fields: _,
76         largest_niche: _,
77     } = layout;
78
79     let (kind, extra) = match *place.inner() {
80         CPlaceInner::Var(place_local, var) => {
81             assert_eq!(local, place_local);
82             ("ssa", Cow::Owned(format!(",var={}", var.index())))
83         }
84         CPlaceInner::VarPair(place_local, var1, var2) => {
85             assert_eq!(local, place_local);
86             (
87                 "ssa",
88                 Cow::Owned(format!(",var=({}, {})", var1.index(), var2.index())),
89             )
90         }
91         CPlaceInner::VarLane(_local, _var, _lane) => unreachable!(),
92         CPlaceInner::Addr(ptr, meta) => {
93             let meta = if let Some(meta) = meta {
94                 Cow::Owned(format!(",meta={}", meta))
95             } else {
96                 Cow::Borrowed("")
97             };
98             match ptr.base_and_offset() {
99                 (crate::pointer::PointerBase::Addr(addr), offset) => (
100                     "reuse",
101                     format!("storage={}{}{}", addr, offset, meta).into(),
102                 ),
103                 (crate::pointer::PointerBase::Stack(stack_slot), offset) => (
104                     "stack",
105                     format!("storage={}{}{}", stack_slot, offset, meta).into(),
106                 ),
107                 (crate::pointer::PointerBase::Dangling(align), offset) => (
108                     "zst",
109                     format!("align={},offset={}", align.bytes(), offset).into(),
110                 ),
111             }
112         }
113     };
114
115     fx.add_global_comment(format!(
116         "{:<5} {:5} {:30} {:4}b {}, {}{}{}",
117         kind,
118         format!("{:?}", local),
119         format!("{:?}", ty),
120         size.bytes(),
121         align.abi.bytes(),
122         align.pref.bytes(),
123         if extra.is_empty() {
124             ""
125         } else {
126             "              "
127         },
128         extra,
129     ));
130 }