preface

LLVM builds and runs are often used in iOS development. We don’t really care about how it works in development, but as a hobbyist, with a basic understanding of this information, let’s start with the interpreter and compiler

The interpreter

The interpreter performs direct line-by-line interpretation of application code

  1. Python, for example, is a scripting language, which is essentially an interpreted language
  2. throughtouch hello.pyCreate a new.py file in the file path
  3. Print (“hello”);
  4. This can be done directly with Python Hello.py
  5. The Python interpreter interprets execution directly and prints Hello

The compiler

The compiler does intermediate processing of the application code into an executable and then executes it

  1. To create a hello.c file, use clang to compile it into the corresponding a.out executable file
  2. The hello.c file needs to be compiled by clang into the corresponding a.out executable file
  3. Executing output
Traditional compiler

Traditional compilers are divided into Frontend Frontend, Optimizer, and Backend end. A process is executed, and each process is associated

Compiler front end

  • Parsing source code, including lexical analysis, syntax analysis, semantic analysis, through semantic analysis can Abstract syntax Tree

The optimizer

  • Application code is optimized, redundant code, reduced compilation time, etc., as shown in BuildSetting in Xcode

The back-end

  • It can also be called a CodeGenerator
  • Into the corresponding assembly code, mapping to the corresponding CPU instruction set, generating machine language
  • You can also optimize your code

LLVM

  1. LLVM is a compiler architecture system that separates the front and back ends to optimize compile time, link time, run time, and idle time

  • LLVM can generate the corresponding IR code to represent the form of the code
  • You can write the front and back ends of any program

Clang

Clang is a subproject of LLVM used to compile the C/C++ Objc compiler, which belongs to the compiler front-end

The compilation process

The compiled flow can be completed via clang-ccC-print-Phases main.m main.m and is the target file output as follows

+- 0: input, "main.m", objective-c
+- 1: preprocessor, {0}, objective-c-cpp-output
+- 2: compiler, {1}, ir
+- 3: backend, {2}, assembler
+- 4: assembler, {3}, object
+- 5: linker, {4}, image
6: bind-arch, "x86_64", {5}, image
Copy the code
  1. The input file

  2. Preprocessing stage, including macro definition replacement, header file import, etc

  3. Compilation phase

    • Lexical analysis, generate one token at a time
    • Syntax analysis, generate abstract syntax tree
    • Check whether the syntax is correct, and finally generate IR
  4. The back-end LLVM is optimized one pass at a time to generate assembly code

  5. Assembler generates the corresponding object file

  6. Link, link required dynamic libraries and static libraries to generate executable files

  7. Generate corresponding executables through different schema generation

Links and bindings

  1. Link: When generating the executable file, you need to link to the required library, for example, when the code needs Print, you need to link to the external library libsystem, at compile time
  2. Bind: Binds at runtime from the address in libsystem, and when the program code executes the procedure, calls are made from this bound address

The advantages of LLVM

  1. Front end separation
  2. The optimization thought is relatively ideal