编译器入门:没有siri的那些年,我们如何实现人机对话?

开发 开发工具
编译器可将源代码转换成计算机理解的可执行的机器代码,或将源代码转换成另一种编程语言。本文从 LLVM 入手介绍了编译器工具。

编译器可将源代码转换成计算机理解的可执行的机器代码,或将源代码转换成另一种编程语言。本文从 LLVM 入手介绍了编译器工具。

编译器不过就是一个翻译其它程序的程序。传统的编译器将源代码转换成计算机可理解的可执行的机器代码。(一些编译器将源代码转换为另一种编程语言,这些编译器被称为源到源转换器或转译器)。LLVM 是一个广泛使用的编译器项目,包括多个模块化的编译器工具。

传统的编译器设计包括三个部分:

传统的编译器设计包括三个部分

  • 前端将源代码转换成一种中间表示(IR)。clang (http://clang.llvm.org/) 是 LLVM 项目中 C 类语言的前端工具。
  • 优化器解析 IR 并将其转换成一种更高效的形式。opt是 LLVM 项目的优化器工具。
  • 后端通过将 IR 映射到目标硬件指令集上来生成机器代码。llc 是 LLVM 项目的后端工具。

LLVM IR 是一种类似汇编的低级语言。但是,它不针对特定的硬件信息编程。

你好,编译器

下面是一个简单的打印「Hello,Compiler」字符串的 C 语言程序。虽然程序员可以读懂 C 语言语法,但是计算机却看的一脸懵逼。接下来我要过一遍编译的三个阶段,以便将以下程序转换成机器可执行的程序。

  1. // compile_me.c 
  2. // Wave to the compiler. The world can wait. 
  3.  
  4. #include <stdio.h> 
  5.  
  6. int main() { 
  7.   printf("Hello, Compiler!\n"); 
  8.   return 0; 

前端

前文讲到,clang 是 LLVM C 类语言的前端工具。Clang 由一个 C 预处理器、词法分析器(lexer)、解析器、语义分析器和中间表示生成器组成。

C 预处理器在源代码转换成 IR 之前对其进行修改。预处理器会将外部文件包含进来,比如上面的 #include 。它会用 C 标准库文件 stdio.h 的所有代码替换 #include 这一行,stdio.h 头文件包含了 printf 函数的声明。通过执行以下命令观察预处理器的输出:

  1. clang -E compile_me.c -o preprocessed.i 

词法分析器(Lexer,也叫 scanner 或 tokenizer)将一串字符转换成一串词。每个词或符号,按其属性被分配到对应的句法类别:标点符号、关键词、标识符、常量或注释。

compile_me.c 的词法分析:

compile_me.c 的词法分析

解析器判定由词法分析器生成的一串词是否包含源语言中的有效语句。在分析完词的语法以后,解析器输出了一个抽象语法树(AST)。Clang AST 中的节点分别表示声明与类型。

compile_me.c 的 AST:

compile_me.c 的 AST

语义分析器遍历 AST,判定语句的涵义是否有效。这个阶段会检查类型错误。如果 compile_me.c 中的 main 函数返回了 "zero" 而不是 0, 语义分析器就会抛出一个错误,因为 "zero" 不是 int 类型。

IR 生成器将 AST 转换为 IR。

在 compile_me.c 上运行 clang 前端,生成 LLVM IR:

  1. clang -S -emit-llvm -o llvm_ir.ll compile_me.c 

llvm_ir.ll 中的 main 函数:

  1. ; llvm_ir.ll 
  2.  
  3. @.str = private unnamed_addr constant [18 x i8] c"Hello, Compiler!\0A\00", align 1 
  4.  
  5. define i32 @main() { 
  6.   %1 = alloca i32, align 4 ; <- memory allocated on the stack 
  7.   store i32 0, i32* %1, align 4 
  8.   %2 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i32 0, i32 0)) ret i32 0 
  9.  
  10.  
  11. declare i32 @printf(i8*, ...) 

优化器

优化器的任务是基于对程序运行时行为的理解,提升代码的效率。优化器的输入为 IR,输出为优化后的 IR。LLVM 的优化器工具 opt 将使用 -O2(大写字母 o,数字 2)标记优化处理器速度,使用-Os(大写字母 o,s)标记优化生成目标的大小。

看一下优化器优化之前的 LLVM IR 代码和优化后的代码:

  1. opt -O2 -S llvm_ir.ll -o optimized.ll 

optimized.ll 的 main 函数:

  1. ; optimized.ll 
  2.  
  3. @str = private unnamed_addr constant [17 x i8] c"Hello, Compiler!\00" 
  4.  
  5. define i32 @main() { 
  6.  
  7.   %puts = tail call i32 @puts(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0)) ret i32 0 
  8.  
  9.  
  10. declare i32 @puts(i8* nocapture readonly) 

优化后,main 函数没有在栈上分配内存,因为它没有使用任何内存。优化后的代码调用了 puts 函数而不是 printf 函数,因为它没有使用 printf 函数的任何格式化功能。当然了,优化器不仅仅知道什么时候该用 puts 代替 printf。优化器也会展开循环,内联简单计算的结果。思考以下代码,它将两个数加起来并打印结果:

  1. // add.c 
  2. #include <stdio.h> 
  3.  
  4. int main() { 
  5.   int a = 5b = 10c = a + b; 
  6.   printf("%i + %i = %i\n", a, b, c); 

未优化的 LLVM IR:

  1. @.str = private unnamed_addr constant [14 x i8] c"%i + %i = %i\0A\00", align 1 
  2.  
  3. define i32 @main() { 
  4.   %1 = alloca i32, align 4 ; <- allocate stack space for var a 
  5.   %2 = alloca i32, align 4 ; <- allocate stack space for var b 
  6.   %3 = alloca i32, align 4 ; <- allocate stack space for var c 
  7.   store i32 5, i32* %1, align 4  ; <- store 5 at memory location %1 
  8.   store i32 10, i32* %2, align 4 ; <- store 10 at memory location %2 
  9.   %4 = load i32, i32* %1, align 4 ; <- load the value at memory address %1 into register %4 
  10.   %5 = load i32, i32* %2, align 4 ; <- load the value at memory address %2 into register %5 
  11.   %6 = add nsw i32 %4, %5 ; <- add the values in registers %4 and %5. put the result in register %6 
  12.   store i32 %6, i32* %3, align 4 ; <- put the value of register %6 into memory address %3 
  13.   %7 = load i32, i32* %1, align 4 ; <- load the value at memory address %1 into register %7 
  14.   %8 = load i32, i32* %2, align 4 ; <- load the value at memory address %2 into register %8 
  15.   %9 = load i32, i32* %3, align 4 ; <- load the value at memory address %3 into register %9 
  16.   %10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i32 0, i32 0), i32 %7, i32 %8, i32 %9) 
  17.   ret i32 0 
  18.  
  19. declare i32 @printf(i8*, ...) 

优化后的 LLVM IR:

  1. @.str = private unnamed_addr constant [14 x i8] c"%i + %i = %i\0A\00", align 1 
  2.  
  3. define i32 @main() { 
  4.   %1 = tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 5, i32 10, i32 15) 
  5.   ret i32 0 
  6.  
  7. declare i32 @printf(i8* nocapture readonly, ...) 

优化后的 main 函数实际上就是在未优化版本的 17 和 18 行将变量进行内联。opt 对加法进行运算,因为所有的变量都是常量。很酷吧?

后端

LLVM 的后端工具是 llc。它经历了三个阶段,最终把 LLVM IR 输入转化生成机器代码:

  • 指令选取(instruction selection)是从 IR 指令到目标机器指令集的映射。这一步使用了虚拟寄存器一个***的命名空间。
  • 寄存器分配(register allocation)是从虚拟寄存器到目标架构真实寄存器的映射。我的 CPU 是 x86 架构的,也就是说只能使用 16 个寄存器。但是,编译器会尽可能少地使用寄存器。
  • 指令调度(instruction scheduling)是对操作的重新安排,它反映了目标机器上的性能限制。

执行以下命令将生成部分机器代码!

  1. llc -o compiled-assembly.s optimized.ll 
  1. _main: 
  2.     pushq   %rbp 
  3.     movq    %rsp, %rbp 
  4.     leaq    L_str(%rip), %rdi 
  5.     callq   _puts 
  6.     xorl    %eax, %eax 
  7.     popq    %rbp 
  8.     retq 
  9. L_str: 
  10.     .asciz  "Hello, Compiler!" 

这是一个 x86 汇编语言程序,是计算机和程序员共通的语言。看似晦涩,但肯定有人懂我。

原文:https://nicoleorchard.com/blog/compilers

【本文是51CTO专栏机构“机器之心”的原创译文,微信公众号“机器之心( id: almosthuman2014)”】

 

戳这里,看该作者更多好文

责任编辑:赵宁宁 来源: 51CTO专栏
相关推荐

2020-10-20 11:16:04

人工智能

2010-01-27 16:39:48

C++编译器

2021-06-08 07:48:26

lambda表达式编译器

2017-03-20 18:01:55

编译器汇编

2017-12-06 17:48:42

谷歌AI人机对话

2022-03-28 10:25:27

前端文件编译器

2018-04-27 14:39:28

物联网物联网应用智能

2016-11-08 18:53:08

编译器

2015-08-14 09:49:25

u3dc#

2022-05-30 11:46:29

GNU C 编译器的

2010-01-14 15:29:44

C++编译器

2020-08-17 17:05:08

人工智能机器学习技术

2013-10-31 10:44:54

IDE工具

2010-03-23 11:17:16

Python 动态编译

2023-11-15 17:58:58

C++代码

2010-10-20 13:43:37

C++编译器

2021-06-25 10:38:05

JavaScript编译器前端开发

2019-08-08 16:37:31

开源技术 数据

2015-03-23 10:04:43

c++编译器c++实现原理总结

2022-08-02 08:11:41

监控埋点埋点方式插桩
点赞
收藏

51CTO技术栈公众号