protected override ThreeAddressCode ProcessTAC(ThreeAddressCode tacContainer)
        {
            var optimization = new CommonSubexprOptimization();

            optimization.Optimize(tacContainer);
            return(tacContainer);
        }
Exemplo n.º 2
0
        public void Optimize_ChangedSecondOperand()
        {
            var tacVisitor = new ThreeAddressCodeVisitor();

            Utils.AddAssignmentNode(tacVisitor, "L1", "t1", "a", "+", "b");
            Utils.AddAssignmentNode(tacVisitor, null, "b", "2");
            Utils.AddAssignmentNode(tacVisitor, null, "t2", "a", "+", "b");

            var expectedResult = new ThreeAddressCodeVisitor();

            Utils.AddAssignmentNode(expectedResult, "L1", "t1", "a", "+", "b");
            Utils.AddAssignmentNode(expectedResult, null, "b", "2");
            Utils.AddAssignmentNode(expectedResult, null, "t2", "a", "+", "b");

            var optimization = new CommonSubexprOptimization();
            var isOptimized  = optimization.Optimize(tacVisitor.TACodeContainer);

            Assert.IsFalse(isOptimized);
            Assert.AreEqual(tacVisitor.ToString(), expectedResult.ToString());
        }
Exemplo n.º 3
0
        public void Optimize_MultipleExpressions()
        {
            var tacVisitor = new ThreeAddressCodeVisitor();

            Utils.AddAssignmentNode(tacVisitor, "L1", "t1", "a", "+", "b");
            Utils.AddAssignmentNode(tacVisitor, null, "t2", "c", "+", "d");
            Utils.AddAssignmentNode(tacVisitor, null, "t3", "c", "+", "d");
            Utils.AddAssignmentNode(tacVisitor, null, "t4", "a", "+", "b");

            var expectedResult = new ThreeAddressCodeVisitor();

            Utils.AddAssignmentNode(expectedResult, "L1", "t1", "a", "+", "b");
            Utils.AddAssignmentNode(expectedResult, null, "t2", "c", "+", "d");
            Utils.AddAssignmentNode(expectedResult, null, "t3", "t2");
            Utils.AddAssignmentNode(expectedResult, null, "t4", "t1");

            var optimization = new CommonSubexprOptimization();
            var isOptimized  = optimization.Optimize(tacVisitor.TACodeContainer);

            Assert.IsTrue(isOptimized);
            Assert.AreEqual(tacVisitor.ToString(), expectedResult.ToString());
        }
Exemplo n.º 4
0
        public void Optimize_ChangedOperandThenTwiceRepeatedExpression()
        {
            var tacContainer = new ThreeAddressCode();

            Utils.AddAssignmentNode(tacContainer, "L1", "t1", "a", "+", "b");
            Utils.AddAssignmentNode(tacContainer, null, "b", "2");
            Utils.AddAssignmentNode(tacContainer, null, "t2", "a", "+", "b");
            Utils.AddAssignmentNode(tacContainer, null, "t3", "a", "+", "b");

            var expectedResult = new ThreeAddressCodeVisitor();

            Utils.AddAssignmentNode(expectedResult, "L1", "t1", "a", "+", "b");
            Utils.AddAssignmentNode(expectedResult, null, "b", "2");
            Utils.AddAssignmentNode(expectedResult, null, "t2", "a", "+", "b");
            Utils.AddAssignmentNode(expectedResult, null, "t3", "t2");

            var optimization = new CommonSubexprOptimization();
            var isOptimized  = optimization.Optimize(tacContainer);

            Assert.IsTrue(isOptimized);
            Assert.AreEqual(tacContainer.ToString(), expectedResult.ToString());
        }