Skip to main content

litex/runtime/
runtime_store_fact.rs

1use crate::prelude::*;
2use std::collections::HashSet;
3
4impl Runtime {
5    pub fn verify_well_defined_and_store_and_infer(
6        &mut self,
7        fact: Fact,
8        verify_state: &VerifyState,
9    ) -> Result<InferResult, RuntimeError> {
10        if let Err(wd_err) = self.verify_fact_well_defined(&fact, verify_state) {
11            return Err(StoreFactRuntimeError(RuntimeErrorStruct::new(
12                Some(fact.clone().into_stmt()),
13                "cannot store fact: not well-defined".to_string(),
14                fact.line_file(),
15                Some(wd_err),
16                vec![],
17            ))
18            .into());
19        }
20        self.store_and_infer_fact_without_well_defined_verified(fact)
21    }
22
23    pub fn verify_well_defined_and_store_and_infer_with_default_verify_state(
24        &mut self,
25        fact: Fact,
26    ) -> Result<InferResult, RuntimeError> {
27        let verify_state = match fact {
28            Fact::ForallFact(_) => VerifyState::new(0, false),
29            Fact::ForallFactWithIff(_) => VerifyState::new(0, false),
30            _ => VerifyState::new_with_final_round(false),
31        };
32        self.verify_well_defined_and_store_and_infer(fact, &verify_state)
33    }
34
35    fn store_and_infer_fact_without_well_defined_verified(
36        &mut self,
37        fact: Fact,
38    ) -> Result<InferResult, RuntimeError> {
39        let mut infer_result = InferResult::new();
40
41        infer_result.new_fact(&fact);
42
43        let ret = match fact {
44            Fact::AtomicFact(_)
45            | Fact::ExistFact(_)
46            | Fact::OrFact(_)
47            | Fact::AndFact(_)
48            | Fact::ChainFact(_)
49            | Fact::NotForall(_) => self.store_whole_fact_update_cache_known_fact_and_infer(fact),
50            Fact::ForallFact(forall_fact) => {
51                self.store_forall_fact_without_well_defined_verified_and_infer(forall_fact)
52            }
53            Fact::ForallFactWithIff(forall_fact_with_iff) => self
54                .store_forall_fact_with_iff_without_well_defined_verified_and_infer(
55                    forall_fact_with_iff,
56                ),
57        };
58
59        infer_result.new_infer_result_inside(ret?);
60
61        Ok(infer_result)
62    }
63
64    pub fn store_fact_without_forall_coverage_check_and_infer(
65        &mut self,
66        fact: Fact,
67    ) -> Result<InferResult, RuntimeError> {
68        self.store_whole_fact_update_cache_known_fact_and_infer(fact)
69    }
70
71    pub(crate) fn store_forall_fact_without_well_defined_verified_and_infer(
72        &mut self,
73        mut forall_fact: ForallFact,
74    ) -> Result<InferResult, RuntimeError> {
75        forall_fact.expand_then_facts_with_order_chain_closure()?;
76
77        let coverage_error_detail_lines =
78            forall_fact.error_messages_if_forall_param_missing_in_some_then_clause();
79
80        if coverage_error_detail_lines.is_empty() {
81            return self
82                .store_whole_fact_update_cache_known_fact_and_infer(Fact::ForallFact(forall_fact));
83        }
84
85        let warning_msg = forall_fact_coverage_warn_after_drop_then(&coverage_error_detail_lines);
86        let then_drop: HashSet<usize> = coverage_error_detail_lines
87            .iter()
88            .map(|(i, _)| *i)
89            .collect();
90
91        forall_fact.then_facts = forall_fact
92            .then_facts
93            .into_iter()
94            .enumerate()
95            .filter(|(i, _)| !then_drop.contains(i))
96            .map(|(_, f)| f)
97            .collect();
98
99        if forall_fact.then_facts.is_empty() {
100            let mut infer_result = InferResult::new();
101            infer_result.new_with_msg(warning_msg);
102            return Ok(infer_result);
103        }
104
105        let mut infer_result = InferResult::new();
106        infer_result.new_with_msg(warning_msg);
107        infer_result.new_infer_result_inside(
108            self.store_whole_fact_update_cache_known_fact_and_infer(Fact::ForallFact(forall_fact))?,
109        );
110        Ok(infer_result)
111    }
112
113    fn store_forall_fact_with_iff_without_well_defined_verified_and_infer(
114        &mut self,
115        forall_fact_with_iff: ForallFactWithIff,
116    ) -> Result<InferResult, RuntimeError> {
117        let (forall_then_implies_iff, forall_iff_implies_then) =
118            forall_fact_with_iff.to_two_forall_facts()?;
119        let mut infer_result = self
120            .store_forall_fact_without_well_defined_verified_and_infer(forall_then_implies_iff)?;
121        infer_result.new_infer_result_inside(
122            self.store_forall_fact_without_well_defined_verified_and_infer(
123                forall_iff_implies_then,
124            )?,
125        );
126        Ok(infer_result)
127    }
128
129    fn store_whole_fact_update_cache_known_fact_and_infer(
130        &mut self,
131        fact: Fact,
132    ) -> Result<InferResult, RuntimeError> {
133        let line_file = fact.line_file();
134        let fact_string: FactString = fact.to_string();
135        let fact_for_infer = fact.clone();
136        self.top_level_env().store_fact(fact)?;
137
138        self.top_level_env()
139            .store_fact_to_cache_known_fact(fact_string, line_file)?;
140
141        Ok(self.infer(&fact_for_infer)?)
142    }
143
144    pub fn store_and_chain_atomic_fact_without_well_defined_verified_and_infer(
145        &mut self,
146        fact: AndChainAtomicFact,
147    ) -> Result<InferResult, RuntimeError> {
148        let line_file = fact.line_file();
149        let fact_string: FactString = fact.to_string();
150        let fact_for_infer: Fact = fact.clone().into();
151        self.top_level_env().store_and_chain_atomic_fact(fact)?;
152
153        self.top_level_env()
154            .store_fact_to_cache_known_fact(fact_string, line_file)?;
155
156        Ok(self.infer(&fact_for_infer)?)
157    }
158
159    pub fn store_atomic_fact_without_well_defined_verified_and_infer(
160        &mut self,
161        fact: AtomicFact,
162    ) -> Result<InferResult, RuntimeError> {
163        let line_file = fact.line_file();
164        let fact_string: FactString = fact.to_string();
165        let infer_wrapped_fact: Fact = fact.clone().into();
166        self.top_level_env().store_atomic_fact(fact)?;
167
168        self.top_level_env()
169            .store_fact_to_cache_known_fact(fact_string, line_file)?;
170
171        Ok(self.infer(&infer_wrapped_fact)?)
172    }
173
174    pub fn store_exist_or_and_chain_atomic_fact_without_well_defined_verified_and_infer(
175        &mut self,
176        fact: ExistOrAndChainAtomicFact,
177    ) -> Result<InferResult, RuntimeError> {
178        let line_file = fact.line_file();
179        let fact_string: FactString = fact.to_string();
180        let fact_for_infer = fact.clone();
181        self.top_level_env()
182            .store_exist_or_and_chain_atomic_fact(fact)?;
183
184        self.top_level_env()
185            .store_fact_to_cache_known_fact(fact_string, line_file)?;
186
187        Ok(self.infer_exist_or_and_chain_atomic_fact(&fact_for_infer)?)
188    }
189
190    pub fn store_or_and_chain_atomic_fact_without_well_defined_verified_and_infer(
191        &mut self,
192        fact: OrAndChainAtomicFact,
193    ) -> Result<InferResult, RuntimeError> {
194        let line_file = fact.line_file();
195        let fact_string: FactString = fact.to_string();
196        let fact_for_infer = fact.clone();
197        self.top_level_env().store_or_and_chain_atomic_fact(fact)?;
198
199        self.top_level_env()
200            .store_fact_to_cache_known_fact(fact_string, line_file)?;
201
202        Ok(self.infer_or_and_chain_atomic_fact(&fact_for_infer)?)
203    }
204}
205fn forall_fact_coverage_warn_after_drop_then(
206    coverage_error_detail_lines: &[(usize, String)],
207) -> String {
208    let body = coverage_error_detail_lines
209        .iter()
210        .map(|(idx, msg)| format!("at index {}: {}", idx, msg))
211        .collect::<Vec<_>>()
212        .join("\n");
213    format!(
214        "Warning: forall missing forall parameter(s) in some then clause(s); dropped problematic clause(s):\n{}",
215        body
216    )
217}