public void IfTrue()
        {
            var constantBuilder = GetConstantFoldingBuilder();
            var id0             = GetVarAndIdExpr("foo", BasicType.GetBvType(8)).Item2;
            var id1             = GetVarAndIdExpr("bar", BasicType.GetBvType(8)).Item2;
            var result          = constantBuilder.IfThenElse(constantBuilder.True, id0, id1);

            Assert.AreEqual("foo", result.ToString());
            CheckIsBvType(result, 8);
            Assert.AreSame(result, id0);
        }
Beispiel #2
0
        public void SameExprAdded()
        {
            var cfb    = GetConstantFoldingBuilder();
            var x      = GetVarAndIdExpr("x", BasicType.GetBvType(8)).Item2;
            var y      = GetVarAndIdExpr("x", BasicType.GetBvType(8)).Item2;
            var result = cfb.BVSUB(x, x);

            Assert.IsNotNull(ExprUtil.AsLiteral(result));
            CheckIsBvType(result, 8);
            Assert.IsTrue(ExprUtil.IsZero(result));
        }
Beispiel #3
0
        public void ConcatExtractsToOriginal()
        {
            var cfb    = GetConstantFoldingBuilder();
            var id     = GetVarAndIdExpr("x", BasicType.GetBvType(16)).Item2;
            var msb    = cfb.BVEXTRACT(id, 16, 8);
            var lsb    = cfb.BVEXTRACT(id, 8, 0);
            var result = cfb.BVCONCAT(msb, lsb);

            CheckIsBvType(result, 16);
            Assert.AreSame(id, result);
        }
Beispiel #4
0
        public void XorWithSameExpr()
        {
            var cfb    = GetConstantFoldingBuilder();
            var id     = GetVarAndIdExpr("x", BasicType.GetBvType(8)).Item2;
            var result = cfb.BVXOR(id, id);

            CheckIsBvType(result, 8);
            var resultAsLit = ExprUtil.AsLiteral(result);

            Assert.IsTrue(ExprUtil.IsZero(resultAsLit));
        }
Beispiel #5
0
        public void mulByRhsZero()
        {
            var cfb    = GetConstantFoldingBuilder();
            var x      = GetVarAndIdExpr("x", BasicType.GetBvType(8)).Item2;
            var result = cfb.BVMUL(x, cfb.ConstantBV(0, 8));

            CheckIsBvType(result, 8);
            var asLit = ExprUtil.AsLiteral(result);

            Assert.IsNotNull(asLit);
            Assert.IsTrue(ExprUtil.IsZero(asLit));
        }
Beispiel #6
0
        public void IfConditionThenTrueElseFalse()
        {
            var builder   = GetConstantFoldingBuilder();
            var id0       = GetVarAndIdExpr("foo", BasicType.GetBvType(8)).Item2;
            var id1       = GetVarAndIdExpr("bar", BasicType.GetBvType(8)).Item2;
            var condition = builder.Eq(id0, id1);
            var result    = builder.IfThenElse(condition, builder.True, builder.False);

            CheckIsBoolType(result);
            Assert.IsNull(ExprUtil.AsLiteral(result));
            Assert.AreSame(condition, result);
        }
Beispiel #7
0
        public void lhsIsZero()
        {
            var cfb    = GetConstantFoldingBuilder();
            var x      = GetVarAndIdExpr("x", BasicType.GetBvType(8)).Item2;
            var result = cfb.BVSUB(cfb.ConstantBV(0, 8), x);

            CheckIsBvType(result, 8);
            var asBvNeg = ExprUtil.AsBVNEG(result);

            Assert.IsNotNull(asBvNeg);
            Assert.AreSame(x, asBvNeg.Args[0]);
        }
Beispiel #8
0
 public void RemByOne()
 {
     var cfb = GetConstantFoldingBuilder();
     var dividend = GetVarAndIdExpr("x", BasicType.GetBvType(4)).Item2;
     var divisor = cfb.ConstantBV(1, 4);
     var result = cfb.BVSMOD(dividend, divisor);
     CheckIsBvType(result, 4);
     var asLit = ExprUtil.AsLiteral(result);
     Assert.IsNotNull(asLit);
     CheckIsBvType(result, 4);
     Assert.IsTrue(ExprUtil.IsZero(result));
 }
Beispiel #9
0
        public void MSBIsZero()
        {
            var cfb    = GetConstantFoldingBuilder();
            var msb    = cfb.ConstantBV(0, 8);
            var lsb    = GetVarAndIdExpr("x", BasicType.GetBvType(4)).Item2;
            var result = cfb.BVCONCAT(msb, lsb);

            var asBvZExt = ExprUtil.AsBVZEXT(result);

            Assert.IsNotNull(asBvZExt);
            CheckIsBvType(result, 12);
            Assert.AreSame(lsb, asBvZExt.Args[0]);
        }
Beispiel #10
0
        public void MergeNotEqToCondition()
        {
            var builder   = GetConstantFoldingBuilder();
            var v         = GetVarAndIdExpr("group_size_y", BasicType.GetBvType(32)).Item2;
            var condition = builder.Eq(v, builder.ConstantBV(1, 32));
            var ite       = builder.IfThenElse(condition, builder.ConstantBV(1, 1), builder.ConstantBV(0, 1));

            Assert.IsNull(ExprUtil.AsLiteral(ite));
            var result = builder.NotEq(ite, builder.ConstantBV(0, 1));

            CheckIsBoolType(result);
            Assert.AreSame(condition, result);
        }
Beispiel #11
0
        public void XorWithAllOnes()
        {
            var cfb     = GetConstantFoldingBuilder();
            var id      = GetVarAndIdExpr("x", BasicType.GetBvType(8)).Item2;
            var allOnes = cfb.ConstantBV(255, 8);
            var result  = cfb.BVXOR(id, allOnes);

            CheckIsBvType(result, 8);
            var resultAsBvNot = ExprUtil.AsBVNOT(result);

            Assert.IsNotNull(resultAsBvNot);
            Assert.AreSame(id, resultAsBvNot.Args[0]);
        }
        public void CreateFunctionCallExprWrongArgCount()
        {
            var FCB = GetFCBuilder();
            var SEB = GetSEBuilder();
            var fc  = FCB.CreateCachedUninterpretedFunctionCall("foo",
                                                                BasicType.Bool,
                                                                new List <Microsoft.Boogie.Type>()
            {
                BasicType.GetBvType(2),
                BasicType.GetBvType(2)
            });

            SEB.UFC(fc, SEB.ConstantBV(0, 3));
        }
Beispiel #13
0
        public void simpleUF()
        {
            var builder = GetSimpleBuilder();
            var FCB     = new FunctionCallBuilder();
            var func    = FCB.CreateUninterpretedFunctionCall("foo", BasicType.Bool, new List <Microsoft.Boogie.Type>()
            {
                BasicType.Real,
                BasicType.Int,
                BasicType.GetBvType(8)
            });
            var root = builder.UFC(func, builder.ConstantReal("0.0"), builder.ConstantInt(5), builder.ConstantBV(5, 8));

            Assert.AreEqual("foo(0e0, 5, 5bv8)", root.ToString());
            DuplicateAndCheck(root, builder);
        }
Beispiel #14
0
 public void NoFold()
 {
     var builders = GetSimpleAndConstantFoldingBuilder();
     SimpleExprBuilder sfb = builders.Item1;
     ConstantFoldingExprBuilder cfb = builders.Item2;
     var arg0 = GetVarAndIdExpr("x", BasicType.GetBvType(8)).Item2;
     var arg1 = GetVarAndIdExpr("y", BasicType.GetBvType(8)).Item2;
     var simpleResult = sfb.BVSMOD(arg0, arg1);
     var result = cfb.BVSMOD(arg0, arg1);
     CheckIsBvType(result, 8);
     CheckIsBvType(simpleResult, 8);
     Assert.IsNull(ExprUtil.AsLiteral(result));
     Assert.IsNotNull(ExprUtil.AsBVSMOD(result));
     Assert.IsTrue(ExprUtil.StructurallyEqual(result, simpleResult));
 }
Beispiel #15
0
        public void NoFold()
        {
            var builderPair  = GetSimpleAndConstantFoldingBuilder();
            var sfb          = builderPair.Item1;
            var cfb          = builderPair.Item2;
            var v0           = GetVarAndIdExpr("x", BasicType.GetBvType(8));
            var v1           = GetVarAndIdExpr("y", BasicType.GetBvType(8));
            var foldedResult = cfb.BVLSHR(v0.Item2, v1.Item2);
            var simpleResult = sfb.BVLSHR(v0.Item2, v1.Item2);

            CheckIsBvType(foldedResult, 8);
            CheckIsBvType(simpleResult, 8);
            Assert.IsNotNull(ExprUtil.AsBVLSHR(foldedResult));
            Assert.AreEqual(simpleResult, foldedResult);
        }
Beispiel #16
0
        public Expr GetUnaryBVFunction(Microsoft.Boogie.Type returnType, string NameWithoutSizeSuffx, string builtin, Expr operand, bool getSuffixFromReturnType = false)
        {
            if (!operand.Type.IsBv)
            {
                throw new ExprTypeCheckException("operand must be BvType");
            }

            int    bits         = operand.Type.BvBits;
            string suffixString = null;

            if (getSuffixFromReturnType)
            {
                if (!returnType.IsBv)
                {
                    throw new ArgumentException("expected return type to be BvType");
                }
                suffixString = returnType.BvBits.ToString();
            }
            else
            {
                suffixString = bits.ToString();
            }

            var          functionName        = NameWithoutSizeSuffx + suffixString;
            FunctionCall builtinFunctionCall = null;

            try
            {
                builtinFunctionCall = CachedFunctions[functionName];
            }
            catch (KeyNotFoundException)
            {
                // Cache miss, build the FunctionCall
                builtinFunctionCall = CreateBVBuiltIn(functionName,
                                                      builtin, returnType, new List <Microsoft.Boogie.Type>()
                {
                    BasicType.GetBvType(bits)
                });
                CachedFunctions[functionName] = builtinFunctionCall;
            }

            var result = GetNAry(builtinFunctionCall, new List <Expr>()
            {
                operand
            });

            return(result);
        }
Beispiel #17
0
        public void OvershiftExpr(int bitWidth, int shiftWidth)
        {
            var builders     = GetSimpleAndConstantFoldingBuilder();
            var sb           = builders.Item1;
            var cfb          = builders.Item2;
            var id           = GetVarAndIdExpr("x", BasicType.GetBvType(bitWidth)).Item2;
            var result       = cfb.BVASHR(id, cfb.ConstantBV(shiftWidth, bitWidth));
            var simpleResult = sb.BVASHR(id, sb.ConstantBV(shiftWidth, bitWidth));

            CheckIsBvType(result, bitWidth);
            CheckIsBvType(simpleResult, bitWidth);
            Assert.IsFalse(ExprUtil.IsZero(result));
            Assert.IsNotNull(ExprUtil.AsBVASHR(result));
            Assert.IsNotNull(ExprUtil.AsBVASHR(simpleResult));
            Assert.AreEqual(simpleResult, result);
        }
Beispiel #18
0
        public void ConcatExtractsToSingleExtract()
        {
            var cfb    = GetConstantFoldingBuilder();
            var id     = GetVarAndIdExpr("x", BasicType.GetBvType(16)).Item2;
            var msb    = cfb.BVEXTRACT(id, 16, 8);
            var lsb    = cfb.BVEXTRACT(id, 8, 5);
            var result = cfb.BVCONCAT(msb, lsb);

            CheckIsBvType(result, 11);
            var asBvExtract = ExprUtil.AsBVEXTRACT(result);

            Assert.IsNotNull(asBvExtract);
            Assert.AreSame(id, asBvExtract.Bitvector);
            Assert.AreEqual(16, asBvExtract.End);
            Assert.AreEqual(5, asBvExtract.Start);
        }
Beispiel #19
0
        private Expr GetBinaryBVFunction(Microsoft.Boogie.Type returnType, string NameWithoutSizeSuffx, string builtin, Expr lhs, Expr rhs)
        {
            if (!lhs.Type.IsBv)
            {
                throw new ExprTypeCheckException("lhs must be bitvector");
            }

            if (!rhs.Type.IsBv)
            {
                throw new ExprTypeCheckException("rhs must be bitvector");
            }

            if (!lhs.Type.Equals(rhs.Type))
            {
                throw new ExprTypeCheckException("bitwidth mistmatch");
            }

            int bits = lhs.Type.BvBits;

            Debug.Assert(bits == rhs.Type.BvBits);

            var          functionName        = NameWithoutSizeSuffx + bits.ToString();
            FunctionCall builtinFunctionCall = null;

            try
            {
                builtinFunctionCall = CachedFunctions[functionName];
            }
            catch (KeyNotFoundException)
            {
                // Cache miss, build the FunctionCall
                builtinFunctionCall = CreateBVBuiltIn(functionName,
                                                      builtin, returnType, new List <Microsoft.Boogie.Type>()
                {
                    BasicType.GetBvType(bits),
                    BasicType.GetBvType(bits)
                });
                CachedFunctions[functionName] = builtinFunctionCall;
            }

            var result = GetNAry(builtinFunctionCall, new List <Expr>()
            {
                lhs, rhs
            });

            return(result);
        }
Beispiel #20
0
        public void noFold()
        {
            var pair         = GetSimpleAndConstantFoldingBuilder();
            var sb           = pair.Item1;
            var cfb          = pair.Item2;
            var id           = GetVarAndIdExpr("foo", BasicType.GetBvType(8)).Item2;
            var foldedResult = cfb.BVZEXT(id, 16);
            var simpleResult = sb.BVZEXT(id, 16);

            CheckIsBvType(foldedResult, 16);
            CheckIsBvType(simpleResult, 16);
            Assert.AreEqual(simpleResult, foldedResult);

            var asBvZExt = ExprUtil.AsBVZEXT(foldedResult);

            Assert.IsNotNull(asBvZExt);
            Assert.AreSame(id, asBvZExt.Args[0]);
        }
        public void CreateFunctionCallExpr()
        {
            var FCB = GetFCBuilder();
            var SEB = GetSEBuilder();
            var fc  = FCB.CreateCachedUninterpretedFunctionCall("foo",
                                                                BasicType.Bool,
                                                                new List <Microsoft.Boogie.Type>()
            {
                BasicType.GetBvType(2),
                BasicType.GetBvType(2)
            });
            var call = SEB.UFC(fc, SEB.ConstantBV(0, 2), SEB.ConstantBV(1, 2));

            Assert.AreEqual("foo(0bv2, 1bv2)", call.ToString());
            Assert.AreEqual(BasicType.Bool, call.ShallowType);
            Assert.IsNotNull(call.Type);
            Assert.AreEqual(BasicType.Bool, call.Type);
        }
        public void CreateFunctionCallMistmatchWrongNumberOfArgs()
        {
            var FCB = GetFCBuilder();

            FCB.CreateCachedUninterpretedFunctionCall("foo",
                                                      BasicType.Bool,
                                                      new List <Microsoft.Boogie.Type>()
            {
                BasicType.GetBvType(2),
                BasicType.GetBvType(2)
            });
            // SimpleExpr builder should hit its cache if we ask for foo again. We ask for the wrong number of args here
            FCB.CreateCachedUninterpretedFunctionCall("foo",
                                                      BasicType.Bool,
                                                      new List <Microsoft.Boogie.Type>()
            {
                BasicType.GetBvType(2)
            });
        }
Beispiel #23
0
        public void noFold()
        {
            var pair         = GetSimpleAndConstantFoldingBuilder();
            var sb           = pair.Item1;
            var cfb          = pair.Item2;
            var id           = GetVarAndIdExpr("foo", BasicType.GetBvType(8)).Item2;
            var foldedResult = cfb.BVEXTRACT(id, 7, 0);
            var simpleResult = sb.BVEXTRACT(id, 7, 0);

            CheckIsBvType(foldedResult, 7);
            CheckIsBvType(simpleResult, 7);
            Assert.AreEqual(simpleResult, foldedResult);

            var asBvExtract = ExprUtil.AsBVEXTRACT(foldedResult);

            Assert.IsNotNull(asBvExtract);
            Assert.AreSame(id, asBvExtract.Bitvector);
            Assert.AreEqual(0, asBvExtract.Start);
            Assert.AreEqual(7, asBvExtract.End);
        }
Beispiel #24
0
        public void nestedBVSEXT(int depth)
        {
            var  pair     = GetVarAndIdExpr("foo", BasicType.GetBvType(8));
            var  variable = pair.Item1;
            var  id       = pair.Item2;
            var  cfb      = GetConstantFoldingBuilder();
            Expr result   = cfb.BVSEXT(id, 8);

            Assert.IsNotNull(ExprUtil.AsBVSEXT(result));
            Expr root = result;

            for (int i = 0; i < depth; ++i)
            {
                // Sign extending to the same width should be a no-op
                result = cfb.BVSEXT(result, 8);
                CheckIsBvType(result, 8);
            }

            Assert.AreSame(root, result);
        }
        public void CreateFunctionCallDistinct()
        {
            var FCB = GetFCBuilder();
            var fc0 = FCB.CreateCachedUninterpretedFunctionCall("foo",
                                                                BasicType.Bool,
                                                                new List <Microsoft.Boogie.Type>()
            {
                BasicType.GetBvType(2),
                BasicType.GetBvType(2)
            });
            // SimpleExpr builder should not hit the cache here as we ask for different function name
            var fc1 = FCB.CreateCachedUninterpretedFunctionCall("foo2",
                                                                BasicType.Bool,
                                                                new List <Microsoft.Boogie.Type>()
            {
                BasicType.GetBvType(2),
                BasicType.GetBvType(2)
            });

            Assert.AreNotSame(fc0, fc1);
        }
        public void CreateFunctionCall()
        {
            var FCB = GetFCBuilder();
            var fc0 = FCB.CreateCachedUninterpretedFunctionCall("foo",
                                                                BasicType.Bool,
                                                                new List <Microsoft.Boogie.Type>()
            {
                BasicType.GetBvType(2),
                BasicType.GetBvType(2)
            });
            // SimpleExpr builder should hit its cache if we ask for foo again
            var fc1 = FCB.CreateCachedUninterpretedFunctionCall("foo",
                                                                BasicType.Bool,
                                                                new List <Microsoft.Boogie.Type>()
            {
                BasicType.GetBvType(2),
                BasicType.GetBvType(2)
            });

            Assert.AreSame(fc0, fc1);
        }
Beispiel #27
0
        public void SameExprAdded()
        {
            var cfb  = GetConstantFoldingBuilder();
            var x    = GetVarAndIdExpr("x", BasicType.GetBvType(8)).Item2;
            var y    = GetVarAndIdExpr("x", BasicType.GetBvType(8)).Item2;
            var side = cfb.BVADD(x, y);

            CheckIsBvType(side, 8);
            Assert.IsNull(ExprUtil.AsLiteral(side));
            var result = cfb.BVADD(side, side);

            CheckIsBvType(result, 8);
            var mul = ExprUtil.AsBVMUL(result);

            Assert.IsNotNull(mul);
            var lhs = ExprUtil.AsLiteral(mul.Args[0]);

            Assert.IsNotNull(lhs);
            Assert.AreEqual(2, lhs.asBvConst.Value.ToInt);
            Assert.AreSame(side, mul.Args[1]);
        }
Beispiel #28
0
        public Expr BVZEXT(Expr operand, int newWidth)
        {
            if (!operand.Type.IsBv)
            {
                throw new ExprTypeCheckException("operand must be BvType");
            }

            int originalWidth = operand.Type.BvBits;

            if (newWidth < originalWidth)
            {
                throw new ArgumentException("newWidth must be greater than the operand's bit width");
            }

            var functionNameWithoutSuffix = string.Format("BV{0}_ZEXT", originalWidth);
            var builtinName = string.Format("zero_extend {0}", (newWidth - originalWidth));
            var newType     = BasicType.GetBvType(newWidth);
            var result      = GetUnaryBVFunction(newType, functionNameWithoutSuffix, builtinName, operand, /*getSuffixFromReturnType=*/ true);

            result.Type = newType;
            return(result);
        }
Beispiel #29
0
        public void nestedBVZEXT(int depth)
        {
            var  pair     = GetVarAndIdExpr("foo", BasicType.GetBvType(4));
            var  variable = pair.Item1;
            var  id       = pair.Item2;
            var  cfb      = GetConstantFoldingBuilder();
            Expr result   = cfb.BVZEXT(id, 8);

            Assert.IsNotNull(ExprUtil.AsBVZEXT(result));
            for (int i = 0; i < depth; ++i)
            {
                // The newest (closest to the root) zero_extend superseeds any below
                result = cfb.BVZEXT(result, 8 + i);
                CheckIsBvType(result, 8 + i);
            }

            CheckIsBvType(result, depth + 8 - 1);
            var asBvZext = ExprUtil.AsBVZEXT(result);

            Assert.IsNotNull(asBvZext);
            Assert.AreSame(id, asBvZext.Args[0]);
        }
Beispiel #30
0
        public void ConcatExtractsNoFold()
        {
            var cfb = GetConstantFoldingBuilder();
            var id0 = GetVarAndIdExpr("x", BasicType.GetBvType(16)).Item2;
            var id1 = GetVarAndIdExpr("y", BasicType.GetBvType(16)).Item2;
            var msb = cfb.BVEXTRACT(id0, 16, 8);
            var lsb = cfb.BVEXTRACT(id1, 8, 0);

            // Different variables so this should not be simplified to a single extract
            var result = cfb.BVCONCAT(msb, lsb);

            // FIXME: Can't check type due to bugs in Boogie. It tries to change the type on immutabl Expr
            // CheckIsBvType(result, 16);
            Assert.IsTrue(result.Type.IsBv);
            Assert.AreEqual(16, result.Type.BvBits);

            var asBvConcat = ExprUtil.AsBVCONCAT(result);

            Assert.IsNotNull(asBvConcat);
            Assert.AreSame(msb, asBvConcat.E0);
            Assert.AreSame(lsb, asBvConcat.E1);
        }