|
1 | 1 | use std::io; |
2 | 2 |
|
| 3 | +use super::run; |
3 | 4 | use rustc_middle::ty::TyCtxt; |
4 | | -use stable_mir::{ |
5 | | - mir::{Mutability, Operand, Rvalue, StatementKind}, |
6 | | - ty::{RigidTy, TyKind}, |
7 | | - CrateItem, |
8 | | -}; |
9 | | - |
10 | | -use super::{internal, run}; |
11 | | - |
12 | | -pub fn write_smir_pretty<'tcx>(tcx: TyCtxt<'tcx>, w: &mut dyn io::Write) -> io::Result<()> { |
13 | | - writeln!(w, "// WARNING: This is highly experimental output it's intended for stable-mir developers only.").unwrap(); |
14 | | - 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(); |
15 | 5 |
|
| 6 | +pub fn write_smir_pretty<'tcx, W: io::Write>(tcx: TyCtxt<'tcx>, w: &mut W) -> io::Result<()> { |
| 7 | + writeln!( |
| 8 | + w, |
| 9 | + "// WARNING: This is highly experimental output it's intended for stable-mir developers only." |
| 10 | + )?; |
| 11 | + writeln!( |
| 12 | + w, |
| 13 | + "// If you find a bug or want to improve the output open a issue at https://github.com/rust-lang/project-stable-mir." |
| 14 | + )?; |
16 | 15 | run(tcx, || { |
17 | 16 | let items = stable_mir::all_local_items(); |
18 | | - let _ = items |
19 | | - .iter() |
20 | | - .map(|item| -> io::Result<()> { |
21 | | - // Because we can't return a Result from a closure, we have to unwrap here. |
22 | | - writeln!(w, "{}", function_name(*item, tcx))?; |
23 | | - writeln!(w, "{}", function_body(*item, tcx))?; |
24 | | - let _ = item |
25 | | - .body() |
26 | | - .blocks |
27 | | - .iter() |
28 | | - .enumerate() |
29 | | - .map(|(index, block)| -> io::Result<()> { |
30 | | - writeln!(w, " bb{}: {{", index)?; |
31 | | - let _ = block |
32 | | - .statements |
33 | | - .iter() |
34 | | - .map(|statement| -> io::Result<()> { |
35 | | - writeln!(w, "{}", pretty_statement(&statement.kind, tcx))?; |
36 | | - Ok(()) |
37 | | - }) |
38 | | - .collect::<Vec<_>>(); |
39 | | - writeln!(w, " }}").unwrap(); |
40 | | - Ok(()) |
41 | | - }) |
42 | | - .collect::<Vec<_>>(); |
43 | | - Ok(()) |
44 | | - }) |
45 | | - .collect::<Vec<_>>(); |
| 17 | + let _ = items.iter().map(|item| -> io::Result<()> { item.dump(w) }).collect::<Vec<_>>(); |
46 | 18 | }); |
47 | 19 | Ok(()) |
48 | 20 | } |
49 | | - |
50 | | -pub fn function_name(item: CrateItem, tcx: TyCtxt<'_>) -> String { |
51 | | - let mut name = String::new(); |
52 | | - let body = item.body(); |
53 | | - name.push_str("fn "); |
54 | | - name.push_str(item.name().as_str()); |
55 | | - if body.arg_locals().is_empty() { |
56 | | - name.push_str("()"); |
57 | | - } else { |
58 | | - name.push_str("("); |
59 | | - } |
60 | | - body.arg_locals().iter().for_each(|local| { |
61 | | - name.push_str(format!("_{}: ", local.local).as_str()); |
62 | | - name.push_str(&pretty_ty(local.ty.kind(), tcx)); |
63 | | - }); |
64 | | - if !body.arg_locals().is_empty() { |
65 | | - name.push_str(")"); |
66 | | - } |
67 | | - let return_local = body.ret_local(); |
68 | | - name.push_str(" -> "); |
69 | | - name.push_str(&pretty_ty(return_local.ty.kind(), tcx)); |
70 | | - name.push_str(" {"); |
71 | | - name |
72 | | -} |
73 | | - |
74 | | -pub fn function_body(item: CrateItem, _tcx: TyCtxt<'_>) -> String { |
75 | | - let mut body_str = String::new(); |
76 | | - let body = item.body(); |
77 | | - body.inner_locals().iter().for_each(|local| { |
78 | | - body_str.push_str(" "); |
79 | | - body_str.push_str(format!("let {}", ret_mutability(&local.mutability)).as_str()); |
80 | | - body_str.push_str(format!("_{}: ", local.local).as_str()); |
81 | | - body_str.push_str(format!("{}", pretty_ty(local.ty.kind(), _tcx)).as_str()); |
82 | | - body_str.push_str(";\n"); |
83 | | - }); |
84 | | - body_str.push_str("}"); |
85 | | - body_str |
86 | | -} |
87 | | - |
88 | | -pub fn ret_mutability(mutability: &Mutability) -> String { |
89 | | - match mutability { |
90 | | - Mutability::Not => "".to_string(), |
91 | | - Mutability::Mut => "mut ".to_string(), |
92 | | - } |
93 | | -} |
94 | | - |
95 | | -pub fn pretty_statement(statement: &StatementKind, tcx: TyCtxt<'_>) -> String { |
96 | | - let mut pretty = String::new(); |
97 | | - match statement { |
98 | | - StatementKind::Assign(place, rval) => { |
99 | | - pretty.push_str(format!(" _{} = ", place.local).as_str()); |
100 | | - pretty.push_str(format!("{}", &pretty_rvalue(rval, tcx)).as_str()); |
101 | | - } |
102 | | - StatementKind::FakeRead(_, _) => todo!(), |
103 | | - StatementKind::SetDiscriminant { .. } => todo!(), |
104 | | - StatementKind::Deinit(_) => todo!(), |
105 | | - StatementKind::StorageLive(_) => todo!(), |
106 | | - StatementKind::StorageDead(_) => todo!(), |
107 | | - StatementKind::Retag(_, _) => todo!(), |
108 | | - StatementKind::PlaceMention(_) => todo!(), |
109 | | - StatementKind::AscribeUserType { .. } => todo!(), |
110 | | - StatementKind::Coverage(_) => todo!(), |
111 | | - StatementKind::Intrinsic(_) => todo!(), |
112 | | - StatementKind::ConstEvalCounter => (), |
113 | | - StatementKind::Nop => (), |
114 | | - } |
115 | | - pretty |
116 | | -} |
117 | | - |
118 | | -pub fn pretty_operand(operand: &Operand, _tcx: TyCtxt<'_>) -> String { |
119 | | - let mut pretty = String::new(); |
120 | | - match operand { |
121 | | - Operand::Copy(copy) => { |
122 | | - pretty.push_str(""); |
123 | | - pretty.push_str(format!("{}", copy.local).as_str()); |
124 | | - } |
125 | | - Operand::Move(mv) => { |
126 | | - pretty.push_str("move "); |
127 | | - pretty.push_str(format!("_{}", mv.local).as_str()); |
128 | | - } |
129 | | - Operand::Constant(cnst) => { |
130 | | - pretty.push_str("const "); |
131 | | - pretty.push_str(internal(&cnst.literal).to_string().as_str()); |
132 | | - } |
133 | | - } |
134 | | - pretty |
135 | | -} |
136 | | - |
137 | | -pub fn pretty_rvalue(rval: &Rvalue, tcx: TyCtxt<'_>) -> String { |
138 | | - let mut pretty = String::new(); |
139 | | - match rval { |
140 | | - Rvalue::AddressOf(muta, addr) => { |
141 | | - pretty.push_str("&raw "); |
142 | | - pretty.push_str(&ret_mutability(&muta)); |
143 | | - pretty.push_str(format!("(*_{})", addr.local).as_str()); |
144 | | - } |
145 | | - Rvalue::Aggregate(aggregatekind, operands) => { |
146 | | - pretty.push_str(format!("{:#?}", aggregatekind).as_str()); |
147 | | - pretty.push_str("("); |
148 | | - operands.iter().enumerate().for_each(|(i, op)| { |
149 | | - pretty.push_str(&pretty_operand(op, tcx)); |
150 | | - if i != operands.len() - 1 { |
151 | | - pretty.push_str(", "); |
152 | | - } |
153 | | - }); |
154 | | - pretty.push_str(")"); |
155 | | - } |
156 | | - Rvalue::BinaryOp(bin, op, op2) => { |
157 | | - pretty.push_str(&pretty_operand(op, tcx)); |
158 | | - pretty.push_str(" "); |
159 | | - pretty.push_str(format!("{:#?}", bin).as_str()); |
160 | | - pretty.push_str(" "); |
161 | | - pretty.push_str(&pretty_operand(op2, tcx)); |
162 | | - } |
163 | | - Rvalue::Cast(_, op, ty) => { |
164 | | - pretty.push_str(&pretty_operand(op, tcx)); |
165 | | - pretty.push_str(" as "); |
166 | | - pretty.push_str(&pretty_ty(ty.kind(), tcx)); |
167 | | - } |
168 | | - Rvalue::CheckedBinaryOp(bin, op1, op2) => { |
169 | | - pretty.push_str(&pretty_operand(op1, tcx)); |
170 | | - pretty.push_str(" "); |
171 | | - pretty.push_str(format!("{:#?}", bin).as_str()); |
172 | | - pretty.push_str(" "); |
173 | | - pretty.push_str(&pretty_operand(op2, tcx)); |
174 | | - } |
175 | | - Rvalue::CopyForDeref(deref) => { |
176 | | - pretty.push_str("CopyForDeref"); |
177 | | - pretty.push_str(format!("{}", deref.local).as_str()); |
178 | | - } |
179 | | - Rvalue::Discriminant(place) => { |
180 | | - pretty.push_str("discriminant"); |
181 | | - pretty.push_str(format!("{}", place.local).as_str()); |
182 | | - } |
183 | | - Rvalue::Len(len) => { |
184 | | - pretty.push_str("len"); |
185 | | - pretty.push_str(format!("{}", len.local).as_str()); |
186 | | - } |
187 | | - Rvalue::Ref(_, borrowkind, place) => { |
188 | | - pretty.push_str("ref"); |
189 | | - pretty.push_str(format!("{:#?}", borrowkind).as_str()); |
190 | | - pretty.push_str(format!("{}", place.local).as_str()); |
191 | | - } |
192 | | - Rvalue::Repeat(op, cnst) => { |
193 | | - pretty.push_str(&pretty_operand(op, tcx)); |
194 | | - pretty.push_str(" "); |
195 | | - pretty.push_str(&pretty_ty(cnst.ty().kind(), tcx)); |
196 | | - } |
197 | | - Rvalue::ShallowInitBox(_, _) => todo!(), |
198 | | - Rvalue::ThreadLocalRef(item) => { |
199 | | - pretty.push_str("thread_local_ref"); |
200 | | - pretty.push_str(format!("{:#?}", item).as_str()); |
201 | | - } |
202 | | - Rvalue::NullaryOp(nul, ty) => { |
203 | | - pretty.push_str(format!("{:#?}", nul).as_str()); |
204 | | - pretty.push_str(&&pretty_ty(ty.kind(), tcx)); |
205 | | - pretty.push_str(" "); |
206 | | - } |
207 | | - Rvalue::UnaryOp(un, op) => { |
208 | | - pretty.push_str(&pretty_operand(op, tcx)); |
209 | | - pretty.push_str(" "); |
210 | | - pretty.push_str(format!("{:#?}", un).as_str()); |
211 | | - } |
212 | | - Rvalue::Use(op) => pretty.push_str(&pretty_operand(op, tcx)), |
213 | | - } |
214 | | - pretty |
215 | | -} |
216 | | - |
217 | | -pub fn pretty_ty(ty: TyKind, tcx: TyCtxt<'_>) -> String { |
218 | | - let mut pretty = String::new(); |
219 | | - pretty.push_str(""); |
220 | | - match ty { |
221 | | - TyKind::RigidTy(rigid_ty) => match rigid_ty { |
222 | | - RigidTy::Bool => "bool".to_string(), |
223 | | - RigidTy::Char => "char".to_string(), |
224 | | - RigidTy::Int(i) => match i { |
225 | | - stable_mir::ty::IntTy::Isize => "isize".to_string(), |
226 | | - stable_mir::ty::IntTy::I8 => "i8".to_string(), |
227 | | - stable_mir::ty::IntTy::I16 => "i16".to_string(), |
228 | | - stable_mir::ty::IntTy::I32 => "i32".to_string(), |
229 | | - stable_mir::ty::IntTy::I64 => "i64".to_string(), |
230 | | - stable_mir::ty::IntTy::I128 => "i128".to_string(), |
231 | | - }, |
232 | | - RigidTy::Uint(u) => match u { |
233 | | - stable_mir::ty::UintTy::Usize => "usize".to_string(), |
234 | | - stable_mir::ty::UintTy::U8 => "u8".to_string(), |
235 | | - stable_mir::ty::UintTy::U16 => "u16".to_string(), |
236 | | - stable_mir::ty::UintTy::U32 => "u32".to_string(), |
237 | | - stable_mir::ty::UintTy::U64 => "u64".to_string(), |
238 | | - stable_mir::ty::UintTy::U128 => "u128".to_string(), |
239 | | - }, |
240 | | - RigidTy::Float(f) => match f { |
241 | | - stable_mir::ty::FloatTy::F32 => "f32".to_string(), |
242 | | - stable_mir::ty::FloatTy::F64 => "f64".to_string(), |
243 | | - }, |
244 | | - RigidTy::Adt(def, _) => { |
245 | | - format!("{}", tcx.type_of(internal(&def.0)).instantiate_identity()) |
246 | | - } |
247 | | - RigidTy::Foreign(_) => format!("{:#?}", rigid_ty), |
248 | | - RigidTy::Str => "str".to_string(), |
249 | | - RigidTy::Array(ty, len) => { |
250 | | - format!( |
251 | | - "[{}; {}]", |
252 | | - pretty_ty(ty.kind(), tcx), |
253 | | - internal(&len).try_to_scalar().unwrap() |
254 | | - ) |
255 | | - } |
256 | | - RigidTy::Slice(ty) => { |
257 | | - format!("[{}]", pretty_ty(ty.kind(), tcx)) |
258 | | - } |
259 | | - RigidTy::RawPtr(ty, mutability) => { |
260 | | - pretty.push_str("*"); |
261 | | - match mutability { |
262 | | - Mutability::Not => pretty.push_str("const "), |
263 | | - Mutability::Mut => pretty.push_str("mut "), |
264 | | - } |
265 | | - pretty.push_str(&pretty_ty(ty.kind(), tcx)); |
266 | | - pretty |
267 | | - } |
268 | | - RigidTy::Ref(_, ty, _) => pretty_ty(ty.kind(), tcx), |
269 | | - RigidTy::FnDef(_, _) => format!("{:#?}", rigid_ty), |
270 | | - RigidTy::FnPtr(_) => format!("{:#?}", rigid_ty), |
271 | | - RigidTy::Closure(_, _) => format!("{:#?}", rigid_ty), |
272 | | - RigidTy::Coroutine(_, _, _) => format!("{:#?}", rigid_ty), |
273 | | - RigidTy::Dynamic(data, region, repr) => { |
274 | | - // FIXME: Fix binder printing, it looks ugly now |
275 | | - pretty.push_str("("); |
276 | | - match repr { |
277 | | - stable_mir::ty::DynKind::Dyn => pretty.push_str("dyn "), |
278 | | - stable_mir::ty::DynKind::DynStar => pretty.push_str("dyn* "), |
279 | | - } |
280 | | - pretty.push_str(format!("{:#?}", data).as_str()); |
281 | | - pretty.push_str(format!(" + {:#?} )", region).as_str()); |
282 | | - pretty |
283 | | - } |
284 | | - RigidTy::Never => "!".to_string(), |
285 | | - RigidTy::Tuple(tuple) => { |
286 | | - if tuple.is_empty() { |
287 | | - "()".to_string() |
288 | | - } else { |
289 | | - let mut tuple_str = String::new(); |
290 | | - tuple_str.push_str("("); |
291 | | - tuple.iter().enumerate().for_each(|(i, ty)| { |
292 | | - tuple_str.push_str(&pretty_ty(ty.kind(), tcx)); |
293 | | - if i != tuple.len() - 1 { |
294 | | - tuple_str.push_str(", "); |
295 | | - } |
296 | | - }); |
297 | | - tuple_str.push_str(")"); |
298 | | - tuple_str |
299 | | - } |
300 | | - } |
301 | | - }, |
302 | | - TyKind::Alias(_, _) => format!("{:#?}", ty), |
303 | | - TyKind::Param(param_ty) => { |
304 | | - format!("{:#?}", param_ty.name) |
305 | | - } |
306 | | - TyKind::Bound(_, _) => format!("{:#?}", ty), |
307 | | - } |
308 | | -} |
0 commit comments