X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=examples%2FBrainF%2FBrainF.cpp;h=f8129b819e3a6da2a3081a0a140a44a8232816f6;hb=b619dd5d5b69ba9f4571a96e1a96d09d8aed03a7;hp=e1615569d2b9c13c6771f0d73ab496b0489beaf1;hpb=debcb01b0f0a15f568ca69e8f288fade4bfc7297;p=oota-llvm.git diff --git a/examples/BrainF/BrainF.cpp b/examples/BrainF/BrainF.cpp index e1615569d2b..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,44 +54,52 @@ void BrainF::header(LLVMContext& C) { //Function prototypes - //declare void @llvm.memset.i32(i8 *, i8, i32, i32) - const Type *Tys[] = { Type::Int32Ty }; + //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-> - getOrInsertFunction("getchar", IntegerType::Int32Ty, NULL)); + getOrInsertFunction("getchar", IntegerType::getInt32Ty(C), NULL)); //declare i32 @putchar(i32) putchar_func = cast(module-> - getOrInsertFunction("putchar", IntegerType::Int32Ty, - IntegerType::Int32Ty, NULL)); + getOrInsertFunction("putchar", IntegerType::getInt32Ty(C), + IntegerType::getInt32Ty(C), NULL)); //Function header //define void @brainf() brainf_func = cast(module-> - getOrInsertFunction("brainf", Type::VoidTy, NULL)); + getOrInsertFunction("brainf", Type::getVoidTy(C), NULL)); - builder = new IRBuilder<>(BasicBlock::Create(label, brainf_func)); + builder = new IRBuilder<>(BasicBlock::Create(C, label, brainf_func)); //%arr = malloc i8, i32 %d ConstantInt *val_mem = ConstantInt::get(C, APInt(32, memtotal)); - ptr_arr = builder->CreateMalloc(IntegerType::Int8Ty, 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); } @@ -110,13 +119,13 @@ void BrainF::header(LLVMContext& C) { //Function footer //brainf.end: - endbb = BasicBlock::Create(label, brainf_func); + 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(endbb); + 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("Error: The head has left the tape.", true); + ConstantDataArray::getString(C, "Error: The head has left the tape.", + true); GlobalVariable *aberrormsg = new GlobalVariable( *module, @@ -137,15 +147,15 @@ void BrainF::header(LLVMContext& C) { //declare i32 @puts(i8 *) Function *puts_func = cast(module-> - getOrInsertFunction("puts", IntegerType::Int32Ty, - PointerType::getUnqual(IntegerType::Int8Ty), NULL)); + getOrInsertFunction("puts", IntegerType::getInt32Ty(C), + PointerType::getUnqual(IntegerType::getInt8Ty(C)), NULL)); //brainf.aberror: - aberrorbb = BasicBlock::Create(label, brainf_func); + aberrorbb = BasicBlock::Create(C, label, brainf_func); //call i32 @puts(i8 *getelementptr([%d x i8] *@aberrormsg, i32 0, i32 0)) { - Constant *zero_32 = C.getNullValue(IntegerType::Int32Ty); + Constant *zero_32 = Constant::getNullValue(IntegerType::getInt32Ty(C)); Constant *gep_params[] = { zero_32, @@ -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); } @@ -198,7 +207,7 @@ void BrainF::readloop(PHINode *phi, BasicBlock *oldbb, BasicBlock *testbb, //%tape.%d = trunc i32 %tape.%d to i8 Value *tape_1 = builder-> - CreateTrunc(tape_0, IntegerType::Int8Ty, tapereg); + CreateTrunc(tape_0, IntegerType::getInt8Ty(C), tapereg); //store i8 %tape.%d, i8 *%head.%d builder->CreateStore(tape_1, curhead); @@ -212,7 +221,7 @@ void BrainF::readloop(PHINode *phi, BasicBlock *oldbb, BasicBlock *testbb, //%tape.%d = sext i8 %tape.%d to i32 Value *tape_1 = builder-> - CreateSExt(tape_0, IntegerType::Int32Ty, tapereg); + CreateSExt(tape_0, IntegerType::getInt32Ty(C), tapereg); //call i32 @putchar(i32 %tape.%d) Value *putchar_params[] = { @@ -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; @@ -248,7 +257,7 @@ void BrainF::readloop(PHINode *phi, BasicBlock *oldbb, BasicBlock *testbb, CreateOr(test_0, test_1, testreg); //br i1 %test.%d, label %main.%d, label %main.%d - BasicBlock *nextbb = BasicBlock::Create(label, brainf_func); + BasicBlock *nextbb = BasicBlock::Create(C, label, brainf_func); builder->CreateCondBr(test_2, aberrorbb, nextbb); //main.%d: @@ -274,19 +283,18 @@ void BrainF::readloop(PHINode *phi, BasicBlock *oldbb, BasicBlock *testbb, case SYM_LOOP: { //br label %main.%d - BasicBlock *testbb = BasicBlock::Create(label, brainf_func); + BasicBlock *testbb = BasicBlock::Create(C, label, brainf_func); builder->CreateBr(testbb); //main.%d: BasicBlock *bb_0 = builder->GetInsertBlock(); - BasicBlock *bb_1 = BasicBlock::Create(label, brainf_func); + BasicBlock *bb_1 = BasicBlock::Create(C, label, brainf_func); builder->SetInsertPoint(bb_1); // Make part of PHI instruction now, wait until end of loop to finish PHINode *phi_0 = - PHINode::Create(PointerType::getUnqual(IntegerType::Int8Ty), - headreg, testbb); - phi_0->reserveOperandSpace(2); + PHINode::Create(PointerType::getUnqual(IntegerType::getInt8Ty(C)), + 2, headreg, testbb); phi_0->addIncoming(curhead, bb_0); curhead = phi_0; @@ -432,7 +440,7 @@ void BrainF::readloop(PHINode *phi, BasicBlock *oldbb, BasicBlock *testbb, ConstantInt::get(C, APInt(8, 0)), testreg); //br i1 %test.%d, label %main.%d, label %main.%d - BasicBlock *bb_0 = BasicBlock::Create(label, brainf_func); + BasicBlock *bb_0 = BasicBlock::Create(C, label, brainf_func); BranchInst::Create(bb_0, oldbb, test_0, testbb); //main.%d: @@ -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::Int8Ty), headreg); - phi_1->reserveOperandSpace(1); + CreatePHI(PointerType::getUnqual(IntegerType::getInt8Ty(C)), 1, + headreg); phi_1->addIncoming(head_0, testbb); curhead = phi_1; }