add copy_term/3 (#232)
This commit is contained in:
@@ -175,7 +175,7 @@ The following predicates are built-in to Scryer.
|
|||||||
* `clause/2`
|
* `clause/2`
|
||||||
* `compare/3`
|
* `compare/3`
|
||||||
* `compound/1`
|
* `compound/1`
|
||||||
* `copy_term/2`
|
* `copy_term/{2,3}`
|
||||||
* `current_predicate/1`
|
* `current_predicate/1`
|
||||||
* `current_op/3`
|
* `current_op/3`
|
||||||
* `cyclic_term/1`
|
* `cyclic_term/1`
|
||||||
|
|||||||
@@ -166,6 +166,7 @@ pub enum SystemClauseType {
|
|||||||
CharCode,
|
CharCode,
|
||||||
CharsToNumber,
|
CharsToNumber,
|
||||||
CodesToNumber,
|
CodesToNumber,
|
||||||
|
CopyTermWithoutAttrVars,
|
||||||
CheckCutPoint,
|
CheckCutPoint,
|
||||||
CopyToLiftedHeap,
|
CopyToLiftedHeap,
|
||||||
DeleteAttribute,
|
DeleteAttribute,
|
||||||
@@ -264,6 +265,7 @@ impl SystemClauseType {
|
|||||||
&SystemClauseType::CharCode => clause_name!("$char_code"),
|
&SystemClauseType::CharCode => clause_name!("$char_code"),
|
||||||
&SystemClauseType::CharsToNumber => clause_name!("$chars_to_number"),
|
&SystemClauseType::CharsToNumber => clause_name!("$chars_to_number"),
|
||||||
&SystemClauseType::CodesToNumber => clause_name!("$codes_to_number"),
|
&SystemClauseType::CodesToNumber => clause_name!("$codes_to_number"),
|
||||||
|
&SystemClauseType::CopyTermWithoutAttrVars => clause_name!("$copy_term_without_attr_vars"),
|
||||||
&SystemClauseType::CheckCutPoint => clause_name!("$check_cp"),
|
&SystemClauseType::CheckCutPoint => clause_name!("$check_cp"),
|
||||||
&SystemClauseType::REPL(REPLCodePtr::CompileBatch) => clause_name!("$compile_batch"),
|
&SystemClauseType::REPL(REPLCodePtr::CompileBatch) => clause_name!("$compile_batch"),
|
||||||
&SystemClauseType::REPL(REPLCodePtr::UseModule) => clause_name!("$use_module"),
|
&SystemClauseType::REPL(REPLCodePtr::UseModule) => clause_name!("$use_module"),
|
||||||
@@ -392,6 +394,7 @@ impl SystemClauseType {
|
|||||||
("$char_code", 2) => Some(SystemClauseType::CharCode),
|
("$char_code", 2) => Some(SystemClauseType::CharCode),
|
||||||
("$chars_to_number", 2) => Some(SystemClauseType::CharsToNumber),
|
("$chars_to_number", 2) => Some(SystemClauseType::CharsToNumber),
|
||||||
("$codes_to_number", 2) => Some(SystemClauseType::CodesToNumber),
|
("$codes_to_number", 2) => Some(SystemClauseType::CodesToNumber),
|
||||||
|
("$copy_term_without_attr_vars", 2) => Some(SystemClauseType::CopyTermWithoutAttrVars),
|
||||||
("$check_cp", 1) => Some(SystemClauseType::CheckCutPoint),
|
("$check_cp", 1) => Some(SystemClauseType::CheckCutPoint),
|
||||||
("$compile_batch", 0) => Some(SystemClauseType::REPL(REPLCodePtr::CompileBatch)),
|
("$compile_batch", 0) => Some(SystemClauseType::REPL(REPLCodePtr::CompileBatch)),
|
||||||
("$copy_to_lh", 2) => Some(SystemClauseType::CopyToLiftedHeap),
|
("$copy_to_lh", 2) => Some(SystemClauseType::CopyToLiftedHeap),
|
||||||
|
|||||||
@@ -149,3 +149,4 @@ call_residue_vars(Goal, Vars) :-
|
|||||||
'$get_attr_var_queue_delim'(B),
|
'$get_attr_var_queue_delim'(B),
|
||||||
call(Goal),
|
call(Goal),
|
||||||
'$get_attr_var_queue_beyond'(B, Vars).
|
'$get_attr_var_queue_beyond'(B, Vars).
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,12 @@ use std::ops::IndexMut;
|
|||||||
|
|
||||||
type Trail = Vec<(Ref, HeapCellValue)>;
|
type Trail = Vec<(Ref, HeapCellValue)>;
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
pub enum AttrVarPolicy {
|
||||||
|
DeepCopy,
|
||||||
|
StripAttributes
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) trait CopierTarget: IndexMut<usize, Output = HeapCellValue> {
|
pub(crate) trait CopierTarget: IndexMut<usize, Output = HeapCellValue> {
|
||||||
fn threshold(&self) -> usize;
|
fn threshold(&self) -> usize;
|
||||||
fn push(&mut self, _: HeapCellValue);
|
fn push(&mut self, _: HeapCellValue);
|
||||||
@@ -13,9 +19,10 @@ pub(crate) trait CopierTarget: IndexMut<usize, Output = HeapCellValue> {
|
|||||||
fn stack(&mut self) -> &mut AndStack;
|
fn stack(&mut self) -> &mut AndStack;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn copy_term<T: CopierTarget>(target: T, addr: Addr) {
|
pub(crate)
|
||||||
let mut copy_term_state = CopyTermState::new(target);
|
fn copy_term<T: CopierTarget>(target: T, addr: Addr, attr_var_policy: AttrVarPolicy) {
|
||||||
copy_term_state.copy_term_impl(addr);
|
let mut copy_term_state = CopyTermState::new(target, attr_var_policy);
|
||||||
|
copy_term_state.copy_term_impl(addr);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct CopyTermState<T: CopierTarget> {
|
struct CopyTermState<T: CopierTarget> {
|
||||||
@@ -23,15 +30,17 @@ struct CopyTermState<T: CopierTarget> {
|
|||||||
scan: usize,
|
scan: usize,
|
||||||
old_h: usize,
|
old_h: usize,
|
||||||
target: T,
|
target: T,
|
||||||
|
attr_var_policy: AttrVarPolicy
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: CopierTarget> CopyTermState<T> {
|
impl<T: CopierTarget> CopyTermState<T> {
|
||||||
fn new(target: T) -> Self {
|
fn new(target: T, attr_var_policy: AttrVarPolicy) -> Self {
|
||||||
CopyTermState {
|
CopyTermState {
|
||||||
trail: vec![],
|
trail: vec![],
|
||||||
scan: 0,
|
scan: 0,
|
||||||
old_h: target.threshold(),
|
old_h: target.threshold(),
|
||||||
target,
|
target,
|
||||||
|
attr_var_policy
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,6 +50,14 @@ impl<T: CopierTarget> CopyTermState<T> {
|
|||||||
&mut self.target[scan]
|
&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) {
|
fn reinstantiate_var(&mut self, addr: Addr, threshold: usize) {
|
||||||
match addr {
|
match addr {
|
||||||
Addr::HeapCell(h) => {
|
Addr::HeapCell(h) => {
|
||||||
@@ -58,8 +75,10 @@ impl<T: CopierTarget> CopyTermState<T> {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
Addr::AttrVar(h) => {
|
Addr::AttrVar(h) => {
|
||||||
self.target[threshold] = HeapCellValue::Addr(Addr::AttrVar(threshold));
|
let redirect_tag = self.attr_var_redirect_tag();
|
||||||
self.target[h] = HeapCellValue::Addr(Addr::AttrVar(threshold));
|
|
||||||
|
self.target[threshold] = HeapCellValue::Addr(redirect_tag(threshold));
|
||||||
|
self.target[h] = HeapCellValue::Addr(redirect_tag(threshold));
|
||||||
self.trail
|
self.trail
|
||||||
.push((Ref::AttrVar(h), HeapCellValue::Addr(Addr::AttrVar(h))));
|
.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));
|
let rd = self.target.store(self.target.deref(ra));
|
||||||
|
|
||||||
match rd.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.target[threshold] = HeapCellValue::Addr(redirect_tag(h));
|
||||||
|
}
|
||||||
|
Addr::HeapCell(h) if h >= self.old_h => {
|
||||||
self.target[threshold] = HeapCellValue::Addr(rd)
|
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 {
|
if ra == rd {
|
||||||
self.reinstantiate_var(ra, threshold);
|
self.reinstantiate_var(ra, threshold);
|
||||||
} else {
|
} else {
|
||||||
@@ -121,20 +152,29 @@ impl<T: CopierTarget> CopyTermState<T> {
|
|||||||
let rd = self.target.store(self.target.deref(addr.clone()));
|
let rd = self.target.store(self.target.deref(addr.clone()));
|
||||||
|
|
||||||
match rd.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.value_at_scan() = HeapCellValue::Addr(rd);
|
||||||
self.scan += 1;
|
self.scan += 1;
|
||||||
}
|
}
|
||||||
Addr::AttrVar(h) if addr == rd => {
|
Addr::AttrVar(h) if addr == rd => {
|
||||||
|
let redirect_tag = self.attr_var_redirect_tag();
|
||||||
let threshold = self.target.threshold();
|
let threshold = self.target.threshold();
|
||||||
self.target
|
|
||||||
.push(HeapCellValue::Addr(Addr::AttrVar(threshold)));
|
|
||||||
|
|
||||||
let list_val = self.target[h + 1].clone();
|
self.target
|
||||||
self.target.push(list_val);
|
.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.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 => {
|
_ if addr == rd => {
|
||||||
let scan = self.scan;
|
let scan = self.scan;
|
||||||
@@ -185,8 +225,8 @@ impl<T: CopierTarget> CopyTermState<T> {
|
|||||||
HeapCellValue::Addr(addr) => match addr {
|
HeapCellValue::Addr(addr) => match addr {
|
||||||
Addr::Lis(addr) => self.copy_list(addr),
|
Addr::Lis(addr) => self.copy_list(addr),
|
||||||
addr @ Addr::AttrVar(_)
|
addr @ Addr::AttrVar(_)
|
||||||
| addr @ Addr::HeapCell(_)
|
| addr @ Addr::HeapCell(_)
|
||||||
| addr @ Addr::StackCell(..) => self.copy_var(addr),
|
| addr @ Addr::StackCell(..) => self.copy_var(addr),
|
||||||
Addr::Str(addr) => self.copy_structure(addr),
|
Addr::Str(addr) => self.copy_structure(addr),
|
||||||
Addr::Con(_) | Addr::DBRef(_) => self.scan += 1,
|
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)
|
return_from_clause!(machine_st.last_call, machine_st)
|
||||||
}
|
}
|
||||||
&BuiltInClauseType::CopyTerm => {
|
&BuiltInClauseType::CopyTerm => {
|
||||||
machine_st.copy_term();
|
machine_st.copy_term(AttrVarPolicy::DeepCopy);
|
||||||
return_from_clause!(machine_st.last_call, machine_st)
|
return_from_clause!(machine_st.last_call, machine_st)
|
||||||
}
|
}
|
||||||
&BuiltInClauseType::Eq => {
|
&BuiltInClauseType::Eq => {
|
||||||
|
|||||||
@@ -2044,6 +2044,7 @@ impl MachineState {
|
|||||||
copy_term(
|
copy_term(
|
||||||
CopyBallTerm::new(&mut self.and_stack, &mut self.heap, &mut self.ball.stub),
|
CopyBallTerm::new(&mut self.and_stack, &mut self.heap, &mut self.ball.stub),
|
||||||
addr,
|
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 old_h = self.heap.h;
|
||||||
|
|
||||||
let a1 = self[temp_v!(1)].clone();
|
let a1 = self[temp_v!(1)].clone();
|
||||||
let a2 = self[temp_v!(2)].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);
|
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) :-
|
gather_modules_for_attrs([Attr|Attrs], [Module|Modules], Modules0) :-
|
||||||
'$module_of'(Module, Attr),
|
'$module_of'(Module, Attr),
|
||||||
gather_modules_for_attrs(Attrs, Modules, Modules0).
|
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 + 3)));
|
||||||
copy_ball_term.push(HeapCellValue::Addr(Addr::HeapCell(threshold + 2)));
|
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
|
threshold + lh_offset + 2
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -867,6 +867,9 @@ impl MachineState {
|
|||||||
_ => self.fail = true,
|
_ => self.fail = true,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
&SystemClauseType::CopyTermWithoutAttrVars => {
|
||||||
|
self.copy_term(AttrVarPolicy::StripAttributes);
|
||||||
|
}
|
||||||
&SystemClauseType::FetchGlobalVar => {
|
&SystemClauseType::FetchGlobalVar => {
|
||||||
let key = self[temp_v!(1)].clone();
|
let key = self[temp_v!(1)].clone();
|
||||||
|
|
||||||
@@ -1364,7 +1367,7 @@ impl MachineState {
|
|||||||
self.truncate_if_no_lifted_heap_diff(|_| Addr::Con(Constant::EmptyList))
|
self.truncate_if_no_lifted_heap_diff(|_| Addr::Con(Constant::EmptyList))
|
||||||
}
|
}
|
||||||
&SystemClauseType::FetchAttributeGoals => {
|
&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));
|
attr_goals.sort_unstable_by(|a1, a2| self.compare_term_test(a1, a2));
|
||||||
self.term_dedup(&mut attr_goals);
|
self.term_dedup(&mut attr_goals);
|
||||||
@@ -1667,6 +1670,7 @@ impl MachineState {
|
|||||||
copy_term(
|
copy_term(
|
||||||
CopyBallTerm::new(&mut self.and_stack, &mut self.heap, &mut ball.stub),
|
CopyBallTerm::new(&mut self.and_stack, &mut self.heap, &mut ball.stub),
|
||||||
value,
|
value,
|
||||||
|
AttrVarPolicy::DeepCopy,
|
||||||
);
|
);
|
||||||
|
|
||||||
let offset = self[temp_v!(3)].clone();
|
let offset = self[temp_v!(3)].clone();
|
||||||
@@ -1905,7 +1909,7 @@ impl MachineState {
|
|||||||
ContinueResult::ContinueQuery => ';',
|
ContinueResult::ContinueQuery => ';',
|
||||||
ContinueResult::Conclude => '.'
|
ContinueResult::Conclude => '.'
|
||||||
};
|
};
|
||||||
|
|
||||||
let target = self[temp_v!(1)].clone();
|
let target = self[temp_v!(1)].clone();
|
||||||
self.unify(Addr::Con(Constant::Char(c)), target);
|
self.unify(Addr::Con(Constant::Char(c)), target);
|
||||||
}
|
}
|
||||||
@@ -1970,6 +1974,7 @@ impl MachineState {
|
|||||||
copy_term(
|
copy_term(
|
||||||
CopyBallTerm::new(&mut self.and_stack, &mut self.heap, &mut ball.stub),
|
CopyBallTerm::new(&mut self.and_stack, &mut self.heap, &mut ball.stub),
|
||||||
value,
|
value,
|
||||||
|
AttrVarPolicy::DeepCopy,
|
||||||
);
|
);
|
||||||
|
|
||||||
indices.global_variables.insert(key, (ball, None));
|
indices.global_variables.insert(key, (ball, None));
|
||||||
@@ -1990,6 +1995,7 @@ impl MachineState {
|
|||||||
copy_term(
|
copy_term(
|
||||||
CopyBallTerm::new(&mut self.and_stack, &mut self.heap, &mut ball.stub),
|
CopyBallTerm::new(&mut self.and_stack, &mut self.heap, &mut ball.stub),
|
||||||
value.clone(),
|
value.clone(),
|
||||||
|
AttrVarPolicy::DeepCopy,
|
||||||
);
|
);
|
||||||
|
|
||||||
let stub = ball.copy_and_align(h);
|
let stub = ball.copy_and_align(h);
|
||||||
|
|||||||
Reference in New Issue
Block a user