Ejemplo n.º 1
0
        public static bool Match(IExpr /*!*/ expr, IFunctionSymbol /*!*/ f, params Matcher[] /*!*/ subs)
        {
            Contract.Requires(subs != null);
            Contract.Requires(f != null);
            Contract.Requires(expr != null);
            IFunApp app = MatchFunctionSymbol(expr, f);

            if (app != null)
            {
                int i = 0; // Note ***0***
                foreach (Matcher /*!*/ s in subs)
                {
                    Contract.Assert(s != null);
                    if (!s(cce.NonNull((IExpr)app.Arguments[i])))
                    {
                        return(false);
                    }
                    i++;
                }
                return(true);
            }
            else
            {
                return(false);
            }
        }
Ejemplo n.º 2
0
 public override bool Understands(IFunctionSymbol /*!*/ f, IList /*!*/ args)
 {
     //Contract.Requires(args != null);
     //Contract.Requires(f != null);
     understandsCount++;
     return(lattice.Understands(f, args));
 }
Ejemplo n.º 3
0
        // Ternary Binding
        public static bool Match(IExpr /*!*/ expr, IFunctionSymbol /*!*/ f, out IExpr arg0, out IExpr arg1, out IExpr arg2, params Matcher[] /*!*/ subs)
        {
            Contract.Requires(subs != null);
            Contract.Requires(f != null);
            Contract.Requires(expr != null);
            arg0 = null;
            arg1 = null;
            arg2 = null;

            IFunApp app = MatchFunctionSymbol(expr, f);

            if (app != null)
            {
                arg0 = (IExpr /*!*/)cce.NonNull(app.Arguments[0]);
                arg1 = (IExpr /*!*/)cce.NonNull(app.Arguments[1]);
                arg2 = (IExpr /*!*/)cce.NonNull(app.Arguments[2]);

                int i = 3; // Note ***3***
                foreach (Matcher /*!*/ s in subs)
                {
                    Contract.Assert(s != null);
                    if (!s(cce.NonNull((IExpr /*!*/)app.Arguments[i])))
                    {
                        return(false);
                    }
                    i++;
                }
                return(true);
            }
            else
            {
                return(false);
            }
        }
Ejemplo n.º 4
0
 public override bool Understands(IFunctionSymbol /*!*/ f, IList /*!*/ args)
 {
     //Contract.Requires(args != null);
     //Contract.Requires(f != null);
     return(f.Equals(Prop.And) ||
            f.Equals(Value.Eq) ||
            microLattice.Understands(f, args));
 }
Ejemplo n.º 5
0
        public override bool Understands(IFunctionSymbol /*!*/ f, IList /*!*/ args)
        {
            //Contract.Requires(args != null);
            //Contract.Requires(f != null);
            bool result = false;

            for (int i = 0; i < this.lattices.Count; i++)
            {
                result = (result || SubLattice(i).Understands(f, args));
            }

            return(result);
        }
Ejemplo n.º 6
0
 public override bool Understands(IFunctionSymbol /*!*/ f, IList /*<IExpr!>*//*!*/ args)
 {
     //Contract.Requires(args != null);
     //Contract.Requires(f != null);
     return(f is IntSymbol ||
            f.Equals(Int.Add) ||
            f.Equals(Int.Sub) ||
            f.Equals(Int.Negate) ||
            f.Equals(Int.Mul) ||
            f.Equals(Int.Eq) ||
            f.Equals(Int.Neq) ||
            f.Equals(Prop.Not) ||
            f.Equals(Int.AtMost) ||
            f.Equals(Int.Less) ||
            f.Equals(Int.Greater) ||
            f.Equals(Int.AtLeast));
 }
Ejemplo n.º 7
0
        private static IFunApp /*?*/ MatchFunctionSymbol(IExpr /*!*/ expr, IFunctionSymbol /*!*/ f)
        {
            Contract.Requires(f != null);
            Contract.Requires(expr != null);
            IFunApp app = expr as IFunApp;

            if (app != null)
            {
                if (app.FunctionSymbol.Equals(f))
                {
                    return(app);
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 8
0
        public override bool Understands(IFunctionSymbol /*!*/ f, IList /*<IExpr!>*//*!*/ args)
        {
            //Contract.Requires(args != null);
            //Contract.Requires(f != null);
            bool isEq = f.Equals(Microsoft.AbstractInterpretationFramework.Value.Eq);

            if (isEq || f.Equals(Microsoft.AbstractInterpretationFramework.Value.Subtype))
            {
                Contract.Assert(args.Count == 2);
                IExpr /*!*/ arg0 = (IExpr /*!*/)cce.NonNull(args[0]);
                IExpr /*!*/ arg1 = (IExpr /*!*/)cce.NonNull(args[1]);

                // Look for $typeof(var) == t or t == $typeof(var) or $typeof(var) <: t
                if (isEq && factory.IsTypeConstant(arg0))
                {
                    // swap the arguments
                    IExpr /*!*/ tmp = arg0;
                    arg0 = arg1;
                    arg1 = tmp;
                }
                else if (!factory.IsTypeConstant(arg1))
                {
                    return(false);
                }
                IFunApp typeofExpr = arg0 as IFunApp;
                if (typeofExpr != null &&
                    typeofExpr.FunctionSymbol.Equals(Microsoft.AbstractInterpretationFramework.Value.Typeof))
                {
                    Contract.Assert(typeofExpr.Arguments.Count == 1);
                    if (typeofExpr.Arguments[0] is IVariable)
                    {
                        // we have a match
                        return(true);
                    }
                }
            }
            return(false);
        }
Ejemplo n.º 9
0
        public override bool Understands(IFunctionSymbol /*!*/ f, IList /*<IExpr!>*//*!*/ args)
        {
            //Contract.Requires(args != null);
            //Contract.Requires(f != null);
            if (f.Equals(Microsoft.AbstractInterpretationFramework.Value.Eq) ||
                f.Equals(Microsoft.AbstractInterpretationFramework.Value.Neq))
            {
                Contract.Assert(args.Count == 2);
                IExpr /*!*/ arg0 = (IExpr /*!*/)cce.NonNull(args[0]);
                IExpr /*!*/ arg1 = (IExpr /*!*/)cce.NonNull(args[1]);

                // Look for "x OP null" or "null OP x" where OP is "==" or "!=".
                if (arg0 is IVariable && arg1 is IFunApp && ((IFunApp)arg1).FunctionSymbol == Ref.Null)
                {
                    return(true);
                }
                else if (arg1 is IVariable && arg0 is IFunApp && ((IFunApp)arg0).FunctionSymbol == Ref.Null)
                {
                    return(true);
                }
            }
            return(false);
        }
Ejemplo n.º 10
0
        public static bool Match(IExpr/*!*/ expr, IFunctionSymbol/*!*/ f, params Matcher[]/*!*/ subs){
Contract.Requires(subs != null);
Contract.Requires(f != null);
Contract.Requires(expr != null);
            IFunApp app = MatchFunctionSymbol(expr,f);
            if (app != null)
            {
                int i = 0; // Note ***0***
                foreach(Matcher/*!*/ s in subs){
Contract.Assert(s != null);
                    if (!s(cce.NonNull((IExpr)app.Arguments[i]))) { return false; }
                    i++;
                }
                return true;
            }
            else { return false; }
        }
Ejemplo n.º 11
0
        private static IFunApp/*?*/ MatchFunctionSymbol(IExpr/*!*/ expr, IFunctionSymbol/*!*/ f){
Contract.Requires(f != null);
Contract.Requires(expr != null);
            IFunApp app = expr as IFunApp;
            if (app != null)
            {
                if (app.FunctionSymbol.Equals(f))
                    return app;
                else
                    return null;
            }
            else
                return null;
        }
Ejemplo n.º 12
0
 /// <summary>
 /// Is this a boolean field that monotonically goes from false to true?
 /// </summary>
 public static bool IsBooleanMonotonicallyWeakening(IFunctionSymbol/*!*/ f) {
   Contract.Requires(f != null);
   return f.Equals(Allocated);
 }
Ejemplo n.º 13
0
 public override bool Understands(IFunctionSymbol/*!*/ f, IList/*!*/ args) {
   //Contract.Requires(args != null);
   //Contract.Requires(f != null);
   understandsCount++;
   return lattice.Understands(f, args);
 }
Ejemplo n.º 14
0
 public override bool Understands(IFunctionSymbol f, IList args) {
   Contract.Requires(f != null);
   Contract.Requires(args != null);
   throw new NotImplementedException();
 }
Ejemplo n.º 15
0
    public override bool Understands(IFunctionSymbol/*!*/ f, IList/*<IExpr!>*//*!*/ args) {
      //Contract.Requires(args != null);
      //Contract.Requires(f != null);
      if (f.Equals(Microsoft.AbstractInterpretationFramework.Value.Eq) ||
          f.Equals(Microsoft.AbstractInterpretationFramework.Value.Neq)) {

        Contract.Assert(args.Count == 2);
        IExpr/*!*/ arg0 = (IExpr/*!*/)cce.NonNull(args[0]);
        IExpr/*!*/ arg1 = (IExpr/*!*/)cce.NonNull(args[1]);

        // Look for "x OP null" or "null OP x" where OP is "==" or "!=".
        if (arg0 is IVariable && arg1 is IFunApp && ((IFunApp)arg1).FunctionSymbol == Ref.Null) {
          return true;
        } else if (arg1 is IVariable && arg0 is IFunApp && ((IFunApp)arg0).FunctionSymbol == Ref.Null) {
          return true;
        }
      }
      return false;
    }
Ejemplo n.º 16
0
 /// <summary>
 /// For the moment consider just equalities. Other case must be considered
 /// </summary>
 public override bool Understands(IFunctionSymbol/*!*/ f, IList /*<IExpr*//*!*/ args) {
   //Contract.Requires(args != null);
   //Contract.Requires(f != null);
   return f.Equals(Microsoft.AbstractInterpretationFramework.Value.Eq);
 }
Ejemplo n.º 17
0
 /// <summary>
 /// For the moment consider just equalities. Other case must be considered
 /// </summary>
 public override bool Understands(IFunctionSymbol /*!*/ f, IList /*<IExpr*//*!*/ args)
 {
     //Contract.Requires(args != null);
     //Contract.Requires(f != null);
     return(f.Equals(Microsoft.AbstractInterpretationFramework.Value.Eq));
 }
Ejemplo n.º 18
0
        // Ternary Binding
        public static bool Match(IExpr/*!*/ expr, IFunctionSymbol/*!*/ f, out IExpr arg0, out IExpr arg1, out IExpr arg2, params Matcher[]/*!*/ subs){
Contract.Requires(subs != null);
Contract.Requires(f != null);
Contract.Requires(expr != null);
            arg0 = null;
            arg1 = null;
            arg2 = null;
        
            IFunApp app = MatchFunctionSymbol(expr,f);
            if (app != null)
            {
                arg0 = (IExpr/*!*/)cce.NonNull(app.Arguments[0]);
                arg1 = (IExpr/*!*/)cce.NonNull(app.Arguments[1]);
                arg2 = (IExpr/*!*/)cce.NonNull(app.Arguments[2]);
                
                int i = 3; // Note ***3***
                foreach(Matcher/*!*/ s in subs){
Contract.Assert(s != null);
                    if (!s(cce.NonNull((IExpr/*!*/)app.Arguments[i]))) { return false; }
                    i++;
                }
                return true;
            }
            else { return false; }
        }
Ejemplo n.º 19
0
    private static IList/*<IExpr!>*//*!*/ BreakJuncts(IExpr/*!*/ e, IFunctionSymbol/*!*/ sym) {
      Contract.Requires(sym != null);
      Contract.Requires(e != null);
      Contract.Ensures(Contract.Result<IList>() != null);
      Contract.Ensures(Contract.ForAll(0, Contract.Result<IList>().Count, i => {
        var sub = Contract.Result<IList>()[i];
        return (sub is IFunApp) || !((IFunApp)sub).FunctionSymbol.Equals(sym);
      }));
      ArrayList/*<IExpr!>*//*!*/ result = new ArrayList();

      IFunApp f = e as IFunApp;
      if (f != null) {
        // If it is a sym, go down into sub-expressions.
        if (f.FunctionSymbol.Equals(sym)) {
          foreach (IExpr/*!*/ arg in f.Arguments) {
            Contract.Assert(arg != null);
            result.AddRange(BreakJuncts(arg, sym));
          }
        }
          // Otherwise, stop.
        else {
          result.Add(e);
        }
      } else {
        result.Add(e);
      }

      return result;
    }
Ejemplo n.º 20
0
    public override bool Understands(IFunctionSymbol/*!*/ f, IList/*!*/ args) {
      //Contract.Requires(args != null);
      //Contract.Requires(f != null);
      bool result = false;

      for (int i = 0; i < this.lattices.Count; i++) {
        result = (result || SubLattice(i).Understands(f, args));
      }

      return result;
    }
Ejemplo n.º 21
0
 public override bool Understands(IFunctionSymbol f, IList args)
 {
     Contract.Requires(f != null);
     Contract.Requires(args != null);
     throw new NotImplementedException();
 }
Ejemplo n.º 22
0
 /// <summary>
 ///  Allows the lattice to specify whether it understands a particular function symbol.
 ///
 ///  The lattice is always allowed to return "true" even when it really can't do anything
 ///  with such functions; however, it is advantageous to say "false" when possible to
 ///  avoid being called to do certain things.
 ///
 ///  The arguments to a function are provided for context so that the lattice can say
 ///  true or false for the same function symbol in different situations.  For example,
 ///  a lattice may understand the multiplication of a variable and a constant but not
 ///  of two variables.  The implementation of a lattice should not hold on to the
 ///  arguments.
 /// </summary>
 /// <param name="f">The function symbol.</param>
 /// <param name="args">The argument context.</param>
 /// <returns>True if it may understand f, false if it does not understand f.</returns>
 public abstract bool Understands(IFunctionSymbol /*!*/ f, IList /*<IExpr!>*//*!*/ args);
Ejemplo n.º 23
0
 public override bool Understands(IFunctionSymbol/*!*/ f, IList/*!*/ args) {
   //Contract.Requires(args != null);
   //Contract.Requires(f != null);
   return f.Equals(Prop.And) ||
          f.Equals(Value.Eq) ||
          microLattice.Understands(f, args);
 }
Ejemplo n.º 24
0
        static /*maybe null*/ LinearCondition GetCond(IExpr e, bool positive) /* throws ArithmeticException */
        {
            IFunApp funapp = e as IFunApp;

            if (funapp == null)
            {
                return(null);
            }
            IFunctionSymbol /*!*/ s = funapp.FunctionSymbol;

            Contract.Assert(s != null);
            if ((positive && s.Equals(Prop.False)) ||
                (!positive && s.Equals(Prop.True)))
            {
                return(new LCBottom());
            }
            else if (s.Equals(Prop.Not))
            {
                Contract.Assert(funapp.Arguments.Count == 1);
                return(GetCond((IExpr /*!*/)cce.NonNull(funapp.Arguments[0]), !positive));
            }
            else if (funapp.Arguments.Count == 2)
            {
                IExpr /*!*/ arg0 = (IExpr /*!*/)cce.NonNull(funapp.Arguments[0]);
                IExpr /*!*/ arg1 = (IExpr /*!*/)cce.NonNull(funapp.Arguments[1]);
                LinearExpr  le0  = AsExpr(arg0);
                if (le0 == null)
                {
                    return(null);
                }
                LinearExpr le1 = AsExpr(arg1);
                if (le1 == null)
                {
                    return(null);
                }

                LinearConstraint constraint = null;
                bool             sense      = true;
                if ((positive && s.Equals(Int.Less)) || (!positive && s.Equals(Int.AtLeast)))
                {
                    constraint = MakeConstraint(le0, le1, LinearConstraint.ConstraintRelation.LE, BigNum.ONE);
                }
                else if ((positive && s.Equals(Int.AtMost)) || (!positive && s.Equals(Int.Greater)))
                {
                    constraint = MakeConstraint(le0, le1, LinearConstraint.ConstraintRelation.LE, BigNum.ZERO);
                }
                else if ((positive && s.Equals(Int.AtLeast)) || (!positive && s.Equals(Int.Less)))
                {
                    constraint = MakeConstraint(le1, le0, LinearConstraint.ConstraintRelation.LE, BigNum.ZERO);
                }
                else if ((positive && s.Equals(Int.Greater)) || (!positive && s.Equals(Int.AtMost)))
                {
                    constraint = MakeConstraint(le1, le0, LinearConstraint.ConstraintRelation.LE, BigNum.ONE);
                }
                else if (s.Equals(Int.Eq))
                {
                    constraint = MakeConstraint(le0, le1, LinearConstraint.ConstraintRelation.EQ, BigNum.ZERO);
                    sense      = positive;
                }
                else if (s.Equals(Int.Neq))
                {
                    constraint = MakeConstraint(le0, le1, LinearConstraint.ConstraintRelation.EQ, BigNum.ZERO);
                    sense      = !positive;
                }
                if (constraint != null)
                {
                    if (constraint.coefficients.Count != 0)
                    {
                        return(new LinearConditionLiteral(sense, constraint));
                    }
                    else if (constraint.IsConstantSatisfiable())
                    {
                        return(null);
                    }
                    else
                    {
                        return(new LCBottom());
                    }
                }
            }
            return(null);
        }
Ejemplo n.º 25
0
        /// <summary>
        /// Builds a linear expression from "e", if possible; returns null if not possible.
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static /*maybe null*/ LinearExpr AsExpr(IExpr /*!*/ e) /* throws ArithmeticException */
        {
            Contract.Requires(e != null);
            if (e is IVariable)
            {
                // Note, without a type for the variable, we don't know if the identifier is intended to hold an integer value.
                // However, it seems that no harm can be caused by here treating the identifier as if it held an
                // integer value, because other parts of this method will reject the expression as a linear expression
                // if non-numeric operations other than equality are applied to the identifier.
                return(new LinearExpr((IVariable)e));
            }
            else if (e is IFunApp)
            {
                IFunApp /*!*/ funapp = (IFunApp)e;
                Contract.Assert(funapp != null);
                IFunctionSymbol /*!*/ s = funapp.FunctionSymbol;
                Contract.Assert(s != null);

                if (s is IntSymbol)
                {
                    return(new LinearExpr(((IntSymbol)s).Value));
                }
                else if (s.Equals(Int.Negate))
                {
                    Contract.Assert(funapp.Arguments.Count == 1);
                    LinearExpr le = AsExpr((IExpr /*!*/)cce.NonNull(funapp.Arguments[0]));
                    if (le != null)
                    {
                        le.Negate();
                        return(le);
                    }
                }
                else if (s.Equals(Int.Add) || s.Equals(Int.Sub) || s.Equals(Int.Mul))
                {
                    Contract.Assert(funapp.Arguments.Count == 2);
                    IExpr /*!*/ arg0 = (IExpr /*!*/)cce.NonNull(funapp.Arguments[0]);
                    IExpr /*!*/ arg1 = (IExpr /*!*/)cce.NonNull(funapp.Arguments[1]);
                    LinearExpr  le0  = AsExpr(arg0);
                    if (le0 == null)
                    {
                        return(null);
                    }
                    LinearExpr le1 = AsExpr(arg1);
                    if (le1 == null)
                    {
                        return(null);
                    }

                    if (s.Equals(Int.Add))
                    {
                        le0.Add(le1);
                        return(le0);
                    }
                    else if (s.Equals(Int.Sub))
                    {
                        le1.Negate();
                        le0.Add(le1);
                        return(le0);
                    }
                    else if (s.Equals(Int.Mul))
                    {
                        BigNum x;
                        if (le0.AsConstant(out x))
                        {
                            le1.Multiply(x);
                            return(le1);
                        }
                        else if (le1.AsConstant(out x))
                        {
                            le0.Multiply(x);
                            return(le0);
                        }
                    }
                }
            }
            return(null);
        }
Ejemplo n.º 26
0
 /// <summary>
 ///  Allows the lattice to specify whether it understands a particular function symbol.
 /// 
 ///  The lattice is always allowed to return "true" even when it really can't do anything
 ///  with such functions; however, it is advantageous to say "false" when possible to
 ///  avoid being called to do certain things.
 /// 
 ///  The arguments to a function are provided for context so that the lattice can say
 ///  true or false for the same function symbol in different situations.  For example,
 ///  a lattice may understand the multiplication of a variable and a constant but not
 ///  of two variables.  The implementation of a lattice should not hold on to the
 ///  arguments.
 /// </summary>
 /// <param name="f">The function symbol.</param>
 /// <param name="args">The argument context.</param>
 /// <returns>True if it may understand f, false if it does not understand f.</returns>
 public abstract bool Understands(IFunctionSymbol/*!*/ f, IList/*<IExpr!>*//*!*/ args);
Ejemplo n.º 27
0
    public override bool Understands(IFunctionSymbol/*!*/ f, IList/*<IExpr!>*//*!*/ args) {
      //Contract.Requires(args != null);
      //Contract.Requires(f != null);
      bool isEq = f.Equals(Microsoft.AbstractInterpretationFramework.Value.Eq);
      if (isEq || f.Equals(Microsoft.AbstractInterpretationFramework.Value.Subtype)) {
        Contract.Assert(args.Count == 2);
        IExpr/*!*/ arg0 = (IExpr/*!*/)cce.NonNull(args[0]);
        IExpr/*!*/ arg1 = (IExpr/*!*/)cce.NonNull(args[1]);

        // Look for $typeof(var) == t or t == $typeof(var) or $typeof(var) <: t
        if (isEq && factory.IsTypeConstant(arg0)) {
          // swap the arguments
          IExpr/*!*/ tmp = arg0;
          arg0 = arg1;
          arg1 = tmp;
        } else if (!factory.IsTypeConstant(arg1)) {
          return false;
        }
        IFunApp typeofExpr = arg0 as IFunApp;
        if (typeofExpr != null &&
            typeofExpr.FunctionSymbol.Equals(Microsoft.AbstractInterpretationFramework.Value.Typeof)) {
          Contract.Assert(typeofExpr.Arguments.Count == 1);
          if (typeofExpr.Arguments[0] is IVariable) {
            // we have a match
            return true;
          }
        }
      }
      return false;
    }
Ejemplo n.º 28
0
 public override bool Understands(IFunctionSymbol/*!*/ f, IList/*<IExpr!>*//*!*/ args) {
   //Contract.Requires(args != null);
   //Contract.Requires(f != null);
   return f is IntSymbol ||
       f.Equals(Int.Add) ||
       f.Equals(Int.Sub) ||
       f.Equals(Int.Negate) ||
       f.Equals(Int.Mul) ||
       f.Equals(Int.Eq) ||
       f.Equals(Int.Neq) ||
       f.Equals(Prop.Not) ||
       f.Equals(Int.AtMost) ||
       f.Equals(Int.Less) ||
       f.Equals(Int.Greater) ||
       f.Equals(Int.AtLeast);
 }