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));
}
pub struct ListingCompiler {
- code_size_offset: usize,
non_counted_bt_preds: HashSet<PredicateKey>,
module: Option<Module>,
user_term_dir: TermDir,
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(),
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())?;
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)
#[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)),
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
}
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 {
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)
}
}
}
UserTermExpansion(usize)
}
-impl LocalCodePtr {
+impl LocalCodePtr {
pub fn assign_if_local(&mut self, cp: CodePtr) {
match cp {
CodePtr::Local(local) => *self = local,
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),
}
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)
}
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 {
+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;
+ }
+}
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::*;
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,
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)
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 {
} 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));
}
}
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);
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);
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};
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,
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)));
}
}
}
*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>
{
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;
term_expanders: Code::new(),
code: Code::new(),
in_situ_code: Code::new(),
- verify_attrs_code: Code::new(),
term_dir: TermDir::new()
}
}
&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])),
+
}
}
}
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],
}
}
}
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")
}
}
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));
+ },
_ => {}
};
}
use prolog::num::bigint::{BigInt};
use std::collections::HashSet;
+use std::mem;
use std::rc::Rc;
struct BrentAlgState {
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 => {