Support using DebugLoc's in a DenseMap.
[llvm/stm8.git] / examples / BrainF / BrainF.cpp
blob54f35535b57d60b2e08cccfd753b262b89ec4ab5
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/Instructions.h"
29 #include "llvm/Intrinsics.h"
30 #include "llvm/ADT/STLExtras.h"
31 #include <iostream>
32 using namespace llvm;
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) {
42 in = in1;
43 memtotal = mem;
44 comflag = cf;
46 header(Context);
47 readloop(0, 0, 0, Context);
48 delete builder;
49 return module;
52 void BrainF::header(LLVMContext& C) {
53 module = new Module("BrainF", C);
55 //Function prototypes
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,
60 Tys, 2);
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));
72 //Function header
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,
88 NULL, "arr");
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[] = {
94 ptr_arr,
95 ConstantInt::get(C, APInt(8, 0)),
96 val_mem,
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)),
115 headreg);
119 //Function footer
121 //brainf.end:
122 endbb = BasicBlock::Create(C, label, brainf_func);
124 //call free(i8 *%arr)
125 endbb->getInstList().push_back(CallInst::CreateFree(ptr_arr, endbb));
127 //ret void
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"
136 Constant *msg_0 =
137 ConstantArray::get(C, "Error: The head has left the tape.", true);
139 GlobalVariable *aberrormsg = new GlobalVariable(
140 *module,
141 msg_0->getType(),
142 true,
143 GlobalValue::InternalLinkage,
144 msg_0,
145 "aberrormsg");
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));
152 //brainf.aberror:
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[] = {
160 zero_32,
161 zero_32
164 Constant *msgptr = ConstantExpr::
165 getGetElementPtr(aberrormsg, gep_params,
166 array_lengthof(gep_params));
168 Value *puts_params[] = {
169 msgptr
172 CallInst *puts_call =
173 CallInst::Create(puts_func,
174 puts_params, array_endof(puts_params),
175 "", aberrorbb);
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,
185 LLVMContext &C) {
186 Symbol cursym = SYM_NONE;
187 int curvalue = 0;
188 Symbol nextsym = SYM_NONE;
189 int nextvalue = 0;
190 char c;
191 int loop;
192 int direction;
194 while(cursym != SYM_EOF && cursym != SYM_ENDLOOP) {
195 // Write out commands
196 switch(cursym) {
197 case SYM_NONE:
198 // Do nothing
199 break;
201 case SYM_READ:
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);
215 break;
217 case SYM_WRITE:
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[] = {
228 tape_1
230 CallInst *putchar_call = builder->
231 CreateCall(putchar_func,
232 putchar_params, array_endof(putchar_params));
233 putchar_call->setTailCall(false);
235 break;
237 case SYM_MOVE:
239 //%head.%d = getelementptr i8 *%head.%d, i32 %d
240 curhead = builder->
241 CreateGEP(curhead, ConstantInt::get(C, APInt(32, curvalue)),
242 headreg);
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);
263 //main.%d:
264 builder->SetInsertPoint(nextbb);
267 break;
269 case SYM_CHANGE:
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);
281 break;
283 case SYM_LOOP:
285 //br label %main.%d
286 BasicBlock *testbb = BasicBlock::Create(C, label, brainf_func);
287 builder->CreateBr(testbb);
289 //main.%d:
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
295 PHINode *phi_0 =
296 PHINode::Create(PointerType::getUnqual(IntegerType::getInt8Ty(C)),
297 2, headreg, testbb);
298 phi_0->addIncoming(curhead, bb_0);
299 curhead = phi_0;
301 readloop(phi_0, bb_1, testbb, C);
303 break;
305 default:
306 std::cerr << "Error: Unknown symbol.\n";
307 abort();
308 break;
311 cursym = nextsym;
312 curvalue = nextvalue;
313 nextsym = SYM_NONE;
315 // Reading stdin loop
316 loop = (cursym == SYM_NONE)
317 || (cursym == SYM_MOVE)
318 || (cursym == SYM_CHANGE);
319 while(loop) {
320 *in>>c;
321 if (in->eof()) {
322 if (cursym == SYM_NONE) {
323 cursym = SYM_EOF;
324 } else {
325 nextsym = SYM_EOF;
327 loop = 0;
328 } else {
329 direction = 1;
330 switch(c) {
331 case '-':
332 direction = -1;
333 // Fall through
335 case '+':
336 if (cursym == SYM_CHANGE) {
337 curvalue += direction;
338 // loop = 1
339 } else {
340 if (cursym == SYM_NONE) {
341 cursym = SYM_CHANGE;
342 curvalue = direction;
343 // loop = 1
344 } else {
345 nextsym = SYM_CHANGE;
346 nextvalue = direction;
347 loop = 0;
350 break;
352 case '<':
353 direction = -1;
354 // Fall through
356 case '>':
357 if (cursym == SYM_MOVE) {
358 curvalue += direction;
359 // loop = 1
360 } else {
361 if (cursym == SYM_NONE) {
362 cursym = SYM_MOVE;
363 curvalue = direction;
364 // loop = 1
365 } else {
366 nextsym = SYM_MOVE;
367 nextvalue = direction;
368 loop = 0;
371 break;
373 case ',':
374 if (cursym == SYM_NONE) {
375 cursym = SYM_READ;
376 } else {
377 nextsym = SYM_READ;
379 loop = 0;
380 break;
382 case '.':
383 if (cursym == SYM_NONE) {
384 cursym = SYM_WRITE;
385 } else {
386 nextsym = SYM_WRITE;
388 loop = 0;
389 break;
391 case '[':
392 if (cursym == SYM_NONE) {
393 cursym = SYM_LOOP;
394 } else {
395 nextsym = SYM_LOOP;
397 loop = 0;
398 break;
400 case ']':
401 if (cursym == SYM_NONE) {
402 cursym = SYM_ENDLOOP;
403 } else {
404 nextsym = SYM_ENDLOOP;
406 loop = 0;
407 break;
409 // Ignore other characters
410 default:
411 break;
417 if (cursym == SYM_ENDLOOP) {
418 if (!phi) {
419 std::cerr << "Error: Extra ']'\n";
420 abort();
423 // Write loop test
425 //br label %main.%d
426 builder->CreateBr(testbb);
428 //main.%d:
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());
433 Value *head_0 = phi;
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);
446 //main.%d:
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,
452 headreg);
453 phi_1->addIncoming(head_0, testbb);
454 curhead = phi_1;
457 return;
460 //End of the program, so go to return block
461 builder->CreateBr(endbb);
463 if (phi) {
464 std::cerr << "Error: Missing ']'\n";
465 abort();