/// <summary> /// Empty constructor /// </summary> public IntermidiatCodeAsBasicBlocksCFG() { root = null; cfgBackEdgesList = null; }
///////////////////////////////////////////////////////// /////////////////// 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 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; }