|
1 | | -use crate::ir::ir::{BinaryOp, Block, FunctionIR, Instr, Place, Terminator, Value}; |
| 1 | +use crate::ir::ir::{BinaryOp, Block, FunctionIR, Instr, Place, Terminator, Value, Const}; |
2 | 2 |
|
3 | | -impl std::fmt::Display for Value { |
4 | | - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
5 | | - match self { |
6 | | - Value::Temp(t) => write!(f, "t{}", t.index()), |
7 | | - Value::ConstNumber(n) => write!(f, "const {}", n), |
8 | | - } |
| 3 | +pub fn format_const(c: &Const) -> String { |
| 4 | + match c { |
| 5 | + Const::Number(n) => n.to_string(), |
| 6 | + Const::Unit => "unit".to_string(), |
9 | 7 | } |
10 | 8 | } |
11 | 9 |
|
12 | | -impl std::fmt::Display for Place { |
13 | | - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
14 | | - match self { |
15 | | - Place::Local(l) => write!(f, "l{}", l.index()), |
16 | | - } |
| 10 | +pub fn format_value(v: &Value) -> String { |
| 11 | + match v { |
| 12 | + Value::Const(c) => format!("const {}", format_const(c)), |
| 13 | + Value::Temp(id) => format!("t{}", t.index()), |
17 | 14 | } |
18 | 15 | } |
19 | 16 |
|
20 | | -impl std::fmt::Display for Instr { |
21 | | - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
22 | | - match self { |
23 | | - Instr::LoadConst { dst, value } => { |
24 | | - write!(f, "t{} = const {}", dst.index(), value) |
25 | | - } |
26 | | - Instr::Binary { dst, op, lhs, rhs } => { |
27 | | - let op_str = match op { |
28 | | - BinaryOp::Add => "+", |
29 | | - BinaryOp::Sub => "-", |
30 | | - BinaryOp::Mul => "*", |
31 | | - BinaryOp::Div => "/", |
32 | | - }; |
33 | | - write!(f, "t{} = {} {} {}", dst.index(), lhs, op_str, rhs) |
34 | | - } |
35 | | - Instr::Store { place, value } => { |
36 | | - write!(f, "store {}, {}", place, value) |
37 | | - } |
38 | | - Instr::Load { dst, place } => { |
39 | | - write!(f, "t{} = load {}", dst.index(), place) |
40 | | - } |
41 | | - Instr::Poision { dst } => { |
42 | | - if let Some(dst) = dst { |
43 | | - write!(f, "poison t{}", dst.index()) |
44 | | - } else { |
45 | | - write!(f,"posion") |
46 | | - } |
| 17 | +pub fn format_place(p: &Place) -> String { |
| 18 | + match p { |
| 19 | + Place::Local(l) => format!("l{}", l.index()), |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +pub fn format_instr(i: &Instr) -> String { |
| 24 | + match i { |
| 25 | + Instr::LoadConst { dst, value } => { |
| 26 | + format!("t{} = const {}", dst.index(), format_const(value)) |
| 27 | + } |
| 28 | + Instr::Binary { dst, op, lhs, rhs } => { |
| 29 | + let op_str = match op { |
| 30 | + BinaryOp::Add => "+", |
| 31 | + BinaryOp::Sub => "-", |
| 32 | + BinaryOp::Mul => "*", |
| 33 | + BinaryOp::Div => "/", |
| 34 | + }; |
| 35 | + format!(f, "t{} = {} {} {}", dst.index(), |
| 36 | + format_value(lhs), |
| 37 | + op_str, |
| 38 | + format_value(rhs) |
| 39 | + ) |
| 40 | + } |
| 41 | + Instr::Store { place, value } => { |
| 42 | + format!("store {}, {}", format_place(place), format_value(value)) |
| 43 | + } |
| 44 | + Instr::Load { dst, place } => { |
| 45 | + format!("t{} = load {}", dst.index(), format_place(place)) |
| 46 | + } |
| 47 | + Instr::Poision { dst } => { |
| 48 | + if let Some(dst) = dst { |
| 49 | + format!("poison t{}", dst.index()) |
| 50 | + } else { |
| 51 | + "posion".to_string() |
47 | 52 | } |
48 | 53 | } |
49 | 54 | } |
50 | 55 | } |
51 | 56 |
|
52 | | -impl std::fmt::Display for Terminator { |
53 | | - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
54 | | - match self { |
55 | | - Terminator::Goto(b) => write!(f, "goto block{}", b.index()), |
56 | | - Terminator::Return(v) => write!(f, "return {}", v), |
57 | | - Terminator::Unreachable => write!(f, "unreachable"), |
58 | | - } |
| 57 | +pub fn format_terminator(t: &Terminator) -> String { |
| 58 | + match t { |
| 59 | + Terminator::Goto(b) => format!("goto block{}", b.index()), |
| 60 | + Terminator::Return(v) => format!("return {}", format_value(v)), |
| 61 | + Terminator::Unreachable => "unreachable".to_string(), |
59 | 62 | } |
60 | 63 | } |
61 | 64 |
|
62 | | -impl std::fmt::Display for Block { |
63 | | - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
64 | | - for instr in &self.instrs { |
65 | | - write!(f, " {}\n", instr)?; |
66 | | - } |
67 | | - write!(f, " {}\n", self.terminator) |
| 65 | +pub fn format_block(b: &Block, id: usize) -> String { |
| 66 | + let mut s = String::new(); |
| 67 | + s.push_str(&format!("block{}:\n", id)); |
| 68 | + for instr in &b.instrs { |
| 69 | + s.push_str(&format!(" {}\n", format_instr(instr))); |
68 | 70 | } |
| 71 | + s.push_str(format!(" {}\n", format_terminator(&b.terminator))); |
| 72 | + s |
69 | 73 | } |
70 | 74 |
|
71 | | -impl std::fmt::Display for FunctionIR { |
72 | | - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
73 | | - write!(f, "locals:\n")?; |
74 | | - for (i, local) in self.locals.iter().enumerate() { |
75 | | - write!(f, " l{}: {:?}\n", i, local.ty)?; |
76 | | - } |
77 | | - for (i, block) in self.blocks.iter().enumerate() { |
78 | | - write!(f, "block{}:\n{}",i,block)?; |
79 | | - } |
80 | | - Ok(()) |
| 75 | +pub fn format_function(f: &FunctionIR, name: &str) -> String { |
| 76 | + let mut s = String::new(); |
| 77 | + s.push_str(&format!("fn {}:\n", name)); |
| 78 | + s.push_str("locals:\n"); |
| 79 | + for (i, local) in f.locals.iter().enumerate() { |
| 80 | + s.push_str(&format!(" l{}: {:?}\n", i, local.ty)); |
| 81 | + } |
| 82 | + for (i, block) in f.blocks.iter().enumerate() { |
| 83 | + s.push_str(&format_block(block, i)) |
81 | 84 | } |
| 85 | + s |
82 | 86 | } |
0 commit comments