예제 #1
0
파일: BlockAtomic.cs 프로젝트: ggrov/tacny
        /// <summary>
        /// Extract the loop guard from the statement
        /// </summary>
        /// <param name="st"></param>
        /// <returns></returns>
        protected Expression ExtractGuard(Statement st)
        {
            Contract.Requires(st != null);

            IfStmt     ifStmt;
            WhileStmt  whileStmt;
            Expression guardWrapper;

            // extract the guard statement
            if ((ifStmt = st as IfStmt) != null)
            {
                guardWrapper = ifStmt.Guard;
            }
            else if ((whileStmt = st as WhileStmt) != null)
            {
                guardWrapper = whileStmt.Guard;
            }
            else
            {
                return(null);
            }
            Guard = ExpressionTree.ExpressionToTree(guardWrapper);

            return(guardWrapper);
        }
예제 #2
0
        private IEnumerable <Solution> ReplaceOperator(Statement st)
        {
            IVariable                lv;
            List <Expression>        callArguments;
            IEnumerable <Expression> enumerator = null;

            InitArgs(st, out lv, out callArguments);
            Contract.Assert(lv != null, Error.MkErr(st, 8));
            Contract.Assert(tcce.OfSize(callArguments, 2), Error.MkErr(st, 0, 3, callArguments.Count));
            var mapList = new List <MapDisplayExpr>();

            foreach (var arg2 in ResolveExpression(callArguments[1]))
            {
                var operatorMap = arg2 as MapDisplayExpr;
                Contract.Assert(operatorMap != null, Error.MkErr(st, 1, "Map"));
                mapList.Add(operatorMap);
            }

            foreach (var arg1 in ResolveExpression(callArguments[0]))
            {
                var expression = arg1 as Expression;
                Contract.Assert(expression != null, Error.MkErr(st, 1, "Expression"));
                var et = ExpressionTree.ExpressionToTree(expression);

                foreach (var operatorMap in mapList)
                {
                    switch (GetMapType(operatorMap))
                    {
                    case MapType.Undefined:
                        Contract.Assert(false, Error.MkErr(st, 1, "Operator or variable map"));
                        break;

                    case MapType.Op:
                        enumerator = SubstOp(et, operatorMap);
                        break;

                    case MapType.Var:
                        enumerator = SubstVar(et, operatorMap);
                        break;

                    default:
                        Contract.Assert(false, Error.MkErr(st, 1, "Operator or variable map"));
                        break;
                    }

                    foreach (var result in enumerator)
                    {
                        yield return(AddNewLocal(lv, result));
                    }
                }
            }
        }
예제 #3
0
        protected static ExpressionTree ExtractGuard(Statement stmt)
        {
            Contract.Requires <ArgumentNullException>(stmt != null);
            // extract the guard statement
            var ifStmt = stmt as dfy.IfStmt;

            if (ifStmt != null)
            {
                return(ExpressionTree.ExpressionToTree(ifStmt.Guard));
            }
            var whileStmt = stmt as WhileStmt;

            return(whileStmt != null?ExpressionTree.ExpressionToTree(whileStmt.Guard) : null);
        }
예제 #4
0
        private IEnumerable <Expression> ReplaceConstants(ExpressionTree expression, List <Expression> constants, List <IVariable> vars)
        {
            if (expression == null)
            {
                yield break;
            }
            if (expression.Root == null)
            {
                expression.SetRoot();
            }

            if (expression.IsLeaf())
            {
                if (HasConstant(expression.Data, constants))
                {
                    foreach (var var in vars)
                    {
                        if (!ValidateType(var, expression.Parent.TreeToExpression() as BinaryExpr))
                        {
                            continue;
                        }
                        var newVal  = ExpressionTree.ExpressionToTree(VariableToExpression(var));
                        var copy    = expression.Root.Copy();
                        var newTree = ExpressionTree.FindAndReplaceNode(copy, newVal, expression.Copy());
                        yield return(newTree.Root.TreeToExpression());

                        foreach (var item in ReplaceConstants(newTree.Root, constants, vars))
                        {
                            yield return(item);
                        }
                    }
                }
            }
            else
            {
                foreach (var item in ReplaceConstants(expression.LChild, constants, vars))
                {
                    yield return(item);
                }
                foreach (var item in ReplaceConstants(expression.RChild, constants, vars))
                {
                    yield return(item);
                }
            }
        }
예제 #5
0
        private IEnumerable <Solution> ReplaceConstant(Statement st)
        {
            IVariable         lv = null;
            List <Expression> callArgs;

            InitArgs(st, out lv, out callArgs);
            Contract.Assert(lv != null, Error.MkErr(st, 8));
            Contract.Assert(callArgs.Count == 3, Error.MkErr(st, 0, 3, callArgs.Count));
            var varLists   = new List <List <IVariable> >();
            var constLists = new List <List <Expression> >();

            foreach (var arg2 in ResolveExpression(callArgs[1]))
            {
                var tmp = arg2 as List <Expression>;
                Contract.Assert(tmp != null, Error.MkErr(st, 1, "Term Seq"));
                constLists.Add(tmp);
            }
            foreach (var arg2 in ResolveExpression(callArgs[2]))
            {
                var tmp = arg2 as List <IVariable>;
                Contract.Assert(tmp != null, Error.MkErr(st, 1, "Term Seq"));
                varLists.Add(tmp);
            }

            foreach (var arg1 in ResolveExpression(callArgs[0]))
            {
                var expression = arg1 as Expression;
                Contract.Assert(expression != null, Error.MkErr(st, 1, "Term"));
                foreach (var varList in varLists)
                {
                    foreach (var constList in constLists)
                    {
                        foreach (var item in ReplaceConstants(ExpressionTree.ExpressionToTree(expression), constList, varList))
                        {
                            yield return(AddNewLocal(lv, item));
                        }
                    }
                }
            }
        }
예제 #6
0
        private Solution Constants(Statement st)
        {
            IVariable         lv = null;
            List <Expression> callArgs;
            var result = new List <Expression>();

            InitArgs(st, out lv, out callArgs);
            Contract.Assert(lv != null, Error.MkErr(st, 8));
            Contract.Assert(callArgs.Count == 1, Error.MkErr(st, 0, 1, callArgs.Count));

            foreach (var arg1 in ResolveExpression(callArgs[0]))
            {
                var expression = arg1 as Expression;
                Contract.Assert(expression != null, Error.MkErr(st, 1, "Term"));
                var expt  = ExpressionTree.ExpressionToTree(expression);
                var leafs = expt.GetLeafData();
                foreach (var leaf in leafs)
                {
                    if (leaf is LiteralExpr)
                    {
                        if (!result.Exists(j => (j as LiteralExpr)?.Value == (leaf as LiteralExpr)?.Value))
                        {
                            result.Add(leaf);
                        }
                    }
                    else if (leaf is ExprDotName)
                    {
                        var edn = leaf as ExprDotName;
                        if (!result.Exists(j => SingletonEquality(j, edn)))
                        {
                            result.Add(leaf);
                        }
                    }
                }
            }

            return(AddNewLocal(lv, result));
        }
예제 #7
0
        private Solution LazyScanMemberBody(MemberDecl md)
        {
            Contract.Requires(md != null);
            Console.WriteLine($"Starting thread: {System.Threading.Thread.CurrentThread.Name}");
            Debug.WriteLine($"Scanning member {md.Name} body");
            var function = md as Function;

            if (function != null)
            {
                var fun = function;
                if (fun.Body == null)
                {
                    return(null);
                }
                var expt = ExpressionTree.ExpressionToTree(fun.Body);
                expt.FindAndResolveTacticApplication(_tacnyProgram, fun);

                /* No reason ot generate new solution
                 * if nothing has been changed
                 */
                if (!expt.Modified)
                {
                    return(null);
                }
                var res    = expt.TreeToExpression();
                var newFun = new Function(fun.tok, fun.Name, fun.HasStaticKeyword, fun.IsProtected, fun.IsGhost, fun.TypeArgs, fun.Formals, fun.ResultType, fun.Req, fun.Reads, fun.Ens, fun.Decreases, res, fun.Attributes, fun.SignatureEllipsis);
                var ac     = new Atomic {
                    IsFunction     = true,
                    DynamicContext = { newTarget = newFun }
                };
                return(new Solution(ac));
            }
            var m = md as Method;

            if (m?.Body == null)
            {
                return(null);
            }

            List <IVariable> variables = new List <IVariable>();
            List <IVariable> resolved;

            lock (_tacnyProgram) {
                resolved = _tacnyProgram.GetResolvedVariables(md);
            }
            variables.AddRange(m.Ins);
            variables.AddRange(m.Outs);
            resolved.AddRange(m.Ins); // add input arguments as resolved variables
            resolved.AddRange(m.Outs);
            // does not work for multiple tactic applications
            foreach (var st in m.Body.Body)
            {
                UpdateStmt us = null;
                WhileStmt  ws = null;
                // register local variables
                if (st is VarDeclStmt)
                {
                    VarDeclStmt vds = st as VarDeclStmt;
                    variables.AddRange(vds.Locals);
                }
                else if (st is UpdateStmt)
                {
                    us = st as UpdateStmt;
                }
                else if (st is WhileStmt)
                {
                    ws = st as WhileStmt;
                    us = ws.TacAps as UpdateStmt;
                    foreach (var wst in ws.Body.Body)
                    {
                        if (!(wst is VarDeclStmt))
                        {
                            continue;
                        }
                        var vds = wst as VarDeclStmt;
                        variables.AddRange(vds.Locals);
                    }
                }
                if (us == null || !_tacnyProgram.IsTacticCall(us))
                {
                    continue;
                }
                Debug.WriteLine("Tactic call found");
                return(ResolveTactic(variables, resolved, us, md, ws));
            }
            return(null);
        }