Esempio n. 1
0
        public static VarSet GetAllVars(Program p)
        {
            //List<GlobalVariable> globalDecls = BoogieUtil.GetGlobalVariables(p);
            var procedures      = BoogieUtil.GetProcedures(p);
            var implementations = BoogieUtil.GetImplementations(p);
            var allVars         = new VarSet();

            foreach (var proc in procedures)
            {
                var uses = new GlobalVarsUsed();
                uses.VisitRequiresSeq(proc.Requires);
                uses.VisitEnsuresSeq(proc.Ensures);
                // skip visiting modifies clause
                allVars.Add(new VarSet(uses.globalsUsed, proc.Name));
            }

            foreach (var impl in implementations)
            {
                var uses = new GlobalVarsUsed();
                uses.VisitBlockList(impl.Blocks);
                allVars.Add(new VarSet(uses.globalsUsed, impl.Name));
            }

            //globalDecls.Iterate(x => procedures.Iterate(y => allVars.Add(x.Name, y.Name)));
            //globalDecls.Iterate(x => implementations.Iterate(y => allVars.Add(x.Name, y.Name)));
            return(allVars);
        }
Esempio n. 2
0
        public override CBAProgram runCBAPass(CBAProgram p)
        {
            if (unrollNum >= 0)
            {
                return(base.runCBAPass(p));
            }

            foreach (var impl in BoogieUtil.GetImplementations(p))
            {
                impl.PruneUnreachableBlocks();
            }

            // save RB
            var rb = CommandLineOptions.Clo.RecursionBound;

            if (BoogieVerify.irreducibleLoopUnroll >= 0)
            {
                CommandLineOptions.Clo.RecursionBound = BoogieVerify.irreducibleLoopUnroll;
            }

            var procsWithIrreducibleLoops = new HashSet <string>();
            var passInfo = p.ExtractLoops(out procsWithIrreducibleLoops);

            // restore RB
            CommandLineOptions.Clo.RecursionBound = rb;

            // no loops found, then this transformation is identity
            if (passInfo.Count == 0 && procsWithIrreducibleLoops.Count == 0)
            {
                return(null);
            }

            if (addUniqueCallLabels)
            {
                // annotate calls with a unique number
                var addIds = new AddUniqueCallIds();

                // Loop unrolling is done for procs with irreducible loops.
                // This simply copies Cmd objects. Duplicate them to remove
                // aliasing
                foreach (var impl in p.TopLevelDeclarations
                         .OfType <Implementation>()
                         .Where(impl => procsWithIrreducibleLoops.Contains(impl.Name)))
                {
                    var dup = new FixedDuplicator(true);
                    foreach (var blk in impl.Blocks)
                    {
                        blk.Cmds = dup.VisitCmdSeq(blk.Cmds);
                    }
                }

                // Add labels again to all procedures
                foreach (var impl in p.TopLevelDeclarations
                         .OfType <Implementation>())
                {
                    addIds.VisitImplementation(impl);
                }
            }

            info = new Dictionary <string, Dictionary <string, string> >();
            passInfo.Iter(kvp =>
            {
                info.Add(kvp.Key, new Dictionary <string, string>());
                kvp.Value.Iter(sb => info[kvp.Key].Add(sb.Key, sb.Value.Label));
            });

            // Construct the set of procs in the original program
            // and the loop procedures
            allProcs  = new HashSet <string>();
            loopProcs = new HashSet <string>();
            foreach (var decl in p.TopLevelDeclarations)
            {
                var impl = decl as Implementation;
                if (impl == null)
                {
                    continue;
                }
                allProcs.Add(impl.Name);
                if (impl.Proc is LoopProcedure)
                {
                    loopProcs.Add(impl.Name);
                    impl.Proc.AddAttribute("LoopProcedure");
                }
            }

            //foreach (var impl in BoogieUtil.GetImplementations(p))
            //{
            //removeAssumeFalseBlocks(impl);
            //}


            // Optimization: if no loop is found, then no need to print
            // out a new program
            if (loopProcs.Count == 0)
            {
                return(null);
            }

            // Remove vars from attributes that are not in scope anymore
            RemoveVarsFromAttributes.Prune(p);

            return(p);
        }