]> git.lizzy.rs Git - rust.git/blobdiff - src/abi/comments.rs
[CI] Update package list before installing packages
[rust.git] / src / abi / comments.rs
index 674449cf06d524272efee594be4beab141a2cd6b..5fbaed7283a6781400a2ca0628cc731ae49edb8e 100644 (file)
+//! 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::mir;
+use rustc_middle::mir;
+use rustc_target::abi::call::PassMode;
+
+use cranelift_codegen::entity::EntityRef;
 
 use crate::prelude::*;
-use crate::abi::pass_mode::*;
 
-pub fn add_local_header_comment(fx: &mut FunctionCx<impl Backend>) {
-    fx.add_global_comment(format!(
-        "msg   loc.idx    param    pass mode                            ssa flags  ty"
-    ));
+pub(super) fn add_args_header_comment(fx: &mut FunctionCx<'_, '_, '_>) {
+    if fx.clif_comments.enabled() {
+        fx.add_global_comment(
+            "kind  loc.idx   param    pass mode                            ty".to_string(),
+        );
+    }
 }
 
-pub fn add_arg_comment<'tcx>(
-    fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
-    msg: &str,
-    local: mir::Local,
+pub(super) fn add_arg_comment<'tcx>(
+    fx: &mut FunctionCx<'_, '_, 'tcx>,
+    kind: &str,
+    local: Option<mir::Local>,
     local_field: Option<usize>,
-    params: EmptySinglePair<Value>,
-    pass_mode: PassMode,
-    ssa: crate::analyze::Flags,
-    ty: Ty<'tcx>,
+    params: &[Value],
+    arg_abi_mode: PassMode,
+    arg_layout: TyAndLayout<'tcx>,
 ) {
+    if !fx.clif_comments.enabled() {
+        return;
+    }
+
+    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)),
+        [] => Cow::Borrowed("-"),
+        [param] => Cow::Owned(format!("= {:?}", param)),
+        [param_a, param_b] => Cow::Owned(format!("= {:?},{:?}", param_a, param_b)),
+        params => Cow::Owned(format!(
+            "= {}",
+            params.iter().map(ToString::to_string).collect::<Vec<_>>().join(",")
+        )),
     };
-    let pass_mode = format!("{:?}", pass_mode);
+
+    let pass_mode = format!("{:?}", arg_abi_mode);
     fx.add_global_comment(format!(
-        "{msg:5}{local:>3}{local_field:<5} {params:10} {pass_mode:36} {ssa:10} {ty:?}",
-        msg = msg,
-        local = format!("{:?}", local),
+        "{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,
-        ssa = format!("{:?}", ssa),
-        ty = ty,
+        ty = arg_layout.ty,
     ));
 }
 
-pub fn add_local_place_comments<'tcx>(
-    fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
+pub(super) fn add_locals_header_comment(fx: &mut FunctionCx<'_, '_, '_>) {
+    if fx.clif_comments.enabled() {
+        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>,
     place: CPlace<'tcx>,
     local: Local,
 ) {
-    let TyLayout { ty, details } = place.layout();
-    let ty::layout::LayoutDetails {
-        size,
-        align,
-        abi: _,
-        variants: _,
-        fields: _,
-        largest_niche: _,
-    } = details;
-    match *place.inner() {
-        CPlaceInner::Var(var) => {
-            assert_eq!(local, var);
-            fx.add_global_comment(format!(
-                "ssa   {:?}: {:?} size={} align={}, {}",
-                local,
-                ty,
-                size.bytes(),
-                align.abi.bytes(),
-                align.pref.bytes(),
-            ));
-        }
-        CPlaceInner::Stack(stack_slot) => fx.add_entity_comment(
-            stack_slot,
-            format!(
-                "{:?}: {:?} size={} align={},{}",
-                local,
-                ty,
-                size.bytes(),
-                align.abi.bytes(),
-                align.pref.bytes(),
-            ),
-        ),
-        CPlaceInner::NoPlace => fx.add_global_comment(format!(
-            "zst   {:?}: {:?} size={} align={}, {}",
-            local,
-            ty,
-            size.bytes(),
-            align.abi.bytes(),
-            align.pref.bytes(),
-        )),
-        CPlaceInner::Addr(_, _) => unreachable!(),
+    if !fx.clif_comments.enabled() {
+        return;
     }
+    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.debug_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,
+    ));
 }