Book Image

Learn LLVM 12

By : Kai Nacke
Book Image

Learn LLVM 12

By: Kai Nacke

Overview of this book

LLVM was built to bridge the gap between compiler textbooks and actual compiler development. It provides a modular codebase and advanced tools which help developers to build compilers easily. This book provides a practical introduction to LLVM, gradually helping you navigate through complex scenarios with ease when it comes to building and working with compilers. You’ll start by configuring, building, and installing LLVM libraries, tools, and external projects. Next, the book will introduce you to LLVM design and how it works in practice during each LLVM compiler stage: frontend, optimizer, and backend. Using a subset of a real programming language as an example, you will then learn how to develop a frontend and generate LLVM IR, hand it over to the optimization pipeline, and generate machine code from it. Later chapters will show you how to extend LLVM with a new pass and how instruction selection in LLVM works. You’ll also focus on Just-in-Time compilation issues and the current state of JIT-compilation support that LLVM provides, before finally going on to understand how to develop a new backend for LLVM. By the end of this LLVM book, you will have gained real-world experience in working with the LLVM compiler development framework with the help of hands-on examples and source code snippets.
Table of Contents (17 chapters)
Section 1 – The Basics of Compiler Construction with LLVM
Section 2 – From Source to Machine Code Generation
Section 3 –Taking LLVM to the Next Level

Implementing the DAG instruction selection classes

A large portion of the DAG instruction selector is generated by the llvm-tblgen tool. We still need to create classes using the generated code and put everything together. Let's begin with a part of the initialization process.

Initializing the target machine

Each backend has to provide at least one TargetMachine class, usually a subclass of the LLVMTargetMachine class. The M88kTargetMachine class holds a lot of the details required for code generation, and it also acts as a factory for other backend classes, most notably for the Subtarget class and the TargetPassConfig class. The Subtarget class holds the configuration for the code generation, such as which features are enabled. The TargetPassConfig class configures the machine passes of the backend. The declaration for our M88kTargetMachine class is in the M88ktargetMachine.h file and looks like this:

class M88kTargetMachine : public LLVMTargetMachine {