Example #1
0
        public ControlFlowGraph(List <BaseBlock> baseBlocks)
        {
            Next = new Dictionary <int, List <int> >();
            Prev = new Dictionary <int, List <int> >();

            StartBlockId = baseBlocks[0].StartLabel;

            _bblocks = baseBlocks;

            _baseBlockByStart = new Dictionary <int, BaseBlock>();
            _genByStart       = new Dictionary <int, GenSet>();
            _killByStart      = new Dictionary <int, KillSet>();
            _useByStart       = new Dictionary <int, VarsSet>();
            _defByStart       = new Dictionary <int, VarsSet>();
            _genExprByStart   = new Dictionary <int, ExprSet>();

            foreach (var bblock in baseBlocks)
            {
                _baseBlockByStart[bblock.StartLabel] = bblock;
                Prev[bblock.StartLabel] = new List <int>();
                Next[bblock.StartLabel] = new List <int>();
            }



            for (int i = 0; i < baseBlocks.Count; ++i)
            {
                var bblock = baseBlocks[i];

                if (ThreeAddrOpType.IsGoto(bblock.LastLine.OpType))
                {
                    Next[bblock.StartLabel].Add(int.Parse(bblock.LastLine.RightOp));
                    if (bblock.LastLine.OpType == ThreeAddrOpType.Goto)
                    {
                        continue;
                    }
                }

                if (i + 1 < baseBlocks.Count)
                {
                    Next[bblock.StartLabel].Add(baseBlocks[i + 1].StartLabel);
                }
            }

            foreach (var vTo in Next)
            {
                foreach (var to in vTo.Value)
                {
                    Prev[to].Add(vTo.Key);
                }
            }


            GenerateGenAndKillSets();
            GenerateUseAndDefSets();
            GenerateGenExprSets();
        }
Example #2
0
        public List <BaseBlock> GetAliveBlocks()
        {
            Queue <int>   Q       = new Queue <int>(); Q.Enqueue(StartBlockId);
            HashSet <int> visited = new HashSet <int>();

            visited.Add(StartBlockId);
            while (Q.Count > 0)
            {
                var cur = Q.Dequeue();
                foreach (var to in Next[cur])
                {
                    if (!visited.Contains(to))
                    {
                        visited.Add(to);
                        Q.Enqueue(to);
                    }
                }
            }

            //Propagate empty blocks
            foreach (var k in visited)
            {
                var bblock = _baseBlockByStart[k];
                if (ThreeAddrOpType.IsGoto(bblock.LastLine.OpType))
                {
                    int to   = int.Parse(bblock.LastLine.RightOp);
                    var tobl = _baseBlockByStart[to];
                    if (tobl.Code[0].OpType == ThreeAddrOpType.Goto)
                    {
                        bblock.LastLine.RightOp = tobl.Code[0].RightOp;
                    }
                    else if (tobl.Code.Count == 1 && tobl.Code[0].OpType == ThreeAddrOpType.Nop)
                    {
                        if (Next[to].Count == 1)
                        {
                            bblock.LastLine.RightOp = Next[to][0].ToString();
                        }
                    }
                }
            }


            return(visited.OrderBy(x => x)
                   .Select(x => _baseBlockByStart[x]).ToList());
        }