Getting Started with LLVM Core Libraries

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Get to grips with LLVM essentials and use the core libraries to build advanced tools About This Book • Learn how to configure, build, and use LLVM and Clang based tools • Explore the depths of the LLVM front-end, IR, code generator, and libraries, and learn how a modern compiler is implemented in a practical way. • Customize your project to benefit from Just in Time compilation (JIT), static analysis and source-to-source transformations. Who This Book Is For This book is intended for enthusiasts, computer science students, and compiler engineers interested in learning about the LLVM framework. You need a background in C++ and, although not mandatory, should know at least some compiler theory. Whether you are a newcomer or a compiler expert, this book provides a practical introduction to LLVM and avoids complex scenarios. If you are interested enough and excited about this technology, then this book is definitely for you. 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 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.

Author(s): Bruno Cardoso Lopes, Rafael Auler
Edition: 1
Publisher: Packt Publishing
Year: 2014

Language: English
Commentary: Publisher's PDF
Pages: 314
City: Birmingham, UK
Tags: Compilers; LLVM

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