llvm.org GIT mirror llvm / ff5877f
Fix compilation of examples after 13680223b9d8 / r352827 Who knew...they're not built by default or as part of the tests. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@352830 91177308-0d34-0410-b5e6-96231b3b80d8 James Y Knight 5 months ago
4 changed file(s) with 18 addition(s) and 14 deletion(s). Raw diff Collapse all Expand all
8181 //Function header
8282
8383 //define void @brainf()
84 brainf_func = module->getOrInsertFunction("brainf", Type::getVoidTy(C));
84 brainf_func = Function::Create(FunctionType::get(Type::getVoidTy(C), false),
85 Function::ExternalLinkage, "brainf", module);
8586
8687 builder = new IRBuilder<>(BasicBlock::Create(C, label, brainf_func));
8788
7777 CompileFlags comflag;
7878 std::istream *in;
7979 Module *module;
80 FunctionCallee brainf_func;
80 Function *brainf_func;
8181 FunctionCallee getchar_func;
8282 FunctionCallee putchar_func;
8383 Value *ptr_arr;
7474 FunctionType *main_func_fty = FunctionType::get(
7575 Type::getInt32Ty(mod->getContext()),
7676 {Type::getInt32Ty(mod->getContext()),
77 Type::getInt8Ty(mod->getContext())->getPointerTo()->getPointerTo()});
77 Type::getInt8Ty(mod->getContext())->getPointerTo()->getPointerTo()},
78 false);
7879 Function *main_func =
79 Function::create(main_func_fty, Function::ExternalLinkage, "main", mod);
80 Function::Create(main_func_fty, Function::ExternalLinkage, "main", mod);
8081
8182 {
8283 Function::arg_iterator args = main_func->arg_begin();
4646 using namespace llvm;
4747
4848 static Function* createAdd1(Module *M) {
49 LLVMContext &Context = M->getContext();
4950 // Create the add1 function entry and insert this entry into module M. The
5051 // function will have a return type of "int" and take an argument of "int".
5152 Function *Add1F =
5556
5657 // Add a basic block to the function. As before, it automatically inserts
5758 // because of the last argument.
58 BasicBlock *BB = BasicBlock::Create(M->getContext(), "EntryBlock", Add1F);
59 BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", Add1F);
5960
6061 // Get pointers to the constant `1'.
61 Value *One = ConstantInt::get(Type::getInt32Ty(M->getContext()), 1);
62 Value *One = ConstantInt::get(Type::getInt32Ty(Context), 1);
6263
6364 // Get pointers to the integer argument of the add1 function...
6465 assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
6970 Instruction *Add = BinaryOperator::CreateAdd(One, ArgX, "addresult", BB);
7071
7172 // Create the return instruction and add it to the basic block
72 ReturnInst::Create(M->getContext(), Add, BB);
73 ReturnInst::Create(Context, Add, BB);
7374
7475 // Now, function add1 is ready.
7576 return Add1F;
7677 }
7778
7879 static Function *CreateFibFunction(Module *M) {
80 LLVMContext &Context = M->getContext();
7981 // Create the fib function and insert it into module M. This function is said
8082 // to return an int and take an int parameter.
8183 FunctionType *FibFTy = FunctionType::get(Type::getInt32Ty(Context),
8486 Function::Create(FibFTy, Function::ExternalLinkage, "fib", M);
8587
8688 // Add a basic block to the function.
87 BasicBlock *BB = BasicBlock::Create(M->getContext(), "EntryBlock", FibF);
89 BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", FibF);
8890
8991 // Get pointers to the constants.
90 Value *One = ConstantInt::get(Type::getInt32Ty(M->getContext()), 1);
91 Value *Two = ConstantInt::get(Type::getInt32Ty(M->getContext()), 2);
92 Value *One = ConstantInt::get(Type::getInt32Ty(Context), 1);
93 Value *Two = ConstantInt::get(Type::getInt32Ty(Context), 2);
9294
9395 // Get pointer to the integer argument of the add1 function...
9496 Argument *ArgX = &*FibF->arg_begin(); // Get the arg.
9597 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
9698
9799 // Create the true_block.
98 BasicBlock *RetBB = BasicBlock::Create(M->getContext(), "return", FibF);
100 BasicBlock *RetBB = BasicBlock::Create(Context, "return", FibF);
99101 // Create an exit block.
100 BasicBlock* RecurseBB = BasicBlock::Create(M->getContext(), "recurse", FibF);
102 BasicBlock *RecurseBB = BasicBlock::Create(Context, "recurse", FibF);
101103
102104 // Create the "if (arg < 2) goto exitbb"
103105 Value *CondInst = new ICmpInst(*BB, ICmpInst::ICMP_SLE, ArgX, Two, "cond");
104106 BranchInst::Create(RetBB, RecurseBB, CondInst, BB);
105107
106108 // Create: ret int 1
107 ReturnInst::Create(M->getContext(), One, RetBB);
109 ReturnInst::Create(Context, One, RetBB);
108110
109111 // create fib(x-1)
110112 Value *Sub = BinaryOperator::CreateSub(ArgX, One, "arg", RecurseBB);
119121 BinaryOperator::CreateAdd(CallFibX1, CallFibX2, "addresult", RecurseBB);
120122
121123 // Create the return instruction and add it to the basic block
122 ReturnInst::Create(M->getContext(), Sum, RecurseBB);
124 ReturnInst::Create(Context, Sum, RecurseBB);
123125
124126 return FibF;
125127 }