call goals in one batch after rebinding variables, add minatotask.pl, update README
This commit is contained in:
38
src/prolog/machine/attributed_variables.pl
Normal file
38
src/prolog/machine/attributed_variables.pl
Normal file
@@ -0,0 +1,38 @@
|
||||
driver(Vars, Values) :-
|
||||
iterate(Vars, Values, ListOfListsOfGoalLists),
|
||||
'$redo_attr_var_bindings', %% the bindings list is emptied here.
|
||||
call_goals(ListOfListsOfGoalLists),
|
||||
'$restore_p_from_sfcp'.
|
||||
|
||||
iterate([Var|VarBindings], [Value|ValueBindings], [ListOfGoalLists | ListsCubed]) :-
|
||||
'$get_attr_list'(Var, Ls),
|
||||
call_verify_attributes(Ls, Var, Value, ListOfGoalLists),
|
||||
iterate(VarBindings, ValueBindings, ListsCubed).
|
||||
iterate([], [], []).
|
||||
|
||||
call_verify_attributes(Attrs, _, _, []) :-
|
||||
var(Attrs), !.
|
||||
call_verify_attributes([Attr|Attrs], Var, Value, [Goals|ListOfGoalLists]) :-
|
||||
'$module_of'(M, Attr), % write the owning module Attr to M.
|
||||
catch(M:verify_attributes(Var, Value, Goals),
|
||||
error(evaluation_error((M:verify_attributes)/3), call_verify_attributes/3),
|
||||
true),
|
||||
call_verify_attributes(Attrs, Var, Value, ListOfGoalLists).
|
||||
|
||||
call_goals([ListOfGoalLists | ListsCubed]) :-
|
||||
call_goals_0(ListOfGoalLists),
|
||||
call_goals(ListsCubed).
|
||||
call_goals([]).
|
||||
|
||||
call_goals_0([GoalList | GoalLists]) :-
|
||||
( var(GoalList), throw(error(instantiation_error, call_goals_0/1))
|
||||
; true
|
||||
),
|
||||
call_goals_1(GoalList),
|
||||
call_goals_0(GoalLists).
|
||||
call_goals_0([]).
|
||||
|
||||
call_goals_1([Goal | Goals]) :-
|
||||
call(Goal),
|
||||
call_goals_1(Goals).
|
||||
call_goals_1([]).
|
||||
@@ -1,35 +1,13 @@
|
||||
use prolog::machine::*;
|
||||
|
||||
pub static VERIFY_ATTRS: &str = "
|
||||
iterate([Var|VarBindings], [Value|ValueBindings]) :-
|
||||
'$get_attr_list'(Var, Ls),
|
||||
call_verify_attributes(Ls, Var, Value),
|
||||
iterate(VarBindings, ValueBindings).
|
||||
iterate([], []) :- '$restore_p_from_sfcp'.
|
||||
|
||||
call_verify_attributes(Attrs, _, _) :-
|
||||
var(Attrs), !.
|
||||
call_verify_attributes([Attr|Attrs], Var, Value) :-
|
||||
'$module_of'(M, Attr), % write the owning module Attr to M.
|
||||
catch(M:verify_attributes(Var, Value, Goals),
|
||||
error(evaluation_error((M:verify_attributes)/3), verify_attributes/3),
|
||||
true),
|
||||
call_verify_attributes_goals(Goals),
|
||||
call_verify_attributes(Attrs, Var, Value).
|
||||
|
||||
call_verify_attributes_goals(Goals) :-
|
||||
var(Goals), throw(error(instantiation_error, call_verify_attributes_goals/1)).
|
||||
call_verify_attributes_goals([Goal|Goals]) :-
|
||||
call(Goal), !, call_verify_attributes_goals(Goals).
|
||||
call_verify_attributes_goals([]).
|
||||
";
|
||||
pub static VERIFY_ATTRS: &str = include_str!("attributed_variables.pl");
|
||||
|
||||
pub(super) type Bindings = Vec<(usize, Addr)>;
|
||||
|
||||
pub(super) struct AttrVarInitializer {
|
||||
pub(super) bindings: Bindings,
|
||||
cp_stack: Vec<CodePtr>,
|
||||
pub(super) registers: Registers,
|
||||
pub(super) special_form_cp: CodePtr,
|
||||
pub(super) verify_attrs_loc: usize
|
||||
}
|
||||
|
||||
@@ -38,27 +16,42 @@ impl AttrVarInitializer {
|
||||
AttrVarInitializer {
|
||||
bindings: vec![],
|
||||
verify_attrs_loc: p,
|
||||
special_form_cp: CodePtr::VerifyAttrInterrupt(p),
|
||||
cp_stack: vec![],
|
||||
registers: vec![Addr::HeapCell(0); MAX_ARITY + 1]
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(super) fn pop_code_ptr(&mut self) -> CodePtr {
|
||||
self.cp_stack.pop().unwrap()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(super) fn reset(&mut self) {
|
||||
self.special_form_cp = CodePtr::VerifyAttrInterrupt(self.verify_attrs_loc);
|
||||
self.cp_stack.clear();
|
||||
self.bindings.clear();
|
||||
}
|
||||
}
|
||||
|
||||
impl MachineState {
|
||||
pub(super) fn add_attr_var_binding(&mut self, h: usize, addr: Addr)
|
||||
pub(super) fn push_attr_var_binding(&mut self, h: usize, addr: Addr)
|
||||
{
|
||||
self.attr_var_init.bindings.push((h, addr));
|
||||
|
||||
if let &CodePtr::VerifyAttrInterrupt(_) = &self.p {
|
||||
return;
|
||||
if self.attr_var_init.bindings.is_empty() {
|
||||
self.attr_var_init.cp_stack.push(self.p.clone() + 1);
|
||||
self.p = CodePtr::VerifyAttrInterrupt(self.attr_var_init.verify_attrs_loc);
|
||||
}
|
||||
|
||||
mem::swap(&mut self.attr_var_init.special_form_cp, &mut self.p);
|
||||
self.attr_var_init.special_form_cp += 1;
|
||||
self.attr_var_init.bindings.push((h, addr));
|
||||
}
|
||||
|
||||
fn populate_var_and_value_lists(&mut self) -> (Addr, Addr) {
|
||||
let iter = self.attr_var_init.bindings.iter().map(|(ref h, _)| Addr::AttrVar(*h));
|
||||
let var_list_addr = Addr::HeapCell(self.heap.to_list(iter));
|
||||
|
||||
let iter = self.attr_var_init.bindings.iter().map(|(_, ref addr)| addr.clone());
|
||||
let value_list_addr = Addr::HeapCell(self.heap.to_list(iter));
|
||||
|
||||
(var_list_addr, value_list_addr)
|
||||
}
|
||||
|
||||
pub(super)
|
||||
@@ -68,28 +61,24 @@ impl MachineState {
|
||||
STEP 2: Write the list of bindings to two lists in the heap, one for vars, one for values.
|
||||
STEP 3: Swap the machine's Registers for attr_var_init's Registers.
|
||||
STEP 4: Pass the addresses of the lists to iterate in the attr_vars special form.
|
||||
STEP 5: Restore AttrVarInitializer::special_form_cp to self.p.
|
||||
STEP 6: Swap the bindings' Registers back for the machine's Registers.
|
||||
STEP 7: Redo the bindings.
|
||||
STEP 8: Continue.
|
||||
Call verify_attributes/3 wherever applicable.
|
||||
STEP 5: Redo the bindings.
|
||||
STEP 6: Call the goals.
|
||||
STEP 7: Pop the top of AttrVarInitializer::cp_stack to self.p.
|
||||
STEP 8: Swap the AttrVarInitializer's Registers back for the machine's Registers.
|
||||
*/
|
||||
|
||||
// STEP 1.
|
||||
for (h, _) in &self.attr_var_init.bindings {
|
||||
self.heap[*h] = HeapCellValue::Addr(Addr::AttrVar(*h));
|
||||
}
|
||||
|
||||
let (var_list_addr, value_list_addr) = {
|
||||
let iter = self.attr_var_init.bindings.iter().map(|(ref h, _)| Addr::AttrVar(*h));
|
||||
let var_list_addr = Addr::HeapCell(self.heap.to_list(iter));
|
||||
|
||||
let iter = self.attr_var_init.bindings.iter().map(|(_, ref addr)| addr.clone());
|
||||
let value_list_addr = Addr::HeapCell(self.heap.to_list(iter));
|
||||
|
||||
(var_list_addr, value_list_addr)
|
||||
};
|
||||
|
||||
// STEP 2.
|
||||
let (var_list_addr, value_list_addr) = self.populate_var_and_value_lists();
|
||||
// STEP 3.
|
||||
mem::swap(&mut self.registers, &mut self.attr_var_init.registers);
|
||||
|
||||
// STEP 4.
|
||||
self[temp_v!(1)] = var_list_addr;
|
||||
self[temp_v!(2)] = value_list_addr;
|
||||
}
|
||||
|
||||
@@ -378,7 +378,7 @@ pub(crate) trait CallPolicy: Any {
|
||||
machine_st.pstr_trail.truncate(machine_st.pstr_tr);
|
||||
|
||||
machine_st.heap.truncate(machine_st.or_stack[b].h);
|
||||
|
||||
|
||||
machine_st.hb = machine_st.heap.h;
|
||||
machine_st.p += offset;
|
||||
|
||||
|
||||
@@ -109,7 +109,7 @@ impl MachineState {
|
||||
self.trail(TrailRef::Ref(Ref::StackCell(fr, sc)));
|
||||
},
|
||||
_ => {
|
||||
self.add_attr_var_binding(h, addr.clone());
|
||||
self.push_attr_var_binding(h, addr.clone());
|
||||
self.heap[h] = HeapCellValue::Addr(addr);
|
||||
self.trail(TrailRef::Ref(Ref::AttrVar(h)));
|
||||
}
|
||||
@@ -2260,7 +2260,7 @@ impl MachineState {
|
||||
self.tr,
|
||||
self.pstr_tr,
|
||||
self.heap.h,
|
||||
self.b0,
|
||||
self.b0,
|
||||
self.num_of_args);
|
||||
|
||||
self.b = self.or_stack.len();
|
||||
|
||||
@@ -325,10 +325,9 @@ impl Machine {
|
||||
match compile_special_form(self, VERIFY_ATTRS.as_bytes()) {
|
||||
Ok(code) => {
|
||||
self.machine_st.attr_var_init.verify_attrs_loc = self.code_repo.code.len();
|
||||
self.machine_st.attr_var_init.reset();
|
||||
self.code_repo.code.extend(code.into_iter());
|
||||
},
|
||||
Err(_e) => panic!("Machine::compile_special_forms() failed")
|
||||
Err(_) => panic!("Machine::compile_special_forms() failed")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -399,6 +399,13 @@ impl MachineState {
|
||||
_ => self.fail = true
|
||||
};
|
||||
},
|
||||
&SystemClauseType::RedoAttrVarBindings => {
|
||||
let mut bindings = mem::replace(&mut self.attr_var_init.bindings, vec![]);
|
||||
|
||||
for (h, addr) in bindings {
|
||||
self.heap[h] = HeapCellValue::Addr(addr);
|
||||
}
|
||||
},
|
||||
&SystemClauseType::RemoveCallPolicyCheck => {
|
||||
let restore_default =
|
||||
match call_policy.downcast_mut::<CWILCallPolicy>().ok() {
|
||||
@@ -443,26 +450,9 @@ impl MachineState {
|
||||
CWILCallPolicy.")
|
||||
},
|
||||
&SystemClauseType::RestoreCodePtrFromSpecialFormCP => {
|
||||
self.p = mem::replace(&mut self.attr_var_init.special_form_cp,
|
||||
CodePtr::VerifyAttrInterrupt(self.attr_var_init.verify_attrs_loc));
|
||||
self.p = self.attr_var_init.pop_code_ptr();
|
||||
mem::swap(&mut self.registers, &mut self.attr_var_init.registers);
|
||||
|
||||
let mut bindings = vec![];
|
||||
mem::swap(&mut bindings, &mut self.attr_var_init.bindings);
|
||||
|
||||
for (h, addr) in bindings {
|
||||
let deref_h = self.store(self.deref(Addr::AttrVar(h)));
|
||||
|
||||
if &Addr::AttrVar(h) != &deref_h {
|
||||
if &deref_h != &addr {
|
||||
self.fail = true;
|
||||
return Ok(());
|
||||
}
|
||||
} else {
|
||||
self.heap[h] = HeapCellValue::Addr(addr);
|
||||
}
|
||||
}
|
||||
|
||||
return Ok(());
|
||||
},
|
||||
&SystemClauseType::RestoreCutPolicy => {
|
||||
@@ -639,8 +629,6 @@ impl MachineState {
|
||||
}
|
||||
};
|
||||
|
||||
self.set_p();
|
||||
|
||||
Ok(())
|
||||
Ok(self.set_p())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user