One thing I really don't understand is about the function of clang, if clang is the front end part of the compiler, it should just do the parser work for the source code, the the remain work will be done by LLVM. But clang can produce executable file too. So how to understand it ?What is the relation between clang and llvm?
Clang is a compiler front end for the C, C++, Objective-C, and Objective-C++ programming languages, as well as the OpenMP, OpenCL, RenderScript, CUDA, and HIP frameworks. It acts as a drop-in replacement for the GNU Compiler Collection (GCC), supporting most of its compilation flags and unofficial language extensions.
In compilers, the frontend translates a computer programming source code into an intermediate representation, and the backend works with the intermediate representation to produce code in a computer output language. The backend usually optimizes to produce code that runs faster.
Like many other compilers design, Clang compiler has three phase: The front end that parses source code, checking it for errors, and builds a language-specific Abstract Syntax Tree (AST) to represent the input code. The optimizer: its goal is to do some optimization on the AST generated by the front end.
Clang is much faster and uses far less memory than GCC. Clang aims to provide extremely clear and concise diagnostics (error and warning messages), and includes support for expressive diagnostics. GCC's warnings are sometimes acceptable, but are often confusing and it does not support expressive diagnostics.
If you are very specific: the clang executable is a compiler driver. It invokes all parts needed to produce an executable. It invokes libclang which does the front-end jobs: parser/lexer, the semantic analysis, building the AST and code generation. When the AST is lowered to LLVM IR the front-end jobs are done and the optimizer and LLVM kicks in. After optmizing the code the compiler driver will invoke the LLVM back-end specified by the target and finally the linker which builds the executable. And that is why the clang compiler driver can build executables.
LLVM is a compiler backend that was written before clang, which originally used the front end from gcc in a tool called 'llvm-gcc'. Clang is the name of the front end code, but clang is also the name of a tool that includes the clang front end, but will also run the whole compile for you. Later phases of compilation are either built into the clang tool as libraries, or if they are separate executables clang knows how to invoke them. With the right command line arguments, you can make clang stop part way thru
Clang will work as the driver for the whole build because that's what programmers usually want, the soure parsed, the object generated, the executable made. Wanting the Abstract Syntax Tree spit back at you is pretty rare.
Obviously this is the souce for all thing LLVM http://llvm.org
Here is a video of Chriss Lattner explaing what LLVM is https://www.youtube.com/watch?v=029YXzHtRy0 . Chandler Carruth has some vids on youtuble explain parts of clang that he has worked on.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With