Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
145 lines (118 loc) · 4.18 KB
/
Copy pathmain.cpp
File metadata and controls
145 lines (118 loc) · 4.18 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
#include "Semantikin.h"
#include "Driver.h"
#include "AstVisitors.h"
#include "IRVisitors.h"
#include "MaximalMunch.h"
#include "MachineInstructionPrinter.h"
#include "DataFlowAnalysis.h"
#include <iostream>
#include <fstream>
#include <unistd.h>
// returns the "value" for the "option" or 0 if the option was not
// specified
char* getCmdOption(char** begin, char** end, const std::string& option) {
char** itr = std::find(begin, end, option);
if (itr != end && ++itr != end)
return *itr;
return 0;
}
bool cmdOptionExists(char** begin, char** end, const std::string& option) {
return std::find(begin, end, option) != end;
}
void printHeader() {
printf("This is Semantikin v. 0.1\n");
printf("--------------------------\n");
}
void printHelp() {
printHeader();
printf("%10s | %s\n", "-h", "Print this help message.");
printf("%10s | %s\n", "-i", "[Mandatory] Specify the source input file.");
printf("%10s | %s\n", "-o", "Specify the executable output file.");
printf("%10s | %s\n", "-dumps", "Specify that no further processing [mounting/linking] is required.");
printf("%10s | %s\n", "-dumpAsts", "Dump the AST for all functions to DOT format.");
printf("%10s | %s\n", "-dumpCfgs", "Dump the CFG for all functions to DOT format.");
printf("%10s | %s\n", "-dumpIR", "Dump the Internal Representation for all functions.");
printf("%10s | %s\n", "-dumpIRTree","Dump the Internal Representation for all functions in a tree-like format.");
printf("%10s | %s\n", "-dumpX86", "Dump the X86 (AT&T) assembly for all functions.");
}
int main(int argc, char *argv[]) {
Parser::Driver driver;
Parser::CompilationUnit* astTree = nullptr;
char* inputFileName = nullptr;
/// Check if the user just want to see the help message
if (cmdOptionExists(argv, argv+argc, "-h")) {
printHelp();
exit(0);
}
/// Check if the user specified an input file
if (!cmdOptionExists(argv, argv+argc, "-i")) {
fprintf(stderr, "You need to specify an input file.\n");
exit(1);
}
inputFileName = getCmdOption(argv, argv+argc, "-i");
/// Start parsing
if (access(inputFileName, F_OK | R_OK)) {
fprintf(stderr, "Could not access the specified input file.\n");
exit(1);
}
/// Check if parsing succeeded. The inline actions of the parser
/// will allocate and initialize the astTree variable.
if (!driver.parse(inputFileName, astTree) || astTree == nullptr)
exit(1);
/// Execute semantic analysis
AstSemaVisitor semantic;
astTree->accept(&semantic);
/// Generate the intermediate representation
AstTACGenVisitor irgen(semantic.currentOffset());
astTree->accept(&irgen);
/// Obtain pointer to the IR generated module
auto irModule = irgen.module();
/* Compute Reaching Definitions and Use-Def / Def-Use Chains */
// for (auto& function : *irModule->functions()) {
// DFA::ReachingDefinitions rd(function->bbs(IR::Function::CFGBasicBlockOrder::TOPO_ORDER));
// rd.execute();
// rd.dump();
//
// DFA::LiveVariables lv(function->bbs(IR::Function::CFGBasicBlockOrder::TOPO_ORDER));
// lv.execute();
// lv.dump();
// }
/* Generate code using maximal munch algorithm */
MaximalMunch codeGen;
irModule->accept(&codeGen);
MachineInstructionPrinter macPrinter;
irModule->accept(&macPrinter);
/* Just print out the AST of the function */
if (cmdOptionExists(argv, argv+argc, "-dumpAsts")) {
for (auto& func : *astTree->getFunctions()) {
AstToDotVisitor ast("ast_" + func->getName() + ".dot");
func->accept(&ast);
}
}
/* Check if we need to print the IR-Tree */
if (cmdOptionExists(argv, argv+argc, "-dumpIRTree")) {
string dotFileName(inputFileName);
IRToDotVisitor itdVisitor(dotFileName + ".dot");
irModule->accept(&itdVisitor);
}
/* Just dump the IR */
if (cmdOptionExists(argv, argv+argc, "-dumpIR")) {
stringstream output;
for (auto& f : *irModule->functions())
f->dump(output);
cout << output.str() << endl;
}
/* Just dump the CFG */
if (cmdOptionExists(argv, argv+argc, "-dumpCFG")) {
for (auto& function : *irModule->functions()) {
cout << "Topological Sort: ";
auto result = function->bbs(IR::Function::CFGBasicBlockOrder::TOPO_ORDER);
for (auto bb : *result) {
cout << bb->id() << " -> ";
}
cout << endl;
}
}
/* Finish */
exit(0);
}