Merge branch 'master' into msp430
[llvm/msp430.git] / examples / BrainF / BrainF.cpp
blob32a14c4d5328bd4260009aa349fc11e75edc32ae
1 //===-- BrainF.cpp - BrainF compiler example ----------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===--------------------------------------------------------------------===//
9 //
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
22 // ] } End loop
24 //===--------------------------------------------------------------------===//
26 #include "BrainF.h"
27 #include "llvm/Constants.h"
28 #include "llvm/Intrinsics.h"
29 #include "llvm/ADT/STLExtras.h"
30 #include <iostream>
31 using namespace llvm;
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) {
40 in = in1;
41 memtotal = mem;
42 comflag = cf;
44 header();
45 readloop(0, 0, 0);
46 delete builder;
47 return module;
50 void BrainF::header() {
51 module = new Module("BrainF");
53 //Function prototypes
55 //declare void @llvm.memset.i32(i8 *, i8, i32, i32)
56 const Type *Tys[] = { Type::Int32Ty };
57 Function *memset_func = Intrinsic::getDeclaration(module, Intrinsic::memset,
58 Tys, 1);
60 //declare i32 @getchar()
61 getchar_func = cast<Function>(module->
62 getOrInsertFunction("getchar", IntegerType::Int32Ty, NULL));
64 //declare i32 @putchar(i32)
65 putchar_func = cast<Function>(module->
66 getOrInsertFunction("putchar", IntegerType::Int32Ty,
67 IntegerType::Int32Ty, NULL));
70 //Function header
72 //define void @brainf()
73 brainf_func = cast<Function>(module->
74 getOrInsertFunction("brainf", Type::VoidTy, NULL));
76 builder = new IRBuilder<>(BasicBlock::Create(label, brainf_func));
78 //%arr = malloc i8, i32 %d
79 ConstantInt *val_mem = ConstantInt::get(APInt(32, memtotal));
80 ptr_arr = builder->CreateMalloc(IntegerType::Int8Ty, val_mem, "arr");
82 //call void @llvm.memset.i32(i8 *%arr, i8 0, i32 %d, i32 1)
84 Value *memset_params[] = {
85 ptr_arr,
86 ConstantInt::get(APInt(8, 0)),
87 val_mem,
88 ConstantInt::get(APInt(32, 1))
91 CallInst *memset_call = builder->
92 CreateCall(memset_func, memset_params, array_endof(memset_params));
93 memset_call->setTailCall(false);
96 //%arrmax = getelementptr i8 *%arr, i32 %d
97 if (comflag & flag_arraybounds) {
98 ptr_arrmax = builder->
99 CreateGEP(ptr_arr, ConstantInt::get(APInt(32, memtotal)), "arrmax");
102 //%head.%d = getelementptr i8 *%arr, i32 %d
103 curhead = builder->CreateGEP(ptr_arr,
104 ConstantInt::get(APInt(32, memtotal/2)),
105 headreg);
109 //Function footer
111 //brainf.end:
112 endbb = BasicBlock::Create(label, brainf_func);
114 //free i8 *%arr
115 new FreeInst(ptr_arr, endbb);
117 //ret void
118 ReturnInst::Create(endbb);
122 //Error block for array out of bounds
123 if (comflag & flag_arraybounds)
125 //@aberrormsg = internal constant [%d x i8] c"\00"
126 Constant *msg_0 = ConstantArray::
127 get("Error: The head has left the tape.", true);
129 GlobalVariable *aberrormsg = new GlobalVariable(
130 msg_0->getType(),
131 true,
132 GlobalValue::InternalLinkage,
133 msg_0,
134 "aberrormsg",
135 module);
137 //declare i32 @puts(i8 *)
138 Function *puts_func = cast<Function>(module->
139 getOrInsertFunction("puts", IntegerType::Int32Ty,
140 PointerType::getUnqual(IntegerType::Int8Ty), NULL));
142 //brainf.aberror:
143 aberrorbb = BasicBlock::Create(label, brainf_func);
145 //call i32 @puts(i8 *getelementptr([%d x i8] *@aberrormsg, i32 0, i32 0))
147 Constant *zero_32 = Constant::getNullValue(IntegerType::Int32Ty);
149 Constant *gep_params[] = {
150 zero_32,
151 zero_32
154 Constant *msgptr = ConstantExpr::
155 getGetElementPtr(aberrormsg, gep_params,
156 array_lengthof(gep_params));
158 Value *puts_params[] = {
159 msgptr
162 CallInst *puts_call =
163 CallInst::Create(puts_func,
164 puts_params, array_endof(puts_params),
165 "", aberrorbb);
166 puts_call->setTailCall(false);
169 //br label %brainf.end
170 BranchInst::Create(endbb, aberrorbb);
174 void BrainF::readloop(PHINode *phi, BasicBlock *oldbb, BasicBlock *testbb) {
175 Symbol cursym = SYM_NONE;
176 int curvalue = 0;
177 Symbol nextsym = SYM_NONE;
178 int nextvalue = 0;
179 char c;
180 int loop;
181 int direction;
183 while(cursym != SYM_EOF && cursym != SYM_ENDLOOP) {
184 // Write out commands
185 switch(cursym) {
186 case SYM_NONE:
187 // Do nothing
188 break;
190 case SYM_READ:
192 //%tape.%d = call i32 @getchar()
193 CallInst *getchar_call = builder->CreateCall(getchar_func, tapereg);
194 getchar_call->setTailCall(false);
195 Value *tape_0 = getchar_call;
197 //%tape.%d = trunc i32 %tape.%d to i8
198 Value *tape_1 = builder->
199 CreateTrunc(tape_0, IntegerType::Int8Ty, tapereg);
201 //store i8 %tape.%d, i8 *%head.%d
202 builder->CreateStore(tape_1, curhead);
204 break;
206 case SYM_WRITE:
208 //%tape.%d = load i8 *%head.%d
209 LoadInst *tape_0 = builder->CreateLoad(curhead, tapereg);
211 //%tape.%d = sext i8 %tape.%d to i32
212 Value *tape_1 = builder->
213 CreateSExt(tape_0, IntegerType::Int32Ty, tapereg);
215 //call i32 @putchar(i32 %tape.%d)
216 Value *putchar_params[] = {
217 tape_1
219 CallInst *putchar_call = builder->
220 CreateCall(putchar_func,
221 putchar_params, array_endof(putchar_params));
222 putchar_call->setTailCall(false);
224 break;
226 case SYM_MOVE:
228 //%head.%d = getelementptr i8 *%head.%d, i32 %d
229 curhead = builder->
230 CreateGEP(curhead, ConstantInt::get(APInt(32, curvalue)),
231 headreg);
233 //Error block for array out of bounds
234 if (comflag & flag_arraybounds)
236 //%test.%d = icmp uge i8 *%head.%d, %arrmax
237 Value *test_0 = builder->
238 CreateICmpUGE(curhead, ptr_arrmax, testreg);
240 //%test.%d = icmp ult i8 *%head.%d, %arr
241 Value *test_1 = builder->
242 CreateICmpULT(curhead, ptr_arr, testreg);
244 //%test.%d = or i1 %test.%d, %test.%d
245 Value *test_2 = builder->
246 CreateOr(test_0, test_1, testreg);
248 //br i1 %test.%d, label %main.%d, label %main.%d
249 BasicBlock *nextbb = BasicBlock::Create(label, brainf_func);
250 builder->CreateCondBr(test_2, aberrorbb, nextbb);
252 //main.%d:
253 builder->SetInsertPoint(nextbb);
256 break;
258 case SYM_CHANGE:
260 //%tape.%d = load i8 *%head.%d
261 LoadInst *tape_0 = builder->CreateLoad(curhead, tapereg);
263 //%tape.%d = add i8 %tape.%d, %d
264 Value *tape_1 = builder->
265 CreateAdd(tape_0, ConstantInt::get(APInt(8, curvalue)), tapereg);
267 //store i8 %tape.%d, i8 *%head.%d\n"
268 builder->CreateStore(tape_1, curhead);
270 break;
272 case SYM_LOOP:
274 //br label %main.%d
275 BasicBlock *testbb = BasicBlock::Create(label, brainf_func);
276 builder->CreateBr(testbb);
278 //main.%d:
279 BasicBlock *bb_0 = builder->GetInsertBlock();
280 BasicBlock *bb_1 = BasicBlock::Create(label, brainf_func);
281 builder->SetInsertPoint(bb_1);
283 // Make part of PHI instruction now, wait until end of loop to finish
284 PHINode *phi_0 =
285 PHINode::Create(PointerType::getUnqual(IntegerType::Int8Ty),
286 headreg, testbb);
287 phi_0->reserveOperandSpace(2);
288 phi_0->addIncoming(curhead, bb_0);
289 curhead = phi_0;
291 readloop(phi_0, bb_1, testbb);
293 break;
295 default:
296 std::cerr << "Error: Unknown symbol.\n";
297 abort();
298 break;
301 cursym = nextsym;
302 curvalue = nextvalue;
303 nextsym = SYM_NONE;
305 // Reading stdin loop
306 loop = (cursym == SYM_NONE)
307 || (cursym == SYM_MOVE)
308 || (cursym == SYM_CHANGE);
309 while(loop) {
310 *in>>c;
311 if (in->eof()) {
312 if (cursym == SYM_NONE) {
313 cursym = SYM_EOF;
314 } else {
315 nextsym = SYM_EOF;
317 loop = 0;
318 } else {
319 direction = 1;
320 switch(c) {
321 case '-':
322 direction = -1;
323 // Fall through
325 case '+':
326 if (cursym == SYM_CHANGE) {
327 curvalue += direction;
328 // loop = 1
329 } else {
330 if (cursym == SYM_NONE) {
331 cursym = SYM_CHANGE;
332 curvalue = direction;
333 // loop = 1
334 } else {
335 nextsym = SYM_CHANGE;
336 nextvalue = direction;
337 loop = 0;
340 break;
342 case '<':
343 direction = -1;
344 // Fall through
346 case '>':
347 if (cursym == SYM_MOVE) {
348 curvalue += direction;
349 // loop = 1
350 } else {
351 if (cursym == SYM_NONE) {
352 cursym = SYM_MOVE;
353 curvalue = direction;
354 // loop = 1
355 } else {
356 nextsym = SYM_MOVE;
357 nextvalue = direction;
358 loop = 0;
361 break;
363 case ',':
364 if (cursym == SYM_NONE) {
365 cursym = SYM_READ;
366 } else {
367 nextsym = SYM_READ;
369 loop = 0;
370 break;
372 case '.':
373 if (cursym == SYM_NONE) {
374 cursym = SYM_WRITE;
375 } else {
376 nextsym = SYM_WRITE;
378 loop = 0;
379 break;
381 case '[':
382 if (cursym == SYM_NONE) {
383 cursym = SYM_LOOP;
384 } else {
385 nextsym = SYM_LOOP;
387 loop = 0;
388 break;
390 case ']':
391 if (cursym == SYM_NONE) {
392 cursym = SYM_ENDLOOP;
393 } else {
394 nextsym = SYM_ENDLOOP;
396 loop = 0;
397 break;
399 // Ignore other characters
400 default:
401 break;
407 if (cursym == SYM_ENDLOOP) {
408 if (!phi) {
409 std::cerr << "Error: Extra ']'\n";
410 abort();
413 // Write loop test
415 //br label %main.%d
416 builder->CreateBr(testbb);
418 //main.%d:
420 //%head.%d = phi i8 *[%head.%d, %main.%d], [%head.%d, %main.%d]
421 //Finish phi made at beginning of loop
422 phi->addIncoming(curhead, builder->GetInsertBlock());
423 Value *head_0 = phi;
425 //%tape.%d = load i8 *%head.%d
426 LoadInst *tape_0 = new LoadInst(head_0, tapereg, testbb);
428 //%test.%d = icmp eq i8 %tape.%d, 0
429 ICmpInst *test_0 = new ICmpInst(ICmpInst::ICMP_EQ, tape_0,
430 ConstantInt::get(APInt(8, 0)), testreg,
431 testbb);
433 //br i1 %test.%d, label %main.%d, label %main.%d
434 BasicBlock *bb_0 = BasicBlock::Create(label, brainf_func);
435 BranchInst::Create(bb_0, oldbb, test_0, testbb);
437 //main.%d:
438 builder->SetInsertPoint(bb_0);
440 //%head.%d = phi i8 *[%head.%d, %main.%d]
441 PHINode *phi_1 = builder->
442 CreatePHI(PointerType::getUnqual(IntegerType::Int8Ty), headreg);
443 phi_1->reserveOperandSpace(1);
444 phi_1->addIncoming(head_0, testbb);
445 curhead = phi_1;
448 return;
451 //End of the program, so go to return block
452 builder->CreateBr(endbb);
454 if (phi) {
455 std::cerr << "Error: Missing ']'\n";
456 abort();