Skip to content

Commit

Permalink
better formatting for statements
Browse files Browse the repository at this point in the history
  • Loading branch information
ouz-a committed Nov 9, 2023
1 parent 2537b87 commit 70b9551
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 24 deletions.
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3779,6 +3779,7 @@ dependencies = [
"rustc_query_system",
"rustc_resolve",
"rustc_session",
"rustc_smir",
"rustc_span",
"rustc_symbol_mangling",
"rustc_target",
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_driver_impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ rustc_privacy = { path = "../rustc_privacy" }
rustc_query_system = { path = "../rustc_query_system" }
rustc_resolve = { path = "../rustc_resolve" }
rustc_session = { path = "../rustc_session" }
rustc_span = { path = "../rustc_span" }
rustc_smir ={ path = "../rustc_smir" }
rustc_span = { path = "../rustc_span" }
rustc_symbol_mangling = { path = "../rustc_symbol_mangling" }
rustc_target = { path = "../rustc_target" }
rustc_trait_selection = { path = "../rustc_trait_selection" }
Expand Down
6 changes: 0 additions & 6 deletions compiler/rustc_smir/src/rustc_internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,4 @@ impl<K: PartialEq + Hash + Eq, V: Copy + Debug + PartialEq + IndexedVal> Index<V
pub trait RustcInternal<'tcx> {
type T;
fn internal(&self, tables: &mut Tables<'tcx>) -> Self::T;

/// Use this when you want to convert to a rustc counterpart in user-code.
/// Do not use this within the smir crates themselves.
fn internal_via_tls(&self) -> Self::T {
with_tables(|tables| self.internal(tables))
}
}
63 changes: 46 additions & 17 deletions compiler/rustc_smir/src/rustc_internal/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,23 @@ use stable_mir::{
CrateItem,
};

use super::{run, RustcInternal};
use super::{internal, run};

pub fn write_smir_pretty<'tcx>(tcx: TyCtxt<'tcx>, w: &mut dyn io::Write) -> io::Result<()> {
writeln!(w, "// WARNING: This is highly experimental output it's intended for stable-mir developers only.").unwrap();
writeln!(w, "// If you find a bug or want to improve the output open a issue at https://github.com/rust-lang/project-stable-mir.").unwrap();
run(tcx, || {
let items = stable_mir::all_local_items();
items.iter().for_each(|item| {
// Because we can't return a Result from a closure, we have to unwrap here.
writeln!(w, "{}", function_name(*item, tcx)).unwrap();
writeln!(w, "{}", function_body(*item, tcx)).unwrap();
writeln!(w, "------------------").unwrap();
item.body().blocks.iter().for_each(|block| {
item.body().blocks.iter().enumerate().for_each(|(index, block)| {
writeln!(w, " bb{}: {{", index).unwrap();
block.statements.iter().for_each(|statement| {
writeln!(w, "{}", pretty_statement(&statement.kind, tcx)).unwrap();
});
writeln!(w, " }}").unwrap();
})
})
});
Expand Down Expand Up @@ -76,8 +79,8 @@ pub fn pretty_statement(statement: &StatementKind, tcx: TyCtxt<'_>) -> String {
let mut pretty = String::new();
match statement {
StatementKind::Assign(place, rval) => {
pretty.push_str(format!("_{} = ", place.local).as_str());
pretty.push_str(&pretty_rvalue(rval, tcx))
pretty.push_str(format!(" _{} = ", place.local).as_str());
pretty.push_str(format!("{}", &pretty_rvalue(rval, tcx)).as_str());
}
StatementKind::FakeRead(_, _) => todo!(),
StatementKind::SetDiscriminant { .. } => todo!(),
Expand All @@ -103,12 +106,12 @@ pub fn pretty_operand(operand: &Operand, _tcx: TyCtxt<'_>) -> String {
pretty.push_str(format!("{}", copy.local).as_str());
}
Operand::Move(mv) => {
pretty.push_str("move");
pretty.push_str(format!("{}", mv.local).as_str());
pretty.push_str("move ");
pretty.push_str(format!("_{}", mv.local).as_str());
}
Operand::Constant(cnst) => {
pretty.push_str("const ");
pretty.push_str(cnst.literal.internal_via_tls().to_string().as_str());
pretty.push_str(internal(&cnst.literal).to_string().as_str());
}
}
pretty
Expand All @@ -118,9 +121,9 @@ pub fn pretty_rvalue(rval: &Rvalue, tcx: TyCtxt<'_>) -> String {
let mut pretty = String::new();
match rval {
Rvalue::AddressOf(muta, addr) => {
pretty.push_str("address_of");
pretty.push_str("&raw ");
pretty.push_str(&ret_mutability(&muta));
pretty.push_str(format!("{}", addr.local).as_str());
pretty.push_str(format!("(*_{})", addr.local).as_str());
}
Rvalue::Aggregate(aggregatekind, operands) => {
pretty.push_str(format!("{:#?}", aggregatekind).as_str());
Expand Down Expand Up @@ -222,21 +225,45 @@ pub fn pretty_ty(ty: TyKind, tcx: TyCtxt<'_>) -> String {
stable_mir::ty::FloatTy::F64 => "f64".to_string(),
},
RigidTy::Adt(def, _) => {
format!("{:#?}", tcx.type_of(def.0.internal_via_tls()).instantiate_identity())
format!("{}", tcx.type_of(internal(&def.0)).instantiate_identity())
}
RigidTy::Foreign(_) => format!("{:#?}", rigid_ty),
RigidTy::Str => "str".to_string(),
RigidTy::Array(_ty, len) => {
format!("[{};{:#?}]", 1, len.internal_via_tls())
RigidTy::Array(ty, len) => {
format!(
"[{}; {}]",
pretty_ty(ty.kind(), tcx),
internal(&len).try_to_scalar().unwrap()
)
}
RigidTy::Slice(ty) => {
format!("[{}]", pretty_ty(ty.kind(), tcx))
}
RigidTy::RawPtr(ty, mutability) => {
pretty.push_str("*");
match mutability {
Mutability::Not => pretty.push_str("const "),
Mutability::Mut => pretty.push_str("mut "),
}
pretty.push_str(&pretty_ty(ty.kind(), tcx));
pretty
}
RigidTy::Slice(ty) => pretty_ty(ty.kind(), tcx),
RigidTy::RawPtr(_, _) => format!("{:#?}", rigid_ty),
RigidTy::Ref(_, ty, _) => pretty_ty(ty.kind(), tcx),
RigidTy::FnDef(_, _) => format!("{:#?}", rigid_ty),
RigidTy::FnPtr(_) => format!("{:#?}", rigid_ty),
RigidTy::Closure(_, _) => format!("{:#?}", rigid_ty),
RigidTy::Coroutine(_, _, _) => format!("{:#?}", rigid_ty),
RigidTy::Dynamic(_, _, _) => format!("{:#?}", rigid_ty),
RigidTy::Dynamic(data, region, repr) => {
// FIXME: Fix binder printing, it looks ugly now
pretty.push_str("(");
match repr {
stable_mir::ty::DynKind::Dyn => pretty.push_str("dyn "),
stable_mir::ty::DynKind::DynStar => pretty.push_str("dyn* "),
}
pretty.push_str(format!("{:#?}", data).as_str());
pretty.push_str(format!(" + {:#?} )", region).as_str());
pretty
}
RigidTy::Never => "!".to_string(),
RigidTy::Tuple(tuple) => {
if tuple.is_empty() {
Expand All @@ -256,7 +283,9 @@ pub fn pretty_ty(ty: TyKind, tcx: TyCtxt<'_>) -> String {
}
},
TyKind::Alias(_, _) => format!("{:#?}", ty),
TyKind::Param(_) => format!("{:#?}", ty),
TyKind::Param(param_ty) => {
format!("{:#?}", param_ty.name)
}
TyKind::Bound(_, _) => format!("{:#?}", ty),
}
}

0 comments on commit 70b9551

Please sign in to comment.