1 //===-- BrainF.cpp - BrainF compiler example ----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===--------------------------------------------------------------------===//
10 // This class compiles the BrainF language into LLVM assembly.
12 // The BrainF language has 8 commands:
13 // Command Equivalent C Action
14 // ------- ------------ ------
15 // , *h=getchar(); Read a character from stdin, 255 on EOF
16 // . putchar(*h); Write a character to stdout
17 // - --*h; Decrement tape
18 // + ++*h; Increment tape
19 // < --h; Move head left
20 // > ++h; Move head right
21 // [ while(*h) { Start loop
24 //===--------------------------------------------------------------------===//
27 #include "llvm/Constants.h"
28 #include "llvm/Intrinsics.h"
29 #include "llvm/ADT/STLExtras.h"
33 //Set the constants for naming
34 const char *BrainF::tapereg = "tape";
35 const char *BrainF::headreg = "head";
36 const char *BrainF::label = "brainf";
37 const char *BrainF::testreg = "test";
39 Module *BrainF::parse(std::istream *in1, int mem, CompileFlags cf) {
50 void BrainF::header() {
51 module = new Module("BrainF");
55 //declare void @llvm.memset.i32(i8 *, i8, i32, i32)
56 Function *memset_func = Intrinsic::getDeclaration(module, Intrinsic::memset_i32);
58 //declare i32 @getchar()
59 getchar_func = cast<Function>(module->
60 getOrInsertFunction("getchar", IntegerType::Int32Ty, NULL));
62 //declare i32 @putchar(i32)
63 putchar_func = cast<Function>(module->
64 getOrInsertFunction("putchar", IntegerType::Int32Ty,
65 IntegerType::Int32Ty, NULL));
70 //define void @brainf()
71 brainf_func = cast<Function>(module->
72 getOrInsertFunction("brainf", Type::VoidTy, NULL));
74 builder = new IRBuilder(BasicBlock::Create(label, brainf_func));
76 //%arr = malloc i8, i32 %d
77 ConstantInt *val_mem = ConstantInt::get(APInt(32, memtotal));
78 ptr_arr = builder->CreateMalloc(IntegerType::Int8Ty, val_mem, "arr");
80 //call void @llvm.memset.i32(i8 *%arr, i8 0, i32 %d, i32 1)
82 Value *memset_params[] = {
84 ConstantInt::get(APInt(8, 0)),
86 ConstantInt::get(APInt(32, 1))
89 CallInst *memset_call = builder->
90 CreateCall(memset_func, memset_params, array_endof(memset_params));
91 memset_call->setTailCall(false);
94 //%arrmax = getelementptr i8 *%arr, i32 %d
95 if (comflag & flag_arraybounds) {
96 ptr_arrmax = builder->
97 CreateGEP(ptr_arr, ConstantInt::get(APInt(32, memtotal)), "arrmax");
100 //%head.%d = getelementptr i8 *%arr, i32 %d
101 curhead = builder->CreateGEP(ptr_arr,
102 ConstantInt::get(APInt(32, memtotal/2)),
110 endbb = BasicBlock::Create(label, brainf_func);
113 new FreeInst(ptr_arr, endbb);
116 ReturnInst::Create(endbb);
120 //Error block for array out of bounds
121 if (comflag & flag_arraybounds)
123 //@aberrormsg = internal constant [%d x i8] c"\00"
124 Constant *msg_0 = ConstantArray::
125 get("Error: The head has left the tape.", true);
127 GlobalVariable *aberrormsg = new GlobalVariable(
130 GlobalValue::InternalLinkage,
135 //declare i32 @puts(i8 *)
136 Function *puts_func = cast<Function>(module->
137 getOrInsertFunction("puts", IntegerType::Int32Ty,
138 PointerType::getUnqual(IntegerType::Int8Ty), NULL));
141 aberrorbb = BasicBlock::Create(label, brainf_func);
143 //call i32 @puts(i8 *getelementptr([%d x i8] *@aberrormsg, i32 0, i32 0))
145 Constant *zero_32 = Constant::getNullValue(IntegerType::Int32Ty);
147 Constant *gep_params[] = {
152 Constant *msgptr = ConstantExpr::
153 getGetElementPtr(aberrormsg, gep_params,
154 array_lengthof(gep_params));
156 Value *puts_params[] = {
160 CallInst *puts_call =
161 CallInst::Create(puts_func,
162 puts_params, array_endof(puts_params),
164 puts_call->setTailCall(false);
167 //br label %brainf.end
168 BranchInst::Create(endbb, aberrorbb);
172 void BrainF::readloop(PHINode *phi, BasicBlock *oldbb, BasicBlock *testbb) {
173 Symbol cursym = SYM_NONE;
175 Symbol nextsym = SYM_NONE;
181 while(cursym != SYM_EOF && cursym != SYM_ENDLOOP) {
182 // Write out commands
190 //%tape.%d = call i32 @getchar()
191 CallInst *getchar_call = builder->CreateCall(getchar_func, tapereg);
192 getchar_call->setTailCall(false);
193 Value *tape_0 = getchar_call;
195 //%tape.%d = trunc i32 %tape.%d to i8
196 Value *tape_1 = builder->
197 CreateTrunc(tape_0, IntegerType::Int8Ty, tapereg);
199 //store i8 %tape.%d, i8 *%head.%d
200 builder->CreateStore(tape_1, curhead);
206 //%tape.%d = load i8 *%head.%d
207 LoadInst *tape_0 = builder->CreateLoad(curhead, tapereg);
209 //%tape.%d = sext i8 %tape.%d to i32
210 Value *tape_1 = builder->
211 CreateSExt(tape_0, IntegerType::Int32Ty, tapereg);
213 //call i32 @putchar(i32 %tape.%d)
214 Value *putchar_params[] = {
217 CallInst *putchar_call = builder->
218 CreateCall(putchar_func,
219 putchar_params, array_endof(putchar_params));
220 putchar_call->setTailCall(false);
226 //%head.%d = getelementptr i8 *%head.%d, i32 %d
228 CreateGEP(curhead, ConstantInt::get(APInt(32, curvalue)),
231 //Error block for array out of bounds
232 if (comflag & flag_arraybounds)
234 //%test.%d = icmp uge i8 *%head.%d, %arrmax
235 Value *test_0 = builder->
236 CreateICmpUGE(curhead, ptr_arrmax, testreg);
238 //%test.%d = icmp ult i8 *%head.%d, %arr
239 Value *test_1 = builder->
240 CreateICmpULT(curhead, ptr_arr, testreg);
242 //%test.%d = or i1 %test.%d, %test.%d
243 Value *test_2 = builder->
244 CreateOr(test_0, test_1, testreg);
246 //br i1 %test.%d, label %main.%d, label %main.%d
247 BasicBlock *nextbb = BasicBlock::Create(label, brainf_func);
248 builder->CreateCondBr(test_2, aberrorbb, nextbb);
251 builder->SetInsertPoint(nextbb);
258 //%tape.%d = load i8 *%head.%d
259 LoadInst *tape_0 = builder->CreateLoad(curhead, tapereg);
261 //%tape.%d = add i8 %tape.%d, %d
262 Value *tape_1 = builder->
263 CreateAdd(tape_0, ConstantInt::get(APInt(8, curvalue)), tapereg);
265 //store i8 %tape.%d, i8 *%head.%d\n"
266 builder->CreateStore(tape_1, curhead);
273 BasicBlock *testbb = BasicBlock::Create(label, brainf_func);
274 builder->CreateBr(testbb);
277 BasicBlock *bb_0 = builder->GetInsertBlock();
278 BasicBlock *bb_1 = BasicBlock::Create(label, brainf_func);
279 builder->SetInsertPoint(bb_1);
281 // Make part of PHI instruction now, wait until end of loop to finish
283 PHINode::Create(PointerType::getUnqual(IntegerType::Int8Ty),
285 phi_0->reserveOperandSpace(2);
286 phi_0->addIncoming(curhead, bb_0);
289 readloop(phi_0, bb_1, testbb);
294 cerr<<"Error: Unknown symbol.\n";
300 curvalue = nextvalue;
303 // Reading stdin loop
304 loop = (cursym == SYM_NONE)
305 || (cursym == SYM_MOVE)
306 || (cursym == SYM_CHANGE);
310 if (cursym == SYM_NONE) {
324 if (cursym == SYM_CHANGE) {
325 curvalue += direction;
328 if (cursym == SYM_NONE) {
330 curvalue = direction;
333 nextsym = SYM_CHANGE;
334 nextvalue = direction;
345 if (cursym == SYM_MOVE) {
346 curvalue += direction;
349 if (cursym == SYM_NONE) {
351 curvalue = direction;
355 nextvalue = direction;
362 if (cursym == SYM_NONE) {
371 if (cursym == SYM_NONE) {
380 if (cursym == SYM_NONE) {
389 if (cursym == SYM_NONE) {
390 cursym = SYM_ENDLOOP;
392 nextsym = SYM_ENDLOOP;
397 // Ignore other characters
405 if (cursym == SYM_ENDLOOP) {
407 cerr<<"Error: Extra ']'\n";
414 builder->CreateBr(testbb);
418 //%head.%d = phi i8 *[%head.%d, %main.%d], [%head.%d, %main.%d]
419 //Finish phi made at beginning of loop
420 phi->addIncoming(curhead, builder->GetInsertBlock());
423 //%tape.%d = load i8 *%head.%d
424 LoadInst *tape_0 = new LoadInst(head_0, tapereg, testbb);
426 //%test.%d = icmp eq i8 %tape.%d, 0
427 ICmpInst *test_0 = new ICmpInst(ICmpInst::ICMP_EQ, tape_0,
428 ConstantInt::get(APInt(8, 0)), testreg,
431 //br i1 %test.%d, label %main.%d, label %main.%d
432 BasicBlock *bb_0 = BasicBlock::Create(label, brainf_func);
433 BranchInst::Create(bb_0, oldbb, test_0, testbb);
436 builder->SetInsertPoint(bb_0);
438 //%head.%d = phi i8 *[%head.%d, %main.%d]
439 PHINode *phi_1 = builder->
440 CreatePHI(PointerType::getUnqual(IntegerType::Int8Ty), headreg);
441 phi_1->reserveOperandSpace(1);
442 phi_1->addIncoming(head_0, testbb);
449 //End of the program, so go to return block
450 builder->CreateBr(endbb);
453 cerr<<"Error: Missing ']'\n";