예제 #1
0
        private bool TryComputeNAryExprs(IdentifierExpr id)
        {
            var toRemove = new HashSet <Expr>();

            foreach (var expr in this.ExpressionMap[id].Keys.ToList())
            {
                if (!(expr is NAryExpr))
                {
                    continue;
                }

                int ixs = 0;

                if (((expr as NAryExpr).Args[0] is IdentifierExpr) &&
                    ((expr as NAryExpr).Args[0] as IdentifierExpr).Name.StartsWith("$M."))
                {
                    toRemove.Add(expr);
                    continue;
                }

                if (PointerArithmeticAnalyser.ShouldSkipFromAnalysis(expr as NAryExpr))
                {
                    toRemove.Add(expr);
                    continue;
                }

                if (PointerArithmeticAnalyser.IsArithmeticExpression(expr as NAryExpr))
                {
                    toRemove.Add(expr);
                    continue;
                }

                Expr p = (expr as NAryExpr).Args[0];
                Expr i = (expr as NAryExpr).Args[1];
                Expr s = (expr as NAryExpr).Args[2];

                if (!(i is LiteralExpr && s is LiteralExpr))
                {
                    toRemove.Add(expr);
                    continue;
                }

                ixs = (i as LiteralExpr).asBigNum.ToInt * (s as LiteralExpr).asBigNum.ToInt;

                this.ExpressionMap[id].Add(p, this.ExpressionMap[id][expr] + ixs);
                toRemove.Add(expr);
            }

            foreach (var expr in toRemove)
            {
                this.ExpressionMap[id].Remove(expr);
            }

            if (this.ExpressionMap[id].Any(val => val.Key is NAryExpr))
            {
                return(false);
            }

            return(true);
        }
예제 #2
0
        /// <summary>
        /// Compute $pa(p, i, s) == p + i * s);
        /// </summary>
        /// <returns>The root pointer.</returns>
        /// <param name="impl">Implementation</param>
        /// <param name="label">Root block label</param>
        /// <param name="id">Identifier expression</param>
        public static Expr ComputeRootPointer(Implementation impl, string label, Expr id)
        {
            if (id is LiteralExpr)
            {
                return(id);
            }
            if (id is NAryExpr && (id as NAryExpr).Args.Count == 1 &&
                (id as NAryExpr).Fun.FunctionName.Equals("-"))
            {
                return(id);
            }

            NAryExpr root = PointerArithmeticAnalyser.GetPointerArithmeticExpr(impl, id) as NAryExpr;

            if (root == null)
            {
                return(id);
            }

            Expr result     = root;
            Expr resolution = result;
            int  ixs        = 0;

            var alreadyVisited = new HashSet <Tuple <string, Expr> >();

            do
            {
                if (result is NAryExpr)
                {
                    if (((result as NAryExpr).Args[0] is IdentifierExpr) &&
                        ((result as NAryExpr).Args[0] as IdentifierExpr).Name.StartsWith("$M."))
                    {
                        return(id);
                    }

                    if (PointerArithmeticAnalyser.TryPerformCast(ref result))
                    {
                        continue;
                    }

                    if (PointerArithmeticAnalyser.ShouldSkipFromAnalysis(result as NAryExpr))
                    {
                        return(id);
                    }

                    if (alreadyVisited.Any(v => v.Item1.Equals(label) && v.Item2.Equals(result)))
                    {
                        return(id);
                    }

                    alreadyVisited.Add(new Tuple <string, Expr>(label, result));

                    if (PointerArithmeticAnalyser.IsArithmeticExpression(result as NAryExpr))
                    {
                        return(id);
                    }

                    Expr p = (result as NAryExpr).Args[0];
                    Expr i = (result as NAryExpr).Args[1];
                    Expr s = (result as NAryExpr).Args[2];

                    if ((i is LiteralExpr) && (s is LiteralExpr))
                    {
                        ixs += (i as LiteralExpr).asBigNum.ToInt * (s as LiteralExpr).asBigNum.ToInt;
                    }
                    else
                    {
                        return(id);
                    }

                    result = p;
                }
                else
                {
                    resolution = PointerArithmeticAnalyser.GetPointerArithmeticExpr(impl, result);
                    if (resolution != null)
                    {
                        result = resolution;
                    }
                }
            }while (resolution != null);

            return(Expr.Add(result, new LiteralExpr(Token.NoToken, BigNum.FromInt(ixs))));
        }