Example #1
0
File: Demos.cs Project: koyre/b8
        public void naturalCycleTest()
        {
            var root   = Parser.ParseString(Samples.SampleProgramText.sample1);
            var code   = ProgramTreeToLinear.Build(root);
            var blocks = LinearToBaseBlock.Build(code);
            var cfg    = ListBlocksToCFG.Build(blocks);

            cfg.ShowCompact = true;
            Console.WriteLine(cfg);
            var ncg = new NaturalCycleGraph(cfg);
            var res = ncg.findBetween(6, 4);

            res.Sort();
            var expected = new List <int>()
            {
                4, 6
            };

            CollectionAssert.AreEqual(res, expected);

            var res1 = ncg.findBetween(13, 8);

            res1.Sort();
            Console.Write("Cycle btw 13, 8: " + string.Join(", ", res1));
            var expected1 = new List <int>()
            {
                8, 10, 11, 12, 13
            };

            CollectionAssert.AreEqual(res1, expected1);
        }
Example #2
0
        public void RegionSequenceTest()
        {
            var root   = Parser.ParseString(Samples.SampleProgramText.regionSqeuenceSample);
            var code   = ProgramTreeToLinear.Build(root);
            var blocks = LinearToBaseBlock.Build(code);
            var cfg    = ListBlocksToCFG.Build(blocks);

            var regSeq    = new RegionSequence(cfg);
            var natCycles = cfg.getNaturalCyclesForBackwardEdges();

            Assert.AreEqual(regSeq.Regions.Count, 24);
            Assert.AreEqual(regSeq.Regions.Last().Header, cfg.GetVertices().ToList()[0]);
            Assert.IsTrue(regSeq.Regions.Exists(r => natCycles[0][0] == r.Header));
        }
Example #3
0
        public void DominatorTreeTest()
        {
            var root   = Parser.ParseString(Samples.SampleProgramText.domSampleTrivial);
            var code   = ProgramTreeToLinear.Build(root);
            var blocks = LYtest.BaseBlocks.LinearToBaseBlock.Build(code);
            var cfg    = ListBlocksToCFG.Build(blocks);

            var dt   = new LYtest.DominatorTree.DominatorTree(cfg);
            var node = dt.GetRoot();

            Console.WriteLine(dt.ToString());
            Console.WriteLine(cfg.ToString());
            Assert.AreEqual(dt.NumberOfVertices(), 4);
        }
Example #4
0
        public void CFGNodeSetTest()
        {
            var root   = Parser.ParseString(Samples.SampleProgramText.sample1);
            var code   = ProgramTreeToLinear.Build(root);
            var blocks = LYtest.BaseBlocks.LinearToBaseBlock.Build(code);
            var cfg    = ListBlocksToCFG.Build(blocks);

            var nodes = cfg.graph.Vertices.Select(n => n.Value);

            Assert.AreEqual(nodes.Count(), blocks.Count());

            foreach (var b in blocks)
            {
                Assert.IsTrue(nodes.Contains(b));
            }
        }
Example #5
0
File: Demos.cs Project: koyre/b8
        public void DominatorTreeDemo()
        {
            var root   = Parser.ParseString(Samples.SampleProgramText.sample2);
            var code   = ProgramTreeToLinear.Build(root);
            var blocks = LYtest.BaseBlocks.LinearToBaseBlock.Build(code);
            var cfg    = ListBlocksToCFG.Build(blocks);

            var dt   = new LYtest.DominatorTree.DominatorTree(cfg);
            var node = dt.GetRoot();

            cfg.ShowCompact = true;
            Console.WriteLine(cfg);

            Console.WriteLine("\nDominator tree:");
            Console.WriteLine(dt.ToString());
        }
Example #6
0
        public void AvailableExpressionsTest()
        {
            Console.Write("----------- Available Expressions Analyzer ---------- \n");
            var root   = Parser.ParseString(Samples.SampleProgramText.AvailableExprsSample);
            var code   = ProgramTreeToLinear.Build(root);
            var blocks = LinearToBaseBlock.Build(code);
            var cfg    = ListBlocksToCFG.Build(blocks);


            var exprsAnalizer = new AvailableExprAnalyzer(cfg);

            exprsAnalizer.analyze();

            Assert.IsTrue(exprsAnalizer.OutBlocks[blocks[0]].Count == 1);
            Assert.IsTrue(exprsAnalizer.InBlocks[blocks[2]].Count == 1);
            Assert.IsTrue(exprsAnalizer.OutBlocks[blocks[2]].Count == 0);
        }
Example #7
0
        public void CFGTest()
        {
            var tree   = Parser.ParseString(Samples.SampleProgramText.sample1);
            var code   = ProgramTreeToLinear.Build(tree);
            var blocks = LYtest.BaseBlocks.LinearToBaseBlock.Build(code);

            var cfg = ListBlocksToCFG.Build(blocks);

            Assert.AreEqual(cfg.GetRoot().ParentsNodes.Count, 0);

            Assert.AreEqual(cfg.Blocks.Count, 15);

            Assert.IsNotNull(cfg.GetRoot().directChild);
            Assert.IsNotNull(cfg.GetRoot().gotoNode);

            cfg.Blocks.ForEach(Console.WriteLine);
        }
Example #8
0
File: Demos.cs Project: koyre/b8
        public void ReachingDefsIterTest()
        {
            var root   = Parser.ParseString(Samples.SampleProgramText.sample2);
            var code   = ProgramTreeToLinear.Build(root);
            var blocks = LYtest.BaseBlocks.LinearToBaseBlock.Build(code);
            var cfg    = ListBlocksToCFG.Build(blocks);

            var defs = new ReachingDefsIterAlg(cfg);

            cfg.InfoFunc = node =>
            {
                var outs = string.Join(", ", defs.Out[node]);
                var ins  = node.ParentsNodes.Count > 0 ? string.Join(", ", defs.In[node]) : "";
                return($" In: {ins} \n\t\t\tOut: {outs}");
            };
            Console.WriteLine(cfg);
        }
Example #9
0
        private void MakeDF()
        {
            var cfg   = ListBlocksToCFG.Build(blocks);
            var start = cfg.GetRoot();

            Queue <CFGNode> queue = new Queue <CFGNode>();

            queue.Enqueue(start);
            var visited = new HashSet <CFGNode>();

            while (queue.Count > 0)
            {
                var cur_node = queue.Dequeue();
                if (visited.Contains(cur_node))
                {
                    continue;
                }
                DF[cur_node.Value].Add(cur_node.Value);
                visited.Add(cur_node);

                var childs = new List <CFGNode>(new CFGNode[] { cur_node.directChild, cur_node.gotoNode });
                foreach (var cur_child in childs)
                {
                    if (cur_child == null)
                    {
                        continue;
                    }
                    queue.Enqueue(cur_child);

                    if (cur_child.directChild != null)
                    {
                        DF[cur_node.Value].Add(cur_child.directChild.Value);
                    }

                    if (cur_child.gotoNode != null)
                    {
                        DF[cur_node.Value].Add(cur_child.gotoNode.Value);
                    }
                }
            }
        }
Example #10
0
        public void ActiveVarsIterTest()
        {
            var root   = Parser.ParseString(Samples.SampleProgramText.sample3);
            var code   = ProgramTreeToLinear.Build(root);
            var blocks = LYtest.BaseBlocks.LinearToBaseBlock.Build(code);
            var cfg    = ListBlocksToCFG.Build(blocks);

            var DefUse = new ActiveVarsIterAlg(cfg);

            foreach (var block in cfg.graph.Vertices)
            {
                Console.Write(block);
                foreach (var labelValue in DefUse.In[block])
                {
                    Console.Write(labelValue);
                    Console.Write(", ");
                }
                Console.WriteLine();
                Console.WriteLine("-----------------------");
            }
        }
Example #11
0
        public void CFGTest()
        {
            var root   = Parser.ParseString(Samples.SampleProgramText.sample1);
            var code   = ProgramTreeToLinear.Build(root);
            var blocks = LYtest.BaseBlocks.LinearToBaseBlock.Build(code);
            var cfg    = ListBlocksToCFG.Build(blocks);

            Assert.AreEqual(cfg.GetRoot().ParentsNodes.Count, 0);

            var cfgview = new CFGLookup(cfg.GetRoot());

            while (cfgview.MoveDirect())
            {
            }
            Assert.AreEqual(cfgview.PathLen, 14);

            while (cfgview.MoveBack())
            {
            }
            Assert.AreEqual(cfgview.Current, cfg.GetRoot());
        }
Example #12
0
File: Demos.cs Project: koyre/b8
        public void allRetreatingEdgesAreBackwardsTest()
        {
            var root   = Parser.ParseString(Samples.SampleProgramText.sample1);
            var code   = ProgramTreeToLinear.Build(root);
            var blocks = LinearToBaseBlock.Build(code);
            var cfg    = ListBlocksToCFG.Build(blocks);

            cfg.ShowCompact = true;
            Console.WriteLine(cfg);
            Console.WriteLine("allRetreatingEdgesAreBackwards: " + cfg.allRetreatingEdgesAreBackwards());
            Assert.AreEqual(cfg.allRetreatingEdgesAreBackwards(), true);

            root            = Parser.ParseString(Samples.SampleProgramText.veryStrangeCode);
            code            = ProgramTreeToLinear.Build(root);
            blocks          = LinearToBaseBlock.Build(code);
            cfg             = ListBlocksToCFG.Build(blocks);
            cfg.ShowCompact = true;
            Console.WriteLine(cfg);
            Console.WriteLine("allRetreatingEdgesAreBackwards: " + cfg.allRetreatingEdgesAreBackwards());
            Assert.AreEqual(cfg.allRetreatingEdgesAreBackwards(), false);
        }
Example #13
0
File: Demos.cs Project: koyre/b8
        public void AvailableExpressionsTest()
        {
            Console.Write("----------- Available Expressions Analyzer ---------- \n");
            var root   = Parser.ParseString(Samples.SampleProgramText.AvailableExprsSample);
            var code   = ProgramTreeToLinear.Build(root);
            var blocks = LYtest.BaseBlocks.LinearToBaseBlock.Build(code);
            var cfg    = ListBlocksToCFG.Build(blocks);


            var exprsAnalizer = new AvailableExprAnalyzer(cfg);

            exprsAnalizer.analyze();


            foreach (var block in blocks)
            {
                Console.Write("Block: " + block);

                Console.Write("\n");

                Console.Write("IN: \t");
                foreach (var expr in exprsAnalizer.InBlocks[block])
                {
                    Console.Write(expr);
                    Console.Write(", ");
                }
                Console.Write("\n");

                Console.Write("Out: \t");
                foreach (var expr in exprsAnalizer.OutBlocks[block])
                {
                    Console.Write(expr);
                    Console.Write(", ");
                }
                Console.Write("\n");

                Console.WriteLine("-----------------------");
            }
        }