This repository has been archived by the owner on May 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
178 lines (142 loc) · 5.31 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
#include "lexer.h"
#include "parser.h"
#include "generator.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/TargetRegistry.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Target/TargetMachine.h"
#include <iostream>
#include <unistd.h>
class InputParser {
public:
InputParser (int &argc, char **argv) {
if (argc == 1) {
show_usage();
exit(0);
}
for (int i = 1; i < argc; ++i) {
if (std::find(std::begin(supported_options), std::end(supported_options), std::string(argv[i]))
!= std::end(supported_options) || std::string(argv[i-1]) == "-o" || i == 1) {
tokens.emplace_back(std::string(argv[i]));
} else {
std::cerr << "error: option '" << argv[i] << "' is not supported!" << std::endl;
}
}
}
void show_usage() {
std::cout << "USAGE: turnip2 <input> [options]" << std::endl
<< "OPTIONS:" << std::endl
<< "\t -g generate source-level debug information" << std::endl
<< "\t -emit-llvm emit LLVM IR for source inputs" << std::endl
<< "\t -o <file> write output to <file>" << std::endl
<< "\t -O optimize code to reduce size and time of execution" << std::endl
<< "\t -S only run compilation steps" << std::endl;
}
const std::string& get_option(const std::string &option) const {
std::vector<std::string>::const_iterator itr;
itr = std::find(this->tokens.begin(), this->tokens.end(), option);
if (itr != this->tokens.end() && ++itr != this->tokens.end()){
return *itr;
}
for (auto &&token : tokens) {
if (token.find(option) == 0) {
return *new std::string(token.substr(token.find_first_of(option.back())+1));
}
}
return *new std::string("");
}
bool option_exists(const std::string &option) const {
return std::find(std::cbegin(tokens), std::cend(tokens), option) != std::cend(tokens);
}
private:
std::vector<std::string> tokens;
const std::vector<std::string> supported_options = {
"-g",
"-emit-llvm",
"-o",
"-O",
"-S"
};
};
void generateObject(Module *m, std::string &out) {
InitializeNativeTarget();
InitializeNativeTargetAsmParser();
InitializeNativeTargetAsmPrinter();
auto targetTriple = sys::getDefaultTargetTriple();
m->setTargetTriple(targetTriple);
std::string error;
auto target = TargetRegistry::lookupTarget(targetTriple, error);
if (target == nullptr) {
errs() << error;
return;
}
auto CPU = "generic";
auto features = "";
TargetOptions opt;
auto RM = Optional<Reloc::Model>();
auto theTargetMachine = target->createTargetMachine(targetTriple, CPU, features, opt, RM);
m->setDataLayout(theTargetMachine->createDataLayout());
std::error_code EC;
raw_fd_ostream dest((out.find('.') != std::string::npos) ? (out.substr(0, std::string(out).find_last_of('.')) + ".o") : (out + ".o"), EC, sys::fs::F_None);
if (EC) {
errs() << "Could not open file: " << EC.message();
return;
}
legacy::PassManager pass;
auto fileType = TargetMachine::CGFT_ObjectFile;
if (theTargetMachine->addPassesToEmitFile(pass, dest, fileType)) {
errs() << "Couldn't emit a file of this type";
return;
}
pass.run(*m);
dest.flush();
#if defined(__linux__)
execl("/usr/bin/gcc", "/usr/bin/gcc", std::string((out.find('.') != std::string::npos) ? (out.substr(0, std::string(out).find_last_of('.')) + ".o") : (out + ".o")).c_str(), std::string("-o" + out).c_str(), NULL);
#endif
}
int main(int argc, char **argv) {
InputParser params(argc, argv);
std::ifstream in(argv[1]);
char c;
std::vector<char> code;
while (!in.eof()) {
in.get(c);
code.emplace_back(c);
}
code.emplace_back(EOF);
try {
Lexer *lexer = new Lexer;
lexer->load(code);
Parser *parser = new Parser(lexer);
std::shared_ptr<Node> ast = parser->parse();
bool optimize = params.option_exists("-O");
bool generateDI = params.option_exists("-g");
if (optimize && generateDI)
generateDI = false;
Generator *generator = new Generator(optimize, generateDI, argv[1]);
generator->generate(ast);
std::string output = "a.out";
if (!params.get_option("-o").empty()) {
output = params.get_option("-o");
}
if (params.option_exists("-emit-llvm")) {
std::string file = std::string(argv[1]).substr(0, std::string(argv[1]).find_last_of('.')) + ".s";
std::error_code EC;
raw_fd_ostream dest(file, EC, sys::fs::F_None);
generator->module.get()->print(dest, nullptr);
dest.close();
}
if (!params.option_exists("-S")) {
generateObject(generator->module.get(), output);
if (generateDI) {
generator->dbuilder->finalize();
}
}
}
catch (const std::string &err) {
std::cerr << "In file " << argv[1] << ":" << err << std::endl;
return 1;
}
return 0;
}