Book Image

LLVM Cookbook

Book Image

LLVM Cookbook

Overview of this book

Table of Contents (16 chapters)
LLVM Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

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 defined, it's time to define the Codegen() functions for declarations and function definitions.

    The Codegen() function for function declarations can be defined as follows:

    Function *FunctionDeclAST::Codegen() {
      std...