You are previewing Getting Started with LLVM Core Libraries.
O'Reilly logo
Getting Started with LLVM Core Libraries

Book Description

Get to grips with LLVM essentials and use the core libraries to build advanced tools

In Detail

LLVM is a bleeding edge compiler technology framework. Easily extendable and designed as a multitude of libraries, LLVM provides a smooth experience for compiler newcomers and reduces the steep learning curve often associated with compiler development.

To start, this book will show you how to configure, build, and install LLVM libraries, tools, and external projects. Next, you will be introduced to LLVM design and how it works in practice throughout each LLVM compiler stage: frontend, IR, backend, the JIT engine, cross-compilation capabilities, and the plugin interface. With multiple hands-on examples and source code snippets, Getting Started with LLVM Core Libraries ensures a solid and smooth first step into the LLVM compiler development environment.

What You Will Learn

  • Configure, build, and install extra LLVM open source projects including Clang tools, static analyzer, Compiler-RT, LLDB, DragonEgg, libc++, and LLVM test-suite
  • Understand the LLVM library design and interaction between libraries and standalone tools
  • Increase your knowledge of source code processing stages by learning how the Clang frontend uses a lexer, parser, and syntax analysis
  • Manipulate, generate, and play with LLVM IR files while writing custom IR analyses and transformation passes
  • Write tools to use LLVM Just-in-Time (JIT) compilation capabilities
  • Find bugs and improve your code by using the static analyzer
  • Design source code analysis and transformation tools using LibClang, LibTooling, and the Clang plugin interface
  • Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

    Table of Contents

    1. Getting Started with LLVM Core Libraries
      1. Table of Contents
      2. Getting Started with LLVM Core Libraries
      3. Credits
      4. About the Authors
      5. About the Reviewers
      6. www.PacktPub.com
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Build and Install LLVM
        1. Understanding LLVM versions
        2. Obtaining prebuilt packages
          1. Obtaining the official prebuilt binaries
          2. Using package managers
            1. Staying updated with snapshot packages
              1. Linux
              2. Windows
        3. Building from sources
          1. System requirements
          2. Obtaining sources
            1. SVN
            2. Git
          3. Building and installing LLVM
            1. Using the autotools-generated configure script
              1. Building and configuring with Unix
            2. Using CMake and Ninja
              1. Building with Unix using CMake and Ninja
              2. Solving build errors
            3. Using other Unix approaches
          4. Windows and Microsoft Visual Studio
          5. Mac OS X and Xcode
        4. Summary
      9. 2. External Projects
        1. Introducing Clang extras
          1. Building and installing Clang extra tools
          2. Understanding Compiler-RT
          3. Seeing Compiler-RT in action
        2. Using the DragonEgg plugin
          1. Building DragonEgg
          2. Understanding the compilation pipeline with DragonEgg and LLVM tools
          3. Understanding the LLVM test suite
          4. Using LLDB
            1. Exercising a debug session with LLDB
          5. Introducing the libc++ standard library
        3. Summary
      10. 3. Tools and Design
        1. Introducing LLVM's basic design principles and its history
        2. Understanding LLVM today
        3. Interacting with the compiler driver
        4. Using standalone tools
        5. Delving into the LLVM internal design
          1. Getting to know LLVM's basic libraries
          2. Introducing LLVM's C++ practices
            1. Seeing polymorphism in practice
            2. Introducing C++ templates in LLVM
            3. Enforcing C++ best practices in LLVM
            4. Making string references lightweight in LLVM
          3. Demonstrating the pluggable pass interface
        6. Writing your first LLVM project
          1. Writing the Makefile
          2. Writing the code
        7. Navigating the LLVM source – general advice
          1. Understanding the code as a documentation
          2. Asking the community for help
          3. Coping with updates – using the SVN log as a documentation
          4. Concluding remarks
        8. Summary
      11. 4. The Frontend
        1. Introducing Clang
          1. Frontend actions
          2. Libraries
            1. Using libclang
          3. Understanding Clang diagnostics
            1. Reading diagnostics
        2. Learning the frontend phases with Clang
          1. Lexical analysis
            1. Exercising lexical errors
            2. Writing libclang code that uses the lexer
            3. Preprocessing
          2. Syntactic analysis
            1. Understanding Clang AST nodes
            2. Understanding the parser actions with a debugger
            3. Exercising a parser error
            4. Writing code that traverses the Clang AST
            5. Serializing the AST with precompiled headers
          3. Semantic analysis
            1. Exercising a semantic error
          4. Generating the LLVM IR code
        3. Putting it together
        4. Summary
      12. 5. The LLVM Intermediate Representation
        1. Overview
          1. Understanding the LLVM IR target dependency
        2. Exercising basic tools to manipulate the IR formats
        3. Introducing the LLVM IR language syntax
          1. Introducing the LLVM IR in-memory model
        4. Writing a custom LLVM IR generator
          1. Building and running the IR generator
          2. Learning how to write code to generate any IR construct with the C++ backend
        5. Optimizing at the IR level
          1. Compile-time and link-time optimizations
          2. Discovering which passes matter
          3. Understanding pass dependencies
          4. Understanding the pass API
          5. Writing a custom pass
            1. Building and running your new pass with the LLVM build system
            2. Building and running your new pass with your own Makefile
        6. Summary
      13. 6. The Backend
        1. Overview
          1. Using the backend tools
        2. Learning the backend code structure
        3. Knowing the backend libraries
        4. Learning how to use TableGen for LLVM backends
          1. The language
          2. Knowing the code generator .td files
            1. Target properties
            2. Registers
            3. Instructions
        5. Understanding the instruction selection phase
          1. The SelectionDAG class
          2. Lowering
          3. DAG combine and legalization
          4. DAG-to-DAG instruction selection
            1. Pattern matching
          5. Visualizing the instruction selection process
          6. Fast instruction selection
        6. Scheduler
          1. Instruction itineraries
          2. Hazard detection
          3. Scheduling units
        7. Machine instructions
        8. Register allocation
          1. Register coalescer
          2. Virtual register rewrite
          3. Target hooks
        9. Prologue and epilogue
          1. Frame indexes
        10. Understanding the machine code framework
          1. MC instructions
          2. Code emission
        11. Writing your own machine pass
        12. Summary
      14. 7. The Just-in-Time Compiler
        1. Getting to know the LLVM JIT engine basics
          1. Introducing the execution engine
          2. Memory management
        2. Introducing the llvm::JIT framework
          1. Writing blobs to memory
          2. Using JITMemoryManager
          3. Target code emitters
          4. Target information
          5. Learning how to use the JIT class
            1. The generic value
        3. Introducing the llvm::MCJIT framework
          1. The MCJIT engine
            1. Learning the module's states
          2. Understanding how MCJIT compiles modules
            1. The Object buffer, the cache, and the image
            2. Dynamic linking
            3. The memory manager
            4. The MC code emission
            5. Object finalization
          3. Using the MCJIT engine
        4. Using LLVM JIT compilation tools
          1. Using the lli tool
          2. Using the llvm-rtdyld tool
        5. Other resources
        6. Summary
      15. 8. Cross-platform Compilation
        1. Comparing GCC and LLVM
        2. Understanding target triples
        3. Preparing your toolchain
          1. Standard C and C++ libraries
          2. Runtime libraries
          3. The assembler and the linker
          4. The Clang frontend
            1. Multilib
        4. Cross-compiling with Clang command-line arguments
          1. Driver options for the target
          2. Dependencies
          3. Cross-compiling
            1. Installing GCC
            2. Potential problems
          4. Changing the system root
        5. Generating a Clang cross-compiler
          1. Configuration options
          2. Building and installing your Clang-based cross-compiler
          3. Alternative build methods
            1. Ninja
            2. ELLCC
            3. EmbToolkit
        6. Testing
          1. Development boards
          2. Simulators
        7. Additional resources
        8. Summary
      16. 9. The Clang Static Analyzer
        1. Understanding the role of a static analyzer
          1. Comparing classic warnings versus the Clang Static Analyzer
          2. The power of the symbolic execution engine
        2. Testing the static analyzer
          1. Using the driver versus using the compiler
          2. Getting to know the available checkers
          3. Using the static analyzer in the Xcode IDE
          4. Generating graphical reports in HTML
          5. Handling large projects
            1. A real-world example – finding bugs in Apache
        3. Extending the static analyzer with your own checkers
          1. Getting familiar with the project architecture
          2. Writing your own checker
            1. Solving the problem with a custom checker
              1. Writing the state class
                1. Understanding ProgramState immutability
                2. Dissecting the code
              2. Defining the Checker subclass
              3. Writing the Register macro
              4. Implementing the Checker subclass
              5. Adding registration code
              6. Building and testing
        4. More resources
        5. Summary
      17. 10. Clang Tools with LibTooling
        1. Generating a compile command database
        2. The clang-tidy tool
          1. Using clang-tidy to check your code
        3. Refactoring tools
          1. Clang Modernizer
          2. Clang Apply Replacements
          3. ClangFormat
          4. Modularize
            1. Understanding C/C++ APIs' Definitions
              1. The linker job
              2. The frontend counterpart
              3. Problems of relying on the C/C++ preprocessor
            2. Understanding the working of modules
            3. Using modules
            4. Understanding Modularize
            5. Using Modularize
          5. Module Map Checker
          6. PPTrace
          7. Clang Query
          8. Clang Check
          9. Remove c_str() calls
        4. Writing your own tool
          1. Problem definition – writing a C++ code refactoring tool
          2. Configuring your source code location
          3. Dissecting tooling boilerplate code
          4. Using AST matchers
            1. Composing matchers
            2. Putting the AST matcher predicates in the code
          5. Writing the callbacks
          6. Testing your new refactoring tool
        5. More resources
        6. Summary
      18. Index