fix copier

This commit is contained in:
Mark Thom
2018-09-16 01:37:42 -06:00
parent 38bb7c7dec
commit 61fe42a2ad
4 changed files with 85 additions and 26 deletions

View File

@@ -3,12 +3,20 @@ use prolog::instructions::*;
use std::ops::IndexMut;
//type ListTrail = HashMap<usize, usize>;
type Trail = Vec<(Ref, HeapCellValue)>;
pub(crate) struct RedirectInfo {
//list_trail: ListTrail,
trail: Trail
}
/*
impl RedirectInfo {
fn new() -> Self {
RedirectInfo { list_trail: ListTrail::new(), trail: Trail::new() }
}
}
*/
pub(crate) trait CopierTarget
{
fn source(&self) -> usize;
@@ -29,12 +37,29 @@ pub(crate) trait CopierTarget
}
}
fn reinstantiate_var(&mut self, ra: Addr, scan: usize, trail: &mut Trail)
where Self: IndexMut<usize, Output=HeapCellValue>
{
self[scan] = HeapCellValue::Addr(Addr::HeapCell(scan));
if let Addr::HeapCell(hc) = ra.clone() {
self[hc] = HeapCellValue::Addr(Addr::HeapCell(scan));
trail.push((Ref::HeapCell(hc),
HeapCellValue::Addr(Addr::HeapCell(hc))));
} else if let Addr::StackCell(fr, sc) = ra {
self.stack()[fr][sc] = Addr::HeapCell(scan);
trail.push((Ref::StackCell(fr, sc),
HeapCellValue::Addr(Addr::StackCell(fr, sc))));
}
}
// duplicate_term_impl(L1, L2) uses Cheney's algorithm to copy the term
// at L1 to L2. trail is kept to restore the innards of L1 after
// it's been copied to L2.
fn duplicate_term_impl(&mut self, addr: Addr) -> RedirectInfo
where Self: IndexMut<usize, Output=HeapCellValue>
{
// let mut redirect_info = RedirectInfo::new();
let mut trail = Trail::new();
let mut scan = self.source();
let old_h = self.threshold();
@@ -58,16 +83,36 @@ pub(crate) trait CopierTarget
let threshold = self.threshold();
self[scan] = HeapCellValue::Addr(Addr::Lis(threshold));
let hcv = self[a].clone();
self.push(hcv);
self.push(hcv.clone());
let ra = hcv.as_addr(threshold);
let rd = self.store(self.deref(ra));
match rd.clone() {
Addr::HeapCell(hc) if hc >= old_h => {
self[threshold] = HeapCellValue::Addr(rd);
scan += 1;
},
addr @ Addr::HeapCell(..) | addr @ Addr::StackCell(..) => {
if rd == addr {
self.reinstantiate_var(addr, threshold, &mut trail);
} else {
self[threshold] = HeapCellValue::Addr(addr);
}
scan += 1;
},
_ => {
trail.push((Ref::HeapCell(a), self[a].clone()));
self[a] = HeapCellValue::Addr(Addr::Lis(threshold))
}
};
let hcv = self[a+1].clone();
self.push(hcv);
trail.push((Ref::HeapCell(a), self[a].clone()));
self[a] = HeapCellValue::Addr(Addr::Lis(threshold));
scan += 1;
},
Addr::HeapCell(_) | Addr::StackCell(_, _) => {
@@ -80,18 +125,7 @@ pub(crate) trait CopierTarget
scan += 1;
},
_ if ra == rd => {
self[scan] = HeapCellValue::Addr(Addr::HeapCell(scan));
if let Addr::HeapCell(hc) = ra.clone() {
self[hc] = HeapCellValue::Addr(Addr::HeapCell(scan));
trail.push((Ref::HeapCell(hc),
HeapCellValue::Addr(Addr::HeapCell(hc))));
} else if let Addr::StackCell(fr, sc) = ra {
self.stack()[fr][sc] = Addr::HeapCell(scan);
trail.push((Ref::StackCell(fr, sc),
HeapCellValue::Addr(Addr::StackCell(fr, sc))));
}
self.reinstantiate_var(ra, scan, &mut trail);
scan += 1;
},
_ => self[scan] = HeapCellValue::Addr(rd)

View File

@@ -168,12 +168,31 @@ impl<'a> CopierTarget for DuplicateBallTerm<'a> {
self.state.ball.stub.push(hcv);
}
fn store(&self, a: Addr) -> Addr {
self.state.store(a)
fn store(&self, addr: Addr) -> Addr {
match addr {
Addr::HeapCell(hc) if hc < self.heap_boundary =>
self.state.heap[hc].as_addr(hc),
Addr::HeapCell(hc) => {
let index = hc - self.heap_boundary;
self.state.ball.stub[index].as_addr(hc)
},
Addr::StackCell(fr, sc) =>
self.state.and_stack[fr][sc].clone(),
addr => addr
}
}
fn deref(&self, a: Addr) -> Addr {
self.state.deref(a)
fn deref(&self, mut addr: Addr) -> Addr {
loop {
let value = self.store(addr.clone());
if value.is_ref() && value != addr {
addr = value;
continue;
}
return addr;
};
}
fn stack(&mut self) -> &mut AndStack {