Generating IR code for functions

In this recipe you, will learn how to generate IR code for a function.

How to do it…

Do the following steps:

  1. 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.

  2. Now that the Codegen() function for a function call has been ...

Get LLVM Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.