编译器编译器相关

LLVM架构-编译原理

2018-08-12  本文已影响250人  那位小姐

什么是LLVM

传统的编译器架构

image.png

LLVM架构

image.png

什么是Clang?

image.png

OC源文件的编译过程

一.编译
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        NSLog(@"Hello, World!");
    }
    return 0;
}
//编译之后的代码
0: input, "main.m", objective-c   //声明 
1: preprocessor, {0}, objective-c-cpp-output // //预处理
2: compiler, {1}, ir //编译生成IR(中间代码)
3: backend, {2}, assembler //汇编器生成汇编代码
4: assembler, {3}, object  //目标代码生成机器码
5: linker, {4}, image  // 静态库,动态库链接
6: bind-arch, "x86_64", {5}, image //可执行文件

编译器前端的工作

二.preprocessor(预处理)
#import <Foundation/Foundation.h>
#define AGE 40
int main(int argc, const char * argv[]) {
     int a = 10;
     int b = 10;
     int c = a + b + AGE;
    return 0;
}

preprocessor 预处理的结果

int main(int argc, const char * argv[]) {
     int a = 10;
     int b = 10;
     int c = a + b + 40;
    return 0;
}
//预处理就已经把宏替换了
三.词法分析
 clang -fmodules -E -Xclang -dump-tokens main.m
annot_module_include '#import <Foundation/Foundation.h>

#define AGE 40

int main(int argc, const char * argv[]) {
    
    int a = 10;
    int b = 10;
    in'     Loc=<main.m:9:1> //第9行的第1个字符
int 'int'    [StartOfLine]  Loc=<main.m:13:1> //第13行的第一个1字符
identifier 'main'    [LeadingSpace] Loc=<main.m:13:5>
l_paren '('     Loc=<main.m:13:9>
int 'int'       Loc=<main.m:13:10>
identifier 'argc'    [LeadingSpace] Loc=<main.m:13:14>
comma ','       Loc=<main.m:13:18>
const 'const'    [LeadingSpace] Loc=<main.m:13:20>
char 'char'  [LeadingSpace] Loc=<main.m:13:26>
star '*'     [LeadingSpace] Loc=<main.m:13:31>
identifier 'argv'    [LeadingSpace] Loc=<main.m:13:33>
l_square '['        Loc=<main.m:13:37>
r_square ']'        Loc=<main.m:13:38>
r_paren ')'     Loc=<main.m:13:39>
l_brace '{'  [LeadingSpace] Loc=<main.m:13:41>
int 'int'    [StartOfLine] [LeadingSpace]   Loc=<main.m:15:5>
identifier 'a'   [LeadingSpace] Loc=<main.m:15:9>
equal '='    [LeadingSpace] Loc=<main.m:15:11>
numeric_constant '10'    [LeadingSpace] Loc=<main.m:15:13>
semi ';'        Loc=<main.m:15:15>
int 'int'    [StartOfLine] [LeadingSpace]   Loc=<main.m:16:5>
identifier 'b'   [LeadingSpace] Loc=<main.m:16:9>
equal '='    [LeadingSpace] Loc=<main.m:16:11>
numeric_constant '10'    [LeadingSpace] Loc=<main.m:16:13>
semi ';'        Loc=<main.m:16:15>
int 'int'    [StartOfLine] [LeadingSpace]   Loc=<main.m:17:5>
identifier 'c'   [LeadingSpace] Loc=<main.m:17:9>
equal '='    [LeadingSpace] Loc=<main.m:17:11>
identifier 'a'   [LeadingSpace] Loc=<main.m:17:13>
plus '+'     [LeadingSpace] Loc=<main.m:17:15>
identifier 'b'   [LeadingSpace] Loc=<main.m:17:17>
plus '+'     [LeadingSpace] Loc=<main.m:17:19>
numeric_constant '40'    [LeadingSpace] Loc=<main.m:17:21 <Spelling=main.m:11:13>>
semi ';'        Loc=<main.m:17:24>
return 'return'  [StartOfLine] [LeadingSpace]   Loc=<main.m:18:5>
numeric_constant '0'     [LeadingSpace] Loc=<main.m:18:12>
semi ';'        Loc=<main.m:18:13>
r_brace '}'  [StartOfLine]  Loc=<main.m:20:1>
eof ''      Loc=<main.m:20:2>

将代码分成一段一段的token

五.语法树-AST
//源代码
void test(int a, int b){
    int c = a + b -3;
}
//转换为语法树之后
-FunctionDecl 0x7fa7f0184300 <line:22:1, line:24:1> line:22:6 test 'void (int, int)' //
| |-ParmVarDecl 0x7fa7f0184180 <col:11, col:15> col:15 used a 'int'
| |-ParmVarDecl 0x7fa7f01841f8 <col:18, col:22> col:22 used b 'int'
| `-CompoundStmt 0x7fa7f0184540 <col:24, line:24:1>
|   `-DeclStmt 0x7fa7f0184528 <line:23:5, col:21>
|     `-VarDecl 0x7fa7f01843d8 <col:5, col:20> col:9 c 'int' cinit
|       `-BinaryOperator 0x7fa7f0184500 <col:13, col:20> 'int' '-'
|         |-BinaryOperator 0x7fa7f01844b8 <col:13, col:17> 'int' '+'
|         | |-ImplicitCastExpr 0x7fa7f0184488 <col:13> 'int' <LValueToRValue>
|         | | `-DeclRefExpr 0x7fa7f0184438 <col:13> 'int' lvalue ParmVar 0x7fa7f0184180 'a' 'int'
|         | `-ImplicitCastExpr 0x7fa7f01844a0 <col:17> 'int' <LValueToRValue>
|         |   `-DeclRefExpr 0x7fa7f0184460 <col:17> 'int' lvalue ParmVar 0x7fa7f01841f8 'b' 'int'
|         `-IntegerLiteral 0x7fa7f01844e0 <col:20> 'int' 3
  `-BinaryOperator 0x7fa7f0184500 <col:13, col:20> 'int' '-'
|         |-BinaryOperator 0x7fa7f01844b8 <col:13, col:17> 'int' '+'
|         | |-ImplicitCastExpr 0x7fa7f0184488 <col:13> 'int' <LValueToRValue>
|         | | `-DeclRefExpr 0x7fa7f0184438 <col:13> 'int' lvalue ParmVar 0x7fa7f0184180 'a' 'int'
|         | `-ImplicitCastExpr 0x7fa7f01844a0 <col:17> 'int' <LValueToRValue>
|         |   `-DeclRefExpr 0x7fa7f0184460 <col:17> 'int' lvalue ParmVar 0x7fa7f01841f8 'b' 'int'
|         `-IntegerLiteral 0x7fa7f01844e0 <col:20> 'int' 3

单独分析一下这段代码

//-  13-20里面装着 - 号
-BinaryOperator 0x7fa7f0184500 <col:13, col:20> 'int' '-'
//减号前面是3
`-IntegerLiteral 0x7fa7f01844e0 <col:20> 'int' 3
//13-17里面是+
|-BinaryOperator 0x7fa7f01844b8 <col:13, col:17> 'int' '+'
//13 是a
`-DeclRefExpr 0x7fa7f0184438 <col:13> 'int' lvalue ParmVar 0x7fa7f0184180 'a' 'int'
//17是b
`-DeclRefExpr 0x7fa7f0184460 <col:17> 'int' lvalue ParmVar 0x7fa7f01841f8 'b' 'int'
image.png

前端代码已经完成(就会生成中间代码IR)

中间代码IR

LLVM IR

; Function Attrs: noinline nounwind optnone ssp uwtable
define void @test(i32, i32) #0 { //(int a, int b)
  %3 = alloca i32, align 4 // int a
  %4 = alloca i32, align 4 // int d
  %5 = alloca i32, align 4 // int e
  store i32 %0, i32* %3, align 4 // c = a
  store i32 %1, i32* %4, align 4 // d = b
  %6 = load i32, i32* %3, align 4 //a
  %7 = load i32, i32* %4, align 4 //b
  %8 = add nsw i32 %6, %7  // a+b
  %9 = sub nsw i32 %8, 3    //a+b -3
  store i32 %9, i32* %5, align 4  //e = a+b-3  
  ret void
}
}

IR基本语法

LLVM源码下载

未完待续......

上一篇下一篇

猜你喜欢

热点阅读