-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainPrints.cs
189 lines (175 loc) · 7.03 KB
/
MainPrints.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
using System;
using System.IO;
using System.Text;
using System.Linq;
using System.Reflection;
using System.Collections.Generic;
using SimpleScanner;
using SimpleParser;
using SimpleLang.Visitors;
using SimpleLang.MiddleEnd;
using SimpleLang.Optimizations;
using SimpleLang.Analysis;
using SimpleLang.CodeGenerator;
namespace SimpleCompiler
{
public partial class SimpleCompilerMain
{
//Функция для вывода заданного графа потока управления
public static void PrintCFG(ControlFlowGraph cfg, bool justCode = false)
{
if (!justCode)
{
Print("Граф потока управления:");
foreach (BaseBlock block in cfg.GetBlocks())
{
if (block != cfg.GetStart() && block != cfg.GetEnd())
{
Console.WriteLine("--- Блок {0} ---", block.nBlock);
Console.WriteLine(block);
Console.WriteLine();
Console.WriteLine("Input:\t" + String.Join(" ", cfg.GetInputs(block).
Select(e => e.nBlock.ToString())));
Console.WriteLine("Output:\t" + String.Join(" ", cfg.GetOutputs(block).
Select(e => e.nBlock.ToString())));
Console.WriteLine("----------------");
}
}
}
else
{
Print("Трёхадресный код:");
foreach (BaseBlock block in cfg.GetBlocks())
{
if (block != cfg.GetStart() && block != cfg.GetEnd())
Console.WriteLine(block);
}
}
}
public static void PrintCode(BlockNode root)
{
Print("Восстановленный исходный код:");
var pp = new PrettyPrintVisitor();
root.Visit(pp);
Console.WriteLine(pp.Text);
}
public static void PrintSymbolTable()
{
Print("Таблица символов:");
foreach (var rec in SymbolTable.vars)
Console.WriteLine(rec);
}
public static void PrintEnd()
{
if (Output != null)
{
Output.Close();
StreamWriter Std = new StreamWriter(Console.OpenStandardOutput(),
System.Text.Encoding.GetEncoding(866));
Std.AutoFlush = true;
Console.SetOut(Std);
}
Console.Write("Для завершения работы программы нажмите Enter...");
Console.ReadLine();
}
public static void Print(string msg)
{
Console.WriteLine();
Console.WriteLine(msg);
Console.WriteLine();
}
/////////////////////////////////////////////////////////////////////////
public delegate void RunAnalysis(ControlFlowGraph CFG);
public static void RunAvailableExpressions(ControlFlowGraph CFG)
{
Print("Доступные выражения:");
AvailableExprsAlgorithm AEA = new AvailableExprsAlgorithm(CFG);
var AEAResult = AEA.Apply();
foreach (var block in AEAResult.Item1.Keys)
if (block != CFG.GetStart() && block != CFG.GetEnd())
{
Console.WriteLine(block);
Console.WriteLine("In:\t" + AEAResult.Item1[block]);
Console.WriteLine("Out:\t" + AEAResult.Item2[block]);
}
}
public static void RunDeadOrAlive(ControlFlowGraph CFG)
{
Print("Активные переменные:");
AliveVarsAlgorithm AVA = new AliveVarsAlgorithm(CFG);
var AVAResult = AVA.Apply();
foreach (var block in AVAResult.Item1.Keys)
if (block != CFG.GetStart() && block != CFG.GetEnd())
{
Console.WriteLine(block);
Console.WriteLine("In:\t" + AVAResult.Item1[block]);
Console.WriteLine("Out:\t" + AVAResult.Item2[block]);
}
}
public static void RunDominatorTree(ControlFlowGraph CFG)
{
Print("Дерево доминаторов:");
DominatorsTree DTree = new DominatorsTree(CFG);
//Stack<Pair<DominatorsTree.TreeNode<BaseBlock>,int>> Path =
// new Stack<Pair<DominatorsTree.TreeNode<BaseBlock>, int>>();
//Path.Push(new Pair<DominatorsTree.TreeNode<BaseBlock>,int>(DTree.Root,0));
//int Depth = 0;
//while(Path.Count>0)
//{
// var Top = Path.Peek();
// if (Top.snd != Top.fst.Items.Count)
// {
// Path.Push(new Pair<DominatorsTree.TreeNode<BaseBlock>, int>(
// Top.fst.Items.ElementAt(Top.snd), 0));
// Top.snd += 1;
// Depth += 1;
// }
// else
// {
// for (int i = 0; i < Depth; ++i)
// Console.Write(" ");
// Console.WriteLine(Top.fst.Value.nBlock);
// Path.Pop();
// Depth -= 1;
// }
//}
Stack<DominatorsTree.TreeNode<BaseBlock>> Path =
new Stack<DominatorsTree.TreeNode<BaseBlock>>();
Path.Push(DTree.Root);
while (Path.Count > 0)
{
var Top = Path.Pop();
if(Top.Items.Count>0)
{
Console.Write("{0} => ",Top.Value.nBlock);
foreach(var it in Top.Items)
{
Console.Write("{0} ",it.Value.nBlock);
Path.Push(it);
}
Console.WriteLine();
}
}
}
public static void RunReachingDefinitions(ControlFlowGraph CFG)
{
Print("Достигающие определения:");
ReachingDefsAlgorithm RDA = new ReachingDefsAlgorithm(CFG);
var RDAResult = RDA.Apply();
foreach (var block in RDAResult.Item1.Keys)
if (block != CFG.GetStart() && block != CFG.GetEnd())
{
Console.WriteLine(block);
Console.WriteLine("In:\t" + RDAResult.Item1[block].ToString().Replace("True", "1").Replace("False", "0"));
Console.WriteLine("Out:\t" + RDAResult.Item2[block].ToString().Replace("True", "1").Replace("False", "0"));
}
}
public static void RunSpanningTree(ControlFlowGraph CFG)
{
Print("Остовное дерево:");
var spTree = new SpanningTree(CFG);
spTree.Print();
spTree.PrintEdges();
}
}
}