-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.cpp
154 lines (137 loc) · 3.79 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
#include <iostream>
#include <string>
#include <boost/program_options.hpp>
#include "BackEnd/BackEnd.hpp"
#include "FrontEnd/FrontEnd.hpp"
#include "Optimizations/FlowGraph.h"
#include "Optimizations/Optimizer.hpp"
#include "BackEnd/BackEnd.hpp"
#include "ProcessLog.hpp"
namespace po = boost::program_options;
#include "logger.h"
int main(int argc, char* argv[])
{
cpsl_log::init_log(argc, argv);
try
{
bool emitFlowGraph = false;
bool emitOptimizedFlowGraph = false;
bool emitAST = false;
std::string outFile = "";
std::string inFile = "";
po::options_description desc("Allowed options");
desc.add_options()("help,h", "produce help message")(
"input,i", po::value<std::string>(), "input cpsl file")(
"output,o", po::value<std::string>(), "output asm file")(
"comments,c", "output assembly comments")(
"flowgraph,f", "output flowgraph")("ast,a", "output ast digraph")(
"flowgraph,f", "output flowgraph")(
"flowgraph-optimized,F", "output optimized flowgraph");
po::positional_options_description positionalOptions;
positionalOptions.add("input", 1);
positionalOptions.add("output", 1);
po::variables_map vm;
po::store(po::command_line_parser(argc, argv)
.options(desc)
.positional(positionalOptions)
.run(),
vm);
po::notify(vm);
if (vm.count("help"))
{
std::cout << desc << "\n";
return EXIT_SUCCESS;
}
if (vm.count("comments"))
{
cs6300::ThreeAddressInstruction::Verbose = true;
}
if (vm.count("ast"))
{
emitAST = true;
}
if (vm.count("flowgraph"))
{
emitFlowGraph = true;
}
if (vm.count("flowgraph-optimized"))
{
emitOptimizedFlowGraph = true;
}
if (vm.count("output"))
{
outFile = vm["output"].as<std::string>();
}
else
{
outFile = "out.asm";
}
if (vm.count("input"))
{
inFile = vm["input"].as<std::string>();
}
else
{
inFile = "in.cpsl";
}
if (emitFlowGraph && emitOptimizedFlowGraph)
{
std::cerr << "Warning: generating multiple flow graphs" << std::endl;
}
LOG(INFO) << "Compiling " << inFile << " to " << outFile;
ProcessLog::getInstance()->set_infile(inFile);
auto program = cs6300::parseCPSL(inFile);
if (emitAST)
{
std::string ast;
for (auto& s : program->main)
{
// TODO: Find out why some statements are NULL
if (!s) continue;
ast += "main -> " + s->id() + "\n";
for (auto& edge : s->ASTLines())
{
ast += edge + "\n";
}
}
for (auto&& f : program->functions)
{
for (auto& s : f.second->body)
{
if (!s) continue;
ast += f.first.name + " -> " + s->id() + "\n";
for (auto& edge : s->ASTLines())
{
ast += edge + "\n";
}
}
}
std::cout << "digraph G {\n"
<< "rank=same\n" << ast << "}" << std::endl;
}
auto optimized = cs6300::optimizer(program);
auto intermediate =
std::make_shared<cs6300::IntermediateRepresentationProgram>(optimized);
/* Print out flowgraph */
if (emitFlowGraph) std::cout << cs6300::flowGraphDot(intermediate->main);
intermediate->main = cs6300::optimizer(intermediate->main);
for (auto&& f : intermediate->functions)
{
f.second = cs6300::optimizer(f.second);
}
/* Print out optimized flowgraph */
if (emitOptimizedFlowGraph)
std::cout << cs6300::flowGraphDot(intermediate->main);
cs6300::writeMIPS(intermediate, outFile);
}
catch (std::exception& e)
{
LOG(ERROR) << "Error: " << e.what();
return EXIT_FAILURE;
}
catch (...)
{
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}