前面一个章节已经简单介绍了LLVM。该章节主要介绍LLVM的编译过程。
1. OC源文件
// Command Line Tool
#include <stdio.h>
#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 <stdio.h>里的内容
int main(int argc, const char * argv[]) {
int a = 10;
int b = 20;
int c = a + b + 30; //这里宏定义的AGE已经被替换掉了
return 0;
}
-
词法分析,生成Token:
clang -fmodules -E -Xclang -dump-tokens main.m
annot_module_include '#include <stdio.h>
.#define AGE 30
int main(int argc, const char * argv[]) {
int a = 10;
int b = 20;
int c = a + b + A' Loc=<main.m:9:1>
int'int'
[StartOfLine] Loc=<main.m:11:1>//m:11:14 第11行第14个字符
identifier'main'
[LeadingSpace] Loc=<main.m:11:5>
l_paren'('
Loc=<main.m:11:9>
int'int'
Loc=<main.m:11:10>
identifier'argc'
[LeadingSpace] Loc=<main.m:11:14>
comma','
Loc=<main.m:11:18>
const'const'
[LeadingSpace] Loc=<main.m:11:20>
char'char'
[LeadingSpace] Loc=<main.m:11:26>
star'*'
[LeadingSpace] Loc=<main.m:11:31>
identifier'argv'
[LeadingSpace] Loc=<main.m:11:33>
l_square'['
Loc=<main.m:11:37>
r_square']'
Loc=<main.m:11:38>
r_paren')'
Loc=<main.m:11:39>
l_brace'{'
[LeadingSpace] Loc=<main.m:11:41>
int'int'
[StartOfLine] [LeadingSpace] Loc=<main.m:12:5>
identifier'a'
[LeadingSpace] Loc=<main.m:12:9>
equal'='
[LeadingSpace] Loc=<main.m:12:11>
numeric_constant'10'
[LeadingSpace] Loc=<main.m:12:13>
semi';'
Loc=<main.m:12:15>
int'int'
[StartOfLine] [LeadingSpace] Loc=<main.m:13:5>
identifier'b'
[LeadingSpace] Loc=<main.m:13:9>
equal'='
[LeadingSpace] Loc=<main.m:13:11>
numeric_constant'20'
[LeadingSpace] Loc=<main.m:13:13>
semi';'
Loc=<main.m:13:15>
int'int'
[StartOfLine] [LeadingSpace] Loc=<main.m:14:5>
identifier'c'
[LeadingSpace] Loc=<main.m:14:9>
equal'='
[LeadingSpace] Loc=<main.m:14:11>
identifier'a'
[LeadingSpace] Loc=<main.m:14:13>
plus'+'
[LeadingSpace] Loc=<main.m:14:15>
identifier'b'
[LeadingSpace] Loc=<main.m:14:17>
plus'+
' [LeadingSpace] Loc=<main.m:14:19>
numeric_constant'30'
[LeadingSpace] Loc=<main.m:14:21 <Spelling=main.m:10:13>>
semi';'
Loc=<main.m:14:24>
return'return'
[StartOfLine] [LeadingSpace] Loc=<main.m:16:5>
numeric_constant'0'
[LeadingSpace] Loc=<main.m:16:12>
semi';'
Loc=<main.m:16:13>
r_brace'}'
[StartOfLine] Loc=<main.m:17:1>
eof '' Loc=<main.m:17:2>
- 词法分析,生成语法树(AST,Abstract Syntax Tree):
- 主要看FunctionDecl之后。main函数语法树
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
读取数据