-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbf2llvm.cpp
188 lines (167 loc) · 6.25 KB
/
bf2llvm.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* Brainf**k compiler based on LLVM API
* $ g++ `llvm-config --cxxflags --ldflags --libs --system-libs` bf2llvm.cpp -o bf2llvm
* $ echo "+++++++++[>++++++++>+++++++++++>+++++<<<-]>.>++.+++++++..+++.\
>-.------------.<++++++++.--------.+++.------.--------.>+." | \
./bf2llvm
* $ gcc output.o
* $ ./a.out
*/
#include <iostream>
#include <string>
#include "llvm/ADT/Optional.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/TargetRegistry.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetOptions.h"
static llvm::LLVMContext TheContext;
static llvm::IRBuilder<> Builder(TheContext);
static std::unique_ptr<llvm::Module> TheModule;
void emit_move_ptr(llvm::Value* ptr, int diff) {
Builder.CreateStore(
Builder.CreateInBoundsGEP(
Builder.getInt8Ty(),
Builder.CreateLoad(ptr),
Builder.getInt32(diff)),
ptr);
}
void emit_add(llvm::Value* ptr, int diff) {
llvm::Value* tmp = Builder.CreateLoad(ptr);
Builder.CreateStore(
Builder.CreateAdd(
Builder.CreateLoad(tmp),
Builder.getInt8(diff)),
tmp);
}
void emit_put(llvm::Value* ptr) {
llvm::Function* funcPutChar = llvm::cast<llvm::Function>(
TheModule->getOrInsertFunction("putchar",
Builder.getInt32Ty(),
Builder.getInt32Ty(),
nullptr));
Builder.CreateCall(
funcPutChar,
Builder.CreateSExt(
Builder.CreateLoad(Builder.CreateLoad(ptr)),
Builder.getInt32Ty()));
}
void emit_get(llvm::Value* ptr) {
llvm::Function* funcGetChar = llvm::cast<llvm::Function>(
TheModule->getOrInsertFunction("getchar",
Builder.getInt32Ty(),
nullptr));
Builder.CreateStore(
Builder.CreateTrunc(
Builder.CreateCall(funcGetChar),
Builder.getInt8Ty()),
Builder.CreateLoad(ptr));
}
struct WhileBlock {
llvm::BasicBlock* cond_block;
llvm::BasicBlock* body_block;
llvm::BasicBlock* end_block;
};
void emit_while_start(llvm::Function* func, llvm::Value* ptr, WhileBlock* while_block, int while_index) {
while_block->cond_block = llvm::BasicBlock::Create(
TheContext, std::string("while_cond") + std::to_string(while_index), func);
while_block->body_block = llvm::BasicBlock::Create(
TheContext, std::string("while_body") + std::to_string(while_index), func);
while_block->end_block = llvm::BasicBlock::Create(
TheContext, std::string("while_end") + std::to_string(while_index), func);
Builder.CreateBr(while_block->cond_block);
Builder.SetInsertPoint(while_block->cond_block);
Builder.CreateCondBr(
Builder.CreateICmpNE(
Builder.CreateLoad(Builder.CreateLoad(ptr)),
Builder.getInt8(0)),
while_block->body_block,
while_block->end_block);
Builder.SetInsertPoint(while_block->body_block);
}
void emit_while_end(WhileBlock* while_block) {
Builder.CreateBr(while_block->cond_block);
Builder.SetInsertPoint(while_block->end_block);
}
int main() {
TheModule = llvm::make_unique<llvm::Module>("top", TheContext);
llvm::Function* mainFunc = llvm::Function::Create(
llvm::FunctionType::get(llvm::Type::getInt32Ty(TheContext), false),
llvm::Function::ExternalLinkage, "main", TheModule.get());
Builder.SetInsertPoint(llvm::BasicBlock::Create(TheContext, "", mainFunc));
llvm::Value* data = Builder.CreateAlloca(Builder.getInt8PtrTy(), nullptr, "data");
llvm::Value* ptr = Builder.CreateAlloca(Builder.getInt8PtrTy(), nullptr, "ptr");
llvm::Function* funcCalloc = llvm::cast<llvm::Function>(
TheModule->getOrInsertFunction("calloc",
Builder.getInt8PtrTy(),
Builder.getInt64Ty(), Builder.getInt64Ty(),
nullptr));
llvm::Value* data_ptr = Builder.CreateCall(funcCalloc, {Builder.getInt64(30000), Builder.getInt64(1)});
Builder.CreateStore(data_ptr, data);
Builder.CreateStore(data_ptr, ptr);
int while_index = 0;
WhileBlock while_blocks[1000];
WhileBlock* while_block_ptr = while_blocks;
char c;
while (std::cin.get(c)) {
switch (c) {
case '>': emit_move_ptr(ptr, 1); break;
case '<': emit_move_ptr(ptr, -1); break;
case '+': emit_add(ptr, 1); break;
case '-': emit_add(ptr, -1); break;
case '[': emit_while_start(mainFunc, ptr, while_block_ptr++, while_index++); break;
case ']': if (--while_block_ptr < while_blocks) {
std::cerr << "unmatching ]\n";
return 1;
}
emit_while_end(while_block_ptr); break;
case '.': emit_put(ptr); break;
case ',': emit_get(ptr); break;
}
}
llvm::Function* funcFree = llvm::cast<llvm::Function>(
TheModule->getOrInsertFunction("free",
Builder.getVoidTy(),
Builder.getInt8PtrTy(),
nullptr));
Builder.CreateCall(funcFree, {Builder.CreateLoad(data)});
Builder.CreateRet(Builder.getInt32(0));
llvm::InitializeAllTargetInfos();
llvm::InitializeAllTargets();
llvm::InitializeAllTargetMCs();
llvm::InitializeAllAsmParsers();
llvm::InitializeAllAsmPrinters();
std::string TargetTriple = llvm::sys::getDefaultTargetTriple();
std::string err;
const llvm::Target* Target = llvm::TargetRegistry::lookupTarget(TargetTriple, err);
if (!Target) {
std::cerr << "Failed to lookup target " + TargetTriple + ": " + err;
return 1;
}
llvm::TargetOptions opt;
llvm::TargetMachine* TheTargetMachine = Target->createTargetMachine(
TargetTriple, "generic", "", opt, llvm::Optional<llvm::Reloc::Model>());
TheModule->setTargetTriple(TargetTriple);
TheModule->setDataLayout(TheTargetMachine->createDataLayout());
std::string Filename = "output.o";
std::error_code err_code;
llvm::raw_fd_ostream dest(Filename, err_code, llvm::sys::fs::F_None);
if (err_code) {
std::cerr << "Could not open file: " << err_code.message();
return 1;
}
llvm::legacy::PassManager pass;
if (TheTargetMachine->addPassesToEmitFile(pass, dest, llvm::TargetMachine::CGFT_ObjectFile)) {
std::cerr << "TheTargetMachine can't emit a file of this type\n";
return 1;
}
pass.run(*TheModule);
dest.flush();
std::cout << "Wrote " << Filename << "\n";
return 0;
}