Mercurial > hg > CbC > CbC_llvm
comparison examples/BrainF/BrainF.cpp @ 95:afa8332a0e37 LLVM3.8
LLVM 3.8
author | Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 13 Oct 2015 17:48:58 +0900 |
parents | 95c75e76d11b |
children | 1172e4bd9c6f |
comparison
equal
deleted
inserted
replaced
84:f3e34b893a5f | 95:afa8332a0e37 |
---|---|
27 #include "llvm/ADT/STLExtras.h" | 27 #include "llvm/ADT/STLExtras.h" |
28 #include "llvm/IR/Constants.h" | 28 #include "llvm/IR/Constants.h" |
29 #include "llvm/IR/Instructions.h" | 29 #include "llvm/IR/Instructions.h" |
30 #include "llvm/IR/Intrinsics.h" | 30 #include "llvm/IR/Intrinsics.h" |
31 #include <iostream> | 31 #include <iostream> |
32 | |
32 using namespace llvm; | 33 using namespace llvm; |
33 | 34 |
34 //Set the constants for naming | 35 //Set the constants for naming |
35 const char *BrainF::tapereg = "tape"; | 36 const char *BrainF::tapereg = "tape"; |
36 const char *BrainF::headreg = "head"; | 37 const char *BrainF::headreg = "head"; |
42 in = in1; | 43 in = in1; |
43 memtotal = mem; | 44 memtotal = mem; |
44 comflag = cf; | 45 comflag = cf; |
45 | 46 |
46 header(Context); | 47 header(Context); |
47 readloop(0, 0, 0, Context); | 48 readloop(nullptr, nullptr, nullptr, Context); |
48 delete builder; | 49 delete builder; |
49 return module; | 50 return module; |
50 } | 51 } |
51 | 52 |
52 void BrainF::header(LLVMContext& C) { | 53 void BrainF::header(LLVMContext& C) { |
65 | 66 |
66 //declare i32 @putchar(i32) | 67 //declare i32 @putchar(i32) |
67 putchar_func = cast<Function>(module-> | 68 putchar_func = cast<Function>(module-> |
68 getOrInsertFunction("putchar", IntegerType::getInt32Ty(C), | 69 getOrInsertFunction("putchar", IntegerType::getInt32Ty(C), |
69 IntegerType::getInt32Ty(C), NULL)); | 70 IntegerType::getInt32Ty(C), NULL)); |
70 | |
71 | 71 |
72 //Function header | 72 //Function header |
73 | 73 |
74 //define void @brainf() | 74 //define void @brainf() |
75 brainf_func = cast<Function>(module-> | 75 brainf_func = cast<Function>(module-> |
83 Type* IntPtrTy = IntegerType::getInt32Ty(C); | 83 Type* IntPtrTy = IntegerType::getInt32Ty(C); |
84 Type* Int8Ty = IntegerType::getInt8Ty(C); | 84 Type* Int8Ty = IntegerType::getInt8Ty(C); |
85 Constant* allocsize = ConstantExpr::getSizeOf(Int8Ty); | 85 Constant* allocsize = ConstantExpr::getSizeOf(Int8Ty); |
86 allocsize = ConstantExpr::getTruncOrBitCast(allocsize, IntPtrTy); | 86 allocsize = ConstantExpr::getTruncOrBitCast(allocsize, IntPtrTy); |
87 ptr_arr = CallInst::CreateMalloc(BB, IntPtrTy, Int8Ty, allocsize, val_mem, | 87 ptr_arr = CallInst::CreateMalloc(BB, IntPtrTy, Int8Ty, allocsize, val_mem, |
88 NULL, "arr"); | 88 nullptr, "arr"); |
89 BB->getInstList().push_back(cast<Instruction>(ptr_arr)); | 89 BB->getInstList().push_back(cast<Instruction>(ptr_arr)); |
90 | 90 |
91 //call void @llvm.memset.p0i8.i32(i8 *%arr, i8 0, i32 %d, i32 1, i1 0) | 91 //call void @llvm.memset.p0i8.i32(i8 *%arr, i8 0, i32 %d, i32 1, i1 0) |
92 { | 92 { |
93 Value *memset_params[] = { | 93 Value *memset_params[] = { |
112 //%head.%d = getelementptr i8 *%arr, i32 %d | 112 //%head.%d = getelementptr i8 *%arr, i32 %d |
113 curhead = builder->CreateGEP(ptr_arr, | 113 curhead = builder->CreateGEP(ptr_arr, |
114 ConstantInt::get(C, APInt(32, memtotal/2)), | 114 ConstantInt::get(C, APInt(32, memtotal/2)), |
115 headreg); | 115 headreg); |
116 | 116 |
117 | |
118 | |
119 //Function footer | 117 //Function footer |
120 | 118 |
121 //brainf.end: | 119 //brainf.end: |
122 endbb = BasicBlock::Create(C, label, brainf_func); | 120 endbb = BasicBlock::Create(C, label, brainf_func); |
123 | 121 |
124 //call free(i8 *%arr) | 122 //call free(i8 *%arr) |
125 endbb->getInstList().push_back(CallInst::CreateFree(ptr_arr, endbb)); | 123 endbb->getInstList().push_back(CallInst::CreateFree(ptr_arr, endbb)); |
126 | 124 |
127 //ret void | 125 //ret void |
128 ReturnInst::Create(C, endbb); | 126 ReturnInst::Create(C, endbb); |
129 | |
130 | |
131 | 127 |
132 //Error block for array out of bounds | 128 //Error block for array out of bounds |
133 if (comflag & flag_arraybounds) | 129 if (comflag & flag_arraybounds) |
134 { | 130 { |
135 //@aberrormsg = internal constant [%d x i8] c"\00" | 131 //@aberrormsg = internal constant [%d x i8] c"\00" |
161 zero_32, | 157 zero_32, |
162 zero_32 | 158 zero_32 |
163 }; | 159 }; |
164 | 160 |
165 Constant *msgptr = ConstantExpr:: | 161 Constant *msgptr = ConstantExpr:: |
166 getGetElementPtr(aberrormsg, gep_params); | 162 getGetElementPtr(aberrormsg->getValueType(), aberrormsg, gep_params); |
167 | 163 |
168 Value *puts_params[] = { | 164 Value *puts_params[] = { |
169 msgptr | 165 msgptr |
170 }; | 166 }; |
171 | 167 |
199 break; | 195 break; |
200 | 196 |
201 case SYM_READ: | 197 case SYM_READ: |
202 { | 198 { |
203 //%tape.%d = call i32 @getchar() | 199 //%tape.%d = call i32 @getchar() |
204 CallInst *getchar_call = builder->CreateCall(getchar_func, tapereg); | 200 CallInst *getchar_call = |
201 builder->CreateCall(getchar_func, {}, tapereg); | |
205 getchar_call->setTailCall(false); | 202 getchar_call->setTailCall(false); |
206 Value *tape_0 = getchar_call; | 203 Value *tape_0 = getchar_call; |
207 | 204 |
208 //%tape.%d = trunc i32 %tape.%d to i8 | 205 //%tape.%d = trunc i32 %tape.%d to i8 |
209 Value *tape_1 = builder-> | 206 Value *tape_1 = builder-> |