X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=examples%2FBrainF%2FBrainF.cpp;h=f8129b819e3a6da2a3081a0a140a44a8232816f6;hb=7172b38af7ed5d1c1e2c97fadfb0ae0c19aff816;hp=5cf2b883bc480783889ad47e75346391aae7a2cc;hpb=1d0be15f89cb5056e20e2d24faa8d6afb1573bca;p=oota-llvm.git diff --git a/examples/BrainF/BrainF.cpp b/examples/BrainF/BrainF.cpp index 5cf2b883bc4..f8129b819e3 100644 --- a/examples/BrainF/BrainF.cpp +++ b/examples/BrainF/BrainF.cpp @@ -24,9 +24,10 @@ //===--------------------------------------------------------------------===// #include "BrainF.h" -#include "llvm/Constants.h" -#include "llvm/Intrinsics.h" #include "llvm/ADT/STLExtras.h" +#include "llvm/IR/Constants.h" +#include "llvm/IR/Instructions.h" +#include "llvm/IR/Intrinsics.h" #include using namespace llvm; @@ -53,10 +54,10 @@ void BrainF::header(LLVMContext& C) { //Function prototypes - //declare void @llvm.memset.i32(i8 *, i8, i32, i32) - const Type *Tys[] = { Type::getInt32Ty(C) }; + //declare void @llvm.memset.p0i8.i32(i8 *, i8, i32, i32, i1) + Type *Tys[] = { Type::getInt8PtrTy(C), Type::getInt32Ty(C) }; Function *memset_func = Intrinsic::getDeclaration(module, Intrinsic::memset, - Tys, 1); + Tys); //declare i32 @getchar() getchar_func = cast(module-> @@ -78,19 +79,27 @@ void BrainF::header(LLVMContext& C) { //%arr = malloc i8, i32 %d ConstantInt *val_mem = ConstantInt::get(C, APInt(32, memtotal)); - ptr_arr = builder->CreateMalloc(IntegerType::getInt8Ty(C), val_mem, "arr"); - - //call void @llvm.memset.i32(i8 *%arr, i8 0, i32 %d, i32 1) + BasicBlock* BB = builder->GetInsertBlock(); + Type* IntPtrTy = IntegerType::getInt32Ty(C); + Type* Int8Ty = IntegerType::getInt8Ty(C); + Constant* allocsize = ConstantExpr::getSizeOf(Int8Ty); + allocsize = ConstantExpr::getTruncOrBitCast(allocsize, IntPtrTy); + ptr_arr = CallInst::CreateMalloc(BB, IntPtrTy, Int8Ty, allocsize, val_mem, + NULL, "arr"); + BB->getInstList().push_back(cast(ptr_arr)); + + //call void @llvm.memset.p0i8.i32(i8 *%arr, i8 0, i32 %d, i32 1, i1 0) { Value *memset_params[] = { ptr_arr, ConstantInt::get(C, APInt(8, 0)), val_mem, - ConstantInt::get(C, APInt(32, 1)) + ConstantInt::get(C, APInt(32, 1)), + ConstantInt::get(C, APInt(1, 0)) }; CallInst *memset_call = builder-> - CreateCall(memset_func, memset_params, array_endof(memset_params)); + CreateCall(memset_func, memset_params); memset_call->setTailCall(false); } @@ -112,8 +121,8 @@ void BrainF::header(LLVMContext& C) { //brainf.end: endbb = BasicBlock::Create(C, label, brainf_func); - //free i8 *%arr - new FreeInst(ptr_arr, endbb); + //call free(i8 *%arr) + endbb->getInstList().push_back(CallInst::CreateFree(ptr_arr, endbb)); //ret void ReturnInst::Create(C, endbb); @@ -125,7 +134,8 @@ void BrainF::header(LLVMContext& C) { { //@aberrormsg = internal constant [%d x i8] c"\00" Constant *msg_0 = - ConstantArray::get(C, "Error: The head has left the tape.", true); + ConstantDataArray::getString(C, "Error: The head has left the tape.", + true); GlobalVariable *aberrormsg = new GlobalVariable( *module, @@ -153,8 +163,7 @@ void BrainF::header(LLVMContext& C) { }; Constant *msgptr = ConstantExpr:: - getGetElementPtr(aberrormsg, gep_params, - array_lengthof(gep_params)); + getGetElementPtr(aberrormsg, gep_params); Value *puts_params[] = { msgptr @@ -162,7 +171,7 @@ void BrainF::header(LLVMContext& C) { CallInst *puts_call = CallInst::Create(puts_func, - puts_params, array_endof(puts_params), + puts_params, "", aberrorbb); puts_call->setTailCall(false); } @@ -220,7 +229,7 @@ void BrainF::readloop(PHINode *phi, BasicBlock *oldbb, BasicBlock *testbb, }; CallInst *putchar_call = builder-> CreateCall(putchar_func, - putchar_params, array_endof(putchar_params)); + putchar_params); putchar_call->setTailCall(false); } break; @@ -285,8 +294,7 @@ void BrainF::readloop(PHINode *phi, BasicBlock *oldbb, BasicBlock *testbb, // Make part of PHI instruction now, wait until end of loop to finish PHINode *phi_0 = PHINode::Create(PointerType::getUnqual(IntegerType::getInt8Ty(C)), - headreg, testbb); - phi_0->reserveOperandSpace(2); + 2, headreg, testbb); phi_0->addIncoming(curhead, bb_0); curhead = phi_0; @@ -440,8 +448,8 @@ void BrainF::readloop(PHINode *phi, BasicBlock *oldbb, BasicBlock *testbb, //%head.%d = phi i8 *[%head.%d, %main.%d] PHINode *phi_1 = builder-> - CreatePHI(PointerType::getUnqual(IntegerType::getInt8Ty(C)), headreg); - phi_1->reserveOperandSpace(1); + CreatePHI(PointerType::getUnqual(IntegerType::getInt8Ty(C)), 1, + headreg); phi_1->addIncoming(head_0, testbb); curhead = phi_1; }