-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.cpp
158 lines (137 loc) · 5.19 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
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
#include <cassert>
#include <cstdio>
#include <stack>
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/Module.h>
#include <llvm/Support/raw_ostream.h>
using namespace llvm;
static const uint64_t DATA_SIZE = 30000;
static const char *DATA_NAME = "data";
static const char *PTR_NAME = "ptr";
struct Loop {
BasicBlock *Entry, *Body, *Exit;
PHINode *DataPtrBody, *DataPtrExit;
};
int main() {
// LLVM context.
LLVMContext &Context = getGlobalContext();
Module MainModule("brainfuck program", Context);
IRBuilder<> Builder(Context);
// Some useful constants.
Type *CellType = Type::getInt8Ty(Context);
Constant *One = ConstantInt::get(CellType, 1);
// Function prototypes for the shim.
FunctionType *PutFunctionType = FunctionType::get(
Type::getVoidTy(Context) /* return type */,
std::vector<Type *>(1, CellType) /* argument types */,
false /* var args */);
Function *PutFunction = Function::Create(PutFunctionType,
Function::ExternalLinkage, "brainfuck_put", &MainModule);
FunctionType *GetFunctionType = FunctionType::get(
CellType /* return type */,
std::vector<Type *>() /* argument types */,
false /* var args */);
Function *GetFunction = Function::Create(GetFunctionType,
Function::ExternalLinkage, "brainfuck_get", &MainModule);
// Global data for a brainfuck program.
ArrayType *DataType = ArrayType::get(CellType, DATA_SIZE);
GlobalVariable *Data = new GlobalVariable(
MainModule, DataType, false /* constant */,
GlobalVariable::InternalLinkage, Constant::getNullValue(DataType),
DATA_NAME);
Value *DataPtr = Builder.CreateConstInBoundsGEP2_32(Data, 0, 0, PTR_NAME);
// Main function definition.
FunctionType *MainFunctionType = FunctionType::get(
Type::getVoidTy(Context) /* return type */,
std::vector<Type *>() /* argument types */,
false /* var args */);
Function *MainFunction = Function::Create(MainFunctionType,
Function::ExternalLinkage, "brainfuck_main", &MainModule);
BasicBlock *MainBlock = BasicBlock::Create(Context, "entry", MainFunction);
Builder.SetInsertPoint(MainBlock);
// Code generation.
int c;
Value *Value;
std::stack<Loop> Loops;
Loop ThisLoop;
while ((c = getchar()) != EOF) {
switch (c) {
case '>':
DataPtr = Builder.CreateConstGEP1_32(DataPtr, 1, PTR_NAME);
break;
case '<':
DataPtr = Builder.CreateConstGEP1_32(DataPtr, -1, PTR_NAME);
break;
case '+':
Value = Builder.CreateLoad(DataPtr);
Value = Builder.CreateAdd(Value, One);
Builder.CreateStore(Value, DataPtr);
break;
case '-':
Value = Builder.CreateLoad(DataPtr);
Value = Builder.CreateSub(Value, One);
Builder.CreateStore(Value, DataPtr);
break;
case '.':
Value = Builder.CreateLoad(DataPtr);
Builder.CreateCall(PutFunction, Value);
break;
case ',':
Value = Builder.CreateCall(GetFunction);
Builder.CreateStore(Value, DataPtr);
break;
case '[':
// Prepare data for the stack.
ThisLoop.Entry = Builder.GetInsertBlock();
ThisLoop.Body = BasicBlock::Create(Context, "loop", MainFunction);
ThisLoop.Exit = BasicBlock::Create(Context, "exit", MainFunction);
// Emit the beginning conditional branch.
Value = Builder.CreateLoad(DataPtr);
Value = Builder.CreateIsNotNull(Value);
Builder.CreateCondBr(Value, ThisLoop.Body, ThisLoop.Exit);
// Define the pointer after the loop.
Builder.SetInsertPoint(ThisLoop.Exit);
ThisLoop.DataPtrExit = Builder.CreatePHI(DataPtr->getType(), 2,
PTR_NAME);
ThisLoop.DataPtrExit->addIncoming(DataPtr, ThisLoop.Entry);
// Define the pointer within the loop.
Builder.SetInsertPoint(ThisLoop.Body);
ThisLoop.DataPtrBody = Builder.CreatePHI(DataPtr->getType(), 2,
PTR_NAME);
ThisLoop.DataPtrBody->addIncoming(DataPtr, ThisLoop.Entry);
// Continue generating code within the loop.
DataPtr = ThisLoop.DataPtrBody;
Loops.push(ThisLoop);
break;
case ']':
// Pop data off the stack.
if (Loops.empty()) {
fputs("] requires matching [\n", stderr);
abort();
}
ThisLoop = Loops.top();
Loops.pop();
// Finish off the phi nodes.
ThisLoop.DataPtrBody->addIncoming(DataPtr, Builder.GetInsertBlock());
ThisLoop.DataPtrExit->addIncoming(DataPtr, Builder.GetInsertBlock());
// Emit the ending conditional branch.
Value = Builder.CreateLoad(DataPtr);
Value = Builder.CreateIsNotNull(Value);
Builder.CreateCondBr(Value, ThisLoop.Body, ThisLoop.Exit);
// Move insertion after the loop.
ThisLoop.Exit->moveAfter(Builder.GetInsertBlock());
DataPtr = ThisLoop.DataPtrExit;
Builder.SetInsertPoint(ThisLoop.Exit);
break;
}
}
// Ensure all loops have been closed.
if (!Loops.empty()) {
fputs("[ requires matching ]\n", stderr);
abort();
}
// Finish off brainfuck_main and dump.
Builder.CreateRetVoid();
MainModule.print(outs(), NULL /* assembly annotation writer */);
}