- Getting Started with LLVM Core Libraries
- Bruno Cardoso Lopes Rafael Auler
- 1113字
- 2021-09-03 09:44:11
Introducing Clang extras
The most noticeable design decision of LLVM is its segregation between backend and frontend as two separate projects, LLVM core and Clang. LLVM started as a set of tools orbiting around the LLVM intermediate representation (IR) and depended on a hacked GCC to translate high-level language programs to its particular IR, stored in bitcode files. Bitcode is a term coined as a parody of Java bytecode files. An important milestone of the LLVM project happened when Clang appeared as the first frontend specifically designed by the LLVM team, bringing with it the same level of quality, clear documentation, and library organization as the core LLVM. It is not only able to convert C and C++ programs into LLVM IR but also to supervise the entire compilation process as a flexible compiler driver that strives to stay compatible with GCC.
We will henceforth refer to Clang as a frontend program rather than a driver, responsible for translating C and C++ programs to the LLVM IR. The exciting aspect of Clang libraries is the possibility to use them to write powerful tools, giving the C++ programmer freedom to work with hot topics of C++ such as C++ code refactoring tools and source-code analysis tools. Clang comes prepackaged with some tools that may give you an idea of what we can do with its libraries:
- Clang Check: It is able to perform syntax checks, apply quick fixes to solve common issues, and also dump the internal Clang Abstract Syntax Tree (AST) representation of any program
- Clang Format: It comprises both a tool and a library,
LibFormat
, that are able to not only indent code but also format any piece of C++ code to conform with LLVM coding standards, Google's style guide, Chromium's style guide, Mozilla's style guide, and WebKit's style guide
The clang-tools-extra
repository is a collection of more applications that are built on top of Clang. They are able to read large C or C++ code bases and perform all sorts of code refactoring and analysis. We enumerate below some of the tools of this package, but it is not limited to them:
- Clang Modernizer: It is a code refactoring tool that scans C++ code and changes old-style constructs to conform with more modern styles proposed by newer standards, such as the C++-11 standard
- Clang Tidy: It is a linter tool that checks for common programming mistakes that violate either LLVM or Google coding standards
- Modularize: It helps you in identifying C++ header files that are suitable to compose a module, a new concept that is being currently discussed by C++ standardization committees (for more information, please refer to Chapter 10, Clang Tools with LibTooling)
- PPTrace: It is a simple tool that tracks the activity of the Clang C++ preprocessor
More information on how to use these tools and how to build your own tool is available in Chapter 10, Clang Tools with LibTooling.
Building and installing Clang extra tools
You can obtain an official snapshot of version 3.4 of this project at it together with the source of the core LLVM and Clang, relying on the LLVM build system. To do this, you must put the source directory into the Clang source tree as follows:
$ wget http://llvm.org/releases/3.4/clang-tools-extra-3.4.src.tar.gz $ tar xzf clang-tools-extra-3.4.src.tar.gz $ mv clang-tools-extra-3.4 llvm/tools/clang/tools/extra
You may also obtain sources directly from the official LLVM subversion repository:
$ cd llvm/tools/clang/tools $ svn co http://llvm.org/svn/llvm-project/clang-tools-extra/trunk extra
Recall from the previous chapter that you can replace trunk
with tags/RELEASE_34/final
if you want to obtain the stable sources for version 3.4. Alternatively, if you prefer using the GIT version control software, you can download it with the following command lines:
$ cd llvm/tools/clang/tools $ git clone http://llvm.org/git/clang-tools-extra.git extra
After placing the sources into the Clang tree, you must proceed with the compilation instructions from successful install, run the clang-modernize
tool as follows:
$ clang-modernize –version clang-modernizer version 3.4
Understanding Compiler-RT
The Compiler-RT (runtime) project provides target-specific support for low-level functionality that is not supported by the hardware. For example, 32-bit targets usually lack instructions to support 64-bit division. Compiler-RT solves this problem by providing a target-specific and optimized function that implements 64-bit division while using 32-bit instructions. It provides the same functionalities and thus is the LLVM equivalent of libgcc
. Moreover, it has runtime support for the address and memory sanitizer tools. You can download Compiler-RT Version 3.4 at http://llvm.org/releases/3.4/compiler-rt-3.4.src.tar.gz or look for more versions at http://llvm.org/releases/download.html.
Since it is a crucial component in a working LLVM-based compiler tool chain, we have already presented how to install Compiler-RT in the previous chapter. If you still do not have it, remember to put its sources into the projects
folder inside the LLVM source tree, such as in the following command sequence:
$ wget http://llvm.org/releases/3.4/compiler-rt-3.4.src.tar.gz. $ tar xzf compiler-rt-3.4.src.tar.gz $ mv compiler-rt-3.4 llvm/projects/compiler-rt
If you prefer, you can rely instead on its SVN repository:
$ cd llvm/projects $ svn checkout http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
You can also download it via a GIT mirror as an alternative to SVN:
$ cd llvm/projects $ git clone http://llvm.org/git/compiler-rt.git
Note
Compiler-RT works, among others, in GNU/Linux, Darwin, FreeBSD, and NetBSD. The supported architectures are the following: i386, x86_64, PowerPC, SPARC64, and ARM.
Seeing Compiler-RT in action
To see a typical situation where the compiler runtime library kicks in, you can perform a simple experiment by writing a C program that performs 64-bit division:
#include <stdio.h> #include <stdint.h> #include <stdlib.h> int main() { uint64_t a = 0ULL, b = 0ULL; scanf ("%lld %lld", &a, &b); printf ("64-bit division is %lld\n", a / b); return EXIT_SUCCESS; }
Tip
Downloading the example code
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.
If you have a 64-bit x86 system, experiment with two commands by using your LLVM compiler:
$ clang -S -m32 test.c -o test-32bit.S $ clang -S test.c -o test-64bit.S
The -m32
flag instructs the compiler to generate a 32-bit x86 program, while the -S
flag produces the x86 assembly language file for this program in test-32bit.S
. If you look at this file, you will see a curious call whenever the program needs to perform the division:
call ___udivdi3
This function is defined by Compiler-RT and demonstrates where the library will be used. However, if you omit the -m32
flag and use the 64-bit x86 compiler, as in the second compiler command that generated the test-64bit.S
assembly language file, you will no longer see a program that requires the assistance of Compiler-RT because it can easily perform this division with a single instruction:
divq -24(%rbp)
- Clojure Programming Cookbook
- UNIX編程藝術
- Qt 5 and OpenCV 4 Computer Vision Projects
- Facebook Application Development with Graph API Cookbook
- Git Version Control Cookbook
- 深入理解Django:框架內幕與實現原理
- Mastering Entity Framework
- 實戰Java程序設計
- x86匯編語言:從實模式到保護模式(第2版)
- SAS數據統計分析與編程實踐
- PhoneGap Mobile Application Development Cookbook
- 劍指Java:核心原理與應用實踐
- Visual FoxPro程序設計習題集及實驗指導(第四版)
- Python機器學習算法: 原理、實現與案例
- Raspberry Pi Blueprints