summary refs log tree commit diff
path: root/compiler/rustc_codegen_cranelift/src/abi/comments.rs
blob: 01073d26e832a98fd68cf6ac7463c6fe17abde98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//! Annotate the clif ir with comments describing how arguments are passed into the current function
//! and where all locals are stored.

use std::borrow::Cow;

use rustc_middle::mir;

use cranelift_codegen::entity::EntityRef;

use crate::abi::pass_mode::*;
use crate::prelude::*;

pub(super) fn add_args_header_comment(fx: &mut FunctionCx<'_, '_, impl Module>) {
    fx.add_global_comment(
        "kind  loc.idx   param    pass mode                            ty".to_string(),
    );
}

pub(super) fn add_arg_comment<'tcx>(
    fx: &mut FunctionCx<'_, 'tcx, impl Module>,
    kind: &str,
    local: Option<mir::Local>,
    local_field: Option<usize>,
    params: EmptySinglePair<Value>,
    pass_mode: PassMode,
    ty: Ty<'tcx>,
) {
    let local = if let Some(local) = local {
        Cow::Owned(format!("{:?}", local))
    } else {
        Cow::Borrowed("???")
    };
    let local_field = if let Some(local_field) = local_field {
        Cow::Owned(format!(".{}", local_field))
    } else {
        Cow::Borrowed("")
    };

    let params = match params {
        Empty => Cow::Borrowed("-"),
        Single(param) => Cow::Owned(format!("= {:?}", param)),
        Pair(param_a, param_b) => Cow::Owned(format!("= {:?}, {:?}", param_a, param_b)),
    };

    let pass_mode = format!("{:?}", pass_mode);
    fx.add_global_comment(format!(
        "{kind:5}{local:>3}{local_field:<5} {params:10} {pass_mode:36} {ty:?}",
        kind = kind,
        local = local,
        local_field = local_field,
        params = params,
        pass_mode = pass_mode,
        ty = ty,
    ));
}

pub(super) fn add_locals_header_comment(fx: &mut FunctionCx<'_, '_, impl Module>) {
    fx.add_global_comment(String::new());
    fx.add_global_comment(
        "kind  local ty                              size align (abi,pref)".to_string(),
    );
}

pub(super) fn add_local_place_comments<'tcx>(
    fx: &mut FunctionCx<'_, 'tcx, impl Module>,
    place: CPlace<'tcx>,
    local: Local,
) {
    let TyAndLayout { ty, layout } = place.layout();
    let rustc_target::abi::Layout {
        size,
        align,
        abi: _,
        variants: _,
        fields: _,
        largest_niche: _,
    } = layout;

    let (kind, extra) = match *place.inner() {
        CPlaceInner::Var(place_local, var) => {
            assert_eq!(local, place_local);
            ("ssa", Cow::Owned(format!(",var={}", var.index())))
        }
        CPlaceInner::VarPair(place_local, var1, var2) => {
            assert_eq!(local, place_local);
            (
                "ssa",
                Cow::Owned(format!(",var=({}, {})", var1.index(), var2.index())),
            )
        }
        CPlaceInner::VarLane(_local, _var, _lane) => unreachable!(),
        CPlaceInner::Addr(ptr, meta) => {
            let meta = if let Some(meta) = meta {
                Cow::Owned(format!(",meta={}", meta))
            } else {
                Cow::Borrowed("")
            };
            match ptr.base_and_offset() {
                (crate::pointer::PointerBase::Addr(addr), offset) => (
                    "reuse",
                    format!("storage={}{}{}", addr, offset, meta).into(),
                ),
                (crate::pointer::PointerBase::Stack(stack_slot), offset) => (
                    "stack",
                    format!("storage={}{}{}", stack_slot, offset, meta).into(),
                ),
                (crate::pointer::PointerBase::Dangling(align), offset) => (
                    "zst",
                    format!("align={},offset={}", align.bytes(), offset).into(),
                ),
            }
        }
    };

    fx.add_global_comment(format!(
        "{:<5} {:5} {:30} {:4}b {}, {}{}{}",
        kind,
        format!("{:?}", local),
        format!("{:?}", ty),
        size.bytes(),
        align.abi.bytes(),
        align.pref.bytes(),
        if extra.is_empty() {
            ""
        } else {
            "              "
        },
        extra,
    ));
}