美文网首页
iOS编译原理

iOS编译原理

作者: 优质胡萝北 | 来源:发表于2021-01-06 18:27 被阅读0次

What is LLVM?

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 后做了什么?

Clang编译流程

前端

  • 预处理(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++

C编译1 C编译2

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后端,持续优化并生成机械码
Swift编译1.png
  • 采用模块化编译而非引用编译
  • 同一模块编译流程中只有词法分析(Parsing)是重复执行的
  • 同一模块下多文件词法分析结果将会共享,即无需声明式的引用文件
Swift编译2.png
  • swift编译器将clang作为一个共享库的方式引用,用于混编oc代码

Swiftc主要编译流程

Swift编译流程
  • 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.

参考文献:

What is Swift Compiler
Modules

相关文章

  • iOS App 编译过程

    这篇文章是对于自己学习 App编译过程的一个总结 学习的相关文章 iOS App的编译过程iOS 编译过程的原理和...

  • iOS编译原理

    What is LLVM? The LLVM Project is a collection of modular...

  • iOS编译原理

    hook改变函数的执行流程。 1.重定向:ASLR(随机值) + 偏移值 = 内存值0x5FCC + 0x0000...

  • iOS编译原理

    主要内容: 理解C、C++以及OC的关系 编译型语言与解释型语言 编译器LLVM与CLang 理解iOS编译流程 ...

  • 深入浅出iOS编译

    前言 两年前曾经写过一篇关于编译的文章《iOS编译过程的原理和应用》,这篇文章介绍了iOS编译相关基础知识和简单应...

  • iOS重签名初探

    iOS签名原理 如上图,iOS的app编译完后会有几次签名处理。具体原理自己去百度,这里不细说。因为Apple私钥...

  • iOS-底层原理 31:LLVM编译流程 & Clang插件开发

    iOS-底层原理 31:LLVM编译流程 & Clang插件开发 本文主要是理解LLVM的编译流程以及clang插...

  • 打怪升级-中级iOS -- 你需要知道的

    iOS 编译原理和过程参考:http://blog.csdn.net/Hello_Hwc/article/deta...

  • iOS的编译原理和应用

    iOS的编译原理和应用 什么是编译和编译器 在一般的编程过程中,都要先编译再执行。所谓编译就是把C语言等编程语言编...

  • Swift 底层原理初探

    Swift 底层原理初探 1. 编译原理 在iOS中我们经常使用Objective-C和Swift这两门语言进行编...

网友评论

      本文标题:iOS编译原理

      本文链接:https://www.haomeiwen.com/subject/pxrooktx.html