Search in sources :

Example 1 with BitVectorType

use of edu.stanford.CVC4.BitVectorType in project java-smt by sosy-lab.

the class CVC4BitvectorFormulaManager method divide.

@Override
protected Expr divide(Expr numerator, Expr denumerator, boolean signed) {
    final Kind operator = signed ? Kind.BITVECTOR_SDIV : Kind.BITVECTOR_UDIV;
    final Expr division = exprManager.mkExpr(operator, numerator, denumerator);
    // CVC4 does not align with SMTLIB standard when it comes to divide-by-zero.
    // For divide-by-zero, we compute the result as: return "1" with the opposite
    // sign than the numerator.
    final int bitsize = ((BitvectorType) formulaCreator.getFormulaType(numerator)).getSize();
    final Expr zero = makeBitvectorImpl(bitsize, 0);
    final Expr one = makeBitvectorImpl(bitsize, 1);
    // all bits equal "1"
    final Expr maxValue = makeBitvectorImpl(bitsize, -1);
    return exprManager.mkExpr(Kind.ITE, exprManager.mkExpr(Kind.EQUAL, denumerator, zero), exprManager.mkExpr(Kind.ITE, lessThan(numerator, zero, signed), one, maxValue), division);
}
Also used : CVC4.vectorExpr(edu.stanford.CVC4.vectorExpr) Expr(edu.stanford.CVC4.Expr) BitvectorType(org.sosy_lab.java_smt.api.FormulaType.BitvectorType) Kind(edu.stanford.CVC4.Kind)

Example 2 with BitVectorType

use of edu.stanford.CVC4.BitVectorType in project java-smt by sosy-lab.

the class CVC4BitvectorFormulaManager method modulo.

@Override
protected Expr modulo(Expr numerator, Expr denumerator, boolean signed) {
    final Kind operator = signed ? Kind.BITVECTOR_SREM : Kind.BITVECTOR_UREM;
    final Expr remainder = exprManager.mkExpr(operator, numerator, denumerator);
    // CVC4 does not align with SMTLIB standard when it comes to modulo-by-zero.
    // For modulo-by-zero, we compute the result as: "return the numerator".
    final int bitsize = ((BitvectorType) formulaCreator.getFormulaType(numerator)).getSize();
    final Expr zero = makeBitvectorImpl(bitsize, 0);
    return exprManager.mkExpr(Kind.ITE, exprManager.mkExpr(Kind.EQUAL, denumerator, zero), numerator, remainder);
}
Also used : CVC4.vectorExpr(edu.stanford.CVC4.vectorExpr) Expr(edu.stanford.CVC4.Expr) BitvectorType(org.sosy_lab.java_smt.api.FormulaType.BitvectorType) Kind(edu.stanford.CVC4.Kind)

Example 3 with BitVectorType

use of edu.stanford.CVC4.BitVectorType in project java-smt by sosy-lab.

the class CVC4FormulaCreator method getFormulaTypeFromTermType.

private FormulaType<?> getFormulaTypeFromTermType(Type t) {
    if (t.isBoolean()) {
        return FormulaType.BooleanType;
    } else if (t.isInteger()) {
        return FormulaType.IntegerType;
    } else if (t.isBitVector()) {
        // not hold, hence we use the new BitVectorType(t) here as a workaround:
        return FormulaType.getBitvectorTypeWithSize((int) new BitVectorType(t).getSize());
    } else if (t.isFloatingPoint()) {
        edu.stanford.CVC4.FloatingPointType fpType = new edu.stanford.CVC4.FloatingPointType(t);
        return FormulaType.getFloatingPointType((int) fpType.getExponentSize(), // without sign bit
        (int) fpType.getSignificandSize() - 1);
    } else if (t.isRoundingMode()) {
        return FormulaType.FloatingPointRoundingModeType;
    } else if (t.isReal()) {
        // As such, the theory RATIONAL is contained in REAL. TODO: find a better solution.
        return FormulaType.RationalType;
    } else if (t.isArray()) {
        // instead of casting, create a new type.
        ArrayType arrayType = new ArrayType(t);
        FormulaType<?> indexType = getFormulaTypeFromTermType(arrayType.getIndexType());
        FormulaType<?> elementType = getFormulaTypeFromTermType(arrayType.getConstituentType());
        return FormulaType.getArrayType(indexType, elementType);
    } else if (t.isString()) {
        return FormulaType.StringType;
    } else if (t.isRegExp()) {
        return FormulaType.RegexType;
    } else {
        throw new AssertionError(String.format("Unhandled type '%s' with base type '%s'.", t, t.getBaseType()));
    }
}
Also used : ArrayType(edu.stanford.CVC4.ArrayType) BitVectorType(edu.stanford.CVC4.BitVectorType) FloatingPointType(org.sosy_lab.java_smt.api.FormulaType.FloatingPointType)

Example 4 with BitVectorType

use of edu.stanford.CVC4.BitVectorType in project java-smt by sosy-lab.

the class CVC4NativeAPITest method checkInvalidTypeOperationsCheckSat.

/**
 * It does not matter if you take an int or array or bv here, all result in the same error.
 */
@Test
public void checkInvalidTypeOperationsCheckSat() {
    BitVectorType bvType = exprMgr.mkBitVectorType(16);
    Expr bvVar = exprMgr.mkVar("bla", bvType);
    Expr assertion = exprMgr.mkExpr(Kind.AND, bvVar, bvVar);
    Exception e = assertThrows(edu.stanford.CVC4.Exception.class, () -> smtEngine.checkSat(assertion));
    assertThat(e.toString()).contains("expecting a Boolean subexpression");
}
Also used : CVC4.vectorExpr(edu.stanford.CVC4.vectorExpr) SExpr(edu.stanford.CVC4.SExpr) Expr(edu.stanford.CVC4.Expr) BitVectorType(edu.stanford.CVC4.BitVectorType) AssumptionViolatedException(org.junit.AssumptionViolatedException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Test(org.junit.Test)

Example 5 with BitVectorType

use of edu.stanford.CVC4.BitVectorType in project java-smt by sosy-lab.

the class CVC4BitvectorFormulaManager method toIntegerFormulaImpl.

@Override
protected Expr toIntegerFormulaImpl(Expr pBv, boolean pSigned) {
    Expr intExpr = exprManager.mkExpr(Kind.BITVECTOR_TO_NAT, pBv);
    // CVC4 returns unsigned int by default
    if (pSigned) {
        // TODO check what is cheaper for the solver:
        // checking the first BV-bit or computing max-int-value for the given size
        final int size = Math.toIntExact(new BitVectorType(pBv.getType()).getSize());
        final BigInteger modulo = BigInteger.ONE.shiftLeft(size);
        final BigInteger maxInt = BigInteger.ONE.shiftLeft(size - 1).subtract(BigInteger.ONE);
        final Expr moduloExpr = exprManager.mkConst(new Rational(modulo.toString()));
        final Expr maxIntExpr = exprManager.mkConst(new Rational(maxInt.toString()));
        intExpr = exprManager.mkExpr(Kind.ITE, exprManager.mkExpr(Kind.GT, intExpr, maxIntExpr), exprManager.mkExpr(Kind.MINUS, intExpr, moduloExpr), intExpr);
    }
    return intExpr;
}
Also used : CVC4.vectorExpr(edu.stanford.CVC4.vectorExpr) Expr(edu.stanford.CVC4.Expr) Rational(edu.stanford.CVC4.Rational) BigInteger(java.math.BigInteger) BitVectorType(edu.stanford.CVC4.BitVectorType)

Aggregations

Expr (edu.stanford.CVC4.Expr)6 CVC4.vectorExpr (edu.stanford.CVC4.vectorExpr)5 BitVectorType (edu.stanford.CVC4.BitVectorType)4 BitvectorType (org.sosy_lab.java_smt.api.FormulaType.BitvectorType)3 Kind (edu.stanford.CVC4.Kind)2 SExpr (edu.stanford.CVC4.SExpr)2 FileNotFoundException (java.io.FileNotFoundException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 AssumptionViolatedException (org.junit.AssumptionViolatedException)2 Test (org.junit.Test)2 FloatingPointType (org.sosy_lab.java_smt.api.FormulaType.FloatingPointType)2 ArrayType (edu.stanford.CVC4.ArrayType)1 FloatingPointConvertSort (edu.stanford.CVC4.FloatingPointConvertSort)1 FloatingPointToFPFloatingPoint (edu.stanford.CVC4.FloatingPointToFPFloatingPoint)1 FloatingPointToSBV (edu.stanford.CVC4.FloatingPointToSBV)1 Rational (edu.stanford.CVC4.Rational)1 BigInteger (java.math.BigInteger)1