From 8ec6546ed22488093fd28c4a01eb4418eb6d5c5e Mon Sep 17 00:00:00 2001 From: Natapat Samutpong Date: Thu, 27 Jan 2022 09:03:06 +0700 Subject: [PATCH] feat: more simpler math compiling --- blspc/src/compiler/compile.rs | 62 ++++++++--------------------------- blspc/src/vm/instr.rs | 11 +++++++ 2 files changed, 24 insertions(+), 49 deletions(-) diff --git a/blspc/src/compiler/compile.rs b/blspc/src/compiler/compile.rs index 3433abd..ae6c09c 100644 --- a/blspc/src/compiler/compile.rs +++ b/blspc/src/compiler/compile.rs @@ -111,34 +111,26 @@ impl Compiler { match atom { Int(i) => { - let r = self.next_register(); - result.push(Instr::Store { - address: r, + result.push(Instr::Push { value: Type::Int(*i), label: self.next_label(), }); } Float(f) => { - let r = self.next_register(); - result.push(Instr::Store { - address: r, + result.push(Instr::Push { value: Type::Float(*f), label: self.next_label(), }); } Boolean(b) => { - let r = self.next_register(); - result.push(Instr::Store { - address: r, + result.push(Instr::Push { value: Type::Boolean(*b), label: self.next_label(), }); } Str(s) => { - let r = self.next_register(); - result.push(Instr::Store { - address: r, - value: Type::String(s.to_string()), + result.push(Instr::Push { + value: Type::String(s.clone()), label: self.next_label(), }); } @@ -158,6 +150,10 @@ impl Compiler { let mut arg = self.compile_atom(&args[0], depth + 1)?; result.append(&mut arg); let arg_pointer = self.current_register(); + result.push(Instr::Pop { + address: arg_pointer, + label: self.next_label(), + }); let call_register = self.next_register(); result.push(Instr::Store { @@ -174,71 +170,39 @@ impl Compiler { }, "add" | "+" => { let mut lhs = self.compile_atom(&args[0], depth + 1)?; - let lhs_pointer = self.current_register(); result.append(&mut lhs); let mut rhs = self.compile_atom(&args[1], depth + 1)?; - let rhs_pointer = self.current_register(); result.append(&mut rhs); - let result_register = self.next_register(); - result.push(Instr::IAdd { - lhs: lhs_pointer, - rhs: rhs_pointer, - to: result_register, - label: self.next_label(), - }); + result.push(Instr::Add { label: self.next_label() }); }, "sub" | "-" => { let mut lhs = self.compile_atom(&args[0], depth + 1)?; - let lhs_pointer = self.current_register(); result.append(&mut lhs); let mut rhs = self.compile_atom(&args[1], depth + 1)?; - let rhs_pointer = self.current_register(); result.append(&mut rhs); - let result_register = self.next_register(); - result.push(Instr::ISub { - lhs: lhs_pointer, - rhs: rhs_pointer, - to: result_register, - label: self.next_label(), - }); + result.push(Instr::Sub { label: self.next_label() }); }, "mul" | "*" => { let mut lhs = self.compile_atom(&args[0], depth + 1)?; - let lhs_pointer = self.current_register(); result.append(&mut lhs); let mut rhs = self.compile_atom(&args[1], depth + 1)?; - let rhs_pointer = self.current_register(); result.append(&mut rhs); - let result_register = self.next_register(); - result.push(Instr::IMul { - lhs: lhs_pointer, - rhs: rhs_pointer, - to: result_register, - label: self.next_label(), - }); + result.push(Instr::Mul { label: self.next_label() }); }, "div" | "/" => { let mut lhs = self.compile_atom(&args[0], depth + 1)?; - let lhs_pointer = self.current_register(); result.append(&mut lhs); let mut rhs = self.compile_atom(&args[1], depth + 1)?; - let rhs_pointer = self.current_register(); result.append(&mut rhs); - let result_register = self.next_register(); - result.push(Instr::IDiv { - lhs: lhs_pointer, - rhs: rhs_pointer, - to: result_register, - label: self.next_label(), - }); + result.push(Instr::Div { label: self.next_label() }); }, _ => return Err(format!("Unknown intrinsic: {}", intrinsic)), } diff --git a/blspc/src/vm/instr.rs b/blspc/src/vm/instr.rs index 162244b..625df5f 100644 --- a/blspc/src/vm/instr.rs +++ b/blspc/src/vm/instr.rs @@ -76,6 +76,11 @@ pub enum Instr { Store { address: Register, value: Type, label: usize }, // Call intrinsic function. Call { address: Register, args: Register, label: usize }, + // Stack operations. + Push { value: Type, label: usize }, Pop { address: Register, label: usize }, + // 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 }, @@ -94,6 +99,12 @@ impl Display for Instr { // Instr::Load { address, label } => write!(f, "{}: LOAD {}", label, address), Instr::Store { address, value , label} => write!(f, "{}: STORE {} {}", label, address, value), Instr::Call { address, args, label } => write!(f, "{}: CALL {} {}", label, address, args), + Instr::Push { value, label } => write!(f, "{}: PUSH {}", label, value), + Instr::Pop { address, label } => write!(f, "{}: POP {}", label, address), + Instr::Add { label } => write!(f, "{}: ADD", label), + 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),