In this recipe you, will learn how to generate IR code for a function.
Do the following steps:
The
Codegen()
function for the function call can be defined as follows:Value *FunctionCallAST::Codegen() { Function *CalleeF = Module_Ob->getFunction(Function_Callee); std::vector<Value*>ArgsV; for(unsigned i = 0, e = Function_Arguments.size(); i != e; ++i) { ArgsV.push_back(Function_Arguments[i]->Codegen()); if(ArgsV.back() == 0) return 0; } return Builder.CreateCall(CalleeF, ArgsV, "calltmp"); }
Once we have the function to call, we recursively call the
Codegen()
function for each argument that is to be passed in and create an LLVM call instruction.Now that the
Codegen()
function for a function call has been defined, it's time to define theCodegen()
functions for declarations and function definitions.The
Codegen()
function for function declarations can be defined as follows:Function *FunctionDeclAST::Codegen() { std...