Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If clang is the front end of a compiler, then why it can produce executable file?

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?

like image 992
storen Avatar asked Dec 19 '14 14:12

storen


People also ask

Is Clang a front end?

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.

Why are compilers separated into front end and back end?

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.

How does Clang compiler work?

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.

What is the difference between GCC and Clang?

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.


2 Answers

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.

like image 86
Michael Haidl Avatar answered Oct 21 '22 11:10

Michael Haidl


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

  • -emit-ast just does the parse and makes the Abstract Syntax Tree
  • -emit-llvm makes the LLVM Intermediate Representation, but not turn it into code for your computer

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.

like image 36
Christopher Ian Stern Avatar answered Oct 21 '22 10:10

Christopher Ian Stern