-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
154 lines (134 loc) · 3.16 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
/**
* Project VYPe 2015/2016
* Marek Milkovič (xmilko01) & Oliver Nemček (xnemce03)
**/
#include <iostream>
#include <fstream>
#include <cstdarg>
#include "frontend/ast.h"
#include "frontend/context.h"
#include "backend/asmgenerator.h"
// Have to be included last
#include "frontend/c_parser.h"
extern FILE* yyin;
extern int yyparse();
extern int yydebug;
extern int yylex_destroy();
int exitCode = 0;
frontend::Program program;
frontend::Context context;
void finalize(int _exitCode)
{
exitCode = _exitCode;
if (yyin != nullptr)
fclose(yyin);
yylex_destroy(); // Fix memory leaks from flex, bison doesn't call this
}
void yyerror(const char *s, ...)
{
va_list ap;
va_start(ap, s);
fprintf(stderr, "Error: ");
vfprintf(stderr, s, ap);
fprintf(stderr, "\n");
}
int main(int argc, char** argv)
{
std::string outputFileName = "out.asm";
yyin = nullptr;
if (argc == 2)
{
;
}
else if (argc == 3)
{
outputFileName = argv[2];
}
else
{
exitCode = 5;
finalize(exitCode);
return exitCode;
}
//yydebug = 1;
yyin = fopen(argv[1], "r");
if (yyin == nullptr)
{
exitCode = 5;
finalize(exitCode);
return exitCode;
}
int result = yyparse();
if (result != 0)
{
if (exitCode == 0)
exitCode = 2;
finalize(exitCode);
return exitCode;
}
// Check if there is no undefined but declared function
for (const auto& pair : context.globalSymbolTable()->getAllSymbols())
{
if (pair.second->getType() != frontend::Symbol::Type::FUNCTION)
continue;
if (!static_cast<frontend::FunctionSymbol*>(pair.second)->isDefined())
{
exitCode = 3;
finalize(exitCode);
return exitCode;
}
}
// Check presence of main
frontend::Symbol* mainSymbol = context.globalSymbolTable()->findSymbol("main");
if (mainSymbol == nullptr)
{
exitCode = 3;
finalize(exitCode);
return exitCode;
}
// It must be a function
if (mainSymbol->getType() != frontend::Symbol::Type::MANGLING_LINK)
{
exitCode = 3;
finalize(exitCode);
return exitCode;
}
// Only one main has to be defined
frontend::ManglingLinkSymbol* manglingLinkMainSymbol = static_cast<frontend::ManglingLinkSymbol*>(mainSymbol);
if (manglingLinkMainSymbol->getFunctions().size() != 1)
{
exitCode = 3;
finalize(exitCode);
return exitCode;
}
// It has to always be int main(void)
frontend::FunctionSymbol* funcMainSymbol = manglingLinkMainSymbol->getFunctions().front();
if (funcMainSymbol->getReturnType() != frontend::Symbol::DataType::INT || funcMainSymbol->getParameters().size() != 0)
{
exitCode = 3;
finalize(exitCode);
return exitCode;
}
ir::Builder builder;
program.generateIr(builder);
std::ofstream outputFile(outputFileName, std::ios::trunc | std::ios::out);
backend::ASMgenerator generator;
generator.translateIR(builder);
//std::cout << builder.codeText() << std::endl;
try
{
outputFile << generator.getTargetCode() << std::endl;
}
catch (int e)
{
std::cerr << "Generated assemly does not fit into 1MB. Assumed code size is: " << e << "B, stack has 64kB\n";
outputFile.close();
exitCode = 4;
finalize(exitCode);
return exitCode;
}
outputFile.close();
fclose(yyin);
yylex_destroy(); // Fix memory leaks from flex, bison doesn't call this
return 0;
}