Emitting a return statement
A function might return a value or it may return void. Here in our example, we have defined that our function returns an integer. Let's assume that our function returns 0
. The first step is to get a 0
value, which can be done using the Constant
class.
Builder.CreateRet(Builder.getInt32(0));
The overall code is as follows:
#include "llvm/IR/IRBuilder.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/Module.h" #include "llvm/IR/Verifier.h" #include <vector> using namespace llvm; static LLVMContext &Context = getGlobalContext(); static Module *ModuleOb = new Module("my compiler", Context); Function *createFunc(IRBuilder<> &Builder, std::string Name) { FunctionType *funcType = llvm::FunctionType::get(Builder.getInt32Ty(), false); Function *fooFunc = llvm::Function::Create( funcType, llvm::Function::ExternalLinkage, Name, ModuleOb); return fooFunc; } BasicBlock *createBB(Function *fooFunc, std::string Name) { return BasicBlock...