Пример #1
0
        public void MergingBlocksTest1()
        {
            var TAC = GenerateTAC(
                @"
{
int x;
x = 14;
y = 2 * (a + b) - c;
x = x + x;
}
");
            var blocks = new TACBaseBlocks(TAC.Instructions);

            blocks.GenBaseBlocks();
            Assert.AreEqual(blocks.blocks.Count, 1);

            var expected = new List <string>()
            {
                "x = 14",
                "#t0 = a + b",
                "#t1 = 2 * #t0",
                "#t2 = #t1 - c",
                "y = #t2",
                "x = x + x"
            };
            var actual = blocks.BlockMerging().Select(instructions => instructions.ToString().Trim());

            Assert.AreEqual(expected, actual);
        }
        private static TACBaseBlocks IterAlgoOptimizations(TACBaseBlocks blocks)
        {
            var           prevInstructions     = blocks.BlockMerging();
            int           AllOptimizationCount = 0;
            TACBaseBlocks prevBlocks;

            do
            {
                IterAlgoOptimizers[AllOptimizationCount].Instructions = prevInstructions.Copy();
                BasicBlock.clearIndexCounter();
                prevBlocks = new TACBaseBlocks(IterAlgoOptimizers[AllOptimizationCount].Instructions);
                prevBlocks.GenBaseBlocks();
                IterAlgoOptimizers[AllOptimizationCount].Blocks = prevBlocks.blocks;
                var cfg = new ControlFlowGraph(prevBlocks.blocks);
                IterAlgoOptimizers[AllOptimizationCount].Cfg = cfg;
                IterAlgoOptimizers[AllOptimizationCount].Run();

                if (prevInstructions.SequenceEqual(IterAlgoOptimizers[AllOptimizationCount].Instructions))
                {
                    AllOptimizationCount++;
                }
                else
                {
                    prevInstructions        = IterAlgoOptimizers[AllOptimizationCount].Instructions;
                    AllOptimizationCount    = 0;
                    GlobalOptimizationCount = 0;
                }
            } while (AllOptimizationCount < IterAlgoOptimizers.Count);
            ++GlobalOptimizationCount;
            return(prevBlocks);
        }
        public static TACBaseBlocks Optimize(Parser parser, bool debugInfo = false)
        {
            var TACGenerator = new TACGenerationVisitor();

            parser.root.Visit(TACGenerator);
            var TACBlocks = new TACBaseBlocks(TACGenerator.Instructions);

            TACBlocks.GenBaseBlocks();
            TACBaseBlocks result = TACBlocks;
            var           i      = 1;

            do
            {
                var oneBlockOptimizations = OptimizeBlock(result);
                if (debugInfo)
                {
                    Console.WriteLine("===============TAC EachBlockOpt: Stage {0}===============", i.ToString());
                    PrintInstructions(oneBlockOptimizations);
                    ++i;
                }
                result = AllOptimization(oneBlockOptimizations);
                if (debugInfo)
                {
                    Console.WriteLine("===============TAC AllBlocksOpt: Stage {0}===============", i.ToString());
                    PrintInstructions(result.BlockMerging());
                    ++i;
                }
                result = IterAlgoOptimizations(result);
                if (debugInfo)
                {
                    Console.WriteLine("=================TAC IterAlgs: Stage {0}=================", i.ToString());
                    PrintInstructions(result.BlockMerging());
                    ++i;
                }
            } while (GlobalOptimizationCount < 3);
            return(result);
        }
Пример #4
0
        public void GlobalManyBlocksTest()
        {
            var TAC = GenerateTAC(
                @"
{
for i=1..10
{
    for j=1..10
    {
        a = 0;
    }
}
for i=1..10
{
    a = 1;
}
}
");
            var blocks = new TACBaseBlocks(TAC.Instructions);

            blocks.GenBaseBlocks();
            Assert.AreEqual(blocks.blocks.Count, 10);

            var expected = new List <List <string> >()
            {
                new List <string>()
                {
                    "i = 1"
                },
                new List <string>()
                {
                    "#L0\t#t0 = i > 10",
                    "if #t0 goto #L1"
                },
                new List <string>()
                {
                    "j = 1"
                },
                new List <string>()
                {
                    "#L2\t#t1 = j > 10",
                    "if #t1 goto #L3"
                },
                new List <string>()
                {
                    "a = 0",
                    "j = j + 1",
                    "goto #L2"
                },
                new List <string>()
                {
                    "#L3",
                    "i = i + 1",
                    "goto #L0"
                },
                new List <string>()
                {
                    "#L1",
                    "i = 1"
                },
                new List <string>()
                {
                    "#L4\t#t2 = i > 10",
                    "if #t2 goto #L5"
                },
                new List <string>()
                {
                    "a = 1",
                    "i = i + 1",
                    "goto #L4"
                },
                new List <string>()
                {
                    "#L5"
                },
            };
            var actual = blocks.blocks.Select(block => block.Instructions.Select(instr => instr.ToString().Trim())).ToList();

            Assert.AreEqual(expected, actual);

            var expectedMerg = new List <string>()
            {
                "i = 1",
                "#L0\t#t0 = i > 10",
                "if #t0 goto #L1",
                "j = 1",
                "#L2\t#t1 = j > 10",
                "if #t1 goto #L3",
                "a = 0",
                "j = j + 1",
                "goto #L2",
                "#L3",
                "i = i + 1",
                "goto #L0",
                "#L1",
                "i = 1",
                "#L4\t#t2 = i > 10",
                "if #t2 goto #L5",
                "a = 1",
                "i = i + 1",
                "goto #L4",
                "#L5"
            };

            var actualMerg = blocks.BlockMerging().Select(instructions => instructions.ToString().Trim());

            Assert.AreEqual(expectedMerg, actualMerg);
        }