예제 #1
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));
 }
예제 #2
0
        private string HexToBinary(char hex)
        {
            switch (hex)
            {
            case '0':
                return("0000");

            case '1':
                return("0001");

            case '2':
                return("0010");

            case '3':
                return("0011");

            case '4':
                return("0100");

            case '5':
                return("0101");

            case '6':
                return("0110");

            case '7':
                return("0111");

            case '8':
                return("1000");

            case '9':
                return("1001");

            case 'a':
            case 'A':
                return("1010");

            case 'b':
            case 'B':
                return("1011");

            case 'c':
            case 'C':
                return("1100");

            case 'd':
            case 'D':
                return("1101");

            case 'e':
            case 'E':
                return("1110");

            case 'f':
            case 'F':
                return("1111");

            default:
                Print.ExitMessage("Unhandled hex character " + hex);
                return(string.Empty);
            }
        }
예제 #3
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);
        }