public static List <Node> Blocks(string Code, int LineNumber) { List <Node> nodesCDG = new List <Node>(); Node block = new Node(); nodesCDG = MControlDependencyGraph(Code); //Create Dependency Graph List <Dom> doms = new List <Dom>(); doms = MDomBasicBlock(Code); //Create Doms Collection Dictionary <int, Reach> reachs = MReach(Code); //Create reachs //Find Basic block foreach (var node in nodesCDG) { if (node.statements != null && node.statements.Exists(x => x.NumberLine == LineNumber)) { block = node; break; } } //Create Blocks Collection List <Node> Blocks = new List <Node>(); foreach (var node in nodesCDG) { if (node.basicBlock.Kind == BasicBlockKind.Entry || node.basicBlock.Kind == BasicBlockKind.Exit) { continue; } Dom dom = doms.Where(x => x.Id == node.id).Single(); Reach reach = reachs.Values.Where(x => x.id == node.id).Single(); if (dom.Doms.Exists(x => x.id == block.id) && reach.ReachsId.Exists(x => x == block.id)) { Blocks.Add(node); } } return(Blocks); }
public static Dictionary <int, Reach> MReach(string code) { Dictionary <int, Node> nodes = MControlFlowGraphWithouthEdgeBack(code); Queue queue = new Queue(); //Create queue Dictionary <int, Reach> setReach = new Dictionary <int, Reach>(); //Create set of reach foreach (var item in nodes) { Reach reach = new Reach(); reach.ReachsId = new List <int>(); reach.Reachs = new List <BasicBlock>(); reach.id = item.Value.id; queue.Enqueue(item.Value.id); while (queue.Count > 0) //Queue is empty? { string basicBlockId = queue.Dequeue().ToString(); //Add Id reach.ReachsId.Add(Int32.Parse(basicBlockId)); Node node = new Node(); node = nodes.First(x => x.Key == int.Parse(basicBlockId)).Value; //Add Child to queue foreach (var child in node.childList) { if (reach.ReachsId.Contains(child) || queue.Contains(child)) // has child in queue or reachsId? { continue; } else { queue.Enqueue(child); } } //Add basicBlock reach.Reachs.Add(node.basicBlock); } setReach.Add(item.Value.id, reach); } return(setReach); }