finalize transition to CutPolicy trait object.

This commit is contained in:
Mark Thom
2018-02-07 21:46:40 -07:00
parent 1bf9c0d205
commit 1b4d1c04c9
6 changed files with 62 additions and 35 deletions

View File

@@ -818,6 +818,7 @@ pub enum BuiltInInstruction {
IsString(RegType), IsString(RegType),
IsVar(RegType), IsVar(RegType),
ResetBlock, ResetBlock,
RestoreCutPolicy,
SetBall, SetBall,
SetCutPoint(RegType), SetCutPoint(RegType),
Succeed, Succeed,

View File

@@ -475,25 +475,27 @@ fn get_builtins(atom_tbl: TabledData<Atom>) -> Code {
query![put_value!(perm_v!(3), 1)], query![put_value!(perm_v!(3), 1)],
call_n!(1), call_n!(1),
query![put_value!(perm_v!(2), 1), query![put_value!(perm_v!(2), 1),
put_unsafe_value!(1, 2)], put_value!(perm_v!(1), 2)],
deallocate!(), deallocate!(),
check_cp_execute!(), check_cp_execute!(),
retry_me_else!(10), retry_me_else!(12),
allocate!(1), allocate!(2),
query![put_value!(temp_v!(3), 1)], query![put_value!(temp_v!(3), 1)],
reset_block!(), reset_block!(),
query![put_var!(perm_v!(1), 1)], query![put_var!(perm_v!(1), 1)],
get_ball!(), get_ball!(),
goto_call!(340, 0), // goto run_cleaners_with_handling/0, 340. get_level!(perm_v!(2)),
query![put_value!(perm_v!(1), 1)], set_cp!(perm_v!(2)),
goto_call!(342, 0), // goto run_cleaners_with_handling/0, 342.
query![put_unsafe_value!(1, 1)],
deallocate!(), deallocate!(),
goto_execute!(59, 1), goto_execute!(59, 1), // goto throw/1, 59.
trust_me!(), trust_me!(),
allocate!(0), allocate!(0),
goto_call!(352, 0), // goto run_cleaners_without_handling/0, 352. goto_call!(354, 0), // goto run_cleaners_without_handling/0, 354.
deallocate!(), deallocate!(),
fail!(), fail!(),
try_me_else!(10), // run_cleaners_with_handling/0, 340. try_me_else!(10), // run_cleaners_with_handling/0, 342.
allocate!(2), allocate!(2),
get_level!(perm_v!(1)), get_level!(perm_v!(1)),
query![put_var!(perm_v!(2), 1)], query![put_var!(perm_v!(2), 1)],
@@ -502,39 +504,41 @@ fn get_builtins(atom_tbl: TabledData<Atom>) -> Code {
put_var!(temp_v!(4), 2), put_var!(temp_v!(4), 2),
put_constant!(Level::Shallow, atom!("true", atom_tbl), temp_v!(3))], put_constant!(Level::Shallow, atom!("true", atom_tbl), temp_v!(3))],
goto_call!(5, 3), // goto catch/3, 5. goto_call!(5, 3), // goto catch/3, 5.
cut!(perm_v!(1)), set_cp!(perm_v!(1)),
deallocate!(), deallocate!(),
goto_execute!(340, 0), // goto run_cleaners_with_handling/0, 340. goto_execute!(342, 0), // goto run_cleaners_with_handling/0, 342.
trust_me!(), trust_me!(),
proceed!(), // goto_execute!(380, 0), // goto restore_cut_points/0, 380. goto_execute!(382, 0), // goto restore_cut_points/0, 382.
try_me_else!(10), // run_cleaners_without_handling/1, 352. try_me_else!(10), // run_cleaners_without_handling/1, 354.
allocate!(2), allocate!(2),
get_level!(perm_v!(1)), get_level!(perm_v!(1)),
query![put_var!(perm_v!(2), 1)], query![put_var!(perm_v!(2), 1)],
get_cleaner_call!(), get_cleaner_call!(),
query![put_value!(perm_v!(2), 1)], query![put_value!(perm_v!(2), 1)],
call_n!(1), call_n!(1),
cut!(perm_v!(1)), set_cp!(perm_v!(1)),
deallocate!(), deallocate!(),
goto_execute!(352, 0), // goto run_cleaners_without_handling/0, 352. goto_execute!(354, 0), // goto run_cleaners_without_handling/0, 354.
trust_me!(), trust_me!(),
proceed!(), // goto_execute!(380, 0), // goto restore_cut_points/0, 380. goto_execute!(382, 0), // goto restore_cut_points/0, 382.
allocate!(1), // sgc_on_success/2, 364. allocate!(1), // sgc_on_success/2, 366.
fact![get_var_in_fact!(perm_v!(1), 2)], fact![get_var_in_fact!(perm_v!(1), 2)],
reset_block!(), reset_block!(),
cut!(perm_v!(1)), cut!(perm_v!(1)),
deallocate!(), deallocate!(),
proceed!(), proceed!(),
is_compound!(temp_v!(1)), // compound/1, 370. is_compound!(temp_v!(1)), // compound/1, 372.
proceed!(), proceed!(),
is_rational!(temp_v!(1)), // rational/1, 372. is_rational!(temp_v!(1)), // rational/1, 374.
proceed!(), proceed!(),
is_string!(temp_v!(1)), // string/1, 374. is_string!(temp_v!(1)), // string/1, 376.
proceed!(), proceed!(),
is_float!(temp_v!(1)), // float/1, 376. is_float!(temp_v!(1)), // float/1, 378.
proceed!(), proceed!(),
is_nonvar!(temp_v!(1)), // nonvar/1, 378. is_nonvar!(temp_v!(1)), // nonvar/1, 380.
proceed!(), proceed!(),
restore_cut_policy!(), // restore_cut_policy/0, 382.
proceed!()
] ]
} }
@@ -621,11 +625,11 @@ pub fn build_code_dir(atom_tbl: TabledData<Atom>) -> (Code, CodeDir, OpDir)
code_dir.insert((tabled_rc!("length", atom_tbl), 2), (PredicateKeyType::BuiltIn, 261)); code_dir.insert((tabled_rc!("length", atom_tbl), 2), (PredicateKeyType::BuiltIn, 261));
code_dir.insert((tabled_rc!("setup_call_cleanup", atom_tbl), 3), (PredicateKeyType::BuiltIn, 294)); code_dir.insert((tabled_rc!("setup_call_cleanup", atom_tbl), 3), (PredicateKeyType::BuiltIn, 294));
code_dir.insert((tabled_rc!("compound", atom_tbl), 1), (PredicateKeyType::BuiltIn, 370)); code_dir.insert((tabled_rc!("compound", atom_tbl), 1), (PredicateKeyType::BuiltIn, 372));
code_dir.insert((tabled_rc!("rational", atom_tbl), 1), (PredicateKeyType::BuiltIn, 372)); code_dir.insert((tabled_rc!("rational", atom_tbl), 1), (PredicateKeyType::BuiltIn, 374));
code_dir.insert((tabled_rc!("string", atom_tbl), 1), (PredicateKeyType::BuiltIn, 374)); code_dir.insert((tabled_rc!("string", atom_tbl), 1), (PredicateKeyType::BuiltIn, 376));
code_dir.insert((tabled_rc!("float", atom_tbl), 1), (PredicateKeyType::BuiltIn, 376)); code_dir.insert((tabled_rc!("float", atom_tbl), 1), (PredicateKeyType::BuiltIn, 378));
code_dir.insert((tabled_rc!("nonvar", atom_tbl), 1), (PredicateKeyType::BuiltIn, 378)); code_dir.insert((tabled_rc!("nonvar", atom_tbl), 1), (PredicateKeyType::BuiltIn, 380));
(builtin_code, code_dir, op_dir) (builtin_code, code_dir, op_dir)
} }

View File

@@ -219,6 +219,8 @@ impl fmt::Display for BuiltInInstruction {
write!(f, "is_var {}", r), write!(f, "is_var {}", r),
&BuiltInInstruction::ResetBlock => &BuiltInInstruction::ResetBlock =>
write!(f, "reset_block"), write!(f, "reset_block"),
&BuiltInInstruction::RestoreCutPolicy =>
write!(f, "restore_cut_point"),
&BuiltInInstruction::SetBall => &BuiltInInstruction::SetBall =>
write!(f, "set_ball"), write!(f, "set_ball"),
&BuiltInInstruction::SetCutPoint(r) => &BuiltInInstruction::SetCutPoint(r) =>

View File

@@ -248,7 +248,7 @@ impl CutPolicy for SetupCallCleanupCutPolicy {
machine_st.cp = machine_st.p; machine_st.cp = machine_st.p;
machine_st.num_of_args = 0; machine_st.num_of_args = 0;
machine_st.b0 = machine_st.b; machine_st.b0 = machine_st.b;
machine_st.p = CodePtr::DirEntry(352); // goto_call run_cleaners_without_handling/0. machine_st.p = CodePtr::DirEntry(354); // goto_call run_cleaners_without_handling/0, 354.
} }
} }
} }

View File

@@ -1204,7 +1204,7 @@ impl MachineState {
let b = self.b; let b = self.b;
let block = self.block; let block = self.block;
if !cut_policy.is::<SetupCallCleanupCutPolicy>() { if cut_policy.downcast_ref::<SetupCallCleanupCutPolicy>().is_err() {
*cut_policy = Box::new(SetupCallCleanupCutPolicy::new()); *cut_policy = Box::new(SetupCallCleanupCutPolicy::new());
} }
@@ -1217,6 +1217,20 @@ SetupCallCleanupCutPolicy.")
self.p += 1; self.p += 1;
}, },
&BuiltInInstruction::RestoreCutPolicy => {
let restore_default =
if let Ok(cut_policy) = cut_policy.downcast_ref::<SetupCallCleanupCutPolicy>() {
cut_policy.out_of_cont_pts()
} else {
false
};
if restore_default {
*cut_policy = Box::new(DefaultCutPolicy {});
}
self.p += 1;
},
&BuiltInInstruction::SetBall => { &BuiltInInstruction::SetBall => {
let addr = self[temp_v!(1)].clone(); let addr = self[temp_v!(1)].clone();
self.ball.0 = self.heap.h; self.ball.0 = self.heap.h;
@@ -1358,7 +1372,7 @@ SetupCallCleanupCutPolicy.")
self.unify(a1, a2); self.unify(a1, a2);
self.p += 1; self.p += 1;
} },
}; };
} }
@@ -1518,7 +1532,7 @@ SetupCallCleanupCutPolicy.")
_ => { _ => {
self.num_of_args = 2; self.num_of_args = 2;
self.b0 = self.b; self.b0 = self.b;
self.p = CodePtr::DirEntry(364); // goto sgc_on_success/2, 364. self.p = CodePtr::DirEntry(366); // goto sgc_on_success/2, 366.
} }
}; };
}, },
@@ -1576,21 +1590,21 @@ SetupCallCleanupCutPolicy.")
}), }),
&ControlInstruction::GetCleanerCall => { &ControlInstruction::GetCleanerCall => {
let dest = self[temp_v!(1)].clone(); let dest = self[temp_v!(1)].clone();
match cut_policy.downcast_mut::<SetupCallCleanupCutPolicy>().ok() { match cut_policy.downcast_mut::<SetupCallCleanupCutPolicy>().ok() {
Some(sgc_policy) => Some(sgc_policy) =>
if let Some((addr, b_cutoff, prev_block)) = sgc_policy.pop_cont_pt() if let Some((addr, b_cutoff, prev_block)) = sgc_policy.pop_cont_pt()
{ {
self.p += 1; self.p += 1;
if self.b <= b_cutoff + 1 { if self.b <= b_cutoff + 1 {
self.block = prev_block; self.block = prev_block;
if let Some(r) = dest.as_var() { if let Some(r) = dest.as_var() {
self.bind(r, addr); self.bind(r, addr);
return; return;
} }
} else { } else {
sgc_policy.push_cont_pt(addr, b_cutoff, prev_block); sgc_policy.push_cont_pt(addr, b_cutoff, prev_block);
} }
}, },

View File

@@ -556,3 +556,9 @@ macro_rules! get_cleaner_call {
Line::Control(ControlInstruction::GetCleanerCall) Line::Control(ControlInstruction::GetCleanerCall)
) )
} }
macro_rules! restore_cut_policy {
() => (
Line::BuiltIn(BuiltInInstruction::RestoreCutPolicy)
)
}