What is LLVM?
The LLVM Project is a collection of modular and reusable compiler and toolchain technologies. Despite its name, LLVM has little to do with traditional virtual machines. The name "LLVM" itself is not an acronym; it is the full name of the project.
- The LLVM Project 是模塊化、可重用的編譯器和工具鏈技術(shù)的集合
- LLVM 包含多個(gè)子項(xiàng)目。其中包括我們熟悉的Clang雌澄、LLDB
command+B 后做了什么脸哀?
前端
- 預(yù)處理(Pre-process):他的主要工作就是將宏替換嫁乘,刪除注釋展開(kāi)頭文件,生成.i文件球碉。
- 詞法分析(Lexical Analysis):將代碼切成一個(gè)個(gè) token蜓斧,比如大小括號(hào),等于號(hào)還有字符串等睁冬。是計(jì)算機(jī)科學(xué)中將字符序列轉(zhuǎn)換為標(biāo)記序列的過(guò)程挎春。
- 語(yǔ)法分析(Semantic Analysis):驗(yàn)證語(yǔ)法是否正確,然后將所有節(jié)點(diǎn)組成抽象語(yǔ)法樹(shù) AST 豆拨。Clang 中 Parser 和 Sema 配合完成
- 靜態(tài)分析(Static Analysis):使用它來(lái)表示用于分析源代碼以便自動(dòng)發(fā)現(xiàn)錯(cuò)誤直奋。
- 中間代碼生成(Code Generation):開(kāi)始IR中間代碼的生成了,CodeGen 會(huì)負(fù)責(zé)將語(yǔ)法樹(shù)自頂向下遍歷逐步翻譯成 LLVM IR施禾,IR 是編譯過(guò)程的前端的輸出后端的輸入脚线。
后端
- 優(yōu)化(Optimize):LLVM 會(huì)去做些優(yōu)化工作,在 Xcode 的編譯設(shè)置里也可以設(shè)置優(yōu)化級(jí)別-01弥搞,-03邮绿,-0s,還可以寫(xiě)些自己的 Pass攀例,官方有比較完整的 Pass 教程: Writing an LLVM Pass — LLVM 5 documentation 船逮。如果開(kāi)啟了 bitcode 蘋(píng)果會(huì)做進(jìn)一步的優(yōu)化,有新的后端架構(gòu)還是可以用這份優(yōu)化過(guò)的 bitcode 去生成粤铭。
- 生成目標(biāo)文件(Assemble):蘋(píng)果平臺(tái)生成Mach-O
- 鏈接(Link):生成 Executable 可執(zhí)行文件挖胃。
What is Clang?
Apple’s official for the C language family
C
C++
Objective-C
Objective-C++
What is Swiftc?
The Swift compiler is principally responsible for translating Swift source code into efficient, executable machine code. However, the Swift compiler front-end also supports a number of other tools, including IDE integration with syntax coloring, code completion, and other conveniences.
- Swift編譯器主要負(fù)責(zé)將Swift源代碼翻譯成高效、可執(zhí)行的機(jī)器碼
- Swift編譯器前端還支持許多其他工具梆惯,包括與語(yǔ)法著色酱鸭、代碼完成和其他便利集成的IDE
- 最終輸出LLVM IR至LLVM后端,持續(xù)優(yōu)化并生成機(jī)械碼
- 采用模塊化編譯而非引用編譯
- 同一模塊編譯流程中只有詞法分析(Parsing)是重復(fù)執(zhí)行的
- 同一模塊下多文件詞法分析結(jié)果將會(huì)共享加袋,即無(wú)需聲明式的引用文件
- swift編譯器將clang作為一個(gè)共享庫(kù)的方式引用凛辣,用于混編oc代碼
Swiftc主要編譯流程
Parsing: The parser is a simple, recursive-descent parser (implemented in lib/Parse) with an integrated, hand-coded lexer. The parser is responsible for generating an Abstract Syntax Tree (AST) without any semantic or type information, and emit warnings or errors for grammatical problems with the input source.
Semantic analysis: Semantic analysis (implemented in lib/Sema) is responsible for taking the parsed AST and transforming it into a well-formed, fully-type-checked form of the AST, emitting warnings or errors for semantic problems in the source code. Semantic analysis includes type inference and, on success, indicates that it is safe to generate code from the resulting, type-checked AST.
Clang importer: The Clang importer (implemented in lib/ClangImporter) imports Clang modules and maps the C or Objective-C APIs they export into their corresponding Swift APIs. The resulting imported ASTs can be referred to by semantic analysis.
SIL generation: The Swift Intermediate Language (SIL) is a high-level, Swift-specific intermediate language suitable for further analysis and optimization of Swift code. The SIL generation phase (implemented in lib/SILGen) lowers the type-checked AST into so-called “raw” SIL. The design of SIL is described in docs/SIL.rst.
SIL guaranteed transformations: The SIL guaranteed transformations (implemented in lib/SILOptimizer/Mandatory) perform additional dataflow diagnostics that affect the correctness of a program (such as a use of uninitialized variables). The end result of these transformations is “canonical” SIL.
SIL Optimizations: The SIL optimizations (implemented in lib/Analysis, lib/ARC, lib/LoopTransforms, and lib/Transforms) perform additional high-level, Swift-specific optimizations to the program, including (for example) Automatic Reference Counting optimizations, devirtualization, and generic specialization.
LLVM IR Generation: IR generation (implemented in lib/IRGen) lowers SIL to LLVM IR, at which point LLVM can continue to optimize it and generate machine code.
參考文獻(xiàn):