-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.cpp
More file actions
170 lines (146 loc) · 4.31 KB
/
Copy pathCode.cpp
File metadata and controls
170 lines (146 loc) · 4.31 KB
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
#pragma warning(disable:4996)
#include <cassert>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <memory>
#include <random>
#include <string>
#include "BasicBlocks.h"
#include "AST.h"
#include "Symbol_Table.h"
#include "Utility.h"
using namespace std;
#pragma warning(disable:4996)
bool O1_optimize = false;
//#define DEBUG
void calc_use()
{
for (auto& func : in_func_var_use) {
/*cout << "\n------------------------------------\n";
cout << "In the func name: " << func.first << endl;*/
vector<shared_ptr<VarDef>>tmp;
for (auto var : func.second) {
tmp.push_back(var);
}
sort(tmp.begin(), tmp.end(), [](const shared_ptr<VarDef>& a, const shared_ptr<VarDef>& b)
{
if(a->use_cnt == b->use_cnt)
{
if(a->is_used_in_cond == b->is_used_in_cond)
{
return a->GetName() > b->GetName();
}
else
return a->is_used_in_cond > b->is_used_in_cond;
}
else
return a->use_cnt > b->use_cnt;
});
for(size_t i = 0;i < tmp.size();i++)
tmp[i]->rank = i + 1;
/*for (auto& var : func.second) {
var->rank = lower_bound(tmp.begin(), tmp.end(), var->use_cnt, greater<int>()) - tmp.begin() + 1;
}*/
/*for (auto& var : func.second) {
tmp.push_back(var->use_cnt);
cout << "VarName: " << var->GetName() << " UseCnt: " << var->use_cnt << " UsedRank: " << var->rank << " IsUsedInCond: " << var->is_used_in_cond << endl;
}*/
}
}
#ifndef __linux__
#include "sysy.tab.h"
#else
#include "y.tab.h"
extern FILE* yyin;
extern int yyparse(shared_ptr<CompUnitAST>& ast);
#endif
#include "AST.h"
int main(int argc, const char* argv[]) {
if (argc < 2)
{
printf("Usage: %s <filename>\n", argv[0]);
return 0;
}
string input_path, output_path;
if (argc >= 6 || argc == 3)
O1_optimize = true;
if(argc >= 5)
{
input_path = string(argv[4]);
output_path = string(argv[3]);
}
else
{
input_path = string(argv[1]);
output_path = "./output.s";
}
// 打开输入文件, 并且指定 lexer 在解析的时候读取这个文件
freopen(input_path.c_str(), "r", stdin);
// 调用 parser 函数, parser 函数会进一步调用 lexer 解析输入文件的
shared_ptr<CompUnitAST> ast;
auto ret = yyparse(ast);
assert(!ret);
fstream input_file(input_path);
size_t file_size = input_file.seekg(0, ios::end).tellg();
file_size/= 1024; // 获得文件大小(单位: KB)
input_file.close();
#ifdef DEBUG
InitOutputStream(output_path.c_str());
string file_name(input_path);
const int backslash_index = max(int(file_name.find_last_of('\\')), int(file_name.find_last_of('/')));
file_name = file_name.substr(backslash_index + 1);
output << "\t.file \"" << file_name << "\"\n";
//O1_optimize = true;
cout << "\nAnalyze -----------------------------\n";
ast->Analyze();
ast->Delete_Useless();
ast->Unroll_Loop();
ast->Dump();
for (auto& func : in_func_var_use) {
cout << "\n------------------------------------\n";
cout << "In the func name: " << func.first << endl;
vector<int>tmp;
for (auto& var : func.second) {
tmp.push_back(var->use_cnt);
}
sort(tmp.begin(), tmp.end(), greater<int>());
for (auto& var : func.second) {
var->rank = lower_bound(tmp.begin(), tmp.end(), var->use_cnt, greater<int>()) - tmp.begin() + 1;
}
for (auto& var : func.second) {
tmp.push_back(var->use_cnt);
cout << "VarName: " << var->GetName() << " UseCnt: " << var->use_cnt << " UsedRank: " << var->rank << " IsUsedInCond: " << var->is_used_in_cond << endl;
}
}
/*cout << "\n Basic_Blocks --------------------------------\n";
auto basic_blocks = ConstructRootBlocks(ast);
for (auto& i : basic_blocks) {
i->simplify();
}
for (auto& i : basic_blocks)
i->Dump();*/
cout << "\n RISC-V --------------------------------\n";
ast->GenerateRSICV();
CloseOutputStream();
cout << "\n END --------------------------------\n";
#else
//O1_optimize = false;
InitOutputStream(output_path.c_str());
string file_name(input_path);
const int backslash_index = max(int(file_name.find_last_of('\\')), int(file_name.find_last_of('/')));
file_name = file_name.substr(backslash_index + 1);
output << "\t.file \"" << file_name << "\"\n";
ast->Analyze();
if(O1_optimize) // 对于大于 1MB 的文件, 进行死代码优化
ast->Delete_Useless();
//if(O1_optimize)
//{
// ast->Delete_Useless();
// //ast->Unroll_Loop();
// calc_use();
//}
ast->GenerateRSICV();
CloseOutputStream();
#endif
}