Having created a decorated abstract syntax tree (AST) for your programming language, the next task is to generate the LLVM IR code from it. LLVM IR code resembles a three-address code with a human-readable representation. Therefore, we need a systematic approach to translate language concepts such as control structures into the lower level of LLVM IR.

In this chapter, you will learn about the basics of LLVM IR and how to generate IR for control flow structures from the AST. You will also learn how to generate LLVM IR for expressions in static single assignment (SSA) form using a modern algorithm. Finally, you will learn how to emit assembler text and object code.

This chapter will cover the following topics:

  • Generating IR from the AST
  • Using AST numbering to generate IR code in SSA form
  • Setting up the module and the driver

By the end of this chapter, you will know how to create a code generator for your programming language and how to integrate it into your compiler.

Generating IR from the AST

The LLVM code generator takes a module in LLVM IR as input and turns it into object code or assembly text. We need to transform the AST representation into IR. To implement an IR code generator, we will look at a simple example first and then develop the classes needed for the code generator. The complete implementation will be divided into three classes:

  • CodeGenerator
  • CGModule
  • CGProcedure

The CodeGenerator class is the general interface used by the compiler driver. The CGModule and CGProcedure classes hold the state required for generating the IR code for a compilation unit and a single function.

We’ll begin by looking at the Clang-generated IR.

Leave a Reply

Your email address will not be published. Required fields are marked *