Exemplo n.º 1
0
        public bool Optimize(ThreeAddressCode tac)
        {
            var isApplied    = false;
            var targetLabels = FindAllTargetLabels(tac);

            var currentNode   = tac.TACodeLines.First;
            var linesToDelete = new List <TacNode>();

            while (currentNode != null)
            {
                var line = currentNode.Value;

                if (line is TacIfGotoNode ifGotoNode && Equals(ifGotoNode.Condition, "True"))
                {
                    if (CheckLabels(targetLabels, currentNode.Next, ifGotoNode.TargetLabel, linesToDelete))
                    {
                        tac[line.Label] = new TacGotoNode {
                            Label = ifGotoNode.Label, TargetLabel = ifGotoNode.TargetLabel
                        };
                        tac.RemoveNodes(linesToDelete);
                        linesToDelete.Clear();
                        isApplied = true;
                    }
                }
                currentNode = currentNode.Next;
            }

            return(isApplied);
        }
Exemplo n.º 2
0
        public bool Optimize(ThreeAddressCode block)
        {
            LinkedList <TacNode> deadCodeList = GetDeadCode(block);

            if (deadCodeList.Count == 0)
            {
                return(false);
            }
            else
            {
                block.RemoveNodes(deadCodeList);
                return(true);
            }
        }
Exemplo n.º 3
0
        public bool Optimize(ThreeAddressCode tac)
        {
            var isApplied      = false;
            var labels         = FindAllLabels(tac);
            var currentNode    = tac.TACodeLines.First;
            var linesToDelete  = new List <TacNode>();
            var variablesValue = new Dictionary <string, string>();
            var previuosNodes  = new HashSet <TacNode>();

            while (currentNode != null)
            {
                var line = currentNode.Value;
                if (line is TacAssignmentNode assignmentNode)
                {
                    var rightPart = $"{assignmentNode.FirstOperand} {assignmentNode.Operation} {assignmentNode.SecondOperand}";
                    if (!variablesValue.ContainsKey(assignmentNode.LeftPartIdentifier))
                    {
                        variablesValue.Add(assignmentNode.LeftPartIdentifier, rightPart);
                    }
                    else
                    {
                        variablesValue[assignmentNode.LeftPartIdentifier] = rightPart;
                    }
                }

                if (line is TacIfGotoNode ifGotoNode && Equals(variablesValue[ifGotoNode.Condition], "True") || line.GetType() == typeof(TacGotoNode))
                {
                    var gotoNode = line as TacGotoNode;
                    if (!previuosNodes.Contains(tac[gotoNode.TargetLabel]))
                    {
                        if (CheckLabels(labels, currentNode.Next, gotoNode.TargetLabel, linesToDelete))
                        {
                            currentNode.Value = new TacGotoNode {
                                Label = gotoNode.Label, TargetLabel = gotoNode.TargetLabel
                            };
                            isApplied = true;
                        }
                    }
                }
                previuosNodes.Add(line);
                currentNode = currentNode.Next;
            }
            tac.RemoveNodes(linesToDelete);

            return(isApplied);
        }
Exemplo n.º 4
0
        public bool Optimize(ThreeAddressCode tac)
        {
            var isApplied      = false;
            var labels         = FindAllLabels(tac);
            var currentNode    = tac.TACodeLines.First;
            var linesToDelete  = new List <TacNode>();
            var variablesValue = new Dictionary <string, string>();

            while (currentNode != null)
            {
                var line = currentNode.Value;
                if (line is TacAssignmentNode assignmentNode)
                {
                    if (!variablesValue.ContainsKey(assignmentNode.LeftPartIdentifier))
                    {
                        variablesValue.Add(assignmentNode.LeftPartIdentifier, assignmentNode.FirstOperand);
                    }
                    variablesValue[assignmentNode.LeftPartIdentifier] = assignmentNode.FirstOperand;
                }

                if (line is TacIfGotoNode ifGotoNode && Equals(variablesValue[ifGotoNode.Condition], "True") || line.GetType() == typeof(TacGotoNode))
                {
                    var gotoNode = line as TacGotoNode;
                    if (CheckLabels(labels, currentNode.Next, gotoNode.TargetLabel, linesToDelete))
                    {
                        currentNode.Value = new TacGotoNode {
                            Label = gotoNode.Label, TargetLabel = gotoNode.TargetLabel
                        };
                        tac.RemoveNodes(linesToDelete);
                        linesToDelete.Clear();
                        isApplied = true;
                    }
                }
                currentNode = currentNode.Next;
            }

            return(isApplied);
        }