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 是模块化、可重用的编译器和工具链技术的集合
- LLVM 包含多个子项目。其中包括我们熟悉的Clang、LLDB
command+B 后做了什么?
前端
- 预处理(Pre-process):他的主要工作就是将宏替换,删除注释展开头文件,生成.i文件。
- 词法分析(Lexical Analysis):将代码切成一个个 token,比如大小括号,等于号还有字符串等。是计算机科学中将字符序列转换为标记序列的过程。
- 语法分析(Semantic Analysis):验证语法是否正确,然后将所有节点组成抽象语法树 AST 。Clang 中 Parser 和 Sema 配合完成
- 静态分析(Static Analysis):使用它来表示用于分析源代码以便自动发现错误。
- 中间代码生成(Code Generation):开始IR中间代码的生成了,CodeGen 会负责将语法树自顶向下遍历逐步翻译成 LLVM IR,IR 是编译过程的前端的输出后端的输入。
后端
- 优化(Optimize):LLVM 会去做些优化工作,在 Xcode 的编译设置里也可以设置优化级别-01,-03,-0s,还可以写些自己的 Pass,官方有比较完整的 Pass 教程: Writing an LLVM Pass — LLVM 5 documentation 。如果开启了 bitcode 苹果会做进一步的优化,有新的后端架构还是可以用这份优化过的 bitcode 去生成。
- 生成目标文件(Assemble):苹果平台生成Mach-O
- 链接(Link):生成 Executable 可执行文件。
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编译器主要负责将Swift源代码翻译成高效、可执行的机器码
- Swift编译器前端还支持许多其他工具,包括与语法着色、代码完成和其他便利集成的IDE
- 最终输出LLVM IR至LLVM后端,持续优化并生成机械码
- 采用模块化编译而非引用编译
- 同一模块编译流程中只有词法分析(Parsing)是重复执行的
- 同一模块下多文件词法分析结果将会共享,即无需声明式的引用文件
- swift编译器将clang作为一个共享库的方式引用,用于混编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.
参考文献: