LLVM 编译过程 ②

前面一个章节已经简单介绍了LLVM。该章节主要介绍LLVM的编译过程。

1. OC源文件

// Command Line Tool

#include 
#define AGE 30
int main(int argc, const char * argv[]) {
    int a = 10;
    int b = 20;
    int c = a + b + AGE;
    
    return 0;
}

2. 编译过程

  • 命令行查看编译过程clang -ccc-print-phases main.m

0: input, "main.m", objective-c //找到原代码main.m
1: preprocessor, {0}, objective-c-cpp-output //预处理器 导入include import 及换掉宏定义
2: compiler, {1}, ir //编译器 中间代码生成ir
3: backend, {2}, assembler// 后端,{2},汇编程序
4: assembler, {3}, object// 汇编程序 生成目标代码
5: linker, {4}, image// 链接器 --- 动态库、静态库
6: bind-arch, "x86_64", {5}, image //生成适合某个架构的代码

  • 查看preprocessor(预处理)的结果clang -E main.m

...
...
...
extern int __vsnprintf_chk (char * restrict, size_t, int, size_t,
const char * restrict, va_list);
412 "/usr/include/stdio.h" 2 3 4
10 "main.m" 2
上面的一大段为:导入include 里的内容
int main(int argc, const char * argv[]) {
int a = 10;
int b = 20;
int c = a + b + 30; //这里宏定义的AGE已经被替换掉了
return 0;
}

  • 词法分析,生成Tokenclang -fmodules -E -Xclang -dump-tokens main.m

annot_module_include '#include
.#define AGE 30
int main(int argc, const char * argv[]) {
int a = 10;
int b = 20;
int c = a + b + A' Loc=
int 'int' [StartOfLine] Loc=//m:11:14 第11行第14个字符
identifier 'main' [LeadingSpace] Loc=
l_paren'(' Loc=
int 'int' Loc=
identifier 'argc' [LeadingSpace] Loc=
comma',' Loc=
const'const' [LeadingSpace] Loc=
char'char' [LeadingSpace] Loc=
star'*' [LeadingSpace] Loc=
identifier'argv' [LeadingSpace] Loc=
l_square '[' Loc=
r_square ']' Loc=
r_paren ')' Loc=
l_brace '{' [LeadingSpace] Loc=
int 'int' [StartOfLine] [LeadingSpace] Loc=
identifier 'a' [LeadingSpace] Loc=
equal '=' [LeadingSpace] Loc=
numeric_constant '10' [LeadingSpace] Loc=
semi';' Loc=
int'int' [StartOfLine] [LeadingSpace] Loc=
identifier 'b' [LeadingSpace] Loc=
equal'=' [LeadingSpace] Loc=
numeric_constant '20' [LeadingSpace] Loc=
semi ';' Loc=
int 'int' [StartOfLine] [LeadingSpace] Loc=
identifier 'c' [LeadingSpace] Loc=
equal '=' [LeadingSpace] Loc=
identifier 'a' [LeadingSpace] Loc=
plus '+' [LeadingSpace] Loc=
identifier'b' [LeadingSpace] Loc=
plus '+' [LeadingSpace] Loc=
numeric_constant'30' [LeadingSpace] Loc=>
semi';' Loc=
return 'return' [StartOfLine] [LeadingSpace] Loc=
numeric_constant'0' [LeadingSpace] Loc=
semi';' Loc=
r_brace'}' [StartOfLine] Loc=
eof '' Loc=

  • 词法分析,生成语法树(AST,Abstract Syntax Tree)

- 主要看FunctionDecl之后。main函数语法树

image.png

单词解释.png

3. LLVM IR(中间代码)

LLVM IR 有三种表现形式(但本质是等价的,就好比水有气态、液态、固态三种形式)

  • text:便于阅读的文本格式,类似于汇编语言,拓展名 .ll ,$clang -S -emit-llvm main.m
  • memory:内存格式
  • bitcode:二进制格式,拓展名 .bc , $clang -c -emit-llvm main.m
//$clang -S -emit-llvm main.m
; Function Attrs: noinline nounwind optnone ssp uwtable
define i32 @main(i32, i8**) #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i8**, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store i32 0, i32* %3, align 4
  store i32 %0, i32* %4, align 4
  store i8** %1, i8*** %5, align 8
  store i32 10, i32* %6, align 4
  store i32 20, i32* %7, align 4
  %9 = load i32, i32* %6, align 4
  %10 = load i32, i32* %7, align 4
  %11 = add nsw i32 %9, %10
  %12 = add nsw i32 %11, 30
  store i32 %12, i32* %8, align 4
  ret i32 0
}
IR基本语法
  • 注释以分号;开头
  • 全局标识以@开头,局部标识以%开头
  • alloca 在当前函数栈帧中分配内存
  • i32 32bit 4个字节的意思
  • align 内存对齐
  • store 写入数据
  • load 读取数据

你可能感兴趣的:(LLVM 编译过程 ②)