From 6f9b6a29c487fa1095a418f2501c5e1733f49c02 Mon Sep 17 00:00:00 2001 From: Mark Thom Date: Wed, 15 Dec 2021 23:41:00 -0700 Subject: [PATCH] remove LocalCodePtr::IndexingBuf --- src/machine/code_repo.rs | 117 +++++++++++------------ src/machine/machine_indices.rs | 56 +++++------ src/machine/machine_state.rs | 149 ++++++++++++++---------------- src/machine/machine_state_impl.rs | 100 ++++++++++++-------- src/machine/mod.rs | 17 ++-- src/machine/stack.rs | 2 + src/machine/system_calls.rs | 6 +- src/write.rs | 6 +- 8 files changed, 235 insertions(+), 218 deletions(-) diff --git a/src/machine/code_repo.rs b/src/machine/code_repo.rs index 54db570f..ba046228 100644 --- a/src/machine/code_repo.rs +++ b/src/machine/code_repo.rs @@ -1,53 +1,19 @@ use crate::clause_types::*; use crate::instructions::*; +use crate::machine::MachineState; use crate::machine::machine_indices::*; +// TODO: remove this, replace with just 'Code'. #[derive(Debug)] pub struct CodeRepo { pub(super) code: Code, } impl CodeRepo { - #[inline] - pub(super) fn new() -> Self { - CodeRepo { code: Code::new() } - } - - #[inline] - pub(super) fn lookup_local_instr<'a>(&'a self, p: LocalCodePtr) -> RefOrOwned<'a, Line> { - match p { - LocalCodePtr::Halt => { - // exit with the interrupt exit code. - std::process::exit(1); - } - LocalCodePtr::DirEntry(p) => RefOrOwned::Borrowed(&self.code[p as usize]), - LocalCodePtr::IndexingBuf(p, o, i) => match &self.code[p] { - &Line::IndexingCode(ref indexing_lines) => match &indexing_lines[o] { - &IndexingLine::IndexedChoice(ref indexed_choice_instrs) => { - RefOrOwned::Owned(Line::IndexedChoice(indexed_choice_instrs[i])) - } - &IndexingLine::DynamicIndexedChoice(ref indexed_choice_instrs) => { - RefOrOwned::Owned(Line::DynamicIndexedChoice(indexed_choice_instrs[i])) - } - _ => { - unreachable!() - } - }, - _ => { - unreachable!() - } - }, - } - } - - pub(super) fn lookup_instr<'a>( - &'a self, - last_call: bool, - p: &CodePtr, - ) -> Option> { + pub(super) fn lookup_instr<'a>(&'a self, machine_st: &MachineState, p: &CodePtr) -> Option> { match p { &CodePtr::Local(local) => { - return Some(self.lookup_local_instr(local)); + return Some(self.lookup_local_instr(machine_st, local)); } &CodePtr::REPL(..) => None, &CodePtr::BuiltInClause(ref built_in, _) => { @@ -55,33 +21,56 @@ impl CodeRepo { ClauseType::BuiltIn(built_in.clone()), built_in.arity(), 0, - last_call + machine_st.last_call ); Some(RefOrOwned::Owned(call_clause)) } &CodePtr::CallN(arity, _, last_call) => { let call_clause = call_clause!(ClauseType::CallN, arity, 0, last_call); - Some(RefOrOwned::Owned(call_clause)) } &CodePtr::VerifyAttrInterrupt(p) => Some(RefOrOwned::Borrowed(&self.code[p])), } } - pub(super) fn find_living_dynamic_else( - &self, - mut p: usize, - cc: usize, - ) -> Option<(usize, usize)> { + #[inline] + pub(super) fn lookup_local_instr<'a>(&'a self, machine_st: &MachineState, p: LocalCodePtr) -> RefOrOwned<'a, Line> { + match p { + LocalCodePtr::Halt => { + // exit with the interrupt exit code. + std::process::exit(1); + } + LocalCodePtr::DirEntry(p) => match &self.code[p] { + &Line::IndexingCode(ref indexing_lines) => { + match &indexing_lines[machine_st.oip as usize] { + &IndexingLine::IndexedChoice(ref indexed_choice_instrs) => { + RefOrOwned::Owned(Line::IndexedChoice(indexed_choice_instrs[machine_st.iip as usize])) + } + &IndexingLine::DynamicIndexedChoice(ref indexed_choice_instrs) => { + RefOrOwned::Owned(Line::DynamicIndexedChoice(indexed_choice_instrs[machine_st.iip as usize])) + } + _ => { + RefOrOwned::Borrowed(&self.code[p as usize]) + } + } + } + _ => RefOrOwned::Borrowed(&self.code[p as usize]), + } + } + } +} + +impl MachineState { + pub(super) fn find_living_dynamic_else(&self, code: &Code, mut p: usize) -> Option<(usize, usize)> { loop { - match &self.code[p] { + match &code[p] { &Line::Choice(ChoiceInstruction::DynamicElse( birth, death, NextOrFail::Next(i), )) => { - if birth < cc && Death::Finite(cc) <= death { + if birth < self.cc && Death::Finite(self.cc) <= death { return Some((p, i)); } else if i > 0 { p += i; @@ -94,7 +83,7 @@ impl CodeRepo { death, NextOrFail::Fail(_), )) => { - if birth < cc && Death::Finite(cc) <= death { + if birth < self.cc && Death::Finite(self.cc) <= death { return Some((p, 0)); } else { return None; @@ -105,7 +94,7 @@ impl CodeRepo { death, NextOrFail::Next(i), )) => { - if birth < cc && Death::Finite(cc) <= death { + if birth < self.cc && Death::Finite(self.cc) <= death { return Some((p, i)); } else if i > 0 { p += i; @@ -118,7 +107,7 @@ impl CodeRepo { death, NextOrFail::Fail(_), )) => { - if birth < cc && Death::Finite(cc) <= death { + if birth < self.cc && Death::Finite(self.cc) <= death { return Some((p, 0)); } else { return None; @@ -134,18 +123,11 @@ impl CodeRepo { } } - pub(super) fn find_living_dynamic( - &self, - p: LocalCodePtr, - cc: usize, - ) -> Option<(usize, usize, usize, bool)> { - let (p, oi, mut ii) = match p { - LocalCodePtr::IndexingBuf(p, oi, ii) => (p, oi, ii), - _ => unreachable!(), - }; + pub(super) fn find_living_dynamic(&self, code: &Code, oi: u32, mut ii: u32) -> Option<(usize, u32, u32, bool)> { + let p = self.p.local().abs_loc(); - let indexed_choice_instrs = match &self.code[p] { - Line::IndexingCode(ref indexing_code) => match &indexing_code[oi] { + let indexed_choice_instrs = match &code[p] { + Line::IndexingCode(ref indexing_code) => match &indexing_code[oi as usize] { IndexingLine::DynamicIndexedChoice(ref indexed_choice_instrs) => { indexed_choice_instrs } @@ -155,14 +137,14 @@ impl CodeRepo { }; loop { - match &indexed_choice_instrs.get(ii) { - Some(&offset) => match &self.code[p + offset - 1] { + match &indexed_choice_instrs.get(ii as usize) { + Some(&offset) => match &code[p + offset - 1] { &Line::Choice(ChoiceInstruction::DynamicInternalElse( birth, death, next_or_fail, )) => { - if birth < cc && Death::Finite(cc) <= death { + if birth < self.cc && Death::Finite(self.cc) <= death { return Some((offset, oi, ii, next_or_fail.is_next())); } else { ii += 1; @@ -175,3 +157,10 @@ impl CodeRepo { } } } + +impl CodeRepo { + #[inline] + pub(super) fn new() -> Self { + CodeRepo { code: Code::new() } + } +} diff --git a/src/machine/machine_indices.rs b/src/machine/machine_indices.rs index 361b084e..e7cda22d 100644 --- a/src/machine/machine_indices.rs +++ b/src/machine/machine_indices.rs @@ -322,8 +322,26 @@ impl CodePtr { pub enum LocalCodePtr { DirEntry(usize), // offset Halt, - IndexingBuf(usize, usize, usize), // DirEntry offset, first internal offset, second internal offset - // TopLevel(usize, usize), // chunk_num, offset + // IndexingBuf(usize, usize, usize), // DirEntry offset, first internal offset, second internal offset + // TopLevel(usize, usize), // chunk_num, offset +} + +impl MachineState { + pub(crate) fn is_reset_cont_marker(&self, code_repo: &CodeRepo, p: LocalCodePtr) -> bool { + match code_repo.lookup_instr(self, &CodePtr::Local(p)) { + Some(line) => match line.as_ref() { + Line::Control(ControlInstruction::CallClause(ref ct, ..)) => { + if let ClauseType::System(SystemClauseType::ResetContinuationMarker) = *ct { + return true; + } + } + _ => {} + }, + None => {} + } + + false + } } impl LocalCodePtr { @@ -338,27 +356,11 @@ impl LocalCodePtr { pub fn abs_loc(&self) -> usize { match self { LocalCodePtr::DirEntry(ref p) => *p, - LocalCodePtr::IndexingBuf(ref p, ..) => *p, + // LocalCodePtr::IndexingBuf(ref p, ..) => *p, LocalCodePtr::Halt => unreachable!(), } } - pub(crate) fn is_reset_cont_marker(&self, code_repo: &CodeRepo, last_call: bool) -> bool { - match code_repo.lookup_instr(last_call, &CodePtr::Local(*self)) { - Some(line) => match line.as_ref() { - Line::Control(ControlInstruction::CallClause(ref ct, ..)) => { - if let ClauseType::System(SystemClauseType::ResetContinuationMarker) = *ct { - return true; - } - } - _ => {} - }, - None => {} - } - - false - } - pub(crate) fn as_functor(&self) -> MachineStub { match self { LocalCodePtr::DirEntry(p) => { @@ -367,12 +369,14 @@ impl LocalCodePtr { LocalCodePtr::Halt => { functor!(atom!("halt")) } + /* LocalCodePtr::IndexingBuf(p, o, i) => { functor!( atom!("indexed_buf"), [fixnum(*p), fixnum(*o), fixnum(*i)] ) } + */ } } } @@ -399,7 +403,7 @@ impl Add for LocalCodePtr { match self { LocalCodePtr::DirEntry(p) => LocalCodePtr::DirEntry(p + rhs), LocalCodePtr::Halt => unreachable!(), - LocalCodePtr::IndexingBuf(p, o, i) => LocalCodePtr::IndexingBuf(p, o, i + rhs), + // LocalCodePtr::IndexingBuf(p, o, i) => LocalCodePtr::IndexingBuf(p, o, i + rhs), } } } @@ -412,9 +416,9 @@ impl Sub for LocalCodePtr { match self { LocalCodePtr::DirEntry(p) => p.checked_sub(rhs).map(LocalCodePtr::DirEntry), LocalCodePtr::Halt => unreachable!(), - LocalCodePtr::IndexingBuf(p, o, i) => i - .checked_sub(rhs) - .map(|r| LocalCodePtr::IndexingBuf(p, o, r)), + // LocalCodePtr::IndexingBuf(p, o, i) => i + // .checked_sub(rhs) + // .map(|r| LocalCodePtr::IndexingBuf(p, o, r)), } } } @@ -424,7 +428,7 @@ impl SubAssign for LocalCodePtr { fn sub_assign(&mut self, rhs: usize) { match self { LocalCodePtr::DirEntry(ref mut p) => *p -= rhs, - LocalCodePtr::Halt | LocalCodePtr::IndexingBuf(..) => unreachable!(), + LocalCodePtr::Halt => unreachable!() // | LocalCodePtr::IndexingBuf(..) => unreachable!(), } } } @@ -433,8 +437,8 @@ impl AddAssign for LocalCodePtr { #[inline] fn add_assign(&mut self, rhs: usize) { match self { - &mut LocalCodePtr::DirEntry(ref mut i) - | &mut LocalCodePtr::IndexingBuf(_, _, ref mut i) => *i += rhs, + &mut LocalCodePtr::DirEntry(ref mut i) => *i += rhs, + // | &mut LocalCodePtr::IndexingBuf(_, _, ref mut i) => *i += rhs, &mut LocalCodePtr::Halt => unreachable!(), } } diff --git a/src/machine/machine_state.rs b/src/machine/machine_state.rs index 58336338..68330be7 100644 --- a/src/machine/machine_state.rs +++ b/src/machine/machine_state.rs @@ -63,6 +63,8 @@ pub struct MachineState { pub(super) pdl: Vec, pub(super) s: HeapPtr, pub(super) p: CodePtr, + pub(super) oip: u32, // first internal code ptr + pub(super) iip : u32, // second internal code ptr pub(super) b: usize, pub(super) b0: usize, pub(super) e: usize, @@ -81,7 +83,7 @@ pub struct MachineState { pub(super) ball: Ball, pub(super) lifted_heap: Heap, pub(super) interms: Vec, // intermediate numbers. - pub(super) last_call: bool, + pub(super) last_call: bool, // TODO: REMOVE THIS. pub(crate) flags: MachineFlags, pub(crate) cc: usize, pub(crate) global_clock: usize, @@ -191,38 +193,34 @@ pub trait CallPolicy: Any + fmt::Debug { global_variables: &mut GlobalVarDir, ) -> CallResult { let b = machine_st.b; - let n = machine_st - .stack - .index_or_frame(b) - .prelude - .univ_prelude - .num_cells; - - for i in 1..n + 1 { - machine_st.registers[i] = machine_st.stack[stack_loc!(OrFrame, b, i - 1)]; + let or_frame = machine_st.stack.index_or_frame_mut(b); + let n = or_frame.prelude.univ_prelude.num_cells; + + for i in 0..n { + machine_st.registers[i + 1] = or_frame[i]; } machine_st.num_of_args = n; - machine_st.e = machine_st.stack.index_or_frame(b).prelude.e; - machine_st.cp = machine_st.stack.index_or_frame(b).prelude.cp; + machine_st.e = or_frame.prelude.e; + machine_st.cp = or_frame.prelude.cp; - machine_st.stack.index_or_frame_mut(b).prelude.bp = machine_st.p.local() + offset; + or_frame.prelude.bp = machine_st.p.local() + offset; - let old_tr = machine_st.stack.index_or_frame(b).prelude.tr; + let old_tr = or_frame.prelude.tr; let curr_tr = machine_st.tr; + let target_h = or_frame.prelude.h; - machine_st.unwind_trail(old_tr, curr_tr, global_variables); - machine_st.tr = machine_st.stack.index_or_frame(b).prelude.tr; - - machine_st.trail.truncate(machine_st.tr); - machine_st - .heap - .truncate(machine_st.stack.index_or_frame(b).prelude.h); + machine_st.tr = or_frame.prelude.tr; machine_st.attr_var_init.reset(); machine_st.hb = machine_st.heap.len(); machine_st.p += 1; + machine_st.unwind_trail(old_tr, curr_tr, global_variables); + + machine_st.trail.truncate(machine_st.tr); + machine_st.heap.truncate(target_h); + Ok(()) } @@ -233,38 +231,38 @@ pub trait CallPolicy: Any + fmt::Debug { global_variables: &mut GlobalVarDir, ) -> CallResult { let b = machine_st.b; - let n = machine_st - .stack - .index_or_frame(b) - .prelude - .univ_prelude - .num_cells; - - for i in 1..n + 1 { - machine_st.registers[i] = machine_st.stack.index_or_frame(b)[i - 1]; + let or_frame = machine_st.stack.index_or_frame_mut(b); + let n = or_frame.prelude.univ_prelude.num_cells; + + for i in 0..n { + machine_st.registers[i+1] = or_frame[i]; } machine_st.num_of_args = n; - machine_st.e = machine_st.stack.index_or_frame(b).prelude.e; - machine_st.cp = machine_st.stack.index_or_frame(b).prelude.cp; + machine_st.e = or_frame.prelude.e; + machine_st.cp = or_frame.prelude.cp; - machine_st.stack.index_or_frame_mut(b).prelude.bp = machine_st.p.local() + 1; + // WAS: or_frame.prelude.bp = machine_st.p.local() + 1; + or_frame.prelude.biip += 1; - let old_tr = machine_st.stack.index_or_frame(b).prelude.tr; + let old_tr = or_frame.prelude.tr; let curr_tr = machine_st.tr; + let target_h = or_frame.prelude.h; + + machine_st.tr = or_frame.prelude.tr; + machine_st.attr_var_init.reset(); machine_st.unwind_trail(old_tr, curr_tr, global_variables); - machine_st.tr = machine_st.stack.index_or_frame(b).prelude.tr; machine_st.trail.truncate(machine_st.tr); - machine_st - .heap - .truncate(machine_st.stack.index_or_frame(b).prelude.h); + machine_st.heap.truncate(target_h); - machine_st.attr_var_init.reset(); machine_st.hb = machine_st.heap.len(); machine_st.p = CodePtr::Local(dir_entry!(machine_st.p.local().abs_loc() + offset)); + machine_st.oip = 0; + machine_st.iip = 0; + Ok(()) } @@ -275,39 +273,38 @@ pub trait CallPolicy: Any + fmt::Debug { global_variables: &mut GlobalVarDir, ) -> CallResult { let b = machine_st.b; - let n = machine_st - .stack - .index_or_frame(b) - .prelude - .univ_prelude - .num_cells; - - for i in 1..n + 1 { - machine_st.registers[i] = machine_st.stack[stack_loc!(OrFrame, b, i - 1)]; + let or_frame = machine_st.stack.index_or_frame(b); + let n = or_frame.prelude.univ_prelude.num_cells; + + for i in 0..n { + machine_st.registers[i+1] = or_frame[i]; } machine_st.num_of_args = n; - machine_st.e = machine_st.stack.index_or_frame(b).prelude.e; - machine_st.cp = machine_st.stack.index_or_frame(b).prelude.cp; + machine_st.e = or_frame.prelude.e; + machine_st.cp = or_frame.prelude.cp; - let old_tr = machine_st.stack.index_or_frame(b).prelude.tr; + let old_tr = or_frame.prelude.tr; let curr_tr = machine_st.tr; + let target_h = or_frame.prelude.h; + + machine_st.tr = or_frame.prelude.tr; + + machine_st.attr_var_init.reset(); + machine_st.b = or_frame.prelude.b; machine_st.unwind_trail(old_tr, curr_tr, global_variables); - machine_st.tr = machine_st.stack.index_or_frame(b).prelude.tr; machine_st.trail.truncate(machine_st.tr); - machine_st - .heap - .truncate(machine_st.stack.index_or_frame(b).prelude.h); - - machine_st.attr_var_init.reset(); - machine_st.b = machine_st.stack.index_or_frame(b).prelude.b; machine_st.stack.truncate(b); + machine_st.heap.truncate(target_h); machine_st.hb = machine_st.heap.len(); machine_st.p = CodePtr::Local(dir_entry!(machine_st.p.local().abs_loc() + offset)); + machine_st.oip = 0; + machine_st.iip = 0; + Ok(()) } @@ -317,35 +314,31 @@ pub trait CallPolicy: Any + fmt::Debug { global_variables: &mut GlobalVarDir, ) -> CallResult { let b = machine_st.b; - let n = machine_st - .stack - .index_or_frame(b) - .prelude - .univ_prelude - .num_cells; - - for i in 1..n + 1 { - machine_st.registers[i] = machine_st.stack[stack_loc!(OrFrame, b, i - 1)]; + let or_frame = machine_st.stack.index_or_frame(b); + let n = or_frame.prelude.univ_prelude.num_cells; + + for i in 0..n { + machine_st.registers[i+1] = or_frame[i]; } machine_st.num_of_args = n; - machine_st.e = machine_st.stack.index_or_frame(b).prelude.e; - machine_st.cp = machine_st.stack.index_or_frame(b).prelude.cp; + machine_st.e = or_frame.prelude.e; + machine_st.cp = or_frame.prelude.cp; - let old_tr = machine_st.stack.index_or_frame(b).prelude.tr; + let old_tr = or_frame.prelude.tr; let curr_tr = machine_st.tr; + let target_h = or_frame.prelude.h; + + machine_st.tr = or_frame.prelude.tr; + + machine_st.attr_var_init.reset(); + machine_st.b = or_frame.prelude.b; machine_st.unwind_trail(old_tr, curr_tr, global_variables); - machine_st.tr = machine_st.stack.index_or_frame(b).prelude.tr; machine_st.trail.truncate(machine_st.tr); - machine_st - .heap - .truncate(machine_st.stack.index_or_frame(b).prelude.h); - - machine_st.attr_var_init.reset(); - machine_st.b = machine_st.stack.index_or_frame(b).prelude.b; machine_st.stack.truncate(b); + machine_st.heap.truncate(target_h); machine_st.hb = machine_st.heap.len(); machine_st.p += 1; @@ -754,7 +747,6 @@ impl<'a> IndexMut for CopyTerm<'a> { } } -// the ordinary, heap term copier, used by duplicate_term. impl<'a> CopierTarget for CopyTerm<'a> { #[inline(always)] fn threshold(&self) -> usize { @@ -827,7 +819,6 @@ impl<'a> IndexMut for CopyBallTerm<'a> { } } -// the ordinary, heap term copier, used by duplicate_term. impl<'a> CopierTarget for CopyBallTerm<'a> { fn threshold(&self) -> usize { self.heap_boundary + self.stub.len() diff --git a/src/machine/machine_state_impl.rs b/src/machine/machine_state_impl.rs index 4f42756e..1c6922d5 100644 --- a/src/machine/machine_state_impl.rs +++ b/src/machine/machine_state_impl.rs @@ -37,6 +37,8 @@ impl MachineState { pdl: Vec::with_capacity(1024), s: HeapPtr::default(), p: CodePtr::default(), + oip: 0, + iip: 0, b: 0, b0: 0, e: 0, @@ -1905,8 +1907,8 @@ impl MachineState { None => unreachable!(), } } - TrailEntryTag::TrailedAttachedValue => { - } + TrailEntryTag::TrailedAttachedValue => { + } } } } @@ -2758,7 +2760,9 @@ impl MachineState { } &IndexingLine::IndexedChoice(_) => { if let LocalCodePtr::DirEntry(p) = self.p.local() { - self.p = CodePtr::Local(LocalCodePtr::IndexingBuf(p, index, 0)); + self.p = CodePtr::Local(LocalCodePtr::DirEntry(p)); + self.oip = index as u32; + self.iip = 0; } else { unreachable!() } @@ -2769,7 +2773,9 @@ impl MachineState { self.dynamic_mode = FirstOrNext::First; if let LocalCodePtr::DirEntry(p) = self.p.local() { - self.p = CodePtr::Local(LocalCodePtr::IndexingBuf(p, index, 0)); + self.p = CodePtr::Local(LocalCodePtr::DirEntry(p)); + self.oip = index as u32; + self.iip = 0; } else { unreachable!() } @@ -3886,9 +3892,11 @@ impl MachineState { ) { let p = self.p.local(); - match code_repo.find_living_dynamic(p, self.cc) { + match self.find_living_dynamic(&code_repo.code, self.oip, self.iip) { Some((offset, oi, ii, is_next_clause)) => { - self.p = CodePtr::Local(LocalCodePtr::IndexingBuf(p.abs_loc(), oi, ii)); + self.p = CodePtr::Local(LocalCodePtr::DirEntry(p.abs_loc())); + self.oip = oi; + self.iip = ii; match self.dynamic_mode { FirstOrNext::First if !is_next_clause => { @@ -3898,14 +3906,15 @@ impl MachineState { // there's a leading DynamicElse that sets self.cc. // self.cc = self.global_clock; - match code_repo.find_living_dynamic( - LocalCodePtr::IndexingBuf(p.abs_loc(), oi, ii + 1), - self.cc, - ) { + // see that there is a following dynamic_else + // clause so we avoid generating a choice + // point in case there isn't. + match self.find_living_dynamic(&code_repo.code, oi, ii + 1) { Some(_) => { self.registers[self.num_of_args + 1] = fixnum_as_cell!(Fixnum::build_with(self.cc as i64)); - self.num_of_args += 1; + + self.num_of_args += 2; self.execute_indexed_choice_instr( &IndexedChoiceInstruction::Try(offset), @@ -3913,39 +3922,39 @@ impl MachineState { global_variables, ); - self.num_of_args -= 1; + self.num_of_args -= 2; } None => { - self.p = - CodePtr::Local(LocalCodePtr::DirEntry(p.abs_loc() + offset)); + self.p = CodePtr::Local(LocalCodePtr::DirEntry(p.abs_loc() + offset)); + self.oip = 0; + self.iip = 0; } } } FirstOrNext::Next => { + let b = self.b; let n = self .stack - .index_or_frame(self.b) + .index_or_frame(b) .prelude .univ_prelude .num_cells; - self.cc = cell_as_fixnum!(self.stack[n - 1]).get_num() as usize; + self.cc = cell_as_fixnum!(self.stack[stack_loc!(OrFrame, b, n-2)]) + .get_num() as usize; if is_next_clause { - match code_repo.find_living_dynamic( - LocalCodePtr::IndexingBuf(p.abs_loc(), oi, ii + 1), - self.cc, - ) { + match self.find_living_dynamic(&code_repo.code, self.oip, self.iip) { Some(_) => { try_or_fail!( self, - call_policy.retry(self, offset, global_variables,) - ) + call_policy.retry(self, offset, global_variables) + ); } None => { try_or_fail!( self, - call_policy.trust(self, offset, global_variables,) + call_policy.trust(self, offset, global_variables) ) } } @@ -3979,19 +3988,36 @@ impl MachineState { or_frame.prelude.e = self.e; or_frame.prelude.cp = self.cp; or_frame.prelude.b = self.b; - or_frame.prelude.bp = self.p.local() + 1; + or_frame.prelude.bp = self.p.local(); // + 1; in self.iip now! + or_frame.prelude.boip = self.oip; + or_frame.prelude.biip = self.iip + 1; or_frame.prelude.tr = self.tr; or_frame.prelude.h = self.heap.len(); or_frame.prelude.b0 = self.b0; self.b = b; - for i in 1..n + 1 { - self.stack.index_or_frame_mut(b)[i - 1] = self.registers[i]; + for i in 0..n { + or_frame[i] = self.registers[i+1]; } + /* + self.iip += 1; + + let oip_b = self.oip.to_ne_bytes(); + let iip_b = self.iip.to_ne_bytes(); + + or_frame[n] = HeapCellValue::from_bytes( + [oip_b[0], oip_b[1], oip_b[2], oip_b[3], + iip_b[0], iip_b[1], iip_b[2], iip_b[3]], + ); + */ + self.hb = self.heap.len(); self.p = CodePtr::Local(dir_entry!(self.p.local().abs_loc() + offset)); + + self.oip = 0; + self.iip = 0; } &IndexedChoiceInstruction::Retry(l) => { try_or_fail!(self, call_policy.retry(self, l, global_variables)); @@ -4017,7 +4043,7 @@ impl MachineState { let p = self.p.local().abs_loc(); - match code_repo.find_living_dynamic_else(p, self.cc) { + match self.find_living_dynamic_else(&code_repo.code, p) { Some((p, next_i)) => { self.p = CodePtr::Local(LocalCodePtr::DirEntry(p)); @@ -4028,7 +4054,7 @@ impl MachineState { FirstOrNext::First => { self.cc = self.global_clock; - match code_repo.find_living_dynamic_else(p + next_i, self.cc) { + match self.find_living_dynamic_else(&code_repo.code, p + next_i) { Some(_) => { self.registers[self.num_of_args + 1] = fixnum_as_cell!(Fixnum::build_with(self.cc as i64)); @@ -4056,11 +4082,11 @@ impl MachineState { .univ_prelude .num_cells; - self.cc = cell_as_fixnum!(self.stack.index_or_frame(self.b)[n - 1]) + self.cc = cell_as_fixnum!(self.stack[stack_loc!(OrFrame, self.b, n-1)]) .get_num() as usize; if next_i > 0 { - match code_repo.find_living_dynamic_else(p + next_i, self.cc) { + match self.find_living_dynamic_else(&code_repo.code, p + next_i) { Some(_) => { try_or_fail!( self, @@ -4094,7 +4120,7 @@ impl MachineState { &ChoiceInstruction::DynamicInternalElse(..) => { let p = self.p.local().abs_loc(); - match code_repo.find_living_dynamic_else(p, self.cc) { + match self.find_living_dynamic_else(&code_repo.code, p) { Some((p, next_i)) => { self.p = CodePtr::Local(LocalCodePtr::DirEntry(p)); @@ -4103,7 +4129,7 @@ impl MachineState { self.p = CodePtr::Local(LocalCodePtr::DirEntry(p + 1)); } FirstOrNext::First => { - match code_repo.find_living_dynamic_else(p + next_i, self.cc) { + match self.find_living_dynamic_else(&code_repo.code, p + next_i) { Some(_) => { self.registers[self.num_of_args + 1] = fixnum_as_cell!(Fixnum::build_with(self.cc as i64)); @@ -4131,11 +4157,11 @@ impl MachineState { .univ_prelude .num_cells; - self.cc = cell_as_fixnum!(self.stack.index_or_frame(self.b)[n - 1]) + self.cc = cell_as_fixnum!(self.stack[stack_loc!(OrFrame, self.b, n-1)]) .get_num() as usize; if next_i > 0 { - match code_repo.find_living_dynamic_else(p + next_i, self.cc) { + match self.find_living_dynamic_else(&code_repo.code, p + next_i) { Some(_) => { try_or_fail!( self, @@ -4149,14 +4175,14 @@ impl MachineState { None => { try_or_fail!( self, - call_policy.trust_me(self, global_variables,) + call_policy.trust_me(self, global_variables) ) } } } else { try_or_fail!( self, - call_policy.trust_me(self, global_variables,) + call_policy.trust_me(self, global_variables) ) } } @@ -4179,6 +4205,8 @@ impl MachineState { or_frame.prelude.cp = self.cp; or_frame.prelude.b = self.b; or_frame.prelude.bp = self.p.local() + offset; + or_frame.prelude.boip = 0; + or_frame.prelude.biip = 0; or_frame.prelude.tr = self.tr; or_frame.prelude.h = self.heap.len(); or_frame.prelude.b0 = self.b0; diff --git a/src/machine/mod.rs b/src/machine/mod.rs index ccb952cd..7a73ac30 100644 --- a/src/machine/mod.rs +++ b/src/machine/mod.rs @@ -612,7 +612,7 @@ impl MachineState { user_input: &mut Stream, user_output: &mut Stream, ) { - let instr = match code_repo.lookup_instr(self.last_call, &self.p) { + let instr = match code_repo.lookup_instr(self, &self.p) { Some(instr) => instr, None => return, }; @@ -629,9 +629,13 @@ impl MachineState { fn backtrack(&mut self) { let b = self.b; + let or_frame = self.stack.index_or_frame(b); - self.b0 = self.stack.index_or_frame(b).prelude.b0; - self.p = CodePtr::Local(self.stack.index_or_frame(b).prelude.bp); + self.b0 = or_frame.prelude.b0; + self.p = CodePtr::Local(or_frame.prelude.bp); + + self.oip = or_frame.prelude.boip; + self.iip = or_frame.prelude.biip; self.pdl.clear(); self.fail = false; @@ -639,8 +643,7 @@ impl MachineState { fn check_machine_index(&mut self, code_repo: &CodeRepo) -> bool { match self.p { - CodePtr::Local(LocalCodePtr::DirEntry(p)) | - CodePtr::Local(LocalCodePtr::IndexingBuf(p, ..)) + CodePtr::Local(LocalCodePtr::DirEntry(p)) if p < code_repo.code.len() => {} CodePtr::Local(LocalCodePtr::Halt) | CodePtr::REPL(..) => { return false; @@ -661,7 +664,7 @@ impl MachineState { user_output: &mut Stream, ) -> bool { loop { - let instr = match code_repo.lookup_instr(self.last_call, &self.p) { + let instr = match code_repo.lookup_instr(self, &self.p) { Some(instr) => { if instr.as_ref().is_head_instr() { instr @@ -721,7 +724,7 @@ impl MachineState { let instigating_p = CodePtr::Local(self.attr_var_init.instigating_p); let instigating_instr = code_repo - .lookup_instr(false, &instigating_p) + .lookup_instr(self, &instigating_p) .unwrap(); if !instigating_instr.as_ref().is_head_instr() { diff --git a/src/machine/stack.rs b/src/machine/stack.rs index 1ab988cd..b175917b 100644 --- a/src/machine/stack.rs +++ b/src/machine/stack.rs @@ -121,6 +121,8 @@ pub(crate) struct OrFramePrelude { pub(crate) cp: LocalCodePtr, pub(crate) b: usize, pub(crate) bp: LocalCodePtr, + pub(crate) boip: u32, + pub(crate) biip: u32, pub(crate) tr: usize, pub(crate) h: usize, pub(crate) b0: usize, diff --git a/src/machine/system_calls.rs b/src/machine/system_calls.rs index 0720c265..15217cec 100644 --- a/src/machine/system_calls.rs +++ b/src/machine/system_calls.rs @@ -3145,7 +3145,7 @@ impl MachineState { let p_functor = self.store(self.deref(self.registers[2])); let p = to_local_code_ptr(&self.heap, p_functor).unwrap(); - let num_cells = match code_repo.lookup_instr(self.last_call, &CodePtr::Local(p)) { + let num_cells = match code_repo.lookup_instr(self, &CodePtr::Local(p)) { Some(line) => { let perm_vars = match line.as_ref() { Line::Control(ref ctrl_instr) => ctrl_instr.perm_vars(), @@ -3720,7 +3720,7 @@ impl MachineState { } }; - if p.is_reset_cont_marker(code_repo, self.last_call) { + if self.is_reset_cont_marker(&code_repo, p) { return return_from_clause!(self.last_call, self); } @@ -4408,7 +4408,7 @@ impl MachineState { let mut cp = self.cp; while e > 0 { - if cp.is_reset_cont_marker(code_repo, self.last_call) { + if self.is_reset_cont_marker(code_repo, cp) { self.e = e; self.p = CodePtr::Local(cp + 1); // skip the reset marker. diff --git a/src/write.rs b/src/write.rs index 49e8c10b..455cc72b 100644 --- a/src/write.rs +++ b/src/write.rs @@ -20,9 +20,9 @@ impl fmt::Display for LocalCodePtr { match self { LocalCodePtr::DirEntry(p) => write!(f, "LocalCodePtr::DirEntry({})", p), LocalCodePtr::Halt => write!(f, "LocalCodePtr::Halt"), - LocalCodePtr::IndexingBuf(p, o, i) => { - write!(f, "LocalCodePtr::IndexingBuf({}, {}, {})", p, o, i) - } + // LocalCodePtr::IndexingBuf(p, o, i) => { + // write!(f, "LocalCodePtr::IndexingBuf({}, {}, {})", p, o, i) + // } } } } -- 2.54.0