Ejemplo n.º 1
0
        internal static string GetASTWithOpt(Parser parser, List <int> lstCheck)
        {
            var b = parser.Parse();

            if (!b)
            {
                return("Error. AST not build");
            }

            var fillParents = new FillParentsVisitor();

            parser.root.Visit(fillParents);
            var listOpt = new List <ChangeVisitor>();

            if (lstCheck.Count > 0)
            {
                foreach (var n in lstCheck)
                {
                    listOpt.Add(ASTOptimizations[n]);
                }
                ASTOptimizer.Optimize(parser, listOpt);
            }

            var pp = new PrettyPrintVisitor();

            parser.root.Visit(pp);
            return(pp.Text);
        }
Ejemplo n.º 2
0
        public void Simple()
        {
            var sourceCode =
                @"
{
a = 3;
a = 5;
b = 3;
c = (b + 1) * 2;
a = 4;
}
";
            Scanner scanner = new Scanner();

            scanner.SetSource(sourceCode, 0);

            Parser parser = new Parser(scanner);

            parser.Parse();

            var parentFiller = new FillParentsVisitor();

            parser.root.Visit(parentFiller);
            var blocks = AllTacOptimization.Optimize(parser);
            var actual = blocks.blocks.Select(b => b.ToString().Trim()).ToList();

            var expected = new List <string>()
            {
                "b = 3\n" +
                "c = 8\n" +
                "a = 4"
            };

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 3
0
        protected ControlFlowGraph GenerateCFG(string sourceCode)
        {
            Scanner scanner = new Scanner();

            scanner.SetSource(sourceCode, 0);

            Parser parser = new Parser(scanner);

            parser.Parse();

            var parentFiller = new FillParentsVisitor();

            parser.root.Visit(parentFiller);

            var TACGenerator = new TACGenerationVisitor();

            parser.root.Visit(TACGenerator);

            var blocks = new TACBaseBlocks(TACGenerator.Instructions);

            blocks.GenBaseBlocks();
            var cfg = new ControlFlowGraph(blocks.blocks);

            return(cfg);
        }
Ejemplo n.º 4
0
        public void SimpleExample2()
        {
            var Text = (
                @"
{
  x = 7 - 8 * 0 + 3;
}
");
            Scanner scanner = new Scanner();

            scanner.SetSource(Text, 0);
            Parser parser = new Parser(scanner);

            parser.Parse();
            var parentFiller = new FillParentsVisitor();

            parser.root.Visit(parentFiller);

            var optimizer = new ConstantFoldingVisitor();

            parser.root.Visit(optimizer);

            var TACGenerator = new TACGenerationVisitor();

            parser.root.Visit(TACGenerator);
            var TAC = TACGenerator.TAC;

            var expected = new List <string>()
            {
                "x = 10"
            };
            var actual = TAC.Instructions.Select(instruction => instruction.ToString().Trim());

            CollectionAssert.AreEqual(expected, actual);
        }
Ejemplo n.º 5
0
        public void SimpleTest2()
        {
            var Text =
                @"
{
  if (a > b) 
  {
    a = a;
  }
}
";
            Scanner scanner = new Scanner();

            scanner.SetSource(Text, 0);
            Parser parser = new Parser(scanner);

            parser.Parse();
            var parentFiller = new FillParentsVisitor();

            parser.root.Visit(parentFiller);

            var ChangeVisitorsOptimization = new List <ChangeVisitor>
            {
                new SameAssignmentOptVisitor(),
                new NullIfElseOptVisitor(),
                new RemoveEmptyStatementVisitor(),
            };


            int countOptimization = 0;

            while (countOptimization < ChangeVisitorsOptimization.Count)
            {
                parser.root.Visit(ChangeVisitorsOptimization[countOptimization]);
                if (ChangeVisitorsOptimization[countOptimization].IsChanged)
                {
                    ChangeVisitorsOptimization[countOptimization].IsChanged = false;
                    countOptimization = 0;
                }
                else
                {
                    countOptimization++;
                }
            }


            var prettyPrinter = new PrettyPrinterVisitor();

            parser.root.Visit(prettyPrinter);

            var TACGenerator = new TACGenerationVisitor();

            parser.root.Visit(TACGenerator);
            var TAC = TACGenerator.TAC;

            var expected = new List <string>();
            var actual   = TAC.Instructions.Select(instruction => instruction.ToString().Trim());

            CollectionAssert.AreEqual(expected, actual);
        }
Ejemplo n.º 6
0
        protected ThreeAddressCode GenerateTAC(string sourceCode)
        {
            Scanner scanner = new Scanner();

            scanner.SetSource(sourceCode, 0);

            Parser parser = new Parser(scanner);

            parser.Parse();

            var parentFiller = new FillParentsVisitor();

            parser.root.Visit(parentFiller);

            var TACGenerator = new TACGenerationVisitor();

            parser.root.Visit(TACGenerator);
            return(TACGenerator.TAC);

            /*
             * var TACBlocks = new TACBaseBlocks(TACGenerator.Instructions);
             * TACBlocks.GenBaseBlocks();
             * var cfg = new ControlFlowGraph(TACBlocks.blocks);
             */
        }
Ejemplo n.º 7
0
        public void IfAterIf()
        {
            var Text =
                @"
{
  if a == a
  {
    b = a;
  }

  if b == b
  {
    c = b;
  }
  else
  {
    c = a;
  }
}
";
            Scanner scanner = new Scanner();

            scanner.SetSource(Text, 0);
            Parser parser = new Parser(scanner);

            parser.Parse();
            var parentFiller = new FillParentsVisitor();

            parser.root.Visit(parentFiller);

            var trueOpt = new TrueConditionOptVisitor();

            parser.root.Visit(trueOpt);

            var prettyPrinter = new PrettyPrinterVisitor();

            parser.root.Visit(prettyPrinter);

            var TAC = GenerateTAC(prettyPrinter.FormattedProgram);

            var expected = new List <string>()
            {
                "if True goto #L0",
                "goto #L1",
                "#L0",
                "b = a",
                "#L1",
                "if True goto #L2",
                "c = a",
                "goto #L3",
                "#L2",
                "c = b",
                "#L3"
            };
            var actual = TAC.Instructions.Select(instruction => instruction.ToString().Trim());

            CollectionAssert.AreEqual(expected, actual);
        }
Ejemplo n.º 8
0
        public void Complex()
        {
            var sourceCode =
                @"
{
a = 2;
b = 5;
c = 3;
if a - 1 > b + 2
{
i = 1;
j = i * i + 0;
}
else 
{
g = c + 3;
}
d = c;
b = g;
}
";
            Scanner scanner = new Scanner();

            scanner.SetSource(sourceCode, 0);

            Parser parser = new Parser(scanner);

            parser.Parse();

            var parentFiller = new FillParentsVisitor();

            parser.root.Visit(parentFiller);
            var blocks = AllTacOptimization.Optimize(parser);
            var actual = blocks.blocks.Select(b => b.ToString().Trim()).ToList();

            var expected = new List <string>()
            {
                "a = 2\n" +
                "b = 5\n" +
                "c = 3\n" +
                "#t2 = False\n" +
                "if #t2 goto #L0",

                "g = 6\n" +
                "goto #L1",

                "#L0\n" +
                "i = 1\n" +
                "j = 1",

                "#L1\n" +
                "d = 3\n" +
                "b = 6"
            };

            Assert.AreEqual(expected, actual);
        }
        public void AllDividedTestCasesTest()
        {
            var     Text = (@"{
a = 1;
b = 1;
c = 0;
c = c/c/1;
if 1*a + 1*b
{
c = a * b * 1;
}
q = a/1 + 5;
q = c + 0;
}");
            Scanner scanner = new Scanner();

            scanner.SetSource(Text, 0);
            Parser parser = new Parser(scanner);

            parser.Parse();
            var parentFiller = new FillParentsVisitor();

            parser.root.Visit(parentFiller);

            var ProdDiv = new AlgebraicIdentityProdDiv1Visitor();
            var Sum     = new AlgebraicIdentitySum0Visitor();

            ProdDiv.Visit(parser.root);
            Sum.Visit(parser.root);

            var prettyPrinter = new PrettyPrinterVisitor();

            parser.root.Visit(prettyPrinter);
            var TAC = GenerateTAC(prettyPrinter.FormattedProgram);

            var expected = new List <string>()
            {
                "a = 1",
                "b = 1",
                "c = 0",
                "c = c / c",
                "#t0 = a + b",
                "if #t0 goto #L0",
                "goto #L1",
                "#L0",
                "c = a * b",
                "#L1",
                "q = a + 5",
                "q = c"
            };
            var actual = TAC.Instructions.Select(instruction => instruction.ToString().Trim());

            CollectionAssert.AreEqual(expected, actual);
        }
Ejemplo n.º 10
0
        public void OptimizationPipeline2()
        {
            var Text =
                @"
{
  x = 13;
  b = 14;
  if (x - x + 2 < a - a + 1 + 0)
  {
    x = x * (a - a);
    b = b;
  }
  if (true) 
  {
    a = 1;
  } 
  else 
  {
    a = 1;
  }
}
";
            Scanner scanner = new Scanner();

            scanner.SetSource(Text, 0);
            Parser parser = new Parser(scanner);

            parser.Parse();
            var parentFiller = new FillParentsVisitor();

            parser.root.Visit(parentFiller);

            AllVisitorsOptimization.Optimization(parser);

            var prettyPrinter = new PrettyPrinterVisitor();

            parser.root.Visit(prettyPrinter);

            var TACGenerator = new TACGenerationVisitor();

            parser.root.Visit(TACGenerator);
            var TAC = TACGenerator.TAC;

            var expected = new List <string>()
            {
                "x = 13",
                "b = 14",
                "a = 1"
            };
            var actual = TAC.Instructions.Select(instruction => instruction.ToString().Trim());

            CollectionAssert.AreEqual(expected, actual);
        }
Ejemplo n.º 11
0
        public void ExprOptimization()
        {
            var Text =
                @"
{
  x = 13;
  b = 14;
  while (x < b)
  {
    x = (x * (a - a)) * 1;
    b = b;
  }
}
";
            Scanner scanner = new Scanner();

            scanner.SetSource(Text, 0);
            Parser parser = new Parser(scanner);

            parser.Parse();
            var parentFiller = new FillParentsVisitor();

            parser.root.Visit(parentFiller);

            AllVisitorsOptimization.Optimization(parser);

            var prettyPrinter = new PrettyPrinterVisitor();

            parser.root.Visit(prettyPrinter);

            var TACGenerator = new TACGenerationVisitor();

            parser.root.Visit(TACGenerator);
            var TAC = TACGenerator.TAC;

            var expected = new List <string>()
            {
                "x = 13",
                "b = 14",
                "#L0",
                "#t0 = x < b",
                "if #t0 goto #L1",
                "goto #L2",
                "#L1",
                "x = 0",
                "goto #L0",
                "#L2"
            };
            var actual = TAC.Instructions.Select(instruction => instruction.ToString().Trim());

            CollectionAssert.AreEqual(expected, actual);
        }
Ejemplo n.º 12
0
        public void IfInsideWhile()
        {
            var Text =
                @"
{
  while (true)
  {
    if true
    {
      b = a;
    }
  }
}
";
            Scanner scanner = new Scanner();

            scanner.SetSource(Text, 0);
            Parser parser = new Parser(scanner);

            parser.Parse();
            var parentFiller = new FillParentsVisitor();

            parser.root.Visit(parentFiller);

            var trueIfOpt = new TrueIfOptVisitor();

            parser.root.Visit(trueIfOpt);

            var prettyPrinter = new PrettyPrinterVisitor();

            parser.root.Visit(prettyPrinter);

            var TACGenerator = new TACGenerationVisitor();

            parser.root.Visit(TACGenerator);
            var TAC = TACGenerator.TAC;

            var expected = new List <string>()
            {
                "#L0",
                "if True goto #L1",
                "goto #L2",
                "#L1",
                "b = a",
                "goto #L0",
                "#L2"
            };
            var actual = TAC.Instructions.Select(instruction => instruction.ToString().Trim());

            CollectionAssert.AreEqual(expected, actual);
        }
Ejemplo n.º 13
0
        protected Parser BuildAST(string sourceCode)
        {
            SymbolTable.vars.Clear();
            var scanner = new Scanner();

            scanner.SetSource(sourceCode, 0);
            var parser = new Parser(scanner);

            parser.Parse();
            var fillParents = new FillParentsVisitor();

            parser.root.Visit(fillParents);
            return(parser);
        }
        public void TrueIfAndAlgebraicIdentities()
        {
            var Text =
                @"
{
  if true
  {
    x = x / 1;
    x = 1 * x;
  }
}
";
            Scanner scanner = new Scanner();

            scanner.SetSource(Text, 0);
            Parser parser = new Parser(scanner);

            parser.Parse();
            var parentFiller = new FillParentsVisitor();

            parser.root.Visit(parentFiller);

            var trueIfOpt = new TrueIfOptVisitor();

            parser.root.Visit(trueIfOpt);

            var prettyPrinter = new PrettyPrinterVisitor();

            parser.root.Visit(prettyPrinter);

            var TACGenerator = new TACGenerationVisitor();

            parser.root.Visit(TACGenerator);
            var TAC = TACGenerator.TAC;

            var AIOptimizer = new AlgebraicIdentitiesOptimizer(TAC);

            AIOptimizer.Run();

            var expected = new List <string>()
            {
                "x = x",
                "x = x"
            };

            var actual = AIOptimizer.TAC.Instructions.Select(instruction => instruction.ToString().Trim());

            CollectionAssert.AreEqual(expected, actual);
        }
Ejemplo n.º 15
0
        public void OptimizationPipeline5()
        {
            var Text =
                @"
{
  c = 3;
  if (2 > 1) 
  {
    a = a;
  }
  else 
  {
    b = b;
    if (3 > 2) 
    {
      a = a;
    }
  }
}
";
            Scanner scanner = new Scanner();

            scanner.SetSource(Text, 0);
            Parser parser = new Parser(scanner);

            parser.Parse();
            var parentFiller = new FillParentsVisitor();

            parser.root.Visit(parentFiller);

            AllVisitorsOptimization.Optimization(parser);

            var prettyPrinter = new PrettyPrinterVisitor();

            parser.root.Visit(prettyPrinter);

            var TACGenerator = new TACGenerationVisitor();

            parser.root.Visit(TACGenerator);
            var TAC = TACGenerator.TAC;

            var expected = new List <string>()
            {
                "c = 3"
            };
            var actual = TAC.Instructions.Select(instruction => instruction.ToString().Trim());

            CollectionAssert.AreEqual(expected, actual);
        }
Ejemplo n.º 16
0
        public void Simple2()
        {
            var sourceCode =
                @"
{
a = b + c;
d = 3 * 2 + 1;
e = (b + c) * d;
c = (b + c) * 2;
b = 2;
goto 1;
b = 3;
1: r = b + c;
}
";
            Scanner scanner = new Scanner();

            scanner.SetSource(sourceCode, 0);

            Parser parser = new Parser(scanner);

            parser.Parse();

            var parentFiller = new FillParentsVisitor();

            parser.root.Visit(parentFiller);
            var blocks = AllTacOptimization.Optimize(parser);
            var actual = blocks.blocks.Select(b => b.ToString().Trim()).ToList();

            var expected = new List <string>()
            {
                "a = b + c\n" +
                "d = 7\n" +
                "#t3 = a * 7\n" +
                "e = #t3\n" +
                "#t5 = a * 2\n" +
                "c = #t5\n" +
                "b = 2\n" +
                "goto 1",

                "b = 3",

                "1\n" +
                "r = b + c"
            };

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 17
0
        public void TrueConditionOptAndTrueIfOpt()
        {
            var Text =
                @"
{
  if a == a
  {
    b = a;
    c = a;
  }
}
";
            Scanner scanner = new Scanner();

            scanner.SetSource(Text, 0);
            Parser parser = new Parser(scanner);

            parser.Parse();
            var parentFiller = new FillParentsVisitor();

            parser.root.Visit(parentFiller);

            var trueOpt = new TrueConditionOptVisitor();

            parser.root.Visit(trueOpt);

            var trueIfOpt = new TrueIfOptVisitor();

            parser.root.Visit(trueIfOpt);

            var prettyPrinter = new PrettyPrinterVisitor();

            parser.root.Visit(prettyPrinter);

            var TACGenerator = new TACGenerationVisitor();

            parser.root.Visit(TACGenerator);
            var TAC = TACGenerator.TAC;

            var expected = new List <string>()
            {
                "b = a",
                "c = a"
            };
            var actual = TAC.Instructions.Select(instruction => instruction.ToString().Trim());

            CollectionAssert.AreEqual(expected, actual);
        }
Ejemplo n.º 18
0
        protected Parser GenerateTree(string sourceCode)
        {
            Scanner scanner = new Scanner();

            scanner.SetSource(sourceCode, 0);

            Parser parser = new Parser(scanner);

            parser.Parse();

            var parentFiller = new FillParentsVisitor();

            parser.root.Visit(parentFiller);

            return(parser);
        }
Ejemplo n.º 19
0
        public void Test2()
        {
            var Text =
                @"
{
  b = 4;
  while (false)
  {
    b = b;
  }
  a = 33;
}
";
            Scanner scanner = new Scanner();

            scanner.SetSource(Text, 0);
            Parser parser = new Parser(scanner);

            parser.Parse();
            var parentFiller = new FillParentsVisitor();

            parser.root.Visit(parentFiller);

            var whileFalse = new WhileFalseVisitor();

            parser.root.Visit(whileFalse);

            var prettyPrinter = new PrettyPrinterVisitor();

            parser.root.Visit(prettyPrinter);

            var TACGenerator = new TACGenerationVisitor();

            parser.root.Visit(TACGenerator);
            var TAC = TACGenerator.TAC;

            var expected = new List <string>()
            {
                "b = 4",
                "a = 33"
            };
            var actual = TAC.Instructions.Select(instruction => instruction.ToString().Trim());

            CollectionAssert.AreEqual(expected, actual);
        }
Ejemplo n.º 20
0
        public void WhileFalseOptimization()
        {
            var Text =
                @"
{
  x = 13;
  b = 14;
  while (7 - 7 == 3 - 1)
  {
    x = x * (a - a);
    b = b;
  }
}
";
            Scanner scanner = new Scanner();

            scanner.SetSource(Text, 0);
            Parser parser = new Parser(scanner);

            parser.Parse();
            var parentFiller = new FillParentsVisitor();

            parser.root.Visit(parentFiller);

            AllVisitorsOptimization.Optimization(parser);

            var prettyPrinter = new PrettyPrinterVisitor();

            parser.root.Visit(prettyPrinter);

            var TACGenerator = new TACGenerationVisitor();

            parser.root.Visit(TACGenerator);
            var TAC = TACGenerator.TAC;

            var expected = new List <string>()
            {
                "x = 13",
                "b = 14"
            };
            var actual = TAC.Instructions.Select(instruction => instruction.ToString().Trim());

            CollectionAssert.AreEqual(expected, actual);
        }
Ejemplo n.º 21
0
        protected List <Instruction> GenTAC(string sourceCode)
        {
            ThreeAddressCodeTmp.ResetTmpName();
            ThreeAddressCodeTmp.ResetTmpLabel();
            SymbolTable.vars.Clear();   // oh yeah, all variables are stored in a static dict :D
            var scanner = new Scanner();

            scanner.SetSource(sourceCode, 0);
            var parser = new Parser(scanner);

            parser.Parse();
            var fillParents = new FillParentsVisitor();

            parser.root.Visit(fillParents);
            var threeAddrCodeVisitor = new ThreeAddrGenVisitor();

            parser.root.Visit(threeAddrCodeVisitor);
            return(threeAddrCodeVisitor.Instructions);
        }
Ejemplo n.º 22
0
        public void WithConstantPropagationIter()
        {
            var sourceCode =
                @"
{
b = 3;
if a 
{
goto 1;
}
b = 3;
1: r = b;
}
";
            Scanner scanner = new Scanner();

            scanner.SetSource(sourceCode, 0);

            Parser parser = new Parser(scanner);

            parser.Parse();

            var parentFiller = new FillParentsVisitor();

            parser.root.Visit(parentFiller);
            var blocks = AllTacOptimization.Optimize(parser);
            var actual = blocks.blocks.Select(b => b.ToString().Trim()).ToList();

            var expected = new List <string>()
            {
                "b = 3\n" +
                "#t0 = !a\n" +
                "#t1 = !#t0\n" +
                "if #t1 goto 1",

                "b = 3",

                "1\n" +
                "r = 3"
            };

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 23
0
        public void Compile(string path)
        {
            try
            {
                string Text = File.ReadAllText(path);

                Scanner scanner = new Scanner();
                scanner.SetSource(Text, 0);

                Parser parser        = new Parser(scanner);
                var    parentVisitor = new FillParentsVisitor();

                var b = parser.Parse();
                if (!b)
                {
                    Console.WriteLine("Parsing error");
                }

                parser.root.Visit(parentVisitor);

                AllVisitorsOptimization.Optimization(parser, true);
                var blocks       = AllTacOptimization.Optimize(parser, true);
                var instructions = blocks.BlockMerging();

                Console.WriteLine("=====================Final Result=====================");
                foreach (var instruction in instructions)
                {
                    Console.WriteLine(instruction);
                }
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("File {0} doesn't exist", path);
            }
            catch (LexException e)
            {
                Console.WriteLine("Lex error. " + e.Message);
            }
            catch (SyntaxException e)
            {
                Console.WriteLine("Syntax error. " + e.Message);
            }
        }
        protected ThreeAddressCode GenerateTAC(string sourceCode)
        {
            Scanner scanner = new Scanner();

            scanner.SetSource(sourceCode, 0);

            Parser parser = new Parser(scanner);

            parser.Parse();

            var parentFiller = new FillParentsVisitor();

            parser.root.Visit(parentFiller);

            var TACGenerator = new TACGenerationVisitor();

            parser.root.Visit(TACGenerator);
            return(TACGenerator.TAC);
        }
Ejemplo n.º 25
0
        public void Test2()
        {
            var text =
                @"{
            c = 4;
            b = 5;
            a = b*0 + 0*c;
            }";
            Scanner scanner = new Scanner();

            scanner.SetSource(text, 0);
            Parser parser = new Parser(scanner);

            parser.Parse();
            var parentFiller = new FillParentsVisitor();

            parser.root.Visit(parentFiller);

            var Opt = new MultiplyOnZero();

            parser.root.Visit(Opt);

            var Opt2 = new MultiplyOnZero2();

            parser.root.Visit(Opt2);

            var prettyPrinter = new PrettyPrinterVisitor();

            parser.root.Visit(prettyPrinter);

            var TAC = GenerateTAC(prettyPrinter.FormattedProgram);

            var expected = new List <string>()
            {
                "c = 4",
                "b = 5",
                "a = 0 + 0"
            };
            var actual = TAC.Instructions.Select(instruction => instruction.ToString().Trim());

            CollectionAssert.AreEqual(expected, actual);
        }
Ejemplo n.º 26
0
        public void Test1()
        {
            var Text =
                @"{
b = 5;
a = b-b;
}
";
            Scanner scanner = new Scanner();

            scanner.SetSource(Text, 0);
            Parser parser = new Parser(scanner);

            parser.Parse();
            var parentFiller = new FillParentsVisitor();

            parser.root.Visit(parentFiller);

            var Opt = new MinusSelf();

            parser.root.Visit(Opt);

            var prettyPrinter = new PrettyPrinterVisitor();

            parser.root.Visit(prettyPrinter);

            var TAC = GenerateTAC(prettyPrinter.FormattedProgram);

            var expected = new List <string>()
            {
                "b = 5",
                "a = 0"
            };
            var actual = TAC.Instructions.Select(instruction => instruction.ToString().Trim());

            CollectionAssert.AreEqual(expected, actual);
        }
Ejemplo n.º 27
0
        public void SimpleOptimization()
        {
            var Text =
                @"
{
  b = a;
  c = a;
}
";
            Scanner scanner = new Scanner();

            scanner.SetSource(Text, 0);
            Parser parser = new Parser(scanner);

            parser.Parse();
            var parentFiller = new FillParentsVisitor();

            parser.root.Visit(parentFiller);

            AllVisitorsOptimization.Optimization(parser);

            var prettyPrinter = new PrettyPrinterVisitor();

            parser.root.Visit(prettyPrinter);

            var TAC = GenerateTAC(prettyPrinter.FormattedProgram);

            var expected = new List <string>()
            {
                "b = a",
                "c = a"
            };
            var actual = TAC.Instructions.Select(instruction => instruction.ToString().Trim());

            CollectionAssert.AreEqual(expected, actual);
        }
Ejemplo n.º 28
0
        public static void Main()
        {
            var FileName = @"../../../a.txt";

            try
            {
                var Text = File.ReadAllText(FileName);

                var scanner = new Scanner();
                scanner.SetSource(Text, 0);

                var parser = new Parser(scanner);

                var b = parser.Parse();
                if (!b)
                {
                    Console.WriteLine("Error");
                }
                else
                {
                    Console.WriteLine("Syntax tree built");

                    var fillParents = new FillParentsVisitor();
                    parser.root.Visit(fillParents);

                    var pp = new PrettyPrintVisitor();
                    parser.root.Visit(pp);
                    Console.WriteLine(pp.Text);

                    ASTOptimizer.Optimize(parser);
                    Console.WriteLine("\n\nAfter AST optimizations");
                    pp = new PrettyPrintVisitor();
                    parser.root.Visit(pp);
                    Console.WriteLine(pp.Text);

                    Console.WriteLine("\n\nThree address code");
                    var threeAddrCodeVisitor = new ThreeAddrGenVisitor();
                    parser.root.Visit(threeAddrCodeVisitor);
                    var threeAddressCode = threeAddrCodeVisitor.Instructions;
                    foreach (var instruction in threeAddressCode)
                    {
                        Console.WriteLine(instruction);
                    }

                    Console.WriteLine("\n\nOptimized three address code");
                    var optResult = ThreeAddressCodeOptimizer.OptimizeAll(threeAddressCode);
                    foreach (var x in optResult)
                    {
                        Console.WriteLine(x);
                    }

                    Console.WriteLine("\n\nDivided three address code");
                    var divResult = BasicBlockLeader.DivideLeaderToLeader(optResult);


                    foreach (var x in divResult)
                    {
                        foreach (var y in x.GetInstructions())
                        {
                            Console.WriteLine(y);
                        }
                        Console.WriteLine("--------------");
                    }

                    var cfg = new ControlFlowGraph(divResult);

                    Console.WriteLine("\n\n Edge Classification");
                    Console.WriteLine("----------");

                    foreach (var pair in cfg.ClassifiedEdges)
                    {
                        Console.WriteLine(pair);
                    }

                    Console.WriteLine("----------");


                    foreach (var block in cfg.GetCurrentBasicBlocks())
                    {
                        Console.WriteLine($"{cfg.VertexOf(block)}  {block.GetInstructions()[0]}");
                        var children    = cfg.GetChildrenBasicBlocks(cfg.VertexOf(block));
                        var childrenStr = string.Join(" | ", children.Select(v => v.block.GetInstructions()[0].ToString()));
                        Console.WriteLine($" children: {childrenStr}");

                        var parents    = cfg.GetParentsBasicBlocks(cfg.VertexOf(block));
                        var parentsStr = string.Join(" | ", parents.Select(v => v.block.GetInstructions()[0].ToString()));
                        Console.WriteLine($" parents: {parentsStr}");
                    }

                    ///
                    /// LiveVariableAnalysisAlgorithm
                    ///
                    Console.WriteLine("------------");
                    Console.WriteLine();
                    var activeVariable    = new LiveVariables();
                    var resActiveVariable = activeVariable.Execute(cfg);

                    foreach (var x in resActiveVariable)
                    {
                        foreach (var y in x.Value.In)
                        {
                            Console.WriteLine("In " + y);
                        }
                        Console.WriteLine();
                        foreach (var y in x.Value.Out)
                        {
                            Console.WriteLine("Out " + y);
                        }
                    }


                    Console.WriteLine();
                    Console.WriteLine();
                    Console.WriteLine("NatLoop");
                    var natLoops = NaturalLoop.GetAllNaturalLoops(cfg);
                    foreach (var x in natLoops)
                    {
                        if (x.Count == 0)
                        {
                            continue;
                        }
                        //Console.WriteLine("Loop");
                        for (var i = 0; i < x.Count; i++)
                        {
                            Console.WriteLine("NumberBlock:" + i);
                            foreach (var xfrom in x[i].GetInstructions())
                            {
                                Console.WriteLine(xfrom.ToString());
                            }
                        }
                        Console.WriteLine();
                        Console.WriteLine("-------------");
                    }

                    Console.WriteLine(" \nDone");
                }
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("File {0} not found", FileName);
            }
            catch (LexException e)
            {
                Console.WriteLine("Lex Error. " + e.Message);
            }
            catch (SyntaxException e)
            {
                Console.WriteLine("Syntax Error. " + e.Message);
            }
            _ = Console.ReadLine();
        }
Ejemplo n.º 29
0
        public static void Main()
        {
            string FileName = @"../../a.txt";

            try
            {
                string Text = File.ReadAllText(FileName);

                Scanner scanner = new Scanner();
                scanner.SetSource(Text, 0);

                Parser parser = new Parser(scanner);

                var b = parser.Parse();
                if (!b)
                {
                    Console.WriteLine("Error");
                }
                else
                {
                    Console.WriteLine("Syntax tree built");

                    var fillParents = new FillParentsVisitor();
                    parser.root.Visit(fillParents);

                    var pp = new PrettyPrintVisitor();
                    parser.root.Visit(pp);
                    Console.WriteLine(pp.Text);

                    ASTOptimizer.Optimize(parser);
                    Console.WriteLine("\n\nAfter AST optimizations");
                    pp = new PrettyPrintVisitor();
                    parser.root.Visit(pp);
                    Console.WriteLine(pp.Text);

                    Console.WriteLine("\n\nThree address code");
                    var threeAddrCodeVisitor = new ThreeAddrGenVisitor();
                    parser.root.Visit(threeAddrCodeVisitor);
                    var threeAddressCode = threeAddrCodeVisitor.Instructions;
                    foreach (var instruction in threeAddressCode)
                    {
                        Console.WriteLine(instruction);
                    }

                    var optimizer = new ThreeAddressCodeOptimizer();
                    var optResult = optimizer.Run(threeAddressCode);
                    Console.WriteLine("\n\nOptimized three address code");
                    foreach (var instruction in optResult)
                    {
                        Console.WriteLine(instruction);
                    }
                }
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("File {0} not found", FileName);
            }
            catch (LexException e)
            {
                Console.WriteLine("Lex Error. " + e.Message);
            }
            catch (SyntaxException e)
            {
                Console.WriteLine("Syntax Error. " + e.Message);
            }
            Console.ReadLine();
        }
Ejemplo n.º 30
0
        public void NestedTest4()
        {
            var Text =
                @"
{
if (a < b)
{
    a = a;
    a = b;
}
else
{
    b = b;
    n = 4;
}
b = b;
c = c;
c = j;
}
";
            Scanner scanner = new Scanner();

            scanner.SetSource(Text, 0);
            Parser parser = new Parser(scanner);

            parser.Parse();
            var parentFiller = new FillParentsVisitor();

            parser.root.Visit(parentFiller);

            var ChangeVisitorsOptimization = new List <ChangeVisitor>
            {
                new SameAssignmentOptVisitor(),
                new RemoveEmptyStatementVisitor()
            };


            int countOptimization = 0;

            while (countOptimization < ChangeVisitorsOptimization.Count)
            {
                parser.root.Visit(ChangeVisitorsOptimization[countOptimization]);
                if (ChangeVisitorsOptimization[countOptimization].IsChanged)
                {
                    ChangeVisitorsOptimization[countOptimization].IsChanged = false;
                    countOptimization = 0;
                }
                else
                {
                    countOptimization++;
                }
            }


            var prettyPrinter = new PrettyPrinterVisitor();

            parser.root.Visit(prettyPrinter);

            var TACGenerator = new TACGenerationVisitor();

            parser.root.Visit(TACGenerator);
            var TAC = TACGenerator.TAC;

            var expected = new List <string>()
            {
                "#t0 = a < b",
                "if #t0 goto #L0",
                "n = 4",
                "goto #L1",
                "#L0",
                "a = b",
                "#L1",
                "c = j"
            };
            var actual = TAC.Instructions.Select(instruction => instruction.ToString().Trim());

            CollectionAssert.AreEqual(expected, actual);
        }