public void naturalCycleTest() { var root = Parser.ParseString(Samples.SampleProgramText.sample1); var code = ProgramTreeToLinear.Build(root); var blocks = LinearToBaseBlock.Build(code); var cfg = ListBlocksToCFG.Build(blocks); cfg.ShowCompact = true; Console.WriteLine(cfg); var ncg = new NaturalCycleGraph(cfg); var res = ncg.findBetween(6, 4); res.Sort(); var expected = new List <int>() { 4, 6 }; CollectionAssert.AreEqual(res, expected); var res1 = ncg.findBetween(13, 8); res1.Sort(); Console.Write("Cycle btw 13, 8: " + string.Join(", ", res1)); var expected1 = new List <int>() { 8, 10, 11, 12, 13 }; CollectionAssert.AreEqual(res1, expected1); }
public void SimpleInterpret() { var root = Parser.ParseString("n = 666; print(n);"); var code = ProgramTreeToLinear.Build(root); var res = LinearInterpretator.Run(code); Assert.AreEqual(res[0], 666); Assert.AreEqual(res.Count, 1); }
public void LabelGotoTest() { var root = Parser.ParseString("label1 :" + "n = 1;" + "goto label1;"); var code = ProgramTreeToLinear.Build(root); foreach (var threeAddressCode in code) { Console.WriteLine(threeAddressCode); } Assert.AreEqual(code.Count, 3); }
public void DominatorTreeTest() { var root = Parser.ParseString(Samples.SampleProgramText.domSampleTrivial); var code = ProgramTreeToLinear.Build(root); var blocks = LYtest.BaseBlocks.LinearToBaseBlock.Build(code); var cfg = ListBlocksToCFG.Build(blocks); var dt = new LYtest.DominatorTree.DominatorTree(cfg); var node = dt.GetRoot(); Console.WriteLine(dt.ToString()); Console.WriteLine(cfg.ToString()); Assert.AreEqual(dt.NumberOfVertices(), 4); }
public void SsaConstructionTest() { var root = Parser.ParseString(Samples.SampleProgramText.ssaoptimizationSample2); var code = ProgramTreeToLinear.Build(root); var blocks = LinearToBaseBlock.Build(code); var cfg = new CFGraph(blocks); Console.WriteLine("###--------- Input CF Graph ---------###"); Console.WriteLine(cfg.ToString()); SsaConstruction ssa = new SsaConstruction(cfg); Console.WriteLine("###--------- Output SSA Graph ---------###"); Console.WriteLine(ssa.SsaForm.ToString()); }
public void ConstantPropagationTest() { var root = Parser.ParseString(Samples.SampleProgramText.constantPropagationSample); var code = ProgramTreeToLinear.Build(root); var blocks = LinearToBaseBlock.Build(code); ConstantPropagation constProp = new ConstantPropagation(); Console.WriteLine(blocks[0]); var resBlock = constProp.OptimizeBlock(blocks[0] as BaseBlock); string res = resBlock.ToString(); Console.WriteLine(res); }
public void RegionSequenceTest() { var root = Parser.ParseString(Samples.SampleProgramText.regionSqeuenceSample); var code = ProgramTreeToLinear.Build(root); var blocks = LinearToBaseBlock.Build(code); var cfg = ListBlocksToCFG.Build(blocks); var regSeq = new RegionSequence(cfg); var natCycles = cfg.getNaturalCyclesForBackwardEdges(); Assert.AreEqual(regSeq.Regions.Count, 24); Assert.AreEqual(regSeq.Regions.Last().Header, cfg.GetVertices().ToList()[0]); Assert.IsTrue(regSeq.Regions.Exists(r => natCycles[0][0] == r.Header)); }
public void LinearCodeTest() { var root = Parser.ParseString(Samples.SampleProgramText.sample1); var code = ProgramTreeToLinear.Build(root); var blocks = LYtest.BaseBlocks.LinearToBaseBlock.Build(code); var labels = code.Select(c => c.Label); // All elems have label Assert.IsFalse(labels.Any(c => c == null)); // All labels different Assert.AreEqual(labels.Distinct().Count(), code.Count); // All elems in blocks Assert.AreEqual(blocks.Select(b => b.Enumerate().Count()).Sum(), code.Count); // There is no empty blocks Assert.IsTrue(blocks.All(b => b.Enumerate().Any())); var isGotoOp = new Func <IThreeAddressCode, bool>( c => c.Operation == Operation.CondGoto || c.Operation == Operation.Goto); foreach (var baseBlock in blocks) { var notLast = baseBlock.Enumerate().Reverse().Skip(1); Assert.IsFalse(notLast.Any(isGotoOp)); } foreach (var baseBlock in blocks) { foreach (var threeAddressCode in baseBlock.Enumerate()) { Console.WriteLine(threeAddressCode); } Console.WriteLine(); } var gotoLables = code.Where(isGotoOp).Select(c => c.Destination.Value); var firstLabels = blocks.Select(b => b.Enumerate().First().Label.Value); foreach (var d in gotoLables) { Assert.IsTrue(firstLabels.Contains(d)); } }
public void SimpleInterpret2() { var root = Parser.ParseString("n = 1;" + "k=n;" + "z=k+1;" + "print(2);" + "print(z);" + ""); var code = ProgramTreeToLinear.Build(root); var res = LinearInterpretator.Run(code); Assert.AreEqual(res.Count, 2); Assert.AreEqual(res[0], 2); Assert.AreEqual(res[1], 2); }
public void DominatorTreeDemo() { var root = Parser.ParseString(Samples.SampleProgramText.sample2); var code = ProgramTreeToLinear.Build(root); var blocks = LYtest.BaseBlocks.LinearToBaseBlock.Build(code); var cfg = ListBlocksToCFG.Build(blocks); var dt = new LYtest.DominatorTree.DominatorTree(cfg); var node = dt.GetRoot(); cfg.ShowCompact = true; Console.WriteLine(cfg); Console.WriteLine("\nDominator tree:"); Console.WriteLine(dt.ToString()); }
public void CFGNodeSetTest() { var root = Parser.ParseString(Samples.SampleProgramText.sample1); var code = ProgramTreeToLinear.Build(root); var blocks = LYtest.BaseBlocks.LinearToBaseBlock.Build(code); var cfg = ListBlocksToCFG.Build(blocks); var nodes = cfg.graph.Vertices.Select(n => n.Value); Assert.AreEqual(nodes.Count(), blocks.Count()); foreach (var b in blocks) { Assert.IsTrue(nodes.Contains(b)); } }
public void AvailableExpressionsTest() { Console.Write("----------- Available Expressions Analyzer ---------- \n"); var root = Parser.ParseString(Samples.SampleProgramText.AvailableExprsSample); var code = ProgramTreeToLinear.Build(root); var blocks = LinearToBaseBlock.Build(code); var cfg = ListBlocksToCFG.Build(blocks); var exprsAnalizer = new AvailableExprAnalyzer(cfg); exprsAnalizer.analyze(); Assert.IsTrue(exprsAnalizer.OutBlocks[blocks[0]].Count == 1); Assert.IsTrue(exprsAnalizer.InBlocks[blocks[2]].Count == 1); Assert.IsTrue(exprsAnalizer.OutBlocks[blocks[2]].Count == 0); }
public void ReachingDefsIterTest() { var root = Parser.ParseString(Samples.SampleProgramText.sample2); var code = ProgramTreeToLinear.Build(root); var blocks = LYtest.BaseBlocks.LinearToBaseBlock.Build(code); var cfg = ListBlocksToCFG.Build(blocks); var defs = new ReachingDefsIterAlg(cfg); cfg.InfoFunc = node => { var outs = string.Join(", ", defs.Out[node]); var ins = node.ParentsNodes.Count > 0 ? string.Join(", ", defs.In[node]) : ""; return($" In: {ins} \n\t\t\tOut: {outs}"); }; Console.WriteLine(cfg); }
public void CFGTest() { var tree = Parser.ParseString(Samples.SampleProgramText.sample1); var code = ProgramTreeToLinear.Build(tree); var blocks = LYtest.BaseBlocks.LinearToBaseBlock.Build(code); var cfg = ListBlocksToCFG.Build(blocks); Assert.AreEqual(cfg.GetRoot().ParentsNodes.Count, 0); Assert.AreEqual(cfg.Blocks.Count, 15); Assert.IsNotNull(cfg.GetRoot().directChild); Assert.IsNotNull(cfg.GetRoot().gotoNode); cfg.Blocks.ForEach(Console.WriteLine); }
public void GenKillTest() { var root = Parser.ParseString(Samples.SampleProgramText.sample2); var code = ProgramTreeToLinear.Build(root); var blocks = LYtest.BaseBlocks.LinearToBaseBlock.Build(code); blocks.ForEach(Console.WriteLine); var genKill = new GenKillBuilder(blocks); Assert.AreEqual(genKill.Gen[blocks[0]].Count, 3); Assert.AreEqual(genKill.Kill[blocks[0]].Count, 4); Assert.AreEqual(genKill.Gen[blocks[6]].Count, 1); Assert.AreEqual(genKill.Kill[blocks[6]].Count, 2); }
public void SimpleInterpret3() { var root = Parser.ParseString("n = 1;" + "k=n;" + "z=k+1;" + "print(z);" + "t = 2*3;" + "if (z > 0) {" + "for x = 2..(z+2) {" + "t = t + 1; }" + "}" + "print (t);"); var code = ProgramTreeToLinear.Build(root); var res = LinearInterpretator.Run(code); Assert.AreEqual(res.Count, 2); Assert.AreEqual(res[0], 2); Assert.AreEqual(res[1], 9); }
public void SsaCopyPropagationTest() { var root = Parser.ParseString(Samples.SampleProgramText.ssaoptimizationSample3); var code = ProgramTreeToLinear.Build(root); var blocks = LinearToBaseBlock.Build(code); var cfg = new CFGraph(blocks); Console.WriteLine("###--------- Input CF Graph ---------###"); Console.WriteLine(cfg.ToString()); SsaConstruction ssa = new SsaConstruction(cfg); CFGraph ssaGraph = ssa.SsaForm; Console.WriteLine("###--------- Constructed SSA Graph ---------###"); Console.WriteLine(ssaGraph.ToString()); SsaCopyPropagation ssaCopyPropagation = new SsaCopyPropagation(ssaGraph); ssaCopyPropagation.Launch(); Console.WriteLine("###--------- Output SSA Graph after copy propagation ---------###"); Console.WriteLine(ssaCopyPropagation.OptimizedSsaGraph); }
public void ActiveVarsIterTest() { var root = Parser.ParseString(Samples.SampleProgramText.sample3); var code = ProgramTreeToLinear.Build(root); var blocks = LYtest.BaseBlocks.LinearToBaseBlock.Build(code); var cfg = ListBlocksToCFG.Build(blocks); var DefUse = new ActiveVarsIterAlg(cfg); foreach (var block in cfg.graph.Vertices) { Console.Write(block); foreach (var labelValue in DefUse.In[block]) { Console.Write(labelValue); Console.Write(", "); } Console.WriteLine(); Console.WriteLine("-----------------------"); } }
public void CFGTest() { var root = Parser.ParseString(Samples.SampleProgramText.sample1); var code = ProgramTreeToLinear.Build(root); var blocks = LYtest.BaseBlocks.LinearToBaseBlock.Build(code); var cfg = ListBlocksToCFG.Build(blocks); Assert.AreEqual(cfg.GetRoot().ParentsNodes.Count, 0); var cfgview = new CFGLookup(cfg.GetRoot()); while (cfgview.MoveDirect()) { } Assert.AreEqual(cfgview.PathLen, 14); while (cfgview.MoveBack()) { } Assert.AreEqual(cfgview.Current, cfg.GetRoot()); }
public void allRetreatingEdgesAreBackwardsTest() { var root = Parser.ParseString(Samples.SampleProgramText.sample1); var code = ProgramTreeToLinear.Build(root); var blocks = LinearToBaseBlock.Build(code); var cfg = ListBlocksToCFG.Build(blocks); cfg.ShowCompact = true; Console.WriteLine(cfg); Console.WriteLine("allRetreatingEdgesAreBackwards: " + cfg.allRetreatingEdgesAreBackwards()); Assert.AreEqual(cfg.allRetreatingEdgesAreBackwards(), true); root = Parser.ParseString(Samples.SampleProgramText.veryStrangeCode); code = ProgramTreeToLinear.Build(root); blocks = LinearToBaseBlock.Build(code); cfg = ListBlocksToCFG.Build(blocks); cfg.ShowCompact = true; Console.WriteLine(cfg); Console.WriteLine("allRetreatingEdgesAreBackwards: " + cfg.allRetreatingEdgesAreBackwards()); Assert.AreEqual(cfg.allRetreatingEdgesAreBackwards(), false); }
public void AvailableExpressionsTest() { Console.Write("----------- Available Expressions Analyzer ---------- \n"); var root = Parser.ParseString(Samples.SampleProgramText.AvailableExprsSample); var code = ProgramTreeToLinear.Build(root); var blocks = LYtest.BaseBlocks.LinearToBaseBlock.Build(code); var cfg = ListBlocksToCFG.Build(blocks); var exprsAnalizer = new AvailableExprAnalyzer(cfg); exprsAnalizer.analyze(); foreach (var block in blocks) { Console.Write("Block: " + block); Console.Write("\n"); Console.Write("IN: \t"); foreach (var expr in exprsAnalizer.InBlocks[block]) { Console.Write(expr); Console.Write(", "); } Console.Write("\n"); Console.Write("Out: \t"); foreach (var expr in exprsAnalizer.OutBlocks[block]) { Console.Write(expr); Console.Write(", "); } Console.Write("\n"); Console.WriteLine("-----------------------"); } }
public void GenKillTest() { var root = Parser.ParseString(Samples.SampleProgramText.sample2); var code = ProgramTreeToLinear.Build(root); var blocks = LYtest.BaseBlocks.LinearToBaseBlock.Build(code); var genKill = new GenKillBuilder(blocks); foreach (var b in blocks) { Console.WriteLine(b); Console.Write("Gen: "); Console.WriteLine(string.Join(",", genKill.Gen[b])); Console.Write("Kill: "); Console.WriteLine(string.Join(",", genKill.Kill[b])); Console.WriteLine("-------------------------"); } Assert.AreEqual(genKill.Gen[blocks[0]].Count, 3); Assert.AreEqual(genKill.Kill[blocks[0]].Count, 4); Assert.AreEqual(genKill.Gen[blocks[6]].Count, 1); Assert.AreEqual(genKill.Kill[blocks[6]].Count, 2); }
public void SymbolicAnalysisTest() { var root = Parser.ParseString(Samples.SampleProgramText.symbolicAnalysisSample); var code = ProgramTreeToLinear.Build(root); var blocks = LinearToBaseBlock.Build(code); SymbolicAnalysis syman = new SymbolicAnalysis(); var map = syman.createMap(blocks[0] as BaseBlock); Console.WriteLine("baseblock1"); Console.WriteLine("------"); foreach (var b in code) { Console.Write(b.ToString() + " "); Console.WriteLine(b.Operation.ToString()); } Console.WriteLine("------"); root = Parser.ParseString(Samples.SampleProgramText.symbolicAnalysisSample2); code = ProgramTreeToLinear.Build(root); blocks = LinearToBaseBlock.Build(code); var map2 = syman.createMap(blocks[0] as BaseBlock); Console.WriteLine("baseblock2"); Console.WriteLine("------"); foreach (var b in code) { Console.Write(b.ToString() + " "); Console.WriteLine(b.Operation.ToString()); } Console.WriteLine("------"); Console.WriteLine("symbolic map for block1"); Console.WriteLine("------"); foreach (var m in map.variableTable) { if (m.Key != null) { Console.Write(m.Key.ToString() + " "); } if (m.Value.type != null) { Console.WriteLine(m.Value.type.ToString()); } Console.Write("Constants: "); if (m.Value.value.constants != null) { foreach (var v in m.Value.value.constants) { Console.Write(v.ToString() + " "); } } Console.Write("Variables: "); if (m.Value.value.variables != null) { foreach (var v in m.Value.value.variables) { Console.Write(v.ToString() + " "); } } Console.WriteLine(); } Console.WriteLine("------"); Console.WriteLine("symbolic map for block2"); Console.WriteLine("------"); foreach (var m in map2.variableTable) { if (m.Key != null) { Console.Write(m.Key.ToString() + " "); } if (m.Value.type != null) { Console.WriteLine(m.Value.type.ToString()); } Console.Write("Constants: "); if (m.Value.value.constants != null) { foreach (var v in m.Value.value.constants) { Console.Write(v.ToString() + " "); } } Console.Write("Variables: "); if (m.Value.value.variables != null) { foreach (var v in m.Value.value.variables) { Console.Write(v.ToString() + " "); } } Console.WriteLine(); } Console.WriteLine("------"); Console.WriteLine("Composition of map1 and map2 on x = 4 + 3*y + z; instruction"); var compmap = syman.Composition(code[code.Count - 1], map2, map); Console.WriteLine("------"); foreach (var m in compmap.variableTable) { if (m.Key != null) { Console.Write(m.Key.ToString() + " "); } if (m.Value.type != null) { Console.WriteLine(m.Value.type.ToString()); } Console.Write("Constants: "); if (m.Value.value.constants != null) { foreach (var v in m.Value.value.constants) { Console.Write(v.ToString() + " "); } } Console.Write("Variables: "); if (m.Value.value.variables != null) { foreach (var v in m.Value.value.variables) { Console.Write(v.ToString() + " "); } } Console.WriteLine(); } Console.WriteLine("------"); }