]>
Commit | Line | Data |
---|---|---|
29967ef6 XL |
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; | |
5869c6ff | 7 | use rustc_target::abi::call::PassMode; |
29967ef6 XL |
8 | |
9 | use cranelift_codegen::entity::EntityRef; | |
10 | ||
29967ef6 XL |
11 | use crate::prelude::*; |
12 | ||
6a06907d | 13 | pub(super) fn add_args_header_comment(fx: &mut FunctionCx<'_, '_, '_>) { |
29967ef6 XL |
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>( | |
6a06907d | 20 | fx: &mut FunctionCx<'_, '_, 'tcx>, |
29967ef6 XL |
21 | kind: &str, |
22 | local: Option<mir::Local>, | |
23 | local_field: Option<usize>, | |
5869c6ff XL |
24 | params: &[Value], |
25 | arg_abi_mode: PassMode, | |
26 | arg_layout: TyAndLayout<'tcx>, | |
29967ef6 XL |
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 { | |
5869c6ff XL |
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 | "= {}", | |
6a06907d | 45 | params.iter().map(ToString::to_string).collect::<Vec<_>>().join(",") |
5869c6ff | 46 | )), |
29967ef6 XL |
47 | }; |
48 | ||
5869c6ff | 49 | let pass_mode = format!("{:?}", arg_abi_mode); |
29967ef6 XL |
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, | |
5869c6ff | 57 | ty = arg_layout.ty, |
29967ef6 XL |
58 | )); |
59 | } | |
60 | ||
6a06907d | 61 | pub(super) fn add_locals_header_comment(fx: &mut FunctionCx<'_, '_, '_>) { |
29967ef6 XL |
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>( | |
6a06907d | 69 | fx: &mut FunctionCx<'_, '_, 'tcx>, |
29967ef6 XL |
70 | place: CPlace<'tcx>, |
71 | local: Local, | |
72 | ) { | |
73 | let TyAndLayout { ty, layout } = place.layout(); | |
6a06907d XL |
74 | let rustc_target::abi::Layout { size, align, abi: _, variants: _, fields: _, largest_niche: _ } = |
75 | layout; | |
29967ef6 XL |
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); | |
6a06907d | 84 | ("ssa", Cow::Owned(format!(",var=({}, {})", var1.index(), var2.index()))) |
29967ef6 XL |
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() { | |
6a06907d XL |
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 | } | |
29967ef6 XL |
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(), | |
6a06907d | 115 | if extra.is_empty() { "" } else { " " }, |
29967ef6 XL |
116 | extra, |
117 | )); | |
118 | } |