From c80637cf6c62e9ad1b1de78ae14907391946781f Mon Sep 17 00:00:00 2001 From: Natapat Samutpong Date: Thu, 27 Jan 2022 09:12:49 +0700 Subject: [PATCH] feat: simplify if compilation --- blspc/src/compiler/compile.rs | 9 +++------ blspc/src/vm/instr.rs | 13 ++----------- 2 files changed, 5 insertions(+), 17 deletions(-) diff --git a/blspc/src/compiler/compile.rs b/blspc/src/compiler/compile.rs index ae6c09c..373bb2c 100644 --- a/blspc/src/compiler/compile.rs +++ b/blspc/src/compiler/compile.rs @@ -50,12 +50,9 @@ impl Compiler { "if" => { // TODO: Remove .clone() let mut cond = self.compile(cdr[0].clone(), depth + 1)?; - let cond_register = self.current_register(); - result.append(&mut cond); - result.push(Instr::JumpIfFalse { - cond: cond_register, + result.push(Instr::PopJumpIfFalse { to: 999, // To be replaced later label: self.next_label(), }); @@ -68,8 +65,8 @@ impl Compiler { let idx = result.len() - 1; match result[idx] { - Instr::JumpIfFalse { cond: c, to: _, label: l } => { - result[idx] = Instr::JumpIfFalse { cond: c, to: else_label, label: l, }; + Instr::PopJumpIfFalse { to: _, label: l } => { + result[idx] = Instr::PopJumpIfFalse { to: else_label, label: l, }; } _ => unreachable!(), } diff --git a/blspc/src/vm/instr.rs b/blspc/src/vm/instr.rs index 625df5f..ac95168 100644 --- a/blspc/src/vm/instr.rs +++ b/blspc/src/vm/instr.rs @@ -81,14 +81,9 @@ pub enum Instr { // Stack arithmetic. Add { label: usize }, Sub { label: usize }, Mul { label: usize }, Div { label: usize }, - // Immediate arithmetic. - IAdd { lhs: Register, rhs: Register, to: Register, label: usize }, - ISub { lhs: Register, rhs: Register, to: Register, label: usize }, - IMul { lhs: Register, rhs: Register, to: Register, label: usize }, - IDiv { lhs: Register, rhs: Register, to: Register, label: usize }, // Jumping Jump { to: usize, label: usize }, - JumpIfFalse { cond: Register, to: usize, label: usize }, + PopJumpIfFalse { to: usize, label: usize }, Return { value: Register, label: usize }, } @@ -105,12 +100,8 @@ impl Display for Instr { Instr::Sub { label } => write!(f, "{}: SUB", label), Instr::Mul { label } => write!(f, "{}: MUL", label), Instr::Div { label } => write!(f, "{}: DIV", label), - Instr::IAdd { lhs, rhs, to, label } => write!(f, "{}: IADD {} {} {}", label, lhs, rhs, to), - Instr::ISub { lhs, rhs, to, label } => write!(f, "{}: ISUB {} {} {}", label, lhs, rhs, to), - Instr::IMul { lhs, rhs, to, label } => write!(f, "{}: IMUL {} {} {}", label, lhs, rhs, to), - Instr::IDiv { lhs, rhs, to, label } => write!(f, "{}: IDIV {} {} {}", label, lhs, rhs, to), Instr::Jump { to, label } => write!(f, "{}: JUMP {}", label, to), - Instr::JumpIfFalse { cond, to, label } => write!(f, "{}: JUMP_IF_FALSE {} {}", label, cond, to), + Instr::PopJumpIfFalse { to, label } => write!(f, "{}: POP_JUMP_IF_FALSE {}", label, to), Instr::Return { value, label } => write!(f, "{}: RETURN {}", label, value), } }