From 700f031cd919392678993bdb0ffcf5047f7c38e2 Mon Sep 17 00:00:00 2001 From: Mark Thom Date: Thu, 25 Jan 2018 09:10:30 -0700 Subject: [PATCH] remove indirection in jmp instructions --- src/prolog/ast.rs | 8 ++++---- src/prolog/codegen.rs | 4 ++-- src/prolog/io.rs | 20 +++++++++++--------- src/prolog/iterators.rs | 4 ++-- src/prolog/machine/machine_state.rs | 8 ++++---- src/prolog/parser | 2 +- 6 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/prolog/ast.rs b/src/prolog/ast.rs index 7f5087d5..ebbc5444 100644 --- a/src/prolog/ast.rs +++ b/src/prolog/ast.rs @@ -367,7 +367,7 @@ pub enum CompareNumberQT { // vars of predicate, toplevel offset. Vec is always a vector // of vars (we get their adjoining cells this way). -pub type JumpStub = (Vec, Rc>); +pub type JumpStub = Vec; pub enum QueryTerm { Arg(Vec>), @@ -395,7 +395,7 @@ impl QueryTerm { &QueryTerm::Functor(_) => 3, &QueryTerm::Inlined(ref term) => term.arity(), &QueryTerm::Is(_) => 2, - &QueryTerm::Jump((ref vars, _)) => vars.len(), + &QueryTerm::Jump(ref vars) => vars.len(), &QueryTerm::CallN(ref terms) => terms.len(), &QueryTerm::Cut => 0, &QueryTerm::Term(ref term) => term.arity(), @@ -818,8 +818,8 @@ pub enum ControlInstruction { ExecuteN(usize), FunctorCall, FunctorExecute, - JmpByCall(usize, Rc>), // arity, global_offset. - JmpByExecute(usize, Rc>), + JmpByCall(usize, usize), // arity, global_offset. + JmpByExecute(usize, usize), // GotoCall(usize, usize), // p, arity. GotoExecute(usize, usize), // p, arity. IsCall(RegType, ArithmeticTerm), diff --git a/src/prolog/codegen.rs b/src/prolog/codegen.rs index 5f3b2482..12f88a3a 100644 --- a/src/prolog/codegen.rs +++ b/src/prolog/codegen.rs @@ -254,8 +254,8 @@ impl<'a, TermMarker: Allocator<'a>> CodeGenerator<'a, TermMarker> code.push(Line::Control(ControlInstruction::FunctorCall)), &QueryTerm::Inlined(_) => code.push(proceed!()), - &QueryTerm::Jump((ref vars, ref offset)) => { - code.push(jmp_call!(vars.len(), offset.clone())); + &QueryTerm::Jump(ref vars) => { + code.push(jmp_call!(vars.len(), 0)); }, &QueryTerm::Term(Term::Constant(_, Constant::Atom(ref atom))) => { let call = ControlInstruction::Call(atom.clone(), 0, pvs); diff --git a/src/prolog/io.rs b/src/prolog/io.rs index 248b2863..4576639e 100644 --- a/src/prolog/io.rs +++ b/src/prolog/io.rs @@ -137,10 +137,10 @@ impl fmt::Display for ControlInstruction { write!(f, "is_call {}, {}", r, at), &ControlInstruction::IsExecute(r, ref at) => write!(f, "is_execute {}, {}", r, at), - &ControlInstruction::JmpByCall(arity, ref offset) => - write!(f, "jmp_by_call {}/{}", offset.get(), arity), - &ControlInstruction::JmpByExecute(arity, ref offset) => - write!(f, "jmp_by_execute {}/{}", offset.get(), arity), + &ControlInstruction::JmpByCall(arity, offset) => + write!(f, "jmp_by_call {}/{}", offset, arity), + &ControlInstruction::JmpByExecute(arity, offset) => + write!(f, "jmp_by_execute {}/{}", offset, arity), &ControlInstruction::Proceed => write!(f, "proceed"), &ControlInstruction::ThrowCall => @@ -412,13 +412,15 @@ fn compile_relation(tl: &TopLevel) -> Result // set first jmp_by_call or jmp_by_index instruction to code.len() - idx, // where idx is the place it occurs. It only does this to the *first* uninitialized // jmp index it encounters, then returns. -fn set_first_index(code: &Code) +fn set_first_index(code: &mut Code) { - for (idx, line) in code.iter().enumerate() { + let code_len = code.len(); + + for (idx, line) in code.iter_mut().enumerate() { match line { - &Line::Control(ControlInstruction::JmpByExecute(_, ref offset)) - | &Line::Control(ControlInstruction::JmpByCall(_, ref offset)) if offset.get() == 0 => { - offset.set(code.len() - idx); + &mut Line::Control(ControlInstruction::JmpByExecute(_, ref mut offset)) + | &mut Line::Control(ControlInstruction::JmpByCall(_, ref mut offset)) if *offset == 0 => { + *offset = code_len - idx; break; }, _ => {} diff --git a/src/prolog/iterators.rs b/src/prolog/iterators.rs index 06f4301a..3f58f3b8 100644 --- a/src/prolog/iterators.rs +++ b/src/prolog/iterators.rs @@ -72,7 +72,7 @@ impl<'a> QueryIterator<'a> { QueryIterator { state_stack: vec![state] } }, &QueryTerm::Cut => QueryIterator { state_stack: vec![] }, - &QueryTerm::Jump((ref vars, _)) => { + &QueryTerm::Jump(ref vars) => { let state_stack = vars.iter().rev().map(|t| { TermIterState::to_state(Level::Shallow, t) }).collect(); @@ -273,7 +273,7 @@ impl<'a> ChunkedIterator<'a> while let Some(term) = item { match term { - &QueryTerm::Jump((ref vars, _)) => { + &QueryTerm::Jump(ref vars) => { result.push(term); arity = vars.len(); break; diff --git a/src/prolog/machine/machine_state.rs b/src/prolog/machine/machine_state.rs index 55b63f9e..ed4547ef 100644 --- a/src/prolog/machine/machine_state.rs +++ b/src/prolog/machine/machine_state.rs @@ -1692,16 +1692,16 @@ impl MachineState { self.unify(a1, Addr::Con(Constant::Number(a2))); self.p = self.cp; }, - &ControlInstruction::JmpByCall(arity, ref offset) => { + &ControlInstruction::JmpByCall(arity, offset) => { self.cp = self.p + 1; self.num_of_args = arity; self.b0 = self.b; - self.p += offset.get(); + self.p += offset; }, - &ControlInstruction::JmpByExecute(arity, ref offset) => { + &ControlInstruction::JmpByExecute(arity, offset) => { self.num_of_args = arity; self.b0 = self.b; - self.p += offset.get(); + self.p += offset; }, &ControlInstruction::Proceed => self.p = self.cp, diff --git a/src/prolog/parser b/src/prolog/parser index 9e80f2bd..e15eb2d2 160000 --- a/src/prolog/parser +++ b/src/prolog/parser @@ -1 +1 @@ -Subproject commit 9e80f2bd8268425c2128772b336f616be8d2c5d8 +Subproject commit e15eb2d27bb866d7a94fe47a829f5c51893bede3 -- 2.54.0