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/Instructions.h"
29 #include "llvm/Intrinsics.h"
30 #include "llvm/ADT/STLExtras.h"
34 //Set the constants for naming
35 const char *BrainF::tapereg
= "tape";
36 const char *BrainF::headreg
= "head";
37 const char *BrainF::label
= "brainf";
38 const char *BrainF::testreg
= "test";
40 Module
*BrainF::parse(std::istream
*in1
, int mem
, CompileFlags cf
,
41 LLVMContext
& Context
) {
47 readloop(0, 0, 0, Context
);
52 void BrainF::header(LLVMContext
& C
) {
53 module
= new Module("BrainF", C
);
57 //declare void @llvm.memset.p0i8.i32(i8 *, i8, i32, i32, i1)
58 const Type
*Tys
[] = { Type::getInt8PtrTy(C
), Type::getInt32Ty(C
) };
59 Function
*memset_func
= Intrinsic::getDeclaration(module
, Intrinsic::memset
,
62 //declare i32 @getchar()
63 getchar_func
= cast
<Function
>(module
->
64 getOrInsertFunction("getchar", IntegerType::getInt32Ty(C
), NULL
));
66 //declare i32 @putchar(i32)
67 putchar_func
= cast
<Function
>(module
->
68 getOrInsertFunction("putchar", IntegerType::getInt32Ty(C
),
69 IntegerType::getInt32Ty(C
), NULL
));
74 //define void @brainf()
75 brainf_func
= cast
<Function
>(module
->
76 getOrInsertFunction("brainf", Type::getVoidTy(C
), NULL
));
78 builder
= new IRBuilder
<>(BasicBlock::Create(C
, label
, brainf_func
));
80 //%arr = malloc i8, i32 %d
81 ConstantInt
*val_mem
= ConstantInt::get(C
, APInt(32, memtotal
));
82 BasicBlock
* BB
= builder
->GetInsertBlock();
83 const Type
* IntPtrTy
= IntegerType::getInt32Ty(C
);
84 const Type
* Int8Ty
= IntegerType::getInt8Ty(C
);
85 Constant
* allocsize
= ConstantExpr::getSizeOf(Int8Ty
);
86 allocsize
= ConstantExpr::getTruncOrBitCast(allocsize
, IntPtrTy
);
87 ptr_arr
= CallInst::CreateMalloc(BB
, IntPtrTy
, Int8Ty
, allocsize
, val_mem
,
89 BB
->getInstList().push_back(cast
<Instruction
>(ptr_arr
));
91 //call void @llvm.memset.p0i8.i32(i8 *%arr, i8 0, i32 %d, i32 1, i1 0)
93 Value
*memset_params
[] = {
95 ConstantInt::get(C
, APInt(8, 0)),
97 ConstantInt::get(C
, APInt(32, 1)),
98 ConstantInt::get(C
, APInt(1, 0))
101 CallInst
*memset_call
= builder
->
102 CreateCall(memset_func
, memset_params
, array_endof(memset_params
));
103 memset_call
->setTailCall(false);
106 //%arrmax = getelementptr i8 *%arr, i32 %d
107 if (comflag
& flag_arraybounds
) {
108 ptr_arrmax
= builder
->
109 CreateGEP(ptr_arr
, ConstantInt::get(C
, APInt(32, memtotal
)), "arrmax");
112 //%head.%d = getelementptr i8 *%arr, i32 %d
113 curhead
= builder
->CreateGEP(ptr_arr
,
114 ConstantInt::get(C
, APInt(32, memtotal
/2)),
122 endbb
= BasicBlock::Create(C
, label
, brainf_func
);
124 //call free(i8 *%arr)
125 endbb
->getInstList().push_back(CallInst::CreateFree(ptr_arr
, endbb
));
128 ReturnInst::Create(C
, endbb
);
132 //Error block for array out of bounds
133 if (comflag
& flag_arraybounds
)
135 //@aberrormsg = internal constant [%d x i8] c"\00"
137 ConstantArray::get(C
, "Error: The head has left the tape.", true);
139 GlobalVariable
*aberrormsg
= new GlobalVariable(
143 GlobalValue::InternalLinkage
,
147 //declare i32 @puts(i8 *)
148 Function
*puts_func
= cast
<Function
>(module
->
149 getOrInsertFunction("puts", IntegerType::getInt32Ty(C
),
150 PointerType::getUnqual(IntegerType::getInt8Ty(C
)), NULL
));
153 aberrorbb
= BasicBlock::Create(C
, label
, brainf_func
);
155 //call i32 @puts(i8 *getelementptr([%d x i8] *@aberrormsg, i32 0, i32 0))
157 Constant
*zero_32
= Constant::getNullValue(IntegerType::getInt32Ty(C
));
159 Constant
*gep_params
[] = {
164 Constant
*msgptr
= ConstantExpr::
165 getGetElementPtr(aberrormsg
, gep_params
,
166 array_lengthof(gep_params
));
168 Value
*puts_params
[] = {
172 CallInst
*puts_call
=
173 CallInst::Create(puts_func
,
174 puts_params
, array_endof(puts_params
),
176 puts_call
->setTailCall(false);
179 //br label %brainf.end
180 BranchInst::Create(endbb
, aberrorbb
);
184 void BrainF::readloop(PHINode
*phi
, BasicBlock
*oldbb
, BasicBlock
*testbb
,
186 Symbol cursym
= SYM_NONE
;
188 Symbol nextsym
= SYM_NONE
;
194 while(cursym
!= SYM_EOF
&& cursym
!= SYM_ENDLOOP
) {
195 // Write out commands
203 //%tape.%d = call i32 @getchar()
204 CallInst
*getchar_call
= builder
->CreateCall(getchar_func
, tapereg
);
205 getchar_call
->setTailCall(false);
206 Value
*tape_0
= getchar_call
;
208 //%tape.%d = trunc i32 %tape.%d to i8
209 Value
*tape_1
= builder
->
210 CreateTrunc(tape_0
, IntegerType::getInt8Ty(C
), tapereg
);
212 //store i8 %tape.%d, i8 *%head.%d
213 builder
->CreateStore(tape_1
, curhead
);
219 //%tape.%d = load i8 *%head.%d
220 LoadInst
*tape_0
= builder
->CreateLoad(curhead
, tapereg
);
222 //%tape.%d = sext i8 %tape.%d to i32
223 Value
*tape_1
= builder
->
224 CreateSExt(tape_0
, IntegerType::getInt32Ty(C
), tapereg
);
226 //call i32 @putchar(i32 %tape.%d)
227 Value
*putchar_params
[] = {
230 CallInst
*putchar_call
= builder
->
231 CreateCall(putchar_func
,
232 putchar_params
, array_endof(putchar_params
));
233 putchar_call
->setTailCall(false);
239 //%head.%d = getelementptr i8 *%head.%d, i32 %d
241 CreateGEP(curhead
, ConstantInt::get(C
, APInt(32, curvalue
)),
244 //Error block for array out of bounds
245 if (comflag
& flag_arraybounds
)
247 //%test.%d = icmp uge i8 *%head.%d, %arrmax
248 Value
*test_0
= builder
->
249 CreateICmpUGE(curhead
, ptr_arrmax
, testreg
);
251 //%test.%d = icmp ult i8 *%head.%d, %arr
252 Value
*test_1
= builder
->
253 CreateICmpULT(curhead
, ptr_arr
, testreg
);
255 //%test.%d = or i1 %test.%d, %test.%d
256 Value
*test_2
= builder
->
257 CreateOr(test_0
, test_1
, testreg
);
259 //br i1 %test.%d, label %main.%d, label %main.%d
260 BasicBlock
*nextbb
= BasicBlock::Create(C
, label
, brainf_func
);
261 builder
->CreateCondBr(test_2
, aberrorbb
, nextbb
);
264 builder
->SetInsertPoint(nextbb
);
271 //%tape.%d = load i8 *%head.%d
272 LoadInst
*tape_0
= builder
->CreateLoad(curhead
, tapereg
);
274 //%tape.%d = add i8 %tape.%d, %d
275 Value
*tape_1
= builder
->
276 CreateAdd(tape_0
, ConstantInt::get(C
, APInt(8, curvalue
)), tapereg
);
278 //store i8 %tape.%d, i8 *%head.%d\n"
279 builder
->CreateStore(tape_1
, curhead
);
286 BasicBlock
*testbb
= BasicBlock::Create(C
, label
, brainf_func
);
287 builder
->CreateBr(testbb
);
290 BasicBlock
*bb_0
= builder
->GetInsertBlock();
291 BasicBlock
*bb_1
= BasicBlock::Create(C
, label
, brainf_func
);
292 builder
->SetInsertPoint(bb_1
);
294 // Make part of PHI instruction now, wait until end of loop to finish
296 PHINode::Create(PointerType::getUnqual(IntegerType::getInt8Ty(C
)),
298 phi_0
->addIncoming(curhead
, bb_0
);
301 readloop(phi_0
, bb_1
, testbb
, C
);
306 std::cerr
<< "Error: Unknown symbol.\n";
312 curvalue
= nextvalue
;
315 // Reading stdin loop
316 loop
= (cursym
== SYM_NONE
)
317 || (cursym
== SYM_MOVE
)
318 || (cursym
== SYM_CHANGE
);
322 if (cursym
== SYM_NONE
) {
336 if (cursym
== SYM_CHANGE
) {
337 curvalue
+= direction
;
340 if (cursym
== SYM_NONE
) {
342 curvalue
= direction
;
345 nextsym
= SYM_CHANGE
;
346 nextvalue
= direction
;
357 if (cursym
== SYM_MOVE
) {
358 curvalue
+= direction
;
361 if (cursym
== SYM_NONE
) {
363 curvalue
= direction
;
367 nextvalue
= direction
;
374 if (cursym
== SYM_NONE
) {
383 if (cursym
== SYM_NONE
) {
392 if (cursym
== SYM_NONE
) {
401 if (cursym
== SYM_NONE
) {
402 cursym
= SYM_ENDLOOP
;
404 nextsym
= SYM_ENDLOOP
;
409 // Ignore other characters
417 if (cursym
== SYM_ENDLOOP
) {
419 std::cerr
<< "Error: Extra ']'\n";
426 builder
->CreateBr(testbb
);
430 //%head.%d = phi i8 *[%head.%d, %main.%d], [%head.%d, %main.%d]
431 //Finish phi made at beginning of loop
432 phi
->addIncoming(curhead
, builder
->GetInsertBlock());
435 //%tape.%d = load i8 *%head.%d
436 LoadInst
*tape_0
= new LoadInst(head_0
, tapereg
, testbb
);
438 //%test.%d = icmp eq i8 %tape.%d, 0
439 ICmpInst
*test_0
= new ICmpInst(*testbb
, ICmpInst::ICMP_EQ
, tape_0
,
440 ConstantInt::get(C
, APInt(8, 0)), testreg
);
442 //br i1 %test.%d, label %main.%d, label %main.%d
443 BasicBlock
*bb_0
= BasicBlock::Create(C
, label
, brainf_func
);
444 BranchInst::Create(bb_0
, oldbb
, test_0
, testbb
);
447 builder
->SetInsertPoint(bb_0
);
449 //%head.%d = phi i8 *[%head.%d, %main.%d]
450 PHINode
*phi_1
= builder
->
451 CreatePHI(PointerType::getUnqual(IntegerType::getInt8Ty(C
)), 1,
453 phi_1
->addIncoming(head_0
, testbb
);
460 //End of the program, so go to return block
461 builder
->CreateBr(endbb
);
464 std::cerr
<< "Error: Missing ']'\n";