From 6fa00b5b550ab28ef73b9e0b2cb75e071b198237 Mon Sep 17 00:00:00 2001 From: Mark Date: Sat, 30 Sep 2023 22:20:50 -0600 Subject: [PATCH] get rid of inference_limit_exceeded(B) as an error term (#2023) --- build/instructions_template.rs | 4 + src/lib/iso_ext.pl | 54 ++--- src/machine/dispatch.rs | 383 ++++++------------------------ src/machine/machine_state.rs | 26 +- src/machine/machine_state_impl.rs | 2 +- src/machine/system_calls.rs | 14 +- 6 files changed, 125 insertions(+), 358 deletions(-) diff --git a/build/instructions_template.rs b/build/instructions_template.rs index 894254c7..15881bfd 100644 --- a/build/instructions_template.rs +++ b/build/instructions_template.rs @@ -601,6 +601,8 @@ enum SystemClauseType { Name = "$keysort_with_constant_var_ordering" )))] KeySortWithConstantVarOrdering, + #[strum_discriminants(strum(props(Arity = "0", Name = "$inference_limit_exceeded")))] + InferenceLimitExceeded, REPL(REPLCodePtr), } @@ -1727,6 +1729,7 @@ fn generate_instruction_preface() -> TokenStream { &Instruction::CallUnattributedVar | &Instruction::CallGetDBRefs | &Instruction::CallKeySortWithConstantVarOrdering | + &Instruction::CallInferenceLimitExceeded | &Instruction::CallFetchGlobalVar | &Instruction::CallFirstStream | &Instruction::CallFlushOutput | @@ -1960,6 +1963,7 @@ fn generate_instruction_preface() -> TokenStream { &Instruction::ExecuteUnattributedVar | &Instruction::ExecuteGetDBRefs | &Instruction::ExecuteKeySortWithConstantVarOrdering | + &Instruction::ExecuteInferenceLimitExceeded | &Instruction::ExecuteFetchGlobalVar | &Instruction::ExecuteFirstStream | &Instruction::ExecuteFlushOutput | diff --git a/src/lib/iso_ext.pl b/src/lib/iso_ext.pl index d53ff47b..5096bfb9 100644 --- a/src/lib/iso_ext.pl +++ b/src/lib/iso_ext.pl @@ -214,27 +214,6 @@ run_cleaners_without_handling(Cp) :- % call_with_inference_limit -:- non_counted_backtracking end_block/4. - -end_block(_, Bb, NBb, _L) :- - '$clean_up_block'(NBb), - '$reset_block'(Bb). -end_block(B, _Bb, NBb, L) :- - '$install_inference_counter'(B, L, _), - '$reset_block'(NBb), - '$fail'. - -:- non_counted_backtracking handle_ile/3. - -handle_ile(B, inference_limit_exceeded(B), R) :- - !, - R = inference_limit_exceeded, - '$pop_ball_stack'. -handle_ile(B, _, _) :- - '$remove_call_policy_check'(B), - '$pop_from_ball_stack', - '$unwind_stack'. - :- meta_predicate(call_with_inference_limit(0, ?, ?)). :- non_counted_backtracking call_with_inference_limit/3. @@ -257,8 +236,6 @@ call_with_inference_limit(G, L, R) :- call_with_inference_limit(G, L, R, Bb, B), '$remove_call_policy_check'(B). -install_inference_counter(B, L, Count0) :- - '$install_inference_counter'(B, L, Count0). :- meta_predicate(call_with_inference_limit(0,?,?,?,?)). @@ -266,23 +243,34 @@ install_inference_counter(B, L, Count0) :- call_with_inference_limit(G, L, R, Bb, B) :- '$install_new_block'(NBb), - '$install_inference_counter'(B, L, Count0), + '$install_inference_counter'(NBb, L, Count0), '$call_with_inference_counting'(call(G)), '$inference_level'(R, B), - '$remove_inference_counter'(B, Count1), + '$remove_inference_counter'(NBb, Count1), Diff is L - (Count1 - Count0), - end_block(B, Bb, NBb, Diff). + ( '$clean_up_block'(NBb), + '$reset_block'(Bb) + ; '$install_inference_counter'(NBb, Diff, _), + '$reset_block'(NBb), + '$fail' + ). call_with_inference_limit(_, _, R, Bb, B) :- + ( '$inference_limit_exceeded' -> + R = inference_limit_exceeded + ; true + ), + '$get_current_block'(NBb), + '$remove_inference_counter'(NBb, _), '$reset_block'(Bb), - '$remove_inference_counter'(B, _), - ( '$get_ball'(Ball), + '$remove_call_policy_check'(B), + ( '$get_ball'(_), '$push_ball_stack', '$get_cp'(Cp), - '$set_cp_by_default'(Cp) - ; '$remove_call_policy_check'(B), - '$fail' - ), - handle_ile(B, Ball, R). + '$set_cp_by_default'(Cp), + '$pop_from_ball_stack', + '$unwind_stack' + ; nonvar(R) + ). %% partial_string(String, L, L0) % diff --git a/src/machine/dispatch.rs b/src/machine/dispatch.rs index 1ac43189..23fe7c02 100644 --- a/src/machine/dispatch.rs +++ b/src/machine/dispatch.rs @@ -34,6 +34,15 @@ macro_rules! try_or_throw { }}; } +macro_rules! increment_call_count { + ($s:expr) => {{ + if !($s.increment_call_count_fn)(&mut $s) { + $s.backtrack(); + continue; + } + }}; +} + macro_rules! try_or_throw_gen { ($s:expr, $e:expr) => {{ match $e { @@ -1096,12 +1105,7 @@ impl Machine { self.trust_me(); } - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)( - &mut self.machine_st - ) - ); + increment_call_count!(self.machine_st); } } } @@ -1174,12 +1178,7 @@ impl Machine { self.trust_me(); } - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)( - &mut self.machine_st - ) - ); + increment_call_count!(self.machine_st); } } } @@ -1205,19 +1204,11 @@ impl Machine { } &Instruction::RetryMeElse(offset) => { self.retry_me_else(offset); - - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); + increment_call_count!(self.machine_st); } &Instruction::TrustMe(_) => { self.trust_me(); - - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); + increment_call_count!(self.machine_st); } &Instruction::NeckCut => { self.machine_st.neck_cut(); @@ -1521,11 +1512,7 @@ impl Machine { if self.machine_st.is_cyclic_term(addr) { self.machine_st.backtrack(); } else { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p += 1; } } @@ -1535,11 +1522,7 @@ impl Machine { if self.machine_st.is_cyclic_term(addr) { self.machine_st.backtrack(); } else { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p = self.machine_st.cp; } } @@ -1549,11 +1532,7 @@ impl Machine { if self.machine_st.fail { self.machine_st.backtrack(); } else { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p += 1; } } @@ -1563,11 +1542,7 @@ impl Machine { if self.machine_st.fail { self.machine_st.backtrack(); } else { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p = self.machine_st.cp; } } @@ -1577,11 +1552,7 @@ impl Machine { if self.machine_st.fail { self.machine_st.backtrack(); } else { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p += 1; } } @@ -1591,11 +1562,7 @@ impl Machine { if self.machine_st.fail { self.machine_st.backtrack(); } else { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p = self.machine_st.cp; } } @@ -1605,11 +1572,7 @@ impl Machine { if let Some(Ordering::Greater) = compare_term_test!(self.machine_st, a1, a2) { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p += 1; } else { self.machine_st.backtrack(); @@ -1621,11 +1584,7 @@ impl Machine { if let Some(Ordering::Greater) = compare_term_test!(self.machine_st, a1, a2) { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p = self.machine_st.cp; } else { self.machine_st.backtrack(); @@ -1636,11 +1595,7 @@ impl Machine { let a2 = self.machine_st.registers[2]; if let Some(Ordering::Less) = compare_term_test!(self.machine_st, a1, a2) { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p += 1; } else { self.machine_st.backtrack(); @@ -1651,11 +1606,7 @@ impl Machine { let a2 = self.machine_st.registers[2]; if let Some(Ordering::Less) = compare_term_test!(self.machine_st, a1, a2) { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p = self.machine_st.cp; } else { self.machine_st.backtrack(); @@ -1667,11 +1618,7 @@ impl Machine { match compare_term_test!(self.machine_st, a1, a2) { Some(Ordering::Greater | Ordering::Equal) => { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p += 1; } _ => { @@ -1685,11 +1632,7 @@ impl Machine { match compare_term_test!(self.machine_st, a1, a2) { Some(Ordering::Greater | Ordering::Equal) => { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p = self.machine_st.cp; } _ => { @@ -1703,11 +1646,7 @@ impl Machine { match compare_term_test!(self.machine_st, a1, a2) { Some(Ordering::Less | Ordering::Equal) => { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p += 1; } _ => { @@ -1721,11 +1660,7 @@ impl Machine { match compare_term_test!(self.machine_st, a1, a2) { Some(Ordering::Less | Ordering::Equal) => { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p = self.machine_st.cp; } _ => { @@ -1739,11 +1674,7 @@ impl Machine { if self.machine_st.fail { self.machine_st.backtrack(); } else { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p += 1; } } @@ -1753,11 +1684,7 @@ impl Machine { if self.machine_st.fail { self.machine_st.backtrack(); } else { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p = self.machine_st.cp; } } @@ -1768,11 +1695,7 @@ impl Machine { if self.machine_st.eq_test(a1, a2) { self.machine_st.backtrack(); } else { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p += 1; } } @@ -1783,11 +1706,7 @@ impl Machine { if self.machine_st.eq_test(a1, a2) { self.machine_st.backtrack(); } else { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p = self.machine_st.cp; } } @@ -1795,11 +1714,7 @@ impl Machine { if self.machine_st.ground_test() { self.machine_st.backtrack(); } else { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p += 1; } } @@ -1807,11 +1722,7 @@ impl Machine { if self.machine_st.ground_test() { self.machine_st.backtrack(); } else { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p = self.machine_st.cp; } } @@ -1821,11 +1732,7 @@ impl Machine { if self.machine_st.fail { self.machine_st.backtrack(); } else { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p += 1; } } @@ -1835,11 +1742,7 @@ impl Machine { if self.machine_st.fail { self.machine_st.backtrack(); } else { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p = self.machine_st.cp; } } @@ -1850,11 +1753,7 @@ impl Machine { if let Some(Ordering::Equal) = compare_term_test!(self.machine_st, a1, a2) { self.machine_st.backtrack(); } else { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p += 1; } } @@ -1865,11 +1764,7 @@ impl Machine { if let Some(Ordering::Equal) = compare_term_test!(self.machine_st, a1, a2) { self.machine_st.backtrack(); } else { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p = self.machine_st.cp; } } @@ -1879,11 +1774,7 @@ impl Machine { if self.machine_st.fail { self.machine_st.backtrack(); } else { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p += 1; } } @@ -1893,11 +1784,7 @@ impl Machine { if self.machine_st.fail { self.machine_st.backtrack(); } else { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p = self.machine_st.cp; } } @@ -1910,11 +1797,7 @@ impl Machine { if self.machine_st.fail { self.machine_st.backtrack(); } else { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p += 1; } } @@ -1927,11 +1810,7 @@ impl Machine { if self.machine_st.fail { self.machine_st.backtrack(); } else { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p = self.machine_st.cp; } } @@ -1944,11 +1823,7 @@ impl Machine { if self.machine_st.fail { self.machine_st.backtrack(); } else { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p += 1; } } @@ -1961,11 +1836,7 @@ impl Machine { if self.machine_st.fail { self.machine_st.backtrack(); } else { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p = self.machine_st.cp; } } @@ -1975,11 +1846,7 @@ impl Machine { if self.machine_st.fail { self.machine_st.backtrack(); } else { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p += 1; } } @@ -1989,11 +1856,7 @@ impl Machine { if self.machine_st.fail { self.machine_st.backtrack(); } else { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p = self.machine_st.cp; } } @@ -2003,11 +1866,7 @@ impl Machine { if self.machine_st.fail { self.machine_st.backtrack(); } else { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p += 1; } } @@ -2017,11 +1876,7 @@ impl Machine { if self.machine_st.fail { self.machine_st.backtrack(); } else { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p = self.machine_st.cp; } } @@ -2039,10 +1894,7 @@ impl Machine { if self.machine_st.fail { self.machine_st.backtrack(); } else { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); + increment_call_count!(self.machine_st); } } &Instruction::ExecuteN(arity) => { @@ -2059,10 +1911,7 @@ impl Machine { if self.machine_st.fail { self.machine_st.backtrack(); } else { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); + increment_call_count!(self.machine_st); } } &Instruction::DefaultCallN(arity) => { @@ -2101,11 +1950,7 @@ impl Machine { match n1.cmp(&n2) { Ordering::Less | Ordering::Equal => { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p += 1; } _ => { @@ -2119,11 +1964,7 @@ impl Machine { match n1.cmp(&n2) { Ordering::Less | Ordering::Equal => { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p = self.machine_st.cp; } _ => { @@ -2137,11 +1978,7 @@ impl Machine { match n1.cmp(&n2) { Ordering::Equal => { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p += 1; } _ => { @@ -2155,11 +1992,7 @@ impl Machine { match n1.cmp(&n2) { Ordering::Equal => { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p = self.machine_st.cp; } _ => { @@ -2176,11 +2009,7 @@ impl Machine { self.machine_st.backtrack(); } _ => { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p += 1; } } @@ -2194,11 +2023,7 @@ impl Machine { self.machine_st.backtrack(); } _ => { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p = self.machine_st.cp; } } @@ -2209,11 +2034,7 @@ impl Machine { match n1.cmp(&n2) { Ordering::Greater | Ordering::Equal => { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p += 1; } _ => { @@ -2227,11 +2048,7 @@ impl Machine { match n1.cmp(&n2) { Ordering::Greater | Ordering::Equal => { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p = self.machine_st.cp; } _ => { @@ -2245,11 +2062,7 @@ impl Machine { match n1.cmp(&n2) { Ordering::Greater => { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p += 1; } _ => { @@ -2263,11 +2076,7 @@ impl Machine { match n1.cmp(&n2) { Ordering::Greater => { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p = self.machine_st.cp; } _ => { @@ -2281,11 +2090,7 @@ impl Machine { match n1.cmp(&n2) { Ordering::Less => { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p += 1; } _ => { @@ -2299,11 +2104,7 @@ impl Machine { match n1.cmp(&n2) { Ordering::Less => { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); - + increment_call_count!(self.machine_st); self.machine_st.p = self.machine_st.cp; } _ => { @@ -2878,10 +2679,7 @@ impl Machine { if self.machine_st.fail { self.machine_st.backtrack(); } else { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); + increment_call_count!(self.machine_st); } } &Instruction::ExecuteNamed(arity, name, ref idx) => { @@ -2892,10 +2690,7 @@ impl Machine { if self.machine_st.fail { self.machine_st.backtrack(); } else { - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)(&mut self.machine_st) - ); + increment_call_count!(self.machine_st); } } &Instruction::DefaultCallNamed(arity, name, ref idx) => { @@ -3224,26 +3019,14 @@ impl Machine { } &IndexedChoiceInstruction::Retry(l) => { self.retry(l); - - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)( - &mut self.machine_st - ) - ); + increment_call_count!(self.machine_st); } &IndexedChoiceInstruction::DefaultRetry(l) => { self.retry(l); } &IndexedChoiceInstruction::Trust(l) => { self.trust(l); - - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)( - &mut self.machine_st - ) - ); + increment_call_count!(self.machine_st); } &IndexedChoiceInstruction::DefaultTrust(l) => { self.trust(l); @@ -3318,38 +3101,16 @@ impl Machine { // this is true iff ii + 1 < len. Some(_) => { self.retry(offset); - - try_or_throw!( - self.machine_st, - (self - .machine_st - .increment_call_count_fn)( - &mut self.machine_st - ) - ); + increment_call_count!(self.machine_st); } _ => { self.trust(offset); - - try_or_throw!( - self.machine_st, - (self - .machine_st - .increment_call_count_fn)( - &mut self.machine_st - ) - ); + increment_call_count!(self.machine_st); } } } else { self.trust(offset); - - try_or_throw!( - self.machine_st, - (self.machine_st.increment_call_count_fn)( - &mut self.machine_st - ) - ); + increment_call_count!(self.machine_st); } } } @@ -5484,6 +5245,14 @@ impl Machine { self.get_db_refs(); step_or_fail!(self, self.machine_st.p = self.machine_st.cp); } + &Instruction::CallInferenceLimitExceeded => { + self.inference_limit_exceeded(); + step_or_fail!(self, self.machine_st.p += 1); + } + &Instruction::ExecuteInferenceLimitExceeded => { + self.inference_limit_exceeded(); + step_or_fail!(self, self.machine_st.p = self.machine_st.cp); + } } } diff --git a/src/machine/machine_state.rs b/src/machine/machine_state.rs index c09ba26b..55a09ef5 100644 --- a/src/machine/machine_state.rs +++ b/src/machine/machine_state.rs @@ -96,7 +96,7 @@ pub struct MachineState { pub(crate) unify_fn: fn(&mut MachineState), pub(crate) bind_fn: fn(&mut MachineState, Ref, HeapCellValue), pub(crate) run_cleaners_fn: fn(&mut Machine) -> bool, - pub(crate) increment_call_count_fn: fn(&mut MachineState) -> CallResult, + pub(crate) increment_call_count_fn: fn(&mut MachineState) -> bool, } impl fmt::Debug for MachineState { @@ -412,22 +412,24 @@ impl MachineState { self.fail = false; } - pub(crate) fn increment_call_count(&mut self) -> CallResult { + pub(crate) fn increment_call_count(&mut self) -> bool { if self.cwil.inference_limit_exceeded || self.ball.stub.len() > 0 { - return Ok(()); + return true; } - if let Some(&(ref limit, bp)) = self.cwil.limits.last() { + if let Some(&(ref limit, block)) = self.cwil.limits.last() { if self.cwil.count == *limit { self.cwil.inference_limit_exceeded = true; + self.block = block; + self.unwind_stack(); - return Err(functor!(atom!("inference_limit_exceeded"), [fixnum(bp)])); + return false; } else { self.cwil.count += 1; } } - Ok(()) + true } #[allow(dead_code)] @@ -945,7 +947,7 @@ impl MachineState { pub(crate) struct CWIL { count: Integer, limits: Vec<(Integer, usize)>, - inference_limit_exceeded: bool, + pub(crate) inference_limit_exceeded: bool, } impl CWIL { @@ -957,22 +959,22 @@ impl CWIL { } } - pub(crate) fn add_limit(&mut self, limit: usize, b: usize) -> &Integer { + pub(crate) fn add_limit(&mut self, limit: usize, block: usize) -> &Integer { let mut limit = Integer::from(limit); limit += &self.count; match self.limits.last() { Some((ref inner_limit, _)) if *inner_limit <= limit => {} - _ => self.limits.push((limit, b)), + _ => self.limits.push((limit, block)), }; &self.count } #[inline(always)] - pub(crate) fn remove_limit(&mut self, b: usize) -> &Integer { - if let Some((_, bp)) = self.limits.last() { - if bp == &b { + pub(crate) fn remove_limit(&mut self, block: usize) -> &Integer { + if let Some((_, bl)) = self.limits.last() { + if bl == &block { self.limits.pop(); } } diff --git a/src/machine/machine_state_impl.rs b/src/machine/machine_state_impl.rs index 1ee0e20d..34b9d55a 100644 --- a/src/machine/machine_state_impl.rs +++ b/src/machine/machine_state_impl.rs @@ -60,7 +60,7 @@ impl MachineState { unify_fn: MachineState::unify, bind_fn: MachineState::bind, run_cleaners_fn: |_| false, - increment_call_count_fn: |_| Ok(()), + increment_call_count_fn: |_| true, } } diff --git a/src/machine/system_calls.rs b/src/machine/system_calls.rs index cf1b060e..5029fcb8 100644 --- a/src/machine/system_calls.rs +++ b/src/machine/system_calls.rs @@ -5695,7 +5695,7 @@ impl Machine { if bp == self.machine_st.b && self.machine_st.cwil.is_empty() { self.machine_st.cwil.reset(); - self.machine_st.increment_call_count_fn = |_| Ok(()); + self.machine_st.increment_call_count_fn = |_| true; } } @@ -5704,11 +5704,10 @@ impl Machine { let a1 = self.deref_register(1); let a2 = self.deref_register(2); - let bp = cell_as_fixnum!(a1).get_num() as usize; - - let count = self.machine_st.cwil.remove_limit(bp).clone(); - + let block = cell_as_fixnum!(a1).get_num() as usize; + let count = self.machine_st.cwil.remove_limit(block).clone(); let result = count.clone().try_into(); + if let Ok(value) = result{ self.machine_st.unify_fixnum(Fixnum::build_with(value), a2); } else { @@ -5875,6 +5874,11 @@ impl Machine { } } + #[inline(always)] + pub(crate) fn inference_limit_exceeded(&mut self) { + self.machine_st.fail = !self.machine_st.cwil.inference_limit_exceeded; + } + #[inline(always)] pub(crate) fn clean_up_block(&mut self) { let nb = self.deref_register(1); -- 2.54.0