Esempio n. 1
0
        ////////////////////////////////////////////////////////////////////////////
        public Statement propagate(EqualityAnalyzer prestate, StatementId si)
        {
//            Console.WriteLine("EqualityAnalyzer.propagate({0})", si.ToString());
            statementId = si;

            Debug.Assert(!ReferenceEquals(prestate, this));
            database = prestate.database; //alias

            Debug.Assert(database != null);
            Debug.Assert(procedure != null);

            database.statementId = si; //for reporting

            firstOccurenceVariables = new HashSet <string>();

            Statement result;

            if (pathCondition.isFalse || database.additionalConditions.isFalse)
            {
                result = null;
            }
            else
            {
                result = si.statement.visit(this);
            }
//            Console.WriteLine("/EqualityAnalyzer.propagate({0})", si.ToString());
            return(result);
        }
Esempio n. 2
0
        public void CreateStatement_ExpectValid()
        {
            //Arrange
            string      localStatementIdString = "STMT01";
            StatementId localStatementId       = new StatementId(localStatementIdString);

            SpecificFieldsFactory localfactory = new SpecificFieldsFactory();

            string[]      listspecificfields = { "Credit Card", "12" };
            StatementType localStatementType = new StatementType(localfactory, "CreditCardProvider", listspecificfields);

            StatementSpecificFields localspecificfields = localStatementType.getSpecificFields();


            int      localstatementAccountnumber     = 1234567;
            string   localstatementAccountholdername = "Bruce";
            DateTime localstatementDate = DateTime.Now;
            StatementCommonFields localStatementCommonFields = new StatementCommonFields(localstatementAccountnumber, localstatementAccountholdername, localstatementDate);

            APSUser        localAPSUser        = new APSUser(new APSUserId("1"), "testusername", "testpassword");
            BillingAccount localBillingAccount = new BillingAccount(new BillingAccountId("1"), new BillingCompanyId("1"), "testusername", "testpassword", localAPSUser);

            //Act
            Statement localStatement = new Statement(localStatementId, localStatementCommonFields, localStatementType, localspecificfields, localAPSUser, localBillingAccount);

            //Assert
            Assert.AreEqual(localStatement.StatementId, localStatementId);
            Assert.AreEqual(localStatement.StatementCommonFields, localStatementCommonFields);
            Assert.AreEqual(localStatement.StatementType, localStatementType);
            Assert.AreEqual(localStatement.StatementSpecificFields, localspecificfields);
            Assert.AreEqual(localStatement.APSUser, localAPSUser);
            Assert.AreEqual(localStatement.BillingAccount, localBillingAccount);
        }
        ////////////////////////////////////////////////////////////////////////////////////////////////
        private static IEnumerable <StatementId> getRelevant(CFG <BasicBlock, BasicEdge> cfg, StatementId target)
        {
            var result = new Dictionary <string, StatementId>();

            var toMark = new Queue <StatementId>();

            foreach (var p in target.predecessors)
            {
                toMark.Enqueue(p.Item2);
            }
            while (toMark.Count > 0)
            {
                StatementId si = toMark.Dequeue();
                if (!result.ContainsKey(si.ToString()))
                {
                    if (si.statement is PredicateStatement)
                    {
                        result.Add(si.ToString(), si);
                    }
                    foreach (var p in si.predecessors)
                    {
                        toMark.Enqueue(p.Item2);
                    }
                }
            }
            return(result.Values);
        }
        public Statement propagate(SubexpressionCounter prestate, StatementId si)
        {
            currentStatement = si;

            if (si.successors.Count == 1 && si.successors.First().Item2.predecessors.Count == 1)
            {
                subexpressionCounts = prestate.subexpressionCounts;
            }
            else
            {
                subexpressionCounts = new Dictionary <string, SubExpression>();

                foreach (var sec in prestate.subexpressionCounts)
                {
                    subexpressionCounts.Add(sec.Key, new SubExpression(sec.Value));
                    //                Debug.Assert(sec.Key == subexpressionCounts[sec.Key].fae.ToString());
                }
            }
            context = new Stack <string>();
            context.Push("");

            Statement ns = si.statement.visit(this);

            Debug.Assert(ns != null);
            return(ns);
        }
        public void join(ICollection <Tuple <BasicEdge.Guard, SubexpressionCounter> > predecessors, StatementId si)
        {
            subexpressionCounts = new Dictionary <string, SubExpression>();
            currentStatement    = si;

//            HashSet<string> ses = new HashSet<string>();
            if (predecessors.Count == 1 && predecessors.First().Item2.currentStatement.predecessors.Count == 1)
            {
                subexpressionCounts = predecessors.First().Item2.subexpressionCounts;
            }
            else
            {
                foreach (var p in predecessors)
                {
                    foreach (var v in p.Item2.subexpressionCounts)
                    {
                        SubExpression se = null;
                        if (!subexpressionCounts.TryGetValue(v.Key, out se))
                        {
                            subexpressionCounts.Add(v.Key, new SubExpression(v.Value));
                            //                        Debug.Assert(v.Key == subexpressionCounts[v.Key].fae.ToString());
                        }
                        else
                        {
                            se.merge(currentStatement, v.Value);
                            //                        Debug.Assert(v.Key == subexpressionCounts[v.Key].fae.ToString());
                        }
                    }
                }
            }
//           currentStatement = null;
        }
        public void CreateStatement_ExpectValid()
        {
            //Arrange
            string localStatementIdString = "STMT01";
            StatementId localStatementId = new StatementId(localStatementIdString);

            SpecificFieldsFactory localfactory = new SpecificFieldsFactory();
            string[] listspecificfields = { "Credit Card", "12" };
            StatementType localStatementType = new StatementType(localfactory, "CreditCardProvider", listspecificfields);

            StatementSpecificFields localspecificfields = localStatementType.getSpecificFields();

            int localstatementAccountnumber = 1234567;
            string localstatementAccountholdername = "Bruce";
            DateTime localstatementDate = DateTime.Now;
            StatementCommonFields localStatementCommonFields = new StatementCommonFields(localstatementAccountnumber, localstatementAccountholdername, localstatementDate);

            APSUser localAPSUser = new APSUser(new APSUserId("1"), "testusername", "testpassword");
            BillingAccount localBillingAccount = new BillingAccount(new BillingAccountId("1"), new BillingCompanyId("1"), "testusername", "testpassword", localAPSUser);

            //Act
            Statement localStatement = new Statement(localStatementId, localStatementCommonFields, localStatementType, localspecificfields, localAPSUser, localBillingAccount);

            //Assert
            Assert.AreEqual(localStatement.StatementId, localStatementId);
            Assert.AreEqual(localStatement.StatementCommonFields, localStatementCommonFields);
            Assert.AreEqual(localStatement.StatementType, localStatementType);
            Assert.AreEqual(localStatement.StatementSpecificFields, localspecificfields);
            Assert.AreEqual(localStatement.APSUser, localAPSUser);
            Assert.AreEqual(localStatement.BillingAccount, localBillingAccount);
        }
Esempio n. 7
0
 public override int GetHashCode()
 {
     unchecked
     {
         int hashCode = (StatementId != null ? StatementId.GetHashCode() : 0);
         hashCode = (hashCode * 397) ^ (Name != null ? Name.GetHashCode() : 0);
         hashCode = (hashCode * 397) ^ (Variable != null ? Variable.GetHashCode() : 0);
         return(hashCode);
     }
 }
Esempio n. 8
0
        public void CreateStatementId_ExpectValid()
        {
            //Arrange
            string localStatementIdString = "STMT01";

            //Act
            StatementId localStatementId = new StatementId(localStatementIdString);

            //Assert
            Assert.AreEqual(localStatementId.StatementIdString, localStatementIdString);
        }
        public void CreateStatementId_ExpectValid()
        {
            //Arrange
            string localStatementIdString = "STMT01";

            //Act
            StatementId localStatementId = new StatementId(localStatementIdString);

            //Assert
            Assert.AreEqual(localStatementId.StatementIdString, localStatementIdString);
        }
Esempio n. 10
0
        public Statement propagate(InitializedVariables prestate, StatementId si)
        {
//            Console.WriteLine("propagate({0} : {1})", si.ToString(), si.statement.ToString());
            firstOccurence =
                new Dictionary <string, Dictionary <string, Tuple <PropositionalFormula, StatementId> > >(
                    prestate.firstOccurence);
            currentStatement = si;
            si.statement.visit(this);
            currentStatement = null;
            return(si.statement);
        }
        public Statement propagate(DeadCodeEliminator prestate, StatementId si)
        {
            liveVariables = new HashSet <string>();
            foreach (var v in prestate.liveVariables)
            {
                liveVariables.Add(v);
            }

            Statement ns = si.statement.visit(this);

            return(ns);
        }
Esempio n. 12
0
        public void collect(StatementId si)
        {
//            currentStatement = si;
            if (si.statement is PredicateStatement)
            {
                (si.statement as PredicateStatement).expression.visit(this);
            }
            else
            {
                Debug.Assert(si.statement is ControlStatement);
            }
        }
 public SubExpression(SubExpression other)
 {
     Debug.Assert(other != null);
     expression = other.expression;
     contexts   = new Dictionary <string, HashSet <StatementId> >();
     foreach (var c in other.contexts)
     {
         contexts.Add(c.Key, new HashSet <StatementId>(c.Value));
     }
     definition = other.definition;
     Debug.Assert(definition != null);
 }
Esempio n. 14
0
        ////////////////////////////////////////////////////////////////////////////
        public void join(ICollection <Tuple <BasicEdge.Guard, EqualityAnalyzer> > predecessors, StatementId si)
        {
/*            Console.WriteLine("EqualityAnalyzer.join({0})", si.ToString());
 *          foreach (var p in predecessors)
 *              Console.WriteLine("\t{0} : {1}", p.Item2.statementId.ToString(), p.Item2.pathCondition.ToString());
 */
            statementId = si;

            Debug.Assert(predecessors.Count > 0);
            Procedure procedure = predecessors.First().Item2.procedure;

            Debug.Assert(procedure != null);

            if (predecessors.Count == 1 && si.predecessors.First().Item2.successors.Count == 1)
            {
                Debug.Assert(((predecessors.First().Item1.expression as LiteralExpression).value as BooleanValue).value);
                EqualityAnalyzer onlyPred = predecessors.First().Item2;
                database             = onlyPred.database;
                database.statementId = si;
            }
            else
            {
                var predConditions = new List <PropositionalFormula>();
                foreach (var p in predecessors)
                {
                    if (!p.Item2.pathCondition.isFalse)
                    {
                        PropositionalFormula c = p.Item2.database.toDNF(p.Item1);
                        PropositionalFormula a = p.Item2.pathCondition & c;
                        predConditions.Add(a);
                    }
                }
//                Console.WriteLine("\tEqualityAnalyzer.join({0}).pathCondition", si.ToString());
                PropositionalFormula pathCondition = PropositionalFormula.or(predConditions);

                var preds = new List <EqualityDatabase>();
                foreach (var p in predecessors)
                {
                    preds.Add(p.Item2.database);
                }

//                Console.WriteLine("\tEqualityAnalyzer.join({0}).database", si.ToString());
                database = new EqualityDatabase(si, procedure, pathCondition, preds);

                if (pathCondition.isFalse)
                {
                    Console.Error.WriteLine("[{0}] Warning - path condition ({1}) is false", si.ToString(),
                                            pathCondition.ToString(conditionVariableNames));
                }
            }
//            Console.WriteLine("/EqualityAnalyzer.join({0})", si.ToString());
        }
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        private void removeTypeQuantification(StatementId si)
        {
            if (!(si.statement is PredicateStatement))
            {
                return;
            }

            var ps = si.statement as PredicateStatement;

            currentSID    = si;
            ps.expression = removeTypeQuantification(ps.expression);
            currentSID    = null;
        }
            internal void add(StatementId currentStatement, string context)
            {
                Debug.Assert(currentStatement != null);
                HashSet <StatementId> c = null;

                if (!contexts.TryGetValue(context, out c))
                {
                    c = new HashSet <StatementId>();
                    contexts.Add(context, c);
                }
                c.Add(currentStatement);
                definition = currentStatement;
                Debug.Assert(definition != null);
            }
Esempio n. 17
0
        private void correctControlStatement(StatementId si, ControlStatement newS)
        {
            IEnumerable <Tuple <BasicEdge.Guard, StatementId> > os = getSuccessors(si);
            IEnumerable <Tuple <BasicEdge.Guard, StatementId> > ns = getSuccessors(si, newS);
            var dif = new HashSet <BasicBlock>(from s in os select s.Item2.basicBlock);
            var nss = new HashSet <BasicBlock>(from s in ns select s.Item2.basicBlock);

            Debug.Assert(nss.IsSubsetOf(dif));
            dif.ExceptWith(nss);
            if (dif.Count == 0)
            {
                return;
            }
            var q          = new Queue <BasicBlock>(dif);
            var numReaches = new Dictionary <BasicBlock, int>();

            foreach (var d in dif)
            {
                numReaches.Add(d, 1);
            }
            while (q.Count > 0)
            {
                BasicBlock bb = q.Dequeue();
                Debug.Assert(bb.statements.First().preState.evaluatorInformation.numIncompletePredecessors > 0);
                bb.statements.First().preState.evaluatorInformation.numIncompletePredecessors--;
                if (bb.statements.First().preState.evaluatorInformation.numIncompletePredecessors == 0)
                {
                    if (bb.predecessors.Count - numReaches[bb] == 0) //will be deleted
                    {
                        foreach (var nbbe in bb.successors)
                        {
                            BasicBlock nbb = nbbe.target;
                            q.Enqueue(nbb);
                            if (!numReaches.ContainsKey(nbb))
                            {
                                numReaches.Add(nbb, 1);
                            }
                            else
                            {
                                numReaches[nbb]++;
                            }
                        }
                    }
                    else
                    {
                        ready.Enqueue(bb.statements[0].statementId);
                    }
                }
            }
        }
Esempio n. 18
0
        /////////////////////////////////////////////////////////
        private void propagateAll(StatementId start)
        {
            Debug.Assert(start != null);

            ready = new Queue <StatementId>();

            ready.Enqueue(start);

            while (ready.Count > 0)
            {
                StatementId s = ready.Dequeue();
                propagate(s);
            }
        }
        public override int GetHashCode()
        {
            int hashcode = 157;

            unchecked {
                hashcode = (hashcode * 397) + SessionId.GetHashCode();
                hashcode = (hashcode * 397) + Statement.GetHashCode();
                hashcode = (hashcode * 397) + StatementId.GetHashCode();
                if (__isset.fetchSize)
                {
                    hashcode = (hashcode * 397) + FetchSize.GetHashCode();
                }
            }
            return(hashcode);
        }
            public SubExpression(Expression e, StatementId id, string context)
            {
                Debug.Assert(e != null);
                Debug.Assert(id != null);
                Debug.Assert(context != null);

                expression = e;
                contexts   = new Dictionary <string, HashSet <StatementId> >();
                var idSet = new HashSet <StatementId>();

                idSet.Add(id);
                contexts.Add(context, idSet);
                definition = id;
                Debug.Assert(definition != null);
            }
Esempio n. 21
0
                public Assignment(StatementId statementId)
                {
                    this.statementId = statementId;
                    Debug.Assert(statementId.statement is Assume);
                    Debug.Assert((statementId.statement as Assume).expression is FAE);
                    Debug.Assert(
                        BFunction.isEquality(
                            ((statementId.statement as Assume).expression as FAE).function));
                    Debug.Assert(
                        (((statementId.statement as Assume).expression as FAE).arguments[0] is
                         ProgramVariableExpression));

                    variable =
                        (((statementId.statement as Assume).expression as FAE).arguments[0] as
                         ProgramVariableExpression).programVariable;
                    value      = ((statementId.statement as Assume).expression as FAE).arguments[1];
                    basicBlock = statementId.basicBlock;
                }
Esempio n. 22
0
        ////////////////////////////////////////////////////////////////////////////
        public void initializeStart(StatementId si)
        {
//            Console.WriteLine("EqualityAnalyzer.initializeStart({0})", si.ToString());
            Debug.Assert(procedure != null);
            database = new EqualityDatabase(si, procedure, new PropositionalFormula(true));

            int numConstants = 0;
//            var constants = (from c in procedure.parentScope.locals where c is Constant select c as Constant).ToArray();
            var constantMap = new Dictionary <string, List <BasicProgramVariableExpression> >();

//            Console.Write("\tCollecting unique constants : ");
            foreach (var d in procedure.parentScope.locals)
            {
                var c = d as Constant;
                if (c != null && c.isUnique)
                {
                    numConstants++;
                    List <BasicProgramVariableExpression> cme;
                    if (!constantMap.TryGetValue(c.type.ToStringN(), out cme))
                    {
                        constantMap[c.type.ToStringN()] = cme = new List <BasicProgramVariableExpression>();
                    }
                    cme.Add(new BasicProgramVariableExpression(c));
                }
            }
//            Console.WriteLine("{0}", numConstants);
            int cc = 0;

            foreach (var l in constantMap)
            {
//                Console.WriteLine("\t\tAdding inequalities for {0} : {1}", l.Key.ToString(), l.Value.Count);
                for (int i = 0; i < l.Value.Count; i++)
                {
                    for (int j = i + 1; j < l.Value.Count; j++)
                    {
//                        Console.WriteLine("\t\t\t{0}: {1} != {2}", cc, l.Value[i].ToString(), l.Value[j].ToString());
                        database.addInequality(l.Value[i], l.Value[j]);
                        cc++;
                    }
                }
            }
            statementId = si;
//            Console.WriteLine("/EqualityAnalyzer.initializeStart({0})", si.ToString());
        }
        ////////////////////////////////////////////////////////////////////////////////////////////////
        private static void addAssumptions(BasicBlock block, PropositionalFormula pathCondition,
                                           PropositionalFormula notPathCondition, StatementId si)
        {
            Debug.Assert((pathCondition & notPathCondition).isFalse);
            Debug.Assert((pathCondition | notPathCondition).isTrue);
            PropositionalFormula statementPathCondition = si.basicBlock.getPreState(0).equalityAnalyzer.pathCondition;

            if ((statementPathCondition & pathCondition).isFalse)
            {
//                Console.WriteLine("Eliminating {0} & {1}", statementPathCondition.ToString(), pathCondition.ToString());
                return;
            }
            if (!(statementPathCondition.isTrue))
            {
                PropositionalFormula simplifiedPathCondition = statementPathCondition | notPathCondition;

                if (simplifiedPathCondition.size() < statementPathCondition.size())
                {
                    statementPathCondition = simplifiedPathCondition;
                }
            }
            var q = new Queue <Expression>();

            q.Enqueue(((PredicateStatement)si.statement).expression);
            while (q.Count > 0)
            {
                Expression e   = q.Dequeue();
                var        fae = e as FAE;
                if (fae != null && BFunction.isConjunction(fae.function))
                {
                    q.Enqueue(fae.arguments[0]);
                    q.Enqueue(fae.arguments[1]);
                }
                else
                {
                    block.appendStatement(new Assume(makeFlatExpression(si.basicBlock, statementPathCondition, e)));
                }
            }
        }
Esempio n. 24
0
        public void propagate(DeadCodeEliminator prestate, StatementId s)
        {
            this.liveVariables = new HashSet <string>();
            foreach (var v in prestate.liveVariables)
            {
                liveVariables.Add(v);
            }

            procedure = prestate.procedure;
            if (!s.deleted)
            {
                var ns = s.statement.visit(this);
                if (ns == null)
                {
                    s.delete();
                }
                else
                {
                    s.statement = ns;
                }
            }
        }
Esempio n. 25
0
//        private HashSet<string> visited;

/*        /////////////////////////////////////////////////////
 *      private void spill(BasicBlock si, BasicEdge e, int l)
 *      {
 *          for (int i = 0; i < l; i++)
 *              Console.Write("\t");
 *          Console.Write(si.label);
 *          if (e!=null && e.isBackEdge())
 *              Console.Write(" - b");
 *          Console.WriteLine();
 *
 *          visited.Add(si.label);
 *          foreach (var p in si.predecessors)
 *              if (!visited.Contains(p.source.label))
 *                  spill(p.source, p, l + 1);
 *      }
 */
        /////////////////////////////////////////////////////
        public void propagate(MapCollector prestate, StatementId s)
        {
/*            if (prestate.procedure == null)
 *          {
 *              visited = new HashSet<string>();
 *              spill(s.basicBlock, null, 0);
 *          }
 *          else
 *          {
 *              Console.WriteLine("propagate: {0}", s.ToString());
 *              foreach (var p in s.predecessors)
 *                  Console.WriteLine("\t{0}", p.Item2.ToString());
 *          }
 */         Debug.Assert(!ReferenceEquals(this, prestate));
            Debug.Assert(!ReferenceEquals(this.maps, prestate.maps));
            Debug.Assert(prestate.procedure != null);

            this.procedure = prestate.procedure;
            maps.procedure = procedure;

            maps.merge(prestate.maps);
            s.statement.visit(this);
        }
            internal void merge(StatementId currentStatement, SubExpression other)
            {
                Debug.Assert(currentStatement != null);
                Debug.Assert(expression.ToString() == other.expression.ToString());
                foreach (var c in other.contexts)
                {
                    HashSet <StatementId> context = null;
                    if (!contexts.TryGetValue(c.Key, out context))
                    {
                        context = new HashSet <StatementId>();
                        contexts.Add(c.Key, context);
                    }

                    int oldSize = context.Count;

                    context.UnionWith(c.Value);
                    if (oldSize != context.Count || c.Value.Count != context.Count ||
                        !ReferenceEquals(other.definition, definition))
                    {
                        definition = currentStatement;
                    }
                }
                Debug.Assert(definition != null);
            }
Esempio n. 27
0
            private Assignment getAssignment(ProgramVariable v, StatementId statementId)
            {
                StatementId si     = statementId;
                var         assume = statementId.statement as Assume;

                if (assume == null)
                {
                    si = null;
                }
                else
                {
                    var fae = assume.expression as FAE;

                    if (fae == null || !BFunction.isEquality(fae.function))
                    {
                        si = null;
                    }
                    else
                    {
                        var dest = fae.arguments[0] as ProgramVariableExpression;
                        if (dest == null || (dest.programVariable != v))
                        {
                            si = null;
                        }
                    }
                }

                if (si == null)
                {
                    return(new Assignment(statementId.basicBlock, v));
                }
                else
                {
                    return(new Assignment(si));
                }
            }
 public async Task<Statement> GetSpecificStatement(StatementId StatementId)
 {
     //Gets a specific statement
     //code logic return null for now
     return null;
 } 
Esempio n. 29
0
 /////////////////////////////////////////////////////////
 protected override IEnumerable <Tuple <BasicEdge.Guard, StatementId> > getSuccessors(StatementId si,
                                                                                      ControlStatement cs)
 {
     return(si.predecessors);
 }
 public void initializeStart(StatementId si)
 {
     subexpressionCounts = new Dictionary <string, SubExpression>();
 }
Esempio n. 31
0
 public bool isEqual(StatementId other)
 {
     return(ReferenceEquals((other as StatementIdImp).statementInfo, statementInfo));
 }
 public void join(ICollection <Tuple <BasicEdge.Guard, DeadCodeEliminator> > predecessors, StatementId si)
 {
     liveVariables = new HashSet <string>();
     foreach (var p in predecessors)
     {
         foreach (var v in p.Item2.liveVariables)
         {
             liveVariables.Add(v);
         }
     }
 }
 public void initializeStart(StatementId si)
 {
     liveVariables = new HashSet <string>();
 }