Skip to content

Commit 2d929b2

Browse files
added unit to IR
1 parent 26a4382 commit 2d929b2

5 files changed

Lines changed: 93 additions & 72 deletions

File tree

src/backend/kerboscript.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,16 @@ impl<'e> KosEmitter<'e> {
3131
self.out.push_str(&format!("l{}", id.index()))
3232
}
3333

34+
fn emit_const(&mut self, c: &Const) {
35+
match c {
36+
Conts::Number(n) => self.out.push_str(&n.to_string()),
37+
Const::Unit => self.out.push_str("0"),
38+
}
39+
}
40+
3441
fn emit_value(&mut self, v: &Value) {
3542
match v {
36-
Value::ConstNumber(n) => self.out.push_str(&n.to_string()),
43+
Value::Const(c) => self.emit_const(c),
3744
Value::Temp(id) => {
3845
let instr = *self.temp_exprs.get(id).unwrap();
3946
self.emit_instr(instr);
@@ -51,8 +58,8 @@ impl<'e> KosEmitter<'e> {
5158
match instr {
5259
Instr::LoadConst { dst, value } => {
5360
if self.temp_exprs.insert(*dst, instr).is_some() {
54-
self.emit(&value.to_string());
55-
self.temp_exprs.remove(dst);
61+
self.emit_const(value);
62+
// self.temp_exprs.remove(dst);
5663
}
5764
}
5865
Instr::Binary { dst, op, lhs, rhs } => {
@@ -68,7 +75,7 @@ impl<'e> KosEmitter<'e> {
6875
self.emit(op_str);
6976
self.emit(" ");
7077
self.emit_value(rhs);
71-
self.temp_exprs.remove(dst);
78+
// self.temp_exprs.remove(dst);
7279
}
7380
}
7481
Instr::Store { place, value } => {
@@ -82,7 +89,7 @@ impl<'e> KosEmitter<'e> {
8289
Instr::Load { dst, place} => {
8390
if self.temp_exprs.insert(*dst, instr).is_some() {
8491
self.emit_place(place);
85-
self.temp_exprs.remove(dst);
92+
// self.temp_exprs.remove(dst);
8693
}
8794
}
8895
Instr::Poision { .. } => {

src/ir/ir.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub enum Terminator {
4040

4141
#[derive(Debug, PartialEq)]
4242
pub enum Instr {
43-
LoadConst { dst: TempId, value: f64 },
43+
LoadConst { dst: TempId, value: Const },
4444
Binary { dst: TempId, op: BinaryOp, lhs: Value, rhs: Value },
4545
Store { place: Place, value: Value },
4646
Load { dst: TempId, place: Place },
@@ -49,11 +49,17 @@ pub enum Instr {
4949

5050
#[derive(Debug, PartialEq)]
5151
pub enum Value {
52-
ConstNumber(f64),
52+
Const(Const),
5353
Temp(TempId),
5454
}
5555

5656
#[derive(Debug, PartialEq)]
5757
pub enum Place {
5858
Local(LocalId)
5959
}
60+
61+
#[derive(Debug, PartialEq)]
62+
pub enum Const {
63+
Number(f64),
64+
Unit,
65+
}

src/ir/lowerer.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl FunctionIRBuilder<'_> {
9797
let dst = self.new_temp(expr.ty);
9898
self.emit(Instr::LoadConst {
9999
dst,
100-
value
100+
value: Const::Number(value)
101101
});
102102
dst
103103
}
@@ -137,7 +137,11 @@ impl FunctionIRBuilder<'_> {
137137
Some(expr) => self.lower_expr(*expr),
138138
None => {
139139
let dst = self.new_temp(Type::Unit);
140-
todo!("Load unit into temporary")
140+
self.emit(Instr::LoadConst {
141+
dst,
142+
value: Const::Unit,
143+
});
144+
dst
141145
}
142146
}
143147
}

src/ir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
mod ir;
22
mod lowerer;
3-
mod pretty;
3+
pub mod pretty;
44

55
pub use ir::*;
66
pub use lowerer::{BlockId, TempId, LocalId};

src/ir/pretty.rs

Lines changed: 66 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,86 @@
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};
22

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(),
97
}
108
}
119

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()),
1714
}
1815
}
1916

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()
4752
}
4853
}
4954
}
5055
}
5156

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(),
5962
}
6063
}
6164

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)));
6870
}
71+
s.push_str(format!(" {}\n", format_terminator(&b.terminator)));
72+
s
6973
}
7074

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))
8184
}
85+
s
8286
}

0 commit comments

Comments
 (0)