Skip to content

Commit ce2688e

Browse files
Bitwuzla: Make the code less error prone by identifying |var| and var directly in the variable cache
1 parent 4b9f33f commit ce2688e

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

src/org/sosy_lab/java_smt/solvers/bitwuzla/BitwuzlaFormulaCreator.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
import static org.sosy_lab.common.collect.Collections3.transformedImmutableSetCopy;
1313

1414
import com.google.common.base.Preconditions;
15-
import com.google.common.collect.HashBasedTable;
1615
import com.google.common.collect.ImmutableList;
1716
import com.google.common.collect.Table;
17+
import com.google.common.collect.TreeBasedTable;
1818
import java.math.BigInteger;
1919
import java.util.ArrayDeque;
2020
import java.util.ArrayList;
2121
import java.util.Collection;
22+
import java.util.Comparator;
2223
import java.util.Deque;
2324
import java.util.HashMap;
2425
import java.util.Iterator;
@@ -59,7 +60,31 @@
5960
public class BitwuzlaFormulaCreator extends FormulaCreator<Term, Sort, Void, BitwuzlaDeclaration> {
6061
private final TermManager termManager;
6162

62-
private final Table<String, Sort, Term> formulaCache = HashBasedTable.create();
63+
/**
64+
* Returns the symbol name without any SMTLIB quotes.
65+
*
66+
* <p>Will turn <code>| 1var\n|</code> into just <code> 1 var\n</code>. Symbols that are not
67+
* quoted are unaffected.
68+
*/
69+
private String removeQuotes(String symbol) {
70+
return (symbol.startsWith("|") && symbol.endsWith("|"))
71+
? symbol.substring(1, symbol.length() - 1)
72+
: symbol;
73+
}
74+
75+
/**
76+
* Stores Bitwuzla terms for all defined symbols.
77+
*
78+
* <p>The cache maps from <code>String x Sort</code> to <code>Term</code>. Here the first argument
79+
* is the name of the symbol, and we allow polymorphic symbols where the same name can have more
80+
* than one sort. If the symbol can be printed as a simple symbol or a quoted symbol (like <code>
81+
* var1</code> and <code>|var1|</code>) in SMTLIB we identify both version as they refer to the
82+
* same variable.
83+
*/
84+
private final Table<String, Sort, Term> formulaCache =
85+
TreeBasedTable.create(
86+
(String symA, String symB) -> removeQuotes(symA).compareTo(removeQuotes(symB)),
87+
Comparator.comparing(Sort::toString));
6388

6489
/**
6590
* This mapping stores symbols and their constraints, such as from fp-to-bv casts with their

src/org/sosy_lab/java_smt/solvers/bitwuzla/BitwuzlaFormulaManager.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,6 @@ public Term parseImpl(String formulaStr) throws IllegalArgumentException {
7474
Term parsed = declParser.get_declared_funs().get(0);
7575

7676
String symbol = parsed.symbol();
77-
if (symbol.startsWith("|") && symbol.endsWith("|")) {
78-
// Strip quotes from the name
79-
symbol = symbol.substring(1, symbol.length() - 1);
80-
}
8177
Sort sort = parsed.sort();
8278

8379
// Check if the symbol is already defined in the variable cache
@@ -138,9 +134,6 @@ public Term parseImpl(String formulaStr) throws IllegalArgumentException {
138134
Map_TermTerm subst = new Map_TermTerm();
139135
for (Term term : declared) {
140136
String symbol = term.symbol();
141-
if (symbol.startsWith("|") && symbol.endsWith("|")) {
142-
symbol = symbol.substring(1, symbol.length() - 1);
143-
}
144137
if (cache.containsRow(symbol)) {
145138
// Symbol is from the context: add the original term to the substitution map
146139
subst.put(term, cache.get(symbol, term.sort()));

0 commit comments

Comments
 (0)