-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathMain.cs
164 lines (140 loc) · 5.89 KB
/
Main.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.IO;
using System.Linq;
using SimpleLang;
using SimpleLang.Visitors;
using SimpleParser;
using SimpleScanner;
namespace SimpleCompiler
{
public class SimpleCompilerMain
{
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().First()}");
var children = cfg.GetChildrenBasicBlocks(cfg.VertexOf(block));
var childrenStr = string.Join(" | ", children.Select(v => v.block.GetInstructions().First().ToString()));
Console.WriteLine($" children: {childrenStr}");
var parents = cfg.GetParentsBasicBlocks(cfg.VertexOf(block));
var parentsStr = string.Join(" | ", parents.Select(v => v.block.GetInstructions().First().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();
}
}
}