]> Repositorios git - scryer-prolog.git/commitdiff
add preliminary support for verify_attributes/3
authorMark Thom <[email protected]>
Fri, 8 Feb 2019 06:33:55 +0000 (23:33 -0700)
committerMark Thom <[email protected]>
Fri, 8 Feb 2019 06:33:55 +0000 (23:33 -0700)
src/prolog/compile.rs
src/prolog/instructions.rs
src/prolog/machine/attributed_variables.rs
src/prolog/machine/machine_state.rs
src/prolog/machine/machine_state_impl.rs
src/prolog/machine/mod.rs
src/prolog/machine/system_calls.rs

index 0c8053454a9c602f777abb3a522fde7c75d202aa..3e70a381c381b6f94e3904ab4c26fc47b2420784 100644 (file)
@@ -143,7 +143,7 @@ pub fn compile_term(wam: &mut Machine, packet: TopLevelPacket) -> EvalSession
                 Err(e) => EvalSession::from(e)
             },
         TopLevelPacket::Decl(TopLevel::Declaration(decl), _) => {
-            let mut compiler = ListingCompiler::new(&wam.code_repo, wam.code_size());
+            let mut compiler = ListingCompiler::new(&wam.code_repo);
             let indices = try_eval_session!(compile_decl(wam, &mut compiler, decl));
 
             try_eval_session!(compiler.add_code(wam, vec![], indices));
@@ -162,7 +162,6 @@ struct GatherResult {
 }
 
 pub struct ListingCompiler {
-    code_size_offset: usize,
     non_counted_bt_preds: HashSet<PredicateKey>,
     module: Option<Module>,
     user_term_dir: TermDir,
@@ -172,9 +171,8 @@ pub struct ListingCompiler {
 
 impl ListingCompiler {
     #[inline]
-    pub fn new(code_repo: &CodeRepo, code_size_offset: usize) -> Self {
+    pub fn new(code_repo: &CodeRepo) -> Self {
         ListingCompiler {
-            code_size_offset,
             non_counted_bt_preds: HashSet::new(),
             module: None,
             user_term_dir: TermDir::new(),
@@ -252,7 +250,7 @@ impl ListingCompiler {
 
             let non_counted_bt = self.non_counted_bt_preds.contains(&(name.clone(), arity));
 
-            let p = code.len() + self.code_size_offset;
+            let p = code.len() + wam.code_size();
             let mut decl_code = compile_relation(&TopLevel::Predicate(decl), non_counted_bt,
                                                  wam.machine_flags())?;
 
@@ -468,7 +466,7 @@ pub fn compile_special_form<R: Read>(wam: &mut Machine, src: R) -> Result<Code,
     let mut indices = default_index_store!(wam.indices.atom_tbl.clone());
     setup_indices(wam, &mut indices)?;
 
-    let mut compiler = ListingCompiler::new(&wam.code_repo, 0);
+    let mut compiler = ListingCompiler::new(&wam.code_repo);
     let results = compiler.gather_items(wam, src, &mut indices)?;
 
     compiler.generate_code(results.worker_results, wam, &mut indices.code_dir)
@@ -477,7 +475,7 @@ pub fn compile_special_form<R: Read>(wam: &mut Machine, src: R) -> Result<Code,
 #[inline]
 pub fn compile_listing<R: Read>(wam: &mut Machine, src: R, indices: IndexStore) -> EvalSession
 {
-    let mut compiler = ListingCompiler::new(&wam.code_repo, wam.code_size());
+    let mut compiler = ListingCompiler::new(&wam.code_repo);
 
     match compile_work(&mut compiler, wam, src, indices) {
         EvalSession::Error(e) => EvalSession::Error(compiler.drop_expansions(wam, e)),
index d56c1733475e3440fc51809a0e07e60e6ea53b09..bb5bc1bb038df5f5e8481096883f452bf1e055c0 100644 (file)
@@ -216,7 +216,6 @@ pub struct CodeRepo {
     pub(super) term_expanders: Code,
     pub(super) code: Code,
     pub(super) in_situ_code: Code,
-    pub(super) verify_attrs_code: Code,
     pub(super) term_dir: TermDir
 }
 
@@ -924,7 +923,8 @@ impl From<(usize, ClauseName)> for CodeIndex {
 pub enum CodePtr {
     BuiltInClause(BuiltInClauseType, LocalCodePtr), // local is the successor call.
     CallN(usize, LocalCodePtr), // arity, local.
-    Local(LocalCodePtr)
+    Local(LocalCodePtr),
+    VerifyAttrInterrupt(usize) // location of the verify attribute interrupt code in the CodeDir.
 }
 
 impl CodePtr {
@@ -932,7 +932,8 @@ impl CodePtr {
         match self {
             &CodePtr::BuiltInClause(_, ref local)
           | &CodePtr::CallN(_, ref local)
-          | &CodePtr::Local(ref local) => local.clone()
+          | &CodePtr::Local(ref local) => local.clone(),
+            &CodePtr::VerifyAttrInterrupt(p) => LocalCodePtr::DirEntry(p)
         }
     }
 }
@@ -946,7 +947,7 @@ pub enum LocalCodePtr {
     UserTermExpansion(usize)
 }
 
-impl LocalCodePtr {
+impl LocalCodePtr {    
     pub fn assign_if_local(&mut self, cp: CodePtr) {
         match cp {
             CodePtr::Local(local) => *self = local,
@@ -1023,6 +1024,7 @@ impl Add<usize> for CodePtr {
 
     fn add(self, rhs: usize) -> Self::Output {
         match self {
+            p @ CodePtr::VerifyAttrInterrupt(_) => p,
             CodePtr::Local(local) => CodePtr::Local(local + rhs),
             CodePtr::CallN(_, local) | CodePtr::BuiltInClause(_, local) => CodePtr::Local(local + rhs),
         }
@@ -1032,6 +1034,7 @@ impl Add<usize> for CodePtr {
 impl AddAssign<usize> for CodePtr {
     fn add_assign(&mut self, rhs: usize) {
         match self {
+            &mut CodePtr::VerifyAttrInterrupt(_) => {},
             &mut CodePtr::Local(ref mut local) => *local += rhs,
             _ => *self = CodePtr::Local(self.local() + rhs)
         }
@@ -1073,6 +1076,20 @@ impl Heap {
         self.heap.clear();
         self.h = 0;
     }
+
+    pub fn to_list<Iter: Iterator<Item=Addr>>(&mut self, values: Iter) -> usize {
+        let head_addr = self.h;
+
+        for value in values {
+            let h = self.h;
+
+            self.push(HeapCellValue::Addr(Addr::Lis(h+1)));
+            self.push(HeapCellValue::Addr(value));
+        }
+
+        self.push(HeapCellValue::Addr(Addr::Con(Constant::EmptyList)));
+        head_addr
+    }
 }
 
 impl Index<usize> for Heap {
index b36997f2818470f7f6637e2f8452cb6a81a826a1..191f0d4b90aca6e9e98dd21528c919c12d8eb96e 100644 (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;
+    }
+}
index 90ce7731a8aa9a8680d32b383b2375720adea787..ae81e94aff28f96ba6ad80853af3fbf808f509e5 100644 (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);
index 13d7143bf7874ea156e4c58a1873fb269672d70c..8c918e85561977f3b2b0383b3e8248d5469e0106 100644 (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;
index f9f185f9ae23c808bbb472db6b296a9aa6e83368..4990078d2f3e86addee314cc82de52c109838708 100644 (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));
+                },
                 _ => {}
             };
         }
index 04772e986184697bbb23fae8568d51aa7fb70493..556a2fdac3a783a3b2da9518515bcab54ab59709 100644 (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 => {