Beispiel #1
0
        public void TransferFunctionStorage()
        {
            string     text    = @"
    i = 1;
    j = 4;
    a = 2;
    while i < 20
    {  
        i = i + 1;
        j = j + 1;
        if i > a
            a = a + 5;
        i = i + 1;
    }";
            SyntaxNode root    = ParserWrap.Parse(text);
            var        graph   = new Graph(BasicBlocksGenerator.CreateBasicBlocks(ThreeAddressCodeGenerator.CreateAndVisit(root).Program));
            var        regions = new RegionSequence().CreateSequence(graph);
            var        storage = new AbstractTransferFunctionStorage <int>();

            for (var i = 0; i < regions.Count - 1; i++)
            {
                storage[regions[i], RegionDirection.Out, regions[i + 1]] = 2 * i;
                storage[regions[i], RegionDirection.In, regions[i + 1]]  = 2 * i + 1;
            }

            for (var i = 0; i < regions.Count - 1; i++)
            {
                Assert.IsTrue(storage[regions[i], RegionDirection.Out, regions[i + 1]] == 2 * i);
                Assert.IsTrue(storage[regions[i], RegionDirection.In, regions[i + 1]] == 2 * i + 1);
            }
        }
Beispiel #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));
        }
Beispiel #3
0
        public void CreateSequenceTest()
        {
            string programText1 = @"
                                i = 1; 
                                j = 4; 
                                a = 2; 
                                while i < 20 
                                { 
                                i = i + 1; 
                                j = j + 1; 
                                if i > a 
                                a = a + 5; 
                                while j < 5
                                {
                                a = 4;
                                }
                                i = i + 1; 
                                }";

            SyntaxNode     root             = ParserWrap.Parse(programText1);
            var            threeAddressCode = ThreeAddressCodeGenerator.CreateAndVisit(root).Program;
            Graph          g   = new Graph(BasicBlocksGenerator.CreateBasicBlocks(threeAddressCode));
            RegionSequence seq = new RegionSequence();

            List <Region> seqList = seq.CreateSequence(g);

            Assert.IsTrue(seqList.Count == 14);

            string programText2 = @"
                                i = 1; 
                                j = 4; 
                                a = 2; 
                                while i < 20 
                                { 
                                j = j + 1;  
                                i = i + 1; 
                                }";

            SyntaxNode     root2             = ParserWrap.Parse(programText2);
            var            threeAddressCode2 = ThreeAddressCodeGenerator.CreateAndVisit(root2).Program;
            Graph          g2   = new Graph(BasicBlocksGenerator.CreateBasicBlocks(threeAddressCode2));
            RegionSequence seq2 = new RegionSequence();

            List <Region> seqList2 = seq2.CreateSequence(g2);

            Assert.IsTrue(seqList2.Count == 7);
        }
Beispiel #4
0
        public static IterativeAlgorithmOutput <ISet <V> > Apply <V>(Graph graph, SetIterativeAlgorithmParameters <V> param)
        {
            List <Region> regions = new RegionSequence().CreateSequence(graph);

            return(ApplyDescendingPart <V>(regions, ApplyAscendingPart <V>(graph, regions, param), param, graph));
        }
Beispiel #5
0
        static void Main(string[] args)
        {
            var text = File.ReadAllText("a.txt");
            //text = "n = 0;" +
            //        "if n {" +
            //        "custom_label1:" +
            //        "goto custom_label2;" +
            //        "}" +
            //        "else {" +
            //        "custom_label2:" +
            //        "goto custom_label1;" +
            //        "}";
            var root = Parser.ParseString(text);

            if (root == null)
            {
                Console.WriteLine("Error");
                return;
            }

            // Генерация и получение трёхзначного кода
            var linearCode = new LinearCodeVisitor();

            root.AcceptVisit(linearCode);
            var code = linearCode.code;


            // Get blocks and print it
            var blocks = LinearToBaseBlock.Build(code);

            foreach (var block in blocks)
            {
                Console.WriteLine(block.ToString());
            }

            // Get graph and made DepthSpanningTree
            var cfg = new CFGraph(blocks);

            Console.WriteLine(cfg.ToString());


            var exprsAnalizer = new AvailableExprAnalyzer(cfg);

            exprsAnalizer.analyze();

            var    dst     = new DepthSpanningTree(cfg);
            string dst_viz = dst.ToString();

            Console.WriteLine(dst_viz);

            Console.WriteLine("");

            Console.WriteLine(cfg.EdgeTypes.ToString());


            var f  = cfg.allRetreatingEdgesAreBackwards();
            var r  = cfg.getNaturalCyclesForBackwardEdges();
            var rs = new RegionSequence(cfg);

            Console.ReadLine();
        }