From 560ac69863bc60381181f3bf38868eb3eca56537 Mon Sep 17 00:00:00 2001 From: Natapat Samutpong Date: Mon, 31 Jan 2022 16:06:53 +0700 Subject: [PATCH] feat: `call` with stack instead of registers --- blspc/src/compiler/compile.rs | 19 ++++--------------- blspc/src/vm/instr.rs | 4 ++-- blspc/src/vm/parser.rs | 5 +---- blspc/src/vm/vm.rs | 8 ++++---- 4 files changed, 11 insertions(+), 25 deletions(-) diff --git a/blspc/src/compiler/compile.rs b/blspc/src/compiler/compile.rs index 14b2e34..c5a8ddb 100644 --- a/blspc/src/compiler/compile.rs +++ b/blspc/src/compiler/compile.rs @@ -38,7 +38,7 @@ impl Compiler { } }, "fun" => { - result.push(Instr::Comment { text: format!("--- {}", comp) }); + result.push(Instr::Comment { text: format!("function {}", comp) }); let function_name = match &cdr[0] { Symbol(ref name) => format!("function_{}", name.clone()), _ => return Err(format!("Expected function name, got {}", cdr[0])), @@ -75,15 +75,8 @@ impl Compiler { let to = self.next_register(); let call_register = self.next_register(); - result.push(Instr::Pop { address: to }); - result.push(Instr::Store { - address: call_register, - value: Type::Int(1), - }); - result.push(Instr::Call { - address: call_register, - args: to, - }); + result.push(Instr::Push { value: Type::Int(1) }); + result.push(Instr::Call); }, "add" | "+" => { let mut lhs = self.compile_atom(&args[0])?; @@ -135,23 +128,19 @@ impl Compiler { match atom { Int(i) => { - result.push(Instr::Comment { text: format!("----- {}", comp) }); result.push(Instr::Push { value: Type::Int(*i) }); }, Float(f) => { - result.push(Instr::Comment { text: format!("----- {}", comp) }); result.push(Instr::Push { value: Type::Float(*f) }); }, Str(s) => { - result.push(Instr::Comment { text: format!("----- {}", comp) }); result.push(Instr::Push { value: Type::String(s.to_string()) }); }, Boolean(b) => { - result.push(Instr::Comment { text: format!("----- {}", comp) }); result.push(Instr::Push { value: Type::Boolean(*b) }); }, Symbol(s) => { - result.push(Instr::Comment { text: format!("----- {} variable", comp) }); + result.push(Instr::Comment { text: format!("{} variable", comp) }); result.push(Instr::Jump { to: format!("function_{}", s), }); }, _ => { result.append(&mut self.compile(atom.clone())?); } diff --git a/blspc/src/vm/instr.rs b/blspc/src/vm/instr.rs index f13cae2..4342cb4 100644 --- a/blspc/src/vm/instr.rs +++ b/blspc/src/vm/instr.rs @@ -183,7 +183,7 @@ pub enum Instr { // Store a literal value into a register. Store { address: Register, value: Type }, // Call intrinsic function. - Call { address: Register, args: Register }, + Call, // Stack operations. Push { value: Type }, Pop { address: Register }, Swap, // Stack arithmetic. @@ -204,7 +204,7 @@ impl Display for Instr { Instr::Label { name } => write!(f, ".{}:", name), Instr::Comment { text } => write!(f, ";{}", text), Instr::Store { address, value } => write!(f, " STORE {} {}", address, value), - Instr::Call { address, args } => write!(f, " CALL {} {}", address, args), + Instr::Call => write!(f, " CALL"), Instr::Push { value } => write!(f, " PUSH {}", value), Instr::Pop { address } => write!(f, " POP {}", address), Instr::Swap => write!(f, " SWAP"), diff --git a/blspc/src/vm/parser.rs b/blspc/src/vm/parser.rs index ed1e6c7..d2bcb96 100644 --- a/blspc/src/vm/parser.rs +++ b/blspc/src/vm/parser.rs @@ -20,10 +20,7 @@ pub fn parse_instr(src: &str) -> Vec { address: register!(tokens[1]), value: value!(tokens[2]), }); }, - "CALL" => { result.push(Instr::Call { - address: register!(tokens[1]), - args: register!(tokens[2]), - }); }, + "CALL" => { result.push(Instr::Call); }, "PUSH" => { result.push(Instr::Push { value: value!(tokens[1]) }); }, "POP" => { result.push(Instr::Pop { address: register!(tokens[1]) }); }, "SWAP" => { result.push(Instr::Swap); }, diff --git a/blspc/src/vm/vm.rs b/blspc/src/vm/vm.rs index dd420c1..f3af8ac 100644 --- a/blspc/src/vm/vm.rs +++ b/blspc/src/vm/vm.rs @@ -73,10 +73,10 @@ impl VM { self.store(&address, &value)?; continue 'tco; }, - Call { address, args, .. } => { - let args = &self.registers[args.value()]; - let address = &self.registers[address.value()]; - call(address, args, self.instr_pointer)?; + Call => { + let index = &self.stack.pop().unwrap(); + let args = &self.stack.pop().unwrap(); + call(index, args, self.instr_pointer)?; continue 'tco; }, Push { value } => {