-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
277 lines (250 loc) · 6.97 KB
/
main.cpp
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include <fstream>
#include <string.h>
#include "errors.hpp"
#include "scanner.hpp"
#include "ast.hpp"
#include "name_analysis.hpp"
#include "type_analysis.hpp"
using namespace holeyc;
static void usageAndDie(){
std::cerr << "Usage: holeycc <infile> <options>\n"
<< " [-t <tokensFile>]: Output tokens to <tokensFile>\n"
<< " [-p]: Parse the input to check syntax\n"
<< " [-u <unparseFile>]: Unparse to <unparseFile>\n"
<< " [-n <nameFile>]: Output name analysis to <namesFile>\n"
<< " [-c]: Do type checking\n"
<< " [-a <3ACFile>]: Output 3AC program to <3ACFile>\n"
<< " [-o <prog.s>]: Output x86 assembly program to <prog.s>\n"
<< "\tNote: Can then perform \"as prog.s -o prog.o\" to assemble and then...\n"
<< "\t\tld -dynamic-linker /lib64/ld-linux-x86-64.so.2 \\\n"
<< "\t\t\t\"/usr/lib/x86_64-linux-gnu/crt1.o \\\n"
<< "\t\t\t/usr/lib/x86_64-linux-gnu/crti.o \\\n"
<< "\t\t\t-lc \\\n"
<< "\t\t\tprog.o \\\n"
<< "\t\t\tstdholeyc.o \\\n"
<< "\t\t\t/usr/lib/x86_64-linux-gnu/crtn.o \\\n"
<< "\t\t\t-o prog.exe\n"
<< "\t...to link the program. It can then be invoked by \"./prog.exe\".\n"
<< "\tMay also need to perform \"gcc stdholeyc.c -c\" to invoke helper C functions.\n";
std::cout << std::flush;
std::cerr << std::flush;
exit(1);
}
static void doTokenization(std::ifstream * input, const char * outPath){
holeyc::Scanner scanner(input);
if (strcmp(outPath, "--") == 0){
scanner.outputTokens(std::cout);
} else {
std::ofstream outStream(outPath);
if (!outStream.good()){
std::string msg = "Bad output file ";
msg += outPath;
throw new holeyc::InternalError(msg.c_str());
}
scanner.outputTokens(outStream);
}
}
static holeyc::ProgramNode * syntacticAnalysis(std::ifstream * input){
if (input == nullptr){
return nullptr;
}
holeyc::ProgramNode * root = nullptr;
holeyc::Scanner scanner(input);
#if 1
holeyc::Parser parser(scanner, &root);
#else
holeyc::Parser parser(scanner);
#endif
int errCode = parser.parse();
if (errCode != 0) {
return nullptr;
}
return root;
}
static void outputAST(ASTNode * ast, const char * outPath){
if (strcmp(outPath, "--") == 0){
ast->unparse(std::cout, 0);
} else {
std::ofstream outStream(outPath);
if (!outStream.good()){
std::string msg = "Bad output file ";
msg += outPath;
throw new holeyc::InternalError(msg.c_str());
}
ast->unparse(outStream, 0);
}
}
static bool doUnparsing(std::ifstream * input, const char * outPath){
holeyc::ProgramNode * ast = syntacticAnalysis(input);
if (ast == nullptr){
std::cerr << "No AST built\n";
return false;
}
if (input == nullptr){
return false;
}
outputAST(ast, outPath);
return true;
}
static holeyc::NameAnalysis * doNameAnalysis(std::ifstream * input){
holeyc::ProgramNode * ast = syntacticAnalysis(input);
if (ast == nullptr){ return nullptr; }
return holeyc::NameAnalysis::build(ast);
}
static holeyc::TypeAnalysis * doTypeAnalysis(std::ifstream * input){
holeyc::NameAnalysis * nameAnalysis = doNameAnalysis(input);
if (nameAnalysis == nullptr){ return nullptr; }
return holeyc::TypeAnalysis::build(nameAnalysis);
}
static holeyc::IRProgram * do3AC(std::ifstream * input){
holeyc::TypeAnalysis * typeAnalysis = doTypeAnalysis(input);
if (typeAnalysis == nullptr){ return nullptr; }
return typeAnalysis->ast->to3AC(typeAnalysis);
}
static void write3AC(holeyc::IRProgram * prog, const char * outPath){
if (outPath == nullptr){
throw new InternalError("Null 3AC file given");
}
std::string flatProg = prog->toString();
if (strcmp(outPath, "--") == 0){
std::cout << flatProg << std::endl;
} else {
std::ofstream outStream(outPath);
outStream << flatProg << std::endl;
outStream.close();
}
}
static void writeX64(holeyc::IRProgram * prog, const char * outPath){
if (outPath == nullptr){
throw new InternalError("Null ASM file given");
}
if (strcmp(outPath, "--") == 0){
prog->toX64(std::cout);
} else {
std::ofstream outStream(outPath);
prog->toX64(outStream);
outStream.close();
}
}
int main(int argc, char * argv[]){
if (argc <= 1){ usageAndDie(); }
std::ifstream * input = new std::ifstream(argv[1]);
if (input == NULL){ usageAndDie(); }
if (!input->good()){
std::cerr << "Bad path " << argv[1] << std::endl;
usageAndDie();
}
bool useful = false; // Check whether the command is
// a no-op
const char * tokensFile = nullptr; // Output file if
// printing tokens
bool checkParse = false; // Flag set if doing
// syntactic analysis
const char * unparseFile = NULL; // Output file if
// unparsing
const char * nameFile = NULL; // Output file if doing
// name analysis
bool checkTypes = false; // Flag set if doing
// syntactic analysis
const char * threeACFile = NULL; // Output file if doing
// 3AC conversion
const char * asmFile = NULL; // Output file for
// X64 representation
for (int i = 1; i < argc; i++){
if (argv[i][0] == '-'){
if (argv[i][1] == 't'){
i++;
if (i >= argc){ usageAndDie(); }
tokensFile = argv[i];
useful = true;
} else if (argv[i][1] == 'p'){
i++;
checkParse = true;
useful = true;
} else if (argv[i][1] == 'u'){
i++;
if (i >= argc){ usageAndDie(); }
unparseFile = argv[i];
useful = true;
} else if (argv[i][1] == 'n'){
i++;
if (i >= argc){ usageAndDie(); }
nameFile = argv[i];
useful = true;
} else if (argv[i][1] == 'c'){
i++;
checkTypes = true;
useful = true;
} else if (argv[i][1] == 'a'){
i++;
if (i >= argc){ usageAndDie(); }
threeACFile = argv[i];
useful = true;
} else if (argv[i][1] == 'o'){
i++;
if (i >= argc){ usageAndDie(); }
asmFile = argv[i];
useful = true;
} else {
std::cerr << "Unknown option"
<< " " << argv[i] << "\n";
usageAndDie();
}
}
}
if (useful == false){
std::cerr << "You didn't specify an operation to do!\n";
usageAndDie();
}
try {
if (tokensFile != nullptr){
doTokenization(input, tokensFile);
}
if (checkParse){
if (!syntacticAnalysis(input)){
std::cerr << "Parse failed";
}
}
if (unparseFile != nullptr){
doUnparsing(input, unparseFile);
}
if (nameFile){
holeyc::NameAnalysis * na;
na = doNameAnalysis(input);
if (na != nullptr){
outputAST(na->ast, nameFile);
return 0;
}
std::cerr << "Name Analysis Failed\n";
return 1;
}
if (checkTypes){
if (doTypeAnalysis(input) != nullptr){
return 0;
}
std::cerr << "Type Analysis Failed\n";
return 1;
}
if (threeACFile){
if (auto prog = do3AC(input)){
write3AC(prog, threeACFile);
return 0;
}
return 1;
}
if (asmFile){
if (auto prog = do3AC(input)){
writeX64(prog, asmFile);
return 0;
}
return 1;
}
} catch (holeyc::ToDoError * e){
std::cerr << "ToDoError: " << e->msg() << "\n";
return 1;
} catch (holeyc::InternalError * e){
std::cerr << "InternalError: " << e->msg() << "\n";
return 1;
}
return 0;
}