-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
109 lines (94 loc) · 2.77 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
#include <iostream>
#include <fstream>
#include <parser/parser.h>
#include <tokens/specifier_combination.h>
#include "llvm/Support/raw_ostream.h"
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/ExecutionEngine/MCJIT.h>
#include "llvm/IR/DataLayout.h"
#include "llvm/ExecutionEngine/Interpreter.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Support/Error.h"
#include <boost/program_options.hpp>
namespace po = boost::program_options;
int main(int argc, char *argv[]) {
std::string inputName;
bool printAST = false;
bool printIR = false;
bool execute = false;
unsigned int ac = 0;
std::string av{};
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("print-ast", "print the abstract syntax tree")
("print-ir", "print the generated IR code")
("execute", "execute main function with parameters")
("input-file", po::value<std::vector<std::string> >(), "input file");
po::positional_options_description p;
p.add("input-file", -1);
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
po::notify(vm);
if (vm.empty()) {
std::cout << "no input files" << std::endl;
return 0;
}
if (vm.count("help")) {
std::cout << "Usage: mycc [options] file..." << std::endl;
std::cout << desc << std::endl;
return 0;
}
if (vm.count("input-file")) {
auto v = vm["input-file"].as<std::vector<std::string>>();
if (v.size() > 1) {
std::cout << "multiple input files detected, currently do not support";
}
inputName = v[0];
}
if (vm.count("print-ast")) {
printAST = true;
}
if (vm.count("print-ir")) {
printIR = true;
}
if (vm.count("execute")) {
execute = true;
}
llvm::InitializeNativeTarget();
llvm::InitializeNativeTargetAsmPrinter();
llvm::InitializeNativeTargetAsmParser();
std::ifstream testFile;
testFile.open(inputName.c_str());
auto parser = Parser(testFile);
auto tr = parser.parseTranslationUnit();
if (printAST) {
tr->print(0);
}
tr->codegen();
if (printIR) {
AST::getModule()->print(llvm::outs(), nullptr);
}
if (llvm::verifyModule(*AST::getModule(), &llvm::errs())) {
return 0;
}
if (execute) {
std::string error;
auto engine = llvm::EngineBuilder(AST::takeModule())
.setErrorStr(&error)
.setOptLevel(llvm::CodeGenOpt::Aggressive)
.setEngineKind(llvm::EngineKind::JIT)
.create();
if (!engine) {
std::cerr << "EE Error: " << error << '\n';
return 1;
}
engine->finalizeObject();
typedef int (*Function)();
Function f = reinterpret_cast<Function>(
engine->getPointerToNamedFunction("main"));
f();
}
return 0;
}