LLVM: compiler infrastructure
today we gonna take a look at llvm, what is it ? how it works ? what is so special about it ?
1. Introduction
llvm is a compiler infrastructure, which is an ecosystem of diverse set tools, used to compile a lot of different languages (C, C++, rust,swift, golang, etc...) into wide range of hardware architecture (x86, sparc, arm, powerpc, mips...)
2. Usage
llvm is an umbrella of a lot of tools like clang
which is an amazingly fast and efficient compiler, and is number one competitor of gcc
, it can compile C,C++ and Objective-C. Technically clang
is just a frontend means it doesn't do everything on its own, instead it just parses the source file, then compiles it into IR
(intermediate representation) which is a bit similar to assembly, then it delivers control to llvm which takes care of the rest, you can read about compilation steps here, llvm takes that IR
and optimize it, then invoke a code generator for the target architecture:
all other frontends(rust, golang, swift ...) are doing the same, each one parses its own language and then turns it into IR
, lastly llvm handles the rest
3. Revolution
Compilers used to be really hard to do and get right, and they still are, but llvm makes it a lot easier than never before, now developers can build theire compilers on top of llvm, w/o worring about optimizers and supported architectures, all they should do is implement theire own language frontend, that turns src file into IR, and llvm will take care of the rest.
A major strength of LLVM is its versatility, flexibility, and reusability, which is why it is being used for such a wide variety of different tasks: everything from doing light-weight JIT compiles of embedded languages like Lua to compiling Fortran code for massive super computers. And significantly reduce the cost of building domain specific compilers, and aid in connecting existing compilers together.