Esempio n. 1
0
        public HashSet <TacNode> Calculate(HashSet <TacNode> _in, ThreeAddressCode bblock)
        {
            basicBlock = bblock;
            var func = _in;

            var gen   = new HashSet <TacNode>();
            var _line = new TacAssignmentNode();

            gen.UnionWith(GetLineGen(_line));

            var kill = new HashSet <TacNode>();

            kill.UnionWith(GetLineKill(_line));

            foreach (var line in GetBasicBlock())
            {
                var exceptKill = new HashSet <TacNode>();
                exceptKill.UnionWith(func);

                exceptKill.ExceptWith(kill);

                gen.UnionWith(exceptKill);

                func = new HashSet <TacNode>();
                func.UnionWith(gen);
            }

            return(func);
        }
        public void Optimize_DivisionInts()
        {
            var threeAddressCodeVisitor = new ThreeAddressCodeVisitor();

            threeAddressCodeVisitor.TACodeContainer.PushNode(
                new TacAssignmentNode()
            {
                LeftPartIdentifier = "t1",
                FirstOperand       = "4",
                Operation          = "/",
                SecondOperand      = "2"
            });

            var isOptimized = new ConvConstOptimization().Optimize(threeAddressCodeVisitor.TACodeContainer);

            var optimizedTac = threeAddressCodeVisitor.TACodeContainer.TACodeLines.First.Value as TacAssignmentNode;
            var res          = new TacAssignmentNode()
            {
                LeftPartIdentifier = "t1",
                FirstOperand       = "2"
            };

            Assert.IsTrue(isOptimized);
            Assert.AreEqual(optimizedTac.ToString(), res.ToString());
        }
Esempio n. 3
0
 private bool Equals(TacAssignmentNode other)
 {
     return(string.Equals(Label, other.Label) &&
            string.Equals(LeftPartIdentifier, other.LeftPartIdentifier) &&
            string.Equals(FirstOperand, other.FirstOperand) &&
            string.Equals(SecondOperand, other.SecondOperand) &&
            string.Equals(Operation, other.Operation));
 }
Esempio n. 4
0
        public string CombineRightPart(TacAssignmentNode node)
        {
            string result = node.FirstOperand;

            if (node.Operation != null)
            {
                result += node.Operation + node.SecondOperand;
            }
            return(result);
        }
Esempio n. 5
0
        public TacNode CreateAssignNode(string idName)
        {
            TacNode assignNode = new TacAssignmentNode()
            {
                LeftPartIdentifier = idName,
                FirstOperand       = this.FirstOperand,
                Operation          = this.Operation,
                SecondOperand      = this.SecondOperand
            };

            return(assignNode);
        }
        private void ChangeByVariable(TacNode node, TacAssignmentNode replacingNode)
        {
            switch (node)
            {
            case TacAssignmentNode assNode:
                if (assNode.FirstOperand.Equals(replacingNode.LeftPartIdentifier))
                {
                    assNode.FirstOperand = replacingNode.FirstOperand;
                }

                if (assNode.SecondOperand != null && assNode.SecondOperand.Equals(replacingNode.LeftPartIdentifier))
                {
                    assNode.SecondOperand = replacingNode.FirstOperand;
                }
                break;

            case TacIfGotoNode ifGotoNode:
                //ifGotoNode.Condition = replacingNode.FirstOperand;
                break;
            }
        }
        public bool Optimize(ThreeAddressCode tac)
        {
            bool isUsed = false;
            var  node   = tac.TACodeLines.First;

            while (node != null)
            {
                var next  = node.Next;
                var val   = node.Value;
                var label = val.Label;
                if (next != null)
                {
                    var nextVal = next.Value;
                    if (val is TacIfGotoNode && nextVal is TacGotoNode)
                    {
                        isUsed = true;
                        var     ifVal      = val as TacIfGotoNode;
                        string  tempVar    = TmpNameManager.Instance.GenerateTmpVariableName();
                        TacNode tempAssign = new TacAssignmentNode()
                        {
                            LeftPartIdentifier = tempVar,
                            Operation          = "!",
                            SecondOperand      = ifVal.Condition
                        };
                        tac.TACodeLines.AddBefore(tac.TACodeLines.Find(val), tempAssign);
                        ifVal.Condition   = tempVar;
                        ifVal.TargetLabel = (nextVal as TacGotoNode).TargetLabel;
                        var remove = next;
                        next = node;
                        tac.TACodeLines.Remove(remove);
                    }
                }
                node = next;
            }
            return(isUsed);
        }
Esempio n. 8
0
        public bool Optimize(ThreeAddressCode tac)
        {
            bool isUsed = false;
            var  node   = tac.TACodeLines.First;

            var currentNumber = 0;
            var valueToNumber = new Dictionary <string, int>();
            var valueDict     = new Dictionary <int, LinkedListNode <TacNode> >();
            var numberToT     = new Dictionary <int, string>();
            var parameters    = tac.TACodeLines.OfType <TacAssignmentNode>().Select(e => e.LeftPartIdentifier);

            while (node != null)
            {
                var val   = node.Value;
                var label = val.Label;
                if (val is TacAssignmentNode assigned)
                {
                    var Ti  = assigned.LeftPartIdentifier;
                    var Li  = assigned.FirstOperand;
                    var Ri  = assigned.SecondOperand;
                    var Opi = assigned.Operation ?? String.Empty;

                    if (Ri != null)
                    {
                        int valL;
                        int valR;
                        if (!valueToNumber.TryGetValue(Li, out valL))
                        {
                            valL = currentNumber++;
                            valueToNumber.Add(Li, valL);
                        }

                        if (!valueToNumber.TryGetValue(Ri, out valR))
                        {
                            valR = currentNumber++;
                            valueToNumber.Add(Ri, valR);
                        }
                        var hash         = $"{valL} {Opi} {valR}".Trim();
                        var hashReversed = $"{valR} {Opi} {valL}".Trim();
                        int tmp;
                        if (valueToNumber.TryGetValue(hash, out tmp) || valueToNumber.TryGetValue(hashReversed, out tmp))
                        {
                            isUsed            = true;
                            valueToNumber[Ti] = tmp;
                            var paramToNumber = valueToNumber.Where(e => parameters.Contains(e.Key) && e.Key != Ti);
                            var findable      = paramToNumber.FirstOrDefault(e => e.Value == tmp);
                            if (findable.Key != null)
                            {
                                assigned.FirstOperand  = findable.Key;
                                assigned.Operation     = null;
                                assigned.SecondOperand = null;
                            }
                            else
                            {
                                var tmpTacNode = new TacAssignmentNode()
                                {
                                    LeftPartIdentifier = TmpNameManager.Instance.GenerateTmpVariableName(),
                                    FirstOperand       = numberToT[tmp]
                                };
                                tac.TACodeLines.AddAfter(valueDict[tmp], tmpTacNode);
                                assigned.FirstOperand  = tmpTacNode.LeftPartIdentifier;
                                assigned.Operation     = null;
                                assigned.SecondOperand = null;
                            }

                            valueDict[tmp] = node;
                        }
                        else
                        {
                            var valN = currentNumber++;
                            valueToNumber.Add(hash, valN);
                            valueDict[valN]   = node;
                            valueToNumber[Ti] = valN;
                            numberToT[valN]   = Ti;
                        }
                    }
                    else
                    {
                        int valL;
                        if (!valueToNumber.TryGetValue(Li, out valL))
                        {
                            valL = currentNumber++;
                            valueToNumber.Add(Li, valL);
                        }
                        valueDict[valL]   = node;
                        valueToNumber[Ti] = valL;
                        numberToT[valL]   = Ti;
                    }
                }
                node = node.Next;
            }
            return(isUsed);
        }
 private void AssignRightPartVarReplace(TacAssignmentNode assign, string idName)
 {
     assign.FirstOperand  = idName;
     assign.Operation     = null;
     assign.SecondOperand = null;
 }
 public TacExpr(TacAssignmentNode assign)
 {
     FirstOperand  = assign.FirstOperand;
     Operation     = assign.Operation;
     SecondOperand = assign.SecondOperand;
 }
Esempio n. 11
0
 private static string RightPartToString(TacAssignmentNode node) =>
 $"{node.FirstOperand}{node.Operation}{node.SecondOperand}";
Esempio n. 12
0
 private bool IsSimpleAssignNode(TacAssignmentNode assign)
 {
     return(assign.Operation == null && assign.SecondOperand == null);
 }
        public void Constructor_IfElse()
        {
            /*
             * Code:
             * a = 1;
             * if(b)
             * {
             * a = 4;
             * } else
             * {
             * c = 5;
             * }
             * a = c;
             * c = 1;
             *
             * Tac:
             * a = 1
             * t1 = b
             * if t1 goto L1
             * c = 5
             * goto L2
             * L1: a = 4
             * L2: a = с
             * c = 1
             */

            var tacContainer = new ThreeAddressCode();

            //1
            Utils.AddAssignmentNode(tacContainer, null, "a", "1");
            Utils.AddAssignmentNode(tacContainer, null, "t1", "b");
            Utils.AddIfGotoNode(tacContainer, null, "L1", "t1");
            //2
            Utils.AddAssignmentNode(tacContainer, null, "c", "5");
            Utils.AddGotoNode(tacContainer, null, "L2");
            //3
            Utils.AddAssignmentNode(tacContainer, "L1", "a", "4");
            //4
            Utils.AddAssignmentNode(tacContainer, "L2", "a", "c");
            Utils.AddAssignmentNode(tacContainer, null, "c", "1");

            var cfg = new ControlFlowGraph(tacContainer);

            E_GenKillVisitor availExprVisitor = new E_GenKillVisitor();
            var availExprContainers           = availExprVisitor.GenerateAvailableExpressionForBlocks(cfg.SourceBasicBlocks);

            var ita = new AvailableExpressionsITA(cfg, availExprContainers);

            var inData  = ita.InOut.In;
            var outData = ita.InOut.Out;

            var expectedIn0 = new TacAssignmentNode();

            var expectedIn1 = new TacAssignmentNode();

            expectedIn1.LeftPartIdentifier = "t1";
            expectedIn1.FirstOperand       = "b";

            var expectedOut0 = new TacAssignmentNode();

            expectedOut0.LeftPartIdentifier = "t1";
            expectedOut0.FirstOperand       = "b";

            Assert.AreEqual(expectedIn1.ToString(), inData[cfg[1]].First().ToString());
            Assert.AreEqual(expectedIn1.ToString(), inData[cfg[2]].First().ToString());
            Assert.AreEqual(expectedIn1.ToString(), inData[cfg[3]].First().ToString());

            Assert.AreEqual(expectedOut0.ToString(), outData[cfg[1]].First().ToString());
            Assert.AreEqual(expectedOut0.ToString(), outData[cfg[2]].First().ToString());
            Assert.AreEqual(expectedOut0.ToString(), outData[cfg[3]].First().ToString());
        }