protected override IDictionary <IVariable, IExpression> Flow(CFGNode node, IDictionary <IVariable, IExpression> input)
        {
            IDictionary <IVariable, IExpression> result;

            if (input == null)
            {
                result = new Dictionary <IVariable, IExpression>();
            }
            else
            {
                result = new Dictionary <IVariable, IExpression>(input);
            }

            foreach (var instruction in node.Instructions)
            {
                var equality = this.Flow(instruction, result);

                foreach (var variable in instruction.ModifiedVariables)
                {
                    this.RemoveEqualitiesWithVariable(result, variable);
                }

                if (equality.HasValue)
                {
                    result.Add(equality.Value);
                }
            }

            return(result);
        }
 public void ConnectNodes(CFGNode predecessor, CFGNode successor)
 {
     successor.Predecessors.Add(predecessor);
     predecessor.Successors.Add(successor);
     this.Nodes.Add(predecessor);
     this.Nodes.Add(successor);
 }
        private static IDictionary <string, CFGNode> CreateNodes(IEnumerable <Instruction> instructions)
        {
            var leaders      = new Dictionary <string, CFGNode>();
            var nextIsLeader = true;
            var nodeId       = 2;

            foreach (var instruction in instructions)
            {
                var isLeader = nextIsLeader;
                nextIsLeader = false;

                if (instruction is TryInstruction ||
                    instruction is CatchInstruction ||
                    instruction is FinallyInstruction)
                {
                    isLeader = true;
                }

                if (isLeader && !leaders.ContainsKey(instruction.Label))
                {
                    var node = new CFGNode(nodeId++);
                    leaders.Add(instruction.Label, node);
                }

                if (instruction is UnconditionalBranchInstruction ||
                    instruction is ConditionalBranchInstruction)
                {
                    nextIsLeader = true;
                    var branch = instruction as BranchInstruction;

                    if (!leaders.ContainsKey(branch.Target))
                    {
                        var node = new CFGNode(nodeId++);
                        leaders.Add(branch.Target, node);
                    }
                }
                else if (instruction is SwitchInstruction)
                {
                    nextIsLeader = true;
                    var branch = instruction as SwitchInstruction;

                    foreach (var target in branch.Targets)
                    {
                        if (!leaders.ContainsKey(target))
                        {
                            var node = new CFGNode(nodeId++);
                            leaders.Add(target, node);
                        }
                    }
                }
                else if (instruction is ReturnInstruction ||
                         instruction is ThrowInstruction)
                {
                    nextIsLeader = true;
                }
            }

            return(leaders);
        }
        protected override Subset <DefinitionInstruction> Flow(CFGNode node, Subset <DefinitionInstruction> input)
        {
            var output = input.Clone();
            var kill   = KILL[node.Id];
            var gen    = GEN[node.Id];

            output.Except(kill);
            output.Union(gen);
            return(output);
        }
Example #5
0
        protected override PointsToGraph Flow(CFGNode node, PointsToGraph input)
        {
            var ptg = input.Clone();

            foreach (var instruction in node.Instructions)
            {
                this.Flow(ptg, instruction);
            }

            return(ptg);
        }
        private static ISet <CFGNode> ComputeDominators(CFGNode node)
        {
            var result = new HashSet <CFGNode>();

            do
            {
                result.Add(node);
                node = node.ImmediateDominator;
            }while (node != null);

            return(result);
        }
        protected override IDictionary <IVariable, IVariable> Flow(CFGNode node, IDictionary <IVariable, IVariable> output)
        {
            var input = new Dictionary <IVariable, IVariable>(output);
            var kill  = KILL[node.Id];
            var gen   = GEN[node.Id];

            foreach (var variable in kill)
            {
                this.RemoveCopiesWithVariable(input, variable);
            }

            input.AddRange(gen);
            return(input);
        }
        private static CFGNode FindCommonAncestor(CFGNode a, CFGNode b)
        {
            while (a.ForwardIndex != b.ForwardIndex)
            {
                while (a.ForwardIndex > b.ForwardIndex)
                {
                    a = a.ImmediateDominator;
                }

                while (b.ForwardIndex > a.ForwardIndex)
                {
                    b = b.ImmediateDominator;
                }
            }

            return(a);
        }
        private static CFGNode[] ComputeBackwardTopologicalSort(ControlFlowGraph cfg)
        {
            // reverse postorder traversal from exit node
            var stack  = new Stack <CFGNode>();
            var result = new CFGNode[cfg.Nodes.Count];
            var status = new TopologicalSortNodeStatus[cfg.Nodes.Count];
            var index  = cfg.Nodes.Count - 1;

            foreach (var node in cfg.Exits)
            {
                stack.Push(node);
                status[node.Id] = TopologicalSortNodeStatus.FirstVisit;
            }

            do
            {
                var node        = stack.Peek();
                var node_status = status[node.Id];

                if (node_status == TopologicalSortNodeStatus.FirstVisit)
                {
                    status[node.Id] = TopologicalSortNodeStatus.SecondVisit;

                    foreach (var pred in node.Predecessors)
                    {
                        var pred_status = status[pred.Id];

                        if (pred_status == TopologicalSortNodeStatus.NeverVisited)
                        {
                            stack.Push(pred);
                            status[pred.Id] = TopologicalSortNodeStatus.FirstVisit;
                        }
                    }
                }
                else if (node_status == TopologicalSortNodeStatus.SecondVisit)
                {
                    stack.Pop();
                    node.BackwardIndex = index;
                    result[index]      = node;
                    index--;
                }
            }while (stack.Count > 0);

            return(result);
        }
 protected override IDictionary <IVariable, IExpression> InitialValue(CFGNode node)
 {
     return(GEN[node.Id]);
 }
 public DataFlowAnalysisResult <IDictionary <IVariable, IExpression> > this[CFGNode node]
 {
     get { return(this.result[node.Id]); }
 }
 public CFGEdge(CFGNode source, CFGNode target)
 {
     this.Source = source;
     this.Target = target;
 }
 protected override Subset <DefinitionInstruction> InitialValue(CFGNode node)
 {
     return(GEN[node.Id]);
 }
        //public IExpression Condition { get; set; }

        public CFGLoop(CFGNode header)
        {
            this.Header = header;
            this.Body   = new HashSet <CFGNode>();
            this.Body.Add(header);
        }
Example #15
0
 protected override PointsToGraph InitialValue(CFGNode node)
 {
     return(this.initialGraph);
 }
 protected abstract T InitialValue(CFGNode node);
 protected abstract T Flow(CFGNode node, T input);
        private void RenameVariables(CFGNode node, IDictionary <IVariable, Stack <DerivedVariable> > derived_variables, Dictionary <IVariable, uint> indices)
        {
            foreach (var instruction in node.Instructions)
            {
                DerivedVariable result_derived = null;

                if (instruction is DefinitionInstruction)
                {
                    var definition = instruction as DefinitionInstruction;

                    //if (definition.HasResult && indices.ContainsKey(definition.Result))
                    if (definition.HasResult)
                    {
                        var result = definition.Result;
                        var index  = indices[result];

                        result_derived    = new DerivedVariable(result, index);
                        definition.Result = result_derived;
                    }
                }

                foreach (var variable in instruction.UsedVariables)
                {
                    // When the instruction is a phi, its arguments (used variables)
                    // are already derived variables, so there is no entry in
                    // derived_variables dictionary for them.
                    if (!derived_variables.ContainsKey(variable))
                    {
                        continue;
                    }

                    var stack   = derived_variables[variable];
                    var derived = stack.Peek();
                    instruction.Replace(variable, derived);
                }

                if (result_derived != null)
                {
                    var result       = result_derived.Original;
                    var index        = result_derived.Index;
                    var result_stack = derived_variables[result];

                    result_stack.Push(result_derived);
                    indices[result] = index + 1;
                }
            }

            foreach (var succ in node.Successors)
            {
                if (!phi_instructions.ContainsKey(succ))
                {
                    continue;
                }
                var node_phi_instructions = phi_instructions[succ];

                foreach (var entry in node_phi_instructions)
                {
                    var variable = entry.Key;
                    var phi      = entry.Value;
                    var stack    = derived_variables[variable];
                    var derived  = stack.Peek();

                    phi.Arguments.Add(derived);
                }
            }

            foreach (var child in node.ImmediateDominated)
            {
                this.RenameVariables(child, derived_variables, indices);
            }

            foreach (var instruction in node.Instructions)
            {
                if (instruction is DefinitionInstruction)
                {
                    var definition = instruction as DefinitionInstruction;

                    if (definition.HasResult)
                    {
                        var derived = definition.Result as DerivedVariable;
                        var result  = derived.Original;
                        var stack   = derived_variables[result];

                        stack.Pop();
                    }
                }
            }
        }