-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.cpp
161 lines (138 loc) Β· 4.89 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
#include <fmt/core.h>
#include <cstdio>
#include <cstdlib>
#include <cxxopts.hpp>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <memory>
#include <string>
#include <utility>
#include "ast.hpp"
#include "ast_dumper.hpp"
#include "llvm_ir_generator.hpp"
#include "qbe_ir_generator.hpp"
#include "scope.hpp"
#include "type_checker.hpp"
#include "util.hpp"
#include "y.tab.hpp"
extern FILE*
yyin; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables):
// flex read from this file pointer.
extern void yylex_destroy(); // NOLINT(readability-identifier-naming): extern
// from flex generated code.
int CompileQbe(std::unique_ptr<AstNode> trans_unit,
const std::string& input_basename,
const std::string& output_name);
int CompileLLVM(std::unique_ptr<AstNode> trans_unit,
const std::string& input_basename,
const std::string& output_name);
int main( // NOLINT(bugprone-exception-escape): Using a big try-catch block to
// catch all exceptions isn't reasonable.
int argc, char** argv)
{
auto cmd_options = cxxopts::Options{
argv[0], // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic):
// std::span is available in C++20.
"A simple C compiler."};
// clang-format off
cmd_options.custom_help("[options] file");
cmd_options.add_options()
("o, output", "Write output to <file>", cxxopts::value<std::string>()->default_value("a.out"), "<file>")
("d, dump", "Dump the abstract syntax tree", cxxopts::value<bool>()->default_value("false"))
("t, target", "Specify target IR", cxxopts::value<std::string>()->default_value("qbe"), "[qbe|llvm]")
("h, help", "Display available options")
;
// clang-format on
auto opts = cmd_options.parse(argc, argv);
if (opts.count("help")) {
std::cerr << cmd_options.help() << '\n';
std::exit(0);
}
auto args = opts.unmatched();
if (args.size() == 0) {
std::cerr << "no input files" << '\n';
std::exit(0);
}
// TODO: support compiling multiple files
if (args.size() > 1) {
std::cerr << "cannot compile more than one input file" << '\n';
std::exit(0);
}
auto input_path = std::filesystem::path(args.at(0));
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
yyin = fopen(input_path.c_str(), "r");
if (yyin == nullptr) {
std::cerr << "cannot open input file" << '\n';
std::exit(0);
}
/// @brief The root node of the translation unit.
auto trans_unit = std::unique_ptr<AstNode>{};
yy::parser parser{trans_unit};
int ret = parser.parse();
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
fclose(yyin);
yylex_destroy();
// 0 on success, 1 otherwise
if (ret) {
return ret;
}
// perform analyses and transformations on the ast
auto scopes = ScopeStack{};
TypeChecker type_checker{scopes};
trans_unit->Accept(type_checker);
if (opts["dump"].as<bool>()) {
const auto max_level = 80u;
AstDumper ast_dumper{Indenter{' ', Indenter::SizePerLevel{2},
Indenter::MaxLevel{max_level}}};
trans_unit->Accept(ast_dumper);
}
auto input_basename = input_path.stem().string();
auto output = opts["output"].as<std::string>();
// generate intermediate representation based on target option
if (opts["target"].as<std::string>() == "qbe") {
return CompileQbe(std::move(trans_unit), input_basename, output);
} else if (opts["target"].as<std::string>() == "llvm") {
return CompileLLVM(std::move(trans_unit), input_basename, output);
} else {
std::cerr << "unknown target" << '\n';
std::exit(0);
}
return 0;
}
int CompileQbe(std::unique_ptr<AstNode> trans_unit,
const std::string& input_basename,
const std::string& output_name) {
auto output_ir = std::ofstream{fmt::format("{}.ssa", input_basename)};
QbeIrGenerator code_generator{output_ir};
trans_unit->Accept(code_generator);
output_ir.close();
// generate assembly
std::string qbe_command = fmt::format("qbe -o {0}.s {0}.ssa", input_basename);
auto qbe_ret = std::system(qbe_command.c_str());
// 0 on success, 1 otherwise
if (qbe_ret) {
return qbe_ret;
}
// generate executable
std::string cc_command =
fmt::format("cc -o {} {}.s", output_name, input_basename);
auto cc_ret = std::system(cc_command.c_str());
// 0 on success, 1 otherwise
if (cc_ret) {
return cc_ret;
}
return 0;
}
int CompileLLVM(std::unique_ptr<AstNode> trans_unit,
const std::string& input_basename,
const std::string& output_name) {
auto output_ir = std::ofstream{fmt::format("{}.ll", input_basename)};
LLVMIRGenerator code_generator{output_ir, input_basename};
trans_unit->Accept(code_generator);
// TODO: Write to stdout based on cxxopts.
// Write LLVM IR to output file "*.ll".
code_generator.PrintIR();
output_ir.close();
return 0;
}