LLVM in Action
Learn how LLVM generates machine code for different architectures from a high-level language.
We'll cover the following...
In this section, let’s use LLVM’s Clang compiler to compile native code into LLVM IR. This will give a better idea of how LLVM works and will be useful for understanding how the compilers use LLVM later.
We first create a C file called sum.c
with the following command:
Press + to interact
touch sum.c
Then we add the following code to it:
Press + to interact
unsigned sum(unsigned a, unsigned b) {return a + b;}
The sum.c
file contains a simple sum function that takes in two unsigned integers and returns the sum of them. LLVM provides the Clang LLVM compiler to compile the C source code. In order to generate the LLVM IR, run the following command:
Press + to interact
clang -S -O3 -emit-llvm sum.c
We provided the Clang compiler with the -S
, ...