Example #1
0
        public void Simple()
        {
            var TAC = GenerateTAC(
                @"
{
a = 3;
b = 3;
c = a + b;
a = d;
e = a;
}
");
            var blocks = new TACBaseBlocks(TAC.Instructions);

            blocks.GenBaseBlocks();
            var cfg       = new ControlFlowGraph(blocks.blocks);
            var optimizer = new ConstantPropagationIter();

            optimizer.Cfg          = cfg;
            optimizer.Instructions = TAC.Instructions;
            optimizer.Blocks       = blocks.blocks;
            optimizer.Run();
            var actual = optimizer.Instructions.Select(i => i.ToString().Trim()).ToList();

            var expected = new List <string>()
            {
                "a = 3",
                "b = 3",
                "c = 3 + 3",
                "a = d",
                "e = a"
            };

            Assert.AreEqual(expected, actual);
        }
Example #2
0
        public void GenericPropagation2()
        {
            var TAC = GenerateTAC(
                @"
{
b = 3;
if a 
{
goto 1;
}
b = 2;
1: r = b;
}
");
            var blocks = new TACBaseBlocks(TAC.Instructions);

            blocks.GenBaseBlocks();
            var cfg       = new ControlFlowGraph(blocks.blocks);
            var optimizer = new ConstantPropagationIter();

            optimizer.Cfg          = cfg;
            optimizer.Instructions = TAC.Instructions;
            optimizer.Blocks       = blocks.blocks;
            optimizer.Run();
            var actual = optimizer.Instructions.Select(i => i.ToString().Trim()).ToList();

            var expected = new List <string>()
            {
                "b = 3",
                "if a goto #L0",
                "goto #L1",
                "#L0",
                "goto 1",
                "#L1",
                "b = 2",
                "1",
                "r = b"
            };

            Assert.AreEqual(expected, actual);
        }