-
Notifications
You must be signed in to change notification settings - Fork 4
/
CodeGenerator.cpp
48 lines (43 loc) · 1.45 KB
/
CodeGenerator.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
#include "ast.h"
#include "CodeGenerator.h"
using namespace std;
void CodeGenerator::generate(Program& astRoot)
{
cout << "[INFO]" << "IR generation begin..." << endl;
astRoot.codeGen(*this);
cout << "[INFO]" << "IR generation finished." << endl;
// llvm::legacy::PassManager pm;
// pm.add(createPrintModulePass(llvm::outs()));
// pm.run(*TheModule);
// TheModule->dump();
TheModule->print(llvm::errs(), nullptr);
}
llvm::GenericValue CodeGenerator::run()
{
cout << "[INFO]" << "IR running begin..." << endl;
llvm::ExecutionEngine* ee = genExeEngine();
vector<llvm::GenericValue> args;
llvm::GenericValue res = ee->runFunction(mainFunction, args);
cout << "[INFO]" << "IR running finished." << endl;
return res;
}
llvm::ExecutionEngine* CodeGenerator::genExeEngine()
{
std::string errStr;
auto RTDyldMM = unique_ptr<llvm::SectionMemoryManager>(new llvm::SectionMemoryManager());
llvm::ExecutionEngine* ee = llvm::EngineBuilder(std::move(TheModule))
// .setEngineKind(llvm::EngineKind::Interpreter)
.setEngineKind(llvm::EngineKind::JIT)
.setErrorStr(&errStr)
.setVerifyModules(true)
.setMCJITMemoryManager(move(RTDyldMM))
.setOptLevel(llvm::CodeGenOpt::Default)
.create();
if (!ee)
{
throw std::logic_error("[ERROR]Create Engine Error: " + errStr);
}
ee->addModule(std::move(TheModule));
ee->finalizeObject();
return ee;
}