-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
138 lines (116 loc) · 2.94 KB
/
main.cpp
File metadata and controls
138 lines (116 loc) · 2.94 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
/*************************************************************
* Compiler: Principles & Practice *
* Ding Rui, dromniscience@gmail.com *
* 2021, Spring *
* *
* SysY Compiler *
* *
* main.cpp: main() *
*************************************************************/
#include "front/ast.hpp"
#include "front/error.hpp"
#include "front/geneey.hpp"
#include "front/parser.tab.hpp"
#include "front/optimee.hpp"
#include "front/eerep.hpp"
#include "back/gentg.hpp"
#include "back/optimtg.hpp"
#include <iostream>
#include <fstream>
#include <cstring>
#define EEMODE 0
#define TGMODE 1
#define RVMODE 2
extern FILE *yyin, *yyout;
static inline void Helper(const char *p){
std::cerr << "usage: " << p << " -S [-e <INPUT> | -t <INPUT>] -o <OUTPUT>" << std::endl;
}
int main(int argc, char *argv[]){
using std::string;
if(argc < 2){
Helper(argv[0]);
exit(-1);
}
// parsing command line parameters
int opt, mode;
char *infile = nullptr, *outfile = nullptr;
for(int i = 1;i < argc;++i){
if(!strcmp(argv[i], "-S")){
if(i == argc - 1) break;
if(argv[i + 1][0] != '-') {
infile = argv[i + 1];
mode = RVMODE;
}
}
else if(!strcmp(argv[i], "-t")){
if(i == argc - 1) break;
if(argv[i + 1][0] != '-') {
infile = argv[i + 1];
mode = TGMODE;
}
}
else if(!strcmp(argv[i], "-e")){
if(i == argc - 1) break;
if(argv[i + 1][0] != '-') {
infile = argv[i + 1];
mode = EEMODE;
}
}
else if(!strcmp(argv[i], "-o")){
if(i == argc - 1) break;
if(argv[i + 1][0] != '-')
outfile = argv[i + 1];
}
else if(!strcmp(argv[i], "-h")){
Helper(argv[0]);
exit(-1);
}
}
if(!infile)
Reporterror(Error::Noinputfile);
FILE *file;
file = fopen(infile, "r");
if(!file){ /* open failed */
Reporterror(Error::Filenotfound, string(infile));
}
yyin = file;
Filepreread(infile);
// output file
if(!outfile) Reporterror(Error::Nooutputfile);
file = fopen(outfile, "w");
if(!file){ /* open failed */
Reporterror(Error::Filenotfound, string(outfile));
}
yyout = file;
std::ofstream fout(outfile);
std::streambuf *oldbuf = std::cout.rdbuf(fout.rdbuf());
// parser
ASTptr root;
yyparse(&root);
if(!mainptr)
Reporterror(Error::Nomain);
Errornummessage();
// Debugsymtab(glbst);
// root->Debug(0);
// eeyore generator
Treatmain(root);
TraverseAST(root);
// Naive Opt (EE-level)
NaiveEEOpt();
// Eliminate redundant Basic Block (Eeyore-level)
RedundantBBEE();
if(mode == EEMODE)
DumpEE2file();
else {
TranslateEE2TG();
// Naive Opt (TG-level)
EliminateSTLDTG();
if(mode == TGMODE)
DumpTG2file();
else
DumpRV2file();
}
// release resources
std::cout.rdbuf(oldbuf);
fout.close();
}