Skip to content

Commit

Permalink
Implement label calculator for if statement.
Browse files Browse the repository at this point in the history
  • Loading branch information
YSawc committed Oct 31, 2020
1 parent 4460d49 commit e0dcf80
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 6 deletions.
96 changes: 96 additions & 0 deletions src/code_gen/gen_llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub struct Femitter {
pub rc: u8,
pub vr: Vec<u8>,
pub hm: FxHashMap<i32, u8>,
pub lah: u8,
}

impl Femitter {
Expand All @@ -18,6 +19,7 @@ impl Femitter {
rc: 1,
vr: vec![],
hm: FxHashMap::default(),
lah: 0,
}
}
}
Expand Down Expand Up @@ -158,6 +160,37 @@ impl Femitter {
self.rc += 1;
return ();
}
NodeKind::If => {
self.emitter(f, ns.to_owned().cond.unwrap().as_ref().to_owned());
write!(f, " %{} = icmp ne i32 %{}, 0\n", self.rc, self.rc - 1).unwrap();
self.calc_label(ns.to_owned().stmt.unwrap().as_ref().to_owned());
let stmt_lah = self.rc + self.lah + 1;
self.lah = 0;
write!(
f,
" br i1 %{}, label %{}, label %{}\n",
self.rc,
self.rc + 1,
stmt_lah
)
.unwrap();
write!(f, "\n{}:\n", self.rc + 1).unwrap();
self.rc += 2;

self.emitter(f, ns.to_owned().cond.unwrap().as_ref().to_owned());
self.calc_label(ns.to_owned().melse_stmt.unwrap().as_ref().to_owned());
let melse_stmt_lah = self.rc + self.lah + 1;
self.rc += 1;
self.lah = 0;
write!(f, " br label %{}", melse_stmt_lah).unwrap();
write!(f, "\n{}:\n", stmt_lah).unwrap();

self.emitter(f, ns.to_owned().melse_stmt.unwrap().as_ref().to_owned());
self.rc += 1;
write!(f, " br label %{}", melse_stmt_lah).unwrap();
write!(f, "\n{}:\n", melse_stmt_lah).unwrap();
return ();
}
_ => (),
}

Expand Down Expand Up @@ -247,4 +280,67 @@ impl Femitter {
_ => unimplemented!(),
}
}

pub fn calc_label(&mut self, ns: NodeSt) {
// println!("ns.c.value: {:?}", ns.c.value);
match ns.c.value {
NodeKind::Num(_) => {
self.lah += 2;
return ();
}
NodeKind::UnderScore => {
return ();
}
NodeKind::NewVar(_) => {
self.lah += 1;
return ();
}
NodeKind::ReAssignVar(_) => {
return ();
}
NodeKind::GVar(_) => {
self.lah += 1;
return ();
}
NodeKind::LVar(_) => {
self.lah += 1;
return ();
}
NodeKind::If => {
self.lah += 4;
return ();
}
_ => (),
}

self.calc_label(ns.to_owned().lhs.unwrap().as_ref().to_owned());
self.calc_label(ns.to_owned().rhs.unwrap().as_ref().to_owned());

match ns.c.value {
NodeKind::Add => {
self.lah += 1;
}
NodeKind::Sub => {
self.lah += 1;
}
NodeKind::Mul => {
self.lah += 1;
}
NodeKind::Div => {
self.lah += 1;
}
NodeKind::Sur => {
self.lah += 1;
}
NodeKind::E
| NodeKind::NE
| NodeKind::L
| NodeKind::LE
| NodeKind::G
| NodeKind::GE => {
self.lah += 4;
}
_ => unimplemented!(),
}
}
}
53 changes: 47 additions & 6 deletions src/node_arr/node_arr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,10 @@ impl NodeArr {
if it.peek().unwrap().to_owned().to_owned().value == TokenKind::RBrace {
b = true;
}
let mut c = beta(
&mut n.to_owned().cond.unwrap().to_owned(),
ev.to_owned(),
&mut uv,
);

let mut ev = ev.to_owned();
ev.push(l.to_owned());
let mut c = beta(&mut n.to_owned().cond.unwrap(), ev.to_owned(), &mut uv);
c = c.simplified();
match c.c.value {
NodeKind::Num(num) => {
Expand All @@ -324,7 +323,49 @@ impl NodeArr {
*n.to_owned().stmt.unwrap().to_owned()
}
}
_ => unreachable!(),
_ => {
let mut ev = ev.to_owned();
ev.push(l.to_owned());
let n = beta(&mut n.to_owned(), ev.to_owned(), &mut uv);
match n.to_owned().cond.unwrap().c.value {
NodeKind::Ident(s) => {
let mut ev = ev.to_owned();
ev.push(l.to_owned());
match Program::find_v(s.to_owned(), ev.to_owned()) {
Some(mut v) => {
if it.peek().unwrap().to_owned().to_owned().value
== TokenKind::RBrace
{
b = true;
}

if !uv.contains(&v.to_owned().s.to_owned()) {
uv.push(v.to_owned().s.to_owned());
v.aln = aln;
aln += 1;
}

if b {
r = v.to_owned().n.to_owned();
}

let mut _n: NodeSt = NodeSt::default();
if v.gf == 1 {
_n = NodeSt::g_var(s, n.to_owned().c.loc);
} else {
_n = NodeSt::l_var(v.aln, n.to_owned().c.loc);
}
// _n
let mut n = n.to_owned();
n.cond = Some(Box::new(_n));
n
}
_ => unimplemented!(),
}
}
_ => unimplemented!(),
}
}
}
}
_ => {
Expand Down
1 change: 1 addition & 0 deletions src/simplified/simplified.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ impl NodeArr {

impl NodeSt {
pub fn simplified(mut self) -> Self {
// println!("self.c.value: {:?}", self.c.value);
match self.c.value {
NodeKind::Add
| NodeKind::Sub
Expand Down
1 change: 1 addition & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ assert_llvm 0 'int _g = 10; fn { _ }'
assert_llvm 0 'fn { int _u = 8; _ }'
assert_llvm 16 'fn int { int _u = 8; int a = 2; a*_u }'
assert_llvm 34 'fn int { int _u = 8; int a = 2; 4+2*a*_u-2 }'
assert_llvm 1 'fn int {int i = 1; if (i) { 0 } else { 0 } i }'

echo "------------------------------"
echo "All llvm test passed!\n"

0 comments on commit e0dcf80

Please sign in to comment.