Exemple #1
0
 /// <summary>
 /// A constructor that recives the code of the basic block
 /// </summary>
 /// <param name="basicBlockCode">The Code in the new basic block</param>
 public GraphNode(String basicBlockCode)
 {
     branch1 = null;
     branch2 = null;
     this.basicBlockCode = basicBlockCode;
 }
Exemple #2
0
 /// <summary>
 /// This method removes a dominatee to my dominated nodes list
 /// </summary>
 /// <param name="dominatedNodeKey">The key of the dominated node</param>
 /// <param name="dominatedNode">The dominated node</param>
 private void RemoveFromDominated(String dominatedNodeKey, GraphNode dominatedNode)
 {
     if (this.DominatedList.ContainsKey(dominatedNodeKey)) //if node is in the list
     {
         DominatedList.Remove(dominatedNodeKey);
     }
 }
Exemple #3
0
 /// <summary>
 /// Empty Constructor
 /// </summary>
 public GraphNode()
 {
     branch1 = null;
     branch2 = null;
     this.basicBlockCode = "";
 }
Exemple #4
0
 /// <summary>
 /// This method adds a dominatee to my dominated nodes list
 /// </summary>
 /// <param name="dominatedNodeKey">The key of the dominated node</param>
 /// <param name="dominatedNode">The dominated node</param>
 private void addToDominated(String dominatedNodeKey,GraphNode dominatedNode)
 {
     if (!this.DominatedList.ContainsKey(dominatedNodeKey)) //if node not allready in list
     {
         DominatedList.Add(dominatedNodeKey, dominatedNode);
     }
 }
Exemple #5
0
 /////////////////////////////////////////////////////////
 /////////////////// STATIC METHODS //////////////////////
 /////////////////////////////////////////////////////////
 /// <summary>
 /// This method calculates the Dominance for a CFG (Recursivly) 
 /// </summary>
 /// <param name="root">Root of a CFG</param>
 public static void calculateDominanceDataforCFG(GraphNode root,BackEdgesList backEdgeList)
 {
     if (root == null) //Stoping condition
         return;
     if (root.DominatorsList.Count == 0) //If i am empty (it means I am the true root of the tree)
         root.dom(root.DominatorsList);
     //PostOrderTraverse
     if (root.branch1 != null)
     {
         if (root.branch1.dom(root.DominatorsList) == true) //If it's not a back edge
             calculateDominanceDataforCFG(root.branch1, backEdgeList);
         else
         {
             backEdgeList.Add(root.ID,root.branch1.ID);
         } //TODO:deal with backedge
     }
     if (root.branch2 != null)
     {
         if (root.branch2.dom(root.DominatorsList) == true) //if it's not a back edge
             calculateDominanceDataforCFG(root.branch2, backEdgeList);
         else
         {
             backEdgeList.Add(root.ID, root.branch2.ID); ;
         } //TODO: deal with backedge
     }
 }
 /// <summary>
 /// This is a recursive method that extract the nodes that belong to the body of the loop.
 /// </summary>
 /// <param name="root">The node that is the target of the backedge</param>
 /// <param name="source">the first node ID, the target of the backedge</param>
 /// <param name="target">the ID of the last node in the loop, the source of the backedge</param>
 /// <param name="loopBody">This is a dictionary that will be used to store the nodes that belong to the body of the loop</param>
 /// <returns>false if the root doesn't belong to the loop body, otherwize true</returns>
 private bool getLoopBodyFromHeaderToLast(GraphNode root, String source,String target,Dictionary<String,GraphNode> loopBody)
 {
     if (root == null) return false;
     if (!root.DominatorsList.ContainsKey(source)) return false; //if the root is not dominated by the loop entrence return false
     if (loopBody.ContainsKey(root.ID)) return true; //if root is allready in the loop return true;
     if (root.ID == target) //if the root is the target add it to the loopBody
     {
         if (!loopBody.ContainsKey(root.ID)) //prevent duplicates
             loopBody.Add(root.ID, root);
         return true;
     }
     bool returnValue = false;
     //Try going to the first branch
     if (getLoopBodyFromHeaderToLast(root.Branch1, source, target, loopBody) && root.DominatorsList.ContainsKey(source))
     {
         if (!loopBody.ContainsKey(root.ID)) //prevent duplicates
             loopBody.Add(root.ID, root);
         returnValue = true;
     }
     //Try going to the second branch
     if (getLoopBodyFromHeaderToLast(root.Branch2, source, target, loopBody) && root.DominatorsList.ContainsKey(source))
     {
         if (!loopBody.ContainsKey(root.ID) )
             loopBody.Add(root.ID, root);
         returnValue = true;
     }
     return returnValue;
 }
 /// <summary>
 /// Empty constructor
 /// </summary>
 public IntermidiatCodeAsBasicBlocksCFG()
 {
     root = null;
     cfgBackEdgesList = null;
 }
Exemple #8
0
        /// <summary>
        /// This method reads a graph input file and builds a tree 
        /// of it
        /// </summary>
        /// <param name="filePath"></param>
        public IntermidiatCodeAsBasicBlocksCFG parsVCGfile(String filePath)
        {
            TextReader tr = new StreamReader(filePath);
            String line = "";
            String basicBlockCode = "";
            String intermidiatCodeInBasicBlocks = "";
            Dictionary<String, GraphNode> basicBlockDictionary = new Dictionary<string, GraphNode>(); //Dictionary will be used for indexing
            BackEdgesList backEdgeList = new BackEdgesList();
            IntermidiatCodeAsBasicBlocksCFG code = new IntermidiatCodeAsBasicBlocksCFG();
            GraphNode root = null;
            while ((line = tr.ReadLine()) != null) //read the file till the end
            {
                if (line.Contains("node")) //Create nodes
                    {
                        GraphNode basicBlock = new GraphNode();
                        line = tr.ReadLine(); //get basic block header
                        int titleIndex = line.IndexOf('\"')+1;
                        line = line.Substring(titleIndex);
                        titleIndex = line.IndexOf('\"');
                        line = line.Substring(0, titleIndex);
                        intermidiatCodeInBasicBlocks += "==============================\n";
                        intermidiatCodeInBasicBlocks += "Basic Block Title = " + line +"\n";
                        intermidiatCodeInBasicBlocks += "==============================\n";
                        basicBlock.ID = line;
                        //Extract the basic blocks code from the file
                        basicBlockCode = "";
                        while (!(line = tr.ReadLine()).Contains('}'))
                            basicBlockCode += line +"\n";
                        if (line.Contains('['))
                        {
                            line = line.Split('}')[0];
                            line = line.Substring(0, line.Length - 2);
                            basicBlockCode += line + "\n";
                        }
                        intermidiatCodeInBasicBlocks += basicBlockCode;

                        basicBlock.BasicBlockCode = basicBlockCode;
                        basicBlockDictionary.Add(basicBlock.ID, basicBlock); //Add basic block to list
                        if (root == null)
                            root = basicBlock;
                        intermidiatCodeInBasicBlocks += "\n";
                    }
                if (line.Contains("edge:")) //Connect nodes
                {
                    line = tr.ReadLine(); //Read color settings of edge
                    line = tr.ReadLine(); //Source
                    //Read source Basic Block
                    int titleIndex = line.IndexOf('\"') + 1;
                    line = line.Substring(titleIndex);
                    titleIndex = line.IndexOf('\"');
                    line = line.Substring(0, titleIndex);
                    GraphNode source = null;
                    if (basicBlockDictionary.ContainsKey(line)) //Check if label exsists
                        source = basicBlockDictionary[line];
                    else
                        throw new KeyNotFoundException();
                    //Read Target Basic Block
                    line = tr.ReadLine(); //Target
                    titleIndex = line.IndexOf('\"') + 1;
                    line = line.Substring(titleIndex);
                    titleIndex = line.IndexOf('\"');
                    line = line.Substring(0, titleIndex);
                    GraphNode target;
                    if (basicBlockDictionary.ContainsKey(line))
                        target = basicBlockDictionary[line];
                    else
                        throw new KeyNotFoundException();
                    //Attach target Basic block to source basic block
                    if (source.Branch1 == null)
                        source.Branch1 = target;
                    else
                        source.Branch2 = target;
                }

            }
            tr.Close();
            GraphNode.calculateDominanceDataforCFG(root,backEdgeList); //Calculate dominance for tree
            code.IntermidiatCodeBasicBlocks = intermidiatCodeInBasicBlocks;
            code.CfgBackEdgesList = backEdgeList;
            code.Root = root;

            return code;
        }