forked from sahilpparmar/emmCompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driveParse.C
301 lines (255 loc) · 8.33 KB
/
driveParse.C
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include <libgen.h>
#include <unistd.h>
#include <iostream>
#include "all.h"
#include "Ast.h"
#include <stdio.h>
#include "SymTabMgr.h"
#include "STEClasses.h"
#include "InterCode.h"
#include "SymTab.h"
#include "Value.h"
#include "FinalMachineCodeGen.h"
using namespace std;
#include "E--.tab.h"
#define CPP_PROG_NAME "CPP" // Environment variable to specify location of cpp
#define DEFAULT_CPP_PROG_NAME "cpp"
#define CC_PROG_NAME "CC" // Environment variable to specify C compiler
#define DEFAULT_CC_PROG_NAME "gcc -g"
#define CC_PROG_OPTS "CFLAGS"
#define MAX_DEBUG_LEVEL 2
#ifdef DEBUG_LEXER
#ifdef LEXER_OUT
ofstream lexOutStr(LEXER_OUT);
ostream& lexOut = lexOutStr;
#else
ostream& lexOut = cout;
#endif
#endif
extern int debugLevel;
extern int optLevel;
extern EventEntry* any;
extern int yyparse();
extern int yylinenum;
extern const char* yyfilename;
extern YYSTYPE yylval;
void yyerror(const char *s)
{
errMsg(s);
}
SymTabMgr stm;
string outputFile;
const char* inputFile = "";
string cppShellCmd, ccShellCmd;
bool genCCode;
bool genBinCode;
bool genSharedLib;
bool genDottyCode;
bool genPrologCode;
string outFileSuffix;
extern FILE* yyin;
void
printUsage(const char* cmd) {
cerr << "Usage: " << cmd << //" [-v <num>] "
"[-I include_dir] [-L<library directory>]\n [-l<library>]"
"[-Dmacro[=defn]] [-Uname] " //[-t <target language>]\n "
"[-o <output file>] <input file>\n"
/*"\t-v: debugging\n"
"\t\t0: none\n"
"\t\t1: print AST 'out' file\n"
"\t\t2: print lots of internal debugging info\n"
"\t-o output file name\n"
"\t-t: Specify type of output\n"
"\t\tc: Output C code\n"
"\t\tb: Output a binary (.o) file\n"
"\t\ts: Output a shared library (default)\n"
"\t\td: Output a dotty file\n"
"\t\tp: Output Prolog code\n"*/
"Environment variable CPP can be used to override the default command\n"
"for running cpp program. This variable can include the program name\n"
"as well as command-line arguments to the command. Similarly, the\n"
"environment variable CC can specify the command to be run for compiling\n"
"C code produced by this compiler, but the options to the compiler can\n"
"specified independently using the environment variable CFLAGS.\n";
}
int lParseOption(char *fOption) {
if (strcmp(fOption, "-debug") == 0 || strcmp(fOption, "--debug-dump") == 0)
debugLevel = 1;
else if (strcmp(fOption, "-noopt") == 0 || strcmp(fOption, "--no-optimizations") == 0)
optLevel = 0;
else {
cerr << "Invalid Option Specified '" << fOption << "'\n";
return 1;
}
return 0;
}
int
parseOptions(int argc, char* argv[]) {
int fileInputFound, invalidOptions, i;
opterr = 0; // Suppress printing of errors by getopt.
if (getenv(CPP_PROG_NAME) == NULL)
cppShellCmd = DEFAULT_CPP_PROG_NAME; else cppShellCmd = getenv(CPP_PROG_NAME);
cppShellCmd += " ";
if (getenv(CC_PROG_NAME) == NULL)
ccShellCmd = DEFAULT_CC_PROG_NAME;
else ccShellCmd = getenv(CC_PROG_NAME);
ccShellCmd += " ";
if (getenv(CC_PROG_OPTS) != NULL) {
ccShellCmd += getenv(CC_PROG_OPTS);
ccShellCmd += " ";
}
fileInputFound = 0;
invalidOptions = 0;
debugLevel = 0;
optLevel = 1;
for (i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
// Commandline Options
invalidOptions += lParseOption(argv[i]);
} else if (fileInputFound == 0) {
fileInputFound = 1;
inputFile = argv[i];
} else {
fileInputFound++;
}
}
if (fileInputFound != 1) {
cerr << "Please specify only a single input file\n";
return -1;
} else if (invalidOptions) {
return -1;
} else {
return 0;
}
genSharedLib = !(genCCode || genBinCode || genDottyCode || genPrologCode);
if (genSharedLib)
outFileSuffix = ".so";
if (*inputFile == '\0')
return -1;
if (outputFile == "") {
size_t pos;
if (genSharedLib) {
string sinputFile(inputFile);
if ((pos = sinputFile.rfind('/')) == string::npos)
outputFile = "lib" + sinputFile;
else outputFile =
sinputFile.substr(0, pos) + "/lib" + sinputFile.substr(pos+1);
}
else outputFile = inputFile;
if ((pos = outputFile.rfind('.')) != outputFile.npos)
outputFile.replace(pos, outputFile.size(), outFileSuffix);
else outputFile += outFileSuffix;
}
return 0;
}
int
main(int argc, char *argv[], char *envp[]) {
string ccCmd;
int optionsOK = parseOptions(argc, argv);
if (optionsOK < 0)
return -1;
cppShellCmd += inputFile;
cppShellCmd += " ";
/* ccCmd = "mv ";
ccCmd += outputFile;
ccCmd += " ";
ccCmd += outputFile;
ccCmd += ".c; ";
ccCmd += ccShellCmd;
if (genSharedLib)
ccCmd += "-shared ";
ccCmd += "-o ";
ccCmd += outputFile;*/
if ((yyin = popen(cppShellCmd.c_str(), "r")) == NULL) {
cerr << "Unexpected error in reading input file\n";
return 1;
}
#ifdef TEST_LEXER
int token;
while (token = yylex()) {
if (token == TOK_SYNTAX_ERROR) {
cout << yyfilename << ":" << yylinenum
<< ": Error: Unrecognized token `" << yytext << "'\n";
}
else {
cout << "Token: " << token << " ";
switch(token)
{
case TOK_UINTNUM:
cout << "Attribute: (int) " << yylval.uVal;
break;
case TOK_DOUBLENUM:
cout << "Attribute: (float) " << yylval.dVal;
break;
case TOK_STRCONST:
cout << "Attribute: (string) " << yylval.cVal;
break;
case TOK_ID:
cout << "Attribute: (id) " << yylval.cVal;
break;
default:
break;
}
cout << endl;
}
}
return 0;
#else
// yydebug = 1;
any = new EventEntry("any");
if (stm.insert(any) == OK) {
Type *te = new Type((vector<Type*>*)NULL, Type::EVENT);
any->type(te);
}
DEBUG("=================Lexical and Syntax Parsing==================\n");
yyparse();
if (errCount() > 0) {
errMsg(itoa(errCount()) + " syntax error(s) reported.\nCompilation terminated.");
return 1;
}
stm.leaveToScope(SymTabEntry::Kind::GLOBAL_KIND);
GlobalEntry *ge = (GlobalEntry*)(stm.currentScope());
if (ge != NULL) {
DEBUG("=========================AST Builder=========================\n");
if (debugLevel > 0) {
ge->print(cout, 0);
}
DEBUG("========================Type Checking========================\n");
ge->typeCheck();
if (errCount() > 0) {
errMsg(itoa(errCount()) + " error(s) reported.\nCompilation terminated.");
return 1;
}
DEBUG("======================3 Addr Generation======================\n");
InterCodesClass* in = ge->codeGen();
if (debugLevel > 0) {
in->print(cout);
}
if (optLevel) {
DEBUG("====================== Basic Code Optimization ======================\n");
in->ioptimize();
if (debugLevel > 0) {
in->print(cout);
}
}
DEBUG("====================Basic Block creation=====================\n");
BasicBlocksContainer *bbC = new BasicBlocksContainer();
bbC->createBlockStruct (in);
if (debugLevel > 0) {
bbC->print(cout);
}
if (optLevel) {
DEBUG("=========================Optimization========================\n");
bbC->optimize();
if (debugLevel > 0) {
DEBUG("========================= Final Optimized Code ========================\n");
bbC->print(cout);
}
}
DEBUG("===================Final Code generation=====================\n");
ge->eventHandler(cout);
FinalMachineCodeGen::finalCodeGen(bbC, cout);
DEBUG("Compilation Successful\n");
}
#endif
}