LLVM,Clang

在使用xcode時經常會遇到這2個概念,今天總結一下。html

wiki中關於llvm的描述:express

LLVM提供了完整編譯系統的中間層,它會將中間語言(IF, Intermediate form)從編譯器取出與最佳化,最佳化後的IF接著被轉換及鏈結到目標平臺的彙編語言。LLVM能夠接受來自GCC工具鏈所編譯的IF,包含它底下現存的編譯器。xcode

請參考如下連接,http://www.weiphone.com/apple/blog/2009-09-27/The_heart_of_Snow_Leopard_LLVM_and_Clang_206356.shtmlapp

Clang 和 LLVM 的更多資料,請參看官方文檔,http://llvm.org    http://clang.llvm.orgiphone

如下是clang tools的簡要介紹工具

 

DESCRIPTION
clang is a C, C++, and Objective-C compiler which encompasses preprocessing, parsing, optimization, code
generation, assembly, and linking. Depending on which high-level mode setting is passed, Clang will stop
before doing a full link. While Clang is highly integrated, it is important to understand the stages of
compilation, to understand how to invoke it. These stages are:ui

 

Driver
The clang executable is actually a small driver which controls the overall execution of other tools
such as the compiler, assembler and linker. Typically you do not need to interact with the driver, but
you transparently use it to run the other tools.this

 

Preprocessing
This stage handles tokenization of the input source file, macro expansion, #include expansion and
handling of other preprocessor directives. The output of this stage is typically called a ".i" (for
C), ".ii" (for C++), ".mi" (for Objective-C) , or ".mii" (for Objective-C++) file.spa

 

Parsing and Semantic Analysis
This stage parses the input file, translating preprocessor tokens into a parse tree. Once in the form
of a parser tree, it applies semantic analysis to compute types for expressions as well and determine
whether the code is well formed. This stage is responsible for generating most of the compiler warnings
as well as parse errors. The output of this stage is an "Abstract Syntax Tree" (AST).code

 

Code Generation and Optimization
This stage translates an AST into low-level intermediate code (known as "LLVM IR") and ultimately to
machine code. This phase is responsible for optimizing the generated code and handling target-specific
code generation. The output of this stage is typically called a ".s" file or "assembly" file.

 

Clang also supports the use of an integrated assembler, in which the code generator produces object
files directly. This avoids the overhead of generating the ".s" file and of calling the target
assembler.

Assembler
This stage runs the target assembler to translate the output of the compiler into a target object file.
The output of this stage is typically called a ".o" file or "object" file.

Linker
This stage runs the target linker to merge multiple object files into an executable or dynamic library.
The output of this stage is typically called an "a.out", ".dylib" or ".so" file.

The Clang compiler supports a large number of options to control each of these stages. In addition to
compilation of code, Clang also supports other tools:

Clang Static Analyzer

The Clang Static Analyzer is a tool that scans source code to try to find bugs through code analysis. This
tool uses many parts of Clang and is built into the same driver.

相關文章
相關標籤/搜索