From e503e312a187d816b40e306b90343f497ae58aec Mon Sep 17 00:00:00 2001 From: Mark Thom Date: Sat, 11 Nov 2017 16:01:29 -0700 Subject: [PATCH] reuse defunct AND stack frames. --- src/prolog/and_stack.rs | 14 +++++++++++--- src/prolog/machine.rs | 33 ++++++++++++++++++++++++++++----- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/src/prolog/and_stack.rs b/src/prolog/and_stack.rs index 77a729f9..f4d043da 100644 --- a/src/prolog/and_stack.rs +++ b/src/prolog/and_stack.rs @@ -49,9 +49,17 @@ impl AndStack { self.0.clear() } - pub fn pop(&mut self) { - self.0.pop(); - } + pub fn resize(&mut self, fr: usize, n: usize) { + let len = self[fr].perms.len(); + + if len < n { + self[fr].perms.reserve(n - len); + + for i in len .. n { + self[fr].perms.push(Addr::StackCell(fr, i)); + } + } + } } impl Index for AndStack { diff --git a/src/prolog/machine.rs b/src/prolog/machine.rs index 0d924618..f22f3ba8 100644 --- a/src/prolog/machine.rs +++ b/src/prolog/machine.rs @@ -1510,11 +1510,34 @@ impl MachineState { { match instr { &ControlInstruction::Allocate(num_cells) => { - let num_frames = self.num_frames(); + if let Some(ref or_fr) = self.or_stack.top() { + let and_gi = if self.and_stack.len() > self.e { + self.and_stack[self.e].global_index + } else { + 0 + }; + + if and_gi <= or_fr.global_index { + self.e = or_fr.e; + } + } + + if self.e + 1 < self.and_stack.len() { + let index = self.e + 1; + + self.and_stack[index].e = self.e; + self.and_stack[index].cp = self.cp; + + self.and_stack.resize(index, num_cells); + + self.e = index; + } else { + let num_frames = self.num_frames(); + + self.and_stack.push(num_frames + 1, self.e, self.cp, num_cells); + self.e = self.and_stack.len() - 1; + }; - self.and_stack.push(num_frames + 1, self.e, self.cp, num_cells); - self.e = self.and_stack.len() - 1; - self.p += 1; }, &ControlInstruction::Call(ref name, arity, _) => @@ -1539,7 +1562,7 @@ impl MachineState { self.cp = self.and_stack[e].cp; self.e = self.and_stack[e].e; - + self.p += 1; }, &ControlInstruction::Execute(ref name, arity) => -- 2.54.0