Skip to content

Commit 7d89b87

Browse files
Merge pull request #509 from sosy-lab/supportMoreTermKinds
Add more FunctionDeclarationKinds for the visitor
2 parents 57d7e4a + 7ebcdbf commit 7d89b87

File tree

6 files changed

+80
-1
lines changed

6 files changed

+80
-1
lines changed

src/org/sosy_lab/java_smt/api/FunctionDeclarationKind.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ public enum FunctionDeclarationKind {
9292
/** Identity operation, converts from integers to rationals, also known as {@code to_real}. */
9393
TO_REAL,
9494

95+
/** Convert from integer to bitvector. */
96+
INT_TO_BV,
97+
9598
// Simple bitvector operations
9699

97100
/** Extraction over bitvectors. */
@@ -205,6 +208,12 @@ public enum FunctionDeclarationKind {
205208
/** Cast a signed bitvector to a floating-point number. */
206209
BV_SCASTTO_FP,
207210

211+
/** Cast an unsigned bitvector to an integer number. */
212+
UBV_TO_INT,
213+
214+
/** Cast a signed bitvector to an integer number. */
215+
SBV_TO_INT,
216+
208217
// Simple floating point operations
209218

210219
/** Negation of a floating point. */

src/org/sosy_lab/java_smt/solvers/cvc4/CVC4FormulaCreator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ private Expr normalize(Expr operator) {
438438
.put(Kind.LEQ, FunctionDeclarationKind.LTE)
439439
.put(Kind.GT, FunctionDeclarationKind.GT)
440440
.put(Kind.GEQ, FunctionDeclarationKind.GTE)
441+
.put(Kind.INT_TO_BITVECTOR, FunctionDeclarationKind.INT_TO_BV)
441442
// Bitvector theory
442443
.put(Kind.BITVECTOR_PLUS, FunctionDeclarationKind.BV_ADD)
443444
.put(Kind.BITVECTOR_SUB, FunctionDeclarationKind.BV_SUB)

src/org/sosy_lab/java_smt/solvers/cvc5/CVC5FormulaCreator.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,7 @@ private Term normalize(Term operator) {
543543
.put(Kind.LEQ, FunctionDeclarationKind.LTE)
544544
.put(Kind.GT, FunctionDeclarationKind.GT)
545545
.put(Kind.GEQ, FunctionDeclarationKind.GTE)
546+
.put(Kind.INT_TO_BITVECTOR, FunctionDeclarationKind.INT_TO_BV)
546547
// Bitvector theory
547548
.put(Kind.BITVECTOR_ADD, FunctionDeclarationKind.BV_ADD)
548549
.put(Kind.BITVECTOR_SUB, FunctionDeclarationKind.BV_SUB)
@@ -574,9 +575,11 @@ private Term normalize(Term operator) {
574575
.put(Kind.BITVECTOR_LSHR, FunctionDeclarationKind.BV_LSHR)
575576
.put(Kind.BITVECTOR_ROTATE_LEFT, FunctionDeclarationKind.BV_ROTATE_LEFT_BY_INT)
576577
.put(Kind.BITVECTOR_ROTATE_RIGHT, FunctionDeclarationKind.BV_ROTATE_RIGHT_BY_INT)
577-
// Floating-point theory
578578
.put(Kind.TO_INTEGER, FunctionDeclarationKind.FLOOR)
579579
.put(Kind.TO_REAL, FunctionDeclarationKind.TO_REAL)
580+
.put(Kind.BITVECTOR_SBV_TO_INT, FunctionDeclarationKind.SBV_TO_INT)
581+
.put(Kind.BITVECTOR_UBV_TO_INT, FunctionDeclarationKind.UBV_TO_INT)
582+
// Floating-point theory
580583
.put(Kind.FLOATINGPOINT_TO_SBV, FunctionDeclarationKind.FP_CASTTO_SBV)
581584
.put(Kind.FLOATINGPOINT_TO_UBV, FunctionDeclarationKind.FP_CASTTO_UBV)
582585
.put(Kind.FLOATINGPOINT_TO_FP_FROM_FP, FunctionDeclarationKind.FP_CASTTO_FP)

src/org/sosy_lab/java_smt/solvers/mathsat5/Mathsat5FormulaCreator.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@
6666
import static org.sosy_lab.java_smt.solvers.mathsat5.Mathsat5NativeApi.MSAT_TAG_FP_TO_SBV;
6767
import static org.sosy_lab.java_smt.solvers.mathsat5.Mathsat5NativeApi.MSAT_TAG_FP_TO_UBV;
6868
import static org.sosy_lab.java_smt.solvers.mathsat5.Mathsat5NativeApi.MSAT_TAG_IFF;
69+
import static org.sosy_lab.java_smt.solvers.mathsat5.Mathsat5NativeApi.MSAT_TAG_INT_FROM_SBV;
70+
import static org.sosy_lab.java_smt.solvers.mathsat5.Mathsat5NativeApi.MSAT_TAG_INT_FROM_UBV;
71+
import static org.sosy_lab.java_smt.solvers.mathsat5.Mathsat5NativeApi.MSAT_TAG_INT_TO_BV;
6972
import static org.sosy_lab.java_smt.solvers.mathsat5.Mathsat5NativeApi.MSAT_TAG_ITE;
7073
import static org.sosy_lab.java_smt.solvers.mathsat5.Mathsat5NativeApi.MSAT_TAG_LEQ;
7174
import static org.sosy_lab.java_smt.solvers.mathsat5.Mathsat5NativeApi.MSAT_TAG_NOT;
@@ -411,6 +414,8 @@ private FunctionDeclarationKind getDeclarationKind(long pF) {
411414
return FunctionDeclarationKind.LTE;
412415
case MSAT_TAG_EQ:
413416
return FunctionDeclarationKind.EQ;
417+
case MSAT_TAG_INT_TO_BV:
418+
return FunctionDeclarationKind.INT_TO_BV;
414419
case MSAT_TAG_ARRAY_READ:
415420
return FunctionDeclarationKind.SELECT;
416421
case MSAT_TAG_ARRAY_WRITE:
@@ -468,6 +473,10 @@ private FunctionDeclarationKind getDeclarationKind(long pF) {
468473
return FunctionDeclarationKind.BV_ROTATE_LEFT_BY_INT;
469474
case MSAT_TAG_BV_ROR:
470475
return FunctionDeclarationKind.BV_ROTATE_RIGHT_BY_INT;
476+
case MSAT_TAG_INT_FROM_UBV:
477+
return FunctionDeclarationKind.UBV_TO_INT;
478+
case MSAT_TAG_INT_FROM_SBV:
479+
return FunctionDeclarationKind.SBV_TO_INT;
471480

472481
case MSAT_TAG_FP_NEG:
473482
return FunctionDeclarationKind.FP_NEG;

src/org/sosy_lab/java_smt/solvers/z3/Z3FormulaCreator.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,8 @@ private FunctionDeclarationKind getDeclarationKind(long f) {
658658
return FunctionDeclarationKind.FLOOR;
659659
case Z3_OP_TO_REAL:
660660
return FunctionDeclarationKind.TO_REAL;
661+
case Z3_OP_INT2BV:
662+
return FunctionDeclarationKind.INT_TO_BV;
661663

662664
case Z3_OP_UNINTERPRETED:
663665
return FunctionDeclarationKind.UF;
@@ -763,6 +765,10 @@ private FunctionDeclarationKind getDeclarationKind(long f) {
763765
return FunctionDeclarationKind.BV_ROTATE_LEFT;
764766
case Z3_OP_EXT_ROTATE_RIGHT:
765767
return FunctionDeclarationKind.BV_ROTATE_RIGHT;
768+
case Z3_OP_BV2INT:
769+
return FunctionDeclarationKind.UBV_TO_INT;
770+
case Z3_OP_SBV2INT:
771+
return FunctionDeclarationKind.SBV_TO_INT;
766772

767773
case Z3_OP_FPA_NEG:
768774
return FunctionDeclarationKind.FP_NEG;

src/org/sosy_lab/java_smt/test/SolverVisitorTest.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,57 @@ public void rationalConstantVisit() {
298298
}
299299
}
300300

301+
@Test
302+
public void integerDivisionVisit() {
303+
requireIntegers();
304+
assume().that(solver).isNotEqualTo(Solvers.PRINCESS); // Princess will rewrite the term
305+
306+
IntegerFormula x = imgr.makeVariable("x");
307+
IntegerFormula y = imgr.makeVariable("y");
308+
IntegerFormula c = imgr.makeNumber(7);
309+
310+
if (solver.equals(Solvers.MATHSAT5)) {
311+
// MathSAT will rewrite if we don't use a variable in the denominator
312+
checkKind(imgr.divide(x, y), FunctionDeclarationKind.DIV);
313+
} else {
314+
// Otherwise, just use a constant to support solvers that don't have non-linear arithmetics
315+
checkKind(imgr.divide(x, c), FunctionDeclarationKind.DIV);
316+
}
317+
}
318+
319+
@Test
320+
public void integerToBitvectorConversionVisit() {
321+
requireIntegers();
322+
requireBitvectors();
323+
324+
// Yices does not support integer to bitvector conversions
325+
assume().that(solver).isNotEqualTo(Solvers.YICES2);
326+
// Princess uses mod_casts internally, which makes it hard to figure out when conversion happen
327+
// TODO Find out if mod_cast/int_cast could be mapped to (S)BV_TO_INT and INT_TO_BV
328+
assume().that(solver).isNotEqualTo(Solvers.PRINCESS);
329+
330+
IntegerFormula x = imgr.makeVariable("x");
331+
checkKind(bvmgr.makeBitvector(8, x), FunctionDeclarationKind.INT_TO_BV);
332+
}
333+
334+
@Test
335+
public void bitvectorToIntegerConversionVisit() {
336+
requireIntegers();
337+
requireBitvectors();
338+
339+
// Yices does not support integer to bitvector conversions
340+
assume().that(solver).isNotEqualTo(Solvers.YICES2);
341+
// CVC4, CVC5 and Z3 will rewrite SBV_TO_INT to a term that only uses unsigned integers
342+
assume().that(solver).isNoneOf(Solvers.Z3, Solvers.CVC4, Solvers.CVC5);
343+
// Princess uses mod_casts internally, which makes it hard to figure out when conversion happen
344+
assume().that(solver).isNotEqualTo(Solvers.PRINCESS);
345+
346+
BitvectorFormula y = bvmgr.makeVariable(8, "y");
347+
348+
checkKind(bvmgr.toIntegerFormula(y, true), FunctionDeclarationKind.SBV_TO_INT);
349+
checkKind(bvmgr.toIntegerFormula(y, false), FunctionDeclarationKind.UBV_TO_INT);
350+
}
351+
301352
@Test
302353
public void arrayVisit() {
303354
requireArrays();

0 commit comments

Comments
 (0)