LLVM is a project which presents a combination of a modular compiler and toolchain software and technologies. LLVM has commenced a research project at the University of Illinois to create a compiler and toolchain supporting both static and dynamic compilation for various programming languages. Even other programming languages like C, C++, Java, C#, Kotlin, Delphi, Rust, Ruby are supported; the most popular programming languages are C and C++ for the LLVM compiler and toolchain.
LLVM Hardware and Operating System Support
As LLVM is an open-source project, it supports different operating systems, platforms and hardware. It can run on OS such as Linux, Solaris, FreeBSD, NetBSD, MacOSX, Windows operating systems. It supports different hardware and processor architectures like x86, amd64, x86_64, PowerPC, V9, ARM. For more specific hardware and operating support, take a look following table.
LLVM Supported Programming Languages
As affirmed at the beginning, LLVM supports a various range of programming languages to compile and bind. The LLVM does not fully support even some programming languages; we will list the supported programming languages and support level and supported features.
C and C++ are the top programming languages for the LLVM. Even LLVM is created as a dynamic compiler, and toolchain C and C++ are the primary targets for support. Also, LLVM is developed with C++.
Objective-C is another entirely supported programming language similar to C and C++.
LLVM Architecture and Components
LLVM provides a complete architecture where there are different components and pieces to complete the picture. LLVM provides Front End, Passes, and Back End. The following graphic illustrates how the LLVM works on source code and generates executables and binaries.
Front End will take the source code and convert it to the intermediate representation or IR. This is a preparation process for the other steps and compiler where LLVM runs appropriately. Front End is the core part of the LLVM and generally additional software or a tool like Clang.
Pass or IR is the core task of the LLVM, where the compilation process takes place. The IR or intermediate code will be optimized again and again with multiple phases.
The Back End is the last step where the optimized IR code is converted into the machine code specific to a CPU architecture or operating system.
Below we will list some essential tools and components of the LLVM.
Clang is the compiler created for the LLVM specifically. Clang can compile the IR code into the executables and binaries. Also, Clang is almost three times faster than the GCC compiler.
LLDB is the debugger created for the LLVM project. LLDB is faster and efficient than GDB and tightly integrated with the LLVM core and Clang.
libc++ and libc++ ABI are C++ standard library implementations.
compiler-rt is a dynamic testing utility for low-level code and can be used to run and text the low-level code in real-time.
LLVM Core is the core component for the LLVM, where core libraries provide optimizer and code generation support for different CPU and hardware architectures.
LLVM vs GCC
Gnu Compiler Collection or GCC is another popular and widely used open-source compiler project. Before the LLVM emerged, the GCC was a defacto compiler for different open source world. With the LLVM, things started to change, and with its advantages, LLVM became popular too. Below we will compare the LLVM and GCC from different points of view.
- GCC supports more traditional programming languages like Ada, Fortran, and Go than LLVM.
- GCC supports more hardware and CPU architectures like RISC-V than LLVM.
- GCC supports more language extensions and assembly language features than LLVM.
- LLVM used by emerging languages like Swift, Rust, Julia, and Ruby, then GCC.
- LLVM complies with the C and C++ programming languages more strictly than GCC.
- LLVM provides more accurate and friendly diagnostics information than GCC.
Installation with LLVM
LLVM can be installed on Linux and Ubuntu operating systems like below. We will provide the package names llvm-10 and llvm-10-tools to install LLVM version 10 core libraries and tools.
$ sudo apt install llvm-10 llvm-10-tools clang
Gaining Started with LLVM
We have installed needed packages named llvm-10, llvm-10-tools, and clang. For example, we will compile a simple hello world example with the LLVM as a start. The hello world application source code is mentioned below.
#include int main() { printf("Hello World From Poftut.com via LLVM"); return 0; }
We will compile and create the executable named hello_world with the clang command like below. The clang command will execute the LLVM under the hood.
$ clang hello_world.c -o hello_world
This executable file "hello_world" can now run from the console or bash command line like below.
$ ./hello_world