Generating IR from the AST
The LLVM code generator takes a module as described in 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 required for the code generator. The complete implementation will be divided into three classes: the
CGModule, and the
CGProcedure classes. The
CodeGenerator class is the general interface used by the compiler driver. The
CGModule and the
CGProcedure classes hold the state required for generating the IR code for a compilation unit and a single function.
We begin with a look at the
clang-generated IR in the next section.
Understanding the IR code
Before generating the IR code, it's good to know the main elements of the IR language. In Chapter 3, The Structure of a Compiler, we already had a brief look at IR. An easy way to get more knowledge of IR is to study...