preliminary cont work

This commit is contained in:
Mark Thom
2019-12-19 20:02:10 -04:00
15 changed files with 359 additions and 144 deletions

View File

@@ -1,6 +1,6 @@
driver(Vars, Values) :-
iterate(Vars, Values, ListOfListsOfGoalLists),
'$redo_attr_var_bindings', % the bindings list is emptied here.
'$clear_attr_var_bindings',
!,
call_goals(ListOfListsOfGoalLists),
'$return_from_verify_attr'.
@@ -8,6 +8,7 @@ driver(Vars, Values) :-
iterate([Var|VarBindings], [Value|ValueBindings], [ListOfGoalLists | ListsCubed]) :-
'$get_attr_list'(Var, Ls),
call_verify_attributes(Ls, Var, Value, ListOfGoalLists),
'$redo_attr_var_binding'(Var, Value),
iterate(VarBindings, ValueBindings, ListsCubed).
iterate([], [], []).
@@ -28,7 +29,7 @@ call_verify_attributes(Attrs, _, _, []) :-
call_verify_attributes([], _, _, []).
call_verify_attributes([Attr|Attrs], Var, Value, ListOfGoalLists) :-
gather_modules([Attr|Attrs], Modules0),
sort(Modules0, Modules),
sort(Modules0, Modules),
verify_attrs(Modules, Var, Value, ListOfGoalLists).
call_goals([ListOfGoalLists | ListsCubed]) :-

View File

@@ -649,7 +649,8 @@ impl ListingCompiler {
let idx = code_dir
.entry((name.clone(), arity))
.or_insert(CodeIndex::default());
set_code_index!(idx, IndexPtr::Index(p), self.get_module_name());
set_code_index!(idx, IndexPtr::Index(p), self.get_module_name());
self.localize_self_calls(name, arity, &mut decl_code, p);
code.extend(decl_code.into_iter());

View File

@@ -5,6 +5,12 @@ use std::ops::IndexMut;
type Trail = Vec<(Ref, HeapCellValue)>;
#[derive(Clone, Copy)]
pub enum AttrVarPolicy {
DeepCopy,
StripAttributes
}
pub(crate) trait CopierTarget: IndexMut<usize, Output = HeapCellValue> {
fn threshold(&self) -> usize;
fn push(&mut self, _: HeapCellValue);
@@ -13,9 +19,10 @@ pub(crate) trait CopierTarget: IndexMut<usize, Output = HeapCellValue> {
fn stack(&mut self) -> &mut Stack;
}
pub(crate) fn copy_term<T: CopierTarget>(target: T, addr: Addr) {
let mut copy_term_state = CopyTermState::new(target);
copy_term_state.copy_term_impl(addr);
pub(crate)
fn copy_term<T: CopierTarget>(target: T, addr: Addr, attr_var_policy: AttrVarPolicy) {
let mut copy_term_state = CopyTermState::new(target, attr_var_policy);
copy_term_state.copy_term_impl(addr);
}
struct CopyTermState<T: CopierTarget> {
@@ -23,15 +30,17 @@ struct CopyTermState<T: CopierTarget> {
scan: usize,
old_h: usize,
target: T,
attr_var_policy: AttrVarPolicy
}
impl<T: CopierTarget> CopyTermState<T> {
fn new(target: T) -> Self {
fn new(target: T, attr_var_policy: AttrVarPolicy) -> Self {
CopyTermState {
trail: vec![],
scan: 0,
old_h: target.threshold(),
target,
attr_var_policy
}
}
@@ -41,40 +50,17 @@ impl<T: CopierTarget> CopyTermState<T> {
&mut self.target[scan]
}
fn reinstantiate_var(&mut self, addr: Addr, threshold: usize) {
match addr {
Addr::HeapCell(h) => {
self.target[threshold] = HeapCellValue::Addr(Addr::HeapCell(threshold));
self.target[h] = HeapCellValue::Addr(Addr::HeapCell(threshold));
self.trail
.push((Ref::HeapCell(h), HeapCellValue::Addr(Addr::HeapCell(h))));
}
Addr::StackCell(fr, sc) => {
self.target[threshold] = HeapCellValue::Addr(Addr::HeapCell(threshold));
self.target.stack().index_and_frame_mut(fr)[sc] = Addr::HeapCell(threshold);
self.trail.push((
Ref::StackCell(fr, sc),
HeapCellValue::Addr(Addr::StackCell(fr, sc)),
));
}
Addr::AttrVar(h) => {
self.target[threshold] = HeapCellValue::Addr(Addr::AttrVar(threshold));
self.target[h] = HeapCellValue::Addr(Addr::AttrVar(threshold));
self.trail
.push((Ref::AttrVar(h), HeapCellValue::Addr(Addr::AttrVar(h))));
fn copied_list(&mut self, addr: usize) -> bool {
match self.target[addr].clone() {
HeapCellValue::Addr(Addr::Lis(addr)) | HeapCellValue::Addr(Addr::HeapCell(addr)) => {
if addr >= self.old_h {
*self.value_at_scan() = HeapCellValue::Addr(Addr::Lis(addr));
self.scan += 1;
return true;
}
}
_ => {}
}
}
fn copied_list(&mut self, addr: usize) -> bool {
if let HeapCellValue::Addr(Addr::Lis(addr)) = self.target[addr].clone() {
if addr >= self.old_h {
*self.value_at_scan() = HeapCellValue::Addr(Addr::Lis(addr));
self.scan += 1;
return true;
}
}
};
false
}
@@ -88,11 +74,15 @@ impl<T: CopierTarget> CopyTermState<T> {
*self.value_at_scan() = HeapCellValue::Addr(Addr::Lis(threshold));
let hcv = self.target[addr].clone();
self.target.push(hcv.clone());
let ra = hcv.as_addr(threshold);
let rd = self.target.store(self.target.deref(ra));
self.target.push(hcv);
let hcv = self.target[addr + 1].clone();
self.target.push(hcv);
match rd.clone() {
Addr::AttrVar(h) | Addr::HeapCell(h) if h >= self.old_h => {
self.target[threshold] = HeapCellValue::Addr(rd)
@@ -100,23 +90,67 @@ impl<T: CopierTarget> CopyTermState<T> {
ra @ Addr::AttrVar(_) | ra @ Addr::HeapCell(..) | ra @ Addr::StackCell(..) => {
if ra == rd {
self.reinstantiate_var(ra, threshold);
if let AttrVarPolicy::StripAttributes = self.attr_var_policy {
self.trail.push((Ref::HeapCell(addr), self.target[addr].clone()));
self.target[addr] = HeapCellValue::Addr(Addr::HeapCell(threshold));
}
} else {
self.target[threshold] = HeapCellValue::Addr(ra);
}
}
_ => {
self.trail
.push((Ref::HeapCell(addr), self.target[addr].clone()));
self.trail.push((Ref::HeapCell(addr), self.target[addr].clone()));
self.target[addr] = HeapCellValue::Addr(Addr::Lis(threshold))
}
};
let hcv = self.target[addr + 1].clone();
self.target.push(hcv);
self.scan += 1;
}
fn reinstantiate_var(&mut self, addr: Addr, frontier: usize) {
match addr {
Addr::HeapCell(h) => {
self.target[frontier] = HeapCellValue::Addr(Addr::HeapCell(frontier));
self.target[h] = HeapCellValue::Addr(Addr::HeapCell(frontier));
self.trail.push((
Ref::HeapCell(h),
HeapCellValue::Addr(Addr::HeapCell(h)),
));
}
Addr::StackCell(fr, sc) => {
self.target[frontier] = HeapCellValue::Addr(Addr::HeapCell(frontier));
self.target.stack().index_and_frame_mut(fr)[sc] = Addr::HeapCell(frontier);
self.trail.push((
Ref::StackCell(fr, sc),
HeapCellValue::Addr(Addr::StackCell(fr, sc)),
));
}
Addr::AttrVar(h) => {
let threshold = if let AttrVarPolicy::DeepCopy = self.attr_var_policy {
self.target.threshold()
} else {
frontier
};
self.target[frontier] = HeapCellValue::Addr(Addr::HeapCell(threshold));
self.target[h] = HeapCellValue::Addr(Addr::HeapCell(threshold));
self.trail.push((
Ref::AttrVar(h),
HeapCellValue::Addr(Addr::AttrVar(h)),
));
if let AttrVarPolicy::DeepCopy = self.attr_var_policy {
self.target.push(HeapCellValue::Addr(Addr::AttrVar(threshold)));
let list_val = self.target[h + 1].clone();
self.target.push(list_val);
}
}
_ => unreachable!()
}
}
fn copy_var(&mut self, addr: Addr) {
let rd = self.target.store(self.target.deref(addr.clone()));
@@ -125,23 +159,13 @@ impl<T: CopierTarget> CopyTermState<T> {
*self.value_at_scan() = HeapCellValue::Addr(rd);
self.scan += 1;
}
Addr::AttrVar(h) if addr == rd => {
let threshold = self.target.threshold();
self.target
.push(HeapCellValue::Addr(Addr::AttrVar(threshold)));
let list_val = self.target[h + 1].clone();
self.target.push(list_val);
self.reinstantiate_var(addr, threshold);
*self.value_at_scan() = HeapCellValue::Addr(Addr::AttrVar(threshold));
}
_ if addr == rd => {
let scan = self.scan;
self.reinstantiate_var(addr, scan);
self.reinstantiate_var(addr, self.scan);
self.scan += 1;
}
_ => *self.value_at_scan() = HeapCellValue::Addr(rd),
_ => {
*self.value_at_scan() = HeapCellValue::Addr(rd);
}
}
}
@@ -158,8 +182,7 @@ impl<T: CopierTarget> CopyTermState<T> {
HeapCellValue::NamedStr(arity, name.clone(), fixity.clone()),
));
self.target
.push(HeapCellValue::NamedStr(arity, name, fixity));
self.target.push(HeapCellValue::NamedStr(arity, name, fixity));
for i in 0..arity {
let hcv = self.target[addr + 1 + i].clone();
@@ -185,8 +208,8 @@ impl<T: CopierTarget> CopyTermState<T> {
HeapCellValue::Addr(addr) => match addr {
Addr::Lis(addr) => self.copy_list(addr),
addr @ Addr::AttrVar(_)
| addr @ Addr::HeapCell(_)
| addr @ Addr::StackCell(..) => self.copy_var(addr),
| addr @ Addr::HeapCell(_)
| addr @ Addr::StackCell(..) => self.copy_var(addr),
Addr::Str(addr) => self.copy_structure(addr),
Addr::Con(_) | Addr::DBRef(_) => self.scan += 1,
},

View File

@@ -61,7 +61,7 @@ impl Ball {
});
}
stub
stub
}
}
@@ -444,7 +444,10 @@ pub(crate) trait CallPolicy: Any {
let attr_var_init_queue_b = machine_st.stack.index_or_frame(b).prelude.attr_var_init_queue_b;
let attr_var_init_bindings_b = machine_st.stack.index_or_frame(b).prelude.attr_var_init_bindings_b;
machine_st.attr_var_init.backtrack(attr_var_init_queue_b, attr_var_init_bindings_b);
machine_st.attr_var_init.backtrack(
attr_var_init_queue_b,
attr_var_init_bindings_b,
);
machine_st.hb = machine_st.heap.h;
machine_st.p += 1;
@@ -481,7 +484,6 @@ pub(crate) trait CallPolicy: Any {
machine_st.pstr_tr = machine_st.stack.index_or_frame(b).prelude.pstr_tr;
machine_st.pstr_trail.truncate(machine_st.pstr_tr);
machine_st.heap.truncate(machine_st.stack.index_or_frame(b).prelude.h);
let attr_var_init_queue_b = machine_st.stack.index_or_frame(b).prelude.attr_var_init_queue_b;
@@ -502,7 +504,7 @@ pub(crate) trait CallPolicy: Any {
for i in 1 .. n + 1 {
machine_st.registers[i] = machine_st.stack.index_or_frame(b)[i-1].clone();
}
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;
@@ -522,13 +524,14 @@ pub(crate) trait CallPolicy: Any {
machine_st.pstr_tr = machine_st.stack.index_or_frame(b).prelude.pstr_tr;
machine_st.pstr_trail.truncate(machine_st.pstr_tr);
machine_st.heap.truncate(machine_st.stack.index_or_frame(b).prelude.h);
let attr_var_init_queue_b = machine_st.stack.index_or_frame(b).prelude.attr_var_init_queue_b;
let attr_var_init_bindings_b = machine_st.stack.index_or_frame(b).prelude.attr_var_init_bindings_b;
machine_st.attr_var_init.backtrack(attr_var_init_queue_b, attr_var_init_bindings_b);
machine_st.attr_var_init.backtrack(
attr_var_init_queue_b,
attr_var_init_bindings_b);
machine_st.b = machine_st.stack.index_or_frame(b).prelude.b;
machine_st.truncate_stack();
@@ -565,6 +568,7 @@ pub(crate) trait CallPolicy: Any {
machine_st.unwind_pstr_trail(old_pstr_tr, curr_pstr_tr);
machine_st.pstr_tr = machine_st.stack.index_or_frame(b).prelude.pstr_tr;
machine_st.pstr_tr = machine_st.stack.index_or_frame(b).prelude.pstr_tr;
machine_st.pstr_trail.truncate(machine_st.pstr_tr);
machine_st.heap.truncate(machine_st.stack.index_or_frame(b).prelude.h);
@@ -572,7 +576,10 @@ pub(crate) trait CallPolicy: Any {
let attr_var_init_queue_b = machine_st.stack.index_or_frame(b).prelude.attr_var_init_queue_b;
let attr_var_init_bindings_b = machine_st.stack.index_or_frame(b).prelude.attr_var_init_bindings_b;
machine_st.attr_var_init.backtrack(attr_var_init_queue_b, attr_var_init_bindings_b);
machine_st.attr_var_init.backtrack(
attr_var_init_queue_b,
attr_var_init_bindings_b
);
machine_st.b = machine_st.stack.index_or_frame(b).prelude.b;
machine_st.truncate_stack();
@@ -715,7 +722,7 @@ pub(crate) trait CallPolicy: Any {
return_from_clause!(machine_st.last_call, machine_st)
}
&BuiltInClauseType::CopyTerm => {
machine_st.copy_term();
machine_st.copy_term(AttrVarPolicy::DeepCopy);
return_from_clause!(machine_st.last_call, machine_st)
}
&BuiltInClauseType::Eq => {

View File

@@ -2005,11 +2005,15 @@ impl MachineState {
}
pub(super) fn set_ball(&mut self) {
self.ball.reset();
let addr = self[temp_v!(1)].clone();
self.ball.boundary = self.heap.h;
copy_term(
CopyBallTerm::new(&mut self.stack, &mut self.heap, &mut self.ball.stub),
addr,
AttrVarPolicy::DeepCopy,
);
}
@@ -2912,13 +2916,13 @@ impl MachineState {
}
}
pub(super) fn copy_term(&mut self) {
pub(super) fn copy_term(&mut self, attr_var_policy: AttrVarPolicy) {
let old_h = self.heap.h;
let a1 = self[temp_v!(1)].clone();
let a2 = self[temp_v!(2)].clone();
copy_term(CopyTerm::new(self), a1);
copy_term(CopyTerm::new(self), a1, attr_var_policy);
self.unify(Addr::HeapCell(old_h), a2);
}

View File

@@ -49,8 +49,8 @@ call_attribute_goals([Module | Modules], GoalCaller, AttrVars) :-
call_query_var_goals([], _, []).
call_query_var_goals([AttrVar|AttrVars], Module, Goals) :-
( catch(( Module:attribute_goals(AttrVar, Goals, RGoals0),
atts:'$default_attr_list'(Module, AttrVar, RGoals0, RGoals)
( catch(( Module:attribute_goals(AttrVar, Goals, RGoals0)
, atts:'$default_attr_list'(Module, AttrVar, RGoals0, RGoals)
),
E,
( '$print_attribute_goals_exception'(Module, E),
@@ -63,7 +63,7 @@ call_query_var_goals([AttrVar|AttrVars], Module, Goals) :-
call_attr_var_goals([], _, []).
call_attr_var_goals([AttrVar|AttrVars], Module, Goals) :-
( catch(Module:attribute_goals(AttrVar, Goals, RGoals),
( catch(Module:attribute_goals(AttrVar, Goals, RGoals),
E,
'$print_attribute_goals_exception'(Module, E)
)
@@ -83,3 +83,26 @@ gather_modules_for_attrs(Attrs, Modules, Modules) :-
gather_modules_for_attrs([Attr|Attrs], [Module|Modules], Modules0) :-
'$module_of'(Module, Attr),
gather_modules_for_attrs(Attrs, Modules, Modules0).
module_prefixed_goals([], _, Gs, Gs).
module_prefixed_goals([G|Gs], Module, [MG|MGs], TailGs) :-
( G = _:_ -> MG = G
; MG = Module:G
),
module_prefixed_goals(Gs, Module, MGs, TailGs).
call_attribute_goals_with_module_prefix([], _, _, []).
call_attribute_goals_with_module_prefix([Module | Modules], GoalCaller, AttrVars, Goals) :-
call(GoalCaller, AttrVars, Module, Goals0),
enqueue_goals(Goals0),
module_prefixed_goals(Goals0, Module, Goals, Gs),
call_attribute_goals_with_module_prefix(Modules, GoalCaller, AttrVars, Gs).
copy_term(Source, Dest, Goals) :-
term_variables(Source, Vars),
gather_modules(Vars, Modules0, _),
sort(Modules0, Modules),
call_attribute_goals_with_module_prefix(Modules, call_query_var_goals, Vars, Goals0),
sort(Goals0, Goals1),
!,
'$copy_term_without_attr_vars'([Source | Goals1], [Dest | Goals]).

View File

@@ -353,7 +353,7 @@ impl MachineState {
copy_ball_term.push(HeapCellValue::Addr(Addr::HeapCell(threshold + 3)));
copy_ball_term.push(HeapCellValue::Addr(Addr::HeapCell(threshold + 2)));
copy_term(copy_ball_term, copy_target);
copy_term(copy_ball_term, copy_target, AttrVarPolicy::DeepCopy);
threshold + lh_offset + 2
}
@@ -527,6 +527,16 @@ impl MachineState {
Ok(())
}
fn fetch_attribute_goals(&mut self, mut attr_goals: Vec<Addr>) {
attr_goals.sort_unstable_by(|a1, a2| self.compare_term_test(a1, a2));
self.term_dedup(&mut attr_goals);
let attr_goals = Addr::HeapCell(self.heap.to_list(attr_goals.into_iter()));
let target = self[temp_v!(1)].clone();
self.unify(attr_goals, target);
}
fn create_instruction_functors(&mut self, code: &Code, first_idx: usize) -> Vec<Addr> {
let mut queue = VecDeque::new();
let mut functors = vec![];
@@ -594,6 +604,25 @@ impl MachineState {
self.p = CodePtr::DynamicTransaction(trans_type, p);
return Ok(());
}
&SystemClauseType::BindFromRegister => {
let reg = self.store(self.deref(self[temp_v!(2)].clone()));
let n = match reg {
Addr::Con(Constant::Integer(n)) => n.to_usize(),
_ => unreachable!()
};
if let Some(n) = n {
if n <= MAX_ARITY {
let target = self[temp_v!(n)].clone();
let addr = self[temp_v!(1)].clone();
self.unify(addr, target);
return return_from_clause!(self.last_call, self);
}
}
self.fail = true;
}
&SystemClauseType::AssertDynamicPredicateToFront => {
let p = self.cp;
let trans_type = DynamicTransactionType::Assert(DynamicAssertPlace::Front);
@@ -874,6 +903,9 @@ impl MachineState {
_ => self.fail = true,
};
}
&SystemClauseType::CopyTermWithoutAttrVars => {
self.copy_term(AttrVarPolicy::StripAttributes);
}
&SystemClauseType::FetchGlobalVar => {
let key = self[temp_v!(1)].clone();
@@ -1109,7 +1141,7 @@ impl MachineState {
for i in (arity + 1 .. arity + narity + 1).rev() {
self.registers[i] = self.registers[i - arity].clone();
}
for i in 1 .. arity + 1 {
self.registers[i] = self.heap[a + i].as_addr(a + i);
}
@@ -1122,7 +1154,7 @@ impl MachineState {
);
}
}
Addr::Con(Constant::Atom(name, _)) => {
Addr::Con(Constant::Atom(name, _)) => {
return self.module_lookup(indices, (name, narity), module_name, true)
}
addr => {
@@ -1370,16 +1402,16 @@ impl MachineState {
&SystemClauseType::TruncateIfNoLiftedHeapGrowth => {
self.truncate_if_no_lifted_heap_diff(|_| Addr::Con(Constant::EmptyList))
}
&SystemClauseType::ClearAttributeGoals => {
self.attr_var_init.attribute_goals.clear();
}
&SystemClauseType::CloneAttributeGoals => {
let attr_goals = self.attr_var_init.attribute_goals.clone();
self.fetch_attribute_goals(attr_goals);
}
&SystemClauseType::FetchAttributeGoals => {
let mut attr_goals = mem::replace(&mut self.attr_var_init.attribute_goals, vec![]);
attr_goals.sort_unstable_by(|a1, a2| self.compare_term_test(a1, a2));
self.term_dedup(&mut attr_goals);
let attr_goals = Addr::HeapCell(self.heap.to_list(attr_goals.into_iter()));
let target = self[temp_v!(1)].clone();
self.unify(attr_goals, target);
let attr_goals = mem::replace(&mut self.attr_var_init.attribute_goals, vec![]);
self.fetch_attribute_goals(attr_goals);
}
&SystemClauseType::GetAttributedVariableList => {
let attr_var = self.store(self.deref(self[temp_v!(1)].clone()));
@@ -1643,11 +1675,32 @@ impl MachineState {
}
};
}
&SystemClauseType::RedoAttrVarBindings => {
let bindings = mem::replace(&mut self.attr_var_init.bindings, vec![]);
&SystemClauseType::ClearAttrVarBindings => {
self.attr_var_init.bindings.clear();
}
&SystemClauseType::RedoAttrVarBinding => {
let var = self.store(self.deref(self[temp_v!(1)].clone()));
let value = self.store(self.deref(self[temp_v!(2)].clone()));
for (h, addr) in bindings {
self.heap[h] = HeapCellValue::Addr(addr);
match var {
Addr::AttrVar(h) => {
if let Addr::AttrVar(h1) = value {
self.heap[h] = HeapCellValue::Addr(Addr::AttrVar(h1));
// append h's attributes list to h1's.
let mut l = h1 + 1;
while let Addr::Lis(l1) = self.store(self.deref(self.heap[l].as_addr(l))) {
l = l1 + 1;
}
self.heap[l] = HeapCellValue::Addr(Addr::HeapCell(h + 1));
self.trail(TrailRef::Ref(Ref::HeapCell(l)));
} else {
self.heap[h] = HeapCellValue::Addr(value);
}
}
_ => unreachable!()
}
}
&SystemClauseType::ResetGlobalVarAtKey => {
@@ -1676,6 +1729,7 @@ impl MachineState {
copy_term(
CopyBallTerm::new(&mut self.stack, &mut self.heap, &mut ball.stub),
value,
AttrVarPolicy::DeepCopy,
);
let offset = self[temp_v!(3)].clone();
@@ -1930,11 +1984,13 @@ impl MachineState {
&SystemClauseType::ReadTerm => {
readline::set_prompt(false);
self.read_term(current_input_stream, indices)?;
},
}
&SystemClauseType::ResetBlock => {
let addr = self.deref(self[temp_v!(1)].clone());
self.reset_block(addr);
}
&SystemClauseType::ResetContinuationMarker => {
}
&SystemClauseType::SetBall =>
self.set_ball(),
&SystemClauseType::SetSeed => {
@@ -1981,6 +2037,7 @@ impl MachineState {
copy_term(
CopyBallTerm::new(&mut self.stack, &mut self.heap, &mut ball.stub),
value,
AttrVarPolicy::DeepCopy,
);
indices.global_variables.insert(key, (ball, None));
@@ -2001,6 +2058,7 @@ impl MachineState {
copy_term(
CopyBallTerm::new(&mut self.stack, &mut self.heap, &mut ball.stub),
value.clone(),
AttrVarPolicy::DeepCopy,
);
let stub = ball.copy_and_align(h);