add preliminary support for verify_attributes/3

This commit is contained in:
Mark Thom
2019-02-07 23:33:55 -07:00
parent 7c2f626d56
commit 661f9b7414
7 changed files with 159 additions and 60 deletions

View File

@@ -1,24 +1,98 @@
use prolog::machine::*;
pub static VERIFY_ATTRS: &str = "
iterate([Var-Value|Bindings]) :-
iterate([Var|VarBindings], [Value|ValueBindings]) :-
'$get_attr_list'(Var, Ls),
call_verify_attributes(Ls, Var, Value),
iterate(Bindings),
iterate(VarBindings, ValueBindings),
'$restore_p_from_sfcp'.
iterate([]).
iterate([], []).
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_attribute(Var, Value, Goals), error(existence_error(procedure, _), _), true),
call_verify_attribute_goals(Goals),
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_attribute_goals(Goals) :-
var(Goals), throw(error(instantiation_error, call_verify_attribute_goals/1)).
call_verify_attribute_goals([Goal|Goals]) :-
call(Goal),
call_verify_attribute_goals(Goals).
call_verify_attribute_goals([]).
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(super) type Bindings = Vec<(usize, Addr)>;
pub(super) struct AttrVarInitializer {
pub(super) bindings: Bindings,
pub(super) registers: Registers,
pub(super) special_form_cp: CodePtr,
pub(super) verify_attrs_loc: usize
}
impl AttrVarInitializer {
pub(super) fn new(p: usize) -> Self {
AttrVarInitializer {
bindings: vec![],
verify_attrs_loc: p,
special_form_cp: CodePtr::VerifyAttrInterrupt(p),
registers: vec![Addr::HeapCell(0); MAX_ARITY + 1]
}
}
pub(super) fn reset(&mut self) {
self.special_form_cp = CodePtr::VerifyAttrInterrupt(self.verify_attrs_loc);
}
}
impl MachineState {
pub(super) fn add_attr_var_binding(&mut self, h: usize, addr: Addr)
{
self.attr_var_init.bindings.push((h, addr));
if let &CodePtr::VerifyAttrInterrupt(_) = &self.p {
return;
}
mem::swap(&mut self.attr_var_init.special_form_cp, &mut self.p);
self.attr_var_init.special_form_cp += 1;
}
pub(super)
fn verify_attributes(&mut self)
{
/* STEP 1: Undo bindings in machine (DONE)
STEP 2: Write the list of bindings to two lists in the heap, one for vars, one for values. (DONE)
STEP 3: Swap the machine's Registers for attr_var_init's Registers. (DONE)
STEP 4: Pass the addresses of the lists to iterate in the attr_vars special form. (DONE)
STEP 5: Restore AttrVarInitializer::special_form_cp to self.p (DONE).
STEP 6: Swap the bindings' Registers back for the machine's Registers. (DONE)
STEP 7: Redo the bindings. (DONE)
STEP 8: Continue.
*/
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)
};
mem::swap(&mut self.registers, &mut self.attr_var_init.registers);
self[temp_v!(1)] = var_list_addr;
self[temp_v!(2)] = value_list_addr;
}
}

View File

@@ -4,7 +4,7 @@ use prolog_parser::string_list::*;
use prolog::instructions::*;
use prolog::and_stack::*;
use prolog::copier::*;
use prolog::machine::IndexStore;
use prolog::machine::{AttrVarInitializer, IndexStore};
use prolog::machine::machine_errors::*;
use prolog::num::{BigInt, BigUint, Zero, One};
use prolog::or_stack::*;
@@ -206,7 +206,7 @@ pub struct MachineState {
pub(super) e: usize,
pub(super) num_of_args: usize,
pub(super) cp: LocalCodePtr,
pub(super) special_form_cp: CodePtr,
pub(super) attr_var_init: AttrVarInitializer,
pub(super) fail: bool,
pub(crate) heap: Heap,
pub(super) mode: MachineMode,
@@ -231,14 +231,14 @@ impl MachineState {
self.cp.assign_if_local(self.p.clone() + 1);
self.num_of_args = arity;
self.b0 = self.b;
self.p = dir_entry!(p);
self.p = dir_entry!(p);
}
fn execute_at_index(&mut self, arity: usize, p: usize)
{
self.num_of_args = arity;
self.b0 = self.b;
self.p = dir_entry!(p);
self.p = dir_entry!(p);
}
pub(super)
@@ -246,8 +246,8 @@ impl MachineState {
last_call: bool)
-> CallResult
{
let (name, arity) = key;
let (name, arity) = key;
if let Some(ref idx) = indices.get_code_index((name.clone(), arity), module_name.clone())
{
if let IndexPtr::Index(compiled_tl_index) = idx.0.borrow().0 {
@@ -256,15 +256,15 @@ impl MachineState {
} else {
self.call_at_index(arity, compiled_tl_index);
}
return Ok(());
}
}
let h = self.heap.h;
let stub = MachineError::functor_stub(name.clone(), arity);
let err = MachineError::module_resolution_error(h, module_name, name, arity);
let stub = MachineError::functor_stub(name.clone(), arity);
let err = MachineError::module_resolution_error(h, module_name, name, arity);
return Err(self.error_form(err, stub));
}
}
@@ -615,7 +615,7 @@ pub(crate) trait CallPolicy: Any {
list.sort_unstable_by(|a1, a2| machine_st.compare_term_test(a1, a2));
machine_st.term_dedup(&mut list);
let heap_addr = Addr::HeapCell(machine_st.to_list(list.into_iter()));
let heap_addr = Addr::HeapCell(machine_st.heap.to_list(list.into_iter()));
let r2 = machine_st[temp_v!(2)].clone();
machine_st.unify(r2, heap_addr);
@@ -637,7 +637,7 @@ pub(crate) trait CallPolicy: Any {
key_pairs.sort_by(|a1, a2| machine_st.compare_term_test(&a1.0, &a2.0));
let key_pairs = key_pairs.into_iter().map(|kp| kp.1);
let heap_addr = Addr::HeapCell(machine_st.to_list(key_pairs));
let heap_addr = Addr::HeapCell(machine_st.heap.to_list(key_pairs));
let r2 = machine_st[temp_v!(2)].clone();
machine_st.unify(r2, heap_addr);

View File

@@ -6,7 +6,7 @@ use prolog::and_stack::*;
use prolog::copier::*;
use prolog::heap_iter::*;
use prolog::heap_print::*;
use prolog::machine::IndexStore;
use prolog::machine::{AttrVarInitializer, IndexStore};
use prolog::machine::machine_errors::*;
use prolog::machine::machine_state::*;
use prolog::num::{Integer, Signed, ToPrimitive, Zero};
@@ -40,7 +40,7 @@ impl MachineState {
e: 0,
num_of_args: 0,
cp: LocalCodePtr::default(),
special_form_cp: CodePtr::default(),
attr_var_init: AttrVarInitializer::new(0),
fail: false,
heap: Heap::with_capacity(256),
mode: MachineMode::Write,
@@ -102,16 +102,16 @@ impl MachineState {
match addr.as_var() {
Some(Ref::HeapCell(hc)) => {
self.heap[hc] = HeapCellValue::Addr(Addr::AttrVar(h));
self.trail(TrailRef::from(Ref::HeapCell(hc)));
self.trail(TrailRef::Ref(Ref::HeapCell(hc)));
},
Some(Ref::StackCell(fr, sc)) => {
self.and_stack[fr][sc] = Addr::AttrVar(h);
self.trail(TrailRef::from(Ref::StackCell(fr, sc)));
self.trail(TrailRef::Ref(Ref::StackCell(fr, sc)));
},
_ => {
// TODO: set up writing to the attribute queue here.
self.add_attr_var_binding(h, addr.clone());
self.heap[h] = HeapCellValue::Addr(addr);
self.trail(TrailRef::from(Ref::AttrVar(h)));
self.trail(TrailRef::Ref(Ref::AttrVar(h)));
}
}
}
@@ -1941,20 +1941,6 @@ impl MachineState {
*list = result;
}
pub(super) fn to_list<Iter: Iterator<Item=Addr>>(&mut self, values: Iter) -> usize {
let head_addr = self.heap.h;
for value in values {
let h = self.heap.h;
self.heap.push(HeapCellValue::Addr(Addr::Lis(h+1)));
self.heap.push(HeapCellValue::Addr(value));
}
self.heap.push(HeapCellValue::Addr(Addr::Con(Constant::EmptyList)));
head_addr
}
pub(super) fn try_from_list(&self, r: RegType, caller: MachineStub)
-> Result<Vec<Addr>, MachineStub>
{
@@ -2364,7 +2350,7 @@ impl MachineState {
self.pstr_tr = 0;
self.p = CodePtr::default();
self.cp = LocalCodePtr::default();
self.special_form_cp = CodePtr::default();
self.attr_var_init.reset();
self.num_of_args = 0;
self.fail = false;

View File

@@ -118,7 +118,6 @@ impl CodeRepo {
term_expanders: Code::new(),
code: Code::new(),
in_situ_code: Code::new(),
verify_attrs_code: Code::new(),
term_dir: TermDir::new()
}
}
@@ -202,7 +201,10 @@ impl CodeRepo {
&CodePtr::CallN(arity, _) => {
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])),
}
}
}
@@ -238,7 +240,7 @@ impl Index<LocalCodePtr> for CodeRepo {
LocalCodePtr::TopLevel(_, p) => &self.cached_query[p],
LocalCodePtr::DirEntry(p) => &self.code[p],
LocalCodePtr::UserGoalExpansion(p) => &self.goal_expanders[p],
LocalCodePtr::UserTermExpansion(p) => &self.term_expanders[p]
LocalCodePtr::UserTermExpansion(p) => &self.term_expanders[p],
}
}
}
@@ -321,7 +323,11 @@ static ATTS: &str = include_str!("../lib/atts.pl");
impl Machine {
fn compile_special_forms(&mut self) {
match compile_special_form(self, VERIFY_ATTRS.as_bytes()) {
Ok(code) => self.code_repo.verify_attrs_code = code,
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")
}
}
@@ -580,6 +586,13 @@ impl MachineState {
if p < code_repo.in_situ_code.len() => {},
CodePtr::Local(_) =>
break,
CodePtr::VerifyAttrInterrupt(p) => {
self.verify_attributes();
self.num_of_args = 2;
self.b0 = self.b;
self.p = CodePtr::Local(LocalCodePtr::DirEntry(p));
},
_ => {}
};
}

View File

@@ -10,6 +10,7 @@ use prolog::num::{ToPrimitive, Zero};
use prolog::num::bigint::{BigInt};
use std::collections::HashSet;
use std::mem;
use std::rc::Rc;
struct BrentAlgState {
@@ -442,7 +443,17 @@ impl MachineState {
CWILCallPolicy.")
},
&SystemClauseType::RestoreCodePtrFromSpecialFormCP => {
self.p = self.special_form_cp.clone();
self.p = mem::replace(&mut self.attr_var_init.special_form_cp,
CodePtr::VerifyAttrInterrupt(self.attr_var_init.verify_attrs_loc));
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 {
self.heap[h] = HeapCellValue::Addr(addr);
}
return Ok(());
},
&SystemClauseType::RestoreCutPolicy => {