コード例 #1
0
        public (List <LabelSet>, List <LabelSet>) GenerateInputOutputReachingDefs()
        {
            var In  = new List <LabelSet>();
            var Out = new List <LabelSet>();

            var startToId = GetStartToId(_bblocks);

            for (int i = 0; i < _bblocks.Count(); ++i)
            {
                In.Add(new LabelSet());
                Out.Add(new LabelSet());
            }

            bool change = true;

            while (change)
            {
                change = false;

                for (int i = 0; i < _bblocks.Count(); ++i)
                {
                    var st = _bblocks[i].StartLabel;
                    In[i] = new LabelSet(Prev[st].SelectMany(p => Out[startToId[p]]));
                    int sz = Out[i].Count;

                    Out[i] = ReachingDefinitions.TransferByGenAndKill(In[i], _genByStart[st], _killByStart[st]);

                    change |= sz != Out[i].Count;
                }
            }

            return(In, Out);
        }
コード例 #2
0
        private static (List <BasicBlock> basicBlocks, InOutInfo inOutInfo) GenGraphAndGetInOutInfo(string program)
        {
            var blocks    = GenBlocks(program);
            var cfg       = new ControlFlowGraph(blocks);
            var inOutInfo = new ReachingDefinitions().Execute(cfg);

            return(blocks, inOutInfo);
        }
コード例 #3
0
        private (List <BasicBlock> basicBlocks, InOutInfo inOutInfo) GenGraphAndGetInOutInfo(string program)
        {
            var TAC       = GenTAC(program);
            var blocks    = BasicBlockLeader.DivideLeaderToLeader(TAC);
            var cfg       = new ControlFlowGraph(blocks);
            var inOutInfo = new ReachingDefinitions().Execute(cfg);

            return(blocks, inOutInfo);
        }
コード例 #4
0
        private void GenerateGenAndKillSets()
        {
            var bblocks = _baseBlockByStart.Select(kp => kp.Value).ToList();

            var(gen, kill) = ReachingDefinitions.GetGenAndKillSets(bblocks);

            for (int i = 0; i < bblocks.Count(); ++i)
            {
                _genByStart[bblocks[i].StartLabel]  = gen[i];
                _killByStart[bblocks[i].StartLabel] = kill[i];
            }
        }
コード例 #5
0
        public void ReachingDefinitionIterativeTest()
        {
            var TAC = GenTAC(@"
var a,b,c;

input (b);
a = b + 1;
if a < c
	c = b - a;
else
	c = b + a;
print (c);"
                             );

            var cfg = new ControlFlowGraph(BasicBlockLeader.DivideLeaderToLeader(TAC));
            var reachingDefinitions    = new ReachingDefinitions();
            var resReachingDefinitions = reachingDefinitions.Execute(cfg);
            var In     = new List <Instruction>();
            var Out    = new List <Instruction>();
            var actual = new List <(List <Instruction> IN, List <Instruction> OUT)>();

            foreach (var x in resReachingDefinitions)
            {
                foreach (var y in x.Value.In)
                {
                    In.Add(y);
                }

                foreach (var y in x.Value.Out)
                {
                    Out.Add(y);
                }
                actual.Add((new List <Instruction>(In), new List <Instruction>(Out)));
                In.Clear(); Out.Clear();
            }

            var expected =
                new List <(List <Instruction> IN, List <Instruction> OUT)>()
            {
                (new List <Instruction>()
                {
                }, new List <Instruction>()
                {
                }),
                (new List <Instruction>()
                {
                }, new List <Instruction>()
                {
                    TAC[2], TAC[0]
                }),
                (new List <Instruction>()
                {
                    TAC[2], TAC[0]
                }, new List <Instruction>()
                {
                    TAC[6], TAC[2], TAC[0]
                }),
                (new List <Instruction>()
                {
                    TAC[2], TAC[0]
                }, new List <Instruction>()
                {
                    TAC[9], TAC[2], TAC[0]
                }),
                (new List <Instruction>()
                {
                    TAC[6], TAC[2], TAC[0], TAC[9]
                }, new List <Instruction>()
                {
                    TAC[6], TAC[2], TAC[0], TAC[9]
                }),
                (new List <Instruction>()
                {
                    TAC[6], TAC[2], TAC[0], TAC[9]
                }, new List <Instruction>()
                {
                    TAC[6], TAC[2], TAC[0], TAC[9]
                })
            };

            AssertSet(expected, actual);
        }
コード例 #6
0
        public void ReachingDefinitions()
        {
            var TAC = GenTAC(@"
var a, b, c;

input(b);
a = b + 1;
if a < c
    c = b - a;
else
    c = b + a;
print(c);
");

            var cfg = GenCFG(TAC);
            var resReachingDefinitions = new ReachingDefinitions().Execute(cfg);
            var actual = cfg.GetCurrentBasicBlocks()
                         .Select(z => resReachingDefinitions[z])
                         .Select(p => ((IEnumerable <Instruction>)p.In, (IEnumerable <Instruction>)p.Out))
                         .ToList();

            var expected =
                new List <(IEnumerable <Instruction>, IEnumerable <Instruction>)>()
            {
                (new List <Instruction>()
                {
                }, new List <Instruction>()
                {
                }),
                (new List <Instruction>()
                {
                }, new List <Instruction>()
                {
                    TAC[2], TAC[0]
                }),
                (new List <Instruction>()
                {
                    TAC[2], TAC[0]
                }, new List <Instruction>()
                {
                    TAC[6], TAC[2], TAC[0]
                }),
                (new List <Instruction>()
                {
                    TAC[2], TAC[0]
                }, new List <Instruction>()
                {
                    TAC[9], TAC[2], TAC[0]
                }),
                (new List <Instruction>()
                {
                    TAC[6], TAC[2], TAC[0], TAC[9]
                }, new List <Instruction>()
                {
                    TAC[6], TAC[2], TAC[0], TAC[9]
                }),
                (new List <Instruction>()
                {
                    TAC[6], TAC[2], TAC[0], TAC[9]
                }, new List <Instruction>()
                {
                    TAC[6], TAC[2], TAC[0], TAC[9]
                })
            };

            AssertSet(expected, actual);
        }