-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
84 lines (64 loc) · 1.71 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
#include "parser.hpp"
#include "llvm/LLVMContext.h"
#include "llvm/Module.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Instructions.h"
#include "llvm/ExecutionEngine/JIT.h"
#include "llvm/ExecutionEngine/Interpreter.h"
#include "llvm/ExecutionEngine/GenericValue.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/IRBuilder.h"
#include <fstream>
#include <streambuf>
using namespace llvm;
// For testing
struct Foo
{
int rc;
int tag;
double _1;
};
IntrusiveRefCntPtr<ModuleBuilder> Context::compileModule(
std::istream& in)
{
std::string str((std::istreambuf_iterator<char>(in)),
std::istreambuf_iterator<char>());
Parser parser(str.data(), str.data() + str.size());
auto mod = parser.module();
auto modBuilder = addModule(mod);
return modBuilder;
}
int main()
{
InitializeNativeTarget();
std::ifstream test("Test.mlk");
Context ctx;
auto modBuilder = ctx.compileModule(test);
ctx.doResolve();
for(auto const& mod : ctx.modules)
{
mod.second->buildAst();
}
#if 1
unique_ptr<llvm::ExecutionEngine> EE(llvm::EngineBuilder(modBuilder->M).create());
for(auto const& otherMod : ctx.modules)
{
if(otherMod.second != modBuilder)
{
EE->addModule(otherMod.second->M);
}
}
Function* f = modBuilder->M->getFunction("main");
vector<llvm::GenericValue> args;
llvm::GenericValue gv = EE->runFunction(f, args);
llvm::outs() << "RC: " << ((Foo*)gv.PointerVal)->rc << "\n";
llvm::outs() << "Tag: " << ((Foo*)gv.PointerVal)->tag << "\n";
llvm::outs() << "Result: " << ((Foo*)gv.PointerVal)->_1 << "\n";
EE->freeMachineCodeForFunction(f);
#endif
llvm_shutdown();
return 0;
}