-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
45 lines (33 loc) · 1.16 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
#include <iostream>
#include <memory>
#include <ctype.h>
#include <main.h>
#include <litint.h>
#include <arithoperator.h>
const string vFStdin = ":stdin:";
void repl(string file) {
auto mod = std::make_unique<Module>(StringRef(file), vLCtx);
vOpts = new PassManager<Function>();
ExecutionEngine *jitter = EngineBuilder(std::move(mod)).create();
LitInt a1(10), a2(2);
BaseAst *ast0 = new ArithOperator(BinOperator::DIV, a1, a2);
auto fn = Function::Create(FunctionType::get(Type::getInt32Ty(vLCtx), ArrayRef<Type *>(), false), Function::InternalLinkage, "__eval", mod.get());
auto bb = BasicBlock::Create(vLCtx, "entry", fn);
vIrb.SetInsertPoint(bb);
auto retv = ast0->codegen();
vIrb.CreateRet(retv);
fn->print(llvm::outs());
//verifyFunction(*fn);
auto fun = (jitter->getPointerToFunction(fn));
int32_t (*func)() = reinterpret_cast<int32_t (*)()>(fun);
cout << "10 / 2 = @" << fun << ": " << flush;
cout << func() << endl;
}
int main(int argc, char **argv)
{
cout << "Jit calculator: (*) (/) > (+) (-)" << endl;
if (argc >1)
{ repl(string(argv[1])); }
else { repl(vFStdin); }
return 0;
}