add copy_term/3 (#232)
This commit is contained in:
@@ -5,6 +5,12 @@ use std::ops::IndexMut;
|
||||
|
||||
type Trail = Vec<(Ref, HeapCellValue)>;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum AttrVarPolicy {
|
||||
DeepCopy,
|
||||
StripAttributes
|
||||
}
|
||||
|
||||
pub(crate) trait CopierTarget: IndexMut<usize, Output = HeapCellValue> {
|
||||
fn threshold(&self) -> usize;
|
||||
fn push(&mut self, _: HeapCellValue);
|
||||
@@ -13,9 +19,10 @@ pub(crate) trait CopierTarget: IndexMut<usize, Output = HeapCellValue> {
|
||||
fn stack(&mut self) -> &mut AndStack;
|
||||
}
|
||||
|
||||
pub(crate) fn copy_term<T: CopierTarget>(target: T, addr: Addr) {
|
||||
let mut copy_term_state = CopyTermState::new(target);
|
||||
copy_term_state.copy_term_impl(addr);
|
||||
pub(crate)
|
||||
fn copy_term<T: CopierTarget>(target: T, addr: Addr, attr_var_policy: AttrVarPolicy) {
|
||||
let mut copy_term_state = CopyTermState::new(target, attr_var_policy);
|
||||
copy_term_state.copy_term_impl(addr);
|
||||
}
|
||||
|
||||
struct CopyTermState<T: CopierTarget> {
|
||||
@@ -23,15 +30,17 @@ struct CopyTermState<T: CopierTarget> {
|
||||
scan: usize,
|
||||
old_h: usize,
|
||||
target: T,
|
||||
attr_var_policy: AttrVarPolicy
|
||||
}
|
||||
|
||||
impl<T: CopierTarget> CopyTermState<T> {
|
||||
fn new(target: T) -> Self {
|
||||
fn new(target: T, attr_var_policy: AttrVarPolicy) -> Self {
|
||||
CopyTermState {
|
||||
trail: vec![],
|
||||
scan: 0,
|
||||
old_h: target.threshold(),
|
||||
target,
|
||||
attr_var_policy
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,6 +50,14 @@ impl<T: CopierTarget> CopyTermState<T> {
|
||||
&mut self.target[scan]
|
||||
}
|
||||
|
||||
fn attr_var_redirect_tag(&self) -> impl Fn(usize) -> Addr {
|
||||
if let AttrVarPolicy::DeepCopy = self.attr_var_policy {
|
||||
Addr::AttrVar
|
||||
} else {
|
||||
Addr::HeapCell
|
||||
}
|
||||
}
|
||||
|
||||
fn reinstantiate_var(&mut self, addr: Addr, threshold: usize) {
|
||||
match addr {
|
||||
Addr::HeapCell(h) => {
|
||||
@@ -58,8 +75,10 @@ impl<T: CopierTarget> CopyTermState<T> {
|
||||
));
|
||||
}
|
||||
Addr::AttrVar(h) => {
|
||||
self.target[threshold] = HeapCellValue::Addr(Addr::AttrVar(threshold));
|
||||
self.target[h] = HeapCellValue::Addr(Addr::AttrVar(threshold));
|
||||
let redirect_tag = self.attr_var_redirect_tag();
|
||||
|
||||
self.target[threshold] = HeapCellValue::Addr(redirect_tag(threshold));
|
||||
self.target[h] = HeapCellValue::Addr(redirect_tag(threshold));
|
||||
self.trail
|
||||
.push((Ref::AttrVar(h), HeapCellValue::Addr(Addr::AttrVar(h))));
|
||||
}
|
||||
@@ -94,10 +113,22 @@ impl<T: CopierTarget> CopyTermState<T> {
|
||||
let rd = self.target.store(self.target.deref(ra));
|
||||
|
||||
match rd.clone() {
|
||||
Addr::AttrVar(h) | Addr::HeapCell(h) if h >= self.old_h => {
|
||||
Addr::AttrVar(h) if h >= self.old_h => {
|
||||
let redirect_tag = self.attr_var_redirect_tag();
|
||||
self.target[threshold] = HeapCellValue::Addr(redirect_tag(h));
|
||||
}
|
||||
Addr::HeapCell(h) if h >= self.old_h => {
|
||||
self.target[threshold] = HeapCellValue::Addr(rd)
|
||||
}
|
||||
ra @ Addr::AttrVar(_) | ra @ Addr::HeapCell(..) | ra @ Addr::StackCell(..) => {
|
||||
Addr::AttrVar(h) => {
|
||||
if Addr::AttrVar(h) == rd {
|
||||
self.reinstantiate_var(Addr::AttrVar(h), threshold);
|
||||
} else {
|
||||
let redirect_tag = self.attr_var_redirect_tag();
|
||||
self.target[threshold] = HeapCellValue::Addr(redirect_tag(h));
|
||||
}
|
||||
}
|
||||
ra @ Addr::HeapCell(..) | ra @ Addr::StackCell(..) => {
|
||||
if ra == rd {
|
||||
self.reinstantiate_var(ra, threshold);
|
||||
} else {
|
||||
@@ -121,20 +152,29 @@ impl<T: CopierTarget> CopyTermState<T> {
|
||||
let rd = self.target.store(self.target.deref(addr.clone()));
|
||||
|
||||
match rd.clone() {
|
||||
Addr::AttrVar(h) | Addr::HeapCell(h) if h >= self.old_h => {
|
||||
Addr::AttrVar(h) if h >= self.old_h => {
|
||||
let redirect_tag = self.attr_var_redirect_tag();
|
||||
*self.value_at_scan() = HeapCellValue::Addr(redirect_tag(h));
|
||||
self.scan += 1;
|
||||
}
|
||||
Addr::HeapCell(h) if h >= self.old_h => {
|
||||
*self.value_at_scan() = HeapCellValue::Addr(rd);
|
||||
self.scan += 1;
|
||||
}
|
||||
Addr::AttrVar(h) if addr == rd => {
|
||||
let redirect_tag = self.attr_var_redirect_tag();
|
||||
let threshold = self.target.threshold();
|
||||
self.target
|
||||
.push(HeapCellValue::Addr(Addr::AttrVar(threshold)));
|
||||
|
||||
let list_val = self.target[h + 1].clone();
|
||||
self.target.push(list_val);
|
||||
self.target
|
||||
.push(HeapCellValue::Addr(redirect_tag(threshold)));
|
||||
|
||||
if let Addr::AttrVar(_) = redirect_tag(threshold) {
|
||||
let list_val = self.target[h + 1].clone();
|
||||
self.target.push(list_val);
|
||||
}
|
||||
|
||||
self.reinstantiate_var(addr, threshold);
|
||||
*self.value_at_scan() = HeapCellValue::Addr(Addr::AttrVar(threshold));
|
||||
*self.value_at_scan() = HeapCellValue::Addr(redirect_tag(threshold));
|
||||
}
|
||||
_ if addr == rd => {
|
||||
let scan = self.scan;
|
||||
@@ -185,8 +225,8 @@ impl<T: CopierTarget> CopyTermState<T> {
|
||||
HeapCellValue::Addr(addr) => match addr {
|
||||
Addr::Lis(addr) => self.copy_list(addr),
|
||||
addr @ Addr::AttrVar(_)
|
||||
| addr @ Addr::HeapCell(_)
|
||||
| addr @ Addr::StackCell(..) => self.copy_var(addr),
|
||||
| addr @ Addr::HeapCell(_)
|
||||
| addr @ Addr::StackCell(..) => self.copy_var(addr),
|
||||
Addr::Str(addr) => self.copy_structure(addr),
|
||||
Addr::Con(_) | Addr::DBRef(_) => self.scan += 1,
|
||||
},
|
||||
|
||||
@@ -729,7 +729,7 @@ pub(crate) trait CallPolicy: Any {
|
||||
return_from_clause!(machine_st.last_call, machine_st)
|
||||
}
|
||||
&BuiltInClauseType::CopyTerm => {
|
||||
machine_st.copy_term();
|
||||
machine_st.copy_term(AttrVarPolicy::DeepCopy);
|
||||
return_from_clause!(machine_st.last_call, machine_st)
|
||||
}
|
||||
&BuiltInClauseType::Eq => {
|
||||
|
||||
@@ -2044,6 +2044,7 @@ impl MachineState {
|
||||
copy_term(
|
||||
CopyBallTerm::new(&mut self.and_stack, &mut self.heap, &mut self.ball.stub),
|
||||
addr,
|
||||
AttrVarPolicy::DeepCopy,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2941,13 +2942,13 @@ impl MachineState {
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn copy_term(&mut self) {
|
||||
pub(super) fn copy_term(&mut self, attr_var_policy: AttrVarPolicy) {
|
||||
let old_h = self.heap.h;
|
||||
|
||||
let a1 = self[temp_v!(1)].clone();
|
||||
let a2 = self[temp_v!(2)].clone();
|
||||
|
||||
copy_term(CopyTerm::new(self), a1);
|
||||
copy_term(CopyTerm::new(self), a1, attr_var_policy);
|
||||
self.unify(Addr::HeapCell(old_h), a2);
|
||||
}
|
||||
|
||||
|
||||
@@ -83,3 +83,10 @@ gather_modules_for_attrs(Attrs, Modules, Modules) :-
|
||||
gather_modules_for_attrs([Attr|Attrs], [Module|Modules], Modules0) :-
|
||||
'$module_of'(Module, Attr),
|
||||
gather_modules_for_attrs(Attrs, Modules, Modules0).
|
||||
|
||||
copy_term(Source, Dest, Goals) :-
|
||||
term_variables(Source, Vars),
|
||||
gather_modules(Vars, Modules, _),
|
||||
call_attribute_goals(Modules, call_query_var_goals, Vars),
|
||||
'$fetch_attribute_goals'(Goals0),
|
||||
'$copy_term_without_attr_vars'([Source | Goals0], [Dest | Goals]).
|
||||
|
||||
@@ -353,7 +353,7 @@ impl MachineState {
|
||||
copy_ball_term.push(HeapCellValue::Addr(Addr::HeapCell(threshold + 3)));
|
||||
copy_ball_term.push(HeapCellValue::Addr(Addr::HeapCell(threshold + 2)));
|
||||
|
||||
copy_term(copy_ball_term, copy_target);
|
||||
copy_term(copy_ball_term, copy_target, AttrVarPolicy::DeepCopy);
|
||||
threshold + lh_offset + 2
|
||||
}
|
||||
|
||||
@@ -867,6 +867,9 @@ impl MachineState {
|
||||
_ => self.fail = true,
|
||||
};
|
||||
}
|
||||
&SystemClauseType::CopyTermWithoutAttrVars => {
|
||||
self.copy_term(AttrVarPolicy::StripAttributes);
|
||||
}
|
||||
&SystemClauseType::FetchGlobalVar => {
|
||||
let key = self[temp_v!(1)].clone();
|
||||
|
||||
@@ -1364,7 +1367,7 @@ impl MachineState {
|
||||
self.truncate_if_no_lifted_heap_diff(|_| Addr::Con(Constant::EmptyList))
|
||||
}
|
||||
&SystemClauseType::FetchAttributeGoals => {
|
||||
let mut attr_goals = mem::replace(&mut self.attr_var_init.attribute_goals, vec![]);
|
||||
let mut attr_goals = self.attr_var_init.attribute_goals.clone();
|
||||
|
||||
attr_goals.sort_unstable_by(|a1, a2| self.compare_term_test(a1, a2));
|
||||
self.term_dedup(&mut attr_goals);
|
||||
@@ -1667,6 +1670,7 @@ impl MachineState {
|
||||
copy_term(
|
||||
CopyBallTerm::new(&mut self.and_stack, &mut self.heap, &mut ball.stub),
|
||||
value,
|
||||
AttrVarPolicy::DeepCopy,
|
||||
);
|
||||
|
||||
let offset = self[temp_v!(3)].clone();
|
||||
@@ -1905,7 +1909,7 @@ impl MachineState {
|
||||
ContinueResult::ContinueQuery => ';',
|
||||
ContinueResult::Conclude => '.'
|
||||
};
|
||||
|
||||
|
||||
let target = self[temp_v!(1)].clone();
|
||||
self.unify(Addr::Con(Constant::Char(c)), target);
|
||||
}
|
||||
@@ -1970,6 +1974,7 @@ impl MachineState {
|
||||
copy_term(
|
||||
CopyBallTerm::new(&mut self.and_stack, &mut self.heap, &mut ball.stub),
|
||||
value,
|
||||
AttrVarPolicy::DeepCopy,
|
||||
);
|
||||
|
||||
indices.global_variables.insert(key, (ball, None));
|
||||
@@ -1990,6 +1995,7 @@ impl MachineState {
|
||||
copy_term(
|
||||
CopyBallTerm::new(&mut self.and_stack, &mut self.heap, &mut ball.stub),
|
||||
value.clone(),
|
||||
AttrVarPolicy::DeepCopy,
|
||||
);
|
||||
|
||||
let stub = ball.copy_and_align(h);
|
||||
|
||||
Reference in New Issue
Block a user