-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
46 lines (34 loc) · 1.27 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
#include <iostream>
#include <fstream>
#include <vector>
#include "BFCompiler.h"
int main(int argc, char ** argv) {
if (argc != 3) {
std::cerr << "Usage: llvm_bf <file.bf> <out.ll>" << std::endl;
return -1;
}
auto ifs = std::ifstream(argv[1]);
if (!ifs) {
std::cerr << "Unable open file: " << argv[1] << std::endl;
return -2;
}
std::string source((std::istreambuf_iterator<char>(ifs)),
std::istreambuf_iterator<char>());
LLVMContextRef context = LLVMContextCreate();
LLVMModuleRef module = LLVMModuleCreateWithNameInContext("brainfuck", context);
LLVMBuilderRef builder = LLVMCreateBuilderInContext(context);
BF::declareFunctions(module);
BF::BFMain data = BF::buildMain(context, module, builder);
auto prog = BF::parse(source, 0, source.length(), data);
for (auto instruction: prog)
data.bb = instruction->compile(context, module, data.main_function, data.bb);
for (auto instruction: prog)
delete instruction;
LLVMPositionBuilderAtEnd(builder, data.bb);
BF::buildClear(context, module, builder, data);
LLVMPrintModuleToFile(module, argv[2], nullptr);
LLVMDisposeBuilder(builder);
LLVMDisposeModule(module);
LLVMContextDispose(context);
return 0;
}