Пример #1
0
        public static BitVector operator %(BitVector a, BitVector b)
        {
            Print.ConditionalExitMessage(a.Bits.Length == b.Bits.Length, "% operator : Bit vectors must have equal widths");
            int width = a.Bits.Length;

            try
            {
                if (width <= 32)
                {
                    int aData = Convert.ToInt32(a.Bits, 2);
                    int bData = Convert.ToInt32(b.Bits, 2);
                    return(new BitVector(checked (aData % bData), width));
                }
                else
                {
                    long aData = Convert.ToInt64(a.Bits, 2);
                    long bData = Convert.ToInt64(b.Bits, 2);
                    return(new BitVector(checked (aData % bData), width));
                }
            }
            catch (DivideByZeroException)
            {
                throw;
            }
        }
Пример #2
0
 public static bool operator ==(BitVector a, BitVector b)
 {
     Print.ConditionalExitMessage(a.Bits.Length == b.Bits.Length, "== operator : Bit vectors must have equal widths");
     if (ReferenceEquals(a, null))
     {
         return(ReferenceEquals(b, null));
     }
     return(a.Equals(b));
 }
Пример #3
0
        public static BitVector Slice(BitVector a, int high, int low)
        {
            Print.ConditionalExitMessage(high > low, "Slicing " + a.ToString() +
                                         " is not defined because the slice [" + high.ToString() + ":" + low.ToString() + "] is not valid");
            int    startIndex = a.Bits.Length - high;
            int    length     = high - low;
            string bits       = a.Bits.Substring(startIndex, length);

            return(new BitVector(bits));
        }
Пример #4
0
        public static BitVector operator |(BitVector a, BitVector b)
        {
            Print.ConditionalExitMessage(a.Bits.Length == b.Bits.Length, "| operator : Bit vectors must have equal widths");
            int width = a.Bits.Length;

            if (width <= 32)
            {
                int aData = Convert.ToInt32(a.Bits, 2);
                int bData = Convert.ToInt32(b.Bits, 2);
                return(new BitVector(aData | bData, width));
            }
            else
            {
                long aData = Convert.ToInt64(a.Bits, 2);
                long bData = Convert.ToInt64(b.Bits, 2);
                return(new BitVector(aData | bData, width));
            }
        }
Пример #5
0
        public BitVector GetValue(string name, SubscriptExpr subscript)
        {
            Print.ConditionalExitMessage(arrays.ContainsKey(name), string.Format("Unable to find array '{0}' in memory", name));
            Dictionary <SubscriptExpr, BitVector> arrayLocations = arrays[name];

            foreach (KeyValuePair <SubscriptExpr, BitVector> item in arrayLocations)
            {
                if (SubscriptExpr.Matches(item.Key, subscript))
                {
                    return(arrays[name][item.Key]);
                }
            }
            Print.WarningMessage(string.Format("Location '{0}' in array '{1}' has not been initialised", subscript.ToString(), name));

            // Assign a random value
            BitVector val = new BitVector(Random.Next(int.MinValue, int.MaxValue));

            arrays[name][subscript] = val;
            return(val);
        }
Пример #6
0
 public static BitVector operator ^(BitVector a, BitVector b)
 {
     Print.ConditionalExitMessage(a.Bits.Length == b.Bits.Length, "^ operator : Bit vectors must have equal widths");
     char[] bits = new char[a.Bits.Length];
     for (int i = 0; i < a.Bits.Length; ++i)
     {
         if (a.Bits[i] == '1' && b.Bits[i] == '0')
         {
             bits[i] = '1';
         }
         else if (a.Bits[i] == '0' && b.Bits[i] == '1')
         {
             bits[i] = '1';
         }
         else
         {
             bits[i] = '0';
         }
     }
     return(new BitVector(new string(bits)));
 }
Пример #7
0
        public static Node CreateFromExpr(Expr expr)
        {
            if (expr is NAryExpr)
            {
                NAryExpr nary = expr as NAryExpr;
                if (nary.Fun is IfThenElse)
                {
                    Node one    = CreateFromExpr(nary.Args[0]);
                    Node two    = CreateFromExpr(nary.Args[1]);
                    Node three  = CreateFromExpr(nary.Args[2]);
                    Node parent = new TernaryNode(nary.Fun.FunctionName, one, two, three);
                    one.parent   = parent;
                    two.parent   = parent;
                    three.parent = parent;
                    return(parent);
                }
                else if (nary.Fun is BinaryOperator)
                {
                    Node one    = CreateFromExpr(nary.Args[0]);
                    Node two    = CreateFromExpr(nary.Args[1]);
                    Node parent = new BinaryNode(nary.Fun.FunctionName, one, two);
                    one.parent = parent;
                    two.parent = parent;
                    return(parent);
                }
                else if (nary.Fun is UnaryOperator)
                {
                    Node      one    = CreateFromExpr(nary.Args[0]);
                    UnaryNode parent = new UnaryNode(nary.Fun.FunctionName, one);
                    one.parent = parent;
                    return(parent);
                }
                else if (nary.Fun is FunctionCall)
                {
                    FunctionCall call = nary.Fun as FunctionCall;
                    if (nary.Args.Count == 1)
                    {
                        Node      one    = CreateFromExpr(nary.Args[0]);
                        UnaryNode parent = new UnaryNode(nary.Fun.FunctionName, one);
                        one.parent = parent;
                        return(parent);
                    }
                    else if (nary.Args.Count == 2)
                    {
                        Node one    = CreateFromExpr(nary.Args[0]);
                        Node two    = CreateFromExpr(nary.Args[1]);
                        Node parent = new BinaryNode(call.FunctionName, one, two);
                        one.parent = parent;
                        two.parent = parent;
                        return(parent);
                    }
                    else
                    {
                        Print.ExitMessage("Unhandled number of arguments in Boogie function call with function: " + nary.Fun.FunctionName);
                    }
                }
                else if (nary.Fun is MapSelect)
                {
                    List <Expr> indices = new List <Expr>();
                    while (true)
                    {
                        NAryExpr nary2 = nary.Args[0] as NAryExpr;
                        Print.ConditionalExitMessage(nary.Args.Count == 2, "Map select has more than two arguments");
                        indices.Insert(0, nary.Args[1]);
                        if (nary2 == null)
                        {
                            break;
                        }
                        else
                        {
                            nary = nary2;
                        }
                    }

                    IdentifierExpr identifier = nary.Args[0] as IdentifierExpr;
                    Node           parent     = new MapSymbolNode(identifier.Name);
                    foreach (Expr index in indices)
                    {
                        Node child = CreateFromExpr(index);
                        parent.Children.Add(child);
                        child.parent = parent;
                    }

                    return(parent);
                }
                else
                {
                    Print.ExitMessage("Unhandled Nary expression: " + nary.Fun.GetType().ToString());
                }
            }
            else if (expr is IdentifierExpr)
            {
                IdentifierExpr identifier = expr as IdentifierExpr;
                return(new ScalarSymbolNode(identifier.Name, identifier.Type));
            }
            else if (expr is LiteralExpr)
            {
                LiteralExpr literal = expr as LiteralExpr;
                if (literal.Val is BvConst)
                {
                    BvConst bv = (BvConst)literal.Val;
                    return(new LiteralNode(new BitVector(bv)));
                }
                else if (literal.Val is BigNum)
                {
                    BigNum num = (BigNum)literal.Val;
                    return(new LiteralNode(new BitVector(num.ToInt)));
                }
                else if (literal.Val is bool)
                {
                    bool boolean = (bool)literal.Val;
                    if (boolean)
                    {
                        return(new LiteralNode(BitVector.True));
                    }
                    else
                    {
                        return(new LiteralNode(BitVector.False));
                    }
                }
                else
                {
                    Print.ExitMessage("Unhandled literal expression: " + literal.ToString());
                }
            }
            else if (expr is BvExtractExpr)
            {
                BvExtractExpr bvExtract = expr as BvExtractExpr;
                Node          child     = CreateFromExpr(bvExtract.Bitvector);
                Node          parent    = new BVExtractNode(child, bvExtract.End, bvExtract.Start);
                child.parent = parent;
                return(parent);
            }
            else if (expr is BvConcatExpr)
            {
                BvConcatExpr bvConcat = expr as BvConcatExpr;
                Node         one      = CreateFromExpr(bvConcat.E0);
                Node         two      = CreateFromExpr(bvConcat.E1);
                Node         parent   = new BVConcatenationNode(one, two);
                one.parent = parent;
                two.parent = parent;
                return(parent);
            }
            else if (expr is ForallExpr)
            {
                ForallExpr forall = expr as ForallExpr;
                Node       one    = CreateFromExpr(forall._Body);
                Node       parent = new ForAllNode(one);
                one.parent = parent;
                return(parent);
            }

            Print.ExitMessage("Unhandled expression tree: " + expr.ToString() + " " + expr.Type.ToString());
            return(null);
        }
Пример #8
0
 public static bool operator !=(BitVector a, BitVector b)
 {
     Print.ConditionalExitMessage(a.Bits.Length == b.Bits.Length, "!= operator : Bit vectors must have equal widths");
     return(!(a == b));
 }