From: Mark Thom Date: Tue, 27 Apr 2021 08:00:54 +0000 (-0600) Subject: fix use of local skeletons to reload predicates (#919) X-Git-Tag: v0.9.0~97 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=7e4cfede7d52ee1651b2c445774447138065690c;p=scryer-prolog.git fix use of local skeletons to reload predicates (#919) --- diff --git a/src/forms.rs b/src/forms.rs index 1f0affc7..ed710305 100644 --- a/src/forms.rs +++ b/src/forms.rs @@ -696,24 +696,22 @@ impl PredicateInfo { } } -#[derive(Debug)] -pub(crate) struct PredicateSkeleton { +#[derive(Clone, Debug)] +pub(crate) struct LocalPredicateSkeleton { pub(crate) is_discontiguous: bool, pub(crate) is_dynamic: bool, pub(crate) is_multifile: bool, - pub(crate) clauses: SliceDeque, pub(crate) clause_clause_locs: SliceDeque, pub(crate) clause_assert_margin: usize, } -impl PredicateSkeleton { +impl LocalPredicateSkeleton { #[inline] pub(crate) fn new() -> Self { - PredicateSkeleton { + Self { is_discontiguous: false, is_dynamic: false, is_multifile: false, - clauses: sdeq![], clause_clause_locs: sdeq![], clause_assert_margin: 0, } @@ -732,23 +730,56 @@ impl PredicateSkeleton { #[inline] pub(crate) fn reset(&mut self) { - self.clauses.clear(); self.clause_clause_locs.clear(); self.clause_assert_margin = 0; } +} + +#[derive(Clone, Debug)] +pub(crate) struct PredicateSkeleton { + pub(crate) core: LocalPredicateSkeleton, + pub(crate) clauses: SliceDeque, +} + +impl PredicateSkeleton { + #[inline] + pub(crate) fn new() -> Self { + PredicateSkeleton { + core: LocalPredicateSkeleton::new(), + clauses: sdeq![], + } + } + + #[inline] + pub(crate) fn predicate_info(&self) -> PredicateInfo { + PredicateInfo { + is_extensible: true, + is_discontiguous: self.core.is_discontiguous, + is_dynamic: self.core.is_dynamic, + is_multifile: self.core.is_multifile, + has_clauses: !self.clauses.is_empty(), + } + } + + #[inline] + pub(crate) fn reset(&mut self) { + self.core.clause_clause_locs.clear(); + self.core.clause_assert_margin = 0; + self.clauses.clear(); + } pub(crate) fn target_pos_of_clause_clause_loc( &self, clause_clause_loc: usize, ) -> Option { - let search_result = self.clause_clause_locs[0..self.clause_assert_margin] + let search_result = self.core.clause_clause_locs[0..self.core.clause_assert_margin] .binary_search_by(|loc| clause_clause_loc.cmp(&loc)); match search_result { Ok(loc) => Some(loc), - Err(_) => self.clause_clause_locs[self.clause_assert_margin..] + Err(_) => self.core.clause_clause_locs[self.core.clause_assert_margin..] .binary_search_by(|loc| loc.cmp(&clause_clause_loc)) - .map(|loc| loc + self.clause_assert_margin) + .map(|loc| loc + self.core.clause_assert_margin) .ok(), } } diff --git a/src/lib/builtins.pl b/src/lib/builtins.pl index c7ea8001..c95de3f5 100644 --- a/src/lib/builtins.pl +++ b/src/lib/builtins.pl @@ -847,7 +847,7 @@ asserta_clause(Head, Body) :- asserta(Clause) :- ( Clause \= (_ :- _) -> Head = Clause, - Body = true, + Body = user:true, asserta_clause(Head, Body) ; Clause = (Head :- Body) -> asserta_clause(Head, Body) @@ -897,7 +897,7 @@ assertz_clause(Head, Body) :- assertz(Clause) :- ( Clause \= (_ :- _) -> Head = Clause, - Body = true, + Body = user:true, assertz_clause(Head, Body) ; Clause = (Head :- Body) -> assertz_clause(Head, Body) diff --git a/src/loader.pl b/src/loader.pl index eb79c162..6324e7cd 100644 --- a/src/loader.pl +++ b/src/loader.pl @@ -509,7 +509,8 @@ use_module(Module, Exports, Evacuable) :- ; ( path_atom(Module, ModulePath) -> load_context_path(ModulePath, Path), open_file(Path, Stream), - file_load(Stream, Path, Subevacuable), + stream_property(Stream, file_name(PathFileName)), + file_load(Stream, PathFileName, Subevacuable), '$use_module'(Evacuable, Subevacuable, Exports) ; type_error(atom, Library, load/1) ) diff --git a/src/machine/compile.rs b/src/machine/compile.rs index c2c68eae..987e88da 100644 --- a/src/machine/compile.rs +++ b/src/machine/compile.rs @@ -2,7 +2,7 @@ use prolog_parser::clause_name; use crate::codegen::*; use crate::debray_allocator::*; -use crate::indexing::{IndexingCodePtr, merge_clause_index, remove_index}; +use crate::indexing::{merge_clause_index, remove_index, IndexingCodePtr}; use crate::machine::load_state::*; use crate::machine::loader::*; use crate::machine::preprocessor::*; @@ -130,8 +130,8 @@ fn derelictize_try_me_else( retraction_info.push_record(RetractionRecord::ReplacedDynamicElseOffset(index, *o)); Some(mem::replace(o, 0)) } - Line::Choice(ChoiceInstruction::DynamicElse(_, _, NextOrFail::Fail(_))) | - Line::Choice(ChoiceInstruction::DynamicInternalElse(_, _, NextOrFail::Fail(_))) => None, + Line::Choice(ChoiceInstruction::DynamicElse(_, _, NextOrFail::Fail(_))) + | Line::Choice(ChoiceInstruction::DynamicInternalElse(_, _, NextOrFail::Fail(_))) => None, Line::Choice(ChoiceInstruction::TryMeElse(0)) => None, Line::Choice(ChoiceInstruction::TryMeElse(ref mut o)) => { retraction_info.push_record(RetractionRecord::ModifiedTryMeElse(index, *o)); @@ -181,14 +181,11 @@ fn merge_indices( } } -fn find_outer_choice_instr( - code: &Code, - mut index: usize, -) -> usize { +fn find_outer_choice_instr(code: &Code, mut index: usize) -> usize { loop { match &code[index] { - Line::Choice(ChoiceInstruction::DynamicElse(_, _, NextOrFail::Next(i))) | - Line::Choice(ChoiceInstruction::DynamicInternalElse(_, _, NextOrFail::Next(i))) + Line::Choice(ChoiceInstruction::DynamicElse(_, _, NextOrFail::Next(i))) + | Line::Choice(ChoiceInstruction::DynamicInternalElse(_, _, NextOrFail::Next(i))) if *i > 0 => { index += i; @@ -200,23 +197,19 @@ fn find_outer_choice_instr( } } -fn find_inner_choice_instr( - code: &Code, - mut index: usize, - index_loc: usize, -) -> usize { +fn find_inner_choice_instr(code: &Code, mut index: usize, index_loc: usize) -> usize { loop { match &code[index] { - Line::Choice(ChoiceInstruction::TryMeElse(o)) | - Line::Choice(ChoiceInstruction::RetryMeElse(o)) => { + Line::Choice(ChoiceInstruction::TryMeElse(o)) + | Line::Choice(ChoiceInstruction::RetryMeElse(o)) => { if *o > 0 { return index; } else { index = index_loc; } } - &Line::Choice(ChoiceInstruction::DynamicElse(_, _, next_or_fail)) | - &Line::Choice(ChoiceInstruction::DynamicInternalElse(_, _, next_or_fail)) => { + &Line::Choice(ChoiceInstruction::DynamicElse(_, _, next_or_fail)) + | &Line::Choice(ChoiceInstruction::DynamicInternalElse(_, _, next_or_fail)) => { match next_or_fail { NextOrFail::Next(i) => { if i == 0 { @@ -234,28 +227,24 @@ fn find_inner_choice_instr( return index; } Line::IndexingCode(indexing_code) => match &indexing_code[0] { - IndexingLine::Indexing(IndexingInstruction::SwitchOnTerm(_, v, ..)) => { - match v { - IndexingCodePtr::External(v) => { - index += v; + IndexingLine::Indexing(IndexingInstruction::SwitchOnTerm(_, v, ..)) => match v { + IndexingCodePtr::External(v) => { + index += v; + } + IndexingCodePtr::DynamicExternal(v) => match &code[index + v] { + &Line::Choice(ChoiceInstruction::DynamicInternalElse( + _, + _, + NextOrFail::Next(0), + )) => { + return index + v; } - IndexingCodePtr::DynamicExternal(v) => { - match &code[index + v] { - &Line::Choice(ChoiceInstruction::DynamicInternalElse( - _, - _, - NextOrFail::Next(0), - )) => { - return index + v; - } - _ => { - index += v; - } - } + _ => { + index += v; } - _ => unreachable!() - } - } + }, + _ => unreachable!(), + }, _ => { unreachable!(); } @@ -399,10 +388,10 @@ fn delete_from_skeleton( retraction_info: &mut RetractionInfo, ) -> usize { let clause_index_info = skeleton.clauses.remove(target_pos); - let clause_clause_loc = skeleton.clause_clause_locs.remove(target_pos); + let clause_clause_loc = skeleton.core.clause_clause_locs.remove(target_pos); - if target_pos < skeleton.clause_assert_margin { - skeleton.clause_assert_margin -= 1; + if target_pos < skeleton.core.clause_assert_margin { + skeleton.core.clause_assert_margin -= 1; } retraction_info.push_record(RetractionRecord::RemovedSkeletonClause( @@ -430,29 +419,36 @@ fn blunt_leading_choice_instr( return instr_loc; } - Line::Choice(ChoiceInstruction::DynamicElse(_, _, NextOrFail::Next(_))) | - Line::Choice(ChoiceInstruction::DynamicInternalElse(_, _, NextOrFail::Next(_))) => { + Line::Choice(ChoiceInstruction::DynamicElse(_, _, NextOrFail::Next(_))) + | Line::Choice(ChoiceInstruction::DynamicInternalElse(_, _, NextOrFail::Next(_))) => { return instr_loc; } &mut Line::Choice(ChoiceInstruction::DynamicElse(b, d, NextOrFail::Fail(o))) => { - retraction_info.push_record( - RetractionRecord::AppendedNextOrFail(instr_loc, NextOrFail::Fail(o)), - ); + retraction_info.push_record(RetractionRecord::AppendedNextOrFail( + instr_loc, + NextOrFail::Fail(o), + )); - code[instr_loc] = Line::Choice( - ChoiceInstruction::DynamicElse(b, d, NextOrFail::Next(0)), - ); + code[instr_loc] = + Line::Choice(ChoiceInstruction::DynamicElse(b, d, NextOrFail::Next(0))); return instr_loc; } - &mut Line::Choice(ChoiceInstruction::DynamicInternalElse(b, d, NextOrFail::Fail(o))) => { - retraction_info.push_record( - RetractionRecord::AppendedNextOrFail(instr_loc, NextOrFail::Fail(o)), - ); + &mut Line::Choice(ChoiceInstruction::DynamicInternalElse( + b, + d, + NextOrFail::Fail(o), + )) => { + retraction_info.push_record(RetractionRecord::AppendedNextOrFail( + instr_loc, + NextOrFail::Fail(o), + )); - code[instr_loc] = Line::Choice( - ChoiceInstruction::DynamicInternalElse(b, d, NextOrFail::Next(0)), - ); + code[instr_loc] = Line::Choice(ChoiceInstruction::DynamicInternalElse( + b, + d, + NextOrFail::Next(0), + )); return instr_loc; } @@ -488,21 +484,19 @@ fn set_switch_var_offset_to_choice_instr( let target_indexing_line = to_indexing_line_mut(&mut code[index_loc]).unwrap(); let v = match &target_indexing_line[0] { - &IndexingLine::Indexing(IndexingInstruction::SwitchOnTerm(_, v, ..)) => { - match v { - IndexingCodePtr::External(v) | IndexingCodePtr::DynamicExternal(v) => v, - _ => unreachable!() - } - } + &IndexingLine::Indexing(IndexingInstruction::SwitchOnTerm(_, v, ..)) => match v { + IndexingCodePtr::External(v) | IndexingCodePtr::DynamicExternal(v) => v, + _ => unreachable!(), + }, _ => { unreachable!(); } }; match &code[index_loc + v] { - Line::Choice(ChoiceInstruction::TryMeElse(_)) | - Line::Choice(ChoiceInstruction::DynamicElse(..)) | - Line::Choice(ChoiceInstruction::DynamicInternalElse(..)) => {} + Line::Choice(ChoiceInstruction::TryMeElse(_)) + | Line::Choice(ChoiceInstruction::DynamicElse(..)) + | Line::Choice(ChoiceInstruction::DynamicInternalElse(..)) => {} _ => { set_switch_var_offset(code, index_loc, offset, retraction_info); } @@ -519,17 +513,13 @@ fn set_switch_var_offset( let target_indexing_line = to_indexing_line_mut(&mut code[index_loc]).unwrap(); let old_v = match &mut target_indexing_line[0] { - IndexingLine::Indexing(IndexingInstruction::SwitchOnTerm(_, ref mut v, ..)) => { - match *v { - IndexingCodePtr::DynamicExternal(_) => { - mem::replace(v, IndexingCodePtr::DynamicExternal(offset)) - } - IndexingCodePtr::External(_) => { - mem::replace(v, IndexingCodePtr::External(offset)) - } - _ => unreachable!() + IndexingLine::Indexing(IndexingInstruction::SwitchOnTerm(_, ref mut v, ..)) => match *v { + IndexingCodePtr::DynamicExternal(_) => { + mem::replace(v, IndexingCodePtr::DynamicExternal(offset)) } - } + IndexingCodePtr::External(_) => mem::replace(v, IndexingCodePtr::External(offset)), + _ => unreachable!(), + }, _ => { unreachable!() } @@ -546,33 +536,33 @@ fn internalize_choice_instr_at( retraction_info: &mut RetractionInfo, ) { match &mut code[instr_loc] { - Line::Choice(ChoiceInstruction::DynamicElse(_, _, NextOrFail::Fail(_))) | - Line::Choice(ChoiceInstruction::DynamicInternalElse(_, _, NextOrFail::Fail(_))) => { - } + Line::Choice(ChoiceInstruction::DynamicElse(_, _, NextOrFail::Fail(_))) + | Line::Choice(ChoiceInstruction::DynamicInternalElse(_, _, NextOrFail::Fail(_))) => {} Line::Choice(ChoiceInstruction::DynamicElse(_, _, ref mut o @ NextOrFail::Next(0))) => { retraction_info.push_record(RetractionRecord::ReplacedDynamicElseOffset(instr_loc, 0)); *o = NextOrFail::Fail(0); } &mut Line::Choice(ChoiceInstruction::DynamicElse(b, d, NextOrFail::Next(o))) => { - retraction_info.push_record( - RetractionRecord::AppendedNextOrFail(instr_loc, NextOrFail::Next(o)), - ); + retraction_info.push_record(RetractionRecord::AppendedNextOrFail( + instr_loc, + NextOrFail::Next(o), + )); match &mut code[instr_loc + o] { Line::Control(ControlInstruction::RevJmpBy(p)) if *p == 0 => { - code[instr_loc] = Line::Choice( - ChoiceInstruction::DynamicElse(b, d, NextOrFail::Fail(o)), - ); + code[instr_loc] = + Line::Choice(ChoiceInstruction::DynamicElse(b, d, NextOrFail::Fail(o))); } _ => { - code[instr_loc] = Line::Choice( - ChoiceInstruction::DynamicElse(b, d, NextOrFail::Next(o)), - ); + code[instr_loc] = + Line::Choice(ChoiceInstruction::DynamicElse(b, d, NextOrFail::Next(o))); } } } Line::Choice(ChoiceInstruction::DynamicInternalElse( - _, _, ref mut o @ NextOrFail::Next(0), + _, + _, + ref mut o @ NextOrFail::Next(0), )) => { retraction_info.push_record(RetractionRecord::ReplacedDynamicElseOffset(instr_loc, 0)); *o = NextOrFail::Fail(0); @@ -582,14 +572,18 @@ fn internalize_choice_instr_at( match &mut code[instr_loc + o] { Line::Control(ControlInstruction::RevJmpBy(p)) if *p == 0 => { - code[instr_loc] = Line::Choice( - ChoiceInstruction::DynamicInternalElse(b, d, NextOrFail::Fail(o)), - ); + code[instr_loc] = Line::Choice(ChoiceInstruction::DynamicInternalElse( + b, + d, + NextOrFail::Fail(o), + )); } _ => { - code[instr_loc] = Line::Choice( - ChoiceInstruction::DynamicInternalElse(b, d, NextOrFail::Next(o)), - ); + code[instr_loc] = Line::Choice(ChoiceInstruction::DynamicInternalElse( + b, + d, + NextOrFail::Next(o), + )); } } } @@ -626,8 +620,8 @@ fn thread_choice_instr_at_to( ) { loop { match &mut code[instr_loc] { - Line::Choice(ChoiceInstruction::TryMeElse(ref mut o)) | - Line::Choice(ChoiceInstruction::RetryMeElse(ref mut o)) + Line::Choice(ChoiceInstruction::TryMeElse(ref mut o)) + | Line::Choice(ChoiceInstruction::RetryMeElse(ref mut o)) if target_loc >= instr_loc => { retraction_info.push_record(RetractionRecord::ReplacedChoiceOffset(instr_loc, *o)); @@ -635,20 +629,27 @@ fn thread_choice_instr_at_to( *o = target_loc - instr_loc; return; } - Line::Choice(ChoiceInstruction::DynamicElse(_, _, NextOrFail::Next(ref mut o))) | - Line::Choice(ChoiceInstruction::DynamicInternalElse(_, _, NextOrFail::Next(ref mut o))) - if target_loc >= instr_loc => - { - retraction_info.push_record(RetractionRecord::ReplacedDynamicElseOffset(instr_loc, *o)); + Line::Choice(ChoiceInstruction::DynamicElse(_, _, NextOrFail::Next(ref mut o))) + | Line::Choice(ChoiceInstruction::DynamicInternalElse( + _, + _, + NextOrFail::Next(ref mut o), + )) if target_loc >= instr_loc => { + retraction_info + .push_record(RetractionRecord::ReplacedDynamicElseOffset(instr_loc, *o)); *o = target_loc - instr_loc; return; } - Line::Choice(ChoiceInstruction::DynamicElse(_, _, NextOrFail::Next(ref mut o))) | - Line::Choice(ChoiceInstruction::DynamicInternalElse(_, _, NextOrFail::Next(ref mut o))) => { + Line::Choice(ChoiceInstruction::DynamicElse(_, _, NextOrFail::Next(ref mut o))) + | Line::Choice(ChoiceInstruction::DynamicInternalElse( + _, + _, + NextOrFail::Next(ref mut o), + )) => { instr_loc += *o; } - Line::Choice(ChoiceInstruction::TryMeElse(ref mut o)) | - Line::Choice(ChoiceInstruction::RetryMeElse(ref mut o)) => { + Line::Choice(ChoiceInstruction::TryMeElse(ref mut o)) + | Line::Choice(ChoiceInstruction::RetryMeElse(ref mut o)) => { instr_loc += *o; } Line::Control(ControlInstruction::RevJmpBy(ref mut o)) if instr_loc >= target_loc => { @@ -660,33 +661,31 @@ fn thread_choice_instr_at_to( &mut Line::Choice(ChoiceInstruction::DynamicElse(birth, death, ref mut fail)) if target_loc >= instr_loc => { - retraction_info.push_record( - RetractionRecord::AppendedNextOrFail(instr_loc, *fail), - ); + retraction_info.push_record(RetractionRecord::AppendedNextOrFail(instr_loc, *fail)); - code[instr_loc] = - Line::Choice(ChoiceInstruction::DynamicElse( - birth, death, NextOrFail::Next(target_loc - instr_loc), - )); + code[instr_loc] = Line::Choice(ChoiceInstruction::DynamicElse( + birth, + death, + NextOrFail::Next(target_loc - instr_loc), + )); return; } - Line::Choice(ChoiceInstruction::DynamicElse(_, _, NextOrFail::Fail(o))) - if *o > 0 => - { + Line::Choice(ChoiceInstruction::DynamicElse(_, _, NextOrFail::Fail(o))) if *o > 0 => { instr_loc += *o; } - &mut Line::Choice(ChoiceInstruction::DynamicInternalElse(birth, death, ref mut fail)) - if target_loc >= instr_loc => - { - retraction_info.push_record( - RetractionRecord::AppendedNextOrFail(instr_loc, *fail), - ); - - code[instr_loc] = - Line::Choice(ChoiceInstruction::DynamicInternalElse( - birth, death, NextOrFail::Next(target_loc - instr_loc), - )); + &mut Line::Choice(ChoiceInstruction::DynamicInternalElse( + birth, + death, + ref mut fail, + )) if target_loc >= instr_loc => { + retraction_info.push_record(RetractionRecord::AppendedNextOrFail(instr_loc, *fail)); + + code[instr_loc] = Line::Choice(ChoiceInstruction::DynamicInternalElse( + birth, + death, + NextOrFail::Next(target_loc - instr_loc), + )); return; } @@ -830,26 +829,17 @@ fn remove_leading_unindexed_clause( } } -fn find_dynamic_outer_choice_instr( - code: &Code, - index_loc: usize, -) -> usize { +fn find_dynamic_outer_choice_instr(code: &Code, index_loc: usize) -> usize { match &code[index_loc] { - Line::IndexingCode(indexing_code) => { - match &indexing_code[0] { - &IndexingLine::Indexing( - IndexingInstruction::SwitchOnTerm( - _, - IndexingCodePtr::DynamicExternal(v), - .., - ) - ) => { - index_loc + v - 2 - } - _ => unreachable!() - } - } - _ => unreachable!() + Line::IndexingCode(indexing_code) => match &indexing_code[0] { + &IndexingLine::Indexing(IndexingInstruction::SwitchOnTerm( + _, + IndexingCodePtr::DynamicExternal(v), + .., + )) => index_loc + v - 2, + _ => unreachable!(), + }, + _ => unreachable!(), } } @@ -869,7 +859,7 @@ fn prepend_compiled_clause( let head_arg_num = skeleton.clauses[1].opt_arg_index_key.arg_num(); let settings = CodeGenSettings { - global_clock_tick: if skeleton.is_dynamic { + global_clock_tick: if skeleton.core.is_dynamic { Some(global_clock_tick) } else { None @@ -895,7 +885,7 @@ fn prepend_compiled_clause( skeleton.clauses[0].clause_start, )); - let outer_thread_choice_loc = if skeleton.is_dynamic { + let outer_thread_choice_loc = if skeleton.core.is_dynamic { find_dynamic_outer_choice_instr(code, index_loc) } else { skeleton.clauses[1].clause_start - 2 @@ -996,7 +986,7 @@ fn prepend_compiled_clause( code.extend(prepend_queue.into_iter()); - if skeleton.is_dynamic { + if skeleton.core.is_dynamic { clause_loc } else { clause_loc + (outer_thread_choice_offset == 0) as usize @@ -1010,15 +1000,11 @@ fn prepend_compiled_clause( let old_clause_start = match skeleton.clauses[1].opt_arg_index_key.switch_on_term_loc() { - Some(index_loc) if skeleton.is_dynamic => { + Some(index_loc) if skeleton.core.is_dynamic => { find_dynamic_outer_choice_instr(code, index_loc) } - Some(_) => { - skeleton.clauses[1].clause_start - 2 - } - None => { - skeleton.clauses[1].clause_start - } + Some(_) => skeleton.clauses[1].clause_start - 2, + None => skeleton.clauses[1].clause_start, }; let inner_thread_rev_offset = @@ -1035,7 +1021,9 @@ fn prepend_compiled_clause( *o = prepend_queue_len - 2; } Line::Choice(ChoiceInstruction::DynamicInternalElse( - _, _, ref mut o @ NextOrFail::Next(0), + _, + _, + ref mut o @ NextOrFail::Next(0), )) => { *o = NextOrFail::Fail(prepend_queue_len - 2); } @@ -1048,9 +1036,7 @@ fn prepend_compiled_clause( inner_thread_rev_offset, ))); - prepend_queue.push_front(Line::Choice( - settings.try_me_else(prepend_queue.len()), - )); + prepend_queue.push_front(Line::Choice(settings.try_me_else(prepend_queue.len()))); // prepend_queue is now: // | TryMeElse(N_2) @@ -1071,7 +1057,7 @@ fn prepend_compiled_clause( Some(index_loc) => { prepend_queue.extend(clause_code.drain(1..)); - let old_clause_start = if skeleton.is_dynamic { + let old_clause_start = if skeleton.core.is_dynamic { find_dynamic_outer_choice_instr(code, index_loc) } else { skeleton.clauses[1].clause_start - 2 @@ -1084,9 +1070,7 @@ fn prepend_compiled_clause( inner_thread_rev_offset, ))); - prepend_queue.push_front(Line::Choice( - settings.try_me_else(prepend_queue.len()), - )); + prepend_queue.push_front(Line::Choice(settings.try_me_else(prepend_queue.len()))); // prepend_queue is now: // | TryMeElse(N_2) @@ -1114,9 +1098,7 @@ fn prepend_compiled_clause( inner_thread_rev_offset, ))); - prepend_queue.push_front(Line::Choice( - settings.try_me_else(prepend_queue.len()), - )); + prepend_queue.push_front(Line::Choice(settings.try_me_else(prepend_queue.len()))); // prepend_queue is now: // | TryMeElse(N_2) @@ -1135,7 +1117,7 @@ fn prepend_compiled_clause( } }; - if skeleton.is_dynamic { + if skeleton.core.is_dynamic { IndexPtr::DynamicIndex(clause_loc) } else { IndexPtr::Index(clause_loc) @@ -1154,7 +1136,7 @@ fn append_compiled_clause( let lower_bound = lower_bound_of_target_clause(skeleton, target_pos); let settings = CodeGenSettings { - global_clock_tick: if skeleton.is_dynamic { + global_clock_tick: if skeleton.core.is_dynamic { Some(global_clock_tick) } else { None @@ -1203,10 +1185,7 @@ fn append_compiled_clause( index_loc, ); - let target_pos_clause_start = find_outer_choice_instr( - code, - target_pos_clause_start, - ); + let target_pos_clause_start = find_outer_choice_instr(code, target_pos_clause_start); if lower_bound + 1 == target_pos { set_switch_var_offset_to_choice_instr( @@ -1215,6 +1194,10 @@ fn append_compiled_clause( target_pos_clause_start - index_loc, retraction_info, ); + + if lower_bound == 0 { + code_ptr_opt = Some(target_pos_clause_start); + } } target_pos_clause_start // skeleton.clauses[target_pos - 1].clause_start @@ -1251,7 +1234,7 @@ fn append_compiled_clause( // its variable offset. skeleton.clauses[target_pos].clause_start += 2; - if !skeleton.is_dynamic { + if !skeleton.core.is_dynamic { set_switch_var_offset(code, index_loc, 2, retraction_info); } } @@ -1267,7 +1250,7 @@ fn append_compiled_clause( thread_choice_instr_at_to(code, threaded_choice_instr_loc, clause_loc, retraction_info); code_ptr_opt.map(|p| { - if skeleton.is_dynamic { + if skeleton.core.is_dynamic { IndexPtr::DynamicIndex(p) } else { IndexPtr::Index(p) @@ -1324,7 +1307,10 @@ impl<'a> LoadState<'a> { if let Some(path_str) = load_context.path.to_str() { if !path_str.is_empty() { - return Some(clause_name!(path_str.to_string(), self.wam.machine_st.atom_tbl)); + return Some(clause_name!( + path_str.to_string(), + self.wam.machine_st.atom_tbl + )); } } } @@ -1366,10 +1352,8 @@ impl<'a> LoadState<'a> { predicates: &mut PredicateQueue, settings: CodeGenSettings, ) -> Result { - let code_index = self.get_or_insert_code_index( - key.clone(), - predicates.compilation_target.clone(), - ); + let code_index = + self.get_or_insert_code_index(key.clone(), predicates.compilation_target.clone()); let code_len = self.wam.code_repo.code.len(); let mut code_ptr = code_len; @@ -1380,7 +1364,7 @@ impl<'a> LoadState<'a> { let mut clauses = vec![]; let mut preprocessor = Preprocessor::new(self.wam.machine_st.flags); - for term in predicates.predicates.drain(0 ..) { + for term in predicates.predicates.drain(0..) { clauses.push(self.try_term_to_tl(term, &mut preprocessor)?); } @@ -1426,14 +1410,16 @@ impl<'a> LoadState<'a> { )); skeleton.clauses.extend(cg.skeleton.clauses.into_iter()); - skeleton.clause_clause_locs.extend_from_slice( - &clause_clause_locs[0 ..] - ); + skeleton + .core + .clause_clause_locs + .extend_from_slice(&clause_clause_locs[0..]); } None => { - cg.skeleton.clause_clause_locs.extend_from_slice( - &clause_clause_locs[0 ..] - ); + cg.skeleton + .core + .clause_clause_locs + .extend_from_slice(&clause_clause_locs[0..]); self.add_extensible_predicate( key.clone(), @@ -1443,23 +1429,6 @@ impl<'a> LoadState<'a> { } }; - if let Some(filename) = self.listing_src_file_name() { - if let CompilationTarget::User = &predicates.compilation_target { - let compilation_target = mem::replace( - &mut self.compilation_target, - CompilationTarget::Module(filename), - ); - - self.extend_local_predicate_skeleton( - &CompilationTarget::User, - &key, - clause_clause_locs.clone(), - ); - - self.compilation_target = compilation_target; - } - } - self.extend_local_predicate_skeleton( &predicates.compilation_target, &key, @@ -1498,30 +1467,28 @@ impl<'a> LoadState<'a> { key: &PredicateKey, clause_clause_locs: SliceDeque, ) { - match self - .wam - .indices - .get_local_predicate_skeleton_mut( - &self.compilation_target, - compilation_target.clone(), - key.clone(), - ) - { + match self.wam.indices.get_local_predicate_skeleton_mut( + self.compilation_target.clone(), + compilation_target.clone(), + self.listing_src_file_name(), + key.clone(), + ) { Some(skeleton) => { - self.retraction_info - .push_record(RetractionRecord::SkeletonLocalClauseTruncateBack( + self.retraction_info.push_record( + RetractionRecord::SkeletonLocalClauseTruncateBack( self.compilation_target.clone(), compilation_target.clone(), key.clone(), skeleton.clause_clause_locs.len(), - )); - - skeleton.clause_clause_locs.extend_from_slice( - &clause_clause_locs[0 ..] + ), ); + + skeleton + .clause_clause_locs + .extend_from_slice(&clause_clause_locs[0..]); } None => { - let mut skeleton = PredicateSkeleton::new(); + let mut skeleton = LocalPredicateSkeleton::new(); skeleton.clause_clause_locs = clause_clause_locs; self.add_local_extensible_predicate( @@ -1539,15 +1506,12 @@ impl<'a> LoadState<'a> { key: &PredicateKey, code_len: usize, ) { - match self - .wam - .indices - .get_local_predicate_skeleton_mut( - &self.compilation_target, - compilation_target.clone(), - key.clone(), - ) - { + match self.wam.indices.get_local_predicate_skeleton_mut( + self.compilation_target.clone(), + compilation_target.clone(), + self.listing_src_file_name(), + key.clone(), + ) { Some(skeleton) => { self.retraction_info.push_record( RetractionRecord::SkeletonLocalClauseClausePopFront( @@ -1560,7 +1524,7 @@ impl<'a> LoadState<'a> { skeleton.clause_clause_locs.push_front(code_len); } None => { - let mut skeleton = PredicateSkeleton::new(); + let mut skeleton = LocalPredicateSkeleton::new(); skeleton.clause_clause_locs.push_front(code_len); self.add_local_extensible_predicate( @@ -1578,15 +1542,12 @@ impl<'a> LoadState<'a> { key: &PredicateKey, code_len: usize, ) { - match self - .wam - .indices - .get_local_predicate_skeleton_mut( - &self.compilation_target, - compilation_target.clone(), - key.clone(), - ) - { + match self.wam.indices.get_local_predicate_skeleton_mut( + self.compilation_target.clone(), + compilation_target.clone(), + self.listing_src_file_name(), + key.clone(), + ) { Some(skeleton) => { self.retraction_info.push_record( RetractionRecord::SkeletonLocalClauseClausePopBack( @@ -1599,7 +1560,7 @@ impl<'a> LoadState<'a> { skeleton.clause_clause_locs.push_back(code_len); } None => { - let mut skeleton = PredicateSkeleton::new(); + let mut skeleton = LocalPredicateSkeleton::new(); skeleton.clause_clause_locs.push_back(code_len); self.add_local_extensible_predicate( @@ -1624,21 +1585,19 @@ impl<'a> LoadState<'a> { .indices .get_predicate_skeleton_mut(&compilation_target, &key) { - Some(skeleton) if !skeleton.clauses.is_empty() => { - CodeGenSettings { - global_clock_tick: if skeleton.is_dynamic { - Some(self.wam.machine_st.global_clock) - } else { - None - }, - is_extensible: true, - non_counted_bt, - } + Some(skeleton) if !skeleton.clauses.is_empty() => CodeGenSettings { + global_clock_tick: if skeleton.core.is_dynamic { + Some(self.wam.machine_st.global_clock) + } else { + None + }, + is_extensible: true, + non_counted_bt, }, skeleton_opt => { let settings = CodeGenSettings { global_clock_tick: if let Some(skeleton) = skeleton_opt { - if skeleton.is_dynamic { + if skeleton.core.is_dynamic { Some(self.wam.machine_st.global_clock) } else { None @@ -1672,7 +1631,7 @@ impl<'a> LoadState<'a> { .get_predicate_skeleton_mut(&compilation_target, &key) { Some(skeleton) if !skeleton.clauses.is_empty() => skeleton, - _ => unreachable!() + _ => unreachable!(), }; match append_or_prepend { @@ -1680,7 +1639,7 @@ impl<'a> LoadState<'a> { let clause_index_info = standalone_skeleton.clauses.pop_back().unwrap(); skeleton.clauses.push_back(clause_index_info); - skeleton.clause_clause_locs.push_back(code_len); + skeleton.core.clause_clause_locs.push_back(code_len); self.retraction_info .push_record(RetractionRecord::SkeletonClausePopBack( @@ -1696,33 +1655,10 @@ impl<'a> LoadState<'a> { self.wam.machine_st.global_clock, ); - self.push_back_to_local_predicate_skeleton( - &compilation_target, - &key, - code_len, - ); - - if let Some(filename) = self.listing_src_file_name() { - if let CompilationTarget::User = &compilation_target { - let compilation_target = mem::replace( - &mut self.compilation_target, - CompilationTarget::Module(filename), - ); - - self.push_back_to_local_predicate_skeleton( - &CompilationTarget::User, - &key, - code_len, - ); - - self.compilation_target = compilation_target; - } - } + self.push_back_to_local_predicate_skeleton(&compilation_target, &key, code_len); - let code_index = self.get_or_insert_code_index( - key.clone(), - compilation_target.clone(), - ); + let code_index = + self.get_or_insert_code_index(key.clone(), compilation_target.clone()); if let Some(new_code_ptr) = result { set_code_index( @@ -1740,8 +1676,8 @@ impl<'a> LoadState<'a> { let clause_index_info = standalone_skeleton.clauses.pop_back().unwrap(); skeleton.clauses.push_front(clause_index_info); - skeleton.clause_clause_locs.push_front(code_len); - skeleton.clause_assert_margin += 1; + skeleton.core.clause_clause_locs.push_front(code_len); + skeleton.core.clause_assert_margin += 1; self.retraction_info .push_record(RetractionRecord::SkeletonClausePopFront( @@ -1759,33 +1695,10 @@ impl<'a> LoadState<'a> { self.wam.machine_st.global_clock, ); - if let Some(filename) = self.listing_src_file_name() { - if let CompilationTarget::User = &compilation_target { - let compilation_target = mem::replace( - &mut self.compilation_target, - CompilationTarget::Module(filename), - ); - - self.push_front_to_local_predicate_skeleton( - &CompilationTarget::User, - &key, - code_len, - ); - - self.compilation_target = compilation_target; - } - } - - self.push_front_to_local_predicate_skeleton( - &compilation_target, - &key, - code_len, - ); + self.push_front_to_local_predicate_skeleton(&compilation_target, &key, code_len); - let code_index = self.get_or_insert_code_index( - key.clone(), - compilation_target.clone(), - ); + let code_index = + self.get_or_insert_code_index(key.clone(), compilation_target.clone()); set_code_index( &mut self.retraction_info, @@ -1816,24 +1729,20 @@ impl<'a> LoadState<'a> { .opt_arg_index_key .switch_on_term_loc() { - Some(index_loc) => { - find_inner_choice_instr( - &self.wam.code_repo.code, - skeleton.clauses[target_pos].clause_start, - index_loc, - ) - } - None => { - skeleton.clauses[target_pos].clause_start - } + Some(index_loc) => find_inner_choice_instr( + &self.wam.code_repo.code, + skeleton.clauses[target_pos].clause_start, + index_loc, + ), + None => skeleton.clauses[target_pos].clause_start, }; match &mut self.wam.code_repo.code[clause_loc] { - Line::Choice(ChoiceInstruction::DynamicElse(_, ref mut d, _)) | - Line::Choice(ChoiceInstruction::DynamicInternalElse(_, ref mut d, _)) => { + Line::Choice(ChoiceInstruction::DynamicElse(_, ref mut d, _)) + | Line::Choice(ChoiceInstruction::DynamicInternalElse(_, ref mut d, _)) => { *d = Death::Finite(self.wam.machine_st.global_clock); } - _ => unreachable!() + _ => unreachable!(), } delete_from_skeleton( @@ -1846,10 +1755,8 @@ impl<'a> LoadState<'a> { } pub(super) fn retract_clause(&mut self, key: PredicateKey, target_pos: usize) -> usize { - let code_index = self.get_or_insert_code_index( - key.clone(), - self.compilation_target.clone(), - ); + let code_index = + self.get_or_insert_code_index(key.clone(), self.compilation_target.clone()); let skeleton = match self .wam @@ -1924,11 +1831,19 @@ impl<'a> LoadState<'a> { skeleton.clauses[target_pos + 1].clause_start = skeleton.clauses[target_pos].clause_start; - return delete_from_skeleton( - self.compilation_target.clone(), + let index_ptr_opt = if target_pos == 0 { + Some(IndexPtr::Index(clause_loc)) + } else { + None + }; + + return finalize_retract( key, + self.compilation_target.clone(), skeleton, + code_index, target_pos, + index_ptr_opt, &mut self.retraction_info, ); } @@ -2179,18 +2094,16 @@ impl<'a, TS: TermStream> Loader<'a, TS> { .get_predicate_skeleton_mut(&compilation_target, &key) { Some(skeleton) if append_or_prepend.is_append() => { - let tail_num = skeleton.clause_clause_locs.len() - num_clause_predicates; - skeleton.clause_clause_locs[tail_num ..] - .iter() - .cloned() - .collect() - } - Some(skeleton) => { - skeleton.clause_clause_locs[0 .. num_clause_predicates] + let tail_num = skeleton.core.clause_clause_locs.len() - num_clause_predicates; + skeleton.core.clause_clause_locs[tail_num..] .iter() .cloned() .collect() } + Some(skeleton) => skeleton.core.clause_clause_locs[0..num_clause_predicates] + .iter() + .cloned() + .collect(), None => { unreachable!() } @@ -2201,21 +2114,21 @@ impl<'a, TS: TermStream> Loader<'a, TS> { &(clause_name!("$clause"), 2), ) { Some(skeleton) if append_or_prepend.is_append() => { - for _ in 0 .. num_clause_predicates { - skeleton.clause_clause_locs.pop_back(); + for _ in 0..num_clause_predicates { + skeleton.core.clause_clause_locs.pop_back(); } for loc in locs_vec { - skeleton.clause_clause_locs.push_back(loc); + skeleton.core.clause_clause_locs.push_back(loc); } } Some(skeleton) => { - for _ in 0 .. num_clause_predicates { - skeleton.clause_clause_locs.pop_front(); + for _ in 0..num_clause_predicates { + skeleton.core.clause_clause_locs.pop_front(); } for loc in locs_vec.into_iter().rev() { - skeleton.clause_clause_locs.push_front(loc); + skeleton.core.clause_clause_locs.push_front(loc); } } None => { @@ -2249,8 +2162,9 @@ impl<'a, TS: TermStream> Loader<'a, TS> { .wam .indices .get_local_predicate_skeleton( - &self.load_state.compilation_target, + self.load_state.compilation_target.clone(), self.predicates.compilation_target.clone(), + self.load_state.listing_src_file_name(), key.clone(), ) .map(|skeleton| skeleton.predicate_info()) @@ -2271,7 +2185,7 @@ impl<'a, TS: TermStream> Loader<'a, TS> { let non_counted_bt = self.non_counted_bt_preds.contains(&key); if do_incremental_compile { - for term in self.predicates.predicates.drain(0 ..) { + for term in self.predicates.predicates.drain(0..) { self.load_state.incremental_compile_clause( key.clone(), term, @@ -2284,18 +2198,18 @@ impl<'a, TS: TermStream> Loader<'a, TS> { if self.load_state.compilation_target != self.predicates.compilation_target { if !local_predicate_info.is_extensible { if predicate_info.is_multifile { - println!("Warning: overwriting multifile predicate {}:{}/{} because \ + println!( + "Warning: overwriting multifile predicate {}:{}/{} because \ it was not locally declared multifile.", - self.predicates.compilation_target, key.0, key.1); + self.predicates.compilation_target, key.0, key.1 + ); } - if let Some(skeleton) = self.load_state + if let Some(skeleton) = self + .load_state .wam .indices - .remove_predicate_skeleton( - &self.predicates.compilation_target, - &key, - ) + .remove_predicate_skeleton(&self.predicates.compilation_target, &key) { if predicate_info.is_dynamic { let clause_clause_compilation_target = @@ -2303,15 +2217,13 @@ impl<'a, TS: TermStream> Loader<'a, TS> { CompilationTarget::User => { CompilationTarget::Module(clause_name!("builtins")) } - module => { - module.clone() - } + module => module.clone(), }; self.load_state.retract_local_clauses_by_locs( clause_clause_compilation_target, (clause_name!("$clause"), 2), - (0 .. skeleton.clauses.len()).map(Some).collect(), + (0..skeleton.clauses.len()).map(Some).collect(), false, // the builtin M:'$clause'/2 is never dynamic. ); @@ -2339,28 +2251,25 @@ impl<'a, TS: TermStream> Loader<'a, TS> { non_counted_bt, }; - let code_index - = self.load_state.compile(key.clone(), &mut self.predicates, settings)?; + let code_index = + self.load_state + .compile(key.clone(), &mut self.predicates, settings)?; if let Some(filename) = self.load_state.listing_src_file_name() { - if let CompilationTarget::User = &self.predicates.compilation_target { - match self.load_state.wam.indices.modules.get_mut(&filename) { - Some(ref mut module) => { - let index_ptr = code_index.get(); - let code_index = module.code_dir.entry(key.clone()) - .or_insert(code_index); - - set_code_index( - &mut self.load_state.retraction_info, - &CompilationTarget::Module(filename), - key.clone(), - &code_index, - index_ptr, - ); - } - None => { - } + match self.load_state.wam.indices.modules.get_mut(&filename) { + Some(ref mut module) => { + let index_ptr = code_index.get(); + let code_index = module.code_dir.entry(key.clone()).or_insert(code_index); + + set_code_index( + &mut self.load_state.retraction_info, + &CompilationTarget::Module(filename), + key.clone(), + &code_index, + index_ptr, + ); } + None => {} } } } @@ -2368,9 +2277,7 @@ impl<'a, TS: TermStream> Loader<'a, TS> { if predicate_info.is_dynamic { self.load_state.wam.machine_st.global_clock += 1; - let clauses_vec: Vec<_> = self.clause_clauses - .drain(0 .. predicates_len) - .collect(); + let clauses_vec: Vec<_> = self.clause_clauses.drain(0..predicates_len).collect(); self.compile_clause_clauses( key, diff --git a/src/machine/load_state.rs b/src/machine/load_state.rs index e59d4a2a..11208158 100644 --- a/src/machine/load_state.rs +++ b/src/machine/load_state.rs @@ -1,7 +1,7 @@ use crate::machine::machine_indices::*; use crate::machine::preprocessor::*; -use crate::machine::*; use crate::machine::term_stream::*; +use crate::machine::*; use prolog_parser::clause_name; @@ -349,15 +349,15 @@ impl<'a> LoadState<'a> { .indices .get_predicate_skeleton(&compilation_target, &key) .map(|skeleton| { - (clause_locs - .iter() - .map(|clause_clause_loc| { - skeleton.target_pos_of_clause_clause_loc( - *clause_clause_loc, - ) - }) - .collect(), - skeleton.is_dynamic) + ( + clause_locs + .iter() + .map(|clause_clause_loc| { + skeleton.target_pos_of_clause_clause_loc(*clause_clause_loc) + }) + .collect(), + skeleton.core.is_dynamic, + ) }); if let Some((clause_target_poses, is_dynamic)) = result_opt { @@ -377,10 +377,7 @@ impl<'a> LoadState<'a> { mut clause_target_poses: Vec>, is_dynamic: bool, ) { - let old_compilation_target = mem::replace( - &mut self.compilation_target, - compilation_target, - ); + let old_compilation_target = mem::replace(&mut self.compilation_target, compilation_target); while let Some(target_pos_opt) = clause_target_poses.pop() { match target_pos_opt { @@ -408,15 +405,12 @@ impl<'a> LoadState<'a> { ) { let key = (clause_name!("$clause"), 2); - match self - .wam - .indices - .get_local_predicate_skeleton_mut( - &self.compilation_target, - clause_clause_compilation_target.clone(), - key.clone(), - ) - { + match self.wam.indices.get_local_predicate_skeleton_mut( + self.compilation_target.clone(), + clause_clause_compilation_target.clone(), + self.listing_src_file_name(), + key.clone(), + ) { Some(skeleton) => { self.retraction_info.push_record( RetractionRecord::RemovedLocalSkeletonClauseLocations( @@ -436,11 +430,7 @@ impl<'a> LoadState<'a> { } }; - self.retract_local_clauses( - clause_clause_compilation_target, - key, - &clause_locs, - ); + self.retract_local_clauses(clause_clause_compilation_target, key, &clause_locs); } pub(super) fn try_term_to_tl( @@ -448,11 +438,7 @@ impl<'a> LoadState<'a> { term: Term, preprocessor: &mut Preprocessor, ) -> Result { - let tl = preprocessor.try_term_to_tl( - self, - term, - CutContext::BlocksCuts, - )?; + let tl = preprocessor.try_term_to_tl(self, term, CutContext::BlocksCuts)?; Ok(match tl { TopLevel::Fact(fact) => PredicateClause::Fact(fact), @@ -485,11 +471,12 @@ impl<'a> LoadState<'a> { if code_index.get() != IndexPtr::Undefined { let old_index_ptr = code_index.replace(IndexPtr::Undefined); - self.retraction_info.push_record( - RetractionRecord::ReplacedModulePredicate( - module_name.clone(), key.clone(), old_index_ptr, - ), - ); + self.retraction_info + .push_record(RetractionRecord::ReplacedModulePredicate( + module_name.clone(), + key.clone(), + old_index_ptr, + )); } } @@ -515,25 +502,24 @@ impl<'a> LoadState<'a> { ModuleExport::PredicateKey(ref key) => { match (removed_module.code_dir.get(key), code_dir.get(key)) { (Some(module_code_index), Some(target_code_index)) - if module_code_index.get() == target_code_index.get() => { - let old_index_ptr = target_code_index.replace(IndexPtr::Undefined); + if module_code_index.get() == target_code_index.get() => + { + let old_index_ptr = target_code_index.replace(IndexPtr::Undefined); - retraction_info.push_record( - predicate_retractor(key.clone(), old_index_ptr), - ); - } + retraction_info + .push_record(predicate_retractor(key.clone(), old_index_ptr)); + } _ => {} } } ModuleExport::OpDecl(op_decl) => { - let op_dir_value_opt = op_dir.remove(&(op_decl.name.clone(), op_decl.fixity())); + let op_dir_value_opt = + op_dir.remove(&(op_decl.name.clone(), op_decl.fixity())); if let Some(op_dir_value) = op_dir_value_opt { let (prec, spec) = op_dir_value.shared_op_desc().get(); - retraction_info.push_record( - op_retractor(op_decl.clone(), prec, spec), - ); + retraction_info.push_record(op_retractor(op_decl.clone(), prec, spec)); } } } @@ -552,33 +538,30 @@ impl<'a> LoadState<'a> { ); } CompilationTarget::Module(ref target_module_name) - if target_module_name.as_str() != module_name.as_str() => { - let predicate_retractor = |key, index_ptr| { - RetractionRecord::ReplacedModulePredicate( - module_name.clone(), key, index_ptr, - ) - }; - - let op_retractor = |op_decl, prec, spec| { - RetractionRecord::ReplacedModuleOp( - module_name.clone(), op_decl, prec, spec, - ) - }; - - if let Some(module) = self.wam.indices.modules.get_mut(target_module_name) { - remove_module_exports( - &removed_module, - &mut module.code_dir, - &mut module.op_dir, - &mut self.retraction_info, - predicate_retractor, - op_retractor, - ); - } else { - unreachable!() - } + if target_module_name.as_str() != module_name.as_str() => + { + let predicate_retractor = |key, index_ptr| { + RetractionRecord::ReplacedModulePredicate(module_name.clone(), key, index_ptr) + }; + + let op_retractor = |op_decl, prec, spec| { + RetractionRecord::ReplacedModuleOp(module_name.clone(), op_decl, prec, spec) + }; + + if let Some(module) = self.wam.indices.modules.get_mut(target_module_name) { + remove_module_exports( + &removed_module, + &mut module.code_dir, + &mut module.op_dir, + &mut self.retraction_info, + predicate_retractor, + op_retractor, + ); + } else { + unreachable!() } - CompilationTarget::Module(_) => {}, + } + CompilationTarget::Module(_) => {} }; self.wam.indices.modules.insert(module_name, removed_module); @@ -599,13 +582,11 @@ impl<'a> LoadState<'a> { self.add_dynamically_generated_module(&module_name); match self.wam.indices.modules.get_mut(&module_name) { - Some(ref mut module) => { - module - .code_dir - .entry(key) - .or_insert_with(|| CodeIndex::new(IndexPtr::Undefined)) - .clone() - } + Some(ref mut module) => module + .code_dir + .entry(key) + .or_insert_with(|| CodeIndex::new(IndexPtr::Undefined)) + .clone(), None => { unreachable!() } @@ -665,18 +646,14 @@ impl<'a> LoadState<'a> { .extensible_predicates .insert(key.clone(), skeleton); - let record = RetractionRecord::AddedExtensiblePredicate( - CompilationTarget::User, - key, - ); + let record = + RetractionRecord::AddedExtensiblePredicate(CompilationTarget::User, key); self.retraction_info.push_record(record); } CompilationTarget::Module(module_name) => { if let Some(module) = self.wam.indices.modules.get_mut(&module_name) { - module - .extensible_predicates - .insert(key.clone(), skeleton); + module.extensible_predicates.insert(key.clone(), skeleton); let record = RetractionRecord::AddedExtensiblePredicate( CompilationTarget::Module(module_name), @@ -696,9 +673,14 @@ impl<'a> LoadState<'a> { &mut self, local_compilation_target: CompilationTarget, key: PredicateKey, - skeleton: PredicateSkeleton, + skeleton: LocalPredicateSkeleton, ) { - match self.compilation_target.clone() { + let src_compilation_target = match self.listing_src_file_name() { + Some(filename) => CompilationTarget::Module(filename), + None => self.compilation_target.clone(), + }; + + match src_compilation_target { CompilationTarget::User => { self.wam .indices @@ -725,8 +707,7 @@ impl<'a> LoadState<'a> { Some(ref mut module) => { op_decl.insert_into_op_dir(&mut module.op_dir); } - None => { - } + None => {} } } @@ -911,11 +892,16 @@ impl<'a> LoadState<'a> { code_dir, op_dir, meta_predicates, - ).unwrap(); + ) + .unwrap(); } } - pub(crate) fn reset_in_situ_module(&mut self, module_decl: ModuleDecl, listing_src: &ListingSource) { + pub(crate) fn reset_in_situ_module( + &mut self, + module_decl: ModuleDecl, + listing_src: &ListingSource, + ) { let module_name = module_decl.name.clone(); self.remove_module_exports(module_name.clone()); @@ -940,8 +926,8 @@ impl<'a> LoadState<'a> { let is_dynamic = self .wam .indices - .get_predicate_skeleton(&compilation_target, &key) - .map(|skeleton| skeleton.is_dynamic) + .get_predicate_skeleton(compilation_target, key) + .map(|skeleton| skeleton.core.is_dynamic) .unwrap_or(false); if is_dynamic { @@ -949,9 +935,7 @@ impl<'a> LoadState<'a> { CompilationTarget::User => { CompilationTarget::Module(clause_name!("builtins")) } - module => { - module.clone() - } + module => module.clone(), }; self.retract_local_clause_clauses( @@ -959,37 +943,14 @@ impl<'a> LoadState<'a> { &skeleton.clause_clause_locs, ); } - - if &self.compilation_target == compilation_target { - if let CompilationTarget::User = &self.compilation_target { - match (key.0.as_str(), key.1) { - ("term_expansion", 2) | ("goal_expansion", 2) => { - continue; - } - _ => {} - } - } - - let skeleton_opt = self.wam.indices.remove_predicate_skeleton( - compilation_target, - &key, - ); - - if let Some(skeleton) = skeleton_opt { - self.retraction_info.push_record(RetractionRecord::RemovedSkeleton( - compilation_target.clone(), - key.clone(), - skeleton, - )); - } - } } - self.retraction_info.push_record(RetractionRecord::ReplacedModule( - old_module_decl, - listing_src.clone(), - local_extensible_predicates, - )); + self.retraction_info + .push_record(RetractionRecord::ReplacedModule( + old_module_decl, + listing_src.clone(), + local_extensible_predicates, + )); } None => {} } diff --git a/src/machine/loader.rs b/src/machine/loader.rs index ea45448a..3a5d4092 100644 --- a/src/machine/loader.rs +++ b/src/machine/loader.rs @@ -198,14 +198,14 @@ impl<'a> Drop for LoadState<'a> { .extensible_predicates .get_mut(&key) .map(|skeleton| { - skeleton.is_discontiguous = false; + skeleton.core.is_discontiguous = false; }); } CompilationTarget::Module(module_name) => { match self.wam.indices.modules.get_mut(&module_name) { Some(ref mut module) => { module.extensible_predicates.get_mut(&key).map(|skeleton| { - skeleton.is_discontiguous = false; + skeleton.core.is_discontiguous = false; }); } None => {} @@ -221,14 +221,14 @@ impl<'a> Drop for LoadState<'a> { .extensible_predicates .get_mut(&key) .map(|skeleton| { - skeleton.is_dynamic = false; + skeleton.core.is_dynamic = false; }); } CompilationTarget::Module(module_name) => { match self.wam.indices.modules.get_mut(&module_name) { Some(ref mut module) => { module.extensible_predicates.get_mut(&key).map(|skeleton| { - skeleton.is_dynamic = false; + skeleton.core.is_dynamic = false; }); } None => {} @@ -244,14 +244,14 @@ impl<'a> Drop for LoadState<'a> { .extensible_predicates .get_mut(&key) .map(|skeleton| { - skeleton.is_multifile = false; + skeleton.core.is_multifile = false; }); } CompilationTarget::Module(module_name) => { match self.wam.indices.modules.get_mut(&module_name) { Some(ref mut module) => { module.extensible_predicates.get_mut(&key).map(|skeleton| { - skeleton.is_multifile = false; + skeleton.core.is_multifile = false; }); } None => {} @@ -429,7 +429,7 @@ impl<'a> Drop for LoadState<'a> { { Some(skeleton) => { skeleton.clauses.pop_back(); - skeleton.clause_clause_locs.pop_back(); + skeleton.core.clause_clause_locs.pop_back(); } None => {} } @@ -442,8 +442,8 @@ impl<'a> Drop for LoadState<'a> { { Some(skeleton) => { skeleton.clauses.pop_front(); - skeleton.clause_clause_locs.pop_front(); - skeleton.clause_assert_margin -= 1; + skeleton.core.clause_clause_locs.pop_front(); + skeleton.core.clause_assert_margin -= 1; } None => {} } @@ -454,8 +454,9 @@ impl<'a> Drop for LoadState<'a> { key, ) => { match self.wam.indices.get_local_predicate_skeleton_mut( - &src_compilation_target, + src_compilation_target, local_compilation_target, + self.listing_src_file_name(), key, ) { Some(skeleton) => { @@ -470,8 +471,9 @@ impl<'a> Drop for LoadState<'a> { key, ) => { match self.wam.indices.get_local_predicate_skeleton_mut( - &src_compilation_target, + src_compilation_target, local_compilation_target, + self.listing_src_file_name(), key, ) { Some(skeleton) => { @@ -487,8 +489,9 @@ impl<'a> Drop for LoadState<'a> { len, ) => { match self.wam.indices.get_local_predicate_skeleton_mut( - &src_compilation_target, + src_compilation_target, local_compilation_target, + self.listing_src_file_name(), key, ) { Some(skeleton) => { @@ -505,7 +508,7 @@ impl<'a> Drop for LoadState<'a> { { Some(skeleton) => { skeleton.clauses.truncate_back(len); - skeleton.clause_clause_locs.truncate_back(len); + skeleton.core.clause_clause_locs.truncate_back(len); } None => {} } @@ -541,6 +544,7 @@ impl<'a> Drop for LoadState<'a> { { Some(skeleton) => { skeleton + .core .clause_clause_locs .insert(target_pos, clause_clause_loc); skeleton.clauses.insert(target_pos, clause_index_info); @@ -558,8 +562,9 @@ impl<'a> Drop for LoadState<'a> { clause_locs, ) => { match self.wam.indices.get_local_predicate_skeleton_mut( - &compilation_target, + compilation_target, local_compilation_target, + self.listing_src_file_name(), key, ) { Some(skeleton) => skeleton.clause_clause_locs = clause_locs, @@ -581,10 +586,14 @@ impl<'a> Drop for LoadState<'a> { RetractionRecord::ReplacedDynamicElseOffset(instr_loc, next) => { match &mut self.wam.code_repo.code[instr_loc] { Line::Choice(ChoiceInstruction::DynamicElse( - _, _, NextOrFail::Next(ref mut o), - )) | - Line::Choice(ChoiceInstruction::DynamicInternalElse( - _, _, NextOrFail::Next(ref mut o), + _, + _, + NextOrFail::Next(ref mut o), + )) + | Line::Choice(ChoiceInstruction::DynamicInternalElse( + _, + _, + NextOrFail::Next(ref mut o), )) => { *o = next; } @@ -594,10 +603,14 @@ impl<'a> Drop for LoadState<'a> { RetractionRecord::AppendedNextOrFail(instr_loc, fail) => { match &mut self.wam.code_repo.code[instr_loc] { Line::Choice(ChoiceInstruction::DynamicElse( - _, _, ref mut next_or_fail, - )) | - Line::Choice(ChoiceInstruction::DynamicInternalElse( - _, _, ref mut next_or_fail, + _, + _, + ref mut next_or_fail, + )) + | Line::Choice(ChoiceInstruction::DynamicInternalElse( + _, + _, + ref mut next_or_fail, )) => { *next_or_fail = fail; } @@ -731,10 +744,9 @@ impl<'a, TS: TermStream> Loader<'a, TS> { } let term = match term { - Term::Clause(_, name, terms, _) - if name.as_str() == ":-" && terms.len() == 1 => { - return Ok(Some(setup_declaration(&self.load_state, terms)?)); - } + Term::Clause(_, name, terms, _) if name.as_str() == ":-" && terms.len() == 1 => { + return Ok(Some(setup_declaration(&self.load_state, terms)?)); + } term => term, }; @@ -905,7 +917,7 @@ impl<'a, TS: TermStream> Loader<'a, TS> { compilation_target: CompilationTarget, name: ClauseName, arity: usize, - flag_accessor: impl Fn(&mut PredicateSkeleton) -> &mut bool, + flag_accessor: impl Fn(&mut LocalPredicateSkeleton) -> &mut bool, retraction_fn: impl Fn(CompilationTarget, PredicateKey) -> RetractionRecord, ) -> Result<(), SessionError> { let key = (name, arity); @@ -920,9 +932,9 @@ impl<'a, TS: TermStream> Loader<'a, TS> { .extensible_predicates .get_mut(&key) { - Some(ref mut skeleton) => { - if !*flag_accessor(skeleton) { - *flag_accessor(skeleton) = true; + Some(skeleton) => { + if !*flag_accessor(&mut skeleton.core) { + *flag_accessor(&mut skeleton.core) = true; self.load_state.retraction_info.push_record(retraction_fn( compilation_target.clone(), @@ -933,7 +945,7 @@ impl<'a, TS: TermStream> Loader<'a, TS> { None => { if self.load_state.compilation_target == compilation_target { let mut skeleton = PredicateSkeleton::new(); - *flag_accessor(&mut skeleton) = true; + *flag_accessor(&mut skeleton.core) = true; self.load_state.add_extensible_predicate( key.clone(), @@ -950,8 +962,8 @@ impl<'a, TS: TermStream> Loader<'a, TS> { match self.load_state.wam.indices.modules.get_mut(module_name) { Some(ref mut module) => match module.extensible_predicates.get_mut(&key) { Some(ref mut skeleton) => { - if !*flag_accessor(skeleton) { - *flag_accessor(skeleton) = true; + if !*flag_accessor(&mut skeleton.core) { + *flag_accessor(&mut skeleton.core) = true; self.load_state.retraction_info.push_record(retraction_fn( compilation_target.clone(), @@ -962,7 +974,7 @@ impl<'a, TS: TermStream> Loader<'a, TS> { None => { if self.load_state.compilation_target == compilation_target { let mut skeleton = PredicateSkeleton::new(); - *flag_accessor(&mut skeleton) = true; + *flag_accessor(&mut skeleton.core) = true; self.load_state.add_extensible_predicate( key.clone(), @@ -979,7 +991,7 @@ impl<'a, TS: TermStream> Loader<'a, TS> { .add_dynamically_generated_module(module_name); let mut skeleton = PredicateSkeleton::new(); - *flag_accessor(&mut skeleton) = true; + *flag_accessor(&mut skeleton.core) = true; self.load_state.add_extensible_predicate( key.clone(), @@ -998,16 +1010,19 @@ impl<'a, TS: TermStream> Loader<'a, TS> { .load_state .wam .indices - .local_extensible_predicates - .get_mut(&(compilation_target.clone(), key.clone())) - { - Some(ref mut skeleton) => { + .get_local_predicate_skeleton_mut( + self.load_state.compilation_target.clone(), + compilation_target.clone(), + self.load_state.listing_src_file_name(), + key.clone(), + ) { + Some(skeleton) => { if !*flag_accessor(skeleton) { *flag_accessor(skeleton) = true; } } None => { - let mut skeleton = PredicateSkeleton::new(); + let mut skeleton = LocalPredicateSkeleton::new(); *flag_accessor(&mut skeleton) = true; self.load_state.add_local_extensible_predicate( @@ -1020,17 +1035,17 @@ impl<'a, TS: TermStream> Loader<'a, TS> { } CompilationTarget::Module(ref module_name) => { match self.load_state.wam.indices.modules.get_mut(module_name) { - Some(ref mut module) => match module + Some(module) => match module .local_extensible_predicates .get_mut(&(compilation_target.clone(), key.clone())) { - Some(ref mut skeleton) => { + Some(skeleton) => { if !*flag_accessor(skeleton) { *flag_accessor(skeleton) = true; } } None => { - let mut skeleton = PredicateSkeleton::new(); + let mut skeleton = LocalPredicateSkeleton::new(); *flag_accessor(&mut skeleton) = true; self.load_state.add_local_extensible_predicate( @@ -1044,7 +1059,7 @@ impl<'a, TS: TermStream> Loader<'a, TS> { self.load_state .add_dynamically_generated_module(module_name); - let mut skeleton = PredicateSkeleton::new(); + let mut skeleton = LocalPredicateSkeleton::new(); *flag_accessor(&mut skeleton) = true; self.load_state.add_local_extensible_predicate( @@ -1139,7 +1154,7 @@ impl<'a, TS: TermStream> Loader<'a, TS> { &self.predicates.compilation_target, &(predicate_name, arity), ) - .map(|skeleton| skeleton.is_dynamic) + .map(|skeleton| skeleton.core.is_dynamic) .unwrap_or(false); if is_dynamic { @@ -1156,8 +1171,9 @@ impl<'a, TS: TermStream> Loader<'a, TS> { .wam .indices .get_local_predicate_skeleton_mut( - &self.load_state.compilation_target, + self.load_state.compilation_target.clone(), self.predicates.compilation_target.clone(), + self.load_state.listing_src_file_name(), key.clone(), ) { Some(skeleton) if !skeleton.clause_clause_locs.is_empty() => { @@ -1440,9 +1456,20 @@ impl Machine { let module_name = module_decl.name.clone(); - if !loader.load_state.wam.indices.modules.contains_key(&module_decl.name) { + if !loader + .load_state + .wam + .indices + .modules + .contains_key(&module_decl.name) + { let module = Module::new_in_situ(module_decl); - loader.load_state.wam.indices.modules.insert(module_name, module); + loader + .load_state + .wam + .indices + .modules + .insert(module_name, module); } else { loader.load_state.reset_in_situ_module( module_decl.clone(), @@ -1451,15 +1478,14 @@ impl Machine { match loader.load_state.wam.indices.modules.get_mut(&module_name) { Some(module) => { - for (key, value) in module.op_dir.drain(0 ..) { + for (key, value) in module.op_dir.drain(0..) { let (prec, spec) = value.shared_op_desc().get(); let mut op_decl = OpDecl::new(prec, spec, key.0); op_decl.remove(&mut loader.load_state.wam.indices.op_dir); } } - None => { - } + None => {} } } } @@ -1638,7 +1664,8 @@ impl Machine { .push(HeapCellValue::Atom(path_atom, None)), ); - self.machine_st.unify(path_addr, self.machine_st[temp_v!(1)]); + self.machine_st + .unify(path_addr, self.machine_st[temp_v!(1)]); } else { self.machine_st.fail = true; } @@ -1658,7 +1685,8 @@ impl Machine { .push(HeapCellValue::Atom(file_name_atom, None)), ); - self.machine_st.unify(file_name_addr, self.machine_st[temp_v!(1)]); + self.machine_st + .unify(file_name_addr, self.machine_st[temp_v!(1)]); return; } _ => { @@ -1684,7 +1712,8 @@ impl Machine { .push(HeapCellValue::Atom(directory_atom, None)), ); - self.machine_st.unify(directory_addr, self.machine_st[temp_v!(1)]); + self.machine_st + .unify(directory_addr, self.machine_st[temp_v!(1)]); return; } } @@ -1700,7 +1729,8 @@ impl Machine { .push(HeapCellValue::Atom(load_context.module.clone(), None)), ); - self.machine_st.unify(module_name_addr, self.machine_st[temp_v!(1)]); + self.machine_st + .unify(module_name_addr, self.machine_st[temp_v!(1)]); } else { self.machine_st.fail = true; } @@ -1714,7 +1744,8 @@ impl Machine { .push(HeapCellValue::Stream(load_context.stream.clone())), ); - self.machine_st.unify(stream_addr, self.machine_st[temp_v!(1)]); + self.machine_st + .unify(stream_addr, self.machine_st[temp_v!(1)]); } else { self.machine_st.fail = true; } @@ -1752,11 +1783,7 @@ impl Machine { ); // if a new predicate was just created, make it dynamic. - loader.add_dynamic_predicate( - compilation_target.clone(), - key.0.clone(), - key.1, - )?; + loader.add_dynamic_predicate(compilation_target.clone(), key.0.clone(), key.1)?; loader.load_state.incremental_compile_clause( key.clone(), @@ -1833,6 +1860,7 @@ impl Machine { ) .map(|clause_clause_skeleton| { skeleton + .core .clause_clause_locs .iter() .map(|clause_clause_loc| { @@ -2060,7 +2088,8 @@ impl Machine { .heap .push(HeapCellValue::Addr(Addr::HeapCell(list_loc))); - self.machine_st.unify(Addr::HeapCell(heap_loc), self.machine_st[temp_v!(4)]); + self.machine_st + .unify(Addr::HeapCell(heap_loc), self.machine_st[temp_v!(4)]); } None => { self.machine_st.fail = true; @@ -2089,7 +2118,7 @@ impl Machine { .get_predicate_skeleton(&compilation_target, &key) { Some(skeleton) => { - self.machine_st.fail = !skeleton.is_dynamic; + self.machine_st.fail = !skeleton.core.is_dynamic; } None => { self.machine_st.fail = true; @@ -2118,7 +2147,7 @@ impl Machine { .get_predicate_skeleton(&compilation_target, &key) { Some(skeleton) => { - self.machine_st.fail = !skeleton.is_multifile; + self.machine_st.fail = !skeleton.core.is_multifile; } None => { self.machine_st.fail = true; @@ -2147,7 +2176,7 @@ impl Machine { .get_predicate_skeleton(&compilation_target, &key) { Some(skeleton) => { - self.machine_st.fail = !skeleton.is_discontiguous; + self.machine_st.fail = !skeleton.core.is_discontiguous; } None => { self.machine_st.fail = true; diff --git a/src/machine/machine_indices.rs b/src/machine/machine_indices.rs index f01cbeee..e66a8713 100644 --- a/src/machine/machine_indices.rs +++ b/src/machine/machine_indices.rs @@ -673,7 +673,7 @@ pub(crate) type MetaPredicateDir = IndexMap>; pub(crate) type ExtensiblePredicates = IndexMap; pub(crate) type LocalExtensiblePredicates = - IndexMap<(CompilationTarget, PredicateKey), PredicateSkeleton>; + IndexMap<(CompilationTarget, PredicateKey), LocalPredicateSkeleton>; #[derive(Debug)] pub(crate) struct IndexStore { @@ -702,7 +702,7 @@ impl IndexStore { key: &PredicateKey, ) -> Option<&mut PredicateSkeleton> { match (key.0.as_str(), key.1) { -// ("term_expansion", 2) => self.extensible_predicates.get_mut(key), + // ("term_expansion", 2) => self.extensible_predicates.get_mut(key), _ => match compilation_target { CompilationTarget::User => self.extensible_predicates.get_mut(key), CompilationTarget::Module(ref module_name) => { @@ -716,12 +716,34 @@ impl IndexStore { } } + pub(crate) fn get_predicate_skeleton( + &self, + compilation_target: &CompilationTarget, + key: &PredicateKey, + ) -> Option<&PredicateSkeleton> { + match compilation_target { + CompilationTarget::User => self.extensible_predicates.get(key), + CompilationTarget::Module(ref module_name) => { + if let Some(module) = self.modules.get(module_name) { + module.extensible_predicates.get(key) + } else { + None + } + } + } + } + pub(crate) fn get_local_predicate_skeleton_mut( &mut self, - src_compilation_target: &CompilationTarget, + mut src_compilation_target: CompilationTarget, local_compilation_target: CompilationTarget, + listing_src_file_name: Option, key: PredicateKey, - ) -> Option<&mut PredicateSkeleton> { + ) -> Option<&mut LocalPredicateSkeleton> { + if let Some(filename) = listing_src_file_name { + src_compilation_target = CompilationTarget::Module(filename); + } + match src_compilation_target { CompilationTarget::User => self .local_extensible_predicates @@ -740,10 +762,15 @@ impl IndexStore { pub(crate) fn get_local_predicate_skeleton( &self, - src_compilation_target: &CompilationTarget, + mut src_compilation_target: CompilationTarget, local_compilation_target: CompilationTarget, + listing_src_file_name: Option, key: PredicateKey, - ) -> Option<&PredicateSkeleton> { + ) -> Option<&LocalPredicateSkeleton> { + if let Some(filename) = listing_src_file_name { + src_compilation_target = CompilationTarget::Module(filename); + } + match src_compilation_target { CompilationTarget::User => self .local_extensible_predicates @@ -760,23 +787,6 @@ impl IndexStore { } } - pub(crate) fn get_predicate_skeleton( - &self, - compilation_target: &CompilationTarget, - key: &PredicateKey, - ) -> Option<&PredicateSkeleton> { - match compilation_target { - CompilationTarget::User => self.extensible_predicates.get(key), - CompilationTarget::Module(ref module_name) => { - if let Some(module) = self.modules.get(module_name) { - module.extensible_predicates.get(key) - } else { - None - } - } - } - } - pub(crate) fn remove_predicate_skeleton( &mut self, compilation_target: &CompilationTarget, @@ -845,13 +855,13 @@ impl IndexStore { "user" => self .extensible_predicates .get(&key) - .map(|skeleton| skeleton.is_dynamic) + .map(|skeleton| skeleton.core.is_dynamic) .unwrap_or(false), _ => match self.modules.get(&module_name) { Some(ref module) => module .extensible_predicates .get(&key) - .map(|skeleton| skeleton.is_dynamic) + .map(|skeleton| skeleton.core.is_dynamic) .unwrap_or(false), None => false, },