-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
217 lines (189 loc) · 6.5 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
#include "ast.h"
#include "error.h"
#include "ir/ir.h"
#include "opt/pass/pass.h"
#include "parser.h"
#include "target/target.h"
#include "visitor.h"
#include <fstream>
#include <getopt.h>
using Passes = opt::PassPipeline<
opt::FillPredsPass, opt::SimplifyCFGPass, opt::FillPredsPass,
opt::FillInlinePass, opt::FunctionInliningPass, opt::FillPredsPass,
opt::FillReversePostOrderPass, opt::FillUsesPass,
opt::CooperFillDominatorsPass, opt::FillDominanceFrontierPass,
opt::SSAConstructPass, opt::FillUsesPass, opt::GVNPass, opt::FillUsesPass,
opt::SimpleDeadCodeEliminationPass, opt::FillPredsPass,
opt::SSADestructPass, opt::FillUsesPass,
opt::SimpleRemoveCopyAfterSSADestructPass,
opt::LocalConstAndCopyPropagationPass, opt::FillUsesPass,
opt::SimpleDeadCodeEliminationPass, opt::FillPredsPass,
opt::SimplifyCFGPass, opt::LocalConstAndCopyPropagationPass,
opt::FillUsesPass, opt::SimpleDeadCodeEliminationPass, opt::FillPredsPass,
opt::SimplifyCFGPass, opt::FillPredsPass, opt::FillReversePostOrderPass,
opt::CooperFillDominatorsPass, opt::FillDominanceFrontierPass,
opt::LivenessAnalysisPass, opt::FillUsesPass,
opt::LoopInvariantCodeMotionPass, opt::SimpleDeadCodeEliminationPass,
opt::FillPredsPass, opt::SimplifyCFGPass, opt::TailRecursionElimination,
opt::FillPredsPass, opt::SimplifyCFGPass>;
using RegisterPasses =
opt::PassPipeline<opt::FillLeafPass, opt::FillUsesPass,
opt::FillReversePostOrderPass, opt::LivenessAnalysisPass,
opt::FillIntervalPass>;
struct Options {
bool optimize = false;
bool emit_ast = false;
bool emit_ir = false;
bool emit_asm = false;
std::string output;
};
void usage(const char *name) {
std::cerr << "Usage: " << name << " [options] [file]" << std::endl;
std::cerr << "Options:" << std::endl;
std::cerr << " -h, --help: Show this help message" << std::endl;
std::cerr << " -O1: Enable optimization" << std::endl;
std::cerr << " --emit-ast: Emit AST as JSON" << std::endl;
std::cerr << " --emit-ir: Emit IR as JSON" << std::endl;
std::cerr << " -S, --emit-asm: Emit assembly" << std::endl;
std::cerr << " -o, --output: Output file" << std::endl;
}
void cmd_error(const char *name, const std::string &msg, int exitcode = 1) {
std::cerr << name << ": " << msg << std::endl;
exit(exitcode);
}
extern FILE *yyin;
void compile(const char *name, const Options &options,
const std::string &input) {
std::ofstream outfile;
auto output = options.output;
yyin = fopen(input.c_str(), "r");
if (yyin == nullptr) {
cmd_error(name, "failed to open file: " + input, 4);
}
auto root = std::make_shared<CompUnits>();
yyparse(root);
if (options.emit_ast) {
if (output.length() == 0) {
output = "out.json";
}
outfile.open(output, std::ios::out);
print_ast(outfile, *root);
return;
}
ir::Module module;
Visitor visitor(module, options.optimize);
visitor.visit(*root);
if (has_error()) {
cmd_error(name, "compilation failed", 5);
}
if (options.optimize) {
Passes pass;
pass.run(module);
}
if (options.emit_ir) {
if (output.length() == 0) {
output = "out.ssa";
}
outfile.open(output, std::ios::out);
module.emit(outfile);
return;
}
RegisterPasses reg_pass;
reg_pass.run(module);
// std::cerr << "Register allocation:" << std::endl;
// for (auto &func : module.functions) {
// std::cerr << "Function: " << func->name << std::endl;
// target::LinearScanAllocator regalloc;
// regalloc.allocate_registers(*func);
// for (auto [temp, reg] : regalloc.get_register_map()) {
// temp->emit(std::cerr);
// std::cerr << " -> " << target::regno2string(reg) << std::endl;
// }
// }
// std::cerr << "Stack layout:" << std::endl;
// for (auto &func : module.functions) {
// std::cerr << "Function: " << func->name << std::endl;
// target::StackManager stack_manager;
// stack_manager.run(*func);
// for (auto [reg, offset] :
// stack_manager.get_callee_saved_regs_offset()) {
// std::cerr << target::regno2string(reg) << " -> sp(" << offset <<
// ")"
// << std::endl;
// }
// for (auto [temp, offset] : stack_manager.get_local_var_offset()) {
// temp->emit(std::cerr);
// std::cerr << " -> sp(" << offset << ")" << std::endl;
// }
// for (auto [temp, offset] : stack_manager.get_spilled_temps_offset())
// {
// temp->emit(std::cerr);
// std::cerr << " -> sp(" << offset << ")" << std::endl;
// }
// }
if (options.emit_asm) {
if (output.length() == 0) {
output = "out.s";
}
outfile.open(output, std::ios::out);
target::Generator generator(outfile, options.optimize);
generator.generate(module);
return;
}
cmd_error(name, "nothing to do", 6);
}
int main(int argc, char *argv[]) {
enum {
HELP = 256,
O1,
EMIT_IR,
EMIT_AST,
EMIT_ASM,
OUTPUT,
};
const struct option long_options[] = {
{"help", no_argument, 0, HELP},
{"O1", no_argument, 0, O1},
{"emit-ast", no_argument, 0, EMIT_AST},
{"emit-ir", no_argument, 0, EMIT_IR},
{"emit-asm", no_argument, 0, EMIT_ASM},
{"output", required_argument, 0, OUTPUT},
{0, 0, 0, 0}};
Options options;
int opt;
while ((opt = getopt_long_only(argc, argv, "hSo:", long_options, NULL)) !=
-1) {
switch (opt) {
case 'h':
case HELP:
usage(argv[0]);
return 0;
case O1:
options.optimize = true;
break;
case EMIT_AST:
options.emit_ast = true;
break;
case EMIT_IR:
options.emit_ir = true;
break;
case 'S':
case EMIT_ASM:
options.emit_asm = true;
break;
case 'o':
case OUTPUT:
options.output = optarg;
break;
case '?':
cmd_error(argv[0], "unknown option", 2);
return 1;
}
}
if (argv[optind] == nullptr) {
cmd_error(argv[0], "no input file", 3);
return 1;
}
compile(argv[0], options, argv[optind]);
return 0;
}