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