|
1 | 1 | use crate::crate_def::CrateDef;
|
2 |
| -use crate::mir::{Operand, Rvalue, StatementKind}; |
| 2 | +use crate::mir::{Operand, Rvalue, StatementKind, UnwindAction}; |
3 | 3 | use crate::ty::{DynKind, FloatTy, IntTy, RigidTy, TyKind, UintTy};
|
4 | 4 | use crate::{with, Body, CrateItem, Mutability};
|
| 5 | +use std::io::Write; |
| 6 | +use std::{io, iter}; |
| 7 | + |
| 8 | +use super::{AssertMessage, BinOp, TerminatorKind}; |
5 | 9 |
|
6 | 10 | pub fn function_name(item: CrateItem) -> String {
|
7 | 11 | let mut pretty_name = String::new();
|
@@ -70,6 +74,209 @@ pub fn pretty_statement(statement: &StatementKind) -> String {
|
70 | 74 | pretty
|
71 | 75 | }
|
72 | 76 |
|
| 77 | +pub fn pretty_terminator<W: io::Write>(terminator: &TerminatorKind, w: &mut W) -> io::Result<()> { |
| 78 | + write!(w, "{}", pretty_terminator_head(terminator))?; |
| 79 | + let successor_count = terminator.successors().count(); |
| 80 | + let labels = pretty_successor_labels(terminator); |
| 81 | + |
| 82 | + let show_unwind = !matches!(terminator.unwind(), None | Some(UnwindAction::Cleanup(_))); |
| 83 | + let fmt_unwind = |fmt: &mut dyn Write| -> io::Result<()> { |
| 84 | + write!(fmt, "unwind ")?; |
| 85 | + match terminator.unwind() { |
| 86 | + None | Some(UnwindAction::Cleanup(_)) => unreachable!(), |
| 87 | + Some(UnwindAction::Continue) => write!(fmt, "continue"), |
| 88 | + Some(UnwindAction::Unreachable) => write!(fmt, "unreachable"), |
| 89 | + Some(UnwindAction::Terminate) => write!(fmt, "terminate"), |
| 90 | + } |
| 91 | + }; |
| 92 | + |
| 93 | + match (successor_count, show_unwind) { |
| 94 | + (0, false) => Ok(()), |
| 95 | + (0, true) => { |
| 96 | + write!(w, " -> ")?; |
| 97 | + fmt_unwind(w)?; |
| 98 | + Ok(()) |
| 99 | + } |
| 100 | + (1, false) => { |
| 101 | + write!(w, " -> {:?}", terminator.successors().next().unwrap())?; |
| 102 | + Ok(()) |
| 103 | + } |
| 104 | + _ => { |
| 105 | + write!(w, " -> [")?; |
| 106 | + for (i, target) in terminator.successors().enumerate() { |
| 107 | + if i > 0 { |
| 108 | + write!(w, ", ")?; |
| 109 | + } |
| 110 | + write!(w, "{}: bb{:?}", labels[i], target)?; |
| 111 | + } |
| 112 | + if show_unwind { |
| 113 | + write!(w, ", ")?; |
| 114 | + fmt_unwind(w)?; |
| 115 | + } |
| 116 | + write!(w, "]") |
| 117 | + } |
| 118 | + }?; |
| 119 | + |
| 120 | + Ok(()) |
| 121 | +} |
| 122 | + |
| 123 | +pub fn pretty_terminator_head(terminator: &TerminatorKind) -> String { |
| 124 | + use self::TerminatorKind::*; |
| 125 | + let mut pretty = String::new(); |
| 126 | + match terminator { |
| 127 | + Goto { .. } => format!(" goto"), |
| 128 | + SwitchInt { discr, .. } => { |
| 129 | + format!(" switchInt(_{})", pretty_operand(discr)) |
| 130 | + } |
| 131 | + Resume => format!(" resume"), |
| 132 | + Abort => format!(" abort"), |
| 133 | + Return => format!(" return"), |
| 134 | + Unreachable => format!(" unreachable"), |
| 135 | + Drop { place, .. } => format!(" drop(_{:?})", place.local), |
| 136 | + Call { func, args, destination, .. } => { |
| 137 | + pretty.push_str(" "); |
| 138 | + pretty.push_str(format!("_{} = ", destination.local).as_str()); |
| 139 | + pretty.push_str(&pretty_operand(func)); |
| 140 | + pretty.push_str("("); |
| 141 | + args.iter().enumerate().for_each(|(i, arg)| { |
| 142 | + if i > 0 { |
| 143 | + pretty.push_str(", "); |
| 144 | + } |
| 145 | + pretty.push_str(&pretty_operand(arg)); |
| 146 | + }); |
| 147 | + pretty.push_str(")"); |
| 148 | + pretty |
| 149 | + } |
| 150 | + Assert { cond, expected, msg, target: _, unwind: _ } => { |
| 151 | + pretty.push_str(" assert("); |
| 152 | + if !expected { |
| 153 | + pretty.push_str("!"); |
| 154 | + } |
| 155 | + pretty.push_str(format!("{} bool),", &pretty_operand(cond)).as_str()); |
| 156 | + pretty.push_str(&pretty_assert_message(msg)); |
| 157 | + pretty.push_str(")"); |
| 158 | + pretty |
| 159 | + } |
| 160 | + CoroutineDrop => format!(" coroutine_drop"), |
| 161 | + InlineAsm { .. } => todo!(), |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +pub fn pretty_successor_labels(terminator: &TerminatorKind) -> Vec<String> { |
| 166 | + use self::TerminatorKind::*; |
| 167 | + match terminator { |
| 168 | + Resume | Abort | Return | Unreachable | CoroutineDrop => vec![], |
| 169 | + Goto { .. } => vec!["".to_string()], |
| 170 | + SwitchInt { targets, .. } => targets |
| 171 | + .value |
| 172 | + .iter() |
| 173 | + .map(|target| format!("{}", target)) |
| 174 | + .chain(iter::once("otherwise".into())) |
| 175 | + .collect(), |
| 176 | + Drop { unwind: UnwindAction::Cleanup(_), .. } => vec!["return".into(), "unwind".into()], |
| 177 | + Drop { unwind: _, .. } => vec!["return".into()], |
| 178 | + Call { target: Some(_), unwind: UnwindAction::Cleanup(_), .. } => { |
| 179 | + vec!["return".into(), "unwind".into()] |
| 180 | + } |
| 181 | + Call { target: Some(_), unwind: _, .. } => vec!["return".into()], |
| 182 | + Call { target: None, unwind: UnwindAction::Cleanup(_), .. } => vec!["unwind".into()], |
| 183 | + Call { target: None, unwind: _, .. } => vec![], |
| 184 | + Assert { unwind: UnwindAction::Cleanup(_), .. } => { |
| 185 | + vec!["success".into(), "unwind".into()] |
| 186 | + } |
| 187 | + Assert { unwind: _, .. } => vec!["success".into()], |
| 188 | + InlineAsm { .. } => todo!(), |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +pub fn pretty_assert_message(msg: &AssertMessage) -> String { |
| 193 | + let mut pretty = String::new(); |
| 194 | + match msg { |
| 195 | + AssertMessage::BoundsCheck { len, index } => { |
| 196 | + let pretty_len = pretty_operand(len); |
| 197 | + let pretty_index = pretty_operand(index); |
| 198 | + pretty.push_str(format!("\"index out of bounds: the length is {{}} but the index is {{}}\", {pretty_len}, {pretty_index}").as_str()); |
| 199 | + pretty |
| 200 | + } |
| 201 | + AssertMessage::Overflow(BinOp::Add, l, r) => { |
| 202 | + let pretty_l = pretty_operand(l); |
| 203 | + let pretty_r = pretty_operand(r); |
| 204 | + pretty.push_str(format!("\"attempt to compute `{{}} + {{}}`, which would overflow\", {pretty_l}, {pretty_r}").as_str()); |
| 205 | + pretty |
| 206 | + } |
| 207 | + AssertMessage::Overflow(BinOp::Sub, l, r) => { |
| 208 | + let pretty_l = pretty_operand(l); |
| 209 | + let pretty_r = pretty_operand(r); |
| 210 | + pretty.push_str(format!("\"attempt to compute `{{}} - {{}}`, which would overflow\", {pretty_l}, {pretty_r}").as_str()); |
| 211 | + pretty |
| 212 | + } |
| 213 | + AssertMessage::Overflow(BinOp::Mul, l, r) => { |
| 214 | + let pretty_l = pretty_operand(l); |
| 215 | + let pretty_r = pretty_operand(r); |
| 216 | + pretty.push_str(format!("\"attempt to compute `{{}} * {{}}`, which would overflow\", {pretty_l}, {pretty_r}").as_str()); |
| 217 | + pretty |
| 218 | + } |
| 219 | + AssertMessage::Overflow(BinOp::Div, l, r) => { |
| 220 | + let pretty_l = pretty_operand(l); |
| 221 | + let pretty_r = pretty_operand(r); |
| 222 | + pretty.push_str(format!("\"attempt to compute `{{}} / {{}}`, which would overflow\", {pretty_l}, {pretty_r}").as_str()); |
| 223 | + pretty |
| 224 | + } |
| 225 | + AssertMessage::Overflow(BinOp::Rem, l, r) => { |
| 226 | + let pretty_l = pretty_operand(l); |
| 227 | + let pretty_r = pretty_operand(r); |
| 228 | + pretty.push_str(format!("\"attempt to compute `{{}} % {{}}`, which would overflow\", {pretty_l}, {pretty_r}").as_str()); |
| 229 | + pretty |
| 230 | + } |
| 231 | + AssertMessage::Overflow(BinOp::Shr, _, r) => { |
| 232 | + let pretty_r = pretty_operand(r); |
| 233 | + pretty.push_str( |
| 234 | + format!("\"attempt to shift right by `{{}}`, which would overflow\", {pretty_r}") |
| 235 | + .as_str(), |
| 236 | + ); |
| 237 | + pretty |
| 238 | + } |
| 239 | + AssertMessage::Overflow(BinOp::Shl, _, r) => { |
| 240 | + let pretty_r = pretty_operand(r); |
| 241 | + pretty.push_str( |
| 242 | + format!("\"attempt to shift left by `{{}}`, which would overflow\", {pretty_r}") |
| 243 | + .as_str(), |
| 244 | + ); |
| 245 | + pretty |
| 246 | + } |
| 247 | + AssertMessage::OverflowNeg(op) => { |
| 248 | + let pretty_op = pretty_operand(op); |
| 249 | + pretty.push_str( |
| 250 | + format!("\"attempt to negate `{{}}`, which would overflow\", {pretty_op}").as_str(), |
| 251 | + ); |
| 252 | + pretty |
| 253 | + } |
| 254 | + AssertMessage::DivisionByZero(op) => { |
| 255 | + let pretty_op = pretty_operand(op); |
| 256 | + pretty.push_str(format!("\"attempt to divide `{{}}` by zero\", {pretty_op}").as_str()); |
| 257 | + pretty |
| 258 | + } |
| 259 | + AssertMessage::RemainderByZero(op) => { |
| 260 | + let pretty_op = pretty_operand(op); |
| 261 | + pretty.push_str( |
| 262 | + format!("\"attempt to calculate the remainder of `{{}}` with a divisor of zero\", {pretty_op}").as_str(), |
| 263 | + ); |
| 264 | + pretty |
| 265 | + } |
| 266 | + AssertMessage::ResumedAfterReturn(_) => { |
| 267 | + format!("attempt to resume a generator after completion") |
| 268 | + } |
| 269 | + AssertMessage::ResumedAfterPanic(_) => format!("attempt to resume a panicked generator"), |
| 270 | + AssertMessage::MisalignedPointerDereference { required, found } => { |
| 271 | + let pretty_required = pretty_operand(required); |
| 272 | + let pretty_found = pretty_operand(found); |
| 273 | + pretty.push_str(format!("\"misaligned pointer dereference: address must be a multiple of {{}} but is {{}}\",{pretty_required}, {pretty_found}").as_str()); |
| 274 | + pretty |
| 275 | + } |
| 276 | + _ => todo!(), |
| 277 | + } |
| 278 | +} |
| 279 | + |
73 | 280 | pub fn pretty_operand(operand: &Operand) -> String {
|
74 | 281 | let mut pretty = String::new();
|
75 | 282 | match operand {
|
|
0 commit comments