Beispiel #1
0
        private void OptimizeBlock(BasicBlock block)
        {
            var currentTable = In[block].Copy();

            for (int i = 0; i < block.Instructions.Count; ++i)
            {
                if (block.Instructions[i].Operation.IsArithmetic())
                {
                    var expression = new AvailableExpression(
                        block.Instructions[i].Argument1,
                        block.Instructions[i].Argument2,
                        block.Instructions[i].Operation);

                    if (currentTable.ExpressionsTable.ContainsKey(expression))
                    {
                        block.Instructions[i].Operation = "=";
                        block.Instructions[i].Argument1 = currentTable.ExpressionsTable[expression];
                        block.Instructions[i].Argument2 = "";
                    }
                    else
                    {
                        currentTable.AddExpression(expression, block.Instructions[i]);
                    }
                }
                if (block.Instructions[i].Result.IsVariable())
                {
                    currentTable.RemoveExpressions(block.Instructions[i].Result);
                }
            }
        }
Beispiel #2
0
 public void AddExpression(AvailableExpression newExpression, TACInstruction instrution)
 {
     if (!ExpressionsTable.ContainsKey(newExpression))
     {
         ExpressionsTable.Add(newExpression, instrution.Result);
     }
 }
Beispiel #3
0
        public void InstanceIsNotEqualToNull()
        {
            var expression = new MultiplyExpression(new IntegerExpression(1), new VariableExpression("x"));
            var available  = new AvailableExpression(_CreateFlowNode(), expression);

            Assert.AreNotEqual(available, null);
        }
Beispiel #4
0
        public void InstanceHasSameHashCodeAsItself()
        {
            var expression = new MultiplyExpression(new IntegerExpression(1), new VariableExpression("x"));
            var available  = new AvailableExpression(_CreateFlowNode(), expression);

            Assert.AreEqual(available.GetHashCode(), available.GetHashCode());
        }
Beispiel #5
0
        public void InstanceIsNotEqualToCompletelyDifferent()
        {
            var expression1 = new AddExpression(new IntegerExpression(1), new IntegerExpression(2));
            var expression2 = new ModuloExpression(new VariableExpression("y"), new VariableExpression("z"));
            var available1  = new AvailableExpression(_CreateFlowNode(), expression1);
            var available2  = new AvailableExpression(_CreateFlowNode(), expression2);

            Assert.AreNotEqual(available1, available2);
        }
Beispiel #6
0
        public void InstanceIsNotEqualToOtherWithDifferentNode()
        {
            var expression1 = new DivideExpression(new IntegerExpression(5), new VariableExpression("y"));
            var expression2 = new DivideExpression(new IntegerExpression(5), new VariableExpression("y"));
            var available1  = new AvailableExpression(_CreateFlowNode(), expression1);
            var available2  = new AvailableExpression(_CreateFlowNode(), expression2);

            Assert.AreNotEqual(available1, available2);
        }
Beispiel #7
0
        public void InstanceHasNotSameHashCodeAsCompletelyDifferent()
        {
            var expression1 = new ModuloExpression(new IntegerExpression(3), new VariableExpression("y"));
            var expression2 = new DivideExpression(new IntegerExpression(4), new VariableExpression("z"));
            var available1  = new AvailableExpression(_CreateFlowNode(), expression1);
            var available2  = new AvailableExpression(_CreateFlowNode(), expression2);

            Assert.AreNotEqual(available1.GetHashCode(), available2.GetHashCode());
        }
Beispiel #8
0
        public void InstanceHasNotSameHashCodeAsOtherWithDifferenNode()
        {
            var expression1 = new DivideExpression(new IntegerExpression(5), new VariableExpression("y"));
            var expression2 = new DivideExpression(new IntegerExpression(5), new VariableExpression("y"));
            var available1  = new AvailableExpression(_CreateFlowNode(), expression1);
            var available2  = new AvailableExpression(_CreateFlowNode(), expression2);

            Assert.AreNotEqual(available1.GetHashCode(), available2.GetHashCode());
        }
Beispiel #9
0
        public void InstanceIsNotEqualToOtherWithDifferentRightOperand()
        {
            var node        = _CreateFlowNode();
            var expression1 = new AddExpression(new VariableExpression("y"), new VariableExpression("y"));
            var expression2 = new AddExpression(new VariableExpression("y"), new VariableExpression("z"));
            var available1  = new AvailableExpression(node, expression1);
            var available2  = new AvailableExpression(node, expression2);

            Assert.AreNotEqual(available1, available2);
        }
Beispiel #10
0
        public void InstanceIsNotEqualToOtherWithOperator()
        {
            var node        = _CreateFlowNode();
            var expression1 = new AddExpression(new IntegerExpression(5), new VariableExpression("y"));
            var expression2 = new MultiplyExpression(new IntegerExpression(5), new VariableExpression("y"));
            var available1  = new AvailableExpression(node, expression1);
            var available2  = new AvailableExpression(node, expression2);

            Assert.AreNotEqual(available1, available2);
        }
Beispiel #11
0
        public void InstanceIsEqualToOtherWithSameInformation()
        {
            var node        = _CreateFlowNode();
            var expression1 = new DivideExpression(new IntegerExpression(5), new VariableExpression("y"));
            var expression2 = new DivideExpression(new IntegerExpression(5), new VariableExpression("y"));
            var available1  = new AvailableExpression(node, expression1);
            var available2  = new AvailableExpression(node, expression2);

            Assert.AreEqual(available1, available2);
        }
Beispiel #12
0
        public void InstanceHasNotSameHashCodeAsOtherWithDifferenLeftRight()
        {
            var node        = _CreateFlowNode();
            var expression1 = new ModuloExpression(new IntegerExpression(3), new VariableExpression("y"));
            var expression2 = new ModuloExpression(new IntegerExpression(3), new VariableExpression("z"));
            var available1  = new AvailableExpression(node, expression1);
            var available2  = new AvailableExpression(node, expression2);

            Assert.AreNotEqual(available1.GetHashCode(), available2.GetHashCode());
        }
Beispiel #13
0
        public void InstanceHasNotSameHashCodeAsOtherWithDifferentOperator()
        {
            var node        = _CreateFlowNode();
            var expression1 = new AddExpression(new IntegerExpression(5), new VariableExpression("y"));
            var expression2 = new MultiplyExpression(new IntegerExpression(5), new VariableExpression("y"));
            var available1  = new AvailableExpression(node, expression1);
            var available2  = new AvailableExpression(node, expression2);

            Assert.AreNotEqual(available1.GetHashCode(), available2.GetHashCode());
        }